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/pm_runtime.h>
12 #include <linux/sizes.h>
13 #include <linux/slab.h>
14 #include <linux/vmalloc.h>
18 /* Switch authorization from userspace is serialized by this lock */
19 static DEFINE_MUTEX(switch_lock
);
21 /* Switch NVM support */
23 #define NVM_DEVID 0x05
24 #define NVM_VERSION 0x08
26 #define NVM_FLASH_SIZE 0x45
28 #define NVM_MIN_SIZE SZ_32K
29 #define NVM_MAX_SIZE SZ_512K
31 static DEFINE_IDA(nvm_ida
);
33 struct nvm_auth_status
{
34 struct list_head list
;
40 * Hold NVM authentication failure status per switch This information
41 * needs to stay around even when the switch gets power cycled so we
44 static LIST_HEAD(nvm_auth_status_cache
);
45 static DEFINE_MUTEX(nvm_auth_status_lock
);
47 static struct nvm_auth_status
*__nvm_get_auth_status(const struct tb_switch
*sw
)
49 struct nvm_auth_status
*st
;
51 list_for_each_entry(st
, &nvm_auth_status_cache
, list
) {
52 if (uuid_equal(&st
->uuid
, sw
->uuid
))
59 static void nvm_get_auth_status(const struct tb_switch
*sw
, u32
*status
)
61 struct nvm_auth_status
*st
;
63 mutex_lock(&nvm_auth_status_lock
);
64 st
= __nvm_get_auth_status(sw
);
65 mutex_unlock(&nvm_auth_status_lock
);
67 *status
= st
? st
->status
: 0;
70 static void nvm_set_auth_status(const struct tb_switch
*sw
, u32 status
)
72 struct nvm_auth_status
*st
;
74 if (WARN_ON(!sw
->uuid
))
77 mutex_lock(&nvm_auth_status_lock
);
78 st
= __nvm_get_auth_status(sw
);
81 st
= kzalloc(sizeof(*st
), GFP_KERNEL
);
85 memcpy(&st
->uuid
, sw
->uuid
, sizeof(st
->uuid
));
86 INIT_LIST_HEAD(&st
->list
);
87 list_add_tail(&st
->list
, &nvm_auth_status_cache
);
92 mutex_unlock(&nvm_auth_status_lock
);
95 static void nvm_clear_auth_status(const struct tb_switch
*sw
)
97 struct nvm_auth_status
*st
;
99 mutex_lock(&nvm_auth_status_lock
);
100 st
= __nvm_get_auth_status(sw
);
105 mutex_unlock(&nvm_auth_status_lock
);
108 static int nvm_validate_and_write(struct tb_switch
*sw
)
110 unsigned int image_size
, hdr_size
;
111 const u8
*buf
= sw
->nvm
->buf
;
118 image_size
= sw
->nvm
->buf_data_size
;
119 if (image_size
< NVM_MIN_SIZE
|| image_size
> NVM_MAX_SIZE
)
123 * FARB pointer must point inside the image and must at least
124 * contain parts of the digital section we will be reading here.
126 hdr_size
= (*(u32
*)buf
) & 0xffffff;
127 if (hdr_size
+ NVM_DEVID
+ 2 >= image_size
)
130 /* Digital section start should be aligned to 4k page */
131 if (!IS_ALIGNED(hdr_size
, SZ_4K
))
135 * Read digital section size and check that it also fits inside
138 ds_size
= *(u16
*)(buf
+ hdr_size
);
139 if (ds_size
>= image_size
)
142 if (!sw
->safe_mode
) {
146 * Make sure the device ID in the image matches the one
147 * we read from the switch config space.
149 device_id
= *(u16
*)(buf
+ hdr_size
+ NVM_DEVID
);
150 if (device_id
!= sw
->config
.device_id
)
153 if (sw
->generation
< 3) {
154 /* Write CSS headers first */
155 ret
= dma_port_flash_write(sw
->dma_port
,
156 DMA_PORT_CSS_ADDRESS
, buf
+ NVM_CSS
,
157 DMA_PORT_CSS_MAX_SIZE
);
162 /* Skip headers in the image */
164 image_size
-= hdr_size
;
167 return dma_port_flash_write(sw
->dma_port
, 0, buf
, image_size
);
170 static int nvm_authenticate_host(struct tb_switch
*sw
)
175 * Root switch NVM upgrade requires that we disconnect the
176 * existing paths first (in case it is not in safe mode
179 if (!sw
->safe_mode
) {
180 ret
= tb_domain_disconnect_all_paths(sw
->tb
);
184 * The host controller goes away pretty soon after this if
185 * everything goes well so getting timeout is expected.
187 ret
= dma_port_flash_update_auth(sw
->dma_port
);
188 return ret
== -ETIMEDOUT
? 0 : ret
;
192 * From safe mode we can get out by just power cycling the
195 dma_port_power_cycle(sw
->dma_port
);
199 static int nvm_authenticate_device(struct tb_switch
*sw
)
201 int ret
, retries
= 10;
203 ret
= dma_port_flash_update_auth(sw
->dma_port
);
204 if (ret
&& ret
!= -ETIMEDOUT
)
208 * Poll here for the authentication status. It takes some time
209 * for the device to respond (we get timeout for a while). Once
210 * we get response the device needs to be power cycled in order
211 * to the new NVM to be taken into use.
216 ret
= dma_port_flash_update_auth_status(sw
->dma_port
, &status
);
217 if (ret
< 0 && ret
!= -ETIMEDOUT
)
221 tb_sw_warn(sw
, "failed to authenticate NVM\n");
222 nvm_set_auth_status(sw
, status
);
225 tb_sw_info(sw
, "power cycling the switch now\n");
226 dma_port_power_cycle(sw
->dma_port
);
236 static int tb_switch_nvm_read(void *priv
, unsigned int offset
, void *val
,
239 struct tb_switch
*sw
= priv
;
242 pm_runtime_get_sync(&sw
->dev
);
243 ret
= dma_port_flash_read(sw
->dma_port
, offset
, val
, bytes
);
244 pm_runtime_mark_last_busy(&sw
->dev
);
245 pm_runtime_put_autosuspend(&sw
->dev
);
250 static int tb_switch_nvm_write(void *priv
, unsigned int offset
, void *val
,
253 struct tb_switch
*sw
= priv
;
256 if (mutex_lock_interruptible(&switch_lock
))
260 * Since writing the NVM image might require some special steps,
261 * for example when CSS headers are written, we cache the image
262 * locally here and handle the special cases when the user asks
263 * us to authenticate the image.
266 sw
->nvm
->buf
= vmalloc(NVM_MAX_SIZE
);
273 sw
->nvm
->buf_data_size
= offset
+ bytes
;
274 memcpy(sw
->nvm
->buf
+ offset
, val
, bytes
);
277 mutex_unlock(&switch_lock
);
282 static struct nvmem_device
*register_nvmem(struct tb_switch
*sw
, int id
,
283 size_t size
, bool active
)
285 struct nvmem_config config
;
287 memset(&config
, 0, sizeof(config
));
290 config
.name
= "nvm_active";
291 config
.reg_read
= tb_switch_nvm_read
;
292 config
.read_only
= true;
294 config
.name
= "nvm_non_active";
295 config
.reg_write
= tb_switch_nvm_write
;
296 config
.root_only
= true;
301 config
.word_size
= 4;
303 config
.dev
= &sw
->dev
;
304 config
.owner
= THIS_MODULE
;
307 return nvmem_register(&config
);
310 static int tb_switch_nvm_add(struct tb_switch
*sw
)
312 struct nvmem_device
*nvm_dev
;
313 struct tb_switch_nvm
*nvm
;
320 nvm
= kzalloc(sizeof(*nvm
), GFP_KERNEL
);
324 nvm
->id
= ida_simple_get(&nvm_ida
, 0, 0, GFP_KERNEL
);
327 * If the switch is in safe-mode the only accessible portion of
328 * the NVM is the non-active one where userspace is expected to
329 * write new functional NVM.
331 if (!sw
->safe_mode
) {
332 u32 nvm_size
, hdr_size
;
334 ret
= dma_port_flash_read(sw
->dma_port
, NVM_FLASH_SIZE
, &val
,
339 hdr_size
= sw
->generation
< 3 ? SZ_8K
: SZ_16K
;
340 nvm_size
= (SZ_1M
<< (val
& 7)) / 8;
341 nvm_size
= (nvm_size
- hdr_size
) / 2;
343 ret
= dma_port_flash_read(sw
->dma_port
, NVM_VERSION
, &val
,
348 nvm
->major
= val
>> 16;
349 nvm
->minor
= val
>> 8;
351 nvm_dev
= register_nvmem(sw
, nvm
->id
, nvm_size
, true);
352 if (IS_ERR(nvm_dev
)) {
353 ret
= PTR_ERR(nvm_dev
);
356 nvm
->active
= nvm_dev
;
359 nvm_dev
= register_nvmem(sw
, nvm
->id
, NVM_MAX_SIZE
, false);
360 if (IS_ERR(nvm_dev
)) {
361 ret
= PTR_ERR(nvm_dev
);
364 nvm
->non_active
= nvm_dev
;
366 mutex_lock(&switch_lock
);
368 mutex_unlock(&switch_lock
);
374 nvmem_unregister(nvm
->active
);
376 ida_simple_remove(&nvm_ida
, nvm
->id
);
382 static void tb_switch_nvm_remove(struct tb_switch
*sw
)
384 struct tb_switch_nvm
*nvm
;
386 mutex_lock(&switch_lock
);
389 mutex_unlock(&switch_lock
);
394 /* Remove authentication status in case the switch is unplugged */
395 if (!nvm
->authenticating
)
396 nvm_clear_auth_status(sw
);
398 nvmem_unregister(nvm
->non_active
);
400 nvmem_unregister(nvm
->active
);
401 ida_simple_remove(&nvm_ida
, nvm
->id
);
406 /* port utility functions */
408 static const char *tb_port_type(struct tb_regs_port_header
*port
)
410 switch (port
->type
>> 16) {
412 switch ((u8
) port
->type
) {
437 static void tb_dump_port(struct tb
*tb
, struct tb_regs_port_header
*port
)
440 " Port %d: %x:%x (Revision: %d, TB Version: %d, Type: %s (%#x))\n",
441 port
->port_number
, port
->vendor_id
, port
->device_id
,
442 port
->revision
, port
->thunderbolt_version
, tb_port_type(port
),
444 tb_info(tb
, " Max hop id (in/out): %d/%d\n",
445 port
->max_in_hop_id
, port
->max_out_hop_id
);
446 tb_info(tb
, " Max counters: %d\n", port
->max_counters
);
447 tb_info(tb
, " NFC Credits: %#x\n", port
->nfc_credits
);
451 * tb_port_state() - get connectedness state of a port
453 * The port must have a TB_CAP_PHY (i.e. it should be a real port).
455 * Return: Returns an enum tb_port_state on success or an error code on failure.
457 static int tb_port_state(struct tb_port
*port
)
459 struct tb_cap_phy phy
;
461 if (port
->cap_phy
== 0) {
462 tb_port_WARN(port
, "does not have a PHY\n");
465 res
= tb_port_read(port
, &phy
, TB_CFG_PORT
, port
->cap_phy
, 2);
472 * tb_wait_for_port() - wait for a port to become ready
474 * Wait up to 1 second for a port to reach state TB_PORT_UP. If
475 * wait_if_unplugged is set then we also wait if the port is in state
476 * TB_PORT_UNPLUGGED (it takes a while for the device to be registered after
477 * switch resume). Otherwise we only wait if a device is registered but the link
478 * has not yet been established.
480 * Return: Returns an error code on failure. Returns 0 if the port is not
481 * connected or failed to reach state TB_PORT_UP within one second. Returns 1
482 * if the port is connected and in state TB_PORT_UP.
484 int tb_wait_for_port(struct tb_port
*port
, bool wait_if_unplugged
)
488 if (!port
->cap_phy
) {
489 tb_port_WARN(port
, "does not have PHY\n");
492 if (tb_is_upstream_port(port
)) {
493 tb_port_WARN(port
, "is the upstream port\n");
498 state
= tb_port_state(port
);
501 if (state
== TB_PORT_DISABLED
) {
502 tb_port_info(port
, "is disabled (state: 0)\n");
505 if (state
== TB_PORT_UNPLUGGED
) {
506 if (wait_if_unplugged
) {
507 /* used during resume */
509 "is unplugged (state: 7), retrying...\n");
513 tb_port_info(port
, "is unplugged (state: 7)\n");
516 if (state
== TB_PORT_UP
) {
518 "is connected, link is up (state: 2)\n");
523 * After plug-in the state is TB_PORT_CONNECTING. Give it some
527 "is connected, link is not up (state: %d), retrying...\n",
532 "failed to reach state TB_PORT_UP. Ignoring port...\n");
537 * tb_port_add_nfc_credits() - add/remove non flow controlled credits to port
539 * Change the number of NFC credits allocated to @port by @credits. To remove
540 * NFC credits pass a negative amount of credits.
542 * Return: Returns 0 on success or an error code on failure.
544 int tb_port_add_nfc_credits(struct tb_port
*port
, int credits
)
549 "adding %#x NFC credits (%#x -> %#x)",
551 port
->config
.nfc_credits
,
552 port
->config
.nfc_credits
+ credits
);
553 port
->config
.nfc_credits
+= credits
;
554 return tb_port_write(port
, &port
->config
.nfc_credits
,
559 * tb_port_clear_counter() - clear a counter in TB_CFG_COUNTER
561 * Return: Returns 0 on success or an error code on failure.
563 int tb_port_clear_counter(struct tb_port
*port
, int counter
)
565 u32 zero
[3] = { 0, 0, 0 };
566 tb_port_info(port
, "clearing counter %d\n", counter
);
567 return tb_port_write(port
, zero
, TB_CFG_COUNTERS
, 3 * counter
, 3);
571 * tb_init_port() - initialize a port
573 * This is a helper method for tb_switch_alloc. Does not check or initialize
574 * any downstream switches.
576 * Return: Returns 0 on success or an error code on failure.
578 static int tb_init_port(struct tb_port
*port
)
583 res
= tb_port_read(port
, &port
->config
, TB_CFG_PORT
, 0, 8);
587 /* Port 0 is the switch itself and has no PHY. */
588 if (port
->config
.type
== TB_TYPE_PORT
&& port
->port
!= 0) {
589 cap
= tb_port_find_cap(port
, TB_PORT_CAP_PHY
);
594 tb_port_WARN(port
, "non switch port without a PHY\n");
597 tb_dump_port(port
->sw
->tb
, &port
->config
);
599 /* TODO: Read dual link port, DP port and more from EEPROM. */
604 /* switch utility functions */
606 static void tb_dump_switch(struct tb
*tb
, struct tb_regs_switch_header
*sw
)
609 " Switch: %x:%x (Revision: %d, TB Version: %d)\n",
610 sw
->vendor_id
, sw
->device_id
, sw
->revision
,
611 sw
->thunderbolt_version
);
612 tb_info(tb
, " Max Port Number: %d\n", sw
->max_port_number
);
613 tb_info(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
);
620 " unknown1: %#x unknown4: %#x\n",
621 sw
->__unknown1
, sw
->__unknown4
);
625 * reset_switch() - reconfigure route, enable and send TB_CFG_PKG_RESET
627 * Return: Returns 0 on success or an error code on failure.
629 int tb_switch_reset(struct tb
*tb
, u64 route
)
631 struct tb_cfg_result res
;
632 struct tb_regs_switch_header header
= {
633 header
.route_hi
= route
>> 32,
634 header
.route_lo
= route
,
635 header
.enabled
= true,
637 tb_info(tb
, "resetting switch at %llx\n", route
);
638 res
.err
= tb_cfg_write(tb
->ctl
, ((u32
*) &header
) + 2, route
,
642 res
= tb_cfg_reset(tb
->ctl
, route
, TB_CFG_DEFAULT_TIMEOUT
);
648 struct tb_switch
*get_switch_at_route(struct tb_switch
*sw
, u64 route
)
650 u8 next_port
= route
; /*
651 * Routes use a stride of 8 bits,
652 * eventhough a port index has 6 bits at most.
656 if (next_port
> sw
->config
.max_port_number
)
658 if (tb_is_upstream_port(&sw
->ports
[next_port
]))
660 if (!sw
->ports
[next_port
].remote
)
662 return get_switch_at_route(sw
->ports
[next_port
].remote
->sw
,
663 route
>> TB_ROUTE_SHIFT
);
667 * tb_plug_events_active() - enable/disable plug events on a switch
669 * Also configures a sane plug_events_delay of 255ms.
671 * Return: Returns 0 on success or an error code on failure.
673 static int tb_plug_events_active(struct tb_switch
*sw
, bool active
)
678 if (!sw
->config
.enabled
)
681 sw
->config
.plug_events_delay
= 0xff;
682 res
= tb_sw_write(sw
, ((u32
*) &sw
->config
) + 4, TB_CFG_SWITCH
, 4, 1);
686 res
= tb_sw_read(sw
, &data
, TB_CFG_SWITCH
, sw
->cap_plug_events
+ 1, 1);
691 data
= data
& 0xFFFFFF83;
692 switch (sw
->config
.device_id
) {
693 case PCI_DEVICE_ID_INTEL_LIGHT_RIDGE
:
694 case PCI_DEVICE_ID_INTEL_EAGLE_RIDGE
:
695 case PCI_DEVICE_ID_INTEL_PORT_RIDGE
:
703 return tb_sw_write(sw
, &data
, TB_CFG_SWITCH
,
704 sw
->cap_plug_events
+ 1, 1);
707 static ssize_t
authorized_show(struct device
*dev
,
708 struct device_attribute
*attr
,
711 struct tb_switch
*sw
= tb_to_switch(dev
);
713 return sprintf(buf
, "%u\n", sw
->authorized
);
716 static int tb_switch_set_authorized(struct tb_switch
*sw
, unsigned int val
)
720 if (mutex_lock_interruptible(&switch_lock
))
727 * Make sure there is no PCIe rescan ongoing when a new PCIe
728 * tunnel is created. Otherwise the PCIe rescan code might find
729 * the new tunnel too early.
731 pci_lock_rescan_remove();
732 pm_runtime_get_sync(&sw
->dev
);
738 ret
= tb_domain_approve_switch_key(sw
->tb
, sw
);
740 ret
= tb_domain_approve_switch(sw
->tb
, sw
);
743 /* Challenge switch */
746 ret
= tb_domain_challenge_switch_key(sw
->tb
, sw
);
753 pm_runtime_mark_last_busy(&sw
->dev
);
754 pm_runtime_put_autosuspend(&sw
->dev
);
755 pci_unlock_rescan_remove();
758 sw
->authorized
= val
;
759 /* Notify status change to the userspace */
760 kobject_uevent(&sw
->dev
.kobj
, KOBJ_CHANGE
);
764 mutex_unlock(&switch_lock
);
768 static ssize_t
authorized_store(struct device
*dev
,
769 struct device_attribute
*attr
,
770 const char *buf
, size_t count
)
772 struct tb_switch
*sw
= tb_to_switch(dev
);
776 ret
= kstrtouint(buf
, 0, &val
);
782 ret
= tb_switch_set_authorized(sw
, val
);
784 return ret
? ret
: count
;
786 static DEVICE_ATTR_RW(authorized
);
788 static ssize_t
boot_show(struct device
*dev
, struct device_attribute
*attr
,
791 struct tb_switch
*sw
= tb_to_switch(dev
);
793 return sprintf(buf
, "%u\n", sw
->boot
);
795 static DEVICE_ATTR_RO(boot
);
797 static ssize_t
device_show(struct device
*dev
, struct device_attribute
*attr
,
800 struct tb_switch
*sw
= tb_to_switch(dev
);
802 return sprintf(buf
, "%#x\n", sw
->device
);
804 static DEVICE_ATTR_RO(device
);
807 device_name_show(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
809 struct tb_switch
*sw
= tb_to_switch(dev
);
811 return sprintf(buf
, "%s\n", sw
->device_name
? sw
->device_name
: "");
813 static DEVICE_ATTR_RO(device_name
);
815 static ssize_t
key_show(struct device
*dev
, struct device_attribute
*attr
,
818 struct tb_switch
*sw
= tb_to_switch(dev
);
821 if (mutex_lock_interruptible(&switch_lock
))
825 ret
= sprintf(buf
, "%*phN\n", TB_SWITCH_KEY_SIZE
, sw
->key
);
827 ret
= sprintf(buf
, "\n");
829 mutex_unlock(&switch_lock
);
833 static ssize_t
key_store(struct device
*dev
, struct device_attribute
*attr
,
834 const char *buf
, size_t count
)
836 struct tb_switch
*sw
= tb_to_switch(dev
);
837 u8 key
[TB_SWITCH_KEY_SIZE
];
841 if (!strcmp(buf
, "\n"))
843 else if (hex2bin(key
, buf
, sizeof(key
)))
846 if (mutex_lock_interruptible(&switch_lock
))
849 if (sw
->authorized
) {
856 sw
->key
= kmemdup(key
, sizeof(key
), GFP_KERNEL
);
862 mutex_unlock(&switch_lock
);
865 static DEVICE_ATTR(key
, 0600, key_show
, key_store
);
867 static void nvm_authenticate_start(struct tb_switch
*sw
)
869 struct pci_dev
*root_port
;
872 * During host router NVM upgrade we should not allow root port to
873 * go into D3cold because some root ports cannot trigger PME
874 * itself. To be on the safe side keep the root port in D0 during
875 * the whole upgrade process.
877 root_port
= pci_find_pcie_root_port(sw
->tb
->nhi
->pdev
);
879 pm_runtime_get_noresume(&root_port
->dev
);
882 static void nvm_authenticate_complete(struct tb_switch
*sw
)
884 struct pci_dev
*root_port
;
886 root_port
= pci_find_pcie_root_port(sw
->tb
->nhi
->pdev
);
888 pm_runtime_put(&root_port
->dev
);
891 static ssize_t
nvm_authenticate_show(struct device
*dev
,
892 struct device_attribute
*attr
, char *buf
)
894 struct tb_switch
*sw
= tb_to_switch(dev
);
897 nvm_get_auth_status(sw
, &status
);
898 return sprintf(buf
, "%#x\n", status
);
901 static ssize_t
nvm_authenticate_store(struct device
*dev
,
902 struct device_attribute
*attr
, const char *buf
, size_t count
)
904 struct tb_switch
*sw
= tb_to_switch(dev
);
908 if (mutex_lock_interruptible(&switch_lock
))
911 /* If NVMem devices are not yet added */
917 ret
= kstrtobool(buf
, &val
);
921 /* Always clear the authentication status */
922 nvm_clear_auth_status(sw
);
930 pm_runtime_get_sync(&sw
->dev
);
931 ret
= nvm_validate_and_write(sw
);
933 pm_runtime_mark_last_busy(&sw
->dev
);
934 pm_runtime_put_autosuspend(&sw
->dev
);
938 sw
->nvm
->authenticating
= true;
942 * Keep root port from suspending as long as the
943 * NVM upgrade process is running.
945 nvm_authenticate_start(sw
);
946 ret
= nvm_authenticate_host(sw
);
948 nvm_authenticate_complete(sw
);
950 ret
= nvm_authenticate_device(sw
);
952 pm_runtime_mark_last_busy(&sw
->dev
);
953 pm_runtime_put_autosuspend(&sw
->dev
);
957 mutex_unlock(&switch_lock
);
963 static DEVICE_ATTR_RW(nvm_authenticate
);
965 static ssize_t
nvm_version_show(struct device
*dev
,
966 struct device_attribute
*attr
, char *buf
)
968 struct tb_switch
*sw
= tb_to_switch(dev
);
971 if (mutex_lock_interruptible(&switch_lock
))
979 ret
= sprintf(buf
, "%x.%x\n", sw
->nvm
->major
, sw
->nvm
->minor
);
981 mutex_unlock(&switch_lock
);
985 static DEVICE_ATTR_RO(nvm_version
);
987 static ssize_t
vendor_show(struct device
*dev
, struct device_attribute
*attr
,
990 struct tb_switch
*sw
= tb_to_switch(dev
);
992 return sprintf(buf
, "%#x\n", sw
->vendor
);
994 static DEVICE_ATTR_RO(vendor
);
997 vendor_name_show(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
999 struct tb_switch
*sw
= tb_to_switch(dev
);
1001 return sprintf(buf
, "%s\n", sw
->vendor_name
? sw
->vendor_name
: "");
1003 static DEVICE_ATTR_RO(vendor_name
);
1005 static ssize_t
unique_id_show(struct device
*dev
, struct device_attribute
*attr
,
1008 struct tb_switch
*sw
= tb_to_switch(dev
);
1010 return sprintf(buf
, "%pUb\n", sw
->uuid
);
1012 static DEVICE_ATTR_RO(unique_id
);
1014 static struct attribute
*switch_attrs
[] = {
1015 &dev_attr_authorized
.attr
,
1016 &dev_attr_boot
.attr
,
1017 &dev_attr_device
.attr
,
1018 &dev_attr_device_name
.attr
,
1020 &dev_attr_nvm_authenticate
.attr
,
1021 &dev_attr_nvm_version
.attr
,
1022 &dev_attr_vendor
.attr
,
1023 &dev_attr_vendor_name
.attr
,
1024 &dev_attr_unique_id
.attr
,
1028 static umode_t
switch_attr_is_visible(struct kobject
*kobj
,
1029 struct attribute
*attr
, int n
)
1031 struct device
*dev
= container_of(kobj
, struct device
, kobj
);
1032 struct tb_switch
*sw
= tb_to_switch(dev
);
1034 if (attr
== &dev_attr_key
.attr
) {
1036 sw
->tb
->security_level
== TB_SECURITY_SECURE
&&
1037 sw
->security_level
== TB_SECURITY_SECURE
)
1040 } else if (attr
== &dev_attr_nvm_authenticate
.attr
||
1041 attr
== &dev_attr_nvm_version
.attr
) {
1045 } else if (attr
== &dev_attr_boot
.attr
) {
1051 return sw
->safe_mode
? 0 : attr
->mode
;
1054 static struct attribute_group switch_group
= {
1055 .is_visible
= switch_attr_is_visible
,
1056 .attrs
= switch_attrs
,
1059 static const struct attribute_group
*switch_groups
[] = {
1064 static void tb_switch_release(struct device
*dev
)
1066 struct tb_switch
*sw
= tb_to_switch(dev
);
1068 dma_port_free(sw
->dma_port
);
1071 kfree(sw
->device_name
);
1072 kfree(sw
->vendor_name
);
1080 * Currently only need to provide the callbacks. Everything else is handled
1081 * in the connection manager.
1083 static int __maybe_unused
tb_switch_runtime_suspend(struct device
*dev
)
1088 static int __maybe_unused
tb_switch_runtime_resume(struct device
*dev
)
1093 static const struct dev_pm_ops tb_switch_pm_ops
= {
1094 SET_RUNTIME_PM_OPS(tb_switch_runtime_suspend
, tb_switch_runtime_resume
,
1098 struct device_type tb_switch_type
= {
1099 .name
= "thunderbolt_device",
1100 .release
= tb_switch_release
,
1101 .pm
= &tb_switch_pm_ops
,
1104 static int tb_switch_get_generation(struct tb_switch
*sw
)
1106 switch (sw
->config
.device_id
) {
1107 case PCI_DEVICE_ID_INTEL_LIGHT_RIDGE
:
1108 case PCI_DEVICE_ID_INTEL_EAGLE_RIDGE
:
1109 case PCI_DEVICE_ID_INTEL_LIGHT_PEAK
:
1110 case PCI_DEVICE_ID_INTEL_CACTUS_RIDGE_2C
:
1111 case PCI_DEVICE_ID_INTEL_CACTUS_RIDGE_4C
:
1112 case PCI_DEVICE_ID_INTEL_PORT_RIDGE
:
1113 case PCI_DEVICE_ID_INTEL_REDWOOD_RIDGE_2C_BRIDGE
:
1114 case PCI_DEVICE_ID_INTEL_REDWOOD_RIDGE_4C_BRIDGE
:
1117 case PCI_DEVICE_ID_INTEL_WIN_RIDGE_2C_BRIDGE
:
1118 case PCI_DEVICE_ID_INTEL_FALCON_RIDGE_2C_BRIDGE
:
1119 case PCI_DEVICE_ID_INTEL_FALCON_RIDGE_4C_BRIDGE
:
1122 case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_LP_BRIDGE
:
1123 case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_2C_BRIDGE
:
1124 case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_4C_BRIDGE
:
1125 case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_C_2C_BRIDGE
:
1126 case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_C_4C_BRIDGE
:
1127 case PCI_DEVICE_ID_INTEL_TITAN_RIDGE_2C_BRIDGE
:
1128 case PCI_DEVICE_ID_INTEL_TITAN_RIDGE_4C_BRIDGE
:
1129 case PCI_DEVICE_ID_INTEL_TITAN_RIDGE_DD_BRIDGE
:
1134 * For unknown switches assume generation to be 1 to be
1137 tb_sw_warn(sw
, "unsupported switch device id %#x\n",
1138 sw
->config
.device_id
);
1144 * tb_switch_alloc() - allocate a switch
1145 * @tb: Pointer to the owning domain
1146 * @parent: Parent device for this switch
1147 * @route: Route string for this switch
1149 * Allocates and initializes a switch. Will not upload configuration to
1150 * the switch. For that you need to call tb_switch_configure()
1151 * separately. The returned switch should be released by calling
1154 * Return: Pointer to the allocated switch or %NULL in case of failure
1156 struct tb_switch
*tb_switch_alloc(struct tb
*tb
, struct device
*parent
,
1161 struct tb_switch
*sw
;
1162 int upstream_port
= tb_cfg_get_upstream_port(tb
->ctl
, route
);
1163 if (upstream_port
< 0)
1166 sw
= kzalloc(sizeof(*sw
), GFP_KERNEL
);
1171 if (tb_cfg_read(tb
->ctl
, &sw
->config
, route
, 0, TB_CFG_SWITCH
, 0, 5))
1172 goto err_free_sw_ports
;
1174 tb_info(tb
, "current switch config:\n");
1175 tb_dump_switch(tb
, &sw
->config
);
1177 /* configure switch */
1178 sw
->config
.upstream_port_number
= upstream_port
;
1179 sw
->config
.depth
= tb_route_length(route
);
1180 sw
->config
.route_lo
= route
;
1181 sw
->config
.route_hi
= route
>> 32;
1182 sw
->config
.enabled
= 0;
1184 /* initialize ports */
1185 sw
->ports
= kcalloc(sw
->config
.max_port_number
+ 1, sizeof(*sw
->ports
),
1188 goto err_free_sw_ports
;
1190 for (i
= 0; i
<= sw
->config
.max_port_number
; i
++) {
1191 /* minimum setup for tb_find_cap and tb_drom_read to work */
1192 sw
->ports
[i
].sw
= sw
;
1193 sw
->ports
[i
].port
= i
;
1196 sw
->generation
= tb_switch_get_generation(sw
);
1198 cap
= tb_switch_find_vse_cap(sw
, TB_VSE_CAP_PLUG_EVENTS
);
1200 tb_sw_warn(sw
, "cannot find TB_VSE_CAP_PLUG_EVENTS aborting\n");
1201 goto err_free_sw_ports
;
1203 sw
->cap_plug_events
= cap
;
1205 /* Root switch is always authorized */
1207 sw
->authorized
= true;
1209 device_initialize(&sw
->dev
);
1210 sw
->dev
.parent
= parent
;
1211 sw
->dev
.bus
= &tb_bus_type
;
1212 sw
->dev
.type
= &tb_switch_type
;
1213 sw
->dev
.groups
= switch_groups
;
1214 dev_set_name(&sw
->dev
, "%u-%llx", tb
->index
, tb_route(sw
));
1226 * tb_switch_alloc_safe_mode() - allocate a switch that is in safe mode
1227 * @tb: Pointer to the owning domain
1228 * @parent: Parent device for this switch
1229 * @route: Route string for this switch
1231 * This creates a switch in safe mode. This means the switch pretty much
1232 * lacks all capabilities except DMA configuration port before it is
1233 * flashed with a valid NVM firmware.
1235 * The returned switch must be released by calling tb_switch_put().
1237 * Return: Pointer to the allocated switch or %NULL in case of failure
1240 tb_switch_alloc_safe_mode(struct tb
*tb
, struct device
*parent
, u64 route
)
1242 struct tb_switch
*sw
;
1244 sw
= kzalloc(sizeof(*sw
), GFP_KERNEL
);
1249 sw
->config
.depth
= tb_route_length(route
);
1250 sw
->config
.route_hi
= upper_32_bits(route
);
1251 sw
->config
.route_lo
= lower_32_bits(route
);
1252 sw
->safe_mode
= true;
1254 device_initialize(&sw
->dev
);
1255 sw
->dev
.parent
= parent
;
1256 sw
->dev
.bus
= &tb_bus_type
;
1257 sw
->dev
.type
= &tb_switch_type
;
1258 sw
->dev
.groups
= switch_groups
;
1259 dev_set_name(&sw
->dev
, "%u-%llx", tb
->index
, tb_route(sw
));
1265 * tb_switch_configure() - Uploads configuration to the switch
1266 * @sw: Switch to configure
1268 * Call this function before the switch is added to the system. It will
1269 * upload configuration to the switch and makes it available for the
1270 * connection manager to use.
1272 * Return: %0 in case of success and negative errno in case of failure
1274 int tb_switch_configure(struct tb_switch
*sw
)
1276 struct tb
*tb
= sw
->tb
;
1280 route
= tb_route(sw
);
1282 "initializing Switch at %#llx (depth: %d, up port: %d)\n",
1283 route
, tb_route_length(route
), sw
->config
.upstream_port_number
);
1285 if (sw
->config
.vendor_id
!= PCI_VENDOR_ID_INTEL
)
1286 tb_sw_warn(sw
, "unknown switch vendor id %#x\n",
1287 sw
->config
.vendor_id
);
1289 sw
->config
.enabled
= 1;
1291 /* upload configuration */
1292 ret
= tb_sw_write(sw
, 1 + (u32
*)&sw
->config
, TB_CFG_SWITCH
, 1, 3);
1296 return tb_plug_events_active(sw
, true);
1299 static void tb_switch_set_uuid(struct tb_switch
*sw
)
1308 * The newer controllers include fused UUID as part of link
1309 * controller specific registers
1311 cap
= tb_switch_find_vse_cap(sw
, TB_VSE_CAP_LINK_CONTROLLER
);
1313 tb_sw_read(sw
, uuid
, TB_CFG_SWITCH
, cap
+ 3, 4);
1316 * ICM generates UUID based on UID and fills the upper
1317 * two words with ones. This is not strictly following
1318 * UUID format but we want to be compatible with it so
1319 * we do the same here.
1321 uuid
[0] = sw
->uid
& 0xffffffff;
1322 uuid
[1] = (sw
->uid
>> 32) & 0xffffffff;
1323 uuid
[2] = 0xffffffff;
1324 uuid
[3] = 0xffffffff;
1327 sw
->uuid
= kmemdup(uuid
, sizeof(uuid
), GFP_KERNEL
);
1330 static int tb_switch_add_dma_port(struct tb_switch
*sw
)
1335 switch (sw
->generation
) {
1340 /* Only root switch can be upgraded */
1347 * DMA port is the only thing available when the switch
1355 if (sw
->no_nvm_upgrade
)
1358 sw
->dma_port
= dma_port_alloc(sw
);
1363 * Check status of the previous flash authentication. If there
1364 * is one we need to power cycle the switch in any case to make
1365 * it functional again.
1367 ret
= dma_port_flash_update_auth_status(sw
->dma_port
, &status
);
1371 /* Now we can allow root port to suspend again */
1373 nvm_authenticate_complete(sw
);
1376 tb_sw_info(sw
, "switch flash authentication failed\n");
1377 tb_switch_set_uuid(sw
);
1378 nvm_set_auth_status(sw
, status
);
1381 tb_sw_info(sw
, "power cycling the switch now\n");
1382 dma_port_power_cycle(sw
->dma_port
);
1385 * We return error here which causes the switch adding failure.
1386 * It should appear back after power cycle is complete.
1392 * tb_switch_add() - Add a switch to the domain
1393 * @sw: Switch to add
1395 * This is the last step in adding switch to the domain. It will read
1396 * identification information from DROM and initializes ports so that
1397 * they can be used to connect other switches. The switch will be
1398 * exposed to the userspace when this function successfully returns. To
1399 * remove and release the switch, call tb_switch_remove().
1401 * Return: %0 in case of success and negative errno in case of failure
1403 int tb_switch_add(struct tb_switch
*sw
)
1408 * Initialize DMA control port now before we read DROM. Recent
1409 * host controllers have more complete DROM on NVM that includes
1410 * vendor and model identification strings which we then expose
1411 * to the userspace. NVM can be accessed through DMA
1412 * configuration based mailbox.
1414 ret
= tb_switch_add_dma_port(sw
);
1418 if (!sw
->safe_mode
) {
1420 ret
= tb_drom_read(sw
);
1422 tb_sw_warn(sw
, "tb_eeprom_read_rom failed\n");
1425 tb_sw_info(sw
, "uid: %#llx\n", sw
->uid
);
1427 tb_switch_set_uuid(sw
);
1429 for (i
= 0; i
<= sw
->config
.max_port_number
; i
++) {
1430 if (sw
->ports
[i
].disabled
) {
1431 tb_port_info(&sw
->ports
[i
], "disabled by eeprom\n");
1434 ret
= tb_init_port(&sw
->ports
[i
]);
1440 ret
= device_add(&sw
->dev
);
1444 ret
= tb_switch_nvm_add(sw
);
1446 device_del(&sw
->dev
);
1450 pm_runtime_set_active(&sw
->dev
);
1452 pm_runtime_set_autosuspend_delay(&sw
->dev
, TB_AUTOSUSPEND_DELAY
);
1453 pm_runtime_use_autosuspend(&sw
->dev
);
1454 pm_runtime_mark_last_busy(&sw
->dev
);
1455 pm_runtime_enable(&sw
->dev
);
1456 pm_request_autosuspend(&sw
->dev
);
1463 * tb_switch_remove() - Remove and release a switch
1464 * @sw: Switch to remove
1466 * This will remove the switch from the domain and release it after last
1467 * reference count drops to zero. If there are switches connected below
1468 * this switch, they will be removed as well.
1470 void tb_switch_remove(struct tb_switch
*sw
)
1475 pm_runtime_get_sync(&sw
->dev
);
1476 pm_runtime_disable(&sw
->dev
);
1479 /* port 0 is the switch itself and never has a remote */
1480 for (i
= 1; i
<= sw
->config
.max_port_number
; i
++) {
1481 if (tb_is_upstream_port(&sw
->ports
[i
]))
1483 if (sw
->ports
[i
].remote
)
1484 tb_switch_remove(sw
->ports
[i
].remote
->sw
);
1485 sw
->ports
[i
].remote
= NULL
;
1486 if (sw
->ports
[i
].xdomain
)
1487 tb_xdomain_remove(sw
->ports
[i
].xdomain
);
1488 sw
->ports
[i
].xdomain
= NULL
;
1491 if (!sw
->is_unplugged
)
1492 tb_plug_events_active(sw
, false);
1494 tb_switch_nvm_remove(sw
);
1495 device_unregister(&sw
->dev
);
1499 * tb_sw_set_unplugged() - set is_unplugged on switch and downstream switches
1501 void tb_sw_set_unplugged(struct tb_switch
*sw
)
1504 if (sw
== sw
->tb
->root_switch
) {
1505 tb_sw_WARN(sw
, "cannot unplug root switch\n");
1508 if (sw
->is_unplugged
) {
1509 tb_sw_WARN(sw
, "is_unplugged already set\n");
1512 sw
->is_unplugged
= true;
1513 for (i
= 0; i
<= sw
->config
.max_port_number
; i
++) {
1514 if (!tb_is_upstream_port(&sw
->ports
[i
]) && sw
->ports
[i
].remote
)
1515 tb_sw_set_unplugged(sw
->ports
[i
].remote
->sw
);
1519 int tb_switch_resume(struct tb_switch
*sw
)
1522 tb_sw_info(sw
, "resuming switch\n");
1525 * Check for UID of the connected switches except for root
1526 * switch which we assume cannot be removed.
1531 err
= tb_drom_read_uid_only(sw
, &uid
);
1533 tb_sw_warn(sw
, "uid read failed\n");
1536 if (sw
->uid
!= uid
) {
1538 "changed while suspended (uid %#llx -> %#llx)\n",
1544 /* upload configuration */
1545 err
= tb_sw_write(sw
, 1 + (u32
*) &sw
->config
, TB_CFG_SWITCH
, 1, 3);
1549 err
= tb_plug_events_active(sw
, true);
1553 /* check for surviving downstream switches */
1554 for (i
= 1; i
<= sw
->config
.max_port_number
; i
++) {
1555 struct tb_port
*port
= &sw
->ports
[i
];
1556 if (tb_is_upstream_port(port
))
1560 if (tb_wait_for_port(port
, true) <= 0
1561 || tb_switch_resume(port
->remote
->sw
)) {
1563 "lost during suspend, disconnecting\n");
1564 tb_sw_set_unplugged(port
->remote
->sw
);
1570 void tb_switch_suspend(struct tb_switch
*sw
)
1573 err
= tb_plug_events_active(sw
, false);
1577 for (i
= 1; i
<= sw
->config
.max_port_number
; i
++) {
1578 if (!tb_is_upstream_port(&sw
->ports
[i
]) && sw
->ports
[i
].remote
)
1579 tb_switch_suspend(sw
->ports
[i
].remote
->sw
);
1582 * TODO: invoke tb_cfg_prepare_to_sleep here? does not seem to have any
1587 struct tb_sw_lookup
{
1595 static int tb_switch_match(struct device
*dev
, void *data
)
1597 struct tb_switch
*sw
= tb_to_switch(dev
);
1598 struct tb_sw_lookup
*lookup
= data
;
1602 if (sw
->tb
!= lookup
->tb
)
1606 return !memcmp(sw
->uuid
, lookup
->uuid
, sizeof(*lookup
->uuid
));
1608 if (lookup
->route
) {
1609 return sw
->config
.route_lo
== lower_32_bits(lookup
->route
) &&
1610 sw
->config
.route_hi
== upper_32_bits(lookup
->route
);
1613 /* Root switch is matched only by depth */
1617 return sw
->link
== lookup
->link
&& sw
->depth
== lookup
->depth
;
1621 * tb_switch_find_by_link_depth() - Find switch by link and depth
1622 * @tb: Domain the switch belongs
1623 * @link: Link number the switch is connected
1624 * @depth: Depth of the switch in link
1626 * Returned switch has reference count increased so the caller needs to
1627 * call tb_switch_put() when done with the switch.
1629 struct tb_switch
*tb_switch_find_by_link_depth(struct tb
*tb
, u8 link
, u8 depth
)
1631 struct tb_sw_lookup lookup
;
1634 memset(&lookup
, 0, sizeof(lookup
));
1637 lookup
.depth
= depth
;
1639 dev
= bus_find_device(&tb_bus_type
, NULL
, &lookup
, tb_switch_match
);
1641 return tb_to_switch(dev
);
1647 * tb_switch_find_by_uuid() - Find switch by UUID
1648 * @tb: Domain the switch belongs
1649 * @uuid: UUID to look for
1651 * Returned switch has reference count increased so the caller needs to
1652 * call tb_switch_put() when done with the switch.
1654 struct tb_switch
*tb_switch_find_by_uuid(struct tb
*tb
, const uuid_t
*uuid
)
1656 struct tb_sw_lookup lookup
;
1659 memset(&lookup
, 0, sizeof(lookup
));
1663 dev
= bus_find_device(&tb_bus_type
, NULL
, &lookup
, tb_switch_match
);
1665 return tb_to_switch(dev
);
1671 * tb_switch_find_by_route() - Find switch by route string
1672 * @tb: Domain the switch belongs
1673 * @route: Route string to look for
1675 * Returned switch has reference count increased so the caller needs to
1676 * call tb_switch_put() when done with the switch.
1678 struct tb_switch
*tb_switch_find_by_route(struct tb
*tb
, u64 route
)
1680 struct tb_sw_lookup lookup
;
1684 return tb_switch_get(tb
->root_switch
);
1686 memset(&lookup
, 0, sizeof(lookup
));
1688 lookup
.route
= route
;
1690 dev
= bus_find_device(&tb_bus_type
, NULL
, &lookup
, tb_switch_match
);
1692 return tb_to_switch(dev
);
1697 void tb_switch_exit(void)
1699 ida_destroy(&nvm_ida
);