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/dmar.h>
11 #include <linux/idr.h>
12 #include <linux/iommu.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>
21 static DEFINE_IDA(tb_domain_ida);
23 static bool match_service_id(const struct tb_service_id *id,
24 const struct tb_service *svc)
26 if (id->match_flags & TBSVC_MATCH_PROTOCOL_KEY) {
27 if (strcmp(id->protocol_key, svc->key))
31 if (id->match_flags & TBSVC_MATCH_PROTOCOL_ID) {
32 if (id->protocol_id != svc->prtcid)
36 if (id->match_flags & TBSVC_MATCH_PROTOCOL_VERSION) {
37 if (id->protocol_version != svc->prtcvers)
41 if (id->match_flags & TBSVC_MATCH_PROTOCOL_VERSION) {
42 if (id->protocol_revision != svc->prtcrevs)
49 static const struct tb_service_id *__tb_service_match(struct device *dev,
50 struct device_driver *drv)
52 struct tb_service_driver *driver;
53 const struct tb_service_id *ids;
54 struct tb_service *svc;
56 svc = tb_to_service(dev);
60 driver = container_of(drv, struct tb_service_driver, driver);
61 if (!driver->id_table)
64 for (ids = driver->id_table; ids->match_flags != 0; ids++) {
65 if (match_service_id(ids, svc))
72 static int tb_service_match(struct device *dev, struct device_driver *drv)
74 return !!__tb_service_match(dev, drv);
77 static int tb_service_probe(struct device *dev)
79 struct tb_service *svc = tb_to_service(dev);
80 struct tb_service_driver *driver;
81 const struct tb_service_id *id;
83 driver = container_of(dev->driver, struct tb_service_driver, driver);
84 id = __tb_service_match(dev, &driver->driver);
86 return driver->probe(svc, id);
89 static void tb_service_remove(struct device *dev)
91 struct tb_service *svc = tb_to_service(dev);
92 struct tb_service_driver *driver;
94 driver = container_of(dev->driver, struct tb_service_driver, driver);
99 static void tb_service_shutdown(struct device *dev)
101 struct tb_service_driver *driver;
102 struct tb_service *svc;
104 svc = tb_to_service(dev);
105 if (!svc || !dev->driver)
108 driver = container_of(dev->driver, struct tb_service_driver, driver);
109 if (driver->shutdown)
110 driver->shutdown(svc);
113 static const char * const tb_security_names[] = {
114 [TB_SECURITY_NONE] = "none",
115 [TB_SECURITY_USER] = "user",
116 [TB_SECURITY_SECURE] = "secure",
117 [TB_SECURITY_DPONLY] = "dponly",
118 [TB_SECURITY_USBONLY] = "usbonly",
119 [TB_SECURITY_NOPCIE] = "nopcie",
122 static ssize_t boot_acl_show(struct device *dev, struct device_attribute *attr,
125 struct tb *tb = container_of(dev, struct tb, dev);
130 uuids = kcalloc(tb->nboot_acl, sizeof(uuid_t), GFP_KERNEL);
134 pm_runtime_get_sync(&tb->dev);
136 if (mutex_lock_interruptible(&tb->lock)) {
140 ret = tb->cm_ops->get_boot_acl(tb, uuids, tb->nboot_acl);
142 mutex_unlock(&tb->lock);
145 mutex_unlock(&tb->lock);
147 for (ret = 0, i = 0; i < tb->nboot_acl; i++) {
148 if (!uuid_is_null(&uuids[i]))
149 ret += scnprintf(buf + ret, PAGE_SIZE - ret, "%pUb",
152 ret += scnprintf(buf + ret, PAGE_SIZE - ret, "%s",
153 i < tb->nboot_acl - 1 ? "," : "\n");
157 pm_runtime_mark_last_busy(&tb->dev);
158 pm_runtime_put_autosuspend(&tb->dev);
164 static ssize_t boot_acl_store(struct device *dev, struct device_attribute *attr,
165 const char *buf, size_t count)
167 struct tb *tb = container_of(dev, struct tb, dev);
168 char *str, *s, *uuid_str;
174 * Make sure the value is not bigger than tb->nboot_acl * UUID
175 * length + commas and optional "\n". Also the smallest allowable
176 * string is tb->nboot_acl * ",".
178 if (count > (UUID_STRING_LEN + 1) * tb->nboot_acl + 1)
180 if (count < tb->nboot_acl - 1)
183 str = kstrdup(buf, GFP_KERNEL);
187 acl = kcalloc(tb->nboot_acl, sizeof(uuid_t), GFP_KERNEL);
193 uuid_str = strim(str);
194 while ((s = strsep(&uuid_str, ",")) != NULL && i < tb->nboot_acl) {
195 size_t len = strlen(s);
198 if (len != UUID_STRING_LEN) {
202 ret = uuid_parse(s, &acl[i]);
210 if (s || i < tb->nboot_acl) {
215 pm_runtime_get_sync(&tb->dev);
217 if (mutex_lock_interruptible(&tb->lock)) {
221 ret = tb->cm_ops->set_boot_acl(tb, acl, tb->nboot_acl);
223 /* Notify userspace about the change */
224 kobject_uevent(&tb->dev.kobj, KOBJ_CHANGE);
226 mutex_unlock(&tb->lock);
229 pm_runtime_mark_last_busy(&tb->dev);
230 pm_runtime_put_autosuspend(&tb->dev);
238 static DEVICE_ATTR_RW(boot_acl);
240 static ssize_t deauthorization_show(struct device *dev,
241 struct device_attribute *attr,
244 const struct tb *tb = container_of(dev, struct tb, dev);
245 bool deauthorization = false;
247 /* Only meaningful if authorization is supported */
248 if (tb->security_level == TB_SECURITY_USER ||
249 tb->security_level == TB_SECURITY_SECURE)
250 deauthorization = !!tb->cm_ops->disapprove_switch;
252 return sprintf(buf, "%d\n", deauthorization);
254 static DEVICE_ATTR_RO(deauthorization);
256 static ssize_t iommu_dma_protection_show(struct device *dev,
257 struct device_attribute *attr,
261 * Kernel DMA protection is a feature where Thunderbolt security is
262 * handled natively using IOMMU. It is enabled when IOMMU is
263 * enabled and ACPI DMAR table has DMAR_PLATFORM_OPT_IN set.
265 return sprintf(buf, "%d\n",
266 iommu_present(&pci_bus_type) && dmar_platform_optin());
268 static DEVICE_ATTR_RO(iommu_dma_protection);
270 static ssize_t security_show(struct device *dev, struct device_attribute *attr,
273 struct tb *tb = container_of(dev, struct tb, dev);
274 const char *name = "unknown";
276 if (tb->security_level < ARRAY_SIZE(tb_security_names))
277 name = tb_security_names[tb->security_level];
279 return sprintf(buf, "%s\n", name);
281 static DEVICE_ATTR_RO(security);
283 static struct attribute *domain_attrs[] = {
284 &dev_attr_boot_acl.attr,
285 &dev_attr_deauthorization.attr,
286 &dev_attr_iommu_dma_protection.attr,
287 &dev_attr_security.attr,
291 static umode_t domain_attr_is_visible(struct kobject *kobj,
292 struct attribute *attr, int n)
294 struct device *dev = kobj_to_dev(kobj);
295 struct tb *tb = container_of(dev, struct tb, dev);
297 if (attr == &dev_attr_boot_acl.attr) {
299 tb->cm_ops->get_boot_acl &&
300 tb->cm_ops->set_boot_acl)
308 static const struct attribute_group domain_attr_group = {
309 .is_visible = domain_attr_is_visible,
310 .attrs = domain_attrs,
313 static const struct attribute_group *domain_attr_groups[] = {
318 struct bus_type tb_bus_type = {
319 .name = "thunderbolt",
320 .match = tb_service_match,
321 .probe = tb_service_probe,
322 .remove = tb_service_remove,
323 .shutdown = tb_service_shutdown,
326 static void tb_domain_release(struct device *dev)
328 struct tb *tb = container_of(dev, struct tb, dev);
330 tb_ctl_free(tb->ctl);
331 destroy_workqueue(tb->wq);
332 ida_simple_remove(&tb_domain_ida, tb->index);
333 mutex_destroy(&tb->lock);
337 struct device_type tb_domain_type = {
338 .name = "thunderbolt_domain",
339 .release = tb_domain_release,
342 static bool tb_domain_event_cb(void *data, enum tb_cfg_pkg_type type,
343 const void *buf, size_t size)
345 struct tb *tb = data;
347 if (!tb->cm_ops->handle_event) {
348 tb_warn(tb, "domain does not have event handler\n");
353 case TB_CFG_PKG_XDOMAIN_REQ:
354 case TB_CFG_PKG_XDOMAIN_RESP:
355 if (tb_is_xdomain_enabled())
356 return tb_xdomain_handle_request(tb, type, buf, size);
360 tb->cm_ops->handle_event(tb, type, buf, size);
367 * tb_domain_alloc() - Allocate a domain
368 * @nhi: Pointer to the host controller
369 * @timeout_msec: Control channel timeout for non-raw messages
370 * @privsize: Size of the connection manager private data
372 * Allocates and initializes a new Thunderbolt domain. Connection
373 * managers are expected to call this and then fill in @cm_ops
376 * Call tb_domain_put() to release the domain before it has been added
379 * Return: allocated domain structure on %NULL in case of error
381 struct tb *tb_domain_alloc(struct tb_nhi *nhi, int timeout_msec, size_t privsize)
386 * Make sure the structure sizes map with that the hardware
387 * expects because bit-fields are being used.
389 BUILD_BUG_ON(sizeof(struct tb_regs_switch_header) != 5 * 4);
390 BUILD_BUG_ON(sizeof(struct tb_regs_port_header) != 8 * 4);
391 BUILD_BUG_ON(sizeof(struct tb_regs_hop) != 2 * 4);
393 tb = kzalloc(sizeof(*tb) + privsize, GFP_KERNEL);
398 mutex_init(&tb->lock);
400 tb->index = ida_simple_get(&tb_domain_ida, 0, 0, GFP_KERNEL);
404 tb->wq = alloc_ordered_workqueue("thunderbolt%d", 0, tb->index);
408 tb->ctl = tb_ctl_alloc(nhi, timeout_msec, tb_domain_event_cb, tb);
412 tb->dev.parent = &nhi->pdev->dev;
413 tb->dev.bus = &tb_bus_type;
414 tb->dev.type = &tb_domain_type;
415 tb->dev.groups = domain_attr_groups;
416 dev_set_name(&tb->dev, "domain%d", tb->index);
417 device_initialize(&tb->dev);
422 destroy_workqueue(tb->wq);
424 ida_simple_remove(&tb_domain_ida, tb->index);
432 * tb_domain_add() - Add domain to the system
435 * Starts the domain and adds it to the system. Hotplugging devices will
436 * work after this has been returned successfully. In order to remove
437 * and release the domain after this function has been called, call
438 * tb_domain_remove().
440 * Return: %0 in case of success and negative errno in case of error
442 int tb_domain_add(struct tb *tb)
446 if (WARN_ON(!tb->cm_ops))
449 mutex_lock(&tb->lock);
451 * tb_schedule_hotplug_handler may be called as soon as the config
452 * channel is started. Thats why we have to hold the lock here.
454 tb_ctl_start(tb->ctl);
456 if (tb->cm_ops->driver_ready) {
457 ret = tb->cm_ops->driver_ready(tb);
462 tb_dbg(tb, "security level set to %s\n",
463 tb_security_names[tb->security_level]);
465 ret = device_add(&tb->dev);
469 /* Start the domain */
470 if (tb->cm_ops->start) {
471 ret = tb->cm_ops->start(tb);
476 /* This starts event processing */
477 mutex_unlock(&tb->lock);
479 device_init_wakeup(&tb->dev, true);
481 pm_runtime_no_callbacks(&tb->dev);
482 pm_runtime_set_active(&tb->dev);
483 pm_runtime_enable(&tb->dev);
484 pm_runtime_set_autosuspend_delay(&tb->dev, TB_AUTOSUSPEND_DELAY);
485 pm_runtime_mark_last_busy(&tb->dev);
486 pm_runtime_use_autosuspend(&tb->dev);
491 device_del(&tb->dev);
493 tb_ctl_stop(tb->ctl);
494 mutex_unlock(&tb->lock);
500 * tb_domain_remove() - Removes and releases a domain
501 * @tb: Domain to remove
503 * Stops the domain, removes it from the system and releases all
504 * resources once the last reference has been released.
506 void tb_domain_remove(struct tb *tb)
508 mutex_lock(&tb->lock);
509 if (tb->cm_ops->stop)
510 tb->cm_ops->stop(tb);
511 /* Stop the domain control traffic */
512 tb_ctl_stop(tb->ctl);
513 mutex_unlock(&tb->lock);
515 flush_workqueue(tb->wq);
516 device_unregister(&tb->dev);
520 * tb_domain_suspend_noirq() - Suspend a domain
521 * @tb: Domain to suspend
523 * Suspends all devices in the domain and stops the control channel.
525 int tb_domain_suspend_noirq(struct tb *tb)
530 * The control channel interrupt is left enabled during suspend
531 * and taking the lock here prevents any events happening before
532 * we actually have stopped the domain and the control channel.
534 mutex_lock(&tb->lock);
535 if (tb->cm_ops->suspend_noirq)
536 ret = tb->cm_ops->suspend_noirq(tb);
538 tb_ctl_stop(tb->ctl);
539 mutex_unlock(&tb->lock);
545 * tb_domain_resume_noirq() - Resume a domain
546 * @tb: Domain to resume
548 * Re-starts the control channel, and resumes all devices connected to
551 int tb_domain_resume_noirq(struct tb *tb)
555 mutex_lock(&tb->lock);
556 tb_ctl_start(tb->ctl);
557 if (tb->cm_ops->resume_noirq)
558 ret = tb->cm_ops->resume_noirq(tb);
559 mutex_unlock(&tb->lock);
564 int tb_domain_suspend(struct tb *tb)
566 return tb->cm_ops->suspend ? tb->cm_ops->suspend(tb) : 0;
569 int tb_domain_freeze_noirq(struct tb *tb)
573 mutex_lock(&tb->lock);
574 if (tb->cm_ops->freeze_noirq)
575 ret = tb->cm_ops->freeze_noirq(tb);
577 tb_ctl_stop(tb->ctl);
578 mutex_unlock(&tb->lock);
583 int tb_domain_thaw_noirq(struct tb *tb)
587 mutex_lock(&tb->lock);
588 tb_ctl_start(tb->ctl);
589 if (tb->cm_ops->thaw_noirq)
590 ret = tb->cm_ops->thaw_noirq(tb);
591 mutex_unlock(&tb->lock);
596 void tb_domain_complete(struct tb *tb)
598 if (tb->cm_ops->complete)
599 tb->cm_ops->complete(tb);
602 int tb_domain_runtime_suspend(struct tb *tb)
604 if (tb->cm_ops->runtime_suspend) {
605 int ret = tb->cm_ops->runtime_suspend(tb);
609 tb_ctl_stop(tb->ctl);
613 int tb_domain_runtime_resume(struct tb *tb)
615 tb_ctl_start(tb->ctl);
616 if (tb->cm_ops->runtime_resume) {
617 int ret = tb->cm_ops->runtime_resume(tb);
625 * tb_domain_disapprove_switch() - Disapprove switch
626 * @tb: Domain the switch belongs to
627 * @sw: Switch to disapprove
629 * This will disconnect PCIe tunnel from parent to this @sw.
631 * Return: %0 on success and negative errno in case of failure.
633 int tb_domain_disapprove_switch(struct tb *tb, struct tb_switch *sw)
635 if (!tb->cm_ops->disapprove_switch)
638 return tb->cm_ops->disapprove_switch(tb, sw);
642 * tb_domain_approve_switch() - Approve switch
643 * @tb: Domain the switch belongs to
644 * @sw: Switch to approve
646 * This will approve switch by connection manager specific means. In
647 * case of success the connection manager will create PCIe tunnel from
650 int tb_domain_approve_switch(struct tb *tb, struct tb_switch *sw)
652 struct tb_switch *parent_sw;
654 if (!tb->cm_ops->approve_switch)
657 /* The parent switch must be authorized before this one */
658 parent_sw = tb_to_switch(sw->dev.parent);
659 if (!parent_sw || !parent_sw->authorized)
662 return tb->cm_ops->approve_switch(tb, sw);
666 * tb_domain_approve_switch_key() - Approve switch and add key
667 * @tb: Domain the switch belongs to
668 * @sw: Switch to approve
670 * For switches that support secure connect, this function first adds
671 * key to the switch NVM using connection manager specific means. If
672 * adding the key is successful, the switch is approved and connected.
674 * Return: %0 on success and negative errno in case of failure.
676 int tb_domain_approve_switch_key(struct tb *tb, struct tb_switch *sw)
678 struct tb_switch *parent_sw;
681 if (!tb->cm_ops->approve_switch || !tb->cm_ops->add_switch_key)
684 /* The parent switch must be authorized before this one */
685 parent_sw = tb_to_switch(sw->dev.parent);
686 if (!parent_sw || !parent_sw->authorized)
689 ret = tb->cm_ops->add_switch_key(tb, sw);
693 return tb->cm_ops->approve_switch(tb, sw);
697 * tb_domain_challenge_switch_key() - Challenge and approve switch
698 * @tb: Domain the switch belongs to
699 * @sw: Switch to approve
701 * For switches that support secure connect, this function generates
702 * random challenge and sends it to the switch. The switch responds to
703 * this and if the response matches our random challenge, the switch is
704 * approved and connected.
706 * Return: %0 on success and negative errno in case of failure.
708 int tb_domain_challenge_switch_key(struct tb *tb, struct tb_switch *sw)
710 u8 challenge[TB_SWITCH_KEY_SIZE];
711 u8 response[TB_SWITCH_KEY_SIZE];
712 u8 hmac[TB_SWITCH_KEY_SIZE];
713 struct tb_switch *parent_sw;
714 struct crypto_shash *tfm;
715 struct shash_desc *shash;
718 if (!tb->cm_ops->approve_switch || !tb->cm_ops->challenge_switch_key)
721 /* The parent switch must be authorized before this one */
722 parent_sw = tb_to_switch(sw->dev.parent);
723 if (!parent_sw || !parent_sw->authorized)
726 get_random_bytes(challenge, sizeof(challenge));
727 ret = tb->cm_ops->challenge_switch_key(tb, sw, challenge, response);
731 tfm = crypto_alloc_shash("hmac(sha256)", 0, 0);
735 ret = crypto_shash_setkey(tfm, sw->key, TB_SWITCH_KEY_SIZE);
739 shash = kzalloc(sizeof(*shash) + crypto_shash_descsize(tfm),
748 memset(hmac, 0, sizeof(hmac));
749 ret = crypto_shash_digest(shash, challenge, sizeof(hmac), hmac);
753 /* The returned HMAC must match the one we calculated */
754 if (memcmp(response, hmac, sizeof(hmac))) {
759 crypto_free_shash(tfm);
762 return tb->cm_ops->approve_switch(tb, sw);
767 crypto_free_shash(tfm);
773 * tb_domain_disconnect_pcie_paths() - Disconnect all PCIe paths
774 * @tb: Domain whose PCIe paths to disconnect
776 * This needs to be called in preparation for NVM upgrade of the host
777 * controller. Makes sure all PCIe paths are disconnected.
779 * Return %0 on success and negative errno in case of error.
781 int tb_domain_disconnect_pcie_paths(struct tb *tb)
783 if (!tb->cm_ops->disconnect_pcie_paths)
786 return tb->cm_ops->disconnect_pcie_paths(tb);
790 * tb_domain_approve_xdomain_paths() - Enable DMA paths for XDomain
791 * @tb: Domain enabling the DMA paths
792 * @xd: XDomain DMA paths are created to
793 * @transmit_path: HopID we are using to send out packets
794 * @transmit_ring: DMA ring used to send out packets
795 * @receive_path: HopID the other end is using to send packets to us
796 * @receive_ring: DMA ring used to receive packets from @receive_path
798 * Calls connection manager specific method to enable DMA paths to the
799 * XDomain in question.
801 * Return: 0% in case of success and negative errno otherwise. In
802 * particular returns %-ENOTSUPP if the connection manager
803 * implementation does not support XDomains.
805 int tb_domain_approve_xdomain_paths(struct tb *tb, struct tb_xdomain *xd,
806 int transmit_path, int transmit_ring,
807 int receive_path, int receive_ring)
809 if (!tb->cm_ops->approve_xdomain_paths)
812 return tb->cm_ops->approve_xdomain_paths(tb, xd, transmit_path,
813 transmit_ring, receive_path, receive_ring);
817 * tb_domain_disconnect_xdomain_paths() - Disable DMA paths for XDomain
818 * @tb: Domain disabling the DMA paths
819 * @xd: XDomain whose DMA paths are disconnected
820 * @transmit_path: HopID we are using to send out packets
821 * @transmit_ring: DMA ring used to send out packets
822 * @receive_path: HopID the other end is using to send packets to us
823 * @receive_ring: DMA ring used to receive packets from @receive_path
825 * Calls connection manager specific method to disconnect DMA paths to
826 * the XDomain in question.
828 * Return: 0% in case of success and negative errno otherwise. In
829 * particular returns %-ENOTSUPP if the connection manager
830 * implementation does not support XDomains.
832 int tb_domain_disconnect_xdomain_paths(struct tb *tb, struct tb_xdomain *xd,
833 int transmit_path, int transmit_ring,
834 int receive_path, int receive_ring)
836 if (!tb->cm_ops->disconnect_xdomain_paths)
839 return tb->cm_ops->disconnect_xdomain_paths(tb, xd, transmit_path,
840 transmit_ring, receive_path, receive_ring);
843 static int disconnect_xdomain(struct device *dev, void *data)
845 struct tb_xdomain *xd;
846 struct tb *tb = data;
849 xd = tb_to_xdomain(dev);
850 if (xd && xd->tb == tb)
851 ret = tb_xdomain_disable_all_paths(xd);
857 * tb_domain_disconnect_all_paths() - Disconnect all paths for the domain
858 * @tb: Domain whose paths are disconnected
860 * This function can be used to disconnect all paths (PCIe, XDomain) for
861 * example in preparation for host NVM firmware upgrade. After this is
862 * called the paths cannot be established without resetting the switch.
864 * Return: %0 in case of success and negative errno otherwise.
866 int tb_domain_disconnect_all_paths(struct tb *tb)
870 ret = tb_domain_disconnect_pcie_paths(tb);
874 return bus_for_each_dev(&tb_bus_type, NULL, tb, disconnect_xdomain);
877 int tb_domain_init(void)
885 ret = tb_xdomain_init();
888 ret = bus_register(&tb_bus_type);
904 void tb_domain_exit(void)
906 bus_unregister(&tb_bus_type);
907 ida_destroy(&tb_domain_ida);