aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/net/mvneta/mvneta_ethdev.c
blob: 2d7666454ee01b66555433f2918fdabe9d5452a7 (plain)
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
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
/* SPDX-License-Identifier: BSD-3-Clause
 * Copyright(c) 2018 Marvell International Ltd.
 * Copyright(c) 2018 Semihalf.
 * All rights reserved.
 */

#include <rte_ethdev_driver.h>
#include <rte_kvargs.h>
#include <rte_bus_vdev.h>

#include <stdio.h>
#include <fcntl.h>
#include <linux/ethtool.h>
#include <linux/sockios.h>
#include <net/if.h>
#include <net/if_arp.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/types.h>

#include <rte_mvep_common.h>

#include "mvneta_rxtx.h"


#define MVNETA_IFACE_NAME_ARG "iface"

#define MVNETA_RX_OFFLOADS (DEV_RX_OFFLOAD_JUMBO_FRAME | \
			  DEV_RX_OFFLOAD_CHECKSUM)

/** Port Tx offloads capabilities */
#define MVNETA_TX_OFFLOADS (DEV_TX_OFFLOAD_IPV4_CKSUM | \
			  DEV_TX_OFFLOAD_UDP_CKSUM | \
			  DEV_TX_OFFLOAD_TCP_CKSUM | \
			  DEV_TX_OFFLOAD_MULTI_SEGS)

#define MVNETA_PKT_SIZE_MAX (16382 - MV_MH_SIZE) /* 9700B */
#define MVNETA_DEFAULT_MTU 1500

#define MVNETA_MAC_ADDRS_MAX 256 /*16 UC, 256 IP, 256 MC/BC */
/** Maximum length of a match string */
#define MVNETA_MATCH_LEN 16

int mvneta_logtype;

static const char * const valid_args[] = {
	MVNETA_IFACE_NAME_ARG,
	NULL
};

struct mvneta_ifnames {
	const char *names[NETA_NUM_ETH_PPIO];
	int idx;
};

static int mvneta_dev_num;

/**
 * Deinitialize packet processor.
 */
static void
mvneta_neta_deinit(void)
{
	neta_deinit();
}

/**
 * Initialize packet processor.
 *
 * @return
 *   0 on success, negative error value otherwise.
 */
static int
mvneta_neta_init(void)
{
	return neta_init();
}

/**
 * Callback used by rte_kvargs_process() during argument parsing.
 *
 * @param key
 *   Pointer to the parsed key (unused).
 * @param value
 *   Pointer to the parsed value.
 * @param extra_args
 *   Pointer to the extra arguments which contains address of the
 *   table of pointers to parsed interface names.
 *
 * @return
 *   Always 0.
 */
static int
mvneta_ifnames_get(const char *key __rte_unused, const char *value,
		 void *extra_args)
{
	struct mvneta_ifnames *ifnames = extra_args;

	ifnames->names[ifnames->idx++] = value;

	return 0;
}

/**
 * Ethernet device configuration.
 *
 * Prepare the driver for a given number of TX and RX queues and
 * configure RSS if supported.
 *
 * @param dev
 *   Pointer to Ethernet device structure.
 *
 * @return
 *   0 on success, negative error value otherwise.
 */
