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 static const char * const tb_tunnel_names[] = { "PCI", "DP", "DMA", "USB3" };
39 #define __TB_TUNNEL_PRINT(level, tunnel, fmt, arg...) \
41 struct tb_tunnel *__tunnel = (tunnel); \
42 level(__tunnel->tb, "%llx:%x <-> %llx:%x (%s): " fmt, \
43 tb_route(__tunnel->src_port->sw), \
44 __tunnel->src_port->port, \
45 tb_route(__tunnel->dst_port->sw), \
46 __tunnel->dst_port->port, \
47 tb_tunnel_names[__tunnel->type], \
51 #define tb_tunnel_WARN(tunnel, fmt, arg...) \
52 __TB_TUNNEL_PRINT(tb_WARN, tunnel, fmt, ##arg)
53 #define tb_tunnel_warn(tunnel, fmt, arg...) \
54 __TB_TUNNEL_PRINT(tb_warn, tunnel, fmt, ##arg)
55 #define tb_tunnel_info(tunnel, fmt, arg...) \
56 __TB_TUNNEL_PRINT(tb_info, tunnel, fmt, ##arg)
57 #define tb_tunnel_dbg(tunnel, fmt, arg...) \
58 __TB_TUNNEL_PRINT(tb_dbg, tunnel, fmt, ##arg)
60 static struct tb_tunnel *tb_tunnel_alloc(struct tb *tb, size_t npaths,
61 enum tb_tunnel_type type)
63 struct tb_tunnel *tunnel;
65 tunnel = kzalloc(sizeof(*tunnel), GFP_KERNEL);
69 tunnel->paths = kcalloc(npaths, sizeof(tunnel->paths[0]), GFP_KERNEL);
71 tb_tunnel_free(tunnel);
75 INIT_LIST_HEAD(&tunnel->list);
77 tunnel->npaths = npaths;
83 static int tb_pci_activate(struct tb_tunnel *tunnel, bool activate)
87 res = tb_pci_port_enable(tunnel->src_port, activate);
91 if (tb_port_is_pcie_up(tunnel->dst_port))
92 return tb_pci_port_enable(tunnel->dst_port, activate);
97 static int tb_initial_credits(const struct tb_switch *sw)
99 /* If the path is complete sw is not NULL */
101 /* More credits for faster link */
102 switch (sw->link_speed * sw->link_width) {
113 static void tb_pci_init_path(struct tb_path *path)
115 path->egress_fc_enable = TB_PATH_SOURCE | TB_PATH_INTERNAL;
116 path->egress_shared_buffer = TB_PATH_NONE;
117 path->ingress_fc_enable = TB_PATH_ALL;
118 path->ingress_shared_buffer = TB_PATH_NONE;
121 path->drop_packages = 0;
122 path->hops[0].initial_credits = 7;
123 if (path->path_length > 1)
124 path->hops[1].initial_credits =
125 tb_initial_credits(path->hops[1].in_port->sw);
129 * tb_tunnel_discover_pci() - Discover existing PCIe tunnels
130 * @tb: Pointer to the domain structure
131 * @down: PCIe downstream adapter
133 * If @down adapter is active, follows the tunnel to the PCIe upstream
134 * adapter and back. Returns the discovered tunnel or %NULL if there was
137 struct tb_tunnel *tb_tunnel_discover_pci(struct tb *tb, struct tb_port *down)
139 struct tb_tunnel *tunnel;
140 struct tb_path *path;
142 if (!tb_pci_port_is_enabled(down))
145 tunnel = tb_tunnel_alloc(tb, 2, TB_TUNNEL_PCI);
149 tunnel->activate = tb_pci_activate;
150 tunnel->src_port = down;
153 * Discover both paths even if they are not complete. We will
154 * clean them up by calling tb_tunnel_deactivate() below in that
157 path = tb_path_discover(down, TB_PCI_HOPID, NULL, -1,
158 &tunnel->dst_port, "PCIe Up");
160 /* Just disable the downstream port */
161 tb_pci_port_enable(down, false);
164 tunnel->paths[TB_PCI_PATH_UP] = path;
165 tb_pci_init_path(tunnel->paths[TB_PCI_PATH_UP]);
167 path = tb_path_discover(tunnel->dst_port, -1, down, TB_PCI_HOPID, NULL,
171 tunnel->paths[TB_PCI_PATH_DOWN] = path;
172 tb_pci_init_path(tunnel->paths[TB_PCI_PATH_DOWN]);
174 /* Validate that the tunnel is complete */
175 if (!tb_port_is_pcie_up(tunnel->dst_port)) {
176 tb_port_warn(tunnel->dst_port,
177 "path does not end on a PCIe adapter, cleaning up\n");
181 if (down != tunnel->src_port) {
182 tb_tunnel_warn(tunnel, "path is not complete, cleaning up\n");
186 if (!tb_pci_port_is_enabled(tunnel->dst_port)) {
187 tb_tunnel_warn(tunnel,
188 "tunnel is not fully activated, cleaning up\n");
192 tb_tunnel_dbg(tunnel, "discovered\n");
196 tb_tunnel_deactivate(tunnel);
198 tb_tunnel_free(tunnel);
204 * tb_tunnel_alloc_pci() - allocate a pci tunnel
205 * @tb: Pointer to the domain structure
206 * @up: PCIe upstream adapter port
207 * @down: PCIe downstream adapter port
209 * Allocate a PCI tunnel. The ports must be of type TB_TYPE_PCIE_UP and
212 * Return: Returns a tb_tunnel on success or NULL on failure.
214 struct tb_tunnel *tb_tunnel_alloc_pci(struct tb *tb, struct tb_port *up,
215 struct tb_port *down)
217 struct tb_tunnel *tunnel;
218 struct tb_path *path;
220 tunnel = tb_tunnel_alloc(tb, 2, TB_TUNNEL_PCI);
224 tunnel->activate = tb_pci_activate;
225 tunnel->src_port = down;
226 tunnel->dst_port = up;
228 path = tb_path_alloc(tb, down, TB_PCI_HOPID, up, TB_PCI_HOPID, 0,
231 tb_tunnel_free(tunnel);
234 tb_pci_init_path(path);
235 tunnel->paths[TB_PCI_PATH_DOWN] = path;
237 path = tb_path_alloc(tb, up, TB_PCI_HOPID, down, TB_PCI_HOPID, 0,
240 tb_tunnel_free(tunnel);
243 tb_pci_init_path(path);
244 tunnel->paths[TB_PCI_PATH_UP] = path;
249 static bool tb_dp_is_usb4(const struct tb_switch *sw)
251 /* Titan Ridge DP adapters need the same treatment as USB4 */
252 return tb_switch_is_usb4(sw) || tb_switch_is_titan_ridge(sw);
255 static int tb_dp_cm_handshake(struct tb_port *in, struct tb_port *out)
261 /* Both ends need to support this */
262 if (!tb_dp_is_usb4(in->sw) || !tb_dp_is_usb4(out->sw))
265 ret = tb_port_read(out, &val, TB_CFG_PORT,
266 out->cap_adap + DP_STATUS_CTRL, 1);
270 val |= DP_STATUS_CTRL_UF | DP_STATUS_CTRL_CMHS;
272 ret = tb_port_write(out, &val, TB_CFG_PORT,
273 out->cap_adap + DP_STATUS_CTRL, 1);
278 ret = tb_port_read(out, &val, TB_CFG_PORT,
279 out->cap_adap + DP_STATUS_CTRL, 1);
282 if (!(val & DP_STATUS_CTRL_CMHS))
284 usleep_range(10, 100);
290 static inline u32 tb_dp_cap_get_rate(u32 val)
292 u32 rate = (val & DP_COMMON_CAP_RATE_MASK) >> DP_COMMON_CAP_RATE_SHIFT;
295 case DP_COMMON_CAP_RATE_RBR:
297 case DP_COMMON_CAP_RATE_HBR:
299 case DP_COMMON_CAP_RATE_HBR2:
301 case DP_COMMON_CAP_RATE_HBR3:
308 static inline u32 tb_dp_cap_set_rate(u32 val, u32 rate)
310 val &= ~DP_COMMON_CAP_RATE_MASK;
313 WARN(1, "invalid rate %u passed, defaulting to 1620 MB/s\n", rate);
316 val |= DP_COMMON_CAP_RATE_RBR << DP_COMMON_CAP_RATE_SHIFT;
319 val |= DP_COMMON_CAP_RATE_HBR << DP_COMMON_CAP_RATE_SHIFT;
322 val |= DP_COMMON_CAP_RATE_HBR2 << DP_COMMON_CAP_RATE_SHIFT;
325 val |= DP_COMMON_CAP_RATE_HBR3 << DP_COMMON_CAP_RATE_SHIFT;
331 static inline u32 tb_dp_cap_get_lanes(u32 val)
333 u32 lanes = (val & DP_COMMON_CAP_LANES_MASK) >> DP_COMMON_CAP_LANES_SHIFT;
336 case DP_COMMON_CAP_1_LANE:
338 case DP_COMMON_CAP_2_LANES:
340 case DP_COMMON_CAP_4_LANES:
347 static inline u32 tb_dp_cap_set_lanes(u32 val, u32 lanes)
349 val &= ~DP_COMMON_CAP_LANES_MASK;
352 WARN(1, "invalid number of lanes %u passed, defaulting to 1\n",
356 val |= DP_COMMON_CAP_1_LANE << DP_COMMON_CAP_LANES_SHIFT;
359 val |= DP_COMMON_CAP_2_LANES << DP_COMMON_CAP_LANES_SHIFT;
362 val |= DP_COMMON_CAP_4_LANES << DP_COMMON_CAP_LANES_SHIFT;
368 static unsigned int tb_dp_bandwidth(unsigned int rate, unsigned int lanes)
370 /* Tunneling removes the DP 8b/10b encoding */
371 return rate * lanes * 8 / 10;
374 static int tb_dp_reduce_bandwidth(int max_bw, u32 in_rate, u32 in_lanes,
375 u32 out_rate, u32 out_lanes, u32 *new_rate,
378 static const u32 dp_bw[][2] = {
380 { 8100, 4 }, /* 25920 Mb/s */
381 { 5400, 4 }, /* 17280 Mb/s */
382 { 8100, 2 }, /* 12960 Mb/s */
383 { 2700, 4 }, /* 8640 Mb/s */
384 { 5400, 2 }, /* 8640 Mb/s */
385 { 8100, 1 }, /* 6480 Mb/s */
386 { 1620, 4 }, /* 5184 Mb/s */
387 { 5400, 1 }, /* 4320 Mb/s */
388 { 2700, 2 }, /* 4320 Mb/s */
389 { 1620, 2 }, /* 2592 Mb/s */
390 { 2700, 1 }, /* 2160 Mb/s */
391 { 1620, 1 }, /* 1296 Mb/s */
396 * Find a combination that can fit into max_bw and does not
397 * exceed the maximum rate and lanes supported by the DP OUT and
400 for (i = 0; i < ARRAY_SIZE(dp_bw); i++) {
401 if (dp_bw[i][0] > out_rate || dp_bw[i][1] > out_lanes)
404 if (dp_bw[i][0] > in_rate || dp_bw[i][1] > in_lanes)
407 if (tb_dp_bandwidth(dp_bw[i][0], dp_bw[i][1]) <= max_bw) {
408 *new_rate = dp_bw[i][0];
409 *new_lanes = dp_bw[i][1];
417 static int tb_dp_xchg_caps(struct tb_tunnel *tunnel)
419 u32 out_dp_cap, out_rate, out_lanes, in_dp_cap, in_rate, in_lanes, bw;
420 struct tb_port *out = tunnel->dst_port;
421 struct tb_port *in = tunnel->src_port;
425 * Copy DP_LOCAL_CAP register to DP_REMOTE_CAP register for
426 * newer generation hardware.
428 if (in->sw->generation < 2 || out->sw->generation < 2)
432 * Perform connection manager handshake between IN and OUT ports
433 * before capabilities exchange can take place.
435 ret = tb_dp_cm_handshake(in, out);
439 /* Read both DP_LOCAL_CAP registers */
440 ret = tb_port_read(in, &in_dp_cap, TB_CFG_PORT,
441 in->cap_adap + DP_LOCAL_CAP, 1);
445 ret = tb_port_read(out, &out_dp_cap, TB_CFG_PORT,
446 out->cap_adap + DP_LOCAL_CAP, 1);
450 /* Write IN local caps to OUT remote caps */
451 ret = tb_port_write(out, &in_dp_cap, TB_CFG_PORT,
452 out->cap_adap + DP_REMOTE_CAP, 1);
456 in_rate = tb_dp_cap_get_rate(in_dp_cap);
457 in_lanes = tb_dp_cap_get_lanes(in_dp_cap);
458 tb_port_dbg(in, "maximum supported bandwidth %u Mb/s x%u = %u Mb/s\n",
459 in_rate, in_lanes, tb_dp_bandwidth(in_rate, in_lanes));
462 * If the tunnel bandwidth is limited (max_bw is set) then see
463 * if we need to reduce bandwidth to fit there.
465 out_rate = tb_dp_cap_get_rate(out_dp_cap);
466 out_lanes = tb_dp_cap_get_lanes(out_dp_cap);
467 bw = tb_dp_bandwidth(out_rate, out_lanes);
468 tb_port_dbg(out, "maximum supported bandwidth %u Mb/s x%u = %u Mb/s\n",
469 out_rate, out_lanes, bw);
471 if (in->sw->config.depth < out->sw->config.depth)
472 max_bw = tunnel->max_down;
474 max_bw = tunnel->max_up;
476 if (max_bw && bw > max_bw) {
477 u32 new_rate, new_lanes, new_bw;
479 ret = tb_dp_reduce_bandwidth(max_bw, in_rate, in_lanes,
480 out_rate, out_lanes, &new_rate,
483 tb_port_info(out, "not enough bandwidth for DP tunnel\n");
487 new_bw = tb_dp_bandwidth(new_rate, new_lanes);
488 tb_port_dbg(out, "bandwidth reduced to %u Mb/s x%u = %u Mb/s\n",
489 new_rate, new_lanes, new_bw);
492 * Set new rate and number of lanes before writing it to
493 * the IN port remote caps.
495 out_dp_cap = tb_dp_cap_set_rate(out_dp_cap, new_rate);
496 out_dp_cap = tb_dp_cap_set_lanes(out_dp_cap, new_lanes);
499 return tb_port_write(in, &out_dp_cap, TB_CFG_PORT,
500 in->cap_adap + DP_REMOTE_CAP, 1);
503 static int tb_dp_activate(struct tb_tunnel *tunnel, bool active)
508 struct tb_path **paths;
511 paths = tunnel->paths;
512 last = paths[TB_DP_VIDEO_PATH_OUT]->path_length - 1;
514 tb_dp_port_set_hops(tunnel->src_port,
515 paths[TB_DP_VIDEO_PATH_OUT]->hops[0].in_hop_index,
516 paths[TB_DP_AUX_PATH_OUT]->hops[0].in_hop_index,
517 paths[TB_DP_AUX_PATH_IN]->hops[last].next_hop_index);
519 tb_dp_port_set_hops(tunnel->dst_port,
520 paths[TB_DP_VIDEO_PATH_OUT]->hops[last].next_hop_index,
521 paths[TB_DP_AUX_PATH_IN]->hops[0].in_hop_index,
522 paths[TB_DP_AUX_PATH_OUT]->hops[last].next_hop_index);
524 tb_dp_port_hpd_clear(tunnel->src_port);
525 tb_dp_port_set_hops(tunnel->src_port, 0, 0, 0);
526 if (tb_port_is_dpout(tunnel->dst_port))
527 tb_dp_port_set_hops(tunnel->dst_port, 0, 0, 0);
530 ret = tb_dp_port_enable(tunnel->src_port, active);
534 if (tb_port_is_dpout(tunnel->dst_port))
535 return tb_dp_port_enable(tunnel->dst_port, active);
540 static int tb_dp_consumed_bandwidth(struct tb_tunnel *tunnel, int *consumed_up,
543 struct tb_port *in = tunnel->src_port;
544 const struct tb_switch *sw = in->sw;
545 u32 val, rate = 0, lanes = 0;
548 if (tb_dp_is_usb4(sw)) {
552 * Wait for DPRX done. Normally it should be already set
556 ret = tb_port_read(in, &val, TB_CFG_PORT,
557 in->cap_adap + DP_COMMON_CAP, 1);
561 if (val & DP_COMMON_CAP_DPRX_DONE) {
562 rate = tb_dp_cap_get_rate(val);
563 lanes = tb_dp_cap_get_lanes(val);
571 } else if (sw->generation >= 2) {
573 * Read from the copied remote cap so that we take into
574 * account if capabilities were reduced during exchange.
576 ret = tb_port_read(in, &val, TB_CFG_PORT,
577 in->cap_adap + DP_REMOTE_CAP, 1);
581 rate = tb_dp_cap_get_rate(val);
582 lanes = tb_dp_cap_get_lanes(val);
584 /* No bandwidth management for legacy devices */
590 if (in->sw->config.depth < tunnel->dst_port->sw->config.depth) {
592 *consumed_down = tb_dp_bandwidth(rate, lanes);
594 *consumed_up = tb_dp_bandwidth(rate, lanes);
601 static void tb_dp_init_aux_path(struct tb_path *path)
605 path->egress_fc_enable = TB_PATH_SOURCE | TB_PATH_INTERNAL;
606 path->egress_shared_buffer = TB_PATH_NONE;
607 path->ingress_fc_enable = TB_PATH_ALL;
608 path->ingress_shared_buffer = TB_PATH_NONE;
612 for (i = 0; i < path->path_length; i++)
613 path->hops[i].initial_credits = 1;
616 static void tb_dp_init_video_path(struct tb_path *path, bool discover)
620 path->egress_fc_enable = TB_PATH_NONE;
621 path->egress_shared_buffer = TB_PATH_NONE;
622 path->ingress_fc_enable = TB_PATH_NONE;
623 path->ingress_shared_buffer = TB_PATH_NONE;
627 for (i = 0; i < path->path_length; i++) {
628 u32 nfc_credits = path->hops[i].in_port->config.nfc_credits;
631 path->hops[i].nfc_credits =
632 nfc_credits & ADP_CS_4_NFC_BUFFERS_MASK;
636 max_credits = (nfc_credits & ADP_CS_4_TOTAL_BUFFERS_MASK) >>
637 ADP_CS_4_TOTAL_BUFFERS_SHIFT;
638 /* Leave some credits for AUX path */
639 path->hops[i].nfc_credits = min(max_credits - 2, 12U);
645 * tb_tunnel_discover_dp() - Discover existing Display Port tunnels
646 * @tb: Pointer to the domain structure
649 * If @in adapter is active, follows the tunnel to the DP out adapter
650 * and back. Returns the discovered tunnel or %NULL if there was no
653 * Return: DP tunnel or %NULL if no tunnel found.
655 struct tb_tunnel *tb_tunnel_discover_dp(struct tb *tb, struct tb_port *in)
657 struct tb_tunnel *tunnel;
658 struct tb_port *port;
659 struct tb_path *path;
661 if (!tb_dp_port_is_enabled(in))
664 tunnel = tb_tunnel_alloc(tb, 3, TB_TUNNEL_DP);
668 tunnel->init = tb_dp_xchg_caps;
669 tunnel->activate = tb_dp_activate;
670 tunnel->consumed_bandwidth = tb_dp_consumed_bandwidth;
671 tunnel->src_port = in;
673 path = tb_path_discover(in, TB_DP_VIDEO_HOPID, NULL, -1,
674 &tunnel->dst_port, "Video");
676 /* Just disable the DP IN port */
677 tb_dp_port_enable(in, false);
680 tunnel->paths[TB_DP_VIDEO_PATH_OUT] = path;
681 tb_dp_init_video_path(tunnel->paths[TB_DP_VIDEO_PATH_OUT], true);
683 path = tb_path_discover(in, TB_DP_AUX_TX_HOPID, NULL, -1, NULL, "AUX TX");
686 tunnel->paths[TB_DP_AUX_PATH_OUT] = path;
687 tb_dp_init_aux_path(tunnel->paths[TB_DP_AUX_PATH_OUT]);
689 path = tb_path_discover(tunnel->dst_port, -1, in, TB_DP_AUX_RX_HOPID,
693 tunnel->paths[TB_DP_AUX_PATH_IN] = path;
694 tb_dp_init_aux_path(tunnel->paths[TB_DP_AUX_PATH_IN]);
696 /* Validate that the tunnel is complete */
697 if (!tb_port_is_dpout(tunnel->dst_port)) {
698 tb_port_warn(in, "path does not end on a DP adapter, cleaning up\n");
702 if (!tb_dp_port_is_enabled(tunnel->dst_port))
705 if (!tb_dp_port_hpd_is_active(tunnel->dst_port))
708 if (port != tunnel->src_port) {
709 tb_tunnel_warn(tunnel, "path is not complete, cleaning up\n");
713 tb_tunnel_dbg(tunnel, "discovered\n");
717 tb_tunnel_deactivate(tunnel);
719 tb_tunnel_free(tunnel);
725 * tb_tunnel_alloc_dp() - allocate a Display Port tunnel
726 * @tb: Pointer to the domain structure
727 * @in: DP in adapter port
728 * @out: DP out adapter port
729 * @max_up: Maximum available upstream bandwidth for the DP tunnel (%0
731 * @max_down: Maximum available downstream bandwidth for the DP tunnel
732 * (%0 if not limited)
734 * Allocates a tunnel between @in and @out that is capable of tunneling
735 * Display Port traffic.
737 * Return: Returns a tb_tunnel on success or NULL on failure.
739 struct tb_tunnel *tb_tunnel_alloc_dp(struct tb *tb, struct tb_port *in,
740 struct tb_port *out, int max_up,
743 struct tb_tunnel *tunnel;
744 struct tb_path **paths;
745 struct tb_path *path;
747 if (WARN_ON(!in->cap_adap || !out->cap_adap))
750 tunnel = tb_tunnel_alloc(tb, 3, TB_TUNNEL_DP);
754 tunnel->init = tb_dp_xchg_caps;
755 tunnel->activate = tb_dp_activate;
756 tunnel->consumed_bandwidth = tb_dp_consumed_bandwidth;
757 tunnel->src_port = in;
758 tunnel->dst_port = out;
759 tunnel->max_up = max_up;
760 tunnel->max_down = max_down;
762 paths = tunnel->paths;
764 path = tb_path_alloc(tb, in, TB_DP_VIDEO_HOPID, out, TB_DP_VIDEO_HOPID,
768 tb_dp_init_video_path(path, false);
769 paths[TB_DP_VIDEO_PATH_OUT] = path;
771 path = tb_path_alloc(tb, in, TB_DP_AUX_TX_HOPID, out,
772 TB_DP_AUX_TX_HOPID, 1, "AUX TX");
775 tb_dp_init_aux_path(path);
776 paths[TB_DP_AUX_PATH_OUT] = path;
778 path = tb_path_alloc(tb, out, TB_DP_AUX_RX_HOPID, in,
779 TB_DP_AUX_RX_HOPID, 1, "AUX RX");
782 tb_dp_init_aux_path(path);
783 paths[TB_DP_AUX_PATH_IN] = path;
788 tb_tunnel_free(tunnel);
792 static u32 tb_dma_credits(struct tb_port *nhi)
796 max_credits = (nhi->config.nfc_credits & ADP_CS_4_TOTAL_BUFFERS_MASK) >>
797 ADP_CS_4_TOTAL_BUFFERS_SHIFT;
798 return min(max_credits, 13U);
801 static void tb_dma_init_path(struct tb_path *path, unsigned int efc, u32 credits)
805 path->egress_fc_enable = efc;
806 path->ingress_fc_enable = TB_PATH_ALL;
807 path->egress_shared_buffer = TB_PATH_NONE;
808 path->ingress_shared_buffer = TB_PATH_NONE;
811 path->clear_fc = true;
813 for (i = 0; i < path->path_length; i++)
814 path->hops[i].initial_credits = credits;
818 * tb_tunnel_alloc_dma() - allocate a DMA tunnel
819 * @tb: Pointer to the domain structure
820 * @nhi: Host controller port
821 * @dst: Destination null port which the other domain is connected to
822 * @transmit_path: HopID used for transmitting packets
823 * @transmit_ring: NHI ring number used to send packets towards the
824 * other domain. Set to %-1 if TX path is not needed.
825 * @receive_path: HopID used for receiving packets
826 * @receive_ring: NHI ring number used to receive packets from the
827 * other domain. Set to %-1 if RX path is not needed.
829 * Return: Returns a tb_tunnel on success or NULL on failure.
831 struct tb_tunnel *tb_tunnel_alloc_dma(struct tb *tb, struct tb_port *nhi,
832 struct tb_port *dst, int transmit_path,
833 int transmit_ring, int receive_path,
836 struct tb_tunnel *tunnel;
837 size_t npaths = 0, i = 0;
838 struct tb_path *path;
841 if (receive_ring > 0)
843 if (transmit_ring > 0)
846 if (WARN_ON(!npaths))
849 tunnel = tb_tunnel_alloc(tb, npaths, TB_TUNNEL_DMA);
853 tunnel->src_port = nhi;
854 tunnel->dst_port = dst;
856 credits = tb_dma_credits(nhi);
858 if (receive_ring > 0) {
859 path = tb_path_alloc(tb, dst, receive_path, nhi, receive_ring, 0,
862 tb_tunnel_free(tunnel);
865 tb_dma_init_path(path, TB_PATH_SOURCE | TB_PATH_INTERNAL, credits);
866 tunnel->paths[i++] = path;
869 if (transmit_ring > 0) {
870 path = tb_path_alloc(tb, nhi, transmit_ring, dst, transmit_path, 0,
873 tb_tunnel_free(tunnel);
876 tb_dma_init_path(path, TB_PATH_ALL, credits);
877 tunnel->paths[i++] = path;
884 * tb_tunnel_match_dma() - Match DMA tunnel
885 * @tunnel: Tunnel to match
886 * @transmit_path: HopID used for transmitting packets. Pass %-1 to ignore.
887 * @transmit_ring: NHI ring number used to send packets towards the
888 * other domain. Pass %-1 to ignore.
889 * @receive_path: HopID used for receiving packets. Pass %-1 to ignore.
890 * @receive_ring: NHI ring number used to receive packets from the
891 * other domain. Pass %-1 to ignore.
893 * This function can be used to match specific DMA tunnel, if there are
894 * multiple DMA tunnels going through the same XDomain connection.
895 * Returns true if there is match and false otherwise.
897 bool tb_tunnel_match_dma(const struct tb_tunnel *tunnel, int transmit_path,
898 int transmit_ring, int receive_path, int receive_ring)
900 const struct tb_path *tx_path = NULL, *rx_path = NULL;
903 if (!receive_ring || !transmit_ring)
906 for (i = 0; i < tunnel->npaths; i++) {
907 const struct tb_path *path = tunnel->paths[i];
912 if (tb_port_is_nhi(path->hops[0].in_port))
914 else if (tb_port_is_nhi(path->hops[path->path_length - 1].out_port))
918 if (transmit_ring > 0 || transmit_path > 0) {
921 if (transmit_ring > 0 &&
922 (tx_path->hops[0].in_hop_index != transmit_ring))
924 if (transmit_path > 0 &&
925 (tx_path->hops[tx_path->path_length - 1].next_hop_index != transmit_path))
929 if (receive_ring > 0 || receive_path > 0) {
932 if (receive_path > 0 &&
933 (rx_path->hops[0].in_hop_index != receive_path))
935 if (receive_ring > 0 &&
936 (rx_path->hops[rx_path->path_length - 1].next_hop_index != receive_ring))
943 static int tb_usb3_max_link_rate(struct tb_port *up, struct tb_port *down)
945 int ret, up_max_rate, down_max_rate;
947 ret = usb4_usb3_port_max_link_rate(up);
952 ret = usb4_usb3_port_max_link_rate(down);
957 return min(up_max_rate, down_max_rate);
960 static int tb_usb3_init(struct tb_tunnel *tunnel)
962 tb_tunnel_dbg(tunnel, "allocating initial bandwidth %d/%d Mb/s\n",
963 tunnel->allocated_up, tunnel->allocated_down);
965 return usb4_usb3_port_allocate_bandwidth(tunnel->src_port,
966 &tunnel->allocated_up,
967 &tunnel->allocated_down);
970 static int tb_usb3_activate(struct tb_tunnel *tunnel, bool activate)
974 res = tb_usb3_port_enable(tunnel->src_port, activate);
978 if (tb_port_is_usb3_up(tunnel->dst_port))
979 return tb_usb3_port_enable(tunnel->dst_port, activate);
984 static int tb_usb3_consumed_bandwidth(struct tb_tunnel *tunnel,
985 int *consumed_up, int *consumed_down)
987 int pcie_enabled = tb_acpi_may_tunnel_pcie();
990 * PCIe tunneling, if enabled, affects the USB3 bandwidth so
991 * take that it into account here.
993 *consumed_up = tunnel->allocated_up * (3 + pcie_enabled) / 3;
994 *consumed_down = tunnel->allocated_down * (3 + pcie_enabled) / 3;
998 static int tb_usb3_release_unused_bandwidth(struct tb_tunnel *tunnel)
1002 ret = usb4_usb3_port_release_bandwidth(tunnel->src_port,
1003 &tunnel->allocated_up,
1004 &tunnel->allocated_down);
1008 tb_tunnel_dbg(tunnel, "decreased bandwidth allocation to %d/%d Mb/s\n",
1009 tunnel->allocated_up, tunnel->allocated_down);
1013 static void tb_usb3_reclaim_available_bandwidth(struct tb_tunnel *tunnel,
1015 int *available_down)
1017 int ret, max_rate, allocate_up, allocate_down;
1019 ret = usb4_usb3_port_actual_link_rate(tunnel->src_port);
1021 tb_tunnel_warn(tunnel, "failed to read actual link rate\n");
1024 /* Use maximum link rate if the link valid is not set */
1025 ret = usb4_usb3_port_max_link_rate(tunnel->src_port);
1027 tb_tunnel_warn(tunnel, "failed to read maximum link rate\n");
1033 * 90% of the max rate can be allocated for isochronous
1036 max_rate = ret * 90 / 100;
1038 /* No need to reclaim if already at maximum */
1039 if (tunnel->allocated_up >= max_rate &&
1040 tunnel->allocated_down >= max_rate)
1043 /* Don't go lower than what is already allocated */
1044 allocate_up = min(max_rate, *available_up);
1045 if (allocate_up < tunnel->allocated_up)
1046 allocate_up = tunnel->allocated_up;
1048 allocate_down = min(max_rate, *available_down);
1049 if (allocate_down < tunnel->allocated_down)
1050 allocate_down = tunnel->allocated_down;
1052 /* If no changes no need to do more */
1053 if (allocate_up == tunnel->allocated_up &&
1054 allocate_down == tunnel->allocated_down)
1057 ret = usb4_usb3_port_allocate_bandwidth(tunnel->src_port, &allocate_up,
1060 tb_tunnel_info(tunnel, "failed to allocate bandwidth\n");
1064 tunnel->allocated_up = allocate_up;
1065 *available_up -= tunnel->allocated_up;
1067 tunnel->allocated_down = allocate_down;
1068 *available_down -= tunnel->allocated_down;
1070 tb_tunnel_dbg(tunnel, "increased bandwidth allocation to %d/%d Mb/s\n",
1071 tunnel->allocated_up, tunnel->allocated_down);
1074 static void tb_usb3_init_path(struct tb_path *path)
1076 path->egress_fc_enable = TB_PATH_SOURCE | TB_PATH_INTERNAL;
1077 path->egress_shared_buffer = TB_PATH_NONE;
1078 path->ingress_fc_enable = TB_PATH_ALL;
1079 path->ingress_shared_buffer = TB_PATH_NONE;
1082 path->drop_packages = 0;
1083 path->hops[0].initial_credits = 7;
1084 if (path->path_length > 1)
1085 path->hops[1].initial_credits =
1086 tb_initial_credits(path->hops[1].in_port->sw);
1090 * tb_tunnel_discover_usb3() - Discover existing USB3 tunnels
1091 * @tb: Pointer to the domain structure
1092 * @down: USB3 downstream adapter
1094 * If @down adapter is active, follows the tunnel to the USB3 upstream
1095 * adapter and back. Returns the discovered tunnel or %NULL if there was
1098 struct tb_tunnel *tb_tunnel_discover_usb3(struct tb *tb, struct tb_port *down)
1100 struct tb_tunnel *tunnel;
1101 struct tb_path *path;
1103 if (!tb_usb3_port_is_enabled(down))
1106 tunnel = tb_tunnel_alloc(tb, 2, TB_TUNNEL_USB3);
1110 tunnel->activate = tb_usb3_activate;
1111 tunnel->src_port = down;
1114 * Discover both paths even if they are not complete. We will
1115 * clean them up by calling tb_tunnel_deactivate() below in that
1118 path = tb_path_discover(down, TB_USB3_HOPID, NULL, -1,
1119 &tunnel->dst_port, "USB3 Down");
1121 /* Just disable the downstream port */
1122 tb_usb3_port_enable(down, false);
1125 tunnel->paths[TB_USB3_PATH_DOWN] = path;
1126 tb_usb3_init_path(tunnel->paths[TB_USB3_PATH_DOWN]);
1128 path = tb_path_discover(tunnel->dst_port, -1, down, TB_USB3_HOPID, NULL,
1131 goto err_deactivate;
1132 tunnel->paths[TB_USB3_PATH_UP] = path;
1133 tb_usb3_init_path(tunnel->paths[TB_USB3_PATH_UP]);
1135 /* Validate that the tunnel is complete */
1136 if (!tb_port_is_usb3_up(tunnel->dst_port)) {
1137 tb_port_warn(tunnel->dst_port,
1138 "path does not end on an USB3 adapter, cleaning up\n");
1139 goto err_deactivate;
1142 if (down != tunnel->src_port) {
1143 tb_tunnel_warn(tunnel, "path is not complete, cleaning up\n");
1144 goto err_deactivate;
1147 if (!tb_usb3_port_is_enabled(tunnel->dst_port)) {
1148 tb_tunnel_warn(tunnel,
1149 "tunnel is not fully activated, cleaning up\n");
1150 goto err_deactivate;
1153 if (!tb_route(down->sw)) {
1157 * Read the initial bandwidth allocation for the first
1160 ret = usb4_usb3_port_allocated_bandwidth(down,
1161 &tunnel->allocated_up, &tunnel->allocated_down);
1163 goto err_deactivate;
1165 tb_tunnel_dbg(tunnel, "currently allocated bandwidth %d/%d Mb/s\n",
1166 tunnel->allocated_up, tunnel->allocated_down);
1168 tunnel->init = tb_usb3_init;
1169 tunnel->consumed_bandwidth = tb_usb3_consumed_bandwidth;
1170 tunnel->release_unused_bandwidth =
1171 tb_usb3_release_unused_bandwidth;
1172 tunnel->reclaim_available_bandwidth =
1173 tb_usb3_reclaim_available_bandwidth;
1176 tb_tunnel_dbg(tunnel, "discovered\n");
1180 tb_tunnel_deactivate(tunnel);
1182 tb_tunnel_free(tunnel);
1188 * tb_tunnel_alloc_usb3() - allocate a USB3 tunnel
1189 * @tb: Pointer to the domain structure
1190 * @up: USB3 upstream adapter port
1191 * @down: USB3 downstream adapter port
1192 * @max_up: Maximum available upstream bandwidth for the USB3 tunnel (%0
1194 * @max_down: Maximum available downstream bandwidth for the USB3 tunnel
1195 * (%0 if not limited).
1197 * Allocate an USB3 tunnel. The ports must be of type @TB_TYPE_USB3_UP and
1198 * @TB_TYPE_USB3_DOWN.
1200 * Return: Returns a tb_tunnel on success or %NULL on failure.
1202 struct tb_tunnel *tb_tunnel_alloc_usb3(struct tb *tb, struct tb_port *up,
1203 struct tb_port *down, int max_up,
1206 struct tb_tunnel *tunnel;
1207 struct tb_path *path;
1211 * Check that we have enough bandwidth available for the new
1214 if (max_up > 0 || max_down > 0) {
1215 max_rate = tb_usb3_max_link_rate(down, up);
1219 /* Only 90% can be allocated for USB3 isochronous transfers */
1220 max_rate = max_rate * 90 / 100;
1221 tb_port_dbg(up, "required bandwidth for USB3 tunnel %d Mb/s\n",
1224 if (max_rate > max_up || max_rate > max_down) {
1225 tb_port_warn(up, "not enough bandwidth for USB3 tunnel\n");
1230 tunnel = tb_tunnel_alloc(tb, 2, TB_TUNNEL_USB3);
1234 tunnel->activate = tb_usb3_activate;
1235 tunnel->src_port = down;
1236 tunnel->dst_port = up;
1237 tunnel->max_up = max_up;
1238 tunnel->max_down = max_down;
1240 path = tb_path_alloc(tb, down, TB_USB3_HOPID, up, TB_USB3_HOPID, 0,
1243 tb_tunnel_free(tunnel);
1246 tb_usb3_init_path(path);
1247 tunnel->paths[TB_USB3_PATH_DOWN] = path;
1249 path = tb_path_alloc(tb, up, TB_USB3_HOPID, down, TB_USB3_HOPID, 0,
1252 tb_tunnel_free(tunnel);
1255 tb_usb3_init_path(path);
1256 tunnel->paths[TB_USB3_PATH_UP] = path;
1258 if (!tb_route(down->sw)) {
1259 tunnel->allocated_up = max_rate;
1260 tunnel->allocated_down = max_rate;
1262 tunnel->init = tb_usb3_init;
1263 tunnel->consumed_bandwidth = tb_usb3_consumed_bandwidth;
1264 tunnel->release_unused_bandwidth =
1265 tb_usb3_release_unused_bandwidth;
1266 tunnel->reclaim_available_bandwidth =
1267 tb_usb3_reclaim_available_bandwidth;
1274 * tb_tunnel_free() - free a tunnel
1275 * @tunnel: Tunnel to be freed
1277 * Frees a tunnel. The tunnel does not need to be deactivated.
1279 void tb_tunnel_free(struct tb_tunnel *tunnel)
1286 for (i = 0; i < tunnel->npaths; i++) {
1287 if (tunnel->paths[i])
1288 tb_path_free(tunnel->paths[i]);
1291 kfree(tunnel->paths);
1296 * tb_tunnel_is_invalid - check whether an activated path is still valid
1297 * @tunnel: Tunnel to check
1299 bool tb_tunnel_is_invalid(struct tb_tunnel *tunnel)
1303 for (i = 0; i < tunnel->npaths; i++) {
1304 WARN_ON(!tunnel->paths[i]->activated);
1305 if (tb_path_is_invalid(tunnel->paths[i]))
1313 * tb_tunnel_restart() - activate a tunnel after a hardware reset
1314 * @tunnel: Tunnel to restart
1316 * Return: 0 on success and negative errno in case if failure
1318 int tb_tunnel_restart(struct tb_tunnel *tunnel)
1322 tb_tunnel_dbg(tunnel, "activating\n");
1325 * Make sure all paths are properly disabled before enabling
1328 for (i = 0; i < tunnel->npaths; i++) {
1329 if (tunnel->paths[i]->activated) {
1330 tb_path_deactivate(tunnel->paths[i]);
1331 tunnel->paths[i]->activated = false;
1336 res = tunnel->init(tunnel);
1341 for (i = 0; i < tunnel->npaths; i++) {
1342 res = tb_path_activate(tunnel->paths[i]);
1347 if (tunnel->activate) {
1348 res = tunnel->activate(tunnel, true);
1356 tb_tunnel_warn(tunnel, "activation failed\n");
1357 tb_tunnel_deactivate(tunnel);
1362 * tb_tunnel_activate() - activate a tunnel
1363 * @tunnel: Tunnel to activate
1365 * Return: Returns 0 on success or an error code on failure.
1367 int tb_tunnel_activate(struct tb_tunnel *tunnel)
1371 for (i = 0; i < tunnel->npaths; i++) {
1372 if (tunnel->paths[i]->activated) {
1373 tb_tunnel_WARN(tunnel,
1374 "trying to activate an already activated tunnel\n");
1379 return tb_tunnel_restart(tunnel);
1383 * tb_tunnel_deactivate() - deactivate a tunnel
1384 * @tunnel: Tunnel to deactivate
1386 void tb_tunnel_deactivate(struct tb_tunnel *tunnel)
1390 tb_tunnel_dbg(tunnel, "deactivating\n");
1392 if (tunnel->activate)
1393 tunnel->activate(tunnel, false);
1395 for (i = 0; i < tunnel->npaths; i++) {
1396 if (tunnel->paths[i] && tunnel->paths[i]->activated)
1397 tb_path_deactivate(tunnel->paths[i]);
1402 * tb_tunnel_port_on_path() - Does the tunnel go through port
1403 * @tunnel: Tunnel to check
1404 * @port: Port to check
1406 * Returns true if @tunnel goes through @port (direction does not matter),
1409 bool tb_tunnel_port_on_path(const struct tb_tunnel *tunnel,
1410 const struct tb_port *port)
1414 for (i = 0; i < tunnel->npaths; i++) {
1415 if (!tunnel->paths[i])
1418 if (tb_path_port_on_path(tunnel->paths[i], port))
1425 static bool tb_tunnel_is_active(const struct tb_tunnel *tunnel)
1429 for (i = 0; i < tunnel->npaths; i++) {
1430 if (!tunnel->paths[i])
1432 if (!tunnel->paths[i]->activated)
1440 * tb_tunnel_consumed_bandwidth() - Return bandwidth consumed by the tunnel
1441 * @tunnel: Tunnel to check
1442 * @consumed_up: Consumed bandwidth in Mb/s from @dst_port to @src_port.
1444 * @consumed_down: Consumed bandwidth in Mb/s from @src_port to @dst_port.
1447 * Stores the amount of isochronous bandwidth @tunnel consumes in
1448 * @consumed_up and @consumed_down. In case of success returns %0,
1449 * negative errno otherwise.
1451 int tb_tunnel_consumed_bandwidth(struct tb_tunnel *tunnel, int *consumed_up,
1454 int up_bw = 0, down_bw = 0;
1456 if (!tb_tunnel_is_active(tunnel))
1459 if (tunnel->consumed_bandwidth) {
1462 ret = tunnel->consumed_bandwidth(tunnel, &up_bw, &down_bw);
1466 tb_tunnel_dbg(tunnel, "consumed bandwidth %d/%d Mb/s\n", up_bw,
1472 *consumed_up = up_bw;
1474 *consumed_down = down_bw;
1480 * tb_tunnel_release_unused_bandwidth() - Release unused bandwidth
1481 * @tunnel: Tunnel whose unused bandwidth to release
1483 * If tunnel supports dynamic bandwidth management (USB3 tunnels at the
1484 * moment) this function makes it to release all the unused bandwidth.
1486 * Returns %0 in case of success and negative errno otherwise.
1488 int tb_tunnel_release_unused_bandwidth(struct tb_tunnel *tunnel)
1490 if (!tb_tunnel_is_active(tunnel))
1493 if (tunnel->release_unused_bandwidth) {
1496 ret = tunnel->release_unused_bandwidth(tunnel);
1505 * tb_tunnel_reclaim_available_bandwidth() - Reclaim available bandwidth
1506 * @tunnel: Tunnel reclaiming available bandwidth
1507 * @available_up: Available upstream bandwidth (in Mb/s)
1508 * @available_down: Available downstream bandwidth (in Mb/s)
1510 * Reclaims bandwidth from @available_up and @available_down and updates
1511 * the variables accordingly (e.g decreases both according to what was
1512 * reclaimed by the tunnel). If nothing was reclaimed the values are
1515 void tb_tunnel_reclaim_available_bandwidth(struct tb_tunnel *tunnel,
1517 int *available_down)
1519 if (!tb_tunnel_is_active(tunnel))
1522 if (tunnel->reclaim_available_bandwidth)
1523 tunnel->reclaim_available_bandwidth(tunnel, available_up,