1 /* SPDX-License-Identifier: BSD-3-Clause 2 * 3 * Copyright(c) 2019-2021 Xilinx, Inc. 4 * Copyright(c) 2016-2019 Solarflare Communications Inc. 5 * 6 * This software was jointly developed between OKTET Labs (under contract 7 * for Solarflare) and Solarflare Communications, Inc. 8 */ 9 10 #include <rte_dev.h> 11 #include <ethdev_driver.h> 12 #include <ethdev_pci.h> 13 #include <rte_pci.h> 14 #include <rte_bus_pci.h> 15 #include <rte_errno.h> 16 #include <rte_string_fns.h> 17 #include <rte_ether.h> 18 19 #include "efx.h" 20 21 #include "sfc.h" 22 #include "sfc_debug.h" 23 #include "sfc_log.h" 24 #include "sfc_kvargs.h" 25 #include "sfc_ev.h" 26 #include "sfc_rx.h" 27 #include "sfc_tx.h" 28 #include "sfc_flow.h" 29 #include "sfc_dp.h" 30 #include "sfc_dp_rx.h" 31 #include "sfc_sw_stats.h" 32 33 #define SFC_XSTAT_ID_INVALID_VAL UINT64_MAX 34 #define SFC_XSTAT_ID_INVALID_NAME '\0' 35 36 uint32_t sfc_logtype_driver; 37 38 static struct sfc_dp_list sfc_dp_head = 39 TAILQ_HEAD_INITIALIZER(sfc_dp_head); 40 41 42 static void sfc_eth_dev_clear_ops(struct rte_eth_dev *dev); 43 44 45 static int 46 sfc_fw_version_get(struct rte_eth_dev *dev, char *fw_version, size_t fw_size) 47 { 48 struct sfc_adapter *sa = sfc_adapter_by_eth_dev(dev); 49 efx_nic_fw_info_t enfi; 50 int ret; 51 int rc; 52 53 rc = efx_nic_get_fw_version(sa->nic, &enfi); 54 if (rc != 0) 55 return -rc; 56 57 ret = snprintf(fw_version, fw_size, 58 "%" PRIu16 ".%" PRIu16 ".%" PRIu16 ".%" PRIu16, 59 enfi.enfi_mc_fw_version[0], enfi.enfi_mc_fw_version[1], 60 enfi.enfi_mc_fw_version[2], enfi.enfi_mc_fw_version[3]); 61 if (ret < 0) 62 return ret; 63 64 if (enfi.enfi_dpcpu_fw_ids_valid) { 65 size_t dpcpu_fw_ids_offset = MIN(fw_size - 1, (size_t)ret); 66 int ret_extra; 67 68 ret_extra = snprintf(fw_version + dpcpu_fw_ids_offset, 69 fw_size - dpcpu_fw_ids_offset, 70 " rx%" PRIx16 " tx%" PRIx16, 71 enfi.enfi_rx_dpcpu_fw_id, 72 enfi.enfi_tx_dpcpu_fw_id); 73 if (ret_extra < 0) 74 return ret_extra; 75 76 ret += ret_extra; 77 } 78 79 if (fw_size < (size_t)(++ret)) 80 return ret; 81 else 82 return 0; 83 } 84 85 static int 86 sfc_dev_infos_get(struct rte_eth_dev *dev, struct rte_eth_dev_info *dev_info) 87 { 88 const struct sfc_adapter_priv *sap = sfc_adapter_priv_by_eth_dev(dev); 89 struct sfc_adapter_shared *sas = sfc_adapter_shared_by_eth_dev(dev); 90 struct sfc_adapter *sa = sfc_adapter_by_eth_dev(dev); 91 struct sfc_rss *rss = &sas->rss; 92 struct sfc_mae *mae = &sa->mae; 93 uint64_t txq_offloads_def = 0; 94 95 sfc_log_init(sa, "entry"); 96 97 dev_info->min_mtu = RTE_ETHER_MIN_MTU; 98 dev_info->max_mtu = EFX_MAC_SDU_MAX; 99 100 dev_info->max_rx_pktlen = EFX_MAC_PDU_MAX; 101 102 dev_info->max_vfs = sa->sriov.num_vfs; 103 104 /* Autonegotiation may be disabled */ 105 dev_info->speed_capa = ETH_LINK_SPEED_FIXED; 106 if (sa->port.phy_adv_cap_mask & (1u << EFX_PHY_CAP_1000FDX)) 107 dev_info->speed_capa |= ETH_LINK_SPEED_1G; 108 if (sa->port.phy_adv_cap_mask & (1u << EFX_PHY_CAP_10000FDX)) 109 dev_info->speed_capa |= ETH_LINK_SPEED_10G; 110 if (sa->port.phy_adv_cap_mask & (1u << EFX_PHY_CAP_25000FDX)) 111 dev_info->speed_capa |= ETH_LINK_SPEED_25G; 112 if (sa->port.phy_adv_cap_mask & (1u << EFX_PHY_CAP_40000FDX)) 113 dev_info->speed_capa |= ETH_LINK_SPEED_40G; 114 if (sa->port.phy_adv_cap_mask & (1u << EFX_PHY_CAP_50000FDX)) 115 dev_info->speed_capa |= ETH_LINK_SPEED_50G; 116 if (sa->port.phy_adv_cap_mask & (1u << EFX_PHY_CAP_100000FDX)) 117 dev_info->speed_capa |= ETH_LINK_SPEED_100G; 118 119 dev_info->max_rx_queues = sa->rxq_max; 120 dev_info->max_tx_queues = sa->txq_max; 121 122 /* By default packets are dropped if no descriptors are available */ 123 dev_info->default_rxconf.rx_drop_en = 1; 124 125 dev_info->rx_queue_offload_capa = sfc_rx_get_queue_offload_caps(sa); 126 127 /* 128 * rx_offload_capa includes both device and queue offloads since 129 * the latter may be requested on a per device basis which makes 130 * sense when some offloads are needed to be set on all queues. 131 */ 132 dev_info->rx_offload_capa = sfc_rx_get_dev_offload_caps(sa) | 133 dev_info->rx_queue_offload_capa; 134 135 dev_info->tx_queue_offload_capa = sfc_tx_get_queue_offload_caps(sa); 136 137 /* 138 * tx_offload_capa includes both device and queue offloads since 139 * the latter may be requested on a per device basis which makes 140 * sense when some offloads are needed to be set on all queues. 141 */ 142 dev_info->tx_offload_capa = sfc_tx_get_dev_offload_caps(sa) | 143 dev_info->tx_queue_offload_capa; 144 145 if (dev_info->tx_offload_capa & DEV_TX_OFFLOAD_MBUF_FAST_FREE) 146 txq_offloads_def |= DEV_TX_OFFLOAD_MBUF_FAST_FREE; 147 148 dev_info->default_txconf.offloads |= txq_offloads_def; 149 150 if (rss->context_type != EFX_RX_SCALE_UNAVAILABLE) { 151 uint64_t rte_hf = 0; 152 unsigned int i; 153 154 for (i = 0; i < rss->hf_map_nb_entries; ++i) 155 rte_hf |= rss->hf_map[i].rte; 156 157 dev_info->reta_size = EFX_RSS_TBL_SIZE; 158 dev_info->hash_key_size = EFX_RSS_KEY_SIZE; 159 dev_info->flow_type_rss_offloads = rte_hf; 160 } 161 162 /* Initialize to hardware limits */ 163 dev_info->rx_desc_lim.nb_max = sa->rxq_max_entries; 164 dev_info->rx_desc_lim.nb_min = sa->rxq_min_entries; 165 /* The RXQ hardware requires that the descriptor count is a power 166 * of 2, but rx_desc_lim cannot properly describe that constraint. 167 */ 168 dev_info->rx_desc_lim.nb_align = sa->rxq_min_entries; 169 170 /* Initialize to hardware limits */ 171 dev_info->tx_desc_lim.nb_max = sa->txq_max_entries; 172 dev_info->tx_desc_lim.nb_min = sa->txq_min_entries; 173 /* 174 * The TXQ hardware requires that the descriptor count is a power 175 * of 2, but tx_desc_lim cannot properly describe that constraint 176 */ 177 dev_info->tx_desc_lim.nb_align = sa->txq_min_entries; 178 179 if (sap->dp_rx->get_dev_info != NULL) 180 sap->dp_rx->get_dev_info(dev_info); 181 if (sap->dp_tx->get_dev_info != NULL) 182 sap->dp_tx->get_dev_info(dev_info); 183 184 dev_info->dev_capa = RTE_ETH_DEV_CAPA_RUNTIME_RX_QUEUE_SETUP | 185 RTE_ETH_DEV_CAPA_RUNTIME_TX_QUEUE_SETUP; 186 187 if (mae->status == SFC_MAE_STATUS_SUPPORTED) { 188 dev_info->switch_info.name = dev->device->driver->name; 189 dev_info->switch_info.domain_id = mae->switch_domain_id; 190 dev_info->switch_info.port_id = mae->switch_port_id; 191 } 192 193 return 0; 194 } 195 196 static const uint32_t * 197 sfc_dev_supported_ptypes_get(struct rte_eth_dev *dev) 198 { 199 const struct sfc_adapter_priv *sap = sfc_adapter_priv_by_eth_dev(dev); 200 201 return sap->dp_rx->supported_ptypes_get(sap->shared->tunnel_encaps); 202 } 203 204 static int 205 sfc_dev_configure(struct rte_eth_dev *dev) 206 { 207 struct rte_eth_dev_data *dev_data = dev->data; 208 struct sfc_adapter *sa = sfc_adapter_by_eth_dev(dev); 209 int rc; 210 211 sfc_log_init(sa, "entry n_rxq=%u n_txq=%u", 212 dev_data->nb_rx_queues, dev_data->nb_tx_queues); 213 214 sfc_adapter_lock(sa); 215 switch (sa->state) { 216 case SFC_ADAPTER_CONFIGURED: 217 /* FALLTHROUGH */ 218 case SFC_ADAPTER_INITIALIZED: 219 rc = sfc_configure(sa); 220 break; 221 default: 222 sfc_err(sa, "unexpected adapter state %u to configure", 223 sa->state); 224 rc = EINVAL; 225 break; 226 } 227 sfc_adapter_unlock(sa); 228 229 sfc_log_init(sa, "done %d", rc); 230 SFC_ASSERT(rc >= 0); 231 return -rc; 232 } 233 234 static int 235 sfc_dev_start(struct rte_eth_dev *dev) 236 { 237 struct sfc_adapter *sa = sfc_adapter_by_eth_dev(dev); 238 int rc; 239 240 sfc_log_init(sa, "entry"); 241 242 sfc_adapter_lock(sa); 243 rc = sfc_start(sa); 244 sfc_adapter_unlock(sa); 245 246 sfc_log_init(sa, "done %d", rc); 247 SFC_ASSERT(rc >= 0); 248 return -rc; 249 } 250 251 static int 252 sfc_dev_link_update(struct rte_eth_dev *dev, int wait_to_complete) 253 { 254 struct sfc_adapter *sa = sfc_adapter_by_eth_dev(dev); 255 struct rte_eth_link current_link; 256 int ret; 257 258 sfc_log_init(sa, "entry"); 259 260 if (sa->state != SFC_ADAPTER_STARTED) { 261 sfc_port_link_mode_to_info(EFX_LINK_UNKNOWN, ¤t_link); 262 } else if (wait_to_complete) { 263 efx_link_mode_t link_mode; 264 265 if (efx_port_poll(sa->nic, &link_mode) != 0) 266 link_mode = EFX_LINK_UNKNOWN; 267 sfc_port_link_mode_to_info(link_mode, ¤t_link); 268 269 } else { 270 sfc_ev_mgmt_qpoll(sa); 271 rte_eth_linkstatus_get(dev, ¤t_link); 272 } 273 274 ret = rte_eth_linkstatus_set(dev, ¤t_link); 275 if (ret == 0) 276 sfc_notice(sa, "Link status is %s", 277 current_link.link_status ? "UP" : "DOWN"); 278 279 return ret; 280 } 281 282 static int 283 sfc_dev_stop(struct rte_eth_dev *dev) 284 { 285 struct sfc_adapter *sa = sfc_adapter_by_eth_dev(dev); 286 287 sfc_log_init(sa, "entry"); 288 289 sfc_adapter_lock(sa); 290 sfc_stop(sa); 291 sfc_adapter_unlock(sa); 292 293 sfc_log_init(sa, "done"); 294 295 return 0; 296 } 297 298 static int 299 sfc_dev_set_link_up(struct rte_eth_dev *dev) 300 { 301 struct sfc_adapter *sa = sfc_adapter_by_eth_dev(dev); 302 int rc; 303 304 sfc_log_init(sa, "entry"); 305 306 sfc_adapter_lock(sa); 307 rc = sfc_start(sa); 308 sfc_adapter_unlock(sa); 309 310 SFC_ASSERT(rc >= 0); 311 return -rc; 312 } 313 314 static int 315 sfc_dev_set_link_down(struct rte_eth_dev *dev) 316 { 317 struct sfc_adapter *sa = sfc_adapter_by_eth_dev(dev); 318 319 sfc_log_init(sa, "entry"); 320 321 sfc_adapter_lock(sa); 322 sfc_stop(sa); 323 sfc_adapter_unlock(sa); 324 325 return 0; 326 } 327 328 static void 329 sfc_eth_dev_secondary_clear_ops(struct rte_eth_dev *dev) 330 { 331 free(dev->process_private); 332 rte_eth_dev_release_port(dev); 333 } 334 335 static int 336 sfc_dev_close(struct rte_eth_dev *dev) 337 { 338 struct sfc_adapter *sa = sfc_adapter_by_eth_dev(dev); 339 340 sfc_log_init(sa, "entry"); 341 342 if (rte_eal_process_type() != RTE_PROC_PRIMARY) { 343 sfc_eth_dev_secondary_clear_ops(dev); 344 return 0; 345 } 346 347 sfc_adapter_lock(sa); 348 switch (sa->state) { 349 case SFC_ADAPTER_STARTED: 350 sfc_stop(sa); 351 SFC_ASSERT(sa->state == SFC_ADAPTER_CONFIGURED); 352 /* FALLTHROUGH */ 353 case SFC_ADAPTER_CONFIGURED: 354 sfc_close(sa); 355 SFC_ASSERT(sa->state == SFC_ADAPTER_INITIALIZED); 356 /* FALLTHROUGH */ 357 case SFC_ADAPTER_INITIALIZED: 358 break; 359 default: 360 sfc_err(sa, "unexpected adapter state %u on close", sa->state); 361 break; 362 } 363 364 /* 365 * Cleanup all resources. 366 * Rollback primary process sfc_eth_dev_init() below. 367 */ 368 369 sfc_eth_dev_clear_ops(dev); 370 371 sfc_detach(sa); 372 sfc_unprobe(sa); 373 374 sfc_kvargs_cleanup(sa); 375 376 sfc_adapter_unlock(sa); 377 sfc_adapter_lock_fini(sa); 378 379 sfc_log_init(sa, "done"); 380 381 /* Required for logging, so cleanup last */ 382 sa->eth_dev = NULL; 383 384 free(sa); 385 386 return 0; 387 } 388 389 static int 390 sfc_dev_filter_set(struct rte_eth_dev *dev, enum sfc_dev_filter_mode mode, 391 boolean_t enabled) 392 { 393 struct sfc_port *port; 394 boolean_t *toggle; 395 struct sfc_adapter *sa = sfc_adapter_by_eth_dev(dev); 396 boolean_t allmulti = (mode == SFC_DEV_FILTER_MODE_ALLMULTI); 397 const char *desc = (allmulti) ? "all-multi" : "promiscuous"; 398 int rc = 0; 399 400 sfc_adapter_lock(sa); 401 402 port = &sa->port; 403 toggle = (allmulti) ? (&port->allmulti) : (&port->promisc); 404 405 if (*toggle != enabled) { 406 *toggle = enabled; 407 408 if (sfc_sa2shared(sa)->isolated) { 409 sfc_warn(sa, "isolated mode is active on the port"); 410 sfc_warn(sa, "the change is to be applied on the next " 411 "start provided that isolated mode is " 412 "disabled prior the next start"); 413 } else if ((sa->state == SFC_ADAPTER_STARTED) && 414 ((rc = sfc_set_rx_mode(sa)) != 0)) { 415 *toggle = !(enabled); 416 sfc_warn(sa, "Failed to %s %s mode, rc = %d", 417 ((enabled) ? "enable" : "disable"), desc, rc); 418 419 /* 420 * For promiscuous and all-multicast filters a 421 * permission failure should be reported as an 422 * unsupported filter. 423 */ 424 if (rc == EPERM) 425 rc = ENOTSUP; 426 } 427 } 428 429 sfc_adapter_unlock(sa); 430 return rc; 431 } 432 433 static int 434 sfc_dev_promisc_enable(struct rte_eth_dev *dev) 435 { 436 int rc = sfc_dev_filter_set(dev, SFC_DEV_FILTER_MODE_PROMISC, B_TRUE); 437 438 SFC_ASSERT(rc >= 0); 439 return -rc; 440 } 441 442 static int 443 sfc_dev_promisc_disable(struct rte_eth_dev *dev) 444 { 445 int rc = sfc_dev_filter_set(dev, SFC_DEV_FILTER_MODE_PROMISC, B_FALSE); 446 447 SFC_ASSERT(rc >= 0); 448 return -rc; 449 } 450 451 static int 452 sfc_dev_allmulti_enable(struct rte_eth_dev *dev) 453 { 454 int rc = sfc_dev_filter_set(dev, SFC_DEV_FILTER_MODE_ALLMULTI, B_TRUE); 455 456 SFC_ASSERT(rc >= 0); 457 return -rc; 458 } 459 460 static int 461 sfc_dev_allmulti_disable(struct rte_eth_dev *dev) 462 { 463 int rc = sfc_dev_filter_set(dev, SFC_DEV_FILTER_MODE_ALLMULTI, B_FALSE); 464 465 SFC_ASSERT(rc >= 0); 466 return -rc; 467 } 468 469 static int 470 sfc_rx_queue_setup(struct rte_eth_dev *dev, uint16_t ethdev_qid, 471 uint16_t nb_rx_desc, unsigned int socket_id, 472 const struct rte_eth_rxconf *rx_conf, 473 struct rte_mempool *mb_pool) 474 { 475 struct sfc_adapter_shared *sas = sfc_adapter_shared_by_eth_dev(dev); 476 struct sfc_adapter *sa = sfc_adapter_by_eth_dev(dev); 477 sfc_ethdev_qid_t sfc_ethdev_qid = ethdev_qid; 478 struct sfc_rxq_info *rxq_info; 479 sfc_sw_index_t sw_index; 480 int rc; 481 482 sfc_log_init(sa, "RxQ=%u nb_rx_desc=%u socket_id=%u", 483 ethdev_qid, nb_rx_desc, socket_id); 484 485 sfc_adapter_lock(sa); 486 487 sw_index = sfc_rxq_sw_index_by_ethdev_rx_qid(sas, sfc_ethdev_qid); 488 rc = sfc_rx_qinit(sa, sw_index, nb_rx_desc, socket_id, 489 rx_conf, mb_pool); 490 if (rc != 0) 491 goto fail_rx_qinit; 492 493 rxq_info = sfc_rxq_info_by_ethdev_qid(sas, sfc_ethdev_qid); 494 dev->data->rx_queues[ethdev_qid] = rxq_info->dp; 495 496 sfc_adapter_unlock(sa); 497 498 return 0; 499 500 fail_rx_qinit: 501 sfc_adapter_unlock(sa); 502 SFC_ASSERT(rc > 0); 503 return -rc; 504 } 505 506 static void 507 sfc_rx_queue_release(void *queue) 508 { 509 struct sfc_dp_rxq *dp_rxq = queue; 510 struct sfc_rxq *rxq; 511 struct sfc_adapter *sa; 512 sfc_sw_index_t sw_index; 513 514 if (dp_rxq == NULL) 515 return; 516 517 rxq = sfc_rxq_by_dp_rxq(dp_rxq); 518 sa = rxq->evq->sa; 519 sfc_adapter_lock(sa); 520 521 sw_index = dp_rxq->dpq.queue_id; 522 523 sfc_log_init(sa, "RxQ=%u", sw_index); 524 525 sfc_rx_qfini(sa, sw_index); 526 527 sfc_adapter_unlock(sa); 528 } 529 530 static int 531 sfc_tx_queue_setup(struct rte_eth_dev *dev, uint16_t ethdev_qid, 532 uint16_t nb_tx_desc, unsigned int socket_id, 533 const struct rte_eth_txconf *tx_conf) 534 { 535 struct sfc_adapter_shared *sas = sfc_adapter_shared_by_eth_dev(dev); 536 struct sfc_adapter *sa = sfc_adapter_by_eth_dev(dev); 537 struct sfc_txq_info *txq_info; 538 sfc_sw_index_t sw_index; 539 int rc; 540 541 sfc_log_init(sa, "TxQ = %u, nb_tx_desc = %u, socket_id = %u", 542 ethdev_qid, nb_tx_desc, socket_id); 543 544 sfc_adapter_lock(sa); 545 546 sw_index = sfc_txq_sw_index_by_ethdev_tx_qid(sas, ethdev_qid); 547 rc = sfc_tx_qinit(sa, sw_index, nb_tx_desc, socket_id, tx_conf); 548 if (rc != 0) 549 goto fail_tx_qinit; 550 551 txq_info = sfc_txq_info_by_ethdev_qid(sas, ethdev_qid); 552 dev->data->tx_queues[ethdev_qid] = txq_info->dp; 553 554 sfc_adapter_unlock(sa); 555 return 0; 556 557 fail_tx_qinit: 558 sfc_adapter_unlock(sa); 559 SFC_ASSERT(rc > 0); 560 return -rc; 561 } 562 563 static void 564 sfc_tx_queue_release(void *queue) 565 { 566 struct sfc_dp_txq *dp_txq = queue; 567 struct sfc_txq *txq; 568 sfc_sw_index_t sw_index; 569 struct sfc_adapter *sa; 570 571 if (dp_txq == NULL) 572 return; 573 574 txq = sfc_txq_by_dp_txq(dp_txq); 575 sw_index = dp_txq->dpq.queue_id; 576 577 SFC_ASSERT(txq->evq != NULL); 578 sa = txq->evq->sa; 579 580 sfc_log_init(sa, "TxQ = %u", sw_index); 581 582 sfc_adapter_lock(sa); 583 584 sfc_tx_qfini(sa, sw_index); 585 586 sfc_adapter_unlock(sa); 587 } 588 589 /* 590 * Some statistics are computed as A - B where A and B each increase 591 * monotonically with some hardware counter(s) and the counters are read 592 * asynchronously. 593 * 594 * If packet X is counted in A, but not counted in B yet, computed value is 595 * greater than real. 596 * 597 * If packet X is not counted in A at the moment of reading the counter, 598 * but counted in B at the moment of reading the counter, computed value 599 * is less than real. 600 * 601 * However, counter which grows backward is worse evil than slightly wrong 602 * value. So, let's try to guarantee that it never happens except may be 603 * the case when the MAC stats are zeroed as a result of a NIC reset. 604 */ 605 static void 606 sfc_update_diff_stat(uint64_t *stat, uint64_t newval) 607 { 608 if ((int64_t)(newval - *stat) > 0 || newval == 0) 609 *stat = newval; 610 } 611 612 static int 613 sfc_stats_get(struct rte_eth_dev *dev, struct rte_eth_stats *stats) 614 { 615 struct sfc_adapter *sa = sfc_adapter_by_eth_dev(dev); 616 struct sfc_port *port = &sa->port; 617 uint64_t *mac_stats; 618 int ret; 619 620 sfc_adapter_lock(sa); 621 622 ret = sfc_port_update_mac_stats(sa, B_FALSE); 623 if (ret != 0) 624 goto unlock; 625 626 mac_stats = port->mac_stats_buf; 627 628 if (EFX_MAC_STAT_SUPPORTED(port->mac_stats_mask, 629 EFX_MAC_VADAPTER_RX_UNICAST_PACKETS)) { 630 stats->ipackets = 631 mac_stats[EFX_MAC_VADAPTER_RX_UNICAST_PACKETS] + 632 mac_stats[EFX_MAC_VADAPTER_RX_MULTICAST_PACKETS] + 633 mac_stats[EFX_MAC_VADAPTER_RX_BROADCAST_PACKETS]; 634 stats->opackets = 635 mac_stats[EFX_MAC_VADAPTER_TX_UNICAST_PACKETS] + 636 mac_stats[EFX_MAC_VADAPTER_TX_MULTICAST_PACKETS] + 637 mac_stats[EFX_MAC_VADAPTER_TX_BROADCAST_PACKETS]; 638 stats->ibytes = 639 mac_stats[EFX_MAC_VADAPTER_RX_UNICAST_BYTES] + 640 mac_stats[EFX_MAC_VADAPTER_RX_MULTICAST_BYTES] + 641 mac_stats[EFX_MAC_VADAPTER_RX_BROADCAST_BYTES]; 642 stats->obytes = 643 mac_stats[EFX_MAC_VADAPTER_TX_UNICAST_BYTES] + 644 mac_stats[EFX_MAC_VADAPTER_TX_MULTICAST_BYTES] + 645 mac_stats[EFX_MAC_VADAPTER_TX_BROADCAST_BYTES]; 646 stats->imissed = mac_stats[EFX_MAC_VADAPTER_RX_BAD_PACKETS]; 647 stats->oerrors = mac_stats[EFX_MAC_VADAPTER_TX_BAD_PACKETS]; 648 649 /* CRC is included in these stats, but shouldn't be */ 650 stats->ibytes -= stats->ipackets * RTE_ETHER_CRC_LEN; 651 stats->obytes -= stats->opackets * RTE_ETHER_CRC_LEN; 652 } else { 653 stats->opackets = mac_stats[EFX_MAC_TX_PKTS]; 654 stats->ibytes = mac_stats[EFX_MAC_RX_OCTETS]; 655 stats->obytes = mac_stats[EFX_MAC_TX_OCTETS]; 656 657 /* CRC is included in these stats, but shouldn't be */ 658 stats->ibytes -= mac_stats[EFX_MAC_RX_PKTS] * RTE_ETHER_CRC_LEN; 659 stats->obytes -= mac_stats[EFX_MAC_TX_PKTS] * RTE_ETHER_CRC_LEN; 660 661 /* 662 * Take into account stats which are whenever supported 663 * on EF10. If some stat is not supported by current 664 * firmware variant or HW revision, it is guaranteed 665 * to be zero in mac_stats. 666 */ 667 stats->imissed = 668 mac_stats[EFX_MAC_RX_NODESC_DROP_CNT] + 669 mac_stats[EFX_MAC_PM_TRUNC_BB_OVERFLOW] + 670 mac_stats[EFX_MAC_PM_DISCARD_BB_OVERFLOW] + 671 mac_stats[EFX_MAC_PM_TRUNC_VFIFO_FULL] + 672 mac_stats[EFX_MAC_PM_DISCARD_VFIFO_FULL] + 673 mac_stats[EFX_MAC_PM_TRUNC_QBB] + 674 mac_stats[EFX_MAC_PM_DISCARD_QBB] + 675 mac_stats[EFX_MAC_PM_DISCARD_MAPPING] + 676 mac_stats[EFX_MAC_RXDP_Q_DISABLED_PKTS] + 677 mac_stats[EFX_MAC_RXDP_DI_DROPPED_PKTS]; 678 stats->ierrors = 679 mac_stats[EFX_MAC_RX_FCS_ERRORS] + 680 mac_stats[EFX_MAC_RX_ALIGN_ERRORS] + 681 mac_stats[EFX_MAC_RX_JABBER_PKTS]; 682 /* no oerrors counters supported on EF10 */ 683 684 /* Exclude missed, errors and pauses from Rx packets */ 685 sfc_update_diff_stat(&port->ipackets, 686 mac_stats[EFX_MAC_RX_PKTS] - 687 mac_stats[EFX_MAC_RX_PAUSE_PKTS] - 688 stats->imissed - stats->ierrors); 689 stats->ipackets = port->ipackets; 690 } 691 692 unlock: 693 sfc_adapter_unlock(sa); 694 SFC_ASSERT(ret >= 0); 695 return -ret; 696 } 697 698 static int 699 sfc_stats_reset(struct rte_eth_dev *dev) 700 { 701 struct sfc_adapter *sa = sfc_adapter_by_eth_dev(dev); 702 struct sfc_port *port = &sa->port; 703 int rc; 704 705 sfc_adapter_lock(sa); 706 707 if (sa->state != SFC_ADAPTER_STARTED) { 708 /* 709 * The operation cannot be done if port is not started; it 710 * will be scheduled to be done during the next port start 711 */ 712 port->mac_stats_reset_pending = B_TRUE; 713 sfc_adapter_unlock(sa); 714 return 0; 715 } 716 717 rc = sfc_port_reset_mac_stats(sa); 718 if (rc != 0) 719 sfc_err(sa, "failed to reset statistics (rc = %d)", rc); 720 721 sfc_sw_xstats_reset(sa); 722 723 sfc_adapter_unlock(sa); 724 725 SFC_ASSERT(rc >= 0); 726 return -rc; 727 } 728 729 static unsigned int 730 sfc_xstats_get_nb_supported(struct sfc_adapter *sa) 731 { 732 struct sfc_port *port = &sa->port; 733 unsigned int nb_supported; 734 735 sfc_adapter_lock(sa); 736 nb_supported = port->mac_stats_nb_supported + 737 sfc_sw_xstats_get_nb_supported(sa); 738 sfc_adapter_unlock(sa); 739 740 return nb_supported; 741 } 742 743 static int 744 sfc_xstats_get(struct rte_eth_dev *dev, struct rte_eth_xstat *xstats, 745 unsigned int xstats_count) 746 { 747 struct sfc_adapter *sa = sfc_adapter_by_eth_dev(dev); 748 unsigned int nb_written = 0; 749 unsigned int nb_supported = 0; 750 int rc; 751 752 if (unlikely(xstats == NULL)) 753 return sfc_xstats_get_nb_supported(sa); 754 755 rc = sfc_port_get_mac_stats(sa, xstats, xstats_count, &nb_written); 756 if (rc < 0) 757 return rc; 758 759 nb_supported = rc; 760 sfc_sw_xstats_get_vals(sa, xstats, xstats_count, &nb_written, 761 &nb_supported); 762 763 return nb_supported; 764 } 765 766 static int 767 sfc_xstats_get_names(struct rte_eth_dev *dev, 768 struct rte_eth_xstat_name *xstats_names, 769 unsigned int xstats_count) 770 { 771 struct sfc_adapter *sa = sfc_adapter_by_eth_dev(dev); 772 struct sfc_port *port = &sa->port; 773 unsigned int i; 774 unsigned int nstats = 0; 775 unsigned int nb_written = 0; 776 int ret; 777 778 if (unlikely(xstats_names == NULL)) 779 return sfc_xstats_get_nb_supported(sa); 780 781 for (i = 0; i < EFX_MAC_NSTATS; ++i) { 782 if (EFX_MAC_STAT_SUPPORTED(port->mac_stats_mask, i)) { 783 if (nstats < xstats_count) { 784 strlcpy(xstats_names[nstats].name, 785 efx_mac_stat_name(sa->nic, i), 786 sizeof(xstats_names[0].name)); 787 nb_written++; 788 } 789 nstats++; 790 } 791 } 792 793 ret = sfc_sw_xstats_get_names(sa, xstats_names, xstats_count, 794 &nb_written, &nstats); 795 if (ret != 0) { 796 SFC_ASSERT(ret < 0); 797 return ret; 798 } 799 800 return nstats; 801 } 802 803 static int 804 sfc_xstats_get_by_id(struct rte_eth_dev *dev, const uint64_t *ids, 805 uint64_t *values, unsigned int n) 806 { 807 struct sfc_adapter *sa = sfc_adapter_by_eth_dev(dev); 808 struct sfc_port *port = &sa->port; 809 unsigned int nb_supported; 810 unsigned int i; 811 int rc; 812 813 if (unlikely(ids == NULL || values == NULL)) 814 return -EINVAL; 815 816 /* 817 * Values array could be filled in nonsequential order. Fill values with 818 * constant indicating invalid ID first. 819 */ 820 for (i = 0; i < n; i++) 821 values[i] = SFC_XSTAT_ID_INVALID_VAL; 822 823 rc = sfc_port_get_mac_stats_by_id(sa, ids, values, n); 824 if (rc != 0) 825 return rc; 826 827 nb_supported = port->mac_stats_nb_supported; 828 sfc_sw_xstats_get_vals_by_id(sa, ids, values, n, &nb_supported); 829 830 /* Return number of written stats before invalid ID is encountered. */ 831 for (i = 0; i < n; i++) { 832 if (values[i] == SFC_XSTAT_ID_INVALID_VAL) 833 return i; 834 } 835 836 return n; 837 } 838 839 static int 840 sfc_xstats_get_names_by_id(struct rte_eth_dev *dev, 841 struct rte_eth_xstat_name *xstats_names, 842 const uint64_t *ids, unsigned int size) 843 { 844 struct sfc_adapter *sa = sfc_adapter_by_eth_dev(dev); 845 struct sfc_port *port = &sa->port; 846 unsigned int nb_supported; 847 unsigned int i; 848 int ret; 849 850 if (unlikely(xstats_names == NULL && ids != NULL) || 851 unlikely(xstats_names != NULL && ids == NULL)) 852 return -EINVAL; 853 854 if (unlikely(xstats_names == NULL && ids == NULL)) 855 return sfc_xstats_get_nb_supported(sa); 856 857 /* 858 * Names array could be filled in nonsequential order. Fill names with 859 * string indicating invalid ID first. 860 */ 861 for (i = 0; i < size; i++) 862 xstats_names[i].name[0] = SFC_XSTAT_ID_INVALID_NAME; 863 864 sfc_adapter_lock(sa); 865 866 SFC_ASSERT(port->mac_stats_nb_supported <= 867 RTE_DIM(port->mac_stats_by_id)); 868 869 for (i = 0; i < size; i++) { 870 if (ids[i] < port->mac_stats_nb_supported) { 871 strlcpy(xstats_names[i].name, 872 efx_mac_stat_name(sa->nic, 873 port->mac_stats_by_id[ids[i]]), 874 sizeof(xstats_names[0].name)); 875 } 876 } 877 878 nb_supported = port->mac_stats_nb_supported; 879 880 sfc_adapter_unlock(sa); 881 882 ret = sfc_sw_xstats_get_names_by_id(sa, ids, xstats_names, size, 883 &nb_supported); 884 if (ret != 0) { 885 SFC_ASSERT(ret < 0); 886 return ret; 887 } 888 889 /* Return number of written names before invalid ID is encountered. */ 890 for (i = 0; i < size; i++) { 891 if (xstats_names[i].name[0] == SFC_XSTAT_ID_INVALID_NAME) 892 return i; 893 } 894 895 return size; 896 } 897 898 static int 899 sfc_flow_ctrl_get(struct rte_eth_dev *dev, struct rte_eth_fc_conf *fc_conf) 900 { 901 struct sfc_adapter *sa = sfc_adapter_by_eth_dev(dev); 902 unsigned int wanted_fc, link_fc; 903 904 memset(fc_conf, 0, sizeof(*fc_conf)); 905 906 sfc_adapter_lock(sa); 907 908 if (sa->state == SFC_ADAPTER_STARTED) 909 efx_mac_fcntl_get(sa->nic, &wanted_fc, &link_fc); 910 else 911 link_fc = sa->port.flow_ctrl; 912 913 switch (link_fc) { 914 case 0: 915 fc_conf->mode = RTE_FC_NONE; 916 break; 917 case EFX_FCNTL_RESPOND: 918 fc_conf->mode = RTE_FC_RX_PAUSE; 919 break; 920 case EFX_FCNTL_GENERATE: 921 fc_conf->mode = RTE_FC_TX_PAUSE; 922 break; 923 case (EFX_FCNTL_RESPOND | EFX_FCNTL_GENERATE): 924 fc_conf->mode = RTE_FC_FULL; 925 break; 926 default: 927 sfc_err(sa, "%s: unexpected flow control value %#x", 928 __func__, link_fc); 929 } 930 931 fc_conf->autoneg = sa->port.flow_ctrl_autoneg; 932 933 sfc_adapter_unlock(sa); 934 935 return 0; 936 } 937 938 static int 939 sfc_flow_ctrl_set(struct rte_eth_dev *dev, struct rte_eth_fc_conf *fc_conf) 940 { 941 struct sfc_adapter *sa = sfc_adapter_by_eth_dev(dev); 942 struct sfc_port *port = &sa->port; 943 unsigned int fcntl; 944 int rc; 945 946 if (fc_conf->high_water != 0 || fc_conf->low_water != 0 || 947 fc_conf->pause_time != 0 || fc_conf->send_xon != 0 || 948 fc_conf->mac_ctrl_frame_fwd != 0) { 949 sfc_err(sa, "unsupported flow control settings specified"); 950 rc = EINVAL; 951 goto fail_inval; 952 } 953 954 switch (fc_conf->mode) { 955 case RTE_FC_NONE: 956 fcntl = 0; 957 break; 958 case RTE_FC_RX_PAUSE: 959 fcntl = EFX_FCNTL_RESPOND; 960 break; 961 case RTE_FC_TX_PAUSE: 962 fcntl = EFX_FCNTL_GENERATE; 963 break; 964 case RTE_FC_FULL: 965 fcntl = EFX_FCNTL_RESPOND | EFX_FCNTL_GENERATE; 966 break; 967 default: 968 rc = EINVAL; 969 goto fail_inval; 970 } 971 972 sfc_adapter_lock(sa); 973 974 if (sa->state == SFC_ADAPTER_STARTED) { 975 rc = efx_mac_fcntl_set(sa->nic, fcntl, fc_conf->autoneg); 976 if (rc != 0) 977 goto fail_mac_fcntl_set; 978 } 979 980 port->flow_ctrl = fcntl; 981 port->flow_ctrl_autoneg = fc_conf->autoneg; 982 983 sfc_adapter_unlock(sa); 984 985 return 0; 986 987 fail_mac_fcntl_set: 988 sfc_adapter_unlock(sa); 989 fail_inval: 990 SFC_ASSERT(rc > 0); 991 return -rc; 992 } 993 994 static int 995 sfc_check_scatter_on_all_rx_queues(struct sfc_adapter *sa, size_t pdu) 996 { 997 struct sfc_adapter_shared * const sas = sfc_sa2shared(sa); 998 const efx_nic_cfg_t *encp = efx_nic_cfg_get(sa->nic); 999 boolean_t scatter_enabled; 1000 const char *error; 1001 unsigned int i; 1002 1003 for (i = 0; i < sas->rxq_count; i++) { 1004 if ((sas->rxq_info[i].state & SFC_RXQ_INITIALIZED) == 0) 1005 continue; 1006 1007 scatter_enabled = (sas->rxq_info[i].type_flags & 1008 EFX_RXQ_FLAG_SCATTER); 1009 1010 if (!sfc_rx_check_scatter(pdu, sa->rxq_ctrl[i].buf_size, 1011 encp->enc_rx_prefix_size, 1012 scatter_enabled, 1013 encp->enc_rx_scatter_max, &error)) { 1014 sfc_err(sa, "MTU check for RxQ %u failed: %s", i, 1015 error); 1016 return EINVAL; 1017 } 1018 } 1019 1020 return 0; 1021 } 1022 1023 static int 1024 sfc_dev_set_mtu(struct rte_eth_dev *dev, uint16_t mtu) 1025 { 1026 struct sfc_adapter *sa = sfc_adapter_by_eth_dev(dev); 1027 size_t pdu = EFX_MAC_PDU(mtu); 1028 size_t old_pdu; 1029 int rc; 1030 1031 sfc_log_init(sa, "mtu=%u", mtu); 1032 1033 rc = EINVAL; 1034 if (pdu < EFX_MAC_PDU_MIN) { 1035 sfc_err(sa, "too small MTU %u (PDU size %u less than min %u)", 1036 (unsigned int)mtu, (unsigned int)pdu, 1037 EFX_MAC_PDU_MIN); 1038 goto fail_inval; 1039 } 1040 if (pdu > EFX_MAC_PDU_MAX) { 1041 sfc_err(sa, "too big MTU %u (PDU size %u greater than max %u)", 1042 (unsigned int)mtu, (unsigned int)pdu, 1043 (unsigned int)EFX_MAC_PDU_MAX); 1044 goto fail_inval; 1045 } 1046 1047 sfc_adapter_lock(sa); 1048 1049 rc = sfc_check_scatter_on_all_rx_queues(sa, pdu); 1050 if (rc != 0) 1051 goto fail_check_scatter; 1052 1053 if (pdu != sa->port.pdu) { 1054 if (sa->state == SFC_ADAPTER_STARTED) { 1055 sfc_stop(sa); 1056 1057 old_pdu = sa->port.pdu; 1058 sa->port.pdu = pdu; 1059 rc = sfc_start(sa); 1060 if (rc != 0) 1061 goto fail_start; 1062 } else { 1063 sa->port.pdu = pdu; 1064 } 1065 } 1066 1067 /* 1068 * The driver does not use it, but other PMDs update jumbo frame 1069 * flag and max_rx_pkt_len when MTU is set. 1070 */ 1071 if (mtu > RTE_ETHER_MTU) { 1072 struct rte_eth_rxmode *rxmode = &dev->data->dev_conf.rxmode; 1073 rxmode->offloads |= DEV_RX_OFFLOAD_JUMBO_FRAME; 1074 } 1075 1076 dev->data->dev_conf.rxmode.max_rx_pkt_len = sa->port.pdu; 1077 1078 sfc_adapter_unlock(sa); 1079 1080 sfc_log_init(sa, "done"); 1081 return 0; 1082 1083 fail_start: 1084 sa->port.pdu = old_pdu; 1085 if (sfc_start(sa) != 0) 1086 sfc_err(sa, "cannot start with neither new (%u) nor old (%u) " 1087 "PDU max size - port is stopped", 1088 (unsigned int)pdu, (unsigned int)old_pdu); 1089 1090 fail_check_scatter: 1091 sfc_adapter_unlock(sa); 1092 1093 fail_inval: 1094 sfc_log_init(sa, "failed %d", rc); 1095 SFC_ASSERT(rc > 0); 1096 return -rc; 1097 } 1098 static int 1099 sfc_mac_addr_set(struct rte_eth_dev *dev, struct rte_ether_addr *mac_addr) 1100 { 1101 struct sfc_adapter *sa = sfc_adapter_by_eth_dev(dev); 1102 const efx_nic_cfg_t *encp = efx_nic_cfg_get(sa->nic); 1103 struct sfc_port *port = &sa->port; 1104 struct rte_ether_addr *old_addr = &dev->data->mac_addrs[0]; 1105 int rc = 0; 1106 1107 sfc_adapter_lock(sa); 1108 1109 if (rte_is_same_ether_addr(mac_addr, &port->default_mac_addr)) 1110 goto unlock; 1111 1112 /* 1113 * Copy the address to the device private data so that 1114 * it could be recalled in the case of adapter restart. 1115 */ 1116 rte_ether_addr_copy(mac_addr, &port->default_mac_addr); 1117 1118 /* 1119 * Neither of the two following checks can return 1120 * an error. The new MAC address is preserved in 1121 * the device private data and can be activated 1122 * on the next port start if the user prevents 1123 * isolated mode from being enabled. 1124 */ 1125 if (sfc_sa2shared(sa)->isolated) { 1126 sfc_warn(sa, "isolated mode is active on the port"); 1127 sfc_warn(sa, "will not set MAC address"); 1128 goto unlock; 1129 } 1130 1131 if (sa->state != SFC_ADAPTER_STARTED) { 1132 sfc_notice(sa, "the port is not started"); 1133 sfc_notice(sa, "the new MAC address will be set on port start"); 1134 1135 goto unlock; 1136 } 1137 1138 if (encp->enc_allow_set_mac_with_installed_filters) { 1139 rc = efx_mac_addr_set(sa->nic, mac_addr->addr_bytes); 1140 if (rc != 0) { 1141 sfc_err(sa, "cannot set MAC address (rc = %u)", rc); 1142 goto unlock; 1143 } 1144 1145 /* 1146 * Changing the MAC address by means of MCDI request 1147 * has no effect on received traffic, therefore 1148 * we also need to update unicast filters 1149 */ 1150 rc = sfc_set_rx_mode_unchecked(sa); 1151 if (rc != 0) { 1152 sfc_err(sa, "cannot set filter (rc = %u)", rc); 1153 /* Rollback the old address */ 1154 (void)efx_mac_addr_set(sa->nic, old_addr->addr_bytes); 1155 (void)sfc_set_rx_mode_unchecked(sa); 1156 } 1157 } else { 1158 sfc_warn(sa, "cannot set MAC address with filters installed"); 1159 sfc_warn(sa, "adapter will be restarted to pick the new MAC"); 1160 sfc_warn(sa, "(some traffic may be dropped)"); 1161 1162 /* 1163 * Since setting MAC address with filters installed is not 1164 * allowed on the adapter, the new MAC address will be set 1165 * by means of adapter restart. sfc_start() shall retrieve 1166 * the new address from the device private data and set it. 1167 */ 1168 sfc_stop(sa); 1169 rc = sfc_start(sa); 1170 if (rc != 0) 1171 sfc_err(sa, "cannot restart adapter (rc = %u)", rc); 1172 } 1173 1174 unlock: 1175 if (rc != 0) 1176 rte_ether_addr_copy(old_addr, &port->default_mac_addr); 1177 1178 sfc_adapter_unlock(sa); 1179 1180 SFC_ASSERT(rc >= 0); 1181 return -rc; 1182 } 1183 1184 1185 static int 1186 sfc_set_mc_addr_list(struct rte_eth_dev *dev, 1187 struct rte_ether_addr *mc_addr_set, uint32_t nb_mc_addr) 1188 { 1189 struct sfc_adapter *sa = sfc_adapter_by_eth_dev(dev); 1190 struct sfc_port *port = &sa->port; 1191 uint8_t *mc_addrs = port->mcast_addrs; 1192 int rc; 1193 unsigned int i; 1194 1195 if (sfc_sa2shared(sa)->isolated) { 1196 sfc_err(sa, "isolated mode is active on the port"); 1197 sfc_err(sa, "will not set multicast address list"); 1198 return -ENOTSUP; 1199 } 1200 1201 if (mc_addrs == NULL) 1202 return -ENOBUFS; 1203 1204 if (nb_mc_addr > port->max_mcast_addrs) { 1205 sfc_err(sa, "too many multicast addresses: %u > %u", 1206 nb_mc_addr, port->max_mcast_addrs); 1207 return -EINVAL; 1208 } 1209 1210 for (i = 0; i < nb_mc_addr; ++i) { 1211 rte_memcpy(mc_addrs, mc_addr_set[i].addr_bytes, 1212 EFX_MAC_ADDR_LEN); 1213 mc_addrs += EFX_MAC_ADDR_LEN; 1214 } 1215 1216 port->nb_mcast_addrs = nb_mc_addr; 1217 1218 if (sa->state != SFC_ADAPTER_STARTED) 1219 return 0; 1220 1221 rc = efx_mac_multicast_list_set(sa->nic, port->mcast_addrs, 1222 port->nb_mcast_addrs); 1223 if (rc != 0) 1224 sfc_err(sa, "cannot set multicast address list (rc = %u)", rc); 1225 1226 SFC_ASSERT(rc >= 0); 1227 return -rc; 1228 } 1229 1230 /* 1231 * The function is used by the secondary process as well. It must not 1232 * use any process-local pointers from the adapter data. 1233 */ 1234 static void 1235 sfc_rx_queue_info_get(struct rte_eth_dev *dev, uint16_t ethdev_qid, 1236 struct rte_eth_rxq_info *qinfo) 1237 { 1238 struct sfc_adapter_shared *sas = sfc_adapter_shared_by_eth_dev(dev); 1239 sfc_ethdev_qid_t sfc_ethdev_qid = ethdev_qid; 1240 struct sfc_rxq_info *rxq_info; 1241 1242 rxq_info = sfc_rxq_info_by_ethdev_qid(sas, sfc_ethdev_qid); 1243 1244 qinfo->mp = rxq_info->refill_mb_pool; 1245 qinfo->conf.rx_free_thresh = rxq_info->refill_threshold; 1246 qinfo->conf.rx_drop_en = 1; 1247 qinfo->conf.rx_deferred_start = rxq_info->deferred_start; 1248 qinfo->conf.offloads = dev->data->dev_conf.rxmode.offloads; 1249 if (rxq_info->type_flags & EFX_RXQ_FLAG_SCATTER) { 1250 qinfo->conf.offloads |= DEV_RX_OFFLOAD_SCATTER; 1251 qinfo->scattered_rx = 1; 1252 } 1253 qinfo->nb_desc = rxq_info->entries; 1254 } 1255 1256 /* 1257 * The function is used by the secondary process as well. It must not 1258 * use any process-local pointers from the adapter data. 1259 */ 1260 static void 1261 sfc_tx_queue_info_get(struct rte_eth_dev *dev, uint16_t ethdev_qid, 1262 struct rte_eth_txq_info *qinfo) 1263 { 1264 struct sfc_adapter_shared *sas = sfc_adapter_shared_by_eth_dev(dev); 1265 struct sfc_txq_info *txq_info; 1266 1267 SFC_ASSERT(ethdev_qid < sas->ethdev_txq_count); 1268 1269 txq_info = sfc_txq_info_by_ethdev_qid(sas, ethdev_qid); 1270 1271 memset(qinfo, 0, sizeof(*qinfo)); 1272 1273 qinfo->conf.offloads = txq_info->offloads; 1274 qinfo->conf.tx_free_thresh = txq_info->free_thresh; 1275 qinfo->conf.tx_deferred_start = txq_info->deferred_start; 1276 qinfo->nb_desc = txq_info->entries; 1277 } 1278 1279 /* 1280 * The function is used by the secondary process as well. It must not 1281 * use any process-local pointers from the adapter data. 1282 */ 1283 static uint32_t 1284 sfc_rx_queue_count(struct rte_eth_dev *dev, uint16_t ethdev_qid) 1285 { 1286 const struct sfc_adapter_priv *sap = sfc_adapter_priv_by_eth_dev(dev); 1287 struct sfc_adapter_shared *sas = sfc_adapter_shared_by_eth_dev(dev); 1288 sfc_ethdev_qid_t sfc_ethdev_qid = ethdev_qid; 1289 struct sfc_rxq_info *rxq_info; 1290 1291 rxq_info = sfc_rxq_info_by_ethdev_qid(sas, sfc_ethdev_qid); 1292 1293 if ((rxq_info->state & SFC_RXQ_STARTED) == 0) 1294 return 0; 1295 1296 return sap->dp_rx->qdesc_npending(rxq_info->dp); 1297 } 1298 1299 /* 1300 * The function is used by the secondary process as well. It must not 1301 * use any process-local pointers from the adapter data. 1302 */ 1303 static int 1304 sfc_rx_descriptor_done(void *queue, uint16_t offset) 1305 { 1306 struct sfc_dp_rxq *dp_rxq = queue; 1307 const struct sfc_dp_rx *dp_rx; 1308 1309 dp_rx = sfc_dp_rx_by_dp_rxq(dp_rxq); 1310 1311 return offset < dp_rx->qdesc_npending(dp_rxq); 1312 } 1313 1314 /* 1315 * The function is used by the secondary process as well. It must not 1316 * use any process-local pointers from the adapter data. 1317 */ 1318 static int 1319 sfc_rx_descriptor_status(void *queue, uint16_t offset) 1320 { 1321 struct sfc_dp_rxq *dp_rxq = queue; 1322 const struct sfc_dp_rx *dp_rx; 1323 1324 dp_rx = sfc_dp_rx_by_dp_rxq(dp_rxq); 1325 1326 return dp_rx->qdesc_status(dp_rxq, offset); 1327 } 1328 1329 /* 1330 * The function is used by the secondary process as well. It must not 1331 * use any process-local pointers from the adapter data. 1332 */ 1333 static int 1334 sfc_tx_descriptor_status(void *queue, uint16_t offset) 1335 { 1336 struct sfc_dp_txq *dp_txq = queue; 1337 const struct sfc_dp_tx *dp_tx; 1338 1339 dp_tx = sfc_dp_tx_by_dp_txq(dp_txq); 1340 1341 return dp_tx->qdesc_status(dp_txq, offset); 1342 } 1343 1344 static int 1345 sfc_rx_queue_start(struct rte_eth_dev *dev, uint16_t ethdev_qid) 1346 { 1347 struct sfc_adapter_shared *sas = sfc_adapter_shared_by_eth_dev(dev); 1348 struct sfc_adapter *sa = sfc_adapter_by_eth_dev(dev); 1349 sfc_ethdev_qid_t sfc_ethdev_qid = ethdev_qid; 1350 struct sfc_rxq_info *rxq_info; 1351 sfc_sw_index_t sw_index; 1352 int rc; 1353 1354 sfc_log_init(sa, "RxQ=%u", ethdev_qid); 1355 1356 sfc_adapter_lock(sa); 1357 1358 rc = EINVAL; 1359 if (sa->state != SFC_ADAPTER_STARTED) 1360 goto fail_not_started; 1361 1362 rxq_info = sfc_rxq_info_by_ethdev_qid(sas, sfc_ethdev_qid); 1363 if (rxq_info->state != SFC_RXQ_INITIALIZED) 1364 goto fail_not_setup; 1365 1366 sw_index = sfc_rxq_sw_index_by_ethdev_rx_qid(sas, sfc_ethdev_qid); 1367 rc = sfc_rx_qstart(sa, sw_index); 1368 if (rc != 0) 1369 goto fail_rx_qstart; 1370 1371 rxq_info->deferred_started = B_TRUE; 1372 1373 sfc_adapter_unlock(sa); 1374 1375 return 0; 1376 1377 fail_rx_qstart: 1378 fail_not_setup: 1379 fail_not_started: 1380 sfc_adapter_unlock(sa); 1381 SFC_ASSERT(rc > 0); 1382 return -rc; 1383 } 1384 1385 static int 1386 sfc_rx_queue_stop(struct rte_eth_dev *dev, uint16_t ethdev_qid) 1387 { 1388 struct sfc_adapter_shared *sas = sfc_adapter_shared_by_eth_dev(dev); 1389 struct sfc_adapter *sa = sfc_adapter_by_eth_dev(dev); 1390 sfc_ethdev_qid_t sfc_ethdev_qid = ethdev_qid; 1391 struct sfc_rxq_info *rxq_info; 1392 sfc_sw_index_t sw_index; 1393 1394 sfc_log_init(sa, "RxQ=%u", ethdev_qid); 1395 1396 sfc_adapter_lock(sa); 1397 1398 sw_index = sfc_rxq_sw_index_by_ethdev_rx_qid(sas, sfc_ethdev_qid); 1399 sfc_rx_qstop(sa, sw_index); 1400 1401 rxq_info = sfc_rxq_info_by_ethdev_qid(sas, sfc_ethdev_qid); 1402 rxq_info->deferred_started = B_FALSE; 1403 1404 sfc_adapter_unlock(sa); 1405 1406 return 0; 1407 } 1408 1409 static int 1410 sfc_tx_queue_start(struct rte_eth_dev *dev, uint16_t ethdev_qid) 1411 { 1412 struct sfc_adapter_shared *sas = sfc_adapter_shared_by_eth_dev(dev); 1413 struct sfc_adapter *sa = sfc_adapter_by_eth_dev(dev); 1414 struct sfc_txq_info *txq_info; 1415 sfc_sw_index_t sw_index; 1416 int rc; 1417 1418 sfc_log_init(sa, "TxQ = %u", ethdev_qid); 1419 1420 sfc_adapter_lock(sa); 1421 1422 rc = EINVAL; 1423 if (sa->state != SFC_ADAPTER_STARTED) 1424 goto fail_not_started; 1425 1426 txq_info = sfc_txq_info_by_ethdev_qid(sas, ethdev_qid); 1427 if (txq_info->state != SFC_TXQ_INITIALIZED) 1428 goto fail_not_setup; 1429 1430 sw_index = sfc_txq_sw_index_by_ethdev_tx_qid(sas, ethdev_qid); 1431 rc = sfc_tx_qstart(sa, sw_index); 1432 if (rc != 0) 1433 goto fail_tx_qstart; 1434 1435 txq_info->deferred_started = B_TRUE; 1436 1437 sfc_adapter_unlock(sa); 1438 return 0; 1439 1440 fail_tx_qstart: 1441 1442 fail_not_setup: 1443 fail_not_started: 1444 sfc_adapter_unlock(sa); 1445 SFC_ASSERT(rc > 0); 1446 return -rc; 1447 } 1448 1449 static int 1450 sfc_tx_queue_stop(struct rte_eth_dev *dev, uint16_t ethdev_qid) 1451 { 1452 struct sfc_adapter_shared *sas = sfc_adapter_shared_by_eth_dev(dev); 1453 struct sfc_adapter *sa = sfc_adapter_by_eth_dev(dev); 1454 struct sfc_txq_info *txq_info; 1455 sfc_sw_index_t sw_index; 1456 1457 sfc_log_init(sa, "TxQ = %u", ethdev_qid); 1458 1459 sfc_adapter_lock(sa); 1460 1461 sw_index = sfc_txq_sw_index_by_ethdev_tx_qid(sas, ethdev_qid); 1462 sfc_tx_qstop(sa, sw_index); 1463 1464 txq_info = sfc_txq_info_by_ethdev_qid(sas, ethdev_qid); 1465 txq_info->deferred_started = B_FALSE; 1466 1467 sfc_adapter_unlock(sa); 1468 return 0; 1469 } 1470 1471 static efx_tunnel_protocol_t 1472 sfc_tunnel_rte_type_to_efx_udp_proto(enum rte_eth_tunnel_type rte_type) 1473 { 1474 switch (rte_type) { 1475 case RTE_TUNNEL_TYPE_VXLAN: 1476 return EFX_TUNNEL_PROTOCOL_VXLAN; 1477 case RTE_TUNNEL_TYPE_GENEVE: 1478 return EFX_TUNNEL_PROTOCOL_GENEVE; 1479 default: 1480 return EFX_TUNNEL_NPROTOS; 1481 } 1482 } 1483 1484 enum sfc_udp_tunnel_op_e { 1485 SFC_UDP_TUNNEL_ADD_PORT, 1486 SFC_UDP_TUNNEL_DEL_PORT, 1487 }; 1488 1489 static int 1490 sfc_dev_udp_tunnel_op(struct rte_eth_dev *dev, 1491 struct rte_eth_udp_tunnel *tunnel_udp, 1492 enum sfc_udp_tunnel_op_e op) 1493 { 1494 struct sfc_adapter *sa = sfc_adapter_by_eth_dev(dev); 1495 efx_tunnel_protocol_t tunnel_proto; 1496 int rc; 1497 1498 sfc_log_init(sa, "%s udp_port=%u prot_type=%u", 1499 (op == SFC_UDP_TUNNEL_ADD_PORT) ? "add" : 1500 (op == SFC_UDP_TUNNEL_DEL_PORT) ? "delete" : "unknown", 1501 tunnel_udp->udp_port, tunnel_udp->prot_type); 1502 1503 tunnel_proto = 1504 sfc_tunnel_rte_type_to_efx_udp_proto(tunnel_udp->prot_type); 1505 if (tunnel_proto >= EFX_TUNNEL_NPROTOS) { 1506 rc = ENOTSUP; 1507 goto fail_bad_proto; 1508 } 1509 1510 sfc_adapter_lock(sa); 1511 1512 switch (op) { 1513 case SFC_UDP_TUNNEL_ADD_PORT: 1514 rc = efx_tunnel_config_udp_add(sa->nic, 1515 tunnel_udp->udp_port, 1516 tunnel_proto); 1517 break; 1518 case SFC_UDP_TUNNEL_DEL_PORT: 1519 rc = efx_tunnel_config_udp_remove(sa->nic, 1520 tunnel_udp->udp_port, 1521 tunnel_proto); 1522 break; 1523 default: 1524 rc = EINVAL; 1525 goto fail_bad_op; 1526 } 1527 1528 if (rc != 0) 1529 goto fail_op; 1530 1531 if (sa->state == SFC_ADAPTER_STARTED) { 1532 rc = efx_tunnel_reconfigure(sa->nic); 1533 if (rc == EAGAIN) { 1534 /* 1535 * Configuration is accepted by FW and MC reboot 1536 * is initiated to apply the changes. MC reboot 1537 * will be handled in a usual way (MC reboot 1538 * event on management event queue and adapter 1539 * restart). 1540 */ 1541 rc = 0; 1542 } else if (rc != 0) { 1543 goto fail_reconfigure; 1544 } 1545 } 1546 1547 sfc_adapter_unlock(sa); 1548 return 0; 1549 1550 fail_reconfigure: 1551 /* Remove/restore entry since the change makes the trouble */ 1552 switch (op) { 1553 case SFC_UDP_TUNNEL_ADD_PORT: 1554 (void)efx_tunnel_config_udp_remove(sa->nic, 1555 tunnel_udp->udp_port, 1556 tunnel_proto); 1557 break; 1558 case SFC_UDP_TUNNEL_DEL_PORT: 1559 (void)efx_tunnel_config_udp_add(sa->nic, 1560 tunnel_udp->udp_port, 1561 tunnel_proto); 1562 break; 1563 } 1564 1565 fail_op: 1566 fail_bad_op: 1567 sfc_adapter_unlock(sa); 1568 1569 fail_bad_proto: 1570 SFC_ASSERT(rc > 0); 1571 return -rc; 1572 } 1573 1574 static int 1575 sfc_dev_udp_tunnel_port_add(struct rte_eth_dev *dev, 1576 struct rte_eth_udp_tunnel *tunnel_udp) 1577 { 1578 return sfc_dev_udp_tunnel_op(dev, tunnel_udp, SFC_UDP_TUNNEL_ADD_PORT); 1579 } 1580 1581 static int 1582 sfc_dev_udp_tunnel_port_del(struct rte_eth_dev *dev, 1583 struct rte_eth_udp_tunnel *tunnel_udp) 1584 { 1585 return sfc_dev_udp_tunnel_op(dev, tunnel_udp, SFC_UDP_TUNNEL_DEL_PORT); 1586 } 1587 1588 /* 1589 * The function is used by the secondary process as well. It must not 1590 * use any process-local pointers from the adapter data. 1591 */ 1592 static int 1593 sfc_dev_rss_hash_conf_get(struct rte_eth_dev *dev, 1594 struct rte_eth_rss_conf *rss_conf) 1595 { 1596 struct sfc_adapter_shared *sas = sfc_adapter_shared_by_eth_dev(dev); 1597 struct sfc_rss *rss = &sas->rss; 1598 1599 if (rss->context_type != EFX_RX_SCALE_EXCLUSIVE) 1600 return -ENOTSUP; 1601 1602 /* 1603 * Mapping of hash configuration between RTE and EFX is not one-to-one, 1604 * hence, conversion is done here to derive a correct set of ETH_RSS 1605 * flags which corresponds to the active EFX configuration stored 1606 * locally in 'sfc_adapter' and kept up-to-date 1607 */ 1608 rss_conf->rss_hf = sfc_rx_hf_efx_to_rte(rss, rss->hash_types); 1609 rss_conf->rss_key_len = EFX_RSS_KEY_SIZE; 1610 if (rss_conf->rss_key != NULL) 1611 rte_memcpy(rss_conf->rss_key, rss->key, EFX_RSS_KEY_SIZE); 1612 1613 return 0; 1614 } 1615 1616 static int 1617 sfc_dev_rss_hash_update(struct rte_eth_dev *dev, 1618 struct rte_eth_rss_conf *rss_conf) 1619 { 1620 struct sfc_adapter *sa = sfc_adapter_by_eth_dev(dev); 1621 struct sfc_rss *rss = &sfc_sa2shared(sa)->rss; 1622 unsigned int efx_hash_types; 1623 uint32_t contexts[] = {EFX_RSS_CONTEXT_DEFAULT, rss->dummy_rss_context}; 1624 unsigned int n_contexts; 1625 unsigned int mode_i = 0; 1626 unsigned int key_i = 0; 1627 unsigned int i = 0; 1628 int rc = 0; 1629 1630 n_contexts = rss->dummy_rss_context == EFX_RSS_CONTEXT_DEFAULT ? 1 : 2; 1631 1632 if (sfc_sa2shared(sa)->isolated) 1633 return -ENOTSUP; 1634 1635 if (rss->context_type != EFX_RX_SCALE_EXCLUSIVE) { 1636 sfc_err(sa, "RSS is not available"); 1637 return -ENOTSUP; 1638 } 1639 1640 if (rss->channels == 0) { 1641 sfc_err(sa, "RSS is not configured"); 1642 return -EINVAL; 1643 } 1644 1645 if ((rss_conf->rss_key != NULL) && 1646 (rss_conf->rss_key_len != sizeof(rss->key))) { 1647 sfc_err(sa, "RSS key size is wrong (should be %zu)", 1648 sizeof(rss->key)); 1649 return -EINVAL; 1650 } 1651 1652 sfc_adapter_lock(sa); 1653 1654 rc = sfc_rx_hf_rte_to_efx(sa, rss_conf->rss_hf, &efx_hash_types); 1655 if (rc != 0) 1656 goto fail_rx_hf_rte_to_efx; 1657 1658 for (mode_i = 0; mode_i < n_contexts; mode_i++) { 1659 rc = efx_rx_scale_mode_set(sa->nic, contexts[mode_i], 1660 rss->hash_alg, efx_hash_types, 1661 B_TRUE); 1662 if (rc != 0) 1663 goto fail_scale_mode_set; 1664 } 1665 1666 if (rss_conf->rss_key != NULL) { 1667 if (sa->state == SFC_ADAPTER_STARTED) { 1668 for (key_i = 0; key_i < n_contexts; key_i++) { 1669 rc = efx_rx_scale_key_set(sa->nic, 1670 contexts[key_i], 1671 rss_conf->rss_key, 1672 sizeof(rss->key)); 1673 if (rc != 0) 1674 goto fail_scale_key_set; 1675 } 1676 } 1677 1678 rte_memcpy(rss->key, rss_conf->rss_key, sizeof(rss->key)); 1679 } 1680 1681 rss->hash_types = efx_hash_types; 1682 1683 sfc_adapter_unlock(sa); 1684 1685 return 0; 1686 1687 fail_scale_key_set: 1688 for (i = 0; i < key_i; i++) { 1689 if (efx_rx_scale_key_set(sa->nic, contexts[i], rss->key, 1690 sizeof(rss->key)) != 0) 1691 sfc_err(sa, "failed to restore RSS key"); 1692 } 1693 1694 fail_scale_mode_set: 1695 for (i = 0; i < mode_i; i++) { 1696 if (efx_rx_scale_mode_set(sa->nic, contexts[i], 1697 EFX_RX_HASHALG_TOEPLITZ, 1698 rss->hash_types, B_TRUE) != 0) 1699 sfc_err(sa, "failed to restore RSS mode"); 1700 } 1701 1702 fail_rx_hf_rte_to_efx: 1703 sfc_adapter_unlock(sa); 1704 return -rc; 1705 } 1706 1707 /* 1708 * The function is used by the secondary process as well. It must not 1709 * use any process-local pointers from the adapter data. 1710 */ 1711 static int 1712 sfc_dev_rss_reta_query(struct rte_eth_dev *dev, 1713 struct rte_eth_rss_reta_entry64 *reta_conf, 1714 uint16_t reta_size) 1715 { 1716 struct sfc_adapter_shared *sas = sfc_adapter_shared_by_eth_dev(dev); 1717 struct sfc_rss *rss = &sas->rss; 1718 int entry; 1719 1720 if (rss->context_type != EFX_RX_SCALE_EXCLUSIVE || sas->isolated) 1721 return -ENOTSUP; 1722 1723 if (rss->channels == 0) 1724 return -EINVAL; 1725 1726 if (reta_size != EFX_RSS_TBL_SIZE) 1727 return -EINVAL; 1728 1729 for (entry = 0; entry < reta_size; entry++) { 1730 int grp = entry / RTE_RETA_GROUP_SIZE; 1731 int grp_idx = entry % RTE_RETA_GROUP_SIZE; 1732 1733 if ((reta_conf[grp].mask >> grp_idx) & 1) 1734 reta_conf[grp].reta[grp_idx] = rss->tbl[entry]; 1735 } 1736 1737 return 0; 1738 } 1739 1740 static int 1741 sfc_dev_rss_reta_update(struct rte_eth_dev *dev, 1742 struct rte_eth_rss_reta_entry64 *reta_conf, 1743 uint16_t reta_size) 1744 { 1745 struct sfc_adapter *sa = sfc_adapter_by_eth_dev(dev); 1746 struct sfc_rss *rss = &sfc_sa2shared(sa)->rss; 1747 unsigned int *rss_tbl_new; 1748 uint16_t entry; 1749 int rc = 0; 1750 1751 1752 if (sfc_sa2shared(sa)->isolated) 1753 return -ENOTSUP; 1754 1755 if (rss->context_type != EFX_RX_SCALE_EXCLUSIVE) { 1756 sfc_err(sa, "RSS is not available"); 1757 return -ENOTSUP; 1758 } 1759 1760 if (rss->channels == 0) { 1761 sfc_err(sa, "RSS is not configured"); 1762 return -EINVAL; 1763 } 1764 1765 if (reta_size != EFX_RSS_TBL_SIZE) { 1766 sfc_err(sa, "RETA size is wrong (should be %u)", 1767 EFX_RSS_TBL_SIZE); 1768 return -EINVAL; 1769 } 1770 1771 rss_tbl_new = rte_zmalloc("rss_tbl_new", sizeof(rss->tbl), 0); 1772 if (rss_tbl_new == NULL) 1773 return -ENOMEM; 1774 1775 sfc_adapter_lock(sa); 1776 1777 rte_memcpy(rss_tbl_new, rss->tbl, sizeof(rss->tbl)); 1778 1779 for (entry = 0; entry < reta_size; entry++) { 1780 int grp_idx = entry % RTE_RETA_GROUP_SIZE; 1781 struct rte_eth_rss_reta_entry64 *grp; 1782 1783 grp = &reta_conf[entry / RTE_RETA_GROUP_SIZE]; 1784 1785 if (grp->mask & (1ull << grp_idx)) { 1786 if (grp->reta[grp_idx] >= rss->channels) { 1787 rc = EINVAL; 1788 goto bad_reta_entry; 1789 } 1790 rss_tbl_new[entry] = grp->reta[grp_idx]; 1791 } 1792 } 1793 1794 if (sa->state == SFC_ADAPTER_STARTED) { 1795 rc = efx_rx_scale_tbl_set(sa->nic, EFX_RSS_CONTEXT_DEFAULT, 1796 rss_tbl_new, EFX_RSS_TBL_SIZE); 1797 if (rc != 0) 1798 goto fail_scale_tbl_set; 1799 } 1800 1801 rte_memcpy(rss->tbl, rss_tbl_new, sizeof(rss->tbl)); 1802 1803 fail_scale_tbl_set: 1804 bad_reta_entry: 1805 sfc_adapter_unlock(sa); 1806 1807 rte_free(rss_tbl_new); 1808 1809 SFC_ASSERT(rc >= 0); 1810 return -rc; 1811 } 1812 1813 static int 1814 sfc_dev_flow_ops_get(struct rte_eth_dev *dev __rte_unused, 1815 const struct rte_flow_ops **ops) 1816 { 1817 *ops = &sfc_flow_ops; 1818 return 0; 1819 } 1820 1821 static int 1822 sfc_pool_ops_supported(struct rte_eth_dev *dev, const char *pool) 1823 { 1824 const struct sfc_adapter_priv *sap = sfc_adapter_priv_by_eth_dev(dev); 1825 1826 /* 1827 * If Rx datapath does not provide callback to check mempool, 1828 * all pools are supported. 1829 */ 1830 if (sap->dp_rx->pool_ops_supported == NULL) 1831 return 1; 1832 1833 return sap->dp_rx->pool_ops_supported(pool); 1834 } 1835 1836 static int 1837 sfc_rx_queue_intr_enable(struct rte_eth_dev *dev, uint16_t ethdev_qid) 1838 { 1839 const struct sfc_adapter_priv *sap = sfc_adapter_priv_by_eth_dev(dev); 1840 struct sfc_adapter_shared *sas = sfc_adapter_shared_by_eth_dev(dev); 1841 sfc_ethdev_qid_t sfc_ethdev_qid = ethdev_qid; 1842 struct sfc_rxq_info *rxq_info; 1843 1844 rxq_info = sfc_rxq_info_by_ethdev_qid(sas, sfc_ethdev_qid); 1845 1846 return sap->dp_rx->intr_enable(rxq_info->dp); 1847 } 1848 1849 static int 1850 sfc_rx_queue_intr_disable(struct rte_eth_dev *dev, uint16_t ethdev_qid) 1851 { 1852 const struct sfc_adapter_priv *sap = sfc_adapter_priv_by_eth_dev(dev); 1853 struct sfc_adapter_shared *sas = sfc_adapter_shared_by_eth_dev(dev); 1854 sfc_ethdev_qid_t sfc_ethdev_qid = ethdev_qid; 1855 struct sfc_rxq_info *rxq_info; 1856 1857 rxq_info = sfc_rxq_info_by_ethdev_qid(sas, sfc_ethdev_qid); 1858 1859 return sap->dp_rx->intr_disable(rxq_info->dp); 1860 } 1861 1862 static const struct eth_dev_ops sfc_eth_dev_ops = { 1863 .dev_configure = sfc_dev_configure, 1864 .dev_start = sfc_dev_start, 1865 .dev_stop = sfc_dev_stop, 1866 .dev_set_link_up = sfc_dev_set_link_up, 1867 .dev_set_link_down = sfc_dev_set_link_down, 1868 .dev_close = sfc_dev_close, 1869 .promiscuous_enable = sfc_dev_promisc_enable, 1870 .promiscuous_disable = sfc_dev_promisc_disable, 1871 .allmulticast_enable = sfc_dev_allmulti_enable, 1872 .allmulticast_disable = sfc_dev_allmulti_disable, 1873 .link_update = sfc_dev_link_update, 1874 .stats_get = sfc_stats_get, 1875 .stats_reset = sfc_stats_reset, 1876 .xstats_get = sfc_xstats_get, 1877 .xstats_reset = sfc_stats_reset, 1878 .xstats_get_names = sfc_xstats_get_names, 1879 .dev_infos_get = sfc_dev_infos_get, 1880 .dev_supported_ptypes_get = sfc_dev_supported_ptypes_get, 1881 .mtu_set = sfc_dev_set_mtu, 1882 .rx_queue_start = sfc_rx_queue_start, 1883 .rx_queue_stop = sfc_rx_queue_stop, 1884 .tx_queue_start = sfc_tx_queue_start, 1885 .tx_queue_stop = sfc_tx_queue_stop, 1886 .rx_queue_setup = sfc_rx_queue_setup, 1887 .rx_queue_release = sfc_rx_queue_release, 1888 .rx_queue_intr_enable = sfc_rx_queue_intr_enable, 1889 .rx_queue_intr_disable = sfc_rx_queue_intr_disable, 1890 .tx_queue_setup = sfc_tx_queue_setup, 1891 .tx_queue_release = sfc_tx_queue_release, 1892 .flow_ctrl_get = sfc_flow_ctrl_get, 1893 .flow_ctrl_set = sfc_flow_ctrl_set, 1894 .mac_addr_set = sfc_mac_addr_set, 1895 .udp_tunnel_port_add = sfc_dev_udp_tunnel_port_add, 1896 .udp_tunnel_port_del = sfc_dev_udp_tunnel_port_del, 1897 .reta_update = sfc_dev_rss_reta_update, 1898 .reta_query = sfc_dev_rss_reta_query, 1899 .rss_hash_update = sfc_dev_rss_hash_update, 1900 .rss_hash_conf_get = sfc_dev_rss_hash_conf_get, 1901 .flow_ops_get = sfc_dev_flow_ops_get, 1902 .set_mc_addr_list = sfc_set_mc_addr_list, 1903 .rxq_info_get = sfc_rx_queue_info_get, 1904 .txq_info_get = sfc_tx_queue_info_get, 1905 .fw_version_get = sfc_fw_version_get, 1906 .xstats_get_by_id = sfc_xstats_get_by_id, 1907 .xstats_get_names_by_id = sfc_xstats_get_names_by_id, 1908 .pool_ops_supported = sfc_pool_ops_supported, 1909 }; 1910 1911 /** 1912 * Duplicate a string in potentially shared memory required for 1913 * multi-process support. 1914 * 1915 * strdup() allocates from process-local heap/memory. 1916 */ 1917 static char * 1918 sfc_strdup(const char *str) 1919 { 1920 size_t size; 1921 char *copy; 1922 1923 if (str == NULL) 1924 return NULL; 1925 1926 size = strlen(str) + 1; 1927 copy = rte_malloc(__func__, size, 0); 1928 if (copy != NULL) 1929 rte_memcpy(copy, str, size); 1930 1931 return copy; 1932 } 1933 1934 static int 1935 sfc_eth_dev_set_ops(struct rte_eth_dev *dev) 1936 { 1937 struct sfc_adapter *sa = sfc_adapter_by_eth_dev(dev); 1938 struct sfc_adapter_shared *sas = sfc_adapter_shared_by_eth_dev(dev); 1939 const struct sfc_dp_rx *dp_rx; 1940 const struct sfc_dp_tx *dp_tx; 1941 const efx_nic_cfg_t *encp; 1942 unsigned int avail_caps = 0; 1943 const char *rx_name = NULL; 1944 const char *tx_name = NULL; 1945 int rc; 1946 1947 switch (sa->family) { 1948 case EFX_FAMILY_HUNTINGTON: 1949 case EFX_FAMILY_MEDFORD: 1950 case EFX_FAMILY_MEDFORD2: 1951 avail_caps |= SFC_DP_HW_FW_CAP_EF10; 1952 avail_caps |= SFC_DP_HW_FW_CAP_RX_EFX; 1953 avail_caps |= SFC_DP_HW_FW_CAP_TX_EFX; 1954 break; 1955 case EFX_FAMILY_RIVERHEAD: 1956 avail_caps |= SFC_DP_HW_FW_CAP_EF100; 1957 break; 1958 default: 1959 break; 1960 } 1961 1962 encp = efx_nic_cfg_get(sa->nic); 1963 if (encp->enc_rx_es_super_buffer_supported) 1964 avail_caps |= SFC_DP_HW_FW_CAP_RX_ES_SUPER_BUFFER; 1965 1966 rc = sfc_kvargs_process(sa, SFC_KVARG_RX_DATAPATH, 1967 sfc_kvarg_string_handler, &rx_name); 1968 if (rc != 0) 1969 goto fail_kvarg_rx_datapath; 1970 1971 if (rx_name != NULL) { 1972 dp_rx = sfc_dp_find_rx_by_name(&sfc_dp_head, rx_name); 1973 if (dp_rx == NULL) { 1974 sfc_err(sa, "Rx datapath %s not found", rx_name); 1975 rc = ENOENT; 1976 goto fail_dp_rx; 1977 } 1978 if (!sfc_dp_match_hw_fw_caps(&dp_rx->dp, avail_caps)) { 1979 sfc_err(sa, 1980 "Insufficient Hw/FW capabilities to use Rx datapath %s", 1981 rx_name); 1982 rc = EINVAL; 1983 goto fail_dp_rx_caps; 1984 } 1985 } else { 1986 dp_rx = sfc_dp_find_rx_by_caps(&sfc_dp_head, avail_caps); 1987 if (dp_rx == NULL) { 1988 sfc_err(sa, "Rx datapath by caps %#x not found", 1989 avail_caps); 1990 rc = ENOENT; 1991 goto fail_dp_rx; 1992 } 1993 } 1994 1995 sas->dp_rx_name = sfc_strdup(dp_rx->dp.name); 1996 if (sas->dp_rx_name == NULL) { 1997 rc = ENOMEM; 1998 goto fail_dp_rx_name; 1999 } 2000 2001 sfc_notice(sa, "use %s Rx datapath", sas->dp_rx_name); 2002 2003 rc = sfc_kvargs_process(sa, SFC_KVARG_TX_DATAPATH, 2004 sfc_kvarg_string_handler, &tx_name); 2005 if (rc != 0) 2006 goto fail_kvarg_tx_datapath; 2007 2008 if (tx_name != NULL) { 2009 dp_tx = sfc_dp_find_tx_by_name(&sfc_dp_head, tx_name); 2010 if (dp_tx == NULL) { 2011 sfc_err(sa, "Tx datapath %s not found", tx_name); 2012 rc = ENOENT; 2013 goto fail_dp_tx; 2014 } 2015 if (!sfc_dp_match_hw_fw_caps(&dp_tx->dp, avail_caps)) { 2016 sfc_err(sa, 2017 "Insufficient Hw/FW capabilities to use Tx datapath %s", 2018 tx_name); 2019 rc = EINVAL; 2020 goto fail_dp_tx_caps; 2021 } 2022 } else { 2023 dp_tx = sfc_dp_find_tx_by_caps(&sfc_dp_head, avail_caps); 2024 if (dp_tx == NULL) { 2025 sfc_err(sa, "Tx datapath by caps %#x not found", 2026 avail_caps); 2027 rc = ENOENT; 2028 goto fail_dp_tx; 2029 } 2030 } 2031 2032 sas->dp_tx_name = sfc_strdup(dp_tx->dp.name); 2033 if (sas->dp_tx_name == NULL) { 2034 rc = ENOMEM; 2035 goto fail_dp_tx_name; 2036 } 2037 2038 sfc_notice(sa, "use %s Tx datapath", sas->dp_tx_name); 2039 2040 sa->priv.dp_rx = dp_rx; 2041 sa->priv.dp_tx = dp_tx; 2042 2043 dev->rx_pkt_burst = dp_rx->pkt_burst; 2044 dev->tx_pkt_prepare = dp_tx->pkt_prepare; 2045 dev->tx_pkt_burst = dp_tx->pkt_burst; 2046 2047 dev->rx_queue_count = sfc_rx_queue_count; 2048 dev->rx_descriptor_done = sfc_rx_descriptor_done; 2049 dev->rx_descriptor_status = sfc_rx_descriptor_status; 2050 dev->tx_descriptor_status = sfc_tx_descriptor_status; 2051 dev->dev_ops = &sfc_eth_dev_ops; 2052 2053 return 0; 2054 2055 fail_dp_tx_name: 2056 fail_dp_tx_caps: 2057 fail_dp_tx: 2058 fail_kvarg_tx_datapath: 2059 rte_free(sas->dp_rx_name); 2060 sas->dp_rx_name = NULL; 2061 2062 fail_dp_rx_name: 2063 fail_dp_rx_caps: 2064 fail_dp_rx: 2065 fail_kvarg_rx_datapath: 2066 return rc; 2067 } 2068 2069 static void 2070 sfc_eth_dev_clear_ops(struct rte_eth_dev *dev) 2071 { 2072 struct sfc_adapter *sa = sfc_adapter_by_eth_dev(dev); 2073 struct sfc_adapter_shared *sas = sfc_adapter_shared_by_eth_dev(dev); 2074 2075 dev->dev_ops = NULL; 2076 dev->tx_pkt_prepare = NULL; 2077 dev->rx_pkt_burst = NULL; 2078 dev->tx_pkt_burst = NULL; 2079 2080 rte_free(sas->dp_tx_name); 2081 sas->dp_tx_name = NULL; 2082 sa->priv.dp_tx = NULL; 2083 2084 rte_free(sas->dp_rx_name); 2085 sas->dp_rx_name = NULL; 2086 sa->priv.dp_rx = NULL; 2087 } 2088 2089 static const struct eth_dev_ops sfc_eth_dev_secondary_ops = { 2090 .dev_supported_ptypes_get = sfc_dev_supported_ptypes_get, 2091 .reta_query = sfc_dev_rss_reta_query, 2092 .rss_hash_conf_get = sfc_dev_rss_hash_conf_get, 2093 .rxq_info_get = sfc_rx_queue_info_get, 2094 .txq_info_get = sfc_tx_queue_info_get, 2095 }; 2096 2097 static int 2098 sfc_eth_dev_secondary_init(struct rte_eth_dev *dev, uint32_t logtype_main) 2099 { 2100 struct sfc_adapter_shared *sas = sfc_adapter_shared_by_eth_dev(dev); 2101 struct sfc_adapter_priv *sap; 2102 const struct sfc_dp_rx *dp_rx; 2103 const struct sfc_dp_tx *dp_tx; 2104 int rc; 2105 2106 /* 2107 * Allocate process private data from heap, since it should not 2108 * be located in shared memory allocated using rte_malloc() API. 2109 */ 2110 sap = calloc(1, sizeof(*sap)); 2111 if (sap == NULL) { 2112 rc = ENOMEM; 2113 goto fail_alloc_priv; 2114 } 2115 2116 sap->logtype_main = logtype_main; 2117 2118 dp_rx = sfc_dp_find_rx_by_name(&sfc_dp_head, sas->dp_rx_name); 2119 if (dp_rx == NULL) { 2120 SFC_LOG(sas, RTE_LOG_ERR, logtype_main, 2121 "cannot find %s Rx datapath", sas->dp_rx_name); 2122 rc = ENOENT; 2123 goto fail_dp_rx; 2124 } 2125 if (~dp_rx->features & SFC_DP_RX_FEAT_MULTI_PROCESS) { 2126 SFC_LOG(sas, RTE_LOG_ERR, logtype_main, 2127 "%s Rx datapath does not support multi-process", 2128 sas->dp_rx_name); 2129 rc = EINVAL; 2130 goto fail_dp_rx_multi_process; 2131 } 2132 2133 dp_tx = sfc_dp_find_tx_by_name(&sfc_dp_head, sas->dp_tx_name); 2134 if (dp_tx == NULL) { 2135 SFC_LOG(sas, RTE_LOG_ERR, logtype_main, 2136 "cannot find %s Tx datapath", sas->dp_tx_name); 2137 rc = ENOENT; 2138 goto fail_dp_tx; 2139 } 2140 if (~dp_tx->features & SFC_DP_TX_FEAT_MULTI_PROCESS) { 2141 SFC_LOG(sas, RTE_LOG_ERR, logtype_main, 2142 "%s Tx datapath does not support multi-process", 2143 sas->dp_tx_name); 2144 rc = EINVAL; 2145 goto fail_dp_tx_multi_process; 2146 } 2147 2148 sap->dp_rx = dp_rx; 2149 sap->dp_tx = dp_tx; 2150 2151 dev->process_private = sap; 2152 dev->rx_pkt_burst = dp_rx->pkt_burst; 2153 dev->tx_pkt_prepare = dp_tx->pkt_prepare; 2154 dev->tx_pkt_burst = dp_tx->pkt_burst; 2155 dev->rx_queue_count = sfc_rx_queue_count; 2156 dev->rx_descriptor_done = sfc_rx_descriptor_done; 2157 dev->rx_descriptor_status = sfc_rx_descriptor_status; 2158 dev->tx_descriptor_status = sfc_tx_descriptor_status; 2159 dev->dev_ops = &sfc_eth_dev_secondary_ops; 2160 2161 return 0; 2162 2163 fail_dp_tx_multi_process: 2164 fail_dp_tx: 2165 fail_dp_rx_multi_process: 2166 fail_dp_rx: 2167 free(sap); 2168 2169 fail_alloc_priv: 2170 return rc; 2171 } 2172 2173 static void 2174 sfc_register_dp(void) 2175 { 2176 /* Register once */ 2177 if (TAILQ_EMPTY(&sfc_dp_head)) { 2178 /* Prefer EF10 datapath */ 2179 sfc_dp_register(&sfc_dp_head, &sfc_ef100_rx.dp); 2180 sfc_dp_register(&sfc_dp_head, &sfc_ef10_essb_rx.dp); 2181 sfc_dp_register(&sfc_dp_head, &sfc_ef10_rx.dp); 2182 sfc_dp_register(&sfc_dp_head, &sfc_efx_rx.dp); 2183 2184 sfc_dp_register(&sfc_dp_head, &sfc_ef100_tx.dp); 2185 sfc_dp_register(&sfc_dp_head, &sfc_ef10_tx.dp); 2186 sfc_dp_register(&sfc_dp_head, &sfc_efx_tx.dp); 2187 sfc_dp_register(&sfc_dp_head, &sfc_ef10_simple_tx.dp); 2188 } 2189 } 2190 2191 static int 2192 sfc_eth_dev_init(struct rte_eth_dev *dev) 2193 { 2194 struct sfc_adapter_shared *sas = sfc_adapter_shared_by_eth_dev(dev); 2195 struct rte_pci_device *pci_dev = RTE_ETH_DEV_TO_PCI(dev); 2196 uint32_t logtype_main; 2197 struct sfc_adapter *sa; 2198 int rc; 2199 const efx_nic_cfg_t *encp; 2200 const struct rte_ether_addr *from; 2201 int ret; 2202 2203 if (sfc_efx_dev_class_get(pci_dev->device.devargs) != 2204 SFC_EFX_DEV_CLASS_NET) { 2205 SFC_GENERIC_LOG(DEBUG, 2206 "Incompatible device class: skip probing, should be probed by other sfc driver."); 2207 return 1; 2208 } 2209 2210 sfc_register_dp(); 2211 2212 logtype_main = sfc_register_logtype(&pci_dev->addr, 2213 SFC_LOGTYPE_MAIN_STR, 2214 RTE_LOG_NOTICE); 2215 2216 if (rte_eal_process_type() != RTE_PROC_PRIMARY) 2217 return -sfc_eth_dev_secondary_init(dev, logtype_main); 2218 2219 /* Required for logging */ 2220 ret = snprintf(sas->log_prefix, sizeof(sas->log_prefix), 2221 "PMD: sfc_efx " PCI_PRI_FMT " #%" PRIu16 ": ", 2222 pci_dev->addr.domain, pci_dev->addr.bus, 2223 pci_dev->addr.devid, pci_dev->addr.function, 2224 dev->data->port_id); 2225 if (ret < 0 || ret >= (int)sizeof(sas->log_prefix)) { 2226 SFC_GENERIC_LOG(ERR, 2227 "reserved log prefix is too short for " PCI_PRI_FMT, 2228 pci_dev->addr.domain, pci_dev->addr.bus, 2229 pci_dev->addr.devid, pci_dev->addr.function); 2230 return -EINVAL; 2231 } 2232 sas->pci_addr = pci_dev->addr; 2233 sas->port_id = dev->data->port_id; 2234 2235 /* 2236 * Allocate process private data from heap, since it should not 2237 * be located in shared memory allocated using rte_malloc() API. 2238 */ 2239 sa = calloc(1, sizeof(*sa)); 2240 if (sa == NULL) { 2241 rc = ENOMEM; 2242 goto fail_alloc_sa; 2243 } 2244 2245 dev->process_private = sa; 2246 2247 /* Required for logging */ 2248 sa->priv.shared = sas; 2249 sa->priv.logtype_main = logtype_main; 2250 2251 sa->eth_dev = dev; 2252 2253 /* Copy PCI device info to the dev->data */ 2254 rte_eth_copy_pci_info(dev, pci_dev); 2255 dev->data->dev_flags |= RTE_ETH_DEV_AUTOFILL_QUEUE_XSTATS; 2256 dev->data->dev_flags |= RTE_ETH_DEV_FLOW_OPS_THREAD_SAFE; 2257 2258 rc = sfc_kvargs_parse(sa); 2259 if (rc != 0) 2260 goto fail_kvargs_parse; 2261 2262 sfc_log_init(sa, "entry"); 2263 2264 dev->data->mac_addrs = rte_zmalloc("sfc", RTE_ETHER_ADDR_LEN, 0); 2265 if (dev->data->mac_addrs == NULL) { 2266 rc = ENOMEM; 2267 goto fail_mac_addrs; 2268 } 2269 2270 sfc_adapter_lock_init(sa); 2271 sfc_adapter_lock(sa); 2272 2273 sfc_log_init(sa, "probing"); 2274 rc = sfc_probe(sa); 2275 if (rc != 0) 2276 goto fail_probe; 2277 2278 sfc_log_init(sa, "set device ops"); 2279 rc = sfc_eth_dev_set_ops(dev); 2280 if (rc != 0) 2281 goto fail_set_ops; 2282 2283 sfc_log_init(sa, "attaching"); 2284 rc = sfc_attach(sa); 2285 if (rc != 0) 2286 goto fail_attach; 2287 2288 encp = efx_nic_cfg_get(sa->nic); 2289 2290 /* 2291 * The arguments are really reverse order in comparison to 2292 * Linux kernel. Copy from NIC config to Ethernet device data. 2293 */ 2294 from = (const struct rte_ether_addr *)(encp->enc_mac_addr); 2295 rte_ether_addr_copy(from, &dev->data->mac_addrs[0]); 2296 2297 sfc_adapter_unlock(sa); 2298 2299 sfc_log_init(sa, "done"); 2300 return 0; 2301 2302 fail_attach: 2303 sfc_eth_dev_clear_ops(dev); 2304 2305 fail_set_ops: 2306 sfc_unprobe(sa); 2307 2308 fail_probe: 2309 sfc_adapter_unlock(sa); 2310 sfc_adapter_lock_fini(sa); 2311 rte_free(dev->data->mac_addrs); 2312 dev->data->mac_addrs = NULL; 2313 2314 fail_mac_addrs: 2315 sfc_kvargs_cleanup(sa); 2316 2317 fail_kvargs_parse: 2318 sfc_log_init(sa, "failed %d", rc); 2319 dev->process_private = NULL; 2320 free(sa); 2321 2322 fail_alloc_sa: 2323 SFC_ASSERT(rc > 0); 2324 return -rc; 2325 } 2326 2327 static int 2328 sfc_eth_dev_uninit(struct rte_eth_dev *dev) 2329 { 2330 sfc_dev_close(dev); 2331 2332 return 0; 2333 } 2334 2335 static const struct rte_pci_id pci_id_sfc_efx_map[] = { 2336 { RTE_PCI_DEVICE(EFX_PCI_VENID_SFC, EFX_PCI_DEVID_FARMINGDALE) }, 2337 { RTE_PCI_DEVICE(EFX_PCI_VENID_SFC, EFX_PCI_DEVID_FARMINGDALE_VF) }, 2338 { RTE_PCI_DEVICE(EFX_PCI_VENID_SFC, EFX_PCI_DEVID_GREENPORT) }, 2339 { RTE_PCI_DEVICE(EFX_PCI_VENID_SFC, EFX_PCI_DEVID_GREENPORT_VF) }, 2340 { RTE_PCI_DEVICE(EFX_PCI_VENID_SFC, EFX_PCI_DEVID_MEDFORD) }, 2341 { RTE_PCI_DEVICE(EFX_PCI_VENID_SFC, EFX_PCI_DEVID_MEDFORD_VF) }, 2342 { RTE_PCI_DEVICE(EFX_PCI_VENID_SFC, EFX_PCI_DEVID_MEDFORD2) }, 2343 { RTE_PCI_DEVICE(EFX_PCI_VENID_SFC, EFX_PCI_DEVID_MEDFORD2_VF) }, 2344 { RTE_PCI_DEVICE(EFX_PCI_VENID_XILINX, EFX_PCI_DEVID_RIVERHEAD) }, 2345 { .vendor_id = 0 /* sentinel */ } 2346 }; 2347 2348 static int sfc_eth_dev_pci_probe(struct rte_pci_driver *pci_drv __rte_unused, 2349 struct rte_pci_device *pci_dev) 2350 { 2351 return rte_eth_dev_pci_generic_probe(pci_dev, 2352 sizeof(struct sfc_adapter_shared), sfc_eth_dev_init); 2353 } 2354 2355 static int sfc_eth_dev_pci_remove(struct rte_pci_device *pci_dev) 2356 { 2357 return rte_eth_dev_pci_generic_remove(pci_dev, sfc_eth_dev_uninit); 2358 } 2359 2360 static struct rte_pci_driver sfc_efx_pmd = { 2361 .id_table = pci_id_sfc_efx_map, 2362 .drv_flags = 2363 RTE_PCI_DRV_INTR_LSC | 2364 RTE_PCI_DRV_NEED_MAPPING, 2365 .probe = sfc_eth_dev_pci_probe, 2366 .remove = sfc_eth_dev_pci_remove, 2367 }; 2368 2369 RTE_PMD_REGISTER_PCI(net_sfc_efx, sfc_efx_pmd); 2370 RTE_PMD_REGISTER_PCI_TABLE(net_sfc_efx, pci_id_sfc_efx_map); 2371 RTE_PMD_REGISTER_KMOD_DEP(net_sfc_efx, "* igb_uio | uio_pci_generic | vfio-pci"); 2372 RTE_PMD_REGISTER_PARAM_STRING(net_sfc_efx, 2373 SFC_KVARG_RX_DATAPATH "=" SFC_KVARG_VALUES_RX_DATAPATH " " 2374 SFC_KVARG_TX_DATAPATH "=" SFC_KVARG_VALUES_TX_DATAPATH " " 2375 SFC_KVARG_PERF_PROFILE "=" SFC_KVARG_VALUES_PERF_PROFILE " " 2376 SFC_KVARG_FW_VARIANT "=" SFC_KVARG_VALUES_FW_VARIANT " " 2377 SFC_KVARG_RXD_WAIT_TIMEOUT_NS "=<long> " 2378 SFC_KVARG_STATS_UPDATE_PERIOD_MS "=<long>"); 2379 2380 RTE_INIT(sfc_driver_register_logtype) 2381 { 2382 int ret; 2383 2384 ret = rte_log_register_type_and_pick_level(SFC_LOGTYPE_PREFIX "driver", 2385 RTE_LOG_NOTICE); 2386 sfc_logtype_driver = (ret < 0) ? RTE_LOGTYPE_PMD : ret; 2387 } 2388