1 // SPDX-License-Identifier: GPL-2.0
3 * Thunderbolt Cactus Ridge driver - switch/port utility functions
5 * Copyright (c) 2014 Andreas Noever <andreas.noever@gmail.com>
8 #include <linux/delay.h>
10 #include <linux/nvmem-provider.h>
11 #include <linux/sizes.h>
12 #include <linux/slab.h>
13 #include <linux/vmalloc.h>
17 /* Switch authorization from userspace is serialized by this lock */
18 static DEFINE_MUTEX(switch_lock
);
20 /* Switch NVM support */
22 #define NVM_DEVID 0x05
23 #define NVM_VERSION 0x08
25 #define NVM_FLASH_SIZE 0x45
27 #define NVM_MIN_SIZE SZ_32K
28 #define NVM_MAX_SIZE SZ_512K
30 static DEFINE_IDA(nvm_ida
);
32 struct nvm_auth_status
{
33 struct list_head list
;
39 * Hold NVM authentication failure status per switch This information
40 * needs to stay around even when the switch gets power cycled so we
43 static LIST_HEAD(nvm_auth_status_cache
);
44 static DEFINE_MUTEX(nvm_auth_status_lock
);
46 static struct nvm_auth_status
*__nvm_get_auth_status(const struct tb_switch
*sw
)
48 struct nvm_auth_status
*st
;
50 list_for_each_entry(st
, &nvm_auth_status_cache
, list
) {
51 if (uuid_equal(&st
->uuid
, sw
->uuid
))
58 static void nvm_get_auth_status(const struct tb_switch
*sw
, u32
*status
)
60 struct nvm_auth_status
*st
;
62 mutex_lock(&nvm_auth_status_lock
);
63 st
= __nvm_get_auth_status(sw
);
64 mutex_unlock(&nvm_auth_status_lock
);
66 *status
= st
? st
->status
: 0;
69 static void nvm_set_auth_status(const struct tb_switch
*sw
, u32 status
)
71 struct nvm_auth_status
*st
;
73 if (WARN_ON(!sw
->uuid
))
76 mutex_lock(&nvm_auth_status_lock
);
77 st
= __nvm_get_auth_status(sw
);
80 st
= kzalloc(sizeof(*st
), GFP_KERNEL
);
84 memcpy(&st
->uuid
, sw
->uuid
, sizeof(st
->uuid
));
85 INIT_LIST_HEAD(&st
->list
);
86 list_add_tail(&st
->list
, &nvm_auth_status_cache
);
91 mutex_unlock(&nvm_auth_status_lock
);
94 static void nvm_clear_auth_status(const struct tb_switch
*sw
)
96 struct nvm_auth_status
*st
;
98 mutex_lock(&nvm_auth_status_lock
);
99 st
= __nvm_get_auth_status(sw
);
104 mutex_unlock(&nvm_auth_status_lock
);
107 static int nvm_validate_and_write(struct tb_switch
*sw
)
109 unsigned int image_size
, hdr_size
;
110 const u8
*buf
= sw
->nvm
->buf
;
117 image_size
= sw
->nvm
->buf_data_size
;
118 if (image_size
< NVM_MIN_SIZE
|| image_size
> NVM_MAX_SIZE
)
122 * FARB pointer must point inside the image and must at least
123 * contain parts of the digital section we will be reading here.
125 hdr_size
= (*(u32
*)buf
) & 0xffffff;
126 if (hdr_size
+ NVM_DEVID
+ 2 >= image_size
)
129 /* Digital section start should be aligned to 4k page */
130 if (!IS_ALIGNED(hdr_size
, SZ_4K
))
134 * Read digital section size and check that it also fits inside
137 ds_size
= *(u16
*)(buf
+ hdr_size
);
138 if (ds_size
>= image_size
)
141 if (!sw
->safe_mode
) {
145 * Make sure the device ID in the image matches the one
146 * we read from the switch config space.
148 device_id
= *(u16
*)(buf
+ hdr_size
+ NVM_DEVID
);
149 if (device_id
!= sw
->config
.device_id
)
152 if (sw
->generation
< 3) {
153 /* Write CSS headers first */
154 ret
= dma_port_flash_write(sw
->dma_port
,
155 DMA_PORT_CSS_ADDRESS
, buf
+ NVM_CSS
,
156 DMA_PORT_CSS_MAX_SIZE
);
161 /* Skip headers in the image */
163 image_size
-= hdr_size
;
166 return dma_port_flash_write(sw
->dma_port
, 0, buf
, image_size
);
169 static int nvm_authenticate_host(struct tb_switch
*sw
)
174 * Root switch NVM upgrade requires that we disconnect the
175 * existing paths first (in case it is not in safe mode
178 if (!sw
->safe_mode
) {
179 ret
= tb_domain_disconnect_all_paths(sw
->tb
);
183 * The host controller goes away pretty soon after this if
184 * everything goes well so getting timeout is expected.
186 ret
= dma_port_flash_update_auth(sw
->dma_port
);
187 return ret
== -ETIMEDOUT
? 0 : ret
;
191 * From safe mode we can get out by just power cycling the
194 dma_port_power_cycle(sw
->dma_port
);
198 static int nvm_authenticate_device(struct tb_switch
*sw
)
200 int ret
, retries
= 10;
202 ret
= dma_port_flash_update_auth(sw
->dma_port
);
203 if (ret
&& ret
!= -ETIMEDOUT
)
207 * Poll here for the authentication status. It takes some time
208 * for the device to respond (we get timeout for a while). Once
209 * we get response the device needs to be power cycled in order
210 * to the new NVM to be taken into use.
215 ret
= dma_port_flash_update_auth_status(sw
->dma_port
, &status
);
216 if (ret
< 0 && ret
!= -ETIMEDOUT
)
220 tb_sw_warn(sw
, "failed to authenticate NVM\n");
221 nvm_set_auth_status(sw
, status
);
224 tb_sw_info(sw
, "power cycling the switch now\n");
225 dma_port_power_cycle(sw
->dma_port
);
235 static int tb_switch_nvm_read(void *priv
, unsigned int offset
, void *val
,
238 struct tb_switch
*sw
= priv
;
240 return dma_port_flash_read(sw
->dma_port
, offset
, val
, bytes
);
243 static int tb_switch_nvm_write(void *priv
, unsigned int offset
, void *val
,
246 struct tb_switch
*sw
= priv
;
249 if (mutex_lock_interruptible(&switch_lock
))
253 * Since writing the NVM image might require some special steps,
254 * for example when CSS headers are written, we cache the image
255 * locally here and handle the special cases when the user asks
256 * us to authenticate the image.
259 sw
->nvm
->buf
= vmalloc(NVM_MAX_SIZE
);
266 sw
->nvm
->buf_data_size
= offset
+ bytes
;
267 memcpy(sw
->nvm
->buf
+ offset
, val
, bytes
);
270 mutex_unlock(&switch_lock
);
275 static struct nvmem_device
*register_nvmem(struct tb_switch
*sw
, int id
,
276 size_t size
, bool active
)
278 struct nvmem_config config
;
280 memset(&config
, 0, sizeof(config
));
283 config
.name
= "nvm_active";
284 config
.reg_read
= tb_switch_nvm_read
;
285 config
.read_only
= true;
287 config
.name
= "nvm_non_active";
288 config
.reg_write
= tb_switch_nvm_write
;
289 config
.root_only
= true;
294 config
.word_size
= 4;
296 config
.dev
= &sw
->dev
;
297 config
.owner
= THIS_MODULE
;
300 return nvmem_register(&config
);
303 static int tb_switch_nvm_add(struct tb_switch
*sw
)
305 struct nvmem_device
*nvm_dev
;
306 struct tb_switch_nvm
*nvm
;
313 nvm
= kzalloc(sizeof(*nvm
), GFP_KERNEL
);
317 nvm
->id
= ida_simple_get(&nvm_ida
, 0, 0, GFP_KERNEL
);
320 * If the switch is in safe-mode the only accessible portion of
321 * the NVM is the non-active one where userspace is expected to
322 * write new functional NVM.
324 if (!sw
->safe_mode
) {
325 u32 nvm_size
, hdr_size
;
327 ret
= dma_port_flash_read(sw
->dma_port
, NVM_FLASH_SIZE
, &val
,
332 hdr_size
= sw
->generation
< 3 ? SZ_8K
: SZ_16K
;
333 nvm_size
= (SZ_1M
<< (val
& 7)) / 8;
334 nvm_size
= (nvm_size
- hdr_size
) / 2;
336 ret
= dma_port_flash_read(sw
->dma_port
, NVM_VERSION
, &val
,
341 nvm
->major
= val
>> 16;
342 nvm
->minor
= val
>> 8;
344 nvm_dev
= register_nvmem(sw
, nvm
->id
, nvm_size
, true);
345 if (IS_ERR(nvm_dev
)) {
346 ret
= PTR_ERR(nvm_dev
);
349 nvm
->active
= nvm_dev
;
352 nvm_dev
= register_nvmem(sw
, nvm
->id
, NVM_MAX_SIZE
, false);
353 if (IS_ERR(nvm_dev
)) {
354 ret
= PTR_ERR(nvm_dev
);
357 nvm
->non_active
= nvm_dev
;
359 mutex_lock(&switch_lock
);
361 mutex_unlock(&switch_lock
);
367 nvmem_unregister(nvm
->active
);
369 ida_simple_remove(&nvm_ida
, nvm
->id
);
375 static void tb_switch_nvm_remove(struct tb_switch
*sw
)
377 struct tb_switch_nvm
*nvm
;
379 mutex_lock(&switch_lock
);
382 mutex_unlock(&switch_lock
);
387 /* Remove authentication status in case the switch is unplugged */
388 if (!nvm
->authenticating
)
389 nvm_clear_auth_status(sw
);
391 nvmem_unregister(nvm
->non_active
);
393 nvmem_unregister(nvm
->active
);
394 ida_simple_remove(&nvm_ida
, nvm
->id
);
399 /* port utility functions */
401 static const char *tb_port_type(struct tb_regs_port_header
*port
)
403 switch (port
->type
>> 16) {
405 switch ((u8
) port
->type
) {
430 static void tb_dump_port(struct tb
*tb
, struct tb_regs_port_header
*port
)
433 " Port %d: %x:%x (Revision: %d, TB Version: %d, Type: %s (%#x))\n",
434 port
->port_number
, port
->vendor_id
, port
->device_id
,
435 port
->revision
, port
->thunderbolt_version
, tb_port_type(port
),
437 tb_info(tb
, " Max hop id (in/out): %d/%d\n",
438 port
->max_in_hop_id
, port
->max_out_hop_id
);
439 tb_info(tb
, " Max counters: %d\n", port
->max_counters
);
440 tb_info(tb
, " NFC Credits: %#x\n", port
->nfc_credits
);
444 * tb_port_state() - get connectedness state of a port
446 * The port must have a TB_CAP_PHY (i.e. it should be a real port).
448 * Return: Returns an enum tb_port_state on success or an error code on failure.
450 static int tb_port_state(struct tb_port
*port
)
452 struct tb_cap_phy phy
;
454 if (port
->cap_phy
== 0) {
455 tb_port_WARN(port
, "does not have a PHY\n");
458 res
= tb_port_read(port
, &phy
, TB_CFG_PORT
, port
->cap_phy
, 2);
465 * tb_wait_for_port() - wait for a port to become ready
467 * Wait up to 1 second for a port to reach state TB_PORT_UP. If
468 * wait_if_unplugged is set then we also wait if the port is in state
469 * TB_PORT_UNPLUGGED (it takes a while for the device to be registered after
470 * switch resume). Otherwise we only wait if a device is registered but the link
471 * has not yet been established.
473 * Return: Returns an error code on failure. Returns 0 if the port is not
474 * connected or failed to reach state TB_PORT_UP within one second. Returns 1
475 * if the port is connected and in state TB_PORT_UP.
477 int tb_wait_for_port(struct tb_port
*port
, bool wait_if_unplugged
)
481 if (!port
->cap_phy
) {
482 tb_port_WARN(port
, "does not have PHY\n");
485 if (tb_is_upstream_port(port
)) {
486 tb_port_WARN(port
, "is the upstream port\n");
491 state
= tb_port_state(port
);
494 if (state
== TB_PORT_DISABLED
) {
495 tb_port_info(port
, "is disabled (state: 0)\n");
498 if (state
== TB_PORT_UNPLUGGED
) {
499 if (wait_if_unplugged
) {
500 /* used during resume */
502 "is unplugged (state: 7), retrying...\n");
506 tb_port_info(port
, "is unplugged (state: 7)\n");
509 if (state
== TB_PORT_UP
) {
511 "is connected, link is up (state: 2)\n");
516 * After plug-in the state is TB_PORT_CONNECTING. Give it some
520 "is connected, link is not up (state: %d), retrying...\n",
525 "failed to reach state TB_PORT_UP. Ignoring port...\n");
530 * tb_port_add_nfc_credits() - add/remove non flow controlled credits to port
532 * Change the number of NFC credits allocated to @port by @credits. To remove
533 * NFC credits pass a negative amount of credits.
535 * Return: Returns 0 on success or an error code on failure.
537 int tb_port_add_nfc_credits(struct tb_port
*port
, int credits
)
542 "adding %#x NFC credits (%#x -> %#x)",
544 port
->config
.nfc_credits
,
545 port
->config
.nfc_credits
+ credits
);
546 port
->config
.nfc_credits
+= credits
;
547 return tb_port_write(port
, &port
->config
.nfc_credits
,
552 * tb_port_clear_counter() - clear a counter in TB_CFG_COUNTER
554 * Return: Returns 0 on success or an error code on failure.
556 int tb_port_clear_counter(struct tb_port
*port
, int counter
)
558 u32 zero
[3] = { 0, 0, 0 };
559 tb_port_info(port
, "clearing counter %d\n", counter
);
560 return tb_port_write(port
, zero
, TB_CFG_COUNTERS
, 3 * counter
, 3);
564 * tb_init_port() - initialize a port
566 * This is a helper method for tb_switch_alloc. Does not check or initialize
567 * any downstream switches.
569 * Return: Returns 0 on success or an error code on failure.
571 static int tb_init_port(struct tb_port
*port
)
576 res
= tb_port_read(port
, &port
->config
, TB_CFG_PORT
, 0, 8);
580 /* Port 0 is the switch itself and has no PHY. */
581 if (port
->config
.type
== TB_TYPE_PORT
&& port
->port
!= 0) {
582 cap
= tb_port_find_cap(port
, TB_PORT_CAP_PHY
);
587 tb_port_WARN(port
, "non switch port without a PHY\n");
590 tb_dump_port(port
->sw
->tb
, &port
->config
);
592 /* TODO: Read dual link port, DP port and more from EEPROM. */
597 /* switch utility functions */
599 static void tb_dump_switch(struct tb
*tb
, struct tb_regs_switch_header
*sw
)
602 " Switch: %x:%x (Revision: %d, TB Version: %d)\n",
603 sw
->vendor_id
, sw
->device_id
, sw
->revision
,
604 sw
->thunderbolt_version
);
605 tb_info(tb
, " Max Port Number: %d\n", sw
->max_port_number
);
606 tb_info(tb
, " Config:\n");
608 " Upstream Port Number: %d Depth: %d Route String: %#llx Enabled: %d, PlugEventsDelay: %dms\n",
609 sw
->upstream_port_number
, sw
->depth
,
610 (((u64
) sw
->route_hi
) << 32) | sw
->route_lo
,
611 sw
->enabled
, sw
->plug_events_delay
);
613 " unknown1: %#x unknown4: %#x\n",
614 sw
->__unknown1
, sw
->__unknown4
);
618 * reset_switch() - reconfigure route, enable and send TB_CFG_PKG_RESET
620 * Return: Returns 0 on success or an error code on failure.
622 int tb_switch_reset(struct tb
*tb
, u64 route
)
624 struct tb_cfg_result res
;
625 struct tb_regs_switch_header header
= {
626 header
.route_hi
= route
>> 32,
627 header
.route_lo
= route
,
628 header
.enabled
= true,
630 tb_info(tb
, "resetting switch at %llx\n", route
);
631 res
.err
= tb_cfg_write(tb
->ctl
, ((u32
*) &header
) + 2, route
,
635 res
= tb_cfg_reset(tb
->ctl
, route
, TB_CFG_DEFAULT_TIMEOUT
);
641 struct tb_switch
*get_switch_at_route(struct tb_switch
*sw
, u64 route
)
643 u8 next_port
= route
; /*
644 * Routes use a stride of 8 bits,
645 * eventhough a port index has 6 bits at most.
649 if (next_port
> sw
->config
.max_port_number
)
651 if (tb_is_upstream_port(&sw
->ports
[next_port
]))
653 if (!sw
->ports
[next_port
].remote
)
655 return get_switch_at_route(sw
->ports
[next_port
].remote
->sw
,
656 route
>> TB_ROUTE_SHIFT
);
660 * tb_plug_events_active() - enable/disable plug events on a switch
662 * Also configures a sane plug_events_delay of 255ms.
664 * Return: Returns 0 on success or an error code on failure.
666 static int tb_plug_events_active(struct tb_switch
*sw
, bool active
)
671 if (!sw
->config
.enabled
)
674 sw
->config
.plug_events_delay
= 0xff;
675 res
= tb_sw_write(sw
, ((u32
*) &sw
->config
) + 4, TB_CFG_SWITCH
, 4, 1);
679 res
= tb_sw_read(sw
, &data
, TB_CFG_SWITCH
, sw
->cap_plug_events
+ 1, 1);
684 data
= data
& 0xFFFFFF83;
685 switch (sw
->config
.device_id
) {
686 case PCI_DEVICE_ID_INTEL_LIGHT_RIDGE
:
687 case PCI_DEVICE_ID_INTEL_EAGLE_RIDGE
:
688 case PCI_DEVICE_ID_INTEL_PORT_RIDGE
:
696 return tb_sw_write(sw
, &data
, TB_CFG_SWITCH
,
697 sw
->cap_plug_events
+ 1, 1);
700 static ssize_t
authorized_show(struct device
*dev
,
701 struct device_attribute
*attr
,
704 struct tb_switch
*sw
= tb_to_switch(dev
);
706 return sprintf(buf
, "%u\n", sw
->authorized
);
709 static int tb_switch_set_authorized(struct tb_switch
*sw
, unsigned int val
)
713 if (mutex_lock_interruptible(&switch_lock
))
720 * Make sure there is no PCIe rescan ongoing when a new PCIe
721 * tunnel is created. Otherwise the PCIe rescan code might find
722 * the new tunnel too early.
724 pci_lock_rescan_remove();
730 ret
= tb_domain_approve_switch_key(sw
->tb
, sw
);
732 ret
= tb_domain_approve_switch(sw
->tb
, sw
);
735 /* Challenge switch */
738 ret
= tb_domain_challenge_switch_key(sw
->tb
, sw
);
745 pci_unlock_rescan_remove();
748 sw
->authorized
= val
;
749 /* Notify status change to the userspace */
750 kobject_uevent(&sw
->dev
.kobj
, KOBJ_CHANGE
);
754 mutex_unlock(&switch_lock
);
758 static ssize_t
authorized_store(struct device
*dev
,
759 struct device_attribute
*attr
,
760 const char *buf
, size_t count
)
762 struct tb_switch
*sw
= tb_to_switch(dev
);
766 ret
= kstrtouint(buf
, 0, &val
);
772 ret
= tb_switch_set_authorized(sw
, val
);
774 return ret
? ret
: count
;
776 static DEVICE_ATTR_RW(authorized
);
778 static ssize_t
boot_show(struct device
*dev
, struct device_attribute
*attr
,
781 struct tb_switch
*sw
= tb_to_switch(dev
);
783 return sprintf(buf
, "%u\n", sw
->boot
);
785 static DEVICE_ATTR_RO(boot
);
787 static ssize_t
device_show(struct device
*dev
, struct device_attribute
*attr
,
790 struct tb_switch
*sw
= tb_to_switch(dev
);
792 return sprintf(buf
, "%#x\n", sw
->device
);
794 static DEVICE_ATTR_RO(device
);
797 device_name_show(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
799 struct tb_switch
*sw
= tb_to_switch(dev
);
801 return sprintf(buf
, "%s\n", sw
->device_name
? sw
->device_name
: "");
803 static DEVICE_ATTR_RO(device_name
);
805 static ssize_t
key_show(struct device
*dev
, struct device_attribute
*attr
,
808 struct tb_switch
*sw
= tb_to_switch(dev
);
811 if (mutex_lock_interruptible(&switch_lock
))
815 ret
= sprintf(buf
, "%*phN\n", TB_SWITCH_KEY_SIZE
, sw
->key
);
817 ret
= sprintf(buf
, "\n");
819 mutex_unlock(&switch_lock
);
823 static ssize_t
key_store(struct device
*dev
, struct device_attribute
*attr
,
824 const char *buf
, size_t count
)
826 struct tb_switch
*sw
= tb_to_switch(dev
);
827 u8 key
[TB_SWITCH_KEY_SIZE
];
831 if (!strcmp(buf
, "\n"))
833 else if (hex2bin(key
, buf
, sizeof(key
)))
836 if (mutex_lock_interruptible(&switch_lock
))
839 if (sw
->authorized
) {
846 sw
->key
= kmemdup(key
, sizeof(key
), GFP_KERNEL
);
852 mutex_unlock(&switch_lock
);
855 static DEVICE_ATTR(key
, 0600, key_show
, key_store
);
857 static ssize_t
nvm_authenticate_show(struct device
*dev
,
858 struct device_attribute
*attr
, char *buf
)
860 struct tb_switch
*sw
= tb_to_switch(dev
);
863 nvm_get_auth_status(sw
, &status
);
864 return sprintf(buf
, "%#x\n", status
);
867 static ssize_t
nvm_authenticate_store(struct device
*dev
,
868 struct device_attribute
*attr
, const char *buf
, size_t count
)
870 struct tb_switch
*sw
= tb_to_switch(dev
);
874 if (mutex_lock_interruptible(&switch_lock
))
877 /* If NVMem devices are not yet added */
883 ret
= kstrtobool(buf
, &val
);
887 /* Always clear the authentication status */
888 nvm_clear_auth_status(sw
);
891 ret
= nvm_validate_and_write(sw
);
895 sw
->nvm
->authenticating
= true;
898 ret
= nvm_authenticate_host(sw
);
900 ret
= nvm_authenticate_device(sw
);
904 mutex_unlock(&switch_lock
);
910 static DEVICE_ATTR_RW(nvm_authenticate
);
912 static ssize_t
nvm_version_show(struct device
*dev
,
913 struct device_attribute
*attr
, char *buf
)
915 struct tb_switch
*sw
= tb_to_switch(dev
);
918 if (mutex_lock_interruptible(&switch_lock
))
926 ret
= sprintf(buf
, "%x.%x\n", sw
->nvm
->major
, sw
->nvm
->minor
);
928 mutex_unlock(&switch_lock
);
932 static DEVICE_ATTR_RO(nvm_version
);
934 static ssize_t
vendor_show(struct device
*dev
, struct device_attribute
*attr
,
937 struct tb_switch
*sw
= tb_to_switch(dev
);
939 return sprintf(buf
, "%#x\n", sw
->vendor
);
941 static DEVICE_ATTR_RO(vendor
);
944 vendor_name_show(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
946 struct tb_switch
*sw
= tb_to_switch(dev
);
948 return sprintf(buf
, "%s\n", sw
->vendor_name
? sw
->vendor_name
: "");
950 static DEVICE_ATTR_RO(vendor_name
);
952 static ssize_t
unique_id_show(struct device
*dev
, struct device_attribute
*attr
,
955 struct tb_switch
*sw
= tb_to_switch(dev
);
957 return sprintf(buf
, "%pUb\n", sw
->uuid
);
959 static DEVICE_ATTR_RO(unique_id
);
961 static struct attribute
*switch_attrs
[] = {
962 &dev_attr_authorized
.attr
,
964 &dev_attr_device
.attr
,
965 &dev_attr_device_name
.attr
,
967 &dev_attr_nvm_authenticate
.attr
,
968 &dev_attr_nvm_version
.attr
,
969 &dev_attr_vendor
.attr
,
970 &dev_attr_vendor_name
.attr
,
971 &dev_attr_unique_id
.attr
,
975 static umode_t
switch_attr_is_visible(struct kobject
*kobj
,
976 struct attribute
*attr
, int n
)
978 struct device
*dev
= container_of(kobj
, struct device
, kobj
);
979 struct tb_switch
*sw
= tb_to_switch(dev
);
981 if (attr
== &dev_attr_key
.attr
) {
983 sw
->tb
->security_level
== TB_SECURITY_SECURE
&&
984 sw
->security_level
== TB_SECURITY_SECURE
)
987 } else if (attr
== &dev_attr_nvm_authenticate
.attr
||
988 attr
== &dev_attr_nvm_version
.attr
) {
992 } else if (attr
== &dev_attr_boot
.attr
) {
998 return sw
->safe_mode
? 0 : attr
->mode
;
1001 static struct attribute_group switch_group
= {
1002 .is_visible
= switch_attr_is_visible
,
1003 .attrs
= switch_attrs
,
1006 static const struct attribute_group
*switch_groups
[] = {
1011 static void tb_switch_release(struct device
*dev
)
1013 struct tb_switch
*sw
= tb_to_switch(dev
);
1015 dma_port_free(sw
->dma_port
);
1018 kfree(sw
->device_name
);
1019 kfree(sw
->vendor_name
);
1026 struct device_type tb_switch_type
= {
1027 .name
= "thunderbolt_device",
1028 .release
= tb_switch_release
,
1031 static int tb_switch_get_generation(struct tb_switch
*sw
)
1033 switch (sw
->config
.device_id
) {
1034 case PCI_DEVICE_ID_INTEL_LIGHT_RIDGE
:
1035 case PCI_DEVICE_ID_INTEL_EAGLE_RIDGE
:
1036 case PCI_DEVICE_ID_INTEL_LIGHT_PEAK
:
1037 case PCI_DEVICE_ID_INTEL_CACTUS_RIDGE_2C
:
1038 case PCI_DEVICE_ID_INTEL_CACTUS_RIDGE_4C
:
1039 case PCI_DEVICE_ID_INTEL_PORT_RIDGE
:
1040 case PCI_DEVICE_ID_INTEL_REDWOOD_RIDGE_2C_BRIDGE
:
1041 case PCI_DEVICE_ID_INTEL_REDWOOD_RIDGE_4C_BRIDGE
:
1044 case PCI_DEVICE_ID_INTEL_WIN_RIDGE_2C_BRIDGE
:
1045 case PCI_DEVICE_ID_INTEL_FALCON_RIDGE_2C_BRIDGE
:
1046 case PCI_DEVICE_ID_INTEL_FALCON_RIDGE_4C_BRIDGE
:
1049 case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_LP_BRIDGE
:
1050 case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_2C_BRIDGE
:
1051 case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_4C_BRIDGE
:
1052 case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_C_2C_BRIDGE
:
1053 case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_C_4C_BRIDGE
:
1054 case PCI_DEVICE_ID_INTEL_TITAN_RIDGE_2C_BRIDGE
:
1055 case PCI_DEVICE_ID_INTEL_TITAN_RIDGE_4C_BRIDGE
:
1056 case PCI_DEVICE_ID_INTEL_TITAN_RIDGE_DD_BRIDGE
:
1061 * For unknown switches assume generation to be 1 to be
1064 tb_sw_warn(sw
, "unsupported switch device id %#x\n",
1065 sw
->config
.device_id
);
1071 * tb_switch_alloc() - allocate a switch
1072 * @tb: Pointer to the owning domain
1073 * @parent: Parent device for this switch
1074 * @route: Route string for this switch
1076 * Allocates and initializes a switch. Will not upload configuration to
1077 * the switch. For that you need to call tb_switch_configure()
1078 * separately. The returned switch should be released by calling
1081 * Return: Pointer to the allocated switch or %NULL in case of failure
1083 struct tb_switch
*tb_switch_alloc(struct tb
*tb
, struct device
*parent
,
1088 struct tb_switch
*sw
;
1089 int upstream_port
= tb_cfg_get_upstream_port(tb
->ctl
, route
);
1090 if (upstream_port
< 0)
1093 sw
= kzalloc(sizeof(*sw
), GFP_KERNEL
);
1098 if (tb_cfg_read(tb
->ctl
, &sw
->config
, route
, 0, TB_CFG_SWITCH
, 0, 5))
1099 goto err_free_sw_ports
;
1101 tb_info(tb
, "current switch config:\n");
1102 tb_dump_switch(tb
, &sw
->config
);
1104 /* configure switch */
1105 sw
->config
.upstream_port_number
= upstream_port
;
1106 sw
->config
.depth
= tb_route_length(route
);
1107 sw
->config
.route_lo
= route
;
1108 sw
->config
.route_hi
= route
>> 32;
1109 sw
->config
.enabled
= 0;
1111 /* initialize ports */
1112 sw
->ports
= kcalloc(sw
->config
.max_port_number
+ 1, sizeof(*sw
->ports
),
1115 goto err_free_sw_ports
;
1117 for (i
= 0; i
<= sw
->config
.max_port_number
; i
++) {
1118 /* minimum setup for tb_find_cap and tb_drom_read to work */
1119 sw
->ports
[i
].sw
= sw
;
1120 sw
->ports
[i
].port
= i
;
1123 sw
->generation
= tb_switch_get_generation(sw
);
1125 cap
= tb_switch_find_vse_cap(sw
, TB_VSE_CAP_PLUG_EVENTS
);
1127 tb_sw_warn(sw
, "cannot find TB_VSE_CAP_PLUG_EVENTS aborting\n");
1128 goto err_free_sw_ports
;
1130 sw
->cap_plug_events
= cap
;
1132 /* Root switch is always authorized */
1134 sw
->authorized
= true;
1136 device_initialize(&sw
->dev
);
1137 sw
->dev
.parent
= parent
;
1138 sw
->dev
.bus
= &tb_bus_type
;
1139 sw
->dev
.type
= &tb_switch_type
;
1140 sw
->dev
.groups
= switch_groups
;
1141 dev_set_name(&sw
->dev
, "%u-%llx", tb
->index
, tb_route(sw
));
1153 * tb_switch_alloc_safe_mode() - allocate a switch that is in safe mode
1154 * @tb: Pointer to the owning domain
1155 * @parent: Parent device for this switch
1156 * @route: Route string for this switch
1158 * This creates a switch in safe mode. This means the switch pretty much
1159 * lacks all capabilities except DMA configuration port before it is
1160 * flashed with a valid NVM firmware.
1162 * The returned switch must be released by calling tb_switch_put().
1164 * Return: Pointer to the allocated switch or %NULL in case of failure
1167 tb_switch_alloc_safe_mode(struct tb
*tb
, struct device
*parent
, u64 route
)
1169 struct tb_switch
*sw
;
1171 sw
= kzalloc(sizeof(*sw
), GFP_KERNEL
);
1176 sw
->config
.depth
= tb_route_length(route
);
1177 sw
->config
.route_hi
= upper_32_bits(route
);
1178 sw
->config
.route_lo
= lower_32_bits(route
);
1179 sw
->safe_mode
= true;
1181 device_initialize(&sw
->dev
);
1182 sw
->dev
.parent
= parent
;
1183 sw
->dev
.bus
= &tb_bus_type
;
1184 sw
->dev
.type
= &tb_switch_type
;
1185 sw
->dev
.groups
= switch_groups
;
1186 dev_set_name(&sw
->dev
, "%u-%llx", tb
->index
, tb_route(sw
));
1192 * tb_switch_configure() - Uploads configuration to the switch
1193 * @sw: Switch to configure
1195 * Call this function before the switch is added to the system. It will
1196 * upload configuration to the switch and makes it available for the
1197 * connection manager to use.
1199 * Return: %0 in case of success and negative errno in case of failure
1201 int tb_switch_configure(struct tb_switch
*sw
)
1203 struct tb
*tb
= sw
->tb
;
1207 route
= tb_route(sw
);
1209 "initializing Switch at %#llx (depth: %d, up port: %d)\n",
1210 route
, tb_route_length(route
), sw
->config
.upstream_port_number
);
1212 if (sw
->config
.vendor_id
!= PCI_VENDOR_ID_INTEL
)
1213 tb_sw_warn(sw
, "unknown switch vendor id %#x\n",
1214 sw
->config
.vendor_id
);
1216 sw
->config
.enabled
= 1;
1218 /* upload configuration */
1219 ret
= tb_sw_write(sw
, 1 + (u32
*)&sw
->config
, TB_CFG_SWITCH
, 1, 3);
1223 return tb_plug_events_active(sw
, true);
1226 static void tb_switch_set_uuid(struct tb_switch
*sw
)
1235 * The newer controllers include fused UUID as part of link
1236 * controller specific registers
1238 cap
= tb_switch_find_vse_cap(sw
, TB_VSE_CAP_LINK_CONTROLLER
);
1240 tb_sw_read(sw
, uuid
, TB_CFG_SWITCH
, cap
+ 3, 4);
1243 * ICM generates UUID based on UID and fills the upper
1244 * two words with ones. This is not strictly following
1245 * UUID format but we want to be compatible with it so
1246 * we do the same here.
1248 uuid
[0] = sw
->uid
& 0xffffffff;
1249 uuid
[1] = (sw
->uid
>> 32) & 0xffffffff;
1250 uuid
[2] = 0xffffffff;
1251 uuid
[3] = 0xffffffff;
1254 sw
->uuid
= kmemdup(uuid
, sizeof(uuid
), GFP_KERNEL
);
1257 static int tb_switch_add_dma_port(struct tb_switch
*sw
)
1262 switch (sw
->generation
) {
1267 /* Only root switch can be upgraded */
1274 * DMA port is the only thing available when the switch
1282 if (sw
->no_nvm_upgrade
)
1285 sw
->dma_port
= dma_port_alloc(sw
);
1290 * Check status of the previous flash authentication. If there
1291 * is one we need to power cycle the switch in any case to make
1292 * it functional again.
1294 ret
= dma_port_flash_update_auth_status(sw
->dma_port
, &status
);
1299 tb_sw_info(sw
, "switch flash authentication failed\n");
1300 tb_switch_set_uuid(sw
);
1301 nvm_set_auth_status(sw
, status
);
1304 tb_sw_info(sw
, "power cycling the switch now\n");
1305 dma_port_power_cycle(sw
->dma_port
);
1308 * We return error here which causes the switch adding failure.
1309 * It should appear back after power cycle is complete.
1315 * tb_switch_add() - Add a switch to the domain
1316 * @sw: Switch to add
1318 * This is the last step in adding switch to the domain. It will read
1319 * identification information from DROM and initializes ports so that
1320 * they can be used to connect other switches. The switch will be
1321 * exposed to the userspace when this function successfully returns. To
1322 * remove and release the switch, call tb_switch_remove().
1324 * Return: %0 in case of success and negative errno in case of failure
1326 int tb_switch_add(struct tb_switch
*sw
)
1331 * Initialize DMA control port now before we read DROM. Recent
1332 * host controllers have more complete DROM on NVM that includes
1333 * vendor and model identification strings which we then expose
1334 * to the userspace. NVM can be accessed through DMA
1335 * configuration based mailbox.
1337 ret
= tb_switch_add_dma_port(sw
);
1341 if (!sw
->safe_mode
) {
1343 ret
= tb_drom_read(sw
);
1345 tb_sw_warn(sw
, "tb_eeprom_read_rom failed\n");
1348 tb_sw_info(sw
, "uid: %#llx\n", sw
->uid
);
1350 tb_switch_set_uuid(sw
);
1352 for (i
= 0; i
<= sw
->config
.max_port_number
; i
++) {
1353 if (sw
->ports
[i
].disabled
) {
1354 tb_port_info(&sw
->ports
[i
], "disabled by eeprom\n");
1357 ret
= tb_init_port(&sw
->ports
[i
]);
1363 ret
= device_add(&sw
->dev
);
1367 ret
= tb_switch_nvm_add(sw
);
1369 device_del(&sw
->dev
);
1375 * tb_switch_remove() - Remove and release a switch
1376 * @sw: Switch to remove
1378 * This will remove the switch from the domain and release it after last
1379 * reference count drops to zero. If there are switches connected below
1380 * this switch, they will be removed as well.
1382 void tb_switch_remove(struct tb_switch
*sw
)
1386 /* port 0 is the switch itself and never has a remote */
1387 for (i
= 1; i
<= sw
->config
.max_port_number
; i
++) {
1388 if (tb_is_upstream_port(&sw
->ports
[i
]))
1390 if (sw
->ports
[i
].remote
)
1391 tb_switch_remove(sw
->ports
[i
].remote
->sw
);
1392 sw
->ports
[i
].remote
= NULL
;
1393 if (sw
->ports
[i
].xdomain
)
1394 tb_xdomain_remove(sw
->ports
[i
].xdomain
);
1395 sw
->ports
[i
].xdomain
= NULL
;
1398 if (!sw
->is_unplugged
)
1399 tb_plug_events_active(sw
, false);
1401 tb_switch_nvm_remove(sw
);
1402 device_unregister(&sw
->dev
);
1406 * tb_sw_set_unplugged() - set is_unplugged on switch and downstream switches
1408 void tb_sw_set_unplugged(struct tb_switch
*sw
)
1411 if (sw
== sw
->tb
->root_switch
) {
1412 tb_sw_WARN(sw
, "cannot unplug root switch\n");
1415 if (sw
->is_unplugged
) {
1416 tb_sw_WARN(sw
, "is_unplugged already set\n");
1419 sw
->is_unplugged
= true;
1420 for (i
= 0; i
<= sw
->config
.max_port_number
; i
++) {
1421 if (!tb_is_upstream_port(&sw
->ports
[i
]) && sw
->ports
[i
].remote
)
1422 tb_sw_set_unplugged(sw
->ports
[i
].remote
->sw
);
1426 int tb_switch_resume(struct tb_switch
*sw
)
1429 tb_sw_info(sw
, "resuming switch\n");
1432 * Check for UID of the connected switches except for root
1433 * switch which we assume cannot be removed.
1438 err
= tb_drom_read_uid_only(sw
, &uid
);
1440 tb_sw_warn(sw
, "uid read failed\n");
1443 if (sw
->uid
!= uid
) {
1445 "changed while suspended (uid %#llx -> %#llx)\n",
1451 /* upload configuration */
1452 err
= tb_sw_write(sw
, 1 + (u32
*) &sw
->config
, TB_CFG_SWITCH
, 1, 3);
1456 err
= tb_plug_events_active(sw
, true);
1460 /* check for surviving downstream switches */
1461 for (i
= 1; i
<= sw
->config
.max_port_number
; i
++) {
1462 struct tb_port
*port
= &sw
->ports
[i
];
1463 if (tb_is_upstream_port(port
))
1467 if (tb_wait_for_port(port
, true) <= 0
1468 || tb_switch_resume(port
->remote
->sw
)) {
1470 "lost during suspend, disconnecting\n");
1471 tb_sw_set_unplugged(port
->remote
->sw
);
1477 void tb_switch_suspend(struct tb_switch
*sw
)
1480 err
= tb_plug_events_active(sw
, false);
1484 for (i
= 1; i
<= sw
->config
.max_port_number
; i
++) {
1485 if (!tb_is_upstream_port(&sw
->ports
[i
]) && sw
->ports
[i
].remote
)
1486 tb_switch_suspend(sw
->ports
[i
].remote
->sw
);
1489 * TODO: invoke tb_cfg_prepare_to_sleep here? does not seem to have any
1494 struct tb_sw_lookup
{
1502 static int tb_switch_match(struct device
*dev
, void *data
)
1504 struct tb_switch
*sw
= tb_to_switch(dev
);
1505 struct tb_sw_lookup
*lookup
= data
;
1509 if (sw
->tb
!= lookup
->tb
)
1513 return !memcmp(sw
->uuid
, lookup
->uuid
, sizeof(*lookup
->uuid
));
1515 if (lookup
->route
) {
1516 return sw
->config
.route_lo
== lower_32_bits(lookup
->route
) &&
1517 sw
->config
.route_hi
== upper_32_bits(lookup
->route
);
1520 /* Root switch is matched only by depth */
1524 return sw
->link
== lookup
->link
&& sw
->depth
== lookup
->depth
;
1528 * tb_switch_find_by_link_depth() - Find switch by link and depth
1529 * @tb: Domain the switch belongs
1530 * @link: Link number the switch is connected
1531 * @depth: Depth of the switch in link
1533 * Returned switch has reference count increased so the caller needs to
1534 * call tb_switch_put() when done with the switch.
1536 struct tb_switch
*tb_switch_find_by_link_depth(struct tb
*tb
, u8 link
, u8 depth
)
1538 struct tb_sw_lookup lookup
;
1541 memset(&lookup
, 0, sizeof(lookup
));
1544 lookup
.depth
= depth
;
1546 dev
= bus_find_device(&tb_bus_type
, NULL
, &lookup
, tb_switch_match
);
1548 return tb_to_switch(dev
);
1554 * tb_switch_find_by_uuid() - Find switch by UUID
1555 * @tb: Domain the switch belongs
1556 * @uuid: UUID to look for
1558 * Returned switch has reference count increased so the caller needs to
1559 * call tb_switch_put() when done with the switch.
1561 struct tb_switch
*tb_switch_find_by_uuid(struct tb
*tb
, const uuid_t
*uuid
)
1563 struct tb_sw_lookup lookup
;
1566 memset(&lookup
, 0, sizeof(lookup
));
1570 dev
= bus_find_device(&tb_bus_type
, NULL
, &lookup
, tb_switch_match
);
1572 return tb_to_switch(dev
);
1578 * tb_switch_find_by_route() - Find switch by route string
1579 * @tb: Domain the switch belongs
1580 * @route: Route string to look for
1582 * Returned switch has reference count increased so the caller needs to
1583 * call tb_switch_put() when done with the switch.
1585 struct tb_switch
*tb_switch_find_by_route(struct tb
*tb
, u64 route
)
1587 struct tb_sw_lookup lookup
;
1591 return tb_switch_get(tb
->root_switch
);
1593 memset(&lookup
, 0, sizeof(lookup
));
1595 lookup
.route
= route
;
1597 dev
= bus_find_device(&tb_bus_type
, NULL
, &lookup
, tb_switch_match
);
1599 return tb_to_switch(dev
);
1604 void tb_switch_exit(void)
1606 ida_destroy(&nvm_ida
);