net/mvneta: support promiscuous mode
[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_ethdev_driver.h>
8 #include <rte_kvargs.h>
9 #include <rte_bus_vdev.h>
10
11 #include <stdio.h>
12 #include <fcntl.h>
13 #include <linux/ethtool.h>
14 #include <linux/sockios.h>
15 #include <net/if.h>
16 #include <net/if_arp.h>
17 #include <sys/ioctl.h>
18 #include <sys/socket.h>
19 #include <sys/stat.h>
20 #include <sys/types.h>
21
22 #include <rte_mvep_common.h>
23
24 #include "mvneta_rxtx.h"
25
26
27 #define MVNETA_IFACE_NAME_ARG "iface"
28
29 #define MVNETA_RX_OFFLOADS (DEV_RX_OFFLOAD_JUMBO_FRAME | \
30                           DEV_RX_OFFLOAD_CHECKSUM)
31
32 /** Port Tx offloads capabilities */
33 #define MVNETA_TX_OFFLOADS (DEV_TX_OFFLOAD_IPV4_CKSUM | \
34                           DEV_TX_OFFLOAD_UDP_CKSUM | \
35                           DEV_TX_OFFLOAD_TCP_CKSUM | \
36                           DEV_TX_OFFLOAD_MULTI_SEGS)
37
38 #define MVNETA_PKT_SIZE_MAX (16382 - MV_MH_SIZE) /* 9700B */
39 #define MVNETA_DEFAULT_MTU 1500
40
41 #define MVNETA_MAC_ADDRS_MAX 256 /*16 UC, 256 IP, 256 MC/BC */
42 /** Maximum length of a match string */
43 #define MVNETA_MATCH_LEN 16
44
45 int mvneta_logtype;
46
47 static const char * const valid_args[] = {
48         MVNETA_IFACE_NAME_ARG,
49         NULL
50 };
51
52 struct mvneta_ifnames {
53         const char *names[NETA_NUM_ETH_PPIO];
54         int idx;
55 };
56
57 static int mvneta_dev_num;
58
59 /**
60  * Deinitialize packet processor.
61  */
62 static void
63 mvneta_neta_deinit(void)
64 {
65         neta_deinit();
66 }
67
68 /**
69  * Initialize packet processor.
70  *
71  * @return
72  *   0 on success, negative error value otherwise.
73  */
74 static int
75 mvneta_neta_init(void)
76 {
77         return neta_init();
78 }
79
80 /**
81  * Callback used by rte_kvargs_process() during argument parsing.
82  *
83  * @param key
84  *   Pointer to the parsed key (unused).
85  * @param value
86  *   Pointer to the parsed value.
87  * @param extra_args
88  *   Pointer to the extra arguments which contains address of the
89  *   table of pointers to parsed interface names.
90  *
91  * @return
92  *   Always 0.
93  */
94 static int
95 mvneta_ifnames_get(const char *key __rte_unused, const char *value,
96                  void *extra_args)
97 {
98         struct mvneta_ifnames *ifnames = extra_args;
99
100         ifnames->names[ifnames->idx++] = value;
101
102         return 0;
103 }
104
105 /**
106  * Ethernet device configuration.
107  *
108  * Prepare the driver for a given number of TX and RX queues and
109  * configure RSS if supported.
110  *
111  * @param dev
112  *   Pointer to Ethernet device structure.
113  *
114  * @return
115  *   0 on success, negative error value otherwise.
116  */
117 static int
118 mvneta_dev_configure(struct rte_eth_dev *dev)
119 {
120         struct mvneta_priv *priv = dev->data->dev_private;
121         struct neta_ppio_params *ppio_params;
122
123         if (dev->data->dev_conf.rxmode.mq_mode != ETH_MQ_RX_NONE) {
124                 MVNETA_LOG(INFO, "Unsupported RSS and rx multi queue mode %d",
125                         dev->data->dev_conf.rxmode.mq_mode);
126                 if (dev->data->nb_rx_queues > 1)
127                         return -EINVAL;
128         }
129
130         if (dev->data->dev_conf.rxmode.split_hdr_size) {
131                 MVNETA_LOG(INFO, "Split headers not supported");
132                 return -EINVAL;
133         }
134
135         if (dev->data->dev_conf.rxmode.offloads & DEV_RX_OFFLOAD_JUMBO_FRAME)
136                 dev->data->mtu = dev->data->dev_conf.rxmode.max_rx_pkt_len -
137                                  MRVL_NETA_ETH_HDRS_LEN;
138
139         if (dev->data->dev_conf.txmode.offloads & DEV_TX_OFFLOAD_MULTI_SEGS)
140                 priv->multiseg = 1;
141
142         ppio_params = &priv->ppio_params;
143         ppio_params->outqs_params.num_outqs = dev->data->nb_tx_queues;
144         /* Default: 1 TC, no QoS supported. */
145         ppio_params->inqs_params.num_tcs = 1;
146         ppio_params->inqs_params.tcs_params[0].pkt_offset = MRVL_NETA_PKT_OFFS;
147         priv->ppio_id = dev->data->port_id;
148
149         return 0;
150 }
151
152 /**
153  * DPDK callback to get information about the device.
154  *
155  * @param dev
156  *   Pointer to Ethernet device structure (unused).
157  * @param info
158  *   Info structure output buffer.
159  */
160 static void
161 mvneta_dev_infos_get(struct rte_eth_dev *dev __rte_unused,
162                    struct rte_eth_dev_info *info)
163 {
164         info->speed_capa = ETH_LINK_SPEED_10M |
165                            ETH_LINK_SPEED_100M |
166                            ETH_LINK_SPEED_1G |
167                            ETH_LINK_SPEED_2_5G;
168
169         info->max_rx_queues = MRVL_NETA_RXQ_MAX;
170         info->max_tx_queues = MRVL_NETA_TXQ_MAX;
171         info->max_mac_addrs = MVNETA_MAC_ADDRS_MAX;
172
173         info->rx_desc_lim.nb_max = MRVL_NETA_RXD_MAX;
174         info->rx_desc_lim.nb_min = MRVL_NETA_RXD_MIN;
175         info->rx_desc_lim.nb_align = MRVL_NETA_RXD_ALIGN;
176
177         info->tx_desc_lim.nb_max = MRVL_NETA_TXD_MAX;
178         info->tx_desc_lim.nb_min = MRVL_NETA_TXD_MIN;
179         info->tx_desc_lim.nb_align = MRVL_NETA_TXD_ALIGN;
180
181         info->rx_offload_capa = MVNETA_RX_OFFLOADS;
182         info->rx_queue_offload_capa = MVNETA_RX_OFFLOADS;
183
184         info->tx_offload_capa =  MVNETA_TX_OFFLOADS;
185         info->tx_queue_offload_capa =  MVNETA_TX_OFFLOADS;
186
187         /* By default packets are dropped if no descriptors are available */
188         info->default_rxconf.rx_drop_en = 1;
189         /* Deferred tx queue start is not supported */
190         info->default_txconf.tx_deferred_start = 0;
191         info->default_txconf.offloads = 0;
192
193         info->max_rx_pktlen = MVNETA_PKT_SIZE_MAX;
194 }
195
196 /**
197  * Return supported packet types.
198  *
199  * @param dev
200  *   Pointer to Ethernet device structure (unused).
201  *
202  * @return
203  *   Const pointer to the table with supported packet types.
204  */
205 static const uint32_t *
206 mvneta_dev_supported_ptypes_get(struct rte_eth_dev *dev __rte_unused)
207 {
208         static const uint32_t ptypes[] = {
209                 RTE_PTYPE_L2_ETHER,
210                 RTE_PTYPE_L2_ETHER_VLAN,
211                 RTE_PTYPE_L3_IPV4,
212                 RTE_PTYPE_L3_IPV6,
213                 RTE_PTYPE_L4_TCP,
214                 RTE_PTYPE_L4_UDP
215         };
216
217         return ptypes;
218 }
219
220 /**
221  * DPDK callback to change the MTU.
222  *
223  * Setting the MTU affects hardware MRU (packets larger than the MRU
224  * will be dropped).
225  *
226  * @param dev
227  *   Pointer to Ethernet device structure.
228  * @param mtu
229  *   New MTU.
230  *
231  * @return
232  *   0 on success, negative error value otherwise.
233  */
234 static int
235 mvneta_mtu_set(struct rte_eth_dev *dev, uint16_t mtu)
236 {
237         struct mvneta_priv *priv = dev->data->dev_private;
238         uint16_t mbuf_data_size = 0; /* SW buffer size */
239         uint16_t mru;
240         int ret;
241
242         mru = MRVL_NETA_MTU_TO_MRU(mtu);
243         /*
244          * min_rx_buf_size is equal to mbuf data size
245          * if pmd didn't set it differently
246          */
247         mbuf_data_size = dev->data->min_rx_buf_size - RTE_PKTMBUF_HEADROOM;
248         /* Prevent PMD from:
249          * - setting mru greater than the mbuf size resulting in
250          * hw and sw buffer size mismatch
251          * - setting mtu that requires the support of scattered packets
252          * when this feature has not been enabled/supported so far.
253          */
254         if (!dev->data->scattered_rx &&
255             (mru + MRVL_NETA_PKT_OFFS > mbuf_data_size)) {
256                 mru = mbuf_data_size - MRVL_NETA_PKT_OFFS;
257                 mtu = MRVL_NETA_MRU_TO_MTU(mru);
258                 MVNETA_LOG(WARNING, "MTU too big, max MTU possible limitted by"
259                         " current mbuf size: %u. Set MTU to %u, MRU to %u",
260                         mbuf_data_size, mtu, mru);
261         }
262
263         if (mtu < ETHER_MIN_MTU || mru > MVNETA_PKT_SIZE_MAX) {
264                 MVNETA_LOG(ERR, "Invalid MTU [%u] or MRU [%u]", mtu, mru);
265                 return -EINVAL;
266         }
267
268         dev->data->mtu = mtu;
269         dev->data->dev_conf.rxmode.max_rx_pkt_len = mru - MV_MH_SIZE;
270
271         if (!priv->ppio)
272                 /* It is OK. New MTU will be set later on mvneta_dev_start */
273                 return 0;
274
275         ret = neta_ppio_set_mru(priv->ppio, mru);
276         if (ret) {
277                 MVNETA_LOG(ERR, "Failed to change MRU");
278                 return ret;
279         }
280
281         ret = neta_ppio_set_mtu(priv->ppio, mtu);
282         if (ret) {
283                 MVNETA_LOG(ERR, "Failed to change MTU");
284                 return ret;
285         }
286         MVNETA_LOG(INFO, "MTU changed to %u, MRU = %u", mtu, mru);
287
288         return 0;
289 }
290
291 /**
292  * DPDK callback to bring the link up.
293  *
294  * @param dev
295  *   Pointer to Ethernet device structure.
296  *
297  * @return
298  *   0 on success, negative error value otherwise.
299  */
300 static int
301 mvneta_dev_set_link_up(struct rte_eth_dev *dev)
302 {
303         struct mvneta_priv *priv = dev->data->dev_private;
304
305         if (!priv->ppio)
306                 return 0;
307
308         return neta_ppio_enable(priv->ppio);
309 }
310
311 /**
312  * DPDK callback to bring the link down.
313  *
314  * @param dev
315  *   Pointer to Ethernet device structure.
316  *
317  * @return
318  *   0 on success, negative error value otherwise.
319  */
320 static int
321 mvneta_dev_set_link_down(struct rte_eth_dev *dev)
322 {
323         struct mvneta_priv *priv = dev->data->dev_private;
324
325         if (!priv->ppio)
326                 return 0;
327
328         return neta_ppio_disable(priv->ppio);
329 }
330
331 /**
332  * DPDK callback to start the device.
333  *
334  * @param dev
335  *   Pointer to Ethernet device structure.
336  *
337  * @return
338  *   0 on success, negative errno value on failure.
339  */
340 static int
341 mvneta_dev_start(struct rte_eth_dev *dev)
342 {
343         struct mvneta_priv *priv = dev->data->dev_private;
344         char match[MVNETA_MATCH_LEN];
345         int ret = 0, i;
346
347         if (priv->ppio)
348                 return mvneta_dev_set_link_up(dev);
349
350         snprintf(match, sizeof(match), "%s", dev->data->name);
351         priv->ppio_params.match = match;
352         priv->ppio_params.inqs_params.mtu = dev->data->mtu;
353
354         ret = neta_ppio_init(&priv->ppio_params, &priv->ppio);
355         if (ret) {
356                 MVNETA_LOG(ERR, "Failed to init ppio");
357                 return ret;
358         }
359         priv->ppio_id = priv->ppio->port_id;
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
454 /**
455  * DPDK callback to retrieve physical link information.
456  *
457  * @param dev
458  *   Pointer to Ethernet device structure.
459  * @param wait_to_complete
460  *   Wait for request completion (ignored).
461  *
462  * @return
463  *   0 on success, negative error value otherwise.
464  */
465 static int
466 mvneta_link_update(struct rte_eth_dev *dev, int wait_to_complete __rte_unused)
467 {
468         /*
469          * TODO
470          * once MUSDK provides necessary API use it here
471          */
472         struct mvneta_priv *priv = dev->data->dev_private;
473         struct ethtool_cmd edata;
474         struct ifreq req;
475         int ret, fd, link_up;
476
477         if (!priv->ppio)
478                 return -EPERM;
479
480         edata.cmd = ETHTOOL_GSET;
481
482         strcpy(req.ifr_name, dev->data->name);
483         req.ifr_data = (void *)&edata;
484
485         fd = socket(AF_INET, SOCK_DGRAM, 0);
486         if (fd == -1)
487                 return -EFAULT;
488         ret = ioctl(fd, SIOCETHTOOL, &req);
489         if (ret == -1) {
490                 close(fd);
491                 return -EFAULT;
492         }
493
494         close(fd);
495
496         switch (ethtool_cmd_speed(&edata)) {
497         case SPEED_10:
498                 dev->data->dev_link.link_speed = ETH_SPEED_NUM_10M;
499                 break;
500         case SPEED_100:
501                 dev->data->dev_link.link_speed = ETH_SPEED_NUM_100M;
502                 break;
503         case SPEED_1000:
504                 dev->data->dev_link.link_speed = ETH_SPEED_NUM_1G;
505                 break;
506         case SPEED_2500:
507                 dev->data->dev_link.link_speed = ETH_SPEED_NUM_2_5G;
508                 break;
509         default:
510                 dev->data->dev_link.link_speed = ETH_SPEED_NUM_NONE;
511         }
512
513         dev->data->dev_link.link_duplex = edata.duplex ? ETH_LINK_FULL_DUPLEX :
514                                                          ETH_LINK_HALF_DUPLEX;
515         dev->data->dev_link.link_autoneg = edata.autoneg ? ETH_LINK_AUTONEG :
516                                                            ETH_LINK_FIXED;
517
518         neta_ppio_get_link_state(priv->ppio, &link_up);
519         dev->data->dev_link.link_status = link_up ? ETH_LINK_UP : ETH_LINK_DOWN;
520
521         return 0;
522 }
523
524 /**
525  * DPDK callback to enable promiscuous mode.
526  *
527  * @param dev
528  *   Pointer to Ethernet device structure.
529  */
530 static void
531 mvneta_promiscuous_enable(struct rte_eth_dev *dev)
532 {
533         struct mvneta_priv *priv = dev->data->dev_private;
534         int ret, en;
535
536         if (!priv->ppio)
537                 return;
538
539         neta_ppio_get_promisc(priv->ppio, &en);
540         if (en) {
541                 MVNETA_LOG(INFO, "Promiscuous already enabled");
542                 return;
543         }
544
545         ret = neta_ppio_set_promisc(priv->ppio, 1);
546         if (ret)
547                 MVNETA_LOG(ERR, "Failed to enable promiscuous mode");
548 }
549
550 /**
551  * DPDK callback to disable allmulticast mode.
552  *
553  * @param dev
554  *   Pointer to Ethernet device structure.
555  */
556 static void
557 mvneta_promiscuous_disable(struct rte_eth_dev *dev)
558 {
559         struct mvneta_priv *priv = dev->data->dev_private;
560         int ret, en;
561
562         if (!priv->ppio)
563                 return;
564
565         neta_ppio_get_promisc(priv->ppio, &en);
566         if (!en) {
567                 MVNETA_LOG(INFO, "Promiscuous already disabled");
568                 return;
569         }
570
571         ret = neta_ppio_set_promisc(priv->ppio, 0);
572         if (ret)
573                 MVNETA_LOG(ERR, "Failed to disable promiscuous mode");
574 }
575
576 /**
577  * DPDK callback to set the primary MAC address.
578  *
579  * @param dev
580  *   Pointer to Ethernet device structure.
581  * @param mac_addr
582  *   MAC address to register.
583  */
584 static int
585 mvneta_mac_addr_set(struct rte_eth_dev *dev, struct ether_addr *mac_addr)
586 {
587         struct mvneta_priv *priv = dev->data->dev_private;
588         int ret;
589
590         if (!priv->ppio)
591                 return -EINVAL;
592
593         ret = neta_ppio_set_mac_addr(priv->ppio, mac_addr->addr_bytes);
594         if (ret) {
595                 char buf[ETHER_ADDR_FMT_SIZE];
596                 ether_format_addr(buf, sizeof(buf), mac_addr);
597                 MVNETA_LOG(ERR, "Failed to set mac to %s", buf);
598         }
599         return 0;
600 }
601
602 static const struct eth_dev_ops mvneta_ops = {
603         .dev_configure = mvneta_dev_configure,
604         .dev_start = mvneta_dev_start,
605         .dev_stop = mvneta_dev_stop,
606         .dev_set_link_up = mvneta_dev_set_link_up,
607         .dev_set_link_down = mvneta_dev_set_link_down,
608         .dev_close = mvneta_dev_close,
609         .link_update = mvneta_link_update,
610         .promiscuous_enable = mvneta_promiscuous_enable,
611         .promiscuous_disable = mvneta_promiscuous_disable,
612         .mac_addr_set = mvneta_mac_addr_set,
613         .mtu_set = mvneta_mtu_set,
614         .dev_infos_get = mvneta_dev_infos_get,
615         .dev_supported_ptypes_get = mvneta_dev_supported_ptypes_get,
616         .rxq_info_get = mvneta_rxq_info_get,
617         .txq_info_get = mvneta_txq_info_get,
618         .rx_queue_setup = mvneta_rx_queue_setup,
619         .rx_queue_release = mvneta_rx_queue_release,
620         .tx_queue_setup = mvneta_tx_queue_setup,
621         .tx_queue_release = mvneta_tx_queue_release,
622 };
623
624 /**
625  * Create device representing Ethernet port.
626  *
627  * @param name
628  *   Pointer to the port's name.
629  *
630  * @return
631  *   0 on success, negative error value otherwise.
632  */
633 static int
634 mvneta_eth_dev_create(struct rte_vdev_device *vdev, const char *name)
635 {
636         int ret, fd = socket(AF_INET, SOCK_DGRAM, 0);
637         struct rte_eth_dev *eth_dev;
638         struct mvneta_priv *priv;
639         struct ifreq req;
640
641         eth_dev = rte_eth_dev_allocate(name);
642         if (!eth_dev)
643                 return -ENOMEM;
644
645         priv = rte_zmalloc_socket(name, sizeof(*priv), 0, rte_socket_id());
646         if (!priv) {
647                 ret = -ENOMEM;
648                 goto out_free_dev;
649         }
650
651         eth_dev->data->mac_addrs =
652                 rte_zmalloc("mac_addrs",
653                             ETHER_ADDR_LEN * MVNETA_MAC_ADDRS_MAX, 0);
654         if (!eth_dev->data->mac_addrs) {
655                 MVNETA_LOG(ERR, "Failed to allocate space for eth addrs");
656                 ret = -ENOMEM;
657                 goto out_free_priv;
658         }
659
660         memset(&req, 0, sizeof(req));
661         strcpy(req.ifr_name, name);
662         ret = ioctl(fd, SIOCGIFHWADDR, &req);
663         if (ret)
664                 goto out_free_mac;
665
666         memcpy(eth_dev->data->mac_addrs[0].addr_bytes,
667                req.ifr_addr.sa_data, ETHER_ADDR_LEN);
668
669         eth_dev->data->kdrv = RTE_KDRV_NONE;
670         eth_dev->data->dev_private = priv;
671         eth_dev->device = &vdev->device;
672         eth_dev->rx_pkt_burst = mvneta_rx_pkt_burst;
673         mvneta_set_tx_function(eth_dev);
674         eth_dev->dev_ops = &mvneta_ops;
675
676         rte_eth_dev_probing_finish(eth_dev);
677         return 0;
678 out_free_mac:
679         rte_free(eth_dev->data->mac_addrs);
680 out_free_priv:
681         rte_free(priv);
682 out_free_dev:
683         rte_eth_dev_release_port(eth_dev);
684
685         return ret;
686 }
687
688 /**
689  * Cleanup previously created device representing Ethernet port.
690  *
691  * @param eth_dev
692  *   Pointer to the corresponding rte_eth_dev structure.
693  */
694 static void
695 mvneta_eth_dev_destroy(struct rte_eth_dev *eth_dev)
696 {
697         rte_free(eth_dev->data->dev_private);
698         rte_free(eth_dev->data->mac_addrs);
699         rte_eth_dev_release_port(eth_dev);
700 }
701
702 /**
703  * Cleanup previously created device representing Ethernet port.
704  *
705  * @param name
706  *   Pointer to the port name.
707  */
708 static void
709 mvneta_eth_dev_destroy_name(const char *name)
710 {
711         struct rte_eth_dev *eth_dev;
712
713         eth_dev = rte_eth_dev_allocated(name);
714         if (!eth_dev)
715                 return;
716
717         mvneta_eth_dev_destroy(eth_dev);
718 }
719
720 /**
721  * DPDK callback to register the virtual device.
722  *
723  * @param vdev
724  *   Pointer to the virtual device.
725  *
726  * @return
727  *   0 on success, negative error value otherwise.
728  */
729 static int
730 rte_pmd_mvneta_probe(struct rte_vdev_device *vdev)
731 {
732         struct rte_kvargs *kvlist;
733         struct mvneta_ifnames ifnames;
734         int ret = -EINVAL;
735         uint32_t i, ifnum;
736         const char *params;
737
738         params = rte_vdev_device_args(vdev);
739         if (!params)
740                 return -EINVAL;
741
742         kvlist = rte_kvargs_parse(params, valid_args);
743         if (!kvlist)
744                 return -EINVAL;
745
746         ifnum = rte_kvargs_count(kvlist, MVNETA_IFACE_NAME_ARG);
747         if (ifnum > RTE_DIM(ifnames.names))
748                 goto out_free_kvlist;
749
750         ifnames.idx = 0;
751         rte_kvargs_process(kvlist, MVNETA_IFACE_NAME_ARG,
752                            mvneta_ifnames_get, &ifnames);
753
754         /*
755          * The below system initialization should be done only once,
756          * on the first provided configuration file
757          */
758         if (mvneta_dev_num)
759                 goto init_devices;
760
761         MVNETA_LOG(INFO, "Perform MUSDK initializations");
762
763         ret = rte_mvep_init(MVEP_MOD_T_NETA, kvlist);
764         if (ret)
765                 goto out_free_kvlist;
766
767         ret = mvneta_neta_init();
768         if (ret) {
769                 MVNETA_LOG(ERR, "Failed to init NETA!");
770                 rte_mvep_deinit(MVEP_MOD_T_NETA);
771                 goto out_free_kvlist;
772         }
773
774 init_devices:
775         for (i = 0; i < ifnum; i++) {
776                 MVNETA_LOG(INFO, "Creating %s", ifnames.names[i]);
777                 ret = mvneta_eth_dev_create(vdev, ifnames.names[i]);
778                 if (ret)
779                         goto out_cleanup;
780         }
781         mvneta_dev_num += ifnum;
782
783         rte_kvargs_free(kvlist);
784
785         return 0;
786 out_cleanup:
787         for (; i > 0; i--)
788                 mvneta_eth_dev_destroy_name(ifnames.names[i]);
789
790         if (mvneta_dev_num == 0) {
791                 mvneta_neta_deinit();
792                 rte_mvep_deinit(MVEP_MOD_T_NETA);
793         }
794 out_free_kvlist:
795         rte_kvargs_free(kvlist);
796
797         return ret;
798 }
799
800 /**
801  * DPDK callback to remove virtual device.
802  *
803  * @param vdev
804  *   Pointer to the removed virtual device.
805  *
806  * @return
807  *   0 on success, negative error value otherwise.
808  */
809 static int
810 rte_pmd_mvneta_remove(struct rte_vdev_device *vdev)
811 {
812         int i;
813         const char *name;
814
815         name = rte_vdev_device_name(vdev);
816         if (!name)
817                 return -EINVAL;
818
819         MVNETA_LOG(INFO, "Removing %s", name);
820
821         RTE_ETH_FOREACH_DEV(i) {
822                 if (rte_eth_devices[i].device != &vdev->device)
823                         continue;
824
825                 mvneta_eth_dev_destroy(&rte_eth_devices[i]);
826                 mvneta_dev_num--;
827         }
828
829         if (mvneta_dev_num == 0) {
830                 MVNETA_LOG(INFO, "Perform MUSDK deinit");
831                 mvneta_neta_deinit();
832                 rte_mvep_deinit(MVEP_MOD_T_NETA);
833         }
834
835         return 0;
836 }
837
838 static struct rte_vdev_driver pmd_mvneta_drv = {
839         .probe = rte_pmd_mvneta_probe,
840         .remove = rte_pmd_mvneta_remove,
841 };
842
843 RTE_PMD_REGISTER_VDEV(net_mvneta, pmd_mvneta_drv);
844 RTE_PMD_REGISTER_PARAM_STRING(net_mvneta, "iface=<ifc>");
845
846 RTE_INIT(mvneta_init_log)
847 {
848         mvneta_logtype = rte_log_register("pmd.net.mvneta");
849         if (mvneta_logtype >= 0)
850                 rte_log_set_level(mvneta_logtype, RTE_LOG_NOTICE);
851 }