1 // SPDX-License-Identifier: GPL-2.0
3 * Thunderbolt driver - bus logic (NHI independent)
5 * Copyright (c) 2014 Andreas Noever <andreas.noever@gmail.com>
6 * Copyright (C) 2019, Intel Corporation
9 #include <linux/slab.h>
10 #include <linux/errno.h>
11 #include <linux/delay.h>
12 #include <linux/pm_runtime.h>
13 #include <linux/platform_data/x86/apple.h>
19 #define TB_TIMEOUT 100 /* ms */
22 * struct tb_cm - Simple Thunderbolt connection manager
23 * @tunnel_list: List of active tunnels
24 * @dp_resources: List of available DP resources for DP tunneling
25 * @hotplug_active: tb_handle_hotplug will stop progressing plug
26 * events and exit if this is not set (it needs to
27 * acquire the lock one more time). Used to drain wq
28 * after cfg has been paused.
29 * @remove_work: Work used to remove any unplugged routers after
33 struct list_head tunnel_list;
34 struct list_head dp_resources;
36 struct delayed_work remove_work;
39 static inline struct tb *tcm_to_tb(struct tb_cm *tcm)
41 return ((void *)tcm - sizeof(struct tb));
44 struct tb_hotplug_event {
45 struct work_struct work;
52 static void tb_handle_hotplug(struct work_struct *work);
54 static void tb_queue_hotplug(struct tb *tb, u64 route, u8 port, bool unplug)
56 struct tb_hotplug_event *ev;
58 ev = kmalloc(sizeof(*ev), GFP_KERNEL);
66 INIT_WORK(&ev->work, tb_handle_hotplug);
67 queue_work(tb->wq, &ev->work);
70 /* enumeration & hot plug handling */
72 static void tb_add_dp_resources(struct tb_switch *sw)
74 struct tb_cm *tcm = tb_priv(sw->tb);
77 tb_switch_for_each_port(sw, port) {
78 if (!tb_port_is_dpin(port))
81 if (!tb_switch_query_dp_resource(sw, port))
84 list_add_tail(&port->list, &tcm->dp_resources);
85 tb_port_dbg(port, "DP IN resource available\n");
89 static void tb_remove_dp_resources(struct tb_switch *sw)
91 struct tb_cm *tcm = tb_priv(sw->tb);
92 struct tb_port *port, *tmp;
94 /* Clear children resources first */
95 tb_switch_for_each_port(sw, port) {
96 if (tb_port_has_remote(port))
97 tb_remove_dp_resources(port->remote->sw);
100 list_for_each_entry_safe(port, tmp, &tcm->dp_resources, list) {
101 if (port->sw == sw) {
102 tb_port_dbg(port, "DP OUT resource unavailable\n");
103 list_del_init(&port->list);
108 static void tb_discover_tunnels(struct tb_switch *sw)
110 struct tb *tb = sw->tb;
111 struct tb_cm *tcm = tb_priv(tb);
112 struct tb_port *port;
114 tb_switch_for_each_port(sw, port) {
115 struct tb_tunnel *tunnel = NULL;
117 switch (port->config.type) {
118 case TB_TYPE_DP_HDMI_IN:
119 tunnel = tb_tunnel_discover_dp(tb, port);
122 case TB_TYPE_PCIE_DOWN:
123 tunnel = tb_tunnel_discover_pci(tb, port);
126 case TB_TYPE_USB3_DOWN:
127 tunnel = tb_tunnel_discover_usb3(tb, port);
137 if (tb_tunnel_is_pci(tunnel)) {
138 struct tb_switch *parent = tunnel->dst_port->sw;
140 while (parent != tunnel->src_port->sw) {
142 parent = tb_switch_parent(parent);
144 } else if (tb_tunnel_is_dp(tunnel)) {
145 /* Keep the domain from powering down */
146 pm_runtime_get_sync(&tunnel->src_port->sw->dev);
147 pm_runtime_get_sync(&tunnel->dst_port->sw->dev);
150 list_add_tail(&tunnel->list, &tcm->tunnel_list);
153 tb_switch_for_each_port(sw, port) {
154 if (tb_port_has_remote(port))
155 tb_discover_tunnels(port->remote->sw);
159 static int tb_port_configure_xdomain(struct tb_port *port)
162 * XDomain paths currently only support single lane so we must
163 * disable the other lane according to USB4 spec.
165 tb_port_disable(port->dual_link_port);
167 if (tb_switch_is_usb4(port->sw))
168 return usb4_port_configure_xdomain(port);
169 return tb_lc_configure_xdomain(port);
172 static void tb_port_unconfigure_xdomain(struct tb_port *port)
174 if (tb_switch_is_usb4(port->sw))
175 usb4_port_unconfigure_xdomain(port);
177 tb_lc_unconfigure_xdomain(port);
179 tb_port_enable(port->dual_link_port);
182 static void tb_scan_xdomain(struct tb_port *port)
184 struct tb_switch *sw = port->sw;
185 struct tb *tb = sw->tb;
186 struct tb_xdomain *xd;
189 if (!tb_is_xdomain_enabled())
192 route = tb_downstream_route(port);
193 xd = tb_xdomain_find_by_route(tb, route);
199 xd = tb_xdomain_alloc(tb, &sw->dev, route, tb->root_switch->uuid,
202 tb_port_at(route, sw)->xdomain = xd;
203 tb_port_configure_xdomain(port);
208 static int tb_enable_tmu(struct tb_switch *sw)
212 /* If it is already enabled in correct mode, don't touch it */
213 if (tb_switch_tmu_is_enabled(sw))
216 ret = tb_switch_tmu_disable(sw);
220 ret = tb_switch_tmu_post_time(sw);
224 return tb_switch_tmu_enable(sw);
228 * tb_find_unused_port() - return the first inactive port on @sw
229 * @sw: Switch to find the port on
230 * @type: Port type to look for
232 static struct tb_port *tb_find_unused_port(struct tb_switch *sw,
233 enum tb_port_type type)
235 struct tb_port *port;
237 tb_switch_for_each_port(sw, port) {
238 if (tb_is_upstream_port(port))
240 if (port->config.type != type)
244 if (tb_port_is_enabled(port))
251 static struct tb_port *tb_find_usb3_down(struct tb_switch *sw,
252 const struct tb_port *port)
254 struct tb_port *down;
256 down = usb4_switch_map_usb3_down(sw, port);
257 if (down && !tb_usb3_port_is_enabled(down))
262 static struct tb_tunnel *tb_find_tunnel(struct tb *tb, enum tb_tunnel_type type,
263 struct tb_port *src_port,
264 struct tb_port *dst_port)
266 struct tb_cm *tcm = tb_priv(tb);
267 struct tb_tunnel *tunnel;
269 list_for_each_entry(tunnel, &tcm->tunnel_list, list) {
270 if (tunnel->type == type &&
271 ((src_port && src_port == tunnel->src_port) ||
272 (dst_port && dst_port == tunnel->dst_port))) {
280 static struct tb_tunnel *tb_find_first_usb3_tunnel(struct tb *tb,
281 struct tb_port *src_port,
282 struct tb_port *dst_port)
284 struct tb_port *port, *usb3_down;
285 struct tb_switch *sw;
287 /* Pick the router that is deepest in the topology */
288 if (dst_port->sw->config.depth > src_port->sw->config.depth)
293 /* Can't be the host router */
294 if (sw == tb->root_switch)
297 /* Find the downstream USB4 port that leads to this router */
298 port = tb_port_at(tb_route(sw), tb->root_switch);
299 /* Find the corresponding host router USB3 downstream port */
300 usb3_down = usb4_switch_map_usb3_down(tb->root_switch, port);
304 return tb_find_tunnel(tb, TB_TUNNEL_USB3, usb3_down, NULL);
307 static int tb_available_bandwidth(struct tb *tb, struct tb_port *src_port,
308 struct tb_port *dst_port, int *available_up, int *available_down)
310 int usb3_consumed_up, usb3_consumed_down, ret;
311 struct tb_cm *tcm = tb_priv(tb);
312 struct tb_tunnel *tunnel;
313 struct tb_port *port;
315 tb_port_dbg(dst_port, "calculating available bandwidth\n");
317 tunnel = tb_find_first_usb3_tunnel(tb, src_port, dst_port);
319 ret = tb_tunnel_consumed_bandwidth(tunnel, &usb3_consumed_up,
320 &usb3_consumed_down);
324 usb3_consumed_up = 0;
325 usb3_consumed_down = 0;
328 *available_up = *available_down = 40000;
330 /* Find the minimum available bandwidth over all links */
331 tb_for_each_port_on_path(src_port, dst_port, port) {
332 int link_speed, link_width, up_bw, down_bw;
334 if (!tb_port_is_null(port))
337 if (tb_is_upstream_port(port)) {
338 link_speed = port->sw->link_speed;
340 link_speed = tb_port_get_link_speed(port);
345 link_width = port->bonded ? 2 : 1;
347 up_bw = link_speed * link_width * 1000; /* Mb/s */
348 /* Leave 10% guard band */
352 tb_port_dbg(port, "link total bandwidth %d Mb/s\n", up_bw);
355 * Find all DP tunnels that cross the port and reduce
356 * their consumed bandwidth from the available.
358 list_for_each_entry(tunnel, &tcm->tunnel_list, list) {
359 int dp_consumed_up, dp_consumed_down;
361 if (!tb_tunnel_is_dp(tunnel))
364 if (!tb_tunnel_port_on_path(tunnel, port))
367 ret = tb_tunnel_consumed_bandwidth(tunnel,
373 up_bw -= dp_consumed_up;
374 down_bw -= dp_consumed_down;
378 * If USB3 is tunneled from the host router down to the
379 * branch leading to port we need to take USB3 consumed
380 * bandwidth into account regardless whether it actually
383 up_bw -= usb3_consumed_up;
384 down_bw -= usb3_consumed_down;
386 if (up_bw < *available_up)
387 *available_up = up_bw;
388 if (down_bw < *available_down)
389 *available_down = down_bw;
392 if (*available_up < 0)
394 if (*available_down < 0)
400 static int tb_release_unused_usb3_bandwidth(struct tb *tb,
401 struct tb_port *src_port,
402 struct tb_port *dst_port)
404 struct tb_tunnel *tunnel;
406 tunnel = tb_find_first_usb3_tunnel(tb, src_port, dst_port);
407 return tunnel ? tb_tunnel_release_unused_bandwidth(tunnel) : 0;
410 static void tb_reclaim_usb3_bandwidth(struct tb *tb, struct tb_port *src_port,
411 struct tb_port *dst_port)
413 int ret, available_up, available_down;
414 struct tb_tunnel *tunnel;
416 tunnel = tb_find_first_usb3_tunnel(tb, src_port, dst_port);
420 tb_dbg(tb, "reclaiming unused bandwidth for USB3\n");
423 * Calculate available bandwidth for the first hop USB3 tunnel.
424 * That determines the whole USB3 bandwidth for this branch.
426 ret = tb_available_bandwidth(tb, tunnel->src_port, tunnel->dst_port,
427 &available_up, &available_down);
429 tb_warn(tb, "failed to calculate available bandwidth\n");
433 tb_dbg(tb, "available bandwidth for USB3 %d/%d Mb/s\n",
434 available_up, available_down);
436 tb_tunnel_reclaim_available_bandwidth(tunnel, &available_up, &available_down);
439 static int tb_tunnel_usb3(struct tb *tb, struct tb_switch *sw)
441 struct tb_switch *parent = tb_switch_parent(sw);
442 int ret, available_up, available_down;
443 struct tb_port *up, *down, *port;
444 struct tb_cm *tcm = tb_priv(tb);
445 struct tb_tunnel *tunnel;
447 if (!tb_acpi_may_tunnel_usb3()) {
448 tb_dbg(tb, "USB3 tunneling disabled, not creating tunnel\n");
452 up = tb_switch_find_port(sw, TB_TYPE_USB3_UP);
460 * Look up available down port. Since we are chaining it should
461 * be found right above this switch.
463 port = tb_port_at(tb_route(sw), parent);
464 down = tb_find_usb3_down(parent, port);
468 if (tb_route(parent)) {
469 struct tb_port *parent_up;
471 * Check first that the parent switch has its upstream USB3
472 * port enabled. Otherwise the chain is not complete and
473 * there is no point setting up a new tunnel.
475 parent_up = tb_switch_find_port(parent, TB_TYPE_USB3_UP);
476 if (!parent_up || !tb_port_is_enabled(parent_up))
479 /* Make all unused bandwidth available for the new tunnel */
480 ret = tb_release_unused_usb3_bandwidth(tb, down, up);
485 ret = tb_available_bandwidth(tb, down, up, &available_up,
490 tb_port_dbg(up, "available bandwidth for new USB3 tunnel %d/%d Mb/s\n",
491 available_up, available_down);
493 tunnel = tb_tunnel_alloc_usb3(tb, up, down, available_up,
500 if (tb_tunnel_activate(tunnel)) {
502 "USB3 tunnel activation failed, aborting\n");
507 list_add_tail(&tunnel->list, &tcm->tunnel_list);
508 if (tb_route(parent))
509 tb_reclaim_usb3_bandwidth(tb, down, up);
514 tb_tunnel_free(tunnel);
516 if (tb_route(parent))
517 tb_reclaim_usb3_bandwidth(tb, down, up);
522 static int tb_create_usb3_tunnels(struct tb_switch *sw)
524 struct tb_port *port;
527 if (!tb_acpi_may_tunnel_usb3())
531 ret = tb_tunnel_usb3(sw->tb, sw);
536 tb_switch_for_each_port(sw, port) {
537 if (!tb_port_has_remote(port))
539 ret = tb_create_usb3_tunnels(port->remote->sw);
547 static void tb_scan_port(struct tb_port *port);
550 * tb_scan_switch() - scan for and initialize downstream switches
552 static void tb_scan_switch(struct tb_switch *sw)
554 struct tb_port *port;
556 pm_runtime_get_sync(&sw->dev);
558 tb_switch_for_each_port(sw, port)
561 pm_runtime_mark_last_busy(&sw->dev);
562 pm_runtime_put_autosuspend(&sw->dev);
566 * tb_scan_port() - check for and initialize switches below port
568 static void tb_scan_port(struct tb_port *port)
570 struct tb_cm *tcm = tb_priv(port->sw->tb);
571 struct tb_port *upstream_port;
572 struct tb_switch *sw;
574 if (tb_is_upstream_port(port))
577 if (tb_port_is_dpout(port) && tb_dp_port_hpd_is_active(port) == 1 &&
578 !tb_dp_port_is_enabled(port)) {
579 tb_port_dbg(port, "DP adapter HPD set, queuing hotplug\n");
580 tb_queue_hotplug(port->sw->tb, tb_route(port->sw), port->port,
585 if (port->config.type != TB_TYPE_PORT)
587 if (port->dual_link_port && port->link_nr)
589 * Downstream switch is reachable through two ports.
590 * Only scan on the primary port (link_nr == 0).
592 if (tb_wait_for_port(port, false) <= 0)
595 tb_port_dbg(port, "port already has a remote\n");
599 tb_retimer_scan(port, true);
601 sw = tb_switch_alloc(port->sw->tb, &port->sw->dev,
602 tb_downstream_route(port));
605 * If there is an error accessing the connected switch
606 * it may be connected to another domain. Also we allow
607 * the other domain to be connected to a max depth switch.
609 if (PTR_ERR(sw) == -EIO || PTR_ERR(sw) == -EADDRNOTAVAIL)
610 tb_scan_xdomain(port);
614 if (tb_switch_configure(sw)) {
620 * If there was previously another domain connected remove it
624 tb_xdomain_remove(port->xdomain);
625 tb_port_unconfigure_xdomain(port);
626 port->xdomain = NULL;
630 * Do not send uevents until we have discovered all existing
631 * tunnels and know which switches were authorized already by
634 if (!tcm->hotplug_active)
635 dev_set_uevent_suppress(&sw->dev, true);
638 * At the moment Thunderbolt 2 and beyond (devices with LC) we
639 * can support runtime PM.
641 sw->rpm = sw->generation > 1;
643 if (tb_switch_add(sw)) {
648 /* Link the switches using both links if available */
649 upstream_port = tb_upstream_port(sw);
650 port->remote = upstream_port;
651 upstream_port->remote = port;
652 if (port->dual_link_port && upstream_port->dual_link_port) {
653 port->dual_link_port->remote = upstream_port->dual_link_port;
654 upstream_port->dual_link_port->remote = port->dual_link_port;
657 /* Enable lane bonding if supported */
658 tb_switch_lane_bonding_enable(sw);
659 /* Set the link configured */
660 tb_switch_configure_link(sw);
662 if (tb_enable_tmu(sw))
663 tb_sw_warn(sw, "failed to enable TMU\n");
665 /* Scan upstream retimers */
666 tb_retimer_scan(upstream_port, true);
669 * Create USB 3.x tunnels only when the switch is plugged to the
670 * domain. This is because we scan the domain also during discovery
671 * and want to discover existing USB 3.x tunnels before we create
674 if (tcm->hotplug_active && tb_tunnel_usb3(sw->tb, sw))
675 tb_sw_warn(sw, "USB3 tunnel creation failed\n");
677 tb_add_dp_resources(sw);
681 static void tb_deactivate_and_free_tunnel(struct tb_tunnel *tunnel)
683 struct tb_port *src_port, *dst_port;
689 tb_tunnel_deactivate(tunnel);
690 list_del(&tunnel->list);
693 src_port = tunnel->src_port;
694 dst_port = tunnel->dst_port;
696 switch (tunnel->type) {
699 * In case of DP tunnel make sure the DP IN resource is
700 * deallocated properly.
702 tb_switch_dealloc_dp_resource(src_port->sw, src_port);
703 /* Now we can allow the domain to runtime suspend again */
704 pm_runtime_mark_last_busy(&dst_port->sw->dev);
705 pm_runtime_put_autosuspend(&dst_port->sw->dev);
706 pm_runtime_mark_last_busy(&src_port->sw->dev);
707 pm_runtime_put_autosuspend(&src_port->sw->dev);
711 tb_reclaim_usb3_bandwidth(tb, src_port, dst_port);
716 * PCIe and DMA tunnels do not consume guaranteed
722 tb_tunnel_free(tunnel);
726 * tb_free_invalid_tunnels() - destroy tunnels of devices that have gone away
728 static void tb_free_invalid_tunnels(struct tb *tb)
730 struct tb_cm *tcm = tb_priv(tb);
731 struct tb_tunnel *tunnel;
734 list_for_each_entry_safe(tunnel, n, &tcm->tunnel_list, list) {
735 if (tb_tunnel_is_invalid(tunnel))
736 tb_deactivate_and_free_tunnel(tunnel);
741 * tb_free_unplugged_children() - traverse hierarchy and free unplugged switches
743 static void tb_free_unplugged_children(struct tb_switch *sw)
745 struct tb_port *port;
747 tb_switch_for_each_port(sw, port) {
748 if (!tb_port_has_remote(port))
751 if (port->remote->sw->is_unplugged) {
752 tb_retimer_remove_all(port);
753 tb_remove_dp_resources(port->remote->sw);
754 tb_switch_unconfigure_link(port->remote->sw);
755 tb_switch_lane_bonding_disable(port->remote->sw);
756 tb_switch_remove(port->remote->sw);
758 if (port->dual_link_port)
759 port->dual_link_port->remote = NULL;
761 tb_free_unplugged_children(port->remote->sw);
766 static struct tb_port *tb_find_pcie_down(struct tb_switch *sw,
767 const struct tb_port *port)
769 struct tb_port *down = NULL;
772 * To keep plugging devices consistently in the same PCIe
773 * hierarchy, do mapping here for switch downstream PCIe ports.
775 if (tb_switch_is_usb4(sw)) {
776 down = usb4_switch_map_pcie_down(sw, port);
777 } else if (!tb_route(sw)) {
778 int phy_port = tb_phy_port_from_link(port->port);
782 * Hard-coded Thunderbolt port to PCIe down port mapping
785 if (tb_switch_is_cactus_ridge(sw) ||
786 tb_switch_is_alpine_ridge(sw))
787 index = !phy_port ? 6 : 7;
788 else if (tb_switch_is_falcon_ridge(sw))
789 index = !phy_port ? 6 : 8;
790 else if (tb_switch_is_titan_ridge(sw))
791 index = !phy_port ? 8 : 9;
795 /* Validate the hard-coding */
796 if (WARN_ON(index > sw->config.max_port_number))
799 down = &sw->ports[index];
803 if (WARN_ON(!tb_port_is_pcie_down(down)))
805 if (tb_pci_port_is_enabled(down))
812 return tb_find_unused_port(sw, TB_TYPE_PCIE_DOWN);
815 static struct tb_port *tb_find_dp_out(struct tb *tb, struct tb_port *in)
817 struct tb_port *host_port, *port;
818 struct tb_cm *tcm = tb_priv(tb);
820 host_port = tb_route(in->sw) ?
821 tb_port_at(tb_route(in->sw), tb->root_switch) : NULL;
823 list_for_each_entry(port, &tcm->dp_resources, list) {
824 if (!tb_port_is_dpout(port))
827 if (tb_port_is_enabled(port)) {
828 tb_port_dbg(port, "in use\n");
832 tb_port_dbg(port, "DP OUT available\n");
835 * Keep the DP tunnel under the topology starting from
836 * the same host router downstream port.
838 if (host_port && tb_route(port->sw)) {
841 p = tb_port_at(tb_route(port->sw), tb->root_switch);
852 static void tb_tunnel_dp(struct tb *tb)
854 int available_up, available_down, ret, link_nr;
855 struct tb_cm *tcm = tb_priv(tb);
856 struct tb_port *port, *in, *out;
857 struct tb_tunnel *tunnel;
859 if (!tb_acpi_may_tunnel_dp()) {
860 tb_dbg(tb, "DP tunneling disabled, not creating tunnel\n");
865 * Find pair of inactive DP IN and DP OUT adapters and then
866 * establish a DP tunnel between them.
868 tb_dbg(tb, "looking for DP IN <-> DP OUT pairs:\n");
872 list_for_each_entry(port, &tcm->dp_resources, list) {
873 if (!tb_port_is_dpin(port))
876 if (tb_port_is_enabled(port)) {
877 tb_port_dbg(port, "in use\n");
881 tb_port_dbg(port, "DP IN available\n");
883 out = tb_find_dp_out(tb, port);
891 tb_dbg(tb, "no suitable DP IN adapter available, not tunneling\n");
895 tb_dbg(tb, "no suitable DP OUT adapter available, not tunneling\n");
900 * This is only applicable to links that are not bonded (so
901 * when Thunderbolt 1 hardware is involved somewhere in the
902 * topology). For these try to share the DP bandwidth between
906 list_for_each_entry(tunnel, &tcm->tunnel_list, list) {
907 if (tb_tunnel_is_dp(tunnel)) {
914 * DP stream needs the domain to be active so runtime resume
915 * both ends of the tunnel.
917 * This should bring the routers in the middle active as well
918 * and keeps the domain from runtime suspending while the DP
921 pm_runtime_get_sync(&in->sw->dev);
922 pm_runtime_get_sync(&out->sw->dev);
924 if (tb_switch_alloc_dp_resource(in->sw, in)) {
925 tb_port_dbg(in, "no resource available for DP IN, not tunneling\n");
929 /* Make all unused USB3 bandwidth available for the new DP tunnel */
930 ret = tb_release_unused_usb3_bandwidth(tb, in, out);
932 tb_warn(tb, "failed to release unused bandwidth\n");
936 ret = tb_available_bandwidth(tb, in, out, &available_up,
941 tb_dbg(tb, "available bandwidth for new DP tunnel %u/%u Mb/s\n",
942 available_up, available_down);
944 tunnel = tb_tunnel_alloc_dp(tb, in, out, link_nr, available_up,
947 tb_port_dbg(out, "could not allocate DP tunnel\n");
951 if (tb_tunnel_activate(tunnel)) {
952 tb_port_info(out, "DP tunnel activation failed, aborting\n");
956 list_add_tail(&tunnel->list, &tcm->tunnel_list);
957 tb_reclaim_usb3_bandwidth(tb, in, out);
961 tb_tunnel_free(tunnel);
963 tb_reclaim_usb3_bandwidth(tb, in, out);
965 tb_switch_dealloc_dp_resource(in->sw, in);
967 pm_runtime_mark_last_busy(&out->sw->dev);
968 pm_runtime_put_autosuspend(&out->sw->dev);
969 pm_runtime_mark_last_busy(&in->sw->dev);
970 pm_runtime_put_autosuspend(&in->sw->dev);
973 static void tb_dp_resource_unavailable(struct tb *tb, struct tb_port *port)
975 struct tb_port *in, *out;
976 struct tb_tunnel *tunnel;
978 if (tb_port_is_dpin(port)) {
979 tb_port_dbg(port, "DP IN resource unavailable\n");
983 tb_port_dbg(port, "DP OUT resource unavailable\n");
988 tunnel = tb_find_tunnel(tb, TB_TUNNEL_DP, in, out);
989 tb_deactivate_and_free_tunnel(tunnel);
990 list_del_init(&port->list);
993 * See if there is another DP OUT port that can be used for
994 * to create another tunnel.
999 static void tb_dp_resource_available(struct tb *tb, struct tb_port *port)
1001 struct tb_cm *tcm = tb_priv(tb);
1004 if (tb_port_is_enabled(port))
1007 list_for_each_entry(p, &tcm->dp_resources, list) {
1012 tb_port_dbg(port, "DP %s resource available\n",
1013 tb_port_is_dpin(port) ? "IN" : "OUT");
1014 list_add_tail(&port->list, &tcm->dp_resources);
1016 /* Look for suitable DP IN <-> DP OUT pairs now */
1020 static void tb_disconnect_and_release_dp(struct tb *tb)
1022 struct tb_cm *tcm = tb_priv(tb);
1023 struct tb_tunnel *tunnel, *n;
1026 * Tear down all DP tunnels and release their resources. They
1027 * will be re-established after resume based on plug events.
1029 list_for_each_entry_safe_reverse(tunnel, n, &tcm->tunnel_list, list) {
1030 if (tb_tunnel_is_dp(tunnel))
1031 tb_deactivate_and_free_tunnel(tunnel);
1034 while (!list_empty(&tcm->dp_resources)) {
1035 struct tb_port *port;
1037 port = list_first_entry(&tcm->dp_resources,
1038 struct tb_port, list);
1039 list_del_init(&port->list);
1043 static int tb_disconnect_pci(struct tb *tb, struct tb_switch *sw)
1045 struct tb_tunnel *tunnel;
1048 up = tb_switch_find_port(sw, TB_TYPE_PCIE_UP);
1052 tunnel = tb_find_tunnel(tb, TB_TUNNEL_PCI, NULL, up);
1053 if (WARN_ON(!tunnel))
1056 tb_tunnel_deactivate(tunnel);
1057 list_del(&tunnel->list);
1058 tb_tunnel_free(tunnel);
1062 static int tb_tunnel_pci(struct tb *tb, struct tb_switch *sw)
1064 struct tb_port *up, *down, *port;
1065 struct tb_cm *tcm = tb_priv(tb);
1066 struct tb_switch *parent_sw;
1067 struct tb_tunnel *tunnel;
1069 up = tb_switch_find_port(sw, TB_TYPE_PCIE_UP);
1074 * Look up available down port. Since we are chaining it should
1075 * be found right above this switch.
1077 parent_sw = tb_to_switch(sw->dev.parent);
1078 port = tb_port_at(tb_route(sw), parent_sw);
1079 down = tb_find_pcie_down(parent_sw, port);
1083 tunnel = tb_tunnel_alloc_pci(tb, up, down);
1087 if (tb_tunnel_activate(tunnel)) {
1089 "PCIe tunnel activation failed, aborting\n");
1090 tb_tunnel_free(tunnel);
1094 list_add_tail(&tunnel->list, &tcm->tunnel_list);
1098 static int tb_approve_xdomain_paths(struct tb *tb, struct tb_xdomain *xd,
1099 int transmit_path, int transmit_ring,
1100 int receive_path, int receive_ring)
1102 struct tb_cm *tcm = tb_priv(tb);
1103 struct tb_port *nhi_port, *dst_port;
1104 struct tb_tunnel *tunnel;
1105 struct tb_switch *sw;
1107 sw = tb_to_switch(xd->dev.parent);
1108 dst_port = tb_port_at(xd->route, sw);
1109 nhi_port = tb_switch_find_port(tb->root_switch, TB_TYPE_NHI);
1111 mutex_lock(&tb->lock);
1112 tunnel = tb_tunnel_alloc_dma(tb, nhi_port, dst_port, transmit_path,
1113 transmit_ring, receive_path, receive_ring);
1115 mutex_unlock(&tb->lock);
1119 if (tb_tunnel_activate(tunnel)) {
1120 tb_port_info(nhi_port,
1121 "DMA tunnel activation failed, aborting\n");
1122 tb_tunnel_free(tunnel);
1123 mutex_unlock(&tb->lock);
1127 list_add_tail(&tunnel->list, &tcm->tunnel_list);
1128 mutex_unlock(&tb->lock);
1132 static void __tb_disconnect_xdomain_paths(struct tb *tb, struct tb_xdomain *xd,
1133 int transmit_path, int transmit_ring,
1134 int receive_path, int receive_ring)
1136 struct tb_cm *tcm = tb_priv(tb);
1137 struct tb_port *nhi_port, *dst_port;
1138 struct tb_tunnel *tunnel, *n;
1139 struct tb_switch *sw;
1141 sw = tb_to_switch(xd->dev.parent);
1142 dst_port = tb_port_at(xd->route, sw);
1143 nhi_port = tb_switch_find_port(tb->root_switch, TB_TYPE_NHI);
1145 list_for_each_entry_safe(tunnel, n, &tcm->tunnel_list, list) {
1146 if (!tb_tunnel_is_dma(tunnel))
1148 if (tunnel->src_port != nhi_port || tunnel->dst_port != dst_port)
1151 if (tb_tunnel_match_dma(tunnel, transmit_path, transmit_ring,
1152 receive_path, receive_ring))
1153 tb_deactivate_and_free_tunnel(tunnel);
1157 static int tb_disconnect_xdomain_paths(struct tb *tb, struct tb_xdomain *xd,
1158 int transmit_path, int transmit_ring,
1159 int receive_path, int receive_ring)
1161 if (!xd->is_unplugged) {
1162 mutex_lock(&tb->lock);
1163 __tb_disconnect_xdomain_paths(tb, xd, transmit_path,
1164 transmit_ring, receive_path,
1166 mutex_unlock(&tb->lock);
1171 /* hotplug handling */
1174 * tb_handle_hotplug() - handle hotplug event
1176 * Executes on tb->wq.
1178 static void tb_handle_hotplug(struct work_struct *work)
1180 struct tb_hotplug_event *ev = container_of(work, typeof(*ev), work);
1181 struct tb *tb = ev->tb;
1182 struct tb_cm *tcm = tb_priv(tb);
1183 struct tb_switch *sw;
1184 struct tb_port *port;
1186 /* Bring the domain back from sleep if it was suspended */
1187 pm_runtime_get_sync(&tb->dev);
1189 mutex_lock(&tb->lock);
1190 if (!tcm->hotplug_active)
1191 goto out; /* during init, suspend or shutdown */
1193 sw = tb_switch_find_by_route(tb, ev->route);
1196 "hotplug event from non existent switch %llx:%x (unplug: %d)\n",
1197 ev->route, ev->port, ev->unplug);
1200 if (ev->port > sw->config.max_port_number) {
1202 "hotplug event from non existent port %llx:%x (unplug: %d)\n",
1203 ev->route, ev->port, ev->unplug);
1206 port = &sw->ports[ev->port];
1207 if (tb_is_upstream_port(port)) {
1208 tb_dbg(tb, "hotplug event for upstream port %llx:%x (unplug: %d)\n",
1209 ev->route, ev->port, ev->unplug);
1213 pm_runtime_get_sync(&sw->dev);
1216 tb_retimer_remove_all(port);
1218 if (tb_port_has_remote(port)) {
1219 tb_port_dbg(port, "switch unplugged\n");
1220 tb_sw_set_unplugged(port->remote->sw);
1221 tb_free_invalid_tunnels(tb);
1222 tb_remove_dp_resources(port->remote->sw);
1223 tb_switch_tmu_disable(port->remote->sw);
1224 tb_switch_unconfigure_link(port->remote->sw);
1225 tb_switch_lane_bonding_disable(port->remote->sw);
1226 tb_switch_remove(port->remote->sw);
1227 port->remote = NULL;
1228 if (port->dual_link_port)
1229 port->dual_link_port->remote = NULL;
1230 /* Maybe we can create another DP tunnel */
1232 } else if (port->xdomain) {
1233 struct tb_xdomain *xd = tb_xdomain_get(port->xdomain);
1235 tb_port_dbg(port, "xdomain unplugged\n");
1237 * Service drivers are unbound during
1238 * tb_xdomain_remove() so setting XDomain as
1239 * unplugged here prevents deadlock if they call
1240 * tb_xdomain_disable_paths(). We will tear down
1241 * all the tunnels below.
1243 xd->is_unplugged = true;
1244 tb_xdomain_remove(xd);
1245 port->xdomain = NULL;
1246 __tb_disconnect_xdomain_paths(tb, xd, -1, -1, -1, -1);
1248 tb_port_unconfigure_xdomain(port);
1249 } else if (tb_port_is_dpout(port) || tb_port_is_dpin(port)) {
1250 tb_dp_resource_unavailable(tb, port);
1253 "got unplug event for disconnected port, ignoring\n");
1255 } else if (port->remote) {
1256 tb_port_dbg(port, "got plug event for connected port, ignoring\n");
1258 if (tb_port_is_null(port)) {
1259 tb_port_dbg(port, "hotplug: scanning\n");
1262 tb_port_dbg(port, "hotplug: no switch found\n");
1263 } else if (tb_port_is_dpout(port) || tb_port_is_dpin(port)) {
1264 tb_dp_resource_available(tb, port);
1268 pm_runtime_mark_last_busy(&sw->dev);
1269 pm_runtime_put_autosuspend(&sw->dev);
1274 mutex_unlock(&tb->lock);
1276 pm_runtime_mark_last_busy(&tb->dev);
1277 pm_runtime_put_autosuspend(&tb->dev);
1283 * tb_schedule_hotplug_handler() - callback function for the control channel
1285 * Delegates to tb_handle_hotplug.
1287 static void tb_handle_event(struct tb *tb, enum tb_cfg_pkg_type type,
1288 const void *buf, size_t size)
1290 const struct cfg_event_pkg *pkg = buf;
1293 if (type != TB_CFG_PKG_EVENT) {
1294 tb_warn(tb, "unexpected event %#x, ignoring\n", type);
1298 route = tb_cfg_get_route(&pkg->header);
1300 if (tb_cfg_ack_plug(tb->ctl, route, pkg->port, pkg->unplug)) {
1301 tb_warn(tb, "could not ack plug event on %llx:%x\n", route,
1305 tb_queue_hotplug(tb, route, pkg->port, pkg->unplug);
1308 static void tb_stop(struct tb *tb)
1310 struct tb_cm *tcm = tb_priv(tb);
1311 struct tb_tunnel *tunnel;
1312 struct tb_tunnel *n;
1314 cancel_delayed_work(&tcm->remove_work);
1315 /* tunnels are only present after everything has been initialized */
1316 list_for_each_entry_safe(tunnel, n, &tcm->tunnel_list, list) {
1318 * DMA tunnels require the driver to be functional so we
1319 * tear them down. Other protocol tunnels can be left
1322 if (tb_tunnel_is_dma(tunnel))
1323 tb_tunnel_deactivate(tunnel);
1324 tb_tunnel_free(tunnel);
1326 tb_switch_remove(tb->root_switch);
1327 tcm->hotplug_active = false; /* signal tb_handle_hotplug to quit */
1330 static int tb_scan_finalize_switch(struct device *dev, void *data)
1332 if (tb_is_switch(dev)) {
1333 struct tb_switch *sw = tb_to_switch(dev);
1336 * If we found that the switch was already setup by the
1337 * boot firmware, mark it as authorized now before we
1338 * send uevent to userspace.
1343 dev_set_uevent_suppress(dev, false);
1344 kobject_uevent(&dev->kobj, KOBJ_ADD);
1345 device_for_each_child(dev, NULL, tb_scan_finalize_switch);
1351 static int tb_start(struct tb *tb)
1353 struct tb_cm *tcm = tb_priv(tb);
1356 tb->root_switch = tb_switch_alloc(tb, &tb->dev, 0);
1357 if (IS_ERR(tb->root_switch))
1358 return PTR_ERR(tb->root_switch);
1361 * ICM firmware upgrade needs running firmware and in native
1362 * mode that is not available so disable firmware upgrade of the
1365 tb->root_switch->no_nvm_upgrade = true;
1366 /* All USB4 routers support runtime PM */
1367 tb->root_switch->rpm = tb_switch_is_usb4(tb->root_switch);
1369 ret = tb_switch_configure(tb->root_switch);
1371 tb_switch_put(tb->root_switch);
1375 /* Announce the switch to the world */
1376 ret = tb_switch_add(tb->root_switch);
1378 tb_switch_put(tb->root_switch);
1382 /* Enable TMU if it is off */
1383 tb_switch_tmu_enable(tb->root_switch);
1384 /* Full scan to discover devices added before the driver was loaded. */
1385 tb_scan_switch(tb->root_switch);
1386 /* Find out tunnels created by the boot firmware */
1387 tb_discover_tunnels(tb->root_switch);
1389 * If the boot firmware did not create USB 3.x tunnels create them
1390 * now for the whole topology.
1392 tb_create_usb3_tunnels(tb->root_switch);
1393 /* Add DP IN resources for the root switch */
1394 tb_add_dp_resources(tb->root_switch);
1395 /* Make the discovered switches available to the userspace */
1396 device_for_each_child(&tb->root_switch->dev, NULL,
1397 tb_scan_finalize_switch);
1399 /* Allow tb_handle_hotplug to progress events */
1400 tcm->hotplug_active = true;
1404 static int tb_suspend_noirq(struct tb *tb)
1406 struct tb_cm *tcm = tb_priv(tb);
1408 tb_dbg(tb, "suspending...\n");
1409 tb_disconnect_and_release_dp(tb);
1410 tb_switch_suspend(tb->root_switch, false);
1411 tcm->hotplug_active = false; /* signal tb_handle_hotplug to quit */
1412 tb_dbg(tb, "suspend finished\n");
1417 static void tb_restore_children(struct tb_switch *sw)
1419 struct tb_port *port;
1421 /* No need to restore if the router is already unplugged */
1422 if (sw->is_unplugged)
1425 if (tb_enable_tmu(sw))
1426 tb_sw_warn(sw, "failed to restore TMU configuration\n");
1428 tb_switch_for_each_port(sw, port) {
1429 if (!tb_port_has_remote(port) && !port->xdomain)
1433 tb_switch_lane_bonding_enable(port->remote->sw);
1434 tb_switch_configure_link(port->remote->sw);
1436 tb_restore_children(port->remote->sw);
1437 } else if (port->xdomain) {
1438 tb_port_configure_xdomain(port);
1443 static int tb_resume_noirq(struct tb *tb)
1445 struct tb_cm *tcm = tb_priv(tb);
1446 struct tb_tunnel *tunnel, *n;
1448 tb_dbg(tb, "resuming...\n");
1450 /* remove any pci devices the firmware might have setup */
1451 tb_switch_reset(tb->root_switch);
1453 tb_switch_resume(tb->root_switch);
1454 tb_free_invalid_tunnels(tb);
1455 tb_free_unplugged_children(tb->root_switch);
1456 tb_restore_children(tb->root_switch);
1457 list_for_each_entry_safe(tunnel, n, &tcm->tunnel_list, list)
1458 tb_tunnel_restart(tunnel);
1459 if (!list_empty(&tcm->tunnel_list)) {
1461 * the pcie links need some time to get going.
1462 * 100ms works for me...
1464 tb_dbg(tb, "tunnels restarted, sleeping for 100ms\n");
1467 /* Allow tb_handle_hotplug to progress events */
1468 tcm->hotplug_active = true;
1469 tb_dbg(tb, "resume finished\n");
1474 static int tb_free_unplugged_xdomains(struct tb_switch *sw)
1476 struct tb_port *port;
1479 tb_switch_for_each_port(sw, port) {
1480 if (tb_is_upstream_port(port))
1482 if (port->xdomain && port->xdomain->is_unplugged) {
1483 tb_retimer_remove_all(port);
1484 tb_xdomain_remove(port->xdomain);
1485 tb_port_unconfigure_xdomain(port);
1486 port->xdomain = NULL;
1488 } else if (port->remote) {
1489 ret += tb_free_unplugged_xdomains(port->remote->sw);
1496 static int tb_freeze_noirq(struct tb *tb)
1498 struct tb_cm *tcm = tb_priv(tb);
1500 tcm->hotplug_active = false;
1504 static int tb_thaw_noirq(struct tb *tb)
1506 struct tb_cm *tcm = tb_priv(tb);
1508 tcm->hotplug_active = true;
1512 static void tb_complete(struct tb *tb)
1515 * Release any unplugged XDomains and if there is a case where
1516 * another domain is swapped in place of unplugged XDomain we
1517 * need to run another rescan.
1519 mutex_lock(&tb->lock);
1520 if (tb_free_unplugged_xdomains(tb->root_switch))
1521 tb_scan_switch(tb->root_switch);
1522 mutex_unlock(&tb->lock);
1525 static int tb_runtime_suspend(struct tb *tb)
1527 struct tb_cm *tcm = tb_priv(tb);
1529 mutex_lock(&tb->lock);
1530 tb_switch_suspend(tb->root_switch, true);
1531 tcm->hotplug_active = false;
1532 mutex_unlock(&tb->lock);
1537 static void tb_remove_work(struct work_struct *work)
1539 struct tb_cm *tcm = container_of(work, struct tb_cm, remove_work.work);
1540 struct tb *tb = tcm_to_tb(tcm);
1542 mutex_lock(&tb->lock);
1543 if (tb->root_switch) {
1544 tb_free_unplugged_children(tb->root_switch);
1545 tb_free_unplugged_xdomains(tb->root_switch);
1547 mutex_unlock(&tb->lock);
1550 static int tb_runtime_resume(struct tb *tb)
1552 struct tb_cm *tcm = tb_priv(tb);
1553 struct tb_tunnel *tunnel, *n;
1555 mutex_lock(&tb->lock);
1556 tb_switch_resume(tb->root_switch);
1557 tb_free_invalid_tunnels(tb);
1558 tb_restore_children(tb->root_switch);
1559 list_for_each_entry_safe(tunnel, n, &tcm->tunnel_list, list)
1560 tb_tunnel_restart(tunnel);
1561 tcm->hotplug_active = true;
1562 mutex_unlock(&tb->lock);
1565 * Schedule cleanup of any unplugged devices. Run this in a
1566 * separate thread to avoid possible deadlock if the device
1567 * removal runtime resumes the unplugged device.
1569 queue_delayed_work(tb->wq, &tcm->remove_work, msecs_to_jiffies(50));
1573 static const struct tb_cm_ops tb_cm_ops = {
1576 .suspend_noirq = tb_suspend_noirq,
1577 .resume_noirq = tb_resume_noirq,
1578 .freeze_noirq = tb_freeze_noirq,
1579 .thaw_noirq = tb_thaw_noirq,
1580 .complete = tb_complete,
1581 .runtime_suspend = tb_runtime_suspend,
1582 .runtime_resume = tb_runtime_resume,
1583 .handle_event = tb_handle_event,
1584 .disapprove_switch = tb_disconnect_pci,
1585 .approve_switch = tb_tunnel_pci,
1586 .approve_xdomain_paths = tb_approve_xdomain_paths,
1587 .disconnect_xdomain_paths = tb_disconnect_xdomain_paths,
1591 * During suspend the Thunderbolt controller is reset and all PCIe
1592 * tunnels are lost. The NHI driver will try to reestablish all tunnels
1593 * during resume. This adds device links between the tunneled PCIe
1594 * downstream ports and the NHI so that the device core will make sure
1595 * NHI is resumed first before the rest.
1597 static void tb_apple_add_links(struct tb_nhi *nhi)
1599 struct pci_dev *upstream, *pdev;
1601 if (!x86_apple_machine)
1604 switch (nhi->pdev->device) {
1605 case PCI_DEVICE_ID_INTEL_LIGHT_RIDGE:
1606 case PCI_DEVICE_ID_INTEL_CACTUS_RIDGE_4C:
1607 case PCI_DEVICE_ID_INTEL_FALCON_RIDGE_2C_NHI:
1608 case PCI_DEVICE_ID_INTEL_FALCON_RIDGE_4C_NHI:
1614 upstream = pci_upstream_bridge(nhi->pdev);
1616 if (!pci_is_pcie(upstream))
1618 if (pci_pcie_type(upstream) == PCI_EXP_TYPE_UPSTREAM)
1620 upstream = pci_upstream_bridge(upstream);
1627 * For each hotplug downstream port, create add device link
1628 * back to NHI so that PCIe tunnels can be re-established after
1631 for_each_pci_bridge(pdev, upstream->subordinate) {
1632 const struct device_link *link;
1634 if (!pci_is_pcie(pdev))
1636 if (pci_pcie_type(pdev) != PCI_EXP_TYPE_DOWNSTREAM ||
1637 !pdev->is_hotplug_bridge)
1640 link = device_link_add(&pdev->dev, &nhi->pdev->dev,
1641 DL_FLAG_AUTOREMOVE_SUPPLIER |
1642 DL_FLAG_PM_RUNTIME);
1644 dev_dbg(&nhi->pdev->dev, "created link from %s\n",
1645 dev_name(&pdev->dev));
1647 dev_warn(&nhi->pdev->dev, "device link creation from %s failed\n",
1648 dev_name(&pdev->dev));
1653 struct tb *tb_probe(struct tb_nhi *nhi)
1658 tb = tb_domain_alloc(nhi, TB_TIMEOUT, sizeof(*tcm));
1662 if (tb_acpi_may_tunnel_pcie())
1663 tb->security_level = TB_SECURITY_USER;
1665 tb->security_level = TB_SECURITY_NOPCIE;
1667 tb->cm_ops = &tb_cm_ops;
1670 INIT_LIST_HEAD(&tcm->tunnel_list);
1671 INIT_LIST_HEAD(&tcm->dp_resources);
1672 INIT_DELAYED_WORK(&tcm->remove_work, tb_remove_work);
1674 tb_dbg(tb, "using software connection manager\n");
1676 tb_apple_add_links(nhi);
1677 tb_acpi_add_links(nhi);