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/sched/signal.h>
14 #include <linux/sizes.h>
15 #include <linux/slab.h>
16 #include <linux/vmalloc.h>
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
;
241 pm_runtime_get_sync(&sw
->dev
);
242 ret
= dma_port_flash_read(sw
->dma_port
, offset
, val
, bytes
);
243 pm_runtime_mark_last_busy(&sw
->dev
);
244 pm_runtime_put_autosuspend(&sw
->dev
);
249 static int tb_switch_nvm_write(void *priv
, unsigned int offset
, void *val
,
252 struct tb_switch
*sw
= priv
;
255 if (!mutex_trylock(&sw
->tb
->lock
))
256 return restart_syscall();
259 * Since writing the NVM image might require some special steps,
260 * for example when CSS headers are written, we cache the image
261 * locally here and handle the special cases when the user asks
262 * us to authenticate the image.
265 sw
->nvm
->buf
= vmalloc(NVM_MAX_SIZE
);
272 sw
->nvm
->buf_data_size
= offset
+ bytes
;
273 memcpy(sw
->nvm
->buf
+ offset
, val
, bytes
);
276 mutex_unlock(&sw
->tb
->lock
);
281 static struct nvmem_device
*register_nvmem(struct tb_switch
*sw
, int id
,
282 size_t size
, bool active
)
284 struct nvmem_config config
;
286 memset(&config
, 0, sizeof(config
));
289 config
.name
= "nvm_active";
290 config
.reg_read
= tb_switch_nvm_read
;
291 config
.read_only
= true;
293 config
.name
= "nvm_non_active";
294 config
.reg_write
= tb_switch_nvm_write
;
295 config
.root_only
= true;
300 config
.word_size
= 4;
302 config
.dev
= &sw
->dev
;
303 config
.owner
= THIS_MODULE
;
306 return nvmem_register(&config
);
309 static int tb_switch_nvm_add(struct tb_switch
*sw
)
311 struct nvmem_device
*nvm_dev
;
312 struct tb_switch_nvm
*nvm
;
319 nvm
= kzalloc(sizeof(*nvm
), GFP_KERNEL
);
323 nvm
->id
= ida_simple_get(&nvm_ida
, 0, 0, GFP_KERNEL
);
326 * If the switch is in safe-mode the only accessible portion of
327 * the NVM is the non-active one where userspace is expected to
328 * write new functional NVM.
330 if (!sw
->safe_mode
) {
331 u32 nvm_size
, hdr_size
;
333 ret
= dma_port_flash_read(sw
->dma_port
, NVM_FLASH_SIZE
, &val
,
338 hdr_size
= sw
->generation
< 3 ? SZ_8K
: SZ_16K
;
339 nvm_size
= (SZ_1M
<< (val
& 7)) / 8;
340 nvm_size
= (nvm_size
- hdr_size
) / 2;
342 ret
= dma_port_flash_read(sw
->dma_port
, NVM_VERSION
, &val
,
347 nvm
->major
= val
>> 16;
348 nvm
->minor
= val
>> 8;
350 nvm_dev
= register_nvmem(sw
, nvm
->id
, nvm_size
, true);
351 if (IS_ERR(nvm_dev
)) {
352 ret
= PTR_ERR(nvm_dev
);
355 nvm
->active
= nvm_dev
;
358 nvm_dev
= register_nvmem(sw
, nvm
->id
, NVM_MAX_SIZE
, false);
359 if (IS_ERR(nvm_dev
)) {
360 ret
= PTR_ERR(nvm_dev
);
363 nvm
->non_active
= nvm_dev
;
370 nvmem_unregister(nvm
->active
);
372 ida_simple_remove(&nvm_ida
, nvm
->id
);
378 static void tb_switch_nvm_remove(struct tb_switch
*sw
)
380 struct tb_switch_nvm
*nvm
;
388 /* Remove authentication status in case the switch is unplugged */
389 if (!nvm
->authenticating
)
390 nvm_clear_auth_status(sw
);
392 nvmem_unregister(nvm
->non_active
);
394 nvmem_unregister(nvm
->active
);
395 ida_simple_remove(&nvm_ida
, nvm
->id
);
400 /* port utility functions */
402 static const char *tb_port_type(struct tb_regs_port_header
*port
)
404 switch (port
->type
>> 16) {
406 switch ((u8
) port
->type
) {
431 static void tb_dump_port(struct tb
*tb
, struct tb_regs_port_header
*port
)
434 " Port %d: %x:%x (Revision: %d, TB Version: %d, Type: %s (%#x))\n",
435 port
->port_number
, port
->vendor_id
, port
->device_id
,
436 port
->revision
, port
->thunderbolt_version
, tb_port_type(port
),
438 tb_dbg(tb
, " Max hop id (in/out): %d/%d\n",
439 port
->max_in_hop_id
, port
->max_out_hop_id
);
440 tb_dbg(tb
, " Max counters: %d\n", port
->max_counters
);
441 tb_dbg(tb
, " NFC Credits: %#x\n", port
->nfc_credits
);
445 * tb_port_state() - get connectedness state of a port
447 * The port must have a TB_CAP_PHY (i.e. it should be a real port).
449 * Return: Returns an enum tb_port_state on success or an error code on failure.
451 static int tb_port_state(struct tb_port
*port
)
453 struct tb_cap_phy phy
;
455 if (port
->cap_phy
== 0) {
456 tb_port_WARN(port
, "does not have a PHY\n");
459 res
= tb_port_read(port
, &phy
, TB_CFG_PORT
, port
->cap_phy
, 2);
466 * tb_wait_for_port() - wait for a port to become ready
468 * Wait up to 1 second for a port to reach state TB_PORT_UP. If
469 * wait_if_unplugged is set then we also wait if the port is in state
470 * TB_PORT_UNPLUGGED (it takes a while for the device to be registered after
471 * switch resume). Otherwise we only wait if a device is registered but the link
472 * has not yet been established.
474 * Return: Returns an error code on failure. Returns 0 if the port is not
475 * connected or failed to reach state TB_PORT_UP within one second. Returns 1
476 * if the port is connected and in state TB_PORT_UP.
478 int tb_wait_for_port(struct tb_port
*port
, bool wait_if_unplugged
)
482 if (!port
->cap_phy
) {
483 tb_port_WARN(port
, "does not have PHY\n");
486 if (tb_is_upstream_port(port
)) {
487 tb_port_WARN(port
, "is the upstream port\n");
492 state
= tb_port_state(port
);
495 if (state
== TB_PORT_DISABLED
) {
496 tb_port_info(port
, "is disabled (state: 0)\n");
499 if (state
== TB_PORT_UNPLUGGED
) {
500 if (wait_if_unplugged
) {
501 /* used during resume */
503 "is unplugged (state: 7), retrying...\n");
507 tb_port_info(port
, "is unplugged (state: 7)\n");
510 if (state
== TB_PORT_UP
) {
512 "is connected, link is up (state: 2)\n");
517 * After plug-in the state is TB_PORT_CONNECTING. Give it some
521 "is connected, link is not up (state: %d), retrying...\n",
526 "failed to reach state TB_PORT_UP. Ignoring port...\n");
531 * tb_port_add_nfc_credits() - add/remove non flow controlled credits to port
533 * Change the number of NFC credits allocated to @port by @credits. To remove
534 * NFC credits pass a negative amount of credits.
536 * Return: Returns 0 on success or an error code on failure.
538 int tb_port_add_nfc_credits(struct tb_port
*port
, int credits
)
543 "adding %#x NFC credits (%#x -> %#x)",
545 port
->config
.nfc_credits
,
546 port
->config
.nfc_credits
+ credits
);
547 port
->config
.nfc_credits
+= credits
;
548 return tb_port_write(port
, &port
->config
.nfc_credits
,
553 * tb_port_clear_counter() - clear a counter in TB_CFG_COUNTER
555 * Return: Returns 0 on success or an error code on failure.
557 int tb_port_clear_counter(struct tb_port
*port
, int counter
)
559 u32 zero
[3] = { 0, 0, 0 };
560 tb_port_info(port
, "clearing counter %d\n", counter
);
561 return tb_port_write(port
, zero
, TB_CFG_COUNTERS
, 3 * counter
, 3);
565 * tb_init_port() - initialize a port
567 * This is a helper method for tb_switch_alloc. Does not check or initialize
568 * any downstream switches.
570 * Return: Returns 0 on success or an error code on failure.
572 static int tb_init_port(struct tb_port
*port
)
577 res
= tb_port_read(port
, &port
->config
, TB_CFG_PORT
, 0, 8);
581 /* Port 0 is the switch itself and has no PHY. */
582 if (port
->config
.type
== TB_TYPE_PORT
&& port
->port
!= 0) {
583 cap
= tb_port_find_cap(port
, TB_PORT_CAP_PHY
);
588 tb_port_WARN(port
, "non switch port without a PHY\n");
589 } else if (port
->port
!= 0) {
590 cap
= tb_port_find_cap(port
, TB_PORT_CAP_ADAP
);
592 port
->cap_adap
= cap
;
595 tb_dump_port(port
->sw
->tb
, &port
->config
);
597 /* TODO: Read dual link port, DP port and more from EEPROM. */
603 * tb_pci_port_enable() - Enable PCIe adapter port
604 * @port: PCIe port to enable
605 * @enable: Enable/disable the PCIe adapter
607 int tb_pci_port_enable(struct tb_port
*port
, bool enable
)
609 u32 word
= enable
? TB_PCI_EN
: 0x0;
612 return tb_port_write(port
, &word
, TB_CFG_PORT
, port
->cap_adap
, 1);
615 /* switch utility functions */
617 static void tb_dump_switch(struct tb
*tb
, struct tb_regs_switch_header
*sw
)
619 tb_dbg(tb
, " Switch: %x:%x (Revision: %d, TB Version: %d)\n",
620 sw
->vendor_id
, sw
->device_id
, sw
->revision
,
621 sw
->thunderbolt_version
);
622 tb_dbg(tb
, " Max Port Number: %d\n", sw
->max_port_number
);
623 tb_dbg(tb
, " Config:\n");
625 " Upstream Port Number: %d Depth: %d Route String: %#llx Enabled: %d, PlugEventsDelay: %dms\n",
626 sw
->upstream_port_number
, sw
->depth
,
627 (((u64
) sw
->route_hi
) << 32) | sw
->route_lo
,
628 sw
->enabled
, sw
->plug_events_delay
);
629 tb_dbg(tb
, " unknown1: %#x unknown4: %#x\n",
630 sw
->__unknown1
, sw
->__unknown4
);
634 * reset_switch() - reconfigure route, enable and send TB_CFG_PKG_RESET
636 * Return: Returns 0 on success or an error code on failure.
638 int tb_switch_reset(struct tb
*tb
, u64 route
)
640 struct tb_cfg_result res
;
641 struct tb_regs_switch_header header
= {
642 header
.route_hi
= route
>> 32,
643 header
.route_lo
= route
,
644 header
.enabled
= true,
646 tb_dbg(tb
, "resetting switch at %llx\n", route
);
647 res
.err
= tb_cfg_write(tb
->ctl
, ((u32
*) &header
) + 2, route
,
651 res
= tb_cfg_reset(tb
->ctl
, route
, TB_CFG_DEFAULT_TIMEOUT
);
658 * tb_plug_events_active() - enable/disable plug events on a switch
660 * Also configures a sane plug_events_delay of 255ms.
662 * Return: Returns 0 on success or an error code on failure.
664 static int tb_plug_events_active(struct tb_switch
*sw
, bool active
)
669 if (!sw
->config
.enabled
)
672 sw
->config
.plug_events_delay
= 0xff;
673 res
= tb_sw_write(sw
, ((u32
*) &sw
->config
) + 4, TB_CFG_SWITCH
, 4, 1);
677 res
= tb_sw_read(sw
, &data
, TB_CFG_SWITCH
, sw
->cap_plug_events
+ 1, 1);
682 data
= data
& 0xFFFFFF83;
683 switch (sw
->config
.device_id
) {
684 case PCI_DEVICE_ID_INTEL_LIGHT_RIDGE
:
685 case PCI_DEVICE_ID_INTEL_EAGLE_RIDGE
:
686 case PCI_DEVICE_ID_INTEL_PORT_RIDGE
:
694 return tb_sw_write(sw
, &data
, TB_CFG_SWITCH
,
695 sw
->cap_plug_events
+ 1, 1);
698 static ssize_t
authorized_show(struct device
*dev
,
699 struct device_attribute
*attr
,
702 struct tb_switch
*sw
= tb_to_switch(dev
);
704 return sprintf(buf
, "%u\n", sw
->authorized
);
707 static int tb_switch_set_authorized(struct tb_switch
*sw
, unsigned int val
)
711 if (!mutex_trylock(&sw
->tb
->lock
))
712 return restart_syscall();
718 * Make sure there is no PCIe rescan ongoing when a new PCIe
719 * tunnel is created. Otherwise the PCIe rescan code might find
720 * the new tunnel too early.
722 pci_lock_rescan_remove();
723 pm_runtime_get_sync(&sw
->dev
);
729 ret
= tb_domain_approve_switch_key(sw
->tb
, sw
);
731 ret
= tb_domain_approve_switch(sw
->tb
, sw
);
734 /* Challenge switch */
737 ret
= tb_domain_challenge_switch_key(sw
->tb
, sw
);
744 pm_runtime_mark_last_busy(&sw
->dev
);
745 pm_runtime_put_autosuspend(&sw
->dev
);
746 pci_unlock_rescan_remove();
749 sw
->authorized
= val
;
750 /* Notify status change to the userspace */
751 kobject_uevent(&sw
->dev
.kobj
, KOBJ_CHANGE
);
755 mutex_unlock(&sw
->tb
->lock
);
759 static ssize_t
authorized_store(struct device
*dev
,
760 struct device_attribute
*attr
,
761 const char *buf
, size_t count
)
763 struct tb_switch
*sw
= tb_to_switch(dev
);
767 ret
= kstrtouint(buf
, 0, &val
);
773 ret
= tb_switch_set_authorized(sw
, val
);
775 return ret
? ret
: count
;
777 static DEVICE_ATTR_RW(authorized
);
779 static ssize_t
boot_show(struct device
*dev
, struct device_attribute
*attr
,
782 struct tb_switch
*sw
= tb_to_switch(dev
);
784 return sprintf(buf
, "%u\n", sw
->boot
);
786 static DEVICE_ATTR_RO(boot
);
788 static ssize_t
device_show(struct device
*dev
, struct device_attribute
*attr
,
791 struct tb_switch
*sw
= tb_to_switch(dev
);
793 return sprintf(buf
, "%#x\n", sw
->device
);
795 static DEVICE_ATTR_RO(device
);
798 device_name_show(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
800 struct tb_switch
*sw
= tb_to_switch(dev
);
802 return sprintf(buf
, "%s\n", sw
->device_name
? sw
->device_name
: "");
804 static DEVICE_ATTR_RO(device_name
);
806 static ssize_t
key_show(struct device
*dev
, struct device_attribute
*attr
,
809 struct tb_switch
*sw
= tb_to_switch(dev
);
812 if (!mutex_trylock(&sw
->tb
->lock
))
813 return restart_syscall();
816 ret
= sprintf(buf
, "%*phN\n", TB_SWITCH_KEY_SIZE
, sw
->key
);
818 ret
= sprintf(buf
, "\n");
820 mutex_unlock(&sw
->tb
->lock
);
824 static ssize_t
key_store(struct device
*dev
, struct device_attribute
*attr
,
825 const char *buf
, size_t count
)
827 struct tb_switch
*sw
= tb_to_switch(dev
);
828 u8 key
[TB_SWITCH_KEY_SIZE
];
832 if (!strcmp(buf
, "\n"))
834 else if (hex2bin(key
, buf
, sizeof(key
)))
837 if (!mutex_trylock(&sw
->tb
->lock
))
838 return restart_syscall();
840 if (sw
->authorized
) {
847 sw
->key
= kmemdup(key
, sizeof(key
), GFP_KERNEL
);
853 mutex_unlock(&sw
->tb
->lock
);
856 static DEVICE_ATTR(key
, 0600, key_show
, key_store
);
858 static void nvm_authenticate_start(struct tb_switch
*sw
)
860 struct pci_dev
*root_port
;
863 * During host router NVM upgrade we should not allow root port to
864 * go into D3cold because some root ports cannot trigger PME
865 * itself. To be on the safe side keep the root port in D0 during
866 * the whole upgrade process.
868 root_port
= pci_find_pcie_root_port(sw
->tb
->nhi
->pdev
);
870 pm_runtime_get_noresume(&root_port
->dev
);
873 static void nvm_authenticate_complete(struct tb_switch
*sw
)
875 struct pci_dev
*root_port
;
877 root_port
= pci_find_pcie_root_port(sw
->tb
->nhi
->pdev
);
879 pm_runtime_put(&root_port
->dev
);
882 static ssize_t
nvm_authenticate_show(struct device
*dev
,
883 struct device_attribute
*attr
, char *buf
)
885 struct tb_switch
*sw
= tb_to_switch(dev
);
888 nvm_get_auth_status(sw
, &status
);
889 return sprintf(buf
, "%#x\n", status
);
892 static ssize_t
nvm_authenticate_store(struct device
*dev
,
893 struct device_attribute
*attr
, const char *buf
, size_t count
)
895 struct tb_switch
*sw
= tb_to_switch(dev
);
899 if (!mutex_trylock(&sw
->tb
->lock
))
900 return restart_syscall();
902 /* If NVMem devices are not yet added */
908 ret
= kstrtobool(buf
, &val
);
912 /* Always clear the authentication status */
913 nvm_clear_auth_status(sw
);
921 pm_runtime_get_sync(&sw
->dev
);
922 ret
= nvm_validate_and_write(sw
);
924 pm_runtime_mark_last_busy(&sw
->dev
);
925 pm_runtime_put_autosuspend(&sw
->dev
);
929 sw
->nvm
->authenticating
= true;
933 * Keep root port from suspending as long as the
934 * NVM upgrade process is running.
936 nvm_authenticate_start(sw
);
937 ret
= nvm_authenticate_host(sw
);
939 nvm_authenticate_complete(sw
);
941 ret
= nvm_authenticate_device(sw
);
943 pm_runtime_mark_last_busy(&sw
->dev
);
944 pm_runtime_put_autosuspend(&sw
->dev
);
948 mutex_unlock(&sw
->tb
->lock
);
954 static DEVICE_ATTR_RW(nvm_authenticate
);
956 static ssize_t
nvm_version_show(struct device
*dev
,
957 struct device_attribute
*attr
, char *buf
)
959 struct tb_switch
*sw
= tb_to_switch(dev
);
962 if (!mutex_trylock(&sw
->tb
->lock
))
963 return restart_syscall();
970 ret
= sprintf(buf
, "%x.%x\n", sw
->nvm
->major
, sw
->nvm
->minor
);
972 mutex_unlock(&sw
->tb
->lock
);
976 static DEVICE_ATTR_RO(nvm_version
);
978 static ssize_t
vendor_show(struct device
*dev
, struct device_attribute
*attr
,
981 struct tb_switch
*sw
= tb_to_switch(dev
);
983 return sprintf(buf
, "%#x\n", sw
->vendor
);
985 static DEVICE_ATTR_RO(vendor
);
988 vendor_name_show(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
990 struct tb_switch
*sw
= tb_to_switch(dev
);
992 return sprintf(buf
, "%s\n", sw
->vendor_name
? sw
->vendor_name
: "");
994 static DEVICE_ATTR_RO(vendor_name
);
996 static ssize_t
unique_id_show(struct device
*dev
, struct device_attribute
*attr
,
999 struct tb_switch
*sw
= tb_to_switch(dev
);
1001 return sprintf(buf
, "%pUb\n", sw
->uuid
);
1003 static DEVICE_ATTR_RO(unique_id
);
1005 static struct attribute
*switch_attrs
[] = {
1006 &dev_attr_authorized
.attr
,
1007 &dev_attr_boot
.attr
,
1008 &dev_attr_device
.attr
,
1009 &dev_attr_device_name
.attr
,
1011 &dev_attr_nvm_authenticate
.attr
,
1012 &dev_attr_nvm_version
.attr
,
1013 &dev_attr_vendor
.attr
,
1014 &dev_attr_vendor_name
.attr
,
1015 &dev_attr_unique_id
.attr
,
1019 static umode_t
switch_attr_is_visible(struct kobject
*kobj
,
1020 struct attribute
*attr
, int n
)
1022 struct device
*dev
= container_of(kobj
, struct device
, kobj
);
1023 struct tb_switch
*sw
= tb_to_switch(dev
);
1025 if (attr
== &dev_attr_key
.attr
) {
1027 sw
->tb
->security_level
== TB_SECURITY_SECURE
&&
1028 sw
->security_level
== TB_SECURITY_SECURE
)
1031 } else if (attr
== &dev_attr_nvm_authenticate
.attr
||
1032 attr
== &dev_attr_nvm_version
.attr
) {
1036 } else if (attr
== &dev_attr_boot
.attr
) {
1042 return sw
->safe_mode
? 0 : attr
->mode
;
1045 static struct attribute_group switch_group
= {
1046 .is_visible
= switch_attr_is_visible
,
1047 .attrs
= switch_attrs
,
1050 static const struct attribute_group
*switch_groups
[] = {
1055 static void tb_switch_release(struct device
*dev
)
1057 struct tb_switch
*sw
= tb_to_switch(dev
);
1059 dma_port_free(sw
->dma_port
);
1062 kfree(sw
->device_name
);
1063 kfree(sw
->vendor_name
);
1071 * Currently only need to provide the callbacks. Everything else is handled
1072 * in the connection manager.
1074 static int __maybe_unused
tb_switch_runtime_suspend(struct device
*dev
)
1079 static int __maybe_unused
tb_switch_runtime_resume(struct device
*dev
)
1084 static const struct dev_pm_ops tb_switch_pm_ops
= {
1085 SET_RUNTIME_PM_OPS(tb_switch_runtime_suspend
, tb_switch_runtime_resume
,
1089 struct device_type tb_switch_type
= {
1090 .name
= "thunderbolt_device",
1091 .release
= tb_switch_release
,
1092 .pm
= &tb_switch_pm_ops
,
1095 static int tb_switch_get_generation(struct tb_switch
*sw
)
1097 switch (sw
->config
.device_id
) {
1098 case PCI_DEVICE_ID_INTEL_LIGHT_RIDGE
:
1099 case PCI_DEVICE_ID_INTEL_EAGLE_RIDGE
:
1100 case PCI_DEVICE_ID_INTEL_LIGHT_PEAK
:
1101 case PCI_DEVICE_ID_INTEL_CACTUS_RIDGE_2C
:
1102 case PCI_DEVICE_ID_INTEL_CACTUS_RIDGE_4C
:
1103 case PCI_DEVICE_ID_INTEL_PORT_RIDGE
:
1104 case PCI_DEVICE_ID_INTEL_REDWOOD_RIDGE_2C_BRIDGE
:
1105 case PCI_DEVICE_ID_INTEL_REDWOOD_RIDGE_4C_BRIDGE
:
1108 case PCI_DEVICE_ID_INTEL_WIN_RIDGE_2C_BRIDGE
:
1109 case PCI_DEVICE_ID_INTEL_FALCON_RIDGE_2C_BRIDGE
:
1110 case PCI_DEVICE_ID_INTEL_FALCON_RIDGE_4C_BRIDGE
:
1113 case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_LP_BRIDGE
:
1114 case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_2C_BRIDGE
:
1115 case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_4C_BRIDGE
:
1116 case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_C_2C_BRIDGE
:
1117 case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_C_4C_BRIDGE
:
1118 case PCI_DEVICE_ID_INTEL_TITAN_RIDGE_2C_BRIDGE
:
1119 case PCI_DEVICE_ID_INTEL_TITAN_RIDGE_4C_BRIDGE
:
1120 case PCI_DEVICE_ID_INTEL_TITAN_RIDGE_DD_BRIDGE
:
1125 * For unknown switches assume generation to be 1 to be
1128 tb_sw_warn(sw
, "unsupported switch device id %#x\n",
1129 sw
->config
.device_id
);
1135 * tb_switch_alloc() - allocate a switch
1136 * @tb: Pointer to the owning domain
1137 * @parent: Parent device for this switch
1138 * @route: Route string for this switch
1140 * Allocates and initializes a switch. Will not upload configuration to
1141 * the switch. For that you need to call tb_switch_configure()
1142 * separately. The returned switch should be released by calling
1145 * Return: Pointer to the allocated switch or %NULL in case of failure
1147 struct tb_switch
*tb_switch_alloc(struct tb
*tb
, struct device
*parent
,
1150 struct tb_switch
*sw
;
1154 /* Make sure we do not exceed maximum topology limit */
1155 depth
= tb_route_length(route
);
1156 if (depth
> TB_SWITCH_MAX_DEPTH
)
1159 upstream_port
= tb_cfg_get_upstream_port(tb
->ctl
, route
);
1160 if (upstream_port
< 0)
1163 sw
= kzalloc(sizeof(*sw
), GFP_KERNEL
);
1168 if (tb_cfg_read(tb
->ctl
, &sw
->config
, route
, 0, TB_CFG_SWITCH
, 0, 5))
1169 goto err_free_sw_ports
;
1171 tb_dbg(tb
, "current switch config:\n");
1172 tb_dump_switch(tb
, &sw
->config
);
1174 /* configure switch */
1175 sw
->config
.upstream_port_number
= upstream_port
;
1176 sw
->config
.depth
= depth
;
1177 sw
->config
.route_hi
= upper_32_bits(route
);
1178 sw
->config
.route_lo
= lower_32_bits(route
);
1179 sw
->config
.enabled
= 0;
1181 /* initialize ports */
1182 sw
->ports
= kcalloc(sw
->config
.max_port_number
+ 1, sizeof(*sw
->ports
),
1185 goto err_free_sw_ports
;
1187 for (i
= 0; i
<= sw
->config
.max_port_number
; i
++) {
1188 /* minimum setup for tb_find_cap and tb_drom_read to work */
1189 sw
->ports
[i
].sw
= sw
;
1190 sw
->ports
[i
].port
= i
;
1193 sw
->generation
= tb_switch_get_generation(sw
);
1195 cap
= tb_switch_find_vse_cap(sw
, TB_VSE_CAP_PLUG_EVENTS
);
1197 tb_sw_warn(sw
, "cannot find TB_VSE_CAP_PLUG_EVENTS aborting\n");
1198 goto err_free_sw_ports
;
1200 sw
->cap_plug_events
= cap
;
1202 cap
= tb_switch_find_vse_cap(sw
, TB_VSE_CAP_LINK_CONTROLLER
);
1206 /* Root switch is always authorized */
1208 sw
->authorized
= true;
1210 device_initialize(&sw
->dev
);
1211 sw
->dev
.parent
= parent
;
1212 sw
->dev
.bus
= &tb_bus_type
;
1213 sw
->dev
.type
= &tb_switch_type
;
1214 sw
->dev
.groups
= switch_groups
;
1215 dev_set_name(&sw
->dev
, "%u-%llx", tb
->index
, tb_route(sw
));
1227 * tb_switch_alloc_safe_mode() - allocate a switch that is in safe mode
1228 * @tb: Pointer to the owning domain
1229 * @parent: Parent device for this switch
1230 * @route: Route string for this switch
1232 * This creates a switch in safe mode. This means the switch pretty much
1233 * lacks all capabilities except DMA configuration port before it is
1234 * flashed with a valid NVM firmware.
1236 * The returned switch must be released by calling tb_switch_put().
1238 * Return: Pointer to the allocated switch or %NULL in case of failure
1241 tb_switch_alloc_safe_mode(struct tb
*tb
, struct device
*parent
, u64 route
)
1243 struct tb_switch
*sw
;
1245 sw
= kzalloc(sizeof(*sw
), GFP_KERNEL
);
1250 sw
->config
.depth
= tb_route_length(route
);
1251 sw
->config
.route_hi
= upper_32_bits(route
);
1252 sw
->config
.route_lo
= lower_32_bits(route
);
1253 sw
->safe_mode
= true;
1255 device_initialize(&sw
->dev
);
1256 sw
->dev
.parent
= parent
;
1257 sw
->dev
.bus
= &tb_bus_type
;
1258 sw
->dev
.type
= &tb_switch_type
;
1259 sw
->dev
.groups
= switch_groups
;
1260 dev_set_name(&sw
->dev
, "%u-%llx", tb
->index
, tb_route(sw
));
1266 * tb_switch_configure() - Uploads configuration to the switch
1267 * @sw: Switch to configure
1269 * Call this function before the switch is added to the system. It will
1270 * upload configuration to the switch and makes it available for the
1271 * connection manager to use.
1273 * Return: %0 in case of success and negative errno in case of failure
1275 int tb_switch_configure(struct tb_switch
*sw
)
1277 struct tb
*tb
= sw
->tb
;
1281 route
= tb_route(sw
);
1282 tb_dbg(tb
, "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 ret
= tb_lc_configure_link(sw
);
1300 return tb_plug_events_active(sw
, true);
1303 static int tb_switch_set_uuid(struct tb_switch
*sw
)
1312 * The newer controllers include fused UUID as part of link
1313 * controller specific registers
1315 ret
= tb_lc_read_uuid(sw
, uuid
);
1318 * ICM generates UUID based on UID and fills the upper
1319 * two words with ones. This is not strictly following
1320 * UUID format but we want to be compatible with it so
1321 * we do the same here.
1323 uuid
[0] = sw
->uid
& 0xffffffff;
1324 uuid
[1] = (sw
->uid
>> 32) & 0xffffffff;
1325 uuid
[2] = 0xffffffff;
1326 uuid
[3] = 0xffffffff;
1329 sw
->uuid
= kmemdup(uuid
, sizeof(uuid
), GFP_KERNEL
);
1335 static int tb_switch_add_dma_port(struct tb_switch
*sw
)
1340 switch (sw
->generation
) {
1345 /* Only root switch can be upgraded */
1352 * DMA port is the only thing available when the switch
1360 if (sw
->no_nvm_upgrade
)
1363 sw
->dma_port
= dma_port_alloc(sw
);
1368 * Check status of the previous flash authentication. If there
1369 * is one we need to power cycle the switch in any case to make
1370 * it functional again.
1372 ret
= dma_port_flash_update_auth_status(sw
->dma_port
, &status
);
1376 /* Now we can allow root port to suspend again */
1378 nvm_authenticate_complete(sw
);
1381 tb_sw_info(sw
, "switch flash authentication failed\n");
1382 ret
= tb_switch_set_uuid(sw
);
1385 nvm_set_auth_status(sw
, status
);
1388 tb_sw_info(sw
, "power cycling the switch now\n");
1389 dma_port_power_cycle(sw
->dma_port
);
1392 * We return error here which causes the switch adding failure.
1393 * It should appear back after power cycle is complete.
1399 * tb_switch_add() - Add a switch to the domain
1400 * @sw: Switch to add
1402 * This is the last step in adding switch to the domain. It will read
1403 * identification information from DROM and initializes ports so that
1404 * they can be used to connect other switches. The switch will be
1405 * exposed to the userspace when this function successfully returns. To
1406 * remove and release the switch, call tb_switch_remove().
1408 * Return: %0 in case of success and negative errno in case of failure
1410 int tb_switch_add(struct tb_switch
*sw
)
1415 * Initialize DMA control port now before we read DROM. Recent
1416 * host controllers have more complete DROM on NVM that includes
1417 * vendor and model identification strings which we then expose
1418 * to the userspace. NVM can be accessed through DMA
1419 * configuration based mailbox.
1421 ret
= tb_switch_add_dma_port(sw
);
1425 if (!sw
->safe_mode
) {
1427 ret
= tb_drom_read(sw
);
1429 tb_sw_warn(sw
, "tb_eeprom_read_rom failed\n");
1432 tb_sw_dbg(sw
, "uid: %#llx\n", sw
->uid
);
1434 ret
= tb_switch_set_uuid(sw
);
1438 for (i
= 0; i
<= sw
->config
.max_port_number
; i
++) {
1439 if (sw
->ports
[i
].disabled
) {
1440 tb_port_dbg(&sw
->ports
[i
], "disabled by eeprom\n");
1443 ret
= tb_init_port(&sw
->ports
[i
]);
1449 ret
= device_add(&sw
->dev
);
1454 dev_info(&sw
->dev
, "new device found, vendor=%#x device=%#x\n",
1455 sw
->vendor
, sw
->device
);
1456 if (sw
->vendor_name
&& sw
->device_name
)
1457 dev_info(&sw
->dev
, "%s %s\n", sw
->vendor_name
,
1461 ret
= tb_switch_nvm_add(sw
);
1463 device_del(&sw
->dev
);
1467 pm_runtime_set_active(&sw
->dev
);
1469 pm_runtime_set_autosuspend_delay(&sw
->dev
, TB_AUTOSUSPEND_DELAY
);
1470 pm_runtime_use_autosuspend(&sw
->dev
);
1471 pm_runtime_mark_last_busy(&sw
->dev
);
1472 pm_runtime_enable(&sw
->dev
);
1473 pm_request_autosuspend(&sw
->dev
);
1480 * tb_switch_remove() - Remove and release a switch
1481 * @sw: Switch to remove
1483 * This will remove the switch from the domain and release it after last
1484 * reference count drops to zero. If there are switches connected below
1485 * this switch, they will be removed as well.
1487 void tb_switch_remove(struct tb_switch
*sw
)
1492 pm_runtime_get_sync(&sw
->dev
);
1493 pm_runtime_disable(&sw
->dev
);
1496 /* port 0 is the switch itself and never has a remote */
1497 for (i
= 1; i
<= sw
->config
.max_port_number
; i
++) {
1498 if (tb_is_upstream_port(&sw
->ports
[i
]))
1500 if (sw
->ports
[i
].remote
)
1501 tb_switch_remove(sw
->ports
[i
].remote
->sw
);
1502 sw
->ports
[i
].remote
= NULL
;
1503 if (sw
->ports
[i
].xdomain
)
1504 tb_xdomain_remove(sw
->ports
[i
].xdomain
);
1505 sw
->ports
[i
].xdomain
= NULL
;
1508 if (!sw
->is_unplugged
)
1509 tb_plug_events_active(sw
, false);
1510 tb_lc_unconfigure_link(sw
);
1512 tb_switch_nvm_remove(sw
);
1515 dev_info(&sw
->dev
, "device disconnected\n");
1516 device_unregister(&sw
->dev
);
1520 * tb_sw_set_unplugged() - set is_unplugged on switch and downstream switches
1522 void tb_sw_set_unplugged(struct tb_switch
*sw
)
1525 if (sw
== sw
->tb
->root_switch
) {
1526 tb_sw_WARN(sw
, "cannot unplug root switch\n");
1529 if (sw
->is_unplugged
) {
1530 tb_sw_WARN(sw
, "is_unplugged already set\n");
1533 sw
->is_unplugged
= true;
1534 for (i
= 0; i
<= sw
->config
.max_port_number
; i
++) {
1535 if (!tb_is_upstream_port(&sw
->ports
[i
]) && sw
->ports
[i
].remote
)
1536 tb_sw_set_unplugged(sw
->ports
[i
].remote
->sw
);
1540 int tb_switch_resume(struct tb_switch
*sw
)
1543 tb_sw_dbg(sw
, "resuming switch\n");
1546 * Check for UID of the connected switches except for root
1547 * switch which we assume cannot be removed.
1552 err
= tb_drom_read_uid_only(sw
, &uid
);
1554 tb_sw_warn(sw
, "uid read failed\n");
1557 if (sw
->uid
!= uid
) {
1559 "changed while suspended (uid %#llx -> %#llx)\n",
1565 /* upload configuration */
1566 err
= tb_sw_write(sw
, 1 + (u32
*) &sw
->config
, TB_CFG_SWITCH
, 1, 3);
1570 err
= tb_lc_configure_link(sw
);
1574 err
= tb_plug_events_active(sw
, true);
1578 /* check for surviving downstream switches */
1579 for (i
= 1; i
<= sw
->config
.max_port_number
; i
++) {
1580 struct tb_port
*port
= &sw
->ports
[i
];
1581 if (tb_is_upstream_port(port
))
1585 if (tb_wait_for_port(port
, true) <= 0
1586 || tb_switch_resume(port
->remote
->sw
)) {
1588 "lost during suspend, disconnecting\n");
1589 tb_sw_set_unplugged(port
->remote
->sw
);
1595 void tb_switch_suspend(struct tb_switch
*sw
)
1598 err
= tb_plug_events_active(sw
, false);
1602 for (i
= 1; i
<= sw
->config
.max_port_number
; i
++) {
1603 if (!tb_is_upstream_port(&sw
->ports
[i
]) && sw
->ports
[i
].remote
)
1604 tb_switch_suspend(sw
->ports
[i
].remote
->sw
);
1607 tb_lc_set_sleep(sw
);
1610 struct tb_sw_lookup
{
1618 static int tb_switch_match(struct device
*dev
, void *data
)
1620 struct tb_switch
*sw
= tb_to_switch(dev
);
1621 struct tb_sw_lookup
*lookup
= data
;
1625 if (sw
->tb
!= lookup
->tb
)
1629 return !memcmp(sw
->uuid
, lookup
->uuid
, sizeof(*lookup
->uuid
));
1631 if (lookup
->route
) {
1632 return sw
->config
.route_lo
== lower_32_bits(lookup
->route
) &&
1633 sw
->config
.route_hi
== upper_32_bits(lookup
->route
);
1636 /* Root switch is matched only by depth */
1640 return sw
->link
== lookup
->link
&& sw
->depth
== lookup
->depth
;
1644 * tb_switch_find_by_link_depth() - Find switch by link and depth
1645 * @tb: Domain the switch belongs
1646 * @link: Link number the switch is connected
1647 * @depth: Depth of the switch in link
1649 * Returned switch has reference count increased so the caller needs to
1650 * call tb_switch_put() when done with the switch.
1652 struct tb_switch
*tb_switch_find_by_link_depth(struct tb
*tb
, u8 link
, u8 depth
)
1654 struct tb_sw_lookup lookup
;
1657 memset(&lookup
, 0, sizeof(lookup
));
1660 lookup
.depth
= depth
;
1662 dev
= bus_find_device(&tb_bus_type
, NULL
, &lookup
, tb_switch_match
);
1664 return tb_to_switch(dev
);
1670 * tb_switch_find_by_uuid() - Find switch by UUID
1671 * @tb: Domain the switch belongs
1672 * @uuid: UUID to look for
1674 * Returned switch has reference count increased so the caller needs to
1675 * call tb_switch_put() when done with the switch.
1677 struct tb_switch
*tb_switch_find_by_uuid(struct tb
*tb
, const uuid_t
*uuid
)
1679 struct tb_sw_lookup lookup
;
1682 memset(&lookup
, 0, sizeof(lookup
));
1686 dev
= bus_find_device(&tb_bus_type
, NULL
, &lookup
, tb_switch_match
);
1688 return tb_to_switch(dev
);
1694 * tb_switch_find_by_route() - Find switch by route string
1695 * @tb: Domain the switch belongs
1696 * @route: Route string to look for
1698 * Returned switch has reference count increased so the caller needs to
1699 * call tb_switch_put() when done with the switch.
1701 struct tb_switch
*tb_switch_find_by_route(struct tb
*tb
, u64 route
)
1703 struct tb_sw_lookup lookup
;
1707 return tb_switch_get(tb
->root_switch
);
1709 memset(&lookup
, 0, sizeof(lookup
));
1711 lookup
.route
= route
;
1713 dev
= bus_find_device(&tb_bus_type
, NULL
, &lookup
, tb_switch_match
);
1715 return tb_to_switch(dev
);
1720 void tb_switch_exit(void)
1722 ida_destroy(&nvm_ida
);