Lines Matching +full:sw +full:- +full:reset +full:- +full:number
1 // SPDX-License-Identifier: GPL-2.0
3 * Thunderbolt driver - switch/port utility functions
12 #include <linux/nvmem-provider.h>
37 static struct nvm_auth_status *__nvm_get_auth_status(const struct tb_switch *sw) in __nvm_get_auth_status() argument
42 if (uuid_equal(&st->uuid, sw->uuid)) in __nvm_get_auth_status()
49 static void nvm_get_auth_status(const struct tb_switch *sw, u32 *status) in nvm_get_auth_status() argument
54 st = __nvm_get_auth_status(sw); in nvm_get_auth_status()
57 *status = st ? st->status : 0; in nvm_get_auth_status()
60 static void nvm_set_auth_status(const struct tb_switch *sw, u32 status) in nvm_set_auth_status() argument
64 if (WARN_ON(!sw->uuid)) in nvm_set_auth_status()
68 st = __nvm_get_auth_status(sw); in nvm_set_auth_status()
75 memcpy(&st->uuid, sw->uuid, sizeof(st->uuid)); in nvm_set_auth_status()
76 INIT_LIST_HEAD(&st->list); in nvm_set_auth_status()
77 list_add_tail(&st->list, &nvm_auth_status_cache); in nvm_set_auth_status()
80 st->status = status; in nvm_set_auth_status()
85 static void nvm_clear_auth_status(const struct tb_switch *sw) in nvm_clear_auth_status() argument
90 st = __nvm_get_auth_status(sw); in nvm_clear_auth_status()
92 list_del(&st->list); in nvm_clear_auth_status()
98 static int nvm_validate_and_write(struct tb_switch *sw) in nvm_validate_and_write() argument
104 ret = tb_nvm_validate(sw->nvm); in nvm_validate_and_write()
108 ret = tb_nvm_write_headers(sw->nvm); in nvm_validate_and_write()
112 buf = sw->nvm->buf_data_start; in nvm_validate_and_write()
113 image_size = sw->nvm->buf_data_size; in nvm_validate_and_write()
115 if (tb_switch_is_usb4(sw)) in nvm_validate_and_write()
116 ret = usb4_switch_nvm_write(sw, 0, buf, image_size); in nvm_validate_and_write()
118 ret = dma_port_flash_write(sw->dma_port, 0, buf, image_size); in nvm_validate_and_write()
122 sw->nvm->flushed = true; in nvm_validate_and_write()
126 static int nvm_authenticate_host_dma_port(struct tb_switch *sw) in nvm_authenticate_host_dma_port() argument
135 if (!sw->safe_mode) { in nvm_authenticate_host_dma_port()
138 ret = tb_domain_disconnect_all_paths(sw->tb); in nvm_authenticate_host_dma_port()
145 ret = dma_port_flash_update_auth(sw->dma_port); in nvm_authenticate_host_dma_port()
146 if (!ret || ret == -ETIMEDOUT) in nvm_authenticate_host_dma_port()
153 tb_sw_warn(sw, "failed to authenticate NVM, power cycling\n"); in nvm_authenticate_host_dma_port()
154 if (dma_port_flash_update_auth_status(sw->dma_port, &status) > 0) in nvm_authenticate_host_dma_port()
155 nvm_set_auth_status(sw, status); in nvm_authenticate_host_dma_port()
162 dma_port_power_cycle(sw->dma_port); in nvm_authenticate_host_dma_port()
166 static int nvm_authenticate_device_dma_port(struct tb_switch *sw) in nvm_authenticate_device_dma_port() argument
170 ret = dma_port_flash_update_auth(sw->dma_port); in nvm_authenticate_device_dma_port()
173 case -ETIMEDOUT: in nvm_authenticate_device_dma_port()
174 case -EACCES: in nvm_authenticate_device_dma_port()
175 case -EINVAL: in nvm_authenticate_device_dma_port()
191 ret = dma_port_flash_update_auth_status(sw->dma_port, &status); in nvm_authenticate_device_dma_port()
192 if (ret < 0 && ret != -ETIMEDOUT) in nvm_authenticate_device_dma_port()
196 tb_sw_warn(sw, "failed to authenticate NVM\n"); in nvm_authenticate_device_dma_port()
197 nvm_set_auth_status(sw, status); in nvm_authenticate_device_dma_port()
200 tb_sw_info(sw, "power cycling the switch now\n"); in nvm_authenticate_device_dma_port()
201 dma_port_power_cycle(sw->dma_port); in nvm_authenticate_device_dma_port()
206 } while (--retries); in nvm_authenticate_device_dma_port()
208 return -ETIMEDOUT; in nvm_authenticate_device_dma_port()
211 static void nvm_authenticate_start_dma_port(struct tb_switch *sw) in nvm_authenticate_start_dma_port() argument
221 root_port = pcie_find_root_port(sw->tb->nhi->pdev); in nvm_authenticate_start_dma_port()
223 pm_runtime_get_noresume(&root_port->dev); in nvm_authenticate_start_dma_port()
226 static void nvm_authenticate_complete_dma_port(struct tb_switch *sw) in nvm_authenticate_complete_dma_port() argument
230 root_port = pcie_find_root_port(sw->tb->nhi->pdev); in nvm_authenticate_complete_dma_port()
232 pm_runtime_put(&root_port->dev); in nvm_authenticate_complete_dma_port()
235 static inline bool nvm_readable(struct tb_switch *sw) in nvm_readable() argument
237 if (tb_switch_is_usb4(sw)) { in nvm_readable()
244 return usb4_switch_nvm_sector_size(sw) > 0; in nvm_readable()
248 return !!sw->dma_port; in nvm_readable()
251 static inline bool nvm_upgradeable(struct tb_switch *sw) in nvm_upgradeable() argument
253 if (sw->no_nvm_upgrade) in nvm_upgradeable()
255 return nvm_readable(sw); in nvm_upgradeable()
258 static int nvm_authenticate(struct tb_switch *sw, bool auth_only) in nvm_authenticate() argument
262 if (tb_switch_is_usb4(sw)) { in nvm_authenticate()
264 ret = usb4_switch_nvm_set_offset(sw, 0); in nvm_authenticate()
268 sw->nvm->authenticating = true; in nvm_authenticate()
269 return usb4_switch_nvm_authenticate(sw); in nvm_authenticate()
272 return -EOPNOTSUPP; in nvm_authenticate()
274 sw->nvm->authenticating = true; in nvm_authenticate()
275 if (!tb_route(sw)) { in nvm_authenticate()
276 nvm_authenticate_start_dma_port(sw); in nvm_authenticate()
277 ret = nvm_authenticate_host_dma_port(sw); in nvm_authenticate()
279 ret = nvm_authenticate_device_dma_port(sw); in nvm_authenticate()
286 * tb_switch_nvm_read() - Read router NVM
287 * @sw: Router whose NVM to read
296 int tb_switch_nvm_read(struct tb_switch *sw, unsigned int address, void *buf, in tb_switch_nvm_read() argument
299 if (tb_switch_is_usb4(sw)) in tb_switch_nvm_read()
300 return usb4_switch_nvm_read(sw, address, buf, size); in tb_switch_nvm_read()
301 return dma_port_flash_read(sw->dma_port, address, buf, size); in tb_switch_nvm_read()
307 struct tb_switch *sw = tb_to_switch(nvm->dev); in nvm_read() local
310 pm_runtime_get_sync(&sw->dev); in nvm_read()
312 if (!mutex_trylock(&sw->tb->lock)) { in nvm_read()
317 ret = tb_switch_nvm_read(sw, offset, val, bytes); in nvm_read()
318 mutex_unlock(&sw->tb->lock); in nvm_read()
321 pm_runtime_mark_last_busy(&sw->dev); in nvm_read()
322 pm_runtime_put_autosuspend(&sw->dev); in nvm_read()
330 struct tb_switch *sw = tb_to_switch(nvm->dev); in nvm_write() local
333 if (!mutex_trylock(&sw->tb->lock)) in nvm_write()
343 mutex_unlock(&sw->tb->lock); in nvm_write()
348 static int tb_switch_nvm_add(struct tb_switch *sw) in tb_switch_nvm_add() argument
353 if (!nvm_readable(sw)) in tb_switch_nvm_add()
356 nvm = tb_nvm_alloc(&sw->dev); in tb_switch_nvm_add()
358 ret = PTR_ERR(nvm) == -EOPNOTSUPP ? 0 : PTR_ERR(nvm); in tb_switch_nvm_add()
367 * If the switch is in safe-mode the only accessible portion of in tb_switch_nvm_add()
368 * the NVM is the non-active one where userspace is expected to in tb_switch_nvm_add()
371 if (!sw->safe_mode) { in tb_switch_nvm_add()
375 tb_sw_dbg(sw, "NVM version %x.%x\n", nvm->major, nvm->minor); in tb_switch_nvm_add()
378 if (!sw->no_nvm_upgrade) { in tb_switch_nvm_add()
384 sw->nvm = nvm; in tb_switch_nvm_add()
388 tb_sw_dbg(sw, "NVM upgrade disabled\n"); in tb_switch_nvm_add()
389 sw->no_nvm_upgrade = true; in tb_switch_nvm_add()
396 static void tb_switch_nvm_remove(struct tb_switch *sw) in tb_switch_nvm_remove() argument
400 nvm = sw->nvm; in tb_switch_nvm_remove()
401 sw->nvm = NULL; in tb_switch_nvm_remove()
407 if (!nvm->authenticating) in tb_switch_nvm_remove()
408 nvm_clear_auth_status(sw); in tb_switch_nvm_remove()
417 switch (port->type >> 16) { in tb_port_type()
419 switch ((u8) port->type) { in tb_port_type()
446 const struct tb_regs_port_header *regs = &port->config; in tb_dump_port()
450 regs->port_number, regs->vendor_id, regs->device_id, in tb_dump_port()
451 regs->revision, regs->thunderbolt_version, tb_port_type(regs), in tb_dump_port()
452 regs->type); in tb_dump_port()
454 regs->max_in_hop_id, regs->max_out_hop_id); in tb_dump_port()
455 tb_dbg(tb, " Max counters: %d\n", regs->max_counters); in tb_dump_port()
456 tb_dbg(tb, " NFC Credits: %#x\n", regs->nfc_credits); in tb_dump_port()
457 tb_dbg(tb, " Credits (total/control): %u/%u\n", port->total_credits, in tb_dump_port()
458 port->ctl_credits); in tb_dump_port()
462 * tb_port_state() - get connectedness state of a port
473 if (port->cap_phy == 0) { in tb_port_state()
475 return -EINVAL; in tb_port_state()
477 res = tb_port_read(port, &phy, TB_CFG_PORT, port->cap_phy, 2); in tb_port_state()
484 * tb_wait_for_port() - wait for a port to become ready
502 if (!port->cap_phy) { in tb_wait_for_port()
504 return -EINVAL; in tb_wait_for_port()
508 return -EINVAL; in tb_wait_for_port()
511 while (retries--) { in tb_wait_for_port()
542 * After plug-in the state is TB_PORT_CONNECTING. Give it some in tb_wait_for_port()
558 * tb_port_add_nfc_credits() - add/remove non flow controlled credits to port
562 * Change the number of NFC credits allocated to @port by @credits. To remove
571 if (credits == 0 || port->sw->is_unplugged) in tb_port_add_nfc_credits()
578 if (tb_switch_is_usb4(port->sw) && !tb_port_is_null(port)) in tb_port_add_nfc_credits()
581 nfc_credits = port->config.nfc_credits & ADP_CS_4_NFC_BUFFERS_MASK; in tb_port_add_nfc_credits()
583 credits = max_t(int, -nfc_credits, credits); in tb_port_add_nfc_credits()
588 port->config.nfc_credits & ADP_CS_4_NFC_BUFFERS_MASK); in tb_port_add_nfc_credits()
590 port->config.nfc_credits &= ~ADP_CS_4_NFC_BUFFERS_MASK; in tb_port_add_nfc_credits()
591 port->config.nfc_credits |= nfc_credits; in tb_port_add_nfc_credits()
593 return tb_port_write(port, &port->config.nfc_credits, in tb_port_add_nfc_credits()
598 * tb_port_clear_counter() - clear a counter in TB_CFG_COUNTER
612 * tb_port_unlock() - Unlock downstream port
620 if (tb_switch_is_icm(port->sw)) in tb_port_unlock()
623 return -EINVAL; in tb_port_unlock()
624 if (tb_switch_is_usb4(port->sw)) in tb_port_unlock()
635 return -EINVAL; in __tb_port_enable()
638 port->cap_phy + LANE_ADP_CS_1, 1); in __tb_port_enable()
649 port->cap_phy + LANE_ADP_CS_1, 1); in __tb_port_enable()
658 * tb_port_enable() - Enable lane adapter
669 * tb_port_disable() - Disable lane adapter
681 if (tb_switch_is_usb4(port->sw)) in tb_port_reset()
682 return port->cap_usb4 ? usb4_port_reset(port) : 0; in tb_port_reset()
687 * tb_init_port() - initialize a port
699 INIT_LIST_HEAD(&port->list); in tb_init_port()
702 if (!port->port) in tb_init_port()
705 res = tb_port_read(port, &port->config, TB_CFG_PORT, 0, 8); in tb_init_port()
707 if (res == -ENODEV) { in tb_init_port()
708 tb_dbg(port->sw->tb, " Port %d: not implemented\n", in tb_init_port()
709 port->port); in tb_init_port()
710 port->disabled = true; in tb_init_port()
717 if (port->config.type == TB_TYPE_PORT) { in tb_init_port()
721 port->cap_phy = cap; in tb_init_port()
727 port->cap_usb4 = cap; in tb_init_port()
732 * devices we use hard-coded value. in tb_init_port()
734 if (port->cap_usb4) { in tb_init_port()
738 port->ctl_credits = hop.initial_credits; in tb_init_port()
740 if (!port->ctl_credits) in tb_init_port()
741 port->ctl_credits = 2; in tb_init_port()
746 port->cap_adap = cap; in tb_init_port()
749 port->total_credits = in tb_init_port()
750 (port->config.nfc_credits & ADP_CS_4_TOTAL_BUFFERS_MASK) >> in tb_init_port()
753 tb_dump_port(port->sw->tb, port); in tb_init_port()
764 port_max_hopid = port->config.max_in_hop_id; in tb_port_alloc_hopid()
765 ida = &port->in_hopids; in tb_port_alloc_hopid()
767 port_max_hopid = port->config.max_out_hop_id; in tb_port_alloc_hopid()
768 ida = &port->out_hopids; in tb_port_alloc_hopid()
772 * NHI can use HopIDs 1-max for other adapters HopIDs 0-7 are in tb_port_alloc_hopid()
785 * tb_port_alloc_in_hopid() - Allocate input HopID from port
799 * tb_port_alloc_out_hopid() - Allocate output HopID from port
813 * tb_port_release_in_hopid() - Release allocated input HopID from port
819 ida_free(&port->in_hopids, hopid); in tb_port_release_in_hopid()
823 * tb_port_release_out_hopid() - Release allocated output HopID from port
829 ida_free(&port->out_hopids, hopid); in tb_port_release_out_hopid()
833 const struct tb_switch *sw) in tb_switch_is_reachable() argument
835 u64 mask = (1ULL << parent->config.depth * 8) - 1; in tb_switch_is_reachable()
836 return (tb_route(parent) & mask) == (tb_route(sw) & mask); in tb_switch_is_reachable()
840 * tb_next_port_on_path() - Return next port for given port on a path
852 * Domain tb->lock must be held when this function is called.
862 if (prev->sw == end->sw) { in tb_next_port_on_path()
868 if (tb_switch_is_reachable(prev->sw, end->sw)) { in tb_next_port_on_path()
869 next = tb_port_at(tb_route(end->sw), prev->sw); in tb_next_port_on_path()
871 if (prev->remote && in tb_next_port_on_path()
872 (next == prev || next->dual_link_port == prev)) in tb_next_port_on_path()
873 next = prev->remote; in tb_next_port_on_path()
876 next = prev->remote; in tb_next_port_on_path()
878 next = tb_upstream_port(prev->sw); in tb_next_port_on_path()
883 if (next->dual_link_port && in tb_next_port_on_path()
884 next->link_nr != prev->link_nr) { in tb_next_port_on_path()
885 next = next->dual_link_port; in tb_next_port_on_path()
894 * tb_port_get_link_speed() - Get current link speed
904 if (!port->cap_phy) in tb_port_get_link_speed()
905 return -EINVAL; in tb_port_get_link_speed()
908 port->cap_phy + LANE_ADP_CS_1, 1); in tb_port_get_link_speed()
926 * tb_port_get_link_generation() - Returns link generation
929 * Returns link generation as number or negative errno in case of
952 * tb_port_get_link_width() - Get current link width
963 if (!port->cap_phy) in tb_port_get_link_width()
964 return -EINVAL; in tb_port_get_link_width()
967 port->cap_phy + LANE_ADP_CS_1, 1); in tb_port_get_link_width()
977 * tb_port_width_supported() - Is the given link width supported
989 if (!port->cap_phy) in tb_port_width_supported()
999 port->cap_phy + LANE_ADP_CS_0, 1); in tb_port_width_supported()
1012 * tb_port_set_link_width() - Set target link width of the lane adapter
1026 if (!port->cap_phy) in tb_port_set_link_width()
1027 return -EINVAL; in tb_port_set_link_width()
1030 port->cap_phy + LANE_ADP_CS_1, 1); in tb_port_set_link_width()
1039 return -EOPNOTSUPP; in tb_port_set_link_width()
1056 return -EINVAL; in tb_port_set_link_width()
1060 port->cap_phy + LANE_ADP_CS_1, 1); in tb_port_set_link_width()
1064 * tb_port_set_lane_bonding() - Enable/disable lane bonding
1080 if (!port->cap_phy) in tb_port_set_lane_bonding()
1081 return -EINVAL; in tb_port_set_lane_bonding()
1084 port->cap_phy + LANE_ADP_CS_1, 1); in tb_port_set_lane_bonding()
1094 port->cap_phy + LANE_ADP_CS_1, 1); in tb_port_set_lane_bonding()
1098 * tb_port_lane_bonding_enable() - Enable bonding on port
1125 width = tb_port_get_link_width(port->dual_link_port); in tb_port_lane_bonding_enable()
1127 ret = tb_port_set_link_width(port->dual_link_port, in tb_port_lane_bonding_enable()
1135 * avoids the lane adapter to re-enter bonding state. in tb_port_lane_bonding_enable()
1147 port->bonded = true; in tb_port_lane_bonding_enable()
1148 port->dual_link_port->bonded = true; in tb_port_lane_bonding_enable()
1153 tb_port_set_link_width(port->dual_link_port, TB_LINK_WIDTH_SINGLE); in tb_port_lane_bonding_enable()
1161 * tb_port_lane_bonding_disable() - Disable bonding on port
1170 tb_port_set_link_width(port->dual_link_port, TB_LINK_WIDTH_SINGLE); in tb_port_lane_bonding_disable()
1172 port->dual_link_port->bonded = false; in tb_port_lane_bonding_disable()
1173 port->bonded = false; in tb_port_lane_bonding_disable()
1177 * tb_port_wait_for_link_width() - Wait until link reaches specific width
1184 * the expected state. Returns %-ETIMEDOUT if the width was not reached
1197 return -EOPNOTSUPP; in tb_port_wait_for_link_width()
1207 if (ret != -EACCES) in tb_port_wait_for_link_width()
1216 return -ETIMEDOUT; in tb_port_wait_for_link_width()
1228 if (nfc_credits != port->config.nfc_credits) { in tb_port_do_update_credits()
1234 tb_port_dbg(port, "total credits changed %u -> %u\n", in tb_port_do_update_credits()
1235 port->total_credits, total); in tb_port_do_update_credits()
1237 port->config.nfc_credits = nfc_credits; in tb_port_do_update_credits()
1238 port->total_credits = total; in tb_port_do_update_credits()
1245 * tb_port_update_credits() - Re-read port total credits
1249 * credits may change, so this function needs to be called to re-read
1260 if (!port->dual_link_port) in tb_port_update_credits()
1262 return tb_port_do_update_credits(port->dual_link_port); in tb_port_update_credits()
1269 if (tb_switch_is_usb4(port->sw)) in tb_port_start_lane_initialization()
1273 return ret == -EINVAL ? 0 : ret; in tb_port_start_lane_initialization()
1284 if (port->usb4) { in tb_port_resume()
1285 usb4_port_device_resume(port->usb4); in tb_port_resume()
1292 * link gets re-established. in tb_port_resume()
1294 * This is only needed for non-USB4 ports. in tb_port_resume()
1296 if (!tb_is_upstream_port(port) || port->xdomain) in tb_port_resume()
1300 return has_remote || port->xdomain; in tb_port_resume()
1304 * tb_port_is_enabled() - Is the adapter port enabled
1309 switch (port->config.type) { in tb_port_is_enabled()
1328 * tb_usb3_port_is_enabled() - Is the USB3 adapter port enabled
1336 port->cap_adap + ADP_USB3_CS_0, 1)) in tb_usb3_port_is_enabled()
1343 * tb_usb3_port_enable() - Enable USB3 adapter port
1352 if (!port->cap_adap) in tb_usb3_port_enable()
1353 return -ENXIO; in tb_usb3_port_enable()
1355 port->cap_adap + ADP_USB3_CS_0, 1); in tb_usb3_port_enable()
1359 * tb_pci_port_is_enabled() - Is the PCIe adapter port enabled
1367 port->cap_adap + ADP_PCIE_CS_0, 1)) in tb_pci_port_is_enabled()
1374 * tb_pci_port_enable() - Enable PCIe adapter port
1381 if (!port->cap_adap) in tb_pci_port_enable()
1382 return -ENXIO; in tb_pci_port_enable()
1384 port->cap_adap + ADP_PCIE_CS_0, 1); in tb_pci_port_enable()
1388 * tb_dp_port_hpd_is_active() - Is HPD already active
1399 port->cap_adap + ADP_DP_CS_2, 1); in tb_dp_port_hpd_is_active()
1407 * tb_dp_port_hpd_clear() - Clear HPD from DP IN port
1418 port->cap_adap + ADP_DP_CS_3, 1); in tb_dp_port_hpd_clear()
1424 port->cap_adap + ADP_DP_CS_3, 1); in tb_dp_port_hpd_clear()
1428 * tb_dp_port_set_hops() - Set video/aux Hop IDs for DP port
1436 * are read-only.
1444 if (tb_switch_is_usb4(port->sw)) in tb_dp_port_set_hops()
1448 port->cap_adap + ADP_DP_CS_0, ARRAY_SIZE(data)); in tb_dp_port_set_hops()
1463 port->cap_adap + ADP_DP_CS_0, ARRAY_SIZE(data)); in tb_dp_port_set_hops()
1467 * tb_dp_port_is_enabled() - Is DP adapter port enabled
1474 if (tb_port_read(port, data, TB_CFG_PORT, port->cap_adap + ADP_DP_CS_0, in tb_dp_port_is_enabled()
1482 * tb_dp_port_enable() - Enables/disables DP paths of a port
1495 port->cap_adap + ADP_DP_CS_0, ARRAY_SIZE(data)); in tb_dp_port_enable()
1505 port->cap_adap + ADP_DP_CS_0, ARRAY_SIZE(data)); in tb_dp_port_enable()
1510 static const char *tb_switch_generation_name(const struct tb_switch *sw) in tb_switch_generation_name() argument
1512 switch (sw->generation) { in tb_switch_generation_name()
1526 static void tb_dump_switch(const struct tb *tb, const struct tb_switch *sw) in tb_dump_switch() argument
1528 const struct tb_regs_switch_header *regs = &sw->config; in tb_dump_switch()
1531 tb_switch_generation_name(sw), regs->vendor_id, regs->device_id, in tb_dump_switch()
1532 regs->revision, regs->thunderbolt_version); in tb_dump_switch()
1533 tb_dbg(tb, " Max Port Number: %d\n", regs->max_port_number); in tb_dump_switch()
1536 " Upstream Port Number: %d Depth: %d Route String: %#llx Enabled: %d, PlugEventsDelay: %dms\n", in tb_dump_switch()
1537 regs->upstream_port_number, regs->depth, in tb_dump_switch()
1538 (((u64) regs->route_hi) << 32) | regs->route_lo, in tb_dump_switch()
1539 regs->enabled, regs->plug_events_delay); in tb_dump_switch()
1541 regs->__unknown1, regs->__unknown4); in tb_dump_switch()
1544 static int tb_switch_reset_host(struct tb_switch *sw) in tb_switch_reset_host() argument
1546 if (sw->generation > 1) { in tb_switch_reset_host()
1549 tb_switch_for_each_port(sw, port) { in tb_switch_reset_host()
1554 * reset and clear up path config spaces. in tb_switch_reset_host()
1579 i <= port->config.max_in_hop_id; i++) { in tb_switch_reset_host()
1588 /* Thunderbolt 1 uses the "reset" config space packet */ in tb_switch_reset_host()
1589 res.err = tb_sw_write(sw, ((u32 *) &sw->config) + 2, in tb_switch_reset_host()
1593 res = tb_cfg_reset(sw->tb->ctl, tb_route(sw)); in tb_switch_reset_host()
1595 return -EIO; in tb_switch_reset_host()
1603 static int tb_switch_reset_device(struct tb_switch *sw) in tb_switch_reset_device() argument
1605 return tb_port_reset(tb_switch_downstream_port(sw)); in tb_switch_reset_device()
1608 static bool tb_switch_enumerated(struct tb_switch *sw) in tb_switch_enumerated() argument
1615 * during system sleep where sw->config.enabled is already set in tb_switch_enumerated()
1618 ret = tb_sw_read(sw, &val, TB_CFG_SWITCH, ROUTER_CS_3, 1); in tb_switch_enumerated()
1626 * tb_switch_reset() - Perform reset to the router
1627 * @sw: Router to reset
1629 * Issues reset to the router @sw. Can be used for any router. For host
1631 * spaces accordingly. For device routers issues downstream port reset
1639 int tb_switch_reset(struct tb_switch *sw) in tb_switch_reset() argument
1646 * equal to being reset so we can skip that here. in tb_switch_reset()
1648 if (!tb_switch_enumerated(sw)) in tb_switch_reset()
1651 tb_sw_dbg(sw, "resetting\n"); in tb_switch_reset()
1653 if (tb_route(sw)) in tb_switch_reset()
1654 ret = tb_switch_reset_device(sw); in tb_switch_reset()
1656 ret = tb_switch_reset_host(sw); in tb_switch_reset()
1659 tb_sw_warn(sw, "failed to reset\n"); in tb_switch_reset()
1665 * tb_switch_wait_for_bit() - Wait for specified value of bits in offset
1666 * @sw: Router to read the offset value from
1673 * Returns %0 in case of success, %-ETIMEDOUT if the @value was not reached
1676 int tb_switch_wait_for_bit(struct tb_switch *sw, u32 offset, u32 bit, in tb_switch_wait_for_bit() argument
1685 ret = tb_sw_read(sw, &val, TB_CFG_SWITCH, offset, 1); in tb_switch_wait_for_bit()
1695 return -ETIMEDOUT; in tb_switch_wait_for_bit()
1699 * tb_plug_events_active() - enable/disable plug events on a switch
1705 static int tb_plug_events_active(struct tb_switch *sw, bool active) in tb_plug_events_active() argument
1710 if (tb_switch_is_icm(sw) || tb_switch_is_usb4(sw)) in tb_plug_events_active()
1713 sw->config.plug_events_delay = 0xff; in tb_plug_events_active()
1714 res = tb_sw_write(sw, ((u32 *) &sw->config) + 4, TB_CFG_SWITCH, 4, 1); in tb_plug_events_active()
1718 res = tb_sw_read(sw, &data, TB_CFG_SWITCH, sw->cap_plug_events + 1, 1); in tb_plug_events_active()
1724 switch (sw->config.device_id) { in tb_plug_events_active()
1735 if (!tb_switch_is_alpine_ridge(sw)) in tb_plug_events_active()
1741 return tb_sw_write(sw, &data, TB_CFG_SWITCH, in tb_plug_events_active()
1742 sw->cap_plug_events + 1, 1); in tb_plug_events_active()
1749 struct tb_switch *sw = tb_to_switch(dev); in authorized_show() local
1751 return sysfs_emit(buf, "%u\n", sw->authorized); in authorized_show()
1757 struct tb_switch *sw; in disapprove_switch() local
1759 sw = tb_to_switch(dev); in disapprove_switch()
1760 if (sw && sw->authorized) { in disapprove_switch()
1764 ret = device_for_each_child_reverse(&sw->dev, NULL, disapprove_switch); in disapprove_switch()
1768 ret = tb_domain_disapprove_switch(sw->tb, sw); in disapprove_switch()
1772 sw->authorized = 0; in disapprove_switch()
1773 kobject_uevent_env(&sw->dev.kobj, KOBJ_CHANGE, envp); in disapprove_switch()
1779 static int tb_switch_set_authorized(struct tb_switch *sw, unsigned int val) in tb_switch_set_authorized() argument
1782 int ret = -EINVAL; in tb_switch_set_authorized()
1785 if (!mutex_trylock(&sw->tb->lock)) in tb_switch_set_authorized()
1788 if (!!sw->authorized == !!val) in tb_switch_set_authorized()
1794 if (tb_route(sw)) { in tb_switch_set_authorized()
1795 ret = disapprove_switch(&sw->dev, NULL); in tb_switch_set_authorized()
1802 if (sw->key) in tb_switch_set_authorized()
1803 ret = tb_domain_approve_switch_key(sw->tb, sw); in tb_switch_set_authorized()
1805 ret = tb_domain_approve_switch(sw->tb, sw); in tb_switch_set_authorized()
1810 if (sw->key) in tb_switch_set_authorized()
1811 ret = tb_domain_challenge_switch_key(sw->tb, sw); in tb_switch_set_authorized()
1819 sw->authorized = val; in tb_switch_set_authorized()
1824 sprintf(envp_string, "AUTHORIZED=%u", sw->authorized); in tb_switch_set_authorized()
1825 kobject_uevent_env(&sw->dev.kobj, KOBJ_CHANGE, envp); in tb_switch_set_authorized()
1829 mutex_unlock(&sw->tb->lock); in tb_switch_set_authorized()
1837 struct tb_switch *sw = tb_to_switch(dev); in authorized_store() local
1845 return -EINVAL; in authorized_store()
1847 pm_runtime_get_sync(&sw->dev); in authorized_store()
1848 ret = tb_switch_set_authorized(sw, val); in authorized_store()
1849 pm_runtime_mark_last_busy(&sw->dev); in authorized_store()
1850 pm_runtime_put_autosuspend(&sw->dev); in authorized_store()
1859 struct tb_switch *sw = tb_to_switch(dev); in boot_show() local
1861 return sysfs_emit(buf, "%u\n", sw->boot); in boot_show()
1868 struct tb_switch *sw = tb_to_switch(dev); in device_show() local
1870 return sysfs_emit(buf, "%#x\n", sw->device); in device_show()
1877 struct tb_switch *sw = tb_to_switch(dev); in device_name_show() local
1879 return sysfs_emit(buf, "%s\n", sw->device_name ?: ""); in device_name_show()
1886 struct tb_switch *sw = tb_to_switch(dev); in generation_show() local
1888 return sysfs_emit(buf, "%u\n", sw->generation); in generation_show()
1895 struct tb_switch *sw = tb_to_switch(dev); in key_show() local
1898 if (!mutex_trylock(&sw->tb->lock)) in key_show()
1901 if (sw->key) in key_show()
1902 ret = sysfs_emit(buf, "%*phN\n", TB_SWITCH_KEY_SIZE, sw->key); in key_show()
1906 mutex_unlock(&sw->tb->lock); in key_show()
1913 struct tb_switch *sw = tb_to_switch(dev); in key_store() local
1921 return -EINVAL; in key_store()
1923 if (!mutex_trylock(&sw->tb->lock)) in key_store()
1926 if (sw->authorized) { in key_store()
1927 ret = -EBUSY; in key_store()
1929 kfree(sw->key); in key_store()
1931 sw->key = NULL; in key_store()
1933 sw->key = kmemdup(key, sizeof(key), GFP_KERNEL); in key_store()
1934 if (!sw->key) in key_store()
1935 ret = -ENOMEM; in key_store()
1939 mutex_unlock(&sw->tb->lock); in key_store()
1947 struct tb_switch *sw = tb_to_switch(dev); in speed_show() local
1949 return sysfs_emit(buf, "%u.0 Gb/s\n", sw->link_speed); in speed_show()
1962 struct tb_switch *sw = tb_to_switch(dev); in rx_lanes_show() local
1965 switch (sw->link_width) { in rx_lanes_show()
1978 return -EINVAL; in rx_lanes_show()
1988 struct tb_switch *sw = tb_to_switch(dev); in tx_lanes_show() local
1991 switch (sw->link_width) { in tx_lanes_show()
2004 return -EINVAL; in tx_lanes_show()
2014 struct tb_switch *sw = tb_to_switch(dev); in nvm_authenticate_show() local
2017 nvm_get_auth_status(sw, &status); in nvm_authenticate_show()
2024 struct tb_switch *sw = tb_to_switch(dev); in nvm_authenticate_sysfs() local
2027 pm_runtime_get_sync(&sw->dev); in nvm_authenticate_sysfs()
2029 if (!mutex_trylock(&sw->tb->lock)) { in nvm_authenticate_sysfs()
2034 if (sw->no_nvm_upgrade) { in nvm_authenticate_sysfs()
2035 ret = -EOPNOTSUPP; in nvm_authenticate_sysfs()
2040 if (!sw->nvm) { in nvm_authenticate_sysfs()
2041 ret = -EAGAIN; in nvm_authenticate_sysfs()
2050 nvm_clear_auth_status(sw); in nvm_authenticate_sysfs()
2055 ret = -EINVAL; in nvm_authenticate_sysfs()
2057 ret = nvm_authenticate(sw, true); in nvm_authenticate_sysfs()
2059 if (!sw->nvm->flushed) { in nvm_authenticate_sysfs()
2060 if (!sw->nvm->buf) { in nvm_authenticate_sysfs()
2061 ret = -EINVAL; in nvm_authenticate_sysfs()
2065 ret = nvm_validate_and_write(sw); in nvm_authenticate_sysfs()
2071 ret = tb_lc_force_power(sw); in nvm_authenticate_sysfs()
2073 ret = nvm_authenticate(sw, false); in nvm_authenticate_sysfs()
2079 mutex_unlock(&sw->tb->lock); in nvm_authenticate_sysfs()
2081 pm_runtime_mark_last_busy(&sw->dev); in nvm_authenticate_sysfs()
2082 pm_runtime_put_autosuspend(&sw->dev); in nvm_authenticate_sysfs()
2116 struct tb_switch *sw = tb_to_switch(dev); in nvm_version_show() local
2119 if (!mutex_trylock(&sw->tb->lock)) in nvm_version_show()
2122 if (sw->safe_mode) in nvm_version_show()
2123 ret = -ENODATA; in nvm_version_show()
2124 else if (!sw->nvm) in nvm_version_show()
2125 ret = -EAGAIN; in nvm_version_show()
2127 ret = sysfs_emit(buf, "%x.%x\n", sw->nvm->major, sw->nvm->minor); in nvm_version_show()
2129 mutex_unlock(&sw->tb->lock); in nvm_version_show()
2138 struct tb_switch *sw = tb_to_switch(dev); in vendor_show() local
2140 return sysfs_emit(buf, "%#x\n", sw->vendor); in vendor_show()
2147 struct tb_switch *sw = tb_to_switch(dev); in vendor_name_show() local
2149 return sysfs_emit(buf, "%s\n", sw->vendor_name ?: ""); in vendor_name_show()
2156 struct tb_switch *sw = tb_to_switch(dev); in unique_id_show() local
2158 return sysfs_emit(buf, "%pUb\n", sw->uuid); in unique_id_show()
2186 struct tb_switch *sw = tb_to_switch(dev); in switch_attr_is_visible() local
2189 if (sw->tb->security_level == TB_SECURITY_NOPCIE || in switch_attr_is_visible()
2190 sw->tb->security_level == TB_SECURITY_DPONLY) in switch_attr_is_visible()
2193 if (!sw->device) in switch_attr_is_visible()
2196 if (!sw->device_name) in switch_attr_is_visible()
2199 if (!sw->vendor) in switch_attr_is_visible()
2202 if (!sw->vendor_name) in switch_attr_is_visible()
2205 if (tb_route(sw) && in switch_attr_is_visible()
2206 sw->tb->security_level == TB_SECURITY_SECURE && in switch_attr_is_visible()
2207 sw->security_level == TB_SECURITY_SECURE) in switch_attr_is_visible()
2208 return attr->mode; in switch_attr_is_visible()
2214 if (tb_route(sw)) in switch_attr_is_visible()
2215 return attr->mode; in switch_attr_is_visible()
2218 if (nvm_upgradeable(sw)) in switch_attr_is_visible()
2219 return attr->mode; in switch_attr_is_visible()
2222 if (nvm_readable(sw)) in switch_attr_is_visible()
2223 return attr->mode; in switch_attr_is_visible()
2226 if (tb_route(sw)) in switch_attr_is_visible()
2227 return attr->mode; in switch_attr_is_visible()
2230 if (sw->quirks & QUIRK_FORCE_POWER_LINK_CONTROLLER) in switch_attr_is_visible()
2231 return attr->mode; in switch_attr_is_visible()
2235 return sw->safe_mode ? 0 : attr->mode; in switch_attr_is_visible()
2250 struct tb_switch *sw = tb_to_switch(dev); in tb_switch_release() local
2253 dma_port_free(sw->dma_port); in tb_switch_release()
2255 tb_switch_for_each_port(sw, port) { in tb_switch_release()
2256 ida_destroy(&port->in_hopids); in tb_switch_release()
2257 ida_destroy(&port->out_hopids); in tb_switch_release()
2260 kfree(sw->uuid); in tb_switch_release()
2261 kfree(sw->device_name); in tb_switch_release()
2262 kfree(sw->vendor_name); in tb_switch_release()
2263 kfree(sw->ports); in tb_switch_release()
2264 kfree(sw->drom); in tb_switch_release()
2265 kfree(sw->key); in tb_switch_release()
2266 kfree(sw); in tb_switch_release()
2271 const struct tb_switch *sw = tb_to_switch(dev); in tb_switch_uevent() local
2274 if (tb_switch_is_usb4(sw)) { in tb_switch_uevent()
2276 usb4_switch_version(sw))) in tb_switch_uevent()
2277 return -ENOMEM; in tb_switch_uevent()
2280 if (!tb_route(sw)) { in tb_switch_uevent()
2287 tb_switch_for_each_port(sw, port) { in tb_switch_uevent()
2288 if (!port->disabled && !tb_is_upstream_port(port) && in tb_switch_uevent()
2299 return -ENOMEM; in tb_switch_uevent()
2309 struct tb_switch *sw = tb_to_switch(dev); in tb_switch_runtime_suspend() local
2310 const struct tb_cm_ops *cm_ops = sw->tb->cm_ops; in tb_switch_runtime_suspend()
2312 if (cm_ops->runtime_suspend_switch) in tb_switch_runtime_suspend()
2313 return cm_ops->runtime_suspend_switch(sw); in tb_switch_runtime_suspend()
2320 struct tb_switch *sw = tb_to_switch(dev); in tb_switch_runtime_resume() local
2321 const struct tb_cm_ops *cm_ops = sw->tb->cm_ops; in tb_switch_runtime_resume()
2323 if (cm_ops->runtime_resume_switch) in tb_switch_runtime_resume()
2324 return cm_ops->runtime_resume_switch(sw); in tb_switch_runtime_resume()
2340 static int tb_switch_get_generation(struct tb_switch *sw) in tb_switch_get_generation() argument
2342 if (tb_switch_is_usb4(sw)) in tb_switch_get_generation()
2345 if (sw->config.vendor_id == PCI_VENDOR_ID_INTEL) { in tb_switch_get_generation()
2346 switch (sw->config.device_id) { in tb_switch_get_generation()
2380 tb_sw_warn(sw, "unsupported switch device id %#x\n", in tb_switch_get_generation()
2381 sw->config.device_id); in tb_switch_get_generation()
2385 static bool tb_switch_exceeds_max_depth(const struct tb_switch *sw, int depth) in tb_switch_exceeds_max_depth() argument
2389 if (tb_switch_is_usb4(sw) || in tb_switch_exceeds_max_depth()
2390 (sw->tb->root_switch && tb_switch_is_usb4(sw->tb->root_switch))) in tb_switch_exceeds_max_depth()
2399 * tb_switch_alloc() - allocate a switch
2415 struct tb_switch *sw; in tb_switch_alloc() local
2430 upstream_port = tb_cfg_get_upstream_port(tb->ctl, route); in tb_switch_alloc()
2434 sw = kzalloc(sizeof(*sw), GFP_KERNEL); in tb_switch_alloc()
2435 if (!sw) in tb_switch_alloc()
2436 return ERR_PTR(-ENOMEM); in tb_switch_alloc()
2438 sw->tb = tb; in tb_switch_alloc()
2439 ret = tb_cfg_read(tb->ctl, &sw->config, route, 0, TB_CFG_SWITCH, 0, 5); in tb_switch_alloc()
2443 sw->generation = tb_switch_get_generation(sw); in tb_switch_alloc()
2446 tb_dump_switch(tb, sw); in tb_switch_alloc()
2449 sw->config.upstream_port_number = upstream_port; in tb_switch_alloc()
2450 sw->config.depth = depth; in tb_switch_alloc()
2451 sw->config.route_hi = upper_32_bits(route); in tb_switch_alloc()
2452 sw->config.route_lo = lower_32_bits(route); in tb_switch_alloc()
2453 sw->config.enabled = 0; in tb_switch_alloc()
2456 if (tb_switch_exceeds_max_depth(sw, depth)) { in tb_switch_alloc()
2457 ret = -EADDRNOTAVAIL; in tb_switch_alloc()
2462 sw->ports = kcalloc(sw->config.max_port_number + 1, sizeof(*sw->ports), in tb_switch_alloc()
2464 if (!sw->ports) { in tb_switch_alloc()
2465 ret = -ENOMEM; in tb_switch_alloc()
2469 for (i = 0; i <= sw->config.max_port_number; i++) { in tb_switch_alloc()
2471 sw->ports[i].sw = sw; in tb_switch_alloc()
2472 sw->ports[i].port = i; in tb_switch_alloc()
2476 ida_init(&sw->ports[i].in_hopids); in tb_switch_alloc()
2477 ida_init(&sw->ports[i].out_hopids); in tb_switch_alloc()
2481 ret = tb_switch_find_vse_cap(sw, TB_VSE_CAP_PLUG_EVENTS); in tb_switch_alloc()
2483 sw->cap_plug_events = ret; in tb_switch_alloc()
2485 ret = tb_switch_find_vse_cap(sw, TB_VSE_CAP_TIME2); in tb_switch_alloc()
2487 sw->cap_vsec_tmu = ret; in tb_switch_alloc()
2489 ret = tb_switch_find_vse_cap(sw, TB_VSE_CAP_LINK_CONTROLLER); in tb_switch_alloc()
2491 sw->cap_lc = ret; in tb_switch_alloc()
2493 ret = tb_switch_find_vse_cap(sw, TB_VSE_CAP_CP_LP); in tb_switch_alloc()
2495 sw->cap_lp = ret; in tb_switch_alloc()
2499 sw->authorized = true; in tb_switch_alloc()
2501 device_initialize(&sw->dev); in tb_switch_alloc()
2502 sw->dev.parent = parent; in tb_switch_alloc()
2503 sw->dev.bus = &tb_bus_type; in tb_switch_alloc()
2504 sw->dev.type = &tb_switch_type; in tb_switch_alloc()
2505 sw->dev.groups = switch_groups; in tb_switch_alloc()
2506 dev_set_name(&sw->dev, "%u-%llx", tb->index, tb_route(sw)); in tb_switch_alloc()
2508 return sw; in tb_switch_alloc()
2511 kfree(sw->ports); in tb_switch_alloc()
2512 kfree(sw); in tb_switch_alloc()
2518 * tb_switch_alloc_safe_mode() - allocate a switch that is in safe mode
2534 struct tb_switch *sw; in tb_switch_alloc_safe_mode() local
2536 sw = kzalloc(sizeof(*sw), GFP_KERNEL); in tb_switch_alloc_safe_mode()
2537 if (!sw) in tb_switch_alloc_safe_mode()
2538 return ERR_PTR(-ENOMEM); in tb_switch_alloc_safe_mode()
2540 sw->tb = tb; in tb_switch_alloc_safe_mode()
2541 sw->config.depth = tb_route_length(route); in tb_switch_alloc_safe_mode()
2542 sw->config.route_hi = upper_32_bits(route); in tb_switch_alloc_safe_mode()
2543 sw->config.route_lo = lower_32_bits(route); in tb_switch_alloc_safe_mode()
2544 sw->safe_mode = true; in tb_switch_alloc_safe_mode()
2546 device_initialize(&sw->dev); in tb_switch_alloc_safe_mode()
2547 sw->dev.parent = parent; in tb_switch_alloc_safe_mode()
2548 sw->dev.bus = &tb_bus_type; in tb_switch_alloc_safe_mode()
2549 sw->dev.type = &tb_switch_type; in tb_switch_alloc_safe_mode()
2550 sw->dev.groups = switch_groups; in tb_switch_alloc_safe_mode()
2551 dev_set_name(&sw->dev, "%u-%llx", tb->index, tb_route(sw)); in tb_switch_alloc_safe_mode()
2553 return sw; in tb_switch_alloc_safe_mode()
2557 * tb_switch_configure() - Uploads configuration to the switch
2558 * @sw: Switch to configure
2563 * resume from low power states to re-initialize it.
2567 int tb_switch_configure(struct tb_switch *sw) in tb_switch_configure() argument
2569 struct tb *tb = sw->tb; in tb_switch_configure()
2573 route = tb_route(sw); in tb_switch_configure()
2576 sw->config.enabled ? "restoring" : "initializing", route, in tb_switch_configure()
2577 tb_route_length(route), sw->config.upstream_port_number); in tb_switch_configure()
2579 sw->config.enabled = 1; in tb_switch_configure()
2581 if (tb_switch_is_usb4(sw)) { in tb_switch_configure()
2588 if (usb4_switch_version(sw) < 2) in tb_switch_configure()
2589 sw->config.cmuv = ROUTER_CS_4_CMUV_V1; in tb_switch_configure()
2591 sw->config.cmuv = ROUTER_CS_4_CMUV_V2; in tb_switch_configure()
2592 sw->config.plug_events_delay = 0xa; in tb_switch_configure()
2595 ret = tb_sw_write(sw, (u32 *)&sw->config + 1, TB_CFG_SWITCH, in tb_switch_configure()
2600 ret = usb4_switch_setup(sw); in tb_switch_configure()
2602 if (sw->config.vendor_id != PCI_VENDOR_ID_INTEL) in tb_switch_configure()
2603 tb_sw_warn(sw, "unknown switch vendor id %#x\n", in tb_switch_configure()
2604 sw->config.vendor_id); in tb_switch_configure()
2606 if (!sw->cap_plug_events) { in tb_switch_configure()
2607 tb_sw_warn(sw, "cannot find TB_VSE_CAP_PLUG_EVENTS aborting\n"); in tb_switch_configure()
2608 return -ENODEV; in tb_switch_configure()
2612 ret = tb_sw_write(sw, (u32 *)&sw->config + 1, TB_CFG_SWITCH, in tb_switch_configure()
2618 return tb_plug_events_active(sw, true); in tb_switch_configure()
2622 * tb_switch_configuration_valid() - Set the tunneling configuration to be valid
2623 * @sw: Router to configure
2630 int tb_switch_configuration_valid(struct tb_switch *sw) in tb_switch_configuration_valid() argument
2632 if (tb_switch_is_usb4(sw)) in tb_switch_configuration_valid()
2633 return usb4_switch_configuration_valid(sw); in tb_switch_configuration_valid()
2637 static int tb_switch_set_uuid(struct tb_switch *sw) in tb_switch_set_uuid() argument
2643 if (sw->uuid) in tb_switch_set_uuid()
2646 if (tb_switch_is_usb4(sw)) { in tb_switch_set_uuid()
2647 ret = usb4_switch_read_uid(sw, &sw->uid); in tb_switch_set_uuid()
2656 ret = tb_lc_read_uuid(sw, uuid); in tb_switch_set_uuid()
2658 if (ret != -EINVAL) in tb_switch_set_uuid()
2671 uuid[0] = sw->uid & 0xffffffff; in tb_switch_set_uuid()
2672 uuid[1] = (sw->uid >> 32) & 0xffffffff; in tb_switch_set_uuid()
2677 sw->uuid = kmemdup(uuid, sizeof(uuid), GFP_KERNEL); in tb_switch_set_uuid()
2678 if (!sw->uuid) in tb_switch_set_uuid()
2679 return -ENOMEM; in tb_switch_set_uuid()
2683 static int tb_switch_add_dma_port(struct tb_switch *sw) in tb_switch_add_dma_port() argument
2688 switch (sw->generation) { in tb_switch_add_dma_port()
2691 if (tb_route(sw)) in tb_switch_add_dma_port()
2697 ret = tb_switch_set_uuid(sw); in tb_switch_add_dma_port()
2707 if (!sw->safe_mode) in tb_switch_add_dma_port()
2712 if (sw->no_nvm_upgrade) in tb_switch_add_dma_port()
2715 if (tb_switch_is_usb4(sw)) { in tb_switch_add_dma_port()
2716 ret = usb4_switch_nvm_authenticate_status(sw, &status); in tb_switch_add_dma_port()
2721 tb_sw_info(sw, "switch flash authentication failed\n"); in tb_switch_add_dma_port()
2722 nvm_set_auth_status(sw, status); in tb_switch_add_dma_port()
2729 if (!tb_route(sw) && !tb_switch_is_icm(sw)) in tb_switch_add_dma_port()
2732 sw->dma_port = dma_port_alloc(sw); in tb_switch_add_dma_port()
2733 if (!sw->dma_port) in tb_switch_add_dma_port()
2742 nvm_get_auth_status(sw, &status); in tb_switch_add_dma_port()
2744 if (!tb_route(sw)) in tb_switch_add_dma_port()
2745 nvm_authenticate_complete_dma_port(sw); in tb_switch_add_dma_port()
2754 ret = dma_port_flash_update_auth_status(sw->dma_port, &status); in tb_switch_add_dma_port()
2759 if (!tb_route(sw)) in tb_switch_add_dma_port()
2760 nvm_authenticate_complete_dma_port(sw); in tb_switch_add_dma_port()
2763 tb_sw_info(sw, "switch flash authentication failed\n"); in tb_switch_add_dma_port()
2764 nvm_set_auth_status(sw, status); in tb_switch_add_dma_port()
2767 tb_sw_info(sw, "power cycling the switch now\n"); in tb_switch_add_dma_port()
2768 dma_port_power_cycle(sw->dma_port); in tb_switch_add_dma_port()
2774 return -ESHUTDOWN; in tb_switch_add_dma_port()
2777 static void tb_switch_default_link_ports(struct tb_switch *sw) in tb_switch_default_link_ports() argument
2781 for (i = 1; i <= sw->config.max_port_number; i++) { in tb_switch_default_link_ports()
2782 struct tb_port *port = &sw->ports[i]; in tb_switch_default_link_ports()
2789 if (i == sw->config.max_port_number || in tb_switch_default_link_ports()
2790 !tb_port_is_null(&sw->ports[i + 1])) in tb_switch_default_link_ports()
2794 subordinate = &sw->ports[i + 1]; in tb_switch_default_link_ports()
2795 if (!port->dual_link_port && !subordinate->dual_link_port) { in tb_switch_default_link_ports()
2796 port->link_nr = 0; in tb_switch_default_link_ports()
2797 port->dual_link_port = subordinate; in tb_switch_default_link_ports()
2798 subordinate->link_nr = 1; in tb_switch_default_link_ports()
2799 subordinate->dual_link_port = port; in tb_switch_default_link_ports()
2801 tb_sw_dbg(sw, "linked ports %d <-> %d\n", in tb_switch_default_link_ports()
2802 port->port, subordinate->port); in tb_switch_default_link_ports()
2807 static bool tb_switch_lane_bonding_possible(struct tb_switch *sw) in tb_switch_lane_bonding_possible() argument
2809 const struct tb_port *up = tb_upstream_port(sw); in tb_switch_lane_bonding_possible()
2811 if (!up->dual_link_port || !up->dual_link_port->remote) in tb_switch_lane_bonding_possible()
2814 if (tb_switch_is_usb4(sw)) in tb_switch_lane_bonding_possible()
2815 return usb4_switch_lane_bonding_possible(sw); in tb_switch_lane_bonding_possible()
2816 return tb_lc_lane_bonding_possible(sw); in tb_switch_lane_bonding_possible()
2819 static int tb_switch_update_link_attributes(struct tb_switch *sw) in tb_switch_update_link_attributes() argument
2825 if (!tb_route(sw) || tb_switch_is_icm(sw)) in tb_switch_update_link_attributes()
2828 up = tb_upstream_port(sw); in tb_switch_update_link_attributes()
2833 if (sw->link_speed != ret) in tb_switch_update_link_attributes()
2835 sw->link_speed = ret; in tb_switch_update_link_attributes()
2840 if (sw->link_width != ret) in tb_switch_update_link_attributes()
2842 sw->link_width = ret; in tb_switch_update_link_attributes()
2845 if (device_is_registered(&sw->dev) && change) in tb_switch_update_link_attributes()
2846 kobject_uevent(&sw->dev.kobj, KOBJ_CHANGE); in tb_switch_update_link_attributes()
2852 static void tb_switch_link_init(struct tb_switch *sw) in tb_switch_link_init() argument
2857 if (!tb_route(sw) || tb_switch_is_icm(sw)) in tb_switch_link_init()
2860 tb_sw_dbg(sw, "current link speed %u.0 Gb/s\n", sw->link_speed); in tb_switch_link_init()
2861 tb_sw_dbg(sw, "current link width %s\n", tb_width_name(sw->link_width)); in tb_switch_link_init()
2863 bonded = sw->link_width >= TB_LINK_WIDTH_DUAL; in tb_switch_link_init()
2869 up = tb_upstream_port(sw); in tb_switch_link_init()
2870 down = tb_switch_downstream_port(sw); in tb_switch_link_init()
2872 up->bonded = bonded; in tb_switch_link_init()
2873 if (up->dual_link_port) in tb_switch_link_init()
2874 up->dual_link_port->bonded = bonded; in tb_switch_link_init()
2877 down->bonded = bonded; in tb_switch_link_init()
2878 if (down->dual_link_port) in tb_switch_link_init()
2879 down->dual_link_port->bonded = bonded; in tb_switch_link_init()
2891 sw->preferred_link_width = sw->link_width; in tb_switch_link_init()
2892 tb_sw_dbg(sw, "preferred link width %s\n", in tb_switch_link_init()
2893 tb_width_name(sw->preferred_link_width)); in tb_switch_link_init()
2897 * tb_switch_lane_bonding_enable() - Enable lane bonding
2898 * @sw: Switch to enable lane bonding
2904 static int tb_switch_lane_bonding_enable(struct tb_switch *sw) in tb_switch_lane_bonding_enable() argument
2910 if (!tb_switch_lane_bonding_possible(sw)) in tb_switch_lane_bonding_enable()
2913 up = tb_upstream_port(sw); in tb_switch_lane_bonding_enable()
2914 down = tb_switch_downstream_port(sw); in tb_switch_lane_bonding_enable()
2924 if (tb_wait_for_port(down->dual_link_port, false) <= 0) in tb_switch_lane_bonding_enable()
2925 return -ENOTCONN; in tb_switch_lane_bonding_enable()
2948 * tb_switch_lane_bonding_disable() - Disable lane bonding
2949 * @sw: Switch whose lane bonding to disable
2951 * Disables lane bonding between @sw and parent. This can be called even
2954 static int tb_switch_lane_bonding_disable(struct tb_switch *sw) in tb_switch_lane_bonding_disable() argument
2959 up = tb_upstream_port(sw); in tb_switch_lane_bonding_disable()
2960 if (!up->bonded) in tb_switch_lane_bonding_disable()
2966 * if the link is not up anymore (sw is unplugged). in tb_switch_lane_bonding_disable()
2972 return -EOPNOTSUPP; in tb_switch_lane_bonding_disable()
2974 down = tb_switch_downstream_port(sw); in tb_switch_lane_bonding_disable()
2985 /* Note updating sw->link_width done in tb_switch_update_link_attributes() */
2986 static int tb_switch_asym_enable(struct tb_switch *sw, enum tb_link_width width) in tb_switch_asym_enable() argument
2992 up = tb_upstream_port(sw); in tb_switch_asym_enable()
2993 down = tb_switch_downstream_port(sw); in tb_switch_asym_enable()
3015 if (sw->link_width != width) { in tb_switch_asym_enable()
3028 /* Note updating sw->link_width done in tb_switch_update_link_attributes() */
3029 static int tb_switch_asym_disable(struct tb_switch *sw) in tb_switch_asym_disable() argument
3034 up = tb_upstream_port(sw); in tb_switch_asym_disable()
3035 down = tb_switch_downstream_port(sw); in tb_switch_asym_disable()
3050 if (sw->link_width > TB_LINK_WIDTH_DUAL) { in tb_switch_asym_disable()
3051 if (sw->link_width == TB_LINK_WIDTH_ASYM_TX) in tb_switch_asym_disable()
3067 * tb_switch_set_link_width() - Configure router link width
3068 * @sw: Router to configure
3079 int tb_switch_set_link_width(struct tb_switch *sw, enum tb_link_width width) in tb_switch_set_link_width() argument
3084 if (!tb_route(sw)) in tb_switch_set_link_width()
3087 up = tb_upstream_port(sw); in tb_switch_set_link_width()
3088 down = tb_switch_downstream_port(sw); in tb_switch_set_link_width()
3092 ret = tb_switch_lane_bonding_disable(sw); in tb_switch_set_link_width()
3096 if (sw->link_width == TB_LINK_WIDTH_ASYM_TX || in tb_switch_set_link_width()
3097 sw->link_width == TB_LINK_WIDTH_ASYM_RX) { in tb_switch_set_link_width()
3098 ret = tb_switch_asym_disable(sw); in tb_switch_set_link_width()
3102 ret = tb_switch_lane_bonding_enable(sw); in tb_switch_set_link_width()
3107 ret = tb_switch_asym_enable(sw, width); in tb_switch_set_link_width()
3115 case -ETIMEDOUT: in tb_switch_set_link_width()
3116 tb_sw_warn(sw, "timeout changing link width\n"); in tb_switch_set_link_width()
3119 case -ENOTCONN: in tb_switch_set_link_width()
3120 case -EOPNOTSUPP: in tb_switch_set_link_width()
3121 case -ENODEV: in tb_switch_set_link_width()
3125 tb_sw_dbg(sw, "failed to change link width: %d\n", ret); in tb_switch_set_link_width()
3132 tb_switch_update_link_attributes(sw); in tb_switch_set_link_width()
3134 tb_sw_dbg(sw, "link width set to %s\n", tb_width_name(width)); in tb_switch_set_link_width()
3139 * tb_switch_configure_link() - Set link configured
3140 * @sw: Switch whose link is configured
3142 * Sets the link upstream from @sw configured (from both ends) so that
3150 int tb_switch_configure_link(struct tb_switch *sw) in tb_switch_configure_link() argument
3155 if (!tb_route(sw) || tb_switch_is_icm(sw)) in tb_switch_configure_link()
3158 up = tb_upstream_port(sw); in tb_switch_configure_link()
3159 if (tb_switch_is_usb4(up->sw)) in tb_switch_configure_link()
3166 down = up->remote; in tb_switch_configure_link()
3167 if (tb_switch_is_usb4(down->sw)) in tb_switch_configure_link()
3173 * tb_switch_unconfigure_link() - Unconfigure link
3174 * @sw: Switch whose link is unconfigured
3176 * Sets the link unconfigured so the @sw will be disconnected if the
3179 void tb_switch_unconfigure_link(struct tb_switch *sw) in tb_switch_unconfigure_link() argument
3183 if (!tb_route(sw) || tb_switch_is_icm(sw)) in tb_switch_unconfigure_link()
3187 * Unconfigure downstream port so that wake-on-connect can be in tb_switch_unconfigure_link()
3191 up = tb_upstream_port(sw); in tb_switch_unconfigure_link()
3192 down = up->remote; in tb_switch_unconfigure_link()
3193 if (tb_switch_is_usb4(down->sw)) in tb_switch_unconfigure_link()
3198 if (sw->is_unplugged) in tb_switch_unconfigure_link()
3201 up = tb_upstream_port(sw); in tb_switch_unconfigure_link()
3202 if (tb_switch_is_usb4(up->sw)) in tb_switch_unconfigure_link()
3208 static void tb_switch_credits_init(struct tb_switch *sw) in tb_switch_credits_init() argument
3210 if (tb_switch_is_icm(sw)) in tb_switch_credits_init()
3212 if (!tb_switch_is_usb4(sw)) in tb_switch_credits_init()
3214 if (usb4_switch_credits_init(sw)) in tb_switch_credits_init()
3215 tb_sw_info(sw, "failed to determine preferred buffer allocation, using defaults\n"); in tb_switch_credits_init()
3218 static int tb_switch_port_hotplug_enable(struct tb_switch *sw) in tb_switch_port_hotplug_enable() argument
3222 if (tb_switch_is_icm(sw)) in tb_switch_port_hotplug_enable()
3225 tb_switch_for_each_port(sw, port) { in tb_switch_port_hotplug_enable()
3228 if (!port->cap_usb4) in tb_switch_port_hotplug_enable()
3239 * tb_switch_add() - Add a switch to the domain
3240 * @sw: Switch to add
3250 int tb_switch_add(struct tb_switch *sw) in tb_switch_add() argument
3261 ret = tb_switch_add_dma_port(sw); in tb_switch_add()
3263 dev_err(&sw->dev, "failed to add DMA port\n"); in tb_switch_add()
3267 if (!sw->safe_mode) { in tb_switch_add()
3268 tb_switch_credits_init(sw); in tb_switch_add()
3271 ret = tb_drom_read(sw); in tb_switch_add()
3273 dev_warn(&sw->dev, "reading DROM failed: %d\n", ret); in tb_switch_add()
3274 tb_sw_dbg(sw, "uid: %#llx\n", sw->uid); in tb_switch_add()
3276 ret = tb_switch_set_uuid(sw); in tb_switch_add()
3278 dev_err(&sw->dev, "failed to set UUID\n"); in tb_switch_add()
3282 for (i = 0; i <= sw->config.max_port_number; i++) { in tb_switch_add()
3283 if (sw->ports[i].disabled) { in tb_switch_add()
3284 tb_port_dbg(&sw->ports[i], "disabled by eeprom\n"); in tb_switch_add()
3287 ret = tb_init_port(&sw->ports[i]); in tb_switch_add()
3289 dev_err(&sw->dev, "failed to initialize port %d\n", i); in tb_switch_add()
3294 tb_check_quirks(sw); in tb_switch_add()
3296 tb_switch_default_link_ports(sw); in tb_switch_add()
3298 ret = tb_switch_update_link_attributes(sw); in tb_switch_add()
3302 tb_switch_link_init(sw); in tb_switch_add()
3304 ret = tb_switch_clx_init(sw); in tb_switch_add()
3308 ret = tb_switch_tmu_init(sw); in tb_switch_add()
3313 ret = tb_switch_port_hotplug_enable(sw); in tb_switch_add()
3317 ret = device_add(&sw->dev); in tb_switch_add()
3319 dev_err(&sw->dev, "failed to add device: %d\n", ret); in tb_switch_add()
3323 if (tb_route(sw)) { in tb_switch_add()
3324 dev_info(&sw->dev, "new device found, vendor=%#x device=%#x\n", in tb_switch_add()
3325 sw->vendor, sw->device); in tb_switch_add()
3326 if (sw->vendor_name && sw->device_name) in tb_switch_add()
3327 dev_info(&sw->dev, "%s %s\n", sw->vendor_name, in tb_switch_add()
3328 sw->device_name); in tb_switch_add()
3331 ret = usb4_switch_add_ports(sw); in tb_switch_add()
3333 dev_err(&sw->dev, "failed to add USB4 ports\n"); in tb_switch_add()
3337 ret = tb_switch_nvm_add(sw); in tb_switch_add()
3339 dev_err(&sw->dev, "failed to add NVM devices\n"); in tb_switch_add()
3348 device_init_wakeup(&sw->dev, true); in tb_switch_add()
3350 pm_runtime_set_active(&sw->dev); in tb_switch_add()
3351 if (sw->rpm) { in tb_switch_add()
3352 pm_runtime_set_autosuspend_delay(&sw->dev, TB_AUTOSUSPEND_DELAY); in tb_switch_add()
3353 pm_runtime_use_autosuspend(&sw->dev); in tb_switch_add()
3354 pm_runtime_mark_last_busy(&sw->dev); in tb_switch_add()
3355 pm_runtime_enable(&sw->dev); in tb_switch_add()
3356 pm_request_autosuspend(&sw->dev); in tb_switch_add()
3359 tb_switch_debugfs_init(sw); in tb_switch_add()
3363 usb4_switch_remove_ports(sw); in tb_switch_add()
3365 device_del(&sw->dev); in tb_switch_add()
3371 * tb_switch_remove() - Remove and release a switch
3372 * @sw: Switch to remove
3378 void tb_switch_remove(struct tb_switch *sw) in tb_switch_remove() argument
3382 tb_switch_debugfs_remove(sw); in tb_switch_remove()
3384 if (sw->rpm) { in tb_switch_remove()
3385 pm_runtime_get_sync(&sw->dev); in tb_switch_remove()
3386 pm_runtime_disable(&sw->dev); in tb_switch_remove()
3390 tb_switch_for_each_port(sw, port) { in tb_switch_remove()
3392 tb_switch_remove(port->remote->sw); in tb_switch_remove()
3393 port->remote = NULL; in tb_switch_remove()
3394 } else if (port->xdomain) { in tb_switch_remove()
3395 port->xdomain->is_unplugged = true; in tb_switch_remove()
3396 tb_xdomain_remove(port->xdomain); in tb_switch_remove()
3397 port->xdomain = NULL; in tb_switch_remove()
3404 if (!sw->is_unplugged) in tb_switch_remove()
3405 tb_plug_events_active(sw, false); in tb_switch_remove()
3407 tb_switch_nvm_remove(sw); in tb_switch_remove()
3408 usb4_switch_remove_ports(sw); in tb_switch_remove()
3410 if (tb_route(sw)) in tb_switch_remove()
3411 dev_info(&sw->dev, "device disconnected\n"); in tb_switch_remove()
3412 device_unregister(&sw->dev); in tb_switch_remove()
3416 * tb_sw_set_unplugged() - set is_unplugged on switch and downstream switches
3417 * @sw: Router to mark unplugged
3419 void tb_sw_set_unplugged(struct tb_switch *sw) in tb_sw_set_unplugged() argument
3423 if (sw == sw->tb->root_switch) { in tb_sw_set_unplugged()
3424 tb_sw_WARN(sw, "cannot unplug root switch\n"); in tb_sw_set_unplugged()
3427 if (sw->is_unplugged) { in tb_sw_set_unplugged()
3428 tb_sw_WARN(sw, "is_unplugged already set\n"); in tb_sw_set_unplugged()
3431 sw->is_unplugged = true; in tb_sw_set_unplugged()
3432 tb_switch_for_each_port(sw, port) { in tb_sw_set_unplugged()
3434 tb_sw_set_unplugged(port->remote->sw); in tb_sw_set_unplugged()
3435 else if (port->xdomain) in tb_sw_set_unplugged()
3436 port->xdomain->is_unplugged = true; in tb_sw_set_unplugged()
3440 static int tb_switch_set_wake(struct tb_switch *sw, unsigned int flags) in tb_switch_set_wake() argument
3443 tb_sw_dbg(sw, "enabling wakeup: %#x\n", flags); in tb_switch_set_wake()
3445 tb_sw_dbg(sw, "disabling wakeup\n"); in tb_switch_set_wake()
3447 if (tb_switch_is_usb4(sw)) in tb_switch_set_wake()
3448 return usb4_switch_set_wake(sw, flags); in tb_switch_set_wake()
3449 return tb_lc_set_wake(sw, flags); in tb_switch_set_wake()
3452 static void tb_switch_check_wakes(struct tb_switch *sw) in tb_switch_check_wakes() argument
3454 if (device_may_wakeup(&sw->dev)) { in tb_switch_check_wakes()
3455 if (tb_switch_is_usb4(sw)) in tb_switch_check_wakes()
3456 usb4_switch_check_wakes(sw); in tb_switch_check_wakes()
3461 * tb_switch_resume() - Resume a switch after sleep
3462 * @sw: Switch to resume
3465 * Resumes and re-enumerates router (and all its children), if still plugged
3471 int tb_switch_resume(struct tb_switch *sw, bool runtime) in tb_switch_resume() argument
3476 tb_sw_dbg(sw, "resuming switch\n"); in tb_switch_resume()
3482 if (tb_route(sw)) { in tb_switch_resume()
3490 err = tb_cfg_get_upstream_port(sw->tb->ctl, tb_route(sw)); in tb_switch_resume()
3492 tb_sw_info(sw, "switch not present anymore\n"); in tb_switch_resume()
3497 if (!sw->uid) in tb_switch_resume()
3498 return -ENODEV; in tb_switch_resume()
3500 if (tb_switch_is_usb4(sw)) in tb_switch_resume()
3501 err = usb4_switch_read_uid(sw, &uid); in tb_switch_resume()
3503 err = tb_drom_read_uid_only(sw, &uid); in tb_switch_resume()
3505 tb_sw_warn(sw, "uid read failed\n"); in tb_switch_resume()
3508 if (sw->uid != uid) { in tb_switch_resume()
3509 tb_sw_info(sw, in tb_switch_resume()
3510 "changed while suspended (uid %#llx -> %#llx)\n", in tb_switch_resume()
3511 sw->uid, uid); in tb_switch_resume()
3512 return -ENODEV; in tb_switch_resume()
3516 err = tb_switch_configure(sw); in tb_switch_resume()
3521 tb_switch_check_wakes(sw); in tb_switch_resume()
3524 tb_switch_set_wake(sw, 0); in tb_switch_resume()
3526 err = tb_switch_tmu_init(sw); in tb_switch_resume()
3531 tb_switch_for_each_port(sw, port) { in tb_switch_resume()
3542 tb_sw_set_unplugged(port->remote->sw); in tb_switch_resume()
3543 else if (port->xdomain) in tb_switch_resume()
3544 port->xdomain->is_unplugged = true; in tb_switch_resume()
3552 if (port->remote && in tb_switch_resume()
3553 tb_switch_resume(port->remote->sw, runtime)) { in tb_switch_resume()
3556 tb_sw_set_unplugged(port->remote->sw); in tb_switch_resume()
3564 * tb_switch_suspend() - Put a switch to sleep
3565 * @sw: Switch to suspend
3569 * value of @runtime and then sets sleep bit for the router. If @sw is
3573 void tb_switch_suspend(struct tb_switch *sw, bool runtime) in tb_switch_suspend() argument
3579 tb_sw_dbg(sw, "suspending switch\n"); in tb_switch_suspend()
3583 * done for USB4 device too as CLx is re-enabled at resume. in tb_switch_suspend()
3585 tb_switch_clx_disable(sw); in tb_switch_suspend()
3587 err = tb_plug_events_active(sw, false); in tb_switch_suspend()
3591 tb_switch_for_each_port(sw, port) { in tb_switch_suspend()
3593 tb_switch_suspend(port->remote->sw, runtime); in tb_switch_suspend()
3601 } else if (device_may_wakeup(&sw->dev)) { in tb_switch_suspend()
3605 tb_switch_set_wake(sw, flags); in tb_switch_suspend()
3607 if (tb_switch_is_usb4(sw)) in tb_switch_suspend()
3608 usb4_switch_set_sleep(sw); in tb_switch_suspend()
3610 tb_lc_set_sleep(sw); in tb_switch_suspend()
3614 * tb_switch_query_dp_resource() - Query availability of DP resource
3615 * @sw: Switch whose DP resource is queried
3621 bool tb_switch_query_dp_resource(struct tb_switch *sw, struct tb_port *in) in tb_switch_query_dp_resource() argument
3623 if (tb_switch_is_usb4(sw)) in tb_switch_query_dp_resource()
3624 return usb4_switch_query_dp_resource(sw, in); in tb_switch_query_dp_resource()
3625 return tb_lc_dp_sink_query(sw, in); in tb_switch_query_dp_resource()
3629 * tb_switch_alloc_dp_resource() - Allocate available DP resource
3630 * @sw: Switch whose DP resource is allocated
3637 int tb_switch_alloc_dp_resource(struct tb_switch *sw, struct tb_port *in) in tb_switch_alloc_dp_resource() argument
3641 if (tb_switch_is_usb4(sw)) in tb_switch_alloc_dp_resource()
3642 ret = usb4_switch_alloc_dp_resource(sw, in); in tb_switch_alloc_dp_resource()
3644 ret = tb_lc_dp_sink_alloc(sw, in); in tb_switch_alloc_dp_resource()
3647 tb_sw_warn(sw, "failed to allocate DP resource for port %d\n", in tb_switch_alloc_dp_resource()
3648 in->port); in tb_switch_alloc_dp_resource()
3650 tb_sw_dbg(sw, "allocated DP resource for port %d\n", in->port); in tb_switch_alloc_dp_resource()
3656 * tb_switch_dealloc_dp_resource() - De-allocate DP resource
3657 * @sw: Switch whose DP resource is de-allocated
3660 * De-allocates DP resource that was previously allocated for DP
3663 void tb_switch_dealloc_dp_resource(struct tb_switch *sw, struct tb_port *in) in tb_switch_dealloc_dp_resource() argument
3667 if (tb_switch_is_usb4(sw)) in tb_switch_dealloc_dp_resource()
3668 ret = usb4_switch_dealloc_dp_resource(sw, in); in tb_switch_dealloc_dp_resource()
3670 ret = tb_lc_dp_sink_dealloc(sw, in); in tb_switch_dealloc_dp_resource()
3673 tb_sw_warn(sw, "failed to de-allocate DP resource for port %d\n", in tb_switch_dealloc_dp_resource()
3674 in->port); in tb_switch_dealloc_dp_resource()
3676 tb_sw_dbg(sw, "released DP resource for port %d\n", in->port); in tb_switch_dealloc_dp_resource()
3689 struct tb_switch *sw = tb_to_switch(dev); in tb_switch_match() local
3692 if (!sw) in tb_switch_match()
3694 if (sw->tb != lookup->tb) in tb_switch_match()
3697 if (lookup->uuid) in tb_switch_match()
3698 return !memcmp(sw->uuid, lookup->uuid, sizeof(*lookup->uuid)); in tb_switch_match()
3700 if (lookup->route) { in tb_switch_match()
3701 return sw->config.route_lo == lower_32_bits(lookup->route) && in tb_switch_match()
3702 sw->config.route_hi == upper_32_bits(lookup->route); in tb_switch_match()
3706 if (!lookup->depth) in tb_switch_match()
3707 return !sw->depth; in tb_switch_match()
3709 return sw->link == lookup->link && sw->depth == lookup->depth; in tb_switch_match()
3713 * tb_switch_find_by_link_depth() - Find switch by link and depth
3715 * @link: Link number the switch is connected
3739 * tb_switch_find_by_uuid() - Find switch by UUID
3763 * tb_switch_find_by_route() - Find switch by route string
3776 return tb_switch_get(tb->root_switch); in tb_switch_find_by_route()
3790 * tb_switch_find_port() - return the first port of @type on @sw or NULL
3791 * @sw: Switch to find the port from
3794 struct tb_port *tb_switch_find_port(struct tb_switch *sw, in tb_switch_find_port() argument
3799 tb_switch_for_each_port(sw, port) { in tb_switch_find_port()
3800 if (port->config.type == type) in tb_switch_find_port()
3811 static int tb_switch_pcie_bridge_write(struct tb_switch *sw, unsigned int bridge, in tb_switch_pcie_bridge_write() argument
3817 if (sw->generation != 3) in tb_switch_pcie_bridge_write()
3818 return -EOPNOTSUPP; in tb_switch_pcie_bridge_write()
3820 offset = sw->cap_plug_events + TB_PLUG_EVENTS_PCIE_WR_DATA; in tb_switch_pcie_bridge_write()
3821 ret = tb_sw_write(sw, &value, TB_CFG_SWITCH, offset, 1); in tb_switch_pcie_bridge_write()
3832 offset = sw->cap_plug_events + TB_PLUG_EVENTS_PCIE_CMD; in tb_switch_pcie_bridge_write()
3834 ret = tb_sw_write(sw, &command, TB_CFG_SWITCH, offset, 1); in tb_switch_pcie_bridge_write()
3838 ret = tb_switch_wait_for_bit(sw, offset, in tb_switch_pcie_bridge_write()
3843 ret = tb_sw_read(sw, &val, TB_CFG_SWITCH, offset, 1); in tb_switch_pcie_bridge_write()
3848 return -ETIMEDOUT; in tb_switch_pcie_bridge_write()
3854 * tb_switch_pcie_l1_enable() - Enable PCIe link to enter L1 state
3855 * @sw: Router to enable PCIe L1
3862 int tb_switch_pcie_l1_enable(struct tb_switch *sw) in tb_switch_pcie_l1_enable() argument
3864 struct tb_switch *parent = tb_switch_parent(sw); in tb_switch_pcie_l1_enable()
3867 if (!tb_route(sw)) in tb_switch_pcie_l1_enable()
3870 if (!tb_switch_is_titan_ridge(sw)) in tb_switch_pcie_l1_enable()
3878 ret = tb_switch_pcie_bridge_write(sw, 5, 0x143, 0x0c7806b1); in tb_switch_pcie_l1_enable()
3883 return tb_switch_pcie_bridge_write(sw, 0, 0x143, 0x0c5806b1); in tb_switch_pcie_l1_enable()
3887 * tb_switch_xhci_connect() - Connect internal xHCI
3888 * @sw: Router whose xHCI to connect
3892 * connected to the type-C port. Call only after PCIe tunnel has been
3896 int tb_switch_xhci_connect(struct tb_switch *sw) in tb_switch_xhci_connect() argument
3901 if (sw->generation != 3) in tb_switch_xhci_connect()
3904 port1 = &sw->ports[1]; in tb_switch_xhci_connect()
3905 port3 = &sw->ports[3]; in tb_switch_xhci_connect()
3907 if (tb_switch_is_alpine_ridge(sw)) { in tb_switch_xhci_connect()
3923 } else if (tb_switch_is_titan_ridge(sw)) { in tb_switch_xhci_connect()
3934 * tb_switch_xhci_disconnect() - Disconnect internal xHCI
3935 * @sw: Router whose xHCI to disconnect
3940 void tb_switch_xhci_disconnect(struct tb_switch *sw) in tb_switch_xhci_disconnect() argument
3942 if (sw->generation == 3) { in tb_switch_xhci_disconnect()
3943 struct tb_port *port1 = &sw->ports[1]; in tb_switch_xhci_disconnect()
3944 struct tb_port *port3 = &sw->ports[3]; in tb_switch_xhci_disconnect()