ethdev: change device info get callback to return int
[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 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 int
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         return 0;
192 }
193
194 /**
195  * Return supported packet types.
196  *
197  * @param dev
198  *   Pointer to Ethernet device structure (unused).
199  *
200  * @return
201  *   Const pointer to the table with supported packet types.
202  */
203 static const uint32_t *
204 mvneta_dev_supported_ptypes_get(struct rte_eth_dev *dev __rte_unused)
205 {
206         static const uint32_t ptypes[] = {
207                 RTE_PTYPE_L2_ETHER,
208                 RTE_PTYPE_L2_ETHER_VLAN,
209                 RTE_PTYPE_L3_IPV4,
210                 RTE_PTYPE_L3_IPV6,
211                 RTE_PTYPE_L4_TCP,
212                 RTE_PTYPE_L4_UDP
213         };
214
215         return ptypes;
216 }
217
218 /**
219  * DPDK callback to change the MTU.
220  *
221  * Setting the MTU affects hardware MRU (packets larger than the MRU
222  * will be dropped).
223  *
224  * @param dev
225  *   Pointer to Ethernet device structure.
226  * @param mtu
227  *   New MTU.
228  *
229  * @return
230  *   0 on success, negative error value otherwise.
231  */
232 static int
233 mvneta_mtu_set(struct rte_eth_dev *dev, uint16_t mtu)
234 {
235         struct mvneta_priv *priv = dev->data->dev_private;
236         uint16_t mbuf_data_size = 0; /* SW buffer size */
237         uint16_t mru;
238         int ret;
239
240         mru = MRVL_NETA_MTU_TO_MRU(mtu);
241         /*
242          * min_rx_buf_size is equal to mbuf data size
243          * if pmd didn't set it differently
244          */
245         mbuf_data_size = dev->data->min_rx_buf_size - RTE_PKTMBUF_HEADROOM;
246         /* Prevent PMD from:
247          * - setting mru greater than the mbuf size resulting in
248          * hw and sw buffer size mismatch
249          * - setting mtu that requires the support of scattered packets
250          * when this feature has not been enabled/supported so far.
251          */
252         if (!dev->data->scattered_rx &&
253             (mru + MRVL_NETA_PKT_OFFS > mbuf_data_size)) {
254                 mru = mbuf_data_size - MRVL_NETA_PKT_OFFS;
255                 mtu = MRVL_NETA_MRU_TO_MTU(mru);
256                 MVNETA_LOG(WARNING, "MTU too big, max MTU possible limitted by"
257                         " current mbuf size: %u. Set MTU to %u, MRU to %u",
258                         mbuf_data_size, mtu, mru);
259         }
260
261         if (mtu < RTE_ETHER_MIN_MTU || mru > MVNETA_PKT_SIZE_MAX) {
262                 MVNETA_LOG(ERR, "Invalid MTU [%u] or MRU [%u]", mtu, mru);
263                 return -EINVAL;
264         }
265
266         dev->data->mtu = mtu;
267         dev->data->dev_conf.rxmode.max_rx_pkt_len = mru - MV_MH_SIZE;
268
269         if (!priv->ppio)
270                 /* It is OK. New MTU will be set later on mvneta_dev_start */
271                 return 0;
272
273         ret = neta_ppio_set_mru(priv->ppio, mru);
274         if (ret) {
275                 MVNETA_LOG(ERR, "Failed to change MRU");
276                 return ret;
277         }
278
279         ret = neta_ppio_set_mtu(priv->ppio, mtu);
280         if (ret) {
281                 MVNETA_LOG(ERR, "Failed to change MTU");
282                 return ret;
283         }
284         MVNETA_LOG(INFO, "MTU changed to %u, MRU = %u", mtu, mru);
285
286         return 0;
287 }
288
289 /**
290  * DPDK callback to bring the link up.
291  *
292  * @param dev
293  *   Pointer to Ethernet device structure.
294  *
295  * @return
296  *   0 on success, negative error value otherwise.
297  */
298 static int
299 mvneta_dev_set_link_up(struct rte_eth_dev *dev)
300 {
301         struct mvneta_priv *priv = dev->data->dev_private;
302
303         if (!priv->ppio)
304                 return 0;
305
306         return neta_ppio_enable(priv->ppio);
307 }
308
309 /**
310  * DPDK callback to bring the link down.
311  *
312  * @param dev
313  *   Pointer to Ethernet device structure.
314  *
315  * @return
316  *   0 on success, negative error value otherwise.
317  */
318 static int
319 mvneta_dev_set_link_down(struct rte_eth_dev *dev)
320 {
321         struct mvneta_priv *priv = dev->data->dev_private;
322
323         if (!priv->ppio)
324                 return 0;
325
326         return neta_ppio_disable(priv->ppio);
327 }
328
329 /**
330  * DPDK callback to start the device.
331  *
332  * @param dev
333  *   Pointer to Ethernet device structure.
334  *
335  * @return
336  *   0 on success, negative errno value on failure.
337  */
338 static int
339 mvneta_dev_start(struct rte_eth_dev *dev)
340 {
341         struct mvneta_priv *priv = dev->data->dev_private;
342         char match[MVNETA_MATCH_LEN];
343         int ret = 0, i;
344
345         if (priv->ppio)
346                 return mvneta_dev_set_link_up(dev);
347
348         strlcpy(match, dev->data->name, sizeof(match));
349         priv->ppio_params.match = match;
350         priv->ppio_params.inqs_params.mtu = dev->data->mtu;
351
352         ret = neta_ppio_init(&priv->ppio_params, &priv->ppio);
353         if (ret) {
354                 MVNETA_LOG(ERR, "Failed to init ppio");
355                 return ret;
356         }
357         priv->ppio_id = priv->ppio->port_id;
358
359         mvneta_stats_reset(dev);
360
361         /*
362          * In case there are some some stale uc/mc mac addresses flush them
363          * here. It cannot be done during mvneta_dev_close() as port information
364          * is already gone at that point (due to neta_ppio_deinit() in
365          * mvneta_dev_stop()).
366          */
367         if (!priv->uc_mc_flushed) {
368                 ret = neta_ppio_flush_mac_addrs(priv->ppio, 0, 1);
369                 if (ret) {
370                         MVNETA_LOG(ERR,
371                                 "Failed to flush uc/mc filter list");
372                         goto out;
373                 }
374                 priv->uc_mc_flushed = 1;
375         }
376
377         ret = mvneta_alloc_rx_bufs(dev);
378         if (ret)
379                 goto out;
380
381         ret = mvneta_mtu_set(dev, dev->data->mtu);
382         if (ret) {
383                 MVNETA_LOG(ERR, "Failed to set MTU %d", dev->data->mtu);
384                 goto out;
385         }
386
387         ret = mvneta_dev_set_link_up(dev);
388         if (ret) {
389                 MVNETA_LOG(ERR, "Failed to set link up");
390                 goto out;
391         }
392
393         /* start tx queues */
394         for (i = 0; i < dev->data->nb_tx_queues; i++)
395                 dev->data->tx_queue_state[i] = RTE_ETH_QUEUE_STATE_STARTED;
396
397         mvneta_set_tx_function(dev);
398
399         return 0;
400
401 out:
402         MVNETA_LOG(ERR, "Failed to start device");
403         neta_ppio_deinit(priv->ppio);
404         return ret;
405 }
406
407 /**
408  * DPDK callback to stop the device.
409  *
410  * @param dev
411  *   Pointer to Ethernet device structure.
412  */
413 static void
414 mvneta_dev_stop(struct rte_eth_dev *dev)
415 {
416         struct mvneta_priv *priv = dev->data->dev_private;
417
418         if (!priv->ppio)
419                 return;
420
421         mvneta_dev_set_link_down(dev);
422         mvneta_flush_queues(dev);
423         neta_ppio_deinit(priv->ppio);
424
425         priv->ppio = NULL;
426 }
427
428 /**
429  * DPDK callback to close the device.
430  *
431  * @param dev
432  *   Pointer to Ethernet device structure.
433  */
434 static void
435 mvneta_dev_close(struct rte_eth_dev *dev)
436 {
437         struct mvneta_priv *priv = dev->data->dev_private;
438         int i;
439
440         if (priv->ppio)
441                 mvneta_dev_stop(dev);
442
443         for (i = 0; i < dev->data->nb_rx_queues; i++) {
444                 mvneta_rx_queue_release(dev->data->rx_queues[i]);
445                 dev->data->rx_queues[i] = NULL;
446         }
447
448         for (i = 0; i < dev->data->nb_tx_queues; i++) {
449                 mvneta_tx_queue_release(dev->data->tx_queues[i]);
450                 dev->data->tx_queues[i] = NULL;
451         }
452
453         mvneta_dev_num--;
454
455         if (mvneta_dev_num == 0) {
456                 MVNETA_LOG(INFO, "Perform MUSDK deinit");
457                 mvneta_neta_deinit();
458                 rte_mvep_deinit(MVEP_MOD_T_NETA);
459         }
460 }
461
462 /**
463  * DPDK callback to retrieve physical link information.
464  *
465  * @param dev
466  *   Pointer to Ethernet device structure.
467  * @param wait_to_complete
468  *   Wait for request completion (ignored).
469  *
470  * @return
471  *   0 on success, negative error value otherwise.
472  */
473 static int
474 mvneta_link_update(struct rte_eth_dev *dev, int wait_to_complete __rte_unused)
475 {
476         /*
477          * TODO
478          * once MUSDK provides necessary API use it here
479          */
480         struct mvneta_priv *priv = dev->data->dev_private;
481         struct ethtool_cmd edata;
482         struct ifreq req;
483         int ret, fd, link_up;
484
485         if (!priv->ppio)
486                 return -EPERM;
487
488         edata.cmd = ETHTOOL_GSET;
489
490         strcpy(req.ifr_name, dev->data->name);
491         req.ifr_data = (void *)&edata;
492
493         fd = socket(AF_INET, SOCK_DGRAM, 0);
494         if (fd == -1)
495                 return -EFAULT;
496         ret = ioctl(fd, SIOCETHTOOL, &req);
497         if (ret == -1) {
498                 close(fd);
499                 return -EFAULT;
500         }
501
502         close(fd);
503
504         switch (ethtool_cmd_speed(&edata)) {
505         case SPEED_10:
506                 dev->data->dev_link.link_speed = ETH_SPEED_NUM_10M;
507                 break;
508         case SPEED_100:
509                 dev->data->dev_link.link_speed = ETH_SPEED_NUM_100M;
510                 break;
511         case SPEED_1000:
512                 dev->data->dev_link.link_speed = ETH_SPEED_NUM_1G;
513                 break;
514         case SPEED_2500:
515                 dev->data->dev_link.link_speed = ETH_SPEED_NUM_2_5G;
516                 break;
517         default:
518                 dev->data->dev_link.link_speed = ETH_SPEED_NUM_NONE;
519         }
520
521         dev->data->dev_link.link_duplex = edata.duplex ? ETH_LINK_FULL_DUPLEX :
522                                                          ETH_LINK_HALF_DUPLEX;
523         dev->data->dev_link.link_autoneg = edata.autoneg ? ETH_LINK_AUTONEG :
524                                                            ETH_LINK_FIXED;
525
526         neta_ppio_get_link_state(priv->ppio, &link_up);
527         dev->data->dev_link.link_status = link_up ? ETH_LINK_UP : ETH_LINK_DOWN;
528
529         return 0;
530 }
531
532 /**
533  * DPDK callback to enable promiscuous mode.
534  *
535  * @param dev
536  *   Pointer to Ethernet device structure.
537  */
538 static void
539 mvneta_promiscuous_enable(struct rte_eth_dev *dev)
540 {
541         struct mvneta_priv *priv = dev->data->dev_private;
542         int ret, en;
543
544         if (!priv->ppio)
545                 return;
546
547         neta_ppio_get_promisc(priv->ppio, &en);
548         if (en) {
549                 MVNETA_LOG(INFO, "Promiscuous already enabled");
550                 return;
551         }
552
553         ret = neta_ppio_set_promisc(priv->ppio, 1);
554         if (ret)
555                 MVNETA_LOG(ERR, "Failed to enable promiscuous mode");
556 }
557
558 /**
559  * DPDK callback to disable allmulticast mode.
560  *
561  * @param dev
562  *   Pointer to Ethernet device structure.
563  */
564 static void
565 mvneta_promiscuous_disable(struct rte_eth_dev *dev)
566 {
567         struct mvneta_priv *priv = dev->data->dev_private;
568         int ret, en;
569
570         if (!priv->ppio)
571                 return;
572
573         neta_ppio_get_promisc(priv->ppio, &en);
574         if (!en) {
575                 MVNETA_LOG(INFO, "Promiscuous already disabled");
576                 return;
577         }
578
579         ret = neta_ppio_set_promisc(priv->ppio, 0);
580         if (ret)
581                 MVNETA_LOG(ERR, "Failed to disable promiscuous mode");
582 }
583
584 /**
585  * DPDK callback to remove a MAC address.
586  *
587  * @param dev
588  *   Pointer to Ethernet device structure.
589  * @param index
590  *   MAC address index.
591  */
592 static void
593 mvneta_mac_addr_remove(struct rte_eth_dev *dev, uint32_t index)
594 {
595         struct mvneta_priv *priv = dev->data->dev_private;
596         char buf[RTE_ETHER_ADDR_FMT_SIZE];
597         int ret;
598
599         if (!priv->ppio)
600                 return;
601
602         ret = neta_ppio_remove_mac_addr(priv->ppio,
603                                        dev->data->mac_addrs[index].addr_bytes);
604         if (ret) {
605                 rte_ether_format_addr(buf, sizeof(buf),
606                                   &dev->data->mac_addrs[index]);
607                 MVNETA_LOG(ERR, "Failed to remove mac %s", buf);
608         }
609 }
610
611 /**
612  * DPDK callback to add a MAC address.
613  *
614  * @param dev
615  *   Pointer to Ethernet device structure.
616  * @param mac_addr
617  *   MAC address to register.
618  * @param index
619  *   MAC address index.
620  * @param vmdq
621  *   VMDq pool index to associate address with (unused).
622  *
623  * @return
624  *   0 on success, negative error value otherwise.
625  */
626 static int
627 mvneta_mac_addr_add(struct rte_eth_dev *dev, struct rte_ether_addr *mac_addr,
628                   uint32_t index, uint32_t vmdq __rte_unused)
629 {
630         struct mvneta_priv *priv = dev->data->dev_private;
631         char buf[RTE_ETHER_ADDR_FMT_SIZE];
632         int ret;
633
634         if (index == 0)
635                 /* For setting index 0, mrvl_mac_addr_set() should be used.*/
636                 return -1;
637
638         if (!priv->ppio)
639                 return 0;
640
641         ret = neta_ppio_add_mac_addr(priv->ppio, mac_addr->addr_bytes);
642         if (ret) {
643                 rte_ether_format_addr(buf, sizeof(buf), mac_addr);
644                 MVNETA_LOG(ERR, "Failed to add mac %s", buf);
645                 return -1;
646         }
647
648         return 0;
649 }
650
651 /**
652  * DPDK callback to set the primary MAC address.
653  *
654  * @param dev
655  *   Pointer to Ethernet device structure.
656  * @param mac_addr
657  *   MAC address to register.
658  */
659 static int
660 mvneta_mac_addr_set(struct rte_eth_dev *dev, struct rte_ether_addr *mac_addr)
661 {
662         struct mvneta_priv *priv = dev->data->dev_private;
663         int ret;
664
665         if (!priv->ppio)
666                 return -EINVAL;
667
668         ret = neta_ppio_set_mac_addr(priv->ppio, mac_addr->addr_bytes);
669         if (ret) {
670                 char buf[RTE_ETHER_ADDR_FMT_SIZE];
671                 rte_ether_format_addr(buf, sizeof(buf), mac_addr);
672                 MVNETA_LOG(ERR, "Failed to set mac to %s", buf);
673         }
674         return 0;
675 }
676
677 /**
678  * DPDK callback to get device statistics.
679  *
680  * @param dev
681  *   Pointer to Ethernet device structure.
682  * @param stats
683  *   Stats structure output buffer.
684  *
685  * @return
686  *   0 on success, negative error value otherwise.
687  */
688 static int
689 mvneta_stats_get(struct rte_eth_dev *dev, struct rte_eth_stats *stats)
690 {
691         struct mvneta_priv *priv = dev->data->dev_private;
692         struct neta_ppio_statistics ppio_stats;
693         unsigned int ret;
694
695         if (!priv->ppio)
696                 return -EPERM;
697
698         ret = neta_ppio_get_statistics(priv->ppio, &ppio_stats);
699         if (unlikely(ret)) {
700                 MVNETA_LOG(ERR, "Failed to update port statistics");
701                 return ret;
702         }
703
704         stats->ipackets += ppio_stats.rx_packets +
705                         ppio_stats.rx_broadcast_packets +
706                         ppio_stats.rx_multicast_packets -
707                         priv->prev_stats.ipackets;
708         stats->opackets += ppio_stats.tx_packets +
709                         ppio_stats.tx_broadcast_packets +
710                         ppio_stats.tx_multicast_packets -
711                         priv->prev_stats.opackets;
712         stats->ibytes += ppio_stats.rx_bytes - priv->prev_stats.ibytes;
713         stats->obytes += ppio_stats.tx_bytes - priv->prev_stats.obytes;
714         stats->imissed += ppio_stats.rx_discard +
715                           ppio_stats.rx_overrun -
716                           priv->prev_stats.imissed;
717         stats->ierrors = ppio_stats.rx_packets_err -
718                         priv->prev_stats.ierrors;
719         stats->oerrors = ppio_stats.tx_errors - priv->prev_stats.oerrors;
720
721         return 0;
722 }
723
724 /**
725  * DPDK callback to clear device statistics.
726  *
727  * @param dev
728  *   Pointer to Ethernet device structure.
729  */
730 static void
731 mvneta_stats_reset(struct rte_eth_dev *dev)
732 {
733         struct mvneta_priv *priv = dev->data->dev_private;
734         unsigned int ret;
735
736         if (!priv->ppio)
737                 return;
738
739         ret = mvneta_stats_get(dev, &priv->prev_stats);
740         if (unlikely(ret))
741                 RTE_LOG(ERR, PMD, "Failed to reset port statistics");
742 }
743
744
745 static const struct eth_dev_ops mvneta_ops = {
746         .dev_configure = mvneta_dev_configure,
747         .dev_start = mvneta_dev_start,
748         .dev_stop = mvneta_dev_stop,
749         .dev_set_link_up = mvneta_dev_set_link_up,
750         .dev_set_link_down = mvneta_dev_set_link_down,
751         .dev_close = mvneta_dev_close,
752         .link_update = mvneta_link_update,
753         .promiscuous_enable = mvneta_promiscuous_enable,
754         .promiscuous_disable = mvneta_promiscuous_disable,
755         .mac_addr_remove = mvneta_mac_addr_remove,
756         .mac_addr_add = mvneta_mac_addr_add,
757         .mac_addr_set = mvneta_mac_addr_set,
758         .mtu_set = mvneta_mtu_set,
759         .stats_get = mvneta_stats_get,
760         .stats_reset = mvneta_stats_reset,
761         .dev_infos_get = mvneta_dev_infos_get,
762         .dev_supported_ptypes_get = mvneta_dev_supported_ptypes_get,
763         .rxq_info_get = mvneta_rxq_info_get,
764         .txq_info_get = mvneta_txq_info_get,
765         .rx_queue_setup = mvneta_rx_queue_setup,
766         .rx_queue_release = mvneta_rx_queue_release,
767         .tx_queue_setup = mvneta_tx_queue_setup,
768         .tx_queue_release = mvneta_tx_queue_release,
769 };
770
771 /**
772  * Create device representing Ethernet port.
773  *
774  * @param name
775  *   Pointer to the port's name.
776  *
777  * @return
778  *   0 on success, negative error value otherwise.
779  */
780 static int
781 mvneta_eth_dev_create(struct rte_vdev_device *vdev, const char *name)
782 {
783         int ret, fd = socket(AF_INET, SOCK_DGRAM, 0);
784         struct rte_eth_dev *eth_dev;
785         struct mvneta_priv *priv;
786         struct ifreq req;
787
788         eth_dev = rte_eth_dev_allocate(name);
789         if (!eth_dev)
790                 return -ENOMEM;
791
792         priv = rte_zmalloc_socket(name, sizeof(*priv), 0, rte_socket_id());
793         if (!priv) {
794                 ret = -ENOMEM;
795                 goto out_free;
796         }
797         eth_dev->data->dev_private = priv;
798
799         eth_dev->data->mac_addrs =
800                 rte_zmalloc("mac_addrs",
801                             RTE_ETHER_ADDR_LEN * MVNETA_MAC_ADDRS_MAX, 0);
802         if (!eth_dev->data->mac_addrs) {
803                 MVNETA_LOG(ERR, "Failed to allocate space for eth addrs");
804                 ret = -ENOMEM;
805                 goto out_free;
806         }
807
808         memset(&req, 0, sizeof(req));
809         strcpy(req.ifr_name, name);
810         ret = ioctl(fd, SIOCGIFHWADDR, &req);
811         if (ret)
812                 goto out_free;
813
814         memcpy(eth_dev->data->mac_addrs[0].addr_bytes,
815                req.ifr_addr.sa_data, RTE_ETHER_ADDR_LEN);
816
817         eth_dev->data->kdrv = RTE_KDRV_NONE;
818         eth_dev->device = &vdev->device;
819         eth_dev->rx_pkt_burst = mvneta_rx_pkt_burst;
820         mvneta_set_tx_function(eth_dev);
821         eth_dev->dev_ops = &mvneta_ops;
822
823         /* Flag to call rte_eth_dev_release_port() in rte_eth_dev_close(). */
824         eth_dev->data->dev_flags |= RTE_ETH_DEV_CLOSE_REMOVE;
825
826         rte_eth_dev_probing_finish(eth_dev);
827         return 0;
828 out_free:
829         rte_eth_dev_release_port(eth_dev);
830
831         return ret;
832 }
833
834 /**
835  * Cleanup previously created device representing Ethernet port.
836  *
837  * @param eth_dev
838  *   Pointer to the corresponding rte_eth_dev structure.
839  */
840 static void
841 mvneta_eth_dev_destroy(struct rte_eth_dev *eth_dev)
842 {
843         rte_eth_dev_release_port(eth_dev);
844 }
845
846 /**
847  * Cleanup previously created device representing Ethernet port.
848  *
849  * @param name
850  *   Pointer to the port name.
851  */
852 static void
853 mvneta_eth_dev_destroy_name(const char *name)
854 {
855         struct rte_eth_dev *eth_dev;
856
857         eth_dev = rte_eth_dev_allocated(name);
858         if (!eth_dev)
859                 return;
860
861         mvneta_eth_dev_destroy(eth_dev);
862 }
863
864 /**
865  * DPDK callback to register the virtual device.
866  *
867  * @param vdev
868  *   Pointer to the virtual device.
869  *
870  * @return
871  *   0 on success, negative error value otherwise.
872  */
873 static int
874 rte_pmd_mvneta_probe(struct rte_vdev_device *vdev)
875 {
876         struct rte_kvargs *kvlist;
877         struct mvneta_ifnames ifnames;
878         int ret = -EINVAL;
879         uint32_t i, ifnum;
880         const char *params;
881
882         params = rte_vdev_device_args(vdev);
883         if (!params)
884                 return -EINVAL;
885
886         kvlist = rte_kvargs_parse(params, valid_args);
887         if (!kvlist)
888                 return -EINVAL;
889
890         ifnum = rte_kvargs_count(kvlist, MVNETA_IFACE_NAME_ARG);
891         if (ifnum > RTE_DIM(ifnames.names))
892                 goto out_free_kvlist;
893
894         ifnames.idx = 0;
895         rte_kvargs_process(kvlist, MVNETA_IFACE_NAME_ARG,
896                            mvneta_ifnames_get, &ifnames);
897
898         /*
899          * The below system initialization should be done only once,
900          * on the first provided configuration file
901          */
902         if (mvneta_dev_num)
903                 goto init_devices;
904
905         MVNETA_LOG(INFO, "Perform MUSDK initializations");
906
907         ret = rte_mvep_init(MVEP_MOD_T_NETA, kvlist);
908         if (ret)
909                 goto out_free_kvlist;
910
911         ret = mvneta_neta_init();
912         if (ret) {
913                 MVNETA_LOG(ERR, "Failed to init NETA!");
914                 rte_mvep_deinit(MVEP_MOD_T_NETA);
915                 goto out_free_kvlist;
916         }
917
918 init_devices:
919         for (i = 0; i < ifnum; i++) {
920                 MVNETA_LOG(INFO, "Creating %s", ifnames.names[i]);
921                 ret = mvneta_eth_dev_create(vdev, ifnames.names[i]);
922                 if (ret)
923                         goto out_cleanup;
924
925                 mvneta_dev_num++;
926         }
927
928         rte_kvargs_free(kvlist);
929
930         return 0;
931 out_cleanup:
932         rte_pmd_mvneta_remove(vdev);
933
934 out_free_kvlist:
935         rte_kvargs_free(kvlist);
936
937         return ret;
938 }
939
940 /**
941  * DPDK callback to remove virtual device.
942  *
943  * @param vdev
944  *   Pointer to the removed virtual device.
945  *
946  * @return
947  *   0 on success, negative error value otherwise.
948  */
949 static int
950 rte_pmd_mvneta_remove(struct rte_vdev_device *vdev)
951 {
952         uint16_t port_id;
953
954         RTE_ETH_FOREACH_DEV(port_id) {
955                 if (rte_eth_devices[port_id].device != &vdev->device)
956                         continue;
957                 rte_eth_dev_close(port_id);
958         }
959
960         return 0;
961 }
962
963 static struct rte_vdev_driver pmd_mvneta_drv = {
964         .probe = rte_pmd_mvneta_probe,
965         .remove = rte_pmd_mvneta_remove,
966 };
967
968 RTE_PMD_REGISTER_VDEV(net_mvneta, pmd_mvneta_drv);
969 RTE_PMD_REGISTER_PARAM_STRING(net_mvneta, "iface=<ifc>");
970
971 RTE_INIT(mvneta_init_log)
972 {
973         mvneta_logtype = rte_log_register("pmd.net.mvneta");
974         if (mvneta_logtype >= 0)
975                 rte_log_set_level(mvneta_logtype, RTE_LOG_NOTICE);
976 }