1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2018 Marvell International Ltd.
3 * Copyright(c) 2018 Semihalf.
7 #include <rte_string_fns.h>
8 #include <ethdev_driver.h>
9 #include <rte_kvargs.h>
10 #include <rte_bus_vdev.h>
14 #include <linux/ethtool.h>
15 #include <linux/sockios.h>
17 #include <net/if_arp.h>
18 #include <sys/ioctl.h>
19 #include <sys/socket.h>
21 #include <sys/types.h>
23 #include <rte_mvep_common.h>
25 #include "mvneta_rxtx.h"
28 #define MVNETA_IFACE_NAME_ARG "iface"
30 #define MVNETA_PKT_SIZE_MAX (16382 - MV_MH_SIZE) /* 9700B */
31 #define MVNETA_DEFAULT_MTU 1500
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
37 static const char * const valid_args[] = {
38 MVNETA_IFACE_NAME_ARG,
42 struct mvneta_ifnames {
43 const char *names[NETA_NUM_ETH_PPIO];
47 static int mvneta_dev_num;
49 static int mvneta_stats_reset(struct rte_eth_dev *dev);
50 static int rte_pmd_mvneta_remove(struct rte_vdev_device *vdev);
54 * Deinitialize packet processor.
57 mvneta_neta_deinit(void)
63 * Initialize packet processor.
66 * 0 on success, negative error value otherwise.
69 mvneta_neta_init(void)
75 * Callback used by rte_kvargs_process() during argument parsing.
78 * Pointer to the parsed key (unused).
80 * Pointer to the parsed value.
82 * Pointer to the extra arguments which contains address of the
83 * table of pointers to parsed interface names.
89 mvneta_ifnames_get(const char *key __rte_unused, const char *value,
92 struct mvneta_ifnames *ifnames = extra_args;
94 ifnames->names[ifnames->idx++] = value;
100 * Ethernet device configuration.
102 * Prepare the driver for a given number of TX and RX queues and
103 * configure RSS if supported.
106 * Pointer to Ethernet device structure.
109 * 0 on success, negative error value otherwise.
112 mvneta_dev_configure(struct rte_eth_dev *dev)
114 struct mvneta_priv *priv = dev->data->dev_private;
115 struct neta_ppio_params *ppio_params;
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)
124 if (dev->data->dev_conf.rxmode.split_hdr_size) {
125 MVNETA_LOG(INFO, "Split headers not supported");
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;
133 if (dev->data->dev_conf.txmode.offloads & DEV_TX_OFFLOAD_MULTI_SEGS)
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;
147 * DPDK callback to get information about the device.
150 * Pointer to Ethernet device structure (unused).
152 * Info structure output buffer.
155 mvneta_dev_infos_get(struct rte_eth_dev *dev __rte_unused,
156 struct rte_eth_dev_info *info)
158 info->speed_capa = ETH_LINK_SPEED_10M |
159 ETH_LINK_SPEED_100M |
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;
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;
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;
175 info->rx_offload_capa = MVNETA_RX_OFFLOADS;
176 info->rx_queue_offload_capa = MVNETA_RX_OFFLOADS;
178 info->tx_offload_capa = MVNETA_TX_OFFLOADS;
179 info->tx_queue_offload_capa = MVNETA_TX_OFFLOADS;
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;
187 info->max_rx_pktlen = MVNETA_PKT_SIZE_MAX;
193 * Return supported packet types.
196 * Pointer to Ethernet device structure (unused).
199 * Const pointer to the table with supported packet types.
201 static const uint32_t *
202 mvneta_dev_supported_ptypes_get(struct rte_eth_dev *dev __rte_unused)
204 static const uint32_t ptypes[] = {
206 RTE_PTYPE_L2_ETHER_VLAN,
217 * DPDK callback to change the MTU.
219 * Setting the MTU affects hardware MRU (packets larger than the MRU
223 * Pointer to Ethernet device structure.
228 * 0 on success, negative error value otherwise.
231 mvneta_mtu_set(struct rte_eth_dev *dev, uint16_t mtu)
233 struct mvneta_priv *priv = dev->data->dev_private;
234 uint16_t mbuf_data_size = 0; /* SW buffer size */
238 mru = MRVL_NETA_MTU_TO_MRU(mtu);
240 * min_rx_buf_size is equal to mbuf data size
241 * if pmd didn't set it differently
243 mbuf_data_size = dev->data->min_rx_buf_size - RTE_PKTMBUF_HEADROOM;
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.
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);
259 if (mtu < RTE_ETHER_MIN_MTU || mru > MVNETA_PKT_SIZE_MAX) {
260 MVNETA_LOG(ERR, "Invalid MTU [%u] or MRU [%u]", mtu, mru);
264 dev->data->mtu = mtu;
265 dev->data->dev_conf.rxmode.max_rx_pkt_len = mru - MV_MH_SIZE;
268 /* It is OK. New MTU will be set later on mvneta_dev_start */
271 ret = neta_ppio_set_mru(priv->ppio, mru);
273 MVNETA_LOG(ERR, "Failed to change MRU");
277 ret = neta_ppio_set_mtu(priv->ppio, mtu);
279 MVNETA_LOG(ERR, "Failed to change MTU");
282 MVNETA_LOG(INFO, "MTU changed to %u, MRU = %u", mtu, mru);
288 * DPDK callback to bring the link up.
291 * Pointer to Ethernet device structure.
294 * 0 on success, negative error value otherwise.
297 mvneta_dev_set_link_up(struct rte_eth_dev *dev)
299 struct mvneta_priv *priv = dev->data->dev_private;
304 return neta_ppio_enable(priv->ppio);
308 * DPDK callback to bring the link down.
311 * Pointer to Ethernet device structure.
314 * 0 on success, negative error value otherwise.
317 mvneta_dev_set_link_down(struct rte_eth_dev *dev)
319 struct mvneta_priv *priv = dev->data->dev_private;
324 return neta_ppio_disable(priv->ppio);
328 * DPDK callback to start the device.
331 * Pointer to Ethernet device structure.
334 * 0 on success, negative errno value on failure.
337 mvneta_dev_start(struct rte_eth_dev *dev)
339 struct mvneta_priv *priv = dev->data->dev_private;
340 char match[MVNETA_MATCH_LEN];
344 return mvneta_dev_set_link_up(dev);
346 strlcpy(match, dev->data->name, sizeof(match));
347 priv->ppio_params.match = match;
348 priv->ppio_params.inqs_params.mtu = dev->data->mtu;
350 ret = neta_ppio_init(&priv->ppio_params, &priv->ppio);
352 MVNETA_LOG(ERR, "Failed to init ppio");
355 priv->ppio_id = priv->ppio->port_id;
357 mvneta_stats_reset(dev);
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()).
365 if (!priv->uc_mc_flushed) {
366 ret = neta_ppio_flush_mac_addrs(priv->ppio, 0, 1);
369 "Failed to flush uc/mc filter list");
372 priv->uc_mc_flushed = 1;
375 ret = mvneta_alloc_rx_bufs(dev);
379 ret = mvneta_mtu_set(dev, dev->data->mtu);
381 MVNETA_LOG(ERR, "Failed to set MTU %d", dev->data->mtu);
385 ret = mvneta_dev_set_link_up(dev);
387 MVNETA_LOG(ERR, "Failed to set link up");
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;
395 mvneta_set_tx_function(dev);
400 MVNETA_LOG(ERR, "Failed to start device");
401 neta_ppio_deinit(priv->ppio);
406 * DPDK callback to stop the device.
409 * Pointer to Ethernet device structure.
412 mvneta_dev_stop(struct rte_eth_dev *dev)
414 struct mvneta_priv *priv = dev->data->dev_private;
416 dev->data->dev_started = 0;
421 mvneta_dev_set_link_down(dev);
422 mvneta_flush_queues(dev);
423 neta_ppio_deinit(priv->ppio);
431 * DPDK callback to close the device.
434 * Pointer to Ethernet device structure.
437 mvneta_dev_close(struct rte_eth_dev *dev)
439 struct mvneta_priv *priv = dev->data->dev_private;
442 if (rte_eal_process_type() != RTE_PROC_PRIMARY)
446 ret = mvneta_dev_stop(dev);
448 for (i = 0; i < dev->data->nb_rx_queues; i++) {
449 mvneta_rx_queue_release(dev, i);
450 dev->data->rx_queues[i] = NULL;
453 for (i = 0; i < dev->data->nb_tx_queues; i++) {
454 mvneta_tx_queue_release(dev, i);
455 dev->data->tx_queues[i] = NULL;
460 if (mvneta_dev_num == 0) {
461 MVNETA_LOG(INFO, "Perform MUSDK deinit");
462 mvneta_neta_deinit();
463 rte_mvep_deinit(MVEP_MOD_T_NETA);
470 * DPDK callback to retrieve physical link information.
473 * Pointer to Ethernet device structure.
474 * @param wait_to_complete
475 * Wait for request completion (ignored).
478 * 0 on success, negative error value otherwise.
481 mvneta_link_update(struct rte_eth_dev *dev, int wait_to_complete __rte_unused)
485 * once MUSDK provides necessary API use it here
487 struct mvneta_priv *priv = dev->data->dev_private;
488 struct ethtool_cmd edata;
490 int ret, fd, link_up;
495 edata.cmd = ETHTOOL_GSET;
497 strcpy(req.ifr_name, dev->data->name);
498 req.ifr_data = (void *)&edata;
500 fd = socket(AF_INET, SOCK_DGRAM, 0);
503 ret = ioctl(fd, SIOCETHTOOL, &req);
511 switch (ethtool_cmd_speed(&edata)) {
513 dev->data->dev_link.link_speed = ETH_SPEED_NUM_10M;
516 dev->data->dev_link.link_speed = ETH_SPEED_NUM_100M;
519 dev->data->dev_link.link_speed = ETH_SPEED_NUM_1G;
522 dev->data->dev_link.link_speed = ETH_SPEED_NUM_2_5G;
525 dev->data->dev_link.link_speed = ETH_SPEED_NUM_NONE;
528 dev->data->dev_link.link_duplex = edata.duplex ? ETH_LINK_FULL_DUPLEX :
529 ETH_LINK_HALF_DUPLEX;
530 dev->data->dev_link.link_autoneg = edata.autoneg ? ETH_LINK_AUTONEG :
533 neta_ppio_get_link_state(priv->ppio, &link_up);
534 dev->data->dev_link.link_status = link_up ? ETH_LINK_UP : ETH_LINK_DOWN;
540 * DPDK callback to enable promiscuous mode.
543 * Pointer to Ethernet device structure.
549 mvneta_promiscuous_enable(struct rte_eth_dev *dev)
551 struct mvneta_priv *priv = dev->data->dev_private;
557 neta_ppio_get_promisc(priv->ppio, &en);
559 MVNETA_LOG(INFO, "Promiscuous already enabled");
563 ret = neta_ppio_set_promisc(priv->ppio, 1);
565 MVNETA_LOG(ERR, "Failed to enable promiscuous mode");
571 * DPDK callback to disable allmulticast mode.
574 * Pointer to Ethernet device structure.
580 mvneta_promiscuous_disable(struct rte_eth_dev *dev)
582 struct mvneta_priv *priv = dev->data->dev_private;
588 neta_ppio_get_promisc(priv->ppio, &en);
590 MVNETA_LOG(INFO, "Promiscuous already disabled");
594 ret = neta_ppio_set_promisc(priv->ppio, 0);
596 MVNETA_LOG(ERR, "Failed to disable promiscuous mode");
602 * DPDK callback to remove a MAC address.
605 * Pointer to Ethernet device structure.
610 mvneta_mac_addr_remove(struct rte_eth_dev *dev, uint32_t index)
612 struct mvneta_priv *priv = dev->data->dev_private;
613 char buf[RTE_ETHER_ADDR_FMT_SIZE];
619 ret = neta_ppio_remove_mac_addr(priv->ppio,
620 dev->data->mac_addrs[index].addr_bytes);
622 rte_ether_format_addr(buf, sizeof(buf),
623 &dev->data->mac_addrs[index]);
624 MVNETA_LOG(ERR, "Failed to remove mac %s", buf);
629 * DPDK callback to add a MAC address.
632 * Pointer to Ethernet device structure.
634 * MAC address to register.
638 * VMDq pool index to associate address with (unused).
641 * 0 on success, negative error value otherwise.
644 mvneta_mac_addr_add(struct rte_eth_dev *dev, struct rte_ether_addr *mac_addr,
645 uint32_t index, uint32_t vmdq __rte_unused)
647 struct mvneta_priv *priv = dev->data->dev_private;
648 char buf[RTE_ETHER_ADDR_FMT_SIZE];
652 /* For setting index 0, mrvl_mac_addr_set() should be used.*/
658 ret = neta_ppio_add_mac_addr(priv->ppio, mac_addr->addr_bytes);
660 rte_ether_format_addr(buf, sizeof(buf), mac_addr);
661 MVNETA_LOG(ERR, "Failed to add mac %s", buf);
669 * DPDK callback to set the primary MAC address.
672 * Pointer to Ethernet device structure.
674 * MAC address to register.
677 mvneta_mac_addr_set(struct rte_eth_dev *dev, struct rte_ether_addr *mac_addr)
679 struct mvneta_priv *priv = dev->data->dev_private;
685 ret = neta_ppio_set_mac_addr(priv->ppio, mac_addr->addr_bytes);
687 char buf[RTE_ETHER_ADDR_FMT_SIZE];
688 rte_ether_format_addr(buf, sizeof(buf), mac_addr);
689 MVNETA_LOG(ERR, "Failed to set mac to %s", buf);
695 * DPDK callback to get device statistics.
698 * Pointer to Ethernet device structure.
700 * Stats structure output buffer.
703 * 0 on success, negative error value otherwise.
706 mvneta_stats_get(struct rte_eth_dev *dev, struct rte_eth_stats *stats)
708 struct mvneta_priv *priv = dev->data->dev_private;
709 struct neta_ppio_statistics ppio_stats;
715 ret = neta_ppio_get_statistics(priv->ppio, &ppio_stats);
717 MVNETA_LOG(ERR, "Failed to update port statistics");
721 stats->ipackets += ppio_stats.rx_packets +
722 ppio_stats.rx_broadcast_packets +
723 ppio_stats.rx_multicast_packets -
724 priv->prev_stats.ipackets;
725 stats->opackets += ppio_stats.tx_packets +
726 ppio_stats.tx_broadcast_packets +
727 ppio_stats.tx_multicast_packets -
728 priv->prev_stats.opackets;
729 stats->ibytes += ppio_stats.rx_bytes - priv->prev_stats.ibytes;
730 stats->obytes += ppio_stats.tx_bytes - priv->prev_stats.obytes;
731 stats->imissed += ppio_stats.rx_discard +
732 ppio_stats.rx_overrun -
733 priv->prev_stats.imissed;
734 stats->ierrors = ppio_stats.rx_packets_err -
735 priv->prev_stats.ierrors;
736 stats->oerrors = ppio_stats.tx_errors - priv->prev_stats.oerrors;
742 * DPDK callback to clear device statistics.
745 * Pointer to Ethernet device structure.
748 * 0 on success, negative error value otherwise.
751 mvneta_stats_reset(struct rte_eth_dev *dev)
753 struct mvneta_priv *priv = dev->data->dev_private;
759 ret = mvneta_stats_get(dev, &priv->prev_stats);
761 MVNETA_LOG(ERR, "Failed to reset port statistics");
767 static const struct eth_dev_ops mvneta_ops = {
768 .dev_configure = mvneta_dev_configure,
769 .dev_start = mvneta_dev_start,
770 .dev_stop = mvneta_dev_stop,
771 .dev_set_link_up = mvneta_dev_set_link_up,
772 .dev_set_link_down = mvneta_dev_set_link_down,
773 .dev_close = mvneta_dev_close,
774 .link_update = mvneta_link_update,
775 .promiscuous_enable = mvneta_promiscuous_enable,
776 .promiscuous_disable = mvneta_promiscuous_disable,
777 .mac_addr_remove = mvneta_mac_addr_remove,
778 .mac_addr_add = mvneta_mac_addr_add,
779 .mac_addr_set = mvneta_mac_addr_set,
780 .mtu_set = mvneta_mtu_set,
781 .stats_get = mvneta_stats_get,
782 .stats_reset = mvneta_stats_reset,
783 .dev_infos_get = mvneta_dev_infos_get,
784 .dev_supported_ptypes_get = mvneta_dev_supported_ptypes_get,
785 .rxq_info_get = mvneta_rxq_info_get,
786 .txq_info_get = mvneta_txq_info_get,
787 .rx_queue_setup = mvneta_rx_queue_setup,
788 .rx_queue_release = mvneta_rx_queue_release,
789 .tx_queue_setup = mvneta_tx_queue_setup,
790 .tx_queue_release = mvneta_tx_queue_release,
794 * Create device representing Ethernet port.
797 * Pointer to the port's name.
800 * 0 on success, negative error value otherwise.
803 mvneta_eth_dev_create(struct rte_vdev_device *vdev, const char *name)
805 int ret, fd = socket(AF_INET, SOCK_DGRAM, 0);
806 struct rte_eth_dev *eth_dev;
807 struct mvneta_priv *priv;
810 eth_dev = rte_eth_dev_allocate(name);
814 priv = rte_zmalloc_socket(name, sizeof(*priv), 0, rte_socket_id());
819 eth_dev->data->dev_private = priv;
821 eth_dev->data->mac_addrs =
822 rte_zmalloc("mac_addrs",
823 RTE_ETHER_ADDR_LEN * MVNETA_MAC_ADDRS_MAX, 0);
824 if (!eth_dev->data->mac_addrs) {
825 MVNETA_LOG(ERR, "Failed to allocate space for eth addrs");
830 memset(&req, 0, sizeof(req));
831 strcpy(req.ifr_name, name);
832 ret = ioctl(fd, SIOCGIFHWADDR, &req);
836 memcpy(eth_dev->data->mac_addrs[0].addr_bytes,
837 req.ifr_addr.sa_data, RTE_ETHER_ADDR_LEN);
839 eth_dev->device = &vdev->device;
840 eth_dev->rx_pkt_burst = mvneta_rx_pkt_burst;
841 mvneta_set_tx_function(eth_dev);
842 eth_dev->dev_ops = &mvneta_ops;
843 eth_dev->data->dev_flags |= RTE_ETH_DEV_AUTOFILL_QUEUE_XSTATS;
845 rte_eth_dev_probing_finish(eth_dev);
848 rte_eth_dev_release_port(eth_dev);
854 * Cleanup previously created device representing Ethernet port.
857 * Pointer to the corresponding rte_eth_dev structure.
860 mvneta_eth_dev_destroy(struct rte_eth_dev *eth_dev)
862 rte_eth_dev_release_port(eth_dev);
866 * Cleanup previously created device representing Ethernet port.
869 * Pointer to the port name.
872 mvneta_eth_dev_destroy_name(const char *name)
874 struct rte_eth_dev *eth_dev;
876 eth_dev = rte_eth_dev_allocated(name);
880 mvneta_eth_dev_destroy(eth_dev);
884 * DPDK callback to register the virtual device.
887 * Pointer to the virtual device.
890 * 0 on success, negative error value otherwise.
893 rte_pmd_mvneta_probe(struct rte_vdev_device *vdev)
895 struct rte_kvargs *kvlist;
896 struct mvneta_ifnames ifnames;
901 params = rte_vdev_device_args(vdev);
905 kvlist = rte_kvargs_parse(params, valid_args);
909 ifnum = rte_kvargs_count(kvlist, MVNETA_IFACE_NAME_ARG);
910 if (ifnum > RTE_DIM(ifnames.names))
911 goto out_free_kvlist;
914 rte_kvargs_process(kvlist, MVNETA_IFACE_NAME_ARG,
915 mvneta_ifnames_get, &ifnames);
918 * The below system initialization should be done only once,
919 * on the first provided configuration file
924 MVNETA_LOG(INFO, "Perform MUSDK initializations");
926 ret = rte_mvep_init(MVEP_MOD_T_NETA, kvlist);
928 goto out_free_kvlist;
930 ret = mvneta_neta_init();
932 MVNETA_LOG(ERR, "Failed to init NETA!");
933 rte_mvep_deinit(MVEP_MOD_T_NETA);
934 goto out_free_kvlist;
938 for (i = 0; i < ifnum; i++) {
939 MVNETA_LOG(INFO, "Creating %s", ifnames.names[i]);
940 ret = mvneta_eth_dev_create(vdev, ifnames.names[i]);
947 rte_kvargs_free(kvlist);
951 rte_pmd_mvneta_remove(vdev);
954 rte_kvargs_free(kvlist);
960 * DPDK callback to remove virtual device.
963 * Pointer to the removed virtual device.
966 * 0 on success, negative error value otherwise.
969 rte_pmd_mvneta_remove(struct rte_vdev_device *vdev)
974 RTE_ETH_FOREACH_DEV(port_id) {
975 if (rte_eth_devices[port_id].device != &vdev->device)
977 ret |= rte_eth_dev_close(port_id);
980 return ret == 0 ? 0 : -EIO;
983 static struct rte_vdev_driver pmd_mvneta_drv = {
984 .probe = rte_pmd_mvneta_probe,
985 .remove = rte_pmd_mvneta_remove,
988 RTE_PMD_REGISTER_VDEV(net_mvneta, pmd_mvneta_drv);
989 RTE_PMD_REGISTER_PARAM_STRING(net_mvneta, "iface=<ifc>");
990 RTE_LOG_REGISTER_DEFAULT(mvneta_logtype, NOTICE);