b62c8683c9447bc8bc61a9feae9f074108993797
[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_atomic.h>
17 #include <rte_eal.h>
18 #include <rte_ether.h>
19 #include <rte_ethdev_driver.h>
20 #include <rte_dev.h>
21
22 #include "iavf.h"
23 #include "iavf_rxtx.h"
24
25 #define MAX_TRY_TIMES 200
26 #define ASQ_DELAY_MS  10
27
28 static uint32_t
29 iavf_convert_link_speed(enum virtchnl_link_speed virt_link_speed)
30 {
31         uint32_t speed;
32
33         switch (virt_link_speed) {
34         case VIRTCHNL_LINK_SPEED_100MB:
35                 speed = 100;
36                 break;
37         case VIRTCHNL_LINK_SPEED_1GB:
38                 speed = 1000;
39                 break;
40         case VIRTCHNL_LINK_SPEED_10GB:
41                 speed = 10000;
42                 break;
43         case VIRTCHNL_LINK_SPEED_40GB:
44                 speed = 40000;
45                 break;
46         case VIRTCHNL_LINK_SPEED_20GB:
47                 speed = 20000;
48                 break;
49         case VIRTCHNL_LINK_SPEED_25GB:
50                 speed = 25000;
51                 break;
52         case VIRTCHNL_LINK_SPEED_2_5GB:
53                 speed = 2500;
54                 break;
55         case VIRTCHNL_LINK_SPEED_5GB:
56                 speed = 5000;
57                 break;
58         default:
59                 speed = 0;
60                 break;
61         }
62
63         return speed;
64 }
65
66 /* Read data in admin queue to get msg from pf driver */
67 static enum iavf_aq_result
68 iavf_read_msg_from_pf(struct iavf_adapter *adapter, uint16_t buf_len,
69                      uint8_t *buf)
70 {
71         struct iavf_hw *hw = IAVF_DEV_PRIVATE_TO_HW(adapter);
72         struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
73         struct rte_eth_dev *dev = adapter->eth_dev;
74         struct iavf_arq_event_info event;
75         enum iavf_aq_result result = IAVF_MSG_NON;
76         enum virtchnl_ops opcode;
77         int ret;
78
79         event.buf_len = buf_len;
80         event.msg_buf = buf;
81         ret = iavf_clean_arq_element(hw, &event, NULL);
82         /* Can't read any msg from adminQ */
83         if (ret) {
84                 PMD_DRV_LOG(DEBUG, "Can't read msg from AQ");
85                 if (ret != IAVF_ERR_ADMIN_QUEUE_NO_WORK)
86                         result = IAVF_MSG_ERR;
87                 return result;
88         }
89
90         opcode = (enum virtchnl_ops)rte_le_to_cpu_32(event.desc.cookie_high);
91         vf->cmd_retval = (enum virtchnl_status_code)rte_le_to_cpu_32(
92                         event.desc.cookie_low);
93
94         PMD_DRV_LOG(DEBUG, "AQ from pf carries opcode %u, retval %d",
95                     opcode, vf->cmd_retval);
96
97         if (opcode == VIRTCHNL_OP_EVENT) {
98                 struct virtchnl_pf_event *vpe =
99                         (struct virtchnl_pf_event *)event.msg_buf;
100
101                 result = IAVF_MSG_SYS;
102                 switch (vpe->event) {
103                 case VIRTCHNL_EVENT_LINK_CHANGE:
104                         vf->link_up =
105                                 vpe->event_data.link_event.link_status;
106                         if (vf->vf_res->vf_cap_flags &
107                                 VIRTCHNL_VF_CAP_ADV_LINK_SPEED) {
108                                 vf->link_speed =
109                                     vpe->event_data.link_event_adv.link_speed;
110                         } else {
111                                 enum virtchnl_link_speed speed;
112                                 speed = vpe->event_data.link_event.link_speed;
113                                 vf->link_speed = iavf_convert_link_speed(speed);
114                         }
115                         iavf_dev_link_update(dev, 0);
116                         PMD_DRV_LOG(INFO, "Link status update:%s",
117                                         vf->link_up ? "up" : "down");
118                         break;
119                 case VIRTCHNL_EVENT_RESET_IMPENDING:
120                         vf->vf_reset = true;
121                         PMD_DRV_LOG(INFO, "VF is resetting");
122                         break;
123                 case VIRTCHNL_EVENT_PF_DRIVER_CLOSE:
124                         vf->dev_closed = true;
125                         PMD_DRV_LOG(INFO, "PF driver closed");
126                         break;
127                 default:
128                         PMD_DRV_LOG(ERR, "%s: Unknown event %d from pf",
129                                         __func__, vpe->event);
130                 }
131         }  else {
132                 /* async reply msg on command issued by vf previously */
133                 result = IAVF_MSG_CMD;
134                 if (opcode != vf->pend_cmd) {
135                         PMD_DRV_LOG(WARNING, "command mismatch, expect %u, get %u",
136                                         vf->pend_cmd, opcode);
137                         result = IAVF_MSG_ERR;
138                 }
139         }
140
141         return result;
142 }
143
144 static int
145 iavf_execute_vf_cmd(struct iavf_adapter *adapter, struct iavf_cmd_info *args)
146 {
147         struct iavf_hw *hw = IAVF_DEV_PRIVATE_TO_HW(adapter);
148         struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
149         enum iavf_aq_result result;
150         enum iavf_status ret;
151         int err = 0;
152         int i = 0;
153
154         if (vf->vf_reset)
155                 return -EIO;
156
157         if (_atomic_set_cmd(vf, args->ops))
158                 return -1;
159
160         ret = iavf_aq_send_msg_to_pf(hw, args->ops, IAVF_SUCCESS,
161                                     args->in_args, args->in_args_size, NULL);
162         if (ret) {
163                 PMD_DRV_LOG(ERR, "fail to send cmd %d", args->ops);
164                 _clear_cmd(vf);
165                 return err;
166         }
167
168         switch (args->ops) {
169         case VIRTCHNL_OP_RESET_VF:
170                 /*no need to wait for response */
171                 _clear_cmd(vf);
172                 break;
173         case VIRTCHNL_OP_VERSION:
174         case VIRTCHNL_OP_GET_VF_RESOURCES:
175         case VIRTCHNL_OP_GET_SUPPORTED_RXDIDS:
176                 /* for init virtchnl ops, need to poll the response */
177                 do {
178                         result = iavf_read_msg_from_pf(adapter, args->out_size,
179                                                    args->out_buffer);
180                         if (result == IAVF_MSG_CMD)
181                                 break;
182                         rte_delay_ms(ASQ_DELAY_MS);
183                 } while (i++ < MAX_TRY_TIMES);
184                 if (i >= MAX_TRY_TIMES ||
185                     vf->cmd_retval != VIRTCHNL_STATUS_SUCCESS) {
186                         err = -1;
187                         PMD_DRV_LOG(ERR, "No response or return failure (%d)"
188                                     " for cmd %d", vf->cmd_retval, args->ops);
189                 }
190                 _clear_cmd(vf);
191                 break;
192
193         default:
194                 /* For other virtchnl ops in running time,
195                  * wait for the cmd done flag.
196                  */
197                 do {
198                         if (vf->pend_cmd == VIRTCHNL_OP_UNKNOWN)
199                                 break;
200                         rte_delay_ms(ASQ_DELAY_MS);
201                         /* If don't read msg or read sys event, continue */
202                 } while (i++ < MAX_TRY_TIMES);
203                 /* If there's no response is received, clear command */
204                 if (i >= MAX_TRY_TIMES  ||
205                     vf->cmd_retval != VIRTCHNL_STATUS_SUCCESS) {
206                         err = -1;
207                         PMD_DRV_LOG(ERR, "No response or return failure (%d)"
208                                     " for cmd %d", vf->cmd_retval, args->ops);
209                         _clear_cmd(vf);
210                 }
211                 break;
212         }
213
214         return err;
215 }
216
217 static void
218 iavf_handle_pf_event_msg(struct rte_eth_dev *dev, uint8_t *msg,
219                         uint16_t msglen)
220 {
221         struct virtchnl_pf_event *pf_msg =
222                         (struct virtchnl_pf_event *)msg;
223         struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(dev->data->dev_private);
224
225         if (msglen < sizeof(struct virtchnl_pf_event)) {
226                 PMD_DRV_LOG(DEBUG, "Error event");
227                 return;
228         }
229         switch (pf_msg->event) {
230         case VIRTCHNL_EVENT_RESET_IMPENDING:
231                 PMD_DRV_LOG(DEBUG, "VIRTCHNL_EVENT_RESET_IMPENDING event");
232                 vf->vf_reset = true;
233                 rte_eth_dev_callback_process(dev, RTE_ETH_EVENT_INTR_RESET,
234                                               NULL);
235                 break;
236         case VIRTCHNL_EVENT_LINK_CHANGE:
237                 PMD_DRV_LOG(DEBUG, "VIRTCHNL_EVENT_LINK_CHANGE event");
238                 vf->link_up = pf_msg->event_data.link_event.link_status;
239                 if (vf->vf_res->vf_cap_flags & VIRTCHNL_VF_CAP_ADV_LINK_SPEED) {
240                         vf->link_speed =
241                                 pf_msg->event_data.link_event_adv.link_speed;
242                 } else {
243                         enum virtchnl_link_speed speed;
244                         speed = pf_msg->event_data.link_event.link_speed;
245                         vf->link_speed = iavf_convert_link_speed(speed);
246                 }
247                 iavf_dev_link_update(dev, 0);
248                 rte_eth_dev_callback_process(dev, RTE_ETH_EVENT_INTR_LSC, NULL);
249                 break;
250         case VIRTCHNL_EVENT_PF_DRIVER_CLOSE:
251                 PMD_DRV_LOG(DEBUG, "VIRTCHNL_EVENT_PF_DRIVER_CLOSE event");
252                 break;
253         default:
254                 PMD_DRV_LOG(ERR, " unknown event received %u", pf_msg->event);
255                 break;
256         }
257 }
258
259 void
260 iavf_handle_virtchnl_msg(struct rte_eth_dev *dev)
261 {
262         struct iavf_hw *hw = IAVF_DEV_PRIVATE_TO_HW(dev->data->dev_private);
263         struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(dev->data->dev_private);
264         struct iavf_arq_event_info info;
265         uint16_t pending, aq_opc;
266         enum virtchnl_ops msg_opc;
267         enum iavf_status msg_ret;
268         int ret;
269
270         info.buf_len = IAVF_AQ_BUF_SZ;
271         if (!vf->aq_resp) {
272                 PMD_DRV_LOG(ERR, "Buffer for adminq resp should not be NULL");
273                 return;
274         }
275         info.msg_buf = vf->aq_resp;
276
277         pending = 1;
278         while (pending) {
279                 ret = iavf_clean_arq_element(hw, &info, &pending);
280
281                 if (ret != IAVF_SUCCESS) {
282                         PMD_DRV_LOG(INFO, "Failed to read msg from AdminQ,"
283                                     "ret: %d", ret);
284                         break;
285                 }
286                 aq_opc = rte_le_to_cpu_16(info.desc.opcode);
287                 /* For the message sent from pf to vf, opcode is stored in
288                  * cookie_high of struct iavf_aq_desc, while return error code
289                  * are stored in cookie_low, Which is done by PF driver.
290                  */
291                 msg_opc = (enum virtchnl_ops)rte_le_to_cpu_32(
292                                                   info.desc.cookie_high);
293                 msg_ret = (enum iavf_status)rte_le_to_cpu_32(
294                                                   info.desc.cookie_low);
295                 switch (aq_opc) {
296                 case iavf_aqc_opc_send_msg_to_vf:
297                         if (msg_opc == VIRTCHNL_OP_EVENT) {
298                                 iavf_handle_pf_event_msg(dev, info.msg_buf,
299                                                         info.msg_len);
300                         } else {
301                                 /* read message and it's expected one */
302                                 if (msg_opc == vf->pend_cmd)
303                                         _notify_cmd(vf, msg_ret);
304                                 else
305                                         PMD_DRV_LOG(ERR, "command mismatch,"
306                                                     "expect %u, get %u",
307                                                     vf->pend_cmd, msg_opc);
308                                 PMD_DRV_LOG(DEBUG,
309                                             "adminq response is received,"
310                                             " opcode = %d", msg_opc);
311                         }
312                         break;
313                 default:
314                         PMD_DRV_LOG(DEBUG, "Request %u is not supported yet",
315                                     aq_opc);
316                         break;
317                 }
318         }
319 }
320
321 int
322 iavf_enable_vlan_strip(struct iavf_adapter *adapter)
323 {
324         struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
325         struct iavf_cmd_info args;
326         int ret;
327
328         memset(&args, 0, sizeof(args));
329         args.ops = VIRTCHNL_OP_ENABLE_VLAN_STRIPPING;
330         args.in_args = NULL;
331         args.in_args_size = 0;
332         args.out_buffer = vf->aq_resp;
333         args.out_size = IAVF_AQ_BUF_SZ;
334         ret = iavf_execute_vf_cmd(adapter, &args);
335         if (ret)
336                 PMD_DRV_LOG(ERR, "Failed to execute command of"
337                             " OP_ENABLE_VLAN_STRIPPING");
338
339         return ret;
340 }
341
342 int
343 iavf_disable_vlan_strip(struct iavf_adapter *adapter)
344 {
345         struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
346         struct iavf_cmd_info args;
347         int ret;
348
349         memset(&args, 0, sizeof(args));
350         args.ops = VIRTCHNL_OP_DISABLE_VLAN_STRIPPING;
351         args.in_args = NULL;
352         args.in_args_size = 0;
353         args.out_buffer = vf->aq_resp;
354         args.out_size = IAVF_AQ_BUF_SZ;
355         ret = iavf_execute_vf_cmd(adapter, &args);
356         if (ret)
357                 PMD_DRV_LOG(ERR, "Failed to execute command of"
358                             " OP_DISABLE_VLAN_STRIPPING");
359
360         return ret;
361 }
362
363 #define VIRTCHNL_VERSION_MAJOR_START 1
364 #define VIRTCHNL_VERSION_MINOR_START 1
365
366 /* Check API version with sync wait until version read from admin queue */
367 int
368 iavf_check_api_version(struct iavf_adapter *adapter)
369 {
370         struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
371         struct virtchnl_version_info version, *pver;
372         struct iavf_cmd_info args;
373         int err;
374
375         version.major = VIRTCHNL_VERSION_MAJOR;
376         version.minor = VIRTCHNL_VERSION_MINOR;
377
378         args.ops = VIRTCHNL_OP_VERSION;
379         args.in_args = (uint8_t *)&version;
380         args.in_args_size = sizeof(version);
381         args.out_buffer = vf->aq_resp;
382         args.out_size = IAVF_AQ_BUF_SZ;
383
384         err = iavf_execute_vf_cmd(adapter, &args);
385         if (err) {
386                 PMD_INIT_LOG(ERR, "Fail to execute command of OP_VERSION");
387                 return err;
388         }
389
390         pver = (struct virtchnl_version_info *)args.out_buffer;
391         vf->virtchnl_version = *pver;
392
393         if (vf->virtchnl_version.major < VIRTCHNL_VERSION_MAJOR_START ||
394             (vf->virtchnl_version.major == VIRTCHNL_VERSION_MAJOR_START &&
395              vf->virtchnl_version.minor < VIRTCHNL_VERSION_MINOR_START)) {
396                 PMD_INIT_LOG(ERR, "VIRTCHNL API version should not be lower"
397                              " than (%u.%u) to support Adapative VF",
398                              VIRTCHNL_VERSION_MAJOR_START,
399                              VIRTCHNL_VERSION_MAJOR_START);
400                 return -1;
401         } else if (vf->virtchnl_version.major > VIRTCHNL_VERSION_MAJOR ||
402                    (vf->virtchnl_version.major == VIRTCHNL_VERSION_MAJOR &&
403                     vf->virtchnl_version.minor > VIRTCHNL_VERSION_MINOR)) {
404                 PMD_INIT_LOG(ERR, "PF/VF API version mismatch:(%u.%u)-(%u.%u)",
405                              vf->virtchnl_version.major,
406                              vf->virtchnl_version.minor,
407                              VIRTCHNL_VERSION_MAJOR,
408                              VIRTCHNL_VERSION_MINOR);
409                 return -1;
410         }
411
412         PMD_DRV_LOG(DEBUG, "Peer is supported PF host");
413         return 0;
414 }
415
416 int
417 iavf_get_vf_resource(struct iavf_adapter *adapter)
418 {
419         struct iavf_hw *hw = IAVF_DEV_PRIVATE_TO_HW(adapter);
420         struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
421         struct iavf_cmd_info args;
422         uint32_t caps, len;
423         int err, i;
424
425         args.ops = VIRTCHNL_OP_GET_VF_RESOURCES;
426         args.out_buffer = vf->aq_resp;
427         args.out_size = IAVF_AQ_BUF_SZ;
428
429         caps = IAVF_BASIC_OFFLOAD_CAPS | VIRTCHNL_VF_CAP_ADV_LINK_SPEED |
430                 VIRTCHNL_VF_OFFLOAD_RX_FLEX_DESC |
431                 VIRTCHNL_VF_OFFLOAD_FDIR_PF |
432                 VIRTCHNL_VF_OFFLOAD_ADV_RSS_PF;
433
434         args.in_args = (uint8_t *)&caps;
435         args.in_args_size = sizeof(caps);
436
437         err = iavf_execute_vf_cmd(adapter, &args);
438
439         if (err) {
440                 PMD_DRV_LOG(ERR,
441                             "Failed to execute command of OP_GET_VF_RESOURCE");
442                 return -1;
443         }
444
445         len =  sizeof(struct virtchnl_vf_resource) +
446                       IAVF_MAX_VF_VSI * sizeof(struct virtchnl_vsi_resource);
447
448         rte_memcpy(vf->vf_res, args.out_buffer,
449                    RTE_MIN(args.out_size, len));
450         /* parse  VF config message back from PF*/
451         iavf_vf_parse_hw_config(hw, vf->vf_res);
452         for (i = 0; i < vf->vf_res->num_vsis; i++) {
453                 if (vf->vf_res->vsi_res[i].vsi_type == VIRTCHNL_VSI_SRIOV)
454                         vf->vsi_res = &vf->vf_res->vsi_res[i];
455         }
456
457         if (!vf->vsi_res) {
458                 PMD_INIT_LOG(ERR, "no LAN VSI found");
459                 return -1;
460         }
461
462         vf->vsi.vsi_id = vf->vsi_res->vsi_id;
463         vf->vsi.nb_qps = vf->vsi_res->num_queue_pairs;
464         vf->vsi.adapter = adapter;
465
466         return 0;
467 }
468
469 int
470 iavf_get_supported_rxdid(struct iavf_adapter *adapter)
471 {
472         struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
473         struct iavf_cmd_info args;
474         int ret;
475
476         args.ops = VIRTCHNL_OP_GET_SUPPORTED_RXDIDS;
477         args.in_args = NULL;
478         args.in_args_size = 0;
479         args.out_buffer = vf->aq_resp;
480         args.out_size = IAVF_AQ_BUF_SZ;
481
482         ret = iavf_execute_vf_cmd(adapter, &args);
483         if (ret) {
484                 PMD_DRV_LOG(ERR,
485                             "Failed to execute command of OP_GET_SUPPORTED_RXDIDS");
486                 return ret;
487         }
488
489         vf->supported_rxdid =
490                 ((struct virtchnl_supported_rxdids *)args.out_buffer)->supported_rxdids;
491
492         return 0;
493 }
494
495 int
496 iavf_enable_queues(struct iavf_adapter *adapter)
497 {
498         struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
499         struct virtchnl_queue_select queue_select;
500         struct iavf_cmd_info args;
501         int err;
502
503         memset(&queue_select, 0, sizeof(queue_select));
504         queue_select.vsi_id = vf->vsi_res->vsi_id;
505
506         queue_select.rx_queues = BIT(adapter->eth_dev->data->nb_rx_queues) - 1;
507         queue_select.tx_queues = BIT(adapter->eth_dev->data->nb_tx_queues) - 1;
508
509         args.ops = VIRTCHNL_OP_ENABLE_QUEUES;
510         args.in_args = (u8 *)&queue_select;
511         args.in_args_size = sizeof(queue_select);
512         args.out_buffer = vf->aq_resp;
513         args.out_size = IAVF_AQ_BUF_SZ;
514         err = iavf_execute_vf_cmd(adapter, &args);
515         if (err) {
516                 PMD_DRV_LOG(ERR,
517                             "Failed to execute command of OP_ENABLE_QUEUES");
518                 return err;
519         }
520         return 0;
521 }
522
523 int
524 iavf_disable_queues(struct iavf_adapter *adapter)
525 {
526         struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
527         struct virtchnl_queue_select queue_select;
528         struct iavf_cmd_info args;
529         int err;
530
531         memset(&queue_select, 0, sizeof(queue_select));
532         queue_select.vsi_id = vf->vsi_res->vsi_id;
533
534         queue_select.rx_queues = BIT(adapter->eth_dev->data->nb_rx_queues) - 1;
535         queue_select.tx_queues = BIT(adapter->eth_dev->data->nb_tx_queues) - 1;
536
537         args.ops = VIRTCHNL_OP_DISABLE_QUEUES;
538         args.in_args = (u8 *)&queue_select;
539         args.in_args_size = sizeof(queue_select);
540         args.out_buffer = vf->aq_resp;
541         args.out_size = IAVF_AQ_BUF_SZ;
542         err = iavf_execute_vf_cmd(adapter, &args);
543         if (err) {
544                 PMD_DRV_LOG(ERR,
545                             "Failed to execute command of OP_DISABLE_QUEUES");
546                 return err;
547         }
548         return 0;
549 }
550
551 int
552 iavf_switch_queue(struct iavf_adapter *adapter, uint16_t qid,
553                  bool rx, bool on)
554 {
555         struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
556         struct virtchnl_queue_select queue_select;
557         struct iavf_cmd_info args;
558         int err;
559
560         memset(&queue_select, 0, sizeof(queue_select));
561         queue_select.vsi_id = vf->vsi_res->vsi_id;
562         if (rx)
563                 queue_select.rx_queues |= 1 << qid;
564         else
565                 queue_select.tx_queues |= 1 << qid;
566
567         if (on)
568                 args.ops = VIRTCHNL_OP_ENABLE_QUEUES;
569         else
570                 args.ops = VIRTCHNL_OP_DISABLE_QUEUES;
571         args.in_args = (u8 *)&queue_select;
572         args.in_args_size = sizeof(queue_select);
573         args.out_buffer = vf->aq_resp;
574         args.out_size = IAVF_AQ_BUF_SZ;
575         err = iavf_execute_vf_cmd(adapter, &args);
576         if (err)
577                 PMD_DRV_LOG(ERR, "Failed to execute command of %s",
578                             on ? "OP_ENABLE_QUEUES" : "OP_DISABLE_QUEUES");
579         return err;
580 }
581
582 int
583 iavf_configure_rss_lut(struct iavf_adapter *adapter)
584 {
585         struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
586         struct virtchnl_rss_lut *rss_lut;
587         struct iavf_cmd_info args;
588         int len, err = 0;
589
590         len = sizeof(*rss_lut) + vf->vf_res->rss_lut_size - 1;
591         rss_lut = rte_zmalloc("rss_lut", len, 0);
592         if (!rss_lut)
593                 return -ENOMEM;
594
595         rss_lut->vsi_id = vf->vsi_res->vsi_id;
596         rss_lut->lut_entries = vf->vf_res->rss_lut_size;
597         rte_memcpy(rss_lut->lut, vf->rss_lut, vf->vf_res->rss_lut_size);
598
599         args.ops = VIRTCHNL_OP_CONFIG_RSS_LUT;
600         args.in_args = (u8 *)rss_lut;
601         args.in_args_size = len;
602         args.out_buffer = vf->aq_resp;
603         args.out_size = IAVF_AQ_BUF_SZ;
604
605         err = iavf_execute_vf_cmd(adapter, &args);
606         if (err)
607                 PMD_DRV_LOG(ERR,
608                             "Failed to execute command of OP_CONFIG_RSS_LUT");
609
610         rte_free(rss_lut);
611         return err;
612 }
613
614 int
615 iavf_configure_rss_key(struct iavf_adapter *adapter)
616 {
617         struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
618         struct virtchnl_rss_key *rss_key;
619         struct iavf_cmd_info args;
620         int len, err = 0;
621
622         len = sizeof(*rss_key) + vf->vf_res->rss_key_size - 1;
623         rss_key = rte_zmalloc("rss_key", len, 0);
624         if (!rss_key)
625                 return -ENOMEM;
626
627         rss_key->vsi_id = vf->vsi_res->vsi_id;
628         rss_key->key_len = vf->vf_res->rss_key_size;
629         rte_memcpy(rss_key->key, vf->rss_key, vf->vf_res->rss_key_size);
630
631         args.ops = VIRTCHNL_OP_CONFIG_RSS_KEY;
632         args.in_args = (u8 *)rss_key;
633         args.in_args_size = len;
634         args.out_buffer = vf->aq_resp;
635         args.out_size = IAVF_AQ_BUF_SZ;
636
637         err = iavf_execute_vf_cmd(adapter, &args);
638         if (err)
639                 PMD_DRV_LOG(ERR,
640                             "Failed to execute command of OP_CONFIG_RSS_KEY");
641
642         rte_free(rss_key);
643         return err;
644 }
645
646 int
647 iavf_configure_queues(struct iavf_adapter *adapter)
648 {
649         struct iavf_rx_queue **rxq =
650                 (struct iavf_rx_queue **)adapter->eth_dev->data->rx_queues;
651         struct iavf_tx_queue **txq =
652                 (struct iavf_tx_queue **)adapter->eth_dev->data->tx_queues;
653         struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
654         struct virtchnl_vsi_queue_config_info *vc_config;
655         struct virtchnl_queue_pair_info *vc_qp;
656         struct iavf_cmd_info args;
657         uint16_t i, size;
658         int err;
659
660         size = sizeof(*vc_config) +
661                sizeof(vc_config->qpair[0]) * vf->num_queue_pairs;
662         vc_config = rte_zmalloc("cfg_queue", size, 0);
663         if (!vc_config)
664                 return -ENOMEM;
665
666         vc_config->vsi_id = vf->vsi_res->vsi_id;
667         vc_config->num_queue_pairs = vf->num_queue_pairs;
668
669         for (i = 0, vc_qp = vc_config->qpair;
670              i < vf->num_queue_pairs;
671              i++, vc_qp++) {
672                 vc_qp->txq.vsi_id = vf->vsi_res->vsi_id;
673                 vc_qp->txq.queue_id = i;
674                 /* Virtchnnl configure queues by pairs */
675                 if (i < adapter->eth_dev->data->nb_tx_queues) {
676                         vc_qp->txq.ring_len = txq[i]->nb_tx_desc;
677                         vc_qp->txq.dma_ring_addr = txq[i]->tx_ring_phys_addr;
678                 }
679                 vc_qp->rxq.vsi_id = vf->vsi_res->vsi_id;
680                 vc_qp->rxq.queue_id = i;
681                 vc_qp->rxq.max_pkt_size = vf->max_pkt_len;
682                 /* Virtchnnl configure queues by pairs */
683                 if (i < adapter->eth_dev->data->nb_rx_queues) {
684                         vc_qp->rxq.ring_len = rxq[i]->nb_rx_desc;
685                         vc_qp->rxq.dma_ring_addr = rxq[i]->rx_ring_phys_addr;
686                         vc_qp->rxq.databuffer_size = rxq[i]->rx_buf_len;
687                 }
688
689 #ifndef RTE_LIBRTE_IAVF_16BYTE_RX_DESC
690                 if (vf->vf_res->vf_cap_flags &
691                         VIRTCHNL_VF_OFFLOAD_RX_FLEX_DESC &&
692                         vf->supported_rxdid & BIT(IAVF_RXDID_COMMS_OVS_1)) {
693                         vc_qp->rxq.rxdid = IAVF_RXDID_COMMS_OVS_1;
694                         PMD_DRV_LOG(NOTICE, "request RXDID == %d in "
695                                         "Queue[%d]", vc_qp->rxq.rxdid, i);
696                 } else {
697                         vc_qp->rxq.rxdid = IAVF_RXDID_LEGACY_1;
698                         PMD_DRV_LOG(NOTICE, "request RXDID == %d in "
699                                         "Queue[%d]", vc_qp->rxq.rxdid, i);
700                 }
701 #else
702                 if (vf->vf_res->vf_cap_flags &
703                         VIRTCHNL_VF_OFFLOAD_RX_FLEX_DESC &&
704                         vf->supported_rxdid & BIT(IAVF_RXDID_LEGACY_0)) {
705                         vc_qp->rxq.rxdid = IAVF_RXDID_LEGACY_0;
706                         PMD_DRV_LOG(NOTICE, "request RXDID == %d in "
707                                         "Queue[%d]", vc_qp->rxq.rxdid, i);
708                 } else {
709                         PMD_DRV_LOG(ERR, "RXDID == 0 is not supported");
710                         return -1;
711                 }
712 #endif
713         }
714
715         memset(&args, 0, sizeof(args));
716         args.ops = VIRTCHNL_OP_CONFIG_VSI_QUEUES;
717         args.in_args = (uint8_t *)vc_config;
718         args.in_args_size = size;
719         args.out_buffer = vf->aq_resp;
720         args.out_size = IAVF_AQ_BUF_SZ;
721
722         err = iavf_execute_vf_cmd(adapter, &args);
723         if (err)
724                 PMD_DRV_LOG(ERR, "Failed to execute command of"
725                             " VIRTCHNL_OP_CONFIG_VSI_QUEUES");
726
727         rte_free(vc_config);
728         return err;
729 }
730
731 int
732 iavf_config_irq_map(struct iavf_adapter *adapter)
733 {
734         struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
735         struct virtchnl_irq_map_info *map_info;
736         struct virtchnl_vector_map *vecmap;
737         struct iavf_cmd_info args;
738         int len, i, err;
739
740         len = sizeof(struct virtchnl_irq_map_info) +
741               sizeof(struct virtchnl_vector_map) * vf->nb_msix;
742
743         map_info = rte_zmalloc("map_info", len, 0);
744         if (!map_info)
745                 return -ENOMEM;
746
747         map_info->num_vectors = vf->nb_msix;
748         for (i = 0; i < vf->nb_msix; i++) {
749                 vecmap = &map_info->vecmap[i];
750                 vecmap->vsi_id = vf->vsi_res->vsi_id;
751                 vecmap->rxitr_idx = IAVF_ITR_INDEX_DEFAULT;
752                 vecmap->vector_id = vf->msix_base + i;
753                 vecmap->txq_map = 0;
754                 vecmap->rxq_map = vf->rxq_map[vf->msix_base + i];
755         }
756
757         args.ops = VIRTCHNL_OP_CONFIG_IRQ_MAP;
758         args.in_args = (u8 *)map_info;
759         args.in_args_size = len;
760         args.out_buffer = vf->aq_resp;
761         args.out_size = IAVF_AQ_BUF_SZ;
762         err = iavf_execute_vf_cmd(adapter, &args);
763         if (err)
764                 PMD_DRV_LOG(ERR, "fail to execute command OP_CONFIG_IRQ_MAP");
765
766         rte_free(map_info);
767         return err;
768 }
769
770 void
771 iavf_add_del_all_mac_addr(struct iavf_adapter *adapter, bool add)
772 {
773         struct virtchnl_ether_addr_list *list;
774         struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
775         struct rte_ether_addr *addr;
776         struct iavf_cmd_info args;
777         int len, err, i, j;
778         int next_begin = 0;
779         int begin = 0;
780
781         do {
782                 j = 0;
783                 len = sizeof(struct virtchnl_ether_addr_list);
784                 for (i = begin; i < IAVF_NUM_MACADDR_MAX; i++, next_begin++) {
785                         addr = &adapter->eth_dev->data->mac_addrs[i];
786                         if (rte_is_zero_ether_addr(addr))
787                                 continue;
788                         len += sizeof(struct virtchnl_ether_addr);
789                         if (len >= IAVF_AQ_BUF_SZ) {
790                                 next_begin = i + 1;
791                                 break;
792                         }
793                 }
794
795                 list = rte_zmalloc("iavf_del_mac_buffer", len, 0);
796                 if (!list) {
797                         PMD_DRV_LOG(ERR, "fail to allocate memory");
798                         return;
799                 }
800
801                 for (i = begin; i < next_begin; i++) {
802                         addr = &adapter->eth_dev->data->mac_addrs[i];
803                         if (rte_is_zero_ether_addr(addr))
804                                 continue;
805                         rte_memcpy(list->list[j].addr, addr->addr_bytes,
806                                    sizeof(addr->addr_bytes));
807                         PMD_DRV_LOG(DEBUG, "add/rm mac:%x:%x:%x:%x:%x:%x",
808                                     addr->addr_bytes[0], addr->addr_bytes[1],
809                                     addr->addr_bytes[2], addr->addr_bytes[3],
810                                     addr->addr_bytes[4], addr->addr_bytes[5]);
811                         j++;
812                 }
813                 list->vsi_id = vf->vsi_res->vsi_id;
814                 list->num_elements = j;
815                 args.ops = add ? VIRTCHNL_OP_ADD_ETH_ADDR :
816                            VIRTCHNL_OP_DEL_ETH_ADDR;
817                 args.in_args = (uint8_t *)list;
818                 args.in_args_size = len;
819                 args.out_buffer = vf->aq_resp;
820                 args.out_size = IAVF_AQ_BUF_SZ;
821                 err = iavf_execute_vf_cmd(adapter, &args);
822                 if (err)
823                         PMD_DRV_LOG(ERR, "fail to execute command %s",
824                                     add ? "OP_ADD_ETHER_ADDRESS" :
825                                     "OP_DEL_ETHER_ADDRESS");
826                 rte_free(list);
827                 begin = next_begin;
828         } while (begin < IAVF_NUM_MACADDR_MAX);
829 }
830
831 int
832 iavf_query_stats(struct iavf_adapter *adapter,
833                 struct virtchnl_eth_stats **pstats)
834 {
835         struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
836         struct virtchnl_queue_select q_stats;
837         struct iavf_cmd_info args;
838         int err;
839
840         memset(&q_stats, 0, sizeof(q_stats));
841         q_stats.vsi_id = vf->vsi_res->vsi_id;
842         args.ops = VIRTCHNL_OP_GET_STATS;
843         args.in_args = (uint8_t *)&q_stats;
844         args.in_args_size = sizeof(q_stats);
845         args.out_buffer = vf->aq_resp;
846         args.out_size = IAVF_AQ_BUF_SZ;
847
848         err = iavf_execute_vf_cmd(adapter, &args);
849         if (err) {
850                 PMD_DRV_LOG(ERR, "fail to execute command OP_GET_STATS");
851                 *pstats = NULL;
852                 return err;
853         }
854         *pstats = (struct virtchnl_eth_stats *)args.out_buffer;
855         return 0;
856 }
857
858 int
859 iavf_config_promisc(struct iavf_adapter *adapter,
860                    bool enable_unicast,
861                    bool enable_multicast)
862 {
863         struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
864         struct virtchnl_promisc_info promisc;
865         struct iavf_cmd_info args;
866         int err;
867
868         promisc.flags = 0;
869         promisc.vsi_id = vf->vsi_res->vsi_id;
870
871         if (enable_unicast)
872                 promisc.flags |= FLAG_VF_UNICAST_PROMISC;
873
874         if (enable_multicast)
875                 promisc.flags |= FLAG_VF_MULTICAST_PROMISC;
876
877         args.ops = VIRTCHNL_OP_CONFIG_PROMISCUOUS_MODE;
878         args.in_args = (uint8_t *)&promisc;
879         args.in_args_size = sizeof(promisc);
880         args.out_buffer = vf->aq_resp;
881         args.out_size = IAVF_AQ_BUF_SZ;
882
883         err = iavf_execute_vf_cmd(adapter, &args);
884
885         if (err) {
886                 PMD_DRV_LOG(ERR,
887                             "fail to execute command CONFIG_PROMISCUOUS_MODE");
888
889                 if (err == IAVF_NOT_SUPPORTED)
890                         return -ENOTSUP;
891
892                 return -EAGAIN;
893         }
894
895         vf->promisc_unicast_enabled = enable_unicast;
896         vf->promisc_multicast_enabled = enable_multicast;
897         return 0;
898 }
899
900 int
901 iavf_add_del_eth_addr(struct iavf_adapter *adapter, struct rte_ether_addr *addr,
902                      bool add)
903 {
904         struct virtchnl_ether_addr_list *list;
905         struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
906         uint8_t cmd_buffer[sizeof(struct virtchnl_ether_addr_list) +
907                            sizeof(struct virtchnl_ether_addr)];
908         struct iavf_cmd_info args;
909         int err;
910
911         list = (struct virtchnl_ether_addr_list *)cmd_buffer;
912         list->vsi_id = vf->vsi_res->vsi_id;
913         list->num_elements = 1;
914         rte_memcpy(list->list[0].addr, addr->addr_bytes,
915                    sizeof(addr->addr_bytes));
916
917         args.ops = add ? VIRTCHNL_OP_ADD_ETH_ADDR : VIRTCHNL_OP_DEL_ETH_ADDR;
918         args.in_args = cmd_buffer;
919         args.in_args_size = sizeof(cmd_buffer);
920         args.out_buffer = vf->aq_resp;
921         args.out_size = IAVF_AQ_BUF_SZ;
922         err = iavf_execute_vf_cmd(adapter, &args);
923         if (err)
924                 PMD_DRV_LOG(ERR, "fail to execute command %s",
925                             add ? "OP_ADD_ETH_ADDR" :  "OP_DEL_ETH_ADDR");
926         return err;
927 }
928
929 int
930 iavf_add_del_vlan(struct iavf_adapter *adapter, uint16_t vlanid, bool add)
931 {
932         struct virtchnl_vlan_filter_list *vlan_list;
933         struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
934         uint8_t cmd_buffer[sizeof(struct virtchnl_vlan_filter_list) +
935                                                         sizeof(uint16_t)];
936         struct iavf_cmd_info args;
937         int err;
938
939         vlan_list = (struct virtchnl_vlan_filter_list *)cmd_buffer;
940         vlan_list->vsi_id = vf->vsi_res->vsi_id;
941         vlan_list->num_elements = 1;
942         vlan_list->vlan_id[0] = vlanid;
943
944         args.ops = add ? VIRTCHNL_OP_ADD_VLAN : VIRTCHNL_OP_DEL_VLAN;
945         args.in_args = cmd_buffer;
946         args.in_args_size = sizeof(cmd_buffer);
947         args.out_buffer = vf->aq_resp;
948         args.out_size = IAVF_AQ_BUF_SZ;
949         err = iavf_execute_vf_cmd(adapter, &args);
950         if (err)
951                 PMD_DRV_LOG(ERR, "fail to execute command %s",
952                             add ? "OP_ADD_VLAN" :  "OP_DEL_VLAN");
953
954         return err;
955 }
956
957 int
958 iavf_fdir_add(struct iavf_adapter *adapter,
959         struct iavf_fdir_conf *filter)
960 {
961         struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
962         struct virtchnl_fdir_add *fdir_ret;
963
964         struct iavf_cmd_info args;
965         int err;
966
967         filter->add_fltr.vsi_id = vf->vsi_res->vsi_id;
968         filter->add_fltr.validate_only = 0;
969
970         args.ops = VIRTCHNL_OP_ADD_FDIR_FILTER;
971         args.in_args = (uint8_t *)(&filter->add_fltr);
972         args.in_args_size = sizeof(*(&filter->add_fltr));
973         args.out_buffer = vf->aq_resp;
974         args.out_size = IAVF_AQ_BUF_SZ;
975
976         err = iavf_execute_vf_cmd(adapter, &args);
977         if (err) {
978                 PMD_DRV_LOG(ERR, "fail to execute command OP_ADD_FDIR_FILTER");
979                 return err;
980         }
981
982         fdir_ret = (struct virtchnl_fdir_add *)args.out_buffer;
983         filter->flow_id = fdir_ret->flow_id;
984
985         if (fdir_ret->status == VIRTCHNL_FDIR_SUCCESS) {
986                 PMD_DRV_LOG(INFO,
987                         "Succeed in adding rule request by PF");
988         } else if (fdir_ret->status == VIRTCHNL_FDIR_FAILURE_RULE_NORESOURCE) {
989                 PMD_DRV_LOG(ERR,
990                         "Failed to add rule request due to no hw resource");
991                 return -1;
992         } else if (fdir_ret->status == VIRTCHNL_FDIR_FAILURE_RULE_EXIST) {
993                 PMD_DRV_LOG(ERR,
994                         "Failed to add rule request due to the rule is already existed");
995                 return -1;
996         } else if (fdir_ret->status == VIRTCHNL_FDIR_FAILURE_RULE_CONFLICT) {
997                 PMD_DRV_LOG(ERR,
998                         "Failed to add rule request due to the rule is conflict with existing rule");
999                 return -1;
1000         } else if (fdir_ret->status == VIRTCHNL_FDIR_FAILURE_RULE_INVALID) {
1001                 PMD_DRV_LOG(ERR,
1002                         "Failed to add rule request due to the hw doesn't support");
1003                 return -1;
1004         } else if (fdir_ret->status == VIRTCHNL_FDIR_FAILURE_RULE_TIMEOUT) {
1005                 PMD_DRV_LOG(ERR,
1006                         "Failed to add rule request due to time out for programming");
1007                 return -1;
1008         } else {
1009                 PMD_DRV_LOG(ERR,
1010                         "Failed to add rule request due to other reasons");
1011                 return -1;
1012         }
1013
1014         return 0;
1015 };
1016
1017 int
1018 iavf_fdir_del(struct iavf_adapter *adapter,
1019         struct iavf_fdir_conf *filter)
1020 {
1021         struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
1022         struct virtchnl_fdir_del *fdir_ret;
1023
1024         struct iavf_cmd_info args;
1025         int err;
1026
1027         filter->del_fltr.vsi_id = vf->vsi_res->vsi_id;
1028         filter->del_fltr.flow_id = filter->flow_id;
1029
1030         args.ops = VIRTCHNL_OP_DEL_FDIR_FILTER;
1031         args.in_args = (uint8_t *)(&filter->del_fltr);
1032         args.in_args_size = sizeof(filter->del_fltr);
1033         args.out_buffer = vf->aq_resp;
1034         args.out_size = IAVF_AQ_BUF_SZ;
1035
1036         err = iavf_execute_vf_cmd(adapter, &args);
1037         if (err) {
1038                 PMD_DRV_LOG(ERR, "fail to execute command OP_DEL_FDIR_FILTER");
1039                 return err;
1040         }
1041
1042         fdir_ret = (struct virtchnl_fdir_del *)args.out_buffer;
1043
1044         if (fdir_ret->status == VIRTCHNL_FDIR_SUCCESS) {
1045                 PMD_DRV_LOG(INFO,
1046                         "Succeed in deleting rule request by PF");
1047         } else if (fdir_ret->status == VIRTCHNL_FDIR_FAILURE_RULE_NONEXIST) {
1048                 PMD_DRV_LOG(ERR,
1049                         "Failed to delete rule request due to this rule doesn't exist");
1050                 return -1;
1051         } else if (fdir_ret->status == VIRTCHNL_FDIR_FAILURE_RULE_TIMEOUT) {
1052                 PMD_DRV_LOG(ERR,
1053                         "Failed to delete rule request due to time out for programming");
1054                 return -1;
1055         } else {
1056                 PMD_DRV_LOG(ERR,
1057                         "Failed to delete rule request due to other reasons");
1058                 return -1;
1059         }
1060
1061         return 0;
1062 };
1063
1064 int
1065 iavf_fdir_check(struct iavf_adapter *adapter,
1066                 struct iavf_fdir_conf *filter)
1067 {
1068         struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
1069         struct virtchnl_fdir_add *fdir_ret;
1070
1071         struct iavf_cmd_info args;
1072         int err;
1073
1074         filter->add_fltr.vsi_id = vf->vsi_res->vsi_id;
1075         filter->add_fltr.validate_only = 1;
1076
1077         args.ops = VIRTCHNL_OP_ADD_FDIR_FILTER;
1078         args.in_args = (uint8_t *)(&filter->add_fltr);
1079         args.in_args_size = sizeof(*(&filter->add_fltr));
1080         args.out_buffer = vf->aq_resp;
1081         args.out_size = IAVF_AQ_BUF_SZ;
1082
1083         err = iavf_execute_vf_cmd(adapter, &args);
1084         if (err) {
1085                 PMD_DRV_LOG(ERR, "fail to check flow direcotor rule");
1086                 return err;
1087         }
1088
1089         fdir_ret = (struct virtchnl_fdir_add *)args.out_buffer;
1090
1091         if (fdir_ret->status == VIRTCHNL_FDIR_SUCCESS) {
1092                 PMD_DRV_LOG(INFO,
1093                         "Succeed in checking rule request by PF");
1094         } else if (fdir_ret->status == VIRTCHNL_FDIR_FAILURE_RULE_INVALID) {
1095                 PMD_DRV_LOG(ERR,
1096                         "Failed to check rule request due to parameters validation"
1097                         " or HW doesn't support");
1098                 return -1;
1099         } else {
1100                 PMD_DRV_LOG(ERR,
1101                         "Failed to check rule request due to other reasons");
1102                 return -1;
1103         }
1104
1105         return 0;
1106 }
1107
1108 int
1109 iavf_add_del_rss_cfg(struct iavf_adapter *adapter,
1110                      struct virtchnl_rss_cfg *rss_cfg, bool add)
1111 {
1112         struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
1113         struct iavf_cmd_info args;
1114         int err;
1115
1116         memset(&args, 0, sizeof(args));
1117         args.ops = add ? VIRTCHNL_OP_ADD_RSS_CFG :
1118                 VIRTCHNL_OP_DEL_RSS_CFG;
1119         args.in_args = (u8 *)rss_cfg;
1120         args.in_args_size = sizeof(*rss_cfg);
1121         args.out_buffer = vf->aq_resp;
1122         args.out_size = IAVF_AQ_BUF_SZ;
1123
1124         err = iavf_execute_vf_cmd(adapter, &args);
1125         if (err)
1126                 PMD_DRV_LOG(ERR,
1127                             "Failed to execute command of %s",
1128                             add ? "OP_ADD_RSS_CFG" :
1129                             "OP_DEL_RSS_INPUT_CFG");
1130
1131         return err;
1132 }
1133
1134 int
1135 iavf_add_del_mc_addr_list(struct iavf_adapter *adapter,
1136                         struct rte_ether_addr *mc_addrs,
1137                         uint32_t mc_addrs_num, bool add)
1138 {
1139         struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
1140         uint8_t cmd_buffer[sizeof(struct virtchnl_ether_addr_list) +
1141                 (IAVF_NUM_MACADDR_MAX * sizeof(struct virtchnl_ether_addr))];
1142         struct virtchnl_ether_addr_list *list;
1143         struct iavf_cmd_info args;
1144         uint32_t i;
1145         int err;
1146
1147         if (mc_addrs == NULL || mc_addrs_num == 0)
1148                 return 0;
1149
1150         list = (struct virtchnl_ether_addr_list *)cmd_buffer;
1151         list->vsi_id = vf->vsi_res->vsi_id;
1152         list->num_elements = mc_addrs_num;
1153
1154         for (i = 0; i < mc_addrs_num; i++) {
1155                 if (!IAVF_IS_MULTICAST(mc_addrs[i].addr_bytes)) {
1156                         PMD_DRV_LOG(ERR, "Invalid mac:%x:%x:%x:%x:%x:%x",
1157                                     mc_addrs[i].addr_bytes[0],
1158                                     mc_addrs[i].addr_bytes[1],
1159                                     mc_addrs[i].addr_bytes[2],
1160                                     mc_addrs[i].addr_bytes[3],
1161                                     mc_addrs[i].addr_bytes[4],
1162                                     mc_addrs[i].addr_bytes[5]);
1163                         return -EINVAL;
1164                 }
1165
1166                 memcpy(list->list[i].addr, mc_addrs[i].addr_bytes,
1167                         sizeof(list->list[i].addr));
1168         }
1169
1170         args.ops = add ? VIRTCHNL_OP_ADD_ETH_ADDR : VIRTCHNL_OP_DEL_ETH_ADDR;
1171         args.in_args = cmd_buffer;
1172         args.in_args_size = sizeof(struct virtchnl_ether_addr_list) +
1173                 i * sizeof(struct virtchnl_ether_addr);
1174         args.out_buffer = vf->aq_resp;
1175         args.out_size = IAVF_AQ_BUF_SZ;
1176         err = iavf_execute_vf_cmd(adapter, &args);
1177
1178         if (err) {
1179                 PMD_DRV_LOG(ERR, "fail to execute command %s",
1180                         add ? "OP_ADD_ETH_ADDR" : "OP_DEL_ETH_ADDR");
1181                 return err;
1182         }
1183
1184         return 0;
1185 }