2 * Copyright(c) 2016 - 2018 Intel Corporation.
4 * This file is provided under a dual BSD/GPLv2 license. When using or
5 * redistributing this file, you may do so under either license.
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of version 2 of the GNU General Public License as
11 * published by the Free Software Foundation.
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
20 * Redistribution and use in source and binary forms, with or without
21 * modification, are permitted provided that the following conditions
24 * - Redistributions of source code must retain the above copyright
25 * notice, this list of conditions and the following disclaimer.
26 * - Redistributions in binary form must reproduce the above copyright
27 * notice, this list of conditions and the following disclaimer in
28 * the documentation and/or other materials provided with the
30 * - Neither the name of Intel Corporation nor the names of its
31 * contributors may be used to endorse or promote products derived
32 * from this software without specific prior written permission.
34 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
35 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
36 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
37 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
38 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
39 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
40 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
41 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
42 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
43 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
44 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
48 #include <linux/slab.h>
49 #include <linux/vmalloc.h>
54 static struct workqueue_struct
*comp_vector_wq
;
57 * rvt_cq_enter - add a new entry to the completion queue
58 * @cq: completion queue
59 * @entry: work completion entry to add
60 * @solicited: true if @entry is solicited
62 * This may be called with qp->s_lock held.
64 * Return: return true on success, else return
65 * false if cq is full.
67 bool rvt_cq_enter(struct rvt_cq
*cq
, struct ib_wc
*entry
, bool solicited
)
69 struct ib_uverbs_wc
*uqueue
= NULL
;
70 struct ib_wc
*kqueue
= NULL
;
71 struct rvt_cq_wc
*u_wc
= NULL
;
72 struct rvt_k_cq_wc
*k_wc
= NULL
;
78 spin_lock_irqsave(&cq
->lock
, flags
);
82 uqueue
= &u_wc
->uqueue
[0];
83 head
= RDMA_READ_UAPI_ATOMIC(u_wc
->head
);
84 tail
= RDMA_READ_UAPI_ATOMIC(u_wc
->tail
);
87 kqueue
= &k_wc
->kqueue
[0];
93 * Note that the head pointer might be writable by
94 * user processes.Take care to verify it is a sane value.
96 if (head
>= (unsigned)cq
->ibcq
.cqe
) {
103 if (unlikely(next
== tail
|| cq
->cq_full
)) {
104 struct rvt_dev_info
*rdi
= cq
->rdi
;
107 rvt_pr_err_ratelimited(rdi
, "CQ is full!\n");
109 spin_unlock_irqrestore(&cq
->lock
, flags
);
110 if (cq
->ibcq
.event_handler
) {
113 ev
.device
= cq
->ibcq
.device
;
114 ev
.element
.cq
= &cq
->ibcq
;
115 ev
.event
= IB_EVENT_CQ_ERR
;
116 cq
->ibcq
.event_handler(&ev
, cq
->ibcq
.cq_context
);
120 trace_rvt_cq_enter(cq
, entry
, head
);
122 uqueue
[head
].wr_id
= entry
->wr_id
;
123 uqueue
[head
].status
= entry
->status
;
124 uqueue
[head
].opcode
= entry
->opcode
;
125 uqueue
[head
].vendor_err
= entry
->vendor_err
;
126 uqueue
[head
].byte_len
= entry
->byte_len
;
127 uqueue
[head
].ex
.imm_data
= entry
->ex
.imm_data
;
128 uqueue
[head
].qp_num
= entry
->qp
->qp_num
;
129 uqueue
[head
].src_qp
= entry
->src_qp
;
130 uqueue
[head
].wc_flags
= entry
->wc_flags
;
131 uqueue
[head
].pkey_index
= entry
->pkey_index
;
132 uqueue
[head
].slid
= ib_lid_cpu16(entry
->slid
);
133 uqueue
[head
].sl
= entry
->sl
;
134 uqueue
[head
].dlid_path_bits
= entry
->dlid_path_bits
;
135 uqueue
[head
].port_num
= entry
->port_num
;
136 /* Make sure entry is written before the head index. */
137 RDMA_WRITE_UAPI_ATOMIC(u_wc
->head
, next
);
139 kqueue
[head
] = *entry
;
143 if (cq
->notify
== IB_CQ_NEXT_COMP
||
144 (cq
->notify
== IB_CQ_SOLICITED
&&
145 (solicited
|| entry
->status
!= IB_WC_SUCCESS
))) {
147 * This will cause send_complete() to be called in
150 cq
->notify
= RVT_CQ_NONE
;
152 queue_work_on(cq
->comp_vector_cpu
, comp_vector_wq
,
156 spin_unlock_irqrestore(&cq
->lock
, flags
);
159 EXPORT_SYMBOL(rvt_cq_enter
);
161 static void send_complete(struct work_struct
*work
)
163 struct rvt_cq
*cq
= container_of(work
, struct rvt_cq
, comptask
);
166 * The completion handler will most likely rearm the notification
167 * and poll for all pending entries. If a new completion entry
168 * is added while we are in this routine, queue_work()
169 * won't call us again until we return so we check triggered to
170 * see if we need to call the handler again.
173 u8 triggered
= cq
->triggered
;
176 * IPoIB connected mode assumes the callback is from a
177 * soft IRQ. We simulate this by blocking "bottom halves".
178 * See the implementation for ipoib_cm_handle_tx_wc(),
179 * netif_tx_lock_bh() and netif_tx_lock().
182 cq
->ibcq
.comp_handler(&cq
->ibcq
, cq
->ibcq
.cq_context
);
185 if (cq
->triggered
== triggered
)
191 * rvt_create_cq - create a completion queue
192 * @ibcq: Allocated CQ
193 * @attr: creation attributes
194 * @udata: user data for libibverbs.so
196 * Called by ib_create_cq() in the generic verbs code.
198 * Return: 0 on success
200 int rvt_create_cq(struct ib_cq
*ibcq
, const struct ib_cq_init_attr
*attr
,
201 struct ib_udata
*udata
)
203 struct ib_device
*ibdev
= ibcq
->device
;
204 struct rvt_dev_info
*rdi
= ib_to_rvt(ibdev
);
205 struct rvt_cq
*cq
= ibcq_to_rvtcq(ibcq
);
206 struct rvt_cq_wc
*u_wc
= NULL
;
207 struct rvt_k_cq_wc
*k_wc
= NULL
;
209 unsigned int entries
= attr
->cqe
;
210 int comp_vector
= attr
->comp_vector
;
216 if (entries
< 1 || entries
> rdi
->dparms
.props
.max_cqe
)
222 comp_vector
= comp_vector
% rdi
->ibdev
.num_comp_vectors
;
225 * Allocate the completion queue entries and head/tail pointers.
226 * This is allocated separately so that it can be resized and
227 * also mapped into user space.
228 * We need to use vmalloc() in order to support mmap and large
229 * numbers of entries.
231 if (udata
&& udata
->outlen
>= sizeof(__u64
)) {
232 sz
= sizeof(struct ib_uverbs_wc
) * (entries
+ 1);
234 u_wc
= vmalloc_user(sz
);
238 sz
= sizeof(struct ib_wc
) * (entries
+ 1);
240 k_wc
= vzalloc_node(sz
, rdi
->dparms
.node
);
246 * Return the address of the WC as the offset to mmap.
247 * See rvt_mmap() for details.
249 if (udata
&& udata
->outlen
>= sizeof(__u64
)) {
250 cq
->ip
= rvt_create_mmap_info(rdi
, sz
, udata
, u_wc
);
256 err
= ib_copy_to_udata(udata
, &cq
->ip
->offset
,
257 sizeof(cq
->ip
->offset
));
262 spin_lock_irq(&rdi
->n_cqs_lock
);
263 if (rdi
->n_cqs_allocated
== rdi
->dparms
.props
.max_cq
) {
264 spin_unlock_irq(&rdi
->n_cqs_lock
);
269 rdi
->n_cqs_allocated
++;
270 spin_unlock_irq(&rdi
->n_cqs_lock
);
273 spin_lock_irq(&rdi
->pending_lock
);
274 list_add(&cq
->ip
->pending_mmaps
, &rdi
->pending_mmaps
);
275 spin_unlock_irq(&rdi
->pending_lock
);
279 * ib_create_cq() will initialize cq->ibcq except for cq->ibcq.cqe.
280 * The number of entries should be >= the number requested or return
284 if (rdi
->driver_f
.comp_vect_cpu_lookup
)
285 cq
->comp_vector_cpu
=
286 rdi
->driver_f
.comp_vect_cpu_lookup(rdi
, comp_vector
);
288 cq
->comp_vector_cpu
=
289 cpumask_first(cpumask_of_node(rdi
->dparms
.node
));
291 cq
->ibcq
.cqe
= entries
;
292 cq
->notify
= RVT_CQ_NONE
;
293 spin_lock_init(&cq
->lock
);
294 INIT_WORK(&cq
->comptask
, send_complete
);
300 trace_rvt_create_cq(cq
, attr
);
312 * rvt_destroy_cq - destroy a completion queue
313 * @ibcq: the completion queue to destroy.
314 * @udata: user data or NULL for kernel object
316 * Called by ib_destroy_cq() in the generic verbs code.
318 void rvt_destroy_cq(struct ib_cq
*ibcq
, struct ib_udata
*udata
)
320 struct rvt_cq
*cq
= ibcq_to_rvtcq(ibcq
);
321 struct rvt_dev_info
*rdi
= cq
->rdi
;
323 flush_work(&cq
->comptask
);
324 spin_lock_irq(&rdi
->n_cqs_lock
);
325 rdi
->n_cqs_allocated
--;
326 spin_unlock_irq(&rdi
->n_cqs_lock
);
328 kref_put(&cq
->ip
->ref
, rvt_release_mmap_info
);
334 * rvt_req_notify_cq - change the notification type for a completion queue
335 * @ibcq: the completion queue
336 * @notify_flags: the type of notification to request
338 * This may be called from interrupt context. Also called by
339 * ib_req_notify_cq() in the generic verbs code.
341 * Return: 0 for success.
343 int rvt_req_notify_cq(struct ib_cq
*ibcq
, enum ib_cq_notify_flags notify_flags
)
345 struct rvt_cq
*cq
= ibcq_to_rvtcq(ibcq
);
349 spin_lock_irqsave(&cq
->lock
, flags
);
351 * Don't change IB_CQ_NEXT_COMP to IB_CQ_SOLICITED but allow
352 * any other transitions (see C11-31 and C11-32 in ch. 11.4.2.2).
354 if (cq
->notify
!= IB_CQ_NEXT_COMP
)
355 cq
->notify
= notify_flags
& IB_CQ_SOLICITED_MASK
;
357 if (notify_flags
& IB_CQ_REPORT_MISSED_EVENTS
) {
359 if (RDMA_READ_UAPI_ATOMIC(cq
->queue
->head
) !=
360 RDMA_READ_UAPI_ATOMIC(cq
->queue
->tail
))
363 if (cq
->kqueue
->head
!= cq
->kqueue
->tail
)
368 spin_unlock_irqrestore(&cq
->lock
, flags
);
374 * rvt_resize_cq - change the size of the CQ
375 * @ibcq: the completion queue
377 * Return: 0 for success.
379 int rvt_resize_cq(struct ib_cq
*ibcq
, int cqe
, struct ib_udata
*udata
)
381 struct rvt_cq
*cq
= ibcq_to_rvtcq(ibcq
);
385 struct rvt_dev_info
*rdi
= cq
->rdi
;
386 struct rvt_cq_wc
*u_wc
= NULL
;
387 struct rvt_cq_wc
*old_u_wc
= NULL
;
388 struct rvt_k_cq_wc
*k_wc
= NULL
;
389 struct rvt_k_cq_wc
*old_k_wc
= NULL
;
391 if (cqe
< 1 || cqe
> rdi
->dparms
.props
.max_cqe
)
395 * Need to use vmalloc() if we want to support large #s of entries.
397 if (udata
&& udata
->outlen
>= sizeof(__u64
)) {
398 sz
= sizeof(struct ib_uverbs_wc
) * (cqe
+ 1);
400 u_wc
= vmalloc_user(sz
);
404 sz
= sizeof(struct ib_wc
) * (cqe
+ 1);
406 k_wc
= vzalloc_node(sz
, rdi
->dparms
.node
);
410 /* Check that we can write the offset to mmap. */
411 if (udata
&& udata
->outlen
>= sizeof(__u64
)) {
414 ret
= ib_copy_to_udata(udata
, &offset
, sizeof(offset
));
419 spin_lock_irq(&cq
->lock
);
421 * Make sure head and tail are sane since they
422 * might be user writable.
425 old_u_wc
= cq
->queue
;
426 head
= RDMA_READ_UAPI_ATOMIC(old_u_wc
->head
);
427 tail
= RDMA_READ_UAPI_ATOMIC(old_u_wc
->tail
);
429 old_k_wc
= cq
->kqueue
;
430 head
= old_k_wc
->head
;
431 tail
= old_k_wc
->tail
;
434 if (head
> (u32
)cq
->ibcq
.cqe
)
435 head
= (u32
)cq
->ibcq
.cqe
;
436 if (tail
> (u32
)cq
->ibcq
.cqe
)
437 tail
= (u32
)cq
->ibcq
.cqe
;
439 n
= cq
->ibcq
.cqe
+ 1 + head
- tail
;
442 if (unlikely((u32
)cqe
< n
)) {
446 for (n
= 0; tail
!= head
; n
++) {
448 u_wc
->uqueue
[n
] = old_u_wc
->uqueue
[tail
];
450 k_wc
->kqueue
[n
] = old_k_wc
->kqueue
[tail
];
451 if (tail
== (u32
)cq
->ibcq
.cqe
)
458 RDMA_WRITE_UAPI_ATOMIC(u_wc
->head
, n
);
459 RDMA_WRITE_UAPI_ATOMIC(u_wc
->tail
, 0);
466 spin_unlock_irq(&cq
->lock
);
474 struct rvt_mmap_info
*ip
= cq
->ip
;
476 rvt_update_mmap_info(rdi
, ip
, sz
, u_wc
);
479 * Return the offset to mmap.
480 * See rvt_mmap() for details.
482 if (udata
&& udata
->outlen
>= sizeof(__u64
)) {
483 ret
= ib_copy_to_udata(udata
, &ip
->offset
,
489 spin_lock_irq(&rdi
->pending_lock
);
490 if (list_empty(&ip
->pending_mmaps
))
491 list_add(&ip
->pending_mmaps
, &rdi
->pending_mmaps
);
492 spin_unlock_irq(&rdi
->pending_lock
);
498 spin_unlock_irq(&cq
->lock
);
507 * rvt_poll_cq - poll for work completion entries
508 * @ibcq: the completion queue to poll
509 * @num_entries: the maximum number of entries to return
510 * @entry: pointer to array where work completions are placed
512 * This may be called from interrupt context. Also called by ib_poll_cq()
513 * in the generic verbs code.
515 * Return: the number of completion entries polled.
517 int rvt_poll_cq(struct ib_cq
*ibcq
, int num_entries
, struct ib_wc
*entry
)
519 struct rvt_cq
*cq
= ibcq_to_rvtcq(ibcq
);
520 struct rvt_k_cq_wc
*wc
;
525 /* The kernel can only poll a kernel completion queue */
529 spin_lock_irqsave(&cq
->lock
, flags
);
533 if (tail
> (u32
)cq
->ibcq
.cqe
)
534 tail
= (u32
)cq
->ibcq
.cqe
;
535 for (npolled
= 0; npolled
< num_entries
; ++npolled
, ++entry
) {
536 if (tail
== wc
->head
)
538 /* The kernel doesn't need a RMB since it has the lock. */
539 trace_rvt_cq_poll(cq
, &wc
->kqueue
[tail
], npolled
);
540 *entry
= wc
->kqueue
[tail
];
541 if (tail
>= cq
->ibcq
.cqe
)
548 spin_unlock_irqrestore(&cq
->lock
, flags
);
554 * rvt_driver_cq_init - Init cq resources on behalf of driver
556 * Return: 0 on success
558 int rvt_driver_cq_init(void)
560 comp_vector_wq
= alloc_workqueue("%s", WQ_HIGHPRI
| WQ_CPU_INTENSIVE
,
569 * rvt_cq_exit - tear down cq reources
571 void rvt_cq_exit(void)
573 destroy_workqueue(comp_vector_wq
);
574 comp_vector_wq
= NULL
;