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