xref: /freebsd/sys/dev/gve/gve_adminq.h (revision dabf006a638fdc44cdcf69731de8ac83959db731)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 2023-2024 Google LLC
5  *
6  * Redistribution and use in source and binary forms, with or without modification,
7  * are permitted provided that the following conditions are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright notice, this
10  *    list of conditions and the following disclaimer.
11  *
12  * 2. Redistributions in binary form must reproduce the above copyright notice,
13  *    this list of conditions and the following disclaimer in the documentation
14  *    and/or other materials provided with the distribution.
15  *
16  * 3. Neither the name of the copyright holder nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software without
18  *    specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
22  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
24  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
25  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
27  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
29  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  */
31 #ifndef _GVE_AQ_H_
32 #define _GVE_AQ_H_ 1
33 
34 #include <sys/types.h>
35 #include <net/if.h>
36 #include <net/iflib.h>
37 #include <machine/bus.h>
38 #include <machine/resource.h>
39 
40 /* Admin queue opcodes */
41 enum gve_adminq_opcodes {
42 	GVE_ADMINQ_DESCRIBE_DEVICE		= 0x1,
43 	GVE_ADMINQ_CONFIGURE_DEVICE_RESOURCES	= 0x2,
44 	GVE_ADMINQ_REGISTER_PAGE_LIST		= 0x3,
45 	GVE_ADMINQ_UNREGISTER_PAGE_LIST		= 0x4,
46 	GVE_ADMINQ_CREATE_TX_QUEUE		= 0x5,
47 	GVE_ADMINQ_CREATE_RX_QUEUE		= 0x6,
48 	GVE_ADMINQ_DESTROY_TX_QUEUE		= 0x7,
49 	GVE_ADMINQ_DESTROY_RX_QUEUE		= 0x8,
50 	GVE_ADMINQ_DECONFIGURE_DEVICE_RESOURCES	= 0x9,
51 	GVE_ADMINQ_SET_DRIVER_PARAMETER		= 0xB,
52 	GVE_ADMINQ_REPORT_STATS			= 0xC,
53 	GVE_ADMINQ_REPORT_LINK_SPEED		= 0xD,
54 	GVE_ADMINQ_GET_PTYPE_MAP		= 0xE,
55 	GVE_ADMINQ_VERIFY_DRIVER_COMPATIBILITY	= 0xF,
56 };
57 
58 /* Admin queue status codes */
59 enum gve_adminq_statuses {
60 	GVE_ADMINQ_COMMAND_UNSET			= 0x0,
61 	GVE_ADMINQ_COMMAND_PASSED			= 0x1,
62 	GVE_ADMINQ_COMMAND_ERROR_ABORTED		= 0xFFFFFFF0,
63 	GVE_ADMINQ_COMMAND_ERROR_ALREADY_EXISTS		= 0xFFFFFFF1,
64 	GVE_ADMINQ_COMMAND_ERROR_CANCELLED		= 0xFFFFFFF2,
65 	GVE_ADMINQ_COMMAND_ERROR_DATALOSS		= 0xFFFFFFF3,
66 	GVE_ADMINQ_COMMAND_ERROR_DEADLINE_EXCEEDED	= 0xFFFFFFF4,
67 	GVE_ADMINQ_COMMAND_ERROR_FAILED_PRECONDITION	= 0xFFFFFFF5,
68 	GVE_ADMINQ_COMMAND_ERROR_INTERNAL_ERROR		= 0xFFFFFFF6,
69 	GVE_ADMINQ_COMMAND_ERROR_INVALID_ARGUMENT	= 0xFFFFFFF7,
70 	GVE_ADMINQ_COMMAND_ERROR_NOT_FOUND		= 0xFFFFFFF8,
71 	GVE_ADMINQ_COMMAND_ERROR_OUT_OF_RANGE		= 0xFFFFFFF9,
72 	GVE_ADMINQ_COMMAND_ERROR_PERMISSION_DENIED	= 0xFFFFFFFA,
73 	GVE_ADMINQ_COMMAND_ERROR_UNAUTHENTICATED	= 0xFFFFFFFB,
74 	GVE_ADMINQ_COMMAND_ERROR_RESOURCE_EXHAUSTED	= 0xFFFFFFFC,
75 	GVE_ADMINQ_COMMAND_ERROR_UNAVAILABLE		= 0xFFFFFFFD,
76 	GVE_ADMINQ_COMMAND_ERROR_UNIMPLEMENTED		= 0xFFFFFFFE,
77 	GVE_ADMINQ_COMMAND_ERROR_UNKNOWN_ERROR		= 0xFFFFFFFF,
78 };
79 
80 #define GVE_ADMINQ_DEVICE_DESCRIPTOR_VERSION 1
81 
82 /*
83  * All AdminQ command structs should be naturally packed. The static_assert
84  * calls make sure this is the case at compile time.
85  */
86 
87 struct gve_adminq_describe_device {
88 	__be64 device_descriptor_addr;
89 	__be32 device_descriptor_version;
90 	__be32 available_length;
91 };
92 
93 _Static_assert(sizeof(struct gve_adminq_describe_device) == 16,
94     "gve: bad admin queue struct length");
95 
96 struct gve_device_descriptor {
97 	__be64 max_registered_pages;
98 	__be16 reserved1;
99 	__be16 tx_queue_entries;
100 	__be16 rx_queue_entries;
101 	__be16 default_num_queues;
102 	__be16 mtu;
103 	__be16 counters;
104 	__be16 reserved2;
105 	__be16 rx_pages_per_qpl;
106 	uint8_t  mac[ETHER_ADDR_LEN];
107 	__be16 num_device_options;
108 	__be16 total_length;
109 	uint8_t  reserved3[6];
110 };
111 
112 _Static_assert(sizeof(struct gve_device_descriptor) == 40,
113     "gve: bad admin queue struct length");
114 
115 struct gve_device_option {
116 	__be16 option_id;
117 	__be16 option_length;
118 	__be32 required_features_mask;
119 };
120 
121 _Static_assert(sizeof(struct gve_device_option) == 8,
122     "gve: bad admin queue struct length");
123 
124 struct gve_device_option_gqi_rda {
125 	__be32 supported_features_mask;
126 };
127 
128 _Static_assert(sizeof(struct gve_device_option_gqi_rda) == 4,
129     "gve: bad admin queue struct length");
130 
131 struct gve_device_option_gqi_qpl {
132 	__be32 supported_features_mask;
133 };
134 
135 _Static_assert(sizeof(struct gve_device_option_gqi_qpl) == 4,
136     "gve: bad admin queue struct length");
137 
138 struct gve_device_option_dqo_rda {
139 	__be32 supported_features_mask;
140 	__be16 tx_comp_ring_entries;
141 	__be16 rx_buff_ring_entries;
142 };
143 
144 _Static_assert(sizeof(struct gve_device_option_dqo_rda) == 8,
145     "gve: bad admin queue struct length");
146 
147 struct gve_device_option_dqo_qpl {
148 	__be32 supported_features_mask;
149 	__be16 tx_comp_ring_entries;
150 	__be16 rx_buff_ring_entries;
151 };
152 
153 _Static_assert(sizeof(struct gve_device_option_dqo_qpl) == 8,
154     "gve: bad admin queue struct length");
155 
156 struct gve_device_option_modify_ring {
157 	__be32 supported_features_mask;
158 	__be16 max_rx_ring_size;
159 	__be16 max_tx_ring_size;
160 };
161 
162 _Static_assert(sizeof(struct gve_device_option_modify_ring) == 8,
163     "gve: bad admin queue struct length");
164 
165 struct gve_device_option_jumbo_frames {
166 	__be32 supported_features_mask;
167 	__be16 max_mtu;
168 	uint8_t padding[2];
169 };
170 
171 _Static_assert(sizeof(struct gve_device_option_jumbo_frames) == 8,
172     "gve: bad admin queue struct length");
173 
174 enum gve_dev_opt_id {
175 	GVE_DEV_OPT_ID_GQI_RAW_ADDRESSING = 0x1,
176 	GVE_DEV_OPT_ID_GQI_RDA = 0x2,
177 	GVE_DEV_OPT_ID_GQI_QPL = 0x3,
178 	GVE_DEV_OPT_ID_DQO_RDA = 0x4,
179 	GVE_DEV_OPT_ID_MODIFY_RING = 0x6,
180 	GVE_DEV_OPT_ID_DQO_QPL = 0x7,
181 	GVE_DEV_OPT_ID_JUMBO_FRAMES = 0x8,
182 };
183 
184 /*
185  * These masks are way to predicate the use of a particular option on the driver
186  * having particular bug fixes represented by each bit position in the mask.
187  * Currently they are all zero because there are no known bugs preventing the
188  * use of any option.
189  */
190 enum gve_dev_opt_req_feat_mask {
191 	GVE_DEV_OPT_REQ_FEAT_MASK_GQI_RAW_ADDRESSING = 0x0,
192 	GVE_DEV_OPT_REQ_FEAT_MASK_GQI_RDA = 0x0,
193 	GVE_DEV_OPT_REQ_FEAT_MASK_GQI_QPL = 0x0,
194 	GVE_DEV_OPT_REQ_FEAT_MASK_DQO_RDA = 0x0,
195 	GVE_DEV_OPT_REQ_FEAT_MASK_DQO_QPL = 0x0,
196 	GVE_DEV_OPT_REQ_FEAT_MASK_MODIFY_RING = 0x0,
197 	GVE_DEV_OPT_REQ_FEAT_MASK_JUMBO_FRAMES = 0x0,
198 };
199 
200 enum gve_sup_feature_mask {
201 	GVE_SUP_MODIFY_RING_MASK  = 1 << 0,
202 	GVE_SUP_JUMBO_FRAMES_MASK = 1 << 2,
203 };
204 
205 #define GVE_VERSION_STR_LEN 128
206 
207 enum gve_driver_capability {
208 	gve_driver_capability_gqi_qpl = 0,
209 	gve_driver_capability_gqi_rda = 1,
210 	gve_driver_capability_dqo_qpl = 2,
211 	gve_driver_capability_dqo_rda = 3,
212 };
213 
214 #define GVE_CAP1(a) BIT((int) a)
215 #define GVE_CAP2(a) BIT(((int) a) - 64)
216 #define GVE_CAP3(a) BIT(((int) a) - 128)
217 #define GVE_CAP4(a) BIT(((int) a) - 192)
218 
219 /*
220  * The following four defines describe 256 compatibility bits.
221  * Only a few bits (as shown in `gve_driver_compatibility`) are currently
222  * defined. The rest are reserved for future use.
223  */
224 #define GVE_DRIVER_CAPABILITY_FLAGS1 \
225 	(GVE_CAP1(gve_driver_capability_gqi_qpl) | \
226 	 GVE_CAP1(gve_driver_capability_dqo_qpl) | \
227 	 GVE_CAP1(gve_driver_capability_dqo_rda))
228 #define GVE_DRIVER_CAPABILITY_FLAGS2 0x0
229 #define GVE_DRIVER_CAPABILITY_FLAGS3 0x0
230 #define GVE_DRIVER_CAPABILITY_FLAGS4 0x0
231 
232 struct gve_driver_info {
233 	uint8_t os_type;
234 	uint8_t driver_major;
235 	uint8_t driver_minor;
236 	uint8_t driver_sub;
237 	__be32 os_version_major;
238 	__be32 os_version_minor;
239 	__be32 os_version_sub;
240 	__be64 driver_capability_flags[4];
241 	uint8_t os_version_str1[GVE_VERSION_STR_LEN];
242 	uint8_t os_version_str2[GVE_VERSION_STR_LEN];
243 };
244 
245 struct gve_adminq_verify_driver_compatibility {
246 	__be64 driver_info_len;
247 	__be64 driver_info_addr;
248 };
249 
250 _Static_assert(sizeof(struct gve_adminq_verify_driver_compatibility) == 16,
251     "gve: bad admin queue struct length");
252 
253 struct gve_adminq_configure_device_resources {
254 	__be64 counter_array;
255 	__be64 irq_db_addr;
256 	__be32 num_counters;
257 	__be32 num_irq_dbs;
258 	__be32 irq_db_stride;
259 	__be32 ntfy_blk_msix_base_idx;
260 	uint8_t queue_format;
261 	uint8_t padding[7];
262 };
263 
264 _Static_assert(sizeof(struct gve_adminq_configure_device_resources) == 40,
265     "gve: bad admin queue struct length");
266 
267 struct gve_adminq_register_page_list {
268 	__be32 page_list_id;
269 	__be32 num_pages;
270 	__be64 page_address_list_addr;
271 	__be64 page_size;
272 };
273 
274 _Static_assert(sizeof(struct gve_adminq_register_page_list) == 24,
275     "gve: bad admin queue struct length");
276 
277 struct gve_adminq_unregister_page_list {
278 	__be32 page_list_id;
279 };
280 
281 _Static_assert(sizeof(struct gve_adminq_unregister_page_list) == 4,
282     "gve: bad admin queue struct length");
283 
284 struct gve_adminq_create_tx_queue {
285 	__be32 queue_id;
286 	__be32 reserved;
287 	__be64 queue_resources_addr;
288 	__be64 tx_ring_addr;
289 	__be32 queue_page_list_id;
290 	__be32 ntfy_id;
291 	__be64 tx_comp_ring_addr;
292 	__be16 tx_ring_size;
293 	__be16 tx_comp_ring_size;
294 	uint8_t padding[4];
295 };
296 
297 _Static_assert(sizeof(struct gve_adminq_create_tx_queue) == 48,
298     "gve: bad admin queue struct length");
299 
300 #define GVE_RAW_ADDRESSING_QPL_ID 0xFFFFFFFF
301 
302 struct gve_adminq_create_rx_queue {
303 	__be32 queue_id;
304 	__be32 index;
305 	__be32 reserved;
306 	__be32 ntfy_id;
307 	__be64 queue_resources_addr;
308 	__be64 rx_desc_ring_addr;
309 	__be64 rx_data_ring_addr;
310 	__be32 queue_page_list_id;
311 	__be16 rx_ring_size;
312 	__be16 packet_buffer_size;
313 	__be16 rx_buff_ring_size;
314 	uint8_t enable_rsc;
315 	uint8_t padding[5];
316 };
317 
318 _Static_assert(sizeof(struct gve_adminq_create_rx_queue) == 56,
319     "gve: bad admin queue struct length");
320 
321 /* Queue resources that are shared with the device */
322 struct gve_queue_resources {
323 	union {
324 		struct {
325 			__be32 db_index;	/* Device -> Guest */
326 			__be32 counter_index;	/* Device -> Guest */
327 		};
328 		uint8_t reserved[64];
329 	};
330 };
331 
332 _Static_assert(sizeof(struct gve_queue_resources) == 64,
333     "gve: bad admin queue struct length");
334 
335 struct gve_adminq_destroy_tx_queue {
336 	__be32 queue_id;
337 };
338 
339 _Static_assert(sizeof(struct gve_adminq_destroy_tx_queue) == 4,
340     "gve: bad admin queue struct length");
341 
342 struct gve_adminq_destroy_rx_queue {
343 	__be32 queue_id;
344 };
345 
346 _Static_assert(sizeof(struct gve_adminq_destroy_rx_queue) == 4,
347     "gve: bad admin queue struct length");
348 
349 /* GVE Set Driver Parameter Types */
350 enum gve_set_driver_param_types {
351 	GVE_SET_PARAM_MTU	= 0x1,
352 };
353 
354 struct gve_adminq_set_driver_parameter {
355 	__be32 parameter_type;
356 	uint8_t reserved[4];
357 	__be64 parameter_value;
358 };
359 
360 _Static_assert(sizeof(struct gve_adminq_set_driver_parameter) == 16,
361     "gve: bad admin queue struct length");
362 
363 struct stats {
364 	__be32 stat_name;
365 	__be32 queue_id;
366 	__be64 value;
367 };
368 
369 _Static_assert(sizeof(struct stats) == 16,
370     "gve: bad admin queue struct length");
371 
372 /* These are control path types for PTYPE which are the same as the data path
373  * types.
374  */
375 struct gve_ptype_entry {
376 	uint8_t l3_type;
377 	uint8_t l4_type;
378 };
379 
380 struct gve_ptype_map {
381 	struct gve_ptype_entry ptypes[1 << 10]; /* PTYPES are always 10 bits. */
382 };
383 
384 struct gve_adminq_get_ptype_map {
385 	__be64 ptype_map_len;
386 	__be64 ptype_map_addr;
387 };
388 
389 struct gve_adminq_command {
390 	__be32 opcode;
391 	__be32 status;
392 	union {
393 		struct gve_adminq_configure_device_resources
394 					configure_device_resources;
395 		struct gve_adminq_create_tx_queue create_tx_queue;
396 		struct gve_adminq_create_rx_queue create_rx_queue;
397 		struct gve_adminq_destroy_tx_queue destroy_tx_queue;
398 		struct gve_adminq_destroy_rx_queue destroy_rx_queue;
399 		struct gve_adminq_describe_device describe_device;
400 		struct gve_adminq_register_page_list reg_page_list;
401 		struct gve_adminq_unregister_page_list unreg_page_list;
402 		struct gve_adminq_set_driver_parameter set_driver_param;
403 		struct gve_adminq_verify_driver_compatibility
404 					verify_driver_compatibility;
405 		struct gve_adminq_get_ptype_map get_ptype_map;
406 		uint8_t reserved[56];
407 	};
408 };
409 
410 _Static_assert(sizeof(struct gve_adminq_command) == 64,
411     "gve: bad admin queue struct length");
412 
413 enum gve_l3_type {
414 	/* Must be zero so zero initialized LUT is unknown. */
415 	GVE_L3_TYPE_UNKNOWN = 0,
416 	GVE_L3_TYPE_OTHER,
417 	GVE_L3_TYPE_IPV4,
418 	GVE_L3_TYPE_IPV6,
419 };
420 
421 enum gve_l4_type {
422 	/* Must be zero so zero initialized LUT is unknown. */
423 	GVE_L4_TYPE_UNKNOWN = 0,
424 	GVE_L4_TYPE_OTHER,
425 	GVE_L4_TYPE_TCP,
426 	GVE_L4_TYPE_UDP,
427 	GVE_L4_TYPE_ICMP,
428 	GVE_L4_TYPE_SCTP,
429 };
430 
431 int gve_adminq_create_rx_queues(struct gve_priv *priv, uint32_t num_queues);
432 int gve_adminq_create_tx_queues(struct gve_priv *priv, uint32_t num_queues);
433 int gve_adminq_destroy_tx_queues(struct gve_priv *priv, uint32_t num_queues);
434 int gve_adminq_destroy_rx_queues(struct gve_priv *priv, uint32_t num_queues);
435 int gve_adminq_set_mtu(struct gve_priv *priv, uint32_t mtu);
436 int gve_adminq_alloc(struct gve_priv *priv);
437 void gve_reset_adminq(struct gve_priv *priv);
438 int gve_adminq_describe_device(struct gve_priv *priv);
439 int gve_adminq_configure_device_resources(struct gve_priv *priv);
440 int gve_adminq_deconfigure_device_resources(struct gve_priv *priv);
441 void gve_release_adminq(struct gve_priv *priv);
442 int gve_adminq_register_page_list(struct gve_priv *priv,
443     struct gve_queue_page_list *qpl);
444 int gve_adminq_unregister_page_list(struct gve_priv *priv, uint32_t page_list_id);
445 int gve_adminq_verify_driver_compatibility(struct gve_priv *priv,
446     uint64_t driver_info_len, vm_paddr_t driver_info_addr);
447 int gve_adminq_get_ptype_map_dqo(struct gve_priv *priv,
448     struct gve_ptype_lut *ptype_lut);
449 #endif /* _GVE_AQ_H_ */
450