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