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