DPDK logo

Elixir Cross Referencer

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
/* SPDX-License-Identifier: BSD-3-Clause
 * Copyright 2008-2019 Cisco Systems, Inc.  All rights reserved.
 */

#include <stdint.h>
#include <stdio.h>

#include <rte_bus_pci.h>
#include <rte_common.h>
#include <rte_dev.h>
#include <ethdev_driver.h>
#include <ethdev_pci.h>
#include <rte_flow_driver.h>
#include <rte_kvargs.h>
#include <rte_pci.h>
#include <rte_string_fns.h>

#include "enic_compat.h"
#include "enic.h"
#include "vnic_dev.h"
#include "vnic_enet.h"
#include "vnic_intr.h"
#include "vnic_cq.h"
#include "vnic_wq.h"
#include "vnic_rq.h"

static uint16_t enic_vf_recv_pkts(void *rx_queue,
				  struct rte_mbuf **rx_pkts,
				  uint16_t nb_pkts)
{
	return enic_recv_pkts(rx_queue, rx_pkts, nb_pkts);
}

static uint16_t enic_vf_xmit_pkts(void *tx_queue,
				  struct rte_mbuf **tx_pkts,
				  uint16_t nb_pkts)
{
	return enic_xmit_pkts(tx_queue, tx_pkts, nb_pkts);
}

static int enic_vf_dev_tx_queue_setup(struct rte_eth_dev *eth_dev,
	uint16_t queue_idx,
	uint16_t nb_desc,
	unsigned int socket_id,
	const struct rte_eth_txconf *tx_conf)
{
	struct enic_vf_representor *vf;
	struct vnic_wq *wq;
	struct enic *pf;
	int err;

	ENICPMD_FUNC_TRACE();
	if (rte_eal_process_type() != RTE_PROC_PRIMARY)
		return -E_RTE_SECONDARY;
	/* Only one queue now */
	if (queue_idx != 0)
		return -EINVAL;
	vf = eth_dev->data->dev_private;
	pf = vf->pf;
	wq = &pf->wq[vf->pf_wq_idx];
	wq->offloads = tx_conf->offloads |
		eth_dev->data->dev_conf.txmode.offloads;
	eth_dev->data->tx_queues[0] = (void *)wq;
	/* Pass vf not pf because of cq index calculation. See enic_alloc_wq */
	err = enic_alloc_wq(&vf->enic, queue_idx, socket_id, nb_desc);
	if (err) {
		ENICPMD_LOG(ERR, "error in allocating wq\n");
		return err;
	}
	return 0;
}

static void enic_vf_dev_tx_queue_release(void *txq)
{
	ENICPMD_FUNC_TRACE();
	if (rte_eal_process_type() != RTE_PROC_PRIMARY)
		return;
	enic_free_wq(txq);
}

static int enic_vf_dev_rx_queue_setup(struct rte_eth_dev *eth_dev,
	uint16_t queue_idx,
	uint16_t nb_desc,
	unsigned int socket_id,
	const struct rte_eth_rxconf *rx_conf,
	struct rte_mempool *mp)
{
	struct enic_vf_representor *vf;
	struct enic *pf;
	int ret;

	ENICPMD_FUNC_TRACE();
	if (rte_eal_process_type() != RTE_PROC_PRIMARY)
		return -E_RTE_SECONDARY;
	/* Only 1 queue now */
	if (queue_idx != 0)
		return -EINVAL;
	vf = eth_dev->data->dev_private;
	pf = vf->pf;
	eth_dev->data->rx_queues[queue_idx] =
		(void *)&pf->rq[vf->pf_rq_sop_idx];
	ret = enic_alloc_rq(&vf->enic, queue_idx, socket_id, mp, nb_desc,
			    rx_conf->rx_free_thresh);
	if (ret) {
		ENICPMD_LOG(ERR, "error in allocating rq\n");
		return ret;
	}
	return 0;
}

static void enic_vf_dev_rx_queue_release(void *rxq)
{
	ENICPMD_FUNC_TRACE();
	if (rte_eal_process_type() != RTE_PROC_PRIMARY)
		return;
	enic_free_rq(rxq);
}

