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>
47 if (uuid_equal(&st->uuid, sw->uuid)) in __nvm_get_auth_status()
62 *status = st ? st->status : 0; in nvm_get_auth_status()
69 if (WARN_ON(!sw->uuid)) 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()
97 list_del(&st->list); in nvm_clear_auth_status()
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()
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()
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()
196 * cycling of the host router. 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()
207 dma_port_power_cycle(sw->dma_port); in nvm_authenticate_host_dma_port()
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()
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()
261 * During host router NVM upgrade we should not allow root port to in nvm_authenticate_start_dma_port()
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()
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()
292 /* Thunderbolt 2 and 3 devices support NVM through DMA port */ in nvm_readable()
293 return !!sw->dma_port; in nvm_readable()
298 if (sw->no_nvm_upgrade) in nvm_upgradeable()
308 return dma_port_flash_read(sw->dma_port, address, buf, size); in nvm_read()
332 struct tb_switch *sw = tb_to_switch(nvm->dev); in tb_switch_nvm_read()
335 pm_runtime_get_sync(&sw->dev); in tb_switch_nvm_read()
337 if (!mutex_trylock(&sw->tb->lock)) { 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()
359 if (!mutex_trylock(&sw->tb->lock)) in tb_switch_nvm_write()
369 mutex_unlock(&sw->tb->lock); in tb_switch_nvm_write()
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()
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()
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()
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()
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
663 * downstream router accessible for CM.
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()
765 if (port->port) { in tb_init_port()
766 ida_init(&port->in_hopids); in tb_init_port()
767 ida_init(&port->out_hopids); in tb_init_port()
770 INIT_LIST_HEAD(&port->list); in tb_init_port()
782 port_max_hopid = port->config.max_in_hop_id; in tb_port_alloc_hopid()
783 ida = &port->in_hopids; in tb_port_alloc_hopid()
785 port_max_hopid = port->config.max_out_hop_id; in tb_port_alloc_hopid()
786 ida = &port->out_hopids; in tb_port_alloc_hopid()
790 * NHI can use HopIDs 1-max for other adapters HopIDs 0-7 are in tb_port_alloc_hopid()
803 * tb_port_alloc_in_hopid() - Allocate input HopID from port
817 * tb_port_alloc_out_hopid() - Allocate output HopID from port
831 * tb_port_release_in_hopid() - Release allocated input HopID from port
837 ida_simple_remove(&port->in_hopids, hopid); in tb_port_release_in_hopid()
841 * tb_port_release_out_hopid() - Release allocated output HopID from port
847 ida_simple_remove(&port->out_hopids, hopid); in tb_port_release_out_hopid()
853 u64 mask = (1ULL << parent->config.depth * 8) - 1; in tb_switch_is_reachable()
858 * tb_next_port_on_path() - Return next port for given port on a path
870 * Domain tb->lock must be held when this function is called.
880 if (prev->sw == end->sw) { in tb_next_port_on_path()
886 if (tb_switch_is_reachable(prev->sw, end->sw)) { in tb_next_port_on_path()
887 next = tb_port_at(tb_route(end->sw), prev->sw); in tb_next_port_on_path()
889 if (prev->remote && in tb_next_port_on_path()
890 (next == prev || next->dual_link_port == prev)) in tb_next_port_on_path()
891 next = prev->remote; in tb_next_port_on_path()
894 next = prev->remote; in tb_next_port_on_path()
896 next = tb_upstream_port(prev->sw); in tb_next_port_on_path()
901 if (next->dual_link_port && in tb_next_port_on_path()
902 next->link_nr != prev->link_nr) { in tb_next_port_on_path()
903 next = next->dual_link_port; in tb_next_port_on_path()
912 * tb_port_get_link_speed() - Get current link speed
922 if (!port->cap_phy) in tb_port_get_link_speed()
923 return -EINVAL; in tb_port_get_link_speed()
926 port->cap_phy + LANE_ADP_CS_1, 1); in tb_port_get_link_speed()
940 if (!port->cap_phy) in tb_port_get_link_width()
941 return -EINVAL; in tb_port_get_link_width()
944 port->cap_phy + LANE_ADP_CS_1, 1); in tb_port_get_link_width()
957 if (!port->cap_phy) in tb_port_is_width_supported()
961 port->cap_phy + LANE_ADP_CS_0, 1); in tb_port_is_width_supported()
976 if (!port->cap_phy) in tb_port_set_link_width()
977 return -EINVAL; in tb_port_set_link_width()
980 port->cap_phy + LANE_ADP_CS_1, 1); in tb_port_set_link_width()
995 return -EINVAL; in tb_port_set_link_width()
1001 port->cap_phy + LANE_ADP_CS_1, 1); in tb_port_set_link_width()
1019 ret = tb_port_get_link_width(port->dual_link_port); in tb_port_lane_bonding_enable()
1021 ret = tb_port_set_link_width(port->dual_link_port, 2); in tb_port_lane_bonding_enable()
1028 port->bonded = true; in tb_port_lane_bonding_enable()
1029 port->dual_link_port->bonded = true; in tb_port_lane_bonding_enable()
1036 port->dual_link_port->bonded = false; in tb_port_lane_bonding_disable()
1037 port->bonded = false; in tb_port_lane_bonding_disable()
1039 tb_port_set_link_width(port->dual_link_port, 1); in tb_port_lane_bonding_disable()
1044 * tb_port_is_enabled() - Is the adapter port enabled
1049 switch (port->config.type) { in tb_port_is_enabled()
1068 * tb_usb3_port_is_enabled() - Is the USB3 adapter port enabled
1076 port->cap_adap + ADP_USB3_CS_0, 1)) in tb_usb3_port_is_enabled()
1083 * tb_usb3_port_enable() - Enable USB3 adapter port
1092 if (!port->cap_adap) in tb_usb3_port_enable()
1093 return -ENXIO; in tb_usb3_port_enable()
1095 port->cap_adap + ADP_USB3_CS_0, 1); in tb_usb3_port_enable()
1099 * tb_pci_port_is_enabled() - Is the PCIe adapter port enabled
1107 port->cap_adap + ADP_PCIE_CS_0, 1)) in tb_pci_port_is_enabled()
1114 * tb_pci_port_enable() - Enable PCIe adapter port
1121 if (!port->cap_adap) in tb_pci_port_enable()
1122 return -ENXIO; in tb_pci_port_enable()
1124 port->cap_adap + ADP_PCIE_CS_0, 1); in tb_pci_port_enable()
1128 * tb_dp_port_hpd_is_active() - Is HPD already active
1139 port->cap_adap + ADP_DP_CS_2, 1); in tb_dp_port_hpd_is_active()
1147 * tb_dp_port_hpd_clear() - Clear HPD from DP IN port
1158 port->cap_adap + ADP_DP_CS_3, 1); in tb_dp_port_hpd_clear()
1164 port->cap_adap + ADP_DP_CS_3, 1); in tb_dp_port_hpd_clear()
1168 * tb_dp_port_set_hops() - Set video/aux Hop IDs for DP port
1183 port->cap_adap + ADP_DP_CS_0, ARRAY_SIZE(data)); in tb_dp_port_set_hops()
1198 port->cap_adap + ADP_DP_CS_0, ARRAY_SIZE(data)); in tb_dp_port_set_hops()
1202 * tb_dp_port_is_enabled() - Is DP adapter port enabled
1209 if (tb_port_read(port, data, TB_CFG_PORT, port->cap_adap + ADP_DP_CS_0, in tb_dp_port_is_enabled()
1217 * tb_dp_port_enable() - Enables/disables DP paths of a port
1230 port->cap_adap + ADP_DP_CS_0, ARRAY_SIZE(data)); in tb_dp_port_enable()
1240 port->cap_adap + ADP_DP_CS_0, ARRAY_SIZE(data)); in tb_dp_port_enable()
1247 switch (sw->generation) { in tb_switch_generation_name()
1263 const struct tb_regs_switch_header *regs = &sw->config; in tb_dump_switch()
1266 tb_switch_generation_name(sw), regs->vendor_id, regs->device_id, in tb_dump_switch()
1267 regs->revision, regs->thunderbolt_version); in tb_dump_switch()
1268 tb_dbg(tb, " Max Port Number: %d\n", regs->max_port_number); in tb_dump_switch()
1272 regs->upstream_port_number, regs->depth, in tb_dump_switch()
1273 (((u64) regs->route_hi) << 32) | regs->route_lo, in tb_dump_switch()
1274 regs->enabled, regs->plug_events_delay); in tb_dump_switch()
1276 regs->__unknown1, regs->__unknown4); in tb_dump_switch()
1280 * reset_switch() - reconfigure route, enable and send TB_CFG_PKG_RESET
1289 if (sw->generation > 1) in tb_switch_reset()
1294 res.err = tb_sw_write(sw, ((u32 *) &sw->config) + 2, in tb_switch_reset()
1298 res = tb_cfg_reset(sw->tb->ctl, tb_route(sw), TB_CFG_DEFAULT_TIMEOUT); in tb_switch_reset()
1300 return -EIO; in tb_switch_reset()
1305 * tb_plug_events_active() - enable/disable plug events on a switch
1319 sw->config.plug_events_delay = 0xff; in tb_plug_events_active()
1320 res = tb_sw_write(sw, ((u32 *) &sw->config) + 4, TB_CFG_SWITCH, 4, 1); in tb_plug_events_active()
1324 res = tb_sw_read(sw, &data, TB_CFG_SWITCH, sw->cap_plug_events + 1, 1); in tb_plug_events_active()
1330 switch (sw->config.device_id) { in tb_plug_events_active()
1342 sw->cap_plug_events + 1, 1); in tb_plug_events_active()
1351 return sprintf(buf, "%u\n", sw->authorized); in authorized_show()
1356 int ret = -EINVAL; in tb_switch_set_authorized()
1358 if (!mutex_trylock(&sw->tb->lock)) in tb_switch_set_authorized()
1361 if (sw->authorized) in tb_switch_set_authorized()
1367 if (sw->key) in tb_switch_set_authorized()
1368 ret = tb_domain_approve_switch_key(sw->tb, sw); in tb_switch_set_authorized()
1370 ret = tb_domain_approve_switch(sw->tb, sw); in tb_switch_set_authorized()
1375 if (sw->key) in tb_switch_set_authorized()
1376 ret = tb_domain_challenge_switch_key(sw->tb, sw); in tb_switch_set_authorized()
1384 sw->authorized = val; in tb_switch_set_authorized()
1386 kobject_uevent(&sw->dev.kobj, KOBJ_CHANGE); in tb_switch_set_authorized()
1390 mutex_unlock(&sw->tb->lock); in tb_switch_set_authorized()
1406 return -EINVAL; in authorized_store()
1408 pm_runtime_get_sync(&sw->dev); in authorized_store()
1410 pm_runtime_mark_last_busy(&sw->dev); in authorized_store()
1411 pm_runtime_put_autosuspend(&sw->dev); in authorized_store()
1422 return sprintf(buf, "%u\n", sw->boot); in boot_show()
1431 return sprintf(buf, "%#x\n", sw->device); in device_show()
1440 return sprintf(buf, "%s\n", sw->device_name ? sw->device_name : ""); in device_name_show()
1449 return sprintf(buf, "%u\n", sw->generation); in generation_show()
1459 if (!mutex_trylock(&sw->tb->lock)) in key_show()
1462 if (sw->key) in key_show()
1463 ret = sprintf(buf, "%*phN\n", TB_SWITCH_KEY_SIZE, sw->key); in key_show()
1467 mutex_unlock(&sw->tb->lock); in key_show()
1482 return -EINVAL; in key_store()
1484 if (!mutex_trylock(&sw->tb->lock)) in key_store()
1487 if (sw->authorized) { in key_store()
1488 ret = -EBUSY; in key_store()
1490 kfree(sw->key); in key_store()
1492 sw->key = NULL; in key_store()
1494 sw->key = kmemdup(key, sizeof(key), GFP_KERNEL); in key_store()
1495 if (!sw->key) in key_store()
1496 ret = -ENOMEM; in key_store()
1500 mutex_unlock(&sw->tb->lock); in key_store()
1510 return sprintf(buf, "%u.0 Gb/s\n", sw->link_speed); in speed_show()
1525 return sprintf(buf, "%u\n", sw->link_width); in lanes_show()
1552 pm_runtime_get_sync(&sw->dev); in nvm_authenticate_sysfs()
1554 if (!mutex_trylock(&sw->tb->lock)) { in nvm_authenticate_sysfs()
1560 if (!sw->nvm) { in nvm_authenticate_sysfs()
1561 ret = -EAGAIN; in nvm_authenticate_sysfs()
1573 if (!sw->nvm->flushed) { in nvm_authenticate_sysfs()
1574 if (!sw->nvm->buf) { in nvm_authenticate_sysfs()
1575 ret = -EINVAL; in nvm_authenticate_sysfs()
1587 sw->nvm->authenticating = true; in nvm_authenticate_sysfs()
1594 mutex_unlock(&sw->tb->lock); in nvm_authenticate_sysfs()
1596 pm_runtime_mark_last_busy(&sw->dev); in nvm_authenticate_sysfs()
1597 pm_runtime_put_autosuspend(&sw->dev); in nvm_authenticate_sysfs()
1634 if (!mutex_trylock(&sw->tb->lock)) in nvm_version_show()
1637 if (sw->safe_mode) in nvm_version_show()
1638 ret = -ENODATA; in nvm_version_show()
1639 else if (!sw->nvm) in nvm_version_show()
1640 ret = -EAGAIN; in nvm_version_show()
1642 ret = sprintf(buf, "%x.%x\n", sw->nvm->major, sw->nvm->minor); in nvm_version_show()
1644 mutex_unlock(&sw->tb->lock); in nvm_version_show()
1655 return sprintf(buf, "%#x\n", sw->vendor); in vendor_show()
1664 return sprintf(buf, "%s\n", sw->vendor_name ? sw->vendor_name : ""); in vendor_name_show()
1673 return sprintf(buf, "%pUb\n", sw->uuid); in unique_id_show()
1704 if (!sw->device) in switch_attr_is_visible()
1707 if (!sw->device_name) in switch_attr_is_visible()
1710 if (!sw->vendor) in switch_attr_is_visible()
1713 if (!sw->vendor_name) in switch_attr_is_visible()
1717 sw->tb->security_level == TB_SECURITY_SECURE && in switch_attr_is_visible()
1718 sw->security_level == TB_SECURITY_SECURE) in switch_attr_is_visible()
1719 return attr->mode; in switch_attr_is_visible()
1726 return attr->mode; in switch_attr_is_visible()
1730 return attr->mode; in switch_attr_is_visible()
1734 return attr->mode; in switch_attr_is_visible()
1738 return attr->mode; in switch_attr_is_visible()
1741 if (sw->quirks & QUIRK_FORCE_POWER_LINK_CONTROLLER) in switch_attr_is_visible()
1742 return attr->mode; in switch_attr_is_visible()
1746 return sw->safe_mode ? 0 : attr->mode; in switch_attr_is_visible()
1764 dma_port_free(sw->dma_port); in tb_switch_release()
1767 if (!port->disabled) { in tb_switch_release()
1768 ida_destroy(&port->in_hopids); in tb_switch_release()
1769 ida_destroy(&port->out_hopids); in tb_switch_release()
1773 kfree(sw->uuid); in tb_switch_release()
1774 kfree(sw->device_name); in tb_switch_release()
1775 kfree(sw->vendor_name); in tb_switch_release()
1776 kfree(sw->ports); in tb_switch_release()
1777 kfree(sw->drom); in tb_switch_release()
1778 kfree(sw->key); in tb_switch_release()
1789 const struct tb_cm_ops *cm_ops = sw->tb->cm_ops; in tb_switch_runtime_suspend()
1791 if (cm_ops->runtime_suspend_switch) in tb_switch_runtime_suspend()
1792 return cm_ops->runtime_suspend_switch(sw); in tb_switch_runtime_suspend()
1800 const struct tb_cm_ops *cm_ops = sw->tb->cm_ops; in tb_switch_runtime_resume()
1802 if (cm_ops->runtime_resume_switch) in tb_switch_runtime_resume()
1803 return cm_ops->runtime_resume_switch(sw); in tb_switch_runtime_resume()
1820 switch (sw->config.device_id) { in tb_switch_get_generation()
1857 sw->config.device_id); in tb_switch_get_generation()
1867 (sw->tb->root_switch && tb_switch_is_usb4(sw->tb->root_switch))) in tb_switch_exceeds_max_depth()
1876 * tb_switch_alloc() - allocate a switch
1907 upstream_port = tb_cfg_get_upstream_port(tb->ctl, route); in tb_switch_alloc()
1913 return ERR_PTR(-ENOMEM); in tb_switch_alloc()
1915 sw->tb = tb; in tb_switch_alloc()
1916 ret = tb_cfg_read(tb->ctl, &sw->config, route, 0, TB_CFG_SWITCH, 0, 5); in tb_switch_alloc()
1920 sw->generation = tb_switch_get_generation(sw); in tb_switch_alloc()
1926 sw->config.upstream_port_number = upstream_port; in tb_switch_alloc()
1927 sw->config.depth = depth; in tb_switch_alloc()
1928 sw->config.route_hi = upper_32_bits(route); in tb_switch_alloc()
1929 sw->config.route_lo = lower_32_bits(route); in tb_switch_alloc()
1930 sw->config.enabled = 0; in tb_switch_alloc()
1934 ret = -EADDRNOTAVAIL; in tb_switch_alloc()
1939 sw->ports = kcalloc(sw->config.max_port_number + 1, sizeof(*sw->ports), in tb_switch_alloc()
1941 if (!sw->ports) { in tb_switch_alloc()
1942 ret = -ENOMEM; in tb_switch_alloc()
1946 for (i = 0; i <= sw->config.max_port_number; i++) { in tb_switch_alloc()
1948 sw->ports[i].sw = sw; in tb_switch_alloc()
1949 sw->ports[i].port = i; in tb_switch_alloc()
1954 sw->cap_plug_events = ret; in tb_switch_alloc()
1958 sw->cap_lc = ret; in tb_switch_alloc()
1962 sw->authorized = true; in tb_switch_alloc()
1964 device_initialize(&sw->dev); in tb_switch_alloc()
1965 sw->dev.parent = parent; in tb_switch_alloc()
1966 sw->dev.bus = &tb_bus_type; in tb_switch_alloc()
1967 sw->dev.type = &tb_switch_type; in tb_switch_alloc()
1968 sw->dev.groups = switch_groups; in tb_switch_alloc()
1969 dev_set_name(&sw->dev, "%u-%llx", tb->index, tb_route(sw)); in tb_switch_alloc()
1974 kfree(sw->ports); in tb_switch_alloc()
1981 * tb_switch_alloc_safe_mode() - allocate a switch that is in safe mode
1987 * lacks all capabilities except DMA configuration port before it is
2001 return ERR_PTR(-ENOMEM); in tb_switch_alloc_safe_mode()
2003 sw->tb = tb; in tb_switch_alloc_safe_mode()
2004 sw->config.depth = tb_route_length(route); in tb_switch_alloc_safe_mode()
2005 sw->config.route_hi = upper_32_bits(route); in tb_switch_alloc_safe_mode()
2006 sw->config.route_lo = lower_32_bits(route); in tb_switch_alloc_safe_mode()
2007 sw->safe_mode = true; in tb_switch_alloc_safe_mode()
2009 device_initialize(&sw->dev); in tb_switch_alloc_safe_mode()
2010 sw->dev.parent = parent; in tb_switch_alloc_safe_mode()
2011 sw->dev.bus = &tb_bus_type; in tb_switch_alloc_safe_mode()
2012 sw->dev.type = &tb_switch_type; in tb_switch_alloc_safe_mode()
2013 sw->dev.groups = switch_groups; in tb_switch_alloc_safe_mode()
2014 dev_set_name(&sw->dev, "%u-%llx", tb->index, tb_route(sw)); in tb_switch_alloc_safe_mode()
2020 * tb_switch_configure() - Uploads configuration to the switch
2026 * resume from low power states to re-initialize it.
2032 struct tb *tb = sw->tb; in tb_switch_configure()
2039 sw->config.enabled ? "restoring" : "initializing", route, in tb_switch_configure()
2040 tb_route_length(route), sw->config.upstream_port_number); in tb_switch_configure()
2042 sw->config.enabled = 1; in tb_switch_configure()
2050 sw->config.cmuv = USB4_VERSION_1_0; in tb_switch_configure()
2053 ret = tb_sw_write(sw, (u32 *)&sw->config + 1, TB_CFG_SWITCH, in tb_switch_configure()
2060 if (sw->config.vendor_id != PCI_VENDOR_ID_INTEL) in tb_switch_configure()
2062 sw->config.vendor_id); in tb_switch_configure()
2064 if (!sw->cap_plug_events) { in tb_switch_configure()
2066 return -ENODEV; in tb_switch_configure()
2070 ret = tb_sw_write(sw, (u32 *)&sw->config + 1, TB_CFG_SWITCH, in tb_switch_configure()
2085 if (sw->uuid) in tb_switch_set_uuid()
2089 ret = usb4_switch_read_uid(sw, &sw->uid); in tb_switch_set_uuid()
2100 if (ret != -EINVAL) in tb_switch_set_uuid()
2113 uuid[0] = sw->uid & 0xffffffff; in tb_switch_set_uuid()
2114 uuid[1] = (sw->uid >> 32) & 0xffffffff; in tb_switch_set_uuid()
2119 sw->uuid = kmemdup(uuid, sizeof(uuid), GFP_KERNEL); in tb_switch_set_uuid()
2120 if (!sw->uuid) in tb_switch_set_uuid()
2121 return -ENOMEM; in tb_switch_set_uuid()
2130 switch (sw->generation) { in tb_switch_add_dma_port()
2145 * DMA port is the only thing available when the switch in tb_switch_add_dma_port()
2148 if (!sw->safe_mode) in tb_switch_add_dma_port()
2153 /* Root switch DMA port requires running firmware */ in tb_switch_add_dma_port()
2157 sw->dma_port = dma_port_alloc(sw); in tb_switch_add_dma_port()
2158 if (!sw->dma_port) in tb_switch_add_dma_port()
2161 if (sw->no_nvm_upgrade) in tb_switch_add_dma_port()
2182 ret = dma_port_flash_update_auth_status(sw->dma_port, &status); in tb_switch_add_dma_port()
2196 dma_port_power_cycle(sw->dma_port); in tb_switch_add_dma_port()
2202 return -ESHUTDOWN; in tb_switch_add_dma_port()
2209 for (i = 1; i <= sw->config.max_port_number; i += 2) { in tb_switch_default_link_ports()
2210 struct tb_port *port = &sw->ports[i]; in tb_switch_default_link_ports()
2217 if (i == sw->config.max_port_number || in tb_switch_default_link_ports()
2218 !tb_port_is_null(&sw->ports[i + 1])) in tb_switch_default_link_ports()
2222 subordinate = &sw->ports[i + 1]; in tb_switch_default_link_ports()
2223 if (!port->dual_link_port && !subordinate->dual_link_port) { in tb_switch_default_link_ports()
2224 port->link_nr = 0; in tb_switch_default_link_ports()
2225 port->dual_link_port = subordinate; in tb_switch_default_link_ports()
2226 subordinate->link_nr = 1; in tb_switch_default_link_ports()
2227 subordinate->dual_link_port = port; in tb_switch_default_link_ports()
2229 tb_sw_dbg(sw, "linked ports %d <-> %d\n", in tb_switch_default_link_ports()
2230 port->port, subordinate->port); in tb_switch_default_link_ports()
2239 if (!up->dual_link_port || !up->dual_link_port->remote) in tb_switch_lane_bonding_possible()
2261 if (sw->link_speed != ret) in tb_switch_update_link_attributes()
2263 sw->link_speed = ret; in tb_switch_update_link_attributes()
2268 if (sw->link_width != ret) in tb_switch_update_link_attributes()
2270 sw->link_width = ret; in tb_switch_update_link_attributes()
2273 if (device_is_registered(&sw->dev) && change) in tb_switch_update_link_attributes()
2274 kobject_uevent(&sw->dev.kobj, KOBJ_CHANGE); in tb_switch_update_link_attributes()
2280 * tb_switch_lane_bonding_enable() - Enable lane bonding
2289 struct tb_switch *parent = tb_to_switch(sw->dev.parent); in tb_switch_lane_bonding_enable()
2327 * tb_switch_lane_bonding_disable() - Disable lane bonding
2335 struct tb_switch *parent = tb_to_switch(sw->dev.parent); in tb_switch_lane_bonding_disable()
2342 if (!up->bonded) in tb_switch_lane_bonding_disable()
2355 * tb_switch_configure_link() - Set link configured
2375 if (tb_switch_is_usb4(up->sw)) in tb_switch_configure_link()
2382 down = up->remote; in tb_switch_configure_link()
2383 if (tb_switch_is_usb4(down->sw)) in tb_switch_configure_link()
2389 * tb_switch_unconfigure_link() - Unconfigure link
2399 if (sw->is_unplugged) in tb_switch_unconfigure_link()
2405 if (tb_switch_is_usb4(up->sw)) in tb_switch_unconfigure_link()
2410 down = up->remote; in tb_switch_unconfigure_link()
2411 if (tb_switch_is_usb4(down->sw)) in tb_switch_unconfigure_link()
2418 * tb_switch_add() - Add a switch to the domain
2434 * Initialize DMA control port now before we read DROM. Recent in tb_switch_add()
2437 * to the userspace. NVM can be accessed through DMA in tb_switch_add()
2442 dev_err(&sw->dev, "failed to add DMA port\n"); in tb_switch_add()
2446 if (!sw->safe_mode) { in tb_switch_add()
2450 dev_err(&sw->dev, "reading DROM failed\n"); in tb_switch_add()
2453 tb_sw_dbg(sw, "uid: %#llx\n", sw->uid); in tb_switch_add()
2457 dev_err(&sw->dev, "failed to set UUID\n"); in tb_switch_add()
2461 for (i = 0; i <= sw->config.max_port_number; i++) { in tb_switch_add()
2462 if (sw->ports[i].disabled) { in tb_switch_add()
2463 tb_port_dbg(&sw->ports[i], "disabled by eeprom\n"); in tb_switch_add()
2466 ret = tb_init_port(&sw->ports[i]); in tb_switch_add()
2468 dev_err(&sw->dev, "failed to initialize port %d\n", i); in tb_switch_add()
2484 ret = device_add(&sw->dev); in tb_switch_add()
2486 dev_err(&sw->dev, "failed to add device: %d\n", ret); in tb_switch_add()
2491 dev_info(&sw->dev, "new device found, vendor=%#x device=%#x\n", in tb_switch_add()
2492 sw->vendor, sw->device); in tb_switch_add()
2493 if (sw->vendor_name && sw->device_name) in tb_switch_add()
2494 dev_info(&sw->dev, "%s %s\n", sw->vendor_name, in tb_switch_add()
2495 sw->device_name); in tb_switch_add()
2500 dev_err(&sw->dev, "failed to add NVM devices\n"); in tb_switch_add()
2501 device_del(&sw->dev); in tb_switch_add()
2510 device_init_wakeup(&sw->dev, true); in tb_switch_add()
2512 pm_runtime_set_active(&sw->dev); in tb_switch_add()
2513 if (sw->rpm) { in tb_switch_add()
2514 pm_runtime_set_autosuspend_delay(&sw->dev, TB_AUTOSUSPEND_DELAY); in tb_switch_add()
2515 pm_runtime_use_autosuspend(&sw->dev); in tb_switch_add()
2516 pm_runtime_mark_last_busy(&sw->dev); in tb_switch_add()
2517 pm_runtime_enable(&sw->dev); in tb_switch_add()
2518 pm_request_autosuspend(&sw->dev); in tb_switch_add()
2526 * tb_switch_remove() - Remove and release a switch
2539 if (sw->rpm) { in tb_switch_remove()
2540 pm_runtime_get_sync(&sw->dev); in tb_switch_remove()
2541 pm_runtime_disable(&sw->dev); in tb_switch_remove()
2547 tb_switch_remove(port->remote->sw); in tb_switch_remove()
2548 port->remote = NULL; in tb_switch_remove()
2549 } else if (port->xdomain) { in tb_switch_remove()
2550 tb_xdomain_remove(port->xdomain); in tb_switch_remove()
2551 port->xdomain = NULL; in tb_switch_remove()
2558 if (!sw->is_unplugged) in tb_switch_remove()
2564 dev_info(&sw->dev, "device disconnected\n"); in tb_switch_remove()
2565 device_unregister(&sw->dev); in tb_switch_remove()
2569 * tb_sw_set_unplugged() - set is_unplugged on switch and downstream switches
2575 if (sw == sw->tb->root_switch) { in tb_sw_set_unplugged()
2579 if (sw->is_unplugged) { in tb_sw_set_unplugged()
2583 sw->is_unplugged = true; in tb_sw_set_unplugged()
2586 tb_sw_set_unplugged(port->remote->sw); in tb_sw_set_unplugged()
2587 else if (port->xdomain) in tb_sw_set_unplugged()
2588 port->xdomain->is_unplugged = true; in tb_sw_set_unplugged()
2623 err = tb_cfg_get_upstream_port(sw->tb->ctl, tb_route(sw)); in tb_switch_resume()
2637 if (sw->uid != uid) { in tb_switch_resume()
2639 "changed while suspended (uid %#llx -> %#llx)\n", in tb_switch_resume()
2640 sw->uid, uid); in tb_switch_resume()
2641 return -ENODEV; in tb_switch_resume()
2658 if (!tb_port_has_remote(port) && !port->xdomain) in tb_switch_resume()
2665 tb_sw_set_unplugged(port->remote->sw); in tb_switch_resume()
2666 else if (port->xdomain) in tb_switch_resume()
2667 port->xdomain->is_unplugged = true; in tb_switch_resume()
2668 } else if (tb_port_has_remote(port) || port->xdomain) { in tb_switch_resume()
2675 if (port->remote && tb_switch_resume(port->remote->sw)) { in tb_switch_resume()
2678 tb_sw_set_unplugged(port->remote->sw); in tb_switch_resume()
2686 * tb_switch_suspend() - Put a switch to sleep
2690 * Suspends router and all its children. Enables wakes according to
2691 * value of @runtime and then sets sleep bit for the router. If @sw is
2692 * host router the domain is ready to go to sleep once this function
2709 tb_switch_suspend(port->remote->sw, runtime); in tb_switch_suspend()
2716 } else if (device_may_wakeup(&sw->dev)) { in tb_switch_suspend()
2729 * tb_switch_query_dp_resource() - Query availability of DP resource
2744 * tb_switch_alloc_dp_resource() - Allocate available DP resource
2760 * tb_switch_dealloc_dp_resource() - De-allocate DP resource
2761 * @sw: Switch whose DP resource is de-allocated
2764 * De-allocates DP resource that was previously allocated for DP
2777 tb_sw_warn(sw, "failed to de-allocate DP resource for port %d\n", in tb_switch_dealloc_dp_resource()
2778 in->port); in tb_switch_dealloc_dp_resource()
2796 if (sw->tb != lookup->tb) in tb_switch_match()
2799 if (lookup->uuid) in tb_switch_match()
2800 return !memcmp(sw->uuid, lookup->uuid, sizeof(*lookup->uuid)); in tb_switch_match()
2802 if (lookup->route) { in tb_switch_match()
2803 return sw->config.route_lo == lower_32_bits(lookup->route) && in tb_switch_match()
2804 sw->config.route_hi == upper_32_bits(lookup->route); in tb_switch_match()
2808 if (!lookup->depth) in tb_switch_match()
2809 return !sw->depth; in tb_switch_match()
2811 return sw->link == lookup->link && sw->depth == lookup->depth; in tb_switch_match()
2815 * tb_switch_find_by_link_depth() - Find switch by link and depth
2841 * tb_switch_find_by_uuid() - Find switch by UUID
2865 * tb_switch_find_by_route() - Find switch by route string
2878 return tb_switch_get(tb->root_switch); in tb_switch_find_by_route()
2892 * tb_switch_find_port() - return the first port of @type on @sw or NULL
2902 if (port->config.type == type) in tb_switch_find_port()