2 * Copyright(c) 2016 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>
50 #include <linux/kthread.h>
55 * rvt_cq_enter - add a new entry to the completion queue
56 * @cq: completion queue
57 * @entry: work completion entry to add
58 * @sig: true if @entry is solicited
60 * This may be called with qp->s_lock held.
62 void rvt_cq_enter(struct rvt_cq *cq, struct ib_wc *entry, bool solicited)
69 spin_lock_irqsave(&cq->lock, flags);
72 * Note that the head pointer might be writable by user processes.
73 * Take care to verify it is a sane value.
77 if (head >= (unsigned)cq->ibcq.cqe) {
84 if (unlikely(next == wc->tail)) {
85 spin_unlock_irqrestore(&cq->lock, flags);
86 if (cq->ibcq.event_handler) {
89 ev.device = cq->ibcq.device;
90 ev.element.cq = &cq->ibcq;
91 ev.event = IB_EVENT_CQ_ERR;
92 cq->ibcq.event_handler(&ev, cq->ibcq.cq_context);
97 wc->uqueue[head].wr_id = entry->wr_id;
98 wc->uqueue[head].status = entry->status;
99 wc->uqueue[head].opcode = entry->opcode;
100 wc->uqueue[head].vendor_err = entry->vendor_err;
101 wc->uqueue[head].byte_len = entry->byte_len;
102 wc->uqueue[head].ex.imm_data =
103 (__u32 __force)entry->ex.imm_data;
104 wc->uqueue[head].qp_num = entry->qp->qp_num;
105 wc->uqueue[head].src_qp = entry->src_qp;
106 wc->uqueue[head].wc_flags = entry->wc_flags;
107 wc->uqueue[head].pkey_index = entry->pkey_index;
108 wc->uqueue[head].slid = entry->slid;
109 wc->uqueue[head].sl = entry->sl;
110 wc->uqueue[head].dlid_path_bits = entry->dlid_path_bits;
111 wc->uqueue[head].port_num = entry->port_num;
112 /* Make sure entry is written before the head index. */
115 wc->kqueue[head] = *entry;
119 if (cq->notify == IB_CQ_NEXT_COMP ||
120 (cq->notify == IB_CQ_SOLICITED &&
121 (solicited || entry->status != IB_WC_SUCCESS))) {
122 struct kthread_worker *worker;
124 * This will cause send_complete() to be called in
127 smp_read_barrier_depends(); /* see rvt_cq_exit */
128 worker = cq->rdi->worker;
129 if (likely(worker)) {
130 cq->notify = RVT_CQ_NONE;
132 kthread_queue_work(worker, &cq->comptask);
136 spin_unlock_irqrestore(&cq->lock, flags);
138 EXPORT_SYMBOL(rvt_cq_enter);
140 static void send_complete(struct kthread_work *work)
142 struct rvt_cq *cq = container_of(work, struct rvt_cq, comptask);
145 * The completion handler will most likely rearm the notification
146 * and poll for all pending entries. If a new completion entry
147 * is added while we are in this routine, queue_work()
148 * won't call us again until we return so we check triggered to
149 * see if we need to call the handler again.
152 u8 triggered = cq->triggered;
155 * IPoIB connected mode assumes the callback is from a
156 * soft IRQ. We simulate this by blocking "bottom halves".
157 * See the implementation for ipoib_cm_handle_tx_wc(),
158 * netif_tx_lock_bh() and netif_tx_lock().
161 cq->ibcq.comp_handler(&cq->ibcq, cq->ibcq.cq_context);
164 if (cq->triggered == triggered)
170 * rvt_create_cq - create a completion queue
171 * @ibdev: the device this completion queue is attached to
172 * @attr: creation attributes
173 * @context: unused by the QLogic_IB driver
174 * @udata: user data for libibverbs.so
176 * Called by ib_create_cq() in the generic verbs code.
178 * Return: pointer to the completion queue or negative errno values
181 struct ib_cq *rvt_create_cq(struct ib_device *ibdev,
182 const struct ib_cq_init_attr *attr,
183 struct ib_ucontext *context,
184 struct ib_udata *udata)
186 struct rvt_dev_info *rdi = ib_to_rvt(ibdev);
188 struct rvt_cq_wc *wc;
191 unsigned int entries = attr->cqe;
194 return ERR_PTR(-EINVAL);
196 if (entries < 1 || entries > rdi->dparms.props.max_cqe)
197 return ERR_PTR(-EINVAL);
199 /* Allocate the completion queue structure. */
200 cq = kzalloc_node(sizeof(*cq), GFP_KERNEL, rdi->dparms.node);
202 return ERR_PTR(-ENOMEM);
205 * Allocate the completion queue entries and head/tail pointers.
206 * This is allocated separately so that it can be resized and
207 * also mapped into user space.
208 * We need to use vmalloc() in order to support mmap and large
209 * numbers of entries.
212 if (udata && udata->outlen >= sizeof(__u64))
213 sz += sizeof(struct ib_uverbs_wc) * (entries + 1);
215 sz += sizeof(struct ib_wc) * (entries + 1);
218 vzalloc_node(sz, rdi->dparms.node);
220 ret = ERR_PTR(-ENOMEM);
225 * Return the address of the WC as the offset to mmap.
226 * See rvt_mmap() for details.
228 if (udata && udata->outlen >= sizeof(__u64)) {
231 cq->ip = rvt_create_mmap_info(rdi, sz, context, wc);
233 ret = ERR_PTR(-ENOMEM);
237 err = ib_copy_to_udata(udata, &cq->ip->offset,
238 sizeof(cq->ip->offset));
245 spin_lock(&rdi->n_cqs_lock);
246 if (rdi->n_cqs_allocated == rdi->dparms.props.max_cq) {
247 spin_unlock(&rdi->n_cqs_lock);
248 ret = ERR_PTR(-ENOMEM);
252 rdi->n_cqs_allocated++;
253 spin_unlock(&rdi->n_cqs_lock);
256 spin_lock_irq(&rdi->pending_lock);
257 list_add(&cq->ip->pending_mmaps, &rdi->pending_mmaps);
258 spin_unlock_irq(&rdi->pending_lock);
262 * ib_create_cq() will initialize cq->ibcq except for cq->ibcq.cqe.
263 * The number of entries should be >= the number requested or return
267 cq->ibcq.cqe = entries;
268 cq->notify = RVT_CQ_NONE;
269 spin_lock_init(&cq->lock);
270 kthread_init_work(&cq->comptask, send_complete);
288 * rvt_destroy_cq - destroy a completion queue
289 * @ibcq: the completion queue to destroy.
291 * Called by ib_destroy_cq() in the generic verbs code.
295 int rvt_destroy_cq(struct ib_cq *ibcq)
297 struct rvt_cq *cq = ibcq_to_rvtcq(ibcq);
298 struct rvt_dev_info *rdi = cq->rdi;
300 kthread_flush_work(&cq->comptask);
301 spin_lock(&rdi->n_cqs_lock);
302 rdi->n_cqs_allocated--;
303 spin_unlock(&rdi->n_cqs_lock);
305 kref_put(&cq->ip->ref, rvt_release_mmap_info);
314 * rvt_req_notify_cq - change the notification type for a completion queue
315 * @ibcq: the completion queue
316 * @notify_flags: the type of notification to request
318 * This may be called from interrupt context. Also called by
319 * ib_req_notify_cq() in the generic verbs code.
321 * Return: 0 for success.
323 int rvt_req_notify_cq(struct ib_cq *ibcq, enum ib_cq_notify_flags notify_flags)
325 struct rvt_cq *cq = ibcq_to_rvtcq(ibcq);
329 spin_lock_irqsave(&cq->lock, flags);
331 * Don't change IB_CQ_NEXT_COMP to IB_CQ_SOLICITED but allow
332 * any other transitions (see C11-31 and C11-32 in ch. 11.4.2.2).
334 if (cq->notify != IB_CQ_NEXT_COMP)
335 cq->notify = notify_flags & IB_CQ_SOLICITED_MASK;
337 if ((notify_flags & IB_CQ_REPORT_MISSED_EVENTS) &&
338 cq->queue->head != cq->queue->tail)
341 spin_unlock_irqrestore(&cq->lock, flags);
347 * rvt_resize_cq - change the size of the CQ
348 * @ibcq: the completion queue
350 * Return: 0 for success.
352 int rvt_resize_cq(struct ib_cq *ibcq, int cqe, struct ib_udata *udata)
354 struct rvt_cq *cq = ibcq_to_rvtcq(ibcq);
355 struct rvt_cq_wc *old_wc;
356 struct rvt_cq_wc *wc;
360 struct rvt_dev_info *rdi = cq->rdi;
362 if (cqe < 1 || cqe > rdi->dparms.props.max_cqe)
366 * Need to use vmalloc() if we want to support large #s of entries.
369 if (udata && udata->outlen >= sizeof(__u64))
370 sz += sizeof(struct ib_uverbs_wc) * (cqe + 1);
372 sz += sizeof(struct ib_wc) * (cqe + 1);
375 vzalloc_node(sz, rdi->dparms.node);
379 /* Check that we can write the offset to mmap. */
380 if (udata && udata->outlen >= sizeof(__u64)) {
383 ret = ib_copy_to_udata(udata, &offset, sizeof(offset));
388 spin_lock_irq(&cq->lock);
390 * Make sure head and tail are sane since they
391 * might be user writable.
395 if (head > (u32)cq->ibcq.cqe)
396 head = (u32)cq->ibcq.cqe;
398 if (tail > (u32)cq->ibcq.cqe)
399 tail = (u32)cq->ibcq.cqe;
401 n = cq->ibcq.cqe + 1 + head - tail;
404 if (unlikely((u32)cqe < n)) {
408 for (n = 0; tail != head; n++) {
410 wc->uqueue[n] = old_wc->uqueue[tail];
412 wc->kqueue[n] = old_wc->kqueue[tail];
413 if (tail == (u32)cq->ibcq.cqe)
422 spin_unlock_irq(&cq->lock);
427 struct rvt_mmap_info *ip = cq->ip;
429 rvt_update_mmap_info(rdi, ip, sz, wc);
432 * Return the offset to mmap.
433 * See rvt_mmap() for details.
435 if (udata && udata->outlen >= sizeof(__u64)) {
436 ret = ib_copy_to_udata(udata, &ip->offset,
442 spin_lock_irq(&rdi->pending_lock);
443 if (list_empty(&ip->pending_mmaps))
444 list_add(&ip->pending_mmaps, &rdi->pending_mmaps);
445 spin_unlock_irq(&rdi->pending_lock);
451 spin_unlock_irq(&cq->lock);
458 * rvt_poll_cq - poll for work completion entries
459 * @ibcq: the completion queue to poll
460 * @num_entries: the maximum number of entries to return
461 * @entry: pointer to array where work completions are placed
463 * This may be called from interrupt context. Also called by ib_poll_cq()
464 * in the generic verbs code.
466 * Return: the number of completion entries polled.
468 int rvt_poll_cq(struct ib_cq *ibcq, int num_entries, struct ib_wc *entry)
470 struct rvt_cq *cq = ibcq_to_rvtcq(ibcq);
471 struct rvt_cq_wc *wc;
476 /* The kernel can only poll a kernel completion queue */
480 spin_lock_irqsave(&cq->lock, flags);
484 if (tail > (u32)cq->ibcq.cqe)
485 tail = (u32)cq->ibcq.cqe;
486 for (npolled = 0; npolled < num_entries; ++npolled, ++entry) {
487 if (tail == wc->head)
489 /* The kernel doesn't need a RMB since it has the lock. */
490 *entry = wc->kqueue[tail];
491 if (tail >= cq->ibcq.cqe)
498 spin_unlock_irqrestore(&cq->lock, flags);
504 * rvt_driver_cq_init - Init cq resources on behalf of driver
505 * @rdi: rvt dev structure
507 * Return: 0 on success
509 int rvt_driver_cq_init(struct rvt_dev_info *rdi)
513 struct task_struct *task;
517 spin_lock_init(&rdi->n_cqs_lock);
518 rdi->worker = kzalloc(sizeof(*rdi->worker), GFP_KERNEL);
521 kthread_init_worker(rdi->worker);
522 task = kthread_create_on_node(
526 "%s", rdi->dparms.cq_name);
530 return PTR_ERR(task);
533 set_user_nice(task, MIN_NICE);
534 cpu = cpumask_first(cpumask_of_node(rdi->dparms.node));
535 kthread_bind(task, cpu);
536 wake_up_process(task);
541 * rvt_cq_exit - tear down cq reources
542 * @rdi: rvt dev structure
544 void rvt_cq_exit(struct rvt_dev_info *rdi)
546 struct kthread_worker *worker;
548 worker = rdi->worker;
551 /* blocks future queuing from send_complete() */
553 smp_wmb(); /* See rdi_cq_enter */
554 kthread_flush_worker(worker);
555 kthread_stop(worker->task);