1 /* SPDX-License-Identifier: BSD-3-Clause 2 * Copyright (c) 2013-2015 Brocade Communications Systems, Inc. 3 * Copyright (c) 2015-2018 Cavium Inc. 4 * All rights reserved. 5 * www.cavium.com 6 */ 7 8 #include "bnx2x.h" 9 #include "bnx2x_rxtx.h" 10 11 #include <rte_dev.h> 12 #include <rte_ethdev_pci.h> 13 #include <rte_alarm.h> 14 15 int bnx2x_logtype_init; 16 int bnx2x_logtype_driver; 17 18 /* 19 * The set of PCI devices this driver supports 20 */ 21 #define BROADCOM_PCI_VENDOR_ID 0x14E4 22 static const struct rte_pci_id pci_id_bnx2x_map[] = { 23 { RTE_PCI_DEVICE(BROADCOM_PCI_VENDOR_ID, CHIP_NUM_57800) }, 24 { RTE_PCI_DEVICE(BROADCOM_PCI_VENDOR_ID, CHIP_NUM_57711) }, 25 { RTE_PCI_DEVICE(BROADCOM_PCI_VENDOR_ID, CHIP_NUM_57810) }, 26 { RTE_PCI_DEVICE(BROADCOM_PCI_VENDOR_ID, CHIP_NUM_57811) }, 27 { RTE_PCI_DEVICE(BROADCOM_PCI_VENDOR_ID, CHIP_NUM_57840_OBS) }, 28 { RTE_PCI_DEVICE(BROADCOM_PCI_VENDOR_ID, CHIP_NUM_57840_4_10) }, 29 { RTE_PCI_DEVICE(BROADCOM_PCI_VENDOR_ID, CHIP_NUM_57840_2_20) }, 30 #ifdef RTE_LIBRTE_BNX2X_MF_SUPPORT 31 { RTE_PCI_DEVICE(BROADCOM_PCI_VENDOR_ID, CHIP_NUM_57810_MF) }, 32 { RTE_PCI_DEVICE(BROADCOM_PCI_VENDOR_ID, CHIP_NUM_57811_MF) }, 33 { RTE_PCI_DEVICE(BROADCOM_PCI_VENDOR_ID, CHIP_NUM_57840_MF) }, 34 #endif 35 { .vendor_id = 0, } 36 }; 37 38 static const struct rte_pci_id pci_id_bnx2xvf_map[] = { 39 { RTE_PCI_DEVICE(BROADCOM_PCI_VENDOR_ID, CHIP_NUM_57800_VF) }, 40 { RTE_PCI_DEVICE(BROADCOM_PCI_VENDOR_ID, CHIP_NUM_57810_VF) }, 41 { RTE_PCI_DEVICE(BROADCOM_PCI_VENDOR_ID, CHIP_NUM_57811_VF) }, 42 { RTE_PCI_DEVICE(BROADCOM_PCI_VENDOR_ID, CHIP_NUM_57840_VF) }, 43 { .vendor_id = 0, } 44 }; 45 46 struct rte_bnx2x_xstats_name_off { 47 char name[RTE_ETH_XSTATS_NAME_SIZE]; 48 uint32_t offset_hi; 49 uint32_t offset_lo; 50 }; 51 52 static const struct rte_bnx2x_xstats_name_off bnx2x_xstats_strings[] = { 53 {"rx_buffer_drops", 54 offsetof(struct bnx2x_eth_stats, brb_drop_hi), 55 offsetof(struct bnx2x_eth_stats, brb_drop_lo)}, 56 {"rx_buffer_truncates", 57 offsetof(struct bnx2x_eth_stats, brb_truncate_hi), 58 offsetof(struct bnx2x_eth_stats, brb_truncate_lo)}, 59 {"rx_buffer_truncate_discard", 60 offsetof(struct bnx2x_eth_stats, brb_truncate_discard), 61 offsetof(struct bnx2x_eth_stats, brb_truncate_discard)}, 62 {"mac_filter_discard", 63 offsetof(struct bnx2x_eth_stats, mac_filter_discard), 64 offsetof(struct bnx2x_eth_stats, mac_filter_discard)}, 65 {"no_match_vlan_tag_discard", 66 offsetof(struct bnx2x_eth_stats, mf_tag_discard), 67 offsetof(struct bnx2x_eth_stats, mf_tag_discard)}, 68 {"tx_pause", 69 offsetof(struct bnx2x_eth_stats, pause_frames_sent_hi), 70 offsetof(struct bnx2x_eth_stats, pause_frames_sent_lo)}, 71 {"rx_pause", 72 offsetof(struct bnx2x_eth_stats, pause_frames_received_hi), 73 offsetof(struct bnx2x_eth_stats, pause_frames_received_lo)}, 74 {"tx_priority_flow_control", 75 offsetof(struct bnx2x_eth_stats, pfc_frames_sent_hi), 76 offsetof(struct bnx2x_eth_stats, pfc_frames_sent_lo)}, 77 {"rx_priority_flow_control", 78 offsetof(struct bnx2x_eth_stats, pfc_frames_received_hi), 79 offsetof(struct bnx2x_eth_stats, pfc_frames_received_lo)} 80 }; 81 82 static int 83 bnx2x_link_update(struct rte_eth_dev *dev) 84 { 85 struct bnx2x_softc *sc = dev->data->dev_private; 86 struct rte_eth_link link; 87 88 PMD_INIT_FUNC_TRACE(sc); 89 90 memset(&link, 0, sizeof(link)); 91 mb(); 92 link.link_speed = sc->link_vars.line_speed; 93 switch (sc->link_vars.duplex) { 94 case DUPLEX_FULL: 95 link.link_duplex = ETH_LINK_FULL_DUPLEX; 96 break; 97 case DUPLEX_HALF: 98 link.link_duplex = ETH_LINK_HALF_DUPLEX; 99 break; 100 } 101 link.link_autoneg = !(dev->data->dev_conf.link_speeds & 102 ETH_LINK_SPEED_FIXED); 103 link.link_status = sc->link_vars.link_up; 104 105 return rte_eth_linkstatus_set(dev, &link); 106 } 107 108 static void 109 bnx2x_interrupt_action(struct rte_eth_dev *dev, int intr_cxt) 110 { 111 struct bnx2x_softc *sc = dev->data->dev_private; 112 uint32_t link_status; 113 114 bnx2x_intr_legacy(sc); 115 116 if ((atomic_load_acq_long(&sc->periodic_flags) == PERIODIC_GO) && 117 !intr_cxt) 118 bnx2x_periodic_callout(sc); 119 link_status = REG_RD(sc, sc->link_params.shmem_base + 120 offsetof(struct shmem_region, 121 port_mb[sc->link_params.port].link_status)); 122 if ((link_status & LINK_STATUS_LINK_UP) != dev->data->dev_link.link_status) 123 bnx2x_link_update(dev); 124 } 125 126 static void 127 bnx2x_interrupt_handler(void *param) 128 { 129 struct rte_eth_dev *dev = (struct rte_eth_dev *)param; 130 struct bnx2x_softc *sc = dev->data->dev_private; 131 132 PMD_DEBUG_PERIODIC_LOG(INFO, sc, "Interrupt handled"); 133 134 bnx2x_interrupt_action(dev, 1); 135 rte_intr_enable(&sc->pci_dev->intr_handle); 136 } 137 138 static void bnx2x_periodic_start(void *param) 139 { 140 struct rte_eth_dev *dev = (struct rte_eth_dev *)param; 141 struct bnx2x_softc *sc = dev->data->dev_private; 142 int ret = 0; 143 144 atomic_store_rel_long(&sc->periodic_flags, PERIODIC_GO); 145 bnx2x_interrupt_action(dev, 0); 146 if (IS_PF(sc)) { 147 ret = rte_eal_alarm_set(BNX2X_SP_TIMER_PERIOD, 148 bnx2x_periodic_start, (void *)dev); 149 if (ret) { 150 PMD_DRV_LOG(ERR, sc, "Unable to start periodic" 151 " timer rc %d", ret); 152 } 153 } 154 } 155 156 void bnx2x_periodic_stop(void *param) 157 { 158 struct rte_eth_dev *dev = (struct rte_eth_dev *)param; 159 struct bnx2x_softc *sc = dev->data->dev_private; 160 161 atomic_store_rel_long(&sc->periodic_flags, PERIODIC_STOP); 162 163 rte_eal_alarm_cancel(bnx2x_periodic_start, (void *)dev); 164 165 PMD_DRV_LOG(DEBUG, sc, "Periodic poll stopped"); 166 } 167 168 /* 169 * Devops - helper functions can be called from user application 170 */ 171 172 static int 173 bnx2x_dev_configure(struct rte_eth_dev *dev) 174 { 175 struct bnx2x_softc *sc = dev->data->dev_private; 176 struct rte_eth_rxmode *rxmode = &dev->data->dev_conf.rxmode; 177 178 int mp_ncpus = sysconf(_SC_NPROCESSORS_CONF); 179 180 PMD_INIT_FUNC_TRACE(sc); 181 182 if (rxmode->offloads & DEV_RX_OFFLOAD_JUMBO_FRAME) { 183 sc->mtu = dev->data->dev_conf.rxmode.max_rx_pkt_len; 184 dev->data->mtu = sc->mtu; 185 } 186 187 if (dev->data->nb_tx_queues > dev->data->nb_rx_queues) { 188 PMD_DRV_LOG(ERR, sc, "The number of TX queues is greater than number of RX queues"); 189 return -EINVAL; 190 } 191 192 sc->num_queues = MAX(dev->data->nb_rx_queues, dev->data->nb_tx_queues); 193 if (sc->num_queues > mp_ncpus) { 194 PMD_DRV_LOG(ERR, sc, "The number of queues is more than number of CPUs"); 195 return -EINVAL; 196 } 197 198 PMD_DRV_LOG(DEBUG, sc, "num_queues=%d, mtu=%d", 199 sc->num_queues, sc->mtu); 200 201 /* allocate ilt */ 202 if (bnx2x_alloc_ilt_mem(sc) != 0) { 203 PMD_DRV_LOG(ERR, sc, "bnx2x_alloc_ilt_mem was failed"); 204 return -ENXIO; 205 } 206 207 bnx2x_dev_rxtx_init_dummy(dev); 208 return 0; 209 } 210 211 static int 212 bnx2x_dev_start(struct rte_eth_dev *dev) 213 { 214 struct bnx2x_softc *sc = dev->data->dev_private; 215 int ret = 0; 216 217 PMD_INIT_FUNC_TRACE(sc); 218 219 /* start the periodic callout */ 220 if (IS_PF(sc)) { 221 if (atomic_load_acq_long(&sc->periodic_flags) == 222 PERIODIC_STOP) { 223 bnx2x_periodic_start(dev); 224 PMD_DRV_LOG(DEBUG, sc, "Periodic poll re-started"); 225 } 226 } 227 228 ret = bnx2x_init(sc); 229 if (ret) { 230 PMD_DRV_LOG(DEBUG, sc, "bnx2x_init failed (%d)", ret); 231 return -1; 232 } 233 234 if (IS_PF(sc)) { 235 rte_intr_callback_register(&sc->pci_dev->intr_handle, 236 bnx2x_interrupt_handler, (void *)dev); 237 238 if (rte_intr_enable(&sc->pci_dev->intr_handle)) 239 PMD_DRV_LOG(ERR, sc, "rte_intr_enable failed"); 240 } 241 242 bnx2x_dev_rxtx_init(dev); 243 244 bnx2x_print_device_info(sc); 245 246 return ret; 247 } 248 249 static void 250 bnx2x_dev_stop(struct rte_eth_dev *dev) 251 { 252 struct bnx2x_softc *sc = dev->data->dev_private; 253 int ret = 0; 254 255 PMD_INIT_FUNC_TRACE(sc); 256 257 bnx2x_dev_rxtx_init_dummy(dev); 258 259 if (IS_PF(sc)) { 260 rte_intr_disable(&sc->pci_dev->intr_handle); 261 rte_intr_callback_unregister(&sc->pci_dev->intr_handle, 262 bnx2x_interrupt_handler, (void *)dev); 263 264 /* stop the periodic callout */ 265 bnx2x_periodic_stop(dev); 266 } 267 268 ret = bnx2x_nic_unload(sc, UNLOAD_NORMAL, FALSE); 269 if (ret) { 270 PMD_DRV_LOG(DEBUG, sc, "bnx2x_nic_unload failed (%d)", ret); 271 return; 272 } 273 274 return; 275 } 276 277 static void 278 bnx2x_dev_close(struct rte_eth_dev *dev) 279 { 280 struct bnx2x_softc *sc = dev->data->dev_private; 281 282 PMD_INIT_FUNC_TRACE(sc); 283 284 if (IS_VF(sc)) 285 bnx2x_vf_close(sc); 286 287 bnx2x_dev_clear_queues(dev); 288 memset(&(dev->data->dev_link), 0 , sizeof(struct rte_eth_link)); 289 290 /* free ilt */ 291 bnx2x_free_ilt_mem(sc); 292 } 293 294 static void 295 bnx2x_promisc_enable(struct rte_eth_dev *dev) 296 { 297 struct bnx2x_softc *sc = dev->data->dev_private; 298 299 PMD_INIT_FUNC_TRACE(sc); 300 sc->rx_mode = BNX2X_RX_MODE_PROMISC; 301 if (rte_eth_allmulticast_get(dev->data->port_id) == 1) 302 sc->rx_mode = BNX2X_RX_MODE_ALLMULTI_PROMISC; 303 bnx2x_set_rx_mode(sc); 304 } 305 306 static void 307 bnx2x_promisc_disable(struct rte_eth_dev *dev) 308 { 309 struct bnx2x_softc *sc = dev->data->dev_private; 310 311 PMD_INIT_FUNC_TRACE(sc); 312 sc->rx_mode = BNX2X_RX_MODE_NORMAL; 313 if (rte_eth_allmulticast_get(dev->data->port_id) == 1) 314 sc->rx_mode = BNX2X_RX_MODE_ALLMULTI; 315 bnx2x_set_rx_mode(sc); 316 } 317 318 static void 319 bnx2x_dev_allmulticast_enable(struct rte_eth_dev *dev) 320 { 321 struct bnx2x_softc *sc = dev->data->dev_private; 322 323 PMD_INIT_FUNC_TRACE(sc); 324 sc->rx_mode = BNX2X_RX_MODE_ALLMULTI; 325 if (rte_eth_promiscuous_get(dev->data->port_id) == 1) 326 sc->rx_mode = BNX2X_RX_MODE_ALLMULTI_PROMISC; 327 bnx2x_set_rx_mode(sc); 328 } 329 330 static void 331 bnx2x_dev_allmulticast_disable(struct rte_eth_dev *dev) 332 { 333 struct bnx2x_softc *sc = dev->data->dev_private; 334 335 PMD_INIT_FUNC_TRACE(sc); 336 sc->rx_mode = BNX2X_RX_MODE_NORMAL; 337 if (rte_eth_promiscuous_get(dev->data->port_id) == 1) 338 sc->rx_mode = BNX2X_RX_MODE_PROMISC; 339 bnx2x_set_rx_mode(sc); 340 } 341 342 static int 343 bnx2x_dev_link_update(struct rte_eth_dev *dev, __rte_unused int wait_to_complete) 344 { 345 struct bnx2x_softc *sc = dev->data->dev_private; 346 347 PMD_INIT_FUNC_TRACE(sc); 348 349 return bnx2x_link_update(dev); 350 } 351 352 static int 353 bnx2xvf_dev_link_update(struct rte_eth_dev *dev, __rte_unused int wait_to_complete) 354 { 355 struct bnx2x_softc *sc = dev->data->dev_private; 356 int ret = 0; 357 358 ret = bnx2x_link_update(dev); 359 360 bnx2x_check_bull(sc); 361 if (sc->old_bulletin.valid_bitmap & (1 << CHANNEL_DOWN)) { 362 PMD_DRV_LOG(ERR, sc, "PF indicated channel is down." 363 "VF device is no longer operational"); 364 dev->data->dev_link.link_status = ETH_LINK_DOWN; 365 } 366 367 return ret; 368 } 369 370 static int 371 bnx2x_dev_stats_get(struct rte_eth_dev *dev, struct rte_eth_stats *stats) 372 { 373 struct bnx2x_softc *sc = dev->data->dev_private; 374 uint32_t brb_truncate_discard; 375 uint64_t brb_drops; 376 uint64_t brb_truncates; 377 378 PMD_INIT_FUNC_TRACE(sc); 379 380 bnx2x_stats_handle(sc, STATS_EVENT_UPDATE); 381 382 memset(stats, 0, sizeof (struct rte_eth_stats)); 383 384 stats->ipackets = 385 HILO_U64(sc->eth_stats.total_unicast_packets_received_hi, 386 sc->eth_stats.total_unicast_packets_received_lo) + 387 HILO_U64(sc->eth_stats.total_multicast_packets_received_hi, 388 sc->eth_stats.total_multicast_packets_received_lo) + 389 HILO_U64(sc->eth_stats.total_broadcast_packets_received_hi, 390 sc->eth_stats.total_broadcast_packets_received_lo); 391 392 stats->opackets = 393 HILO_U64(sc->eth_stats.total_unicast_packets_transmitted_hi, 394 sc->eth_stats.total_unicast_packets_transmitted_lo) + 395 HILO_U64(sc->eth_stats.total_multicast_packets_transmitted_hi, 396 sc->eth_stats.total_multicast_packets_transmitted_lo) + 397 HILO_U64(sc->eth_stats.total_broadcast_packets_transmitted_hi, 398 sc->eth_stats.total_broadcast_packets_transmitted_lo); 399 400 stats->ibytes = 401 HILO_U64(sc->eth_stats.total_bytes_received_hi, 402 sc->eth_stats.total_bytes_received_lo); 403 404 stats->obytes = 405 HILO_U64(sc->eth_stats.total_bytes_transmitted_hi, 406 sc->eth_stats.total_bytes_transmitted_lo); 407 408 stats->ierrors = 409 HILO_U64(sc->eth_stats.error_bytes_received_hi, 410 sc->eth_stats.error_bytes_received_lo); 411 412 stats->oerrors = 0; 413 414 stats->rx_nombuf = 415 HILO_U64(sc->eth_stats.no_buff_discard_hi, 416 sc->eth_stats.no_buff_discard_lo); 417 418 brb_drops = 419 HILO_U64(sc->eth_stats.brb_drop_hi, 420 sc->eth_stats.brb_drop_lo); 421 422 brb_truncates = 423 HILO_U64(sc->eth_stats.brb_truncate_hi, 424 sc->eth_stats.brb_truncate_lo); 425 426 brb_truncate_discard = sc->eth_stats.brb_truncate_discard; 427 428 stats->imissed = brb_drops + brb_truncates + 429 brb_truncate_discard + stats->rx_nombuf; 430 431 return 0; 432 } 433 434 static int 435 bnx2x_get_xstats_names(__rte_unused struct rte_eth_dev *dev, 436 struct rte_eth_xstat_name *xstats_names, 437 __rte_unused unsigned limit) 438 { 439 unsigned int i, stat_cnt = RTE_DIM(bnx2x_xstats_strings); 440 441 if (xstats_names != NULL) 442 for (i = 0; i < stat_cnt; i++) 443 snprintf(xstats_names[i].name, 444 sizeof(xstats_names[i].name), 445 "%s", 446 bnx2x_xstats_strings[i].name); 447 448 return stat_cnt; 449 } 450 451 static int 452 bnx2x_dev_xstats_get(struct rte_eth_dev *dev, struct rte_eth_xstat *xstats, 453 unsigned int n) 454 { 455 struct bnx2x_softc *sc = dev->data->dev_private; 456 unsigned int num = RTE_DIM(bnx2x_xstats_strings); 457 458 if (n < num) 459 return num; 460 461 bnx2x_stats_handle(sc, STATS_EVENT_UPDATE); 462 463 for (num = 0; num < n; num++) { 464 if (bnx2x_xstats_strings[num].offset_hi != 465 bnx2x_xstats_strings[num].offset_lo) 466 xstats[num].value = HILO_U64( 467 *(uint32_t *)((char *)&sc->eth_stats + 468 bnx2x_xstats_strings[num].offset_hi), 469 *(uint32_t *)((char *)&sc->eth_stats + 470 bnx2x_xstats_strings[num].offset_lo)); 471 else 472 xstats[num].value = 473 *(uint64_t *)((char *)&sc->eth_stats + 474 bnx2x_xstats_strings[num].offset_lo); 475 xstats[num].id = num; 476 } 477 478 return num; 479 } 480 481 static void 482 bnx2x_dev_infos_get(struct rte_eth_dev *dev, struct rte_eth_dev_info *dev_info) 483 { 484 struct bnx2x_softc *sc = dev->data->dev_private; 485 486 dev_info->max_rx_queues = sc->max_rx_queues; 487 dev_info->max_tx_queues = sc->max_tx_queues; 488 dev_info->min_rx_bufsize = BNX2X_MIN_RX_BUF_SIZE; 489 dev_info->max_rx_pktlen = BNX2X_MAX_RX_PKT_LEN; 490 dev_info->max_mac_addrs = BNX2X_MAX_MAC_ADDRS; 491 dev_info->speed_capa = ETH_LINK_SPEED_10G | ETH_LINK_SPEED_20G; 492 dev_info->rx_offload_capa = DEV_RX_OFFLOAD_JUMBO_FRAME; 493 494 dev_info->rx_desc_lim.nb_max = MAX_RX_AVAIL; 495 dev_info->rx_desc_lim.nb_min = MIN_RX_SIZE_NONTPA; 496 dev_info->tx_desc_lim.nb_max = MAX_TX_AVAIL; 497 } 498 499 static int 500 bnx2x_mac_addr_add(struct rte_eth_dev *dev, struct ether_addr *mac_addr, 501 uint32_t index, uint32_t pool) 502 { 503 struct bnx2x_softc *sc = dev->data->dev_private; 504 505 if (sc->mac_ops.mac_addr_add) { 506 sc->mac_ops.mac_addr_add(dev, mac_addr, index, pool); 507 return 0; 508 } 509 return -ENOTSUP; 510 } 511 512 static void 513 bnx2x_mac_addr_remove(struct rte_eth_dev *dev, uint32_t index) 514 { 515 struct bnx2x_softc *sc = dev->data->dev_private; 516 517 if (sc->mac_ops.mac_addr_remove) 518 sc->mac_ops.mac_addr_remove(dev, index); 519 } 520 521 static const struct eth_dev_ops bnx2x_eth_dev_ops = { 522 .dev_configure = bnx2x_dev_configure, 523 .dev_start = bnx2x_dev_start, 524 .dev_stop = bnx2x_dev_stop, 525 .dev_close = bnx2x_dev_close, 526 .promiscuous_enable = bnx2x_promisc_enable, 527 .promiscuous_disable = bnx2x_promisc_disable, 528 .allmulticast_enable = bnx2x_dev_allmulticast_enable, 529 .allmulticast_disable = bnx2x_dev_allmulticast_disable, 530 .link_update = bnx2x_dev_link_update, 531 .stats_get = bnx2x_dev_stats_get, 532 .xstats_get = bnx2x_dev_xstats_get, 533 .xstats_get_names = bnx2x_get_xstats_names, 534 .dev_infos_get = bnx2x_dev_infos_get, 535 .rx_queue_setup = bnx2x_dev_rx_queue_setup, 536 .rx_queue_release = bnx2x_dev_rx_queue_release, 537 .tx_queue_setup = bnx2x_dev_tx_queue_setup, 538 .tx_queue_release = bnx2x_dev_tx_queue_release, 539 .mac_addr_add = bnx2x_mac_addr_add, 540 .mac_addr_remove = bnx2x_mac_addr_remove, 541 }; 542 543 /* 544 * dev_ops for virtual function 545 */ 546 static const struct eth_dev_ops bnx2xvf_eth_dev_ops = { 547 .dev_configure = bnx2x_dev_configure, 548 .dev_start = bnx2x_dev_start, 549 .dev_stop = bnx2x_dev_stop, 550 .dev_close = bnx2x_dev_close, 551 .promiscuous_enable = bnx2x_promisc_enable, 552 .promiscuous_disable = bnx2x_promisc_disable, 553 .allmulticast_enable = bnx2x_dev_allmulticast_enable, 554 .allmulticast_disable = bnx2x_dev_allmulticast_disable, 555 .link_update = bnx2xvf_dev_link_update, 556 .stats_get = bnx2x_dev_stats_get, 557 .xstats_get = bnx2x_dev_xstats_get, 558 .xstats_get_names = bnx2x_get_xstats_names, 559 .dev_infos_get = bnx2x_dev_infos_get, 560 .rx_queue_setup = bnx2x_dev_rx_queue_setup, 561 .rx_queue_release = bnx2x_dev_rx_queue_release, 562 .tx_queue_setup = bnx2x_dev_tx_queue_setup, 563 .tx_queue_release = bnx2x_dev_tx_queue_release, 564 .mac_addr_add = bnx2x_mac_addr_add, 565 .mac_addr_remove = bnx2x_mac_addr_remove, 566 }; 567 568 569 static int 570 bnx2x_common_dev_init(struct rte_eth_dev *eth_dev, int is_vf) 571 { 572 int ret = 0; 573 struct rte_pci_device *pci_dev; 574 struct rte_pci_addr pci_addr; 575 struct bnx2x_softc *sc; 576 static bool adapter_info = true; 577 578 /* Extract key data structures */ 579 sc = eth_dev->data->dev_private; 580 pci_dev = RTE_DEV_TO_PCI(eth_dev->device); 581 pci_addr = pci_dev->addr; 582 583 snprintf(sc->devinfo.name, NAME_SIZE, PCI_SHORT_PRI_FMT ":dpdk-port-%u", 584 pci_addr.bus, pci_addr.devid, pci_addr.function, 585 eth_dev->data->port_id); 586 587 PMD_INIT_FUNC_TRACE(sc); 588 589 eth_dev->dev_ops = is_vf ? &bnx2xvf_eth_dev_ops : &bnx2x_eth_dev_ops; 590 591 rte_eth_copy_pci_info(eth_dev, pci_dev); 592 593 sc->pcie_bus = pci_dev->addr.bus; 594 sc->pcie_device = pci_dev->addr.devid; 595 596 sc->devinfo.vendor_id = pci_dev->id.vendor_id; 597 sc->devinfo.device_id = pci_dev->id.device_id; 598 sc->devinfo.subvendor_id = pci_dev->id.subsystem_vendor_id; 599 sc->devinfo.subdevice_id = pci_dev->id.subsystem_device_id; 600 601 if (is_vf) 602 sc->flags = BNX2X_IS_VF_FLAG; 603 604 sc->pcie_func = pci_dev->addr.function; 605 sc->bar[BAR0].base_addr = (void *)pci_dev->mem_resource[0].addr; 606 if (is_vf) 607 sc->bar[BAR1].base_addr = (void *) 608 ((uintptr_t)pci_dev->mem_resource[0].addr + PXP_VF_ADDR_DB_START); 609 else 610 sc->bar[BAR1].base_addr = pci_dev->mem_resource[2].addr; 611 612 assert(sc->bar[BAR0].base_addr); 613 assert(sc->bar[BAR1].base_addr); 614 615 bnx2x_load_firmware(sc); 616 assert(sc->firmware); 617 618 if (eth_dev->data->dev_conf.rx_adv_conf.rss_conf.rss_hf & ETH_RSS_NONFRAG_IPV4_UDP) 619 sc->udp_rss = 1; 620 621 sc->rx_budget = BNX2X_RX_BUDGET; 622 sc->hc_rx_ticks = BNX2X_RX_TICKS; 623 sc->hc_tx_ticks = BNX2X_TX_TICKS; 624 625 sc->interrupt_mode = INTR_MODE_SINGLE_MSIX; 626 sc->rx_mode = BNX2X_RX_MODE_NORMAL; 627 628 sc->pci_dev = pci_dev; 629 ret = bnx2x_attach(sc); 630 if (ret) { 631 PMD_DRV_LOG(ERR, sc, "bnx2x_attach failed (%d)", ret); 632 return ret; 633 } 634 635 /* Print important adapter info for the user. */ 636 if (adapter_info) { 637 bnx2x_print_adapter_info(sc); 638 adapter_info = false; 639 } 640 641 /* schedule periodic poll for slowpath link events */ 642 if (IS_PF(sc)) { 643 PMD_DRV_LOG(DEBUG, sc, "Scheduling periodic poll for slowpath link events"); 644 ret = rte_eal_alarm_set(BNX2X_SP_TIMER_PERIOD, 645 bnx2x_periodic_start, (void *)eth_dev); 646 if (ret) { 647 PMD_DRV_LOG(ERR, sc, "Unable to start periodic" 648 " timer rc %d", ret); 649 return -EINVAL; 650 } 651 } 652 653 eth_dev->data->mac_addrs = (struct ether_addr *)sc->link_params.mac_addr; 654 655 if (IS_VF(sc)) { 656 rte_spinlock_init(&sc->vf2pf_lock); 657 658 ret = bnx2x_dma_alloc(sc, sizeof(struct bnx2x_vf_mbx_msg), 659 &sc->vf2pf_mbox_mapping, "vf2pf_mbox", 660 RTE_CACHE_LINE_SIZE); 661 if (ret) 662 goto out; 663 664 sc->vf2pf_mbox = (struct bnx2x_vf_mbx_msg *) 665 sc->vf2pf_mbox_mapping.vaddr; 666 667 ret = bnx2x_dma_alloc(sc, sizeof(struct bnx2x_vf_bulletin), 668 &sc->pf2vf_bulletin_mapping, "vf2pf_bull", 669 RTE_CACHE_LINE_SIZE); 670 if (ret) 671 goto out; 672 673 sc->pf2vf_bulletin = (struct bnx2x_vf_bulletin *) 674 sc->pf2vf_bulletin_mapping.vaddr; 675 676 ret = bnx2x_vf_get_resources(sc, sc->max_tx_queues, 677 sc->max_rx_queues); 678 if (ret) 679 goto out; 680 } 681 682 return 0; 683 684 out: 685 if (IS_PF(sc)) 686 bnx2x_periodic_stop(eth_dev); 687 688 return ret; 689 } 690 691 static int 692 eth_bnx2x_dev_init(struct rte_eth_dev *eth_dev) 693 { 694 struct bnx2x_softc *sc = eth_dev->data->dev_private; 695 PMD_INIT_FUNC_TRACE(sc); 696 return bnx2x_common_dev_init(eth_dev, 0); 697 } 698 699 static int 700 eth_bnx2xvf_dev_init(struct rte_eth_dev *eth_dev) 701 { 702 struct bnx2x_softc *sc = eth_dev->data->dev_private; 703 PMD_INIT_FUNC_TRACE(sc); 704 return bnx2x_common_dev_init(eth_dev, 1); 705 } 706 707 static int eth_bnx2x_dev_uninit(struct rte_eth_dev *eth_dev) 708 { 709 /* mac_addrs must not be freed alone because part of dev_private */ 710 eth_dev->data->mac_addrs = NULL; 711 return 0; 712 } 713 714 static struct rte_pci_driver rte_bnx2x_pmd; 715 static struct rte_pci_driver rte_bnx2xvf_pmd; 716 717 static int eth_bnx2x_pci_probe(struct rte_pci_driver *pci_drv, 718 struct rte_pci_device *pci_dev) 719 { 720 if (pci_drv == &rte_bnx2x_pmd) 721 return rte_eth_dev_pci_generic_probe(pci_dev, 722 sizeof(struct bnx2x_softc), eth_bnx2x_dev_init); 723 else if (pci_drv == &rte_bnx2xvf_pmd) 724 return rte_eth_dev_pci_generic_probe(pci_dev, 725 sizeof(struct bnx2x_softc), eth_bnx2xvf_dev_init); 726 else 727 return -EINVAL; 728 } 729 730 static int eth_bnx2x_pci_remove(struct rte_pci_device *pci_dev) 731 { 732 return rte_eth_dev_pci_generic_remove(pci_dev, eth_bnx2x_dev_uninit); 733 } 734 735 static struct rte_pci_driver rte_bnx2x_pmd = { 736 .id_table = pci_id_bnx2x_map, 737 .drv_flags = RTE_PCI_DRV_NEED_MAPPING | RTE_PCI_DRV_INTR_LSC, 738 .probe = eth_bnx2x_pci_probe, 739 .remove = eth_bnx2x_pci_remove, 740 }; 741 742 /* 743 * virtual function driver struct 744 */ 745 static struct rte_pci_driver rte_bnx2xvf_pmd = { 746 .id_table = pci_id_bnx2xvf_map, 747 .drv_flags = RTE_PCI_DRV_NEED_MAPPING, 748 .probe = eth_bnx2x_pci_probe, 749 .remove = eth_bnx2x_pci_remove, 750 }; 751 752 RTE_PMD_REGISTER_PCI(net_bnx2x, rte_bnx2x_pmd); 753 RTE_PMD_REGISTER_PCI_TABLE(net_bnx2x, pci_id_bnx2x_map); 754 RTE_PMD_REGISTER_KMOD_DEP(net_bnx2x, "* igb_uio | uio_pci_generic | vfio-pci"); 755 RTE_PMD_REGISTER_PCI(net_bnx2xvf, rte_bnx2xvf_pmd); 756 RTE_PMD_REGISTER_PCI_TABLE(net_bnx2xvf, pci_id_bnx2xvf_map); 757 RTE_PMD_REGISTER_KMOD_DEP(net_bnx2xvf, "* igb_uio | vfio-pci"); 758 759 RTE_INIT(bnx2x_init_log) 760 { 761 bnx2x_logtype_init = rte_log_register("pmd.net.bnx2x.init"); 762 if (bnx2x_logtype_init >= 0) 763 rte_log_set_level(bnx2x_logtype_init, RTE_LOG_NOTICE); 764 bnx2x_logtype_driver = rte_log_register("pmd.net.bnx2x.driver"); 765 if (bnx2x_logtype_driver >= 0) 766 rte_log_set_level(bnx2x_logtype_driver, RTE_LOG_NOTICE); 767 } 768