domain.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Thunderbolt bus support
  4. *
  5. * Copyright (C) 2017, Intel Corporation
  6. * Author: Mika Westerberg <[email protected]>
  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. struct device_driver *drv)
  40. {
  41. 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(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, 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. 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_simple_remove(&tb_domain_ida, tb->index);
  265. mutex_destroy(&tb->lock);
  266. kfree(tb);
  267. }
  268. 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_simple_get(&tb_domain_ida, 0, 0, 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, 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_simple_remove(&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. *
  349. * Starts the domain and adds it to the system. Hotplugging devices will
  350. * work after this has been returned successfully. In order to remove
  351. * and release the domain after this function has been called, call
  352. * tb_domain_remove().
  353. *
  354. * Return: %0 in case of success and negative errno in case of error
  355. */
  356. int tb_domain_add(struct tb *tb)
  357. {
  358. int ret;
  359. if (WARN_ON(!tb->cm_ops))
  360. return -EINVAL;
  361. mutex_lock(&tb->lock);
  362. /*
  363. * tb_schedule_hotplug_handler may be called as soon as the config
  364. * channel is started. Thats why we have to hold the lock here.
  365. */
  366. tb_ctl_start(tb->ctl);
  367. if (tb->cm_ops->driver_ready) {
  368. ret = tb->cm_ops->driver_ready(tb);
  369. if (ret)
  370. goto err_ctl_stop;
  371. }
  372. tb_dbg(tb, "security level set to %s\n",
  373. tb_security_names[tb->security_level]);
  374. ret = device_add(&tb->dev);
  375. if (ret)
  376. goto err_ctl_stop;
  377. /* Start the domain */
  378. if (tb->cm_ops->start) {
  379. ret = tb->cm_ops->start(tb);
  380. if (ret)
  381. goto err_domain_del;
  382. }
  383. /* This starts event processing */
  384. mutex_unlock(&tb->lock);
  385. device_init_wakeup(&tb->dev, true);
  386. pm_runtime_no_callbacks(&tb->dev);
  387. pm_runtime_set_active(&tb->dev);
  388. pm_runtime_enable(&tb->dev);
  389. pm_runtime_set_autosuspend_delay(&tb->dev, TB_AUTOSUSPEND_DELAY);
  390. pm_runtime_mark_last_busy(&tb->dev);
  391. pm_runtime_use_autosuspend(&tb->dev);
  392. return 0;
  393. err_domain_del:
  394. device_del(&tb->dev);
  395. err_ctl_stop:
  396. tb_ctl_stop(tb->ctl);
  397. mutex_unlock(&tb->lock);
  398. return ret;
  399. }
  400. /**
  401. * tb_domain_remove() - Removes and releases a domain
  402. * @tb: Domain to remove
  403. *
  404. * Stops the domain, removes it from the system and releases all
  405. * resources once the last reference has been released.
  406. */
  407. void tb_domain_remove(struct tb *tb)
  408. {
  409. mutex_lock(&tb->lock);
  410. if (tb->cm_ops->stop)
  411. tb->cm_ops->stop(tb);
  412. /* Stop the domain control traffic */
  413. tb_ctl_stop(tb->ctl);
  414. mutex_unlock(&tb->lock);
  415. flush_workqueue(tb->wq);
  416. device_unregister(&tb->dev);
  417. }
  418. /**
  419. * tb_domain_suspend_noirq() - Suspend a domain
  420. * @tb: Domain to suspend
  421. *
  422. * Suspends all devices in the domain and stops the control channel.
  423. */
  424. int tb_domain_suspend_noirq(struct tb *tb)
  425. {
  426. int ret = 0;
  427. /*
  428. * The control channel interrupt is left enabled during suspend
  429. * and taking the lock here prevents any events happening before
  430. * we actually have stopped the domain and the control channel.
  431. */
  432. mutex_lock(&tb->lock);
  433. if (tb->cm_ops->suspend_noirq)
  434. ret = tb->cm_ops->suspend_noirq(tb);
  435. if (!ret)
  436. tb_ctl_stop(tb->ctl);
  437. mutex_unlock(&tb->lock);
  438. return ret;
  439. }
  440. /**
  441. * tb_domain_resume_noirq() - Resume a domain
  442. * @tb: Domain to resume
  443. *
  444. * Re-starts the control channel, and resumes all devices connected to
  445. * the domain.
  446. */
  447. int tb_domain_resume_noirq(struct tb *tb)
  448. {
  449. int ret = 0;
  450. mutex_lock(&tb->lock);
  451. tb_ctl_start(tb->ctl);
  452. if (tb->cm_ops->resume_noirq)
  453. ret = tb->cm_ops->resume_noirq(tb);
  454. mutex_unlock(&tb->lock);
  455. return ret;
  456. }
  457. int tb_domain_suspend(struct tb *tb)
  458. {
  459. return tb->cm_ops->suspend ? tb->cm_ops->suspend(tb) : 0;
  460. }
  461. int tb_domain_freeze_noirq(struct tb *tb)
  462. {
  463. int ret = 0;
  464. mutex_lock(&tb->lock);
  465. if (tb->cm_ops->freeze_noirq)
  466. ret = tb->cm_ops->freeze_noirq(tb);
  467. if (!ret)
  468. tb_ctl_stop(tb->ctl);
  469. mutex_unlock(&tb->lock);
  470. return ret;
  471. }
  472. int tb_domain_thaw_noirq(struct tb *tb)
  473. {
  474. int ret = 0;
  475. mutex_lock(&tb->lock);
  476. tb_ctl_start(tb->ctl);
  477. if (tb->cm_ops->thaw_noirq)
  478. ret = tb->cm_ops->thaw_noirq(tb);
  479. mutex_unlock(&tb->lock);
  480. return ret;
  481. }
  482. void tb_domain_complete(struct tb *tb)
  483. {
  484. if (tb->cm_ops->complete)
  485. tb->cm_ops->complete(tb);
  486. }
  487. int tb_domain_runtime_suspend(struct tb *tb)
  488. {
  489. if (tb->cm_ops->runtime_suspend) {
  490. int ret = tb->cm_ops->runtime_suspend(tb);
  491. if (ret)
  492. return ret;
  493. }
  494. tb_ctl_stop(tb->ctl);
  495. return 0;
  496. }
  497. int tb_domain_runtime_resume(struct tb *tb)
  498. {
  499. tb_ctl_start(tb->ctl);
  500. if (tb->cm_ops->runtime_resume) {
  501. int ret = tb->cm_ops->runtime_resume(tb);
  502. if (ret)
  503. return ret;
  504. }
  505. return 0;
  506. }
  507. /**
  508. * tb_domain_disapprove_switch() - Disapprove switch
  509. * @tb: Domain the switch belongs to
  510. * @sw: Switch to disapprove
  511. *
  512. * This will disconnect PCIe tunnel from parent to this @sw.
  513. *
  514. * Return: %0 on success and negative errno in case of failure.
  515. */
  516. int tb_domain_disapprove_switch(struct tb *tb, struct tb_switch *sw)
  517. {
  518. if (!tb->cm_ops->disapprove_switch)
  519. return -EPERM;
  520. return tb->cm_ops->disapprove_switch(tb, sw);
  521. }
  522. /**
  523. * tb_domain_approve_switch() - Approve switch
  524. * @tb: Domain the switch belongs to
  525. * @sw: Switch to approve
  526. *
  527. * This will approve switch by connection manager specific means. In
  528. * case of success the connection manager will create PCIe tunnel from
  529. * parent to @sw.
  530. */
  531. int tb_domain_approve_switch(struct tb *tb, struct tb_switch *sw)
  532. {
  533. struct tb_switch *parent_sw;
  534. if (!tb->cm_ops->approve_switch)
  535. return -EPERM;
  536. /* The parent switch must be authorized before this one */
  537. parent_sw = tb_to_switch(sw->dev.parent);
  538. if (!parent_sw || !parent_sw->authorized)
  539. return -EINVAL;
  540. return tb->cm_ops->approve_switch(tb, sw);
  541. }
  542. /**
  543. * tb_domain_approve_switch_key() - Approve switch and add key
  544. * @tb: Domain the switch belongs to
  545. * @sw: Switch to approve
  546. *
  547. * For switches that support secure connect, this function first adds
  548. * key to the switch NVM using connection manager specific means. If
  549. * adding the key is successful, the switch is approved and connected.
  550. *
  551. * Return: %0 on success and negative errno in case of failure.
  552. */
  553. int tb_domain_approve_switch_key(struct tb *tb, struct tb_switch *sw)
  554. {
  555. struct tb_switch *parent_sw;
  556. int ret;
  557. if (!tb->cm_ops->approve_switch || !tb->cm_ops->add_switch_key)
  558. return -EPERM;
  559. /* The parent switch must be authorized before this one */
  560. parent_sw = tb_to_switch(sw->dev.parent);
  561. if (!parent_sw || !parent_sw->authorized)
  562. return -EINVAL;
  563. ret = tb->cm_ops->add_switch_key(tb, sw);
  564. if (ret)
  565. return ret;
  566. return tb->cm_ops->approve_switch(tb, sw);
  567. }
  568. /**
  569. * tb_domain_challenge_switch_key() - Challenge and approve switch
  570. * @tb: Domain the switch belongs to
  571. * @sw: Switch to approve
  572. *
  573. * For switches that support secure connect, this function generates
  574. * random challenge and sends it to the switch. The switch responds to
  575. * this and if the response matches our random challenge, the switch is
  576. * approved and connected.
  577. *
  578. * Return: %0 on success and negative errno in case of failure.
  579. */
  580. int tb_domain_challenge_switch_key(struct tb *tb, struct tb_switch *sw)
  581. {
  582. u8 challenge[TB_SWITCH_KEY_SIZE];
  583. u8 response[TB_SWITCH_KEY_SIZE];
  584. u8 hmac[TB_SWITCH_KEY_SIZE];
  585. struct tb_switch *parent_sw;
  586. struct crypto_shash *tfm;
  587. struct shash_desc *shash;
  588. int ret;
  589. if (!tb->cm_ops->approve_switch || !tb->cm_ops->challenge_switch_key)
  590. return -EPERM;
  591. /* The parent switch must be authorized before this one */
  592. parent_sw = tb_to_switch(sw->dev.parent);
  593. if (!parent_sw || !parent_sw->authorized)
  594. return -EINVAL;
  595. get_random_bytes(challenge, sizeof(challenge));
  596. ret = tb->cm_ops->challenge_switch_key(tb, sw, challenge, response);
  597. if (ret)
  598. return ret;
  599. tfm = crypto_alloc_shash("hmac(sha256)", 0, 0);
  600. if (IS_ERR(tfm))
  601. return PTR_ERR(tfm);
  602. ret = crypto_shash_setkey(tfm, sw->key, TB_SWITCH_KEY_SIZE);
  603. if (ret)
  604. goto err_free_tfm;
  605. shash = kzalloc(sizeof(*shash) + crypto_shash_descsize(tfm),
  606. GFP_KERNEL);
  607. if (!shash) {
  608. ret = -ENOMEM;
  609. goto err_free_tfm;
  610. }
  611. shash->tfm = tfm;
  612. memset(hmac, 0, sizeof(hmac));
  613. ret = crypto_shash_digest(shash, challenge, sizeof(hmac), hmac);
  614. if (ret)
  615. goto err_free_shash;
  616. /* The returned HMAC must match the one we calculated */
  617. if (memcmp(response, hmac, sizeof(hmac))) {
  618. ret = -EKEYREJECTED;
  619. goto err_free_shash;
  620. }
  621. crypto_free_shash(tfm);
  622. kfree(shash);
  623. return tb->cm_ops->approve_switch(tb, sw);
  624. err_free_shash:
  625. kfree(shash);
  626. err_free_tfm:
  627. crypto_free_shash(tfm);
  628. return ret;
  629. }
  630. /**
  631. * tb_domain_disconnect_pcie_paths() - Disconnect all PCIe paths
  632. * @tb: Domain whose PCIe paths to disconnect
  633. *
  634. * This needs to be called in preparation for NVM upgrade of the host
  635. * controller. Makes sure all PCIe paths are disconnected.
  636. *
  637. * Return %0 on success and negative errno in case of error.
  638. */
  639. int tb_domain_disconnect_pcie_paths(struct tb *tb)
  640. {
  641. if (!tb->cm_ops->disconnect_pcie_paths)
  642. return -EPERM;
  643. return tb->cm_ops->disconnect_pcie_paths(tb);
  644. }
  645. /**
  646. * tb_domain_approve_xdomain_paths() - Enable DMA paths for XDomain
  647. * @tb: Domain enabling the DMA paths
  648. * @xd: XDomain DMA paths are created to
  649. * @transmit_path: HopID we are using to send out packets
  650. * @transmit_ring: DMA ring used to send out packets
  651. * @receive_path: HopID the other end is using to send packets to us
  652. * @receive_ring: DMA ring used to receive packets from @receive_path
  653. *
  654. * Calls connection manager specific method to enable DMA paths to the
  655. * XDomain in question.
  656. *
  657. * Return: 0% in case of success and negative errno otherwise. In
  658. * particular returns %-ENOTSUPP if the connection manager
  659. * implementation does not support XDomains.
  660. */
  661. int tb_domain_approve_xdomain_paths(struct tb *tb, struct tb_xdomain *xd,
  662. int transmit_path, int transmit_ring,
  663. int receive_path, int receive_ring)
  664. {
  665. if (!tb->cm_ops->approve_xdomain_paths)
  666. return -ENOTSUPP;
  667. return tb->cm_ops->approve_xdomain_paths(tb, xd, transmit_path,
  668. transmit_ring, receive_path, receive_ring);
  669. }
  670. /**
  671. * tb_domain_disconnect_xdomain_paths() - Disable DMA paths for XDomain
  672. * @tb: Domain disabling the DMA paths
  673. * @xd: XDomain whose DMA paths are disconnected
  674. * @transmit_path: HopID we are using to send out packets
  675. * @transmit_ring: DMA ring used to send out packets
  676. * @receive_path: HopID the other end is using to send packets to us
  677. * @receive_ring: DMA ring used to receive packets from @receive_path
  678. *
  679. * Calls connection manager specific method to disconnect DMA paths to
  680. * the XDomain in question.
  681. *
  682. * Return: 0% in case of success and negative errno otherwise. In
  683. * particular returns %-ENOTSUPP if the connection manager
  684. * implementation does not support XDomains.
  685. */
  686. int tb_domain_disconnect_xdomain_paths(struct tb *tb, struct tb_xdomain *xd,
  687. int transmit_path, int transmit_ring,
  688. int receive_path, int receive_ring)
  689. {
  690. if (!tb->cm_ops->disconnect_xdomain_paths)
  691. return -ENOTSUPP;
  692. return tb->cm_ops->disconnect_xdomain_paths(tb, xd, transmit_path,
  693. transmit_ring, receive_path, receive_ring);
  694. }
  695. static int disconnect_xdomain(struct device *dev, void *data)
  696. {
  697. struct tb_xdomain *xd;
  698. struct tb *tb = data;
  699. int ret = 0;
  700. xd = tb_to_xdomain(dev);
  701. if (xd && xd->tb == tb)
  702. ret = tb_xdomain_disable_all_paths(xd);
  703. return ret;
  704. }
  705. /**
  706. * tb_domain_disconnect_all_paths() - Disconnect all paths for the domain
  707. * @tb: Domain whose paths are disconnected
  708. *
  709. * This function can be used to disconnect all paths (PCIe, XDomain) for
  710. * example in preparation for host NVM firmware upgrade. After this is
  711. * called the paths cannot be established without resetting the switch.
  712. *
  713. * Return: %0 in case of success and negative errno otherwise.
  714. */
  715. int tb_domain_disconnect_all_paths(struct tb *tb)
  716. {
  717. int ret;
  718. ret = tb_domain_disconnect_pcie_paths(tb);
  719. if (ret)
  720. return ret;
  721. return bus_for_each_dev(&tb_bus_type, NULL, tb, disconnect_xdomain);
  722. }
  723. int tb_domain_init(void)
  724. {
  725. int ret;
  726. tb_debugfs_init();
  727. tb_acpi_init();
  728. ret = tb_xdomain_init();
  729. if (ret)
  730. goto err_acpi;
  731. ret = bus_register(&tb_bus_type);
  732. if (ret)
  733. goto err_xdomain;
  734. return 0;
  735. err_xdomain:
  736. tb_xdomain_exit();
  737. err_acpi:
  738. tb_acpi_exit();
  739. tb_debugfs_exit();
  740. return ret;
  741. }
  742. void tb_domain_exit(void)
  743. {
  744. bus_unregister(&tb_bus_type);
  745. ida_destroy(&tb_domain_ida);
  746. tb_nvm_exit();
  747. tb_xdomain_exit();
  748. tb_acpi_exit();
  749. tb_debugfs_exit();
  750. }