domain.c 21 KB

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