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