Lines Matching refs:sw
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()
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()
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()
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()
113 image_size = sw->nvm->buf_data_size; 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()
148 if (sw->generation < 3) { in nvm_validate_and_write()
150 ret = dma_port_flash_write(sw->dma_port, 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
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()
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()
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()
236 ret = dma_port_flash_update_auth_status(sw->dma_port, &status); 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()
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()
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()
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()
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()
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()
416 ret = nvm_read(sw, NVM_VERSION, &val, sizeof(val)); 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()
455 nvm_clear_auth_status(sw); in tb_switch_nvm_remove()
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()
667 if (tb_switch_is_icm(port->sw)) in tb_port_unlock()
671 if (tb_switch_is_usb4(port->sw)) in tb_port_unlock()
736 tb_dbg(port->sw->tb, " Port %d: not implemented\n", in tb_init_port()
762 tb_dump_port(port->sw->tb, &port->config); in tb_init_port()
845 const struct tb_switch *sw) in tb_switch_is_reachable() argument
848 return (tb_route(parent) & mask) == (tb_route(sw) & mask); in tb_switch_is_reachable()
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()
890 next = tb_upstream_port(prev->sw); in tb_next_port_on_path()
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()
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()
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
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
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
1478 if (!mutex_trylock(&sw->tb->lock)) in key_store()
1481 if (sw->authorized) { 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()
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()
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()
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()
1633 else if (!sw->nvm) 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()
1719 if (tb_route(sw)) in switch_attr_is_visible()
1723 if (nvm_upgradeable(sw)) in switch_attr_is_visible()
1727 if (nvm_readable(sw)) in switch_attr_is_visible()
1731 if (tb_route(sw)) in switch_attr_is_visible()
1735 if (sw->quirks & QUIRK_FORCE_POWER_LINK_CONTROLLER) 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()
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()
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()
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()
1884 struct tb_switch *sw; in tb_switch_alloc() local
1903 sw = kzalloc(sizeof(*sw), GFP_KERNEL); in tb_switch_alloc()
1904 if (!sw) 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()
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()
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()
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()
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()
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()
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()
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()
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()
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()
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()
2228 tb_sw_dbg(sw, "linked ports %d <-> %d\n", 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()
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()
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()
2319 tb_switch_update_link_attributes(sw); in tb_switch_lane_bonding_enable()
2321 tb_sw_dbg(sw, "lane bonding enabled\n"); in tb_switch_lane_bonding_enable()
2332 void tb_switch_lane_bonding_disable(struct tb_switch *sw) in tb_switch_lane_bonding_disable() argument
2334 struct tb_switch *parent = tb_to_switch(sw->dev.parent); in tb_switch_lane_bonding_disable()
2337 if (!tb_route(sw)) in tb_switch_lane_bonding_disable()
2340 up = tb_upstream_port(sw); in tb_switch_lane_bonding_disable()
2344 down = tb_port_at(tb_route(sw), parent); in tb_switch_lane_bonding_disable()
2349 tb_switch_update_link_attributes(sw); in tb_switch_lane_bonding_disable()
2350 tb_sw_dbg(sw, "lane bonding disabled\n"); in tb_switch_lane_bonding_disable()
2365 int tb_switch_configure_link(struct tb_switch *sw) in tb_switch_configure_link() argument
2370 if (!tb_route(sw) || tb_switch_is_icm(sw)) in tb_switch_configure_link()
2373 up = tb_upstream_port(sw); in tb_switch_configure_link()
2374 if (tb_switch_is_usb4(up->sw)) in tb_switch_configure_link()
2382 if (tb_switch_is_usb4(down->sw)) in tb_switch_configure_link()
2394 void tb_switch_unconfigure_link(struct tb_switch *sw) in tb_switch_unconfigure_link() argument
2398 if (sw->is_unplugged) in tb_switch_unconfigure_link()
2400 if (!tb_route(sw) || tb_switch_is_icm(sw)) in tb_switch_unconfigure_link()
2403 up = tb_upstream_port(sw); in tb_switch_unconfigure_link()
2404 if (tb_switch_is_usb4(up->sw)) in tb_switch_unconfigure_link()
2410 if (tb_switch_is_usb4(down->sw)) in tb_switch_unconfigure_link()
2416 static int tb_switch_port_hotplug_enable(struct tb_switch *sw) in tb_switch_port_hotplug_enable() argument
2420 if (tb_switch_is_icm(sw)) in tb_switch_port_hotplug_enable()
2423 tb_switch_for_each_port(sw, port) { in tb_switch_port_hotplug_enable()
2448 int tb_switch_add(struct tb_switch *sw) in tb_switch_add() argument
2459 ret = tb_switch_add_dma_port(sw); in tb_switch_add()
2461 dev_err(&sw->dev, "failed to add DMA port\n"); in tb_switch_add()
2465 if (!sw->safe_mode) { in tb_switch_add()
2467 ret = tb_drom_read(sw); in tb_switch_add()
2469 dev_err(&sw->dev, "reading DROM failed\n"); in tb_switch_add()
2472 tb_sw_dbg(sw, "uid: %#llx\n", sw->uid); in tb_switch_add()
2474 ret = tb_switch_set_uuid(sw); in tb_switch_add()
2476 dev_err(&sw->dev, "failed to set UUID\n"); in tb_switch_add()
2480 for (i = 0; i <= sw->config.max_port_number; i++) { in tb_switch_add()
2481 if (sw->ports[i].disabled) { in tb_switch_add()
2482 tb_port_dbg(&sw->ports[i], "disabled by eeprom\n"); in tb_switch_add()
2485 ret = tb_init_port(&sw->ports[i]); in tb_switch_add()
2487 dev_err(&sw->dev, "failed to initialize port %d\n", i); in tb_switch_add()
2492 tb_switch_default_link_ports(sw); in tb_switch_add()
2494 ret = tb_switch_update_link_attributes(sw); in tb_switch_add()
2498 ret = tb_switch_tmu_init(sw); in tb_switch_add()
2503 ret = tb_switch_port_hotplug_enable(sw); in tb_switch_add()
2507 ret = device_add(&sw->dev); in tb_switch_add()
2509 dev_err(&sw->dev, "failed to add device: %d\n", ret); in tb_switch_add()
2513 if (tb_route(sw)) { in tb_switch_add()
2514 dev_info(&sw->dev, "new device found, vendor=%#x device=%#x\n", in tb_switch_add()
2515 sw->vendor, sw->device); in tb_switch_add()
2516 if (sw->vendor_name && sw->device_name) in tb_switch_add()
2517 dev_info(&sw->dev, "%s %s\n", sw->vendor_name, in tb_switch_add()
2518 sw->device_name); in tb_switch_add()
2521 ret = tb_switch_nvm_add(sw); in tb_switch_add()
2523 dev_err(&sw->dev, "failed to add NVM devices\n"); in tb_switch_add()
2524 device_del(&sw->dev); in tb_switch_add()
2533 device_init_wakeup(&sw->dev, true); in tb_switch_add()
2535 pm_runtime_set_active(&sw->dev); in tb_switch_add()
2536 if (sw->rpm) { in tb_switch_add()
2537 pm_runtime_set_autosuspend_delay(&sw->dev, TB_AUTOSUSPEND_DELAY); in tb_switch_add()
2538 pm_runtime_use_autosuspend(&sw->dev); in tb_switch_add()
2539 pm_runtime_mark_last_busy(&sw->dev); in tb_switch_add()
2540 pm_runtime_enable(&sw->dev); in tb_switch_add()
2541 pm_request_autosuspend(&sw->dev); in tb_switch_add()
2544 tb_switch_debugfs_init(sw); in tb_switch_add()
2556 void tb_switch_remove(struct tb_switch *sw) in tb_switch_remove() argument
2560 tb_switch_debugfs_remove(sw); in tb_switch_remove()
2562 if (sw->rpm) { in tb_switch_remove()
2563 pm_runtime_get_sync(&sw->dev); in tb_switch_remove()
2564 pm_runtime_disable(&sw->dev); in tb_switch_remove()
2568 tb_switch_for_each_port(sw, port) { in tb_switch_remove()
2570 tb_switch_remove(port->remote->sw); in tb_switch_remove()
2581 if (!sw->is_unplugged) in tb_switch_remove()
2582 tb_plug_events_active(sw, false); in tb_switch_remove()
2584 tb_switch_nvm_remove(sw); in tb_switch_remove()
2586 if (tb_route(sw)) in tb_switch_remove()
2587 dev_info(&sw->dev, "device disconnected\n"); in tb_switch_remove()
2588 device_unregister(&sw->dev); in tb_switch_remove()
2594 void tb_sw_set_unplugged(struct tb_switch *sw) in tb_sw_set_unplugged() argument
2598 if (sw == sw->tb->root_switch) { in tb_sw_set_unplugged()
2599 tb_sw_WARN(sw, "cannot unplug root switch\n"); in tb_sw_set_unplugged()
2602 if (sw->is_unplugged) { in tb_sw_set_unplugged()
2603 tb_sw_WARN(sw, "is_unplugged already set\n"); in tb_sw_set_unplugged()
2606 sw->is_unplugged = true; in tb_sw_set_unplugged()
2607 tb_switch_for_each_port(sw, port) { in tb_sw_set_unplugged()
2609 tb_sw_set_unplugged(port->remote->sw); in tb_sw_set_unplugged()
2615 static int tb_switch_set_wake(struct tb_switch *sw, unsigned int flags) in tb_switch_set_wake() argument
2618 tb_sw_dbg(sw, "enabling wakeup: %#x\n", flags); in tb_switch_set_wake()
2620 tb_sw_dbg(sw, "disabling wakeup\n"); in tb_switch_set_wake()
2622 if (tb_switch_is_usb4(sw)) in tb_switch_set_wake()
2623 return usb4_switch_set_wake(sw, flags); in tb_switch_set_wake()
2624 return tb_lc_set_wake(sw, flags); in tb_switch_set_wake()
2627 int tb_switch_resume(struct tb_switch *sw) in tb_switch_resume() argument
2632 tb_sw_dbg(sw, "resuming switch\n"); in tb_switch_resume()
2638 if (tb_route(sw)) { in tb_switch_resume()
2646 err = tb_cfg_get_upstream_port(sw->tb->ctl, tb_route(sw)); in tb_switch_resume()
2648 tb_sw_info(sw, "switch not present anymore\n"); in tb_switch_resume()
2652 if (tb_switch_is_usb4(sw)) in tb_switch_resume()
2653 err = usb4_switch_read_uid(sw, &uid); in tb_switch_resume()
2655 err = tb_drom_read_uid_only(sw, &uid); in tb_switch_resume()
2657 tb_sw_warn(sw, "uid read failed\n"); in tb_switch_resume()
2660 if (sw->uid != uid) { in tb_switch_resume()
2661 tb_sw_info(sw, in tb_switch_resume()
2663 sw->uid, uid); in tb_switch_resume()
2668 err = tb_switch_configure(sw); in tb_switch_resume()
2673 tb_switch_set_wake(sw, 0); in tb_switch_resume()
2675 err = tb_switch_tmu_init(sw); in tb_switch_resume()
2680 tb_switch_for_each_port(sw, port) { in tb_switch_resume()
2688 tb_sw_set_unplugged(port->remote->sw); in tb_switch_resume()
2698 if (port->remote && tb_switch_resume(port->remote->sw)) { in tb_switch_resume()
2701 tb_sw_set_unplugged(port->remote->sw); in tb_switch_resume()
2718 void tb_switch_suspend(struct tb_switch *sw, bool runtime) in tb_switch_suspend() argument
2724 tb_sw_dbg(sw, "suspending switch\n"); in tb_switch_suspend()
2726 err = tb_plug_events_active(sw, false); in tb_switch_suspend()
2730 tb_switch_for_each_port(sw, port) { in tb_switch_suspend()
2732 tb_switch_suspend(port->remote->sw, runtime); in tb_switch_suspend()
2739 } else if (device_may_wakeup(&sw->dev)) { in tb_switch_suspend()
2743 tb_switch_set_wake(sw, flags); in tb_switch_suspend()
2745 if (tb_switch_is_usb4(sw)) in tb_switch_suspend()
2746 usb4_switch_set_sleep(sw); in tb_switch_suspend()
2748 tb_lc_set_sleep(sw); in tb_switch_suspend()
2759 bool tb_switch_query_dp_resource(struct tb_switch *sw, struct tb_port *in) in tb_switch_query_dp_resource() argument
2761 if (tb_switch_is_usb4(sw)) in tb_switch_query_dp_resource()
2762 return usb4_switch_query_dp_resource(sw, in); in tb_switch_query_dp_resource()
2763 return tb_lc_dp_sink_query(sw, in); in tb_switch_query_dp_resource()
2775 int tb_switch_alloc_dp_resource(struct tb_switch *sw, struct tb_port *in) in tb_switch_alloc_dp_resource() argument
2777 if (tb_switch_is_usb4(sw)) in tb_switch_alloc_dp_resource()
2778 return usb4_switch_alloc_dp_resource(sw, in); in tb_switch_alloc_dp_resource()
2779 return tb_lc_dp_sink_alloc(sw, in); in tb_switch_alloc_dp_resource()
2790 void tb_switch_dealloc_dp_resource(struct tb_switch *sw, struct tb_port *in) in tb_switch_dealloc_dp_resource() argument
2794 if (tb_switch_is_usb4(sw)) in tb_switch_dealloc_dp_resource()
2795 ret = usb4_switch_dealloc_dp_resource(sw, in); in tb_switch_dealloc_dp_resource()
2797 ret = tb_lc_dp_sink_dealloc(sw, in); in tb_switch_dealloc_dp_resource()
2800 tb_sw_warn(sw, "failed to de-allocate DP resource for port %d\n", in tb_switch_dealloc_dp_resource()
2814 struct tb_switch *sw = tb_to_switch(dev); in tb_switch_match() local
2817 if (!sw) in tb_switch_match()
2819 if (sw->tb != lookup->tb) in tb_switch_match()
2823 return !memcmp(sw->uuid, lookup->uuid, sizeof(*lookup->uuid)); in tb_switch_match()
2826 return sw->config.route_lo == lower_32_bits(lookup->route) && in tb_switch_match()
2827 sw->config.route_hi == upper_32_bits(lookup->route); in tb_switch_match()
2832 return !sw->depth; in tb_switch_match()
2834 return sw->link == lookup->link && sw->depth == lookup->depth; in tb_switch_match()
2919 struct tb_port *tb_switch_find_port(struct tb_switch *sw, in tb_switch_find_port() argument
2924 tb_switch_for_each_port(sw, port) { in tb_switch_find_port()