1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2019 Cesnet
3 * Copyright(c) 2019 Netcope Technologies, a.s. <info@netcope.com>
9 #include <netcope/rxmac.h>
10 #include <netcope/txmac.h>
12 #include <rte_ethdev_pci.h>
13 #include <rte_kvargs.h>
15 #include "nfb_stats.h"
18 #include "nfb_rxmode.h"
24 static const struct rte_ether_addr eth_addr = {
25 .addr_bytes = { 0x00, 0x11, 0x17, 0x00, 0x00, 0x00 }
29 * Open all RX DMA queues
32 * Pointer to nfb device.
34 * Pointer to output array of nc_rxmac
35 * @param[out] max_rxmac
36 * Pointer to output max index of rxmac
39 nfb_nc_rxmac_init(struct nfb_device *nfb,
40 struct nc_rxmac *rxmac[RTE_MAX_NC_RXMAC],
44 while ((rxmac[*max_rxmac] = nc_rxmac_open_index(nfb, *max_rxmac)))
49 * Open all TX DMA queues
52 * Pointer to nfb device.
54 * Pointer to output array of nc_txmac
55 * @param[out] max_rxmac
56 * Pointer to output max index of txmac
59 nfb_nc_txmac_init(struct nfb_device *nfb,
60 struct nc_txmac *txmac[RTE_MAX_NC_TXMAC],
64 while ((txmac[*max_txmac] = nc_txmac_open_index(nfb, *max_txmac)))
69 * Close all RX DMA queues
72 * Pointer to array of nc_rxmac
74 * Maximum index of rxmac
77 nfb_nc_rxmac_deinit(struct nc_rxmac *rxmac[RTE_MAX_NC_RXMAC],
80 for (; max_rxmac > 0; --max_rxmac) {
81 nc_rxmac_close(rxmac[max_rxmac]);
82 rxmac[max_rxmac] = NULL;
87 * Close all TX DMA queues
90 * Pointer to array of nc_txmac
92 * Maximum index of txmac
95 nfb_nc_txmac_deinit(struct nc_txmac *txmac[RTE_MAX_NC_TXMAC],
98 for (; max_txmac > 0; --max_txmac) {
99 nc_txmac_close(txmac[max_txmac]);
100 txmac[max_txmac] = NULL;
105 * DPDK callback to start the device.
107 * Start device by starting all configured queues.
110 * Pointer to Ethernet device structure.
113 * 0 on success, a negative errno value otherwise.
116 nfb_eth_dev_start(struct rte_eth_dev *dev)
120 uint16_t nb_rx = dev->data->nb_rx_queues;
121 uint16_t nb_tx = dev->data->nb_tx_queues;
123 for (i = 0; i < nb_rx; i++) {
124 ret = nfb_eth_rx_queue_start(dev, i);
129 for (i = 0; i < nb_tx; i++) {
130 ret = nfb_eth_tx_queue_start(dev, i);
138 for (i = 0; i < nb_tx; i++)
139 nfb_eth_tx_queue_stop(dev, i);
141 for (i = 0; i < nb_rx; i++)
142 nfb_eth_rx_queue_stop(dev, i);
147 * DPDK callback to stop the device.
149 * Stop device by stopping all configured queues.
152 * Pointer to Ethernet device structure.
155 nfb_eth_dev_stop(struct rte_eth_dev *dev)
158 uint16_t nb_rx = dev->data->nb_rx_queues;
159 uint16_t nb_tx = dev->data->nb_tx_queues;
161 dev->data->dev_started = 0;
163 for (i = 0; i < nb_tx; i++)
164 nfb_eth_tx_queue_stop(dev, i);
166 for (i = 0; i < nb_rx; i++)
167 nfb_eth_rx_queue_stop(dev, i);
171 * DPDK callback for Ethernet device configuration.
174 * Pointer to Ethernet device structure.
177 * 0 on success, a negative errno value otherwise.
180 nfb_eth_dev_configure(struct rte_eth_dev *dev __rte_unused)
186 * DPDK callback to get information about the device.
189 * Pointer to Ethernet device structure.
191 * Info structure output buffer.
194 nfb_eth_dev_info(struct rte_eth_dev *dev,
195 struct rte_eth_dev_info *dev_info)
197 dev_info->max_mac_addrs = 1;
198 dev_info->max_rx_pktlen = (uint32_t)-1;
199 dev_info->max_rx_queues = dev->data->nb_rx_queues;
200 dev_info->max_tx_queues = dev->data->nb_tx_queues;
201 dev_info->speed_capa = ETH_LINK_SPEED_100G;
207 * DPDK callback to close the device.
209 * Destroy all queues and objects, free memory.
212 * Pointer to Ethernet device structure.
215 nfb_eth_dev_close(struct rte_eth_dev *dev)
217 struct pmd_internals *internals = dev->data->dev_private;
219 uint16_t nb_rx = dev->data->nb_rx_queues;
220 uint16_t nb_tx = dev->data->nb_tx_queues;
222 if (rte_eal_process_type() != RTE_PROC_PRIMARY)
225 nfb_eth_dev_stop(dev);
227 nfb_nc_rxmac_deinit(internals->rxmac, internals->max_rxmac);
228 nfb_nc_txmac_deinit(internals->txmac, internals->max_txmac);
230 for (i = 0; i < nb_rx; i++) {
231 nfb_eth_rx_queue_release(dev->data->rx_queues[i]);
232 dev->data->rx_queues[i] = NULL;
234 dev->data->nb_rx_queues = 0;
235 for (i = 0; i < nb_tx; i++) {
236 nfb_eth_tx_queue_release(dev->data->tx_queues[i]);
237 dev->data->tx_queues[i] = NULL;
239 dev->data->nb_tx_queues = 0;
245 * DPDK callback to retrieve physical link information.
248 * Pointer to Ethernet device structure.
250 * Storage for current link status.
253 * 0 on success, a negative errno value otherwise.
256 nfb_eth_link_update(struct rte_eth_dev *dev,
257 int wait_to_complete __rte_unused)
260 struct nc_rxmac_status status;
261 struct rte_eth_link link;
262 memset(&link, 0, sizeof(link));
264 struct pmd_internals *internals = dev->data->dev_private;
266 status.speed = MAC_SPEED_UNKNOWN;
268 link.link_speed = ETH_SPEED_NUM_NONE;
269 link.link_status = ETH_LINK_DOWN;
270 link.link_duplex = ETH_LINK_FULL_DUPLEX;
271 link.link_autoneg = ETH_LINK_SPEED_FIXED;
273 if (internals->rxmac[0] != NULL) {
274 nc_rxmac_read_status(internals->rxmac[0], &status);
276 switch (status.speed) {
278 link.link_speed = ETH_SPEED_NUM_10G;
281 link.link_speed = ETH_SPEED_NUM_40G;
284 link.link_speed = ETH_SPEED_NUM_100G;
287 link.link_speed = ETH_SPEED_NUM_NONE;
292 for (i = 0; i < internals->max_rxmac; ++i) {
293 nc_rxmac_read_status(internals->rxmac[i], &status);
295 if (status.enabled && status.link_up) {
296 link.link_status = ETH_LINK_UP;
301 rte_eth_linkstatus_set(dev, &link);
307 * DPDK callback to bring the link UP.
310 * Pointer to Ethernet device structure.
313 * 0 on success, a negative errno value otherwise.
316 nfb_eth_dev_set_link_up(struct rte_eth_dev *dev)
318 struct pmd_internals *internals = (struct pmd_internals *)
319 dev->data->dev_private;
322 for (i = 0; i < internals->max_rxmac; ++i)
323 nc_rxmac_enable(internals->rxmac[i]);
325 for (i = 0; i < internals->max_txmac; ++i)
326 nc_txmac_enable(internals->txmac[i]);
332 * DPDK callback to bring the link DOWN.
335 * Pointer to Ethernet device structure.
338 * 0 on success, a negative errno value otherwise.
341 nfb_eth_dev_set_link_down(struct rte_eth_dev *dev)
343 struct pmd_internals *internals = (struct pmd_internals *)
344 dev->data->dev_private;
347 for (i = 0; i < internals->max_rxmac; ++i)
348 nc_rxmac_disable(internals->rxmac[i]);
350 for (i = 0; i < internals->max_txmac; ++i)
351 nc_txmac_disable(internals->txmac[i]);
357 * DPDK callback to set primary MAC address.
360 * Pointer to Ethernet device structure.
362 * MAC address to register.
365 * 0 on success, a negative errno value otherwise.
368 nfb_eth_mac_addr_set(struct rte_eth_dev *dev,
369 struct rte_ether_addr *mac_addr)
373 struct rte_eth_dev_data *data = dev->data;
374 struct pmd_internals *internals = (struct pmd_internals *)
377 if (!rte_is_valid_assigned_ether_addr(mac_addr))
380 for (i = 0; i < RTE_ETHER_ADDR_LEN; i++) {
382 mac |= mac_addr->addr_bytes[i] & 0xFF;
385 for (i = 0; i < internals->max_rxmac; ++i)
386 nc_rxmac_set_mac(internals->rxmac[i], 0, mac, 1);
388 rte_ether_addr_copy(mac_addr, data->mac_addrs);
392 static const struct eth_dev_ops ops = {
393 .dev_start = nfb_eth_dev_start,
394 .dev_stop = nfb_eth_dev_stop,
395 .dev_set_link_up = nfb_eth_dev_set_link_up,
396 .dev_set_link_down = nfb_eth_dev_set_link_down,
397 .dev_close = nfb_eth_dev_close,
398 .dev_configure = nfb_eth_dev_configure,
399 .dev_infos_get = nfb_eth_dev_info,
400 .promiscuous_enable = nfb_eth_promiscuous_enable,
401 .promiscuous_disable = nfb_eth_promiscuous_disable,
402 .allmulticast_enable = nfb_eth_allmulticast_enable,
403 .allmulticast_disable = nfb_eth_allmulticast_disable,
404 .rx_queue_start = nfb_eth_rx_queue_start,
405 .rx_queue_stop = nfb_eth_rx_queue_stop,
406 .tx_queue_start = nfb_eth_tx_queue_start,
407 .tx_queue_stop = nfb_eth_tx_queue_stop,
408 .rx_queue_setup = nfb_eth_rx_queue_setup,
409 .tx_queue_setup = nfb_eth_tx_queue_setup,
410 .rx_queue_release = nfb_eth_rx_queue_release,
411 .tx_queue_release = nfb_eth_tx_queue_release,
412 .link_update = nfb_eth_link_update,
413 .stats_get = nfb_eth_stats_get,
414 .stats_reset = nfb_eth_stats_reset,
415 .mac_addr_set = nfb_eth_mac_addr_set,
419 * DPDK callback to initialize an ethernet device
422 * Pointer to ethernet device structure
425 * 0 on success, a negative errno value otherwise.
428 nfb_eth_dev_init(struct rte_eth_dev *dev)
430 struct rte_eth_dev_data *data = dev->data;
431 struct pmd_internals *internals = (struct pmd_internals *)
433 struct rte_pci_device *pci_dev = RTE_ETH_DEV_TO_PCI(dev);
434 struct rte_pci_addr *pci_addr = &pci_dev->addr;
435 struct rte_ether_addr eth_addr_init;
436 struct rte_kvargs *kvlist;
438 RTE_LOG(INFO, PMD, "Initializing NFB device (" PCI_PRI_FMT ")\n",
439 pci_addr->domain, pci_addr->bus, pci_addr->devid,
442 snprintf(internals->nfb_dev, PATH_MAX,
443 "/dev/nfb/by-pci-slot/" PCI_PRI_FMT,
444 pci_addr->domain, pci_addr->bus, pci_addr->devid,
447 /* Check validity of device args */
448 if (dev->device->devargs != NULL &&
449 dev->device->devargs->args != NULL &&
450 strlen(dev->device->devargs->args) > 0) {
451 kvlist = rte_kvargs_parse(dev->device->devargs->args,
453 if (kvlist == NULL) {
454 RTE_LOG(ERR, PMD, "Failed to parse device arguments %s",
455 dev->device->devargs->args);
456 rte_kvargs_free(kvlist);
459 rte_kvargs_free(kvlist);
463 * Get number of available DMA RX and TX queues, which is maximum
464 * number of queues that can be created and store it in private device
467 internals->nfb = nfb_open(internals->nfb_dev);
468 if (internals->nfb == NULL) {
469 RTE_LOG(ERR, PMD, "nfb_open(): failed to open %s",
473 data->nb_rx_queues = ndp_get_rx_queue_available_count(internals->nfb);
474 data->nb_tx_queues = ndp_get_tx_queue_available_count(internals->nfb);
476 RTE_LOG(INFO, PMD, "Available NDP queues RX: %u TX: %u\n",
477 data->nb_rx_queues, data->nb_tx_queues);
479 nfb_nc_rxmac_init(internals->nfb,
481 &internals->max_rxmac);
482 nfb_nc_txmac_init(internals->nfb,
484 &internals->max_txmac);
486 /* Set rx, tx burst functions */
487 dev->rx_pkt_burst = nfb_eth_ndp_rx;
488 dev->tx_pkt_burst = nfb_eth_ndp_tx;
490 /* Set function callbacks for Ethernet API */
494 nfb_eth_link_update(dev, 0);
496 /* Allocate space for one mac address */
497 data->mac_addrs = rte_zmalloc(data->name, sizeof(struct rte_ether_addr),
498 RTE_CACHE_LINE_SIZE);
499 if (data->mac_addrs == NULL) {
500 RTE_LOG(ERR, PMD, "Could not alloc space for MAC address!\n");
501 nfb_close(internals->nfb);
505 rte_eth_random_addr(eth_addr_init.addr_bytes);
506 eth_addr_init.addr_bytes[0] = eth_addr.addr_bytes[0];
507 eth_addr_init.addr_bytes[1] = eth_addr.addr_bytes[1];
508 eth_addr_init.addr_bytes[2] = eth_addr.addr_bytes[2];
510 nfb_eth_mac_addr_set(dev, ð_addr_init);
512 data->promiscuous = nfb_eth_promiscuous_get(dev);
513 data->all_multicast = nfb_eth_allmulticast_get(dev);
514 internals->rx_filter_original = data->promiscuous;
516 RTE_LOG(INFO, PMD, "NFB device ("
517 PCI_PRI_FMT ") successfully initialized\n",
518 pci_addr->domain, pci_addr->bus, pci_addr->devid,
525 * DPDK callback to uninitialize an ethernet device
528 * Pointer to ethernet device structure
531 * 0 on success, a negative errno value otherwise.
534 nfb_eth_dev_uninit(struct rte_eth_dev *dev)
536 struct rte_pci_device *pci_dev = RTE_ETH_DEV_TO_PCI(dev);
537 struct rte_pci_addr *pci_addr = &pci_dev->addr;
539 nfb_eth_dev_close(dev);
541 RTE_LOG(INFO, PMD, "NFB device ("
542 PCI_PRI_FMT ") successfully uninitialized\n",
543 pci_addr->domain, pci_addr->bus, pci_addr->devid,
549 static const struct rte_pci_id nfb_pci_id_table[] = {
550 { RTE_PCI_DEVICE(PCI_VENDOR_ID_NETCOPE, PCI_DEVICE_ID_NFB_40G2) },
551 { RTE_PCI_DEVICE(PCI_VENDOR_ID_NETCOPE, PCI_DEVICE_ID_NFB_100G2) },
552 { RTE_PCI_DEVICE(PCI_VENDOR_ID_NETCOPE, PCI_DEVICE_ID_NFB_200G2QL) },
553 { RTE_PCI_DEVICE(PCI_VENDOR_ID_SILICOM, PCI_DEVICE_ID_FB2CGG3) },
554 { RTE_PCI_DEVICE(PCI_VENDOR_ID_SILICOM, PCI_DEVICE_ID_FB2CGG3D) },
559 * DPDK callback to register a PCI device.
561 * This function spawns Ethernet devices out of a given PCI device.
564 * PCI driver structure (nfb_driver).
566 * PCI device information.
569 * 0 on success, a negative errno value otherwise.
572 nfb_eth_pci_probe(struct rte_pci_driver *pci_drv __rte_unused,
573 struct rte_pci_device *pci_dev)
575 return rte_eth_dev_pci_generic_probe(pci_dev,
576 sizeof(struct pmd_internals), nfb_eth_dev_init);
580 * DPDK callback to remove a PCI device.
582 * This function removes all Ethernet devices belong to a given PCI device.
585 * Pointer to the PCI device.
588 * 0 on success, the function cannot fail.
591 nfb_eth_pci_remove(struct rte_pci_device *pci_dev)
593 return rte_eth_dev_pci_generic_remove(pci_dev, nfb_eth_dev_uninit);
596 static struct rte_pci_driver nfb_eth_driver = {
597 .id_table = nfb_pci_id_table,
598 .probe = nfb_eth_pci_probe,
599 .remove = nfb_eth_pci_remove,
602 RTE_PMD_REGISTER_PCI(RTE_NFB_DRIVER_NAME, nfb_eth_driver);
603 RTE_PMD_REGISTER_PCI_TABLE(RTE_NFB_DRIVER_NAME, nfb_pci_id_table);
604 RTE_PMD_REGISTER_KMOD_DEP(RTE_NFB_DRIVER_NAME, "* nfb");
605 RTE_PMD_REGISTER_PARAM_STRING(RTE_NFB_DRIVER_NAME, TIMESTAMP_ARG "=<0|1>");