2 * Thunderbolt bus support
4 * Copyright (C) 2017, Intel Corporation
5 * Author: Mika Westerberg <mika.westerberg@linux.intel.com>
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.
12 #include <linux/device.h>
13 #include <linux/idr.h>
14 #include <linux/module.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 int 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);
101 static void tb_service_shutdown(struct device *dev)
103 struct tb_service_driver *driver;
104 struct tb_service *svc;
106 svc = tb_to_service(dev);
107 if (!svc || !dev->driver)
110 driver = container_of(dev->driver, struct tb_service_driver, driver);
111 if (driver->shutdown)
112 driver->shutdown(svc);
115 static const char * const tb_security_names[] = {
116 [TB_SECURITY_NONE] = "none",
117 [TB_SECURITY_USER] = "user",
118 [TB_SECURITY_SECURE] = "secure",
119 [TB_SECURITY_DPONLY] = "dponly",
120 [TB_SECURITY_USBONLY] = "usbonly",
123 static ssize_t boot_acl_show(struct device *dev, struct device_attribute *attr,
126 struct tb *tb = container_of(dev, struct tb, dev);
131 uuids = kcalloc(tb->nboot_acl, sizeof(uuid_t), GFP_KERNEL);
135 if (mutex_lock_interruptible(&tb->lock)) {
139 ret = tb->cm_ops->get_boot_acl(tb, uuids, tb->nboot_acl);
141 mutex_unlock(&tb->lock);
144 mutex_unlock(&tb->lock);
146 for (ret = 0, i = 0; i < tb->nboot_acl; i++) {
147 if (!uuid_is_null(&uuids[i]))
148 ret += snprintf(buf + ret, PAGE_SIZE - ret, "%pUb",
151 ret += snprintf(buf + ret, PAGE_SIZE - ret, "%s",
152 i < tb->nboot_acl - 1 ? "," : "\n");
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;
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)
176 if (count < tb->nboot_acl - 1)
179 str = kstrdup(buf, GFP_KERNEL);
183 acl = kcalloc(tb->nboot_acl, sizeof(uuid_t), GFP_KERNEL);
189 uuid_str = strim(str);
190 while ((s = strsep(&uuid_str, ",")) != NULL && i < tb->nboot_acl) {
191 size_t len = strlen(s);
194 if (len != UUID_STRING_LEN) {
198 ret = uuid_parse(s, &acl[i]);
206 if (s || i < tb->nboot_acl) {
211 if (mutex_lock_interruptible(&tb->lock)) {
215 ret = tb->cm_ops->set_boot_acl(tb, acl, tb->nboot_acl);
217 /* Notify userspace about the change */
218 kobject_uevent(&tb->dev.kobj, KOBJ_CHANGE);
220 mutex_unlock(&tb->lock);
229 static DEVICE_ATTR_RW(boot_acl);
231 static ssize_t security_show(struct device *dev, struct device_attribute *attr,
234 struct tb *tb = container_of(dev, struct tb, dev);
235 const char *name = "unknown";
237 if (tb->security_level < ARRAY_SIZE(tb_security_names))
238 name = tb_security_names[tb->security_level];
240 return sprintf(buf, "%s\n", name);
242 static DEVICE_ATTR_RO(security);
244 static struct attribute *domain_attrs[] = {
245 &dev_attr_boot_acl.attr,
246 &dev_attr_security.attr,
250 static umode_t domain_attr_is_visible(struct kobject *kobj,
251 struct attribute *attr, int n)
253 struct device *dev = container_of(kobj, struct device, kobj);
254 struct tb *tb = container_of(dev, struct tb, dev);
256 if (attr == &dev_attr_boot_acl.attr) {
258 tb->cm_ops->get_boot_acl &&
259 tb->cm_ops->set_boot_acl)
267 static struct attribute_group domain_attr_group = {
268 .is_visible = domain_attr_is_visible,
269 .attrs = domain_attrs,
272 static const struct attribute_group *domain_attr_groups[] = {
277 struct bus_type tb_bus_type = {
278 .name = "thunderbolt",
279 .match = tb_service_match,
280 .probe = tb_service_probe,
281 .remove = tb_service_remove,
282 .shutdown = tb_service_shutdown,
285 static void tb_domain_release(struct device *dev)
287 struct tb *tb = container_of(dev, struct tb, dev);
289 tb_ctl_free(tb->ctl);
290 destroy_workqueue(tb->wq);
291 ida_simple_remove(&tb_domain_ida, tb->index);
292 mutex_destroy(&tb->lock);
296 struct device_type tb_domain_type = {
297 .name = "thunderbolt_domain",
298 .release = tb_domain_release,
302 * tb_domain_alloc() - Allocate a domain
303 * @nhi: Pointer to the host controller
304 * @privsize: Size of the connection manager private data
306 * Allocates and initializes a new Thunderbolt domain. Connection
307 * managers are expected to call this and then fill in @cm_ops
310 * Call tb_domain_put() to release the domain before it has been added
313 * Return: allocated domain structure on %NULL in case of error
315 struct tb *tb_domain_alloc(struct tb_nhi *nhi, size_t privsize)
320 * Make sure the structure sizes map with that the hardware
321 * expects because bit-fields are being used.
323 BUILD_BUG_ON(sizeof(struct tb_regs_switch_header) != 5 * 4);
324 BUILD_BUG_ON(sizeof(struct tb_regs_port_header) != 8 * 4);
325 BUILD_BUG_ON(sizeof(struct tb_regs_hop) != 2 * 4);
327 tb = kzalloc(sizeof(*tb) + privsize, GFP_KERNEL);
332 mutex_init(&tb->lock);
334 tb->index = ida_simple_get(&tb_domain_ida, 0, 0, GFP_KERNEL);
338 tb->wq = alloc_ordered_workqueue("thunderbolt%d", 0, tb->index);
342 tb->dev.parent = &nhi->pdev->dev;
343 tb->dev.bus = &tb_bus_type;
344 tb->dev.type = &tb_domain_type;
345 tb->dev.groups = domain_attr_groups;
346 dev_set_name(&tb->dev, "domain%d", tb->index);
347 device_initialize(&tb->dev);
352 ida_simple_remove(&tb_domain_ida, tb->index);
359 static bool tb_domain_event_cb(void *data, enum tb_cfg_pkg_type type,
360 const void *buf, size_t size)
362 struct tb *tb = data;
364 if (!tb->cm_ops->handle_event) {
365 tb_warn(tb, "domain does not have event handler\n");
370 case TB_CFG_PKG_XDOMAIN_REQ:
371 case TB_CFG_PKG_XDOMAIN_RESP:
372 return tb_xdomain_handle_request(tb, type, buf, size);
375 tb->cm_ops->handle_event(tb, type, buf, size);
382 * tb_domain_add() - Add domain to the system
385 * Starts the domain and adds it to the system. Hotplugging devices will
386 * work after this has been returned successfully. In order to remove
387 * and release the domain after this function has been called, call
388 * tb_domain_remove().
390 * Return: %0 in case of success and negative errno in case of error
392 int tb_domain_add(struct tb *tb)
396 if (WARN_ON(!tb->cm_ops))
399 mutex_lock(&tb->lock);
401 tb->ctl = tb_ctl_alloc(tb->nhi, tb_domain_event_cb, tb);
408 * tb_schedule_hotplug_handler may be called as soon as the config
409 * channel is started. Thats why we have to hold the lock here.
411 tb_ctl_start(tb->ctl);
413 if (tb->cm_ops->driver_ready) {
414 ret = tb->cm_ops->driver_ready(tb);
419 ret = device_add(&tb->dev);
423 /* Start the domain */
424 if (tb->cm_ops->start) {
425 ret = tb->cm_ops->start(tb);
430 /* This starts event processing */
431 mutex_unlock(&tb->lock);
436 device_del(&tb->dev);
438 tb_ctl_stop(tb->ctl);
440 mutex_unlock(&tb->lock);
446 * tb_domain_remove() - Removes and releases a domain
447 * @tb: Domain to remove
449 * Stops the domain, removes it from the system and releases all
450 * resources once the last reference has been released.
452 void tb_domain_remove(struct tb *tb)
454 mutex_lock(&tb->lock);
455 if (tb->cm_ops->stop)
456 tb->cm_ops->stop(tb);
457 /* Stop the domain control traffic */
458 tb_ctl_stop(tb->ctl);
459 mutex_unlock(&tb->lock);
461 flush_workqueue(tb->wq);
462 device_unregister(&tb->dev);
466 * tb_domain_suspend_noirq() - Suspend a domain
467 * @tb: Domain to suspend
469 * Suspends all devices in the domain and stops the control channel.
471 int tb_domain_suspend_noirq(struct tb *tb)
476 * The control channel interrupt is left enabled during suspend
477 * and taking the lock here prevents any events happening before
478 * we actually have stopped the domain and the control channel.
480 mutex_lock(&tb->lock);
481 if (tb->cm_ops->suspend_noirq)
482 ret = tb->cm_ops->suspend_noirq(tb);
484 tb_ctl_stop(tb->ctl);
485 mutex_unlock(&tb->lock);
491 * tb_domain_resume_noirq() - Resume a domain
492 * @tb: Domain to resume
494 * Re-starts the control channel, and resumes all devices connected to
497 int tb_domain_resume_noirq(struct tb *tb)
501 mutex_lock(&tb->lock);
502 tb_ctl_start(tb->ctl);
503 if (tb->cm_ops->resume_noirq)
504 ret = tb->cm_ops->resume_noirq(tb);
505 mutex_unlock(&tb->lock);
510 int tb_domain_suspend(struct tb *tb)
514 mutex_lock(&tb->lock);
515 if (tb->cm_ops->suspend) {
516 ret = tb->cm_ops->suspend(tb);
518 mutex_unlock(&tb->lock);
522 mutex_unlock(&tb->lock);
526 void tb_domain_complete(struct tb *tb)
528 mutex_lock(&tb->lock);
529 if (tb->cm_ops->complete)
530 tb->cm_ops->complete(tb);
531 mutex_unlock(&tb->lock);
535 * tb_domain_approve_switch() - Approve switch
536 * @tb: Domain the switch belongs to
537 * @sw: Switch to approve
539 * This will approve switch by connection manager specific means. In
540 * case of success the connection manager will create tunnels for all
541 * supported protocols.
543 int tb_domain_approve_switch(struct tb *tb, struct tb_switch *sw)
545 struct tb_switch *parent_sw;
547 if (!tb->cm_ops->approve_switch)
550 /* The parent switch must be authorized before this one */
551 parent_sw = tb_to_switch(sw->dev.parent);
552 if (!parent_sw || !parent_sw->authorized)
555 return tb->cm_ops->approve_switch(tb, sw);
559 * tb_domain_approve_switch_key() - Approve switch and add key
560 * @tb: Domain the switch belongs to
561 * @sw: Switch to approve
563 * For switches that support secure connect, this function first adds
564 * key to the switch NVM using connection manager specific means. If
565 * adding the key is successful, the switch is approved and connected.
567 * Return: %0 on success and negative errno in case of failure.
569 int tb_domain_approve_switch_key(struct tb *tb, struct tb_switch *sw)
571 struct tb_switch *parent_sw;
574 if (!tb->cm_ops->approve_switch || !tb->cm_ops->add_switch_key)
577 /* The parent switch must be authorized before this one */
578 parent_sw = tb_to_switch(sw->dev.parent);
579 if (!parent_sw || !parent_sw->authorized)
582 ret = tb->cm_ops->add_switch_key(tb, sw);
586 return tb->cm_ops->approve_switch(tb, sw);
590 * tb_domain_challenge_switch_key() - Challenge and approve switch
591 * @tb: Domain the switch belongs to
592 * @sw: Switch to approve
594 * For switches that support secure connect, this function generates
595 * random challenge and sends it to the switch. The switch responds to
596 * this and if the response matches our random challenge, the switch is
597 * approved and connected.
599 * Return: %0 on success and negative errno in case of failure.
601 int tb_domain_challenge_switch_key(struct tb *tb, struct tb_switch *sw)
603 u8 challenge[TB_SWITCH_KEY_SIZE];
604 u8 response[TB_SWITCH_KEY_SIZE];
605 u8 hmac[TB_SWITCH_KEY_SIZE];
606 struct tb_switch *parent_sw;
607 struct crypto_shash *tfm;
608 struct shash_desc *shash;
611 if (!tb->cm_ops->approve_switch || !tb->cm_ops->challenge_switch_key)
614 /* The parent switch must be authorized before this one */
615 parent_sw = tb_to_switch(sw->dev.parent);
616 if (!parent_sw || !parent_sw->authorized)
619 get_random_bytes(challenge, sizeof(challenge));
620 ret = tb->cm_ops->challenge_switch_key(tb, sw, challenge, response);
624 tfm = crypto_alloc_shash("hmac(sha256)", 0, 0);
628 ret = crypto_shash_setkey(tfm, sw->key, TB_SWITCH_KEY_SIZE);
632 shash = kzalloc(sizeof(*shash) + crypto_shash_descsize(tfm),
640 shash->flags = CRYPTO_TFM_REQ_MAY_SLEEP;
642 memset(hmac, 0, sizeof(hmac));
643 ret = crypto_shash_digest(shash, challenge, sizeof(hmac), hmac);
647 /* The returned HMAC must match the one we calculated */
648 if (memcmp(response, hmac, sizeof(hmac))) {
653 crypto_free_shash(tfm);
656 return tb->cm_ops->approve_switch(tb, sw);
661 crypto_free_shash(tfm);
667 * tb_domain_disconnect_pcie_paths() - Disconnect all PCIe paths
668 * @tb: Domain whose PCIe paths to disconnect
670 * This needs to be called in preparation for NVM upgrade of the host
671 * controller. Makes sure all PCIe paths are disconnected.
673 * Return %0 on success and negative errno in case of error.
675 int tb_domain_disconnect_pcie_paths(struct tb *tb)
677 if (!tb->cm_ops->disconnect_pcie_paths)
680 return tb->cm_ops->disconnect_pcie_paths(tb);
684 * tb_domain_approve_xdomain_paths() - Enable DMA paths for XDomain
685 * @tb: Domain enabling the DMA paths
686 * @xd: XDomain DMA paths are created to
688 * Calls connection manager specific method to enable DMA paths to the
689 * XDomain in question.
691 * Return: 0% in case of success and negative errno otherwise. In
692 * particular returns %-ENOTSUPP if the connection manager
693 * implementation does not support XDomains.
695 int tb_domain_approve_xdomain_paths(struct tb *tb, struct tb_xdomain *xd)
697 if (!tb->cm_ops->approve_xdomain_paths)
700 return tb->cm_ops->approve_xdomain_paths(tb, xd);
704 * tb_domain_disconnect_xdomain_paths() - Disable DMA paths for XDomain
705 * @tb: Domain disabling the DMA paths
706 * @xd: XDomain whose DMA paths are disconnected
708 * Calls connection manager specific method to disconnect DMA paths to
709 * the XDomain in question.
711 * Return: 0% in case of success and negative errno otherwise. In
712 * particular returns %-ENOTSUPP if the connection manager
713 * implementation does not support XDomains.
715 int tb_domain_disconnect_xdomain_paths(struct tb *tb, struct tb_xdomain *xd)
717 if (!tb->cm_ops->disconnect_xdomain_paths)
720 return tb->cm_ops->disconnect_xdomain_paths(tb, xd);
723 static int disconnect_xdomain(struct device *dev, void *data)
725 struct tb_xdomain *xd;
726 struct tb *tb = data;
729 xd = tb_to_xdomain(dev);
730 if (xd && xd->tb == tb)
731 ret = tb_xdomain_disable_paths(xd);
737 * tb_domain_disconnect_all_paths() - Disconnect all paths for the domain
738 * @tb: Domain whose paths are disconnected
740 * This function can be used to disconnect all paths (PCIe, XDomain) for
741 * example in preparation for host NVM firmware upgrade. After this is
742 * called the paths cannot be established without resetting the switch.
744 * Return: %0 in case of success and negative errno otherwise.
746 int tb_domain_disconnect_all_paths(struct tb *tb)
750 ret = tb_domain_disconnect_pcie_paths(tb);
754 return bus_for_each_dev(&tb_bus_type, NULL, tb, disconnect_xdomain);
757 int tb_domain_init(void)
761 ret = tb_xdomain_init();
764 ret = bus_register(&tb_bus_type);
771 void tb_domain_exit(void)
773 bus_unregister(&tb_bus_type);
774 ida_destroy(&tb_domain_ida);