static int enic_vf_dev_configure(struct rte_eth_dev *eth_dev __rte_unused)
{
	ENICPMD_FUNC_TRACE();
	if (rte_eal_process_type() != RTE_PROC_PRIMARY)
		return -E_RTE_SECONDARY;
	return 0;
}

static int
setup_rep_vf_fwd(struct enic_vf_representor *vf)
{
	int ret;

	ENICPMD_FUNC_TRACE();
	/* Representor -> VF rule
	 * Egress packets from this representor are on the representor's WQ.
	 * So, loop back that WQ to VF.
	 */
	ret = enic_fm_add_rep2vf_flow(vf);
	if (ret) {
		ENICPMD_LOG(ERR, "Cannot create representor->VF flow");
		return ret;
	}
	/* VF -> representor rule
	 * Packets from VF loop back to the representor, unless they match
	 * user-added flows.
	 */
	ret = enic_fm_add_vf2rep_flow(vf);
	if (ret) {
		ENICPMD_LOG(ERR, "Cannot create VF->representor flow");
		return ret;
	}
	return 0;
}

static int enic_vf_dev_start(struct rte_eth_dev *eth_dev)
{
	struct enic_vf_representor *vf;
	struct vnic_rq *data_rq;
	int index, cq_idx;
	struct enic *pf;
	int ret;

	ENICPMD_FUNC_TRACE();
	if (rte_eal_process_type() != RTE_PROC_PRIMARY)
		return -E_RTE_SECONDARY;

	vf = eth_dev->data->dev_private;
	pf = vf->pf;
	/* Get representor flowman for flow API and representor path */
	ret = enic_fm_init(&vf->enic);
	if (ret)
		return ret;
	/* Set up implicit flow rules to forward between representor and VF */
	ret = setup_rep_vf_fwd(vf);
	if (ret) {
		ENICPMD_LOG(ERR, "Cannot set up representor-VF flows");
		return ret;
	}
	/* Remove all packet filters so no ingress packets go to VF.
	 * When PF enables switchdev, it will ensure packet filters
	 * are removed.  So, this is not technically needed.
	 */
	ENICPMD_LOG(DEBUG, "Clear packet filters");
	ret = vnic_dev_packet_filter(vf->enic.vdev, 0, 0, 0, 0, 0);
	if (ret) {
		ENICPMD_LOG(ERR, "Cannot clear packet filters");
		return ret;
	}

	/* Start WQ: see enic_init_vnic_resources */
	index = vf->pf_wq_idx;
	cq_idx = vf->pf_wq_cq_idx;
	vnic_wq_init(&pf->wq[index], cq_idx, 1, 0);
	vnic_cq_init(&pf->cq[cq_idx],
		     0 /* flow_control_enable */,
		     1 /* color_enable */,
		     0 /* cq_head */,
		     0 /* cq_tail */,
		     1 /* cq_tail_color */,
		     0 /* interrupt_enable */,
		     0 /* cq_entry_enable */,
		     1 /* cq_message_enable */,
		     0 /* interrupt offset */,
		     (uint64_t)pf->wq[index].cqmsg_rz->iova);
	/* enic_start_wq */
	vnic_wq_enable(&pf->wq[index]);
	eth_dev->data->tx_queue_state[0] = RTE_ETH_QUEUE_STATE_STARTED;

	/* Start RQ: see enic_init_vnic_resources */
	index = vf->pf_rq_sop_idx;
	cq_idx = enic_cq_rq(vf->pf, index);
	vnic_rq_init(&pf->rq[index], cq_idx, 1, 0);
	data_rq = &pf->rq[vf->pf_rq_data_idx];
	if (data_rq->in_use)
		vnic_rq_init(data_rq, cq_idx, 1, 0);
	vnic_cq_init(&pf->cq[cq_idx],
		     0 /* flow_control_enable */,
		     1 /* color_enable */,
		     0 /* cq_head */,
		     0 /* cq_tail */,
		     1 /* cq_tail_color */,
		     0,
		     1 /* cq_entry_enable */,
		     0 /* cq_message_enable */,
		     0,
		     0 /* cq_message_addr */);
	/* enic_enable */
	ret = enic_alloc_rx_queue_mbufs(pf, &pf->rq[index]);
	if (ret) {
		ENICPMD_LOG(ERR, "Failed to alloc sop RX queue mbufs\n");
		return ret;
	}
	ret = enic_alloc_rx_queue_mbufs(pf, data_rq);
	if (ret) {
		/* Release the allocated mbufs for the sop rq*/
		enic_rxmbuf_queue_release(pf, &pf->rq[index]);
		ENICPMD_LOG(ERR, "Failed to alloc data RX queue mbufs\n");
		return ret;
	}
	enic_start_rq(pf, vf->pf_rq_sop_idx);
	eth_dev->data->tx_queue_state[0] = RTE_ETH_QUEUE_STATE_STARTED;
	eth_dev->data->rx_queue_state[0] = RTE_ETH_QUEUE_STATE_STARTED;
	return 0;
}

