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_dp_resource(struct tb *tb, struct tb_port *port)
110 struct tb_cm *tcm = tb_priv(tb);
113 list_for_each_entry(p, &tcm->dp_resources, list) {
118 tb_port_dbg(port, "DP %s resource available discovered\n",
119 tb_port_is_dpin(port) ? "IN" : "OUT");
120 list_add_tail(&port->list, &tcm->dp_resources);
123 static void tb_discover_dp_resources(struct tb *tb)
125 struct tb_cm *tcm = tb_priv(tb);
126 struct tb_tunnel *tunnel;
128 list_for_each_entry(tunnel, &tcm->tunnel_list, list) {
129 if (tb_tunnel_is_dp(tunnel))
130 tb_discover_dp_resource(tb, tunnel->dst_port);
134 static void tb_switch_discover_tunnels(struct tb_switch *sw,
135 struct list_head *list,
138 struct tb *tb = sw->tb;
139 struct tb_port *port;
141 tb_switch_for_each_port(sw, port) {
142 struct tb_tunnel *tunnel = NULL;
144 switch (port->config.type) {
145 case TB_TYPE_DP_HDMI_IN:
146 tunnel = tb_tunnel_discover_dp(tb, port, alloc_hopids);
149 case TB_TYPE_PCIE_DOWN:
150 tunnel = tb_tunnel_discover_pci(tb, port, alloc_hopids);
153 case TB_TYPE_USB3_DOWN:
154 tunnel = tb_tunnel_discover_usb3(tb, port, alloc_hopids);
162 list_add_tail(&tunnel->list, list);
165 tb_switch_for_each_port(sw, port) {
166 if (tb_port_has_remote(port)) {
167 tb_switch_discover_tunnels(port->remote->sw, list,
173 static void tb_discover_tunnels(struct tb *tb)
175 struct tb_cm *tcm = tb_priv(tb);
176 struct tb_tunnel *tunnel;
178 tb_switch_discover_tunnels(tb->root_switch, &tcm->tunnel_list, true);
180 list_for_each_entry(tunnel, &tcm->tunnel_list, list) {
181 if (tb_tunnel_is_pci(tunnel)) {
182 struct tb_switch *parent = tunnel->dst_port->sw;
184 while (parent != tunnel->src_port->sw) {
186 parent = tb_switch_parent(parent);
188 } else if (tb_tunnel_is_dp(tunnel)) {
189 /* Keep the domain from powering down */
190 pm_runtime_get_sync(&tunnel->src_port->sw->dev);
191 pm_runtime_get_sync(&tunnel->dst_port->sw->dev);
196 static int tb_port_configure_xdomain(struct tb_port *port)
199 * XDomain paths currently only support single lane so we must
200 * disable the other lane according to USB4 spec.
202 tb_port_disable(port->dual_link_port);
204 if (tb_switch_is_usb4(port->sw))
205 return usb4_port_configure_xdomain(port);
206 return tb_lc_configure_xdomain(port);
209 static void tb_port_unconfigure_xdomain(struct tb_port *port)
211 if (tb_switch_is_usb4(port->sw))
212 usb4_port_unconfigure_xdomain(port);
214 tb_lc_unconfigure_xdomain(port);
216 tb_port_enable(port->dual_link_port);
219 static void tb_scan_xdomain(struct tb_port *port)
221 struct tb_switch *sw = port->sw;
222 struct tb *tb = sw->tb;
223 struct tb_xdomain *xd;
226 if (!tb_is_xdomain_enabled())
229 route = tb_downstream_route(port);
230 xd = tb_xdomain_find_by_route(tb, route);
236 xd = tb_xdomain_alloc(tb, &sw->dev, route, tb->root_switch->uuid,
239 tb_port_at(route, sw)->xdomain = xd;
240 tb_port_configure_xdomain(port);
245 static int tb_enable_tmu(struct tb_switch *sw)
249 /* If it is already enabled in correct mode, don't touch it */
250 if (tb_switch_tmu_is_enabled(sw))
253 ret = tb_switch_tmu_disable(sw);
257 ret = tb_switch_tmu_post_time(sw);
261 return tb_switch_tmu_enable(sw);
265 * tb_find_unused_port() - return the first inactive port on @sw
266 * @sw: Switch to find the port on
267 * @type: Port type to look for
269 static struct tb_port *tb_find_unused_port(struct tb_switch *sw,
270 enum tb_port_type type)
272 struct tb_port *port;
274 tb_switch_for_each_port(sw, port) {
275 if (tb_is_upstream_port(port))
277 if (port->config.type != type)
281 if (tb_port_is_enabled(port))
288 static struct tb_port *tb_find_usb3_down(struct tb_switch *sw,
289 const struct tb_port *port)
291 struct tb_port *down;
293 down = usb4_switch_map_usb3_down(sw, port);
294 if (down && !tb_usb3_port_is_enabled(down))
299 static struct tb_tunnel *tb_find_tunnel(struct tb *tb, enum tb_tunnel_type type,
300 struct tb_port *src_port,
301 struct tb_port *dst_port)
303 struct tb_cm *tcm = tb_priv(tb);
304 struct tb_tunnel *tunnel;
306 list_for_each_entry(tunnel, &tcm->tunnel_list, list) {
307 if (tunnel->type == type &&
308 ((src_port && src_port == tunnel->src_port) ||
309 (dst_port && dst_port == tunnel->dst_port))) {
317 static struct tb_tunnel *tb_find_first_usb3_tunnel(struct tb *tb,
318 struct tb_port *src_port,
319 struct tb_port *dst_port)
321 struct tb_port *port, *usb3_down;
322 struct tb_switch *sw;
324 /* Pick the router that is deepest in the topology */
325 if (dst_port->sw->config.depth > src_port->sw->config.depth)
330 /* Can't be the host router */
331 if (sw == tb->root_switch)
334 /* Find the downstream USB4 port that leads to this router */
335 port = tb_port_at(tb_route(sw), tb->root_switch);
336 /* Find the corresponding host router USB3 downstream port */
337 usb3_down = usb4_switch_map_usb3_down(tb->root_switch, port);
341 return tb_find_tunnel(tb, TB_TUNNEL_USB3, usb3_down, NULL);
344 static int tb_available_bandwidth(struct tb *tb, struct tb_port *src_port,
345 struct tb_port *dst_port, int *available_up, int *available_down)
347 int usb3_consumed_up, usb3_consumed_down, ret;
348 struct tb_cm *tcm = tb_priv(tb);
349 struct tb_tunnel *tunnel;
350 struct tb_port *port;
352 tb_port_dbg(dst_port, "calculating available bandwidth\n");
354 tunnel = tb_find_first_usb3_tunnel(tb, src_port, dst_port);
356 ret = tb_tunnel_consumed_bandwidth(tunnel, &usb3_consumed_up,
357 &usb3_consumed_down);
361 usb3_consumed_up = 0;
362 usb3_consumed_down = 0;
365 *available_up = *available_down = 40000;
367 /* Find the minimum available bandwidth over all links */
368 tb_for_each_port_on_path(src_port, dst_port, port) {
369 int link_speed, link_width, up_bw, down_bw;
371 if (!tb_port_is_null(port))
374 if (tb_is_upstream_port(port)) {
375 link_speed = port->sw->link_speed;
377 link_speed = tb_port_get_link_speed(port);
382 link_width = port->bonded ? 2 : 1;
384 up_bw = link_speed * link_width * 1000; /* Mb/s */
385 /* Leave 10% guard band */
389 tb_port_dbg(port, "link total bandwidth %d Mb/s\n", up_bw);
392 * Find all DP tunnels that cross the port and reduce
393 * their consumed bandwidth from the available.
395 list_for_each_entry(tunnel, &tcm->tunnel_list, list) {
396 int dp_consumed_up, dp_consumed_down;
398 if (!tb_tunnel_is_dp(tunnel))
401 if (!tb_tunnel_port_on_path(tunnel, port))
404 ret = tb_tunnel_consumed_bandwidth(tunnel,
410 up_bw -= dp_consumed_up;
411 down_bw -= dp_consumed_down;
415 * If USB3 is tunneled from the host router down to the
416 * branch leading to port we need to take USB3 consumed
417 * bandwidth into account regardless whether it actually
420 up_bw -= usb3_consumed_up;
421 down_bw -= usb3_consumed_down;
423 if (up_bw < *available_up)
424 *available_up = up_bw;
425 if (down_bw < *available_down)
426 *available_down = down_bw;
429 if (*available_up < 0)
431 if (*available_down < 0)
437 static int tb_release_unused_usb3_bandwidth(struct tb *tb,
438 struct tb_port *src_port,
439 struct tb_port *dst_port)
441 struct tb_tunnel *tunnel;
443 tunnel = tb_find_first_usb3_tunnel(tb, src_port, dst_port);
444 return tunnel ? tb_tunnel_release_unused_bandwidth(tunnel) : 0;
447 static void tb_reclaim_usb3_bandwidth(struct tb *tb, struct tb_port *src_port,
448 struct tb_port *dst_port)
450 int ret, available_up, available_down;
451 struct tb_tunnel *tunnel;
453 tunnel = tb_find_first_usb3_tunnel(tb, src_port, dst_port);
457 tb_dbg(tb, "reclaiming unused bandwidth for USB3\n");
460 * Calculate available bandwidth for the first hop USB3 tunnel.
461 * That determines the whole USB3 bandwidth for this branch.
463 ret = tb_available_bandwidth(tb, tunnel->src_port, tunnel->dst_port,
464 &available_up, &available_down);
466 tb_warn(tb, "failed to calculate available bandwidth\n");
470 tb_dbg(tb, "available bandwidth for USB3 %d/%d Mb/s\n",
471 available_up, available_down);
473 tb_tunnel_reclaim_available_bandwidth(tunnel, &available_up, &available_down);
476 static int tb_tunnel_usb3(struct tb *tb, struct tb_switch *sw)
478 struct tb_switch *parent = tb_switch_parent(sw);
479 int ret, available_up, available_down;
480 struct tb_port *up, *down, *port;
481 struct tb_cm *tcm = tb_priv(tb);
482 struct tb_tunnel *tunnel;
484 if (!tb_acpi_may_tunnel_usb3()) {
485 tb_dbg(tb, "USB3 tunneling disabled, not creating tunnel\n");
489 up = tb_switch_find_port(sw, TB_TYPE_USB3_UP);
497 * Look up available down port. Since we are chaining it should
498 * be found right above this switch.
500 port = tb_port_at(tb_route(sw), parent);
501 down = tb_find_usb3_down(parent, port);
505 if (tb_route(parent)) {
506 struct tb_port *parent_up;
508 * Check first that the parent switch has its upstream USB3
509 * port enabled. Otherwise the chain is not complete and
510 * there is no point setting up a new tunnel.
512 parent_up = tb_switch_find_port(parent, TB_TYPE_USB3_UP);
513 if (!parent_up || !tb_port_is_enabled(parent_up))
516 /* Make all unused bandwidth available for the new tunnel */
517 ret = tb_release_unused_usb3_bandwidth(tb, down, up);
522 ret = tb_available_bandwidth(tb, down, up, &available_up,
527 tb_port_dbg(up, "available bandwidth for new USB3 tunnel %d/%d Mb/s\n",
528 available_up, available_down);
530 tunnel = tb_tunnel_alloc_usb3(tb, up, down, available_up,
537 if (tb_tunnel_activate(tunnel)) {
539 "USB3 tunnel activation failed, aborting\n");
544 list_add_tail(&tunnel->list, &tcm->tunnel_list);
545 if (tb_route(parent))
546 tb_reclaim_usb3_bandwidth(tb, down, up);
551 tb_tunnel_free(tunnel);
553 if (tb_route(parent))
554 tb_reclaim_usb3_bandwidth(tb, down, up);
559 static int tb_create_usb3_tunnels(struct tb_switch *sw)
561 struct tb_port *port;
564 if (!tb_acpi_may_tunnel_usb3())
568 ret = tb_tunnel_usb3(sw->tb, sw);
573 tb_switch_for_each_port(sw, port) {
574 if (!tb_port_has_remote(port))
576 ret = tb_create_usb3_tunnels(port->remote->sw);
584 static void tb_scan_port(struct tb_port *port);
587 * tb_scan_switch() - scan for and initialize downstream switches
589 static void tb_scan_switch(struct tb_switch *sw)
591 struct tb_port *port;
593 pm_runtime_get_sync(&sw->dev);
595 tb_switch_for_each_port(sw, port)
598 pm_runtime_mark_last_busy(&sw->dev);
599 pm_runtime_put_autosuspend(&sw->dev);
603 * tb_scan_port() - check for and initialize switches below port
605 static void tb_scan_port(struct tb_port *port)
607 struct tb_cm *tcm = tb_priv(port->sw->tb);
608 struct tb_port *upstream_port;
609 struct tb_switch *sw;
611 if (tb_is_upstream_port(port))
614 if (tb_port_is_dpout(port) && tb_dp_port_hpd_is_active(port) == 1 &&
615 !tb_dp_port_is_enabled(port)) {
616 tb_port_dbg(port, "DP adapter HPD set, queuing hotplug\n");
617 tb_queue_hotplug(port->sw->tb, tb_route(port->sw), port->port,
622 if (port->config.type != TB_TYPE_PORT)
624 if (port->dual_link_port && port->link_nr)
626 * Downstream switch is reachable through two ports.
627 * Only scan on the primary port (link_nr == 0).
629 if (tb_wait_for_port(port, false) <= 0)
632 tb_port_dbg(port, "port already has a remote\n");
636 tb_retimer_scan(port, true);
638 sw = tb_switch_alloc(port->sw->tb, &port->sw->dev,
639 tb_downstream_route(port));
642 * If there is an error accessing the connected switch
643 * it may be connected to another domain. Also we allow
644 * the other domain to be connected to a max depth switch.
646 if (PTR_ERR(sw) == -EIO || PTR_ERR(sw) == -EADDRNOTAVAIL)
647 tb_scan_xdomain(port);
651 if (tb_switch_configure(sw)) {
657 * If there was previously another domain connected remove it
661 tb_xdomain_remove(port->xdomain);
662 tb_port_unconfigure_xdomain(port);
663 port->xdomain = NULL;
667 * Do not send uevents until we have discovered all existing
668 * tunnels and know which switches were authorized already by
671 if (!tcm->hotplug_active)
672 dev_set_uevent_suppress(&sw->dev, true);
675 * At the moment Thunderbolt 2 and beyond (devices with LC) we
676 * can support runtime PM.
678 sw->rpm = sw->generation > 1;
680 if (tb_switch_add(sw)) {
685 /* Link the switches using both links if available */
686 upstream_port = tb_upstream_port(sw);
687 port->remote = upstream_port;
688 upstream_port->remote = port;
689 if (port->dual_link_port && upstream_port->dual_link_port) {
690 port->dual_link_port->remote = upstream_port->dual_link_port;
691 upstream_port->dual_link_port->remote = port->dual_link_port;
694 /* Enable lane bonding if supported */
695 tb_switch_lane_bonding_enable(sw);
696 /* Set the link configured */
697 tb_switch_configure_link(sw);
699 if (tb_enable_tmu(sw))
700 tb_sw_warn(sw, "failed to enable TMU\n");
702 /* Scan upstream retimers */
703 tb_retimer_scan(upstream_port, true);
706 * Create USB 3.x tunnels only when the switch is plugged to the
707 * domain. This is because we scan the domain also during discovery
708 * and want to discover existing USB 3.x tunnels before we create
711 if (tcm->hotplug_active && tb_tunnel_usb3(sw->tb, sw))
712 tb_sw_warn(sw, "USB3 tunnel creation failed\n");
714 tb_add_dp_resources(sw);
718 static void tb_deactivate_and_free_tunnel(struct tb_tunnel *tunnel)
720 struct tb_port *src_port, *dst_port;
726 tb_tunnel_deactivate(tunnel);
727 list_del(&tunnel->list);
730 src_port = tunnel->src_port;
731 dst_port = tunnel->dst_port;
733 switch (tunnel->type) {
736 * In case of DP tunnel make sure the DP IN resource is
737 * deallocated properly.
739 tb_switch_dealloc_dp_resource(src_port->sw, src_port);
740 /* Now we can allow the domain to runtime suspend again */
741 pm_runtime_mark_last_busy(&dst_port->sw->dev);
742 pm_runtime_put_autosuspend(&dst_port->sw->dev);
743 pm_runtime_mark_last_busy(&src_port->sw->dev);
744 pm_runtime_put_autosuspend(&src_port->sw->dev);
748 tb_reclaim_usb3_bandwidth(tb, src_port, dst_port);
753 * PCIe and DMA tunnels do not consume guaranteed
759 tb_tunnel_free(tunnel);
763 * tb_free_invalid_tunnels() - destroy tunnels of devices that have gone away
765 static void tb_free_invalid_tunnels(struct tb *tb)
767 struct tb_cm *tcm = tb_priv(tb);
768 struct tb_tunnel *tunnel;
771 list_for_each_entry_safe(tunnel, n, &tcm->tunnel_list, list) {
772 if (tb_tunnel_is_invalid(tunnel))
773 tb_deactivate_and_free_tunnel(tunnel);
778 * tb_free_unplugged_children() - traverse hierarchy and free unplugged switches
780 static void tb_free_unplugged_children(struct tb_switch *sw)
782 struct tb_port *port;
784 tb_switch_for_each_port(sw, port) {
785 if (!tb_port_has_remote(port))
788 if (port->remote->sw->is_unplugged) {
789 tb_retimer_remove_all(port);
790 tb_remove_dp_resources(port->remote->sw);
791 tb_switch_unconfigure_link(port->remote->sw);
792 tb_switch_lane_bonding_disable(port->remote->sw);
793 tb_switch_remove(port->remote->sw);
795 if (port->dual_link_port)
796 port->dual_link_port->remote = NULL;
798 tb_free_unplugged_children(port->remote->sw);
803 static struct tb_port *tb_find_pcie_down(struct tb_switch *sw,
804 const struct tb_port *port)
806 struct tb_port *down = NULL;
809 * To keep plugging devices consistently in the same PCIe
810 * hierarchy, do mapping here for switch downstream PCIe ports.
812 if (tb_switch_is_usb4(sw)) {
813 down = usb4_switch_map_pcie_down(sw, port);
814 } else if (!tb_route(sw)) {
815 int phy_port = tb_phy_port_from_link(port->port);
819 * Hard-coded Thunderbolt port to PCIe down port mapping
822 if (tb_switch_is_cactus_ridge(sw) ||
823 tb_switch_is_alpine_ridge(sw))
824 index = !phy_port ? 6 : 7;
825 else if (tb_switch_is_falcon_ridge(sw))
826 index = !phy_port ? 6 : 8;
827 else if (tb_switch_is_titan_ridge(sw))
828 index = !phy_port ? 8 : 9;
832 /* Validate the hard-coding */
833 if (WARN_ON(index > sw->config.max_port_number))
836 down = &sw->ports[index];
840 if (WARN_ON(!tb_port_is_pcie_down(down)))
842 if (tb_pci_port_is_enabled(down))
849 return tb_find_unused_port(sw, TB_TYPE_PCIE_DOWN);
852 static struct tb_port *tb_find_dp_out(struct tb *tb, struct tb_port *in)
854 struct tb_port *host_port, *port;
855 struct tb_cm *tcm = tb_priv(tb);
857 host_port = tb_route(in->sw) ?
858 tb_port_at(tb_route(in->sw), tb->root_switch) : NULL;
860 list_for_each_entry(port, &tcm->dp_resources, list) {
861 if (!tb_port_is_dpout(port))
864 if (tb_port_is_enabled(port)) {
865 tb_port_dbg(port, "in use\n");
869 tb_port_dbg(port, "DP OUT available\n");
872 * Keep the DP tunnel under the topology starting from
873 * the same host router downstream port.
875 if (host_port && tb_route(port->sw)) {
878 p = tb_port_at(tb_route(port->sw), tb->root_switch);
889 static void tb_tunnel_dp(struct tb *tb)
891 int available_up, available_down, ret, link_nr;
892 struct tb_cm *tcm = tb_priv(tb);
893 struct tb_port *port, *in, *out;
894 struct tb_tunnel *tunnel;
896 if (!tb_acpi_may_tunnel_dp()) {
897 tb_dbg(tb, "DP tunneling disabled, not creating tunnel\n");
902 * Find pair of inactive DP IN and DP OUT adapters and then
903 * establish a DP tunnel between them.
905 tb_dbg(tb, "looking for DP IN <-> DP OUT pairs:\n");
909 list_for_each_entry(port, &tcm->dp_resources, list) {
910 if (!tb_port_is_dpin(port))
913 if (tb_port_is_enabled(port)) {
914 tb_port_dbg(port, "in use\n");
918 tb_port_dbg(port, "DP IN available\n");
920 out = tb_find_dp_out(tb, port);
928 tb_dbg(tb, "no suitable DP IN adapter available, not tunneling\n");
932 tb_dbg(tb, "no suitable DP OUT adapter available, not tunneling\n");
937 * This is only applicable to links that are not bonded (so
938 * when Thunderbolt 1 hardware is involved somewhere in the
939 * topology). For these try to share the DP bandwidth between
943 list_for_each_entry(tunnel, &tcm->tunnel_list, list) {
944 if (tb_tunnel_is_dp(tunnel)) {
951 * DP stream needs the domain to be active so runtime resume
952 * both ends of the tunnel.
954 * This should bring the routers in the middle active as well
955 * and keeps the domain from runtime suspending while the DP
958 pm_runtime_get_sync(&in->sw->dev);
959 pm_runtime_get_sync(&out->sw->dev);
961 if (tb_switch_alloc_dp_resource(in->sw, in)) {
962 tb_port_dbg(in, "no resource available for DP IN, not tunneling\n");
966 /* Make all unused USB3 bandwidth available for the new DP tunnel */
967 ret = tb_release_unused_usb3_bandwidth(tb, in, out);
969 tb_warn(tb, "failed to release unused bandwidth\n");
973 ret = tb_available_bandwidth(tb, in, out, &available_up,
978 tb_dbg(tb, "available bandwidth for new DP tunnel %u/%u Mb/s\n",
979 available_up, available_down);
981 tunnel = tb_tunnel_alloc_dp(tb, in, out, link_nr, available_up,
984 tb_port_dbg(out, "could not allocate DP tunnel\n");
988 if (tb_tunnel_activate(tunnel)) {
989 tb_port_info(out, "DP tunnel activation failed, aborting\n");
993 list_add_tail(&tunnel->list, &tcm->tunnel_list);
994 tb_reclaim_usb3_bandwidth(tb, in, out);
998 tb_tunnel_free(tunnel);
1000 tb_reclaim_usb3_bandwidth(tb, in, out);
1002 tb_switch_dealloc_dp_resource(in->sw, in);
1004 pm_runtime_mark_last_busy(&out->sw->dev);
1005 pm_runtime_put_autosuspend(&out->sw->dev);
1006 pm_runtime_mark_last_busy(&in->sw->dev);
1007 pm_runtime_put_autosuspend(&in->sw->dev);
1010 static void tb_dp_resource_unavailable(struct tb *tb, struct tb_port *port)
1012 struct tb_port *in, *out;
1013 struct tb_tunnel *tunnel;
1015 if (tb_port_is_dpin(port)) {
1016 tb_port_dbg(port, "DP IN resource unavailable\n");
1020 tb_port_dbg(port, "DP OUT resource unavailable\n");
1025 tunnel = tb_find_tunnel(tb, TB_TUNNEL_DP, in, out);
1026 tb_deactivate_and_free_tunnel(tunnel);
1027 list_del_init(&port->list);
1030 * See if there is another DP OUT port that can be used for
1031 * to create another tunnel.
1036 static void tb_dp_resource_available(struct tb *tb, struct tb_port *port)
1038 struct tb_cm *tcm = tb_priv(tb);
1041 if (tb_port_is_enabled(port))
1044 list_for_each_entry(p, &tcm->dp_resources, list) {
1049 tb_port_dbg(port, "DP %s resource available\n",
1050 tb_port_is_dpin(port) ? "IN" : "OUT");
1051 list_add_tail(&port->list, &tcm->dp_resources);
1053 /* Look for suitable DP IN <-> DP OUT pairs now */
1057 static void tb_disconnect_and_release_dp(struct tb *tb)
1059 struct tb_cm *tcm = tb_priv(tb);
1060 struct tb_tunnel *tunnel, *n;
1063 * Tear down all DP tunnels and release their resources. They
1064 * will be re-established after resume based on plug events.
1066 list_for_each_entry_safe_reverse(tunnel, n, &tcm->tunnel_list, list) {
1067 if (tb_tunnel_is_dp(tunnel))
1068 tb_deactivate_and_free_tunnel(tunnel);
1071 while (!list_empty(&tcm->dp_resources)) {
1072 struct tb_port *port;
1074 port = list_first_entry(&tcm->dp_resources,
1075 struct tb_port, list);
1076 list_del_init(&port->list);
1080 static int tb_disconnect_pci(struct tb *tb, struct tb_switch *sw)
1082 struct tb_tunnel *tunnel;
1085 up = tb_switch_find_port(sw, TB_TYPE_PCIE_UP);
1089 tunnel = tb_find_tunnel(tb, TB_TUNNEL_PCI, NULL, up);
1090 if (WARN_ON(!tunnel))
1093 tb_tunnel_deactivate(tunnel);
1094 list_del(&tunnel->list);
1095 tb_tunnel_free(tunnel);
1099 static int tb_tunnel_pci(struct tb *tb, struct tb_switch *sw)
1101 struct tb_port *up, *down, *port;
1102 struct tb_cm *tcm = tb_priv(tb);
1103 struct tb_switch *parent_sw;
1104 struct tb_tunnel *tunnel;
1106 up = tb_switch_find_port(sw, TB_TYPE_PCIE_UP);
1111 * Look up available down port. Since we are chaining it should
1112 * be found right above this switch.
1114 parent_sw = tb_to_switch(sw->dev.parent);
1115 port = tb_port_at(tb_route(sw), parent_sw);
1116 down = tb_find_pcie_down(parent_sw, port);
1120 tunnel = tb_tunnel_alloc_pci(tb, up, down);
1124 if (tb_tunnel_activate(tunnel)) {
1126 "PCIe tunnel activation failed, aborting\n");
1127 tb_tunnel_free(tunnel);
1131 list_add_tail(&tunnel->list, &tcm->tunnel_list);
1135 static int tb_approve_xdomain_paths(struct tb *tb, struct tb_xdomain *xd,
1136 int transmit_path, int transmit_ring,
1137 int receive_path, int receive_ring)
1139 struct tb_cm *tcm = tb_priv(tb);
1140 struct tb_port *nhi_port, *dst_port;
1141 struct tb_tunnel *tunnel;
1142 struct tb_switch *sw;
1144 sw = tb_to_switch(xd->dev.parent);
1145 dst_port = tb_port_at(xd->route, sw);
1146 nhi_port = tb_switch_find_port(tb->root_switch, TB_TYPE_NHI);
1148 mutex_lock(&tb->lock);
1149 tunnel = tb_tunnel_alloc_dma(tb, nhi_port, dst_port, transmit_path,
1150 transmit_ring, receive_path, receive_ring);
1152 mutex_unlock(&tb->lock);
1156 if (tb_tunnel_activate(tunnel)) {
1157 tb_port_info(nhi_port,
1158 "DMA tunnel activation failed, aborting\n");
1159 tb_tunnel_free(tunnel);
1160 mutex_unlock(&tb->lock);
1164 list_add_tail(&tunnel->list, &tcm->tunnel_list);
1165 mutex_unlock(&tb->lock);
1169 static void __tb_disconnect_xdomain_paths(struct tb *tb, struct tb_xdomain *xd,
1170 int transmit_path, int transmit_ring,
1171 int receive_path, int receive_ring)
1173 struct tb_cm *tcm = tb_priv(tb);
1174 struct tb_port *nhi_port, *dst_port;
1175 struct tb_tunnel *tunnel, *n;
1176 struct tb_switch *sw;
1178 sw = tb_to_switch(xd->dev.parent);
1179 dst_port = tb_port_at(xd->route, sw);
1180 nhi_port = tb_switch_find_port(tb->root_switch, TB_TYPE_NHI);
1182 list_for_each_entry_safe(tunnel, n, &tcm->tunnel_list, list) {
1183 if (!tb_tunnel_is_dma(tunnel))
1185 if (tunnel->src_port != nhi_port || tunnel->dst_port != dst_port)
1188 if (tb_tunnel_match_dma(tunnel, transmit_path, transmit_ring,
1189 receive_path, receive_ring))
1190 tb_deactivate_and_free_tunnel(tunnel);
1194 static int tb_disconnect_xdomain_paths(struct tb *tb, struct tb_xdomain *xd,
1195 int transmit_path, int transmit_ring,
1196 int receive_path, int receive_ring)
1198 if (!xd->is_unplugged) {
1199 mutex_lock(&tb->lock);
1200 __tb_disconnect_xdomain_paths(tb, xd, transmit_path,
1201 transmit_ring, receive_path,
1203 mutex_unlock(&tb->lock);
1208 /* hotplug handling */
1211 * tb_handle_hotplug() - handle hotplug event
1213 * Executes on tb->wq.
1215 static void tb_handle_hotplug(struct work_struct *work)
1217 struct tb_hotplug_event *ev = container_of(work, typeof(*ev), work);
1218 struct tb *tb = ev->tb;
1219 struct tb_cm *tcm = tb_priv(tb);
1220 struct tb_switch *sw;
1221 struct tb_port *port;
1223 /* Bring the domain back from sleep if it was suspended */
1224 pm_runtime_get_sync(&tb->dev);
1226 mutex_lock(&tb->lock);
1227 if (!tcm->hotplug_active)
1228 goto out; /* during init, suspend or shutdown */
1230 sw = tb_switch_find_by_route(tb, ev->route);
1233 "hotplug event from non existent switch %llx:%x (unplug: %d)\n",
1234 ev->route, ev->port, ev->unplug);
1237 if (ev->port > sw->config.max_port_number) {
1239 "hotplug event from non existent port %llx:%x (unplug: %d)\n",
1240 ev->route, ev->port, ev->unplug);
1243 port = &sw->ports[ev->port];
1244 if (tb_is_upstream_port(port)) {
1245 tb_dbg(tb, "hotplug event for upstream port %llx:%x (unplug: %d)\n",
1246 ev->route, ev->port, ev->unplug);
1250 pm_runtime_get_sync(&sw->dev);
1253 tb_retimer_remove_all(port);
1255 if (tb_port_has_remote(port)) {
1256 tb_port_dbg(port, "switch unplugged\n");
1257 tb_sw_set_unplugged(port->remote->sw);
1258 tb_free_invalid_tunnels(tb);
1259 tb_remove_dp_resources(port->remote->sw);
1260 tb_switch_tmu_disable(port->remote->sw);
1261 tb_switch_unconfigure_link(port->remote->sw);
1262 tb_switch_lane_bonding_disable(port->remote->sw);
1263 tb_switch_remove(port->remote->sw);
1264 port->remote = NULL;
1265 if (port->dual_link_port)
1266 port->dual_link_port->remote = NULL;
1267 /* Maybe we can create another DP tunnel */
1269 } else if (port->xdomain) {
1270 struct tb_xdomain *xd = tb_xdomain_get(port->xdomain);
1272 tb_port_dbg(port, "xdomain unplugged\n");
1274 * Service drivers are unbound during
1275 * tb_xdomain_remove() so setting XDomain as
1276 * unplugged here prevents deadlock if they call
1277 * tb_xdomain_disable_paths(). We will tear down
1278 * all the tunnels below.
1280 xd->is_unplugged = true;
1281 tb_xdomain_remove(xd);
1282 port->xdomain = NULL;
1283 __tb_disconnect_xdomain_paths(tb, xd, -1, -1, -1, -1);
1285 tb_port_unconfigure_xdomain(port);
1286 } else if (tb_port_is_dpout(port) || tb_port_is_dpin(port)) {
1287 tb_dp_resource_unavailable(tb, port);
1290 "got unplug event for disconnected port, ignoring\n");
1292 } else if (port->remote) {
1293 tb_port_dbg(port, "got plug event for connected port, ignoring\n");
1295 if (tb_port_is_null(port)) {
1296 tb_port_dbg(port, "hotplug: scanning\n");
1299 tb_port_dbg(port, "hotplug: no switch found\n");
1300 } else if (tb_port_is_dpout(port) || tb_port_is_dpin(port)) {
1301 tb_dp_resource_available(tb, port);
1305 pm_runtime_mark_last_busy(&sw->dev);
1306 pm_runtime_put_autosuspend(&sw->dev);
1311 mutex_unlock(&tb->lock);
1313 pm_runtime_mark_last_busy(&tb->dev);
1314 pm_runtime_put_autosuspend(&tb->dev);
1320 * tb_schedule_hotplug_handler() - callback function for the control channel
1322 * Delegates to tb_handle_hotplug.
1324 static void tb_handle_event(struct tb *tb, enum tb_cfg_pkg_type type,
1325 const void *buf, size_t size)
1327 const struct cfg_event_pkg *pkg = buf;
1330 if (type != TB_CFG_PKG_EVENT) {
1331 tb_warn(tb, "unexpected event %#x, ignoring\n", type);
1335 route = tb_cfg_get_route(&pkg->header);
1337 if (tb_cfg_ack_plug(tb->ctl, route, pkg->port, pkg->unplug)) {
1338 tb_warn(tb, "could not ack plug event on %llx:%x\n", route,
1342 tb_queue_hotplug(tb, route, pkg->port, pkg->unplug);
1345 static void tb_stop(struct tb *tb)
1347 struct tb_cm *tcm = tb_priv(tb);
1348 struct tb_tunnel *tunnel;
1349 struct tb_tunnel *n;
1351 cancel_delayed_work(&tcm->remove_work);
1352 /* tunnels are only present after everything has been initialized */
1353 list_for_each_entry_safe(tunnel, n, &tcm->tunnel_list, list) {
1355 * DMA tunnels require the driver to be functional so we
1356 * tear them down. Other protocol tunnels can be left
1359 if (tb_tunnel_is_dma(tunnel))
1360 tb_tunnel_deactivate(tunnel);
1361 tb_tunnel_free(tunnel);
1363 tb_switch_remove(tb->root_switch);
1364 tcm->hotplug_active = false; /* signal tb_handle_hotplug to quit */
1367 static int tb_scan_finalize_switch(struct device *dev, void *data)
1369 if (tb_is_switch(dev)) {
1370 struct tb_switch *sw = tb_to_switch(dev);
1373 * If we found that the switch was already setup by the
1374 * boot firmware, mark it as authorized now before we
1375 * send uevent to userspace.
1380 dev_set_uevent_suppress(dev, false);
1381 kobject_uevent(&dev->kobj, KOBJ_ADD);
1382 device_for_each_child(dev, NULL, tb_scan_finalize_switch);
1388 static int tb_start(struct tb *tb)
1390 struct tb_cm *tcm = tb_priv(tb);
1393 tb->root_switch = tb_switch_alloc(tb, &tb->dev, 0);
1394 if (IS_ERR(tb->root_switch))
1395 return PTR_ERR(tb->root_switch);
1398 * ICM firmware upgrade needs running firmware and in native
1399 * mode that is not available so disable firmware upgrade of the
1402 tb->root_switch->no_nvm_upgrade = true;
1403 /* All USB4 routers support runtime PM */
1404 tb->root_switch->rpm = tb_switch_is_usb4(tb->root_switch);
1406 ret = tb_switch_configure(tb->root_switch);
1408 tb_switch_put(tb->root_switch);
1412 /* Announce the switch to the world */
1413 ret = tb_switch_add(tb->root_switch);
1415 tb_switch_put(tb->root_switch);
1419 /* Enable TMU if it is off */
1420 tb_switch_tmu_enable(tb->root_switch);
1421 /* Full scan to discover devices added before the driver was loaded. */
1422 tb_scan_switch(tb->root_switch);
1423 /* Find out tunnels created by the boot firmware */
1424 tb_discover_tunnels(tb);
1425 /* Add DP resources from the DP tunnels created by the boot firmware */
1426 tb_discover_dp_resources(tb);
1428 * If the boot firmware did not create USB 3.x tunnels create them
1429 * now for the whole topology.
1431 tb_create_usb3_tunnels(tb->root_switch);
1432 /* Add DP IN resources for the root switch */
1433 tb_add_dp_resources(tb->root_switch);
1434 /* Make the discovered switches available to the userspace */
1435 device_for_each_child(&tb->root_switch->dev, NULL,
1436 tb_scan_finalize_switch);
1438 /* Allow tb_handle_hotplug to progress events */
1439 tcm->hotplug_active = true;
1443 static int tb_suspend_noirq(struct tb *tb)
1445 struct tb_cm *tcm = tb_priv(tb);
1447 tb_dbg(tb, "suspending...\n");
1448 tb_disconnect_and_release_dp(tb);
1449 tb_switch_suspend(tb->root_switch, false);
1450 tcm->hotplug_active = false; /* signal tb_handle_hotplug to quit */
1451 tb_dbg(tb, "suspend finished\n");
1456 static void tb_restore_children(struct tb_switch *sw)
1458 struct tb_port *port;
1460 /* No need to restore if the router is already unplugged */
1461 if (sw->is_unplugged)
1464 if (tb_enable_tmu(sw))
1465 tb_sw_warn(sw, "failed to restore TMU configuration\n");
1467 tb_switch_for_each_port(sw, port) {
1468 if (!tb_port_has_remote(port) && !port->xdomain)
1472 tb_switch_lane_bonding_enable(port->remote->sw);
1473 tb_switch_configure_link(port->remote->sw);
1475 tb_restore_children(port->remote->sw);
1476 } else if (port->xdomain) {
1477 tb_port_configure_xdomain(port);
1482 static int tb_resume_noirq(struct tb *tb)
1484 struct tb_cm *tcm = tb_priv(tb);
1485 struct tb_tunnel *tunnel, *n;
1486 unsigned int usb3_delay = 0;
1489 tb_dbg(tb, "resuming...\n");
1491 /* remove any pci devices the firmware might have setup */
1492 tb_switch_reset(tb->root_switch);
1494 tb_switch_resume(tb->root_switch);
1495 tb_free_invalid_tunnels(tb);
1496 tb_free_unplugged_children(tb->root_switch);
1497 tb_restore_children(tb->root_switch);
1500 * If we get here from suspend to disk the boot firmware or the
1501 * restore kernel might have created tunnels of its own. Since
1502 * we cannot be sure they are usable for us we find and tear
1505 tb_switch_discover_tunnels(tb->root_switch, &tunnels, false);
1506 list_for_each_entry_safe_reverse(tunnel, n, &tunnels, list) {
1507 if (tb_tunnel_is_usb3(tunnel))
1509 tb_tunnel_deactivate(tunnel);
1510 tb_tunnel_free(tunnel);
1513 /* Re-create our tunnels now */
1514 list_for_each_entry_safe(tunnel, n, &tcm->tunnel_list, list) {
1515 /* USB3 requires delay before it can be re-activated */
1516 if (tb_tunnel_is_usb3(tunnel)) {
1518 /* Only need to do it once */
1521 tb_tunnel_restart(tunnel);
1523 if (!list_empty(&tcm->tunnel_list)) {
1525 * the pcie links need some time to get going.
1526 * 100ms works for me...
1528 tb_dbg(tb, "tunnels restarted, sleeping for 100ms\n");
1531 /* Allow tb_handle_hotplug to progress events */
1532 tcm->hotplug_active = true;
1533 tb_dbg(tb, "resume finished\n");
1538 static int tb_free_unplugged_xdomains(struct tb_switch *sw)
1540 struct tb_port *port;
1543 tb_switch_for_each_port(sw, port) {
1544 if (tb_is_upstream_port(port))
1546 if (port->xdomain && port->xdomain->is_unplugged) {
1547 tb_retimer_remove_all(port);
1548 tb_xdomain_remove(port->xdomain);
1549 tb_port_unconfigure_xdomain(port);
1550 port->xdomain = NULL;
1552 } else if (port->remote) {
1553 ret += tb_free_unplugged_xdomains(port->remote->sw);
1560 static int tb_freeze_noirq(struct tb *tb)
1562 struct tb_cm *tcm = tb_priv(tb);
1564 tcm->hotplug_active = false;
1568 static int tb_thaw_noirq(struct tb *tb)
1570 struct tb_cm *tcm = tb_priv(tb);
1572 tcm->hotplug_active = true;
1576 static void tb_complete(struct tb *tb)
1579 * Release any unplugged XDomains and if there is a case where
1580 * another domain is swapped in place of unplugged XDomain we
1581 * need to run another rescan.
1583 mutex_lock(&tb->lock);
1584 if (tb_free_unplugged_xdomains(tb->root_switch))
1585 tb_scan_switch(tb->root_switch);
1586 mutex_unlock(&tb->lock);
1589 static int tb_runtime_suspend(struct tb *tb)
1591 struct tb_cm *tcm = tb_priv(tb);
1593 mutex_lock(&tb->lock);
1594 tb_switch_suspend(tb->root_switch, true);
1595 tcm->hotplug_active = false;
1596 mutex_unlock(&tb->lock);
1601 static void tb_remove_work(struct work_struct *work)
1603 struct tb_cm *tcm = container_of(work, struct tb_cm, remove_work.work);
1604 struct tb *tb = tcm_to_tb(tcm);
1606 mutex_lock(&tb->lock);
1607 if (tb->root_switch) {
1608 tb_free_unplugged_children(tb->root_switch);
1609 tb_free_unplugged_xdomains(tb->root_switch);
1611 mutex_unlock(&tb->lock);
1614 static int tb_runtime_resume(struct tb *tb)
1616 struct tb_cm *tcm = tb_priv(tb);
1617 struct tb_tunnel *tunnel, *n;
1619 mutex_lock(&tb->lock);
1620 tb_switch_resume(tb->root_switch);
1621 tb_free_invalid_tunnels(tb);
1622 tb_restore_children(tb->root_switch);
1623 list_for_each_entry_safe(tunnel, n, &tcm->tunnel_list, list)
1624 tb_tunnel_restart(tunnel);
1625 tcm->hotplug_active = true;
1626 mutex_unlock(&tb->lock);
1629 * Schedule cleanup of any unplugged devices. Run this in a
1630 * separate thread to avoid possible deadlock if the device
1631 * removal runtime resumes the unplugged device.
1633 queue_delayed_work(tb->wq, &tcm->remove_work, msecs_to_jiffies(50));
1637 static const struct tb_cm_ops tb_cm_ops = {
1640 .suspend_noirq = tb_suspend_noirq,
1641 .resume_noirq = tb_resume_noirq,
1642 .freeze_noirq = tb_freeze_noirq,
1643 .thaw_noirq = tb_thaw_noirq,
1644 .complete = tb_complete,
1645 .runtime_suspend = tb_runtime_suspend,
1646 .runtime_resume = tb_runtime_resume,
1647 .handle_event = tb_handle_event,
1648 .disapprove_switch = tb_disconnect_pci,
1649 .approve_switch = tb_tunnel_pci,
1650 .approve_xdomain_paths = tb_approve_xdomain_paths,
1651 .disconnect_xdomain_paths = tb_disconnect_xdomain_paths,
1655 * During suspend the Thunderbolt controller is reset and all PCIe
1656 * tunnels are lost. The NHI driver will try to reestablish all tunnels
1657 * during resume. This adds device links between the tunneled PCIe
1658 * downstream ports and the NHI so that the device core will make sure
1659 * NHI is resumed first before the rest.
1661 static void tb_apple_add_links(struct tb_nhi *nhi)
1663 struct pci_dev *upstream, *pdev;
1665 if (!x86_apple_machine)
1668 switch (nhi->pdev->device) {
1669 case PCI_DEVICE_ID_INTEL_LIGHT_RIDGE:
1670 case PCI_DEVICE_ID_INTEL_CACTUS_RIDGE_4C:
1671 case PCI_DEVICE_ID_INTEL_FALCON_RIDGE_2C_NHI:
1672 case PCI_DEVICE_ID_INTEL_FALCON_RIDGE_4C_NHI:
1678 upstream = pci_upstream_bridge(nhi->pdev);
1680 if (!pci_is_pcie(upstream))
1682 if (pci_pcie_type(upstream) == PCI_EXP_TYPE_UPSTREAM)
1684 upstream = pci_upstream_bridge(upstream);
1691 * For each hotplug downstream port, create add device link
1692 * back to NHI so that PCIe tunnels can be re-established after
1695 for_each_pci_bridge(pdev, upstream->subordinate) {
1696 const struct device_link *link;
1698 if (!pci_is_pcie(pdev))
1700 if (pci_pcie_type(pdev) != PCI_EXP_TYPE_DOWNSTREAM ||
1701 !pdev->is_hotplug_bridge)
1704 link = device_link_add(&pdev->dev, &nhi->pdev->dev,
1705 DL_FLAG_AUTOREMOVE_SUPPLIER |
1706 DL_FLAG_PM_RUNTIME);
1708 dev_dbg(&nhi->pdev->dev, "created link from %s\n",
1709 dev_name(&pdev->dev));
1711 dev_warn(&nhi->pdev->dev, "device link creation from %s failed\n",
1712 dev_name(&pdev->dev));
1717 struct tb *tb_probe(struct tb_nhi *nhi)
1722 tb = tb_domain_alloc(nhi, TB_TIMEOUT, sizeof(*tcm));
1726 if (tb_acpi_may_tunnel_pcie())
1727 tb->security_level = TB_SECURITY_USER;
1729 tb->security_level = TB_SECURITY_NOPCIE;
1731 tb->cm_ops = &tb_cm_ops;
1734 INIT_LIST_HEAD(&tcm->tunnel_list);
1735 INIT_LIST_HEAD(&tcm->dp_resources);
1736 INIT_DELAYED_WORK(&tcm->remove_work, tb_remove_work);
1738 tb_dbg(tb, "using software connection manager\n");
1740 tb_apple_add_links(nhi);
1741 tb_acpi_add_links(nhi);