Lines Matching +full:use +full:- +full:sw +full:- +full:pm
1 // SPDX-License-Identifier: GPL-2.0
3 * Thunderbolt driver - bus logic (NHI independent)
22 * struct tb_cm - Simple Thunderbolt connection manager
41 return ((void *)tcm - sizeof(struct tb)); in tcm_to_tb()
62 ev->tb = tb; in tb_queue_hotplug()
63 ev->route = route; in tb_queue_hotplug()
64 ev->port = port; in tb_queue_hotplug()
65 ev->unplug = unplug; in tb_queue_hotplug()
66 INIT_WORK(&ev->work, tb_handle_hotplug); in tb_queue_hotplug()
67 queue_work(tb->wq, &ev->work); in tb_queue_hotplug()
72 static void tb_add_dp_resources(struct tb_switch *sw) in tb_add_dp_resources() argument
74 struct tb_cm *tcm = tb_priv(sw->tb); in tb_add_dp_resources()
77 tb_switch_for_each_port(sw, port) { in tb_add_dp_resources()
81 if (!tb_switch_query_dp_resource(sw, port)) in tb_add_dp_resources()
84 list_add_tail(&port->list, &tcm->dp_resources); in tb_add_dp_resources()
89 static void tb_remove_dp_resources(struct tb_switch *sw) in tb_remove_dp_resources() argument
91 struct tb_cm *tcm = tb_priv(sw->tb); in tb_remove_dp_resources()
95 tb_switch_for_each_port(sw, port) { in tb_remove_dp_resources()
97 tb_remove_dp_resources(port->remote->sw); in tb_remove_dp_resources()
100 list_for_each_entry_safe(port, tmp, &tcm->dp_resources, list) { in tb_remove_dp_resources()
101 if (port->sw == sw) { in tb_remove_dp_resources()
103 list_del_init(&port->list); in tb_remove_dp_resources()
113 list_for_each_entry(p, &tcm->dp_resources, list) { in tb_discover_dp_resource()
120 list_add_tail(&port->list, &tcm->dp_resources); in tb_discover_dp_resource()
128 list_for_each_entry(tunnel, &tcm->tunnel_list, list) { in tb_discover_dp_resources()
130 tb_discover_dp_resource(tb, tunnel->dst_port); in tb_discover_dp_resources()
134 static void tb_switch_discover_tunnels(struct tb_switch *sw, in tb_switch_discover_tunnels() argument
138 struct tb *tb = sw->tb; in tb_switch_discover_tunnels()
141 tb_switch_for_each_port(sw, port) { in tb_switch_discover_tunnels()
144 switch (port->config.type) { in tb_switch_discover_tunnels()
152 tb_switch_enable_tmu_1st_child(tb->root_switch, in tb_switch_discover_tunnels()
169 list_add_tail(&tunnel->list, list); in tb_switch_discover_tunnels()
172 tb_switch_for_each_port(sw, port) { in tb_switch_discover_tunnels()
174 tb_switch_discover_tunnels(port->remote->sw, list, in tb_switch_discover_tunnels()
185 tb_switch_discover_tunnels(tb->root_switch, &tcm->tunnel_list, true); in tb_discover_tunnels()
187 list_for_each_entry(tunnel, &tcm->tunnel_list, list) { in tb_discover_tunnels()
189 struct tb_switch *parent = tunnel->dst_port->sw; in tb_discover_tunnels()
191 while (parent != tunnel->src_port->sw) { in tb_discover_tunnels()
192 parent->boot = true; in tb_discover_tunnels()
197 pm_runtime_get_sync(&tunnel->src_port->sw->dev); in tb_discover_tunnels()
198 pm_runtime_get_sync(&tunnel->dst_port->sw->dev); in tb_discover_tunnels()
205 if (tb_switch_is_usb4(port->sw)) in tb_port_configure_xdomain()
212 if (tb_switch_is_usb4(port->sw)) in tb_port_unconfigure_xdomain()
217 tb_port_enable(port->dual_link_port); in tb_port_unconfigure_xdomain()
222 struct tb_switch *sw = port->sw; in tb_scan_xdomain() local
223 struct tb *tb = sw->tb; in tb_scan_xdomain()
237 xd = tb_xdomain_alloc(tb, &sw->dev, route, tb->root_switch->uuid, in tb_scan_xdomain()
240 tb_port_at(route, sw)->xdomain = xd; in tb_scan_xdomain()
246 static int tb_enable_tmu(struct tb_switch *sw) in tb_enable_tmu() argument
251 if (tb_switch_tmu_is_enabled(sw, sw->tmu.unidirectional_request)) in tb_enable_tmu()
254 ret = tb_switch_tmu_disable(sw); in tb_enable_tmu()
258 ret = tb_switch_tmu_post_time(sw); in tb_enable_tmu()
262 return tb_switch_tmu_enable(sw); in tb_enable_tmu()
266 * tb_find_unused_port() - return the first inactive port on @sw
267 * @sw: Switch to find the port on
270 static struct tb_port *tb_find_unused_port(struct tb_switch *sw, in tb_find_unused_port() argument
275 tb_switch_for_each_port(sw, port) { in tb_find_unused_port()
278 if (port->config.type != type) in tb_find_unused_port()
280 if (!port->cap_adap) in tb_find_unused_port()
289 static struct tb_port *tb_find_usb3_down(struct tb_switch *sw, in tb_find_usb3_down() argument
294 down = usb4_switch_map_usb3_down(sw, port); in tb_find_usb3_down()
307 list_for_each_entry(tunnel, &tcm->tunnel_list, list) { in tb_find_tunnel()
308 if (tunnel->type == type && in tb_find_tunnel()
309 ((src_port && src_port == tunnel->src_port) || in tb_find_tunnel()
310 (dst_port && dst_port == tunnel->dst_port))) { in tb_find_tunnel()
323 struct tb_switch *sw; in tb_find_first_usb3_tunnel() local
326 if (dst_port->sw->config.depth > src_port->sw->config.depth) in tb_find_first_usb3_tunnel()
327 sw = dst_port->sw; in tb_find_first_usb3_tunnel()
329 sw = src_port->sw; in tb_find_first_usb3_tunnel()
332 if (sw == tb->root_switch) in tb_find_first_usb3_tunnel()
336 port = tb_port_at(tb_route(sw), tb->root_switch); in tb_find_first_usb3_tunnel()
338 usb3_down = usb4_switch_map_usb3_down(tb->root_switch, port); in tb_find_first_usb3_tunnel()
376 link_speed = port->sw->link_speed; in tb_available_bandwidth()
383 link_width = port->bonded ? 2 : 1; in tb_available_bandwidth()
387 up_bw -= up_bw / 10; in tb_available_bandwidth()
396 list_for_each_entry(tunnel, &tcm->tunnel_list, list) { in tb_available_bandwidth()
411 up_bw -= dp_consumed_up; in tb_available_bandwidth()
412 down_bw -= dp_consumed_down; in tb_available_bandwidth()
421 up_bw -= usb3_consumed_up; in tb_available_bandwidth()
422 down_bw -= usb3_consumed_down; in tb_available_bandwidth()
464 ret = tb_available_bandwidth(tb, tunnel->src_port, tunnel->dst_port, in tb_reclaim_usb3_bandwidth()
477 static int tb_tunnel_usb3(struct tb *tb, struct tb_switch *sw) in tb_tunnel_usb3() argument
479 struct tb_switch *parent = tb_switch_parent(sw); in tb_tunnel_usb3()
490 up = tb_switch_find_port(sw, TB_TYPE_USB3_UP); in tb_tunnel_usb3()
494 if (!sw->link_usb4) in tb_tunnel_usb3()
501 port = tb_port_at(tb_route(sw), parent); in tb_tunnel_usb3()
534 ret = -ENOMEM; in tb_tunnel_usb3()
541 ret = -EIO; in tb_tunnel_usb3()
545 list_add_tail(&tunnel->list, &tcm->tunnel_list); in tb_tunnel_usb3()
560 static int tb_create_usb3_tunnels(struct tb_switch *sw) in tb_create_usb3_tunnels() argument
568 if (tb_route(sw)) { in tb_create_usb3_tunnels()
569 ret = tb_tunnel_usb3(sw->tb, sw); in tb_create_usb3_tunnels()
574 tb_switch_for_each_port(sw, port) { in tb_create_usb3_tunnels()
577 ret = tb_create_usb3_tunnels(port->remote->sw); in tb_create_usb3_tunnels()
588 * tb_scan_switch() - scan for and initialize downstream switches
590 static void tb_scan_switch(struct tb_switch *sw) in tb_scan_switch() argument
594 pm_runtime_get_sync(&sw->dev); in tb_scan_switch()
596 tb_switch_for_each_port(sw, port) in tb_scan_switch()
599 pm_runtime_mark_last_busy(&sw->dev); in tb_scan_switch()
600 pm_runtime_put_autosuspend(&sw->dev); in tb_scan_switch()
604 * tb_scan_port() - check for and initialize switches below port
608 struct tb_cm *tcm = tb_priv(port->sw->tb); in tb_scan_port()
610 struct tb_switch *sw; in tb_scan_port() local
619 tb_queue_hotplug(port->sw->tb, tb_route(port->sw), port->port, in tb_scan_port()
624 if (port->config.type != TB_TYPE_PORT) in tb_scan_port()
626 if (port->dual_link_port && port->link_nr) in tb_scan_port()
633 if (port->remote) { in tb_scan_port()
640 sw = tb_switch_alloc(port->sw->tb, &port->sw->dev, in tb_scan_port()
642 if (IS_ERR(sw)) { in tb_scan_port()
648 if (PTR_ERR(sw) == -EIO || PTR_ERR(sw) == -EADDRNOTAVAIL) in tb_scan_port()
653 if (tb_switch_configure(sw)) { in tb_scan_port()
654 tb_switch_put(sw); in tb_scan_port()
662 if (port->xdomain) { in tb_scan_port()
663 tb_xdomain_remove(port->xdomain); in tb_scan_port()
665 port->xdomain = NULL; in tb_scan_port()
673 if (!tcm->hotplug_active) in tb_scan_port()
674 dev_set_uevent_suppress(&sw->dev, true); in tb_scan_port()
678 * can support runtime PM. in tb_scan_port()
680 sw->rpm = sw->generation > 1; in tb_scan_port()
682 if (tb_switch_add(sw)) { in tb_scan_port()
683 tb_switch_put(sw); in tb_scan_port()
688 upstream_port = tb_upstream_port(sw); in tb_scan_port()
689 port->remote = upstream_port; in tb_scan_port()
690 upstream_port->remote = port; in tb_scan_port()
691 if (port->dual_link_port && upstream_port->dual_link_port) { in tb_scan_port()
692 port->dual_link_port->remote = upstream_port->dual_link_port; in tb_scan_port()
693 upstream_port->dual_link_port->remote = port->dual_link_port; in tb_scan_port()
697 tb_switch_lane_bonding_enable(sw); in tb_scan_port()
699 tb_switch_configure_link(sw); in tb_scan_port()
704 ret = tb_switch_enable_clx(sw, TB_CL1); in tb_scan_port()
705 if (ret && ret != -EOPNOTSUPP) in tb_scan_port()
706 tb_sw_warn(sw, "failed to enable %s on upstream port\n", in tb_scan_port()
709 if (tb_switch_is_clx_enabled(sw, TB_CL1)) in tb_scan_port()
712 * Normal-Uni mode. in tb_scan_port()
714 tb_switch_tmu_configure(sw, TB_SWITCH_TMU_RATE_NORMAL, true); in tb_scan_port()
716 /* If CLx disabled, configure router's TMU to HiFi-Bidir mode*/ in tb_scan_port()
717 tb_switch_tmu_configure(sw, TB_SWITCH_TMU_RATE_HIFI, false); in tb_scan_port()
719 if (tb_enable_tmu(sw)) in tb_scan_port()
720 tb_sw_warn(sw, "failed to enable TMU\n"); in tb_scan_port()
731 if (tcm->hotplug_active && tb_tunnel_usb3(sw->tb, sw)) in tb_scan_port()
732 tb_sw_warn(sw, "USB3 tunnel creation failed\n"); in tb_scan_port()
734 tb_add_dp_resources(sw); in tb_scan_port()
735 tb_scan_switch(sw); in tb_scan_port()
747 list_del(&tunnel->list); in tb_deactivate_and_free_tunnel()
749 tb = tunnel->tb; in tb_deactivate_and_free_tunnel()
750 src_port = tunnel->src_port; in tb_deactivate_and_free_tunnel()
751 dst_port = tunnel->dst_port; in tb_deactivate_and_free_tunnel()
753 switch (tunnel->type) { in tb_deactivate_and_free_tunnel()
759 tb_switch_dealloc_dp_resource(src_port->sw, src_port); in tb_deactivate_and_free_tunnel()
761 pm_runtime_mark_last_busy(&dst_port->sw->dev); in tb_deactivate_and_free_tunnel()
762 pm_runtime_put_autosuspend(&dst_port->sw->dev); in tb_deactivate_and_free_tunnel()
763 pm_runtime_mark_last_busy(&src_port->sw->dev); in tb_deactivate_and_free_tunnel()
764 pm_runtime_put_autosuspend(&src_port->sw->dev); in tb_deactivate_and_free_tunnel()
783 * tb_free_invalid_tunnels() - destroy tunnels of devices that have gone away
791 list_for_each_entry_safe(tunnel, n, &tcm->tunnel_list, list) { in tb_free_invalid_tunnels()
798 * tb_free_unplugged_children() - traverse hierarchy and free unplugged switches
800 static void tb_free_unplugged_children(struct tb_switch *sw) in tb_free_unplugged_children() argument
804 tb_switch_for_each_port(sw, port) { in tb_free_unplugged_children()
808 if (port->remote->sw->is_unplugged) { in tb_free_unplugged_children()
810 tb_remove_dp_resources(port->remote->sw); in tb_free_unplugged_children()
811 tb_switch_unconfigure_link(port->remote->sw); in tb_free_unplugged_children()
812 tb_switch_lane_bonding_disable(port->remote->sw); in tb_free_unplugged_children()
813 tb_switch_remove(port->remote->sw); in tb_free_unplugged_children()
814 port->remote = NULL; in tb_free_unplugged_children()
815 if (port->dual_link_port) in tb_free_unplugged_children()
816 port->dual_link_port->remote = NULL; in tb_free_unplugged_children()
818 tb_free_unplugged_children(port->remote->sw); in tb_free_unplugged_children()
823 static struct tb_port *tb_find_pcie_down(struct tb_switch *sw, in tb_find_pcie_down() argument
832 if (tb_switch_is_usb4(sw)) { in tb_find_pcie_down()
833 down = usb4_switch_map_pcie_down(sw, port); in tb_find_pcie_down()
834 } else if (!tb_route(sw)) { in tb_find_pcie_down()
835 int phy_port = tb_phy_port_from_link(port->port); in tb_find_pcie_down()
839 * Hard-coded Thunderbolt port to PCIe down port mapping in tb_find_pcie_down()
842 if (tb_switch_is_cactus_ridge(sw) || in tb_find_pcie_down()
843 tb_switch_is_alpine_ridge(sw)) in tb_find_pcie_down()
845 else if (tb_switch_is_falcon_ridge(sw)) in tb_find_pcie_down()
847 else if (tb_switch_is_titan_ridge(sw)) in tb_find_pcie_down()
852 /* Validate the hard-coding */ in tb_find_pcie_down()
853 if (WARN_ON(index > sw->config.max_port_number)) in tb_find_pcie_down()
856 down = &sw->ports[index]; in tb_find_pcie_down()
869 return tb_find_unused_port(sw, TB_TYPE_PCIE_DOWN); in tb_find_pcie_down()
877 host_port = tb_route(in->sw) ? in tb_find_dp_out()
878 tb_port_at(tb_route(in->sw), tb->root_switch) : NULL; in tb_find_dp_out()
880 list_for_each_entry(port, &tcm->dp_resources, list) { in tb_find_dp_out()
885 tb_port_dbg(port, "in use\n"); in tb_find_dp_out()
895 if (host_port && tb_route(port->sw)) { in tb_find_dp_out()
898 p = tb_port_at(tb_route(port->sw), tb->root_switch); in tb_find_dp_out()
925 tb_dbg(tb, "looking for DP IN <-> DP OUT pairs:\n"); in tb_tunnel_dp()
929 list_for_each_entry(port, &tcm->dp_resources, list) { in tb_tunnel_dp()
934 tb_port_dbg(port, "in use\n"); in tb_tunnel_dp()
963 list_for_each_entry(tunnel, &tcm->tunnel_list, list) { in tb_tunnel_dp()
978 pm_runtime_get_sync(&in->sw->dev); in tb_tunnel_dp()
979 pm_runtime_get_sync(&out->sw->dev); in tb_tunnel_dp()
981 if (tb_switch_alloc_dp_resource(in->sw, in)) { in tb_tunnel_dp()
1013 list_add_tail(&tunnel->list, &tcm->tunnel_list); in tb_tunnel_dp()
1019 tb_switch_enable_tmu_1st_child(tb->root_switch, TB_SWITCH_TMU_RATE_HIFI); in tb_tunnel_dp()
1028 tb_switch_dealloc_dp_resource(in->sw, in); in tb_tunnel_dp()
1030 pm_runtime_mark_last_busy(&out->sw->dev); in tb_tunnel_dp()
1031 pm_runtime_put_autosuspend(&out->sw->dev); in tb_tunnel_dp()
1032 pm_runtime_mark_last_busy(&in->sw->dev); in tb_tunnel_dp()
1033 pm_runtime_put_autosuspend(&in->sw->dev); in tb_tunnel_dp()
1053 list_del_init(&port->list); in tb_dp_resource_unavailable()
1070 list_for_each_entry(p, &tcm->dp_resources, list) { in tb_dp_resource_available()
1077 list_add_tail(&port->list, &tcm->dp_resources); in tb_dp_resource_available()
1079 /* Look for suitable DP IN <-> DP OUT pairs now */ in tb_dp_resource_available()
1090 * will be re-established after resume based on plug events. in tb_disconnect_and_release_dp()
1092 list_for_each_entry_safe_reverse(tunnel, n, &tcm->tunnel_list, list) { in tb_disconnect_and_release_dp()
1097 while (!list_empty(&tcm->dp_resources)) { in tb_disconnect_and_release_dp()
1100 port = list_first_entry(&tcm->dp_resources, in tb_disconnect_and_release_dp()
1102 list_del_init(&port->list); in tb_disconnect_and_release_dp()
1106 static int tb_disconnect_pci(struct tb *tb, struct tb_switch *sw) in tb_disconnect_pci() argument
1111 up = tb_switch_find_port(sw, TB_TYPE_PCIE_UP); in tb_disconnect_pci()
1113 return -ENODEV; in tb_disconnect_pci()
1117 return -ENODEV; in tb_disconnect_pci()
1119 tb_switch_xhci_disconnect(sw); in tb_disconnect_pci()
1122 list_del(&tunnel->list); in tb_disconnect_pci()
1127 static int tb_tunnel_pci(struct tb *tb, struct tb_switch *sw) in tb_tunnel_pci() argument
1134 up = tb_switch_find_port(sw, TB_TYPE_PCIE_UP); in tb_tunnel_pci()
1142 parent_sw = tb_to_switch(sw->dev.parent); in tb_tunnel_pci()
1143 port = tb_port_at(tb_route(sw), parent_sw); in tb_tunnel_pci()
1150 return -ENOMEM; in tb_tunnel_pci()
1156 return -EIO; in tb_tunnel_pci()
1163 if (tb_switch_pcie_l1_enable(sw)) in tb_tunnel_pci()
1164 tb_sw_warn(sw, "failed to enable PCIe L1 for Titan Ridge\n"); in tb_tunnel_pci()
1166 if (tb_switch_xhci_connect(sw)) in tb_tunnel_pci()
1167 tb_sw_warn(sw, "failed to connect xHCI\n"); in tb_tunnel_pci()
1169 list_add_tail(&tunnel->list, &tcm->tunnel_list); in tb_tunnel_pci()
1180 struct tb_switch *sw; in tb_approve_xdomain_paths() local
1182 sw = tb_to_switch(xd->dev.parent); in tb_approve_xdomain_paths()
1183 dst_port = tb_port_at(xd->route, sw); in tb_approve_xdomain_paths()
1184 nhi_port = tb_switch_find_port(tb->root_switch, TB_TYPE_NHI); in tb_approve_xdomain_paths()
1186 mutex_lock(&tb->lock); in tb_approve_xdomain_paths()
1190 mutex_unlock(&tb->lock); in tb_approve_xdomain_paths()
1191 return -ENOMEM; in tb_approve_xdomain_paths()
1198 mutex_unlock(&tb->lock); in tb_approve_xdomain_paths()
1199 return -EIO; in tb_approve_xdomain_paths()
1202 list_add_tail(&tunnel->list, &tcm->tunnel_list); in tb_approve_xdomain_paths()
1203 mutex_unlock(&tb->lock); in tb_approve_xdomain_paths()
1214 struct tb_switch *sw; in __tb_disconnect_xdomain_paths() local
1216 sw = tb_to_switch(xd->dev.parent); in __tb_disconnect_xdomain_paths()
1217 dst_port = tb_port_at(xd->route, sw); in __tb_disconnect_xdomain_paths()
1218 nhi_port = tb_switch_find_port(tb->root_switch, TB_TYPE_NHI); in __tb_disconnect_xdomain_paths()
1220 list_for_each_entry_safe(tunnel, n, &tcm->tunnel_list, list) { in __tb_disconnect_xdomain_paths()
1223 if (tunnel->src_port != nhi_port || tunnel->dst_port != dst_port) in __tb_disconnect_xdomain_paths()
1236 if (!xd->is_unplugged) { in tb_disconnect_xdomain_paths()
1237 mutex_lock(&tb->lock); in tb_disconnect_xdomain_paths()
1241 mutex_unlock(&tb->lock); in tb_disconnect_xdomain_paths()
1249 * tb_handle_hotplug() - handle hotplug event
1251 * Executes on tb->wq.
1256 struct tb *tb = ev->tb; in tb_handle_hotplug()
1258 struct tb_switch *sw; in tb_handle_hotplug() local
1262 pm_runtime_get_sync(&tb->dev); in tb_handle_hotplug()
1264 mutex_lock(&tb->lock); in tb_handle_hotplug()
1265 if (!tcm->hotplug_active) in tb_handle_hotplug()
1268 sw = tb_switch_find_by_route(tb, ev->route); in tb_handle_hotplug()
1269 if (!sw) { in tb_handle_hotplug()
1272 ev->route, ev->port, ev->unplug); in tb_handle_hotplug()
1275 if (ev->port > sw->config.max_port_number) { in tb_handle_hotplug()
1278 ev->route, ev->port, ev->unplug); in tb_handle_hotplug()
1281 port = &sw->ports[ev->port]; in tb_handle_hotplug()
1284 ev->route, ev->port, ev->unplug); in tb_handle_hotplug()
1288 pm_runtime_get_sync(&sw->dev); in tb_handle_hotplug()
1290 if (ev->unplug) { in tb_handle_hotplug()
1295 tb_sw_set_unplugged(port->remote->sw); in tb_handle_hotplug()
1297 tb_remove_dp_resources(port->remote->sw); in tb_handle_hotplug()
1298 tb_switch_tmu_disable(port->remote->sw); in tb_handle_hotplug()
1299 tb_switch_unconfigure_link(port->remote->sw); in tb_handle_hotplug()
1300 tb_switch_lane_bonding_disable(port->remote->sw); in tb_handle_hotplug()
1301 tb_switch_remove(port->remote->sw); in tb_handle_hotplug()
1302 port->remote = NULL; in tb_handle_hotplug()
1303 if (port->dual_link_port) in tb_handle_hotplug()
1304 port->dual_link_port->remote = NULL; in tb_handle_hotplug()
1307 } else if (port->xdomain) { in tb_handle_hotplug()
1308 struct tb_xdomain *xd = tb_xdomain_get(port->xdomain); in tb_handle_hotplug()
1318 xd->is_unplugged = true; in tb_handle_hotplug()
1320 port->xdomain = NULL; in tb_handle_hotplug()
1321 __tb_disconnect_xdomain_paths(tb, xd, -1, -1, -1, -1); in tb_handle_hotplug()
1326 } else if (!port->port) { in tb_handle_hotplug()
1327 tb_sw_dbg(sw, "xHCI disconnect request\n"); in tb_handle_hotplug()
1328 tb_switch_xhci_disconnect(sw); in tb_handle_hotplug()
1333 } else if (port->remote) { in tb_handle_hotplug()
1335 } else if (!port->port && sw->authorized) { in tb_handle_hotplug()
1336 tb_sw_dbg(sw, "xHCI connect request\n"); in tb_handle_hotplug()
1337 tb_switch_xhci_connect(sw); in tb_handle_hotplug()
1342 if (!port->remote) in tb_handle_hotplug()
1349 pm_runtime_mark_last_busy(&sw->dev); in tb_handle_hotplug()
1350 pm_runtime_put_autosuspend(&sw->dev); in tb_handle_hotplug()
1353 tb_switch_put(sw); in tb_handle_hotplug()
1355 mutex_unlock(&tb->lock); in tb_handle_hotplug()
1357 pm_runtime_mark_last_busy(&tb->dev); in tb_handle_hotplug()
1358 pm_runtime_put_autosuspend(&tb->dev); in tb_handle_hotplug()
1364 * tb_schedule_hotplug_handler() - callback function for the control channel
1379 route = tb_cfg_get_route(&pkg->header); in tb_handle_event()
1381 if (tb_cfg_ack_plug(tb->ctl, route, pkg->port, pkg->unplug)) { in tb_handle_event()
1383 pkg->port); in tb_handle_event()
1386 tb_queue_hotplug(tb, route, pkg->port, pkg->unplug); in tb_handle_event()
1395 cancel_delayed_work(&tcm->remove_work); in tb_stop()
1397 list_for_each_entry_safe(tunnel, n, &tcm->tunnel_list, list) { in tb_stop()
1407 tb_switch_remove(tb->root_switch); in tb_stop()
1408 tcm->hotplug_active = false; /* signal tb_handle_hotplug to quit */ in tb_stop()
1414 struct tb_switch *sw = tb_to_switch(dev); in tb_scan_finalize_switch() local
1421 if (sw->boot) in tb_scan_finalize_switch()
1422 sw->authorized = 1; in tb_scan_finalize_switch()
1425 kobject_uevent(&dev->kobj, KOBJ_ADD); in tb_scan_finalize_switch()
1437 tb->root_switch = tb_switch_alloc(tb, &tb->dev, 0); in tb_start()
1438 if (IS_ERR(tb->root_switch)) in tb_start()
1439 return PTR_ERR(tb->root_switch); in tb_start()
1449 tb->root_switch->no_nvm_upgrade = !tb_switch_is_usb4(tb->root_switch); in tb_start()
1450 /* All USB4 routers support runtime PM */ in tb_start()
1451 tb->root_switch->rpm = tb_switch_is_usb4(tb->root_switch); in tb_start()
1453 ret = tb_switch_configure(tb->root_switch); in tb_start()
1455 tb_switch_put(tb->root_switch); in tb_start()
1460 ret = tb_switch_add(tb->root_switch); in tb_start()
1462 tb_switch_put(tb->root_switch); in tb_start()
1470 tb_switch_tmu_configure(tb->root_switch, TB_SWITCH_TMU_RATE_NORMAL, in tb_start()
1473 tb_switch_tmu_enable(tb->root_switch); in tb_start()
1475 tb_scan_switch(tb->root_switch); in tb_start()
1484 tb_create_usb3_tunnels(tb->root_switch); in tb_start()
1486 tb_add_dp_resources(tb->root_switch); in tb_start()
1488 device_for_each_child(&tb->root_switch->dev, NULL, in tb_start()
1492 tcm->hotplug_active = true; in tb_start()
1502 tb_switch_suspend(tb->root_switch, false); in tb_suspend_noirq()
1503 tcm->hotplug_active = false; /* signal tb_handle_hotplug to quit */ in tb_suspend_noirq()
1509 static void tb_restore_children(struct tb_switch *sw) in tb_restore_children() argument
1515 if (sw->is_unplugged) in tb_restore_children()
1520 * Silently ignore CLx re-enabling in case CLx is not supported. in tb_restore_children()
1522 ret = tb_switch_enable_clx(sw, TB_CL1); in tb_restore_children()
1523 if (ret && ret != -EOPNOTSUPP) in tb_restore_children()
1524 tb_sw_warn(sw, "failed to re-enable %s on upstream port\n", in tb_restore_children()
1527 if (tb_switch_is_clx_enabled(sw, TB_CL1)) in tb_restore_children()
1530 * Normal-Uni mode. in tb_restore_children()
1532 tb_switch_tmu_configure(sw, TB_SWITCH_TMU_RATE_NORMAL, true); in tb_restore_children()
1534 /* If CLx disabled, configure router's TMU to HiFi-Bidir mode*/ in tb_restore_children()
1535 tb_switch_tmu_configure(sw, TB_SWITCH_TMU_RATE_HIFI, false); in tb_restore_children()
1537 if (tb_enable_tmu(sw)) in tb_restore_children()
1538 tb_sw_warn(sw, "failed to restore TMU configuration\n"); in tb_restore_children()
1540 tb_switch_for_each_port(sw, port) { in tb_restore_children()
1541 if (!tb_port_has_remote(port) && !port->xdomain) in tb_restore_children()
1544 if (port->remote) { in tb_restore_children()
1545 tb_switch_lane_bonding_enable(port->remote->sw); in tb_restore_children()
1546 tb_switch_configure_link(port->remote->sw); in tb_restore_children()
1548 tb_restore_children(port->remote->sw); in tb_restore_children()
1549 } else if (port->xdomain) { in tb_restore_children()
1550 tb_port_configure_xdomain(port, port->xdomain); in tb_restore_children()
1565 tb_switch_reset(tb->root_switch); in tb_resume_noirq()
1567 tb_switch_resume(tb->root_switch); in tb_resume_noirq()
1569 tb_free_unplugged_children(tb->root_switch); in tb_resume_noirq()
1570 tb_restore_children(tb->root_switch); in tb_resume_noirq()
1578 tb_switch_discover_tunnels(tb->root_switch, &tunnels, false); in tb_resume_noirq()
1586 /* Re-create our tunnels now */ in tb_resume_noirq()
1587 list_for_each_entry_safe(tunnel, n, &tcm->tunnel_list, list) { in tb_resume_noirq()
1588 /* USB3 requires delay before it can be re-activated */ in tb_resume_noirq()
1596 if (!list_empty(&tcm->tunnel_list)) { in tb_resume_noirq()
1605 tcm->hotplug_active = true; in tb_resume_noirq()
1611 static int tb_free_unplugged_xdomains(struct tb_switch *sw) in tb_free_unplugged_xdomains() argument
1616 tb_switch_for_each_port(sw, port) { in tb_free_unplugged_xdomains()
1619 if (port->xdomain && port->xdomain->is_unplugged) { in tb_free_unplugged_xdomains()
1621 tb_xdomain_remove(port->xdomain); in tb_free_unplugged_xdomains()
1623 port->xdomain = NULL; in tb_free_unplugged_xdomains()
1625 } else if (port->remote) { in tb_free_unplugged_xdomains()
1626 ret += tb_free_unplugged_xdomains(port->remote->sw); in tb_free_unplugged_xdomains()
1637 tcm->hotplug_active = false; in tb_freeze_noirq()
1645 tcm->hotplug_active = true; in tb_thaw_noirq()
1656 mutex_lock(&tb->lock); in tb_complete()
1657 if (tb_free_unplugged_xdomains(tb->root_switch)) in tb_complete()
1658 tb_scan_switch(tb->root_switch); in tb_complete()
1659 mutex_unlock(&tb->lock); in tb_complete()
1666 mutex_lock(&tb->lock); in tb_runtime_suspend()
1667 tb_switch_suspend(tb->root_switch, true); in tb_runtime_suspend()
1668 tcm->hotplug_active = false; in tb_runtime_suspend()
1669 mutex_unlock(&tb->lock); in tb_runtime_suspend()
1679 mutex_lock(&tb->lock); in tb_remove_work()
1680 if (tb->root_switch) { in tb_remove_work()
1681 tb_free_unplugged_children(tb->root_switch); in tb_remove_work()
1682 tb_free_unplugged_xdomains(tb->root_switch); in tb_remove_work()
1684 mutex_unlock(&tb->lock); in tb_remove_work()
1692 mutex_lock(&tb->lock); in tb_runtime_resume()
1693 tb_switch_resume(tb->root_switch); in tb_runtime_resume()
1695 tb_restore_children(tb->root_switch); in tb_runtime_resume()
1696 list_for_each_entry_safe(tunnel, n, &tcm->tunnel_list, list) in tb_runtime_resume()
1698 tcm->hotplug_active = true; in tb_runtime_resume()
1699 mutex_unlock(&tb->lock); in tb_runtime_resume()
1706 queue_delayed_work(tb->wq, &tcm->remove_work, msecs_to_jiffies(50)); in tb_runtime_resume()
1741 switch (nhi->pdev->device) { in tb_apple_add_links()
1751 upstream = pci_upstream_bridge(nhi->pdev); in tb_apple_add_links()
1765 * back to NHI so that PCIe tunnels can be re-established after in tb_apple_add_links()
1768 for_each_pci_bridge(pdev, upstream->subordinate) { in tb_apple_add_links()
1774 !pdev->is_hotplug_bridge) in tb_apple_add_links()
1777 link = device_link_add(&pdev->dev, &nhi->pdev->dev, in tb_apple_add_links()
1781 dev_dbg(&nhi->pdev->dev, "created link from %s\n", in tb_apple_add_links()
1782 dev_name(&pdev->dev)); in tb_apple_add_links()
1784 dev_warn(&nhi->pdev->dev, "device link creation from %s failed\n", in tb_apple_add_links()
1785 dev_name(&pdev->dev)); in tb_apple_add_links()
1800 tb->security_level = TB_SECURITY_USER; in tb_probe()
1802 tb->security_level = TB_SECURITY_NOPCIE; in tb_probe()
1804 tb->cm_ops = &tb_cm_ops; in tb_probe()
1807 INIT_LIST_HEAD(&tcm->tunnel_list); in tb_probe()
1808 INIT_LIST_HEAD(&tcm->dp_resources); in tb_probe()
1809 INIT_DELAYED_WORK(&tcm->remove_work, tb_remove_work); in tb_probe()