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