static int enic_vf_dev_stop(struct rte_eth_dev *eth_dev)
{
	struct enic_vf_representor *vf;
	struct vnic_rq *rq;
	struct enic *pf;

	ENICPMD_FUNC_TRACE();
	if (rte_eal_process_type() != RTE_PROC_PRIMARY)
		return 0;
	/* Undo dev_start. Disable/clean WQ */
	vf = eth_dev->data->dev_private;
	pf = vf->pf;
	vnic_wq_disable(&pf->wq[vf->pf_wq_idx]);
	vnic_wq_clean(&pf->wq[vf->pf_wq_idx], enic_free_wq_buf);
	vnic_cq_clean(&pf->cq[vf->pf_wq_cq_idx]);
	/* Disable/clean RQ */
	rq = &pf->rq[vf->pf_rq_sop_idx];
	vnic_rq_disable(rq);
	vnic_rq_clean(rq, enic_free_rq_buf);
	rq = &pf->rq[vf->pf_rq_data_idx];
	if (rq->in_use) {
		vnic_rq_disable(rq);
		vnic_rq_clean(rq, enic_free_rq_buf);
	}
	vnic_cq_clean(&pf->cq[enic_cq_rq(vf->pf, vf->pf_rq_sop_idx)]);
	eth_dev->data->tx_queue_state[0] = RTE_ETH_QUEUE_STATE_STOPPED;
	eth_dev->data->rx_queue_state[0] = RTE_ETH_QUEUE_STATE_STOPPED;
	/* Clean up representor flowman */
	enic_fm_destroy(&vf->enic);

	return 0;
}

/*
 * "close" is no-op for now and solely exists so that rte_eth_dev_close()
 * can finish its own cleanup without errors.
 */
static int enic_vf_dev_close(struct rte_eth_dev *eth_dev __rte_unused)
{
	ENICPMD_FUNC_TRACE();
	if (rte_eal_process_type() != RTE_PROC_PRIMARY)
		return 0;
	return 0;
}

static int
adjust_flow_attr(const struct rte_flow_attr *attrs,
		 struct rte_flow_attr *vf_attrs,
		 struct rte_flow_error *error)
{
	if (!attrs) {
		return rte_flow_error_set(error, EINVAL,
				RTE_FLOW_ERROR_TYPE_ATTR,
				NULL, "no attribute specified");
	}
	/*
	 * Swap ingress and egress as the firmware view of direction
	 * is the opposite of the representor.
	 */
	*vf_attrs = *attrs;
	if (attrs->ingress && !attrs->egress) {
		vf_attrs->ingress = 0;
		vf_attrs->egress = 1;
		return 0;
	}
	return rte_flow_error_set(error, ENOTSUP,
			RTE_FLOW_ERROR_TYPE_ATTR_EGRESS, NULL,
			"representor only supports ingress");
}

static int
enic_vf_flow_validate(struct rte_eth_dev *dev,
		      const struct rte_flow_attr *attrs,
		      const struct rte_flow_item pattern[],
		      const struct rte_flow_action actions[],
		      struct rte_flow_error *error)
{
	struct rte_flow_attr vf_attrs;
	int ret;

	ret = adjust_flow_attr(attrs, &vf_attrs, error);
	if (ret)
		return ret;
	attrs = &vf_attrs;
	return enic_fm_flow_ops.validate(dev, attrs, pattern, actions, error);
}

