c33194cdc85ef4106b0a8679a25f69739a91652c
[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_ethdev_pci.h>
21 #include <rte_dev.h>
22
23 #include "iavf.h"
24 #include "iavf_rxtx.h"
25
26 #define MAX_TRY_TIMES 200
27 #define ASQ_DELAY_MS  10
28
29 static uint32_t
30 iavf_convert_link_speed(enum virtchnl_link_speed virt_link_speed)
31 {
32         uint32_t speed;
33
34         switch (virt_link_speed) {
35         case VIRTCHNL_LINK_SPEED_100MB:
36                 speed = 100;
37                 break;
38         case VIRTCHNL_LINK_SPEED_1GB:
39                 speed = 1000;
40                 break;
41         case VIRTCHNL_LINK_SPEED_10GB:
42                 speed = 10000;
43                 break;
44         case VIRTCHNL_LINK_SPEED_40GB:
45                 speed = 40000;
46                 break;
47         case VIRTCHNL_LINK_SPEED_20GB:
48                 speed = 20000;
49                 break;
50         case VIRTCHNL_LINK_SPEED_25GB:
51                 speed = 25000;
52                 break;
53         case VIRTCHNL_LINK_SPEED_2_5GB:
54                 speed = 2500;
55                 break;
56         case VIRTCHNL_LINK_SPEED_5GB:
57                 speed = 5000;
58                 break;
59         default:
60                 speed = 0;
61                 break;
62         }
63
64         return speed;
65 }
66
67 /* Read data in admin queue to get msg from pf driver */
68 static enum iavf_aq_result
69 iavf_read_msg_from_pf(struct iavf_adapter *adapter, uint16_t buf_len,
70                      uint8_t *buf)
71 {
72         struct iavf_hw *hw = IAVF_DEV_PRIVATE_TO_HW(adapter);
73         struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
74         struct rte_eth_dev *dev = adapter->eth_dev;
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(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 {
148         struct iavf_hw *hw = IAVF_DEV_PRIVATE_TO_HW(adapter);
149         struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
150         enum iavf_aq_result result;
151         enum iavf_status ret;
152         int err = 0;
153         int i = 0;
154
155         if (vf->vf_reset)
156                 return -EIO;
157
158         if (_atomic_set_cmd(vf, args->ops))
159                 return -1;
160
161         ret = iavf_aq_send_msg_to_pf(hw, args->ops, IAVF_SUCCESS,
162                                     args->in_args, args->in_args_size, NULL);
163         if (ret) {
164                 PMD_DRV_LOG(ERR, "fail to send cmd %d", args->ops);
165                 _clear_cmd(vf);
166                 return err;
167         }
168
169         switch (args->ops) {
170         case VIRTCHNL_OP_RESET_VF:
171                 /*no need to wait for response */
172                 _clear_cmd(vf);
173                 break;
174         case VIRTCHNL_OP_VERSION:
175         case VIRTCHNL_OP_GET_VF_RESOURCES:
176         case VIRTCHNL_OP_GET_SUPPORTED_RXDIDS:
177                 /* for init virtchnl ops, need to poll the response */
178                 do {
179                         result = iavf_read_msg_from_pf(adapter, args->out_size,
180                                                    args->out_buffer);
181                         if (result == IAVF_MSG_CMD)
182                                 break;
183                         rte_delay_ms(ASQ_DELAY_MS);
184                 } while (i++ < MAX_TRY_TIMES);
185                 if (i >= MAX_TRY_TIMES ||
186                     vf->cmd_retval != VIRTCHNL_STATUS_SUCCESS) {
187                         err = -1;
188                         PMD_DRV_LOG(ERR, "No response or return failure (%d)"
189                                     " for cmd %d", vf->cmd_retval, args->ops);
190                 }
191                 _clear_cmd(vf);
192                 break;
193         case VIRTCHNL_OP_REQUEST_QUEUES:
194                 /*
195                  * ignore async reply, only wait for system message,
196                  * vf_reset = true if get VIRTCHNL_EVENT_RESET_IMPENDING,
197                  * if not, means request queues failed.
198                  */
199                 do {
200                         result = iavf_read_msg_from_pf(adapter, args->out_size,
201                                                    args->out_buffer);
202                         if (result == IAVF_MSG_SYS && vf->vf_reset) {
203                                 break;
204                         } else if (result == IAVF_MSG_CMD ||
205                                 result == IAVF_MSG_ERR) {
206                                 err = -1;
207                                 break;
208                         }
209                         rte_delay_ms(ASQ_DELAY_MS);
210                         /* If don't read msg or read sys event, continue */
211                 } while (i++ < MAX_TRY_TIMES);
212                 if (i >= MAX_TRY_TIMES ||
213                         vf->cmd_retval != VIRTCHNL_STATUS_SUCCESS) {
214                         err = -1;
215                         PMD_DRV_LOG(ERR, "No response or return failure (%d)"
216                                     " for cmd %d", vf->cmd_retval, args->ops);
217                 }
218                 _clear_cmd(vf);
219                 break;
220         default:
221                 /* For other virtchnl ops in running time,
222                  * wait for the cmd done flag.
223                  */
224                 do {
225                         if (vf->pend_cmd == VIRTCHNL_OP_UNKNOWN)
226                                 break;
227                         rte_delay_ms(ASQ_DELAY_MS);
228                         /* If don't read msg or read sys event, continue */
229                 } while (i++ < MAX_TRY_TIMES);
230                 /* If there's no response is received, clear command */
231                 if (i >= MAX_TRY_TIMES  ||
232                     vf->cmd_retval != VIRTCHNL_STATUS_SUCCESS) {
233                         err = -1;
234                         PMD_DRV_LOG(ERR, "No response or return failure (%d)"
235                                     " for cmd %d", vf->cmd_retval, args->ops);
236                         _clear_cmd(vf);
237                 }
238                 break;
239         }
240
241         return err;
242 }
243
244 static void
245 iavf_handle_pf_event_msg(struct rte_eth_dev *dev, uint8_t *msg,
246                         uint16_t msglen)
247 {
248         struct virtchnl_pf_event *pf_msg =
249                         (struct virtchnl_pf_event *)msg;
250         struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(dev->data->dev_private);
251
252         if (msglen < sizeof(struct virtchnl_pf_event)) {
253                 PMD_DRV_LOG(DEBUG, "Error event");
254                 return;
255         }
256         switch (pf_msg->event) {
257         case VIRTCHNL_EVENT_RESET_IMPENDING:
258                 PMD_DRV_LOG(DEBUG, "VIRTCHNL_EVENT_RESET_IMPENDING event");
259                 vf->vf_reset = true;
260                 rte_eth_dev_callback_process(dev, RTE_ETH_EVENT_INTR_RESET,
261                                               NULL);
262                 break;
263         case VIRTCHNL_EVENT_LINK_CHANGE:
264                 PMD_DRV_LOG(DEBUG, "VIRTCHNL_EVENT_LINK_CHANGE event");
265                 vf->link_up = pf_msg->event_data.link_event.link_status;
266                 if (vf->vf_res->vf_cap_flags & VIRTCHNL_VF_CAP_ADV_LINK_SPEED) {
267                         vf->link_speed =
268                                 pf_msg->event_data.link_event_adv.link_speed;
269                 } else {
270                         enum virtchnl_link_speed speed;
271                         speed = pf_msg->event_data.link_event.link_speed;
272                         vf->link_speed = iavf_convert_link_speed(speed);
273                 }
274                 iavf_dev_link_update(dev, 0);
275                 rte_eth_dev_callback_process(dev, RTE_ETH_EVENT_INTR_LSC, NULL);
276                 break;
277         case VIRTCHNL_EVENT_PF_DRIVER_CLOSE:
278                 PMD_DRV_LOG(DEBUG, "VIRTCHNL_EVENT_PF_DRIVER_CLOSE event");
279                 break;
280         default:
281                 PMD_DRV_LOG(ERR, " unknown event received %u", pf_msg->event);
282                 break;
283         }
284 }
285
286 void
287 iavf_handle_virtchnl_msg(struct rte_eth_dev *dev)
288 {
289         struct iavf_hw *hw = IAVF_DEV_PRIVATE_TO_HW(dev->data->dev_private);
290         struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(dev->data->dev_private);
291         struct iavf_arq_event_info info;
292         uint16_t pending, aq_opc;
293         enum virtchnl_ops msg_opc;
294         enum iavf_status msg_ret;
295         int ret;
296
297         info.buf_len = IAVF_AQ_BUF_SZ;
298         if (!vf->aq_resp) {
299                 PMD_DRV_LOG(ERR, "Buffer for adminq resp should not be NULL");
300                 return;
301         }
302         info.msg_buf = vf->aq_resp;
303
304         pending = 1;
305         while (pending) {
306                 ret = iavf_clean_arq_element(hw, &info, &pending);
307
308                 if (ret != IAVF_SUCCESS) {
309                         PMD_DRV_LOG(INFO, "Failed to read msg from AdminQ,"
310                                     "ret: %d", ret);
311                         break;
312                 }
313                 aq_opc = rte_le_to_cpu_16(info.desc.opcode);
314                 /* For the message sent from pf to vf, opcode is stored in
315                  * cookie_high of struct iavf_aq_desc, while return error code
316                  * are stored in cookie_low, Which is done by PF driver.
317                  */
318                 msg_opc = (enum virtchnl_ops)rte_le_to_cpu_32(
319                                                   info.desc.cookie_high);
320                 msg_ret = (enum iavf_status)rte_le_to_cpu_32(
321                                                   info.desc.cookie_low);
322                 switch (aq_opc) {
323                 case iavf_aqc_opc_send_msg_to_vf:
324                         if (msg_opc == VIRTCHNL_OP_EVENT) {
325                                 iavf_handle_pf_event_msg(dev, info.msg_buf,
326                                                         info.msg_len);
327                         } else {
328                                 /* read message and it's expected one */
329                                 if (msg_opc == vf->pend_cmd)
330                                         _notify_cmd(vf, msg_ret);
331                                 else
332                                         PMD_DRV_LOG(ERR, "command mismatch,"
333                                                     "expect %u, get %u",
334                                                     vf->pend_cmd, msg_opc);
335                                 PMD_DRV_LOG(DEBUG,
336                                             "adminq response is received,"
337                                             " opcode = %d", msg_opc);
338                         }
339                         break;
340                 default:
341                         PMD_DRV_LOG(DEBUG, "Request %u is not supported yet",
342                                     aq_opc);
343                         break;
344                 }
345         }
346 }
347
348 int
349 iavf_enable_vlan_strip(struct iavf_adapter *adapter)
350 {
351         struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
352         struct iavf_cmd_info args;
353         int ret;
354
355         memset(&args, 0, sizeof(args));
356         args.ops = VIRTCHNL_OP_ENABLE_VLAN_STRIPPING;
357         args.in_args = NULL;
358         args.in_args_size = 0;
359         args.out_buffer = vf->aq_resp;
360         args.out_size = IAVF_AQ_BUF_SZ;
361         ret = iavf_execute_vf_cmd(adapter, &args);
362         if (ret)
363                 PMD_DRV_LOG(ERR, "Failed to execute command of"
364                             " OP_ENABLE_VLAN_STRIPPING");
365
366         return ret;
367 }
368
369 int
370 iavf_disable_vlan_strip(struct iavf_adapter *adapter)
371 {
372         struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
373         struct iavf_cmd_info args;
374         int ret;
375
376         memset(&args, 0, sizeof(args));
377         args.ops = VIRTCHNL_OP_DISABLE_VLAN_STRIPPING;
378         args.in_args = NULL;
379         args.in_args_size = 0;
380         args.out_buffer = vf->aq_resp;
381         args.out_size = IAVF_AQ_BUF_SZ;
382         ret = iavf_execute_vf_cmd(adapter, &args);
383         if (ret)
384                 PMD_DRV_LOG(ERR, "Failed to execute command of"
385                             " OP_DISABLE_VLAN_STRIPPING");
386
387         return ret;
388 }
389
390 #define VIRTCHNL_VERSION_MAJOR_START 1
391 #define VIRTCHNL_VERSION_MINOR_START 1
392
393 /* Check API version with sync wait until version read from admin queue */
394 int
395 iavf_check_api_version(struct iavf_adapter *adapter)
396 {
397         struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
398         struct virtchnl_version_info version, *pver;
399         struct iavf_cmd_info args;
400         int err;
401
402         version.major = VIRTCHNL_VERSION_MAJOR;
403         version.minor = VIRTCHNL_VERSION_MINOR;
404
405         args.ops = VIRTCHNL_OP_VERSION;
406         args.in_args = (uint8_t *)&version;
407         args.in_args_size = sizeof(version);
408         args.out_buffer = vf->aq_resp;
409         args.out_size = IAVF_AQ_BUF_SZ;
410
411         err = iavf_execute_vf_cmd(adapter, &args);
412         if (err) {
413                 PMD_INIT_LOG(ERR, "Fail to execute command of OP_VERSION");
414                 return err;
415         }
416
417         pver = (struct virtchnl_version_info *)args.out_buffer;
418         vf->virtchnl_version = *pver;
419
420         if (vf->virtchnl_version.major < VIRTCHNL_VERSION_MAJOR_START ||
421             (vf->virtchnl_version.major == VIRTCHNL_VERSION_MAJOR_START &&
422              vf->virtchnl_version.minor < VIRTCHNL_VERSION_MINOR_START)) {
423                 PMD_INIT_LOG(ERR, "VIRTCHNL API version should not be lower"
424                              " than (%u.%u) to support Adapative VF",
425                              VIRTCHNL_VERSION_MAJOR_START,
426                              VIRTCHNL_VERSION_MAJOR_START);
427                 return -1;
428         } else if (vf->virtchnl_version.major > VIRTCHNL_VERSION_MAJOR ||
429                    (vf->virtchnl_version.major == VIRTCHNL_VERSION_MAJOR &&
430                     vf->virtchnl_version.minor > VIRTCHNL_VERSION_MINOR)) {
431                 PMD_INIT_LOG(ERR, "PF/VF API version mismatch:(%u.%u)-(%u.%u)",
432                              vf->virtchnl_version.major,
433                              vf->virtchnl_version.minor,
434                              VIRTCHNL_VERSION_MAJOR,
435                              VIRTCHNL_VERSION_MINOR);
436                 return -1;
437         }
438
439         PMD_DRV_LOG(DEBUG, "Peer is supported PF host");
440         return 0;
441 }
442
443 int
444 iavf_get_vf_resource(struct iavf_adapter *adapter)
445 {
446         struct iavf_hw *hw = IAVF_DEV_PRIVATE_TO_HW(adapter);
447         struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
448         struct iavf_cmd_info args;
449         uint32_t caps, len;
450         int err, i;
451
452         args.ops = VIRTCHNL_OP_GET_VF_RESOURCES;
453         args.out_buffer = vf->aq_resp;
454         args.out_size = IAVF_AQ_BUF_SZ;
455
456         caps = IAVF_BASIC_OFFLOAD_CAPS | VIRTCHNL_VF_CAP_ADV_LINK_SPEED |
457                 VIRTCHNL_VF_OFFLOAD_RX_FLEX_DESC |
458                 VIRTCHNL_VF_OFFLOAD_FDIR_PF |
459                 VIRTCHNL_VF_OFFLOAD_ADV_RSS_PF |
460                 VIRTCHNL_VF_OFFLOAD_REQ_QUEUES |
461                 VIRTCHNL_VF_OFFLOAD_CRC |
462                 VIRTCHNL_VF_LARGE_NUM_QPAIRS;
463
464         args.in_args = (uint8_t *)&caps;
465         args.in_args_size = sizeof(caps);
466
467         err = iavf_execute_vf_cmd(adapter, &args);
468
469         if (err) {
470                 PMD_DRV_LOG(ERR,
471                             "Failed to execute command of OP_GET_VF_RESOURCE");
472                 return -1;
473         }
474
475         len =  sizeof(struct virtchnl_vf_resource) +
476                       IAVF_MAX_VF_VSI * sizeof(struct virtchnl_vsi_resource);
477
478         rte_memcpy(vf->vf_res, args.out_buffer,
479                    RTE_MIN(args.out_size, len));
480         /* parse  VF config message back from PF*/
481         iavf_vf_parse_hw_config(hw, vf->vf_res);
482         for (i = 0; i < vf->vf_res->num_vsis; i++) {
483                 if (vf->vf_res->vsi_res[i].vsi_type == VIRTCHNL_VSI_SRIOV)
484                         vf->vsi_res = &vf->vf_res->vsi_res[i];
485         }
486
487         if (!vf->vsi_res) {
488                 PMD_INIT_LOG(ERR, "no LAN VSI found");
489                 return -1;
490         }
491
492         vf->vsi.vsi_id = vf->vsi_res->vsi_id;
493         vf->vsi.nb_qps = vf->vsi_res->num_queue_pairs;
494         vf->vsi.adapter = adapter;
495
496         return 0;
497 }
498
499 int
500 iavf_get_supported_rxdid(struct iavf_adapter *adapter)
501 {
502         struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
503         struct iavf_cmd_info args;
504         int ret;
505
506         args.ops = VIRTCHNL_OP_GET_SUPPORTED_RXDIDS;
507         args.in_args = NULL;
508         args.in_args_size = 0;
509         args.out_buffer = vf->aq_resp;
510         args.out_size = IAVF_AQ_BUF_SZ;
511
512         ret = iavf_execute_vf_cmd(adapter, &args);
513         if (ret) {
514                 PMD_DRV_LOG(ERR,
515                             "Failed to execute command of OP_GET_SUPPORTED_RXDIDS");
516                 return ret;
517         }
518
519         vf->supported_rxdid =
520                 ((struct virtchnl_supported_rxdids *)args.out_buffer)->supported_rxdids;
521
522         return 0;
523 }
524
525 int
526 iavf_enable_queues(struct iavf_adapter *adapter)
527 {
528         struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
529         struct virtchnl_queue_select queue_select;
530         struct iavf_cmd_info args;
531         int err;
532
533         memset(&queue_select, 0, sizeof(queue_select));
534         queue_select.vsi_id = vf->vsi_res->vsi_id;
535
536         queue_select.rx_queues = BIT(adapter->eth_dev->data->nb_rx_queues) - 1;
537         queue_select.tx_queues = BIT(adapter->eth_dev->data->nb_tx_queues) - 1;
538
539         args.ops = VIRTCHNL_OP_ENABLE_QUEUES;
540         args.in_args = (u8 *)&queue_select;
541         args.in_args_size = sizeof(queue_select);
542         args.out_buffer = vf->aq_resp;
543         args.out_size = IAVF_AQ_BUF_SZ;
544         err = iavf_execute_vf_cmd(adapter, &args);
545         if (err) {
546                 PMD_DRV_LOG(ERR,
547                             "Failed to execute command of OP_ENABLE_QUEUES");
548                 return err;
549         }
550         return 0;
551 }
552
553 int
554 iavf_disable_queues(struct iavf_adapter *adapter)
555 {
556         struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
557         struct virtchnl_queue_select queue_select;
558         struct iavf_cmd_info args;
559         int err;
560
561         memset(&queue_select, 0, sizeof(queue_select));
562         queue_select.vsi_id = vf->vsi_res->vsi_id;
563
564         queue_select.rx_queues = BIT(adapter->eth_dev->data->nb_rx_queues) - 1;
565         queue_select.tx_queues = BIT(adapter->eth_dev->data->nb_tx_queues) - 1;
566
567         args.ops = VIRTCHNL_OP_DISABLE_QUEUES;
568         args.in_args = (u8 *)&queue_select;
569         args.in_args_size = sizeof(queue_select);
570         args.out_buffer = vf->aq_resp;
571         args.out_size = IAVF_AQ_BUF_SZ;
572         err = iavf_execute_vf_cmd(adapter, &args);
573         if (err) {
574                 PMD_DRV_LOG(ERR,
575                             "Failed to execute command of OP_DISABLE_QUEUES");
576                 return err;
577         }
578         return 0;
579 }
580
581 int
582 iavf_switch_queue(struct iavf_adapter *adapter, uint16_t qid,
583                  bool rx, bool on)
584 {
585         struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
586         struct virtchnl_queue_select queue_select;
587         struct iavf_cmd_info args;
588         int err;
589
590         memset(&queue_select, 0, sizeof(queue_select));
591         queue_select.vsi_id = vf->vsi_res->vsi_id;
592         if (rx)
593                 queue_select.rx_queues |= 1 << qid;
594         else
595                 queue_select.tx_queues |= 1 << qid;
596
597         if (on)
598                 args.ops = VIRTCHNL_OP_ENABLE_QUEUES;
599         else
600                 args.ops = VIRTCHNL_OP_DISABLE_QUEUES;
601         args.in_args = (u8 *)&queue_select;
602         args.in_args_size = sizeof(queue_select);
603         args.out_buffer = vf->aq_resp;
604         args.out_size = IAVF_AQ_BUF_SZ;
605         err = iavf_execute_vf_cmd(adapter, &args);
606         if (err)
607                 PMD_DRV_LOG(ERR, "Failed to execute command of %s",
608                             on ? "OP_ENABLE_QUEUES" : "OP_DISABLE_QUEUES");
609         return err;
610 }
611
612 int
613 iavf_enable_queues_lv(struct iavf_adapter *adapter)
614 {
615         struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
616         struct virtchnl_del_ena_dis_queues *queue_select;
617         struct virtchnl_queue_chunk *queue_chunk;
618         struct iavf_cmd_info args;
619         int err, len;
620
621         len = sizeof(struct virtchnl_del_ena_dis_queues) +
622                   sizeof(struct virtchnl_queue_chunk) *
623                   (IAVF_RXTX_QUEUE_CHUNKS_NUM - 1);
624         queue_select = rte_zmalloc("queue_select", len, 0);
625         if (!queue_select)
626                 return -ENOMEM;
627
628         queue_chunk = queue_select->chunks.chunks;
629         queue_select->chunks.num_chunks = IAVF_RXTX_QUEUE_CHUNKS_NUM;
630         queue_select->vport_id = vf->vsi_res->vsi_id;
631
632         queue_chunk[VIRTCHNL_QUEUE_TYPE_TX].type = VIRTCHNL_QUEUE_TYPE_TX;
633         queue_chunk[VIRTCHNL_QUEUE_TYPE_TX].start_queue_id = 0;
634         queue_chunk[VIRTCHNL_QUEUE_TYPE_TX].num_queues =
635                 adapter->eth_dev->data->nb_tx_queues;
636
637         queue_chunk[VIRTCHNL_QUEUE_TYPE_RX].type = VIRTCHNL_QUEUE_TYPE_RX;
638         queue_chunk[VIRTCHNL_QUEUE_TYPE_RX].start_queue_id = 0;
639         queue_chunk[VIRTCHNL_QUEUE_TYPE_RX].num_queues =
640                 adapter->eth_dev->data->nb_rx_queues;
641
642         args.ops = VIRTCHNL_OP_ENABLE_QUEUES_V2;
643         args.in_args = (u8 *)queue_select;
644         args.in_args_size = len;
645         args.out_buffer = vf->aq_resp;
646         args.out_size = IAVF_AQ_BUF_SZ;
647         err = iavf_execute_vf_cmd(adapter, &args);
648         if (err)
649                 PMD_DRV_LOG(ERR,
650                             "Failed to execute command of OP_ENABLE_QUEUES_V2");
651
652         rte_free(queue_select);
653         return err;
654 }
655
656 int
657 iavf_disable_queues_lv(struct iavf_adapter *adapter)
658 {
659         struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
660         struct virtchnl_del_ena_dis_queues *queue_select;
661         struct virtchnl_queue_chunk *queue_chunk;
662         struct iavf_cmd_info args;
663         int err, len;
664
665         len = sizeof(struct virtchnl_del_ena_dis_queues) +
666                   sizeof(struct virtchnl_queue_chunk) *
667                   (IAVF_RXTX_QUEUE_CHUNKS_NUM - 1);
668         queue_select = rte_zmalloc("queue_select", len, 0);
669         if (!queue_select)
670                 return -ENOMEM;
671
672         queue_chunk = queue_select->chunks.chunks;
673         queue_select->chunks.num_chunks = IAVF_RXTX_QUEUE_CHUNKS_NUM;
674         queue_select->vport_id = vf->vsi_res->vsi_id;
675
676         queue_chunk[VIRTCHNL_QUEUE_TYPE_TX].type = VIRTCHNL_QUEUE_TYPE_TX;
677         queue_chunk[VIRTCHNL_QUEUE_TYPE_TX].start_queue_id = 0;
678         queue_chunk[VIRTCHNL_QUEUE_TYPE_TX].num_queues =
679                 adapter->eth_dev->data->nb_tx_queues;
680
681         queue_chunk[VIRTCHNL_QUEUE_TYPE_RX].type = VIRTCHNL_QUEUE_TYPE_RX;
682         queue_chunk[VIRTCHNL_QUEUE_TYPE_RX].start_queue_id = 0;
683         queue_chunk[VIRTCHNL_QUEUE_TYPE_RX].num_queues =
684                 adapter->eth_dev->data->nb_rx_queues;
685
686         args.ops = VIRTCHNL_OP_DISABLE_QUEUES_V2;
687         args.in_args = (u8 *)queue_select;
688         args.in_args_size = len;
689         args.out_buffer = vf->aq_resp;
690         args.out_size = IAVF_AQ_BUF_SZ;
691         err = iavf_execute_vf_cmd(adapter, &args);
692         if (err)
693                 PMD_DRV_LOG(ERR,
694                             "Failed to execute command of OP_DISABLE_QUEUES_V2");
695
696         rte_free(queue_select);
697         return err;
698 }
699
700 int
701 iavf_switch_queue_lv(struct iavf_adapter *adapter, uint16_t qid,
702                  bool rx, bool on)
703 {
704         struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
705         struct virtchnl_del_ena_dis_queues *queue_select;
706         struct virtchnl_queue_chunk *queue_chunk;
707         struct iavf_cmd_info args;
708         int err, len;
709
710         len = sizeof(struct virtchnl_del_ena_dis_queues);
711         queue_select = rte_zmalloc("queue_select", len, 0);
712         if (!queue_select)
713                 return -ENOMEM;
714
715         queue_chunk = queue_select->chunks.chunks;
716         queue_select->chunks.num_chunks = 1;
717         queue_select->vport_id = vf->vsi_res->vsi_id;
718
719         if (rx) {
720                 queue_chunk->type = VIRTCHNL_QUEUE_TYPE_RX;
721                 queue_chunk->start_queue_id = qid;
722                 queue_chunk->num_queues = 1;
723         } else {
724                 queue_chunk->type = VIRTCHNL_QUEUE_TYPE_TX;
725                 queue_chunk->start_queue_id = qid;
726                 queue_chunk->num_queues = 1;
727         }
728
729         if (on)
730                 args.ops = VIRTCHNL_OP_ENABLE_QUEUES_V2;
731         else
732                 args.ops = VIRTCHNL_OP_DISABLE_QUEUES_V2;
733         args.in_args = (u8 *)queue_select;
734         args.in_args_size = len;
735         args.out_buffer = vf->aq_resp;
736         args.out_size = IAVF_AQ_BUF_SZ;
737         err = iavf_execute_vf_cmd(adapter, &args);
738         if (err)
739                 PMD_DRV_LOG(ERR, "Failed to execute command of %s",
740                             on ? "OP_ENABLE_QUEUES_V2" : "OP_DISABLE_QUEUES_V2");
741
742         rte_free(queue_select);
743         return err;
744 }
745
746 int
747 iavf_configure_rss_lut(struct iavf_adapter *adapter)
748 {
749         struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
750         struct virtchnl_rss_lut *rss_lut;
751         struct iavf_cmd_info args;
752         int len, err = 0;
753
754         len = sizeof(*rss_lut) + vf->vf_res->rss_lut_size - 1;
755         rss_lut = rte_zmalloc("rss_lut", len, 0);
756         if (!rss_lut)
757                 return -ENOMEM;
758
759         rss_lut->vsi_id = vf->vsi_res->vsi_id;
760         rss_lut->lut_entries = vf->vf_res->rss_lut_size;
761         rte_memcpy(rss_lut->lut, vf->rss_lut, vf->vf_res->rss_lut_size);
762
763         args.ops = VIRTCHNL_OP_CONFIG_RSS_LUT;
764         args.in_args = (u8 *)rss_lut;
765         args.in_args_size = len;
766         args.out_buffer = vf->aq_resp;
767         args.out_size = IAVF_AQ_BUF_SZ;
768
769         err = iavf_execute_vf_cmd(adapter, &args);
770         if (err)
771                 PMD_DRV_LOG(ERR,
772                             "Failed to execute command of OP_CONFIG_RSS_LUT");
773
774         rte_free(rss_lut);
775         return err;
776 }
777
778 int
779 iavf_configure_rss_key(struct iavf_adapter *adapter)
780 {
781         struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
782         struct virtchnl_rss_key *rss_key;
783         struct iavf_cmd_info args;
784         int len, err = 0;
785
786         len = sizeof(*rss_key) + vf->vf_res->rss_key_size - 1;
787         rss_key = rte_zmalloc("rss_key", len, 0);
788         if (!rss_key)
789                 return -ENOMEM;
790
791         rss_key->vsi_id = vf->vsi_res->vsi_id;
792         rss_key->key_len = vf->vf_res->rss_key_size;
793         rte_memcpy(rss_key->key, vf->rss_key, vf->vf_res->rss_key_size);
794
795         args.ops = VIRTCHNL_OP_CONFIG_RSS_KEY;
796         args.in_args = (u8 *)rss_key;
797         args.in_args_size = len;
798         args.out_buffer = vf->aq_resp;
799         args.out_size = IAVF_AQ_BUF_SZ;
800
801         err = iavf_execute_vf_cmd(adapter, &args);
802         if (err)
803                 PMD_DRV_LOG(ERR,
804                             "Failed to execute command of OP_CONFIG_RSS_KEY");
805
806         rte_free(rss_key);
807         return err;
808 }
809
810 int
811 iavf_configure_queues(struct iavf_adapter *adapter,
812                 uint16_t num_queue_pairs, uint16_t index)
813 {
814         struct iavf_rx_queue **rxq =
815                 (struct iavf_rx_queue **)adapter->eth_dev->data->rx_queues;
816         struct iavf_tx_queue **txq =
817                 (struct iavf_tx_queue **)adapter->eth_dev->data->tx_queues;
818         struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
819         struct virtchnl_vsi_queue_config_info *vc_config;
820         struct virtchnl_queue_pair_info *vc_qp;
821         struct iavf_cmd_info args;
822         uint16_t i, size;
823         int err;
824
825         size = sizeof(*vc_config) +
826                sizeof(vc_config->qpair[0]) * num_queue_pairs;
827         vc_config = rte_zmalloc("cfg_queue", size, 0);
828         if (!vc_config)
829                 return -ENOMEM;
830
831         vc_config->vsi_id = vf->vsi_res->vsi_id;
832         vc_config->num_queue_pairs = num_queue_pairs;
833
834         for (i = index, vc_qp = vc_config->qpair;
835                  i < index + num_queue_pairs;
836              i++, vc_qp++) {
837                 vc_qp->txq.vsi_id = vf->vsi_res->vsi_id;
838                 vc_qp->txq.queue_id = i;
839
840                 /* Virtchnnl configure tx queues by pairs */
841                 if (i < adapter->eth_dev->data->nb_tx_queues) {
842                         vc_qp->txq.ring_len = txq[i]->nb_tx_desc;
843                         vc_qp->txq.dma_ring_addr = txq[i]->tx_ring_phys_addr;
844                 }
845
846                 vc_qp->rxq.vsi_id = vf->vsi_res->vsi_id;
847                 vc_qp->rxq.queue_id = i;
848                 vc_qp->rxq.max_pkt_size = vf->max_pkt_len;
849
850                 if (i >= adapter->eth_dev->data->nb_rx_queues)
851                         continue;
852
853                 /* Virtchnnl configure rx queues by pairs */
854                 vc_qp->rxq.ring_len = rxq[i]->nb_rx_desc;
855                 vc_qp->rxq.dma_ring_addr = rxq[i]->rx_ring_phys_addr;
856                 vc_qp->rxq.databuffer_size = rxq[i]->rx_buf_len;
857                 vc_qp->rxq.crc_disable = rxq[i]->crc_len != 0 ? 1 : 0;
858 #ifndef RTE_LIBRTE_IAVF_16BYTE_RX_DESC
859                 if (vf->vf_res->vf_cap_flags &
860                     VIRTCHNL_VF_OFFLOAD_RX_FLEX_DESC &&
861                     vf->supported_rxdid & BIT(rxq[i]->rxdid)) {
862                         vc_qp->rxq.rxdid = rxq[i]->rxdid;
863                         PMD_DRV_LOG(NOTICE, "request RXDID[%d] in Queue[%d]",
864                                     vc_qp->rxq.rxdid, i);
865                 } else {
866                         PMD_DRV_LOG(NOTICE, "RXDID[%d] is not supported, "
867                                     "request default RXDID[%d] in Queue[%d]",
868                                     rxq[i]->rxdid, IAVF_RXDID_LEGACY_1, i);
869                         vc_qp->rxq.rxdid = IAVF_RXDID_LEGACY_1;
870                 }
871 #else
872                 if (vf->vf_res->vf_cap_flags &
873                         VIRTCHNL_VF_OFFLOAD_RX_FLEX_DESC &&
874                         vf->supported_rxdid & BIT(IAVF_RXDID_LEGACY_0)) {
875                         vc_qp->rxq.rxdid = IAVF_RXDID_LEGACY_0;
876                         PMD_DRV_LOG(NOTICE, "request RXDID[%d] in Queue[%d]",
877                                     vc_qp->rxq.rxdid, i);
878                 } else {
879                         PMD_DRV_LOG(ERR, "RXDID[%d] is not supported",
880                                     IAVF_RXDID_LEGACY_0);
881                         return -1;
882                 }
883 #endif
884         }
885
886         memset(&args, 0, sizeof(args));
887         args.ops = VIRTCHNL_OP_CONFIG_VSI_QUEUES;
888         args.in_args = (uint8_t *)vc_config;
889         args.in_args_size = size;
890         args.out_buffer = vf->aq_resp;
891         args.out_size = IAVF_AQ_BUF_SZ;
892
893         err = iavf_execute_vf_cmd(adapter, &args);
894         if (err)
895                 PMD_DRV_LOG(ERR, "Failed to execute command of"
896                             " VIRTCHNL_OP_CONFIG_VSI_QUEUES");
897
898         rte_free(vc_config);
899         return err;
900 }
901
902 int
903 iavf_config_irq_map(struct iavf_adapter *adapter)
904 {
905         struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
906         struct virtchnl_irq_map_info *map_info;
907         struct virtchnl_vector_map *vecmap;
908         struct iavf_cmd_info args;
909         int len, i, err;
910
911         len = sizeof(struct virtchnl_irq_map_info) +
912               sizeof(struct virtchnl_vector_map) * vf->nb_msix;
913
914         map_info = rte_zmalloc("map_info", len, 0);
915         if (!map_info)
916                 return -ENOMEM;
917
918         map_info->num_vectors = vf->nb_msix;
919         for (i = 0; i < adapter->eth_dev->data->nb_rx_queues; i++) {
920                 vecmap =
921                     &map_info->vecmap[vf->qv_map[i].vector_id - vf->msix_base];
922                 vecmap->vsi_id = vf->vsi_res->vsi_id;
923                 vecmap->rxitr_idx = IAVF_ITR_INDEX_DEFAULT;
924                 vecmap->vector_id = vf->qv_map[i].vector_id;
925                 vecmap->txq_map = 0;
926                 vecmap->rxq_map |= 1 << vf->qv_map[i].queue_id;
927         }
928
929         args.ops = VIRTCHNL_OP_CONFIG_IRQ_MAP;
930         args.in_args = (u8 *)map_info;
931         args.in_args_size = len;
932         args.out_buffer = vf->aq_resp;
933         args.out_size = IAVF_AQ_BUF_SZ;
934         err = iavf_execute_vf_cmd(adapter, &args);
935         if (err)
936                 PMD_DRV_LOG(ERR, "fail to execute command OP_CONFIG_IRQ_MAP");
937
938         rte_free(map_info);
939         return err;
940 }
941
942 int
943 iavf_config_irq_map_lv(struct iavf_adapter *adapter, uint16_t num,
944                 uint16_t index)
945 {
946         struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
947         struct virtchnl_queue_vector_maps *map_info;
948         struct virtchnl_queue_vector *qv_maps;
949         struct iavf_cmd_info args;
950         int len, i, err;
951         int count = 0;
952
953         len = sizeof(struct virtchnl_queue_vector_maps) +
954               sizeof(struct virtchnl_queue_vector) * (num - 1);
955
956         map_info = rte_zmalloc("map_info", len, 0);
957         if (!map_info)
958                 return -ENOMEM;
959
960         map_info->vport_id = vf->vsi_res->vsi_id;
961         map_info->num_qv_maps = num;
962         for (i = index; i < index + map_info->num_qv_maps; i++) {
963                 qv_maps = &map_info->qv_maps[count++];
964                 qv_maps->itr_idx = VIRTCHNL_ITR_IDX_0;
965                 qv_maps->queue_type = VIRTCHNL_QUEUE_TYPE_RX;
966                 qv_maps->queue_id = vf->qv_map[i].queue_id;
967                 qv_maps->vector_id = vf->qv_map[i].vector_id;
968         }
969
970         args.ops = VIRTCHNL_OP_MAP_QUEUE_VECTOR;
971         args.in_args = (u8 *)map_info;
972         args.in_args_size = len;
973         args.out_buffer = vf->aq_resp;
974         args.out_size = IAVF_AQ_BUF_SZ;
975         err = iavf_execute_vf_cmd(adapter, &args);
976         if (err)
977                 PMD_DRV_LOG(ERR, "fail to execute command OP_MAP_QUEUE_VECTOR");
978
979         rte_free(map_info);
980         return err;
981 }
982
983 void
984 iavf_add_del_all_mac_addr(struct iavf_adapter *adapter, bool add)
985 {
986         struct virtchnl_ether_addr_list *list;
987         struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
988         struct rte_ether_addr *addr;
989         struct iavf_cmd_info args;
990         int len, err, i, j;
991         int next_begin = 0;
992         int begin = 0;
993
994         do {
995                 j = 0;
996                 len = sizeof(struct virtchnl_ether_addr_list);
997                 for (i = begin; i < IAVF_NUM_MACADDR_MAX; i++, next_begin++) {
998                         addr = &adapter->eth_dev->data->mac_addrs[i];
999                         if (rte_is_zero_ether_addr(addr))
1000                                 continue;
1001                         len += sizeof(struct virtchnl_ether_addr);
1002                         if (len >= IAVF_AQ_BUF_SZ) {
1003                                 next_begin = i + 1;
1004                                 break;
1005                         }
1006                 }
1007
1008                 list = rte_zmalloc("iavf_del_mac_buffer", len, 0);
1009                 if (!list) {
1010                         PMD_DRV_LOG(ERR, "fail to allocate memory");
1011                         return;
1012                 }
1013
1014                 for (i = begin; i < next_begin; i++) {
1015                         addr = &adapter->eth_dev->data->mac_addrs[i];
1016                         if (rte_is_zero_ether_addr(addr))
1017                                 continue;
1018                         rte_memcpy(list->list[j].addr, addr->addr_bytes,
1019                                    sizeof(addr->addr_bytes));
1020                         PMD_DRV_LOG(DEBUG, "add/rm mac:%x:%x:%x:%x:%x:%x",
1021                                     addr->addr_bytes[0], addr->addr_bytes[1],
1022                                     addr->addr_bytes[2], addr->addr_bytes[3],
1023                                     addr->addr_bytes[4], addr->addr_bytes[5]);
1024                         j++;
1025                 }
1026                 list->vsi_id = vf->vsi_res->vsi_id;
1027                 list->num_elements = j;
1028                 args.ops = add ? VIRTCHNL_OP_ADD_ETH_ADDR :
1029                            VIRTCHNL_OP_DEL_ETH_ADDR;
1030                 args.in_args = (uint8_t *)list;
1031                 args.in_args_size = len;
1032                 args.out_buffer = vf->aq_resp;
1033                 args.out_size = IAVF_AQ_BUF_SZ;
1034                 err = iavf_execute_vf_cmd(adapter, &args);
1035                 if (err)
1036                         PMD_DRV_LOG(ERR, "fail to execute command %s",
1037                                     add ? "OP_ADD_ETHER_ADDRESS" :
1038                                     "OP_DEL_ETHER_ADDRESS");
1039                 rte_free(list);
1040                 begin = next_begin;
1041         } while (begin < IAVF_NUM_MACADDR_MAX);
1042 }
1043
1044 int
1045 iavf_query_stats(struct iavf_adapter *adapter,
1046                 struct virtchnl_eth_stats **pstats)
1047 {
1048         struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
1049         struct virtchnl_queue_select q_stats;
1050         struct iavf_cmd_info args;
1051         int err;
1052
1053         memset(&q_stats, 0, sizeof(q_stats));
1054         q_stats.vsi_id = vf->vsi_res->vsi_id;
1055         args.ops = VIRTCHNL_OP_GET_STATS;
1056         args.in_args = (uint8_t *)&q_stats;
1057         args.in_args_size = sizeof(q_stats);
1058         args.out_buffer = vf->aq_resp;
1059         args.out_size = IAVF_AQ_BUF_SZ;
1060
1061         err = iavf_execute_vf_cmd(adapter, &args);
1062         if (err) {
1063                 PMD_DRV_LOG(ERR, "fail to execute command OP_GET_STATS");
1064                 *pstats = NULL;
1065                 return err;
1066         }
1067         *pstats = (struct virtchnl_eth_stats *)args.out_buffer;
1068         return 0;
1069 }
1070
1071 int
1072 iavf_config_promisc(struct iavf_adapter *adapter,
1073                    bool enable_unicast,
1074                    bool enable_multicast)
1075 {
1076         struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
1077         struct virtchnl_promisc_info promisc;
1078         struct iavf_cmd_info args;
1079         int err;
1080
1081         promisc.flags = 0;
1082         promisc.vsi_id = vf->vsi_res->vsi_id;
1083
1084         if (enable_unicast)
1085                 promisc.flags |= FLAG_VF_UNICAST_PROMISC;
1086
1087         if (enable_multicast)
1088                 promisc.flags |= FLAG_VF_MULTICAST_PROMISC;
1089
1090         args.ops = VIRTCHNL_OP_CONFIG_PROMISCUOUS_MODE;
1091         args.in_args = (uint8_t *)&promisc;
1092         args.in_args_size = sizeof(promisc);
1093         args.out_buffer = vf->aq_resp;
1094         args.out_size = IAVF_AQ_BUF_SZ;
1095
1096         err = iavf_execute_vf_cmd(adapter, &args);
1097
1098         if (err) {
1099                 PMD_DRV_LOG(ERR,
1100                             "fail to execute command CONFIG_PROMISCUOUS_MODE");
1101
1102                 if (err == IAVF_NOT_SUPPORTED)
1103                         return -ENOTSUP;
1104
1105                 return -EAGAIN;
1106         }
1107
1108         vf->promisc_unicast_enabled = enable_unicast;
1109         vf->promisc_multicast_enabled = enable_multicast;
1110         return 0;
1111 }
1112
1113 int
1114 iavf_add_del_eth_addr(struct iavf_adapter *adapter, struct rte_ether_addr *addr,
1115                      bool add)
1116 {
1117         struct virtchnl_ether_addr_list *list;
1118         struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
1119         uint8_t cmd_buffer[sizeof(struct virtchnl_ether_addr_list) +
1120                            sizeof(struct virtchnl_ether_addr)];
1121         struct iavf_cmd_info args;
1122         int err;
1123
1124         list = (struct virtchnl_ether_addr_list *)cmd_buffer;
1125         list->vsi_id = vf->vsi_res->vsi_id;
1126         list->num_elements = 1;
1127         rte_memcpy(list->list[0].addr, addr->addr_bytes,
1128                    sizeof(addr->addr_bytes));
1129
1130         args.ops = add ? VIRTCHNL_OP_ADD_ETH_ADDR : VIRTCHNL_OP_DEL_ETH_ADDR;
1131         args.in_args = cmd_buffer;
1132         args.in_args_size = sizeof(cmd_buffer);
1133         args.out_buffer = vf->aq_resp;
1134         args.out_size = IAVF_AQ_BUF_SZ;
1135         err = iavf_execute_vf_cmd(adapter, &args);
1136         if (err)
1137                 PMD_DRV_LOG(ERR, "fail to execute command %s",
1138                             add ? "OP_ADD_ETH_ADDR" :  "OP_DEL_ETH_ADDR");
1139         return err;
1140 }
1141
1142 int
1143 iavf_add_del_vlan(struct iavf_adapter *adapter, uint16_t vlanid, bool add)
1144 {
1145         struct virtchnl_vlan_filter_list *vlan_list;
1146         struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
1147         uint8_t cmd_buffer[sizeof(struct virtchnl_vlan_filter_list) +
1148                                                         sizeof(uint16_t)];
1149         struct iavf_cmd_info args;
1150         int err;
1151
1152         vlan_list = (struct virtchnl_vlan_filter_list *)cmd_buffer;
1153         vlan_list->vsi_id = vf->vsi_res->vsi_id;
1154         vlan_list->num_elements = 1;
1155         vlan_list->vlan_id[0] = vlanid;
1156
1157         args.ops = add ? VIRTCHNL_OP_ADD_VLAN : VIRTCHNL_OP_DEL_VLAN;
1158         args.in_args = cmd_buffer;
1159         args.in_args_size = sizeof(cmd_buffer);
1160         args.out_buffer = vf->aq_resp;
1161         args.out_size = IAVF_AQ_BUF_SZ;
1162         err = iavf_execute_vf_cmd(adapter, &args);
1163         if (err)
1164                 PMD_DRV_LOG(ERR, "fail to execute command %s",
1165                             add ? "OP_ADD_VLAN" :  "OP_DEL_VLAN");
1166
1167         return err;
1168 }
1169
1170 int
1171 iavf_fdir_add(struct iavf_adapter *adapter,
1172         struct iavf_fdir_conf *filter)
1173 {
1174         struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
1175         struct virtchnl_fdir_add *fdir_ret;
1176
1177         struct iavf_cmd_info args;
1178         int err;
1179
1180         filter->add_fltr.vsi_id = vf->vsi_res->vsi_id;
1181         filter->add_fltr.validate_only = 0;
1182
1183         args.ops = VIRTCHNL_OP_ADD_FDIR_FILTER;
1184         args.in_args = (uint8_t *)(&filter->add_fltr);
1185         args.in_args_size = sizeof(*(&filter->add_fltr));
1186         args.out_buffer = vf->aq_resp;
1187         args.out_size = IAVF_AQ_BUF_SZ;
1188
1189         err = iavf_execute_vf_cmd(adapter, &args);
1190         if (err) {
1191                 PMD_DRV_LOG(ERR, "fail to execute command OP_ADD_FDIR_FILTER");
1192                 return err;
1193         }
1194
1195         fdir_ret = (struct virtchnl_fdir_add *)args.out_buffer;
1196         filter->flow_id = fdir_ret->flow_id;
1197
1198         if (fdir_ret->status == VIRTCHNL_FDIR_SUCCESS) {
1199                 PMD_DRV_LOG(INFO,
1200                         "Succeed in adding rule request by PF");
1201         } else if (fdir_ret->status == VIRTCHNL_FDIR_FAILURE_RULE_NORESOURCE) {
1202                 PMD_DRV_LOG(ERR,
1203                         "Failed to add rule request due to no hw resource");
1204                 return -1;
1205         } else if (fdir_ret->status == VIRTCHNL_FDIR_FAILURE_RULE_EXIST) {
1206                 PMD_DRV_LOG(ERR,
1207                         "Failed to add rule request due to the rule is already existed");
1208                 return -1;
1209         } else if (fdir_ret->status == VIRTCHNL_FDIR_FAILURE_RULE_CONFLICT) {
1210                 PMD_DRV_LOG(ERR,
1211                         "Failed to add rule request due to the rule is conflict with existing rule");
1212                 return -1;
1213         } else if (fdir_ret->status == VIRTCHNL_FDIR_FAILURE_RULE_INVALID) {
1214                 PMD_DRV_LOG(ERR,
1215                         "Failed to add rule request due to the hw doesn't support");
1216                 return -1;
1217         } else if (fdir_ret->status == VIRTCHNL_FDIR_FAILURE_RULE_TIMEOUT) {
1218                 PMD_DRV_LOG(ERR,
1219                         "Failed to add rule request due to time out for programming");
1220                 return -1;
1221         } else {
1222                 PMD_DRV_LOG(ERR,
1223                         "Failed to add rule request due to other reasons");
1224                 return -1;
1225         }
1226
1227         return 0;
1228 };
1229
1230 int
1231 iavf_fdir_del(struct iavf_adapter *adapter,
1232         struct iavf_fdir_conf *filter)
1233 {
1234         struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
1235         struct virtchnl_fdir_del *fdir_ret;
1236
1237         struct iavf_cmd_info args;
1238         int err;
1239
1240         filter->del_fltr.vsi_id = vf->vsi_res->vsi_id;
1241         filter->del_fltr.flow_id = filter->flow_id;
1242
1243         args.ops = VIRTCHNL_OP_DEL_FDIR_FILTER;
1244         args.in_args = (uint8_t *)(&filter->del_fltr);
1245         args.in_args_size = sizeof(filter->del_fltr);
1246         args.out_buffer = vf->aq_resp;
1247         args.out_size = IAVF_AQ_BUF_SZ;
1248
1249         err = iavf_execute_vf_cmd(adapter, &args);
1250         if (err) {
1251                 PMD_DRV_LOG(ERR, "fail to execute command OP_DEL_FDIR_FILTER");
1252                 return err;
1253         }
1254
1255         fdir_ret = (struct virtchnl_fdir_del *)args.out_buffer;
1256
1257         if (fdir_ret->status == VIRTCHNL_FDIR_SUCCESS) {
1258                 PMD_DRV_LOG(INFO,
1259                         "Succeed in deleting rule request by PF");
1260         } else if (fdir_ret->status == VIRTCHNL_FDIR_FAILURE_RULE_NONEXIST) {
1261                 PMD_DRV_LOG(ERR,
1262                         "Failed to delete rule request due to this rule doesn't exist");
1263                 return -1;
1264         } else if (fdir_ret->status == VIRTCHNL_FDIR_FAILURE_RULE_TIMEOUT) {
1265                 PMD_DRV_LOG(ERR,
1266                         "Failed to delete rule request due to time out for programming");
1267                 return -1;
1268         } else {
1269                 PMD_DRV_LOG(ERR,
1270                         "Failed to delete rule request due to other reasons");
1271                 return -1;
1272         }
1273
1274         return 0;
1275 };
1276
1277 int
1278 iavf_fdir_check(struct iavf_adapter *adapter,
1279                 struct iavf_fdir_conf *filter)
1280 {
1281         struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
1282         struct virtchnl_fdir_add *fdir_ret;
1283
1284         struct iavf_cmd_info args;
1285         int err;
1286
1287         filter->add_fltr.vsi_id = vf->vsi_res->vsi_id;
1288         filter->add_fltr.validate_only = 1;
1289
1290         args.ops = VIRTCHNL_OP_ADD_FDIR_FILTER;
1291         args.in_args = (uint8_t *)(&filter->add_fltr);
1292         args.in_args_size = sizeof(*(&filter->add_fltr));
1293         args.out_buffer = vf->aq_resp;
1294         args.out_size = IAVF_AQ_BUF_SZ;
1295
1296         err = iavf_execute_vf_cmd(adapter, &args);
1297         if (err) {
1298                 PMD_DRV_LOG(ERR, "fail to check flow direcotor rule");
1299                 return err;
1300         }
1301
1302         fdir_ret = (struct virtchnl_fdir_add *)args.out_buffer;
1303
1304         if (fdir_ret->status == VIRTCHNL_FDIR_SUCCESS) {
1305                 PMD_DRV_LOG(INFO,
1306                         "Succeed in checking rule request by PF");
1307         } else if (fdir_ret->status == VIRTCHNL_FDIR_FAILURE_RULE_INVALID) {
1308                 PMD_DRV_LOG(ERR,
1309                         "Failed to check rule request due to parameters validation"
1310                         " or HW doesn't support");
1311                 return -1;
1312         } else {
1313                 PMD_DRV_LOG(ERR,
1314                         "Failed to check rule request due to other reasons");
1315                 return -1;
1316         }
1317
1318         return 0;
1319 }
1320
1321 int
1322 iavf_add_del_rss_cfg(struct iavf_adapter *adapter,
1323                      struct virtchnl_rss_cfg *rss_cfg, bool add)
1324 {
1325         struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
1326         struct iavf_cmd_info args;
1327         int err;
1328
1329         memset(&args, 0, sizeof(args));
1330         args.ops = add ? VIRTCHNL_OP_ADD_RSS_CFG :
1331                 VIRTCHNL_OP_DEL_RSS_CFG;
1332         args.in_args = (u8 *)rss_cfg;
1333         args.in_args_size = sizeof(*rss_cfg);
1334         args.out_buffer = vf->aq_resp;
1335         args.out_size = IAVF_AQ_BUF_SZ;
1336
1337         err = iavf_execute_vf_cmd(adapter, &args);
1338         if (err)
1339                 PMD_DRV_LOG(ERR,
1340                             "Failed to execute command of %s",
1341                             add ? "OP_ADD_RSS_CFG" :
1342                             "OP_DEL_RSS_INPUT_CFG");
1343
1344         return err;
1345 }
1346
1347 int
1348 iavf_set_hena(struct iavf_adapter *adapter, uint64_t hena)
1349 {
1350         struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
1351         struct virtchnl_rss_hena vrh;
1352         struct iavf_cmd_info args;
1353         int err;
1354
1355         vrh.hena = hena;
1356         args.ops = VIRTCHNL_OP_SET_RSS_HENA;
1357         args.in_args = (u8 *)&vrh;
1358         args.in_args_size = sizeof(vrh);
1359         args.out_buffer = vf->aq_resp;
1360         args.out_size = IAVF_AQ_BUF_SZ;
1361
1362         err = iavf_execute_vf_cmd(adapter, &args);
1363         if (err)
1364                 PMD_DRV_LOG(ERR,
1365                             "Failed to execute command of OP_SET_RSS_HENA");
1366
1367         return err;
1368 }
1369
1370 int
1371 iavf_add_del_mc_addr_list(struct iavf_adapter *adapter,
1372                         struct rte_ether_addr *mc_addrs,
1373                         uint32_t mc_addrs_num, bool add)
1374 {
1375         struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
1376         uint8_t cmd_buffer[sizeof(struct virtchnl_ether_addr_list) +
1377                 (IAVF_NUM_MACADDR_MAX * sizeof(struct virtchnl_ether_addr))];
1378         struct virtchnl_ether_addr_list *list;
1379         struct iavf_cmd_info args;
1380         uint32_t i;
1381         int err;
1382
1383         if (mc_addrs == NULL || mc_addrs_num == 0)
1384                 return 0;
1385
1386         list = (struct virtchnl_ether_addr_list *)cmd_buffer;
1387         list->vsi_id = vf->vsi_res->vsi_id;
1388         list->num_elements = mc_addrs_num;
1389
1390         for (i = 0; i < mc_addrs_num; i++) {
1391                 if (!IAVF_IS_MULTICAST(mc_addrs[i].addr_bytes)) {
1392                         PMD_DRV_LOG(ERR, "Invalid mac:%x:%x:%x:%x:%x:%x",
1393                                     mc_addrs[i].addr_bytes[0],
1394                                     mc_addrs[i].addr_bytes[1],
1395                                     mc_addrs[i].addr_bytes[2],
1396                                     mc_addrs[i].addr_bytes[3],
1397                                     mc_addrs[i].addr_bytes[4],
1398                                     mc_addrs[i].addr_bytes[5]);
1399                         return -EINVAL;
1400                 }
1401
1402                 memcpy(list->list[i].addr, mc_addrs[i].addr_bytes,
1403                         sizeof(list->list[i].addr));
1404         }
1405
1406         args.ops = add ? VIRTCHNL_OP_ADD_ETH_ADDR : VIRTCHNL_OP_DEL_ETH_ADDR;
1407         args.in_args = cmd_buffer;
1408         args.in_args_size = sizeof(struct virtchnl_ether_addr_list) +
1409                 i * sizeof(struct virtchnl_ether_addr);
1410         args.out_buffer = vf->aq_resp;
1411         args.out_size = IAVF_AQ_BUF_SZ;
1412         err = iavf_execute_vf_cmd(adapter, &args);
1413
1414         if (err) {
1415                 PMD_DRV_LOG(ERR, "fail to execute command %s",
1416                         add ? "OP_ADD_ETH_ADDR" : "OP_DEL_ETH_ADDR");
1417                 return err;
1418         }
1419
1420         return 0;
1421 }
1422
1423 int
1424 iavf_request_queues(struct iavf_adapter *adapter, uint16_t num)
1425 {
1426         struct rte_eth_dev *dev = adapter->eth_dev;
1427         struct iavf_info *vf =  IAVF_DEV_PRIVATE_TO_VF(adapter);
1428         struct rte_pci_device *pci_dev = RTE_ETH_DEV_TO_PCI(dev);
1429         struct virtchnl_vf_res_request vfres;
1430         struct iavf_cmd_info args;
1431         uint16_t num_queue_pairs;
1432         int err;
1433
1434         if (!(vf->vf_res->vf_cap_flags &
1435                 VIRTCHNL_VF_OFFLOAD_REQ_QUEUES)) {
1436                 PMD_DRV_LOG(ERR, "request queues not supported");
1437                 return -1;
1438         }
1439
1440         if (num == 0) {
1441                 PMD_DRV_LOG(ERR, "queue number cannot be zero");
1442                 return -1;
1443         }
1444         vfres.num_queue_pairs = num;
1445
1446         args.ops = VIRTCHNL_OP_REQUEST_QUEUES;
1447         args.in_args = (u8 *)&vfres;
1448         args.in_args_size = sizeof(vfres);
1449         args.out_buffer = vf->aq_resp;
1450         args.out_size = IAVF_AQ_BUF_SZ;
1451
1452         /*
1453          * disable interrupt to avoid the admin queue message to be read
1454          * before iavf_read_msg_from_pf.
1455          */
1456         rte_intr_disable(&pci_dev->intr_handle);
1457         err = iavf_execute_vf_cmd(adapter, &args);
1458         rte_intr_enable(&pci_dev->intr_handle);
1459         if (err) {
1460                 PMD_DRV_LOG(ERR, "fail to execute command OP_REQUEST_QUEUES");
1461                 return err;
1462         }
1463
1464         /* request queues succeeded, vf is resetting */
1465         if (vf->vf_reset) {
1466                 PMD_DRV_LOG(INFO, "vf is resetting");
1467                 return 0;
1468         }
1469
1470         /* request additional queues failed, return available number */
1471         num_queue_pairs =
1472           ((struct virtchnl_vf_res_request *)args.out_buffer)->num_queue_pairs;
1473         PMD_DRV_LOG(ERR, "request queues failed, only %u queues "
1474                 "available", num_queue_pairs);
1475
1476         return -1;
1477 }
1478
1479 int
1480 iavf_get_max_rss_queue_region(struct iavf_adapter *adapter)
1481 {
1482         struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
1483         struct iavf_cmd_info args;
1484         uint16_t qregion_width;
1485         int err;
1486
1487         args.ops = VIRTCHNL_OP_GET_MAX_RSS_QREGION;
1488         args.in_args = NULL;
1489         args.in_args_size = 0;
1490         args.out_buffer = vf->aq_resp;
1491         args.out_size = IAVF_AQ_BUF_SZ;
1492
1493         err = iavf_execute_vf_cmd(adapter, &args);
1494         if (err) {
1495                 PMD_DRV_LOG(ERR, "Failed to execute command of VIRTCHNL_OP_GET_MAX_RSS_QREGION");
1496                 return err;
1497         }
1498
1499         qregion_width =
1500         ((struct virtchnl_max_rss_qregion *)args.out_buffer)->qregion_width;
1501
1502         vf->max_rss_qregion = (uint16_t)(1 << qregion_width);
1503
1504         return 0;
1505 }