domain.c 18 KB

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