Lines Matching +full:dma +full:- +full:router
1 // SPDX-License-Identifier: GPL-2.0
3 * Thunderbolt driver - bus logic (NHI independent)
23 * direction. This is 40G - 10% guard band bandwidth.
46 * struct tb_cm - Simple Thunderbolt connection manager
67 return ((void *)tcm - sizeof(struct tb)); in tcm_to_tb()
82 for (i = 0; i < ARRAY_SIZE(tcm->groups); i++) { in tb_init_bandwidth_groups()
83 struct tb_bandwidth_group *group = &tcm->groups[i]; in tb_init_bandwidth_groups()
85 group->tb = tcm_to_tb(tcm); in tb_init_bandwidth_groups()
86 group->index = i + 1; in tb_init_bandwidth_groups()
87 INIT_LIST_HEAD(&group->ports); in tb_init_bandwidth_groups()
94 if (!group || WARN_ON(in->group)) in tb_bandwidth_group_attach_port()
97 in->group = group; in tb_bandwidth_group_attach_port()
98 list_add_tail(&in->group_list, &group->ports); in tb_bandwidth_group_attach_port()
100 tb_port_dbg(in, "attached to bandwidth group %d\n", group->index); in tb_bandwidth_group_attach_port()
107 for (i = 0; i < ARRAY_SIZE(tcm->groups); i++) { in tb_find_free_bandwidth_group()
108 struct tb_bandwidth_group *group = &tcm->groups[i]; in tb_find_free_bandwidth_group()
110 if (list_empty(&group->ports)) in tb_find_free_bandwidth_group()
130 list_for_each_entry(tunnel, &tcm->tunnel_list, list) { in tb_attach_bandwidth_group()
134 if (tunnel->src_port->sw == in->sw && in tb_attach_bandwidth_group()
135 tunnel->dst_port->sw == out->sw) { in tb_attach_bandwidth_group()
136 group = tunnel->src_port->group; in tb_attach_bandwidth_group()
161 for (i = 0; i < ARRAY_SIZE(tcm->groups); i++) { in tb_discover_bandwidth_group()
162 if (tcm->groups[i].index == index) { in tb_discover_bandwidth_group()
163 tb_bandwidth_group_attach_port(&tcm->groups[i], in); in tb_discover_bandwidth_group()
174 struct tb_bandwidth_group *group = in->group; in tb_detach_bandwidth_group()
177 in->group = NULL; in tb_detach_bandwidth_group()
178 list_del_init(&in->group_list); in tb_detach_bandwidth_group()
180 tb_port_dbg(in, "detached from bandwidth group %d\n", group->index); in tb_detach_bandwidth_group()
194 ev->tb = tb; in tb_queue_hotplug()
195 ev->route = route; in tb_queue_hotplug()
196 ev->port = port; in tb_queue_hotplug()
197 ev->unplug = unplug; in tb_queue_hotplug()
198 INIT_WORK(&ev->work, tb_handle_hotplug); in tb_queue_hotplug()
199 queue_work(tb->wq, &ev->work); in tb_queue_hotplug()
206 struct tb_cm *tcm = tb_priv(sw->tb); in tb_add_dp_resources()
216 list_add_tail(&port->list, &tcm->dp_resources); in tb_add_dp_resources()
223 struct tb_cm *tcm = tb_priv(sw->tb); in tb_remove_dp_resources()
229 tb_remove_dp_resources(port->remote->sw); in tb_remove_dp_resources()
232 list_for_each_entry_safe(port, tmp, &tcm->dp_resources, list) { in tb_remove_dp_resources()
233 if (port->sw == sw) { in tb_remove_dp_resources()
235 list_del_init(&port->list); in tb_remove_dp_resources()
245 list_for_each_entry(p, &tcm->dp_resources, list) { in tb_discover_dp_resource()
252 list_add_tail(&port->list, &tcm->dp_resources); in tb_discover_dp_resource()
260 list_for_each_entry(tunnel, &tcm->tunnel_list, list) { in tb_discover_dp_resources()
262 tb_discover_dp_resource(tb, tunnel->dst_port); in tb_discover_dp_resources()
266 /* Enables CL states up to host router */
269 struct tb_cm *tcm = tb_priv(sw->tb); in tb_enable_clx()
291 * If we are re-enabling then check if there is an active DMA in tb_enable_clx()
294 list_for_each_entry(tunnel, &tcm->tunnel_list, list) { in tb_enable_clx()
306 if (ret == -EOPNOTSUPP) in tb_enable_clx()
308 return ret == -EOPNOTSUPP ? 0 : ret; in tb_enable_clx()
312 * tb_disable_clx() - Disable CL states up to host router
313 * @sw: Router to start
315 * Disables CL states from @sw up to the host router. Returns true if
375 * accuracy of first depth child routers (and the host router) in tb_increase_tmu_accuracy()
382 sw = tunnel->tb->root_switch; in tb_increase_tmu_accuracy()
383 device_for_each_child(&sw->dev, NULL, tb_increase_switch_tmu_accuracy); in tb_increase_tmu_accuracy()
400 return device_for_each_child(&tb->dev, NULL, in tb_tmu_hifi_uni_required()
410 * enable the enhanched uni-directional mode. That covers all in tb_enable_tmu()
417 if (ret == -EOPNOTSUPP) { in tb_enable_tmu()
420 * Figure out uni-directional HiFi TMU requirements in tb_enable_tmu()
422 * uni-directional HiFi requirements we can put the TMU in tb_enable_tmu()
425 * Deliberately skip bi-directional HiFi links in tb_enable_tmu()
429 if (tb_tmu_hifi_uni_required(sw->tb)) in tb_enable_tmu()
439 /* If not supported, fallback to bi-directional HiFi */ in tb_enable_tmu()
440 if (ret == -EOPNOTSUPP) in tb_enable_tmu()
465 struct tb *tb = sw->tb; in tb_switch_discover_tunnels()
471 switch (port->config.type) { in tb_switch_discover_tunnels()
490 list_add_tail(&tunnel->list, list); in tb_switch_discover_tunnels()
495 tb_switch_discover_tunnels(port->remote->sw, list, in tb_switch_discover_tunnels()
506 tb_switch_discover_tunnels(tb->root_switch, &tcm->tunnel_list, true); in tb_discover_tunnels()
508 list_for_each_entry(tunnel, &tcm->tunnel_list, list) { in tb_discover_tunnels()
510 struct tb_switch *parent = tunnel->dst_port->sw; in tb_discover_tunnels()
512 while (parent != tunnel->src_port->sw) { in tb_discover_tunnels()
513 parent->boot = true; in tb_discover_tunnels()
517 struct tb_port *in = tunnel->src_port; in tb_discover_tunnels()
518 struct tb_port *out = tunnel->dst_port; in tb_discover_tunnels()
521 pm_runtime_get_sync(&in->sw->dev); in tb_discover_tunnels()
522 pm_runtime_get_sync(&out->sw->dev); in tb_discover_tunnels()
531 if (tb_switch_is_usb4(port->sw)) in tb_port_configure_xdomain()
538 if (tb_switch_is_usb4(port->sw)) in tb_port_unconfigure_xdomain()
543 tb_port_enable(port->dual_link_port); in tb_port_unconfigure_xdomain()
548 struct tb_switch *sw = port->sw; in tb_scan_xdomain()
549 struct tb *tb = sw->tb; in tb_scan_xdomain()
563 xd = tb_xdomain_alloc(tb, &sw->dev, route, tb->root_switch->uuid, in tb_scan_xdomain()
566 tb_port_at(route, sw)->xdomain = xd; in tb_scan_xdomain()
573 * tb_find_unused_port() - return the first inactive port on @sw
585 if (port->config.type != type) in tb_find_unused_port()
587 if (!port->cap_adap) in tb_find_unused_port()
614 list_for_each_entry(tunnel, &tcm->tunnel_list, list) { in tb_find_tunnel()
615 if (tunnel->type == type && in tb_find_tunnel()
616 ((src_port && src_port == tunnel->src_port) || in tb_find_tunnel()
617 (dst_port && dst_port == tunnel->dst_port))) { in tb_find_tunnel()
632 /* Pick the router that is deepest in the topology */ in tb_find_first_usb3_tunnel()
634 sw = dst_port->sw; in tb_find_first_usb3_tunnel()
636 sw = src_port->sw; in tb_find_first_usb3_tunnel()
638 /* Can't be the host router */ in tb_find_first_usb3_tunnel()
639 if (sw == tb->root_switch) in tb_find_first_usb3_tunnel()
642 /* Find the downstream USB4 port that leads to this router */ in tb_find_first_usb3_tunnel()
643 port = tb_port_at(tb_route(sw), tb->root_switch); in tb_find_first_usb3_tunnel()
644 /* Find the corresponding host router USB3 downstream port */ in tb_find_first_usb3_tunnel()
645 usb3_down = usb4_switch_map_usb3_down(tb->root_switch, port); in tb_find_first_usb3_tunnel()
653 * tb_consumed_usb3_pcie_bandwidth() - Consumed USB3/PCIe bandwidth over a single link
678 if (tunnel && tunnel->src_port != src_port && in tb_consumed_usb3_pcie_bandwidth()
679 tunnel->dst_port != dst_port) { in tb_consumed_usb3_pcie_bandwidth()
701 * tb_consumed_dp_bandwidth() - Consumed DP bandwidth over a single link
730 list_for_each_entry(tunnel, &tcm->tunnel_list, list) { in tb_consumed_dp_bandwidth()
745 * re-calculating estimated bandwidth. in tb_consumed_dp_bandwidth()
747 if (tunnel->src_port == src_port && in tb_consumed_dp_bandwidth()
748 tunnel->dst_port == dst_port) in tb_consumed_dp_bandwidth()
778 * tb_maximum_banwidth() - Maximum bandwidth over a single link
808 link_speed = port->sw->link_speed; in tb_maximum_bandwidth()
810 * sw->link_width is from upstream perspective so we use in tb_maximum_bandwidth()
811 * the opposite for downstream of the host router. in tb_maximum_bandwidth()
813 if (port->sw->link_width == TB_LINK_WIDTH_ASYM_TX) { in tb_maximum_bandwidth()
816 } else if (port->sw->link_width == TB_LINK_WIDTH_ASYM_RX) { in tb_maximum_bandwidth()
834 up_bw = link_speed * port->sw->link_width * 1000; in tb_maximum_bandwidth()
873 *max_up = up_bw - up_bw / 10; in tb_maximum_bandwidth()
874 *max_down = down_bw - down_bw / 10; in tb_maximum_bandwidth()
881 * tb_available_bandwidth() - Available bandwidth for tunneling
926 max_up -= consumed_up; in tb_available_bandwidth()
927 max_down -= consumed_down; in tb_available_bandwidth()
933 max_up -= consumed_up; in tb_available_bandwidth()
934 max_down -= consumed_down; in tb_available_bandwidth()
976 ret = tb_available_bandwidth(tb, tunnel->src_port, tunnel->dst_port, in tb_reclaim_usb3_bandwidth()
1006 if (!sw->link_usb4) in tb_tunnel_usb3()
1046 ret = -ENOMEM; in tb_tunnel_usb3()
1053 ret = -EIO; in tb_tunnel_usb3()
1057 list_add_tail(&tunnel->list, &tcm->tunnel_list); in tb_tunnel_usb3()
1081 ret = tb_tunnel_usb3(sw->tb, sw); in tb_create_usb3_tunnels()
1089 ret = tb_create_usb3_tunnels(port->remote->sw); in tb_create_usb3_tunnels()
1098 * tb_configure_asym() - Transition links to asymmetric if needed
1126 /* Pick up router deepest in the hierarchy */ in tb_configure_asym()
1128 sw = dst_port->sw; in tb_configure_asym()
1130 sw = src_port->sw; in tb_configure_asym()
1146 * (40G - guard band 10%), and the requested is above in tb_configure_asym()
1150 ret = -ENOBUFS; in tb_configure_asym()
1161 ret = -ENOBUFS; in tb_configure_asym()
1170 if (up->sw->link_width == width) in tb_configure_asym()
1176 tb_sw_dbg(up->sw, "configuring asymmetric link\n"); in tb_configure_asym()
1182 ret = tb_switch_set_link_width(up->sw, width); in tb_configure_asym()
1184 tb_sw_warn(up->sw, "failed to set link width\n"); in tb_configure_asym()
1189 /* Re-enable CL states if they were previosly enabled */ in tb_configure_asym()
1197 * tb_configure_sym() - Transition links to symmetric if possible
1222 /* Pick up router deepest in the hierarchy */ in tb_configure_sym()
1224 sw = dst_port->sw; in tb_configure_sym()
1226 sw = src_port->sw; in tb_configure_sym()
1234 if (up->sw->link_width <= TB_LINK_WIDTH_DUAL) in tb_configure_sym()
1237 if (up->sw->is_unplugged) in tb_configure_sym()
1259 if (up->sw->link_width == TB_LINK_WIDTH_DUAL) in tb_configure_sym()
1262 tb_sw_dbg(up->sw, "configuring symmetric link\n"); in tb_configure_sym()
1264 ret = tb_switch_set_link_width(up->sw, TB_LINK_WIDTH_DUAL); in tb_configure_sym()
1266 tb_sw_warn(up->sw, "failed to set link width\n"); in tb_configure_sym()
1271 /* Re-enable CL states if they were previosly enabled */ in tb_configure_sym()
1281 struct tb *tb = sw->tb; in tb_configure_link()
1284 down->remote = up; in tb_configure_link()
1285 up->remote = down; in tb_configure_link()
1286 if (down->dual_link_port && up->dual_link_port) { in tb_configure_link()
1287 down->dual_link_port->remote = up->dual_link_port; in tb_configure_link()
1288 up->dual_link_port->remote = down->dual_link_port; in tb_configure_link()
1295 if (sw->link_width < TB_LINK_WIDTH_DUAL) in tb_configure_link()
1299 * Device router that comes up as symmetric link is in tb_configure_link()
1305 up->sw->link_width == TB_LINK_WIDTH_DUAL) { in tb_configure_link()
1308 host_port = tb_port_at(tb_route(sw), tb->root_switch); in tb_configure_link()
1319 * tb_scan_switch() - scan for and initialize downstream switches
1325 pm_runtime_get_sync(&sw->dev); in tb_scan_switch()
1330 pm_runtime_mark_last_busy(&sw->dev); in tb_scan_switch()
1331 pm_runtime_put_autosuspend(&sw->dev); in tb_scan_switch()
1335 * tb_scan_port() - check for and initialize switches below port
1339 struct tb_cm *tcm = tb_priv(port->sw->tb); in tb_scan_port()
1350 tb_queue_hotplug(port->sw->tb, tb_route(port->sw), port->port, in tb_scan_port()
1355 if (port->config.type != TB_TYPE_PORT) in tb_scan_port()
1357 if (port->dual_link_port && port->link_nr) in tb_scan_port()
1363 if (port->usb4) in tb_scan_port()
1364 pm_runtime_get_sync(&port->usb4->dev); in tb_scan_port()
1368 if (port->remote) { in tb_scan_port()
1375 sw = tb_switch_alloc(port->sw->tb, &port->sw->dev, in tb_scan_port()
1383 if (PTR_ERR(sw) == -EIO || PTR_ERR(sw) == -EADDRNOTAVAIL) in tb_scan_port()
1397 if (port->xdomain) { in tb_scan_port()
1398 tb_xdomain_remove(port->xdomain); in tb_scan_port()
1400 port->xdomain = NULL; in tb_scan_port()
1408 if (!tcm->hotplug_active) { in tb_scan_port()
1409 dev_set_uevent_suppress(&sw->dev, true); in tb_scan_port()
1417 sw->rpm = sw->generation > 1; in tb_scan_port()
1441 * enabled for the upstream port of the router so we do it here. in tb_scan_port()
1454 if (tcm->hotplug_active && tb_tunnel_usb3(sw->tb, sw)) in tb_scan_port()
1461 if (port->usb4) { in tb_scan_port()
1462 pm_runtime_mark_last_busy(&port->usb4->dev); in tb_scan_port()
1463 pm_runtime_put_autosuspend(&port->usb4->dev); in tb_scan_port()
1476 list_del(&tunnel->list); in tb_deactivate_and_free_tunnel()
1478 tb = tunnel->tb; in tb_deactivate_and_free_tunnel()
1479 src_port = tunnel->src_port; in tb_deactivate_and_free_tunnel()
1480 dst_port = tunnel->dst_port; in tb_deactivate_and_free_tunnel()
1482 switch (tunnel->type) { in tb_deactivate_and_free_tunnel()
1489 tb_switch_dealloc_dp_resource(src_port->sw, src_port); in tb_deactivate_and_free_tunnel()
1496 pm_runtime_mark_last_busy(&dst_port->sw->dev); in tb_deactivate_and_free_tunnel()
1497 pm_runtime_put_autosuspend(&dst_port->sw->dev); in tb_deactivate_and_free_tunnel()
1498 pm_runtime_mark_last_busy(&src_port->sw->dev); in tb_deactivate_and_free_tunnel()
1499 pm_runtime_put_autosuspend(&src_port->sw->dev); in tb_deactivate_and_free_tunnel()
1508 * PCIe and DMA tunnels do not consume guaranteed in tb_deactivate_and_free_tunnel()
1518 * tb_free_invalid_tunnels() - destroy tunnels of devices that have gone away
1526 list_for_each_entry_safe(tunnel, n, &tcm->tunnel_list, list) { in tb_free_invalid_tunnels()
1533 * tb_free_unplugged_children() - traverse hierarchy and free unplugged switches
1543 if (port->remote->sw->is_unplugged) { in tb_free_unplugged_children()
1545 tb_remove_dp_resources(port->remote->sw); in tb_free_unplugged_children()
1546 tb_switch_unconfigure_link(port->remote->sw); in tb_free_unplugged_children()
1547 tb_switch_set_link_width(port->remote->sw, in tb_free_unplugged_children()
1549 tb_switch_remove(port->remote->sw); in tb_free_unplugged_children()
1550 port->remote = NULL; in tb_free_unplugged_children()
1551 if (port->dual_link_port) in tb_free_unplugged_children()
1552 port->dual_link_port->remote = NULL; in tb_free_unplugged_children()
1554 tb_free_unplugged_children(port->remote->sw); in tb_free_unplugged_children()
1571 int phy_port = tb_phy_port_from_link(port->port); in tb_find_pcie_down()
1575 * Hard-coded Thunderbolt port to PCIe down port mapping in tb_find_pcie_down()
1588 /* Validate the hard-coding */ in tb_find_pcie_down()
1589 if (WARN_ON(index > sw->config.max_port_number)) in tb_find_pcie_down()
1592 down = &sw->ports[index]; in tb_find_pcie_down()
1612 struct tb *tb = group->tb; in tb_recalc_estimated_bandwidth_for_group()
1616 tb_dbg(tb, "re-calculating bandwidth estimation for group %u\n", in tb_recalc_estimated_bandwidth_for_group()
1617 group->index); in tb_recalc_estimated_bandwidth_for_group()
1620 list_for_each_entry(in, &group->ports, group_list) { in tb_recalc_estimated_bandwidth_for_group()
1635 * tunnels under the host router USB4 port, even in tb_recalc_estimated_bandwidth_for_group()
1636 * if they do not begin from the host router, we in tb_recalc_estimated_bandwidth_for_group()
1642 first_tunnel->src_port, first_tunnel->dst_port); in tb_recalc_estimated_bandwidth_for_group()
1650 out = tunnel->dst_port; in tb_recalc_estimated_bandwidth_for_group()
1655 "failed to re-calculate estimated bandwidth\n"); in tb_recalc_estimated_bandwidth_for_group()
1661 * - already allocated bandwidth for the DP tunnel in tb_recalc_estimated_bandwidth_for_group()
1662 * - available bandwidth along the path in tb_recalc_estimated_bandwidth_for_group()
1663 * - bandwidth allocated for USB 3.x but not used. in tb_recalc_estimated_bandwidth_for_group()
1665 tb_port_dbg(in, "re-calculated estimated bandwidth %u/%u Mb/s\n", in tb_recalc_estimated_bandwidth_for_group()
1678 tb_reclaim_usb3_bandwidth(tb, first_tunnel->src_port, in tb_recalc_estimated_bandwidth_for_group()
1679 first_tunnel->dst_port); in tb_recalc_estimated_bandwidth_for_group()
1681 tb_dbg(tb, "bandwidth estimation for group %u done\n", group->index); in tb_recalc_estimated_bandwidth_for_group()
1689 tb_dbg(tb, "bandwidth consumption changed, re-calculating estimated bandwidth\n"); in tb_recalc_estimated_bandwidth()
1691 for (i = 0; i < ARRAY_SIZE(tcm->groups); i++) { in tb_recalc_estimated_bandwidth()
1692 struct tb_bandwidth_group *group = &tcm->groups[i]; in tb_recalc_estimated_bandwidth()
1694 if (!list_empty(&group->ports)) in tb_recalc_estimated_bandwidth()
1698 tb_dbg(tb, "bandwidth re-calculation done\n"); in tb_recalc_estimated_bandwidth()
1706 host_port = tb_route(in->sw) ? in tb_find_dp_out()
1707 tb_port_at(tb_route(in->sw), tb->root_switch) : NULL; in tb_find_dp_out()
1709 list_for_each_entry(port, &tcm->dp_resources, list) { in tb_find_dp_out()
1722 * the same host router downstream port. in tb_find_dp_out()
1724 if (host_port && tb_route(port->sw)) { in tb_find_dp_out()
1727 p = tb_port_at(tb_route(port->sw), tb->root_switch); in tb_find_dp_out()
1753 list_for_each_entry(tunnel, &tcm->tunnel_list, list) { in tb_tunnel_one_dp()
1768 pm_runtime_get_sync(&in->sw->dev); in tb_tunnel_one_dp()
1769 pm_runtime_get_sync(&out->sw->dev); in tb_tunnel_one_dp()
1771 if (tb_switch_alloc_dp_resource(in->sw, in)) { in tb_tunnel_one_dp()
1811 list_add_tail(&tunnel->list, &tcm->tunnel_list); in tb_tunnel_one_dp()
1824 * In case of DP tunnel exists, change host router's 1st children in tb_tunnel_one_dp()
1839 tb_switch_dealloc_dp_resource(in->sw, in); in tb_tunnel_one_dp()
1841 pm_runtime_mark_last_busy(&out->sw->dev); in tb_tunnel_one_dp()
1842 pm_runtime_put_autosuspend(&out->sw->dev); in tb_tunnel_one_dp()
1843 pm_runtime_mark_last_busy(&in->sw->dev); in tb_tunnel_one_dp()
1844 pm_runtime_put_autosuspend(&in->sw->dev); in tb_tunnel_one_dp()
1863 tb_dbg(tb, "looking for DP IN <-> DP OUT pairs:\n"); in tb_tunnel_dp()
1867 list_for_each_entry(port, &tcm->dp_resources, list) { in tb_tunnel_dp()
1892 struct tb_switch *sw = port->sw; in tb_enter_redrive()
1894 if (!(sw->quirks & QUIRK_KEEP_POWER_IN_DP_REDRIVE)) in tb_enter_redrive()
1898 * If we get hot-unplug for the DP IN port of the host router in tb_enter_redrive()
1900 * is a monitor connected directly to the Type-C port and we are in tb_enter_redrive()
1909 port->redrive = true; in tb_enter_redrive()
1910 pm_runtime_get(&sw->dev); in tb_enter_redrive()
1917 struct tb_switch *sw = port->sw; in tb_exit_redrive()
1919 if (!(sw->quirks & QUIRK_KEEP_POWER_IN_DP_REDRIVE)) in tb_exit_redrive()
1926 if (port->redrive && tb_switch_query_dp_resource(sw, port)) { in tb_exit_redrive()
1927 port->redrive = false; in tb_exit_redrive()
1928 pm_runtime_put(&sw->dev); in tb_exit_redrive()
1949 if (!(sw->quirks & QUIRK_KEEP_POWER_IN_DP_REDRIVE)) in tb_switch_exit_redrive()
1956 if (port->redrive) { in tb_switch_exit_redrive()
1957 port->redrive = false; in tb_switch_exit_redrive()
1958 pm_runtime_put(&sw->dev); in tb_switch_exit_redrive()
1984 list_del_init(&port->list); in tb_dp_resource_unavailable()
2002 list_for_each_entry(p, &tcm->dp_resources, list) { in tb_dp_resource_available()
2009 list_add_tail(&port->list, &tcm->dp_resources); in tb_dp_resource_available()
2012 /* Look for suitable DP IN <-> DP OUT pairs now */ in tb_dp_resource_available()
2023 * will be re-established after resume based on plug events. in tb_disconnect_and_release_dp()
2025 list_for_each_entry_safe_reverse(tunnel, n, &tcm->tunnel_list, list) { in tb_disconnect_and_release_dp()
2030 while (!list_empty(&tcm->dp_resources)) { in tb_disconnect_and_release_dp()
2033 port = list_first_entry(&tcm->dp_resources, in tb_disconnect_and_release_dp()
2035 list_del_init(&port->list); in tb_disconnect_and_release_dp()
2046 return -ENODEV; in tb_disconnect_pci()
2050 return -ENODEV; in tb_disconnect_pci()
2055 list_del(&tunnel->list); in tb_disconnect_pci()
2081 return -ENOMEM; in tb_tunnel_pci()
2087 return -EIO; in tb_tunnel_pci()
2100 list_add_tail(&tunnel->list, &tcm->tunnel_list); in tb_tunnel_pci()
2114 sw = tb_to_switch(xd->dev.parent); in tb_approve_xdomain_paths()
2115 dst_port = tb_port_at(xd->route, sw); in tb_approve_xdomain_paths()
2116 nhi_port = tb_switch_find_port(tb->root_switch, TB_TYPE_NHI); in tb_approve_xdomain_paths()
2118 mutex_lock(&tb->lock); in tb_approve_xdomain_paths()
2121 * When tunneling DMA paths the link should not enter CL states in tb_approve_xdomain_paths()
2129 ret = -ENOMEM; in tb_approve_xdomain_paths()
2135 "DMA tunnel activation failed, aborting\n"); in tb_approve_xdomain_paths()
2136 ret = -EIO; in tb_approve_xdomain_paths()
2140 list_add_tail(&tunnel->list, &tcm->tunnel_list); in tb_approve_xdomain_paths()
2141 mutex_unlock(&tb->lock); in tb_approve_xdomain_paths()
2148 mutex_unlock(&tb->lock); in tb_approve_xdomain_paths()
2162 sw = tb_to_switch(xd->dev.parent); in __tb_disconnect_xdomain_paths()
2163 dst_port = tb_port_at(xd->route, sw); in __tb_disconnect_xdomain_paths()
2164 nhi_port = tb_switch_find_port(tb->root_switch, TB_TYPE_NHI); in __tb_disconnect_xdomain_paths()
2166 list_for_each_entry_safe(tunnel, n, &tcm->tunnel_list, list) { in __tb_disconnect_xdomain_paths()
2169 if (tunnel->src_port != nhi_port || tunnel->dst_port != dst_port) in __tb_disconnect_xdomain_paths()
2178 * Try to re-enable CL states now, it is OK if this fails in __tb_disconnect_xdomain_paths()
2179 * because we may still have another DMA tunnel active through in __tb_disconnect_xdomain_paths()
2180 * the same host router USB4 downstream port. in __tb_disconnect_xdomain_paths()
2189 if (!xd->is_unplugged) { in tb_disconnect_xdomain_paths()
2190 mutex_lock(&tb->lock); in tb_disconnect_xdomain_paths()
2194 mutex_unlock(&tb->lock); in tb_disconnect_xdomain_paths()
2202 * tb_handle_hotplug() - handle hotplug event
2204 * Executes on tb->wq.
2209 struct tb *tb = ev->tb; in tb_handle_hotplug()
2215 pm_runtime_get_sync(&tb->dev); in tb_handle_hotplug()
2217 mutex_lock(&tb->lock); in tb_handle_hotplug()
2218 if (!tcm->hotplug_active) in tb_handle_hotplug()
2221 sw = tb_switch_find_by_route(tb, ev->route); in tb_handle_hotplug()
2225 ev->route, ev->port, ev->unplug); in tb_handle_hotplug()
2228 if (ev->port > sw->config.max_port_number) { in tb_handle_hotplug()
2231 ev->route, ev->port, ev->unplug); in tb_handle_hotplug()
2234 port = &sw->ports[ev->port]; in tb_handle_hotplug()
2237 ev->route, ev->port, ev->unplug); in tb_handle_hotplug()
2241 pm_runtime_get_sync(&sw->dev); in tb_handle_hotplug()
2243 if (ev->unplug) { in tb_handle_hotplug()
2248 tb_sw_set_unplugged(port->remote->sw); in tb_handle_hotplug()
2250 tb_remove_dp_resources(port->remote->sw); in tb_handle_hotplug()
2251 tb_switch_tmu_disable(port->remote->sw); in tb_handle_hotplug()
2252 tb_switch_unconfigure_link(port->remote->sw); in tb_handle_hotplug()
2253 tb_switch_set_link_width(port->remote->sw, in tb_handle_hotplug()
2255 tb_switch_remove(port->remote->sw); in tb_handle_hotplug()
2256 port->remote = NULL; in tb_handle_hotplug()
2257 if (port->dual_link_port) in tb_handle_hotplug()
2258 port->dual_link_port->remote = NULL; in tb_handle_hotplug()
2262 } else if (port->xdomain) { in tb_handle_hotplug()
2263 struct tb_xdomain *xd = tb_xdomain_get(port->xdomain); in tb_handle_hotplug()
2273 xd->is_unplugged = true; in tb_handle_hotplug()
2275 port->xdomain = NULL; in tb_handle_hotplug()
2276 __tb_disconnect_xdomain_paths(tb, xd, -1, -1, -1, -1); in tb_handle_hotplug()
2281 } else if (!port->port) { in tb_handle_hotplug()
2288 } else if (port->remote) { in tb_handle_hotplug()
2290 } else if (!port->port && sw->authorized) { in tb_handle_hotplug()
2297 if (!port->remote) in tb_handle_hotplug()
2304 pm_runtime_mark_last_busy(&sw->dev); in tb_handle_hotplug()
2305 pm_runtime_put_autosuspend(&sw->dev); in tb_handle_hotplug()
2310 mutex_unlock(&tb->lock); in tb_handle_hotplug()
2312 pm_runtime_mark_last_busy(&tb->dev); in tb_handle_hotplug()
2313 pm_runtime_put_autosuspend(&tb->dev); in tb_handle_hotplug()
2324 struct tb *tb = tunnel->tb; in tb_alloc_dp_bandwidth()
2331 in = tunnel->src_port; in tb_alloc_dp_bandwidth()
2332 out = tunnel->dst_port; in tb_alloc_dp_bandwidth()
2384 return -ENOBUFS; in tb_alloc_dp_bandwidth()
2447 ret = -ENOBUFS; in tb_alloc_dp_bandwidth()
2461 struct tb *tb = ev->tb; in tb_handle_dp_bandwidth_request()
2465 pm_runtime_get_sync(&tb->dev); in tb_handle_dp_bandwidth_request()
2467 mutex_lock(&tb->lock); in tb_handle_dp_bandwidth_request()
2468 if (!tcm->hotplug_active) in tb_handle_dp_bandwidth_request()
2471 sw = tb_switch_find_by_route(tb, ev->route); in tb_handle_dp_bandwidth_request()
2473 tb_warn(tb, "bandwidth request from non-existent router %llx\n", in tb_handle_dp_bandwidth_request()
2474 ev->route); in tb_handle_dp_bandwidth_request()
2478 in = &sw->ports[ev->port]; in tb_handle_dp_bandwidth_request()
2480 tb_port_warn(in, "bandwidth request to non-DP IN adapter\n"); in tb_handle_dp_bandwidth_request()
2493 if (ret == -ENODATA) in tb_handle_dp_bandwidth_request()
2509 out = tunnel->dst_port; in tb_handle_dp_bandwidth_request()
2512 requested_up = -1; in tb_handle_dp_bandwidth_request()
2516 requested_down = -1; in tb_handle_dp_bandwidth_request()
2521 if (ret == -ENOBUFS) in tb_handle_dp_bandwidth_request()
2536 mutex_unlock(&tb->lock); in tb_handle_dp_bandwidth_request()
2538 pm_runtime_mark_last_busy(&tb->dev); in tb_handle_dp_bandwidth_request()
2539 pm_runtime_put_autosuspend(&tb->dev); in tb_handle_dp_bandwidth_request()
2552 ev->tb = tb; in tb_queue_dp_bandwidth_request()
2553 ev->route = route; in tb_queue_dp_bandwidth_request()
2554 ev->port = port; in tb_queue_dp_bandwidth_request()
2555 INIT_WORK(&ev->work, tb_handle_dp_bandwidth_request); in tb_queue_dp_bandwidth_request()
2556 queue_work(tb->wq, &ev->work); in tb_queue_dp_bandwidth_request()
2563 switch (error->error) { in tb_handle_notification()
2567 if (tb_cfg_ack_notification(tb->ctl, route, error)) in tb_handle_notification()
2573 if (tb_cfg_ack_notification(tb->ctl, route, error)) in tb_handle_notification()
2576 tb_queue_dp_bandwidth_request(tb, route, error->port); in tb_handle_notification()
2586 * tb_schedule_hotplug_handler() - callback function for the control channel
2594 u64 route = tb_cfg_get_route(&pkg->header); in tb_handle_event()
2607 if (tb_cfg_ack_plug(tb->ctl, route, pkg->port, pkg->unplug)) { in tb_handle_event()
2609 pkg->port); in tb_handle_event()
2612 tb_queue_hotplug(tb, route, pkg->port, pkg->unplug); in tb_handle_event()
2621 cancel_delayed_work(&tcm->remove_work); in tb_stop()
2623 list_for_each_entry_safe(tunnel, n, &tcm->tunnel_list, list) { in tb_stop()
2625 * DMA tunnels require the driver to be functional so we in tb_stop()
2633 tb_switch_remove(tb->root_switch); in tb_stop()
2634 tcm->hotplug_active = false; /* signal tb_handle_hotplug to quit */ in tb_stop()
2647 if (sw->boot) in tb_scan_finalize_switch()
2648 sw->authorized = 1; in tb_scan_finalize_switch()
2651 kobject_uevent(&dev->kobj, KOBJ_ADD); in tb_scan_finalize_switch()
2663 tb->root_switch = tb_switch_alloc(tb, &tb->dev, 0); in tb_start()
2664 if (IS_ERR(tb->root_switch)) in tb_start()
2665 return PTR_ERR(tb->root_switch); in tb_start()
2673 * implement the necessary router operations. in tb_start()
2675 tb->root_switch->no_nvm_upgrade = !tb_switch_is_usb4(tb->root_switch); in tb_start()
2677 tb->root_switch->rpm = tb_switch_is_usb4(tb->root_switch); in tb_start()
2679 ret = tb_switch_configure(tb->root_switch); in tb_start()
2681 tb_switch_put(tb->root_switch); in tb_start()
2686 ret = tb_switch_add(tb->root_switch); in tb_start()
2688 tb_switch_put(tb->root_switch); in tb_start()
2693 * To support highest CLx state, we set host router's TMU to in tb_start()
2696 tb_switch_tmu_configure(tb->root_switch, TB_SWITCH_TMU_MODE_LOWRES); in tb_start()
2698 tb_switch_tmu_enable(tb->root_switch); in tb_start()
2706 if (reset && usb4_switch_version(tb->root_switch) == 1) { in tb_start()
2707 tb_switch_reset(tb->root_switch); in tb_start()
2710 tb_scan_switch(tb->root_switch); in tb_start()
2721 tb_create_usb3_tunnels(tb->root_switch); in tb_start()
2723 tb_add_dp_resources(tb->root_switch); in tb_start()
2724 tb_switch_enter_redrive(tb->root_switch); in tb_start()
2726 device_for_each_child(&tb->root_switch->dev, NULL, in tb_start()
2730 tcm->hotplug_active = true; in tb_start()
2740 tb_switch_exit_redrive(tb->root_switch); in tb_suspend_noirq()
2741 tb_switch_suspend(tb->root_switch, false); in tb_suspend_noirq()
2742 tcm->hotplug_active = false; /* signal tb_handle_hotplug to quit */ in tb_suspend_noirq()
2752 /* No need to restore if the router is already unplugged */ in tb_restore_children()
2753 if (sw->is_unplugged) in tb_restore_children()
2757 tb_sw_warn(sw, "failed to re-enable CL states\n"); in tb_restore_children()
2765 if (!tb_port_has_remote(port) && !port->xdomain) in tb_restore_children()
2768 if (port->remote) { in tb_restore_children()
2769 tb_switch_set_link_width(port->remote->sw, in tb_restore_children()
2770 port->remote->sw->link_width); in tb_restore_children()
2771 tb_switch_configure_link(port->remote->sw); in tb_restore_children()
2773 tb_restore_children(port->remote->sw); in tb_restore_children()
2774 } else if (port->xdomain) { in tb_restore_children()
2775 tb_port_configure_xdomain(port, port->xdomain); in tb_restore_children()
2790 * For non-USB4 hosts (Apple systems) remove any PCIe devices in tb_resume_noirq()
2793 if (!tb_switch_is_usb4(tb->root_switch)) in tb_resume_noirq()
2794 tb_switch_reset(tb->root_switch); in tb_resume_noirq()
2796 tb_switch_resume(tb->root_switch, false); in tb_resume_noirq()
2798 tb_free_unplugged_children(tb->root_switch); in tb_resume_noirq()
2799 tb_restore_children(tb->root_switch); in tb_resume_noirq()
2807 tb_switch_discover_tunnels(tb->root_switch, &tunnels, false); in tb_resume_noirq()
2815 /* Re-create our tunnels now */ in tb_resume_noirq()
2816 list_for_each_entry_safe(tunnel, n, &tcm->tunnel_list, list) { in tb_resume_noirq()
2817 /* USB3 requires delay before it can be re-activated */ in tb_resume_noirq()
2825 if (!list_empty(&tcm->tunnel_list)) { in tb_resume_noirq()
2833 tb_switch_enter_redrive(tb->root_switch); in tb_resume_noirq()
2835 tcm->hotplug_active = true; in tb_resume_noirq()
2849 if (port->xdomain && port->xdomain->is_unplugged) { in tb_free_unplugged_xdomains()
2851 tb_xdomain_remove(port->xdomain); in tb_free_unplugged_xdomains()
2853 port->xdomain = NULL; in tb_free_unplugged_xdomains()
2855 } else if (port->remote) { in tb_free_unplugged_xdomains()
2856 ret += tb_free_unplugged_xdomains(port->remote->sw); in tb_free_unplugged_xdomains()
2867 tcm->hotplug_active = false; in tb_freeze_noirq()
2875 tcm->hotplug_active = true; in tb_thaw_noirq()
2886 mutex_lock(&tb->lock); in tb_complete()
2887 if (tb_free_unplugged_xdomains(tb->root_switch)) in tb_complete()
2888 tb_scan_switch(tb->root_switch); in tb_complete()
2889 mutex_unlock(&tb->lock); in tb_complete()
2896 mutex_lock(&tb->lock); in tb_runtime_suspend()
2899 * re-entering redrive mode. in tb_runtime_suspend()
2902 tb_switch_exit_redrive(tb->root_switch); in tb_runtime_suspend()
2903 tb_switch_suspend(tb->root_switch, true); in tb_runtime_suspend()
2904 tcm->hotplug_active = false; in tb_runtime_suspend()
2905 mutex_unlock(&tb->lock); in tb_runtime_suspend()
2915 mutex_lock(&tb->lock); in tb_remove_work()
2916 if (tb->root_switch) { in tb_remove_work()
2917 tb_free_unplugged_children(tb->root_switch); in tb_remove_work()
2918 tb_free_unplugged_xdomains(tb->root_switch); in tb_remove_work()
2920 mutex_unlock(&tb->lock); in tb_remove_work()
2928 mutex_lock(&tb->lock); in tb_runtime_resume()
2929 tb_switch_resume(tb->root_switch, true); in tb_runtime_resume()
2931 tb_restore_children(tb->root_switch); in tb_runtime_resume()
2932 list_for_each_entry_safe(tunnel, n, &tcm->tunnel_list, list) in tb_runtime_resume()
2934 tb_switch_enter_redrive(tb->root_switch); in tb_runtime_resume()
2935 tcm->hotplug_active = true; in tb_runtime_resume()
2936 mutex_unlock(&tb->lock); in tb_runtime_resume()
2943 queue_delayed_work(tb->wq, &tcm->remove_work, msecs_to_jiffies(50)); in tb_runtime_resume()
2979 switch (nhi->pdev->device) { in tb_apple_add_links()
2989 upstream = pci_upstream_bridge(nhi->pdev); in tb_apple_add_links()
3003 * back to NHI so that PCIe tunnels can be re-established after in tb_apple_add_links()
3007 for_each_pci_bridge(pdev, upstream->subordinate) { in tb_apple_add_links()
3013 !pdev->is_hotplug_bridge) in tb_apple_add_links()
3016 link = device_link_add(&pdev->dev, &nhi->pdev->dev, in tb_apple_add_links()
3020 dev_dbg(&nhi->pdev->dev, "created link from %s\n", in tb_apple_add_links()
3021 dev_name(&pdev->dev)); in tb_apple_add_links()
3024 dev_warn(&nhi->pdev->dev, "device link creation from %s failed\n", in tb_apple_add_links()
3025 dev_name(&pdev->dev)); in tb_apple_add_links()
3042 tb->security_level = TB_SECURITY_USER; in tb_probe()
3044 tb->security_level = TB_SECURITY_NOPCIE; in tb_probe()
3046 tb->cm_ops = &tb_cm_ops; in tb_probe()
3049 INIT_LIST_HEAD(&tcm->tunnel_list); in tb_probe()
3050 INIT_LIST_HEAD(&tcm->dp_resources); in tb_probe()
3051 INIT_DELAYED_WORK(&tcm->remove_work, tb_remove_work); in tb_probe()