static struct rte_flow *
enic_vf_flow_create(struct rte_eth_dev *dev,
		    const struct rte_flow_attr *attrs,
		    const struct rte_flow_item pattern[],
		    const struct rte_flow_action actions[],
		    struct rte_flow_error *error)
{
	struct rte_flow_attr vf_attrs;

	if (adjust_flow_attr(attrs, &vf_attrs, error))
		return NULL;
	attrs = &vf_attrs;
	return enic_fm_flow_ops.create(dev, attrs, pattern, actions, error);
}

static int
enic_vf_flow_destroy(struct rte_eth_dev *dev, struct rte_flow *flow,
		     struct rte_flow_error *error)
{
	return enic_fm_flow_ops.destroy(dev, flow, error);
}

static int
enic_vf_flow_query(struct rte_eth_dev *dev,
		   struct rte_flow *flow,
		   const struct rte_flow_action *actions,
		   void *data,
		   struct rte_flow_error *error)
{
	return enic_fm_flow_ops.query(dev, flow, actions, data, error);
}

static int
enic_vf_flow_flush(struct rte_eth_dev *dev,
		   struct rte_flow_error *error)
{
	return enic_fm_flow_ops.flush(dev, error);
}

static const struct rte_flow_ops enic_vf_flow_ops = {
	.validate = enic_vf_flow_validate,
	.create = enic_vf_flow_create,
	.destroy = enic_vf_flow_destroy,
	.flush = enic_vf_flow_flush,
	.query = enic_vf_flow_query,
};

static int
enic_vf_filter_ctrl(struct rte_eth_dev *eth_dev,
		    enum rte_filter_type filter_type,
		    enum rte_filter_op filter_op,
		    void *arg)
{
	struct enic_vf_representor *vf;
	int ret = 0;

	ENICPMD_FUNC_TRACE();
	vf = eth_dev->data->dev_private;
	switch (filter_type) {
	case RTE_ETH_FILTER_GENERIC:
		if (filter_op != RTE_ETH_FILTER_GET)
			return -EINVAL;
		if (vf->enic.flow_filter_mode == FILTER_FLOWMAN) {
			*(const void **)arg = &enic_vf_flow_ops;
		} else {
			ENICPMD_LOG(WARNING, "VF representors require flowman support for rte_flow API");
			ret = -EINVAL;
		}
		break;
	default:
		ENICPMD_LOG(WARNING, "Filter type (%d) not supported",
			    filter_type);
		ret = -EINVAL;
		break;
	}
	return ret;
}

static int enic_vf_link_update(struct rte_eth_dev *eth_dev,
	int wait_to_complete __rte_unused)
{
	struct enic_vf_representor *vf;
	struct rte_eth_link link;
	struct enic *pf;

	ENICPMD_FUNC_TRACE();
	vf = eth_dev->data->dev_private;
	pf = vf->pf;
	/*
	 * Link status and speed are same as PF. Update PF status and then
	 * copy it to VF.
	 */
	enic_link_update(pf->rte_dev);
	rte_eth_linkstatus_get(pf->rte_dev, &link);
	rte_eth_linkstatus_set(eth_dev, &link);
	return 0;
}

static int enic_vf_stats_get(struct rte_eth_dev *eth_dev,
	struct rte_eth_stats *stats)
{
	struct enic_vf_representor *vf;
	struct vnic_stats *vs;
	int err;

	ENICPMD_FUNC_TRACE();
	vf = eth_dev->data->dev_private;
	/* Get VF stats via PF */
	err = vnic_dev_stats_dump(vf->enic.vdev, &vs);
	if (err) {
		ENICPMD_LOG(ERR, "error in getting stats\n");
		return err;
	}
	stats->ipackets = vs->rx.rx_frames_ok;
	stats->opackets = vs->tx.tx_frames_ok;
	stats->ibytes = vs->rx.rx_bytes_ok;
	stats->obytes = vs->tx.tx_bytes_ok;
	stats->ierrors = vs->rx.rx_errors + vs->rx.rx_drop;
	stats->oerrors = vs->tx.tx_errors;
	stats->imissed = vs->rx.rx_no_bufs;
	return 0;
}

