domain.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Thunderbolt bus support
  4. *
  5. * Copyright (C) 2017, Intel Corporation
  6. * Author: Mika Westerberg <mika.westerberg@linux.intel.com>
  7. */
  8. #include <linux/device.h>
  9. #include <linux/dmar.h>
  10. #include <linux/idr.h>
  11. #include <linux/iommu.h>
  12. #include <linux/module.h>
  13. #include <linux/pm_runtime.h>
  14. #include <linux/slab.h>
  15. #include <linux/random.h>
  16. #include <crypto/hash.h>
  17. #include "tb.h"
  18. static DEFINE_IDA(tb_domain_ida);
  19. static bool match_service_id(const struct tb_service_id *id,
  20. const struct tb_service *svc)
  21. {
  22. if (id->match_flags & TBSVC_MATCH_PROTOCOL_KEY) {
  23. if (strcmp(id->protocol_key, svc->key))
  24. return false;
  25. }
  26. if (id->match_flags & TBSVC_MATCH_PROTOCOL_ID) {
  27. if (id->protocol_id != svc->prtcid)
  28. return false;
  29. }
  30. if (id->match_flags & TBSVC_MATCH_PROTOCOL_VERSION) {
  31. if (id->protocol_version != svc->prtcvers)
  32. return false;
  33. }
  34. if (id->match_flags & TBSVC_MATCH_PROTOCOL_VERSION) {
  35. if (id->protocol_revision != svc->prtcrevs)
  36. return false;
  37. }
  38. return true;
  39. }
  40. static const struct tb_service_id *__tb_service_match(struct device *dev,
  41. struct device_driver *drv)
  42. {
  43. struct tb_service_driver *driver;
  44. const struct tb_service_id *ids;
  45. struct tb_service *svc;
  46. svc = tb_to_service(dev);
  47. if (!svc)
  48. return NULL;
  49. driver = container_of(drv, struct tb_service_driver, driver);
  50. if (!driver->id_table)
  51. return NULL;
  52. for (ids = driver->id_table; ids->match_flags != 0; ids++) {
  53. if (match_service_id(ids, svc))
  54. return ids;
  55. }
  56. return NULL;
  57. }
  58. static int tb_service_match(struct device *dev, struct device_driver *drv)
  59. {
  60. return !!__tb_service_match(dev, drv);
  61. }
  62. static int tb_service_probe(struct device *dev)
  63. {
  64. struct tb_service *svc = tb_to_service(dev);
  65. struct tb_service_driver *driver;
  66. const struct tb_service_id *id;
  67. driver = container_of(dev->driver, struct tb_service_driver, driver);
  68. id = __tb_service_match(dev, &driver->driver);
  69. return driver->probe(svc, id);
  70. }
  71. static int tb_service_remove(struct device *dev)
  72. {
  73. struct tb_service *svc = tb_to_service(dev);
  74. struct tb_service_driver *driver;
  75. driver = container_of(dev->driver, struct tb_service_driver, driver);
  76. if (driver->remove)
  77. driver->remove(svc);
  78. return 0;
  79. }
  80. static void tb_service_shutdown(struct device *dev)
  81. {
  82. struct tb_service_driver *driver;
  83. struct tb_service *svc;
  84. svc = tb_to_service(dev);
  85. if (!svc || !dev->driver)
  86. return;
  87. driver = container_of(dev->driver, struct tb_service_driver, driver);
  88. if (driver->shutdown)
  89. driver->shutdown(svc);
  90. }
  91. static const char * const tb_security_names[] = {
  92. [TB_SECURITY_NONE] = "none",
  93. [TB_SECURITY_USER] = "user",
  94. [TB_SECURITY_SECURE] = "secure",
  95. [TB_SECURITY_DPONLY] = "dponly",
  96. [TB_SECURITY_USBONLY] = "usbonly",
  97. };
  98. static ssize_t boot_acl_show(struct device *dev, struct device_attribute *attr,
  99. char *buf)
  100. {
  101. struct tb *tb = container_of(dev, struct tb, dev);
  102. uuid_t *uuids;
  103. ssize_t ret;
  104. int i;
  105. uuids = kcalloc(tb->nboot_acl, sizeof(uuid_t), GFP_KERNEL);
  106. if (!uuids)
  107. return -ENOMEM;
  108. pm_runtime_get_sync(&tb->dev);
  109. if (mutex_lock_interruptible(&tb->lock)) {
  110. ret = -ERESTARTSYS;
  111. goto out;
  112. }
  113. ret = tb->cm_ops->get_boot_acl(tb, uuids, tb->nboot_acl);
  114. if (ret) {
  115. mutex_unlock(&tb->lock);
  116. goto out;
  117. }
  118. mutex_unlock(&tb->lock);
  119. for (ret = 0, i = 0; i < tb->nboot_acl; i++) {
  120. if (!uuid_is_null(&uuids[i]))
  121. ret += scnprintf(buf + ret, PAGE_SIZE - ret, "%pUb",
  122. &uuids[i]);
  123. ret += scnprintf(buf + ret, PAGE_SIZE - ret, "%s",
  124. i < tb->nboot_acl - 1 ? "," : "\n");
  125. }
  126. out:
  127. pm_runtime_mark_last_busy(&tb->dev);
  128. pm_runtime_put_autosuspend(&tb->dev);
  129. kfree(uuids);
  130. return ret;
  131. }
  132. static ssize_t boot_acl_store(struct device *dev, struct device_attribute *attr,
  133. const char *buf, size_t count)
  134. {
  135. struct tb *tb = container_of(dev, struct tb, dev);
  136. char *str, *s, *uuid_str;
  137. ssize_t ret = 0;
  138. uuid_t *acl;
  139. int i = 0;
  140. /*
  141. * Make sure the value is not bigger than tb->nboot_acl * UUID
  142. * length + commas and optional "\n". Also the smallest allowable
  143. * string is tb->nboot_acl * ",".
  144. */
  145. if (count > (UUID_STRING_LEN + 1) * tb->nboot_acl + 1)
  146. return -EINVAL;
  147. if (count < tb->nboot_acl - 1)
  148. return -EINVAL;
  149. str = kstrdup(buf, GFP_KERNEL);
  150. if (!str)
  151. return -ENOMEM;
  152. acl = kcalloc(tb->nboot_acl, sizeof(uuid_t), GFP_KERNEL);
  153. if (!acl) {
  154. ret = -ENOMEM;
  155. goto err_free_str;
  156. }
  157. uuid_str = strim(str);
  158. while ((s = strsep(&uuid_str, ",")) != NULL && i < tb->nboot_acl) {
  159. size_t len = strlen(s);
  160. if (len) {
  161. if (len != UUID_STRING_LEN) {
  162. ret = -EINVAL;
  163. goto err_free_acl;
  164. }
  165. ret = uuid_parse(s, &acl[i]);
  166. if (ret)
  167. goto err_free_acl;
  168. }
  169. i++;
  170. }
  171. if (s || i < tb->nboot_acl) {
  172. ret = -EINVAL;
  173. goto err_free_acl;
  174. }
  175. pm_runtime_get_sync(&tb->dev);
  176. if (mutex_lock_interruptible(&tb->lock)) {
  177. ret = -ERESTARTSYS;
  178. goto err_rpm_put;
  179. }
  180. ret = tb->cm_ops->set_boot_acl(tb, acl, tb->nboot_acl);
  181. if (!ret) {
  182. /* Notify userspace about the change */
  183. kobject_uevent(&tb->dev.kobj, KOBJ_CHANGE);
  184. }
  185. mutex_unlock(&tb->lock);
  186. err_rpm_put:
  187. pm_runtime_mark_last_busy(&tb->dev);
  188. pm_runtime_put_autosuspend(&tb->dev);
  189. err_free_acl:
  190. kfree(acl);
  191. err_free_str:
  192. kfree(str);
  193. return ret ?: count;
  194. }
  195. static DEVICE_ATTR_RW(boot_acl);
  196. static ssize_t iommu_dma_protection_show(struct device *dev,
  197. struct device_attribute *attr,
  198. char *buf)
  199. {
  200. /*
  201. * Kernel DMA protection is a feature where Thunderbolt security is
  202. * handled natively using IOMMU. It is enabled when IOMMU is
  203. * enabled and ACPI DMAR table has DMAR_PLATFORM_OPT_IN set.
  204. */
  205. return sprintf(buf, "%d\n",
  206. iommu_present(&pci_bus_type) && dmar_platform_optin());
  207. }
  208. static DEVICE_ATTR_RO(iommu_dma_protection);
  209. static ssize_t security_show(struct device *dev, struct device_attribute *attr,
  210. char *buf)
  211. {
  212. struct tb *tb = container_of(dev, struct tb, dev);
  213. const char *name = "unknown";
  214. if (tb->security_level < ARRAY_SIZE(tb_security_names))
  215. name = tb_security_names[tb->security_level];
  216. return sprintf(buf, "%s\n", name);
  217. }
  218. static DEVICE_ATTR_RO(security);
  219. static struct attribute *domain_attrs[] = {
  220. &dev_attr_boot_acl.attr,
  221. &dev_attr_iommu_dma_protection.attr,
  222. &dev_attr_security.attr,
  223. NULL,
  224. };
  225. static umode_t domain_attr_is_visible(struct kobject *kobj,
  226. struct attribute *attr, int n)
  227. {
  228. struct device *dev = kobj_to_dev(kobj);
  229. struct tb *tb = container_of(dev, struct tb, dev);
  230. if (attr == &dev_attr_boot_acl.attr) {
  231. if (tb->nboot_acl &&
  232. tb->cm_ops->get_boot_acl &&
  233. tb->cm_ops->set_boot_acl)
  234. return attr->mode;
  235. return 0;
  236. }
  237. return attr->mode;
  238. }
  239. static struct attribute_group domain_attr_group = {
  240. .is_visible = domain_attr_is_visible,
  241. .attrs = domain_attrs,
  242. };
  243. static const struct attribute_group *domain_attr_groups[] = {
  244. &domain_attr_group,
  245. NULL,
  246. };
  247. struct bus_type tb_bus_type = {
  248. .name = "thunderbolt",
  249. .match = tb_service_match,
  250. .probe = tb_service_probe,
  251. .remove = tb_service_remove,
  252. .shutdown = tb_service_shutdown,
  253. };
  254. static void tb_domain_release(struct device *dev)
  255. {
  256. struct tb *tb = container_of(dev, struct tb, dev);
  257. tb_ctl_free(tb->ctl);
  258. destroy_workqueue(tb->wq);
  259. ida_simple_remove(&tb_domain_ida, tb->index);
  260. mutex_destroy(&tb->lock);
  261. kfree(tb);
  262. }
  263. struct device_type tb_domain_type = {
  264. .name = "thunderbolt_domain",
  265. .release = tb_domain_release,
  266. };
  267. /**
  268. * tb_domain_alloc() - Allocate a domain
  269. * @nhi: Pointer to the host controller
  270. * @privsize: Size of the connection manager private data
  271. *
  272. * Allocates and initializes a new Thunderbolt domain. Connection
  273. * managers are expected to call this and then fill in @cm_ops
  274. * accordingly.
  275. *
  276. * Call tb_domain_put() to release the domain before it has been added
  277. * to the system.
  278. *
  279. * Return: allocated domain structure on %NULL in case of error
  280. */
  281. struct tb *tb_domain_alloc(struct tb_nhi *nhi, size_t privsize)
  282. {
  283. struct tb *tb;
  284. /*
  285. * Make sure the structure sizes map with that the hardware
  286. * expects because bit-fields are being used.
  287. */
  288. BUILD_BUG_ON(sizeof(struct tb_regs_switch_header) != 5 * 4);
  289. BUILD_BUG_ON(sizeof(struct tb_regs_port_header) != 8 * 4);
  290. BUILD_BUG_ON(sizeof(struct tb_regs_hop) != 2 * 4);
  291. tb = kzalloc(sizeof(*tb) + privsize, GFP_KERNEL);
  292. if (!tb)
  293. return NULL;
  294. tb->nhi = nhi;
  295. mutex_init(&tb->lock);
  296. tb->index = ida_simple_get(&tb_domain_ida, 0, 0, GFP_KERNEL);
  297. if (tb->index < 0)
  298. goto err_free;
  299. tb->wq = alloc_ordered_workqueue("thunderbolt%d", 0, tb->index);
  300. if (!tb->wq)
  301. goto err_remove_ida;
  302. tb->dev.parent = &nhi->pdev->dev;
  303. tb->dev.bus = &tb_bus_type;
  304. tb->dev.type = &tb_domain_type;
  305. tb->dev.groups = domain_attr_groups;
  306. dev_set_name(&tb->dev, "domain%d", tb->index);
  307. device_initialize(&tb->dev);
  308. return tb;
  309. err_remove_ida:
  310. ida_simple_remove(&tb_domain_ida, tb->index);
  311. err_free:
  312. kfree(tb);
  313. return NULL;
  314. }
  315. static bool tb_domain_event_cb(void *data, enum tb_cfg_pkg_type type,
  316. const void *buf, size_t size)
  317. {
  318. struct tb *tb = data;
  319. if (!tb->cm_ops->handle_event) {
  320. tb_warn(tb, "domain does not have event handler\n");
  321. return true;
  322. }
  323. switch (type) {
  324. case TB_CFG_PKG_XDOMAIN_REQ:
  325. case TB_CFG_PKG_XDOMAIN_RESP:
  326. return tb_xdomain_handle_request(tb, type, buf, size);
  327. default:
  328. tb->cm_ops->handle_event(tb, type, buf, size);
  329. }
  330. return true;
  331. }
  332. /**
  333. * tb_domain_add() - Add domain to the system
  334. * @tb: Domain to add
  335. *
  336. * Starts the domain and adds it to the system. Hotplugging devices will
  337. * work after this has been returned successfully. In order to remove
  338. * and release the domain after this function has been called, call
  339. * tb_domain_remove().
  340. *
  341. * Return: %0 in case of success and negative errno in case of error
  342. */
  343. int tb_domain_add(struct tb *tb)
  344. {
  345. int ret;
  346. if (WARN_ON(!tb->cm_ops))
  347. return -EINVAL;
  348. mutex_lock(&tb->lock);
  349. tb->ctl = tb_ctl_alloc(tb->nhi, tb_domain_event_cb, tb);
  350. if (!tb->ctl) {
  351. ret = -ENOMEM;
  352. goto err_unlock;
  353. }
  354. /*
  355. * tb_schedule_hotplug_handler may be called as soon as the config
  356. * channel is started. Thats why we have to hold the lock here.
  357. */
  358. tb_ctl_start(tb->ctl);
  359. if (tb->cm_ops->driver_ready) {
  360. ret = tb->cm_ops->driver_ready(tb);
  361. if (ret)
  362. goto err_ctl_stop;
  363. }
  364. ret = device_add(&tb->dev);
  365. if (ret)
  366. goto err_ctl_stop;
  367. /* Start the domain */
  368. if (tb->cm_ops->start) {
  369. ret = tb->cm_ops->start(tb);
  370. if (ret)
  371. goto err_domain_del;
  372. }
  373. /* This starts event processing */
  374. mutex_unlock(&tb->lock);
  375. device_init_wakeup(&tb->dev, true);
  376. pm_runtime_no_callbacks(&tb->dev);
  377. pm_runtime_set_active(&tb->dev);
  378. pm_runtime_enable(&tb->dev);
  379. pm_runtime_set_autosuspend_delay(&tb->dev, TB_AUTOSUSPEND_DELAY);
  380. pm_runtime_mark_last_busy(&tb->dev);
  381. pm_runtime_use_autosuspend(&tb->dev);
  382. return 0;
  383. err_domain_del:
  384. device_del(&tb->dev);
  385. err_ctl_stop:
  386. tb_ctl_stop(tb->ctl);
  387. err_unlock:
  388. mutex_unlock(&tb->lock);
  389. return ret;
  390. }
  391. /**
  392. * tb_domain_remove() - Removes and releases a domain
  393. * @tb: Domain to remove
  394. *
  395. * Stops the domain, removes it from the system and releases all
  396. * resources once the last reference has been released.
  397. */
  398. void tb_domain_remove(struct tb *tb)
  399. {
  400. mutex_lock(&tb->lock);
  401. if (tb->cm_ops->stop)
  402. tb->cm_ops->stop(tb);
  403. /* Stop the domain control traffic */
  404. tb_ctl_stop(tb->ctl);
  405. mutex_unlock(&tb->lock);
  406. flush_workqueue(tb->wq);
  407. device_unregister(&tb->dev);
  408. }
  409. /**
  410. * tb_domain_suspend_noirq() - Suspend a domain
  411. * @tb: Domain to suspend
  412. *
  413. * Suspends all devices in the domain and stops the control channel.
  414. */
  415. int tb_domain_suspend_noirq(struct tb *tb)
  416. {
  417. int ret = 0;
  418. /*
  419. * The control channel interrupt is left enabled during suspend
  420. * and taking the lock here prevents any events happening before
  421. * we actually have stopped the domain and the control channel.
  422. */
  423. mutex_lock(&tb->lock);
  424. if (tb->cm_ops->suspend_noirq)
  425. ret = tb->cm_ops->suspend_noirq(tb);
  426. if (!ret)
  427. tb_ctl_stop(tb->ctl);
  428. mutex_unlock(&tb->lock);
  429. return ret;
  430. }
  431. /**
  432. * tb_domain_resume_noirq() - Resume a domain
  433. * @tb: Domain to resume
  434. *
  435. * Re-starts the control channel, and resumes all devices connected to
  436. * the domain.
  437. */
  438. int tb_domain_resume_noirq(struct tb *tb)
  439. {
  440. int ret = 0;
  441. mutex_lock(&tb->lock);
  442. tb_ctl_start(tb->ctl);
  443. if (tb->cm_ops->resume_noirq)
  444. ret = tb->cm_ops->resume_noirq(tb);
  445. mutex_unlock(&tb->lock);
  446. return ret;
  447. }
  448. int tb_domain_suspend(struct tb *tb)
  449. {
  450. return tb->cm_ops->suspend ? tb->cm_ops->suspend(tb) : 0;
  451. }
  452. int tb_domain_freeze_noirq(struct tb *tb)
  453. {
  454. int ret = 0;
  455. mutex_lock(&tb->lock);
  456. if (tb->cm_ops->freeze_noirq)
  457. ret = tb->cm_ops->freeze_noirq(tb);
  458. if (!ret)
  459. tb_ctl_stop(tb->ctl);
  460. mutex_unlock(&tb->lock);
  461. return ret;
  462. }
  463. int tb_domain_thaw_noirq(struct tb *tb)
  464. {
  465. int ret = 0;
  466. mutex_lock(&tb->lock);
  467. tb_ctl_start(tb->ctl);
  468. if (tb->cm_ops->thaw_noirq)
  469. ret = tb->cm_ops->thaw_noirq(tb);
  470. mutex_unlock(&tb->lock);
  471. return ret;
  472. }
  473. void tb_domain_complete(struct tb *tb)
  474. {
  475. if (tb->cm_ops->complete)
  476. tb->cm_ops->complete(tb);
  477. }
  478. int tb_domain_runtime_suspend(struct tb *tb)
  479. {
  480. if (tb->cm_ops->runtime_suspend) {
  481. int ret = tb->cm_ops->runtime_suspend(tb);
  482. if (ret)
  483. return ret;
  484. }
  485. tb_ctl_stop(tb->ctl);
  486. return 0;
  487. }
  488. int tb_domain_runtime_resume(struct tb *tb)
  489. {
  490. tb_ctl_start(tb->ctl);
  491. if (tb->cm_ops->runtime_resume) {
  492. int ret = tb->cm_ops->runtime_resume(tb);
  493. if (ret)
  494. return ret;
  495. }
  496. return 0;
  497. }
  498. /**
  499. * tb_domain_approve_switch() - Approve switch
  500. * @tb: Domain the switch belongs to
  501. * @sw: Switch to approve
  502. *
  503. * This will approve switch by connection manager specific means. In
  504. * case of success the connection manager will create tunnels for all
  505. * supported protocols.
  506. */
  507. int tb_domain_approve_switch(struct tb *tb, struct tb_switch *sw)
  508. {
  509. struct tb_switch *parent_sw;
  510. if (!tb->cm_ops->approve_switch)
  511. return -EPERM;
  512. /* The parent switch must be authorized before this one */
  513. parent_sw = tb_to_switch(sw->dev.parent);
  514. if (!parent_sw || !parent_sw->authorized)
  515. return -EINVAL;
  516. return tb->cm_ops->approve_switch(tb, sw);
  517. }
  518. /**
  519. * tb_domain_approve_switch_key() - Approve switch and add key
  520. * @tb: Domain the switch belongs to
  521. * @sw: Switch to approve
  522. *
  523. * For switches that support secure connect, this function first adds
  524. * key to the switch NVM using connection manager specific means. If
  525. * adding the key is successful, the switch is approved and connected.
  526. *
  527. * Return: %0 on success and negative errno in case of failure.
  528. */
  529. int tb_domain_approve_switch_key(struct tb *tb, struct tb_switch *sw)
  530. {
  531. struct tb_switch *parent_sw;
  532. int ret;
  533. if (!tb->cm_ops->approve_switch || !tb->cm_ops->add_switch_key)
  534. return -EPERM;
  535. /* The parent switch must be authorized before this one */
  536. parent_sw = tb_to_switch(sw->dev.parent);
  537. if (!parent_sw || !parent_sw->authorized)
  538. return -EINVAL;
  539. ret = tb->cm_ops->add_switch_key(tb, sw);
  540. if (ret)
  541. return ret;
  542. return tb->cm_ops->approve_switch(tb, sw);
  543. }
  544. /**
  545. * tb_domain_challenge_switch_key() - Challenge and approve switch
  546. * @tb: Domain the switch belongs to
  547. * @sw: Switch to approve
  548. *
  549. * For switches that support secure connect, this function generates
  550. * random challenge and sends it to the switch. The switch responds to
  551. * this and if the response matches our random challenge, the switch is
  552. * approved and connected.
  553. *
  554. * Return: %0 on success and negative errno in case of failure.
  555. */
  556. int tb_domain_challenge_switch_key(struct tb *tb, struct tb_switch *sw)
  557. {
  558. u8 challenge[TB_SWITCH_KEY_SIZE];
  559. u8 response[TB_SWITCH_KEY_SIZE];
  560. u8 hmac[TB_SWITCH_KEY_SIZE];
  561. struct tb_switch *parent_sw;
  562. struct crypto_shash *tfm;
  563. struct shash_desc *shash;
  564. int ret;
  565. if (!tb->cm_ops->approve_switch || !tb->cm_ops->challenge_switch_key)
  566. return -EPERM;
  567. /* The parent switch must be authorized before this one */
  568. parent_sw = tb_to_switch(sw->dev.parent);
  569. if (!parent_sw || !parent_sw->authorized)
  570. return -EINVAL;
  571. get_random_bytes(challenge, sizeof(challenge));
  572. ret = tb->cm_ops->challenge_switch_key(tb, sw, challenge, response);
  573. if (ret)
  574. return ret;
  575. tfm = crypto_alloc_shash("hmac(sha256)", 0, 0);
  576. if (IS_ERR(tfm))
  577. return PTR_ERR(tfm);
  578. ret = crypto_shash_setkey(tfm, sw->key, TB_SWITCH_KEY_SIZE);
  579. if (ret)
  580. goto err_free_tfm;
  581. shash = kzalloc(sizeof(*shash) + crypto_shash_descsize(tfm),
  582. GFP_KERNEL);
  583. if (!shash) {
  584. ret = -ENOMEM;
  585. goto err_free_tfm;
  586. }
  587. shash->tfm = tfm;
  588. memset(hmac, 0, sizeof(hmac));
  589. ret = crypto_shash_digest(shash, challenge, sizeof(hmac), hmac);
  590. if (ret)
  591. goto err_free_shash;
  592. /* The returned HMAC must match the one we calculated */
  593. if (memcmp(response, hmac, sizeof(hmac))) {
  594. ret = -EKEYREJECTED;
  595. goto err_free_shash;
  596. }
  597. crypto_free_shash(tfm);
  598. kfree(shash);
  599. return tb->cm_ops->approve_switch(tb, sw);
  600. err_free_shash:
  601. kfree(shash);
  602. err_free_tfm:
  603. crypto_free_shash(tfm);
  604. return ret;
  605. }
  606. /**
  607. * tb_domain_disconnect_pcie_paths() - Disconnect all PCIe paths
  608. * @tb: Domain whose PCIe paths to disconnect
  609. *
  610. * This needs to be called in preparation for NVM upgrade of the host
  611. * controller. Makes sure all PCIe paths are disconnected.
  612. *
  613. * Return %0 on success and negative errno in case of error.
  614. */
  615. int tb_domain_disconnect_pcie_paths(struct tb *tb)
  616. {
  617. if (!tb->cm_ops->disconnect_pcie_paths)
  618. return -EPERM;
  619. return tb->cm_ops->disconnect_pcie_paths(tb);
  620. }
  621. /**
  622. * tb_domain_approve_xdomain_paths() - Enable DMA paths for XDomain
  623. * @tb: Domain enabling the DMA paths
  624. * @xd: XDomain DMA paths are created to
  625. *
  626. * Calls connection manager specific method to enable DMA paths to the
  627. * XDomain in question.
  628. *
  629. * Return: 0% in case of success and negative errno otherwise. In
  630. * particular returns %-ENOTSUPP if the connection manager
  631. * implementation does not support XDomains.
  632. */
  633. int tb_domain_approve_xdomain_paths(struct tb *tb, struct tb_xdomain *xd)
  634. {
  635. if (!tb->cm_ops->approve_xdomain_paths)
  636. return -ENOTSUPP;
  637. return tb->cm_ops->approve_xdomain_paths(tb, xd);
  638. }
  639. /**
  640. * tb_domain_disconnect_xdomain_paths() - Disable DMA paths for XDomain
  641. * @tb: Domain disabling the DMA paths
  642. * @xd: XDomain whose DMA paths are disconnected
  643. *
  644. * Calls connection manager specific method to disconnect DMA paths to
  645. * the XDomain in question.
  646. *
  647. * Return: 0% in case of success and negative errno otherwise. In
  648. * particular returns %-ENOTSUPP if the connection manager
  649. * implementation does not support XDomains.
  650. */
  651. int tb_domain_disconnect_xdomain_paths(struct tb *tb, struct tb_xdomain *xd)
  652. {
  653. if (!tb->cm_ops->disconnect_xdomain_paths)
  654. return -ENOTSUPP;
  655. return tb->cm_ops->disconnect_xdomain_paths(tb, xd);
  656. }
  657. static int disconnect_xdomain(struct device *dev, void *data)
  658. {
  659. struct tb_xdomain *xd;
  660. struct tb *tb = data;
  661. int ret = 0;
  662. xd = tb_to_xdomain(dev);
  663. if (xd && xd->tb == tb)
  664. ret = tb_xdomain_disable_paths(xd);
  665. return ret;
  666. }
  667. /**
  668. * tb_domain_disconnect_all_paths() - Disconnect all paths for the domain
  669. * @tb: Domain whose paths are disconnected
  670. *
  671. * This function can be used to disconnect all paths (PCIe, XDomain) for
  672. * example in preparation for host NVM firmware upgrade. After this is
  673. * called the paths cannot be established without resetting the switch.
  674. *
  675. * Return: %0 in case of success and negative errno otherwise.
  676. */
  677. int tb_domain_disconnect_all_paths(struct tb *tb)
  678. {
  679. int ret;
  680. ret = tb_domain_disconnect_pcie_paths(tb);
  681. if (ret)
  682. return ret;
  683. return bus_for_each_dev(&tb_bus_type, NULL, tb, disconnect_xdomain);
  684. }
  685. int tb_domain_init(void)
  686. {
  687. int ret;
  688. tb_test_init();
  689. tb_debugfs_init();
  690. ret = tb_xdomain_init();
  691. if (ret)
  692. goto err_debugfs;
  693. ret = bus_register(&tb_bus_type);
  694. if (ret)
  695. goto err_xdomain;
  696. return 0;
  697. err_xdomain:
  698. tb_xdomain_exit();
  699. err_debugfs:
  700. tb_debugfs_exit();
  701. tb_test_exit();
  702. return ret;
  703. }
  704. void tb_domain_exit(void)
  705. {
  706. bus_unregister(&tb_bus_type);
  707. ida_destroy(&tb_domain_ida);
  708. tb_nvm_exit();
  709. tb_xdomain_exit();
  710. tb_debugfs_exit();
  711. tb_test_exit();
  712. }