net/mlx5: add eCPRI flex parser capacity check
[dpdk.git] / drivers / net / mvneta / mvneta_ethdev.c
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 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  * @return
537  *   always 0
538  */
539 static int
540 mvneta_promiscuous_enable(struct rte_eth_dev *dev)
541 {
542         struct mvneta_priv *priv = dev->data->dev_private;
543         int ret, en;
544
545         if (!priv->ppio)
546                 return 0;
547
548         neta_ppio_get_promisc(priv->ppio, &en);
549         if (en) {
550                 MVNETA_LOG(INFO, "Promiscuous already enabled");
551                 return 0;
552         }
553
554         ret = neta_ppio_set_promisc(priv->ppio, 1);
555         if (ret)
556                 MVNETA_LOG(ERR, "Failed to enable promiscuous mode");
557
558         return 0;
559 }
560
561 /**
562  * DPDK callback to disable allmulticast mode.
563  *
564  * @param dev
565  *   Pointer to Ethernet device structure.
566  *
567  * @return
568  *   always 0
569  */
570 static int
571 mvneta_promiscuous_disable(struct rte_eth_dev *dev)
572 {
573         struct mvneta_priv *priv = dev->data->dev_private;
574         int ret, en;
575
576         if (!priv->ppio)
577                 return 0;
578
579         neta_ppio_get_promisc(priv->ppio, &en);
580         if (!en) {
581                 MVNETA_LOG(INFO, "Promiscuous already disabled");
582                 return 0;
583         }
584
585         ret = neta_ppio_set_promisc(priv->ppio, 0);
586         if (ret)
587                 MVNETA_LOG(ERR, "Failed to disable promiscuous mode");
588
589         return 0;
590 }
591
592 /**
593  * DPDK callback to remove a MAC address.
594  *
595  * @param dev
596  *   Pointer to Ethernet device structure.
597  * @param index
598  *   MAC address index.
599  */
600 static void
601 mvneta_mac_addr_remove(struct rte_eth_dev *dev, uint32_t index)
602 {
603         struct mvneta_priv *priv = dev->data->dev_private;
604         char buf[RTE_ETHER_ADDR_FMT_SIZE];
605         int ret;
606
607         if (!priv->ppio)
608                 return;
609
610         ret = neta_ppio_remove_mac_addr(priv->ppio,
611                                        dev->data->mac_addrs[index].addr_bytes);
612         if (ret) {
613                 rte_ether_format_addr(buf, sizeof(buf),
614                                   &dev->data->mac_addrs[index]);
615                 MVNETA_LOG(ERR, "Failed to remove mac %s", buf);
616         }
617 }
618
619 /**
620  * DPDK callback to add a MAC address.
621  *
622  * @param dev
623  *   Pointer to Ethernet device structure.
624  * @param mac_addr
625  *   MAC address to register.
626  * @param index
627  *   MAC address index.
628  * @param vmdq
629  *   VMDq pool index to associate address with (unused).
630  *
631  * @return
632  *   0 on success, negative error value otherwise.
633  */
634 static int
635 mvneta_mac_addr_add(struct rte_eth_dev *dev, struct rte_ether_addr *mac_addr,
636                   uint32_t index, uint32_t vmdq __rte_unused)
637 {
638         struct mvneta_priv *priv = dev->data->dev_private;
639         char buf[RTE_ETHER_ADDR_FMT_SIZE];
640         int ret;
641
642         if (index == 0)
643                 /* For setting index 0, mrvl_mac_addr_set() should be used.*/
644                 return -1;
645
646         if (!priv->ppio)
647                 return 0;
648
649         ret = neta_ppio_add_mac_addr(priv->ppio, mac_addr->addr_bytes);
650         if (ret) {
651                 rte_ether_format_addr(buf, sizeof(buf), mac_addr);
652                 MVNETA_LOG(ERR, "Failed to add mac %s", buf);
653                 return -1;
654         }
655
656         return 0;
657 }
658
659 /**
660  * DPDK callback to set the primary MAC address.
661  *
662  * @param dev
663  *   Pointer to Ethernet device structure.
664  * @param mac_addr
665  *   MAC address to register.
666  */
667 static int
668 mvneta_mac_addr_set(struct rte_eth_dev *dev, struct rte_ether_addr *mac_addr)
669 {
670         struct mvneta_priv *priv = dev->data->dev_private;
671         int ret;
672
673         if (!priv->ppio)
674                 return -EINVAL;
675
676         ret = neta_ppio_set_mac_addr(priv->ppio, mac_addr->addr_bytes);
677         if (ret) {
678                 char buf[RTE_ETHER_ADDR_FMT_SIZE];
679                 rte_ether_format_addr(buf, sizeof(buf), mac_addr);
680                 MVNETA_LOG(ERR, "Failed to set mac to %s", buf);
681         }
682         return 0;
683 }
684
685 /**
686  * DPDK callback to get device statistics.
687  *
688  * @param dev
689  *   Pointer to Ethernet device structure.
690  * @param stats
691  *   Stats structure output buffer.
692  *
693  * @return
694  *   0 on success, negative error value otherwise.
695  */
696 static int
697 mvneta_stats_get(struct rte_eth_dev *dev, struct rte_eth_stats *stats)
698 {
699         struct mvneta_priv *priv = dev->data->dev_private;
700         struct neta_ppio_statistics ppio_stats;
701         unsigned int ret;
702
703         if (!priv->ppio)
704                 return -EPERM;
705
706         ret = neta_ppio_get_statistics(priv->ppio, &ppio_stats);
707         if (unlikely(ret)) {
708                 MVNETA_LOG(ERR, "Failed to update port statistics");
709                 return ret;
710         }
711
712         stats->ipackets += ppio_stats.rx_packets +
713                         ppio_stats.rx_broadcast_packets +
714                         ppio_stats.rx_multicast_packets -
715                         priv->prev_stats.ipackets;
716         stats->opackets += ppio_stats.tx_packets +
717                         ppio_stats.tx_broadcast_packets +
718                         ppio_stats.tx_multicast_packets -
719                         priv->prev_stats.opackets;
720         stats->ibytes += ppio_stats.rx_bytes - priv->prev_stats.ibytes;
721         stats->obytes += ppio_stats.tx_bytes - priv->prev_stats.obytes;
722         stats->imissed += ppio_stats.rx_discard +
723                           ppio_stats.rx_overrun -
724                           priv->prev_stats.imissed;
725         stats->ierrors = ppio_stats.rx_packets_err -
726                         priv->prev_stats.ierrors;
727         stats->oerrors = ppio_stats.tx_errors - priv->prev_stats.oerrors;
728
729         return 0;
730 }
731
732 /**
733  * DPDK callback to clear device statistics.
734  *
735  * @param dev
736  *   Pointer to Ethernet device structure.
737  *
738  * @return
739  *   0 on success, negative error value otherwise.
740  */
741 static int
742 mvneta_stats_reset(struct rte_eth_dev *dev)
743 {
744         struct mvneta_priv *priv = dev->data->dev_private;
745         unsigned int ret;
746
747         if (!priv->ppio)
748                 return 0;
749
750         ret = mvneta_stats_get(dev, &priv->prev_stats);
751         if (unlikely(ret))
752                 MVNETA_LOG(ERR, "Failed to reset port statistics");
753
754         return ret;
755 }
756
757
758 static const struct eth_dev_ops mvneta_ops = {
759         .dev_configure = mvneta_dev_configure,
760         .dev_start = mvneta_dev_start,
761         .dev_stop = mvneta_dev_stop,
762         .dev_set_link_up = mvneta_dev_set_link_up,
763         .dev_set_link_down = mvneta_dev_set_link_down,
764         .dev_close = mvneta_dev_close,
765         .link_update = mvneta_link_update,
766         .promiscuous_enable = mvneta_promiscuous_enable,
767         .promiscuous_disable = mvneta_promiscuous_disable,
768         .mac_addr_remove = mvneta_mac_addr_remove,
769         .mac_addr_add = mvneta_mac_addr_add,
770         .mac_addr_set = mvneta_mac_addr_set,
771         .mtu_set = mvneta_mtu_set,
772         .stats_get = mvneta_stats_get,
773         .stats_reset = mvneta_stats_reset,
774         .dev_infos_get = mvneta_dev_infos_get,
775         .dev_supported_ptypes_get = mvneta_dev_supported_ptypes_get,
776         .rxq_info_get = mvneta_rxq_info_get,
777         .txq_info_get = mvneta_txq_info_get,
778         .rx_queue_setup = mvneta_rx_queue_setup,
779         .rx_queue_release = mvneta_rx_queue_release,
780         .tx_queue_setup = mvneta_tx_queue_setup,
781         .tx_queue_release = mvneta_tx_queue_release,
782 };
783
784 /**
785  * Create device representing Ethernet port.
786  *
787  * @param name
788  *   Pointer to the port's name.
789  *
790  * @return
791  *   0 on success, negative error value otherwise.
792  */
793 static int
794 mvneta_eth_dev_create(struct rte_vdev_device *vdev, const char *name)
795 {
796         int ret, fd = socket(AF_INET, SOCK_DGRAM, 0);
797         struct rte_eth_dev *eth_dev;
798         struct mvneta_priv *priv;
799         struct ifreq req;
800
801         eth_dev = rte_eth_dev_allocate(name);
802         if (!eth_dev)
803                 return -ENOMEM;
804
805         priv = rte_zmalloc_socket(name, sizeof(*priv), 0, rte_socket_id());
806         if (!priv) {
807                 ret = -ENOMEM;
808                 goto out_free;
809         }
810         eth_dev->data->dev_private = priv;
811
812         eth_dev->data->mac_addrs =
813                 rte_zmalloc("mac_addrs",
814                             RTE_ETHER_ADDR_LEN * MVNETA_MAC_ADDRS_MAX, 0);
815         if (!eth_dev->data->mac_addrs) {
816                 MVNETA_LOG(ERR, "Failed to allocate space for eth addrs");
817                 ret = -ENOMEM;
818                 goto out_free;
819         }
820
821         memset(&req, 0, sizeof(req));
822         strcpy(req.ifr_name, name);
823         ret = ioctl(fd, SIOCGIFHWADDR, &req);
824         if (ret)
825                 goto out_free;
826
827         memcpy(eth_dev->data->mac_addrs[0].addr_bytes,
828                req.ifr_addr.sa_data, RTE_ETHER_ADDR_LEN);
829
830         eth_dev->data->kdrv = RTE_KDRV_NONE;
831         eth_dev->device = &vdev->device;
832         eth_dev->rx_pkt_burst = mvneta_rx_pkt_burst;
833         mvneta_set_tx_function(eth_dev);
834         eth_dev->dev_ops = &mvneta_ops;
835
836         /* Flag to call rte_eth_dev_release_port() in rte_eth_dev_close(). */
837         eth_dev->data->dev_flags |= RTE_ETH_DEV_CLOSE_REMOVE;
838
839         rte_eth_dev_probing_finish(eth_dev);
840         return 0;
841 out_free:
842         rte_eth_dev_release_port(eth_dev);
843
844         return ret;
845 }
846
847 /**
848  * Cleanup previously created device representing Ethernet port.
849  *
850  * @param eth_dev
851  *   Pointer to the corresponding rte_eth_dev structure.
852  */
853 static void
854 mvneta_eth_dev_destroy(struct rte_eth_dev *eth_dev)
855 {
856         rte_eth_dev_release_port(eth_dev);
857 }
858
859 /**
860  * Cleanup previously created device representing Ethernet port.
861  *
862  * @param name
863  *   Pointer to the port name.
864  */
865 static void
866 mvneta_eth_dev_destroy_name(const char *name)
867 {
868         struct rte_eth_dev *eth_dev;
869
870         eth_dev = rte_eth_dev_allocated(name);
871         if (!eth_dev)
872                 return;
873
874         mvneta_eth_dev_destroy(eth_dev);
875 }
876
877 /**
878  * DPDK callback to register the virtual device.
879  *
880  * @param vdev
881  *   Pointer to the virtual device.
882  *
883  * @return
884  *   0 on success, negative error value otherwise.
885  */
886 static int
887 rte_pmd_mvneta_probe(struct rte_vdev_device *vdev)
888 {
889         struct rte_kvargs *kvlist;
890         struct mvneta_ifnames ifnames;
891         int ret = -EINVAL;
892         uint32_t i, ifnum;
893         const char *params;
894
895         params = rte_vdev_device_args(vdev);
896         if (!params)
897                 return -EINVAL;
898
899         kvlist = rte_kvargs_parse(params, valid_args);
900         if (!kvlist)
901                 return -EINVAL;
902
903         ifnum = rte_kvargs_count(kvlist, MVNETA_IFACE_NAME_ARG);
904         if (ifnum > RTE_DIM(ifnames.names))
905                 goto out_free_kvlist;
906
907         ifnames.idx = 0;
908         rte_kvargs_process(kvlist, MVNETA_IFACE_NAME_ARG,
909                            mvneta_ifnames_get, &ifnames);
910
911         /*
912          * The below system initialization should be done only once,
913          * on the first provided configuration file
914          */
915         if (mvneta_dev_num)
916                 goto init_devices;
917
918         MVNETA_LOG(INFO, "Perform MUSDK initializations");
919
920         ret = rte_mvep_init(MVEP_MOD_T_NETA, kvlist);
921         if (ret)
922                 goto out_free_kvlist;
923
924         ret = mvneta_neta_init();
925         if (ret) {
926                 MVNETA_LOG(ERR, "Failed to init NETA!");
927                 rte_mvep_deinit(MVEP_MOD_T_NETA);
928                 goto out_free_kvlist;
929         }
930
931 init_devices:
932         for (i = 0; i < ifnum; i++) {
933                 MVNETA_LOG(INFO, "Creating %s", ifnames.names[i]);
934                 ret = mvneta_eth_dev_create(vdev, ifnames.names[i]);
935                 if (ret)
936                         goto out_cleanup;
937
938                 mvneta_dev_num++;
939         }
940
941         rte_kvargs_free(kvlist);
942
943         return 0;
944 out_cleanup:
945         rte_pmd_mvneta_remove(vdev);
946
947 out_free_kvlist:
948         rte_kvargs_free(kvlist);
949
950         return ret;
951 }
952
953 /**
954  * DPDK callback to remove virtual device.
955  *
956  * @param vdev
957  *   Pointer to the removed virtual device.
958  *
959  * @return
960  *   0 on success, negative error value otherwise.
961  */
962 static int
963 rte_pmd_mvneta_remove(struct rte_vdev_device *vdev)
964 {
965         uint16_t port_id;
966
967         RTE_ETH_FOREACH_DEV(port_id) {
968                 if (rte_eth_devices[port_id].device != &vdev->device)
969                         continue;
970                 rte_eth_dev_close(port_id);
971         }
972
973         return 0;
974 }
975
976 static struct rte_vdev_driver pmd_mvneta_drv = {
977         .probe = rte_pmd_mvneta_probe,
978         .remove = rte_pmd_mvneta_remove,
979 };
980
981 RTE_PMD_REGISTER_VDEV(net_mvneta, pmd_mvneta_drv);
982 RTE_PMD_REGISTER_PARAM_STRING(net_mvneta, "iface=<ifc>");
983 RTE_LOG_REGISTER(mvneta_logtype, pmd.net.mvneta, NOTICE);