Lines Matching +full:dma +full:- +full:router
1 // SPDX-License-Identifier: GPL-2.0
3 * Thunderbolt driver - bus logic (NHI independent)
19 * struct tb_cm - Simple Thunderbolt connection manager
38 return ((void *)tcm - sizeof(struct tb)); in tcm_to_tb()
59 ev->tb = tb; in tb_queue_hotplug()
60 ev->route = route; in tb_queue_hotplug()
61 ev->port = port; in tb_queue_hotplug()
62 ev->unplug = unplug; in tb_queue_hotplug()
63 INIT_WORK(&ev->work, tb_handle_hotplug); in tb_queue_hotplug()
64 queue_work(tb->wq, &ev->work); in tb_queue_hotplug()
71 struct tb_cm *tcm = tb_priv(sw->tb); in tb_add_dp_resources()
81 list_add_tail(&port->list, &tcm->dp_resources); in tb_add_dp_resources()
88 struct tb_cm *tcm = tb_priv(sw->tb); in tb_remove_dp_resources()
94 tb_remove_dp_resources(port->remote->sw); in tb_remove_dp_resources()
97 list_for_each_entry_safe(port, tmp, &tcm->dp_resources, list) { in tb_remove_dp_resources()
98 if (port->sw == sw) { in tb_remove_dp_resources()
100 list_del_init(&port->list); in tb_remove_dp_resources()
107 struct tb *tb = sw->tb; in tb_discover_tunnels()
114 switch (port->config.type) { in tb_discover_tunnels()
135 struct tb_switch *parent = tunnel->dst_port->sw; in tb_discover_tunnels()
137 while (parent != tunnel->src_port->sw) { in tb_discover_tunnels()
138 parent->boot = true; in tb_discover_tunnels()
143 list_add_tail(&tunnel->list, &tcm->tunnel_list); in tb_discover_tunnels()
148 tb_discover_tunnels(port->remote->sw); in tb_discover_tunnels()
158 tb_port_disable(port->dual_link_port); in tb_port_configure_xdomain()
160 if (tb_switch_is_usb4(port->sw)) in tb_port_configure_xdomain()
167 if (tb_switch_is_usb4(port->sw)) in tb_port_unconfigure_xdomain()
172 tb_port_enable(port->dual_link_port); in tb_port_unconfigure_xdomain()
177 struct tb_switch *sw = port->sw; in tb_scan_xdomain()
178 struct tb *tb = sw->tb; in tb_scan_xdomain()
189 xd = tb_xdomain_alloc(tb, &sw->dev, route, tb->root_switch->uuid, in tb_scan_xdomain()
192 tb_port_at(route, sw)->xdomain = xd; in tb_scan_xdomain()
218 * tb_find_unused_port() - return the first inactive port on @sw
230 if (port->config.type != type) in tb_find_unused_port()
232 if (!port->cap_adap) in tb_find_unused_port()
259 list_for_each_entry(tunnel, &tcm->tunnel_list, list) { in tb_find_tunnel()
260 if (tunnel->type == type && in tb_find_tunnel()
261 ((src_port && src_port == tunnel->src_port) || in tb_find_tunnel()
262 (dst_port && dst_port == tunnel->dst_port))) { in tb_find_tunnel()
277 /* Pick the router that is deepest in the topology */ in tb_find_first_usb3_tunnel()
278 if (dst_port->sw->config.depth > src_port->sw->config.depth) in tb_find_first_usb3_tunnel()
279 sw = dst_port->sw; in tb_find_first_usb3_tunnel()
281 sw = src_port->sw; in tb_find_first_usb3_tunnel()
283 /* Can't be the host router */ in tb_find_first_usb3_tunnel()
284 if (sw == tb->root_switch) in tb_find_first_usb3_tunnel()
287 /* Find the downstream USB4 port that leads to this router */ in tb_find_first_usb3_tunnel()
288 port = tb_port_at(tb_route(sw), tb->root_switch); in tb_find_first_usb3_tunnel()
289 /* Find the corresponding host router USB3 downstream port */ in tb_find_first_usb3_tunnel()
290 usb3_down = usb4_switch_map_usb3_down(tb->root_switch, port); in tb_find_first_usb3_tunnel()
328 link_speed = port->sw->link_speed; in tb_available_bandwidth()
335 link_width = port->bonded ? 2 : 1; in tb_available_bandwidth()
339 up_bw -= up_bw / 10; in tb_available_bandwidth()
348 list_for_each_entry(tunnel, &tcm->tunnel_list, list) { in tb_available_bandwidth()
363 up_bw -= dp_consumed_up; in tb_available_bandwidth()
364 down_bw -= dp_consumed_down; in tb_available_bandwidth()
368 * If USB3 is tunneled from the host router down to the in tb_available_bandwidth()
373 up_bw -= usb3_consumed_up; in tb_available_bandwidth()
374 down_bw -= usb3_consumed_down; in tb_available_bandwidth()
416 ret = tb_available_bandwidth(tb, tunnel->src_port, tunnel->dst_port, in tb_reclaim_usb3_bandwidth()
441 if (!sw->link_usb4) in tb_tunnel_usb3()
481 ret = -ENOMEM; in tb_tunnel_usb3()
488 ret = -EIO; in tb_tunnel_usb3()
492 list_add_tail(&tunnel->list, &tcm->tunnel_list); in tb_tunnel_usb3()
513 ret = tb_tunnel_usb3(sw->tb, sw); in tb_create_usb3_tunnels()
521 ret = tb_create_usb3_tunnels(port->remote->sw); in tb_create_usb3_tunnels()
532 * tb_scan_switch() - scan for and initialize downstream switches
538 pm_runtime_get_sync(&sw->dev); in tb_scan_switch()
543 pm_runtime_mark_last_busy(&sw->dev); in tb_scan_switch()
544 pm_runtime_put_autosuspend(&sw->dev); in tb_scan_switch()
548 * tb_scan_port() - check for and initialize switches below port
552 struct tb_cm *tcm = tb_priv(port->sw->tb); in tb_scan_port()
562 tb_queue_hotplug(port->sw->tb, tb_route(port->sw), port->port, in tb_scan_port()
567 if (port->config.type != TB_TYPE_PORT) in tb_scan_port()
569 if (port->dual_link_port && port->link_nr) in tb_scan_port()
576 if (port->remote) { in tb_scan_port()
583 sw = tb_switch_alloc(port->sw->tb, &port->sw->dev, in tb_scan_port()
591 if (PTR_ERR(sw) == -EIO || PTR_ERR(sw) == -EADDRNOTAVAIL) in tb_scan_port()
605 if (port->xdomain) { in tb_scan_port()
606 tb_xdomain_remove(port->xdomain); in tb_scan_port()
608 port->xdomain = NULL; in tb_scan_port()
616 if (!tcm->hotplug_active) in tb_scan_port()
617 dev_set_uevent_suppress(&sw->dev, true); in tb_scan_port()
623 sw->rpm = sw->generation > 1; in tb_scan_port()
632 port->remote = upstream_port; in tb_scan_port()
633 upstream_port->remote = port; in tb_scan_port()
634 if (port->dual_link_port && upstream_port->dual_link_port) { in tb_scan_port()
635 port->dual_link_port->remote = upstream_port->dual_link_port; in tb_scan_port()
636 upstream_port->dual_link_port->remote = port->dual_link_port; in tb_scan_port()
656 if (tcm->hotplug_active && tb_tunnel_usb3(sw->tb, sw)) in tb_scan_port()
672 list_del(&tunnel->list); in tb_deactivate_and_free_tunnel()
674 tb = tunnel->tb; in tb_deactivate_and_free_tunnel()
675 src_port = tunnel->src_port; in tb_deactivate_and_free_tunnel()
676 dst_port = tunnel->dst_port; in tb_deactivate_and_free_tunnel()
678 switch (tunnel->type) { in tb_deactivate_and_free_tunnel()
684 tb_switch_dealloc_dp_resource(src_port->sw, src_port); in tb_deactivate_and_free_tunnel()
686 pm_runtime_mark_last_busy(&dst_port->sw->dev); in tb_deactivate_and_free_tunnel()
687 pm_runtime_put_autosuspend(&dst_port->sw->dev); in tb_deactivate_and_free_tunnel()
688 pm_runtime_mark_last_busy(&src_port->sw->dev); in tb_deactivate_and_free_tunnel()
689 pm_runtime_put_autosuspend(&src_port->sw->dev); in tb_deactivate_and_free_tunnel()
698 * PCIe and DMA tunnels do not consume guaranteed in tb_deactivate_and_free_tunnel()
708 * tb_free_invalid_tunnels() - destroy tunnels of devices that have gone away
716 list_for_each_entry_safe(tunnel, n, &tcm->tunnel_list, list) { in tb_free_invalid_tunnels()
723 * tb_free_unplugged_children() - traverse hierarchy and free unplugged switches
733 if (port->remote->sw->is_unplugged) { in tb_free_unplugged_children()
735 tb_remove_dp_resources(port->remote->sw); in tb_free_unplugged_children()
736 tb_switch_unconfigure_link(port->remote->sw); in tb_free_unplugged_children()
737 tb_switch_lane_bonding_disable(port->remote->sw); in tb_free_unplugged_children()
738 tb_switch_remove(port->remote->sw); in tb_free_unplugged_children()
739 port->remote = NULL; in tb_free_unplugged_children()
740 if (port->dual_link_port) in tb_free_unplugged_children()
741 port->dual_link_port->remote = NULL; in tb_free_unplugged_children()
743 tb_free_unplugged_children(port->remote->sw); in tb_free_unplugged_children()
760 int phy_port = tb_phy_port_from_link(port->port); in tb_find_pcie_down()
764 * Hard-coded Thunderbolt port to PCIe down port mapping in tb_find_pcie_down()
777 /* Validate the hard-coding */ in tb_find_pcie_down()
778 if (WARN_ON(index > sw->config.max_port_number)) in tb_find_pcie_down()
781 down = &sw->ports[index]; in tb_find_pcie_down()
802 host_port = tb_route(in->sw) ? in tb_find_dp_out()
803 tb_port_at(tb_route(in->sw), tb->root_switch) : NULL; in tb_find_dp_out()
805 list_for_each_entry(port, &tcm->dp_resources, list) { in tb_find_dp_out()
818 * the same host router downstream port. in tb_find_dp_out()
820 if (host_port && tb_route(port->sw)) { in tb_find_dp_out()
823 p = tb_port_at(tb_route(port->sw), tb->root_switch); in tb_find_dp_out()
845 tb_dbg(tb, "looking for DP IN <-> DP OUT pairs:\n"); in tb_tunnel_dp()
849 list_for_each_entry(port, &tcm->dp_resources, list) { in tb_tunnel_dp()
884 pm_runtime_get_sync(&in->sw->dev); in tb_tunnel_dp()
885 pm_runtime_get_sync(&out->sw->dev); in tb_tunnel_dp()
887 if (tb_switch_alloc_dp_resource(in->sw, in)) { in tb_tunnel_dp()
918 list_add_tail(&tunnel->list, &tcm->tunnel_list); in tb_tunnel_dp()
927 tb_switch_dealloc_dp_resource(in->sw, in); in tb_tunnel_dp()
929 pm_runtime_mark_last_busy(&out->sw->dev); in tb_tunnel_dp()
930 pm_runtime_put_autosuspend(&out->sw->dev); in tb_tunnel_dp()
931 pm_runtime_mark_last_busy(&in->sw->dev); in tb_tunnel_dp()
932 pm_runtime_put_autosuspend(&in->sw->dev); in tb_tunnel_dp()
952 list_del_init(&port->list); in tb_dp_resource_unavailable()
969 list_for_each_entry(p, &tcm->dp_resources, list) { in tb_dp_resource_available()
976 list_add_tail(&port->list, &tcm->dp_resources); in tb_dp_resource_available()
978 /* Look for suitable DP IN <-> DP OUT pairs now */ in tb_dp_resource_available()
989 * will be re-established after resume based on plug events. in tb_disconnect_and_release_dp()
991 list_for_each_entry_safe_reverse(tunnel, n, &tcm->tunnel_list, list) { in tb_disconnect_and_release_dp()
996 while (!list_empty(&tcm->dp_resources)) { in tb_disconnect_and_release_dp()
999 port = list_first_entry(&tcm->dp_resources, in tb_disconnect_and_release_dp()
1001 list_del_init(&port->list); in tb_disconnect_and_release_dp()
1020 parent_sw = tb_to_switch(sw->dev.parent); in tb_tunnel_pci()
1028 return -ENOMEM; in tb_tunnel_pci()
1034 return -EIO; in tb_tunnel_pci()
1037 list_add_tail(&tunnel->list, &tcm->tunnel_list); in tb_tunnel_pci()
1048 sw = tb_to_switch(xd->dev.parent); in tb_approve_xdomain_paths()
1049 dst_port = tb_port_at(xd->route, sw); in tb_approve_xdomain_paths()
1050 nhi_port = tb_switch_find_port(tb->root_switch, TB_TYPE_NHI); in tb_approve_xdomain_paths()
1052 mutex_lock(&tb->lock); in tb_approve_xdomain_paths()
1053 tunnel = tb_tunnel_alloc_dma(tb, nhi_port, dst_port, xd->transmit_ring, in tb_approve_xdomain_paths()
1054 xd->transmit_path, xd->receive_ring, in tb_approve_xdomain_paths()
1055 xd->receive_path); in tb_approve_xdomain_paths()
1057 mutex_unlock(&tb->lock); in tb_approve_xdomain_paths()
1058 return -ENOMEM; in tb_approve_xdomain_paths()
1063 "DMA tunnel activation failed, aborting\n"); in tb_approve_xdomain_paths()
1065 mutex_unlock(&tb->lock); in tb_approve_xdomain_paths()
1066 return -EIO; in tb_approve_xdomain_paths()
1069 list_add_tail(&tunnel->list, &tcm->tunnel_list); in tb_approve_xdomain_paths()
1070 mutex_unlock(&tb->lock); in tb_approve_xdomain_paths()
1080 sw = tb_to_switch(xd->dev.parent); in __tb_disconnect_xdomain_paths()
1081 dst_port = tb_port_at(xd->route, sw); in __tb_disconnect_xdomain_paths()
1094 if (!xd->is_unplugged) { in tb_disconnect_xdomain_paths()
1095 mutex_lock(&tb->lock); in tb_disconnect_xdomain_paths()
1097 mutex_unlock(&tb->lock); in tb_disconnect_xdomain_paths()
1105 * tb_handle_hotplug() - handle hotplug event
1107 * Executes on tb->wq.
1112 struct tb *tb = ev->tb; in tb_handle_hotplug()
1118 pm_runtime_get_sync(&tb->dev); in tb_handle_hotplug()
1120 mutex_lock(&tb->lock); in tb_handle_hotplug()
1121 if (!tcm->hotplug_active) in tb_handle_hotplug()
1124 sw = tb_switch_find_by_route(tb, ev->route); in tb_handle_hotplug()
1128 ev->route, ev->port, ev->unplug); in tb_handle_hotplug()
1131 if (ev->port > sw->config.max_port_number) { in tb_handle_hotplug()
1134 ev->route, ev->port, ev->unplug); in tb_handle_hotplug()
1137 port = &sw->ports[ev->port]; in tb_handle_hotplug()
1140 ev->route, ev->port, ev->unplug); in tb_handle_hotplug()
1144 pm_runtime_get_sync(&sw->dev); in tb_handle_hotplug()
1146 if (ev->unplug) { in tb_handle_hotplug()
1151 tb_sw_set_unplugged(port->remote->sw); in tb_handle_hotplug()
1153 tb_remove_dp_resources(port->remote->sw); in tb_handle_hotplug()
1154 tb_switch_tmu_disable(port->remote->sw); in tb_handle_hotplug()
1155 tb_switch_unconfigure_link(port->remote->sw); in tb_handle_hotplug()
1156 tb_switch_lane_bonding_disable(port->remote->sw); in tb_handle_hotplug()
1157 tb_switch_remove(port->remote->sw); in tb_handle_hotplug()
1158 port->remote = NULL; in tb_handle_hotplug()
1159 if (port->dual_link_port) in tb_handle_hotplug()
1160 port->dual_link_port->remote = NULL; in tb_handle_hotplug()
1163 } else if (port->xdomain) { in tb_handle_hotplug()
1164 struct tb_xdomain *xd = tb_xdomain_get(port->xdomain); in tb_handle_hotplug()
1174 xd->is_unplugged = true; in tb_handle_hotplug()
1176 port->xdomain = NULL; in tb_handle_hotplug()
1186 } else if (port->remote) { in tb_handle_hotplug()
1192 if (!port->remote) in tb_handle_hotplug()
1199 pm_runtime_mark_last_busy(&sw->dev); in tb_handle_hotplug()
1200 pm_runtime_put_autosuspend(&sw->dev); in tb_handle_hotplug()
1205 mutex_unlock(&tb->lock); in tb_handle_hotplug()
1207 pm_runtime_mark_last_busy(&tb->dev); in tb_handle_hotplug()
1208 pm_runtime_put_autosuspend(&tb->dev); in tb_handle_hotplug()
1214 * tb_schedule_hotplug_handler() - callback function for the control channel
1229 route = tb_cfg_get_route(&pkg->header); in tb_handle_event()
1231 if (tb_cfg_ack_plug(tb->ctl, route, pkg->port, pkg->unplug)) { in tb_handle_event()
1233 pkg->port); in tb_handle_event()
1236 tb_queue_hotplug(tb, route, pkg->port, pkg->unplug); in tb_handle_event()
1245 cancel_delayed_work(&tcm->remove_work); in tb_stop()
1247 list_for_each_entry_safe(tunnel, n, &tcm->tunnel_list, list) { in tb_stop()
1249 * DMA tunnels require the driver to be functional so we in tb_stop()
1257 tb_switch_remove(tb->root_switch); in tb_stop()
1258 tcm->hotplug_active = false; /* signal tb_handle_hotplug to quit */ in tb_stop()
1271 if (sw->boot) in tb_scan_finalize_switch()
1272 sw->authorized = 1; in tb_scan_finalize_switch()
1275 kobject_uevent(&dev->kobj, KOBJ_ADD); in tb_scan_finalize_switch()
1287 tb->root_switch = tb_switch_alloc(tb, &tb->dev, 0); in tb_start()
1288 if (IS_ERR(tb->root_switch)) in tb_start()
1289 return PTR_ERR(tb->root_switch); in tb_start()
1296 tb->root_switch->no_nvm_upgrade = true; in tb_start()
1298 tb->root_switch->rpm = tb_switch_is_usb4(tb->root_switch); in tb_start()
1300 ret = tb_switch_configure(tb->root_switch); in tb_start()
1302 tb_switch_put(tb->root_switch); in tb_start()
1307 ret = tb_switch_add(tb->root_switch); in tb_start()
1309 tb_switch_put(tb->root_switch); in tb_start()
1314 tb_switch_tmu_enable(tb->root_switch); in tb_start()
1316 tb_scan_switch(tb->root_switch); in tb_start()
1318 tb_discover_tunnels(tb->root_switch); in tb_start()
1323 tb_create_usb3_tunnels(tb->root_switch); in tb_start()
1325 tb_add_dp_resources(tb->root_switch); in tb_start()
1327 device_for_each_child(&tb->root_switch->dev, NULL, in tb_start()
1331 tcm->hotplug_active = true; in tb_start()
1341 tb_switch_suspend(tb->root_switch, false); in tb_suspend_noirq()
1342 tcm->hotplug_active = false; /* signal tb_handle_hotplug to quit */ in tb_suspend_noirq()
1352 /* No need to restore if the router is already unplugged */ in tb_restore_children()
1353 if (sw->is_unplugged) in tb_restore_children()
1360 if (!tb_port_has_remote(port) && !port->xdomain) in tb_restore_children()
1363 if (port->remote) { in tb_restore_children()
1364 tb_switch_lane_bonding_enable(port->remote->sw); in tb_restore_children()
1365 tb_switch_configure_link(port->remote->sw); in tb_restore_children()
1367 tb_restore_children(port->remote->sw); in tb_restore_children()
1368 } else if (port->xdomain) { in tb_restore_children()
1382 tb_switch_reset(tb->root_switch); in tb_resume_noirq()
1384 tb_switch_resume(tb->root_switch); in tb_resume_noirq()
1386 tb_free_unplugged_children(tb->root_switch); in tb_resume_noirq()
1387 tb_restore_children(tb->root_switch); in tb_resume_noirq()
1388 list_for_each_entry_safe(tunnel, n, &tcm->tunnel_list, list) in tb_resume_noirq()
1390 if (!list_empty(&tcm->tunnel_list)) { in tb_resume_noirq()
1399 tcm->hotplug_active = true; in tb_resume_noirq()
1413 if (port->xdomain && port->xdomain->is_unplugged) { in tb_free_unplugged_xdomains()
1415 tb_xdomain_remove(port->xdomain); in tb_free_unplugged_xdomains()
1417 port->xdomain = NULL; in tb_free_unplugged_xdomains()
1419 } else if (port->remote) { in tb_free_unplugged_xdomains()
1420 ret += tb_free_unplugged_xdomains(port->remote->sw); in tb_free_unplugged_xdomains()
1431 tcm->hotplug_active = false; in tb_freeze_noirq()
1439 tcm->hotplug_active = true; in tb_thaw_noirq()
1450 mutex_lock(&tb->lock); in tb_complete()
1451 if (tb_free_unplugged_xdomains(tb->root_switch)) in tb_complete()
1452 tb_scan_switch(tb->root_switch); in tb_complete()
1453 mutex_unlock(&tb->lock); in tb_complete()
1460 mutex_lock(&tb->lock); in tb_runtime_suspend()
1461 tb_switch_suspend(tb->root_switch, true); in tb_runtime_suspend()
1462 tcm->hotplug_active = false; in tb_runtime_suspend()
1463 mutex_unlock(&tb->lock); in tb_runtime_suspend()
1473 mutex_lock(&tb->lock); in tb_remove_work()
1474 if (tb->root_switch) { in tb_remove_work()
1475 tb_free_unplugged_children(tb->root_switch); in tb_remove_work()
1476 tb_free_unplugged_xdomains(tb->root_switch); in tb_remove_work()
1478 mutex_unlock(&tb->lock); in tb_remove_work()
1486 mutex_lock(&tb->lock); in tb_runtime_resume()
1487 tb_switch_resume(tb->root_switch); in tb_runtime_resume()
1489 tb_restore_children(tb->root_switch); in tb_runtime_resume()
1490 list_for_each_entry_safe(tunnel, n, &tcm->tunnel_list, list) in tb_runtime_resume()
1492 tcm->hotplug_active = true; in tb_runtime_resume()
1493 mutex_unlock(&tb->lock); in tb_runtime_resume()
1500 queue_delayed_work(tb->wq, &tcm->remove_work, msecs_to_jiffies(50)); in tb_runtime_resume()
1529 tb->security_level = TB_SECURITY_USER; in tb_probe()
1530 tb->cm_ops = &tb_cm_ops; in tb_probe()
1533 INIT_LIST_HEAD(&tcm->tunnel_list); in tb_probe()
1534 INIT_LIST_HEAD(&tcm->dp_resources); in tb_probe()
1535 INIT_DELAYED_WORK(&tcm->remove_work, tb_remove_work); in tb_probe()