Lines Matching +full:use +full:- +full:sw +full:- +full:pm
1 // SPDX-License-Identifier: GPL-2.0
3 * Thunderbolt driver - bus logic (NHI independent)
23 * struct tb_cm - Simple Thunderbolt connection manager
44 return ((void *)tcm - sizeof(struct tb)); in tcm_to_tb()
59 for (i = 0; i < ARRAY_SIZE(tcm->groups); i++) { in tb_init_bandwidth_groups()
60 struct tb_bandwidth_group *group = &tcm->groups[i]; in tb_init_bandwidth_groups()
62 group->tb = tcm_to_tb(tcm); in tb_init_bandwidth_groups()
63 group->index = i + 1; in tb_init_bandwidth_groups()
64 INIT_LIST_HEAD(&group->ports); in tb_init_bandwidth_groups()
71 if (!group || WARN_ON(in->group)) in tb_bandwidth_group_attach_port()
74 in->group = group; in tb_bandwidth_group_attach_port()
75 list_add_tail(&in->group_list, &group->ports); in tb_bandwidth_group_attach_port()
77 tb_port_dbg(in, "attached to bandwidth group %d\n", group->index); in tb_bandwidth_group_attach_port()
84 for (i = 0; i < ARRAY_SIZE(tcm->groups); i++) { in tb_find_free_bandwidth_group()
85 struct tb_bandwidth_group *group = &tcm->groups[i]; in tb_find_free_bandwidth_group()
87 if (list_empty(&group->ports)) in tb_find_free_bandwidth_group()
107 list_for_each_entry(tunnel, &tcm->tunnel_list, list) { in tb_attach_bandwidth_group()
111 if (tunnel->src_port->sw == in->sw && in tb_attach_bandwidth_group()
112 tunnel->dst_port->sw == out->sw) { in tb_attach_bandwidth_group()
113 group = tunnel->src_port->group; in tb_attach_bandwidth_group()
138 for (i = 0; i < ARRAY_SIZE(tcm->groups); i++) { in tb_discover_bandwidth_group()
139 if (tcm->groups[i].index == index) { in tb_discover_bandwidth_group()
140 tb_bandwidth_group_attach_port(&tcm->groups[i], in); in tb_discover_bandwidth_group()
151 struct tb_bandwidth_group *group = in->group; in tb_detach_bandwidth_group()
154 in->group = NULL; in tb_detach_bandwidth_group()
155 list_del_init(&in->group_list); in tb_detach_bandwidth_group()
157 tb_port_dbg(in, "detached from bandwidth group %d\n", group->index); in tb_detach_bandwidth_group()
171 ev->tb = tb; in tb_queue_hotplug()
172 ev->route = route; in tb_queue_hotplug()
173 ev->port = port; in tb_queue_hotplug()
174 ev->unplug = unplug; in tb_queue_hotplug()
175 INIT_WORK(&ev->work, tb_handle_hotplug); in tb_queue_hotplug()
176 queue_work(tb->wq, &ev->work); in tb_queue_hotplug()
181 static void tb_add_dp_resources(struct tb_switch *sw) in tb_add_dp_resources() argument
183 struct tb_cm *tcm = tb_priv(sw->tb); in tb_add_dp_resources()
186 tb_switch_for_each_port(sw, port) { in tb_add_dp_resources()
190 if (!tb_switch_query_dp_resource(sw, port)) in tb_add_dp_resources()
193 list_add_tail(&port->list, &tcm->dp_resources); in tb_add_dp_resources()
198 static void tb_remove_dp_resources(struct tb_switch *sw) in tb_remove_dp_resources() argument
200 struct tb_cm *tcm = tb_priv(sw->tb); in tb_remove_dp_resources()
204 tb_switch_for_each_port(sw, port) { in tb_remove_dp_resources()
206 tb_remove_dp_resources(port->remote->sw); in tb_remove_dp_resources()
209 list_for_each_entry_safe(port, tmp, &tcm->dp_resources, list) { in tb_remove_dp_resources()
210 if (port->sw == sw) { in tb_remove_dp_resources()
212 list_del_init(&port->list); in tb_remove_dp_resources()
222 list_for_each_entry(p, &tcm->dp_resources, list) { in tb_discover_dp_resource()
229 list_add_tail(&port->list, &tcm->dp_resources); in tb_discover_dp_resource()
237 list_for_each_entry(tunnel, &tcm->tunnel_list, list) { in tb_discover_dp_resources()
239 tb_discover_dp_resource(tb, tunnel->dst_port); in tb_discover_dp_resources()
244 static int tb_enable_clx(struct tb_switch *sw) in tb_enable_clx() argument
246 struct tb_cm *tcm = tb_priv(sw->tb); in tb_enable_clx()
258 while (sw && sw->config.depth > 1) in tb_enable_clx()
259 sw = tb_switch_parent(sw); in tb_enable_clx()
261 if (!sw) in tb_enable_clx()
264 if (sw->config.depth != 1) in tb_enable_clx()
268 * If we are re-enabling then check if there is an active DMA in tb_enable_clx()
271 list_for_each_entry(tunnel, &tcm->tunnel_list, list) { in tb_enable_clx()
273 if (tb_tunnel_port_on_path(tunnel, tb_upstream_port(sw))) in tb_enable_clx()
282 ret = tb_switch_clx_enable(sw, clx | TB_CL2); in tb_enable_clx()
283 if (ret == -EOPNOTSUPP) in tb_enable_clx()
284 ret = tb_switch_clx_enable(sw, clx); in tb_enable_clx()
285 return ret == -EOPNOTSUPP ? 0 : ret; in tb_enable_clx()
289 static void tb_disable_clx(struct tb_switch *sw) in tb_disable_clx() argument
292 if (tb_switch_clx_disable(sw) < 0) in tb_disable_clx()
293 tb_sw_warn(sw, "failed to disable CL states\n"); in tb_disable_clx()
294 sw = tb_switch_parent(sw); in tb_disable_clx()
295 } while (sw); in tb_disable_clx()
300 struct tb_switch *sw; in tb_increase_switch_tmu_accuracy() local
302 sw = tb_to_switch(dev); in tb_increase_switch_tmu_accuracy()
303 if (!sw) in tb_increase_switch_tmu_accuracy()
306 if (tb_switch_tmu_is_configured(sw, TB_SWITCH_TMU_MODE_LOWRES)) { in tb_increase_switch_tmu_accuracy()
310 if (tb_switch_clx_is_enabled(sw, TB_CL1)) in tb_increase_switch_tmu_accuracy()
315 ret = tb_switch_tmu_configure(sw, mode); in tb_increase_switch_tmu_accuracy()
319 return tb_switch_tmu_enable(sw); in tb_increase_switch_tmu_accuracy()
327 struct tb_switch *sw; in tb_increase_tmu_accuracy() local
341 sw = tunnel->tb->root_switch; in tb_increase_tmu_accuracy()
342 device_for_each_child(&sw->dev, NULL, tb_increase_switch_tmu_accuracy); in tb_increase_tmu_accuracy()
345 static int tb_enable_tmu(struct tb_switch *sw) in tb_enable_tmu() argument
351 * enable the enhanched uni-directional mode. That covers all in tb_enable_tmu()
352 * the CL states. For v1 and before we need to use the normal in tb_enable_tmu()
356 ret = tb_switch_tmu_configure(sw, in tb_enable_tmu()
358 if (ret == -EOPNOTSUPP) { in tb_enable_tmu()
359 if (tb_switch_clx_is_enabled(sw, TB_CL1)) in tb_enable_tmu()
360 ret = tb_switch_tmu_configure(sw, in tb_enable_tmu()
363 ret = tb_switch_tmu_configure(sw, in tb_enable_tmu()
370 if (tb_switch_tmu_is_enabled(sw)) in tb_enable_tmu()
373 ret = tb_switch_tmu_disable(sw); in tb_enable_tmu()
377 ret = tb_switch_tmu_post_time(sw); in tb_enable_tmu()
381 return tb_switch_tmu_enable(sw); in tb_enable_tmu()
384 static void tb_switch_discover_tunnels(struct tb_switch *sw, in tb_switch_discover_tunnels() argument
388 struct tb *tb = sw->tb; in tb_switch_discover_tunnels()
391 tb_switch_for_each_port(sw, port) { in tb_switch_discover_tunnels()
394 switch (port->config.type) { in tb_switch_discover_tunnels()
413 list_add_tail(&tunnel->list, list); in tb_switch_discover_tunnels()
416 tb_switch_for_each_port(sw, port) { in tb_switch_discover_tunnels()
418 tb_switch_discover_tunnels(port->remote->sw, list, in tb_switch_discover_tunnels()
429 tb_switch_discover_tunnels(tb->root_switch, &tcm->tunnel_list, true); in tb_discover_tunnels()
431 list_for_each_entry(tunnel, &tcm->tunnel_list, list) { in tb_discover_tunnels()
433 struct tb_switch *parent = tunnel->dst_port->sw; in tb_discover_tunnels()
435 while (parent != tunnel->src_port->sw) { in tb_discover_tunnels()
436 parent->boot = true; in tb_discover_tunnels()
440 struct tb_port *in = tunnel->src_port; in tb_discover_tunnels()
441 struct tb_port *out = tunnel->dst_port; in tb_discover_tunnels()
444 pm_runtime_get_sync(&in->sw->dev); in tb_discover_tunnels()
445 pm_runtime_get_sync(&out->sw->dev); in tb_discover_tunnels()
454 if (tb_switch_is_usb4(port->sw)) in tb_port_configure_xdomain()
461 if (tb_switch_is_usb4(port->sw)) in tb_port_unconfigure_xdomain()
466 tb_port_enable(port->dual_link_port); in tb_port_unconfigure_xdomain()
471 struct tb_switch *sw = port->sw; in tb_scan_xdomain() local
472 struct tb *tb = sw->tb; in tb_scan_xdomain()
486 xd = tb_xdomain_alloc(tb, &sw->dev, route, tb->root_switch->uuid, in tb_scan_xdomain()
489 tb_port_at(route, sw)->xdomain = xd; in tb_scan_xdomain()
496 * tb_find_unused_port() - return the first inactive port on @sw
497 * @sw: Switch to find the port on
500 static struct tb_port *tb_find_unused_port(struct tb_switch *sw, in tb_find_unused_port() argument
505 tb_switch_for_each_port(sw, port) { in tb_find_unused_port()
508 if (port->config.type != type) in tb_find_unused_port()
510 if (!port->cap_adap) in tb_find_unused_port()
519 static struct tb_port *tb_find_usb3_down(struct tb_switch *sw, in tb_find_usb3_down() argument
524 down = usb4_switch_map_usb3_down(sw, port); in tb_find_usb3_down()
537 list_for_each_entry(tunnel, &tcm->tunnel_list, list) { in tb_find_tunnel()
538 if (tunnel->type == type && in tb_find_tunnel()
539 ((src_port && src_port == tunnel->src_port) || in tb_find_tunnel()
540 (dst_port && dst_port == tunnel->dst_port))) { in tb_find_tunnel()
553 struct tb_switch *sw; in tb_find_first_usb3_tunnel() local
556 if (dst_port->sw->config.depth > src_port->sw->config.depth) in tb_find_first_usb3_tunnel()
557 sw = dst_port->sw; in tb_find_first_usb3_tunnel()
559 sw = src_port->sw; in tb_find_first_usb3_tunnel()
562 if (sw == tb->root_switch) in tb_find_first_usb3_tunnel()
566 port = tb_port_at(tb_route(sw), tb->root_switch); in tb_find_first_usb3_tunnel()
568 usb3_down = usb4_switch_map_usb3_down(tb->root_switch, port); in tb_find_first_usb3_tunnel()
583 tb_dbg(tb, "calculating available bandwidth between %llx:%u <-> %llx:%u\n", in tb_available_bandwidth()
584 tb_route(src_port->sw), src_port->port, tb_route(dst_port->sw), in tb_available_bandwidth()
585 dst_port->port); in tb_available_bandwidth()
588 if (tunnel && tunnel->src_port != src_port && in tb_available_bandwidth()
589 tunnel->dst_port != dst_port) { in tb_available_bandwidth()
610 link_speed = port->sw->link_speed; in tb_available_bandwidth()
612 * sw->link_width is from upstream perspective in tb_available_bandwidth()
613 * so we use the opposite for downstream of the in tb_available_bandwidth()
616 if (port->sw->link_width == TB_LINK_WIDTH_ASYM_TX) { in tb_available_bandwidth()
619 } else if (port->sw->link_width == TB_LINK_WIDTH_ASYM_RX) { in tb_available_bandwidth()
623 up_bw = link_speed * port->sw->link_width * 1000; in tb_available_bandwidth()
648 up_bw -= up_bw / 10; in tb_available_bandwidth()
649 down_bw -= down_bw / 10; in tb_available_bandwidth()
658 list_for_each_entry(tunnel, &tcm->tunnel_list, list) { in tb_available_bandwidth()
673 * may be re-calculating estimated bandwidth. in tb_available_bandwidth()
675 if (tunnel->src_port == src_port && in tb_available_bandwidth()
676 tunnel->dst_port == dst_port) in tb_available_bandwidth()
685 up_bw -= dp_consumed_up; in tb_available_bandwidth()
686 down_bw -= dp_consumed_down; in tb_available_bandwidth()
695 up_bw -= usb3_consumed_up; in tb_available_bandwidth()
696 down_bw -= usb3_consumed_down; in tb_available_bandwidth()
738 ret = tb_available_bandwidth(tb, tunnel->src_port, tunnel->dst_port, in tb_reclaim_usb3_bandwidth()
751 static int tb_tunnel_usb3(struct tb *tb, struct tb_switch *sw) in tb_tunnel_usb3() argument
753 struct tb_switch *parent = tb_switch_parent(sw); in tb_tunnel_usb3()
764 up = tb_switch_find_port(sw, TB_TYPE_USB3_UP); in tb_tunnel_usb3()
768 if (!sw->link_usb4) in tb_tunnel_usb3()
775 port = tb_switch_downstream_port(sw); in tb_tunnel_usb3()
808 ret = -ENOMEM; in tb_tunnel_usb3()
815 ret = -EIO; in tb_tunnel_usb3()
819 list_add_tail(&tunnel->list, &tcm->tunnel_list); in tb_tunnel_usb3()
834 static int tb_create_usb3_tunnels(struct tb_switch *sw) in tb_create_usb3_tunnels() argument
842 if (tb_route(sw)) { in tb_create_usb3_tunnels()
843 ret = tb_tunnel_usb3(sw->tb, sw); in tb_create_usb3_tunnels()
848 tb_switch_for_each_port(sw, port) { in tb_create_usb3_tunnels()
851 ret = tb_create_usb3_tunnels(port->remote->sw); in tb_create_usb3_tunnels()
862 * tb_scan_switch() - scan for and initialize downstream switches
864 static void tb_scan_switch(struct tb_switch *sw) in tb_scan_switch() argument
868 pm_runtime_get_sync(&sw->dev); in tb_scan_switch()
870 tb_switch_for_each_port(sw, port) in tb_scan_switch()
873 pm_runtime_mark_last_busy(&sw->dev); in tb_scan_switch()
874 pm_runtime_put_autosuspend(&sw->dev); in tb_scan_switch()
878 * tb_scan_port() - check for and initialize switches below port
882 struct tb_cm *tcm = tb_priv(port->sw->tb); in tb_scan_port()
885 struct tb_switch *sw; in tb_scan_port() local
893 tb_queue_hotplug(port->sw->tb, tb_route(port->sw), port->port, in tb_scan_port()
898 if (port->config.type != TB_TYPE_PORT) in tb_scan_port()
900 if (port->dual_link_port && port->link_nr) in tb_scan_port()
906 if (port->usb4) in tb_scan_port()
907 pm_runtime_get_sync(&port->usb4->dev); in tb_scan_port()
911 if (port->remote) { in tb_scan_port()
918 sw = tb_switch_alloc(port->sw->tb, &port->sw->dev, in tb_scan_port()
920 if (IS_ERR(sw)) { in tb_scan_port()
926 if (PTR_ERR(sw) == -EIO || PTR_ERR(sw) == -EADDRNOTAVAIL) in tb_scan_port()
931 if (tb_switch_configure(sw)) { in tb_scan_port()
932 tb_switch_put(sw); in tb_scan_port()
940 if (port->xdomain) { in tb_scan_port()
941 tb_xdomain_remove(port->xdomain); in tb_scan_port()
943 port->xdomain = NULL; in tb_scan_port()
951 if (!tcm->hotplug_active) { in tb_scan_port()
952 dev_set_uevent_suppress(&sw->dev, true); in tb_scan_port()
958 * can support runtime PM. in tb_scan_port()
960 sw->rpm = sw->generation > 1; in tb_scan_port()
962 if (tb_switch_add(sw)) { in tb_scan_port()
963 tb_switch_put(sw); in tb_scan_port()
968 upstream_port = tb_upstream_port(sw); in tb_scan_port()
969 port->remote = upstream_port; in tb_scan_port()
970 upstream_port->remote = port; in tb_scan_port()
971 if (port->dual_link_port && upstream_port->dual_link_port) { in tb_scan_port()
972 port->dual_link_port->remote = upstream_port->dual_link_port; in tb_scan_port()
973 upstream_port->dual_link_port->remote = port->dual_link_port; in tb_scan_port()
977 tb_switch_lane_bonding_enable(sw); in tb_scan_port()
979 tb_switch_configure_link(sw); in tb_scan_port()
985 tb_sw_dbg(sw, "discovery, not touching CL states\n"); in tb_scan_port()
986 else if (tb_enable_clx(sw)) in tb_scan_port()
987 tb_sw_warn(sw, "failed to enable CL states\n"); in tb_scan_port()
989 if (tb_enable_tmu(sw)) in tb_scan_port()
990 tb_sw_warn(sw, "failed to enable TMU\n"); in tb_scan_port()
996 tb_switch_configuration_valid(sw); in tb_scan_port()
1007 if (tcm->hotplug_active && tb_tunnel_usb3(sw->tb, sw)) in tb_scan_port()
1008 tb_sw_warn(sw, "USB3 tunnel creation failed\n"); in tb_scan_port()
1010 tb_add_dp_resources(sw); in tb_scan_port()
1011 tb_scan_switch(sw); in tb_scan_port()
1014 if (port->usb4) { in tb_scan_port()
1015 pm_runtime_mark_last_busy(&port->usb4->dev); in tb_scan_port()
1016 pm_runtime_put_autosuspend(&port->usb4->dev); in tb_scan_port()
1029 list_del(&tunnel->list); in tb_deactivate_and_free_tunnel()
1031 tb = tunnel->tb; in tb_deactivate_and_free_tunnel()
1032 src_port = tunnel->src_port; in tb_deactivate_and_free_tunnel()
1033 dst_port = tunnel->dst_port; in tb_deactivate_and_free_tunnel()
1035 switch (tunnel->type) { in tb_deactivate_and_free_tunnel()
1042 tb_switch_dealloc_dp_resource(src_port->sw, src_port); in tb_deactivate_and_free_tunnel()
1044 pm_runtime_mark_last_busy(&dst_port->sw->dev); in tb_deactivate_and_free_tunnel()
1045 pm_runtime_put_autosuspend(&dst_port->sw->dev); in tb_deactivate_and_free_tunnel()
1046 pm_runtime_mark_last_busy(&src_port->sw->dev); in tb_deactivate_and_free_tunnel()
1047 pm_runtime_put_autosuspend(&src_port->sw->dev); in tb_deactivate_and_free_tunnel()
1066 * tb_free_invalid_tunnels() - destroy tunnels of devices that have gone away
1074 list_for_each_entry_safe(tunnel, n, &tcm->tunnel_list, list) { in tb_free_invalid_tunnels()
1081 * tb_free_unplugged_children() - traverse hierarchy and free unplugged switches
1083 static void tb_free_unplugged_children(struct tb_switch *sw) in tb_free_unplugged_children() argument
1087 tb_switch_for_each_port(sw, port) { in tb_free_unplugged_children()
1091 if (port->remote->sw->is_unplugged) { in tb_free_unplugged_children()
1093 tb_remove_dp_resources(port->remote->sw); in tb_free_unplugged_children()
1094 tb_switch_unconfigure_link(port->remote->sw); in tb_free_unplugged_children()
1095 tb_switch_lane_bonding_disable(port->remote->sw); in tb_free_unplugged_children()
1096 tb_switch_remove(port->remote->sw); in tb_free_unplugged_children()
1097 port->remote = NULL; in tb_free_unplugged_children()
1098 if (port->dual_link_port) in tb_free_unplugged_children()
1099 port->dual_link_port->remote = NULL; in tb_free_unplugged_children()
1101 tb_free_unplugged_children(port->remote->sw); in tb_free_unplugged_children()
1106 static struct tb_port *tb_find_pcie_down(struct tb_switch *sw, in tb_find_pcie_down() argument
1115 if (tb_switch_is_usb4(sw)) { in tb_find_pcie_down()
1116 down = usb4_switch_map_pcie_down(sw, port); in tb_find_pcie_down()
1117 } else if (!tb_route(sw)) { in tb_find_pcie_down()
1118 int phy_port = tb_phy_port_from_link(port->port); in tb_find_pcie_down()
1122 * Hard-coded Thunderbolt port to PCIe down port mapping in tb_find_pcie_down()
1125 if (tb_switch_is_cactus_ridge(sw) || in tb_find_pcie_down()
1126 tb_switch_is_alpine_ridge(sw)) in tb_find_pcie_down()
1128 else if (tb_switch_is_falcon_ridge(sw)) in tb_find_pcie_down()
1130 else if (tb_switch_is_titan_ridge(sw)) in tb_find_pcie_down()
1135 /* Validate the hard-coding */ in tb_find_pcie_down()
1136 if (WARN_ON(index > sw->config.max_port_number)) in tb_find_pcie_down()
1139 down = &sw->ports[index]; in tb_find_pcie_down()
1152 return tb_find_unused_port(sw, TB_TYPE_PCIE_DOWN); in tb_find_pcie_down()
1159 struct tb *tb = group->tb; in tb_recalc_estimated_bandwidth_for_group()
1163 tb_dbg(tb, "re-calculating bandwidth estimation for group %u\n", in tb_recalc_estimated_bandwidth_for_group()
1164 group->index); in tb_recalc_estimated_bandwidth_for_group()
1167 list_for_each_entry(in, &group->ports, group_list) { in tb_recalc_estimated_bandwidth_for_group()
1189 first_tunnel->src_port, first_tunnel->dst_port); in tb_recalc_estimated_bandwidth_for_group()
1197 out = tunnel->dst_port; in tb_recalc_estimated_bandwidth_for_group()
1202 "failed to re-calculate estimated bandwidth\n"); in tb_recalc_estimated_bandwidth_for_group()
1208 * - already allocated bandwidth for the DP tunnel in tb_recalc_estimated_bandwidth_for_group()
1209 * - available bandwidth along the path in tb_recalc_estimated_bandwidth_for_group()
1210 * - bandwidth allocated for USB 3.x but not used. in tb_recalc_estimated_bandwidth_for_group()
1212 tb_port_dbg(in, "re-calculated estimated bandwidth %u/%u Mb/s\n", in tb_recalc_estimated_bandwidth_for_group()
1215 if (in->sw->config.depth < out->sw->config.depth) in tb_recalc_estimated_bandwidth_for_group()
1225 tb_reclaim_usb3_bandwidth(tb, first_tunnel->src_port, in tb_recalc_estimated_bandwidth_for_group()
1226 first_tunnel->dst_port); in tb_recalc_estimated_bandwidth_for_group()
1228 tb_dbg(tb, "bandwidth estimation for group %u done\n", group->index); in tb_recalc_estimated_bandwidth_for_group()
1236 tb_dbg(tb, "bandwidth consumption changed, re-calculating estimated bandwidth\n"); in tb_recalc_estimated_bandwidth()
1238 for (i = 0; i < ARRAY_SIZE(tcm->groups); i++) { in tb_recalc_estimated_bandwidth()
1239 struct tb_bandwidth_group *group = &tcm->groups[i]; in tb_recalc_estimated_bandwidth()
1241 if (!list_empty(&group->ports)) in tb_recalc_estimated_bandwidth()
1245 tb_dbg(tb, "bandwidth re-calculation done\n"); in tb_recalc_estimated_bandwidth()
1253 host_port = tb_route(in->sw) ? in tb_find_dp_out()
1254 tb_port_at(tb_route(in->sw), tb->root_switch) : NULL; in tb_find_dp_out()
1256 list_for_each_entry(port, &tcm->dp_resources, list) { in tb_find_dp_out()
1261 tb_port_dbg(port, "DP OUT in use\n"); in tb_find_dp_out()
1271 if (host_port && tb_route(port->sw)) { in tb_find_dp_out()
1274 p = tb_port_at(tb_route(port->sw), tb->root_switch); in tb_find_dp_out()
1301 tb_dbg(tb, "looking for DP IN <-> DP OUT pairs:\n"); in tb_tunnel_dp()
1305 list_for_each_entry(port, &tcm->dp_resources, list) { in tb_tunnel_dp()
1310 tb_port_dbg(port, "DP IN in use\n"); in tb_tunnel_dp()
1339 list_for_each_entry(tunnel, &tcm->tunnel_list, list) { in tb_tunnel_dp()
1354 pm_runtime_get_sync(&in->sw->dev); in tb_tunnel_dp()
1355 pm_runtime_get_sync(&out->sw->dev); in tb_tunnel_dp()
1357 if (tb_switch_alloc_dp_resource(in->sw, in)) { in tb_tunnel_dp()
1391 list_add_tail(&tunnel->list, &tcm->tunnel_list); in tb_tunnel_dp()
1411 tb_switch_dealloc_dp_resource(in->sw, in); in tb_tunnel_dp()
1413 pm_runtime_mark_last_busy(&out->sw->dev); in tb_tunnel_dp()
1414 pm_runtime_put_autosuspend(&out->sw->dev); in tb_tunnel_dp()
1415 pm_runtime_mark_last_busy(&in->sw->dev); in tb_tunnel_dp()
1416 pm_runtime_put_autosuspend(&in->sw->dev); in tb_tunnel_dp()
1436 list_del_init(&port->list); in tb_dp_resource_unavailable()
1454 list_for_each_entry(p, &tcm->dp_resources, list) { in tb_dp_resource_available()
1461 list_add_tail(&port->list, &tcm->dp_resources); in tb_dp_resource_available()
1463 /* Look for suitable DP IN <-> DP OUT pairs now */ in tb_dp_resource_available()
1474 * will be re-established after resume based on plug events. in tb_disconnect_and_release_dp()
1476 list_for_each_entry_safe_reverse(tunnel, n, &tcm->tunnel_list, list) { in tb_disconnect_and_release_dp()
1481 while (!list_empty(&tcm->dp_resources)) { in tb_disconnect_and_release_dp()
1484 port = list_first_entry(&tcm->dp_resources, in tb_disconnect_and_release_dp()
1486 list_del_init(&port->list); in tb_disconnect_and_release_dp()
1490 static int tb_disconnect_pci(struct tb *tb, struct tb_switch *sw) in tb_disconnect_pci() argument
1495 up = tb_switch_find_port(sw, TB_TYPE_PCIE_UP); in tb_disconnect_pci()
1497 return -ENODEV; in tb_disconnect_pci()
1501 return -ENODEV; in tb_disconnect_pci()
1503 tb_switch_xhci_disconnect(sw); in tb_disconnect_pci()
1506 list_del(&tunnel->list); in tb_disconnect_pci()
1511 static int tb_tunnel_pci(struct tb *tb, struct tb_switch *sw) in tb_tunnel_pci() argument
1517 up = tb_switch_find_port(sw, TB_TYPE_PCIE_UP); in tb_tunnel_pci()
1525 port = tb_switch_downstream_port(sw); in tb_tunnel_pci()
1526 down = tb_find_pcie_down(tb_switch_parent(sw), port); in tb_tunnel_pci()
1532 return -ENOMEM; in tb_tunnel_pci()
1538 return -EIO; in tb_tunnel_pci()
1545 if (tb_switch_pcie_l1_enable(sw)) in tb_tunnel_pci()
1546 tb_sw_warn(sw, "failed to enable PCIe L1 for Titan Ridge\n"); in tb_tunnel_pci()
1548 if (tb_switch_xhci_connect(sw)) in tb_tunnel_pci()
1549 tb_sw_warn(sw, "failed to connect xHCI\n"); in tb_tunnel_pci()
1551 list_add_tail(&tunnel->list, &tcm->tunnel_list); in tb_tunnel_pci()
1562 struct tb_switch *sw; in tb_approve_xdomain_paths() local
1565 sw = tb_to_switch(xd->dev.parent); in tb_approve_xdomain_paths()
1566 dst_port = tb_port_at(xd->route, sw); in tb_approve_xdomain_paths()
1567 nhi_port = tb_switch_find_port(tb->root_switch, TB_TYPE_NHI); in tb_approve_xdomain_paths()
1569 mutex_lock(&tb->lock); in tb_approve_xdomain_paths()
1575 tb_disable_clx(sw); in tb_approve_xdomain_paths()
1580 ret = -ENOMEM; in tb_approve_xdomain_paths()
1587 ret = -EIO; in tb_approve_xdomain_paths()
1591 list_add_tail(&tunnel->list, &tcm->tunnel_list); in tb_approve_xdomain_paths()
1592 mutex_unlock(&tb->lock); in tb_approve_xdomain_paths()
1598 tb_enable_clx(sw); in tb_approve_xdomain_paths()
1599 mutex_unlock(&tb->lock); in tb_approve_xdomain_paths()
1611 struct tb_switch *sw; in __tb_disconnect_xdomain_paths() local
1613 sw = tb_to_switch(xd->dev.parent); in __tb_disconnect_xdomain_paths()
1614 dst_port = tb_port_at(xd->route, sw); in __tb_disconnect_xdomain_paths()
1615 nhi_port = tb_switch_find_port(tb->root_switch, TB_TYPE_NHI); in __tb_disconnect_xdomain_paths()
1617 list_for_each_entry_safe(tunnel, n, &tcm->tunnel_list, list) { in __tb_disconnect_xdomain_paths()
1620 if (tunnel->src_port != nhi_port || tunnel->dst_port != dst_port) in __tb_disconnect_xdomain_paths()
1629 * Try to re-enable CL states now, it is OK if this fails in __tb_disconnect_xdomain_paths()
1633 tb_enable_clx(sw); in __tb_disconnect_xdomain_paths()
1640 if (!xd->is_unplugged) { in tb_disconnect_xdomain_paths()
1641 mutex_lock(&tb->lock); in tb_disconnect_xdomain_paths()
1645 mutex_unlock(&tb->lock); in tb_disconnect_xdomain_paths()
1653 * tb_handle_hotplug() - handle hotplug event
1655 * Executes on tb->wq.
1660 struct tb *tb = ev->tb; in tb_handle_hotplug()
1662 struct tb_switch *sw; in tb_handle_hotplug() local
1666 pm_runtime_get_sync(&tb->dev); in tb_handle_hotplug()
1668 mutex_lock(&tb->lock); in tb_handle_hotplug()
1669 if (!tcm->hotplug_active) in tb_handle_hotplug()
1672 sw = tb_switch_find_by_route(tb, ev->route); in tb_handle_hotplug()
1673 if (!sw) { in tb_handle_hotplug()
1676 ev->route, ev->port, ev->unplug); in tb_handle_hotplug()
1679 if (ev->port > sw->config.max_port_number) { in tb_handle_hotplug()
1682 ev->route, ev->port, ev->unplug); in tb_handle_hotplug()
1685 port = &sw->ports[ev->port]; in tb_handle_hotplug()
1688 ev->route, ev->port, ev->unplug); in tb_handle_hotplug()
1692 pm_runtime_get_sync(&sw->dev); in tb_handle_hotplug()
1694 if (ev->unplug) { in tb_handle_hotplug()
1699 tb_sw_set_unplugged(port->remote->sw); in tb_handle_hotplug()
1701 tb_remove_dp_resources(port->remote->sw); in tb_handle_hotplug()
1702 tb_switch_tmu_disable(port->remote->sw); in tb_handle_hotplug()
1703 tb_switch_unconfigure_link(port->remote->sw); in tb_handle_hotplug()
1704 tb_switch_lane_bonding_disable(port->remote->sw); in tb_handle_hotplug()
1705 tb_switch_remove(port->remote->sw); in tb_handle_hotplug()
1706 port->remote = NULL; in tb_handle_hotplug()
1707 if (port->dual_link_port) in tb_handle_hotplug()
1708 port->dual_link_port->remote = NULL; in tb_handle_hotplug()
1712 } else if (port->xdomain) { in tb_handle_hotplug()
1713 struct tb_xdomain *xd = tb_xdomain_get(port->xdomain); in tb_handle_hotplug()
1723 xd->is_unplugged = true; in tb_handle_hotplug()
1725 port->xdomain = NULL; in tb_handle_hotplug()
1726 __tb_disconnect_xdomain_paths(tb, xd, -1, -1, -1, -1); in tb_handle_hotplug()
1731 } else if (!port->port) { in tb_handle_hotplug()
1732 tb_sw_dbg(sw, "xHCI disconnect request\n"); in tb_handle_hotplug()
1733 tb_switch_xhci_disconnect(sw); in tb_handle_hotplug()
1738 } else if (port->remote) { in tb_handle_hotplug()
1740 } else if (!port->port && sw->authorized) { in tb_handle_hotplug()
1741 tb_sw_dbg(sw, "xHCI connect request\n"); in tb_handle_hotplug()
1742 tb_switch_xhci_connect(sw); in tb_handle_hotplug()
1747 if (!port->remote) in tb_handle_hotplug()
1754 pm_runtime_mark_last_busy(&sw->dev); in tb_handle_hotplug()
1755 pm_runtime_put_autosuspend(&sw->dev); in tb_handle_hotplug()
1758 tb_switch_put(sw); in tb_handle_hotplug()
1760 mutex_unlock(&tb->lock); in tb_handle_hotplug()
1762 pm_runtime_mark_last_busy(&tb->dev); in tb_handle_hotplug()
1763 pm_runtime_put_autosuspend(&tb->dev); in tb_handle_hotplug()
1774 struct tb *tb = tunnel->tb; in tb_alloc_dp_bandwidth()
1781 in = tunnel->src_port; in tb_alloc_dp_bandwidth()
1782 out = tunnel->dst_port; in tb_alloc_dp_bandwidth()
1794 * Since the link cannot go higher than 17280 we use that in our in tb_alloc_dp_bandwidth()
1834 return -ENOBUFS; in tb_alloc_dp_bandwidth()
1861 * are also in the same group but we use the same function here in tb_alloc_dp_bandwidth()
1862 * that we use with the normal bandwidth allocation). in tb_alloc_dp_bandwidth()
1876 ret = -ENOBUFS; in tb_alloc_dp_bandwidth()
1890 struct tb *tb = ev->tb; in tb_handle_dp_bandwidth_request()
1892 struct tb_switch *sw; in tb_handle_dp_bandwidth_request() local
1894 pm_runtime_get_sync(&tb->dev); in tb_handle_dp_bandwidth_request()
1896 mutex_lock(&tb->lock); in tb_handle_dp_bandwidth_request()
1897 if (!tcm->hotplug_active) in tb_handle_dp_bandwidth_request()
1900 sw = tb_switch_find_by_route(tb, ev->route); in tb_handle_dp_bandwidth_request()
1901 if (!sw) { in tb_handle_dp_bandwidth_request()
1902 tb_warn(tb, "bandwidth request from non-existent router %llx\n", in tb_handle_dp_bandwidth_request()
1903 ev->route); in tb_handle_dp_bandwidth_request()
1907 in = &sw->ports[ev->port]; in tb_handle_dp_bandwidth_request()
1909 tb_port_warn(in, "bandwidth request to non-DP IN adapter\n"); in tb_handle_dp_bandwidth_request()
1922 if (ret == -ENODATA) in tb_handle_dp_bandwidth_request()
1938 out = tunnel->dst_port; in tb_handle_dp_bandwidth_request()
1940 if (in->sw->config.depth < out->sw->config.depth) { in tb_handle_dp_bandwidth_request()
1941 requested_up = -1; in tb_handle_dp_bandwidth_request()
1945 requested_down = -1; in tb_handle_dp_bandwidth_request()
1950 if (ret == -ENOBUFS) in tb_handle_dp_bandwidth_request()
1963 tb_switch_put(sw); in tb_handle_dp_bandwidth_request()
1965 mutex_unlock(&tb->lock); in tb_handle_dp_bandwidth_request()
1967 pm_runtime_mark_last_busy(&tb->dev); in tb_handle_dp_bandwidth_request()
1968 pm_runtime_put_autosuspend(&tb->dev); in tb_handle_dp_bandwidth_request()
1981 ev->tb = tb; in tb_queue_dp_bandwidth_request()
1982 ev->route = route; in tb_queue_dp_bandwidth_request()
1983 ev->port = port; in tb_queue_dp_bandwidth_request()
1984 INIT_WORK(&ev->work, tb_handle_dp_bandwidth_request); in tb_queue_dp_bandwidth_request()
1985 queue_work(tb->wq, &ev->work); in tb_queue_dp_bandwidth_request()
1992 switch (error->error) { in tb_handle_notification()
1996 if (tb_cfg_ack_notification(tb->ctl, route, error)) in tb_handle_notification()
2002 if (tb_cfg_ack_notification(tb->ctl, route, error)) in tb_handle_notification()
2005 tb_queue_dp_bandwidth_request(tb, route, error->port); in tb_handle_notification()
2015 * tb_schedule_hotplug_handler() - callback function for the control channel
2023 u64 route = tb_cfg_get_route(&pkg->header); in tb_handle_event()
2036 if (tb_cfg_ack_plug(tb->ctl, route, pkg->port, pkg->unplug)) { in tb_handle_event()
2038 pkg->port); in tb_handle_event()
2041 tb_queue_hotplug(tb, route, pkg->port, pkg->unplug); in tb_handle_event()
2050 cancel_delayed_work(&tcm->remove_work); in tb_stop()
2052 list_for_each_entry_safe(tunnel, n, &tcm->tunnel_list, list) { in tb_stop()
2062 tb_switch_remove(tb->root_switch); in tb_stop()
2063 tcm->hotplug_active = false; /* signal tb_handle_hotplug to quit */ in tb_stop()
2069 struct tb_switch *sw = tb_to_switch(dev); in tb_scan_finalize_switch() local
2076 if (sw->boot) in tb_scan_finalize_switch()
2077 sw->authorized = 1; in tb_scan_finalize_switch()
2080 kobject_uevent(&dev->kobj, KOBJ_ADD); in tb_scan_finalize_switch()
2092 tb->root_switch = tb_switch_alloc(tb, &tb->dev, 0); in tb_start()
2093 if (IS_ERR(tb->root_switch)) in tb_start()
2094 return PTR_ERR(tb->root_switch); in tb_start()
2104 tb->root_switch->no_nvm_upgrade = !tb_switch_is_usb4(tb->root_switch); in tb_start()
2105 /* All USB4 routers support runtime PM */ in tb_start()
2106 tb->root_switch->rpm = tb_switch_is_usb4(tb->root_switch); in tb_start()
2108 ret = tb_switch_configure(tb->root_switch); in tb_start()
2110 tb_switch_put(tb->root_switch); in tb_start()
2115 ret = tb_switch_add(tb->root_switch); in tb_start()
2117 tb_switch_put(tb->root_switch); in tb_start()
2125 tb_switch_tmu_configure(tb->root_switch, TB_SWITCH_TMU_MODE_LOWRES); in tb_start()
2127 tb_switch_tmu_enable(tb->root_switch); in tb_start()
2129 tb_scan_switch(tb->root_switch); in tb_start()
2138 tb_create_usb3_tunnels(tb->root_switch); in tb_start()
2140 tb_add_dp_resources(tb->root_switch); in tb_start()
2142 device_for_each_child(&tb->root_switch->dev, NULL, in tb_start()
2146 tcm->hotplug_active = true; in tb_start()
2156 tb_switch_suspend(tb->root_switch, false); in tb_suspend_noirq()
2157 tcm->hotplug_active = false; /* signal tb_handle_hotplug to quit */ in tb_suspend_noirq()
2163 static void tb_restore_children(struct tb_switch *sw) in tb_restore_children() argument
2168 if (sw->is_unplugged) in tb_restore_children()
2171 if (tb_enable_clx(sw)) in tb_restore_children()
2172 tb_sw_warn(sw, "failed to re-enable CL states\n"); in tb_restore_children()
2174 if (tb_enable_tmu(sw)) in tb_restore_children()
2175 tb_sw_warn(sw, "failed to restore TMU configuration\n"); in tb_restore_children()
2177 tb_switch_configuration_valid(sw); in tb_restore_children()
2179 tb_switch_for_each_port(sw, port) { in tb_restore_children()
2180 if (!tb_port_has_remote(port) && !port->xdomain) in tb_restore_children()
2183 if (port->remote) { in tb_restore_children()
2184 tb_switch_lane_bonding_enable(port->remote->sw); in tb_restore_children()
2185 tb_switch_configure_link(port->remote->sw); in tb_restore_children()
2187 tb_restore_children(port->remote->sw); in tb_restore_children()
2188 } else if (port->xdomain) { in tb_restore_children()
2189 tb_port_configure_xdomain(port, port->xdomain); in tb_restore_children()
2204 tb_switch_reset(tb->root_switch); in tb_resume_noirq()
2206 tb_switch_resume(tb->root_switch); in tb_resume_noirq()
2208 tb_free_unplugged_children(tb->root_switch); in tb_resume_noirq()
2209 tb_restore_children(tb->root_switch); in tb_resume_noirq()
2217 tb_switch_discover_tunnels(tb->root_switch, &tunnels, false); in tb_resume_noirq()
2225 /* Re-create our tunnels now */ in tb_resume_noirq()
2226 list_for_each_entry_safe(tunnel, n, &tcm->tunnel_list, list) { in tb_resume_noirq()
2227 /* USB3 requires delay before it can be re-activated */ in tb_resume_noirq()
2235 if (!list_empty(&tcm->tunnel_list)) { in tb_resume_noirq()
2244 tcm->hotplug_active = true; in tb_resume_noirq()
2250 static int tb_free_unplugged_xdomains(struct tb_switch *sw) in tb_free_unplugged_xdomains() argument
2255 tb_switch_for_each_port(sw, port) { in tb_free_unplugged_xdomains()
2258 if (port->xdomain && port->xdomain->is_unplugged) { in tb_free_unplugged_xdomains()
2260 tb_xdomain_remove(port->xdomain); in tb_free_unplugged_xdomains()
2262 port->xdomain = NULL; in tb_free_unplugged_xdomains()
2264 } else if (port->remote) { in tb_free_unplugged_xdomains()
2265 ret += tb_free_unplugged_xdomains(port->remote->sw); in tb_free_unplugged_xdomains()
2276 tcm->hotplug_active = false; in tb_freeze_noirq()
2284 tcm->hotplug_active = true; in tb_thaw_noirq()
2295 mutex_lock(&tb->lock); in tb_complete()
2296 if (tb_free_unplugged_xdomains(tb->root_switch)) in tb_complete()
2297 tb_scan_switch(tb->root_switch); in tb_complete()
2298 mutex_unlock(&tb->lock); in tb_complete()
2305 mutex_lock(&tb->lock); in tb_runtime_suspend()
2306 tb_switch_suspend(tb->root_switch, true); in tb_runtime_suspend()
2307 tcm->hotplug_active = false; in tb_runtime_suspend()
2308 mutex_unlock(&tb->lock); in tb_runtime_suspend()
2318 mutex_lock(&tb->lock); in tb_remove_work()
2319 if (tb->root_switch) { in tb_remove_work()
2320 tb_free_unplugged_children(tb->root_switch); in tb_remove_work()
2321 tb_free_unplugged_xdomains(tb->root_switch); in tb_remove_work()
2323 mutex_unlock(&tb->lock); in tb_remove_work()
2331 mutex_lock(&tb->lock); in tb_runtime_resume()
2332 tb_switch_resume(tb->root_switch); in tb_runtime_resume()
2334 tb_restore_children(tb->root_switch); in tb_runtime_resume()
2335 list_for_each_entry_safe(tunnel, n, &tcm->tunnel_list, list) in tb_runtime_resume()
2337 tcm->hotplug_active = true; in tb_runtime_resume()
2338 mutex_unlock(&tb->lock); in tb_runtime_resume()
2345 queue_delayed_work(tb->wq, &tcm->remove_work, msecs_to_jiffies(50)); in tb_runtime_resume()
2381 switch (nhi->pdev->device) { in tb_apple_add_links()
2391 upstream = pci_upstream_bridge(nhi->pdev); in tb_apple_add_links()
2405 * back to NHI so that PCIe tunnels can be re-established after in tb_apple_add_links()
2409 for_each_pci_bridge(pdev, upstream->subordinate) { in tb_apple_add_links()
2415 !pdev->is_hotplug_bridge) in tb_apple_add_links()
2418 link = device_link_add(&pdev->dev, &nhi->pdev->dev, in tb_apple_add_links()
2422 dev_dbg(&nhi->pdev->dev, "created link from %s\n", in tb_apple_add_links()
2423 dev_name(&pdev->dev)); in tb_apple_add_links()
2426 dev_warn(&nhi->pdev->dev, "device link creation from %s failed\n", in tb_apple_add_links()
2427 dev_name(&pdev->dev)); in tb_apple_add_links()
2444 tb->security_level = TB_SECURITY_USER; in tb_probe()
2446 tb->security_level = TB_SECURITY_NOPCIE; in tb_probe()
2448 tb->cm_ops = &tb_cm_ops; in tb_probe()
2451 INIT_LIST_HEAD(&tcm->tunnel_list); in tb_probe()
2452 INIT_LIST_HEAD(&tcm->dp_resources); in tb_probe()
2453 INIT_DELAYED_WORK(&tcm->remove_work, tb_remove_work); in tb_probe()