xref: /dpdk/drivers/net/mvneta/mvneta_ethdev.c (revision 7f12dfdc)
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 int mvneta_logtype;
38 
39 static const char * const valid_args[] = {
40 	MVNETA_IFACE_NAME_ARG,
41 	NULL
42 };
43 
44 struct mvneta_ifnames {
45 	const char *names[NETA_NUM_ETH_PPIO];
46 	int idx;
47 };
48 
49 static int mvneta_dev_num;
50 
51 static void mvneta_stats_reset(struct rte_eth_dev *dev);
52 static int rte_pmd_mvneta_remove(struct rte_vdev_device *vdev);
53 
54 
55 /**
56  * Deinitialize packet processor.
57  */
58 static void
59 mvneta_neta_deinit(void)
60 {
61 	neta_deinit();
62 }
63 
64 /**
65  * Initialize packet processor.
66  *
67  * @return
68  *   0 on success, negative error value otherwise.
69  */
70 static int
71 mvneta_neta_init(void)
72 {
73 	return neta_init();
74 }
75 
76 /**
77  * Callback used by rte_kvargs_process() during argument parsing.
78  *
79  * @param key
80  *   Pointer to the parsed key (unused).
81  * @param value
82  *   Pointer to the parsed value.
83  * @param extra_args
84  *   Pointer to the extra arguments which contains address of the
85  *   table of pointers to parsed interface names.
86  *
87  * @return
88  *   Always 0.
89  */
90 static int
91 mvneta_ifnames_get(const char *key __rte_unused, const char *value,
92 		 void *extra_args)
93 {
94 	struct mvneta_ifnames *ifnames = extra_args;
95 
96 	ifnames->names[ifnames->idx++] = value;
97 
98 	return 0;
99 }
100 
101 /**
102  * Ethernet device configuration.
103  *
104  * Prepare the driver for a given number of TX and RX queues and
105  * configure RSS if supported.
106  *
107  * @param dev
108  *   Pointer to Ethernet device structure.
109  *
110  * @return
111  *   0 on success, negative error value otherwise.
112  */
113 static int
114 mvneta_dev_configure(struct rte_eth_dev *dev)
115 {
116 	struct mvneta_priv *priv = dev->data->dev_private;
117 	struct neta_ppio_params *ppio_params;
118 
119 	if (dev->data->dev_conf.rxmode.mq_mode != ETH_MQ_RX_NONE) {
120 		MVNETA_LOG(INFO, "Unsupported RSS and rx multi queue mode %d",
121 			dev->data->dev_conf.rxmode.mq_mode);
122 		if (dev->data->nb_rx_queues > 1)
123 			return -EINVAL;
124 	}
125 
126 	if (dev->data->dev_conf.rxmode.split_hdr_size) {
127 		MVNETA_LOG(INFO, "Split headers not supported");
128 		return -EINVAL;
129 	}
130 
131 	if (dev->data->dev_conf.rxmode.offloads & DEV_RX_OFFLOAD_JUMBO_FRAME)
132 		dev->data->mtu = dev->data->dev_conf.rxmode.max_rx_pkt_len -
133 				 MRVL_NETA_ETH_HDRS_LEN;
134 
135 	if (dev->data->dev_conf.txmode.offloads & DEV_TX_OFFLOAD_MULTI_SEGS)
136 		priv->multiseg = 1;
137 
138 	ppio_params = &priv->ppio_params;
139 	ppio_params->outqs_params.num_outqs = dev->data->nb_tx_queues;
140 	/* Default: 1 TC, no QoS supported. */
141 	ppio_params->inqs_params.num_tcs = 1;
142 	ppio_params->inqs_params.tcs_params[0].pkt_offset = MRVL_NETA_PKT_OFFS;
143 	priv->ppio_id = dev->data->port_id;
144 
145 	return 0;
146 }
147 
148 /**
149  * DPDK callback to get information about the device.
150  *
151  * @param dev
152  *   Pointer to Ethernet device structure (unused).
153  * @param info
154  *   Info structure output buffer.
155  */
156 static void
157 mvneta_dev_infos_get(struct rte_eth_dev *dev __rte_unused,
158 		   struct rte_eth_dev_info *info)
159 {
160 	info->speed_capa = ETH_LINK_SPEED_10M |
161 			   ETH_LINK_SPEED_100M |
162 			   ETH_LINK_SPEED_1G |
163 			   ETH_LINK_SPEED_2_5G;
164 
165 	info->max_rx_queues = MRVL_NETA_RXQ_MAX;
166 	info->max_tx_queues = MRVL_NETA_TXQ_MAX;
167 	info->max_mac_addrs = MVNETA_MAC_ADDRS_MAX;
168 
169 	info->rx_desc_lim.nb_max = MRVL_NETA_RXD_MAX;
170 	info->rx_desc_lim.nb_min = MRVL_NETA_RXD_MIN;
171 	info->rx_desc_lim.nb_align = MRVL_NETA_RXD_ALIGN;
172 
173 	info->tx_desc_lim.nb_max = MRVL_NETA_TXD_MAX;
174 	info->tx_desc_lim.nb_min = MRVL_NETA_TXD_MIN;
175 	info->tx_desc_lim.nb_align = MRVL_NETA_TXD_ALIGN;
176 
177 	info->rx_offload_capa = MVNETA_RX_OFFLOADS;
178 	info->rx_queue_offload_capa = MVNETA_RX_OFFLOADS;
179 
180 	info->tx_offload_capa =  MVNETA_TX_OFFLOADS;
181 	info->tx_queue_offload_capa =  MVNETA_TX_OFFLOADS;
182 
183 	/* By default packets are dropped if no descriptors are available */
184 	info->default_rxconf.rx_drop_en = 1;
185 	/* Deferred tx queue start is not supported */
186 	info->default_txconf.tx_deferred_start = 0;
187 	info->default_txconf.offloads = 0;
188 
189 	info->max_rx_pktlen = MVNETA_PKT_SIZE_MAX;
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 void
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 (priv->ppio)
439 		mvneta_dev_stop(dev);
440 
441 	for (i = 0; i < dev->data->nb_rx_queues; i++) {
442 		mvneta_rx_queue_release(dev->data->rx_queues[i]);
443 		dev->data->rx_queues[i] = NULL;
444 	}
445 
446 	for (i = 0; i < dev->data->nb_tx_queues; i++) {
447 		mvneta_tx_queue_release(dev->data->tx_queues[i]);
448 		dev->data->tx_queues[i] = NULL;
449 	}
450 
451 	mvneta_dev_num--;
452 
453 	if (mvneta_dev_num == 0) {
454 		MVNETA_LOG(INFO, "Perform MUSDK deinit");
455 		mvneta_neta_deinit();
456 		rte_mvep_deinit(MVEP_MOD_T_NETA);
457 	}
458 }
459 
460 /**
461  * DPDK callback to retrieve physical link information.
462  *
463  * @param dev
464  *   Pointer to Ethernet device structure.
465  * @param wait_to_complete
466  *   Wait for request completion (ignored).
467  *
468  * @return
469  *   0 on success, negative error value otherwise.
470  */
471 static int
472 mvneta_link_update(struct rte_eth_dev *dev, int wait_to_complete __rte_unused)
473 {
474 	/*
475 	 * TODO
476 	 * once MUSDK provides necessary API use it here
477 	 */
478 	struct mvneta_priv *priv = dev->data->dev_private;
479 	struct ethtool_cmd edata;
480 	struct ifreq req;
481 	int ret, fd, link_up;
482 
483 	if (!priv->ppio)
484 		return -EPERM;
485 
486 	edata.cmd = ETHTOOL_GSET;
487 
488 	strcpy(req.ifr_name, dev->data->name);
489 	req.ifr_data = (void *)&edata;
490 
491 	fd = socket(AF_INET, SOCK_DGRAM, 0);
492 	if (fd == -1)
493 		return -EFAULT;
494 	ret = ioctl(fd, SIOCETHTOOL, &req);
495 	if (ret == -1) {
496 		close(fd);
497 		return -EFAULT;
498 	}
499 
500 	close(fd);
501 
502 	switch (ethtool_cmd_speed(&edata)) {
503 	case SPEED_10:
504 		dev->data->dev_link.link_speed = ETH_SPEED_NUM_10M;
505 		break;
506 	case SPEED_100:
507 		dev->data->dev_link.link_speed = ETH_SPEED_NUM_100M;
508 		break;
509 	case SPEED_1000:
510 		dev->data->dev_link.link_speed = ETH_SPEED_NUM_1G;
511 		break;
512 	case SPEED_2500:
513 		dev->data->dev_link.link_speed = ETH_SPEED_NUM_2_5G;
514 		break;
515 	default:
516 		dev->data->dev_link.link_speed = ETH_SPEED_NUM_NONE;
517 	}
518 
519 	dev->data->dev_link.link_duplex = edata.duplex ? ETH_LINK_FULL_DUPLEX :
520 							 ETH_LINK_HALF_DUPLEX;
521 	dev->data->dev_link.link_autoneg = edata.autoneg ? ETH_LINK_AUTONEG :
522 							   ETH_LINK_FIXED;
523 
524 	neta_ppio_get_link_state(priv->ppio, &link_up);
525 	dev->data->dev_link.link_status = link_up ? ETH_LINK_UP : ETH_LINK_DOWN;
526 
527 	return 0;
528 }
529 
530 /**
531  * DPDK callback to enable promiscuous mode.
532  *
533  * @param dev
534  *   Pointer to Ethernet device structure.
535  */
536 static void
537 mvneta_promiscuous_enable(struct rte_eth_dev *dev)
538 {
539 	struct mvneta_priv *priv = dev->data->dev_private;
540 	int ret, en;
541 
542 	if (!priv->ppio)
543 		return;
544 
545 	neta_ppio_get_promisc(priv->ppio, &en);
546 	if (en) {
547 		MVNETA_LOG(INFO, "Promiscuous already enabled");
548 		return;
549 	}
550 
551 	ret = neta_ppio_set_promisc(priv->ppio, 1);
552 	if (ret)
553 		MVNETA_LOG(ERR, "Failed to enable promiscuous mode");
554 }
555 
556 /**
557  * DPDK callback to disable allmulticast mode.
558  *
559  * @param dev
560  *   Pointer to Ethernet device structure.
561  */
562 static void
563 mvneta_promiscuous_disable(struct rte_eth_dev *dev)
564 {
565 	struct mvneta_priv *priv = dev->data->dev_private;
566 	int ret, en;
567 
568 	if (!priv->ppio)
569 		return;
570 
571 	neta_ppio_get_promisc(priv->ppio, &en);
572 	if (!en) {
573 		MVNETA_LOG(INFO, "Promiscuous already disabled");
574 		return;
575 	}
576 
577 	ret = neta_ppio_set_promisc(priv->ppio, 0);
578 	if (ret)
579 		MVNETA_LOG(ERR, "Failed to disable promiscuous mode");
580 }
581 
582 /**
583  * DPDK callback to remove a MAC address.
584  *
585  * @param dev
586  *   Pointer to Ethernet device structure.
587  * @param index
588  *   MAC address index.
589  */
590 static void
591 mvneta_mac_addr_remove(struct rte_eth_dev *dev, uint32_t index)
592 {
593 	struct mvneta_priv *priv = dev->data->dev_private;
594 	char buf[RTE_ETHER_ADDR_FMT_SIZE];
595 	int ret;
596 
597 	if (!priv->ppio)
598 		return;
599 
600 	ret = neta_ppio_remove_mac_addr(priv->ppio,
601 				       dev->data->mac_addrs[index].addr_bytes);
602 	if (ret) {
603 		rte_ether_format_addr(buf, sizeof(buf),
604 				  &dev->data->mac_addrs[index]);
605 		MVNETA_LOG(ERR, "Failed to remove mac %s", buf);
606 	}
607 }
608 
609 /**
610  * DPDK callback to add a MAC address.
611  *
612  * @param dev
613  *   Pointer to Ethernet device structure.
614  * @param mac_addr
615  *   MAC address to register.
616  * @param index
617  *   MAC address index.
618  * @param vmdq
619  *   VMDq pool index to associate address with (unused).
620  *
621  * @return
622  *   0 on success, negative error value otherwise.
623  */
624 static int
625 mvneta_mac_addr_add(struct rte_eth_dev *dev, struct rte_ether_addr *mac_addr,
626 		  uint32_t index, uint32_t vmdq __rte_unused)
627 {
628 	struct mvneta_priv *priv = dev->data->dev_private;
629 	char buf[RTE_ETHER_ADDR_FMT_SIZE];
630 	int ret;
631 
632 	if (index == 0)
633 		/* For setting index 0, mrvl_mac_addr_set() should be used.*/
634 		return -1;
635 
636 	if (!priv->ppio)
637 		return 0;
638 
639 	ret = neta_ppio_add_mac_addr(priv->ppio, mac_addr->addr_bytes);
640 	if (ret) {
641 		rte_ether_format_addr(buf, sizeof(buf), mac_addr);
642 		MVNETA_LOG(ERR, "Failed to add mac %s", buf);
643 		return -1;
644 	}
645 
646 	return 0;
647 }
648 
649 /**
650  * DPDK callback to set the primary MAC address.
651  *
652  * @param dev
653  *   Pointer to Ethernet device structure.
654  * @param mac_addr
655  *   MAC address to register.
656  */
657 static int
658 mvneta_mac_addr_set(struct rte_eth_dev *dev, struct rte_ether_addr *mac_addr)
659 {
660 	struct mvneta_priv *priv = dev->data->dev_private;
661 	int ret;
662 
663 	if (!priv->ppio)
664 		return -EINVAL;
665 
666 	ret = neta_ppio_set_mac_addr(priv->ppio, mac_addr->addr_bytes);
667 	if (ret) {
668 		char buf[RTE_ETHER_ADDR_FMT_SIZE];
669 		rte_ether_format_addr(buf, sizeof(buf), mac_addr);
670 		MVNETA_LOG(ERR, "Failed to set mac to %s", buf);
671 	}
672 	return 0;
673 }
674 
675 /**
676  * DPDK callback to get device statistics.
677  *
678  * @param dev
679  *   Pointer to Ethernet device structure.
680  * @param stats
681  *   Stats structure output buffer.
682  *
683  * @return
684  *   0 on success, negative error value otherwise.
685  */
686 static int
687 mvneta_stats_get(struct rte_eth_dev *dev, struct rte_eth_stats *stats)
688 {
689 	struct mvneta_priv *priv = dev->data->dev_private;
690 	struct neta_ppio_statistics ppio_stats;
691 	unsigned int ret;
692 
693 	if (!priv->ppio)
694 		return -EPERM;
695 
696 	ret = neta_ppio_get_statistics(priv->ppio, &ppio_stats);
697 	if (unlikely(ret)) {
698 		MVNETA_LOG(ERR, "Failed to update port statistics");
699 		return ret;
700 	}
701 
702 	stats->ipackets += ppio_stats.rx_packets +
703 			ppio_stats.rx_broadcast_packets +
704 			ppio_stats.rx_multicast_packets -
705 			priv->prev_stats.ipackets;
706 	stats->opackets += ppio_stats.tx_packets +
707 			ppio_stats.tx_broadcast_packets +
708 			ppio_stats.tx_multicast_packets -
709 			priv->prev_stats.opackets;
710 	stats->ibytes += ppio_stats.rx_bytes - priv->prev_stats.ibytes;
711 	stats->obytes += ppio_stats.tx_bytes - priv->prev_stats.obytes;
712 	stats->imissed += ppio_stats.rx_discard +
713 			  ppio_stats.rx_overrun -
714 			  priv->prev_stats.imissed;
715 	stats->ierrors = ppio_stats.rx_packets_err -
716 			priv->prev_stats.ierrors;
717 	stats->oerrors = ppio_stats.tx_errors - priv->prev_stats.oerrors;
718 
719 	return 0;
720 }
721 
722 /**
723  * DPDK callback to clear device statistics.
724  *
725  * @param dev
726  *   Pointer to Ethernet device structure.
727  */
728 static void
729 mvneta_stats_reset(struct rte_eth_dev *dev)
730 {
731 	struct mvneta_priv *priv = dev->data->dev_private;
732 	unsigned int ret;
733 
734 	if (!priv->ppio)
735 		return;
736 
737 	ret = mvneta_stats_get(dev, &priv->prev_stats);
738 	if (unlikely(ret))
739 		RTE_LOG(ERR, PMD, "Failed to reset port statistics");
740 }
741 
742 
743 static const struct eth_dev_ops mvneta_ops = {
744 	.dev_configure = mvneta_dev_configure,
745 	.dev_start = mvneta_dev_start,
746 	.dev_stop = mvneta_dev_stop,
747 	.dev_set_link_up = mvneta_dev_set_link_up,
748 	.dev_set_link_down = mvneta_dev_set_link_down,
749 	.dev_close = mvneta_dev_close,
750 	.link_update = mvneta_link_update,
751 	.promiscuous_enable = mvneta_promiscuous_enable,
752 	.promiscuous_disable = mvneta_promiscuous_disable,
753 	.mac_addr_remove = mvneta_mac_addr_remove,
754 	.mac_addr_add = mvneta_mac_addr_add,
755 	.mac_addr_set = mvneta_mac_addr_set,
756 	.mtu_set = mvneta_mtu_set,
757 	.stats_get = mvneta_stats_get,
758 	.stats_reset = mvneta_stats_reset,
759 	.dev_infos_get = mvneta_dev_infos_get,
760 	.dev_supported_ptypes_get = mvneta_dev_supported_ptypes_get,
761 	.rxq_info_get = mvneta_rxq_info_get,
762 	.txq_info_get = mvneta_txq_info_get,
763 	.rx_queue_setup = mvneta_rx_queue_setup,
764 	.rx_queue_release = mvneta_rx_queue_release,
765 	.tx_queue_setup = mvneta_tx_queue_setup,
766 	.tx_queue_release = mvneta_tx_queue_release,
767 };
768 
769 /**
770  * Create device representing Ethernet port.
771  *
772  * @param name
773  *   Pointer to the port's name.
774  *
775  * @return
776  *   0 on success, negative error value otherwise.
777  */
778 static int
779 mvneta_eth_dev_create(struct rte_vdev_device *vdev, const char *name)
780 {
781 	int ret, fd = socket(AF_INET, SOCK_DGRAM, 0);
782 	struct rte_eth_dev *eth_dev;
783 	struct mvneta_priv *priv;
784 	struct ifreq req;
785 
786 	eth_dev = rte_eth_dev_allocate(name);
787 	if (!eth_dev)
788 		return -ENOMEM;
789 
790 	priv = rte_zmalloc_socket(name, sizeof(*priv), 0, rte_socket_id());
791 	if (!priv) {
792 		ret = -ENOMEM;
793 		goto out_free;
794 	}
795 	eth_dev->data->dev_private = priv;
796 
797 	eth_dev->data->mac_addrs =
798 		rte_zmalloc("mac_addrs",
799 			    RTE_ETHER_ADDR_LEN * MVNETA_MAC_ADDRS_MAX, 0);
800 	if (!eth_dev->data->mac_addrs) {
801 		MVNETA_LOG(ERR, "Failed to allocate space for eth addrs");
802 		ret = -ENOMEM;
803 		goto out_free;
804 	}
805 
806 	memset(&req, 0, sizeof(req));
807 	strcpy(req.ifr_name, name);
808 	ret = ioctl(fd, SIOCGIFHWADDR, &req);
809 	if (ret)
810 		goto out_free;
811 
812 	memcpy(eth_dev->data->mac_addrs[0].addr_bytes,
813 	       req.ifr_addr.sa_data, RTE_ETHER_ADDR_LEN);
814 
815 	eth_dev->data->kdrv = RTE_KDRV_NONE;
816 	eth_dev->device = &vdev->device;
817 	eth_dev->rx_pkt_burst = mvneta_rx_pkt_burst;
818 	mvneta_set_tx_function(eth_dev);
819 	eth_dev->dev_ops = &mvneta_ops;
820 
821 	/* Flag to call rte_eth_dev_release_port() in rte_eth_dev_close(). */
822 	eth_dev->data->dev_flags |= RTE_ETH_DEV_CLOSE_REMOVE;
823 
824 	rte_eth_dev_probing_finish(eth_dev);
825 	return 0;
826 out_free:
827 	rte_eth_dev_release_port(eth_dev);
828 
829 	return ret;
830 }
831 
832 /**
833  * Cleanup previously created device representing Ethernet port.
834  *
835  * @param eth_dev
836  *   Pointer to the corresponding rte_eth_dev structure.
837  */
838 static void
839 mvneta_eth_dev_destroy(struct rte_eth_dev *eth_dev)
840 {
841 	rte_eth_dev_release_port(eth_dev);
842 }
843 
844 /**
845  * Cleanup previously created device representing Ethernet port.
846  *
847  * @param name
848  *   Pointer to the port name.
849  */
850 static void
851 mvneta_eth_dev_destroy_name(const char *name)
852 {
853 	struct rte_eth_dev *eth_dev;
854 
855 	eth_dev = rte_eth_dev_allocated(name);
856 	if (!eth_dev)
857 		return;
858 
859 	mvneta_eth_dev_destroy(eth_dev);
860 }
861 
862 /**
863  * DPDK callback to register the virtual device.
864  *
865  * @param vdev
866  *   Pointer to the virtual device.
867  *
868  * @return
869  *   0 on success, negative error value otherwise.
870  */
871 static int
872 rte_pmd_mvneta_probe(struct rte_vdev_device *vdev)
873 {
874 	struct rte_kvargs *kvlist;
875 	struct mvneta_ifnames ifnames;
876 	int ret = -EINVAL;
877 	uint32_t i, ifnum;
878 	const char *params;
879 
880 	params = rte_vdev_device_args(vdev);
881 	if (!params)
882 		return -EINVAL;
883 
884 	kvlist = rte_kvargs_parse(params, valid_args);
885 	if (!kvlist)
886 		return -EINVAL;
887 
888 	ifnum = rte_kvargs_count(kvlist, MVNETA_IFACE_NAME_ARG);
889 	if (ifnum > RTE_DIM(ifnames.names))
890 		goto out_free_kvlist;
891 
892 	ifnames.idx = 0;
893 	rte_kvargs_process(kvlist, MVNETA_IFACE_NAME_ARG,
894 			   mvneta_ifnames_get, &ifnames);
895 
896 	/*
897 	 * The below system initialization should be done only once,
898 	 * on the first provided configuration file
899 	 */
900 	if (mvneta_dev_num)
901 		goto init_devices;
902 
903 	MVNETA_LOG(INFO, "Perform MUSDK initializations");
904 
905 	ret = rte_mvep_init(MVEP_MOD_T_NETA, kvlist);
906 	if (ret)
907 		goto out_free_kvlist;
908 
909 	ret = mvneta_neta_init();
910 	if (ret) {
911 		MVNETA_LOG(ERR, "Failed to init NETA!");
912 		rte_mvep_deinit(MVEP_MOD_T_NETA);
913 		goto out_free_kvlist;
914 	}
915 
916 init_devices:
917 	for (i = 0; i < ifnum; i++) {
918 		MVNETA_LOG(INFO, "Creating %s", ifnames.names[i]);
919 		ret = mvneta_eth_dev_create(vdev, ifnames.names[i]);
920 		if (ret)
921 			goto out_cleanup;
922 
923 		mvneta_dev_num++;
924 	}
925 
926 	rte_kvargs_free(kvlist);
927 
928 	return 0;
929 out_cleanup:
930 	rte_pmd_mvneta_remove(vdev);
931 
932 out_free_kvlist:
933 	rte_kvargs_free(kvlist);
934 
935 	return ret;
936 }
937 
938 /**
939  * DPDK callback to remove virtual device.
940  *
941  * @param vdev
942  *   Pointer to the removed virtual device.
943  *
944  * @return
945  *   0 on success, negative error value otherwise.
946  */
947 static int
948 rte_pmd_mvneta_remove(struct rte_vdev_device *vdev)
949 {
950 	uint16_t port_id;
951 
952 	RTE_ETH_FOREACH_DEV(port_id) {
953 		if (rte_eth_devices[port_id].device != &vdev->device)
954 			continue;
955 		rte_eth_dev_close(port_id);
956 	}
957 
958 	return 0;
959 }
960 
961 static struct rte_vdev_driver pmd_mvneta_drv = {
962 	.probe = rte_pmd_mvneta_probe,
963 	.remove = rte_pmd_mvneta_remove,
964 };
965 
966 RTE_PMD_REGISTER_VDEV(net_mvneta, pmd_mvneta_drv);
967 RTE_PMD_REGISTER_PARAM_STRING(net_mvneta, "iface=<ifc>");
968 
969 RTE_INIT(mvneta_init_log)
970 {
971 	mvneta_logtype = rte_log_register("pmd.net.mvneta");
972 	if (mvneta_logtype >= 0)
973 		rte_log_set_level(mvneta_logtype, RTE_LOG_NOTICE);
974 }
975