Merge tag 'hwmon-for-v6.13-rc7' of git://git.kernel.org/pub/scm/linux/kernel/git...
[linux.git] / drivers / thunderbolt / domain.c
blob144d0232a70c11c530650d82c3d6b3acad6316d2
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Thunderbolt bus support
5 * Copyright (C) 2017, Intel Corporation
6 * Author: Mika Westerberg <mika.westerberg@linux.intel.com>
7 */
9 #include <linux/device.h>
10 #include <linux/idr.h>
11 #include <linux/module.h>
12 #include <linux/pm_runtime.h>
13 #include <linux/slab.h>
14 #include <linux/random.h>
15 #include <crypto/hash.h>
17 #include "tb.h"
19 static DEFINE_IDA(tb_domain_ida);
21 static bool match_service_id(const struct tb_service_id *id,
22 const struct tb_service *svc)
24 if (id->match_flags & TBSVC_MATCH_PROTOCOL_KEY) {
25 if (strcmp(id->protocol_key, svc->key))
26 return false;
29 if (id->match_flags & TBSVC_MATCH_PROTOCOL_ID) {
30 if (id->protocol_id != svc->prtcid)
31 return false;
34 if (id->match_flags & TBSVC_MATCH_PROTOCOL_VERSION) {
35 if (id->protocol_version != svc->prtcvers)
36 return false;
39 if (id->match_flags & TBSVC_MATCH_PROTOCOL_VERSION) {
40 if (id->protocol_revision != svc->prtcrevs)
41 return false;
44 return true;
47 static const struct tb_service_id *__tb_service_match(struct device *dev,
48 const struct device_driver *drv)
50 const struct tb_service_driver *driver;
51 const struct tb_service_id *ids;
52 struct tb_service *svc;
54 svc = tb_to_service(dev);
55 if (!svc)
56 return NULL;
58 driver = container_of_const(drv, struct tb_service_driver, driver);
59 if (!driver->id_table)
60 return NULL;
62 for (ids = driver->id_table; ids->match_flags != 0; ids++) {
63 if (match_service_id(ids, svc))
64 return ids;
67 return NULL;
70 static int tb_service_match(struct device *dev, const struct device_driver *drv)
72 return !!__tb_service_match(dev, drv);
75 static int tb_service_probe(struct device *dev)
77 struct tb_service *svc = tb_to_service(dev);
78 struct tb_service_driver *driver;
79 const struct tb_service_id *id;
81 driver = container_of(dev->driver, struct tb_service_driver, driver);
82 id = __tb_service_match(dev, &driver->driver);
84 return driver->probe(svc, id);
87 static void tb_service_remove(struct device *dev)
89 struct tb_service *svc = tb_to_service(dev);
90 struct tb_service_driver *driver;
92 driver = container_of(dev->driver, struct tb_service_driver, driver);
93 if (driver->remove)
94 driver->remove(svc);
97 static void tb_service_shutdown(struct device *dev)
99 struct tb_service_driver *driver;
100 struct tb_service *svc;
102 svc = tb_to_service(dev);
103 if (!svc || !dev->driver)
104 return;
106 driver = container_of(dev->driver, struct tb_service_driver, driver);
107 if (driver->shutdown)
108 driver->shutdown(svc);
111 static const char * const tb_security_names[] = {
112 [TB_SECURITY_NONE] = "none",
113 [TB_SECURITY_USER] = "user",
114 [TB_SECURITY_SECURE] = "secure",
115 [TB_SECURITY_DPONLY] = "dponly",
116 [TB_SECURITY_USBONLY] = "usbonly",
117 [TB_SECURITY_NOPCIE] = "nopcie",
120 static ssize_t boot_acl_show(struct device *dev, struct device_attribute *attr,
121 char *buf)
123 struct tb *tb = container_of(dev, struct tb, dev);
124 uuid_t *uuids;
125 ssize_t ret;
126 int i;
128 uuids = kcalloc(tb->nboot_acl, sizeof(uuid_t), GFP_KERNEL);
129 if (!uuids)
130 return -ENOMEM;
132 pm_runtime_get_sync(&tb->dev);
134 if (mutex_lock_interruptible(&tb->lock)) {
135 ret = -ERESTARTSYS;
136 goto out;
138 ret = tb->cm_ops->get_boot_acl(tb, uuids, tb->nboot_acl);
139 if (ret) {
140 mutex_unlock(&tb->lock);
141 goto out;
143 mutex_unlock(&tb->lock);
145 for (ret = 0, i = 0; i < tb->nboot_acl; i++) {
146 if (!uuid_is_null(&uuids[i]))
147 ret += sysfs_emit_at(buf, ret, "%pUb", &uuids[i]);
149 ret += sysfs_emit_at(buf, ret, "%s", i < tb->nboot_acl - 1 ? "," : "\n");
152 out:
153 pm_runtime_mark_last_busy(&tb->dev);
154 pm_runtime_put_autosuspend(&tb->dev);
155 kfree(uuids);
157 return ret;
160 static ssize_t boot_acl_store(struct device *dev, struct device_attribute *attr,
161 const char *buf, size_t count)
163 struct tb *tb = container_of(dev, struct tb, dev);
164 char *str, *s, *uuid_str;
165 ssize_t ret = 0;
166 uuid_t *acl;
167 int i = 0;
170 * Make sure the value is not bigger than tb->nboot_acl * UUID
171 * length + commas and optional "\n". Also the smallest allowable
172 * string is tb->nboot_acl * ",".
174 if (count > (UUID_STRING_LEN + 1) * tb->nboot_acl + 1)
175 return -EINVAL;
176 if (count < tb->nboot_acl - 1)
177 return -EINVAL;
179 str = kstrdup(buf, GFP_KERNEL);
180 if (!str)
181 return -ENOMEM;
183 acl = kcalloc(tb->nboot_acl, sizeof(uuid_t), GFP_KERNEL);
184 if (!acl) {
185 ret = -ENOMEM;
186 goto err_free_str;
189 uuid_str = strim(str);
190 while ((s = strsep(&uuid_str, ",")) != NULL && i < tb->nboot_acl) {
191 size_t len = strlen(s);
193 if (len) {
194 if (len != UUID_STRING_LEN) {
195 ret = -EINVAL;
196 goto err_free_acl;
198 ret = uuid_parse(s, &acl[i]);
199 if (ret)
200 goto err_free_acl;
203 i++;
206 if (s || i < tb->nboot_acl) {
207 ret = -EINVAL;
208 goto err_free_acl;
211 pm_runtime_get_sync(&tb->dev);
213 if (mutex_lock_interruptible(&tb->lock)) {
214 ret = -ERESTARTSYS;
215 goto err_rpm_put;
217 ret = tb->cm_ops->set_boot_acl(tb, acl, tb->nboot_acl);
218 if (!ret) {
219 /* Notify userspace about the change */
220 kobject_uevent(&tb->dev.kobj, KOBJ_CHANGE);
222 mutex_unlock(&tb->lock);
224 err_rpm_put:
225 pm_runtime_mark_last_busy(&tb->dev);
226 pm_runtime_put_autosuspend(&tb->dev);
227 err_free_acl:
228 kfree(acl);
229 err_free_str:
230 kfree(str);
232 return ret ?: count;
234 static DEVICE_ATTR_RW(boot_acl);
236 static ssize_t deauthorization_show(struct device *dev,
237 struct device_attribute *attr,
238 char *buf)
240 const struct tb *tb = container_of(dev, struct tb, dev);
241 bool deauthorization = false;
243 /* Only meaningful if authorization is supported */
244 if (tb->security_level == TB_SECURITY_USER ||
245 tb->security_level == TB_SECURITY_SECURE)
246 deauthorization = !!tb->cm_ops->disapprove_switch;
248 return sysfs_emit(buf, "%d\n", deauthorization);
250 static DEVICE_ATTR_RO(deauthorization);
252 static ssize_t iommu_dma_protection_show(struct device *dev,
253 struct device_attribute *attr,
254 char *buf)
256 struct tb *tb = container_of(dev, struct tb, dev);
258 return sysfs_emit(buf, "%d\n", tb->nhi->iommu_dma_protection);
260 static DEVICE_ATTR_RO(iommu_dma_protection);
262 static ssize_t security_show(struct device *dev, struct device_attribute *attr,
263 char *buf)
265 struct tb *tb = container_of(dev, struct tb, dev);
266 const char *name = "unknown";
268 if (tb->security_level < ARRAY_SIZE(tb_security_names))
269 name = tb_security_names[tb->security_level];
271 return sysfs_emit(buf, "%s\n", name);
273 static DEVICE_ATTR_RO(security);
275 static struct attribute *domain_attrs[] = {
276 &dev_attr_boot_acl.attr,
277 &dev_attr_deauthorization.attr,
278 &dev_attr_iommu_dma_protection.attr,
279 &dev_attr_security.attr,
280 NULL,
283 static umode_t domain_attr_is_visible(struct kobject *kobj,
284 struct attribute *attr, int n)
286 struct device *dev = kobj_to_dev(kobj);
287 struct tb *tb = container_of(dev, struct tb, dev);
289 if (attr == &dev_attr_boot_acl.attr) {
290 if (tb->nboot_acl &&
291 tb->cm_ops->get_boot_acl &&
292 tb->cm_ops->set_boot_acl)
293 return attr->mode;
294 return 0;
297 return attr->mode;
300 static const struct attribute_group domain_attr_group = {
301 .is_visible = domain_attr_is_visible,
302 .attrs = domain_attrs,
305 static const struct attribute_group *domain_attr_groups[] = {
306 &domain_attr_group,
307 NULL,
310 const struct bus_type tb_bus_type = {
311 .name = "thunderbolt",
312 .match = tb_service_match,
313 .probe = tb_service_probe,
314 .remove = tb_service_remove,
315 .shutdown = tb_service_shutdown,
318 static void tb_domain_release(struct device *dev)
320 struct tb *tb = container_of(dev, struct tb, dev);
322 tb_ctl_free(tb->ctl);
323 destroy_workqueue(tb->wq);
324 ida_free(&tb_domain_ida, tb->index);
325 mutex_destroy(&tb->lock);
326 kfree(tb);
329 const struct device_type tb_domain_type = {
330 .name = "thunderbolt_domain",
331 .release = tb_domain_release,
334 static bool tb_domain_event_cb(void *data, enum tb_cfg_pkg_type type,
335 const void *buf, size_t size)
337 struct tb *tb = data;
339 if (!tb->cm_ops->handle_event) {
340 tb_warn(tb, "domain does not have event handler\n");
341 return true;
344 switch (type) {
345 case TB_CFG_PKG_XDOMAIN_REQ:
346 case TB_CFG_PKG_XDOMAIN_RESP:
347 if (tb_is_xdomain_enabled())
348 return tb_xdomain_handle_request(tb, type, buf, size);
349 break;
351 default:
352 tb->cm_ops->handle_event(tb, type, buf, size);
355 return true;
359 * tb_domain_alloc() - Allocate a domain
360 * @nhi: Pointer to the host controller
361 * @timeout_msec: Control channel timeout for non-raw messages
362 * @privsize: Size of the connection manager private data
364 * Allocates and initializes a new Thunderbolt domain. Connection
365 * managers are expected to call this and then fill in @cm_ops
366 * accordingly.
368 * Call tb_domain_put() to release the domain before it has been added
369 * to the system.
371 * Return: allocated domain structure on %NULL in case of error
373 struct tb *tb_domain_alloc(struct tb_nhi *nhi, int timeout_msec, size_t privsize)
375 struct tb *tb;
378 * Make sure the structure sizes map with that the hardware
379 * expects because bit-fields are being used.
381 BUILD_BUG_ON(sizeof(struct tb_regs_switch_header) != 5 * 4);
382 BUILD_BUG_ON(sizeof(struct tb_regs_port_header) != 8 * 4);
383 BUILD_BUG_ON(sizeof(struct tb_regs_hop) != 2 * 4);
385 tb = kzalloc(sizeof(*tb) + privsize, GFP_KERNEL);
386 if (!tb)
387 return NULL;
389 tb->nhi = nhi;
390 mutex_init(&tb->lock);
392 tb->index = ida_alloc(&tb_domain_ida, GFP_KERNEL);
393 if (tb->index < 0)
394 goto err_free;
396 tb->wq = alloc_ordered_workqueue("thunderbolt%d", 0, tb->index);
397 if (!tb->wq)
398 goto err_remove_ida;
400 tb->ctl = tb_ctl_alloc(nhi, tb->index, timeout_msec, tb_domain_event_cb, tb);
401 if (!tb->ctl)
402 goto err_destroy_wq;
404 tb->dev.parent = &nhi->pdev->dev;
405 tb->dev.bus = &tb_bus_type;
406 tb->dev.type = &tb_domain_type;
407 tb->dev.groups = domain_attr_groups;
408 dev_set_name(&tb->dev, "domain%d", tb->index);
409 device_initialize(&tb->dev);
411 return tb;
413 err_destroy_wq:
414 destroy_workqueue(tb->wq);
415 err_remove_ida:
416 ida_free(&tb_domain_ida, tb->index);
417 err_free:
418 kfree(tb);
420 return NULL;
424 * tb_domain_add() - Add domain to the system
425 * @tb: Domain to add
426 * @reset: Issue reset to the host router
428 * Starts the domain and adds it to the system. Hotplugging devices will
429 * work after this has been returned successfully. In order to remove
430 * and release the domain after this function has been called, call
431 * tb_domain_remove().
433 * Return: %0 in case of success and negative errno in case of error
435 int tb_domain_add(struct tb *tb, bool reset)
437 int ret;
439 if (WARN_ON(!tb->cm_ops))
440 return -EINVAL;
442 mutex_lock(&tb->lock);
444 * tb_schedule_hotplug_handler may be called as soon as the config
445 * channel is started. Thats why we have to hold the lock here.
447 tb_ctl_start(tb->ctl);
449 if (tb->cm_ops->driver_ready) {
450 ret = tb->cm_ops->driver_ready(tb);
451 if (ret)
452 goto err_ctl_stop;
455 tb_dbg(tb, "security level set to %s\n",
456 tb_security_names[tb->security_level]);
458 ret = device_add(&tb->dev);
459 if (ret)
460 goto err_ctl_stop;
462 /* Start the domain */
463 if (tb->cm_ops->start) {
464 ret = tb->cm_ops->start(tb, reset);
465 if (ret)
466 goto err_domain_del;
469 /* This starts event processing */
470 mutex_unlock(&tb->lock);
472 device_init_wakeup(&tb->dev, true);
474 pm_runtime_no_callbacks(&tb->dev);
475 pm_runtime_set_active(&tb->dev);
476 pm_runtime_enable(&tb->dev);
477 pm_runtime_set_autosuspend_delay(&tb->dev, TB_AUTOSUSPEND_DELAY);
478 pm_runtime_mark_last_busy(&tb->dev);
479 pm_runtime_use_autosuspend(&tb->dev);
481 return 0;
483 err_domain_del:
484 device_del(&tb->dev);
485 err_ctl_stop:
486 tb_ctl_stop(tb->ctl);
487 mutex_unlock(&tb->lock);
489 return ret;
493 * tb_domain_remove() - Removes and releases a domain
494 * @tb: Domain to remove
496 * Stops the domain, removes it from the system and releases all
497 * resources once the last reference has been released.
499 void tb_domain_remove(struct tb *tb)
501 mutex_lock(&tb->lock);
502 if (tb->cm_ops->stop)
503 tb->cm_ops->stop(tb);
504 /* Stop the domain control traffic */
505 tb_ctl_stop(tb->ctl);
506 mutex_unlock(&tb->lock);
508 flush_workqueue(tb->wq);
510 if (tb->cm_ops->deinit)
511 tb->cm_ops->deinit(tb);
513 device_unregister(&tb->dev);
517 * tb_domain_suspend_noirq() - Suspend a domain
518 * @tb: Domain to suspend
520 * Suspends all devices in the domain and stops the control channel.
522 int tb_domain_suspend_noirq(struct tb *tb)
524 int ret = 0;
527 * The control channel interrupt is left enabled during suspend
528 * and taking the lock here prevents any events happening before
529 * we actually have stopped the domain and the control channel.
531 mutex_lock(&tb->lock);
532 if (tb->cm_ops->suspend_noirq)
533 ret = tb->cm_ops->suspend_noirq(tb);
534 if (!ret)
535 tb_ctl_stop(tb->ctl);
536 mutex_unlock(&tb->lock);
538 return ret;
542 * tb_domain_resume_noirq() - Resume a domain
543 * @tb: Domain to resume
545 * Re-starts the control channel, and resumes all devices connected to
546 * the domain.
548 int tb_domain_resume_noirq(struct tb *tb)
550 int ret = 0;
552 mutex_lock(&tb->lock);
553 tb_ctl_start(tb->ctl);
554 if (tb->cm_ops->resume_noirq)
555 ret = tb->cm_ops->resume_noirq(tb);
556 mutex_unlock(&tb->lock);
558 return ret;
561 int tb_domain_suspend(struct tb *tb)
563 return tb->cm_ops->suspend ? tb->cm_ops->suspend(tb) : 0;
566 int tb_domain_freeze_noirq(struct tb *tb)
568 int ret = 0;
570 mutex_lock(&tb->lock);
571 if (tb->cm_ops->freeze_noirq)
572 ret = tb->cm_ops->freeze_noirq(tb);
573 if (!ret)
574 tb_ctl_stop(tb->ctl);
575 mutex_unlock(&tb->lock);
577 return ret;
580 int tb_domain_thaw_noirq(struct tb *tb)
582 int ret = 0;
584 mutex_lock(&tb->lock);
585 tb_ctl_start(tb->ctl);
586 if (tb->cm_ops->thaw_noirq)
587 ret = tb->cm_ops->thaw_noirq(tb);
588 mutex_unlock(&tb->lock);
590 return ret;
593 void tb_domain_complete(struct tb *tb)
595 if (tb->cm_ops->complete)
596 tb->cm_ops->complete(tb);
599 int tb_domain_runtime_suspend(struct tb *tb)
601 if (tb->cm_ops->runtime_suspend) {
602 int ret = tb->cm_ops->runtime_suspend(tb);
603 if (ret)
604 return ret;
606 tb_ctl_stop(tb->ctl);
607 return 0;
610 int tb_domain_runtime_resume(struct tb *tb)
612 tb_ctl_start(tb->ctl);
613 if (tb->cm_ops->runtime_resume) {
614 int ret = tb->cm_ops->runtime_resume(tb);
615 if (ret)
616 return ret;
618 return 0;
622 * tb_domain_disapprove_switch() - Disapprove switch
623 * @tb: Domain the switch belongs to
624 * @sw: Switch to disapprove
626 * This will disconnect PCIe tunnel from parent to this @sw.
628 * Return: %0 on success and negative errno in case of failure.
630 int tb_domain_disapprove_switch(struct tb *tb, struct tb_switch *sw)
632 if (!tb->cm_ops->disapprove_switch)
633 return -EPERM;
635 return tb->cm_ops->disapprove_switch(tb, sw);
639 * tb_domain_approve_switch() - Approve switch
640 * @tb: Domain the switch belongs to
641 * @sw: Switch to approve
643 * This will approve switch by connection manager specific means. In
644 * case of success the connection manager will create PCIe tunnel from
645 * parent to @sw.
647 int tb_domain_approve_switch(struct tb *tb, struct tb_switch *sw)
649 struct tb_switch *parent_sw;
651 if (!tb->cm_ops->approve_switch)
652 return -EPERM;
654 /* The parent switch must be authorized before this one */
655 parent_sw = tb_to_switch(sw->dev.parent);
656 if (!parent_sw || !parent_sw->authorized)
657 return -EINVAL;
659 return tb->cm_ops->approve_switch(tb, sw);
663 * tb_domain_approve_switch_key() - Approve switch and add key
664 * @tb: Domain the switch belongs to
665 * @sw: Switch to approve
667 * For switches that support secure connect, this function first adds
668 * key to the switch NVM using connection manager specific means. If
669 * adding the key is successful, the switch is approved and connected.
671 * Return: %0 on success and negative errno in case of failure.
673 int tb_domain_approve_switch_key(struct tb *tb, struct tb_switch *sw)
675 struct tb_switch *parent_sw;
676 int ret;
678 if (!tb->cm_ops->approve_switch || !tb->cm_ops->add_switch_key)
679 return -EPERM;
681 /* The parent switch must be authorized before this one */
682 parent_sw = tb_to_switch(sw->dev.parent);
683 if (!parent_sw || !parent_sw->authorized)
684 return -EINVAL;
686 ret = tb->cm_ops->add_switch_key(tb, sw);
687 if (ret)
688 return ret;
690 return tb->cm_ops->approve_switch(tb, sw);
694 * tb_domain_challenge_switch_key() - Challenge and approve switch
695 * @tb: Domain the switch belongs to
696 * @sw: Switch to approve
698 * For switches that support secure connect, this function generates
699 * random challenge and sends it to the switch. The switch responds to
700 * this and if the response matches our random challenge, the switch is
701 * approved and connected.
703 * Return: %0 on success and negative errno in case of failure.
705 int tb_domain_challenge_switch_key(struct tb *tb, struct tb_switch *sw)
707 u8 challenge[TB_SWITCH_KEY_SIZE];
708 u8 response[TB_SWITCH_KEY_SIZE];
709 u8 hmac[TB_SWITCH_KEY_SIZE];
710 struct tb_switch *parent_sw;
711 struct crypto_shash *tfm;
712 struct shash_desc *shash;
713 int ret;
715 if (!tb->cm_ops->approve_switch || !tb->cm_ops->challenge_switch_key)
716 return -EPERM;
718 /* The parent switch must be authorized before this one */
719 parent_sw = tb_to_switch(sw->dev.parent);
720 if (!parent_sw || !parent_sw->authorized)
721 return -EINVAL;
723 get_random_bytes(challenge, sizeof(challenge));
724 ret = tb->cm_ops->challenge_switch_key(tb, sw, challenge, response);
725 if (ret)
726 return ret;
728 tfm = crypto_alloc_shash("hmac(sha256)", 0, 0);
729 if (IS_ERR(tfm))
730 return PTR_ERR(tfm);
732 ret = crypto_shash_setkey(tfm, sw->key, TB_SWITCH_KEY_SIZE);
733 if (ret)
734 goto err_free_tfm;
736 shash = kzalloc(sizeof(*shash) + crypto_shash_descsize(tfm),
737 GFP_KERNEL);
738 if (!shash) {
739 ret = -ENOMEM;
740 goto err_free_tfm;
743 shash->tfm = tfm;
745 memset(hmac, 0, sizeof(hmac));
746 ret = crypto_shash_digest(shash, challenge, sizeof(hmac), hmac);
747 if (ret)
748 goto err_free_shash;
750 /* The returned HMAC must match the one we calculated */
751 if (memcmp(response, hmac, sizeof(hmac))) {
752 ret = -EKEYREJECTED;
753 goto err_free_shash;
756 crypto_free_shash(tfm);
757 kfree(shash);
759 return tb->cm_ops->approve_switch(tb, sw);
761 err_free_shash:
762 kfree(shash);
763 err_free_tfm:
764 crypto_free_shash(tfm);
766 return ret;
770 * tb_domain_disconnect_pcie_paths() - Disconnect all PCIe paths
771 * @tb: Domain whose PCIe paths to disconnect
773 * This needs to be called in preparation for NVM upgrade of the host
774 * controller. Makes sure all PCIe paths are disconnected.
776 * Return %0 on success and negative errno in case of error.
778 int tb_domain_disconnect_pcie_paths(struct tb *tb)
780 if (!tb->cm_ops->disconnect_pcie_paths)
781 return -EPERM;
783 return tb->cm_ops->disconnect_pcie_paths(tb);
787 * tb_domain_approve_xdomain_paths() - Enable DMA paths for XDomain
788 * @tb: Domain enabling the DMA paths
789 * @xd: XDomain DMA paths are created to
790 * @transmit_path: HopID we are using to send out packets
791 * @transmit_ring: DMA ring used to send out packets
792 * @receive_path: HopID the other end is using to send packets to us
793 * @receive_ring: DMA ring used to receive packets from @receive_path
795 * Calls connection manager specific method to enable DMA paths to the
796 * XDomain in question.
798 * Return: 0% in case of success and negative errno otherwise. In
799 * particular returns %-ENOTSUPP if the connection manager
800 * implementation does not support XDomains.
802 int tb_domain_approve_xdomain_paths(struct tb *tb, struct tb_xdomain *xd,
803 int transmit_path, int transmit_ring,
804 int receive_path, int receive_ring)
806 if (!tb->cm_ops->approve_xdomain_paths)
807 return -ENOTSUPP;
809 return tb->cm_ops->approve_xdomain_paths(tb, xd, transmit_path,
810 transmit_ring, receive_path, receive_ring);
814 * tb_domain_disconnect_xdomain_paths() - Disable DMA paths for XDomain
815 * @tb: Domain disabling the DMA paths
816 * @xd: XDomain whose DMA paths are disconnected
817 * @transmit_path: HopID we are using to send out packets
818 * @transmit_ring: DMA ring used to send out packets
819 * @receive_path: HopID the other end is using to send packets to us
820 * @receive_ring: DMA ring used to receive packets from @receive_path
822 * Calls connection manager specific method to disconnect DMA paths to
823 * the XDomain in question.
825 * Return: 0% in case of success and negative errno otherwise. In
826 * particular returns %-ENOTSUPP if the connection manager
827 * implementation does not support XDomains.
829 int tb_domain_disconnect_xdomain_paths(struct tb *tb, struct tb_xdomain *xd,
830 int transmit_path, int transmit_ring,
831 int receive_path, int receive_ring)
833 if (!tb->cm_ops->disconnect_xdomain_paths)
834 return -ENOTSUPP;
836 return tb->cm_ops->disconnect_xdomain_paths(tb, xd, transmit_path,
837 transmit_ring, receive_path, receive_ring);
840 static int disconnect_xdomain(struct device *dev, void *data)
842 struct tb_xdomain *xd;
843 struct tb *tb = data;
844 int ret = 0;
846 xd = tb_to_xdomain(dev);
847 if (xd && xd->tb == tb)
848 ret = tb_xdomain_disable_all_paths(xd);
850 return ret;
854 * tb_domain_disconnect_all_paths() - Disconnect all paths for the domain
855 * @tb: Domain whose paths are disconnected
857 * This function can be used to disconnect all paths (PCIe, XDomain) for
858 * example in preparation for host NVM firmware upgrade. After this is
859 * called the paths cannot be established without resetting the switch.
861 * Return: %0 in case of success and negative errno otherwise.
863 int tb_domain_disconnect_all_paths(struct tb *tb)
865 int ret;
867 ret = tb_domain_disconnect_pcie_paths(tb);
868 if (ret)
869 return ret;
871 return bus_for_each_dev(&tb_bus_type, NULL, tb, disconnect_xdomain);
874 int tb_domain_init(void)
876 int ret;
878 tb_debugfs_init();
879 tb_acpi_init();
881 ret = tb_xdomain_init();
882 if (ret)
883 goto err_acpi;
884 ret = bus_register(&tb_bus_type);
885 if (ret)
886 goto err_xdomain;
888 return 0;
890 err_xdomain:
891 tb_xdomain_exit();
892 err_acpi:
893 tb_acpi_exit();
894 tb_debugfs_exit();
896 return ret;
899 void tb_domain_exit(void)
901 bus_unregister(&tb_bus_type);
902 ida_destroy(&tb_domain_ida);
903 tb_nvm_exit();
904 tb_xdomain_exit();
905 tb_acpi_exit();
906 tb_debugfs_exit();