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