static int
mvneta_dev_configure(struct rte_eth_dev *dev)
{
	struct mvneta_priv *priv = dev->data->dev_private;
	struct neta_ppio_params *ppio_params;

	if (dev->data->dev_conf.rxmode.mq_mode != ETH_MQ_RX_NONE) {
		MVNETA_LOG(INFO, "Unsupported RSS and rx multi queue mode %d",
			dev->data->dev_conf.rxmode.mq_mode);
		if (dev->data->nb_rx_queues > 1)
			return -EINVAL;
	}

	if (dev->data->dev_conf.rxmode.split_hdr_size) {
		MVNETA_LOG(INFO, "Split headers not supported");
		return -EINVAL;
	}

	if (dev->data->dev_conf.rxmode.offloads & DEV_RX_OFFLOAD_JUMBO_FRAME)
		dev->data->mtu = dev->data->dev_conf.rxmode.max_rx_pkt_len -
				 MRVL_NETA_ETH_HDRS_LEN;

	if (dev->data->dev_conf.txmode.offloads & DEV_TX_OFFLOAD_MULTI_SEGS)
		priv->multiseg = 1;

	ppio_params = &priv->ppio_params;
	ppio_params->outqs_params.num_outqs = dev->data->nb_tx_queues;
	/* Default: 1 TC, no QoS supported. */
	ppio_params->inqs_params.num_tcs = 1;
	ppio_params->inqs_params.tcs_params[0].pkt_offset = MRVL_NETA_PKT_OFFS;
	priv->ppio_id = dev->data->port_id;

	return 0;
}

/**
 * DPDK callback to get information about the device.
 *
 * @param dev
 *   Pointer to Ethernet device structure (unused).
 * @param info
 *   Info structure output buffer.
 */
static void
mvneta_dev_infos_get(struct rte_eth_dev *dev __rte_unused,
		   struct rte_eth_dev_info *info)
{
	info->speed_capa = ETH_LINK_SPEED_10M |
			   ETH_LINK_SPEED_100M |
			   ETH_LINK_SPEED_1G |
			   ETH_LINK_SPEED_2_5G;

	info->max_rx_queues = MRVL_NETA_RXQ_MAX;
	info->max_tx_queues = MRVL_NETA_TXQ_MAX;
	info->max_mac_addrs = MVNETA_MAC_ADDRS_MAX;

	info->rx_desc_lim.nb_max = MRVL_NETA_RXD_MAX;
	info->rx_desc_lim.nb_min = MRVL_NETA_RXD_MIN;
	info->rx_desc_lim.nb_align = MRVL_NETA_RXD_ALIGN;

	info->tx_desc_lim.nb_max = MRVL_NETA_TXD_MAX;
	info->tx_desc_lim.nb_min = MRVL_NETA_TXD_MIN;
	info->tx_desc_lim.nb_align = MRVL_NETA_TXD_ALIGN;

	info->rx_offload_capa = MVNETA_RX_OFFLOADS;
	info->rx_queue_offload_capa = MVNETA_RX_OFFLOADS;

	info->tx_offload_capa =  MVNETA_TX_OFFLOADS;
	info->tx_queue_offload_capa =  MVNETA_TX_OFFLOADS;

	/* By default packets are dropped if no descriptors are available */
	info->default_rxconf.rx_drop_en = 1;
	/* Deferred tx queue start is not supported */
	info->default_txconf.tx_deferred_start = 0;
	info->default_txconf.offloads = 0;

	info->max_rx_pktlen = MVNETA_PKT_SIZE_MAX;
}

/**
 * Return supported packet types.
 *
 * @param dev
 *   Pointer to Ethernet device structure (unused).
 *
 * @return
 *   Const pointer to the table with supported packet types.
 */
static const uint32_t *
mvneta_dev_supported_ptypes_get(struct rte_eth_dev *dev __rte_unused)
{
	static const uint32_t ptypes[] = {
		RTE_PTYPE_L2_ETHER,
		RTE_PTYPE_L2_ETHER_VLAN,
		RTE_PTYPE_L3_IPV4,
		RTE_PTYPE_L3_IPV6,
		RTE_PTYPE_L4_TCP,
		RTE_PTYPE_L4_UDP
	};

	return ptypes;
}

/**
 * DPDK callback to change the MTU.
 *
 * Setting the MTU affects hardware MRU (packets larger than the MRU
 * will be dropped).
 *
 * @param dev
 *   Pointer to Ethernet device structure.
 * @param mtu
 *   New MTU.
 *
 * @return
 *   0 on success, negative error value otherwise.
 */
