1 #ifndef _NETVSC_H
2 #define _NETVSC_H
3 
4 /** @file
5  *
6  * Hyper-V network virtual service client
7  *
8  */
9 
10 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
11 
12 /** Maximum supported NetVSC message length */
13 #define NETVSC_MTU 512
14 
15 /** Maximum time to wait for a transaction to complete
16  *
17  * This is a policy decision.
18  */
19 #define NETVSC_MAX_WAIT_MS 1000
20 
21 /** Number of transmit ring entries
22  *
23  * Must be a power of two.  This is a policy decision.  This value
24  * must be sufficiently small to guarantee that we never run out of
25  * space in the VMBus outbound ring buffer.
26  */
27 #define NETVSC_TX_NUM_DESC 32
28 
29 /** RX data buffer page set ID
30  *
31  * This is a policy decision.
32  */
33 #define NETVSC_RX_BUF_PAGESET 0xbead
34 
35 /** RX data buffer length
36  *
37  * This is a policy decision.
38  */
39 #define NETVSC_RX_BUF_LEN ( 16 * PAGE_SIZE )
40 
41 /** Base transaction ID
42  *
43  * This is a policy decision.
44  */
45 #define NETVSC_BASE_XID 0x18ae0000UL
46 
47 /** Relative transaction IDs */
48 enum netvsc_xrid {
49 	/** Transmit descriptors (one per transmit buffer ID) */
50 	NETVSC_TX_BASE_XRID = 0,
51 	/** Initialisation */
52 	NETVSC_INIT_XRID = ( NETVSC_TX_BASE_XRID + NETVSC_TX_NUM_DESC ),
53 	/** NDIS version */
54 	NETVSC_NDIS_VERSION_XRID,
55 	/** Establish receive buffer */
56 	NETVSC_RX_ESTABLISH_XRID,
57 	/** Revoke receive buffer */
58 	NETVSC_RX_REVOKE_XRID,
59 };
60 
61 /** NetVSC status codes */
62 enum netvsc_status {
63 	NETVSC_NONE = 0,
64 	NETVSC_OK = 1,
65 	NETVSC_FAIL = 2,
66 	NETVSC_TOO_NEW = 3,
67 	NETVSC_TOO_OLD = 4,
68 	NETVSC_BAD_PACKET = 5,
69 	NETVSC_BUSY = 6,
70 	NETVSC_UNSUPPORTED = 7,
71 };
72 
73 /** NetVSC message header */
74 struct netvsc_header {
75 	/** Type */
76 	uint32_t type;
77 } __attribute__ (( packed ));
78 
79 /** NetVSC initialisation message */
80 #define NETVSC_INIT_MSG 1
81 
82 /** NetVSC initialisation message */
83 struct netvsc_init_message {
84 	/** Message header */
85 	struct netvsc_header header;
86 	/** Minimum supported protocol version */
87 	uint32_t min;
88 	/** Maximum supported protocol version */
89 	uint32_t max;
90 	/** Reserved */
91 	uint8_t reserved[20];
92 } __attribute__ (( packed ));
93 
94 /** Oldest known NetVSC protocol version */
95 #define NETVSC_VERSION_1 2 /* sic */
96 
97 /** NetVSC initialisation completion */
98 #define NETVSC_INIT_CMPLT 2
99 
100 /** NetVSC initialisation completion */
101 struct netvsc_init_completion {
102 	/** Message header */
103 	struct netvsc_header header;
104 	/** Protocol version */
105 	uint32_t version;
106 	/** Maximum memory descriptor list length */
107 	uint32_t max_mdl_len;
108 	/** Status */
109 	uint32_t status;
110 	/** Reserved */
111 	uint8_t reserved[16];
112 } __attribute__ (( packed ));
113 
114 /** NetVSC NDIS version message */
115 #define NETVSC_NDIS_VERSION_MSG 100
116 
117 /** NetVSC NDIS version message */
118 struct netvsc_ndis_version_message {
119 	/** Message header */
120 	struct netvsc_header header;
121 	/** Major version */
122 	uint32_t major;
123 	/** Minor version */
124 	uint32_t minor;
125 	/** Reserved */
126 	uint8_t reserved[20];
127 } __attribute__ (( packed ));
128 
129 /** NetVSC NDIS major version */
130 #define NETVSC_NDIS_MAJOR 6
131 
132 /** NetVSC NDIS minor version */
133 #define NETVSC_NDIS_MINOR 1
134 
135 /** NetVSC establish receive data buffer message */
136 #define NETVSC_RX_ESTABLISH_MSG 101
137 
138 /** NetVSC establish receive data buffer completion */
139 #define NETVSC_RX_ESTABLISH_CMPLT 102
140 
141 /** NetVSC revoke receive data buffer message */
142 #define NETVSC_RX_REVOKE_MSG 103
143 
144 /** NetVSC establish transmit data buffer message */
145 #define NETVSC_TX_ESTABLISH_MSG 104
146 
147 /** NetVSC establish transmit data buffer completion */
148 #define NETVSC_TX_ESTABLISH_CMPLT 105
149 
150 /** NetVSC revoke transmit data buffer message */
151 #define NETVSC_TX_REVOKE_MSG 106
152 
153 /** NetVSC establish data buffer message */
154 struct netvsc_establish_buffer_message {
155 	/** Message header */
156 	struct netvsc_header header;
157 	/** GPADL ID */
158 	uint32_t gpadl;
159 	/** Page set ID */
160 	uint16_t pageset;
161 	/** Reserved */
162 	uint8_t reserved[22];
163 } __attribute__ (( packed ));
164 
165 /** NetVSC receive data buffer section */
166 struct netvsc_rx_buffer_section {
167 	/** Starting offset */
168 	uint32_t start;
169 	/** Subsection length */
170 	uint32_t len;
171 	/** Number of subsections */
172 	uint32_t count;
173 	/** Ending offset */
174 	uint32_t end;
175 } __attribute__ (( packed ));
176 
177 /** NetVSC establish receive data buffer completion */
178 struct netvsc_rx_establish_buffer_completion {
179 	/** Message header */
180 	struct netvsc_header header;
181 	/** Status */
182 	uint32_t status;
183 	/** Number of sections (must be 1) */
184 	uint32_t count;
185 	/** Section descriptors */
186 	struct netvsc_rx_buffer_section section[1];
187 } __attribute__ (( packed ));
188 
189 /** NetVSC establish transmit data buffer completion */
190 struct netvsc_tx_establish_buffer_completion {
191 	/** Message header */
192 	struct netvsc_header header;
193 	/** Status */
194 	uint32_t status;
195 	/** Section length */
196 	uint32_t len;
197 } __attribute__ (( packed ));
198 
199 /** NetVSC revoke data buffer message */
200 struct netvsc_revoke_buffer_message {
201 	/** Message header */
202 	struct netvsc_header header;
203 	/** Page set ID */
204 	uint16_t pageset;
205 	/** Reserved */
206 	uint8_t reserved[26];
207 } __attribute__ (( packed ));
208 
209 /** NetVSC RNDIS message */
210 #define NETVSC_RNDIS_MSG 107
211 
212 /** NetVSC RNDIS message */
213 struct netvsc_rndis_message {
214 	/** Message header */
215 	struct netvsc_header header;
216 	/** RNDIS channel */
217 	uint32_t channel;
218 	/** Buffer index (or NETVSC_RNDIS_NO_BUFFER) */
219 	uint32_t buffer;
220 	/** Buffer length */
221 	uint32_t len;
222 	/** Reserved */
223 	uint8_t reserved[16];
224 } __attribute__ (( packed ));
225 
226 /** RNDIS data channel (for RNDIS_PACKET_MSG only) */
227 #define NETVSC_RNDIS_DATA 0
228 
229 /** RNDIS control channel (for all other RNDIS messages) */
230 #define NETVSC_RNDIS_CONTROL 1
231 
232 /** "No buffer used" index */
233 #define NETVSC_RNDIS_NO_BUFFER 0xffffffffUL
234 
235 /** A NetVSC descriptor ring */
236 struct netvsc_ring {
237 	/** Number of descriptors */
238 	unsigned int count;
239 	/** I/O buffers, indexed by buffer ID */
240 	struct io_buffer **iobufs;
241 	/** Buffer ID ring */
242 	uint8_t *ids;
243 	/** Buffer ID producer counter */
244 	unsigned int id_prod;
245 	/** Buffer ID consumer counter */
246 	unsigned int id_cons;
247 };
248 
249 /**
250  * Initialise descriptor ring
251  *
252  * @v ring		Descriptor ring
253  * @v count		Maximum number of used descriptors
254  * @v iobufs		I/O buffers
255  * @v ids		Buffer IDs
256  */
257 static inline __attribute__ (( always_inline )) void
netvsc_init_ring(struct netvsc_ring * ring,unsigned int count,struct io_buffer ** iobufs,uint8_t * ids)258 netvsc_init_ring ( struct netvsc_ring *ring, unsigned int count,
259 		   struct io_buffer **iobufs, uint8_t *ids ) {
260 
261 	ring->count = count;
262 	ring->iobufs = iobufs;
263 	ring->ids = ids;
264 }
265 
266 /**
267  * Check whether or not descriptor ring is full
268  *
269  * @v ring		Descriptor ring
270  * @v is_full		Ring is full
271  */
272 static inline __attribute__ (( always_inline )) int
netvsc_ring_is_full(struct netvsc_ring * ring)273 netvsc_ring_is_full ( struct netvsc_ring *ring ) {
274 	unsigned int fill_level;
275 
276 	fill_level = ( ring->id_prod - ring->id_cons );
277 	assert ( fill_level <= ring->count );
278 	return ( fill_level >= ring->count );
279 }
280 
281 /**
282  * Check whether or not descriptor ring is empty
283  *
284  * @v ring		Descriptor ring
285  * @v is_empty		Ring is empty
286  */
287 static inline __attribute__ (( always_inline )) int
netvsc_ring_is_empty(struct netvsc_ring * ring)288 netvsc_ring_is_empty ( struct netvsc_ring *ring ) {
289 
290 	return ( ring->id_prod == ring->id_cons );
291 }
292 
293 /** A NetVSC data buffer */
294 struct netvsc_buffer {
295 	/** Transfer page set */
296 	struct vmbus_xfer_pages pages;
297 	/** Establish data buffer message type */
298 	uint8_t establish_type;
299 	/** Establish data buffer relative transaction ID */
300 	uint8_t establish_xrid;
301 	/** Revoke data buffer message type */
302 	uint8_t revoke_type;
303 	/** Revoke data buffer relative transaction ID */
304 	uint8_t revoke_xrid;
305 	/** Buffer length */
306 	size_t len;
307 	/** Buffer */
308 	userptr_t data;
309 	/** GPADL ID */
310 	unsigned int gpadl;
311 };
312 
313 /**
314  * Initialise data buffer
315  *
316  * @v buffer		Data buffer
317  * @v pageset		Page set ID
318  * @v op		Page set operations
319  * @v establish_type	Establish data buffer message type
320  * @v establish_xrid	Establish data buffer relative transaction ID
321  * @v revoke_type	Revoke data buffer message type
322  * @v revoke_type	Revoke data buffer relative transaction ID
323  * @v len		Required length
324  */
325 static inline __attribute__ (( always_inline )) void
netvsc_init_buffer(struct netvsc_buffer * buffer,uint16_t pageset,struct vmbus_xfer_pages_operations * op,uint8_t establish_type,uint8_t establish_xrid,uint8_t revoke_type,uint8_t revoke_xrid,size_t len)326 netvsc_init_buffer ( struct netvsc_buffer *buffer, uint16_t pageset,
327 		     struct vmbus_xfer_pages_operations *op,
328 		     uint8_t establish_type, uint8_t establish_xrid,
329 		     uint8_t revoke_type, uint8_t revoke_xrid, size_t len ) {
330 
331 	buffer->pages.pageset = cpu_to_le16 ( pageset );
332 	buffer->pages.op = op;
333 	buffer->establish_type = establish_type;
334 	buffer->establish_xrid = establish_xrid;
335 	buffer->revoke_type = revoke_type;
336 	buffer->revoke_xrid = revoke_xrid;
337 	buffer->len = len;
338 }
339 
340 /** A NetVSC device */
341 struct netvsc_device {
342 	/** VMBus device */
343 	struct vmbus_device *vmdev;
344 	/** RNDIS device */
345 	struct rndis_device *rndis;
346 	/** Name */
347 	const char *name;
348 
349 	/** Transmit ring */
350 	struct netvsc_ring tx;
351 	/** Transmit buffer IDs */
352 	uint8_t tx_ids[NETVSC_TX_NUM_DESC];
353 	/** Transmit I/O buffers */
354 	struct io_buffer *tx_iobufs[NETVSC_TX_NUM_DESC];
355 
356 	/** Receive buffer */
357 	struct netvsc_buffer rx;
358 
359 	/** Relative transaction ID for current blocking transaction */
360 	unsigned int wait_xrid;
361 	/** Return status code for current blocking transaction */
362 	int wait_rc;
363 };
364 
365 /**
366  * Check if NetVSC device is obsolete
367  *
368  * @v netvsc		NetVSC device
369  * @v is_obsolete	NetVSC device is obsolete
370  *
371  * Check if NetVSC device is obsolete (i.e. was opened before the most
372  * recent Hyper-V reset).
373  */
374 static inline __attribute__ (( always_inline )) int
netvsc_is_obsolete(struct netvsc_device * netvsc)375 netvsc_is_obsolete ( struct netvsc_device *netvsc ) {
376 
377 	return vmbus_gpadl_is_obsolete ( netvsc->rx.gpadl );
378 }
379 
380 #endif /* _NETVSC_H */
381