1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright(c) 2023 Advanced Micro Devices, Inc */
4 #include <linux/dynamic_debug.h>
8 struct pdsc_wait_context {
10 struct completion wait_completion;
13 static int pdsc_process_notifyq(struct pdsc_qcq *qcq)
15 union pds_core_notifyq_comp *comp;
16 struct pdsc *pdsc = qcq->pdsc;
17 struct pdsc_cq *cq = &qcq->cq;
18 struct pdsc_cq_info *cq_info;
22 cq_info = &cq->info[cq->tail_idx];
24 eid = le64_to_cpu(comp->event.eid);
25 while (eid > pdsc->last_eid) {
26 u16 ecode = le16_to_cpu(comp->event.ecode);
29 case PDS_EVENT_LINK_CHANGE:
30 dev_info(pdsc->dev, "NotifyQ LINK_CHANGE ecode %d eid %lld\n",
32 pdsc_notify(PDS_EVENT_LINK_CHANGE, comp);
36 dev_info(pdsc->dev, "NotifyQ RESET ecode %d eid %lld\n",
38 pdsc_notify(PDS_EVENT_RESET, comp);
42 dev_info(pdsc->dev, "NotifyQ XCVR ecode %d eid %lld\n",
47 dev_info(pdsc->dev, "NotifyQ ecode %d eid %lld\n",
53 cq->tail_idx = (cq->tail_idx + 1) & (cq->num_descs - 1);
54 cq_info = &cq->info[cq->tail_idx];
56 eid = le64_to_cpu(comp->event.eid);
61 qcq->accum_work += nq_work;
66 static bool pdsc_adminq_inc_if_up(struct pdsc *pdsc)
68 if (pdsc->state & BIT_ULL(PDSC_S_STOPPING_DRIVER) ||
69 pdsc->state & BIT_ULL(PDSC_S_FW_DEAD))
72 return refcount_inc_not_zero(&pdsc->adminq_refcnt);
75 void pdsc_process_adminq(struct pdsc_qcq *qcq)
77 union pds_core_adminq_comp *comp;
78 struct pdsc_queue *q = &qcq->q;
79 struct pdsc *pdsc = qcq->pdsc;
80 struct pdsc_cq *cq = &qcq->cq;
81 struct pdsc_q_info *q_info;
82 unsigned long irqflags;
87 /* Don't process AdminQ when it's not up */
88 if (!pdsc_adminq_inc_if_up(pdsc)) {
89 dev_err(pdsc->dev, "%s: called while adminq is unavailable\n",
94 /* Check for NotifyQ event */
95 nq_work = pdsc_process_notifyq(&pdsc->notifyqcq);
97 /* Check for empty queue, which can happen if the interrupt was
98 * for a NotifyQ event and there are no new AdminQ completions.
100 if (q->tail_idx == q->head_idx)
103 /* Find the first completion to clean,
104 * run the callback in the related q_info,
105 * and continue while we still match done color
107 spin_lock_irqsave(&pdsc->adminq_lock, irqflags);
108 comp = cq->info[cq->tail_idx].comp;
109 while (pdsc_color_match(comp->color, cq->done_color)) {
110 q_info = &q->info[q->tail_idx];
111 q->tail_idx = (q->tail_idx + 1) & (q->num_descs - 1);
113 /* Copy out the completion data */
114 memcpy(q_info->dest, comp, sizeof(*comp));
116 complete_all(&q_info->wc->wait_completion);
118 if (cq->tail_idx == cq->num_descs - 1)
119 cq->done_color = !cq->done_color;
120 cq->tail_idx = (cq->tail_idx + 1) & (cq->num_descs - 1);
121 comp = cq->info[cq->tail_idx].comp;
125 spin_unlock_irqrestore(&pdsc->adminq_lock, irqflags);
127 qcq->accum_work += aq_work;
130 /* Return the interrupt credits, one for each completion */
131 credits = nq_work + aq_work;
133 pds_core_intr_credits(&pdsc->intr_ctrl[qcq->intx],
135 PDS_CORE_INTR_CRED_REARM);
136 refcount_dec(&pdsc->adminq_refcnt);
139 void pdsc_work_thread(struct work_struct *work)
141 struct pdsc_qcq *qcq = container_of(work, struct pdsc_qcq, work);
143 pdsc_process_adminq(qcq);
146 irqreturn_t pdsc_adminq_isr(int irq, void *data)
148 struct pdsc *pdsc = data;
149 struct pdsc_qcq *qcq;
151 /* Don't process AdminQ when it's not up */
152 if (!pdsc_adminq_inc_if_up(pdsc)) {
153 dev_err(pdsc->dev, "%s: called while adminq is unavailable\n",
158 qcq = &pdsc->adminqcq;
159 queue_work(pdsc->wq, &qcq->work);
160 pds_core_intr_mask(&pdsc->intr_ctrl[qcq->intx], PDS_CORE_INTR_MASK_CLEAR);
161 refcount_dec(&pdsc->adminq_refcnt);
166 static int __pdsc_adminq_post(struct pdsc *pdsc,
167 struct pdsc_qcq *qcq,
168 union pds_core_adminq_cmd *cmd,
169 union pds_core_adminq_comp *comp,
170 struct pdsc_wait_context *wc)
172 struct pdsc_queue *q = &qcq->q;
173 struct pdsc_q_info *q_info;
174 unsigned long irqflags;
179 spin_lock_irqsave(&pdsc->adminq_lock, irqflags);
181 /* Check for space in the queue */
183 if (q->head_idx >= avail)
184 avail += q->num_descs - q->head_idx - 1;
186 avail -= q->head_idx + 1;
192 /* Check that the FW is running */
193 if (!pdsc_is_fw_running(pdsc)) {
194 if (pdsc->info_regs) {
196 ioread8(&pdsc->info_regs->fw_status);
198 dev_info(pdsc->dev, "%s: post failed - fw not running %#02x:\n",
199 __func__, fw_status);
201 dev_info(pdsc->dev, "%s: post failed - BARs not setup\n",
209 /* Post the request */
211 q_info = &q->info[index];
214 memcpy(q_info->desc, cmd, sizeof(*cmd));
216 dev_dbg(pdsc->dev, "head_idx %d tail_idx %d\n",
217 q->head_idx, q->tail_idx);
218 dev_dbg(pdsc->dev, "post admin queue command:\n");
219 dynamic_hex_dump("cmd ", DUMP_PREFIX_OFFSET, 16, 1,
220 cmd, sizeof(*cmd), true);
222 q->head_idx = (q->head_idx + 1) & (q->num_descs - 1);
224 pds_core_dbell_ring(pdsc->kern_dbpage,
225 q->hw_type, q->dbval | q->head_idx);
229 spin_unlock_irqrestore(&pdsc->adminq_lock, irqflags);
233 int pdsc_adminq_post(struct pdsc *pdsc,
234 union pds_core_adminq_cmd *cmd,
235 union pds_core_adminq_comp *comp,
238 struct pdsc_wait_context wc = {
240 COMPLETION_INITIALIZER_ONSTACK(wc.wait_completion),
242 unsigned long poll_interval = 1;
243 unsigned long poll_jiffies;
244 unsigned long time_limit;
245 unsigned long time_start;
246 unsigned long time_done;
247 unsigned long remaining;
251 if (!pdsc_adminq_inc_if_up(pdsc)) {
252 dev_dbg(pdsc->dev, "%s: preventing adminq cmd %u\n",
253 __func__, cmd->opcode);
257 wc.qcq = &pdsc->adminqcq;
258 index = __pdsc_adminq_post(pdsc, &pdsc->adminqcq, cmd, comp, &wc);
264 time_start = jiffies;
265 time_limit = time_start + HZ * pdsc->devcmd_timeout;
267 /* Timeslice the actual wait to catch IO errors etc early */
268 poll_jiffies = msecs_to_jiffies(poll_interval);
269 remaining = wait_for_completion_timeout(&wc.wait_completion,
274 if (!pdsc_is_fw_running(pdsc)) {
275 if (pdsc->info_regs) {
277 ioread8(&pdsc->info_regs->fw_status);
279 dev_dbg(pdsc->dev, "%s: post wait failed - fw not running %#02x:\n",
280 __func__, fw_status);
282 dev_dbg(pdsc->dev, "%s: post wait failed - BARs not setup\n",
289 /* When fast_poll is not requested, prevent aggressive polling
290 * on failures due to timeouts by doing exponential back off.
292 if (!fast_poll && poll_interval < PDSC_ADMINQ_MAX_POLL_INTERVAL)
294 } while (time_before(jiffies, time_limit));
296 dev_dbg(pdsc->dev, "%s: elapsed %d msecs\n",
297 __func__, jiffies_to_msecs(time_done - time_start));
299 /* Check the results */
300 if (time_after_eq(time_done, time_limit))
303 dev_dbg(pdsc->dev, "read admin queue completion idx %d:\n", index);
304 dynamic_hex_dump("comp ", DUMP_PREFIX_OFFSET, 16, 1,
305 comp, sizeof(*comp), true);
307 if (remaining && comp->status)
308 err = pdsc_err_to_errno(comp->status);
312 dev_dbg(pdsc->dev, "%s: opcode %d status %d err %pe\n",
313 __func__, cmd->opcode, comp->status, ERR_PTR(err));
314 if (err == -ENXIO || err == -ETIMEDOUT)
315 queue_work(pdsc->wq, &pdsc->health_work);
318 refcount_dec(&pdsc->adminq_refcnt);
322 EXPORT_SYMBOL_GPL(pdsc_adminq_post);