static int
mvneta_mtu_set(struct rte_eth_dev *dev, uint16_t mtu)
{
	struct mvneta_priv *priv = dev->data->dev_private;
	uint16_t mbuf_data_size = 0; /* SW buffer size */
	uint16_t mru;
	int ret;

	mru = MRVL_NETA_MTU_TO_MRU(mtu);
	/*
	 * min_rx_buf_size is equal to mbuf data size
	 * if pmd didn't set it differently
	 */
	mbuf_data_size = dev->data->min_rx_buf_size - RTE_PKTMBUF_HEADROOM;
	/* Prevent PMD from:
	 * - setting mru greater than the mbuf size resulting in
	 * hw and sw buffer size mismatch
	 * - setting mtu that requires the support of scattered packets
	 * when this feature has not been enabled/supported so far.
	 */
	if (!dev->data->scattered_rx &&
	    (mru + MRVL_NETA_PKT_OFFS > mbuf_data_size)) {
		mru = mbuf_data_size - MRVL_NETA_PKT_OFFS;
		mtu = MRVL_NETA_MRU_TO_MTU(mru);
		MVNETA_LOG(WARNING, "MTU too big, max MTU possible limitted by"
			" current mbuf size: %u. Set MTU to %u, MRU to %u",
			mbuf_data_size, mtu, mru);
	}

	if (mtu < ETHER_MIN_MTU || mru > MVNETA_PKT_SIZE_MAX) {
		MVNETA_LOG(ERR, "Invalid MTU [%u] or MRU [%u]", mtu, mru);
		return -EINVAL;
	}

	dev->data->mtu = mtu;
	dev->data->dev_conf.rxmode.max_rx_pkt_len = mru - MV_MH_SIZE;

	if (!priv->ppio)
		/* It is OK. New MTU will be set later on mvneta_dev_start */
		return 0;

	ret = neta_ppio_set_mru(priv->ppio, mru);
	if (ret) {
		MVNETA_LOG(ERR, "Failed to change MRU");
		return ret;
	}

	ret = neta_ppio_set_mtu(priv->ppio, mtu);
	if (ret) {
		MVNETA_LOG(ERR, "Failed to change MTU");
		return ret;
	}
	MVNETA_LOG(INFO, "MTU changed to %u, MRU = %u", mtu, mru);

	return 0;
}

/**
 * DPDK callback to bring the link up.
 *
 * @param dev
 *   Pointer to Ethernet device structure.
 *
 * @return
 *   0 on success, negative error value otherwise.
 */
static int
mvneta_dev_set_link_up(struct rte_eth_dev *dev)
{
	struct mvneta_priv *priv = dev->data->dev_private;

	if (!priv->ppio)
		return 0;

	return neta_ppio_enable(priv->ppio);
}

/**
 * DPDK callback to bring the link down.
 *
 * @param dev
 *   Pointer to Ethernet device structure.
 *
 * @return
 *   0 on success, negative error value otherwise.
 */
static int
mvneta_dev_set_link_down(struct rte_eth_dev *dev)
{
	struct mvneta_priv *priv = dev->data->dev_private;

	if (!priv->ppio)
		return 0;

	return neta_ppio_disable(priv->ppio);
}

/**
 * DPDK callback to start the device.
 *
 * @param dev
 *   Pointer to Ethernet device structure.
 *
 * @return
 *   0 on success, negative errno value on failure.
 */
