Lines Matching full:sw
37 static struct nvm_auth_status *__nvm_get_auth_status(const struct tb_switch *sw) in __nvm_get_auth_status() argument
42 if (uuid_equal(&st->uuid, sw->uuid)) in __nvm_get_auth_status()
49 static void nvm_get_auth_status(const struct tb_switch *sw, u32 *status) in nvm_get_auth_status() argument
54 st = __nvm_get_auth_status(sw); in nvm_get_auth_status()
60 static void nvm_set_auth_status(const struct tb_switch *sw, u32 status) in nvm_set_auth_status() argument
64 if (WARN_ON(!sw->uuid)) in nvm_set_auth_status()
68 st = __nvm_get_auth_status(sw); in nvm_set_auth_status()
75 memcpy(&st->uuid, sw->uuid, sizeof(st->uuid)); in nvm_set_auth_status()
85 static void nvm_clear_auth_status(const struct tb_switch *sw) in nvm_clear_auth_status() argument
90 st = __nvm_get_auth_status(sw); in nvm_clear_auth_status()
98 static int nvm_validate_and_write(struct tb_switch *sw) in nvm_validate_and_write() argument
101 const u8 *buf = sw->nvm->buf; in nvm_validate_and_write()
108 image_size = sw->nvm->buf_data_size; in nvm_validate_and_write()
132 if (!sw->safe_mode) { in nvm_validate_and_write()
140 if (device_id != sw->config.device_id) in nvm_validate_and_write()
143 if (sw->generation < 3) { in nvm_validate_and_write()
145 ret = dma_port_flash_write(sw->dma_port, in nvm_validate_and_write()
157 if (tb_switch_is_usb4(sw)) in nvm_validate_and_write()
158 ret = usb4_switch_nvm_write(sw, 0, buf, image_size); in nvm_validate_and_write()
160 ret = dma_port_flash_write(sw->dma_port, 0, buf, image_size); in nvm_validate_and_write()
162 sw->nvm->flushed = true; in nvm_validate_and_write()
166 static int nvm_authenticate_host_dma_port(struct tb_switch *sw) in nvm_authenticate_host_dma_port() argument
175 if (!sw->safe_mode) { in nvm_authenticate_host_dma_port()
178 ret = tb_domain_disconnect_all_paths(sw->tb); in nvm_authenticate_host_dma_port()
185 ret = dma_port_flash_update_auth(sw->dma_port); in nvm_authenticate_host_dma_port()
193 tb_sw_warn(sw, "failed to authenticate NVM, power cycling\n"); in nvm_authenticate_host_dma_port()
194 if (dma_port_flash_update_auth_status(sw->dma_port, &status) > 0) in nvm_authenticate_host_dma_port()
195 nvm_set_auth_status(sw, status); in nvm_authenticate_host_dma_port()
202 dma_port_power_cycle(sw->dma_port); in nvm_authenticate_host_dma_port()
206 static int nvm_authenticate_device_dma_port(struct tb_switch *sw) in nvm_authenticate_device_dma_port() argument
210 ret = dma_port_flash_update_auth(sw->dma_port); in nvm_authenticate_device_dma_port()
231 ret = dma_port_flash_update_auth_status(sw->dma_port, &status); in nvm_authenticate_device_dma_port()
236 tb_sw_warn(sw, "failed to authenticate NVM\n"); in nvm_authenticate_device_dma_port()
237 nvm_set_auth_status(sw, status); in nvm_authenticate_device_dma_port()
240 tb_sw_info(sw, "power cycling the switch now\n"); in nvm_authenticate_device_dma_port()
241 dma_port_power_cycle(sw->dma_port); in nvm_authenticate_device_dma_port()
251 static void nvm_authenticate_start_dma_port(struct tb_switch *sw) in nvm_authenticate_start_dma_port() argument
261 root_port = pcie_find_root_port(sw->tb->nhi->pdev); in nvm_authenticate_start_dma_port()
266 static void nvm_authenticate_complete_dma_port(struct tb_switch *sw) in nvm_authenticate_complete_dma_port() argument
270 root_port = pcie_find_root_port(sw->tb->nhi->pdev); in nvm_authenticate_complete_dma_port()
275 static inline bool nvm_readable(struct tb_switch *sw) in nvm_readable() argument
277 if (tb_switch_is_usb4(sw)) { in nvm_readable()
284 return usb4_switch_nvm_sector_size(sw) > 0; in nvm_readable()
288 return !!sw->dma_port; in nvm_readable()
291 static inline bool nvm_upgradeable(struct tb_switch *sw) in nvm_upgradeable() argument
293 if (sw->no_nvm_upgrade) in nvm_upgradeable()
295 return nvm_readable(sw); in nvm_upgradeable()
298 static inline int nvm_read(struct tb_switch *sw, unsigned int address, in nvm_read() argument
301 if (tb_switch_is_usb4(sw)) in nvm_read()
302 return usb4_switch_nvm_read(sw, address, buf, size); in nvm_read()
303 return dma_port_flash_read(sw->dma_port, address, buf, size); in nvm_read()
306 static int nvm_authenticate(struct tb_switch *sw, bool auth_only) in nvm_authenticate() argument
310 if (tb_switch_is_usb4(sw)) { in nvm_authenticate()
312 ret = usb4_switch_nvm_set_offset(sw, 0); in nvm_authenticate()
316 sw->nvm->authenticating = true; in nvm_authenticate()
317 return usb4_switch_nvm_authenticate(sw); in nvm_authenticate()
322 sw->nvm->authenticating = true; in nvm_authenticate()
323 if (!tb_route(sw)) { in nvm_authenticate()
324 nvm_authenticate_start_dma_port(sw); in nvm_authenticate()
325 ret = nvm_authenticate_host_dma_port(sw); in nvm_authenticate()
327 ret = nvm_authenticate_device_dma_port(sw); in nvm_authenticate()
337 struct tb_switch *sw = tb_to_switch(nvm->dev); in tb_switch_nvm_read() local
340 pm_runtime_get_sync(&sw->dev); in tb_switch_nvm_read()
342 if (!mutex_trylock(&sw->tb->lock)) { in tb_switch_nvm_read()
347 ret = nvm_read(sw, offset, val, bytes); in tb_switch_nvm_read()
348 mutex_unlock(&sw->tb->lock); in tb_switch_nvm_read()
351 pm_runtime_mark_last_busy(&sw->dev); in tb_switch_nvm_read()
352 pm_runtime_put_autosuspend(&sw->dev); in tb_switch_nvm_read()
361 struct tb_switch *sw = tb_to_switch(nvm->dev); in tb_switch_nvm_write() local
364 if (!mutex_trylock(&sw->tb->lock)) in tb_switch_nvm_write()
374 mutex_unlock(&sw->tb->lock); in tb_switch_nvm_write()
379 static int tb_switch_nvm_add(struct tb_switch *sw) in tb_switch_nvm_add() argument
385 if (!nvm_readable(sw)) in tb_switch_nvm_add()
393 if (sw->config.vendor_id != PCI_VENDOR_ID_INTEL && in tb_switch_nvm_add()
394 sw->config.vendor_id != 0x8087) { in tb_switch_nvm_add()
395 dev_info(&sw->dev, in tb_switch_nvm_add()
397 sw->config.vendor_id); in tb_switch_nvm_add()
401 nvm = tb_nvm_alloc(&sw->dev); in tb_switch_nvm_add()
410 if (!sw->safe_mode) { in tb_switch_nvm_add()
413 ret = nvm_read(sw, NVM_FLASH_SIZE, &val, sizeof(val)); in tb_switch_nvm_add()
417 hdr_size = sw->generation < 3 ? SZ_8K : SZ_16K; in tb_switch_nvm_add()
421 ret = nvm_read(sw, NVM_VERSION, &val, sizeof(val)); in tb_switch_nvm_add()
433 if (!sw->no_nvm_upgrade) { in tb_switch_nvm_add()
440 sw->nvm = nvm; in tb_switch_nvm_add()
448 static void tb_switch_nvm_remove(struct tb_switch *sw) in tb_switch_nvm_remove() argument
452 nvm = sw->nvm; in tb_switch_nvm_remove()
453 sw->nvm = NULL; in tb_switch_nvm_remove()
460 nvm_clear_auth_status(sw); in tb_switch_nvm_remove()
615 if (credits == 0 || port->sw->is_unplugged) in tb_port_add_nfc_credits()
622 if (tb_switch_is_usb4(port->sw) && !tb_port_is_null(port)) in tb_port_add_nfc_credits()
661 if (tb_switch_is_icm(port->sw)) in tb_port_unlock()
665 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 if (tb_switch_is_usb4(port->sw)) { in tb_init_port()
781 tb_dump_port(port->sw->tb, port); in tb_init_port()
861 const struct tb_switch *sw) in tb_switch_is_reachable() argument
864 return (tb_route(parent) & mask) == (tb_route(sw) & mask); in tb_switch_is_reachable()
890 if (prev->sw == end->sw) { in tb_next_port_on_path()
896 if (tb_switch_is_reachable(prev->sw, end->sw)) { in tb_next_port_on_path()
897 next = tb_port_at(tb_route(end->sw), prev->sw); in tb_next_port_on_path()
906 next = tb_upstream_port(prev->sw); in tb_next_port_on_path()
1157 if (tb_switch_is_usb4(port->sw)) in tb_port_start_lane_initialization()
1393 static const char *tb_switch_generation_name(const struct tb_switch *sw) in tb_switch_generation_name() argument
1395 switch (sw->generation) { in tb_switch_generation_name()
1409 static void tb_dump_switch(const struct tb *tb, const struct tb_switch *sw) in tb_dump_switch() argument
1411 const struct tb_regs_switch_header *regs = &sw->config; in tb_dump_switch()
1414 tb_switch_generation_name(sw), regs->vendor_id, regs->device_id, in tb_dump_switch()
1429 * @sw: Switch to reset
1433 int tb_switch_reset(struct tb_switch *sw) in tb_switch_reset() argument
1437 if (sw->generation > 1) in tb_switch_reset()
1440 tb_sw_dbg(sw, "resetting switch\n"); in tb_switch_reset()
1442 res.err = tb_sw_write(sw, ((u32 *) &sw->config) + 2, in tb_switch_reset()
1446 res = tb_cfg_reset(sw->tb->ctl, tb_route(sw)); in tb_switch_reset()
1459 static int tb_plug_events_active(struct tb_switch *sw, bool active) in tb_plug_events_active() argument
1464 if (tb_switch_is_icm(sw) || tb_switch_is_usb4(sw)) in tb_plug_events_active()
1467 sw->config.plug_events_delay = 0xff; in tb_plug_events_active()
1468 res = tb_sw_write(sw, ((u32 *) &sw->config) + 4, TB_CFG_SWITCH, 4, 1); in tb_plug_events_active()
1472 res = tb_sw_read(sw, &data, TB_CFG_SWITCH, sw->cap_plug_events + 1, 1); in tb_plug_events_active()
1478 switch (sw->config.device_id) { in tb_plug_events_active()
1489 return tb_sw_write(sw, &data, TB_CFG_SWITCH, in tb_plug_events_active()
1490 sw->cap_plug_events + 1, 1); in tb_plug_events_active()
1497 struct tb_switch *sw = tb_to_switch(dev); in authorized_show() local
1499 return sprintf(buf, "%u\n", sw->authorized); in authorized_show()
1505 struct tb_switch *sw; in disapprove_switch() local
1507 sw = tb_to_switch(dev); in disapprove_switch()
1508 if (sw && sw->authorized) { in disapprove_switch()
1512 ret = device_for_each_child_reverse(&sw->dev, NULL, disapprove_switch); in disapprove_switch()
1516 ret = tb_domain_disapprove_switch(sw->tb, sw); in disapprove_switch()
1520 sw->authorized = 0; in disapprove_switch()
1521 kobject_uevent_env(&sw->dev.kobj, KOBJ_CHANGE, envp); in disapprove_switch()
1527 static int tb_switch_set_authorized(struct tb_switch *sw, unsigned int val) in tb_switch_set_authorized() argument
1533 if (!mutex_trylock(&sw->tb->lock)) in tb_switch_set_authorized()
1536 if (!!sw->authorized == !!val) in tb_switch_set_authorized()
1542 if (tb_route(sw)) { in tb_switch_set_authorized()
1543 ret = disapprove_switch(&sw->dev, NULL); in tb_switch_set_authorized()
1550 if (sw->key) in tb_switch_set_authorized()
1551 ret = tb_domain_approve_switch_key(sw->tb, sw); in tb_switch_set_authorized()
1553 ret = tb_domain_approve_switch(sw->tb, sw); in tb_switch_set_authorized()
1558 if (sw->key) in tb_switch_set_authorized()
1559 ret = tb_domain_challenge_switch_key(sw->tb, sw); in tb_switch_set_authorized()
1567 sw->authorized = val; in tb_switch_set_authorized()
1572 sprintf(envp_string, "AUTHORIZED=%u", sw->authorized); in tb_switch_set_authorized()
1573 kobject_uevent_env(&sw->dev.kobj, KOBJ_CHANGE, envp); in tb_switch_set_authorized()
1577 mutex_unlock(&sw->tb->lock); in tb_switch_set_authorized()
1585 struct tb_switch *sw = tb_to_switch(dev); in authorized_store() local
1595 pm_runtime_get_sync(&sw->dev); in authorized_store()
1596 ret = tb_switch_set_authorized(sw, val); in authorized_store()
1597 pm_runtime_mark_last_busy(&sw->dev); in authorized_store()
1598 pm_runtime_put_autosuspend(&sw->dev); in authorized_store()
1607 struct tb_switch *sw = tb_to_switch(dev); in boot_show() local
1609 return sprintf(buf, "%u\n", sw->boot); in boot_show()
1616 struct tb_switch *sw = tb_to_switch(dev); in device_show() local
1618 return sprintf(buf, "%#x\n", sw->device); in device_show()
1625 struct tb_switch *sw = tb_to_switch(dev); in device_name_show() local
1627 return sprintf(buf, "%s\n", sw->device_name ? sw->device_name : ""); in device_name_show()
1634 struct tb_switch *sw = tb_to_switch(dev); in generation_show() local
1636 return sprintf(buf, "%u\n", sw->generation); in generation_show()
1643 struct tb_switch *sw = tb_to_switch(dev); in key_show() local
1646 if (!mutex_trylock(&sw->tb->lock)) in key_show()
1649 if (sw->key) in key_show()
1650 ret = sprintf(buf, "%*phN\n", TB_SWITCH_KEY_SIZE, sw->key); in key_show()
1654 mutex_unlock(&sw->tb->lock); in key_show()
1661 struct tb_switch *sw = tb_to_switch(dev); in key_store() local
1671 if (!mutex_trylock(&sw->tb->lock)) in key_store()
1674 if (sw->authorized) { in key_store()
1677 kfree(sw->key); in key_store()
1679 sw->key = NULL; in key_store()
1681 sw->key = kmemdup(key, sizeof(key), GFP_KERNEL); in key_store()
1682 if (!sw->key) in key_store()
1687 mutex_unlock(&sw->tb->lock); in key_store()
1695 struct tb_switch *sw = tb_to_switch(dev); in speed_show() local
1697 return sprintf(buf, "%u.0 Gb/s\n", sw->link_speed); in speed_show()
1710 struct tb_switch *sw = tb_to_switch(dev); in lanes_show() local
1712 return sprintf(buf, "%u\n", sw->link_width); in lanes_show()
1725 struct tb_switch *sw = tb_to_switch(dev); in nvm_authenticate_show() local
1728 nvm_get_auth_status(sw, &status); in nvm_authenticate_show()
1735 struct tb_switch *sw = tb_to_switch(dev); in nvm_authenticate_sysfs() local
1738 pm_runtime_get_sync(&sw->dev); in nvm_authenticate_sysfs()
1740 if (!mutex_trylock(&sw->tb->lock)) { in nvm_authenticate_sysfs()
1746 if (!sw->nvm) { in nvm_authenticate_sysfs()
1756 nvm_clear_auth_status(sw); in nvm_authenticate_sysfs()
1763 ret = nvm_authenticate(sw, true); in nvm_authenticate_sysfs()
1765 if (!sw->nvm->flushed) { in nvm_authenticate_sysfs()
1766 if (!sw->nvm->buf) { in nvm_authenticate_sysfs()
1771 ret = nvm_validate_and_write(sw); in nvm_authenticate_sysfs()
1777 ret = tb_lc_force_power(sw); in nvm_authenticate_sysfs()
1779 ret = nvm_authenticate(sw, false); in nvm_authenticate_sysfs()
1785 mutex_unlock(&sw->tb->lock); in nvm_authenticate_sysfs()
1787 pm_runtime_mark_last_busy(&sw->dev); in nvm_authenticate_sysfs()
1788 pm_runtime_put_autosuspend(&sw->dev); in nvm_authenticate_sysfs()
1822 struct tb_switch *sw = tb_to_switch(dev); in nvm_version_show() local
1825 if (!mutex_trylock(&sw->tb->lock)) in nvm_version_show()
1828 if (sw->safe_mode) in nvm_version_show()
1830 else if (!sw->nvm) in nvm_version_show()
1833 ret = sprintf(buf, "%x.%x\n", sw->nvm->major, sw->nvm->minor); in nvm_version_show()
1835 mutex_unlock(&sw->tb->lock); in nvm_version_show()
1844 struct tb_switch *sw = tb_to_switch(dev); in vendor_show() local
1846 return sprintf(buf, "%#x\n", sw->vendor); in vendor_show()
1853 struct tb_switch *sw = tb_to_switch(dev); in vendor_name_show() local
1855 return sprintf(buf, "%s\n", sw->vendor_name ? sw->vendor_name : ""); in vendor_name_show()
1862 struct tb_switch *sw = tb_to_switch(dev); in unique_id_show() local
1864 return sprintf(buf, "%pUb\n", sw->uuid); in unique_id_show()
1892 struct tb_switch *sw = tb_to_switch(dev); in switch_attr_is_visible() local
1895 if (sw->tb->security_level == TB_SECURITY_NOPCIE || in switch_attr_is_visible()
1896 sw->tb->security_level == TB_SECURITY_DPONLY) in switch_attr_is_visible()
1899 if (!sw->device) in switch_attr_is_visible()
1902 if (!sw->device_name) in switch_attr_is_visible()
1905 if (!sw->vendor) in switch_attr_is_visible()
1908 if (!sw->vendor_name) in switch_attr_is_visible()
1911 if (tb_route(sw) && in switch_attr_is_visible()
1912 sw->tb->security_level == TB_SECURITY_SECURE && in switch_attr_is_visible()
1913 sw->security_level == TB_SECURITY_SECURE) in switch_attr_is_visible()
1920 if (tb_route(sw)) in switch_attr_is_visible()
1924 if (nvm_upgradeable(sw)) in switch_attr_is_visible()
1928 if (nvm_readable(sw)) in switch_attr_is_visible()
1932 if (tb_route(sw)) in switch_attr_is_visible()
1936 if (sw->quirks & QUIRK_FORCE_POWER_LINK_CONTROLLER) in switch_attr_is_visible()
1941 return sw->safe_mode ? 0 : attr->mode; in switch_attr_is_visible()
1956 struct tb_switch *sw = tb_to_switch(dev); in tb_switch_release() local
1959 dma_port_free(sw->dma_port); in tb_switch_release()
1961 tb_switch_for_each_port(sw, port) { in tb_switch_release()
1966 kfree(sw->uuid); in tb_switch_release()
1967 kfree(sw->device_name); in tb_switch_release()
1968 kfree(sw->vendor_name); in tb_switch_release()
1969 kfree(sw->ports); in tb_switch_release()
1970 kfree(sw->drom); in tb_switch_release()
1971 kfree(sw->key); in tb_switch_release()
1972 kfree(sw); in tb_switch_release()
1977 struct tb_switch *sw = tb_to_switch(dev); in tb_switch_uevent() local
1980 if (sw->config.thunderbolt_version == USB4_VERSION_1_0) { in tb_switch_uevent()
1985 if (!tb_route(sw)) { in tb_switch_uevent()
1992 tb_switch_for_each_port(sw, port) { in tb_switch_uevent()
2014 struct tb_switch *sw = tb_to_switch(dev); in tb_switch_runtime_suspend() local
2015 const struct tb_cm_ops *cm_ops = sw->tb->cm_ops; in tb_switch_runtime_suspend()
2018 return cm_ops->runtime_suspend_switch(sw); in tb_switch_runtime_suspend()
2025 struct tb_switch *sw = tb_to_switch(dev); in tb_switch_runtime_resume() local
2026 const struct tb_cm_ops *cm_ops = sw->tb->cm_ops; in tb_switch_runtime_resume()
2029 return cm_ops->runtime_resume_switch(sw); in tb_switch_runtime_resume()
2045 static int tb_switch_get_generation(struct tb_switch *sw) in tb_switch_get_generation() argument
2047 switch (sw->config.device_id) { in tb_switch_get_generation()
2076 if (tb_switch_is_usb4(sw)) in tb_switch_get_generation()
2083 tb_sw_warn(sw, "unsupported switch device id %#x\n", in tb_switch_get_generation()
2084 sw->config.device_id); in tb_switch_get_generation()
2089 static bool tb_switch_exceeds_max_depth(const struct tb_switch *sw, int depth) in tb_switch_exceeds_max_depth() argument
2093 if (tb_switch_is_usb4(sw) || in tb_switch_exceeds_max_depth()
2094 (sw->tb->root_switch && tb_switch_is_usb4(sw->tb->root_switch))) in tb_switch_exceeds_max_depth()
2119 struct tb_switch *sw; in tb_switch_alloc() local
2138 sw = kzalloc(sizeof(*sw), GFP_KERNEL); in tb_switch_alloc()
2139 if (!sw) in tb_switch_alloc()
2142 sw->tb = tb; in tb_switch_alloc()
2143 ret = tb_cfg_read(tb->ctl, &sw->config, route, 0, TB_CFG_SWITCH, 0, 5); in tb_switch_alloc()
2147 sw->generation = tb_switch_get_generation(sw); in tb_switch_alloc()
2150 tb_dump_switch(tb, sw); in tb_switch_alloc()
2153 sw->config.upstream_port_number = upstream_port; in tb_switch_alloc()
2154 sw->config.depth = depth; in tb_switch_alloc()
2155 sw->config.route_hi = upper_32_bits(route); in tb_switch_alloc()
2156 sw->config.route_lo = lower_32_bits(route); in tb_switch_alloc()
2157 sw->config.enabled = 0; in tb_switch_alloc()
2160 if (tb_switch_exceeds_max_depth(sw, depth)) { in tb_switch_alloc()
2166 sw->ports = kcalloc(sw->config.max_port_number + 1, sizeof(*sw->ports), in tb_switch_alloc()
2168 if (!sw->ports) { in tb_switch_alloc()
2173 for (i = 0; i <= sw->config.max_port_number; i++) { in tb_switch_alloc()
2175 sw->ports[i].sw = sw; in tb_switch_alloc()
2176 sw->ports[i].port = i; in tb_switch_alloc()
2180 ida_init(&sw->ports[i].in_hopids); in tb_switch_alloc()
2181 ida_init(&sw->ports[i].out_hopids); in tb_switch_alloc()
2185 ret = tb_switch_find_vse_cap(sw, TB_VSE_CAP_PLUG_EVENTS); in tb_switch_alloc()
2187 sw->cap_plug_events = ret; in tb_switch_alloc()
2189 ret = tb_switch_find_vse_cap(sw, TB_VSE_CAP_LINK_CONTROLLER); in tb_switch_alloc()
2191 sw->cap_lc = ret; in tb_switch_alloc()
2195 sw->authorized = true; in tb_switch_alloc()
2197 device_initialize(&sw->dev); in tb_switch_alloc()
2198 sw->dev.parent = parent; in tb_switch_alloc()
2199 sw->dev.bus = &tb_bus_type; in tb_switch_alloc()
2200 sw->dev.type = &tb_switch_type; in tb_switch_alloc()
2201 sw->dev.groups = switch_groups; in tb_switch_alloc()
2202 dev_set_name(&sw->dev, "%u-%llx", tb->index, tb_route(sw)); in tb_switch_alloc()
2204 return sw; in tb_switch_alloc()
2207 kfree(sw->ports); in tb_switch_alloc()
2208 kfree(sw); in tb_switch_alloc()
2230 struct tb_switch *sw; in tb_switch_alloc_safe_mode() local
2232 sw = kzalloc(sizeof(*sw), GFP_KERNEL); in tb_switch_alloc_safe_mode()
2233 if (!sw) in tb_switch_alloc_safe_mode()
2236 sw->tb = tb; in tb_switch_alloc_safe_mode()
2237 sw->config.depth = tb_route_length(route); in tb_switch_alloc_safe_mode()
2238 sw->config.route_hi = upper_32_bits(route); in tb_switch_alloc_safe_mode()
2239 sw->config.route_lo = lower_32_bits(route); in tb_switch_alloc_safe_mode()
2240 sw->safe_mode = true; in tb_switch_alloc_safe_mode()
2242 device_initialize(&sw->dev); in tb_switch_alloc_safe_mode()
2243 sw->dev.parent = parent; in tb_switch_alloc_safe_mode()
2244 sw->dev.bus = &tb_bus_type; in tb_switch_alloc_safe_mode()
2245 sw->dev.type = &tb_switch_type; in tb_switch_alloc_safe_mode()
2246 sw->dev.groups = switch_groups; in tb_switch_alloc_safe_mode()
2247 dev_set_name(&sw->dev, "%u-%llx", tb->index, tb_route(sw)); in tb_switch_alloc_safe_mode()
2249 return sw; in tb_switch_alloc_safe_mode()
2254 * @sw: Switch to configure
2263 int tb_switch_configure(struct tb_switch *sw) in tb_switch_configure() argument
2265 struct tb *tb = sw->tb; in tb_switch_configure()
2269 route = tb_route(sw); in tb_switch_configure()
2272 sw->config.enabled ? "restoring" : "initializing", route, in tb_switch_configure()
2273 tb_route_length(route), sw->config.upstream_port_number); in tb_switch_configure()
2275 sw->config.enabled = 1; in tb_switch_configure()
2277 if (tb_switch_is_usb4(sw)) { in tb_switch_configure()
2283 sw->config.cmuv = USB4_VERSION_1_0; in tb_switch_configure()
2286 ret = tb_sw_write(sw, (u32 *)&sw->config + 1, TB_CFG_SWITCH, in tb_switch_configure()
2291 ret = usb4_switch_setup(sw); in tb_switch_configure()
2293 if (sw->config.vendor_id != PCI_VENDOR_ID_INTEL) in tb_switch_configure()
2294 tb_sw_warn(sw, "unknown switch vendor id %#x\n", in tb_switch_configure()
2295 sw->config.vendor_id); in tb_switch_configure()
2297 if (!sw->cap_plug_events) { in tb_switch_configure()
2298 tb_sw_warn(sw, "cannot find TB_VSE_CAP_PLUG_EVENTS aborting\n"); in tb_switch_configure()
2303 ret = tb_sw_write(sw, (u32 *)&sw->config + 1, TB_CFG_SWITCH, in tb_switch_configure()
2309 return tb_plug_events_active(sw, true); in tb_switch_configure()
2312 static int tb_switch_set_uuid(struct tb_switch *sw) in tb_switch_set_uuid() argument
2318 if (sw->uuid) in tb_switch_set_uuid()
2321 if (tb_switch_is_usb4(sw)) { in tb_switch_set_uuid()
2322 ret = usb4_switch_read_uid(sw, &sw->uid); in tb_switch_set_uuid()
2331 ret = tb_lc_read_uuid(sw, uuid); in tb_switch_set_uuid()
2346 uuid[0] = sw->uid & 0xffffffff; in tb_switch_set_uuid()
2347 uuid[1] = (sw->uid >> 32) & 0xffffffff; in tb_switch_set_uuid()
2352 sw->uuid = kmemdup(uuid, sizeof(uuid), GFP_KERNEL); in tb_switch_set_uuid()
2353 if (!sw->uuid) in tb_switch_set_uuid()
2358 static int tb_switch_add_dma_port(struct tb_switch *sw) in tb_switch_add_dma_port() argument
2363 switch (sw->generation) { in tb_switch_add_dma_port()
2366 if (tb_route(sw)) in tb_switch_add_dma_port()
2372 ret = tb_switch_set_uuid(sw); in tb_switch_add_dma_port()
2382 if (!sw->safe_mode) in tb_switch_add_dma_port()
2387 if (sw->no_nvm_upgrade) in tb_switch_add_dma_port()
2390 if (tb_switch_is_usb4(sw)) { in tb_switch_add_dma_port()
2391 ret = usb4_switch_nvm_authenticate_status(sw, &status); in tb_switch_add_dma_port()
2396 tb_sw_info(sw, "switch flash authentication failed\n"); in tb_switch_add_dma_port()
2397 nvm_set_auth_status(sw, status); in tb_switch_add_dma_port()
2404 if (!tb_route(sw) && !tb_switch_is_icm(sw)) in tb_switch_add_dma_port()
2407 sw->dma_port = dma_port_alloc(sw); in tb_switch_add_dma_port()
2408 if (!sw->dma_port) in tb_switch_add_dma_port()
2417 nvm_get_auth_status(sw, &status); in tb_switch_add_dma_port()
2419 if (!tb_route(sw)) in tb_switch_add_dma_port()
2420 nvm_authenticate_complete_dma_port(sw); in tb_switch_add_dma_port()
2429 ret = dma_port_flash_update_auth_status(sw->dma_port, &status); in tb_switch_add_dma_port()
2434 if (!tb_route(sw)) in tb_switch_add_dma_port()
2435 nvm_authenticate_complete_dma_port(sw); in tb_switch_add_dma_port()
2438 tb_sw_info(sw, "switch flash authentication failed\n"); in tb_switch_add_dma_port()
2439 nvm_set_auth_status(sw, status); in tb_switch_add_dma_port()
2442 tb_sw_info(sw, "power cycling the switch now\n"); in tb_switch_add_dma_port()
2443 dma_port_power_cycle(sw->dma_port); in tb_switch_add_dma_port()
2452 static void tb_switch_default_link_ports(struct tb_switch *sw) in tb_switch_default_link_ports() argument
2456 for (i = 1; i <= sw->config.max_port_number; i++) { in tb_switch_default_link_ports()
2457 struct tb_port *port = &sw->ports[i]; in tb_switch_default_link_ports()
2464 if (i == sw->config.max_port_number || in tb_switch_default_link_ports()
2465 !tb_port_is_null(&sw->ports[i + 1])) in tb_switch_default_link_ports()
2469 subordinate = &sw->ports[i + 1]; in tb_switch_default_link_ports()
2476 tb_sw_dbg(sw, "linked ports %d <-> %d\n", in tb_switch_default_link_ports()
2482 static bool tb_switch_lane_bonding_possible(struct tb_switch *sw) in tb_switch_lane_bonding_possible() argument
2484 const struct tb_port *up = tb_upstream_port(sw); in tb_switch_lane_bonding_possible()
2489 if (tb_switch_is_usb4(sw)) in tb_switch_lane_bonding_possible()
2490 return usb4_switch_lane_bonding_possible(sw); in tb_switch_lane_bonding_possible()
2491 return tb_lc_lane_bonding_possible(sw); in tb_switch_lane_bonding_possible()
2494 static int tb_switch_update_link_attributes(struct tb_switch *sw) in tb_switch_update_link_attributes() argument
2500 if (!tb_route(sw) || tb_switch_is_icm(sw)) in tb_switch_update_link_attributes()
2503 up = tb_upstream_port(sw); in tb_switch_update_link_attributes()
2508 if (sw->link_speed != ret) in tb_switch_update_link_attributes()
2510 sw->link_speed = ret; in tb_switch_update_link_attributes()
2515 if (sw->link_width != ret) in tb_switch_update_link_attributes()
2517 sw->link_width = ret; in tb_switch_update_link_attributes()
2520 if (device_is_registered(&sw->dev) && change) in tb_switch_update_link_attributes()
2521 kobject_uevent(&sw->dev.kobj, KOBJ_CHANGE); in tb_switch_update_link_attributes()
2528 * @sw: Switch to enable lane bonding
2534 int tb_switch_lane_bonding_enable(struct tb_switch *sw) in tb_switch_lane_bonding_enable() argument
2536 struct tb_switch *parent = tb_to_switch(sw->dev.parent); in tb_switch_lane_bonding_enable()
2538 u64 route = tb_route(sw); in tb_switch_lane_bonding_enable()
2544 if (!tb_switch_lane_bonding_possible(sw)) in tb_switch_lane_bonding_enable()
2547 up = tb_upstream_port(sw); in tb_switch_lane_bonding_enable()
2575 tb_switch_update_link_attributes(sw); in tb_switch_lane_bonding_enable()
2577 tb_sw_dbg(sw, "lane bonding enabled\n"); in tb_switch_lane_bonding_enable()
2583 * @sw: Switch whose lane bonding to disable
2585 * Disables lane bonding between @sw and parent. This can be called even
2588 void tb_switch_lane_bonding_disable(struct tb_switch *sw) in tb_switch_lane_bonding_disable() argument
2590 struct tb_switch *parent = tb_to_switch(sw->dev.parent); in tb_switch_lane_bonding_disable()
2593 if (!tb_route(sw)) in tb_switch_lane_bonding_disable()
2596 up = tb_upstream_port(sw); in tb_switch_lane_bonding_disable()
2600 down = tb_port_at(tb_route(sw), parent); in tb_switch_lane_bonding_disable()
2610 tb_sw_warn(sw, "timeout disabling lane bonding\n"); in tb_switch_lane_bonding_disable()
2614 tb_switch_update_link_attributes(sw); in tb_switch_lane_bonding_disable()
2616 tb_sw_dbg(sw, "lane bonding disabled\n"); in tb_switch_lane_bonding_disable()
2621 * @sw: Switch whose link is configured
2623 * Sets the link upstream from @sw configured (from both ends) so that
2631 int tb_switch_configure_link(struct tb_switch *sw) in tb_switch_configure_link() argument
2636 if (!tb_route(sw) || tb_switch_is_icm(sw)) in tb_switch_configure_link()
2639 up = tb_upstream_port(sw); in tb_switch_configure_link()
2640 if (tb_switch_is_usb4(up->sw)) in tb_switch_configure_link()
2648 if (tb_switch_is_usb4(down->sw)) in tb_switch_configure_link()
2655 * @sw: Switch whose link is unconfigured
2657 * Sets the link unconfigured so the @sw will be disconnected if the
2660 void tb_switch_unconfigure_link(struct tb_switch *sw) in tb_switch_unconfigure_link() argument
2664 if (sw->is_unplugged) in tb_switch_unconfigure_link()
2666 if (!tb_route(sw) || tb_switch_is_icm(sw)) in tb_switch_unconfigure_link()
2669 up = tb_upstream_port(sw); in tb_switch_unconfigure_link()
2670 if (tb_switch_is_usb4(up->sw)) in tb_switch_unconfigure_link()
2676 if (tb_switch_is_usb4(down->sw)) in tb_switch_unconfigure_link()
2682 static void tb_switch_credits_init(struct tb_switch *sw) in tb_switch_credits_init() argument
2684 if (tb_switch_is_icm(sw)) in tb_switch_credits_init()
2686 if (!tb_switch_is_usb4(sw)) in tb_switch_credits_init()
2688 if (usb4_switch_credits_init(sw)) in tb_switch_credits_init()
2689 tb_sw_info(sw, "failed to determine preferred buffer allocation, using defaults\n"); in tb_switch_credits_init()
2694 * @sw: Switch to add
2704 int tb_switch_add(struct tb_switch *sw) in tb_switch_add() argument
2715 ret = tb_switch_add_dma_port(sw); in tb_switch_add()
2717 dev_err(&sw->dev, "failed to add DMA port\n"); in tb_switch_add()
2721 if (!sw->safe_mode) { in tb_switch_add()
2722 tb_switch_credits_init(sw); in tb_switch_add()
2725 ret = tb_drom_read(sw); in tb_switch_add()
2727 dev_err(&sw->dev, "reading DROM failed\n"); in tb_switch_add()
2730 tb_sw_dbg(sw, "uid: %#llx\n", sw->uid); in tb_switch_add()
2732 tb_check_quirks(sw); in tb_switch_add()
2734 ret = tb_switch_set_uuid(sw); in tb_switch_add()
2736 dev_err(&sw->dev, "failed to set UUID\n"); in tb_switch_add()
2740 for (i = 0; i <= sw->config.max_port_number; i++) { in tb_switch_add()
2741 if (sw->ports[i].disabled) { in tb_switch_add()
2742 tb_port_dbg(&sw->ports[i], "disabled by eeprom\n"); in tb_switch_add()
2745 ret = tb_init_port(&sw->ports[i]); in tb_switch_add()
2747 dev_err(&sw->dev, "failed to initialize port %d\n", i); in tb_switch_add()
2752 tb_switch_default_link_ports(sw); in tb_switch_add()
2754 ret = tb_switch_update_link_attributes(sw); in tb_switch_add()
2758 ret = tb_switch_tmu_init(sw); in tb_switch_add()
2763 ret = device_add(&sw->dev); in tb_switch_add()
2765 dev_err(&sw->dev, "failed to add device: %d\n", ret); in tb_switch_add()
2769 if (tb_route(sw)) { in tb_switch_add()
2770 dev_info(&sw->dev, "new device found, vendor=%#x device=%#x\n", in tb_switch_add()
2771 sw->vendor, sw->device); in tb_switch_add()
2772 if (sw->vendor_name && sw->device_name) in tb_switch_add()
2773 dev_info(&sw->dev, "%s %s\n", sw->vendor_name, in tb_switch_add()
2774 sw->device_name); in tb_switch_add()
2777 ret = usb4_switch_add_ports(sw); in tb_switch_add()
2779 dev_err(&sw->dev, "failed to add USB4 ports\n"); in tb_switch_add()
2783 ret = tb_switch_nvm_add(sw); in tb_switch_add()
2785 dev_err(&sw->dev, "failed to add NVM devices\n"); in tb_switch_add()
2794 device_init_wakeup(&sw->dev, true); in tb_switch_add()
2796 pm_runtime_set_active(&sw->dev); in tb_switch_add()
2797 if (sw->rpm) { in tb_switch_add()
2798 pm_runtime_set_autosuspend_delay(&sw->dev, TB_AUTOSUSPEND_DELAY); in tb_switch_add()
2799 pm_runtime_use_autosuspend(&sw->dev); in tb_switch_add()
2800 pm_runtime_mark_last_busy(&sw->dev); in tb_switch_add()
2801 pm_runtime_enable(&sw->dev); in tb_switch_add()
2802 pm_request_autosuspend(&sw->dev); in tb_switch_add()
2805 tb_switch_debugfs_init(sw); in tb_switch_add()
2809 usb4_switch_remove_ports(sw); in tb_switch_add()
2811 device_del(&sw->dev); in tb_switch_add()
2818 * @sw: Switch to remove
2824 void tb_switch_remove(struct tb_switch *sw) in tb_switch_remove() argument
2828 tb_switch_debugfs_remove(sw); in tb_switch_remove()
2830 if (sw->rpm) { in tb_switch_remove()
2831 pm_runtime_get_sync(&sw->dev); in tb_switch_remove()
2832 pm_runtime_disable(&sw->dev); in tb_switch_remove()
2836 tb_switch_for_each_port(sw, port) { in tb_switch_remove()
2838 tb_switch_remove(port->remote->sw); in tb_switch_remove()
2849 if (!sw->is_unplugged) in tb_switch_remove()
2850 tb_plug_events_active(sw, false); in tb_switch_remove()
2852 tb_switch_nvm_remove(sw); in tb_switch_remove()
2853 usb4_switch_remove_ports(sw); in tb_switch_remove()
2855 if (tb_route(sw)) in tb_switch_remove()
2856 dev_info(&sw->dev, "device disconnected\n"); in tb_switch_remove()
2857 device_unregister(&sw->dev); in tb_switch_remove()
2862 * @sw: Router to mark unplugged
2864 void tb_sw_set_unplugged(struct tb_switch *sw) in tb_sw_set_unplugged() argument
2868 if (sw == sw->tb->root_switch) { in tb_sw_set_unplugged()
2869 tb_sw_WARN(sw, "cannot unplug root switch\n"); in tb_sw_set_unplugged()
2872 if (sw->is_unplugged) { in tb_sw_set_unplugged()
2873 tb_sw_WARN(sw, "is_unplugged already set\n"); in tb_sw_set_unplugged()
2876 sw->is_unplugged = true; in tb_sw_set_unplugged()
2877 tb_switch_for_each_port(sw, port) { in tb_sw_set_unplugged()
2879 tb_sw_set_unplugged(port->remote->sw); in tb_sw_set_unplugged()
2885 static int tb_switch_set_wake(struct tb_switch *sw, unsigned int flags) in tb_switch_set_wake() argument
2888 tb_sw_dbg(sw, "enabling wakeup: %#x\n", flags); in tb_switch_set_wake()
2890 tb_sw_dbg(sw, "disabling wakeup\n"); in tb_switch_set_wake()
2892 if (tb_switch_is_usb4(sw)) in tb_switch_set_wake()
2893 return usb4_switch_set_wake(sw, flags); in tb_switch_set_wake()
2894 return tb_lc_set_wake(sw, flags); in tb_switch_set_wake()
2897 int tb_switch_resume(struct tb_switch *sw) in tb_switch_resume() argument
2902 tb_sw_dbg(sw, "resuming switch\n"); in tb_switch_resume()
2908 if (tb_route(sw)) { in tb_switch_resume()
2916 err = tb_cfg_get_upstream_port(sw->tb->ctl, tb_route(sw)); in tb_switch_resume()
2918 tb_sw_info(sw, "switch not present anymore\n"); in tb_switch_resume()
2922 if (tb_switch_is_usb4(sw)) in tb_switch_resume()
2923 err = usb4_switch_read_uid(sw, &uid); in tb_switch_resume()
2925 err = tb_drom_read_uid_only(sw, &uid); in tb_switch_resume()
2927 tb_sw_warn(sw, "uid read failed\n"); in tb_switch_resume()
2930 if (sw->uid != uid) { in tb_switch_resume()
2931 tb_sw_info(sw, in tb_switch_resume()
2933 sw->uid, uid); in tb_switch_resume()
2938 err = tb_switch_configure(sw); in tb_switch_resume()
2943 tb_switch_set_wake(sw, 0); in tb_switch_resume()
2945 err = tb_switch_tmu_init(sw); in tb_switch_resume()
2950 tb_switch_for_each_port(sw, port) { in tb_switch_resume()
2961 tb_sw_set_unplugged(port->remote->sw); in tb_switch_resume()
2971 if (port->remote && tb_switch_resume(port->remote->sw)) { in tb_switch_resume()
2974 tb_sw_set_unplugged(port->remote->sw); in tb_switch_resume()
2983 * @sw: Switch to suspend
2987 * value of @runtime and then sets sleep bit for the router. If @sw is
2991 void tb_switch_suspend(struct tb_switch *sw, bool runtime) in tb_switch_suspend() argument
2997 tb_sw_dbg(sw, "suspending switch\n"); in tb_switch_suspend()
2999 err = tb_plug_events_active(sw, false); in tb_switch_suspend()
3003 tb_switch_for_each_port(sw, port) { in tb_switch_suspend()
3005 tb_switch_suspend(port->remote->sw, runtime); in tb_switch_suspend()
3013 } else if (device_may_wakeup(&sw->dev)) { in tb_switch_suspend()
3017 tb_switch_set_wake(sw, flags); in tb_switch_suspend()
3019 if (tb_switch_is_usb4(sw)) in tb_switch_suspend()
3020 usb4_switch_set_sleep(sw); in tb_switch_suspend()
3022 tb_lc_set_sleep(sw); in tb_switch_suspend()
3027 * @sw: Switch whose DP resource is queried
3033 bool tb_switch_query_dp_resource(struct tb_switch *sw, struct tb_port *in) in tb_switch_query_dp_resource() argument
3035 if (tb_switch_is_usb4(sw)) in tb_switch_query_dp_resource()
3036 return usb4_switch_query_dp_resource(sw, in); in tb_switch_query_dp_resource()
3037 return tb_lc_dp_sink_query(sw, in); in tb_switch_query_dp_resource()
3042 * @sw: Switch whose DP resource is allocated
3049 int tb_switch_alloc_dp_resource(struct tb_switch *sw, struct tb_port *in) in tb_switch_alloc_dp_resource() argument
3051 if (tb_switch_is_usb4(sw)) in tb_switch_alloc_dp_resource()
3052 return usb4_switch_alloc_dp_resource(sw, in); in tb_switch_alloc_dp_resource()
3053 return tb_lc_dp_sink_alloc(sw, in); in tb_switch_alloc_dp_resource()
3058 * @sw: Switch whose DP resource is de-allocated
3064 void tb_switch_dealloc_dp_resource(struct tb_switch *sw, struct tb_port *in) in tb_switch_dealloc_dp_resource() argument
3068 if (tb_switch_is_usb4(sw)) in tb_switch_dealloc_dp_resource()
3069 ret = usb4_switch_dealloc_dp_resource(sw, in); in tb_switch_dealloc_dp_resource()
3071 ret = tb_lc_dp_sink_dealloc(sw, in); in tb_switch_dealloc_dp_resource()
3074 tb_sw_warn(sw, "failed to de-allocate DP resource for port %d\n", in tb_switch_dealloc_dp_resource()
3088 struct tb_switch *sw = tb_to_switch(dev); in tb_switch_match() local
3091 if (!sw) in tb_switch_match()
3093 if (sw->tb != lookup->tb) in tb_switch_match()
3097 return !memcmp(sw->uuid, lookup->uuid, sizeof(*lookup->uuid)); in tb_switch_match()
3100 return sw->config.route_lo == lower_32_bits(lookup->route) && in tb_switch_match()
3101 sw->config.route_hi == upper_32_bits(lookup->route); in tb_switch_match()
3106 return !sw->depth; in tb_switch_match()
3108 return sw->link == lookup->link && sw->depth == lookup->depth; in tb_switch_match()
3189 * tb_switch_find_port() - return the first port of @type on @sw or NULL
3190 * @sw: Switch to find the port from
3193 struct tb_port *tb_switch_find_port(struct tb_switch *sw, in tb_switch_find_port() argument
3198 tb_switch_for_each_port(sw, port) { in tb_switch_find_port()