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