b913f06c3a2f57fd9919f9f909cb60111892bcbb
[dpdk.git] / drivers / net / iavf / iavf_vchnl.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2017 Intel Corporation
3  */
4
5 #include <stdio.h>
6 #include <errno.h>
7 #include <stdint.h>
8 #include <string.h>
9 #include <unistd.h>
10 #include <stdarg.h>
11 #include <inttypes.h>
12 #include <rte_byteorder.h>
13 #include <rte_common.h>
14
15 #include <rte_debug.h>
16 #include <rte_atomic.h>
17 #include <rte_eal.h>
18 #include <rte_ether.h>
19 #include <rte_ethdev_driver.h>
20 #include <rte_dev.h>
21
22 #include "iavf.h"
23 #include "iavf_rxtx.h"
24
25 #define MAX_TRY_TIMES 200
26 #define ASQ_DELAY_MS  10
27
28 /* Read data in admin queue to get msg from pf driver */
29 static enum iavf_status
30 iavf_read_msg_from_pf(struct iavf_adapter *adapter, uint16_t buf_len,
31                      uint8_t *buf)
32 {
33         struct iavf_hw *hw = IAVF_DEV_PRIVATE_TO_HW(adapter);
34         struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
35         struct iavf_arq_event_info event;
36         enum virtchnl_ops opcode;
37         int ret;
38
39         event.buf_len = buf_len;
40         event.msg_buf = buf;
41         ret = iavf_clean_arq_element(hw, &event, NULL);
42         /* Can't read any msg from adminQ */
43         if (ret) {
44                 PMD_DRV_LOG(DEBUG, "Can't read msg from AQ");
45                 return ret;
46         }
47
48         opcode = (enum virtchnl_ops)rte_le_to_cpu_32(event.desc.cookie_high);
49         vf->cmd_retval = (enum virtchnl_status_code)rte_le_to_cpu_32(
50                         event.desc.cookie_low);
51
52         PMD_DRV_LOG(DEBUG, "AQ from pf carries opcode %u, retval %d",
53                     opcode, vf->cmd_retval);
54
55         if (opcode != vf->pend_cmd) {
56                 PMD_DRV_LOG(WARNING, "command mismatch, expect %u, get %u",
57                             vf->pend_cmd, opcode);
58                 return IAVF_ERR_OPCODE_MISMATCH;
59         }
60
61         return IAVF_SUCCESS;
62 }
63
64 static int
65 iavf_execute_vf_cmd(struct iavf_adapter *adapter, struct iavf_cmd_info *args)
66 {
67         struct iavf_hw *hw = IAVF_DEV_PRIVATE_TO_HW(adapter);
68         struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
69         enum iavf_status ret;
70         int err = 0;
71         int i = 0;
72
73         if (_atomic_set_cmd(vf, args->ops))
74                 return -1;
75
76         ret = iavf_aq_send_msg_to_pf(hw, args->ops, IAVF_SUCCESS,
77                                     args->in_args, args->in_args_size, NULL);
78         if (ret) {
79                 PMD_DRV_LOG(ERR, "fail to send cmd %d", args->ops);
80                 _clear_cmd(vf);
81                 return err;
82         }
83
84         switch (args->ops) {
85         case VIRTCHNL_OP_RESET_VF:
86                 /*no need to wait for response */
87                 _clear_cmd(vf);
88                 break;
89         case VIRTCHNL_OP_VERSION:
90         case VIRTCHNL_OP_GET_VF_RESOURCES:
91         case VIRTCHNL_OP_GET_SUPPORTED_RXDIDS:
92                 /* for init virtchnl ops, need to poll the response */
93                 do {
94                         ret = iavf_read_msg_from_pf(adapter, args->out_size,
95                                                    args->out_buffer);
96                         if (ret == IAVF_SUCCESS)
97                                 break;
98                         rte_delay_ms(ASQ_DELAY_MS);
99                 } while (i++ < MAX_TRY_TIMES);
100                 if (i >= MAX_TRY_TIMES ||
101                     vf->cmd_retval != VIRTCHNL_STATUS_SUCCESS) {
102                         err = -1;
103                         PMD_DRV_LOG(ERR, "No response or return failure (%d)"
104                                     " for cmd %d", vf->cmd_retval, args->ops);
105                 }
106                 _clear_cmd(vf);
107                 break;
108
109         default:
110                 /* For other virtchnl ops in running time,
111                  * wait for the cmd done flag.
112                  */
113                 do {
114                         if (vf->pend_cmd == VIRTCHNL_OP_UNKNOWN)
115                                 break;
116                         rte_delay_ms(ASQ_DELAY_MS);
117                         /* If don't read msg or read sys event, continue */
118                 } while (i++ < MAX_TRY_TIMES);
119                 /* If there's no response is received, clear command */
120                 if (i >= MAX_TRY_TIMES  ||
121                     vf->cmd_retval != VIRTCHNL_STATUS_SUCCESS) {
122                         err = -1;
123                         PMD_DRV_LOG(ERR, "No response or return failure (%d)"
124                                     " for cmd %d", vf->cmd_retval, args->ops);
125                         _clear_cmd(vf);
126                 }
127                 break;
128         }
129
130         return err;
131 }
132
133 static void
134 iavf_handle_pf_event_msg(struct rte_eth_dev *dev, uint8_t *msg,
135                         uint16_t msglen)
136 {
137         struct virtchnl_pf_event *pf_msg =
138                         (struct virtchnl_pf_event *)msg;
139         struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(dev->data->dev_private);
140
141         if (msglen < sizeof(struct virtchnl_pf_event)) {
142                 PMD_DRV_LOG(DEBUG, "Error event");
143                 return;
144         }
145         switch (pf_msg->event) {
146         case VIRTCHNL_EVENT_RESET_IMPENDING:
147                 PMD_DRV_LOG(DEBUG, "VIRTCHNL_EVENT_RESET_IMPENDING event");
148                 _rte_eth_dev_callback_process(dev, RTE_ETH_EVENT_INTR_RESET,
149                                               NULL);
150                 break;
151         case VIRTCHNL_EVENT_LINK_CHANGE:
152                 PMD_DRV_LOG(DEBUG, "VIRTCHNL_EVENT_LINK_CHANGE event");
153                 vf->link_up = pf_msg->event_data.link_event.link_status;
154                 vf->link_speed = pf_msg->event_data.link_event_adv.link_speed;
155                 iavf_dev_link_update(dev, 0);
156                 _rte_eth_dev_callback_process(dev, RTE_ETH_EVENT_INTR_LSC,
157                                               NULL);
158                 break;
159         case VIRTCHNL_EVENT_PF_DRIVER_CLOSE:
160                 PMD_DRV_LOG(DEBUG, "VIRTCHNL_EVENT_PF_DRIVER_CLOSE event");
161                 break;
162         default:
163                 PMD_DRV_LOG(ERR, " unknown event received %u", pf_msg->event);
164                 break;
165         }
166 }
167
168 void
169 iavf_handle_virtchnl_msg(struct rte_eth_dev *dev)
170 {
171         struct iavf_hw *hw = IAVF_DEV_PRIVATE_TO_HW(dev->data->dev_private);
172         struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(dev->data->dev_private);
173         struct iavf_arq_event_info info;
174         uint16_t pending, aq_opc;
175         enum virtchnl_ops msg_opc;
176         enum iavf_status msg_ret;
177         int ret;
178
179         info.buf_len = IAVF_AQ_BUF_SZ;
180         if (!vf->aq_resp) {
181                 PMD_DRV_LOG(ERR, "Buffer for adminq resp should not be NULL");
182                 return;
183         }
184         info.msg_buf = vf->aq_resp;
185
186         pending = 1;
187         while (pending) {
188                 ret = iavf_clean_arq_element(hw, &info, &pending);
189
190                 if (ret != IAVF_SUCCESS) {
191                         PMD_DRV_LOG(INFO, "Failed to read msg from AdminQ,"
192                                     "ret: %d", ret);
193                         break;
194                 }
195                 aq_opc = rte_le_to_cpu_16(info.desc.opcode);
196                 /* For the message sent from pf to vf, opcode is stored in
197                  * cookie_high of struct iavf_aq_desc, while return error code
198                  * are stored in cookie_low, Which is done by PF driver.
199                  */
200                 msg_opc = (enum virtchnl_ops)rte_le_to_cpu_32(
201                                                   info.desc.cookie_high);
202                 msg_ret = (enum iavf_status)rte_le_to_cpu_32(
203                                                   info.desc.cookie_low);
204                 switch (aq_opc) {
205                 case iavf_aqc_opc_send_msg_to_vf:
206                         if (msg_opc == VIRTCHNL_OP_EVENT) {
207                                 iavf_handle_pf_event_msg(dev, info.msg_buf,
208                                                         info.msg_len);
209                         } else {
210                                 /* read message and it's expected one */
211                                 if (msg_opc == vf->pend_cmd)
212                                         _notify_cmd(vf, msg_ret);
213                                 else
214                                         PMD_DRV_LOG(ERR, "command mismatch,"
215                                                     "expect %u, get %u",
216                                                     vf->pend_cmd, msg_opc);
217                                 PMD_DRV_LOG(DEBUG,
218                                             "adminq response is received,"
219                                             " opcode = %d", msg_opc);
220                         }
221                         break;
222                 default:
223                         PMD_DRV_LOG(ERR, "Request %u is not supported yet",
224                                     aq_opc);
225                         break;
226                 }
227         }
228 }
229
230 int
231 iavf_enable_vlan_strip(struct iavf_adapter *adapter)
232 {
233         struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
234         struct iavf_cmd_info args;
235         int ret;
236
237         memset(&args, 0, sizeof(args));
238         args.ops = VIRTCHNL_OP_ENABLE_VLAN_STRIPPING;
239         args.in_args = NULL;
240         args.in_args_size = 0;
241         args.out_buffer = vf->aq_resp;
242         args.out_size = IAVF_AQ_BUF_SZ;
243         ret = iavf_execute_vf_cmd(adapter, &args);
244         if (ret)
245                 PMD_DRV_LOG(ERR, "Failed to execute command of"
246                             " OP_ENABLE_VLAN_STRIPPING");
247
248         return ret;
249 }
250
251 int
252 iavf_disable_vlan_strip(struct iavf_adapter *adapter)
253 {
254         struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
255         struct iavf_cmd_info args;
256         int ret;
257
258         memset(&args, 0, sizeof(args));
259         args.ops = VIRTCHNL_OP_DISABLE_VLAN_STRIPPING;
260         args.in_args = NULL;
261         args.in_args_size = 0;
262         args.out_buffer = vf->aq_resp;
263         args.out_size = IAVF_AQ_BUF_SZ;
264         ret = iavf_execute_vf_cmd(adapter, &args);
265         if (ret)
266                 PMD_DRV_LOG(ERR, "Failed to execute command of"
267                             " OP_DISABLE_VLAN_STRIPPING");
268
269         return ret;
270 }
271
272 #define VIRTCHNL_VERSION_MAJOR_START 1
273 #define VIRTCHNL_VERSION_MINOR_START 1
274
275 /* Check API version with sync wait until version read from admin queue */
276 int
277 iavf_check_api_version(struct iavf_adapter *adapter)
278 {
279         struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
280         struct virtchnl_version_info version, *pver;
281         struct iavf_cmd_info args;
282         int err;
283
284         version.major = VIRTCHNL_VERSION_MAJOR;
285         version.minor = VIRTCHNL_VERSION_MINOR;
286
287         args.ops = VIRTCHNL_OP_VERSION;
288         args.in_args = (uint8_t *)&version;
289         args.in_args_size = sizeof(version);
290         args.out_buffer = vf->aq_resp;
291         args.out_size = IAVF_AQ_BUF_SZ;
292
293         err = iavf_execute_vf_cmd(adapter, &args);
294         if (err) {
295                 PMD_INIT_LOG(ERR, "Fail to execute command of OP_VERSION");
296                 return err;
297         }
298
299         pver = (struct virtchnl_version_info *)args.out_buffer;
300         vf->virtchnl_version = *pver;
301
302         if (vf->virtchnl_version.major < VIRTCHNL_VERSION_MAJOR_START ||
303             (vf->virtchnl_version.major == VIRTCHNL_VERSION_MAJOR_START &&
304              vf->virtchnl_version.minor < VIRTCHNL_VERSION_MINOR_START)) {
305                 PMD_INIT_LOG(ERR, "VIRTCHNL API version should not be lower"
306                              " than (%u.%u) to support Adapative VF",
307                              VIRTCHNL_VERSION_MAJOR_START,
308                              VIRTCHNL_VERSION_MAJOR_START);
309                 return -1;
310         } else if (vf->virtchnl_version.major > VIRTCHNL_VERSION_MAJOR ||
311                    (vf->virtchnl_version.major == VIRTCHNL_VERSION_MAJOR &&
312                     vf->virtchnl_version.minor > VIRTCHNL_VERSION_MINOR)) {
313                 PMD_INIT_LOG(ERR, "PF/VF API version mismatch:(%u.%u)-(%u.%u)",
314                              vf->virtchnl_version.major,
315                              vf->virtchnl_version.minor,
316                              VIRTCHNL_VERSION_MAJOR,
317                              VIRTCHNL_VERSION_MINOR);
318                 return -1;
319         }
320
321         PMD_DRV_LOG(DEBUG, "Peer is supported PF host");
322         return 0;
323 }
324
325 int
326 iavf_get_vf_resource(struct iavf_adapter *adapter)
327 {
328         struct iavf_hw *hw = IAVF_DEV_PRIVATE_TO_HW(adapter);
329         struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
330         struct iavf_cmd_info args;
331         uint32_t caps, len;
332         int err, i;
333
334         args.ops = VIRTCHNL_OP_GET_VF_RESOURCES;
335         args.out_buffer = vf->aq_resp;
336         args.out_size = IAVF_AQ_BUF_SZ;
337
338         /* TODO: basic offload capabilities, need to
339          * add advanced/optional offload capabilities
340          */
341
342         caps = IAVF_BASIC_OFFLOAD_CAPS | VIRTCHNL_VF_CAP_ADV_LINK_SPEED |
343                 VIRTCHNL_VF_OFFLOAD_RX_FLEX_DESC;
344
345         args.in_args = (uint8_t *)&caps;
346         args.in_args_size = sizeof(caps);
347
348         err = iavf_execute_vf_cmd(adapter, &args);
349
350         if (err) {
351                 PMD_DRV_LOG(ERR,
352                             "Failed to execute command of OP_GET_VF_RESOURCE");
353                 return -1;
354         }
355
356         len =  sizeof(struct virtchnl_vf_resource) +
357                       IAVF_MAX_VF_VSI * sizeof(struct virtchnl_vsi_resource);
358
359         rte_memcpy(vf->vf_res, args.out_buffer,
360                    RTE_MIN(args.out_size, len));
361         /* parse  VF config message back from PF*/
362         iavf_vf_parse_hw_config(hw, vf->vf_res);
363         for (i = 0; i < vf->vf_res->num_vsis; i++) {
364                 if (vf->vf_res->vsi_res[i].vsi_type == VIRTCHNL_VSI_SRIOV)
365                         vf->vsi_res = &vf->vf_res->vsi_res[i];
366         }
367
368         if (!vf->vsi_res) {
369                 PMD_INIT_LOG(ERR, "no LAN VSI found");
370                 return -1;
371         }
372
373         vf->vsi.vsi_id = vf->vsi_res->vsi_id;
374         vf->vsi.nb_qps = vf->vsi_res->num_queue_pairs;
375         vf->vsi.adapter = adapter;
376
377         return 0;
378 }
379
380 int
381 iavf_get_supported_rxdid(struct iavf_adapter *adapter)
382 {
383         struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
384         struct iavf_cmd_info args;
385         int ret;
386
387         args.ops = VIRTCHNL_OP_GET_SUPPORTED_RXDIDS;
388         args.in_args = NULL;
389         args.in_args_size = 0;
390         args.out_buffer = vf->aq_resp;
391         args.out_size = IAVF_AQ_BUF_SZ;
392
393         ret = iavf_execute_vf_cmd(adapter, &args);
394         if (ret) {
395                 PMD_DRV_LOG(ERR,
396                             "Failed to execute command of OP_GET_SUPPORTED_RXDIDS");
397                 return ret;
398         }
399
400         vf->supported_rxdid =
401                 ((struct virtchnl_supported_rxdids *)args.out_buffer)->supported_rxdids;
402
403         return 0;
404 }
405
406 int
407 iavf_enable_queues(struct iavf_adapter *adapter)
408 {
409         struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
410         struct virtchnl_queue_select queue_select;
411         struct iavf_cmd_info args;
412         int err;
413
414         memset(&queue_select, 0, sizeof(queue_select));
415         queue_select.vsi_id = vf->vsi_res->vsi_id;
416
417         queue_select.rx_queues = BIT(adapter->eth_dev->data->nb_rx_queues) - 1;
418         queue_select.tx_queues = BIT(adapter->eth_dev->data->nb_tx_queues) - 1;
419
420         args.ops = VIRTCHNL_OP_ENABLE_QUEUES;
421         args.in_args = (u8 *)&queue_select;
422         args.in_args_size = sizeof(queue_select);
423         args.out_buffer = vf->aq_resp;
424         args.out_size = IAVF_AQ_BUF_SZ;
425         err = iavf_execute_vf_cmd(adapter, &args);
426         if (err) {
427                 PMD_DRV_LOG(ERR,
428                             "Failed to execute command of OP_ENABLE_QUEUES");
429                 return err;
430         }
431         return 0;
432 }
433
434 int
435 iavf_disable_queues(struct iavf_adapter *adapter)
436 {
437         struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
438         struct virtchnl_queue_select queue_select;
439         struct iavf_cmd_info args;
440         int err;
441
442         memset(&queue_select, 0, sizeof(queue_select));
443         queue_select.vsi_id = vf->vsi_res->vsi_id;
444
445         queue_select.rx_queues = BIT(adapter->eth_dev->data->nb_rx_queues) - 1;
446         queue_select.tx_queues = BIT(adapter->eth_dev->data->nb_tx_queues) - 1;
447
448         args.ops = VIRTCHNL_OP_DISABLE_QUEUES;
449         args.in_args = (u8 *)&queue_select;
450         args.in_args_size = sizeof(queue_select);
451         args.out_buffer = vf->aq_resp;
452         args.out_size = IAVF_AQ_BUF_SZ;
453         err = iavf_execute_vf_cmd(adapter, &args);
454         if (err) {
455                 PMD_DRV_LOG(ERR,
456                             "Failed to execute command of OP_DISABLE_QUEUES");
457                 return err;
458         }
459         return 0;
460 }
461
462 int
463 iavf_switch_queue(struct iavf_adapter *adapter, uint16_t qid,
464                  bool rx, bool on)
465 {
466         struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
467         struct virtchnl_queue_select queue_select;
468         struct iavf_cmd_info args;
469         int err;
470
471         memset(&queue_select, 0, sizeof(queue_select));
472         queue_select.vsi_id = vf->vsi_res->vsi_id;
473         if (rx)
474                 queue_select.rx_queues |= 1 << qid;
475         else
476                 queue_select.tx_queues |= 1 << qid;
477
478         if (on)
479                 args.ops = VIRTCHNL_OP_ENABLE_QUEUES;
480         else
481                 args.ops = VIRTCHNL_OP_DISABLE_QUEUES;
482         args.in_args = (u8 *)&queue_select;
483         args.in_args_size = sizeof(queue_select);
484         args.out_buffer = vf->aq_resp;
485         args.out_size = IAVF_AQ_BUF_SZ;
486         err = iavf_execute_vf_cmd(adapter, &args);
487         if (err)
488                 PMD_DRV_LOG(ERR, "Failed to execute command of %s",
489                             on ? "OP_ENABLE_QUEUES" : "OP_DISABLE_QUEUES");
490         return err;
491 }
492
493 int
494 iavf_configure_rss_lut(struct iavf_adapter *adapter)
495 {
496         struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
497         struct virtchnl_rss_lut *rss_lut;
498         struct iavf_cmd_info args;
499         int len, err = 0;
500
501         len = sizeof(*rss_lut) + vf->vf_res->rss_lut_size - 1;
502         rss_lut = rte_zmalloc("rss_lut", len, 0);
503         if (!rss_lut)
504                 return -ENOMEM;
505
506         rss_lut->vsi_id = vf->vsi_res->vsi_id;
507         rss_lut->lut_entries = vf->vf_res->rss_lut_size;
508         rte_memcpy(rss_lut->lut, vf->rss_lut, vf->vf_res->rss_lut_size);
509
510         args.ops = VIRTCHNL_OP_CONFIG_RSS_LUT;
511         args.in_args = (u8 *)rss_lut;
512         args.in_args_size = len;
513         args.out_buffer = vf->aq_resp;
514         args.out_size = IAVF_AQ_BUF_SZ;
515
516         err = iavf_execute_vf_cmd(adapter, &args);
517         if (err)
518                 PMD_DRV_LOG(ERR,
519                             "Failed to execute command of OP_CONFIG_RSS_LUT");
520
521         rte_free(rss_lut);
522         return err;
523 }
524
525 int
526 iavf_configure_rss_key(struct iavf_adapter *adapter)
527 {
528         struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
529         struct virtchnl_rss_key *rss_key;
530         struct iavf_cmd_info args;
531         int len, err = 0;
532
533         len = sizeof(*rss_key) + vf->vf_res->rss_key_size - 1;
534         rss_key = rte_zmalloc("rss_key", len, 0);
535         if (!rss_key)
536                 return -ENOMEM;
537
538         rss_key->vsi_id = vf->vsi_res->vsi_id;
539         rss_key->key_len = vf->vf_res->rss_key_size;
540         rte_memcpy(rss_key->key, vf->rss_key, vf->vf_res->rss_key_size);
541
542         args.ops = VIRTCHNL_OP_CONFIG_RSS_KEY;
543         args.in_args = (u8 *)rss_key;
544         args.in_args_size = len;
545         args.out_buffer = vf->aq_resp;
546         args.out_size = IAVF_AQ_BUF_SZ;
547
548         err = iavf_execute_vf_cmd(adapter, &args);
549         if (err)
550                 PMD_DRV_LOG(ERR,
551                             "Failed to execute command of OP_CONFIG_RSS_KEY");
552
553         rte_free(rss_key);
554         return err;
555 }
556
557 int
558 iavf_configure_queues(struct iavf_adapter *adapter)
559 {
560         struct iavf_rx_queue **rxq =
561                 (struct iavf_rx_queue **)adapter->eth_dev->data->rx_queues;
562         struct iavf_tx_queue **txq =
563                 (struct iavf_tx_queue **)adapter->eth_dev->data->tx_queues;
564         struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
565         struct virtchnl_vsi_queue_config_info *vc_config;
566         struct virtchnl_queue_pair_info *vc_qp;
567         struct iavf_cmd_info args;
568         uint16_t i, size;
569         int err;
570
571         size = sizeof(*vc_config) +
572                sizeof(vc_config->qpair[0]) * vf->num_queue_pairs;
573         vc_config = rte_zmalloc("cfg_queue", size, 0);
574         if (!vc_config)
575                 return -ENOMEM;
576
577         vc_config->vsi_id = vf->vsi_res->vsi_id;
578         vc_config->num_queue_pairs = vf->num_queue_pairs;
579
580         for (i = 0, vc_qp = vc_config->qpair;
581              i < vf->num_queue_pairs;
582              i++, vc_qp++) {
583                 vc_qp->txq.vsi_id = vf->vsi_res->vsi_id;
584                 vc_qp->txq.queue_id = i;
585                 /* Virtchnnl configure queues by pairs */
586                 if (i < adapter->eth_dev->data->nb_tx_queues) {
587                         vc_qp->txq.ring_len = txq[i]->nb_tx_desc;
588                         vc_qp->txq.dma_ring_addr = txq[i]->tx_ring_phys_addr;
589                 }
590                 vc_qp->rxq.vsi_id = vf->vsi_res->vsi_id;
591                 vc_qp->rxq.queue_id = i;
592                 vc_qp->rxq.max_pkt_size = vf->max_pkt_len;
593                 /* Virtchnnl configure queues by pairs */
594                 if (i < adapter->eth_dev->data->nb_rx_queues) {
595                         vc_qp->rxq.ring_len = rxq[i]->nb_rx_desc;
596                         vc_qp->rxq.dma_ring_addr = rxq[i]->rx_ring_phys_addr;
597                         vc_qp->rxq.databuffer_size = rxq[i]->rx_buf_len;
598
599 #ifndef RTE_LIBRTE_IAVF_16BYTE_RX_DESC
600                         if (vf->vf_res->vf_cap_flags &
601                             VIRTCHNL_VF_OFFLOAD_RX_FLEX_DESC &&
602                             vf->supported_rxdid & BIT(IAVF_RXDID_COMMS_OVS_1)) {
603                                 vc_qp->rxq.rxdid = IAVF_RXDID_COMMS_OVS_1;
604                                 PMD_DRV_LOG(NOTICE, "request RXDID == %d in "
605                                             "Queue[%d]", vc_qp->rxq.rxdid, i);
606                         } else {
607                                 vc_qp->rxq.rxdid = IAVF_RXDID_LEGACY_1;
608                                 PMD_DRV_LOG(NOTICE, "request RXDID == %d in "
609                                             "Queue[%d]", vc_qp->rxq.rxdid, i);
610                         }
611 #else
612                         if (vf->vf_res->vf_cap_flags &
613                             VIRTCHNL_VF_OFFLOAD_RX_FLEX_DESC &&
614                             vf->supported_rxdid & BIT(IAVF_RXDID_LEGACY_0)) {
615                                 vc_qp->rxq.rxdid = IAVF_RXDID_LEGACY_0;
616                                 PMD_DRV_LOG(NOTICE, "request RXDID == %d in "
617                                             "Queue[%d]", vc_qp->rxq.rxdid, i);
618                         } else {
619                                 PMD_DRV_LOG(ERR, "RXDID == 0 is not supported");
620                                 return -1;
621                         }
622 #endif
623                 }
624         }
625
626         memset(&args, 0, sizeof(args));
627         args.ops = VIRTCHNL_OP_CONFIG_VSI_QUEUES;
628         args.in_args = (uint8_t *)vc_config;
629         args.in_args_size = size;
630         args.out_buffer = vf->aq_resp;
631         args.out_size = IAVF_AQ_BUF_SZ;
632
633         err = iavf_execute_vf_cmd(adapter, &args);
634         if (err)
635                 PMD_DRV_LOG(ERR, "Failed to execute command of"
636                             " VIRTCHNL_OP_CONFIG_VSI_QUEUES");
637
638         rte_free(vc_config);
639         return err;
640 }
641
642 int
643 iavf_config_irq_map(struct iavf_adapter *adapter)
644 {
645         struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
646         struct virtchnl_irq_map_info *map_info;
647         struct virtchnl_vector_map *vecmap;
648         struct iavf_cmd_info args;
649         int len, i, err;
650
651         len = sizeof(struct virtchnl_irq_map_info) +
652               sizeof(struct virtchnl_vector_map) * vf->nb_msix;
653
654         map_info = rte_zmalloc("map_info", len, 0);
655         if (!map_info)
656                 return -ENOMEM;
657
658         map_info->num_vectors = vf->nb_msix;
659         for (i = 0; i < vf->nb_msix; i++) {
660                 vecmap = &map_info->vecmap[i];
661                 vecmap->vsi_id = vf->vsi_res->vsi_id;
662                 vecmap->rxitr_idx = IAVF_ITR_INDEX_DEFAULT;
663                 vecmap->vector_id = vf->msix_base + i;
664                 vecmap->txq_map = 0;
665                 vecmap->rxq_map = vf->rxq_map[vf->msix_base + i];
666         }
667
668         args.ops = VIRTCHNL_OP_CONFIG_IRQ_MAP;
669         args.in_args = (u8 *)map_info;
670         args.in_args_size = len;
671         args.out_buffer = vf->aq_resp;
672         args.out_size = IAVF_AQ_BUF_SZ;
673         err = iavf_execute_vf_cmd(adapter, &args);
674         if (err)
675                 PMD_DRV_LOG(ERR, "fail to execute command OP_CONFIG_IRQ_MAP");
676
677         rte_free(map_info);
678         return err;
679 }
680
681 void
682 iavf_add_del_all_mac_addr(struct iavf_adapter *adapter, bool add)
683 {
684         struct virtchnl_ether_addr_list *list;
685         struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
686         struct rte_ether_addr *addr;
687         struct iavf_cmd_info args;
688         int len, err, i, j;
689         int next_begin = 0;
690         int begin = 0;
691
692         do {
693                 j = 0;
694                 len = sizeof(struct virtchnl_ether_addr_list);
695                 for (i = begin; i < IAVF_NUM_MACADDR_MAX; i++, next_begin++) {
696                         addr = &adapter->eth_dev->data->mac_addrs[i];
697                         if (rte_is_zero_ether_addr(addr))
698                                 continue;
699                         len += sizeof(struct virtchnl_ether_addr);
700                         if (len >= IAVF_AQ_BUF_SZ) {
701                                 next_begin = i + 1;
702                                 break;
703                         }
704                 }
705
706                 list = rte_zmalloc("iavf_del_mac_buffer", len, 0);
707                 if (!list) {
708                         PMD_DRV_LOG(ERR, "fail to allocate memory");
709                         return;
710                 }
711
712                 for (i = begin; i < next_begin; i++) {
713                         addr = &adapter->eth_dev->data->mac_addrs[i];
714                         if (rte_is_zero_ether_addr(addr))
715                                 continue;
716                         rte_memcpy(list->list[j].addr, addr->addr_bytes,
717                                    sizeof(addr->addr_bytes));
718                         PMD_DRV_LOG(DEBUG, "add/rm mac:%x:%x:%x:%x:%x:%x",
719                                     addr->addr_bytes[0], addr->addr_bytes[1],
720                                     addr->addr_bytes[2], addr->addr_bytes[3],
721                                     addr->addr_bytes[4], addr->addr_bytes[5]);
722                         j++;
723                 }
724                 list->vsi_id = vf->vsi_res->vsi_id;
725                 list->num_elements = j;
726                 args.ops = add ? VIRTCHNL_OP_ADD_ETH_ADDR :
727                            VIRTCHNL_OP_DEL_ETH_ADDR;
728                 args.in_args = (uint8_t *)list;
729                 args.in_args_size = len;
730                 args.out_buffer = vf->aq_resp;
731                 args.out_size = IAVF_AQ_BUF_SZ;
732                 err = iavf_execute_vf_cmd(adapter, &args);
733                 if (err)
734                         PMD_DRV_LOG(ERR, "fail to execute command %s",
735                                     add ? "OP_ADD_ETHER_ADDRESS" :
736                                     "OP_DEL_ETHER_ADDRESS");
737                 rte_free(list);
738                 begin = next_begin;
739         } while (begin < IAVF_NUM_MACADDR_MAX);
740 }
741
742 int
743 iavf_query_stats(struct iavf_adapter *adapter,
744                 struct virtchnl_eth_stats **pstats)
745 {
746         struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
747         struct virtchnl_queue_select q_stats;
748         struct iavf_cmd_info args;
749         int err;
750
751         memset(&q_stats, 0, sizeof(q_stats));
752         q_stats.vsi_id = vf->vsi_res->vsi_id;
753         args.ops = VIRTCHNL_OP_GET_STATS;
754         args.in_args = (uint8_t *)&q_stats;
755         args.in_args_size = sizeof(q_stats);
756         args.out_buffer = vf->aq_resp;
757         args.out_size = IAVF_AQ_BUF_SZ;
758
759         err = iavf_execute_vf_cmd(adapter, &args);
760         if (err) {
761                 PMD_DRV_LOG(ERR, "fail to execute command OP_GET_STATS");
762                 *pstats = NULL;
763                 return err;
764         }
765         *pstats = (struct virtchnl_eth_stats *)args.out_buffer;
766         return 0;
767 }
768
769 int
770 iavf_config_promisc(struct iavf_adapter *adapter,
771                    bool enable_unicast,
772                    bool enable_multicast)
773 {
774         struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
775         struct virtchnl_promisc_info promisc;
776         struct iavf_cmd_info args;
777         int err;
778
779         promisc.flags = 0;
780         promisc.vsi_id = vf->vsi_res->vsi_id;
781
782         if (enable_unicast)
783                 promisc.flags |= FLAG_VF_UNICAST_PROMISC;
784
785         if (enable_multicast)
786                 promisc.flags |= FLAG_VF_MULTICAST_PROMISC;
787
788         args.ops = VIRTCHNL_OP_CONFIG_PROMISCUOUS_MODE;
789         args.in_args = (uint8_t *)&promisc;
790         args.in_args_size = sizeof(promisc);
791         args.out_buffer = vf->aq_resp;
792         args.out_size = IAVF_AQ_BUF_SZ;
793
794         err = iavf_execute_vf_cmd(adapter, &args);
795
796         if (err)
797                 PMD_DRV_LOG(ERR,
798                             "fail to execute command CONFIG_PROMISCUOUS_MODE");
799         return err;
800 }
801
802 int
803 iavf_add_del_eth_addr(struct iavf_adapter *adapter, struct rte_ether_addr *addr,
804                      bool add)
805 {
806         struct virtchnl_ether_addr_list *list;
807         struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
808         uint8_t cmd_buffer[sizeof(struct virtchnl_ether_addr_list) +
809                            sizeof(struct virtchnl_ether_addr)];
810         struct iavf_cmd_info args;
811         int err;
812
813         list = (struct virtchnl_ether_addr_list *)cmd_buffer;
814         list->vsi_id = vf->vsi_res->vsi_id;
815         list->num_elements = 1;
816         rte_memcpy(list->list[0].addr, addr->addr_bytes,
817                    sizeof(addr->addr_bytes));
818
819         args.ops = add ? VIRTCHNL_OP_ADD_ETH_ADDR : VIRTCHNL_OP_DEL_ETH_ADDR;
820         args.in_args = cmd_buffer;
821         args.in_args_size = sizeof(cmd_buffer);
822         args.out_buffer = vf->aq_resp;
823         args.out_size = IAVF_AQ_BUF_SZ;
824         err = iavf_execute_vf_cmd(adapter, &args);
825         if (err)
826                 PMD_DRV_LOG(ERR, "fail to execute command %s",
827                             add ? "OP_ADD_ETH_ADDR" :  "OP_DEL_ETH_ADDR");
828         return err;
829 }
830
831 int
832 iavf_add_del_vlan(struct iavf_adapter *adapter, uint16_t vlanid, bool add)
833 {
834         struct virtchnl_vlan_filter_list *vlan_list;
835         struct iavf_info *vf = IAVF_DEV_PRIVATE_TO_VF(adapter);
836         uint8_t cmd_buffer[sizeof(struct virtchnl_vlan_filter_list) +
837                                                         sizeof(uint16_t)];
838         struct iavf_cmd_info args;
839         int err;
840
841         vlan_list = (struct virtchnl_vlan_filter_list *)cmd_buffer;
842         vlan_list->vsi_id = vf->vsi_res->vsi_id;
843         vlan_list->num_elements = 1;
844         vlan_list->vlan_id[0] = vlanid;
845
846         args.ops = add ? VIRTCHNL_OP_ADD_VLAN : VIRTCHNL_OP_DEL_VLAN;
847         args.in_args = cmd_buffer;
848         args.in_args_size = sizeof(cmd_buffer);
849         args.out_buffer = vf->aq_resp;
850         args.out_size = IAVF_AQ_BUF_SZ;
851         err = iavf_execute_vf_cmd(adapter, &args);
852         if (err)
853                 PMD_DRV_LOG(ERR, "fail to execute command %s",
854                             add ? "OP_ADD_VLAN" :  "OP_DEL_VLAN");
855
856         return err;
857 }