static int enic_vf_stats_reset(struct rte_eth_dev *eth_dev)
{
	struct enic_vf_representor *vf;
	int err;

	ENICPMD_FUNC_TRACE();
	vf = eth_dev->data->dev_private;
	/* Ask PF to clear VF stats */
	err = vnic_dev_stats_clear(vf->enic.vdev);
	if (err)
		ENICPMD_LOG(ERR, "error in clearing stats\n");
	return err;
}

static int enic_vf_dev_infos_get(struct rte_eth_dev *eth_dev,
	struct rte_eth_dev_info *device_info)
{
	struct enic_vf_representor *vf;
	struct enic *pf;

	ENICPMD_FUNC_TRACE();
	vf = eth_dev->data->dev_private;
	pf = vf->pf;
	device_info->max_rx_queues = eth_dev->data->nb_rx_queues;
	device_info->max_tx_queues = eth_dev->data->nb_tx_queues;
	device_info->min_rx_bufsize = ENIC_MIN_MTU;
	/* Max packet size is same as PF */
	device_info->max_rx_pktlen = enic_mtu_to_max_rx_pktlen(pf->max_mtu);
	device_info->max_mac_addrs = ENIC_UNICAST_PERFECT_FILTERS;
	/* No offload capa, RSS, etc. until Tx/Rx handlers are added */
	device_info->rx_offload_capa = 0;
	device_info->tx_offload_capa = 0;
	device_info->switch_info.name =	pf->rte_dev->device->name;
	device_info->switch_info.domain_id = vf->switch_domain_id;
	device_info->switch_info.port_id = vf->vf_id;
	return 0;
}

static void set_vf_packet_filter(struct enic_vf_representor *vf)
{
	/* switchdev: packet filters are ignored */
	if (vf->enic.switchdev_mode)
		return;
	/* Ask PF to apply filters on VF */
	vnic_dev_packet_filter(vf->enic.vdev, 1 /* unicast */, 1 /* mcast */,
		1 /* bcast */, vf->promisc, vf->allmulti);
}

static int enic_vf_promiscuous_enable(struct rte_eth_dev *eth_dev)
{
	struct enic_vf_representor *vf;

	ENICPMD_FUNC_TRACE();
	if (rte_eal_process_type() != RTE_PROC_PRIMARY)
		return -E_RTE_SECONDARY;
	vf = eth_dev->data->dev_private;
	vf->promisc = 1;
	set_vf_packet_filter(vf);
	return 0;
}

static int enic_vf_promiscuous_disable(struct rte_eth_dev *eth_dev)
{
	struct enic_vf_representor *vf;

	ENICPMD_FUNC_TRACE();
	if (rte_eal_process_type() != RTE_PROC_PRIMARY)
		return -E_RTE_SECONDARY;
	vf = eth_dev->data->dev_private;
	vf->promisc = 0;
	set_vf_packet_filter(vf);
	return 0;
}

static int enic_vf_allmulticast_enable(struct rte_eth_dev *eth_dev)
{
	struct enic_vf_representor *vf;

	ENICPMD_FUNC_TRACE();
	if (rte_eal_process_type() != RTE_PROC_PRIMARY)
		return -E_RTE_SECONDARY;
	vf = eth_dev->data->dev_private;
	vf->allmulti = 1;
	set_vf_packet_filter(vf);
	return 0;
}

static int enic_vf_allmulticast_disable(struct rte_eth_dev *eth_dev)
{
	struct enic_vf_representor *vf;

	ENICPMD_FUNC_TRACE();
	if (rte_eal_process_type() != RTE_PROC_PRIMARY)
		return -E_RTE_SECONDARY;
	vf = eth_dev->data->dev_private;
	vf->allmulti = 0;
	set_vf_packet_filter(vf);
	return 0;
}

/*
 * A minimal set of handlers.
 * The representor can get/set a small set of VF settings via "proxy" devcmd.
 * With proxy devcmd, the PF driver basically tells the VIC firmware to
 * "perform this devcmd on that VF".
 */
