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 * Hold NVM authentication failure status per switch This information
31 * needs to stay around even when the switch gets power cycled so we
34 static LIST_HEAD(nvm_auth_status_cache);
35 static DEFINE_MUTEX(nvm_auth_status_lock);
37 static struct nvm_auth_status *__nvm_get_auth_status(const struct tb_switch *sw)
39 struct nvm_auth_status *st;
41 list_for_each_entry(st, &nvm_auth_status_cache, list) {
42 if (uuid_equal(&st->uuid, sw->uuid))
49 static void nvm_get_auth_status(const struct tb_switch *sw, u32 *status)
51 struct nvm_auth_status *st;
53 mutex_lock(&nvm_auth_status_lock);
54 st = __nvm_get_auth_status(sw);
55 mutex_unlock(&nvm_auth_status_lock);
57 *status = st ? st->status : 0;
60 static void nvm_set_auth_status(const struct tb_switch *sw, u32 status)
62 struct nvm_auth_status *st;
64 if (WARN_ON(!sw->uuid))
67 mutex_lock(&nvm_auth_status_lock);
68 st = __nvm_get_auth_status(sw);
71 st = kzalloc(sizeof(*st), GFP_KERNEL);
75 memcpy(&st->uuid, sw->uuid, sizeof(st->uuid));
76 INIT_LIST_HEAD(&st->list);
77 list_add_tail(&st->list, &nvm_auth_status_cache);
82 mutex_unlock(&nvm_auth_status_lock);
85 static void nvm_clear_auth_status(const struct tb_switch *sw)
87 struct nvm_auth_status *st;
89 mutex_lock(&nvm_auth_status_lock);
90 st = __nvm_get_auth_status(sw);
95 mutex_unlock(&nvm_auth_status_lock);
98 static int nvm_validate_and_write(struct tb_switch *sw)
100 unsigned int image_size, hdr_size;
101 const u8 *buf = sw->nvm->buf;
108 image_size = sw->nvm->buf_data_size;
109 if (image_size < NVM_MIN_SIZE || image_size > NVM_MAX_SIZE)
113 * FARB pointer must point inside the image and must at least
114 * contain parts of the digital section we will be reading here.
116 hdr_size = (*(u32 *)buf) & 0xffffff;
117 if (hdr_size + NVM_DEVID + 2 >= image_size)
120 /* Digital section start should be aligned to 4k page */
121 if (!IS_ALIGNED(hdr_size, SZ_4K))
125 * Read digital section size and check that it also fits inside
128 ds_size = *(u16 *)(buf + hdr_size);
129 if (ds_size >= image_size)
132 if (!sw->safe_mode) {
136 * Make sure the device ID in the image matches the one
137 * we read from the switch config space.
139 device_id = *(u16 *)(buf + hdr_size + NVM_DEVID);
140 if (device_id != sw->config.device_id)
143 if (sw->generation < 3) {
144 /* Write CSS headers first */
145 ret = dma_port_flash_write(sw->dma_port,
146 DMA_PORT_CSS_ADDRESS, buf + NVM_CSS,
147 DMA_PORT_CSS_MAX_SIZE);
152 /* Skip headers in the image */
154 image_size -= hdr_size;
157 if (tb_switch_is_usb4(sw))
158 ret = usb4_switch_nvm_write(sw, 0, buf, image_size);
160 ret = dma_port_flash_write(sw->dma_port, 0, buf, image_size);
162 sw->nvm->flushed = true;
166 static int nvm_authenticate_host_dma_port(struct tb_switch *sw)
171 * Root switch NVM upgrade requires that we disconnect the
172 * existing paths first (in case it is not in safe mode
175 if (!sw->safe_mode) {
178 ret = tb_domain_disconnect_all_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 if (!ret || ret == -ETIMEDOUT)
190 * Any error from update auth operation requires power
191 * cycling of the host router.
193 tb_sw_warn(sw, "failed to authenticate NVM, power cycling\n");
194 if (dma_port_flash_update_auth_status(sw->dma_port, &status) > 0)
195 nvm_set_auth_status(sw, status);
199 * From safe mode we can get out by just power cycling the
202 dma_port_power_cycle(sw->dma_port);
206 static int nvm_authenticate_device_dma_port(struct tb_switch *sw)
208 int ret, retries = 10;
210 ret = dma_port_flash_update_auth(sw->dma_port);
216 /* Power cycle is required */
223 * Poll here for the authentication status. It takes some time
224 * for the device to respond (we get timeout for a while). Once
225 * we get response the device needs to be power cycled in order
226 * to the new NVM to be taken into use.
231 ret = dma_port_flash_update_auth_status(sw->dma_port, &status);
232 if (ret < 0 && ret != -ETIMEDOUT)
236 tb_sw_warn(sw, "failed to authenticate NVM\n");
237 nvm_set_auth_status(sw, status);
240 tb_sw_info(sw, "power cycling the switch now\n");
241 dma_port_power_cycle(sw->dma_port);
251 static void nvm_authenticate_start_dma_port(struct tb_switch *sw)
253 struct pci_dev *root_port;
256 * During host router NVM upgrade we should not allow root port to
257 * go into D3cold because some root ports cannot trigger PME
258 * itself. To be on the safe side keep the root port in D0 during
259 * the whole upgrade process.
261 root_port = pcie_find_root_port(sw->tb->nhi->pdev);
263 pm_runtime_get_noresume(&root_port->dev);
266 static void nvm_authenticate_complete_dma_port(struct tb_switch *sw)
268 struct pci_dev *root_port;
270 root_port = pcie_find_root_port(sw->tb->nhi->pdev);
272 pm_runtime_put(&root_port->dev);
275 static inline bool nvm_readable(struct tb_switch *sw)
277 if (tb_switch_is_usb4(sw)) {
279 * USB4 devices must support NVM operations but it is
280 * optional for hosts. Therefore we query the NVM sector
281 * size here and if it is supported assume NVM
282 * operations are implemented.
284 return usb4_switch_nvm_sector_size(sw) > 0;
287 /* Thunderbolt 2 and 3 devices support NVM through DMA port */
288 return !!sw->dma_port;
291 static inline bool nvm_upgradeable(struct tb_switch *sw)
293 if (sw->no_nvm_upgrade)
295 return nvm_readable(sw);
298 static inline int nvm_read(struct tb_switch *sw, unsigned int address,
299 void *buf, size_t size)
301 if (tb_switch_is_usb4(sw))
302 return usb4_switch_nvm_read(sw, address, buf, size);
303 return dma_port_flash_read(sw->dma_port, address, buf, size);
306 static int nvm_authenticate(struct tb_switch *sw, bool auth_only)
310 if (tb_switch_is_usb4(sw)) {
312 ret = usb4_switch_nvm_set_offset(sw, 0);
316 sw->nvm->authenticating = true;
317 return usb4_switch_nvm_authenticate(sw);
318 } else if (auth_only) {
322 sw->nvm->authenticating = true;
324 nvm_authenticate_start_dma_port(sw);
325 ret = nvm_authenticate_host_dma_port(sw);
327 ret = nvm_authenticate_device_dma_port(sw);
333 static int tb_switch_nvm_read(void *priv, unsigned int offset, void *val,
336 struct tb_nvm *nvm = priv;
337 struct tb_switch *sw = tb_to_switch(nvm->dev);
340 pm_runtime_get_sync(&sw->dev);
342 if (!mutex_trylock(&sw->tb->lock)) {
343 ret = restart_syscall();
347 ret = nvm_read(sw, offset, val, bytes);
348 mutex_unlock(&sw->tb->lock);
351 pm_runtime_mark_last_busy(&sw->dev);
352 pm_runtime_put_autosuspend(&sw->dev);
357 static int tb_switch_nvm_write(void *priv, unsigned int offset, void *val,
360 struct tb_nvm *nvm = priv;
361 struct tb_switch *sw = tb_to_switch(nvm->dev);
364 if (!mutex_trylock(&sw->tb->lock))
365 return restart_syscall();
368 * Since writing the NVM image might require some special steps,
369 * for example when CSS headers are written, we cache the image
370 * locally here and handle the special cases when the user asks
371 * us to authenticate the image.
373 ret = tb_nvm_write_buf(nvm, offset, val, bytes);
374 mutex_unlock(&sw->tb->lock);
379 static int tb_switch_nvm_add(struct tb_switch *sw)
385 if (!nvm_readable(sw))
389 * The NVM format of non-Intel hardware is not known so
390 * currently restrict NVM upgrade for Intel hardware. We may
391 * relax this in the future when we learn other NVM formats.
393 if (sw->config.vendor_id != PCI_VENDOR_ID_INTEL &&
394 sw->config.vendor_id != 0x8087) {
396 "NVM format of vendor %#x is not known, disabling NVM upgrade\n",
397 sw->config.vendor_id);
401 nvm = tb_nvm_alloc(&sw->dev);
406 * If the switch is in safe-mode the only accessible portion of
407 * the NVM is the non-active one where userspace is expected to
408 * write new functional NVM.
410 if (!sw->safe_mode) {
411 u32 nvm_size, hdr_size;
413 ret = nvm_read(sw, NVM_FLASH_SIZE, &val, sizeof(val));
417 hdr_size = sw->generation < 3 ? SZ_8K : SZ_16K;
418 nvm_size = (SZ_1M << (val & 7)) / 8;
419 nvm_size = (nvm_size - hdr_size) / 2;
421 ret = nvm_read(sw, NVM_VERSION, &val, sizeof(val));
425 nvm->major = val >> 16;
426 nvm->minor = val >> 8;
428 ret = tb_nvm_add_active(nvm, nvm_size, tb_switch_nvm_read);
433 if (!sw->no_nvm_upgrade) {
434 ret = tb_nvm_add_non_active(nvm, NVM_MAX_SIZE,
435 tb_switch_nvm_write);
448 static void tb_switch_nvm_remove(struct tb_switch *sw)
458 /* Remove authentication status in case the switch is unplugged */
459 if (!nvm->authenticating)
460 nvm_clear_auth_status(sw);
465 /* port utility functions */
467 static const char *tb_port_type(const struct tb_regs_port_header *port)
469 switch (port->type >> 16) {
471 switch ((u8) port->type) {
496 static void tb_dump_port(struct tb *tb, const struct tb_port *port)
498 const struct tb_regs_port_header *regs = &port->config;
501 " Port %d: %x:%x (Revision: %d, TB Version: %d, Type: %s (%#x))\n",
502 regs->port_number, regs->vendor_id, regs->device_id,
503 regs->revision, regs->thunderbolt_version, tb_port_type(regs),
505 tb_dbg(tb, " Max hop id (in/out): %d/%d\n",
506 regs->max_in_hop_id, regs->max_out_hop_id);
507 tb_dbg(tb, " Max counters: %d\n", regs->max_counters);
508 tb_dbg(tb, " NFC Credits: %#x\n", regs->nfc_credits);
509 tb_dbg(tb, " Credits (total/control): %u/%u\n", port->total_credits,
514 * tb_port_state() - get connectedness state of a port
515 * @port: the port to check
517 * The port must have a TB_CAP_PHY (i.e. it should be a real port).
519 * Return: Returns an enum tb_port_state on success or an error code on failure.
521 int tb_port_state(struct tb_port *port)
523 struct tb_cap_phy phy;
525 if (port->cap_phy == 0) {
526 tb_port_WARN(port, "does not have a PHY\n");
529 res = tb_port_read(port, &phy, TB_CFG_PORT, port->cap_phy, 2);
536 * tb_wait_for_port() - wait for a port to become ready
537 * @port: Port to wait
538 * @wait_if_unplugged: Wait also when port is unplugged
540 * Wait up to 1 second for a port to reach state TB_PORT_UP. If
541 * wait_if_unplugged is set then we also wait if the port is in state
542 * TB_PORT_UNPLUGGED (it takes a while for the device to be registered after
543 * switch resume). Otherwise we only wait if a device is registered but the link
544 * has not yet been established.
546 * Return: Returns an error code on failure. Returns 0 if the port is not
547 * connected or failed to reach state TB_PORT_UP within one second. Returns 1
548 * if the port is connected and in state TB_PORT_UP.
550 int tb_wait_for_port(struct tb_port *port, bool wait_if_unplugged)
554 if (!port->cap_phy) {
555 tb_port_WARN(port, "does not have PHY\n");
558 if (tb_is_upstream_port(port)) {
559 tb_port_WARN(port, "is the upstream port\n");
564 state = tb_port_state(port);
567 if (state == TB_PORT_DISABLED) {
568 tb_port_dbg(port, "is disabled (state: 0)\n");
571 if (state == TB_PORT_UNPLUGGED) {
572 if (wait_if_unplugged) {
573 /* used during resume */
575 "is unplugged (state: 7), retrying...\n");
579 tb_port_dbg(port, "is unplugged (state: 7)\n");
582 if (state == TB_PORT_UP) {
583 tb_port_dbg(port, "is connected, link is up (state: 2)\n");
588 * After plug-in the state is TB_PORT_CONNECTING. Give it some
592 "is connected, link is not up (state: %d), retrying...\n",
597 "failed to reach state TB_PORT_UP. Ignoring port...\n");
602 * tb_port_add_nfc_credits() - add/remove non flow controlled credits to port
603 * @port: Port to add/remove NFC credits
604 * @credits: Credits to add/remove
606 * Change the number of NFC credits allocated to @port by @credits. To remove
607 * NFC credits pass a negative amount of credits.
609 * Return: Returns 0 on success or an error code on failure.
611 int tb_port_add_nfc_credits(struct tb_port *port, int credits)
615 if (credits == 0 || port->sw->is_unplugged)
619 * USB4 restricts programming NFC buffers to lane adapters only
620 * so skip other ports.
622 if (tb_switch_is_usb4(port->sw) && !tb_port_is_null(port))
625 nfc_credits = port->config.nfc_credits & ADP_CS_4_NFC_BUFFERS_MASK;
626 nfc_credits += credits;
628 tb_port_dbg(port, "adding %d NFC credits to %lu", credits,
629 port->config.nfc_credits & ADP_CS_4_NFC_BUFFERS_MASK);
631 port->config.nfc_credits &= ~ADP_CS_4_NFC_BUFFERS_MASK;
632 port->config.nfc_credits |= nfc_credits;
634 return tb_port_write(port, &port->config.nfc_credits,
635 TB_CFG_PORT, ADP_CS_4, 1);
639 * tb_port_clear_counter() - clear a counter in TB_CFG_COUNTER
640 * @port: Port whose counters to clear
641 * @counter: Counter index to clear
643 * Return: Returns 0 on success or an error code on failure.
645 int tb_port_clear_counter(struct tb_port *port, int counter)
647 u32 zero[3] = { 0, 0, 0 };
648 tb_port_dbg(port, "clearing counter %d\n", counter);
649 return tb_port_write(port, zero, TB_CFG_COUNTERS, 3 * counter, 3);
653 * tb_port_unlock() - Unlock downstream port
654 * @port: Port to unlock
656 * Needed for USB4 but can be called for any CIO/USB4 ports. Makes the
657 * downstream router accessible for CM.
659 int tb_port_unlock(struct tb_port *port)
661 if (tb_switch_is_icm(port->sw))
663 if (!tb_port_is_null(port))
665 if (tb_switch_is_usb4(port->sw))
666 return usb4_port_unlock(port);
670 static int __tb_port_enable(struct tb_port *port, bool enable)
675 if (!tb_port_is_null(port))
678 ret = tb_port_read(port, &phy, TB_CFG_PORT,
679 port->cap_phy + LANE_ADP_CS_1, 1);
684 phy &= ~LANE_ADP_CS_1_LD;
686 phy |= LANE_ADP_CS_1_LD;
688 return tb_port_write(port, &phy, TB_CFG_PORT,
689 port->cap_phy + LANE_ADP_CS_1, 1);
693 * tb_port_enable() - Enable lane adapter
694 * @port: Port to enable (can be %NULL)
696 * This is used for lane 0 and 1 adapters to enable it.
698 int tb_port_enable(struct tb_port *port)
700 return __tb_port_enable(port, true);
704 * tb_port_disable() - Disable lane adapter
705 * @port: Port to disable (can be %NULL)
707 * This is used for lane 0 and 1 adapters to disable it.
709 int tb_port_disable(struct tb_port *port)
711 return __tb_port_enable(port, false);
715 * tb_init_port() - initialize a port
717 * This is a helper method for tb_switch_alloc. Does not check or initialize
718 * any downstream switches.
720 * Return: Returns 0 on success or an error code on failure.
722 static int tb_init_port(struct tb_port *port)
727 res = tb_port_read(port, &port->config, TB_CFG_PORT, 0, 8);
729 if (res == -ENODEV) {
730 tb_dbg(port->sw->tb, " Port %d: not implemented\n",
732 port->disabled = true;
738 /* Port 0 is the switch itself and has no PHY. */
739 if (port->config.type == TB_TYPE_PORT && port->port != 0) {
740 cap = tb_port_find_cap(port, TB_PORT_CAP_PHY);
745 tb_port_WARN(port, "non switch port without a PHY\n");
747 cap = tb_port_find_cap(port, TB_PORT_CAP_USB4);
749 port->cap_usb4 = cap;
752 * USB4 ports the buffers allocated for the control path
753 * can be read from the path config space. Legacy
754 * devices we use hard-coded value.
756 if (tb_switch_is_usb4(port->sw)) {
757 struct tb_regs_hop hop;
759 if (!tb_port_read(port, &hop, TB_CFG_HOPS, 0, 2))
760 port->ctl_credits = hop.initial_credits;
762 if (!port->ctl_credits)
763 port->ctl_credits = 2;
765 } else if (port->port != 0) {
766 cap = tb_port_find_cap(port, TB_PORT_CAP_ADAP);
768 port->cap_adap = cap;
771 port->total_credits =
772 (port->config.nfc_credits & ADP_CS_4_TOTAL_BUFFERS_MASK) >>
773 ADP_CS_4_TOTAL_BUFFERS_SHIFT;
775 tb_dump_port(port->sw->tb, port);
777 INIT_LIST_HEAD(&port->list);
782 static int tb_port_alloc_hopid(struct tb_port *port, bool in, int min_hopid,
789 port_max_hopid = port->config.max_in_hop_id;
790 ida = &port->in_hopids;
792 port_max_hopid = port->config.max_out_hop_id;
793 ida = &port->out_hopids;
797 * NHI can use HopIDs 1-max for other adapters HopIDs 0-7 are
800 if (!tb_port_is_nhi(port) && min_hopid < TB_PATH_MIN_HOPID)
801 min_hopid = TB_PATH_MIN_HOPID;
803 if (max_hopid < 0 || max_hopid > port_max_hopid)
804 max_hopid = port_max_hopid;
806 return ida_simple_get(ida, min_hopid, max_hopid + 1, GFP_KERNEL);
810 * tb_port_alloc_in_hopid() - Allocate input HopID from port
811 * @port: Port to allocate HopID for
812 * @min_hopid: Minimum acceptable input HopID
813 * @max_hopid: Maximum acceptable input HopID
815 * Return: HopID between @min_hopid and @max_hopid or negative errno in
818 int tb_port_alloc_in_hopid(struct tb_port *port, int min_hopid, int max_hopid)
820 return tb_port_alloc_hopid(port, true, min_hopid, max_hopid);
824 * tb_port_alloc_out_hopid() - Allocate output HopID from port
825 * @port: Port to allocate HopID for
826 * @min_hopid: Minimum acceptable output HopID
827 * @max_hopid: Maximum acceptable output HopID
829 * Return: HopID between @min_hopid and @max_hopid or negative errno in
832 int tb_port_alloc_out_hopid(struct tb_port *port, int min_hopid, int max_hopid)
834 return tb_port_alloc_hopid(port, false, min_hopid, max_hopid);
838 * tb_port_release_in_hopid() - Release allocated input HopID from port
839 * @port: Port whose HopID to release
840 * @hopid: HopID to release
842 void tb_port_release_in_hopid(struct tb_port *port, int hopid)
844 ida_simple_remove(&port->in_hopids, hopid);
848 * tb_port_release_out_hopid() - Release allocated output HopID from port
849 * @port: Port whose HopID to release
850 * @hopid: HopID to release
852 void tb_port_release_out_hopid(struct tb_port *port, int hopid)
854 ida_simple_remove(&port->out_hopids, hopid);
857 static inline bool tb_switch_is_reachable(const struct tb_switch *parent,
858 const struct tb_switch *sw)
860 u64 mask = (1ULL << parent->config.depth * 8) - 1;
861 return (tb_route(parent) & mask) == (tb_route(sw) & mask);
865 * tb_next_port_on_path() - Return next port for given port on a path
866 * @start: Start port of the walk
867 * @end: End port of the walk
868 * @prev: Previous port (%NULL if this is the first)
870 * This function can be used to walk from one port to another if they
871 * are connected through zero or more switches. If the @prev is dual
872 * link port, the function follows that link and returns another end on
875 * If the @end port has been reached, return %NULL.
877 * Domain tb->lock must be held when this function is called.
879 struct tb_port *tb_next_port_on_path(struct tb_port *start, struct tb_port *end,
880 struct tb_port *prev)
882 struct tb_port *next;
887 if (prev->sw == end->sw) {
893 if (tb_switch_is_reachable(prev->sw, end->sw)) {
894 next = tb_port_at(tb_route(end->sw), prev->sw);
895 /* Walk down the topology if next == prev */
897 (next == prev || next->dual_link_port == prev))
900 if (tb_is_upstream_port(prev)) {
903 next = tb_upstream_port(prev->sw);
905 * Keep the same link if prev and next are both
908 if (next->dual_link_port &&
909 next->link_nr != prev->link_nr) {
910 next = next->dual_link_port;
915 return next != prev ? next : NULL;
919 * tb_port_get_link_speed() - Get current link speed
920 * @port: Port to check (USB4 or CIO)
922 * Returns link speed in Gb/s or negative errno in case of failure.
924 int tb_port_get_link_speed(struct tb_port *port)
932 ret = tb_port_read(port, &val, TB_CFG_PORT,
933 port->cap_phy + LANE_ADP_CS_1, 1);
937 speed = (val & LANE_ADP_CS_1_CURRENT_SPEED_MASK) >>
938 LANE_ADP_CS_1_CURRENT_SPEED_SHIFT;
939 return speed == LANE_ADP_CS_1_CURRENT_SPEED_GEN3 ? 20 : 10;
943 * tb_port_get_link_width() - Get current link width
944 * @port: Port to check (USB4 or CIO)
946 * Returns link width. Return values can be 1 (Single-Lane), 2 (Dual-Lane)
947 * or negative errno in case of failure.
949 int tb_port_get_link_width(struct tb_port *port)
957 ret = tb_port_read(port, &val, TB_CFG_PORT,
958 port->cap_phy + LANE_ADP_CS_1, 1);
962 return (val & LANE_ADP_CS_1_CURRENT_WIDTH_MASK) >>
963 LANE_ADP_CS_1_CURRENT_WIDTH_SHIFT;
966 static bool tb_port_is_width_supported(struct tb_port *port, int width)
974 ret = tb_port_read(port, &phy, TB_CFG_PORT,
975 port->cap_phy + LANE_ADP_CS_0, 1);
979 widths = (phy & LANE_ADP_CS_0_SUPPORTED_WIDTH_MASK) >>
980 LANE_ADP_CS_0_SUPPORTED_WIDTH_SHIFT;
982 return !!(widths & width);
985 static int tb_port_set_link_width(struct tb_port *port, unsigned int width)
993 ret = tb_port_read(port, &val, TB_CFG_PORT,
994 port->cap_phy + LANE_ADP_CS_1, 1);
998 val &= ~LANE_ADP_CS_1_TARGET_WIDTH_MASK;
1001 val |= LANE_ADP_CS_1_TARGET_WIDTH_SINGLE <<
1002 LANE_ADP_CS_1_TARGET_WIDTH_SHIFT;
1005 val |= LANE_ADP_CS_1_TARGET_WIDTH_DUAL <<
1006 LANE_ADP_CS_1_TARGET_WIDTH_SHIFT;
1012 val |= LANE_ADP_CS_1_LB;
1014 return tb_port_write(port, &val, TB_CFG_PORT,
1015 port->cap_phy + LANE_ADP_CS_1, 1);
1019 * tb_port_lane_bonding_enable() - Enable bonding on port
1020 * @port: port to enable
1022 * Enable bonding by setting the link width of the port and the other
1023 * port in case of dual link port. Does not wait for the link to
1024 * actually reach the bonded state so caller needs to call
1025 * tb_port_wait_for_link_width() before enabling any paths through the
1026 * link to make sure the link is in expected state.
1028 * Return: %0 in case of success and negative errno in case of error
1030 int tb_port_lane_bonding_enable(struct tb_port *port)
1035 * Enable lane bonding for both links if not already enabled by
1036 * for example the boot firmware.
1038 ret = tb_port_get_link_width(port);
1040 ret = tb_port_set_link_width(port, 2);
1045 ret = tb_port_get_link_width(port->dual_link_port);
1047 ret = tb_port_set_link_width(port->dual_link_port, 2);
1049 tb_port_set_link_width(port, 1);
1054 port->bonded = true;
1055 port->dual_link_port->bonded = true;
1061 * tb_port_lane_bonding_disable() - Disable bonding on port
1062 * @port: port to disable
1064 * Disable bonding by setting the link width of the port and the
1065 * other port in case of dual link port.
1068 void tb_port_lane_bonding_disable(struct tb_port *port)
1070 port->dual_link_port->bonded = false;
1071 port->bonded = false;
1073 tb_port_set_link_width(port->dual_link_port, 1);
1074 tb_port_set_link_width(port, 1);
1078 * tb_port_wait_for_link_width() - Wait until link reaches specific width
1079 * @port: Port to wait for
1080 * @width: Expected link width (%1 or %2)
1081 * @timeout_msec: Timeout in ms how long to wait
1083 * Should be used after both ends of the link have been bonded (or
1084 * bonding has been disabled) to wait until the link actually reaches
1085 * the expected state. Returns %-ETIMEDOUT if the @width was not reached
1086 * within the given timeout, %0 if it did.
1088 int tb_port_wait_for_link_width(struct tb_port *port, int width,
1091 ktime_t timeout = ktime_add_ms(ktime_get(), timeout_msec);
1095 ret = tb_port_get_link_width(port);
1098 else if (ret == width)
1101 usleep_range(1000, 2000);
1102 } while (ktime_before(ktime_get(), timeout));
1107 static int tb_port_do_update_credits(struct tb_port *port)
1112 ret = tb_port_read(port, &nfc_credits, TB_CFG_PORT, ADP_CS_4, 1);
1116 if (nfc_credits != port->config.nfc_credits) {
1119 total = (nfc_credits & ADP_CS_4_TOTAL_BUFFERS_MASK) >>
1120 ADP_CS_4_TOTAL_BUFFERS_SHIFT;
1122 tb_port_dbg(port, "total credits changed %u -> %u\n",
1123 port->total_credits, total);
1125 port->config.nfc_credits = nfc_credits;
1126 port->total_credits = total;
1133 * tb_port_update_credits() - Re-read port total credits
1134 * @port: Port to update
1136 * After the link is bonded (or bonding was disabled) the port total
1137 * credits may change, so this function needs to be called to re-read
1138 * the credits. Updates also the second lane adapter.
1140 int tb_port_update_credits(struct tb_port *port)
1144 ret = tb_port_do_update_credits(port);
1147 return tb_port_do_update_credits(port->dual_link_port);
1150 static int tb_port_start_lane_initialization(struct tb_port *port)
1154 if (tb_switch_is_usb4(port->sw))
1157 ret = tb_lc_start_lane_initialization(port);
1158 return ret == -EINVAL ? 0 : ret;
1162 * Returns true if the port had something (router, XDomain) connected
1165 static bool tb_port_resume(struct tb_port *port)
1167 bool has_remote = tb_port_has_remote(port);
1170 usb4_port_device_resume(port->usb4);
1171 } else if (!has_remote) {
1173 * For disconnected downstream lane adapters start lane
1174 * initialization now so we detect future connects.
1176 * For XDomain start the lane initialzation now so the
1177 * link gets re-established.
1179 * This is only needed for non-USB4 ports.
1181 if (!tb_is_upstream_port(port) || port->xdomain)
1182 tb_port_start_lane_initialization(port);
1185 return has_remote || port->xdomain;
1189 * tb_port_is_enabled() - Is the adapter port enabled
1190 * @port: Port to check
1192 bool tb_port_is_enabled(struct tb_port *port)
1194 switch (port->config.type) {
1195 case TB_TYPE_PCIE_UP:
1196 case TB_TYPE_PCIE_DOWN:
1197 return tb_pci_port_is_enabled(port);
1199 case TB_TYPE_DP_HDMI_IN:
1200 case TB_TYPE_DP_HDMI_OUT:
1201 return tb_dp_port_is_enabled(port);
1203 case TB_TYPE_USB3_UP:
1204 case TB_TYPE_USB3_DOWN:
1205 return tb_usb3_port_is_enabled(port);
1213 * tb_usb3_port_is_enabled() - Is the USB3 adapter port enabled
1214 * @port: USB3 adapter port to check
1216 bool tb_usb3_port_is_enabled(struct tb_port *port)
1220 if (tb_port_read(port, &data, TB_CFG_PORT,
1221 port->cap_adap + ADP_USB3_CS_0, 1))
1224 return !!(data & ADP_USB3_CS_0_PE);
1228 * tb_usb3_port_enable() - Enable USB3 adapter port
1229 * @port: USB3 adapter port to enable
1230 * @enable: Enable/disable the USB3 adapter
1232 int tb_usb3_port_enable(struct tb_port *port, bool enable)
1234 u32 word = enable ? (ADP_USB3_CS_0_PE | ADP_USB3_CS_0_V)
1237 if (!port->cap_adap)
1239 return tb_port_write(port, &word, TB_CFG_PORT,
1240 port->cap_adap + ADP_USB3_CS_0, 1);
1244 * tb_pci_port_is_enabled() - Is the PCIe adapter port enabled
1245 * @port: PCIe port to check
1247 bool tb_pci_port_is_enabled(struct tb_port *port)
1251 if (tb_port_read(port, &data, TB_CFG_PORT,
1252 port->cap_adap + ADP_PCIE_CS_0, 1))
1255 return !!(data & ADP_PCIE_CS_0_PE);
1259 * tb_pci_port_enable() - Enable PCIe adapter port
1260 * @port: PCIe port to enable
1261 * @enable: Enable/disable the PCIe adapter
1263 int tb_pci_port_enable(struct tb_port *port, bool enable)
1265 u32 word = enable ? ADP_PCIE_CS_0_PE : 0x0;
1266 if (!port->cap_adap)
1268 return tb_port_write(port, &word, TB_CFG_PORT,
1269 port->cap_adap + ADP_PCIE_CS_0, 1);
1273 * tb_dp_port_hpd_is_active() - Is HPD already active
1274 * @port: DP out port to check
1276 * Checks if the DP OUT adapter port has HDP bit already set.
1278 int tb_dp_port_hpd_is_active(struct tb_port *port)
1283 ret = tb_port_read(port, &data, TB_CFG_PORT,
1284 port->cap_adap + ADP_DP_CS_2, 1);
1288 return !!(data & ADP_DP_CS_2_HDP);
1292 * tb_dp_port_hpd_clear() - Clear HPD from DP IN port
1293 * @port: Port to clear HPD
1295 * If the DP IN port has HDP set, this function can be used to clear it.
1297 int tb_dp_port_hpd_clear(struct tb_port *port)
1302 ret = tb_port_read(port, &data, TB_CFG_PORT,
1303 port->cap_adap + ADP_DP_CS_3, 1);
1307 data |= ADP_DP_CS_3_HDPC;
1308 return tb_port_write(port, &data, TB_CFG_PORT,
1309 port->cap_adap + ADP_DP_CS_3, 1);
1313 * tb_dp_port_set_hops() - Set video/aux Hop IDs for DP port
1314 * @port: DP IN/OUT port to set hops
1315 * @video: Video Hop ID
1316 * @aux_tx: AUX TX Hop ID
1317 * @aux_rx: AUX RX Hop ID
1319 * Programs specified Hop IDs for DP IN/OUT port.
1321 int tb_dp_port_set_hops(struct tb_port *port, unsigned int video,
1322 unsigned int aux_tx, unsigned int aux_rx)
1327 ret = tb_port_read(port, data, TB_CFG_PORT,
1328 port->cap_adap + ADP_DP_CS_0, ARRAY_SIZE(data));
1332 data[0] &= ~ADP_DP_CS_0_VIDEO_HOPID_MASK;
1333 data[1] &= ~ADP_DP_CS_1_AUX_RX_HOPID_MASK;
1334 data[1] &= ~ADP_DP_CS_1_AUX_RX_HOPID_MASK;
1336 data[0] |= (video << ADP_DP_CS_0_VIDEO_HOPID_SHIFT) &
1337 ADP_DP_CS_0_VIDEO_HOPID_MASK;
1338 data[1] |= aux_tx & ADP_DP_CS_1_AUX_TX_HOPID_MASK;
1339 data[1] |= (aux_rx << ADP_DP_CS_1_AUX_RX_HOPID_SHIFT) &
1340 ADP_DP_CS_1_AUX_RX_HOPID_MASK;
1342 return tb_port_write(port, data, TB_CFG_PORT,
1343 port->cap_adap + ADP_DP_CS_0, ARRAY_SIZE(data));
1347 * tb_dp_port_is_enabled() - Is DP adapter port enabled
1348 * @port: DP adapter port to check
1350 bool tb_dp_port_is_enabled(struct tb_port *port)
1354 if (tb_port_read(port, data, TB_CFG_PORT, port->cap_adap + ADP_DP_CS_0,
1358 return !!(data[0] & (ADP_DP_CS_0_VE | ADP_DP_CS_0_AE));
1362 * tb_dp_port_enable() - Enables/disables DP paths of a port
1363 * @port: DP IN/OUT port
1364 * @enable: Enable/disable DP path
1366 * Once Hop IDs are programmed DP paths can be enabled or disabled by
1367 * calling this function.
1369 int tb_dp_port_enable(struct tb_port *port, bool enable)
1374 ret = tb_port_read(port, data, TB_CFG_PORT,
1375 port->cap_adap + ADP_DP_CS_0, ARRAY_SIZE(data));
1380 data[0] |= ADP_DP_CS_0_VE | ADP_DP_CS_0_AE;
1382 data[0] &= ~(ADP_DP_CS_0_VE | ADP_DP_CS_0_AE);
1384 return tb_port_write(port, data, TB_CFG_PORT,
1385 port->cap_adap + ADP_DP_CS_0, ARRAY_SIZE(data));
1388 /* switch utility functions */
1390 static const char *tb_switch_generation_name(const struct tb_switch *sw)
1392 switch (sw->generation) {
1394 return "Thunderbolt 1";
1396 return "Thunderbolt 2";
1398 return "Thunderbolt 3";
1406 static void tb_dump_switch(const struct tb *tb, const struct tb_switch *sw)
1408 const struct tb_regs_switch_header *regs = &sw->config;
1410 tb_dbg(tb, " %s Switch: %x:%x (Revision: %d, TB Version: %d)\n",
1411 tb_switch_generation_name(sw), regs->vendor_id, regs->device_id,
1412 regs->revision, regs->thunderbolt_version);
1413 tb_dbg(tb, " Max Port Number: %d\n", regs->max_port_number);
1414 tb_dbg(tb, " Config:\n");
1416 " Upstream Port Number: %d Depth: %d Route String: %#llx Enabled: %d, PlugEventsDelay: %dms\n",
1417 regs->upstream_port_number, regs->depth,
1418 (((u64) regs->route_hi) << 32) | regs->route_lo,
1419 regs->enabled, regs->plug_events_delay);
1420 tb_dbg(tb, " unknown1: %#x unknown4: %#x\n",
1421 regs->__unknown1, regs->__unknown4);
1425 * tb_switch_reset() - reconfigure route, enable and send TB_CFG_PKG_RESET
1426 * @sw: Switch to reset
1428 * Return: Returns 0 on success or an error code on failure.
1430 int tb_switch_reset(struct tb_switch *sw)
1432 struct tb_cfg_result res;
1434 if (sw->generation > 1)
1437 tb_sw_dbg(sw, "resetting switch\n");
1439 res.err = tb_sw_write(sw, ((u32 *) &sw->config) + 2,
1440 TB_CFG_SWITCH, 2, 2);
1443 res = tb_cfg_reset(sw->tb->ctl, tb_route(sw));
1450 * tb_plug_events_active() - enable/disable plug events on a switch
1452 * Also configures a sane plug_events_delay of 255ms.
1454 * Return: Returns 0 on success or an error code on failure.
1456 static int tb_plug_events_active(struct tb_switch *sw, bool active)
1461 if (tb_switch_is_icm(sw) || tb_switch_is_usb4(sw))
1464 sw->config.plug_events_delay = 0xff;
1465 res = tb_sw_write(sw, ((u32 *) &sw->config) + 4, TB_CFG_SWITCH, 4, 1);
1469 res = tb_sw_read(sw, &data, TB_CFG_SWITCH, sw->cap_plug_events + 1, 1);
1474 data = data & 0xFFFFFF83;
1475 switch (sw->config.device_id) {
1476 case PCI_DEVICE_ID_INTEL_LIGHT_RIDGE:
1477 case PCI_DEVICE_ID_INTEL_EAGLE_RIDGE:
1478 case PCI_DEVICE_ID_INTEL_PORT_RIDGE:
1486 return tb_sw_write(sw, &data, TB_CFG_SWITCH,
1487 sw->cap_plug_events + 1, 1);
1490 static ssize_t authorized_show(struct device *dev,
1491 struct device_attribute *attr,
1494 struct tb_switch *sw = tb_to_switch(dev);
1496 return sprintf(buf, "%u\n", sw->authorized);
1499 static int disapprove_switch(struct device *dev, void *not_used)
1501 struct tb_switch *sw;
1503 sw = tb_to_switch(dev);
1504 if (sw && sw->authorized) {
1507 /* First children */
1508 ret = device_for_each_child_reverse(&sw->dev, NULL, disapprove_switch);
1512 ret = tb_domain_disapprove_switch(sw->tb, sw);
1517 kobject_uevent(&sw->dev.kobj, KOBJ_CHANGE);
1523 static int tb_switch_set_authorized(struct tb_switch *sw, unsigned int val)
1527 if (!mutex_trylock(&sw->tb->lock))
1528 return restart_syscall();
1530 if (!!sw->authorized == !!val)
1534 /* Disapprove switch */
1537 ret = disapprove_switch(&sw->dev, NULL);
1542 /* Approve switch */
1545 ret = tb_domain_approve_switch_key(sw->tb, sw);
1547 ret = tb_domain_approve_switch(sw->tb, sw);
1550 /* Challenge switch */
1553 ret = tb_domain_challenge_switch_key(sw->tb, sw);
1561 sw->authorized = val;
1562 /* Notify status change to the userspace */
1563 kobject_uevent(&sw->dev.kobj, KOBJ_CHANGE);
1567 mutex_unlock(&sw->tb->lock);
1571 static ssize_t authorized_store(struct device *dev,
1572 struct device_attribute *attr,
1573 const char *buf, size_t count)
1575 struct tb_switch *sw = tb_to_switch(dev);
1579 ret = kstrtouint(buf, 0, &val);
1585 pm_runtime_get_sync(&sw->dev);
1586 ret = tb_switch_set_authorized(sw, val);
1587 pm_runtime_mark_last_busy(&sw->dev);
1588 pm_runtime_put_autosuspend(&sw->dev);
1590 return ret ? ret : count;
1592 static DEVICE_ATTR_RW(authorized);
1594 static ssize_t boot_show(struct device *dev, struct device_attribute *attr,
1597 struct tb_switch *sw = tb_to_switch(dev);
1599 return sprintf(buf, "%u\n", sw->boot);
1601 static DEVICE_ATTR_RO(boot);
1603 static ssize_t device_show(struct device *dev, struct device_attribute *attr,
1606 struct tb_switch *sw = tb_to_switch(dev);
1608 return sprintf(buf, "%#x\n", sw->device);
1610 static DEVICE_ATTR_RO(device);
1613 device_name_show(struct device *dev, struct device_attribute *attr, char *buf)
1615 struct tb_switch *sw = tb_to_switch(dev);
1617 return sprintf(buf, "%s\n", sw->device_name ? sw->device_name : "");
1619 static DEVICE_ATTR_RO(device_name);
1622 generation_show(struct device *dev, struct device_attribute *attr, char *buf)
1624 struct tb_switch *sw = tb_to_switch(dev);
1626 return sprintf(buf, "%u\n", sw->generation);
1628 static DEVICE_ATTR_RO(generation);
1630 static ssize_t key_show(struct device *dev, struct device_attribute *attr,
1633 struct tb_switch *sw = tb_to_switch(dev);
1636 if (!mutex_trylock(&sw->tb->lock))
1637 return restart_syscall();
1640 ret = sprintf(buf, "%*phN\n", TB_SWITCH_KEY_SIZE, sw->key);
1642 ret = sprintf(buf, "\n");
1644 mutex_unlock(&sw->tb->lock);
1648 static ssize_t key_store(struct device *dev, struct device_attribute *attr,
1649 const char *buf, size_t count)
1651 struct tb_switch *sw = tb_to_switch(dev);
1652 u8 key[TB_SWITCH_KEY_SIZE];
1653 ssize_t ret = count;
1656 if (!strcmp(buf, "\n"))
1658 else if (hex2bin(key, buf, sizeof(key)))
1661 if (!mutex_trylock(&sw->tb->lock))
1662 return restart_syscall();
1664 if (sw->authorized) {
1671 sw->key = kmemdup(key, sizeof(key), GFP_KERNEL);
1677 mutex_unlock(&sw->tb->lock);
1680 static DEVICE_ATTR(key, 0600, key_show, key_store);
1682 static ssize_t speed_show(struct device *dev, struct device_attribute *attr,
1685 struct tb_switch *sw = tb_to_switch(dev);
1687 return sprintf(buf, "%u.0 Gb/s\n", sw->link_speed);
1691 * Currently all lanes must run at the same speed but we expose here
1692 * both directions to allow possible asymmetric links in the future.
1694 static DEVICE_ATTR(rx_speed, 0444, speed_show, NULL);
1695 static DEVICE_ATTR(tx_speed, 0444, speed_show, NULL);
1697 static ssize_t lanes_show(struct device *dev, struct device_attribute *attr,
1700 struct tb_switch *sw = tb_to_switch(dev);
1702 return sprintf(buf, "%u\n", sw->link_width);
1706 * Currently link has same amount of lanes both directions (1 or 2) but
1707 * expose them separately to allow possible asymmetric links in the future.
1709 static DEVICE_ATTR(rx_lanes, 0444, lanes_show, NULL);
1710 static DEVICE_ATTR(tx_lanes, 0444, lanes_show, NULL);
1712 static ssize_t nvm_authenticate_show(struct device *dev,
1713 struct device_attribute *attr, char *buf)
1715 struct tb_switch *sw = tb_to_switch(dev);
1718 nvm_get_auth_status(sw, &status);
1719 return sprintf(buf, "%#x\n", status);
1722 static ssize_t nvm_authenticate_sysfs(struct device *dev, const char *buf,
1725 struct tb_switch *sw = tb_to_switch(dev);
1728 pm_runtime_get_sync(&sw->dev);
1730 if (!mutex_trylock(&sw->tb->lock)) {
1731 ret = restart_syscall();
1735 /* If NVMem devices are not yet added */
1741 ret = kstrtoint(buf, 10, &val);
1745 /* Always clear the authentication status */
1746 nvm_clear_auth_status(sw);
1749 if (val == AUTHENTICATE_ONLY) {
1753 ret = nvm_authenticate(sw, true);
1755 if (!sw->nvm->flushed) {
1756 if (!sw->nvm->buf) {
1761 ret = nvm_validate_and_write(sw);
1762 if (ret || val == WRITE_ONLY)
1765 if (val == WRITE_AND_AUTHENTICATE) {
1767 ret = tb_lc_force_power(sw);
1769 ret = nvm_authenticate(sw, false);
1775 mutex_unlock(&sw->tb->lock);
1777 pm_runtime_mark_last_busy(&sw->dev);
1778 pm_runtime_put_autosuspend(&sw->dev);
1783 static ssize_t nvm_authenticate_store(struct device *dev,
1784 struct device_attribute *attr, const char *buf, size_t count)
1786 int ret = nvm_authenticate_sysfs(dev, buf, false);
1791 static DEVICE_ATTR_RW(nvm_authenticate);
1793 static ssize_t nvm_authenticate_on_disconnect_show(struct device *dev,
1794 struct device_attribute *attr, char *buf)
1796 return nvm_authenticate_show(dev, attr, buf);
1799 static ssize_t nvm_authenticate_on_disconnect_store(struct device *dev,
1800 struct device_attribute *attr, const char *buf, size_t count)
1804 ret = nvm_authenticate_sysfs(dev, buf, true);
1805 return ret ? ret : count;
1807 static DEVICE_ATTR_RW(nvm_authenticate_on_disconnect);
1809 static ssize_t nvm_version_show(struct device *dev,
1810 struct device_attribute *attr, char *buf)
1812 struct tb_switch *sw = tb_to_switch(dev);
1815 if (!mutex_trylock(&sw->tb->lock))
1816 return restart_syscall();
1823 ret = sprintf(buf, "%x.%x\n", sw->nvm->major, sw->nvm->minor);
1825 mutex_unlock(&sw->tb->lock);
1829 static DEVICE_ATTR_RO(nvm_version);
1831 static ssize_t vendor_show(struct device *dev, struct device_attribute *attr,
1834 struct tb_switch *sw = tb_to_switch(dev);
1836 return sprintf(buf, "%#x\n", sw->vendor);
1838 static DEVICE_ATTR_RO(vendor);
1841 vendor_name_show(struct device *dev, struct device_attribute *attr, char *buf)
1843 struct tb_switch *sw = tb_to_switch(dev);
1845 return sprintf(buf, "%s\n", sw->vendor_name ? sw->vendor_name : "");
1847 static DEVICE_ATTR_RO(vendor_name);
1849 static ssize_t unique_id_show(struct device *dev, struct device_attribute *attr,
1852 struct tb_switch *sw = tb_to_switch(dev);
1854 return sprintf(buf, "%pUb\n", sw->uuid);
1856 static DEVICE_ATTR_RO(unique_id);
1858 static struct attribute *switch_attrs[] = {
1859 &dev_attr_authorized.attr,
1860 &dev_attr_boot.attr,
1861 &dev_attr_device.attr,
1862 &dev_attr_device_name.attr,
1863 &dev_attr_generation.attr,
1865 &dev_attr_nvm_authenticate.attr,
1866 &dev_attr_nvm_authenticate_on_disconnect.attr,
1867 &dev_attr_nvm_version.attr,
1868 &dev_attr_rx_speed.attr,
1869 &dev_attr_rx_lanes.attr,
1870 &dev_attr_tx_speed.attr,
1871 &dev_attr_tx_lanes.attr,
1872 &dev_attr_vendor.attr,
1873 &dev_attr_vendor_name.attr,
1874 &dev_attr_unique_id.attr,
1878 static umode_t switch_attr_is_visible(struct kobject *kobj,
1879 struct attribute *attr, int n)
1881 struct device *dev = kobj_to_dev(kobj);
1882 struct tb_switch *sw = tb_to_switch(dev);
1884 if (attr == &dev_attr_authorized.attr) {
1885 if (sw->tb->security_level == TB_SECURITY_NOPCIE ||
1886 sw->tb->security_level == TB_SECURITY_DPONLY)
1888 } else if (attr == &dev_attr_device.attr) {
1891 } else if (attr == &dev_attr_device_name.attr) {
1892 if (!sw->device_name)
1894 } else if (attr == &dev_attr_vendor.attr) {
1897 } else if (attr == &dev_attr_vendor_name.attr) {
1898 if (!sw->vendor_name)
1900 } else if (attr == &dev_attr_key.attr) {
1902 sw->tb->security_level == TB_SECURITY_SECURE &&
1903 sw->security_level == TB_SECURITY_SECURE)
1906 } else if (attr == &dev_attr_rx_speed.attr ||
1907 attr == &dev_attr_rx_lanes.attr ||
1908 attr == &dev_attr_tx_speed.attr ||
1909 attr == &dev_attr_tx_lanes.attr) {
1913 } else if (attr == &dev_attr_nvm_authenticate.attr) {
1914 if (nvm_upgradeable(sw))
1917 } else if (attr == &dev_attr_nvm_version.attr) {
1918 if (nvm_readable(sw))
1921 } else if (attr == &dev_attr_boot.attr) {
1925 } else if (attr == &dev_attr_nvm_authenticate_on_disconnect.attr) {
1926 if (sw->quirks & QUIRK_FORCE_POWER_LINK_CONTROLLER)
1931 return sw->safe_mode ? 0 : attr->mode;
1934 static const struct attribute_group switch_group = {
1935 .is_visible = switch_attr_is_visible,
1936 .attrs = switch_attrs,
1939 static const struct attribute_group *switch_groups[] = {
1944 static void tb_switch_release(struct device *dev)
1946 struct tb_switch *sw = tb_to_switch(dev);
1947 struct tb_port *port;
1949 dma_port_free(sw->dma_port);
1951 tb_switch_for_each_port(sw, port) {
1952 ida_destroy(&port->in_hopids);
1953 ida_destroy(&port->out_hopids);
1957 kfree(sw->device_name);
1958 kfree(sw->vendor_name);
1965 static int tb_switch_uevent(struct device *dev, struct kobj_uevent_env *env)
1967 struct tb_switch *sw = tb_to_switch(dev);
1970 if (sw->config.thunderbolt_version == USB4_VERSION_1_0) {
1971 if (add_uevent_var(env, "USB4_VERSION=1.0"))
1975 if (!tb_route(sw)) {
1978 const struct tb_port *port;
1981 /* Device is hub if it has any downstream ports */
1982 tb_switch_for_each_port(sw, port) {
1983 if (!port->disabled && !tb_is_upstream_port(port) &&
1984 tb_port_is_null(port)) {
1990 type = hub ? "hub" : "device";
1993 if (add_uevent_var(env, "USB4_TYPE=%s", type))
1999 * Currently only need to provide the callbacks. Everything else is handled
2000 * in the connection manager.
2002 static int __maybe_unused tb_switch_runtime_suspend(struct device *dev)
2004 struct tb_switch *sw = tb_to_switch(dev);
2005 const struct tb_cm_ops *cm_ops = sw->tb->cm_ops;
2007 if (cm_ops->runtime_suspend_switch)
2008 return cm_ops->runtime_suspend_switch(sw);
2013 static int __maybe_unused tb_switch_runtime_resume(struct device *dev)
2015 struct tb_switch *sw = tb_to_switch(dev);
2016 const struct tb_cm_ops *cm_ops = sw->tb->cm_ops;
2018 if (cm_ops->runtime_resume_switch)
2019 return cm_ops->runtime_resume_switch(sw);
2023 static const struct dev_pm_ops tb_switch_pm_ops = {
2024 SET_RUNTIME_PM_OPS(tb_switch_runtime_suspend, tb_switch_runtime_resume,
2028 struct device_type tb_switch_type = {
2029 .name = "thunderbolt_device",
2030 .release = tb_switch_release,
2031 .uevent = tb_switch_uevent,
2032 .pm = &tb_switch_pm_ops,
2035 static int tb_switch_get_generation(struct tb_switch *sw)
2037 switch (sw->config.device_id) {
2038 case PCI_DEVICE_ID_INTEL_LIGHT_RIDGE:
2039 case PCI_DEVICE_ID_INTEL_EAGLE_RIDGE:
2040 case PCI_DEVICE_ID_INTEL_LIGHT_PEAK:
2041 case PCI_DEVICE_ID_INTEL_CACTUS_RIDGE_2C:
2042 case PCI_DEVICE_ID_INTEL_CACTUS_RIDGE_4C:
2043 case PCI_DEVICE_ID_INTEL_PORT_RIDGE:
2044 case PCI_DEVICE_ID_INTEL_REDWOOD_RIDGE_2C_BRIDGE:
2045 case PCI_DEVICE_ID_INTEL_REDWOOD_RIDGE_4C_BRIDGE:
2048 case PCI_DEVICE_ID_INTEL_WIN_RIDGE_2C_BRIDGE:
2049 case PCI_DEVICE_ID_INTEL_FALCON_RIDGE_2C_BRIDGE:
2050 case PCI_DEVICE_ID_INTEL_FALCON_RIDGE_4C_BRIDGE:
2053 case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_LP_BRIDGE:
2054 case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_2C_BRIDGE:
2055 case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_4C_BRIDGE:
2056 case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_C_2C_BRIDGE:
2057 case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_C_4C_BRIDGE:
2058 case PCI_DEVICE_ID_INTEL_TITAN_RIDGE_2C_BRIDGE:
2059 case PCI_DEVICE_ID_INTEL_TITAN_RIDGE_4C_BRIDGE:
2060 case PCI_DEVICE_ID_INTEL_TITAN_RIDGE_DD_BRIDGE:
2061 case PCI_DEVICE_ID_INTEL_ICL_NHI0:
2062 case PCI_DEVICE_ID_INTEL_ICL_NHI1:
2066 if (tb_switch_is_usb4(sw))
2070 * For unknown switches assume generation to be 1 to be
2073 tb_sw_warn(sw, "unsupported switch device id %#x\n",
2074 sw->config.device_id);
2079 static bool tb_switch_exceeds_max_depth(const struct tb_switch *sw, int depth)
2083 if (tb_switch_is_usb4(sw) ||
2084 (sw->tb->root_switch && tb_switch_is_usb4(sw->tb->root_switch)))
2085 max_depth = USB4_SWITCH_MAX_DEPTH;
2087 max_depth = TB_SWITCH_MAX_DEPTH;
2089 return depth > max_depth;
2093 * tb_switch_alloc() - allocate a switch
2094 * @tb: Pointer to the owning domain
2095 * @parent: Parent device for this switch
2096 * @route: Route string for this switch
2098 * Allocates and initializes a switch. Will not upload configuration to
2099 * the switch. For that you need to call tb_switch_configure()
2100 * separately. The returned switch should be released by calling
2103 * Return: Pointer to the allocated switch or ERR_PTR() in case of
2106 struct tb_switch *tb_switch_alloc(struct tb *tb, struct device *parent,
2109 struct tb_switch *sw;
2113 /* Unlock the downstream port so we can access the switch below */
2115 struct tb_switch *parent_sw = tb_to_switch(parent);
2116 struct tb_port *down;
2118 down = tb_port_at(route, parent_sw);
2119 tb_port_unlock(down);
2122 depth = tb_route_length(route);
2124 upstream_port = tb_cfg_get_upstream_port(tb->ctl, route);
2125 if (upstream_port < 0)
2126 return ERR_PTR(upstream_port);
2128 sw = kzalloc(sizeof(*sw), GFP_KERNEL);
2130 return ERR_PTR(-ENOMEM);
2133 ret = tb_cfg_read(tb->ctl, &sw->config, route, 0, TB_CFG_SWITCH, 0, 5);
2135 goto err_free_sw_ports;
2137 sw->generation = tb_switch_get_generation(sw);
2139 tb_dbg(tb, "current switch config:\n");
2140 tb_dump_switch(tb, sw);
2142 /* configure switch */
2143 sw->config.upstream_port_number = upstream_port;
2144 sw->config.depth = depth;
2145 sw->config.route_hi = upper_32_bits(route);
2146 sw->config.route_lo = lower_32_bits(route);
2147 sw->config.enabled = 0;
2149 /* Make sure we do not exceed maximum topology limit */
2150 if (tb_switch_exceeds_max_depth(sw, depth)) {
2151 ret = -EADDRNOTAVAIL;
2152 goto err_free_sw_ports;
2155 /* initialize ports */
2156 sw->ports = kcalloc(sw->config.max_port_number + 1, sizeof(*sw->ports),
2160 goto err_free_sw_ports;
2163 for (i = 0; i <= sw->config.max_port_number; i++) {
2164 /* minimum setup for tb_find_cap and tb_drom_read to work */
2165 sw->ports[i].sw = sw;
2166 sw->ports[i].port = i;
2168 /* Control port does not need HopID allocation */
2170 ida_init(&sw->ports[i].in_hopids);
2171 ida_init(&sw->ports[i].out_hopids);
2175 ret = tb_switch_find_vse_cap(sw, TB_VSE_CAP_PLUG_EVENTS);
2177 sw->cap_plug_events = ret;
2179 ret = tb_switch_find_vse_cap(sw, TB_VSE_CAP_LINK_CONTROLLER);
2183 /* Root switch is always authorized */
2185 sw->authorized = true;
2187 device_initialize(&sw->dev);
2188 sw->dev.parent = parent;
2189 sw->dev.bus = &tb_bus_type;
2190 sw->dev.type = &tb_switch_type;
2191 sw->dev.groups = switch_groups;
2192 dev_set_name(&sw->dev, "%u-%llx", tb->index, tb_route(sw));
2200 return ERR_PTR(ret);
2204 * tb_switch_alloc_safe_mode() - allocate a switch that is in safe mode
2205 * @tb: Pointer to the owning domain
2206 * @parent: Parent device for this switch
2207 * @route: Route string for this switch
2209 * This creates a switch in safe mode. This means the switch pretty much
2210 * lacks all capabilities except DMA configuration port before it is
2211 * flashed with a valid NVM firmware.
2213 * The returned switch must be released by calling tb_switch_put().
2215 * Return: Pointer to the allocated switch or ERR_PTR() in case of failure
2218 tb_switch_alloc_safe_mode(struct tb *tb, struct device *parent, u64 route)
2220 struct tb_switch *sw;
2222 sw = kzalloc(sizeof(*sw), GFP_KERNEL);
2224 return ERR_PTR(-ENOMEM);
2227 sw->config.depth = tb_route_length(route);
2228 sw->config.route_hi = upper_32_bits(route);
2229 sw->config.route_lo = lower_32_bits(route);
2230 sw->safe_mode = true;
2232 device_initialize(&sw->dev);
2233 sw->dev.parent = parent;
2234 sw->dev.bus = &tb_bus_type;
2235 sw->dev.type = &tb_switch_type;
2236 sw->dev.groups = switch_groups;
2237 dev_set_name(&sw->dev, "%u-%llx", tb->index, tb_route(sw));
2243 * tb_switch_configure() - Uploads configuration to the switch
2244 * @sw: Switch to configure
2246 * Call this function before the switch is added to the system. It will
2247 * upload configuration to the switch and makes it available for the
2248 * connection manager to use. Can be called to the switch again after
2249 * resume from low power states to re-initialize it.
2251 * Return: %0 in case of success and negative errno in case of failure
2253 int tb_switch_configure(struct tb_switch *sw)
2255 struct tb *tb = sw->tb;
2259 route = tb_route(sw);
2261 tb_dbg(tb, "%s Switch at %#llx (depth: %d, up port: %d)\n",
2262 sw->config.enabled ? "restoring" : "initializing", route,
2263 tb_route_length(route), sw->config.upstream_port_number);
2265 sw->config.enabled = 1;
2267 if (tb_switch_is_usb4(sw)) {
2269 * For USB4 devices, we need to program the CM version
2270 * accordingly so that it knows to expose all the
2271 * additional capabilities.
2273 sw->config.cmuv = USB4_VERSION_1_0;
2275 /* Enumerate the switch */
2276 ret = tb_sw_write(sw, (u32 *)&sw->config + 1, TB_CFG_SWITCH,
2281 ret = usb4_switch_setup(sw);
2283 if (sw->config.vendor_id != PCI_VENDOR_ID_INTEL)
2284 tb_sw_warn(sw, "unknown switch vendor id %#x\n",
2285 sw->config.vendor_id);
2287 if (!sw->cap_plug_events) {
2288 tb_sw_warn(sw, "cannot find TB_VSE_CAP_PLUG_EVENTS aborting\n");
2292 /* Enumerate the switch */
2293 ret = tb_sw_write(sw, (u32 *)&sw->config + 1, TB_CFG_SWITCH,
2299 return tb_plug_events_active(sw, true);
2302 static int tb_switch_set_uuid(struct tb_switch *sw)
2311 if (tb_switch_is_usb4(sw)) {
2312 ret = usb4_switch_read_uid(sw, &sw->uid);
2318 * The newer controllers include fused UUID as part of
2319 * link controller specific registers
2321 ret = tb_lc_read_uuid(sw, uuid);
2331 * ICM generates UUID based on UID and fills the upper
2332 * two words with ones. This is not strictly following
2333 * UUID format but we want to be compatible with it so
2334 * we do the same here.
2336 uuid[0] = sw->uid & 0xffffffff;
2337 uuid[1] = (sw->uid >> 32) & 0xffffffff;
2338 uuid[2] = 0xffffffff;
2339 uuid[3] = 0xffffffff;
2342 sw->uuid = kmemdup(uuid, sizeof(uuid), GFP_KERNEL);
2348 static int tb_switch_add_dma_port(struct tb_switch *sw)
2353 switch (sw->generation) {
2355 /* Only root switch can be upgraded */
2362 ret = tb_switch_set_uuid(sw);
2369 * DMA port is the only thing available when the switch
2377 if (sw->no_nvm_upgrade)
2380 if (tb_switch_is_usb4(sw)) {
2381 ret = usb4_switch_nvm_authenticate_status(sw, &status);
2386 tb_sw_info(sw, "switch flash authentication failed\n");
2387 nvm_set_auth_status(sw, status);
2393 /* Root switch DMA port requires running firmware */
2394 if (!tb_route(sw) && !tb_switch_is_icm(sw))
2397 sw->dma_port = dma_port_alloc(sw);
2402 * If there is status already set then authentication failed
2403 * when the dma_port_flash_update_auth() returned. Power cycling
2404 * is not needed (it was done already) so only thing we do here
2405 * is to unblock runtime PM of the root port.
2407 nvm_get_auth_status(sw, &status);
2410 nvm_authenticate_complete_dma_port(sw);
2415 * Check status of the previous flash authentication. If there
2416 * is one we need to power cycle the switch in any case to make
2417 * it functional again.
2419 ret = dma_port_flash_update_auth_status(sw->dma_port, &status);
2423 /* Now we can allow root port to suspend again */
2425 nvm_authenticate_complete_dma_port(sw);
2428 tb_sw_info(sw, "switch flash authentication failed\n");
2429 nvm_set_auth_status(sw, status);
2432 tb_sw_info(sw, "power cycling the switch now\n");
2433 dma_port_power_cycle(sw->dma_port);
2436 * We return error here which causes the switch adding failure.
2437 * It should appear back after power cycle is complete.
2442 static void tb_switch_default_link_ports(struct tb_switch *sw)
2446 for (i = 1; i <= sw->config.max_port_number; i += 2) {
2447 struct tb_port *port = &sw->ports[i];
2448 struct tb_port *subordinate;
2450 if (!tb_port_is_null(port))
2453 /* Check for the subordinate port */
2454 if (i == sw->config.max_port_number ||
2455 !tb_port_is_null(&sw->ports[i + 1]))
2458 /* Link them if not already done so (by DROM) */
2459 subordinate = &sw->ports[i + 1];
2460 if (!port->dual_link_port && !subordinate->dual_link_port) {
2462 port->dual_link_port = subordinate;
2463 subordinate->link_nr = 1;
2464 subordinate->dual_link_port = port;
2466 tb_sw_dbg(sw, "linked ports %d <-> %d\n",
2467 port->port, subordinate->port);
2472 static bool tb_switch_lane_bonding_possible(struct tb_switch *sw)
2474 const struct tb_port *up = tb_upstream_port(sw);
2476 if (!up->dual_link_port || !up->dual_link_port->remote)
2479 if (tb_switch_is_usb4(sw))
2480 return usb4_switch_lane_bonding_possible(sw);
2481 return tb_lc_lane_bonding_possible(sw);
2484 static int tb_switch_update_link_attributes(struct tb_switch *sw)
2487 bool change = false;
2490 if (!tb_route(sw) || tb_switch_is_icm(sw))
2493 up = tb_upstream_port(sw);
2495 ret = tb_port_get_link_speed(up);
2498 if (sw->link_speed != ret)
2500 sw->link_speed = ret;
2502 ret = tb_port_get_link_width(up);
2505 if (sw->link_width != ret)
2507 sw->link_width = ret;
2509 /* Notify userspace that there is possible link attribute change */
2510 if (device_is_registered(&sw->dev) && change)
2511 kobject_uevent(&sw->dev.kobj, KOBJ_CHANGE);
2517 * tb_switch_lane_bonding_enable() - Enable lane bonding
2518 * @sw: Switch to enable lane bonding
2520 * Connection manager can call this function to enable lane bonding of a
2521 * switch. If conditions are correct and both switches support the feature,
2522 * lanes are bonded. It is safe to call this to any switch.
2524 int tb_switch_lane_bonding_enable(struct tb_switch *sw)
2526 struct tb_switch *parent = tb_to_switch(sw->dev.parent);
2527 struct tb_port *up, *down;
2528 u64 route = tb_route(sw);
2534 if (!tb_switch_lane_bonding_possible(sw))
2537 up = tb_upstream_port(sw);
2538 down = tb_port_at(route, parent);
2540 if (!tb_port_is_width_supported(up, 2) ||
2541 !tb_port_is_width_supported(down, 2))
2544 ret = tb_port_lane_bonding_enable(up);
2546 tb_port_warn(up, "failed to enable lane bonding\n");
2550 ret = tb_port_lane_bonding_enable(down);
2552 tb_port_warn(down, "failed to enable lane bonding\n");
2553 tb_port_lane_bonding_disable(up);
2557 ret = tb_port_wait_for_link_width(down, 2, 100);
2559 tb_port_warn(down, "timeout enabling lane bonding\n");
2563 tb_port_update_credits(down);
2564 tb_port_update_credits(up);
2565 tb_switch_update_link_attributes(sw);
2567 tb_sw_dbg(sw, "lane bonding enabled\n");
2572 * tb_switch_lane_bonding_disable() - Disable lane bonding
2573 * @sw: Switch whose lane bonding to disable
2575 * Disables lane bonding between @sw and parent. This can be called even
2576 * if lanes were not bonded originally.
2578 void tb_switch_lane_bonding_disable(struct tb_switch *sw)
2580 struct tb_switch *parent = tb_to_switch(sw->dev.parent);
2581 struct tb_port *up, *down;
2586 up = tb_upstream_port(sw);
2590 down = tb_port_at(tb_route(sw), parent);
2592 tb_port_lane_bonding_disable(up);
2593 tb_port_lane_bonding_disable(down);
2596 * It is fine if we get other errors as the router might have
2599 if (tb_port_wait_for_link_width(down, 1, 100) == -ETIMEDOUT)
2600 tb_sw_warn(sw, "timeout disabling lane bonding\n");
2602 tb_port_update_credits(down);
2603 tb_port_update_credits(up);
2604 tb_switch_update_link_attributes(sw);
2606 tb_sw_dbg(sw, "lane bonding disabled\n");
2610 * tb_switch_configure_link() - Set link configured
2611 * @sw: Switch whose link is configured
2613 * Sets the link upstream from @sw configured (from both ends) so that
2614 * it will not be disconnected when the domain exits sleep. Can be
2615 * called for any switch.
2617 * It is recommended that this is called after lane bonding is enabled.
2619 * Returns %0 on success and negative errno in case of error.
2621 int tb_switch_configure_link(struct tb_switch *sw)
2623 struct tb_port *up, *down;
2626 if (!tb_route(sw) || tb_switch_is_icm(sw))
2629 up = tb_upstream_port(sw);
2630 if (tb_switch_is_usb4(up->sw))
2631 ret = usb4_port_configure(up);
2633 ret = tb_lc_configure_port(up);
2638 if (tb_switch_is_usb4(down->sw))
2639 return usb4_port_configure(down);
2640 return tb_lc_configure_port(down);
2644 * tb_switch_unconfigure_link() - Unconfigure link
2645 * @sw: Switch whose link is unconfigured
2647 * Sets the link unconfigured so the @sw will be disconnected if the
2648 * domain exists sleep.
2650 void tb_switch_unconfigure_link(struct tb_switch *sw)
2652 struct tb_port *up, *down;
2654 if (sw->is_unplugged)
2656 if (!tb_route(sw) || tb_switch_is_icm(sw))
2659 up = tb_upstream_port(sw);
2660 if (tb_switch_is_usb4(up->sw))
2661 usb4_port_unconfigure(up);
2663 tb_lc_unconfigure_port(up);
2666 if (tb_switch_is_usb4(down->sw))
2667 usb4_port_unconfigure(down);
2669 tb_lc_unconfigure_port(down);
2672 static void tb_switch_credits_init(struct tb_switch *sw)
2674 if (tb_switch_is_icm(sw))
2676 if (!tb_switch_is_usb4(sw))
2678 if (usb4_switch_credits_init(sw))
2679 tb_sw_info(sw, "failed to determine preferred buffer allocation, using defaults\n");
2683 * tb_switch_add() - Add a switch to the domain
2684 * @sw: Switch to add
2686 * This is the last step in adding switch to the domain. It will read
2687 * identification information from DROM and initializes ports so that
2688 * they can be used to connect other switches. The switch will be
2689 * exposed to the userspace when this function successfully returns. To
2690 * remove and release the switch, call tb_switch_remove().
2692 * Return: %0 in case of success and negative errno in case of failure
2694 int tb_switch_add(struct tb_switch *sw)
2699 * Initialize DMA control port now before we read DROM. Recent
2700 * host controllers have more complete DROM on NVM that includes
2701 * vendor and model identification strings which we then expose
2702 * to the userspace. NVM can be accessed through DMA
2703 * configuration based mailbox.
2705 ret = tb_switch_add_dma_port(sw);
2707 dev_err(&sw->dev, "failed to add DMA port\n");
2711 if (!sw->safe_mode) {
2712 tb_switch_credits_init(sw);
2715 ret = tb_drom_read(sw);
2717 dev_err(&sw->dev, "reading DROM failed\n");
2720 tb_sw_dbg(sw, "uid: %#llx\n", sw->uid);
2722 tb_check_quirks(sw);
2724 ret = tb_switch_set_uuid(sw);
2726 dev_err(&sw->dev, "failed to set UUID\n");
2730 for (i = 0; i <= sw->config.max_port_number; i++) {
2731 if (sw->ports[i].disabled) {
2732 tb_port_dbg(&sw->ports[i], "disabled by eeprom\n");
2735 ret = tb_init_port(&sw->ports[i]);
2737 dev_err(&sw->dev, "failed to initialize port %d\n", i);
2742 tb_switch_default_link_ports(sw);
2744 ret = tb_switch_update_link_attributes(sw);
2748 ret = tb_switch_tmu_init(sw);
2753 ret = device_add(&sw->dev);
2755 dev_err(&sw->dev, "failed to add device: %d\n", ret);
2760 dev_info(&sw->dev, "new device found, vendor=%#x device=%#x\n",
2761 sw->vendor, sw->device);
2762 if (sw->vendor_name && sw->device_name)
2763 dev_info(&sw->dev, "%s %s\n", sw->vendor_name,
2767 ret = usb4_switch_add_ports(sw);
2769 dev_err(&sw->dev, "failed to add USB4 ports\n");
2773 ret = tb_switch_nvm_add(sw);
2775 dev_err(&sw->dev, "failed to add NVM devices\n");
2780 * Thunderbolt routers do not generate wakeups themselves but
2781 * they forward wakeups from tunneled protocols, so enable it
2784 device_init_wakeup(&sw->dev, true);
2786 pm_runtime_set_active(&sw->dev);
2788 pm_runtime_set_autosuspend_delay(&sw->dev, TB_AUTOSUSPEND_DELAY);
2789 pm_runtime_use_autosuspend(&sw->dev);
2790 pm_runtime_mark_last_busy(&sw->dev);
2791 pm_runtime_enable(&sw->dev);
2792 pm_request_autosuspend(&sw->dev);
2795 tb_switch_debugfs_init(sw);
2799 usb4_switch_remove_ports(sw);
2801 device_del(&sw->dev);
2807 * tb_switch_remove() - Remove and release a switch
2808 * @sw: Switch to remove
2810 * This will remove the switch from the domain and release it after last
2811 * reference count drops to zero. If there are switches connected below
2812 * this switch, they will be removed as well.
2814 void tb_switch_remove(struct tb_switch *sw)
2816 struct tb_port *port;
2818 tb_switch_debugfs_remove(sw);
2821 pm_runtime_get_sync(&sw->dev);
2822 pm_runtime_disable(&sw->dev);
2825 /* port 0 is the switch itself and never has a remote */
2826 tb_switch_for_each_port(sw, port) {
2827 if (tb_port_has_remote(port)) {
2828 tb_switch_remove(port->remote->sw);
2829 port->remote = NULL;
2830 } else if (port->xdomain) {
2831 tb_xdomain_remove(port->xdomain);
2832 port->xdomain = NULL;
2835 /* Remove any downstream retimers */
2836 tb_retimer_remove_all(port);
2839 if (!sw->is_unplugged)
2840 tb_plug_events_active(sw, false);
2842 tb_switch_nvm_remove(sw);
2843 usb4_switch_remove_ports(sw);
2846 dev_info(&sw->dev, "device disconnected\n");
2847 device_unregister(&sw->dev);
2851 * tb_sw_set_unplugged() - set is_unplugged on switch and downstream switches
2852 * @sw: Router to mark unplugged
2854 void tb_sw_set_unplugged(struct tb_switch *sw)
2856 struct tb_port *port;
2858 if (sw == sw->tb->root_switch) {
2859 tb_sw_WARN(sw, "cannot unplug root switch\n");
2862 if (sw->is_unplugged) {
2863 tb_sw_WARN(sw, "is_unplugged already set\n");
2866 sw->is_unplugged = true;
2867 tb_switch_for_each_port(sw, port) {
2868 if (tb_port_has_remote(port))
2869 tb_sw_set_unplugged(port->remote->sw);
2870 else if (port->xdomain)
2871 port->xdomain->is_unplugged = true;
2875 static int tb_switch_set_wake(struct tb_switch *sw, unsigned int flags)
2878 tb_sw_dbg(sw, "enabling wakeup: %#x\n", flags);
2880 tb_sw_dbg(sw, "disabling wakeup\n");
2882 if (tb_switch_is_usb4(sw))
2883 return usb4_switch_set_wake(sw, flags);
2884 return tb_lc_set_wake(sw, flags);
2887 int tb_switch_resume(struct tb_switch *sw)
2889 struct tb_port *port;
2892 tb_sw_dbg(sw, "resuming switch\n");
2895 * Check for UID of the connected switches except for root
2896 * switch which we assume cannot be removed.
2902 * Check first that we can still read the switch config
2903 * space. It may be that there is now another domain
2906 err = tb_cfg_get_upstream_port(sw->tb->ctl, tb_route(sw));
2908 tb_sw_info(sw, "switch not present anymore\n");
2912 if (tb_switch_is_usb4(sw))
2913 err = usb4_switch_read_uid(sw, &uid);
2915 err = tb_drom_read_uid_only(sw, &uid);
2917 tb_sw_warn(sw, "uid read failed\n");
2920 if (sw->uid != uid) {
2922 "changed while suspended (uid %#llx -> %#llx)\n",
2928 err = tb_switch_configure(sw);
2933 tb_switch_set_wake(sw, 0);
2935 err = tb_switch_tmu_init(sw);
2939 /* check for surviving downstream switches */
2940 tb_switch_for_each_port(sw, port) {
2941 if (!tb_port_is_null(port))
2944 if (!tb_port_resume(port))
2947 if (tb_wait_for_port(port, true) <= 0) {
2949 "lost during suspend, disconnecting\n");
2950 if (tb_port_has_remote(port))
2951 tb_sw_set_unplugged(port->remote->sw);
2952 else if (port->xdomain)
2953 port->xdomain->is_unplugged = true;
2956 * Always unlock the port so the downstream
2957 * switch/domain is accessible.
2959 if (tb_port_unlock(port))
2960 tb_port_warn(port, "failed to unlock port\n");
2961 if (port->remote && tb_switch_resume(port->remote->sw)) {
2963 "lost during suspend, disconnecting\n");
2964 tb_sw_set_unplugged(port->remote->sw);
2972 * tb_switch_suspend() - Put a switch to sleep
2973 * @sw: Switch to suspend
2974 * @runtime: Is this runtime suspend or system sleep
2976 * Suspends router and all its children. Enables wakes according to
2977 * value of @runtime and then sets sleep bit for the router. If @sw is
2978 * host router the domain is ready to go to sleep once this function
2981 void tb_switch_suspend(struct tb_switch *sw, bool runtime)
2983 unsigned int flags = 0;
2984 struct tb_port *port;
2987 tb_sw_dbg(sw, "suspending switch\n");
2989 err = tb_plug_events_active(sw, false);
2993 tb_switch_for_each_port(sw, port) {
2994 if (tb_port_has_remote(port))
2995 tb_switch_suspend(port->remote->sw, runtime);
2999 /* Trigger wake when something is plugged in/out */
3000 flags |= TB_WAKE_ON_CONNECT | TB_WAKE_ON_DISCONNECT;
3001 flags |= TB_WAKE_ON_USB4;
3002 flags |= TB_WAKE_ON_USB3 | TB_WAKE_ON_PCIE | TB_WAKE_ON_DP;
3003 } else if (device_may_wakeup(&sw->dev)) {
3004 flags |= TB_WAKE_ON_USB4 | TB_WAKE_ON_USB3 | TB_WAKE_ON_PCIE;
3007 tb_switch_set_wake(sw, flags);
3009 if (tb_switch_is_usb4(sw))
3010 usb4_switch_set_sleep(sw);
3012 tb_lc_set_sleep(sw);
3016 * tb_switch_query_dp_resource() - Query availability of DP resource
3017 * @sw: Switch whose DP resource is queried
3020 * Queries availability of DP resource for DP tunneling using switch
3021 * specific means. Returns %true if resource is available.
3023 bool tb_switch_query_dp_resource(struct tb_switch *sw, struct tb_port *in)
3025 if (tb_switch_is_usb4(sw))
3026 return usb4_switch_query_dp_resource(sw, in);
3027 return tb_lc_dp_sink_query(sw, in);
3031 * tb_switch_alloc_dp_resource() - Allocate available DP resource
3032 * @sw: Switch whose DP resource is allocated
3035 * Allocates DP resource for DP tunneling. The resource must be
3036 * available for this to succeed (see tb_switch_query_dp_resource()).
3037 * Returns %0 in success and negative errno otherwise.
3039 int tb_switch_alloc_dp_resource(struct tb_switch *sw, struct tb_port *in)
3041 if (tb_switch_is_usb4(sw))
3042 return usb4_switch_alloc_dp_resource(sw, in);
3043 return tb_lc_dp_sink_alloc(sw, in);
3047 * tb_switch_dealloc_dp_resource() - De-allocate DP resource
3048 * @sw: Switch whose DP resource is de-allocated
3051 * De-allocates DP resource that was previously allocated for DP
3054 void tb_switch_dealloc_dp_resource(struct tb_switch *sw, struct tb_port *in)
3058 if (tb_switch_is_usb4(sw))
3059 ret = usb4_switch_dealloc_dp_resource(sw, in);
3061 ret = tb_lc_dp_sink_dealloc(sw, in);
3064 tb_sw_warn(sw, "failed to de-allocate DP resource for port %d\n",
3068 struct tb_sw_lookup {
3076 static int tb_switch_match(struct device *dev, const void *data)
3078 struct tb_switch *sw = tb_to_switch(dev);
3079 const struct tb_sw_lookup *lookup = data;
3083 if (sw->tb != lookup->tb)
3087 return !memcmp(sw->uuid, lookup->uuid, sizeof(*lookup->uuid));
3089 if (lookup->route) {
3090 return sw->config.route_lo == lower_32_bits(lookup->route) &&
3091 sw->config.route_hi == upper_32_bits(lookup->route);
3094 /* Root switch is matched only by depth */
3098 return sw->link == lookup->link && sw->depth == lookup->depth;
3102 * tb_switch_find_by_link_depth() - Find switch by link and depth
3103 * @tb: Domain the switch belongs
3104 * @link: Link number the switch is connected
3105 * @depth: Depth of the switch in link
3107 * Returned switch has reference count increased so the caller needs to
3108 * call tb_switch_put() when done with the switch.
3110 struct tb_switch *tb_switch_find_by_link_depth(struct tb *tb, u8 link, u8 depth)
3112 struct tb_sw_lookup lookup;
3115 memset(&lookup, 0, sizeof(lookup));
3118 lookup.depth = depth;
3120 dev = bus_find_device(&tb_bus_type, NULL, &lookup, tb_switch_match);
3122 return tb_to_switch(dev);
3128 * tb_switch_find_by_uuid() - Find switch by UUID
3129 * @tb: Domain the switch belongs
3130 * @uuid: UUID to look for
3132 * Returned switch has reference count increased so the caller needs to
3133 * call tb_switch_put() when done with the switch.
3135 struct tb_switch *tb_switch_find_by_uuid(struct tb *tb, const uuid_t *uuid)
3137 struct tb_sw_lookup lookup;
3140 memset(&lookup, 0, sizeof(lookup));
3144 dev = bus_find_device(&tb_bus_type, NULL, &lookup, tb_switch_match);
3146 return tb_to_switch(dev);
3152 * tb_switch_find_by_route() - Find switch by route string
3153 * @tb: Domain the switch belongs
3154 * @route: Route string to look for
3156 * Returned switch has reference count increased so the caller needs to
3157 * call tb_switch_put() when done with the switch.
3159 struct tb_switch *tb_switch_find_by_route(struct tb *tb, u64 route)
3161 struct tb_sw_lookup lookup;
3165 return tb_switch_get(tb->root_switch);
3167 memset(&lookup, 0, sizeof(lookup));
3169 lookup.route = route;
3171 dev = bus_find_device(&tb_bus_type, NULL, &lookup, tb_switch_match);
3173 return tb_to_switch(dev);
3179 * tb_switch_find_port() - return the first port of @type on @sw or NULL
3180 * @sw: Switch to find the port from
3181 * @type: Port type to look for
3183 struct tb_port *tb_switch_find_port(struct tb_switch *sw,
3184 enum tb_port_type type)
3186 struct tb_port *port;
3188 tb_switch_for_each_port(sw, port) {
3189 if (port->config.type == type)