xref: /linux/drivers/thunderbolt/tunnel.h (revision 5ea5880764cbb164afb17a62e76ca75dc371409d)
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 
14 enum tb_tunnel_type {
15 	TB_TUNNEL_PCI,
16 	TB_TUNNEL_DP,
17 	TB_TUNNEL_DMA,
18 	TB_TUNNEL_USB3,
19 };
20 
21 /**
22  * enum tb_tunnel_state - State of a tunnel
23  * @TB_TUNNEL_INACTIVE: tb_tunnel_activate() is not called for the tunnel
24  * @TB_TUNNEL_ACTIVATING: tb_tunnel_activate() returned successfully for the tunnel
25  * @TB_TUNNEL_ACTIVE: The tunnel is fully active
26  */
27 enum tb_tunnel_state {
28 	TB_TUNNEL_INACTIVE,
29 	TB_TUNNEL_ACTIVATING,
30 	TB_TUNNEL_ACTIVE,
31 };
32 
33 /**
34  * struct tb_tunnel - Tunnel between two ports
35  * @kref: Reference count
36  * @tb: Pointer to the domain
37  * @src_port: Source port of the tunnel
38  * @dst_port: Destination port of the tunnel. For discovered incomplete
39  *	      tunnels may be %NULL or null adapter port instead.
40  * @npaths: Number of paths in @paths
41  * @pre_activate: Optional tunnel specific initialization called before
42  *		  activation. Can touch hardware.
43  * @activate: Optional tunnel specific activation/deactivation
44  * @post_deactivate: Optional tunnel specific de-initialization called
45  *		     after deactivation. Can touch hardware.
46  * @destroy: Optional tunnel specific callback called when the tunnel
47  *	     memory is being released. Should not touch hardware.
48  * @maximum_bandwidth: Returns maximum possible bandwidth for this tunnel
49  * @allocated_bandwidth: Return how much bandwidth is allocated for the tunnel
50  * @alloc_bandwidth: Change tunnel bandwidth allocation
51  * @consumed_bandwidth: Return how much bandwidth the tunnel consumes
52  * @release_unused_bandwidth: Release all unused bandwidth
53  * @reclaim_available_bandwidth: Reclaim back available bandwidth
54  * @list: Tunnels are linked using this field
55  * @type: Type of the tunnel
56  * @state: Current state of the tunnel
57  * @max_up: Maximum upstream bandwidth (Mb/s) available for the tunnel.
58  *	    Only set if the bandwidth needs to be limited.
59  * @max_down: Maximum downstream bandwidth (Mb/s) available for the tunnel.
60  *	      Only set if the bandwidth needs to be limited.
61  * @allocated_up: Allocated upstream bandwidth (only for USB3)
62  * @allocated_down: Allocated downstream bandwidth (only for USB3)
63  * @bw_mode: DP bandwidth allocation mode registers can be used to
64  *	     determine consumed and allocated bandwidth
65  * @dprx_started: DPRX negotiation was started (tb_dp_dprx_start() was called for it)
66  * @dprx_canceled: Was DPRX capabilities read poll canceled
67  * @dprx_timeout: If set DPRX capabilities read poll work will timeout after this passes
68  * @dprx_work: Worker that is scheduled to poll completion of DPRX capabilities read
69  * @callback: Optional callback called when DP tunnel is fully activated
70  * @callback_data: Optional data for @callback
71  * @paths: All paths required by the tunnel
72  */
73 struct tb_tunnel {
74 	struct kref kref;
75 	struct tb *tb;
76 	struct tb_port *src_port;
77 	struct tb_port *dst_port;
78 	size_t npaths;
79 	int (*pre_activate)(struct tb_tunnel *tunnel);
80 	int (*activate)(struct tb_tunnel *tunnel, bool activate);
81 	void (*post_deactivate)(struct tb_tunnel *tunnel);
82 	void (*destroy)(struct tb_tunnel *tunnel);
83 	int (*maximum_bandwidth)(struct tb_tunnel *tunnel, int *max_up,
84 				 int *max_down);
85 	int (*allocated_bandwidth)(struct tb_tunnel *tunnel, int *allocated_up,
86 				   int *allocated_down);
87 	int (*alloc_bandwidth)(struct tb_tunnel *tunnel, int *alloc_up,
88 			       int *alloc_down);
89 	int (*consumed_bandwidth)(struct tb_tunnel *tunnel, int *consumed_up,
90 				  int *consumed_down);
91 	int (*release_unused_bandwidth)(struct tb_tunnel *tunnel);
92 	void (*reclaim_available_bandwidth)(struct tb_tunnel *tunnel,
93 					    int *available_up,
94 					    int *available_down);
95 	struct list_head list;
96 	enum tb_tunnel_type type;
97 	enum tb_tunnel_state state;
98 	int max_up;
99 	int max_down;
100 	int allocated_up;
101 	int allocated_down;
102 	bool bw_mode;
103 	bool dprx_started;
104 	bool dprx_canceled;
105 	ktime_t dprx_timeout;
106 	struct delayed_work dprx_work;
107 	void (*callback)(struct tb_tunnel *tunnel, void *data);
108 	void *callback_data;
109 
110 	struct tb_path *paths[] __counted_by(npaths);
111 };
112 
113 struct tb_tunnel *tb_tunnel_discover_pci(struct tb *tb, struct tb_port *down,
114 					 bool alloc_hopid);
115 struct tb_tunnel *tb_tunnel_alloc_pci(struct tb *tb, struct tb_port *up,
116 				      struct tb_port *down);
117 bool tb_tunnel_reserved_pci(struct tb_port *port, int *reserved_up,
118 			    int *reserved_down);
119 struct tb_tunnel *tb_tunnel_discover_dp(struct tb *tb, struct tb_port *in,
120 					bool alloc_hopid);
121 struct tb_tunnel *tb_tunnel_alloc_dp(struct tb *tb, struct tb_port *in,
122 				     struct tb_port *out, int link_nr,
123 				     int max_up, int max_down,
124 				     void (*callback)(struct tb_tunnel *, void *),
125 				     void *callback_data);
126 struct tb_tunnel *tb_tunnel_alloc_dma(struct tb *tb, struct tb_port *nhi,
127 				      struct tb_port *dst, int transmit_path,
128 				      int transmit_ring, int receive_path,
129 				      int receive_ring);
130 bool tb_tunnel_match_dma(const struct tb_tunnel *tunnel, int transmit_path,
131 			 int transmit_ring, int receive_path, int receive_ring);
132 struct tb_tunnel *tb_tunnel_discover_usb3(struct tb *tb, struct tb_port *down,
133 					  bool alloc_hopid);
134 struct tb_tunnel *tb_tunnel_alloc_usb3(struct tb *tb, struct tb_port *up,
135 				       struct tb_port *down, int max_up,
136 				       int max_down);
137 
138 void tb_tunnel_put(struct tb_tunnel *tunnel);
139 int tb_tunnel_activate(struct tb_tunnel *tunnel);
140 void tb_tunnel_deactivate(struct tb_tunnel *tunnel);
141 
142 /**
143  * tb_tunnel_is_active() - Is tunnel fully activated
144  * @tunnel: Tunnel to check
145  *
146  * Return: %true if @tunnel is fully activated.
147  *
148  * Note for DP tunnels this returns %true only once the DPRX capabilities
149  * read has been issued successfully. For other tunnels, this function
150  * returns %true pretty much once tb_tunnel_activate() returns successfully.
151  */
152 static inline bool tb_tunnel_is_active(const struct tb_tunnel *tunnel)
153 {
154 	return tunnel->state == TB_TUNNEL_ACTIVE;
155 }
156 
157 bool tb_tunnel_is_invalid(struct tb_tunnel *tunnel);
158 bool tb_tunnel_port_on_path(const struct tb_tunnel *tunnel,
159 			    const struct tb_port *port);
160 int tb_tunnel_maximum_bandwidth(struct tb_tunnel *tunnel, int *max_up,
161 				int *max_down);
162 int tb_tunnel_allocated_bandwidth(struct tb_tunnel *tunnel, int *allocated_up,
163 				  int *allocated_down);
164 int tb_tunnel_alloc_bandwidth(struct tb_tunnel *tunnel, int *alloc_up,
165 			      int *alloc_down);
166 int tb_tunnel_consumed_bandwidth(struct tb_tunnel *tunnel, int *consumed_up,
167 				 int *consumed_down);
168 int tb_tunnel_release_unused_bandwidth(struct tb_tunnel *tunnel);
169 void tb_tunnel_reclaim_available_bandwidth(struct tb_tunnel *tunnel,
170 					   int *available_up,
171 					   int *available_down);
172 
173 static inline bool tb_tunnel_is_pci(const struct tb_tunnel *tunnel)
174 {
175 	return tunnel->type == TB_TUNNEL_PCI;
176 }
177 
178 static inline bool tb_tunnel_is_dp(const struct tb_tunnel *tunnel)
179 {
180 	return tunnel->type == TB_TUNNEL_DP;
181 }
182 
183 static inline bool tb_tunnel_is_dma(const struct tb_tunnel *tunnel)
184 {
185 	return tunnel->type == TB_TUNNEL_DMA;
186 }
187 
188 static inline bool tb_tunnel_is_usb3(const struct tb_tunnel *tunnel)
189 {
190 	return tunnel->type == TB_TUNNEL_USB3;
191 }
192 
193 static inline bool tb_tunnel_direction_downstream(const struct tb_tunnel *tunnel)
194 {
195 	return tb_port_path_direction_downstream(tunnel->src_port,
196 						 tunnel->dst_port);
197 }
198 
199 /**
200  * enum tb_tunnel_event - Tunnel related events
201  * @TB_TUNNEL_ACTIVATED: A tunnel was activated
202  * @TB_TUNNEL_CHANGED: There is a tunneling change in the domain. Includes
203  *		       full %TUNNEL_DETAILS if the tunnel in question is known
204  *		       (ICM does not provide that information).
205  * @TB_TUNNEL_DEACTIVATED: A tunnel was torn down
206  * @TB_TUNNEL_LOW_BANDWIDTH: Tunnel bandwidth is not optimal
207  * @TB_TUNNEL_NO_BANDWIDTH: There is not enough bandwidth for a tunnel
208  */
209 enum tb_tunnel_event {
210 	TB_TUNNEL_ACTIVATED,
211 	TB_TUNNEL_CHANGED,
212 	TB_TUNNEL_DEACTIVATED,
213 	TB_TUNNEL_LOW_BANDWIDTH,
214 	TB_TUNNEL_NO_BANDWIDTH,
215 };
216 
217 void tb_tunnel_event(struct tb *tb, enum tb_tunnel_event event,
218 		     enum tb_tunnel_type type,
219 		     const struct tb_port *src_port,
220 		     const struct tb_port *dst_port);
221 
222 const char *tb_tunnel_type_name(const struct tb_tunnel *tunnel);
223 
224 #define __TB_TUNNEL_PRINT(level, tunnel, fmt, arg...)                   \
225 	do {                                                            \
226 		struct tb_tunnel *__tunnel = (tunnel);                  \
227 		level(__tunnel->tb, "%llx:%u <-> %llx:%u (%s): " fmt,   \
228 		      tb_route(__tunnel->src_port->sw),                 \
229 		      __tunnel->src_port->port,                         \
230 		      tb_route(__tunnel->dst_port->sw),                 \
231 		      __tunnel->dst_port->port,                         \
232 		      tb_tunnel_type_name(__tunnel),			\
233 		      ## arg);                                          \
234 	} while (0)
235 
236 #define tb_tunnel_WARN(tunnel, fmt, arg...) \
237 	__TB_TUNNEL_PRINT(tb_WARN, tunnel, fmt, ##arg)
238 #define tb_tunnel_warn(tunnel, fmt, arg...) \
239 	__TB_TUNNEL_PRINT(tb_warn, tunnel, fmt, ##arg)
240 #define tb_tunnel_info(tunnel, fmt, arg...) \
241 	__TB_TUNNEL_PRINT(tb_info, tunnel, fmt, ##arg)
242 #define tb_tunnel_dbg(tunnel, fmt, arg...) \
243 	__TB_TUNNEL_PRINT(tb_dbg, tunnel, fmt, ##arg)
244 
245 #endif
246