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
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
/* SPDX-License-Identifier: BSD-3-Clause
 * Copyright (c) 2018 Microsoft Corp.
 * All rights reserved.
 */

#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <stdbool.h>
#include <errno.h>
#include <unistd.h>
#include <dirent.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/uio.h>

#include <rte_ether.h>
#include <rte_ethdev.h>
#include <ethdev_driver.h>
#include <rte_lcore.h>
#include <rte_memory.h>
#include <rte_bus_vmbus.h>
#include <rte_pci.h>
#include <rte_bus_pci.h>
#include <rte_log.h>
#include <rte_string_fns.h>
#include <rte_alarm.h>

#include "hn_logs.h"
#include "hn_var.h"
#include "hn_nvs.h"

/* Search for VF with matching MAC address, return port id */
static int hn_vf_match(const struct rte_eth_dev *dev)
{
	const struct rte_ether_addr *mac = dev->data->mac_addrs;
	int i;

	RTE_ETH_FOREACH_DEV(i) {
		const struct rte_eth_dev *vf_dev = &rte_eth_devices[i];
		const struct rte_ether_addr *vf_mac = vf_dev->data->mac_addrs;

		if (vf_dev == dev)
			continue;

		if (rte_is_same_ether_addr(mac, vf_mac))
			return i;
	}
	return -ENOENT;
}


/*
 * Attach new PCI VF device and return the port_id
 */
static int hn_vf_attach(struct rte_eth_dev *dev, struct hn_data *hv)
{
	struct rte_eth_dev_owner owner = { .id = RTE_ETH_DEV_NO_OWNER };
	int port, ret;

	if (hv->vf_ctx.vf_attached) {
		PMD_DRV_LOG(ERR, "VF already attached");
		return 0;
	}

	port = hn_vf_match(dev);
	if (port < 0) {
		PMD_DRV_LOG(NOTICE, "Couldn't find port for VF");
		return port;
	}

	PMD_DRV_LOG(NOTICE, "found matching VF port %d\n", port);
	ret = rte_eth_dev_owner_get(port, &owner);
	if (ret < 0) {
		PMD_DRV_LOG(ERR, "Can not find owner for port %d", port);
		return ret;
	}

	if (owner.id != RTE_ETH_DEV_NO_OWNER) {
		PMD_DRV_LOG(ERR, "Port %u already owned by other device %s",
			    port, owner.name);
		return -EBUSY;
	}

	ret = rte_eth_dev_owner_set(port, &hv->owner);
	if (ret < 0) {
		PMD_DRV_LOG(ERR, "Can set owner for port %d", port);
		return ret;
	}

	PMD_DRV_LOG(DEBUG, "Attach VF device %u", port);
	hv->vf_ctx.vf_attached = true;
	hv->vf_ctx.vf_port = port;
	return 0;
}

static void hn_vf_remove(struct hn_data *hv);

static void hn_remove_delayed(void *args)
{
	struct hn_data *hv = args;
	uint16_t port_id = hv->vf_ctx.vf_port;
	struct rte_device *dev = rte_eth_devices[port_id].device;
	int ret;

	/* Tell VSP to switch data path to synthentic */
	hn_vf_remove(hv);

	PMD_DRV_LOG(NOTICE, "Start to remove port %d\n", port_id);
	rte_rwlock_write_lock(&hv->vf_lock);

	/* Give back ownership */
	ret = rte_eth_dev_owner_unset(port_id, hv->owner.id);
	if (ret)
		PMD_DRV_LOG(ERR, "rte_eth_dev_owner_unset failed ret=%d\n",
			    ret);
	hv->vf_ctx.vf_attached = false;

	ret = rte_eth_dev_callback_unregister(port_id, RTE_ETH_EVENT_INTR_RMV,
					      hn_eth_rmv_event_callback, hv);
	if (ret)
		PMD_DRV_LOG(ERR,
			    "rte_eth_dev_callback_unregister failed ret=%d\n",
			    ret);

	/* Detach and release port_id from system */
	ret = rte_eth_dev_stop(port_id);
	if (ret)
		PMD_DRV_LOG(ERR, "rte_eth_dev_stop failed port_id=%u ret=%d\n",
			    port_id, ret);

	ret = rte_eth_dev_close(port_id);
	if (ret)
		PMD_DRV_LOG(ERR, "rte_eth_dev_close failed port_id=%u ret=%d\n",
			    port_id, ret);

	ret = rte_dev_remove(dev);
	hv->vf_ctx.vf_state = vf_removed;

	rte_rwlock_write_unlock(&hv->vf_lock);
}

