1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright(c) 2019 Intel Corporation. All rights rsvd. */
3 #include <linux/init.h>
4 #include <linux/kernel.h>
5 #include <linux/module.h>
7 #include <uapi/linux/idxd.h>
11 static struct idxd_desc *__get_desc(struct idxd_wq *wq, int idx, int cpu)
13 struct idxd_desc *desc;
14 struct idxd_device *idxd = wq->idxd;
16 desc = wq->descs[idx];
17 memset(desc->hw, 0, sizeof(struct dsa_hw_desc));
18 memset(desc->completion, 0, idxd->data->compl_size);
21 if (device_pasid_enabled(idxd))
22 desc->hw->pasid = idxd->pasid;
25 * On host, MSIX vecotr 0 is used for misc interrupt. Therefore when we match
26 * vector 1:1 to the WQ id, we need to add 1
28 if (!idxd->int_handles)
29 desc->hw->int_handle = wq->id + 1;
31 desc->hw->int_handle = idxd->int_handles[wq->id];
36 struct idxd_desc *idxd_alloc_desc(struct idxd_wq *wq, enum idxd_op_type optype)
39 struct idxd_device *idxd = wq->idxd;
40 DEFINE_SBQ_WAIT(wait);
41 struct sbq_wait_state *ws;
42 struct sbitmap_queue *sbq;
44 if (idxd->state != IDXD_DEV_ENABLED)
48 idx = sbitmap_queue_get(sbq, &cpu);
50 if (optype == IDXD_OP_NONBLOCK)
51 return ERR_PTR(-EAGAIN);
53 return __get_desc(wq, idx, cpu);
58 sbitmap_prepare_to_wait(sbq, ws, &wait, TASK_INTERRUPTIBLE);
59 if (signal_pending_state(TASK_INTERRUPTIBLE, current))
61 idx = sbitmap_queue_get(sbq, &cpu);
67 sbitmap_finish_wait(sbq, ws, &wait);
69 return ERR_PTR(-EAGAIN);
71 return __get_desc(wq, idx, cpu);
74 void idxd_free_desc(struct idxd_wq *wq, struct idxd_desc *desc)
79 sbitmap_queue_clear(&wq->sbq, desc->id, cpu);
82 static struct idxd_desc *list_abort_desc(struct idxd_wq *wq, struct idxd_irq_entry *ie,
83 struct idxd_desc *desc)
85 struct idxd_desc *d, *n;
87 lockdep_assert_held(&ie->list_lock);
88 list_for_each_entry_safe(d, n, &ie->work_list, list) {
96 * At this point, the desc needs to be aborted is held by the completion
97 * handler where it has taken it off the pending list but has not added to the
98 * work list. It will be cleaned up by the interrupt handler when it sees the
99 * IDXD_COMP_DESC_ABORT for completion status.
104 static void llist_abort_desc(struct idxd_wq *wq, struct idxd_irq_entry *ie,
105 struct idxd_desc *desc)
107 struct idxd_desc *d, *t, *found = NULL;
108 struct llist_node *head;
111 desc->completion->status = IDXD_COMP_DESC_ABORT;
113 * Grab the list lock so it will block the irq thread handler. This allows the
114 * abort code to locate the descriptor need to be aborted.
116 spin_lock(&ie->list_lock);
117 head = llist_del_all(&ie->pending_llist);
119 llist_for_each_entry_safe(d, t, head, llnode) {
125 if (d->completion->status)
126 list_add_tail(&d->list, &flist);
128 list_add_tail(&d->list, &ie->work_list);
133 found = list_abort_desc(wq, ie, desc);
134 spin_unlock(&ie->list_lock);
137 complete_desc(found, IDXD_COMPLETE_ABORT);
140 * complete_desc() will return desc to allocator and the desc can be
141 * acquired by a different process and the desc->list can be modified.
142 * Delete desc from list so the list trasversing does not get corrupted
143 * by the other process.
145 list_for_each_entry_safe(d, t, &flist, list) {
146 list_del_init(&d->list);
147 complete_desc(d, IDXD_COMPLETE_NORMAL);
151 int idxd_submit_desc(struct idxd_wq *wq, struct idxd_desc *desc)
153 struct idxd_device *idxd = wq->idxd;
154 struct idxd_irq_entry *ie = NULL;
155 void __iomem *portal;
158 if (idxd->state != IDXD_DEV_ENABLED) {
159 idxd_free_desc(wq, desc);
163 if (!percpu_ref_tryget_live(&wq->wq_active)) {
164 idxd_free_desc(wq, desc);
168 portal = idxd_wq_portal_addr(wq);
171 * The wmb() flushes writes to coherent DMA data before
172 * possibly triggering a DMA read. The wmb() is necessary
173 * even on UP because the recipient is a device.
178 * Pending the descriptor to the lockless list for the irq_entry
179 * that we designated the descriptor to.
181 if (desc->hw->flags & IDXD_OP_FLAG_RCI) {
182 ie = &idxd->irq_entries[wq->id + 1];
183 llist_add(&desc->llnode, &ie->pending_llist);
186 if (wq_dedicated(wq)) {
187 iosubmit_cmds512(portal, desc->hw, 1);
190 * It's not likely that we would receive queue full rejection
191 * since the descriptor allocation gates at wq size. If we
192 * receive a -EAGAIN, that means something went wrong such as the
193 * device is not accepting descriptor at all.
195 rc = enqcmds(portal, desc->hw);
197 percpu_ref_put(&wq->wq_active);
198 /* abort operation frees the descriptor */
200 llist_abort_desc(wq, ie, desc);
202 idxd_free_desc(wq, desc);
207 percpu_ref_put(&wq->wq_active);