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