1 // SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
3 * Copyright(c) 2016 - 2018 Intel Corporation.
6 #include <linux/slab.h>
7 #include <linux/vmalloc.h>
8 #include <rdma/uverbs_ioctl.h>
13 static struct workqueue_struct
*comp_vector_wq
;
16 * rvt_cq_enter - add a new entry to the completion queue
17 * @cq: completion queue
18 * @entry: work completion entry to add
19 * @solicited: true if @entry is solicited
21 * This may be called with qp->s_lock held.
23 * Return: return true on success, else return
24 * false if cq is full.
26 bool rvt_cq_enter(struct rvt_cq
*cq
, struct ib_wc
*entry
, bool solicited
)
28 struct ib_uverbs_wc
*uqueue
= NULL
;
29 struct ib_wc
*kqueue
= NULL
;
30 struct rvt_cq_wc
*u_wc
= NULL
;
31 struct rvt_k_cq_wc
*k_wc
= NULL
;
37 spin_lock_irqsave(&cq
->lock
, flags
);
41 uqueue
= &u_wc
->uqueue
[0];
42 head
= RDMA_READ_UAPI_ATOMIC(u_wc
->head
);
43 tail
= RDMA_READ_UAPI_ATOMIC(u_wc
->tail
);
46 kqueue
= &k_wc
->kqueue
[0];
52 * Note that the head pointer might be writable by
53 * user processes.Take care to verify it is a sane value.
55 if (head
>= (unsigned)cq
->ibcq
.cqe
) {
62 if (unlikely(next
== tail
|| cq
->cq_full
)) {
63 struct rvt_dev_info
*rdi
= cq
->rdi
;
66 rvt_pr_err_ratelimited(rdi
, "CQ is full!\n");
68 spin_unlock_irqrestore(&cq
->lock
, flags
);
69 if (cq
->ibcq
.event_handler
) {
72 ev
.device
= cq
->ibcq
.device
;
73 ev
.element
.cq
= &cq
->ibcq
;
74 ev
.event
= IB_EVENT_CQ_ERR
;
75 cq
->ibcq
.event_handler(&ev
, cq
->ibcq
.cq_context
);
79 trace_rvt_cq_enter(cq
, entry
, head
);
81 uqueue
[head
].wr_id
= entry
->wr_id
;
82 uqueue
[head
].status
= entry
->status
;
83 uqueue
[head
].opcode
= entry
->opcode
;
84 uqueue
[head
].vendor_err
= entry
->vendor_err
;
85 uqueue
[head
].byte_len
= entry
->byte_len
;
86 uqueue
[head
].ex
.imm_data
= entry
->ex
.imm_data
;
87 uqueue
[head
].qp_num
= entry
->qp
->qp_num
;
88 uqueue
[head
].src_qp
= entry
->src_qp
;
89 uqueue
[head
].wc_flags
= entry
->wc_flags
;
90 uqueue
[head
].pkey_index
= entry
->pkey_index
;
91 uqueue
[head
].slid
= ib_lid_cpu16(entry
->slid
);
92 uqueue
[head
].sl
= entry
->sl
;
93 uqueue
[head
].dlid_path_bits
= entry
->dlid_path_bits
;
94 uqueue
[head
].port_num
= entry
->port_num
;
95 /* Make sure entry is written before the head index. */
96 RDMA_WRITE_UAPI_ATOMIC(u_wc
->head
, next
);
98 kqueue
[head
] = *entry
;
102 if (cq
->notify
== IB_CQ_NEXT_COMP
||
103 (cq
->notify
== IB_CQ_SOLICITED
&&
104 (solicited
|| entry
->status
!= IB_WC_SUCCESS
))) {
106 * This will cause send_complete() to be called in
109 cq
->notify
= RVT_CQ_NONE
;
111 queue_work_on(cq
->comp_vector_cpu
, comp_vector_wq
,
115 spin_unlock_irqrestore(&cq
->lock
, flags
);
118 EXPORT_SYMBOL(rvt_cq_enter
);
120 static void send_complete(struct work_struct
*work
)
122 struct rvt_cq
*cq
= container_of(work
, struct rvt_cq
, comptask
);
125 * The completion handler will most likely rearm the notification
126 * and poll for all pending entries. If a new completion entry
127 * is added while we are in this routine, queue_work()
128 * won't call us again until we return so we check triggered to
129 * see if we need to call the handler again.
132 u8 triggered
= cq
->triggered
;
135 * IPoIB connected mode assumes the callback is from a
136 * soft IRQ. We simulate this by blocking "bottom halves".
137 * See the implementation for ipoib_cm_handle_tx_wc(),
138 * netif_tx_lock_bh() and netif_tx_lock().
141 cq
->ibcq
.comp_handler(&cq
->ibcq
, cq
->ibcq
.cq_context
);
144 if (cq
->triggered
== triggered
)
150 * rvt_create_cq - create a completion queue
151 * @ibcq: Allocated CQ
152 * @attr: creation attributes
153 * @attrs: uverbs bundle
155 * Called by ib_create_cq() in the generic verbs code.
157 * Return: 0 on success
159 int rvt_create_cq(struct ib_cq
*ibcq
, const struct ib_cq_init_attr
*attr
,
160 struct uverbs_attr_bundle
*attrs
)
162 struct ib_udata
*udata
= &attrs
->driver_udata
;
163 struct ib_device
*ibdev
= ibcq
->device
;
164 struct rvt_dev_info
*rdi
= ib_to_rvt(ibdev
);
165 struct rvt_cq
*cq
= ibcq_to_rvtcq(ibcq
);
166 struct rvt_cq_wc
*u_wc
= NULL
;
167 struct rvt_k_cq_wc
*k_wc
= NULL
;
169 unsigned int entries
= attr
->cqe
;
170 int comp_vector
= attr
->comp_vector
;
176 if (entries
< 1 || entries
> rdi
->dparms
.props
.max_cqe
)
182 comp_vector
= comp_vector
% rdi
->ibdev
.num_comp_vectors
;
185 * Allocate the completion queue entries and head/tail pointers.
186 * This is allocated separately so that it can be resized and
187 * also mapped into user space.
188 * We need to use vmalloc() in order to support mmap and large
189 * numbers of entries.
191 if (udata
&& udata
->outlen
>= sizeof(__u64
)) {
192 sz
= sizeof(struct ib_uverbs_wc
) * (entries
+ 1);
194 u_wc
= vmalloc_user(sz
);
198 sz
= sizeof(struct ib_wc
) * (entries
+ 1);
200 k_wc
= vzalloc_node(sz
, rdi
->dparms
.node
);
206 * Return the address of the WC as the offset to mmap.
207 * See rvt_mmap() for details.
209 if (udata
&& udata
->outlen
>= sizeof(__u64
)) {
210 cq
->ip
= rvt_create_mmap_info(rdi
, sz
, udata
, u_wc
);
211 if (IS_ERR(cq
->ip
)) {
212 err
= PTR_ERR(cq
->ip
);
216 err
= ib_copy_to_udata(udata
, &cq
->ip
->offset
,
217 sizeof(cq
->ip
->offset
));
222 spin_lock_irq(&rdi
->n_cqs_lock
);
223 if (rdi
->n_cqs_allocated
== rdi
->dparms
.props
.max_cq
) {
224 spin_unlock_irq(&rdi
->n_cqs_lock
);
229 rdi
->n_cqs_allocated
++;
230 spin_unlock_irq(&rdi
->n_cqs_lock
);
233 spin_lock_irq(&rdi
->pending_lock
);
234 list_add(&cq
->ip
->pending_mmaps
, &rdi
->pending_mmaps
);
235 spin_unlock_irq(&rdi
->pending_lock
);
239 * ib_create_cq() will initialize cq->ibcq except for cq->ibcq.cqe.
240 * The number of entries should be >= the number requested or return
244 if (rdi
->driver_f
.comp_vect_cpu_lookup
)
245 cq
->comp_vector_cpu
=
246 rdi
->driver_f
.comp_vect_cpu_lookup(rdi
, comp_vector
);
248 cq
->comp_vector_cpu
=
249 cpumask_first(cpumask_of_node(rdi
->dparms
.node
));
251 cq
->ibcq
.cqe
= entries
;
252 cq
->notify
= RVT_CQ_NONE
;
253 spin_lock_init(&cq
->lock
);
254 INIT_WORK(&cq
->comptask
, send_complete
);
260 trace_rvt_create_cq(cq
, attr
);
272 * rvt_destroy_cq - destroy a completion queue
273 * @ibcq: the completion queue to destroy.
274 * @udata: user data or NULL for kernel object
276 * Called by ib_destroy_cq() in the generic verbs code.
278 int rvt_destroy_cq(struct ib_cq
*ibcq
, struct ib_udata
*udata
)
280 struct rvt_cq
*cq
= ibcq_to_rvtcq(ibcq
);
281 struct rvt_dev_info
*rdi
= cq
->rdi
;
283 flush_work(&cq
->comptask
);
284 spin_lock_irq(&rdi
->n_cqs_lock
);
285 rdi
->n_cqs_allocated
--;
286 spin_unlock_irq(&rdi
->n_cqs_lock
);
288 kref_put(&cq
->ip
->ref
, rvt_release_mmap_info
);
295 * rvt_req_notify_cq - change the notification type for a completion queue
296 * @ibcq: the completion queue
297 * @notify_flags: the type of notification to request
299 * This may be called from interrupt context. Also called by
300 * ib_req_notify_cq() in the generic verbs code.
302 * Return: 0 for success.
304 int rvt_req_notify_cq(struct ib_cq
*ibcq
, enum ib_cq_notify_flags notify_flags
)
306 struct rvt_cq
*cq
= ibcq_to_rvtcq(ibcq
);
310 spin_lock_irqsave(&cq
->lock
, flags
);
312 * Don't change IB_CQ_NEXT_COMP to IB_CQ_SOLICITED but allow
313 * any other transitions (see C11-31 and C11-32 in ch. 11.4.2.2).
315 if (cq
->notify
!= IB_CQ_NEXT_COMP
)
316 cq
->notify
= notify_flags
& IB_CQ_SOLICITED_MASK
;
318 if (notify_flags
& IB_CQ_REPORT_MISSED_EVENTS
) {
320 if (RDMA_READ_UAPI_ATOMIC(cq
->queue
->head
) !=
321 RDMA_READ_UAPI_ATOMIC(cq
->queue
->tail
))
324 if (cq
->kqueue
->head
!= cq
->kqueue
->tail
)
329 spin_unlock_irqrestore(&cq
->lock
, flags
);
335 * rvt_resize_cq - change the size of the CQ
336 * @ibcq: the completion queue
338 * Return: 0 for success.
340 int rvt_resize_cq(struct ib_cq
*ibcq
, int cqe
, struct ib_udata
*udata
)
342 struct rvt_cq
*cq
= ibcq_to_rvtcq(ibcq
);
346 struct rvt_dev_info
*rdi
= cq
->rdi
;
347 struct rvt_cq_wc
*u_wc
= NULL
;
348 struct rvt_cq_wc
*old_u_wc
= NULL
;
349 struct rvt_k_cq_wc
*k_wc
= NULL
;
350 struct rvt_k_cq_wc
*old_k_wc
= NULL
;
352 if (cqe
< 1 || cqe
> rdi
->dparms
.props
.max_cqe
)
356 * Need to use vmalloc() if we want to support large #s of entries.
358 if (udata
&& udata
->outlen
>= sizeof(__u64
)) {
359 sz
= sizeof(struct ib_uverbs_wc
) * (cqe
+ 1);
361 u_wc
= vmalloc_user(sz
);
365 sz
= sizeof(struct ib_wc
) * (cqe
+ 1);
367 k_wc
= vzalloc_node(sz
, rdi
->dparms
.node
);
371 /* Check that we can write the offset to mmap. */
372 if (udata
&& udata
->outlen
>= sizeof(__u64
)) {
375 ret
= ib_copy_to_udata(udata
, &offset
, sizeof(offset
));
380 spin_lock_irq(&cq
->lock
);
382 * Make sure head and tail are sane since they
383 * might be user writable.
386 old_u_wc
= cq
->queue
;
387 head
= RDMA_READ_UAPI_ATOMIC(old_u_wc
->head
);
388 tail
= RDMA_READ_UAPI_ATOMIC(old_u_wc
->tail
);
390 old_k_wc
= cq
->kqueue
;
391 head
= old_k_wc
->head
;
392 tail
= old_k_wc
->tail
;
395 if (head
> (u32
)cq
->ibcq
.cqe
)
396 head
= (u32
)cq
->ibcq
.cqe
;
397 if (tail
> (u32
)cq
->ibcq
.cqe
)
398 tail
= (u32
)cq
->ibcq
.cqe
;
400 n
= cq
->ibcq
.cqe
+ 1 + head
- tail
;
403 if (unlikely((u32
)cqe
< n
)) {
407 for (n
= 0; tail
!= head
; n
++) {
409 u_wc
->uqueue
[n
] = old_u_wc
->uqueue
[tail
];
411 k_wc
->kqueue
[n
] = old_k_wc
->kqueue
[tail
];
412 if (tail
== (u32
)cq
->ibcq
.cqe
)
419 RDMA_WRITE_UAPI_ATOMIC(u_wc
->head
, n
);
420 RDMA_WRITE_UAPI_ATOMIC(u_wc
->tail
, 0);
427 spin_unlock_irq(&cq
->lock
);
435 struct rvt_mmap_info
*ip
= cq
->ip
;
437 rvt_update_mmap_info(rdi
, ip
, sz
, u_wc
);
440 * Return the offset to mmap.
441 * See rvt_mmap() for details.
443 if (udata
&& udata
->outlen
>= sizeof(__u64
)) {
444 ret
= ib_copy_to_udata(udata
, &ip
->offset
,
450 spin_lock_irq(&rdi
->pending_lock
);
451 if (list_empty(&ip
->pending_mmaps
))
452 list_add(&ip
->pending_mmaps
, &rdi
->pending_mmaps
);
453 spin_unlock_irq(&rdi
->pending_lock
);
459 spin_unlock_irq(&cq
->lock
);
468 * rvt_poll_cq - poll for work completion entries
469 * @ibcq: the completion queue to poll
470 * @num_entries: the maximum number of entries to return
471 * @entry: pointer to array where work completions are placed
473 * This may be called from interrupt context. Also called by ib_poll_cq()
474 * in the generic verbs code.
476 * Return: the number of completion entries polled.
478 int rvt_poll_cq(struct ib_cq
*ibcq
, int num_entries
, struct ib_wc
*entry
)
480 struct rvt_cq
*cq
= ibcq_to_rvtcq(ibcq
);
481 struct rvt_k_cq_wc
*wc
;
486 /* The kernel can only poll a kernel completion queue */
490 spin_lock_irqsave(&cq
->lock
, flags
);
494 if (tail
> (u32
)cq
->ibcq
.cqe
)
495 tail
= (u32
)cq
->ibcq
.cqe
;
496 for (npolled
= 0; npolled
< num_entries
; ++npolled
, ++entry
) {
497 if (tail
== wc
->head
)
499 /* The kernel doesn't need a RMB since it has the lock. */
500 trace_rvt_cq_poll(cq
, &wc
->kqueue
[tail
], npolled
);
501 *entry
= wc
->kqueue
[tail
];
502 if (tail
>= cq
->ibcq
.cqe
)
509 spin_unlock_irqrestore(&cq
->lock
, flags
);
515 * rvt_driver_cq_init - Init cq resources on behalf of driver
517 * Return: 0 on success
519 int rvt_driver_cq_init(void)
521 comp_vector_wq
= alloc_workqueue("%s", WQ_HIGHPRI
| WQ_CPU_INTENSIVE
,
530 * rvt_cq_exit - tear down cq reources
532 void rvt_cq_exit(void)
534 destroy_workqueue(comp_vector_wq
);
535 comp_vector_wq
= NULL
;