xref: /freebsd/sys/dev/iavf/virtchnl.h (revision d0b2dbfa0ecf2bbc9709efc5e20baf8e4b44bbbf)
1 /* SPDX-License-Identifier: BSD-3-Clause */
2 /*  Copyright (c) 2021, Intel Corporation
3  *  All rights reserved.
4  *
5  *  Redistribution and use in source and binary forms, with or without
6  *  modification, are permitted provided that the following conditions are met:
7  *
8  *   1. Redistributions of source code must retain the above copyright notice,
9  *      this list of conditions and the following disclaimer.
10  *
11  *   2. Redistributions in binary form must reproduce the above copyright
12  *      notice, this list of conditions and the following disclaimer in the
13  *      documentation and/or other materials provided with the distribution.
14  *
15  *   3. Neither the name of the Intel Corporation nor the names of its
16  *      contributors may be used to endorse or promote products derived from
17  *      this software without specific prior written permission.
18  *
19  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20  *  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  *  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  *  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
23  *  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  *  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  *  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  *  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  *  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  *  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  *  POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 #ifndef _VIRTCHNL_H_
33 #define _VIRTCHNL_H_
34 
35 /* Description:
36  * This header file describes the VF-PF communication protocol used
37  * by the drivers for all devices starting from our 40G product line
38  *
39  * Admin queue buffer usage:
40  * desc->opcode is always aqc_opc_send_msg_to_pf
41  * flags, retval, datalen, and data addr are all used normally.
42  * The Firmware copies the cookie fields when sending messages between the
43  * PF and VF, but uses all other fields internally. Due to this limitation,
44  * we must send all messages as "indirect", i.e. using an external buffer.
45  *
46  * All the VSI indexes are relative to the VF. Each VF can have maximum of
47  * three VSIs. All the queue indexes are relative to the VSI.  Each VF can
48  * have a maximum of sixteen queues for all of its VSIs.
49  *
50  * The PF is required to return a status code in v_retval for all messages
51  * except RESET_VF, which does not require any response. The return value
52  * is of status_code type, defined in the shared type.h.
53  *
54  * In general, VF driver initialization should roughly follow the order of
55  * these opcodes. The VF driver must first validate the API version of the
56  * PF driver, then request a reset, then get resources, then configure
57  * queues and interrupts. After these operations are complete, the VF
58  * driver may start its queues, optionally add MAC and VLAN filters, and
59  * process traffic.
60  */
61 
62 /* START GENERIC DEFINES
63  * Need to ensure the following enums and defines hold the same meaning and
64  * value in current and future projects
65  */
66 
67 /* Error Codes */
68 enum virtchnl_status_code {
69 	VIRTCHNL_STATUS_SUCCESS				= 0,
70 	VIRTCHNL_STATUS_ERR_PARAM			= -5,
71 	VIRTCHNL_STATUS_ERR_NO_MEMORY			= -18,
72 	VIRTCHNL_STATUS_ERR_OPCODE_MISMATCH		= -38,
73 	VIRTCHNL_STATUS_ERR_CQP_COMPL_ERROR		= -39,
74 	VIRTCHNL_STATUS_ERR_INVALID_VF_ID		= -40,
75 	VIRTCHNL_STATUS_ERR_ADMIN_QUEUE_ERROR		= -53,
76 	VIRTCHNL_STATUS_ERR_NOT_SUPPORTED		= -64,
77 };
78 
79 /* Backward compatibility */
80 #define VIRTCHNL_ERR_PARAM VIRTCHNL_STATUS_ERR_PARAM
81 #define VIRTCHNL_STATUS_NOT_SUPPORTED VIRTCHNL_STATUS_ERR_NOT_SUPPORTED
82 
83 #define VIRTCHNL_LINK_SPEED_2_5GB_SHIFT		0x0
84 #define VIRTCHNL_LINK_SPEED_100MB_SHIFT		0x1
85 #define VIRTCHNL_LINK_SPEED_1000MB_SHIFT	0x2
86 #define VIRTCHNL_LINK_SPEED_10GB_SHIFT		0x3
87 #define VIRTCHNL_LINK_SPEED_40GB_SHIFT		0x4
88 #define VIRTCHNL_LINK_SPEED_20GB_SHIFT		0x5
89 #define VIRTCHNL_LINK_SPEED_25GB_SHIFT		0x6
90 #define VIRTCHNL_LINK_SPEED_5GB_SHIFT		0x7
91 
92 enum virtchnl_link_speed {
93 	VIRTCHNL_LINK_SPEED_UNKNOWN	= 0,
94 	VIRTCHNL_LINK_SPEED_100MB	= BIT(VIRTCHNL_LINK_SPEED_100MB_SHIFT),
95 	VIRTCHNL_LINK_SPEED_1GB		= BIT(VIRTCHNL_LINK_SPEED_1000MB_SHIFT),
96 	VIRTCHNL_LINK_SPEED_10GB	= BIT(VIRTCHNL_LINK_SPEED_10GB_SHIFT),
97 	VIRTCHNL_LINK_SPEED_40GB	= BIT(VIRTCHNL_LINK_SPEED_40GB_SHIFT),
98 	VIRTCHNL_LINK_SPEED_20GB	= BIT(VIRTCHNL_LINK_SPEED_20GB_SHIFT),
99 	VIRTCHNL_LINK_SPEED_25GB	= BIT(VIRTCHNL_LINK_SPEED_25GB_SHIFT),
100 	VIRTCHNL_LINK_SPEED_2_5GB	= BIT(VIRTCHNL_LINK_SPEED_2_5GB_SHIFT),
101 	VIRTCHNL_LINK_SPEED_5GB		= BIT(VIRTCHNL_LINK_SPEED_5GB_SHIFT),
102 };
103 
104 /* for hsplit_0 field of Rx HMC context */
105 /* deprecated with AVF 1.0 */
106 enum virtchnl_rx_hsplit {
107 	VIRTCHNL_RX_HSPLIT_NO_SPLIT      = 0,
108 	VIRTCHNL_RX_HSPLIT_SPLIT_L2      = 1,
109 	VIRTCHNL_RX_HSPLIT_SPLIT_IP      = 2,
110 	VIRTCHNL_RX_HSPLIT_SPLIT_TCP_UDP = 4,
111 	VIRTCHNL_RX_HSPLIT_SPLIT_SCTP    = 8,
112 };
113 
114 #define VIRTCHNL_ETH_LENGTH_OF_ADDRESS	6
115 /* END GENERIC DEFINES */
116 
117 /* Opcodes for VF-PF communication. These are placed in the v_opcode field
118  * of the virtchnl_msg structure.
119  */
120 enum virtchnl_ops {
121 /* The PF sends status change events to VFs using
122  * the VIRTCHNL_OP_EVENT opcode.
123  * VFs send requests to the PF using the other ops.
124  * Use of "advanced opcode" features must be negotiated as part of capabilities
125  * exchange and are not considered part of base mode feature set.
126  */
127 	VIRTCHNL_OP_UNKNOWN = 0,
128 	VIRTCHNL_OP_VERSION = 1, /* must ALWAYS be 1 */
129 	VIRTCHNL_OP_RESET_VF = 2,
130 	VIRTCHNL_OP_GET_VF_RESOURCES = 3,
131 	VIRTCHNL_OP_CONFIG_TX_QUEUE = 4,
132 	VIRTCHNL_OP_CONFIG_RX_QUEUE = 5,
133 	VIRTCHNL_OP_CONFIG_VSI_QUEUES = 6,
134 	VIRTCHNL_OP_CONFIG_IRQ_MAP = 7,
135 	VIRTCHNL_OP_ENABLE_QUEUES = 8,
136 	VIRTCHNL_OP_DISABLE_QUEUES = 9,
137 	VIRTCHNL_OP_ADD_ETH_ADDR = 10,
138 	VIRTCHNL_OP_DEL_ETH_ADDR = 11,
139 	VIRTCHNL_OP_ADD_VLAN = 12,
140 	VIRTCHNL_OP_DEL_VLAN = 13,
141 	VIRTCHNL_OP_CONFIG_PROMISCUOUS_MODE = 14,
142 	VIRTCHNL_OP_GET_STATS = 15,
143 	VIRTCHNL_OP_RSVD = 16,
144 	VIRTCHNL_OP_EVENT = 17, /* must ALWAYS be 17 */
145 	/* opcode 19 is reserved */
146 	VIRTCHNL_OP_IWARP = 20, /* advanced opcode */
147 	VIRTCHNL_OP_CONFIG_IWARP_IRQ_MAP = 21, /* advanced opcode */
148 	VIRTCHNL_OP_RELEASE_IWARP_IRQ_MAP = 22, /* advanced opcode */
149 	VIRTCHNL_OP_CONFIG_RSS_KEY = 23,
150 	VIRTCHNL_OP_CONFIG_RSS_LUT = 24,
151 	VIRTCHNL_OP_GET_RSS_HENA_CAPS = 25,
152 	VIRTCHNL_OP_SET_RSS_HENA = 26,
153 	VIRTCHNL_OP_ENABLE_VLAN_STRIPPING = 27,
154 	VIRTCHNL_OP_DISABLE_VLAN_STRIPPING = 28,
155 	VIRTCHNL_OP_REQUEST_QUEUES = 29,
156 	VIRTCHNL_OP_ENABLE_CHANNELS = 30,
157 	VIRTCHNL_OP_DISABLE_CHANNELS = 31,
158 	VIRTCHNL_OP_ADD_CLOUD_FILTER = 32,
159 	VIRTCHNL_OP_DEL_CLOUD_FILTER = 33,
160 	/* opcode 34 is reserved */
161 	/* opcodes 39, 40, 41, 42 and 43 are reserved */
162 	/* opcode 44, 45, 46, 47, 48 and 49 are reserved */
163 
164 };
165 
166 /* These macros are used to generate compilation errors if a structure/union
167  * is not exactly the correct length. It gives a divide by zero error if the
168  * structure/union is not of the correct size, otherwise it creates an enum
169  * that is never used.
170  */
171 #define VIRTCHNL_CHECK_STRUCT_LEN(n, X) enum virtchnl_static_assert_enum_##X \
172 	{ virtchnl_static_assert_##X = (n)/((sizeof(struct X) == (n)) ? 1 : 0) }
173 #define VIRTCHNL_CHECK_UNION_LEN(n, X) enum virtchnl_static_asset_enum_##X \
174 	{ virtchnl_static_assert_##X = (n)/((sizeof(union X) == (n)) ? 1 : 0) }
175 
176 /* Virtual channel message descriptor. This overlays the admin queue
177  * descriptor. All other data is passed in external buffers.
178  */
179 
180 struct virtchnl_msg {
181 	u8 pad[8];			 /* AQ flags/opcode/len/retval fields */
182 	enum virtchnl_ops v_opcode; /* avoid confusion with desc->opcode */
183 	enum virtchnl_status_code v_retval;  /* ditto for desc->retval */
184 	u32 vfid;			 /* used by PF when sending to VF */
185 };
186 
187 VIRTCHNL_CHECK_STRUCT_LEN(20, virtchnl_msg);
188 
189 /* Message descriptions and data structures. */
190 
191 /* VIRTCHNL_OP_VERSION
192  * VF posts its version number to the PF. PF responds with its version number
193  * in the same format, along with a return code.
194  * Reply from PF has its major/minor versions also in param0 and param1.
195  * If there is a major version mismatch, then the VF cannot operate.
196  * If there is a minor version mismatch, then the VF can operate but should
197  * add a warning to the system log.
198  *
199  * This enum element MUST always be specified as == 1, regardless of other
200  * changes in the API. The PF must always respond to this message without
201  * error regardless of version mismatch.
202  */
203 #define VIRTCHNL_VERSION_MAJOR		1
204 #define VIRTCHNL_VERSION_MINOR		1
205 #define VIRTCHNL_VERSION_MINOR_NO_VF_CAPS	0
206 
207 struct virtchnl_version_info {
208 	u32 major;
209 	u32 minor;
210 };
211 
212 VIRTCHNL_CHECK_STRUCT_LEN(8, virtchnl_version_info);
213 
214 #define VF_IS_V10(_v) (((_v)->major == 1) && ((_v)->minor == 0))
215 #define VF_IS_V11(_ver) (((_ver)->major == 1) && ((_ver)->minor == 1))
216 
217 /* VIRTCHNL_OP_RESET_VF
218  * VF sends this request to PF with no parameters
219  * PF does NOT respond! VF driver must delay then poll VFGEN_RSTAT register
220  * until reset completion is indicated. The admin queue must be reinitialized
221  * after this operation.
222  *
223  * When reset is complete, PF must ensure that all queues in all VSIs associated
224  * with the VF are stopped, all queue configurations in the HMC are set to 0,
225  * and all MAC and VLAN filters (except the default MAC address) on all VSIs
226  * are cleared.
227  */
228 
229 /* VSI types that use VIRTCHNL interface for VF-PF communication. VSI_SRIOV
230  * vsi_type should always be 6 for backward compatibility. Add other fields
231  * as needed.
232  */
233 enum virtchnl_vsi_type {
234 	VIRTCHNL_VSI_TYPE_INVALID = 0,
235 	VIRTCHNL_VSI_SRIOV = 6,
236 };
237 
238 /* VIRTCHNL_OP_GET_VF_RESOURCES
239  * Version 1.0 VF sends this request to PF with no parameters
240  * Version 1.1 VF sends this request to PF with u32 bitmap of its capabilities
241  * PF responds with an indirect message containing
242  * virtchnl_vf_resource and one or more
243  * virtchnl_vsi_resource structures.
244  */
245 
246 struct virtchnl_vsi_resource {
247 	u16 vsi_id;
248 	u16 num_queue_pairs;
249 	enum virtchnl_vsi_type vsi_type;
250 	u16 qset_handle;
251 	u8 default_mac_addr[VIRTCHNL_ETH_LENGTH_OF_ADDRESS];
252 };
253 
254 VIRTCHNL_CHECK_STRUCT_LEN(16, virtchnl_vsi_resource);
255 
256 /* VF capability flags
257  * VIRTCHNL_VF_OFFLOAD_L2 flag is inclusive of base mode L2 offloads including
258  * TX/RX Checksum offloading and TSO for non-tunnelled packets.
259  */
260 #define VIRTCHNL_VF_OFFLOAD_L2			0x00000001
261 #define VIRTCHNL_VF_OFFLOAD_IWARP		0x00000002
262 #define VIRTCHNL_VF_OFFLOAD_RSVD		0x00000004
263 #define VIRTCHNL_VF_OFFLOAD_RSS_AQ		0x00000008
264 #define VIRTCHNL_VF_OFFLOAD_RSS_REG		0x00000010
265 #define VIRTCHNL_VF_OFFLOAD_WB_ON_ITR		0x00000020
266 #define VIRTCHNL_VF_OFFLOAD_REQ_QUEUES		0x00000040
267 #define VIRTCHNL_VF_OFFLOAD_CRC			0x00000080
268 #define VIRTCHNL_VF_OFFLOAD_VLAN		0x00010000
269 #define VIRTCHNL_VF_OFFLOAD_RX_POLLING		0x00020000
270 #define VIRTCHNL_VF_OFFLOAD_RSS_PCTYPE_V2	0x00040000
271 #define VIRTCHNL_VF_OFFLOAD_RSS_PF		0X00080000
272 #define VIRTCHNL_VF_OFFLOAD_ENCAP		0X00100000
273 #define VIRTCHNL_VF_OFFLOAD_ENCAP_CSUM		0X00200000
274 #define VIRTCHNL_VF_OFFLOAD_RX_ENCAP_CSUM	0X00400000
275 #define VIRTCHNL_VF_OFFLOAD_ADQ			0X00800000
276 #define VIRTCHNL_VF_OFFLOAD_ADQ_V2		0X01000000
277 #define VIRTCHNL_VF_OFFLOAD_USO			0X02000000
278 	/* 0X40000000 is reserved */
279 	/* 0X04000000, 0X08000000 and 0X10000000 are reserved */
280 	/* 0X80000000 is reserved */
281 
282 /* Define below the capability flags that are not offloads */
283 #define VIRTCHNL_VF_CAP_ADV_LINK_SPEED		0x00000080
284 #define VF_BASE_MODE_OFFLOADS (VIRTCHNL_VF_OFFLOAD_L2 | \
285 			       VIRTCHNL_VF_OFFLOAD_VLAN | \
286 			       VIRTCHNL_VF_OFFLOAD_RSS_PF)
287 
288 struct virtchnl_vf_resource {
289 	u16 num_vsis;
290 	u16 num_queue_pairs;
291 	u16 max_vectors;
292 	u16 max_mtu;
293 
294 	u32 vf_cap_flags;
295 	u32 rss_key_size;
296 	u32 rss_lut_size;
297 
298 	struct virtchnl_vsi_resource vsi_res[1];
299 };
300 
301 VIRTCHNL_CHECK_STRUCT_LEN(36, virtchnl_vf_resource);
302 
303 /* VIRTCHNL_OP_CONFIG_TX_QUEUE
304  * VF sends this message to set up parameters for one TX queue.
305  * External data buffer contains one instance of virtchnl_txq_info.
306  * PF configures requested queue and returns a status code.
307  */
308 
309 /* Tx queue config info */
310 struct virtchnl_txq_info {
311 	u16 vsi_id;
312 	u16 queue_id;
313 	u16 ring_len;		/* number of descriptors, multiple of 8 */
314 	u16 headwb_enabled; /* deprecated with AVF 1.0 */
315 	u64 dma_ring_addr;
316 	u64 dma_headwb_addr; /* deprecated with AVF 1.0 */
317 };
318 
319 VIRTCHNL_CHECK_STRUCT_LEN(24, virtchnl_txq_info);
320 
321 /* VIRTCHNL_OP_CONFIG_RX_QUEUE
322  * VF sends this message to set up parameters for one RX queue.
323  * External data buffer contains one instance of virtchnl_rxq_info.
324  * PF configures requested queue and returns a status code. The
325  * crc_disable flag disables CRC stripping on the VF. Setting
326  * the crc_disable flag to 1 will disable CRC stripping for each
327  * queue in the VF where the flag is set. The VIRTCHNL_VF_OFFLOAD_CRC
328  * offload must have been set prior to sending this info or the PF
329  * will ignore the request. This flag should be set the same for
330  * all of the queues for a VF.
331  */
332 
333 /* Rx queue config info */
334 struct virtchnl_rxq_info {
335 	u16 vsi_id;
336 	u16 queue_id;
337 	u32 ring_len;		/* number of descriptors, multiple of 32 */
338 	u16 hdr_size;
339 	u16 splithdr_enabled; /* deprecated with AVF 1.0 */
340 	u32 databuffer_size;
341 	u32 max_pkt_size;
342 	u8 crc_disable;
343 	u8 pad1[3];
344 	u64 dma_ring_addr;
345 	enum virtchnl_rx_hsplit rx_split_pos; /* deprecated with AVF 1.0 */
346 	u32 pad2;
347 };
348 
349 VIRTCHNL_CHECK_STRUCT_LEN(40, virtchnl_rxq_info);
350 
351 /* VIRTCHNL_OP_CONFIG_VSI_QUEUES
352  * VF sends this message to set parameters for active TX and RX queues
353  * associated with the specified VSI.
354  * PF configures queues and returns status.
355  * If the number of queues specified is greater than the number of queues
356  * associated with the VSI, an error is returned and no queues are configured.
357  * NOTE: The VF is not required to configure all queues in a single request.
358  * It may send multiple messages. PF drivers must correctly handle all VF
359  * requests.
360  */
361 struct virtchnl_queue_pair_info {
362 	/* NOTE: vsi_id and queue_id should be identical for both queues. */
363 	struct virtchnl_txq_info txq;
364 	struct virtchnl_rxq_info rxq;
365 };
366 
367 VIRTCHNL_CHECK_STRUCT_LEN(64, virtchnl_queue_pair_info);
368 
369 struct virtchnl_vsi_queue_config_info {
370 	u16 vsi_id;
371 	u16 num_queue_pairs;
372 	u32 pad;
373 	struct virtchnl_queue_pair_info qpair[1];
374 };
375 
376 VIRTCHNL_CHECK_STRUCT_LEN(72, virtchnl_vsi_queue_config_info);
377 
378 /* VIRTCHNL_OP_REQUEST_QUEUES
379  * VF sends this message to request the PF to allocate additional queues to
380  * this VF.  Each VF gets a guaranteed number of queues on init but asking for
381  * additional queues must be negotiated.  This is a best effort request as it
382  * is possible the PF does not have enough queues left to support the request.
383  * If the PF cannot support the number requested it will respond with the
384  * maximum number it is able to support.  If the request is successful, PF will
385  * then reset the VF to institute required changes.
386  */
387 
388 /* VF resource request */
389 struct virtchnl_vf_res_request {
390 	u16 num_queue_pairs;
391 };
392 
393 /* VIRTCHNL_OP_CONFIG_IRQ_MAP
394  * VF uses this message to map vectors to queues.
395  * The rxq_map and txq_map fields are bitmaps used to indicate which queues
396  * are to be associated with the specified vector.
397  * The "other" causes are always mapped to vector 0. The VF may not request
398  * that vector 0 be used for traffic.
399  * PF configures interrupt mapping and returns status.
400  * NOTE: due to hardware requirements, all active queues (both TX and RX)
401  * should be mapped to interrupts, even if the driver intends to operate
402  * only in polling mode. In this case the interrupt may be disabled, but
403  * the ITR timer will still run to trigger writebacks.
404  */
405 struct virtchnl_vector_map {
406 	u16 vsi_id;
407 	u16 vector_id;
408 	u16 rxq_map;
409 	u16 txq_map;
410 	u16 rxitr_idx;
411 	u16 txitr_idx;
412 };
413 
414 VIRTCHNL_CHECK_STRUCT_LEN(12, virtchnl_vector_map);
415 
416 struct virtchnl_irq_map_info {
417 	u16 num_vectors;
418 	struct virtchnl_vector_map vecmap[1];
419 };
420 
421 VIRTCHNL_CHECK_STRUCT_LEN(14, virtchnl_irq_map_info);
422 
423 /* VIRTCHNL_OP_ENABLE_QUEUES
424  * VIRTCHNL_OP_DISABLE_QUEUES
425  * VF sends these message to enable or disable TX/RX queue pairs.
426  * The queues fields are bitmaps indicating which queues to act upon.
427  * (Currently, we only support 16 queues per VF, but we make the field
428  * u32 to allow for expansion.)
429  * PF performs requested action and returns status.
430  * NOTE: The VF is not required to enable/disable all queues in a single
431  * request. It may send multiple messages.
432  * PF drivers must correctly handle all VF requests.
433  */
434 struct virtchnl_queue_select {
435 	u16 vsi_id;
436 	u16 pad;
437 	u32 rx_queues;
438 	u32 tx_queues;
439 };
440 
441 VIRTCHNL_CHECK_STRUCT_LEN(12, virtchnl_queue_select);
442 
443 /* VIRTCHNL_OP_ADD_ETH_ADDR
444  * VF sends this message in order to add one or more unicast or multicast
445  * address filters for the specified VSI.
446  * PF adds the filters and returns status.
447  */
448 
449 /* VIRTCHNL_OP_DEL_ETH_ADDR
450  * VF sends this message in order to remove one or more unicast or multicast
451  * filters for the specified VSI.
452  * PF removes the filters and returns status.
453  */
454 
455 struct virtchnl_ether_addr {
456 	u8 addr[VIRTCHNL_ETH_LENGTH_OF_ADDRESS];
457 	u8 pad[2];
458 };
459 
460 VIRTCHNL_CHECK_STRUCT_LEN(8, virtchnl_ether_addr);
461 
462 struct virtchnl_ether_addr_list {
463 	u16 vsi_id;
464 	u16 num_elements;
465 	struct virtchnl_ether_addr list[1];
466 };
467 
468 VIRTCHNL_CHECK_STRUCT_LEN(12, virtchnl_ether_addr_list);
469 
470 /* VIRTCHNL_OP_ADD_VLAN
471  * VF sends this message to add one or more VLAN tag filters for receives.
472  * PF adds the filters and returns status.
473  * If a port VLAN is configured by the PF, this operation will return an
474  * error to the VF.
475  */
476 
477 /* VIRTCHNL_OP_DEL_VLAN
478  * VF sends this message to remove one or more VLAN tag filters for receives.
479  * PF removes the filters and returns status.
480  * If a port VLAN is configured by the PF, this operation will return an
481  * error to the VF.
482  */
483 
484 struct virtchnl_vlan_filter_list {
485 	u16 vsi_id;
486 	u16 num_elements;
487 	u16 vlan_id[1];
488 };
489 
490 VIRTCHNL_CHECK_STRUCT_LEN(6, virtchnl_vlan_filter_list);
491 
492 /* VIRTCHNL_OP_CONFIG_PROMISCUOUS_MODE
493  * VF sends VSI id and flags.
494  * PF returns status code in retval.
495  * Note: we assume that broadcast accept mode is always enabled.
496  */
497 struct virtchnl_promisc_info {
498 	u16 vsi_id;
499 	u16 flags;
500 };
501 
502 VIRTCHNL_CHECK_STRUCT_LEN(4, virtchnl_promisc_info);
503 
504 #define FLAG_VF_UNICAST_PROMISC	0x00000001
505 #define FLAG_VF_MULTICAST_PROMISC	0x00000002
506 
507 /* VIRTCHNL_OP_GET_STATS
508  * VF sends this message to request stats for the selected VSI. VF uses
509  * the virtchnl_queue_select struct to specify the VSI. The queue_id
510  * field is ignored by the PF.
511  *
512  * PF replies with struct virtchnl_eth_stats in an external buffer.
513  */
514 
515 struct virtchnl_eth_stats {
516 	u64 rx_bytes;			/* received bytes */
517 	u64 rx_unicast;			/* received unicast pkts */
518 	u64 rx_multicast;		/* received multicast pkts */
519 	u64 rx_broadcast;		/* received broadcast pkts */
520 	u64 rx_discards;
521 	u64 rx_unknown_protocol;
522 	u64 tx_bytes;			/* transmitted bytes */
523 	u64 tx_unicast;			/* transmitted unicast pkts */
524 	u64 tx_multicast;		/* transmitted multicast pkts */
525 	u64 tx_broadcast;		/* transmitted broadcast pkts */
526 	u64 tx_discards;
527 	u64 tx_errors;
528 };
529 
530 /* VIRTCHNL_OP_CONFIG_RSS_KEY
531  * VIRTCHNL_OP_CONFIG_RSS_LUT
532  * VF sends these messages to configure RSS. Only supported if both PF
533  * and VF drivers set the VIRTCHNL_VF_OFFLOAD_RSS_PF bit during
534  * configuration negotiation. If this is the case, then the RSS fields in
535  * the VF resource struct are valid.
536  * Both the key and LUT are initialized to 0 by the PF, meaning that
537  * RSS is effectively disabled until set up by the VF.
538  */
539 struct virtchnl_rss_key {
540 	u16 vsi_id;
541 	u16 key_len;
542 	u8 key[1];         /* RSS hash key, packed bytes */
543 };
544 
545 VIRTCHNL_CHECK_STRUCT_LEN(6, virtchnl_rss_key);
546 
547 struct virtchnl_rss_lut {
548 	u16 vsi_id;
549 	u16 lut_entries;
550 	u8 lut[1];        /* RSS lookup table */
551 };
552 
553 VIRTCHNL_CHECK_STRUCT_LEN(6, virtchnl_rss_lut);
554 
555 /* VIRTCHNL_OP_GET_RSS_HENA_CAPS
556  * VIRTCHNL_OP_SET_RSS_HENA
557  * VF sends these messages to get and set the hash filter enable bits for RSS.
558  * By default, the PF sets these to all possible traffic types that the
559  * hardware supports. The VF can query this value if it wants to change the
560  * traffic types that are hashed by the hardware.
561  */
562 struct virtchnl_rss_hena {
563 	u64 hena;
564 };
565 
566 VIRTCHNL_CHECK_STRUCT_LEN(8, virtchnl_rss_hena);
567 
568 /* This is used by PF driver to enforce how many channels can be supported.
569  * When ADQ_V2 capability is negotiated, it will allow 16 channels otherwise
570  * PF driver will allow only max 4 channels
571  */
572 #define VIRTCHNL_MAX_ADQ_CHANNELS 4
573 #define VIRTCHNL_MAX_ADQ_V2_CHANNELS 16
574 
575 /* VIRTCHNL_OP_ENABLE_CHANNELS
576  * VIRTCHNL_OP_DISABLE_CHANNELS
577  * VF sends these messages to enable or disable channels based on
578  * the user specified queue count and queue offset for each traffic class.
579  * This struct encompasses all the information that the PF needs from
580  * VF to create a channel.
581  */
582 struct virtchnl_channel_info {
583 	u16 count; /* number of queues in a channel */
584 	u16 offset; /* queues in a channel start from 'offset' */
585 	u32 pad;
586 	u64 max_tx_rate;
587 };
588 
589 VIRTCHNL_CHECK_STRUCT_LEN(16, virtchnl_channel_info);
590 
591 struct virtchnl_tc_info {
592 	u32	num_tc;
593 	u32	pad;
594 	struct	virtchnl_channel_info list[1];
595 };
596 
597 VIRTCHNL_CHECK_STRUCT_LEN(24, virtchnl_tc_info);
598 
599 /* VIRTCHNL_ADD_CLOUD_FILTER
600  * VIRTCHNL_DEL_CLOUD_FILTER
601  * VF sends these messages to add or delete a cloud filter based on the
602  * user specified match and action filters. These structures encompass
603  * all the information that the PF needs from the VF to add/delete a
604  * cloud filter.
605  */
606 
607 struct virtchnl_l4_spec {
608 	u8	src_mac[ETH_ALEN];
609 	u8	dst_mac[ETH_ALEN];
610 	/* vlan_prio is part of this 16 bit field even from OS perspective
611 	 * vlan_id:12 is actual vlan_id, then vlanid:bit14..12 is vlan_prio
612 	 * in future, when decided to offload vlan_prio, pass that information
613 	 * as part of the "vlan_id" field, Bit14..12
614 	 */
615 	__be16	vlan_id;
616 	__be16	pad; /* reserved for future use */
617 	__be32	src_ip[4];
618 	__be32	dst_ip[4];
619 	__be16	src_port;
620 	__be16	dst_port;
621 };
622 
623 VIRTCHNL_CHECK_STRUCT_LEN(52, virtchnl_l4_spec);
624 
625 union virtchnl_flow_spec {
626 	struct	virtchnl_l4_spec tcp_spec;
627 	u8	buffer[128]; /* reserved for future use */
628 };
629 
630 VIRTCHNL_CHECK_UNION_LEN(128, virtchnl_flow_spec);
631 
632 enum virtchnl_action {
633 	/* action types */
634 	VIRTCHNL_ACTION_DROP = 0,
635 	VIRTCHNL_ACTION_TC_REDIRECT,
636 	VIRTCHNL_ACTION_PASSTHRU,
637 	VIRTCHNL_ACTION_QUEUE,
638 	VIRTCHNL_ACTION_Q_REGION,
639 	VIRTCHNL_ACTION_MARK,
640 	VIRTCHNL_ACTION_COUNT,
641 };
642 
643 enum virtchnl_flow_type {
644 	/* flow types */
645 	VIRTCHNL_TCP_V4_FLOW = 0,
646 	VIRTCHNL_TCP_V6_FLOW,
647 	VIRTCHNL_UDP_V4_FLOW,
648 	VIRTCHNL_UDP_V6_FLOW,
649 };
650 
651 struct virtchnl_filter {
652 	union	virtchnl_flow_spec data;
653 	union	virtchnl_flow_spec mask;
654 	enum	virtchnl_flow_type flow_type;
655 	enum	virtchnl_action action;
656 	u32	action_meta;
657 	u8	field_flags;
658 };
659 
660 VIRTCHNL_CHECK_STRUCT_LEN(272, virtchnl_filter);
661 
662 /* VIRTCHNL_OP_EVENT
663  * PF sends this message to inform the VF driver of events that may affect it.
664  * No direct response is expected from the VF, though it may generate other
665  * messages in response to this one.
666  */
667 enum virtchnl_event_codes {
668 	VIRTCHNL_EVENT_UNKNOWN = 0,
669 	VIRTCHNL_EVENT_LINK_CHANGE,
670 	VIRTCHNL_EVENT_RESET_IMPENDING,
671 	VIRTCHNL_EVENT_PF_DRIVER_CLOSE,
672 };
673 
674 #define PF_EVENT_SEVERITY_INFO		0
675 #define PF_EVENT_SEVERITY_ATTENTION	1
676 #define PF_EVENT_SEVERITY_ACTION_REQUIRED	2
677 #define PF_EVENT_SEVERITY_CERTAIN_DOOM	255
678 
679 struct virtchnl_pf_event {
680 	enum virtchnl_event_codes event;
681 	union {
682 		/* If the PF driver does not support the new speed reporting
683 		 * capabilities then use link_event else use link_event_adv to
684 		 * get the speed and link information. The ability to understand
685 		 * new speeds is indicated by setting the capability flag
686 		 * VIRTCHNL_VF_CAP_ADV_LINK_SPEED in vf_cap_flags parameter
687 		 * in virtchnl_vf_resource struct and can be used to determine
688 		 * which link event struct to use below.
689 		 */
690 		struct {
691 			enum virtchnl_link_speed link_speed;
692 			u8 link_status;
693 		} link_event;
694 		struct {
695 			/* link_speed provided in Mbps */
696 			u32 link_speed;
697 			u8 link_status;
698 		} link_event_adv;
699 	} event_data;
700 
701 	int severity;
702 };
703 
704 VIRTCHNL_CHECK_STRUCT_LEN(16, virtchnl_pf_event);
705 
706 /* VIRTCHNL_OP_CONFIG_IWARP_IRQ_MAP
707  * VF uses this message to request PF to map IWARP vectors to IWARP queues.
708  * The request for this originates from the VF IWARP driver through
709  * a client interface between VF LAN and VF IWARP driver.
710  * A vector could have an AEQ and CEQ attached to it although
711  * there is a single AEQ per VF IWARP instance in which case
712  * most vectors will have an INVALID_IDX for aeq and valid idx for ceq.
713  * There will never be a case where there will be multiple CEQs attached
714  * to a single vector.
715  * PF configures interrupt mapping and returns status.
716  */
717 struct virtchnl_iwarp_qv_info {
718 	u32 v_idx; /* msix_vector */
719 	u16 ceq_idx;
720 	u16 aeq_idx;
721 	u8 itr_idx;
722 };
723 
724 VIRTCHNL_CHECK_STRUCT_LEN(12, virtchnl_iwarp_qv_info);
725 
726 struct virtchnl_iwarp_qvlist_info {
727 	u32 num_vectors;
728 	struct virtchnl_iwarp_qv_info qv_info[1];
729 };
730 
731 VIRTCHNL_CHECK_STRUCT_LEN(16, virtchnl_iwarp_qvlist_info);
732 
733 /* Since VF messages are limited by u16 size, precalculate the maximum possible
734  * values of nested elements in virtchnl structures that virtual channel can
735  * possibly handle in a single message.
736  */
737 enum virtchnl_vector_limits {
738 	VIRTCHNL_OP_CONFIG_VSI_QUEUES_MAX	=
739 		((u16)(~0) - sizeof(struct virtchnl_vsi_queue_config_info)) /
740 		sizeof(struct virtchnl_queue_pair_info),
741 
742 	VIRTCHNL_OP_CONFIG_IRQ_MAP_MAX		=
743 		((u16)(~0) - sizeof(struct virtchnl_irq_map_info)) /
744 		sizeof(struct virtchnl_vector_map),
745 
746 	VIRTCHNL_OP_ADD_DEL_ETH_ADDR_MAX	=
747 		((u16)(~0) - sizeof(struct virtchnl_ether_addr_list)) /
748 		sizeof(struct virtchnl_ether_addr),
749 
750 	VIRTCHNL_OP_ADD_DEL_VLAN_MAX		=
751 		((u16)(~0) - sizeof(struct virtchnl_vlan_filter_list)) /
752 		sizeof(u16),
753 
754 	VIRTCHNL_OP_CONFIG_IWARP_IRQ_MAP_MAX	=
755 		((u16)(~0) - sizeof(struct virtchnl_iwarp_qvlist_info)) /
756 		sizeof(struct virtchnl_iwarp_qv_info),
757 
758 	VIRTCHNL_OP_ENABLE_CHANNELS_MAX		=
759 		((u16)(~0) - sizeof(struct virtchnl_tc_info)) /
760 		sizeof(struct virtchnl_channel_info),
761 };
762 
763 /* VF reset states - these are written into the RSTAT register:
764  * VFGEN_RSTAT on the VF
765  * When the PF initiates a reset, it writes 0
766  * When the reset is complete, it writes 1
767  * When the PF detects that the VF has recovered, it writes 2
768  * VF checks this register periodically to determine if a reset has occurred,
769  * then polls it to know when the reset is complete.
770  * If either the PF or VF reads the register while the hardware
771  * is in a reset state, it will return DEADBEEF, which, when masked
772  * will result in 3.
773  */
774 enum virtchnl_vfr_states {
775 	VIRTCHNL_VFR_INPROGRESS = 0,
776 	VIRTCHNL_VFR_COMPLETED,
777 	VIRTCHNL_VFR_VFACTIVE,
778 };
779 
780 /**
781  * virtchnl_vc_validate_vf_msg
782  * @ver: Virtchnl version info
783  * @v_opcode: Opcode for the message
784  * @msg: pointer to the msg buffer
785  * @msglen: msg length
786  *
787  * validate msg format against struct for each opcode
788  */
789 static inline int
790 virtchnl_vc_validate_vf_msg(struct virtchnl_version_info *ver, u32 v_opcode,
791 			    u8 *msg, u16 msglen)
792 {
793 	bool err_msg_format = false;
794 	u32 valid_len = 0;
795 
796 	/* Validate message length. */
797 	switch (v_opcode) {
798 	case VIRTCHNL_OP_VERSION:
799 		valid_len = sizeof(struct virtchnl_version_info);
800 		break;
801 	case VIRTCHNL_OP_RESET_VF:
802 		break;
803 	case VIRTCHNL_OP_GET_VF_RESOURCES:
804 		if (VF_IS_V11(ver))
805 			valid_len = sizeof(u32);
806 		break;
807 	case VIRTCHNL_OP_CONFIG_TX_QUEUE:
808 		valid_len = sizeof(struct virtchnl_txq_info);
809 		break;
810 	case VIRTCHNL_OP_CONFIG_RX_QUEUE:
811 		valid_len = sizeof(struct virtchnl_rxq_info);
812 		break;
813 	case VIRTCHNL_OP_CONFIG_VSI_QUEUES:
814 		valid_len = sizeof(struct virtchnl_vsi_queue_config_info);
815 		if (msglen >= valid_len) {
816 			struct virtchnl_vsi_queue_config_info *vqc =
817 			    (struct virtchnl_vsi_queue_config_info *)msg;
818 
819 			if (vqc->num_queue_pairs == 0 || vqc->num_queue_pairs >
820 			    VIRTCHNL_OP_CONFIG_VSI_QUEUES_MAX) {
821 				err_msg_format = true;
822 				break;
823 			}
824 
825 			valid_len += (vqc->num_queue_pairs *
826 				      sizeof(struct
827 					     virtchnl_queue_pair_info));
828 		}
829 		break;
830 	case VIRTCHNL_OP_CONFIG_IRQ_MAP:
831 		valid_len = sizeof(struct virtchnl_irq_map_info);
832 		if (msglen >= valid_len) {
833 			struct virtchnl_irq_map_info *vimi =
834 			    (struct virtchnl_irq_map_info *)msg;
835 
836 			if (vimi->num_vectors == 0 || vimi->num_vectors >
837 			    VIRTCHNL_OP_CONFIG_IRQ_MAP_MAX) {
838 				err_msg_format = true;
839 				break;
840 			}
841 
842 			valid_len += (vimi->num_vectors *
843 				      sizeof(struct virtchnl_vector_map));
844 		}
845 		break;
846 	case VIRTCHNL_OP_ENABLE_QUEUES:
847 	case VIRTCHNL_OP_DISABLE_QUEUES:
848 		valid_len = sizeof(struct virtchnl_queue_select);
849 		break;
850 	case VIRTCHNL_OP_ADD_ETH_ADDR:
851 	case VIRTCHNL_OP_DEL_ETH_ADDR:
852 		valid_len = sizeof(struct virtchnl_ether_addr_list);
853 		if (msglen >= valid_len) {
854 			struct virtchnl_ether_addr_list *veal =
855 			    (struct virtchnl_ether_addr_list *)msg;
856 
857 			if (veal->num_elements == 0 || veal->num_elements >
858 			    VIRTCHNL_OP_ADD_DEL_ETH_ADDR_MAX) {
859 				err_msg_format = true;
860 				break;
861 			}
862 
863 			valid_len += veal->num_elements *
864 			    sizeof(struct virtchnl_ether_addr);
865 		}
866 		break;
867 	case VIRTCHNL_OP_ADD_VLAN:
868 	case VIRTCHNL_OP_DEL_VLAN:
869 		valid_len = sizeof(struct virtchnl_vlan_filter_list);
870 		if (msglen >= valid_len) {
871 			struct virtchnl_vlan_filter_list *vfl =
872 			    (struct virtchnl_vlan_filter_list *)msg;
873 
874 			if (vfl->num_elements == 0 || vfl->num_elements >
875 			    VIRTCHNL_OP_ADD_DEL_VLAN_MAX) {
876 				err_msg_format = true;
877 				break;
878 			}
879 
880 			valid_len += vfl->num_elements * sizeof(u16);
881 		}
882 		break;
883 	case VIRTCHNL_OP_CONFIG_PROMISCUOUS_MODE:
884 		valid_len = sizeof(struct virtchnl_promisc_info);
885 		break;
886 	case VIRTCHNL_OP_GET_STATS:
887 		valid_len = sizeof(struct virtchnl_queue_select);
888 		break;
889 	case VIRTCHNL_OP_IWARP:
890 		/* These messages are opaque to us and will be validated in
891 		 * the RDMA client code. We just need to check for nonzero
892 		 * length. The firmware will enforce max length restrictions.
893 		 */
894 		if (msglen)
895 			valid_len = msglen;
896 		else
897 			err_msg_format = true;
898 		break;
899 	case VIRTCHNL_OP_RELEASE_IWARP_IRQ_MAP:
900 		break;
901 	case VIRTCHNL_OP_CONFIG_IWARP_IRQ_MAP:
902 		valid_len = sizeof(struct virtchnl_iwarp_qvlist_info);
903 		if (msglen >= valid_len) {
904 			struct virtchnl_iwarp_qvlist_info *qv =
905 				(struct virtchnl_iwarp_qvlist_info *)msg;
906 
907 			if (qv->num_vectors == 0 || qv->num_vectors >
908 			    VIRTCHNL_OP_CONFIG_IWARP_IRQ_MAP_MAX) {
909 				err_msg_format = true;
910 				break;
911 			}
912 
913 			valid_len += ((qv->num_vectors - 1) *
914 				sizeof(struct virtchnl_iwarp_qv_info));
915 		}
916 		break;
917 	case VIRTCHNL_OP_CONFIG_RSS_KEY:
918 		valid_len = sizeof(struct virtchnl_rss_key);
919 		if (msglen >= valid_len) {
920 			struct virtchnl_rss_key *vrk =
921 				(struct virtchnl_rss_key *)msg;
922 
923 			if (vrk->key_len == 0) {
924 				/* zero length is allowed as input */
925 				break;
926 			}
927 
928 			valid_len += vrk->key_len - 1;
929 		}
930 		break;
931 	case VIRTCHNL_OP_CONFIG_RSS_LUT:
932 		valid_len = sizeof(struct virtchnl_rss_lut);
933 		if (msglen >= valid_len) {
934 			struct virtchnl_rss_lut *vrl =
935 				(struct virtchnl_rss_lut *)msg;
936 
937 			if (vrl->lut_entries == 0) {
938 				/* zero entries is allowed as input */
939 				break;
940 			}
941 
942 			valid_len += vrl->lut_entries - 1;
943 		}
944 		break;
945 	case VIRTCHNL_OP_GET_RSS_HENA_CAPS:
946 		break;
947 	case VIRTCHNL_OP_SET_RSS_HENA:
948 		valid_len = sizeof(struct virtchnl_rss_hena);
949 		break;
950 	case VIRTCHNL_OP_ENABLE_VLAN_STRIPPING:
951 	case VIRTCHNL_OP_DISABLE_VLAN_STRIPPING:
952 		break;
953 	case VIRTCHNL_OP_REQUEST_QUEUES:
954 		valid_len = sizeof(struct virtchnl_vf_res_request);
955 		break;
956 	case VIRTCHNL_OP_ENABLE_CHANNELS:
957 		valid_len = sizeof(struct virtchnl_tc_info);
958 		if (msglen >= valid_len) {
959 			struct virtchnl_tc_info *vti =
960 				(struct virtchnl_tc_info *)msg;
961 
962 			if (vti->num_tc == 0 || vti->num_tc >
963 			    VIRTCHNL_OP_ENABLE_CHANNELS_MAX) {
964 				err_msg_format = true;
965 				break;
966 			}
967 
968 			valid_len += (vti->num_tc - 1) *
969 				     sizeof(struct virtchnl_channel_info);
970 		}
971 		break;
972 	case VIRTCHNL_OP_DISABLE_CHANNELS:
973 		break;
974 	case VIRTCHNL_OP_ADD_CLOUD_FILTER:
975 	case VIRTCHNL_OP_DEL_CLOUD_FILTER:
976 		valid_len = sizeof(struct virtchnl_filter);
977 		break;
978 	/* These are always errors coming from the VF. */
979 	case VIRTCHNL_OP_EVENT:
980 	case VIRTCHNL_OP_UNKNOWN:
981 	default:
982 		return VIRTCHNL_STATUS_ERR_PARAM;
983 	}
984 	/* few more checks */
985 	if (err_msg_format || valid_len != msglen)
986 		return VIRTCHNL_STATUS_ERR_OPCODE_MISMATCH;
987 
988 	return 0;
989 }
990 #endif /* _VIRTCHNL_H_ */
991