static const struct eth_dev_ops enic_vf_representor_dev_ops = {
	.allmulticast_enable  = enic_vf_allmulticast_enable,
	.allmulticast_disable = enic_vf_allmulticast_disable,
	.dev_configure        = enic_vf_dev_configure,
	.dev_infos_get        = enic_vf_dev_infos_get,
	.dev_start            = enic_vf_dev_start,
	.dev_stop             = enic_vf_dev_stop,
	.dev_close            = enic_vf_dev_close,
	.filter_ctrl          = enic_vf_filter_ctrl,
	.link_update          = enic_vf_link_update,
	.promiscuous_enable   = enic_vf_promiscuous_enable,
	.promiscuous_disable  = enic_vf_promiscuous_disable,
	.stats_get            = enic_vf_stats_get,
	.stats_reset          = enic_vf_stats_reset,
	.rx_queue_setup	      = enic_vf_dev_rx_queue_setup,
	.rx_queue_release     = enic_vf_dev_rx_queue_release,
	.tx_queue_setup	      = enic_vf_dev_tx_queue_setup,
	.tx_queue_release     = enic_vf_dev_tx_queue_release,
};

static int get_vf_config(struct enic_vf_representor *vf)
{
	struct vnic_enet_config *c;
	struct enic *pf;
	int switch_mtu;
	int err;

	c = &vf->config;
	pf = vf->pf;
	/* VF MAC */
	err = vnic_dev_get_mac_addr(vf->enic.vdev, vf->mac_addr.addr_bytes);
	if (err) {
		ENICPMD_LOG(ERR, "error in getting MAC address\n");
		return err;
	}
	rte_ether_addr_copy(&vf->mac_addr, vf->eth_dev->data->mac_addrs);

	/* VF MTU per its vNIC setting */
	err = vnic_dev_spec(vf->enic.vdev,
			    offsetof(struct vnic_enet_config, mtu),
			    sizeof(c->mtu), &c->mtu);
	if (err) {
		ENICPMD_LOG(ERR, "error in getting MTU\n");
		return err;
	}
	/*
	 * Blade switch (fabric interconnect) port's MTU. Assume the kernel
	 * enic driver runs on VF. That driver automatically adjusts its MTU
	 * according to the switch MTU.
	 */
	switch_mtu = vnic_dev_mtu(pf->vdev);
	vf->eth_dev->data->mtu = c->mtu;
	if (switch_mtu > c->mtu)
		vf->eth_dev->data->mtu = RTE_MIN(ENIC_MAX_MTU, switch_mtu);
	return 0;
}

