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 for (i = 0; i < nb_tx; i++)
162 nfb_eth_tx_queue_stop(dev, i);
164 for (i = 0; i < nb_rx; i++)
165 nfb_eth_rx_queue_stop(dev, i);
169 * DPDK callback for Ethernet device configuration.
172 * Pointer to Ethernet device structure.
175 * 0 on success, a negative errno value otherwise.
178 nfb_eth_dev_configure(struct rte_eth_dev *dev __rte_unused)
184 * DPDK callback to get information about the device.
187 * Pointer to Ethernet device structure.
189 * Info structure output buffer.
192 nfb_eth_dev_info(struct rte_eth_dev *dev,
193 struct rte_eth_dev_info *dev_info)
195 dev_info->max_mac_addrs = 1;
196 dev_info->max_rx_pktlen = (uint32_t)-1;
197 dev_info->max_rx_queues = dev->data->nb_rx_queues;
198 dev_info->max_tx_queues = dev->data->nb_tx_queues;
199 dev_info->speed_capa = ETH_LINK_SPEED_100G;
205 * DPDK callback to close the device.
207 * Destroy all queues and objects, free memory.
210 * Pointer to Ethernet device structure.
213 nfb_eth_dev_close(struct rte_eth_dev *dev)
215 struct pmd_internals *internals = dev->data->dev_private;
217 uint16_t nb_rx = dev->data->nb_rx_queues;
218 uint16_t nb_tx = dev->data->nb_tx_queues;
220 nfb_eth_dev_stop(dev);
222 nfb_nc_rxmac_deinit(internals->rxmac, internals->max_rxmac);
223 nfb_nc_txmac_deinit(internals->txmac, internals->max_txmac);
225 for (i = 0; i < nb_rx; i++) {
226 nfb_eth_rx_queue_release(dev->data->rx_queues[i]);
227 dev->data->rx_queues[i] = NULL;
229 dev->data->nb_rx_queues = 0;
230 for (i = 0; i < nb_tx; i++) {
231 nfb_eth_tx_queue_release(dev->data->tx_queues[i]);
232 dev->data->tx_queues[i] = NULL;
234 dev->data->nb_tx_queues = 0;
236 rte_free(dev->data->mac_addrs);
237 dev->data->mac_addrs = NULL;
241 * DPDK callback to retrieve physical link information.
244 * Pointer to Ethernet device structure.
246 * Storage for current link status.
249 * 0 on success, a negative errno value otherwise.
252 nfb_eth_link_update(struct rte_eth_dev *dev,
253 int wait_to_complete __rte_unused)
256 struct nc_rxmac_status status;
257 struct rte_eth_link link;
258 memset(&link, 0, sizeof(link));
260 struct pmd_internals *internals = dev->data->dev_private;
262 status.speed = MAC_SPEED_UNKNOWN;
264 link.link_speed = ETH_SPEED_NUM_NONE;
265 link.link_status = ETH_LINK_DOWN;
266 link.link_duplex = ETH_LINK_FULL_DUPLEX;
267 link.link_autoneg = ETH_LINK_SPEED_FIXED;
269 if (internals->rxmac[0] != NULL) {
270 nc_rxmac_read_status(internals->rxmac[0], &status);
272 switch (status.speed) {
274 link.link_speed = ETH_SPEED_NUM_10G;
277 link.link_speed = ETH_SPEED_NUM_40G;
280 link.link_speed = ETH_SPEED_NUM_100G;
283 link.link_speed = ETH_SPEED_NUM_NONE;
288 for (i = 0; i < internals->max_rxmac; ++i) {
289 nc_rxmac_read_status(internals->rxmac[i], &status);
291 if (status.enabled && status.link_up) {
292 link.link_status = ETH_LINK_UP;
297 rte_eth_linkstatus_set(dev, &link);
303 * DPDK callback to bring the link UP.
306 * Pointer to Ethernet device structure.
309 * 0 on success, a negative errno value otherwise.
312 nfb_eth_dev_set_link_up(struct rte_eth_dev *dev)
314 struct pmd_internals *internals = (struct pmd_internals *)
315 dev->data->dev_private;
318 for (i = 0; i < internals->max_rxmac; ++i)
319 nc_rxmac_enable(internals->rxmac[i]);
321 for (i = 0; i < internals->max_txmac; ++i)
322 nc_txmac_enable(internals->txmac[i]);
328 * DPDK callback to bring the link DOWN.
331 * Pointer to Ethernet device structure.
334 * 0 on success, a negative errno value otherwise.
337 nfb_eth_dev_set_link_down(struct rte_eth_dev *dev)
339 struct pmd_internals *internals = (struct pmd_internals *)
340 dev->data->dev_private;
343 for (i = 0; i < internals->max_rxmac; ++i)
344 nc_rxmac_disable(internals->rxmac[i]);
346 for (i = 0; i < internals->max_txmac; ++i)
347 nc_txmac_disable(internals->txmac[i]);
353 * DPDK callback to set primary MAC address.
356 * Pointer to Ethernet device structure.
358 * MAC address to register.
361 * 0 on success, a negative errno value otherwise.
364 nfb_eth_mac_addr_set(struct rte_eth_dev *dev,
365 struct rte_ether_addr *mac_addr)
369 struct rte_eth_dev_data *data = dev->data;
370 struct pmd_internals *internals = (struct pmd_internals *)
373 if (!rte_is_valid_assigned_ether_addr(mac_addr))
376 for (i = 0; i < RTE_ETHER_ADDR_LEN; i++) {
378 mac |= mac_addr->addr_bytes[i] & 0xFF;
381 for (i = 0; i < internals->max_rxmac; ++i)
382 nc_rxmac_set_mac(internals->rxmac[i], 0, mac, 1);
384 rte_ether_addr_copy(mac_addr, data->mac_addrs);
388 static const struct eth_dev_ops ops = {
389 .dev_start = nfb_eth_dev_start,
390 .dev_stop = nfb_eth_dev_stop,
391 .dev_set_link_up = nfb_eth_dev_set_link_up,
392 .dev_set_link_down = nfb_eth_dev_set_link_down,
393 .dev_close = nfb_eth_dev_close,
394 .dev_configure = nfb_eth_dev_configure,
395 .dev_infos_get = nfb_eth_dev_info,
396 .promiscuous_enable = nfb_eth_promiscuous_enable,
397 .promiscuous_disable = nfb_eth_promiscuous_disable,
398 .allmulticast_enable = nfb_eth_allmulticast_enable,
399 .allmulticast_disable = nfb_eth_allmulticast_disable,
400 .rx_queue_start = nfb_eth_rx_queue_start,
401 .rx_queue_stop = nfb_eth_rx_queue_stop,
402 .tx_queue_start = nfb_eth_tx_queue_start,
403 .tx_queue_stop = nfb_eth_tx_queue_stop,
404 .rx_queue_setup = nfb_eth_rx_queue_setup,
405 .tx_queue_setup = nfb_eth_tx_queue_setup,
406 .rx_queue_release = nfb_eth_rx_queue_release,
407 .tx_queue_release = nfb_eth_tx_queue_release,
408 .link_update = nfb_eth_link_update,
409 .stats_get = nfb_eth_stats_get,
410 .stats_reset = nfb_eth_stats_reset,
411 .mac_addr_set = nfb_eth_mac_addr_set,
415 * DPDK callback to initialize an ethernet device
418 * Pointer to ethernet device structure
421 * 0 on success, a negative errno value otherwise.
424 nfb_eth_dev_init(struct rte_eth_dev *dev)
426 struct rte_eth_dev_data *data = dev->data;
427 struct pmd_internals *internals = (struct pmd_internals *)
429 struct rte_pci_device *pci_dev = RTE_ETH_DEV_TO_PCI(dev);
430 struct rte_pci_addr *pci_addr = &pci_dev->addr;
431 struct rte_ether_addr eth_addr_init;
432 struct rte_kvargs *kvlist;
434 RTE_LOG(INFO, PMD, "Initializing NFB device (" PCI_PRI_FMT ")\n",
435 pci_addr->domain, pci_addr->bus, pci_addr->devid,
438 snprintf(internals->nfb_dev, PATH_MAX,
439 "/dev/nfb/by-pci-slot/" PCI_PRI_FMT,
440 pci_addr->domain, pci_addr->bus, pci_addr->devid,
443 /* Check validity of device args */
444 if (dev->device->devargs != NULL &&
445 dev->device->devargs->args != NULL &&
446 strlen(dev->device->devargs->args) > 0) {
447 kvlist = rte_kvargs_parse(dev->device->devargs->args,
449 if (kvlist == NULL) {
450 RTE_LOG(ERR, PMD, "Failed to parse device arguments %s",
451 dev->device->devargs->args);
452 rte_kvargs_free(kvlist);
455 rte_kvargs_free(kvlist);
458 /* Let rte_eth_dev_close() release the port resources */
459 dev->data->dev_flags |= RTE_ETH_DEV_CLOSE_REMOVE;
462 * Get number of available DMA RX and TX queues, which is maximum
463 * number of queues that can be created and store it in private device
466 internals->nfb = nfb_open(internals->nfb_dev);
467 if (internals->nfb == NULL) {
468 RTE_LOG(ERR, PMD, "nfb_open(): failed to open %s",
472 data->nb_rx_queues = ndp_get_rx_queue_available_count(internals->nfb);
473 data->nb_tx_queues = ndp_get_tx_queue_available_count(internals->nfb);
475 RTE_LOG(INFO, PMD, "Available NDP queues RX: %u TX: %u\n",
476 data->nb_rx_queues, data->nb_tx_queues);
478 nfb_nc_rxmac_init(internals->nfb,
480 &internals->max_rxmac);
481 nfb_nc_txmac_init(internals->nfb,
483 &internals->max_txmac);
485 /* Set rx, tx burst functions */
486 dev->rx_pkt_burst = nfb_eth_ndp_rx;
487 dev->tx_pkt_burst = nfb_eth_ndp_tx;
489 /* Set function callbacks for Ethernet API */
493 nfb_eth_link_update(dev, 0);
495 /* Allocate space for one mac address */
496 data->mac_addrs = rte_zmalloc(data->name, sizeof(struct rte_ether_addr),
497 RTE_CACHE_LINE_SIZE);
498 if (data->mac_addrs == NULL) {
499 RTE_LOG(ERR, PMD, "Could not alloc space for MAC address!\n");
500 nfb_close(internals->nfb);
504 rte_eth_random_addr(eth_addr_init.addr_bytes);
505 eth_addr_init.addr_bytes[0] = eth_addr.addr_bytes[0];
506 eth_addr_init.addr_bytes[1] = eth_addr.addr_bytes[1];
507 eth_addr_init.addr_bytes[2] = eth_addr.addr_bytes[2];
509 nfb_eth_mac_addr_set(dev, ð_addr_init);
511 data->promiscuous = nfb_eth_promiscuous_get(dev);
512 data->all_multicast = nfb_eth_allmulticast_get(dev);
513 internals->rx_filter_original = data->promiscuous;
515 RTE_LOG(INFO, PMD, "NFB device ("
516 PCI_PRI_FMT ") successfully initialized\n",
517 pci_addr->domain, pci_addr->bus, pci_addr->devid,
524 * DPDK callback to uninitialize an ethernet device
527 * Pointer to ethernet device structure
530 * 0 on success, a negative errno value otherwise.
533 nfb_eth_dev_uninit(struct rte_eth_dev *dev)
535 struct rte_pci_device *pci_dev = RTE_ETH_DEV_TO_PCI(dev);
536 struct rte_pci_addr *pci_addr = &pci_dev->addr;
538 nfb_eth_dev_close(dev);
540 RTE_LOG(INFO, PMD, "NFB device ("
541 PCI_PRI_FMT ") successfully uninitialized\n",
542 pci_addr->domain, pci_addr->bus, pci_addr->devid,
548 static const struct rte_pci_id nfb_pci_id_table[] = {
549 { RTE_PCI_DEVICE(PCI_VENDOR_ID_NETCOPE, PCI_DEVICE_ID_NFB_40G2) },
550 { RTE_PCI_DEVICE(PCI_VENDOR_ID_NETCOPE, PCI_DEVICE_ID_NFB_100G2) },
551 { RTE_PCI_DEVICE(PCI_VENDOR_ID_NETCOPE, PCI_DEVICE_ID_NFB_200G2QL) },
552 { RTE_PCI_DEVICE(PCI_VENDOR_ID_SILICOM, PCI_DEVICE_ID_FB2CGG3) },
553 { RTE_PCI_DEVICE(PCI_VENDOR_ID_SILICOM, PCI_DEVICE_ID_FB2CGG3D) },
558 * DPDK callback to register a PCI device.
560 * This function spawns Ethernet devices out of a given PCI device.
563 * PCI driver structure (nfb_driver).
565 * PCI device information.
568 * 0 on success, a negative errno value otherwise.
571 nfb_eth_pci_probe(struct rte_pci_driver *pci_drv __rte_unused,
572 struct rte_pci_device *pci_dev)
574 return rte_eth_dev_pci_generic_probe(pci_dev,
575 sizeof(struct pmd_internals), nfb_eth_dev_init);
579 * DPDK callback to remove a PCI device.
581 * This function removes all Ethernet devices belong to a given PCI device.
584 * Pointer to the PCI device.
587 * 0 on success, the function cannot fail.
590 nfb_eth_pci_remove(struct rte_pci_device *pci_dev)
592 return rte_eth_dev_pci_generic_remove(pci_dev, nfb_eth_dev_uninit);
595 static struct rte_pci_driver nfb_eth_driver = {
596 .id_table = nfb_pci_id_table,
597 .probe = nfb_eth_pci_probe,
598 .remove = nfb_eth_pci_remove,
601 RTE_PMD_REGISTER_PCI(RTE_NFB_DRIVER_NAME, nfb_eth_driver);
602 RTE_PMD_REGISTER_PCI_TABLE(RTE_NFB_DRIVER_NAME, nfb_pci_id_table);
603 RTE_PMD_REGISTER_KMOD_DEP(RTE_NFB_DRIVER_NAME, "* nfb");
604 RTE_PMD_REGISTER_PARAM_STRING(RTE_NFB_DRIVER_NAME, TIMESTAMP_ARG "=<0|1>");