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