1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2018 Marvell International Ltd.
3 * Copyright(c) 2018 Semihalf.
7 #include <rte_ethdev_driver.h>
8 #include <rte_kvargs.h>
9 #include <rte_bus_vdev.h>
13 #include <linux/ethtool.h>
14 #include <linux/sockios.h>
16 #include <net/if_arp.h>
17 #include <sys/ioctl.h>
18 #include <sys/socket.h>
20 #include <sys/types.h>
22 #include <rte_mvep_common.h>
24 #include "mvneta_rxtx.h"
27 #define MVNETA_IFACE_NAME_ARG "iface"
29 #define MVNETA_RX_OFFLOADS (DEV_RX_OFFLOAD_JUMBO_FRAME | \
30 DEV_RX_OFFLOAD_CHECKSUM)
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)
38 #define MVNETA_PKT_SIZE_MAX (16382 - MV_MH_SIZE) /* 9700B */
39 #define MVNETA_DEFAULT_MTU 1500
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
47 static const char * const valid_args[] = {
48 MVNETA_IFACE_NAME_ARG,
52 struct mvneta_ifnames {
53 const char *names[NETA_NUM_ETH_PPIO];
57 static int mvneta_dev_num;
60 * Deinitialize packet processor.
63 mvneta_neta_deinit(void)
69 * Initialize packet processor.
72 * 0 on success, negative error value otherwise.
75 mvneta_neta_init(void)
81 * Callback used by rte_kvargs_process() during argument parsing.
84 * Pointer to the parsed key (unused).
86 * Pointer to the parsed value.
88 * Pointer to the extra arguments which contains address of the
89 * table of pointers to parsed interface names.
95 mvneta_ifnames_get(const char *key __rte_unused, const char *value,
98 struct mvneta_ifnames *ifnames = extra_args;
100 ifnames->names[ifnames->idx++] = value;
106 * Ethernet device configuration.
108 * Prepare the driver for a given number of TX and RX queues and
109 * configure RSS if supported.
112 * Pointer to Ethernet device structure.
115 * 0 on success, negative error value otherwise.
118 mvneta_dev_configure(struct rte_eth_dev *dev)
120 struct mvneta_priv *priv = dev->data->dev_private;
121 struct neta_ppio_params *ppio_params;
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)
130 if (dev->data->dev_conf.rxmode.split_hdr_size) {
131 MVNETA_LOG(INFO, "Split headers not supported");
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;
139 if (dev->data->dev_conf.txmode.offloads & DEV_TX_OFFLOAD_MULTI_SEGS)
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;
153 * DPDK callback to get information about the device.
156 * Pointer to Ethernet device structure (unused).
158 * Info structure output buffer.
161 mvneta_dev_infos_get(struct rte_eth_dev *dev __rte_unused,
162 struct rte_eth_dev_info *info)
164 info->speed_capa = ETH_LINK_SPEED_10M |
165 ETH_LINK_SPEED_100M |
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;
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;
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;
181 info->rx_offload_capa = MVNETA_RX_OFFLOADS;
182 info->rx_queue_offload_capa = MVNETA_RX_OFFLOADS;
184 info->tx_offload_capa = MVNETA_TX_OFFLOADS;
185 info->tx_queue_offload_capa = MVNETA_TX_OFFLOADS;
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;
193 info->max_rx_pktlen = MVNETA_PKT_SIZE_MAX;
197 * Return supported packet types.
200 * Pointer to Ethernet device structure (unused).
203 * Const pointer to the table with supported packet types.
205 static const uint32_t *
206 mvneta_dev_supported_ptypes_get(struct rte_eth_dev *dev __rte_unused)
208 static const uint32_t ptypes[] = {
210 RTE_PTYPE_L2_ETHER_VLAN,
221 * DPDK callback to bring the link up.
224 * Pointer to Ethernet device structure.
227 * 0 on success, negative error value otherwise.
230 mvneta_dev_set_link_up(struct rte_eth_dev *dev)
232 struct mvneta_priv *priv = dev->data->dev_private;
237 return neta_ppio_enable(priv->ppio);
241 * DPDK callback to bring the link down.
244 * Pointer to Ethernet device structure.
247 * 0 on success, negative error value otherwise.
250 mvneta_dev_set_link_down(struct rte_eth_dev *dev)
252 struct mvneta_priv *priv = dev->data->dev_private;
257 return neta_ppio_disable(priv->ppio);
261 * DPDK callback to start the device.
264 * Pointer to Ethernet device structure.
267 * 0 on success, negative errno value on failure.
270 mvneta_dev_start(struct rte_eth_dev *dev)
272 struct mvneta_priv *priv = dev->data->dev_private;
273 char match[MVNETA_MATCH_LEN];
277 return mvneta_dev_set_link_up(dev);
279 snprintf(match, sizeof(match), "%s", dev->data->name);
280 priv->ppio_params.match = match;
281 priv->ppio_params.inqs_params.mtu = dev->data->mtu;
283 ret = neta_ppio_init(&priv->ppio_params, &priv->ppio);
285 MVNETA_LOG(ERR, "Failed to init ppio");
288 priv->ppio_id = priv->ppio->port_id;
291 * In case there are some some stale uc/mc mac addresses flush them
292 * here. It cannot be done during mvneta_dev_close() as port information
293 * is already gone at that point (due to neta_ppio_deinit() in
294 * mvneta_dev_stop()).
296 if (!priv->uc_mc_flushed) {
297 ret = neta_ppio_flush_mac_addrs(priv->ppio, 0, 1);
300 "Failed to flush uc/mc filter list");
303 priv->uc_mc_flushed = 1;
306 ret = mvneta_alloc_rx_bufs(dev);
310 ret = mvneta_dev_set_link_up(dev);
312 MVNETA_LOG(ERR, "Failed to set link up");
316 /* start tx queues */
317 for (i = 0; i < dev->data->nb_tx_queues; i++)
318 dev->data->tx_queue_state[i] = RTE_ETH_QUEUE_STATE_STARTED;
320 mvneta_set_tx_function(dev);
325 MVNETA_LOG(ERR, "Failed to start device");
326 neta_ppio_deinit(priv->ppio);
331 * DPDK callback to stop the device.
334 * Pointer to Ethernet device structure.
337 mvneta_dev_stop(struct rte_eth_dev *dev)
339 struct mvneta_priv *priv = dev->data->dev_private;
344 mvneta_dev_set_link_down(dev);
345 mvneta_flush_queues(dev);
346 neta_ppio_deinit(priv->ppio);
352 * DPDK callback to close the device.
355 * Pointer to Ethernet device structure.
358 mvneta_dev_close(struct rte_eth_dev *dev)
360 struct mvneta_priv *priv = dev->data->dev_private;
364 mvneta_dev_stop(dev);
366 for (i = 0; i < dev->data->nb_rx_queues; i++) {
367 mvneta_rx_queue_release(dev->data->rx_queues[i]);
368 dev->data->rx_queues[i] = NULL;
371 for (i = 0; i < dev->data->nb_tx_queues; i++) {
372 mvneta_tx_queue_release(dev->data->tx_queues[i]);
373 dev->data->tx_queues[i] = NULL;
378 * DPDK callback to set the primary MAC address.
381 * Pointer to Ethernet device structure.
383 * MAC address to register.
386 mvneta_mac_addr_set(struct rte_eth_dev *dev, struct ether_addr *mac_addr)
388 struct mvneta_priv *priv = dev->data->dev_private;
394 ret = neta_ppio_set_mac_addr(priv->ppio, mac_addr->addr_bytes);
396 char buf[ETHER_ADDR_FMT_SIZE];
397 ether_format_addr(buf, sizeof(buf), mac_addr);
398 MVNETA_LOG(ERR, "Failed to set mac to %s", buf);
403 static const struct eth_dev_ops mvneta_ops = {
404 .dev_configure = mvneta_dev_configure,
405 .dev_start = mvneta_dev_start,
406 .dev_stop = mvneta_dev_stop,
407 .dev_set_link_up = mvneta_dev_set_link_up,
408 .dev_set_link_down = mvneta_dev_set_link_down,
409 .dev_close = mvneta_dev_close,
410 .mac_addr_set = mvneta_mac_addr_set,
411 .dev_infos_get = mvneta_dev_infos_get,
412 .dev_supported_ptypes_get = mvneta_dev_supported_ptypes_get,
413 .rxq_info_get = mvneta_rxq_info_get,
414 .txq_info_get = mvneta_txq_info_get,
415 .rx_queue_setup = mvneta_rx_queue_setup,
416 .rx_queue_release = mvneta_rx_queue_release,
417 .tx_queue_setup = mvneta_tx_queue_setup,
418 .tx_queue_release = mvneta_tx_queue_release,
422 * Create device representing Ethernet port.
425 * Pointer to the port's name.
428 * 0 on success, negative error value otherwise.
431 mvneta_eth_dev_create(struct rte_vdev_device *vdev, const char *name)
433 int ret, fd = socket(AF_INET, SOCK_DGRAM, 0);
434 struct rte_eth_dev *eth_dev;
435 struct mvneta_priv *priv;
438 eth_dev = rte_eth_dev_allocate(name);
442 priv = rte_zmalloc_socket(name, sizeof(*priv), 0, rte_socket_id());
448 eth_dev->data->mac_addrs =
449 rte_zmalloc("mac_addrs",
450 ETHER_ADDR_LEN * MVNETA_MAC_ADDRS_MAX, 0);
451 if (!eth_dev->data->mac_addrs) {
452 MVNETA_LOG(ERR, "Failed to allocate space for eth addrs");
457 memset(&req, 0, sizeof(req));
458 strcpy(req.ifr_name, name);
459 ret = ioctl(fd, SIOCGIFHWADDR, &req);
463 memcpy(eth_dev->data->mac_addrs[0].addr_bytes,
464 req.ifr_addr.sa_data, ETHER_ADDR_LEN);
466 eth_dev->data->kdrv = RTE_KDRV_NONE;
467 eth_dev->data->dev_private = priv;
468 eth_dev->device = &vdev->device;
469 eth_dev->rx_pkt_burst = mvneta_rx_pkt_burst;
470 mvneta_set_tx_function(eth_dev);
471 eth_dev->dev_ops = &mvneta_ops;
473 rte_eth_dev_probing_finish(eth_dev);
476 rte_free(eth_dev->data->mac_addrs);
480 rte_eth_dev_release_port(eth_dev);
486 * Cleanup previously created device representing Ethernet port.
489 * Pointer to the corresponding rte_eth_dev structure.
492 mvneta_eth_dev_destroy(struct rte_eth_dev *eth_dev)
494 rte_free(eth_dev->data->dev_private);
495 rte_free(eth_dev->data->mac_addrs);
496 rte_eth_dev_release_port(eth_dev);
500 * Cleanup previously created device representing Ethernet port.
503 * Pointer to the port name.
506 mvneta_eth_dev_destroy_name(const char *name)
508 struct rte_eth_dev *eth_dev;
510 eth_dev = rte_eth_dev_allocated(name);
514 mvneta_eth_dev_destroy(eth_dev);
518 * DPDK callback to register the virtual device.
521 * Pointer to the virtual device.
524 * 0 on success, negative error value otherwise.
527 rte_pmd_mvneta_probe(struct rte_vdev_device *vdev)
529 struct rte_kvargs *kvlist;
530 struct mvneta_ifnames ifnames;
535 params = rte_vdev_device_args(vdev);
539 kvlist = rte_kvargs_parse(params, valid_args);
543 ifnum = rte_kvargs_count(kvlist, MVNETA_IFACE_NAME_ARG);
544 if (ifnum > RTE_DIM(ifnames.names))
545 goto out_free_kvlist;
548 rte_kvargs_process(kvlist, MVNETA_IFACE_NAME_ARG,
549 mvneta_ifnames_get, &ifnames);
552 * The below system initialization should be done only once,
553 * on the first provided configuration file
558 MVNETA_LOG(INFO, "Perform MUSDK initializations");
560 ret = rte_mvep_init(MVEP_MOD_T_NETA, kvlist);
562 goto out_free_kvlist;
564 ret = mvneta_neta_init();
566 MVNETA_LOG(ERR, "Failed to init NETA!");
567 rte_mvep_deinit(MVEP_MOD_T_NETA);
568 goto out_free_kvlist;
572 for (i = 0; i < ifnum; i++) {
573 MVNETA_LOG(INFO, "Creating %s", ifnames.names[i]);
574 ret = mvneta_eth_dev_create(vdev, ifnames.names[i]);
578 mvneta_dev_num += ifnum;
580 rte_kvargs_free(kvlist);
585 mvneta_eth_dev_destroy_name(ifnames.names[i]);
587 if (mvneta_dev_num == 0) {
588 mvneta_neta_deinit();
589 rte_mvep_deinit(MVEP_MOD_T_NETA);
592 rte_kvargs_free(kvlist);
598 * DPDK callback to remove virtual device.
601 * Pointer to the removed virtual device.
604 * 0 on success, negative error value otherwise.
607 rte_pmd_mvneta_remove(struct rte_vdev_device *vdev)
612 name = rte_vdev_device_name(vdev);
616 MVNETA_LOG(INFO, "Removing %s", name);
618 RTE_ETH_FOREACH_DEV(i) {
619 if (rte_eth_devices[i].device != &vdev->device)
622 mvneta_eth_dev_destroy(&rte_eth_devices[i]);
626 if (mvneta_dev_num == 0) {
627 MVNETA_LOG(INFO, "Perform MUSDK deinit");
628 mvneta_neta_deinit();
629 rte_mvep_deinit(MVEP_MOD_T_NETA);
635 static struct rte_vdev_driver pmd_mvneta_drv = {
636 .probe = rte_pmd_mvneta_probe,
637 .remove = rte_pmd_mvneta_remove,
640 RTE_PMD_REGISTER_VDEV(net_mvneta, pmd_mvneta_drv);
641 RTE_PMD_REGISTER_PARAM_STRING(net_mvneta, "iface=<ifc>");
643 RTE_INIT(mvneta_init_log)
645 mvneta_logtype = rte_log_register("pmd.net.mvneta");
646 if (mvneta_logtype >= 0)
647 rte_log_set_level(mvneta_logtype, RTE_LOG_NOTICE);