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>
10 #include <rte_malloc.h>
11 #include <rte_bus_vdev.h>
15 #include <linux/ethtool.h>
16 #include <linux/sockios.h>
18 #include <net/if_arp.h>
19 #include <sys/ioctl.h>
20 #include <sys/socket.h>
22 #include <sys/types.h>
24 #include <rte_mvep_common.h>
26 #include "mvneta_ethdev.h"
29 #define MVNETA_IFACE_NAME_ARG "iface"
31 #define MVNETA_RX_OFFLOADS (DEV_RX_OFFLOAD_JUMBO_FRAME | \
32 DEV_RX_OFFLOAD_CHECKSUM)
34 /** Port Tx offloads capabilities */
35 #define MVNETA_TX_OFFLOADS (DEV_TX_OFFLOAD_IPV4_CKSUM | \
36 DEV_TX_OFFLOAD_UDP_CKSUM | \
37 DEV_TX_OFFLOAD_TCP_CKSUM | \
38 DEV_TX_OFFLOAD_MULTI_SEGS)
40 #define MVNETA_PKT_SIZE_MAX (16382 - MV_MH_SIZE) /* 9700B */
41 #define MVNETA_DEFAULT_MTU 1500
43 #define MVNETA_MAC_ADDRS_MAX 256 /*16 UC, 256 IP, 256 MC/BC */
44 /** Maximum length of a match string */
45 #define MVNETA_MATCH_LEN 16
49 static const char * const valid_args[] = {
50 MVNETA_IFACE_NAME_ARG,
54 struct mvneta_ifnames {
55 const char *names[NETA_NUM_ETH_PPIO];
59 static int mvneta_dev_num;
62 * Deinitialize packet processor.
65 mvneta_neta_deinit(void)
71 * Initialize packet processor.
74 * 0 on success, negative error value otherwise.
77 mvneta_neta_init(void)
83 * Callback used by rte_kvargs_process() during argument parsing.
86 * Pointer to the parsed key (unused).
88 * Pointer to the parsed value.
90 * Pointer to the extra arguments which contains address of the
91 * table of pointers to parsed interface names.
97 mvneta_ifnames_get(const char *key __rte_unused, const char *value,
100 struct mvneta_ifnames *ifnames = extra_args;
102 ifnames->names[ifnames->idx++] = value;
108 * Ethernet device configuration.
110 * Prepare the driver for a given number of TX and RX queues and
111 * configure RSS if supported.
114 * Pointer to Ethernet device structure.
117 * 0 on success, negative error value otherwise.
120 mvneta_dev_configure(struct rte_eth_dev *dev)
122 struct mvneta_priv *priv = dev->data->dev_private;
123 struct neta_ppio_params *ppio_params;
125 if (dev->data->dev_conf.rxmode.mq_mode != ETH_MQ_RX_NONE) {
126 MVNETA_LOG(INFO, "Unsupported RSS and rx multi queue mode %d",
127 dev->data->dev_conf.rxmode.mq_mode);
128 if (dev->data->nb_rx_queues > 1)
132 if (dev->data->dev_conf.rxmode.split_hdr_size) {
133 MVNETA_LOG(INFO, "Split headers not supported");
137 if (dev->data->dev_conf.rxmode.offloads & DEV_RX_OFFLOAD_JUMBO_FRAME)
138 dev->data->mtu = dev->data->dev_conf.rxmode.max_rx_pkt_len -
139 MRVL_NETA_ETH_HDRS_LEN;
141 if (dev->data->dev_conf.txmode.offloads & DEV_TX_OFFLOAD_MULTI_SEGS)
144 ppio_params = &priv->ppio_params;
145 ppio_params->outqs_params.num_outqs = dev->data->nb_tx_queues;
146 /* Default: 1 TC, no QoS supported. */
147 ppio_params->inqs_params.num_tcs = 1;
148 ppio_params->inqs_params.tcs_params[0].pkt_offset = MRVL_NETA_PKT_OFFS;
149 priv->ppio_id = dev->data->port_id;
155 * DPDK callback to get information about the device.
158 * Pointer to Ethernet device structure (unused).
160 * Info structure output buffer.
163 mvneta_dev_infos_get(struct rte_eth_dev *dev __rte_unused,
164 struct rte_eth_dev_info *info)
166 info->speed_capa = ETH_LINK_SPEED_10M |
167 ETH_LINK_SPEED_100M |
171 info->max_rx_queues = MRVL_NETA_RXQ_MAX;
172 info->max_tx_queues = MRVL_NETA_TXQ_MAX;
173 info->max_mac_addrs = MVNETA_MAC_ADDRS_MAX;
175 info->rx_desc_lim.nb_max = MRVL_NETA_RXD_MAX;
176 info->rx_desc_lim.nb_min = MRVL_NETA_RXD_MIN;
177 info->rx_desc_lim.nb_align = MRVL_NETA_RXD_ALIGN;
179 info->tx_desc_lim.nb_max = MRVL_NETA_TXD_MAX;
180 info->tx_desc_lim.nb_min = MRVL_NETA_TXD_MIN;
181 info->tx_desc_lim.nb_align = MRVL_NETA_TXD_ALIGN;
183 info->rx_offload_capa = MVNETA_RX_OFFLOADS;
184 info->rx_queue_offload_capa = MVNETA_RX_OFFLOADS;
186 info->tx_offload_capa = MVNETA_TX_OFFLOADS;
187 info->tx_queue_offload_capa = MVNETA_TX_OFFLOADS;
189 /* By default packets are dropped if no descriptors are available */
190 info->default_rxconf.rx_drop_en = 1;
191 /* Deferred tx queue start is not supported */
192 info->default_txconf.tx_deferred_start = 0;
193 info->default_txconf.offloads = 0;
195 info->max_rx_pktlen = MVNETA_PKT_SIZE_MAX;
199 * Return supported packet types.
202 * Pointer to Ethernet device structure (unused).
205 * Const pointer to the table with supported packet types.
207 static const uint32_t *
208 mvneta_dev_supported_ptypes_get(struct rte_eth_dev *dev __rte_unused)
210 static const uint32_t ptypes[] = {
212 RTE_PTYPE_L2_ETHER_VLAN,
223 * DPDK callback to bring the link up.
226 * Pointer to Ethernet device structure.
229 * 0 on success, negative error value otherwise.
232 mvneta_dev_set_link_up(struct rte_eth_dev *dev)
234 struct mvneta_priv *priv = dev->data->dev_private;
239 return neta_ppio_enable(priv->ppio);
243 * DPDK callback to bring the link down.
246 * Pointer to Ethernet device structure.
249 * 0 on success, negative error value otherwise.
252 mvneta_dev_set_link_down(struct rte_eth_dev *dev)
254 struct mvneta_priv *priv = dev->data->dev_private;
259 return neta_ppio_disable(priv->ppio);
263 * DPDK callback to start the device.
266 * Pointer to Ethernet device structure.
269 * 0 on success, negative errno value on failure.
272 mvneta_dev_start(struct rte_eth_dev *dev)
274 struct mvneta_priv *priv = dev->data->dev_private;
275 char match[MVNETA_MATCH_LEN];
279 return mvneta_dev_set_link_up(dev);
281 snprintf(match, sizeof(match), "%s", dev->data->name);
282 priv->ppio_params.match = match;
283 priv->ppio_params.inqs_params.mtu = dev->data->mtu;
285 ret = neta_ppio_init(&priv->ppio_params, &priv->ppio);
287 MVNETA_LOG(ERR, "Failed to init ppio");
290 priv->ppio_id = priv->ppio->port_id;
293 * In case there are some some stale uc/mc mac addresses flush them
294 * here. It cannot be done during mvneta_dev_close() as port information
295 * is already gone at that point (due to neta_ppio_deinit() in
296 * mvneta_dev_stop()).
298 if (!priv->uc_mc_flushed) {
299 ret = neta_ppio_flush_mac_addrs(priv->ppio, 0, 1);
302 "Failed to flush uc/mc filter list");
305 priv->uc_mc_flushed = 1;
308 ret = mvneta_dev_set_link_up(dev);
310 MVNETA_LOG(ERR, "Failed to set link up");
314 /* start tx queues */
315 for (i = 0; i < dev->data->nb_tx_queues; i++)
316 dev->data->tx_queue_state[i] = RTE_ETH_QUEUE_STATE_STARTED;
321 MVNETA_LOG(ERR, "Failed to start device");
322 neta_ppio_deinit(priv->ppio);
327 * DPDK callback to stop the device.
330 * Pointer to Ethernet device structure.
333 mvneta_dev_stop(struct rte_eth_dev *dev)
335 struct mvneta_priv *priv = dev->data->dev_private;
340 mvneta_dev_set_link_down(dev);
342 neta_ppio_deinit(priv->ppio);
348 * DPDK callback to close the device.
351 * Pointer to Ethernet device structure.
354 mvneta_dev_close(struct rte_eth_dev *dev)
356 struct mvneta_priv *priv = dev->data->dev_private;
359 mvneta_dev_stop(dev);
363 * DPDK callback to set the primary MAC address.
366 * Pointer to Ethernet device structure.
368 * MAC address to register.
371 mvneta_mac_addr_set(struct rte_eth_dev *dev, struct ether_addr *mac_addr)
373 struct mvneta_priv *priv = dev->data->dev_private;
379 ret = neta_ppio_set_mac_addr(priv->ppio, mac_addr->addr_bytes);
381 char buf[ETHER_ADDR_FMT_SIZE];
382 ether_format_addr(buf, sizeof(buf), mac_addr);
383 MVNETA_LOG(ERR, "Failed to set mac to %s", buf);
388 static const struct eth_dev_ops mvneta_ops = {
389 .dev_configure = mvneta_dev_configure,
390 .dev_start = mvneta_dev_start,
391 .dev_stop = mvneta_dev_stop,
392 .dev_set_link_up = mvneta_dev_set_link_up,
393 .dev_set_link_down = mvneta_dev_set_link_down,
394 .dev_close = mvneta_dev_close,
395 .mac_addr_set = mvneta_mac_addr_set,
396 .dev_infos_get = mvneta_dev_infos_get,
397 .dev_supported_ptypes_get = mvneta_dev_supported_ptypes_get,
401 * Create device representing Ethernet port.
404 * Pointer to the port's name.
407 * 0 on success, negative error value otherwise.
410 mvneta_eth_dev_create(struct rte_vdev_device *vdev, const char *name)
412 int ret, fd = socket(AF_INET, SOCK_DGRAM, 0);
413 struct rte_eth_dev *eth_dev;
414 struct mvneta_priv *priv;
417 eth_dev = rte_eth_dev_allocate(name);
421 priv = rte_zmalloc_socket(name, sizeof(*priv), 0, rte_socket_id());
427 eth_dev->data->mac_addrs =
428 rte_zmalloc("mac_addrs",
429 ETHER_ADDR_LEN * MVNETA_MAC_ADDRS_MAX, 0);
430 if (!eth_dev->data->mac_addrs) {
431 MVNETA_LOG(ERR, "Failed to allocate space for eth addrs");
436 memset(&req, 0, sizeof(req));
437 strcpy(req.ifr_name, name);
438 ret = ioctl(fd, SIOCGIFHWADDR, &req);
442 memcpy(eth_dev->data->mac_addrs[0].addr_bytes,
443 req.ifr_addr.sa_data, ETHER_ADDR_LEN);
445 eth_dev->data->kdrv = RTE_KDRV_NONE;
446 eth_dev->data->dev_private = priv;
447 eth_dev->device = &vdev->device;
448 eth_dev->dev_ops = &mvneta_ops;
450 rte_eth_dev_probing_finish(eth_dev);
453 rte_free(eth_dev->data->mac_addrs);
457 rte_eth_dev_release_port(eth_dev);
463 * Cleanup previously created device representing Ethernet port.
466 * Pointer to the corresponding rte_eth_dev structure.
469 mvneta_eth_dev_destroy(struct rte_eth_dev *eth_dev)
471 rte_free(eth_dev->data->dev_private);
472 rte_free(eth_dev->data->mac_addrs);
473 rte_eth_dev_release_port(eth_dev);
477 * Cleanup previously created device representing Ethernet port.
480 * Pointer to the port name.
483 mvneta_eth_dev_destroy_name(const char *name)
485 struct rte_eth_dev *eth_dev;
487 eth_dev = rte_eth_dev_allocated(name);
491 mvneta_eth_dev_destroy(eth_dev);
495 * DPDK callback to register the virtual device.
498 * Pointer to the virtual device.
501 * 0 on success, negative error value otherwise.
504 rte_pmd_mvneta_probe(struct rte_vdev_device *vdev)
506 struct rte_kvargs *kvlist;
507 struct mvneta_ifnames ifnames;
512 params = rte_vdev_device_args(vdev);
516 kvlist = rte_kvargs_parse(params, valid_args);
520 ifnum = rte_kvargs_count(kvlist, MVNETA_IFACE_NAME_ARG);
521 if (ifnum > RTE_DIM(ifnames.names))
522 goto out_free_kvlist;
525 rte_kvargs_process(kvlist, MVNETA_IFACE_NAME_ARG,
526 mvneta_ifnames_get, &ifnames);
529 * The below system initialization should be done only once,
530 * on the first provided configuration file
535 MVNETA_LOG(INFO, "Perform MUSDK initializations");
537 ret = rte_mvep_init(MVEP_MOD_T_NETA, kvlist);
539 goto out_free_kvlist;
541 ret = mvneta_neta_init();
543 MVNETA_LOG(ERR, "Failed to init NETA!");
544 rte_mvep_deinit(MVEP_MOD_T_NETA);
545 goto out_free_kvlist;
549 for (i = 0; i < ifnum; i++) {
550 MVNETA_LOG(INFO, "Creating %s", ifnames.names[i]);
551 ret = mvneta_eth_dev_create(vdev, ifnames.names[i]);
555 mvneta_dev_num += ifnum;
557 rte_kvargs_free(kvlist);
562 mvneta_eth_dev_destroy_name(ifnames.names[i]);
564 if (mvneta_dev_num == 0) {
565 mvneta_neta_deinit();
566 rte_mvep_deinit(MVEP_MOD_T_NETA);
569 rte_kvargs_free(kvlist);
575 * DPDK callback to remove virtual device.
578 * Pointer to the removed virtual device.
581 * 0 on success, negative error value otherwise.
584 rte_pmd_mvneta_remove(struct rte_vdev_device *vdev)
589 name = rte_vdev_device_name(vdev);
593 MVNETA_LOG(INFO, "Removing %s", name);
595 RTE_ETH_FOREACH_DEV(i) {
596 if (rte_eth_devices[i].device != &vdev->device)
599 mvneta_eth_dev_destroy(&rte_eth_devices[i]);
603 if (mvneta_dev_num == 0) {
604 MVNETA_LOG(INFO, "Perform MUSDK deinit");
605 mvneta_neta_deinit();
606 rte_mvep_deinit(MVEP_MOD_T_NETA);
612 static struct rte_vdev_driver pmd_mvneta_drv = {
613 .probe = rte_pmd_mvneta_probe,
614 .remove = rte_pmd_mvneta_remove,
617 RTE_PMD_REGISTER_VDEV(net_mvneta, pmd_mvneta_drv);
618 RTE_PMD_REGISTER_PARAM_STRING(net_mvneta, "iface=<ifc>");
620 RTE_INIT(mvneta_init_log)
622 mvneta_logtype = rte_log_register("pmd.net.mvneta");
623 if (mvneta_logtype >= 0)
624 rte_log_set_level(mvneta_logtype, RTE_LOG_NOTICE);