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_hw_stats.h"
13 #include "fbnic_netdev.h"
15 char fbnic_driver_name
[] = DRV_NAME
;
17 MODULE_DESCRIPTION(DRV_SUMMARY
);
18 MODULE_LICENSE("GPL");
20 static const struct fbnic_info fbnic_asic_info
= {
21 .max_num_queues
= FBNIC_MAX_QUEUES
,
22 .bar_mask
= BIT(0) | BIT(4)
25 static const struct fbnic_info
*fbnic_info_tbl
[] = {
26 [fbnic_board_asic
] = &fbnic_asic_info
,
29 static const struct pci_device_id fbnic_pci_tbl
[] = {
30 { PCI_DEVICE_DATA(META
, FBNIC_ASIC
, fbnic_board_asic
) },
31 /* Required last entry */
34 MODULE_DEVICE_TABLE(pci
, fbnic_pci_tbl
);
36 u32
fbnic_rd32(struct fbnic_dev
*fbd
, u32 reg
)
38 u32 __iomem
*csr
= READ_ONCE(fbd
->uc_addr0
);
44 value
= readl(csr
+ reg
);
46 /* If any bits are 0 value should be valid */
50 /* All 1's may be valid if ZEROs register still works */
51 if (reg
!= FBNIC_MASTER_SPARE_0
&& ~readl(csr
+ FBNIC_MASTER_SPARE_0
))
54 /* Hardware is giving us all 1's reads, assume it is gone */
55 WRITE_ONCE(fbd
->uc_addr0
, NULL
);
56 WRITE_ONCE(fbd
->uc_addr4
, NULL
);
59 "Failed read (idx 0x%x AKA addr 0x%x), disabled CSR access, awaiting reset\n",
62 /* Notify stack that device has lost (PCIe) link */
63 if (!fbnic_init_failure(fbd
))
64 netif_device_detach(fbd
->netdev
);
69 bool fbnic_fw_present(struct fbnic_dev
*fbd
)
71 return !!READ_ONCE(fbd
->uc_addr4
);
74 void fbnic_fw_wr32(struct fbnic_dev
*fbd
, u32 reg
, u32 val
)
76 u32 __iomem
*csr
= READ_ONCE(fbd
->uc_addr4
);
79 writel(val
, csr
+ reg
);
82 u32
fbnic_fw_rd32(struct fbnic_dev
*fbd
, u32 reg
)
84 u32 __iomem
*csr
= READ_ONCE(fbd
->uc_addr4
);
90 value
= readl(csr
+ reg
);
92 /* If any bits are 0 value should be valid */
96 /* All 1's may be valid if ZEROs register still works */
97 if (reg
!= FBNIC_FW_ZERO_REG
&& ~readl(csr
+ FBNIC_FW_ZERO_REG
))
100 /* Hardware is giving us all 1's reads, assume it is gone */
101 WRITE_ONCE(fbd
->uc_addr0
, NULL
);
102 WRITE_ONCE(fbd
->uc_addr4
, NULL
);
105 "Failed read (idx 0x%x AKA addr 0x%x), disabled CSR access, awaiting reset\n",
108 /* Notify stack that device has lost (PCIe) link */
109 if (!fbnic_init_failure(fbd
))
110 netif_device_detach(fbd
->netdev
);
115 static void fbnic_service_task_start(struct fbnic_net
*fbn
)
117 struct fbnic_dev
*fbd
= fbn
->fbd
;
119 schedule_delayed_work(&fbd
->service_task
, HZ
);
120 phylink_resume(fbn
->phylink
);
123 static void fbnic_service_task_stop(struct fbnic_net
*fbn
)
125 struct fbnic_dev
*fbd
= fbn
->fbd
;
127 phylink_suspend(fbn
->phylink
, fbnic_bmc_present(fbd
));
128 cancel_delayed_work(&fbd
->service_task
);
131 void fbnic_up(struct fbnic_net
*fbn
)
137 fbnic_rss_reinit_hw(fbn
->fbd
, fbn
);
139 __fbnic_set_rx_mode(fbn
->netdev
);
141 /* Enable Tx/Rx processing */
142 fbnic_napi_enable(fbn
);
143 netif_tx_start_all_queues(fbn
->netdev
);
145 fbnic_service_task_start(fbn
);
148 static void fbnic_down_noidle(struct fbnic_net
*fbn
)
150 fbnic_service_task_stop(fbn
);
152 /* Disable Tx/Rx Processing */
153 fbnic_napi_disable(fbn
);
154 netif_tx_disable(fbn
->netdev
);
156 fbnic_clear_rx_mode(fbn
->netdev
);
157 fbnic_clear_rules(fbn
->fbd
);
158 fbnic_rss_disable_hw(fbn
->fbd
);
162 void fbnic_down(struct fbnic_net
*fbn
)
164 fbnic_down_noidle(fbn
);
166 fbnic_wait_all_queues_idle(fbn
->fbd
, false);
171 static void fbnic_health_check(struct fbnic_dev
*fbd
)
173 struct fbnic_fw_mbx
*tx_mbx
= &fbd
->mbx
[FBNIC_IPC_MBX_TX_IDX
];
175 /* As long as the heart is beating the FW is healty */
176 if (fbd
->fw_heartbeat_enabled
)
179 /* If the Tx mailbox still has messages sitting in it then there likely
180 * isn't anything we can do. We will wait until the mailbox is empty to
181 * report the fault so we can collect the crashlog.
183 if (tx_mbx
->head
!= tx_mbx
->tail
)
186 /* TBD: Need to add a more thorough recovery here.
187 * Specifically I need to verify what all the firmware will have
188 * changed since we had setup and it rebooted. May just need to
189 * perform a down/up. For now we will just reclaim ownership so
190 * the heartbeat can catch the next fault.
192 fbnic_fw_xmit_ownership_msg(fbd
, true);
195 static void fbnic_service_task(struct work_struct
*work
)
197 struct fbnic_dev
*fbd
= container_of(to_delayed_work(work
),
198 struct fbnic_dev
, service_task
);
202 fbnic_get_hw_stats32(fbd
);
204 fbnic_fw_check_heartbeat(fbd
);
206 fbnic_health_check(fbd
);
208 if (netif_carrier_ok(fbd
->netdev
))
209 fbnic_napi_depletion_check(fbd
->netdev
);
211 if (netif_running(fbd
->netdev
))
212 schedule_delayed_work(&fbd
->service_task
, HZ
);
218 * fbnic_probe - Device Initialization Routine
219 * @pdev: PCI device information struct
220 * @ent: entry in fbnic_pci_tbl
222 * Initializes a PCI device identified by a pci_dev structure.
223 * The OS initialization, configuring of the adapter private structure,
224 * and a hardware reset occur.
226 * Return: 0 on success, negative on failure
228 static int fbnic_probe(struct pci_dev
*pdev
, const struct pci_device_id
*ent
)
230 const struct fbnic_info
*info
= fbnic_info_tbl
[ent
->driver_data
];
231 struct net_device
*netdev
;
232 struct fbnic_dev
*fbd
;
235 if (pdev
->error_state
!= pci_channel_io_normal
) {
237 "PCI device still in an error state. Unable to load...\n");
241 err
= pcim_enable_device(pdev
);
243 dev_err(&pdev
->dev
, "PCI enable device failed: %d\n", err
);
247 err
= dma_set_mask_and_coherent(&pdev
->dev
, DMA_BIT_MASK(46));
249 err
= dma_set_mask_and_coherent(&pdev
->dev
, DMA_BIT_MASK(32));
251 dev_err(&pdev
->dev
, "DMA configuration failed: %d\n", err
);
255 err
= pcim_iomap_regions(pdev
, info
->bar_mask
, fbnic_driver_name
);
258 "pci_request_selected_regions failed: %d\n", err
);
262 fbd
= fbnic_devlink_alloc(pdev
);
264 dev_err(&pdev
->dev
, "Devlink allocation failed\n");
268 /* Populate driver with hardware-specific info and handlers */
269 fbd
->max_num_queues
= info
->max_num_queues
;
271 pci_set_master(pdev
);
272 pci_save_state(pdev
);
274 INIT_DELAYED_WORK(&fbd
->service_task
, fbnic_service_task
);
276 err
= fbnic_alloc_irqs(fbd
);
280 err
= fbnic_mac_init(fbd
);
282 dev_err(&pdev
->dev
, "Failed to initialize MAC: %d\n", err
);
286 err
= fbnic_fw_enable_mbx(fbd
);
289 "Firmware mailbox initialization failure\n");
293 fbnic_devlink_register(fbd
);
294 fbnic_dbg_fbd_init(fbd
);
296 /* Capture snapshot of hardware stats so netdev can calculate delta */
297 fbnic_reset_hw_stats(fbd
);
299 fbnic_hwmon_register(fbd
);
302 dev_warn(&pdev
->dev
, "Reading serial number failed\n");
303 goto init_failure_mode
;
306 netdev
= fbnic_netdev_alloc(fbd
);
308 dev_err(&pdev
->dev
, "Netdev allocation failed\n");
309 goto init_failure_mode
;
312 err
= fbnic_ptp_setup(fbd
);
314 goto ifm_free_netdev
;
316 err
= fbnic_netdev_register(netdev
);
318 dev_err(&pdev
->dev
, "Netdev registration failed: %d\n", err
);
319 goto ifm_destroy_ptp
;
325 fbnic_ptp_destroy(fbd
);
327 fbnic_netdev_free(fbd
);
329 dev_warn(&pdev
->dev
, "Probe error encountered, entering init failure mode. Normal networking functionality will not be available.\n");
330 /* Always return 0 even on error so devlink is registered to allow
331 * firmware updates for fixes.
335 fbnic_free_irqs(fbd
);
337 fbnic_devlink_free(fbd
);
343 * fbnic_remove - Device Removal Routine
344 * @pdev: PCI device information struct
346 * Called by the PCI subsystem to alert the driver that it should release
347 * a PCI device. The could be caused by a Hot-Plug event, or because the
348 * driver is going to be removed from memory.
350 static void fbnic_remove(struct pci_dev
*pdev
)
352 struct fbnic_dev
*fbd
= pci_get_drvdata(pdev
);
354 if (!fbnic_init_failure(fbd
)) {
355 struct net_device
*netdev
= fbd
->netdev
;
357 fbnic_netdev_unregister(netdev
);
358 cancel_delayed_work_sync(&fbd
->service_task
);
359 fbnic_ptp_destroy(fbd
);
360 fbnic_netdev_free(fbd
);
363 fbnic_hwmon_unregister(fbd
);
364 fbnic_dbg_fbd_exit(fbd
);
365 fbnic_devlink_unregister(fbd
);
366 fbnic_fw_disable_mbx(fbd
);
367 fbnic_free_irqs(fbd
);
369 fbnic_devlink_free(fbd
);
372 static int fbnic_pm_suspend(struct device
*dev
)
374 struct fbnic_dev
*fbd
= dev_get_drvdata(dev
);
375 struct net_device
*netdev
= fbd
->netdev
;
377 if (fbnic_init_failure(fbd
))
382 netif_device_detach(netdev
);
384 if (netif_running(netdev
))
385 netdev
->netdev_ops
->ndo_stop(netdev
);
390 fbnic_fw_disable_mbx(fbd
);
392 /* Free the IRQs so they aren't trying to occupy sleeping CPUs */
393 fbnic_free_irqs(fbd
);
395 /* Hardware is about to go away, so switch off MMIO access internally */
396 WRITE_ONCE(fbd
->uc_addr0
, NULL
);
397 WRITE_ONCE(fbd
->uc_addr4
, NULL
);
402 static int __fbnic_pm_resume(struct device
*dev
)
404 struct fbnic_dev
*fbd
= dev_get_drvdata(dev
);
405 struct net_device
*netdev
= fbd
->netdev
;
406 void __iomem
* const *iomap_table
;
407 struct fbnic_net
*fbn
;
410 /* Restore MMIO access */
411 iomap_table
= pcim_iomap_table(to_pci_dev(dev
));
412 fbd
->uc_addr0
= iomap_table
[0];
413 fbd
->uc_addr4
= iomap_table
[4];
415 /* Rerequest the IRQs */
416 err
= fbnic_alloc_irqs(fbd
);
418 goto err_invalidate_uc_addr
;
420 fbd
->mac
->init_regs(fbd
);
422 /* Re-enable mailbox */
423 err
= fbnic_fw_enable_mbx(fbd
);
427 /* No netdev means there isn't a network interface to bring up */
428 if (fbnic_init_failure(fbd
))
431 fbn
= netdev_priv(netdev
);
433 /* Reset the queues if needed */
434 fbnic_reset_queues(fbn
, fbn
->num_tx_queues
, fbn
->num_rx_queues
);
438 if (netif_running(netdev
)) {
439 err
= __fbnic_open(fbn
);
441 goto err_disable_mbx
;
449 fbnic_fw_disable_mbx(fbd
);
451 fbnic_free_irqs(fbd
);
452 err_invalidate_uc_addr
:
453 WRITE_ONCE(fbd
->uc_addr0
, NULL
);
454 WRITE_ONCE(fbd
->uc_addr4
, NULL
);
458 static void __fbnic_pm_attach(struct device
*dev
)
460 struct fbnic_dev
*fbd
= dev_get_drvdata(dev
);
461 struct net_device
*netdev
= fbd
->netdev
;
462 struct fbnic_net
*fbn
;
464 if (fbnic_init_failure(fbd
))
467 fbn
= netdev_priv(netdev
);
469 if (netif_running(netdev
))
472 netif_device_attach(netdev
);
475 static int __maybe_unused
fbnic_pm_resume(struct device
*dev
)
479 err
= __fbnic_pm_resume(dev
);
481 __fbnic_pm_attach(dev
);
486 static const struct dev_pm_ops fbnic_pm_ops
= {
487 SET_SYSTEM_SLEEP_PM_OPS(fbnic_pm_suspend
, fbnic_pm_resume
)
490 static void fbnic_shutdown(struct pci_dev
*pdev
)
492 fbnic_pm_suspend(&pdev
->dev
);
495 static pci_ers_result_t
fbnic_err_error_detected(struct pci_dev
*pdev
,
496 pci_channel_state_t state
)
498 /* Disconnect device if failure is not recoverable via reset */
499 if (state
== pci_channel_io_perm_failure
)
500 return PCI_ERS_RESULT_DISCONNECT
;
502 fbnic_pm_suspend(&pdev
->dev
);
504 /* Request a slot reset */
505 return PCI_ERS_RESULT_NEED_RESET
;
508 static pci_ers_result_t
fbnic_err_slot_reset(struct pci_dev
*pdev
)
512 pci_set_power_state(pdev
, PCI_D0
);
513 pci_restore_state(pdev
);
514 pci_save_state(pdev
);
516 if (pci_enable_device_mem(pdev
)) {
518 "Cannot re-enable PCI device after reset.\n");
519 return PCI_ERS_RESULT_DISCONNECT
;
522 /* Restore device to previous state */
523 err
= __fbnic_pm_resume(&pdev
->dev
);
525 return err
? PCI_ERS_RESULT_DISCONNECT
: PCI_ERS_RESULT_RECOVERED
;
528 static void fbnic_err_resume(struct pci_dev
*pdev
)
530 __fbnic_pm_attach(&pdev
->dev
);
533 static const struct pci_error_handlers fbnic_err_handler
= {
534 .error_detected
= fbnic_err_error_detected
,
535 .slot_reset
= fbnic_err_slot_reset
,
536 .resume
= fbnic_err_resume
,
539 static struct pci_driver fbnic_driver
= {
540 .name
= fbnic_driver_name
,
541 .id_table
= fbnic_pci_tbl
,
542 .probe
= fbnic_probe
,
543 .remove
= fbnic_remove
,
544 .driver
.pm
= &fbnic_pm_ops
,
545 .shutdown
= fbnic_shutdown
,
546 .err_handler
= &fbnic_err_handler
,
550 * fbnic_init_module - Driver Registration Routine
552 * The first routine called when the driver is loaded. All it does is
553 * register with the PCI subsystem.
555 * Return: 0 on success, negative on failure
557 static int __init
fbnic_init_module(void)
563 err
= pci_register_driver(&fbnic_driver
);
569 pr_info(DRV_SUMMARY
" (%s)", fbnic_driver
.name
);
573 module_init(fbnic_init_module
);
576 * fbnic_exit_module - Driver Exit Cleanup Routine
578 * Called just before the driver is removed from memory.
580 static void __exit
fbnic_exit_module(void)
582 pci_unregister_driver(&fbnic_driver
);
586 module_exit(fbnic_exit_module
);