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>
18 * struct tb_cm - Simple Thunderbolt connection manager
19 * @tunnel_list: List of active tunnels
20 * @dp_resources: List of available DP resources for DP tunneling
21 * @hotplug_active: tb_handle_hotplug will stop progressing plug
22 * events and exit if this is not set (it needs to
23 * acquire the lock one more time). Used to drain wq
24 * after cfg has been paused.
27 struct list_head tunnel_list;
28 struct list_head dp_resources;
32 struct tb_hotplug_event {
33 struct work_struct work;
40 static void tb_handle_hotplug(struct work_struct *work);
42 static void tb_queue_hotplug(struct tb *tb, u64 route, u8 port, bool unplug)
44 struct tb_hotplug_event *ev;
46 ev = kmalloc(sizeof(*ev), GFP_KERNEL);
54 INIT_WORK(&ev->work, tb_handle_hotplug);
55 queue_work(tb->wq, &ev->work);
58 /* enumeration & hot plug handling */
60 static void tb_add_dp_resources(struct tb_switch *sw)
62 struct tb_cm *tcm = tb_priv(sw->tb);
65 tb_switch_for_each_port(sw, port) {
66 if (!tb_port_is_dpin(port))
69 if (!tb_switch_query_dp_resource(sw, port))
72 list_add_tail(&port->list, &tcm->dp_resources);
73 tb_port_dbg(port, "DP IN resource available\n");
77 static void tb_remove_dp_resources(struct tb_switch *sw)
79 struct tb_cm *tcm = tb_priv(sw->tb);
80 struct tb_port *port, *tmp;
82 /* Clear children resources first */
83 tb_switch_for_each_port(sw, port) {
84 if (tb_port_has_remote(port))
85 tb_remove_dp_resources(port->remote->sw);
88 list_for_each_entry_safe(port, tmp, &tcm->dp_resources, list) {
90 tb_port_dbg(port, "DP OUT resource unavailable\n");
91 list_del_init(&port->list);
96 static void tb_discover_tunnels(struct tb_switch *sw)
98 struct tb *tb = sw->tb;
99 struct tb_cm *tcm = tb_priv(tb);
100 struct tb_port *port;
102 tb_switch_for_each_port(sw, port) {
103 struct tb_tunnel *tunnel = NULL;
105 switch (port->config.type) {
106 case TB_TYPE_DP_HDMI_IN:
107 tunnel = tb_tunnel_discover_dp(tb, port);
110 case TB_TYPE_PCIE_DOWN:
111 tunnel = tb_tunnel_discover_pci(tb, port);
114 case TB_TYPE_USB3_DOWN:
115 tunnel = tb_tunnel_discover_usb3(tb, port);
125 if (tb_tunnel_is_pci(tunnel)) {
126 struct tb_switch *parent = tunnel->dst_port->sw;
128 while (parent != tunnel->src_port->sw) {
130 parent = tb_switch_parent(parent);
134 list_add_tail(&tunnel->list, &tcm->tunnel_list);
137 tb_switch_for_each_port(sw, port) {
138 if (tb_port_has_remote(port))
139 tb_discover_tunnels(port->remote->sw);
143 static void tb_scan_xdomain(struct tb_port *port)
145 struct tb_switch *sw = port->sw;
146 struct tb *tb = sw->tb;
147 struct tb_xdomain *xd;
150 route = tb_downstream_route(port);
151 xd = tb_xdomain_find_by_route(tb, route);
157 xd = tb_xdomain_alloc(tb, &sw->dev, route, tb->root_switch->uuid,
160 tb_port_at(route, sw)->xdomain = xd;
165 static int tb_enable_tmu(struct tb_switch *sw)
169 /* If it is already enabled in correct mode, don't touch it */
170 if (tb_switch_tmu_is_enabled(sw))
173 ret = tb_switch_tmu_disable(sw);
177 ret = tb_switch_tmu_post_time(sw);
181 return tb_switch_tmu_enable(sw);
185 * tb_find_unused_port() - return the first inactive port on @sw
186 * @sw: Switch to find the port on
187 * @type: Port type to look for
189 static struct tb_port *tb_find_unused_port(struct tb_switch *sw,
190 enum tb_port_type type)
192 struct tb_port *port;
194 tb_switch_for_each_port(sw, port) {
195 if (tb_is_upstream_port(port))
197 if (port->config.type != type)
201 if (tb_port_is_enabled(port))
208 static struct tb_port *tb_find_usb3_down(struct tb_switch *sw,
209 const struct tb_port *port)
211 struct tb_port *down;
213 down = usb4_switch_map_usb3_down(sw, port);
214 if (down && !tb_usb3_port_is_enabled(down))
219 static struct tb_tunnel *tb_find_tunnel(struct tb *tb, enum tb_tunnel_type type,
220 struct tb_port *src_port,
221 struct tb_port *dst_port)
223 struct tb_cm *tcm = tb_priv(tb);
224 struct tb_tunnel *tunnel;
226 list_for_each_entry(tunnel, &tcm->tunnel_list, list) {
227 if (tunnel->type == type &&
228 ((src_port && src_port == tunnel->src_port) ||
229 (dst_port && dst_port == tunnel->dst_port))) {
237 static struct tb_tunnel *tb_find_first_usb3_tunnel(struct tb *tb,
238 struct tb_port *src_port,
239 struct tb_port *dst_port)
241 struct tb_port *port, *usb3_down;
242 struct tb_switch *sw;
244 /* Pick the router that is deepest in the topology */
245 if (dst_port->sw->config.depth > src_port->sw->config.depth)
250 /* Can't be the host router */
251 if (sw == tb->root_switch)
254 /* Find the downstream USB4 port that leads to this router */
255 port = tb_port_at(tb_route(sw), tb->root_switch);
256 /* Find the corresponding host router USB3 downstream port */
257 usb3_down = usb4_switch_map_usb3_down(tb->root_switch, port);
261 return tb_find_tunnel(tb, TB_TUNNEL_USB3, usb3_down, NULL);
264 static int tb_available_bandwidth(struct tb *tb, struct tb_port *src_port,
265 struct tb_port *dst_port, int *available_up, int *available_down)
267 int usb3_consumed_up, usb3_consumed_down, ret;
268 struct tb_cm *tcm = tb_priv(tb);
269 struct tb_tunnel *tunnel;
270 struct tb_port *port;
272 tb_port_dbg(dst_port, "calculating available bandwidth\n");
274 tunnel = tb_find_first_usb3_tunnel(tb, src_port, dst_port);
276 ret = tb_tunnel_consumed_bandwidth(tunnel, &usb3_consumed_up,
277 &usb3_consumed_down);
281 usb3_consumed_up = 0;
282 usb3_consumed_down = 0;
285 *available_up = *available_down = 40000;
287 /* Find the minimum available bandwidth over all links */
288 tb_for_each_port_on_path(src_port, dst_port, port) {
289 int link_speed, link_width, up_bw, down_bw;
291 if (!tb_port_is_null(port))
294 if (tb_is_upstream_port(port)) {
295 link_speed = port->sw->link_speed;
297 link_speed = tb_port_get_link_speed(port);
302 link_width = port->bonded ? 2 : 1;
304 up_bw = link_speed * link_width * 1000; /* Mb/s */
305 /* Leave 10% guard band */
309 tb_port_dbg(port, "link total bandwidth %d Mb/s\n", up_bw);
312 * Find all DP tunnels that cross the port and reduce
313 * their consumed bandwidth from the available.
315 list_for_each_entry(tunnel, &tcm->tunnel_list, list) {
316 int dp_consumed_up, dp_consumed_down;
318 if (!tb_tunnel_is_dp(tunnel))
321 if (!tb_tunnel_port_on_path(tunnel, port))
324 ret = tb_tunnel_consumed_bandwidth(tunnel,
330 up_bw -= dp_consumed_up;
331 down_bw -= dp_consumed_down;
335 * If USB3 is tunneled from the host router down to the
336 * branch leading to port we need to take USB3 consumed
337 * bandwidth into account regardless whether it actually
340 up_bw -= usb3_consumed_up;
341 down_bw -= usb3_consumed_down;
343 if (up_bw < *available_up)
344 *available_up = up_bw;
345 if (down_bw < *available_down)
346 *available_down = down_bw;
349 if (*available_up < 0)
351 if (*available_down < 0)
357 static int tb_release_unused_usb3_bandwidth(struct tb *tb,
358 struct tb_port *src_port,
359 struct tb_port *dst_port)
361 struct tb_tunnel *tunnel;
363 tunnel = tb_find_first_usb3_tunnel(tb, src_port, dst_port);
364 return tunnel ? tb_tunnel_release_unused_bandwidth(tunnel) : 0;
367 static void tb_reclaim_usb3_bandwidth(struct tb *tb, struct tb_port *src_port,
368 struct tb_port *dst_port)
370 int ret, available_up, available_down;
371 struct tb_tunnel *tunnel;
373 tunnel = tb_find_first_usb3_tunnel(tb, src_port, dst_port);
377 tb_dbg(tb, "reclaiming unused bandwidth for USB3\n");
380 * Calculate available bandwidth for the first hop USB3 tunnel.
381 * That determines the whole USB3 bandwidth for this branch.
383 ret = tb_available_bandwidth(tb, tunnel->src_port, tunnel->dst_port,
384 &available_up, &available_down);
386 tb_warn(tb, "failed to calculate available bandwidth\n");
390 tb_dbg(tb, "available bandwidth for USB3 %d/%d Mb/s\n",
391 available_up, available_down);
393 tb_tunnel_reclaim_available_bandwidth(tunnel, &available_up, &available_down);
396 static int tb_tunnel_usb3(struct tb *tb, struct tb_switch *sw)
398 struct tb_switch *parent = tb_switch_parent(sw);
399 int ret, available_up, available_down;
400 struct tb_port *up, *down, *port;
401 struct tb_cm *tcm = tb_priv(tb);
402 struct tb_tunnel *tunnel;
404 up = tb_switch_find_port(sw, TB_TYPE_USB3_UP);
412 * Look up available down port. Since we are chaining it should
413 * be found right above this switch.
415 port = tb_port_at(tb_route(sw), parent);
416 down = tb_find_usb3_down(parent, port);
420 if (tb_route(parent)) {
421 struct tb_port *parent_up;
423 * Check first that the parent switch has its upstream USB3
424 * port enabled. Otherwise the chain is not complete and
425 * there is no point setting up a new tunnel.
427 parent_up = tb_switch_find_port(parent, TB_TYPE_USB3_UP);
428 if (!parent_up || !tb_port_is_enabled(parent_up))
431 /* Make all unused bandwidth available for the new tunnel */
432 ret = tb_release_unused_usb3_bandwidth(tb, down, up);
437 ret = tb_available_bandwidth(tb, down, up, &available_up,
442 tb_port_dbg(up, "available bandwidth for new USB3 tunnel %d/%d Mb/s\n",
443 available_up, available_down);
445 tunnel = tb_tunnel_alloc_usb3(tb, up, down, available_up,
452 if (tb_tunnel_activate(tunnel)) {
454 "USB3 tunnel activation failed, aborting\n");
459 list_add_tail(&tunnel->list, &tcm->tunnel_list);
460 if (tb_route(parent))
461 tb_reclaim_usb3_bandwidth(tb, down, up);
466 tb_tunnel_free(tunnel);
468 if (tb_route(parent))
469 tb_reclaim_usb3_bandwidth(tb, down, up);
474 static int tb_create_usb3_tunnels(struct tb_switch *sw)
476 struct tb_port *port;
480 ret = tb_tunnel_usb3(sw->tb, sw);
485 tb_switch_for_each_port(sw, port) {
486 if (!tb_port_has_remote(port))
488 ret = tb_create_usb3_tunnels(port->remote->sw);
496 static void tb_scan_port(struct tb_port *port);
499 * tb_scan_switch() - scan for and initialize downstream switches
501 static void tb_scan_switch(struct tb_switch *sw)
503 struct tb_port *port;
505 tb_switch_for_each_port(sw, port)
510 * tb_scan_port() - check for and initialize switches below port
512 static void tb_scan_port(struct tb_port *port)
514 struct tb_cm *tcm = tb_priv(port->sw->tb);
515 struct tb_port *upstream_port;
516 struct tb_switch *sw;
518 if (tb_is_upstream_port(port))
521 if (tb_port_is_dpout(port) && tb_dp_port_hpd_is_active(port) == 1 &&
522 !tb_dp_port_is_enabled(port)) {
523 tb_port_dbg(port, "DP adapter HPD set, queuing hotplug\n");
524 tb_queue_hotplug(port->sw->tb, tb_route(port->sw), port->port,
529 if (port->config.type != TB_TYPE_PORT)
531 if (port->dual_link_port && port->link_nr)
533 * Downstream switch is reachable through two ports.
534 * Only scan on the primary port (link_nr == 0).
536 if (tb_wait_for_port(port, false) <= 0)
539 tb_port_dbg(port, "port already has a remote\n");
543 tb_retimer_scan(port);
545 sw = tb_switch_alloc(port->sw->tb, &port->sw->dev,
546 tb_downstream_route(port));
549 * If there is an error accessing the connected switch
550 * it may be connected to another domain. Also we allow
551 * the other domain to be connected to a max depth switch.
553 if (PTR_ERR(sw) == -EIO || PTR_ERR(sw) == -EADDRNOTAVAIL)
554 tb_scan_xdomain(port);
558 if (tb_switch_configure(sw)) {
564 * If there was previously another domain connected remove it
568 tb_xdomain_remove(port->xdomain);
569 port->xdomain = NULL;
573 * Do not send uevents until we have discovered all existing
574 * tunnels and know which switches were authorized already by
577 if (!tcm->hotplug_active)
578 dev_set_uevent_suppress(&sw->dev, true);
580 if (tb_switch_add(sw)) {
585 /* Link the switches using both links if available */
586 upstream_port = tb_upstream_port(sw);
587 port->remote = upstream_port;
588 upstream_port->remote = port;
589 if (port->dual_link_port && upstream_port->dual_link_port) {
590 port->dual_link_port->remote = upstream_port->dual_link_port;
591 upstream_port->dual_link_port->remote = port->dual_link_port;
594 /* Enable lane bonding if supported */
595 if (tb_switch_lane_bonding_enable(sw))
596 tb_sw_warn(sw, "failed to enable lane bonding\n");
598 if (tb_enable_tmu(sw))
599 tb_sw_warn(sw, "failed to enable TMU\n");
601 /* Scan upstream retimers */
602 tb_retimer_scan(upstream_port);
605 * Create USB 3.x tunnels only when the switch is plugged to the
606 * domain. This is because we scan the domain also during discovery
607 * and want to discover existing USB 3.x tunnels before we create
610 if (tcm->hotplug_active && tb_tunnel_usb3(sw->tb, sw))
611 tb_sw_warn(sw, "USB3 tunnel creation failed\n");
613 tb_add_dp_resources(sw);
617 static void tb_deactivate_and_free_tunnel(struct tb_tunnel *tunnel)
619 struct tb_port *src_port, *dst_port;
625 tb_tunnel_deactivate(tunnel);
626 list_del(&tunnel->list);
629 src_port = tunnel->src_port;
630 dst_port = tunnel->dst_port;
632 switch (tunnel->type) {
635 * In case of DP tunnel make sure the DP IN resource is
636 * deallocated properly.
638 tb_switch_dealloc_dp_resource(src_port->sw, src_port);
642 tb_reclaim_usb3_bandwidth(tb, src_port, dst_port);
647 * PCIe and DMA tunnels do not consume guaranteed
653 tb_tunnel_free(tunnel);
657 * tb_free_invalid_tunnels() - destroy tunnels of devices that have gone away
659 static void tb_free_invalid_tunnels(struct tb *tb)
661 struct tb_cm *tcm = tb_priv(tb);
662 struct tb_tunnel *tunnel;
665 list_for_each_entry_safe(tunnel, n, &tcm->tunnel_list, list) {
666 if (tb_tunnel_is_invalid(tunnel))
667 tb_deactivate_and_free_tunnel(tunnel);
672 * tb_free_unplugged_children() - traverse hierarchy and free unplugged switches
674 static void tb_free_unplugged_children(struct tb_switch *sw)
676 struct tb_port *port;
678 tb_switch_for_each_port(sw, port) {
679 if (!tb_port_has_remote(port))
682 if (port->remote->sw->is_unplugged) {
683 tb_retimer_remove_all(port);
684 tb_remove_dp_resources(port->remote->sw);
685 tb_switch_lane_bonding_disable(port->remote->sw);
686 tb_switch_remove(port->remote->sw);
688 if (port->dual_link_port)
689 port->dual_link_port->remote = NULL;
691 tb_free_unplugged_children(port->remote->sw);
696 static struct tb_port *tb_find_pcie_down(struct tb_switch *sw,
697 const struct tb_port *port)
699 struct tb_port *down = NULL;
702 * To keep plugging devices consistently in the same PCIe
703 * hierarchy, do mapping here for switch downstream PCIe ports.
705 if (tb_switch_is_usb4(sw)) {
706 down = usb4_switch_map_pcie_down(sw, port);
707 } else if (!tb_route(sw)) {
708 int phy_port = tb_phy_port_from_link(port->port);
712 * Hard-coded Thunderbolt port to PCIe down port mapping
715 if (tb_switch_is_cactus_ridge(sw) ||
716 tb_switch_is_alpine_ridge(sw))
717 index = !phy_port ? 6 : 7;
718 else if (tb_switch_is_falcon_ridge(sw))
719 index = !phy_port ? 6 : 8;
720 else if (tb_switch_is_titan_ridge(sw))
721 index = !phy_port ? 8 : 9;
725 /* Validate the hard-coding */
726 if (WARN_ON(index > sw->config.max_port_number))
729 down = &sw->ports[index];
733 if (WARN_ON(!tb_port_is_pcie_down(down)))
735 if (tb_pci_port_is_enabled(down))
742 return tb_find_unused_port(sw, TB_TYPE_PCIE_DOWN);
745 static struct tb_port *tb_find_dp_out(struct tb *tb, struct tb_port *in)
747 struct tb_port *host_port, *port;
748 struct tb_cm *tcm = tb_priv(tb);
750 host_port = tb_route(in->sw) ?
751 tb_port_at(tb_route(in->sw), tb->root_switch) : NULL;
753 list_for_each_entry(port, &tcm->dp_resources, list) {
754 if (!tb_port_is_dpout(port))
757 if (tb_port_is_enabled(port)) {
758 tb_port_dbg(port, "in use\n");
762 tb_port_dbg(port, "DP OUT available\n");
765 * Keep the DP tunnel under the topology starting from
766 * the same host router downstream port.
768 if (host_port && tb_route(port->sw)) {
771 p = tb_port_at(tb_route(port->sw), tb->root_switch);
782 static void tb_tunnel_dp(struct tb *tb)
784 int available_up, available_down, ret;
785 struct tb_cm *tcm = tb_priv(tb);
786 struct tb_port *port, *in, *out;
787 struct tb_tunnel *tunnel;
790 * Find pair of inactive DP IN and DP OUT adapters and then
791 * establish a DP tunnel between them.
793 tb_dbg(tb, "looking for DP IN <-> DP OUT pairs:\n");
797 list_for_each_entry(port, &tcm->dp_resources, list) {
798 if (!tb_port_is_dpin(port))
801 if (tb_port_is_enabled(port)) {
802 tb_port_dbg(port, "in use\n");
806 tb_port_dbg(port, "DP IN available\n");
808 out = tb_find_dp_out(tb, port);
816 tb_dbg(tb, "no suitable DP IN adapter available, not tunneling\n");
820 tb_dbg(tb, "no suitable DP OUT adapter available, not tunneling\n");
824 if (tb_switch_alloc_dp_resource(in->sw, in)) {
825 tb_port_dbg(in, "no resource available for DP IN, not tunneling\n");
829 /* Make all unused USB3 bandwidth available for the new DP tunnel */
830 ret = tb_release_unused_usb3_bandwidth(tb, in, out);
832 tb_warn(tb, "failed to release unused bandwidth\n");
836 ret = tb_available_bandwidth(tb, in, out, &available_up,
841 tb_dbg(tb, "available bandwidth for new DP tunnel %u/%u Mb/s\n",
842 available_up, available_down);
844 tunnel = tb_tunnel_alloc_dp(tb, in, out, available_up, available_down);
846 tb_port_dbg(out, "could not allocate DP tunnel\n");
850 if (tb_tunnel_activate(tunnel)) {
851 tb_port_info(out, "DP tunnel activation failed, aborting\n");
855 list_add_tail(&tunnel->list, &tcm->tunnel_list);
856 tb_reclaim_usb3_bandwidth(tb, in, out);
860 tb_tunnel_free(tunnel);
862 tb_reclaim_usb3_bandwidth(tb, in, out);
864 tb_switch_dealloc_dp_resource(in->sw, in);
867 static void tb_dp_resource_unavailable(struct tb *tb, struct tb_port *port)
869 struct tb_port *in, *out;
870 struct tb_tunnel *tunnel;
872 if (tb_port_is_dpin(port)) {
873 tb_port_dbg(port, "DP IN resource unavailable\n");
877 tb_port_dbg(port, "DP OUT resource unavailable\n");
882 tunnel = tb_find_tunnel(tb, TB_TUNNEL_DP, in, out);
883 tb_deactivate_and_free_tunnel(tunnel);
884 list_del_init(&port->list);
887 * See if there is another DP OUT port that can be used for
888 * to create another tunnel.
893 static void tb_dp_resource_available(struct tb *tb, struct tb_port *port)
895 struct tb_cm *tcm = tb_priv(tb);
898 if (tb_port_is_enabled(port))
901 list_for_each_entry(p, &tcm->dp_resources, list) {
906 tb_port_dbg(port, "DP %s resource available\n",
907 tb_port_is_dpin(port) ? "IN" : "OUT");
908 list_add_tail(&port->list, &tcm->dp_resources);
910 /* Look for suitable DP IN <-> DP OUT pairs now */
914 static int tb_tunnel_pci(struct tb *tb, struct tb_switch *sw)
916 struct tb_port *up, *down, *port;
917 struct tb_cm *tcm = tb_priv(tb);
918 struct tb_switch *parent_sw;
919 struct tb_tunnel *tunnel;
921 up = tb_switch_find_port(sw, TB_TYPE_PCIE_UP);
926 * Look up available down port. Since we are chaining it should
927 * be found right above this switch.
929 parent_sw = tb_to_switch(sw->dev.parent);
930 port = tb_port_at(tb_route(sw), parent_sw);
931 down = tb_find_pcie_down(parent_sw, port);
935 tunnel = tb_tunnel_alloc_pci(tb, up, down);
939 if (tb_tunnel_activate(tunnel)) {
941 "PCIe tunnel activation failed, aborting\n");
942 tb_tunnel_free(tunnel);
946 list_add_tail(&tunnel->list, &tcm->tunnel_list);
950 static int tb_approve_xdomain_paths(struct tb *tb, struct tb_xdomain *xd)
952 struct tb_cm *tcm = tb_priv(tb);
953 struct tb_port *nhi_port, *dst_port;
954 struct tb_tunnel *tunnel;
955 struct tb_switch *sw;
957 sw = tb_to_switch(xd->dev.parent);
958 dst_port = tb_port_at(xd->route, sw);
959 nhi_port = tb_switch_find_port(tb->root_switch, TB_TYPE_NHI);
961 mutex_lock(&tb->lock);
962 tunnel = tb_tunnel_alloc_dma(tb, nhi_port, dst_port, xd->transmit_ring,
963 xd->transmit_path, xd->receive_ring,
966 mutex_unlock(&tb->lock);
970 if (tb_tunnel_activate(tunnel)) {
971 tb_port_info(nhi_port,
972 "DMA tunnel activation failed, aborting\n");
973 tb_tunnel_free(tunnel);
974 mutex_unlock(&tb->lock);
978 list_add_tail(&tunnel->list, &tcm->tunnel_list);
979 mutex_unlock(&tb->lock);
983 static void __tb_disconnect_xdomain_paths(struct tb *tb, struct tb_xdomain *xd)
985 struct tb_port *dst_port;
986 struct tb_tunnel *tunnel;
987 struct tb_switch *sw;
989 sw = tb_to_switch(xd->dev.parent);
990 dst_port = tb_port_at(xd->route, sw);
993 * It is possible that the tunnel was already teared down (in
994 * case of cable disconnect) so it is fine if we cannot find it
997 tunnel = tb_find_tunnel(tb, TB_TUNNEL_DMA, NULL, dst_port);
998 tb_deactivate_and_free_tunnel(tunnel);
1001 static int tb_disconnect_xdomain_paths(struct tb *tb, struct tb_xdomain *xd)
1003 if (!xd->is_unplugged) {
1004 mutex_lock(&tb->lock);
1005 __tb_disconnect_xdomain_paths(tb, xd);
1006 mutex_unlock(&tb->lock);
1011 /* hotplug handling */
1014 * tb_handle_hotplug() - handle hotplug event
1016 * Executes on tb->wq.
1018 static void tb_handle_hotplug(struct work_struct *work)
1020 struct tb_hotplug_event *ev = container_of(work, typeof(*ev), work);
1021 struct tb *tb = ev->tb;
1022 struct tb_cm *tcm = tb_priv(tb);
1023 struct tb_switch *sw;
1024 struct tb_port *port;
1025 mutex_lock(&tb->lock);
1026 if (!tcm->hotplug_active)
1027 goto out; /* during init, suspend or shutdown */
1029 sw = tb_switch_find_by_route(tb, ev->route);
1032 "hotplug event from non existent switch %llx:%x (unplug: %d)\n",
1033 ev->route, ev->port, ev->unplug);
1036 if (ev->port > sw->config.max_port_number) {
1038 "hotplug event from non existent port %llx:%x (unplug: %d)\n",
1039 ev->route, ev->port, ev->unplug);
1042 port = &sw->ports[ev->port];
1043 if (tb_is_upstream_port(port)) {
1044 tb_dbg(tb, "hotplug event for upstream port %llx:%x (unplug: %d)\n",
1045 ev->route, ev->port, ev->unplug);
1049 tb_retimer_remove_all(port);
1051 if (tb_port_has_remote(port)) {
1052 tb_port_dbg(port, "switch unplugged\n");
1053 tb_sw_set_unplugged(port->remote->sw);
1054 tb_free_invalid_tunnels(tb);
1055 tb_remove_dp_resources(port->remote->sw);
1056 tb_switch_tmu_disable(port->remote->sw);
1057 tb_switch_lane_bonding_disable(port->remote->sw);
1058 tb_switch_remove(port->remote->sw);
1059 port->remote = NULL;
1060 if (port->dual_link_port)
1061 port->dual_link_port->remote = NULL;
1062 /* Maybe we can create another DP tunnel */
1064 } else if (port->xdomain) {
1065 struct tb_xdomain *xd = tb_xdomain_get(port->xdomain);
1067 tb_port_dbg(port, "xdomain unplugged\n");
1069 * Service drivers are unbound during
1070 * tb_xdomain_remove() so setting XDomain as
1071 * unplugged here prevents deadlock if they call
1072 * tb_xdomain_disable_paths(). We will tear down
1075 xd->is_unplugged = true;
1076 tb_xdomain_remove(xd);
1077 port->xdomain = NULL;
1078 __tb_disconnect_xdomain_paths(tb, xd);
1080 } else if (tb_port_is_dpout(port) || tb_port_is_dpin(port)) {
1081 tb_dp_resource_unavailable(tb, port);
1084 "got unplug event for disconnected port, ignoring\n");
1086 } else if (port->remote) {
1087 tb_port_dbg(port, "got plug event for connected port, ignoring\n");
1089 if (tb_port_is_null(port)) {
1090 tb_port_dbg(port, "hotplug: scanning\n");
1093 tb_port_dbg(port, "hotplug: no switch found\n");
1094 } else if (tb_port_is_dpout(port) || tb_port_is_dpin(port)) {
1095 tb_dp_resource_available(tb, port);
1102 mutex_unlock(&tb->lock);
1107 * tb_schedule_hotplug_handler() - callback function for the control channel
1109 * Delegates to tb_handle_hotplug.
1111 static void tb_handle_event(struct tb *tb, enum tb_cfg_pkg_type type,
1112 const void *buf, size_t size)
1114 const struct cfg_event_pkg *pkg = buf;
1117 if (type != TB_CFG_PKG_EVENT) {
1118 tb_warn(tb, "unexpected event %#x, ignoring\n", type);
1122 route = tb_cfg_get_route(&pkg->header);
1124 if (tb_cfg_ack_plug(tb->ctl, route, pkg->port, pkg->unplug)) {
1125 tb_warn(tb, "could not ack plug event on %llx:%x\n", route,
1129 tb_queue_hotplug(tb, route, pkg->port, pkg->unplug);
1132 static void tb_stop(struct tb *tb)
1134 struct tb_cm *tcm = tb_priv(tb);
1135 struct tb_tunnel *tunnel;
1136 struct tb_tunnel *n;
1138 /* tunnels are only present after everything has been initialized */
1139 list_for_each_entry_safe(tunnel, n, &tcm->tunnel_list, list) {
1141 * DMA tunnels require the driver to be functional so we
1142 * tear them down. Other protocol tunnels can be left
1145 if (tb_tunnel_is_dma(tunnel))
1146 tb_tunnel_deactivate(tunnel);
1147 tb_tunnel_free(tunnel);
1149 tb_switch_remove(tb->root_switch);
1150 tcm->hotplug_active = false; /* signal tb_handle_hotplug to quit */
1153 static int tb_scan_finalize_switch(struct device *dev, void *data)
1155 if (tb_is_switch(dev)) {
1156 struct tb_switch *sw = tb_to_switch(dev);
1159 * If we found that the switch was already setup by the
1160 * boot firmware, mark it as authorized now before we
1161 * send uevent to userspace.
1166 dev_set_uevent_suppress(dev, false);
1167 kobject_uevent(&dev->kobj, KOBJ_ADD);
1168 device_for_each_child(dev, NULL, tb_scan_finalize_switch);
1174 static int tb_start(struct tb *tb)
1176 struct tb_cm *tcm = tb_priv(tb);
1179 tb->root_switch = tb_switch_alloc(tb, &tb->dev, 0);
1180 if (IS_ERR(tb->root_switch))
1181 return PTR_ERR(tb->root_switch);
1184 * ICM firmware upgrade needs running firmware and in native
1185 * mode that is not available so disable firmware upgrade of the
1188 tb->root_switch->no_nvm_upgrade = true;
1190 ret = tb_switch_configure(tb->root_switch);
1192 tb_switch_put(tb->root_switch);
1196 /* Announce the switch to the world */
1197 ret = tb_switch_add(tb->root_switch);
1199 tb_switch_put(tb->root_switch);
1203 /* Enable TMU if it is off */
1204 tb_switch_tmu_enable(tb->root_switch);
1205 /* Full scan to discover devices added before the driver was loaded. */
1206 tb_scan_switch(tb->root_switch);
1207 /* Find out tunnels created by the boot firmware */
1208 tb_discover_tunnels(tb->root_switch);
1210 * If the boot firmware did not create USB 3.x tunnels create them
1211 * now for the whole topology.
1213 tb_create_usb3_tunnels(tb->root_switch);
1214 /* Add DP IN resources for the root switch */
1215 tb_add_dp_resources(tb->root_switch);
1216 /* Make the discovered switches available to the userspace */
1217 device_for_each_child(&tb->root_switch->dev, NULL,
1218 tb_scan_finalize_switch);
1220 /* Allow tb_handle_hotplug to progress events */
1221 tcm->hotplug_active = true;
1225 static int tb_suspend_noirq(struct tb *tb)
1227 struct tb_cm *tcm = tb_priv(tb);
1229 tb_dbg(tb, "suspending...\n");
1230 tb_switch_suspend(tb->root_switch);
1231 tcm->hotplug_active = false; /* signal tb_handle_hotplug to quit */
1232 tb_dbg(tb, "suspend finished\n");
1237 static void tb_restore_children(struct tb_switch *sw)
1239 struct tb_port *port;
1241 if (tb_enable_tmu(sw))
1242 tb_sw_warn(sw, "failed to restore TMU configuration\n");
1244 tb_switch_for_each_port(sw, port) {
1245 if (!tb_port_has_remote(port))
1248 if (tb_switch_lane_bonding_enable(port->remote->sw))
1249 dev_warn(&sw->dev, "failed to restore lane bonding\n");
1251 tb_restore_children(port->remote->sw);
1255 static int tb_resume_noirq(struct tb *tb)
1257 struct tb_cm *tcm = tb_priv(tb);
1258 struct tb_tunnel *tunnel, *n;
1260 tb_dbg(tb, "resuming...\n");
1262 /* remove any pci devices the firmware might have setup */
1263 tb_switch_reset(tb, 0);
1265 tb_switch_resume(tb->root_switch);
1266 tb_free_invalid_tunnels(tb);
1267 tb_free_unplugged_children(tb->root_switch);
1268 tb_restore_children(tb->root_switch);
1269 list_for_each_entry_safe(tunnel, n, &tcm->tunnel_list, list)
1270 tb_tunnel_restart(tunnel);
1271 if (!list_empty(&tcm->tunnel_list)) {
1273 * the pcie links need some time to get going.
1274 * 100ms works for me...
1276 tb_dbg(tb, "tunnels restarted, sleeping for 100ms\n");
1279 /* Allow tb_handle_hotplug to progress events */
1280 tcm->hotplug_active = true;
1281 tb_dbg(tb, "resume finished\n");
1286 static int tb_free_unplugged_xdomains(struct tb_switch *sw)
1288 struct tb_port *port;
1291 tb_switch_for_each_port(sw, port) {
1292 if (tb_is_upstream_port(port))
1294 if (port->xdomain && port->xdomain->is_unplugged) {
1295 tb_retimer_remove_all(port);
1296 tb_xdomain_remove(port->xdomain);
1297 port->xdomain = NULL;
1299 } else if (port->remote) {
1300 ret += tb_free_unplugged_xdomains(port->remote->sw);
1307 static void tb_complete(struct tb *tb)
1310 * Release any unplugged XDomains and if there is a case where
1311 * another domain is swapped in place of unplugged XDomain we
1312 * need to run another rescan.
1314 mutex_lock(&tb->lock);
1315 if (tb_free_unplugged_xdomains(tb->root_switch))
1316 tb_scan_switch(tb->root_switch);
1317 mutex_unlock(&tb->lock);
1320 static const struct tb_cm_ops tb_cm_ops = {
1323 .suspend_noirq = tb_suspend_noirq,
1324 .resume_noirq = tb_resume_noirq,
1325 .complete = tb_complete,
1326 .handle_event = tb_handle_event,
1327 .approve_switch = tb_tunnel_pci,
1328 .approve_xdomain_paths = tb_approve_xdomain_paths,
1329 .disconnect_xdomain_paths = tb_disconnect_xdomain_paths,
1332 struct tb *tb_probe(struct tb_nhi *nhi)
1337 tb = tb_domain_alloc(nhi, sizeof(*tcm));
1341 tb->security_level = TB_SECURITY_USER;
1342 tb->cm_ops = &tb_cm_ops;
1345 INIT_LIST_HEAD(&tcm->tunnel_list);
1346 INIT_LIST_HEAD(&tcm->dp_resources);