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