Lines Matching +full:dma +full:- +full:router

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()
74 struct tb_cm *tcm = tb_priv(sw->tb); in tb_add_dp_resources()
84 list_add_tail(&port->list, &tcm->dp_resources); in tb_add_dp_resources()
91 struct tb_cm *tcm = tb_priv(sw->tb); 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()
110 struct tb *tb = sw->tb; in tb_discover_tunnels()
117 switch (port->config.type) { in tb_discover_tunnels()
138 struct tb_switch *parent = tunnel->dst_port->sw; in tb_discover_tunnels()
140 while (parent != tunnel->src_port->sw) { in tb_discover_tunnels()
141 parent->boot = true; in tb_discover_tunnels()
146 pm_runtime_get_sync(&tunnel->src_port->sw->dev); in tb_discover_tunnels()
147 pm_runtime_get_sync(&tunnel->dst_port->sw->dev); in tb_discover_tunnels()
150 list_add_tail(&tunnel->list, &tcm->tunnel_list); in tb_discover_tunnels()
155 tb_discover_tunnels(port->remote->sw); in tb_discover_tunnels()
165 tb_port_disable(port->dual_link_port); in tb_port_configure_xdomain()
167 if (tb_switch_is_usb4(port->sw)) in tb_port_configure_xdomain()
174 if (tb_switch_is_usb4(port->sw)) in tb_port_unconfigure_xdomain()
179 tb_port_enable(port->dual_link_port); in tb_port_unconfigure_xdomain()
184 struct tb_switch *sw = port->sw; in tb_scan_xdomain()
185 struct tb *tb = sw->tb; in tb_scan_xdomain()
199 xd = tb_xdomain_alloc(tb, &sw->dev, route, tb->root_switch->uuid, in tb_scan_xdomain()
202 tb_port_at(route, sw)->xdomain = xd; in tb_scan_xdomain()
228 * tb_find_unused_port() - return the first inactive port on @sw
240 if (port->config.type != type) in tb_find_unused_port()
242 if (!port->cap_adap) in tb_find_unused_port()
269 list_for_each_entry(tunnel, &tcm->tunnel_list, list) { in tb_find_tunnel()
270 if (tunnel->type == type && in tb_find_tunnel()
271 ((src_port && src_port == tunnel->src_port) || in tb_find_tunnel()
272 (dst_port && dst_port == tunnel->dst_port))) { in tb_find_tunnel()
287 /* Pick the router that is deepest in the topology */ in tb_find_first_usb3_tunnel()
288 if (dst_port->sw->config.depth > src_port->sw->config.depth) in tb_find_first_usb3_tunnel()
289 sw = dst_port->sw; in tb_find_first_usb3_tunnel()
291 sw = src_port->sw; in tb_find_first_usb3_tunnel()
293 /* Can't be the host router */ in tb_find_first_usb3_tunnel()
294 if (sw == tb->root_switch) in tb_find_first_usb3_tunnel()
297 /* Find the downstream USB4 port that leads to this router */ in tb_find_first_usb3_tunnel()
298 port = tb_port_at(tb_route(sw), tb->root_switch); in tb_find_first_usb3_tunnel()
299 /* Find the corresponding host router USB3 downstream port */ in tb_find_first_usb3_tunnel()
300 usb3_down = usb4_switch_map_usb3_down(tb->root_switch, port); in tb_find_first_usb3_tunnel()
338 link_speed = port->sw->link_speed; in tb_available_bandwidth()
345 link_width = port->bonded ? 2 : 1; in tb_available_bandwidth()
349 up_bw -= up_bw / 10; in tb_available_bandwidth()
358 list_for_each_entry(tunnel, &tcm->tunnel_list, list) { in tb_available_bandwidth()
373 up_bw -= dp_consumed_up; in tb_available_bandwidth()
374 down_bw -= dp_consumed_down; in tb_available_bandwidth()
378 * If USB3 is tunneled from the host router down to the in tb_available_bandwidth()
383 up_bw -= usb3_consumed_up; in tb_available_bandwidth()
384 down_bw -= usb3_consumed_down; in tb_available_bandwidth()
426 ret = tb_available_bandwidth(tb, tunnel->src_port, tunnel->dst_port, in tb_reclaim_usb3_bandwidth()
456 if (!sw->link_usb4) in tb_tunnel_usb3()
496 ret = -ENOMEM; in tb_tunnel_usb3()
503 ret = -EIO; in tb_tunnel_usb3()
507 list_add_tail(&tunnel->list, &tcm->tunnel_list); in tb_tunnel_usb3()
531 ret = tb_tunnel_usb3(sw->tb, sw); in tb_create_usb3_tunnels()
539 ret = tb_create_usb3_tunnels(port->remote->sw); in tb_create_usb3_tunnels()
550 * tb_scan_switch() - scan for and initialize downstream switches
556 pm_runtime_get_sync(&sw->dev); in tb_scan_switch()
561 pm_runtime_mark_last_busy(&sw->dev); in tb_scan_switch()
562 pm_runtime_put_autosuspend(&sw->dev); in tb_scan_switch()
566 * tb_scan_port() - check for and initialize switches below port
570 struct tb_cm *tcm = tb_priv(port->sw->tb); in tb_scan_port()
580 tb_queue_hotplug(port->sw->tb, tb_route(port->sw), port->port, in tb_scan_port()
585 if (port->config.type != TB_TYPE_PORT) in tb_scan_port()
587 if (port->dual_link_port && port->link_nr) in tb_scan_port()
594 if (port->remote) { in tb_scan_port()
601 sw = tb_switch_alloc(port->sw->tb, &port->sw->dev, in tb_scan_port()
609 if (PTR_ERR(sw) == -EIO || PTR_ERR(sw) == -EADDRNOTAVAIL) in tb_scan_port()
623 if (port->xdomain) { in tb_scan_port()
624 tb_xdomain_remove(port->xdomain); in tb_scan_port()
626 port->xdomain = NULL; in tb_scan_port()
634 if (!tcm->hotplug_active) in tb_scan_port()
635 dev_set_uevent_suppress(&sw->dev, true); in tb_scan_port()
641 sw->rpm = sw->generation > 1; in tb_scan_port()
650 port->remote = upstream_port; in tb_scan_port()
651 upstream_port->remote = port; in tb_scan_port()
652 if (port->dual_link_port && upstream_port->dual_link_port) { in tb_scan_port()
653 port->dual_link_port->remote = upstream_port->dual_link_port; in tb_scan_port()
654 upstream_port->dual_link_port->remote = port->dual_link_port; in tb_scan_port()
674 if (tcm->hotplug_active && tb_tunnel_usb3(sw->tb, sw)) in tb_scan_port()
690 list_del(&tunnel->list); in tb_deactivate_and_free_tunnel()
692 tb = tunnel->tb; in tb_deactivate_and_free_tunnel()
693 src_port = tunnel->src_port; in tb_deactivate_and_free_tunnel()
694 dst_port = tunnel->dst_port; in tb_deactivate_and_free_tunnel()
696 switch (tunnel->type) { in tb_deactivate_and_free_tunnel()
702 tb_switch_dealloc_dp_resource(src_port->sw, src_port); in tb_deactivate_and_free_tunnel()
704 pm_runtime_mark_last_busy(&dst_port->sw->dev); in tb_deactivate_and_free_tunnel()
705 pm_runtime_put_autosuspend(&dst_port->sw->dev); in tb_deactivate_and_free_tunnel()
706 pm_runtime_mark_last_busy(&src_port->sw->dev); in tb_deactivate_and_free_tunnel()
707 pm_runtime_put_autosuspend(&src_port->sw->dev); in tb_deactivate_and_free_tunnel()
716 * PCIe and DMA tunnels do not consume guaranteed in tb_deactivate_and_free_tunnel()
726 * tb_free_invalid_tunnels() - destroy tunnels of devices that have gone away
734 list_for_each_entry_safe(tunnel, n, &tcm->tunnel_list, list) { in tb_free_invalid_tunnels()
741 * tb_free_unplugged_children() - traverse hierarchy and free unplugged switches
751 if (port->remote->sw->is_unplugged) { in tb_free_unplugged_children()
753 tb_remove_dp_resources(port->remote->sw); in tb_free_unplugged_children()
754 tb_switch_unconfigure_link(port->remote->sw); in tb_free_unplugged_children()
755 tb_switch_lane_bonding_disable(port->remote->sw); in tb_free_unplugged_children()
756 tb_switch_remove(port->remote->sw); in tb_free_unplugged_children()
757 port->remote = NULL; in tb_free_unplugged_children()
758 if (port->dual_link_port) in tb_free_unplugged_children()
759 port->dual_link_port->remote = NULL; in tb_free_unplugged_children()
761 tb_free_unplugged_children(port->remote->sw); in tb_free_unplugged_children()
778 int phy_port = tb_phy_port_from_link(port->port); in tb_find_pcie_down()
782 * Hard-coded Thunderbolt port to PCIe down port mapping in tb_find_pcie_down()
795 /* Validate the hard-coding */ in tb_find_pcie_down()
796 if (WARN_ON(index > sw->config.max_port_number)) in tb_find_pcie_down()
799 down = &sw->ports[index]; in tb_find_pcie_down()
820 host_port = tb_route(in->sw) ? in tb_find_dp_out()
821 tb_port_at(tb_route(in->sw), tb->root_switch) : NULL; in tb_find_dp_out()
823 list_for_each_entry(port, &tcm->dp_resources, list) { in tb_find_dp_out()
836 * the same host router downstream port. in tb_find_dp_out()
838 if (host_port && tb_route(port->sw)) { in tb_find_dp_out()
841 p = tb_port_at(tb_route(port->sw), tb->root_switch); in tb_find_dp_out()
868 tb_dbg(tb, "looking for DP IN <-> DP OUT pairs:\n"); in tb_tunnel_dp()
872 list_for_each_entry(port, &tcm->dp_resources, list) { in tb_tunnel_dp()
907 pm_runtime_get_sync(&in->sw->dev); in tb_tunnel_dp()
908 pm_runtime_get_sync(&out->sw->dev); in tb_tunnel_dp()
910 if (tb_switch_alloc_dp_resource(in->sw, in)) { in tb_tunnel_dp()
941 list_add_tail(&tunnel->list, &tcm->tunnel_list); in tb_tunnel_dp()
950 tb_switch_dealloc_dp_resource(in->sw, in); in tb_tunnel_dp()
952 pm_runtime_mark_last_busy(&out->sw->dev); in tb_tunnel_dp()
953 pm_runtime_put_autosuspend(&out->sw->dev); in tb_tunnel_dp()
954 pm_runtime_mark_last_busy(&in->sw->dev); in tb_tunnel_dp()
955 pm_runtime_put_autosuspend(&in->sw->dev); in tb_tunnel_dp()
975 list_del_init(&port->list); in tb_dp_resource_unavailable()
992 list_for_each_entry(p, &tcm->dp_resources, list) { in tb_dp_resource_available()
999 list_add_tail(&port->list, &tcm->dp_resources); in tb_dp_resource_available()
1001 /* Look for suitable DP IN <-> DP OUT pairs now */ in tb_dp_resource_available()
1012 * will be re-established after resume based on plug events. in tb_disconnect_and_release_dp()
1014 list_for_each_entry_safe_reverse(tunnel, n, &tcm->tunnel_list, list) { in tb_disconnect_and_release_dp()
1019 while (!list_empty(&tcm->dp_resources)) { in tb_disconnect_and_release_dp()
1022 port = list_first_entry(&tcm->dp_resources, in tb_disconnect_and_release_dp()
1024 list_del_init(&port->list); in tb_disconnect_and_release_dp()
1035 return -ENODEV; in tb_disconnect_pci()
1039 return -ENODEV; in tb_disconnect_pci()
1042 list_del(&tunnel->list); in tb_disconnect_pci()
1062 parent_sw = tb_to_switch(sw->dev.parent); in tb_tunnel_pci()
1070 return -ENOMEM; in tb_tunnel_pci()
1076 return -EIO; in tb_tunnel_pci()
1079 list_add_tail(&tunnel->list, &tcm->tunnel_list); in tb_tunnel_pci()
1092 sw = tb_to_switch(xd->dev.parent); in tb_approve_xdomain_paths()
1093 dst_port = tb_port_at(xd->route, sw); in tb_approve_xdomain_paths()
1094 nhi_port = tb_switch_find_port(tb->root_switch, TB_TYPE_NHI); in tb_approve_xdomain_paths()
1096 mutex_lock(&tb->lock); in tb_approve_xdomain_paths()
1100 mutex_unlock(&tb->lock); in tb_approve_xdomain_paths()
1101 return -ENOMEM; in tb_approve_xdomain_paths()
1106 "DMA tunnel activation failed, aborting\n"); in tb_approve_xdomain_paths()
1108 mutex_unlock(&tb->lock); in tb_approve_xdomain_paths()
1109 return -EIO; in tb_approve_xdomain_paths()
1112 list_add_tail(&tunnel->list, &tcm->tunnel_list); in tb_approve_xdomain_paths()
1113 mutex_unlock(&tb->lock); in tb_approve_xdomain_paths()
1126 sw = tb_to_switch(xd->dev.parent); in __tb_disconnect_xdomain_paths()
1127 dst_port = tb_port_at(xd->route, sw); in __tb_disconnect_xdomain_paths()
1128 nhi_port = tb_switch_find_port(tb->root_switch, TB_TYPE_NHI); in __tb_disconnect_xdomain_paths()
1130 list_for_each_entry_safe(tunnel, n, &tcm->tunnel_list, list) { in __tb_disconnect_xdomain_paths()
1133 if (tunnel->src_port != nhi_port || tunnel->dst_port != dst_port) in __tb_disconnect_xdomain_paths()
1146 if (!xd->is_unplugged) { in tb_disconnect_xdomain_paths()
1147 mutex_lock(&tb->lock); in tb_disconnect_xdomain_paths()
1151 mutex_unlock(&tb->lock); in tb_disconnect_xdomain_paths()
1159 * tb_handle_hotplug() - handle hotplug event
1161 * Executes on tb->wq.
1166 struct tb *tb = ev->tb; in tb_handle_hotplug()
1172 pm_runtime_get_sync(&tb->dev); in tb_handle_hotplug()
1174 mutex_lock(&tb->lock); in tb_handle_hotplug()
1175 if (!tcm->hotplug_active) in tb_handle_hotplug()
1178 sw = tb_switch_find_by_route(tb, ev->route); in tb_handle_hotplug()
1182 ev->route, ev->port, ev->unplug); in tb_handle_hotplug()
1185 if (ev->port > sw->config.max_port_number) { in tb_handle_hotplug()
1188 ev->route, ev->port, ev->unplug); in tb_handle_hotplug()
1191 port = &sw->ports[ev->port]; in tb_handle_hotplug()
1194 ev->route, ev->port, ev->unplug); in tb_handle_hotplug()
1198 pm_runtime_get_sync(&sw->dev); in tb_handle_hotplug()
1200 if (ev->unplug) { in tb_handle_hotplug()
1205 tb_sw_set_unplugged(port->remote->sw); in tb_handle_hotplug()
1207 tb_remove_dp_resources(port->remote->sw); in tb_handle_hotplug()
1208 tb_switch_tmu_disable(port->remote->sw); in tb_handle_hotplug()
1209 tb_switch_unconfigure_link(port->remote->sw); in tb_handle_hotplug()
1210 tb_switch_lane_bonding_disable(port->remote->sw); in tb_handle_hotplug()
1211 tb_switch_remove(port->remote->sw); in tb_handle_hotplug()
1212 port->remote = NULL; in tb_handle_hotplug()
1213 if (port->dual_link_port) in tb_handle_hotplug()
1214 port->dual_link_port->remote = NULL; in tb_handle_hotplug()
1217 } else if (port->xdomain) { in tb_handle_hotplug()
1218 struct tb_xdomain *xd = tb_xdomain_get(port->xdomain); in tb_handle_hotplug()
1228 xd->is_unplugged = true; in tb_handle_hotplug()
1230 port->xdomain = NULL; in tb_handle_hotplug()
1231 __tb_disconnect_xdomain_paths(tb, xd, -1, -1, -1, -1); in tb_handle_hotplug()
1240 } else if (port->remote) { in tb_handle_hotplug()
1246 if (!port->remote) in tb_handle_hotplug()
1253 pm_runtime_mark_last_busy(&sw->dev); in tb_handle_hotplug()
1254 pm_runtime_put_autosuspend(&sw->dev); in tb_handle_hotplug()
1259 mutex_unlock(&tb->lock); in tb_handle_hotplug()
1261 pm_runtime_mark_last_busy(&tb->dev); in tb_handle_hotplug()
1262 pm_runtime_put_autosuspend(&tb->dev); in tb_handle_hotplug()
1268 * tb_schedule_hotplug_handler() - callback function for the control channel
1283 route = tb_cfg_get_route(&pkg->header); in tb_handle_event()
1285 if (tb_cfg_ack_plug(tb->ctl, route, pkg->port, pkg->unplug)) { in tb_handle_event()
1287 pkg->port); in tb_handle_event()
1290 tb_queue_hotplug(tb, route, pkg->port, pkg->unplug); in tb_handle_event()
1299 cancel_delayed_work(&tcm->remove_work); in tb_stop()
1301 list_for_each_entry_safe(tunnel, n, &tcm->tunnel_list, list) { in tb_stop()
1303 * DMA tunnels require the driver to be functional so we in tb_stop()
1311 tb_switch_remove(tb->root_switch); in tb_stop()
1312 tcm->hotplug_active = false; /* signal tb_handle_hotplug to quit */ in tb_stop()
1325 if (sw->boot) in tb_scan_finalize_switch()
1326 sw->authorized = 1; in tb_scan_finalize_switch()
1329 kobject_uevent(&dev->kobj, KOBJ_ADD); in tb_scan_finalize_switch()
1341 tb->root_switch = tb_switch_alloc(tb, &tb->dev, 0); in tb_start()
1342 if (IS_ERR(tb->root_switch)) in tb_start()
1343 return PTR_ERR(tb->root_switch); in tb_start()
1350 tb->root_switch->no_nvm_upgrade = true; in tb_start()
1352 tb->root_switch->rpm = tb_switch_is_usb4(tb->root_switch); in tb_start()
1354 ret = tb_switch_configure(tb->root_switch); in tb_start()
1356 tb_switch_put(tb->root_switch); in tb_start()
1361 ret = tb_switch_add(tb->root_switch); in tb_start()
1363 tb_switch_put(tb->root_switch); in tb_start()
1368 tb_switch_tmu_enable(tb->root_switch); in tb_start()
1370 tb_scan_switch(tb->root_switch); in tb_start()
1372 tb_discover_tunnels(tb->root_switch); in tb_start()
1377 tb_create_usb3_tunnels(tb->root_switch); in tb_start()
1379 tb_add_dp_resources(tb->root_switch); in tb_start()
1381 device_for_each_child(&tb->root_switch->dev, NULL, in tb_start()
1385 tcm->hotplug_active = true; in tb_start()
1395 tb_switch_suspend(tb->root_switch, false); in tb_suspend_noirq()
1396 tcm->hotplug_active = false; /* signal tb_handle_hotplug to quit */ in tb_suspend_noirq()
1406 /* No need to restore if the router is already unplugged */ in tb_restore_children()
1407 if (sw->is_unplugged) in tb_restore_children()
1414 if (!tb_port_has_remote(port) && !port->xdomain) in tb_restore_children()
1417 if (port->remote) { in tb_restore_children()
1418 tb_switch_lane_bonding_enable(port->remote->sw); in tb_restore_children()
1419 tb_switch_configure_link(port->remote->sw); in tb_restore_children()
1421 tb_restore_children(port->remote->sw); in tb_restore_children()
1422 } else if (port->xdomain) { in tb_restore_children()
1436 tb_switch_reset(tb->root_switch); in tb_resume_noirq()
1438 tb_switch_resume(tb->root_switch); in tb_resume_noirq()
1440 tb_free_unplugged_children(tb->root_switch); in tb_resume_noirq()
1441 tb_restore_children(tb->root_switch); in tb_resume_noirq()
1442 list_for_each_entry_safe(tunnel, n, &tcm->tunnel_list, list) in tb_resume_noirq()
1444 if (!list_empty(&tcm->tunnel_list)) { in tb_resume_noirq()
1453 tcm->hotplug_active = true; in tb_resume_noirq()
1467 if (port->xdomain && port->xdomain->is_unplugged) { in tb_free_unplugged_xdomains()
1469 tb_xdomain_remove(port->xdomain); in tb_free_unplugged_xdomains()
1471 port->xdomain = NULL; in tb_free_unplugged_xdomains()
1473 } else if (port->remote) { in tb_free_unplugged_xdomains()
1474 ret += tb_free_unplugged_xdomains(port->remote->sw); in tb_free_unplugged_xdomains()
1485 tcm->hotplug_active = false; in tb_freeze_noirq()
1493 tcm->hotplug_active = true; in tb_thaw_noirq()
1504 mutex_lock(&tb->lock); in tb_complete()
1505 if (tb_free_unplugged_xdomains(tb->root_switch)) in tb_complete()
1506 tb_scan_switch(tb->root_switch); in tb_complete()
1507 mutex_unlock(&tb->lock); in tb_complete()
1514 mutex_lock(&tb->lock); in tb_runtime_suspend()
1515 tb_switch_suspend(tb->root_switch, true); in tb_runtime_suspend()
1516 tcm->hotplug_active = false; in tb_runtime_suspend()
1517 mutex_unlock(&tb->lock); in tb_runtime_suspend()
1527 mutex_lock(&tb->lock); in tb_remove_work()
1528 if (tb->root_switch) { in tb_remove_work()
1529 tb_free_unplugged_children(tb->root_switch); in tb_remove_work()
1530 tb_free_unplugged_xdomains(tb->root_switch); in tb_remove_work()
1532 mutex_unlock(&tb->lock); in tb_remove_work()
1540 mutex_lock(&tb->lock); in tb_runtime_resume()
1541 tb_switch_resume(tb->root_switch); in tb_runtime_resume()
1543 tb_restore_children(tb->root_switch); in tb_runtime_resume()
1544 list_for_each_entry_safe(tunnel, n, &tcm->tunnel_list, list) in tb_runtime_resume()
1546 tcm->hotplug_active = true; in tb_runtime_resume()
1547 mutex_unlock(&tb->lock); in tb_runtime_resume()
1554 queue_delayed_work(tb->wq, &tcm->remove_work, msecs_to_jiffies(50)); in tb_runtime_resume()
1589 switch (nhi->pdev->device) { in tb_apple_add_links()
1599 upstream = pci_upstream_bridge(nhi->pdev); in tb_apple_add_links()
1613 * back to NHI so that PCIe tunnels can be re-established after in tb_apple_add_links()
1616 for_each_pci_bridge(pdev, upstream->subordinate) { in tb_apple_add_links()
1622 !pdev->is_hotplug_bridge) in tb_apple_add_links()
1625 link = device_link_add(&pdev->dev, &nhi->pdev->dev, in tb_apple_add_links()
1629 dev_dbg(&nhi->pdev->dev, "created link from %s\n", in tb_apple_add_links()
1630 dev_name(&pdev->dev)); in tb_apple_add_links()
1632 dev_warn(&nhi->pdev->dev, "device link creation from %s failed\n", in tb_apple_add_links()
1633 dev_name(&pdev->dev)); in tb_apple_add_links()
1648 tb->security_level = TB_SECURITY_USER; in tb_probe()
1650 tb->security_level = TB_SECURITY_NOPCIE; in tb_probe()
1652 tb->cm_ops = &tb_cm_ops; in tb_probe()
1655 INIT_LIST_HEAD(&tcm->tunnel_list); in tb_probe()
1656 INIT_LIST_HEAD(&tcm->dp_resources); in tb_probe()
1657 INIT_DELAYED_WORK(&tcm->remove_work, tb_remove_work); in tb_probe()