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);
148 * In case of DP tunnel exists, change host router's
149 * 1st children TMU mode to HiFi for CL0s to work.
152 tb_switch_enable_tmu_1st_child(tb->root_switch,
153 TB_SWITCH_TMU_RATE_HIFI);
156 case TB_TYPE_PCIE_DOWN:
157 tunnel = tb_tunnel_discover_pci(tb, port, alloc_hopids);
160 case TB_TYPE_USB3_DOWN:
161 tunnel = tb_tunnel_discover_usb3(tb, port, alloc_hopids);
169 list_add_tail(&tunnel->list, list);
172 tb_switch_for_each_port(sw, port) {
173 if (tb_port_has_remote(port)) {
174 tb_switch_discover_tunnels(port->remote->sw, list,
180 static void tb_discover_tunnels(struct tb *tb)
182 struct tb_cm *tcm = tb_priv(tb);
183 struct tb_tunnel *tunnel;
185 tb_switch_discover_tunnels(tb->root_switch, &tcm->tunnel_list, true);
187 list_for_each_entry(tunnel, &tcm->tunnel_list, list) {
188 if (tb_tunnel_is_pci(tunnel)) {
189 struct tb_switch *parent = tunnel->dst_port->sw;
191 while (parent != tunnel->src_port->sw) {
193 parent = tb_switch_parent(parent);
195 } else if (tb_tunnel_is_dp(tunnel)) {
196 /* Keep the domain from powering down */
197 pm_runtime_get_sync(&tunnel->src_port->sw->dev);
198 pm_runtime_get_sync(&tunnel->dst_port->sw->dev);
203 static int tb_port_configure_xdomain(struct tb_port *port, struct tb_xdomain *xd)
205 if (tb_switch_is_usb4(port->sw))
206 return usb4_port_configure_xdomain(port, xd);
207 return tb_lc_configure_xdomain(port);
210 static void tb_port_unconfigure_xdomain(struct tb_port *port)
212 if (tb_switch_is_usb4(port->sw))
213 usb4_port_unconfigure_xdomain(port);
215 tb_lc_unconfigure_xdomain(port);
217 tb_port_enable(port->dual_link_port);
220 static void tb_scan_xdomain(struct tb_port *port)
222 struct tb_switch *sw = port->sw;
223 struct tb *tb = sw->tb;
224 struct tb_xdomain *xd;
227 if (!tb_is_xdomain_enabled())
230 route = tb_downstream_route(port);
231 xd = tb_xdomain_find_by_route(tb, route);
237 xd = tb_xdomain_alloc(tb, &sw->dev, route, tb->root_switch->uuid,
240 tb_port_at(route, sw)->xdomain = xd;
241 tb_port_configure_xdomain(port, xd);
246 static int tb_enable_tmu(struct tb_switch *sw)
250 /* If it is already enabled in correct mode, don't touch it */
251 if (tb_switch_tmu_is_enabled(sw, sw->tmu.unidirectional_request))
254 ret = tb_switch_tmu_disable(sw);
258 ret = tb_switch_tmu_post_time(sw);
262 return tb_switch_tmu_enable(sw);
266 * tb_find_unused_port() - return the first inactive port on @sw
267 * @sw: Switch to find the port on
268 * @type: Port type to look for
270 static struct tb_port *tb_find_unused_port(struct tb_switch *sw,
271 enum tb_port_type type)
273 struct tb_port *port;
275 tb_switch_for_each_port(sw, port) {
276 if (tb_is_upstream_port(port))
278 if (port->config.type != type)
282 if (tb_port_is_enabled(port))
289 static struct tb_port *tb_find_usb3_down(struct tb_switch *sw,
290 const struct tb_port *port)
292 struct tb_port *down;
294 down = usb4_switch_map_usb3_down(sw, port);
295 if (down && !tb_usb3_port_is_enabled(down))
300 static struct tb_tunnel *tb_find_tunnel(struct tb *tb, enum tb_tunnel_type type,
301 struct tb_port *src_port,
302 struct tb_port *dst_port)
304 struct tb_cm *tcm = tb_priv(tb);
305 struct tb_tunnel *tunnel;
307 list_for_each_entry(tunnel, &tcm->tunnel_list, list) {
308 if (tunnel->type == type &&
309 ((src_port && src_port == tunnel->src_port) ||
310 (dst_port && dst_port == tunnel->dst_port))) {
318 static struct tb_tunnel *tb_find_first_usb3_tunnel(struct tb *tb,
319 struct tb_port *src_port,
320 struct tb_port *dst_port)
322 struct tb_port *port, *usb3_down;
323 struct tb_switch *sw;
325 /* Pick the router that is deepest in the topology */
326 if (dst_port->sw->config.depth > src_port->sw->config.depth)
331 /* Can't be the host router */
332 if (sw == tb->root_switch)
335 /* Find the downstream USB4 port that leads to this router */
336 port = tb_port_at(tb_route(sw), tb->root_switch);
337 /* Find the corresponding host router USB3 downstream port */
338 usb3_down = usb4_switch_map_usb3_down(tb->root_switch, port);
342 return tb_find_tunnel(tb, TB_TUNNEL_USB3, usb3_down, NULL);
345 static int tb_available_bandwidth(struct tb *tb, struct tb_port *src_port,
346 struct tb_port *dst_port, int *available_up, int *available_down)
348 int usb3_consumed_up, usb3_consumed_down, ret;
349 struct tb_cm *tcm = tb_priv(tb);
350 struct tb_tunnel *tunnel;
351 struct tb_port *port;
353 tb_port_dbg(dst_port, "calculating available bandwidth\n");
355 tunnel = tb_find_first_usb3_tunnel(tb, src_port, dst_port);
357 ret = tb_tunnel_consumed_bandwidth(tunnel, &usb3_consumed_up,
358 &usb3_consumed_down);
362 usb3_consumed_up = 0;
363 usb3_consumed_down = 0;
366 *available_up = *available_down = 40000;
368 /* Find the minimum available bandwidth over all links */
369 tb_for_each_port_on_path(src_port, dst_port, port) {
370 int link_speed, link_width, up_bw, down_bw;
372 if (!tb_port_is_null(port))
375 if (tb_is_upstream_port(port)) {
376 link_speed = port->sw->link_speed;
378 link_speed = tb_port_get_link_speed(port);
383 link_width = port->bonded ? 2 : 1;
385 up_bw = link_speed * link_width * 1000; /* Mb/s */
386 /* Leave 10% guard band */
390 tb_port_dbg(port, "link total bandwidth %d Mb/s\n", up_bw);
393 * Find all DP tunnels that cross the port and reduce
394 * their consumed bandwidth from the available.
396 list_for_each_entry(tunnel, &tcm->tunnel_list, list) {
397 int dp_consumed_up, dp_consumed_down;
399 if (!tb_tunnel_is_dp(tunnel))
402 if (!tb_tunnel_port_on_path(tunnel, port))
405 ret = tb_tunnel_consumed_bandwidth(tunnel,
411 up_bw -= dp_consumed_up;
412 down_bw -= dp_consumed_down;
416 * If USB3 is tunneled from the host router down to the
417 * branch leading to port we need to take USB3 consumed
418 * bandwidth into account regardless whether it actually
421 up_bw -= usb3_consumed_up;
422 down_bw -= usb3_consumed_down;
424 if (up_bw < *available_up)
425 *available_up = up_bw;
426 if (down_bw < *available_down)
427 *available_down = down_bw;
430 if (*available_up < 0)
432 if (*available_down < 0)
438 static int tb_release_unused_usb3_bandwidth(struct tb *tb,
439 struct tb_port *src_port,
440 struct tb_port *dst_port)
442 struct tb_tunnel *tunnel;
444 tunnel = tb_find_first_usb3_tunnel(tb, src_port, dst_port);
445 return tunnel ? tb_tunnel_release_unused_bandwidth(tunnel) : 0;
448 static void tb_reclaim_usb3_bandwidth(struct tb *tb, struct tb_port *src_port,
449 struct tb_port *dst_port)
451 int ret, available_up, available_down;
452 struct tb_tunnel *tunnel;
454 tunnel = tb_find_first_usb3_tunnel(tb, src_port, dst_port);
458 tb_dbg(tb, "reclaiming unused bandwidth for USB3\n");
461 * Calculate available bandwidth for the first hop USB3 tunnel.
462 * That determines the whole USB3 bandwidth for this branch.
464 ret = tb_available_bandwidth(tb, tunnel->src_port, tunnel->dst_port,
465 &available_up, &available_down);
467 tb_warn(tb, "failed to calculate available bandwidth\n");
471 tb_dbg(tb, "available bandwidth for USB3 %d/%d Mb/s\n",
472 available_up, available_down);
474 tb_tunnel_reclaim_available_bandwidth(tunnel, &available_up, &available_down);
477 static int tb_tunnel_usb3(struct tb *tb, struct tb_switch *sw)
479 struct tb_switch *parent = tb_switch_parent(sw);
480 int ret, available_up, available_down;
481 struct tb_port *up, *down, *port;
482 struct tb_cm *tcm = tb_priv(tb);
483 struct tb_tunnel *tunnel;
485 if (!tb_acpi_may_tunnel_usb3()) {
486 tb_dbg(tb, "USB3 tunneling disabled, not creating tunnel\n");
490 up = tb_switch_find_port(sw, TB_TYPE_USB3_UP);
498 * Look up available down port. Since we are chaining it should
499 * be found right above this switch.
501 port = tb_port_at(tb_route(sw), parent);
502 down = tb_find_usb3_down(parent, port);
506 if (tb_route(parent)) {
507 struct tb_port *parent_up;
509 * Check first that the parent switch has its upstream USB3
510 * port enabled. Otherwise the chain is not complete and
511 * there is no point setting up a new tunnel.
513 parent_up = tb_switch_find_port(parent, TB_TYPE_USB3_UP);
514 if (!parent_up || !tb_port_is_enabled(parent_up))
517 /* Make all unused bandwidth available for the new tunnel */
518 ret = tb_release_unused_usb3_bandwidth(tb, down, up);
523 ret = tb_available_bandwidth(tb, down, up, &available_up,
528 tb_port_dbg(up, "available bandwidth for new USB3 tunnel %d/%d Mb/s\n",
529 available_up, available_down);
531 tunnel = tb_tunnel_alloc_usb3(tb, up, down, available_up,
538 if (tb_tunnel_activate(tunnel)) {
540 "USB3 tunnel activation failed, aborting\n");
545 list_add_tail(&tunnel->list, &tcm->tunnel_list);
546 if (tb_route(parent))
547 tb_reclaim_usb3_bandwidth(tb, down, up);
552 tb_tunnel_free(tunnel);
554 if (tb_route(parent))
555 tb_reclaim_usb3_bandwidth(tb, down, up);
560 static int tb_create_usb3_tunnels(struct tb_switch *sw)
562 struct tb_port *port;
565 if (!tb_acpi_may_tunnel_usb3())
569 ret = tb_tunnel_usb3(sw->tb, sw);
574 tb_switch_for_each_port(sw, port) {
575 if (!tb_port_has_remote(port))
577 ret = tb_create_usb3_tunnels(port->remote->sw);
585 static void tb_scan_port(struct tb_port *port);
588 * tb_scan_switch() - scan for and initialize downstream switches
590 static void tb_scan_switch(struct tb_switch *sw)
592 struct tb_port *port;
594 pm_runtime_get_sync(&sw->dev);
596 tb_switch_for_each_port(sw, port)
599 pm_runtime_mark_last_busy(&sw->dev);
600 pm_runtime_put_autosuspend(&sw->dev);
604 * tb_scan_port() - check for and initialize switches below port
606 static void tb_scan_port(struct tb_port *port)
608 struct tb_cm *tcm = tb_priv(port->sw->tb);
609 struct tb_port *upstream_port;
610 struct tb_switch *sw;
613 if (tb_is_upstream_port(port))
616 if (tb_port_is_dpout(port) && tb_dp_port_hpd_is_active(port) == 1 &&
617 !tb_dp_port_is_enabled(port)) {
618 tb_port_dbg(port, "DP adapter HPD set, queuing hotplug\n");
619 tb_queue_hotplug(port->sw->tb, tb_route(port->sw), port->port,
624 if (port->config.type != TB_TYPE_PORT)
626 if (port->dual_link_port && port->link_nr)
628 * Downstream switch is reachable through two ports.
629 * Only scan on the primary port (link_nr == 0).
631 if (tb_wait_for_port(port, false) <= 0)
634 tb_port_dbg(port, "port already has a remote\n");
638 tb_retimer_scan(port, true);
640 sw = tb_switch_alloc(port->sw->tb, &port->sw->dev,
641 tb_downstream_route(port));
644 * If there is an error accessing the connected switch
645 * it may be connected to another domain. Also we allow
646 * the other domain to be connected to a max depth switch.
648 if (PTR_ERR(sw) == -EIO || PTR_ERR(sw) == -EADDRNOTAVAIL)
649 tb_scan_xdomain(port);
653 if (tb_switch_configure(sw)) {
659 * If there was previously another domain connected remove it
663 tb_xdomain_remove(port->xdomain);
664 tb_port_unconfigure_xdomain(port);
665 port->xdomain = NULL;
669 * Do not send uevents until we have discovered all existing
670 * tunnels and know which switches were authorized already by
673 if (!tcm->hotplug_active)
674 dev_set_uevent_suppress(&sw->dev, true);
677 * At the moment Thunderbolt 2 and beyond (devices with LC) we
678 * can support runtime PM.
680 sw->rpm = sw->generation > 1;
682 if (tb_switch_add(sw)) {
687 /* Link the switches using both links if available */
688 upstream_port = tb_upstream_port(sw);
689 port->remote = upstream_port;
690 upstream_port->remote = port;
691 if (port->dual_link_port && upstream_port->dual_link_port) {
692 port->dual_link_port->remote = upstream_port->dual_link_port;
693 upstream_port->dual_link_port->remote = port->dual_link_port;
696 /* Enable lane bonding if supported */
697 tb_switch_lane_bonding_enable(sw);
698 /* Set the link configured */
699 tb_switch_configure_link(sw);
701 * CL0s and CL1 are enabled and supported together.
702 * Silently ignore CLx enabling in case CLx is not supported.
704 ret = tb_switch_enable_clx(sw, TB_CL1);
705 if (ret && ret != -EOPNOTSUPP)
706 tb_sw_warn(sw, "failed to enable %s on upstream port\n",
707 tb_switch_clx_name(TB_CL1));
709 if (tb_switch_is_clx_enabled(sw, TB_CL1))
711 * To support highest CLx state, we set router's TMU to
714 tb_switch_tmu_configure(sw, TB_SWITCH_TMU_RATE_NORMAL, true);
716 /* If CLx disabled, configure router's TMU to HiFi-Bidir mode*/
717 tb_switch_tmu_configure(sw, TB_SWITCH_TMU_RATE_HIFI, false);
719 if (tb_enable_tmu(sw))
720 tb_sw_warn(sw, "failed to enable TMU\n");
722 /* Scan upstream retimers */
723 tb_retimer_scan(upstream_port, true);
726 * Create USB 3.x tunnels only when the switch is plugged to the
727 * domain. This is because we scan the domain also during discovery
728 * and want to discover existing USB 3.x tunnels before we create
731 if (tcm->hotplug_active && tb_tunnel_usb3(sw->tb, sw))
732 tb_sw_warn(sw, "USB3 tunnel creation failed\n");
734 tb_add_dp_resources(sw);
738 static void tb_deactivate_and_free_tunnel(struct tb_tunnel *tunnel)
740 struct tb_port *src_port, *dst_port;
746 tb_tunnel_deactivate(tunnel);
747 list_del(&tunnel->list);
750 src_port = tunnel->src_port;
751 dst_port = tunnel->dst_port;
753 switch (tunnel->type) {
756 * In case of DP tunnel make sure the DP IN resource is
757 * deallocated properly.
759 tb_switch_dealloc_dp_resource(src_port->sw, src_port);
760 /* Now we can allow the domain to runtime suspend again */
761 pm_runtime_mark_last_busy(&dst_port->sw->dev);
762 pm_runtime_put_autosuspend(&dst_port->sw->dev);
763 pm_runtime_mark_last_busy(&src_port->sw->dev);
764 pm_runtime_put_autosuspend(&src_port->sw->dev);
768 tb_reclaim_usb3_bandwidth(tb, src_port, dst_port);
773 * PCIe and DMA tunnels do not consume guaranteed
779 tb_tunnel_free(tunnel);
783 * tb_free_invalid_tunnels() - destroy tunnels of devices that have gone away
785 static void tb_free_invalid_tunnels(struct tb *tb)
787 struct tb_cm *tcm = tb_priv(tb);
788 struct tb_tunnel *tunnel;
791 list_for_each_entry_safe(tunnel, n, &tcm->tunnel_list, list) {
792 if (tb_tunnel_is_invalid(tunnel))
793 tb_deactivate_and_free_tunnel(tunnel);
798 * tb_free_unplugged_children() - traverse hierarchy and free unplugged switches
800 static void tb_free_unplugged_children(struct tb_switch *sw)
802 struct tb_port *port;
804 tb_switch_for_each_port(sw, port) {
805 if (!tb_port_has_remote(port))
808 if (port->remote->sw->is_unplugged) {
809 tb_retimer_remove_all(port);
810 tb_remove_dp_resources(port->remote->sw);
811 tb_switch_unconfigure_link(port->remote->sw);
812 tb_switch_lane_bonding_disable(port->remote->sw);
813 tb_switch_remove(port->remote->sw);
815 if (port->dual_link_port)
816 port->dual_link_port->remote = NULL;
818 tb_free_unplugged_children(port->remote->sw);
823 static struct tb_port *tb_find_pcie_down(struct tb_switch *sw,
824 const struct tb_port *port)
826 struct tb_port *down = NULL;
829 * To keep plugging devices consistently in the same PCIe
830 * hierarchy, do mapping here for switch downstream PCIe ports.
832 if (tb_switch_is_usb4(sw)) {
833 down = usb4_switch_map_pcie_down(sw, port);
834 } else if (!tb_route(sw)) {
835 int phy_port = tb_phy_port_from_link(port->port);
839 * Hard-coded Thunderbolt port to PCIe down port mapping
842 if (tb_switch_is_cactus_ridge(sw) ||
843 tb_switch_is_alpine_ridge(sw))
844 index = !phy_port ? 6 : 7;
845 else if (tb_switch_is_falcon_ridge(sw))
846 index = !phy_port ? 6 : 8;
847 else if (tb_switch_is_titan_ridge(sw))
848 index = !phy_port ? 8 : 9;
852 /* Validate the hard-coding */
853 if (WARN_ON(index > sw->config.max_port_number))
856 down = &sw->ports[index];
860 if (WARN_ON(!tb_port_is_pcie_down(down)))
862 if (tb_pci_port_is_enabled(down))
869 return tb_find_unused_port(sw, TB_TYPE_PCIE_DOWN);
872 static struct tb_port *tb_find_dp_out(struct tb *tb, struct tb_port *in)
874 struct tb_port *host_port, *port;
875 struct tb_cm *tcm = tb_priv(tb);
877 host_port = tb_route(in->sw) ?
878 tb_port_at(tb_route(in->sw), tb->root_switch) : NULL;
880 list_for_each_entry(port, &tcm->dp_resources, list) {
881 if (!tb_port_is_dpout(port))
884 if (tb_port_is_enabled(port)) {
885 tb_port_dbg(port, "in use\n");
889 tb_port_dbg(port, "DP OUT available\n");
892 * Keep the DP tunnel under the topology starting from
893 * the same host router downstream port.
895 if (host_port && tb_route(port->sw)) {
898 p = tb_port_at(tb_route(port->sw), tb->root_switch);
909 static void tb_tunnel_dp(struct tb *tb)
911 int available_up, available_down, ret, link_nr;
912 struct tb_cm *tcm = tb_priv(tb);
913 struct tb_port *port, *in, *out;
914 struct tb_tunnel *tunnel;
916 if (!tb_acpi_may_tunnel_dp()) {
917 tb_dbg(tb, "DP tunneling disabled, not creating tunnel\n");
922 * Find pair of inactive DP IN and DP OUT adapters and then
923 * establish a DP tunnel between them.
925 tb_dbg(tb, "looking for DP IN <-> DP OUT pairs:\n");
929 list_for_each_entry(port, &tcm->dp_resources, list) {
930 if (!tb_port_is_dpin(port))
933 if (tb_port_is_enabled(port)) {
934 tb_port_dbg(port, "in use\n");
938 tb_port_dbg(port, "DP IN available\n");
940 out = tb_find_dp_out(tb, port);
948 tb_dbg(tb, "no suitable DP IN adapter available, not tunneling\n");
952 tb_dbg(tb, "no suitable DP OUT adapter available, not tunneling\n");
957 * This is only applicable to links that are not bonded (so
958 * when Thunderbolt 1 hardware is involved somewhere in the
959 * topology). For these try to share the DP bandwidth between
963 list_for_each_entry(tunnel, &tcm->tunnel_list, list) {
964 if (tb_tunnel_is_dp(tunnel)) {
971 * DP stream needs the domain to be active so runtime resume
972 * both ends of the tunnel.
974 * This should bring the routers in the middle active as well
975 * and keeps the domain from runtime suspending while the DP
978 pm_runtime_get_sync(&in->sw->dev);
979 pm_runtime_get_sync(&out->sw->dev);
981 if (tb_switch_alloc_dp_resource(in->sw, in)) {
982 tb_port_dbg(in, "no resource available for DP IN, not tunneling\n");
986 /* Make all unused USB3 bandwidth available for the new DP tunnel */
987 ret = tb_release_unused_usb3_bandwidth(tb, in, out);
989 tb_warn(tb, "failed to release unused bandwidth\n");
993 ret = tb_available_bandwidth(tb, in, out, &available_up,
998 tb_dbg(tb, "available bandwidth for new DP tunnel %u/%u Mb/s\n",
999 available_up, available_down);
1001 tunnel = tb_tunnel_alloc_dp(tb, in, out, link_nr, available_up,
1004 tb_port_dbg(out, "could not allocate DP tunnel\n");
1008 if (tb_tunnel_activate(tunnel)) {
1009 tb_port_info(out, "DP tunnel activation failed, aborting\n");
1013 list_add_tail(&tunnel->list, &tcm->tunnel_list);
1014 tb_reclaim_usb3_bandwidth(tb, in, out);
1016 * In case of DP tunnel exists, change host router's 1st children
1017 * TMU mode to HiFi for CL0s to work.
1019 tb_switch_enable_tmu_1st_child(tb->root_switch, TB_SWITCH_TMU_RATE_HIFI);
1024 tb_tunnel_free(tunnel);
1026 tb_reclaim_usb3_bandwidth(tb, in, out);
1028 tb_switch_dealloc_dp_resource(in->sw, in);
1030 pm_runtime_mark_last_busy(&out->sw->dev);
1031 pm_runtime_put_autosuspend(&out->sw->dev);
1032 pm_runtime_mark_last_busy(&in->sw->dev);
1033 pm_runtime_put_autosuspend(&in->sw->dev);
1036 static void tb_dp_resource_unavailable(struct tb *tb, struct tb_port *port)
1038 struct tb_port *in, *out;
1039 struct tb_tunnel *tunnel;
1041 if (tb_port_is_dpin(port)) {
1042 tb_port_dbg(port, "DP IN resource unavailable\n");
1046 tb_port_dbg(port, "DP OUT resource unavailable\n");
1051 tunnel = tb_find_tunnel(tb, TB_TUNNEL_DP, in, out);
1052 tb_deactivate_and_free_tunnel(tunnel);
1053 list_del_init(&port->list);
1056 * See if there is another DP OUT port that can be used for
1057 * to create another tunnel.
1062 static void tb_dp_resource_available(struct tb *tb, struct tb_port *port)
1064 struct tb_cm *tcm = tb_priv(tb);
1067 if (tb_port_is_enabled(port))
1070 list_for_each_entry(p, &tcm->dp_resources, list) {
1075 tb_port_dbg(port, "DP %s resource available\n",
1076 tb_port_is_dpin(port) ? "IN" : "OUT");
1077 list_add_tail(&port->list, &tcm->dp_resources);
1079 /* Look for suitable DP IN <-> DP OUT pairs now */
1083 static void tb_disconnect_and_release_dp(struct tb *tb)
1085 struct tb_cm *tcm = tb_priv(tb);
1086 struct tb_tunnel *tunnel, *n;
1089 * Tear down all DP tunnels and release their resources. They
1090 * will be re-established after resume based on plug events.
1092 list_for_each_entry_safe_reverse(tunnel, n, &tcm->tunnel_list, list) {
1093 if (tb_tunnel_is_dp(tunnel))
1094 tb_deactivate_and_free_tunnel(tunnel);
1097 while (!list_empty(&tcm->dp_resources)) {
1098 struct tb_port *port;
1100 port = list_first_entry(&tcm->dp_resources,
1101 struct tb_port, list);
1102 list_del_init(&port->list);
1106 static int tb_disconnect_pci(struct tb *tb, struct tb_switch *sw)
1108 struct tb_tunnel *tunnel;
1111 up = tb_switch_find_port(sw, TB_TYPE_PCIE_UP);
1115 tunnel = tb_find_tunnel(tb, TB_TUNNEL_PCI, NULL, up);
1116 if (WARN_ON(!tunnel))
1119 tb_switch_xhci_disconnect(sw);
1121 tb_tunnel_deactivate(tunnel);
1122 list_del(&tunnel->list);
1123 tb_tunnel_free(tunnel);
1127 static int tb_tunnel_pci(struct tb *tb, struct tb_switch *sw)
1129 struct tb_port *up, *down, *port;
1130 struct tb_cm *tcm = tb_priv(tb);
1131 struct tb_switch *parent_sw;
1132 struct tb_tunnel *tunnel;
1134 up = tb_switch_find_port(sw, TB_TYPE_PCIE_UP);
1139 * Look up available down port. Since we are chaining it should
1140 * be found right above this switch.
1142 parent_sw = tb_to_switch(sw->dev.parent);
1143 port = tb_port_at(tb_route(sw), parent_sw);
1144 down = tb_find_pcie_down(parent_sw, port);
1148 tunnel = tb_tunnel_alloc_pci(tb, up, down);
1152 if (tb_tunnel_activate(tunnel)) {
1154 "PCIe tunnel activation failed, aborting\n");
1155 tb_tunnel_free(tunnel);
1160 * PCIe L1 is needed to enable CL0s for Titan Ridge so enable it
1163 if (tb_switch_pcie_l1_enable(sw))
1164 tb_sw_warn(sw, "failed to enable PCIe L1 for Titan Ridge\n");
1166 if (tb_switch_xhci_connect(sw))
1167 tb_sw_warn(sw, "failed to connect xHCI\n");
1169 list_add_tail(&tunnel->list, &tcm->tunnel_list);
1173 static int tb_approve_xdomain_paths(struct tb *tb, struct tb_xdomain *xd,
1174 int transmit_path, int transmit_ring,
1175 int receive_path, int receive_ring)
1177 struct tb_cm *tcm = tb_priv(tb);
1178 struct tb_port *nhi_port, *dst_port;
1179 struct tb_tunnel *tunnel;
1180 struct tb_switch *sw;
1182 sw = tb_to_switch(xd->dev.parent);
1183 dst_port = tb_port_at(xd->route, sw);
1184 nhi_port = tb_switch_find_port(tb->root_switch, TB_TYPE_NHI);
1186 mutex_lock(&tb->lock);
1187 tunnel = tb_tunnel_alloc_dma(tb, nhi_port, dst_port, transmit_path,
1188 transmit_ring, receive_path, receive_ring);
1190 mutex_unlock(&tb->lock);
1194 if (tb_tunnel_activate(tunnel)) {
1195 tb_port_info(nhi_port,
1196 "DMA tunnel activation failed, aborting\n");
1197 tb_tunnel_free(tunnel);
1198 mutex_unlock(&tb->lock);
1202 list_add_tail(&tunnel->list, &tcm->tunnel_list);
1203 mutex_unlock(&tb->lock);
1207 static void __tb_disconnect_xdomain_paths(struct tb *tb, struct tb_xdomain *xd,
1208 int transmit_path, int transmit_ring,
1209 int receive_path, int receive_ring)
1211 struct tb_cm *tcm = tb_priv(tb);
1212 struct tb_port *nhi_port, *dst_port;
1213 struct tb_tunnel *tunnel, *n;
1214 struct tb_switch *sw;
1216 sw = tb_to_switch(xd->dev.parent);
1217 dst_port = tb_port_at(xd->route, sw);
1218 nhi_port = tb_switch_find_port(tb->root_switch, TB_TYPE_NHI);
1220 list_for_each_entry_safe(tunnel, n, &tcm->tunnel_list, list) {
1221 if (!tb_tunnel_is_dma(tunnel))
1223 if (tunnel->src_port != nhi_port || tunnel->dst_port != dst_port)
1226 if (tb_tunnel_match_dma(tunnel, transmit_path, transmit_ring,
1227 receive_path, receive_ring))
1228 tb_deactivate_and_free_tunnel(tunnel);
1232 static int tb_disconnect_xdomain_paths(struct tb *tb, struct tb_xdomain *xd,
1233 int transmit_path, int transmit_ring,
1234 int receive_path, int receive_ring)
1236 if (!xd->is_unplugged) {
1237 mutex_lock(&tb->lock);
1238 __tb_disconnect_xdomain_paths(tb, xd, transmit_path,
1239 transmit_ring, receive_path,
1241 mutex_unlock(&tb->lock);
1246 /* hotplug handling */
1249 * tb_handle_hotplug() - handle hotplug event
1251 * Executes on tb->wq.
1253 static void tb_handle_hotplug(struct work_struct *work)
1255 struct tb_hotplug_event *ev = container_of(work, typeof(*ev), work);
1256 struct tb *tb = ev->tb;
1257 struct tb_cm *tcm = tb_priv(tb);
1258 struct tb_switch *sw;
1259 struct tb_port *port;
1261 /* Bring the domain back from sleep if it was suspended */
1262 pm_runtime_get_sync(&tb->dev);
1264 mutex_lock(&tb->lock);
1265 if (!tcm->hotplug_active)
1266 goto out; /* during init, suspend or shutdown */
1268 sw = tb_switch_find_by_route(tb, ev->route);
1271 "hotplug event from non existent switch %llx:%x (unplug: %d)\n",
1272 ev->route, ev->port, ev->unplug);
1275 if (ev->port > sw->config.max_port_number) {
1277 "hotplug event from non existent port %llx:%x (unplug: %d)\n",
1278 ev->route, ev->port, ev->unplug);
1281 port = &sw->ports[ev->port];
1282 if (tb_is_upstream_port(port)) {
1283 tb_dbg(tb, "hotplug event for upstream port %llx:%x (unplug: %d)\n",
1284 ev->route, ev->port, ev->unplug);
1288 pm_runtime_get_sync(&sw->dev);
1291 tb_retimer_remove_all(port);
1293 if (tb_port_has_remote(port)) {
1294 tb_port_dbg(port, "switch unplugged\n");
1295 tb_sw_set_unplugged(port->remote->sw);
1296 tb_free_invalid_tunnels(tb);
1297 tb_remove_dp_resources(port->remote->sw);
1298 tb_switch_tmu_disable(port->remote->sw);
1299 tb_switch_unconfigure_link(port->remote->sw);
1300 tb_switch_lane_bonding_disable(port->remote->sw);
1301 tb_switch_remove(port->remote->sw);
1302 port->remote = NULL;
1303 if (port->dual_link_port)
1304 port->dual_link_port->remote = NULL;
1305 /* Maybe we can create another DP tunnel */
1307 } else if (port->xdomain) {
1308 struct tb_xdomain *xd = tb_xdomain_get(port->xdomain);
1310 tb_port_dbg(port, "xdomain unplugged\n");
1312 * Service drivers are unbound during
1313 * tb_xdomain_remove() so setting XDomain as
1314 * unplugged here prevents deadlock if they call
1315 * tb_xdomain_disable_paths(). We will tear down
1316 * all the tunnels below.
1318 xd->is_unplugged = true;
1319 tb_xdomain_remove(xd);
1320 port->xdomain = NULL;
1321 __tb_disconnect_xdomain_paths(tb, xd, -1, -1, -1, -1);
1323 tb_port_unconfigure_xdomain(port);
1324 } else if (tb_port_is_dpout(port) || tb_port_is_dpin(port)) {
1325 tb_dp_resource_unavailable(tb, port);
1326 } else if (!port->port) {
1327 tb_sw_dbg(sw, "xHCI disconnect request\n");
1328 tb_switch_xhci_disconnect(sw);
1331 "got unplug event for disconnected port, ignoring\n");
1333 } else if (port->remote) {
1334 tb_port_dbg(port, "got plug event for connected port, ignoring\n");
1335 } else if (!port->port && sw->authorized) {
1336 tb_sw_dbg(sw, "xHCI connect request\n");
1337 tb_switch_xhci_connect(sw);
1339 if (tb_port_is_null(port)) {
1340 tb_port_dbg(port, "hotplug: scanning\n");
1343 tb_port_dbg(port, "hotplug: no switch found\n");
1344 } else if (tb_port_is_dpout(port) || tb_port_is_dpin(port)) {
1345 tb_dp_resource_available(tb, port);
1349 pm_runtime_mark_last_busy(&sw->dev);
1350 pm_runtime_put_autosuspend(&sw->dev);
1355 mutex_unlock(&tb->lock);
1357 pm_runtime_mark_last_busy(&tb->dev);
1358 pm_runtime_put_autosuspend(&tb->dev);
1364 * tb_schedule_hotplug_handler() - callback function for the control channel
1366 * Delegates to tb_handle_hotplug.
1368 static void tb_handle_event(struct tb *tb, enum tb_cfg_pkg_type type,
1369 const void *buf, size_t size)
1371 const struct cfg_event_pkg *pkg = buf;
1374 if (type != TB_CFG_PKG_EVENT) {
1375 tb_warn(tb, "unexpected event %#x, ignoring\n", type);
1379 route = tb_cfg_get_route(&pkg->header);
1381 if (tb_cfg_ack_plug(tb->ctl, route, pkg->port, pkg->unplug)) {
1382 tb_warn(tb, "could not ack plug event on %llx:%x\n", route,
1386 tb_queue_hotplug(tb, route, pkg->port, pkg->unplug);
1389 static void tb_stop(struct tb *tb)
1391 struct tb_cm *tcm = tb_priv(tb);
1392 struct tb_tunnel *tunnel;
1393 struct tb_tunnel *n;
1395 cancel_delayed_work(&tcm->remove_work);
1396 /* tunnels are only present after everything has been initialized */
1397 list_for_each_entry_safe(tunnel, n, &tcm->tunnel_list, list) {
1399 * DMA tunnels require the driver to be functional so we
1400 * tear them down. Other protocol tunnels can be left
1403 if (tb_tunnel_is_dma(tunnel))
1404 tb_tunnel_deactivate(tunnel);
1405 tb_tunnel_free(tunnel);
1407 tb_switch_remove(tb->root_switch);
1408 tcm->hotplug_active = false; /* signal tb_handle_hotplug to quit */
1411 static int tb_scan_finalize_switch(struct device *dev, void *data)
1413 if (tb_is_switch(dev)) {
1414 struct tb_switch *sw = tb_to_switch(dev);
1417 * If we found that the switch was already setup by the
1418 * boot firmware, mark it as authorized now before we
1419 * send uevent to userspace.
1424 dev_set_uevent_suppress(dev, false);
1425 kobject_uevent(&dev->kobj, KOBJ_ADD);
1426 device_for_each_child(dev, NULL, tb_scan_finalize_switch);
1432 static int tb_start(struct tb *tb)
1434 struct tb_cm *tcm = tb_priv(tb);
1437 tb->root_switch = tb_switch_alloc(tb, &tb->dev, 0);
1438 if (IS_ERR(tb->root_switch))
1439 return PTR_ERR(tb->root_switch);
1442 * ICM firmware upgrade needs running firmware and in native
1443 * mode that is not available so disable firmware upgrade of the
1446 * However, USB4 routers support NVM firmware upgrade if they
1447 * implement the necessary router operations.
1449 tb->root_switch->no_nvm_upgrade = !tb_switch_is_usb4(tb->root_switch);
1450 /* All USB4 routers support runtime PM */
1451 tb->root_switch->rpm = tb_switch_is_usb4(tb->root_switch);
1453 ret = tb_switch_configure(tb->root_switch);
1455 tb_switch_put(tb->root_switch);
1459 /* Announce the switch to the world */
1460 ret = tb_switch_add(tb->root_switch);
1462 tb_switch_put(tb->root_switch);
1467 * To support highest CLx state, we set host router's TMU to
1470 tb_switch_tmu_configure(tb->root_switch, TB_SWITCH_TMU_RATE_NORMAL,
1472 /* Enable TMU if it is off */
1473 tb_switch_tmu_enable(tb->root_switch);
1474 /* Full scan to discover devices added before the driver was loaded. */
1475 tb_scan_switch(tb->root_switch);
1476 /* Find out tunnels created by the boot firmware */
1477 tb_discover_tunnels(tb);
1478 /* Add DP resources from the DP tunnels created by the boot firmware */
1479 tb_discover_dp_resources(tb);
1481 * If the boot firmware did not create USB 3.x tunnels create them
1482 * now for the whole topology.
1484 tb_create_usb3_tunnels(tb->root_switch);
1485 /* Add DP IN resources for the root switch */
1486 tb_add_dp_resources(tb->root_switch);
1487 /* Make the discovered switches available to the userspace */
1488 device_for_each_child(&tb->root_switch->dev, NULL,
1489 tb_scan_finalize_switch);
1491 /* Allow tb_handle_hotplug to progress events */
1492 tcm->hotplug_active = true;
1496 static int tb_suspend_noirq(struct tb *tb)
1498 struct tb_cm *tcm = tb_priv(tb);
1500 tb_dbg(tb, "suspending...\n");
1501 tb_disconnect_and_release_dp(tb);
1502 tb_switch_suspend(tb->root_switch, false);
1503 tcm->hotplug_active = false; /* signal tb_handle_hotplug to quit */
1504 tb_dbg(tb, "suspend finished\n");
1509 static void tb_restore_children(struct tb_switch *sw)
1511 struct tb_port *port;
1514 /* No need to restore if the router is already unplugged */
1515 if (sw->is_unplugged)
1519 * CL0s and CL1 are enabled and supported together.
1520 * Silently ignore CLx re-enabling in case CLx is not supported.
1522 ret = tb_switch_enable_clx(sw, TB_CL1);
1523 if (ret && ret != -EOPNOTSUPP)
1524 tb_sw_warn(sw, "failed to re-enable %s on upstream port\n",
1525 tb_switch_clx_name(TB_CL1));
1527 if (tb_switch_is_clx_enabled(sw, TB_CL1))
1529 * To support highest CLx state, we set router's TMU to
1532 tb_switch_tmu_configure(sw, TB_SWITCH_TMU_RATE_NORMAL, true);
1534 /* If CLx disabled, configure router's TMU to HiFi-Bidir mode*/
1535 tb_switch_tmu_configure(sw, TB_SWITCH_TMU_RATE_HIFI, false);
1537 if (tb_enable_tmu(sw))
1538 tb_sw_warn(sw, "failed to restore TMU configuration\n");
1540 tb_switch_for_each_port(sw, port) {
1541 if (!tb_port_has_remote(port) && !port->xdomain)
1545 tb_switch_lane_bonding_enable(port->remote->sw);
1546 tb_switch_configure_link(port->remote->sw);
1548 tb_restore_children(port->remote->sw);
1549 } else if (port->xdomain) {
1550 tb_port_configure_xdomain(port, port->xdomain);
1555 static int tb_resume_noirq(struct tb *tb)
1557 struct tb_cm *tcm = tb_priv(tb);
1558 struct tb_tunnel *tunnel, *n;
1559 unsigned int usb3_delay = 0;
1562 tb_dbg(tb, "resuming...\n");
1564 /* remove any pci devices the firmware might have setup */
1565 tb_switch_reset(tb->root_switch);
1567 tb_switch_resume(tb->root_switch);
1568 tb_free_invalid_tunnels(tb);
1569 tb_free_unplugged_children(tb->root_switch);
1570 tb_restore_children(tb->root_switch);
1573 * If we get here from suspend to disk the boot firmware or the
1574 * restore kernel might have created tunnels of its own. Since
1575 * we cannot be sure they are usable for us we find and tear
1578 tb_switch_discover_tunnels(tb->root_switch, &tunnels, false);
1579 list_for_each_entry_safe_reverse(tunnel, n, &tunnels, list) {
1580 if (tb_tunnel_is_usb3(tunnel))
1582 tb_tunnel_deactivate(tunnel);
1583 tb_tunnel_free(tunnel);
1586 /* Re-create our tunnels now */
1587 list_for_each_entry_safe(tunnel, n, &tcm->tunnel_list, list) {
1588 /* USB3 requires delay before it can be re-activated */
1589 if (tb_tunnel_is_usb3(tunnel)) {
1591 /* Only need to do it once */
1594 tb_tunnel_restart(tunnel);
1596 if (!list_empty(&tcm->tunnel_list)) {
1598 * the pcie links need some time to get going.
1599 * 100ms works for me...
1601 tb_dbg(tb, "tunnels restarted, sleeping for 100ms\n");
1604 /* Allow tb_handle_hotplug to progress events */
1605 tcm->hotplug_active = true;
1606 tb_dbg(tb, "resume finished\n");
1611 static int tb_free_unplugged_xdomains(struct tb_switch *sw)
1613 struct tb_port *port;
1616 tb_switch_for_each_port(sw, port) {
1617 if (tb_is_upstream_port(port))
1619 if (port->xdomain && port->xdomain->is_unplugged) {
1620 tb_retimer_remove_all(port);
1621 tb_xdomain_remove(port->xdomain);
1622 tb_port_unconfigure_xdomain(port);
1623 port->xdomain = NULL;
1625 } else if (port->remote) {
1626 ret += tb_free_unplugged_xdomains(port->remote->sw);
1633 static int tb_freeze_noirq(struct tb *tb)
1635 struct tb_cm *tcm = tb_priv(tb);
1637 tcm->hotplug_active = false;
1641 static int tb_thaw_noirq(struct tb *tb)
1643 struct tb_cm *tcm = tb_priv(tb);
1645 tcm->hotplug_active = true;
1649 static void tb_complete(struct tb *tb)
1652 * Release any unplugged XDomains and if there is a case where
1653 * another domain is swapped in place of unplugged XDomain we
1654 * need to run another rescan.
1656 mutex_lock(&tb->lock);
1657 if (tb_free_unplugged_xdomains(tb->root_switch))
1658 tb_scan_switch(tb->root_switch);
1659 mutex_unlock(&tb->lock);
1662 static int tb_runtime_suspend(struct tb *tb)
1664 struct tb_cm *tcm = tb_priv(tb);
1666 mutex_lock(&tb->lock);
1667 tb_switch_suspend(tb->root_switch, true);
1668 tcm->hotplug_active = false;
1669 mutex_unlock(&tb->lock);
1674 static void tb_remove_work(struct work_struct *work)
1676 struct tb_cm *tcm = container_of(work, struct tb_cm, remove_work.work);
1677 struct tb *tb = tcm_to_tb(tcm);
1679 mutex_lock(&tb->lock);
1680 if (tb->root_switch) {
1681 tb_free_unplugged_children(tb->root_switch);
1682 tb_free_unplugged_xdomains(tb->root_switch);
1684 mutex_unlock(&tb->lock);
1687 static int tb_runtime_resume(struct tb *tb)
1689 struct tb_cm *tcm = tb_priv(tb);
1690 struct tb_tunnel *tunnel, *n;
1692 mutex_lock(&tb->lock);
1693 tb_switch_resume(tb->root_switch);
1694 tb_free_invalid_tunnels(tb);
1695 tb_restore_children(tb->root_switch);
1696 list_for_each_entry_safe(tunnel, n, &tcm->tunnel_list, list)
1697 tb_tunnel_restart(tunnel);
1698 tcm->hotplug_active = true;
1699 mutex_unlock(&tb->lock);
1702 * Schedule cleanup of any unplugged devices. Run this in a
1703 * separate thread to avoid possible deadlock if the device
1704 * removal runtime resumes the unplugged device.
1706 queue_delayed_work(tb->wq, &tcm->remove_work, msecs_to_jiffies(50));
1710 static const struct tb_cm_ops tb_cm_ops = {
1713 .suspend_noirq = tb_suspend_noirq,
1714 .resume_noirq = tb_resume_noirq,
1715 .freeze_noirq = tb_freeze_noirq,
1716 .thaw_noirq = tb_thaw_noirq,
1717 .complete = tb_complete,
1718 .runtime_suspend = tb_runtime_suspend,
1719 .runtime_resume = tb_runtime_resume,
1720 .handle_event = tb_handle_event,
1721 .disapprove_switch = tb_disconnect_pci,
1722 .approve_switch = tb_tunnel_pci,
1723 .approve_xdomain_paths = tb_approve_xdomain_paths,
1724 .disconnect_xdomain_paths = tb_disconnect_xdomain_paths,
1728 * During suspend the Thunderbolt controller is reset and all PCIe
1729 * tunnels are lost. The NHI driver will try to reestablish all tunnels
1730 * during resume. This adds device links between the tunneled PCIe
1731 * downstream ports and the NHI so that the device core will make sure
1732 * NHI is resumed first before the rest.
1734 static void tb_apple_add_links(struct tb_nhi *nhi)
1736 struct pci_dev *upstream, *pdev;
1738 if (!x86_apple_machine)
1741 switch (nhi->pdev->device) {
1742 case PCI_DEVICE_ID_INTEL_LIGHT_RIDGE:
1743 case PCI_DEVICE_ID_INTEL_CACTUS_RIDGE_4C:
1744 case PCI_DEVICE_ID_INTEL_FALCON_RIDGE_2C_NHI:
1745 case PCI_DEVICE_ID_INTEL_FALCON_RIDGE_4C_NHI:
1751 upstream = pci_upstream_bridge(nhi->pdev);
1753 if (!pci_is_pcie(upstream))
1755 if (pci_pcie_type(upstream) == PCI_EXP_TYPE_UPSTREAM)
1757 upstream = pci_upstream_bridge(upstream);
1764 * For each hotplug downstream port, create add device link
1765 * back to NHI so that PCIe tunnels can be re-established after
1768 for_each_pci_bridge(pdev, upstream->subordinate) {
1769 const struct device_link *link;
1771 if (!pci_is_pcie(pdev))
1773 if (pci_pcie_type(pdev) != PCI_EXP_TYPE_DOWNSTREAM ||
1774 !pdev->is_hotplug_bridge)
1777 link = device_link_add(&pdev->dev, &nhi->pdev->dev,
1778 DL_FLAG_AUTOREMOVE_SUPPLIER |
1779 DL_FLAG_PM_RUNTIME);
1781 dev_dbg(&nhi->pdev->dev, "created link from %s\n",
1782 dev_name(&pdev->dev));
1784 dev_warn(&nhi->pdev->dev, "device link creation from %s failed\n",
1785 dev_name(&pdev->dev));
1790 struct tb *tb_probe(struct tb_nhi *nhi)
1795 tb = tb_domain_alloc(nhi, TB_TIMEOUT, sizeof(*tcm));
1799 if (tb_acpi_may_tunnel_pcie())
1800 tb->security_level = TB_SECURITY_USER;
1802 tb->security_level = TB_SECURITY_NOPCIE;
1804 tb->cm_ops = &tb_cm_ops;
1807 INIT_LIST_HEAD(&tcm->tunnel_list);
1808 INIT_LIST_HEAD(&tcm->dp_resources);
1809 INIT_DELAYED_WORK(&tcm->remove_work, tb_remove_work);
1811 tb_dbg(tb, "using software connection manager\n");
1813 tb_apple_add_links(nhi);
1814 tb_acpi_add_links(nhi);