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 int 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
);
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",
121 static ssize_t
boot_acl_show(struct device
*dev
, struct device_attribute
*attr
,
124 struct tb
*tb
= container_of(dev
, struct tb
, dev
);
129 uuids
= kcalloc(tb
->nboot_acl
, sizeof(uuid_t
), GFP_KERNEL
);
133 pm_runtime_get_sync(&tb
->dev
);
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");
156 pm_runtime_mark_last_busy(&tb
->dev
);
157 pm_runtime_put_autosuspend(&tb
->dev
);
163 static ssize_t
boot_acl_store(struct device
*dev
, struct device_attribute
*attr
,
164 const char *buf
, size_t count
)
166 struct tb
*tb
= container_of(dev
, struct tb
, dev
);
167 char *str
, *s
, *uuid_str
;
173 * Make sure the value is not bigger than tb->nboot_acl * UUID
174 * length + commas and optional "\n". Also the smallest allowable
175 * string is tb->nboot_acl * ",".
177 if (count
> (UUID_STRING_LEN
+ 1) * tb
->nboot_acl
+ 1)
179 if (count
< tb
->nboot_acl
- 1)
182 str
= kstrdup(buf
, GFP_KERNEL
);
186 acl
= kcalloc(tb
->nboot_acl
, sizeof(uuid_t
), GFP_KERNEL
);
192 uuid_str
= strim(str
);
193 while ((s
= strsep(&uuid_str
, ",")) != NULL
&& i
< tb
->nboot_acl
) {
194 size_t len
= strlen(s
);
197 if (len
!= UUID_STRING_LEN
) {
201 ret
= uuid_parse(s
, &acl
[i
]);
209 if (s
|| i
< tb
->nboot_acl
) {
214 pm_runtime_get_sync(&tb
->dev
);
216 if (mutex_lock_interruptible(&tb
->lock
)) {
220 ret
= tb
->cm_ops
->set_boot_acl(tb
, acl
, tb
->nboot_acl
);
222 /* Notify userspace about the change */
223 kobject_uevent(&tb
->dev
.kobj
, KOBJ_CHANGE
);
225 mutex_unlock(&tb
->lock
);
228 pm_runtime_mark_last_busy(&tb
->dev
);
229 pm_runtime_put_autosuspend(&tb
->dev
);
237 static DEVICE_ATTR_RW(boot_acl
);
239 static ssize_t
security_show(struct device
*dev
, struct device_attribute
*attr
,
242 struct tb
*tb
= container_of(dev
, struct tb
, dev
);
243 const char *name
= "unknown";
245 if (tb
->security_level
< ARRAY_SIZE(tb_security_names
))
246 name
= tb_security_names
[tb
->security_level
];
248 return sprintf(buf
, "%s\n", name
);
250 static DEVICE_ATTR_RO(security
);
252 static struct attribute
*domain_attrs
[] = {
253 &dev_attr_boot_acl
.attr
,
254 &dev_attr_security
.attr
,
258 static umode_t
domain_attr_is_visible(struct kobject
*kobj
,
259 struct attribute
*attr
, int n
)
261 struct device
*dev
= container_of(kobj
, struct device
, kobj
);
262 struct tb
*tb
= container_of(dev
, struct tb
, dev
);
264 if (attr
== &dev_attr_boot_acl
.attr
) {
266 tb
->cm_ops
->get_boot_acl
&&
267 tb
->cm_ops
->set_boot_acl
)
275 static struct attribute_group domain_attr_group
= {
276 .is_visible
= domain_attr_is_visible
,
277 .attrs
= domain_attrs
,
280 static const struct attribute_group
*domain_attr_groups
[] = {
285 struct bus_type tb_bus_type
= {
286 .name
= "thunderbolt",
287 .match
= tb_service_match
,
288 .probe
= tb_service_probe
,
289 .remove
= tb_service_remove
,
290 .shutdown
= tb_service_shutdown
,
293 static void tb_domain_release(struct device
*dev
)
295 struct tb
*tb
= container_of(dev
, struct tb
, dev
);
297 tb_ctl_free(tb
->ctl
);
298 destroy_workqueue(tb
->wq
);
299 ida_simple_remove(&tb_domain_ida
, tb
->index
);
300 mutex_destroy(&tb
->lock
);
304 struct device_type tb_domain_type
= {
305 .name
= "thunderbolt_domain",
306 .release
= tb_domain_release
,
310 * tb_domain_alloc() - Allocate a domain
311 * @nhi: Pointer to the host controller
312 * @privsize: Size of the connection manager private data
314 * Allocates and initializes a new Thunderbolt domain. Connection
315 * managers are expected to call this and then fill in @cm_ops
318 * Call tb_domain_put() to release the domain before it has been added
321 * Return: allocated domain structure on %NULL in case of error
323 struct tb
*tb_domain_alloc(struct tb_nhi
*nhi
, size_t privsize
)
328 * Make sure the structure sizes map with that the hardware
329 * expects because bit-fields are being used.
331 BUILD_BUG_ON(sizeof(struct tb_regs_switch_header
) != 5 * 4);
332 BUILD_BUG_ON(sizeof(struct tb_regs_port_header
) != 8 * 4);
333 BUILD_BUG_ON(sizeof(struct tb_regs_hop
) != 2 * 4);
335 tb
= kzalloc(sizeof(*tb
) + privsize
, GFP_KERNEL
);
340 mutex_init(&tb
->lock
);
342 tb
->index
= ida_simple_get(&tb_domain_ida
, 0, 0, GFP_KERNEL
);
346 tb
->wq
= alloc_ordered_workqueue("thunderbolt%d", 0, tb
->index
);
350 tb
->dev
.parent
= &nhi
->pdev
->dev
;
351 tb
->dev
.bus
= &tb_bus_type
;
352 tb
->dev
.type
= &tb_domain_type
;
353 tb
->dev
.groups
= domain_attr_groups
;
354 dev_set_name(&tb
->dev
, "domain%d", tb
->index
);
355 device_initialize(&tb
->dev
);
360 ida_simple_remove(&tb_domain_ida
, tb
->index
);
367 static bool tb_domain_event_cb(void *data
, enum tb_cfg_pkg_type type
,
368 const void *buf
, size_t size
)
370 struct tb
*tb
= data
;
372 if (!tb
->cm_ops
->handle_event
) {
373 tb_warn(tb
, "domain does not have event handler\n");
378 case TB_CFG_PKG_XDOMAIN_REQ
:
379 case TB_CFG_PKG_XDOMAIN_RESP
:
380 return tb_xdomain_handle_request(tb
, type
, buf
, size
);
383 tb
->cm_ops
->handle_event(tb
, type
, buf
, size
);
390 * tb_domain_add() - Add domain to the system
393 * Starts the domain and adds it to the system. Hotplugging devices will
394 * work after this has been returned successfully. In order to remove
395 * and release the domain after this function has been called, call
396 * tb_domain_remove().
398 * Return: %0 in case of success and negative errno in case of error
400 int tb_domain_add(struct tb
*tb
)
404 if (WARN_ON(!tb
->cm_ops
))
407 mutex_lock(&tb
->lock
);
409 tb
->ctl
= tb_ctl_alloc(tb
->nhi
, tb_domain_event_cb
, tb
);
416 * tb_schedule_hotplug_handler may be called as soon as the config
417 * channel is started. Thats why we have to hold the lock here.
419 tb_ctl_start(tb
->ctl
);
421 if (tb
->cm_ops
->driver_ready
) {
422 ret
= tb
->cm_ops
->driver_ready(tb
);
427 ret
= device_add(&tb
->dev
);
431 /* Start the domain */
432 if (tb
->cm_ops
->start
) {
433 ret
= tb
->cm_ops
->start(tb
);
438 /* This starts event processing */
439 mutex_unlock(&tb
->lock
);
441 pm_runtime_no_callbacks(&tb
->dev
);
442 pm_runtime_set_active(&tb
->dev
);
443 pm_runtime_enable(&tb
->dev
);
444 pm_runtime_set_autosuspend_delay(&tb
->dev
, TB_AUTOSUSPEND_DELAY
);
445 pm_runtime_mark_last_busy(&tb
->dev
);
446 pm_runtime_use_autosuspend(&tb
->dev
);
451 device_del(&tb
->dev
);
453 tb_ctl_stop(tb
->ctl
);
455 mutex_unlock(&tb
->lock
);
461 * tb_domain_remove() - Removes and releases a domain
462 * @tb: Domain to remove
464 * Stops the domain, removes it from the system and releases all
465 * resources once the last reference has been released.
467 void tb_domain_remove(struct tb
*tb
)
469 mutex_lock(&tb
->lock
);
470 if (tb
->cm_ops
->stop
)
471 tb
->cm_ops
->stop(tb
);
472 /* Stop the domain control traffic */
473 tb_ctl_stop(tb
->ctl
);
474 mutex_unlock(&tb
->lock
);
476 flush_workqueue(tb
->wq
);
477 device_unregister(&tb
->dev
);
481 * tb_domain_suspend_noirq() - Suspend a domain
482 * @tb: Domain to suspend
484 * Suspends all devices in the domain and stops the control channel.
486 int tb_domain_suspend_noirq(struct tb
*tb
)
491 * The control channel interrupt is left enabled during suspend
492 * and taking the lock here prevents any events happening before
493 * we actually have stopped the domain and the control channel.
495 mutex_lock(&tb
->lock
);
496 if (tb
->cm_ops
->suspend_noirq
)
497 ret
= tb
->cm_ops
->suspend_noirq(tb
);
499 tb_ctl_stop(tb
->ctl
);
500 mutex_unlock(&tb
->lock
);
506 * tb_domain_resume_noirq() - Resume a domain
507 * @tb: Domain to resume
509 * Re-starts the control channel, and resumes all devices connected to
512 int tb_domain_resume_noirq(struct tb
*tb
)
516 mutex_lock(&tb
->lock
);
517 tb_ctl_start(tb
->ctl
);
518 if (tb
->cm_ops
->resume_noirq
)
519 ret
= tb
->cm_ops
->resume_noirq(tb
);
520 mutex_unlock(&tb
->lock
);
525 int tb_domain_suspend(struct tb
*tb
)
527 return tb
->cm_ops
->suspend
? tb
->cm_ops
->suspend(tb
) : 0;
530 void tb_domain_complete(struct tb
*tb
)
532 if (tb
->cm_ops
->complete
)
533 tb
->cm_ops
->complete(tb
);
536 int tb_domain_runtime_suspend(struct tb
*tb
)
538 if (tb
->cm_ops
->runtime_suspend
) {
539 int ret
= tb
->cm_ops
->runtime_suspend(tb
);
543 tb_ctl_stop(tb
->ctl
);
547 int tb_domain_runtime_resume(struct tb
*tb
)
549 tb_ctl_start(tb
->ctl
);
550 if (tb
->cm_ops
->runtime_resume
) {
551 int ret
= tb
->cm_ops
->runtime_resume(tb
);
559 * tb_domain_approve_switch() - Approve switch
560 * @tb: Domain the switch belongs to
561 * @sw: Switch to approve
563 * This will approve switch by connection manager specific means. In
564 * case of success the connection manager will create tunnels for all
565 * supported protocols.
567 int tb_domain_approve_switch(struct tb
*tb
, struct tb_switch
*sw
)
569 struct tb_switch
*parent_sw
;
571 if (!tb
->cm_ops
->approve_switch
)
574 /* The parent switch must be authorized before this one */
575 parent_sw
= tb_to_switch(sw
->dev
.parent
);
576 if (!parent_sw
|| !parent_sw
->authorized
)
579 return tb
->cm_ops
->approve_switch(tb
, sw
);
583 * tb_domain_approve_switch_key() - Approve switch and add key
584 * @tb: Domain the switch belongs to
585 * @sw: Switch to approve
587 * For switches that support secure connect, this function first adds
588 * key to the switch NVM using connection manager specific means. If
589 * adding the key is successful, the switch is approved and connected.
591 * Return: %0 on success and negative errno in case of failure.
593 int tb_domain_approve_switch_key(struct tb
*tb
, struct tb_switch
*sw
)
595 struct tb_switch
*parent_sw
;
598 if (!tb
->cm_ops
->approve_switch
|| !tb
->cm_ops
->add_switch_key
)
601 /* The parent switch must be authorized before this one */
602 parent_sw
= tb_to_switch(sw
->dev
.parent
);
603 if (!parent_sw
|| !parent_sw
->authorized
)
606 ret
= tb
->cm_ops
->add_switch_key(tb
, sw
);
610 return tb
->cm_ops
->approve_switch(tb
, sw
);
614 * tb_domain_challenge_switch_key() - Challenge and approve switch
615 * @tb: Domain the switch belongs to
616 * @sw: Switch to approve
618 * For switches that support secure connect, this function generates
619 * random challenge and sends it to the switch. The switch responds to
620 * this and if the response matches our random challenge, the switch is
621 * approved and connected.
623 * Return: %0 on success and negative errno in case of failure.
625 int tb_domain_challenge_switch_key(struct tb
*tb
, struct tb_switch
*sw
)
627 u8 challenge
[TB_SWITCH_KEY_SIZE
];
628 u8 response
[TB_SWITCH_KEY_SIZE
];
629 u8 hmac
[TB_SWITCH_KEY_SIZE
];
630 struct tb_switch
*parent_sw
;
631 struct crypto_shash
*tfm
;
632 struct shash_desc
*shash
;
635 if (!tb
->cm_ops
->approve_switch
|| !tb
->cm_ops
->challenge_switch_key
)
638 /* The parent switch must be authorized before this one */
639 parent_sw
= tb_to_switch(sw
->dev
.parent
);
640 if (!parent_sw
|| !parent_sw
->authorized
)
643 get_random_bytes(challenge
, sizeof(challenge
));
644 ret
= tb
->cm_ops
->challenge_switch_key(tb
, sw
, challenge
, response
);
648 tfm
= crypto_alloc_shash("hmac(sha256)", 0, 0);
652 ret
= crypto_shash_setkey(tfm
, sw
->key
, TB_SWITCH_KEY_SIZE
);
656 shash
= kzalloc(sizeof(*shash
) + crypto_shash_descsize(tfm
),
664 shash
->flags
= CRYPTO_TFM_REQ_MAY_SLEEP
;
666 memset(hmac
, 0, sizeof(hmac
));
667 ret
= crypto_shash_digest(shash
, challenge
, sizeof(hmac
), hmac
);
671 /* The returned HMAC must match the one we calculated */
672 if (memcmp(response
, hmac
, sizeof(hmac
))) {
677 crypto_free_shash(tfm
);
680 return tb
->cm_ops
->approve_switch(tb
, sw
);
685 crypto_free_shash(tfm
);
691 * tb_domain_disconnect_pcie_paths() - Disconnect all PCIe paths
692 * @tb: Domain whose PCIe paths to disconnect
694 * This needs to be called in preparation for NVM upgrade of the host
695 * controller. Makes sure all PCIe paths are disconnected.
697 * Return %0 on success and negative errno in case of error.
699 int tb_domain_disconnect_pcie_paths(struct tb
*tb
)
701 if (!tb
->cm_ops
->disconnect_pcie_paths
)
704 return tb
->cm_ops
->disconnect_pcie_paths(tb
);
708 * tb_domain_approve_xdomain_paths() - Enable DMA paths for XDomain
709 * @tb: Domain enabling the DMA paths
710 * @xd: XDomain DMA paths are created to
712 * Calls connection manager specific method to enable DMA paths to the
713 * XDomain in question.
715 * Return: 0% in case of success and negative errno otherwise. In
716 * particular returns %-ENOTSUPP if the connection manager
717 * implementation does not support XDomains.
719 int tb_domain_approve_xdomain_paths(struct tb
*tb
, struct tb_xdomain
*xd
)
721 if (!tb
->cm_ops
->approve_xdomain_paths
)
724 return tb
->cm_ops
->approve_xdomain_paths(tb
, xd
);
728 * tb_domain_disconnect_xdomain_paths() - Disable DMA paths for XDomain
729 * @tb: Domain disabling the DMA paths
730 * @xd: XDomain whose DMA paths are disconnected
732 * Calls connection manager specific method to disconnect DMA paths to
733 * the XDomain in question.
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.
739 int tb_domain_disconnect_xdomain_paths(struct tb
*tb
, struct tb_xdomain
*xd
)
741 if (!tb
->cm_ops
->disconnect_xdomain_paths
)
744 return tb
->cm_ops
->disconnect_xdomain_paths(tb
, xd
);
747 static int disconnect_xdomain(struct device
*dev
, void *data
)
749 struct tb_xdomain
*xd
;
750 struct tb
*tb
= data
;
753 xd
= tb_to_xdomain(dev
);
754 if (xd
&& xd
->tb
== tb
)
755 ret
= tb_xdomain_disable_paths(xd
);
761 * tb_domain_disconnect_all_paths() - Disconnect all paths for the domain
762 * @tb: Domain whose paths are disconnected
764 * This function can be used to disconnect all paths (PCIe, XDomain) for
765 * example in preparation for host NVM firmware upgrade. After this is
766 * called the paths cannot be established without resetting the switch.
768 * Return: %0 in case of success and negative errno otherwise.
770 int tb_domain_disconnect_all_paths(struct tb
*tb
)
774 ret
= tb_domain_disconnect_pcie_paths(tb
);
778 return bus_for_each_dev(&tb_bus_type
, NULL
, tb
, disconnect_xdomain
);
781 int tb_domain_init(void)
785 ret
= tb_xdomain_init();
788 ret
= bus_register(&tb_bus_type
);
795 void tb_domain_exit(void)
797 bus_unregister(&tb_bus_type
);
798 ida_destroy(&tb_domain_ida
);