1 // SPDX-License-Identifier: GPL-2.0
3 * Thunderbolt driver - switch/port utility functions
5 * Copyright (c) 2014 Andreas Noever <andreas.noever@gmail.com>
6 * Copyright (C) 2018, Intel Corporation
9 #include <linux/delay.h>
10 #include <linux/idr.h>
11 #include <linux/nvmem-provider.h>
12 #include <linux/pm_runtime.h>
13 #include <linux/sched/signal.h>
14 #include <linux/sizes.h>
15 #include <linux/slab.h>
16 #include <linux/module.h>
20 /* Switch NVM support */
22 struct nvm_auth_status {
23 struct list_head list;
28 static bool clx_enabled = true;
29 module_param_named(clx, clx_enabled, bool, 0444);
30 MODULE_PARM_DESC(clx, "allow low power states on the high-speed lanes (default: true)");
33 * Hold NVM authentication failure status per switch This information
34 * needs to stay around even when the switch gets power cycled so we
37 static LIST_HEAD(nvm_auth_status_cache);
38 static DEFINE_MUTEX(nvm_auth_status_lock);
40 static struct nvm_auth_status *__nvm_get_auth_status(const struct tb_switch *sw)
42 struct nvm_auth_status *st;
44 list_for_each_entry(st, &nvm_auth_status_cache, list) {
45 if (uuid_equal(&st->uuid, sw->uuid))
52 static void nvm_get_auth_status(const struct tb_switch *sw, u32 *status)
54 struct nvm_auth_status *st;
56 mutex_lock(&nvm_auth_status_lock);
57 st = __nvm_get_auth_status(sw);
58 mutex_unlock(&nvm_auth_status_lock);
60 *status = st ? st->status : 0;
63 static void nvm_set_auth_status(const struct tb_switch *sw, u32 status)
65 struct nvm_auth_status *st;
67 if (WARN_ON(!sw->uuid))
70 mutex_lock(&nvm_auth_status_lock);
71 st = __nvm_get_auth_status(sw);
74 st = kzalloc(sizeof(*st), GFP_KERNEL);
78 memcpy(&st->uuid, sw->uuid, sizeof(st->uuid));
79 INIT_LIST_HEAD(&st->list);
80 list_add_tail(&st->list, &nvm_auth_status_cache);
85 mutex_unlock(&nvm_auth_status_lock);
88 static void nvm_clear_auth_status(const struct tb_switch *sw)
90 struct nvm_auth_status *st;
92 mutex_lock(&nvm_auth_status_lock);
93 st = __nvm_get_auth_status(sw);
98 mutex_unlock(&nvm_auth_status_lock);
101 static int nvm_validate_and_write(struct tb_switch *sw)
103 unsigned int image_size;
107 ret = tb_nvm_validate(sw->nvm);
111 ret = tb_nvm_write_headers(sw->nvm);
115 buf = sw->nvm->buf_data_start;
116 image_size = sw->nvm->buf_data_size;
118 if (tb_switch_is_usb4(sw))
119 ret = usb4_switch_nvm_write(sw, 0, buf, image_size);
121 ret = dma_port_flash_write(sw->dma_port, 0, buf, image_size);
125 sw->nvm->flushed = true;
129 static int nvm_authenticate_host_dma_port(struct tb_switch *sw)
134 * Root switch NVM upgrade requires that we disconnect the
135 * existing paths first (in case it is not in safe mode
138 if (!sw->safe_mode) {
141 ret = tb_domain_disconnect_all_paths(sw->tb);
145 * The host controller goes away pretty soon after this if
146 * everything goes well so getting timeout is expected.
148 ret = dma_port_flash_update_auth(sw->dma_port);
149 if (!ret || ret == -ETIMEDOUT)
153 * Any error from update auth operation requires power
154 * cycling of the host router.
156 tb_sw_warn(sw, "failed to authenticate NVM, power cycling\n");
157 if (dma_port_flash_update_auth_status(sw->dma_port, &status) > 0)
158 nvm_set_auth_status(sw, status);
162 * From safe mode we can get out by just power cycling the
165 dma_port_power_cycle(sw->dma_port);
169 static int nvm_authenticate_device_dma_port(struct tb_switch *sw)
171 int ret, retries = 10;
173 ret = dma_port_flash_update_auth(sw->dma_port);
179 /* Power cycle is required */
186 * Poll here for the authentication status. It takes some time
187 * for the device to respond (we get timeout for a while). Once
188 * we get response the device needs to be power cycled in order
189 * to the new NVM to be taken into use.
194 ret = dma_port_flash_update_auth_status(sw->dma_port, &status);
195 if (ret < 0 && ret != -ETIMEDOUT)
199 tb_sw_warn(sw, "failed to authenticate NVM\n");
200 nvm_set_auth_status(sw, status);
203 tb_sw_info(sw, "power cycling the switch now\n");
204 dma_port_power_cycle(sw->dma_port);
214 static void nvm_authenticate_start_dma_port(struct tb_switch *sw)
216 struct pci_dev *root_port;
219 * During host router NVM upgrade we should not allow root port to
220 * go into D3cold because some root ports cannot trigger PME
221 * itself. To be on the safe side keep the root port in D0 during
222 * the whole upgrade process.
224 root_port = pcie_find_root_port(sw->tb->nhi->pdev);
226 pm_runtime_get_noresume(&root_port->dev);
229 static void nvm_authenticate_complete_dma_port(struct tb_switch *sw)
231 struct pci_dev *root_port;
233 root_port = pcie_find_root_port(sw->tb->nhi->pdev);
235 pm_runtime_put(&root_port->dev);
238 static inline bool nvm_readable(struct tb_switch *sw)
240 if (tb_switch_is_usb4(sw)) {
242 * USB4 devices must support NVM operations but it is
243 * optional for hosts. Therefore we query the NVM sector
244 * size here and if it is supported assume NVM
245 * operations are implemented.
247 return usb4_switch_nvm_sector_size(sw) > 0;
250 /* Thunderbolt 2 and 3 devices support NVM through DMA port */
251 return !!sw->dma_port;
254 static inline bool nvm_upgradeable(struct tb_switch *sw)
256 if (sw->no_nvm_upgrade)
258 return nvm_readable(sw);
261 static int nvm_authenticate(struct tb_switch *sw, bool auth_only)
265 if (tb_switch_is_usb4(sw)) {
267 ret = usb4_switch_nvm_set_offset(sw, 0);
271 sw->nvm->authenticating = true;
272 return usb4_switch_nvm_authenticate(sw);
273 } else if (auth_only) {
277 sw->nvm->authenticating = true;
279 nvm_authenticate_start_dma_port(sw);
280 ret = nvm_authenticate_host_dma_port(sw);
282 ret = nvm_authenticate_device_dma_port(sw);
289 * tb_switch_nvm_read() - Read router NVM
290 * @sw: Router whose NVM to read
291 * @address: Start address on the NVM
292 * @buf: Buffer where the read data is copied
293 * @size: Size of the buffer in bytes
295 * Reads from router NVM and returns the requested data in @buf. Locking
296 * is up to the caller. Returns %0 in success and negative errno in case
299 int tb_switch_nvm_read(struct tb_switch *sw, unsigned int address, void *buf,
302 if (tb_switch_is_usb4(sw))
303 return usb4_switch_nvm_read(sw, address, buf, size);
304 return dma_port_flash_read(sw->dma_port, address, buf, size);
307 static int nvm_read(void *priv, unsigned int offset, void *val, size_t bytes)
309 struct tb_nvm *nvm = priv;
310 struct tb_switch *sw = tb_to_switch(nvm->dev);
313 pm_runtime_get_sync(&sw->dev);
315 if (!mutex_trylock(&sw->tb->lock)) {
316 ret = restart_syscall();
320 ret = tb_switch_nvm_read(sw, offset, val, bytes);
321 mutex_unlock(&sw->tb->lock);
324 pm_runtime_mark_last_busy(&sw->dev);
325 pm_runtime_put_autosuspend(&sw->dev);
330 static int nvm_write(void *priv, unsigned int offset, void *val, size_t bytes)
332 struct tb_nvm *nvm = priv;
333 struct tb_switch *sw = tb_to_switch(nvm->dev);
336 if (!mutex_trylock(&sw->tb->lock))
337 return restart_syscall();
340 * Since writing the NVM image might require some special steps,
341 * for example when CSS headers are written, we cache the image
342 * locally here and handle the special cases when the user asks
343 * us to authenticate the image.
345 ret = tb_nvm_write_buf(nvm, offset, val, bytes);
346 mutex_unlock(&sw->tb->lock);
351 static int tb_switch_nvm_add(struct tb_switch *sw)
356 if (!nvm_readable(sw))
359 nvm = tb_nvm_alloc(&sw->dev);
361 ret = PTR_ERR(nvm) == -EOPNOTSUPP ? 0 : PTR_ERR(nvm);
365 ret = tb_nvm_read_version(nvm);
370 * If the switch is in safe-mode the only accessible portion of
371 * the NVM is the non-active one where userspace is expected to
372 * write new functional NVM.
374 if (!sw->safe_mode) {
375 ret = tb_nvm_add_active(nvm, nvm_read);
380 if (!sw->no_nvm_upgrade) {
381 ret = tb_nvm_add_non_active(nvm, nvm_write);
390 tb_sw_dbg(sw, "NVM upgrade disabled\n");
391 sw->no_nvm_upgrade = true;
398 static void tb_switch_nvm_remove(struct tb_switch *sw)
408 /* Remove authentication status in case the switch is unplugged */
409 if (!nvm->authenticating)
410 nvm_clear_auth_status(sw);
415 /* port utility functions */
417 static const char *tb_port_type(const struct tb_regs_port_header *port)
419 switch (port->type >> 16) {
421 switch ((u8) port->type) {
446 static void tb_dump_port(struct tb *tb, const struct tb_port *port)
448 const struct tb_regs_port_header *regs = &port->config;
451 " Port %d: %x:%x (Revision: %d, TB Version: %d, Type: %s (%#x))\n",
452 regs->port_number, regs->vendor_id, regs->device_id,
453 regs->revision, regs->thunderbolt_version, tb_port_type(regs),
455 tb_dbg(tb, " Max hop id (in/out): %d/%d\n",
456 regs->max_in_hop_id, regs->max_out_hop_id);
457 tb_dbg(tb, " Max counters: %d\n", regs->max_counters);
458 tb_dbg(tb, " NFC Credits: %#x\n", regs->nfc_credits);
459 tb_dbg(tb, " Credits (total/control): %u/%u\n", port->total_credits,
464 * tb_port_state() - get connectedness state of a port
465 * @port: the port to check
467 * The port must have a TB_CAP_PHY (i.e. it should be a real port).
469 * Return: Returns an enum tb_port_state on success or an error code on failure.
471 int tb_port_state(struct tb_port *port)
473 struct tb_cap_phy phy;
475 if (port->cap_phy == 0) {
476 tb_port_WARN(port, "does not have a PHY\n");
479 res = tb_port_read(port, &phy, TB_CFG_PORT, port->cap_phy, 2);
486 * tb_wait_for_port() - wait for a port to become ready
487 * @port: Port to wait
488 * @wait_if_unplugged: Wait also when port is unplugged
490 * Wait up to 1 second for a port to reach state TB_PORT_UP. If
491 * wait_if_unplugged is set then we also wait if the port is in state
492 * TB_PORT_UNPLUGGED (it takes a while for the device to be registered after
493 * switch resume). Otherwise we only wait if a device is registered but the link
494 * has not yet been established.
496 * Return: Returns an error code on failure. Returns 0 if the port is not
497 * connected or failed to reach state TB_PORT_UP within one second. Returns 1
498 * if the port is connected and in state TB_PORT_UP.
500 int tb_wait_for_port(struct tb_port *port, bool wait_if_unplugged)
504 if (!port->cap_phy) {
505 tb_port_WARN(port, "does not have PHY\n");
508 if (tb_is_upstream_port(port)) {
509 tb_port_WARN(port, "is the upstream port\n");
514 state = tb_port_state(port);
517 if (state == TB_PORT_DISABLED) {
518 tb_port_dbg(port, "is disabled (state: 0)\n");
521 if (state == TB_PORT_UNPLUGGED) {
522 if (wait_if_unplugged) {
523 /* used during resume */
525 "is unplugged (state: 7), retrying...\n");
529 tb_port_dbg(port, "is unplugged (state: 7)\n");
532 if (state == TB_PORT_UP) {
533 tb_port_dbg(port, "is connected, link is up (state: 2)\n");
538 * After plug-in the state is TB_PORT_CONNECTING. Give it some
542 "is connected, link is not up (state: %d), retrying...\n",
547 "failed to reach state TB_PORT_UP. Ignoring port...\n");
552 * tb_port_add_nfc_credits() - add/remove non flow controlled credits to port
553 * @port: Port to add/remove NFC credits
554 * @credits: Credits to add/remove
556 * Change the number of NFC credits allocated to @port by @credits. To remove
557 * NFC credits pass a negative amount of credits.
559 * Return: Returns 0 on success or an error code on failure.
561 int tb_port_add_nfc_credits(struct tb_port *port, int credits)
565 if (credits == 0 || port->sw->is_unplugged)
569 * USB4 restricts programming NFC buffers to lane adapters only
570 * so skip other ports.
572 if (tb_switch_is_usb4(port->sw) && !tb_port_is_null(port))
575 nfc_credits = port->config.nfc_credits & ADP_CS_4_NFC_BUFFERS_MASK;
577 credits = max_t(int, -nfc_credits, credits);
579 nfc_credits += credits;
581 tb_port_dbg(port, "adding %d NFC credits to %lu", credits,
582 port->config.nfc_credits & ADP_CS_4_NFC_BUFFERS_MASK);
584 port->config.nfc_credits &= ~ADP_CS_4_NFC_BUFFERS_MASK;
585 port->config.nfc_credits |= nfc_credits;
587 return tb_port_write(port, &port->config.nfc_credits,
588 TB_CFG_PORT, ADP_CS_4, 1);
592 * tb_port_clear_counter() - clear a counter in TB_CFG_COUNTER
593 * @port: Port whose counters to clear
594 * @counter: Counter index to clear
596 * Return: Returns 0 on success or an error code on failure.
598 int tb_port_clear_counter(struct tb_port *port, int counter)
600 u32 zero[3] = { 0, 0, 0 };
601 tb_port_dbg(port, "clearing counter %d\n", counter);
602 return tb_port_write(port, zero, TB_CFG_COUNTERS, 3 * counter, 3);
606 * tb_port_unlock() - Unlock downstream port
607 * @port: Port to unlock
609 * Needed for USB4 but can be called for any CIO/USB4 ports. Makes the
610 * downstream router accessible for CM.
612 int tb_port_unlock(struct tb_port *port)
614 if (tb_switch_is_icm(port->sw))
616 if (!tb_port_is_null(port))
618 if (tb_switch_is_usb4(port->sw))
619 return usb4_port_unlock(port);
623 static int __tb_port_enable(struct tb_port *port, bool enable)
628 if (!tb_port_is_null(port))
631 ret = tb_port_read(port, &phy, TB_CFG_PORT,
632 port->cap_phy + LANE_ADP_CS_1, 1);
637 phy &= ~LANE_ADP_CS_1_LD;
639 phy |= LANE_ADP_CS_1_LD;
642 ret = tb_port_write(port, &phy, TB_CFG_PORT,
643 port->cap_phy + LANE_ADP_CS_1, 1);
647 tb_port_dbg(port, "lane %sabled\n", enable ? "en" : "dis");
652 * tb_port_enable() - Enable lane adapter
653 * @port: Port to enable (can be %NULL)
655 * This is used for lane 0 and 1 adapters to enable it.
657 int tb_port_enable(struct tb_port *port)
659 return __tb_port_enable(port, true);
663 * tb_port_disable() - Disable lane adapter
664 * @port: Port to disable (can be %NULL)
666 * This is used for lane 0 and 1 adapters to disable it.
668 int tb_port_disable(struct tb_port *port)
670 return __tb_port_enable(port, false);
674 * tb_init_port() - initialize a port
676 * This is a helper method for tb_switch_alloc. Does not check or initialize
677 * any downstream switches.
679 * Return: Returns 0 on success or an error code on failure.
681 static int tb_init_port(struct tb_port *port)
686 INIT_LIST_HEAD(&port->list);
688 /* Control adapter does not have configuration space */
692 res = tb_port_read(port, &port->config, TB_CFG_PORT, 0, 8);
694 if (res == -ENODEV) {
695 tb_dbg(port->sw->tb, " Port %d: not implemented\n",
697 port->disabled = true;
703 /* Port 0 is the switch itself and has no PHY. */
704 if (port->config.type == TB_TYPE_PORT) {
705 cap = tb_port_find_cap(port, TB_PORT_CAP_PHY);
710 tb_port_WARN(port, "non switch port without a PHY\n");
712 cap = tb_port_find_cap(port, TB_PORT_CAP_USB4);
714 port->cap_usb4 = cap;
717 * USB4 ports the buffers allocated for the control path
718 * can be read from the path config space. Legacy
719 * devices we use hard-coded value.
721 if (tb_switch_is_usb4(port->sw)) {
722 struct tb_regs_hop hop;
724 if (!tb_port_read(port, &hop, TB_CFG_HOPS, 0, 2))
725 port->ctl_credits = hop.initial_credits;
727 if (!port->ctl_credits)
728 port->ctl_credits = 2;
731 cap = tb_port_find_cap(port, TB_PORT_CAP_ADAP);
733 port->cap_adap = cap;
736 port->total_credits =
737 (port->config.nfc_credits & ADP_CS_4_TOTAL_BUFFERS_MASK) >>
738 ADP_CS_4_TOTAL_BUFFERS_SHIFT;
740 tb_dump_port(port->sw->tb, port);
744 static int tb_port_alloc_hopid(struct tb_port *port, bool in, int min_hopid,
751 port_max_hopid = port->config.max_in_hop_id;
752 ida = &port->in_hopids;
754 port_max_hopid = port->config.max_out_hop_id;
755 ida = &port->out_hopids;
759 * NHI can use HopIDs 1-max for other adapters HopIDs 0-7 are
762 if (!tb_port_is_nhi(port) && min_hopid < TB_PATH_MIN_HOPID)
763 min_hopid = TB_PATH_MIN_HOPID;
765 if (max_hopid < 0 || max_hopid > port_max_hopid)
766 max_hopid = port_max_hopid;
768 return ida_simple_get(ida, min_hopid, max_hopid + 1, GFP_KERNEL);
772 * tb_port_alloc_in_hopid() - Allocate input HopID from port
773 * @port: Port to allocate HopID for
774 * @min_hopid: Minimum acceptable input HopID
775 * @max_hopid: Maximum acceptable input HopID
777 * Return: HopID between @min_hopid and @max_hopid or negative errno in
780 int tb_port_alloc_in_hopid(struct tb_port *port, int min_hopid, int max_hopid)
782 return tb_port_alloc_hopid(port, true, min_hopid, max_hopid);
786 * tb_port_alloc_out_hopid() - Allocate output HopID from port
787 * @port: Port to allocate HopID for
788 * @min_hopid: Minimum acceptable output HopID
789 * @max_hopid: Maximum acceptable output HopID
791 * Return: HopID between @min_hopid and @max_hopid or negative errno in
794 int tb_port_alloc_out_hopid(struct tb_port *port, int min_hopid, int max_hopid)
796 return tb_port_alloc_hopid(port, false, min_hopid, max_hopid);
800 * tb_port_release_in_hopid() - Release allocated input HopID from port
801 * @port: Port whose HopID to release
802 * @hopid: HopID to release
804 void tb_port_release_in_hopid(struct tb_port *port, int hopid)
806 ida_simple_remove(&port->in_hopids, hopid);
810 * tb_port_release_out_hopid() - Release allocated output HopID from port
811 * @port: Port whose HopID to release
812 * @hopid: HopID to release
814 void tb_port_release_out_hopid(struct tb_port *port, int hopid)
816 ida_simple_remove(&port->out_hopids, hopid);
819 static inline bool tb_switch_is_reachable(const struct tb_switch *parent,
820 const struct tb_switch *sw)
822 u64 mask = (1ULL << parent->config.depth * 8) - 1;
823 return (tb_route(parent) & mask) == (tb_route(sw) & mask);
827 * tb_next_port_on_path() - Return next port for given port on a path
828 * @start: Start port of the walk
829 * @end: End port of the walk
830 * @prev: Previous port (%NULL if this is the first)
832 * This function can be used to walk from one port to another if they
833 * are connected through zero or more switches. If the @prev is dual
834 * link port, the function follows that link and returns another end on
837 * If the @end port has been reached, return %NULL.
839 * Domain tb->lock must be held when this function is called.
841 struct tb_port *tb_next_port_on_path(struct tb_port *start, struct tb_port *end,
842 struct tb_port *prev)
844 struct tb_port *next;
849 if (prev->sw == end->sw) {
855 if (tb_switch_is_reachable(prev->sw, end->sw)) {
856 next = tb_port_at(tb_route(end->sw), prev->sw);
857 /* Walk down the topology if next == prev */
859 (next == prev || next->dual_link_port == prev))
862 if (tb_is_upstream_port(prev)) {
865 next = tb_upstream_port(prev->sw);
867 * Keep the same link if prev and next are both
870 if (next->dual_link_port &&
871 next->link_nr != prev->link_nr) {
872 next = next->dual_link_port;
877 return next != prev ? next : NULL;
881 * tb_port_get_link_speed() - Get current link speed
882 * @port: Port to check (USB4 or CIO)
884 * Returns link speed in Gb/s or negative errno in case of failure.
886 int tb_port_get_link_speed(struct tb_port *port)
894 ret = tb_port_read(port, &val, TB_CFG_PORT,
895 port->cap_phy + LANE_ADP_CS_1, 1);
899 speed = (val & LANE_ADP_CS_1_CURRENT_SPEED_MASK) >>
900 LANE_ADP_CS_1_CURRENT_SPEED_SHIFT;
901 return speed == LANE_ADP_CS_1_CURRENT_SPEED_GEN3 ? 20 : 10;
905 * tb_port_get_link_width() - Get current link width
906 * @port: Port to check (USB4 or CIO)
908 * Returns link width. Return values can be 1 (Single-Lane), 2 (Dual-Lane)
909 * or negative errno in case of failure.
911 int tb_port_get_link_width(struct tb_port *port)
919 ret = tb_port_read(port, &val, TB_CFG_PORT,
920 port->cap_phy + LANE_ADP_CS_1, 1);
924 return (val & LANE_ADP_CS_1_CURRENT_WIDTH_MASK) >>
925 LANE_ADP_CS_1_CURRENT_WIDTH_SHIFT;
928 static bool tb_port_is_width_supported(struct tb_port *port, int width)
936 ret = tb_port_read(port, &phy, TB_CFG_PORT,
937 port->cap_phy + LANE_ADP_CS_0, 1);
941 widths = (phy & LANE_ADP_CS_0_SUPPORTED_WIDTH_MASK) >>
942 LANE_ADP_CS_0_SUPPORTED_WIDTH_SHIFT;
944 return !!(widths & width);
948 * tb_port_set_link_width() - Set target link width of the lane adapter
949 * @port: Lane adapter
950 * @width: Target link width (%1 or %2)
952 * Sets the target link width of the lane adapter to @width. Does not
953 * enable/disable lane bonding. For that call tb_port_set_lane_bonding().
955 * Return: %0 in case of success and negative errno in case of error
957 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 return tb_port_write(port, &val, TB_CFG_PORT,
985 port->cap_phy + LANE_ADP_CS_1, 1);
989 * tb_port_set_lane_bonding() - Enable/disable lane bonding
990 * @port: Lane adapter
991 * @bonding: enable/disable bonding
993 * Enables or disables lane bonding. This should be called after target
994 * link width has been set (tb_port_set_link_width()). Note in most
995 * cases one should use tb_port_lane_bonding_enable() instead to enable
998 * As a side effect sets @port->bonding accordingly (and does the same
1001 * Return: %0 in case of success and negative errno in case of error
1003 int tb_port_set_lane_bonding(struct tb_port *port, bool bonding)
1011 ret = tb_port_read(port, &val, TB_CFG_PORT,
1012 port->cap_phy + LANE_ADP_CS_1, 1);
1017 val |= LANE_ADP_CS_1_LB;
1019 val &= ~LANE_ADP_CS_1_LB;
1021 ret = tb_port_write(port, &val, TB_CFG_PORT,
1022 port->cap_phy + LANE_ADP_CS_1, 1);
1027 * When lane 0 bonding is set it will affect lane 1 too so
1030 port->bonded = bonding;
1031 port->dual_link_port->bonded = bonding;
1037 * tb_port_lane_bonding_enable() - Enable bonding on port
1038 * @port: port to enable
1040 * Enable bonding by setting the link width of the port and the other
1041 * port in case of dual link port. Does not wait for the link to
1042 * actually reach the bonded state so caller needs to call
1043 * tb_port_wait_for_link_width() before enabling any paths through the
1044 * link to make sure the link is in expected state.
1046 * Return: %0 in case of success and negative errno in case of error
1048 int tb_port_lane_bonding_enable(struct tb_port *port)
1053 * Enable lane bonding for both links if not already enabled by
1054 * for example the boot firmware.
1056 ret = tb_port_get_link_width(port);
1058 ret = tb_port_set_link_width(port, 2);
1063 ret = tb_port_get_link_width(port->dual_link_port);
1065 ret = tb_port_set_link_width(port->dual_link_port, 2);
1070 ret = tb_port_set_lane_bonding(port, true);
1077 tb_port_set_link_width(port->dual_link_port, 1);
1079 tb_port_set_link_width(port, 1);
1084 * tb_port_lane_bonding_disable() - Disable bonding on port
1085 * @port: port to disable
1087 * Disable bonding by setting the link width of the port and the
1088 * other port in case of dual link port.
1090 void tb_port_lane_bonding_disable(struct tb_port *port)
1092 tb_port_set_lane_bonding(port, false);
1093 tb_port_set_link_width(port->dual_link_port, 1);
1094 tb_port_set_link_width(port, 1);
1098 * tb_port_wait_for_link_width() - Wait until link reaches specific width
1099 * @port: Port to wait for
1100 * @width: Expected link width (%1 or %2)
1101 * @timeout_msec: Timeout in ms how long to wait
1103 * Should be used after both ends of the link have been bonded (or
1104 * bonding has been disabled) to wait until the link actually reaches
1105 * the expected state. Returns %-ETIMEDOUT if the @width was not reached
1106 * within the given timeout, %0 if it did.
1108 int tb_port_wait_for_link_width(struct tb_port *port, int width,
1111 ktime_t timeout = ktime_add_ms(ktime_get(), timeout_msec);
1115 ret = tb_port_get_link_width(port);
1118 * Sometimes we get port locked error when
1119 * polling the lanes so we can ignore it and
1124 } else if (ret == width) {
1128 usleep_range(1000, 2000);
1129 } while (ktime_before(ktime_get(), timeout));
1134 static int tb_port_do_update_credits(struct tb_port *port)
1139 ret = tb_port_read(port, &nfc_credits, TB_CFG_PORT, ADP_CS_4, 1);
1143 if (nfc_credits != port->config.nfc_credits) {
1146 total = (nfc_credits & ADP_CS_4_TOTAL_BUFFERS_MASK) >>
1147 ADP_CS_4_TOTAL_BUFFERS_SHIFT;
1149 tb_port_dbg(port, "total credits changed %u -> %u\n",
1150 port->total_credits, total);
1152 port->config.nfc_credits = nfc_credits;
1153 port->total_credits = total;
1160 * tb_port_update_credits() - Re-read port total credits
1161 * @port: Port to update
1163 * After the link is bonded (or bonding was disabled) the port total
1164 * credits may change, so this function needs to be called to re-read
1165 * the credits. Updates also the second lane adapter.
1167 int tb_port_update_credits(struct tb_port *port)
1171 ret = tb_port_do_update_credits(port);
1174 return tb_port_do_update_credits(port->dual_link_port);
1177 static int __tb_port_pm_secondary_set(struct tb_port *port, bool secondary)
1182 ret = tb_port_read(port, &phy, TB_CFG_PORT,
1183 port->cap_phy + LANE_ADP_CS_1, 1);
1188 phy |= LANE_ADP_CS_1_PMS;
1190 phy &= ~LANE_ADP_CS_1_PMS;
1192 return tb_port_write(port, &phy, TB_CFG_PORT,
1193 port->cap_phy + LANE_ADP_CS_1, 1);
1196 static int tb_port_pm_secondary_enable(struct tb_port *port)
1198 return __tb_port_pm_secondary_set(port, true);
1201 static int tb_port_pm_secondary_disable(struct tb_port *port)
1203 return __tb_port_pm_secondary_set(port, false);
1206 /* Called for USB4 or Titan Ridge routers only */
1207 static bool tb_port_clx_supported(struct tb_port *port, unsigned int clx_mask)
1212 /* Don't enable CLx in case of two single-lane links */
1213 if (!port->bonded && port->dual_link_port)
1216 /* Don't enable CLx in case of inter-domain link */
1220 if (tb_switch_is_usb4(port->sw)) {
1221 if (!usb4_port_clx_supported(port))
1223 } else if (!tb_lc_is_clx_supported(port)) {
1227 if (clx_mask & TB_CL1) {
1228 /* CL0s and CL1 are enabled and supported together */
1229 mask |= LANE_ADP_CS_0_CL0S_SUPPORT | LANE_ADP_CS_0_CL1_SUPPORT;
1231 if (clx_mask & TB_CL2)
1232 mask |= LANE_ADP_CS_0_CL2_SUPPORT;
1234 ret = tb_port_read(port, &val, TB_CFG_PORT,
1235 port->cap_phy + LANE_ADP_CS_0, 1);
1239 return !!(val & mask);
1242 static int __tb_port_clx_set(struct tb_port *port, enum tb_clx clx, bool enable)
1247 /* CL0s and CL1 are enabled and supported together */
1249 mask = LANE_ADP_CS_1_CL0S_ENABLE | LANE_ADP_CS_1_CL1_ENABLE;
1251 /* For now we support only CL0s and CL1. Not CL2 */
1254 ret = tb_port_read(port, &phy, TB_CFG_PORT,
1255 port->cap_phy + LANE_ADP_CS_1, 1);
1264 return tb_port_write(port, &phy, TB_CFG_PORT,
1265 port->cap_phy + LANE_ADP_CS_1, 1);
1268 static int tb_port_clx_disable(struct tb_port *port, enum tb_clx clx)
1270 return __tb_port_clx_set(port, clx, false);
1273 static int tb_port_clx_enable(struct tb_port *port, enum tb_clx clx)
1275 return __tb_port_clx_set(port, clx, true);
1279 * tb_port_is_clx_enabled() - Is given CL state enabled
1280 * @port: USB4 port to check
1281 * @clx_mask: Mask of CL states to check
1283 * Returns true if any of the given CL states is enabled for @port.
1285 bool tb_port_is_clx_enabled(struct tb_port *port, unsigned int clx_mask)
1290 if (!tb_port_clx_supported(port, clx_mask))
1293 if (clx_mask & TB_CL1)
1294 mask |= LANE_ADP_CS_1_CL0S_ENABLE | LANE_ADP_CS_1_CL1_ENABLE;
1295 if (clx_mask & TB_CL2)
1296 mask |= LANE_ADP_CS_1_CL2_ENABLE;
1298 ret = tb_port_read(port, &val, TB_CFG_PORT,
1299 port->cap_phy + LANE_ADP_CS_1, 1);
1303 return !!(val & mask);
1306 static int tb_port_start_lane_initialization(struct tb_port *port)
1310 if (tb_switch_is_usb4(port->sw))
1313 ret = tb_lc_start_lane_initialization(port);
1314 return ret == -EINVAL ? 0 : ret;
1318 * Returns true if the port had something (router, XDomain) connected
1321 static bool tb_port_resume(struct tb_port *port)
1323 bool has_remote = tb_port_has_remote(port);
1326 usb4_port_device_resume(port->usb4);
1327 } else if (!has_remote) {
1329 * For disconnected downstream lane adapters start lane
1330 * initialization now so we detect future connects.
1332 * For XDomain start the lane initialzation now so the
1333 * link gets re-established.
1335 * This is only needed for non-USB4 ports.
1337 if (!tb_is_upstream_port(port) || port->xdomain)
1338 tb_port_start_lane_initialization(port);
1341 return has_remote || port->xdomain;
1345 * tb_port_is_enabled() - Is the adapter port enabled
1346 * @port: Port to check
1348 bool tb_port_is_enabled(struct tb_port *port)
1350 switch (port->config.type) {
1351 case TB_TYPE_PCIE_UP:
1352 case TB_TYPE_PCIE_DOWN:
1353 return tb_pci_port_is_enabled(port);
1355 case TB_TYPE_DP_HDMI_IN:
1356 case TB_TYPE_DP_HDMI_OUT:
1357 return tb_dp_port_is_enabled(port);
1359 case TB_TYPE_USB3_UP:
1360 case TB_TYPE_USB3_DOWN:
1361 return tb_usb3_port_is_enabled(port);
1369 * tb_usb3_port_is_enabled() - Is the USB3 adapter port enabled
1370 * @port: USB3 adapter port to check
1372 bool tb_usb3_port_is_enabled(struct tb_port *port)
1376 if (tb_port_read(port, &data, TB_CFG_PORT,
1377 port->cap_adap + ADP_USB3_CS_0, 1))
1380 return !!(data & ADP_USB3_CS_0_PE);
1384 * tb_usb3_port_enable() - Enable USB3 adapter port
1385 * @port: USB3 adapter port to enable
1386 * @enable: Enable/disable the USB3 adapter
1388 int tb_usb3_port_enable(struct tb_port *port, bool enable)
1390 u32 word = enable ? (ADP_USB3_CS_0_PE | ADP_USB3_CS_0_V)
1393 if (!port->cap_adap)
1395 return tb_port_write(port, &word, TB_CFG_PORT,
1396 port->cap_adap + ADP_USB3_CS_0, 1);
1400 * tb_pci_port_is_enabled() - Is the PCIe adapter port enabled
1401 * @port: PCIe port to check
1403 bool tb_pci_port_is_enabled(struct tb_port *port)
1407 if (tb_port_read(port, &data, TB_CFG_PORT,
1408 port->cap_adap + ADP_PCIE_CS_0, 1))
1411 return !!(data & ADP_PCIE_CS_0_PE);
1415 * tb_pci_port_enable() - Enable PCIe adapter port
1416 * @port: PCIe port to enable
1417 * @enable: Enable/disable the PCIe adapter
1419 int tb_pci_port_enable(struct tb_port *port, bool enable)
1421 u32 word = enable ? ADP_PCIE_CS_0_PE : 0x0;
1422 if (!port->cap_adap)
1424 return tb_port_write(port, &word, TB_CFG_PORT,
1425 port->cap_adap + ADP_PCIE_CS_0, 1);
1429 * tb_dp_port_hpd_is_active() - Is HPD already active
1430 * @port: DP out port to check
1432 * Checks if the DP OUT adapter port has HDP bit already set.
1434 int tb_dp_port_hpd_is_active(struct tb_port *port)
1439 ret = tb_port_read(port, &data, TB_CFG_PORT,
1440 port->cap_adap + ADP_DP_CS_2, 1);
1444 return !!(data & ADP_DP_CS_2_HDP);
1448 * tb_dp_port_hpd_clear() - Clear HPD from DP IN port
1449 * @port: Port to clear HPD
1451 * If the DP IN port has HDP set, this function can be used to clear it.
1453 int tb_dp_port_hpd_clear(struct tb_port *port)
1458 ret = tb_port_read(port, &data, TB_CFG_PORT,
1459 port->cap_adap + ADP_DP_CS_3, 1);
1463 data |= ADP_DP_CS_3_HDPC;
1464 return tb_port_write(port, &data, TB_CFG_PORT,
1465 port->cap_adap + ADP_DP_CS_3, 1);
1469 * tb_dp_port_set_hops() - Set video/aux Hop IDs for DP port
1470 * @port: DP IN/OUT port to set hops
1471 * @video: Video Hop ID
1472 * @aux_tx: AUX TX Hop ID
1473 * @aux_rx: AUX RX Hop ID
1475 * Programs specified Hop IDs for DP IN/OUT port. Can be called for USB4
1476 * router DP adapters too but does not program the values as the fields
1479 int tb_dp_port_set_hops(struct tb_port *port, unsigned int video,
1480 unsigned int aux_tx, unsigned int aux_rx)
1485 if (tb_switch_is_usb4(port->sw))
1488 ret = tb_port_read(port, data, TB_CFG_PORT,
1489 port->cap_adap + ADP_DP_CS_0, ARRAY_SIZE(data));
1493 data[0] &= ~ADP_DP_CS_0_VIDEO_HOPID_MASK;
1494 data[1] &= ~ADP_DP_CS_1_AUX_RX_HOPID_MASK;
1495 data[1] &= ~ADP_DP_CS_1_AUX_RX_HOPID_MASK;
1497 data[0] |= (video << ADP_DP_CS_0_VIDEO_HOPID_SHIFT) &
1498 ADP_DP_CS_0_VIDEO_HOPID_MASK;
1499 data[1] |= aux_tx & ADP_DP_CS_1_AUX_TX_HOPID_MASK;
1500 data[1] |= (aux_rx << ADP_DP_CS_1_AUX_RX_HOPID_SHIFT) &
1501 ADP_DP_CS_1_AUX_RX_HOPID_MASK;
1503 return tb_port_write(port, data, TB_CFG_PORT,
1504 port->cap_adap + ADP_DP_CS_0, ARRAY_SIZE(data));
1508 * tb_dp_port_is_enabled() - Is DP adapter port enabled
1509 * @port: DP adapter port to check
1511 bool tb_dp_port_is_enabled(struct tb_port *port)
1515 if (tb_port_read(port, data, TB_CFG_PORT, port->cap_adap + ADP_DP_CS_0,
1519 return !!(data[0] & (ADP_DP_CS_0_VE | ADP_DP_CS_0_AE));
1523 * tb_dp_port_enable() - Enables/disables DP paths of a port
1524 * @port: DP IN/OUT port
1525 * @enable: Enable/disable DP path
1527 * Once Hop IDs are programmed DP paths can be enabled or disabled by
1528 * calling this function.
1530 int tb_dp_port_enable(struct tb_port *port, bool enable)
1535 ret = tb_port_read(port, data, TB_CFG_PORT,
1536 port->cap_adap + ADP_DP_CS_0, ARRAY_SIZE(data));
1541 data[0] |= ADP_DP_CS_0_VE | ADP_DP_CS_0_AE;
1543 data[0] &= ~(ADP_DP_CS_0_VE | ADP_DP_CS_0_AE);
1545 return tb_port_write(port, data, TB_CFG_PORT,
1546 port->cap_adap + ADP_DP_CS_0, ARRAY_SIZE(data));
1549 /* switch utility functions */
1551 static const char *tb_switch_generation_name(const struct tb_switch *sw)
1553 switch (sw->generation) {
1555 return "Thunderbolt 1";
1557 return "Thunderbolt 2";
1559 return "Thunderbolt 3";
1567 static void tb_dump_switch(const struct tb *tb, const struct tb_switch *sw)
1569 const struct tb_regs_switch_header *regs = &sw->config;
1571 tb_dbg(tb, " %s Switch: %x:%x (Revision: %d, TB Version: %d)\n",
1572 tb_switch_generation_name(sw), regs->vendor_id, regs->device_id,
1573 regs->revision, regs->thunderbolt_version);
1574 tb_dbg(tb, " Max Port Number: %d\n", regs->max_port_number);
1575 tb_dbg(tb, " Config:\n");
1577 " Upstream Port Number: %d Depth: %d Route String: %#llx Enabled: %d, PlugEventsDelay: %dms\n",
1578 regs->upstream_port_number, regs->depth,
1579 (((u64) regs->route_hi) << 32) | regs->route_lo,
1580 regs->enabled, regs->plug_events_delay);
1581 tb_dbg(tb, " unknown1: %#x unknown4: %#x\n",
1582 regs->__unknown1, regs->__unknown4);
1586 * tb_switch_reset() - reconfigure route, enable and send TB_CFG_PKG_RESET
1587 * @sw: Switch to reset
1589 * Return: Returns 0 on success or an error code on failure.
1591 int tb_switch_reset(struct tb_switch *sw)
1593 struct tb_cfg_result res;
1595 if (sw->generation > 1)
1598 tb_sw_dbg(sw, "resetting switch\n");
1600 res.err = tb_sw_write(sw, ((u32 *) &sw->config) + 2,
1601 TB_CFG_SWITCH, 2, 2);
1604 res = tb_cfg_reset(sw->tb->ctl, tb_route(sw));
1611 * tb_switch_wait_for_bit() - Wait for specified value of bits in offset
1612 * @sw: Router to read the offset value from
1613 * @offset: Offset in the router config space to read from
1614 * @bit: Bit mask in the offset to wait for
1615 * @value: Value of the bits to wait for
1616 * @timeout_msec: Timeout in ms how long to wait
1618 * Wait till the specified bits in specified offset reach specified value.
1619 * Returns %0 in case of success, %-ETIMEDOUT if the @value was not reached
1620 * within the given timeout or a negative errno in case of failure.
1622 int tb_switch_wait_for_bit(struct tb_switch *sw, u32 offset, u32 bit,
1623 u32 value, int timeout_msec)
1625 ktime_t timeout = ktime_add_ms(ktime_get(), timeout_msec);
1631 ret = tb_sw_read(sw, &val, TB_CFG_SWITCH, offset, 1);
1635 if ((val & bit) == value)
1638 usleep_range(50, 100);
1639 } while (ktime_before(ktime_get(), timeout));
1645 * tb_plug_events_active() - enable/disable plug events on a switch
1647 * Also configures a sane plug_events_delay of 255ms.
1649 * Return: Returns 0 on success or an error code on failure.
1651 static int tb_plug_events_active(struct tb_switch *sw, bool active)
1656 if (tb_switch_is_icm(sw) || tb_switch_is_usb4(sw))
1659 sw->config.plug_events_delay = 0xff;
1660 res = tb_sw_write(sw, ((u32 *) &sw->config) + 4, TB_CFG_SWITCH, 4, 1);
1664 res = tb_sw_read(sw, &data, TB_CFG_SWITCH, sw->cap_plug_events + 1, 1);
1669 data = data & 0xFFFFFF83;
1670 switch (sw->config.device_id) {
1671 case PCI_DEVICE_ID_INTEL_LIGHT_RIDGE:
1672 case PCI_DEVICE_ID_INTEL_EAGLE_RIDGE:
1673 case PCI_DEVICE_ID_INTEL_PORT_RIDGE:
1677 * Skip Alpine Ridge, it needs to have vendor
1678 * specific USB hotplug event enabled for the
1679 * internal xHCI to work.
1681 if (!tb_switch_is_alpine_ridge(sw))
1682 data |= TB_PLUG_EVENTS_USB_DISABLE;
1687 return tb_sw_write(sw, &data, TB_CFG_SWITCH,
1688 sw->cap_plug_events + 1, 1);
1691 static ssize_t authorized_show(struct device *dev,
1692 struct device_attribute *attr,
1695 struct tb_switch *sw = tb_to_switch(dev);
1697 return sysfs_emit(buf, "%u\n", sw->authorized);
1700 static int disapprove_switch(struct device *dev, void *not_used)
1702 char *envp[] = { "AUTHORIZED=0", NULL };
1703 struct tb_switch *sw;
1705 sw = tb_to_switch(dev);
1706 if (sw && sw->authorized) {
1709 /* First children */
1710 ret = device_for_each_child_reverse(&sw->dev, NULL, disapprove_switch);
1714 ret = tb_domain_disapprove_switch(sw->tb, sw);
1719 kobject_uevent_env(&sw->dev.kobj, KOBJ_CHANGE, envp);
1725 static int tb_switch_set_authorized(struct tb_switch *sw, unsigned int val)
1727 char envp_string[13];
1729 char *envp[] = { envp_string, NULL };
1731 if (!mutex_trylock(&sw->tb->lock))
1732 return restart_syscall();
1734 if (!!sw->authorized == !!val)
1738 /* Disapprove switch */
1741 ret = disapprove_switch(&sw->dev, NULL);
1746 /* Approve switch */
1749 ret = tb_domain_approve_switch_key(sw->tb, sw);
1751 ret = tb_domain_approve_switch(sw->tb, sw);
1754 /* Challenge switch */
1757 ret = tb_domain_challenge_switch_key(sw->tb, sw);
1765 sw->authorized = val;
1767 * Notify status change to the userspace, informing the new
1768 * value of /sys/bus/thunderbolt/devices/.../authorized.
1770 sprintf(envp_string, "AUTHORIZED=%u", sw->authorized);
1771 kobject_uevent_env(&sw->dev.kobj, KOBJ_CHANGE, envp);
1775 mutex_unlock(&sw->tb->lock);
1779 static ssize_t authorized_store(struct device *dev,
1780 struct device_attribute *attr,
1781 const char *buf, size_t count)
1783 struct tb_switch *sw = tb_to_switch(dev);
1787 ret = kstrtouint(buf, 0, &val);
1793 pm_runtime_get_sync(&sw->dev);
1794 ret = tb_switch_set_authorized(sw, val);
1795 pm_runtime_mark_last_busy(&sw->dev);
1796 pm_runtime_put_autosuspend(&sw->dev);
1798 return ret ? ret : count;
1800 static DEVICE_ATTR_RW(authorized);
1802 static ssize_t boot_show(struct device *dev, struct device_attribute *attr,
1805 struct tb_switch *sw = tb_to_switch(dev);
1807 return sysfs_emit(buf, "%u\n", sw->boot);
1809 static DEVICE_ATTR_RO(boot);
1811 static ssize_t device_show(struct device *dev, struct device_attribute *attr,
1814 struct tb_switch *sw = tb_to_switch(dev);
1816 return sysfs_emit(buf, "%#x\n", sw->device);
1818 static DEVICE_ATTR_RO(device);
1821 device_name_show(struct device *dev, struct device_attribute *attr, char *buf)
1823 struct tb_switch *sw = tb_to_switch(dev);
1825 return sysfs_emit(buf, "%s\n", sw->device_name ?: "");
1827 static DEVICE_ATTR_RO(device_name);
1830 generation_show(struct device *dev, struct device_attribute *attr, char *buf)
1832 struct tb_switch *sw = tb_to_switch(dev);
1834 return sysfs_emit(buf, "%u\n", sw->generation);
1836 static DEVICE_ATTR_RO(generation);
1838 static ssize_t key_show(struct device *dev, struct device_attribute *attr,
1841 struct tb_switch *sw = tb_to_switch(dev);
1844 if (!mutex_trylock(&sw->tb->lock))
1845 return restart_syscall();
1848 ret = sysfs_emit(buf, "%*phN\n", TB_SWITCH_KEY_SIZE, sw->key);
1850 ret = sysfs_emit(buf, "\n");
1852 mutex_unlock(&sw->tb->lock);
1856 static ssize_t key_store(struct device *dev, struct device_attribute *attr,
1857 const char *buf, size_t count)
1859 struct tb_switch *sw = tb_to_switch(dev);
1860 u8 key[TB_SWITCH_KEY_SIZE];
1861 ssize_t ret = count;
1864 if (!strcmp(buf, "\n"))
1866 else if (hex2bin(key, buf, sizeof(key)))
1869 if (!mutex_trylock(&sw->tb->lock))
1870 return restart_syscall();
1872 if (sw->authorized) {
1879 sw->key = kmemdup(key, sizeof(key), GFP_KERNEL);
1885 mutex_unlock(&sw->tb->lock);
1888 static DEVICE_ATTR(key, 0600, key_show, key_store);
1890 static ssize_t speed_show(struct device *dev, struct device_attribute *attr,
1893 struct tb_switch *sw = tb_to_switch(dev);
1895 return sysfs_emit(buf, "%u.0 Gb/s\n", sw->link_speed);
1899 * Currently all lanes must run at the same speed but we expose here
1900 * both directions to allow possible asymmetric links in the future.
1902 static DEVICE_ATTR(rx_speed, 0444, speed_show, NULL);
1903 static DEVICE_ATTR(tx_speed, 0444, speed_show, NULL);
1905 static ssize_t lanes_show(struct device *dev, struct device_attribute *attr,
1908 struct tb_switch *sw = tb_to_switch(dev);
1910 return sysfs_emit(buf, "%u\n", sw->link_width);
1914 * Currently link has same amount of lanes both directions (1 or 2) but
1915 * expose them separately to allow possible asymmetric links in the future.
1917 static DEVICE_ATTR(rx_lanes, 0444, lanes_show, NULL);
1918 static DEVICE_ATTR(tx_lanes, 0444, lanes_show, NULL);
1920 static ssize_t nvm_authenticate_show(struct device *dev,
1921 struct device_attribute *attr, char *buf)
1923 struct tb_switch *sw = tb_to_switch(dev);
1926 nvm_get_auth_status(sw, &status);
1927 return sysfs_emit(buf, "%#x\n", status);
1930 static ssize_t nvm_authenticate_sysfs(struct device *dev, const char *buf,
1933 struct tb_switch *sw = tb_to_switch(dev);
1936 pm_runtime_get_sync(&sw->dev);
1938 if (!mutex_trylock(&sw->tb->lock)) {
1939 ret = restart_syscall();
1943 if (sw->no_nvm_upgrade) {
1948 /* If NVMem devices are not yet added */
1954 ret = kstrtoint(buf, 10, &val);
1958 /* Always clear the authentication status */
1959 nvm_clear_auth_status(sw);
1962 if (val == AUTHENTICATE_ONLY) {
1966 ret = nvm_authenticate(sw, true);
1968 if (!sw->nvm->flushed) {
1969 if (!sw->nvm->buf) {
1974 ret = nvm_validate_and_write(sw);
1975 if (ret || val == WRITE_ONLY)
1978 if (val == WRITE_AND_AUTHENTICATE) {
1980 ret = tb_lc_force_power(sw);
1982 ret = nvm_authenticate(sw, false);
1988 mutex_unlock(&sw->tb->lock);
1990 pm_runtime_mark_last_busy(&sw->dev);
1991 pm_runtime_put_autosuspend(&sw->dev);
1996 static ssize_t nvm_authenticate_store(struct device *dev,
1997 struct device_attribute *attr, const char *buf, size_t count)
1999 int ret = nvm_authenticate_sysfs(dev, buf, false);
2004 static DEVICE_ATTR_RW(nvm_authenticate);
2006 static ssize_t nvm_authenticate_on_disconnect_show(struct device *dev,
2007 struct device_attribute *attr, char *buf)
2009 return nvm_authenticate_show(dev, attr, buf);
2012 static ssize_t nvm_authenticate_on_disconnect_store(struct device *dev,
2013 struct device_attribute *attr, const char *buf, size_t count)
2017 ret = nvm_authenticate_sysfs(dev, buf, true);
2018 return ret ? ret : count;
2020 static DEVICE_ATTR_RW(nvm_authenticate_on_disconnect);
2022 static ssize_t nvm_version_show(struct device *dev,
2023 struct device_attribute *attr, char *buf)
2025 struct tb_switch *sw = tb_to_switch(dev);
2028 if (!mutex_trylock(&sw->tb->lock))
2029 return restart_syscall();
2036 ret = sysfs_emit(buf, "%x.%x\n", sw->nvm->major, sw->nvm->minor);
2038 mutex_unlock(&sw->tb->lock);
2042 static DEVICE_ATTR_RO(nvm_version);
2044 static ssize_t vendor_show(struct device *dev, struct device_attribute *attr,
2047 struct tb_switch *sw = tb_to_switch(dev);
2049 return sysfs_emit(buf, "%#x\n", sw->vendor);
2051 static DEVICE_ATTR_RO(vendor);
2054 vendor_name_show(struct device *dev, struct device_attribute *attr, char *buf)
2056 struct tb_switch *sw = tb_to_switch(dev);
2058 return sysfs_emit(buf, "%s\n", sw->vendor_name ?: "");
2060 static DEVICE_ATTR_RO(vendor_name);
2062 static ssize_t unique_id_show(struct device *dev, struct device_attribute *attr,
2065 struct tb_switch *sw = tb_to_switch(dev);
2067 return sysfs_emit(buf, "%pUb\n", sw->uuid);
2069 static DEVICE_ATTR_RO(unique_id);
2071 static struct attribute *switch_attrs[] = {
2072 &dev_attr_authorized.attr,
2073 &dev_attr_boot.attr,
2074 &dev_attr_device.attr,
2075 &dev_attr_device_name.attr,
2076 &dev_attr_generation.attr,
2078 &dev_attr_nvm_authenticate.attr,
2079 &dev_attr_nvm_authenticate_on_disconnect.attr,
2080 &dev_attr_nvm_version.attr,
2081 &dev_attr_rx_speed.attr,
2082 &dev_attr_rx_lanes.attr,
2083 &dev_attr_tx_speed.attr,
2084 &dev_attr_tx_lanes.attr,
2085 &dev_attr_vendor.attr,
2086 &dev_attr_vendor_name.attr,
2087 &dev_attr_unique_id.attr,
2091 static umode_t switch_attr_is_visible(struct kobject *kobj,
2092 struct attribute *attr, int n)
2094 struct device *dev = kobj_to_dev(kobj);
2095 struct tb_switch *sw = tb_to_switch(dev);
2097 if (attr == &dev_attr_authorized.attr) {
2098 if (sw->tb->security_level == TB_SECURITY_NOPCIE ||
2099 sw->tb->security_level == TB_SECURITY_DPONLY)
2101 } else if (attr == &dev_attr_device.attr) {
2104 } else if (attr == &dev_attr_device_name.attr) {
2105 if (!sw->device_name)
2107 } else if (attr == &dev_attr_vendor.attr) {
2110 } else if (attr == &dev_attr_vendor_name.attr) {
2111 if (!sw->vendor_name)
2113 } else if (attr == &dev_attr_key.attr) {
2115 sw->tb->security_level == TB_SECURITY_SECURE &&
2116 sw->security_level == TB_SECURITY_SECURE)
2119 } else if (attr == &dev_attr_rx_speed.attr ||
2120 attr == &dev_attr_rx_lanes.attr ||
2121 attr == &dev_attr_tx_speed.attr ||
2122 attr == &dev_attr_tx_lanes.attr) {
2126 } else if (attr == &dev_attr_nvm_authenticate.attr) {
2127 if (nvm_upgradeable(sw))
2130 } else if (attr == &dev_attr_nvm_version.attr) {
2131 if (nvm_readable(sw))
2134 } else if (attr == &dev_attr_boot.attr) {
2138 } else if (attr == &dev_attr_nvm_authenticate_on_disconnect.attr) {
2139 if (sw->quirks & QUIRK_FORCE_POWER_LINK_CONTROLLER)
2144 return sw->safe_mode ? 0 : attr->mode;
2147 static const struct attribute_group switch_group = {
2148 .is_visible = switch_attr_is_visible,
2149 .attrs = switch_attrs,
2152 static const struct attribute_group *switch_groups[] = {
2157 static void tb_switch_release(struct device *dev)
2159 struct tb_switch *sw = tb_to_switch(dev);
2160 struct tb_port *port;
2162 dma_port_free(sw->dma_port);
2164 tb_switch_for_each_port(sw, port) {
2165 ida_destroy(&port->in_hopids);
2166 ida_destroy(&port->out_hopids);
2170 kfree(sw->device_name);
2171 kfree(sw->vendor_name);
2178 static int tb_switch_uevent(struct device *dev, struct kobj_uevent_env *env)
2180 struct tb_switch *sw = tb_to_switch(dev);
2183 if (sw->config.thunderbolt_version == USB4_VERSION_1_0) {
2184 if (add_uevent_var(env, "USB4_VERSION=1.0"))
2188 if (!tb_route(sw)) {
2191 const struct tb_port *port;
2194 /* Device is hub if it has any downstream ports */
2195 tb_switch_for_each_port(sw, port) {
2196 if (!port->disabled && !tb_is_upstream_port(port) &&
2197 tb_port_is_null(port)) {
2203 type = hub ? "hub" : "device";
2206 if (add_uevent_var(env, "USB4_TYPE=%s", type))
2212 * Currently only need to provide the callbacks. Everything else is handled
2213 * in the connection manager.
2215 static int __maybe_unused tb_switch_runtime_suspend(struct device *dev)
2217 struct tb_switch *sw = tb_to_switch(dev);
2218 const struct tb_cm_ops *cm_ops = sw->tb->cm_ops;
2220 if (cm_ops->runtime_suspend_switch)
2221 return cm_ops->runtime_suspend_switch(sw);
2226 static int __maybe_unused tb_switch_runtime_resume(struct device *dev)
2228 struct tb_switch *sw = tb_to_switch(dev);
2229 const struct tb_cm_ops *cm_ops = sw->tb->cm_ops;
2231 if (cm_ops->runtime_resume_switch)
2232 return cm_ops->runtime_resume_switch(sw);
2236 static const struct dev_pm_ops tb_switch_pm_ops = {
2237 SET_RUNTIME_PM_OPS(tb_switch_runtime_suspend, tb_switch_runtime_resume,
2241 struct device_type tb_switch_type = {
2242 .name = "thunderbolt_device",
2243 .release = tb_switch_release,
2244 .uevent = tb_switch_uevent,
2245 .pm = &tb_switch_pm_ops,
2248 static int tb_switch_get_generation(struct tb_switch *sw)
2250 switch (sw->config.device_id) {
2251 case PCI_DEVICE_ID_INTEL_LIGHT_RIDGE:
2252 case PCI_DEVICE_ID_INTEL_EAGLE_RIDGE:
2253 case PCI_DEVICE_ID_INTEL_LIGHT_PEAK:
2254 case PCI_DEVICE_ID_INTEL_CACTUS_RIDGE_2C:
2255 case PCI_DEVICE_ID_INTEL_CACTUS_RIDGE_4C:
2256 case PCI_DEVICE_ID_INTEL_PORT_RIDGE:
2257 case PCI_DEVICE_ID_INTEL_REDWOOD_RIDGE_2C_BRIDGE:
2258 case PCI_DEVICE_ID_INTEL_REDWOOD_RIDGE_4C_BRIDGE:
2261 case PCI_DEVICE_ID_INTEL_WIN_RIDGE_2C_BRIDGE:
2262 case PCI_DEVICE_ID_INTEL_FALCON_RIDGE_2C_BRIDGE:
2263 case PCI_DEVICE_ID_INTEL_FALCON_RIDGE_4C_BRIDGE:
2266 case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_LP_BRIDGE:
2267 case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_2C_BRIDGE:
2268 case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_4C_BRIDGE:
2269 case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_C_2C_BRIDGE:
2270 case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_C_4C_BRIDGE:
2271 case PCI_DEVICE_ID_INTEL_TITAN_RIDGE_2C_BRIDGE:
2272 case PCI_DEVICE_ID_INTEL_TITAN_RIDGE_4C_BRIDGE:
2273 case PCI_DEVICE_ID_INTEL_TITAN_RIDGE_DD_BRIDGE:
2274 case PCI_DEVICE_ID_INTEL_ICL_NHI0:
2275 case PCI_DEVICE_ID_INTEL_ICL_NHI1:
2279 if (tb_switch_is_usb4(sw))
2283 * For unknown switches assume generation to be 1 to be
2286 tb_sw_warn(sw, "unsupported switch device id %#x\n",
2287 sw->config.device_id);
2292 static bool tb_switch_exceeds_max_depth(const struct tb_switch *sw, int depth)
2296 if (tb_switch_is_usb4(sw) ||
2297 (sw->tb->root_switch && tb_switch_is_usb4(sw->tb->root_switch)))
2298 max_depth = USB4_SWITCH_MAX_DEPTH;
2300 max_depth = TB_SWITCH_MAX_DEPTH;
2302 return depth > max_depth;
2306 * tb_switch_alloc() - allocate a switch
2307 * @tb: Pointer to the owning domain
2308 * @parent: Parent device for this switch
2309 * @route: Route string for this switch
2311 * Allocates and initializes a switch. Will not upload configuration to
2312 * the switch. For that you need to call tb_switch_configure()
2313 * separately. The returned switch should be released by calling
2316 * Return: Pointer to the allocated switch or ERR_PTR() in case of
2319 struct tb_switch *tb_switch_alloc(struct tb *tb, struct device *parent,
2322 struct tb_switch *sw;
2326 /* Unlock the downstream port so we can access the switch below */
2328 struct tb_switch *parent_sw = tb_to_switch(parent);
2329 struct tb_port *down;
2331 down = tb_port_at(route, parent_sw);
2332 tb_port_unlock(down);
2335 depth = tb_route_length(route);
2337 upstream_port = tb_cfg_get_upstream_port(tb->ctl, route);
2338 if (upstream_port < 0)
2339 return ERR_PTR(upstream_port);
2341 sw = kzalloc(sizeof(*sw), GFP_KERNEL);
2343 return ERR_PTR(-ENOMEM);
2346 ret = tb_cfg_read(tb->ctl, &sw->config, route, 0, TB_CFG_SWITCH, 0, 5);
2348 goto err_free_sw_ports;
2350 sw->generation = tb_switch_get_generation(sw);
2352 tb_dbg(tb, "current switch config:\n");
2353 tb_dump_switch(tb, sw);
2355 /* configure switch */
2356 sw->config.upstream_port_number = upstream_port;
2357 sw->config.depth = depth;
2358 sw->config.route_hi = upper_32_bits(route);
2359 sw->config.route_lo = lower_32_bits(route);
2360 sw->config.enabled = 0;
2362 /* Make sure we do not exceed maximum topology limit */
2363 if (tb_switch_exceeds_max_depth(sw, depth)) {
2364 ret = -EADDRNOTAVAIL;
2365 goto err_free_sw_ports;
2368 /* initialize ports */
2369 sw->ports = kcalloc(sw->config.max_port_number + 1, sizeof(*sw->ports),
2373 goto err_free_sw_ports;
2376 for (i = 0; i <= sw->config.max_port_number; i++) {
2377 /* minimum setup for tb_find_cap and tb_drom_read to work */
2378 sw->ports[i].sw = sw;
2379 sw->ports[i].port = i;
2381 /* Control port does not need HopID allocation */
2383 ida_init(&sw->ports[i].in_hopids);
2384 ida_init(&sw->ports[i].out_hopids);
2388 ret = tb_switch_find_vse_cap(sw, TB_VSE_CAP_PLUG_EVENTS);
2390 sw->cap_plug_events = ret;
2392 ret = tb_switch_find_vse_cap(sw, TB_VSE_CAP_TIME2);
2394 sw->cap_vsec_tmu = ret;
2396 ret = tb_switch_find_vse_cap(sw, TB_VSE_CAP_LINK_CONTROLLER);
2400 ret = tb_switch_find_vse_cap(sw, TB_VSE_CAP_CP_LP);
2404 /* Root switch is always authorized */
2406 sw->authorized = true;
2408 device_initialize(&sw->dev);
2409 sw->dev.parent = parent;
2410 sw->dev.bus = &tb_bus_type;
2411 sw->dev.type = &tb_switch_type;
2412 sw->dev.groups = switch_groups;
2413 dev_set_name(&sw->dev, "%u-%llx", tb->index, tb_route(sw));
2421 return ERR_PTR(ret);
2425 * tb_switch_alloc_safe_mode() - allocate a switch that is in safe mode
2426 * @tb: Pointer to the owning domain
2427 * @parent: Parent device for this switch
2428 * @route: Route string for this switch
2430 * This creates a switch in safe mode. This means the switch pretty much
2431 * lacks all capabilities except DMA configuration port before it is
2432 * flashed with a valid NVM firmware.
2434 * The returned switch must be released by calling tb_switch_put().
2436 * Return: Pointer to the allocated switch or ERR_PTR() in case of failure
2439 tb_switch_alloc_safe_mode(struct tb *tb, struct device *parent, u64 route)
2441 struct tb_switch *sw;
2443 sw = kzalloc(sizeof(*sw), GFP_KERNEL);
2445 return ERR_PTR(-ENOMEM);
2448 sw->config.depth = tb_route_length(route);
2449 sw->config.route_hi = upper_32_bits(route);
2450 sw->config.route_lo = lower_32_bits(route);
2451 sw->safe_mode = true;
2453 device_initialize(&sw->dev);
2454 sw->dev.parent = parent;
2455 sw->dev.bus = &tb_bus_type;
2456 sw->dev.type = &tb_switch_type;
2457 sw->dev.groups = switch_groups;
2458 dev_set_name(&sw->dev, "%u-%llx", tb->index, tb_route(sw));
2464 * tb_switch_configure() - Uploads configuration to the switch
2465 * @sw: Switch to configure
2467 * Call this function before the switch is added to the system. It will
2468 * upload configuration to the switch and makes it available for the
2469 * connection manager to use. Can be called to the switch again after
2470 * resume from low power states to re-initialize it.
2472 * Return: %0 in case of success and negative errno in case of failure
2474 int tb_switch_configure(struct tb_switch *sw)
2476 struct tb *tb = sw->tb;
2480 route = tb_route(sw);
2482 tb_dbg(tb, "%s Switch at %#llx (depth: %d, up port: %d)\n",
2483 sw->config.enabled ? "restoring" : "initializing", route,
2484 tb_route_length(route), sw->config.upstream_port_number);
2486 sw->config.enabled = 1;
2488 if (tb_switch_is_usb4(sw)) {
2490 * For USB4 devices, we need to program the CM version
2491 * accordingly so that it knows to expose all the
2492 * additional capabilities.
2494 sw->config.cmuv = USB4_VERSION_1_0;
2495 sw->config.plug_events_delay = 0xa;
2497 /* Enumerate the switch */
2498 ret = tb_sw_write(sw, (u32 *)&sw->config + 1, TB_CFG_SWITCH,
2503 ret = usb4_switch_setup(sw);
2505 if (sw->config.vendor_id != PCI_VENDOR_ID_INTEL)
2506 tb_sw_warn(sw, "unknown switch vendor id %#x\n",
2507 sw->config.vendor_id);
2509 if (!sw->cap_plug_events) {
2510 tb_sw_warn(sw, "cannot find TB_VSE_CAP_PLUG_EVENTS aborting\n");
2514 /* Enumerate the switch */
2515 ret = tb_sw_write(sw, (u32 *)&sw->config + 1, TB_CFG_SWITCH,
2521 return tb_plug_events_active(sw, true);
2524 static int tb_switch_set_uuid(struct tb_switch *sw)
2533 if (tb_switch_is_usb4(sw)) {
2534 ret = usb4_switch_read_uid(sw, &sw->uid);
2540 * The newer controllers include fused UUID as part of
2541 * link controller specific registers
2543 ret = tb_lc_read_uuid(sw, uuid);
2553 * ICM generates UUID based on UID and fills the upper
2554 * two words with ones. This is not strictly following
2555 * UUID format but we want to be compatible with it so
2556 * we do the same here.
2558 uuid[0] = sw->uid & 0xffffffff;
2559 uuid[1] = (sw->uid >> 32) & 0xffffffff;
2560 uuid[2] = 0xffffffff;
2561 uuid[3] = 0xffffffff;
2564 sw->uuid = kmemdup(uuid, sizeof(uuid), GFP_KERNEL);
2570 static int tb_switch_add_dma_port(struct tb_switch *sw)
2575 switch (sw->generation) {
2577 /* Only root switch can be upgraded */
2584 ret = tb_switch_set_uuid(sw);
2591 * DMA port is the only thing available when the switch
2599 if (sw->no_nvm_upgrade)
2602 if (tb_switch_is_usb4(sw)) {
2603 ret = usb4_switch_nvm_authenticate_status(sw, &status);
2608 tb_sw_info(sw, "switch flash authentication failed\n");
2609 nvm_set_auth_status(sw, status);
2615 /* Root switch DMA port requires running firmware */
2616 if (!tb_route(sw) && !tb_switch_is_icm(sw))
2619 sw->dma_port = dma_port_alloc(sw);
2624 * If there is status already set then authentication failed
2625 * when the dma_port_flash_update_auth() returned. Power cycling
2626 * is not needed (it was done already) so only thing we do here
2627 * is to unblock runtime PM of the root port.
2629 nvm_get_auth_status(sw, &status);
2632 nvm_authenticate_complete_dma_port(sw);
2637 * Check status of the previous flash authentication. If there
2638 * is one we need to power cycle the switch in any case to make
2639 * it functional again.
2641 ret = dma_port_flash_update_auth_status(sw->dma_port, &status);
2645 /* Now we can allow root port to suspend again */
2647 nvm_authenticate_complete_dma_port(sw);
2650 tb_sw_info(sw, "switch flash authentication failed\n");
2651 nvm_set_auth_status(sw, status);
2654 tb_sw_info(sw, "power cycling the switch now\n");
2655 dma_port_power_cycle(sw->dma_port);
2658 * We return error here which causes the switch adding failure.
2659 * It should appear back after power cycle is complete.
2664 static void tb_switch_default_link_ports(struct tb_switch *sw)
2668 for (i = 1; i <= sw->config.max_port_number; i++) {
2669 struct tb_port *port = &sw->ports[i];
2670 struct tb_port *subordinate;
2672 if (!tb_port_is_null(port))
2675 /* Check for the subordinate port */
2676 if (i == sw->config.max_port_number ||
2677 !tb_port_is_null(&sw->ports[i + 1]))
2680 /* Link them if not already done so (by DROM) */
2681 subordinate = &sw->ports[i + 1];
2682 if (!port->dual_link_port && !subordinate->dual_link_port) {
2684 port->dual_link_port = subordinate;
2685 subordinate->link_nr = 1;
2686 subordinate->dual_link_port = port;
2688 tb_sw_dbg(sw, "linked ports %d <-> %d\n",
2689 port->port, subordinate->port);
2694 static bool tb_switch_lane_bonding_possible(struct tb_switch *sw)
2696 const struct tb_port *up = tb_upstream_port(sw);
2698 if (!up->dual_link_port || !up->dual_link_port->remote)
2701 if (tb_switch_is_usb4(sw))
2702 return usb4_switch_lane_bonding_possible(sw);
2703 return tb_lc_lane_bonding_possible(sw);
2706 static int tb_switch_update_link_attributes(struct tb_switch *sw)
2709 bool change = false;
2712 if (!tb_route(sw) || tb_switch_is_icm(sw))
2715 up = tb_upstream_port(sw);
2717 ret = tb_port_get_link_speed(up);
2720 if (sw->link_speed != ret)
2722 sw->link_speed = ret;
2724 ret = tb_port_get_link_width(up);
2727 if (sw->link_width != ret)
2729 sw->link_width = ret;
2731 /* Notify userspace that there is possible link attribute change */
2732 if (device_is_registered(&sw->dev) && change)
2733 kobject_uevent(&sw->dev.kobj, KOBJ_CHANGE);
2739 * tb_switch_lane_bonding_enable() - Enable lane bonding
2740 * @sw: Switch to enable lane bonding
2742 * Connection manager can call this function to enable lane bonding of a
2743 * switch. If conditions are correct and both switches support the feature,
2744 * lanes are bonded. It is safe to call this to any switch.
2746 int tb_switch_lane_bonding_enable(struct tb_switch *sw)
2748 struct tb_switch *parent = tb_to_switch(sw->dev.parent);
2749 struct tb_port *up, *down;
2750 u64 route = tb_route(sw);
2756 if (!tb_switch_lane_bonding_possible(sw))
2759 up = tb_upstream_port(sw);
2760 down = tb_port_at(route, parent);
2762 if (!tb_port_is_width_supported(up, 2) ||
2763 !tb_port_is_width_supported(down, 2))
2766 ret = tb_port_lane_bonding_enable(up);
2768 tb_port_warn(up, "failed to enable lane bonding\n");
2772 ret = tb_port_lane_bonding_enable(down);
2774 tb_port_warn(down, "failed to enable lane bonding\n");
2775 tb_port_lane_bonding_disable(up);
2779 ret = tb_port_wait_for_link_width(down, 2, 100);
2781 tb_port_warn(down, "timeout enabling lane bonding\n");
2785 tb_port_update_credits(down);
2786 tb_port_update_credits(up);
2787 tb_switch_update_link_attributes(sw);
2789 tb_sw_dbg(sw, "lane bonding enabled\n");
2794 * tb_switch_lane_bonding_disable() - Disable lane bonding
2795 * @sw: Switch whose lane bonding to disable
2797 * Disables lane bonding between @sw and parent. This can be called even
2798 * if lanes were not bonded originally.
2800 void tb_switch_lane_bonding_disable(struct tb_switch *sw)
2802 struct tb_switch *parent = tb_to_switch(sw->dev.parent);
2803 struct tb_port *up, *down;
2808 up = tb_upstream_port(sw);
2812 down = tb_port_at(tb_route(sw), parent);
2814 tb_port_lane_bonding_disable(up);
2815 tb_port_lane_bonding_disable(down);
2818 * It is fine if we get other errors as the router might have
2821 if (tb_port_wait_for_link_width(down, 1, 100) == -ETIMEDOUT)
2822 tb_sw_warn(sw, "timeout disabling lane bonding\n");
2824 tb_port_update_credits(down);
2825 tb_port_update_credits(up);
2826 tb_switch_update_link_attributes(sw);
2828 tb_sw_dbg(sw, "lane bonding disabled\n");
2832 * tb_switch_configure_link() - Set link configured
2833 * @sw: Switch whose link is configured
2835 * Sets the link upstream from @sw configured (from both ends) so that
2836 * it will not be disconnected when the domain exits sleep. Can be
2837 * called for any switch.
2839 * It is recommended that this is called after lane bonding is enabled.
2841 * Returns %0 on success and negative errno in case of error.
2843 int tb_switch_configure_link(struct tb_switch *sw)
2845 struct tb_port *up, *down;
2848 if (!tb_route(sw) || tb_switch_is_icm(sw))
2851 up = tb_upstream_port(sw);
2852 if (tb_switch_is_usb4(up->sw))
2853 ret = usb4_port_configure(up);
2855 ret = tb_lc_configure_port(up);
2860 if (tb_switch_is_usb4(down->sw))
2861 return usb4_port_configure(down);
2862 return tb_lc_configure_port(down);
2866 * tb_switch_unconfigure_link() - Unconfigure link
2867 * @sw: Switch whose link is unconfigured
2869 * Sets the link unconfigured so the @sw will be disconnected if the
2870 * domain exists sleep.
2872 void tb_switch_unconfigure_link(struct tb_switch *sw)
2874 struct tb_port *up, *down;
2876 if (sw->is_unplugged)
2878 if (!tb_route(sw) || tb_switch_is_icm(sw))
2881 up = tb_upstream_port(sw);
2882 if (tb_switch_is_usb4(up->sw))
2883 usb4_port_unconfigure(up);
2885 tb_lc_unconfigure_port(up);
2888 if (tb_switch_is_usb4(down->sw))
2889 usb4_port_unconfigure(down);
2891 tb_lc_unconfigure_port(down);
2894 static void tb_switch_credits_init(struct tb_switch *sw)
2896 if (tb_switch_is_icm(sw))
2898 if (!tb_switch_is_usb4(sw))
2900 if (usb4_switch_credits_init(sw))
2901 tb_sw_info(sw, "failed to determine preferred buffer allocation, using defaults\n");
2904 static int tb_switch_port_hotplug_enable(struct tb_switch *sw)
2906 struct tb_port *port;
2908 if (tb_switch_is_icm(sw))
2911 tb_switch_for_each_port(sw, port) {
2914 if (!port->cap_usb4)
2917 res = usb4_port_hotplug_enable(port);
2925 * tb_switch_add() - Add a switch to the domain
2926 * @sw: Switch to add
2928 * This is the last step in adding switch to the domain. It will read
2929 * identification information from DROM and initializes ports so that
2930 * they can be used to connect other switches. The switch will be
2931 * exposed to the userspace when this function successfully returns. To
2932 * remove and release the switch, call tb_switch_remove().
2934 * Return: %0 in case of success and negative errno in case of failure
2936 int tb_switch_add(struct tb_switch *sw)
2941 * Initialize DMA control port now before we read DROM. Recent
2942 * host controllers have more complete DROM on NVM that includes
2943 * vendor and model identification strings which we then expose
2944 * to the userspace. NVM can be accessed through DMA
2945 * configuration based mailbox.
2947 ret = tb_switch_add_dma_port(sw);
2949 dev_err(&sw->dev, "failed to add DMA port\n");
2953 if (!sw->safe_mode) {
2954 tb_switch_credits_init(sw);
2957 ret = tb_drom_read(sw);
2959 dev_warn(&sw->dev, "reading DROM failed: %d\n", ret);
2960 tb_sw_dbg(sw, "uid: %#llx\n", sw->uid);
2962 ret = tb_switch_set_uuid(sw);
2964 dev_err(&sw->dev, "failed to set UUID\n");
2968 for (i = 0; i <= sw->config.max_port_number; i++) {
2969 if (sw->ports[i].disabled) {
2970 tb_port_dbg(&sw->ports[i], "disabled by eeprom\n");
2973 ret = tb_init_port(&sw->ports[i]);
2975 dev_err(&sw->dev, "failed to initialize port %d\n", i);
2980 tb_check_quirks(sw);
2982 tb_switch_default_link_ports(sw);
2984 ret = tb_switch_update_link_attributes(sw);
2988 ret = tb_switch_tmu_init(sw);
2993 ret = tb_switch_port_hotplug_enable(sw);
2997 ret = device_add(&sw->dev);
2999 dev_err(&sw->dev, "failed to add device: %d\n", ret);
3004 dev_info(&sw->dev, "new device found, vendor=%#x device=%#x\n",
3005 sw->vendor, sw->device);
3006 if (sw->vendor_name && sw->device_name)
3007 dev_info(&sw->dev, "%s %s\n", sw->vendor_name,
3011 ret = usb4_switch_add_ports(sw);
3013 dev_err(&sw->dev, "failed to add USB4 ports\n");
3017 ret = tb_switch_nvm_add(sw);
3019 dev_err(&sw->dev, "failed to add NVM devices\n");
3024 * Thunderbolt routers do not generate wakeups themselves but
3025 * they forward wakeups from tunneled protocols, so enable it
3028 device_init_wakeup(&sw->dev, true);
3030 pm_runtime_set_active(&sw->dev);
3032 pm_runtime_set_autosuspend_delay(&sw->dev, TB_AUTOSUSPEND_DELAY);
3033 pm_runtime_use_autosuspend(&sw->dev);
3034 pm_runtime_mark_last_busy(&sw->dev);
3035 pm_runtime_enable(&sw->dev);
3036 pm_request_autosuspend(&sw->dev);
3039 tb_switch_debugfs_init(sw);
3043 usb4_switch_remove_ports(sw);
3045 device_del(&sw->dev);
3051 * tb_switch_remove() - Remove and release a switch
3052 * @sw: Switch to remove
3054 * This will remove the switch from the domain and release it after last
3055 * reference count drops to zero. If there are switches connected below
3056 * this switch, they will be removed as well.
3058 void tb_switch_remove(struct tb_switch *sw)
3060 struct tb_port *port;
3062 tb_switch_debugfs_remove(sw);
3065 pm_runtime_get_sync(&sw->dev);
3066 pm_runtime_disable(&sw->dev);
3069 /* port 0 is the switch itself and never has a remote */
3070 tb_switch_for_each_port(sw, port) {
3071 if (tb_port_has_remote(port)) {
3072 tb_switch_remove(port->remote->sw);
3073 port->remote = NULL;
3074 } else if (port->xdomain) {
3075 tb_xdomain_remove(port->xdomain);
3076 port->xdomain = NULL;
3079 /* Remove any downstream retimers */
3080 tb_retimer_remove_all(port);
3083 if (!sw->is_unplugged)
3084 tb_plug_events_active(sw, false);
3086 tb_switch_nvm_remove(sw);
3087 usb4_switch_remove_ports(sw);
3090 dev_info(&sw->dev, "device disconnected\n");
3091 device_unregister(&sw->dev);
3095 * tb_sw_set_unplugged() - set is_unplugged on switch and downstream switches
3096 * @sw: Router to mark unplugged
3098 void tb_sw_set_unplugged(struct tb_switch *sw)
3100 struct tb_port *port;
3102 if (sw == sw->tb->root_switch) {
3103 tb_sw_WARN(sw, "cannot unplug root switch\n");
3106 if (sw->is_unplugged) {
3107 tb_sw_WARN(sw, "is_unplugged already set\n");
3110 sw->is_unplugged = true;
3111 tb_switch_for_each_port(sw, port) {
3112 if (tb_port_has_remote(port))
3113 tb_sw_set_unplugged(port->remote->sw);
3114 else if (port->xdomain)
3115 port->xdomain->is_unplugged = true;
3119 static int tb_switch_set_wake(struct tb_switch *sw, unsigned int flags)
3122 tb_sw_dbg(sw, "enabling wakeup: %#x\n", flags);
3124 tb_sw_dbg(sw, "disabling wakeup\n");
3126 if (tb_switch_is_usb4(sw))
3127 return usb4_switch_set_wake(sw, flags);
3128 return tb_lc_set_wake(sw, flags);
3131 int tb_switch_resume(struct tb_switch *sw)
3133 struct tb_port *port;
3136 tb_sw_dbg(sw, "resuming switch\n");
3139 * Check for UID of the connected switches except for root
3140 * switch which we assume cannot be removed.
3146 * Check first that we can still read the switch config
3147 * space. It may be that there is now another domain
3150 err = tb_cfg_get_upstream_port(sw->tb->ctl, tb_route(sw));
3152 tb_sw_info(sw, "switch not present anymore\n");
3156 /* We don't have any way to confirm this was the same device */
3160 if (tb_switch_is_usb4(sw))
3161 err = usb4_switch_read_uid(sw, &uid);
3163 err = tb_drom_read_uid_only(sw, &uid);
3165 tb_sw_warn(sw, "uid read failed\n");
3168 if (sw->uid != uid) {
3170 "changed while suspended (uid %#llx -> %#llx)\n",
3176 err = tb_switch_configure(sw);
3181 tb_switch_set_wake(sw, 0);
3183 err = tb_switch_tmu_init(sw);
3187 /* check for surviving downstream switches */
3188 tb_switch_for_each_port(sw, port) {
3189 if (!tb_port_is_null(port))
3192 if (!tb_port_resume(port))
3195 if (tb_wait_for_port(port, true) <= 0) {
3197 "lost during suspend, disconnecting\n");
3198 if (tb_port_has_remote(port))
3199 tb_sw_set_unplugged(port->remote->sw);
3200 else if (port->xdomain)
3201 port->xdomain->is_unplugged = true;
3204 * Always unlock the port so the downstream
3205 * switch/domain is accessible.
3207 if (tb_port_unlock(port))
3208 tb_port_warn(port, "failed to unlock port\n");
3209 if (port->remote && tb_switch_resume(port->remote->sw)) {
3211 "lost during suspend, disconnecting\n");
3212 tb_sw_set_unplugged(port->remote->sw);
3220 * tb_switch_suspend() - Put a switch to sleep
3221 * @sw: Switch to suspend
3222 * @runtime: Is this runtime suspend or system sleep
3224 * Suspends router and all its children. Enables wakes according to
3225 * value of @runtime and then sets sleep bit for the router. If @sw is
3226 * host router the domain is ready to go to sleep once this function
3229 void tb_switch_suspend(struct tb_switch *sw, bool runtime)
3231 unsigned int flags = 0;
3232 struct tb_port *port;
3235 tb_sw_dbg(sw, "suspending switch\n");
3238 * Actually only needed for Titan Ridge but for simplicity can be
3239 * done for USB4 device too as CLx is re-enabled at resume.
3240 * CL0s and CL1 are enabled and supported together.
3242 if (tb_switch_is_clx_enabled(sw, TB_CL1)) {
3243 if (tb_switch_disable_clx(sw, TB_CL1))
3244 tb_sw_warn(sw, "failed to disable %s on upstream port\n",
3245 tb_switch_clx_name(TB_CL1));
3248 err = tb_plug_events_active(sw, false);
3252 tb_switch_for_each_port(sw, port) {
3253 if (tb_port_has_remote(port))
3254 tb_switch_suspend(port->remote->sw, runtime);
3258 /* Trigger wake when something is plugged in/out */
3259 flags |= TB_WAKE_ON_CONNECT | TB_WAKE_ON_DISCONNECT;
3260 flags |= TB_WAKE_ON_USB4;
3261 flags |= TB_WAKE_ON_USB3 | TB_WAKE_ON_PCIE | TB_WAKE_ON_DP;
3262 } else if (device_may_wakeup(&sw->dev)) {
3263 flags |= TB_WAKE_ON_USB4 | TB_WAKE_ON_USB3 | TB_WAKE_ON_PCIE;
3266 tb_switch_set_wake(sw, flags);
3268 if (tb_switch_is_usb4(sw))
3269 usb4_switch_set_sleep(sw);
3271 tb_lc_set_sleep(sw);
3275 * tb_switch_query_dp_resource() - Query availability of DP resource
3276 * @sw: Switch whose DP resource is queried
3279 * Queries availability of DP resource for DP tunneling using switch
3280 * specific means. Returns %true if resource is available.
3282 bool tb_switch_query_dp_resource(struct tb_switch *sw, struct tb_port *in)
3284 if (tb_switch_is_usb4(sw))
3285 return usb4_switch_query_dp_resource(sw, in);
3286 return tb_lc_dp_sink_query(sw, in);
3290 * tb_switch_alloc_dp_resource() - Allocate available DP resource
3291 * @sw: Switch whose DP resource is allocated
3294 * Allocates DP resource for DP tunneling. The resource must be
3295 * available for this to succeed (see tb_switch_query_dp_resource()).
3296 * Returns %0 in success and negative errno otherwise.
3298 int tb_switch_alloc_dp_resource(struct tb_switch *sw, struct tb_port *in)
3302 if (tb_switch_is_usb4(sw))
3303 ret = usb4_switch_alloc_dp_resource(sw, in);
3305 ret = tb_lc_dp_sink_alloc(sw, in);
3308 tb_sw_warn(sw, "failed to allocate DP resource for port %d\n",
3311 tb_sw_dbg(sw, "allocated DP resource for port %d\n", in->port);
3317 * tb_switch_dealloc_dp_resource() - De-allocate DP resource
3318 * @sw: Switch whose DP resource is de-allocated
3321 * De-allocates DP resource that was previously allocated for DP
3324 void tb_switch_dealloc_dp_resource(struct tb_switch *sw, struct tb_port *in)
3328 if (tb_switch_is_usb4(sw))
3329 ret = usb4_switch_dealloc_dp_resource(sw, in);
3331 ret = tb_lc_dp_sink_dealloc(sw, in);
3334 tb_sw_warn(sw, "failed to de-allocate DP resource for port %d\n",
3337 tb_sw_dbg(sw, "released DP resource for port %d\n", in->port);
3340 struct tb_sw_lookup {
3348 static int tb_switch_match(struct device *dev, const void *data)
3350 struct tb_switch *sw = tb_to_switch(dev);
3351 const struct tb_sw_lookup *lookup = data;
3355 if (sw->tb != lookup->tb)
3359 return !memcmp(sw->uuid, lookup->uuid, sizeof(*lookup->uuid));
3361 if (lookup->route) {
3362 return sw->config.route_lo == lower_32_bits(lookup->route) &&
3363 sw->config.route_hi == upper_32_bits(lookup->route);
3366 /* Root switch is matched only by depth */
3370 return sw->link == lookup->link && sw->depth == lookup->depth;
3374 * tb_switch_find_by_link_depth() - Find switch by link and depth
3375 * @tb: Domain the switch belongs
3376 * @link: Link number the switch is connected
3377 * @depth: Depth of the switch in link
3379 * Returned switch has reference count increased so the caller needs to
3380 * call tb_switch_put() when done with the switch.
3382 struct tb_switch *tb_switch_find_by_link_depth(struct tb *tb, u8 link, u8 depth)
3384 struct tb_sw_lookup lookup;
3387 memset(&lookup, 0, sizeof(lookup));
3390 lookup.depth = depth;
3392 dev = bus_find_device(&tb_bus_type, NULL, &lookup, tb_switch_match);
3394 return tb_to_switch(dev);
3400 * tb_switch_find_by_uuid() - Find switch by UUID
3401 * @tb: Domain the switch belongs
3402 * @uuid: UUID to look for
3404 * Returned switch has reference count increased so the caller needs to
3405 * call tb_switch_put() when done with the switch.
3407 struct tb_switch *tb_switch_find_by_uuid(struct tb *tb, const uuid_t *uuid)
3409 struct tb_sw_lookup lookup;
3412 memset(&lookup, 0, sizeof(lookup));
3416 dev = bus_find_device(&tb_bus_type, NULL, &lookup, tb_switch_match);
3418 return tb_to_switch(dev);
3424 * tb_switch_find_by_route() - Find switch by route string
3425 * @tb: Domain the switch belongs
3426 * @route: Route string to look for
3428 * Returned switch has reference count increased so the caller needs to
3429 * call tb_switch_put() when done with the switch.
3431 struct tb_switch *tb_switch_find_by_route(struct tb *tb, u64 route)
3433 struct tb_sw_lookup lookup;
3437 return tb_switch_get(tb->root_switch);
3439 memset(&lookup, 0, sizeof(lookup));
3441 lookup.route = route;
3443 dev = bus_find_device(&tb_bus_type, NULL, &lookup, tb_switch_match);
3445 return tb_to_switch(dev);
3451 * tb_switch_find_port() - return the first port of @type on @sw or NULL
3452 * @sw: Switch to find the port from
3453 * @type: Port type to look for
3455 struct tb_port *tb_switch_find_port(struct tb_switch *sw,
3456 enum tb_port_type type)
3458 struct tb_port *port;
3460 tb_switch_for_each_port(sw, port) {
3461 if (port->config.type == type)
3468 static int tb_switch_pm_secondary_resolve(struct tb_switch *sw)
3470 struct tb_switch *parent = tb_switch_parent(sw);
3471 struct tb_port *up, *down;
3477 up = tb_upstream_port(sw);
3478 down = tb_port_at(tb_route(sw), parent);
3479 ret = tb_port_pm_secondary_enable(up);
3483 return tb_port_pm_secondary_disable(down);
3486 static int __tb_switch_enable_clx(struct tb_switch *sw, enum tb_clx clx)
3488 struct tb_switch *parent = tb_switch_parent(sw);
3489 bool up_clx_support, down_clx_support;
3490 struct tb_port *up, *down;
3493 if (!tb_switch_is_clx_supported(sw))
3497 * Enable CLx for host router's downstream port as part of the
3498 * downstream router enabling procedure.
3503 /* Enable CLx only for first hop router (depth = 1) */
3504 if (tb_route(parent))
3507 ret = tb_switch_pm_secondary_resolve(sw);
3511 up = tb_upstream_port(sw);
3512 down = tb_port_at(tb_route(sw), parent);
3514 up_clx_support = tb_port_clx_supported(up, clx);
3515 down_clx_support = tb_port_clx_supported(down, clx);
3517 tb_port_dbg(up, "%s %ssupported\n", tb_switch_clx_name(clx),
3518 up_clx_support ? "" : "not ");
3519 tb_port_dbg(down, "%s %ssupported\n", tb_switch_clx_name(clx),
3520 down_clx_support ? "" : "not ");
3522 if (!up_clx_support || !down_clx_support)
3525 ret = tb_port_clx_enable(up, clx);
3529 ret = tb_port_clx_enable(down, clx);
3531 tb_port_clx_disable(up, clx);
3535 ret = tb_switch_mask_clx_objections(sw);
3537 tb_port_clx_disable(up, clx);
3538 tb_port_clx_disable(down, clx);
3544 tb_port_dbg(up, "%s enabled\n", tb_switch_clx_name(clx));
3549 * tb_switch_enable_clx() - Enable CLx on upstream port of specified router
3550 * @sw: Router to enable CLx for
3551 * @clx: The CLx state to enable
3553 * Enable CLx state only for first hop router. That is the most common
3554 * use-case, that is intended for better thermal management, and so helps
3555 * to improve performance. CLx is enabled only if both sides of the link
3556 * support CLx, and if both sides of the link are not configured as two
3557 * single lane links and only if the link is not inter-domain link. The
3558 * complete set of conditions is described in CM Guide 1.0 section 8.1.
3560 * Return: Returns 0 on success or an error code on failure.
3562 int tb_switch_enable_clx(struct tb_switch *sw, enum tb_clx clx)
3564 struct tb_switch *root_sw = sw->tb->root_switch;
3570 * CLx is not enabled and validated on Intel USB4 platforms before
3573 if (root_sw->generation < 4 || tb_switch_is_tiger_lake(root_sw))
3578 /* CL0s and CL1 are enabled and supported together */
3579 return __tb_switch_enable_clx(sw, clx);
3586 static int __tb_switch_disable_clx(struct tb_switch *sw, enum tb_clx clx)
3588 struct tb_switch *parent = tb_switch_parent(sw);
3589 struct tb_port *up, *down;
3592 if (!tb_switch_is_clx_supported(sw))
3596 * Disable CLx for host router's downstream port as part of the
3597 * downstream router enabling procedure.
3602 /* Disable CLx only for first hop router (depth = 1) */
3603 if (tb_route(parent))
3606 up = tb_upstream_port(sw);
3607 down = tb_port_at(tb_route(sw), parent);
3608 ret = tb_port_clx_disable(up, clx);
3612 ret = tb_port_clx_disable(down, clx);
3616 sw->clx = TB_CLX_DISABLE;
3618 tb_port_dbg(up, "%s disabled\n", tb_switch_clx_name(clx));
3623 * tb_switch_disable_clx() - Disable CLx on upstream port of specified router
3624 * @sw: Router to disable CLx for
3625 * @clx: The CLx state to disable
3627 * Return: Returns 0 on success or an error code on failure.
3629 int tb_switch_disable_clx(struct tb_switch *sw, enum tb_clx clx)
3636 /* CL0s and CL1 are enabled and supported together */
3637 return __tb_switch_disable_clx(sw, clx);
3645 * tb_switch_mask_clx_objections() - Mask CLx objections for a router
3646 * @sw: Router to mask objections for
3648 * Mask the objections coming from the second depth routers in order to
3649 * stop these objections from interfering with the CLx states of the first
3652 int tb_switch_mask_clx_objections(struct tb_switch *sw)
3654 int up_port = sw->config.upstream_port_number;
3655 u32 offset, val[2], mask_obj, unmask_obj;
3658 /* Only Titan Ridge of pre-USB4 devices support CLx states */
3659 if (!tb_switch_is_titan_ridge(sw))
3666 * In Titan Ridge there are only 2 dual-lane Thunderbolt ports:
3667 * Port A consists of lane adapters 1,2 and
3668 * Port B consists of lane adapters 3,4
3669 * If upstream port is A, (lanes are 1,2), we mask objections from
3670 * port B (lanes 3,4) and unmask objections from Port A and vice-versa.
3673 mask_obj = TB_LOW_PWR_C0_PORT_B_MASK;
3674 unmask_obj = TB_LOW_PWR_C1_PORT_A_MASK;
3675 offset = TB_LOW_PWR_C1_CL1;
3677 mask_obj = TB_LOW_PWR_C1_PORT_A_MASK;
3678 unmask_obj = TB_LOW_PWR_C0_PORT_B_MASK;
3679 offset = TB_LOW_PWR_C3_CL1;
3682 ret = tb_sw_read(sw, &val, TB_CFG_SWITCH,
3683 sw->cap_lp + offset, ARRAY_SIZE(val));
3687 for (i = 0; i < ARRAY_SIZE(val); i++) {
3689 val[i] &= ~unmask_obj;
3692 return tb_sw_write(sw, &val, TB_CFG_SWITCH,
3693 sw->cap_lp + offset, ARRAY_SIZE(val));
3697 * Can be used for read/write a specified PCIe bridge for any Thunderbolt 3
3698 * device. For now used only for Titan Ridge.
3700 static int tb_switch_pcie_bridge_write(struct tb_switch *sw, unsigned int bridge,
3701 unsigned int pcie_offset, u32 value)
3703 u32 offset, command, val;
3706 if (sw->generation != 3)
3709 offset = sw->cap_plug_events + TB_PLUG_EVENTS_PCIE_WR_DATA;
3710 ret = tb_sw_write(sw, &value, TB_CFG_SWITCH, offset, 1);
3714 command = pcie_offset & TB_PLUG_EVENTS_PCIE_CMD_DW_OFFSET_MASK;
3715 command |= BIT(bridge + TB_PLUG_EVENTS_PCIE_CMD_BR_SHIFT);
3716 command |= TB_PLUG_EVENTS_PCIE_CMD_RD_WR_MASK;
3717 command |= TB_PLUG_EVENTS_PCIE_CMD_COMMAND_VAL
3718 << TB_PLUG_EVENTS_PCIE_CMD_COMMAND_SHIFT;
3719 command |= TB_PLUG_EVENTS_PCIE_CMD_REQ_ACK_MASK;
3721 offset = sw->cap_plug_events + TB_PLUG_EVENTS_PCIE_CMD;
3723 ret = tb_sw_write(sw, &command, TB_CFG_SWITCH, offset, 1);
3727 ret = tb_switch_wait_for_bit(sw, offset,
3728 TB_PLUG_EVENTS_PCIE_CMD_REQ_ACK_MASK, 0, 100);
3732 ret = tb_sw_read(sw, &val, TB_CFG_SWITCH, offset, 1);
3736 if (val & TB_PLUG_EVENTS_PCIE_CMD_TIMEOUT_MASK)
3743 * tb_switch_pcie_l1_enable() - Enable PCIe link to enter L1 state
3744 * @sw: Router to enable PCIe L1
3746 * For Titan Ridge switch to enter CLx state, its PCIe bridges shall enable
3747 * entry to PCIe L1 state. Shall be called after the upstream PCIe tunnel
3748 * was configured. Due to Intel platforms limitation, shall be called only
3749 * for first hop switch.
3751 int tb_switch_pcie_l1_enable(struct tb_switch *sw)
3753 struct tb_switch *parent = tb_switch_parent(sw);
3759 if (!tb_switch_is_titan_ridge(sw))
3762 /* Enable PCIe L1 enable only for first hop router (depth = 1) */
3763 if (tb_route(parent))
3766 /* Write to downstream PCIe bridge #5 aka Dn4 */
3767 ret = tb_switch_pcie_bridge_write(sw, 5, 0x143, 0x0c7806b1);
3771 /* Write to Upstream PCIe bridge #0 aka Up0 */
3772 return tb_switch_pcie_bridge_write(sw, 0, 0x143, 0x0c5806b1);
3776 * tb_switch_xhci_connect() - Connect internal xHCI
3777 * @sw: Router whose xHCI to connect
3779 * Can be called to any router. For Alpine Ridge and Titan Ridge
3780 * performs special flows that bring the xHCI functional for any device
3781 * connected to the type-C port. Call only after PCIe tunnel has been
3782 * established. The function only does the connect if not done already
3783 * so can be called several times for the same router.
3785 int tb_switch_xhci_connect(struct tb_switch *sw)
3787 struct tb_port *port1, *port3;
3790 if (sw->generation != 3)
3793 port1 = &sw->ports[1];
3794 port3 = &sw->ports[3];
3796 if (tb_switch_is_alpine_ridge(sw)) {
3797 bool usb_port1, usb_port3, xhci_port1, xhci_port3;
3799 usb_port1 = tb_lc_is_usb_plugged(port1);
3800 usb_port3 = tb_lc_is_usb_plugged(port3);
3801 xhci_port1 = tb_lc_is_xhci_connected(port1);
3802 xhci_port3 = tb_lc_is_xhci_connected(port3);
3804 /* Figure out correct USB port to connect */
3805 if (usb_port1 && !xhci_port1) {
3806 ret = tb_lc_xhci_connect(port1);
3810 if (usb_port3 && !xhci_port3)
3811 return tb_lc_xhci_connect(port3);
3812 } else if (tb_switch_is_titan_ridge(sw)) {
3813 ret = tb_lc_xhci_connect(port1);
3816 return tb_lc_xhci_connect(port3);
3823 * tb_switch_xhci_disconnect() - Disconnect internal xHCI
3824 * @sw: Router whose xHCI to disconnect
3826 * The opposite of tb_switch_xhci_connect(). Disconnects xHCI on both
3829 void tb_switch_xhci_disconnect(struct tb_switch *sw)
3831 if (sw->generation == 3) {
3832 struct tb_port *port1 = &sw->ports[1];
3833 struct tb_port *port3 = &sw->ports[3];
3835 tb_lc_xhci_disconnect(port1);
3836 tb_port_dbg(port1, "disconnected xHCI\n");
3837 tb_lc_xhci_disconnect(port3);
3838 tb_port_dbg(port3, "disconnected xHCI\n");