static int
mvneta_dev_start(struct rte_eth_dev *dev)
{
	struct mvneta_priv *priv = dev->data->dev_private;
	char match[MVNETA_MATCH_LEN];
	int ret = 0, i;

	if (priv->ppio)
		return mvneta_dev_set_link_up(dev);

	snprintf(match, sizeof(match), "%s", dev->data->name);
	priv->ppio_params.match = match;
	priv->ppio_params.inqs_params.mtu = dev->data->mtu;

	ret = neta_ppio_init(&priv->ppio_params, &priv->ppio);
	if (ret) {
		MVNETA_LOG(ERR, "Failed to init ppio");
		return ret;
	}
	priv->ppio_id = priv->ppio->port_id;

	/*
	 * In case there are some some stale uc/mc mac addresses flush them
	 * here. It cannot be done during mvneta_dev_close() as port information
	 * is already gone at that point (due to neta_ppio_deinit() in
	 * mvneta_dev_stop()).
	 */
	if (!priv->uc_mc_flushed) {
		ret = neta_ppio_flush_mac_addrs(priv->ppio, 0, 1);
		if (ret) {
			MVNETA_LOG(ERR,
				"Failed to flush uc/mc filter list");
			goto out;
		}
		priv->uc_mc_flushed = 1;
	}

	ret = mvneta_alloc_rx_bufs(dev);
	if (ret)
		goto out;

	ret = mvneta_mtu_set(dev, dev->data->mtu);
	if (ret) {
		MVNETA_LOG(ERR, "Failed to set MTU %d", dev->data->mtu);
		goto out;
	}

	ret = mvneta_dev_set_link_up(dev);
	if (ret) {
		MVNETA_LOG(ERR, "Failed to set link up");
		goto out;
	}

	/* start tx queues */
	for (i = 0; i < dev->data->nb_tx_queues; i++)
		dev->data->tx_queue_state[i] = RTE_ETH_QUEUE_STATE_STARTED;

	mvneta_set_tx_function(dev);

	return 0;

out:
	MVNETA_LOG(ERR, "Failed to start device");
	neta_ppio_deinit(priv->ppio);
	return ret;
}

/**
 * DPDK callback to stop the device.
 *
 * @param dev
 *   Pointer to Ethernet device structure.
 */
static void
mvneta_dev_stop(struct rte_eth_dev *dev)
{
	struct mvneta_priv *priv = dev->data->dev_private;

	if (!priv->ppio)
		return;

	mvneta_dev_set_link_down(dev);
	mvneta_flush_queues(dev);
	neta_ppio_deinit(priv->ppio);

	priv->ppio = NULL;
}

/**
 * DPDK callback to close the device.
 *
 * @param dev
 *   Pointer to Ethernet device structure.
 */
static void
mvneta_dev_close(struct rte_eth_dev *dev)
{
	struct mvneta_priv *priv = dev->data->dev_private;
	int i;

	if (priv->ppio)
		mvneta_dev_stop(dev);

	for (i = 0; i < dev->data->nb_rx_queues; i++) {
		mvneta_rx_queue_release(dev->data->rx_queues[i]);
		dev->data->rx_queues[i] = NULL;
	}

	for (i = 0; i < dev->data->nb_tx_queues; i++) {
		mvneta_tx_queue_release(dev->data->tx_queues[i]);
		dev->data->tx_queues[i] = NULL;
	}
}

/**
 * DPDK callback to retrieve physical link information.
 *
 * @param dev
 *   Pointer to Ethernet device structure.
 * @param wait_to_complete
 *   Wait for request completion (ignored).
 *
 * @return
 *   0 on success, negative error value otherwise.
 */
