Lines Matching refs:sw
41 static struct nvm_auth_status *__nvm_get_auth_status(const struct tb_switch *sw)
46 if (uuid_equal(&st->uuid, sw->uuid))
53 static void nvm_get_auth_status(const struct tb_switch *sw, u32 *status)
58 st = __nvm_get_auth_status(sw);
64 static void nvm_set_auth_status(const struct tb_switch *sw, u32 status)
68 if (WARN_ON(!sw->uuid))
72 st = __nvm_get_auth_status(sw);
79 memcpy(&st->uuid, sw->uuid, sizeof(st->uuid));
89 static void nvm_clear_auth_status(const struct tb_switch *sw)
94 st = __nvm_get_auth_status(sw);
102 static int nvm_validate_and_write(struct tb_switch *sw)
108 ret = tb_nvm_validate(sw->nvm);
112 ret = tb_nvm_write_headers(sw->nvm);
116 buf = sw->nvm->buf_data_start;
117 image_size = sw->nvm->buf_data_size;
119 if (tb_switch_is_usb4(sw))
120 ret = usb4_switch_nvm_write(sw, 0, buf, image_size);
122 ret = dma_port_flash_write(sw->dma_port, 0, buf, image_size);
126 sw->nvm->flushed = true;
130 static int nvm_authenticate_host_dma_port(struct tb_switch *sw)
139 if (!sw->safe_mode) {
142 ret = tb_domain_disconnect_all_paths(sw->tb);
149 ret = dma_port_flash_update_auth(sw->dma_port);
157 tb_sw_warn(sw, "failed to authenticate NVM, power cycling\n");
158 if (dma_port_flash_update_auth_status(sw->dma_port, &status) > 0)
159 nvm_set_auth_status(sw, status);
166 dma_port_power_cycle(sw->dma_port);
170 static int nvm_authenticate_device_dma_port(struct tb_switch *sw)
174 ret = dma_port_flash_update_auth(sw->dma_port);
195 ret = dma_port_flash_update_auth_status(sw->dma_port, &status);
200 tb_sw_warn(sw, "failed to authenticate NVM\n");
201 nvm_set_auth_status(sw, status);
204 tb_sw_info(sw, "power cycling the switch now\n");
205 dma_port_power_cycle(sw->dma_port);
215 static inline bool nvm_readable(struct tb_switch *sw)
217 if (tb_switch_is_usb4(sw)) {
224 return usb4_switch_nvm_sector_size(sw) > 0;
228 return !!sw->dma_port;
231 static inline bool nvm_upgradeable(struct tb_switch *sw)
233 if (sw->no_nvm_upgrade)
235 return nvm_readable(sw);
238 static int nvm_authenticate(struct tb_switch *sw, bool auth_only)
240 struct tb_nhi *nhi = sw->tb->nhi;
243 if (tb_switch_is_usb4(sw)) {
245 ret = usb4_switch_nvm_set_offset(sw, 0);
249 sw->nvm->authenticating = true;
250 return usb4_switch_nvm_authenticate(sw);
255 sw->nvm->authenticating = true;
256 if (!tb_route(sw)) {
259 ret = nvm_authenticate_host_dma_port(sw);
261 ret = nvm_authenticate_device_dma_port(sw);
269 * @sw: Router whose NVM to read
279 int tb_switch_nvm_read(struct tb_switch *sw, unsigned int address, void *buf,
282 if (tb_switch_is_usb4(sw))
283 return usb4_switch_nvm_read(sw, address, buf, size);
284 return dma_port_flash_read(sw->dma_port, address, buf, size);
290 struct tb_switch *sw = tb_to_switch(nvm->dev);
293 pm_runtime_get_sync(&sw->dev);
295 if (!mutex_trylock(&sw->tb->lock)) {
300 ret = tb_switch_nvm_read(sw, offset, val, bytes);
301 mutex_unlock(&sw->tb->lock);
304 pm_runtime_mark_last_busy(&sw->dev);
305 pm_runtime_put_autosuspend(&sw->dev);
313 struct tb_switch *sw = tb_to_switch(nvm->dev);
316 if (!mutex_trylock(&sw->tb->lock))
326 mutex_unlock(&sw->tb->lock);
331 static int tb_switch_nvm_init(struct tb_switch *sw)
336 if (!nvm_readable(sw))
339 nvm = tb_nvm_alloc(&sw->dev);
349 sw->nvm = nvm;
353 tb_sw_dbg(sw, "NVM upgrade disabled\n");
354 sw->no_nvm_upgrade = true;
361 static int tb_switch_nvm_add(struct tb_switch *sw)
363 struct tb_nvm *nvm = sw->nvm;
374 if (!sw->safe_mode) {
378 tb_sw_dbg(sw, "NVM version %x.%x\n", nvm->major, nvm->minor);
381 if (!sw->no_nvm_upgrade) {
390 tb_sw_dbg(sw, "NVM upgrade disabled\n");
391 sw->no_nvm_upgrade = true;
397 static void tb_switch_nvm_remove(struct tb_switch *sw)
401 nvm = sw->nvm;
402 sw->nvm = NULL;
409 nvm_clear_auth_status(sw);
574 if (credits == 0 || port->sw->is_unplugged)
581 if (tb_switch_is_usb4(port->sw) && !tb_port_is_null(port))
625 if (tb_switch_is_icm(port->sw))
629 if (tb_switch_is_usb4(port->sw))
699 if (tb_switch_is_usb4(port->sw))
727 tb_dbg(port->sw->tb, " Port %d: not implemented\n",
772 tb_dump_port(port->sw->tb, port);
803 tb_switch_get(port->sw);
844 tb_switch_put(port->sw);
855 tb_switch_put(port->sw);
859 const struct tb_switch *sw)
862 return (tb_route(parent) & mask) == (tb_route(sw) & mask);
888 if (prev->sw == end->sw) {
894 if (tb_switch_is_reachable(prev->sw, end->sw)) {
895 next = tb_port_at(tb_route(end->sw), prev->sw);
904 next = tb_upstream_port(prev->sw);
1306 if (tb_switch_is_usb4(port->sw))
1497 if (tb_switch_is_usb4(port->sw))
1567 static const char *tb_switch_generation_name(const struct tb_switch *sw)
1569 switch (sw->generation) {
1583 static void tb_dump_switch(const struct tb *tb, const struct tb_switch *sw)
1585 const struct tb_regs_switch_header *regs = &sw->config;
1588 tb_switch_generation_name(sw), regs->vendor_id, regs->device_id,
1601 static int tb_switch_reset_host(struct tb_switch *sw)
1603 if (sw->generation > 1) {
1606 tb_switch_for_each_port(sw, port) {
1625 if (tb_switch_is_usb4(sw) && !port->usb4)
1652 res.err = tb_sw_write(sw, ((u32 *) &sw->config) + 2,
1656 res = tb_cfg_reset(sw->tb->ctl, tb_route(sw));
1666 static int tb_switch_reset_device(struct tb_switch *sw)
1668 return tb_port_reset(tb_switch_downstream_port(sw));
1671 static bool tb_switch_enumerated(struct tb_switch *sw)
1678 * during system sleep where sw->config.enabled is already set
1681 ret = tb_sw_read(sw, &val, TB_CFG_SWITCH, ROUTER_CS_3, 1);
1690 * @sw: Router to reset
1692 * Issues reset to the router @sw. Can be used for any router. For host
1702 int tb_switch_reset(struct tb_switch *sw)
1711 if (!tb_switch_enumerated(sw))
1714 tb_sw_dbg(sw, "resetting\n");
1716 if (tb_route(sw))
1717 ret = tb_switch_reset_device(sw);
1719 ret = tb_switch_reset_host(sw);
1722 tb_sw_warn(sw, "failed to reset\n");
1729 * @sw: Router to read the offset value from
1743 int tb_switch_wait_for_bit(struct tb_switch *sw, u32 offset, u32 bit,
1752 ret = tb_sw_read(sw, &val, TB_CFG_SWITCH, offset, 1);
1770 static int tb_plug_events_active(struct tb_switch *sw, bool active)
1775 if (tb_switch_is_icm(sw) || tb_switch_is_usb4(sw))
1778 res = tb_sw_read(sw, &data, TB_CFG_SWITCH, sw->cap_plug_events + 1, 1);
1784 switch (sw->config.device_id) {
1795 if (!tb_switch_is_alpine_ridge(sw))
1801 return tb_sw_write(sw, &data, TB_CFG_SWITCH,
1802 sw->cap_plug_events + 1, 1);
1809 struct tb_switch *sw = tb_to_switch(dev);
1811 return sysfs_emit(buf, "%u\n", sw->authorized);
1817 struct tb_switch *sw;
1819 sw = tb_to_switch(dev);
1820 if (sw && sw->authorized) {
1824 ret = device_for_each_child_reverse(&sw->dev, NULL, disapprove_switch);
1828 ret = tb_domain_disapprove_switch(sw->tb, sw);
1832 sw->authorized = 0;
1833 kobject_uevent_env(&sw->dev.kobj, KOBJ_CHANGE, envp);
1839 static int tb_switch_set_authorized(struct tb_switch *sw, unsigned int val)
1845 if (!mutex_trylock(&sw->tb->lock))
1848 if (!!sw->authorized == !!val)
1854 if (tb_route(sw)) {
1855 ret = disapprove_switch(&sw->dev, NULL);
1862 if (sw->key)
1863 ret = tb_domain_approve_switch_key(sw->tb, sw);
1865 ret = tb_domain_approve_switch(sw->tb, sw);
1870 if (sw->key)
1871 ret = tb_domain_challenge_switch_key(sw->tb, sw);
1879 sw->authorized = val;
1884 sprintf(envp_string, "AUTHORIZED=%u", sw->authorized);
1885 kobject_uevent_env(&sw->dev.kobj, KOBJ_CHANGE, envp);
1889 mutex_unlock(&sw->tb->lock);
1897 struct tb_switch *sw = tb_to_switch(dev);
1907 pm_runtime_get_sync(&sw->dev);
1908 ret = tb_switch_set_authorized(sw, val);
1909 pm_runtime_mark_last_busy(&sw->dev);
1910 pm_runtime_put_autosuspend(&sw->dev);
1919 struct tb_switch *sw = tb_to_switch(dev);
1921 return sysfs_emit(buf, "%u\n", sw->boot);
1928 struct tb_switch *sw = tb_to_switch(dev);
1930 return sysfs_emit(buf, "%#x\n", sw->device);
1937 struct tb_switch *sw = tb_to_switch(dev);
1939 return sysfs_emit(buf, "%s\n", sw->device_name ?: "");
1946 struct tb_switch *sw = tb_to_switch(dev);
1948 return sysfs_emit(buf, "%u\n", sw->generation);
1955 struct tb_switch *sw = tb_to_switch(dev);
1958 if (!mutex_trylock(&sw->tb->lock))
1961 if (sw->key)
1962 ret = sysfs_emit(buf, "%*phN\n", TB_SWITCH_KEY_SIZE, sw->key);
1966 mutex_unlock(&sw->tb->lock);
1973 struct tb_switch *sw = tb_to_switch(dev);
1983 if (!mutex_trylock(&sw->tb->lock))
1986 if (sw->authorized) {
1989 kfree(sw->key);
1991 sw->key = NULL;
1993 sw->key = kmemdup(key, sizeof(key), GFP_KERNEL);
1994 if (!sw->key)
1999 mutex_unlock(&sw->tb->lock);
2007 struct tb_switch *sw = tb_to_switch(dev);
2009 return sysfs_emit(buf, "%u.0 Gb/s\n", sw->link_speed);
2022 struct tb_switch *sw = tb_to_switch(dev);
2025 switch (sw->link_width) {
2048 struct tb_switch *sw = tb_to_switch(dev);
2051 switch (sw->link_width) {
2074 struct tb_switch *sw = tb_to_switch(dev);
2077 nvm_get_auth_status(sw, &status);
2084 struct tb_switch *sw = tb_to_switch(dev);
2087 pm_runtime_get_sync(&sw->dev);
2089 if (!mutex_trylock(&sw->tb->lock)) {
2094 if (sw->no_nvm_upgrade) {
2100 if (!sw->nvm) {
2110 nvm_clear_auth_status(sw);
2117 ret = nvm_authenticate(sw, true);
2119 if (!sw->nvm->flushed) {
2120 if (!sw->nvm->buf) {
2125 ret = nvm_validate_and_write(sw);
2131 ret = tb_lc_force_power(sw);
2133 ret = nvm_authenticate(sw, false);
2139 mutex_unlock(&sw->tb->lock);
2141 pm_runtime_mark_last_busy(&sw->dev);
2142 pm_runtime_put_autosuspend(&sw->dev);
2176 struct tb_switch *sw = tb_to_switch(dev);
2179 if (!mutex_trylock(&sw->tb->lock))
2182 if (sw->safe_mode)
2184 else if (!sw->nvm)
2187 ret = sysfs_emit(buf, "%x.%x\n", sw->nvm->major, sw->nvm->minor);
2189 mutex_unlock(&sw->tb->lock);
2198 struct tb_switch *sw = tb_to_switch(dev);
2200 return sysfs_emit(buf, "%#x\n", sw->vendor);
2207 struct tb_switch *sw = tb_to_switch(dev);
2209 return sysfs_emit(buf, "%s\n", sw->vendor_name ?: "");
2216 struct tb_switch *sw = tb_to_switch(dev);
2218 return sysfs_emit(buf, "%pUb\n", sw->uuid);
2246 struct tb_switch *sw = tb_to_switch(dev);
2249 if (sw->tb->security_level == TB_SECURITY_NOPCIE ||
2250 sw->tb->security_level == TB_SECURITY_DPONLY)
2253 if (!sw->device)
2256 if (!sw->device_name)
2259 if (!sw->vendor)
2262 if (!sw->vendor_name)
2265 if (tb_route(sw) &&
2266 sw->tb->security_level == TB_SECURITY_SECURE &&
2267 sw->security_level == TB_SECURITY_SECURE)
2274 if (tb_route(sw))
2278 if (nvm_upgradeable(sw))
2282 if (nvm_readable(sw))
2286 if (tb_route(sw))
2290 if (sw->quirks & QUIRK_FORCE_POWER_LINK_CONTROLLER)
2295 return sw->safe_mode ? 0 : attr->mode;
2310 struct tb_switch *sw = tb_to_switch(dev);
2313 dma_port_free(sw->dma_port);
2315 tb_switch_for_each_port(sw, port) {
2320 kfree(sw->uuid);
2321 kfree(sw->device_name);
2322 kfree(sw->vendor_name);
2323 kfree(sw->ports);
2324 kfree(sw->drom);
2325 kfree(sw->key);
2326 kfree(sw);
2331 const struct tb_switch *sw = tb_to_switch(dev);
2334 if (tb_switch_is_usb4(sw)) {
2336 usb4_switch_version(sw)))
2340 if (!tb_route(sw)) {
2347 tb_switch_for_each_port(sw, port) {
2369 struct tb_switch *sw = tb_to_switch(dev);
2370 const struct tb_cm_ops *cm_ops = sw->tb->cm_ops;
2373 return cm_ops->runtime_suspend_switch(sw);
2380 struct tb_switch *sw = tb_to_switch(dev);
2381 const struct tb_cm_ops *cm_ops = sw->tb->cm_ops;
2384 return cm_ops->runtime_resume_switch(sw);
2400 static int tb_switch_get_generation(struct tb_switch *sw)
2402 if (tb_switch_is_usb4(sw))
2405 if (sw->config.vendor_id == PCI_VENDOR_ID_INTEL) {
2406 switch (sw->config.device_id) {
2440 tb_sw_warn(sw, "unsupported switch device id %#x\n",
2441 sw->config.device_id);
2445 static bool tb_switch_exceeds_max_depth(const struct tb_switch *sw, int depth)
2449 if (tb_switch_is_usb4(sw) ||
2450 (sw->tb->root_switch && tb_switch_is_usb4(sw->tb->root_switch)))
2474 struct tb_switch *sw;
2493 sw = kzalloc_obj(*sw);
2494 if (!sw)
2497 sw->tb = tb;
2498 ret = tb_cfg_read(tb->ctl, &sw->config, route, 0, TB_CFG_SWITCH, 0, 5);
2502 sw->generation = tb_switch_get_generation(sw);
2505 tb_dump_switch(tb, sw);
2508 sw->config.upstream_port_number = upstream_port;
2509 sw->config.depth = depth;
2510 sw->config.route_hi = upper_32_bits(route);
2511 sw->config.route_lo = lower_32_bits(route);
2512 sw->config.enabled = 0;
2515 if (tb_switch_exceeds_max_depth(sw, depth)) {
2521 sw->ports = kzalloc_objs(*sw->ports, sw->config.max_port_number + 1);
2522 if (!sw->ports) {
2527 for (i = 0; i <= sw->config.max_port_number; i++) {
2529 sw->ports[i].sw = sw;
2530 sw->ports[i].port = i;
2534 ida_init(&sw->ports[i].in_hopids);
2535 ida_init(&sw->ports[i].out_hopids);
2539 ret = tb_switch_find_vse_cap(sw, TB_VSE_CAP_PLUG_EVENTS);
2541 sw->cap_plug_events = ret;
2543 ret = tb_switch_find_vse_cap(sw, TB_VSE_CAP_TIME2);
2545 sw->cap_vsec_tmu = ret;
2547 ret = tb_switch_find_vse_cap(sw, TB_VSE_CAP_LINK_CONTROLLER);
2549 sw->cap_lc = ret;
2551 ret = tb_switch_find_vse_cap(sw, TB_VSE_CAP_CP_LP);
2553 sw->cap_lp = ret;
2557 sw->authorized = true;
2559 device_initialize(&sw->dev);
2560 sw->dev.parent = parent;
2561 sw->dev.bus = &tb_bus_type;
2562 sw->dev.type = &tb_switch_type;
2563 sw->dev.groups = switch_groups;
2564 dev_set_name(&sw->dev, "%u-%llx", tb->index, tb_route(sw));
2566 return sw;
2569 kfree(sw->ports);
2570 kfree(sw);
2592 struct tb_switch *sw;
2594 sw = kzalloc_obj(*sw);
2595 if (!sw)
2598 sw->tb = tb;
2599 sw->config.depth = tb_route_length(route);
2600 sw->config.route_hi = upper_32_bits(route);
2601 sw->config.route_lo = lower_32_bits(route);
2602 sw->safe_mode = true;
2604 device_initialize(&sw->dev);
2605 sw->dev.parent = parent;
2606 sw->dev.bus = &tb_bus_type;
2607 sw->dev.type = &tb_switch_type;
2608 sw->dev.groups = switch_groups;
2609 dev_set_name(&sw->dev, "%u-%llx", tb->index, tb_route(sw));
2611 return sw;
2616 * @sw: Switch to configure
2625 int tb_switch_configure(struct tb_switch *sw)
2627 struct tb *tb = sw->tb;
2631 route = tb_route(sw);
2634 sw->config.enabled ? "restoring" : "initializing", route,
2635 tb_route_length(route), sw->config.upstream_port_number);
2637 sw->config.enabled = 1;
2640 sw->config.plug_events_delay = 0xff;
2641 if (tb_switch_is_usb4(sw)) {
2648 if (usb4_switch_version(sw) < 2)
2649 sw->config.cmuv = ROUTER_CS_4_CMUV_V1;
2651 sw->config.cmuv = ROUTER_CS_4_CMUV_V2;
2654 ret = tb_sw_write(sw, (u32 *)&sw->config + 1, TB_CFG_SWITCH,
2659 ret = usb4_switch_setup(sw);
2661 if (sw->config.vendor_id != PCI_VENDOR_ID_INTEL)
2662 tb_sw_warn(sw, "unknown switch vendor id %#x\n",
2663 sw->config.vendor_id);
2665 if (!sw->cap_plug_events) {
2666 tb_sw_warn(sw, "cannot find TB_VSE_CAP_PLUG_EVENTS aborting\n");
2671 ret = tb_sw_write(sw, (u32 *)&sw->config + 1, TB_CFG_SWITCH,
2677 return tb_plug_events_active(sw, true);
2682 * @sw: Router to configure
2689 int tb_switch_configuration_valid(struct tb_switch *sw)
2691 if (tb_switch_is_usb4(sw))
2692 return usb4_switch_configuration_valid(sw);
2696 static int tb_switch_set_uuid(struct tb_switch *sw)
2702 if (sw->uuid)
2705 if (tb_switch_is_usb4(sw)) {
2706 ret = usb4_switch_read_uid(sw, &sw->uid);
2715 ret = tb_lc_read_uuid(sw, uuid);
2730 uuid[0] = sw->uid & 0xffffffff;
2731 uuid[1] = (sw->uid >> 32) & 0xffffffff;
2736 sw->uuid = kmemdup(uuid, sizeof(uuid), GFP_KERNEL);
2737 if (!sw->uuid)
2742 static int tb_switch_add_dma_port(struct tb_switch *sw)
2744 struct tb_nhi *nhi = sw->tb->nhi;
2748 switch (sw->generation) {
2751 if (tb_route(sw))
2757 ret = tb_switch_set_uuid(sw);
2767 if (!sw->safe_mode)
2772 if (sw->no_nvm_upgrade)
2775 if (tb_switch_is_usb4(sw)) {
2776 ret = usb4_switch_nvm_authenticate_status(sw, &status);
2781 tb_sw_info(sw, "switch flash authentication failed\n");
2782 nvm_set_auth_status(sw, status);
2789 if (!tb_route(sw) && !tb_switch_is_icm(sw))
2792 sw->dma_port = dma_port_alloc(sw);
2793 if (!sw->dma_port)
2802 nvm_get_auth_status(sw, &status);
2804 if (!tb_route(sw)) {
2816 ret = dma_port_flash_update_auth_status(sw->dma_port, &status);
2821 if (!tb_route(sw)) {
2827 tb_sw_info(sw, "switch flash authentication failed\n");
2828 nvm_set_auth_status(sw, status);
2831 tb_sw_info(sw, "power cycling the switch now\n");
2832 dma_port_power_cycle(sw->dma_port);
2841 static void tb_switch_default_link_ports(struct tb_switch *sw)
2845 for (i = 1; i <= sw->config.max_port_number; i++) {
2846 struct tb_port *port = &sw->ports[i];
2853 if (i == sw->config.max_port_number ||
2854 !tb_port_is_null(&sw->ports[i + 1]))
2858 subordinate = &sw->ports[i + 1];
2865 tb_sw_dbg(sw, "linked ports %d <-> %d\n",
2871 static bool tb_switch_lane_bonding_possible(struct tb_switch *sw)
2873 const struct tb_port *up = tb_upstream_port(sw);
2878 if (tb_switch_is_usb4(sw))
2879 return usb4_switch_lane_bonding_possible(sw);
2880 return tb_lc_lane_bonding_possible(sw);
2883 static int tb_switch_update_link_attributes(struct tb_switch *sw)
2889 if (!tb_route(sw) || tb_switch_is_icm(sw))
2892 up = tb_upstream_port(sw);
2897 if (sw->link_speed != ret)
2899 sw->link_speed = ret;
2904 if (sw->link_width != ret)
2906 sw->link_width = ret;
2909 if (device_is_registered(&sw->dev) && change)
2910 kobject_uevent(&sw->dev.kobj, KOBJ_CHANGE);
2916 static void tb_switch_link_init(struct tb_switch *sw)
2921 if (!tb_route(sw) || tb_switch_is_icm(sw))
2924 tb_sw_dbg(sw, "current link speed %u.0 Gb/s\n", sw->link_speed);
2925 tb_sw_dbg(sw, "current link width %s\n", tb_width_name(sw->link_width));
2927 bonded = sw->link_width >= TB_LINK_WIDTH_DUAL;
2933 up = tb_upstream_port(sw);
2934 down = tb_switch_downstream_port(sw);
2955 sw->preferred_link_width = sw->link_width;
2956 tb_sw_dbg(sw, "preferred link width %s\n",
2957 tb_width_name(sw->preferred_link_width));
2962 * @sw: Switch to enable lane bonding
2970 static int tb_switch_lane_bonding_enable(struct tb_switch *sw)
2976 if (!tb_switch_lane_bonding_possible(sw))
2979 up = tb_upstream_port(sw);
2980 down = tb_switch_downstream_port(sw);
3015 * @sw: Switch whose lane bonding to disable
3017 * Disables lane bonding between @sw and parent. This can be called even
3022 static int tb_switch_lane_bonding_disable(struct tb_switch *sw)
3027 up = tb_upstream_port(sw);
3034 * if the link is not up anymore (sw is unplugged).
3042 down = tb_switch_downstream_port(sw);
3053 /* Note updating sw->link_width done in tb_switch_update_link_attributes() */
3054 static int tb_switch_asym_enable(struct tb_switch *sw, enum tb_link_width width)
3060 up = tb_upstream_port(sw);
3061 down = tb_switch_downstream_port(sw);
3083 if (sw->link_width != width) {
3096 /* Note updating sw->link_width done in tb_switch_update_link_attributes() */
3097 static int tb_switch_asym_disable(struct tb_switch *sw)
3102 up = tb_upstream_port(sw);
3103 down = tb_switch_downstream_port(sw);
3118 if (sw->link_width > TB_LINK_WIDTH_DUAL) {
3119 if (sw->link_width == TB_LINK_WIDTH_ASYM_TX)
3136 * @sw: Router to configure
3147 int tb_switch_set_link_width(struct tb_switch *sw, enum tb_link_width width)
3152 if (!tb_route(sw))
3155 up = tb_upstream_port(sw);
3156 down = tb_switch_downstream_port(sw);
3160 ret = tb_switch_lane_bonding_disable(sw);
3164 if (sw->link_width == TB_LINK_WIDTH_ASYM_TX ||
3165 sw->link_width == TB_LINK_WIDTH_ASYM_RX) {
3166 ret = tb_switch_asym_disable(sw);
3170 ret = tb_switch_lane_bonding_enable(sw);
3175 ret = tb_switch_asym_enable(sw, width);
3184 tb_sw_warn(sw, "timeout changing link width\n");
3193 tb_sw_dbg(sw, "failed to change link width: %d\n", ret);
3200 tb_switch_update_link_attributes(sw);
3202 tb_sw_dbg(sw, "link width set to %s\n", tb_width_name(width));
3208 * @sw: Switch whose link is configured
3210 * Sets the link upstream from @sw configured (from both ends) so that
3218 int tb_switch_configure_link(struct tb_switch *sw)
3223 if (!tb_route(sw) || tb_switch_is_icm(sw))
3226 up = tb_upstream_port(sw);
3227 if (tb_switch_is_usb4(up->sw))
3235 if (tb_switch_is_usb4(down->sw))
3242 * @sw: Switch whose link is unconfigured
3244 * Sets the link unconfigured so the @sw will be disconnected if the
3247 void tb_switch_unconfigure_link(struct tb_switch *sw)
3251 if (!tb_route(sw) || tb_switch_is_icm(sw))
3259 up = tb_upstream_port(sw);
3261 if (tb_switch_is_usb4(down->sw))
3266 if (sw->is_unplugged)
3269 up = tb_upstream_port(sw);
3270 if (tb_switch_is_usb4(up->sw))
3276 static void tb_switch_credits_init(struct tb_switch *sw)
3278 if (tb_switch_is_icm(sw))
3280 if (!tb_switch_is_usb4(sw))
3282 if (usb4_switch_credits_init(sw))
3283 tb_sw_info(sw, "failed to determine preferred buffer allocation, using defaults\n");
3286 static int tb_switch_port_hotplug_enable(struct tb_switch *sw)
3290 if (tb_switch_is_icm(sw))
3293 tb_switch_for_each_port(sw, port) {
3308 * @sw: Switch to add
3318 int tb_switch_add(struct tb_switch *sw)
3329 ret = tb_switch_add_dma_port(sw);
3331 dev_err(&sw->dev, "failed to add DMA port\n");
3335 ret = tb_switch_nvm_init(sw);
3339 if (!sw->safe_mode) {
3340 tb_switch_credits_init(sw);
3343 ret = tb_drom_read(sw);
3345 dev_warn(&sw->dev, "reading DROM failed: %d\n", ret);
3346 tb_sw_dbg(sw, "uid: %#llx\n", sw->uid);
3348 ret = tb_switch_set_uuid(sw);
3350 dev_err(&sw->dev, "failed to set UUID\n");
3354 for (i = 0; i <= sw->config.max_port_number; i++) {
3355 if (sw->ports[i].disabled) {
3356 tb_port_dbg(&sw->ports[i], "disabled by eeprom\n");
3359 ret = tb_init_port(&sw->ports[i]);
3361 dev_err(&sw->dev, "failed to initialize port %d\n", i);
3366 tb_check_quirks(sw);
3368 tb_switch_default_link_ports(sw);
3370 ret = tb_switch_update_link_attributes(sw);
3374 tb_switch_link_init(sw);
3376 ret = tb_switch_clx_init(sw);
3380 ret = tb_switch_tmu_init(sw);
3385 ret = tb_switch_port_hotplug_enable(sw);
3389 ret = device_add(&sw->dev);
3391 dev_err(&sw->dev, "failed to add device: %d\n", ret);
3395 if (tb_route(sw)) {
3396 dev_info(&sw->dev, "new device found, vendor=%#x device=%#x\n",
3397 sw->vendor, sw->device);
3398 if (sw->vendor_name && sw->device_name)
3399 dev_info(&sw->dev, "%s %s\n", sw->vendor_name,
3400 sw->device_name);
3403 ret = usb4_switch_add_ports(sw);
3405 dev_err(&sw->dev, "failed to add USB4 ports\n");
3409 ret = tb_switch_nvm_add(sw);
3411 dev_err(&sw->dev, "failed to add NVM devices\n");
3420 device_init_wakeup(&sw->dev, true);
3422 pm_runtime_set_active(&sw->dev);
3423 if (sw->rpm) {
3424 pm_runtime_set_autosuspend_delay(&sw->dev, TB_AUTOSUSPEND_DELAY);
3425 pm_runtime_use_autosuspend(&sw->dev);
3426 pm_runtime_mark_last_busy(&sw->dev);
3427 pm_runtime_enable(&sw->dev);
3428 pm_request_autosuspend(&sw->dev);
3431 tb_switch_debugfs_init(sw);
3435 usb4_switch_remove_ports(sw);
3437 device_del(&sw->dev);
3444 * @sw: Switch to remove
3450 void tb_switch_remove(struct tb_switch *sw)
3454 tb_switch_debugfs_remove(sw);
3456 if (sw->rpm) {
3457 pm_runtime_get_sync(&sw->dev);
3458 pm_runtime_disable(&sw->dev);
3462 tb_switch_for_each_port(sw, port) {
3464 tb_switch_remove(port->remote->sw);
3476 if (!sw->is_unplugged)
3477 tb_plug_events_active(sw, false);
3479 tb_switch_nvm_remove(sw);
3480 usb4_switch_remove_ports(sw);
3482 if (tb_route(sw))
3483 dev_info(&sw->dev, "device disconnected\n");
3484 device_unregister(&sw->dev);
3489 * @sw: Router to mark unplugged
3491 void tb_sw_set_unplugged(struct tb_switch *sw)
3495 if (sw == sw->tb->root_switch) {
3496 tb_sw_WARN(sw, "cannot unplug root switch\n");
3499 if (sw->is_unplugged) {
3500 tb_sw_WARN(sw, "is_unplugged already set\n");
3503 sw->is_unplugged = true;
3504 tb_switch_for_each_port(sw, port) {
3506 tb_sw_set_unplugged(port->remote->sw);
3512 static int tb_switch_set_wake(struct tb_switch *sw, unsigned int flags, bool runtime)
3515 tb_sw_dbg(sw, "enabling wakeup: %#x\n", flags);
3517 tb_sw_dbg(sw, "disabling wakeup\n");
3519 if (tb_switch_is_usb4(sw))
3520 return usb4_switch_set_wake(sw, flags, runtime);
3521 return tb_lc_set_wake(sw, flags);
3524 static void tb_switch_check_wakes(struct tb_switch *sw)
3526 if (device_may_wakeup(&sw->dev)) {
3527 if (tb_switch_is_usb4(sw))
3528 usb4_switch_check_wakes(sw);
3534 * @sw: Switch to resume
3545 int tb_switch_resume(struct tb_switch *sw, bool runtime)
3550 tb_sw_dbg(sw, "resuming switch\n");
3556 if (tb_route(sw)) {
3564 err = tb_cfg_get_upstream_port(sw->tb->ctl, tb_route(sw));
3566 tb_sw_info(sw, "switch not present anymore\n");
3571 if (!sw->uid)
3574 if (tb_switch_is_usb4(sw))
3575 err = usb4_switch_read_uid(sw, &uid);
3577 err = tb_drom_read_uid_only(sw, &uid);
3579 tb_sw_warn(sw, "uid read failed\n");
3582 if (sw->uid != uid) {
3583 tb_sw_info(sw,
3585 sw->uid, uid);
3590 err = tb_switch_configure(sw);
3595 tb_switch_check_wakes(sw);
3598 tb_switch_set_wake(sw, 0, true);
3600 err = tb_switch_tmu_init(sw);
3605 tb_switch_for_each_port(sw, port) {
3616 tb_sw_set_unplugged(port->remote->sw);
3627 tb_switch_resume(port->remote->sw, runtime)) {
3630 tb_sw_set_unplugged(port->remote->sw);
3638 err = tb_cfg_get_upstream_port(sw->tb->ctl,
3653 * @sw: Switch to suspend
3657 * value of @runtime and then sets sleep bit for the router. If @sw is
3661 void tb_switch_suspend(struct tb_switch *sw, bool runtime)
3667 tb_sw_dbg(sw, "suspending switch\n");
3673 tb_switch_clx_disable(sw);
3675 err = tb_plug_events_active(sw, false);
3679 tb_switch_for_each_port(sw, port) {
3681 tb_switch_suspend(port->remote->sw, runtime);
3689 } else if (device_may_wakeup(&sw->dev)) {
3694 tb_switch_set_wake(sw, flags, runtime);
3696 if (tb_switch_is_usb4(sw))
3697 usb4_switch_set_sleep(sw);
3699 tb_lc_set_sleep(sw);
3704 * @sw: Switch whose DP resource is queried
3712 bool tb_switch_query_dp_resource(struct tb_switch *sw, struct tb_port *in)
3714 if (tb_switch_is_usb4(sw))
3715 return usb4_switch_query_dp_resource(sw, in);
3716 return tb_lc_dp_sink_query(sw, in);
3721 * @sw: Switch whose DP resource is allocated
3729 int tb_switch_alloc_dp_resource(struct tb_switch *sw, struct tb_port *in)
3733 if (tb_switch_is_usb4(sw))
3734 ret = usb4_switch_alloc_dp_resource(sw, in);
3736 ret = tb_lc_dp_sink_alloc(sw, in);
3739 tb_sw_warn(sw, "failed to allocate DP resource for port %d\n",
3742 tb_sw_dbg(sw, "allocated DP resource for port %d\n", in->port);
3749 * @sw: Switch whose DP resource is de-allocated
3755 void tb_switch_dealloc_dp_resource(struct tb_switch *sw, struct tb_port *in)
3759 if (tb_switch_is_usb4(sw))
3760 ret = usb4_switch_dealloc_dp_resource(sw, in);
3762 ret = tb_lc_dp_sink_dealloc(sw, in);
3765 tb_sw_warn(sw, "failed to de-allocate DP resource for port %d\n",
3768 tb_sw_dbg(sw, "released DP resource for port %d\n", in->port);
3781 struct tb_switch *sw = tb_to_switch(dev);
3784 if (!sw)
3786 if (sw->tb != lookup->tb)
3790 return !memcmp(sw->uuid, lookup->uuid, sizeof(*lookup->uuid));
3793 return sw->config.route_lo == lower_32_bits(lookup->route) &&
3794 sw->config.route_hi == upper_32_bits(lookup->route);
3799 return !sw->depth;
3801 return sw->link == lookup->link && sw->depth == lookup->depth;
3888 * tb_switch_find_port() - return the first port of @type on @sw or NULL
3889 * @sw: Switch to find the port from
3894 struct tb_port *tb_switch_find_port(struct tb_switch *sw,
3899 tb_switch_for_each_port(sw, port) {
3911 static int tb_switch_pcie_bridge_write(struct tb_switch *sw, unsigned int bridge,
3917 if (sw->generation != 3)
3920 offset = sw->cap_plug_events + TB_PLUG_EVENTS_PCIE_WR_DATA;
3921 ret = tb_sw_write(sw, &value, TB_CFG_SWITCH, offset, 1);
3932 offset = sw->cap_plug_events + TB_PLUG_EVENTS_PCIE_CMD;
3934 ret = tb_sw_write(sw, &command, TB_CFG_SWITCH, offset, 1);
3938 ret = tb_switch_wait_for_bit(sw, offset,
3943 ret = tb_sw_read(sw, &val, TB_CFG_SWITCH, offset, 1);
3955 * @sw: Router to enable PCIe L1
3964 int tb_switch_pcie_l1_enable(struct tb_switch *sw)
3966 struct tb_switch *parent = tb_switch_parent(sw);
3969 if (!tb_route(sw))
3972 if (!tb_switch_is_titan_ridge(sw))
3980 ret = tb_switch_pcie_bridge_write(sw, 5, 0x143, 0x0c7806b1);
3985 return tb_switch_pcie_bridge_write(sw, 0, 0x143, 0x0c5806b1);
3990 * @sw: Router whose xHCI to connect
4000 int tb_switch_xhci_connect(struct tb_switch *sw)
4005 if (sw->generation != 3)
4008 port1 = &sw->ports[1];
4009 port3 = &sw->ports[3];
4011 if (tb_switch_is_alpine_ridge(sw)) {
4027 } else if (tb_switch_is_titan_ridge(sw)) {
4039 * @sw: Router whose xHCI to disconnect
4044 void tb_switch_xhci_disconnect(struct tb_switch *sw)
4046 if (sw->generation == 3) {
4047 struct tb_port *port1 = &sw->ports[1];
4048 struct tb_port *port3 = &sw->ports[3];