xref: /dpdk/drivers/net/mvneta/mvneta_ethdev.c (revision f5057be340e44f3edc0fe90fa875eb89a4c49b4f)
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2018 Marvell International Ltd.
3  * Copyright(c) 2018 Semihalf.
4  * All rights reserved.
5  */
6 
7 #include <rte_string_fns.h>
8 #include <rte_ethdev_driver.h>
9 #include <rte_kvargs.h>
10 #include <rte_bus_vdev.h>
11 
12 #include <stdio.h>
13 #include <fcntl.h>
14 #include <linux/ethtool.h>
15 #include <linux/sockios.h>
16 #include <net/if.h>
17 #include <net/if_arp.h>
18 #include <sys/ioctl.h>
19 #include <sys/socket.h>
20 #include <sys/stat.h>
21 #include <sys/types.h>
22 
23 #include <rte_mvep_common.h>
24 
25 #include "mvneta_rxtx.h"
26 
27 
28 #define MVNETA_IFACE_NAME_ARG "iface"
29 
30 #define MVNETA_PKT_SIZE_MAX (16382 - MV_MH_SIZE) /* 9700B */
31 #define MVNETA_DEFAULT_MTU 1500
32 
33 #define MVNETA_MAC_ADDRS_MAX 256 /*16 UC, 256 IP, 256 MC/BC */
34 /** Maximum length of a match string */
35 #define MVNETA_MATCH_LEN 16
36 
37 static const char * const valid_args[] = {
38 	MVNETA_IFACE_NAME_ARG,
39 	NULL
40 };
41 
42 struct mvneta_ifnames {
43 	const char *names[NETA_NUM_ETH_PPIO];
44 	int idx;
45 };
46 
47 static int mvneta_dev_num;
48 
49 static int mvneta_stats_reset(struct rte_eth_dev *dev);
50 static int rte_pmd_mvneta_remove(struct rte_vdev_device *vdev);
51 
52 
53 /**
54  * Deinitialize packet processor.
55  */
56 static void
57 mvneta_neta_deinit(void)
58 {
59 	neta_deinit();
60 }
61 
62 /**
63  * Initialize packet processor.
64  *
65  * @return
66  *   0 on success, negative error value otherwise.
67  */
68 static int
69 mvneta_neta_init(void)
70 {
71 	return neta_init();
72 }
73 
74 /**
75  * Callback used by rte_kvargs_process() during argument parsing.
76  *
77  * @param key
78  *   Pointer to the parsed key (unused).
79  * @param value
80  *   Pointer to the parsed value.
81  * @param extra_args
82  *   Pointer to the extra arguments which contains address of the
83  *   table of pointers to parsed interface names.
84  *
85  * @return
86  *   Always 0.
87  */
88 static int
89 mvneta_ifnames_get(const char *key __rte_unused, const char *value,
90 		 void *extra_args)
91 {
92 	struct mvneta_ifnames *ifnames = extra_args;
93 
94 	ifnames->names[ifnames->idx++] = value;
95 
96 	return 0;
97 }
98 
99 /**
100  * Ethernet device configuration.
101  *
102  * Prepare the driver for a given number of TX and RX queues and
103  * configure RSS if supported.
104  *
105  * @param dev
106  *   Pointer to Ethernet device structure.
107  *
108  * @return
109  *   0 on success, negative error value otherwise.
110  */
111 static int
112 mvneta_dev_configure(struct rte_eth_dev *dev)
113 {
114 	struct mvneta_priv *priv = dev->data->dev_private;
115 	struct neta_ppio_params *ppio_params;
116 
117 	if (dev->data->dev_conf.rxmode.mq_mode != ETH_MQ_RX_NONE) {
118 		MVNETA_LOG(INFO, "Unsupported RSS and rx multi queue mode %d",
119 			dev->data->dev_conf.rxmode.mq_mode);
120 		if (dev->data->nb_rx_queues > 1)
121 			return -EINVAL;
122 	}
123 
124 	if (dev->data->dev_conf.rxmode.split_hdr_size) {
125 		MVNETA_LOG(INFO, "Split headers not supported");
126 		return -EINVAL;
127 	}
128 
129 	if (dev->data->dev_conf.rxmode.offloads & DEV_RX_OFFLOAD_JUMBO_FRAME)
130 		dev->data->mtu = dev->data->dev_conf.rxmode.max_rx_pkt_len -
131 				 MRVL_NETA_ETH_HDRS_LEN;
132 
133 	if (dev->data->dev_conf.txmode.offloads & DEV_TX_OFFLOAD_MULTI_SEGS)
134 		priv->multiseg = 1;
135 
136 	ppio_params = &priv->ppio_params;
137 	ppio_params->outqs_params.num_outqs = dev->data->nb_tx_queues;
138 	/* Default: 1 TC, no QoS supported. */
139 	ppio_params->inqs_params.num_tcs = 1;
140 	ppio_params->inqs_params.tcs_params[0].pkt_offset = MRVL_NETA_PKT_OFFS;
141 	priv->ppio_id = dev->data->port_id;
142 
143 	return 0;
144 }
145 
146 /**
147  * DPDK callback to get information about the device.
148  *
149  * @param dev
150  *   Pointer to Ethernet device structure (unused).
151  * @param info
152  *   Info structure output buffer.
153  */
154 static int
155 mvneta_dev_infos_get(struct rte_eth_dev *dev __rte_unused,
156 		   struct rte_eth_dev_info *info)
157 {
158 	info->speed_capa = ETH_LINK_SPEED_10M |
159 			   ETH_LINK_SPEED_100M |
160 			   ETH_LINK_SPEED_1G |
161 			   ETH_LINK_SPEED_2_5G;
162 
163 	info->max_rx_queues = MRVL_NETA_RXQ_MAX;
164 	info->max_tx_queues = MRVL_NETA_TXQ_MAX;
165 	info->max_mac_addrs = MVNETA_MAC_ADDRS_MAX;
166 
167 	info->rx_desc_lim.nb_max = MRVL_NETA_RXD_MAX;
168 	info->rx_desc_lim.nb_min = MRVL_NETA_RXD_MIN;
169 	info->rx_desc_lim.nb_align = MRVL_NETA_RXD_ALIGN;
170 
171 	info->tx_desc_lim.nb_max = MRVL_NETA_TXD_MAX;
172 	info->tx_desc_lim.nb_min = MRVL_NETA_TXD_MIN;
173 	info->tx_desc_lim.nb_align = MRVL_NETA_TXD_ALIGN;
174 
175 	info->rx_offload_capa = MVNETA_RX_OFFLOADS;
176 	info->rx_queue_offload_capa = MVNETA_RX_OFFLOADS;
177 
178 	info->tx_offload_capa =  MVNETA_TX_OFFLOADS;
179 	info->tx_queue_offload_capa =  MVNETA_TX_OFFLOADS;
180 
181 	/* By default packets are dropped if no descriptors are available */
182 	info->default_rxconf.rx_drop_en = 1;
183 	/* Deferred tx queue start is not supported */
184 	info->default_txconf.tx_deferred_start = 0;
185 	info->default_txconf.offloads = 0;
186 
187 	info->max_rx_pktlen = MVNETA_PKT_SIZE_MAX;
188 
189 	return 0;
190 }
191 
192 /**
193  * Return supported packet types.
194  *
195  * @param dev
196  *   Pointer to Ethernet device structure (unused).
197  *
198  * @return
199  *   Const pointer to the table with supported packet types.
200  */
201 static const uint32_t *
202 mvneta_dev_supported_ptypes_get(struct rte_eth_dev *dev __rte_unused)
203 {
204 	static const uint32_t ptypes[] = {
205 		RTE_PTYPE_L2_ETHER,
206 		RTE_PTYPE_L2_ETHER_VLAN,
207 		RTE_PTYPE_L3_IPV4,
208 		RTE_PTYPE_L3_IPV6,
209 		RTE_PTYPE_L4_TCP,
210 		RTE_PTYPE_L4_UDP
211 	};
212 
213 	return ptypes;
214 }
215 
216 /**
217  * DPDK callback to change the MTU.
218  *
219  * Setting the MTU affects hardware MRU (packets larger than the MRU
220  * will be dropped).
221  *
222  * @param dev
223  *   Pointer to Ethernet device structure.
224  * @param mtu
225  *   New MTU.
226  *
227  * @return
228  *   0 on success, negative error value otherwise.
229  */
230 static int
231 mvneta_mtu_set(struct rte_eth_dev *dev, uint16_t mtu)
232 {
233 	struct mvneta_priv *priv = dev->data->dev_private;
234 	uint16_t mbuf_data_size = 0; /* SW buffer size */
235 	uint16_t mru;
236 	int ret;
237 
238 	mru = MRVL_NETA_MTU_TO_MRU(mtu);
239 	/*
240 	 * min_rx_buf_size is equal to mbuf data size
241 	 * if pmd didn't set it differently
242 	 */
243 	mbuf_data_size = dev->data->min_rx_buf_size - RTE_PKTMBUF_HEADROOM;
244 	/* Prevent PMD from:
245 	 * - setting mru greater than the mbuf size resulting in
246 	 * hw and sw buffer size mismatch
247 	 * - setting mtu that requires the support of scattered packets
248 	 * when this feature has not been enabled/supported so far.
249 	 */
250 	if (!dev->data->scattered_rx &&
251 	    (mru + MRVL_NETA_PKT_OFFS > mbuf_data_size)) {
252 		mru = mbuf_data_size - MRVL_NETA_PKT_OFFS;
253 		mtu = MRVL_NETA_MRU_TO_MTU(mru);
254 		MVNETA_LOG(WARNING, "MTU too big, max MTU possible limitted by"
255 			" current mbuf size: %u. Set MTU to %u, MRU to %u",
256 			mbuf_data_size, mtu, mru);
257 	}
258 
259 	if (mtu < RTE_ETHER_MIN_MTU || mru > MVNETA_PKT_SIZE_MAX) {
260 		MVNETA_LOG(ERR, "Invalid MTU [%u] or MRU [%u]", mtu, mru);
261 		return -EINVAL;
262 	}
263 
264 	dev->data->mtu = mtu;
265 	dev->data->dev_conf.rxmode.max_rx_pkt_len = mru - MV_MH_SIZE;
266 
267 	if (!priv->ppio)
268 		/* It is OK. New MTU will be set later on mvneta_dev_start */
269 		return 0;
270 
271 	ret = neta_ppio_set_mru(priv->ppio, mru);
272 	if (ret) {
273 		MVNETA_LOG(ERR, "Failed to change MRU");
274 		return ret;
275 	}
276 
277 	ret = neta_ppio_set_mtu(priv->ppio, mtu);
278 	if (ret) {
279 		MVNETA_LOG(ERR, "Failed to change MTU");
280 		return ret;
281 	}
282 	MVNETA_LOG(INFO, "MTU changed to %u, MRU = %u", mtu, mru);
283 
284 	return 0;
285 }
286 
287 /**
288  * DPDK callback to bring the link up.
289  *
290  * @param dev
291  *   Pointer to Ethernet device structure.
292  *
293  * @return
294  *   0 on success, negative error value otherwise.
295  */
296 static int
297 mvneta_dev_set_link_up(struct rte_eth_dev *dev)
298 {
299 	struct mvneta_priv *priv = dev->data->dev_private;
300 
301 	if (!priv->ppio)
302 		return 0;
303 
304 	return neta_ppio_enable(priv->ppio);
305 }
306 
307 /**
308  * DPDK callback to bring the link down.
309  *
310  * @param dev
311  *   Pointer to Ethernet device structure.
312  *
313  * @return
314  *   0 on success, negative error value otherwise.
315  */
316 static int
317 mvneta_dev_set_link_down(struct rte_eth_dev *dev)
318 {
319 	struct mvneta_priv *priv = dev->data->dev_private;
320 
321 	if (!priv->ppio)
322 		return 0;
323 
324 	return neta_ppio_disable(priv->ppio);
325 }
326 
327 /**
328  * DPDK callback to start the device.
329  *
330  * @param dev
331  *   Pointer to Ethernet device structure.
332  *
333  * @return
334  *   0 on success, negative errno value on failure.
335  */
336 static int
337 mvneta_dev_start(struct rte_eth_dev *dev)
338 {
339 	struct mvneta_priv *priv = dev->data->dev_private;
340 	char match[MVNETA_MATCH_LEN];
341 	int ret = 0, i;
342 
343 	if (priv->ppio)
344 		return mvneta_dev_set_link_up(dev);
345 
346 	strlcpy(match, dev->data->name, sizeof(match));
347 	priv->ppio_params.match = match;
348 	priv->ppio_params.inqs_params.mtu = dev->data->mtu;
349 
350 	ret = neta_ppio_init(&priv->ppio_params, &priv->ppio);
351 	if (ret) {
352 		MVNETA_LOG(ERR, "Failed to init ppio");
353 		return ret;
354 	}
355 	priv->ppio_id = priv->ppio->port_id;
356 
357 	mvneta_stats_reset(dev);
358 
359 	/*
360 	 * In case there are some some stale uc/mc mac addresses flush them
361 	 * here. It cannot be done during mvneta_dev_close() as port information
362 	 * is already gone at that point (due to neta_ppio_deinit() in
363 	 * mvneta_dev_stop()).
364 	 */
365 	if (!priv->uc_mc_flushed) {
366 		ret = neta_ppio_flush_mac_addrs(priv->ppio, 0, 1);
367 		if (ret) {
368 			MVNETA_LOG(ERR,
369 				"Failed to flush uc/mc filter list");
370 			goto out;
371 		}
372 		priv->uc_mc_flushed = 1;
373 	}
374 
375 	ret = mvneta_alloc_rx_bufs(dev);
376 	if (ret)
377 		goto out;
378 
379 	ret = mvneta_mtu_set(dev, dev->data->mtu);
380 	if (ret) {
381 		MVNETA_LOG(ERR, "Failed to set MTU %d", dev->data->mtu);
382 		goto out;
383 	}
384 
385 	ret = mvneta_dev_set_link_up(dev);
386 	if (ret) {
387 		MVNETA_LOG(ERR, "Failed to set link up");
388 		goto out;
389 	}
390 
391 	/* start tx queues */
392 	for (i = 0; i < dev->data->nb_tx_queues; i++)
393 		dev->data->tx_queue_state[i] = RTE_ETH_QUEUE_STATE_STARTED;
394 
395 	mvneta_set_tx_function(dev);
396 
397 	return 0;
398 
399 out:
400 	MVNETA_LOG(ERR, "Failed to start device");
401 	neta_ppio_deinit(priv->ppio);
402 	return ret;
403 }
404 
405 /**
406  * DPDK callback to stop the device.
407  *
408  * @param dev
409  *   Pointer to Ethernet device structure.
410  */
411 static void
412 mvneta_dev_stop(struct rte_eth_dev *dev)
413 {
414 	struct mvneta_priv *priv = dev->data->dev_private;
415 
416 	if (!priv->ppio)
417 		return;
418 
419 	mvneta_dev_set_link_down(dev);
420 	mvneta_flush_queues(dev);
421 	neta_ppio_deinit(priv->ppio);
422 
423 	priv->ppio = NULL;
424 }
425 
426 /**
427  * DPDK callback to close the device.
428  *
429  * @param dev
430  *   Pointer to Ethernet device structure.
431  */
432 static int
433 mvneta_dev_close(struct rte_eth_dev *dev)
434 {
435 	struct mvneta_priv *priv = dev->data->dev_private;
436 	int i;
437 
438 	if (rte_eal_process_type() != RTE_PROC_PRIMARY)
439 		return 0;
440 
441 	if (priv->ppio)
442 		mvneta_dev_stop(dev);
443 
444 	for (i = 0; i < dev->data->nb_rx_queues; i++) {
445 		mvneta_rx_queue_release(dev->data->rx_queues[i]);
446 		dev->data->rx_queues[i] = NULL;
447 	}
448 
449 	for (i = 0; i < dev->data->nb_tx_queues; i++) {
450 		mvneta_tx_queue_release(dev->data->tx_queues[i]);
451 		dev->data->tx_queues[i] = NULL;
452 	}
453 
454 	mvneta_dev_num--;
455 
456 	if (mvneta_dev_num == 0) {
457 		MVNETA_LOG(INFO, "Perform MUSDK deinit");
458 		mvneta_neta_deinit();
459 		rte_mvep_deinit(MVEP_MOD_T_NETA);
460 	}
461 
462 	return 0;
463 }
464 
465 /**
466  * DPDK callback to retrieve physical link information.
467  *
468  * @param dev
469  *   Pointer to Ethernet device structure.
470  * @param wait_to_complete
471  *   Wait for request completion (ignored).
472  *
473  * @return
474  *   0 on success, negative error value otherwise.
475  */
476 static int
477 mvneta_link_update(struct rte_eth_dev *dev, int wait_to_complete __rte_unused)
478 {
479 	/*
480 	 * TODO
481 	 * once MUSDK provides necessary API use it here
482 	 */
483 	struct mvneta_priv *priv = dev->data->dev_private;
484 	struct ethtool_cmd edata;
485 	struct ifreq req;
486 	int ret, fd, link_up;
487 
488 	if (!priv->ppio)
489 		return -EPERM;
490 
491 	edata.cmd = ETHTOOL_GSET;
492 
493 	strcpy(req.ifr_name, dev->data->name);
494 	req.ifr_data = (void *)&edata;
495 
496 	fd = socket(AF_INET, SOCK_DGRAM, 0);
497 	if (fd == -1)
498 		return -EFAULT;
499 	ret = ioctl(fd, SIOCETHTOOL, &req);
500 	if (ret == -1) {
501 		close(fd);
502 		return -EFAULT;
503 	}
504 
505 	close(fd);
506 
507 	switch (ethtool_cmd_speed(&edata)) {
508 	case SPEED_10:
509 		dev->data->dev_link.link_speed = ETH_SPEED_NUM_10M;
510 		break;
511 	case SPEED_100:
512 		dev->data->dev_link.link_speed = ETH_SPEED_NUM_100M;
513 		break;
514 	case SPEED_1000:
515 		dev->data->dev_link.link_speed = ETH_SPEED_NUM_1G;
516 		break;
517 	case SPEED_2500:
518 		dev->data->dev_link.link_speed = ETH_SPEED_NUM_2_5G;
519 		break;
520 	default:
521 		dev->data->dev_link.link_speed = ETH_SPEED_NUM_NONE;
522 	}
523 
524 	dev->data->dev_link.link_duplex = edata.duplex ? ETH_LINK_FULL_DUPLEX :
525 							 ETH_LINK_HALF_DUPLEX;
526 	dev->data->dev_link.link_autoneg = edata.autoneg ? ETH_LINK_AUTONEG :
527 							   ETH_LINK_FIXED;
528 
529 	neta_ppio_get_link_state(priv->ppio, &link_up);
530 	dev->data->dev_link.link_status = link_up ? ETH_LINK_UP : ETH_LINK_DOWN;
531 
532 	return 0;
533 }
534 
535 /**
536  * DPDK callback to enable promiscuous mode.
537  *
538  * @param dev
539  *   Pointer to Ethernet device structure.
540  *
541  * @return
542  *   always 0
543  */
544 static int
545 mvneta_promiscuous_enable(struct rte_eth_dev *dev)
546 {
547 	struct mvneta_priv *priv = dev->data->dev_private;
548 	int ret, en;
549 
550 	if (!priv->ppio)
551 		return 0;
552 
553 	neta_ppio_get_promisc(priv->ppio, &en);
554 	if (en) {
555 		MVNETA_LOG(INFO, "Promiscuous already enabled");
556 		return 0;
557 	}
558 
559 	ret = neta_ppio_set_promisc(priv->ppio, 1);
560 	if (ret)
561 		MVNETA_LOG(ERR, "Failed to enable promiscuous mode");
562 
563 	return 0;
564 }
565 
566 /**
567  * DPDK callback to disable allmulticast mode.
568  *
569  * @param dev
570  *   Pointer to Ethernet device structure.
571  *
572  * @return
573  *   always 0
574  */
575 static int
576 mvneta_promiscuous_disable(struct rte_eth_dev *dev)
577 {
578 	struct mvneta_priv *priv = dev->data->dev_private;
579 	int ret, en;
580 
581 	if (!priv->ppio)
582 		return 0;
583 
584 	neta_ppio_get_promisc(priv->ppio, &en);
585 	if (!en) {
586 		MVNETA_LOG(INFO, "Promiscuous already disabled");
587 		return 0;
588 	}
589 
590 	ret = neta_ppio_set_promisc(priv->ppio, 0);
591 	if (ret)
592 		MVNETA_LOG(ERR, "Failed to disable promiscuous mode");
593 
594 	return 0;
595 }
596 
597 /**
598  * DPDK callback to remove a MAC address.
599  *
600  * @param dev
601  *   Pointer to Ethernet device structure.
602  * @param index
603  *   MAC address index.
604  */
605 static void
606 mvneta_mac_addr_remove(struct rte_eth_dev *dev, uint32_t index)
607 {
608 	struct mvneta_priv *priv = dev->data->dev_private;
609 	char buf[RTE_ETHER_ADDR_FMT_SIZE];
610 	int ret;
611 
612 	if (!priv->ppio)
613 		return;
614 
615 	ret = neta_ppio_remove_mac_addr(priv->ppio,
616 				       dev->data->mac_addrs[index].addr_bytes);
617 	if (ret) {
618 		rte_ether_format_addr(buf, sizeof(buf),
619 				  &dev->data->mac_addrs[index]);
620 		MVNETA_LOG(ERR, "Failed to remove mac %s", buf);
621 	}
622 }
623 
624 /**
625  * DPDK callback to add a MAC address.
626  *
627  * @param dev
628  *   Pointer to Ethernet device structure.
629  * @param mac_addr
630  *   MAC address to register.
631  * @param index
632  *   MAC address index.
633  * @param vmdq
634  *   VMDq pool index to associate address with (unused).
635  *
636  * @return
637  *   0 on success, negative error value otherwise.
638  */
639 static int
640 mvneta_mac_addr_add(struct rte_eth_dev *dev, struct rte_ether_addr *mac_addr,
641 		  uint32_t index, uint32_t vmdq __rte_unused)
642 {
643 	struct mvneta_priv *priv = dev->data->dev_private;
644 	char buf[RTE_ETHER_ADDR_FMT_SIZE];
645 	int ret;
646 
647 	if (index == 0)
648 		/* For setting index 0, mrvl_mac_addr_set() should be used.*/
649 		return -1;
650 
651 	if (!priv->ppio)
652 		return 0;
653 
654 	ret = neta_ppio_add_mac_addr(priv->ppio, mac_addr->addr_bytes);
655 	if (ret) {
656 		rte_ether_format_addr(buf, sizeof(buf), mac_addr);
657 		MVNETA_LOG(ERR, "Failed to add mac %s", buf);
658 		return -1;
659 	}
660 
661 	return 0;
662 }
663 
664 /**
665  * DPDK callback to set the primary MAC address.
666  *
667  * @param dev
668  *   Pointer to Ethernet device structure.
669  * @param mac_addr
670  *   MAC address to register.
671  */
672 static int
673 mvneta_mac_addr_set(struct rte_eth_dev *dev, struct rte_ether_addr *mac_addr)
674 {
675 	struct mvneta_priv *priv = dev->data->dev_private;
676 	int ret;
677 
678 	if (!priv->ppio)
679 		return -EINVAL;
680 
681 	ret = neta_ppio_set_mac_addr(priv->ppio, mac_addr->addr_bytes);
682 	if (ret) {
683 		char buf[RTE_ETHER_ADDR_FMT_SIZE];
684 		rte_ether_format_addr(buf, sizeof(buf), mac_addr);
685 		MVNETA_LOG(ERR, "Failed to set mac to %s", buf);
686 	}
687 	return 0;
688 }
689 
690 /**
691  * DPDK callback to get device statistics.
692  *
693  * @param dev
694  *   Pointer to Ethernet device structure.
695  * @param stats
696  *   Stats structure output buffer.
697  *
698  * @return
699  *   0 on success, negative error value otherwise.
700  */
701 static int
702 mvneta_stats_get(struct rte_eth_dev *dev, struct rte_eth_stats *stats)
703 {
704 	struct mvneta_priv *priv = dev->data->dev_private;
705 	struct neta_ppio_statistics ppio_stats;
706 	unsigned int ret;
707 
708 	if (!priv->ppio)
709 		return -EPERM;
710 
711 	ret = neta_ppio_get_statistics(priv->ppio, &ppio_stats);
712 	if (unlikely(ret)) {
713 		MVNETA_LOG(ERR, "Failed to update port statistics");
714 		return ret;
715 	}
716 
717 	stats->ipackets += ppio_stats.rx_packets +
718 			ppio_stats.rx_broadcast_packets +
719 			ppio_stats.rx_multicast_packets -
720 			priv->prev_stats.ipackets;
721 	stats->opackets += ppio_stats.tx_packets +
722 			ppio_stats.tx_broadcast_packets +
723 			ppio_stats.tx_multicast_packets -
724 			priv->prev_stats.opackets;
725 	stats->ibytes += ppio_stats.rx_bytes - priv->prev_stats.ibytes;
726 	stats->obytes += ppio_stats.tx_bytes - priv->prev_stats.obytes;
727 	stats->imissed += ppio_stats.rx_discard +
728 			  ppio_stats.rx_overrun -
729 			  priv->prev_stats.imissed;
730 	stats->ierrors = ppio_stats.rx_packets_err -
731 			priv->prev_stats.ierrors;
732 	stats->oerrors = ppio_stats.tx_errors - priv->prev_stats.oerrors;
733 
734 	return 0;
735 }
736 
737 /**
738  * DPDK callback to clear device statistics.
739  *
740  * @param dev
741  *   Pointer to Ethernet device structure.
742  *
743  * @return
744  *   0 on success, negative error value otherwise.
745  */
746 static int
747 mvneta_stats_reset(struct rte_eth_dev *dev)
748 {
749 	struct mvneta_priv *priv = dev->data->dev_private;
750 	unsigned int ret;
751 
752 	if (!priv->ppio)
753 		return 0;
754 
755 	ret = mvneta_stats_get(dev, &priv->prev_stats);
756 	if (unlikely(ret))
757 		MVNETA_LOG(ERR, "Failed to reset port statistics");
758 
759 	return ret;
760 }
761 
762 
763 static const struct eth_dev_ops mvneta_ops = {
764 	.dev_configure = mvneta_dev_configure,
765 	.dev_start = mvneta_dev_start,
766 	.dev_stop = mvneta_dev_stop,
767 	.dev_set_link_up = mvneta_dev_set_link_up,
768 	.dev_set_link_down = mvneta_dev_set_link_down,
769 	.dev_close = mvneta_dev_close,
770 	.link_update = mvneta_link_update,
771 	.promiscuous_enable = mvneta_promiscuous_enable,
772 	.promiscuous_disable = mvneta_promiscuous_disable,
773 	.mac_addr_remove = mvneta_mac_addr_remove,
774 	.mac_addr_add = mvneta_mac_addr_add,
775 	.mac_addr_set = mvneta_mac_addr_set,
776 	.mtu_set = mvneta_mtu_set,
777 	.stats_get = mvneta_stats_get,
778 	.stats_reset = mvneta_stats_reset,
779 	.dev_infos_get = mvneta_dev_infos_get,
780 	.dev_supported_ptypes_get = mvneta_dev_supported_ptypes_get,
781 	.rxq_info_get = mvneta_rxq_info_get,
782 	.txq_info_get = mvneta_txq_info_get,
783 	.rx_queue_setup = mvneta_rx_queue_setup,
784 	.rx_queue_release = mvneta_rx_queue_release,
785 	.tx_queue_setup = mvneta_tx_queue_setup,
786 	.tx_queue_release = mvneta_tx_queue_release,
787 };
788 
789 /**
790  * Create device representing Ethernet port.
791  *
792  * @param name
793  *   Pointer to the port's name.
794  *
795  * @return
796  *   0 on success, negative error value otherwise.
797  */
798 static int
799 mvneta_eth_dev_create(struct rte_vdev_device *vdev, const char *name)
800 {
801 	int ret, fd = socket(AF_INET, SOCK_DGRAM, 0);
802 	struct rte_eth_dev *eth_dev;
803 	struct mvneta_priv *priv;
804 	struct ifreq req;
805 
806 	eth_dev = rte_eth_dev_allocate(name);
807 	if (!eth_dev)
808 		return -ENOMEM;
809 
810 	priv = rte_zmalloc_socket(name, sizeof(*priv), 0, rte_socket_id());
811 	if (!priv) {
812 		ret = -ENOMEM;
813 		goto out_free;
814 	}
815 	eth_dev->data->dev_private = priv;
816 
817 	eth_dev->data->mac_addrs =
818 		rte_zmalloc("mac_addrs",
819 			    RTE_ETHER_ADDR_LEN * MVNETA_MAC_ADDRS_MAX, 0);
820 	if (!eth_dev->data->mac_addrs) {
821 		MVNETA_LOG(ERR, "Failed to allocate space for eth addrs");
822 		ret = -ENOMEM;
823 		goto out_free;
824 	}
825 
826 	memset(&req, 0, sizeof(req));
827 	strcpy(req.ifr_name, name);
828 	ret = ioctl(fd, SIOCGIFHWADDR, &req);
829 	if (ret)
830 		goto out_free;
831 
832 	memcpy(eth_dev->data->mac_addrs[0].addr_bytes,
833 	       req.ifr_addr.sa_data, RTE_ETHER_ADDR_LEN);
834 
835 	eth_dev->device = &vdev->device;
836 	eth_dev->rx_pkt_burst = mvneta_rx_pkt_burst;
837 	mvneta_set_tx_function(eth_dev);
838 	eth_dev->dev_ops = &mvneta_ops;
839 
840 	rte_eth_dev_probing_finish(eth_dev);
841 	return 0;
842 out_free:
843 	rte_eth_dev_release_port(eth_dev);
844 
845 	return ret;
846 }
847 
848 /**
849  * Cleanup previously created device representing Ethernet port.
850  *
851  * @param eth_dev
852  *   Pointer to the corresponding rte_eth_dev structure.
853  */
854 static void
855 mvneta_eth_dev_destroy(struct rte_eth_dev *eth_dev)
856 {
857 	rte_eth_dev_release_port(eth_dev);
858 }
859 
860 /**
861  * Cleanup previously created device representing Ethernet port.
862  *
863  * @param name
864  *   Pointer to the port name.
865  */
866 static void
867 mvneta_eth_dev_destroy_name(const char *name)
868 {
869 	struct rte_eth_dev *eth_dev;
870 
871 	eth_dev = rte_eth_dev_allocated(name);
872 	if (!eth_dev)
873 		return;
874 
875 	mvneta_eth_dev_destroy(eth_dev);
876 }
877 
878 /**
879  * DPDK callback to register the virtual device.
880  *
881  * @param vdev
882  *   Pointer to the virtual device.
883  *
884  * @return
885  *   0 on success, negative error value otherwise.
886  */
887 static int
888 rte_pmd_mvneta_probe(struct rte_vdev_device *vdev)
889 {
890 	struct rte_kvargs *kvlist;
891 	struct mvneta_ifnames ifnames;
892 	int ret = -EINVAL;
893 	uint32_t i, ifnum;
894 	const char *params;
895 
896 	params = rte_vdev_device_args(vdev);
897 	if (!params)
898 		return -EINVAL;
899 
900 	kvlist = rte_kvargs_parse(params, valid_args);
901 	if (!kvlist)
902 		return -EINVAL;
903 
904 	ifnum = rte_kvargs_count(kvlist, MVNETA_IFACE_NAME_ARG);
905 	if (ifnum > RTE_DIM(ifnames.names))
906 		goto out_free_kvlist;
907 
908 	ifnames.idx = 0;
909 	rte_kvargs_process(kvlist, MVNETA_IFACE_NAME_ARG,
910 			   mvneta_ifnames_get, &ifnames);
911 
912 	/*
913 	 * The below system initialization should be done only once,
914 	 * on the first provided configuration file
915 	 */
916 	if (mvneta_dev_num)
917 		goto init_devices;
918 
919 	MVNETA_LOG(INFO, "Perform MUSDK initializations");
920 
921 	ret = rte_mvep_init(MVEP_MOD_T_NETA, kvlist);
922 	if (ret)
923 		goto out_free_kvlist;
924 
925 	ret = mvneta_neta_init();
926 	if (ret) {
927 		MVNETA_LOG(ERR, "Failed to init NETA!");
928 		rte_mvep_deinit(MVEP_MOD_T_NETA);
929 		goto out_free_kvlist;
930 	}
931 
932 init_devices:
933 	for (i = 0; i < ifnum; i++) {
934 		MVNETA_LOG(INFO, "Creating %s", ifnames.names[i]);
935 		ret = mvneta_eth_dev_create(vdev, ifnames.names[i]);
936 		if (ret)
937 			goto out_cleanup;
938 
939 		mvneta_dev_num++;
940 	}
941 
942 	rte_kvargs_free(kvlist);
943 
944 	return 0;
945 out_cleanup:
946 	rte_pmd_mvneta_remove(vdev);
947 
948 out_free_kvlist:
949 	rte_kvargs_free(kvlist);
950 
951 	return ret;
952 }
953 
954 /**
955  * DPDK callback to remove virtual device.
956  *
957  * @param vdev
958  *   Pointer to the removed virtual device.
959  *
960  * @return
961  *   0 on success, negative error value otherwise.
962  */
963 static int
964 rte_pmd_mvneta_remove(struct rte_vdev_device *vdev)
965 {
966 	uint16_t port_id;
967 
968 	RTE_ETH_FOREACH_DEV(port_id) {
969 		if (rte_eth_devices[port_id].device != &vdev->device)
970 			continue;
971 		rte_eth_dev_close(port_id);
972 	}
973 
974 	return 0;
975 }
976 
977 static struct rte_vdev_driver pmd_mvneta_drv = {
978 	.probe = rte_pmd_mvneta_probe,
979 	.remove = rte_pmd_mvneta_remove,
980 };
981 
982 RTE_PMD_REGISTER_VDEV(net_mvneta, pmd_mvneta_drv);
983 RTE_PMD_REGISTER_PARAM_STRING(net_mvneta, "iface=<ifc>");
984 RTE_LOG_REGISTER(mvneta_logtype, pmd.net.mvneta, NOTICE);
985