c17ae06227a499817aeb14f52a8b91b4a6d92081
[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_LARGE_NUM_QPAIRS;
462
463         args.in_args = (uint8_t *)&caps;
464         args.in_args_size = sizeof(caps);
465
466         err = iavf_execute_vf_cmd(adapter, &args);
467
468         if (err) {
469                 PMD_DRV_LOG(ERR,
470                             "Failed to execute command of OP_GET_VF_RESOURCE");
471                 return -1;
472         }
473
474         len =  sizeof(struct virtchnl_vf_resource) +
475                       IAVF_MAX_VF_VSI * sizeof(struct virtchnl_vsi_resource);
476
477         rte_memcpy(vf->vf_res, args.out_buffer,
478                    RTE_MIN(args.out_size, len));
479         /* parse  VF config message back from PF*/
480         iavf_vf_parse_hw_config(hw, vf->vf_res);
481         for (i = 0; i < vf->vf_res->num_vsis; i++) {
482                 if (vf->vf_res->vsi_res[i].vsi_type == VIRTCHNL_VSI_SRIOV)
483                         vf->vsi_res = &vf->vf_res->vsi_res[i];
484         }
485
486         if (!vf->vsi_res) {
487                 PMD_INIT_LOG(ERR, "no LAN VSI found");
488                 return -1;
489         }
490
491         vf->vsi.vsi_id = vf->vsi_res->vsi_id;
492         vf->vsi.nb_qps = vf->vsi_res->num_queue_pairs;
493         vf->vsi.adapter = adapter;
494
495         return 0;
496 }
497
498 int
499 iavf_get_supported_rxdid(struct iavf_adapter *adapter)
500 {
501         struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
502         struct iavf_cmd_info args;
503         int ret;
504
505         args.ops = VIRTCHNL_OP_GET_SUPPORTED_RXDIDS;
506         args.in_args = NULL;
507         args.in_args_size = 0;
508         args.out_buffer = vf->aq_resp;
509         args.out_size = IAVF_AQ_BUF_SZ;
510
511         ret = iavf_execute_vf_cmd(adapter, &args);
512         if (ret) {
513                 PMD_DRV_LOG(ERR,
514                             "Failed to execute command of OP_GET_SUPPORTED_RXDIDS");
515                 return ret;
516         }
517
518         vf->supported_rxdid =
519                 ((struct virtchnl_supported_rxdids *)args.out_buffer)->supported_rxdids;
520
521         return 0;
522 }
523
524 int
525 iavf_enable_queues(struct iavf_adapter *adapter)
526 {
527         struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
528         struct virtchnl_queue_select queue_select;
529         struct iavf_cmd_info args;
530         int err;
531
532         memset(&queue_select, 0, sizeof(queue_select));
533         queue_select.vsi_id = vf->vsi_res->vsi_id;
534
535         queue_select.rx_queues = BIT(adapter->eth_dev->data->nb_rx_queues) - 1;
536         queue_select.tx_queues = BIT(adapter->eth_dev->data->nb_tx_queues) - 1;
537
538         args.ops = VIRTCHNL_OP_ENABLE_QUEUES;
539         args.in_args = (u8 *)&queue_select;
540         args.in_args_size = sizeof(queue_select);
541         args.out_buffer = vf->aq_resp;
542         args.out_size = IAVF_AQ_BUF_SZ;
543         err = iavf_execute_vf_cmd(adapter, &args);
544         if (err) {
545                 PMD_DRV_LOG(ERR,
546                             "Failed to execute command of OP_ENABLE_QUEUES");
547                 return err;
548         }
549         return 0;
550 }
551
552 int
553 iavf_disable_queues(struct iavf_adapter *adapter)
554 {
555         struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
556         struct virtchnl_queue_select queue_select;
557         struct iavf_cmd_info args;
558         int err;
559
560         memset(&queue_select, 0, sizeof(queue_select));
561         queue_select.vsi_id = vf->vsi_res->vsi_id;
562
563         queue_select.rx_queues = BIT(adapter->eth_dev->data->nb_rx_queues) - 1;
564         queue_select.tx_queues = BIT(adapter->eth_dev->data->nb_tx_queues) - 1;
565
566         args.ops = VIRTCHNL_OP_DISABLE_QUEUES;
567         args.in_args = (u8 *)&queue_select;
568         args.in_args_size = sizeof(queue_select);
569         args.out_buffer = vf->aq_resp;
570         args.out_size = IAVF_AQ_BUF_SZ;
571         err = iavf_execute_vf_cmd(adapter, &args);
572         if (err) {
573                 PMD_DRV_LOG(ERR,
574                             "Failed to execute command of OP_DISABLE_QUEUES");
575                 return err;
576         }
577         return 0;
578 }
579
580 int
581 iavf_switch_queue(struct iavf_adapter *adapter, uint16_t qid,
582                  bool rx, bool on)
583 {
584         struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
585         struct virtchnl_queue_select queue_select;
586         struct iavf_cmd_info args;
587         int err;
588
589         memset(&queue_select, 0, sizeof(queue_select));
590         queue_select.vsi_id = vf->vsi_res->vsi_id;
591         if (rx)
592                 queue_select.rx_queues |= 1 << qid;
593         else
594                 queue_select.tx_queues |= 1 << qid;
595
596         if (on)
597                 args.ops = VIRTCHNL_OP_ENABLE_QUEUES;
598         else
599                 args.ops = VIRTCHNL_OP_DISABLE_QUEUES;
600         args.in_args = (u8 *)&queue_select;
601         args.in_args_size = sizeof(queue_select);
602         args.out_buffer = vf->aq_resp;
603         args.out_size = IAVF_AQ_BUF_SZ;
604         err = iavf_execute_vf_cmd(adapter, &args);
605         if (err)
606                 PMD_DRV_LOG(ERR, "Failed to execute command of %s",
607                             on ? "OP_ENABLE_QUEUES" : "OP_DISABLE_QUEUES");
608         return err;
609 }
610
611 int
612 iavf_enable_queues_lv(struct iavf_adapter *adapter)
613 {
614         struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
615         struct virtchnl_del_ena_dis_queues *queue_select;
616         struct virtchnl_queue_chunk *queue_chunk;
617         struct iavf_cmd_info args;
618         int err, len;
619
620         len = sizeof(struct virtchnl_del_ena_dis_queues) +
621                   sizeof(struct virtchnl_queue_chunk) *
622                   (IAVF_RXTX_QUEUE_CHUNKS_NUM - 1);
623         queue_select = rte_zmalloc("queue_select", len, 0);
624         if (!queue_select)
625                 return -ENOMEM;
626
627         queue_chunk = queue_select->chunks.chunks;
628         queue_select->chunks.num_chunks = IAVF_RXTX_QUEUE_CHUNKS_NUM;
629         queue_select->vport_id = vf->vsi_res->vsi_id;
630
631         queue_chunk[VIRTCHNL_QUEUE_TYPE_TX].type = VIRTCHNL_QUEUE_TYPE_TX;
632         queue_chunk[VIRTCHNL_QUEUE_TYPE_TX].start_queue_id = 0;
633         queue_chunk[VIRTCHNL_QUEUE_TYPE_TX].num_queues =
634                 adapter->eth_dev->data->nb_tx_queues;
635
636         queue_chunk[VIRTCHNL_QUEUE_TYPE_RX].type = VIRTCHNL_QUEUE_TYPE_RX;
637         queue_chunk[VIRTCHNL_QUEUE_TYPE_RX].start_queue_id = 0;
638         queue_chunk[VIRTCHNL_QUEUE_TYPE_RX].num_queues =
639                 adapter->eth_dev->data->nb_rx_queues;
640
641         args.ops = VIRTCHNL_OP_ENABLE_QUEUES_V2;
642         args.in_args = (u8 *)queue_select;
643         args.in_args_size = len;
644         args.out_buffer = vf->aq_resp;
645         args.out_size = IAVF_AQ_BUF_SZ;
646         err = iavf_execute_vf_cmd(adapter, &args);
647         if (err)
648                 PMD_DRV_LOG(ERR,
649                             "Failed to execute command of OP_ENABLE_QUEUES_V2");
650
651         rte_free(queue_select);
652         return err;
653 }
654
655 int
656 iavf_disable_queues_lv(struct iavf_adapter *adapter)
657 {
658         struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
659         struct virtchnl_del_ena_dis_queues *queue_select;
660         struct virtchnl_queue_chunk *queue_chunk;
661         struct iavf_cmd_info args;
662         int err, len;
663
664         len = sizeof(struct virtchnl_del_ena_dis_queues) +
665                   sizeof(struct virtchnl_queue_chunk) *
666                   (IAVF_RXTX_QUEUE_CHUNKS_NUM - 1);
667         queue_select = rte_zmalloc("queue_select", len, 0);
668         if (!queue_select)
669                 return -ENOMEM;
670
671         queue_chunk = queue_select->chunks.chunks;
672         queue_select->chunks.num_chunks = IAVF_RXTX_QUEUE_CHUNKS_NUM;
673         queue_select->vport_id = vf->vsi_res->vsi_id;
674
675         queue_chunk[VIRTCHNL_QUEUE_TYPE_TX].type = VIRTCHNL_QUEUE_TYPE_TX;
676         queue_chunk[VIRTCHNL_QUEUE_TYPE_TX].start_queue_id = 0;
677         queue_chunk[VIRTCHNL_QUEUE_TYPE_TX].num_queues =
678                 adapter->eth_dev->data->nb_tx_queues;
679
680         queue_chunk[VIRTCHNL_QUEUE_TYPE_RX].type = VIRTCHNL_QUEUE_TYPE_RX;
681         queue_chunk[VIRTCHNL_QUEUE_TYPE_RX].start_queue_id = 0;
682         queue_chunk[VIRTCHNL_QUEUE_TYPE_RX].num_queues =
683                 adapter->eth_dev->data->nb_rx_queues;
684
685         args.ops = VIRTCHNL_OP_DISABLE_QUEUES_V2;
686         args.in_args = (u8 *)queue_select;
687         args.in_args_size = len;
688         args.out_buffer = vf->aq_resp;
689         args.out_size = IAVF_AQ_BUF_SZ;
690         err = iavf_execute_vf_cmd(adapter, &args);
691         if (err)
692                 PMD_DRV_LOG(ERR,
693                             "Failed to execute command of OP_DISABLE_QUEUES_V2");
694
695         rte_free(queue_select);
696         return err;
697 }
698
699 int
700 iavf_switch_queue_lv(struct iavf_adapter *adapter, uint16_t qid,
701                  bool rx, bool on)
702 {
703         struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
704         struct virtchnl_del_ena_dis_queues *queue_select;
705         struct virtchnl_queue_chunk *queue_chunk;
706         struct iavf_cmd_info args;
707         int err, len;
708
709         len = sizeof(struct virtchnl_del_ena_dis_queues);
710         queue_select = rte_zmalloc("queue_select", len, 0);
711         if (!queue_select)
712                 return -ENOMEM;
713
714         queue_chunk = queue_select->chunks.chunks;
715         queue_select->chunks.num_chunks = 1;
716         queue_select->vport_id = vf->vsi_res->vsi_id;
717
718         if (rx) {
719                 queue_chunk->type = VIRTCHNL_QUEUE_TYPE_RX;
720                 queue_chunk->start_queue_id = qid;
721                 queue_chunk->num_queues = 1;
722         } else {
723                 queue_chunk->type = VIRTCHNL_QUEUE_TYPE_TX;
724                 queue_chunk->start_queue_id = qid;
725                 queue_chunk->num_queues = 1;
726         }
727
728         if (on)
729                 args.ops = VIRTCHNL_OP_ENABLE_QUEUES_V2;
730         else
731                 args.ops = VIRTCHNL_OP_DISABLE_QUEUES_V2;
732         args.in_args = (u8 *)queue_select;
733         args.in_args_size = len;
734         args.out_buffer = vf->aq_resp;
735         args.out_size = IAVF_AQ_BUF_SZ;
736         err = iavf_execute_vf_cmd(adapter, &args);
737         if (err)
738                 PMD_DRV_LOG(ERR, "Failed to execute command of %s",
739                             on ? "OP_ENABLE_QUEUES_V2" : "OP_DISABLE_QUEUES_V2");
740
741         rte_free(queue_select);
742         return err;
743 }
744
745 int
746 iavf_configure_rss_lut(struct iavf_adapter *adapter)
747 {
748         struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
749         struct virtchnl_rss_lut *rss_lut;
750         struct iavf_cmd_info args;
751         int len, err = 0;
752
753         len = sizeof(*rss_lut) + vf->vf_res->rss_lut_size - 1;
754         rss_lut = rte_zmalloc("rss_lut", len, 0);
755         if (!rss_lut)
756                 return -ENOMEM;
757
758         rss_lut->vsi_id = vf->vsi_res->vsi_id;
759         rss_lut->lut_entries = vf->vf_res->rss_lut_size;
760         rte_memcpy(rss_lut->lut, vf->rss_lut, vf->vf_res->rss_lut_size);
761
762         args.ops = VIRTCHNL_OP_CONFIG_RSS_LUT;
763         args.in_args = (u8 *)rss_lut;
764         args.in_args_size = len;
765         args.out_buffer = vf->aq_resp;
766         args.out_size = IAVF_AQ_BUF_SZ;
767
768         err = iavf_execute_vf_cmd(adapter, &args);
769         if (err)
770                 PMD_DRV_LOG(ERR,
771                             "Failed to execute command of OP_CONFIG_RSS_LUT");
772
773         rte_free(rss_lut);
774         return err;
775 }
776
777 int
778 iavf_configure_rss_key(struct iavf_adapter *adapter)
779 {
780         struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
781         struct virtchnl_rss_key *rss_key;
782         struct iavf_cmd_info args;
783         int len, err = 0;
784
785         len = sizeof(*rss_key) + vf->vf_res->rss_key_size - 1;
786         rss_key = rte_zmalloc("rss_key", len, 0);
787         if (!rss_key)
788                 return -ENOMEM;
789
790         rss_key->vsi_id = vf->vsi_res->vsi_id;
791         rss_key->key_len = vf->vf_res->rss_key_size;
792         rte_memcpy(rss_key->key, vf->rss_key, vf->vf_res->rss_key_size);
793
794         args.ops = VIRTCHNL_OP_CONFIG_RSS_KEY;
795         args.in_args = (u8 *)rss_key;
796         args.in_args_size = len;
797         args.out_buffer = vf->aq_resp;
798         args.out_size = IAVF_AQ_BUF_SZ;
799
800         err = iavf_execute_vf_cmd(adapter, &args);
801         if (err)
802                 PMD_DRV_LOG(ERR,
803                             "Failed to execute command of OP_CONFIG_RSS_KEY");
804
805         rte_free(rss_key);
806         return err;
807 }
808
809 int
810 iavf_configure_queues(struct iavf_adapter *adapter,
811                 uint16_t num_queue_pairs, uint16_t index)
812 {
813         struct iavf_rx_queue **rxq =
814                 (struct iavf_rx_queue **)adapter->eth_dev->data->rx_queues;
815         struct iavf_tx_queue **txq =
816                 (struct iavf_tx_queue **)adapter->eth_dev->data->tx_queues;
817         struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
818         struct virtchnl_vsi_queue_config_info *vc_config;
819         struct virtchnl_queue_pair_info *vc_qp;
820         struct iavf_cmd_info args;
821         uint16_t i, size;
822         int err;
823
824         size = sizeof(*vc_config) +
825                sizeof(vc_config->qpair[0]) * num_queue_pairs;
826         vc_config = rte_zmalloc("cfg_queue", size, 0);
827         if (!vc_config)
828                 return -ENOMEM;
829
830         vc_config->vsi_id = vf->vsi_res->vsi_id;
831         vc_config->num_queue_pairs = num_queue_pairs;
832
833         for (i = index, vc_qp = vc_config->qpair;
834                  i < index + num_queue_pairs;
835              i++, vc_qp++) {
836                 vc_qp->txq.vsi_id = vf->vsi_res->vsi_id;
837                 vc_qp->txq.queue_id = i;
838
839                 /* Virtchnnl configure tx queues by pairs */
840                 if (i < adapter->eth_dev->data->nb_tx_queues) {
841                         vc_qp->txq.ring_len = txq[i]->nb_tx_desc;
842                         vc_qp->txq.dma_ring_addr = txq[i]->tx_ring_phys_addr;
843                 }
844
845                 vc_qp->rxq.vsi_id = vf->vsi_res->vsi_id;
846                 vc_qp->rxq.queue_id = i;
847                 vc_qp->rxq.max_pkt_size = vf->max_pkt_len;
848
849                 if (i >= adapter->eth_dev->data->nb_rx_queues)
850                         continue;
851
852                 /* Virtchnnl configure rx queues by pairs */
853                 vc_qp->rxq.ring_len = rxq[i]->nb_rx_desc;
854                 vc_qp->rxq.dma_ring_addr = rxq[i]->rx_ring_phys_addr;
855                 vc_qp->rxq.databuffer_size = rxq[i]->rx_buf_len;
856
857 #ifndef RTE_LIBRTE_IAVF_16BYTE_RX_DESC
858                 if (vf->vf_res->vf_cap_flags &
859                     VIRTCHNL_VF_OFFLOAD_RX_FLEX_DESC &&
860                     vf->supported_rxdid & BIT(rxq[i]->rxdid)) {
861                         vc_qp->rxq.rxdid = rxq[i]->rxdid;
862                         PMD_DRV_LOG(NOTICE, "request RXDID[%d] in Queue[%d]",
863                                     vc_qp->rxq.rxdid, i);
864                 } else {
865                         PMD_DRV_LOG(NOTICE, "RXDID[%d] is not supported, "
866                                     "request default RXDID[%d] in Queue[%d]",
867                                     rxq[i]->rxdid, IAVF_RXDID_LEGACY_1, i);
868                         vc_qp->rxq.rxdid = IAVF_RXDID_LEGACY_1;
869                 }
870 #else
871                 if (vf->vf_res->vf_cap_flags &
872                         VIRTCHNL_VF_OFFLOAD_RX_FLEX_DESC &&
873                         vf->supported_rxdid & BIT(IAVF_RXDID_LEGACY_0)) {
874                         vc_qp->rxq.rxdid = IAVF_RXDID_LEGACY_0;
875                         PMD_DRV_LOG(NOTICE, "request RXDID[%d] in Queue[%d]",
876                                     vc_qp->rxq.rxdid, i);
877                 } else {
878                         PMD_DRV_LOG(ERR, "RXDID[%d] is not supported",
879                                     IAVF_RXDID_LEGACY_0);
880                         return -1;
881                 }
882 #endif
883         }
884
885         memset(&args, 0, sizeof(args));
886         args.ops = VIRTCHNL_OP_CONFIG_VSI_QUEUES;
887         args.in_args = (uint8_t *)vc_config;
888         args.in_args_size = size;
889         args.out_buffer = vf->aq_resp;
890         args.out_size = IAVF_AQ_BUF_SZ;
891
892         err = iavf_execute_vf_cmd(adapter, &args);
893         if (err)
894                 PMD_DRV_LOG(ERR, "Failed to execute command of"
895                             " VIRTCHNL_OP_CONFIG_VSI_QUEUES");
896
897         rte_free(vc_config);
898         return err;
899 }
900
901 int
902 iavf_config_irq_map(struct iavf_adapter *adapter)
903 {
904         struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
905         struct virtchnl_irq_map_info *map_info;
906         struct virtchnl_vector_map *vecmap;
907         struct iavf_cmd_info args;
908         int len, i, err;
909
910         len = sizeof(struct virtchnl_irq_map_info) +
911               sizeof(struct virtchnl_vector_map) * vf->nb_msix;
912
913         map_info = rte_zmalloc("map_info", len, 0);
914         if (!map_info)
915                 return -ENOMEM;
916
917         map_info->num_vectors = vf->nb_msix;
918         for (i = 0; i < adapter->eth_dev->data->nb_rx_queues; i++) {
919                 vecmap =
920                     &map_info->vecmap[vf->qv_map[i].vector_id - vf->msix_base];
921                 vecmap->vsi_id = vf->vsi_res->vsi_id;
922                 vecmap->rxitr_idx = IAVF_ITR_INDEX_DEFAULT;
923                 vecmap->vector_id = vf->qv_map[i].vector_id;
924                 vecmap->txq_map = 0;
925                 vecmap->rxq_map |= 1 << vf->qv_map[i].queue_id;
926         }
927
928         args.ops = VIRTCHNL_OP_CONFIG_IRQ_MAP;
929         args.in_args = (u8 *)map_info;
930         args.in_args_size = len;
931         args.out_buffer = vf->aq_resp;
932         args.out_size = IAVF_AQ_BUF_SZ;
933         err = iavf_execute_vf_cmd(adapter, &args);
934         if (err)
935                 PMD_DRV_LOG(ERR, "fail to execute command OP_CONFIG_IRQ_MAP");
936
937         rte_free(map_info);
938         return err;
939 }
940
941 int
942 iavf_config_irq_map_lv(struct iavf_adapter *adapter, uint16_t num,
943                 uint16_t index)
944 {
945         struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
946         struct virtchnl_queue_vector_maps *map_info;
947         struct virtchnl_queue_vector *qv_maps;
948         struct iavf_cmd_info args;
949         int len, i, err;
950         int count = 0;
951
952         len = sizeof(struct virtchnl_queue_vector_maps) +
953               sizeof(struct virtchnl_queue_vector) * (num - 1);
954
955         map_info = rte_zmalloc("map_info", len, 0);
956         if (!map_info)
957                 return -ENOMEM;
958
959         map_info->vport_id = vf->vsi_res->vsi_id;
960         map_info->num_qv_maps = num;
961         for (i = index; i < index + map_info->num_qv_maps; i++) {
962                 qv_maps = &map_info->qv_maps[count++];
963                 qv_maps->itr_idx = VIRTCHNL_ITR_IDX_0;
964                 qv_maps->queue_type = VIRTCHNL_QUEUE_TYPE_RX;
965                 qv_maps->queue_id = vf->qv_map[i].queue_id;
966                 qv_maps->vector_id = vf->qv_map[i].vector_id;
967         }
968
969         args.ops = VIRTCHNL_OP_MAP_QUEUE_VECTOR;
970         args.in_args = (u8 *)map_info;
971         args.in_args_size = len;
972         args.out_buffer = vf->aq_resp;
973         args.out_size = IAVF_AQ_BUF_SZ;
974         err = iavf_execute_vf_cmd(adapter, &args);
975         if (err)
976                 PMD_DRV_LOG(ERR, "fail to execute command OP_MAP_QUEUE_VECTOR");
977
978         rte_free(map_info);
979         return err;
980 }
981
982 void
983 iavf_add_del_all_mac_addr(struct iavf_adapter *adapter, bool add)
984 {
985         struct virtchnl_ether_addr_list *list;
986         struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
987         struct rte_ether_addr *addr;
988         struct iavf_cmd_info args;
989         int len, err, i, j;
990         int next_begin = 0;
991         int begin = 0;
992
993         do {
994                 j = 0;
995                 len = sizeof(struct virtchnl_ether_addr_list);
996                 for (i = begin; i < IAVF_NUM_MACADDR_MAX; i++, next_begin++) {
997                         addr = &adapter->eth_dev->data->mac_addrs[i];
998                         if (rte_is_zero_ether_addr(addr))
999                                 continue;
1000                         len += sizeof(struct virtchnl_ether_addr);
1001                         if (len >= IAVF_AQ_BUF_SZ) {
1002                                 next_begin = i + 1;
1003                                 break;
1004                         }
1005                 }
1006
1007                 list = rte_zmalloc("iavf_del_mac_buffer", len, 0);
1008                 if (!list) {
1009                         PMD_DRV_LOG(ERR, "fail to allocate memory");
1010                         return;
1011                 }
1012
1013                 for (i = begin; i < next_begin; i++) {
1014                         addr = &adapter->eth_dev->data->mac_addrs[i];
1015                         if (rte_is_zero_ether_addr(addr))
1016                                 continue;
1017                         rte_memcpy(list->list[j].addr, addr->addr_bytes,
1018                                    sizeof(addr->addr_bytes));
1019                         PMD_DRV_LOG(DEBUG, "add/rm mac:%x:%x:%x:%x:%x:%x",
1020                                     addr->addr_bytes[0], addr->addr_bytes[1],
1021                                     addr->addr_bytes[2], addr->addr_bytes[3],
1022                                     addr->addr_bytes[4], addr->addr_bytes[5]);
1023                         j++;
1024                 }
1025                 list->vsi_id = vf->vsi_res->vsi_id;
1026                 list->num_elements = j;
1027                 args.ops = add ? VIRTCHNL_OP_ADD_ETH_ADDR :
1028                            VIRTCHNL_OP_DEL_ETH_ADDR;
1029                 args.in_args = (uint8_t *)list;
1030                 args.in_args_size = len;
1031                 args.out_buffer = vf->aq_resp;
1032                 args.out_size = IAVF_AQ_BUF_SZ;
1033                 err = iavf_execute_vf_cmd(adapter, &args);
1034                 if (err)
1035                         PMD_DRV_LOG(ERR, "fail to execute command %s",
1036                                     add ? "OP_ADD_ETHER_ADDRESS" :
1037                                     "OP_DEL_ETHER_ADDRESS");
1038                 rte_free(list);
1039                 begin = next_begin;
1040         } while (begin < IAVF_NUM_MACADDR_MAX);
1041 }
1042
1043 int
1044 iavf_query_stats(struct iavf_adapter *adapter,
1045                 struct virtchnl_eth_stats **pstats)
1046 {
1047         struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
1048         struct virtchnl_queue_select q_stats;
1049         struct iavf_cmd_info args;
1050         int err;
1051
1052         memset(&q_stats, 0, sizeof(q_stats));
1053         q_stats.vsi_id = vf->vsi_res->vsi_id;
1054         args.ops = VIRTCHNL_OP_GET_STATS;
1055         args.in_args = (uint8_t *)&q_stats;
1056         args.in_args_size = sizeof(q_stats);
1057         args.out_buffer = vf->aq_resp;
1058         args.out_size = IAVF_AQ_BUF_SZ;
1059
1060         err = iavf_execute_vf_cmd(adapter, &args);
1061         if (err) {
1062                 PMD_DRV_LOG(ERR, "fail to execute command OP_GET_STATS");
1063                 *pstats = NULL;
1064                 return err;
1065         }
1066         *pstats = (struct virtchnl_eth_stats *)args.out_buffer;
1067         return 0;
1068 }
1069
1070 int
1071 iavf_config_promisc(struct iavf_adapter *adapter,
1072                    bool enable_unicast,
1073                    bool enable_multicast)
1074 {
1075         struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
1076         struct virtchnl_promisc_info promisc;
1077         struct iavf_cmd_info args;
1078         int err;
1079
1080         promisc.flags = 0;
1081         promisc.vsi_id = vf->vsi_res->vsi_id;
1082
1083         if (enable_unicast)
1084                 promisc.flags |= FLAG_VF_UNICAST_PROMISC;
1085
1086         if (enable_multicast)
1087                 promisc.flags |= FLAG_VF_MULTICAST_PROMISC;
1088
1089         args.ops = VIRTCHNL_OP_CONFIG_PROMISCUOUS_MODE;
1090         args.in_args = (uint8_t *)&promisc;
1091         args.in_args_size = sizeof(promisc);
1092         args.out_buffer = vf->aq_resp;
1093         args.out_size = IAVF_AQ_BUF_SZ;
1094
1095         err = iavf_execute_vf_cmd(adapter, &args);
1096
1097         if (err) {
1098                 PMD_DRV_LOG(ERR,
1099                             "fail to execute command CONFIG_PROMISCUOUS_MODE");
1100
1101                 if (err == IAVF_NOT_SUPPORTED)
1102                         return -ENOTSUP;
1103
1104                 return -EAGAIN;
1105         }
1106
1107         vf->promisc_unicast_enabled = enable_unicast;
1108         vf->promisc_multicast_enabled = enable_multicast;
1109         return 0;
1110 }
1111
1112 int
1113 iavf_add_del_eth_addr(struct iavf_adapter *adapter, struct rte_ether_addr *addr,
1114                      bool add)
1115 {
1116         struct virtchnl_ether_addr_list *list;
1117         struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
1118         uint8_t cmd_buffer[sizeof(struct virtchnl_ether_addr_list) +
1119                            sizeof(struct virtchnl_ether_addr)];
1120         struct iavf_cmd_info args;
1121         int err;
1122
1123         list = (struct virtchnl_ether_addr_list *)cmd_buffer;
1124         list->vsi_id = vf->vsi_res->vsi_id;
1125         list->num_elements = 1;
1126         rte_memcpy(list->list[0].addr, addr->addr_bytes,
1127                    sizeof(addr->addr_bytes));
1128
1129         args.ops = add ? VIRTCHNL_OP_ADD_ETH_ADDR : VIRTCHNL_OP_DEL_ETH_ADDR;
1130         args.in_args = cmd_buffer;
1131         args.in_args_size = sizeof(cmd_buffer);
1132         args.out_buffer = vf->aq_resp;
1133         args.out_size = IAVF_AQ_BUF_SZ;
1134         err = iavf_execute_vf_cmd(adapter, &args);
1135         if (err)
1136                 PMD_DRV_LOG(ERR, "fail to execute command %s",
1137                             add ? "OP_ADD_ETH_ADDR" :  "OP_DEL_ETH_ADDR");
1138         return err;
1139 }
1140
1141 int
1142 iavf_add_del_vlan(struct iavf_adapter *adapter, uint16_t vlanid, bool add)
1143 {
1144         struct virtchnl_vlan_filter_list *vlan_list;
1145         struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
1146         uint8_t cmd_buffer[sizeof(struct virtchnl_vlan_filter_list) +
1147                                                         sizeof(uint16_t)];
1148         struct iavf_cmd_info args;
1149         int err;
1150
1151         vlan_list = (struct virtchnl_vlan_filter_list *)cmd_buffer;
1152         vlan_list->vsi_id = vf->vsi_res->vsi_id;
1153         vlan_list->num_elements = 1;
1154         vlan_list->vlan_id[0] = vlanid;
1155
1156         args.ops = add ? VIRTCHNL_OP_ADD_VLAN : VIRTCHNL_OP_DEL_VLAN;
1157         args.in_args = cmd_buffer;
1158         args.in_args_size = sizeof(cmd_buffer);
1159         args.out_buffer = vf->aq_resp;
1160         args.out_size = IAVF_AQ_BUF_SZ;
1161         err = iavf_execute_vf_cmd(adapter, &args);
1162         if (err)
1163                 PMD_DRV_LOG(ERR, "fail to execute command %s",
1164                             add ? "OP_ADD_VLAN" :  "OP_DEL_VLAN");
1165
1166         return err;
1167 }
1168
1169 int
1170 iavf_fdir_add(struct iavf_adapter *adapter,
1171         struct iavf_fdir_conf *filter)
1172 {
1173         struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
1174         struct virtchnl_fdir_add *fdir_ret;
1175
1176         struct iavf_cmd_info args;
1177         int err;
1178
1179         filter->add_fltr.vsi_id = vf->vsi_res->vsi_id;
1180         filter->add_fltr.validate_only = 0;
1181
1182         args.ops = VIRTCHNL_OP_ADD_FDIR_FILTER;
1183         args.in_args = (uint8_t *)(&filter->add_fltr);
1184         args.in_args_size = sizeof(*(&filter->add_fltr));
1185         args.out_buffer = vf->aq_resp;
1186         args.out_size = IAVF_AQ_BUF_SZ;
1187
1188         err = iavf_execute_vf_cmd(adapter, &args);
1189         if (err) {
1190                 PMD_DRV_LOG(ERR, "fail to execute command OP_ADD_FDIR_FILTER");
1191                 return err;
1192         }
1193
1194         fdir_ret = (struct virtchnl_fdir_add *)args.out_buffer;
1195         filter->flow_id = fdir_ret->flow_id;
1196
1197         if (fdir_ret->status == VIRTCHNL_FDIR_SUCCESS) {
1198                 PMD_DRV_LOG(INFO,
1199                         "Succeed in adding rule request by PF");
1200         } else if (fdir_ret->status == VIRTCHNL_FDIR_FAILURE_RULE_NORESOURCE) {
1201                 PMD_DRV_LOG(ERR,
1202                         "Failed to add rule request due to no hw resource");
1203                 return -1;
1204         } else if (fdir_ret->status == VIRTCHNL_FDIR_FAILURE_RULE_EXIST) {
1205                 PMD_DRV_LOG(ERR,
1206                         "Failed to add rule request due to the rule is already existed");
1207                 return -1;
1208         } else if (fdir_ret->status == VIRTCHNL_FDIR_FAILURE_RULE_CONFLICT) {
1209                 PMD_DRV_LOG(ERR,
1210                         "Failed to add rule request due to the rule is conflict with existing rule");
1211                 return -1;
1212         } else if (fdir_ret->status == VIRTCHNL_FDIR_FAILURE_RULE_INVALID) {
1213                 PMD_DRV_LOG(ERR,
1214                         "Failed to add rule request due to the hw doesn't support");
1215                 return -1;
1216         } else if (fdir_ret->status == VIRTCHNL_FDIR_FAILURE_RULE_TIMEOUT) {
1217                 PMD_DRV_LOG(ERR,
1218                         "Failed to add rule request due to time out for programming");
1219                 return -1;
1220         } else {
1221                 PMD_DRV_LOG(ERR,
1222                         "Failed to add rule request due to other reasons");
1223                 return -1;
1224         }
1225
1226         return 0;
1227 };
1228
1229 int
1230 iavf_fdir_del(struct iavf_adapter *adapter,
1231         struct iavf_fdir_conf *filter)
1232 {
1233         struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
1234         struct virtchnl_fdir_del *fdir_ret;
1235
1236         struct iavf_cmd_info args;
1237         int err;
1238
1239         filter->del_fltr.vsi_id = vf->vsi_res->vsi_id;
1240         filter->del_fltr.flow_id = filter->flow_id;
1241
1242         args.ops = VIRTCHNL_OP_DEL_FDIR_FILTER;
1243         args.in_args = (uint8_t *)(&filter->del_fltr);
1244         args.in_args_size = sizeof(filter->del_fltr);
1245         args.out_buffer = vf->aq_resp;
1246         args.out_size = IAVF_AQ_BUF_SZ;
1247
1248         err = iavf_execute_vf_cmd(adapter, &args);
1249         if (err) {
1250                 PMD_DRV_LOG(ERR, "fail to execute command OP_DEL_FDIR_FILTER");
1251                 return err;
1252         }
1253
1254         fdir_ret = (struct virtchnl_fdir_del *)args.out_buffer;
1255
1256         if (fdir_ret->status == VIRTCHNL_FDIR_SUCCESS) {
1257                 PMD_DRV_LOG(INFO,
1258                         "Succeed in deleting rule request by PF");
1259         } else if (fdir_ret->status == VIRTCHNL_FDIR_FAILURE_RULE_NONEXIST) {
1260                 PMD_DRV_LOG(ERR,
1261                         "Failed to delete rule request due to this rule doesn't exist");
1262                 return -1;
1263         } else if (fdir_ret->status == VIRTCHNL_FDIR_FAILURE_RULE_TIMEOUT) {
1264                 PMD_DRV_LOG(ERR,
1265                         "Failed to delete rule request due to time out for programming");
1266                 return -1;
1267         } else {
1268                 PMD_DRV_LOG(ERR,
1269                         "Failed to delete rule request due to other reasons");
1270                 return -1;
1271         }
1272
1273         return 0;
1274 };
1275
1276 int
1277 iavf_fdir_check(struct iavf_adapter *adapter,
1278                 struct iavf_fdir_conf *filter)
1279 {
1280         struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
1281         struct virtchnl_fdir_add *fdir_ret;
1282
1283         struct iavf_cmd_info args;
1284         int err;
1285
1286         filter->add_fltr.vsi_id = vf->vsi_res->vsi_id;
1287         filter->add_fltr.validate_only = 1;
1288
1289         args.ops = VIRTCHNL_OP_ADD_FDIR_FILTER;
1290         args.in_args = (uint8_t *)(&filter->add_fltr);
1291         args.in_args_size = sizeof(*(&filter->add_fltr));
1292         args.out_buffer = vf->aq_resp;
1293         args.out_size = IAVF_AQ_BUF_SZ;
1294
1295         err = iavf_execute_vf_cmd(adapter, &args);
1296         if (err) {
1297                 PMD_DRV_LOG(ERR, "fail to check flow direcotor rule");
1298                 return err;
1299         }
1300
1301         fdir_ret = (struct virtchnl_fdir_add *)args.out_buffer;
1302
1303         if (fdir_ret->status == VIRTCHNL_FDIR_SUCCESS) {
1304                 PMD_DRV_LOG(INFO,
1305                         "Succeed in checking rule request by PF");
1306         } else if (fdir_ret->status == VIRTCHNL_FDIR_FAILURE_RULE_INVALID) {
1307                 PMD_DRV_LOG(ERR,
1308                         "Failed to check rule request due to parameters validation"
1309                         " or HW doesn't support");
1310                 return -1;
1311         } else {
1312                 PMD_DRV_LOG(ERR,
1313                         "Failed to check rule request due to other reasons");
1314                 return -1;
1315         }
1316
1317         return 0;
1318 }
1319
1320 int
1321 iavf_add_del_rss_cfg(struct iavf_adapter *adapter,
1322                      struct virtchnl_rss_cfg *rss_cfg, bool add)
1323 {
1324         struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
1325         struct iavf_cmd_info args;
1326         int err;
1327
1328         memset(&args, 0, sizeof(args));
1329         args.ops = add ? VIRTCHNL_OP_ADD_RSS_CFG :
1330                 VIRTCHNL_OP_DEL_RSS_CFG;
1331         args.in_args = (u8 *)rss_cfg;
1332         args.in_args_size = sizeof(*rss_cfg);
1333         args.out_buffer = vf->aq_resp;
1334         args.out_size = IAVF_AQ_BUF_SZ;
1335
1336         err = iavf_execute_vf_cmd(adapter, &args);
1337         if (err)
1338                 PMD_DRV_LOG(ERR,
1339                             "Failed to execute command of %s",
1340                             add ? "OP_ADD_RSS_CFG" :
1341                             "OP_DEL_RSS_INPUT_CFG");
1342
1343         return err;
1344 }
1345
1346 int
1347 iavf_add_del_mc_addr_list(struct iavf_adapter *adapter,
1348                         struct rte_ether_addr *mc_addrs,
1349                         uint32_t mc_addrs_num, bool add)
1350 {
1351         struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
1352         uint8_t cmd_buffer[sizeof(struct virtchnl_ether_addr_list) +
1353                 (IAVF_NUM_MACADDR_MAX * sizeof(struct virtchnl_ether_addr))];
1354         struct virtchnl_ether_addr_list *list;
1355         struct iavf_cmd_info args;
1356         uint32_t i;
1357         int err;
1358
1359         if (mc_addrs == NULL || mc_addrs_num == 0)
1360                 return 0;
1361
1362         list = (struct virtchnl_ether_addr_list *)cmd_buffer;
1363         list->vsi_id = vf->vsi_res->vsi_id;
1364         list->num_elements = mc_addrs_num;
1365
1366         for (i = 0; i < mc_addrs_num; i++) {
1367                 if (!IAVF_IS_MULTICAST(mc_addrs[i].addr_bytes)) {
1368                         PMD_DRV_LOG(ERR, "Invalid mac:%x:%x:%x:%x:%x:%x",
1369                                     mc_addrs[i].addr_bytes[0],
1370                                     mc_addrs[i].addr_bytes[1],
1371                                     mc_addrs[i].addr_bytes[2],
1372                                     mc_addrs[i].addr_bytes[3],
1373                                     mc_addrs[i].addr_bytes[4],
1374                                     mc_addrs[i].addr_bytes[5]);
1375                         return -EINVAL;
1376                 }
1377
1378                 memcpy(list->list[i].addr, mc_addrs[i].addr_bytes,
1379                         sizeof(list->list[i].addr));
1380         }
1381
1382         args.ops = add ? VIRTCHNL_OP_ADD_ETH_ADDR : VIRTCHNL_OP_DEL_ETH_ADDR;
1383         args.in_args = cmd_buffer;
1384         args.in_args_size = sizeof(struct virtchnl_ether_addr_list) +
1385                 i * sizeof(struct virtchnl_ether_addr);
1386         args.out_buffer = vf->aq_resp;
1387         args.out_size = IAVF_AQ_BUF_SZ;
1388         err = iavf_execute_vf_cmd(adapter, &args);
1389
1390         if (err) {
1391                 PMD_DRV_LOG(ERR, "fail to execute command %s",
1392                         add ? "OP_ADD_ETH_ADDR" : "OP_DEL_ETH_ADDR");
1393                 return err;
1394         }
1395
1396         return 0;
1397 }
1398
1399 int
1400 iavf_request_queues(struct iavf_adapter *adapter, uint16_t num)
1401 {
1402         struct rte_eth_dev *dev = adapter->eth_dev;
1403         struct iavf_info *vf =  IAVF_DEV_PRIVATE_TO_VF(adapter);
1404         struct rte_pci_device *pci_dev = RTE_ETH_DEV_TO_PCI(dev);
1405         struct virtchnl_vf_res_request vfres;
1406         struct iavf_cmd_info args;
1407         uint16_t num_queue_pairs;
1408         int err;
1409
1410         if (!(vf->vf_res->vf_cap_flags &
1411                 VIRTCHNL_VF_OFFLOAD_REQ_QUEUES)) {
1412                 PMD_DRV_LOG(ERR, "request queues not supported");
1413                 return -1;
1414         }
1415
1416         if (num == 0) {
1417                 PMD_DRV_LOG(ERR, "queue number cannot be zero");
1418                 return -1;
1419         }
1420         vfres.num_queue_pairs = num;
1421
1422         args.ops = VIRTCHNL_OP_REQUEST_QUEUES;
1423         args.in_args = (u8 *)&vfres;
1424         args.in_args_size = sizeof(vfres);
1425         args.out_buffer = vf->aq_resp;
1426         args.out_size = IAVF_AQ_BUF_SZ;
1427
1428         /*
1429          * disable interrupt to avoid the admin queue message to be read
1430          * before iavf_read_msg_from_pf.
1431          */
1432         rte_intr_disable(&pci_dev->intr_handle);
1433         err = iavf_execute_vf_cmd(adapter, &args);
1434         rte_intr_enable(&pci_dev->intr_handle);
1435         if (err) {
1436                 PMD_DRV_LOG(ERR, "fail to execute command OP_REQUEST_QUEUES");
1437                 return err;
1438         }
1439
1440         /* request queues succeeded, vf is resetting */
1441         if (vf->vf_reset) {
1442                 PMD_DRV_LOG(INFO, "vf is resetting");
1443                 return 0;
1444         }
1445
1446         /* request additional queues failed, return available number */
1447         num_queue_pairs =
1448           ((struct virtchnl_vf_res_request *)args.out_buffer)->num_queue_pairs;
1449         PMD_DRV_LOG(ERR, "request queues failed, only %u queues "
1450                 "available", num_queue_pairs);
1451
1452         return -1;
1453 }
1454
1455 int
1456 iavf_get_max_rss_queue_region(struct iavf_adapter *adapter)
1457 {
1458         struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
1459         struct iavf_cmd_info args;
1460         uint16_t qregion_width;
1461         int err;
1462
1463         args.ops = VIRTCHNL_OP_GET_MAX_RSS_QREGION;
1464         args.in_args = NULL;
1465         args.in_args_size = 0;
1466         args.out_buffer = vf->aq_resp;
1467         args.out_size = IAVF_AQ_BUF_SZ;
1468
1469         err = iavf_execute_vf_cmd(adapter, &args);
1470         if (err) {
1471                 PMD_DRV_LOG(ERR, "Failed to execute command of VIRTCHNL_OP_GET_MAX_RSS_QREGION");
1472                 return err;
1473         }
1474
1475         qregion_width =
1476         ((struct virtchnl_max_rss_qregion *)args.out_buffer)->qregion_width;
1477
1478         vf->max_rss_qregion = (uint16_t)(1 << qregion_width);
1479
1480         return 0;
1481 }