Lines Matching +full:use +full:- +full:sw +full:- +full:pm

1 // SPDX-License-Identifier: GPL-2.0
3 * Thunderbolt driver - bus logic (NHI independent)
24 * direction. This is 40G - 10% guard band bandwidth.
47 * struct tb_cm - Simple Thunderbolt connection manager
68 return ((void *)tcm - sizeof(struct tb)); in tcm_to_tb()
89 ev->tb = tb; in tb_queue_hotplug()
90 ev->route = route; in tb_queue_hotplug()
91 ev->port = port; in tb_queue_hotplug()
92 ev->unplug = unplug; in tb_queue_hotplug()
93 INIT_WORK(&ev->work, tb_handle_hotplug); in tb_queue_hotplug()
94 queue_work(tb->wq, &ev->work); in tb_queue_hotplug()
99 static void tb_add_dp_resources(struct tb_switch *sw) in tb_add_dp_resources() argument
101 struct tb_cm *tcm = tb_priv(sw->tb); in tb_add_dp_resources()
104 tb_switch_for_each_port(sw, port) { in tb_add_dp_resources()
108 if (!tb_switch_query_dp_resource(sw, port)) in tb_add_dp_resources()
117 if (tb_route(sw)) in tb_add_dp_resources()
118 list_add(&port->list, &tcm->dp_resources); in tb_add_dp_resources()
120 list_add_tail(&port->list, &tcm->dp_resources); in tb_add_dp_resources()
126 static void tb_remove_dp_resources(struct tb_switch *sw) in tb_remove_dp_resources() argument
128 struct tb_cm *tcm = tb_priv(sw->tb); in tb_remove_dp_resources()
132 tb_switch_for_each_port(sw, port) { in tb_remove_dp_resources()
134 tb_remove_dp_resources(port->remote->sw); in tb_remove_dp_resources()
137 list_for_each_entry_safe(port, tmp, &tcm->dp_resources, list) { in tb_remove_dp_resources()
138 if (port->sw == sw) { in tb_remove_dp_resources()
140 list_del_init(&port->list); in tb_remove_dp_resources()
150 list_for_each_entry(p, &tcm->dp_resources, list) { in tb_discover_dp_resource()
157 list_add_tail(&port->list, &tcm->dp_resources); in tb_discover_dp_resource()
165 list_for_each_entry(tunnel, &tcm->tunnel_list, list) { in tb_discover_dp_resources()
167 tb_discover_dp_resource(tb, tunnel->dst_port); in tb_discover_dp_resources()
172 static int tb_enable_clx(struct tb_switch *sw) in tb_enable_clx() argument
174 struct tb_cm *tcm = tb_priv(sw->tb); in tb_enable_clx()
186 while (sw && tb_switch_depth(sw) > 1) in tb_enable_clx()
187 sw = tb_switch_parent(sw); in tb_enable_clx()
189 if (!sw) in tb_enable_clx()
192 if (tb_switch_depth(sw) != 1) in tb_enable_clx()
196 * If we are re-enabling then check if there is an active DMA in tb_enable_clx()
199 list_for_each_entry(tunnel, &tcm->tunnel_list, list) { in tb_enable_clx()
201 if (tb_tunnel_port_on_path(tunnel, tb_upstream_port(sw))) in tb_enable_clx()
210 ret = tb_switch_clx_enable(sw, clx | TB_CL2); in tb_enable_clx()
211 if (ret == -EOPNOTSUPP) in tb_enable_clx()
212 ret = tb_switch_clx_enable(sw, clx); in tb_enable_clx()
213 return ret == -EOPNOTSUPP ? 0 : ret; in tb_enable_clx()
217 * tb_disable_clx() - Disable CL states up to host router
218 * @sw: Router to start
220 * Disables CL states from @sw up to the host router. Returns true if
225 static bool tb_disable_clx(struct tb_switch *sw) in tb_disable_clx() argument
232 ret = tb_switch_clx_disable(sw); in tb_disable_clx()
236 tb_sw_warn(sw, "failed to disable CL states\n"); in tb_disable_clx()
238 sw = tb_switch_parent(sw); in tb_disable_clx()
239 } while (sw); in tb_disable_clx()
246 struct tb_switch *sw; in tb_increase_switch_tmu_accuracy() local
248 sw = tb_to_switch(dev); in tb_increase_switch_tmu_accuracy()
249 if (!sw) in tb_increase_switch_tmu_accuracy()
252 if (tb_switch_tmu_is_configured(sw, TB_SWITCH_TMU_MODE_LOWRES)) { in tb_increase_switch_tmu_accuracy()
256 if (tb_switch_clx_is_enabled(sw, TB_CL1)) in tb_increase_switch_tmu_accuracy()
261 ret = tb_switch_tmu_configure(sw, mode); in tb_increase_switch_tmu_accuracy()
265 return tb_switch_tmu_enable(sw); in tb_increase_switch_tmu_accuracy()
273 struct tb_switch *sw; in tb_increase_tmu_accuracy() local
287 sw = tunnel->tb->root_switch; in tb_increase_tmu_accuracy()
288 device_for_each_child(&sw->dev, NULL, tb_increase_switch_tmu_accuracy); in tb_increase_tmu_accuracy()
293 struct tb_switch *sw = tb_to_switch(dev); in tb_switch_tmu_hifi_uni_required() local
295 if (sw && tb_switch_tmu_is_enabled(sw) && in tb_switch_tmu_hifi_uni_required()
296 tb_switch_tmu_is_configured(sw, TB_SWITCH_TMU_MODE_HIFI_UNI)) in tb_switch_tmu_hifi_uni_required()
305 return device_for_each_child(&tb->dev, NULL, in tb_tmu_hifi_uni_required()
309 static int tb_enable_tmu(struct tb_switch *sw) in tb_enable_tmu() argument
315 * enable the enhanched uni-directional mode. That covers all in tb_enable_tmu()
316 * the CL states. For v1 and before we need to use the normal in tb_enable_tmu()
320 ret = tb_switch_tmu_configure(sw, in tb_enable_tmu()
322 if (ret == -EOPNOTSUPP) { in tb_enable_tmu()
323 if (tb_switch_clx_is_enabled(sw, TB_CL1)) { in tb_enable_tmu()
325 * Figure out uni-directional HiFi TMU requirements in tb_enable_tmu()
327 * uni-directional HiFi requirements we can put the TMU in tb_enable_tmu()
330 * Deliberately skip bi-directional HiFi links in tb_enable_tmu()
334 if (tb_tmu_hifi_uni_required(sw->tb)) in tb_enable_tmu()
335 ret = tb_switch_tmu_configure(sw, in tb_enable_tmu()
338 ret = tb_switch_tmu_configure(sw, in tb_enable_tmu()
341 ret = tb_switch_tmu_configure(sw, TB_SWITCH_TMU_MODE_HIFI_BI); in tb_enable_tmu()
344 /* If not supported, fallback to bi-directional HiFi */ in tb_enable_tmu()
345 if (ret == -EOPNOTSUPP) in tb_enable_tmu()
346 ret = tb_switch_tmu_configure(sw, TB_SWITCH_TMU_MODE_HIFI_BI); in tb_enable_tmu()
352 if (tb_switch_tmu_is_enabled(sw)) in tb_enable_tmu()
355 ret = tb_switch_tmu_disable(sw); in tb_enable_tmu()
359 ret = tb_switch_tmu_post_time(sw); in tb_enable_tmu()
363 return tb_switch_tmu_enable(sw); in tb_enable_tmu()
366 static void tb_switch_discover_tunnels(struct tb_switch *sw, in tb_switch_discover_tunnels() argument
370 struct tb *tb = sw->tb; in tb_switch_discover_tunnels()
373 tb_switch_for_each_port(sw, port) { in tb_switch_discover_tunnels()
376 switch (port->config.type) { in tb_switch_discover_tunnels()
395 list_add_tail(&tunnel->list, list); in tb_switch_discover_tunnels()
398 tb_switch_for_each_port(sw, port) { in tb_switch_discover_tunnels()
400 tb_switch_discover_tunnels(port->remote->sw, list, in tb_switch_discover_tunnels()
408 if (tb_switch_is_usb4(port->sw)) in tb_port_configure_xdomain()
415 if (tb_switch_is_usb4(port->sw)) in tb_port_unconfigure_xdomain()
423 struct tb_switch *sw = port->sw; in tb_scan_xdomain() local
424 struct tb *tb = sw->tb; in tb_scan_xdomain()
438 xd = tb_xdomain_alloc(tb, &sw->dev, route, tb->root_switch->uuid, in tb_scan_xdomain()
441 tb_port_at(route, sw)->xdomain = xd; in tb_scan_xdomain()
448 * tb_find_unused_port() - return the first inactive port on @sw
449 * @sw: Switch to find the port on
452 static struct tb_port *tb_find_unused_port(struct tb_switch *sw, in tb_find_unused_port() argument
457 tb_switch_for_each_port(sw, port) { in tb_find_unused_port()
460 if (port->config.type != type) in tb_find_unused_port()
462 if (!port->cap_adap) in tb_find_unused_port()
471 static struct tb_port *tb_find_usb3_down(struct tb_switch *sw, in tb_find_usb3_down() argument
476 down = usb4_switch_map_usb3_down(sw, port); in tb_find_usb3_down()
489 list_for_each_entry(tunnel, &tcm->tunnel_list, list) { in tb_find_tunnel()
490 if (tunnel->type == type && in tb_find_tunnel()
491 ((src_port && src_port == tunnel->src_port) || in tb_find_tunnel()
492 (dst_port && dst_port == tunnel->dst_port))) { in tb_find_tunnel()
505 struct tb_switch *sw; in tb_find_first_usb3_tunnel() local
509 sw = dst_port->sw; in tb_find_first_usb3_tunnel()
511 sw = src_port->sw; in tb_find_first_usb3_tunnel()
514 if (sw == tb->root_switch) in tb_find_first_usb3_tunnel()
518 port = tb_port_at(tb_route(sw), tb->root_switch); in tb_find_first_usb3_tunnel()
520 usb3_down = usb4_switch_map_usb3_down(tb->root_switch, port); in tb_find_first_usb3_tunnel()
528 * tb_consumed_usb3_pcie_bandwidth() - Consumed USB3/PCIe bandwidth over a single link
577 * tb_consumed_dp_bandwidth() - Consumed DP bandwidth over a single link
612 list_for_each_entry(tunnel, &tcm->tunnel_list, list) { in tb_consumed_dp_bandwidth()
630 group = tunnel->src_port->group; in tb_consumed_dp_bandwidth()
631 if (group && group->reserved && !group_reserved[group->index]) in tb_consumed_dp_bandwidth()
632 group_reserved[group->index] = group->reserved; in tb_consumed_dp_bandwidth()
637 * re-calculating estimated bandwidth. in tb_consumed_dp_bandwidth()
639 if (tunnel->src_port == src_port && in tb_consumed_dp_bandwidth()
640 tunnel->dst_port == dst_port) in tb_consumed_dp_bandwidth()
678 * tb_maximum_bandwidth() - Maximum bandwidth over a single link
708 link_speed = port->sw->link_speed; in tb_maximum_bandwidth()
710 * sw->link_width is from upstream perspective so we use in tb_maximum_bandwidth()
713 if (port->sw->link_width == TB_LINK_WIDTH_ASYM_TX) { in tb_maximum_bandwidth()
716 } else if (port->sw->link_width == TB_LINK_WIDTH_ASYM_RX) { in tb_maximum_bandwidth()
734 up_bw = link_speed * port->sw->link_width * 1000; in tb_maximum_bandwidth()
773 *max_up = up_bw - up_bw / 10; in tb_maximum_bandwidth()
774 *max_down = down_bw - down_bw / 10; in tb_maximum_bandwidth()
781 * tb_available_bandwidth() - Available bandwidth for tunneling
826 max_up -= consumed_up; in tb_available_bandwidth()
827 max_down -= consumed_down; in tb_available_bandwidth()
833 max_up -= consumed_up; in tb_available_bandwidth()
834 max_down -= consumed_down; in tb_available_bandwidth()
876 ret = tb_available_bandwidth(tb, tunnel->src_port, tunnel->dst_port, in tb_reclaim_usb3_bandwidth()
889 static int tb_tunnel_usb3(struct tb *tb, struct tb_switch *sw) in tb_tunnel_usb3() argument
891 struct tb_switch *parent = tb_switch_parent(sw); in tb_tunnel_usb3()
902 up = tb_switch_find_port(sw, TB_TYPE_USB3_UP); in tb_tunnel_usb3()
906 if (!sw->link_usb4) in tb_tunnel_usb3()
913 port = tb_switch_downstream_port(sw); in tb_tunnel_usb3()
946 ret = -ENOMEM; in tb_tunnel_usb3()
953 ret = -EIO; in tb_tunnel_usb3()
957 list_add_tail(&tunnel->list, &tcm->tunnel_list); in tb_tunnel_usb3()
972 static int tb_create_usb3_tunnels(struct tb_switch *sw) in tb_create_usb3_tunnels() argument
980 if (tb_route(sw)) { in tb_create_usb3_tunnels()
981 ret = tb_tunnel_usb3(sw->tb, sw); in tb_create_usb3_tunnels()
986 tb_switch_for_each_port(sw, port) { in tb_create_usb3_tunnels()
989 ret = tb_create_usb3_tunnels(port->remote->sw); in tb_create_usb3_tunnels()
998 * tb_configure_asym() - Transition links to asymmetric if needed
1017 struct tb_switch *sw; in tb_configure_asym() local
1027 sw = dst_port->sw; in tb_configure_asym()
1029 sw = src_port->sw; in tb_configure_asym()
1032 struct tb_port *down = tb_switch_downstream_port(up->sw); in tb_configure_asym()
1044 * (40G - guard band 10%), and the requested is above in tb_configure_asym()
1048 ret = -ENOBUFS; in tb_configure_asym()
1060 ret = -ENOBUFS; in tb_configure_asym()
1070 if (up->sw->link_width == width_up) in tb_configure_asym()
1083 clx = tb_disable_clx(sw); in tb_configure_asym()
1087 tb_sw_dbg(up->sw, "configuring asymmetric link\n"); in tb_configure_asym()
1093 ret = tb_switch_set_link_width(up->sw, width_up); in tb_configure_asym()
1095 tb_sw_warn(up->sw, "failed to set link width\n"); in tb_configure_asym()
1100 /* Re-enable CL states if they were previosly enabled */ in tb_configure_asym()
1102 tb_enable_clx(sw); in tb_configure_asym()
1108 * tb_configure_sym() - Transition links to symmetric if possible
1122 struct tb_switch *sw; in tb_configure_sym() local
1132 sw = dst_port->sw; in tb_configure_sym()
1134 sw = src_port->sw; in tb_configure_sym()
1140 if (up->sw->link_width <= TB_LINK_WIDTH_DUAL) in tb_configure_sym()
1143 if (up->sw->is_unplugged) in tb_configure_sym()
1165 if (up->sw->link_width == TB_LINK_WIDTH_DUAL) in tb_configure_sym()
1176 up->sw->preferred_link_width > TB_LINK_WIDTH_DUAL) { in tb_configure_sym()
1177 tb_sw_dbg(up->sw, "keeping preferred asymmetric link\n"); in tb_configure_sym()
1183 clx = tb_disable_clx(sw); in tb_configure_sym()
1187 tb_sw_dbg(up->sw, "configuring symmetric link\n"); in tb_configure_sym()
1189 ret = tb_switch_set_link_width(up->sw, TB_LINK_WIDTH_DUAL); in tb_configure_sym()
1191 tb_sw_warn(up->sw, "failed to set link width\n"); in tb_configure_sym()
1196 /* Re-enable CL states if they were previosly enabled */ in tb_configure_sym()
1198 tb_enable_clx(sw); in tb_configure_sym()
1204 struct tb_switch *sw) in tb_configure_link() argument
1206 struct tb *tb = sw->tb; in tb_configure_link()
1209 down->remote = up; in tb_configure_link()
1210 up->remote = down; in tb_configure_link()
1211 if (down->dual_link_port && up->dual_link_port) { in tb_configure_link()
1212 down->dual_link_port->remote = up->dual_link_port; in tb_configure_link()
1213 up->dual_link_port->remote = down->dual_link_port; in tb_configure_link()
1220 if (sw->link_width < TB_LINK_WIDTH_DUAL) in tb_configure_link()
1221 tb_switch_set_link_width(sw, TB_LINK_WIDTH_DUAL); in tb_configure_link()
1228 if (tb_switch_depth(sw) > 1 && in tb_configure_link()
1230 up->sw->link_width == TB_LINK_WIDTH_DUAL) { in tb_configure_link()
1233 host_port = tb_port_at(tb_route(sw), tb->root_switch); in tb_configure_link()
1238 tb_switch_configure_link(sw); in tb_configure_link()
1244 * tb_scan_switch() - scan for and initialize downstream switches
1246 static void tb_scan_switch(struct tb_switch *sw) in tb_scan_switch() argument
1250 pm_runtime_get_sync(&sw->dev); in tb_scan_switch()
1252 tb_switch_for_each_port(sw, port) in tb_scan_switch()
1255 pm_runtime_mark_last_busy(&sw->dev); in tb_scan_switch()
1256 pm_runtime_put_autosuspend(&sw->dev); in tb_scan_switch()
1260 * tb_scan_port() - check for and initialize switches below port
1264 struct tb_cm *tcm = tb_priv(port->sw->tb); in tb_scan_port()
1267 struct tb_switch *sw; in tb_scan_port() local
1275 tb_queue_hotplug(port->sw->tb, tb_route(port->sw), port->port, in tb_scan_port()
1280 if (port->config.type != TB_TYPE_PORT) in tb_scan_port()
1282 if (port->dual_link_port && port->link_nr) in tb_scan_port()
1288 if (port->usb4) in tb_scan_port()
1289 pm_runtime_get_sync(&port->usb4->dev); in tb_scan_port()
1293 if (port->remote) { in tb_scan_port()
1300 sw = tb_switch_alloc(port->sw->tb, &port->sw->dev, in tb_scan_port()
1302 if (IS_ERR(sw)) { in tb_scan_port()
1308 if (PTR_ERR(sw) == -EIO || PTR_ERR(sw) == -EADDRNOTAVAIL) in tb_scan_port()
1313 if (tb_switch_configure(sw)) { in tb_scan_port()
1314 tb_switch_put(sw); in tb_scan_port()
1322 if (port->xdomain) { in tb_scan_port()
1323 tb_xdomain_remove(port->xdomain); in tb_scan_port()
1325 port->xdomain = NULL; in tb_scan_port()
1333 if (!tcm->hotplug_active) { in tb_scan_port()
1334 dev_set_uevent_suppress(&sw->dev, true); in tb_scan_port()
1340 * can support runtime PM. in tb_scan_port()
1342 sw->rpm = sw->generation > 1; in tb_scan_port()
1344 if (tb_switch_add(sw)) { in tb_scan_port()
1345 tb_switch_put(sw); in tb_scan_port()
1349 upstream_port = tb_upstream_port(sw); in tb_scan_port()
1350 tb_configure_link(port, upstream_port, sw); in tb_scan_port()
1357 tb_sw_dbg(sw, "discovery, not touching CL states\n"); in tb_scan_port()
1358 else if (tb_enable_clx(sw)) in tb_scan_port()
1359 tb_sw_warn(sw, "failed to enable CL states\n"); in tb_scan_port()
1361 if (tb_enable_tmu(sw)) in tb_scan_port()
1362 tb_sw_warn(sw, "failed to enable TMU\n"); in tb_scan_port()
1368 tb_switch_configuration_valid(sw); in tb_scan_port()
1379 if (tcm->hotplug_active && tb_tunnel_usb3(sw->tb, sw)) in tb_scan_port()
1380 tb_sw_warn(sw, "USB3 tunnel creation failed\n"); in tb_scan_port()
1382 tb_add_dp_resources(sw); in tb_scan_port()
1383 tb_scan_switch(sw); in tb_scan_port()
1386 if (port->usb4) { in tb_scan_port()
1387 pm_runtime_mark_last_busy(&port->usb4->dev); in tb_scan_port()
1388 pm_runtime_put_autosuspend(&port->usb4->dev); in tb_scan_port()
1396 struct tb *tb = group->tb; in tb_recalc_estimated_bandwidth_for_group()
1400 tb_dbg(tb, "re-calculating bandwidth estimation for group %u\n", in tb_recalc_estimated_bandwidth_for_group()
1401 group->index); in tb_recalc_estimated_bandwidth_for_group()
1404 list_for_each_entry(in, &group->ports, group_list) { in tb_recalc_estimated_bandwidth_for_group()
1426 first_tunnel->src_port, first_tunnel->dst_port); in tb_recalc_estimated_bandwidth_for_group()
1434 out = tunnel->dst_port; in tb_recalc_estimated_bandwidth_for_group()
1439 "failed to re-calculate estimated bandwidth\n"); in tb_recalc_estimated_bandwidth_for_group()
1445 * - already allocated bandwidth for the DP tunnel in tb_recalc_estimated_bandwidth_for_group()
1446 * - available bandwidth along the path in tb_recalc_estimated_bandwidth_for_group()
1447 * - bandwidth allocated for USB 3.x but not used. in tb_recalc_estimated_bandwidth_for_group()
1459 "re-calculated estimated bandwidth %u (+ %u reserved) = %u Mb/s\n", in tb_recalc_estimated_bandwidth_for_group()
1460 estimated_bw, group->reserved, in tb_recalc_estimated_bandwidth_for_group()
1461 estimated_bw + group->reserved); in tb_recalc_estimated_bandwidth_for_group()
1464 estimated_bw + group->reserved)) in tb_recalc_estimated_bandwidth_for_group()
1470 tb_reclaim_usb3_bandwidth(tb, first_tunnel->src_port, in tb_recalc_estimated_bandwidth_for_group()
1471 first_tunnel->dst_port); in tb_recalc_estimated_bandwidth_for_group()
1473 tb_dbg(tb, "bandwidth estimation for group %u done\n", group->index); in tb_recalc_estimated_bandwidth_for_group()
1481 tb_dbg(tb, "bandwidth consumption changed, re-calculating estimated bandwidth\n"); in tb_recalc_estimated_bandwidth()
1483 for (i = 0; i < ARRAY_SIZE(tcm->groups); i++) { in tb_recalc_estimated_bandwidth()
1484 struct tb_bandwidth_group *group = &tcm->groups[i]; in tb_recalc_estimated_bandwidth()
1486 if (!list_empty(&group->ports)) in tb_recalc_estimated_bandwidth()
1490 tb_dbg(tb, "bandwidth re-calculation done\n"); in tb_recalc_estimated_bandwidth()
1495 if (group->reserved) { in __release_group_bandwidth()
1496 tb_dbg(group->tb, "group %d released total %d Mb/s\n", group->index, in __release_group_bandwidth()
1497 group->reserved); in __release_group_bandwidth()
1498 group->reserved = 0; in __release_group_bandwidth()
1509 if (list_empty(&group->ports)) in __configure_group_sym()
1522 in = list_first_entry(&group->ports, struct tb_port, group_list); in __configure_group_sym()
1523 tunnel = tb_find_tunnel(group->tb, TB_TUNNEL_DP, in, NULL); in __configure_group_sym()
1525 tb_configure_sym(group->tb, in, tunnel->dst_port, true); in __configure_group_sym()
1532 struct tb *tb = group->tb; in tb_bandwidth_group_release_work()
1534 mutex_lock(&tb->lock); in tb_bandwidth_group_release_work()
1538 mutex_unlock(&tb->lock); in tb_bandwidth_group_release_work()
1545 for (i = 0; i < ARRAY_SIZE(tcm->groups); i++) { in tb_init_bandwidth_groups()
1546 struct tb_bandwidth_group *group = &tcm->groups[i]; in tb_init_bandwidth_groups()
1548 group->tb = tcm_to_tb(tcm); in tb_init_bandwidth_groups()
1549 group->index = i + 1; in tb_init_bandwidth_groups()
1550 INIT_LIST_HEAD(&group->ports); in tb_init_bandwidth_groups()
1551 INIT_DELAYED_WORK(&group->release_work, in tb_init_bandwidth_groups()
1559 if (!group || WARN_ON(in->group)) in tb_bandwidth_group_attach_port()
1562 in->group = group; in tb_bandwidth_group_attach_port()
1563 list_add_tail(&in->group_list, &group->ports); in tb_bandwidth_group_attach_port()
1565 tb_port_dbg(in, "attached to bandwidth group %d\n", group->index); in tb_bandwidth_group_attach_port()
1572 for (i = 0; i < ARRAY_SIZE(tcm->groups); i++) { in tb_find_free_bandwidth_group()
1573 struct tb_bandwidth_group *group = &tcm->groups[i]; in tb_find_free_bandwidth_group()
1575 if (list_empty(&group->ports)) in tb_find_free_bandwidth_group()
1595 list_for_each_entry(tunnel, &tcm->tunnel_list, list) { in tb_attach_bandwidth_group()
1599 if (tunnel->src_port->sw == in->sw && in tb_attach_bandwidth_group()
1600 tunnel->dst_port->sw == out->sw) { in tb_attach_bandwidth_group()
1601 group = tunnel->src_port->group; in tb_attach_bandwidth_group()
1626 for (i = 0; i < ARRAY_SIZE(tcm->groups); i++) { in tb_discover_bandwidth_group()
1627 if (tcm->groups[i].index == index) { in tb_discover_bandwidth_group()
1628 tb_bandwidth_group_attach_port(&tcm->groups[i], in); in tb_discover_bandwidth_group()
1639 struct tb_bandwidth_group *group = in->group; in tb_detach_bandwidth_group()
1642 in->group = NULL; in tb_detach_bandwidth_group()
1643 list_del_init(&in->group_list); in tb_detach_bandwidth_group()
1645 tb_port_dbg(in, "detached from bandwidth group %d\n", group->index); in tb_detach_bandwidth_group()
1648 if (list_empty(&group->ports)) { in tb_detach_bandwidth_group()
1649 cancel_delayed_work(&group->release_work); in tb_detach_bandwidth_group()
1660 tb_switch_discover_tunnels(tb->root_switch, &tcm->tunnel_list, true); in tb_discover_tunnels()
1662 list_for_each_entry(tunnel, &tcm->tunnel_list, list) { in tb_discover_tunnels()
1664 struct tb_switch *parent = tunnel->dst_port->sw; in tb_discover_tunnels()
1666 while (parent != tunnel->src_port->sw) { in tb_discover_tunnels()
1667 parent->boot = true; in tb_discover_tunnels()
1671 struct tb_port *in = tunnel->src_port; in tb_discover_tunnels()
1672 struct tb_port *out = tunnel->dst_port; in tb_discover_tunnels()
1675 pm_runtime_get_sync(&in->sw->dev); in tb_discover_tunnels()
1676 pm_runtime_get_sync(&out->sw->dev); in tb_discover_tunnels()
1692 list_del(&tunnel->list); in tb_deactivate_and_free_tunnel()
1694 tb = tunnel->tb; in tb_deactivate_and_free_tunnel()
1695 src_port = tunnel->src_port; in tb_deactivate_and_free_tunnel()
1696 dst_port = tunnel->dst_port; in tb_deactivate_and_free_tunnel()
1698 switch (tunnel->type) { in tb_deactivate_and_free_tunnel()
1705 tb_switch_dealloc_dp_resource(src_port->sw, src_port); in tb_deactivate_and_free_tunnel()
1712 pm_runtime_mark_last_busy(&dst_port->sw->dev); in tb_deactivate_and_free_tunnel()
1713 pm_runtime_put_autosuspend(&dst_port->sw->dev); in tb_deactivate_and_free_tunnel()
1714 pm_runtime_mark_last_busy(&src_port->sw->dev); in tb_deactivate_and_free_tunnel()
1715 pm_runtime_put_autosuspend(&src_port->sw->dev); in tb_deactivate_and_free_tunnel()
1734 * tb_free_invalid_tunnels() - destroy tunnels of devices that have gone away
1742 list_for_each_entry_safe(tunnel, n, &tcm->tunnel_list, list) { in tb_free_invalid_tunnels()
1749 * tb_free_unplugged_children() - traverse hierarchy and free unplugged switches
1751 static void tb_free_unplugged_children(struct tb_switch *sw) in tb_free_unplugged_children() argument
1755 tb_switch_for_each_port(sw, port) { in tb_free_unplugged_children()
1759 if (port->remote->sw->is_unplugged) { in tb_free_unplugged_children()
1761 tb_remove_dp_resources(port->remote->sw); in tb_free_unplugged_children()
1762 tb_switch_unconfigure_link(port->remote->sw); in tb_free_unplugged_children()
1763 tb_switch_set_link_width(port->remote->sw, in tb_free_unplugged_children()
1765 tb_switch_remove(port->remote->sw); in tb_free_unplugged_children()
1766 port->remote = NULL; in tb_free_unplugged_children()
1767 if (port->dual_link_port) in tb_free_unplugged_children()
1768 port->dual_link_port->remote = NULL; in tb_free_unplugged_children()
1770 tb_free_unplugged_children(port->remote->sw); in tb_free_unplugged_children()
1775 static struct tb_port *tb_find_pcie_down(struct tb_switch *sw, in tb_find_pcie_down() argument
1784 if (tb_switch_is_usb4(sw)) { in tb_find_pcie_down()
1785 down = usb4_switch_map_pcie_down(sw, port); in tb_find_pcie_down()
1786 } else if (!tb_route(sw)) { in tb_find_pcie_down()
1787 int phy_port = tb_phy_port_from_link(port->port); in tb_find_pcie_down()
1791 * Hard-coded Thunderbolt port to PCIe down port mapping in tb_find_pcie_down()
1794 if (tb_switch_is_cactus_ridge(sw) || in tb_find_pcie_down()
1795 tb_switch_is_alpine_ridge(sw)) in tb_find_pcie_down()
1797 else if (tb_switch_is_falcon_ridge(sw)) in tb_find_pcie_down()
1799 else if (tb_switch_is_titan_ridge(sw)) in tb_find_pcie_down()
1804 /* Validate the hard-coding */ in tb_find_pcie_down()
1805 if (WARN_ON(index > sw->config.max_port_number)) in tb_find_pcie_down()
1808 down = &sw->ports[index]; in tb_find_pcie_down()
1821 return tb_find_unused_port(sw, TB_TYPE_PCIE_DOWN); in tb_find_pcie_down()
1829 host_port = tb_route(in->sw) ? in tb_find_dp_out()
1830 tb_port_at(tb_route(in->sw), tb->root_switch) : NULL; in tb_find_dp_out()
1832 list_for_each_entry(port, &tcm->dp_resources, list) { in tb_find_dp_out()
1837 tb_port_dbg(port, "DP OUT in use\n"); in tb_find_dp_out()
1842 if (in->sw == port->sw) { in tb_find_dp_out()
1853 if (host_port && tb_route(port->sw)) { in tb_find_dp_out()
1856 p = tb_port_at(tb_route(port->sw), tb->root_switch); in tb_find_dp_out()
1882 list_for_each_entry(tunnel, &tcm->tunnel_list, list) { in tb_tunnel_one_dp()
1897 pm_runtime_get_sync(&in->sw->dev); in tb_tunnel_one_dp()
1898 pm_runtime_get_sync(&out->sw->dev); in tb_tunnel_one_dp()
1900 if (tb_switch_alloc_dp_resource(in->sw, in)) { in tb_tunnel_one_dp()
1940 list_add_tail(&tunnel->list, &tcm->tunnel_list); in tb_tunnel_one_dp()
1968 tb_switch_dealloc_dp_resource(in->sw, in); in tb_tunnel_one_dp()
1970 pm_runtime_mark_last_busy(&out->sw->dev); in tb_tunnel_one_dp()
1971 pm_runtime_put_autosuspend(&out->sw->dev); in tb_tunnel_one_dp()
1972 pm_runtime_mark_last_busy(&in->sw->dev); in tb_tunnel_one_dp()
1973 pm_runtime_put_autosuspend(&in->sw->dev); in tb_tunnel_one_dp()
1992 tb_dbg(tb, "looking for DP IN <-> DP OUT pairs:\n"); in tb_tunnel_dp()
1996 list_for_each_entry(port, &tcm->dp_resources, list) { in tb_tunnel_dp()
2001 tb_port_dbg(port, "DP IN in use\n"); in tb_tunnel_dp()
2021 struct tb_switch *sw = port->sw; in tb_enter_redrive() local
2023 if (!(sw->quirks & QUIRK_KEEP_POWER_IN_DP_REDRIVE)) in tb_enter_redrive()
2027 * If we get hot-unplug for the DP IN port of the host router in tb_enter_redrive()
2029 * is a monitor connected directly to the Type-C port and we are in tb_enter_redrive()
2031 * we bump up the runtime PM reference count here. in tb_enter_redrive()
2035 if (tb_route(sw)) in tb_enter_redrive()
2037 if (!tb_switch_query_dp_resource(sw, port)) { in tb_enter_redrive()
2038 port->redrive = true; in tb_enter_redrive()
2039 pm_runtime_get(&sw->dev); in tb_enter_redrive()
2046 struct tb_switch *sw = port->sw; in tb_exit_redrive() local
2048 if (!(sw->quirks & QUIRK_KEEP_POWER_IN_DP_REDRIVE)) in tb_exit_redrive()
2053 if (tb_route(sw)) in tb_exit_redrive()
2055 if (port->redrive && tb_switch_query_dp_resource(sw, port)) { in tb_exit_redrive()
2056 port->redrive = false; in tb_exit_redrive()
2057 pm_runtime_put(&sw->dev); in tb_exit_redrive()
2062 static void tb_switch_enter_redrive(struct tb_switch *sw) in tb_switch_enter_redrive() argument
2066 tb_switch_for_each_port(sw, port) in tb_switch_enter_redrive()
2074 static void tb_switch_exit_redrive(struct tb_switch *sw) in tb_switch_exit_redrive() argument
2078 if (!(sw->quirks & QUIRK_KEEP_POWER_IN_DP_REDRIVE)) in tb_switch_exit_redrive()
2081 tb_switch_for_each_port(sw, port) { in tb_switch_exit_redrive()
2085 if (port->redrive) { in tb_switch_exit_redrive()
2086 port->redrive = false; in tb_switch_exit_redrive()
2087 pm_runtime_put(&sw->dev); in tb_switch_exit_redrive()
2113 list_del_init(&port->list); in tb_dp_resource_unavailable()
2131 list_for_each_entry(p, &tcm->dp_resources, list) { in tb_dp_resource_available()
2138 list_add_tail(&port->list, &tcm->dp_resources); in tb_dp_resource_available()
2141 /* Look for suitable DP IN <-> DP OUT pairs now */ in tb_dp_resource_available()
2152 * will be re-established after resume based on plug events. in tb_disconnect_and_release_dp()
2154 list_for_each_entry_safe_reverse(tunnel, n, &tcm->tunnel_list, list) { in tb_disconnect_and_release_dp()
2159 while (!list_empty(&tcm->dp_resources)) { in tb_disconnect_and_release_dp()
2162 port = list_first_entry(&tcm->dp_resources, in tb_disconnect_and_release_dp()
2164 list_del_init(&port->list); in tb_disconnect_and_release_dp()
2168 static int tb_disconnect_pci(struct tb *tb, struct tb_switch *sw) in tb_disconnect_pci() argument
2173 up = tb_switch_find_port(sw, TB_TYPE_PCIE_UP); in tb_disconnect_pci()
2175 return -ENODEV; in tb_disconnect_pci()
2179 return -ENODEV; in tb_disconnect_pci()
2181 tb_switch_xhci_disconnect(sw); in tb_disconnect_pci()
2184 list_del(&tunnel->list); in tb_disconnect_pci()
2189 static int tb_tunnel_pci(struct tb *tb, struct tb_switch *sw) in tb_tunnel_pci() argument
2195 up = tb_switch_find_port(sw, TB_TYPE_PCIE_UP); in tb_tunnel_pci()
2203 port = tb_switch_downstream_port(sw); in tb_tunnel_pci()
2204 down = tb_find_pcie_down(tb_switch_parent(sw), port); in tb_tunnel_pci()
2210 return -ENOMEM; in tb_tunnel_pci()
2216 return -EIO; in tb_tunnel_pci()
2223 if (tb_switch_pcie_l1_enable(sw)) in tb_tunnel_pci()
2224 tb_sw_warn(sw, "failed to enable PCIe L1 for Titan Ridge\n"); in tb_tunnel_pci()
2226 if (tb_switch_xhci_connect(sw)) in tb_tunnel_pci()
2227 tb_sw_warn(sw, "failed to connect xHCI\n"); in tb_tunnel_pci()
2229 list_add_tail(&tunnel->list, &tcm->tunnel_list); in tb_tunnel_pci()
2240 struct tb_switch *sw; in tb_approve_xdomain_paths() local
2243 sw = tb_to_switch(xd->dev.parent); in tb_approve_xdomain_paths()
2244 dst_port = tb_port_at(xd->route, sw); in tb_approve_xdomain_paths()
2245 nhi_port = tb_switch_find_port(tb->root_switch, TB_TYPE_NHI); in tb_approve_xdomain_paths()
2247 mutex_lock(&tb->lock); in tb_approve_xdomain_paths()
2253 tb_disable_clx(sw); in tb_approve_xdomain_paths()
2258 ret = -ENOMEM; in tb_approve_xdomain_paths()
2265 ret = -EIO; in tb_approve_xdomain_paths()
2269 list_add_tail(&tunnel->list, &tcm->tunnel_list); in tb_approve_xdomain_paths()
2270 mutex_unlock(&tb->lock); in tb_approve_xdomain_paths()
2276 tb_enable_clx(sw); in tb_approve_xdomain_paths()
2277 mutex_unlock(&tb->lock); in tb_approve_xdomain_paths()
2289 struct tb_switch *sw; in __tb_disconnect_xdomain_paths() local
2291 sw = tb_to_switch(xd->dev.parent); in __tb_disconnect_xdomain_paths()
2292 dst_port = tb_port_at(xd->route, sw); in __tb_disconnect_xdomain_paths()
2293 nhi_port = tb_switch_find_port(tb->root_switch, TB_TYPE_NHI); in __tb_disconnect_xdomain_paths()
2295 list_for_each_entry_safe(tunnel, n, &tcm->tunnel_list, list) { in __tb_disconnect_xdomain_paths()
2298 if (tunnel->src_port != nhi_port || tunnel->dst_port != dst_port) in __tb_disconnect_xdomain_paths()
2307 * Try to re-enable CL states now, it is OK if this fails in __tb_disconnect_xdomain_paths()
2311 tb_enable_clx(sw); in __tb_disconnect_xdomain_paths()
2318 if (!xd->is_unplugged) { in tb_disconnect_xdomain_paths()
2319 mutex_lock(&tb->lock); in tb_disconnect_xdomain_paths()
2323 mutex_unlock(&tb->lock); in tb_disconnect_xdomain_paths()
2331 * tb_handle_hotplug() - handle hotplug event
2333 * Executes on tb->wq.
2338 struct tb *tb = ev->tb; in tb_handle_hotplug()
2340 struct tb_switch *sw; in tb_handle_hotplug() local
2344 pm_runtime_get_sync(&tb->dev); in tb_handle_hotplug()
2346 mutex_lock(&tb->lock); in tb_handle_hotplug()
2347 if (!tcm->hotplug_active) in tb_handle_hotplug()
2350 sw = tb_switch_find_by_route(tb, ev->route); in tb_handle_hotplug()
2351 if (!sw) { in tb_handle_hotplug()
2354 ev->route, ev->port, ev->unplug); in tb_handle_hotplug()
2357 if (ev->port > sw->config.max_port_number) { in tb_handle_hotplug()
2360 ev->route, ev->port, ev->unplug); in tb_handle_hotplug()
2363 port = &sw->ports[ev->port]; in tb_handle_hotplug()
2366 ev->route, ev->port, ev->unplug); in tb_handle_hotplug()
2370 pm_runtime_get_sync(&sw->dev); in tb_handle_hotplug()
2372 if (ev->unplug) { in tb_handle_hotplug()
2377 tb_sw_set_unplugged(port->remote->sw); in tb_handle_hotplug()
2379 tb_remove_dp_resources(port->remote->sw); in tb_handle_hotplug()
2380 tb_switch_tmu_disable(port->remote->sw); in tb_handle_hotplug()
2381 tb_switch_unconfigure_link(port->remote->sw); in tb_handle_hotplug()
2382 tb_switch_set_link_width(port->remote->sw, in tb_handle_hotplug()
2384 tb_switch_remove(port->remote->sw); in tb_handle_hotplug()
2385 port->remote = NULL; in tb_handle_hotplug()
2386 if (port->dual_link_port) in tb_handle_hotplug()
2387 port->dual_link_port->remote = NULL; in tb_handle_hotplug()
2391 } else if (port->xdomain) { in tb_handle_hotplug()
2392 struct tb_xdomain *xd = tb_xdomain_get(port->xdomain); in tb_handle_hotplug()
2402 xd->is_unplugged = true; in tb_handle_hotplug()
2404 port->xdomain = NULL; in tb_handle_hotplug()
2405 __tb_disconnect_xdomain_paths(tb, xd, -1, -1, -1, -1); in tb_handle_hotplug()
2410 } else if (!port->port) { in tb_handle_hotplug()
2411 tb_sw_dbg(sw, "xHCI disconnect request\n"); in tb_handle_hotplug()
2412 tb_switch_xhci_disconnect(sw); in tb_handle_hotplug()
2417 } else if (port->remote) { in tb_handle_hotplug()
2419 } else if (!port->port && sw->authorized) { in tb_handle_hotplug()
2420 tb_sw_dbg(sw, "xHCI connect request\n"); in tb_handle_hotplug()
2421 tb_switch_xhci_connect(sw); in tb_handle_hotplug()
2426 if (!port->remote) in tb_handle_hotplug()
2433 pm_runtime_mark_last_busy(&sw->dev); in tb_handle_hotplug()
2434 pm_runtime_put_autosuspend(&sw->dev); in tb_handle_hotplug()
2437 tb_switch_put(sw); in tb_handle_hotplug()
2439 mutex_unlock(&tb->lock); in tb_handle_hotplug()
2441 pm_runtime_mark_last_busy(&tb->dev); in tb_handle_hotplug()
2442 pm_runtime_put_autosuspend(&tb->dev); in tb_handle_hotplug()
2454 struct tb *tb = tunnel->tb; in tb_alloc_dp_bandwidth()
2462 in = tunnel->src_port; in tb_alloc_dp_bandwidth()
2463 out = tunnel->dst_port; in tb_alloc_dp_bandwidth()
2475 * Since the link cannot go higher than 17280 we use that in our in tb_alloc_dp_bandwidth()
2516 ret = -ENOBUFS; in tb_alloc_dp_bandwidth()
2521 group = in->group; in tb_alloc_dp_bandwidth()
2525 if (tunnel->bw_mode) { in tb_alloc_dp_bandwidth()
2533 * others to use. in tb_alloc_dp_bandwidth()
2536 reserved = allocated_down - *requested_down; in tb_alloc_dp_bandwidth()
2538 reserved = allocated_up - *requested_up; in tb_alloc_dp_bandwidth()
2541 group->reserved += reserved; in tb_alloc_dp_bandwidth()
2543 group->index, reserved, group->reserved); in tb_alloc_dp_bandwidth()
2553 mod_delayed_work(system_wq, &group->release_work, in tb_alloc_dp_bandwidth()
2572 * are also in the same group but we use the same function here in tb_alloc_dp_bandwidth()
2573 * that we use with the normal bandwidth allocation). in tb_alloc_dp_bandwidth()
2581 available_up, available_down, group->reserved); in tb_alloc_dp_bandwidth()
2584 available_up + group->reserved >= requested_up_corrected) || in tb_alloc_dp_bandwidth()
2586 available_down + group->reserved >= requested_down_corrected)) { in tb_alloc_dp_bandwidth()
2609 released = *requested_down - available_down; in tb_alloc_dp_bandwidth()
2612 released = *requested_up - available_up; in tb_alloc_dp_bandwidth()
2615 group->reserved -= released; in tb_alloc_dp_bandwidth()
2617 group->index, released, group->reserved); in tb_alloc_dp_bandwidth()
2620 ret = -ENOBUFS; in tb_alloc_dp_bandwidth()
2626 if (ret && ret != -ENODEV) { in tb_alloc_dp_bandwidth()
2645 struct tb *tb = ev->tb; in tb_handle_dp_bandwidth_request()
2647 struct tb_switch *sw; in tb_handle_dp_bandwidth_request() local
2650 pm_runtime_get_sync(&tb->dev); in tb_handle_dp_bandwidth_request()
2652 mutex_lock(&tb->lock); in tb_handle_dp_bandwidth_request()
2653 if (!tcm->hotplug_active) in tb_handle_dp_bandwidth_request()
2656 sw = tb_switch_find_by_route(tb, ev->route); in tb_handle_dp_bandwidth_request()
2657 if (!sw) { in tb_handle_dp_bandwidth_request()
2658 tb_warn(tb, "bandwidth request from non-existent router %llx\n", in tb_handle_dp_bandwidth_request()
2659 ev->route); in tb_handle_dp_bandwidth_request()
2663 in = &sw->ports[ev->port]; in tb_handle_dp_bandwidth_request()
2665 tb_port_warn(in, "bandwidth request to non-DP IN adapter\n"); in tb_handle_dp_bandwidth_request()
2678 if (tunnel->bw_mode) { in tb_handle_dp_bandwidth_request()
2680 * Reset the tunnel back to use the legacy in tb_handle_dp_bandwidth_request()
2683 tunnel->bw_mode = false; in tb_handle_dp_bandwidth_request()
2693 if (ret == -ENODATA) { in tb_handle_dp_bandwidth_request()
2713 requested_up = -1; in tb_handle_dp_bandwidth_request()
2717 requested_down = -1; in tb_handle_dp_bandwidth_request()
2722 if (ret == -ENOBUFS) in tb_handle_dp_bandwidth_request()
2738 tb_switch_put(sw); in tb_handle_dp_bandwidth_request()
2740 mutex_unlock(&tb->lock); in tb_handle_dp_bandwidth_request()
2742 pm_runtime_mark_last_busy(&tb->dev); in tb_handle_dp_bandwidth_request()
2743 pm_runtime_put_autosuspend(&tb->dev); in tb_handle_dp_bandwidth_request()
2756 ev->tb = tb; in tb_queue_dp_bandwidth_request()
2757 ev->route = route; in tb_queue_dp_bandwidth_request()
2758 ev->port = port; in tb_queue_dp_bandwidth_request()
2759 INIT_WORK(&ev->work, tb_handle_dp_bandwidth_request); in tb_queue_dp_bandwidth_request()
2760 queue_work(tb->wq, &ev->work); in tb_queue_dp_bandwidth_request()
2767 switch (error->error) { in tb_handle_notification()
2771 if (tb_cfg_ack_notification(tb->ctl, route, error)) in tb_handle_notification()
2777 if (tb_cfg_ack_notification(tb->ctl, route, error)) in tb_handle_notification()
2780 tb_queue_dp_bandwidth_request(tb, route, error->port); in tb_handle_notification()
2790 * tb_schedule_hotplug_handler() - callback function for the control channel
2798 u64 route = tb_cfg_get_route(&pkg->header); in tb_handle_event()
2811 if (tb_cfg_ack_plug(tb->ctl, route, pkg->port, pkg->unplug)) { in tb_handle_event()
2813 pkg->port); in tb_handle_event()
2816 tb_queue_hotplug(tb, route, pkg->port, pkg->unplug); in tb_handle_event()
2825 cancel_delayed_work(&tcm->remove_work); in tb_stop()
2827 list_for_each_entry_safe(tunnel, n, &tcm->tunnel_list, list) { in tb_stop()
2837 tb_switch_remove(tb->root_switch); in tb_stop()
2838 tcm->hotplug_active = false; /* signal tb_handle_hotplug to quit */ in tb_stop()
2847 for (i = 0; i < ARRAY_SIZE(tcm->groups); i++) in tb_deinit()
2848 cancel_delayed_work_sync(&tcm->groups[i].release_work); in tb_deinit()
2854 struct tb_switch *sw = tb_to_switch(dev); in tb_scan_finalize_switch() local
2861 if (sw->boot) in tb_scan_finalize_switch()
2862 sw->authorized = 1; in tb_scan_finalize_switch()
2865 kobject_uevent(&dev->kobj, KOBJ_ADD); in tb_scan_finalize_switch()
2878 tb->root_switch = tb_switch_alloc(tb, &tb->dev, 0); in tb_start()
2879 if (IS_ERR(tb->root_switch)) in tb_start()
2880 return PTR_ERR(tb->root_switch); in tb_start()
2890 tb->root_switch->no_nvm_upgrade = !tb_switch_is_usb4(tb->root_switch); in tb_start()
2891 /* All USB4 routers support runtime PM */ in tb_start()
2892 tb->root_switch->rpm = tb_switch_is_usb4(tb->root_switch); in tb_start()
2894 ret = tb_switch_configure(tb->root_switch); in tb_start()
2896 tb_switch_put(tb->root_switch); in tb_start()
2901 ret = tb_switch_add(tb->root_switch); in tb_start()
2903 tb_switch_put(tb->root_switch); in tb_start()
2911 tb_switch_tmu_configure(tb->root_switch, TB_SWITCH_TMU_MODE_LOWRES); in tb_start()
2913 tb_switch_tmu_enable(tb->root_switch); in tb_start()
2921 if (reset && tb_switch_is_usb4(tb->root_switch)) { in tb_start()
2923 if (usb4_switch_version(tb->root_switch) == 1) in tb_start()
2924 tb_switch_reset(tb->root_switch); in tb_start()
2929 tb_scan_switch(tb->root_switch); in tb_start()
2940 tb_create_usb3_tunnels(tb->root_switch); in tb_start()
2942 tb_add_dp_resources(tb->root_switch); in tb_start()
2943 tb_switch_enter_redrive(tb->root_switch); in tb_start()
2945 device_for_each_child(&tb->root_switch->dev, NULL, in tb_start()
2949 tcm->hotplug_active = true; in tb_start()
2959 tb_switch_exit_redrive(tb->root_switch); in tb_suspend_noirq()
2960 tb_switch_suspend(tb->root_switch, false); in tb_suspend_noirq()
2961 tcm->hotplug_active = false; /* signal tb_handle_hotplug to quit */ in tb_suspend_noirq()
2967 static void tb_restore_children(struct tb_switch *sw) in tb_restore_children() argument
2972 if (sw->is_unplugged) in tb_restore_children()
2975 if (tb_enable_clx(sw)) in tb_restore_children()
2976 tb_sw_warn(sw, "failed to re-enable CL states\n"); in tb_restore_children()
2978 if (tb_enable_tmu(sw)) in tb_restore_children()
2979 tb_sw_warn(sw, "failed to restore TMU configuration\n"); in tb_restore_children()
2981 tb_switch_configuration_valid(sw); in tb_restore_children()
2983 tb_switch_for_each_port(sw, port) { in tb_restore_children()
2984 if (!tb_port_has_remote(port) && !port->xdomain) in tb_restore_children()
2987 if (port->remote) { in tb_restore_children()
2988 tb_switch_set_link_width(port->remote->sw, in tb_restore_children()
2989 port->remote->sw->link_width); in tb_restore_children()
2990 tb_switch_configure_link(port->remote->sw); in tb_restore_children()
2992 tb_restore_children(port->remote->sw); in tb_restore_children()
2993 } else if (port->xdomain) { in tb_restore_children()
2994 tb_port_configure_xdomain(port, port->xdomain); in tb_restore_children()
3009 * For non-USB4 hosts (Apple systems) remove any PCIe devices in tb_resume_noirq()
3012 if (!tb_switch_is_usb4(tb->root_switch)) in tb_resume_noirq()
3013 tb_switch_reset(tb->root_switch); in tb_resume_noirq()
3015 tb_switch_resume(tb->root_switch, false); in tb_resume_noirq()
3017 tb_free_unplugged_children(tb->root_switch); in tb_resume_noirq()
3018 tb_restore_children(tb->root_switch); in tb_resume_noirq()
3026 tb_switch_discover_tunnels(tb->root_switch, &tunnels, false); in tb_resume_noirq()
3034 /* Re-create our tunnels now */ in tb_resume_noirq()
3035 list_for_each_entry_safe(tunnel, n, &tcm->tunnel_list, list) { in tb_resume_noirq()
3036 /* USB3 requires delay before it can be re-activated */ in tb_resume_noirq()
3044 if (!list_empty(&tcm->tunnel_list)) { in tb_resume_noirq()
3052 tb_switch_enter_redrive(tb->root_switch); in tb_resume_noirq()
3054 tcm->hotplug_active = true; in tb_resume_noirq()
3060 static int tb_free_unplugged_xdomains(struct tb_switch *sw) in tb_free_unplugged_xdomains() argument
3065 tb_switch_for_each_port(sw, port) { in tb_free_unplugged_xdomains()
3068 if (port->xdomain && port->xdomain->is_unplugged) { in tb_free_unplugged_xdomains()
3070 tb_xdomain_remove(port->xdomain); in tb_free_unplugged_xdomains()
3072 port->xdomain = NULL; in tb_free_unplugged_xdomains()
3074 } else if (port->remote) { in tb_free_unplugged_xdomains()
3075 ret += tb_free_unplugged_xdomains(port->remote->sw); in tb_free_unplugged_xdomains()
3086 tcm->hotplug_active = false; in tb_freeze_noirq()
3094 tcm->hotplug_active = true; in tb_thaw_noirq()
3105 mutex_lock(&tb->lock); in tb_complete()
3106 if (tb_free_unplugged_xdomains(tb->root_switch)) in tb_complete()
3107 tb_scan_switch(tb->root_switch); in tb_complete()
3108 mutex_unlock(&tb->lock); in tb_complete()
3115 mutex_lock(&tb->lock); in tb_runtime_suspend()
3118 * re-entering redrive mode. in tb_runtime_suspend()
3121 tb_switch_exit_redrive(tb->root_switch); in tb_runtime_suspend()
3122 tb_switch_suspend(tb->root_switch, true); in tb_runtime_suspend()
3123 tcm->hotplug_active = false; in tb_runtime_suspend()
3124 mutex_unlock(&tb->lock); in tb_runtime_suspend()
3134 mutex_lock(&tb->lock); in tb_remove_work()
3135 if (tb->root_switch) { in tb_remove_work()
3136 tb_free_unplugged_children(tb->root_switch); in tb_remove_work()
3137 tb_free_unplugged_xdomains(tb->root_switch); in tb_remove_work()
3139 mutex_unlock(&tb->lock); in tb_remove_work()
3147 mutex_lock(&tb->lock); in tb_runtime_resume()
3148 tb_switch_resume(tb->root_switch, true); in tb_runtime_resume()
3150 tb_restore_children(tb->root_switch); in tb_runtime_resume()
3151 list_for_each_entry_safe(tunnel, n, &tcm->tunnel_list, list) in tb_runtime_resume()
3153 tb_switch_enter_redrive(tb->root_switch); in tb_runtime_resume()
3154 tcm->hotplug_active = true; in tb_runtime_resume()
3155 mutex_unlock(&tb->lock); in tb_runtime_resume()
3162 queue_delayed_work(tb->wq, &tcm->remove_work, msecs_to_jiffies(50)); in tb_runtime_resume()
3199 switch (nhi->pdev->device) { in tb_apple_add_links()
3209 upstream = pci_upstream_bridge(nhi->pdev); in tb_apple_add_links()
3223 * back to NHI so that PCIe tunnels can be re-established after in tb_apple_add_links()
3227 for_each_pci_bridge(pdev, upstream->subordinate) { in tb_apple_add_links()
3233 !pdev->is_hotplug_bridge) in tb_apple_add_links()
3236 link = device_link_add(&pdev->dev, &nhi->pdev->dev, in tb_apple_add_links()
3240 dev_dbg(&nhi->pdev->dev, "created link from %s\n", in tb_apple_add_links()
3241 dev_name(&pdev->dev)); in tb_apple_add_links()
3244 dev_warn(&nhi->pdev->dev, "device link creation from %s failed\n", in tb_apple_add_links()
3245 dev_name(&pdev->dev)); in tb_apple_add_links()
3262 tb->security_level = TB_SECURITY_USER; in tb_probe()
3264 tb->security_level = TB_SECURITY_NOPCIE; in tb_probe()
3266 tb->cm_ops = &tb_cm_ops; in tb_probe()
3269 INIT_LIST_HEAD(&tcm->tunnel_list); in tb_probe()
3270 INIT_LIST_HEAD(&tcm->dp_resources); in tb_probe()
3271 INIT_DELAYED_WORK(&tcm->remove_work, tb_remove_work); in tb_probe()