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 #define TB_DMA_PATH_OUT 0
38 #define TB_DMA_PATH_IN 1
40 static const char * const tb_tunnel_names
[] = { "PCI", "DP", "DMA", "USB3" };
42 #define __TB_TUNNEL_PRINT(level, tunnel, fmt, arg...) \
44 struct tb_tunnel *__tunnel = (tunnel); \
45 level(__tunnel->tb, "%llx:%x <-> %llx:%x (%s): " fmt, \
46 tb_route(__tunnel->src_port->sw), \
47 __tunnel->src_port->port, \
48 tb_route(__tunnel->dst_port->sw), \
49 __tunnel->dst_port->port, \
50 tb_tunnel_names[__tunnel->type], \
54 #define tb_tunnel_WARN(tunnel, fmt, arg...) \
55 __TB_TUNNEL_PRINT(tb_WARN, tunnel, fmt, ##arg)
56 #define tb_tunnel_warn(tunnel, fmt, arg...) \
57 __TB_TUNNEL_PRINT(tb_warn, tunnel, fmt, ##arg)
58 #define tb_tunnel_info(tunnel, fmt, arg...) \
59 __TB_TUNNEL_PRINT(tb_info, tunnel, fmt, ##arg)
60 #define tb_tunnel_dbg(tunnel, fmt, arg...) \
61 __TB_TUNNEL_PRINT(tb_dbg, tunnel, fmt, ##arg)
63 static struct tb_tunnel
*tb_tunnel_alloc(struct tb
*tb
, size_t npaths
,
64 enum tb_tunnel_type type
)
66 struct tb_tunnel
*tunnel
;
68 tunnel
= kzalloc(sizeof(*tunnel
), GFP_KERNEL
);
72 tunnel
->paths
= kcalloc(npaths
, sizeof(tunnel
->paths
[0]), GFP_KERNEL
);
74 tb_tunnel_free(tunnel
);
78 INIT_LIST_HEAD(&tunnel
->list
);
80 tunnel
->npaths
= npaths
;
86 static int tb_pci_activate(struct tb_tunnel
*tunnel
, bool activate
)
90 res
= tb_pci_port_enable(tunnel
->src_port
, activate
);
94 if (tb_port_is_pcie_up(tunnel
->dst_port
))
95 return tb_pci_port_enable(tunnel
->dst_port
, activate
);
100 static int tb_initial_credits(const struct tb_switch
*sw
)
102 /* If the path is complete sw is not NULL */
104 /* More credits for faster link */
105 switch (sw
->link_speed
* sw
->link_width
) {
116 static void tb_pci_init_path(struct tb_path
*path
)
118 path
->egress_fc_enable
= TB_PATH_SOURCE
| TB_PATH_INTERNAL
;
119 path
->egress_shared_buffer
= TB_PATH_NONE
;
120 path
->ingress_fc_enable
= TB_PATH_ALL
;
121 path
->ingress_shared_buffer
= TB_PATH_NONE
;
124 path
->drop_packages
= 0;
125 path
->nfc_credits
= 0;
126 path
->hops
[0].initial_credits
= 7;
127 path
->hops
[1].initial_credits
=
128 tb_initial_credits(path
->hops
[1].in_port
->sw
);
132 * tb_tunnel_discover_pci() - Discover existing PCIe tunnels
133 * @tb: Pointer to the domain structure
134 * @down: PCIe downstream adapter
136 * If @down adapter is active, follows the tunnel to the PCIe upstream
137 * adapter and back. Returns the discovered tunnel or %NULL if there was
140 struct tb_tunnel
*tb_tunnel_discover_pci(struct tb
*tb
, struct tb_port
*down
)
142 struct tb_tunnel
*tunnel
;
143 struct tb_path
*path
;
145 if (!tb_pci_port_is_enabled(down
))
148 tunnel
= tb_tunnel_alloc(tb
, 2, TB_TUNNEL_PCI
);
152 tunnel
->activate
= tb_pci_activate
;
153 tunnel
->src_port
= down
;
156 * Discover both paths even if they are not complete. We will
157 * clean them up by calling tb_tunnel_deactivate() below in that
160 path
= tb_path_discover(down
, TB_PCI_HOPID
, NULL
, -1,
161 &tunnel
->dst_port
, "PCIe Up");
163 /* Just disable the downstream port */
164 tb_pci_port_enable(down
, false);
167 tunnel
->paths
[TB_PCI_PATH_UP
] = path
;
168 tb_pci_init_path(tunnel
->paths
[TB_PCI_PATH_UP
]);
170 path
= tb_path_discover(tunnel
->dst_port
, -1, down
, TB_PCI_HOPID
, NULL
,
174 tunnel
->paths
[TB_PCI_PATH_DOWN
] = path
;
175 tb_pci_init_path(tunnel
->paths
[TB_PCI_PATH_DOWN
]);
177 /* Validate that the tunnel is complete */
178 if (!tb_port_is_pcie_up(tunnel
->dst_port
)) {
179 tb_port_warn(tunnel
->dst_port
,
180 "path does not end on a PCIe adapter, cleaning up\n");
184 if (down
!= tunnel
->src_port
) {
185 tb_tunnel_warn(tunnel
, "path is not complete, cleaning up\n");
189 if (!tb_pci_port_is_enabled(tunnel
->dst_port
)) {
190 tb_tunnel_warn(tunnel
,
191 "tunnel is not fully activated, cleaning up\n");
195 tb_tunnel_dbg(tunnel
, "discovered\n");
199 tb_tunnel_deactivate(tunnel
);
201 tb_tunnel_free(tunnel
);
207 * tb_tunnel_alloc_pci() - allocate a pci tunnel
208 * @tb: Pointer to the domain structure
209 * @up: PCIe upstream adapter port
210 * @down: PCIe downstream adapter port
212 * Allocate a PCI tunnel. The ports must be of type TB_TYPE_PCIE_UP and
215 * Return: Returns a tb_tunnel on success or NULL on failure.
217 struct tb_tunnel
*tb_tunnel_alloc_pci(struct tb
*tb
, struct tb_port
*up
,
218 struct tb_port
*down
)
220 struct tb_tunnel
*tunnel
;
221 struct tb_path
*path
;
223 tunnel
= tb_tunnel_alloc(tb
, 2, TB_TUNNEL_PCI
);
227 tunnel
->activate
= tb_pci_activate
;
228 tunnel
->src_port
= down
;
229 tunnel
->dst_port
= up
;
231 path
= tb_path_alloc(tb
, down
, TB_PCI_HOPID
, up
, TB_PCI_HOPID
, 0,
234 tb_tunnel_free(tunnel
);
237 tb_pci_init_path(path
);
238 tunnel
->paths
[TB_PCI_PATH_DOWN
] = path
;
240 path
= tb_path_alloc(tb
, up
, TB_PCI_HOPID
, down
, TB_PCI_HOPID
, 0,
243 tb_tunnel_free(tunnel
);
246 tb_pci_init_path(path
);
247 tunnel
->paths
[TB_PCI_PATH_UP
] = path
;
252 static bool tb_dp_is_usb4(const struct tb_switch
*sw
)
254 /* Titan Ridge DP adapters need the same treatment as USB4 */
255 return tb_switch_is_usb4(sw
) || tb_switch_is_titan_ridge(sw
);
258 static int tb_dp_cm_handshake(struct tb_port
*in
, struct tb_port
*out
)
264 /* Both ends need to support this */
265 if (!tb_dp_is_usb4(in
->sw
) || !tb_dp_is_usb4(out
->sw
))
268 ret
= tb_port_read(out
, &val
, TB_CFG_PORT
,
269 out
->cap_adap
+ DP_STATUS_CTRL
, 1);
273 val
|= DP_STATUS_CTRL_UF
| DP_STATUS_CTRL_CMHS
;
275 ret
= tb_port_write(out
, &val
, TB_CFG_PORT
,
276 out
->cap_adap
+ DP_STATUS_CTRL
, 1);
281 ret
= tb_port_read(out
, &val
, TB_CFG_PORT
,
282 out
->cap_adap
+ DP_STATUS_CTRL
, 1);
285 if (!(val
& DP_STATUS_CTRL_CMHS
))
287 usleep_range(10, 100);
293 static inline u32
tb_dp_cap_get_rate(u32 val
)
295 u32 rate
= (val
& DP_COMMON_CAP_RATE_MASK
) >> DP_COMMON_CAP_RATE_SHIFT
;
298 case DP_COMMON_CAP_RATE_RBR
:
300 case DP_COMMON_CAP_RATE_HBR
:
302 case DP_COMMON_CAP_RATE_HBR2
:
304 case DP_COMMON_CAP_RATE_HBR3
:
311 static inline u32
tb_dp_cap_set_rate(u32 val
, u32 rate
)
313 val
&= ~DP_COMMON_CAP_RATE_MASK
;
316 WARN(1, "invalid rate %u passed, defaulting to 1620 MB/s\n", rate
);
319 val
|= DP_COMMON_CAP_RATE_RBR
<< DP_COMMON_CAP_RATE_SHIFT
;
322 val
|= DP_COMMON_CAP_RATE_HBR
<< DP_COMMON_CAP_RATE_SHIFT
;
325 val
|= DP_COMMON_CAP_RATE_HBR2
<< DP_COMMON_CAP_RATE_SHIFT
;
328 val
|= DP_COMMON_CAP_RATE_HBR3
<< DP_COMMON_CAP_RATE_SHIFT
;
334 static inline u32
tb_dp_cap_get_lanes(u32 val
)
336 u32 lanes
= (val
& DP_COMMON_CAP_LANES_MASK
) >> DP_COMMON_CAP_LANES_SHIFT
;
339 case DP_COMMON_CAP_1_LANE
:
341 case DP_COMMON_CAP_2_LANES
:
343 case DP_COMMON_CAP_4_LANES
:
350 static inline u32
tb_dp_cap_set_lanes(u32 val
, u32 lanes
)
352 val
&= ~DP_COMMON_CAP_LANES_MASK
;
355 WARN(1, "invalid number of lanes %u passed, defaulting to 1\n",
359 val
|= DP_COMMON_CAP_1_LANE
<< DP_COMMON_CAP_LANES_SHIFT
;
362 val
|= DP_COMMON_CAP_2_LANES
<< DP_COMMON_CAP_LANES_SHIFT
;
365 val
|= DP_COMMON_CAP_4_LANES
<< DP_COMMON_CAP_LANES_SHIFT
;
371 static unsigned int tb_dp_bandwidth(unsigned int rate
, unsigned int lanes
)
373 /* Tunneling removes the DP 8b/10b encoding */
374 return rate
* lanes
* 8 / 10;
377 static int tb_dp_reduce_bandwidth(int max_bw
, u32 in_rate
, u32 in_lanes
,
378 u32 out_rate
, u32 out_lanes
, u32
*new_rate
,
381 static const u32 dp_bw
[][2] = {
383 { 8100, 4 }, /* 25920 Mb/s */
384 { 5400, 4 }, /* 17280 Mb/s */
385 { 8100, 2 }, /* 12960 Mb/s */
386 { 2700, 4 }, /* 8640 Mb/s */
387 { 5400, 2 }, /* 8640 Mb/s */
388 { 8100, 1 }, /* 6480 Mb/s */
389 { 1620, 4 }, /* 5184 Mb/s */
390 { 5400, 1 }, /* 4320 Mb/s */
391 { 2700, 2 }, /* 4320 Mb/s */
392 { 1620, 2 }, /* 2592 Mb/s */
393 { 2700, 1 }, /* 2160 Mb/s */
394 { 1620, 1 }, /* 1296 Mb/s */
399 * Find a combination that can fit into max_bw and does not
400 * exceed the maximum rate and lanes supported by the DP OUT and
403 for (i
= 0; i
< ARRAY_SIZE(dp_bw
); i
++) {
404 if (dp_bw
[i
][0] > out_rate
|| dp_bw
[i
][1] > out_lanes
)
407 if (dp_bw
[i
][0] > in_rate
|| dp_bw
[i
][1] > in_lanes
)
410 if (tb_dp_bandwidth(dp_bw
[i
][0], dp_bw
[i
][1]) <= max_bw
) {
411 *new_rate
= dp_bw
[i
][0];
412 *new_lanes
= dp_bw
[i
][1];
420 static int tb_dp_xchg_caps(struct tb_tunnel
*tunnel
)
422 u32 out_dp_cap
, out_rate
, out_lanes
, in_dp_cap
, in_rate
, in_lanes
, bw
;
423 struct tb_port
*out
= tunnel
->dst_port
;
424 struct tb_port
*in
= tunnel
->src_port
;
428 * Copy DP_LOCAL_CAP register to DP_REMOTE_CAP register for
429 * newer generation hardware.
431 if (in
->sw
->generation
< 2 || out
->sw
->generation
< 2)
435 * Perform connection manager handshake between IN and OUT ports
436 * before capabilities exchange can take place.
438 ret
= tb_dp_cm_handshake(in
, out
);
442 /* Read both DP_LOCAL_CAP registers */
443 ret
= tb_port_read(in
, &in_dp_cap
, TB_CFG_PORT
,
444 in
->cap_adap
+ DP_LOCAL_CAP
, 1);
448 ret
= tb_port_read(out
, &out_dp_cap
, TB_CFG_PORT
,
449 out
->cap_adap
+ DP_LOCAL_CAP
, 1);
453 /* Write IN local caps to OUT remote caps */
454 ret
= tb_port_write(out
, &in_dp_cap
, TB_CFG_PORT
,
455 out
->cap_adap
+ DP_REMOTE_CAP
, 1);
459 in_rate
= tb_dp_cap_get_rate(in_dp_cap
);
460 in_lanes
= tb_dp_cap_get_lanes(in_dp_cap
);
461 tb_port_dbg(in
, "maximum supported bandwidth %u Mb/s x%u = %u Mb/s\n",
462 in_rate
, in_lanes
, tb_dp_bandwidth(in_rate
, in_lanes
));
465 * If the tunnel bandwidth is limited (max_bw is set) then see
466 * if we need to reduce bandwidth to fit there.
468 out_rate
= tb_dp_cap_get_rate(out_dp_cap
);
469 out_lanes
= tb_dp_cap_get_lanes(out_dp_cap
);
470 bw
= tb_dp_bandwidth(out_rate
, out_lanes
);
471 tb_port_dbg(out
, "maximum supported bandwidth %u Mb/s x%u = %u Mb/s\n",
472 out_rate
, out_lanes
, bw
);
474 if (tunnel
->max_bw
&& bw
> tunnel
->max_bw
) {
475 u32 new_rate
, new_lanes
, new_bw
;
477 ret
= tb_dp_reduce_bandwidth(tunnel
->max_bw
, in_rate
, in_lanes
,
478 out_rate
, out_lanes
, &new_rate
,
481 tb_port_info(out
, "not enough bandwidth for DP tunnel\n");
485 new_bw
= tb_dp_bandwidth(new_rate
, new_lanes
);
486 tb_port_dbg(out
, "bandwidth reduced to %u Mb/s x%u = %u Mb/s\n",
487 new_rate
, new_lanes
, new_bw
);
490 * Set new rate and number of lanes before writing it to
491 * the IN port remote caps.
493 out_dp_cap
= tb_dp_cap_set_rate(out_dp_cap
, new_rate
);
494 out_dp_cap
= tb_dp_cap_set_lanes(out_dp_cap
, new_lanes
);
497 return tb_port_write(in
, &out_dp_cap
, TB_CFG_PORT
,
498 in
->cap_adap
+ DP_REMOTE_CAP
, 1);
501 static int tb_dp_activate(struct tb_tunnel
*tunnel
, bool active
)
506 struct tb_path
**paths
;
509 paths
= tunnel
->paths
;
510 last
= paths
[TB_DP_VIDEO_PATH_OUT
]->path_length
- 1;
512 tb_dp_port_set_hops(tunnel
->src_port
,
513 paths
[TB_DP_VIDEO_PATH_OUT
]->hops
[0].in_hop_index
,
514 paths
[TB_DP_AUX_PATH_OUT
]->hops
[0].in_hop_index
,
515 paths
[TB_DP_AUX_PATH_IN
]->hops
[last
].next_hop_index
);
517 tb_dp_port_set_hops(tunnel
->dst_port
,
518 paths
[TB_DP_VIDEO_PATH_OUT
]->hops
[last
].next_hop_index
,
519 paths
[TB_DP_AUX_PATH_IN
]->hops
[0].in_hop_index
,
520 paths
[TB_DP_AUX_PATH_OUT
]->hops
[last
].next_hop_index
);
522 tb_dp_port_hpd_clear(tunnel
->src_port
);
523 tb_dp_port_set_hops(tunnel
->src_port
, 0, 0, 0);
524 if (tb_port_is_dpout(tunnel
->dst_port
))
525 tb_dp_port_set_hops(tunnel
->dst_port
, 0, 0, 0);
528 ret
= tb_dp_port_enable(tunnel
->src_port
, active
);
532 if (tb_port_is_dpout(tunnel
->dst_port
))
533 return tb_dp_port_enable(tunnel
->dst_port
, active
);
538 static int tb_dp_consumed_bandwidth(struct tb_tunnel
*tunnel
)
540 struct tb_port
*in
= tunnel
->src_port
;
541 const struct tb_switch
*sw
= in
->sw
;
542 u32 val
, rate
= 0, lanes
= 0;
545 if (tb_dp_is_usb4(sw
)) {
549 * Wait for DPRX done. Normally it should be already set
553 ret
= tb_port_read(in
, &val
, TB_CFG_PORT
,
554 in
->cap_adap
+ DP_COMMON_CAP
, 1);
558 if (val
& DP_COMMON_CAP_DPRX_DONE
) {
559 rate
= tb_dp_cap_get_rate(val
);
560 lanes
= tb_dp_cap_get_lanes(val
);
568 } else if (sw
->generation
>= 2) {
570 * Read from the copied remote cap so that we take into
571 * account if capabilities were reduced during exchange.
573 ret
= tb_port_read(in
, &val
, TB_CFG_PORT
,
574 in
->cap_adap
+ DP_REMOTE_CAP
, 1);
578 rate
= tb_dp_cap_get_rate(val
);
579 lanes
= tb_dp_cap_get_lanes(val
);
581 /* No bandwidth management for legacy devices */
585 return tb_dp_bandwidth(rate
, lanes
);
588 static void tb_dp_init_aux_path(struct tb_path
*path
)
592 path
->egress_fc_enable
= TB_PATH_SOURCE
| TB_PATH_INTERNAL
;
593 path
->egress_shared_buffer
= TB_PATH_NONE
;
594 path
->ingress_fc_enable
= TB_PATH_ALL
;
595 path
->ingress_shared_buffer
= TB_PATH_NONE
;
599 for (i
= 0; i
< path
->path_length
; i
++)
600 path
->hops
[i
].initial_credits
= 1;
603 static void tb_dp_init_video_path(struct tb_path
*path
, bool discover
)
605 u32 nfc_credits
= path
->hops
[0].in_port
->config
.nfc_credits
;
607 path
->egress_fc_enable
= TB_PATH_NONE
;
608 path
->egress_shared_buffer
= TB_PATH_NONE
;
609 path
->ingress_fc_enable
= TB_PATH_NONE
;
610 path
->ingress_shared_buffer
= TB_PATH_NONE
;
615 path
->nfc_credits
= nfc_credits
& ADP_CS_4_NFC_BUFFERS_MASK
;
619 max_credits
= (nfc_credits
& ADP_CS_4_TOTAL_BUFFERS_MASK
) >>
620 ADP_CS_4_TOTAL_BUFFERS_SHIFT
;
621 /* Leave some credits for AUX path */
622 path
->nfc_credits
= min(max_credits
- 2, 12U);
627 * tb_tunnel_discover_dp() - Discover existing Display Port tunnels
628 * @tb: Pointer to the domain structure
631 * If @in adapter is active, follows the tunnel to the DP out adapter
632 * and back. Returns the discovered tunnel or %NULL if there was no
635 * Return: DP tunnel or %NULL if no tunnel found.
637 struct tb_tunnel
*tb_tunnel_discover_dp(struct tb
*tb
, struct tb_port
*in
)
639 struct tb_tunnel
*tunnel
;
640 struct tb_port
*port
;
641 struct tb_path
*path
;
643 if (!tb_dp_port_is_enabled(in
))
646 tunnel
= tb_tunnel_alloc(tb
, 3, TB_TUNNEL_DP
);
650 tunnel
->init
= tb_dp_xchg_caps
;
651 tunnel
->activate
= tb_dp_activate
;
652 tunnel
->consumed_bandwidth
= tb_dp_consumed_bandwidth
;
653 tunnel
->src_port
= in
;
655 path
= tb_path_discover(in
, TB_DP_VIDEO_HOPID
, NULL
, -1,
656 &tunnel
->dst_port
, "Video");
658 /* Just disable the DP IN port */
659 tb_dp_port_enable(in
, false);
662 tunnel
->paths
[TB_DP_VIDEO_PATH_OUT
] = path
;
663 tb_dp_init_video_path(tunnel
->paths
[TB_DP_VIDEO_PATH_OUT
], true);
665 path
= tb_path_discover(in
, TB_DP_AUX_TX_HOPID
, NULL
, -1, NULL
, "AUX TX");
668 tunnel
->paths
[TB_DP_AUX_PATH_OUT
] = path
;
669 tb_dp_init_aux_path(tunnel
->paths
[TB_DP_AUX_PATH_OUT
]);
671 path
= tb_path_discover(tunnel
->dst_port
, -1, in
, TB_DP_AUX_RX_HOPID
,
675 tunnel
->paths
[TB_DP_AUX_PATH_IN
] = path
;
676 tb_dp_init_aux_path(tunnel
->paths
[TB_DP_AUX_PATH_IN
]);
678 /* Validate that the tunnel is complete */
679 if (!tb_port_is_dpout(tunnel
->dst_port
)) {
680 tb_port_warn(in
, "path does not end on a DP adapter, cleaning up\n");
684 if (!tb_dp_port_is_enabled(tunnel
->dst_port
))
687 if (!tb_dp_port_hpd_is_active(tunnel
->dst_port
))
690 if (port
!= tunnel
->src_port
) {
691 tb_tunnel_warn(tunnel
, "path is not complete, cleaning up\n");
695 tb_tunnel_dbg(tunnel
, "discovered\n");
699 tb_tunnel_deactivate(tunnel
);
701 tb_tunnel_free(tunnel
);
707 * tb_tunnel_alloc_dp() - allocate a Display Port tunnel
708 * @tb: Pointer to the domain structure
709 * @in: DP in adapter port
710 * @out: DP out adapter port
711 * @max_bw: Maximum available bandwidth for the DP tunnel (%0 if not limited)
713 * Allocates a tunnel between @in and @out that is capable of tunneling
714 * Display Port traffic.
716 * Return: Returns a tb_tunnel on success or NULL on failure.
718 struct tb_tunnel
*tb_tunnel_alloc_dp(struct tb
*tb
, struct tb_port
*in
,
719 struct tb_port
*out
, int max_bw
)
721 struct tb_tunnel
*tunnel
;
722 struct tb_path
**paths
;
723 struct tb_path
*path
;
725 if (WARN_ON(!in
->cap_adap
|| !out
->cap_adap
))
728 tunnel
= tb_tunnel_alloc(tb
, 3, TB_TUNNEL_DP
);
732 tunnel
->init
= tb_dp_xchg_caps
;
733 tunnel
->activate
= tb_dp_activate
;
734 tunnel
->consumed_bandwidth
= tb_dp_consumed_bandwidth
;
735 tunnel
->src_port
= in
;
736 tunnel
->dst_port
= out
;
737 tunnel
->max_bw
= max_bw
;
739 paths
= tunnel
->paths
;
741 path
= tb_path_alloc(tb
, in
, TB_DP_VIDEO_HOPID
, out
, TB_DP_VIDEO_HOPID
,
745 tb_dp_init_video_path(path
, false);
746 paths
[TB_DP_VIDEO_PATH_OUT
] = path
;
748 path
= tb_path_alloc(tb
, in
, TB_DP_AUX_TX_HOPID
, out
,
749 TB_DP_AUX_TX_HOPID
, 1, "AUX TX");
752 tb_dp_init_aux_path(path
);
753 paths
[TB_DP_AUX_PATH_OUT
] = path
;
755 path
= tb_path_alloc(tb
, out
, TB_DP_AUX_RX_HOPID
, in
,
756 TB_DP_AUX_RX_HOPID
, 1, "AUX RX");
759 tb_dp_init_aux_path(path
);
760 paths
[TB_DP_AUX_PATH_IN
] = path
;
765 tb_tunnel_free(tunnel
);
769 static u32
tb_dma_credits(struct tb_port
*nhi
)
773 max_credits
= (nhi
->config
.nfc_credits
& ADP_CS_4_TOTAL_BUFFERS_MASK
) >>
774 ADP_CS_4_TOTAL_BUFFERS_SHIFT
;
775 return min(max_credits
, 13U);
778 static int tb_dma_activate(struct tb_tunnel
*tunnel
, bool active
)
780 struct tb_port
*nhi
= tunnel
->src_port
;
783 credits
= active
? tb_dma_credits(nhi
) : 0;
784 return tb_port_set_initial_credits(nhi
, credits
);
787 static void tb_dma_init_path(struct tb_path
*path
, unsigned int isb
,
788 unsigned int efc
, u32 credits
)
792 path
->egress_fc_enable
= efc
;
793 path
->ingress_fc_enable
= TB_PATH_ALL
;
794 path
->egress_shared_buffer
= TB_PATH_NONE
;
795 path
->ingress_shared_buffer
= isb
;
798 path
->clear_fc
= true;
800 for (i
= 0; i
< path
->path_length
; i
++)
801 path
->hops
[i
].initial_credits
= credits
;
805 * tb_tunnel_alloc_dma() - allocate a DMA tunnel
806 * @tb: Pointer to the domain structure
807 * @nhi: Host controller port
808 * @dst: Destination null port which the other domain is connected to
809 * @transmit_ring: NHI ring number used to send packets towards the
811 * @transmit_path: HopID used for transmitting packets
812 * @receive_ring: NHI ring number used to receive packets from the
814 * @reveive_path: HopID used for receiving packets
816 * Return: Returns a tb_tunnel on success or NULL on failure.
818 struct tb_tunnel
*tb_tunnel_alloc_dma(struct tb
*tb
, struct tb_port
*nhi
,
819 struct tb_port
*dst
, int transmit_ring
,
820 int transmit_path
, int receive_ring
,
823 struct tb_tunnel
*tunnel
;
824 struct tb_path
*path
;
827 tunnel
= tb_tunnel_alloc(tb
, 2, TB_TUNNEL_DMA
);
831 tunnel
->activate
= tb_dma_activate
;
832 tunnel
->src_port
= nhi
;
833 tunnel
->dst_port
= dst
;
835 credits
= tb_dma_credits(nhi
);
837 path
= tb_path_alloc(tb
, dst
, receive_path
, nhi
, receive_ring
, 0, "DMA RX");
839 tb_tunnel_free(tunnel
);
842 tb_dma_init_path(path
, TB_PATH_NONE
, TB_PATH_SOURCE
| TB_PATH_INTERNAL
,
844 tunnel
->paths
[TB_DMA_PATH_IN
] = path
;
846 path
= tb_path_alloc(tb
, nhi
, transmit_ring
, dst
, transmit_path
, 0, "DMA TX");
848 tb_tunnel_free(tunnel
);
851 tb_dma_init_path(path
, TB_PATH_SOURCE
, TB_PATH_ALL
, credits
);
852 tunnel
->paths
[TB_DMA_PATH_OUT
] = path
;
857 static int tb_usb3_activate(struct tb_tunnel
*tunnel
, bool activate
)
861 res
= tb_usb3_port_enable(tunnel
->src_port
, activate
);
865 if (tb_port_is_usb3_up(tunnel
->dst_port
))
866 return tb_usb3_port_enable(tunnel
->dst_port
, activate
);
871 static void tb_usb3_init_path(struct tb_path
*path
)
873 path
->egress_fc_enable
= TB_PATH_SOURCE
| TB_PATH_INTERNAL
;
874 path
->egress_shared_buffer
= TB_PATH_NONE
;
875 path
->ingress_fc_enable
= TB_PATH_ALL
;
876 path
->ingress_shared_buffer
= TB_PATH_NONE
;
879 path
->drop_packages
= 0;
880 path
->nfc_credits
= 0;
881 path
->hops
[0].initial_credits
= 7;
882 path
->hops
[1].initial_credits
=
883 tb_initial_credits(path
->hops
[1].in_port
->sw
);
887 * tb_tunnel_discover_usb3() - Discover existing USB3 tunnels
888 * @tb: Pointer to the domain structure
889 * @down: USB3 downstream adapter
891 * If @down adapter is active, follows the tunnel to the USB3 upstream
892 * adapter and back. Returns the discovered tunnel or %NULL if there was
895 struct tb_tunnel
*tb_tunnel_discover_usb3(struct tb
*tb
, struct tb_port
*down
)
897 struct tb_tunnel
*tunnel
;
898 struct tb_path
*path
;
900 if (!tb_usb3_port_is_enabled(down
))
903 tunnel
= tb_tunnel_alloc(tb
, 2, TB_TUNNEL_USB3
);
907 tunnel
->activate
= tb_usb3_activate
;
908 tunnel
->src_port
= down
;
911 * Discover both paths even if they are not complete. We will
912 * clean them up by calling tb_tunnel_deactivate() below in that
915 path
= tb_path_discover(down
, TB_USB3_HOPID
, NULL
, -1,
916 &tunnel
->dst_port
, "USB3 Up");
918 /* Just disable the downstream port */
919 tb_usb3_port_enable(down
, false);
922 tunnel
->paths
[TB_USB3_PATH_UP
] = path
;
923 tb_usb3_init_path(tunnel
->paths
[TB_USB3_PATH_UP
]);
925 path
= tb_path_discover(tunnel
->dst_port
, -1, down
, TB_USB3_HOPID
, NULL
,
929 tunnel
->paths
[TB_USB3_PATH_DOWN
] = path
;
930 tb_usb3_init_path(tunnel
->paths
[TB_USB3_PATH_DOWN
]);
932 /* Validate that the tunnel is complete */
933 if (!tb_port_is_usb3_up(tunnel
->dst_port
)) {
934 tb_port_warn(tunnel
->dst_port
,
935 "path does not end on an USB3 adapter, cleaning up\n");
939 if (down
!= tunnel
->src_port
) {
940 tb_tunnel_warn(tunnel
, "path is not complete, cleaning up\n");
944 if (!tb_usb3_port_is_enabled(tunnel
->dst_port
)) {
945 tb_tunnel_warn(tunnel
,
946 "tunnel is not fully activated, cleaning up\n");
950 tb_tunnel_dbg(tunnel
, "discovered\n");
954 tb_tunnel_deactivate(tunnel
);
956 tb_tunnel_free(tunnel
);
962 * tb_tunnel_alloc_usb3() - allocate a USB3 tunnel
963 * @tb: Pointer to the domain structure
964 * @up: USB3 upstream adapter port
965 * @down: USB3 downstream adapter port
967 * Allocate an USB3 tunnel. The ports must be of type @TB_TYPE_USB3_UP and
968 * @TB_TYPE_USB3_DOWN.
970 * Return: Returns a tb_tunnel on success or %NULL on failure.
972 struct tb_tunnel
*tb_tunnel_alloc_usb3(struct tb
*tb
, struct tb_port
*up
,
973 struct tb_port
*down
)
975 struct tb_tunnel
*tunnel
;
976 struct tb_path
*path
;
978 tunnel
= tb_tunnel_alloc(tb
, 2, TB_TUNNEL_USB3
);
982 tunnel
->activate
= tb_usb3_activate
;
983 tunnel
->src_port
= down
;
984 tunnel
->dst_port
= up
;
986 path
= tb_path_alloc(tb
, down
, TB_USB3_HOPID
, up
, TB_USB3_HOPID
, 0,
989 tb_tunnel_free(tunnel
);
992 tb_usb3_init_path(path
);
993 tunnel
->paths
[TB_USB3_PATH_DOWN
] = path
;
995 path
= tb_path_alloc(tb
, up
, TB_USB3_HOPID
, down
, TB_USB3_HOPID
, 0,
998 tb_tunnel_free(tunnel
);
1001 tb_usb3_init_path(path
);
1002 tunnel
->paths
[TB_USB3_PATH_UP
] = path
;
1008 * tb_tunnel_free() - free a tunnel
1009 * @tunnel: Tunnel to be freed
1011 * Frees a tunnel. The tunnel does not need to be deactivated.
1013 void tb_tunnel_free(struct tb_tunnel
*tunnel
)
1020 for (i
= 0; i
< tunnel
->npaths
; i
++) {
1021 if (tunnel
->paths
[i
])
1022 tb_path_free(tunnel
->paths
[i
]);
1025 kfree(tunnel
->paths
);
1030 * tb_tunnel_is_invalid - check whether an activated path is still valid
1031 * @tunnel: Tunnel to check
1033 bool tb_tunnel_is_invalid(struct tb_tunnel
*tunnel
)
1037 for (i
= 0; i
< tunnel
->npaths
; i
++) {
1038 WARN_ON(!tunnel
->paths
[i
]->activated
);
1039 if (tb_path_is_invalid(tunnel
->paths
[i
]))
1047 * tb_tunnel_restart() - activate a tunnel after a hardware reset
1048 * @tunnel: Tunnel to restart
1050 * Return: 0 on success and negative errno in case if failure
1052 int tb_tunnel_restart(struct tb_tunnel
*tunnel
)
1056 tb_tunnel_dbg(tunnel
, "activating\n");
1059 * Make sure all paths are properly disabled before enabling
1062 for (i
= 0; i
< tunnel
->npaths
; i
++) {
1063 if (tunnel
->paths
[i
]->activated
) {
1064 tb_path_deactivate(tunnel
->paths
[i
]);
1065 tunnel
->paths
[i
]->activated
= false;
1070 res
= tunnel
->init(tunnel
);
1075 for (i
= 0; i
< tunnel
->npaths
; i
++) {
1076 res
= tb_path_activate(tunnel
->paths
[i
]);
1081 if (tunnel
->activate
) {
1082 res
= tunnel
->activate(tunnel
, true);
1090 tb_tunnel_warn(tunnel
, "activation failed\n");
1091 tb_tunnel_deactivate(tunnel
);
1096 * tb_tunnel_activate() - activate a tunnel
1097 * @tunnel: Tunnel to activate
1099 * Return: Returns 0 on success or an error code on failure.
1101 int tb_tunnel_activate(struct tb_tunnel
*tunnel
)
1105 for (i
= 0; i
< tunnel
->npaths
; i
++) {
1106 if (tunnel
->paths
[i
]->activated
) {
1107 tb_tunnel_WARN(tunnel
,
1108 "trying to activate an already activated tunnel\n");
1113 return tb_tunnel_restart(tunnel
);
1117 * tb_tunnel_deactivate() - deactivate a tunnel
1118 * @tunnel: Tunnel to deactivate
1120 void tb_tunnel_deactivate(struct tb_tunnel
*tunnel
)
1124 tb_tunnel_dbg(tunnel
, "deactivating\n");
1126 if (tunnel
->activate
)
1127 tunnel
->activate(tunnel
, false);
1129 for (i
= 0; i
< tunnel
->npaths
; i
++) {
1130 if (tunnel
->paths
[i
] && tunnel
->paths
[i
]->activated
)
1131 tb_path_deactivate(tunnel
->paths
[i
]);
1136 * tb_tunnel_switch_on_path() - Does the tunnel go through switch
1137 * @tunnel: Tunnel to check
1138 * @sw: Switch to check
1140 * Returns true if @tunnel goes through @sw (direction does not matter),
1143 bool tb_tunnel_switch_on_path(const struct tb_tunnel
*tunnel
,
1144 const struct tb_switch
*sw
)
1148 for (i
= 0; i
< tunnel
->npaths
; i
++) {
1149 if (!tunnel
->paths
[i
])
1151 if (tb_path_switch_on_path(tunnel
->paths
[i
], sw
))
1158 static bool tb_tunnel_is_active(const struct tb_tunnel
*tunnel
)
1162 for (i
= 0; i
< tunnel
->npaths
; i
++) {
1163 if (!tunnel
->paths
[i
])
1165 if (!tunnel
->paths
[i
]->activated
)
1173 * tb_tunnel_consumed_bandwidth() - Return bandwidth consumed by the tunnel
1174 * @tunnel: Tunnel to check
1176 * Returns bandwidth currently consumed by @tunnel and %0 if the @tunnel
1177 * is not active or does consume bandwidth.
1179 int tb_tunnel_consumed_bandwidth(struct tb_tunnel
*tunnel
)
1181 if (!tb_tunnel_is_active(tunnel
))
1184 if (tunnel
->consumed_bandwidth
) {
1185 int ret
= tunnel
->consumed_bandwidth(tunnel
);
1187 tb_tunnel_dbg(tunnel
, "consumed bandwidth %d Mb/s\n", ret
);