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