Lines Matching +full:uni +full:- +full:directional

1 // SPDX-License-Identifier: GPL-2.0
3 * Thunderbolt driver - bus logic (NHI independent)
30 * direction. This is 40G - 10% guard band bandwidth.
53 * struct tb_cm - Simple Thunderbolt connection manager
74 return ((void *)tcm - sizeof(struct tb)); in tcm_to_tb()
101 ev->tb = tb; in tb_queue_hotplug()
102 ev->route = route; in tb_queue_hotplug()
103 ev->port = port; in tb_queue_hotplug()
104 ev->unplug = unplug; in tb_queue_hotplug()
105 INIT_DELAYED_WORK(&ev->work, tb_handle_hotplug); in tb_queue_hotplug()
106 queue_delayed_work(tb->wq, &ev->work, 0); in tb_queue_hotplug()
113 struct tb_cm *tcm = tb_priv(sw->tb); in tb_add_dp_resources()
130 list_add(&port->list, &tcm->dp_resources); in tb_add_dp_resources()
132 list_add_tail(&port->list, &tcm->dp_resources); in tb_add_dp_resources()
140 struct tb_cm *tcm = tb_priv(sw->tb); in tb_remove_dp_resources()
146 tb_remove_dp_resources(port->remote->sw); in tb_remove_dp_resources()
149 list_for_each_entry_safe(port, tmp, &tcm->dp_resources, list) { in tb_remove_dp_resources()
150 if (port->sw == sw) { in tb_remove_dp_resources()
152 list_del_init(&port->list); in tb_remove_dp_resources()
162 list_for_each_entry(p, &tcm->dp_resources, list) { in tb_discover_dp_resource()
169 list_add_tail(&port->list, &tcm->dp_resources); in tb_discover_dp_resource()
177 list_for_each_entry(tunnel, &tcm->tunnel_list, list) { in tb_discover_dp_resources()
179 tb_discover_dp_resource(tb, tunnel->dst_port); in tb_discover_dp_resources()
186 struct tb_cm *tcm = tb_priv(sw->tb); in tb_enable_clx()
208 * If we are re-enabling then check if there is an active DMA in tb_enable_clx()
211 list_for_each_entry(tunnel, &tcm->tunnel_list, list) { in tb_enable_clx()
223 if (ret == -EOPNOTSUPP) in tb_enable_clx()
225 return ret == -EOPNOTSUPP ? 0 : ret; in tb_enable_clx()
229 * tb_disable_clx() - Disable CL states up to host router
299 sw = tunnel->tb->root_switch; in tb_increase_tmu_accuracy()
300 device_for_each_child(&sw->dev, NULL, tb_increase_switch_tmu_accuracy); in tb_increase_tmu_accuracy()
317 return device_for_each_child(&tb->dev, NULL, in tb_tmu_hifi_uni_required()
327 * enable the enhanched uni-directional mode. That covers all in tb_enable_tmu()
334 if (ret == -EOPNOTSUPP) { in tb_enable_tmu()
337 * Figure out uni-directional HiFi TMU requirements in tb_enable_tmu()
339 * uni-directional HiFi requirements we can put the TMU in tb_enable_tmu()
342 * Deliberately skip bi-directional HiFi links in tb_enable_tmu()
346 if (tb_tmu_hifi_uni_required(sw->tb)) in tb_enable_tmu()
356 /* If not supported, fallback to bi-directional HiFi */ in tb_enable_tmu()
357 if (ret == -EOPNOTSUPP) in tb_enable_tmu()
382 struct tb *tb = sw->tb; in tb_switch_discover_tunnels()
388 switch (port->config.type) { in tb_switch_discover_tunnels()
407 list_add_tail(&tunnel->list, list); in tb_switch_discover_tunnels()
412 tb_switch_discover_tunnels(port->remote->sw, list, in tb_switch_discover_tunnels()
420 if (tb_switch_is_usb4(port->sw)) in tb_port_configure_xdomain()
427 if (tb_switch_is_usb4(port->sw)) in tb_port_unconfigure_xdomain()
435 struct tb_switch *sw = port->sw; in tb_scan_xdomain()
436 struct tb *tb = sw->tb; in tb_scan_xdomain()
450 xd = tb_xdomain_alloc(tb, &sw->dev, route, tb->root_switch->uuid, in tb_scan_xdomain()
453 tb_port_at(route, sw)->xdomain = xd; in tb_scan_xdomain()
460 * tb_find_unused_port() - return the first inactive port on @sw
472 if (port->config.type != type) in tb_find_unused_port()
474 if (!port->cap_adap) in tb_find_unused_port()
501 list_for_each_entry(tunnel, &tcm->tunnel_list, list) { in tb_find_tunnel()
502 if (tunnel->type == type && in tb_find_tunnel()
503 ((src_port && src_port == tunnel->src_port) || in tb_find_tunnel()
504 (dst_port && dst_port == tunnel->dst_port))) { in tb_find_tunnel()
521 sw = dst_port->sw; in tb_find_first_usb3_tunnel()
523 sw = src_port->sw; in tb_find_first_usb3_tunnel()
526 if (sw == tb->root_switch) in tb_find_first_usb3_tunnel()
530 port = tb_port_at(tb_route(sw), tb->root_switch); in tb_find_first_usb3_tunnel()
532 usb3_down = usb4_switch_map_usb3_down(tb->root_switch, port); in tb_find_first_usb3_tunnel()
540 * tb_consumed_usb3_pcie_bandwidth() - Consumed USB3/PCIe bandwidth over a single link
589 * tb_consumed_dp_bandwidth() - Consumed DP bandwidth over a single link
624 list_for_each_entry(tunnel, &tcm->tunnel_list, list) { in tb_consumed_dp_bandwidth()
642 group = tunnel->src_port->group; in tb_consumed_dp_bandwidth()
643 if (group && group->reserved && !group_reserved[group->index]) in tb_consumed_dp_bandwidth()
644 group_reserved[group->index] = group->reserved; in tb_consumed_dp_bandwidth()
649 * re-calculating estimated bandwidth. in tb_consumed_dp_bandwidth()
651 if (tunnel->src_port == src_port && in tb_consumed_dp_bandwidth()
652 tunnel->dst_port == dst_port) in tb_consumed_dp_bandwidth()
690 * tb_maximum_bandwidth() - Maximum bandwidth over a single link
720 link_speed = port->sw->link_speed; in tb_maximum_bandwidth()
722 * sw->link_width is from upstream perspective so we use in tb_maximum_bandwidth()
725 if (port->sw->link_width == TB_LINK_WIDTH_ASYM_TX) { in tb_maximum_bandwidth()
728 } else if (port->sw->link_width == TB_LINK_WIDTH_ASYM_RX) { in tb_maximum_bandwidth()
746 up_bw = link_speed * port->sw->link_width * 1000; in tb_maximum_bandwidth()
785 *max_up = up_bw - up_bw / 10; in tb_maximum_bandwidth()
786 *max_down = down_bw - down_bw / 10; in tb_maximum_bandwidth()
793 * tb_available_bandwidth() - Available bandwidth for tunneling
838 max_up -= consumed_up; in tb_available_bandwidth()
839 max_down -= consumed_down; in tb_available_bandwidth()
845 max_up -= consumed_up; in tb_available_bandwidth()
846 max_down -= consumed_down; in tb_available_bandwidth()
888 ret = tb_available_bandwidth(tb, tunnel->src_port, tunnel->dst_port, in tb_reclaim_usb3_bandwidth()
918 if (!sw->link_usb4) in tb_tunnel_usb3()
967 ret = -ENOMEM; in tb_tunnel_usb3()
974 ret = -EIO; in tb_tunnel_usb3()
978 list_add_tail(&tunnel->list, &tcm->tunnel_list); in tb_tunnel_usb3()
1002 ret = tb_tunnel_usb3(sw->tb, sw); in tb_create_usb3_tunnels()
1010 ret = tb_create_usb3_tunnels(port->remote->sw); in tb_create_usb3_tunnels()
1019 * tb_configure_asym() - Transition links to asymmetric if needed
1048 sw = dst_port->sw; in tb_configure_asym()
1050 sw = src_port->sw; in tb_configure_asym()
1053 struct tb_port *down = tb_switch_downstream_port(up->sw); in tb_configure_asym()
1065 * (40G - guard band 10%), and the requested is above in tb_configure_asym()
1069 ret = -ENOBUFS; in tb_configure_asym()
1081 ret = -ENOBUFS; in tb_configure_asym()
1091 if (up->sw->link_width == width_up) in tb_configure_asym()
1108 tb_sw_dbg(up->sw, "configuring asymmetric link\n"); in tb_configure_asym()
1114 ret = tb_switch_set_link_width(up->sw, width_up); in tb_configure_asym()
1116 tb_sw_warn(up->sw, "failed to set link width\n"); in tb_configure_asym()
1121 /* Re-enable CL states if they were previosly enabled */ in tb_configure_asym()
1129 * tb_configure_sym() - Transition links to symmetric if possible
1153 sw = dst_port->sw; in tb_configure_sym()
1155 sw = src_port->sw; in tb_configure_sym()
1161 if (up->sw->link_width <= TB_LINK_WIDTH_DUAL) in tb_configure_sym()
1164 if (up->sw->is_unplugged) in tb_configure_sym()
1186 if (up->sw->link_width == TB_LINK_WIDTH_DUAL) in tb_configure_sym()
1197 up->sw->preferred_link_width > TB_LINK_WIDTH_DUAL) { in tb_configure_sym()
1198 tb_sw_dbg(up->sw, "keeping preferred asymmetric link\n"); in tb_configure_sym()
1208 tb_sw_dbg(up->sw, "configuring symmetric link\n"); in tb_configure_sym()
1210 ret = tb_switch_set_link_width(up->sw, TB_LINK_WIDTH_DUAL); in tb_configure_sym()
1212 tb_sw_warn(up->sw, "failed to set link width\n"); in tb_configure_sym()
1217 /* Re-enable CL states if they were previosly enabled */ in tb_configure_sym()
1227 struct tb *tb = sw->tb; in tb_configure_link()
1230 down->remote = up; in tb_configure_link()
1231 up->remote = down; in tb_configure_link()
1232 if (down->dual_link_port && up->dual_link_port) { in tb_configure_link()
1233 down->dual_link_port->remote = up->dual_link_port; in tb_configure_link()
1234 up->dual_link_port->remote = down->dual_link_port; in tb_configure_link()
1241 if (sw->link_width < TB_LINK_WIDTH_DUAL) in tb_configure_link()
1251 up->sw->link_width == TB_LINK_WIDTH_DUAL) { in tb_configure_link()
1254 host_port = tb_port_at(tb_route(sw), tb->root_switch); in tb_configure_link()
1263 * tb_scan_switch() - scan for and initialize downstream switches
1269 pm_runtime_get_sync(&sw->dev); in tb_scan_switch()
1274 pm_runtime_mark_last_busy(&sw->dev); in tb_scan_switch()
1275 pm_runtime_put_autosuspend(&sw->dev); in tb_scan_switch()
1279 * tb_scan_port() - check for and initialize switches below port
1283 struct tb_cm *tcm = tb_priv(port->sw->tb); in tb_scan_port()
1294 tb_queue_hotplug(port->sw->tb, tb_route(port->sw), port->port, in tb_scan_port()
1299 if (port->config.type != TB_TYPE_PORT) in tb_scan_port()
1301 if (port->dual_link_port && port->link_nr) in tb_scan_port()
1307 if (port->usb4) in tb_scan_port()
1308 pm_runtime_get_sync(&port->usb4->dev); in tb_scan_port()
1312 if (port->remote) { in tb_scan_port()
1317 sw = tb_switch_alloc(port->sw->tb, &port->sw->dev, in tb_scan_port()
1331 if (PTR_ERR(sw) == -EIO || PTR_ERR(sw) == -EADDRNOTAVAIL) in tb_scan_port()
1345 if (port->xdomain) { in tb_scan_port()
1346 tb_xdomain_remove(port->xdomain); in tb_scan_port()
1348 port->xdomain = NULL; in tb_scan_port()
1356 if (!tcm->hotplug_active) { in tb_scan_port()
1357 dev_set_uevent_suppress(&sw->dev, true); in tb_scan_port()
1365 sw->rpm = sw->generation > 1; in tb_scan_port()
1410 if (tcm->hotplug_active && tb_tunnel_usb3(sw->tb, sw)) in tb_scan_port()
1417 if (port->usb4) { in tb_scan_port()
1418 pm_runtime_mark_last_busy(&port->usb4->dev); in tb_scan_port()
1419 pm_runtime_put_autosuspend(&port->usb4->dev); in tb_scan_port()
1427 struct tb *tb = group->tb; in tb_recalc_estimated_bandwidth_for_group()
1431 tb_dbg(tb, "re-calculating bandwidth estimation for group %u\n", in tb_recalc_estimated_bandwidth_for_group()
1432 group->index); in tb_recalc_estimated_bandwidth_for_group()
1435 list_for_each_entry(in, &group->ports, group_list) { in tb_recalc_estimated_bandwidth_for_group()
1457 first_tunnel->src_port, first_tunnel->dst_port); in tb_recalc_estimated_bandwidth_for_group()
1465 out = tunnel->dst_port; in tb_recalc_estimated_bandwidth_for_group()
1470 "failed to re-calculate estimated bandwidth\n"); in tb_recalc_estimated_bandwidth_for_group()
1476 * - already allocated bandwidth for the DP tunnel in tb_recalc_estimated_bandwidth_for_group()
1477 * - available bandwidth along the path in tb_recalc_estimated_bandwidth_for_group()
1478 * - bandwidth allocated for USB 3.x but not used. in tb_recalc_estimated_bandwidth_for_group()
1490 "re-calculated estimated bandwidth %u (+ %u reserved) = %u Mb/s\n", in tb_recalc_estimated_bandwidth_for_group()
1491 estimated_bw, group->reserved, in tb_recalc_estimated_bandwidth_for_group()
1492 estimated_bw + group->reserved); in tb_recalc_estimated_bandwidth_for_group()
1495 estimated_bw + group->reserved)) in tb_recalc_estimated_bandwidth_for_group()
1501 tb_reclaim_usb3_bandwidth(tb, first_tunnel->src_port, in tb_recalc_estimated_bandwidth_for_group()
1502 first_tunnel->dst_port); in tb_recalc_estimated_bandwidth_for_group()
1504 tb_dbg(tb, "bandwidth estimation for group %u done\n", group->index); in tb_recalc_estimated_bandwidth_for_group()
1512 tb_dbg(tb, "bandwidth consumption changed, re-calculating estimated bandwidth\n"); in tb_recalc_estimated_bandwidth()
1514 for (i = 0; i < ARRAY_SIZE(tcm->groups); i++) { in tb_recalc_estimated_bandwidth()
1515 struct tb_bandwidth_group *group = &tcm->groups[i]; in tb_recalc_estimated_bandwidth()
1517 if (!list_empty(&group->ports)) in tb_recalc_estimated_bandwidth()
1521 tb_dbg(tb, "bandwidth re-calculation done\n"); in tb_recalc_estimated_bandwidth()
1526 if (group->reserved) { in __release_group_bandwidth()
1527 tb_dbg(group->tb, "group %d released total %d Mb/s\n", group->index, in __release_group_bandwidth()
1528 group->reserved); in __release_group_bandwidth()
1529 group->reserved = 0; in __release_group_bandwidth()
1540 if (list_empty(&group->ports)) in __configure_group_sym()
1553 in = list_first_entry(&group->ports, struct tb_port, group_list); in __configure_group_sym()
1554 tunnel = tb_find_tunnel(group->tb, TB_TUNNEL_DP, in, NULL); in __configure_group_sym()
1556 tb_configure_sym(group->tb, in, tunnel->dst_port, true); in __configure_group_sym()
1563 struct tb *tb = group->tb; in tb_bandwidth_group_release_work()
1565 mutex_lock(&tb->lock); in tb_bandwidth_group_release_work()
1569 mutex_unlock(&tb->lock); in tb_bandwidth_group_release_work()
1576 for (i = 0; i < ARRAY_SIZE(tcm->groups); i++) { in tb_init_bandwidth_groups()
1577 struct tb_bandwidth_group *group = &tcm->groups[i]; in tb_init_bandwidth_groups()
1579 group->tb = tcm_to_tb(tcm); in tb_init_bandwidth_groups()
1580 group->index = i + 1; in tb_init_bandwidth_groups()
1581 INIT_LIST_HEAD(&group->ports); in tb_init_bandwidth_groups()
1582 INIT_DELAYED_WORK(&group->release_work, in tb_init_bandwidth_groups()
1590 if (!group || WARN_ON(in->group)) in tb_bandwidth_group_attach_port()
1593 in->group = group; in tb_bandwidth_group_attach_port()
1594 list_add_tail(&in->group_list, &group->ports); in tb_bandwidth_group_attach_port()
1596 tb_port_dbg(in, "attached to bandwidth group %d\n", group->index); in tb_bandwidth_group_attach_port()
1603 for (i = 0; i < ARRAY_SIZE(tcm->groups); i++) { in tb_find_free_bandwidth_group()
1604 struct tb_bandwidth_group *group = &tcm->groups[i]; in tb_find_free_bandwidth_group()
1606 if (list_empty(&group->ports)) in tb_find_free_bandwidth_group()
1626 list_for_each_entry(tunnel, &tcm->tunnel_list, list) { in tb_attach_bandwidth_group()
1630 if (tunnel->src_port->sw == in->sw && in tb_attach_bandwidth_group()
1631 tunnel->dst_port->sw == out->sw) { in tb_attach_bandwidth_group()
1632 group = tunnel->src_port->group; in tb_attach_bandwidth_group()
1657 for (i = 0; i < ARRAY_SIZE(tcm->groups); i++) { in tb_discover_bandwidth_group()
1658 if (tcm->groups[i].index == index) { in tb_discover_bandwidth_group()
1659 tb_bandwidth_group_attach_port(&tcm->groups[i], in); in tb_discover_bandwidth_group()
1670 struct tb_bandwidth_group *group = in->group; in tb_detach_bandwidth_group()
1673 in->group = NULL; in tb_detach_bandwidth_group()
1674 list_del_init(&in->group_list); in tb_detach_bandwidth_group()
1676 tb_port_dbg(in, "detached from bandwidth group %d\n", group->index); in tb_detach_bandwidth_group()
1679 if (list_empty(&group->ports)) { in tb_detach_bandwidth_group()
1680 cancel_delayed_work(&group->release_work); in tb_detach_bandwidth_group()
1691 tb_switch_discover_tunnels(tb->root_switch, &tcm->tunnel_list, true); in tb_discover_tunnels()
1693 list_for_each_entry(tunnel, &tcm->tunnel_list, list) { in tb_discover_tunnels()
1695 struct tb_switch *parent = tunnel->dst_port->sw; in tb_discover_tunnels()
1697 while (parent != tunnel->src_port->sw) { in tb_discover_tunnels()
1698 parent->boot = true; in tb_discover_tunnels()
1702 struct tb_port *in = tunnel->src_port; in tb_discover_tunnels()
1703 struct tb_port *out = tunnel->dst_port; in tb_discover_tunnels()
1706 pm_runtime_get_sync(&in->sw->dev); in tb_discover_tunnels()
1707 pm_runtime_get_sync(&out->sw->dev); in tb_discover_tunnels()
1723 list_del(&tunnel->list); in tb_deactivate_and_free_tunnel()
1725 tb = tunnel->tb; in tb_deactivate_and_free_tunnel()
1726 src_port = tunnel->src_port; in tb_deactivate_and_free_tunnel()
1727 dst_port = tunnel->dst_port; in tb_deactivate_and_free_tunnel()
1729 switch (tunnel->type) { in tb_deactivate_and_free_tunnel()
1736 tb_switch_dealloc_dp_resource(src_port->sw, src_port); in tb_deactivate_and_free_tunnel()
1743 pm_runtime_mark_last_busy(&dst_port->sw->dev); in tb_deactivate_and_free_tunnel()
1744 pm_runtime_put_autosuspend(&dst_port->sw->dev); in tb_deactivate_and_free_tunnel()
1745 pm_runtime_mark_last_busy(&src_port->sw->dev); in tb_deactivate_and_free_tunnel()
1746 pm_runtime_put_autosuspend(&src_port->sw->dev); in tb_deactivate_and_free_tunnel()
1765 * tb_free_invalid_tunnels() - destroy tunnels of devices that have gone away
1773 list_for_each_entry_safe(tunnel, n, &tcm->tunnel_list, list) { in tb_free_invalid_tunnels()
1780 * tb_free_unplugged_children() - traverse hierarchy and free unplugged switches
1790 if (port->remote->sw->is_unplugged) { in tb_free_unplugged_children()
1792 tb_remove_dp_resources(port->remote->sw); in tb_free_unplugged_children()
1793 tb_switch_unconfigure_link(port->remote->sw); in tb_free_unplugged_children()
1794 tb_switch_set_link_width(port->remote->sw, in tb_free_unplugged_children()
1796 tb_switch_remove(port->remote->sw); in tb_free_unplugged_children()
1797 port->remote = NULL; in tb_free_unplugged_children()
1798 if (port->dual_link_port) in tb_free_unplugged_children()
1799 port->dual_link_port->remote = NULL; in tb_free_unplugged_children()
1801 tb_free_unplugged_children(port->remote->sw); in tb_free_unplugged_children()
1818 int phy_port = tb_phy_port_from_link(port->port); in tb_find_pcie_down()
1822 * Hard-coded Thunderbolt port to PCIe down port mapping in tb_find_pcie_down()
1835 /* Validate the hard-coding */ in tb_find_pcie_down()
1836 if (WARN_ON(index > sw->config.max_port_number)) in tb_find_pcie_down()
1839 down = &sw->ports[index]; in tb_find_pcie_down()
1860 host_port = tb_route(in->sw) ? in tb_find_dp_out()
1861 tb_port_at(tb_route(in->sw), tb->root_switch) : NULL; in tb_find_dp_out()
1863 list_for_each_entry(port, &tcm->dp_resources, list) { in tb_find_dp_out()
1873 if (in->sw == port->sw) { in tb_find_dp_out()
1884 if (host_port && tb_route(port->sw)) { in tb_find_dp_out()
1887 p = tb_port_at(tb_route(port->sw), tb->root_switch); in tb_find_dp_out()
1900 struct tb_port *in = tunnel->src_port; in tb_dp_tunnel_active()
1901 struct tb_port *out = tunnel->dst_port; in tb_dp_tunnel_active()
1904 mutex_lock(&tb->lock); in tb_dp_tunnel_active()
1938 struct tb_port *in = tunnel->src_port; in tb_dp_tunnel_active()
1949 * also tear down the tunnel and try to re-use the in tb_dp_tunnel_active()
1958 mutex_unlock(&tb->lock); in tb_dp_tunnel_active()
1977 list_for_each_entry(tunnel, &tcm->tunnel_list, list) { in tb_tunnel_one_dp()
1992 pm_runtime_get_sync(&in->sw->dev); in tb_tunnel_one_dp()
1993 pm_runtime_get_sync(&out->sw->dev); in tb_tunnel_one_dp()
1995 if (tb_switch_alloc_dp_resource(in->sw, in)) { in tb_tunnel_one_dp()
2028 list_add_tail(&tunnel->list, &tcm->tunnel_list); in tb_tunnel_one_dp()
2031 if (ret && ret != -EINPROGRESS) { in tb_tunnel_one_dp()
2033 list_del(&tunnel->list); in tb_tunnel_one_dp()
2047 tb_switch_dealloc_dp_resource(in->sw, in); in tb_tunnel_one_dp()
2049 pm_runtime_mark_last_busy(&out->sw->dev); in tb_tunnel_one_dp()
2050 pm_runtime_put_autosuspend(&out->sw->dev); in tb_tunnel_one_dp()
2051 pm_runtime_mark_last_busy(&in->sw->dev); in tb_tunnel_one_dp()
2052 pm_runtime_put_autosuspend(&in->sw->dev); in tb_tunnel_one_dp()
2069 tb_dbg(tb, "looking for DP IN <-> DP OUT pairs:\n"); in tb_tunnel_dp()
2073 list_for_each_entry(port, &tcm->dp_resources, list) { in tb_tunnel_dp()
2098 struct tb_switch *sw = port->sw; in tb_enter_redrive()
2100 if (!(sw->quirks & QUIRK_KEEP_POWER_IN_DP_REDRIVE)) in tb_enter_redrive()
2104 * If we get hot-unplug for the DP IN port of the host router in tb_enter_redrive()
2106 * is a monitor connected directly to the Type-C port and we are in tb_enter_redrive()
2115 port->redrive = true; in tb_enter_redrive()
2116 pm_runtime_get(&sw->dev); in tb_enter_redrive()
2123 struct tb_switch *sw = port->sw; in tb_exit_redrive()
2125 if (!(sw->quirks & QUIRK_KEEP_POWER_IN_DP_REDRIVE)) in tb_exit_redrive()
2132 if (port->redrive && tb_switch_query_dp_resource(sw, port)) { in tb_exit_redrive()
2133 port->redrive = false; in tb_exit_redrive()
2134 pm_runtime_put(&sw->dev); in tb_exit_redrive()
2155 if (!(sw->quirks & QUIRK_KEEP_POWER_IN_DP_REDRIVE)) in tb_switch_exit_redrive()
2162 if (port->redrive) { in tb_switch_exit_redrive()
2163 port->redrive = false; in tb_switch_exit_redrive()
2164 pm_runtime_put(&sw->dev); in tb_switch_exit_redrive()
2191 list_del_init(&port->list); in tb_dp_resource_unavailable()
2209 list_for_each_entry(p, &tcm->dp_resources, list) { in tb_dp_resource_available()
2216 list_add_tail(&port->list, &tcm->dp_resources); in tb_dp_resource_available()
2219 /* Look for suitable DP IN <-> DP OUT pairs now */ in tb_dp_resource_available()
2230 * will be re-established after resume based on plug events. in tb_disconnect_and_release_dp()
2232 list_for_each_entry_safe_reverse(tunnel, n, &tcm->tunnel_list, list) { in tb_disconnect_and_release_dp()
2237 while (!list_empty(&tcm->dp_resources)) { in tb_disconnect_and_release_dp()
2240 port = list_first_entry(&tcm->dp_resources, in tb_disconnect_and_release_dp()
2242 list_del_init(&port->list); in tb_disconnect_and_release_dp()
2253 return -ENODEV; in tb_disconnect_pci()
2257 return -ENODEV; in tb_disconnect_pci()
2262 list_del(&tunnel->list); in tb_disconnect_pci()
2288 return -ENOMEM; in tb_tunnel_pci()
2294 return -EIO; in tb_tunnel_pci()
2307 list_add_tail(&tunnel->list, &tcm->tunnel_list); in tb_tunnel_pci()
2321 sw = tb_to_switch(xd->dev.parent); in tb_approve_xdomain_paths()
2322 dst_port = tb_port_at(xd->route, sw); in tb_approve_xdomain_paths()
2323 nhi_port = tb_switch_find_port(tb->root_switch, TB_TYPE_NHI); in tb_approve_xdomain_paths()
2325 mutex_lock(&tb->lock); in tb_approve_xdomain_paths()
2336 ret = -ENOMEM; in tb_approve_xdomain_paths()
2343 ret = -EIO; in tb_approve_xdomain_paths()
2347 list_add_tail(&tunnel->list, &tcm->tunnel_list); in tb_approve_xdomain_paths()
2348 mutex_unlock(&tb->lock); in tb_approve_xdomain_paths()
2355 mutex_unlock(&tb->lock); in tb_approve_xdomain_paths()
2369 sw = tb_to_switch(xd->dev.parent); in __tb_disconnect_xdomain_paths()
2370 dst_port = tb_port_at(xd->route, sw); in __tb_disconnect_xdomain_paths()
2371 nhi_port = tb_switch_find_port(tb->root_switch, TB_TYPE_NHI); in __tb_disconnect_xdomain_paths()
2373 list_for_each_entry_safe(tunnel, n, &tcm->tunnel_list, list) { in __tb_disconnect_xdomain_paths()
2376 if (tunnel->src_port != nhi_port || tunnel->dst_port != dst_port) in __tb_disconnect_xdomain_paths()
2385 * Try to re-enable CL states now, it is OK if this fails in __tb_disconnect_xdomain_paths()
2396 if (!xd->is_unplugged) { in tb_disconnect_xdomain_paths()
2397 mutex_lock(&tb->lock); in tb_disconnect_xdomain_paths()
2401 mutex_unlock(&tb->lock); in tb_disconnect_xdomain_paths()
2409 * tb_handle_hotplug() - handle hotplug event
2411 * Executes on tb->wq.
2416 struct tb *tb = ev->tb; in tb_handle_hotplug()
2422 pm_runtime_get_sync(&tb->dev); in tb_handle_hotplug()
2424 mutex_lock(&tb->lock); in tb_handle_hotplug()
2425 if (!tcm->hotplug_active) in tb_handle_hotplug()
2428 sw = tb_switch_find_by_route(tb, ev->route); in tb_handle_hotplug()
2432 ev->route, ev->port, ev->unplug); in tb_handle_hotplug()
2435 if (ev->port > sw->config.max_port_number) { in tb_handle_hotplug()
2438 ev->route, ev->port, ev->unplug); in tb_handle_hotplug()
2441 port = &sw->ports[ev->port]; in tb_handle_hotplug()
2444 ev->route, ev->port, ev->unplug); in tb_handle_hotplug()
2448 pm_runtime_get_sync(&sw->dev); in tb_handle_hotplug()
2450 if (ev->unplug) { in tb_handle_hotplug()
2455 tb_sw_set_unplugged(port->remote->sw); in tb_handle_hotplug()
2457 tb_remove_dp_resources(port->remote->sw); in tb_handle_hotplug()
2458 tb_switch_tmu_disable(port->remote->sw); in tb_handle_hotplug()
2459 tb_switch_unconfigure_link(port->remote->sw); in tb_handle_hotplug()
2460 tb_switch_set_link_width(port->remote->sw, in tb_handle_hotplug()
2462 tb_switch_remove(port->remote->sw); in tb_handle_hotplug()
2463 port->remote = NULL; in tb_handle_hotplug()
2464 if (port->dual_link_port) in tb_handle_hotplug()
2465 port->dual_link_port->remote = NULL; in tb_handle_hotplug()
2469 } else if (port->xdomain) { in tb_handle_hotplug()
2470 struct tb_xdomain *xd = tb_xdomain_get(port->xdomain); in tb_handle_hotplug()
2480 xd->is_unplugged = true; in tb_handle_hotplug()
2482 port->xdomain = NULL; in tb_handle_hotplug()
2483 __tb_disconnect_xdomain_paths(tb, xd, -1, -1, -1, -1); in tb_handle_hotplug()
2488 } else if (!port->port) { in tb_handle_hotplug()
2495 } else if (port->remote) { in tb_handle_hotplug()
2497 } else if (!port->port && sw->authorized) { in tb_handle_hotplug()
2504 if (!port->remote) in tb_handle_hotplug()
2511 pm_runtime_mark_last_busy(&sw->dev); in tb_handle_hotplug()
2512 pm_runtime_put_autosuspend(&sw->dev); in tb_handle_hotplug()
2517 mutex_unlock(&tb->lock); in tb_handle_hotplug()
2519 pm_runtime_mark_last_busy(&tb->dev); in tb_handle_hotplug()
2520 pm_runtime_put_autosuspend(&tb->dev); in tb_handle_hotplug()
2532 struct tb *tb = tunnel->tb; in tb_alloc_dp_bandwidth()
2540 in = tunnel->src_port; in tb_alloc_dp_bandwidth()
2541 out = tunnel->dst_port; in tb_alloc_dp_bandwidth()
2594 ret = -ENOBUFS; in tb_alloc_dp_bandwidth()
2599 group = in->group; in tb_alloc_dp_bandwidth()
2603 if (tunnel->bw_mode) { in tb_alloc_dp_bandwidth()
2614 reserved = allocated_down - *requested_down; in tb_alloc_dp_bandwidth()
2616 reserved = allocated_up - *requested_up; in tb_alloc_dp_bandwidth()
2619 group->reserved += reserved; in tb_alloc_dp_bandwidth()
2621 group->index, reserved, group->reserved); in tb_alloc_dp_bandwidth()
2631 mod_delayed_work(system_wq, &group->release_work, in tb_alloc_dp_bandwidth()
2663 available_up, available_down, group->reserved); in tb_alloc_dp_bandwidth()
2666 available_up + group->reserved >= requested_up_corrected) || in tb_alloc_dp_bandwidth()
2668 available_down + group->reserved >= requested_down_corrected)) { in tb_alloc_dp_bandwidth()
2691 released = *requested_down - available_down; in tb_alloc_dp_bandwidth()
2694 released = *requested_up - available_up; in tb_alloc_dp_bandwidth()
2697 group->reserved -= released; in tb_alloc_dp_bandwidth()
2699 group->index, released, group->reserved); in tb_alloc_dp_bandwidth()
2702 ret = -ENOBUFS; in tb_alloc_dp_bandwidth()
2708 if (ret && ret != -ENODEV) { in tb_alloc_dp_bandwidth()
2728 struct tb *tb = ev->tb; in tb_handle_dp_bandwidth_request()
2733 pm_runtime_get_sync(&tb->dev); in tb_handle_dp_bandwidth_request()
2735 mutex_lock(&tb->lock); in tb_handle_dp_bandwidth_request()
2736 if (!tcm->hotplug_active) in tb_handle_dp_bandwidth_request()
2739 sw = tb_switch_find_by_route(tb, ev->route); in tb_handle_dp_bandwidth_request()
2741 tb_warn(tb, "bandwidth request from non-existent router %llx\n", in tb_handle_dp_bandwidth_request()
2742 ev->route); in tb_handle_dp_bandwidth_request()
2746 in = &sw->ports[ev->port]; in tb_handle_dp_bandwidth_request()
2748 tb_port_warn(in, "bandwidth request to non-DP IN adapter\n"); in tb_handle_dp_bandwidth_request()
2752 tb_port_dbg(in, "handling bandwidth allocation request, retry %d\n", ev->retry); in tb_handle_dp_bandwidth_request()
2761 if (tunnel->bw_mode) { in tb_handle_dp_bandwidth_request()
2766 tunnel->bw_mode = false; in tb_handle_dp_bandwidth_request()
2776 if (ret == -ENODATA) { in tb_handle_dp_bandwidth_request()
2796 requested_up = -1; in tb_handle_dp_bandwidth_request()
2800 requested_down = -1; in tb_handle_dp_bandwidth_request()
2805 if (ret == -ENOBUFS) { in tb_handle_dp_bandwidth_request()
2808 } else if (ret == -ENOTCONN) { in tb_handle_dp_bandwidth_request()
2817 if (ev->retry < TB_BW_ALLOC_RETRIES) { in tb_handle_dp_bandwidth_request()
2820 tb_queue_dp_bandwidth_request(tb, ev->route, in tb_handle_dp_bandwidth_request()
2821 ev->port, in tb_handle_dp_bandwidth_request()
2822 ev->retry + 1, in tb_handle_dp_bandwidth_request()
2844 mutex_unlock(&tb->lock); in tb_handle_dp_bandwidth_request()
2846 pm_runtime_mark_last_busy(&tb->dev); in tb_handle_dp_bandwidth_request()
2847 pm_runtime_put_autosuspend(&tb->dev); in tb_handle_dp_bandwidth_request()
2861 ev->tb = tb; in tb_queue_dp_bandwidth_request()
2862 ev->route = route; in tb_queue_dp_bandwidth_request()
2863 ev->port = port; in tb_queue_dp_bandwidth_request()
2864 ev->retry = retry; in tb_queue_dp_bandwidth_request()
2865 INIT_DELAYED_WORK(&ev->work, tb_handle_dp_bandwidth_request); in tb_queue_dp_bandwidth_request()
2866 queue_delayed_work(tb->wq, &ev->work, delay); in tb_queue_dp_bandwidth_request()
2873 switch (error->error) { in tb_handle_notification()
2877 if (tb_cfg_ack_notification(tb->ctl, route, error)) in tb_handle_notification()
2883 if (tb_cfg_ack_notification(tb->ctl, route, error)) in tb_handle_notification()
2886 tb_queue_dp_bandwidth_request(tb, route, error->port, 0, 0); in tb_handle_notification()
2896 * tb_schedule_hotplug_handler() - callback function for the control channel
2904 u64 route = tb_cfg_get_route(&pkg->header); in tb_handle_event()
2917 if (tb_cfg_ack_plug(tb->ctl, route, pkg->port, pkg->unplug)) { in tb_handle_event()
2919 pkg->port); in tb_handle_event()
2922 tb_queue_hotplug(tb, route, pkg->port, pkg->unplug); in tb_handle_event()
2931 cancel_delayed_work(&tcm->remove_work); in tb_stop()
2933 list_for_each_entry_safe(tunnel, n, &tcm->tunnel_list, list) { in tb_stop()
2943 tb_switch_remove(tb->root_switch); in tb_stop()
2944 tcm->hotplug_active = false; /* signal tb_handle_hotplug to quit */ in tb_stop()
2953 for (i = 0; i < ARRAY_SIZE(tcm->groups); i++) in tb_deinit()
2954 cancel_delayed_work_sync(&tcm->groups[i].release_work); in tb_deinit()
2967 if (sw->boot) in tb_scan_finalize_switch()
2968 sw->authorized = 1; in tb_scan_finalize_switch()
2971 kobject_uevent(&dev->kobj, KOBJ_ADD); in tb_scan_finalize_switch()
2984 tb->root_switch = tb_switch_alloc(tb, &tb->dev, 0); in tb_start()
2985 if (IS_ERR(tb->root_switch)) in tb_start()
2986 return PTR_ERR(tb->root_switch); in tb_start()
2996 tb->root_switch->no_nvm_upgrade = !tb_switch_is_usb4(tb->root_switch); in tb_start()
2998 tb->root_switch->rpm = tb_switch_is_usb4(tb->root_switch); in tb_start()
3000 ret = tb_switch_configure(tb->root_switch); in tb_start()
3002 tb_switch_put(tb->root_switch); in tb_start()
3007 ret = tb_switch_add(tb->root_switch); in tb_start()
3009 tb_switch_put(tb->root_switch); in tb_start()
3017 tb_switch_tmu_configure(tb->root_switch, TB_SWITCH_TMU_MODE_LOWRES); in tb_start()
3019 tb_switch_tmu_enable(tb->root_switch); in tb_start()
3027 if (reset && tb_switch_is_usb4(tb->root_switch)) { in tb_start()
3029 if (usb4_switch_version(tb->root_switch) == 1) in tb_start()
3030 tb_switch_reset(tb->root_switch); in tb_start()
3035 tb_scan_switch(tb->root_switch); in tb_start()
3046 tb_create_usb3_tunnels(tb->root_switch); in tb_start()
3048 tb_add_dp_resources(tb->root_switch); in tb_start()
3049 tb_switch_enter_redrive(tb->root_switch); in tb_start()
3051 device_for_each_child(&tb->root_switch->dev, NULL, in tb_start()
3055 tcm->hotplug_active = true; in tb_start()
3065 tb_switch_exit_redrive(tb->root_switch); in tb_suspend_noirq()
3066 tb_switch_suspend(tb->root_switch, false); in tb_suspend_noirq()
3067 tcm->hotplug_active = false; /* signal tb_handle_hotplug to quit */ in tb_suspend_noirq()
3078 if (sw->is_unplugged) in tb_restore_children()
3082 tb_sw_warn(sw, "failed to re-enable CL states\n"); in tb_restore_children()
3090 if (!tb_port_has_remote(port) && !port->xdomain) in tb_restore_children()
3093 if (port->remote) { in tb_restore_children()
3094 tb_switch_set_link_width(port->remote->sw, in tb_restore_children()
3095 port->remote->sw->link_width); in tb_restore_children()
3096 tb_switch_configure_link(port->remote->sw); in tb_restore_children()
3098 tb_restore_children(port->remote->sw); in tb_restore_children()
3099 } else if (port->xdomain) { in tb_restore_children()
3100 tb_port_configure_xdomain(port, port->xdomain); in tb_restore_children()
3115 * For non-USB4 hosts (Apple systems) remove any PCIe devices in tb_resume_noirq()
3118 if (!tb_switch_is_usb4(tb->root_switch)) in tb_resume_noirq()
3119 tb_switch_reset(tb->root_switch); in tb_resume_noirq()
3121 tb_switch_resume(tb->root_switch, false); in tb_resume_noirq()
3123 tb_free_unplugged_children(tb->root_switch); in tb_resume_noirq()
3124 tb_restore_children(tb->root_switch); in tb_resume_noirq()
3132 tb_switch_discover_tunnels(tb->root_switch, &tunnels, false); in tb_resume_noirq()
3140 /* Re-create our tunnels now */ in tb_resume_noirq()
3141 list_for_each_entry_safe(tunnel, n, &tcm->tunnel_list, list) { in tb_resume_noirq()
3142 /* USB3 requires delay before it can be re-activated */ in tb_resume_noirq()
3150 if (!list_empty(&tcm->tunnel_list)) { in tb_resume_noirq()
3158 tb_switch_enter_redrive(tb->root_switch); in tb_resume_noirq()
3160 tcm->hotplug_active = true; in tb_resume_noirq()
3174 if (port->xdomain && port->xdomain->is_unplugged) { in tb_free_unplugged_xdomains()
3176 tb_xdomain_remove(port->xdomain); in tb_free_unplugged_xdomains()
3178 port->xdomain = NULL; in tb_free_unplugged_xdomains()
3180 } else if (port->remote) { in tb_free_unplugged_xdomains()
3181 ret += tb_free_unplugged_xdomains(port->remote->sw); in tb_free_unplugged_xdomains()
3192 tcm->hotplug_active = false; in tb_freeze_noirq()
3200 tcm->hotplug_active = true; in tb_thaw_noirq()
3211 mutex_lock(&tb->lock); in tb_complete()
3212 if (tb_free_unplugged_xdomains(tb->root_switch)) in tb_complete()
3213 tb_scan_switch(tb->root_switch); in tb_complete()
3214 mutex_unlock(&tb->lock); in tb_complete()
3221 mutex_lock(&tb->lock); in tb_runtime_suspend()
3224 * re-entering redrive mode. in tb_runtime_suspend()
3227 tb_switch_exit_redrive(tb->root_switch); in tb_runtime_suspend()
3228 tb_switch_suspend(tb->root_switch, true); in tb_runtime_suspend()
3229 tcm->hotplug_active = false; in tb_runtime_suspend()
3230 mutex_unlock(&tb->lock); in tb_runtime_suspend()
3240 mutex_lock(&tb->lock); in tb_remove_work()
3241 if (tb->root_switch) { in tb_remove_work()
3242 tb_free_unplugged_children(tb->root_switch); in tb_remove_work()
3243 tb_free_unplugged_xdomains(tb->root_switch); in tb_remove_work()
3245 mutex_unlock(&tb->lock); in tb_remove_work()
3253 mutex_lock(&tb->lock); in tb_runtime_resume()
3254 tb_switch_resume(tb->root_switch, true); in tb_runtime_resume()
3256 tb_restore_children(tb->root_switch); in tb_runtime_resume()
3257 list_for_each_entry_safe(tunnel, n, &tcm->tunnel_list, list) in tb_runtime_resume()
3259 tb_switch_enter_redrive(tb->root_switch); in tb_runtime_resume()
3260 tcm->hotplug_active = true; in tb_runtime_resume()
3261 mutex_unlock(&tb->lock); in tb_runtime_resume()
3268 queue_delayed_work(tb->wq, &tcm->remove_work, msecs_to_jiffies(50)); in tb_runtime_resume()
3305 switch (nhi->pdev->device) { in tb_apple_add_links()
3315 upstream = pci_upstream_bridge(nhi->pdev); in tb_apple_add_links()
3329 * back to NHI so that PCIe tunnels can be re-established after in tb_apple_add_links()
3333 for_each_pci_bridge(pdev, upstream->subordinate) { in tb_apple_add_links()
3339 !pdev->is_hotplug_bridge) in tb_apple_add_links()
3342 link = device_link_add(&pdev->dev, &nhi->pdev->dev, in tb_apple_add_links()
3346 dev_dbg(&nhi->pdev->dev, "created link from %s\n", in tb_apple_add_links()
3347 dev_name(&pdev->dev)); in tb_apple_add_links()
3350 dev_warn(&nhi->pdev->dev, "device link creation from %s failed\n", in tb_apple_add_links()
3351 dev_name(&pdev->dev)); in tb_apple_add_links()
3368 tb->security_level = TB_SECURITY_USER; in tb_probe()
3370 tb->security_level = TB_SECURITY_NOPCIE; in tb_probe()
3372 tb->cm_ops = &tb_cm_ops; in tb_probe()
3375 INIT_LIST_HEAD(&tcm->tunnel_list); in tb_probe()
3376 INIT_LIST_HEAD(&tcm->dp_resources); in tb_probe()
3377 INIT_DELAYED_WORK(&tcm->remove_work, tb_remove_work); in tb_probe()