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