int hn_eth_rmv_event_callback(uint16_t port_id,
			      enum rte_eth_event_type event __rte_unused,
			      void *cb_arg, void *out __rte_unused)
{
	struct hn_data *hv = cb_arg;

	PMD_DRV_LOG(NOTICE, "Removing VF portid %d\n", port_id);
	rte_eal_alarm_set(1, hn_remove_delayed, hv);

	return 0;
}

static int hn_setup_vf_queues(int port, struct rte_eth_dev *dev)
{
	struct hn_rx_queue *rx_queue;
	struct rte_eth_txq_info txinfo;
	struct rte_eth_rxq_info rxinfo;
	int i, ret = 0;

	for (i = 0; i < dev->data->nb_tx_queues; i++) {
		ret = rte_eth_tx_queue_info_get(dev->data->port_id, i, &txinfo);
		if (ret) {
			PMD_DRV_LOG(ERR,
				    "rte_eth_tx_queue_info_get failed ret=%d\n",
				    ret);
			return ret;
		}

		ret = rte_eth_tx_queue_setup(port, i, txinfo.nb_desc, 0,
					     &txinfo.conf);
		if (ret) {
			PMD_DRV_LOG(ERR,
				    "rte_eth_tx_queue_setup failed ret=%d\n",
				    ret);
			return ret;
		}
	}

	for (i = 0; i < dev->data->nb_rx_queues; i++) {
		ret = rte_eth_rx_queue_info_get(dev->data->port_id, i, &rxinfo);
		if (ret) {
			PMD_DRV_LOG(ERR,
				    "rte_eth_rx_queue_info_get failed ret=%d\n",
				    ret);
			return ret;
		}

		rx_queue = dev->data->rx_queues[i];

		ret = rte_eth_rx_queue_setup(port, i, rxinfo.nb_desc, 0,
					     &rxinfo.conf, rx_queue->mb_pool);
		if (ret) {
			PMD_DRV_LOG(ERR,
				    "rte_eth_rx_queue_setup failed ret=%d\n",
				    ret);
			return ret;
		}
	}

	return ret;
}

int hn_vf_add(struct rte_eth_dev *dev, struct hn_data *hv);

static void hn_vf_add_retry(void *args)
{
	struct rte_eth_dev *dev = args;
	struct hn_data *hv = dev->data->dev_private;

	hn_vf_add(dev, hv);
}

int hn_vf_configure(struct rte_eth_dev *dev,
		    const struct rte_eth_conf *dev_conf);

