• Home
  • Raw
  • Download

Lines Matching +full:sw +full:- +full:mode

1 // SPDX-License-Identifier: GPL-2.0
3 * Thunderbolt driver - switch/port utility functions
11 #include <linux/nvmem-provider.h>
42 static struct nvm_auth_status *__nvm_get_auth_status(const struct tb_switch *sw) in __nvm_get_auth_status() argument
47 if (uuid_equal(&st->uuid, sw->uuid)) in __nvm_get_auth_status()
54 static void nvm_get_auth_status(const struct tb_switch *sw, u32 *status) in nvm_get_auth_status() argument
59 st = __nvm_get_auth_status(sw); in nvm_get_auth_status()
62 *status = st ? st->status : 0; in nvm_get_auth_status()
65 static void nvm_set_auth_status(const struct tb_switch *sw, u32 status) in nvm_set_auth_status() argument
69 if (WARN_ON(!sw->uuid)) in nvm_set_auth_status()
73 st = __nvm_get_auth_status(sw); in nvm_set_auth_status()
80 memcpy(&st->uuid, sw->uuid, sizeof(st->uuid)); in nvm_set_auth_status()
81 INIT_LIST_HEAD(&st->list); in nvm_set_auth_status()
82 list_add_tail(&st->list, &nvm_auth_status_cache); in nvm_set_auth_status()
85 st->status = status; in nvm_set_auth_status()
90 static void nvm_clear_auth_status(const struct tb_switch *sw) in nvm_clear_auth_status() argument
95 st = __nvm_get_auth_status(sw); in nvm_clear_auth_status()
97 list_del(&st->list); in nvm_clear_auth_status()
103 static int nvm_validate_and_write(struct tb_switch *sw) in nvm_validate_and_write() argument
106 const u8 *buf = sw->nvm->buf; in nvm_validate_and_write()
111 return -EINVAL; in nvm_validate_and_write()
113 image_size = sw->nvm->buf_data_size; in nvm_validate_and_write()
115 return -EINVAL; in nvm_validate_and_write()
123 return -EINVAL; in nvm_validate_and_write()
127 return -EINVAL; in nvm_validate_and_write()
135 return -EINVAL; in nvm_validate_and_write()
137 if (!sw->safe_mode) { in nvm_validate_and_write()
145 if (device_id != sw->config.device_id) in nvm_validate_and_write()
146 return -EINVAL; in nvm_validate_and_write()
148 if (sw->generation < 3) { in nvm_validate_and_write()
150 ret = dma_port_flash_write(sw->dma_port, in nvm_validate_and_write()
159 image_size -= hdr_size; in nvm_validate_and_write()
162 if (tb_switch_is_usb4(sw)) in nvm_validate_and_write()
163 ret = usb4_switch_nvm_write(sw, 0, buf, image_size); in nvm_validate_and_write()
165 ret = dma_port_flash_write(sw->dma_port, 0, buf, image_size); in nvm_validate_and_write()
167 sw->nvm->flushed = true; in nvm_validate_and_write()
171 static int nvm_authenticate_host_dma_port(struct tb_switch *sw) in nvm_authenticate_host_dma_port() argument
177 * existing paths first (in case it is not in safe mode in nvm_authenticate_host_dma_port()
180 if (!sw->safe_mode) { in nvm_authenticate_host_dma_port()
183 ret = tb_domain_disconnect_all_paths(sw->tb); in nvm_authenticate_host_dma_port()
190 ret = dma_port_flash_update_auth(sw->dma_port); in nvm_authenticate_host_dma_port()
191 if (!ret || ret == -ETIMEDOUT) in nvm_authenticate_host_dma_port()
198 tb_sw_warn(sw, "failed to authenticate NVM, power cycling\n"); in nvm_authenticate_host_dma_port()
199 if (dma_port_flash_update_auth_status(sw->dma_port, &status) > 0) in nvm_authenticate_host_dma_port()
200 nvm_set_auth_status(sw, status); in nvm_authenticate_host_dma_port()
204 * From safe mode we can get out by just power cycling the in nvm_authenticate_host_dma_port()
207 dma_port_power_cycle(sw->dma_port); in nvm_authenticate_host_dma_port()
211 static int nvm_authenticate_device_dma_port(struct tb_switch *sw) in nvm_authenticate_device_dma_port() argument
215 ret = dma_port_flash_update_auth(sw->dma_port); in nvm_authenticate_device_dma_port()
218 case -ETIMEDOUT: in nvm_authenticate_device_dma_port()
219 case -EACCES: in nvm_authenticate_device_dma_port()
220 case -EINVAL: in nvm_authenticate_device_dma_port()
236 ret = dma_port_flash_update_auth_status(sw->dma_port, &status); in nvm_authenticate_device_dma_port()
237 if (ret < 0 && ret != -ETIMEDOUT) in nvm_authenticate_device_dma_port()
241 tb_sw_warn(sw, "failed to authenticate NVM\n"); in nvm_authenticate_device_dma_port()
242 nvm_set_auth_status(sw, status); in nvm_authenticate_device_dma_port()
245 tb_sw_info(sw, "power cycling the switch now\n"); in nvm_authenticate_device_dma_port()
246 dma_port_power_cycle(sw->dma_port); in nvm_authenticate_device_dma_port()
251 } while (--retries); in nvm_authenticate_device_dma_port()
253 return -ETIMEDOUT; in nvm_authenticate_device_dma_port()
256 static void nvm_authenticate_start_dma_port(struct tb_switch *sw) in nvm_authenticate_start_dma_port() argument
266 root_port = pcie_find_root_port(sw->tb->nhi->pdev); in nvm_authenticate_start_dma_port()
268 pm_runtime_get_noresume(&root_port->dev); in nvm_authenticate_start_dma_port()
271 static void nvm_authenticate_complete_dma_port(struct tb_switch *sw) in nvm_authenticate_complete_dma_port() argument
275 root_port = pcie_find_root_port(sw->tb->nhi->pdev); in nvm_authenticate_complete_dma_port()
277 pm_runtime_put(&root_port->dev); in nvm_authenticate_complete_dma_port()
280 static inline bool nvm_readable(struct tb_switch *sw) in nvm_readable() argument
282 if (tb_switch_is_usb4(sw)) { in nvm_readable()
289 return usb4_switch_nvm_sector_size(sw) > 0; in nvm_readable()
293 return !!sw->dma_port; in nvm_readable()
296 static inline bool nvm_upgradeable(struct tb_switch *sw) in nvm_upgradeable() argument
298 if (sw->no_nvm_upgrade) in nvm_upgradeable()
300 return nvm_readable(sw); in nvm_upgradeable()
303 static inline int nvm_read(struct tb_switch *sw, unsigned int address, in nvm_read() argument
306 if (tb_switch_is_usb4(sw)) in nvm_read()
307 return usb4_switch_nvm_read(sw, address, buf, size); in nvm_read()
308 return dma_port_flash_read(sw->dma_port, address, buf, size); in nvm_read()
311 static int nvm_authenticate(struct tb_switch *sw) in nvm_authenticate() argument
315 if (tb_switch_is_usb4(sw)) in nvm_authenticate()
316 return usb4_switch_nvm_authenticate(sw); in nvm_authenticate()
318 if (!tb_route(sw)) { in nvm_authenticate()
319 nvm_authenticate_start_dma_port(sw); in nvm_authenticate()
320 ret = nvm_authenticate_host_dma_port(sw); in nvm_authenticate()
322 ret = nvm_authenticate_device_dma_port(sw); in nvm_authenticate()
332 struct tb_switch *sw = tb_to_switch(nvm->dev); in tb_switch_nvm_read() local
335 pm_runtime_get_sync(&sw->dev); in tb_switch_nvm_read()
337 if (!mutex_trylock(&sw->tb->lock)) { in tb_switch_nvm_read()
342 ret = nvm_read(sw, offset, val, bytes); in tb_switch_nvm_read()
343 mutex_unlock(&sw->tb->lock); in tb_switch_nvm_read()
346 pm_runtime_mark_last_busy(&sw->dev); in tb_switch_nvm_read()
347 pm_runtime_put_autosuspend(&sw->dev); in tb_switch_nvm_read()
356 struct tb_switch *sw = tb_to_switch(nvm->dev); in tb_switch_nvm_write() local
359 if (!mutex_trylock(&sw->tb->lock)) in tb_switch_nvm_write()
369 mutex_unlock(&sw->tb->lock); in tb_switch_nvm_write()
374 static int tb_switch_nvm_add(struct tb_switch *sw) in tb_switch_nvm_add() argument
380 if (!nvm_readable(sw)) in tb_switch_nvm_add()
384 * The NVM format of non-Intel hardware is not known so in tb_switch_nvm_add()
388 if (sw->config.vendor_id != PCI_VENDOR_ID_INTEL && in tb_switch_nvm_add()
389 sw->config.vendor_id != 0x8087) { in tb_switch_nvm_add()
390 dev_info(&sw->dev, in tb_switch_nvm_add()
392 sw->config.vendor_id); in tb_switch_nvm_add()
396 nvm = tb_nvm_alloc(&sw->dev); in tb_switch_nvm_add()
401 * If the switch is in safe-mode the only accessible portion of in tb_switch_nvm_add()
402 * the NVM is the non-active one where userspace is expected to in tb_switch_nvm_add()
405 if (!sw->safe_mode) { in tb_switch_nvm_add()
408 ret = nvm_read(sw, NVM_FLASH_SIZE, &val, sizeof(val)); in tb_switch_nvm_add()
412 hdr_size = sw->generation < 3 ? SZ_8K : SZ_16K; in tb_switch_nvm_add()
414 nvm_size = (nvm_size - hdr_size) / 2; in tb_switch_nvm_add()
416 ret = nvm_read(sw, NVM_VERSION, &val, sizeof(val)); in tb_switch_nvm_add()
420 nvm->major = val >> 16; in tb_switch_nvm_add()
421 nvm->minor = val >> 8; in tb_switch_nvm_add()
428 if (!sw->no_nvm_upgrade) { in tb_switch_nvm_add()
435 sw->nvm = nvm; in tb_switch_nvm_add()
443 static void tb_switch_nvm_remove(struct tb_switch *sw) in tb_switch_nvm_remove() argument
447 nvm = sw->nvm; in tb_switch_nvm_remove()
448 sw->nvm = NULL; in tb_switch_nvm_remove()
454 if (!nvm->authenticating) in tb_switch_nvm_remove()
455 nvm_clear_auth_status(sw); in tb_switch_nvm_remove()
464 switch (port->type >> 16) { in tb_port_type()
466 switch ((u8) port->type) { in tb_port_type()
495 port->port_number, port->vendor_id, port->device_id, in tb_dump_port()
496 port->revision, port->thunderbolt_version, tb_port_type(port), in tb_dump_port()
497 port->type); in tb_dump_port()
499 port->max_in_hop_id, port->max_out_hop_id); in tb_dump_port()
500 tb_dbg(tb, " Max counters: %d\n", port->max_counters); in tb_dump_port()
501 tb_dbg(tb, " NFC Credits: %#x\n", port->nfc_credits); in tb_dump_port()
505 * tb_port_state() - get connectedness state of a port
515 if (port->cap_phy == 0) { in tb_port_state()
517 return -EINVAL; in tb_port_state()
519 res = tb_port_read(port, &phy, TB_CFG_PORT, port->cap_phy, 2); in tb_port_state()
526 * tb_wait_for_port() - wait for a port to become ready
542 if (!port->cap_phy) { in tb_wait_for_port()
544 return -EINVAL; in tb_wait_for_port()
548 return -EINVAL; in tb_wait_for_port()
551 while (retries--) { in tb_wait_for_port()
576 * After plug-in the state is TB_PORT_CONNECTING. Give it some in tb_wait_for_port()
590 * tb_port_add_nfc_credits() - add/remove non flow controlled credits to port
601 if (credits == 0 || port->sw->is_unplugged) in tb_port_add_nfc_credits()
608 if (tb_switch_is_usb4(port->sw) && !tb_port_is_null(port)) in tb_port_add_nfc_credits()
611 nfc_credits = port->config.nfc_credits & ADP_CS_4_NFC_BUFFERS_MASK; in tb_port_add_nfc_credits()
615 port->config.nfc_credits & ADP_CS_4_NFC_BUFFERS_MASK); in tb_port_add_nfc_credits()
617 port->config.nfc_credits &= ~ADP_CS_4_NFC_BUFFERS_MASK; in tb_port_add_nfc_credits()
618 port->config.nfc_credits |= nfc_credits; in tb_port_add_nfc_credits()
620 return tb_port_write(port, &port->config.nfc_credits, in tb_port_add_nfc_credits()
625 * tb_port_set_initial_credits() - Set initial port link credits allocated
647 * tb_port_clear_counter() - clear a counter in TB_CFG_COUNTER
659 * tb_port_unlock() - Unlock downstream port
667 if (tb_switch_is_icm(port->sw)) in tb_port_unlock()
670 return -EINVAL; in tb_port_unlock()
671 if (tb_switch_is_usb4(port->sw)) in tb_port_unlock()
682 return -EINVAL; in __tb_port_enable()
685 port->cap_phy + LANE_ADP_CS_1, 1); in __tb_port_enable()
695 port->cap_phy + LANE_ADP_CS_1, 1); in __tb_port_enable()
699 * tb_port_enable() - Enable lane adapter
710 * tb_port_disable() - Disable lane adapter
721 * tb_init_port() - initialize a port
733 res = tb_port_read(port, &port->config, TB_CFG_PORT, 0, 8); in tb_init_port()
735 if (res == -ENODEV) { in tb_init_port()
736 tb_dbg(port->sw->tb, " Port %d: not implemented\n", in tb_init_port()
737 port->port); in tb_init_port()
738 port->disabled = true; in tb_init_port()
745 if (port->config.type == TB_TYPE_PORT && port->port != 0) { in tb_init_port()
749 port->cap_phy = cap; in tb_init_port()
755 port->cap_usb4 = cap; in tb_init_port()
756 } else if (port->port != 0) { in tb_init_port()
759 port->cap_adap = cap; in tb_init_port()
762 tb_dump_port(port->sw->tb, &port->config); in tb_init_port()
764 INIT_LIST_HEAD(&port->list); in tb_init_port()
776 port_max_hopid = port->config.max_in_hop_id; in tb_port_alloc_hopid()
777 ida = &port->in_hopids; in tb_port_alloc_hopid()
779 port_max_hopid = port->config.max_out_hop_id; in tb_port_alloc_hopid()
780 ida = &port->out_hopids; in tb_port_alloc_hopid()
784 * NHI can use HopIDs 1-max for other adapters HopIDs 0-7 are in tb_port_alloc_hopid()
797 * tb_port_alloc_in_hopid() - Allocate input HopID from port
811 * tb_port_alloc_out_hopid() - Allocate output HopID from port
825 * tb_port_release_in_hopid() - Release allocated input HopID from port
831 ida_simple_remove(&port->in_hopids, hopid); in tb_port_release_in_hopid()
835 * tb_port_release_out_hopid() - Release allocated output HopID from port
841 ida_simple_remove(&port->out_hopids, hopid); in tb_port_release_out_hopid()
845 const struct tb_switch *sw) in tb_switch_is_reachable() argument
847 u64 mask = (1ULL << parent->config.depth * 8) - 1; in tb_switch_is_reachable()
848 return (tb_route(parent) & mask) == (tb_route(sw) & mask); in tb_switch_is_reachable()
852 * tb_next_port_on_path() - Return next port for given port on a path
864 * Domain tb->lock must be held when this function is called.
874 if (prev->sw == end->sw) { in tb_next_port_on_path()
880 if (tb_switch_is_reachable(prev->sw, end->sw)) { in tb_next_port_on_path()
881 next = tb_port_at(tb_route(end->sw), prev->sw); in tb_next_port_on_path()
883 if (prev->remote && in tb_next_port_on_path()
884 (next == prev || next->dual_link_port == prev)) in tb_next_port_on_path()
885 next = prev->remote; in tb_next_port_on_path()
888 next = prev->remote; in tb_next_port_on_path()
890 next = tb_upstream_port(prev->sw); in tb_next_port_on_path()
895 if (next->dual_link_port && in tb_next_port_on_path()
896 next->link_nr != prev->link_nr) { in tb_next_port_on_path()
897 next = next->dual_link_port; in tb_next_port_on_path()
906 * tb_port_get_link_speed() - Get current link speed
916 if (!port->cap_phy) in tb_port_get_link_speed()
917 return -EINVAL; in tb_port_get_link_speed()
920 port->cap_phy + LANE_ADP_CS_1, 1); in tb_port_get_link_speed()
934 if (!port->cap_phy) in tb_port_get_link_width()
935 return -EINVAL; in tb_port_get_link_width()
938 port->cap_phy + LANE_ADP_CS_1, 1); in tb_port_get_link_width()
951 if (!port->cap_phy) in tb_port_is_width_supported()
955 port->cap_phy + LANE_ADP_CS_0, 1); in tb_port_is_width_supported()
970 if (!port->cap_phy) in tb_port_set_link_width()
971 return -EINVAL; in tb_port_set_link_width()
974 port->cap_phy + LANE_ADP_CS_1, 1); in tb_port_set_link_width()
989 return -EINVAL; in tb_port_set_link_width()
995 port->cap_phy + LANE_ADP_CS_1, 1); in tb_port_set_link_width()
1013 ret = tb_port_get_link_width(port->dual_link_port); in tb_port_lane_bonding_enable()
1015 ret = tb_port_set_link_width(port->dual_link_port, 2); in tb_port_lane_bonding_enable()
1022 port->bonded = true; in tb_port_lane_bonding_enable()
1023 port->dual_link_port->bonded = true; in tb_port_lane_bonding_enable()
1030 port->dual_link_port->bonded = false; in tb_port_lane_bonding_disable()
1031 port->bonded = false; in tb_port_lane_bonding_disable()
1033 tb_port_set_link_width(port->dual_link_port, 1); in tb_port_lane_bonding_disable()
1038 * tb_port_is_enabled() - Is the adapter port enabled
1043 switch (port->config.type) { in tb_port_is_enabled()
1062 * tb_usb3_port_is_enabled() - Is the USB3 adapter port enabled
1070 port->cap_adap + ADP_USB3_CS_0, 1)) in tb_usb3_port_is_enabled()
1077 * tb_usb3_port_enable() - Enable USB3 adapter port
1086 if (!port->cap_adap) in tb_usb3_port_enable()
1087 return -ENXIO; in tb_usb3_port_enable()
1089 port->cap_adap + ADP_USB3_CS_0, 1); in tb_usb3_port_enable()
1093 * tb_pci_port_is_enabled() - Is the PCIe adapter port enabled
1101 port->cap_adap + ADP_PCIE_CS_0, 1)) in tb_pci_port_is_enabled()
1108 * tb_pci_port_enable() - Enable PCIe adapter port
1115 if (!port->cap_adap) in tb_pci_port_enable()
1116 return -ENXIO; in tb_pci_port_enable()
1118 port->cap_adap + ADP_PCIE_CS_0, 1); in tb_pci_port_enable()
1122 * tb_dp_port_hpd_is_active() - Is HPD already active
1133 port->cap_adap + ADP_DP_CS_2, 1); in tb_dp_port_hpd_is_active()
1141 * tb_dp_port_hpd_clear() - Clear HPD from DP IN port
1152 port->cap_adap + ADP_DP_CS_3, 1); in tb_dp_port_hpd_clear()
1158 port->cap_adap + ADP_DP_CS_3, 1); in tb_dp_port_hpd_clear()
1162 * tb_dp_port_set_hops() - Set video/aux Hop IDs for DP port
1177 port->cap_adap + ADP_DP_CS_0, ARRAY_SIZE(data)); in tb_dp_port_set_hops()
1192 port->cap_adap + ADP_DP_CS_0, ARRAY_SIZE(data)); in tb_dp_port_set_hops()
1196 * tb_dp_port_is_enabled() - Is DP adapter port enabled
1203 if (tb_port_read(port, data, TB_CFG_PORT, port->cap_adap + ADP_DP_CS_0, in tb_dp_port_is_enabled()
1211 * tb_dp_port_enable() - Enables/disables DP paths of a port
1224 port->cap_adap + ADP_DP_CS_0, ARRAY_SIZE(data)); in tb_dp_port_enable()
1234 port->cap_adap + ADP_DP_CS_0, ARRAY_SIZE(data)); in tb_dp_port_enable()
1239 static const char *tb_switch_generation_name(const struct tb_switch *sw) in tb_switch_generation_name() argument
1241 switch (sw->generation) { in tb_switch_generation_name()
1255 static void tb_dump_switch(const struct tb *tb, const struct tb_switch *sw) in tb_dump_switch() argument
1257 const struct tb_regs_switch_header *regs = &sw->config; in tb_dump_switch()
1260 tb_switch_generation_name(sw), regs->vendor_id, regs->device_id, in tb_dump_switch()
1261 regs->revision, regs->thunderbolt_version); in tb_dump_switch()
1262 tb_dbg(tb, " Max Port Number: %d\n", regs->max_port_number); in tb_dump_switch()
1266 regs->upstream_port_number, regs->depth, in tb_dump_switch()
1267 (((u64) regs->route_hi) << 32) | regs->route_lo, in tb_dump_switch()
1268 regs->enabled, regs->plug_events_delay); in tb_dump_switch()
1270 regs->__unknown1, regs->__unknown4); in tb_dump_switch()
1274 * reset_switch() - reconfigure route, enable and send TB_CFG_PKG_RESET
1275 * @sw: Switch to reset
1279 int tb_switch_reset(struct tb_switch *sw) in tb_switch_reset() argument
1283 if (sw->generation > 1) in tb_switch_reset()
1286 tb_sw_dbg(sw, "resetting switch\n"); in tb_switch_reset()
1288 res.err = tb_sw_write(sw, ((u32 *) &sw->config) + 2, in tb_switch_reset()
1292 res = tb_cfg_reset(sw->tb->ctl, tb_route(sw), TB_CFG_DEFAULT_TIMEOUT); in tb_switch_reset()
1294 return -EIO; in tb_switch_reset()
1299 * tb_plug_events_active() - enable/disable plug events on a switch
1305 static int tb_plug_events_active(struct tb_switch *sw, bool active) in tb_plug_events_active() argument
1310 if (tb_switch_is_icm(sw) || tb_switch_is_usb4(sw)) in tb_plug_events_active()
1313 sw->config.plug_events_delay = 0xff; in tb_plug_events_active()
1314 res = tb_sw_write(sw, ((u32 *) &sw->config) + 4, TB_CFG_SWITCH, 4, 1); in tb_plug_events_active()
1318 res = tb_sw_read(sw, &data, TB_CFG_SWITCH, sw->cap_plug_events + 1, 1); in tb_plug_events_active()
1324 switch (sw->config.device_id) { in tb_plug_events_active()
1335 return tb_sw_write(sw, &data, TB_CFG_SWITCH, in tb_plug_events_active()
1336 sw->cap_plug_events + 1, 1); in tb_plug_events_active()
1343 struct tb_switch *sw = tb_to_switch(dev); in authorized_show() local
1345 return sprintf(buf, "%u\n", sw->authorized); in authorized_show()
1348 static int tb_switch_set_authorized(struct tb_switch *sw, unsigned int val) in tb_switch_set_authorized() argument
1350 int ret = -EINVAL; in tb_switch_set_authorized()
1352 if (!mutex_trylock(&sw->tb->lock)) in tb_switch_set_authorized()
1355 if (sw->authorized) in tb_switch_set_authorized()
1361 if (sw->key) in tb_switch_set_authorized()
1362 ret = tb_domain_approve_switch_key(sw->tb, sw); in tb_switch_set_authorized()
1364 ret = tb_domain_approve_switch(sw->tb, sw); in tb_switch_set_authorized()
1369 if (sw->key) in tb_switch_set_authorized()
1370 ret = tb_domain_challenge_switch_key(sw->tb, sw); in tb_switch_set_authorized()
1378 sw->authorized = val; in tb_switch_set_authorized()
1380 kobject_uevent(&sw->dev.kobj, KOBJ_CHANGE); in tb_switch_set_authorized()
1384 mutex_unlock(&sw->tb->lock); in tb_switch_set_authorized()
1392 struct tb_switch *sw = tb_to_switch(dev); in authorized_store() local
1400 return -EINVAL; in authorized_store()
1402 pm_runtime_get_sync(&sw->dev); in authorized_store()
1403 ret = tb_switch_set_authorized(sw, val); in authorized_store()
1404 pm_runtime_mark_last_busy(&sw->dev); in authorized_store()
1405 pm_runtime_put_autosuspend(&sw->dev); in authorized_store()
1414 struct tb_switch *sw = tb_to_switch(dev); in boot_show() local
1416 return sprintf(buf, "%u\n", sw->boot); in boot_show()
1423 struct tb_switch *sw = tb_to_switch(dev); in device_show() local
1425 return sprintf(buf, "%#x\n", sw->device); in device_show()
1432 struct tb_switch *sw = tb_to_switch(dev); in device_name_show() local
1434 return sprintf(buf, "%s\n", sw->device_name ? sw->device_name : ""); in device_name_show()
1441 struct tb_switch *sw = tb_to_switch(dev); in generation_show() local
1443 return sprintf(buf, "%u\n", sw->generation); in generation_show()
1450 struct tb_switch *sw = tb_to_switch(dev); in key_show() local
1453 if (!mutex_trylock(&sw->tb->lock)) in key_show()
1456 if (sw->key) in key_show()
1457 ret = sprintf(buf, "%*phN\n", TB_SWITCH_KEY_SIZE, sw->key); in key_show()
1461 mutex_unlock(&sw->tb->lock); in key_show()
1468 struct tb_switch *sw = tb_to_switch(dev); in key_store() local
1476 return -EINVAL; in key_store()
1478 if (!mutex_trylock(&sw->tb->lock)) in key_store()
1481 if (sw->authorized) { in key_store()
1482 ret = -EBUSY; in key_store()
1484 kfree(sw->key); in key_store()
1486 sw->key = NULL; in key_store()
1488 sw->key = kmemdup(key, sizeof(key), GFP_KERNEL); in key_store()
1489 if (!sw->key) in key_store()
1490 ret = -ENOMEM; in key_store()
1494 mutex_unlock(&sw->tb->lock); in key_store()
1502 struct tb_switch *sw = tb_to_switch(dev); in speed_show() local
1504 return sprintf(buf, "%u.0 Gb/s\n", sw->link_speed); in speed_show()
1517 struct tb_switch *sw = tb_to_switch(dev); in lanes_show() local
1519 return sprintf(buf, "%u\n", sw->link_width); in lanes_show()
1532 struct tb_switch *sw = tb_to_switch(dev); in nvm_authenticate_show() local
1535 nvm_get_auth_status(sw, &status); in nvm_authenticate_show()
1542 struct tb_switch *sw = tb_to_switch(dev); in nvm_authenticate_sysfs() local
1546 pm_runtime_get_sync(&sw->dev); in nvm_authenticate_sysfs()
1548 if (!mutex_trylock(&sw->tb->lock)) { in nvm_authenticate_sysfs()
1554 if (!sw->nvm) { in nvm_authenticate_sysfs()
1555 ret = -EAGAIN; in nvm_authenticate_sysfs()
1564 nvm_clear_auth_status(sw); in nvm_authenticate_sysfs()
1567 if (!sw->nvm->flushed) { in nvm_authenticate_sysfs()
1568 if (!sw->nvm->buf) { in nvm_authenticate_sysfs()
1569 ret = -EINVAL; in nvm_authenticate_sysfs()
1573 ret = nvm_validate_and_write(sw); in nvm_authenticate_sysfs()
1579 ret = tb_lc_force_power(sw); in nvm_authenticate_sysfs()
1581 sw->nvm->authenticating = true; in nvm_authenticate_sysfs()
1582 ret = nvm_authenticate(sw); in nvm_authenticate_sysfs()
1588 mutex_unlock(&sw->tb->lock); in nvm_authenticate_sysfs()
1590 pm_runtime_mark_last_busy(&sw->dev); in nvm_authenticate_sysfs()
1591 pm_runtime_put_autosuspend(&sw->dev); in nvm_authenticate_sysfs()
1625 struct tb_switch *sw = tb_to_switch(dev); in nvm_version_show() local
1628 if (!mutex_trylock(&sw->tb->lock)) in nvm_version_show()
1631 if (sw->safe_mode) in nvm_version_show()
1632 ret = -ENODATA; in nvm_version_show()
1633 else if (!sw->nvm) in nvm_version_show()
1634 ret = -EAGAIN; in nvm_version_show()
1636 ret = sprintf(buf, "%x.%x\n", sw->nvm->major, sw->nvm->minor); in nvm_version_show()
1638 mutex_unlock(&sw->tb->lock); in nvm_version_show()
1647 struct tb_switch *sw = tb_to_switch(dev); in vendor_show() local
1649 return sprintf(buf, "%#x\n", sw->vendor); in vendor_show()
1656 struct tb_switch *sw = tb_to_switch(dev); in vendor_name_show() local
1658 return sprintf(buf, "%s\n", sw->vendor_name ? sw->vendor_name : ""); in vendor_name_show()
1665 struct tb_switch *sw = tb_to_switch(dev); in unique_id_show() local
1667 return sprintf(buf, "%pUb\n", sw->uuid); in unique_id_show()
1695 struct tb_switch *sw = tb_to_switch(dev); in switch_attr_is_visible() local
1698 if (!sw->device) in switch_attr_is_visible()
1701 if (!sw->device_name) in switch_attr_is_visible()
1704 if (!sw->vendor) in switch_attr_is_visible()
1707 if (!sw->vendor_name) in switch_attr_is_visible()
1710 if (tb_route(sw) && in switch_attr_is_visible()
1711 sw->tb->security_level == TB_SECURITY_SECURE && in switch_attr_is_visible()
1712 sw->security_level == TB_SECURITY_SECURE) in switch_attr_is_visible()
1713 return attr->mode; in switch_attr_is_visible()
1719 if (tb_route(sw)) in switch_attr_is_visible()
1720 return attr->mode; in switch_attr_is_visible()
1723 if (nvm_upgradeable(sw)) in switch_attr_is_visible()
1724 return attr->mode; in switch_attr_is_visible()
1727 if (nvm_readable(sw)) in switch_attr_is_visible()
1728 return attr->mode; in switch_attr_is_visible()
1731 if (tb_route(sw)) in switch_attr_is_visible()
1732 return attr->mode; in switch_attr_is_visible()
1735 if (sw->quirks & QUIRK_FORCE_POWER_LINK_CONTROLLER) in switch_attr_is_visible()
1736 return attr->mode; in switch_attr_is_visible()
1740 return sw->safe_mode ? 0 : attr->mode; in switch_attr_is_visible()
1755 struct tb_switch *sw = tb_to_switch(dev); in tb_switch_release() local
1758 dma_port_free(sw->dma_port); in tb_switch_release()
1760 tb_switch_for_each_port(sw, port) { in tb_switch_release()
1761 ida_destroy(&port->in_hopids); in tb_switch_release()
1762 ida_destroy(&port->out_hopids); in tb_switch_release()
1765 kfree(sw->uuid); in tb_switch_release()
1766 kfree(sw->device_name); in tb_switch_release()
1767 kfree(sw->vendor_name); in tb_switch_release()
1768 kfree(sw->ports); in tb_switch_release()
1769 kfree(sw->drom); in tb_switch_release()
1770 kfree(sw->key); in tb_switch_release()
1771 kfree(sw); in tb_switch_release()
1780 struct tb_switch *sw = tb_to_switch(dev); in tb_switch_runtime_suspend() local
1781 const struct tb_cm_ops *cm_ops = sw->tb->cm_ops; in tb_switch_runtime_suspend()
1783 if (cm_ops->runtime_suspend_switch) in tb_switch_runtime_suspend()
1784 return cm_ops->runtime_suspend_switch(sw); in tb_switch_runtime_suspend()
1791 struct tb_switch *sw = tb_to_switch(dev); in tb_switch_runtime_resume() local
1792 const struct tb_cm_ops *cm_ops = sw->tb->cm_ops; in tb_switch_runtime_resume()
1794 if (cm_ops->runtime_resume_switch) in tb_switch_runtime_resume()
1795 return cm_ops->runtime_resume_switch(sw); in tb_switch_runtime_resume()
1810 static int tb_switch_get_generation(struct tb_switch *sw) in tb_switch_get_generation() argument
1812 switch (sw->config.device_id) { in tb_switch_get_generation()
1841 if (tb_switch_is_usb4(sw)) in tb_switch_get_generation()
1848 tb_sw_warn(sw, "unsupported switch device id %#x\n", in tb_switch_get_generation()
1849 sw->config.device_id); in tb_switch_get_generation()
1854 static bool tb_switch_exceeds_max_depth(const struct tb_switch *sw, int depth) in tb_switch_exceeds_max_depth() argument
1858 if (tb_switch_is_usb4(sw) || in tb_switch_exceeds_max_depth()
1859 (sw->tb->root_switch && tb_switch_is_usb4(sw->tb->root_switch))) in tb_switch_exceeds_max_depth()
1868 * tb_switch_alloc() - allocate a switch
1884 struct tb_switch *sw; in tb_switch_alloc() local
1899 upstream_port = tb_cfg_get_upstream_port(tb->ctl, route); in tb_switch_alloc()
1903 sw = kzalloc(sizeof(*sw), GFP_KERNEL); in tb_switch_alloc()
1904 if (!sw) in tb_switch_alloc()
1905 return ERR_PTR(-ENOMEM); in tb_switch_alloc()
1907 sw->tb = tb; in tb_switch_alloc()
1908 ret = tb_cfg_read(tb->ctl, &sw->config, route, 0, TB_CFG_SWITCH, 0, 5); in tb_switch_alloc()
1912 sw->generation = tb_switch_get_generation(sw); in tb_switch_alloc()
1915 tb_dump_switch(tb, sw); in tb_switch_alloc()
1918 sw->config.upstream_port_number = upstream_port; in tb_switch_alloc()
1919 sw->config.depth = depth; in tb_switch_alloc()
1920 sw->config.route_hi = upper_32_bits(route); in tb_switch_alloc()
1921 sw->config.route_lo = lower_32_bits(route); in tb_switch_alloc()
1922 sw->config.enabled = 0; in tb_switch_alloc()
1925 if (tb_switch_exceeds_max_depth(sw, depth)) { in tb_switch_alloc()
1926 ret = -EADDRNOTAVAIL; in tb_switch_alloc()
1931 sw->ports = kcalloc(sw->config.max_port_number + 1, sizeof(*sw->ports), in tb_switch_alloc()
1933 if (!sw->ports) { in tb_switch_alloc()
1934 ret = -ENOMEM; in tb_switch_alloc()
1938 for (i = 0; i <= sw->config.max_port_number; i++) { in tb_switch_alloc()
1940 sw->ports[i].sw = sw; in tb_switch_alloc()
1941 sw->ports[i].port = i; in tb_switch_alloc()
1945 ida_init(&sw->ports[i].in_hopids); in tb_switch_alloc()
1946 ida_init(&sw->ports[i].out_hopids); in tb_switch_alloc()
1950 ret = tb_switch_find_vse_cap(sw, TB_VSE_CAP_PLUG_EVENTS); in tb_switch_alloc()
1952 sw->cap_plug_events = ret; in tb_switch_alloc()
1954 ret = tb_switch_find_vse_cap(sw, TB_VSE_CAP_LINK_CONTROLLER); in tb_switch_alloc()
1956 sw->cap_lc = ret; in tb_switch_alloc()
1960 sw->authorized = true; in tb_switch_alloc()
1962 device_initialize(&sw->dev); in tb_switch_alloc()
1963 sw->dev.parent = parent; in tb_switch_alloc()
1964 sw->dev.bus = &tb_bus_type; in tb_switch_alloc()
1965 sw->dev.type = &tb_switch_type; in tb_switch_alloc()
1966 sw->dev.groups = switch_groups; in tb_switch_alloc()
1967 dev_set_name(&sw->dev, "%u-%llx", tb->index, tb_route(sw)); in tb_switch_alloc()
1969 return sw; in tb_switch_alloc()
1972 kfree(sw->ports); in tb_switch_alloc()
1973 kfree(sw); in tb_switch_alloc()
1979 * tb_switch_alloc_safe_mode() - allocate a switch that is in safe mode
1984 * This creates a switch in safe mode. This means the switch pretty much
1995 struct tb_switch *sw; in tb_switch_alloc_safe_mode() local
1997 sw = kzalloc(sizeof(*sw), GFP_KERNEL); in tb_switch_alloc_safe_mode()
1998 if (!sw) in tb_switch_alloc_safe_mode()
1999 return ERR_PTR(-ENOMEM); in tb_switch_alloc_safe_mode()
2001 sw->tb = tb; in tb_switch_alloc_safe_mode()
2002 sw->config.depth = tb_route_length(route); in tb_switch_alloc_safe_mode()
2003 sw->config.route_hi = upper_32_bits(route); in tb_switch_alloc_safe_mode()
2004 sw->config.route_lo = lower_32_bits(route); in tb_switch_alloc_safe_mode()
2005 sw->safe_mode = true; in tb_switch_alloc_safe_mode()
2007 device_initialize(&sw->dev); in tb_switch_alloc_safe_mode()
2008 sw->dev.parent = parent; in tb_switch_alloc_safe_mode()
2009 sw->dev.bus = &tb_bus_type; in tb_switch_alloc_safe_mode()
2010 sw->dev.type = &tb_switch_type; in tb_switch_alloc_safe_mode()
2011 sw->dev.groups = switch_groups; in tb_switch_alloc_safe_mode()
2012 dev_set_name(&sw->dev, "%u-%llx", tb->index, tb_route(sw)); in tb_switch_alloc_safe_mode()
2014 return sw; in tb_switch_alloc_safe_mode()
2018 * tb_switch_configure() - Uploads configuration to the switch
2019 * @sw: Switch to configure
2024 * resume from low power states to re-initialize it.
2028 int tb_switch_configure(struct tb_switch *sw) in tb_switch_configure() argument
2030 struct tb *tb = sw->tb; in tb_switch_configure()
2034 route = tb_route(sw); in tb_switch_configure()
2037 sw->config.enabled ? "restoring" : "initializing", route, in tb_switch_configure()
2038 tb_route_length(route), sw->config.upstream_port_number); in tb_switch_configure()
2040 sw->config.enabled = 1; in tb_switch_configure()
2042 if (tb_switch_is_usb4(sw)) { in tb_switch_configure()
2048 sw->config.cmuv = USB4_VERSION_1_0; in tb_switch_configure()
2049 sw->config.plug_events_delay = 0xa; in tb_switch_configure()
2052 ret = tb_sw_write(sw, (u32 *)&sw->config + 1, TB_CFG_SWITCH, in tb_switch_configure()
2057 ret = usb4_switch_setup(sw); in tb_switch_configure()
2059 if (sw->config.vendor_id != PCI_VENDOR_ID_INTEL) in tb_switch_configure()
2060 tb_sw_warn(sw, "unknown switch vendor id %#x\n", in tb_switch_configure()
2061 sw->config.vendor_id); in tb_switch_configure()
2063 if (!sw->cap_plug_events) { in tb_switch_configure()
2064 tb_sw_warn(sw, "cannot find TB_VSE_CAP_PLUG_EVENTS aborting\n"); in tb_switch_configure()
2065 return -ENODEV; in tb_switch_configure()
2069 ret = tb_sw_write(sw, (u32 *)&sw->config + 1, TB_CFG_SWITCH, in tb_switch_configure()
2075 return tb_plug_events_active(sw, true); in tb_switch_configure()
2078 static int tb_switch_set_uuid(struct tb_switch *sw) in tb_switch_set_uuid() argument
2084 if (sw->uuid) in tb_switch_set_uuid()
2087 if (tb_switch_is_usb4(sw)) { in tb_switch_set_uuid()
2088 ret = usb4_switch_read_uid(sw, &sw->uid); in tb_switch_set_uuid()
2097 ret = tb_lc_read_uuid(sw, uuid); in tb_switch_set_uuid()
2099 if (ret != -EINVAL) in tb_switch_set_uuid()
2112 uuid[0] = sw->uid & 0xffffffff; in tb_switch_set_uuid()
2113 uuid[1] = (sw->uid >> 32) & 0xffffffff; in tb_switch_set_uuid()
2118 sw->uuid = kmemdup(uuid, sizeof(uuid), GFP_KERNEL); in tb_switch_set_uuid()
2119 if (!sw->uuid) in tb_switch_set_uuid()
2120 return -ENOMEM; in tb_switch_set_uuid()
2124 static int tb_switch_add_dma_port(struct tb_switch *sw) in tb_switch_add_dma_port() argument
2129 switch (sw->generation) { in tb_switch_add_dma_port()
2132 if (tb_route(sw)) in tb_switch_add_dma_port()
2137 ret = tb_switch_set_uuid(sw); in tb_switch_add_dma_port()
2145 * is in safe mode. in tb_switch_add_dma_port()
2147 if (!sw->safe_mode) in tb_switch_add_dma_port()
2153 if (!tb_route(sw) && !tb_switch_is_icm(sw)) in tb_switch_add_dma_port()
2156 sw->dma_port = dma_port_alloc(sw); in tb_switch_add_dma_port()
2157 if (!sw->dma_port) in tb_switch_add_dma_port()
2160 if (sw->no_nvm_upgrade) in tb_switch_add_dma_port()
2169 nvm_get_auth_status(sw, &status); in tb_switch_add_dma_port()
2171 if (!tb_route(sw)) in tb_switch_add_dma_port()
2172 nvm_authenticate_complete_dma_port(sw); in tb_switch_add_dma_port()
2181 ret = dma_port_flash_update_auth_status(sw->dma_port, &status); in tb_switch_add_dma_port()
2186 if (!tb_route(sw)) in tb_switch_add_dma_port()
2187 nvm_authenticate_complete_dma_port(sw); in tb_switch_add_dma_port()
2190 tb_sw_info(sw, "switch flash authentication failed\n"); in tb_switch_add_dma_port()
2191 nvm_set_auth_status(sw, status); in tb_switch_add_dma_port()
2194 tb_sw_info(sw, "power cycling the switch now\n"); in tb_switch_add_dma_port()
2195 dma_port_power_cycle(sw->dma_port); in tb_switch_add_dma_port()
2201 return -ESHUTDOWN; in tb_switch_add_dma_port()
2204 static void tb_switch_default_link_ports(struct tb_switch *sw) in tb_switch_default_link_ports() argument
2208 for (i = 1; i <= sw->config.max_port_number; i++) { in tb_switch_default_link_ports()
2209 struct tb_port *port = &sw->ports[i]; in tb_switch_default_link_ports()
2216 if (i == sw->config.max_port_number || in tb_switch_default_link_ports()
2217 !tb_port_is_null(&sw->ports[i + 1])) in tb_switch_default_link_ports()
2221 subordinate = &sw->ports[i + 1]; in tb_switch_default_link_ports()
2222 if (!port->dual_link_port && !subordinate->dual_link_port) { in tb_switch_default_link_ports()
2223 port->link_nr = 0; in tb_switch_default_link_ports()
2224 port->dual_link_port = subordinate; in tb_switch_default_link_ports()
2225 subordinate->link_nr = 1; in tb_switch_default_link_ports()
2226 subordinate->dual_link_port = port; in tb_switch_default_link_ports()
2228 tb_sw_dbg(sw, "linked ports %d <-> %d\n", in tb_switch_default_link_ports()
2229 port->port, subordinate->port); in tb_switch_default_link_ports()
2234 static bool tb_switch_lane_bonding_possible(struct tb_switch *sw) in tb_switch_lane_bonding_possible() argument
2236 const struct tb_port *up = tb_upstream_port(sw); in tb_switch_lane_bonding_possible()
2238 if (!up->dual_link_port || !up->dual_link_port->remote) in tb_switch_lane_bonding_possible()
2241 if (tb_switch_is_usb4(sw)) in tb_switch_lane_bonding_possible()
2242 return usb4_switch_lane_bonding_possible(sw); in tb_switch_lane_bonding_possible()
2243 return tb_lc_lane_bonding_possible(sw); in tb_switch_lane_bonding_possible()
2246 static int tb_switch_update_link_attributes(struct tb_switch *sw) in tb_switch_update_link_attributes() argument
2252 if (!tb_route(sw) || tb_switch_is_icm(sw)) in tb_switch_update_link_attributes()
2255 up = tb_upstream_port(sw); in tb_switch_update_link_attributes()
2260 if (sw->link_speed != ret) in tb_switch_update_link_attributes()
2262 sw->link_speed = ret; in tb_switch_update_link_attributes()
2267 if (sw->link_width != ret) in tb_switch_update_link_attributes()
2269 sw->link_width = ret; in tb_switch_update_link_attributes()
2272 if (device_is_registered(&sw->dev) && change) in tb_switch_update_link_attributes()
2273 kobject_uevent(&sw->dev.kobj, KOBJ_CHANGE); in tb_switch_update_link_attributes()
2279 * tb_switch_lane_bonding_enable() - Enable lane bonding
2280 * @sw: Switch to enable lane bonding
2286 int tb_switch_lane_bonding_enable(struct tb_switch *sw) in tb_switch_lane_bonding_enable() argument
2288 struct tb_switch *parent = tb_to_switch(sw->dev.parent); in tb_switch_lane_bonding_enable()
2290 u64 route = tb_route(sw); in tb_switch_lane_bonding_enable()
2296 if (!tb_switch_lane_bonding_possible(sw)) in tb_switch_lane_bonding_enable()
2299 up = tb_upstream_port(sw); in tb_switch_lane_bonding_enable()
2310 if (tb_wait_for_port(down->dual_link_port, false) <= 0) in tb_switch_lane_bonding_enable()
2311 return -ENOTCONN; in tb_switch_lane_bonding_enable()
2326 tb_switch_update_link_attributes(sw); in tb_switch_lane_bonding_enable()
2328 tb_sw_dbg(sw, "lane bonding enabled\n"); in tb_switch_lane_bonding_enable()
2333 * tb_switch_lane_bonding_disable() - Disable lane bonding
2334 * @sw: Switch whose lane bonding to disable
2336 * Disables lane bonding between @sw and parent. This can be called even
2339 void tb_switch_lane_bonding_disable(struct tb_switch *sw) in tb_switch_lane_bonding_disable() argument
2341 struct tb_switch *parent = tb_to_switch(sw->dev.parent); in tb_switch_lane_bonding_disable()
2344 if (!tb_route(sw)) in tb_switch_lane_bonding_disable()
2347 up = tb_upstream_port(sw); in tb_switch_lane_bonding_disable()
2348 if (!up->bonded) in tb_switch_lane_bonding_disable()
2351 down = tb_port_at(tb_route(sw), parent); in tb_switch_lane_bonding_disable()
2356 tb_switch_update_link_attributes(sw); in tb_switch_lane_bonding_disable()
2357 tb_sw_dbg(sw, "lane bonding disabled\n"); in tb_switch_lane_bonding_disable()
2361 * tb_switch_configure_link() - Set link configured
2362 * @sw: Switch whose link is configured
2364 * Sets the link upstream from @sw configured (from both ends) so that
2372 int tb_switch_configure_link(struct tb_switch *sw) in tb_switch_configure_link() argument
2377 if (!tb_route(sw) || tb_switch_is_icm(sw)) in tb_switch_configure_link()
2380 up = tb_upstream_port(sw); in tb_switch_configure_link()
2381 if (tb_switch_is_usb4(up->sw)) in tb_switch_configure_link()
2388 down = up->remote; in tb_switch_configure_link()
2389 if (tb_switch_is_usb4(down->sw)) in tb_switch_configure_link()
2395 * tb_switch_unconfigure_link() - Unconfigure link
2396 * @sw: Switch whose link is unconfigured
2398 * Sets the link unconfigured so the @sw will be disconnected if the
2401 void tb_switch_unconfigure_link(struct tb_switch *sw) in tb_switch_unconfigure_link() argument
2405 if (sw->is_unplugged) in tb_switch_unconfigure_link()
2407 if (!tb_route(sw) || tb_switch_is_icm(sw)) in tb_switch_unconfigure_link()
2410 up = tb_upstream_port(sw); in tb_switch_unconfigure_link()
2411 if (tb_switch_is_usb4(up->sw)) in tb_switch_unconfigure_link()
2416 down = up->remote; in tb_switch_unconfigure_link()
2417 if (tb_switch_is_usb4(down->sw)) in tb_switch_unconfigure_link()
2423 static int tb_switch_port_hotplug_enable(struct tb_switch *sw) in tb_switch_port_hotplug_enable() argument
2427 if (tb_switch_is_icm(sw)) in tb_switch_port_hotplug_enable()
2430 tb_switch_for_each_port(sw, port) { in tb_switch_port_hotplug_enable()
2433 if (!port->cap_usb4) in tb_switch_port_hotplug_enable()
2444 * tb_switch_add() - Add a switch to the domain
2445 * @sw: Switch to add
2455 int tb_switch_add(struct tb_switch *sw) in tb_switch_add() argument
2466 ret = tb_switch_add_dma_port(sw); in tb_switch_add()
2468 dev_err(&sw->dev, "failed to add DMA port\n"); in tb_switch_add()
2472 if (!sw->safe_mode) { in tb_switch_add()
2474 ret = tb_drom_read(sw); in tb_switch_add()
2476 dev_err(&sw->dev, "reading DROM failed\n"); in tb_switch_add()
2479 tb_sw_dbg(sw, "uid: %#llx\n", sw->uid); in tb_switch_add()
2481 ret = tb_switch_set_uuid(sw); in tb_switch_add()
2483 dev_err(&sw->dev, "failed to set UUID\n"); in tb_switch_add()
2487 for (i = 0; i <= sw->config.max_port_number; i++) { in tb_switch_add()
2488 if (sw->ports[i].disabled) { in tb_switch_add()
2489 tb_port_dbg(&sw->ports[i], "disabled by eeprom\n"); in tb_switch_add()
2492 ret = tb_init_port(&sw->ports[i]); in tb_switch_add()
2494 dev_err(&sw->dev, "failed to initialize port %d\n", i); in tb_switch_add()
2499 tb_switch_default_link_ports(sw); in tb_switch_add()
2501 ret = tb_switch_update_link_attributes(sw); in tb_switch_add()
2505 ret = tb_switch_tmu_init(sw); in tb_switch_add()
2510 ret = tb_switch_port_hotplug_enable(sw); in tb_switch_add()
2514 ret = device_add(&sw->dev); in tb_switch_add()
2516 dev_err(&sw->dev, "failed to add device: %d\n", ret); in tb_switch_add()
2520 if (tb_route(sw)) { in tb_switch_add()
2521 dev_info(&sw->dev, "new device found, vendor=%#x device=%#x\n", in tb_switch_add()
2522 sw->vendor, sw->device); in tb_switch_add()
2523 if (sw->vendor_name && sw->device_name) in tb_switch_add()
2524 dev_info(&sw->dev, "%s %s\n", sw->vendor_name, in tb_switch_add()
2525 sw->device_name); in tb_switch_add()
2528 ret = tb_switch_nvm_add(sw); in tb_switch_add()
2530 dev_err(&sw->dev, "failed to add NVM devices\n"); in tb_switch_add()
2531 device_del(&sw->dev); in tb_switch_add()
2540 device_init_wakeup(&sw->dev, true); in tb_switch_add()
2542 pm_runtime_set_active(&sw->dev); in tb_switch_add()
2543 if (sw->rpm) { in tb_switch_add()
2544 pm_runtime_set_autosuspend_delay(&sw->dev, TB_AUTOSUSPEND_DELAY); in tb_switch_add()
2545 pm_runtime_use_autosuspend(&sw->dev); in tb_switch_add()
2546 pm_runtime_mark_last_busy(&sw->dev); in tb_switch_add()
2547 pm_runtime_enable(&sw->dev); in tb_switch_add()
2548 pm_request_autosuspend(&sw->dev); in tb_switch_add()
2551 tb_switch_debugfs_init(sw); in tb_switch_add()
2556 * tb_switch_remove() - Remove and release a switch
2557 * @sw: Switch to remove
2563 void tb_switch_remove(struct tb_switch *sw) in tb_switch_remove() argument
2567 tb_switch_debugfs_remove(sw); in tb_switch_remove()
2569 if (sw->rpm) { in tb_switch_remove()
2570 pm_runtime_get_sync(&sw->dev); in tb_switch_remove()
2571 pm_runtime_disable(&sw->dev); in tb_switch_remove()
2575 tb_switch_for_each_port(sw, port) { in tb_switch_remove()
2577 tb_switch_remove(port->remote->sw); in tb_switch_remove()
2578 port->remote = NULL; in tb_switch_remove()
2579 } else if (port->xdomain) { in tb_switch_remove()
2580 tb_xdomain_remove(port->xdomain); in tb_switch_remove()
2581 port->xdomain = NULL; in tb_switch_remove()
2588 if (!sw->is_unplugged) in tb_switch_remove()
2589 tb_plug_events_active(sw, false); in tb_switch_remove()
2591 tb_switch_nvm_remove(sw); in tb_switch_remove()
2593 if (tb_route(sw)) in tb_switch_remove()
2594 dev_info(&sw->dev, "device disconnected\n"); in tb_switch_remove()
2595 device_unregister(&sw->dev); in tb_switch_remove()
2599 * tb_sw_set_unplugged() - set is_unplugged on switch and downstream switches
2601 void tb_sw_set_unplugged(struct tb_switch *sw) in tb_sw_set_unplugged() argument
2605 if (sw == sw->tb->root_switch) { in tb_sw_set_unplugged()
2606 tb_sw_WARN(sw, "cannot unplug root switch\n"); in tb_sw_set_unplugged()
2609 if (sw->is_unplugged) { in tb_sw_set_unplugged()
2610 tb_sw_WARN(sw, "is_unplugged already set\n"); in tb_sw_set_unplugged()
2613 sw->is_unplugged = true; in tb_sw_set_unplugged()
2614 tb_switch_for_each_port(sw, port) { in tb_sw_set_unplugged()
2616 tb_sw_set_unplugged(port->remote->sw); in tb_sw_set_unplugged()
2617 else if (port->xdomain) in tb_sw_set_unplugged()
2618 port->xdomain->is_unplugged = true; in tb_sw_set_unplugged()
2622 static int tb_switch_set_wake(struct tb_switch *sw, unsigned int flags) in tb_switch_set_wake() argument
2625 tb_sw_dbg(sw, "enabling wakeup: %#x\n", flags); in tb_switch_set_wake()
2627 tb_sw_dbg(sw, "disabling wakeup\n"); in tb_switch_set_wake()
2629 if (tb_switch_is_usb4(sw)) in tb_switch_set_wake()
2630 return usb4_switch_set_wake(sw, flags); in tb_switch_set_wake()
2631 return tb_lc_set_wake(sw, flags); in tb_switch_set_wake()
2634 int tb_switch_resume(struct tb_switch *sw) in tb_switch_resume() argument
2639 tb_sw_dbg(sw, "resuming switch\n"); in tb_switch_resume()
2645 if (tb_route(sw)) { in tb_switch_resume()
2653 err = tb_cfg_get_upstream_port(sw->tb->ctl, tb_route(sw)); in tb_switch_resume()
2655 tb_sw_info(sw, "switch not present anymore\n"); in tb_switch_resume()
2659 if (tb_switch_is_usb4(sw)) in tb_switch_resume()
2660 err = usb4_switch_read_uid(sw, &uid); in tb_switch_resume()
2662 err = tb_drom_read_uid_only(sw, &uid); in tb_switch_resume()
2664 tb_sw_warn(sw, "uid read failed\n"); in tb_switch_resume()
2667 if (sw->uid != uid) { in tb_switch_resume()
2668 tb_sw_info(sw, in tb_switch_resume()
2669 "changed while suspended (uid %#llx -> %#llx)\n", in tb_switch_resume()
2670 sw->uid, uid); in tb_switch_resume()
2671 return -ENODEV; in tb_switch_resume()
2675 err = tb_switch_configure(sw); in tb_switch_resume()
2680 tb_switch_set_wake(sw, 0); in tb_switch_resume()
2682 err = tb_switch_tmu_init(sw); in tb_switch_resume()
2687 tb_switch_for_each_port(sw, port) { in tb_switch_resume()
2688 if (!tb_port_has_remote(port) && !port->xdomain) in tb_switch_resume()
2695 tb_sw_set_unplugged(port->remote->sw); in tb_switch_resume()
2696 else if (port->xdomain) in tb_switch_resume()
2697 port->xdomain->is_unplugged = true; in tb_switch_resume()
2698 } else if (tb_port_has_remote(port) || port->xdomain) { in tb_switch_resume()
2705 if (port->remote && tb_switch_resume(port->remote->sw)) { in tb_switch_resume()
2708 tb_sw_set_unplugged(port->remote->sw); in tb_switch_resume()
2716 * tb_switch_suspend() - Put a switch to sleep
2717 * @sw: Switch to suspend
2721 * value of @runtime and then sets sleep bit for the router. If @sw is
2725 void tb_switch_suspend(struct tb_switch *sw, bool runtime) in tb_switch_suspend() argument
2731 tb_sw_dbg(sw, "suspending switch\n"); in tb_switch_suspend()
2733 err = tb_plug_events_active(sw, false); in tb_switch_suspend()
2737 tb_switch_for_each_port(sw, port) { in tb_switch_suspend()
2739 tb_switch_suspend(port->remote->sw, runtime); in tb_switch_suspend()
2746 } else if (device_may_wakeup(&sw->dev)) { in tb_switch_suspend()
2750 tb_switch_set_wake(sw, flags); in tb_switch_suspend()
2752 if (tb_switch_is_usb4(sw)) in tb_switch_suspend()
2753 usb4_switch_set_sleep(sw); in tb_switch_suspend()
2755 tb_lc_set_sleep(sw); in tb_switch_suspend()
2759 * tb_switch_query_dp_resource() - Query availability of DP resource
2760 * @sw: Switch whose DP resource is queried
2766 bool tb_switch_query_dp_resource(struct tb_switch *sw, struct tb_port *in) in tb_switch_query_dp_resource() argument
2768 if (tb_switch_is_usb4(sw)) in tb_switch_query_dp_resource()
2769 return usb4_switch_query_dp_resource(sw, in); in tb_switch_query_dp_resource()
2770 return tb_lc_dp_sink_query(sw, in); in tb_switch_query_dp_resource()
2774 * tb_switch_alloc_dp_resource() - Allocate available DP resource
2775 * @sw: Switch whose DP resource is allocated
2782 int tb_switch_alloc_dp_resource(struct tb_switch *sw, struct tb_port *in) in tb_switch_alloc_dp_resource() argument
2784 if (tb_switch_is_usb4(sw)) in tb_switch_alloc_dp_resource()
2785 return usb4_switch_alloc_dp_resource(sw, in); in tb_switch_alloc_dp_resource()
2786 return tb_lc_dp_sink_alloc(sw, in); in tb_switch_alloc_dp_resource()
2790 * tb_switch_dealloc_dp_resource() - De-allocate DP resource
2791 * @sw: Switch whose DP resource is de-allocated
2794 * De-allocates DP resource that was previously allocated for DP
2797 void tb_switch_dealloc_dp_resource(struct tb_switch *sw, struct tb_port *in) in tb_switch_dealloc_dp_resource() argument
2801 if (tb_switch_is_usb4(sw)) in tb_switch_dealloc_dp_resource()
2802 ret = usb4_switch_dealloc_dp_resource(sw, in); in tb_switch_dealloc_dp_resource()
2804 ret = tb_lc_dp_sink_dealloc(sw, in); in tb_switch_dealloc_dp_resource()
2807 tb_sw_warn(sw, "failed to de-allocate DP resource for port %d\n", in tb_switch_dealloc_dp_resource()
2808 in->port); in tb_switch_dealloc_dp_resource()
2821 struct tb_switch *sw = tb_to_switch(dev); in tb_switch_match() local
2824 if (!sw) in tb_switch_match()
2826 if (sw->tb != lookup->tb) in tb_switch_match()
2829 if (lookup->uuid) in tb_switch_match()
2830 return !memcmp(sw->uuid, lookup->uuid, sizeof(*lookup->uuid)); in tb_switch_match()
2832 if (lookup->route) { in tb_switch_match()
2833 return sw->config.route_lo == lower_32_bits(lookup->route) && in tb_switch_match()
2834 sw->config.route_hi == upper_32_bits(lookup->route); in tb_switch_match()
2838 if (!lookup->depth) in tb_switch_match()
2839 return !sw->depth; in tb_switch_match()
2841 return sw->link == lookup->link && sw->depth == lookup->depth; in tb_switch_match()
2845 * tb_switch_find_by_link_depth() - Find switch by link and depth
2871 * tb_switch_find_by_uuid() - Find switch by UUID
2895 * tb_switch_find_by_route() - Find switch by route string
2908 return tb_switch_get(tb->root_switch); in tb_switch_find_by_route()
2922 * tb_switch_find_port() - return the first port of @type on @sw or NULL
2923 * @sw: Switch to find the port from
2926 struct tb_port *tb_switch_find_port(struct tb_switch *sw, in tb_switch_find_port() argument
2931 tb_switch_for_each_port(sw, port) { in tb_switch_find_port()
2932 if (port->config.type == type) in tb_switch_find_port()