static int
mvneta_link_update(struct rte_eth_dev *dev, int wait_to_complete __rte_unused)
{
	/*
	 * TODO
	 * once MUSDK provides necessary API use it here
	 */
	struct mvneta_priv *priv = dev->data->dev_private;
	struct ethtool_cmd edata;
	struct ifreq req;
	int ret, fd, link_up;

	if (!priv->ppio)
		return -EPERM;

	edata.cmd = ETHTOOL_GSET;

	strcpy(req.ifr_name, dev->data->name);
	req.ifr_data = (void *)&edata;

	fd = socket(AF_INET, SOCK_DGRAM, 0);
	if (fd == -1)
		return -EFAULT;
	ret = ioctl(fd, SIOCETHTOOL, &req);
	if (ret == -1) {
		close(fd);
		return -EFAULT;
	}

	close(fd);

	switch (ethtool_cmd_speed(&edata)) {
	case SPEED_10:
		dev->data->dev_link.link_speed = ETH_SPEED_NUM_10M;
		break;
	case SPEED_100:
		dev->data->dev_link.link_speed = ETH_SPEED_NUM_100M;
		break;
	case SPEED_1000:
		dev->data->dev_link.link_speed = ETH_SPEED_NUM_1G;
		break;
	case SPEED_2500:
		dev->data->dev_link.link_speed = ETH_SPEED_NUM_2_5G;
		break;
	default:
		dev->data->dev_link.link_speed = ETH_SPEED_NUM_NONE;
	}

	dev->data->dev_link.link_duplex = edata.duplex ? ETH_LINK_FULL_DUPLEX :
							 ETH_LINK_HALF_DUPLEX;
	dev->data->dev_link.link_autoneg = edata.autoneg ? ETH_LINK_AUTONEG :
							   ETH_LINK_FIXED;

	neta_ppio_get_link_state(priv->ppio, &link_up);
	dev->data->dev_link.link_status = link_up ? ETH_LINK_UP : ETH_LINK_DOWN;

	return 0;
}

/**
 * DPDK callback to enable promiscuous mode.
 *
 * @param dev
 *   Pointer to Ethernet device structure.
 */
static void
mvneta_promiscuous_enable(struct rte_eth_dev *dev)
{
	struct mvneta_priv *priv = dev->data->dev_private;
	int ret, en;

	if (!priv->ppio)
		return;

	neta_ppio_get_promisc(priv->ppio, &en);
	if (en) {
		MVNETA_LOG(INFO, "Promiscuous already enabled");
		return;
	}

	ret = neta_ppio_set_promisc(priv->ppio, 1);
	if (ret)
		MVNETA_LOG(ERR, "Failed to enable promiscuous mode");
}

/**
 * DPDK callback to disable allmulticast mode.
 *
 * @param dev
 *   Pointer to Ethernet device structure.
 */
static void
mvneta_promiscuous_disable(struct rte_eth_dev *dev)
{
	struct mvneta_priv *priv = dev->data->dev_private;
	int ret, en;

	if (!priv->ppio)
		return;

	neta_ppio_get_promisc(priv->ppio, &en);
	if (!en) {
		MVNETA_LOG(INFO, "Promiscuous already disabled");
		return;
	}

	ret = neta_ppio_set_promisc(priv->ppio, 0);
	if (ret)
		MVNETA_LOG(ERR, "Failed to disable promiscuous mode");
}

/**
 * DPDK callback to remove a MAC address.
 *
 * @param dev
 *   Pointer to Ethernet device structure.
 * @param index
 *   MAC address index.
 */
static void
mvneta_mac_addr_remove(struct rte_eth_dev *dev, uint32_t index)
{
	struct mvneta_priv *priv = dev->data->dev_private;
	char buf[ETHER_ADDR_FMT_SIZE];
	int ret;

	if (!priv->ppio)
		return;

	ret = neta_ppio_remove_mac_addr(priv->ppio,
				       dev->data->mac_addrs[index].addr_bytes);
	if (ret) {
		ether_format_addr(buf, sizeof(buf),
				  &dev->data->mac_addrs[index]);
		MVNETA_LOG(ERR, "Failed to remove mac %s", buf);
	}
}

/**
 * DPDK callback to add a MAC address.
 *
 * @param dev
 *   Pointer to Ethernet device structure.
 * @param mac_addr
 *   MAC address to register.
 * @param index
 *   MAC address index.
 * @param vmdq
 *   VMDq pool index to associate address with (unused).
 *
 * @return
 *   0 on success, negative error value otherwise.
 */