/* Add new VF device to synthetic device */
int hn_vf_add(struct rte_eth_dev *dev, struct hn_data *hv)
{
	int ret, port;

	if (!hv->vf_ctx.vf_vsp_reported || hv->vf_ctx.vf_vsc_switched)
		return 0;

	rte_rwlock_write_lock(&hv->vf_lock);

	ret = hn_vf_attach(dev, hv);
	if (ret) {
		PMD_DRV_LOG(NOTICE,
			    "RNDIS reports VF but device not found, retrying");
		rte_eal_alarm_set(1000000, hn_vf_add_retry, dev);
		goto exit;
	}

	port = hv->vf_ctx.vf_port;

	/* If the primary device has started, this is a VF host add.
	 * Configure and start VF device.
	 */
	if (dev->data->dev_started) {
		if (rte_eth_devices[port].data->dev_started) {
			PMD_DRV_LOG(ERR, "VF already started on hot add");
			goto exit;
		}

		PMD_DRV_LOG(NOTICE, "configuring VF port %d\n", port);
		ret = hn_vf_configure(dev, &dev->data->dev_conf);
		if (ret) {
			PMD_DRV_LOG(ERR, "Failed to configure VF port %d\n",
				    port);
			goto exit;
		}

		ret = hn_setup_vf_queues(port, dev);
		if (ret) {
			PMD_DRV_LOG(ERR,
				    "Failed to configure VF queues port %d\n",
				    port);
			goto exit;
		}

		PMD_DRV_LOG(NOTICE, "Starting VF port %d\n", port);
		ret = rte_eth_dev_start(port);
		if (ret) {
			PMD_DRV_LOG(ERR, "rte_eth_dev_start failed ret=%d\n",
				    ret);
			goto exit;
		}
		hv->vf_ctx.vf_state = vf_started;
	}

	ret = hn_nvs_set_datapath(hv, NVS_DATAPATH_VF);
	if (ret == 0)
		hv->vf_ctx.vf_vsc_switched = true;

exit:
	rte_rwlock_write_unlock(&hv->vf_lock);
	return ret;
}

/* Switch data path to VF device */
static void hn_vf_remove(struct hn_data *hv)
{
	int ret;

	if (!hv->vf_ctx.vf_vsc_switched) {
		PMD_DRV_LOG(ERR, "VF path not active");
		return;
	}

	rte_rwlock_write_lock(&hv->vf_lock);
	if (!hv->vf_ctx.vf_vsc_switched) {
		PMD_DRV_LOG(ERR, "VF path not active");
	} else {
		/* Stop incoming packets from arriving on VF */
		ret = hn_nvs_set_datapath(hv, NVS_DATAPATH_SYNTHETIC);
		if (ret == 0)
			hv->vf_ctx.vf_vsc_switched = false;
	}
	rte_rwlock_write_unlock(&hv->vf_lock);
}

/* Handle VF association message from host */
void
hn_nvs_handle_vfassoc(struct rte_eth_dev *dev,
		      const struct vmbus_chanpkt_hdr *hdr,
		      const void *data)
{
	struct hn_data *hv = dev->data->dev_private;
	const struct hn_nvs_vf_association *vf_assoc = data;

	if (unlikely(vmbus_chanpkt_datalen(hdr) < sizeof(*vf_assoc))) {
		PMD_DRV_LOG(ERR, "invalid vf association NVS");
		return;
	}

	PMD_DRV_LOG(DEBUG, "VF serial %u %s port %u",
		    vf_assoc->serial,
		    vf_assoc->allocated ? "add to" : "remove from",
		    dev->data->port_id);

	hv->vf_ctx.vf_vsp_reported = vf_assoc->allocated;

	if (dev->state == RTE_ETH_DEV_ATTACHED) {
		if (vf_assoc->allocated)
			hn_vf_add(dev, hv);
		else
			hn_vf_remove(hv);
	}
}

static void
hn_vf_merge_desc_lim(struct rte_eth_desc_lim *lim,
		     const struct rte_eth_desc_lim *vf_lim)
{
	lim->nb_max = RTE_MIN(vf_lim->nb_max, lim->nb_max);
	lim->nb_min = RTE_MAX(vf_lim->nb_min, lim->nb_min);
	lim->nb_align = RTE_MAX(vf_lim->nb_align, lim->nb_align);
	lim->nb_seg_max = RTE_MIN(vf_lim->nb_seg_max, lim->nb_seg_max);
	lim->nb_mtu_seg_max = RTE_MIN(vf_lim->nb_seg_max, lim->nb_seg_max);
}

/*
 * Merge the info from the VF and synthetic path.
 * use the default config of the VF
 * and the minimum number of queues and buffer sizes.
 */
static int hn_vf_info_merge(struct rte_eth_dev *vf_dev,
			     struct rte_eth_dev_info *info)
{
	struct rte_eth_dev_info vf_info;
	int ret;

