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