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>
16 /* PCIe adapters use always HopID of 8 for both directions */
17 #define TB_PCI_HOPID 8
19 #define TB_PCI_PATH_DOWN 0
20 #define TB_PCI_PATH_UP 1
22 /* USB3 adapters use always HopID of 8 for both directions */
23 #define TB_USB3_HOPID 8
25 #define TB_USB3_PATH_DOWN 0
26 #define TB_USB3_PATH_UP 1
28 /* DP adapters use HopID 8 for AUX and 9 for Video */
29 #define TB_DP_AUX_TX_HOPID 8
30 #define TB_DP_AUX_RX_HOPID 8
31 #define TB_DP_VIDEO_HOPID 9
33 #define TB_DP_VIDEO_PATH_OUT 0
34 #define TB_DP_AUX_PATH_OUT 1
35 #define TB_DP_AUX_PATH_IN 2
37 /* Minimum number of credits needed for PCIe path */
38 #define TB_MIN_PCIE_CREDITS 6U
40 * Number of credits we try to allocate for each DMA path if not limited
41 * by the host router baMaxHI.
43 #define TB_DMA_CREDITS 14U
44 /* Minimum number of credits for DMA path */
45 #define TB_MIN_DMA_CREDITS 1U
47 static const char * const tb_tunnel_names[] = { "PCI", "DP", "DMA", "USB3" };
49 #define __TB_TUNNEL_PRINT(level, tunnel, fmt, arg...) \
51 struct tb_tunnel *__tunnel = (tunnel); \
52 level(__tunnel->tb, "%llx:%x <-> %llx:%x (%s): " fmt, \
53 tb_route(__tunnel->src_port->sw), \
54 __tunnel->src_port->port, \
55 tb_route(__tunnel->dst_port->sw), \
56 __tunnel->dst_port->port, \
57 tb_tunnel_names[__tunnel->type], \
61 #define tb_tunnel_WARN(tunnel, fmt, arg...) \
62 __TB_TUNNEL_PRINT(tb_WARN, tunnel, fmt, ##arg)
63 #define tb_tunnel_warn(tunnel, fmt, arg...) \
64 __TB_TUNNEL_PRINT(tb_warn, tunnel, fmt, ##arg)
65 #define tb_tunnel_info(tunnel, fmt, arg...) \
66 __TB_TUNNEL_PRINT(tb_info, tunnel, fmt, ##arg)
67 #define tb_tunnel_dbg(tunnel, fmt, arg...) \
68 __TB_TUNNEL_PRINT(tb_dbg, tunnel, fmt, ##arg)
70 static inline unsigned int tb_usable_credits(const struct tb_port *port)
72 return port->total_credits - port->ctl_credits;
76 * tb_available_credits() - Available credits for PCIe and DMA
77 * @port: Lane adapter to check
78 * @max_dp_streams: If non-%NULL stores maximum number of simultaneous DP
79 * streams possible through this lane adapter
81 static unsigned int tb_available_credits(const struct tb_port *port,
82 size_t *max_dp_streams)
84 const struct tb_switch *sw = port->sw;
85 int credits, usb3, pcie, spare;
88 usb3 = tb_acpi_may_tunnel_usb3() ? sw->max_usb3_credits : 0;
89 pcie = tb_acpi_may_tunnel_pcie() ? sw->max_pcie_credits : 0;
91 if (tb_acpi_is_xdomain_allowed()) {
92 spare = min_not_zero(sw->max_dma_credits, TB_DMA_CREDITS);
93 /* Add some credits for potential second DMA tunnel */
94 spare += TB_MIN_DMA_CREDITS;
99 credits = tb_usable_credits(port);
100 if (tb_acpi_may_tunnel_dp()) {
102 * Maximum number of DP streams possible through the
105 if (sw->min_dp_aux_credits + sw->min_dp_main_credits)
106 ndp = (credits - (usb3 + pcie + spare)) /
107 (sw->min_dp_aux_credits + sw->min_dp_main_credits);
113 credits -= ndp * (sw->min_dp_aux_credits + sw->min_dp_main_credits);
117 *max_dp_streams = ndp;
119 return credits > 0 ? credits : 0;
122 static struct tb_tunnel *tb_tunnel_alloc(struct tb *tb, size_t npaths,
123 enum tb_tunnel_type type)
125 struct tb_tunnel *tunnel;
127 tunnel = kzalloc(sizeof(*tunnel), GFP_KERNEL);
131 tunnel->paths = kcalloc(npaths, sizeof(tunnel->paths[0]), GFP_KERNEL);
132 if (!tunnel->paths) {
133 tb_tunnel_free(tunnel);
137 INIT_LIST_HEAD(&tunnel->list);
139 tunnel->npaths = npaths;
145 static int tb_pci_activate(struct tb_tunnel *tunnel, bool activate)
149 res = tb_pci_port_enable(tunnel->src_port, activate);
153 if (tb_port_is_pcie_up(tunnel->dst_port))
154 return tb_pci_port_enable(tunnel->dst_port, activate);
159 static int tb_pci_init_credits(struct tb_path_hop *hop)
161 struct tb_port *port = hop->in_port;
162 struct tb_switch *sw = port->sw;
163 unsigned int credits;
165 if (tb_port_use_credit_allocation(port)) {
166 unsigned int available;
168 available = tb_available_credits(port, NULL);
169 credits = min(sw->max_pcie_credits, available);
171 if (credits < TB_MIN_PCIE_CREDITS)
174 credits = max(TB_MIN_PCIE_CREDITS, credits);
176 if (tb_port_is_null(port))
177 credits = port->bonded ? 32 : 16;
182 hop->initial_credits = credits;
186 static int tb_pci_init_path(struct tb_path *path)
188 struct tb_path_hop *hop;
190 path->egress_fc_enable = TB_PATH_SOURCE | TB_PATH_INTERNAL;
191 path->egress_shared_buffer = TB_PATH_NONE;
192 path->ingress_fc_enable = TB_PATH_ALL;
193 path->ingress_shared_buffer = TB_PATH_NONE;
196 path->drop_packages = 0;
198 tb_path_for_each_hop(path, hop) {
201 ret = tb_pci_init_credits(hop);
210 * tb_tunnel_discover_pci() - Discover existing PCIe tunnels
211 * @tb: Pointer to the domain structure
212 * @down: PCIe downstream adapter
213 * @alloc_hopid: Allocate HopIDs from visited ports
215 * If @down adapter is active, follows the tunnel to the PCIe upstream
216 * adapter and back. Returns the discovered tunnel or %NULL if there was
219 struct tb_tunnel *tb_tunnel_discover_pci(struct tb *tb, struct tb_port *down,
222 struct tb_tunnel *tunnel;
223 struct tb_path *path;
225 if (!tb_pci_port_is_enabled(down))
228 tunnel = tb_tunnel_alloc(tb, 2, TB_TUNNEL_PCI);
232 tunnel->activate = tb_pci_activate;
233 tunnel->src_port = down;
236 * Discover both paths even if they are not complete. We will
237 * clean them up by calling tb_tunnel_deactivate() below in that
240 path = tb_path_discover(down, TB_PCI_HOPID, NULL, -1,
241 &tunnel->dst_port, "PCIe Up", alloc_hopid);
243 /* Just disable the downstream port */
244 tb_pci_port_enable(down, false);
247 tunnel->paths[TB_PCI_PATH_UP] = path;
248 if (tb_pci_init_path(tunnel->paths[TB_PCI_PATH_UP]))
251 path = tb_path_discover(tunnel->dst_port, -1, down, TB_PCI_HOPID, NULL,
252 "PCIe Down", alloc_hopid);
255 tunnel->paths[TB_PCI_PATH_DOWN] = path;
256 if (tb_pci_init_path(tunnel->paths[TB_PCI_PATH_DOWN]))
259 /* Validate that the tunnel is complete */
260 if (!tb_port_is_pcie_up(tunnel->dst_port)) {
261 tb_port_warn(tunnel->dst_port,
262 "path does not end on a PCIe adapter, cleaning up\n");
266 if (down != tunnel->src_port) {
267 tb_tunnel_warn(tunnel, "path is not complete, cleaning up\n");
271 if (!tb_pci_port_is_enabled(tunnel->dst_port)) {
272 tb_tunnel_warn(tunnel,
273 "tunnel is not fully activated, cleaning up\n");
277 tb_tunnel_dbg(tunnel, "discovered\n");
281 tb_tunnel_deactivate(tunnel);
283 tb_tunnel_free(tunnel);
289 * tb_tunnel_alloc_pci() - allocate a pci tunnel
290 * @tb: Pointer to the domain structure
291 * @up: PCIe upstream adapter port
292 * @down: PCIe downstream adapter port
294 * Allocate a PCI tunnel. The ports must be of type TB_TYPE_PCIE_UP and
297 * Return: Returns a tb_tunnel on success or NULL on failure.
299 struct tb_tunnel *tb_tunnel_alloc_pci(struct tb *tb, struct tb_port *up,
300 struct tb_port *down)
302 struct tb_tunnel *tunnel;
303 struct tb_path *path;
305 tunnel = tb_tunnel_alloc(tb, 2, TB_TUNNEL_PCI);
309 tunnel->activate = tb_pci_activate;
310 tunnel->src_port = down;
311 tunnel->dst_port = up;
313 path = tb_path_alloc(tb, down, TB_PCI_HOPID, up, TB_PCI_HOPID, 0,
317 tunnel->paths[TB_PCI_PATH_DOWN] = path;
318 if (tb_pci_init_path(path))
321 path = tb_path_alloc(tb, up, TB_PCI_HOPID, down, TB_PCI_HOPID, 0,
325 tunnel->paths[TB_PCI_PATH_UP] = path;
326 if (tb_pci_init_path(path))
332 tb_tunnel_free(tunnel);
336 static bool tb_dp_is_usb4(const struct tb_switch *sw)
338 /* Titan Ridge DP adapters need the same treatment as USB4 */
339 return tb_switch_is_usb4(sw) || tb_switch_is_titan_ridge(sw);
342 static int tb_dp_cm_handshake(struct tb_port *in, struct tb_port *out)
348 /* Both ends need to support this */
349 if (!tb_dp_is_usb4(in->sw) || !tb_dp_is_usb4(out->sw))
352 ret = tb_port_read(out, &val, TB_CFG_PORT,
353 out->cap_adap + DP_STATUS_CTRL, 1);
357 val |= DP_STATUS_CTRL_UF | DP_STATUS_CTRL_CMHS;
359 ret = tb_port_write(out, &val, TB_CFG_PORT,
360 out->cap_adap + DP_STATUS_CTRL, 1);
365 ret = tb_port_read(out, &val, TB_CFG_PORT,
366 out->cap_adap + DP_STATUS_CTRL, 1);
369 if (!(val & DP_STATUS_CTRL_CMHS))
371 usleep_range(10, 100);
377 static inline u32 tb_dp_cap_get_rate(u32 val)
379 u32 rate = (val & DP_COMMON_CAP_RATE_MASK) >> DP_COMMON_CAP_RATE_SHIFT;
382 case DP_COMMON_CAP_RATE_RBR:
384 case DP_COMMON_CAP_RATE_HBR:
386 case DP_COMMON_CAP_RATE_HBR2:
388 case DP_COMMON_CAP_RATE_HBR3:
395 static inline u32 tb_dp_cap_set_rate(u32 val, u32 rate)
397 val &= ~DP_COMMON_CAP_RATE_MASK;
400 WARN(1, "invalid rate %u passed, defaulting to 1620 MB/s\n", rate);
403 val |= DP_COMMON_CAP_RATE_RBR << DP_COMMON_CAP_RATE_SHIFT;
406 val |= DP_COMMON_CAP_RATE_HBR << DP_COMMON_CAP_RATE_SHIFT;
409 val |= DP_COMMON_CAP_RATE_HBR2 << DP_COMMON_CAP_RATE_SHIFT;
412 val |= DP_COMMON_CAP_RATE_HBR3 << DP_COMMON_CAP_RATE_SHIFT;
418 static inline u32 tb_dp_cap_get_lanes(u32 val)
420 u32 lanes = (val & DP_COMMON_CAP_LANES_MASK) >> DP_COMMON_CAP_LANES_SHIFT;
423 case DP_COMMON_CAP_1_LANE:
425 case DP_COMMON_CAP_2_LANES:
427 case DP_COMMON_CAP_4_LANES:
434 static inline u32 tb_dp_cap_set_lanes(u32 val, u32 lanes)
436 val &= ~DP_COMMON_CAP_LANES_MASK;
439 WARN(1, "invalid number of lanes %u passed, defaulting to 1\n",
443 val |= DP_COMMON_CAP_1_LANE << DP_COMMON_CAP_LANES_SHIFT;
446 val |= DP_COMMON_CAP_2_LANES << DP_COMMON_CAP_LANES_SHIFT;
449 val |= DP_COMMON_CAP_4_LANES << DP_COMMON_CAP_LANES_SHIFT;
455 static unsigned int tb_dp_bandwidth(unsigned int rate, unsigned int lanes)
457 /* Tunneling removes the DP 8b/10b encoding */
458 return rate * lanes * 8 / 10;
461 static int tb_dp_reduce_bandwidth(int max_bw, u32 in_rate, u32 in_lanes,
462 u32 out_rate, u32 out_lanes, u32 *new_rate,
465 static const u32 dp_bw[][2] = {
467 { 8100, 4 }, /* 25920 Mb/s */
468 { 5400, 4 }, /* 17280 Mb/s */
469 { 8100, 2 }, /* 12960 Mb/s */
470 { 2700, 4 }, /* 8640 Mb/s */
471 { 5400, 2 }, /* 8640 Mb/s */
472 { 8100, 1 }, /* 6480 Mb/s */
473 { 1620, 4 }, /* 5184 Mb/s */
474 { 5400, 1 }, /* 4320 Mb/s */
475 { 2700, 2 }, /* 4320 Mb/s */
476 { 1620, 2 }, /* 2592 Mb/s */
477 { 2700, 1 }, /* 2160 Mb/s */
478 { 1620, 1 }, /* 1296 Mb/s */
483 * Find a combination that can fit into max_bw and does not
484 * exceed the maximum rate and lanes supported by the DP OUT and
487 for (i = 0; i < ARRAY_SIZE(dp_bw); i++) {
488 if (dp_bw[i][0] > out_rate || dp_bw[i][1] > out_lanes)
491 if (dp_bw[i][0] > in_rate || dp_bw[i][1] > in_lanes)
494 if (tb_dp_bandwidth(dp_bw[i][0], dp_bw[i][1]) <= max_bw) {
495 *new_rate = dp_bw[i][0];
496 *new_lanes = dp_bw[i][1];
504 static int tb_dp_xchg_caps(struct tb_tunnel *tunnel)
506 u32 out_dp_cap, out_rate, out_lanes, in_dp_cap, in_rate, in_lanes, bw;
507 struct tb_port *out = tunnel->dst_port;
508 struct tb_port *in = tunnel->src_port;
512 * Copy DP_LOCAL_CAP register to DP_REMOTE_CAP register for
513 * newer generation hardware.
515 if (in->sw->generation < 2 || out->sw->generation < 2)
519 * Perform connection manager handshake between IN and OUT ports
520 * before capabilities exchange can take place.
522 ret = tb_dp_cm_handshake(in, out);
526 /* Read both DP_LOCAL_CAP registers */
527 ret = tb_port_read(in, &in_dp_cap, TB_CFG_PORT,
528 in->cap_adap + DP_LOCAL_CAP, 1);
532 ret = tb_port_read(out, &out_dp_cap, TB_CFG_PORT,
533 out->cap_adap + DP_LOCAL_CAP, 1);
537 /* Write IN local caps to OUT remote caps */
538 ret = tb_port_write(out, &in_dp_cap, TB_CFG_PORT,
539 out->cap_adap + DP_REMOTE_CAP, 1);
543 in_rate = tb_dp_cap_get_rate(in_dp_cap);
544 in_lanes = tb_dp_cap_get_lanes(in_dp_cap);
545 tb_port_dbg(in, "maximum supported bandwidth %u Mb/s x%u = %u Mb/s\n",
546 in_rate, in_lanes, tb_dp_bandwidth(in_rate, in_lanes));
549 * If the tunnel bandwidth is limited (max_bw is set) then see
550 * if we need to reduce bandwidth to fit there.
552 out_rate = tb_dp_cap_get_rate(out_dp_cap);
553 out_lanes = tb_dp_cap_get_lanes(out_dp_cap);
554 bw = tb_dp_bandwidth(out_rate, out_lanes);
555 tb_port_dbg(out, "maximum supported bandwidth %u Mb/s x%u = %u Mb/s\n",
556 out_rate, out_lanes, bw);
558 if (in->sw->config.depth < out->sw->config.depth)
559 max_bw = tunnel->max_down;
561 max_bw = tunnel->max_up;
563 if (max_bw && bw > max_bw) {
564 u32 new_rate, new_lanes, new_bw;
566 ret = tb_dp_reduce_bandwidth(max_bw, in_rate, in_lanes,
567 out_rate, out_lanes, &new_rate,
570 tb_port_info(out, "not enough bandwidth for DP tunnel\n");
574 new_bw = tb_dp_bandwidth(new_rate, new_lanes);
575 tb_port_dbg(out, "bandwidth reduced to %u Mb/s x%u = %u Mb/s\n",
576 new_rate, new_lanes, new_bw);
579 * Set new rate and number of lanes before writing it to
580 * the IN port remote caps.
582 out_dp_cap = tb_dp_cap_set_rate(out_dp_cap, new_rate);
583 out_dp_cap = tb_dp_cap_set_lanes(out_dp_cap, new_lanes);
587 * Titan Ridge does not disable AUX timers when it gets
588 * SET_CONFIG with SET_LTTPR_MODE set. This causes problems with
591 if (tb_route(out->sw) && tb_switch_is_titan_ridge(out->sw)) {
592 out_dp_cap |= DP_COMMON_CAP_LTTPR_NS;
593 tb_port_dbg(out, "disabling LTTPR\n");
596 return tb_port_write(in, &out_dp_cap, TB_CFG_PORT,
597 in->cap_adap + DP_REMOTE_CAP, 1);
600 static int tb_dp_activate(struct tb_tunnel *tunnel, bool active)
605 struct tb_path **paths;
608 paths = tunnel->paths;
609 last = paths[TB_DP_VIDEO_PATH_OUT]->path_length - 1;
611 tb_dp_port_set_hops(tunnel->src_port,
612 paths[TB_DP_VIDEO_PATH_OUT]->hops[0].in_hop_index,
613 paths[TB_DP_AUX_PATH_OUT]->hops[0].in_hop_index,
614 paths[TB_DP_AUX_PATH_IN]->hops[last].next_hop_index);
616 tb_dp_port_set_hops(tunnel->dst_port,
617 paths[TB_DP_VIDEO_PATH_OUT]->hops[last].next_hop_index,
618 paths[TB_DP_AUX_PATH_IN]->hops[0].in_hop_index,
619 paths[TB_DP_AUX_PATH_OUT]->hops[last].next_hop_index);
621 tb_dp_port_hpd_clear(tunnel->src_port);
622 tb_dp_port_set_hops(tunnel->src_port, 0, 0, 0);
623 if (tb_port_is_dpout(tunnel->dst_port))
624 tb_dp_port_set_hops(tunnel->dst_port, 0, 0, 0);
627 ret = tb_dp_port_enable(tunnel->src_port, active);
631 if (tb_port_is_dpout(tunnel->dst_port))
632 return tb_dp_port_enable(tunnel->dst_port, active);
637 static int tb_dp_consumed_bandwidth(struct tb_tunnel *tunnel, int *consumed_up,
640 struct tb_port *in = tunnel->src_port;
641 const struct tb_switch *sw = in->sw;
642 u32 val, rate = 0, lanes = 0;
645 if (tb_dp_is_usb4(sw)) {
649 * Wait for DPRX done. Normally it should be already set
653 ret = tb_port_read(in, &val, TB_CFG_PORT,
654 in->cap_adap + DP_COMMON_CAP, 1);
658 if (val & DP_COMMON_CAP_DPRX_DONE) {
659 rate = tb_dp_cap_get_rate(val);
660 lanes = tb_dp_cap_get_lanes(val);
668 } else if (sw->generation >= 2) {
670 * Read from the copied remote cap so that we take into
671 * account if capabilities were reduced during exchange.
673 ret = tb_port_read(in, &val, TB_CFG_PORT,
674 in->cap_adap + DP_REMOTE_CAP, 1);
678 rate = tb_dp_cap_get_rate(val);
679 lanes = tb_dp_cap_get_lanes(val);
681 /* No bandwidth management for legacy devices */
687 if (in->sw->config.depth < tunnel->dst_port->sw->config.depth) {
689 *consumed_down = tb_dp_bandwidth(rate, lanes);
691 *consumed_up = tb_dp_bandwidth(rate, lanes);
698 static void tb_dp_init_aux_credits(struct tb_path_hop *hop)
700 struct tb_port *port = hop->in_port;
701 struct tb_switch *sw = port->sw;
703 if (tb_port_use_credit_allocation(port))
704 hop->initial_credits = sw->min_dp_aux_credits;
706 hop->initial_credits = 1;
709 static void tb_dp_init_aux_path(struct tb_path *path)
711 struct tb_path_hop *hop;
713 path->egress_fc_enable = TB_PATH_SOURCE | TB_PATH_INTERNAL;
714 path->egress_shared_buffer = TB_PATH_NONE;
715 path->ingress_fc_enable = TB_PATH_ALL;
716 path->ingress_shared_buffer = TB_PATH_NONE;
720 tb_path_for_each_hop(path, hop)
721 tb_dp_init_aux_credits(hop);
724 static int tb_dp_init_video_credits(struct tb_path_hop *hop)
726 struct tb_port *port = hop->in_port;
727 struct tb_switch *sw = port->sw;
729 if (tb_port_use_credit_allocation(port)) {
730 unsigned int nfc_credits;
731 size_t max_dp_streams;
733 tb_available_credits(port, &max_dp_streams);
735 * Read the number of currently allocated NFC credits
736 * from the lane adapter. Since we only use them for DP
737 * tunneling we can use that to figure out how many DP
738 * tunnels already go through the lane adapter.
740 nfc_credits = port->config.nfc_credits &
741 ADP_CS_4_NFC_BUFFERS_MASK;
742 if (nfc_credits / sw->min_dp_main_credits > max_dp_streams)
745 hop->nfc_credits = sw->min_dp_main_credits;
747 hop->nfc_credits = min(port->total_credits - 2, 12U);
753 static int tb_dp_init_video_path(struct tb_path *path)
755 struct tb_path_hop *hop;
757 path->egress_fc_enable = TB_PATH_NONE;
758 path->egress_shared_buffer = TB_PATH_NONE;
759 path->ingress_fc_enable = TB_PATH_NONE;
760 path->ingress_shared_buffer = TB_PATH_NONE;
764 tb_path_for_each_hop(path, hop) {
767 ret = tb_dp_init_video_credits(hop);
776 * tb_tunnel_discover_dp() - Discover existing Display Port tunnels
777 * @tb: Pointer to the domain structure
779 * @alloc_hopid: Allocate HopIDs from visited ports
781 * If @in adapter is active, follows the tunnel to the DP out adapter
782 * and back. Returns the discovered tunnel or %NULL if there was no
785 * Return: DP tunnel or %NULL if no tunnel found.
787 struct tb_tunnel *tb_tunnel_discover_dp(struct tb *tb, struct tb_port *in,
790 struct tb_tunnel *tunnel;
791 struct tb_port *port;
792 struct tb_path *path;
794 if (!tb_dp_port_is_enabled(in))
797 tunnel = tb_tunnel_alloc(tb, 3, TB_TUNNEL_DP);
801 tunnel->init = tb_dp_xchg_caps;
802 tunnel->activate = tb_dp_activate;
803 tunnel->consumed_bandwidth = tb_dp_consumed_bandwidth;
804 tunnel->src_port = in;
806 path = tb_path_discover(in, TB_DP_VIDEO_HOPID, NULL, -1,
807 &tunnel->dst_port, "Video", alloc_hopid);
809 /* Just disable the DP IN port */
810 tb_dp_port_enable(in, false);
813 tunnel->paths[TB_DP_VIDEO_PATH_OUT] = path;
814 if (tb_dp_init_video_path(tunnel->paths[TB_DP_VIDEO_PATH_OUT]))
817 path = tb_path_discover(in, TB_DP_AUX_TX_HOPID, NULL, -1, NULL, "AUX TX",
821 tunnel->paths[TB_DP_AUX_PATH_OUT] = path;
822 tb_dp_init_aux_path(tunnel->paths[TB_DP_AUX_PATH_OUT]);
824 path = tb_path_discover(tunnel->dst_port, -1, in, TB_DP_AUX_RX_HOPID,
825 &port, "AUX RX", alloc_hopid);
828 tunnel->paths[TB_DP_AUX_PATH_IN] = path;
829 tb_dp_init_aux_path(tunnel->paths[TB_DP_AUX_PATH_IN]);
831 /* Validate that the tunnel is complete */
832 if (!tb_port_is_dpout(tunnel->dst_port)) {
833 tb_port_warn(in, "path does not end on a DP adapter, cleaning up\n");
837 if (!tb_dp_port_is_enabled(tunnel->dst_port))
840 if (!tb_dp_port_hpd_is_active(tunnel->dst_port))
843 if (port != tunnel->src_port) {
844 tb_tunnel_warn(tunnel, "path is not complete, cleaning up\n");
848 tb_tunnel_dbg(tunnel, "discovered\n");
852 tb_tunnel_deactivate(tunnel);
854 tb_tunnel_free(tunnel);
860 * tb_tunnel_alloc_dp() - allocate a Display Port tunnel
861 * @tb: Pointer to the domain structure
862 * @in: DP in adapter port
863 * @out: DP out adapter port
864 * @link_nr: Preferred lane adapter when the link is not bonded
865 * @max_up: Maximum available upstream bandwidth for the DP tunnel (%0
867 * @max_down: Maximum available downstream bandwidth for the DP tunnel
868 * (%0 if not limited)
870 * Allocates a tunnel between @in and @out that is capable of tunneling
871 * Display Port traffic.
873 * Return: Returns a tb_tunnel on success or NULL on failure.
875 struct tb_tunnel *tb_tunnel_alloc_dp(struct tb *tb, struct tb_port *in,
876 struct tb_port *out, int link_nr,
877 int max_up, int max_down)
879 struct tb_tunnel *tunnel;
880 struct tb_path **paths;
881 struct tb_path *path;
883 if (WARN_ON(!in->cap_adap || !out->cap_adap))
886 tunnel = tb_tunnel_alloc(tb, 3, TB_TUNNEL_DP);
890 tunnel->init = tb_dp_xchg_caps;
891 tunnel->activate = tb_dp_activate;
892 tunnel->consumed_bandwidth = tb_dp_consumed_bandwidth;
893 tunnel->src_port = in;
894 tunnel->dst_port = out;
895 tunnel->max_up = max_up;
896 tunnel->max_down = max_down;
898 paths = tunnel->paths;
900 path = tb_path_alloc(tb, in, TB_DP_VIDEO_HOPID, out, TB_DP_VIDEO_HOPID,
904 tb_dp_init_video_path(path);
905 paths[TB_DP_VIDEO_PATH_OUT] = path;
907 path = tb_path_alloc(tb, in, TB_DP_AUX_TX_HOPID, out,
908 TB_DP_AUX_TX_HOPID, link_nr, "AUX TX");
911 tb_dp_init_aux_path(path);
912 paths[TB_DP_AUX_PATH_OUT] = path;
914 path = tb_path_alloc(tb, out, TB_DP_AUX_RX_HOPID, in,
915 TB_DP_AUX_RX_HOPID, link_nr, "AUX RX");
918 tb_dp_init_aux_path(path);
919 paths[TB_DP_AUX_PATH_IN] = path;
924 tb_tunnel_free(tunnel);
928 static unsigned int tb_dma_available_credits(const struct tb_port *port)
930 const struct tb_switch *sw = port->sw;
933 credits = tb_available_credits(port, NULL);
934 if (tb_acpi_may_tunnel_pcie())
935 credits -= sw->max_pcie_credits;
936 credits -= port->dma_credits;
938 return credits > 0 ? credits : 0;
941 static int tb_dma_reserve_credits(struct tb_path_hop *hop, unsigned int credits)
943 struct tb_port *port = hop->in_port;
945 if (tb_port_use_credit_allocation(port)) {
946 unsigned int available = tb_dma_available_credits(port);
949 * Need to have at least TB_MIN_DMA_CREDITS, otherwise
950 * DMA path cannot be established.
952 if (available < TB_MIN_DMA_CREDITS)
955 while (credits > available)
958 tb_port_dbg(port, "reserving %u credits for DMA path\n",
961 port->dma_credits += credits;
963 if (tb_port_is_null(port))
964 credits = port->bonded ? 14 : 6;
966 credits = min(port->total_credits, credits);
969 hop->initial_credits = credits;
973 /* Path from lane adapter to NHI */
974 static int tb_dma_init_rx_path(struct tb_path *path, unsigned int credits)
976 struct tb_path_hop *hop;
979 path->egress_fc_enable = TB_PATH_SOURCE | TB_PATH_INTERNAL;
980 path->ingress_fc_enable = TB_PATH_ALL;
981 path->egress_shared_buffer = TB_PATH_NONE;
982 path->ingress_shared_buffer = TB_PATH_NONE;
985 path->clear_fc = true;
988 * First lane adapter is the one connected to the remote host.
989 * We don't tunnel other traffic over this link so can use all
990 * the credits (except the ones reserved for control traffic).
992 hop = &path->hops[0];
993 tmp = min(tb_usable_credits(hop->in_port), credits);
994 hop->initial_credits = tmp;
995 hop->in_port->dma_credits += tmp;
997 for (i = 1; i < path->path_length; i++) {
1000 ret = tb_dma_reserve_credits(&path->hops[i], credits);
1008 /* Path from NHI to lane adapter */
1009 static int tb_dma_init_tx_path(struct tb_path *path, unsigned int credits)
1011 struct tb_path_hop *hop;
1013 path->egress_fc_enable = TB_PATH_ALL;
1014 path->ingress_fc_enable = TB_PATH_ALL;
1015 path->egress_shared_buffer = TB_PATH_NONE;
1016 path->ingress_shared_buffer = TB_PATH_NONE;
1019 path->clear_fc = true;
1021 tb_path_for_each_hop(path, hop) {
1024 ret = tb_dma_reserve_credits(hop, credits);
1032 static void tb_dma_release_credits(struct tb_path_hop *hop)
1034 struct tb_port *port = hop->in_port;
1036 if (tb_port_use_credit_allocation(port)) {
1037 port->dma_credits -= hop->initial_credits;
1039 tb_port_dbg(port, "released %u DMA path credits\n",
1040 hop->initial_credits);
1044 static void tb_dma_deinit_path(struct tb_path *path)
1046 struct tb_path_hop *hop;
1048 tb_path_for_each_hop(path, hop)
1049 tb_dma_release_credits(hop);
1052 static void tb_dma_deinit(struct tb_tunnel *tunnel)
1056 for (i = 0; i < tunnel->npaths; i++) {
1057 if (!tunnel->paths[i])
1059 tb_dma_deinit_path(tunnel->paths[i]);
1064 * tb_tunnel_alloc_dma() - allocate a DMA tunnel
1065 * @tb: Pointer to the domain structure
1066 * @nhi: Host controller port
1067 * @dst: Destination null port which the other domain is connected to
1068 * @transmit_path: HopID used for transmitting packets
1069 * @transmit_ring: NHI ring number used to send packets towards the
1070 * other domain. Set to %-1 if TX path is not needed.
1071 * @receive_path: HopID used for receiving packets
1072 * @receive_ring: NHI ring number used to receive packets from the
1073 * other domain. Set to %-1 if RX path is not needed.
1075 * Return: Returns a tb_tunnel on success or NULL on failure.
1077 struct tb_tunnel *tb_tunnel_alloc_dma(struct tb *tb, struct tb_port *nhi,
1078 struct tb_port *dst, int transmit_path,
1079 int transmit_ring, int receive_path,
1082 struct tb_tunnel *tunnel;
1083 size_t npaths = 0, i = 0;
1084 struct tb_path *path;
1087 if (receive_ring > 0)
1089 if (transmit_ring > 0)
1092 if (WARN_ON(!npaths))
1095 tunnel = tb_tunnel_alloc(tb, npaths, TB_TUNNEL_DMA);
1099 tunnel->src_port = nhi;
1100 tunnel->dst_port = dst;
1101 tunnel->deinit = tb_dma_deinit;
1103 credits = min_not_zero(TB_DMA_CREDITS, nhi->sw->max_dma_credits);
1105 if (receive_ring > 0) {
1106 path = tb_path_alloc(tb, dst, receive_path, nhi, receive_ring, 0,
1110 tunnel->paths[i++] = path;
1111 if (tb_dma_init_rx_path(path, credits)) {
1112 tb_tunnel_dbg(tunnel, "not enough buffers for RX path\n");
1117 if (transmit_ring > 0) {
1118 path = tb_path_alloc(tb, nhi, transmit_ring, dst, transmit_path, 0,
1122 tunnel->paths[i++] = path;
1123 if (tb_dma_init_tx_path(path, credits)) {
1124 tb_tunnel_dbg(tunnel, "not enough buffers for TX path\n");
1132 tb_tunnel_free(tunnel);
1137 * tb_tunnel_match_dma() - Match DMA tunnel
1138 * @tunnel: Tunnel to match
1139 * @transmit_path: HopID used for transmitting packets. Pass %-1 to ignore.
1140 * @transmit_ring: NHI ring number used to send packets towards the
1141 * other domain. Pass %-1 to ignore.
1142 * @receive_path: HopID used for receiving packets. Pass %-1 to ignore.
1143 * @receive_ring: NHI ring number used to receive packets from the
1144 * other domain. Pass %-1 to ignore.
1146 * This function can be used to match specific DMA tunnel, if there are
1147 * multiple DMA tunnels going through the same XDomain connection.
1148 * Returns true if there is match and false otherwise.
1150 bool tb_tunnel_match_dma(const struct tb_tunnel *tunnel, int transmit_path,
1151 int transmit_ring, int receive_path, int receive_ring)
1153 const struct tb_path *tx_path = NULL, *rx_path = NULL;
1156 if (!receive_ring || !transmit_ring)
1159 for (i = 0; i < tunnel->npaths; i++) {
1160 const struct tb_path *path = tunnel->paths[i];
1165 if (tb_port_is_nhi(path->hops[0].in_port))
1167 else if (tb_port_is_nhi(path->hops[path->path_length - 1].out_port))
1171 if (transmit_ring > 0 || transmit_path > 0) {
1174 if (transmit_ring > 0 &&
1175 (tx_path->hops[0].in_hop_index != transmit_ring))
1177 if (transmit_path > 0 &&
1178 (tx_path->hops[tx_path->path_length - 1].next_hop_index != transmit_path))
1182 if (receive_ring > 0 || receive_path > 0) {
1185 if (receive_path > 0 &&
1186 (rx_path->hops[0].in_hop_index != receive_path))
1188 if (receive_ring > 0 &&
1189 (rx_path->hops[rx_path->path_length - 1].next_hop_index != receive_ring))
1196 static int tb_usb3_max_link_rate(struct tb_port *up, struct tb_port *down)
1198 int ret, up_max_rate, down_max_rate;
1200 ret = usb4_usb3_port_max_link_rate(up);
1205 ret = usb4_usb3_port_max_link_rate(down);
1208 down_max_rate = ret;
1210 return min(up_max_rate, down_max_rate);
1213 static int tb_usb3_init(struct tb_tunnel *tunnel)
1215 tb_tunnel_dbg(tunnel, "allocating initial bandwidth %d/%d Mb/s\n",
1216 tunnel->allocated_up, tunnel->allocated_down);
1218 return usb4_usb3_port_allocate_bandwidth(tunnel->src_port,
1219 &tunnel->allocated_up,
1220 &tunnel->allocated_down);
1223 static int tb_usb3_activate(struct tb_tunnel *tunnel, bool activate)
1227 res = tb_usb3_port_enable(tunnel->src_port, activate);
1231 if (tb_port_is_usb3_up(tunnel->dst_port))
1232 return tb_usb3_port_enable(tunnel->dst_port, activate);
1237 static int tb_usb3_consumed_bandwidth(struct tb_tunnel *tunnel,
1238 int *consumed_up, int *consumed_down)
1240 int pcie_enabled = tb_acpi_may_tunnel_pcie();
1243 * PCIe tunneling, if enabled, affects the USB3 bandwidth so
1244 * take that it into account here.
1246 *consumed_up = tunnel->allocated_up * (3 + pcie_enabled) / 3;
1247 *consumed_down = tunnel->allocated_down * (3 + pcie_enabled) / 3;
1251 static int tb_usb3_release_unused_bandwidth(struct tb_tunnel *tunnel)
1255 ret = usb4_usb3_port_release_bandwidth(tunnel->src_port,
1256 &tunnel->allocated_up,
1257 &tunnel->allocated_down);
1261 tb_tunnel_dbg(tunnel, "decreased bandwidth allocation to %d/%d Mb/s\n",
1262 tunnel->allocated_up, tunnel->allocated_down);
1266 static void tb_usb3_reclaim_available_bandwidth(struct tb_tunnel *tunnel,
1268 int *available_down)
1270 int ret, max_rate, allocate_up, allocate_down;
1272 ret = usb4_usb3_port_actual_link_rate(tunnel->src_port);
1274 tb_tunnel_warn(tunnel, "failed to read actual link rate\n");
1277 /* Use maximum link rate if the link valid is not set */
1278 ret = usb4_usb3_port_max_link_rate(tunnel->src_port);
1280 tb_tunnel_warn(tunnel, "failed to read maximum link rate\n");
1286 * 90% of the max rate can be allocated for isochronous
1289 max_rate = ret * 90 / 100;
1291 /* No need to reclaim if already at maximum */
1292 if (tunnel->allocated_up >= max_rate &&
1293 tunnel->allocated_down >= max_rate)
1296 /* Don't go lower than what is already allocated */
1297 allocate_up = min(max_rate, *available_up);
1298 if (allocate_up < tunnel->allocated_up)
1299 allocate_up = tunnel->allocated_up;
1301 allocate_down = min(max_rate, *available_down);
1302 if (allocate_down < tunnel->allocated_down)
1303 allocate_down = tunnel->allocated_down;
1305 /* If no changes no need to do more */
1306 if (allocate_up == tunnel->allocated_up &&
1307 allocate_down == tunnel->allocated_down)
1310 ret = usb4_usb3_port_allocate_bandwidth(tunnel->src_port, &allocate_up,
1313 tb_tunnel_info(tunnel, "failed to allocate bandwidth\n");
1317 tunnel->allocated_up = allocate_up;
1318 *available_up -= tunnel->allocated_up;
1320 tunnel->allocated_down = allocate_down;
1321 *available_down -= tunnel->allocated_down;
1323 tb_tunnel_dbg(tunnel, "increased bandwidth allocation to %d/%d Mb/s\n",
1324 tunnel->allocated_up, tunnel->allocated_down);
1327 static void tb_usb3_init_credits(struct tb_path_hop *hop)
1329 struct tb_port *port = hop->in_port;
1330 struct tb_switch *sw = port->sw;
1331 unsigned int credits;
1333 if (tb_port_use_credit_allocation(port)) {
1334 credits = sw->max_usb3_credits;
1336 if (tb_port_is_null(port))
1337 credits = port->bonded ? 32 : 16;
1342 hop->initial_credits = credits;
1345 static void tb_usb3_init_path(struct tb_path *path)
1347 struct tb_path_hop *hop;
1349 path->egress_fc_enable = TB_PATH_SOURCE | TB_PATH_INTERNAL;
1350 path->egress_shared_buffer = TB_PATH_NONE;
1351 path->ingress_fc_enable = TB_PATH_ALL;
1352 path->ingress_shared_buffer = TB_PATH_NONE;
1355 path->drop_packages = 0;
1357 tb_path_for_each_hop(path, hop)
1358 tb_usb3_init_credits(hop);
1362 * tb_tunnel_discover_usb3() - Discover existing USB3 tunnels
1363 * @tb: Pointer to the domain structure
1364 * @down: USB3 downstream adapter
1365 * @alloc_hopid: Allocate HopIDs from visited ports
1367 * If @down adapter is active, follows the tunnel to the USB3 upstream
1368 * adapter and back. Returns the discovered tunnel or %NULL if there was
1371 struct tb_tunnel *tb_tunnel_discover_usb3(struct tb *tb, struct tb_port *down,
1374 struct tb_tunnel *tunnel;
1375 struct tb_path *path;
1377 if (!tb_usb3_port_is_enabled(down))
1380 tunnel = tb_tunnel_alloc(tb, 2, TB_TUNNEL_USB3);
1384 tunnel->activate = tb_usb3_activate;
1385 tunnel->src_port = down;
1388 * Discover both paths even if they are not complete. We will
1389 * clean them up by calling tb_tunnel_deactivate() below in that
1392 path = tb_path_discover(down, TB_USB3_HOPID, NULL, -1,
1393 &tunnel->dst_port, "USB3 Down", alloc_hopid);
1395 /* Just disable the downstream port */
1396 tb_usb3_port_enable(down, false);
1399 tunnel->paths[TB_USB3_PATH_DOWN] = path;
1400 tb_usb3_init_path(tunnel->paths[TB_USB3_PATH_DOWN]);
1402 path = tb_path_discover(tunnel->dst_port, -1, down, TB_USB3_HOPID, NULL,
1403 "USB3 Up", alloc_hopid);
1405 goto err_deactivate;
1406 tunnel->paths[TB_USB3_PATH_UP] = path;
1407 tb_usb3_init_path(tunnel->paths[TB_USB3_PATH_UP]);
1409 /* Validate that the tunnel is complete */
1410 if (!tb_port_is_usb3_up(tunnel->dst_port)) {
1411 tb_port_warn(tunnel->dst_port,
1412 "path does not end on an USB3 adapter, cleaning up\n");
1413 goto err_deactivate;
1416 if (down != tunnel->src_port) {
1417 tb_tunnel_warn(tunnel, "path is not complete, cleaning up\n");
1418 goto err_deactivate;
1421 if (!tb_usb3_port_is_enabled(tunnel->dst_port)) {
1422 tb_tunnel_warn(tunnel,
1423 "tunnel is not fully activated, cleaning up\n");
1424 goto err_deactivate;
1427 if (!tb_route(down->sw)) {
1431 * Read the initial bandwidth allocation for the first
1434 ret = usb4_usb3_port_allocated_bandwidth(down,
1435 &tunnel->allocated_up, &tunnel->allocated_down);
1437 goto err_deactivate;
1439 tb_tunnel_dbg(tunnel, "currently allocated bandwidth %d/%d Mb/s\n",
1440 tunnel->allocated_up, tunnel->allocated_down);
1442 tunnel->init = tb_usb3_init;
1443 tunnel->consumed_bandwidth = tb_usb3_consumed_bandwidth;
1444 tunnel->release_unused_bandwidth =
1445 tb_usb3_release_unused_bandwidth;
1446 tunnel->reclaim_available_bandwidth =
1447 tb_usb3_reclaim_available_bandwidth;
1450 tb_tunnel_dbg(tunnel, "discovered\n");
1454 tb_tunnel_deactivate(tunnel);
1456 tb_tunnel_free(tunnel);
1462 * tb_tunnel_alloc_usb3() - allocate a USB3 tunnel
1463 * @tb: Pointer to the domain structure
1464 * @up: USB3 upstream adapter port
1465 * @down: USB3 downstream adapter port
1466 * @max_up: Maximum available upstream bandwidth for the USB3 tunnel (%0
1468 * @max_down: Maximum available downstream bandwidth for the USB3 tunnel
1469 * (%0 if not limited).
1471 * Allocate an USB3 tunnel. The ports must be of type @TB_TYPE_USB3_UP and
1472 * @TB_TYPE_USB3_DOWN.
1474 * Return: Returns a tb_tunnel on success or %NULL on failure.
1476 struct tb_tunnel *tb_tunnel_alloc_usb3(struct tb *tb, struct tb_port *up,
1477 struct tb_port *down, int max_up,
1480 struct tb_tunnel *tunnel;
1481 struct tb_path *path;
1485 * Check that we have enough bandwidth available for the new
1488 if (max_up > 0 || max_down > 0) {
1489 max_rate = tb_usb3_max_link_rate(down, up);
1493 /* Only 90% can be allocated for USB3 isochronous transfers */
1494 max_rate = max_rate * 90 / 100;
1495 tb_port_dbg(up, "required bandwidth for USB3 tunnel %d Mb/s\n",
1498 if (max_rate > max_up || max_rate > max_down) {
1499 tb_port_warn(up, "not enough bandwidth for USB3 tunnel\n");
1504 tunnel = tb_tunnel_alloc(tb, 2, TB_TUNNEL_USB3);
1508 tunnel->activate = tb_usb3_activate;
1509 tunnel->src_port = down;
1510 tunnel->dst_port = up;
1511 tunnel->max_up = max_up;
1512 tunnel->max_down = max_down;
1514 path = tb_path_alloc(tb, down, TB_USB3_HOPID, up, TB_USB3_HOPID, 0,
1517 tb_tunnel_free(tunnel);
1520 tb_usb3_init_path(path);
1521 tunnel->paths[TB_USB3_PATH_DOWN] = path;
1523 path = tb_path_alloc(tb, up, TB_USB3_HOPID, down, TB_USB3_HOPID, 0,
1526 tb_tunnel_free(tunnel);
1529 tb_usb3_init_path(path);
1530 tunnel->paths[TB_USB3_PATH_UP] = path;
1532 if (!tb_route(down->sw)) {
1533 tunnel->allocated_up = max_rate;
1534 tunnel->allocated_down = max_rate;
1536 tunnel->init = tb_usb3_init;
1537 tunnel->consumed_bandwidth = tb_usb3_consumed_bandwidth;
1538 tunnel->release_unused_bandwidth =
1539 tb_usb3_release_unused_bandwidth;
1540 tunnel->reclaim_available_bandwidth =
1541 tb_usb3_reclaim_available_bandwidth;
1548 * tb_tunnel_free() - free a tunnel
1549 * @tunnel: Tunnel to be freed
1551 * Frees a tunnel. The tunnel does not need to be deactivated.
1553 void tb_tunnel_free(struct tb_tunnel *tunnel)
1561 tunnel->deinit(tunnel);
1563 for (i = 0; i < tunnel->npaths; i++) {
1564 if (tunnel->paths[i])
1565 tb_path_free(tunnel->paths[i]);
1568 kfree(tunnel->paths);
1573 * tb_tunnel_is_invalid - check whether an activated path is still valid
1574 * @tunnel: Tunnel to check
1576 bool tb_tunnel_is_invalid(struct tb_tunnel *tunnel)
1580 for (i = 0; i < tunnel->npaths; i++) {
1581 WARN_ON(!tunnel->paths[i]->activated);
1582 if (tb_path_is_invalid(tunnel->paths[i]))
1590 * tb_tunnel_restart() - activate a tunnel after a hardware reset
1591 * @tunnel: Tunnel to restart
1593 * Return: 0 on success and negative errno in case if failure
1595 int tb_tunnel_restart(struct tb_tunnel *tunnel)
1599 tb_tunnel_dbg(tunnel, "activating\n");
1602 * Make sure all paths are properly disabled before enabling
1605 for (i = 0; i < tunnel->npaths; i++) {
1606 if (tunnel->paths[i]->activated) {
1607 tb_path_deactivate(tunnel->paths[i]);
1608 tunnel->paths[i]->activated = false;
1613 res = tunnel->init(tunnel);
1618 for (i = 0; i < tunnel->npaths; i++) {
1619 res = tb_path_activate(tunnel->paths[i]);
1624 if (tunnel->activate) {
1625 res = tunnel->activate(tunnel, true);
1633 tb_tunnel_warn(tunnel, "activation failed\n");
1634 tb_tunnel_deactivate(tunnel);
1639 * tb_tunnel_activate() - activate a tunnel
1640 * @tunnel: Tunnel to activate
1642 * Return: Returns 0 on success or an error code on failure.
1644 int tb_tunnel_activate(struct tb_tunnel *tunnel)
1648 for (i = 0; i < tunnel->npaths; i++) {
1649 if (tunnel->paths[i]->activated) {
1650 tb_tunnel_WARN(tunnel,
1651 "trying to activate an already activated tunnel\n");
1656 return tb_tunnel_restart(tunnel);
1660 * tb_tunnel_deactivate() - deactivate a tunnel
1661 * @tunnel: Tunnel to deactivate
1663 void tb_tunnel_deactivate(struct tb_tunnel *tunnel)
1667 tb_tunnel_dbg(tunnel, "deactivating\n");
1669 if (tunnel->activate)
1670 tunnel->activate(tunnel, false);
1672 for (i = 0; i < tunnel->npaths; i++) {
1673 if (tunnel->paths[i] && tunnel->paths[i]->activated)
1674 tb_path_deactivate(tunnel->paths[i]);
1679 * tb_tunnel_port_on_path() - Does the tunnel go through port
1680 * @tunnel: Tunnel to check
1681 * @port: Port to check
1683 * Returns true if @tunnel goes through @port (direction does not matter),
1686 bool tb_tunnel_port_on_path(const struct tb_tunnel *tunnel,
1687 const struct tb_port *port)
1691 for (i = 0; i < tunnel->npaths; i++) {
1692 if (!tunnel->paths[i])
1695 if (tb_path_port_on_path(tunnel->paths[i], port))
1702 static bool tb_tunnel_is_active(const struct tb_tunnel *tunnel)
1706 for (i = 0; i < tunnel->npaths; i++) {
1707 if (!tunnel->paths[i])
1709 if (!tunnel->paths[i]->activated)
1717 * tb_tunnel_consumed_bandwidth() - Return bandwidth consumed by the tunnel
1718 * @tunnel: Tunnel to check
1719 * @consumed_up: Consumed bandwidth in Mb/s from @dst_port to @src_port.
1721 * @consumed_down: Consumed bandwidth in Mb/s from @src_port to @dst_port.
1724 * Stores the amount of isochronous bandwidth @tunnel consumes in
1725 * @consumed_up and @consumed_down. In case of success returns %0,
1726 * negative errno otherwise.
1728 int tb_tunnel_consumed_bandwidth(struct tb_tunnel *tunnel, int *consumed_up,
1731 int up_bw = 0, down_bw = 0;
1733 if (!tb_tunnel_is_active(tunnel))
1736 if (tunnel->consumed_bandwidth) {
1739 ret = tunnel->consumed_bandwidth(tunnel, &up_bw, &down_bw);
1743 tb_tunnel_dbg(tunnel, "consumed bandwidth %d/%d Mb/s\n", up_bw,
1749 *consumed_up = up_bw;
1751 *consumed_down = down_bw;
1757 * tb_tunnel_release_unused_bandwidth() - Release unused bandwidth
1758 * @tunnel: Tunnel whose unused bandwidth to release
1760 * If tunnel supports dynamic bandwidth management (USB3 tunnels at the
1761 * moment) this function makes it to release all the unused bandwidth.
1763 * Returns %0 in case of success and negative errno otherwise.
1765 int tb_tunnel_release_unused_bandwidth(struct tb_tunnel *tunnel)
1767 if (!tb_tunnel_is_active(tunnel))
1770 if (tunnel->release_unused_bandwidth) {
1773 ret = tunnel->release_unused_bandwidth(tunnel);
1782 * tb_tunnel_reclaim_available_bandwidth() - Reclaim available bandwidth
1783 * @tunnel: Tunnel reclaiming available bandwidth
1784 * @available_up: Available upstream bandwidth (in Mb/s)
1785 * @available_down: Available downstream bandwidth (in Mb/s)
1787 * Reclaims bandwidth from @available_up and @available_down and updates
1788 * the variables accordingly (e.g decreases both according to what was
1789 * reclaimed by the tunnel). If nothing was reclaimed the values are
1792 void tb_tunnel_reclaim_available_bandwidth(struct tb_tunnel *tunnel,
1794 int *available_down)
1796 if (!tb_tunnel_is_active(tunnel))
1799 if (tunnel->reclaim_available_bandwidth)
1800 tunnel->reclaim_available_bandwidth(tunnel, available_up,