	ret = rte_eth_dev_info_get(vf_dev->data->port_id, &vf_info);
	if (ret != 0)
		return ret;

	info->speed_capa = vf_info.speed_capa;
	info->default_rxportconf = vf_info.default_rxportconf;
	info->default_txportconf = vf_info.default_txportconf;

	info->max_rx_queues = RTE_MIN(vf_info.max_rx_queues,
				      info->max_rx_queues);
	info->rx_offload_capa &= vf_info.rx_offload_capa;
	info->rx_queue_offload_capa &= vf_info.rx_queue_offload_capa;
	info->flow_type_rss_offloads &= vf_info.flow_type_rss_offloads;

	info->max_tx_queues = RTE_MIN(vf_info.max_tx_queues,
				      info->max_tx_queues);
	info->tx_offload_capa &= vf_info.tx_offload_capa;
	info->tx_queue_offload_capa &= vf_info.tx_queue_offload_capa;
	hn_vf_merge_desc_lim(&info->tx_desc_lim, &vf_info.tx_desc_lim);

	info->min_rx_bufsize = RTE_MAX(vf_info.min_rx_bufsize,
				       info->min_rx_bufsize);
	info->max_rx_pktlen  = RTE_MAX(vf_info.max_rx_pktlen,
				       info->max_rx_pktlen);
	hn_vf_merge_desc_lim(&info->rx_desc_lim, &vf_info.rx_desc_lim);

	return 0;
}

int hn_vf_info_get(struct hn_data *hv, struct rte_eth_dev_info *info)
{
	struct rte_eth_dev *vf_dev;
	int ret = 0;

	rte_rwlock_read_lock(&hv->vf_lock);
	vf_dev = hn_get_vf_dev(hv);
	if (vf_dev)
		ret = hn_vf_info_merge(vf_dev, info);
	rte_rwlock_read_unlock(&hv->vf_lock);
	return ret;
}

int hn_vf_configure(struct rte_eth_dev *dev,
		    const struct rte_eth_conf *dev_conf)
{
	struct hn_data *hv = dev->data->dev_private;
	struct rte_eth_conf vf_conf = *dev_conf;
	int ret = 0;

	/* link state interrupt does not matter here. */
	vf_conf.intr_conf.lsc = 0;

	/* need to monitor removal event */
	vf_conf.intr_conf.rmv = 1;

	if (hv->vf_ctx.vf_attached) {
		ret = rte_eth_dev_callback_register(hv->vf_ctx.vf_port,
						    RTE_ETH_EVENT_INTR_RMV,
						    hn_eth_rmv_event_callback,
						    hv);
		if (ret) {
			PMD_DRV_LOG(ERR,
				    "Registering callback failed for "
				    "vf port %d ret %d\n",
				    hv->vf_ctx.vf_port, ret);
			return ret;
		}

		ret = rte_eth_dev_configure(hv->vf_ctx.vf_port,
					    dev->data->nb_rx_queues,
					    dev->data->nb_tx_queues,
					    &vf_conf);
		if (ret) {
			PMD_DRV_LOG(ERR, "VF configuration failed: %d", ret);

			rte_eth_dev_callback_unregister(hv->vf_ctx.vf_port,
							RTE_ETH_EVENT_INTR_RMV,
							hn_eth_rmv_event_callback,
							hv);

			return ret;
		}

		hv->vf_ctx.vf_state = vf_configured;
	}

	return ret;
}

/* Configure VF if present.
 * VF device will have the same number of queues as the synthetic device
 */
int hn_vf_configure_locked(struct rte_eth_dev *dev,
			   const struct rte_eth_conf *dev_conf)
{
	struct hn_data *hv = dev->data->dev_private;
	int ret = 0;

	rte_rwlock_write_lock(&hv->vf_lock);
	ret = hn_vf_configure(dev, dev_conf);
	rte_rwlock_write_unlock(&hv->vf_lock);

	return ret;
}

