1 // SPDX-License-Identifier: GPL-2.0 2 /* Copyright (c) Meta Platforms, Inc. and affiliates. */ 3 4 #include <linux/init.h> 5 #include <linux/module.h> 6 #include <linux/pci.h> 7 #include <linux/rtnetlink.h> 8 #include <linux/types.h> 9 #include <net/devlink.h> 10 11 #include "fbnic.h" 12 #include "fbnic_drvinfo.h" 13 #include "fbnic_hw_stats.h" 14 #include "fbnic_netdev.h" 15 16 char fbnic_driver_name[] = DRV_NAME; 17 18 MODULE_DESCRIPTION(DRV_SUMMARY); 19 MODULE_LICENSE("GPL"); 20 21 static const struct fbnic_info fbnic_asic_info = { 22 .max_num_queues = FBNIC_MAX_QUEUES, 23 .bar_mask = BIT(0) | BIT(4) 24 }; 25 26 static const struct fbnic_info *fbnic_info_tbl[] = { 27 [fbnic_board_asic] = &fbnic_asic_info, 28 }; 29 30 static const struct pci_device_id fbnic_pci_tbl[] = { 31 { PCI_DEVICE_DATA(META, FBNIC_ASIC, fbnic_board_asic) }, 32 /* Required last entry */ 33 {0, } 34 }; 35 MODULE_DEVICE_TABLE(pci, fbnic_pci_tbl); 36 37 u32 fbnic_rd32(struct fbnic_dev *fbd, u32 reg) 38 { 39 u32 __iomem *csr = READ_ONCE(fbd->uc_addr0); 40 u32 value; 41 42 if (!csr) 43 return ~0U; 44 45 value = readl(csr + reg); 46 47 /* If any bits are 0 value should be valid */ 48 if (~value) 49 return value; 50 51 /* All 1's may be valid if ZEROs register still works */ 52 if (reg != FBNIC_MASTER_SPARE_0 && ~readl(csr + FBNIC_MASTER_SPARE_0)) 53 return value; 54 55 /* Hardware is giving us all 1's reads, assume it is gone */ 56 WRITE_ONCE(fbd->uc_addr0, NULL); 57 WRITE_ONCE(fbd->uc_addr4, NULL); 58 59 dev_err(fbd->dev, 60 "Failed read (idx 0x%x AKA addr 0x%x), disabled CSR access, awaiting reset\n", 61 reg, reg << 2); 62 63 /* Notify stack that device has lost (PCIe) link */ 64 if (!fbnic_init_failure(fbd)) 65 netif_device_detach(fbd->netdev); 66 67 return ~0U; 68 } 69 70 bool fbnic_fw_present(struct fbnic_dev *fbd) 71 { 72 return !!READ_ONCE(fbd->uc_addr4); 73 } 74 75 void fbnic_fw_wr32(struct fbnic_dev *fbd, u32 reg, u32 val) 76 { 77 u32 __iomem *csr = READ_ONCE(fbd->uc_addr4); 78 79 if (csr) 80 writel(val, csr + reg); 81 } 82 83 u32 fbnic_fw_rd32(struct fbnic_dev *fbd, u32 reg) 84 { 85 u32 __iomem *csr = READ_ONCE(fbd->uc_addr4); 86 u32 value; 87 88 if (!csr) 89 return ~0U; 90 91 value = readl(csr + reg); 92 93 /* If any bits are 0 value should be valid */ 94 if (~value) 95 return value; 96 97 /* All 1's may be valid if ZEROs register still works */ 98 if (reg != FBNIC_FW_ZERO_REG && ~readl(csr + FBNIC_FW_ZERO_REG)) 99 return value; 100 101 /* Hardware is giving us all 1's reads, assume it is gone */ 102 WRITE_ONCE(fbd->uc_addr0, NULL); 103 WRITE_ONCE(fbd->uc_addr4, NULL); 104 105 dev_err(fbd->dev, 106 "Failed read (idx 0x%x AKA addr 0x%x), disabled CSR access, awaiting reset\n", 107 reg, reg << 2); 108 109 /* Notify stack that device has lost (PCIe) link */ 110 if (!fbnic_init_failure(fbd)) 111 netif_device_detach(fbd->netdev); 112 113 return ~0U; 114 } 115 116 static void fbnic_service_task_start(struct fbnic_net *fbn) 117 { 118 struct fbnic_dev *fbd = fbn->fbd; 119 120 schedule_delayed_work(&fbd->service_task, HZ); 121 phylink_resume(fbn->phylink); 122 } 123 124 static void fbnic_service_task_stop(struct fbnic_net *fbn) 125 { 126 struct fbnic_dev *fbd = fbn->fbd; 127 128 phylink_suspend(fbn->phylink, fbnic_bmc_present(fbd)); 129 cancel_delayed_work(&fbd->service_task); 130 } 131 132 void fbnic_up(struct fbnic_net *fbn) 133 { 134 fbnic_enable(fbn); 135 136 fbnic_fill(fbn); 137 138 fbnic_rss_reinit_hw(fbn->fbd, fbn); 139 140 __fbnic_set_rx_mode(fbn->netdev); 141 142 /* Enable Tx/Rx processing */ 143 fbnic_napi_enable(fbn); 144 netif_tx_start_all_queues(fbn->netdev); 145 146 fbnic_service_task_start(fbn); 147 } 148 149 void fbnic_down_noidle(struct fbnic_net *fbn) 150 { 151 fbnic_service_task_stop(fbn); 152 153 /* Disable Tx/Rx Processing */ 154 fbnic_napi_disable(fbn); 155 netif_tx_disable(fbn->netdev); 156 157 fbnic_clear_rx_mode(fbn->netdev); 158 fbnic_clear_rules(fbn->fbd); 159 fbnic_rss_disable_hw(fbn->fbd); 160 fbnic_disable(fbn); 161 } 162 163 void fbnic_down(struct fbnic_net *fbn) 164 { 165 fbnic_down_noidle(fbn); 166 167 fbnic_wait_all_queues_idle(fbn->fbd, false); 168 169 fbnic_flush(fbn); 170 } 171 172 static void fbnic_health_check(struct fbnic_dev *fbd) 173 { 174 struct fbnic_fw_mbx *tx_mbx = &fbd->mbx[FBNIC_IPC_MBX_TX_IDX]; 175 176 /* As long as the heart is beating the FW is healty */ 177 if (fbd->fw_heartbeat_enabled) 178 return; 179 180 /* If the Tx mailbox still has messages sitting in it then there likely 181 * isn't anything we can do. We will wait until the mailbox is empty to 182 * report the fault so we can collect the crashlog. 183 */ 184 if (tx_mbx->head != tx_mbx->tail) 185 return; 186 187 /* TBD: Need to add a more thorough recovery here. 188 * Specifically I need to verify what all the firmware will have 189 * changed since we had setup and it rebooted. May just need to 190 * perform a down/up. For now we will just reclaim ownership so 191 * the heartbeat can catch the next fault. 192 */ 193 fbnic_fw_xmit_ownership_msg(fbd, true); 194 } 195 196 static void fbnic_service_task(struct work_struct *work) 197 { 198 struct fbnic_dev *fbd = container_of(to_delayed_work(work), 199 struct fbnic_dev, service_task); 200 201 rtnl_lock(); 202 203 fbnic_get_hw_stats32(fbd); 204 205 fbnic_fw_check_heartbeat(fbd); 206 207 fbnic_health_check(fbd); 208 209 if (netif_carrier_ok(fbd->netdev)) 210 fbnic_napi_depletion_check(fbd->netdev); 211 212 if (netif_running(fbd->netdev)) 213 schedule_delayed_work(&fbd->service_task, HZ); 214 215 rtnl_unlock(); 216 } 217 218 /** 219 * fbnic_probe - Device Initialization Routine 220 * @pdev: PCI device information struct 221 * @ent: entry in fbnic_pci_tbl 222 * 223 * Initializes a PCI device identified by a pci_dev structure. 224 * The OS initialization, configuring of the adapter private structure, 225 * and a hardware reset occur. 226 * 227 * Return: 0 on success, negative on failure 228 **/ 229 static int fbnic_probe(struct pci_dev *pdev, const struct pci_device_id *ent) 230 { 231 const struct fbnic_info *info = fbnic_info_tbl[ent->driver_data]; 232 struct net_device *netdev; 233 struct fbnic_dev *fbd; 234 int err; 235 236 if (pdev->error_state != pci_channel_io_normal) { 237 dev_err(&pdev->dev, 238 "PCI device still in an error state. Unable to load...\n"); 239 return -EIO; 240 } 241 242 err = pcim_enable_device(pdev); 243 if (err) { 244 dev_err(&pdev->dev, "PCI enable device failed: %d\n", err); 245 return err; 246 } 247 248 err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(46)); 249 if (err) 250 err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32)); 251 if (err) { 252 dev_err(&pdev->dev, "DMA configuration failed: %d\n", err); 253 return err; 254 } 255 256 err = pcim_iomap_regions(pdev, info->bar_mask, fbnic_driver_name); 257 if (err) { 258 dev_err(&pdev->dev, 259 "pci_request_selected_regions failed: %d\n", err); 260 return err; 261 } 262 263 fbd = fbnic_devlink_alloc(pdev); 264 if (!fbd) { 265 dev_err(&pdev->dev, "Devlink allocation failed\n"); 266 return -ENOMEM; 267 } 268 269 /* Populate driver with hardware-specific info and handlers */ 270 fbd->max_num_queues = info->max_num_queues; 271 272 pci_set_master(pdev); 273 pci_save_state(pdev); 274 275 INIT_DELAYED_WORK(&fbd->service_task, fbnic_service_task); 276 277 err = fbnic_alloc_irqs(fbd); 278 if (err) 279 goto free_fbd; 280 281 err = fbnic_mac_init(fbd); 282 if (err) { 283 dev_err(&pdev->dev, "Failed to initialize MAC: %d\n", err); 284 goto free_irqs; 285 } 286 287 err = fbnic_fw_request_mbx(fbd); 288 if (err) { 289 dev_err(&pdev->dev, 290 "Firmware mailbox initialization failure\n"); 291 goto free_irqs; 292 } 293 294 /* Send the request to enable the FW logging to host. Note if this 295 * fails we ignore the error and just display a message as it is 296 * possible the FW is just too old to support the logging and needs 297 * to be updated. 298 */ 299 err = fbnic_fw_log_init(fbd); 300 if (err) 301 dev_warn(fbd->dev, 302 "Unable to initialize firmware log buffer: %d\n", 303 err); 304 305 fbnic_devlink_register(fbd); 306 fbnic_dbg_fbd_init(fbd); 307 spin_lock_init(&fbd->hw_stats_lock); 308 309 /* Capture snapshot of hardware stats so netdev can calculate delta */ 310 fbnic_reset_hw_stats(fbd); 311 312 fbnic_hwmon_register(fbd); 313 314 if (!fbd->dsn) { 315 dev_warn(&pdev->dev, "Reading serial number failed\n"); 316 goto init_failure_mode; 317 } 318 319 netdev = fbnic_netdev_alloc(fbd); 320 if (!netdev) { 321 dev_err(&pdev->dev, "Netdev allocation failed\n"); 322 goto init_failure_mode; 323 } 324 325 err = fbnic_ptp_setup(fbd); 326 if (err) 327 goto ifm_free_netdev; 328 329 err = fbnic_netdev_register(netdev); 330 if (err) { 331 dev_err(&pdev->dev, "Netdev registration failed: %d\n", err); 332 goto ifm_destroy_ptp; 333 } 334 335 return 0; 336 337 ifm_destroy_ptp: 338 fbnic_ptp_destroy(fbd); 339 ifm_free_netdev: 340 fbnic_netdev_free(fbd); 341 init_failure_mode: 342 dev_warn(&pdev->dev, "Probe error encountered, entering init failure mode. Normal networking functionality will not be available.\n"); 343 /* Always return 0 even on error so devlink is registered to allow 344 * firmware updates for fixes. 345 */ 346 return 0; 347 free_irqs: 348 fbnic_free_irqs(fbd); 349 free_fbd: 350 fbnic_devlink_free(fbd); 351 352 return err; 353 } 354 355 /** 356 * fbnic_remove - Device Removal Routine 357 * @pdev: PCI device information struct 358 * 359 * Called by the PCI subsystem to alert the driver that it should release 360 * a PCI device. The could be caused by a Hot-Plug event, or because the 361 * driver is going to be removed from memory. 362 **/ 363 static void fbnic_remove(struct pci_dev *pdev) 364 { 365 struct fbnic_dev *fbd = pci_get_drvdata(pdev); 366 367 if (!fbnic_init_failure(fbd)) { 368 struct net_device *netdev = fbd->netdev; 369 370 fbnic_netdev_unregister(netdev); 371 cancel_delayed_work_sync(&fbd->service_task); 372 fbnic_ptp_destroy(fbd); 373 fbnic_netdev_free(fbd); 374 } 375 376 fbnic_hwmon_unregister(fbd); 377 fbnic_dbg_fbd_exit(fbd); 378 fbnic_devlink_unregister(fbd); 379 fbnic_fw_log_free(fbd); 380 fbnic_fw_free_mbx(fbd); 381 fbnic_free_irqs(fbd); 382 383 fbnic_devlink_free(fbd); 384 } 385 386 static int fbnic_pm_suspend(struct device *dev) 387 { 388 struct fbnic_dev *fbd = dev_get_drvdata(dev); 389 struct net_device *netdev = fbd->netdev; 390 391 if (fbnic_init_failure(fbd)) 392 goto null_uc_addr; 393 394 rtnl_lock(); 395 396 netif_device_detach(netdev); 397 398 if (netif_running(netdev)) 399 netdev->netdev_ops->ndo_stop(netdev); 400 401 rtnl_unlock(); 402 403 null_uc_addr: 404 fbnic_fw_log_disable(fbd); 405 406 devl_lock(priv_to_devlink(fbd)); 407 408 fbnic_fw_free_mbx(fbd); 409 410 devl_unlock(priv_to_devlink(fbd)); 411 412 /* Free the IRQs so they aren't trying to occupy sleeping CPUs */ 413 fbnic_free_irqs(fbd); 414 415 /* Hardware is about to go away, so switch off MMIO access internally */ 416 WRITE_ONCE(fbd->uc_addr0, NULL); 417 WRITE_ONCE(fbd->uc_addr4, NULL); 418 419 return 0; 420 } 421 422 static int __fbnic_pm_resume(struct device *dev) 423 { 424 struct fbnic_dev *fbd = dev_get_drvdata(dev); 425 struct net_device *netdev = fbd->netdev; 426 void __iomem * const *iomap_table; 427 struct fbnic_net *fbn; 428 int err; 429 430 /* Restore MMIO access */ 431 iomap_table = pcim_iomap_table(to_pci_dev(dev)); 432 fbd->uc_addr0 = iomap_table[0]; 433 fbd->uc_addr4 = iomap_table[4]; 434 435 /* Rerequest the IRQs */ 436 err = fbnic_alloc_irqs(fbd); 437 if (err) 438 goto err_invalidate_uc_addr; 439 440 fbd->mac->init_regs(fbd); 441 442 devl_lock(priv_to_devlink(fbd)); 443 444 /* Re-enable mailbox */ 445 err = fbnic_fw_request_mbx(fbd); 446 if (err) 447 goto err_free_irqs; 448 449 devl_unlock(priv_to_devlink(fbd)); 450 451 /* Only send log history if log buffer is empty to prevent duplicate 452 * log entries. 453 */ 454 fbnic_fw_log_enable(fbd, list_empty(&fbd->fw_log.entries)); 455 456 /* No netdev means there isn't a network interface to bring up */ 457 if (fbnic_init_failure(fbd)) 458 return 0; 459 460 fbn = netdev_priv(netdev); 461 462 /* Reset the queues if needed */ 463 fbnic_reset_queues(fbn, fbn->num_tx_queues, fbn->num_rx_queues); 464 465 rtnl_lock(); 466 467 if (netif_running(netdev)) { 468 err = __fbnic_open(fbn); 469 if (err) 470 goto err_free_mbx; 471 } 472 473 rtnl_unlock(); 474 475 return 0; 476 err_free_mbx: 477 fbnic_fw_log_disable(fbd); 478 479 rtnl_unlock(); 480 fbnic_fw_free_mbx(fbd); 481 err_free_irqs: 482 fbnic_free_irqs(fbd); 483 err_invalidate_uc_addr: 484 WRITE_ONCE(fbd->uc_addr0, NULL); 485 WRITE_ONCE(fbd->uc_addr4, NULL); 486 return err; 487 } 488 489 static void __fbnic_pm_attach(struct device *dev) 490 { 491 struct fbnic_dev *fbd = dev_get_drvdata(dev); 492 struct net_device *netdev = fbd->netdev; 493 struct fbnic_net *fbn; 494 495 if (fbnic_init_failure(fbd)) 496 return; 497 498 fbn = netdev_priv(netdev); 499 500 if (netif_running(netdev)) 501 fbnic_up(fbn); 502 503 netif_device_attach(netdev); 504 } 505 506 static int __maybe_unused fbnic_pm_resume(struct device *dev) 507 { 508 int err; 509 510 err = __fbnic_pm_resume(dev); 511 if (!err) 512 __fbnic_pm_attach(dev); 513 514 return err; 515 } 516 517 static const struct dev_pm_ops fbnic_pm_ops = { 518 SET_SYSTEM_SLEEP_PM_OPS(fbnic_pm_suspend, fbnic_pm_resume) 519 }; 520 521 static void fbnic_shutdown(struct pci_dev *pdev) 522 { 523 fbnic_pm_suspend(&pdev->dev); 524 } 525 526 static pci_ers_result_t fbnic_err_error_detected(struct pci_dev *pdev, 527 pci_channel_state_t state) 528 { 529 /* Disconnect device if failure is not recoverable via reset */ 530 if (state == pci_channel_io_perm_failure) 531 return PCI_ERS_RESULT_DISCONNECT; 532 533 fbnic_pm_suspend(&pdev->dev); 534 535 /* Request a slot reset */ 536 return PCI_ERS_RESULT_NEED_RESET; 537 } 538 539 static pci_ers_result_t fbnic_err_slot_reset(struct pci_dev *pdev) 540 { 541 int err; 542 543 pci_set_power_state(pdev, PCI_D0); 544 pci_restore_state(pdev); 545 pci_save_state(pdev); 546 547 if (pci_enable_device_mem(pdev)) { 548 dev_err(&pdev->dev, 549 "Cannot re-enable PCI device after reset.\n"); 550 return PCI_ERS_RESULT_DISCONNECT; 551 } 552 553 /* Restore device to previous state */ 554 err = __fbnic_pm_resume(&pdev->dev); 555 556 return err ? PCI_ERS_RESULT_DISCONNECT : PCI_ERS_RESULT_RECOVERED; 557 } 558 559 static void fbnic_err_resume(struct pci_dev *pdev) 560 { 561 __fbnic_pm_attach(&pdev->dev); 562 } 563 564 static const struct pci_error_handlers fbnic_err_handler = { 565 .error_detected = fbnic_err_error_detected, 566 .slot_reset = fbnic_err_slot_reset, 567 .resume = fbnic_err_resume, 568 }; 569 570 static struct pci_driver fbnic_driver = { 571 .name = fbnic_driver_name, 572 .id_table = fbnic_pci_tbl, 573 .probe = fbnic_probe, 574 .remove = fbnic_remove, 575 .driver.pm = &fbnic_pm_ops, 576 .shutdown = fbnic_shutdown, 577 .err_handler = &fbnic_err_handler, 578 }; 579 580 /** 581 * fbnic_init_module - Driver Registration Routine 582 * 583 * The first routine called when the driver is loaded. All it does is 584 * register with the PCI subsystem. 585 * 586 * Return: 0 on success, negative on failure 587 **/ 588 static int __init fbnic_init_module(void) 589 { 590 int err; 591 592 fbnic_dbg_init(); 593 594 err = pci_register_driver(&fbnic_driver); 595 if (err) { 596 fbnic_dbg_exit(); 597 goto out; 598 } 599 600 pr_info(DRV_SUMMARY " (%s)", fbnic_driver.name); 601 out: 602 return err; 603 } 604 module_init(fbnic_init_module); 605 606 /** 607 * fbnic_exit_module - Driver Exit Cleanup Routine 608 * 609 * Called just before the driver is removed from memory. 610 **/ 611 static void __exit fbnic_exit_module(void) 612 { 613 pci_unregister_driver(&fbnic_driver); 614 615 fbnic_dbg_exit(); 616 } 617 module_exit(fbnic_exit_module); 618