net/iavf: support asynchronous virtual channel message
[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 (msglen < sizeof(struct virtchnl_pf_event)) {
269                 PMD_DRV_LOG(DEBUG, "Error event");
270                 return;
271         }
272         switch (pf_msg->event) {
273         case VIRTCHNL_EVENT_RESET_IMPENDING:
274                 PMD_DRV_LOG(DEBUG, "VIRTCHNL_EVENT_RESET_IMPENDING event");
275                 vf->vf_reset = true;
276                 rte_eth_dev_callback_process(dev, RTE_ETH_EVENT_INTR_RESET,
277                                               NULL);
278                 break;
279         case VIRTCHNL_EVENT_LINK_CHANGE:
280                 PMD_DRV_LOG(DEBUG, "VIRTCHNL_EVENT_LINK_CHANGE event");
281                 vf->link_up = pf_msg->event_data.link_event.link_status;
282                 if (vf->vf_res->vf_cap_flags & VIRTCHNL_VF_CAP_ADV_LINK_SPEED) {
283                         vf->link_speed =
284                                 pf_msg->event_data.link_event_adv.link_speed;
285                 } else {
286                         enum virtchnl_link_speed speed;
287                         speed = pf_msg->event_data.link_event.link_speed;
288                         vf->link_speed = iavf_convert_link_speed(speed);
289                 }
290                 iavf_dev_link_update(dev, 0);
291                 rte_eth_dev_callback_process(dev, RTE_ETH_EVENT_INTR_LSC, NULL);
292                 break;
293         case VIRTCHNL_EVENT_PF_DRIVER_CLOSE:
294                 PMD_DRV_LOG(DEBUG, "VIRTCHNL_EVENT_PF_DRIVER_CLOSE event");
295                 break;
296         default:
297                 PMD_DRV_LOG(ERR, " unknown event received %u", pf_msg->event);
298                 break;
299         }
300 }
301
302 void
303 iavf_handle_virtchnl_msg(struct rte_eth_dev *dev)
304 {
305         struct iavf_hw *hw = IAVF_DEV_PRIVATE_TO_HW(dev->data->dev_private);
306         struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(dev->data->dev_private);
307         struct iavf_arq_event_info info;
308         uint16_t pending, aq_opc;
309         enum virtchnl_ops msg_opc;
310         enum iavf_status msg_ret;
311         int ret;
312
313         info.buf_len = IAVF_AQ_BUF_SZ;
314         if (!vf->aq_resp) {
315                 PMD_DRV_LOG(ERR, "Buffer for adminq resp should not be NULL");
316                 return;
317         }
318         info.msg_buf = vf->aq_resp;
319
320         pending = 1;
321         while (pending) {
322                 ret = iavf_clean_arq_element(hw, &info, &pending);
323
324                 if (ret != IAVF_SUCCESS) {
325                         PMD_DRV_LOG(INFO, "Failed to read msg from AdminQ,"
326                                     "ret: %d", ret);
327                         break;
328                 }
329                 aq_opc = rte_le_to_cpu_16(info.desc.opcode);
330                 /* For the message sent from pf to vf, opcode is stored in
331                  * cookie_high of struct iavf_aq_desc, while return error code
332                  * are stored in cookie_low, Which is done by PF driver.
333                  */
334                 msg_opc = (enum virtchnl_ops)rte_le_to_cpu_32(
335                                                   info.desc.cookie_high);
336                 msg_ret = (enum iavf_status)rte_le_to_cpu_32(
337                                                   info.desc.cookie_low);
338                 switch (aq_opc) {
339                 case iavf_aqc_opc_send_msg_to_vf:
340                         if (msg_opc == VIRTCHNL_OP_EVENT) {
341                                 iavf_handle_pf_event_msg(dev, info.msg_buf,
342                                                 info.msg_len);
343                         } else {
344                                 /* check for inline IPsec events */
345                                 struct inline_ipsec_msg *imsg =
346                                         (struct inline_ipsec_msg *)info.msg_buf;
347                                 struct rte_eth_event_ipsec_desc desc;
348                                 if (msg_opc ==
349                                         VIRTCHNL_OP_INLINE_IPSEC_CRYPTO &&
350                                         imsg->ipsec_opcode ==
351                                                 INLINE_IPSEC_OP_EVENT) {
352                                         struct virtchnl_ipsec_event *ev =
353                                                         imsg->ipsec_data.event;
354                                         desc.subtype =
355                                                 RTE_ETH_EVENT_IPSEC_UNKNOWN;
356                                         desc.metadata = ev->ipsec_event_data;
357                                         rte_eth_dev_callback_process(dev,
358                                                         RTE_ETH_EVENT_IPSEC,
359                                                         &desc);
360                                         return;
361                                 }
362
363                                 /* read message and it's expected one */
364                                 if (msg_opc == vf->pend_cmd) {
365                                         uint32_t cmd_count =
366                                         __atomic_sub_fetch(&vf->pend_cmd_count,
367                                                         1, __ATOMIC_RELAXED);
368                                         if (cmd_count == 0)
369                                                 _notify_cmd(vf, msg_ret);
370                                 } else {
371                                         PMD_DRV_LOG(ERR,
372                                         "command mismatch, expect %u, get %u",
373                                                 vf->pend_cmd, msg_opc);
374                                 }
375                                 PMD_DRV_LOG(DEBUG,
376                                 "adminq response is received, opcode = %d",
377                                                 msg_opc);
378                         }
379                         break;
380                 default:
381                         PMD_DRV_LOG(DEBUG, "Request %u is not supported yet",
382                                     aq_opc);
383                         break;
384                 }
385         }
386 }
387
388 int
389 iavf_enable_vlan_strip(struct iavf_adapter *adapter)
390 {
391         struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
392         struct iavf_cmd_info args;
393         int ret;
394
395         memset(&args, 0, sizeof(args));
396         args.ops = VIRTCHNL_OP_ENABLE_VLAN_STRIPPING;
397         args.in_args = NULL;
398         args.in_args_size = 0;
399         args.out_buffer = vf->aq_resp;
400         args.out_size = IAVF_AQ_BUF_SZ;
401         ret = iavf_execute_vf_cmd(adapter, &args, 0);
402         if (ret)
403                 PMD_DRV_LOG(ERR, "Failed to execute command of"
404                             " OP_ENABLE_VLAN_STRIPPING");
405
406         return ret;
407 }
408
409 int
410 iavf_disable_vlan_strip(struct iavf_adapter *adapter)
411 {
412         struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
413         struct iavf_cmd_info args;
414         int ret;
415
416         memset(&args, 0, sizeof(args));
417         args.ops = VIRTCHNL_OP_DISABLE_VLAN_STRIPPING;
418         args.in_args = NULL;
419         args.in_args_size = 0;
420         args.out_buffer = vf->aq_resp;
421         args.out_size = IAVF_AQ_BUF_SZ;
422         ret = iavf_execute_vf_cmd(adapter, &args, 0);
423         if (ret)
424                 PMD_DRV_LOG(ERR, "Failed to execute command of"
425                             " OP_DISABLE_VLAN_STRIPPING");
426
427         return ret;
428 }
429
430 #define VIRTCHNL_VERSION_MAJOR_START 1
431 #define VIRTCHNL_VERSION_MINOR_START 1
432
433 /* Check API version with sync wait until version read from admin queue */
434 int
435 iavf_check_api_version(struct iavf_adapter *adapter)
436 {
437         struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
438         struct virtchnl_version_info version, *pver;
439         struct iavf_cmd_info args;
440         int err;
441
442         version.major = VIRTCHNL_VERSION_MAJOR;
443         version.minor = VIRTCHNL_VERSION_MINOR;
444
445         args.ops = VIRTCHNL_OP_VERSION;
446         args.in_args = (uint8_t *)&version;
447         args.in_args_size = sizeof(version);
448         args.out_buffer = vf->aq_resp;
449         args.out_size = IAVF_AQ_BUF_SZ;
450
451         err = iavf_execute_vf_cmd(adapter, &args, 0);
452         if (err) {
453                 PMD_INIT_LOG(ERR, "Fail to execute command of OP_VERSION");
454                 return err;
455         }
456
457         pver = (struct virtchnl_version_info *)args.out_buffer;
458         vf->virtchnl_version = *pver;
459
460         if (vf->virtchnl_version.major < VIRTCHNL_VERSION_MAJOR_START ||
461             (vf->virtchnl_version.major == VIRTCHNL_VERSION_MAJOR_START &&
462              vf->virtchnl_version.minor < VIRTCHNL_VERSION_MINOR_START)) {
463                 PMD_INIT_LOG(ERR, "VIRTCHNL API version should not be lower"
464                              " than (%u.%u) to support Adapative VF",
465                              VIRTCHNL_VERSION_MAJOR_START,
466                              VIRTCHNL_VERSION_MAJOR_START);
467                 return -1;
468         } else if (vf->virtchnl_version.major > VIRTCHNL_VERSION_MAJOR ||
469                    (vf->virtchnl_version.major == VIRTCHNL_VERSION_MAJOR &&
470                     vf->virtchnl_version.minor > VIRTCHNL_VERSION_MINOR)) {
471                 PMD_INIT_LOG(ERR, "PF/VF API version mismatch:(%u.%u)-(%u.%u)",
472                              vf->virtchnl_version.major,
473                              vf->virtchnl_version.minor,
474                              VIRTCHNL_VERSION_MAJOR,
475                              VIRTCHNL_VERSION_MINOR);
476                 return -1;
477         }
478
479         PMD_DRV_LOG(DEBUG, "Peer is supported PF host");
480         return 0;
481 }
482
483 int
484 iavf_get_vf_resource(struct iavf_adapter *adapter)
485 {
486         struct iavf_hw *hw = IAVF_DEV_PRIVATE_TO_HW(adapter);
487         struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
488         struct iavf_cmd_info args;
489         uint32_t caps, len;
490         int err, i;
491
492         args.ops = VIRTCHNL_OP_GET_VF_RESOURCES;
493         args.out_buffer = vf->aq_resp;
494         args.out_size = IAVF_AQ_BUF_SZ;
495
496         caps = IAVF_BASIC_OFFLOAD_CAPS | VIRTCHNL_VF_CAP_ADV_LINK_SPEED |
497                 VIRTCHNL_VF_OFFLOAD_RX_FLEX_DESC |
498                 VIRTCHNL_VF_OFFLOAD_FDIR_PF |
499                 VIRTCHNL_VF_OFFLOAD_ADV_RSS_PF |
500                 VIRTCHNL_VF_OFFLOAD_REQ_QUEUES |
501                 VIRTCHNL_VF_OFFLOAD_CRC |
502                 VIRTCHNL_VF_OFFLOAD_VLAN_V2 |
503                 VIRTCHNL_VF_LARGE_NUM_QPAIRS |
504                 VIRTCHNL_VF_OFFLOAD_QOS |
505 +               VIRTCHNL_VF_OFFLOAD_INLINE_IPSEC_CRYPTO;
506
507         args.in_args = (uint8_t *)&caps;
508         args.in_args_size = sizeof(caps);
509
510         err = iavf_execute_vf_cmd(adapter, &args, 0);
511
512         if (err) {
513                 PMD_DRV_LOG(ERR,
514                             "Failed to execute command of OP_GET_VF_RESOURCE");
515                 return -1;
516         }
517
518         len =  sizeof(struct virtchnl_vf_resource) +
519                       IAVF_MAX_VF_VSI * sizeof(struct virtchnl_vsi_resource);
520
521         rte_memcpy(vf->vf_res, args.out_buffer,
522                    RTE_MIN(args.out_size, len));
523         /* parse  VF config message back from PF*/
524         iavf_vf_parse_hw_config(hw, vf->vf_res);
525         for (i = 0; i < vf->vf_res->num_vsis; i++) {
526                 if (vf->vf_res->vsi_res[i].vsi_type == VIRTCHNL_VSI_SRIOV)
527                         vf->vsi_res = &vf->vf_res->vsi_res[i];
528         }
529
530         if (!vf->vsi_res) {
531                 PMD_INIT_LOG(ERR, "no LAN VSI found");
532                 return -1;
533         }
534
535         vf->vsi.vsi_id = vf->vsi_res->vsi_id;
536         vf->vsi.nb_qps = vf->vsi_res->num_queue_pairs;
537         vf->vsi.adapter = adapter;
538
539         return 0;
540 }
541
542 int
543 iavf_get_supported_rxdid(struct iavf_adapter *adapter)
544 {
545         struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
546         struct iavf_cmd_info args;
547         int ret;
548
549         args.ops = VIRTCHNL_OP_GET_SUPPORTED_RXDIDS;
550         args.in_args = NULL;
551         args.in_args_size = 0;
552         args.out_buffer = vf->aq_resp;
553         args.out_size = IAVF_AQ_BUF_SZ;
554
555         ret = iavf_execute_vf_cmd(adapter, &args, 0);
556         if (ret) {
557                 PMD_DRV_LOG(ERR,
558                             "Failed to execute command of OP_GET_SUPPORTED_RXDIDS");
559                 return ret;
560         }
561
562         vf->supported_rxdid =
563                 ((struct virtchnl_supported_rxdids *)args.out_buffer)->supported_rxdids;
564
565         return 0;
566 }
567
568 int
569 iavf_config_vlan_strip_v2(struct iavf_adapter *adapter, bool enable)
570 {
571         struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
572         struct virtchnl_vlan_supported_caps *stripping_caps;
573         struct virtchnl_vlan_setting vlan_strip;
574         struct iavf_cmd_info args;
575         uint32_t *ethertype;
576         int ret;
577
578         stripping_caps = &vf->vlan_v2_caps.offloads.stripping_support;
579
580         if ((stripping_caps->outer & VIRTCHNL_VLAN_ETHERTYPE_8100) &&
581             (stripping_caps->outer & VIRTCHNL_VLAN_TOGGLE))
582                 ethertype = &vlan_strip.outer_ethertype_setting;
583         else if ((stripping_caps->inner & VIRTCHNL_VLAN_ETHERTYPE_8100) &&
584                  (stripping_caps->inner & VIRTCHNL_VLAN_TOGGLE))
585                 ethertype = &vlan_strip.inner_ethertype_setting;
586         else
587                 return -ENOTSUP;
588
589         memset(&vlan_strip, 0, sizeof(vlan_strip));
590         vlan_strip.vport_id = vf->vsi_res->vsi_id;
591         *ethertype = VIRTCHNL_VLAN_ETHERTYPE_8100;
592
593         args.ops = enable ? VIRTCHNL_OP_ENABLE_VLAN_STRIPPING_V2 :
594                             VIRTCHNL_OP_DISABLE_VLAN_STRIPPING_V2;
595         args.in_args = (uint8_t *)&vlan_strip;
596         args.in_args_size = sizeof(vlan_strip);
597         args.out_buffer = vf->aq_resp;
598         args.out_size = IAVF_AQ_BUF_SZ;
599         ret = iavf_execute_vf_cmd(adapter, &args, 0);
600         if (ret)
601                 PMD_DRV_LOG(ERR, "fail to execute command %s",
602                             enable ? "VIRTCHNL_OP_ENABLE_VLAN_STRIPPING_V2" :
603                                      "VIRTCHNL_OP_DISABLE_VLAN_STRIPPING_V2");
604
605         return ret;
606 }
607
608 int
609 iavf_config_vlan_insert_v2(struct iavf_adapter *adapter, bool enable)
610 {
611         struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
612         struct virtchnl_vlan_supported_caps *insertion_caps;
613         struct virtchnl_vlan_setting vlan_insert;
614         struct iavf_cmd_info args;
615         uint32_t *ethertype;
616         int ret;
617
618         insertion_caps = &vf->vlan_v2_caps.offloads.insertion_support;
619
620         if ((insertion_caps->outer & VIRTCHNL_VLAN_ETHERTYPE_8100) &&
621             (insertion_caps->outer & VIRTCHNL_VLAN_TOGGLE))
622                 ethertype = &vlan_insert.outer_ethertype_setting;
623         else if ((insertion_caps->inner & VIRTCHNL_VLAN_ETHERTYPE_8100) &&
624                  (insertion_caps->inner & VIRTCHNL_VLAN_TOGGLE))
625                 ethertype = &vlan_insert.inner_ethertype_setting;
626         else
627                 return -ENOTSUP;
628
629         memset(&vlan_insert, 0, sizeof(vlan_insert));
630         vlan_insert.vport_id = vf->vsi_res->vsi_id;
631         *ethertype = VIRTCHNL_VLAN_ETHERTYPE_8100;
632
633         args.ops = enable ? VIRTCHNL_OP_ENABLE_VLAN_INSERTION_V2 :
634                             VIRTCHNL_OP_DISABLE_VLAN_INSERTION_V2;
635         args.in_args = (uint8_t *)&vlan_insert;
636         args.in_args_size = sizeof(vlan_insert);
637         args.out_buffer = vf->aq_resp;
638         args.out_size = IAVF_AQ_BUF_SZ;
639         ret = iavf_execute_vf_cmd(adapter, &args, 0);
640         if (ret)
641                 PMD_DRV_LOG(ERR, "fail to execute command %s",
642                             enable ? "VIRTCHNL_OP_ENABLE_VLAN_INSERTION_V2" :
643                                      "VIRTCHNL_OP_DISABLE_VLAN_INSERTION_V2");
644
645         return ret;
646 }
647
648 int
649 iavf_add_del_vlan_v2(struct iavf_adapter *adapter, uint16_t vlanid, bool add)
650 {
651         struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
652         struct virtchnl_vlan_supported_caps *supported_caps;
653         struct virtchnl_vlan_filter_list_v2 vlan_filter;
654         struct virtchnl_vlan *vlan_setting;
655         struct iavf_cmd_info args;
656         uint32_t filtering_caps;
657         int err;
658
659         supported_caps = &vf->vlan_v2_caps.filtering.filtering_support;
660         if (supported_caps->outer) {
661                 filtering_caps = supported_caps->outer;
662                 vlan_setting = &vlan_filter.filters[0].outer;
663         } else {
664                 filtering_caps = supported_caps->inner;
665                 vlan_setting = &vlan_filter.filters[0].inner;
666         }
667
668         if (!(filtering_caps & VIRTCHNL_VLAN_ETHERTYPE_8100))
669                 return -ENOTSUP;
670
671         memset(&vlan_filter, 0, sizeof(vlan_filter));
672         vlan_filter.vport_id = vf->vsi_res->vsi_id;
673         vlan_filter.num_elements = 1;
674         vlan_setting->tpid = RTE_ETHER_TYPE_VLAN;
675         vlan_setting->tci = vlanid;
676
677         args.ops = add ? VIRTCHNL_OP_ADD_VLAN_V2 : VIRTCHNL_OP_DEL_VLAN_V2;
678         args.in_args = (uint8_t *)&vlan_filter;
679         args.in_args_size = sizeof(vlan_filter);
680         args.out_buffer = vf->aq_resp;
681         args.out_size = IAVF_AQ_BUF_SZ;
682         err = iavf_execute_vf_cmd(adapter, &args, 0);
683         if (err)
684                 PMD_DRV_LOG(ERR, "fail to execute command %s",
685                             add ? "OP_ADD_VLAN_V2" :  "OP_DEL_VLAN_V2");
686
687         return err;
688 }
689
690 int
691 iavf_get_vlan_offload_caps_v2(struct iavf_adapter *adapter)
692 {
693         struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
694         struct iavf_cmd_info args;
695         int ret;
696
697         args.ops = VIRTCHNL_OP_GET_OFFLOAD_VLAN_V2_CAPS;
698         args.in_args = NULL;
699         args.in_args_size = 0;
700         args.out_buffer = vf->aq_resp;
701         args.out_size = IAVF_AQ_BUF_SZ;
702
703         ret = iavf_execute_vf_cmd(adapter, &args, 0);
704         if (ret) {
705                 PMD_DRV_LOG(ERR,
706                             "Failed to execute command of VIRTCHNL_OP_GET_OFFLOAD_VLAN_V2_CAPS");
707                 return ret;
708         }
709
710         rte_memcpy(&vf->vlan_v2_caps, vf->aq_resp, sizeof(vf->vlan_v2_caps));
711
712         return 0;
713 }
714
715 int
716 iavf_enable_queues(struct iavf_adapter *adapter)
717 {
718         struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
719         struct virtchnl_queue_select queue_select;
720         struct iavf_cmd_info args;
721         int err;
722
723         memset(&queue_select, 0, sizeof(queue_select));
724         queue_select.vsi_id = vf->vsi_res->vsi_id;
725
726         queue_select.rx_queues = BIT(adapter->dev_data->nb_rx_queues) - 1;
727         queue_select.tx_queues = BIT(adapter->dev_data->nb_tx_queues) - 1;
728
729         args.ops = VIRTCHNL_OP_ENABLE_QUEUES;
730         args.in_args = (u8 *)&queue_select;
731         args.in_args_size = sizeof(queue_select);
732         args.out_buffer = vf->aq_resp;
733         args.out_size = IAVF_AQ_BUF_SZ;
734         err = iavf_execute_vf_cmd(adapter, &args, 0);
735         if (err) {
736                 PMD_DRV_LOG(ERR,
737                             "Failed to execute command of OP_ENABLE_QUEUES");
738                 return err;
739         }
740         return 0;
741 }
742
743 int
744 iavf_disable_queues(struct iavf_adapter *adapter)
745 {
746         struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
747         struct virtchnl_queue_select queue_select;
748         struct iavf_cmd_info args;
749         int err;
750
751         memset(&queue_select, 0, sizeof(queue_select));
752         queue_select.vsi_id = vf->vsi_res->vsi_id;
753
754         queue_select.rx_queues = BIT(adapter->dev_data->nb_rx_queues) - 1;
755         queue_select.tx_queues = BIT(adapter->dev_data->nb_tx_queues) - 1;
756
757         args.ops = VIRTCHNL_OP_DISABLE_QUEUES;
758         args.in_args = (u8 *)&queue_select;
759         args.in_args_size = sizeof(queue_select);
760         args.out_buffer = vf->aq_resp;
761         args.out_size = IAVF_AQ_BUF_SZ;
762         err = iavf_execute_vf_cmd(adapter, &args, 0);
763         if (err) {
764                 PMD_DRV_LOG(ERR,
765                             "Failed to execute command of OP_DISABLE_QUEUES");
766                 return err;
767         }
768         return 0;
769 }
770
771 int
772 iavf_switch_queue(struct iavf_adapter *adapter, uint16_t qid,
773                  bool rx, bool on)
774 {
775         struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
776         struct virtchnl_queue_select queue_select;
777         struct iavf_cmd_info args;
778         int err;
779
780         memset(&queue_select, 0, sizeof(queue_select));
781         queue_select.vsi_id = vf->vsi_res->vsi_id;
782         if (rx)
783                 queue_select.rx_queues |= 1 << qid;
784         else
785                 queue_select.tx_queues |= 1 << qid;
786
787         if (on)
788                 args.ops = VIRTCHNL_OP_ENABLE_QUEUES;
789         else
790                 args.ops = VIRTCHNL_OP_DISABLE_QUEUES;
791         args.in_args = (u8 *)&queue_select;
792         args.in_args_size = sizeof(queue_select);
793         args.out_buffer = vf->aq_resp;
794         args.out_size = IAVF_AQ_BUF_SZ;
795         err = iavf_execute_vf_cmd(adapter, &args, 0);
796         if (err)
797                 PMD_DRV_LOG(ERR, "Failed to execute command of %s",
798                             on ? "OP_ENABLE_QUEUES" : "OP_DISABLE_QUEUES");
799         return err;
800 }
801
802 int
803 iavf_enable_queues_lv(struct iavf_adapter *adapter)
804 {
805         struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
806         struct virtchnl_del_ena_dis_queues *queue_select;
807         struct virtchnl_queue_chunk *queue_chunk;
808         struct iavf_cmd_info args;
809         int err, len;
810
811         len = sizeof(struct virtchnl_del_ena_dis_queues) +
812                   sizeof(struct virtchnl_queue_chunk) *
813                   (IAVF_RXTX_QUEUE_CHUNKS_NUM - 1);
814         queue_select = rte_zmalloc("queue_select", len, 0);
815         if (!queue_select)
816                 return -ENOMEM;
817
818         queue_chunk = queue_select->chunks.chunks;
819         queue_select->chunks.num_chunks = IAVF_RXTX_QUEUE_CHUNKS_NUM;
820         queue_select->vport_id = vf->vsi_res->vsi_id;
821
822         queue_chunk[VIRTCHNL_QUEUE_TYPE_TX].type = VIRTCHNL_QUEUE_TYPE_TX;
823         queue_chunk[VIRTCHNL_QUEUE_TYPE_TX].start_queue_id = 0;
824         queue_chunk[VIRTCHNL_QUEUE_TYPE_TX].num_queues =
825                 adapter->dev_data->nb_tx_queues;
826
827         queue_chunk[VIRTCHNL_QUEUE_TYPE_RX].type = VIRTCHNL_QUEUE_TYPE_RX;
828         queue_chunk[VIRTCHNL_QUEUE_TYPE_RX].start_queue_id = 0;
829         queue_chunk[VIRTCHNL_QUEUE_TYPE_RX].num_queues =
830                 adapter->dev_data->nb_rx_queues;
831
832         args.ops = VIRTCHNL_OP_ENABLE_QUEUES_V2;
833         args.in_args = (u8 *)queue_select;
834         args.in_args_size = len;
835         args.out_buffer = vf->aq_resp;
836         args.out_size = IAVF_AQ_BUF_SZ;
837         err = iavf_execute_vf_cmd(adapter, &args, 0);
838         if (err)
839                 PMD_DRV_LOG(ERR,
840                             "Failed to execute command of OP_ENABLE_QUEUES_V2");
841
842         rte_free(queue_select);
843         return err;
844 }
845
846 int
847 iavf_disable_queues_lv(struct iavf_adapter *adapter)
848 {
849         struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
850         struct virtchnl_del_ena_dis_queues *queue_select;
851         struct virtchnl_queue_chunk *queue_chunk;
852         struct iavf_cmd_info args;
853         int err, len;
854
855         len = sizeof(struct virtchnl_del_ena_dis_queues) +
856                   sizeof(struct virtchnl_queue_chunk) *
857                   (IAVF_RXTX_QUEUE_CHUNKS_NUM - 1);
858         queue_select = rte_zmalloc("queue_select", len, 0);
859         if (!queue_select)
860                 return -ENOMEM;
861
862         queue_chunk = queue_select->chunks.chunks;
863         queue_select->chunks.num_chunks = IAVF_RXTX_QUEUE_CHUNKS_NUM;
864         queue_select->vport_id = vf->vsi_res->vsi_id;
865
866         queue_chunk[VIRTCHNL_QUEUE_TYPE_TX].type = VIRTCHNL_QUEUE_TYPE_TX;
867         queue_chunk[VIRTCHNL_QUEUE_TYPE_TX].start_queue_id = 0;
868         queue_chunk[VIRTCHNL_QUEUE_TYPE_TX].num_queues =
869                 adapter->dev_data->nb_tx_queues;
870
871         queue_chunk[VIRTCHNL_QUEUE_TYPE_RX].type = VIRTCHNL_QUEUE_TYPE_RX;
872         queue_chunk[VIRTCHNL_QUEUE_TYPE_RX].start_queue_id = 0;
873         queue_chunk[VIRTCHNL_QUEUE_TYPE_RX].num_queues =
874                 adapter->dev_data->nb_rx_queues;
875
876         args.ops = VIRTCHNL_OP_DISABLE_QUEUES_V2;
877         args.in_args = (u8 *)queue_select;
878         args.in_args_size = len;
879         args.out_buffer = vf->aq_resp;
880         args.out_size = IAVF_AQ_BUF_SZ;
881         err = iavf_execute_vf_cmd(adapter, &args, 0);
882         if (err)
883                 PMD_DRV_LOG(ERR,
884                             "Failed to execute command of OP_DISABLE_QUEUES_V2");
885
886         rte_free(queue_select);
887         return err;
888 }
889
890 int
891 iavf_switch_queue_lv(struct iavf_adapter *adapter, uint16_t qid,
892                  bool rx, bool on)
893 {
894         struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
895         struct virtchnl_del_ena_dis_queues *queue_select;
896         struct virtchnl_queue_chunk *queue_chunk;
897         struct iavf_cmd_info args;
898         int err, len;
899
900         len = sizeof(struct virtchnl_del_ena_dis_queues);
901         queue_select = rte_zmalloc("queue_select", len, 0);
902         if (!queue_select)
903                 return -ENOMEM;
904
905         queue_chunk = queue_select->chunks.chunks;
906         queue_select->chunks.num_chunks = 1;
907         queue_select->vport_id = vf->vsi_res->vsi_id;
908
909         if (rx) {
910                 queue_chunk->type = VIRTCHNL_QUEUE_TYPE_RX;
911                 queue_chunk->start_queue_id = qid;
912                 queue_chunk->num_queues = 1;
913         } else {
914                 queue_chunk->type = VIRTCHNL_QUEUE_TYPE_TX;
915                 queue_chunk->start_queue_id = qid;
916                 queue_chunk->num_queues = 1;
917         }
918
919         if (on)
920                 args.ops = VIRTCHNL_OP_ENABLE_QUEUES_V2;
921         else
922                 args.ops = VIRTCHNL_OP_DISABLE_QUEUES_V2;
923         args.in_args = (u8 *)queue_select;
924         args.in_args_size = len;
925         args.out_buffer = vf->aq_resp;
926         args.out_size = IAVF_AQ_BUF_SZ;
927         err = iavf_execute_vf_cmd(adapter, &args, 0);
928         if (err)
929                 PMD_DRV_LOG(ERR, "Failed to execute command of %s",
930                             on ? "OP_ENABLE_QUEUES_V2" : "OP_DISABLE_QUEUES_V2");
931
932         rte_free(queue_select);
933         return err;
934 }
935
936 int
937 iavf_configure_rss_lut(struct iavf_adapter *adapter)
938 {
939         struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
940         struct virtchnl_rss_lut *rss_lut;
941         struct iavf_cmd_info args;
942         int len, err = 0;
943
944         len = sizeof(*rss_lut) + vf->vf_res->rss_lut_size - 1;
945         rss_lut = rte_zmalloc("rss_lut", len, 0);
946         if (!rss_lut)
947                 return -ENOMEM;
948
949         rss_lut->vsi_id = vf->vsi_res->vsi_id;
950         rss_lut->lut_entries = vf->vf_res->rss_lut_size;
951         rte_memcpy(rss_lut->lut, vf->rss_lut, vf->vf_res->rss_lut_size);
952
953         args.ops = VIRTCHNL_OP_CONFIG_RSS_LUT;
954         args.in_args = (u8 *)rss_lut;
955         args.in_args_size = len;
956         args.out_buffer = vf->aq_resp;
957         args.out_size = IAVF_AQ_BUF_SZ;
958
959         err = iavf_execute_vf_cmd(adapter, &args, 0);
960         if (err)
961                 PMD_DRV_LOG(ERR,
962                             "Failed to execute command of OP_CONFIG_RSS_LUT");
963
964         rte_free(rss_lut);
965         return err;
966 }
967
968 int
969 iavf_configure_rss_key(struct iavf_adapter *adapter)
970 {
971         struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
972         struct virtchnl_rss_key *rss_key;
973         struct iavf_cmd_info args;
974         int len, err = 0;
975
976         len = sizeof(*rss_key) + vf->vf_res->rss_key_size - 1;
977         rss_key = rte_zmalloc("rss_key", len, 0);
978         if (!rss_key)
979                 return -ENOMEM;
980
981         rss_key->vsi_id = vf->vsi_res->vsi_id;
982         rss_key->key_len = vf->vf_res->rss_key_size;
983         rte_memcpy(rss_key->key, vf->rss_key, vf->vf_res->rss_key_size);
984
985         args.ops = VIRTCHNL_OP_CONFIG_RSS_KEY;
986         args.in_args = (u8 *)rss_key;
987         args.in_args_size = len;
988         args.out_buffer = vf->aq_resp;
989         args.out_size = IAVF_AQ_BUF_SZ;
990
991         err = iavf_execute_vf_cmd(adapter, &args, 0);
992         if (err)
993                 PMD_DRV_LOG(ERR,
994                             "Failed to execute command of OP_CONFIG_RSS_KEY");
995
996         rte_free(rss_key);
997         return err;
998 }
999
1000 int
1001 iavf_configure_queues(struct iavf_adapter *adapter,
1002                 uint16_t num_queue_pairs, uint16_t index)
1003 {
1004         struct iavf_rx_queue **rxq =
1005                 (struct iavf_rx_queue **)adapter->dev_data->rx_queues;
1006         struct iavf_tx_queue **txq =
1007                 (struct iavf_tx_queue **)adapter->dev_data->tx_queues;
1008         struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
1009         struct virtchnl_vsi_queue_config_info *vc_config;
1010         struct virtchnl_queue_pair_info *vc_qp;
1011         struct iavf_cmd_info args;
1012         uint16_t i, size;
1013         int err;
1014
1015         size = sizeof(*vc_config) +
1016                sizeof(vc_config->qpair[0]) * num_queue_pairs;
1017         vc_config = rte_zmalloc("cfg_queue", size, 0);
1018         if (!vc_config)
1019                 return -ENOMEM;
1020
1021         vc_config->vsi_id = vf->vsi_res->vsi_id;
1022         vc_config->num_queue_pairs = num_queue_pairs;
1023
1024         for (i = index, vc_qp = vc_config->qpair;
1025                  i < index + num_queue_pairs;
1026              i++, vc_qp++) {
1027                 vc_qp->txq.vsi_id = vf->vsi_res->vsi_id;
1028                 vc_qp->txq.queue_id = i;
1029
1030                 /* Virtchnnl configure tx queues by pairs */
1031                 if (i < adapter->dev_data->nb_tx_queues) {
1032                         vc_qp->txq.ring_len = txq[i]->nb_tx_desc;
1033                         vc_qp->txq.dma_ring_addr = txq[i]->tx_ring_phys_addr;
1034                 }
1035
1036                 vc_qp->rxq.vsi_id = vf->vsi_res->vsi_id;
1037                 vc_qp->rxq.queue_id = i;
1038                 vc_qp->rxq.max_pkt_size = vf->max_pkt_len;
1039
1040                 if (i >= adapter->dev_data->nb_rx_queues)
1041                         continue;
1042
1043                 /* Virtchnnl configure rx queues by pairs */
1044                 vc_qp->rxq.ring_len = rxq[i]->nb_rx_desc;
1045                 vc_qp->rxq.dma_ring_addr = rxq[i]->rx_ring_phys_addr;
1046                 vc_qp->rxq.databuffer_size = rxq[i]->rx_buf_len;
1047                 vc_qp->rxq.crc_disable = rxq[i]->crc_len != 0 ? 1 : 0;
1048 #ifndef RTE_LIBRTE_IAVF_16BYTE_RX_DESC
1049                 if (vf->vf_res->vf_cap_flags &
1050                     VIRTCHNL_VF_OFFLOAD_RX_FLEX_DESC &&
1051                     vf->supported_rxdid & BIT(rxq[i]->rxdid)) {
1052                         vc_qp->rxq.rxdid = rxq[i]->rxdid;
1053                         PMD_DRV_LOG(NOTICE, "request RXDID[%d] in Queue[%d]",
1054                                     vc_qp->rxq.rxdid, i);
1055                 } else {
1056                         PMD_DRV_LOG(NOTICE, "RXDID[%d] is not supported, "
1057                                     "request default RXDID[%d] in Queue[%d]",
1058                                     rxq[i]->rxdid, IAVF_RXDID_LEGACY_1, i);
1059                         vc_qp->rxq.rxdid = IAVF_RXDID_LEGACY_1;
1060                 }
1061 #else
1062                 if (vf->vf_res->vf_cap_flags &
1063                         VIRTCHNL_VF_OFFLOAD_RX_FLEX_DESC &&
1064                         vf->supported_rxdid & BIT(IAVF_RXDID_LEGACY_0)) {
1065                         vc_qp->rxq.rxdid = IAVF_RXDID_LEGACY_0;
1066                         PMD_DRV_LOG(NOTICE, "request RXDID[%d] in Queue[%d]",
1067                                     vc_qp->rxq.rxdid, i);
1068                 } else {
1069                         PMD_DRV_LOG(ERR, "RXDID[%d] is not supported",
1070                                     IAVF_RXDID_LEGACY_0);
1071                         return -1;
1072                 }
1073 #endif
1074         }
1075
1076         memset(&args, 0, sizeof(args));
1077         args.ops = VIRTCHNL_OP_CONFIG_VSI_QUEUES;
1078         args.in_args = (uint8_t *)vc_config;
1079         args.in_args_size = size;
1080         args.out_buffer = vf->aq_resp;
1081         args.out_size = IAVF_AQ_BUF_SZ;
1082
1083         err = iavf_execute_vf_cmd(adapter, &args, 0);
1084         if (err)
1085                 PMD_DRV_LOG(ERR, "Failed to execute command of"
1086                             " VIRTCHNL_OP_CONFIG_VSI_QUEUES");
1087
1088         rte_free(vc_config);
1089         return err;
1090 }
1091
1092 int
1093 iavf_config_irq_map(struct iavf_adapter *adapter)
1094 {
1095         struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
1096         struct virtchnl_irq_map_info *map_info;
1097         struct virtchnl_vector_map *vecmap;
1098         struct iavf_cmd_info args;
1099         int len, i, err;
1100
1101         len = sizeof(struct virtchnl_irq_map_info) +
1102               sizeof(struct virtchnl_vector_map) * vf->nb_msix;
1103
1104         map_info = rte_zmalloc("map_info", len, 0);
1105         if (!map_info)
1106                 return -ENOMEM;
1107
1108         map_info->num_vectors = vf->nb_msix;
1109         for (i = 0; i < adapter->dev_data->nb_rx_queues; i++) {
1110                 vecmap =
1111                     &map_info->vecmap[vf->qv_map[i].vector_id - vf->msix_base];
1112                 vecmap->vsi_id = vf->vsi_res->vsi_id;
1113                 vecmap->rxitr_idx = IAVF_ITR_INDEX_DEFAULT;
1114                 vecmap->vector_id = vf->qv_map[i].vector_id;
1115                 vecmap->txq_map = 0;
1116                 vecmap->rxq_map |= 1 << vf->qv_map[i].queue_id;
1117         }
1118
1119         args.ops = VIRTCHNL_OP_CONFIG_IRQ_MAP;
1120         args.in_args = (u8 *)map_info;
1121         args.in_args_size = len;
1122         args.out_buffer = vf->aq_resp;
1123         args.out_size = IAVF_AQ_BUF_SZ;
1124         err = iavf_execute_vf_cmd(adapter, &args, 0);
1125         if (err)
1126                 PMD_DRV_LOG(ERR, "fail to execute command OP_CONFIG_IRQ_MAP");
1127
1128         rte_free(map_info);
1129         return err;
1130 }
1131
1132 int
1133 iavf_config_irq_map_lv(struct iavf_adapter *adapter, uint16_t num,
1134                 uint16_t index)
1135 {
1136         struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
1137         struct virtchnl_queue_vector_maps *map_info;
1138         struct virtchnl_queue_vector *qv_maps;
1139         struct iavf_cmd_info args;
1140         int len, i, err;
1141         int count = 0;
1142
1143         len = sizeof(struct virtchnl_queue_vector_maps) +
1144               sizeof(struct virtchnl_queue_vector) * (num - 1);
1145
1146         map_info = rte_zmalloc("map_info", len, 0);
1147         if (!map_info)
1148                 return -ENOMEM;
1149
1150         map_info->vport_id = vf->vsi_res->vsi_id;
1151         map_info->num_qv_maps = num;
1152         for (i = index; i < index + map_info->num_qv_maps; i++) {
1153                 qv_maps = &map_info->qv_maps[count++];
1154                 qv_maps->itr_idx = VIRTCHNL_ITR_IDX_0;
1155                 qv_maps->queue_type = VIRTCHNL_QUEUE_TYPE_RX;
1156                 qv_maps->queue_id = vf->qv_map[i].queue_id;
1157                 qv_maps->vector_id = vf->qv_map[i].vector_id;
1158         }
1159
1160         args.ops = VIRTCHNL_OP_MAP_QUEUE_VECTOR;
1161         args.in_args = (u8 *)map_info;
1162         args.in_args_size = len;
1163         args.out_buffer = vf->aq_resp;
1164         args.out_size = IAVF_AQ_BUF_SZ;
1165         err = iavf_execute_vf_cmd(adapter, &args, 0);
1166         if (err)
1167                 PMD_DRV_LOG(ERR, "fail to execute command OP_MAP_QUEUE_VECTOR");
1168
1169         rte_free(map_info);
1170         return err;
1171 }
1172
1173 void
1174 iavf_add_del_all_mac_addr(struct iavf_adapter *adapter, bool add)
1175 {
1176         struct virtchnl_ether_addr_list *list;
1177         struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
1178         struct rte_ether_addr *addr;
1179         struct iavf_cmd_info args;
1180         int len, err, i, j;
1181         int next_begin = 0;
1182         int begin = 0;
1183
1184         do {
1185                 j = 0;
1186                 len = sizeof(struct virtchnl_ether_addr_list);
1187                 for (i = begin; i < IAVF_NUM_MACADDR_MAX; i++, next_begin++) {
1188                         addr = &adapter->dev_data->mac_addrs[i];
1189                         if (rte_is_zero_ether_addr(addr))
1190                                 continue;
1191                         len += sizeof(struct virtchnl_ether_addr);
1192                         if (len >= IAVF_AQ_BUF_SZ) {
1193                                 next_begin = i + 1;
1194                                 break;
1195                         }
1196                 }
1197
1198                 list = rte_zmalloc("iavf_del_mac_buffer", len, 0);
1199                 if (!list) {
1200                         PMD_DRV_LOG(ERR, "fail to allocate memory");
1201                         return;
1202                 }
1203
1204                 for (i = begin; i < next_begin; i++) {
1205                         addr = &adapter->dev_data->mac_addrs[i];
1206                         if (rte_is_zero_ether_addr(addr))
1207                                 continue;
1208                         rte_memcpy(list->list[j].addr, addr->addr_bytes,
1209                                    sizeof(addr->addr_bytes));
1210                         list->list[j].type = (j == 0 ?
1211                                               VIRTCHNL_ETHER_ADDR_PRIMARY :
1212                                               VIRTCHNL_ETHER_ADDR_EXTRA);
1213                         PMD_DRV_LOG(DEBUG, "add/rm mac:" RTE_ETHER_ADDR_PRT_FMT,
1214                                     RTE_ETHER_ADDR_BYTES(addr));
1215                         j++;
1216                 }
1217                 list->vsi_id = vf->vsi_res->vsi_id;
1218                 list->num_elements = j;
1219                 args.ops = add ? VIRTCHNL_OP_ADD_ETH_ADDR :
1220                            VIRTCHNL_OP_DEL_ETH_ADDR;
1221                 args.in_args = (uint8_t *)list;
1222                 args.in_args_size = len;
1223                 args.out_buffer = vf->aq_resp;
1224                 args.out_size = IAVF_AQ_BUF_SZ;
1225                 err = iavf_execute_vf_cmd(adapter, &args, 0);
1226                 if (err)
1227                         PMD_DRV_LOG(ERR, "fail to execute command %s",
1228                                     add ? "OP_ADD_ETHER_ADDRESS" :
1229                                     "OP_DEL_ETHER_ADDRESS");
1230                 rte_free(list);
1231                 begin = next_begin;
1232         } while (begin < IAVF_NUM_MACADDR_MAX);
1233 }
1234
1235 int
1236 iavf_query_stats(struct iavf_adapter *adapter,
1237                 struct virtchnl_eth_stats **pstats)
1238 {
1239         struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
1240         struct virtchnl_queue_select q_stats;
1241         struct iavf_cmd_info args;
1242         int err;
1243
1244         memset(&q_stats, 0, sizeof(q_stats));
1245         q_stats.vsi_id = vf->vsi_res->vsi_id;
1246         args.ops = VIRTCHNL_OP_GET_STATS;
1247         args.in_args = (uint8_t *)&q_stats;
1248         args.in_args_size = sizeof(q_stats);
1249         args.out_buffer = vf->aq_resp;
1250         args.out_size = IAVF_AQ_BUF_SZ;
1251
1252         err = iavf_execute_vf_cmd(adapter, &args, 0);
1253         if (err) {
1254                 PMD_DRV_LOG(ERR, "fail to execute command OP_GET_STATS");
1255                 *pstats = NULL;
1256                 return err;
1257         }
1258         *pstats = (struct virtchnl_eth_stats *)args.out_buffer;
1259         return 0;
1260 }
1261
1262 int
1263 iavf_config_promisc(struct iavf_adapter *adapter,
1264                    bool enable_unicast,
1265                    bool enable_multicast)
1266 {
1267         struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
1268         struct virtchnl_promisc_info promisc;
1269         struct iavf_cmd_info args;
1270         int err;
1271
1272         promisc.flags = 0;
1273         promisc.vsi_id = vf->vsi_res->vsi_id;
1274
1275         if (enable_unicast)
1276                 promisc.flags |= FLAG_VF_UNICAST_PROMISC;
1277
1278         if (enable_multicast)
1279                 promisc.flags |= FLAG_VF_MULTICAST_PROMISC;
1280
1281         args.ops = VIRTCHNL_OP_CONFIG_PROMISCUOUS_MODE;
1282         args.in_args = (uint8_t *)&promisc;
1283         args.in_args_size = sizeof(promisc);
1284         args.out_buffer = vf->aq_resp;
1285         args.out_size = IAVF_AQ_BUF_SZ;
1286
1287         err = iavf_execute_vf_cmd(adapter, &args, 0);
1288
1289         if (err) {
1290                 PMD_DRV_LOG(ERR,
1291                             "fail to execute command CONFIG_PROMISCUOUS_MODE");
1292
1293                 if (err == -ENOTSUP)
1294                         return err;
1295
1296                 return -EAGAIN;
1297         }
1298
1299         vf->promisc_unicast_enabled = enable_unicast;
1300         vf->promisc_multicast_enabled = enable_multicast;
1301         return 0;
1302 }
1303
1304 int
1305 iavf_add_del_eth_addr(struct iavf_adapter *adapter, struct rte_ether_addr *addr,
1306                      bool add, uint8_t type)
1307 {
1308         struct virtchnl_ether_addr_list *list;
1309         struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
1310         uint8_t cmd_buffer[sizeof(struct virtchnl_ether_addr_list) +
1311                            sizeof(struct virtchnl_ether_addr)];
1312         struct iavf_cmd_info args;
1313         int err;
1314
1315         list = (struct virtchnl_ether_addr_list *)cmd_buffer;
1316         list->vsi_id = vf->vsi_res->vsi_id;
1317         list->num_elements = 1;
1318         list->list[0].type = type;
1319         rte_memcpy(list->list[0].addr, addr->addr_bytes,
1320                    sizeof(addr->addr_bytes));
1321
1322         args.ops = add ? VIRTCHNL_OP_ADD_ETH_ADDR : VIRTCHNL_OP_DEL_ETH_ADDR;
1323         args.in_args = cmd_buffer;
1324         args.in_args_size = sizeof(cmd_buffer);
1325         args.out_buffer = vf->aq_resp;
1326         args.out_size = IAVF_AQ_BUF_SZ;
1327         err = iavf_execute_vf_cmd(adapter, &args, 0);
1328         if (err)
1329                 PMD_DRV_LOG(ERR, "fail to execute command %s",
1330                             add ? "OP_ADD_ETH_ADDR" :  "OP_DEL_ETH_ADDR");
1331         return err;
1332 }
1333
1334 int
1335 iavf_add_del_vlan(struct iavf_adapter *adapter, uint16_t vlanid, bool add)
1336 {
1337         struct virtchnl_vlan_filter_list *vlan_list;
1338         struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
1339         uint8_t cmd_buffer[sizeof(struct virtchnl_vlan_filter_list) +
1340                                                         sizeof(uint16_t)];
1341         struct iavf_cmd_info args;
1342         int err;
1343
1344         vlan_list = (struct virtchnl_vlan_filter_list *)cmd_buffer;
1345         vlan_list->vsi_id = vf->vsi_res->vsi_id;
1346         vlan_list->num_elements = 1;
1347         vlan_list->vlan_id[0] = vlanid;
1348
1349         args.ops = add ? VIRTCHNL_OP_ADD_VLAN : VIRTCHNL_OP_DEL_VLAN;
1350         args.in_args = cmd_buffer;
1351         args.in_args_size = sizeof(cmd_buffer);
1352         args.out_buffer = vf->aq_resp;
1353         args.out_size = IAVF_AQ_BUF_SZ;
1354         err = iavf_execute_vf_cmd(adapter, &args, 0);
1355         if (err)
1356                 PMD_DRV_LOG(ERR, "fail to execute command %s",
1357                             add ? "OP_ADD_VLAN" :  "OP_DEL_VLAN");
1358
1359         return err;
1360 }
1361
1362 int
1363 iavf_fdir_add(struct iavf_adapter *adapter,
1364         struct iavf_fdir_conf *filter)
1365 {
1366         struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
1367         struct virtchnl_fdir_add *fdir_ret;
1368
1369         struct iavf_cmd_info args;
1370         int err;
1371
1372         filter->add_fltr.vsi_id = vf->vsi_res->vsi_id;
1373         filter->add_fltr.validate_only = 0;
1374
1375         args.ops = VIRTCHNL_OP_ADD_FDIR_FILTER;
1376         args.in_args = (uint8_t *)(&filter->add_fltr);
1377         args.in_args_size = sizeof(*(&filter->add_fltr));
1378         args.out_buffer = vf->aq_resp;
1379         args.out_size = IAVF_AQ_BUF_SZ;
1380
1381         err = iavf_execute_vf_cmd(adapter, &args, 0);
1382         if (err) {
1383                 PMD_DRV_LOG(ERR, "fail to execute command OP_ADD_FDIR_FILTER");
1384                 return err;
1385         }
1386
1387         fdir_ret = (struct virtchnl_fdir_add *)args.out_buffer;
1388         filter->flow_id = fdir_ret->flow_id;
1389
1390         if (fdir_ret->status == VIRTCHNL_FDIR_SUCCESS) {
1391                 PMD_DRV_LOG(INFO,
1392                         "Succeed in adding rule request by PF");
1393         } else if (fdir_ret->status == VIRTCHNL_FDIR_FAILURE_RULE_NORESOURCE) {
1394                 PMD_DRV_LOG(ERR,
1395                         "Failed to add rule request due to no hw resource");
1396                 return -1;
1397         } else if (fdir_ret->status == VIRTCHNL_FDIR_FAILURE_RULE_EXIST) {
1398                 PMD_DRV_LOG(ERR,
1399                         "Failed to add rule request due to the rule is already existed");
1400                 return -1;
1401         } else if (fdir_ret->status == VIRTCHNL_FDIR_FAILURE_RULE_CONFLICT) {
1402                 PMD_DRV_LOG(ERR,
1403                         "Failed to add rule request due to the rule is conflict with existing rule");
1404                 return -1;
1405         } else if (fdir_ret->status == VIRTCHNL_FDIR_FAILURE_RULE_INVALID) {
1406                 PMD_DRV_LOG(ERR,
1407                         "Failed to add rule request due to the hw doesn't support");
1408                 return -1;
1409         } else if (fdir_ret->status == VIRTCHNL_FDIR_FAILURE_RULE_TIMEOUT) {
1410                 PMD_DRV_LOG(ERR,
1411                         "Failed to add rule request due to time out for programming");
1412                 return -1;
1413         } else {
1414                 PMD_DRV_LOG(ERR,
1415                         "Failed to add rule request due to other reasons");
1416                 return -1;
1417         }
1418
1419         return 0;
1420 };
1421
1422 int
1423 iavf_fdir_del(struct iavf_adapter *adapter,
1424         struct iavf_fdir_conf *filter)
1425 {
1426         struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
1427         struct virtchnl_fdir_del *fdir_ret;
1428
1429         struct iavf_cmd_info args;
1430         int err;
1431
1432         filter->del_fltr.vsi_id = vf->vsi_res->vsi_id;
1433         filter->del_fltr.flow_id = filter->flow_id;
1434
1435         args.ops = VIRTCHNL_OP_DEL_FDIR_FILTER;
1436         args.in_args = (uint8_t *)(&filter->del_fltr);
1437         args.in_args_size = sizeof(filter->del_fltr);
1438         args.out_buffer = vf->aq_resp;
1439         args.out_size = IAVF_AQ_BUF_SZ;
1440
1441         err = iavf_execute_vf_cmd(adapter, &args, 0);
1442         if (err) {
1443                 PMD_DRV_LOG(ERR, "fail to execute command OP_DEL_FDIR_FILTER");
1444                 return err;
1445         }
1446
1447         fdir_ret = (struct virtchnl_fdir_del *)args.out_buffer;
1448
1449         if (fdir_ret->status == VIRTCHNL_FDIR_SUCCESS) {
1450                 PMD_DRV_LOG(INFO,
1451                         "Succeed in deleting rule request by PF");
1452         } else if (fdir_ret->status == VIRTCHNL_FDIR_FAILURE_RULE_NONEXIST) {
1453                 PMD_DRV_LOG(ERR,
1454                         "Failed to delete rule request due to this rule doesn't exist");
1455                 return -1;
1456         } else if (fdir_ret->status == VIRTCHNL_FDIR_FAILURE_RULE_TIMEOUT) {
1457                 PMD_DRV_LOG(ERR,
1458                         "Failed to delete rule request due to time out for programming");
1459                 return -1;
1460         } else {
1461                 PMD_DRV_LOG(ERR,
1462                         "Failed to delete rule request due to other reasons");
1463                 return -1;
1464         }
1465
1466         return 0;
1467 };
1468
1469 int
1470 iavf_fdir_check(struct iavf_adapter *adapter,
1471                 struct iavf_fdir_conf *filter)
1472 {
1473         struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
1474         struct virtchnl_fdir_add *fdir_ret;
1475
1476         struct iavf_cmd_info args;
1477         int err;
1478
1479         filter->add_fltr.vsi_id = vf->vsi_res->vsi_id;
1480         filter->add_fltr.validate_only = 1;
1481
1482         args.ops = VIRTCHNL_OP_ADD_FDIR_FILTER;
1483         args.in_args = (uint8_t *)(&filter->add_fltr);
1484         args.in_args_size = sizeof(*(&filter->add_fltr));
1485         args.out_buffer = vf->aq_resp;
1486         args.out_size = IAVF_AQ_BUF_SZ;
1487
1488         err = iavf_execute_vf_cmd(adapter, &args, 0);
1489         if (err) {
1490                 PMD_DRV_LOG(ERR, "fail to check flow direcotor rule");
1491                 return err;
1492         }
1493
1494         fdir_ret = (struct virtchnl_fdir_add *)args.out_buffer;
1495
1496         if (fdir_ret->status == VIRTCHNL_FDIR_SUCCESS) {
1497                 PMD_DRV_LOG(INFO,
1498                         "Succeed in checking rule request by PF");
1499         } else if (fdir_ret->status == VIRTCHNL_FDIR_FAILURE_RULE_INVALID) {
1500                 PMD_DRV_LOG(ERR,
1501                         "Failed to check rule request due to parameters validation"
1502                         " or HW doesn't support");
1503                 return -1;
1504         } else {
1505                 PMD_DRV_LOG(ERR,
1506                         "Failed to check rule request due to other reasons");
1507                 return -1;
1508         }
1509
1510         return 0;
1511 }
1512
1513 int
1514 iavf_add_del_rss_cfg(struct iavf_adapter *adapter,
1515                      struct virtchnl_rss_cfg *rss_cfg, bool add)
1516 {
1517         struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
1518         struct iavf_cmd_info args;
1519         int err;
1520
1521         memset(&args, 0, sizeof(args));
1522         args.ops = add ? VIRTCHNL_OP_ADD_RSS_CFG :
1523                 VIRTCHNL_OP_DEL_RSS_CFG;
1524         args.in_args = (u8 *)rss_cfg;
1525         args.in_args_size = sizeof(*rss_cfg);
1526         args.out_buffer = vf->aq_resp;
1527         args.out_size = IAVF_AQ_BUF_SZ;
1528
1529         err = iavf_execute_vf_cmd(adapter, &args, 0);
1530         if (err)
1531                 PMD_DRV_LOG(ERR,
1532                             "Failed to execute command of %s",
1533                             add ? "OP_ADD_RSS_CFG" :
1534                             "OP_DEL_RSS_INPUT_CFG");
1535
1536         return err;
1537 }
1538
1539 int
1540 iavf_get_hena_caps(struct iavf_adapter *adapter, uint64_t *caps)
1541 {
1542         struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
1543         struct iavf_cmd_info args;
1544         int err;
1545
1546         args.ops = VIRTCHNL_OP_GET_RSS_HENA_CAPS;
1547         args.in_args = NULL;
1548         args.in_args_size = 0;
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 OP_GET_RSS_HENA_CAPS");
1556                 return err;
1557         }
1558
1559         *caps = ((struct virtchnl_rss_hena *)args.out_buffer)->hena;
1560         return 0;
1561 }
1562
1563 int
1564 iavf_set_hena(struct iavf_adapter *adapter, uint64_t hena)
1565 {
1566         struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
1567         struct virtchnl_rss_hena vrh;
1568         struct iavf_cmd_info args;
1569         int err;
1570
1571         vrh.hena = hena;
1572         args.ops = VIRTCHNL_OP_SET_RSS_HENA;
1573         args.in_args = (u8 *)&vrh;
1574         args.in_args_size = sizeof(vrh);
1575         args.out_buffer = vf->aq_resp;
1576         args.out_size = IAVF_AQ_BUF_SZ;
1577
1578         err = iavf_execute_vf_cmd(adapter, &args, 0);
1579         if (err)
1580                 PMD_DRV_LOG(ERR,
1581                             "Failed to execute command of OP_SET_RSS_HENA");
1582
1583         return err;
1584 }
1585
1586 int
1587 iavf_get_qos_cap(struct iavf_adapter *adapter)
1588 {
1589         struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
1590         struct iavf_cmd_info args;
1591         uint32_t len;
1592         int err;
1593
1594         args.ops = VIRTCHNL_OP_GET_QOS_CAPS;
1595         args.in_args = NULL;
1596         args.in_args_size = 0;
1597         args.out_buffer = vf->aq_resp;
1598         args.out_size = IAVF_AQ_BUF_SZ;
1599         err = iavf_execute_vf_cmd(adapter, &args, 0);
1600
1601         if (err) {
1602                 PMD_DRV_LOG(ERR,
1603                             "Failed to execute command of OP_GET_VF_RESOURCE");
1604                 return -1;
1605         }
1606
1607         len =  sizeof(struct virtchnl_qos_cap_list) +
1608                 IAVF_MAX_TRAFFIC_CLASS * sizeof(struct virtchnl_qos_cap_elem);
1609
1610         rte_memcpy(vf->qos_cap, args.out_buffer,
1611                    RTE_MIN(args.out_size, len));
1612
1613         return 0;
1614 }
1615
1616 int iavf_set_q_tc_map(struct rte_eth_dev *dev,
1617                 struct virtchnl_queue_tc_mapping *q_tc_mapping, uint16_t size)
1618 {
1619         struct iavf_adapter *adapter =
1620                         IAVF_DEV_PRIVATE_TO_ADAPTER(dev->data->dev_private);
1621         struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(dev->data->dev_private);
1622         struct iavf_cmd_info args;
1623         int err;
1624
1625         memset(&args, 0, sizeof(args));
1626         args.ops = VIRTCHNL_OP_CONFIG_QUEUE_TC_MAP;
1627         args.in_args = (uint8_t *)q_tc_mapping;
1628         args.in_args_size = size;
1629         args.out_buffer = vf->aq_resp;
1630         args.out_size = IAVF_AQ_BUF_SZ;
1631
1632         err = iavf_execute_vf_cmd(adapter, &args, 0);
1633         if (err)
1634                 PMD_DRV_LOG(ERR, "Failed to execute command of"
1635                             " VIRTCHNL_OP_CONFIG_TC_MAP");
1636         return err;
1637 }
1638
1639 int
1640 iavf_add_del_mc_addr_list(struct iavf_adapter *adapter,
1641                         struct rte_ether_addr *mc_addrs,
1642                         uint32_t mc_addrs_num, bool add)
1643 {
1644         struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
1645         uint8_t cmd_buffer[sizeof(struct virtchnl_ether_addr_list) +
1646                 (IAVF_NUM_MACADDR_MAX * sizeof(struct virtchnl_ether_addr))];
1647         struct virtchnl_ether_addr_list *list;
1648         struct iavf_cmd_info args;
1649         uint32_t i;
1650         int err;
1651
1652         if (mc_addrs == NULL || mc_addrs_num == 0)
1653                 return 0;
1654
1655         list = (struct virtchnl_ether_addr_list *)cmd_buffer;
1656         list->vsi_id = vf->vsi_res->vsi_id;
1657         list->num_elements = mc_addrs_num;
1658
1659         for (i = 0; i < mc_addrs_num; i++) {
1660                 if (!IAVF_IS_MULTICAST(mc_addrs[i].addr_bytes)) {
1661                         PMD_DRV_LOG(ERR, "Invalid mac:" RTE_ETHER_ADDR_PRT_FMT,
1662                                     RTE_ETHER_ADDR_BYTES(&mc_addrs[i]));
1663                         return -EINVAL;
1664                 }
1665
1666                 memcpy(list->list[i].addr, mc_addrs[i].addr_bytes,
1667                         sizeof(list->list[i].addr));
1668                 list->list[i].type = VIRTCHNL_ETHER_ADDR_EXTRA;
1669         }
1670
1671         args.ops = add ? VIRTCHNL_OP_ADD_ETH_ADDR : VIRTCHNL_OP_DEL_ETH_ADDR;
1672         args.in_args = cmd_buffer;
1673         args.in_args_size = sizeof(struct virtchnl_ether_addr_list) +
1674                 i * sizeof(struct virtchnl_ether_addr);
1675         args.out_buffer = vf->aq_resp;
1676         args.out_size = IAVF_AQ_BUF_SZ;
1677         err = iavf_execute_vf_cmd(adapter, &args, 0);
1678
1679         if (err) {
1680                 PMD_DRV_LOG(ERR, "fail to execute command %s",
1681                         add ? "OP_ADD_ETH_ADDR" : "OP_DEL_ETH_ADDR");
1682                 return err;
1683         }
1684
1685         return 0;
1686 }
1687
1688 int
1689 iavf_request_queues(struct rte_eth_dev *dev, uint16_t num)
1690 {
1691         struct iavf_adapter *adapter =
1692                 IAVF_DEV_PRIVATE_TO_ADAPTER(dev->data->dev_private);
1693         struct iavf_info *vf =  IAVF_DEV_PRIVATE_TO_VF(adapter);
1694         struct rte_pci_device *pci_dev = RTE_ETH_DEV_TO_PCI(dev);
1695         struct virtchnl_vf_res_request vfres;
1696         struct iavf_cmd_info args;
1697         uint16_t num_queue_pairs;
1698         int err;
1699
1700         if (!(vf->vf_res->vf_cap_flags &
1701                 VIRTCHNL_VF_OFFLOAD_REQ_QUEUES)) {
1702                 PMD_DRV_LOG(ERR, "request queues not supported");
1703                 return -1;
1704         }
1705
1706         if (num == 0) {
1707                 PMD_DRV_LOG(ERR, "queue number cannot be zero");
1708                 return -1;
1709         }
1710         vfres.num_queue_pairs = num;
1711
1712         args.ops = VIRTCHNL_OP_REQUEST_QUEUES;
1713         args.in_args = (u8 *)&vfres;
1714         args.in_args_size = sizeof(vfres);
1715         args.out_buffer = vf->aq_resp;
1716         args.out_size = IAVF_AQ_BUF_SZ;
1717
1718         if (vf->vf_res->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_WB_ON_ITR) {
1719                 /* disable interrupt to avoid the admin queue message to be read
1720                  * before iavf_read_msg_from_pf.
1721                  */
1722                 rte_intr_disable(pci_dev->intr_handle);
1723                 err = iavf_execute_vf_cmd(adapter, &args, 0);
1724                 rte_intr_enable(pci_dev->intr_handle);
1725         } else {
1726                 rte_eal_alarm_cancel(iavf_dev_alarm_handler, dev);
1727                 err = iavf_execute_vf_cmd(adapter, &args, 0);
1728                 rte_eal_alarm_set(IAVF_ALARM_INTERVAL,
1729                                   iavf_dev_alarm_handler, dev);
1730         }
1731
1732         if (err) {
1733                 PMD_DRV_LOG(ERR, "fail to execute command OP_REQUEST_QUEUES");
1734                 return err;
1735         }
1736
1737         /* request queues succeeded, vf is resetting */
1738         if (vf->vf_reset) {
1739                 PMD_DRV_LOG(INFO, "vf is resetting");
1740                 return 0;
1741         }
1742
1743         /* request additional queues failed, return available number */
1744         num_queue_pairs =
1745           ((struct virtchnl_vf_res_request *)args.out_buffer)->num_queue_pairs;
1746         PMD_DRV_LOG(ERR, "request queues failed, only %u queues "
1747                 "available", num_queue_pairs);
1748
1749         return -1;
1750 }
1751
1752 int
1753 iavf_get_max_rss_queue_region(struct iavf_adapter *adapter)
1754 {
1755         struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
1756         struct iavf_cmd_info args;
1757         uint16_t qregion_width;
1758         int err;
1759
1760         args.ops = VIRTCHNL_OP_GET_MAX_RSS_QREGION;
1761         args.in_args = NULL;
1762         args.in_args_size = 0;
1763         args.out_buffer = vf->aq_resp;
1764         args.out_size = IAVF_AQ_BUF_SZ;
1765
1766         err = iavf_execute_vf_cmd(adapter, &args, 0);
1767         if (err) {
1768                 PMD_DRV_LOG(ERR, "Failed to execute command of VIRTCHNL_OP_GET_MAX_RSS_QREGION");
1769                 return err;
1770         }
1771
1772         qregion_width =
1773         ((struct virtchnl_max_rss_qregion *)args.out_buffer)->qregion_width;
1774
1775         vf->max_rss_qregion = (uint16_t)(1 << qregion_width);
1776
1777         return 0;
1778 }