domain.c 18 KB

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