net/ice: fix outer L4 checksum in scalar Rx
[dpdk.git] / drivers / net / iavf / iavf_vchnl.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2017 Intel Corporation
3  */
4
5 #include <stdio.h>
6 #include <errno.h>
7 #include <stdint.h>
8 #include <string.h>
9 #include <unistd.h>
10 #include <stdarg.h>
11 #include <inttypes.h>
12 #include <rte_byteorder.h>
13 #include <rte_common.h>
14
15 #include <rte_debug.h>
16 #include <rte_alarm.h>
17 #include <rte_atomic.h>
18 #include <rte_eal.h>
19 #include <rte_ether.h>
20 #include <ethdev_driver.h>
21 #include <ethdev_pci.h>
22 #include <rte_dev.h>
23
24 #include "iavf.h"
25 #include "iavf_rxtx.h"
26
27 #define MAX_TRY_TIMES 2000
28 #define ASQ_DELAY_MS  1
29
30 static uint32_t
31 iavf_convert_link_speed(enum virtchnl_link_speed virt_link_speed)
32 {
33         uint32_t speed;
34
35         switch (virt_link_speed) {
36         case VIRTCHNL_LINK_SPEED_100MB:
37                 speed = 100;
38                 break;
39         case VIRTCHNL_LINK_SPEED_1GB:
40                 speed = 1000;
41                 break;
42         case VIRTCHNL_LINK_SPEED_10GB:
43                 speed = 10000;
44                 break;
45         case VIRTCHNL_LINK_SPEED_40GB:
46                 speed = 40000;
47                 break;
48         case VIRTCHNL_LINK_SPEED_20GB:
49                 speed = 20000;
50                 break;
51         case VIRTCHNL_LINK_SPEED_25GB:
52                 speed = 25000;
53                 break;
54         case VIRTCHNL_LINK_SPEED_2_5GB:
55                 speed = 2500;
56                 break;
57         case VIRTCHNL_LINK_SPEED_5GB:
58                 speed = 5000;
59                 break;
60         default:
61                 speed = 0;
62                 break;
63         }
64
65         return speed;
66 }
67
68 /* Read data in admin queue to get msg from pf driver */
69 static enum iavf_aq_result
70 iavf_read_msg_from_pf(struct iavf_adapter *adapter, uint16_t buf_len,
71                      uint8_t *buf)
72 {
73         struct iavf_hw *hw = IAVF_DEV_PRIVATE_TO_HW(adapter);
74         struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
75         struct iavf_arq_event_info event;
76         enum iavf_aq_result result = IAVF_MSG_NON;
77         enum virtchnl_ops opcode;
78         int ret;
79
80         event.buf_len = buf_len;
81         event.msg_buf = buf;
82         ret = iavf_clean_arq_element(hw, &event, NULL);
83         /* Can't read any msg from adminQ */
84         if (ret) {
85                 PMD_DRV_LOG(DEBUG, "Can't read msg from AQ");
86                 if (ret != IAVF_ERR_ADMIN_QUEUE_NO_WORK)
87                         result = IAVF_MSG_ERR;
88                 return result;
89         }
90
91         opcode = (enum virtchnl_ops)rte_le_to_cpu_32(event.desc.cookie_high);
92         vf->cmd_retval = (enum virtchnl_status_code)rte_le_to_cpu_32(
93                         event.desc.cookie_low);
94
95         PMD_DRV_LOG(DEBUG, "AQ from pf carries opcode %u, retval %d",
96                     opcode, vf->cmd_retval);
97
98         if (opcode == VIRTCHNL_OP_EVENT) {
99                 struct virtchnl_pf_event *vpe =
100                         (struct virtchnl_pf_event *)event.msg_buf;
101
102                 result = IAVF_MSG_SYS;
103                 switch (vpe->event) {
104                 case VIRTCHNL_EVENT_LINK_CHANGE:
105                         vf->link_up =
106                                 vpe->event_data.link_event.link_status;
107                         if (vf->vf_res->vf_cap_flags &
108                                 VIRTCHNL_VF_CAP_ADV_LINK_SPEED) {
109                                 vf->link_speed =
110                                     vpe->event_data.link_event_adv.link_speed;
111                         } else {
112                                 enum virtchnl_link_speed speed;
113                                 speed = vpe->event_data.link_event.link_speed;
114                                 vf->link_speed = iavf_convert_link_speed(speed);
115                         }
116                         iavf_dev_link_update(vf->eth_dev, 0);
117                         PMD_DRV_LOG(INFO, "Link status update:%s",
118                                         vf->link_up ? "up" : "down");
119                         break;
120                 case VIRTCHNL_EVENT_RESET_IMPENDING:
121                         vf->vf_reset = true;
122                         PMD_DRV_LOG(INFO, "VF is resetting");
123                         break;
124                 case VIRTCHNL_EVENT_PF_DRIVER_CLOSE:
125                         vf->dev_closed = true;
126                         PMD_DRV_LOG(INFO, "PF driver closed");
127                         break;
128                 default:
129                         PMD_DRV_LOG(ERR, "%s: Unknown event %d from pf",
130                                         __func__, vpe->event);
131                 }
132         }  else {
133                 /* async reply msg on command issued by vf previously */
134                 result = IAVF_MSG_CMD;
135                 if (opcode != vf->pend_cmd) {
136                         PMD_DRV_LOG(WARNING, "command mismatch, expect %u, get %u",
137                                         vf->pend_cmd, opcode);
138                         result = IAVF_MSG_ERR;
139                 }
140         }
141
142         return result;
143 }
144
145 static int
146 iavf_execute_vf_cmd(struct iavf_adapter *adapter, struct iavf_cmd_info *args,
147         int async)
148 {
149         struct iavf_hw *hw = IAVF_DEV_PRIVATE_TO_HW(adapter);
150         struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
151         enum iavf_aq_result result;
152         enum iavf_status ret;
153         int err = 0;
154         int i = 0;
155
156         if (vf->vf_reset)
157                 return -EIO;
158
159
160         if (async) {
161                 if (_atomic_set_async_response_cmd(vf, args->ops))
162                         return -1;
163         } else {
164                 if (_atomic_set_cmd(vf, args->ops))
165                         return -1;
166         }
167
168         ret = iavf_aq_send_msg_to_pf(hw, args->ops, IAVF_SUCCESS,
169                                     args->in_args, args->in_args_size, NULL);
170         if (ret) {
171                 PMD_DRV_LOG(ERR, "fail to send cmd %d", args->ops);
172                 _clear_cmd(vf);
173                 return err;
174         }
175
176         switch (args->ops) {
177         case VIRTCHNL_OP_RESET_VF:
178                 /*no need to wait for response */
179                 _clear_cmd(vf);
180                 break;
181         case VIRTCHNL_OP_VERSION:
182         case VIRTCHNL_OP_GET_VF_RESOURCES:
183         case VIRTCHNL_OP_GET_SUPPORTED_RXDIDS:
184         case VIRTCHNL_OP_GET_OFFLOAD_VLAN_V2_CAPS:
185                 /* for init virtchnl ops, need to poll the response */
186                 do {
187                         result = iavf_read_msg_from_pf(adapter, args->out_size,
188                                                    args->out_buffer);
189                         if (result == IAVF_MSG_CMD)
190                                 break;
191                         iavf_msec_delay(ASQ_DELAY_MS);
192                 } while (i++ < MAX_TRY_TIMES);
193                 if (i >= MAX_TRY_TIMES ||
194                     vf->cmd_retval != VIRTCHNL_STATUS_SUCCESS) {
195                         err = -1;
196                         PMD_DRV_LOG(ERR, "No response or return failure (%d)"
197                                     " for cmd %d", vf->cmd_retval, args->ops);
198                 }
199                 _clear_cmd(vf);
200                 break;
201         case VIRTCHNL_OP_REQUEST_QUEUES:
202                 /*
203                  * ignore async reply, only wait for system message,
204                  * vf_reset = true if get VIRTCHNL_EVENT_RESET_IMPENDING,
205                  * if not, means request queues failed.
206                  */
207                 do {
208                         result = iavf_read_msg_from_pf(adapter, args->out_size,
209                                                    args->out_buffer);
210                         if (result == IAVF_MSG_SYS && vf->vf_reset) {
211                                 break;
212                         } else if (result == IAVF_MSG_CMD ||
213                                 result == IAVF_MSG_ERR) {
214                                 err = -1;
215                                 break;
216                         }
217                         iavf_msec_delay(ASQ_DELAY_MS);
218                         /* If don't read msg or read sys event, continue */
219                 } while (i++ < MAX_TRY_TIMES);
220                 if (i >= MAX_TRY_TIMES ||
221                         vf->cmd_retval != VIRTCHNL_STATUS_SUCCESS) {
222                         err = -1;
223                         PMD_DRV_LOG(ERR, "No response or return failure (%d)"
224                                     " for cmd %d", vf->cmd_retval, args->ops);
225                 }
226                 _clear_cmd(vf);
227                 break;
228         default:
229                 /* For other virtchnl ops in running time,
230                  * wait for the cmd done flag.
231                  */
232                 do {
233                         if (vf->pend_cmd == VIRTCHNL_OP_UNKNOWN)
234                                 break;
235                         iavf_msec_delay(ASQ_DELAY_MS);
236                         /* If don't read msg or read sys event, continue */
237                 } while (i++ < MAX_TRY_TIMES);
238
239                 if (i >= MAX_TRY_TIMES) {
240                         PMD_DRV_LOG(ERR, "No response for cmd %d", args->ops);
241                         _clear_cmd(vf);
242                         err = -EIO;
243                 } else if (vf->cmd_retval ==
244                            VIRTCHNL_STATUS_ERR_NOT_SUPPORTED) {
245                         PMD_DRV_LOG(ERR, "Cmd %d not supported", args->ops);
246                         err = -ENOTSUP;
247                 } else if (vf->cmd_retval != VIRTCHNL_STATUS_SUCCESS) {
248                         PMD_DRV_LOG(ERR, "Return failure %d for cmd %d",
249                                     vf->cmd_retval, args->ops);
250                         err = -EINVAL;
251                 }
252                 break;
253         }
254
255         return err;
256 }
257
258 static void
259 iavf_handle_pf_event_msg(struct rte_eth_dev *dev, uint8_t *msg,
260                         uint16_t msglen)
261 {
262         struct iavf_adapter *adapter =
263                 IAVF_DEV_PRIVATE_TO_ADAPTER(dev->data->dev_private);
264         struct iavf_info *vf = &adapter->vf;
265         struct virtchnl_pf_event *pf_msg =
266                         (struct virtchnl_pf_event *)msg;
267
268         if (adapter->closed) {
269                 PMD_DRV_LOG(DEBUG, "Port closed");
270                 return;
271         }
272
273         if (msglen < sizeof(struct virtchnl_pf_event)) {
274                 PMD_DRV_LOG(DEBUG, "Error event");
275                 return;
276         }
277         switch (pf_msg->event) {
278         case VIRTCHNL_EVENT_RESET_IMPENDING:
279                 PMD_DRV_LOG(DEBUG, "VIRTCHNL_EVENT_RESET_IMPENDING event");
280                 vf->vf_reset = true;
281                 rte_eth_dev_callback_process(dev, RTE_ETH_EVENT_INTR_RESET,
282                                               NULL);
283                 break;
284         case VIRTCHNL_EVENT_LINK_CHANGE:
285                 PMD_DRV_LOG(DEBUG, "VIRTCHNL_EVENT_LINK_CHANGE event");
286                 vf->link_up = pf_msg->event_data.link_event.link_status;
287                 if (vf->vf_res->vf_cap_flags & VIRTCHNL_VF_CAP_ADV_LINK_SPEED) {
288                         vf->link_speed =
289                                 pf_msg->event_data.link_event_adv.link_speed;
290                 } else {
291                         enum virtchnl_link_speed speed;
292                         speed = pf_msg->event_data.link_event.link_speed;
293                         vf->link_speed = iavf_convert_link_speed(speed);
294                 }
295                 iavf_dev_link_update(dev, 0);
296                 rte_eth_dev_callback_process(dev, RTE_ETH_EVENT_INTR_LSC, NULL);
297                 break;
298         case VIRTCHNL_EVENT_PF_DRIVER_CLOSE:
299                 PMD_DRV_LOG(DEBUG, "VIRTCHNL_EVENT_PF_DRIVER_CLOSE event");
300                 break;
301         default:
302                 PMD_DRV_LOG(ERR, " unknown event received %u", pf_msg->event);
303                 break;
304         }
305 }
306
307 void
308 iavf_handle_virtchnl_msg(struct rte_eth_dev *dev)
309 {
310         struct iavf_hw *hw = IAVF_DEV_PRIVATE_TO_HW(dev->data->dev_private);
311         struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(dev->data->dev_private);
312         struct iavf_arq_event_info info;
313         uint16_t pending, aq_opc;
314         enum virtchnl_ops msg_opc;
315         enum iavf_status msg_ret;
316         int ret;
317
318         info.buf_len = IAVF_AQ_BUF_SZ;
319         if (!vf->aq_resp) {
320                 PMD_DRV_LOG(ERR, "Buffer for adminq resp should not be NULL");
321                 return;
322         }
323         info.msg_buf = vf->aq_resp;
324
325         pending = 1;
326         while (pending) {
327                 ret = iavf_clean_arq_element(hw, &info, &pending);
328
329                 if (ret != IAVF_SUCCESS) {
330                         PMD_DRV_LOG(INFO, "Failed to read msg from AdminQ,"
331                                     "ret: %d", ret);
332                         break;
333                 }
334                 aq_opc = rte_le_to_cpu_16(info.desc.opcode);
335                 /* For the message sent from pf to vf, opcode is stored in
336                  * cookie_high of struct iavf_aq_desc, while return error code
337                  * are stored in cookie_low, Which is done by PF driver.
338                  */
339                 msg_opc = (enum virtchnl_ops)rte_le_to_cpu_32(
340                                                   info.desc.cookie_high);
341                 msg_ret = (enum iavf_status)rte_le_to_cpu_32(
342                                                   info.desc.cookie_low);
343                 switch (aq_opc) {
344                 case iavf_aqc_opc_send_msg_to_vf:
345                         if (msg_opc == VIRTCHNL_OP_EVENT) {
346                                 iavf_handle_pf_event_msg(dev, info.msg_buf,
347                                                 info.msg_len);
348                         } else {
349                                 /* check for inline IPsec events */
350                                 struct inline_ipsec_msg *imsg =
351                                         (struct inline_ipsec_msg *)info.msg_buf;
352                                 struct rte_eth_event_ipsec_desc desc;
353                                 if (msg_opc ==
354                                         VIRTCHNL_OP_INLINE_IPSEC_CRYPTO &&
355                                         imsg->ipsec_opcode ==
356                                                 INLINE_IPSEC_OP_EVENT) {
357                                         struct virtchnl_ipsec_event *ev =
358                                                         imsg->ipsec_data.event;
359                                         desc.subtype =
360                                                 RTE_ETH_EVENT_IPSEC_UNKNOWN;
361                                         desc.metadata = ev->ipsec_event_data;
362                                         rte_eth_dev_callback_process(dev,
363                                                         RTE_ETH_EVENT_IPSEC,
364                                                         &desc);
365                                         return;
366                                 }
367
368                                 /* read message and it's expected one */
369                                 if (msg_opc == vf->pend_cmd) {
370                                         uint32_t cmd_count =
371                                         __atomic_sub_fetch(&vf->pend_cmd_count,
372                                                         1, __ATOMIC_RELAXED);
373                                         if (cmd_count == 0)
374                                                 _notify_cmd(vf, msg_ret);
375                                 } else {
376                                         PMD_DRV_LOG(ERR,
377                                         "command mismatch, expect %u, get %u",
378                                                 vf->pend_cmd, msg_opc);
379                                 }
380                                 PMD_DRV_LOG(DEBUG,
381                                 "adminq response is received, opcode = %d",
382                                                 msg_opc);
383                         }
384                         break;
385                 default:
386                         PMD_DRV_LOG(DEBUG, "Request %u is not supported yet",
387                                     aq_opc);
388                         break;
389                 }
390         }
391 }
392
393 int
394 iavf_enable_vlan_strip(struct iavf_adapter *adapter)
395 {
396         struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
397         struct iavf_cmd_info args;
398         int ret;
399
400         memset(&args, 0, sizeof(args));
401         args.ops = VIRTCHNL_OP_ENABLE_VLAN_STRIPPING;
402         args.in_args = NULL;
403         args.in_args_size = 0;
404         args.out_buffer = vf->aq_resp;
405         args.out_size = IAVF_AQ_BUF_SZ;
406         ret = iavf_execute_vf_cmd(adapter, &args, 0);
407         if (ret)
408                 PMD_DRV_LOG(ERR, "Failed to execute command of"
409                             " OP_ENABLE_VLAN_STRIPPING");
410
411         return ret;
412 }
413
414 int
415 iavf_disable_vlan_strip(struct iavf_adapter *adapter)
416 {
417         struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
418         struct iavf_cmd_info args;
419         int ret;
420
421         memset(&args, 0, sizeof(args));
422         args.ops = VIRTCHNL_OP_DISABLE_VLAN_STRIPPING;
423         args.in_args = NULL;
424         args.in_args_size = 0;
425         args.out_buffer = vf->aq_resp;
426         args.out_size = IAVF_AQ_BUF_SZ;
427         ret = iavf_execute_vf_cmd(adapter, &args, 0);
428         if (ret)
429                 PMD_DRV_LOG(ERR, "Failed to execute command of"
430                             " OP_DISABLE_VLAN_STRIPPING");
431
432         return ret;
433 }
434
435 #define VIRTCHNL_VERSION_MAJOR_START 1
436 #define VIRTCHNL_VERSION_MINOR_START 1
437
438 /* Check API version with sync wait until version read from admin queue */
439 int
440 iavf_check_api_version(struct iavf_adapter *adapter)
441 {
442         struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
443         struct virtchnl_version_info version, *pver;
444         struct iavf_cmd_info args;
445         int err;
446
447         version.major = VIRTCHNL_VERSION_MAJOR;
448         version.minor = VIRTCHNL_VERSION_MINOR;
449
450         args.ops = VIRTCHNL_OP_VERSION;
451         args.in_args = (uint8_t *)&version;
452         args.in_args_size = sizeof(version);
453         args.out_buffer = vf->aq_resp;
454         args.out_size = IAVF_AQ_BUF_SZ;
455
456         err = iavf_execute_vf_cmd(adapter, &args, 0);
457         if (err) {
458                 PMD_INIT_LOG(ERR, "Fail to execute command of OP_VERSION");
459                 return err;
460         }
461
462         pver = (struct virtchnl_version_info *)args.out_buffer;
463         vf->virtchnl_version = *pver;
464
465         if (vf->virtchnl_version.major < VIRTCHNL_VERSION_MAJOR_START ||
466             (vf->virtchnl_version.major == VIRTCHNL_VERSION_MAJOR_START &&
467              vf->virtchnl_version.minor < VIRTCHNL_VERSION_MINOR_START)) {
468                 PMD_INIT_LOG(ERR, "VIRTCHNL API version should not be lower"
469                              " than (%u.%u) to support Adaptive VF",
470                              VIRTCHNL_VERSION_MAJOR_START,
471                              VIRTCHNL_VERSION_MAJOR_START);
472                 return -1;
473         } else if (vf->virtchnl_version.major > VIRTCHNL_VERSION_MAJOR ||
474                    (vf->virtchnl_version.major == VIRTCHNL_VERSION_MAJOR &&
475                     vf->virtchnl_version.minor > VIRTCHNL_VERSION_MINOR)) {
476                 PMD_INIT_LOG(ERR, "PF/VF API version mismatch:(%u.%u)-(%u.%u)",
477                              vf->virtchnl_version.major,
478                              vf->virtchnl_version.minor,
479                              VIRTCHNL_VERSION_MAJOR,
480                              VIRTCHNL_VERSION_MINOR);
481                 return -1;
482         }
483
484         PMD_DRV_LOG(DEBUG, "Peer is supported PF host");
485         return 0;
486 }
487
488 int
489 iavf_get_vf_resource(struct iavf_adapter *adapter)
490 {
491         struct iavf_hw *hw = IAVF_DEV_PRIVATE_TO_HW(adapter);
492         struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
493         struct iavf_cmd_info args;
494         uint32_t caps, len;
495         int err, i;
496
497         args.ops = VIRTCHNL_OP_GET_VF_RESOURCES;
498         args.out_buffer = vf->aq_resp;
499         args.out_size = IAVF_AQ_BUF_SZ;
500
501         caps = IAVF_BASIC_OFFLOAD_CAPS | VIRTCHNL_VF_CAP_ADV_LINK_SPEED |
502                 VIRTCHNL_VF_OFFLOAD_RX_FLEX_DESC |
503                 VIRTCHNL_VF_OFFLOAD_FDIR_PF |
504                 VIRTCHNL_VF_OFFLOAD_ADV_RSS_PF |
505                 VIRTCHNL_VF_OFFLOAD_REQ_QUEUES |
506                 VIRTCHNL_VF_OFFLOAD_CRC |
507                 VIRTCHNL_VF_OFFLOAD_VLAN_V2 |
508                 VIRTCHNL_VF_LARGE_NUM_QPAIRS |
509                 VIRTCHNL_VF_OFFLOAD_QOS |
510                 VIRTCHNL_VF_OFFLOAD_INLINE_IPSEC_CRYPTO |
511                 VIRTCHNL_VF_CAP_PTP;
512
513         args.in_args = (uint8_t *)&caps;
514         args.in_args_size = sizeof(caps);
515
516         err = iavf_execute_vf_cmd(adapter, &args, 0);
517
518         if (err) {
519                 PMD_DRV_LOG(ERR,
520                             "Failed to execute command of OP_GET_VF_RESOURCE");
521                 return -1;
522         }
523
524         len =  sizeof(struct virtchnl_vf_resource) +
525                       IAVF_MAX_VF_VSI * sizeof(struct virtchnl_vsi_resource);
526
527         rte_memcpy(vf->vf_res, args.out_buffer,
528                    RTE_MIN(args.out_size, len));
529         /* parse  VF config message back from PF*/
530         iavf_vf_parse_hw_config(hw, vf->vf_res);
531         for (i = 0; i < vf->vf_res->num_vsis; i++) {
532                 if (vf->vf_res->vsi_res[i].vsi_type == VIRTCHNL_VSI_SRIOV)
533                         vf->vsi_res = &vf->vf_res->vsi_res[i];
534         }
535
536         if (!vf->vsi_res) {
537                 PMD_INIT_LOG(ERR, "no LAN VSI found");
538                 return -1;
539         }
540
541         vf->vsi.vsi_id = vf->vsi_res->vsi_id;
542         vf->vsi.nb_qps = vf->vsi_res->num_queue_pairs;
543         vf->vsi.adapter = adapter;
544
545         return 0;
546 }
547
548 int
549 iavf_get_supported_rxdid(struct iavf_adapter *adapter)
550 {
551         struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
552         struct iavf_cmd_info args;
553         int ret;
554
555         args.ops = VIRTCHNL_OP_GET_SUPPORTED_RXDIDS;
556         args.in_args = NULL;
557         args.in_args_size = 0;
558         args.out_buffer = vf->aq_resp;
559         args.out_size = IAVF_AQ_BUF_SZ;
560
561         ret = iavf_execute_vf_cmd(adapter, &args, 0);
562         if (ret) {
563                 PMD_DRV_LOG(ERR,
564                             "Failed to execute command of OP_GET_SUPPORTED_RXDIDS");
565                 return ret;
566         }
567
568         vf->supported_rxdid =
569                 ((struct virtchnl_supported_rxdids *)args.out_buffer)->supported_rxdids;
570
571         return 0;
572 }
573
574 int
575 iavf_config_vlan_strip_v2(struct iavf_adapter *adapter, bool enable)
576 {
577         struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
578         struct virtchnl_vlan_supported_caps *stripping_caps;
579         struct virtchnl_vlan_setting vlan_strip;
580         struct iavf_cmd_info args;
581         uint32_t *ethertype;
582         int ret;
583
584         stripping_caps = &vf->vlan_v2_caps.offloads.stripping_support;
585
586         if ((stripping_caps->outer & VIRTCHNL_VLAN_ETHERTYPE_8100) &&
587             (stripping_caps->outer & VIRTCHNL_VLAN_TOGGLE))
588                 ethertype = &vlan_strip.outer_ethertype_setting;
589         else if ((stripping_caps->inner & VIRTCHNL_VLAN_ETHERTYPE_8100) &&
590                  (stripping_caps->inner & VIRTCHNL_VLAN_TOGGLE))
591                 ethertype = &vlan_strip.inner_ethertype_setting;
592         else
593                 return -ENOTSUP;
594
595         memset(&vlan_strip, 0, sizeof(vlan_strip));
596         vlan_strip.vport_id = vf->vsi_res->vsi_id;
597         *ethertype = VIRTCHNL_VLAN_ETHERTYPE_8100;
598
599         args.ops = enable ? VIRTCHNL_OP_ENABLE_VLAN_STRIPPING_V2 :
600                             VIRTCHNL_OP_DISABLE_VLAN_STRIPPING_V2;
601         args.in_args = (uint8_t *)&vlan_strip;
602         args.in_args_size = sizeof(vlan_strip);
603         args.out_buffer = vf->aq_resp;
604         args.out_size = IAVF_AQ_BUF_SZ;
605         ret = iavf_execute_vf_cmd(adapter, &args, 0);
606         if (ret)
607                 PMD_DRV_LOG(ERR, "fail to execute command %s",
608                             enable ? "VIRTCHNL_OP_ENABLE_VLAN_STRIPPING_V2" :
609                                      "VIRTCHNL_OP_DISABLE_VLAN_STRIPPING_V2");
610
611         return ret;
612 }
613
614 int
615 iavf_config_vlan_insert_v2(struct iavf_adapter *adapter, bool enable)
616 {
617         struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
618         struct virtchnl_vlan_supported_caps *insertion_caps;
619         struct virtchnl_vlan_setting vlan_insert;
620         struct iavf_cmd_info args;
621         uint32_t *ethertype;
622         int ret;
623
624         insertion_caps = &vf->vlan_v2_caps.offloads.insertion_support;
625
626         if ((insertion_caps->outer & VIRTCHNL_VLAN_ETHERTYPE_8100) &&
627             (insertion_caps->outer & VIRTCHNL_VLAN_TOGGLE))
628                 ethertype = &vlan_insert.outer_ethertype_setting;
629         else if ((insertion_caps->inner & VIRTCHNL_VLAN_ETHERTYPE_8100) &&
630                  (insertion_caps->inner & VIRTCHNL_VLAN_TOGGLE))
631                 ethertype = &vlan_insert.inner_ethertype_setting;
632         else
633                 return -ENOTSUP;
634
635         memset(&vlan_insert, 0, sizeof(vlan_insert));
636         vlan_insert.vport_id = vf->vsi_res->vsi_id;
637         *ethertype = VIRTCHNL_VLAN_ETHERTYPE_8100;
638
639         args.ops = enable ? VIRTCHNL_OP_ENABLE_VLAN_INSERTION_V2 :
640                             VIRTCHNL_OP_DISABLE_VLAN_INSERTION_V2;
641         args.in_args = (uint8_t *)&vlan_insert;
642         args.in_args_size = sizeof(vlan_insert);
643         args.out_buffer = vf->aq_resp;
644         args.out_size = IAVF_AQ_BUF_SZ;
645         ret = iavf_execute_vf_cmd(adapter, &args, 0);
646         if (ret)
647                 PMD_DRV_LOG(ERR, "fail to execute command %s",
648                             enable ? "VIRTCHNL_OP_ENABLE_VLAN_INSERTION_V2" :
649                                      "VIRTCHNL_OP_DISABLE_VLAN_INSERTION_V2");
650
651         return ret;
652 }
653
654 int
655 iavf_add_del_vlan_v2(struct iavf_adapter *adapter, uint16_t vlanid, bool add)
656 {
657         struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
658         struct virtchnl_vlan_supported_caps *supported_caps;
659         struct virtchnl_vlan_filter_list_v2 vlan_filter;
660         struct virtchnl_vlan *vlan_setting;
661         struct iavf_cmd_info args;
662         uint32_t filtering_caps;
663         int err;
664
665         supported_caps = &vf->vlan_v2_caps.filtering.filtering_support;
666         if (supported_caps->outer) {
667                 filtering_caps = supported_caps->outer;
668                 vlan_setting = &vlan_filter.filters[0].outer;
669         } else {
670                 filtering_caps = supported_caps->inner;
671                 vlan_setting = &vlan_filter.filters[0].inner;
672         }
673
674         if (!(filtering_caps & VIRTCHNL_VLAN_ETHERTYPE_8100))
675                 return -ENOTSUP;
676
677         memset(&vlan_filter, 0, sizeof(vlan_filter));
678         vlan_filter.vport_id = vf->vsi_res->vsi_id;
679         vlan_filter.num_elements = 1;
680         vlan_setting->tpid = RTE_ETHER_TYPE_VLAN;
681         vlan_setting->tci = vlanid;
682
683         args.ops = add ? VIRTCHNL_OP_ADD_VLAN_V2 : VIRTCHNL_OP_DEL_VLAN_V2;
684         args.in_args = (uint8_t *)&vlan_filter;
685         args.in_args_size = sizeof(vlan_filter);
686         args.out_buffer = vf->aq_resp;
687         args.out_size = IAVF_AQ_BUF_SZ;
688         err = iavf_execute_vf_cmd(adapter, &args, 0);
689         if (err)
690                 PMD_DRV_LOG(ERR, "fail to execute command %s",
691                             add ? "OP_ADD_VLAN_V2" :  "OP_DEL_VLAN_V2");
692
693         return err;
694 }
695
696 int
697 iavf_get_vlan_offload_caps_v2(struct iavf_adapter *adapter)
698 {
699         struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
700         struct iavf_cmd_info args;
701         int ret;
702
703         args.ops = VIRTCHNL_OP_GET_OFFLOAD_VLAN_V2_CAPS;
704         args.in_args = NULL;
705         args.in_args_size = 0;
706         args.out_buffer = vf->aq_resp;
707         args.out_size = IAVF_AQ_BUF_SZ;
708
709         ret = iavf_execute_vf_cmd(adapter, &args, 0);
710         if (ret) {
711                 PMD_DRV_LOG(ERR,
712                             "Failed to execute command of VIRTCHNL_OP_GET_OFFLOAD_VLAN_V2_CAPS");
713                 return ret;
714         }
715
716         rte_memcpy(&vf->vlan_v2_caps, vf->aq_resp, sizeof(vf->vlan_v2_caps));
717
718         return 0;
719 }
720
721 int
722 iavf_enable_queues(struct iavf_adapter *adapter)
723 {
724         struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
725         struct virtchnl_queue_select queue_select;
726         struct iavf_cmd_info args;
727         int err;
728
729         memset(&queue_select, 0, sizeof(queue_select));
730         queue_select.vsi_id = vf->vsi_res->vsi_id;
731
732         queue_select.rx_queues = BIT(adapter->dev_data->nb_rx_queues) - 1;
733         queue_select.tx_queues = BIT(adapter->dev_data->nb_tx_queues) - 1;
734
735         args.ops = VIRTCHNL_OP_ENABLE_QUEUES;
736         args.in_args = (u8 *)&queue_select;
737         args.in_args_size = sizeof(queue_select);
738         args.out_buffer = vf->aq_resp;
739         args.out_size = IAVF_AQ_BUF_SZ;
740         err = iavf_execute_vf_cmd(adapter, &args, 0);
741         if (err) {
742                 PMD_DRV_LOG(ERR,
743                             "Failed to execute command of OP_ENABLE_QUEUES");
744                 return err;
745         }
746         return 0;
747 }
748
749 int
750 iavf_disable_queues(struct iavf_adapter *adapter)
751 {
752         struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
753         struct virtchnl_queue_select queue_select;
754         struct iavf_cmd_info args;
755         int err;
756
757         memset(&queue_select, 0, sizeof(queue_select));
758         queue_select.vsi_id = vf->vsi_res->vsi_id;
759
760         queue_select.rx_queues = BIT(adapter->dev_data->nb_rx_queues) - 1;
761         queue_select.tx_queues = BIT(adapter->dev_data->nb_tx_queues) - 1;
762
763         args.ops = VIRTCHNL_OP_DISABLE_QUEUES;
764         args.in_args = (u8 *)&queue_select;
765         args.in_args_size = sizeof(queue_select);
766         args.out_buffer = vf->aq_resp;
767         args.out_size = IAVF_AQ_BUF_SZ;
768         err = iavf_execute_vf_cmd(adapter, &args, 0);
769         if (err) {
770                 PMD_DRV_LOG(ERR,
771                             "Failed to execute command of OP_DISABLE_QUEUES");
772                 return err;
773         }
774         return 0;
775 }
776
777 int
778 iavf_switch_queue(struct iavf_adapter *adapter, uint16_t qid,
779                  bool rx, bool on)
780 {
781         struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
782         struct virtchnl_queue_select queue_select;
783         struct iavf_cmd_info args;
784         int err;
785
786         if (adapter->closed)
787                 return -EIO;
788
789         memset(&queue_select, 0, sizeof(queue_select));
790         queue_select.vsi_id = vf->vsi_res->vsi_id;
791         if (rx)
792                 queue_select.rx_queues |= 1 << qid;
793         else
794                 queue_select.tx_queues |= 1 << qid;
795
796         if (on)
797                 args.ops = VIRTCHNL_OP_ENABLE_QUEUES;
798         else
799                 args.ops = VIRTCHNL_OP_DISABLE_QUEUES;
800         args.in_args = (u8 *)&queue_select;
801         args.in_args_size = sizeof(queue_select);
802         args.out_buffer = vf->aq_resp;
803         args.out_size = IAVF_AQ_BUF_SZ;
804         err = iavf_execute_vf_cmd(adapter, &args, 0);
805         if (err)
806                 PMD_DRV_LOG(ERR, "Failed to execute command of %s",
807                             on ? "OP_ENABLE_QUEUES" : "OP_DISABLE_QUEUES");
808         return err;
809 }
810
811 int
812 iavf_enable_queues_lv(struct iavf_adapter *adapter)
813 {
814         struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
815         struct virtchnl_del_ena_dis_queues *queue_select;
816         struct virtchnl_queue_chunk *queue_chunk;
817         struct iavf_cmd_info args;
818         int err, len;
819
820         len = sizeof(struct virtchnl_del_ena_dis_queues) +
821                   sizeof(struct virtchnl_queue_chunk) *
822                   (IAVF_RXTX_QUEUE_CHUNKS_NUM - 1);
823         queue_select = rte_zmalloc("queue_select", len, 0);
824         if (!queue_select)
825                 return -ENOMEM;
826
827         queue_chunk = queue_select->chunks.chunks;
828         queue_select->chunks.num_chunks = IAVF_RXTX_QUEUE_CHUNKS_NUM;
829         queue_select->vport_id = vf->vsi_res->vsi_id;
830
831         queue_chunk[VIRTCHNL_QUEUE_TYPE_TX].type = VIRTCHNL_QUEUE_TYPE_TX;
832         queue_chunk[VIRTCHNL_QUEUE_TYPE_TX].start_queue_id = 0;
833         queue_chunk[VIRTCHNL_QUEUE_TYPE_TX].num_queues =
834                 adapter->dev_data->nb_tx_queues;
835
836         queue_chunk[VIRTCHNL_QUEUE_TYPE_RX].type = VIRTCHNL_QUEUE_TYPE_RX;
837         queue_chunk[VIRTCHNL_QUEUE_TYPE_RX].start_queue_id = 0;
838         queue_chunk[VIRTCHNL_QUEUE_TYPE_RX].num_queues =
839                 adapter->dev_data->nb_rx_queues;
840
841         args.ops = VIRTCHNL_OP_ENABLE_QUEUES_V2;
842         args.in_args = (u8 *)queue_select;
843         args.in_args_size = len;
844         args.out_buffer = vf->aq_resp;
845         args.out_size = IAVF_AQ_BUF_SZ;
846         err = iavf_execute_vf_cmd(adapter, &args, 0);
847         if (err)
848                 PMD_DRV_LOG(ERR,
849                             "Failed to execute command of OP_ENABLE_QUEUES_V2");
850
851         rte_free(queue_select);
852         return err;
853 }
854
855 int
856 iavf_disable_queues_lv(struct iavf_adapter *adapter)
857 {
858         struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
859         struct virtchnl_del_ena_dis_queues *queue_select;
860         struct virtchnl_queue_chunk *queue_chunk;
861         struct iavf_cmd_info args;
862         int err, len;
863
864         len = sizeof(struct virtchnl_del_ena_dis_queues) +
865                   sizeof(struct virtchnl_queue_chunk) *
866                   (IAVF_RXTX_QUEUE_CHUNKS_NUM - 1);
867         queue_select = rte_zmalloc("queue_select", len, 0);
868         if (!queue_select)
869                 return -ENOMEM;
870
871         queue_chunk = queue_select->chunks.chunks;
872         queue_select->chunks.num_chunks = IAVF_RXTX_QUEUE_CHUNKS_NUM;
873         queue_select->vport_id = vf->vsi_res->vsi_id;
874
875         queue_chunk[VIRTCHNL_QUEUE_TYPE_TX].type = VIRTCHNL_QUEUE_TYPE_TX;
876         queue_chunk[VIRTCHNL_QUEUE_TYPE_TX].start_queue_id = 0;
877         queue_chunk[VIRTCHNL_QUEUE_TYPE_TX].num_queues =
878                 adapter->dev_data->nb_tx_queues;
879
880         queue_chunk[VIRTCHNL_QUEUE_TYPE_RX].type = VIRTCHNL_QUEUE_TYPE_RX;
881         queue_chunk[VIRTCHNL_QUEUE_TYPE_RX].start_queue_id = 0;
882         queue_chunk[VIRTCHNL_QUEUE_TYPE_RX].num_queues =
883                 adapter->dev_data->nb_rx_queues;
884
885         args.ops = VIRTCHNL_OP_DISABLE_QUEUES_V2;
886         args.in_args = (u8 *)queue_select;
887         args.in_args_size = len;
888         args.out_buffer = vf->aq_resp;
889         args.out_size = IAVF_AQ_BUF_SZ;
890         err = iavf_execute_vf_cmd(adapter, &args, 0);
891         if (err)
892                 PMD_DRV_LOG(ERR,
893                             "Failed to execute command of OP_DISABLE_QUEUES_V2");
894
895         rte_free(queue_select);
896         return err;
897 }
898
899 int
900 iavf_switch_queue_lv(struct iavf_adapter *adapter, uint16_t qid,
901                  bool rx, bool on)
902 {
903         struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
904         struct virtchnl_del_ena_dis_queues *queue_select;
905         struct virtchnl_queue_chunk *queue_chunk;
906         struct iavf_cmd_info args;
907         int err, len;
908
909         len = sizeof(struct virtchnl_del_ena_dis_queues);
910         queue_select = rte_zmalloc("queue_select", len, 0);
911         if (!queue_select)
912                 return -ENOMEM;
913
914         queue_chunk = queue_select->chunks.chunks;
915         queue_select->chunks.num_chunks = 1;
916         queue_select->vport_id = vf->vsi_res->vsi_id;
917
918         if (rx) {
919                 queue_chunk->type = VIRTCHNL_QUEUE_TYPE_RX;
920                 queue_chunk->start_queue_id = qid;
921                 queue_chunk->num_queues = 1;
922         } else {
923                 queue_chunk->type = VIRTCHNL_QUEUE_TYPE_TX;
924                 queue_chunk->start_queue_id = qid;
925                 queue_chunk->num_queues = 1;
926         }
927
928         if (on)
929                 args.ops = VIRTCHNL_OP_ENABLE_QUEUES_V2;
930         else
931                 args.ops = VIRTCHNL_OP_DISABLE_QUEUES_V2;
932         args.in_args = (u8 *)queue_select;
933         args.in_args_size = len;
934         args.out_buffer = vf->aq_resp;
935         args.out_size = IAVF_AQ_BUF_SZ;
936         err = iavf_execute_vf_cmd(adapter, &args, 0);
937         if (err)
938                 PMD_DRV_LOG(ERR, "Failed to execute command of %s",
939                             on ? "OP_ENABLE_QUEUES_V2" : "OP_DISABLE_QUEUES_V2");
940
941         rte_free(queue_select);
942         return err;
943 }
944
945 int
946 iavf_configure_rss_lut(struct iavf_adapter *adapter)
947 {
948         struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
949         struct virtchnl_rss_lut *rss_lut;
950         struct iavf_cmd_info args;
951         int len, err = 0;
952
953         len = sizeof(*rss_lut) + vf->vf_res->rss_lut_size - 1;
954         rss_lut = rte_zmalloc("rss_lut", len, 0);
955         if (!rss_lut)
956                 return -ENOMEM;
957
958         rss_lut->vsi_id = vf->vsi_res->vsi_id;
959         rss_lut->lut_entries = vf->vf_res->rss_lut_size;
960         rte_memcpy(rss_lut->lut, vf->rss_lut, vf->vf_res->rss_lut_size);
961
962         args.ops = VIRTCHNL_OP_CONFIG_RSS_LUT;
963         args.in_args = (u8 *)rss_lut;
964         args.in_args_size = len;
965         args.out_buffer = vf->aq_resp;
966         args.out_size = IAVF_AQ_BUF_SZ;
967
968         err = iavf_execute_vf_cmd(adapter, &args, 0);
969         if (err)
970                 PMD_DRV_LOG(ERR,
971                             "Failed to execute command of OP_CONFIG_RSS_LUT");
972
973         rte_free(rss_lut);
974         return err;
975 }
976
977 int
978 iavf_configure_rss_key(struct iavf_adapter *adapter)
979 {
980         struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
981         struct virtchnl_rss_key *rss_key;
982         struct iavf_cmd_info args;
983         int len, err = 0;
984
985         len = sizeof(*rss_key) + vf->vf_res->rss_key_size - 1;
986         rss_key = rte_zmalloc("rss_key", len, 0);
987         if (!rss_key)
988                 return -ENOMEM;
989
990         rss_key->vsi_id = vf->vsi_res->vsi_id;
991         rss_key->key_len = vf->vf_res->rss_key_size;
992         rte_memcpy(rss_key->key, vf->rss_key, vf->vf_res->rss_key_size);
993
994         args.ops = VIRTCHNL_OP_CONFIG_RSS_KEY;
995         args.in_args = (u8 *)rss_key;
996         args.in_args_size = len;
997         args.out_buffer = vf->aq_resp;
998         args.out_size = IAVF_AQ_BUF_SZ;
999
1000         err = iavf_execute_vf_cmd(adapter, &args, 0);
1001         if (err)
1002                 PMD_DRV_LOG(ERR,
1003                             "Failed to execute command of OP_CONFIG_RSS_KEY");
1004
1005         rte_free(rss_key);
1006         return err;
1007 }
1008
1009 int
1010 iavf_configure_queues(struct iavf_adapter *adapter,
1011                 uint16_t num_queue_pairs, uint16_t index)
1012 {
1013         struct iavf_rx_queue **rxq =
1014                 (struct iavf_rx_queue **)adapter->dev_data->rx_queues;
1015         struct iavf_tx_queue **txq =
1016                 (struct iavf_tx_queue **)adapter->dev_data->tx_queues;
1017         struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
1018         struct virtchnl_vsi_queue_config_info *vc_config;
1019         struct virtchnl_queue_pair_info *vc_qp;
1020         struct iavf_cmd_info args;
1021         uint16_t i, size;
1022         int err;
1023
1024         size = sizeof(*vc_config) +
1025                sizeof(vc_config->qpair[0]) * num_queue_pairs;
1026         vc_config = rte_zmalloc("cfg_queue", size, 0);
1027         if (!vc_config)
1028                 return -ENOMEM;
1029
1030         vc_config->vsi_id = vf->vsi_res->vsi_id;
1031         vc_config->num_queue_pairs = num_queue_pairs;
1032
1033         for (i = index, vc_qp = vc_config->qpair;
1034                  i < index + num_queue_pairs;
1035              i++, vc_qp++) {
1036                 vc_qp->txq.vsi_id = vf->vsi_res->vsi_id;
1037                 vc_qp->txq.queue_id = i;
1038
1039                 /* Virtchnnl configure tx queues by pairs */
1040                 if (i < adapter->dev_data->nb_tx_queues) {
1041                         vc_qp->txq.ring_len = txq[i]->nb_tx_desc;
1042                         vc_qp->txq.dma_ring_addr = txq[i]->tx_ring_phys_addr;
1043                 }
1044
1045                 vc_qp->rxq.vsi_id = vf->vsi_res->vsi_id;
1046                 vc_qp->rxq.queue_id = i;
1047                 vc_qp->rxq.max_pkt_size = vf->max_pkt_len;
1048
1049                 if (i >= adapter->dev_data->nb_rx_queues)
1050                         continue;
1051
1052                 /* Virtchnnl configure rx queues by pairs */
1053                 vc_qp->rxq.ring_len = rxq[i]->nb_rx_desc;
1054                 vc_qp->rxq.dma_ring_addr = rxq[i]->rx_ring_phys_addr;
1055                 vc_qp->rxq.databuffer_size = rxq[i]->rx_buf_len;
1056                 vc_qp->rxq.crc_disable = rxq[i]->crc_len != 0 ? 1 : 0;
1057 #ifndef RTE_LIBRTE_IAVF_16BYTE_RX_DESC
1058                 if (vf->vf_res->vf_cap_flags &
1059                     VIRTCHNL_VF_OFFLOAD_RX_FLEX_DESC) {
1060                         if (vf->supported_rxdid & BIT(rxq[i]->rxdid)) {
1061                                 vc_qp->rxq.rxdid = rxq[i]->rxdid;
1062                                 PMD_DRV_LOG(NOTICE, "request RXDID[%d] in Queue[%d]",
1063                                             vc_qp->rxq.rxdid, i);
1064                         } else {
1065                                 PMD_DRV_LOG(NOTICE, "RXDID[%d] is not supported, "
1066                                             "request default RXDID[%d] in Queue[%d]",
1067                                             rxq[i]->rxdid, IAVF_RXDID_LEGACY_1, i);
1068                                 vc_qp->rxq.rxdid = IAVF_RXDID_LEGACY_1;
1069                         }
1070
1071                         if (vf->vf_res->vf_cap_flags & VIRTCHNL_VF_CAP_PTP &&
1072                             vf->ptp_caps & VIRTCHNL_1588_PTP_CAP_RX_TSTAMP)
1073                                 vc_qp->rxq.flags |= VIRTCHNL_PTP_RX_TSTAMP;
1074                 }
1075 #else
1076                 if (vf->vf_res->vf_cap_flags &
1077                         VIRTCHNL_VF_OFFLOAD_RX_FLEX_DESC &&
1078                         vf->supported_rxdid & BIT(IAVF_RXDID_LEGACY_0)) {
1079                         vc_qp->rxq.rxdid = IAVF_RXDID_LEGACY_0;
1080                         PMD_DRV_LOG(NOTICE, "request RXDID[%d] in Queue[%d]",
1081                                     vc_qp->rxq.rxdid, i);
1082                 } else {
1083                         PMD_DRV_LOG(ERR, "RXDID[%d] is not supported",
1084                                     IAVF_RXDID_LEGACY_0);
1085                         return -1;
1086                 }
1087 #endif
1088         }
1089
1090         memset(&args, 0, sizeof(args));
1091         args.ops = VIRTCHNL_OP_CONFIG_VSI_QUEUES;
1092         args.in_args = (uint8_t *)vc_config;
1093         args.in_args_size = size;
1094         args.out_buffer = vf->aq_resp;
1095         args.out_size = IAVF_AQ_BUF_SZ;
1096
1097         err = iavf_execute_vf_cmd(adapter, &args, 0);
1098         if (err)
1099                 PMD_DRV_LOG(ERR, "Failed to execute command of"
1100                             " VIRTCHNL_OP_CONFIG_VSI_QUEUES");
1101
1102         rte_free(vc_config);
1103         return err;
1104 }
1105
1106 int
1107 iavf_config_irq_map(struct iavf_adapter *adapter)
1108 {
1109         struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
1110         struct virtchnl_irq_map_info *map_info;
1111         struct virtchnl_vector_map *vecmap;
1112         struct iavf_cmd_info args;
1113         int len, i, err;
1114
1115         len = sizeof(struct virtchnl_irq_map_info) +
1116               sizeof(struct virtchnl_vector_map) * vf->nb_msix;
1117
1118         map_info = rte_zmalloc("map_info", len, 0);
1119         if (!map_info)
1120                 return -ENOMEM;
1121
1122         map_info->num_vectors = vf->nb_msix;
1123         for (i = 0; i < adapter->dev_data->nb_rx_queues; i++) {
1124                 vecmap =
1125                     &map_info->vecmap[vf->qv_map[i].vector_id - vf->msix_base];
1126                 vecmap->vsi_id = vf->vsi_res->vsi_id;
1127                 vecmap->rxitr_idx = IAVF_ITR_INDEX_DEFAULT;
1128                 vecmap->vector_id = vf->qv_map[i].vector_id;
1129                 vecmap->txq_map = 0;
1130                 vecmap->rxq_map |= 1 << vf->qv_map[i].queue_id;
1131         }
1132
1133         args.ops = VIRTCHNL_OP_CONFIG_IRQ_MAP;
1134         args.in_args = (u8 *)map_info;
1135         args.in_args_size = len;
1136         args.out_buffer = vf->aq_resp;
1137         args.out_size = IAVF_AQ_BUF_SZ;
1138         err = iavf_execute_vf_cmd(adapter, &args, 0);
1139         if (err)
1140                 PMD_DRV_LOG(ERR, "fail to execute command OP_CONFIG_IRQ_MAP");
1141
1142         rte_free(map_info);
1143         return err;
1144 }
1145
1146 int
1147 iavf_config_irq_map_lv(struct iavf_adapter *adapter, uint16_t num,
1148                 uint16_t index)
1149 {
1150         struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
1151         struct virtchnl_queue_vector_maps *map_info;
1152         struct virtchnl_queue_vector *qv_maps;
1153         struct iavf_cmd_info args;
1154         int len, i, err;
1155         int count = 0;
1156
1157         len = sizeof(struct virtchnl_queue_vector_maps) +
1158               sizeof(struct virtchnl_queue_vector) * (num - 1);
1159
1160         map_info = rte_zmalloc("map_info", len, 0);
1161         if (!map_info)
1162                 return -ENOMEM;
1163
1164         map_info->vport_id = vf->vsi_res->vsi_id;
1165         map_info->num_qv_maps = num;
1166         for (i = index; i < index + map_info->num_qv_maps; i++) {
1167                 qv_maps = &map_info->qv_maps[count++];
1168                 qv_maps->itr_idx = VIRTCHNL_ITR_IDX_0;
1169                 qv_maps->queue_type = VIRTCHNL_QUEUE_TYPE_RX;
1170                 qv_maps->queue_id = vf->qv_map[i].queue_id;
1171                 qv_maps->vector_id = vf->qv_map[i].vector_id;
1172         }
1173
1174         args.ops = VIRTCHNL_OP_MAP_QUEUE_VECTOR;
1175         args.in_args = (u8 *)map_info;
1176         args.in_args_size = len;
1177         args.out_buffer = vf->aq_resp;
1178         args.out_size = IAVF_AQ_BUF_SZ;
1179         err = iavf_execute_vf_cmd(adapter, &args, 0);
1180         if (err)
1181                 PMD_DRV_LOG(ERR, "fail to execute command OP_MAP_QUEUE_VECTOR");
1182
1183         rte_free(map_info);
1184         return err;
1185 }
1186
1187 void
1188 iavf_add_del_all_mac_addr(struct iavf_adapter *adapter, bool add)
1189 {
1190         struct virtchnl_ether_addr_list *list;
1191         struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
1192         struct rte_ether_addr *addr;
1193         struct iavf_cmd_info args;
1194         int len, err, i, j;
1195         int next_begin = 0;
1196         int begin = 0;
1197
1198         do {
1199                 j = 0;
1200                 len = sizeof(struct virtchnl_ether_addr_list);
1201                 for (i = begin; i < IAVF_NUM_MACADDR_MAX; i++, next_begin++) {
1202                         addr = &adapter->dev_data->mac_addrs[i];
1203                         if (rte_is_zero_ether_addr(addr))
1204                                 continue;
1205                         len += sizeof(struct virtchnl_ether_addr);
1206                         if (len >= IAVF_AQ_BUF_SZ) {
1207                                 next_begin = i + 1;
1208                                 break;
1209                         }
1210                 }
1211
1212                 list = rte_zmalloc("iavf_del_mac_buffer", len, 0);
1213                 if (!list) {
1214                         PMD_DRV_LOG(ERR, "fail to allocate memory");
1215                         return;
1216                 }
1217
1218                 for (i = begin; i < next_begin; i++) {
1219                         addr = &adapter->dev_data->mac_addrs[i];
1220                         if (rte_is_zero_ether_addr(addr))
1221                                 continue;
1222                         rte_memcpy(list->list[j].addr, addr->addr_bytes,
1223                                    sizeof(addr->addr_bytes));
1224                         list->list[j].type = (j == 0 ?
1225                                               VIRTCHNL_ETHER_ADDR_PRIMARY :
1226                                               VIRTCHNL_ETHER_ADDR_EXTRA);
1227                         PMD_DRV_LOG(DEBUG, "add/rm mac:" RTE_ETHER_ADDR_PRT_FMT,
1228                                     RTE_ETHER_ADDR_BYTES(addr));
1229                         j++;
1230                 }
1231                 list->vsi_id = vf->vsi_res->vsi_id;
1232                 list->num_elements = j;
1233                 args.ops = add ? VIRTCHNL_OP_ADD_ETH_ADDR :
1234                            VIRTCHNL_OP_DEL_ETH_ADDR;
1235                 args.in_args = (uint8_t *)list;
1236                 args.in_args_size = len;
1237                 args.out_buffer = vf->aq_resp;
1238                 args.out_size = IAVF_AQ_BUF_SZ;
1239                 err = iavf_execute_vf_cmd(adapter, &args, 0);
1240                 if (err)
1241                         PMD_DRV_LOG(ERR, "fail to execute command %s",
1242                                     add ? "OP_ADD_ETHER_ADDRESS" :
1243                                     "OP_DEL_ETHER_ADDRESS");
1244                 rte_free(list);
1245                 begin = next_begin;
1246         } while (begin < IAVF_NUM_MACADDR_MAX);
1247 }
1248
1249 int
1250 iavf_query_stats(struct iavf_adapter *adapter,
1251                 struct virtchnl_eth_stats **pstats)
1252 {
1253         struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
1254         struct virtchnl_queue_select q_stats;
1255         struct iavf_cmd_info args;
1256         int err;
1257
1258         if (adapter->closed)
1259                 return -EIO;
1260
1261         memset(&q_stats, 0, sizeof(q_stats));
1262         q_stats.vsi_id = vf->vsi_res->vsi_id;
1263         args.ops = VIRTCHNL_OP_GET_STATS;
1264         args.in_args = (uint8_t *)&q_stats;
1265         args.in_args_size = sizeof(q_stats);
1266         args.out_buffer = vf->aq_resp;
1267         args.out_size = IAVF_AQ_BUF_SZ;
1268
1269         err = iavf_execute_vf_cmd(adapter, &args, 0);
1270         if (err) {
1271                 PMD_DRV_LOG(ERR, "fail to execute command OP_GET_STATS");
1272                 *pstats = NULL;
1273                 return err;
1274         }
1275         *pstats = (struct virtchnl_eth_stats *)args.out_buffer;
1276         return 0;
1277 }
1278
1279 int
1280 iavf_config_promisc(struct iavf_adapter *adapter,
1281                    bool enable_unicast,
1282                    bool enable_multicast)
1283 {
1284         struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
1285         struct virtchnl_promisc_info promisc;
1286         struct iavf_cmd_info args;
1287         int err;
1288
1289         if (adapter->closed)
1290                 return -EIO;
1291
1292         promisc.flags = 0;
1293         promisc.vsi_id = vf->vsi_res->vsi_id;
1294
1295         if (enable_unicast)
1296                 promisc.flags |= FLAG_VF_UNICAST_PROMISC;
1297
1298         if (enable_multicast)
1299                 promisc.flags |= FLAG_VF_MULTICAST_PROMISC;
1300
1301         args.ops = VIRTCHNL_OP_CONFIG_PROMISCUOUS_MODE;
1302         args.in_args = (uint8_t *)&promisc;
1303         args.in_args_size = sizeof(promisc);
1304         args.out_buffer = vf->aq_resp;
1305         args.out_size = IAVF_AQ_BUF_SZ;
1306
1307         err = iavf_execute_vf_cmd(adapter, &args, 0);
1308
1309         if (err) {
1310                 PMD_DRV_LOG(ERR,
1311                             "fail to execute command CONFIG_PROMISCUOUS_MODE");
1312
1313                 if (err == -ENOTSUP)
1314                         return err;
1315
1316                 return -EAGAIN;
1317         }
1318
1319         vf->promisc_unicast_enabled = enable_unicast;
1320         vf->promisc_multicast_enabled = enable_multicast;
1321         return 0;
1322 }
1323
1324 int
1325 iavf_add_del_eth_addr(struct iavf_adapter *adapter, struct rte_ether_addr *addr,
1326                      bool add, uint8_t type)
1327 {
1328         struct virtchnl_ether_addr_list *list;
1329         struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
1330         uint8_t cmd_buffer[sizeof(struct virtchnl_ether_addr_list) +
1331                            sizeof(struct virtchnl_ether_addr)];
1332         struct iavf_cmd_info args;
1333         int err;
1334
1335         if (adapter->closed)
1336                 return -EIO;
1337
1338         list = (struct virtchnl_ether_addr_list *)cmd_buffer;
1339         list->vsi_id = vf->vsi_res->vsi_id;
1340         list->num_elements = 1;
1341         list->list[0].type = type;
1342         rte_memcpy(list->list[0].addr, addr->addr_bytes,
1343                    sizeof(addr->addr_bytes));
1344
1345         args.ops = add ? VIRTCHNL_OP_ADD_ETH_ADDR : VIRTCHNL_OP_DEL_ETH_ADDR;
1346         args.in_args = cmd_buffer;
1347         args.in_args_size = sizeof(cmd_buffer);
1348         args.out_buffer = vf->aq_resp;
1349         args.out_size = IAVF_AQ_BUF_SZ;
1350         err = iavf_execute_vf_cmd(adapter, &args, 0);
1351         if (err)
1352                 PMD_DRV_LOG(ERR, "fail to execute command %s",
1353                             add ? "OP_ADD_ETH_ADDR" :  "OP_DEL_ETH_ADDR");
1354         return err;
1355 }
1356
1357 int
1358 iavf_add_del_vlan(struct iavf_adapter *adapter, uint16_t vlanid, bool add)
1359 {
1360         struct virtchnl_vlan_filter_list *vlan_list;
1361         struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
1362         uint8_t cmd_buffer[sizeof(struct virtchnl_vlan_filter_list) +
1363                                                         sizeof(uint16_t)];
1364         struct iavf_cmd_info args;
1365         int err;
1366
1367         vlan_list = (struct virtchnl_vlan_filter_list *)cmd_buffer;
1368         vlan_list->vsi_id = vf->vsi_res->vsi_id;
1369         vlan_list->num_elements = 1;
1370         vlan_list->vlan_id[0] = vlanid;
1371
1372         args.ops = add ? VIRTCHNL_OP_ADD_VLAN : VIRTCHNL_OP_DEL_VLAN;
1373         args.in_args = cmd_buffer;
1374         args.in_args_size = sizeof(cmd_buffer);
1375         args.out_buffer = vf->aq_resp;
1376         args.out_size = IAVF_AQ_BUF_SZ;
1377         err = iavf_execute_vf_cmd(adapter, &args, 0);
1378         if (err)
1379                 PMD_DRV_LOG(ERR, "fail to execute command %s",
1380                             add ? "OP_ADD_VLAN" :  "OP_DEL_VLAN");
1381
1382         return err;
1383 }
1384
1385 int
1386 iavf_fdir_add(struct iavf_adapter *adapter,
1387         struct iavf_fdir_conf *filter)
1388 {
1389         struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
1390         struct virtchnl_fdir_add *fdir_ret;
1391
1392         struct iavf_cmd_info args;
1393         int err;
1394
1395         filter->add_fltr.vsi_id = vf->vsi_res->vsi_id;
1396         filter->add_fltr.validate_only = 0;
1397
1398         args.ops = VIRTCHNL_OP_ADD_FDIR_FILTER;
1399         args.in_args = (uint8_t *)(&filter->add_fltr);
1400         args.in_args_size = sizeof(*(&filter->add_fltr));
1401         args.out_buffer = vf->aq_resp;
1402         args.out_size = IAVF_AQ_BUF_SZ;
1403
1404         err = iavf_execute_vf_cmd(adapter, &args, 0);
1405         if (err) {
1406                 PMD_DRV_LOG(ERR, "fail to execute command OP_ADD_FDIR_FILTER");
1407                 return err;
1408         }
1409
1410         fdir_ret = (struct virtchnl_fdir_add *)args.out_buffer;
1411         filter->flow_id = fdir_ret->flow_id;
1412
1413         if (fdir_ret->status == VIRTCHNL_FDIR_SUCCESS) {
1414                 PMD_DRV_LOG(INFO,
1415                         "Succeed in adding rule request by PF");
1416         } else if (fdir_ret->status == VIRTCHNL_FDIR_FAILURE_RULE_NORESOURCE) {
1417                 PMD_DRV_LOG(ERR,
1418                         "Failed to add rule request due to no hw resource");
1419                 return -1;
1420         } else if (fdir_ret->status == VIRTCHNL_FDIR_FAILURE_RULE_EXIST) {
1421                 PMD_DRV_LOG(ERR,
1422                         "Failed to add rule request due to the rule is already existed");
1423                 return -1;
1424         } else if (fdir_ret->status == VIRTCHNL_FDIR_FAILURE_RULE_CONFLICT) {
1425                 PMD_DRV_LOG(ERR,
1426                         "Failed to add rule request due to the rule is conflict with existing rule");
1427                 return -1;
1428         } else if (fdir_ret->status == VIRTCHNL_FDIR_FAILURE_RULE_INVALID) {
1429                 PMD_DRV_LOG(ERR,
1430                         "Failed to add rule request due to the hw doesn't support");
1431                 return -1;
1432         } else if (fdir_ret->status == VIRTCHNL_FDIR_FAILURE_RULE_TIMEOUT) {
1433                 PMD_DRV_LOG(ERR,
1434                         "Failed to add rule request due to time out for programming");
1435                 return -1;
1436         } else {
1437                 PMD_DRV_LOG(ERR,
1438                         "Failed to add rule request due to other reasons");
1439                 return -1;
1440         }
1441
1442         return 0;
1443 };
1444
1445 int
1446 iavf_fdir_del(struct iavf_adapter *adapter,
1447         struct iavf_fdir_conf *filter)
1448 {
1449         struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
1450         struct virtchnl_fdir_del *fdir_ret;
1451
1452         struct iavf_cmd_info args;
1453         int err;
1454
1455         filter->del_fltr.vsi_id = vf->vsi_res->vsi_id;
1456         filter->del_fltr.flow_id = filter->flow_id;
1457
1458         args.ops = VIRTCHNL_OP_DEL_FDIR_FILTER;
1459         args.in_args = (uint8_t *)(&filter->del_fltr);
1460         args.in_args_size = sizeof(filter->del_fltr);
1461         args.out_buffer = vf->aq_resp;
1462         args.out_size = IAVF_AQ_BUF_SZ;
1463
1464         err = iavf_execute_vf_cmd(adapter, &args, 0);
1465         if (err) {
1466                 PMD_DRV_LOG(ERR, "fail to execute command OP_DEL_FDIR_FILTER");
1467                 return err;
1468         }
1469
1470         fdir_ret = (struct virtchnl_fdir_del *)args.out_buffer;
1471
1472         if (fdir_ret->status == VIRTCHNL_FDIR_SUCCESS) {
1473                 PMD_DRV_LOG(INFO,
1474                         "Succeed in deleting rule request by PF");
1475         } else if (fdir_ret->status == VIRTCHNL_FDIR_FAILURE_RULE_NONEXIST) {
1476                 PMD_DRV_LOG(ERR,
1477                         "Failed to delete rule request due to this rule doesn't exist");
1478                 return -1;
1479         } else if (fdir_ret->status == VIRTCHNL_FDIR_FAILURE_RULE_TIMEOUT) {
1480                 PMD_DRV_LOG(ERR,
1481                         "Failed to delete rule request due to time out for programming");
1482                 return -1;
1483         } else {
1484                 PMD_DRV_LOG(ERR,
1485                         "Failed to delete rule request due to other reasons");
1486                 return -1;
1487         }
1488
1489         return 0;
1490 };
1491
1492 int
1493 iavf_fdir_check(struct iavf_adapter *adapter,
1494                 struct iavf_fdir_conf *filter)
1495 {
1496         struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
1497         struct virtchnl_fdir_add *fdir_ret;
1498
1499         struct iavf_cmd_info args;
1500         int err;
1501
1502         filter->add_fltr.vsi_id = vf->vsi_res->vsi_id;
1503         filter->add_fltr.validate_only = 1;
1504
1505         args.ops = VIRTCHNL_OP_ADD_FDIR_FILTER;
1506         args.in_args = (uint8_t *)(&filter->add_fltr);
1507         args.in_args_size = sizeof(*(&filter->add_fltr));
1508         args.out_buffer = vf->aq_resp;
1509         args.out_size = IAVF_AQ_BUF_SZ;
1510
1511         err = iavf_execute_vf_cmd(adapter, &args, 0);
1512         if (err) {
1513                 PMD_DRV_LOG(ERR, "fail to check flow director rule");
1514                 return err;
1515         }
1516
1517         fdir_ret = (struct virtchnl_fdir_add *)args.out_buffer;
1518
1519         if (fdir_ret->status == VIRTCHNL_FDIR_SUCCESS) {
1520                 PMD_DRV_LOG(INFO,
1521                         "Succeed in checking rule request by PF");
1522         } else if (fdir_ret->status == VIRTCHNL_FDIR_FAILURE_RULE_INVALID) {
1523                 PMD_DRV_LOG(ERR,
1524                         "Failed to check rule request due to parameters validation"
1525                         " or HW doesn't support");
1526                 return -1;
1527         } else {
1528                 PMD_DRV_LOG(ERR,
1529                         "Failed to check rule request due to other reasons");
1530                 return -1;
1531         }
1532
1533         return 0;
1534 }
1535
1536 int
1537 iavf_add_del_rss_cfg(struct iavf_adapter *adapter,
1538                      struct virtchnl_rss_cfg *rss_cfg, bool add)
1539 {
1540         struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
1541         struct iavf_cmd_info args;
1542         int err;
1543
1544         memset(&args, 0, sizeof(args));
1545         args.ops = add ? VIRTCHNL_OP_ADD_RSS_CFG :
1546                 VIRTCHNL_OP_DEL_RSS_CFG;
1547         args.in_args = (u8 *)rss_cfg;
1548         args.in_args_size = sizeof(*rss_cfg);
1549         args.out_buffer = vf->aq_resp;
1550         args.out_size = IAVF_AQ_BUF_SZ;
1551
1552         err = iavf_execute_vf_cmd(adapter, &args, 0);
1553         if (err)
1554                 PMD_DRV_LOG(ERR,
1555                             "Failed to execute command of %s",
1556                             add ? "OP_ADD_RSS_CFG" :
1557                             "OP_DEL_RSS_INPUT_CFG");
1558
1559         return err;
1560 }
1561
1562 int
1563 iavf_get_hena_caps(struct iavf_adapter *adapter, uint64_t *caps)
1564 {
1565         struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
1566         struct iavf_cmd_info args;
1567         int err;
1568
1569         args.ops = VIRTCHNL_OP_GET_RSS_HENA_CAPS;
1570         args.in_args = NULL;
1571         args.in_args_size = 0;
1572         args.out_buffer = vf->aq_resp;
1573         args.out_size = IAVF_AQ_BUF_SZ;
1574
1575         err = iavf_execute_vf_cmd(adapter, &args, 0);
1576         if (err) {
1577                 PMD_DRV_LOG(ERR,
1578                             "Failed to execute command of OP_GET_RSS_HENA_CAPS");
1579                 return err;
1580         }
1581
1582         *caps = ((struct virtchnl_rss_hena *)args.out_buffer)->hena;
1583         return 0;
1584 }
1585
1586 int
1587 iavf_set_hena(struct iavf_adapter *adapter, uint64_t hena)
1588 {
1589         struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
1590         struct virtchnl_rss_hena vrh;
1591         struct iavf_cmd_info args;
1592         int err;
1593
1594         vrh.hena = hena;
1595         args.ops = VIRTCHNL_OP_SET_RSS_HENA;
1596         args.in_args = (u8 *)&vrh;
1597         args.in_args_size = sizeof(vrh);
1598         args.out_buffer = vf->aq_resp;
1599         args.out_size = IAVF_AQ_BUF_SZ;
1600
1601         err = iavf_execute_vf_cmd(adapter, &args, 0);
1602         if (err)
1603                 PMD_DRV_LOG(ERR,
1604                             "Failed to execute command of OP_SET_RSS_HENA");
1605
1606         return err;
1607 }
1608
1609 int
1610 iavf_get_qos_cap(struct iavf_adapter *adapter)
1611 {
1612         struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
1613         struct iavf_cmd_info args;
1614         uint32_t len;
1615         int err;
1616
1617         args.ops = VIRTCHNL_OP_GET_QOS_CAPS;
1618         args.in_args = NULL;
1619         args.in_args_size = 0;
1620         args.out_buffer = vf->aq_resp;
1621         args.out_size = IAVF_AQ_BUF_SZ;
1622         err = iavf_execute_vf_cmd(adapter, &args, 0);
1623
1624         if (err) {
1625                 PMD_DRV_LOG(ERR,
1626                             "Failed to execute command of OP_GET_VF_RESOURCE");
1627                 return -1;
1628         }
1629
1630         len =  sizeof(struct virtchnl_qos_cap_list) +
1631                 IAVF_MAX_TRAFFIC_CLASS * sizeof(struct virtchnl_qos_cap_elem);
1632
1633         rte_memcpy(vf->qos_cap, args.out_buffer,
1634                    RTE_MIN(args.out_size, len));
1635
1636         return 0;
1637 }
1638
1639 int iavf_set_q_tc_map(struct rte_eth_dev *dev,
1640                 struct virtchnl_queue_tc_mapping *q_tc_mapping, uint16_t size)
1641 {
1642         struct iavf_adapter *adapter =
1643                         IAVF_DEV_PRIVATE_TO_ADAPTER(dev->data->dev_private);
1644         struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(dev->data->dev_private);
1645         struct iavf_cmd_info args;
1646         int err;
1647
1648         memset(&args, 0, sizeof(args));
1649         args.ops = VIRTCHNL_OP_CONFIG_QUEUE_TC_MAP;
1650         args.in_args = (uint8_t *)q_tc_mapping;
1651         args.in_args_size = size;
1652         args.out_buffer = vf->aq_resp;
1653         args.out_size = IAVF_AQ_BUF_SZ;
1654
1655         err = iavf_execute_vf_cmd(adapter, &args, 0);
1656         if (err)
1657                 PMD_DRV_LOG(ERR, "Failed to execute command of"
1658                             " VIRTCHNL_OP_CONFIG_TC_MAP");
1659         return err;
1660 }
1661
1662 int iavf_set_q_bw(struct rte_eth_dev *dev,
1663                 struct virtchnl_queues_bw_cfg *q_bw, uint16_t size)
1664 {
1665         struct iavf_adapter *adapter =
1666                         IAVF_DEV_PRIVATE_TO_ADAPTER(dev->data->dev_private);
1667         struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(dev->data->dev_private);
1668         struct iavf_cmd_info args;
1669         int err;
1670
1671         memset(&args, 0, sizeof(args));
1672         args.ops = VIRTCHNL_OP_CONFIG_QUEUE_BW;
1673         args.in_args = (uint8_t *)q_bw;
1674         args.in_args_size = size;
1675         args.out_buffer = vf->aq_resp;
1676         args.out_size = IAVF_AQ_BUF_SZ;
1677
1678         err = iavf_execute_vf_cmd(adapter, &args, 0);
1679         if (err)
1680                 PMD_DRV_LOG(ERR, "Failed to execute command of"
1681                             " VIRTCHNL_OP_CONFIG_QUEUE_BW");
1682         return err;
1683 }
1684
1685 int
1686 iavf_add_del_mc_addr_list(struct iavf_adapter *adapter,
1687                         struct rte_ether_addr *mc_addrs,
1688                         uint32_t mc_addrs_num, bool add)
1689 {
1690         struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
1691         uint8_t cmd_buffer[sizeof(struct virtchnl_ether_addr_list) +
1692                 (IAVF_NUM_MACADDR_MAX * sizeof(struct virtchnl_ether_addr))];
1693         struct virtchnl_ether_addr_list *list;
1694         struct iavf_cmd_info args;
1695         uint32_t i;
1696         int err;
1697
1698         if (mc_addrs == NULL || mc_addrs_num == 0)
1699                 return 0;
1700
1701         list = (struct virtchnl_ether_addr_list *)cmd_buffer;
1702         list->vsi_id = vf->vsi_res->vsi_id;
1703         list->num_elements = mc_addrs_num;
1704
1705         for (i = 0; i < mc_addrs_num; i++) {
1706                 if (!IAVF_IS_MULTICAST(mc_addrs[i].addr_bytes)) {
1707                         PMD_DRV_LOG(ERR, "Invalid mac:" RTE_ETHER_ADDR_PRT_FMT,
1708                                     RTE_ETHER_ADDR_BYTES(&mc_addrs[i]));
1709                         return -EINVAL;
1710                 }
1711
1712                 memcpy(list->list[i].addr, mc_addrs[i].addr_bytes,
1713                         sizeof(list->list[i].addr));
1714                 list->list[i].type = VIRTCHNL_ETHER_ADDR_EXTRA;
1715         }
1716
1717         args.ops = add ? VIRTCHNL_OP_ADD_ETH_ADDR : VIRTCHNL_OP_DEL_ETH_ADDR;
1718         args.in_args = cmd_buffer;
1719         args.in_args_size = sizeof(struct virtchnl_ether_addr_list) +
1720                 i * sizeof(struct virtchnl_ether_addr);
1721         args.out_buffer = vf->aq_resp;
1722         args.out_size = IAVF_AQ_BUF_SZ;
1723         err = iavf_execute_vf_cmd(adapter, &args, 0);
1724
1725         if (err) {
1726                 PMD_DRV_LOG(ERR, "fail to execute command %s",
1727                         add ? "OP_ADD_ETH_ADDR" : "OP_DEL_ETH_ADDR");
1728                 return err;
1729         }
1730
1731         return 0;
1732 }
1733
1734 int
1735 iavf_request_queues(struct rte_eth_dev *dev, uint16_t num)
1736 {
1737         struct iavf_adapter *adapter =
1738                 IAVF_DEV_PRIVATE_TO_ADAPTER(dev->data->dev_private);
1739         struct iavf_info *vf =  IAVF_DEV_PRIVATE_TO_VF(adapter);
1740         struct rte_pci_device *pci_dev = RTE_ETH_DEV_TO_PCI(dev);
1741         struct virtchnl_vf_res_request vfres;
1742         struct iavf_cmd_info args;
1743         uint16_t num_queue_pairs;
1744         int err;
1745
1746         if (!(vf->vf_res->vf_cap_flags &
1747                 VIRTCHNL_VF_OFFLOAD_REQ_QUEUES)) {
1748                 PMD_DRV_LOG(ERR, "request queues not supported");
1749                 return -1;
1750         }
1751
1752         if (num == 0) {
1753                 PMD_DRV_LOG(ERR, "queue number cannot be zero");
1754                 return -1;
1755         }
1756         vfres.num_queue_pairs = num;
1757
1758         args.ops = VIRTCHNL_OP_REQUEST_QUEUES;
1759         args.in_args = (u8 *)&vfres;
1760         args.in_args_size = sizeof(vfres);
1761         args.out_buffer = vf->aq_resp;
1762         args.out_size = IAVF_AQ_BUF_SZ;
1763
1764         if (vf->vf_res->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_WB_ON_ITR) {
1765                 /* disable interrupt to avoid the admin queue message to be read
1766                  * before iavf_read_msg_from_pf.
1767                  */
1768                 rte_intr_disable(pci_dev->intr_handle);
1769                 err = iavf_execute_vf_cmd(adapter, &args, 0);
1770                 rte_intr_enable(pci_dev->intr_handle);
1771         } else {
1772                 rte_eal_alarm_cancel(iavf_dev_alarm_handler, dev);
1773                 err = iavf_execute_vf_cmd(adapter, &args, 0);
1774                 rte_eal_alarm_set(IAVF_ALARM_INTERVAL,
1775                                   iavf_dev_alarm_handler, dev);
1776         }
1777
1778         if (err) {
1779                 PMD_DRV_LOG(ERR, "fail to execute command OP_REQUEST_QUEUES");
1780                 return err;
1781         }
1782
1783         /* request queues succeeded, vf is resetting */
1784         if (vf->vf_reset) {
1785                 PMD_DRV_LOG(INFO, "vf is resetting");
1786                 return 0;
1787         }
1788
1789         /* request additional queues failed, return available number */
1790         num_queue_pairs =
1791           ((struct virtchnl_vf_res_request *)args.out_buffer)->num_queue_pairs;
1792         PMD_DRV_LOG(ERR, "request queues failed, only %u queues "
1793                 "available", num_queue_pairs);
1794
1795         return -1;
1796 }
1797
1798 int
1799 iavf_get_max_rss_queue_region(struct iavf_adapter *adapter)
1800 {
1801         struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
1802         struct iavf_cmd_info args;
1803         uint16_t qregion_width;
1804         int err;
1805
1806         args.ops = VIRTCHNL_OP_GET_MAX_RSS_QREGION;
1807         args.in_args = NULL;
1808         args.in_args_size = 0;
1809         args.out_buffer = vf->aq_resp;
1810         args.out_size = IAVF_AQ_BUF_SZ;
1811
1812         err = iavf_execute_vf_cmd(adapter, &args, 0);
1813         if (err) {
1814                 PMD_DRV_LOG(ERR, "Failed to execute command of VIRTCHNL_OP_GET_MAX_RSS_QREGION");
1815                 return err;
1816         }
1817
1818         qregion_width =
1819         ((struct virtchnl_max_rss_qregion *)args.out_buffer)->qregion_width;
1820
1821         vf->max_rss_qregion = (uint16_t)(1 << qregion_width);
1822
1823         return 0;
1824 }
1825
1826
1827
1828 int
1829 iavf_ipsec_crypto_request(struct iavf_adapter *adapter,
1830                 uint8_t *msg, size_t msg_len,
1831                 uint8_t *resp_msg, size_t resp_msg_len)
1832 {
1833         struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
1834         struct iavf_cmd_info args;
1835         int err;
1836
1837         args.ops = VIRTCHNL_OP_INLINE_IPSEC_CRYPTO;
1838         args.in_args = msg;
1839         args.in_args_size = msg_len;
1840         args.out_buffer = vf->aq_resp;
1841         args.out_size = IAVF_AQ_BUF_SZ;
1842
1843         err = iavf_execute_vf_cmd(adapter, &args, 1);
1844         if (err) {
1845                 PMD_DRV_LOG(ERR, "fail to execute command %s",
1846                                 "OP_INLINE_IPSEC_CRYPTO");
1847                 return err;
1848         }
1849
1850         memcpy(resp_msg, args.out_buffer, resp_msg_len);
1851
1852         return 0;
1853 }
1854
1855 int
1856 iavf_set_vf_quanta_size(struct iavf_adapter *adapter, u16 start_queue_id, u16 num_queues)
1857 {
1858         struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
1859         struct iavf_cmd_info args;
1860         struct virtchnl_quanta_cfg q_quanta;
1861         int err;
1862
1863         if (adapter->devargs.quanta_size == 0)
1864                 return 0;
1865
1866         q_quanta.quanta_size = adapter->devargs.quanta_size;
1867         q_quanta.queue_select.type = VIRTCHNL_QUEUE_TYPE_TX;
1868         q_quanta.queue_select.start_queue_id = start_queue_id;
1869         q_quanta.queue_select.num_queues = num_queues;
1870
1871         args.ops = VIRTCHNL_OP_CONFIG_QUANTA;
1872         args.in_args = (uint8_t *)&q_quanta;
1873         args.in_args_size = sizeof(q_quanta);
1874         args.out_buffer = vf->aq_resp;
1875         args.out_size = IAVF_AQ_BUF_SZ;
1876
1877         err = iavf_execute_vf_cmd(adapter, &args, 0);
1878         if (err) {
1879                 PMD_DRV_LOG(ERR, "Failed to execute command VIRTCHNL_OP_CONFIG_QUANTA");
1880                 return err;
1881         }
1882
1883         return 0;
1884 }
1885
1886 int
1887 iavf_get_ptp_cap(struct iavf_adapter *adapter)
1888 {
1889         struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
1890         struct virtchnl_ptp_caps ptp_caps;
1891         struct iavf_cmd_info args;
1892         int err;
1893
1894         ptp_caps.caps = VIRTCHNL_1588_PTP_CAP_RX_TSTAMP |
1895                         VIRTCHNL_1588_PTP_CAP_READ_PHC;
1896
1897         args.ops = VIRTCHNL_OP_1588_PTP_GET_CAPS;
1898         args.in_args = (uint8_t *)&ptp_caps;
1899         args.in_args_size = sizeof(ptp_caps);
1900         args.out_buffer = vf->aq_resp;
1901         args.out_size = IAVF_AQ_BUF_SZ;
1902
1903         err = iavf_execute_vf_cmd(adapter, &args, 0);
1904         if (err) {
1905                 PMD_DRV_LOG(ERR,
1906                             "Failed to execute command of OP_1588_PTP_GET_CAPS");
1907                 return err;
1908         }
1909
1910         vf->ptp_caps = ((struct virtchnl_ptp_caps *)args.out_buffer)->caps;
1911
1912         return 0;
1913 }
1914
1915 int
1916 iavf_get_phc_time(struct iavf_rx_queue *rxq)
1917 {
1918         struct iavf_adapter *adapter = rxq->vsi->adapter;
1919         struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
1920         struct virtchnl_phc_time phc_time;
1921         struct iavf_cmd_info args;
1922         int err = 0;
1923
1924         args.ops = VIRTCHNL_OP_1588_PTP_GET_TIME;
1925         args.in_args = (uint8_t *)&phc_time;
1926         args.in_args_size = sizeof(phc_time);
1927         args.out_buffer = vf->aq_resp;
1928         args.out_size = IAVF_AQ_BUF_SZ;
1929
1930         rte_spinlock_lock(&vf->phc_time_aq_lock);
1931         err = iavf_execute_vf_cmd(adapter, &args, 0);
1932         if (err) {
1933                 PMD_DRV_LOG(ERR,
1934                             "Failed to execute command of VIRTCHNL_OP_1588_PTP_GET_TIME");
1935                 goto out;
1936         }
1937         rxq->phc_time = ((struct virtchnl_phc_time *)args.out_buffer)->time;
1938
1939 out:
1940         rte_spinlock_unlock(&vf->phc_time_aq_lock);
1941         return err;
1942 }