const uint32_t *hn_vf_supported_ptypes(struct rte_eth_dev *dev)
{
	struct hn_data *hv = dev->data->dev_private;
	struct rte_eth_dev *vf_dev;
	const uint32_t *ptypes = NULL;

	rte_rwlock_read_lock(&hv->vf_lock);
	vf_dev = hn_get_vf_dev(hv);
	if (vf_dev && vf_dev->dev_ops->dev_supported_ptypes_get)
		ptypes = (*vf_dev->dev_ops->dev_supported_ptypes_get)(vf_dev);
	rte_rwlock_read_unlock(&hv->vf_lock);

	return ptypes;
}

int hn_vf_start(struct rte_eth_dev *dev)
{
	struct hn_data *hv = dev->data->dev_private;
	struct rte_eth_dev *vf_dev;
	int ret = 0;

	rte_rwlock_read_lock(&hv->vf_lock);
	vf_dev = hn_get_vf_dev(hv);
	if (vf_dev)
		ret = rte_eth_dev_start(vf_dev->data->port_id);
	rte_rwlock_read_unlock(&hv->vf_lock);
	return ret;
}

int hn_vf_stop(struct rte_eth_dev *dev)
{
	struct hn_data *hv = dev->data->dev_private;
	struct rte_eth_dev *vf_dev;
	int ret = 0;

	rte_rwlock_read_lock(&hv->vf_lock);
	vf_dev = hn_get_vf_dev(hv);
	if (vf_dev) {
		ret = rte_eth_dev_stop(vf_dev->data->port_id);
		if (ret != 0)
			PMD_DRV_LOG(ERR, "Failed to stop device on port %u",
				    vf_dev->data->port_id);
	}
	rte_rwlock_read_unlock(&hv->vf_lock);

	return ret;
}

/* If VF is present, then cascade configuration down */
#define VF_ETHDEV_FUNC(dev, func)				\
	{							\
		struct hn_data *hv = (dev)->data->dev_private;	\
		struct rte_eth_dev *vf_dev;			\
		rte_rwlock_read_lock(&hv->vf_lock);		\
		vf_dev = hn_get_vf_dev(hv);			\
		if (vf_dev)					\
			func(vf_dev->data->port_id);		\
		rte_rwlock_read_unlock(&hv->vf_lock);		\
	}

/* If VF is present, then cascade configuration down */
#define VF_ETHDEV_FUNC_RET_STATUS(dev, func)			\
	{							\
		struct hn_data *hv = (dev)->data->dev_private;	\
		struct rte_eth_dev *vf_dev;			\
		int ret = 0;					\
		rte_rwlock_read_lock(&hv->vf_lock);		\
		vf_dev = hn_get_vf_dev(hv);			\
		if (vf_dev)					\
			ret = func(vf_dev->data->port_id);	\
		rte_rwlock_read_unlock(&hv->vf_lock);		\
		return ret;					\
	}

void hn_vf_reset(struct rte_eth_dev *dev)
{
	VF_ETHDEV_FUNC(dev, rte_eth_dev_reset);
}

int hn_vf_close(struct rte_eth_dev *dev)
{
	int ret = 0;
	struct hn_data *hv = dev->data->dev_private;

	rte_eal_alarm_cancel(hn_vf_add_retry, dev);

	rte_rwlock_read_lock(&hv->vf_lock);
	if (hv->vf_ctx.vf_attached) {
		rte_eth_dev_callback_unregister(hv->vf_ctx.vf_port,
						RTE_ETH_EVENT_INTR_RMV,
						hn_eth_rmv_event_callback,
						hv);
		rte_eal_alarm_cancel(hn_remove_delayed, hv);
		ret = rte_eth_dev_close(hv->vf_ctx.vf_port);
		hv->vf_ctx.vf_attached = false;
	}
	rte_rwlock_read_unlock(&hv->vf_lock);

	return ret;
}

int hn_vf_stats_reset(struct rte_eth_dev *dev)
{
	VF_ETHDEV_FUNC_RET_STATUS(dev, rte_eth_stats_reset);
}

