Loading...
1/* SPDX-License-Identifier: GPL-2.0 */
2/*
3 * Thunderbolt driver - Tunneling support
4 *
5 * Copyright (c) 2014 Andreas Noever <andreas.noever@gmail.com>
6 * Copyright (C) 2019, Intel Corporation
7 */
8
9#ifndef TB_TUNNEL_H_
10#define TB_TUNNEL_H_
11
12#include "tb.h"
13
14enum tb_tunnel_type {
15 TB_TUNNEL_PCI,
16 TB_TUNNEL_DP,
17 TB_TUNNEL_DMA,
18 TB_TUNNEL_USB3,
19};
20
21/**
22 * struct tb_tunnel - Tunnel between two ports
23 * @tb: Pointer to the domain
24 * @src_port: Source port of the tunnel
25 * @dst_port: Destination port of the tunnel. For discovered incomplete
26 * tunnels may be %NULL or null adapter port instead.
27 * @paths: All paths required by the tunnel
28 * @npaths: Number of paths in @paths
29 * @init: Optional tunnel specific initialization
30 * @activate: Optional tunnel specific activation/deactivation
31 * @consumed_bandwidth: Return how much bandwidth the tunnel consumes
32 * @release_unused_bandwidth: Release all unused bandwidth
33 * @reclaim_available_bandwidth: Reclaim back available bandwidth
34 * @list: Tunnels are linked using this field
35 * @type: Type of the tunnel
36 * @max_up: Maximum upstream bandwidth (Mb/s) available for the tunnel.
37 * Only set if the bandwidth needs to be limited.
38 * @max_down: Maximum downstream bandwidth (Mb/s) available for the tunnel.
39 * Only set if the bandwidth needs to be limited.
40 * @allocated_up: Allocated upstream bandwidth (only for USB3)
41 * @allocated_down: Allocated downstream bandwidth (only for USB3)
42 */
43struct tb_tunnel {
44 struct tb *tb;
45 struct tb_port *src_port;
46 struct tb_port *dst_port;
47 struct tb_path **paths;
48 size_t npaths;
49 int (*init)(struct tb_tunnel *tunnel);
50 int (*activate)(struct tb_tunnel *tunnel, bool activate);
51 int (*consumed_bandwidth)(struct tb_tunnel *tunnel, int *consumed_up,
52 int *consumed_down);
53 int (*release_unused_bandwidth)(struct tb_tunnel *tunnel);
54 void (*reclaim_available_bandwidth)(struct tb_tunnel *tunnel,
55 int *available_up,
56 int *available_down);
57 struct list_head list;
58 enum tb_tunnel_type type;
59 int max_up;
60 int max_down;
61 int allocated_up;
62 int allocated_down;
63};
64
65struct tb_tunnel *tb_tunnel_discover_pci(struct tb *tb, struct tb_port *down);
66struct tb_tunnel *tb_tunnel_alloc_pci(struct tb *tb, struct tb_port *up,
67 struct tb_port *down);
68struct tb_tunnel *tb_tunnel_discover_dp(struct tb *tb, struct tb_port *in);
69struct tb_tunnel *tb_tunnel_alloc_dp(struct tb *tb, struct tb_port *in,
70 struct tb_port *out, int max_up,
71 int max_down);
72struct tb_tunnel *tb_tunnel_alloc_dma(struct tb *tb, struct tb_port *nhi,
73 struct tb_port *dst, int transmit_ring,
74 int transmit_path, int receive_ring,
75 int receive_path);
76struct tb_tunnel *tb_tunnel_discover_usb3(struct tb *tb, struct tb_port *down);
77struct tb_tunnel *tb_tunnel_alloc_usb3(struct tb *tb, struct tb_port *up,
78 struct tb_port *down, int max_up,
79 int max_down);
80
81void tb_tunnel_free(struct tb_tunnel *tunnel);
82int tb_tunnel_activate(struct tb_tunnel *tunnel);
83int tb_tunnel_restart(struct tb_tunnel *tunnel);
84void tb_tunnel_deactivate(struct tb_tunnel *tunnel);
85bool tb_tunnel_is_invalid(struct tb_tunnel *tunnel);
86bool tb_tunnel_port_on_path(const struct tb_tunnel *tunnel,
87 const struct tb_port *port);
88int tb_tunnel_consumed_bandwidth(struct tb_tunnel *tunnel, int *consumed_up,
89 int *consumed_down);
90int tb_tunnel_release_unused_bandwidth(struct tb_tunnel *tunnel);
91void tb_tunnel_reclaim_available_bandwidth(struct tb_tunnel *tunnel,
92 int *available_up,
93 int *available_down);
94
95static inline bool tb_tunnel_is_pci(const struct tb_tunnel *tunnel)
96{
97 return tunnel->type == TB_TUNNEL_PCI;
98}
99
100static inline bool tb_tunnel_is_dp(const struct tb_tunnel *tunnel)
101{
102 return tunnel->type == TB_TUNNEL_DP;
103}
104
105static inline bool tb_tunnel_is_dma(const struct tb_tunnel *tunnel)
106{
107 return tunnel->type == TB_TUNNEL_DMA;
108}
109
110static inline bool tb_tunnel_is_usb3(const struct tb_tunnel *tunnel)
111{
112 return tunnel->type == TB_TUNNEL_USB3;
113}
114
115#endif
116
1/* SPDX-License-Identifier: GPL-2.0 */
2/*
3 * Thunderbolt driver - Tunneling support
4 *
5 * Copyright (c) 2014 Andreas Noever <andreas.noever@gmail.com>
6 * Copyright (C) 2019, Intel Corporation
7 */
8
9#ifndef TB_TUNNEL_H_
10#define TB_TUNNEL_H_
11
12#include "tb.h"
13
14enum tb_tunnel_type {
15 TB_TUNNEL_PCI,
16 TB_TUNNEL_DP,
17 TB_TUNNEL_DMA,
18 TB_TUNNEL_USB3,
19};
20
21/**
22 * struct tb_tunnel - Tunnel between two ports
23 * @tb: Pointer to the domain
24 * @src_port: Source port of the tunnel
25 * @dst_port: Destination port of the tunnel. For discovered incomplete
26 * tunnels may be %NULL or null adapter port instead.
27 * @paths: All paths required by the tunnel
28 * @npaths: Number of paths in @paths
29 * @init: Optional tunnel specific initialization
30 * @deinit: Optional tunnel specific de-initialization
31 * @activate: Optional tunnel specific activation/deactivation
32 * @maximum_bandwidth: Returns maximum possible bandwidth for this tunnel
33 * @allocated_bandwidth: Return how much bandwidth is allocated for the tunnel
34 * @alloc_bandwidth: Change tunnel bandwidth allocation
35 * @consumed_bandwidth: Return how much bandwidth the tunnel consumes
36 * @release_unused_bandwidth: Release all unused bandwidth
37 * @reclaim_available_bandwidth: Reclaim back available bandwidth
38 * @list: Tunnels are linked using this field
39 * @type: Type of the tunnel
40 * @max_up: Maximum upstream bandwidth (Mb/s) available for the tunnel.
41 * Only set if the bandwidth needs to be limited.
42 * @max_down: Maximum downstream bandwidth (Mb/s) available for the tunnel.
43 * Only set if the bandwidth needs to be limited.
44 * @allocated_up: Allocated upstream bandwidth (only for USB3)
45 * @allocated_down: Allocated downstream bandwidth (only for USB3)
46 * @bw_mode: DP bandwidth allocation mode registers can be used to
47 * determine consumed and allocated bandwidth
48 */
49struct tb_tunnel {
50 struct tb *tb;
51 struct tb_port *src_port;
52 struct tb_port *dst_port;
53 struct tb_path **paths;
54 size_t npaths;
55 int (*init)(struct tb_tunnel *tunnel);
56 void (*deinit)(struct tb_tunnel *tunnel);
57 int (*activate)(struct tb_tunnel *tunnel, bool activate);
58 int (*maximum_bandwidth)(struct tb_tunnel *tunnel, int *max_up,
59 int *max_down);
60 int (*allocated_bandwidth)(struct tb_tunnel *tunnel, int *allocated_up,
61 int *allocated_down);
62 int (*alloc_bandwidth)(struct tb_tunnel *tunnel, int *alloc_up,
63 int *alloc_down);
64 int (*consumed_bandwidth)(struct tb_tunnel *tunnel, int *consumed_up,
65 int *consumed_down);
66 int (*release_unused_bandwidth)(struct tb_tunnel *tunnel);
67 void (*reclaim_available_bandwidth)(struct tb_tunnel *tunnel,
68 int *available_up,
69 int *available_down);
70 struct list_head list;
71 enum tb_tunnel_type type;
72 int max_up;
73 int max_down;
74 int allocated_up;
75 int allocated_down;
76 bool bw_mode;
77};
78
79struct tb_tunnel *tb_tunnel_discover_pci(struct tb *tb, struct tb_port *down,
80 bool alloc_hopid);
81struct tb_tunnel *tb_tunnel_alloc_pci(struct tb *tb, struct tb_port *up,
82 struct tb_port *down);
83bool tb_tunnel_reserved_pci(struct tb_port *port, int *reserved_up,
84 int *reserved_down);
85struct tb_tunnel *tb_tunnel_discover_dp(struct tb *tb, struct tb_port *in,
86 bool alloc_hopid);
87struct tb_tunnel *tb_tunnel_alloc_dp(struct tb *tb, struct tb_port *in,
88 struct tb_port *out, int link_nr,
89 int max_up, int max_down);
90struct tb_tunnel *tb_tunnel_alloc_dma(struct tb *tb, struct tb_port *nhi,
91 struct tb_port *dst, int transmit_path,
92 int transmit_ring, int receive_path,
93 int receive_ring);
94bool tb_tunnel_match_dma(const struct tb_tunnel *tunnel, int transmit_path,
95 int transmit_ring, int receive_path, int receive_ring);
96struct tb_tunnel *tb_tunnel_discover_usb3(struct tb *tb, struct tb_port *down,
97 bool alloc_hopid);
98struct tb_tunnel *tb_tunnel_alloc_usb3(struct tb *tb, struct tb_port *up,
99 struct tb_port *down, int max_up,
100 int max_down);
101
102void tb_tunnel_free(struct tb_tunnel *tunnel);
103int tb_tunnel_activate(struct tb_tunnel *tunnel);
104int tb_tunnel_restart(struct tb_tunnel *tunnel);
105void tb_tunnel_deactivate(struct tb_tunnel *tunnel);
106bool tb_tunnel_is_invalid(struct tb_tunnel *tunnel);
107bool tb_tunnel_port_on_path(const struct tb_tunnel *tunnel,
108 const struct tb_port *port);
109int tb_tunnel_maximum_bandwidth(struct tb_tunnel *tunnel, int *max_up,
110 int *max_down);
111int tb_tunnel_allocated_bandwidth(struct tb_tunnel *tunnel, int *allocated_up,
112 int *allocated_down);
113int tb_tunnel_alloc_bandwidth(struct tb_tunnel *tunnel, int *alloc_up,
114 int *alloc_down);
115int tb_tunnel_consumed_bandwidth(struct tb_tunnel *tunnel, int *consumed_up,
116 int *consumed_down);
117int tb_tunnel_release_unused_bandwidth(struct tb_tunnel *tunnel);
118void tb_tunnel_reclaim_available_bandwidth(struct tb_tunnel *tunnel,
119 int *available_up,
120 int *available_down);
121
122static inline bool tb_tunnel_is_pci(const struct tb_tunnel *tunnel)
123{
124 return tunnel->type == TB_TUNNEL_PCI;
125}
126
127static inline bool tb_tunnel_is_dp(const struct tb_tunnel *tunnel)
128{
129 return tunnel->type == TB_TUNNEL_DP;
130}
131
132static inline bool tb_tunnel_is_dma(const struct tb_tunnel *tunnel)
133{
134 return tunnel->type == TB_TUNNEL_DMA;
135}
136
137static inline bool tb_tunnel_is_usb3(const struct tb_tunnel *tunnel)
138{
139 return tunnel->type == TB_TUNNEL_USB3;
140}
141
142const char *tb_tunnel_type_name(const struct tb_tunnel *tunnel);
143
144#define __TB_TUNNEL_PRINT(level, tunnel, fmt, arg...) \
145 do { \
146 struct tb_tunnel *__tunnel = (tunnel); \
147 level(__tunnel->tb, "%llx:%u <-> %llx:%u (%s): " fmt, \
148 tb_route(__tunnel->src_port->sw), \
149 __tunnel->src_port->port, \
150 tb_route(__tunnel->dst_port->sw), \
151 __tunnel->dst_port->port, \
152 tb_tunnel_type_name(__tunnel), \
153 ## arg); \
154 } while (0)
155
156#define tb_tunnel_WARN(tunnel, fmt, arg...) \
157 __TB_TUNNEL_PRINT(tb_WARN, tunnel, fmt, ##arg)
158#define tb_tunnel_warn(tunnel, fmt, arg...) \
159 __TB_TUNNEL_PRINT(tb_warn, tunnel, fmt, ##arg)
160#define tb_tunnel_info(tunnel, fmt, arg...) \
161 __TB_TUNNEL_PRINT(tb_info, tunnel, fmt, ##arg)
162#define tb_tunnel_dbg(tunnel, fmt, arg...) \
163 __TB_TUNNEL_PRINT(tb_dbg, tunnel, fmt, ##arg)
164
165#endif