1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright(c) 2023 Advanced Micro Devices, Inc */
5 #include <linux/vmalloc.h>
9 static BLOCKING_NOTIFIER_HEAD(pds_notify_chain);
11 int pdsc_register_notify(struct notifier_block *nb)
13 return blocking_notifier_chain_register(&pds_notify_chain, nb);
15 EXPORT_SYMBOL_GPL(pdsc_register_notify);
17 void pdsc_unregister_notify(struct notifier_block *nb)
19 blocking_notifier_chain_unregister(&pds_notify_chain, nb);
21 EXPORT_SYMBOL_GPL(pdsc_unregister_notify);
23 void pdsc_notify(unsigned long event, void *data)
25 blocking_notifier_call_chain(&pds_notify_chain, event, data);
28 void pdsc_intr_free(struct pdsc *pdsc, int index)
30 struct pdsc_intr_info *intr_info;
32 if (index >= pdsc->nintrs || index < 0) {
33 WARN(true, "bad intr index %d\n", index);
37 intr_info = &pdsc->intr_info[index];
38 if (!intr_info->vector)
40 dev_dbg(pdsc->dev, "%s: idx %d vec %d name %s\n",
41 __func__, index, intr_info->vector, intr_info->name);
43 pds_core_intr_mask(&pdsc->intr_ctrl[index], PDS_CORE_INTR_MASK_SET);
44 pds_core_intr_clean(&pdsc->intr_ctrl[index]);
46 free_irq(intr_info->vector, intr_info->data);
48 memset(intr_info, 0, sizeof(*intr_info));
51 int pdsc_intr_alloc(struct pdsc *pdsc, char *name,
52 irq_handler_t handler, void *data)
54 struct pdsc_intr_info *intr_info;
58 /* Find the first available interrupt */
59 for (index = 0; index < pdsc->nintrs; index++)
60 if (!pdsc->intr_info[index].vector)
62 if (index >= pdsc->nintrs) {
63 dev_warn(pdsc->dev, "%s: no intr, index=%d nintrs=%d\n",
64 __func__, index, pdsc->nintrs);
68 pds_core_intr_clean_flags(&pdsc->intr_ctrl[index],
69 PDS_CORE_INTR_CRED_RESET_COALESCE);
71 intr_info = &pdsc->intr_info[index];
73 intr_info->index = index;
74 intr_info->data = data;
75 strscpy(intr_info->name, name, sizeof(intr_info->name));
77 /* Get the OS vector number for the interrupt */
78 err = pci_irq_vector(pdsc->pdev, index);
80 dev_err(pdsc->dev, "failed to get intr vector index %d: %pe\n",
82 goto err_out_free_intr;
84 intr_info->vector = err;
86 /* Init the device's intr mask */
87 pds_core_intr_clean(&pdsc->intr_ctrl[index]);
88 pds_core_intr_mask_assert(&pdsc->intr_ctrl[index], 1);
89 pds_core_intr_mask(&pdsc->intr_ctrl[index], PDS_CORE_INTR_MASK_SET);
91 /* Register the isr with a name */
92 err = request_irq(intr_info->vector, handler, 0, intr_info->name, data);
94 dev_err(pdsc->dev, "failed to get intr irq vector %d: %pe\n",
95 intr_info->vector, ERR_PTR(err));
96 goto err_out_free_intr;
102 pdsc_intr_free(pdsc, index);
106 static void pdsc_qcq_intr_free(struct pdsc *pdsc, struct pdsc_qcq *qcq)
108 if (!(qcq->flags & PDS_CORE_QCQ_F_INTR) ||
109 qcq->intx == PDS_CORE_INTR_INDEX_NOT_ASSIGNED)
112 pdsc_intr_free(pdsc, qcq->intx);
113 qcq->intx = PDS_CORE_INTR_INDEX_NOT_ASSIGNED;
116 static int pdsc_qcq_intr_alloc(struct pdsc *pdsc, struct pdsc_qcq *qcq)
118 char name[PDSC_INTR_NAME_MAX_SZ];
121 if (!(qcq->flags & PDS_CORE_QCQ_F_INTR)) {
122 qcq->intx = PDS_CORE_INTR_INDEX_NOT_ASSIGNED;
126 snprintf(name, sizeof(name), "%s-%d-%s",
127 PDS_CORE_DRV_NAME, pdsc->pdev->bus->number, qcq->q.name);
128 index = pdsc_intr_alloc(pdsc, name, pdsc_adminq_isr, pdsc);
136 void pdsc_qcq_free(struct pdsc *pdsc, struct pdsc_qcq *qcq)
138 struct device *dev = pdsc->dev;
140 if (!(qcq && qcq->pdsc))
143 pdsc_debugfs_del_qcq(qcq);
145 pdsc_qcq_intr_free(pdsc, qcq);
148 dma_free_coherent(dev, qcq->q_size,
149 qcq->q_base, qcq->q_base_pa);
152 dma_free_coherent(dev, qcq->cq_size,
153 qcq->cq_base, qcq->cq_base_pa);
158 memset(qcq, 0, sizeof(*qcq));
161 static void pdsc_q_map(struct pdsc_queue *q, void *base, dma_addr_t base_pa)
163 struct pdsc_q_info *cur;
167 q->base_pa = base_pa;
169 for (i = 0, cur = q->info; i < q->num_descs; i++, cur++)
170 cur->desc = base + (i * q->desc_size);
173 static void pdsc_cq_map(struct pdsc_cq *cq, void *base, dma_addr_t base_pa)
175 struct pdsc_cq_info *cur;
179 cq->base_pa = base_pa;
181 for (i = 0, cur = cq->info; i < cq->num_descs; i++, cur++)
182 cur->comp = base + (i * cq->desc_size);
185 int pdsc_qcq_alloc(struct pdsc *pdsc, unsigned int type, unsigned int index,
186 const char *name, unsigned int flags, unsigned int num_descs,
187 unsigned int desc_size, unsigned int cq_desc_size,
188 unsigned int pid, struct pdsc_qcq *qcq)
190 struct device *dev = pdsc->dev;
191 void *q_base, *cq_base;
192 dma_addr_t cq_base_pa;
193 dma_addr_t q_base_pa;
196 qcq->q.info = vcalloc(num_descs, sizeof(*qcq->q.info));
204 INIT_WORK(&qcq->work, pdsc_work_thread);
207 qcq->q.index = index;
208 qcq->q.num_descs = num_descs;
209 qcq->q.desc_size = desc_size;
213 snprintf(qcq->q.name, sizeof(qcq->q.name), "%s%u", name, index);
215 err = pdsc_qcq_intr_alloc(pdsc, qcq);
217 goto err_out_free_q_info;
219 qcq->cq.info = vcalloc(num_descs, sizeof(*qcq->cq.info));
222 goto err_out_free_irq;
225 qcq->cq.bound_intr = &pdsc->intr_info[qcq->intx];
226 qcq->cq.num_descs = num_descs;
227 qcq->cq.desc_size = cq_desc_size;
228 qcq->cq.tail_idx = 0;
229 qcq->cq.done_color = 1;
231 if (flags & PDS_CORE_QCQ_F_NOTIFYQ) {
232 /* q & cq need to be contiguous in case of notifyq */
233 qcq->q_size = PDS_PAGE_SIZE +
234 ALIGN(num_descs * desc_size, PDS_PAGE_SIZE) +
235 ALIGN(num_descs * cq_desc_size, PDS_PAGE_SIZE);
236 qcq->q_base = dma_alloc_coherent(dev,
237 qcq->q_size + qcq->cq_size,
242 goto err_out_free_cq_info;
244 q_base = PTR_ALIGN(qcq->q_base, PDS_PAGE_SIZE);
245 q_base_pa = ALIGN(qcq->q_base_pa, PDS_PAGE_SIZE);
246 pdsc_q_map(&qcq->q, q_base, q_base_pa);
248 cq_base = PTR_ALIGN(q_base +
249 ALIGN(num_descs * desc_size, PDS_PAGE_SIZE),
251 cq_base_pa = ALIGN(qcq->q_base_pa +
252 ALIGN(num_descs * desc_size, PDS_PAGE_SIZE),
256 /* q DMA descriptors */
257 qcq->q_size = PDS_PAGE_SIZE + (num_descs * desc_size);
258 qcq->q_base = dma_alloc_coherent(dev, qcq->q_size,
263 goto err_out_free_cq_info;
265 q_base = PTR_ALIGN(qcq->q_base, PDS_PAGE_SIZE);
266 q_base_pa = ALIGN(qcq->q_base_pa, PDS_PAGE_SIZE);
267 pdsc_q_map(&qcq->q, q_base, q_base_pa);
269 /* cq DMA descriptors */
270 qcq->cq_size = PDS_PAGE_SIZE + (num_descs * cq_desc_size);
271 qcq->cq_base = dma_alloc_coherent(dev, qcq->cq_size,
278 cq_base = PTR_ALIGN(qcq->cq_base, PDS_PAGE_SIZE);
279 cq_base_pa = ALIGN(qcq->cq_base_pa, PDS_PAGE_SIZE);
282 pdsc_cq_map(&qcq->cq, cq_base, cq_base_pa);
283 qcq->cq.bound_q = &qcq->q;
285 pdsc_debugfs_add_qcq(pdsc, qcq);
290 dma_free_coherent(dev, qcq->q_size, qcq->q_base, qcq->q_base_pa);
291 err_out_free_cq_info:
294 pdsc_qcq_intr_free(pdsc, qcq);
297 memset(qcq, 0, sizeof(*qcq));
299 dev_err(dev, "qcq alloc of %s%d failed %d\n", name, index, err);
303 static int pdsc_core_init(struct pdsc *pdsc)
305 union pds_core_dev_comp comp = {};
306 union pds_core_dev_cmd cmd = {
307 .init.opcode = PDS_CORE_CMD_INIT,
309 struct pds_core_dev_init_data_out cido;
310 struct pds_core_dev_init_data_in cidi;
316 cidi.adminq_q_base = cpu_to_le64(pdsc->adminqcq.q_base_pa);
317 cidi.adminq_cq_base = cpu_to_le64(pdsc->adminqcq.cq_base_pa);
318 cidi.notifyq_cq_base = cpu_to_le64(pdsc->notifyqcq.cq.base_pa);
319 cidi.flags = cpu_to_le32(PDS_CORE_QINIT_F_IRQ | PDS_CORE_QINIT_F_ENA);
320 cidi.intr_index = cpu_to_le16(pdsc->adminqcq.intx);
321 cidi.adminq_ring_size = ilog2(pdsc->adminqcq.q.num_descs);
322 cidi.notifyq_ring_size = ilog2(pdsc->notifyqcq.q.num_descs);
324 mutex_lock(&pdsc->devcmd_lock);
326 sz = min_t(size_t, sizeof(cidi), sizeof(pdsc->cmd_regs->data));
327 memcpy_toio(&pdsc->cmd_regs->data, &cidi, sz);
329 err = pdsc_devcmd_locked(pdsc, &cmd, &comp, pdsc->devcmd_timeout);
331 sz = min_t(size_t, sizeof(cido), sizeof(pdsc->cmd_regs->data));
332 memcpy_fromio(&cido, &pdsc->cmd_regs->data, sz);
335 mutex_unlock(&pdsc->devcmd_lock);
337 dev_err(pdsc->dev, "Device init command failed: %pe\n",
342 pdsc->hw_index = le32_to_cpu(cido.core_hw_index);
344 dbid_count = le32_to_cpu(pdsc->dev_ident.ndbpgs_per_lif);
345 dbpage_num = pdsc->hw_index * dbid_count;
346 pdsc->kern_dbpage = pdsc_map_dbpage(pdsc, dbpage_num);
347 if (!pdsc->kern_dbpage) {
348 dev_err(pdsc->dev, "Cannot map dbpage, aborting\n");
352 pdsc->adminqcq.q.hw_type = cido.adminq_hw_type;
353 pdsc->adminqcq.q.hw_index = le32_to_cpu(cido.adminq_hw_index);
354 pdsc->adminqcq.q.dbval = PDS_CORE_DBELL_QID(pdsc->adminqcq.q.hw_index);
356 pdsc->notifyqcq.q.hw_type = cido.notifyq_hw_type;
357 pdsc->notifyqcq.q.hw_index = le32_to_cpu(cido.notifyq_hw_index);
358 pdsc->notifyqcq.q.dbval = PDS_CORE_DBELL_QID(pdsc->notifyqcq.q.hw_index);
365 static struct pdsc_viftype pdsc_viftype_defaults[] = {
366 [PDS_DEV_TYPE_VDPA] = { .name = PDS_DEV_TYPE_VDPA_STR,
367 .vif_id = PDS_DEV_TYPE_VDPA,
368 .dl_id = DEVLINK_PARAM_GENERIC_ID_ENABLE_VNET },
369 [PDS_DEV_TYPE_MAX] = {}
372 static int pdsc_viftypes_init(struct pdsc *pdsc)
374 enum pds_core_vif_types vt;
376 pdsc->viftype_status = kzalloc(sizeof(pdsc_viftype_defaults),
378 if (!pdsc->viftype_status)
381 for (vt = 0; vt < PDS_DEV_TYPE_MAX; vt++) {
384 if (!pdsc_viftype_defaults[vt].name)
387 /* Grab the defaults */
388 pdsc->viftype_status[vt] = pdsc_viftype_defaults[vt];
390 /* See what the Core device has for support */
391 vt_support = !!le16_to_cpu(pdsc->dev_ident.vif_types[vt]);
392 dev_dbg(pdsc->dev, "VIF %s is %ssupported\n",
393 pdsc->viftype_status[vt].name,
394 vt_support ? "" : "not ");
396 pdsc->viftype_status[vt].supported = vt_support;
402 int pdsc_setup(struct pdsc *pdsc, bool init)
407 err = pdsc_dev_init(pdsc);
411 /* Scale the descriptor ring length based on number of CPUs and VFs */
412 numdescs = max_t(int, PDSC_ADMINQ_MIN_LENGTH, num_online_cpus());
413 numdescs += 2 * pci_sriov_get_totalvfs(pdsc->pdev);
414 numdescs = roundup_pow_of_two(numdescs);
415 err = pdsc_qcq_alloc(pdsc, PDS_CORE_QTYPE_ADMINQ, 0, "adminq",
416 PDS_CORE_QCQ_F_CORE | PDS_CORE_QCQ_F_INTR,
418 sizeof(union pds_core_adminq_cmd),
419 sizeof(union pds_core_adminq_comp),
422 goto err_out_teardown;
424 err = pdsc_qcq_alloc(pdsc, PDS_CORE_QTYPE_NOTIFYQ, 0, "notifyq",
425 PDS_CORE_QCQ_F_NOTIFYQ,
427 sizeof(struct pds_core_notifyq_cmd),
428 sizeof(union pds_core_notifyq_comp),
429 0, &pdsc->notifyqcq);
431 goto err_out_teardown;
433 /* NotifyQ rides on the AdminQ interrupt */
434 pdsc->notifyqcq.intx = pdsc->adminqcq.intx;
436 /* Set up the Core with the AdminQ and NotifyQ info */
437 err = pdsc_core_init(pdsc);
439 goto err_out_teardown;
441 /* Set up the VIFs */
443 err = pdsc_viftypes_init(pdsc);
445 goto err_out_teardown;
447 pdsc_debugfs_add_viftype(pdsc);
450 refcount_set(&pdsc->adminq_refcnt, 1);
451 clear_bit(PDSC_S_FW_DEAD, &pdsc->state);
455 pdsc_teardown(pdsc, init);
459 void pdsc_teardown(struct pdsc *pdsc, bool removing)
463 if (!pdsc->pdev->is_virtfn)
464 pdsc_devcmd_reset(pdsc);
465 if (pdsc->adminqcq.work.func)
466 cancel_work_sync(&pdsc->adminqcq.work);
467 pdsc_qcq_free(pdsc, &pdsc->notifyqcq);
468 pdsc_qcq_free(pdsc, &pdsc->adminqcq);
471 kfree(pdsc->viftype_status);
472 pdsc->viftype_status = NULL;
475 if (pdsc->intr_info) {
476 for (i = 0; i < pdsc->nintrs; i++)
477 pdsc_intr_free(pdsc, i);
479 kfree(pdsc->intr_info);
480 pdsc->intr_info = NULL;
484 if (pdsc->kern_dbpage) {
485 iounmap(pdsc->kern_dbpage);
486 pdsc->kern_dbpage = NULL;
489 pci_free_irq_vectors(pdsc->pdev);
490 set_bit(PDSC_S_FW_DEAD, &pdsc->state);
493 int pdsc_start(struct pdsc *pdsc)
495 pds_core_intr_mask(&pdsc->intr_ctrl[pdsc->adminqcq.intx],
496 PDS_CORE_INTR_MASK_CLEAR);
501 void pdsc_stop(struct pdsc *pdsc)
505 if (!pdsc->intr_info)
508 /* Mask interrupts that are in use */
509 for (i = 0; i < pdsc->nintrs; i++)
510 if (pdsc->intr_info[i].vector)
511 pds_core_intr_mask(&pdsc->intr_ctrl[i],
512 PDS_CORE_INTR_MASK_SET);
515 static void pdsc_adminq_wait_and_dec_once_unused(struct pdsc *pdsc)
517 /* The driver initializes the adminq_refcnt to 1 when the adminq is
518 * allocated and ready for use. Other users/requesters will increment
519 * the refcnt while in use. If the refcnt is down to 1 then the adminq
520 * is not in use and the refcnt can be cleared and adminq freed. Before
521 * calling this function the driver will set PDSC_S_FW_DEAD, which
522 * prevent subsequent attempts to use the adminq and increment the
523 * refcnt to fail. This guarantees that this function will eventually
526 while (!refcount_dec_if_one(&pdsc->adminq_refcnt)) {
527 dev_dbg_ratelimited(pdsc->dev, "%s: adminq in use\n",
533 void pdsc_fw_down(struct pdsc *pdsc)
535 union pds_core_notifyq_comp reset_event = {
536 .reset.ecode = cpu_to_le16(PDS_EVENT_RESET),
540 if (test_and_set_bit(PDSC_S_FW_DEAD, &pdsc->state)) {
541 dev_warn(pdsc->dev, "%s: already happening\n", __func__);
545 if (pdsc->pdev->is_virtfn)
548 pdsc_adminq_wait_and_dec_once_unused(pdsc);
550 /* Notify clients of fw_down */
551 if (pdsc->fw_reporter)
552 devlink_health_report(pdsc->fw_reporter, "FW down reported", pdsc);
553 pdsc_notify(PDS_EVENT_RESET, &reset_event);
556 pdsc_teardown(pdsc, PDSC_TEARDOWN_RECOVERY);
559 void pdsc_fw_up(struct pdsc *pdsc)
561 union pds_core_notifyq_comp reset_event = {
562 .reset.ecode = cpu_to_le16(PDS_EVENT_RESET),
567 if (!test_bit(PDSC_S_FW_DEAD, &pdsc->state)) {
568 dev_err(pdsc->dev, "%s: fw not dead\n", __func__);
572 if (pdsc->pdev->is_virtfn) {
573 clear_bit(PDSC_S_FW_DEAD, &pdsc->state);
577 err = pdsc_setup(pdsc, PDSC_SETUP_RECOVERY);
581 err = pdsc_start(pdsc);
585 /* Notify clients of fw_up */
586 pdsc->fw_recoveries++;
587 if (pdsc->fw_reporter)
588 devlink_health_reporter_state_update(pdsc->fw_reporter,
589 DEVLINK_HEALTH_REPORTER_STATE_HEALTHY);
590 pdsc_notify(PDS_EVENT_RESET, &reset_event);
595 pdsc_teardown(pdsc, PDSC_TEARDOWN_RECOVERY);
598 void pdsc_pci_reset_thread(struct work_struct *work)
600 struct pdsc *pdsc = container_of(work, struct pdsc, pci_reset_work);
601 struct pci_dev *pdev = pdsc->pdev;
604 pci_reset_function(pdev);
608 static void pdsc_check_pci_health(struct pdsc *pdsc)
612 /* some sort of teardown already in progress */
613 if (!pdsc->info_regs)
616 fw_status = ioread8(&pdsc->info_regs->fw_status);
619 if (fw_status != PDS_RC_BAD_PCI)
622 /* prevent deadlock between pdsc_reset_prepare and pdsc_health_thread */
623 queue_work(pdsc->wq, &pdsc->pci_reset_work);
626 void pdsc_health_thread(struct work_struct *work)
628 struct pdsc *pdsc = container_of(work, struct pdsc, health_work);
632 mutex_lock(&pdsc->config_lock);
634 /* Don't do a check when in a transition state */
635 mask = BIT_ULL(PDSC_S_INITING_DRIVER) |
636 BIT_ULL(PDSC_S_STOPPING_DRIVER);
637 if (pdsc->state & mask)
640 healthy = pdsc_is_fw_good(pdsc);
641 dev_dbg(pdsc->dev, "%s: health %d fw_status %#02x fw_heartbeat %d\n",
642 __func__, healthy, pdsc->fw_status, pdsc->last_hb);
644 if (test_bit(PDSC_S_FW_DEAD, &pdsc->state)) {
652 pdsc_check_pci_health(pdsc);
654 pdsc->fw_generation = pdsc->fw_status & PDS_CORE_FW_STS_F_GENERATION;
657 mutex_unlock(&pdsc->config_lock);