int hn_vf_allmulticast_enable(struct rte_eth_dev *dev)
{
	VF_ETHDEV_FUNC_RET_STATUS(dev, rte_eth_allmulticast_enable);
}

int hn_vf_allmulticast_disable(struct rte_eth_dev *dev)
{
	VF_ETHDEV_FUNC_RET_STATUS(dev, rte_eth_allmulticast_disable);
}

int hn_vf_promiscuous_enable(struct rte_eth_dev *dev)
{
	VF_ETHDEV_FUNC_RET_STATUS(dev, rte_eth_promiscuous_enable);
}

int hn_vf_promiscuous_disable(struct rte_eth_dev *dev)
{
	VF_ETHDEV_FUNC_RET_STATUS(dev, rte_eth_promiscuous_disable);
}

int hn_vf_mc_addr_list(struct rte_eth_dev *dev,
			struct rte_ether_addr *mc_addr_set,
			uint32_t nb_mc_addr)
{
	struct hn_data *hv = dev->data->dev_private;
	struct rte_eth_dev *vf_dev;
	int ret = 0;

	rte_rwlock_read_lock(&hv->vf_lock);
	vf_dev = hn_get_vf_dev(hv);
	if (vf_dev)
		ret = rte_eth_dev_set_mc_addr_list(vf_dev->data->port_id,
						   mc_addr_set, nb_mc_addr);
	rte_rwlock_read_unlock(&hv->vf_lock);
	return ret;
}

int hn_vf_tx_queue_setup(struct rte_eth_dev *dev,
			 uint16_t queue_idx, uint16_t nb_desc,
			 unsigned int socket_id,
			 const struct rte_eth_txconf *tx_conf)
{
	struct hn_data *hv = dev->data->dev_private;
	struct rte_eth_dev *vf_dev;
	int ret = 0;

	rte_rwlock_read_lock(&hv->vf_lock);
	vf_dev = hn_get_vf_dev(hv);
	if (vf_dev)
		ret = rte_eth_tx_queue_setup(vf_dev->data->port_id,
					     queue_idx, nb_desc,
					     socket_id, tx_conf);
	rte_rwlock_read_unlock(&hv->vf_lock);
	return ret;
}

void hn_vf_tx_queue_release(struct hn_data *hv, uint16_t queue_id)
{
	struct rte_eth_dev *vf_dev;

	rte_rwlock_read_lock(&hv->vf_lock);
	vf_dev = hn_get_vf_dev(hv);
	if (vf_dev && vf_dev->dev_ops->tx_queue_release) {
		void *subq = vf_dev->data->tx_queues[queue_id];

		(*vf_dev->dev_ops->tx_queue_release)(subq);
	}

	rte_rwlock_read_unlock(&hv->vf_lock);
}

int hn_vf_rx_queue_setup(struct rte_eth_dev *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 hn_data *hv = dev->data->dev_private;
	struct rte_eth_dev *vf_dev;
	int ret = 0;

	rte_rwlock_read_lock(&hv->vf_lock);
	vf_dev = hn_get_vf_dev(hv);
	if (vf_dev)
		ret = rte_eth_rx_queue_setup(vf_dev->data->port_id,
					     queue_idx, nb_desc,
					     socket_id, rx_conf, mp);
	rte_rwlock_read_unlock(&hv->vf_lock);
	return ret;
}

void hn_vf_rx_queue_release(struct hn_data *hv, uint16_t queue_id)
{
	struct rte_eth_dev *vf_dev;

	rte_rwlock_read_lock(&hv->vf_lock);
	vf_dev = hn_get_vf_dev(hv);
	if (vf_dev && vf_dev->dev_ops->rx_queue_release) {
		void *subq = vf_dev->data->rx_queues[queue_id];

		(*vf_dev->dev_ops->rx_queue_release)(subq);
	}
	rte_rwlock_read_unlock(&hv->vf_lock);
}

int hn_vf_stats_get(struct rte_eth_dev *dev,
		    struct rte_eth_stats *stats)
{
	struct hn_data *hv = dev->data->dev_private;
	struct rte_eth_dev *vf_dev;
	int ret = 0;

