1 // SPDX-License-Identifier: GPL-2.0 2 /* Copyright(c) 2023 Advanced Micro Devices, Inc */ 3 4 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 5 6 #include <linux/pci.h> 7 8 #include <linux/pds/pds_common.h> 9 10 #include "core.h" 11 12 MODULE_DESCRIPTION(PDSC_DRV_DESCRIPTION); 13 MODULE_AUTHOR("Advanced Micro Devices, Inc"); 14 MODULE_LICENSE("GPL"); 15 16 /* Supported devices */ 17 static const struct pci_device_id pdsc_id_table[] = { 18 { PCI_VDEVICE(PENSANDO, PCI_DEVICE_ID_PENSANDO_CORE_PF) }, 19 { 0, } /* end of table */ 20 }; 21 MODULE_DEVICE_TABLE(pci, pdsc_id_table); 22 23 static void pdsc_wdtimer_cb(struct timer_list *t) 24 { 25 struct pdsc *pdsc = from_timer(pdsc, t, wdtimer); 26 27 dev_dbg(pdsc->dev, "%s: jiffies %ld\n", __func__, jiffies); 28 mod_timer(&pdsc->wdtimer, 29 round_jiffies(jiffies + pdsc->wdtimer_period)); 30 31 queue_work(pdsc->wq, &pdsc->health_work); 32 } 33 34 static void pdsc_unmap_bars(struct pdsc *pdsc) 35 { 36 struct pdsc_dev_bar *bars = pdsc->bars; 37 unsigned int i; 38 39 for (i = 0; i < PDS_CORE_BARS_MAX; i++) { 40 if (bars[i].vaddr) 41 pci_iounmap(pdsc->pdev, bars[i].vaddr); 42 } 43 } 44 45 static int pdsc_map_bars(struct pdsc *pdsc) 46 { 47 struct pdsc_dev_bar *bar = pdsc->bars; 48 struct pci_dev *pdev = pdsc->pdev; 49 struct device *dev = pdsc->dev; 50 struct pdsc_dev_bar *bars; 51 unsigned int i, j; 52 int num_bars = 0; 53 int err; 54 u32 sig; 55 56 bars = pdsc->bars; 57 58 /* Since the PCI interface in the hardware is configurable, 59 * we need to poke into all the bars to find the set we're 60 * expecting. 61 */ 62 for (i = 0, j = 0; i < PDS_CORE_BARS_MAX; i++) { 63 if (!(pci_resource_flags(pdev, i) & IORESOURCE_MEM)) 64 continue; 65 66 bars[j].len = pci_resource_len(pdev, i); 67 bars[j].bus_addr = pci_resource_start(pdev, i); 68 bars[j].res_index = i; 69 70 /* only map the whole bar 0 */ 71 if (j > 0) { 72 bars[j].vaddr = NULL; 73 } else { 74 bars[j].vaddr = pci_iomap(pdev, i, bars[j].len); 75 if (!bars[j].vaddr) { 76 dev_err(dev, "Cannot map BAR %d, aborting\n", i); 77 return -ENODEV; 78 } 79 } 80 81 j++; 82 } 83 num_bars = j; 84 85 /* BAR0: dev_cmd and interrupts */ 86 if (num_bars < 1) { 87 dev_err(dev, "No bars found\n"); 88 err = -EFAULT; 89 goto err_out; 90 } 91 92 if (bar->len < PDS_CORE_BAR0_SIZE) { 93 dev_err(dev, "Resource bar size %lu too small\n", bar->len); 94 err = -EFAULT; 95 goto err_out; 96 } 97 98 pdsc->info_regs = bar->vaddr + PDS_CORE_BAR0_DEV_INFO_REGS_OFFSET; 99 pdsc->cmd_regs = bar->vaddr + PDS_CORE_BAR0_DEV_CMD_REGS_OFFSET; 100 pdsc->intr_status = bar->vaddr + PDS_CORE_BAR0_INTR_STATUS_OFFSET; 101 pdsc->intr_ctrl = bar->vaddr + PDS_CORE_BAR0_INTR_CTRL_OFFSET; 102 103 sig = ioread32(&pdsc->info_regs->signature); 104 if (sig != PDS_CORE_DEV_INFO_SIGNATURE) { 105 dev_err(dev, "Incompatible firmware signature %x", sig); 106 err = -EFAULT; 107 goto err_out; 108 } 109 110 /* BAR1: doorbells */ 111 bar++; 112 if (num_bars < 2) { 113 dev_err(dev, "Doorbell bar missing\n"); 114 err = -EFAULT; 115 goto err_out; 116 } 117 118 pdsc->db_pages = bar->vaddr; 119 pdsc->phy_db_pages = bar->bus_addr; 120 121 return 0; 122 123 err_out: 124 pdsc_unmap_bars(pdsc); 125 return err; 126 } 127 128 void __iomem *pdsc_map_dbpage(struct pdsc *pdsc, int page_num) 129 { 130 return pci_iomap_range(pdsc->pdev, 131 pdsc->bars[PDS_CORE_PCI_BAR_DBELL].res_index, 132 (u64)page_num << PAGE_SHIFT, PAGE_SIZE); 133 } 134 135 static int pdsc_init_vf(struct pdsc *vf) 136 { 137 return -1; 138 } 139 140 static const struct devlink_health_reporter_ops pdsc_fw_reporter_ops = { 141 .name = "fw", 142 .diagnose = pdsc_fw_reporter_diagnose, 143 }; 144 145 #define PDSC_WQ_NAME_LEN 24 146 147 static int pdsc_init_pf(struct pdsc *pdsc) 148 { 149 struct devlink_health_reporter *hr; 150 char wq_name[PDSC_WQ_NAME_LEN]; 151 struct devlink *dl; 152 int err; 153 154 pcie_print_link_status(pdsc->pdev); 155 156 err = pci_request_regions(pdsc->pdev, PDS_CORE_DRV_NAME); 157 if (err) { 158 dev_err(pdsc->dev, "Cannot request PCI regions: %pe\n", 159 ERR_PTR(err)); 160 return err; 161 } 162 163 err = pdsc_map_bars(pdsc); 164 if (err) 165 goto err_out_release_regions; 166 167 /* General workqueue and timer, but don't start timer yet */ 168 snprintf(wq_name, sizeof(wq_name), "%s.%d", PDS_CORE_DRV_NAME, pdsc->uid); 169 pdsc->wq = create_singlethread_workqueue(wq_name); 170 INIT_WORK(&pdsc->health_work, pdsc_health_thread); 171 timer_setup(&pdsc->wdtimer, pdsc_wdtimer_cb, 0); 172 pdsc->wdtimer_period = PDSC_WATCHDOG_SECS * HZ; 173 174 mutex_init(&pdsc->devcmd_lock); 175 mutex_init(&pdsc->config_lock); 176 spin_lock_init(&pdsc->adminq_lock); 177 178 mutex_lock(&pdsc->config_lock); 179 set_bit(PDSC_S_FW_DEAD, &pdsc->state); 180 181 err = pdsc_setup(pdsc, PDSC_SETUP_INIT); 182 if (err) 183 goto err_out_unmap_bars; 184 err = pdsc_start(pdsc); 185 if (err) 186 goto err_out_teardown; 187 188 mutex_unlock(&pdsc->config_lock); 189 190 dl = priv_to_devlink(pdsc); 191 devl_lock(dl); 192 193 hr = devl_health_reporter_create(dl, &pdsc_fw_reporter_ops, 0, pdsc); 194 if (IS_ERR(hr)) { 195 dev_warn(pdsc->dev, "Failed to create fw reporter: %pe\n", hr); 196 err = PTR_ERR(hr); 197 devl_unlock(dl); 198 goto err_out_stop; 199 } 200 pdsc->fw_reporter = hr; 201 202 devl_register(dl); 203 devl_unlock(dl); 204 205 /* Lastly, start the health check timer */ 206 mod_timer(&pdsc->wdtimer, round_jiffies(jiffies + pdsc->wdtimer_period)); 207 208 return 0; 209 210 err_out_stop: 211 pdsc_stop(pdsc); 212 err_out_teardown: 213 pdsc_teardown(pdsc, PDSC_TEARDOWN_REMOVING); 214 err_out_unmap_bars: 215 mutex_unlock(&pdsc->config_lock); 216 del_timer_sync(&pdsc->wdtimer); 217 if (pdsc->wq) 218 destroy_workqueue(pdsc->wq); 219 mutex_destroy(&pdsc->config_lock); 220 mutex_destroy(&pdsc->devcmd_lock); 221 pci_free_irq_vectors(pdsc->pdev); 222 pdsc_unmap_bars(pdsc); 223 err_out_release_regions: 224 pci_release_regions(pdsc->pdev); 225 226 return err; 227 } 228 229 static const struct devlink_ops pdsc_dl_ops = { 230 .info_get = pdsc_dl_info_get, 231 .flash_update = pdsc_dl_flash_update, 232 }; 233 234 static const struct devlink_ops pdsc_dl_vf_ops = { 235 }; 236 237 static DEFINE_IDA(pdsc_ida); 238 239 static int pdsc_probe(struct pci_dev *pdev, const struct pci_device_id *ent) 240 { 241 struct device *dev = &pdev->dev; 242 const struct devlink_ops *ops; 243 struct devlink *dl; 244 struct pdsc *pdsc; 245 bool is_pf; 246 int err; 247 248 is_pf = !pdev->is_virtfn; 249 ops = is_pf ? &pdsc_dl_ops : &pdsc_dl_vf_ops; 250 dl = devlink_alloc(ops, sizeof(struct pdsc), dev); 251 if (!dl) 252 return -ENOMEM; 253 pdsc = devlink_priv(dl); 254 255 pdsc->pdev = pdev; 256 pdsc->dev = &pdev->dev; 257 set_bit(PDSC_S_INITING_DRIVER, &pdsc->state); 258 pci_set_drvdata(pdev, pdsc); 259 pdsc_debugfs_add_dev(pdsc); 260 261 err = ida_alloc(&pdsc_ida, GFP_KERNEL); 262 if (err < 0) { 263 dev_err(pdsc->dev, "%s: id alloc failed: %pe\n", 264 __func__, ERR_PTR(err)); 265 goto err_out_free_devlink; 266 } 267 pdsc->uid = err; 268 269 /* Query system for DMA addressing limitation for the device. */ 270 err = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(PDS_CORE_ADDR_LEN)); 271 if (err) { 272 dev_err(dev, "Unable to obtain 64-bit DMA for consistent allocations, aborting: %pe\n", 273 ERR_PTR(err)); 274 goto err_out_free_ida; 275 } 276 277 err = pci_enable_device(pdev); 278 if (err) { 279 dev_err(dev, "Cannot enable PCI device: %pe\n", ERR_PTR(err)); 280 goto err_out_free_ida; 281 } 282 pci_set_master(pdev); 283 284 if (is_pf) 285 err = pdsc_init_pf(pdsc); 286 else 287 err = pdsc_init_vf(pdsc); 288 if (err) { 289 dev_err(dev, "Cannot init device: %pe\n", ERR_PTR(err)); 290 goto err_out_clear_master; 291 } 292 293 clear_bit(PDSC_S_INITING_DRIVER, &pdsc->state); 294 return 0; 295 296 err_out_clear_master: 297 pci_clear_master(pdev); 298 pci_disable_device(pdev); 299 err_out_free_ida: 300 ida_free(&pdsc_ida, pdsc->uid); 301 err_out_free_devlink: 302 pdsc_debugfs_del_dev(pdsc); 303 devlink_free(dl); 304 305 return err; 306 } 307 308 static void pdsc_remove(struct pci_dev *pdev) 309 { 310 struct pdsc *pdsc = pci_get_drvdata(pdev); 311 struct devlink *dl; 312 313 /* Unhook the registrations first to be sure there 314 * are no requests while we're stopping. 315 */ 316 dl = priv_to_devlink(pdsc); 317 devl_lock(dl); 318 devl_unregister(dl); 319 if (pdsc->fw_reporter) { 320 devl_health_reporter_destroy(pdsc->fw_reporter); 321 pdsc->fw_reporter = NULL; 322 } 323 devl_unlock(dl); 324 325 if (!pdev->is_virtfn) { 326 del_timer_sync(&pdsc->wdtimer); 327 if (pdsc->wq) 328 destroy_workqueue(pdsc->wq); 329 330 mutex_lock(&pdsc->config_lock); 331 set_bit(PDSC_S_STOPPING_DRIVER, &pdsc->state); 332 333 pdsc_stop(pdsc); 334 pdsc_teardown(pdsc, PDSC_TEARDOWN_REMOVING); 335 mutex_unlock(&pdsc->config_lock); 336 mutex_destroy(&pdsc->config_lock); 337 mutex_destroy(&pdsc->devcmd_lock); 338 339 pci_free_irq_vectors(pdev); 340 pdsc_unmap_bars(pdsc); 341 pci_release_regions(pdev); 342 } 343 344 pci_clear_master(pdev); 345 pci_disable_device(pdev); 346 347 ida_free(&pdsc_ida, pdsc->uid); 348 pdsc_debugfs_del_dev(pdsc); 349 devlink_free(dl); 350 } 351 352 static struct pci_driver pdsc_driver = { 353 .name = PDS_CORE_DRV_NAME, 354 .id_table = pdsc_id_table, 355 .probe = pdsc_probe, 356 .remove = pdsc_remove, 357 }; 358 359 static int __init pdsc_init_module(void) 360 { 361 if (strcmp(KBUILD_MODNAME, PDS_CORE_DRV_NAME)) 362 return -EINVAL; 363 364 pdsc_debugfs_create(); 365 return pci_register_driver(&pdsc_driver); 366 } 367 368 static void __exit pdsc_cleanup_module(void) 369 { 370 pci_unregister_driver(&pdsc_driver); 371 pdsc_debugfs_destroy(); 372 } 373 374 module_init(pdsc_init_module); 375 module_exit(pdsc_cleanup_module); 376