1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright(c) 2023 Advanced Micro Devices, Inc */
4 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
8 #include <linux/pds/pds_common.h>
12 MODULE_DESCRIPTION(PDSC_DRV_DESCRIPTION);
13 MODULE_AUTHOR("Advanced Micro Devices, Inc");
14 MODULE_LICENSE("GPL");
16 /* Supported devices */
17 static const struct pci_device_id pdsc_id_table[] = {
18 { PCI_VDEVICE(PENSANDO, PCI_DEVICE_ID_PENSANDO_CORE_PF) },
19 { PCI_VDEVICE(PENSANDO, PCI_DEVICE_ID_PENSANDO_VDPA_VF) },
20 { 0, } /* end of table */
22 MODULE_DEVICE_TABLE(pci, pdsc_id_table);
24 static void pdsc_wdtimer_cb(struct timer_list *t)
26 struct pdsc *pdsc = from_timer(pdsc, t, wdtimer);
28 dev_dbg(pdsc->dev, "%s: jiffies %ld\n", __func__, jiffies);
29 mod_timer(&pdsc->wdtimer,
30 round_jiffies(jiffies + pdsc->wdtimer_period));
32 queue_work(pdsc->wq, &pdsc->health_work);
35 static void pdsc_unmap_bars(struct pdsc *pdsc)
37 struct pdsc_dev_bar *bars = pdsc->bars;
40 pdsc->info_regs = NULL;
41 pdsc->cmd_regs = NULL;
42 pdsc->intr_status = NULL;
43 pdsc->intr_ctrl = NULL;
45 for (i = 0; i < PDS_CORE_BARS_MAX; i++) {
47 pci_iounmap(pdsc->pdev, bars[i].vaddr);
51 static int pdsc_map_bars(struct pdsc *pdsc)
53 struct pdsc_dev_bar *bar = pdsc->bars;
54 struct pci_dev *pdev = pdsc->pdev;
55 struct device *dev = pdsc->dev;
56 struct pdsc_dev_bar *bars;
64 /* Since the PCI interface in the hardware is configurable,
65 * we need to poke into all the bars to find the set we're
68 for (i = 0, j = 0; i < PDS_CORE_BARS_MAX; i++) {
69 if (!(pci_resource_flags(pdev, i) & IORESOURCE_MEM))
72 bars[j].len = pci_resource_len(pdev, i);
73 bars[j].bus_addr = pci_resource_start(pdev, i);
74 bars[j].res_index = i;
76 /* only map the whole bar 0 */
80 bars[j].vaddr = pci_iomap(pdev, i, bars[j].len);
82 dev_err(dev, "Cannot map BAR %d, aborting\n", i);
91 /* BAR0: dev_cmd and interrupts */
93 dev_err(dev, "No bars found\n");
98 if (bar->len < PDS_CORE_BAR0_SIZE) {
99 dev_err(dev, "Resource bar size %lu too small\n", bar->len);
104 pdsc->info_regs = bar->vaddr + PDS_CORE_BAR0_DEV_INFO_REGS_OFFSET;
105 pdsc->cmd_regs = bar->vaddr + PDS_CORE_BAR0_DEV_CMD_REGS_OFFSET;
106 pdsc->intr_status = bar->vaddr + PDS_CORE_BAR0_INTR_STATUS_OFFSET;
107 pdsc->intr_ctrl = bar->vaddr + PDS_CORE_BAR0_INTR_CTRL_OFFSET;
109 sig = ioread32(&pdsc->info_regs->signature);
110 if (sig != PDS_CORE_DEV_INFO_SIGNATURE) {
111 dev_err(dev, "Incompatible firmware signature %x", sig);
116 /* BAR1: doorbells */
119 dev_err(dev, "Doorbell bar missing\n");
124 pdsc->db_pages = bar->vaddr;
125 pdsc->phy_db_pages = bar->bus_addr;
130 pdsc_unmap_bars(pdsc);
134 void __iomem *pdsc_map_dbpage(struct pdsc *pdsc, int page_num)
136 return pci_iomap_range(pdsc->pdev,
137 pdsc->bars[PDS_CORE_PCI_BAR_DBELL].res_index,
138 (u64)page_num << PAGE_SHIFT, PAGE_SIZE);
141 static int pdsc_sriov_configure(struct pci_dev *pdev, int num_vfs)
143 struct pdsc *pdsc = pci_get_drvdata(pdev);
144 struct device *dev = pdsc->dev;
148 pdsc->vfs = kcalloc(num_vfs, sizeof(struct pdsc_vf),
152 pdsc->num_vfs = num_vfs;
154 ret = pci_enable_sriov(pdev, num_vfs);
156 dev_err(dev, "Cannot enable SRIOV: %pe\n",
165 pci_disable_sriov(pdev);
174 static int pdsc_init_vf(struct pdsc *vf)
180 pf = pdsc_get_pf_struct(vf->pdev);
181 if (IS_ERR_OR_NULL(pf))
182 return PTR_ERR(pf) ?: -1;
184 vf->vf_id = pci_iov_vf_id(vf->pdev);
186 dl = priv_to_devlink(vf);
191 pf->vfs[vf->vf_id].vf = vf;
192 err = pdsc_auxbus_dev_add(vf, pf);
202 static const struct devlink_health_reporter_ops pdsc_fw_reporter_ops = {
204 .diagnose = pdsc_fw_reporter_diagnose,
207 static const struct devlink_param pdsc_dl_params[] = {
208 DEVLINK_PARAM_GENERIC(ENABLE_VNET,
209 BIT(DEVLINK_PARAM_CMODE_RUNTIME),
212 pdsc_dl_enable_validate),
215 #define PDSC_WQ_NAME_LEN 24
217 static int pdsc_init_pf(struct pdsc *pdsc)
219 struct devlink_health_reporter *hr;
220 char wq_name[PDSC_WQ_NAME_LEN];
224 pcie_print_link_status(pdsc->pdev);
226 err = pci_request_regions(pdsc->pdev, PDS_CORE_DRV_NAME);
228 dev_err(pdsc->dev, "Cannot request PCI regions: %pe\n",
233 err = pdsc_map_bars(pdsc);
235 goto err_out_release_regions;
237 /* General workqueue and timer, but don't start timer yet */
238 snprintf(wq_name, sizeof(wq_name), "%s.%d", PDS_CORE_DRV_NAME, pdsc->uid);
239 pdsc->wq = create_singlethread_workqueue(wq_name);
240 INIT_WORK(&pdsc->health_work, pdsc_health_thread);
241 INIT_WORK(&pdsc->pci_reset_work, pdsc_pci_reset_thread);
242 timer_setup(&pdsc->wdtimer, pdsc_wdtimer_cb, 0);
243 pdsc->wdtimer_period = PDSC_WATCHDOG_SECS * HZ;
245 mutex_init(&pdsc->devcmd_lock);
246 mutex_init(&pdsc->config_lock);
247 spin_lock_init(&pdsc->adminq_lock);
249 mutex_lock(&pdsc->config_lock);
250 set_bit(PDSC_S_FW_DEAD, &pdsc->state);
252 err = pdsc_setup(pdsc, PDSC_SETUP_INIT);
254 mutex_unlock(&pdsc->config_lock);
255 goto err_out_unmap_bars;
258 err = pdsc_start(pdsc);
260 mutex_unlock(&pdsc->config_lock);
261 goto err_out_teardown;
264 mutex_unlock(&pdsc->config_lock);
266 dl = priv_to_devlink(pdsc);
268 err = devl_params_register(dl, pdsc_dl_params,
269 ARRAY_SIZE(pdsc_dl_params));
272 dev_warn(pdsc->dev, "Failed to register devlink params: %pe\n",
277 hr = devl_health_reporter_create(dl, &pdsc_fw_reporter_ops, 0, pdsc);
280 dev_warn(pdsc->dev, "Failed to create fw reporter: %pe\n", hr);
282 goto err_out_unreg_params;
284 pdsc->fw_reporter = hr;
289 /* Lastly, start the health check timer */
290 mod_timer(&pdsc->wdtimer, round_jiffies(jiffies + pdsc->wdtimer_period));
294 err_out_unreg_params:
295 devlink_params_unregister(dl, pdsc_dl_params,
296 ARRAY_SIZE(pdsc_dl_params));
300 pdsc_teardown(pdsc, PDSC_TEARDOWN_REMOVING);
302 timer_shutdown_sync(&pdsc->wdtimer);
304 destroy_workqueue(pdsc->wq);
305 mutex_destroy(&pdsc->config_lock);
306 mutex_destroy(&pdsc->devcmd_lock);
307 pci_free_irq_vectors(pdsc->pdev);
308 pdsc_unmap_bars(pdsc);
309 err_out_release_regions:
310 pci_release_regions(pdsc->pdev);
315 static const struct devlink_ops pdsc_dl_ops = {
316 .info_get = pdsc_dl_info_get,
317 .flash_update = pdsc_dl_flash_update,
320 static const struct devlink_ops pdsc_dl_vf_ops = {
323 static DEFINE_IDA(pdsc_ida);
325 static int pdsc_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
327 struct device *dev = &pdev->dev;
328 const struct devlink_ops *ops;
334 is_pf = !pdev->is_virtfn;
335 ops = is_pf ? &pdsc_dl_ops : &pdsc_dl_vf_ops;
336 dl = devlink_alloc(ops, sizeof(struct pdsc), dev);
339 pdsc = devlink_priv(dl);
342 pdsc->dev = &pdev->dev;
343 set_bit(PDSC_S_INITING_DRIVER, &pdsc->state);
344 pci_set_drvdata(pdev, pdsc);
345 pdsc_debugfs_add_dev(pdsc);
347 err = ida_alloc(&pdsc_ida, GFP_KERNEL);
349 dev_err(pdsc->dev, "%s: id alloc failed: %pe\n",
350 __func__, ERR_PTR(err));
351 goto err_out_free_devlink;
355 /* Query system for DMA addressing limitation for the device. */
356 err = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(PDS_CORE_ADDR_LEN));
358 dev_err(dev, "Unable to obtain 64-bit DMA for consistent allocations, aborting: %pe\n",
360 goto err_out_free_ida;
363 err = pci_enable_device(pdev);
365 dev_err(dev, "Cannot enable PCI device: %pe\n", ERR_PTR(err));
366 goto err_out_free_ida;
368 pci_set_master(pdev);
371 err = pdsc_init_pf(pdsc);
373 err = pdsc_init_vf(pdsc);
375 dev_err(dev, "Cannot init device: %pe\n", ERR_PTR(err));
376 goto err_out_disable_device;
379 clear_bit(PDSC_S_INITING_DRIVER, &pdsc->state);
382 err_out_disable_device:
383 pci_disable_device(pdev);
385 ida_free(&pdsc_ida, pdsc->uid);
386 err_out_free_devlink:
387 pdsc_debugfs_del_dev(pdsc);
393 static void pdsc_remove(struct pci_dev *pdev)
395 struct pdsc *pdsc = pci_get_drvdata(pdev);
398 /* Unhook the registrations first to be sure there
399 * are no requests while we're stopping.
401 dl = priv_to_devlink(pdsc);
404 if (!pdev->is_virtfn) {
405 if (pdsc->fw_reporter) {
406 devl_health_reporter_destroy(pdsc->fw_reporter);
407 pdsc->fw_reporter = NULL;
409 devl_params_unregister(dl, pdsc_dl_params,
410 ARRAY_SIZE(pdsc_dl_params));
414 if (pdev->is_virtfn) {
417 pf = pdsc_get_pf_struct(pdsc->pdev);
419 pdsc_auxbus_dev_del(pdsc, pf);
420 pf->vfs[pdsc->vf_id].vf = NULL;
423 /* Remove the VFs and their aux_bus connections before other
424 * cleanup so that the clients can use the AdminQ to cleanly
425 * shut themselves down.
427 pdsc_sriov_configure(pdev, 0);
429 timer_shutdown_sync(&pdsc->wdtimer);
431 destroy_workqueue(pdsc->wq);
433 mutex_lock(&pdsc->config_lock);
434 set_bit(PDSC_S_STOPPING_DRIVER, &pdsc->state);
437 pdsc_teardown(pdsc, PDSC_TEARDOWN_REMOVING);
438 mutex_unlock(&pdsc->config_lock);
439 mutex_destroy(&pdsc->config_lock);
440 mutex_destroy(&pdsc->devcmd_lock);
442 pdsc_unmap_bars(pdsc);
443 pci_release_regions(pdev);
446 pci_disable_device(pdev);
448 ida_free(&pdsc_ida, pdsc->uid);
449 pdsc_debugfs_del_dev(pdsc);
453 static void pdsc_stop_health_thread(struct pdsc *pdsc)
455 if (pdsc->pdev->is_virtfn)
458 timer_shutdown_sync(&pdsc->wdtimer);
459 if (pdsc->health_work.func)
460 cancel_work_sync(&pdsc->health_work);
463 static void pdsc_restart_health_thread(struct pdsc *pdsc)
465 if (pdsc->pdev->is_virtfn)
468 timer_setup(&pdsc->wdtimer, pdsc_wdtimer_cb, 0);
469 mod_timer(&pdsc->wdtimer, jiffies + 1);
472 static void pdsc_reset_prepare(struct pci_dev *pdev)
474 struct pdsc *pdsc = pci_get_drvdata(pdev);
476 pdsc_stop_health_thread(pdsc);
479 pdsc_unmap_bars(pdsc);
480 pci_release_regions(pdev);
481 if (pci_is_enabled(pdev))
482 pci_disable_device(pdev);
485 static void pdsc_reset_done(struct pci_dev *pdev)
487 struct pdsc *pdsc = pci_get_drvdata(pdev);
488 struct device *dev = pdsc->dev;
491 err = pci_enable_device(pdev);
493 dev_err(dev, "Cannot enable PCI device: %pe\n", ERR_PTR(err));
496 pci_set_master(pdev);
498 if (!pdev->is_virtfn) {
499 pcie_print_link_status(pdsc->pdev);
501 err = pci_request_regions(pdsc->pdev, PDS_CORE_DRV_NAME);
503 dev_err(pdsc->dev, "Cannot request PCI regions: %pe\n",
508 err = pdsc_map_bars(pdsc);
514 pdsc_restart_health_thread(pdsc);
517 static const struct pci_error_handlers pdsc_err_handler = {
519 .reset_prepare = pdsc_reset_prepare,
520 .reset_done = pdsc_reset_done,
523 static struct pci_driver pdsc_driver = {
524 .name = PDS_CORE_DRV_NAME,
525 .id_table = pdsc_id_table,
527 .remove = pdsc_remove,
528 .sriov_configure = pdsc_sriov_configure,
529 .err_handler = &pdsc_err_handler,
532 void *pdsc_get_pf_struct(struct pci_dev *vf_pdev)
534 return pci_iov_get_pf_drvdata(vf_pdev, &pdsc_driver);
536 EXPORT_SYMBOL_GPL(pdsc_get_pf_struct);
538 static int __init pdsc_init_module(void)
540 if (strcmp(KBUILD_MODNAME, PDS_CORE_DRV_NAME))
543 pdsc_debugfs_create();
544 return pci_register_driver(&pdsc_driver);
547 static void __exit pdsc_cleanup_module(void)
549 pci_unregister_driver(&pdsc_driver);
550 pdsc_debugfs_destroy();
553 module_init(pdsc_init_module);
554 module_exit(pdsc_cleanup_module);