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