1 // SPDX-License-Identifier: GPL-2.0
3 * Thunderbolt driver - Tunneling support
5 * Copyright (c) 2014 Andreas Noever <andreas.noever@gmail.com>
6 * Copyright (C) 2019, Intel Corporation
9 #include <linux/delay.h>
10 #include <linux/slab.h>
11 #include <linux/list.h>
12 #include <linux/ktime.h>
13 #include <linux/string_helpers.h>
18 /* PCIe adapters use always HopID of 8 for both directions */
19 #define TB_PCI_HOPID 8
21 #define TB_PCI_PATH_DOWN 0
22 #define TB_PCI_PATH_UP 1
24 /* USB3 adapters use always HopID of 8 for both directions */
25 #define TB_USB3_HOPID 8
27 #define TB_USB3_PATH_DOWN 0
28 #define TB_USB3_PATH_UP 1
30 /* DP adapters use HopID 8 for AUX and 9 for Video */
31 #define TB_DP_AUX_TX_HOPID 8
32 #define TB_DP_AUX_RX_HOPID 8
33 #define TB_DP_VIDEO_HOPID 9
35 #define TB_DP_VIDEO_PATH_OUT 0
36 #define TB_DP_AUX_PATH_OUT 1
37 #define TB_DP_AUX_PATH_IN 2
39 /* Minimum number of credits needed for PCIe path */
40 #define TB_MIN_PCIE_CREDITS 6U
42 * Number of credits we try to allocate for each DMA path if not limited
43 * by the host router baMaxHI.
45 #define TB_DMA_CREDITS 14
46 /* Minimum number of credits for DMA path */
47 #define TB_MIN_DMA_CREDITS 1
49 static unsigned int dma_credits = TB_DMA_CREDITS;
50 module_param(dma_credits, uint, 0444);
51 MODULE_PARM_DESC(dma_credits, "specify custom credits for DMA tunnels (default: "
52 __MODULE_STRING(TB_DMA_CREDITS) ")");
54 static bool bw_alloc_mode = true;
55 module_param(bw_alloc_mode, bool, 0444);
56 MODULE_PARM_DESC(bw_alloc_mode,
57 "enable bandwidth allocation mode if supported (default: true)");
59 static const char * const tb_tunnel_names[] = { "PCI", "DP", "DMA", "USB3" };
61 #define __TB_TUNNEL_PRINT(level, tunnel, fmt, arg...) \
63 struct tb_tunnel *__tunnel = (tunnel); \
64 level(__tunnel->tb, "%llx:%u <-> %llx:%u (%s): " fmt, \
65 tb_route(__tunnel->src_port->sw), \
66 __tunnel->src_port->port, \
67 tb_route(__tunnel->dst_port->sw), \
68 __tunnel->dst_port->port, \
69 tb_tunnel_names[__tunnel->type], \
73 #define tb_tunnel_WARN(tunnel, fmt, arg...) \
74 __TB_TUNNEL_PRINT(tb_WARN, tunnel, fmt, ##arg)
75 #define tb_tunnel_warn(tunnel, fmt, arg...) \
76 __TB_TUNNEL_PRINT(tb_warn, tunnel, fmt, ##arg)
77 #define tb_tunnel_info(tunnel, fmt, arg...) \
78 __TB_TUNNEL_PRINT(tb_info, tunnel, fmt, ##arg)
79 #define tb_tunnel_dbg(tunnel, fmt, arg...) \
80 __TB_TUNNEL_PRINT(tb_dbg, tunnel, fmt, ##arg)
82 static inline unsigned int tb_usable_credits(const struct tb_port *port)
84 return port->total_credits - port->ctl_credits;
88 * tb_available_credits() - Available credits for PCIe and DMA
89 * @port: Lane adapter to check
90 * @max_dp_streams: If non-%NULL stores maximum number of simultaneous DP
91 * streams possible through this lane adapter
93 static unsigned int tb_available_credits(const struct tb_port *port,
94 size_t *max_dp_streams)
96 const struct tb_switch *sw = port->sw;
97 int credits, usb3, pcie, spare;
100 usb3 = tb_acpi_may_tunnel_usb3() ? sw->max_usb3_credits : 0;
101 pcie = tb_acpi_may_tunnel_pcie() ? sw->max_pcie_credits : 0;
103 if (tb_acpi_is_xdomain_allowed()) {
104 spare = min_not_zero(sw->max_dma_credits, dma_credits);
105 /* Add some credits for potential second DMA tunnel */
106 spare += TB_MIN_DMA_CREDITS;
111 credits = tb_usable_credits(port);
112 if (tb_acpi_may_tunnel_dp()) {
114 * Maximum number of DP streams possible through the
117 if (sw->min_dp_aux_credits + sw->min_dp_main_credits)
118 ndp = (credits - (usb3 + pcie + spare)) /
119 (sw->min_dp_aux_credits + sw->min_dp_main_credits);
125 credits -= ndp * (sw->min_dp_aux_credits + sw->min_dp_main_credits);
129 *max_dp_streams = ndp;
131 return credits > 0 ? credits : 0;
134 static struct tb_tunnel *tb_tunnel_alloc(struct tb *tb, size_t npaths,
135 enum tb_tunnel_type type)
137 struct tb_tunnel *tunnel;
139 tunnel = kzalloc(sizeof(*tunnel), GFP_KERNEL);
143 tunnel->paths = kcalloc(npaths, sizeof(tunnel->paths[0]), GFP_KERNEL);
144 if (!tunnel->paths) {
145 tb_tunnel_free(tunnel);
149 INIT_LIST_HEAD(&tunnel->list);
151 tunnel->npaths = npaths;
157 static int tb_pci_set_ext_encapsulation(struct tb_tunnel *tunnel, bool enable)
161 /* Only supported of both routers are at least USB4 v2 */
162 if (usb4_switch_version(tunnel->src_port->sw) < 2 ||
163 usb4_switch_version(tunnel->dst_port->sw) < 2)
166 ret = usb4_pci_port_set_ext_encapsulation(tunnel->src_port, enable);
170 ret = usb4_pci_port_set_ext_encapsulation(tunnel->dst_port, enable);
174 tb_tunnel_dbg(tunnel, "extended encapsulation %s\n",
175 str_enabled_disabled(enable));
179 static int tb_pci_activate(struct tb_tunnel *tunnel, bool activate)
184 res = tb_pci_set_ext_encapsulation(tunnel, activate);
189 res = tb_pci_port_enable(tunnel->src_port, activate);
193 if (tb_port_is_pcie_up(tunnel->dst_port)) {
194 res = tb_pci_port_enable(tunnel->dst_port, activate);
199 return activate ? 0 : tb_pci_set_ext_encapsulation(tunnel, activate);
202 static int tb_pci_init_credits(struct tb_path_hop *hop)
204 struct tb_port *port = hop->in_port;
205 struct tb_switch *sw = port->sw;
206 unsigned int credits;
208 if (tb_port_use_credit_allocation(port)) {
209 unsigned int available;
211 available = tb_available_credits(port, NULL);
212 credits = min(sw->max_pcie_credits, available);
214 if (credits < TB_MIN_PCIE_CREDITS)
217 credits = max(TB_MIN_PCIE_CREDITS, credits);
219 if (tb_port_is_null(port))
220 credits = port->bonded ? 32 : 16;
225 hop->initial_credits = credits;
229 static int tb_pci_init_path(struct tb_path *path)
231 struct tb_path_hop *hop;
233 path->egress_fc_enable = TB_PATH_SOURCE | TB_PATH_INTERNAL;
234 path->egress_shared_buffer = TB_PATH_NONE;
235 path->ingress_fc_enable = TB_PATH_ALL;
236 path->ingress_shared_buffer = TB_PATH_NONE;
239 path->drop_packages = 0;
241 tb_path_for_each_hop(path, hop) {
244 ret = tb_pci_init_credits(hop);
253 * tb_tunnel_discover_pci() - Discover existing PCIe tunnels
254 * @tb: Pointer to the domain structure
255 * @down: PCIe downstream adapter
256 * @alloc_hopid: Allocate HopIDs from visited ports
258 * If @down adapter is active, follows the tunnel to the PCIe upstream
259 * adapter and back. Returns the discovered tunnel or %NULL if there was
262 struct tb_tunnel *tb_tunnel_discover_pci(struct tb *tb, struct tb_port *down,
265 struct tb_tunnel *tunnel;
266 struct tb_path *path;
268 if (!tb_pci_port_is_enabled(down))
271 tunnel = tb_tunnel_alloc(tb, 2, TB_TUNNEL_PCI);
275 tunnel->activate = tb_pci_activate;
276 tunnel->src_port = down;
279 * Discover both paths even if they are not complete. We will
280 * clean them up by calling tb_tunnel_deactivate() below in that
283 path = tb_path_discover(down, TB_PCI_HOPID, NULL, -1,
284 &tunnel->dst_port, "PCIe Up", alloc_hopid);
286 /* Just disable the downstream port */
287 tb_pci_port_enable(down, false);
290 tunnel->paths[TB_PCI_PATH_UP] = path;
291 if (tb_pci_init_path(tunnel->paths[TB_PCI_PATH_UP]))
294 path = tb_path_discover(tunnel->dst_port, -1, down, TB_PCI_HOPID, NULL,
295 "PCIe Down", alloc_hopid);
298 tunnel->paths[TB_PCI_PATH_DOWN] = path;
299 if (tb_pci_init_path(tunnel->paths[TB_PCI_PATH_DOWN]))
302 /* Validate that the tunnel is complete */
303 if (!tb_port_is_pcie_up(tunnel->dst_port)) {
304 tb_port_warn(tunnel->dst_port,
305 "path does not end on a PCIe adapter, cleaning up\n");
309 if (down != tunnel->src_port) {
310 tb_tunnel_warn(tunnel, "path is not complete, cleaning up\n");
314 if (!tb_pci_port_is_enabled(tunnel->dst_port)) {
315 tb_tunnel_warn(tunnel,
316 "tunnel is not fully activated, cleaning up\n");
320 tb_tunnel_dbg(tunnel, "discovered\n");
324 tb_tunnel_deactivate(tunnel);
326 tb_tunnel_free(tunnel);
332 * tb_tunnel_alloc_pci() - allocate a pci tunnel
333 * @tb: Pointer to the domain structure
334 * @up: PCIe upstream adapter port
335 * @down: PCIe downstream adapter port
337 * Allocate a PCI tunnel. The ports must be of type TB_TYPE_PCIE_UP and
340 * Return: Returns a tb_tunnel on success or NULL on failure.
342 struct tb_tunnel *tb_tunnel_alloc_pci(struct tb *tb, struct tb_port *up,
343 struct tb_port *down)
345 struct tb_tunnel *tunnel;
346 struct tb_path *path;
348 tunnel = tb_tunnel_alloc(tb, 2, TB_TUNNEL_PCI);
352 tunnel->activate = tb_pci_activate;
353 tunnel->src_port = down;
354 tunnel->dst_port = up;
356 path = tb_path_alloc(tb, down, TB_PCI_HOPID, up, TB_PCI_HOPID, 0,
360 tunnel->paths[TB_PCI_PATH_DOWN] = path;
361 if (tb_pci_init_path(path))
364 path = tb_path_alloc(tb, up, TB_PCI_HOPID, down, TB_PCI_HOPID, 0,
368 tunnel->paths[TB_PCI_PATH_UP] = path;
369 if (tb_pci_init_path(path))
375 tb_tunnel_free(tunnel);
379 static bool tb_dp_is_usb4(const struct tb_switch *sw)
381 /* Titan Ridge DP adapters need the same treatment as USB4 */
382 return tb_switch_is_usb4(sw) || tb_switch_is_titan_ridge(sw);
385 static int tb_dp_cm_handshake(struct tb_port *in, struct tb_port *out,
388 ktime_t timeout = ktime_add_ms(ktime_get(), timeout_msec);
392 /* Both ends need to support this */
393 if (!tb_dp_is_usb4(in->sw) || !tb_dp_is_usb4(out->sw))
396 ret = tb_port_read(out, &val, TB_CFG_PORT,
397 out->cap_adap + DP_STATUS_CTRL, 1);
401 val |= DP_STATUS_CTRL_UF | DP_STATUS_CTRL_CMHS;
403 ret = tb_port_write(out, &val, TB_CFG_PORT,
404 out->cap_adap + DP_STATUS_CTRL, 1);
409 ret = tb_port_read(out, &val, TB_CFG_PORT,
410 out->cap_adap + DP_STATUS_CTRL, 1);
413 if (!(val & DP_STATUS_CTRL_CMHS))
415 usleep_range(100, 150);
416 } while (ktime_before(ktime_get(), timeout));
422 * Returns maximum possible rate from capability supporting only DP 2.0
423 * and below. Used when DP BW allocation mode is not enabled.
425 static inline u32 tb_dp_cap_get_rate(u32 val)
427 u32 rate = (val & DP_COMMON_CAP_RATE_MASK) >> DP_COMMON_CAP_RATE_SHIFT;
430 case DP_COMMON_CAP_RATE_RBR:
432 case DP_COMMON_CAP_RATE_HBR:
434 case DP_COMMON_CAP_RATE_HBR2:
436 case DP_COMMON_CAP_RATE_HBR3:
444 * Returns maximum possible rate from capability supporting DP 2.1
445 * UHBR20, 13.5 and 10 rates as well. Use only when DP BW allocation
448 static inline u32 tb_dp_cap_get_rate_ext(u32 val)
450 if (val & DP_COMMON_CAP_UHBR20)
452 else if (val & DP_COMMON_CAP_UHBR13_5)
454 else if (val & DP_COMMON_CAP_UHBR10)
457 return tb_dp_cap_get_rate(val);
460 static inline bool tb_dp_is_uhbr_rate(unsigned int rate)
462 return rate >= 10000;
465 static inline u32 tb_dp_cap_set_rate(u32 val, u32 rate)
467 val &= ~DP_COMMON_CAP_RATE_MASK;
470 WARN(1, "invalid rate %u passed, defaulting to 1620 MB/s\n", rate);
473 val |= DP_COMMON_CAP_RATE_RBR << DP_COMMON_CAP_RATE_SHIFT;
476 val |= DP_COMMON_CAP_RATE_HBR << DP_COMMON_CAP_RATE_SHIFT;
479 val |= DP_COMMON_CAP_RATE_HBR2 << DP_COMMON_CAP_RATE_SHIFT;
482 val |= DP_COMMON_CAP_RATE_HBR3 << DP_COMMON_CAP_RATE_SHIFT;
488 static inline u32 tb_dp_cap_get_lanes(u32 val)
490 u32 lanes = (val & DP_COMMON_CAP_LANES_MASK) >> DP_COMMON_CAP_LANES_SHIFT;
493 case DP_COMMON_CAP_1_LANE:
495 case DP_COMMON_CAP_2_LANES:
497 case DP_COMMON_CAP_4_LANES:
504 static inline u32 tb_dp_cap_set_lanes(u32 val, u32 lanes)
506 val &= ~DP_COMMON_CAP_LANES_MASK;
509 WARN(1, "invalid number of lanes %u passed, defaulting to 1\n",
513 val |= DP_COMMON_CAP_1_LANE << DP_COMMON_CAP_LANES_SHIFT;
516 val |= DP_COMMON_CAP_2_LANES << DP_COMMON_CAP_LANES_SHIFT;
519 val |= DP_COMMON_CAP_4_LANES << DP_COMMON_CAP_LANES_SHIFT;
525 static unsigned int tb_dp_bandwidth(unsigned int rate, unsigned int lanes)
527 /* Tunneling removes the DP 8b/10b 128/132b encoding */
528 if (tb_dp_is_uhbr_rate(rate))
529 return rate * lanes * 128 / 132;
530 return rate * lanes * 8 / 10;
533 static int tb_dp_reduce_bandwidth(int max_bw, u32 in_rate, u32 in_lanes,
534 u32 out_rate, u32 out_lanes, u32 *new_rate,
537 static const u32 dp_bw[][2] = {
539 { 8100, 4 }, /* 25920 Mb/s */
540 { 5400, 4 }, /* 17280 Mb/s */
541 { 8100, 2 }, /* 12960 Mb/s */
542 { 2700, 4 }, /* 8640 Mb/s */
543 { 5400, 2 }, /* 8640 Mb/s */
544 { 8100, 1 }, /* 6480 Mb/s */
545 { 1620, 4 }, /* 5184 Mb/s */
546 { 5400, 1 }, /* 4320 Mb/s */
547 { 2700, 2 }, /* 4320 Mb/s */
548 { 1620, 2 }, /* 2592 Mb/s */
549 { 2700, 1 }, /* 2160 Mb/s */
550 { 1620, 1 }, /* 1296 Mb/s */
555 * Find a combination that can fit into max_bw and does not
556 * exceed the maximum rate and lanes supported by the DP OUT and
559 for (i = 0; i < ARRAY_SIZE(dp_bw); i++) {
560 if (dp_bw[i][0] > out_rate || dp_bw[i][1] > out_lanes)
563 if (dp_bw[i][0] > in_rate || dp_bw[i][1] > in_lanes)
566 if (tb_dp_bandwidth(dp_bw[i][0], dp_bw[i][1]) <= max_bw) {
567 *new_rate = dp_bw[i][0];
568 *new_lanes = dp_bw[i][1];
576 static int tb_dp_xchg_caps(struct tb_tunnel *tunnel)
578 u32 out_dp_cap, out_rate, out_lanes, in_dp_cap, in_rate, in_lanes, bw;
579 struct tb_port *out = tunnel->dst_port;
580 struct tb_port *in = tunnel->src_port;
584 * Copy DP_LOCAL_CAP register to DP_REMOTE_CAP register for
585 * newer generation hardware.
587 if (in->sw->generation < 2 || out->sw->generation < 2)
591 * Perform connection manager handshake between IN and OUT ports
592 * before capabilities exchange can take place.
594 ret = tb_dp_cm_handshake(in, out, 3000);
598 /* Read both DP_LOCAL_CAP registers */
599 ret = tb_port_read(in, &in_dp_cap, TB_CFG_PORT,
600 in->cap_adap + DP_LOCAL_CAP, 1);
604 ret = tb_port_read(out, &out_dp_cap, TB_CFG_PORT,
605 out->cap_adap + DP_LOCAL_CAP, 1);
609 /* Write IN local caps to OUT remote caps */
610 ret = tb_port_write(out, &in_dp_cap, TB_CFG_PORT,
611 out->cap_adap + DP_REMOTE_CAP, 1);
615 in_rate = tb_dp_cap_get_rate(in_dp_cap);
616 in_lanes = tb_dp_cap_get_lanes(in_dp_cap);
617 tb_port_dbg(in, "maximum supported bandwidth %u Mb/s x%u = %u Mb/s\n",
618 in_rate, in_lanes, tb_dp_bandwidth(in_rate, in_lanes));
621 * If the tunnel bandwidth is limited (max_bw is set) then see
622 * if we need to reduce bandwidth to fit there.
624 out_rate = tb_dp_cap_get_rate(out_dp_cap);
625 out_lanes = tb_dp_cap_get_lanes(out_dp_cap);
626 bw = tb_dp_bandwidth(out_rate, out_lanes);
627 tb_port_dbg(out, "maximum supported bandwidth %u Mb/s x%u = %u Mb/s\n",
628 out_rate, out_lanes, bw);
630 if (in->sw->config.depth < out->sw->config.depth)
631 max_bw = tunnel->max_down;
633 max_bw = tunnel->max_up;
635 if (max_bw && bw > max_bw) {
636 u32 new_rate, new_lanes, new_bw;
638 ret = tb_dp_reduce_bandwidth(max_bw, in_rate, in_lanes,
639 out_rate, out_lanes, &new_rate,
642 tb_port_info(out, "not enough bandwidth for DP tunnel\n");
646 new_bw = tb_dp_bandwidth(new_rate, new_lanes);
647 tb_port_dbg(out, "bandwidth reduced to %u Mb/s x%u = %u Mb/s\n",
648 new_rate, new_lanes, new_bw);
651 * Set new rate and number of lanes before writing it to
652 * the IN port remote caps.
654 out_dp_cap = tb_dp_cap_set_rate(out_dp_cap, new_rate);
655 out_dp_cap = tb_dp_cap_set_lanes(out_dp_cap, new_lanes);
659 * Titan Ridge does not disable AUX timers when it gets
660 * SET_CONFIG with SET_LTTPR_MODE set. This causes problems with
663 if (tb_route(out->sw) && tb_switch_is_titan_ridge(out->sw)) {
664 out_dp_cap |= DP_COMMON_CAP_LTTPR_NS;
665 tb_port_dbg(out, "disabling LTTPR\n");
668 return tb_port_write(in, &out_dp_cap, TB_CFG_PORT,
669 in->cap_adap + DP_REMOTE_CAP, 1);
672 static int tb_dp_bandwidth_alloc_mode_enable(struct tb_tunnel *tunnel)
674 int ret, estimated_bw, granularity, tmp;
675 struct tb_port *out = tunnel->dst_port;
676 struct tb_port *in = tunnel->src_port;
677 u32 out_dp_cap, out_rate, out_lanes;
678 u32 in_dp_cap, in_rate, in_lanes;
684 ret = usb4_dp_port_set_cm_bandwidth_mode_supported(in, true);
688 ret = usb4_dp_port_set_group_id(in, in->group->index);
693 * Get the non-reduced rate and lanes based on the lowest
694 * capability of both adapters.
696 ret = tb_port_read(in, &in_dp_cap, TB_CFG_PORT,
697 in->cap_adap + DP_LOCAL_CAP, 1);
701 ret = tb_port_read(out, &out_dp_cap, TB_CFG_PORT,
702 out->cap_adap + DP_LOCAL_CAP, 1);
706 in_rate = tb_dp_cap_get_rate(in_dp_cap);
707 in_lanes = tb_dp_cap_get_lanes(in_dp_cap);
708 out_rate = tb_dp_cap_get_rate(out_dp_cap);
709 out_lanes = tb_dp_cap_get_lanes(out_dp_cap);
711 rate = min(in_rate, out_rate);
712 lanes = min(in_lanes, out_lanes);
713 tmp = tb_dp_bandwidth(rate, lanes);
715 tb_port_dbg(in, "non-reduced bandwidth %u Mb/s x%u = %u Mb/s\n", rate,
718 ret = usb4_dp_port_set_nrd(in, rate, lanes);
723 * Pick up granularity that supports maximum possible bandwidth.
724 * For that we use the UHBR rates too.
726 in_rate = tb_dp_cap_get_rate_ext(in_dp_cap);
727 out_rate = tb_dp_cap_get_rate_ext(out_dp_cap);
728 rate = min(in_rate, out_rate);
729 tmp = tb_dp_bandwidth(rate, lanes);
732 "maximum bandwidth through allocation mode %u Mb/s x%u = %u Mb/s\n",
735 for (granularity = 250; tmp / granularity > 255 && granularity <= 1000;
739 tb_port_dbg(in, "granularity %d Mb/s\n", granularity);
742 * Returns -EINVAL if granularity above is outside of the
745 ret = usb4_dp_port_set_granularity(in, granularity);
750 * Bandwidth estimation is pretty much what we have in
751 * max_up/down fields. For discovery we just read what the
752 * estimation was set to.
754 if (in->sw->config.depth < out->sw->config.depth)
755 estimated_bw = tunnel->max_down;
757 estimated_bw = tunnel->max_up;
759 tb_port_dbg(in, "estimated bandwidth %d Mb/s\n", estimated_bw);
761 ret = usb4_dp_port_set_estimated_bandwidth(in, estimated_bw);
765 /* Initial allocation should be 0 according the spec */
766 ret = usb4_dp_port_allocate_bandwidth(in, 0);
770 tb_port_dbg(in, "bandwidth allocation mode enabled\n");
774 static int tb_dp_init(struct tb_tunnel *tunnel)
776 struct tb_port *in = tunnel->src_port;
777 struct tb_switch *sw = in->sw;
778 struct tb *tb = in->sw->tb;
781 ret = tb_dp_xchg_caps(tunnel);
785 if (!tb_switch_is_usb4(sw))
788 if (!usb4_dp_port_bandwidth_mode_supported(in))
791 tb_port_dbg(in, "bandwidth allocation mode supported\n");
793 ret = usb4_dp_port_set_cm_id(in, tb->index);
797 return tb_dp_bandwidth_alloc_mode_enable(tunnel);
800 static void tb_dp_deinit(struct tb_tunnel *tunnel)
802 struct tb_port *in = tunnel->src_port;
804 if (!usb4_dp_port_bandwidth_mode_supported(in))
806 if (usb4_dp_port_bandwidth_mode_enabled(in)) {
807 usb4_dp_port_set_cm_bandwidth_mode_supported(in, false);
808 tb_port_dbg(in, "bandwidth allocation mode disabled\n");
812 static int tb_dp_activate(struct tb_tunnel *tunnel, bool active)
817 struct tb_path **paths;
820 paths = tunnel->paths;
821 last = paths[TB_DP_VIDEO_PATH_OUT]->path_length - 1;
823 tb_dp_port_set_hops(tunnel->src_port,
824 paths[TB_DP_VIDEO_PATH_OUT]->hops[0].in_hop_index,
825 paths[TB_DP_AUX_PATH_OUT]->hops[0].in_hop_index,
826 paths[TB_DP_AUX_PATH_IN]->hops[last].next_hop_index);
828 tb_dp_port_set_hops(tunnel->dst_port,
829 paths[TB_DP_VIDEO_PATH_OUT]->hops[last].next_hop_index,
830 paths[TB_DP_AUX_PATH_IN]->hops[0].in_hop_index,
831 paths[TB_DP_AUX_PATH_OUT]->hops[last].next_hop_index);
833 tb_dp_port_hpd_clear(tunnel->src_port);
834 tb_dp_port_set_hops(tunnel->src_port, 0, 0, 0);
835 if (tb_port_is_dpout(tunnel->dst_port))
836 tb_dp_port_set_hops(tunnel->dst_port, 0, 0, 0);
839 ret = tb_dp_port_enable(tunnel->src_port, active);
843 if (tb_port_is_dpout(tunnel->dst_port))
844 return tb_dp_port_enable(tunnel->dst_port, active);
849 /* max_bw is rounded up to next granularity */
850 static int tb_dp_bandwidth_mode_maximum_bandwidth(struct tb_tunnel *tunnel,
853 struct tb_port *in = tunnel->src_port;
854 int ret, rate, lanes, nrd_bw;
858 * DP IN adapter DP_LOCAL_CAP gets updated to the lowest AUX
859 * read parameter values so this so we can use this to determine
860 * the maximum possible bandwidth over this link.
862 * See USB4 v2 spec 1.0 10.4.4.5.
864 ret = tb_port_read(in, &cap, TB_CFG_PORT,
865 in->cap_adap + DP_LOCAL_CAP, 1);
869 rate = tb_dp_cap_get_rate_ext(cap);
870 if (tb_dp_is_uhbr_rate(rate)) {
872 * When UHBR is used there is no reduction in lanes so
873 * we can use this directly.
875 lanes = tb_dp_cap_get_lanes(cap);
878 * If there is no UHBR supported then check the
879 * non-reduced rate and lanes.
881 ret = usb4_dp_port_nrd(in, &rate, &lanes);
886 nrd_bw = tb_dp_bandwidth(rate, lanes);
889 ret = usb4_dp_port_granularity(in);
892 *max_bw = roundup(nrd_bw, ret);
898 static int tb_dp_bandwidth_mode_consumed_bandwidth(struct tb_tunnel *tunnel,
902 struct tb_port *out = tunnel->dst_port;
903 struct tb_port *in = tunnel->src_port;
904 int ret, allocated_bw, max_bw;
906 if (!usb4_dp_port_bandwidth_mode_enabled(in))
909 if (!tunnel->bw_mode)
912 /* Read what was allocated previously if any */
913 ret = usb4_dp_port_allocated_bandwidth(in);
918 ret = tb_dp_bandwidth_mode_maximum_bandwidth(tunnel, &max_bw);
921 if (allocated_bw == max_bw)
924 tb_port_dbg(in, "consumed bandwidth through allocation mode %d Mb/s\n",
927 if (in->sw->config.depth < out->sw->config.depth) {
929 *consumed_down = allocated_bw;
931 *consumed_up = allocated_bw;
938 static int tb_dp_allocated_bandwidth(struct tb_tunnel *tunnel, int *allocated_up,
941 struct tb_port *out = tunnel->dst_port;
942 struct tb_port *in = tunnel->src_port;
945 * If we have already set the allocated bandwidth then use that.
946 * Otherwise we read it from the DPRX.
948 if (usb4_dp_port_bandwidth_mode_enabled(in) && tunnel->bw_mode) {
949 int ret, allocated_bw, max_bw;
951 ret = usb4_dp_port_allocated_bandwidth(in);
956 ret = tb_dp_bandwidth_mode_maximum_bandwidth(tunnel, &max_bw);
959 if (allocated_bw == max_bw)
962 if (in->sw->config.depth < out->sw->config.depth) {
964 *allocated_down = allocated_bw;
966 *allocated_up = allocated_bw;
972 return tunnel->consumed_bandwidth(tunnel, allocated_up,
976 static int tb_dp_alloc_bandwidth(struct tb_tunnel *tunnel, int *alloc_up,
979 struct tb_port *out = tunnel->dst_port;
980 struct tb_port *in = tunnel->src_port;
981 int max_bw, ret, tmp;
983 if (!usb4_dp_port_bandwidth_mode_enabled(in))
986 ret = tb_dp_bandwidth_mode_maximum_bandwidth(tunnel, &max_bw);
990 if (in->sw->config.depth < out->sw->config.depth) {
991 tmp = min(*alloc_down, max_bw);
992 ret = usb4_dp_port_allocate_bandwidth(in, tmp);
998 tmp = min(*alloc_up, max_bw);
999 ret = usb4_dp_port_allocate_bandwidth(in, tmp);
1006 /* Now we can use BW mode registers to figure out the bandwidth */
1007 /* TODO: need to handle discovery too */
1008 tunnel->bw_mode = true;
1010 tb_port_dbg(in, "allocated bandwidth through allocation mode %d Mb/s\n",
1015 static int tb_dp_read_dprx(struct tb_tunnel *tunnel, u32 *rate, u32 *lanes,
1018 ktime_t timeout = ktime_add_ms(ktime_get(), timeout_msec);
1019 struct tb_port *in = tunnel->src_port;
1022 * Wait for DPRX done. Normally it should be already set for
1029 ret = tb_port_read(in, &val, TB_CFG_PORT,
1030 in->cap_adap + DP_COMMON_CAP, 1);
1034 if (val & DP_COMMON_CAP_DPRX_DONE) {
1035 *rate = tb_dp_cap_get_rate(val);
1036 *lanes = tb_dp_cap_get_lanes(val);
1038 tb_port_dbg(in, "consumed bandwidth through DPRX %d Mb/s\n",
1039 tb_dp_bandwidth(*rate, *lanes));
1042 usleep_range(100, 150);
1043 } while (ktime_before(ktime_get(), timeout));
1048 /* Read cap from tunnel DP IN */
1049 static int tb_dp_read_cap(struct tb_tunnel *tunnel, unsigned int cap, u32 *rate,
1052 struct tb_port *in = tunnel->src_port;
1062 tb_tunnel_WARN(tunnel, "invalid capability index %#x\n", cap);
1067 * Read from the copied remote cap so that we take into account
1068 * if capabilities were reduced during exchange.
1070 ret = tb_port_read(in, &val, TB_CFG_PORT, in->cap_adap + cap, 1);
1074 *rate = tb_dp_cap_get_rate(val);
1075 *lanes = tb_dp_cap_get_lanes(val);
1077 tb_port_dbg(in, "bandwidth from %#x capability %d Mb/s\n", cap,
1078 tb_dp_bandwidth(*rate, *lanes));
1082 static int tb_dp_maximum_bandwidth(struct tb_tunnel *tunnel, int *max_up,
1085 struct tb_port *in = tunnel->src_port;
1088 if (!usb4_dp_port_bandwidth_mode_enabled(in))
1091 ret = tb_dp_bandwidth_mode_maximum_bandwidth(tunnel, NULL);
1095 if (in->sw->config.depth < tunnel->dst_port->sw->config.depth) {
1106 static int tb_dp_consumed_bandwidth(struct tb_tunnel *tunnel, int *consumed_up,
1109 struct tb_port *in = tunnel->src_port;
1110 const struct tb_switch *sw = in->sw;
1111 u32 rate = 0, lanes = 0;
1114 if (tb_dp_is_usb4(sw)) {
1116 * On USB4 routers check if the bandwidth allocation
1117 * mode is enabled first and then read the bandwidth
1118 * through those registers.
1120 ret = tb_dp_bandwidth_mode_consumed_bandwidth(tunnel, consumed_up,
1123 if (ret != -EOPNOTSUPP)
1129 * Then see if the DPRX negotiation is ready and if yes
1130 * return that bandwidth (it may be smaller than the
1131 * reduced one). Otherwise return the remote (possibly
1134 ret = tb_dp_read_dprx(tunnel, &rate, &lanes, 150);
1136 if (ret == -ETIMEDOUT)
1137 ret = tb_dp_read_cap(tunnel, DP_REMOTE_CAP,
1142 } else if (sw->generation >= 2) {
1143 ret = tb_dp_read_cap(tunnel, DP_REMOTE_CAP, &rate, &lanes);
1147 /* No bandwidth management for legacy devices */
1153 if (in->sw->config.depth < tunnel->dst_port->sw->config.depth) {
1155 *consumed_down = tb_dp_bandwidth(rate, lanes);
1157 *consumed_up = tb_dp_bandwidth(rate, lanes);
1164 static void tb_dp_init_aux_credits(struct tb_path_hop *hop)
1166 struct tb_port *port = hop->in_port;
1167 struct tb_switch *sw = port->sw;
1169 if (tb_port_use_credit_allocation(port))
1170 hop->initial_credits = sw->min_dp_aux_credits;
1172 hop->initial_credits = 1;
1175 static void tb_dp_init_aux_path(struct tb_path *path)
1177 struct tb_path_hop *hop;
1179 path->egress_fc_enable = TB_PATH_SOURCE | TB_PATH_INTERNAL;
1180 path->egress_shared_buffer = TB_PATH_NONE;
1181 path->ingress_fc_enable = TB_PATH_ALL;
1182 path->ingress_shared_buffer = TB_PATH_NONE;
1186 tb_path_for_each_hop(path, hop)
1187 tb_dp_init_aux_credits(hop);
1190 static int tb_dp_init_video_credits(struct tb_path_hop *hop)
1192 struct tb_port *port = hop->in_port;
1193 struct tb_switch *sw = port->sw;
1195 if (tb_port_use_credit_allocation(port)) {
1196 unsigned int nfc_credits;
1197 size_t max_dp_streams;
1199 tb_available_credits(port, &max_dp_streams);
1201 * Read the number of currently allocated NFC credits
1202 * from the lane adapter. Since we only use them for DP
1203 * tunneling we can use that to figure out how many DP
1204 * tunnels already go through the lane adapter.
1206 nfc_credits = port->config.nfc_credits &
1207 ADP_CS_4_NFC_BUFFERS_MASK;
1208 if (nfc_credits / sw->min_dp_main_credits > max_dp_streams)
1211 hop->nfc_credits = sw->min_dp_main_credits;
1213 hop->nfc_credits = min(port->total_credits - 2, 12U);
1219 static int tb_dp_init_video_path(struct tb_path *path)
1221 struct tb_path_hop *hop;
1223 path->egress_fc_enable = TB_PATH_NONE;
1224 path->egress_shared_buffer = TB_PATH_NONE;
1225 path->ingress_fc_enable = TB_PATH_NONE;
1226 path->ingress_shared_buffer = TB_PATH_NONE;
1230 tb_path_for_each_hop(path, hop) {
1233 ret = tb_dp_init_video_credits(hop);
1241 static void tb_dp_dump(struct tb_tunnel *tunnel)
1243 struct tb_port *in, *out;
1244 u32 dp_cap, rate, lanes;
1246 in = tunnel->src_port;
1247 out = tunnel->dst_port;
1249 if (tb_port_read(in, &dp_cap, TB_CFG_PORT,
1250 in->cap_adap + DP_LOCAL_CAP, 1))
1253 rate = tb_dp_cap_get_rate(dp_cap);
1254 lanes = tb_dp_cap_get_lanes(dp_cap);
1256 tb_port_dbg(in, "maximum supported bandwidth %u Mb/s x%u = %u Mb/s\n",
1257 rate, lanes, tb_dp_bandwidth(rate, lanes));
1259 out = tunnel->dst_port;
1261 if (tb_port_read(out, &dp_cap, TB_CFG_PORT,
1262 out->cap_adap + DP_LOCAL_CAP, 1))
1265 rate = tb_dp_cap_get_rate(dp_cap);
1266 lanes = tb_dp_cap_get_lanes(dp_cap);
1268 tb_port_dbg(out, "maximum supported bandwidth %u Mb/s x%u = %u Mb/s\n",
1269 rate, lanes, tb_dp_bandwidth(rate, lanes));
1271 if (tb_port_read(in, &dp_cap, TB_CFG_PORT,
1272 in->cap_adap + DP_REMOTE_CAP, 1))
1275 rate = tb_dp_cap_get_rate(dp_cap);
1276 lanes = tb_dp_cap_get_lanes(dp_cap);
1278 tb_port_dbg(in, "reduced bandwidth %u Mb/s x%u = %u Mb/s\n",
1279 rate, lanes, tb_dp_bandwidth(rate, lanes));
1283 * tb_tunnel_discover_dp() - Discover existing Display Port tunnels
1284 * @tb: Pointer to the domain structure
1285 * @in: DP in adapter
1286 * @alloc_hopid: Allocate HopIDs from visited ports
1288 * If @in adapter is active, follows the tunnel to the DP out adapter
1289 * and back. Returns the discovered tunnel or %NULL if there was no
1292 * Return: DP tunnel or %NULL if no tunnel found.
1294 struct tb_tunnel *tb_tunnel_discover_dp(struct tb *tb, struct tb_port *in,
1297 struct tb_tunnel *tunnel;
1298 struct tb_port *port;
1299 struct tb_path *path;
1301 if (!tb_dp_port_is_enabled(in))
1304 tunnel = tb_tunnel_alloc(tb, 3, TB_TUNNEL_DP);
1308 tunnel->init = tb_dp_init;
1309 tunnel->deinit = tb_dp_deinit;
1310 tunnel->activate = tb_dp_activate;
1311 tunnel->maximum_bandwidth = tb_dp_maximum_bandwidth;
1312 tunnel->allocated_bandwidth = tb_dp_allocated_bandwidth;
1313 tunnel->alloc_bandwidth = tb_dp_alloc_bandwidth;
1314 tunnel->consumed_bandwidth = tb_dp_consumed_bandwidth;
1315 tunnel->src_port = in;
1317 path = tb_path_discover(in, TB_DP_VIDEO_HOPID, NULL, -1,
1318 &tunnel->dst_port, "Video", alloc_hopid);
1320 /* Just disable the DP IN port */
1321 tb_dp_port_enable(in, false);
1324 tunnel->paths[TB_DP_VIDEO_PATH_OUT] = path;
1325 if (tb_dp_init_video_path(tunnel->paths[TB_DP_VIDEO_PATH_OUT]))
1328 path = tb_path_discover(in, TB_DP_AUX_TX_HOPID, NULL, -1, NULL, "AUX TX",
1331 goto err_deactivate;
1332 tunnel->paths[TB_DP_AUX_PATH_OUT] = path;
1333 tb_dp_init_aux_path(tunnel->paths[TB_DP_AUX_PATH_OUT]);
1335 path = tb_path_discover(tunnel->dst_port, -1, in, TB_DP_AUX_RX_HOPID,
1336 &port, "AUX RX", alloc_hopid);
1338 goto err_deactivate;
1339 tunnel->paths[TB_DP_AUX_PATH_IN] = path;
1340 tb_dp_init_aux_path(tunnel->paths[TB_DP_AUX_PATH_IN]);
1342 /* Validate that the tunnel is complete */
1343 if (!tb_port_is_dpout(tunnel->dst_port)) {
1344 tb_port_warn(in, "path does not end on a DP adapter, cleaning up\n");
1345 goto err_deactivate;
1348 if (!tb_dp_port_is_enabled(tunnel->dst_port))
1349 goto err_deactivate;
1351 if (!tb_dp_port_hpd_is_active(tunnel->dst_port))
1352 goto err_deactivate;
1354 if (port != tunnel->src_port) {
1355 tb_tunnel_warn(tunnel, "path is not complete, cleaning up\n");
1356 goto err_deactivate;
1361 tb_tunnel_dbg(tunnel, "discovered\n");
1365 tb_tunnel_deactivate(tunnel);
1367 tb_tunnel_free(tunnel);
1373 * tb_tunnel_alloc_dp() - allocate a Display Port tunnel
1374 * @tb: Pointer to the domain structure
1375 * @in: DP in adapter port
1376 * @out: DP out adapter port
1377 * @link_nr: Preferred lane adapter when the link is not bonded
1378 * @max_up: Maximum available upstream bandwidth for the DP tunnel (%0
1380 * @max_down: Maximum available downstream bandwidth for the DP tunnel
1381 * (%0 if not limited)
1383 * Allocates a tunnel between @in and @out that is capable of tunneling
1384 * Display Port traffic.
1386 * Return: Returns a tb_tunnel on success or NULL on failure.
1388 struct tb_tunnel *tb_tunnel_alloc_dp(struct tb *tb, struct tb_port *in,
1389 struct tb_port *out, int link_nr,
1390 int max_up, int max_down)
1392 struct tb_tunnel *tunnel;
1393 struct tb_path **paths;
1394 struct tb_path *path;
1396 if (WARN_ON(!in->cap_adap || !out->cap_adap))
1399 tunnel = tb_tunnel_alloc(tb, 3, TB_TUNNEL_DP);
1403 tunnel->init = tb_dp_init;
1404 tunnel->deinit = tb_dp_deinit;
1405 tunnel->activate = tb_dp_activate;
1406 tunnel->maximum_bandwidth = tb_dp_maximum_bandwidth;
1407 tunnel->allocated_bandwidth = tb_dp_allocated_bandwidth;
1408 tunnel->alloc_bandwidth = tb_dp_alloc_bandwidth;
1409 tunnel->consumed_bandwidth = tb_dp_consumed_bandwidth;
1410 tunnel->src_port = in;
1411 tunnel->dst_port = out;
1412 tunnel->max_up = max_up;
1413 tunnel->max_down = max_down;
1415 paths = tunnel->paths;
1417 path = tb_path_alloc(tb, in, TB_DP_VIDEO_HOPID, out, TB_DP_VIDEO_HOPID,
1421 tb_dp_init_video_path(path);
1422 paths[TB_DP_VIDEO_PATH_OUT] = path;
1424 path = tb_path_alloc(tb, in, TB_DP_AUX_TX_HOPID, out,
1425 TB_DP_AUX_TX_HOPID, link_nr, "AUX TX");
1428 tb_dp_init_aux_path(path);
1429 paths[TB_DP_AUX_PATH_OUT] = path;
1431 path = tb_path_alloc(tb, out, TB_DP_AUX_RX_HOPID, in,
1432 TB_DP_AUX_RX_HOPID, link_nr, "AUX RX");
1435 tb_dp_init_aux_path(path);
1436 paths[TB_DP_AUX_PATH_IN] = path;
1441 tb_tunnel_free(tunnel);
1445 static unsigned int tb_dma_available_credits(const struct tb_port *port)
1447 const struct tb_switch *sw = port->sw;
1450 credits = tb_available_credits(port, NULL);
1451 if (tb_acpi_may_tunnel_pcie())
1452 credits -= sw->max_pcie_credits;
1453 credits -= port->dma_credits;
1455 return credits > 0 ? credits : 0;
1458 static int tb_dma_reserve_credits(struct tb_path_hop *hop, unsigned int credits)
1460 struct tb_port *port = hop->in_port;
1462 if (tb_port_use_credit_allocation(port)) {
1463 unsigned int available = tb_dma_available_credits(port);
1466 * Need to have at least TB_MIN_DMA_CREDITS, otherwise
1467 * DMA path cannot be established.
1469 if (available < TB_MIN_DMA_CREDITS)
1472 while (credits > available)
1475 tb_port_dbg(port, "reserving %u credits for DMA path\n",
1478 port->dma_credits += credits;
1480 if (tb_port_is_null(port))
1481 credits = port->bonded ? 14 : 6;
1483 credits = min(port->total_credits, credits);
1486 hop->initial_credits = credits;
1490 /* Path from lane adapter to NHI */
1491 static int tb_dma_init_rx_path(struct tb_path *path, unsigned int credits)
1493 struct tb_path_hop *hop;
1494 unsigned int i, tmp;
1496 path->egress_fc_enable = TB_PATH_SOURCE | TB_PATH_INTERNAL;
1497 path->ingress_fc_enable = TB_PATH_ALL;
1498 path->egress_shared_buffer = TB_PATH_NONE;
1499 path->ingress_shared_buffer = TB_PATH_NONE;
1502 path->clear_fc = true;
1505 * First lane adapter is the one connected to the remote host.
1506 * We don't tunnel other traffic over this link so can use all
1507 * the credits (except the ones reserved for control traffic).
1509 hop = &path->hops[0];
1510 tmp = min(tb_usable_credits(hop->in_port), credits);
1511 hop->initial_credits = tmp;
1512 hop->in_port->dma_credits += tmp;
1514 for (i = 1; i < path->path_length; i++) {
1517 ret = tb_dma_reserve_credits(&path->hops[i], credits);
1525 /* Path from NHI to lane adapter */
1526 static int tb_dma_init_tx_path(struct tb_path *path, unsigned int credits)
1528 struct tb_path_hop *hop;
1530 path->egress_fc_enable = TB_PATH_ALL;
1531 path->ingress_fc_enable = TB_PATH_ALL;
1532 path->egress_shared_buffer = TB_PATH_NONE;
1533 path->ingress_shared_buffer = TB_PATH_NONE;
1536 path->clear_fc = true;
1538 tb_path_for_each_hop(path, hop) {
1541 ret = tb_dma_reserve_credits(hop, credits);
1549 static void tb_dma_release_credits(struct tb_path_hop *hop)
1551 struct tb_port *port = hop->in_port;
1553 if (tb_port_use_credit_allocation(port)) {
1554 port->dma_credits -= hop->initial_credits;
1556 tb_port_dbg(port, "released %u DMA path credits\n",
1557 hop->initial_credits);
1561 static void tb_dma_deinit_path(struct tb_path *path)
1563 struct tb_path_hop *hop;
1565 tb_path_for_each_hop(path, hop)
1566 tb_dma_release_credits(hop);
1569 static void tb_dma_deinit(struct tb_tunnel *tunnel)
1573 for (i = 0; i < tunnel->npaths; i++) {
1574 if (!tunnel->paths[i])
1576 tb_dma_deinit_path(tunnel->paths[i]);
1581 * tb_tunnel_alloc_dma() - allocate a DMA tunnel
1582 * @tb: Pointer to the domain structure
1583 * @nhi: Host controller port
1584 * @dst: Destination null port which the other domain is connected to
1585 * @transmit_path: HopID used for transmitting packets
1586 * @transmit_ring: NHI ring number used to send packets towards the
1587 * other domain. Set to %-1 if TX path is not needed.
1588 * @receive_path: HopID used for receiving packets
1589 * @receive_ring: NHI ring number used to receive packets from the
1590 * other domain. Set to %-1 if RX path is not needed.
1592 * Return: Returns a tb_tunnel on success or NULL on failure.
1594 struct tb_tunnel *tb_tunnel_alloc_dma(struct tb *tb, struct tb_port *nhi,
1595 struct tb_port *dst, int transmit_path,
1596 int transmit_ring, int receive_path,
1599 struct tb_tunnel *tunnel;
1600 size_t npaths = 0, i = 0;
1601 struct tb_path *path;
1604 /* Ring 0 is reserved for control channel */
1605 if (WARN_ON(!receive_ring || !transmit_ring))
1608 if (receive_ring > 0)
1610 if (transmit_ring > 0)
1613 if (WARN_ON(!npaths))
1616 tunnel = tb_tunnel_alloc(tb, npaths, TB_TUNNEL_DMA);
1620 tunnel->src_port = nhi;
1621 tunnel->dst_port = dst;
1622 tunnel->deinit = tb_dma_deinit;
1624 credits = min_not_zero(dma_credits, nhi->sw->max_dma_credits);
1626 if (receive_ring > 0) {
1627 path = tb_path_alloc(tb, dst, receive_path, nhi, receive_ring, 0,
1631 tunnel->paths[i++] = path;
1632 if (tb_dma_init_rx_path(path, credits)) {
1633 tb_tunnel_dbg(tunnel, "not enough buffers for RX path\n");
1638 if (transmit_ring > 0) {
1639 path = tb_path_alloc(tb, nhi, transmit_ring, dst, transmit_path, 0,
1643 tunnel->paths[i++] = path;
1644 if (tb_dma_init_tx_path(path, credits)) {
1645 tb_tunnel_dbg(tunnel, "not enough buffers for TX path\n");
1653 tb_tunnel_free(tunnel);
1658 * tb_tunnel_match_dma() - Match DMA tunnel
1659 * @tunnel: Tunnel to match
1660 * @transmit_path: HopID used for transmitting packets. Pass %-1 to ignore.
1661 * @transmit_ring: NHI ring number used to send packets towards the
1662 * other domain. Pass %-1 to ignore.
1663 * @receive_path: HopID used for receiving packets. Pass %-1 to ignore.
1664 * @receive_ring: NHI ring number used to receive packets from the
1665 * other domain. Pass %-1 to ignore.
1667 * This function can be used to match specific DMA tunnel, if there are
1668 * multiple DMA tunnels going through the same XDomain connection.
1669 * Returns true if there is match and false otherwise.
1671 bool tb_tunnel_match_dma(const struct tb_tunnel *tunnel, int transmit_path,
1672 int transmit_ring, int receive_path, int receive_ring)
1674 const struct tb_path *tx_path = NULL, *rx_path = NULL;
1677 if (!receive_ring || !transmit_ring)
1680 for (i = 0; i < tunnel->npaths; i++) {
1681 const struct tb_path *path = tunnel->paths[i];
1686 if (tb_port_is_nhi(path->hops[0].in_port))
1688 else if (tb_port_is_nhi(path->hops[path->path_length - 1].out_port))
1692 if (transmit_ring > 0 || transmit_path > 0) {
1695 if (transmit_ring > 0 &&
1696 (tx_path->hops[0].in_hop_index != transmit_ring))
1698 if (transmit_path > 0 &&
1699 (tx_path->hops[tx_path->path_length - 1].next_hop_index != transmit_path))
1703 if (receive_ring > 0 || receive_path > 0) {
1706 if (receive_path > 0 &&
1707 (rx_path->hops[0].in_hop_index != receive_path))
1709 if (receive_ring > 0 &&
1710 (rx_path->hops[rx_path->path_length - 1].next_hop_index != receive_ring))
1717 static int tb_usb3_max_link_rate(struct tb_port *up, struct tb_port *down)
1719 int ret, up_max_rate, down_max_rate;
1721 ret = usb4_usb3_port_max_link_rate(up);
1726 ret = usb4_usb3_port_max_link_rate(down);
1729 down_max_rate = ret;
1731 return min(up_max_rate, down_max_rate);
1734 static int tb_usb3_init(struct tb_tunnel *tunnel)
1736 tb_tunnel_dbg(tunnel, "allocating initial bandwidth %d/%d Mb/s\n",
1737 tunnel->allocated_up, tunnel->allocated_down);
1739 return usb4_usb3_port_allocate_bandwidth(tunnel->src_port,
1740 &tunnel->allocated_up,
1741 &tunnel->allocated_down);
1744 static int tb_usb3_activate(struct tb_tunnel *tunnel, bool activate)
1748 res = tb_usb3_port_enable(tunnel->src_port, activate);
1752 if (tb_port_is_usb3_up(tunnel->dst_port))
1753 return tb_usb3_port_enable(tunnel->dst_port, activate);
1758 static int tb_usb3_consumed_bandwidth(struct tb_tunnel *tunnel,
1759 int *consumed_up, int *consumed_down)
1761 int pcie_enabled = tb_acpi_may_tunnel_pcie();
1764 * PCIe tunneling, if enabled, affects the USB3 bandwidth so
1765 * take that it into account here.
1767 *consumed_up = tunnel->allocated_up * (3 + pcie_enabled) / 3;
1768 *consumed_down = tunnel->allocated_down * (3 + pcie_enabled) / 3;
1772 static int tb_usb3_release_unused_bandwidth(struct tb_tunnel *tunnel)
1776 ret = usb4_usb3_port_release_bandwidth(tunnel->src_port,
1777 &tunnel->allocated_up,
1778 &tunnel->allocated_down);
1782 tb_tunnel_dbg(tunnel, "decreased bandwidth allocation to %d/%d Mb/s\n",
1783 tunnel->allocated_up, tunnel->allocated_down);
1787 static void tb_usb3_reclaim_available_bandwidth(struct tb_tunnel *tunnel,
1789 int *available_down)
1791 int ret, max_rate, allocate_up, allocate_down;
1793 ret = usb4_usb3_port_actual_link_rate(tunnel->src_port);
1795 tb_tunnel_warn(tunnel, "failed to read actual link rate\n");
1798 /* Use maximum link rate if the link valid is not set */
1799 ret = tb_usb3_max_link_rate(tunnel->dst_port, tunnel->src_port);
1801 tb_tunnel_warn(tunnel, "failed to read maximum link rate\n");
1807 * 90% of the max rate can be allocated for isochronous
1810 max_rate = ret * 90 / 100;
1812 /* No need to reclaim if already at maximum */
1813 if (tunnel->allocated_up >= max_rate &&
1814 tunnel->allocated_down >= max_rate)
1817 /* Don't go lower than what is already allocated */
1818 allocate_up = min(max_rate, *available_up);
1819 if (allocate_up < tunnel->allocated_up)
1820 allocate_up = tunnel->allocated_up;
1822 allocate_down = min(max_rate, *available_down);
1823 if (allocate_down < tunnel->allocated_down)
1824 allocate_down = tunnel->allocated_down;
1826 /* If no changes no need to do more */
1827 if (allocate_up == tunnel->allocated_up &&
1828 allocate_down == tunnel->allocated_down)
1831 ret = usb4_usb3_port_allocate_bandwidth(tunnel->src_port, &allocate_up,
1834 tb_tunnel_info(tunnel, "failed to allocate bandwidth\n");
1838 tunnel->allocated_up = allocate_up;
1839 *available_up -= tunnel->allocated_up;
1841 tunnel->allocated_down = allocate_down;
1842 *available_down -= tunnel->allocated_down;
1844 tb_tunnel_dbg(tunnel, "increased bandwidth allocation to %d/%d Mb/s\n",
1845 tunnel->allocated_up, tunnel->allocated_down);
1848 static void tb_usb3_init_credits(struct tb_path_hop *hop)
1850 struct tb_port *port = hop->in_port;
1851 struct tb_switch *sw = port->sw;
1852 unsigned int credits;
1854 if (tb_port_use_credit_allocation(port)) {
1855 credits = sw->max_usb3_credits;
1857 if (tb_port_is_null(port))
1858 credits = port->bonded ? 32 : 16;
1863 hop->initial_credits = credits;
1866 static void tb_usb3_init_path(struct tb_path *path)
1868 struct tb_path_hop *hop;
1870 path->egress_fc_enable = TB_PATH_SOURCE | TB_PATH_INTERNAL;
1871 path->egress_shared_buffer = TB_PATH_NONE;
1872 path->ingress_fc_enable = TB_PATH_ALL;
1873 path->ingress_shared_buffer = TB_PATH_NONE;
1876 path->drop_packages = 0;
1878 tb_path_for_each_hop(path, hop)
1879 tb_usb3_init_credits(hop);
1883 * tb_tunnel_discover_usb3() - Discover existing USB3 tunnels
1884 * @tb: Pointer to the domain structure
1885 * @down: USB3 downstream adapter
1886 * @alloc_hopid: Allocate HopIDs from visited ports
1888 * If @down adapter is active, follows the tunnel to the USB3 upstream
1889 * adapter and back. Returns the discovered tunnel or %NULL if there was
1892 struct tb_tunnel *tb_tunnel_discover_usb3(struct tb *tb, struct tb_port *down,
1895 struct tb_tunnel *tunnel;
1896 struct tb_path *path;
1898 if (!tb_usb3_port_is_enabled(down))
1901 tunnel = tb_tunnel_alloc(tb, 2, TB_TUNNEL_USB3);
1905 tunnel->activate = tb_usb3_activate;
1906 tunnel->src_port = down;
1909 * Discover both paths even if they are not complete. We will
1910 * clean them up by calling tb_tunnel_deactivate() below in that
1913 path = tb_path_discover(down, TB_USB3_HOPID, NULL, -1,
1914 &tunnel->dst_port, "USB3 Down", alloc_hopid);
1916 /* Just disable the downstream port */
1917 tb_usb3_port_enable(down, false);
1920 tunnel->paths[TB_USB3_PATH_DOWN] = path;
1921 tb_usb3_init_path(tunnel->paths[TB_USB3_PATH_DOWN]);
1923 path = tb_path_discover(tunnel->dst_port, -1, down, TB_USB3_HOPID, NULL,
1924 "USB3 Up", alloc_hopid);
1926 goto err_deactivate;
1927 tunnel->paths[TB_USB3_PATH_UP] = path;
1928 tb_usb3_init_path(tunnel->paths[TB_USB3_PATH_UP]);
1930 /* Validate that the tunnel is complete */
1931 if (!tb_port_is_usb3_up(tunnel->dst_port)) {
1932 tb_port_warn(tunnel->dst_port,
1933 "path does not end on an USB3 adapter, cleaning up\n");
1934 goto err_deactivate;
1937 if (down != tunnel->src_port) {
1938 tb_tunnel_warn(tunnel, "path is not complete, cleaning up\n");
1939 goto err_deactivate;
1942 if (!tb_usb3_port_is_enabled(tunnel->dst_port)) {
1943 tb_tunnel_warn(tunnel,
1944 "tunnel is not fully activated, cleaning up\n");
1945 goto err_deactivate;
1948 if (!tb_route(down->sw)) {
1952 * Read the initial bandwidth allocation for the first
1955 ret = usb4_usb3_port_allocated_bandwidth(down,
1956 &tunnel->allocated_up, &tunnel->allocated_down);
1958 goto err_deactivate;
1960 tb_tunnel_dbg(tunnel, "currently allocated bandwidth %d/%d Mb/s\n",
1961 tunnel->allocated_up, tunnel->allocated_down);
1963 tunnel->init = tb_usb3_init;
1964 tunnel->consumed_bandwidth = tb_usb3_consumed_bandwidth;
1965 tunnel->release_unused_bandwidth =
1966 tb_usb3_release_unused_bandwidth;
1967 tunnel->reclaim_available_bandwidth =
1968 tb_usb3_reclaim_available_bandwidth;
1971 tb_tunnel_dbg(tunnel, "discovered\n");
1975 tb_tunnel_deactivate(tunnel);
1977 tb_tunnel_free(tunnel);
1983 * tb_tunnel_alloc_usb3() - allocate a USB3 tunnel
1984 * @tb: Pointer to the domain structure
1985 * @up: USB3 upstream adapter port
1986 * @down: USB3 downstream adapter port
1987 * @max_up: Maximum available upstream bandwidth for the USB3 tunnel (%0
1989 * @max_down: Maximum available downstream bandwidth for the USB3 tunnel
1990 * (%0 if not limited).
1992 * Allocate an USB3 tunnel. The ports must be of type @TB_TYPE_USB3_UP and
1993 * @TB_TYPE_USB3_DOWN.
1995 * Return: Returns a tb_tunnel on success or %NULL on failure.
1997 struct tb_tunnel *tb_tunnel_alloc_usb3(struct tb *tb, struct tb_port *up,
1998 struct tb_port *down, int max_up,
2001 struct tb_tunnel *tunnel;
2002 struct tb_path *path;
2006 * Check that we have enough bandwidth available for the new
2009 if (max_up > 0 || max_down > 0) {
2010 max_rate = tb_usb3_max_link_rate(down, up);
2014 /* Only 90% can be allocated for USB3 isochronous transfers */
2015 max_rate = max_rate * 90 / 100;
2016 tb_port_dbg(up, "required bandwidth for USB3 tunnel %d Mb/s\n",
2019 if (max_rate > max_up || max_rate > max_down) {
2020 tb_port_warn(up, "not enough bandwidth for USB3 tunnel\n");
2025 tunnel = tb_tunnel_alloc(tb, 2, TB_TUNNEL_USB3);
2029 tunnel->activate = tb_usb3_activate;
2030 tunnel->src_port = down;
2031 tunnel->dst_port = up;
2032 tunnel->max_up = max_up;
2033 tunnel->max_down = max_down;
2035 path = tb_path_alloc(tb, down, TB_USB3_HOPID, up, TB_USB3_HOPID, 0,
2038 tb_tunnel_free(tunnel);
2041 tb_usb3_init_path(path);
2042 tunnel->paths[TB_USB3_PATH_DOWN] = path;
2044 path = tb_path_alloc(tb, up, TB_USB3_HOPID, down, TB_USB3_HOPID, 0,
2047 tb_tunnel_free(tunnel);
2050 tb_usb3_init_path(path);
2051 tunnel->paths[TB_USB3_PATH_UP] = path;
2053 if (!tb_route(down->sw)) {
2054 tunnel->allocated_up = max_rate;
2055 tunnel->allocated_down = max_rate;
2057 tunnel->init = tb_usb3_init;
2058 tunnel->consumed_bandwidth = tb_usb3_consumed_bandwidth;
2059 tunnel->release_unused_bandwidth =
2060 tb_usb3_release_unused_bandwidth;
2061 tunnel->reclaim_available_bandwidth =
2062 tb_usb3_reclaim_available_bandwidth;
2069 * tb_tunnel_free() - free a tunnel
2070 * @tunnel: Tunnel to be freed
2072 * Frees a tunnel. The tunnel does not need to be deactivated.
2074 void tb_tunnel_free(struct tb_tunnel *tunnel)
2082 tunnel->deinit(tunnel);
2084 for (i = 0; i < tunnel->npaths; i++) {
2085 if (tunnel->paths[i])
2086 tb_path_free(tunnel->paths[i]);
2089 kfree(tunnel->paths);
2094 * tb_tunnel_is_invalid - check whether an activated path is still valid
2095 * @tunnel: Tunnel to check
2097 bool tb_tunnel_is_invalid(struct tb_tunnel *tunnel)
2101 for (i = 0; i < tunnel->npaths; i++) {
2102 WARN_ON(!tunnel->paths[i]->activated);
2103 if (tb_path_is_invalid(tunnel->paths[i]))
2111 * tb_tunnel_restart() - activate a tunnel after a hardware reset
2112 * @tunnel: Tunnel to restart
2114 * Return: 0 on success and negative errno in case if failure
2116 int tb_tunnel_restart(struct tb_tunnel *tunnel)
2120 tb_tunnel_dbg(tunnel, "activating\n");
2123 * Make sure all paths are properly disabled before enabling
2126 for (i = 0; i < tunnel->npaths; i++) {
2127 if (tunnel->paths[i]->activated) {
2128 tb_path_deactivate(tunnel->paths[i]);
2129 tunnel->paths[i]->activated = false;
2134 res = tunnel->init(tunnel);
2139 for (i = 0; i < tunnel->npaths; i++) {
2140 res = tb_path_activate(tunnel->paths[i]);
2145 if (tunnel->activate) {
2146 res = tunnel->activate(tunnel, true);
2154 tb_tunnel_warn(tunnel, "activation failed\n");
2155 tb_tunnel_deactivate(tunnel);
2160 * tb_tunnel_activate() - activate a tunnel
2161 * @tunnel: Tunnel to activate
2163 * Return: Returns 0 on success or an error code on failure.
2165 int tb_tunnel_activate(struct tb_tunnel *tunnel)
2169 for (i = 0; i < tunnel->npaths; i++) {
2170 if (tunnel->paths[i]->activated) {
2171 tb_tunnel_WARN(tunnel,
2172 "trying to activate an already activated tunnel\n");
2177 return tb_tunnel_restart(tunnel);
2181 * tb_tunnel_deactivate() - deactivate a tunnel
2182 * @tunnel: Tunnel to deactivate
2184 void tb_tunnel_deactivate(struct tb_tunnel *tunnel)
2188 tb_tunnel_dbg(tunnel, "deactivating\n");
2190 if (tunnel->activate)
2191 tunnel->activate(tunnel, false);
2193 for (i = 0; i < tunnel->npaths; i++) {
2194 if (tunnel->paths[i] && tunnel->paths[i]->activated)
2195 tb_path_deactivate(tunnel->paths[i]);
2200 * tb_tunnel_port_on_path() - Does the tunnel go through port
2201 * @tunnel: Tunnel to check
2202 * @port: Port to check
2204 * Returns true if @tunnel goes through @port (direction does not matter),
2207 bool tb_tunnel_port_on_path(const struct tb_tunnel *tunnel,
2208 const struct tb_port *port)
2212 for (i = 0; i < tunnel->npaths; i++) {
2213 if (!tunnel->paths[i])
2216 if (tb_path_port_on_path(tunnel->paths[i], port))
2223 static bool tb_tunnel_is_active(const struct tb_tunnel *tunnel)
2227 for (i = 0; i < tunnel->npaths; i++) {
2228 if (!tunnel->paths[i])
2230 if (!tunnel->paths[i]->activated)
2238 * tb_tunnel_maximum_bandwidth() - Return maximum possible bandwidth
2239 * @tunnel: Tunnel to check
2240 * @max_up: Maximum upstream bandwidth in Mb/s
2241 * @max_down: Maximum downstream bandwidth in Mb/s
2243 * Returns maximum possible bandwidth this tunnel can go if not limited
2244 * by other bandwidth clients. If the tunnel does not support this
2245 * returns %-EOPNOTSUPP.
2247 int tb_tunnel_maximum_bandwidth(struct tb_tunnel *tunnel, int *max_up,
2250 if (!tb_tunnel_is_active(tunnel))
2253 if (tunnel->maximum_bandwidth)
2254 return tunnel->maximum_bandwidth(tunnel, max_up, max_down);
2259 * tb_tunnel_allocated_bandwidth() - Return bandwidth allocated for the tunnel
2260 * @tunnel: Tunnel to check
2261 * @allocated_up: Currently allocated upstream bandwidth in Mb/s is stored here
2262 * @allocated_down: Currently allocated downstream bandwidth in Mb/s is
2265 * Returns the bandwidth allocated for the tunnel. This may be higher
2266 * than what the tunnel actually consumes.
2268 int tb_tunnel_allocated_bandwidth(struct tb_tunnel *tunnel, int *allocated_up,
2269 int *allocated_down)
2271 if (!tb_tunnel_is_active(tunnel))
2274 if (tunnel->allocated_bandwidth)
2275 return tunnel->allocated_bandwidth(tunnel, allocated_up,
2281 * tb_tunnel_alloc_bandwidth() - Change tunnel bandwidth allocation
2282 * @tunnel: Tunnel whose bandwidth allocation to change
2283 * @alloc_up: New upstream bandwidth in Mb/s
2284 * @alloc_down: New downstream bandwidth in Mb/s
2286 * Tries to change tunnel bandwidth allocation. If succeeds returns %0
2287 * and updates @alloc_up and @alloc_down to that was actually allocated
2288 * (it may not be the same as passed originally). Returns negative errno
2289 * in case of failure.
2291 int tb_tunnel_alloc_bandwidth(struct tb_tunnel *tunnel, int *alloc_up,
2294 if (!tb_tunnel_is_active(tunnel))
2297 if (tunnel->alloc_bandwidth)
2298 return tunnel->alloc_bandwidth(tunnel, alloc_up, alloc_down);
2304 * tb_tunnel_consumed_bandwidth() - Return bandwidth consumed by the tunnel
2305 * @tunnel: Tunnel to check
2306 * @consumed_up: Consumed bandwidth in Mb/s from @dst_port to @src_port.
2308 * @consumed_down: Consumed bandwidth in Mb/s from @src_port to @dst_port.
2311 * Stores the amount of isochronous bandwidth @tunnel consumes in
2312 * @consumed_up and @consumed_down. In case of success returns %0,
2313 * negative errno otherwise.
2315 int tb_tunnel_consumed_bandwidth(struct tb_tunnel *tunnel, int *consumed_up,
2318 int up_bw = 0, down_bw = 0;
2320 if (!tb_tunnel_is_active(tunnel))
2323 if (tunnel->consumed_bandwidth) {
2326 ret = tunnel->consumed_bandwidth(tunnel, &up_bw, &down_bw);
2330 tb_tunnel_dbg(tunnel, "consumed bandwidth %d/%d Mb/s\n", up_bw,
2336 *consumed_up = up_bw;
2338 *consumed_down = down_bw;
2344 * tb_tunnel_release_unused_bandwidth() - Release unused bandwidth
2345 * @tunnel: Tunnel whose unused bandwidth to release
2347 * If tunnel supports dynamic bandwidth management (USB3 tunnels at the
2348 * moment) this function makes it to release all the unused bandwidth.
2350 * Returns %0 in case of success and negative errno otherwise.
2352 int tb_tunnel_release_unused_bandwidth(struct tb_tunnel *tunnel)
2354 if (!tb_tunnel_is_active(tunnel))
2357 if (tunnel->release_unused_bandwidth) {
2360 ret = tunnel->release_unused_bandwidth(tunnel);
2369 * tb_tunnel_reclaim_available_bandwidth() - Reclaim available bandwidth
2370 * @tunnel: Tunnel reclaiming available bandwidth
2371 * @available_up: Available upstream bandwidth (in Mb/s)
2372 * @available_down: Available downstream bandwidth (in Mb/s)
2374 * Reclaims bandwidth from @available_up and @available_down and updates
2375 * the variables accordingly (e.g decreases both according to what was
2376 * reclaimed by the tunnel). If nothing was reclaimed the values are
2379 void tb_tunnel_reclaim_available_bandwidth(struct tb_tunnel *tunnel,
2381 int *available_down)
2383 if (!tb_tunnel_is_active(tunnel))
2386 if (tunnel->reclaim_available_bandwidth)
2387 tunnel->reclaim_available_bandwidth(tunnel, available_up,