static int
mvneta_mac_addr_add(struct rte_eth_dev *dev, struct ether_addr *mac_addr,
		  uint32_t index, uint32_t vmdq __rte_unused)
{
	struct mvneta_priv *priv = dev->data->dev_private;
	char buf[ETHER_ADDR_FMT_SIZE];
	int ret;

	if (index == 0)
		/* For setting index 0, mrvl_mac_addr_set() should be used.*/
		return -1;

	if (!priv->ppio)
		return 0;

	ret = neta_ppio_add_mac_addr(priv->ppio, mac_addr->addr_bytes);
	if (ret) {
		ether_format_addr(buf, sizeof(buf), mac_addr);
		MVNETA_LOG(ERR, "Failed to add mac %s", buf);
		return -1;
	}

	return 0;
}

/**
 * DPDK callback to set the primary MAC address.
 *
 * @param dev
 *   Pointer to Ethernet device structure.
 * @param mac_addr
 *   MAC address to register.
 */
static int
mvneta_mac_addr_set(struct rte_eth_dev *dev, struct ether_addr *mac_addr)
{
	struct mvneta_priv *priv = dev->data->dev_private;
	int ret;

	if (!priv->ppio)
		return -EINVAL;

	ret = neta_ppio_set_mac_addr(priv->ppio, mac_addr->addr_bytes);
	if (ret) {
		char buf[ETHER_ADDR_FMT_SIZE];
		ether_format_addr(buf, sizeof(buf), mac_addr);
		MVNETA_LOG(ERR, "Failed to set mac to %s", buf);
	}
	return 0;
}

/**
 * DPDK callback to get device statistics.
 *
 * @param dev
 *   Pointer to Ethernet device structure.
 * @param stats
 *   Stats structure output buffer.
 *
 * @return
 *   0 on success, negative error value otherwise.
 */
static int
mvneta_stats_get(struct rte_eth_dev *dev, struct rte_eth_stats *stats)
{
	struct mvneta_priv *priv = dev->data->dev_private;
	struct neta_ppio_statistics ppio_stats;
	unsigned int ret;

	if (!priv->ppio)
		return -EPERM;

	ret = neta_ppio_get_statistics(priv->ppio, &ppio_stats);
	if (unlikely(ret)) {
		MVNETA_LOG(ERR, "Failed to update port statistics");
		return ret;
	}

	stats->ipackets += ppio_stats.rx_packets +
			ppio_stats.rx_broadcast_packets +
			ppio_stats.rx_multicast_packets -
			priv->prev_stats.ipackets;
	stats->opackets += ppio_stats.tx_packets +
			ppio_stats.tx_broadcast_packets +
			ppio_stats.tx_multicast_packets -
			priv->prev_stats.opackets;
	stats->ibytes += ppio_stats.rx_bytes - priv->prev_stats.ibytes;
	stats->obytes += ppio_stats.tx_bytes - priv->prev_stats.obytes;
	stats->imissed += ppio_stats.rx_discard +
			  ppio_stats.rx_overrun -
			  priv->prev_stats.imissed;

	stats->ierrors = ppio_stats.rx_packets_err +
			ppio_stats.rx_errors +
			ppio_stats.rx_crc_error -
			priv->prev_stats.ierrors;
	stats->oerrors = ppio_stats.tx_errors - priv->prev_stats.oerrors;

	return 0;
}

/**
 * DPDK callback to clear device statistics.
 *
 * @param dev
 *   Pointer to Ethernet device structure.
 */
static void
mvneta_stats_reset(struct rte_eth_dev *dev)
{
	struct mvneta_priv *priv = dev->data->dev_private;
	unsigned int ret;

	if (!priv->ppio)
		return;

	ret = mvneta_stats_get(dev, &priv->prev_stats);
	if (unlikely(ret))
		RTE_LOG(ERR, PMD, "Failed to reset port statistics");
}


