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