int enic_vf_representor_init(struct rte_eth_dev *eth_dev, void *init_params)
{
	struct enic_vf_representor *vf, *params;
	struct rte_pci_device *pdev;
	struct enic *pf, *vf_enic;
	struct rte_pci_addr *addr;
	int ret;

	ENICPMD_FUNC_TRACE();
	params = init_params;
	vf = eth_dev->data->dev_private;
	vf->switch_domain_id = params->switch_domain_id;
	vf->vf_id = params->vf_id;
	vf->eth_dev = eth_dev;
	vf->pf = params->pf;
	vf->allmulti = 1;
	vf->promisc = 0;
	pf = vf->pf;
	vf->enic.switchdev_mode = pf->switchdev_mode;
	/* Only switchdev is supported now */
	RTE_ASSERT(vf->enic.switchdev_mode);
	/* Allocate WQ, RQ, CQ for the representor */
	vf->pf_wq_idx = vf_wq_idx(vf);
	vf->pf_wq_cq_idx = vf_wq_cq_idx(vf);
	vf->pf_rq_sop_idx = vf_rq_sop_idx(vf);
	vf->pf_rq_data_idx = vf_rq_data_idx(vf);
	/* Remove these assertions once queue allocation has an easy-to-use
	 * allocator API instead of index number calculations used throughout
	 * the driver..
	 */
	RTE_ASSERT(enic_cq_rq(pf, vf->pf_rq_sop_idx) == vf->pf_rq_sop_idx);
	RTE_ASSERT(enic_rte_rq_idx_to_sop_idx(vf->pf_rq_sop_idx) ==
		   vf->pf_rq_sop_idx);
	/* RX handlers use enic_cq_rq(sop) to get CQ, so do not save it */
	pf->vf_required_wq++;
	pf->vf_required_rq += 2; /* sop and data */
	pf->vf_required_cq += 2; /* 1 for rq sop and 1 for wq */
	ENICPMD_LOG(DEBUG, "vf_id %u wq %u rq_sop %u rq_data %u wq_cq %u rq_cq %u",
		vf->vf_id, vf->pf_wq_idx, vf->pf_rq_sop_idx, vf->pf_rq_data_idx,
		vf->pf_wq_cq_idx, enic_cq_rq(pf, vf->pf_rq_sop_idx));
	if (enic_cq_rq(pf, vf->pf_rq_sop_idx) >= pf->conf_cq_count) {
		ENICPMD_LOG(ERR, "Insufficient CQs. Please ensure number of CQs (%u)"
			    " >= number of RQs (%u) in CIMC or UCSM",
			    pf->conf_cq_count, pf->conf_rq_count);
		return -EINVAL;
	}

	/* Check for non-existent VFs */
	pdev = RTE_ETH_DEV_TO_PCI(pf->rte_dev);
	if (vf->vf_id >= pdev->max_vfs) {
		ENICPMD_LOG(ERR, "VF ID is invalid. vf_id %u max_vfs %u",
			    vf->vf_id, pdev->max_vfs);
		return -ENODEV;
	}

	eth_dev->device->driver = pf->rte_dev->device->driver;
	eth_dev->dev_ops = &enic_vf_representor_dev_ops;
	eth_dev->data->dev_flags |= RTE_ETH_DEV_REPRESENTOR |
					RTE_ETH_DEV_AUTOFILL_QUEUE_XSTATS;
	eth_dev->data->representor_id = vf->vf_id;
	eth_dev->data->mac_addrs = rte_zmalloc("enic_mac_addr_vf",
		sizeof(struct rte_ether_addr) *
		ENIC_UNICAST_PERFECT_FILTERS, 0);
	if (eth_dev->data->mac_addrs == NULL)
		return -ENOMEM;
	/* Use 1 RX queue and 1 TX queue for representor path */
	eth_dev->data->nb_rx_queues = 1;
	eth_dev->data->nb_tx_queues = 1;
	eth_dev->rx_pkt_burst = &enic_vf_recv_pkts;
	eth_dev->tx_pkt_burst = &enic_vf_xmit_pkts;
	/* Initial link state copied from PF */
	eth_dev->data->dev_link = pf->rte_dev->data->dev_link;
	/* Representor vdev to perform devcmd */
	vf->enic.vdev = vnic_vf_rep_register(&vf->enic, pf->vdev, vf->vf_id);
	if (vf->enic.vdev == NULL)
		return -ENOMEM;
	ret = vnic_dev_alloc_stats_mem(vf->enic.vdev);
	if (ret)
		return ret;
	/* Get/copy VF vNIC MAC, MTU, etc. into eth_dev */
	ret = get_vf_config(vf);
	if (ret)
		return ret;

	/*
	 * Calculate VF BDF. The firmware ensures that PF BDF is always
	 * bus:dev.0, and VF BDFs are dev.1, dev.2, and so on.
	 */
	vf->bdf = pdev->addr;
	vf->bdf.function += vf->vf_id + 1;

	/* Copy a few fields used by enic_fm_flow */
	vf_enic = &vf->enic;
	vf_enic->switch_domain_id = vf->switch_domain_id;
	vf_enic->flow_filter_mode = pf->flow_filter_mode;
	vf_enic->rte_dev = eth_dev;
	vf_enic->dev_data = eth_dev->data;
	LIST_INIT(&vf_enic->flows);
	LIST_INIT(&vf_enic->memzone_list);
	rte_spinlock_init(&vf_enic->memzone_list_lock);
	addr = &vf->bdf;
	snprintf(vf_enic->bdf_name, ENICPMD_BDF_LENGTH, "%04x:%02x:%02x.%x",
		 addr->domain, addr->bus, addr->devid, addr->function);
	return 0;
}

int enic_vf_representor_uninit(struct rte_eth_dev *eth_dev)
{
	struct enic_vf_representor *vf;

	ENICPMD_FUNC_TRACE();
	vf = eth_dev->data->dev_private;
	vnic_dev_unregister(vf->enic.vdev);
	return 0;
}