1 // SPDX-License-Identifier: GPL-2.0
3 * Thunderbolt bus support
5 * Copyright (C) 2017, Intel Corporation
6 * Author: Mika Westerberg <mika.westerberg@linux.intel.com>
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>
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))
29 if (id->match_flags & TBSVC_MATCH_PROTOCOL_ID) {
30 if (id->protocol_id != svc->prtcid)
34 if (id->match_flags & TBSVC_MATCH_PROTOCOL_VERSION) {
35 if (id->protocol_version != svc->prtcvers)
39 if (id->match_flags & TBSVC_MATCH_PROTOCOL_VERSION) {
40 if (id->protocol_revision != svc->prtcrevs)
47 static const struct tb_service_id *__tb_service_match(struct device *dev,
48 struct device_driver *drv)
50 struct tb_service_driver *driver;
51 const struct tb_service_id *ids;
52 struct tb_service *svc;
54 svc = tb_to_service(dev);
58 driver = container_of(drv, struct tb_service_driver, driver);
59 if (!driver->id_table)
62 for (ids = driver->id_table; ids->match_flags != 0; ids++) {
63 if (match_service_id(ids, svc))
70 static int tb_service_match(struct device *dev, 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);
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)
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,
123 struct tb *tb = container_of(dev, struct tb, dev);
128 uuids = kcalloc(tb->nboot_acl, sizeof(uuid_t), GFP_KERNEL);
132 pm_runtime_get_sync(&tb->dev);
134 if (mutex_lock_interruptible(&tb->lock)) {
138 ret = tb->cm_ops->get_boot_acl(tb, uuids, tb->nboot_acl);
140 mutex_unlock(&tb->lock);
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 += scnprintf(buf + ret, PAGE_SIZE - ret, "%pUb",
150 ret += scnprintf(buf + ret, PAGE_SIZE - ret, "%s",
151 i < tb->nboot_acl - 1 ? "," : "\n");
155 pm_runtime_mark_last_busy(&tb->dev);
156 pm_runtime_put_autosuspend(&tb->dev);
162 static ssize_t boot_acl_store(struct device *dev, struct device_attribute *attr,
163 const char *buf, size_t count)
165 struct tb *tb = container_of(dev, struct tb, dev);
166 char *str, *s, *uuid_str;
172 * Make sure the value is not bigger than tb->nboot_acl * UUID
173 * length + commas and optional "\n". Also the smallest allowable
174 * string is tb->nboot_acl * ",".
176 if (count > (UUID_STRING_LEN + 1) * tb->nboot_acl + 1)
178 if (count < tb->nboot_acl - 1)
181 str = kstrdup(buf, GFP_KERNEL);
185 acl = kcalloc(tb->nboot_acl, sizeof(uuid_t), GFP_KERNEL);
191 uuid_str = strim(str);
192 while ((s = strsep(&uuid_str, ",")) != NULL && i < tb->nboot_acl) {
193 size_t len = strlen(s);
196 if (len != UUID_STRING_LEN) {
200 ret = uuid_parse(s, &acl[i]);
208 if (s || i < tb->nboot_acl) {
213 pm_runtime_get_sync(&tb->dev);
215 if (mutex_lock_interruptible(&tb->lock)) {
219 ret = tb->cm_ops->set_boot_acl(tb, acl, tb->nboot_acl);
221 /* Notify userspace about the change */
222 kobject_uevent(&tb->dev.kobj, KOBJ_CHANGE);
224 mutex_unlock(&tb->lock);
227 pm_runtime_mark_last_busy(&tb->dev);
228 pm_runtime_put_autosuspend(&tb->dev);
236 static DEVICE_ATTR_RW(boot_acl);
238 static ssize_t deauthorization_show(struct device *dev,
239 struct device_attribute *attr,
242 const struct tb *tb = container_of(dev, struct tb, dev);
243 bool deauthorization = false;
245 /* Only meaningful if authorization is supported */
246 if (tb->security_level == TB_SECURITY_USER ||
247 tb->security_level == TB_SECURITY_SECURE)
248 deauthorization = !!tb->cm_ops->disapprove_switch;
250 return sprintf(buf, "%d\n", deauthorization);
252 static DEVICE_ATTR_RO(deauthorization);
254 static ssize_t iommu_dma_protection_show(struct device *dev,
255 struct device_attribute *attr,
258 struct tb *tb = container_of(dev, struct tb, dev);
260 return sysfs_emit(buf, "%d\n", tb->nhi->iommu_dma_protection);
262 static DEVICE_ATTR_RO(iommu_dma_protection);
264 static ssize_t security_show(struct device *dev, struct device_attribute *attr,
267 struct tb *tb = container_of(dev, struct tb, dev);
268 const char *name = "unknown";
270 if (tb->security_level < ARRAY_SIZE(tb_security_names))
271 name = tb_security_names[tb->security_level];
273 return sprintf(buf, "%s\n", name);
275 static DEVICE_ATTR_RO(security);
277 static struct attribute *domain_attrs[] = {
278 &dev_attr_boot_acl.attr,
279 &dev_attr_deauthorization.attr,
280 &dev_attr_iommu_dma_protection.attr,
281 &dev_attr_security.attr,
285 static umode_t domain_attr_is_visible(struct kobject *kobj,
286 struct attribute *attr, int n)
288 struct device *dev = kobj_to_dev(kobj);
289 struct tb *tb = container_of(dev, struct tb, dev);
291 if (attr == &dev_attr_boot_acl.attr) {
293 tb->cm_ops->get_boot_acl &&
294 tb->cm_ops->set_boot_acl)
302 static const struct attribute_group domain_attr_group = {
303 .is_visible = domain_attr_is_visible,
304 .attrs = domain_attrs,
307 static const struct attribute_group *domain_attr_groups[] = {
312 struct bus_type tb_bus_type = {
313 .name = "thunderbolt",
314 .match = tb_service_match,
315 .probe = tb_service_probe,
316 .remove = tb_service_remove,
317 .shutdown = tb_service_shutdown,
320 static void tb_domain_release(struct device *dev)
322 struct tb *tb = container_of(dev, struct tb, dev);
324 tb_ctl_free(tb->ctl);
325 destroy_workqueue(tb->wq);
326 ida_simple_remove(&tb_domain_ida, tb->index);
327 mutex_destroy(&tb->lock);
331 struct device_type tb_domain_type = {
332 .name = "thunderbolt_domain",
333 .release = tb_domain_release,
336 static bool tb_domain_event_cb(void *data, enum tb_cfg_pkg_type type,
337 const void *buf, size_t size)
339 struct tb *tb = data;
341 if (!tb->cm_ops->handle_event) {
342 tb_warn(tb, "domain does not have event handler\n");
347 case TB_CFG_PKG_XDOMAIN_REQ:
348 case TB_CFG_PKG_XDOMAIN_RESP:
349 if (tb_is_xdomain_enabled())
350 return tb_xdomain_handle_request(tb, type, buf, size);
354 tb->cm_ops->handle_event(tb, type, buf, size);
361 * tb_domain_alloc() - Allocate a domain
362 * @nhi: Pointer to the host controller
363 * @timeout_msec: Control channel timeout for non-raw messages
364 * @privsize: Size of the connection manager private data
366 * Allocates and initializes a new Thunderbolt domain. Connection
367 * managers are expected to call this and then fill in @cm_ops
370 * Call tb_domain_put() to release the domain before it has been added
373 * Return: allocated domain structure on %NULL in case of error
375 struct tb *tb_domain_alloc(struct tb_nhi *nhi, int timeout_msec, size_t privsize)
380 * Make sure the structure sizes map with that the hardware
381 * expects because bit-fields are being used.
383 BUILD_BUG_ON(sizeof(struct tb_regs_switch_header) != 5 * 4);
384 BUILD_BUG_ON(sizeof(struct tb_regs_port_header) != 8 * 4);
385 BUILD_BUG_ON(sizeof(struct tb_regs_hop) != 2 * 4);
387 tb = kzalloc(sizeof(*tb) + privsize, GFP_KERNEL);
392 mutex_init(&tb->lock);
394 tb->index = ida_simple_get(&tb_domain_ida, 0, 0, GFP_KERNEL);
398 tb->wq = alloc_ordered_workqueue("thunderbolt%d", 0, tb->index);
402 tb->ctl = tb_ctl_alloc(nhi, timeout_msec, tb_domain_event_cb, tb);
406 tb->dev.parent = &nhi->pdev->dev;
407 tb->dev.bus = &tb_bus_type;
408 tb->dev.type = &tb_domain_type;
409 tb->dev.groups = domain_attr_groups;
410 dev_set_name(&tb->dev, "domain%d", tb->index);
411 device_initialize(&tb->dev);
416 destroy_workqueue(tb->wq);
418 ida_simple_remove(&tb_domain_ida, tb->index);
426 * tb_domain_add() - Add domain to the system
429 * Starts the domain and adds it to the system. Hotplugging devices will
430 * work after this has been returned successfully. In order to remove
431 * and release the domain after this function has been called, call
432 * tb_domain_remove().
434 * Return: %0 in case of success and negative errno in case of error
436 int tb_domain_add(struct tb *tb)
440 if (WARN_ON(!tb->cm_ops))
443 mutex_lock(&tb->lock);
445 * tb_schedule_hotplug_handler may be called as soon as the config
446 * channel is started. Thats why we have to hold the lock here.
448 tb_ctl_start(tb->ctl);
450 if (tb->cm_ops->driver_ready) {
451 ret = tb->cm_ops->driver_ready(tb);
456 tb_dbg(tb, "security level set to %s\n",
457 tb_security_names[tb->security_level]);
459 ret = device_add(&tb->dev);
463 /* Start the domain */
464 if (tb->cm_ops->start) {
465 ret = tb->cm_ops->start(tb);
470 /* This starts event processing */
471 mutex_unlock(&tb->lock);
473 device_init_wakeup(&tb->dev, true);
475 pm_runtime_no_callbacks(&tb->dev);
476 pm_runtime_set_active(&tb->dev);
477 pm_runtime_enable(&tb->dev);
478 pm_runtime_set_autosuspend_delay(&tb->dev, TB_AUTOSUSPEND_DELAY);
479 pm_runtime_mark_last_busy(&tb->dev);
480 pm_runtime_use_autosuspend(&tb->dev);
485 device_del(&tb->dev);
487 tb_ctl_stop(tb->ctl);
488 mutex_unlock(&tb->lock);
494 * tb_domain_remove() - Removes and releases a domain
495 * @tb: Domain to remove
497 * Stops the domain, removes it from the system and releases all
498 * resources once the last reference has been released.
500 void tb_domain_remove(struct tb *tb)
502 mutex_lock(&tb->lock);
503 if (tb->cm_ops->stop)
504 tb->cm_ops->stop(tb);
505 /* Stop the domain control traffic */
506 tb_ctl_stop(tb->ctl);
507 mutex_unlock(&tb->lock);
509 flush_workqueue(tb->wq);
510 device_unregister(&tb->dev);
514 * tb_domain_suspend_noirq() - Suspend a domain
515 * @tb: Domain to suspend
517 * Suspends all devices in the domain and stops the control channel.
519 int tb_domain_suspend_noirq(struct tb *tb)
524 * The control channel interrupt is left enabled during suspend
525 * and taking the lock here prevents any events happening before
526 * we actually have stopped the domain and the control channel.
528 mutex_lock(&tb->lock);
529 if (tb->cm_ops->suspend_noirq)
530 ret = tb->cm_ops->suspend_noirq(tb);
532 tb_ctl_stop(tb->ctl);
533 mutex_unlock(&tb->lock);
539 * tb_domain_resume_noirq() - Resume a domain
540 * @tb: Domain to resume
542 * Re-starts the control channel, and resumes all devices connected to
545 int tb_domain_resume_noirq(struct tb *tb)
549 mutex_lock(&tb->lock);
550 tb_ctl_start(tb->ctl);
551 if (tb->cm_ops->resume_noirq)
552 ret = tb->cm_ops->resume_noirq(tb);
553 mutex_unlock(&tb->lock);
558 int tb_domain_suspend(struct tb *tb)
560 return tb->cm_ops->suspend ? tb->cm_ops->suspend(tb) : 0;
563 int tb_domain_freeze_noirq(struct tb *tb)
567 mutex_lock(&tb->lock);
568 if (tb->cm_ops->freeze_noirq)
569 ret = tb->cm_ops->freeze_noirq(tb);
571 tb_ctl_stop(tb->ctl);
572 mutex_unlock(&tb->lock);
577 int tb_domain_thaw_noirq(struct tb *tb)
581 mutex_lock(&tb->lock);
582 tb_ctl_start(tb->ctl);
583 if (tb->cm_ops->thaw_noirq)
584 ret = tb->cm_ops->thaw_noirq(tb);
585 mutex_unlock(&tb->lock);
590 void tb_domain_complete(struct tb *tb)
592 if (tb->cm_ops->complete)
593 tb->cm_ops->complete(tb);
596 int tb_domain_runtime_suspend(struct tb *tb)
598 if (tb->cm_ops->runtime_suspend) {
599 int ret = tb->cm_ops->runtime_suspend(tb);
603 tb_ctl_stop(tb->ctl);
607 int tb_domain_runtime_resume(struct tb *tb)
609 tb_ctl_start(tb->ctl);
610 if (tb->cm_ops->runtime_resume) {
611 int ret = tb->cm_ops->runtime_resume(tb);
619 * tb_domain_disapprove_switch() - Disapprove switch
620 * @tb: Domain the switch belongs to
621 * @sw: Switch to disapprove
623 * This will disconnect PCIe tunnel from parent to this @sw.
625 * Return: %0 on success and negative errno in case of failure.
627 int tb_domain_disapprove_switch(struct tb *tb, struct tb_switch *sw)
629 if (!tb->cm_ops->disapprove_switch)
632 return tb->cm_ops->disapprove_switch(tb, sw);
636 * tb_domain_approve_switch() - Approve switch
637 * @tb: Domain the switch belongs to
638 * @sw: Switch to approve
640 * This will approve switch by connection manager specific means. In
641 * case of success the connection manager will create PCIe tunnel from
644 int tb_domain_approve_switch(struct tb *tb, struct tb_switch *sw)
646 struct tb_switch *parent_sw;
648 if (!tb->cm_ops->approve_switch)
651 /* The parent switch must be authorized before this one */
652 parent_sw = tb_to_switch(sw->dev.parent);
653 if (!parent_sw || !parent_sw->authorized)
656 return tb->cm_ops->approve_switch(tb, sw);
660 * tb_domain_approve_switch_key() - Approve switch and add key
661 * @tb: Domain the switch belongs to
662 * @sw: Switch to approve
664 * For switches that support secure connect, this function first adds
665 * key to the switch NVM using connection manager specific means. If
666 * adding the key is successful, the switch is approved and connected.
668 * Return: %0 on success and negative errno in case of failure.
670 int tb_domain_approve_switch_key(struct tb *tb, struct tb_switch *sw)
672 struct tb_switch *parent_sw;
675 if (!tb->cm_ops->approve_switch || !tb->cm_ops->add_switch_key)
678 /* The parent switch must be authorized before this one */
679 parent_sw = tb_to_switch(sw->dev.parent);
680 if (!parent_sw || !parent_sw->authorized)
683 ret = tb->cm_ops->add_switch_key(tb, sw);
687 return tb->cm_ops->approve_switch(tb, sw);
691 * tb_domain_challenge_switch_key() - Challenge and approve switch
692 * @tb: Domain the switch belongs to
693 * @sw: Switch to approve
695 * For switches that support secure connect, this function generates
696 * random challenge and sends it to the switch. The switch responds to
697 * this and if the response matches our random challenge, the switch is
698 * approved and connected.
700 * Return: %0 on success and negative errno in case of failure.
702 int tb_domain_challenge_switch_key(struct tb *tb, struct tb_switch *sw)
704 u8 challenge[TB_SWITCH_KEY_SIZE];
705 u8 response[TB_SWITCH_KEY_SIZE];
706 u8 hmac[TB_SWITCH_KEY_SIZE];
707 struct tb_switch *parent_sw;
708 struct crypto_shash *tfm;
709 struct shash_desc *shash;
712 if (!tb->cm_ops->approve_switch || !tb->cm_ops->challenge_switch_key)
715 /* The parent switch must be authorized before this one */
716 parent_sw = tb_to_switch(sw->dev.parent);
717 if (!parent_sw || !parent_sw->authorized)
720 get_random_bytes(challenge, sizeof(challenge));
721 ret = tb->cm_ops->challenge_switch_key(tb, sw, challenge, response);
725 tfm = crypto_alloc_shash("hmac(sha256)", 0, 0);
729 ret = crypto_shash_setkey(tfm, sw->key, TB_SWITCH_KEY_SIZE);
733 shash = kzalloc(sizeof(*shash) + crypto_shash_descsize(tfm),
742 memset(hmac, 0, sizeof(hmac));
743 ret = crypto_shash_digest(shash, challenge, sizeof(hmac), hmac);
747 /* The returned HMAC must match the one we calculated */
748 if (memcmp(response, hmac, sizeof(hmac))) {
753 crypto_free_shash(tfm);
756 return tb->cm_ops->approve_switch(tb, sw);
761 crypto_free_shash(tfm);
767 * tb_domain_disconnect_pcie_paths() - Disconnect all PCIe paths
768 * @tb: Domain whose PCIe paths to disconnect
770 * This needs to be called in preparation for NVM upgrade of the host
771 * controller. Makes sure all PCIe paths are disconnected.
773 * Return %0 on success and negative errno in case of error.
775 int tb_domain_disconnect_pcie_paths(struct tb *tb)
777 if (!tb->cm_ops->disconnect_pcie_paths)
780 return tb->cm_ops->disconnect_pcie_paths(tb);
784 * tb_domain_approve_xdomain_paths() - Enable DMA paths for XDomain
785 * @tb: Domain enabling the DMA paths
786 * @xd: XDomain DMA paths are created to
787 * @transmit_path: HopID we are using to send out packets
788 * @transmit_ring: DMA ring used to send out packets
789 * @receive_path: HopID the other end is using to send packets to us
790 * @receive_ring: DMA ring used to receive packets from @receive_path
792 * Calls connection manager specific method to enable DMA paths to the
793 * XDomain in question.
795 * Return: 0% in case of success and negative errno otherwise. In
796 * particular returns %-ENOTSUPP if the connection manager
797 * implementation does not support XDomains.
799 int tb_domain_approve_xdomain_paths(struct tb *tb, struct tb_xdomain *xd,
800 int transmit_path, int transmit_ring,
801 int receive_path, int receive_ring)
803 if (!tb->cm_ops->approve_xdomain_paths)
806 return tb->cm_ops->approve_xdomain_paths(tb, xd, transmit_path,
807 transmit_ring, receive_path, receive_ring);
811 * tb_domain_disconnect_xdomain_paths() - Disable DMA paths for XDomain
812 * @tb: Domain disabling the DMA paths
813 * @xd: XDomain whose DMA paths are disconnected
814 * @transmit_path: HopID we are using to send out packets
815 * @transmit_ring: DMA ring used to send out packets
816 * @receive_path: HopID the other end is using to send packets to us
817 * @receive_ring: DMA ring used to receive packets from @receive_path
819 * Calls connection manager specific method to disconnect DMA paths to
820 * the XDomain in question.
822 * Return: 0% in case of success and negative errno otherwise. In
823 * particular returns %-ENOTSUPP if the connection manager
824 * implementation does not support XDomains.
826 int tb_domain_disconnect_xdomain_paths(struct tb *tb, struct tb_xdomain *xd,
827 int transmit_path, int transmit_ring,
828 int receive_path, int receive_ring)
830 if (!tb->cm_ops->disconnect_xdomain_paths)
833 return tb->cm_ops->disconnect_xdomain_paths(tb, xd, transmit_path,
834 transmit_ring, receive_path, receive_ring);
837 static int disconnect_xdomain(struct device *dev, void *data)
839 struct tb_xdomain *xd;
840 struct tb *tb = data;
843 xd = tb_to_xdomain(dev);
844 if (xd && xd->tb == tb)
845 ret = tb_xdomain_disable_all_paths(xd);
851 * tb_domain_disconnect_all_paths() - Disconnect all paths for the domain
852 * @tb: Domain whose paths are disconnected
854 * This function can be used to disconnect all paths (PCIe, XDomain) for
855 * example in preparation for host NVM firmware upgrade. After this is
856 * called the paths cannot be established without resetting the switch.
858 * Return: %0 in case of success and negative errno otherwise.
860 int tb_domain_disconnect_all_paths(struct tb *tb)
864 ret = tb_domain_disconnect_pcie_paths(tb);
868 return bus_for_each_dev(&tb_bus_type, NULL, tb, disconnect_xdomain);
871 int tb_domain_init(void)
879 ret = tb_xdomain_init();
882 ret = bus_register(&tb_bus_type);
898 void tb_domain_exit(void)
900 bus_unregister(&tb_bus_type);
901 ida_destroy(&tb_domain_ida);