2 * Thunderbolt Cactus Ridge driver - switch/port utility functions
4 * Copyright (c) 2014 Andreas Noever <andreas.noever@gmail.com>
7 #include <linux/delay.h>
9 #include <linux/nvmem-provider.h>
10 #include <linux/sizes.h>
11 #include <linux/slab.h>
12 #include <linux/vmalloc.h>
16 /* Switch authorization from userspace is serialized by this lock */
17 static DEFINE_MUTEX(switch_lock
);
19 /* Switch NVM support */
21 #define NVM_DEVID 0x05
22 #define NVM_VERSION 0x08
24 #define NVM_FLASH_SIZE 0x45
26 #define NVM_MIN_SIZE SZ_32K
27 #define NVM_MAX_SIZE SZ_512K
29 static DEFINE_IDA(nvm_ida
);
31 struct nvm_auth_status
{
32 struct list_head list
;
38 * Hold NVM authentication failure status per switch This information
39 * needs to stay around even when the switch gets power cycled so we
42 static LIST_HEAD(nvm_auth_status_cache
);
43 static DEFINE_MUTEX(nvm_auth_status_lock
);
45 static struct nvm_auth_status
*__nvm_get_auth_status(const struct tb_switch
*sw
)
47 struct nvm_auth_status
*st
;
49 list_for_each_entry(st
, &nvm_auth_status_cache
, list
) {
50 if (uuid_equal(&st
->uuid
, sw
->uuid
))
57 static void nvm_get_auth_status(const struct tb_switch
*sw
, u32
*status
)
59 struct nvm_auth_status
*st
;
61 mutex_lock(&nvm_auth_status_lock
);
62 st
= __nvm_get_auth_status(sw
);
63 mutex_unlock(&nvm_auth_status_lock
);
65 *status
= st
? st
->status
: 0;
68 static void nvm_set_auth_status(const struct tb_switch
*sw
, u32 status
)
70 struct nvm_auth_status
*st
;
72 if (WARN_ON(!sw
->uuid
))
75 mutex_lock(&nvm_auth_status_lock
);
76 st
= __nvm_get_auth_status(sw
);
79 st
= kzalloc(sizeof(*st
), GFP_KERNEL
);
83 memcpy(&st
->uuid
, sw
->uuid
, sizeof(st
->uuid
));
84 INIT_LIST_HEAD(&st
->list
);
85 list_add_tail(&st
->list
, &nvm_auth_status_cache
);
90 mutex_unlock(&nvm_auth_status_lock
);
93 static void nvm_clear_auth_status(const struct tb_switch
*sw
)
95 struct nvm_auth_status
*st
;
97 mutex_lock(&nvm_auth_status_lock
);
98 st
= __nvm_get_auth_status(sw
);
103 mutex_unlock(&nvm_auth_status_lock
);
106 static int nvm_validate_and_write(struct tb_switch
*sw
)
108 unsigned int image_size
, hdr_size
;
109 const u8
*buf
= sw
->nvm
->buf
;
116 image_size
= sw
->nvm
->buf_data_size
;
117 if (image_size
< NVM_MIN_SIZE
|| image_size
> NVM_MAX_SIZE
)
121 * FARB pointer must point inside the image and must at least
122 * contain parts of the digital section we will be reading here.
124 hdr_size
= (*(u32
*)buf
) & 0xffffff;
125 if (hdr_size
+ NVM_DEVID
+ 2 >= image_size
)
128 /* Digital section start should be aligned to 4k page */
129 if (!IS_ALIGNED(hdr_size
, SZ_4K
))
133 * Read digital section size and check that it also fits inside
136 ds_size
= *(u16
*)(buf
+ hdr_size
);
137 if (ds_size
>= image_size
)
140 if (!sw
->safe_mode
) {
144 * Make sure the device ID in the image matches the one
145 * we read from the switch config space.
147 device_id
= *(u16
*)(buf
+ hdr_size
+ NVM_DEVID
);
148 if (device_id
!= sw
->config
.device_id
)
151 if (sw
->generation
< 3) {
152 /* Write CSS headers first */
153 ret
= dma_port_flash_write(sw
->dma_port
,
154 DMA_PORT_CSS_ADDRESS
, buf
+ NVM_CSS
,
155 DMA_PORT_CSS_MAX_SIZE
);
160 /* Skip headers in the image */
162 image_size
-= hdr_size
;
165 return dma_port_flash_write(sw
->dma_port
, 0, buf
, image_size
);
168 static int nvm_authenticate_host(struct tb_switch
*sw
)
173 * Root switch NVM upgrade requires that we disconnect the
174 * existing PCIe paths first (in case it is not in safe mode
177 if (!sw
->safe_mode
) {
178 ret
= tb_domain_disconnect_pcie_paths(sw
->tb
);
182 * The host controller goes away pretty soon after this if
183 * everything goes well so getting timeout is expected.
185 ret
= dma_port_flash_update_auth(sw
->dma_port
);
186 return ret
== -ETIMEDOUT
? 0 : ret
;
190 * From safe mode we can get out by just power cycling the
193 dma_port_power_cycle(sw
->dma_port
);
197 static int nvm_authenticate_device(struct tb_switch
*sw
)
199 int ret
, retries
= 10;
201 ret
= dma_port_flash_update_auth(sw
->dma_port
);
202 if (ret
&& ret
!= -ETIMEDOUT
)
206 * Poll here for the authentication status. It takes some time
207 * for the device to respond (we get timeout for a while). Once
208 * we get response the device needs to be power cycled in order
209 * to the new NVM to be taken into use.
214 ret
= dma_port_flash_update_auth_status(sw
->dma_port
, &status
);
215 if (ret
< 0 && ret
!= -ETIMEDOUT
)
219 tb_sw_warn(sw
, "failed to authenticate NVM\n");
220 nvm_set_auth_status(sw
, status
);
223 tb_sw_info(sw
, "power cycling the switch now\n");
224 dma_port_power_cycle(sw
->dma_port
);
234 static int tb_switch_nvm_read(void *priv
, unsigned int offset
, void *val
,
237 struct tb_switch
*sw
= priv
;
239 return dma_port_flash_read(sw
->dma_port
, offset
, val
, bytes
);
242 static int tb_switch_nvm_write(void *priv
, unsigned int offset
, void *val
,
245 struct tb_switch
*sw
= priv
;
248 if (mutex_lock_interruptible(&switch_lock
))
252 * Since writing the NVM image might require some special steps,
253 * for example when CSS headers are written, we cache the image
254 * locally here and handle the special cases when the user asks
255 * us to authenticate the image.
258 sw
->nvm
->buf
= vmalloc(NVM_MAX_SIZE
);
265 sw
->nvm
->buf_data_size
= offset
+ bytes
;
266 memcpy(sw
->nvm
->buf
+ offset
, val
, bytes
);
269 mutex_unlock(&switch_lock
);
274 static struct nvmem_device
*register_nvmem(struct tb_switch
*sw
, int id
,
275 size_t size
, bool active
)
277 struct nvmem_config config
;
279 memset(&config
, 0, sizeof(config
));
282 config
.name
= "nvm_active";
283 config
.reg_read
= tb_switch_nvm_read
;
284 config
.read_only
= true;
286 config
.name
= "nvm_non_active";
287 config
.reg_write
= tb_switch_nvm_write
;
288 config
.root_only
= true;
293 config
.word_size
= 4;
295 config
.dev
= &sw
->dev
;
296 config
.owner
= THIS_MODULE
;
299 return nvmem_register(&config
);
302 static int tb_switch_nvm_add(struct tb_switch
*sw
)
304 struct nvmem_device
*nvm_dev
;
305 struct tb_switch_nvm
*nvm
;
312 nvm
= kzalloc(sizeof(*nvm
), GFP_KERNEL
);
316 nvm
->id
= ida_simple_get(&nvm_ida
, 0, 0, GFP_KERNEL
);
319 * If the switch is in safe-mode the only accessible portion of
320 * the NVM is the non-active one where userspace is expected to
321 * write new functional NVM.
323 if (!sw
->safe_mode
) {
324 u32 nvm_size
, hdr_size
;
326 ret
= dma_port_flash_read(sw
->dma_port
, NVM_FLASH_SIZE
, &val
,
331 hdr_size
= sw
->generation
< 3 ? SZ_8K
: SZ_16K
;
332 nvm_size
= (SZ_1M
<< (val
& 7)) / 8;
333 nvm_size
= (nvm_size
- hdr_size
) / 2;
335 ret
= dma_port_flash_read(sw
->dma_port
, NVM_VERSION
, &val
,
340 nvm
->major
= val
>> 16;
341 nvm
->minor
= val
>> 8;
343 nvm_dev
= register_nvmem(sw
, nvm
->id
, nvm_size
, true);
344 if (IS_ERR(nvm_dev
)) {
345 ret
= PTR_ERR(nvm_dev
);
348 nvm
->active
= nvm_dev
;
351 nvm_dev
= register_nvmem(sw
, nvm
->id
, NVM_MAX_SIZE
, false);
352 if (IS_ERR(nvm_dev
)) {
353 ret
= PTR_ERR(nvm_dev
);
356 nvm
->non_active
= nvm_dev
;
358 mutex_lock(&switch_lock
);
360 mutex_unlock(&switch_lock
);
366 nvmem_unregister(nvm
->active
);
368 ida_simple_remove(&nvm_ida
, nvm
->id
);
374 static void tb_switch_nvm_remove(struct tb_switch
*sw
)
376 struct tb_switch_nvm
*nvm
;
378 mutex_lock(&switch_lock
);
381 mutex_unlock(&switch_lock
);
386 /* Remove authentication status in case the switch is unplugged */
387 if (!nvm
->authenticating
)
388 nvm_clear_auth_status(sw
);
390 nvmem_unregister(nvm
->non_active
);
392 nvmem_unregister(nvm
->active
);
393 ida_simple_remove(&nvm_ida
, nvm
->id
);
398 /* port utility functions */
400 static const char *tb_port_type(struct tb_regs_port_header
*port
)
402 switch (port
->type
>> 16) {
404 switch ((u8
) port
->type
) {
429 static void tb_dump_port(struct tb
*tb
, struct tb_regs_port_header
*port
)
432 " Port %d: %x:%x (Revision: %d, TB Version: %d, Type: %s (%#x))\n",
433 port
->port_number
, port
->vendor_id
, port
->device_id
,
434 port
->revision
, port
->thunderbolt_version
, tb_port_type(port
),
436 tb_info(tb
, " Max hop id (in/out): %d/%d\n",
437 port
->max_in_hop_id
, port
->max_out_hop_id
);
438 tb_info(tb
, " Max counters: %d\n", port
->max_counters
);
439 tb_info(tb
, " NFC Credits: %#x\n", port
->nfc_credits
);
443 * tb_port_state() - get connectedness state of a port
445 * The port must have a TB_CAP_PHY (i.e. it should be a real port).
447 * Return: Returns an enum tb_port_state on success or an error code on failure.
449 static int tb_port_state(struct tb_port
*port
)
451 struct tb_cap_phy phy
;
453 if (port
->cap_phy
== 0) {
454 tb_port_WARN(port
, "does not have a PHY\n");
457 res
= tb_port_read(port
, &phy
, TB_CFG_PORT
, port
->cap_phy
, 2);
464 * tb_wait_for_port() - wait for a port to become ready
466 * Wait up to 1 second for a port to reach state TB_PORT_UP. If
467 * wait_if_unplugged is set then we also wait if the port is in state
468 * TB_PORT_UNPLUGGED (it takes a while for the device to be registered after
469 * switch resume). Otherwise we only wait if a device is registered but the link
470 * has not yet been established.
472 * Return: Returns an error code on failure. Returns 0 if the port is not
473 * connected or failed to reach state TB_PORT_UP within one second. Returns 1
474 * if the port is connected and in state TB_PORT_UP.
476 int tb_wait_for_port(struct tb_port
*port
, bool wait_if_unplugged
)
480 if (!port
->cap_phy
) {
481 tb_port_WARN(port
, "does not have PHY\n");
484 if (tb_is_upstream_port(port
)) {
485 tb_port_WARN(port
, "is the upstream port\n");
490 state
= tb_port_state(port
);
493 if (state
== TB_PORT_DISABLED
) {
494 tb_port_info(port
, "is disabled (state: 0)\n");
497 if (state
== TB_PORT_UNPLUGGED
) {
498 if (wait_if_unplugged
) {
499 /* used during resume */
501 "is unplugged (state: 7), retrying...\n");
505 tb_port_info(port
, "is unplugged (state: 7)\n");
508 if (state
== TB_PORT_UP
) {
510 "is connected, link is up (state: 2)\n");
515 * After plug-in the state is TB_PORT_CONNECTING. Give it some
519 "is connected, link is not up (state: %d), retrying...\n",
524 "failed to reach state TB_PORT_UP. Ignoring port...\n");
529 * tb_port_add_nfc_credits() - add/remove non flow controlled credits to port
531 * Change the number of NFC credits allocated to @port by @credits. To remove
532 * NFC credits pass a negative amount of credits.
534 * Return: Returns 0 on success or an error code on failure.
536 int tb_port_add_nfc_credits(struct tb_port
*port
, int credits
)
541 "adding %#x NFC credits (%#x -> %#x)",
543 port
->config
.nfc_credits
,
544 port
->config
.nfc_credits
+ credits
);
545 port
->config
.nfc_credits
+= credits
;
546 return tb_port_write(port
, &port
->config
.nfc_credits
,
551 * tb_port_clear_counter() - clear a counter in TB_CFG_COUNTER
553 * Return: Returns 0 on success or an error code on failure.
555 int tb_port_clear_counter(struct tb_port
*port
, int counter
)
557 u32 zero
[3] = { 0, 0, 0 };
558 tb_port_info(port
, "clearing counter %d\n", counter
);
559 return tb_port_write(port
, zero
, TB_CFG_COUNTERS
, 3 * counter
, 3);
563 * tb_init_port() - initialize a port
565 * This is a helper method for tb_switch_alloc. Does not check or initialize
566 * any downstream switches.
568 * Return: Returns 0 on success or an error code on failure.
570 static int tb_init_port(struct tb_port
*port
)
575 res
= tb_port_read(port
, &port
->config
, TB_CFG_PORT
, 0, 8);
579 /* Port 0 is the switch itself and has no PHY. */
580 if (port
->config
.type
== TB_TYPE_PORT
&& port
->port
!= 0) {
581 cap
= tb_port_find_cap(port
, TB_PORT_CAP_PHY
);
586 tb_port_WARN(port
, "non switch port without a PHY\n");
589 tb_dump_port(port
->sw
->tb
, &port
->config
);
591 /* TODO: Read dual link port, DP port and more from EEPROM. */
596 /* switch utility functions */
598 static void tb_dump_switch(struct tb
*tb
, struct tb_regs_switch_header
*sw
)
601 " Switch: %x:%x (Revision: %d, TB Version: %d)\n",
602 sw
->vendor_id
, sw
->device_id
, sw
->revision
,
603 sw
->thunderbolt_version
);
604 tb_info(tb
, " Max Port Number: %d\n", sw
->max_port_number
);
605 tb_info(tb
, " Config:\n");
607 " Upstream Port Number: %d Depth: %d Route String: %#llx Enabled: %d, PlugEventsDelay: %dms\n",
608 sw
->upstream_port_number
, sw
->depth
,
609 (((u64
) sw
->route_hi
) << 32) | sw
->route_lo
,
610 sw
->enabled
, sw
->plug_events_delay
);
612 " unknown1: %#x unknown4: %#x\n",
613 sw
->__unknown1
, sw
->__unknown4
);
617 * reset_switch() - reconfigure route, enable and send TB_CFG_PKG_RESET
619 * Return: Returns 0 on success or an error code on failure.
621 int tb_switch_reset(struct tb
*tb
, u64 route
)
623 struct tb_cfg_result res
;
624 struct tb_regs_switch_header header
= {
625 header
.route_hi
= route
>> 32,
626 header
.route_lo
= route
,
627 header
.enabled
= true,
629 tb_info(tb
, "resetting switch at %llx\n", route
);
630 res
.err
= tb_cfg_write(tb
->ctl
, ((u32
*) &header
) + 2, route
,
634 res
= tb_cfg_reset(tb
->ctl
, route
, TB_CFG_DEFAULT_TIMEOUT
);
640 struct tb_switch
*get_switch_at_route(struct tb_switch
*sw
, u64 route
)
642 u8 next_port
= route
; /*
643 * Routes use a stride of 8 bits,
644 * eventhough a port index has 6 bits at most.
648 if (next_port
> sw
->config
.max_port_number
)
650 if (tb_is_upstream_port(&sw
->ports
[next_port
]))
652 if (!sw
->ports
[next_port
].remote
)
654 return get_switch_at_route(sw
->ports
[next_port
].remote
->sw
,
655 route
>> TB_ROUTE_SHIFT
);
659 * tb_plug_events_active() - enable/disable plug events on a switch
661 * Also configures a sane plug_events_delay of 255ms.
663 * Return: Returns 0 on success or an error code on failure.
665 static int tb_plug_events_active(struct tb_switch
*sw
, bool active
)
670 if (!sw
->config
.enabled
)
673 sw
->config
.plug_events_delay
= 0xff;
674 res
= tb_sw_write(sw
, ((u32
*) &sw
->config
) + 4, TB_CFG_SWITCH
, 4, 1);
678 res
= tb_sw_read(sw
, &data
, TB_CFG_SWITCH
, sw
->cap_plug_events
+ 1, 1);
683 data
= data
& 0xFFFFFF83;
684 switch (sw
->config
.device_id
) {
685 case PCI_DEVICE_ID_INTEL_LIGHT_RIDGE
:
686 case PCI_DEVICE_ID_INTEL_EAGLE_RIDGE
:
687 case PCI_DEVICE_ID_INTEL_PORT_RIDGE
:
695 return tb_sw_write(sw
, &data
, TB_CFG_SWITCH
,
696 sw
->cap_plug_events
+ 1, 1);
699 static ssize_t
authorized_show(struct device
*dev
,
700 struct device_attribute
*attr
,
703 struct tb_switch
*sw
= tb_to_switch(dev
);
705 return sprintf(buf
, "%u\n", sw
->authorized
);
708 static int tb_switch_set_authorized(struct tb_switch
*sw
, unsigned int val
)
712 if (mutex_lock_interruptible(&switch_lock
))
722 ret
= tb_domain_approve_switch_key(sw
->tb
, sw
);
724 ret
= tb_domain_approve_switch(sw
->tb
, sw
);
727 /* Challenge switch */
730 ret
= tb_domain_challenge_switch_key(sw
->tb
, sw
);
738 sw
->authorized
= val
;
739 /* Notify status change to the userspace */
740 kobject_uevent(&sw
->dev
.kobj
, KOBJ_CHANGE
);
744 mutex_unlock(&switch_lock
);
748 static ssize_t
authorized_store(struct device
*dev
,
749 struct device_attribute
*attr
,
750 const char *buf
, size_t count
)
752 struct tb_switch
*sw
= tb_to_switch(dev
);
756 ret
= kstrtouint(buf
, 0, &val
);
762 ret
= tb_switch_set_authorized(sw
, val
);
764 return ret
? ret
: count
;
766 static DEVICE_ATTR_RW(authorized
);
768 static ssize_t
device_show(struct device
*dev
, struct device_attribute
*attr
,
771 struct tb_switch
*sw
= tb_to_switch(dev
);
773 return sprintf(buf
, "%#x\n", sw
->device
);
775 static DEVICE_ATTR_RO(device
);
778 device_name_show(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
780 struct tb_switch
*sw
= tb_to_switch(dev
);
782 return sprintf(buf
, "%s\n", sw
->device_name
? sw
->device_name
: "");
784 static DEVICE_ATTR_RO(device_name
);
786 static ssize_t
key_show(struct device
*dev
, struct device_attribute
*attr
,
789 struct tb_switch
*sw
= tb_to_switch(dev
);
792 if (mutex_lock_interruptible(&switch_lock
))
796 ret
= sprintf(buf
, "%*phN\n", TB_SWITCH_KEY_SIZE
, sw
->key
);
798 ret
= sprintf(buf
, "\n");
800 mutex_unlock(&switch_lock
);
804 static ssize_t
key_store(struct device
*dev
, struct device_attribute
*attr
,
805 const char *buf
, size_t count
)
807 struct tb_switch
*sw
= tb_to_switch(dev
);
808 u8 key
[TB_SWITCH_KEY_SIZE
];
814 if (hex2bin(key
, buf
, sizeof(key
)))
817 if (mutex_lock_interruptible(&switch_lock
))
820 if (sw
->authorized
) {
824 sw
->key
= kmemdup(key
, sizeof(key
), GFP_KERNEL
);
829 mutex_unlock(&switch_lock
);
832 static DEVICE_ATTR_RW(key
);
834 static ssize_t
nvm_authenticate_show(struct device
*dev
,
835 struct device_attribute
*attr
, char *buf
)
837 struct tb_switch
*sw
= tb_to_switch(dev
);
840 nvm_get_auth_status(sw
, &status
);
841 return sprintf(buf
, "%#x\n", status
);
844 static ssize_t
nvm_authenticate_store(struct device
*dev
,
845 struct device_attribute
*attr
, const char *buf
, size_t count
)
847 struct tb_switch
*sw
= tb_to_switch(dev
);
851 if (mutex_lock_interruptible(&switch_lock
))
854 /* If NVMem devices are not yet added */
860 ret
= kstrtobool(buf
, &val
);
864 /* Always clear the authentication status */
865 nvm_clear_auth_status(sw
);
868 ret
= nvm_validate_and_write(sw
);
872 sw
->nvm
->authenticating
= true;
875 ret
= nvm_authenticate_host(sw
);
877 ret
= nvm_authenticate_device(sw
);
881 mutex_unlock(&switch_lock
);
887 static DEVICE_ATTR_RW(nvm_authenticate
);
889 static ssize_t
nvm_version_show(struct device
*dev
,
890 struct device_attribute
*attr
, char *buf
)
892 struct tb_switch
*sw
= tb_to_switch(dev
);
895 if (mutex_lock_interruptible(&switch_lock
))
903 ret
= sprintf(buf
, "%x.%x\n", sw
->nvm
->major
, sw
->nvm
->minor
);
905 mutex_unlock(&switch_lock
);
909 static DEVICE_ATTR_RO(nvm_version
);
911 static ssize_t
vendor_show(struct device
*dev
, struct device_attribute
*attr
,
914 struct tb_switch
*sw
= tb_to_switch(dev
);
916 return sprintf(buf
, "%#x\n", sw
->vendor
);
918 static DEVICE_ATTR_RO(vendor
);
921 vendor_name_show(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
923 struct tb_switch
*sw
= tb_to_switch(dev
);
925 return sprintf(buf
, "%s\n", sw
->vendor_name
? sw
->vendor_name
: "");
927 static DEVICE_ATTR_RO(vendor_name
);
929 static ssize_t
unique_id_show(struct device
*dev
, struct device_attribute
*attr
,
932 struct tb_switch
*sw
= tb_to_switch(dev
);
934 return sprintf(buf
, "%pUb\n", sw
->uuid
);
936 static DEVICE_ATTR_RO(unique_id
);
938 static struct attribute
*switch_attrs
[] = {
939 &dev_attr_authorized
.attr
,
940 &dev_attr_device
.attr
,
941 &dev_attr_device_name
.attr
,
943 &dev_attr_nvm_authenticate
.attr
,
944 &dev_attr_nvm_version
.attr
,
945 &dev_attr_vendor
.attr
,
946 &dev_attr_vendor_name
.attr
,
947 &dev_attr_unique_id
.attr
,
951 static umode_t
switch_attr_is_visible(struct kobject
*kobj
,
952 struct attribute
*attr
, int n
)
954 struct device
*dev
= container_of(kobj
, struct device
, kobj
);
955 struct tb_switch
*sw
= tb_to_switch(dev
);
957 if (attr
== &dev_attr_key
.attr
) {
959 sw
->tb
->security_level
== TB_SECURITY_SECURE
&&
960 sw
->security_level
== TB_SECURITY_SECURE
)
963 } else if (attr
== &dev_attr_nvm_authenticate
.attr
||
964 attr
== &dev_attr_nvm_version
.attr
) {
970 return sw
->safe_mode
? 0 : attr
->mode
;
973 static struct attribute_group switch_group
= {
974 .is_visible
= switch_attr_is_visible
,
975 .attrs
= switch_attrs
,
978 static const struct attribute_group
*switch_groups
[] = {
983 static void tb_switch_release(struct device
*dev
)
985 struct tb_switch
*sw
= tb_to_switch(dev
);
987 dma_port_free(sw
->dma_port
);
990 kfree(sw
->device_name
);
991 kfree(sw
->vendor_name
);
998 struct device_type tb_switch_type
= {
999 .name
= "thunderbolt_device",
1000 .release
= tb_switch_release
,
1003 static int tb_switch_get_generation(struct tb_switch
*sw
)
1005 switch (sw
->config
.device_id
) {
1006 case PCI_DEVICE_ID_INTEL_LIGHT_RIDGE
:
1007 case PCI_DEVICE_ID_INTEL_EAGLE_RIDGE
:
1008 case PCI_DEVICE_ID_INTEL_LIGHT_PEAK
:
1009 case PCI_DEVICE_ID_INTEL_CACTUS_RIDGE_2C
:
1010 case PCI_DEVICE_ID_INTEL_CACTUS_RIDGE_4C
:
1011 case PCI_DEVICE_ID_INTEL_PORT_RIDGE
:
1012 case PCI_DEVICE_ID_INTEL_REDWOOD_RIDGE_2C_BRIDGE
:
1013 case PCI_DEVICE_ID_INTEL_REDWOOD_RIDGE_4C_BRIDGE
:
1016 case PCI_DEVICE_ID_INTEL_WIN_RIDGE_2C_BRIDGE
:
1017 case PCI_DEVICE_ID_INTEL_FALCON_RIDGE_2C_BRIDGE
:
1018 case PCI_DEVICE_ID_INTEL_FALCON_RIDGE_4C_BRIDGE
:
1021 case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_LP_BRIDGE
:
1022 case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_2C_BRIDGE
:
1023 case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_4C_BRIDGE
:
1024 case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_C_2C_BRIDGE
:
1025 case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_C_4C_BRIDGE
:
1030 * For unknown switches assume generation to be 1 to be
1033 tb_sw_warn(sw
, "unsupported switch device id %#x\n",
1034 sw
->config
.device_id
);
1040 * tb_switch_alloc() - allocate a switch
1041 * @tb: Pointer to the owning domain
1042 * @parent: Parent device for this switch
1043 * @route: Route string for this switch
1045 * Allocates and initializes a switch. Will not upload configuration to
1046 * the switch. For that you need to call tb_switch_configure()
1047 * separately. The returned switch should be released by calling
1050 * Return: Pointer to the allocated switch or %NULL in case of failure
1052 struct tb_switch
*tb_switch_alloc(struct tb
*tb
, struct device
*parent
,
1057 struct tb_switch
*sw
;
1058 int upstream_port
= tb_cfg_get_upstream_port(tb
->ctl
, route
);
1059 if (upstream_port
< 0)
1062 sw
= kzalloc(sizeof(*sw
), GFP_KERNEL
);
1067 if (tb_cfg_read(tb
->ctl
, &sw
->config
, route
, 0, TB_CFG_SWITCH
, 0, 5))
1068 goto err_free_sw_ports
;
1070 tb_info(tb
, "current switch config:\n");
1071 tb_dump_switch(tb
, &sw
->config
);
1073 /* configure switch */
1074 sw
->config
.upstream_port_number
= upstream_port
;
1075 sw
->config
.depth
= tb_route_length(route
);
1076 sw
->config
.route_lo
= route
;
1077 sw
->config
.route_hi
= route
>> 32;
1078 sw
->config
.enabled
= 0;
1080 /* initialize ports */
1081 sw
->ports
= kcalloc(sw
->config
.max_port_number
+ 1, sizeof(*sw
->ports
),
1084 goto err_free_sw_ports
;
1086 for (i
= 0; i
<= sw
->config
.max_port_number
; i
++) {
1087 /* minimum setup for tb_find_cap and tb_drom_read to work */
1088 sw
->ports
[i
].sw
= sw
;
1089 sw
->ports
[i
].port
= i
;
1092 sw
->generation
= tb_switch_get_generation(sw
);
1094 cap
= tb_switch_find_vse_cap(sw
, TB_VSE_CAP_PLUG_EVENTS
);
1096 tb_sw_warn(sw
, "cannot find TB_VSE_CAP_PLUG_EVENTS aborting\n");
1097 goto err_free_sw_ports
;
1099 sw
->cap_plug_events
= cap
;
1101 /* Root switch is always authorized */
1103 sw
->authorized
= true;
1105 device_initialize(&sw
->dev
);
1106 sw
->dev
.parent
= parent
;
1107 sw
->dev
.bus
= &tb_bus_type
;
1108 sw
->dev
.type
= &tb_switch_type
;
1109 sw
->dev
.groups
= switch_groups
;
1110 dev_set_name(&sw
->dev
, "%u-%llx", tb
->index
, tb_route(sw
));
1122 * tb_switch_alloc_safe_mode() - allocate a switch that is in safe mode
1123 * @tb: Pointer to the owning domain
1124 * @parent: Parent device for this switch
1125 * @route: Route string for this switch
1127 * This creates a switch in safe mode. This means the switch pretty much
1128 * lacks all capabilities except DMA configuration port before it is
1129 * flashed with a valid NVM firmware.
1131 * The returned switch must be released by calling tb_switch_put().
1133 * Return: Pointer to the allocated switch or %NULL in case of failure
1136 tb_switch_alloc_safe_mode(struct tb
*tb
, struct device
*parent
, u64 route
)
1138 struct tb_switch
*sw
;
1140 sw
= kzalloc(sizeof(*sw
), GFP_KERNEL
);
1145 sw
->config
.depth
= tb_route_length(route
);
1146 sw
->config
.route_hi
= upper_32_bits(route
);
1147 sw
->config
.route_lo
= lower_32_bits(route
);
1148 sw
->safe_mode
= true;
1150 device_initialize(&sw
->dev
);
1151 sw
->dev
.parent
= parent
;
1152 sw
->dev
.bus
= &tb_bus_type
;
1153 sw
->dev
.type
= &tb_switch_type
;
1154 sw
->dev
.groups
= switch_groups
;
1155 dev_set_name(&sw
->dev
, "%u-%llx", tb
->index
, tb_route(sw
));
1161 * tb_switch_configure() - Uploads configuration to the switch
1162 * @sw: Switch to configure
1164 * Call this function before the switch is added to the system. It will
1165 * upload configuration to the switch and makes it available for the
1166 * connection manager to use.
1168 * Return: %0 in case of success and negative errno in case of failure
1170 int tb_switch_configure(struct tb_switch
*sw
)
1172 struct tb
*tb
= sw
->tb
;
1176 route
= tb_route(sw
);
1178 "initializing Switch at %#llx (depth: %d, up port: %d)\n",
1179 route
, tb_route_length(route
), sw
->config
.upstream_port_number
);
1181 if (sw
->config
.vendor_id
!= PCI_VENDOR_ID_INTEL
)
1182 tb_sw_warn(sw
, "unknown switch vendor id %#x\n",
1183 sw
->config
.vendor_id
);
1185 sw
->config
.enabled
= 1;
1187 /* upload configuration */
1188 ret
= tb_sw_write(sw
, 1 + (u32
*)&sw
->config
, TB_CFG_SWITCH
, 1, 3);
1192 return tb_plug_events_active(sw
, true);
1195 static void tb_switch_set_uuid(struct tb_switch
*sw
)
1204 * The newer controllers include fused UUID as part of link
1205 * controller specific registers
1207 cap
= tb_switch_find_vse_cap(sw
, TB_VSE_CAP_LINK_CONTROLLER
);
1209 tb_sw_read(sw
, uuid
, TB_CFG_SWITCH
, cap
+ 3, 4);
1212 * ICM generates UUID based on UID and fills the upper
1213 * two words with ones. This is not strictly following
1214 * UUID format but we want to be compatible with it so
1215 * we do the same here.
1217 uuid
[0] = sw
->uid
& 0xffffffff;
1218 uuid
[1] = (sw
->uid
>> 32) & 0xffffffff;
1219 uuid
[2] = 0xffffffff;
1220 uuid
[3] = 0xffffffff;
1223 sw
->uuid
= kmemdup(uuid
, sizeof(uuid
), GFP_KERNEL
);
1226 static int tb_switch_add_dma_port(struct tb_switch
*sw
)
1231 switch (sw
->generation
) {
1236 /* Only root switch can be upgraded */
1243 * DMA port is the only thing available when the switch
1251 if (sw
->no_nvm_upgrade
)
1254 sw
->dma_port
= dma_port_alloc(sw
);
1259 * Check status of the previous flash authentication. If there
1260 * is one we need to power cycle the switch in any case to make
1261 * it functional again.
1263 ret
= dma_port_flash_update_auth_status(sw
->dma_port
, &status
);
1268 tb_sw_info(sw
, "switch flash authentication failed\n");
1269 tb_switch_set_uuid(sw
);
1270 nvm_set_auth_status(sw
, status
);
1273 tb_sw_info(sw
, "power cycling the switch now\n");
1274 dma_port_power_cycle(sw
->dma_port
);
1277 * We return error here which causes the switch adding failure.
1278 * It should appear back after power cycle is complete.
1284 * tb_switch_add() - Add a switch to the domain
1285 * @sw: Switch to add
1287 * This is the last step in adding switch to the domain. It will read
1288 * identification information from DROM and initializes ports so that
1289 * they can be used to connect other switches. The switch will be
1290 * exposed to the userspace when this function successfully returns. To
1291 * remove and release the switch, call tb_switch_remove().
1293 * Return: %0 in case of success and negative errno in case of failure
1295 int tb_switch_add(struct tb_switch
*sw
)
1300 * Initialize DMA control port now before we read DROM. Recent
1301 * host controllers have more complete DROM on NVM that includes
1302 * vendor and model identification strings which we then expose
1303 * to the userspace. NVM can be accessed through DMA
1304 * configuration based mailbox.
1306 ret
= tb_switch_add_dma_port(sw
);
1310 if (!sw
->safe_mode
) {
1312 ret
= tb_drom_read(sw
);
1314 tb_sw_warn(sw
, "tb_eeprom_read_rom failed\n");
1317 tb_sw_info(sw
, "uid: %#llx\n", sw
->uid
);
1319 tb_switch_set_uuid(sw
);
1321 for (i
= 0; i
<= sw
->config
.max_port_number
; i
++) {
1322 if (sw
->ports
[i
].disabled
) {
1323 tb_port_info(&sw
->ports
[i
], "disabled by eeprom\n");
1326 ret
= tb_init_port(&sw
->ports
[i
]);
1332 ret
= device_add(&sw
->dev
);
1336 ret
= tb_switch_nvm_add(sw
);
1338 device_del(&sw
->dev
);
1344 * tb_switch_remove() - Remove and release a switch
1345 * @sw: Switch to remove
1347 * This will remove the switch from the domain and release it after last
1348 * reference count drops to zero. If there are switches connected below
1349 * this switch, they will be removed as well.
1351 void tb_switch_remove(struct tb_switch
*sw
)
1355 /* port 0 is the switch itself and never has a remote */
1356 for (i
= 1; i
<= sw
->config
.max_port_number
; i
++) {
1357 if (tb_is_upstream_port(&sw
->ports
[i
]))
1359 if (sw
->ports
[i
].remote
)
1360 tb_switch_remove(sw
->ports
[i
].remote
->sw
);
1361 sw
->ports
[i
].remote
= NULL
;
1364 if (!sw
->is_unplugged
)
1365 tb_plug_events_active(sw
, false);
1367 tb_switch_nvm_remove(sw
);
1368 device_unregister(&sw
->dev
);
1372 * tb_sw_set_unplugged() - set is_unplugged on switch and downstream switches
1374 void tb_sw_set_unplugged(struct tb_switch
*sw
)
1377 if (sw
== sw
->tb
->root_switch
) {
1378 tb_sw_WARN(sw
, "cannot unplug root switch\n");
1381 if (sw
->is_unplugged
) {
1382 tb_sw_WARN(sw
, "is_unplugged already set\n");
1385 sw
->is_unplugged
= true;
1386 for (i
= 0; i
<= sw
->config
.max_port_number
; i
++) {
1387 if (!tb_is_upstream_port(&sw
->ports
[i
]) && sw
->ports
[i
].remote
)
1388 tb_sw_set_unplugged(sw
->ports
[i
].remote
->sw
);
1392 int tb_switch_resume(struct tb_switch
*sw
)
1395 tb_sw_info(sw
, "resuming switch\n");
1398 * Check for UID of the connected switches except for root
1399 * switch which we assume cannot be removed.
1404 err
= tb_drom_read_uid_only(sw
, &uid
);
1406 tb_sw_warn(sw
, "uid read failed\n");
1409 if (sw
->uid
!= uid
) {
1411 "changed while suspended (uid %#llx -> %#llx)\n",
1417 /* upload configuration */
1418 err
= tb_sw_write(sw
, 1 + (u32
*) &sw
->config
, TB_CFG_SWITCH
, 1, 3);
1422 err
= tb_plug_events_active(sw
, true);
1426 /* check for surviving downstream switches */
1427 for (i
= 1; i
<= sw
->config
.max_port_number
; i
++) {
1428 struct tb_port
*port
= &sw
->ports
[i
];
1429 if (tb_is_upstream_port(port
))
1433 if (tb_wait_for_port(port
, true) <= 0
1434 || tb_switch_resume(port
->remote
->sw
)) {
1436 "lost during suspend, disconnecting\n");
1437 tb_sw_set_unplugged(port
->remote
->sw
);
1443 void tb_switch_suspend(struct tb_switch
*sw
)
1446 err
= tb_plug_events_active(sw
, false);
1450 for (i
= 1; i
<= sw
->config
.max_port_number
; i
++) {
1451 if (!tb_is_upstream_port(&sw
->ports
[i
]) && sw
->ports
[i
].remote
)
1452 tb_switch_suspend(sw
->ports
[i
].remote
->sw
);
1455 * TODO: invoke tb_cfg_prepare_to_sleep here? does not seem to have any
1460 struct tb_sw_lookup
{
1467 static int tb_switch_match(struct device
*dev
, void *data
)
1469 struct tb_switch
*sw
= tb_to_switch(dev
);
1470 struct tb_sw_lookup
*lookup
= data
;
1474 if (sw
->tb
!= lookup
->tb
)
1478 return !memcmp(sw
->uuid
, lookup
->uuid
, sizeof(*lookup
->uuid
));
1480 /* Root switch is matched only by depth */
1484 return sw
->link
== lookup
->link
&& sw
->depth
== lookup
->depth
;
1488 * tb_switch_find_by_link_depth() - Find switch by link and depth
1489 * @tb: Domain the switch belongs
1490 * @link: Link number the switch is connected
1491 * @depth: Depth of the switch in link
1493 * Returned switch has reference count increased so the caller needs to
1494 * call tb_switch_put() when done with the switch.
1496 struct tb_switch
*tb_switch_find_by_link_depth(struct tb
*tb
, u8 link
, u8 depth
)
1498 struct tb_sw_lookup lookup
;
1501 memset(&lookup
, 0, sizeof(lookup
));
1504 lookup
.depth
= depth
;
1506 dev
= bus_find_device(&tb_bus_type
, NULL
, &lookup
, tb_switch_match
);
1508 return tb_to_switch(dev
);
1514 * tb_switch_find_by_link_depth() - Find switch by UUID
1515 * @tb: Domain the switch belongs
1516 * @uuid: UUID to look for
1518 * Returned switch has reference count increased so the caller needs to
1519 * call tb_switch_put() when done with the switch.
1521 struct tb_switch
*tb_switch_find_by_uuid(struct tb
*tb
, const uuid_t
*uuid
)
1523 struct tb_sw_lookup lookup
;
1526 memset(&lookup
, 0, sizeof(lookup
));
1530 dev
= bus_find_device(&tb_bus_type
, NULL
, &lookup
, tb_switch_match
);
1532 return tb_to_switch(dev
);
1537 void tb_switch_exit(void)
1539 ida_destroy(&nvm_ida
);