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