static const struct eth_dev_ops mvneta_ops = {
	.dev_configure = mvneta_dev_configure,
	.dev_start = mvneta_dev_start,
	.dev_stop = mvneta_dev_stop,
	.dev_set_link_up = mvneta_dev_set_link_up,
	.dev_set_link_down = mvneta_dev_set_link_down,
	.dev_close = mvneta_dev_close,
	.link_update = mvneta_link_update,
	.promiscuous_enable = mvneta_promiscuous_enable,
	.promiscuous_disable = mvneta_promiscuous_disable,
	.mac_addr_remove = mvneta_mac_addr_remove,
	.mac_addr_add = mvneta_mac_addr_add,
	.mac_addr_set = mvneta_mac_addr_set,
	.mtu_set = mvneta_mtu_set,
	.stats_get = mvneta_stats_get,
	.stats_reset = mvneta_stats_reset,
	.dev_infos_get = mvneta_dev_infos_get,
	.dev_supported_ptypes_get = mvneta_dev_supported_ptypes_get,
	.rxq_info_get = mvneta_rxq_info_get,
	.txq_info_get = mvneta_txq_info_get,
	.rx_queue_setup = mvneta_rx_queue_setup,
	.rx_queue_release = mvneta_rx_queue_release,
	.tx_queue_setup = mvneta_tx_queue_setup,
	.tx_queue_release = mvneta_tx_queue_release,
};

/**
 * Create device representing Ethernet port.
 *
 * @param name
 *   Pointer to the port's name.
 *
 * @return
 *   0 on success, negative error value otherwise.
 */
static int
mvneta_eth_dev_create(struct rte_vdev_device *vdev, const char *name)
{
	int ret, fd = socket(AF_INET, SOCK_DGRAM, 0);
	struct rte_eth_dev *eth_dev;
	struct mvneta_priv *priv;
	struct ifreq req;

	eth_dev = rte_eth_dev_allocate(name);
	if (!eth_dev)
		return -ENOMEM;

	priv = rte_zmalloc_socket(name, sizeof(*priv), 0, rte_socket_id());
	if (!priv) {
		ret = -ENOMEM;
		goto out_free;
	}
	eth_dev->data->dev_private = priv;

	eth_dev->data->mac_addrs =
		rte_zmalloc("mac_addrs",
			    ETHER_ADDR_LEN * MVNETA_MAC_ADDRS_MAX, 0);
	if (!eth_dev->data->mac_addrs) {
		MVNETA_LOG(ERR, "Failed to allocate space for eth addrs");
		ret = -ENOMEM;
		goto out_free;
	}

	memset(&req, 0, sizeof(req));
	strcpy(req.ifr_name, name);
	ret = ioctl(fd, SIOCGIFHWADDR, &req);
	if (ret)
		goto out_free;

	memcpy(eth_dev->data->mac_addrs[0].addr_bytes,
	       req.ifr_addr.sa_data, ETHER_ADDR_LEN);

	eth_dev->data->kdrv = RTE_KDRV_NONE;
	eth_dev->device = &vdev->device;
	eth_dev->rx_pkt_burst = mvneta_rx_pkt_burst;
	mvneta_set_tx_function(eth_dev);
	eth_dev->dev_ops = &mvneta_ops;

	rte_eth_dev_probing_finish(eth_dev);
	return 0;
out_free:
	rte_eth_dev_release_port(eth_dev);

	return ret;
}

/**
 * Cleanup previously created device representing Ethernet port.
 *
 * @param eth_dev
 *   Pointer to the corresponding rte_eth_dev structure.
 */
static void
mvneta_eth_dev_destroy(struct rte_eth_dev *eth_dev)
{
	rte_eth_dev_release_port(eth_dev);
}

/**
 * Cleanup previously created device representing Ethernet port.
 *
 * @param name
 *   Pointer to the port name.
 */
static void
mvneta_eth_dev_destroy_name(const char *name)
{
	struct rte_eth_dev *eth_dev;

	eth_dev = rte_eth_dev_allocated(name);
	if (!eth_dev)
		return;

	mvneta_eth_dev_destroy(eth_dev);
}

