Lines Matching full: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()
851 const struct tb_switch *sw) in tb_switch_is_reachable() argument
854 return (tb_route(parent) & mask) == (tb_route(sw) & mask); in tb_switch_is_reachable()
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()
896 next = tb_upstream_port(prev->sw); in tb_next_port_on_path()
1245 static const char *tb_switch_generation_name(const struct tb_switch *sw) in tb_switch_generation_name() argument
1247 switch (sw->generation) { in tb_switch_generation_name()
1261 static void tb_dump_switch(const struct tb *tb, const struct tb_switch *sw) in tb_dump_switch() argument
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()
1281 * @sw: Switch to reset
1285 int tb_switch_reset(struct tb_switch *sw) in tb_switch_reset() argument
1289 if (sw->generation > 1) in tb_switch_reset()
1292 tb_sw_dbg(sw, "resetting switch\n"); 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()
1311 static int tb_plug_events_active(struct tb_switch *sw, bool active) in tb_plug_events_active() argument
1316 if (tb_switch_is_icm(sw) || tb_switch_is_usb4(sw)) in tb_plug_events_active()
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()
1341 return tb_sw_write(sw, &data, TB_CFG_SWITCH, in tb_plug_events_active()
1342 sw->cap_plug_events + 1, 1); in tb_plug_events_active()
1349 struct tb_switch *sw = tb_to_switch(dev); in authorized_show() local
1351 return sprintf(buf, "%u\n", sw->authorized); in authorized_show()
1354 static int tb_switch_set_authorized(struct tb_switch *sw, unsigned int val) in tb_switch_set_authorized() argument
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()
1398 struct tb_switch *sw = tb_to_switch(dev); in authorized_store() local
1408 pm_runtime_get_sync(&sw->dev); in authorized_store()
1409 ret = tb_switch_set_authorized(sw, val); in authorized_store()
1410 pm_runtime_mark_last_busy(&sw->dev); in authorized_store()
1411 pm_runtime_put_autosuspend(&sw->dev); in authorized_store()
1420 struct tb_switch *sw = tb_to_switch(dev); in boot_show() local
1422 return sprintf(buf, "%u\n", sw->boot); in boot_show()
1429 struct tb_switch *sw = tb_to_switch(dev); in device_show() local
1431 return sprintf(buf, "%#x\n", sw->device); in device_show()
1438 struct tb_switch *sw = tb_to_switch(dev); in device_name_show() local
1440 return sprintf(buf, "%s\n", sw->device_name ? sw->device_name : ""); in device_name_show()
1447 struct tb_switch *sw = tb_to_switch(dev); in generation_show() local
1449 return sprintf(buf, "%u\n", sw->generation); in generation_show()
1456 struct tb_switch *sw = tb_to_switch(dev); in key_show() local
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()
1474 struct tb_switch *sw = tb_to_switch(dev); in key_store() local
1484 if (!mutex_trylock(&sw->tb->lock)) in key_store()
1487 if (sw->authorized) { 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()
1500 mutex_unlock(&sw->tb->lock); in key_store()
1508 struct tb_switch *sw = tb_to_switch(dev); in speed_show() local
1510 return sprintf(buf, "%u.0 Gb/s\n", sw->link_speed); in speed_show()
1523 struct tb_switch *sw = tb_to_switch(dev); in lanes_show() local
1525 return sprintf(buf, "%u\n", sw->link_width); in lanes_show()
1538 struct tb_switch *sw = tb_to_switch(dev); in nvm_authenticate_show() local
1541 nvm_get_auth_status(sw, &status); in nvm_authenticate_show()
1548 struct tb_switch *sw = tb_to_switch(dev); in nvm_authenticate_sysfs() local
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()
1570 nvm_clear_auth_status(sw); in nvm_authenticate_sysfs()
1573 if (!sw->nvm->flushed) { in nvm_authenticate_sysfs()
1574 if (!sw->nvm->buf) { in nvm_authenticate_sysfs()
1579 ret = nvm_validate_and_write(sw); in nvm_authenticate_sysfs()
1585 ret = tb_lc_force_power(sw); in nvm_authenticate_sysfs()
1587 sw->nvm->authenticating = true; in nvm_authenticate_sysfs()
1588 ret = nvm_authenticate(sw); 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()
1631 struct tb_switch *sw = tb_to_switch(dev); in nvm_version_show() local
1634 if (!mutex_trylock(&sw->tb->lock)) in nvm_version_show()
1637 if (sw->safe_mode) in nvm_version_show()
1639 else if (!sw->nvm) 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()
1653 struct tb_switch *sw = tb_to_switch(dev); in vendor_show() local
1655 return sprintf(buf, "%#x\n", sw->vendor); in vendor_show()
1662 struct tb_switch *sw = tb_to_switch(dev); in vendor_name_show() local
1664 return sprintf(buf, "%s\n", sw->vendor_name ? sw->vendor_name : ""); in vendor_name_show()
1671 struct tb_switch *sw = tb_to_switch(dev); in unique_id_show() local
1673 return sprintf(buf, "%pUb\n", sw->uuid); in unique_id_show()
1701 struct tb_switch *sw = tb_to_switch(dev); in switch_attr_is_visible() local
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()
1716 if (tb_route(sw) && 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()
1725 if (tb_route(sw)) in switch_attr_is_visible()
1729 if (nvm_upgradeable(sw)) in switch_attr_is_visible()
1733 if (nvm_readable(sw)) in switch_attr_is_visible()
1737 if (tb_route(sw)) in switch_attr_is_visible()
1741 if (sw->quirks & QUIRK_FORCE_POWER_LINK_CONTROLLER) in switch_attr_is_visible()
1746 return sw->safe_mode ? 0 : attr->mode; in switch_attr_is_visible()
1761 struct tb_switch *sw = tb_to_switch(dev); in tb_switch_release() local
1764 dma_port_free(sw->dma_port); in tb_switch_release()
1766 tb_switch_for_each_port(sw, port) { 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()
1779 kfree(sw); in tb_switch_release()
1788 struct tb_switch *sw = tb_to_switch(dev); in tb_switch_runtime_suspend() local
1789 const struct tb_cm_ops *cm_ops = sw->tb->cm_ops; in tb_switch_runtime_suspend()
1792 return cm_ops->runtime_suspend_switch(sw); in tb_switch_runtime_suspend()
1799 struct tb_switch *sw = tb_to_switch(dev); in tb_switch_runtime_resume() local
1800 const struct tb_cm_ops *cm_ops = sw->tb->cm_ops; in tb_switch_runtime_resume()
1803 return cm_ops->runtime_resume_switch(sw); in tb_switch_runtime_resume()
1818 static int tb_switch_get_generation(struct tb_switch *sw) in tb_switch_get_generation() argument
1820 switch (sw->config.device_id) { in tb_switch_get_generation()
1849 if (tb_switch_is_usb4(sw)) in tb_switch_get_generation()
1856 tb_sw_warn(sw, "unsupported switch device id %#x\n", in tb_switch_get_generation()
1857 sw->config.device_id); in tb_switch_get_generation()
1862 static bool tb_switch_exceeds_max_depth(const struct tb_switch *sw, int depth) in tb_switch_exceeds_max_depth() argument
1866 if (tb_switch_is_usb4(sw) || in tb_switch_exceeds_max_depth()
1867 (sw->tb->root_switch && tb_switch_is_usb4(sw->tb->root_switch))) in tb_switch_exceeds_max_depth()
1892 struct tb_switch *sw; in tb_switch_alloc() local
1911 sw = kzalloc(sizeof(*sw), GFP_KERNEL); in tb_switch_alloc()
1912 if (!sw) 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()
1923 tb_dump_switch(tb, 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()
1933 if (tb_switch_exceeds_max_depth(sw, depth)) { 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()
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()
1952 ret = tb_switch_find_vse_cap(sw, TB_VSE_CAP_PLUG_EVENTS); in tb_switch_alloc()
1954 sw->cap_plug_events = ret; in tb_switch_alloc()
1956 ret = tb_switch_find_vse_cap(sw, TB_VSE_CAP_LINK_CONTROLLER); 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()
1971 return sw; in tb_switch_alloc()
1974 kfree(sw->ports); in tb_switch_alloc()
1975 kfree(sw); in tb_switch_alloc()
1997 struct tb_switch *sw; in tb_switch_alloc_safe_mode() local
1999 sw = kzalloc(sizeof(*sw), GFP_KERNEL); in tb_switch_alloc_safe_mode()
2000 if (!sw) 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()
2016 return sw; in tb_switch_alloc_safe_mode()
2021 * @sw: Switch to configure
2030 int tb_switch_configure(struct tb_switch *sw) in tb_switch_configure() argument
2032 struct tb *tb = sw->tb; in tb_switch_configure()
2036 route = tb_route(sw); 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()
2044 if (tb_switch_is_usb4(sw)) { 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()
2058 ret = usb4_switch_setup(sw); in tb_switch_configure()
2060 if (sw->config.vendor_id != PCI_VENDOR_ID_INTEL) in tb_switch_configure()
2061 tb_sw_warn(sw, "unknown switch vendor id %#x\n", in tb_switch_configure()
2062 sw->config.vendor_id); in tb_switch_configure()
2064 if (!sw->cap_plug_events) { in tb_switch_configure()
2065 tb_sw_warn(sw, "cannot find TB_VSE_CAP_PLUG_EVENTS aborting\n"); in tb_switch_configure()
2070 ret = tb_sw_write(sw, (u32 *)&sw->config + 1, TB_CFG_SWITCH, in tb_switch_configure()
2076 return tb_plug_events_active(sw, true); in tb_switch_configure()
2079 static int tb_switch_set_uuid(struct tb_switch *sw) in tb_switch_set_uuid() argument
2085 if (sw->uuid) in tb_switch_set_uuid()
2088 if (tb_switch_is_usb4(sw)) { in tb_switch_set_uuid()
2089 ret = usb4_switch_read_uid(sw, &sw->uid); in tb_switch_set_uuid()
2098 ret = tb_lc_read_uuid(sw, uuid); 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()
2125 static int tb_switch_add_dma_port(struct tb_switch *sw) in tb_switch_add_dma_port() argument
2130 switch (sw->generation) { in tb_switch_add_dma_port()
2133 if (tb_route(sw)) in tb_switch_add_dma_port()
2138 ret = tb_switch_set_uuid(sw); in tb_switch_add_dma_port()
2148 if (!sw->safe_mode) in tb_switch_add_dma_port()
2154 if (!tb_route(sw) && !tb_switch_is_icm(sw)) 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()
2170 nvm_get_auth_status(sw, &status); in tb_switch_add_dma_port()
2172 if (!tb_route(sw)) in tb_switch_add_dma_port()
2173 nvm_authenticate_complete_dma_port(sw); in tb_switch_add_dma_port()
2182 ret = dma_port_flash_update_auth_status(sw->dma_port, &status); in tb_switch_add_dma_port()
2187 if (!tb_route(sw)) in tb_switch_add_dma_port()
2188 nvm_authenticate_complete_dma_port(sw); in tb_switch_add_dma_port()
2191 tb_sw_info(sw, "switch flash authentication failed\n"); in tb_switch_add_dma_port()
2192 nvm_set_auth_status(sw, status); in tb_switch_add_dma_port()
2195 tb_sw_info(sw, "power cycling the switch now\n"); in tb_switch_add_dma_port()
2196 dma_port_power_cycle(sw->dma_port); in tb_switch_add_dma_port()
2205 static void tb_switch_default_link_ports(struct tb_switch *sw) in tb_switch_default_link_ports() argument
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()
2229 tb_sw_dbg(sw, "linked ports %d <-> %d\n", in tb_switch_default_link_ports()
2235 static bool tb_switch_lane_bonding_possible(struct tb_switch *sw) in tb_switch_lane_bonding_possible() argument
2237 const struct tb_port *up = tb_upstream_port(sw); in tb_switch_lane_bonding_possible()
2242 if (tb_switch_is_usb4(sw)) in tb_switch_lane_bonding_possible()
2243 return usb4_switch_lane_bonding_possible(sw); in tb_switch_lane_bonding_possible()
2244 return tb_lc_lane_bonding_possible(sw); in tb_switch_lane_bonding_possible()
2247 static int tb_switch_update_link_attributes(struct tb_switch *sw) in tb_switch_update_link_attributes() argument
2253 if (!tb_route(sw) || tb_switch_is_icm(sw)) in tb_switch_update_link_attributes()
2256 up = tb_upstream_port(sw); in tb_switch_update_link_attributes()
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()
2281 * @sw: Switch to enable lane bonding
2287 int tb_switch_lane_bonding_enable(struct tb_switch *sw) in tb_switch_lane_bonding_enable() argument
2289 struct tb_switch *parent = tb_to_switch(sw->dev.parent); in tb_switch_lane_bonding_enable()
2291 u64 route = tb_route(sw); in tb_switch_lane_bonding_enable()
2297 if (!tb_switch_lane_bonding_possible(sw)) in tb_switch_lane_bonding_enable()
2300 up = tb_upstream_port(sw); in tb_switch_lane_bonding_enable()
2320 tb_switch_update_link_attributes(sw); in tb_switch_lane_bonding_enable()
2322 tb_sw_dbg(sw, "lane bonding enabled\n"); in tb_switch_lane_bonding_enable()
2328 * @sw: Switch whose lane bonding to disable
2330 * Disables lane bonding between @sw and parent. This can be called even
2333 void tb_switch_lane_bonding_disable(struct tb_switch *sw) in tb_switch_lane_bonding_disable() argument
2335 struct tb_switch *parent = tb_to_switch(sw->dev.parent); in tb_switch_lane_bonding_disable()
2338 if (!tb_route(sw)) in tb_switch_lane_bonding_disable()
2341 up = tb_upstream_port(sw); in tb_switch_lane_bonding_disable()
2345 down = tb_port_at(tb_route(sw), parent); in tb_switch_lane_bonding_disable()
2350 tb_switch_update_link_attributes(sw); in tb_switch_lane_bonding_disable()
2351 tb_sw_dbg(sw, "lane bonding disabled\n"); in tb_switch_lane_bonding_disable()
2356 * @sw: Switch whose link is configured
2358 * Sets the link upstream from @sw configured (from both ends) so that
2366 int tb_switch_configure_link(struct tb_switch *sw) in tb_switch_configure_link() argument
2371 if (!tb_route(sw) || tb_switch_is_icm(sw)) in tb_switch_configure_link()
2374 up = tb_upstream_port(sw); in tb_switch_configure_link()
2375 if (tb_switch_is_usb4(up->sw)) in tb_switch_configure_link()
2383 if (tb_switch_is_usb4(down->sw)) in tb_switch_configure_link()
2390 * @sw: Switch whose link is unconfigured
2392 * Sets the link unconfigured so the @sw will be disconnected if the
2395 void tb_switch_unconfigure_link(struct tb_switch *sw) in tb_switch_unconfigure_link() argument
2399 if (sw->is_unplugged) in tb_switch_unconfigure_link()
2401 if (!tb_route(sw) || tb_switch_is_icm(sw)) in tb_switch_unconfigure_link()
2404 up = tb_upstream_port(sw); in tb_switch_unconfigure_link()
2405 if (tb_switch_is_usb4(up->sw)) in tb_switch_unconfigure_link()
2411 if (tb_switch_is_usb4(down->sw)) in tb_switch_unconfigure_link()
2419 * @sw: Switch to add
2429 int tb_switch_add(struct tb_switch *sw) in tb_switch_add() argument
2440 ret = tb_switch_add_dma_port(sw); 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()
2448 ret = tb_drom_read(sw); 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()
2455 ret = tb_switch_set_uuid(sw); 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()
2473 tb_switch_default_link_ports(sw); in tb_switch_add()
2475 ret = tb_switch_update_link_attributes(sw); in tb_switch_add()
2479 ret = tb_switch_tmu_init(sw); 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()
2490 if (tb_route(sw)) { 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()
2498 ret = tb_switch_nvm_add(sw); 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()
2521 tb_switch_debugfs_init(sw); in tb_switch_add()
2527 * @sw: Switch to remove
2533 void tb_switch_remove(struct tb_switch *sw) in tb_switch_remove() argument
2537 tb_switch_debugfs_remove(sw); in tb_switch_remove()
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()
2545 tb_switch_for_each_port(sw, port) { in tb_switch_remove()
2547 tb_switch_remove(port->remote->sw); in tb_switch_remove()
2558 if (!sw->is_unplugged) in tb_switch_remove()
2559 tb_plug_events_active(sw, false); in tb_switch_remove()
2561 tb_switch_nvm_remove(sw); in tb_switch_remove()
2563 if (tb_route(sw)) 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()
2571 void tb_sw_set_unplugged(struct tb_switch *sw) in tb_sw_set_unplugged() argument
2575 if (sw == sw->tb->root_switch) { in tb_sw_set_unplugged()
2576 tb_sw_WARN(sw, "cannot unplug root switch\n"); in tb_sw_set_unplugged()
2579 if (sw->is_unplugged) { in tb_sw_set_unplugged()
2580 tb_sw_WARN(sw, "is_unplugged already set\n"); in tb_sw_set_unplugged()
2583 sw->is_unplugged = true; in tb_sw_set_unplugged()
2584 tb_switch_for_each_port(sw, port) { in tb_sw_set_unplugged()
2586 tb_sw_set_unplugged(port->remote->sw); in tb_sw_set_unplugged()
2592 static int tb_switch_set_wake(struct tb_switch *sw, unsigned int flags) in tb_switch_set_wake() argument
2595 tb_sw_dbg(sw, "enabling wakeup: %#x\n", flags); in tb_switch_set_wake()
2597 tb_sw_dbg(sw, "disabling wakeup\n"); in tb_switch_set_wake()
2599 if (tb_switch_is_usb4(sw)) in tb_switch_set_wake()
2600 return usb4_switch_set_wake(sw, flags); in tb_switch_set_wake()
2601 return tb_lc_set_wake(sw, flags); in tb_switch_set_wake()
2604 int tb_switch_resume(struct tb_switch *sw) in tb_switch_resume() argument
2609 tb_sw_dbg(sw, "resuming switch\n"); in tb_switch_resume()
2615 if (tb_route(sw)) { in tb_switch_resume()
2623 err = tb_cfg_get_upstream_port(sw->tb->ctl, tb_route(sw)); in tb_switch_resume()
2625 tb_sw_info(sw, "switch not present anymore\n"); in tb_switch_resume()
2629 if (tb_switch_is_usb4(sw)) in tb_switch_resume()
2630 err = usb4_switch_read_uid(sw, &uid); in tb_switch_resume()
2632 err = tb_drom_read_uid_only(sw, &uid); in tb_switch_resume()
2634 tb_sw_warn(sw, "uid read failed\n"); in tb_switch_resume()
2637 if (sw->uid != uid) { in tb_switch_resume()
2638 tb_sw_info(sw, in tb_switch_resume()
2640 sw->uid, uid); in tb_switch_resume()
2645 err = tb_switch_configure(sw); in tb_switch_resume()
2650 tb_switch_set_wake(sw, 0); in tb_switch_resume()
2652 err = tb_switch_tmu_init(sw); in tb_switch_resume()
2657 tb_switch_for_each_port(sw, port) { in tb_switch_resume()
2665 tb_sw_set_unplugged(port->remote->sw); 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()
2687 * @sw: Switch to suspend
2691 * value of @runtime and then sets sleep bit for the router. If @sw is
2695 void tb_switch_suspend(struct tb_switch *sw, bool runtime) in tb_switch_suspend() argument
2701 tb_sw_dbg(sw, "suspending switch\n"); in tb_switch_suspend()
2703 err = tb_plug_events_active(sw, false); in tb_switch_suspend()
2707 tb_switch_for_each_port(sw, port) { in tb_switch_suspend()
2709 tb_switch_suspend(port->remote->sw, runtime); in tb_switch_suspend()
2716 } else if (device_may_wakeup(&sw->dev)) { in tb_switch_suspend()
2720 tb_switch_set_wake(sw, flags); in tb_switch_suspend()
2722 if (tb_switch_is_usb4(sw)) in tb_switch_suspend()
2723 usb4_switch_set_sleep(sw); in tb_switch_suspend()
2725 tb_lc_set_sleep(sw); in tb_switch_suspend()
2730 * @sw: Switch whose DP resource is queried
2736 bool tb_switch_query_dp_resource(struct tb_switch *sw, struct tb_port *in) in tb_switch_query_dp_resource() argument
2738 if (tb_switch_is_usb4(sw)) in tb_switch_query_dp_resource()
2739 return usb4_switch_query_dp_resource(sw, in); in tb_switch_query_dp_resource()
2740 return tb_lc_dp_sink_query(sw, in); in tb_switch_query_dp_resource()
2745 * @sw: Switch whose DP resource is allocated
2752 int tb_switch_alloc_dp_resource(struct tb_switch *sw, struct tb_port *in) in tb_switch_alloc_dp_resource() argument
2754 if (tb_switch_is_usb4(sw)) in tb_switch_alloc_dp_resource()
2755 return usb4_switch_alloc_dp_resource(sw, in); in tb_switch_alloc_dp_resource()
2756 return tb_lc_dp_sink_alloc(sw, in); in tb_switch_alloc_dp_resource()
2761 * @sw: Switch whose DP resource is de-allocated
2767 void tb_switch_dealloc_dp_resource(struct tb_switch *sw, struct tb_port *in) in tb_switch_dealloc_dp_resource() argument
2771 if (tb_switch_is_usb4(sw)) in tb_switch_dealloc_dp_resource()
2772 ret = usb4_switch_dealloc_dp_resource(sw, in); in tb_switch_dealloc_dp_resource()
2774 ret = tb_lc_dp_sink_dealloc(sw, in); in tb_switch_dealloc_dp_resource()
2777 tb_sw_warn(sw, "failed to de-allocate DP resource for port %d\n", in tb_switch_dealloc_dp_resource()
2791 struct tb_switch *sw = tb_to_switch(dev); in tb_switch_match() local
2794 if (!sw) in tb_switch_match()
2796 if (sw->tb != lookup->tb) in tb_switch_match()
2800 return !memcmp(sw->uuid, lookup->uuid, sizeof(*lookup->uuid)); 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()
2809 return !sw->depth; in tb_switch_match()
2811 return sw->link == lookup->link && sw->depth == lookup->depth; in tb_switch_match()
2892 * tb_switch_find_port() - return the first port of @type on @sw or NULL
2893 * @sw: Switch to find the port from
2896 struct tb_port *tb_switch_find_port(struct tb_switch *sw, in tb_switch_find_port() argument
2901 tb_switch_for_each_port(sw, port) { in tb_switch_find_port()