1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) Meta Platforms, Inc. and affiliates. */
4 #include <linux/init.h>
5 #include <linux/module.h>
7 #include <linux/rtnetlink.h>
8 #include <linux/types.h>
11 #include "fbnic_drvinfo.h"
12 #include "fbnic_netdev.h"
14 char fbnic_driver_name[] = DRV_NAME;
16 MODULE_DESCRIPTION(DRV_SUMMARY);
17 MODULE_LICENSE("GPL");
19 static const struct fbnic_info fbnic_asic_info = {
20 .max_num_queues = FBNIC_MAX_QUEUES,
21 .bar_mask = BIT(0) | BIT(4)
24 static const struct fbnic_info *fbnic_info_tbl[] = {
25 [fbnic_board_asic] = &fbnic_asic_info,
28 static const struct pci_device_id fbnic_pci_tbl[] = {
29 { PCI_DEVICE_DATA(META, FBNIC_ASIC, fbnic_board_asic) },
30 /* Required last entry */
33 MODULE_DEVICE_TABLE(pci, fbnic_pci_tbl);
35 u32 fbnic_rd32(struct fbnic_dev *fbd, u32 reg)
37 u32 __iomem *csr = READ_ONCE(fbd->uc_addr0);
43 value = readl(csr + reg);
45 /* If any bits are 0 value should be valid */
49 /* All 1's may be valid if ZEROs register still works */
50 if (reg != FBNIC_MASTER_SPARE_0 && ~readl(csr + FBNIC_MASTER_SPARE_0))
53 /* Hardware is giving us all 1's reads, assume it is gone */
54 WRITE_ONCE(fbd->uc_addr0, NULL);
55 WRITE_ONCE(fbd->uc_addr4, NULL);
58 "Failed read (idx 0x%x AKA addr 0x%x), disabled CSR access, awaiting reset\n",
61 /* Notify stack that device has lost (PCIe) link */
62 if (!fbnic_init_failure(fbd))
63 netif_device_detach(fbd->netdev);
68 bool fbnic_fw_present(struct fbnic_dev *fbd)
70 return !!READ_ONCE(fbd->uc_addr4);
73 void fbnic_fw_wr32(struct fbnic_dev *fbd, u32 reg, u32 val)
75 u32 __iomem *csr = READ_ONCE(fbd->uc_addr4);
78 writel(val, csr + reg);
81 u32 fbnic_fw_rd32(struct fbnic_dev *fbd, u32 reg)
83 u32 __iomem *csr = READ_ONCE(fbd->uc_addr4);
89 value = readl(csr + reg);
91 /* If any bits are 0 value should be valid */
95 /* All 1's may be valid if ZEROs register still works */
96 if (reg != FBNIC_FW_ZERO_REG && ~readl(csr + FBNIC_FW_ZERO_REG))
99 /* Hardware is giving us all 1's reads, assume it is gone */
100 WRITE_ONCE(fbd->uc_addr0, NULL);
101 WRITE_ONCE(fbd->uc_addr4, NULL);
104 "Failed read (idx 0x%x AKA addr 0x%x), disabled CSR access, awaiting reset\n",
107 /* Notify stack that device has lost (PCIe) link */
108 if (!fbnic_init_failure(fbd))
109 netif_device_detach(fbd->netdev);
114 static void fbnic_service_task_start(struct fbnic_net *fbn)
116 struct fbnic_dev *fbd = fbn->fbd;
118 schedule_delayed_work(&fbd->service_task, HZ);
119 phylink_resume(fbn->phylink);
122 static void fbnic_service_task_stop(struct fbnic_net *fbn)
124 struct fbnic_dev *fbd = fbn->fbd;
126 phylink_suspend(fbn->phylink, fbnic_bmc_present(fbd));
127 cancel_delayed_work(&fbd->service_task);
130 void fbnic_up(struct fbnic_net *fbn)
136 fbnic_rss_reinit_hw(fbn->fbd, fbn);
138 __fbnic_set_rx_mode(fbn->netdev);
140 /* Enable Tx/Rx processing */
141 fbnic_napi_enable(fbn);
142 netif_tx_start_all_queues(fbn->netdev);
144 fbnic_service_task_start(fbn);
147 static void fbnic_down_noidle(struct fbnic_net *fbn)
149 fbnic_service_task_stop(fbn);
151 /* Disable Tx/Rx Processing */
152 fbnic_napi_disable(fbn);
153 netif_tx_disable(fbn->netdev);
155 fbnic_clear_rx_mode(fbn->netdev);
156 fbnic_clear_rules(fbn->fbd);
157 fbnic_rss_disable_hw(fbn->fbd);
161 void fbnic_down(struct fbnic_net *fbn)
163 fbnic_down_noidle(fbn);
165 fbnic_wait_all_queues_idle(fbn->fbd, false);
170 static void fbnic_health_check(struct fbnic_dev *fbd)
172 struct fbnic_fw_mbx *tx_mbx = &fbd->mbx[FBNIC_IPC_MBX_TX_IDX];
174 /* As long as the heart is beating the FW is healty */
175 if (fbd->fw_heartbeat_enabled)
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.
182 if (tx_mbx->head != tx_mbx->tail)
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.
191 fbnic_fw_xmit_ownership_msg(fbd, true);
194 static void fbnic_service_task(struct work_struct *work)
196 struct fbnic_dev *fbd = container_of(to_delayed_work(work),
197 struct fbnic_dev, service_task);
201 fbnic_fw_check_heartbeat(fbd);
203 fbnic_health_check(fbd);
205 if (netif_carrier_ok(fbd->netdev))
206 fbnic_napi_depletion_check(fbd->netdev);
208 if (netif_running(fbd->netdev))
209 schedule_delayed_work(&fbd->service_task, HZ);
215 * fbnic_probe - Device Initialization Routine
216 * @pdev: PCI device information struct
217 * @ent: entry in fbnic_pci_tbl
219 * Initializes a PCI device identified by a pci_dev structure.
220 * The OS initialization, configuring of the adapter private structure,
221 * and a hardware reset occur.
223 * Return: 0 on success, negative on failure
225 static int fbnic_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
227 const struct fbnic_info *info = fbnic_info_tbl[ent->driver_data];
228 struct net_device *netdev;
229 struct fbnic_dev *fbd;
232 if (pdev->error_state != pci_channel_io_normal) {
234 "PCI device still in an error state. Unable to load...\n");
238 err = pcim_enable_device(pdev);
240 dev_err(&pdev->dev, "PCI enable device failed: %d\n", err);
244 err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(46));
246 err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
248 dev_err(&pdev->dev, "DMA configuration failed: %d\n", err);
252 err = pcim_iomap_regions(pdev, info->bar_mask, fbnic_driver_name);
255 "pci_request_selected_regions failed: %d\n", err);
259 fbd = fbnic_devlink_alloc(pdev);
261 dev_err(&pdev->dev, "Devlink allocation failed\n");
265 /* Populate driver with hardware-specific info and handlers */
266 fbd->max_num_queues = info->max_num_queues;
268 pci_set_master(pdev);
269 pci_save_state(pdev);
271 INIT_DELAYED_WORK(&fbd->service_task, fbnic_service_task);
273 err = fbnic_alloc_irqs(fbd);
277 err = fbnic_mac_init(fbd);
279 dev_err(&pdev->dev, "Failed to initialize MAC: %d\n", err);
283 err = fbnic_fw_enable_mbx(fbd);
286 "Firmware mailbox initialization failure\n");
290 fbnic_devlink_register(fbd);
293 dev_warn(&pdev->dev, "Reading serial number failed\n");
294 goto init_failure_mode;
297 netdev = fbnic_netdev_alloc(fbd);
299 dev_err(&pdev->dev, "Netdev allocation failed\n");
300 goto init_failure_mode;
303 err = fbnic_netdev_register(netdev);
305 dev_err(&pdev->dev, "Netdev registration failed: %d\n", err);
306 goto ifm_free_netdev;
312 fbnic_netdev_free(fbd);
314 dev_warn(&pdev->dev, "Probe error encountered, entering init failure mode. Normal networking functionality will not be available.\n");
315 /* Always return 0 even on error so devlink is registered to allow
316 * firmware updates for fixes.
320 fbnic_free_irqs(fbd);
322 pci_disable_device(pdev);
323 fbnic_devlink_free(fbd);
329 * fbnic_remove - Device Removal Routine
330 * @pdev: PCI device information struct
332 * Called by the PCI subsystem to alert the driver that it should release
333 * a PCI device. The could be caused by a Hot-Plug event, or because the
334 * driver is going to be removed from memory.
336 static void fbnic_remove(struct pci_dev *pdev)
338 struct fbnic_dev *fbd = pci_get_drvdata(pdev);
340 if (!fbnic_init_failure(fbd)) {
341 struct net_device *netdev = fbd->netdev;
343 fbnic_netdev_unregister(netdev);
344 cancel_delayed_work_sync(&fbd->service_task);
345 fbnic_netdev_free(fbd);
348 fbnic_devlink_unregister(fbd);
349 fbnic_fw_disable_mbx(fbd);
350 fbnic_free_irqs(fbd);
352 pci_disable_device(pdev);
353 fbnic_devlink_free(fbd);
356 static int fbnic_pm_suspend(struct device *dev)
358 struct fbnic_dev *fbd = dev_get_drvdata(dev);
359 struct net_device *netdev = fbd->netdev;
361 if (fbnic_init_failure(fbd))
366 netif_device_detach(netdev);
368 if (netif_running(netdev))
369 netdev->netdev_ops->ndo_stop(netdev);
374 fbnic_fw_disable_mbx(fbd);
376 /* Free the IRQs so they aren't trying to occupy sleeping CPUs */
377 fbnic_free_irqs(fbd);
379 /* Hardware is about to go away, so switch off MMIO access internally */
380 WRITE_ONCE(fbd->uc_addr0, NULL);
381 WRITE_ONCE(fbd->uc_addr4, NULL);
386 static int __fbnic_pm_resume(struct device *dev)
388 struct fbnic_dev *fbd = dev_get_drvdata(dev);
389 struct net_device *netdev = fbd->netdev;
390 void __iomem * const *iomap_table;
391 struct fbnic_net *fbn;
394 /* Restore MMIO access */
395 iomap_table = pcim_iomap_table(to_pci_dev(dev));
396 fbd->uc_addr0 = iomap_table[0];
397 fbd->uc_addr4 = iomap_table[4];
399 /* Rerequest the IRQs */
400 err = fbnic_alloc_irqs(fbd);
402 goto err_invalidate_uc_addr;
404 fbd->mac->init_regs(fbd);
406 /* Re-enable mailbox */
407 err = fbnic_fw_enable_mbx(fbd);
411 /* No netdev means there isn't a network interface to bring up */
412 if (fbnic_init_failure(fbd))
415 fbn = netdev_priv(netdev);
417 /* Reset the queues if needed */
418 fbnic_reset_queues(fbn, fbn->num_tx_queues, fbn->num_rx_queues);
422 if (netif_running(netdev)) {
423 err = __fbnic_open(fbn);
425 goto err_disable_mbx;
433 fbnic_fw_disable_mbx(fbd);
435 fbnic_free_irqs(fbd);
436 err_invalidate_uc_addr:
437 WRITE_ONCE(fbd->uc_addr0, NULL);
438 WRITE_ONCE(fbd->uc_addr4, NULL);
442 static void __fbnic_pm_attach(struct device *dev)
444 struct fbnic_dev *fbd = dev_get_drvdata(dev);
445 struct net_device *netdev = fbd->netdev;
446 struct fbnic_net *fbn;
448 if (fbnic_init_failure(fbd))
451 fbn = netdev_priv(netdev);
453 if (netif_running(netdev))
456 netif_device_attach(netdev);
459 static int __maybe_unused fbnic_pm_resume(struct device *dev)
463 err = __fbnic_pm_resume(dev);
465 __fbnic_pm_attach(dev);
470 static const struct dev_pm_ops fbnic_pm_ops = {
471 SET_SYSTEM_SLEEP_PM_OPS(fbnic_pm_suspend, fbnic_pm_resume)
474 static void fbnic_shutdown(struct pci_dev *pdev)
476 fbnic_pm_suspend(&pdev->dev);
479 static pci_ers_result_t fbnic_err_error_detected(struct pci_dev *pdev,
480 pci_channel_state_t state)
482 /* Disconnect device if failure is not recoverable via reset */
483 if (state == pci_channel_io_perm_failure)
484 return PCI_ERS_RESULT_DISCONNECT;
486 fbnic_pm_suspend(&pdev->dev);
488 /* Request a slot reset */
489 return PCI_ERS_RESULT_NEED_RESET;
492 static pci_ers_result_t fbnic_err_slot_reset(struct pci_dev *pdev)
496 pci_set_power_state(pdev, PCI_D0);
497 pci_restore_state(pdev);
498 pci_save_state(pdev);
500 if (pci_enable_device_mem(pdev)) {
502 "Cannot re-enable PCI device after reset.\n");
503 return PCI_ERS_RESULT_DISCONNECT;
506 /* Restore device to previous state */
507 err = __fbnic_pm_resume(&pdev->dev);
509 return err ? PCI_ERS_RESULT_DISCONNECT : PCI_ERS_RESULT_RECOVERED;
512 static void fbnic_err_resume(struct pci_dev *pdev)
514 __fbnic_pm_attach(&pdev->dev);
517 static const struct pci_error_handlers fbnic_err_handler = {
518 .error_detected = fbnic_err_error_detected,
519 .slot_reset = fbnic_err_slot_reset,
520 .resume = fbnic_err_resume,
523 static struct pci_driver fbnic_driver = {
524 .name = fbnic_driver_name,
525 .id_table = fbnic_pci_tbl,
526 .probe = fbnic_probe,
527 .remove = fbnic_remove,
528 .driver.pm = &fbnic_pm_ops,
529 .shutdown = fbnic_shutdown,
530 .err_handler = &fbnic_err_handler,
534 * fbnic_init_module - Driver Registration Routine
536 * The first routine called when the driver is loaded. All it does is
537 * register with the PCI subsystem.
539 * Return: 0 on success, negative on failure
541 static int __init fbnic_init_module(void)
545 err = pci_register_driver(&fbnic_driver);
549 pr_info(DRV_SUMMARY " (%s)", fbnic_driver.name);
553 module_init(fbnic_init_module);
556 * fbnic_exit_module - Driver Exit Cleanup Routine
558 * Called just before the driver is removed from memory.
560 static void __exit fbnic_exit_module(void)
562 pci_unregister_driver(&fbnic_driver);
564 module_exit(fbnic_exit_module);