/**
 * DPDK callback to register the virtual device.
 *
 * @param vdev
 *   Pointer to the virtual device.
 *
 * @return
 *   0 on success, negative error value otherwise.
 */
static int
rte_pmd_mvneta_probe(struct rte_vdev_device *vdev)
{
	struct rte_kvargs *kvlist;
	struct mvneta_ifnames ifnames;
	int ret = -EINVAL;
	uint32_t i, ifnum;
	const char *params;

	params = rte_vdev_device_args(vdev);
	if (!params)
		return -EINVAL;

	kvlist = rte_kvargs_parse(params, valid_args);
	if (!kvlist)
		return -EINVAL;

	ifnum = rte_kvargs_count(kvlist, MVNETA_IFACE_NAME_ARG);
	if (ifnum > RTE_DIM(ifnames.names))
		goto out_free_kvlist;

	ifnames.idx = 0;
	rte_kvargs_process(kvlist, MVNETA_IFACE_NAME_ARG,
			   mvneta_ifnames_get, &ifnames);

	/*
	 * The below system initialization should be done only once,
	 * on the first provided configuration file
	 */
	if (mvneta_dev_num)
		goto init_devices;

	MVNETA_LOG(INFO, "Perform MUSDK initializations");

	ret = rte_mvep_init(MVEP_MOD_T_NETA, kvlist);
	if (ret)
		goto out_free_kvlist;

	ret = mvneta_neta_init();
	if (ret) {
		MVNETA_LOG(ERR, "Failed to init NETA!");
		rte_mvep_deinit(MVEP_MOD_T_NETA);
		goto out_free_kvlist;
	}

init_devices:
	for (i = 0; i < ifnum; i++) {
		MVNETA_LOG(INFO, "Creating %s", ifnames.names[i]);
		ret = mvneta_eth_dev_create(vdev, ifnames.names[i]);
		if (ret)
			goto out_cleanup;
	}
	mvneta_dev_num += ifnum;

	rte_kvargs_free(kvlist);

	return 0;
out_cleanup:
	for (; i > 0; i--)
		mvneta_eth_dev_destroy_name(ifnames.names[i]);

	if (mvneta_dev_num == 0) {
		mvneta_neta_deinit();
		rte_mvep_deinit(MVEP_MOD_T_NETA);
	}
out_free_kvlist:
	rte_kvargs_free(kvlist);

	return ret;
}

/**
 * DPDK callback to remove virtual device.
 *
 * @param vdev
 *   Pointer to the removed virtual device.
 *
 * @return
 *   0 on success, negative error value otherwise.
 */
static int
rte_pmd_mvneta_remove(struct rte_vdev_device *vdev)
{
	int i;
	const char *name;

	name = rte_vdev_device_name(vdev);
	if (!name)
		return -EINVAL;

	MVNETA_LOG(INFO, "Removing %s", name);

	RTE_ETH_FOREACH_DEV(i) {
		if (rte_eth_devices[i].device != &vdev->device)
			continue;

		mvneta_eth_dev_destroy(&rte_eth_devices[i]);
		mvneta_dev_num--;
	}

	if (mvneta_dev_num == 0) {
		MVNETA_LOG(INFO, "Perform MUSDK deinit");
		mvneta_neta_deinit();
		rte_mvep_deinit(MVEP_MOD_T_NETA);
	}

	return 0;
}

static struct rte_vdev_driver pmd_mvneta_drv = {
	.probe = rte_pmd_mvneta_probe,
	.remove = rte_pmd_mvneta_remove,
};

RTE_PMD_REGISTER_VDEV(net_mvneta, pmd_mvneta_drv);
RTE_PMD_REGISTER_PARAM_STRING(net_mvneta, "iface=<ifc>");

RTE_INIT(mvneta_init_log)
{
	mvneta_logtype = rte_log_register("pmd.net.mvneta");
	if (mvneta_logtype >= 0)
		rte_log_set_level(mvneta_logtype, RTE_LOG_NOTICE);
}