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 <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],
81 for (i = 0; i < max_rxmac; i++) {
82 nc_rxmac_close(rxmac[i]);
88 * Close all TX DMA queues
91 * Pointer to array of nc_txmac
93 * Maximum index of txmac
96 nfb_nc_txmac_deinit(struct nc_txmac *txmac[RTE_MAX_NC_TXMAC],
100 for (i = 0; i < max_txmac; i++) {
101 nc_txmac_close(txmac[i]);
107 * DPDK callback to start the device.
109 * Start device by starting all configured queues.
112 * Pointer to Ethernet device structure.
115 * 0 on success, a negative errno value otherwise.
118 nfb_eth_dev_start(struct rte_eth_dev *dev)
122 uint16_t nb_rx = dev->data->nb_rx_queues;
123 uint16_t nb_tx = dev->data->nb_tx_queues;
125 for (i = 0; i < nb_rx; i++) {
126 ret = nfb_eth_rx_queue_start(dev, i);
131 for (i = 0; i < nb_tx; i++) {
132 ret = nfb_eth_tx_queue_start(dev, i);
140 for (i = 0; i < nb_tx; i++)
141 nfb_eth_tx_queue_stop(dev, i);
143 for (i = 0; i < nb_rx; i++)
144 nfb_eth_rx_queue_stop(dev, i);
149 * DPDK callback to stop the device.
151 * Stop device by stopping all configured queues.
154 * Pointer to Ethernet device structure.
157 nfb_eth_dev_stop(struct rte_eth_dev *dev)
160 uint16_t nb_rx = dev->data->nb_rx_queues;
161 uint16_t nb_tx = dev->data->nb_tx_queues;
163 dev->data->dev_started = 0;
165 for (i = 0; i < nb_tx; i++)
166 nfb_eth_tx_queue_stop(dev, i);
168 for (i = 0; i < nb_rx; i++)
169 nfb_eth_rx_queue_stop(dev, i);
175 * DPDK callback for Ethernet device configuration.
178 * Pointer to Ethernet device structure.
181 * 0 on success, a negative errno value otherwise.
184 nfb_eth_dev_configure(struct rte_eth_dev *dev __rte_unused)
187 struct pmd_internals *internals = dev->data->dev_private;
188 struct rte_eth_conf *dev_conf = &dev->data->dev_conf;
190 if (dev_conf->rxmode.offloads & RTE_ETH_RX_OFFLOAD_TIMESTAMP) {
191 ret = rte_mbuf_dyn_rx_timestamp_register
192 (&nfb_timestamp_dynfield_offset,
193 &nfb_timestamp_rx_dynflag);
195 RTE_LOG(ERR, PMD, "Cannot register Rx timestamp"
196 " field/flag %d\n", ret);
197 nfb_close(internals->nfb);
206 nfb_eth_get_max_mac_address_count(struct rte_eth_dev *dev)
210 uint32_t ret = (uint32_t)-1;
211 struct pmd_internals *internals = dev->data->dev_private;
214 * Go through all RX MAC components in firmware and find
215 * the minimal indicated space size for MAC addresses.
217 for (i = 0; i < internals->max_rxmac; i++) {
218 c = nc_rxmac_mac_address_count(internals->rxmac[i]);
219 ret = RTE_MIN(c, ret);
222 /* The driver must support at least 1 MAC address, pretend that */
223 if (internals->max_rxmac == 0 || ret == 0)
230 * DPDK callback to get information about the device.
233 * Pointer to Ethernet device structure.
235 * Info structure output buffer.
238 nfb_eth_dev_info(struct rte_eth_dev *dev,
239 struct rte_eth_dev_info *dev_info)
241 dev_info->max_mac_addrs = nfb_eth_get_max_mac_address_count(dev);
243 dev_info->max_rx_pktlen = (uint32_t)-1;
244 dev_info->max_rx_queues = dev->data->nb_rx_queues;
245 dev_info->max_tx_queues = dev->data->nb_tx_queues;
246 dev_info->speed_capa = RTE_ETH_LINK_SPEED_100G;
247 dev_info->rx_offload_capa =
248 RTE_ETH_RX_OFFLOAD_TIMESTAMP;
254 * DPDK callback to close the device.
256 * Destroy all queues and objects, free memory.
259 * Pointer to Ethernet device structure.
262 nfb_eth_dev_close(struct rte_eth_dev *dev)
264 struct pmd_internals *internals = dev->data->dev_private;
266 uint16_t nb_rx = dev->data->nb_rx_queues;
267 uint16_t nb_tx = dev->data->nb_tx_queues;
270 if (rte_eal_process_type() != RTE_PROC_PRIMARY)
273 ret = nfb_eth_dev_stop(dev);
275 nfb_nc_rxmac_deinit(internals->rxmac, internals->max_rxmac);
276 nfb_nc_txmac_deinit(internals->txmac, internals->max_txmac);
278 for (i = 0; i < nb_rx; i++) {
279 nfb_eth_rx_queue_release(dev, i);
280 dev->data->rx_queues[i] = NULL;
282 dev->data->nb_rx_queues = 0;
283 for (i = 0; i < nb_tx; i++) {
284 nfb_eth_tx_queue_release(dev, i);
285 dev->data->tx_queues[i] = NULL;
287 dev->data->nb_tx_queues = 0;
293 * DPDK callback to retrieve physical link information.
296 * Pointer to Ethernet device structure.
298 * Storage for current link status.
301 * 0 on success, a negative errno value otherwise.
304 nfb_eth_link_update(struct rte_eth_dev *dev,
305 int wait_to_complete __rte_unused)
308 struct nc_rxmac_status status;
309 struct rte_eth_link link;
310 memset(&link, 0, sizeof(link));
312 struct pmd_internals *internals = dev->data->dev_private;
314 status.speed = MAC_SPEED_UNKNOWN;
316 link.link_speed = RTE_ETH_SPEED_NUM_NONE;
317 link.link_status = RTE_ETH_LINK_DOWN;
318 link.link_duplex = RTE_ETH_LINK_FULL_DUPLEX;
319 link.link_autoneg = RTE_ETH_LINK_SPEED_FIXED;
321 if (internals->rxmac[0] != NULL) {
322 nc_rxmac_read_status(internals->rxmac[0], &status);
324 switch (status.speed) {
326 link.link_speed = RTE_ETH_SPEED_NUM_10G;
329 link.link_speed = RTE_ETH_SPEED_NUM_40G;
332 link.link_speed = RTE_ETH_SPEED_NUM_100G;
335 link.link_speed = RTE_ETH_SPEED_NUM_NONE;
340 for (i = 0; i < internals->max_rxmac; ++i) {
341 nc_rxmac_read_status(internals->rxmac[i], &status);
343 if (status.enabled && status.link_up) {
344 link.link_status = RTE_ETH_LINK_UP;
349 rte_eth_linkstatus_set(dev, &link);
355 * DPDK callback to bring the link UP.
358 * Pointer to Ethernet device structure.
361 * 0 on success, a negative errno value otherwise.
364 nfb_eth_dev_set_link_up(struct rte_eth_dev *dev)
366 struct pmd_internals *internals = (struct pmd_internals *)
367 dev->data->dev_private;
370 for (i = 0; i < internals->max_rxmac; ++i)
371 nc_rxmac_enable(internals->rxmac[i]);
373 for (i = 0; i < internals->max_txmac; ++i)
374 nc_txmac_enable(internals->txmac[i]);
380 * DPDK callback to bring the link DOWN.
383 * Pointer to Ethernet device structure.
386 * 0 on success, a negative errno value otherwise.
389 nfb_eth_dev_set_link_down(struct rte_eth_dev *dev)
391 struct pmd_internals *internals = (struct pmd_internals *)
392 dev->data->dev_private;
395 for (i = 0; i < internals->max_rxmac; ++i)
396 nc_rxmac_disable(internals->rxmac[i]);
398 for (i = 0; i < internals->max_txmac; ++i)
399 nc_txmac_disable(internals->txmac[i]);
405 nfb_eth_mac_addr_conv(struct rte_ether_addr *mac_addr)
409 for (i = 0; i < RTE_ETHER_ADDR_LEN; i++) {
411 res |= mac_addr->addr_bytes[i] & 0xFF;
417 * DPDK callback to set primary MAC address.
420 * Pointer to Ethernet device structure.
422 * MAC address to register.
425 * 0 on success, a negative errno value otherwise.
428 nfb_eth_mac_addr_set(struct rte_eth_dev *dev,
429 struct rte_ether_addr *mac_addr)
433 struct rte_eth_dev_data *data = dev->data;
434 struct pmd_internals *internals = (struct pmd_internals *)
437 mac = nfb_eth_mac_addr_conv(mac_addr);
438 /* Until no real multi-port support, configure all RX MACs the same */
439 for (i = 0; i < internals->max_rxmac; ++i)
440 nc_rxmac_set_mac(internals->rxmac[i], 0, mac, 1);
446 nfb_eth_mac_addr_add(struct rte_eth_dev *dev,
447 struct rte_ether_addr *mac_addr, uint32_t index, uint32_t pool __rte_unused)
451 struct rte_eth_dev_data *data = dev->data;
452 struct pmd_internals *internals = (struct pmd_internals *)
455 mac = nfb_eth_mac_addr_conv(mac_addr);
456 for (i = 0; i < internals->max_rxmac; ++i)
457 nc_rxmac_set_mac(internals->rxmac[i], index, mac, 1);
463 nfb_eth_mac_addr_remove(struct rte_eth_dev *dev, uint32_t index)
466 struct rte_eth_dev_data *data = dev->data;
467 struct pmd_internals *internals = (struct pmd_internals *)
470 for (i = 0; i < internals->max_rxmac; ++i)
471 nc_rxmac_set_mac(internals->rxmac[i], index, 0, 0);
474 static const struct eth_dev_ops ops = {
475 .dev_start = nfb_eth_dev_start,
476 .dev_stop = nfb_eth_dev_stop,
477 .dev_set_link_up = nfb_eth_dev_set_link_up,
478 .dev_set_link_down = nfb_eth_dev_set_link_down,
479 .dev_close = nfb_eth_dev_close,
480 .dev_configure = nfb_eth_dev_configure,
481 .dev_infos_get = nfb_eth_dev_info,
482 .promiscuous_enable = nfb_eth_promiscuous_enable,
483 .promiscuous_disable = nfb_eth_promiscuous_disable,
484 .allmulticast_enable = nfb_eth_allmulticast_enable,
485 .allmulticast_disable = nfb_eth_allmulticast_disable,
486 .rx_queue_start = nfb_eth_rx_queue_start,
487 .rx_queue_stop = nfb_eth_rx_queue_stop,
488 .tx_queue_start = nfb_eth_tx_queue_start,
489 .tx_queue_stop = nfb_eth_tx_queue_stop,
490 .rx_queue_setup = nfb_eth_rx_queue_setup,
491 .tx_queue_setup = nfb_eth_tx_queue_setup,
492 .rx_queue_release = nfb_eth_rx_queue_release,
493 .tx_queue_release = nfb_eth_tx_queue_release,
494 .link_update = nfb_eth_link_update,
495 .stats_get = nfb_eth_stats_get,
496 .stats_reset = nfb_eth_stats_reset,
497 .mac_addr_set = nfb_eth_mac_addr_set,
498 .mac_addr_add = nfb_eth_mac_addr_add,
499 .mac_addr_remove = nfb_eth_mac_addr_remove,
503 * DPDK callback to initialize an ethernet device
506 * Pointer to ethernet device structure
509 * 0 on success, a negative errno value otherwise.
512 nfb_eth_dev_init(struct rte_eth_dev *dev)
515 struct rte_eth_dev_data *data = dev->data;
516 struct pmd_internals *internals = (struct pmd_internals *)
518 struct rte_pci_device *pci_dev = RTE_ETH_DEV_TO_PCI(dev);
519 struct rte_pci_addr *pci_addr = &pci_dev->addr;
520 struct rte_ether_addr eth_addr_init;
521 struct rte_kvargs *kvlist;
523 RTE_LOG(INFO, PMD, "Initializing NFB device (" PCI_PRI_FMT ")\n",
524 pci_addr->domain, pci_addr->bus, pci_addr->devid,
527 snprintf(internals->nfb_dev, PATH_MAX,
528 "/dev/nfb/by-pci-slot/" PCI_PRI_FMT,
529 pci_addr->domain, pci_addr->bus, pci_addr->devid,
532 /* Check validity of device args */
533 if (dev->device->devargs != NULL &&
534 dev->device->devargs->args != NULL &&
535 strlen(dev->device->devargs->args) > 0) {
536 kvlist = rte_kvargs_parse(dev->device->devargs->args,
538 if (kvlist == NULL) {
539 RTE_LOG(ERR, PMD, "Failed to parse device arguments %s",
540 dev->device->devargs->args);
541 rte_kvargs_free(kvlist);
544 rte_kvargs_free(kvlist);
548 * Get number of available DMA RX and TX queues, which is maximum
549 * number of queues that can be created and store it in private device
552 internals->nfb = nfb_open(internals->nfb_dev);
553 if (internals->nfb == NULL) {
554 RTE_LOG(ERR, PMD, "nfb_open(): failed to open %s",
558 data->nb_rx_queues = ndp_get_rx_queue_available_count(internals->nfb);
559 data->nb_tx_queues = ndp_get_tx_queue_available_count(internals->nfb);
561 RTE_LOG(INFO, PMD, "Available NDP queues RX: %u TX: %u\n",
562 data->nb_rx_queues, data->nb_tx_queues);
564 nfb_nc_rxmac_init(internals->nfb,
566 &internals->max_rxmac);
567 nfb_nc_txmac_init(internals->nfb,
569 &internals->max_txmac);
571 /* Set rx, tx burst functions */
572 dev->rx_pkt_burst = nfb_eth_ndp_rx;
573 dev->tx_pkt_burst = nfb_eth_ndp_tx;
575 /* Set function callbacks for Ethernet API */
579 nfb_eth_link_update(dev, 0);
581 /* Allocate space for MAC addresses */
582 mac_count = nfb_eth_get_max_mac_address_count(dev);
583 data->mac_addrs = rte_zmalloc(data->name,
584 sizeof(struct rte_ether_addr) * mac_count, RTE_CACHE_LINE_SIZE);
585 if (data->mac_addrs == NULL) {
586 RTE_LOG(ERR, PMD, "Could not alloc space for MAC address!\n");
587 nfb_close(internals->nfb);
591 rte_eth_random_addr(eth_addr_init.addr_bytes);
592 eth_addr_init.addr_bytes[0] = eth_addr.addr_bytes[0];
593 eth_addr_init.addr_bytes[1] = eth_addr.addr_bytes[1];
594 eth_addr_init.addr_bytes[2] = eth_addr.addr_bytes[2];
596 nfb_eth_mac_addr_set(dev, ð_addr_init);
597 rte_ether_addr_copy(ð_addr_init, &dev->data->mac_addrs[0]);
599 data->promiscuous = nfb_eth_promiscuous_get(dev);
600 data->all_multicast = nfb_eth_allmulticast_get(dev);
602 dev->data->dev_flags |= RTE_ETH_DEV_AUTOFILL_QUEUE_XSTATS;
604 RTE_LOG(INFO, PMD, "NFB device ("
605 PCI_PRI_FMT ") successfully initialized\n",
606 pci_addr->domain, pci_addr->bus, pci_addr->devid,
613 * DPDK callback to uninitialize an ethernet device
616 * Pointer to ethernet device structure
619 * 0 on success, a negative errno value otherwise.
622 nfb_eth_dev_uninit(struct rte_eth_dev *dev)
624 struct rte_pci_device *pci_dev = RTE_ETH_DEV_TO_PCI(dev);
625 struct rte_pci_addr *pci_addr = &pci_dev->addr;
627 nfb_eth_dev_close(dev);
629 RTE_LOG(INFO, PMD, "NFB device ("
630 PCI_PRI_FMT ") successfully uninitialized\n",
631 pci_addr->domain, pci_addr->bus, pci_addr->devid,
637 static const struct rte_pci_id nfb_pci_id_table[] = {
638 { RTE_PCI_DEVICE(PCI_VENDOR_ID_NETCOPE, PCI_DEVICE_ID_NFB_40G2) },
639 { RTE_PCI_DEVICE(PCI_VENDOR_ID_NETCOPE, PCI_DEVICE_ID_NFB_100G2) },
640 { RTE_PCI_DEVICE(PCI_VENDOR_ID_NETCOPE, PCI_DEVICE_ID_NFB_200G2QL) },
641 { RTE_PCI_DEVICE(PCI_VENDOR_ID_SILICOM, PCI_DEVICE_ID_FB2CGG3) },
642 { RTE_PCI_DEVICE(PCI_VENDOR_ID_SILICOM, PCI_DEVICE_ID_FB2CGG3D) },
647 * DPDK callback to register a PCI device.
649 * This function spawns Ethernet devices out of a given PCI device.
652 * PCI driver structure (nfb_driver).
654 * PCI device information.
657 * 0 on success, a negative errno value otherwise.
660 nfb_eth_pci_probe(struct rte_pci_driver *pci_drv __rte_unused,
661 struct rte_pci_device *pci_dev)
663 return rte_eth_dev_pci_generic_probe(pci_dev,
664 sizeof(struct pmd_internals), nfb_eth_dev_init);
668 * DPDK callback to remove a PCI device.
670 * This function removes all Ethernet devices belong to a given PCI device.
673 * Pointer to the PCI device.
676 * 0 on success, the function cannot fail.
679 nfb_eth_pci_remove(struct rte_pci_device *pci_dev)
681 return rte_eth_dev_pci_generic_remove(pci_dev, nfb_eth_dev_uninit);
684 static struct rte_pci_driver nfb_eth_driver = {
685 .id_table = nfb_pci_id_table,
686 .probe = nfb_eth_pci_probe,
687 .remove = nfb_eth_pci_remove,
690 RTE_PMD_REGISTER_PCI(RTE_NFB_DRIVER_NAME, nfb_eth_driver);
691 RTE_PMD_REGISTER_PCI_TABLE(RTE_NFB_DRIVER_NAME, nfb_pci_id_table);
692 RTE_PMD_REGISTER_KMOD_DEP(RTE_NFB_DRIVER_NAME, "* nfb");