1 /* SPDX-License-Identifier: BSD-3-Clause 2 * Copyright(c) 2019 Cesnet 3 * Copyright(c) 2019 Netcope Technologies, a.s. <[email protected]> 4 * All rights reserved. 5 */ 6 7 #include <nfb/nfb.h> 8 #include <nfb/ndp.h> 9 #include <netcope/rxmac.h> 10 #include <netcope/txmac.h> 11 12 #include <rte_ethdev_pci.h> 13 #include <rte_kvargs.h> 14 15 #include "nfb_stats.h" 16 #include "nfb_rx.h" 17 #include "nfb_tx.h" 18 #include "nfb_rxmode.h" 19 #include "nfb.h" 20 21 /** 22 * Default MAC addr 23 */ 24 static const struct rte_ether_addr eth_addr = { 25 .addr_bytes = { 0x00, 0x11, 0x17, 0x00, 0x00, 0x00 } 26 }; 27 28 /** 29 * Open all RX DMA queues 30 * 31 * @param dev 32 * Pointer to nfb device. 33 * @param[out] rxmac 34 * Pointer to output array of nc_rxmac 35 * @param[out] max_rxmac 36 * Pointer to output max index of rxmac 37 */ 38 static void 39 nfb_nc_rxmac_init(struct nfb_device *nfb, 40 struct nc_rxmac *rxmac[RTE_MAX_NC_RXMAC], 41 uint16_t *max_rxmac) 42 { 43 *max_rxmac = 0; 44 while ((rxmac[*max_rxmac] = nc_rxmac_open_index(nfb, *max_rxmac))) 45 ++(*max_rxmac); 46 } 47 48 /** 49 * Open all TX DMA queues 50 * 51 * @param dev 52 * Pointer to nfb device. 53 * @param[out] txmac 54 * Pointer to output array of nc_txmac 55 * @param[out] max_rxmac 56 * Pointer to output max index of txmac 57 */ 58 static void 59 nfb_nc_txmac_init(struct nfb_device *nfb, 60 struct nc_txmac *txmac[RTE_MAX_NC_TXMAC], 61 uint16_t *max_txmac) 62 { 63 *max_txmac = 0; 64 while ((txmac[*max_txmac] = nc_txmac_open_index(nfb, *max_txmac))) 65 ++(*max_txmac); 66 } 67 68 /** 69 * Close all RX DMA queues 70 * 71 * @param rxmac 72 * Pointer to array of nc_rxmac 73 * @param max_rxmac 74 * Maximum index of rxmac 75 */ 76 static void 77 nfb_nc_rxmac_deinit(struct nc_rxmac *rxmac[RTE_MAX_NC_RXMAC], 78 uint16_t max_rxmac) 79 { 80 for (; max_rxmac > 0; --max_rxmac) { 81 nc_rxmac_close(rxmac[max_rxmac]); 82 rxmac[max_rxmac] = NULL; 83 } 84 } 85 86 /** 87 * Close all TX DMA queues 88 * 89 * @param txmac 90 * Pointer to array of nc_txmac 91 * @param max_txmac 92 * Maximum index of txmac 93 */ 94 static void 95 nfb_nc_txmac_deinit(struct nc_txmac *txmac[RTE_MAX_NC_TXMAC], 96 uint16_t max_txmac) 97 { 98 for (; max_txmac > 0; --max_txmac) { 99 nc_txmac_close(txmac[max_txmac]); 100 txmac[max_txmac] = NULL; 101 } 102 } 103 104 /** 105 * DPDK callback to start the device. 106 * 107 * Start device by starting all configured queues. 108 * 109 * @param dev 110 * Pointer to Ethernet device structure. 111 * 112 * @return 113 * 0 on success, a negative errno value otherwise. 114 */ 115 static int 116 nfb_eth_dev_start(struct rte_eth_dev *dev) 117 { 118 int ret; 119 uint16_t i; 120 uint16_t nb_rx = dev->data->nb_rx_queues; 121 uint16_t nb_tx = dev->data->nb_tx_queues; 122 123 for (i = 0; i < nb_rx; i++) { 124 ret = nfb_eth_rx_queue_start(dev, i); 125 if (ret != 0) 126 goto err_rx; 127 } 128 129 for (i = 0; i < nb_tx; i++) { 130 ret = nfb_eth_tx_queue_start(dev, i); 131 if (ret != 0) 132 goto err_tx; 133 } 134 135 return 0; 136 137 err_tx: 138 for (i = 0; i < nb_tx; i++) 139 nfb_eth_tx_queue_stop(dev, i); 140 err_rx: 141 for (i = 0; i < nb_rx; i++) 142 nfb_eth_rx_queue_stop(dev, i); 143 return ret; 144 } 145 146 /** 147 * DPDK callback to stop the device. 148 * 149 * Stop device by stopping all configured queues. 150 * 151 * @param dev 152 * Pointer to Ethernet device structure. 153 */ 154 static void 155 nfb_eth_dev_stop(struct rte_eth_dev *dev) 156 { 157 uint16_t i; 158 uint16_t nb_rx = dev->data->nb_rx_queues; 159 uint16_t nb_tx = dev->data->nb_tx_queues; 160 161 for (i = 0; i < nb_tx; i++) 162 nfb_eth_tx_queue_stop(dev, i); 163 164 for (i = 0; i < nb_rx; i++) 165 nfb_eth_rx_queue_stop(dev, i); 166 } 167 168 /** 169 * DPDK callback for Ethernet device configuration. 170 * 171 * @param dev 172 * Pointer to Ethernet device structure. 173 * 174 * @return 175 * 0 on success, a negative errno value otherwise. 176 */ 177 static int 178 nfb_eth_dev_configure(struct rte_eth_dev *dev __rte_unused) 179 { 180 return 0; 181 } 182 183 /** 184 * DPDK callback to get information about the device. 185 * 186 * @param dev 187 * Pointer to Ethernet device structure. 188 * @param[out] info 189 * Info structure output buffer. 190 */ 191 static void 192 nfb_eth_dev_info(struct rte_eth_dev *dev, 193 struct rte_eth_dev_info *dev_info) 194 { 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; 200 } 201 202 /** 203 * DPDK callback to close the device. 204 * 205 * Destroy all queues and objects, free memory. 206 * 207 * @param dev 208 * Pointer to Ethernet device structure. 209 */ 210 static void 211 nfb_eth_dev_close(struct rte_eth_dev *dev) 212 { 213 struct pmd_internals *internals = dev->data->dev_private; 214 uint16_t i; 215 uint16_t nb_rx = dev->data->nb_rx_queues; 216 uint16_t nb_tx = dev->data->nb_tx_queues; 217 218 nfb_eth_dev_stop(dev); 219 220 nfb_nc_rxmac_deinit(internals->rxmac, internals->max_rxmac); 221 nfb_nc_txmac_deinit(internals->txmac, internals->max_txmac); 222 223 for (i = 0; i < nb_rx; i++) { 224 nfb_eth_rx_queue_release(dev->data->rx_queues[i]); 225 dev->data->rx_queues[i] = NULL; 226 } 227 dev->data->nb_rx_queues = 0; 228 for (i = 0; i < nb_tx; i++) { 229 nfb_eth_tx_queue_release(dev->data->tx_queues[i]); 230 dev->data->tx_queues[i] = NULL; 231 } 232 dev->data->nb_tx_queues = 0; 233 234 rte_free(dev->data->mac_addrs); 235 dev->data->mac_addrs = NULL; 236 } 237 238 /** 239 * DPDK callback to retrieve physical link information. 240 * 241 * @param dev 242 * Pointer to Ethernet device structure. 243 * @param[out] link 244 * Storage for current link status. 245 * 246 * @return 247 * 0 on success, a negative errno value otherwise. 248 */ 249 static int 250 nfb_eth_link_update(struct rte_eth_dev *dev, 251 int wait_to_complete __rte_unused) 252 { 253 uint16_t i; 254 struct nc_rxmac_status status; 255 struct rte_eth_link link; 256 memset(&link, 0, sizeof(link)); 257 258 struct pmd_internals *internals = dev->data->dev_private; 259 260 status.speed = MAC_SPEED_UNKNOWN; 261 262 link.link_speed = ETH_SPEED_NUM_NONE; 263 link.link_status = ETH_LINK_DOWN; 264 link.link_duplex = ETH_LINK_FULL_DUPLEX; 265 link.link_autoneg = ETH_LINK_SPEED_FIXED; 266 267 if (internals->rxmac[0] != NULL) { 268 nc_rxmac_read_status(internals->rxmac[0], &status); 269 270 switch (status.speed) { 271 case MAC_SPEED_10G: 272 link.link_speed = ETH_SPEED_NUM_10G; 273 break; 274 case MAC_SPEED_40G: 275 link.link_speed = ETH_SPEED_NUM_40G; 276 break; 277 case MAC_SPEED_100G: 278 link.link_speed = ETH_SPEED_NUM_100G; 279 break; 280 default: 281 link.link_speed = ETH_SPEED_NUM_NONE; 282 break; 283 } 284 } 285 286 for (i = 0; i < internals->max_rxmac; ++i) { 287 nc_rxmac_read_status(internals->rxmac[i], &status); 288 289 if (status.enabled && status.link_up) { 290 link.link_status = ETH_LINK_UP; 291 break; 292 } 293 } 294 295 rte_eth_linkstatus_set(dev, &link); 296 297 return 0; 298 } 299 300 /** 301 * DPDK callback to bring the link UP. 302 * 303 * @param dev 304 * Pointer to Ethernet device structure. 305 * 306 * @return 307 * 0 on success, a negative errno value otherwise. 308 */ 309 static int 310 nfb_eth_dev_set_link_up(struct rte_eth_dev *dev) 311 { 312 struct pmd_internals *internals = (struct pmd_internals *) 313 dev->data->dev_private; 314 315 uint16_t i; 316 for (i = 0; i < internals->max_rxmac; ++i) 317 nc_rxmac_enable(internals->rxmac[i]); 318 319 for (i = 0; i < internals->max_txmac; ++i) 320 nc_txmac_enable(internals->txmac[i]); 321 322 return 0; 323 } 324 325 /** 326 * DPDK callback to bring the link DOWN. 327 * 328 * @param dev 329 * Pointer to Ethernet device structure. 330 * 331 * @return 332 * 0 on success, a negative errno value otherwise. 333 */ 334 static int 335 nfb_eth_dev_set_link_down(struct rte_eth_dev *dev) 336 { 337 struct pmd_internals *internals = (struct pmd_internals *) 338 dev->data->dev_private; 339 340 uint16_t i; 341 for (i = 0; i < internals->max_rxmac; ++i) 342 nc_rxmac_disable(internals->rxmac[i]); 343 344 for (i = 0; i < internals->max_txmac; ++i) 345 nc_txmac_disable(internals->txmac[i]); 346 347 return 0; 348 } 349 350 /** 351 * DPDK callback to set primary MAC address. 352 * 353 * @param dev 354 * Pointer to Ethernet device structure. 355 * @param mac_addr 356 * MAC address to register. 357 * 358 * @return 359 * 0 on success, a negative errno value otherwise. 360 */ 361 static int 362 nfb_eth_mac_addr_set(struct rte_eth_dev *dev, 363 struct rte_ether_addr *mac_addr) 364 { 365 unsigned int i; 366 uint64_t mac = 0; 367 struct rte_eth_dev_data *data = dev->data; 368 struct pmd_internals *internals = (struct pmd_internals *) 369 data->dev_private; 370 371 if (!rte_is_valid_assigned_ether_addr(mac_addr)) 372 return -EINVAL; 373 374 for (i = 0; i < RTE_ETHER_ADDR_LEN; i++) { 375 mac <<= 8; 376 mac |= mac_addr->addr_bytes[i] & 0xFF; 377 } 378 379 for (i = 0; i < internals->max_rxmac; ++i) 380 nc_rxmac_set_mac(internals->rxmac[i], 0, mac, 1); 381 382 rte_ether_addr_copy(mac_addr, data->mac_addrs); 383 return 0; 384 } 385 386 static const struct eth_dev_ops ops = { 387 .dev_start = nfb_eth_dev_start, 388 .dev_stop = nfb_eth_dev_stop, 389 .dev_set_link_up = nfb_eth_dev_set_link_up, 390 .dev_set_link_down = nfb_eth_dev_set_link_down, 391 .dev_close = nfb_eth_dev_close, 392 .dev_configure = nfb_eth_dev_configure, 393 .dev_infos_get = nfb_eth_dev_info, 394 .promiscuous_enable = nfb_eth_promiscuous_enable, 395 .promiscuous_disable = nfb_eth_promiscuous_disable, 396 .allmulticast_enable = nfb_eth_allmulticast_enable, 397 .allmulticast_disable = nfb_eth_allmulticast_disable, 398 .rx_queue_start = nfb_eth_rx_queue_start, 399 .rx_queue_stop = nfb_eth_rx_queue_stop, 400 .tx_queue_start = nfb_eth_tx_queue_start, 401 .tx_queue_stop = nfb_eth_tx_queue_stop, 402 .rx_queue_setup = nfb_eth_rx_queue_setup, 403 .tx_queue_setup = nfb_eth_tx_queue_setup, 404 .rx_queue_release = nfb_eth_rx_queue_release, 405 .tx_queue_release = nfb_eth_tx_queue_release, 406 .link_update = nfb_eth_link_update, 407 .stats_get = nfb_eth_stats_get, 408 .stats_reset = nfb_eth_stats_reset, 409 .mac_addr_set = nfb_eth_mac_addr_set, 410 }; 411 412 /** 413 * DPDK callback to initialize an ethernet device 414 * 415 * @param dev 416 * Pointer to ethernet device structure 417 * 418 * @return 419 * 0 on success, a negative errno value otherwise. 420 */ 421 static int 422 nfb_eth_dev_init(struct rte_eth_dev *dev) 423 { 424 struct rte_eth_dev_data *data = dev->data; 425 struct pmd_internals *internals = (struct pmd_internals *) 426 data->dev_private; 427 struct rte_pci_device *pci_dev = RTE_ETH_DEV_TO_PCI(dev); 428 struct rte_pci_addr *pci_addr = &pci_dev->addr; 429 struct rte_ether_addr eth_addr_init; 430 struct rte_kvargs *kvlist; 431 432 RTE_LOG(INFO, PMD, "Initializing NFB device (" PCI_PRI_FMT ")\n", 433 pci_addr->domain, pci_addr->bus, pci_addr->devid, 434 pci_addr->function); 435 436 snprintf(internals->nfb_dev, PATH_MAX, 437 "/dev/nfb/by-pci-slot/" PCI_PRI_FMT, 438 pci_addr->domain, pci_addr->bus, pci_addr->devid, 439 pci_addr->function); 440 441 /* Check validity of device args */ 442 if (dev->device->devargs != NULL && 443 dev->device->devargs->args != NULL && 444 strlen(dev->device->devargs->args) > 0) { 445 kvlist = rte_kvargs_parse(dev->device->devargs->args, 446 VALID_KEYS); 447 if (kvlist == NULL) { 448 RTE_LOG(ERR, PMD, "Failed to parse device arguments %s", 449 dev->device->devargs->args); 450 rte_kvargs_free(kvlist); 451 return -EINVAL; 452 } 453 rte_kvargs_free(kvlist); 454 } 455 456 /* Let rte_eth_dev_close() release the port resources */ 457 dev->data->dev_flags |= RTE_ETH_DEV_CLOSE_REMOVE; 458 459 /* 460 * Get number of available DMA RX and TX queues, which is maximum 461 * number of queues that can be created and store it in private device 462 * data structure. 463 */ 464 internals->nfb = nfb_open(internals->nfb_dev); 465 if (internals->nfb == NULL) { 466 RTE_LOG(ERR, PMD, "nfb_open(): failed to open %s", 467 internals->nfb_dev); 468 return -EINVAL; 469 } 470 data->nb_rx_queues = ndp_get_rx_queue_available_count(internals->nfb); 471 data->nb_tx_queues = ndp_get_tx_queue_available_count(internals->nfb); 472 473 RTE_LOG(INFO, PMD, "Available NDP queues RX: %u TX: %u\n", 474 data->nb_rx_queues, data->nb_tx_queues); 475 476 nfb_nc_rxmac_init(internals->nfb, 477 internals->rxmac, 478 &internals->max_rxmac); 479 nfb_nc_txmac_init(internals->nfb, 480 internals->txmac, 481 &internals->max_txmac); 482 483 /* Set rx, tx burst functions */ 484 dev->rx_pkt_burst = nfb_eth_ndp_rx; 485 dev->tx_pkt_burst = nfb_eth_ndp_tx; 486 487 /* Set function callbacks for Ethernet API */ 488 dev->dev_ops = &ops; 489 490 /* Get link state */ 491 nfb_eth_link_update(dev, 0); 492 493 /* Allocate space for one mac address */ 494 data->mac_addrs = rte_zmalloc(data->name, sizeof(struct rte_ether_addr), 495 RTE_CACHE_LINE_SIZE); 496 if (data->mac_addrs == NULL) { 497 RTE_LOG(ERR, PMD, "Could not alloc space for MAC address!\n"); 498 nfb_close(internals->nfb); 499 return -EINVAL; 500 } 501 502 rte_eth_random_addr(eth_addr_init.addr_bytes); 503 eth_addr_init.addr_bytes[0] = eth_addr.addr_bytes[0]; 504 eth_addr_init.addr_bytes[1] = eth_addr.addr_bytes[1]; 505 eth_addr_init.addr_bytes[2] = eth_addr.addr_bytes[2]; 506 507 nfb_eth_mac_addr_set(dev, ð_addr_init); 508 509 data->promiscuous = nfb_eth_promiscuous_get(dev); 510 data->all_multicast = nfb_eth_allmulticast_get(dev); 511 internals->rx_filter_original = data->promiscuous; 512 513 RTE_LOG(INFO, PMD, "NFB device (" 514 PCI_PRI_FMT ") successfully initialized\n", 515 pci_addr->domain, pci_addr->bus, pci_addr->devid, 516 pci_addr->function); 517 518 return 0; 519 } 520 521 /** 522 * DPDK callback to uninitialize an ethernet device 523 * 524 * @param dev 525 * Pointer to ethernet device structure 526 * 527 * @return 528 * 0 on success, a negative errno value otherwise. 529 */ 530 static int 531 nfb_eth_dev_uninit(struct rte_eth_dev *dev) 532 { 533 struct rte_pci_device *pci_dev = RTE_ETH_DEV_TO_PCI(dev); 534 struct rte_pci_addr *pci_addr = &pci_dev->addr; 535 536 nfb_eth_dev_close(dev); 537 538 RTE_LOG(INFO, PMD, "NFB device (" 539 PCI_PRI_FMT ") successfully uninitialized\n", 540 pci_addr->domain, pci_addr->bus, pci_addr->devid, 541 pci_addr->function); 542 543 return 0; 544 } 545 546 static const struct rte_pci_id nfb_pci_id_table[] = { 547 { RTE_PCI_DEVICE(PCI_VENDOR_ID_NETCOPE, PCI_DEVICE_ID_NFB_40G2) }, 548 { RTE_PCI_DEVICE(PCI_VENDOR_ID_NETCOPE, PCI_DEVICE_ID_NFB_100G2) }, 549 { RTE_PCI_DEVICE(PCI_VENDOR_ID_NETCOPE, PCI_DEVICE_ID_NFB_200G2QL) }, 550 { RTE_PCI_DEVICE(PCI_VENDOR_ID_SILICOM, PCI_DEVICE_ID_FB2CGG3) }, 551 { RTE_PCI_DEVICE(PCI_VENDOR_ID_SILICOM, PCI_DEVICE_ID_FB2CGG3D) }, 552 { .vendor_id = 0, } 553 }; 554 555 /** 556 * DPDK callback to register a PCI device. 557 * 558 * This function spawns Ethernet devices out of a given PCI device. 559 * 560 * @param[in] pci_drv 561 * PCI driver structure (nfb_driver). 562 * @param[in] pci_dev 563 * PCI device information. 564 * 565 * @return 566 * 0 on success, a negative errno value otherwise. 567 */ 568 static int 569 nfb_eth_pci_probe(struct rte_pci_driver *pci_drv __rte_unused, 570 struct rte_pci_device *pci_dev) 571 { 572 return rte_eth_dev_pci_generic_probe(pci_dev, 573 sizeof(struct pmd_internals), nfb_eth_dev_init); 574 } 575 576 /** 577 * DPDK callback to remove a PCI device. 578 * 579 * This function removes all Ethernet devices belong to a given PCI device. 580 * 581 * @param[in] pci_dev 582 * Pointer to the PCI device. 583 * 584 * @return 585 * 0 on success, the function cannot fail. 586 */ 587 static int 588 nfb_eth_pci_remove(struct rte_pci_device *pci_dev) 589 { 590 return rte_eth_dev_pci_generic_remove(pci_dev, nfb_eth_dev_uninit); 591 } 592 593 static struct rte_pci_driver nfb_eth_driver = { 594 .id_table = nfb_pci_id_table, 595 .probe = nfb_eth_pci_probe, 596 .remove = nfb_eth_pci_remove, 597 }; 598 599 RTE_PMD_REGISTER_PCI(RTE_NFB_DRIVER_NAME, nfb_eth_driver); 600 RTE_PMD_REGISTER_PCI_TABLE(RTE_NFB_DRIVER_NAME, nfb_pci_id_table); 601 RTE_PMD_REGISTER_KMOD_DEP(RTE_NFB_DRIVER_NAME, "* nfb"); 602 RTE_PMD_REGISTER_PARAM_STRING(RTE_NFB_DRIVER_NAME, TIMESTAMP_ARG "=<0|1>"); 603