1 // SPDX-License-Identifier: GPL-2.0
3 * Thunderbolt driver - switch/port utility functions
5 * Copyright (c) 2014 Andreas Noever <andreas.noever@gmail.com>
6 * Copyright (C) 2018, Intel Corporation
9 #include <linux/delay.h>
10 #include <linux/idr.h>
11 #include <linux/nvmem-provider.h>
12 #include <linux/pm_runtime.h>
13 #include <linux/sizes.h>
14 #include <linux/slab.h>
15 #include <linux/vmalloc.h>
19 /* Switch authorization from userspace is serialized by this lock */
20 static DEFINE_MUTEX(switch_lock
);
22 /* Switch NVM support */
24 #define NVM_DEVID 0x05
25 #define NVM_VERSION 0x08
27 #define NVM_FLASH_SIZE 0x45
29 #define NVM_MIN_SIZE SZ_32K
30 #define NVM_MAX_SIZE SZ_512K
32 static DEFINE_IDA(nvm_ida
);
34 struct nvm_auth_status
{
35 struct list_head list
;
41 * Hold NVM authentication failure status per switch This information
42 * needs to stay around even when the switch gets power cycled so we
45 static LIST_HEAD(nvm_auth_status_cache
);
46 static DEFINE_MUTEX(nvm_auth_status_lock
);
48 static struct nvm_auth_status
*__nvm_get_auth_status(const struct tb_switch
*sw
)
50 struct nvm_auth_status
*st
;
52 list_for_each_entry(st
, &nvm_auth_status_cache
, list
) {
53 if (uuid_equal(&st
->uuid
, sw
->uuid
))
60 static void nvm_get_auth_status(const struct tb_switch
*sw
, u32
*status
)
62 struct nvm_auth_status
*st
;
64 mutex_lock(&nvm_auth_status_lock
);
65 st
= __nvm_get_auth_status(sw
);
66 mutex_unlock(&nvm_auth_status_lock
);
68 *status
= st
? st
->status
: 0;
71 static void nvm_set_auth_status(const struct tb_switch
*sw
, u32 status
)
73 struct nvm_auth_status
*st
;
75 if (WARN_ON(!sw
->uuid
))
78 mutex_lock(&nvm_auth_status_lock
);
79 st
= __nvm_get_auth_status(sw
);
82 st
= kzalloc(sizeof(*st
), GFP_KERNEL
);
86 memcpy(&st
->uuid
, sw
->uuid
, sizeof(st
->uuid
));
87 INIT_LIST_HEAD(&st
->list
);
88 list_add_tail(&st
->list
, &nvm_auth_status_cache
);
93 mutex_unlock(&nvm_auth_status_lock
);
96 static void nvm_clear_auth_status(const struct tb_switch
*sw
)
98 struct nvm_auth_status
*st
;
100 mutex_lock(&nvm_auth_status_lock
);
101 st
= __nvm_get_auth_status(sw
);
106 mutex_unlock(&nvm_auth_status_lock
);
109 static int nvm_validate_and_write(struct tb_switch
*sw
)
111 unsigned int image_size
, hdr_size
;
112 const u8
*buf
= sw
->nvm
->buf
;
119 image_size
= sw
->nvm
->buf_data_size
;
120 if (image_size
< NVM_MIN_SIZE
|| image_size
> NVM_MAX_SIZE
)
124 * FARB pointer must point inside the image and must at least
125 * contain parts of the digital section we will be reading here.
127 hdr_size
= (*(u32
*)buf
) & 0xffffff;
128 if (hdr_size
+ NVM_DEVID
+ 2 >= image_size
)
131 /* Digital section start should be aligned to 4k page */
132 if (!IS_ALIGNED(hdr_size
, SZ_4K
))
136 * Read digital section size and check that it also fits inside
139 ds_size
= *(u16
*)(buf
+ hdr_size
);
140 if (ds_size
>= image_size
)
143 if (!sw
->safe_mode
) {
147 * Make sure the device ID in the image matches the one
148 * we read from the switch config space.
150 device_id
= *(u16
*)(buf
+ hdr_size
+ NVM_DEVID
);
151 if (device_id
!= sw
->config
.device_id
)
154 if (sw
->generation
< 3) {
155 /* Write CSS headers first */
156 ret
= dma_port_flash_write(sw
->dma_port
,
157 DMA_PORT_CSS_ADDRESS
, buf
+ NVM_CSS
,
158 DMA_PORT_CSS_MAX_SIZE
);
163 /* Skip headers in the image */
165 image_size
-= hdr_size
;
168 return dma_port_flash_write(sw
->dma_port
, 0, buf
, image_size
);
171 static int nvm_authenticate_host(struct tb_switch
*sw
)
176 * Root switch NVM upgrade requires that we disconnect the
177 * existing paths first (in case it is not in safe mode
180 if (!sw
->safe_mode
) {
181 ret
= tb_domain_disconnect_all_paths(sw
->tb
);
185 * The host controller goes away pretty soon after this if
186 * everything goes well so getting timeout is expected.
188 ret
= dma_port_flash_update_auth(sw
->dma_port
);
189 return ret
== -ETIMEDOUT
? 0 : ret
;
193 * From safe mode we can get out by just power cycling the
196 dma_port_power_cycle(sw
->dma_port
);
200 static int nvm_authenticate_device(struct tb_switch
*sw
)
202 int ret
, retries
= 10;
204 ret
= dma_port_flash_update_auth(sw
->dma_port
);
205 if (ret
&& ret
!= -ETIMEDOUT
)
209 * Poll here for the authentication status. It takes some time
210 * for the device to respond (we get timeout for a while). Once
211 * we get response the device needs to be power cycled in order
212 * to the new NVM to be taken into use.
217 ret
= dma_port_flash_update_auth_status(sw
->dma_port
, &status
);
218 if (ret
< 0 && ret
!= -ETIMEDOUT
)
222 tb_sw_warn(sw
, "failed to authenticate NVM\n");
223 nvm_set_auth_status(sw
, status
);
226 tb_sw_info(sw
, "power cycling the switch now\n");
227 dma_port_power_cycle(sw
->dma_port
);
237 static int tb_switch_nvm_read(void *priv
, unsigned int offset
, void *val
,
240 struct tb_switch
*sw
= priv
;
243 pm_runtime_get_sync(&sw
->dev
);
244 ret
= dma_port_flash_read(sw
->dma_port
, offset
, val
, bytes
);
245 pm_runtime_mark_last_busy(&sw
->dev
);
246 pm_runtime_put_autosuspend(&sw
->dev
);
251 static int tb_switch_nvm_write(void *priv
, unsigned int offset
, void *val
,
254 struct tb_switch
*sw
= priv
;
257 if (mutex_lock_interruptible(&switch_lock
))
261 * Since writing the NVM image might require some special steps,
262 * for example when CSS headers are written, we cache the image
263 * locally here and handle the special cases when the user asks
264 * us to authenticate the image.
267 sw
->nvm
->buf
= vmalloc(NVM_MAX_SIZE
);
274 sw
->nvm
->buf_data_size
= offset
+ bytes
;
275 memcpy(sw
->nvm
->buf
+ offset
, val
, bytes
);
278 mutex_unlock(&switch_lock
);
283 static struct nvmem_device
*register_nvmem(struct tb_switch
*sw
, int id
,
284 size_t size
, bool active
)
286 struct nvmem_config config
;
288 memset(&config
, 0, sizeof(config
));
291 config
.name
= "nvm_active";
292 config
.reg_read
= tb_switch_nvm_read
;
293 config
.read_only
= true;
295 config
.name
= "nvm_non_active";
296 config
.reg_write
= tb_switch_nvm_write
;
297 config
.root_only
= true;
302 config
.word_size
= 4;
304 config
.dev
= &sw
->dev
;
305 config
.owner
= THIS_MODULE
;
308 return nvmem_register(&config
);
311 static int tb_switch_nvm_add(struct tb_switch
*sw
)
313 struct nvmem_device
*nvm_dev
;
314 struct tb_switch_nvm
*nvm
;
321 nvm
= kzalloc(sizeof(*nvm
), GFP_KERNEL
);
325 nvm
->id
= ida_simple_get(&nvm_ida
, 0, 0, GFP_KERNEL
);
328 * If the switch is in safe-mode the only accessible portion of
329 * the NVM is the non-active one where userspace is expected to
330 * write new functional NVM.
332 if (!sw
->safe_mode
) {
333 u32 nvm_size
, hdr_size
;
335 ret
= dma_port_flash_read(sw
->dma_port
, NVM_FLASH_SIZE
, &val
,
340 hdr_size
= sw
->generation
< 3 ? SZ_8K
: SZ_16K
;
341 nvm_size
= (SZ_1M
<< (val
& 7)) / 8;
342 nvm_size
= (nvm_size
- hdr_size
) / 2;
344 ret
= dma_port_flash_read(sw
->dma_port
, NVM_VERSION
, &val
,
349 nvm
->major
= val
>> 16;
350 nvm
->minor
= val
>> 8;
352 nvm_dev
= register_nvmem(sw
, nvm
->id
, nvm_size
, true);
353 if (IS_ERR(nvm_dev
)) {
354 ret
= PTR_ERR(nvm_dev
);
357 nvm
->active
= nvm_dev
;
360 nvm_dev
= register_nvmem(sw
, nvm
->id
, NVM_MAX_SIZE
, false);
361 if (IS_ERR(nvm_dev
)) {
362 ret
= PTR_ERR(nvm_dev
);
365 nvm
->non_active
= nvm_dev
;
367 mutex_lock(&switch_lock
);
369 mutex_unlock(&switch_lock
);
375 nvmem_unregister(nvm
->active
);
377 ida_simple_remove(&nvm_ida
, nvm
->id
);
383 static void tb_switch_nvm_remove(struct tb_switch
*sw
)
385 struct tb_switch_nvm
*nvm
;
387 mutex_lock(&switch_lock
);
390 mutex_unlock(&switch_lock
);
395 /* Remove authentication status in case the switch is unplugged */
396 if (!nvm
->authenticating
)
397 nvm_clear_auth_status(sw
);
399 nvmem_unregister(nvm
->non_active
);
401 nvmem_unregister(nvm
->active
);
402 ida_simple_remove(&nvm_ida
, nvm
->id
);
407 /* port utility functions */
409 static const char *tb_port_type(struct tb_regs_port_header
*port
)
411 switch (port
->type
>> 16) {
413 switch ((u8
) port
->type
) {
438 static void tb_dump_port(struct tb
*tb
, struct tb_regs_port_header
*port
)
441 " Port %d: %x:%x (Revision: %d, TB Version: %d, Type: %s (%#x))\n",
442 port
->port_number
, port
->vendor_id
, port
->device_id
,
443 port
->revision
, port
->thunderbolt_version
, tb_port_type(port
),
445 tb_dbg(tb
, " Max hop id (in/out): %d/%d\n",
446 port
->max_in_hop_id
, port
->max_out_hop_id
);
447 tb_dbg(tb
, " Max counters: %d\n", port
->max_counters
);
448 tb_dbg(tb
, " NFC Credits: %#x\n", port
->nfc_credits
);
452 * tb_port_state() - get connectedness state of a port
454 * The port must have a TB_CAP_PHY (i.e. it should be a real port).
456 * Return: Returns an enum tb_port_state on success or an error code on failure.
458 static int tb_port_state(struct tb_port
*port
)
460 struct tb_cap_phy phy
;
462 if (port
->cap_phy
== 0) {
463 tb_port_WARN(port
, "does not have a PHY\n");
466 res
= tb_port_read(port
, &phy
, TB_CFG_PORT
, port
->cap_phy
, 2);
473 * tb_wait_for_port() - wait for a port to become ready
475 * Wait up to 1 second for a port to reach state TB_PORT_UP. If
476 * wait_if_unplugged is set then we also wait if the port is in state
477 * TB_PORT_UNPLUGGED (it takes a while for the device to be registered after
478 * switch resume). Otherwise we only wait if a device is registered but the link
479 * has not yet been established.
481 * Return: Returns an error code on failure. Returns 0 if the port is not
482 * connected or failed to reach state TB_PORT_UP within one second. Returns 1
483 * if the port is connected and in state TB_PORT_UP.
485 int tb_wait_for_port(struct tb_port
*port
, bool wait_if_unplugged
)
489 if (!port
->cap_phy
) {
490 tb_port_WARN(port
, "does not have PHY\n");
493 if (tb_is_upstream_port(port
)) {
494 tb_port_WARN(port
, "is the upstream port\n");
499 state
= tb_port_state(port
);
502 if (state
== TB_PORT_DISABLED
) {
503 tb_port_info(port
, "is disabled (state: 0)\n");
506 if (state
== TB_PORT_UNPLUGGED
) {
507 if (wait_if_unplugged
) {
508 /* used during resume */
510 "is unplugged (state: 7), retrying...\n");
514 tb_port_info(port
, "is unplugged (state: 7)\n");
517 if (state
== TB_PORT_UP
) {
519 "is connected, link is up (state: 2)\n");
524 * After plug-in the state is TB_PORT_CONNECTING. Give it some
528 "is connected, link is not up (state: %d), retrying...\n",
533 "failed to reach state TB_PORT_UP. Ignoring port...\n");
538 * tb_port_add_nfc_credits() - add/remove non flow controlled credits to port
540 * Change the number of NFC credits allocated to @port by @credits. To remove
541 * NFC credits pass a negative amount of credits.
543 * Return: Returns 0 on success or an error code on failure.
545 int tb_port_add_nfc_credits(struct tb_port
*port
, int credits
)
550 "adding %#x NFC credits (%#x -> %#x)",
552 port
->config
.nfc_credits
,
553 port
->config
.nfc_credits
+ credits
);
554 port
->config
.nfc_credits
+= credits
;
555 return tb_port_write(port
, &port
->config
.nfc_credits
,
560 * tb_port_clear_counter() - clear a counter in TB_CFG_COUNTER
562 * Return: Returns 0 on success or an error code on failure.
564 int tb_port_clear_counter(struct tb_port
*port
, int counter
)
566 u32 zero
[3] = { 0, 0, 0 };
567 tb_port_info(port
, "clearing counter %d\n", counter
);
568 return tb_port_write(port
, zero
, TB_CFG_COUNTERS
, 3 * counter
, 3);
572 * tb_init_port() - initialize a port
574 * This is a helper method for tb_switch_alloc. Does not check or initialize
575 * any downstream switches.
577 * Return: Returns 0 on success or an error code on failure.
579 static int tb_init_port(struct tb_port
*port
)
584 res
= tb_port_read(port
, &port
->config
, TB_CFG_PORT
, 0, 8);
588 /* Port 0 is the switch itself and has no PHY. */
589 if (port
->config
.type
== TB_TYPE_PORT
&& port
->port
!= 0) {
590 cap
= tb_port_find_cap(port
, TB_PORT_CAP_PHY
);
595 tb_port_WARN(port
, "non switch port without a PHY\n");
598 tb_dump_port(port
->sw
->tb
, &port
->config
);
600 /* TODO: Read dual link port, DP port and more from EEPROM. */
605 /* switch utility functions */
607 static void tb_dump_switch(struct tb
*tb
, struct tb_regs_switch_header
*sw
)
609 tb_dbg(tb
, " Switch: %x:%x (Revision: %d, TB Version: %d)\n",
610 sw
->vendor_id
, sw
->device_id
, sw
->revision
,
611 sw
->thunderbolt_version
);
612 tb_dbg(tb
, " Max Port Number: %d\n", sw
->max_port_number
);
613 tb_dbg(tb
, " Config:\n");
615 " Upstream Port Number: %d Depth: %d Route String: %#llx Enabled: %d, PlugEventsDelay: %dms\n",
616 sw
->upstream_port_number
, sw
->depth
,
617 (((u64
) sw
->route_hi
) << 32) | sw
->route_lo
,
618 sw
->enabled
, sw
->plug_events_delay
);
619 tb_dbg(tb
, " unknown1: %#x unknown4: %#x\n",
620 sw
->__unknown1
, sw
->__unknown4
);
624 * reset_switch() - reconfigure route, enable and send TB_CFG_PKG_RESET
626 * Return: Returns 0 on success or an error code on failure.
628 int tb_switch_reset(struct tb
*tb
, u64 route
)
630 struct tb_cfg_result res
;
631 struct tb_regs_switch_header header
= {
632 header
.route_hi
= route
>> 32,
633 header
.route_lo
= route
,
634 header
.enabled
= true,
636 tb_dbg(tb
, "resetting switch at %llx\n", route
);
637 res
.err
= tb_cfg_write(tb
->ctl
, ((u32
*) &header
) + 2, route
,
641 res
= tb_cfg_reset(tb
->ctl
, route
, TB_CFG_DEFAULT_TIMEOUT
);
647 struct tb_switch
*get_switch_at_route(struct tb_switch
*sw
, u64 route
)
649 u8 next_port
= route
; /*
650 * Routes use a stride of 8 bits,
651 * eventhough a port index has 6 bits at most.
655 if (next_port
> sw
->config
.max_port_number
)
657 if (tb_is_upstream_port(&sw
->ports
[next_port
]))
659 if (!sw
->ports
[next_port
].remote
)
661 return get_switch_at_route(sw
->ports
[next_port
].remote
->sw
,
662 route
>> TB_ROUTE_SHIFT
);
666 * tb_plug_events_active() - enable/disable plug events on a switch
668 * Also configures a sane plug_events_delay of 255ms.
670 * Return: Returns 0 on success or an error code on failure.
672 static int tb_plug_events_active(struct tb_switch
*sw
, bool active
)
677 if (!sw
->config
.enabled
)
680 sw
->config
.plug_events_delay
= 0xff;
681 res
= tb_sw_write(sw
, ((u32
*) &sw
->config
) + 4, TB_CFG_SWITCH
, 4, 1);
685 res
= tb_sw_read(sw
, &data
, TB_CFG_SWITCH
, sw
->cap_plug_events
+ 1, 1);
690 data
= data
& 0xFFFFFF83;
691 switch (sw
->config
.device_id
) {
692 case PCI_DEVICE_ID_INTEL_LIGHT_RIDGE
:
693 case PCI_DEVICE_ID_INTEL_EAGLE_RIDGE
:
694 case PCI_DEVICE_ID_INTEL_PORT_RIDGE
:
702 return tb_sw_write(sw
, &data
, TB_CFG_SWITCH
,
703 sw
->cap_plug_events
+ 1, 1);
706 static ssize_t
authorized_show(struct device
*dev
,
707 struct device_attribute
*attr
,
710 struct tb_switch
*sw
= tb_to_switch(dev
);
712 return sprintf(buf
, "%u\n", sw
->authorized
);
715 static int tb_switch_set_authorized(struct tb_switch
*sw
, unsigned int val
)
719 if (mutex_lock_interruptible(&switch_lock
))
726 * Make sure there is no PCIe rescan ongoing when a new PCIe
727 * tunnel is created. Otherwise the PCIe rescan code might find
728 * the new tunnel too early.
730 pci_lock_rescan_remove();
731 pm_runtime_get_sync(&sw
->dev
);
737 ret
= tb_domain_approve_switch_key(sw
->tb
, sw
);
739 ret
= tb_domain_approve_switch(sw
->tb
, sw
);
742 /* Challenge switch */
745 ret
= tb_domain_challenge_switch_key(sw
->tb
, sw
);
752 pm_runtime_mark_last_busy(&sw
->dev
);
753 pm_runtime_put_autosuspend(&sw
->dev
);
754 pci_unlock_rescan_remove();
757 sw
->authorized
= val
;
758 /* Notify status change to the userspace */
759 kobject_uevent(&sw
->dev
.kobj
, KOBJ_CHANGE
);
763 mutex_unlock(&switch_lock
);
767 static ssize_t
authorized_store(struct device
*dev
,
768 struct device_attribute
*attr
,
769 const char *buf
, size_t count
)
771 struct tb_switch
*sw
= tb_to_switch(dev
);
775 ret
= kstrtouint(buf
, 0, &val
);
781 ret
= tb_switch_set_authorized(sw
, val
);
783 return ret
? ret
: count
;
785 static DEVICE_ATTR_RW(authorized
);
787 static ssize_t
boot_show(struct device
*dev
, struct device_attribute
*attr
,
790 struct tb_switch
*sw
= tb_to_switch(dev
);
792 return sprintf(buf
, "%u\n", sw
->boot
);
794 static DEVICE_ATTR_RO(boot
);
796 static ssize_t
device_show(struct device
*dev
, struct device_attribute
*attr
,
799 struct tb_switch
*sw
= tb_to_switch(dev
);
801 return sprintf(buf
, "%#x\n", sw
->device
);
803 static DEVICE_ATTR_RO(device
);
806 device_name_show(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
808 struct tb_switch
*sw
= tb_to_switch(dev
);
810 return sprintf(buf
, "%s\n", sw
->device_name
? sw
->device_name
: "");
812 static DEVICE_ATTR_RO(device_name
);
814 static ssize_t
key_show(struct device
*dev
, struct device_attribute
*attr
,
817 struct tb_switch
*sw
= tb_to_switch(dev
);
820 if (mutex_lock_interruptible(&switch_lock
))
824 ret
= sprintf(buf
, "%*phN\n", TB_SWITCH_KEY_SIZE
, sw
->key
);
826 ret
= sprintf(buf
, "\n");
828 mutex_unlock(&switch_lock
);
832 static ssize_t
key_store(struct device
*dev
, struct device_attribute
*attr
,
833 const char *buf
, size_t count
)
835 struct tb_switch
*sw
= tb_to_switch(dev
);
836 u8 key
[TB_SWITCH_KEY_SIZE
];
840 if (!strcmp(buf
, "\n"))
842 else if (hex2bin(key
, buf
, sizeof(key
)))
845 if (mutex_lock_interruptible(&switch_lock
))
848 if (sw
->authorized
) {
855 sw
->key
= kmemdup(key
, sizeof(key
), GFP_KERNEL
);
861 mutex_unlock(&switch_lock
);
864 static DEVICE_ATTR(key
, 0600, key_show
, key_store
);
866 static void nvm_authenticate_start(struct tb_switch
*sw
)
868 struct pci_dev
*root_port
;
871 * During host router NVM upgrade we should not allow root port to
872 * go into D3cold because some root ports cannot trigger PME
873 * itself. To be on the safe side keep the root port in D0 during
874 * the whole upgrade process.
876 root_port
= pci_find_pcie_root_port(sw
->tb
->nhi
->pdev
);
878 pm_runtime_get_noresume(&root_port
->dev
);
881 static void nvm_authenticate_complete(struct tb_switch
*sw
)
883 struct pci_dev
*root_port
;
885 root_port
= pci_find_pcie_root_port(sw
->tb
->nhi
->pdev
);
887 pm_runtime_put(&root_port
->dev
);
890 static ssize_t
nvm_authenticate_show(struct device
*dev
,
891 struct device_attribute
*attr
, char *buf
)
893 struct tb_switch
*sw
= tb_to_switch(dev
);
896 nvm_get_auth_status(sw
, &status
);
897 return sprintf(buf
, "%#x\n", status
);
900 static ssize_t
nvm_authenticate_store(struct device
*dev
,
901 struct device_attribute
*attr
, const char *buf
, size_t count
)
903 struct tb_switch
*sw
= tb_to_switch(dev
);
907 if (mutex_lock_interruptible(&switch_lock
))
910 /* If NVMem devices are not yet added */
916 ret
= kstrtobool(buf
, &val
);
920 /* Always clear the authentication status */
921 nvm_clear_auth_status(sw
);
929 pm_runtime_get_sync(&sw
->dev
);
930 ret
= nvm_validate_and_write(sw
);
932 pm_runtime_mark_last_busy(&sw
->dev
);
933 pm_runtime_put_autosuspend(&sw
->dev
);
937 sw
->nvm
->authenticating
= true;
941 * Keep root port from suspending as long as the
942 * NVM upgrade process is running.
944 nvm_authenticate_start(sw
);
945 ret
= nvm_authenticate_host(sw
);
947 nvm_authenticate_complete(sw
);
949 ret
= nvm_authenticate_device(sw
);
951 pm_runtime_mark_last_busy(&sw
->dev
);
952 pm_runtime_put_autosuspend(&sw
->dev
);
956 mutex_unlock(&switch_lock
);
962 static DEVICE_ATTR_RW(nvm_authenticate
);
964 static ssize_t
nvm_version_show(struct device
*dev
,
965 struct device_attribute
*attr
, char *buf
)
967 struct tb_switch
*sw
= tb_to_switch(dev
);
970 if (mutex_lock_interruptible(&switch_lock
))
978 ret
= sprintf(buf
, "%x.%x\n", sw
->nvm
->major
, sw
->nvm
->minor
);
980 mutex_unlock(&switch_lock
);
984 static DEVICE_ATTR_RO(nvm_version
);
986 static ssize_t
vendor_show(struct device
*dev
, struct device_attribute
*attr
,
989 struct tb_switch
*sw
= tb_to_switch(dev
);
991 return sprintf(buf
, "%#x\n", sw
->vendor
);
993 static DEVICE_ATTR_RO(vendor
);
996 vendor_name_show(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
998 struct tb_switch
*sw
= tb_to_switch(dev
);
1000 return sprintf(buf
, "%s\n", sw
->vendor_name
? sw
->vendor_name
: "");
1002 static DEVICE_ATTR_RO(vendor_name
);
1004 static ssize_t
unique_id_show(struct device
*dev
, struct device_attribute
*attr
,
1007 struct tb_switch
*sw
= tb_to_switch(dev
);
1009 return sprintf(buf
, "%pUb\n", sw
->uuid
);
1011 static DEVICE_ATTR_RO(unique_id
);
1013 static struct attribute
*switch_attrs
[] = {
1014 &dev_attr_authorized
.attr
,
1015 &dev_attr_boot
.attr
,
1016 &dev_attr_device
.attr
,
1017 &dev_attr_device_name
.attr
,
1019 &dev_attr_nvm_authenticate
.attr
,
1020 &dev_attr_nvm_version
.attr
,
1021 &dev_attr_vendor
.attr
,
1022 &dev_attr_vendor_name
.attr
,
1023 &dev_attr_unique_id
.attr
,
1027 static umode_t
switch_attr_is_visible(struct kobject
*kobj
,
1028 struct attribute
*attr
, int n
)
1030 struct device
*dev
= container_of(kobj
, struct device
, kobj
);
1031 struct tb_switch
*sw
= tb_to_switch(dev
);
1033 if (attr
== &dev_attr_key
.attr
) {
1035 sw
->tb
->security_level
== TB_SECURITY_SECURE
&&
1036 sw
->security_level
== TB_SECURITY_SECURE
)
1039 } else if (attr
== &dev_attr_nvm_authenticate
.attr
||
1040 attr
== &dev_attr_nvm_version
.attr
) {
1044 } else if (attr
== &dev_attr_boot
.attr
) {
1050 return sw
->safe_mode
? 0 : attr
->mode
;
1053 static struct attribute_group switch_group
= {
1054 .is_visible
= switch_attr_is_visible
,
1055 .attrs
= switch_attrs
,
1058 static const struct attribute_group
*switch_groups
[] = {
1063 static void tb_switch_release(struct device
*dev
)
1065 struct tb_switch
*sw
= tb_to_switch(dev
);
1067 dma_port_free(sw
->dma_port
);
1070 kfree(sw
->device_name
);
1071 kfree(sw
->vendor_name
);
1079 * Currently only need to provide the callbacks. Everything else is handled
1080 * in the connection manager.
1082 static int __maybe_unused
tb_switch_runtime_suspend(struct device
*dev
)
1087 static int __maybe_unused
tb_switch_runtime_resume(struct device
*dev
)
1092 static const struct dev_pm_ops tb_switch_pm_ops
= {
1093 SET_RUNTIME_PM_OPS(tb_switch_runtime_suspend
, tb_switch_runtime_resume
,
1097 struct device_type tb_switch_type
= {
1098 .name
= "thunderbolt_device",
1099 .release
= tb_switch_release
,
1100 .pm
= &tb_switch_pm_ops
,
1103 static int tb_switch_get_generation(struct tb_switch
*sw
)
1105 switch (sw
->config
.device_id
) {
1106 case PCI_DEVICE_ID_INTEL_LIGHT_RIDGE
:
1107 case PCI_DEVICE_ID_INTEL_EAGLE_RIDGE
:
1108 case PCI_DEVICE_ID_INTEL_LIGHT_PEAK
:
1109 case PCI_DEVICE_ID_INTEL_CACTUS_RIDGE_2C
:
1110 case PCI_DEVICE_ID_INTEL_CACTUS_RIDGE_4C
:
1111 case PCI_DEVICE_ID_INTEL_PORT_RIDGE
:
1112 case PCI_DEVICE_ID_INTEL_REDWOOD_RIDGE_2C_BRIDGE
:
1113 case PCI_DEVICE_ID_INTEL_REDWOOD_RIDGE_4C_BRIDGE
:
1116 case PCI_DEVICE_ID_INTEL_WIN_RIDGE_2C_BRIDGE
:
1117 case PCI_DEVICE_ID_INTEL_FALCON_RIDGE_2C_BRIDGE
:
1118 case PCI_DEVICE_ID_INTEL_FALCON_RIDGE_4C_BRIDGE
:
1121 case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_LP_BRIDGE
:
1122 case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_2C_BRIDGE
:
1123 case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_4C_BRIDGE
:
1124 case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_C_2C_BRIDGE
:
1125 case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_C_4C_BRIDGE
:
1126 case PCI_DEVICE_ID_INTEL_TITAN_RIDGE_2C_BRIDGE
:
1127 case PCI_DEVICE_ID_INTEL_TITAN_RIDGE_4C_BRIDGE
:
1128 case PCI_DEVICE_ID_INTEL_TITAN_RIDGE_DD_BRIDGE
:
1133 * For unknown switches assume generation to be 1 to be
1136 tb_sw_warn(sw
, "unsupported switch device id %#x\n",
1137 sw
->config
.device_id
);
1143 * tb_switch_alloc() - allocate a switch
1144 * @tb: Pointer to the owning domain
1145 * @parent: Parent device for this switch
1146 * @route: Route string for this switch
1148 * Allocates and initializes a switch. Will not upload configuration to
1149 * the switch. For that you need to call tb_switch_configure()
1150 * separately. The returned switch should be released by calling
1153 * Return: Pointer to the allocated switch or %NULL in case of failure
1155 struct tb_switch
*tb_switch_alloc(struct tb
*tb
, struct device
*parent
,
1160 struct tb_switch
*sw
;
1161 int upstream_port
= tb_cfg_get_upstream_port(tb
->ctl
, route
);
1162 if (upstream_port
< 0)
1165 sw
= kzalloc(sizeof(*sw
), GFP_KERNEL
);
1170 if (tb_cfg_read(tb
->ctl
, &sw
->config
, route
, 0, TB_CFG_SWITCH
, 0, 5))
1171 goto err_free_sw_ports
;
1173 tb_dbg(tb
, "current switch config:\n");
1174 tb_dump_switch(tb
, &sw
->config
);
1176 /* configure switch */
1177 sw
->config
.upstream_port_number
= upstream_port
;
1178 sw
->config
.depth
= tb_route_length(route
);
1179 sw
->config
.route_lo
= route
;
1180 sw
->config
.route_hi
= route
>> 32;
1181 sw
->config
.enabled
= 0;
1183 /* initialize ports */
1184 sw
->ports
= kcalloc(sw
->config
.max_port_number
+ 1, sizeof(*sw
->ports
),
1187 goto err_free_sw_ports
;
1189 for (i
= 0; i
<= sw
->config
.max_port_number
; i
++) {
1190 /* minimum setup for tb_find_cap and tb_drom_read to work */
1191 sw
->ports
[i
].sw
= sw
;
1192 sw
->ports
[i
].port
= i
;
1195 sw
->generation
= tb_switch_get_generation(sw
);
1197 cap
= tb_switch_find_vse_cap(sw
, TB_VSE_CAP_PLUG_EVENTS
);
1199 tb_sw_warn(sw
, "cannot find TB_VSE_CAP_PLUG_EVENTS aborting\n");
1200 goto err_free_sw_ports
;
1202 sw
->cap_plug_events
= cap
;
1204 /* Root switch is always authorized */
1206 sw
->authorized
= true;
1208 device_initialize(&sw
->dev
);
1209 sw
->dev
.parent
= parent
;
1210 sw
->dev
.bus
= &tb_bus_type
;
1211 sw
->dev
.type
= &tb_switch_type
;
1212 sw
->dev
.groups
= switch_groups
;
1213 dev_set_name(&sw
->dev
, "%u-%llx", tb
->index
, tb_route(sw
));
1225 * tb_switch_alloc_safe_mode() - allocate a switch that is in safe mode
1226 * @tb: Pointer to the owning domain
1227 * @parent: Parent device for this switch
1228 * @route: Route string for this switch
1230 * This creates a switch in safe mode. This means the switch pretty much
1231 * lacks all capabilities except DMA configuration port before it is
1232 * flashed with a valid NVM firmware.
1234 * The returned switch must be released by calling tb_switch_put().
1236 * Return: Pointer to the allocated switch or %NULL in case of failure
1239 tb_switch_alloc_safe_mode(struct tb
*tb
, struct device
*parent
, u64 route
)
1241 struct tb_switch
*sw
;
1243 sw
= kzalloc(sizeof(*sw
), GFP_KERNEL
);
1248 sw
->config
.depth
= tb_route_length(route
);
1249 sw
->config
.route_hi
= upper_32_bits(route
);
1250 sw
->config
.route_lo
= lower_32_bits(route
);
1251 sw
->safe_mode
= true;
1253 device_initialize(&sw
->dev
);
1254 sw
->dev
.parent
= parent
;
1255 sw
->dev
.bus
= &tb_bus_type
;
1256 sw
->dev
.type
= &tb_switch_type
;
1257 sw
->dev
.groups
= switch_groups
;
1258 dev_set_name(&sw
->dev
, "%u-%llx", tb
->index
, tb_route(sw
));
1264 * tb_switch_configure() - Uploads configuration to the switch
1265 * @sw: Switch to configure
1267 * Call this function before the switch is added to the system. It will
1268 * upload configuration to the switch and makes it available for the
1269 * connection manager to use.
1271 * Return: %0 in case of success and negative errno in case of failure
1273 int tb_switch_configure(struct tb_switch
*sw
)
1275 struct tb
*tb
= sw
->tb
;
1279 route
= tb_route(sw
);
1280 tb_dbg(tb
, "initializing Switch at %#llx (depth: %d, up port: %d)\n",
1281 route
, tb_route_length(route
), sw
->config
.upstream_port_number
);
1283 if (sw
->config
.vendor_id
!= PCI_VENDOR_ID_INTEL
)
1284 tb_sw_warn(sw
, "unknown switch vendor id %#x\n",
1285 sw
->config
.vendor_id
);
1287 sw
->config
.enabled
= 1;
1289 /* upload configuration */
1290 ret
= tb_sw_write(sw
, 1 + (u32
*)&sw
->config
, TB_CFG_SWITCH
, 1, 3);
1294 return tb_plug_events_active(sw
, true);
1297 static void tb_switch_set_uuid(struct tb_switch
*sw
)
1306 * The newer controllers include fused UUID as part of link
1307 * controller specific registers
1309 cap
= tb_switch_find_vse_cap(sw
, TB_VSE_CAP_LINK_CONTROLLER
);
1311 tb_sw_read(sw
, uuid
, TB_CFG_SWITCH
, cap
+ 3, 4);
1314 * ICM generates UUID based on UID and fills the upper
1315 * two words with ones. This is not strictly following
1316 * UUID format but we want to be compatible with it so
1317 * we do the same here.
1319 uuid
[0] = sw
->uid
& 0xffffffff;
1320 uuid
[1] = (sw
->uid
>> 32) & 0xffffffff;
1321 uuid
[2] = 0xffffffff;
1322 uuid
[3] = 0xffffffff;
1325 sw
->uuid
= kmemdup(uuid
, sizeof(uuid
), GFP_KERNEL
);
1328 static int tb_switch_add_dma_port(struct tb_switch
*sw
)
1333 switch (sw
->generation
) {
1338 /* Only root switch can be upgraded */
1345 * DMA port is the only thing available when the switch
1353 if (sw
->no_nvm_upgrade
)
1356 sw
->dma_port
= dma_port_alloc(sw
);
1361 * Check status of the previous flash authentication. If there
1362 * is one we need to power cycle the switch in any case to make
1363 * it functional again.
1365 ret
= dma_port_flash_update_auth_status(sw
->dma_port
, &status
);
1369 /* Now we can allow root port to suspend again */
1371 nvm_authenticate_complete(sw
);
1374 tb_sw_info(sw
, "switch flash authentication failed\n");
1375 tb_switch_set_uuid(sw
);
1376 nvm_set_auth_status(sw
, status
);
1379 tb_sw_info(sw
, "power cycling the switch now\n");
1380 dma_port_power_cycle(sw
->dma_port
);
1383 * We return error here which causes the switch adding failure.
1384 * It should appear back after power cycle is complete.
1390 * tb_switch_add() - Add a switch to the domain
1391 * @sw: Switch to add
1393 * This is the last step in adding switch to the domain. It will read
1394 * identification information from DROM and initializes ports so that
1395 * they can be used to connect other switches. The switch will be
1396 * exposed to the userspace when this function successfully returns. To
1397 * remove and release the switch, call tb_switch_remove().
1399 * Return: %0 in case of success and negative errno in case of failure
1401 int tb_switch_add(struct tb_switch
*sw
)
1406 * Initialize DMA control port now before we read DROM. Recent
1407 * host controllers have more complete DROM on NVM that includes
1408 * vendor and model identification strings which we then expose
1409 * to the userspace. NVM can be accessed through DMA
1410 * configuration based mailbox.
1412 ret
= tb_switch_add_dma_port(sw
);
1416 if (!sw
->safe_mode
) {
1418 ret
= tb_drom_read(sw
);
1420 tb_sw_warn(sw
, "tb_eeprom_read_rom failed\n");
1423 tb_sw_dbg(sw
, "uid: %#llx\n", sw
->uid
);
1425 tb_switch_set_uuid(sw
);
1427 for (i
= 0; i
<= sw
->config
.max_port_number
; i
++) {
1428 if (sw
->ports
[i
].disabled
) {
1429 tb_port_dbg(&sw
->ports
[i
], "disabled by eeprom\n");
1432 ret
= tb_init_port(&sw
->ports
[i
]);
1438 ret
= device_add(&sw
->dev
);
1443 dev_info(&sw
->dev
, "new device found, vendor=%#x device=%#x\n",
1444 sw
->vendor
, sw
->device
);
1445 if (sw
->vendor_name
&& sw
->device_name
)
1446 dev_info(&sw
->dev
, "%s %s\n", sw
->vendor_name
,
1450 ret
= tb_switch_nvm_add(sw
);
1452 device_del(&sw
->dev
);
1456 pm_runtime_set_active(&sw
->dev
);
1458 pm_runtime_set_autosuspend_delay(&sw
->dev
, TB_AUTOSUSPEND_DELAY
);
1459 pm_runtime_use_autosuspend(&sw
->dev
);
1460 pm_runtime_mark_last_busy(&sw
->dev
);
1461 pm_runtime_enable(&sw
->dev
);
1462 pm_request_autosuspend(&sw
->dev
);
1469 * tb_switch_remove() - Remove and release a switch
1470 * @sw: Switch to remove
1472 * This will remove the switch from the domain and release it after last
1473 * reference count drops to zero. If there are switches connected below
1474 * this switch, they will be removed as well.
1476 void tb_switch_remove(struct tb_switch
*sw
)
1481 pm_runtime_get_sync(&sw
->dev
);
1482 pm_runtime_disable(&sw
->dev
);
1485 /* port 0 is the switch itself and never has a remote */
1486 for (i
= 1; i
<= sw
->config
.max_port_number
; i
++) {
1487 if (tb_is_upstream_port(&sw
->ports
[i
]))
1489 if (sw
->ports
[i
].remote
)
1490 tb_switch_remove(sw
->ports
[i
].remote
->sw
);
1491 sw
->ports
[i
].remote
= NULL
;
1492 if (sw
->ports
[i
].xdomain
)
1493 tb_xdomain_remove(sw
->ports
[i
].xdomain
);
1494 sw
->ports
[i
].xdomain
= NULL
;
1497 if (!sw
->is_unplugged
)
1498 tb_plug_events_active(sw
, false);
1500 tb_switch_nvm_remove(sw
);
1503 dev_info(&sw
->dev
, "device disconnected\n");
1504 device_unregister(&sw
->dev
);
1508 * tb_sw_set_unplugged() - set is_unplugged on switch and downstream switches
1510 void tb_sw_set_unplugged(struct tb_switch
*sw
)
1513 if (sw
== sw
->tb
->root_switch
) {
1514 tb_sw_WARN(sw
, "cannot unplug root switch\n");
1517 if (sw
->is_unplugged
) {
1518 tb_sw_WARN(sw
, "is_unplugged already set\n");
1521 sw
->is_unplugged
= true;
1522 for (i
= 0; i
<= sw
->config
.max_port_number
; i
++) {
1523 if (!tb_is_upstream_port(&sw
->ports
[i
]) && sw
->ports
[i
].remote
)
1524 tb_sw_set_unplugged(sw
->ports
[i
].remote
->sw
);
1528 int tb_switch_resume(struct tb_switch
*sw
)
1531 tb_sw_dbg(sw
, "resuming switch\n");
1534 * Check for UID of the connected switches except for root
1535 * switch which we assume cannot be removed.
1540 err
= tb_drom_read_uid_only(sw
, &uid
);
1542 tb_sw_warn(sw
, "uid read failed\n");
1545 if (sw
->uid
!= uid
) {
1547 "changed while suspended (uid %#llx -> %#llx)\n",
1553 /* upload configuration */
1554 err
= tb_sw_write(sw
, 1 + (u32
*) &sw
->config
, TB_CFG_SWITCH
, 1, 3);
1558 err
= tb_plug_events_active(sw
, true);
1562 /* check for surviving downstream switches */
1563 for (i
= 1; i
<= sw
->config
.max_port_number
; i
++) {
1564 struct tb_port
*port
= &sw
->ports
[i
];
1565 if (tb_is_upstream_port(port
))
1569 if (tb_wait_for_port(port
, true) <= 0
1570 || tb_switch_resume(port
->remote
->sw
)) {
1572 "lost during suspend, disconnecting\n");
1573 tb_sw_set_unplugged(port
->remote
->sw
);
1579 void tb_switch_suspend(struct tb_switch
*sw
)
1582 err
= tb_plug_events_active(sw
, false);
1586 for (i
= 1; i
<= sw
->config
.max_port_number
; i
++) {
1587 if (!tb_is_upstream_port(&sw
->ports
[i
]) && sw
->ports
[i
].remote
)
1588 tb_switch_suspend(sw
->ports
[i
].remote
->sw
);
1591 * TODO: invoke tb_cfg_prepare_to_sleep here? does not seem to have any
1596 struct tb_sw_lookup
{
1604 static int tb_switch_match(struct device
*dev
, void *data
)
1606 struct tb_switch
*sw
= tb_to_switch(dev
);
1607 struct tb_sw_lookup
*lookup
= data
;
1611 if (sw
->tb
!= lookup
->tb
)
1615 return !memcmp(sw
->uuid
, lookup
->uuid
, sizeof(*lookup
->uuid
));
1617 if (lookup
->route
) {
1618 return sw
->config
.route_lo
== lower_32_bits(lookup
->route
) &&
1619 sw
->config
.route_hi
== upper_32_bits(lookup
->route
);
1622 /* Root switch is matched only by depth */
1626 return sw
->link
== lookup
->link
&& sw
->depth
== lookup
->depth
;
1630 * tb_switch_find_by_link_depth() - Find switch by link and depth
1631 * @tb: Domain the switch belongs
1632 * @link: Link number the switch is connected
1633 * @depth: Depth of the switch in link
1635 * Returned switch has reference count increased so the caller needs to
1636 * call tb_switch_put() when done with the switch.
1638 struct tb_switch
*tb_switch_find_by_link_depth(struct tb
*tb
, u8 link
, u8 depth
)
1640 struct tb_sw_lookup lookup
;
1643 memset(&lookup
, 0, sizeof(lookup
));
1646 lookup
.depth
= depth
;
1648 dev
= bus_find_device(&tb_bus_type
, NULL
, &lookup
, tb_switch_match
);
1650 return tb_to_switch(dev
);
1656 * tb_switch_find_by_uuid() - Find switch by UUID
1657 * @tb: Domain the switch belongs
1658 * @uuid: UUID to look for
1660 * Returned switch has reference count increased so the caller needs to
1661 * call tb_switch_put() when done with the switch.
1663 struct tb_switch
*tb_switch_find_by_uuid(struct tb
*tb
, const uuid_t
*uuid
)
1665 struct tb_sw_lookup lookup
;
1668 memset(&lookup
, 0, sizeof(lookup
));
1672 dev
= bus_find_device(&tb_bus_type
, NULL
, &lookup
, tb_switch_match
);
1674 return tb_to_switch(dev
);
1680 * tb_switch_find_by_route() - Find switch by route string
1681 * @tb: Domain the switch belongs
1682 * @route: Route string to look for
1684 * Returned switch has reference count increased so the caller needs to
1685 * call tb_switch_put() when done with the switch.
1687 struct tb_switch
*tb_switch_find_by_route(struct tb
*tb
, u64 route
)
1689 struct tb_sw_lookup lookup
;
1693 return tb_switch_get(tb
->root_switch
);
1695 memset(&lookup
, 0, sizeof(lookup
));
1697 lookup
.route
= route
;
1699 dev
= bus_find_device(&tb_bus_type
, NULL
, &lookup
, tb_switch_match
);
1701 return tb_to_switch(dev
);
1706 void tb_switch_exit(void)
1708 ida_destroy(&nvm_ida
);