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