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>
19 /* Switch NVM support */
23 struct nvm_auth_status {
24 struct list_head list;
30 WRITE_AND_AUTHENTICATE = 1,
35 * Hold NVM authentication failure status per switch This information
36 * needs to stay around even when the switch gets power cycled so we
39 static LIST_HEAD(nvm_auth_status_cache);
40 static DEFINE_MUTEX(nvm_auth_status_lock);
42 static struct nvm_auth_status *__nvm_get_auth_status(const struct tb_switch *sw)
44 struct nvm_auth_status *st;
46 list_for_each_entry(st, &nvm_auth_status_cache, list) {
47 if (uuid_equal(&st->uuid, sw->uuid))
54 static void nvm_get_auth_status(const struct tb_switch *sw, u32 *status)
56 struct nvm_auth_status *st;
58 mutex_lock(&nvm_auth_status_lock);
59 st = __nvm_get_auth_status(sw);
60 mutex_unlock(&nvm_auth_status_lock);
62 *status = st ? st->status : 0;
65 static void nvm_set_auth_status(const struct tb_switch *sw, u32 status)
67 struct nvm_auth_status *st;
69 if (WARN_ON(!sw->uuid))
72 mutex_lock(&nvm_auth_status_lock);
73 st = __nvm_get_auth_status(sw);
76 st = kzalloc(sizeof(*st), GFP_KERNEL);
80 memcpy(&st->uuid, sw->uuid, sizeof(st->uuid));
81 INIT_LIST_HEAD(&st->list);
82 list_add_tail(&st->list, &nvm_auth_status_cache);
87 mutex_unlock(&nvm_auth_status_lock);
90 static void nvm_clear_auth_status(const struct tb_switch *sw)
92 struct nvm_auth_status *st;
94 mutex_lock(&nvm_auth_status_lock);
95 st = __nvm_get_auth_status(sw);
100 mutex_unlock(&nvm_auth_status_lock);
103 static int nvm_validate_and_write(struct tb_switch *sw)
105 unsigned int image_size, hdr_size;
106 const u8 *buf = sw->nvm->buf;
113 image_size = sw->nvm->buf_data_size;
114 if (image_size < NVM_MIN_SIZE || image_size > NVM_MAX_SIZE)
118 * FARB pointer must point inside the image and must at least
119 * contain parts of the digital section we will be reading here.
121 hdr_size = (*(u32 *)buf) & 0xffffff;
122 if (hdr_size + NVM_DEVID + 2 >= image_size)
125 /* Digital section start should be aligned to 4k page */
126 if (!IS_ALIGNED(hdr_size, SZ_4K))
130 * Read digital section size and check that it also fits inside
133 ds_size = *(u16 *)(buf + hdr_size);
134 if (ds_size >= image_size)
137 if (!sw->safe_mode) {
141 * Make sure the device ID in the image matches the one
142 * we read from the switch config space.
144 device_id = *(u16 *)(buf + hdr_size + NVM_DEVID);
145 if (device_id != sw->config.device_id)
148 if (sw->generation < 3) {
149 /* Write CSS headers first */
150 ret = dma_port_flash_write(sw->dma_port,
151 DMA_PORT_CSS_ADDRESS, buf + NVM_CSS,
152 DMA_PORT_CSS_MAX_SIZE);
157 /* Skip headers in the image */
159 image_size -= hdr_size;
162 if (tb_switch_is_usb4(sw))
163 ret = usb4_switch_nvm_write(sw, 0, buf, image_size);
165 ret = dma_port_flash_write(sw->dma_port, 0, buf, image_size);
167 sw->nvm->flushed = true;
171 static int nvm_authenticate_host_dma_port(struct tb_switch *sw)
176 * Root switch NVM upgrade requires that we disconnect the
177 * existing paths first (in case it is not in safe mode
180 if (!sw->safe_mode) {
183 ret = tb_domain_disconnect_all_paths(sw->tb);
187 * The host controller goes away pretty soon after this if
188 * everything goes well so getting timeout is expected.
190 ret = dma_port_flash_update_auth(sw->dma_port);
191 if (!ret || ret == -ETIMEDOUT)
195 * Any error from update auth operation requires power
196 * cycling of the host router.
198 tb_sw_warn(sw, "failed to authenticate NVM, power cycling\n");
199 if (dma_port_flash_update_auth_status(sw->dma_port, &status) > 0)
200 nvm_set_auth_status(sw, status);
204 * From safe mode we can get out by just power cycling the
207 dma_port_power_cycle(sw->dma_port);
211 static int nvm_authenticate_device_dma_port(struct tb_switch *sw)
213 int ret, retries = 10;
215 ret = dma_port_flash_update_auth(sw->dma_port);
221 /* Power cycle is required */
228 * Poll here for the authentication status. It takes some time
229 * for the device to respond (we get timeout for a while). Once
230 * we get response the device needs to be power cycled in order
231 * to the new NVM to be taken into use.
236 ret = dma_port_flash_update_auth_status(sw->dma_port, &status);
237 if (ret < 0 && ret != -ETIMEDOUT)
241 tb_sw_warn(sw, "failed to authenticate NVM\n");
242 nvm_set_auth_status(sw, status);
245 tb_sw_info(sw, "power cycling the switch now\n");
246 dma_port_power_cycle(sw->dma_port);
256 static void nvm_authenticate_start_dma_port(struct tb_switch *sw)
258 struct pci_dev *root_port;
261 * During host router NVM upgrade we should not allow root port to
262 * go into D3cold because some root ports cannot trigger PME
263 * itself. To be on the safe side keep the root port in D0 during
264 * the whole upgrade process.
266 root_port = pcie_find_root_port(sw->tb->nhi->pdev);
268 pm_runtime_get_noresume(&root_port->dev);
271 static void nvm_authenticate_complete_dma_port(struct tb_switch *sw)
273 struct pci_dev *root_port;
275 root_port = pcie_find_root_port(sw->tb->nhi->pdev);
277 pm_runtime_put(&root_port->dev);
280 static inline bool nvm_readable(struct tb_switch *sw)
282 if (tb_switch_is_usb4(sw)) {
284 * USB4 devices must support NVM operations but it is
285 * optional for hosts. Therefore we query the NVM sector
286 * size here and if it is supported assume NVM
287 * operations are implemented.
289 return usb4_switch_nvm_sector_size(sw) > 0;
292 /* Thunderbolt 2 and 3 devices support NVM through DMA port */
293 return !!sw->dma_port;
296 static inline bool nvm_upgradeable(struct tb_switch *sw)
298 if (sw->no_nvm_upgrade)
300 return nvm_readable(sw);
303 static inline int nvm_read(struct tb_switch *sw, unsigned int address,
304 void *buf, size_t size)
306 if (tb_switch_is_usb4(sw))
307 return usb4_switch_nvm_read(sw, address, buf, size);
308 return dma_port_flash_read(sw->dma_port, address, buf, size);
311 static int nvm_authenticate(struct tb_switch *sw)
315 if (tb_switch_is_usb4(sw))
316 return usb4_switch_nvm_authenticate(sw);
319 nvm_authenticate_start_dma_port(sw);
320 ret = nvm_authenticate_host_dma_port(sw);
322 ret = nvm_authenticate_device_dma_port(sw);
328 static int tb_switch_nvm_read(void *priv, unsigned int offset, void *val,
331 struct tb_nvm *nvm = priv;
332 struct tb_switch *sw = tb_to_switch(nvm->dev);
335 pm_runtime_get_sync(&sw->dev);
337 if (!mutex_trylock(&sw->tb->lock)) {
338 ret = restart_syscall();
342 ret = nvm_read(sw, offset, val, bytes);
343 mutex_unlock(&sw->tb->lock);
346 pm_runtime_mark_last_busy(&sw->dev);
347 pm_runtime_put_autosuspend(&sw->dev);
352 static int tb_switch_nvm_write(void *priv, unsigned int offset, void *val,
355 struct tb_nvm *nvm = priv;
356 struct tb_switch *sw = tb_to_switch(nvm->dev);
359 if (!mutex_trylock(&sw->tb->lock))
360 return restart_syscall();
363 * Since writing the NVM image might require some special steps,
364 * for example when CSS headers are written, we cache the image
365 * locally here and handle the special cases when the user asks
366 * us to authenticate the image.
368 ret = tb_nvm_write_buf(nvm, offset, val, bytes);
369 mutex_unlock(&sw->tb->lock);
374 static int tb_switch_nvm_add(struct tb_switch *sw)
380 if (!nvm_readable(sw))
384 * The NVM format of non-Intel hardware is not known so
385 * currently restrict NVM upgrade for Intel hardware. We may
386 * relax this in the future when we learn other NVM formats.
388 if (sw->config.vendor_id != PCI_VENDOR_ID_INTEL &&
389 sw->config.vendor_id != 0x8087) {
391 "NVM format of vendor %#x is not known, disabling NVM upgrade\n",
392 sw->config.vendor_id);
396 nvm = tb_nvm_alloc(&sw->dev);
401 * If the switch is in safe-mode the only accessible portion of
402 * the NVM is the non-active one where userspace is expected to
403 * write new functional NVM.
405 if (!sw->safe_mode) {
406 u32 nvm_size, hdr_size;
408 ret = nvm_read(sw, NVM_FLASH_SIZE, &val, sizeof(val));
412 hdr_size = sw->generation < 3 ? SZ_8K : SZ_16K;
413 nvm_size = (SZ_1M << (val & 7)) / 8;
414 nvm_size = (nvm_size - hdr_size) / 2;
416 ret = nvm_read(sw, NVM_VERSION, &val, sizeof(val));
420 nvm->major = val >> 16;
421 nvm->minor = val >> 8;
423 ret = tb_nvm_add_active(nvm, nvm_size, tb_switch_nvm_read);
428 if (!sw->no_nvm_upgrade) {
429 ret = tb_nvm_add_non_active(nvm, NVM_MAX_SIZE,
430 tb_switch_nvm_write);
443 static void tb_switch_nvm_remove(struct tb_switch *sw)
453 /* Remove authentication status in case the switch is unplugged */
454 if (!nvm->authenticating)
455 nvm_clear_auth_status(sw);
460 /* port utility functions */
462 static const char *tb_port_type(struct tb_regs_port_header *port)
464 switch (port->type >> 16) {
466 switch ((u8) port->type) {
491 static void tb_dump_port(struct tb *tb, struct tb_regs_port_header *port)
494 " Port %d: %x:%x (Revision: %d, TB Version: %d, Type: %s (%#x))\n",
495 port->port_number, port->vendor_id, port->device_id,
496 port->revision, port->thunderbolt_version, tb_port_type(port),
498 tb_dbg(tb, " Max hop id (in/out): %d/%d\n",
499 port->max_in_hop_id, port->max_out_hop_id);
500 tb_dbg(tb, " Max counters: %d\n", port->max_counters);
501 tb_dbg(tb, " NFC Credits: %#x\n", port->nfc_credits);
505 * tb_port_state() - get connectedness state of a port
506 * @port: the port to check
508 * The port must have a TB_CAP_PHY (i.e. it should be a real port).
510 * Return: Returns an enum tb_port_state on success or an error code on failure.
512 int tb_port_state(struct tb_port *port)
514 struct tb_cap_phy phy;
516 if (port->cap_phy == 0) {
517 tb_port_WARN(port, "does not have a PHY\n");
520 res = tb_port_read(port, &phy, TB_CFG_PORT, port->cap_phy, 2);
527 * tb_wait_for_port() - wait for a port to become ready
528 * @port: Port to wait
529 * @wait_if_unplugged: Wait also when port is unplugged
531 * Wait up to 1 second for a port to reach state TB_PORT_UP. If
532 * wait_if_unplugged is set then we also wait if the port is in state
533 * TB_PORT_UNPLUGGED (it takes a while for the device to be registered after
534 * switch resume). Otherwise we only wait if a device is registered but the link
535 * has not yet been established.
537 * Return: Returns an error code on failure. Returns 0 if the port is not
538 * connected or failed to reach state TB_PORT_UP within one second. Returns 1
539 * if the port is connected and in state TB_PORT_UP.
541 int tb_wait_for_port(struct tb_port *port, bool wait_if_unplugged)
545 if (!port->cap_phy) {
546 tb_port_WARN(port, "does not have PHY\n");
549 if (tb_is_upstream_port(port)) {
550 tb_port_WARN(port, "is the upstream port\n");
555 state = tb_port_state(port);
558 if (state == TB_PORT_DISABLED) {
559 tb_port_dbg(port, "is disabled (state: 0)\n");
562 if (state == TB_PORT_UNPLUGGED) {
563 if (wait_if_unplugged) {
564 /* used during resume */
566 "is unplugged (state: 7), retrying...\n");
570 tb_port_dbg(port, "is unplugged (state: 7)\n");
573 if (state == TB_PORT_UP) {
574 tb_port_dbg(port, "is connected, link is up (state: 2)\n");
579 * After plug-in the state is TB_PORT_CONNECTING. Give it some
583 "is connected, link is not up (state: %d), retrying...\n",
588 "failed to reach state TB_PORT_UP. Ignoring port...\n");
593 * tb_port_add_nfc_credits() - add/remove non flow controlled credits to port
594 * @port: Port to add/remove NFC credits
595 * @credits: Credits to add/remove
597 * Change the number of NFC credits allocated to @port by @credits. To remove
598 * NFC credits pass a negative amount of credits.
600 * Return: Returns 0 on success or an error code on failure.
602 int tb_port_add_nfc_credits(struct tb_port *port, int credits)
606 if (credits == 0 || port->sw->is_unplugged)
610 * USB4 restricts programming NFC buffers to lane adapters only
611 * so skip other ports.
613 if (tb_switch_is_usb4(port->sw) && !tb_port_is_null(port))
616 nfc_credits = port->config.nfc_credits & ADP_CS_4_NFC_BUFFERS_MASK;
617 nfc_credits += credits;
619 tb_port_dbg(port, "adding %d NFC credits to %lu", credits,
620 port->config.nfc_credits & ADP_CS_4_NFC_BUFFERS_MASK);
622 port->config.nfc_credits &= ~ADP_CS_4_NFC_BUFFERS_MASK;
623 port->config.nfc_credits |= nfc_credits;
625 return tb_port_write(port, &port->config.nfc_credits,
626 TB_CFG_PORT, ADP_CS_4, 1);
630 * tb_port_clear_counter() - clear a counter in TB_CFG_COUNTER
631 * @port: Port whose counters to clear
632 * @counter: Counter index to clear
634 * Return: Returns 0 on success or an error code on failure.
636 int tb_port_clear_counter(struct tb_port *port, int counter)
638 u32 zero[3] = { 0, 0, 0 };
639 tb_port_dbg(port, "clearing counter %d\n", counter);
640 return tb_port_write(port, zero, TB_CFG_COUNTERS, 3 * counter, 3);
644 * tb_port_unlock() - Unlock downstream port
645 * @port: Port to unlock
647 * Needed for USB4 but can be called for any CIO/USB4 ports. Makes the
648 * downstream router accessible for CM.
650 int tb_port_unlock(struct tb_port *port)
652 if (tb_switch_is_icm(port->sw))
654 if (!tb_port_is_null(port))
656 if (tb_switch_is_usb4(port->sw))
657 return usb4_port_unlock(port);
661 static int __tb_port_enable(struct tb_port *port, bool enable)
666 if (!tb_port_is_null(port))
669 ret = tb_port_read(port, &phy, TB_CFG_PORT,
670 port->cap_phy + LANE_ADP_CS_1, 1);
675 phy &= ~LANE_ADP_CS_1_LD;
677 phy |= LANE_ADP_CS_1_LD;
679 return tb_port_write(port, &phy, TB_CFG_PORT,
680 port->cap_phy + LANE_ADP_CS_1, 1);
684 * tb_port_enable() - Enable lane adapter
685 * @port: Port to enable (can be %NULL)
687 * This is used for lane 0 and 1 adapters to enable it.
689 int tb_port_enable(struct tb_port *port)
691 return __tb_port_enable(port, true);
695 * tb_port_disable() - Disable lane adapter
696 * @port: Port to disable (can be %NULL)
698 * This is used for lane 0 and 1 adapters to disable it.
700 int tb_port_disable(struct tb_port *port)
702 return __tb_port_enable(port, false);
706 * tb_init_port() - initialize a port
708 * This is a helper method for tb_switch_alloc. Does not check or initialize
709 * any downstream switches.
711 * Return: Returns 0 on success or an error code on failure.
713 static int tb_init_port(struct tb_port *port)
718 res = tb_port_read(port, &port->config, TB_CFG_PORT, 0, 8);
720 if (res == -ENODEV) {
721 tb_dbg(port->sw->tb, " Port %d: not implemented\n",
723 port->disabled = true;
729 /* Port 0 is the switch itself and has no PHY. */
730 if (port->config.type == TB_TYPE_PORT && port->port != 0) {
731 cap = tb_port_find_cap(port, TB_PORT_CAP_PHY);
736 tb_port_WARN(port, "non switch port without a PHY\n");
738 cap = tb_port_find_cap(port, TB_PORT_CAP_USB4);
740 port->cap_usb4 = cap;
741 } else if (port->port != 0) {
742 cap = tb_port_find_cap(port, TB_PORT_CAP_ADAP);
744 port->cap_adap = cap;
747 tb_dump_port(port->sw->tb, &port->config);
749 INIT_LIST_HEAD(&port->list);
754 static int tb_port_alloc_hopid(struct tb_port *port, bool in, int min_hopid,
761 port_max_hopid = port->config.max_in_hop_id;
762 ida = &port->in_hopids;
764 port_max_hopid = port->config.max_out_hop_id;
765 ida = &port->out_hopids;
769 * NHI can use HopIDs 1-max for other adapters HopIDs 0-7 are
772 if (!tb_port_is_nhi(port) && min_hopid < TB_PATH_MIN_HOPID)
773 min_hopid = TB_PATH_MIN_HOPID;
775 if (max_hopid < 0 || max_hopid > port_max_hopid)
776 max_hopid = port_max_hopid;
778 return ida_simple_get(ida, min_hopid, max_hopid + 1, GFP_KERNEL);
782 * tb_port_alloc_in_hopid() - Allocate input HopID from port
783 * @port: Port to allocate HopID for
784 * @min_hopid: Minimum acceptable input HopID
785 * @max_hopid: Maximum acceptable input HopID
787 * Return: HopID between @min_hopid and @max_hopid or negative errno in
790 int tb_port_alloc_in_hopid(struct tb_port *port, int min_hopid, int max_hopid)
792 return tb_port_alloc_hopid(port, true, min_hopid, max_hopid);
796 * tb_port_alloc_out_hopid() - Allocate output HopID from port
797 * @port: Port to allocate HopID for
798 * @min_hopid: Minimum acceptable output HopID
799 * @max_hopid: Maximum acceptable output HopID
801 * Return: HopID between @min_hopid and @max_hopid or negative errno in
804 int tb_port_alloc_out_hopid(struct tb_port *port, int min_hopid, int max_hopid)
806 return tb_port_alloc_hopid(port, false, min_hopid, max_hopid);
810 * tb_port_release_in_hopid() - Release allocated input HopID from port
811 * @port: Port whose HopID to release
812 * @hopid: HopID to release
814 void tb_port_release_in_hopid(struct tb_port *port, int hopid)
816 ida_simple_remove(&port->in_hopids, hopid);
820 * tb_port_release_out_hopid() - Release allocated output HopID from port
821 * @port: Port whose HopID to release
822 * @hopid: HopID to release
824 void tb_port_release_out_hopid(struct tb_port *port, int hopid)
826 ida_simple_remove(&port->out_hopids, hopid);
829 static inline bool tb_switch_is_reachable(const struct tb_switch *parent,
830 const struct tb_switch *sw)
832 u64 mask = (1ULL << parent->config.depth * 8) - 1;
833 return (tb_route(parent) & mask) == (tb_route(sw) & mask);
837 * tb_next_port_on_path() - Return next port for given port on a path
838 * @start: Start port of the walk
839 * @end: End port of the walk
840 * @prev: Previous port (%NULL if this is the first)
842 * This function can be used to walk from one port to another if they
843 * are connected through zero or more switches. If the @prev is dual
844 * link port, the function follows that link and returns another end on
847 * If the @end port has been reached, return %NULL.
849 * Domain tb->lock must be held when this function is called.
851 struct tb_port *tb_next_port_on_path(struct tb_port *start, struct tb_port *end,
852 struct tb_port *prev)
854 struct tb_port *next;
859 if (prev->sw == end->sw) {
865 if (tb_switch_is_reachable(prev->sw, end->sw)) {
866 next = tb_port_at(tb_route(end->sw), prev->sw);
867 /* Walk down the topology if next == prev */
869 (next == prev || next->dual_link_port == prev))
872 if (tb_is_upstream_port(prev)) {
875 next = tb_upstream_port(prev->sw);
877 * Keep the same link if prev and next are both
880 if (next->dual_link_port &&
881 next->link_nr != prev->link_nr) {
882 next = next->dual_link_port;
887 return next != prev ? next : NULL;
891 * tb_port_get_link_speed() - Get current link speed
892 * @port: Port to check (USB4 or CIO)
894 * Returns link speed in Gb/s or negative errno in case of failure.
896 int tb_port_get_link_speed(struct tb_port *port)
904 ret = tb_port_read(port, &val, TB_CFG_PORT,
905 port->cap_phy + LANE_ADP_CS_1, 1);
909 speed = (val & LANE_ADP_CS_1_CURRENT_SPEED_MASK) >>
910 LANE_ADP_CS_1_CURRENT_SPEED_SHIFT;
911 return speed == LANE_ADP_CS_1_CURRENT_SPEED_GEN3 ? 20 : 10;
915 * tb_port_get_link_width() - Get current link width
916 * @port: Port to check (USB4 or CIO)
918 * Returns link width. Return values can be 1 (Single-Lane), 2 (Dual-Lane)
919 * or negative errno in case of failure.
921 int tb_port_get_link_width(struct tb_port *port)
929 ret = tb_port_read(port, &val, TB_CFG_PORT,
930 port->cap_phy + LANE_ADP_CS_1, 1);
934 return (val & LANE_ADP_CS_1_CURRENT_WIDTH_MASK) >>
935 LANE_ADP_CS_1_CURRENT_WIDTH_SHIFT;
938 static bool tb_port_is_width_supported(struct tb_port *port, int width)
946 ret = tb_port_read(port, &phy, TB_CFG_PORT,
947 port->cap_phy + LANE_ADP_CS_0, 1);
951 widths = (phy & LANE_ADP_CS_0_SUPPORTED_WIDTH_MASK) >>
952 LANE_ADP_CS_0_SUPPORTED_WIDTH_SHIFT;
954 return !!(widths & width);
957 static int tb_port_set_link_width(struct tb_port *port, unsigned int width)
965 ret = tb_port_read(port, &val, TB_CFG_PORT,
966 port->cap_phy + LANE_ADP_CS_1, 1);
970 val &= ~LANE_ADP_CS_1_TARGET_WIDTH_MASK;
973 val |= LANE_ADP_CS_1_TARGET_WIDTH_SINGLE <<
974 LANE_ADP_CS_1_TARGET_WIDTH_SHIFT;
977 val |= LANE_ADP_CS_1_TARGET_WIDTH_DUAL <<
978 LANE_ADP_CS_1_TARGET_WIDTH_SHIFT;
984 val |= LANE_ADP_CS_1_LB;
986 return tb_port_write(port, &val, TB_CFG_PORT,
987 port->cap_phy + LANE_ADP_CS_1, 1);
991 * tb_port_lane_bonding_enable() - Enable bonding on port
992 * @port: port to enable
994 * Enable bonding by setting the link width of the port and the
995 * other port in case of dual link port.
997 * Return: %0 in case of success and negative errno in case of error
999 int tb_port_lane_bonding_enable(struct tb_port *port)
1004 * Enable lane bonding for both links if not already enabled by
1005 * for example the boot firmware.
1007 ret = tb_port_get_link_width(port);
1009 ret = tb_port_set_link_width(port, 2);
1014 ret = tb_port_get_link_width(port->dual_link_port);
1016 ret = tb_port_set_link_width(port->dual_link_port, 2);
1018 tb_port_set_link_width(port, 1);
1023 port->bonded = true;
1024 port->dual_link_port->bonded = true;
1030 * tb_port_lane_bonding_disable() - Disable bonding on port
1031 * @port: port to disable
1033 * Disable bonding by setting the link width of the port and the
1034 * other port in case of dual link port.
1037 void tb_port_lane_bonding_disable(struct tb_port *port)
1039 port->dual_link_port->bonded = false;
1040 port->bonded = false;
1042 tb_port_set_link_width(port->dual_link_port, 1);
1043 tb_port_set_link_width(port, 1);
1046 static int tb_port_start_lane_initialization(struct tb_port *port)
1050 if (tb_switch_is_usb4(port->sw))
1053 ret = tb_lc_start_lane_initialization(port);
1054 return ret == -EINVAL ? 0 : ret;
1058 * tb_port_is_enabled() - Is the adapter port enabled
1059 * @port: Port to check
1061 bool tb_port_is_enabled(struct tb_port *port)
1063 switch (port->config.type) {
1064 case TB_TYPE_PCIE_UP:
1065 case TB_TYPE_PCIE_DOWN:
1066 return tb_pci_port_is_enabled(port);
1068 case TB_TYPE_DP_HDMI_IN:
1069 case TB_TYPE_DP_HDMI_OUT:
1070 return tb_dp_port_is_enabled(port);
1072 case TB_TYPE_USB3_UP:
1073 case TB_TYPE_USB3_DOWN:
1074 return tb_usb3_port_is_enabled(port);
1082 * tb_usb3_port_is_enabled() - Is the USB3 adapter port enabled
1083 * @port: USB3 adapter port to check
1085 bool tb_usb3_port_is_enabled(struct tb_port *port)
1089 if (tb_port_read(port, &data, TB_CFG_PORT,
1090 port->cap_adap + ADP_USB3_CS_0, 1))
1093 return !!(data & ADP_USB3_CS_0_PE);
1097 * tb_usb3_port_enable() - Enable USB3 adapter port
1098 * @port: USB3 adapter port to enable
1099 * @enable: Enable/disable the USB3 adapter
1101 int tb_usb3_port_enable(struct tb_port *port, bool enable)
1103 u32 word = enable ? (ADP_USB3_CS_0_PE | ADP_USB3_CS_0_V)
1106 if (!port->cap_adap)
1108 return tb_port_write(port, &word, TB_CFG_PORT,
1109 port->cap_adap + ADP_USB3_CS_0, 1);
1113 * tb_pci_port_is_enabled() - Is the PCIe adapter port enabled
1114 * @port: PCIe port to check
1116 bool tb_pci_port_is_enabled(struct tb_port *port)
1120 if (tb_port_read(port, &data, TB_CFG_PORT,
1121 port->cap_adap + ADP_PCIE_CS_0, 1))
1124 return !!(data & ADP_PCIE_CS_0_PE);
1128 * tb_pci_port_enable() - Enable PCIe adapter port
1129 * @port: PCIe port to enable
1130 * @enable: Enable/disable the PCIe adapter
1132 int tb_pci_port_enable(struct tb_port *port, bool enable)
1134 u32 word = enable ? ADP_PCIE_CS_0_PE : 0x0;
1135 if (!port->cap_adap)
1137 return tb_port_write(port, &word, TB_CFG_PORT,
1138 port->cap_adap + ADP_PCIE_CS_0, 1);
1142 * tb_dp_port_hpd_is_active() - Is HPD already active
1143 * @port: DP out port to check
1145 * Checks if the DP OUT adapter port has HDP bit already set.
1147 int tb_dp_port_hpd_is_active(struct tb_port *port)
1152 ret = tb_port_read(port, &data, TB_CFG_PORT,
1153 port->cap_adap + ADP_DP_CS_2, 1);
1157 return !!(data & ADP_DP_CS_2_HDP);
1161 * tb_dp_port_hpd_clear() - Clear HPD from DP IN port
1162 * @port: Port to clear HPD
1164 * If the DP IN port has HDP set, this function can be used to clear it.
1166 int tb_dp_port_hpd_clear(struct tb_port *port)
1171 ret = tb_port_read(port, &data, TB_CFG_PORT,
1172 port->cap_adap + ADP_DP_CS_3, 1);
1176 data |= ADP_DP_CS_3_HDPC;
1177 return tb_port_write(port, &data, TB_CFG_PORT,
1178 port->cap_adap + ADP_DP_CS_3, 1);
1182 * tb_dp_port_set_hops() - Set video/aux Hop IDs for DP port
1183 * @port: DP IN/OUT port to set hops
1184 * @video: Video Hop ID
1185 * @aux_tx: AUX TX Hop ID
1186 * @aux_rx: AUX RX Hop ID
1188 * Programs specified Hop IDs for DP IN/OUT port.
1190 int tb_dp_port_set_hops(struct tb_port *port, unsigned int video,
1191 unsigned int aux_tx, unsigned int aux_rx)
1196 ret = tb_port_read(port, data, TB_CFG_PORT,
1197 port->cap_adap + ADP_DP_CS_0, ARRAY_SIZE(data));
1201 data[0] &= ~ADP_DP_CS_0_VIDEO_HOPID_MASK;
1202 data[1] &= ~ADP_DP_CS_1_AUX_RX_HOPID_MASK;
1203 data[1] &= ~ADP_DP_CS_1_AUX_RX_HOPID_MASK;
1205 data[0] |= (video << ADP_DP_CS_0_VIDEO_HOPID_SHIFT) &
1206 ADP_DP_CS_0_VIDEO_HOPID_MASK;
1207 data[1] |= aux_tx & ADP_DP_CS_1_AUX_TX_HOPID_MASK;
1208 data[1] |= (aux_rx << ADP_DP_CS_1_AUX_RX_HOPID_SHIFT) &
1209 ADP_DP_CS_1_AUX_RX_HOPID_MASK;
1211 return tb_port_write(port, data, TB_CFG_PORT,
1212 port->cap_adap + ADP_DP_CS_0, ARRAY_SIZE(data));
1216 * tb_dp_port_is_enabled() - Is DP adapter port enabled
1217 * @port: DP adapter port to check
1219 bool tb_dp_port_is_enabled(struct tb_port *port)
1223 if (tb_port_read(port, data, TB_CFG_PORT, port->cap_adap + ADP_DP_CS_0,
1227 return !!(data[0] & (ADP_DP_CS_0_VE | ADP_DP_CS_0_AE));
1231 * tb_dp_port_enable() - Enables/disables DP paths of a port
1232 * @port: DP IN/OUT port
1233 * @enable: Enable/disable DP path
1235 * Once Hop IDs are programmed DP paths can be enabled or disabled by
1236 * calling this function.
1238 int tb_dp_port_enable(struct tb_port *port, bool enable)
1243 ret = tb_port_read(port, data, TB_CFG_PORT,
1244 port->cap_adap + ADP_DP_CS_0, ARRAY_SIZE(data));
1249 data[0] |= ADP_DP_CS_0_VE | ADP_DP_CS_0_AE;
1251 data[0] &= ~(ADP_DP_CS_0_VE | ADP_DP_CS_0_AE);
1253 return tb_port_write(port, data, TB_CFG_PORT,
1254 port->cap_adap + ADP_DP_CS_0, ARRAY_SIZE(data));
1257 /* switch utility functions */
1259 static const char *tb_switch_generation_name(const struct tb_switch *sw)
1261 switch (sw->generation) {
1263 return "Thunderbolt 1";
1265 return "Thunderbolt 2";
1267 return "Thunderbolt 3";
1275 static void tb_dump_switch(const struct tb *tb, const struct tb_switch *sw)
1277 const struct tb_regs_switch_header *regs = &sw->config;
1279 tb_dbg(tb, " %s Switch: %x:%x (Revision: %d, TB Version: %d)\n",
1280 tb_switch_generation_name(sw), regs->vendor_id, regs->device_id,
1281 regs->revision, regs->thunderbolt_version);
1282 tb_dbg(tb, " Max Port Number: %d\n", regs->max_port_number);
1283 tb_dbg(tb, " Config:\n");
1285 " Upstream Port Number: %d Depth: %d Route String: %#llx Enabled: %d, PlugEventsDelay: %dms\n",
1286 regs->upstream_port_number, regs->depth,
1287 (((u64) regs->route_hi) << 32) | regs->route_lo,
1288 regs->enabled, regs->plug_events_delay);
1289 tb_dbg(tb, " unknown1: %#x unknown4: %#x\n",
1290 regs->__unknown1, regs->__unknown4);
1294 * tb_switch_reset() - reconfigure route, enable and send TB_CFG_PKG_RESET
1295 * @sw: Switch to reset
1297 * Return: Returns 0 on success or an error code on failure.
1299 int tb_switch_reset(struct tb_switch *sw)
1301 struct tb_cfg_result res;
1303 if (sw->generation > 1)
1306 tb_sw_dbg(sw, "resetting switch\n");
1308 res.err = tb_sw_write(sw, ((u32 *) &sw->config) + 2,
1309 TB_CFG_SWITCH, 2, 2);
1312 res = tb_cfg_reset(sw->tb->ctl, tb_route(sw));
1319 * tb_plug_events_active() - enable/disable plug events on a switch
1321 * Also configures a sane plug_events_delay of 255ms.
1323 * Return: Returns 0 on success or an error code on failure.
1325 static int tb_plug_events_active(struct tb_switch *sw, bool active)
1330 if (tb_switch_is_icm(sw) || tb_switch_is_usb4(sw))
1333 sw->config.plug_events_delay = 0xff;
1334 res = tb_sw_write(sw, ((u32 *) &sw->config) + 4, TB_CFG_SWITCH, 4, 1);
1338 res = tb_sw_read(sw, &data, TB_CFG_SWITCH, sw->cap_plug_events + 1, 1);
1343 data = data & 0xFFFFFF83;
1344 switch (sw->config.device_id) {
1345 case PCI_DEVICE_ID_INTEL_LIGHT_RIDGE:
1346 case PCI_DEVICE_ID_INTEL_EAGLE_RIDGE:
1347 case PCI_DEVICE_ID_INTEL_PORT_RIDGE:
1355 return tb_sw_write(sw, &data, TB_CFG_SWITCH,
1356 sw->cap_plug_events + 1, 1);
1359 static ssize_t authorized_show(struct device *dev,
1360 struct device_attribute *attr,
1363 struct tb_switch *sw = tb_to_switch(dev);
1365 return sprintf(buf, "%u\n", sw->authorized);
1368 static int disapprove_switch(struct device *dev, void *not_used)
1370 struct tb_switch *sw;
1372 sw = tb_to_switch(dev);
1373 if (sw && sw->authorized) {
1376 /* First children */
1377 ret = device_for_each_child_reverse(&sw->dev, NULL, disapprove_switch);
1381 ret = tb_domain_disapprove_switch(sw->tb, sw);
1386 kobject_uevent(&sw->dev.kobj, KOBJ_CHANGE);
1392 static int tb_switch_set_authorized(struct tb_switch *sw, unsigned int val)
1396 if (!mutex_trylock(&sw->tb->lock))
1397 return restart_syscall();
1399 if (!!sw->authorized == !!val)
1403 /* Disapprove switch */
1406 ret = disapprove_switch(&sw->dev, NULL);
1411 /* Approve switch */
1414 ret = tb_domain_approve_switch_key(sw->tb, sw);
1416 ret = tb_domain_approve_switch(sw->tb, sw);
1419 /* Challenge switch */
1422 ret = tb_domain_challenge_switch_key(sw->tb, sw);
1430 sw->authorized = val;
1431 /* Notify status change to the userspace */
1432 kobject_uevent(&sw->dev.kobj, KOBJ_CHANGE);
1436 mutex_unlock(&sw->tb->lock);
1440 static ssize_t authorized_store(struct device *dev,
1441 struct device_attribute *attr,
1442 const char *buf, size_t count)
1444 struct tb_switch *sw = tb_to_switch(dev);
1448 ret = kstrtouint(buf, 0, &val);
1454 pm_runtime_get_sync(&sw->dev);
1455 ret = tb_switch_set_authorized(sw, val);
1456 pm_runtime_mark_last_busy(&sw->dev);
1457 pm_runtime_put_autosuspend(&sw->dev);
1459 return ret ? ret : count;
1461 static DEVICE_ATTR_RW(authorized);
1463 static ssize_t boot_show(struct device *dev, struct device_attribute *attr,
1466 struct tb_switch *sw = tb_to_switch(dev);
1468 return sprintf(buf, "%u\n", sw->boot);
1470 static DEVICE_ATTR_RO(boot);
1472 static ssize_t device_show(struct device *dev, struct device_attribute *attr,
1475 struct tb_switch *sw = tb_to_switch(dev);
1477 return sprintf(buf, "%#x\n", sw->device);
1479 static DEVICE_ATTR_RO(device);
1482 device_name_show(struct device *dev, struct device_attribute *attr, char *buf)
1484 struct tb_switch *sw = tb_to_switch(dev);
1486 return sprintf(buf, "%s\n", sw->device_name ? sw->device_name : "");
1488 static DEVICE_ATTR_RO(device_name);
1491 generation_show(struct device *dev, struct device_attribute *attr, char *buf)
1493 struct tb_switch *sw = tb_to_switch(dev);
1495 return sprintf(buf, "%u\n", sw->generation);
1497 static DEVICE_ATTR_RO(generation);
1499 static ssize_t key_show(struct device *dev, struct device_attribute *attr,
1502 struct tb_switch *sw = tb_to_switch(dev);
1505 if (!mutex_trylock(&sw->tb->lock))
1506 return restart_syscall();
1509 ret = sprintf(buf, "%*phN\n", TB_SWITCH_KEY_SIZE, sw->key);
1511 ret = sprintf(buf, "\n");
1513 mutex_unlock(&sw->tb->lock);
1517 static ssize_t key_store(struct device *dev, struct device_attribute *attr,
1518 const char *buf, size_t count)
1520 struct tb_switch *sw = tb_to_switch(dev);
1521 u8 key[TB_SWITCH_KEY_SIZE];
1522 ssize_t ret = count;
1525 if (!strcmp(buf, "\n"))
1527 else if (hex2bin(key, buf, sizeof(key)))
1530 if (!mutex_trylock(&sw->tb->lock))
1531 return restart_syscall();
1533 if (sw->authorized) {
1540 sw->key = kmemdup(key, sizeof(key), GFP_KERNEL);
1546 mutex_unlock(&sw->tb->lock);
1549 static DEVICE_ATTR(key, 0600, key_show, key_store);
1551 static ssize_t speed_show(struct device *dev, struct device_attribute *attr,
1554 struct tb_switch *sw = tb_to_switch(dev);
1556 return sprintf(buf, "%u.0 Gb/s\n", sw->link_speed);
1560 * Currently all lanes must run at the same speed but we expose here
1561 * both directions to allow possible asymmetric links in the future.
1563 static DEVICE_ATTR(rx_speed, 0444, speed_show, NULL);
1564 static DEVICE_ATTR(tx_speed, 0444, speed_show, NULL);
1566 static ssize_t lanes_show(struct device *dev, struct device_attribute *attr,
1569 struct tb_switch *sw = tb_to_switch(dev);
1571 return sprintf(buf, "%u\n", sw->link_width);
1575 * Currently link has same amount of lanes both directions (1 or 2) but
1576 * expose them separately to allow possible asymmetric links in the future.
1578 static DEVICE_ATTR(rx_lanes, 0444, lanes_show, NULL);
1579 static DEVICE_ATTR(tx_lanes, 0444, lanes_show, NULL);
1581 static ssize_t nvm_authenticate_show(struct device *dev,
1582 struct device_attribute *attr, char *buf)
1584 struct tb_switch *sw = tb_to_switch(dev);
1587 nvm_get_auth_status(sw, &status);
1588 return sprintf(buf, "%#x\n", status);
1591 static ssize_t nvm_authenticate_sysfs(struct device *dev, const char *buf,
1594 struct tb_switch *sw = tb_to_switch(dev);
1598 pm_runtime_get_sync(&sw->dev);
1600 if (!mutex_trylock(&sw->tb->lock)) {
1601 ret = restart_syscall();
1605 /* If NVMem devices are not yet added */
1611 ret = kstrtoint(buf, 10, &val);
1615 /* Always clear the authentication status */
1616 nvm_clear_auth_status(sw);
1619 if (!sw->nvm->flushed) {
1620 if (!sw->nvm->buf) {
1625 ret = nvm_validate_and_write(sw);
1626 if (ret || val == WRITE_ONLY)
1629 if (val == WRITE_AND_AUTHENTICATE) {
1631 ret = tb_lc_force_power(sw);
1633 sw->nvm->authenticating = true;
1634 ret = nvm_authenticate(sw);
1640 mutex_unlock(&sw->tb->lock);
1642 pm_runtime_mark_last_busy(&sw->dev);
1643 pm_runtime_put_autosuspend(&sw->dev);
1648 static ssize_t nvm_authenticate_store(struct device *dev,
1649 struct device_attribute *attr, const char *buf, size_t count)
1651 int ret = nvm_authenticate_sysfs(dev, buf, false);
1656 static DEVICE_ATTR_RW(nvm_authenticate);
1658 static ssize_t nvm_authenticate_on_disconnect_show(struct device *dev,
1659 struct device_attribute *attr, char *buf)
1661 return nvm_authenticate_show(dev, attr, buf);
1664 static ssize_t nvm_authenticate_on_disconnect_store(struct device *dev,
1665 struct device_attribute *attr, const char *buf, size_t count)
1669 ret = nvm_authenticate_sysfs(dev, buf, true);
1670 return ret ? ret : count;
1672 static DEVICE_ATTR_RW(nvm_authenticate_on_disconnect);
1674 static ssize_t nvm_version_show(struct device *dev,
1675 struct device_attribute *attr, char *buf)
1677 struct tb_switch *sw = tb_to_switch(dev);
1680 if (!mutex_trylock(&sw->tb->lock))
1681 return restart_syscall();
1688 ret = sprintf(buf, "%x.%x\n", sw->nvm->major, sw->nvm->minor);
1690 mutex_unlock(&sw->tb->lock);
1694 static DEVICE_ATTR_RO(nvm_version);
1696 static ssize_t vendor_show(struct device *dev, struct device_attribute *attr,
1699 struct tb_switch *sw = tb_to_switch(dev);
1701 return sprintf(buf, "%#x\n", sw->vendor);
1703 static DEVICE_ATTR_RO(vendor);
1706 vendor_name_show(struct device *dev, struct device_attribute *attr, char *buf)
1708 struct tb_switch *sw = tb_to_switch(dev);
1710 return sprintf(buf, "%s\n", sw->vendor_name ? sw->vendor_name : "");
1712 static DEVICE_ATTR_RO(vendor_name);
1714 static ssize_t unique_id_show(struct device *dev, struct device_attribute *attr,
1717 struct tb_switch *sw = tb_to_switch(dev);
1719 return sprintf(buf, "%pUb\n", sw->uuid);
1721 static DEVICE_ATTR_RO(unique_id);
1723 static struct attribute *switch_attrs[] = {
1724 &dev_attr_authorized.attr,
1725 &dev_attr_boot.attr,
1726 &dev_attr_device.attr,
1727 &dev_attr_device_name.attr,
1728 &dev_attr_generation.attr,
1730 &dev_attr_nvm_authenticate.attr,
1731 &dev_attr_nvm_authenticate_on_disconnect.attr,
1732 &dev_attr_nvm_version.attr,
1733 &dev_attr_rx_speed.attr,
1734 &dev_attr_rx_lanes.attr,
1735 &dev_attr_tx_speed.attr,
1736 &dev_attr_tx_lanes.attr,
1737 &dev_attr_vendor.attr,
1738 &dev_attr_vendor_name.attr,
1739 &dev_attr_unique_id.attr,
1743 static umode_t switch_attr_is_visible(struct kobject *kobj,
1744 struct attribute *attr, int n)
1746 struct device *dev = kobj_to_dev(kobj);
1747 struct tb_switch *sw = tb_to_switch(dev);
1749 if (attr == &dev_attr_authorized.attr) {
1750 if (sw->tb->security_level == TB_SECURITY_NOPCIE ||
1751 sw->tb->security_level == TB_SECURITY_DPONLY)
1753 } else if (attr == &dev_attr_device.attr) {
1756 } else if (attr == &dev_attr_device_name.attr) {
1757 if (!sw->device_name)
1759 } else if (attr == &dev_attr_vendor.attr) {
1762 } else if (attr == &dev_attr_vendor_name.attr) {
1763 if (!sw->vendor_name)
1765 } else if (attr == &dev_attr_key.attr) {
1767 sw->tb->security_level == TB_SECURITY_SECURE &&
1768 sw->security_level == TB_SECURITY_SECURE)
1771 } else if (attr == &dev_attr_rx_speed.attr ||
1772 attr == &dev_attr_rx_lanes.attr ||
1773 attr == &dev_attr_tx_speed.attr ||
1774 attr == &dev_attr_tx_lanes.attr) {
1778 } else if (attr == &dev_attr_nvm_authenticate.attr) {
1779 if (nvm_upgradeable(sw))
1782 } else if (attr == &dev_attr_nvm_version.attr) {
1783 if (nvm_readable(sw))
1786 } else if (attr == &dev_attr_boot.attr) {
1790 } else if (attr == &dev_attr_nvm_authenticate_on_disconnect.attr) {
1791 if (sw->quirks & QUIRK_FORCE_POWER_LINK_CONTROLLER)
1796 return sw->safe_mode ? 0 : attr->mode;
1799 static const struct attribute_group switch_group = {
1800 .is_visible = switch_attr_is_visible,
1801 .attrs = switch_attrs,
1804 static const struct attribute_group *switch_groups[] = {
1809 static void tb_switch_release(struct device *dev)
1811 struct tb_switch *sw = tb_to_switch(dev);
1812 struct tb_port *port;
1814 dma_port_free(sw->dma_port);
1816 tb_switch_for_each_port(sw, port) {
1817 ida_destroy(&port->in_hopids);
1818 ida_destroy(&port->out_hopids);
1822 kfree(sw->device_name);
1823 kfree(sw->vendor_name);
1830 static int tb_switch_uevent(struct device *dev, struct kobj_uevent_env *env)
1832 struct tb_switch *sw = tb_to_switch(dev);
1835 if (sw->config.thunderbolt_version == USB4_VERSION_1_0) {
1836 if (add_uevent_var(env, "USB4_VERSION=1.0"))
1840 if (!tb_route(sw)) {
1843 const struct tb_port *port;
1846 /* Device is hub if it has any downstream ports */
1847 tb_switch_for_each_port(sw, port) {
1848 if (!port->disabled && !tb_is_upstream_port(port) &&
1849 tb_port_is_null(port)) {
1855 type = hub ? "hub" : "device";
1858 if (add_uevent_var(env, "USB4_TYPE=%s", type))
1864 * Currently only need to provide the callbacks. Everything else is handled
1865 * in the connection manager.
1867 static int __maybe_unused tb_switch_runtime_suspend(struct device *dev)
1869 struct tb_switch *sw = tb_to_switch(dev);
1870 const struct tb_cm_ops *cm_ops = sw->tb->cm_ops;
1872 if (cm_ops->runtime_suspend_switch)
1873 return cm_ops->runtime_suspend_switch(sw);
1878 static int __maybe_unused tb_switch_runtime_resume(struct device *dev)
1880 struct tb_switch *sw = tb_to_switch(dev);
1881 const struct tb_cm_ops *cm_ops = sw->tb->cm_ops;
1883 if (cm_ops->runtime_resume_switch)
1884 return cm_ops->runtime_resume_switch(sw);
1888 static const struct dev_pm_ops tb_switch_pm_ops = {
1889 SET_RUNTIME_PM_OPS(tb_switch_runtime_suspend, tb_switch_runtime_resume,
1893 struct device_type tb_switch_type = {
1894 .name = "thunderbolt_device",
1895 .release = tb_switch_release,
1896 .uevent = tb_switch_uevent,
1897 .pm = &tb_switch_pm_ops,
1900 static int tb_switch_get_generation(struct tb_switch *sw)
1902 switch (sw->config.device_id) {
1903 case PCI_DEVICE_ID_INTEL_LIGHT_RIDGE:
1904 case PCI_DEVICE_ID_INTEL_EAGLE_RIDGE:
1905 case PCI_DEVICE_ID_INTEL_LIGHT_PEAK:
1906 case PCI_DEVICE_ID_INTEL_CACTUS_RIDGE_2C:
1907 case PCI_DEVICE_ID_INTEL_CACTUS_RIDGE_4C:
1908 case PCI_DEVICE_ID_INTEL_PORT_RIDGE:
1909 case PCI_DEVICE_ID_INTEL_REDWOOD_RIDGE_2C_BRIDGE:
1910 case PCI_DEVICE_ID_INTEL_REDWOOD_RIDGE_4C_BRIDGE:
1913 case PCI_DEVICE_ID_INTEL_WIN_RIDGE_2C_BRIDGE:
1914 case PCI_DEVICE_ID_INTEL_FALCON_RIDGE_2C_BRIDGE:
1915 case PCI_DEVICE_ID_INTEL_FALCON_RIDGE_4C_BRIDGE:
1918 case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_LP_BRIDGE:
1919 case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_2C_BRIDGE:
1920 case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_4C_BRIDGE:
1921 case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_C_2C_BRIDGE:
1922 case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_C_4C_BRIDGE:
1923 case PCI_DEVICE_ID_INTEL_TITAN_RIDGE_2C_BRIDGE:
1924 case PCI_DEVICE_ID_INTEL_TITAN_RIDGE_4C_BRIDGE:
1925 case PCI_DEVICE_ID_INTEL_TITAN_RIDGE_DD_BRIDGE:
1926 case PCI_DEVICE_ID_INTEL_ICL_NHI0:
1927 case PCI_DEVICE_ID_INTEL_ICL_NHI1:
1931 if (tb_switch_is_usb4(sw))
1935 * For unknown switches assume generation to be 1 to be
1938 tb_sw_warn(sw, "unsupported switch device id %#x\n",
1939 sw->config.device_id);
1944 static bool tb_switch_exceeds_max_depth(const struct tb_switch *sw, int depth)
1948 if (tb_switch_is_usb4(sw) ||
1949 (sw->tb->root_switch && tb_switch_is_usb4(sw->tb->root_switch)))
1950 max_depth = USB4_SWITCH_MAX_DEPTH;
1952 max_depth = TB_SWITCH_MAX_DEPTH;
1954 return depth > max_depth;
1958 * tb_switch_alloc() - allocate a switch
1959 * @tb: Pointer to the owning domain
1960 * @parent: Parent device for this switch
1961 * @route: Route string for this switch
1963 * Allocates and initializes a switch. Will not upload configuration to
1964 * the switch. For that you need to call tb_switch_configure()
1965 * separately. The returned switch should be released by calling
1968 * Return: Pointer to the allocated switch or ERR_PTR() in case of
1971 struct tb_switch *tb_switch_alloc(struct tb *tb, struct device *parent,
1974 struct tb_switch *sw;
1978 /* Unlock the downstream port so we can access the switch below */
1980 struct tb_switch *parent_sw = tb_to_switch(parent);
1981 struct tb_port *down;
1983 down = tb_port_at(route, parent_sw);
1984 tb_port_unlock(down);
1987 depth = tb_route_length(route);
1989 upstream_port = tb_cfg_get_upstream_port(tb->ctl, route);
1990 if (upstream_port < 0)
1991 return ERR_PTR(upstream_port);
1993 sw = kzalloc(sizeof(*sw), GFP_KERNEL);
1995 return ERR_PTR(-ENOMEM);
1998 ret = tb_cfg_read(tb->ctl, &sw->config, route, 0, TB_CFG_SWITCH, 0, 5);
2000 goto err_free_sw_ports;
2002 sw->generation = tb_switch_get_generation(sw);
2004 tb_dbg(tb, "current switch config:\n");
2005 tb_dump_switch(tb, sw);
2007 /* configure switch */
2008 sw->config.upstream_port_number = upstream_port;
2009 sw->config.depth = depth;
2010 sw->config.route_hi = upper_32_bits(route);
2011 sw->config.route_lo = lower_32_bits(route);
2012 sw->config.enabled = 0;
2014 /* Make sure we do not exceed maximum topology limit */
2015 if (tb_switch_exceeds_max_depth(sw, depth)) {
2016 ret = -EADDRNOTAVAIL;
2017 goto err_free_sw_ports;
2020 /* initialize ports */
2021 sw->ports = kcalloc(sw->config.max_port_number + 1, sizeof(*sw->ports),
2025 goto err_free_sw_ports;
2028 for (i = 0; i <= sw->config.max_port_number; i++) {
2029 /* minimum setup for tb_find_cap and tb_drom_read to work */
2030 sw->ports[i].sw = sw;
2031 sw->ports[i].port = i;
2033 /* Control port does not need HopID allocation */
2035 ida_init(&sw->ports[i].in_hopids);
2036 ida_init(&sw->ports[i].out_hopids);
2040 ret = tb_switch_find_vse_cap(sw, TB_VSE_CAP_PLUG_EVENTS);
2042 sw->cap_plug_events = ret;
2044 ret = tb_switch_find_vse_cap(sw, TB_VSE_CAP_LINK_CONTROLLER);
2048 /* Root switch is always authorized */
2050 sw->authorized = true;
2052 device_initialize(&sw->dev);
2053 sw->dev.parent = parent;
2054 sw->dev.bus = &tb_bus_type;
2055 sw->dev.type = &tb_switch_type;
2056 sw->dev.groups = switch_groups;
2057 dev_set_name(&sw->dev, "%u-%llx", tb->index, tb_route(sw));
2065 return ERR_PTR(ret);
2069 * tb_switch_alloc_safe_mode() - allocate a switch that is in safe mode
2070 * @tb: Pointer to the owning domain
2071 * @parent: Parent device for this switch
2072 * @route: Route string for this switch
2074 * This creates a switch in safe mode. This means the switch pretty much
2075 * lacks all capabilities except DMA configuration port before it is
2076 * flashed with a valid NVM firmware.
2078 * The returned switch must be released by calling tb_switch_put().
2080 * Return: Pointer to the allocated switch or ERR_PTR() in case of failure
2083 tb_switch_alloc_safe_mode(struct tb *tb, struct device *parent, u64 route)
2085 struct tb_switch *sw;
2087 sw = kzalloc(sizeof(*sw), GFP_KERNEL);
2089 return ERR_PTR(-ENOMEM);
2092 sw->config.depth = tb_route_length(route);
2093 sw->config.route_hi = upper_32_bits(route);
2094 sw->config.route_lo = lower_32_bits(route);
2095 sw->safe_mode = true;
2097 device_initialize(&sw->dev);
2098 sw->dev.parent = parent;
2099 sw->dev.bus = &tb_bus_type;
2100 sw->dev.type = &tb_switch_type;
2101 sw->dev.groups = switch_groups;
2102 dev_set_name(&sw->dev, "%u-%llx", tb->index, tb_route(sw));
2108 * tb_switch_configure() - Uploads configuration to the switch
2109 * @sw: Switch to configure
2111 * Call this function before the switch is added to the system. It will
2112 * upload configuration to the switch and makes it available for the
2113 * connection manager to use. Can be called to the switch again after
2114 * resume from low power states to re-initialize it.
2116 * Return: %0 in case of success and negative errno in case of failure
2118 int tb_switch_configure(struct tb_switch *sw)
2120 struct tb *tb = sw->tb;
2124 route = tb_route(sw);
2126 tb_dbg(tb, "%s Switch at %#llx (depth: %d, up port: %d)\n",
2127 sw->config.enabled ? "restoring" : "initializing", route,
2128 tb_route_length(route), sw->config.upstream_port_number);
2130 sw->config.enabled = 1;
2132 if (tb_switch_is_usb4(sw)) {
2134 * For USB4 devices, we need to program the CM version
2135 * accordingly so that it knows to expose all the
2136 * additional capabilities.
2138 sw->config.cmuv = USB4_VERSION_1_0;
2140 /* Enumerate the switch */
2141 ret = tb_sw_write(sw, (u32 *)&sw->config + 1, TB_CFG_SWITCH,
2146 ret = usb4_switch_setup(sw);
2148 if (sw->config.vendor_id != PCI_VENDOR_ID_INTEL)
2149 tb_sw_warn(sw, "unknown switch vendor id %#x\n",
2150 sw->config.vendor_id);
2152 if (!sw->cap_plug_events) {
2153 tb_sw_warn(sw, "cannot find TB_VSE_CAP_PLUG_EVENTS aborting\n");
2157 /* Enumerate the switch */
2158 ret = tb_sw_write(sw, (u32 *)&sw->config + 1, TB_CFG_SWITCH,
2164 return tb_plug_events_active(sw, true);
2167 static int tb_switch_set_uuid(struct tb_switch *sw)
2176 if (tb_switch_is_usb4(sw)) {
2177 ret = usb4_switch_read_uid(sw, &sw->uid);
2183 * The newer controllers include fused UUID as part of
2184 * link controller specific registers
2186 ret = tb_lc_read_uuid(sw, uuid);
2196 * ICM generates UUID based on UID and fills the upper
2197 * two words with ones. This is not strictly following
2198 * UUID format but we want to be compatible with it so
2199 * we do the same here.
2201 uuid[0] = sw->uid & 0xffffffff;
2202 uuid[1] = (sw->uid >> 32) & 0xffffffff;
2203 uuid[2] = 0xffffffff;
2204 uuid[3] = 0xffffffff;
2207 sw->uuid = kmemdup(uuid, sizeof(uuid), GFP_KERNEL);
2213 static int tb_switch_add_dma_port(struct tb_switch *sw)
2218 switch (sw->generation) {
2220 /* Only root switch can be upgraded */
2227 ret = tb_switch_set_uuid(sw);
2234 * DMA port is the only thing available when the switch
2242 if (sw->no_nvm_upgrade)
2245 if (tb_switch_is_usb4(sw)) {
2246 ret = usb4_switch_nvm_authenticate_status(sw, &status);
2251 tb_sw_info(sw, "switch flash authentication failed\n");
2252 nvm_set_auth_status(sw, status);
2258 /* Root switch DMA port requires running firmware */
2259 if (!tb_route(sw) && !tb_switch_is_icm(sw))
2262 sw->dma_port = dma_port_alloc(sw);
2267 * If there is status already set then authentication failed
2268 * when the dma_port_flash_update_auth() returned. Power cycling
2269 * is not needed (it was done already) so only thing we do here
2270 * is to unblock runtime PM of the root port.
2272 nvm_get_auth_status(sw, &status);
2275 nvm_authenticate_complete_dma_port(sw);
2280 * Check status of the previous flash authentication. If there
2281 * is one we need to power cycle the switch in any case to make
2282 * it functional again.
2284 ret = dma_port_flash_update_auth_status(sw->dma_port, &status);
2288 /* Now we can allow root port to suspend again */
2290 nvm_authenticate_complete_dma_port(sw);
2293 tb_sw_info(sw, "switch flash authentication failed\n");
2294 nvm_set_auth_status(sw, status);
2297 tb_sw_info(sw, "power cycling the switch now\n");
2298 dma_port_power_cycle(sw->dma_port);
2301 * We return error here which causes the switch adding failure.
2302 * It should appear back after power cycle is complete.
2307 static void tb_switch_default_link_ports(struct tb_switch *sw)
2311 for (i = 1; i <= sw->config.max_port_number; i += 2) {
2312 struct tb_port *port = &sw->ports[i];
2313 struct tb_port *subordinate;
2315 if (!tb_port_is_null(port))
2318 /* Check for the subordinate port */
2319 if (i == sw->config.max_port_number ||
2320 !tb_port_is_null(&sw->ports[i + 1]))
2323 /* Link them if not already done so (by DROM) */
2324 subordinate = &sw->ports[i + 1];
2325 if (!port->dual_link_port && !subordinate->dual_link_port) {
2327 port->dual_link_port = subordinate;
2328 subordinate->link_nr = 1;
2329 subordinate->dual_link_port = port;
2331 tb_sw_dbg(sw, "linked ports %d <-> %d\n",
2332 port->port, subordinate->port);
2337 static bool tb_switch_lane_bonding_possible(struct tb_switch *sw)
2339 const struct tb_port *up = tb_upstream_port(sw);
2341 if (!up->dual_link_port || !up->dual_link_port->remote)
2344 if (tb_switch_is_usb4(sw))
2345 return usb4_switch_lane_bonding_possible(sw);
2346 return tb_lc_lane_bonding_possible(sw);
2349 static int tb_switch_update_link_attributes(struct tb_switch *sw)
2352 bool change = false;
2355 if (!tb_route(sw) || tb_switch_is_icm(sw))
2358 up = tb_upstream_port(sw);
2360 ret = tb_port_get_link_speed(up);
2363 if (sw->link_speed != ret)
2365 sw->link_speed = ret;
2367 ret = tb_port_get_link_width(up);
2370 if (sw->link_width != ret)
2372 sw->link_width = ret;
2374 /* Notify userspace that there is possible link attribute change */
2375 if (device_is_registered(&sw->dev) && change)
2376 kobject_uevent(&sw->dev.kobj, KOBJ_CHANGE);
2382 * tb_switch_lane_bonding_enable() - Enable lane bonding
2383 * @sw: Switch to enable lane bonding
2385 * Connection manager can call this function to enable lane bonding of a
2386 * switch. If conditions are correct and both switches support the feature,
2387 * lanes are bonded. It is safe to call this to any switch.
2389 int tb_switch_lane_bonding_enable(struct tb_switch *sw)
2391 struct tb_switch *parent = tb_to_switch(sw->dev.parent);
2392 struct tb_port *up, *down;
2393 u64 route = tb_route(sw);
2399 if (!tb_switch_lane_bonding_possible(sw))
2402 up = tb_upstream_port(sw);
2403 down = tb_port_at(route, parent);
2405 if (!tb_port_is_width_supported(up, 2) ||
2406 !tb_port_is_width_supported(down, 2))
2409 ret = tb_port_lane_bonding_enable(up);
2411 tb_port_warn(up, "failed to enable lane bonding\n");
2415 ret = tb_port_lane_bonding_enable(down);
2417 tb_port_warn(down, "failed to enable lane bonding\n");
2418 tb_port_lane_bonding_disable(up);
2422 tb_switch_update_link_attributes(sw);
2424 tb_sw_dbg(sw, "lane bonding enabled\n");
2429 * tb_switch_lane_bonding_disable() - Disable lane bonding
2430 * @sw: Switch whose lane bonding to disable
2432 * Disables lane bonding between @sw and parent. This can be called even
2433 * if lanes were not bonded originally.
2435 void tb_switch_lane_bonding_disable(struct tb_switch *sw)
2437 struct tb_switch *parent = tb_to_switch(sw->dev.parent);
2438 struct tb_port *up, *down;
2443 up = tb_upstream_port(sw);
2447 down = tb_port_at(tb_route(sw), parent);
2449 tb_port_lane_bonding_disable(up);
2450 tb_port_lane_bonding_disable(down);
2452 tb_switch_update_link_attributes(sw);
2453 tb_sw_dbg(sw, "lane bonding disabled\n");
2457 * tb_switch_configure_link() - Set link configured
2458 * @sw: Switch whose link is configured
2460 * Sets the link upstream from @sw configured (from both ends) so that
2461 * it will not be disconnected when the domain exits sleep. Can be
2462 * called for any switch.
2464 * It is recommended that this is called after lane bonding is enabled.
2466 * Returns %0 on success and negative errno in case of error.
2468 int tb_switch_configure_link(struct tb_switch *sw)
2470 struct tb_port *up, *down;
2473 if (!tb_route(sw) || tb_switch_is_icm(sw))
2476 up = tb_upstream_port(sw);
2477 if (tb_switch_is_usb4(up->sw))
2478 ret = usb4_port_configure(up);
2480 ret = tb_lc_configure_port(up);
2485 if (tb_switch_is_usb4(down->sw))
2486 return usb4_port_configure(down);
2487 return tb_lc_configure_port(down);
2491 * tb_switch_unconfigure_link() - Unconfigure link
2492 * @sw: Switch whose link is unconfigured
2494 * Sets the link unconfigured so the @sw will be disconnected if the
2495 * domain exists sleep.
2497 void tb_switch_unconfigure_link(struct tb_switch *sw)
2499 struct tb_port *up, *down;
2501 if (sw->is_unplugged)
2503 if (!tb_route(sw) || tb_switch_is_icm(sw))
2506 up = tb_upstream_port(sw);
2507 if (tb_switch_is_usb4(up->sw))
2508 usb4_port_unconfigure(up);
2510 tb_lc_unconfigure_port(up);
2513 if (tb_switch_is_usb4(down->sw))
2514 usb4_port_unconfigure(down);
2516 tb_lc_unconfigure_port(down);
2520 * tb_switch_add() - Add a switch to the domain
2521 * @sw: Switch to add
2523 * This is the last step in adding switch to the domain. It will read
2524 * identification information from DROM and initializes ports so that
2525 * they can be used to connect other switches. The switch will be
2526 * exposed to the userspace when this function successfully returns. To
2527 * remove and release the switch, call tb_switch_remove().
2529 * Return: %0 in case of success and negative errno in case of failure
2531 int tb_switch_add(struct tb_switch *sw)
2536 * Initialize DMA control port now before we read DROM. Recent
2537 * host controllers have more complete DROM on NVM that includes
2538 * vendor and model identification strings which we then expose
2539 * to the userspace. NVM can be accessed through DMA
2540 * configuration based mailbox.
2542 ret = tb_switch_add_dma_port(sw);
2544 dev_err(&sw->dev, "failed to add DMA port\n");
2548 if (!sw->safe_mode) {
2550 ret = tb_drom_read(sw);
2552 dev_err(&sw->dev, "reading DROM failed\n");
2555 tb_sw_dbg(sw, "uid: %#llx\n", sw->uid);
2557 tb_check_quirks(sw);
2559 ret = tb_switch_set_uuid(sw);
2561 dev_err(&sw->dev, "failed to set UUID\n");
2565 for (i = 0; i <= sw->config.max_port_number; i++) {
2566 if (sw->ports[i].disabled) {
2567 tb_port_dbg(&sw->ports[i], "disabled by eeprom\n");
2570 ret = tb_init_port(&sw->ports[i]);
2572 dev_err(&sw->dev, "failed to initialize port %d\n", i);
2577 tb_switch_default_link_ports(sw);
2579 ret = tb_switch_update_link_attributes(sw);
2583 ret = tb_switch_tmu_init(sw);
2588 ret = device_add(&sw->dev);
2590 dev_err(&sw->dev, "failed to add device: %d\n", ret);
2595 dev_info(&sw->dev, "new device found, vendor=%#x device=%#x\n",
2596 sw->vendor, sw->device);
2597 if (sw->vendor_name && sw->device_name)
2598 dev_info(&sw->dev, "%s %s\n", sw->vendor_name,
2602 ret = tb_switch_nvm_add(sw);
2604 dev_err(&sw->dev, "failed to add NVM devices\n");
2605 device_del(&sw->dev);
2610 * Thunderbolt routers do not generate wakeups themselves but
2611 * they forward wakeups from tunneled protocols, so enable it
2614 device_init_wakeup(&sw->dev, true);
2616 pm_runtime_set_active(&sw->dev);
2618 pm_runtime_set_autosuspend_delay(&sw->dev, TB_AUTOSUSPEND_DELAY);
2619 pm_runtime_use_autosuspend(&sw->dev);
2620 pm_runtime_mark_last_busy(&sw->dev);
2621 pm_runtime_enable(&sw->dev);
2622 pm_request_autosuspend(&sw->dev);
2625 tb_switch_debugfs_init(sw);
2630 * tb_switch_remove() - Remove and release a switch
2631 * @sw: Switch to remove
2633 * This will remove the switch from the domain and release it after last
2634 * reference count drops to zero. If there are switches connected below
2635 * this switch, they will be removed as well.
2637 void tb_switch_remove(struct tb_switch *sw)
2639 struct tb_port *port;
2641 tb_switch_debugfs_remove(sw);
2644 pm_runtime_get_sync(&sw->dev);
2645 pm_runtime_disable(&sw->dev);
2648 /* port 0 is the switch itself and never has a remote */
2649 tb_switch_for_each_port(sw, port) {
2650 if (tb_port_has_remote(port)) {
2651 tb_switch_remove(port->remote->sw);
2652 port->remote = NULL;
2653 } else if (port->xdomain) {
2654 tb_xdomain_remove(port->xdomain);
2655 port->xdomain = NULL;
2658 /* Remove any downstream retimers */
2659 tb_retimer_remove_all(port);
2662 if (!sw->is_unplugged)
2663 tb_plug_events_active(sw, false);
2665 tb_switch_nvm_remove(sw);
2668 dev_info(&sw->dev, "device disconnected\n");
2669 device_unregister(&sw->dev);
2673 * tb_sw_set_unplugged() - set is_unplugged on switch and downstream switches
2674 * @sw: Router to mark unplugged
2676 void tb_sw_set_unplugged(struct tb_switch *sw)
2678 struct tb_port *port;
2680 if (sw == sw->tb->root_switch) {
2681 tb_sw_WARN(sw, "cannot unplug root switch\n");
2684 if (sw->is_unplugged) {
2685 tb_sw_WARN(sw, "is_unplugged already set\n");
2688 sw->is_unplugged = true;
2689 tb_switch_for_each_port(sw, port) {
2690 if (tb_port_has_remote(port))
2691 tb_sw_set_unplugged(port->remote->sw);
2692 else if (port->xdomain)
2693 port->xdomain->is_unplugged = true;
2697 static int tb_switch_set_wake(struct tb_switch *sw, unsigned int flags)
2700 tb_sw_dbg(sw, "enabling wakeup: %#x\n", flags);
2702 tb_sw_dbg(sw, "disabling wakeup\n");
2704 if (tb_switch_is_usb4(sw))
2705 return usb4_switch_set_wake(sw, flags);
2706 return tb_lc_set_wake(sw, flags);
2709 int tb_switch_resume(struct tb_switch *sw)
2711 struct tb_port *port;
2714 tb_sw_dbg(sw, "resuming switch\n");
2717 * Check for UID of the connected switches except for root
2718 * switch which we assume cannot be removed.
2724 * Check first that we can still read the switch config
2725 * space. It may be that there is now another domain
2728 err = tb_cfg_get_upstream_port(sw->tb->ctl, tb_route(sw));
2730 tb_sw_info(sw, "switch not present anymore\n");
2734 if (tb_switch_is_usb4(sw))
2735 err = usb4_switch_read_uid(sw, &uid);
2737 err = tb_drom_read_uid_only(sw, &uid);
2739 tb_sw_warn(sw, "uid read failed\n");
2742 if (sw->uid != uid) {
2744 "changed while suspended (uid %#llx -> %#llx)\n",
2750 err = tb_switch_configure(sw);
2755 tb_switch_set_wake(sw, 0);
2757 err = tb_switch_tmu_init(sw);
2761 /* check for surviving downstream switches */
2762 tb_switch_for_each_port(sw, port) {
2763 if (!tb_port_has_remote(port) && !port->xdomain) {
2765 * For disconnected downstream lane adapters
2766 * start lane initialization now so we detect
2769 if (!tb_is_upstream_port(port) && tb_port_is_null(port))
2770 tb_port_start_lane_initialization(port);
2772 } else if (port->xdomain) {
2774 * Start lane initialization for XDomain so the
2775 * link gets re-established.
2777 tb_port_start_lane_initialization(port);
2780 if (tb_wait_for_port(port, true) <= 0) {
2782 "lost during suspend, disconnecting\n");
2783 if (tb_port_has_remote(port))
2784 tb_sw_set_unplugged(port->remote->sw);
2785 else if (port->xdomain)
2786 port->xdomain->is_unplugged = true;
2787 } else if (tb_port_has_remote(port) || port->xdomain) {
2789 * Always unlock the port so the downstream
2790 * switch/domain is accessible.
2792 if (tb_port_unlock(port))
2793 tb_port_warn(port, "failed to unlock port\n");
2794 if (port->remote && tb_switch_resume(port->remote->sw)) {
2796 "lost during suspend, disconnecting\n");
2797 tb_sw_set_unplugged(port->remote->sw);
2805 * tb_switch_suspend() - Put a switch to sleep
2806 * @sw: Switch to suspend
2807 * @runtime: Is this runtime suspend or system sleep
2809 * Suspends router and all its children. Enables wakes according to
2810 * value of @runtime and then sets sleep bit for the router. If @sw is
2811 * host router the domain is ready to go to sleep once this function
2814 void tb_switch_suspend(struct tb_switch *sw, bool runtime)
2816 unsigned int flags = 0;
2817 struct tb_port *port;
2820 tb_sw_dbg(sw, "suspending switch\n");
2822 err = tb_plug_events_active(sw, false);
2826 tb_switch_for_each_port(sw, port) {
2827 if (tb_port_has_remote(port))
2828 tb_switch_suspend(port->remote->sw, runtime);
2832 /* Trigger wake when something is plugged in/out */
2833 flags |= TB_WAKE_ON_CONNECT | TB_WAKE_ON_DISCONNECT;
2834 flags |= TB_WAKE_ON_USB4 | TB_WAKE_ON_USB3 | TB_WAKE_ON_PCIE;
2835 } else if (device_may_wakeup(&sw->dev)) {
2836 flags |= TB_WAKE_ON_USB4 | TB_WAKE_ON_USB3 | TB_WAKE_ON_PCIE;
2839 tb_switch_set_wake(sw, flags);
2841 if (tb_switch_is_usb4(sw))
2842 usb4_switch_set_sleep(sw);
2844 tb_lc_set_sleep(sw);
2848 * tb_switch_query_dp_resource() - Query availability of DP resource
2849 * @sw: Switch whose DP resource is queried
2852 * Queries availability of DP resource for DP tunneling using switch
2853 * specific means. Returns %true if resource is available.
2855 bool tb_switch_query_dp_resource(struct tb_switch *sw, struct tb_port *in)
2857 if (tb_switch_is_usb4(sw))
2858 return usb4_switch_query_dp_resource(sw, in);
2859 return tb_lc_dp_sink_query(sw, in);
2863 * tb_switch_alloc_dp_resource() - Allocate available DP resource
2864 * @sw: Switch whose DP resource is allocated
2867 * Allocates DP resource for DP tunneling. The resource must be
2868 * available for this to succeed (see tb_switch_query_dp_resource()).
2869 * Returns %0 in success and negative errno otherwise.
2871 int tb_switch_alloc_dp_resource(struct tb_switch *sw, struct tb_port *in)
2873 if (tb_switch_is_usb4(sw))
2874 return usb4_switch_alloc_dp_resource(sw, in);
2875 return tb_lc_dp_sink_alloc(sw, in);
2879 * tb_switch_dealloc_dp_resource() - De-allocate DP resource
2880 * @sw: Switch whose DP resource is de-allocated
2883 * De-allocates DP resource that was previously allocated for DP
2886 void tb_switch_dealloc_dp_resource(struct tb_switch *sw, struct tb_port *in)
2890 if (tb_switch_is_usb4(sw))
2891 ret = usb4_switch_dealloc_dp_resource(sw, in);
2893 ret = tb_lc_dp_sink_dealloc(sw, in);
2896 tb_sw_warn(sw, "failed to de-allocate DP resource for port %d\n",
2900 struct tb_sw_lookup {
2908 static int tb_switch_match(struct device *dev, const void *data)
2910 struct tb_switch *sw = tb_to_switch(dev);
2911 const struct tb_sw_lookup *lookup = data;
2915 if (sw->tb != lookup->tb)
2919 return !memcmp(sw->uuid, lookup->uuid, sizeof(*lookup->uuid));
2921 if (lookup->route) {
2922 return sw->config.route_lo == lower_32_bits(lookup->route) &&
2923 sw->config.route_hi == upper_32_bits(lookup->route);
2926 /* Root switch is matched only by depth */
2930 return sw->link == lookup->link && sw->depth == lookup->depth;
2934 * tb_switch_find_by_link_depth() - Find switch by link and depth
2935 * @tb: Domain the switch belongs
2936 * @link: Link number the switch is connected
2937 * @depth: Depth of the switch in link
2939 * Returned switch has reference count increased so the caller needs to
2940 * call tb_switch_put() when done with the switch.
2942 struct tb_switch *tb_switch_find_by_link_depth(struct tb *tb, u8 link, u8 depth)
2944 struct tb_sw_lookup lookup;
2947 memset(&lookup, 0, sizeof(lookup));
2950 lookup.depth = depth;
2952 dev = bus_find_device(&tb_bus_type, NULL, &lookup, tb_switch_match);
2954 return tb_to_switch(dev);
2960 * tb_switch_find_by_uuid() - Find switch by UUID
2961 * @tb: Domain the switch belongs
2962 * @uuid: UUID to look for
2964 * Returned switch has reference count increased so the caller needs to
2965 * call tb_switch_put() when done with the switch.
2967 struct tb_switch *tb_switch_find_by_uuid(struct tb *tb, const uuid_t *uuid)
2969 struct tb_sw_lookup lookup;
2972 memset(&lookup, 0, sizeof(lookup));
2976 dev = bus_find_device(&tb_bus_type, NULL, &lookup, tb_switch_match);
2978 return tb_to_switch(dev);
2984 * tb_switch_find_by_route() - Find switch by route string
2985 * @tb: Domain the switch belongs
2986 * @route: Route string to look for
2988 * Returned switch has reference count increased so the caller needs to
2989 * call tb_switch_put() when done with the switch.
2991 struct tb_switch *tb_switch_find_by_route(struct tb *tb, u64 route)
2993 struct tb_sw_lookup lookup;
2997 return tb_switch_get(tb->root_switch);
2999 memset(&lookup, 0, sizeof(lookup));
3001 lookup.route = route;
3003 dev = bus_find_device(&tb_bus_type, NULL, &lookup, tb_switch_match);
3005 return tb_to_switch(dev);
3011 * tb_switch_find_port() - return the first port of @type on @sw or NULL
3012 * @sw: Switch to find the port from
3013 * @type: Port type to look for
3015 struct tb_port *tb_switch_find_port(struct tb_switch *sw,
3016 enum tb_port_type type)
3018 struct tb_port *port;
3020 tb_switch_for_each_port(sw, port) {
3021 if (port->config.type == type)