Lines Matching +full:dma +full:- +full:router
1 // SPDX-License-Identifier: GPL-2.0
3 * Thunderbolt driver - switch/port utility functions
11 #include <linux/nvmem-provider.h>
42 if (uuid_equal(&st->uuid, sw->uuid)) in __nvm_get_auth_status()
57 *status = st ? st->status : 0; in nvm_get_auth_status()
64 if (WARN_ON(!sw->uuid)) 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()
92 list_del(&st->list); in nvm_clear_auth_status()
101 const u8 *buf = sw->nvm->buf; in nvm_validate_and_write()
106 return -EINVAL; in nvm_validate_and_write()
108 image_size = sw->nvm->buf_data_size; in nvm_validate_and_write()
110 return -EINVAL; in nvm_validate_and_write()
118 return -EINVAL; in nvm_validate_and_write()
122 return -EINVAL; in nvm_validate_and_write()
130 return -EINVAL; in nvm_validate_and_write()
132 if (!sw->safe_mode) { in nvm_validate_and_write()
140 if (device_id != sw->config.device_id) in nvm_validate_and_write()
141 return -EINVAL; in nvm_validate_and_write()
143 if (sw->generation < 3) { in nvm_validate_and_write()
145 ret = dma_port_flash_write(sw->dma_port, in nvm_validate_and_write()
154 image_size -= hdr_size; in nvm_validate_and_write()
160 ret = dma_port_flash_write(sw->dma_port, 0, buf, image_size); in nvm_validate_and_write()
162 sw->nvm->flushed = true; in nvm_validate_and_write()
175 if (!sw->safe_mode) { in nvm_authenticate_host_dma_port()
178 ret = tb_domain_disconnect_all_paths(sw->tb); in nvm_authenticate_host_dma_port()
185 ret = dma_port_flash_update_auth(sw->dma_port); in nvm_authenticate_host_dma_port()
186 if (!ret || ret == -ETIMEDOUT) in nvm_authenticate_host_dma_port()
191 * cycling of the host router. in nvm_authenticate_host_dma_port()
194 if (dma_port_flash_update_auth_status(sw->dma_port, &status) > 0) in nvm_authenticate_host_dma_port()
202 dma_port_power_cycle(sw->dma_port); in nvm_authenticate_host_dma_port()
210 ret = dma_port_flash_update_auth(sw->dma_port); in nvm_authenticate_device_dma_port()
213 case -ETIMEDOUT: in nvm_authenticate_device_dma_port()
214 case -EACCES: in nvm_authenticate_device_dma_port()
215 case -EINVAL: in nvm_authenticate_device_dma_port()
231 ret = dma_port_flash_update_auth_status(sw->dma_port, &status); in nvm_authenticate_device_dma_port()
232 if (ret < 0 && ret != -ETIMEDOUT) in nvm_authenticate_device_dma_port()
241 dma_port_power_cycle(sw->dma_port); in nvm_authenticate_device_dma_port()
246 } while (--retries); in nvm_authenticate_device_dma_port()
248 return -ETIMEDOUT; in nvm_authenticate_device_dma_port()
256 * During host router NVM upgrade we should not allow root port to in nvm_authenticate_start_dma_port()
261 root_port = pcie_find_root_port(sw->tb->nhi->pdev); in nvm_authenticate_start_dma_port()
263 pm_runtime_get_noresume(&root_port->dev); in nvm_authenticate_start_dma_port()
270 root_port = pcie_find_root_port(sw->tb->nhi->pdev); in nvm_authenticate_complete_dma_port()
272 pm_runtime_put(&root_port->dev); in nvm_authenticate_complete_dma_port()
287 /* Thunderbolt 2 and 3 devices support NVM through DMA port */ in nvm_readable()
288 return !!sw->dma_port; in nvm_readable()
293 if (sw->no_nvm_upgrade) in nvm_upgradeable()
303 return dma_port_flash_read(sw->dma_port, address, buf, size); in nvm_read()
316 sw->nvm->authenticating = true; in nvm_authenticate()
319 return -EOPNOTSUPP; in nvm_authenticate()
322 sw->nvm->authenticating = true; in nvm_authenticate()
337 struct tb_switch *sw = tb_to_switch(nvm->dev); in tb_switch_nvm_read()
340 pm_runtime_get_sync(&sw->dev); in tb_switch_nvm_read()
342 if (!mutex_trylock(&sw->tb->lock)) { in tb_switch_nvm_read()
348 mutex_unlock(&sw->tb->lock); in tb_switch_nvm_read()
351 pm_runtime_mark_last_busy(&sw->dev); in tb_switch_nvm_read()
352 pm_runtime_put_autosuspend(&sw->dev); in tb_switch_nvm_read()
361 struct tb_switch *sw = tb_to_switch(nvm->dev); in tb_switch_nvm_write()
364 if (!mutex_trylock(&sw->tb->lock)) in tb_switch_nvm_write()
374 mutex_unlock(&sw->tb->lock); in tb_switch_nvm_write()
389 * The NVM format of non-Intel hardware is not known so in tb_switch_nvm_add()
393 if (sw->config.vendor_id != PCI_VENDOR_ID_INTEL && in tb_switch_nvm_add()
394 sw->config.vendor_id != 0x8087) { in tb_switch_nvm_add()
395 dev_info(&sw->dev, in tb_switch_nvm_add()
397 sw->config.vendor_id); in tb_switch_nvm_add()
401 nvm = tb_nvm_alloc(&sw->dev); in tb_switch_nvm_add()
406 * If the switch is in safe-mode the only accessible portion of in tb_switch_nvm_add()
407 * the NVM is the non-active one where userspace is expected to in tb_switch_nvm_add()
410 if (!sw->safe_mode) { in tb_switch_nvm_add()
417 hdr_size = sw->generation < 3 ? SZ_8K : SZ_16K; in tb_switch_nvm_add()
419 nvm_size = (nvm_size - hdr_size) / 2; in tb_switch_nvm_add()
425 nvm->major = val >> 16; in tb_switch_nvm_add()
426 nvm->minor = val >> 8; in tb_switch_nvm_add()
433 if (!sw->no_nvm_upgrade) { in tb_switch_nvm_add()
440 sw->nvm = nvm; in tb_switch_nvm_add()
452 nvm = sw->nvm; in tb_switch_nvm_remove()
453 sw->nvm = NULL; in tb_switch_nvm_remove()
459 if (!nvm->authenticating) in tb_switch_nvm_remove()
469 switch (port->type >> 16) { in tb_port_type()
471 switch ((u8) port->type) { in tb_port_type()
498 const struct tb_regs_port_header *regs = &port->config; in tb_dump_port()
502 regs->port_number, regs->vendor_id, regs->device_id, in tb_dump_port()
503 regs->revision, regs->thunderbolt_version, tb_port_type(regs), in tb_dump_port()
504 regs->type); in tb_dump_port()
506 regs->max_in_hop_id, regs->max_out_hop_id); in tb_dump_port()
507 tb_dbg(tb, " Max counters: %d\n", regs->max_counters); in tb_dump_port()
508 tb_dbg(tb, " NFC Credits: %#x\n", regs->nfc_credits); in tb_dump_port()
509 tb_dbg(tb, " Credits (total/control): %u/%u\n", port->total_credits, in tb_dump_port()
510 port->ctl_credits); in tb_dump_port()
514 * tb_port_state() - get connectedness state of a port
525 if (port->cap_phy == 0) { in tb_port_state()
527 return -EINVAL; in tb_port_state()
529 res = tb_port_read(port, &phy, TB_CFG_PORT, port->cap_phy, 2); in tb_port_state()
536 * tb_wait_for_port() - wait for a port to become ready
554 if (!port->cap_phy) { in tb_wait_for_port()
556 return -EINVAL; in tb_wait_for_port()
560 return -EINVAL; in tb_wait_for_port()
563 while (retries--) { in tb_wait_for_port()
588 * After plug-in the state is TB_PORT_CONNECTING. Give it some in tb_wait_for_port()
602 * tb_port_add_nfc_credits() - add/remove non flow controlled credits to port
615 if (credits == 0 || port->sw->is_unplugged) in tb_port_add_nfc_credits()
622 if (tb_switch_is_usb4(port->sw) && !tb_port_is_null(port)) in tb_port_add_nfc_credits()
625 nfc_credits = port->config.nfc_credits & ADP_CS_4_NFC_BUFFERS_MASK; in tb_port_add_nfc_credits()
629 port->config.nfc_credits & ADP_CS_4_NFC_BUFFERS_MASK); in tb_port_add_nfc_credits()
631 port->config.nfc_credits &= ~ADP_CS_4_NFC_BUFFERS_MASK; in tb_port_add_nfc_credits()
632 port->config.nfc_credits |= nfc_credits; in tb_port_add_nfc_credits()
634 return tb_port_write(port, &port->config.nfc_credits, in tb_port_add_nfc_credits()
639 * tb_port_clear_counter() - clear a counter in TB_CFG_COUNTER
653 * tb_port_unlock() - Unlock downstream port
657 * downstream router accessible for CM.
661 if (tb_switch_is_icm(port->sw)) in tb_port_unlock()
664 return -EINVAL; in tb_port_unlock()
665 if (tb_switch_is_usb4(port->sw)) in tb_port_unlock()
676 return -EINVAL; in __tb_port_enable()
679 port->cap_phy + LANE_ADP_CS_1, 1); in __tb_port_enable()
689 port->cap_phy + LANE_ADP_CS_1, 1); in __tb_port_enable()
693 * tb_port_enable() - Enable lane adapter
704 * tb_port_disable() - Disable lane adapter
715 * tb_init_port() - initialize a port
727 INIT_LIST_HEAD(&port->list); in tb_init_port()
730 if (!port->port) in tb_init_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) { in tb_init_port()
749 port->cap_phy = cap; in tb_init_port()
755 port->cap_usb4 = cap; in tb_init_port()
760 * devices we use hard-coded value. in tb_init_port()
762 if (tb_switch_is_usb4(port->sw)) { in tb_init_port()
766 port->ctl_credits = hop.initial_credits; in tb_init_port()
768 if (!port->ctl_credits) in tb_init_port()
769 port->ctl_credits = 2; in tb_init_port()
774 port->cap_adap = cap; in tb_init_port()
777 port->total_credits = in tb_init_port()
778 (port->config.nfc_credits & ADP_CS_4_TOTAL_BUFFERS_MASK) >> in tb_init_port()
781 tb_dump_port(port->sw->tb, port); in tb_init_port()
792 port_max_hopid = port->config.max_in_hop_id; in tb_port_alloc_hopid()
793 ida = &port->in_hopids; in tb_port_alloc_hopid()
795 port_max_hopid = port->config.max_out_hop_id; in tb_port_alloc_hopid()
796 ida = &port->out_hopids; in tb_port_alloc_hopid()
800 * NHI can use HopIDs 1-max for other adapters HopIDs 0-7 are in tb_port_alloc_hopid()
813 * tb_port_alloc_in_hopid() - Allocate input HopID from port
827 * tb_port_alloc_out_hopid() - Allocate output HopID from port
841 * tb_port_release_in_hopid() - Release allocated input HopID from port
847 ida_simple_remove(&port->in_hopids, hopid); in tb_port_release_in_hopid()
851 * tb_port_release_out_hopid() - Release allocated output HopID from port
857 ida_simple_remove(&port->out_hopids, hopid); in tb_port_release_out_hopid()
863 u64 mask = (1ULL << parent->config.depth * 8) - 1; in tb_switch_is_reachable()
868 * tb_next_port_on_path() - Return next port for given port on a path
880 * Domain tb->lock must be held when this function is called.
890 if (prev->sw == end->sw) { in tb_next_port_on_path()
896 if (tb_switch_is_reachable(prev->sw, end->sw)) { in tb_next_port_on_path()
897 next = tb_port_at(tb_route(end->sw), prev->sw); in tb_next_port_on_path()
899 if (prev->remote && in tb_next_port_on_path()
900 (next == prev || next->dual_link_port == prev)) in tb_next_port_on_path()
901 next = prev->remote; in tb_next_port_on_path()
904 next = prev->remote; in tb_next_port_on_path()
906 next = tb_upstream_port(prev->sw); in tb_next_port_on_path()
911 if (next->dual_link_port && in tb_next_port_on_path()
912 next->link_nr != prev->link_nr) { in tb_next_port_on_path()
913 next = next->dual_link_port; in tb_next_port_on_path()
922 * tb_port_get_link_speed() - Get current link speed
932 if (!port->cap_phy) in tb_port_get_link_speed()
933 return -EINVAL; in tb_port_get_link_speed()
936 port->cap_phy + LANE_ADP_CS_1, 1); in tb_port_get_link_speed()
946 * tb_port_get_link_width() - Get current link width
949 * Returns link width. Return values can be 1 (Single-Lane), 2 (Dual-Lane)
957 if (!port->cap_phy) in tb_port_get_link_width()
958 return -EINVAL; in tb_port_get_link_width()
961 port->cap_phy + LANE_ADP_CS_1, 1); in tb_port_get_link_width()
974 if (!port->cap_phy) in tb_port_is_width_supported()
978 port->cap_phy + LANE_ADP_CS_0, 1); in tb_port_is_width_supported()
993 if (!port->cap_phy) in tb_port_set_link_width()
994 return -EINVAL; in tb_port_set_link_width()
997 port->cap_phy + LANE_ADP_CS_1, 1); in tb_port_set_link_width()
1012 return -EINVAL; in tb_port_set_link_width()
1018 port->cap_phy + LANE_ADP_CS_1, 1); in tb_port_set_link_width()
1022 * tb_port_lane_bonding_enable() - Enable bonding on port
1048 ret = tb_port_get_link_width(port->dual_link_port); in tb_port_lane_bonding_enable()
1050 ret = tb_port_set_link_width(port->dual_link_port, 2); in tb_port_lane_bonding_enable()
1057 port->bonded = true; in tb_port_lane_bonding_enable()
1058 port->dual_link_port->bonded = true; in tb_port_lane_bonding_enable()
1064 * tb_port_lane_bonding_disable() - Disable bonding on port
1073 port->dual_link_port->bonded = false; in tb_port_lane_bonding_disable()
1074 port->bonded = false; in tb_port_lane_bonding_disable()
1076 tb_port_set_link_width(port->dual_link_port, 1); in tb_port_lane_bonding_disable()
1081 * tb_port_wait_for_link_width() - Wait until link reaches specific width
1088 * the expected state. Returns %-ETIMEDOUT if the @width was not reached
1107 return -ETIMEDOUT; in tb_port_wait_for_link_width()
1119 if (nfc_credits != port->config.nfc_credits) { in tb_port_do_update_credits()
1125 tb_port_dbg(port, "total credits changed %u -> %u\n", in tb_port_do_update_credits()
1126 port->total_credits, total); in tb_port_do_update_credits()
1128 port->config.nfc_credits = nfc_credits; in tb_port_do_update_credits()
1129 port->total_credits = total; in tb_port_do_update_credits()
1136 * tb_port_update_credits() - Re-read port total credits
1140 * credits may change, so this function needs to be called to re-read
1150 return tb_port_do_update_credits(port->dual_link_port); in tb_port_update_credits()
1157 if (tb_switch_is_usb4(port->sw)) in tb_port_start_lane_initialization()
1161 return ret == -EINVAL ? 0 : ret; in tb_port_start_lane_initialization()
1165 * Returns true if the port had something (router, XDomain) connected
1172 if (port->usb4) { in tb_port_resume()
1173 usb4_port_device_resume(port->usb4); in tb_port_resume()
1180 * link gets re-established. in tb_port_resume()
1182 * This is only needed for non-USB4 ports. in tb_port_resume()
1184 if (!tb_is_upstream_port(port) || port->xdomain) in tb_port_resume()
1188 return has_remote || port->xdomain; in tb_port_resume()
1192 * tb_port_is_enabled() - Is the adapter port enabled
1197 switch (port->config.type) { in tb_port_is_enabled()
1216 * tb_usb3_port_is_enabled() - Is the USB3 adapter port enabled
1224 port->cap_adap + ADP_USB3_CS_0, 1)) in tb_usb3_port_is_enabled()
1231 * tb_usb3_port_enable() - Enable USB3 adapter port
1240 if (!port->cap_adap) in tb_usb3_port_enable()
1241 return -ENXIO; in tb_usb3_port_enable()
1243 port->cap_adap + ADP_USB3_CS_0, 1); in tb_usb3_port_enable()
1247 * tb_pci_port_is_enabled() - Is the PCIe adapter port enabled
1255 port->cap_adap + ADP_PCIE_CS_0, 1)) in tb_pci_port_is_enabled()
1262 * tb_pci_port_enable() - Enable PCIe adapter port
1269 if (!port->cap_adap) in tb_pci_port_enable()
1270 return -ENXIO; in tb_pci_port_enable()
1272 port->cap_adap + ADP_PCIE_CS_0, 1); in tb_pci_port_enable()
1276 * tb_dp_port_hpd_is_active() - Is HPD already active
1287 port->cap_adap + ADP_DP_CS_2, 1); in tb_dp_port_hpd_is_active()
1295 * tb_dp_port_hpd_clear() - Clear HPD from DP IN port
1306 port->cap_adap + ADP_DP_CS_3, 1); in tb_dp_port_hpd_clear()
1312 port->cap_adap + ADP_DP_CS_3, 1); in tb_dp_port_hpd_clear()
1316 * tb_dp_port_set_hops() - Set video/aux Hop IDs for DP port
1331 port->cap_adap + ADP_DP_CS_0, ARRAY_SIZE(data)); in tb_dp_port_set_hops()
1346 port->cap_adap + ADP_DP_CS_0, ARRAY_SIZE(data)); in tb_dp_port_set_hops()
1350 * tb_dp_port_is_enabled() - Is DP adapter port enabled
1357 if (tb_port_read(port, data, TB_CFG_PORT, port->cap_adap + ADP_DP_CS_0, in tb_dp_port_is_enabled()
1365 * tb_dp_port_enable() - Enables/disables DP paths of a port
1378 port->cap_adap + ADP_DP_CS_0, ARRAY_SIZE(data)); in tb_dp_port_enable()
1388 port->cap_adap + ADP_DP_CS_0, ARRAY_SIZE(data)); in tb_dp_port_enable()
1395 switch (sw->generation) { in tb_switch_generation_name()
1411 const struct tb_regs_switch_header *regs = &sw->config; in tb_dump_switch()
1414 tb_switch_generation_name(sw), regs->vendor_id, regs->device_id, in tb_dump_switch()
1415 regs->revision, regs->thunderbolt_version); in tb_dump_switch()
1416 tb_dbg(tb, " Max Port Number: %d\n", regs->max_port_number); in tb_dump_switch()
1420 regs->upstream_port_number, regs->depth, in tb_dump_switch()
1421 (((u64) regs->route_hi) << 32) | regs->route_lo, in tb_dump_switch()
1422 regs->enabled, regs->plug_events_delay); in tb_dump_switch()
1424 regs->__unknown1, regs->__unknown4); in tb_dump_switch()
1428 * tb_switch_reset() - reconfigure route, enable and send TB_CFG_PKG_RESET
1437 if (sw->generation > 1) in tb_switch_reset()
1442 res.err = tb_sw_write(sw, ((u32 *) &sw->config) + 2, in tb_switch_reset()
1446 res = tb_cfg_reset(sw->tb->ctl, tb_route(sw)); in tb_switch_reset()
1448 return -EIO; in tb_switch_reset()
1453 * tb_plug_events_active() - enable/disable plug events on a switch
1467 sw->config.plug_events_delay = 0xff; in tb_plug_events_active()
1468 res = tb_sw_write(sw, ((u32 *) &sw->config) + 4, TB_CFG_SWITCH, 4, 1); in tb_plug_events_active()
1472 res = tb_sw_read(sw, &data, TB_CFG_SWITCH, sw->cap_plug_events + 1, 1); in tb_plug_events_active()
1478 switch (sw->config.device_id) { in tb_plug_events_active()
1490 sw->cap_plug_events + 1, 1); in tb_plug_events_active()
1499 return sprintf(buf, "%u\n", sw->authorized); in authorized_show()
1508 if (sw && sw->authorized) { in disapprove_switch()
1512 ret = device_for_each_child_reverse(&sw->dev, NULL, disapprove_switch); in disapprove_switch()
1516 ret = tb_domain_disapprove_switch(sw->tb, sw); in disapprove_switch()
1520 sw->authorized = 0; in disapprove_switch()
1521 kobject_uevent_env(&sw->dev.kobj, KOBJ_CHANGE, envp); in disapprove_switch()
1530 int ret = -EINVAL; in tb_switch_set_authorized()
1533 if (!mutex_trylock(&sw->tb->lock)) in tb_switch_set_authorized()
1536 if (!!sw->authorized == !!val) in tb_switch_set_authorized()
1543 ret = disapprove_switch(&sw->dev, NULL); in tb_switch_set_authorized()
1550 if (sw->key) in tb_switch_set_authorized()
1551 ret = tb_domain_approve_switch_key(sw->tb, sw); in tb_switch_set_authorized()
1553 ret = tb_domain_approve_switch(sw->tb, sw); in tb_switch_set_authorized()
1558 if (sw->key) in tb_switch_set_authorized()
1559 ret = tb_domain_challenge_switch_key(sw->tb, sw); in tb_switch_set_authorized()
1567 sw->authorized = val; in tb_switch_set_authorized()
1572 sprintf(envp_string, "AUTHORIZED=%u", sw->authorized); in tb_switch_set_authorized()
1573 kobject_uevent_env(&sw->dev.kobj, KOBJ_CHANGE, envp); in tb_switch_set_authorized()
1577 mutex_unlock(&sw->tb->lock); in tb_switch_set_authorized()
1593 return -EINVAL; in authorized_store()
1595 pm_runtime_get_sync(&sw->dev); in authorized_store()
1597 pm_runtime_mark_last_busy(&sw->dev); in authorized_store()
1598 pm_runtime_put_autosuspend(&sw->dev); in authorized_store()
1609 return sprintf(buf, "%u\n", sw->boot); in boot_show()
1618 return sprintf(buf, "%#x\n", sw->device); in device_show()
1627 return sprintf(buf, "%s\n", sw->device_name ? sw->device_name : ""); in device_name_show()
1636 return sprintf(buf, "%u\n", sw->generation); in generation_show()
1646 if (!mutex_trylock(&sw->tb->lock)) in key_show()
1649 if (sw->key) in key_show()
1650 ret = sprintf(buf, "%*phN\n", TB_SWITCH_KEY_SIZE, sw->key); in key_show()
1654 mutex_unlock(&sw->tb->lock); in key_show()
1669 return -EINVAL; in key_store()
1671 if (!mutex_trylock(&sw->tb->lock)) in key_store()
1674 if (sw->authorized) { in key_store()
1675 ret = -EBUSY; in key_store()
1677 kfree(sw->key); in key_store()
1679 sw->key = NULL; in key_store()
1681 sw->key = kmemdup(key, sizeof(key), GFP_KERNEL); in key_store()
1682 if (!sw->key) in key_store()
1683 ret = -ENOMEM; in key_store()
1687 mutex_unlock(&sw->tb->lock); in key_store()
1697 return sprintf(buf, "%u.0 Gb/s\n", sw->link_speed); in speed_show()
1712 return sprintf(buf, "%u\n", sw->link_width); in lanes_show()
1738 pm_runtime_get_sync(&sw->dev); in nvm_authenticate_sysfs()
1740 if (!mutex_trylock(&sw->tb->lock)) { in nvm_authenticate_sysfs()
1746 if (!sw->nvm) { in nvm_authenticate_sysfs()
1747 ret = -EAGAIN; in nvm_authenticate_sysfs()
1761 ret = -EINVAL; in nvm_authenticate_sysfs()
1765 if (!sw->nvm->flushed) { in nvm_authenticate_sysfs()
1766 if (!sw->nvm->buf) { in nvm_authenticate_sysfs()
1767 ret = -EINVAL; in nvm_authenticate_sysfs()
1785 mutex_unlock(&sw->tb->lock); in nvm_authenticate_sysfs()
1787 pm_runtime_mark_last_busy(&sw->dev); in nvm_authenticate_sysfs()
1788 pm_runtime_put_autosuspend(&sw->dev); in nvm_authenticate_sysfs()
1825 if (!mutex_trylock(&sw->tb->lock)) in nvm_version_show()
1828 if (sw->safe_mode) in nvm_version_show()
1829 ret = -ENODATA; in nvm_version_show()
1830 else if (!sw->nvm) in nvm_version_show()
1831 ret = -EAGAIN; in nvm_version_show()
1833 ret = sprintf(buf, "%x.%x\n", sw->nvm->major, sw->nvm->minor); in nvm_version_show()
1835 mutex_unlock(&sw->tb->lock); in nvm_version_show()
1846 return sprintf(buf, "%#x\n", sw->vendor); in vendor_show()
1855 return sprintf(buf, "%s\n", sw->vendor_name ? sw->vendor_name : ""); in vendor_name_show()
1864 return sprintf(buf, "%pUb\n", sw->uuid); in unique_id_show()
1895 if (sw->tb->security_level == TB_SECURITY_NOPCIE || in switch_attr_is_visible()
1896 sw->tb->security_level == TB_SECURITY_DPONLY) in switch_attr_is_visible()
1899 if (!sw->device) in switch_attr_is_visible()
1902 if (!sw->device_name) in switch_attr_is_visible()
1905 if (!sw->vendor) in switch_attr_is_visible()
1908 if (!sw->vendor_name) in switch_attr_is_visible()
1912 sw->tb->security_level == TB_SECURITY_SECURE && in switch_attr_is_visible()
1913 sw->security_level == TB_SECURITY_SECURE) in switch_attr_is_visible()
1914 return attr->mode; in switch_attr_is_visible()
1921 return attr->mode; in switch_attr_is_visible()
1925 return attr->mode; in switch_attr_is_visible()
1929 return attr->mode; in switch_attr_is_visible()
1933 return attr->mode; in switch_attr_is_visible()
1936 if (sw->quirks & QUIRK_FORCE_POWER_LINK_CONTROLLER) in switch_attr_is_visible()
1937 return attr->mode; in switch_attr_is_visible()
1941 return sw->safe_mode ? 0 : attr->mode; in switch_attr_is_visible()
1959 dma_port_free(sw->dma_port); in tb_switch_release()
1962 ida_destroy(&port->in_hopids); in tb_switch_release()
1963 ida_destroy(&port->out_hopids); in tb_switch_release()
1966 kfree(sw->uuid); in tb_switch_release()
1967 kfree(sw->device_name); in tb_switch_release()
1968 kfree(sw->vendor_name); in tb_switch_release()
1969 kfree(sw->ports); in tb_switch_release()
1970 kfree(sw->drom); in tb_switch_release()
1971 kfree(sw->key); in tb_switch_release()
1980 if (sw->config.thunderbolt_version == USB4_VERSION_1_0) { in tb_switch_uevent()
1982 return -ENOMEM; in tb_switch_uevent()
1993 if (!port->disabled && !tb_is_upstream_port(port) && in tb_switch_uevent()
2004 return -ENOMEM; in tb_switch_uevent()
2015 const struct tb_cm_ops *cm_ops = sw->tb->cm_ops; in tb_switch_runtime_suspend()
2017 if (cm_ops->runtime_suspend_switch) in tb_switch_runtime_suspend()
2018 return cm_ops->runtime_suspend_switch(sw); in tb_switch_runtime_suspend()
2026 const struct tb_cm_ops *cm_ops = sw->tb->cm_ops; in tb_switch_runtime_resume()
2028 if (cm_ops->runtime_resume_switch) in tb_switch_runtime_resume()
2029 return cm_ops->runtime_resume_switch(sw); in tb_switch_runtime_resume()
2047 switch (sw->config.device_id) { in tb_switch_get_generation()
2084 sw->config.device_id); in tb_switch_get_generation()
2094 (sw->tb->root_switch && tb_switch_is_usb4(sw->tb->root_switch))) in tb_switch_exceeds_max_depth()
2103 * tb_switch_alloc() - allocate a switch
2134 upstream_port = tb_cfg_get_upstream_port(tb->ctl, route); in tb_switch_alloc()
2140 return ERR_PTR(-ENOMEM); in tb_switch_alloc()
2142 sw->tb = tb; in tb_switch_alloc()
2143 ret = tb_cfg_read(tb->ctl, &sw->config, route, 0, TB_CFG_SWITCH, 0, 5); in tb_switch_alloc()
2147 sw->generation = tb_switch_get_generation(sw); in tb_switch_alloc()
2153 sw->config.upstream_port_number = upstream_port; in tb_switch_alloc()
2154 sw->config.depth = depth; in tb_switch_alloc()
2155 sw->config.route_hi = upper_32_bits(route); in tb_switch_alloc()
2156 sw->config.route_lo = lower_32_bits(route); in tb_switch_alloc()
2157 sw->config.enabled = 0; in tb_switch_alloc()
2161 ret = -EADDRNOTAVAIL; in tb_switch_alloc()
2166 sw->ports = kcalloc(sw->config.max_port_number + 1, sizeof(*sw->ports), in tb_switch_alloc()
2168 if (!sw->ports) { in tb_switch_alloc()
2169 ret = -ENOMEM; in tb_switch_alloc()
2173 for (i = 0; i <= sw->config.max_port_number; i++) { in tb_switch_alloc()
2175 sw->ports[i].sw = sw; in tb_switch_alloc()
2176 sw->ports[i].port = i; in tb_switch_alloc()
2180 ida_init(&sw->ports[i].in_hopids); in tb_switch_alloc()
2181 ida_init(&sw->ports[i].out_hopids); in tb_switch_alloc()
2187 sw->cap_plug_events = ret; in tb_switch_alloc()
2191 sw->cap_lc = ret; in tb_switch_alloc()
2195 sw->authorized = true; in tb_switch_alloc()
2197 device_initialize(&sw->dev); in tb_switch_alloc()
2198 sw->dev.parent = parent; in tb_switch_alloc()
2199 sw->dev.bus = &tb_bus_type; in tb_switch_alloc()
2200 sw->dev.type = &tb_switch_type; in tb_switch_alloc()
2201 sw->dev.groups = switch_groups; in tb_switch_alloc()
2202 dev_set_name(&sw->dev, "%u-%llx", tb->index, tb_route(sw)); in tb_switch_alloc()
2207 kfree(sw->ports); in tb_switch_alloc()
2214 * tb_switch_alloc_safe_mode() - allocate a switch that is in safe mode
2220 * lacks all capabilities except DMA configuration port before it is
2234 return ERR_PTR(-ENOMEM); in tb_switch_alloc_safe_mode()
2236 sw->tb = tb; in tb_switch_alloc_safe_mode()
2237 sw->config.depth = tb_route_length(route); in tb_switch_alloc_safe_mode()
2238 sw->config.route_hi = upper_32_bits(route); in tb_switch_alloc_safe_mode()
2239 sw->config.route_lo = lower_32_bits(route); in tb_switch_alloc_safe_mode()
2240 sw->safe_mode = true; in tb_switch_alloc_safe_mode()
2242 device_initialize(&sw->dev); in tb_switch_alloc_safe_mode()
2243 sw->dev.parent = parent; in tb_switch_alloc_safe_mode()
2244 sw->dev.bus = &tb_bus_type; in tb_switch_alloc_safe_mode()
2245 sw->dev.type = &tb_switch_type; in tb_switch_alloc_safe_mode()
2246 sw->dev.groups = switch_groups; in tb_switch_alloc_safe_mode()
2247 dev_set_name(&sw->dev, "%u-%llx", tb->index, tb_route(sw)); in tb_switch_alloc_safe_mode()
2253 * tb_switch_configure() - Uploads configuration to the switch
2259 * resume from low power states to re-initialize it.
2265 struct tb *tb = sw->tb; in tb_switch_configure()
2272 sw->config.enabled ? "restoring" : "initializing", route, in tb_switch_configure()
2273 tb_route_length(route), sw->config.upstream_port_number); in tb_switch_configure()
2275 sw->config.enabled = 1; in tb_switch_configure()
2283 sw->config.cmuv = USB4_VERSION_1_0; in tb_switch_configure()
2286 ret = tb_sw_write(sw, (u32 *)&sw->config + 1, TB_CFG_SWITCH, in tb_switch_configure()
2293 if (sw->config.vendor_id != PCI_VENDOR_ID_INTEL) in tb_switch_configure()
2295 sw->config.vendor_id); in tb_switch_configure()
2297 if (!sw->cap_plug_events) { in tb_switch_configure()
2299 return -ENODEV; in tb_switch_configure()
2303 ret = tb_sw_write(sw, (u32 *)&sw->config + 1, TB_CFG_SWITCH, in tb_switch_configure()
2318 if (sw->uuid) in tb_switch_set_uuid()
2322 ret = usb4_switch_read_uid(sw, &sw->uid); in tb_switch_set_uuid()
2333 if (ret != -EINVAL) in tb_switch_set_uuid()
2346 uuid[0] = sw->uid & 0xffffffff; in tb_switch_set_uuid()
2347 uuid[1] = (sw->uid >> 32) & 0xffffffff; in tb_switch_set_uuid()
2352 sw->uuid = kmemdup(uuid, sizeof(uuid), GFP_KERNEL); in tb_switch_set_uuid()
2353 if (!sw->uuid) in tb_switch_set_uuid()
2354 return -ENOMEM; in tb_switch_set_uuid()
2363 switch (sw->generation) { in tb_switch_add_dma_port()
2379 * DMA port is the only thing available when the switch in tb_switch_add_dma_port()
2382 if (!sw->safe_mode) in tb_switch_add_dma_port()
2387 if (sw->no_nvm_upgrade) in tb_switch_add_dma_port()
2403 /* Root switch DMA port requires running firmware */ in tb_switch_add_dma_port()
2407 sw->dma_port = dma_port_alloc(sw); in tb_switch_add_dma_port()
2408 if (!sw->dma_port) in tb_switch_add_dma_port()
2429 ret = dma_port_flash_update_auth_status(sw->dma_port, &status); in tb_switch_add_dma_port()
2443 dma_port_power_cycle(sw->dma_port); in tb_switch_add_dma_port()
2449 return -ESHUTDOWN; in tb_switch_add_dma_port()
2456 for (i = 1; i <= sw->config.max_port_number; i++) { in tb_switch_default_link_ports()
2457 struct tb_port *port = &sw->ports[i]; in tb_switch_default_link_ports()
2464 if (i == sw->config.max_port_number || in tb_switch_default_link_ports()
2465 !tb_port_is_null(&sw->ports[i + 1])) in tb_switch_default_link_ports()
2469 subordinate = &sw->ports[i + 1]; in tb_switch_default_link_ports()
2470 if (!port->dual_link_port && !subordinate->dual_link_port) { in tb_switch_default_link_ports()
2471 port->link_nr = 0; in tb_switch_default_link_ports()
2472 port->dual_link_port = subordinate; in tb_switch_default_link_ports()
2473 subordinate->link_nr = 1; in tb_switch_default_link_ports()
2474 subordinate->dual_link_port = port; in tb_switch_default_link_ports()
2476 tb_sw_dbg(sw, "linked ports %d <-> %d\n", in tb_switch_default_link_ports()
2477 port->port, subordinate->port); in tb_switch_default_link_ports()
2486 if (!up->dual_link_port || !up->dual_link_port->remote) in tb_switch_lane_bonding_possible()
2508 if (sw->link_speed != ret) in tb_switch_update_link_attributes()
2510 sw->link_speed = ret; in tb_switch_update_link_attributes()
2515 if (sw->link_width != ret) in tb_switch_update_link_attributes()
2517 sw->link_width = ret; in tb_switch_update_link_attributes()
2520 if (device_is_registered(&sw->dev) && change) in tb_switch_update_link_attributes()
2521 kobject_uevent(&sw->dev.kobj, KOBJ_CHANGE); in tb_switch_update_link_attributes()
2527 * tb_switch_lane_bonding_enable() - Enable lane bonding
2536 struct tb_switch *parent = tb_to_switch(sw->dev.parent); in tb_switch_lane_bonding_enable()
2582 * tb_switch_lane_bonding_disable() - Disable lane bonding
2590 struct tb_switch *parent = tb_to_switch(sw->dev.parent); in tb_switch_lane_bonding_disable()
2597 if (!up->bonded) in tb_switch_lane_bonding_disable()
2606 * It is fine if we get other errors as the router might have in tb_switch_lane_bonding_disable()
2609 if (tb_port_wait_for_link_width(down, 1, 100) == -ETIMEDOUT) in tb_switch_lane_bonding_disable()
2620 * tb_switch_configure_link() - Set link configured
2640 if (tb_switch_is_usb4(up->sw)) in tb_switch_configure_link()
2647 down = up->remote; in tb_switch_configure_link()
2648 if (tb_switch_is_usb4(down->sw)) in tb_switch_configure_link()
2654 * tb_switch_unconfigure_link() - Unconfigure link
2664 if (sw->is_unplugged) in tb_switch_unconfigure_link()
2670 if (tb_switch_is_usb4(up->sw)) in tb_switch_unconfigure_link()
2675 down = up->remote; in tb_switch_unconfigure_link()
2676 if (tb_switch_is_usb4(down->sw)) in tb_switch_unconfigure_link()
2693 * tb_switch_add() - Add a switch to the domain
2709 * Initialize DMA control port now before we read DROM. Recent in tb_switch_add()
2712 * to the userspace. NVM can be accessed through DMA in tb_switch_add()
2717 dev_err(&sw->dev, "failed to add DMA port\n"); in tb_switch_add()
2721 if (!sw->safe_mode) { in tb_switch_add()
2727 dev_err(&sw->dev, "reading DROM failed\n"); in tb_switch_add()
2730 tb_sw_dbg(sw, "uid: %#llx\n", sw->uid); in tb_switch_add()
2736 dev_err(&sw->dev, "failed to set UUID\n"); in tb_switch_add()
2740 for (i = 0; i <= sw->config.max_port_number; i++) { in tb_switch_add()
2741 if (sw->ports[i].disabled) { in tb_switch_add()
2742 tb_port_dbg(&sw->ports[i], "disabled by eeprom\n"); in tb_switch_add()
2745 ret = tb_init_port(&sw->ports[i]); in tb_switch_add()
2747 dev_err(&sw->dev, "failed to initialize port %d\n", i); in tb_switch_add()
2763 ret = device_add(&sw->dev); in tb_switch_add()
2765 dev_err(&sw->dev, "failed to add device: %d\n", ret); in tb_switch_add()
2770 dev_info(&sw->dev, "new device found, vendor=%#x device=%#x\n", in tb_switch_add()
2771 sw->vendor, sw->device); in tb_switch_add()
2772 if (sw->vendor_name && sw->device_name) in tb_switch_add()
2773 dev_info(&sw->dev, "%s %s\n", sw->vendor_name, in tb_switch_add()
2774 sw->device_name); in tb_switch_add()
2779 dev_err(&sw->dev, "failed to add USB4 ports\n"); in tb_switch_add()
2785 dev_err(&sw->dev, "failed to add NVM devices\n"); in tb_switch_add()
2794 device_init_wakeup(&sw->dev, true); in tb_switch_add()
2796 pm_runtime_set_active(&sw->dev); in tb_switch_add()
2797 if (sw->rpm) { in tb_switch_add()
2798 pm_runtime_set_autosuspend_delay(&sw->dev, TB_AUTOSUSPEND_DELAY); in tb_switch_add()
2799 pm_runtime_use_autosuspend(&sw->dev); in tb_switch_add()
2800 pm_runtime_mark_last_busy(&sw->dev); in tb_switch_add()
2801 pm_runtime_enable(&sw->dev); in tb_switch_add()
2802 pm_request_autosuspend(&sw->dev); in tb_switch_add()
2811 device_del(&sw->dev); in tb_switch_add()
2817 * tb_switch_remove() - Remove and release a switch
2830 if (sw->rpm) { in tb_switch_remove()
2831 pm_runtime_get_sync(&sw->dev); in tb_switch_remove()
2832 pm_runtime_disable(&sw->dev); in tb_switch_remove()
2838 tb_switch_remove(port->remote->sw); in tb_switch_remove()
2839 port->remote = NULL; in tb_switch_remove()
2840 } else if (port->xdomain) { in tb_switch_remove()
2841 tb_xdomain_remove(port->xdomain); in tb_switch_remove()
2842 port->xdomain = NULL; in tb_switch_remove()
2849 if (!sw->is_unplugged) in tb_switch_remove()
2856 dev_info(&sw->dev, "device disconnected\n"); in tb_switch_remove()
2857 device_unregister(&sw->dev); in tb_switch_remove()
2861 * tb_sw_set_unplugged() - set is_unplugged on switch and downstream switches
2862 * @sw: Router to mark unplugged
2868 if (sw == sw->tb->root_switch) { in tb_sw_set_unplugged()
2872 if (sw->is_unplugged) { in tb_sw_set_unplugged()
2876 sw->is_unplugged = true; in tb_sw_set_unplugged()
2879 tb_sw_set_unplugged(port->remote->sw); in tb_sw_set_unplugged()
2880 else if (port->xdomain) in tb_sw_set_unplugged()
2881 port->xdomain->is_unplugged = true; in tb_sw_set_unplugged()
2916 err = tb_cfg_get_upstream_port(sw->tb->ctl, tb_route(sw)); in tb_switch_resume()
2930 if (sw->uid != uid) { in tb_switch_resume()
2932 "changed while suspended (uid %#llx -> %#llx)\n", in tb_switch_resume()
2933 sw->uid, uid); in tb_switch_resume()
2934 return -ENODEV; in tb_switch_resume()
2961 tb_sw_set_unplugged(port->remote->sw); in tb_switch_resume()
2962 else if (port->xdomain) in tb_switch_resume()
2963 port->xdomain->is_unplugged = true; in tb_switch_resume()
2971 if (port->remote && tb_switch_resume(port->remote->sw)) { in tb_switch_resume()
2974 tb_sw_set_unplugged(port->remote->sw); in tb_switch_resume()
2982 * tb_switch_suspend() - Put a switch to sleep
2986 * Suspends router and all its children. Enables wakes according to
2987 * value of @runtime and then sets sleep bit for the router. If @sw is
2988 * host router the domain is ready to go to sleep once this function
3005 tb_switch_suspend(port->remote->sw, runtime); in tb_switch_suspend()
3013 } else if (device_may_wakeup(&sw->dev)) { in tb_switch_suspend()
3026 * tb_switch_query_dp_resource() - Query availability of DP resource
3041 * tb_switch_alloc_dp_resource() - Allocate available DP resource
3057 * tb_switch_dealloc_dp_resource() - De-allocate DP resource
3058 * @sw: Switch whose DP resource is de-allocated
3061 * De-allocates DP resource that was previously allocated for DP
3074 tb_sw_warn(sw, "failed to de-allocate DP resource for port %d\n", in tb_switch_dealloc_dp_resource()
3075 in->port); in tb_switch_dealloc_dp_resource()
3093 if (sw->tb != lookup->tb) in tb_switch_match()
3096 if (lookup->uuid) in tb_switch_match()
3097 return !memcmp(sw->uuid, lookup->uuid, sizeof(*lookup->uuid)); in tb_switch_match()
3099 if (lookup->route) { in tb_switch_match()
3100 return sw->config.route_lo == lower_32_bits(lookup->route) && in tb_switch_match()
3101 sw->config.route_hi == upper_32_bits(lookup->route); in tb_switch_match()
3105 if (!lookup->depth) in tb_switch_match()
3106 return !sw->depth; in tb_switch_match()
3108 return sw->link == lookup->link && sw->depth == lookup->depth; in tb_switch_match()
3112 * tb_switch_find_by_link_depth() - Find switch by link and depth
3138 * tb_switch_find_by_uuid() - Find switch by UUID
3162 * tb_switch_find_by_route() - Find switch by route string
3175 return tb_switch_get(tb->root_switch); in tb_switch_find_by_route()
3189 * tb_switch_find_port() - return the first port of @type on @sw or NULL
3199 if (port->config.type == type) in tb_switch_find_port()