domain.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773
  1. /*
  2. * Thunderbolt bus support
  3. *
  4. * Copyright (C) 2017, Intel Corporation
  5. * Author: Mika Westerberg <mika.westerberg@linux.intel.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. */
  11. #include <linux/device.h>
  12. #include <linux/idr.h>
  13. #include <linux/module.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. if (mutex_lock_interruptible(&tb->lock)) {
  109. ret = -ERESTARTSYS;
  110. goto out;
  111. }
  112. ret = tb->cm_ops->get_boot_acl(tb, uuids, tb->nboot_acl);
  113. if (ret) {
  114. mutex_unlock(&tb->lock);
  115. goto out;
  116. }
  117. mutex_unlock(&tb->lock);
  118. for (ret = 0, i = 0; i < tb->nboot_acl; i++) {
  119. if (!uuid_is_null(&uuids[i]))
  120. ret += snprintf(buf + ret, PAGE_SIZE - ret, "%pUb",
  121. &uuids[i]);
  122. ret += snprintf(buf + ret, PAGE_SIZE - ret, "%s",
  123. i < tb->nboot_acl - 1 ? "," : "\n");
  124. }
  125. out:
  126. kfree(uuids);
  127. return ret;
  128. }
  129. static ssize_t boot_acl_store(struct device *dev, struct device_attribute *attr,
  130. const char *buf, size_t count)
  131. {
  132. struct tb *tb = container_of(dev, struct tb, dev);
  133. char *str, *s, *uuid_str;
  134. ssize_t ret = 0;
  135. uuid_t *acl;
  136. int i = 0;
  137. /*
  138. * Make sure the value is not bigger than tb->nboot_acl * UUID
  139. * length + commas and optional "\n". Also the smallest allowable
  140. * string is tb->nboot_acl * ",".
  141. */
  142. if (count > (UUID_STRING_LEN + 1) * tb->nboot_acl + 1)
  143. return -EINVAL;
  144. if (count < tb->nboot_acl - 1)
  145. return -EINVAL;
  146. str = kstrdup(buf, GFP_KERNEL);
  147. if (!str)
  148. return -ENOMEM;
  149. acl = kcalloc(tb->nboot_acl, sizeof(uuid_t), GFP_KERNEL);
  150. if (!acl) {
  151. ret = -ENOMEM;
  152. goto err_free_str;
  153. }
  154. uuid_str = strim(str);
  155. while ((s = strsep(&uuid_str, ",")) != NULL && i < tb->nboot_acl) {
  156. size_t len = strlen(s);
  157. if (len) {
  158. if (len != UUID_STRING_LEN) {
  159. ret = -EINVAL;
  160. goto err_free_acl;
  161. }
  162. ret = uuid_parse(s, &acl[i]);
  163. if (ret)
  164. goto err_free_acl;
  165. }
  166. i++;
  167. }
  168. if (s || i < tb->nboot_acl) {
  169. ret = -EINVAL;
  170. goto err_free_acl;
  171. }
  172. if (mutex_lock_interruptible(&tb->lock)) {
  173. ret = -ERESTARTSYS;
  174. goto err_free_acl;
  175. }
  176. ret = tb->cm_ops->set_boot_acl(tb, acl, tb->nboot_acl);
  177. mutex_unlock(&tb->lock);
  178. err_free_acl:
  179. kfree(acl);
  180. err_free_str:
  181. kfree(str);
  182. return ret ?: count;
  183. }
  184. static DEVICE_ATTR_RW(boot_acl);
  185. static ssize_t security_show(struct device *dev, struct device_attribute *attr,
  186. char *buf)
  187. {
  188. struct tb *tb = container_of(dev, struct tb, dev);
  189. const char *name = "unknown";
  190. if (tb->security_level < ARRAY_SIZE(tb_security_names))
  191. name = tb_security_names[tb->security_level];
  192. return sprintf(buf, "%s\n", name);
  193. }
  194. static DEVICE_ATTR_RO(security);
  195. static struct attribute *domain_attrs[] = {
  196. &dev_attr_boot_acl.attr,
  197. &dev_attr_security.attr,
  198. NULL,
  199. };
  200. static umode_t domain_attr_is_visible(struct kobject *kobj,
  201. struct attribute *attr, int n)
  202. {
  203. struct device *dev = container_of(kobj, struct device, kobj);
  204. struct tb *tb = container_of(dev, struct tb, dev);
  205. if (attr == &dev_attr_boot_acl.attr) {
  206. if (tb->nboot_acl &&
  207. tb->cm_ops->get_boot_acl &&
  208. tb->cm_ops->set_boot_acl)
  209. return attr->mode;
  210. return 0;
  211. }
  212. return attr->mode;
  213. }
  214. static struct attribute_group domain_attr_group = {
  215. .is_visible = domain_attr_is_visible,
  216. .attrs = domain_attrs,
  217. };
  218. static const struct attribute_group *domain_attr_groups[] = {
  219. &domain_attr_group,
  220. NULL,
  221. };
  222. struct bus_type tb_bus_type = {
  223. .name = "thunderbolt",
  224. .match = tb_service_match,
  225. .probe = tb_service_probe,
  226. .remove = tb_service_remove,
  227. .shutdown = tb_service_shutdown,
  228. };
  229. static void tb_domain_release(struct device *dev)
  230. {
  231. struct tb *tb = container_of(dev, struct tb, dev);
  232. tb_ctl_free(tb->ctl);
  233. destroy_workqueue(tb->wq);
  234. ida_simple_remove(&tb_domain_ida, tb->index);
  235. mutex_destroy(&tb->lock);
  236. kfree(tb);
  237. }
  238. struct device_type tb_domain_type = {
  239. .name = "thunderbolt_domain",
  240. .release = tb_domain_release,
  241. };
  242. /**
  243. * tb_domain_alloc() - Allocate a domain
  244. * @nhi: Pointer to the host controller
  245. * @privsize: Size of the connection manager private data
  246. *
  247. * Allocates and initializes a new Thunderbolt domain. Connection
  248. * managers are expected to call this and then fill in @cm_ops
  249. * accordingly.
  250. *
  251. * Call tb_domain_put() to release the domain before it has been added
  252. * to the system.
  253. *
  254. * Return: allocated domain structure on %NULL in case of error
  255. */
  256. struct tb *tb_domain_alloc(struct tb_nhi *nhi, size_t privsize)
  257. {
  258. struct tb *tb;
  259. /*
  260. * Make sure the structure sizes map with that the hardware
  261. * expects because bit-fields are being used.
  262. */
  263. BUILD_BUG_ON(sizeof(struct tb_regs_switch_header) != 5 * 4);
  264. BUILD_BUG_ON(sizeof(struct tb_regs_port_header) != 8 * 4);
  265. BUILD_BUG_ON(sizeof(struct tb_regs_hop) != 2 * 4);
  266. tb = kzalloc(sizeof(*tb) + privsize, GFP_KERNEL);
  267. if (!tb)
  268. return NULL;
  269. tb->nhi = nhi;
  270. mutex_init(&tb->lock);
  271. tb->index = ida_simple_get(&tb_domain_ida, 0, 0, GFP_KERNEL);
  272. if (tb->index < 0)
  273. goto err_free;
  274. tb->wq = alloc_ordered_workqueue("thunderbolt%d", 0, tb->index);
  275. if (!tb->wq)
  276. goto err_remove_ida;
  277. tb->dev.parent = &nhi->pdev->dev;
  278. tb->dev.bus = &tb_bus_type;
  279. tb->dev.type = &tb_domain_type;
  280. tb->dev.groups = domain_attr_groups;
  281. dev_set_name(&tb->dev, "domain%d", tb->index);
  282. device_initialize(&tb->dev);
  283. return tb;
  284. err_remove_ida:
  285. ida_simple_remove(&tb_domain_ida, tb->index);
  286. err_free:
  287. kfree(tb);
  288. return NULL;
  289. }
  290. static bool tb_domain_event_cb(void *data, enum tb_cfg_pkg_type type,
  291. const void *buf, size_t size)
  292. {
  293. struct tb *tb = data;
  294. if (!tb->cm_ops->handle_event) {
  295. tb_warn(tb, "domain does not have event handler\n");
  296. return true;
  297. }
  298. switch (type) {
  299. case TB_CFG_PKG_XDOMAIN_REQ:
  300. case TB_CFG_PKG_XDOMAIN_RESP:
  301. return tb_xdomain_handle_request(tb, type, buf, size);
  302. default:
  303. tb->cm_ops->handle_event(tb, type, buf, size);
  304. }
  305. return true;
  306. }
  307. /**
  308. * tb_domain_add() - Add domain to the system
  309. * @tb: Domain to add
  310. *
  311. * Starts the domain and adds it to the system. Hotplugging devices will
  312. * work after this has been returned successfully. In order to remove
  313. * and release the domain after this function has been called, call
  314. * tb_domain_remove().
  315. *
  316. * Return: %0 in case of success and negative errno in case of error
  317. */
  318. int tb_domain_add(struct tb *tb)
  319. {
  320. int ret;
  321. if (WARN_ON(!tb->cm_ops))
  322. return -EINVAL;
  323. mutex_lock(&tb->lock);
  324. tb->ctl = tb_ctl_alloc(tb->nhi, tb_domain_event_cb, tb);
  325. if (!tb->ctl) {
  326. ret = -ENOMEM;
  327. goto err_unlock;
  328. }
  329. /*
  330. * tb_schedule_hotplug_handler may be called as soon as the config
  331. * channel is started. Thats why we have to hold the lock here.
  332. */
  333. tb_ctl_start(tb->ctl);
  334. if (tb->cm_ops->driver_ready) {
  335. ret = tb->cm_ops->driver_ready(tb);
  336. if (ret)
  337. goto err_ctl_stop;
  338. }
  339. ret = device_add(&tb->dev);
  340. if (ret)
  341. goto err_ctl_stop;
  342. /* Start the domain */
  343. if (tb->cm_ops->start) {
  344. ret = tb->cm_ops->start(tb);
  345. if (ret)
  346. goto err_domain_del;
  347. }
  348. /* This starts event processing */
  349. mutex_unlock(&tb->lock);
  350. return 0;
  351. err_domain_del:
  352. device_del(&tb->dev);
  353. err_ctl_stop:
  354. tb_ctl_stop(tb->ctl);
  355. err_unlock:
  356. mutex_unlock(&tb->lock);
  357. return ret;
  358. }
  359. /**
  360. * tb_domain_remove() - Removes and releases a domain
  361. * @tb: Domain to remove
  362. *
  363. * Stops the domain, removes it from the system and releases all
  364. * resources once the last reference has been released.
  365. */
  366. void tb_domain_remove(struct tb *tb)
  367. {
  368. mutex_lock(&tb->lock);
  369. if (tb->cm_ops->stop)
  370. tb->cm_ops->stop(tb);
  371. /* Stop the domain control traffic */
  372. tb_ctl_stop(tb->ctl);
  373. mutex_unlock(&tb->lock);
  374. flush_workqueue(tb->wq);
  375. device_unregister(&tb->dev);
  376. }
  377. /**
  378. * tb_domain_suspend_noirq() - Suspend a domain
  379. * @tb: Domain to suspend
  380. *
  381. * Suspends all devices in the domain and stops the control channel.
  382. */
  383. int tb_domain_suspend_noirq(struct tb *tb)
  384. {
  385. int ret = 0;
  386. /*
  387. * The control channel interrupt is left enabled during suspend
  388. * and taking the lock here prevents any events happening before
  389. * we actually have stopped the domain and the control channel.
  390. */
  391. mutex_lock(&tb->lock);
  392. if (tb->cm_ops->suspend_noirq)
  393. ret = tb->cm_ops->suspend_noirq(tb);
  394. if (!ret)
  395. tb_ctl_stop(tb->ctl);
  396. mutex_unlock(&tb->lock);
  397. return ret;
  398. }
  399. /**
  400. * tb_domain_resume_noirq() - Resume a domain
  401. * @tb: Domain to resume
  402. *
  403. * Re-starts the control channel, and resumes all devices connected to
  404. * the domain.
  405. */
  406. int tb_domain_resume_noirq(struct tb *tb)
  407. {
  408. int ret = 0;
  409. mutex_lock(&tb->lock);
  410. tb_ctl_start(tb->ctl);
  411. if (tb->cm_ops->resume_noirq)
  412. ret = tb->cm_ops->resume_noirq(tb);
  413. mutex_unlock(&tb->lock);
  414. return ret;
  415. }
  416. int tb_domain_suspend(struct tb *tb)
  417. {
  418. int ret;
  419. mutex_lock(&tb->lock);
  420. if (tb->cm_ops->suspend) {
  421. ret = tb->cm_ops->suspend(tb);
  422. if (ret) {
  423. mutex_unlock(&tb->lock);
  424. return ret;
  425. }
  426. }
  427. mutex_unlock(&tb->lock);
  428. return 0;
  429. }
  430. void tb_domain_complete(struct tb *tb)
  431. {
  432. mutex_lock(&tb->lock);
  433. if (tb->cm_ops->complete)
  434. tb->cm_ops->complete(tb);
  435. mutex_unlock(&tb->lock);
  436. }
  437. /**
  438. * tb_domain_approve_switch() - Approve switch
  439. * @tb: Domain the switch belongs to
  440. * @sw: Switch to approve
  441. *
  442. * This will approve switch by connection manager specific means. In
  443. * case of success the connection manager will create tunnels for all
  444. * supported protocols.
  445. */
  446. int tb_domain_approve_switch(struct tb *tb, struct tb_switch *sw)
  447. {
  448. struct tb_switch *parent_sw;
  449. if (!tb->cm_ops->approve_switch)
  450. return -EPERM;
  451. /* The parent switch must be authorized before this one */
  452. parent_sw = tb_to_switch(sw->dev.parent);
  453. if (!parent_sw || !parent_sw->authorized)
  454. return -EINVAL;
  455. return tb->cm_ops->approve_switch(tb, sw);
  456. }
  457. /**
  458. * tb_domain_approve_switch_key() - Approve switch and add key
  459. * @tb: Domain the switch belongs to
  460. * @sw: Switch to approve
  461. *
  462. * For switches that support secure connect, this function first adds
  463. * key to the switch NVM using connection manager specific means. If
  464. * adding the key is successful, the switch is approved and connected.
  465. *
  466. * Return: %0 on success and negative errno in case of failure.
  467. */
  468. int tb_domain_approve_switch_key(struct tb *tb, struct tb_switch *sw)
  469. {
  470. struct tb_switch *parent_sw;
  471. int ret;
  472. if (!tb->cm_ops->approve_switch || !tb->cm_ops->add_switch_key)
  473. return -EPERM;
  474. /* The parent switch must be authorized before this one */
  475. parent_sw = tb_to_switch(sw->dev.parent);
  476. if (!parent_sw || !parent_sw->authorized)
  477. return -EINVAL;
  478. ret = tb->cm_ops->add_switch_key(tb, sw);
  479. if (ret)
  480. return ret;
  481. return tb->cm_ops->approve_switch(tb, sw);
  482. }
  483. /**
  484. * tb_domain_challenge_switch_key() - Challenge and approve switch
  485. * @tb: Domain the switch belongs to
  486. * @sw: Switch to approve
  487. *
  488. * For switches that support secure connect, this function generates
  489. * random challenge and sends it to the switch. The switch responds to
  490. * this and if the response matches our random challenge, the switch is
  491. * approved and connected.
  492. *
  493. * Return: %0 on success and negative errno in case of failure.
  494. */
  495. int tb_domain_challenge_switch_key(struct tb *tb, struct tb_switch *sw)
  496. {
  497. u8 challenge[TB_SWITCH_KEY_SIZE];
  498. u8 response[TB_SWITCH_KEY_SIZE];
  499. u8 hmac[TB_SWITCH_KEY_SIZE];
  500. struct tb_switch *parent_sw;
  501. struct crypto_shash *tfm;
  502. struct shash_desc *shash;
  503. int ret;
  504. if (!tb->cm_ops->approve_switch || !tb->cm_ops->challenge_switch_key)
  505. return -EPERM;
  506. /* The parent switch must be authorized before this one */
  507. parent_sw = tb_to_switch(sw->dev.parent);
  508. if (!parent_sw || !parent_sw->authorized)
  509. return -EINVAL;
  510. get_random_bytes(challenge, sizeof(challenge));
  511. ret = tb->cm_ops->challenge_switch_key(tb, sw, challenge, response);
  512. if (ret)
  513. return ret;
  514. tfm = crypto_alloc_shash("hmac(sha256)", 0, 0);
  515. if (IS_ERR(tfm))
  516. return PTR_ERR(tfm);
  517. ret = crypto_shash_setkey(tfm, sw->key, TB_SWITCH_KEY_SIZE);
  518. if (ret)
  519. goto err_free_tfm;
  520. shash = kzalloc(sizeof(*shash) + crypto_shash_descsize(tfm),
  521. GFP_KERNEL);
  522. if (!shash) {
  523. ret = -ENOMEM;
  524. goto err_free_tfm;
  525. }
  526. shash->tfm = tfm;
  527. shash->flags = CRYPTO_TFM_REQ_MAY_SLEEP;
  528. memset(hmac, 0, sizeof(hmac));
  529. ret = crypto_shash_digest(shash, challenge, sizeof(hmac), hmac);
  530. if (ret)
  531. goto err_free_shash;
  532. /* The returned HMAC must match the one we calculated */
  533. if (memcmp(response, hmac, sizeof(hmac))) {
  534. ret = -EKEYREJECTED;
  535. goto err_free_shash;
  536. }
  537. crypto_free_shash(tfm);
  538. kfree(shash);
  539. return tb->cm_ops->approve_switch(tb, sw);
  540. err_free_shash:
  541. kfree(shash);
  542. err_free_tfm:
  543. crypto_free_shash(tfm);
  544. return ret;
  545. }
  546. /**
  547. * tb_domain_disconnect_pcie_paths() - Disconnect all PCIe paths
  548. * @tb: Domain whose PCIe paths to disconnect
  549. *
  550. * This needs to be called in preparation for NVM upgrade of the host
  551. * controller. Makes sure all PCIe paths are disconnected.
  552. *
  553. * Return %0 on success and negative errno in case of error.
  554. */
  555. int tb_domain_disconnect_pcie_paths(struct tb *tb)
  556. {
  557. if (!tb->cm_ops->disconnect_pcie_paths)
  558. return -EPERM;
  559. return tb->cm_ops->disconnect_pcie_paths(tb);
  560. }
  561. /**
  562. * tb_domain_approve_xdomain_paths() - Enable DMA paths for XDomain
  563. * @tb: Domain enabling the DMA paths
  564. * @xd: XDomain DMA paths are created to
  565. *
  566. * Calls connection manager specific method to enable DMA paths to the
  567. * XDomain in question.
  568. *
  569. * Return: 0% in case of success and negative errno otherwise. In
  570. * particular returns %-ENOTSUPP if the connection manager
  571. * implementation does not support XDomains.
  572. */
  573. int tb_domain_approve_xdomain_paths(struct tb *tb, struct tb_xdomain *xd)
  574. {
  575. if (!tb->cm_ops->approve_xdomain_paths)
  576. return -ENOTSUPP;
  577. return tb->cm_ops->approve_xdomain_paths(tb, xd);
  578. }
  579. /**
  580. * tb_domain_disconnect_xdomain_paths() - Disable DMA paths for XDomain
  581. * @tb: Domain disabling the DMA paths
  582. * @xd: XDomain whose DMA paths are disconnected
  583. *
  584. * Calls connection manager specific method to disconnect DMA paths to
  585. * the XDomain in question.
  586. *
  587. * Return: 0% in case of success and negative errno otherwise. In
  588. * particular returns %-ENOTSUPP if the connection manager
  589. * implementation does not support XDomains.
  590. */
  591. int tb_domain_disconnect_xdomain_paths(struct tb *tb, struct tb_xdomain *xd)
  592. {
  593. if (!tb->cm_ops->disconnect_xdomain_paths)
  594. return -ENOTSUPP;
  595. return tb->cm_ops->disconnect_xdomain_paths(tb, xd);
  596. }
  597. static int disconnect_xdomain(struct device *dev, void *data)
  598. {
  599. struct tb_xdomain *xd;
  600. struct tb *tb = data;
  601. int ret = 0;
  602. xd = tb_to_xdomain(dev);
  603. if (xd && xd->tb == tb)
  604. ret = tb_xdomain_disable_paths(xd);
  605. return ret;
  606. }
  607. /**
  608. * tb_domain_disconnect_all_paths() - Disconnect all paths for the domain
  609. * @tb: Domain whose paths are disconnected
  610. *
  611. * This function can be used to disconnect all paths (PCIe, XDomain) for
  612. * example in preparation for host NVM firmware upgrade. After this is
  613. * called the paths cannot be established without resetting the switch.
  614. *
  615. * Return: %0 in case of success and negative errno otherwise.
  616. */
  617. int tb_domain_disconnect_all_paths(struct tb *tb)
  618. {
  619. int ret;
  620. ret = tb_domain_disconnect_pcie_paths(tb);
  621. if (ret)
  622. return ret;
  623. return bus_for_each_dev(&tb_bus_type, NULL, tb, disconnect_xdomain);
  624. }
  625. int tb_domain_init(void)
  626. {
  627. int ret;
  628. ret = tb_xdomain_init();
  629. if (ret)
  630. return ret;
  631. ret = bus_register(&tb_bus_type);
  632. if (ret)
  633. tb_xdomain_exit();
  634. return ret;
  635. }
  636. void tb_domain_exit(void)
  637. {
  638. bus_unregister(&tb_bus_type);
  639. ida_destroy(&tb_domain_ida);
  640. tb_switch_exit();
  641. tb_xdomain_exit();
  642. }