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