	rte_rwlock_read_lock(&hv->vf_lock);
	vf_dev = hn_get_vf_dev(hv);
	if (vf_dev)
		ret = rte_eth_stats_get(vf_dev->data->port_id, stats);
	rte_rwlock_read_unlock(&hv->vf_lock);
	return ret;
}

int hn_vf_xstats_get_names(struct rte_eth_dev *dev,
			   struct rte_eth_xstat_name *names,
			   unsigned int n)
{
	struct hn_data *hv = dev->data->dev_private;
	struct rte_eth_dev *vf_dev;
	int i, count = 0;

	rte_rwlock_read_lock(&hv->vf_lock);
	vf_dev = hn_get_vf_dev(hv);
	if (vf_dev)
		count = rte_eth_xstats_get_names(vf_dev->data->port_id,
						 names, n);
	rte_rwlock_read_unlock(&hv->vf_lock);

	/* add vf_ prefix to xstat names */
	if (names) {
		for (i = 0; i < count; i++) {
			char tmp[RTE_ETH_XSTATS_NAME_SIZE];

			snprintf(tmp, sizeof(tmp), "vf_%s", names[i].name);
			strlcpy(names[i].name, tmp, sizeof(names[i].name));
		}
	}

	return count;
}

int hn_vf_xstats_get(struct rte_eth_dev *dev,
		     struct rte_eth_xstat *xstats,
		     unsigned int offset,
		     unsigned int n)
{
	struct hn_data *hv = dev->data->dev_private;
	struct rte_eth_dev *vf_dev;
	int i, count = 0;

	rte_rwlock_read_lock(&hv->vf_lock);
	vf_dev = hn_get_vf_dev(hv);
	if (vf_dev)
		count = rte_eth_xstats_get(vf_dev->data->port_id,
					   xstats + offset, n - offset);
	rte_rwlock_read_unlock(&hv->vf_lock);

	/* Offset id's for VF stats */
	if (count > 0) {
		for (i = 0; i < count; i++)
			xstats[i + offset].id += offset;
	}

	return count;
}

int hn_vf_xstats_reset(struct rte_eth_dev *dev)
{
	struct hn_data *hv = dev->data->dev_private;
	struct rte_eth_dev *vf_dev;
	int ret;

	rte_rwlock_read_lock(&hv->vf_lock);
	vf_dev = hn_get_vf_dev(hv);
	if (vf_dev)
		ret = rte_eth_xstats_reset(vf_dev->data->port_id);
	else
		ret = -EINVAL;
	rte_rwlock_read_unlock(&hv->vf_lock);

	return ret;
}

int hn_vf_rss_hash_update(struct rte_eth_dev *dev,
			  struct rte_eth_rss_conf *rss_conf)
{
	struct hn_data *hv = dev->data->dev_private;
	struct rte_eth_dev *vf_dev;
	int ret = 0;

	rte_rwlock_read_lock(&hv->vf_lock);
	vf_dev = hn_get_vf_dev(hv);
	if (vf_dev && vf_dev->dev_ops->rss_hash_update)
		ret = vf_dev->dev_ops->rss_hash_update(vf_dev, rss_conf);
	rte_rwlock_read_unlock(&hv->vf_lock);

	return ret;
}

int hn_vf_reta_hash_update(struct rte_eth_dev *dev,
			   struct rte_eth_rss_reta_entry64 *reta_conf,
			   uint16_t reta_size)
{
	struct hn_data *hv = dev->data->dev_private;
	struct rte_eth_dev *vf_dev;
	int ret = 0;

	rte_rwlock_read_lock(&hv->vf_lock);
	vf_dev = hn_get_vf_dev(hv);
	if (vf_dev && vf_dev->dev_ops->reta_update)
		ret = vf_dev->dev_ops->reta_update(vf_dev,
						   reta_conf, reta_size);
	rte_rwlock_read_unlock(&hv->vf_lock);

	return ret;
}