2 * Copyright (c) 2013 Intel Corporation. All rights reserved.
3 * Copyright (c) 2006, 2007, 2008, 2010 QLogic Corporation. All rights reserved.
4 * Copyright (c) 2005, 2006 PathScale, Inc. All rights reserved.
6 * This software is available to you under a choice of one of two
7 * licenses. You may choose to be licensed under the terms of the GNU
8 * General Public License (GPL) Version 2, available from the file
9 * COPYING in the main directory of this source tree, or the
10 * OpenIB.org BSD license below:
12 * Redistribution and use in source and binary forms, with or
13 * without modification, are permitted provided that the following
16 * - Redistributions of source code must retain the above
17 * copyright notice, this list of conditions and the following
20 * - Redistributions in binary form must reproduce the above
21 * copyright notice, this list of conditions and the following
22 * disclaimer in the documentation and/or other materials
23 * provided with the distribution.
25 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
29 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
30 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
31 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
35 #include <linux/err.h>
36 #include <linux/slab.h>
37 #include <linux/vmalloc.h>
38 #include <linux/kthread.h>
40 #include "qib_verbs.h"
44 * qib_cq_enter - add a new entry to the completion queue
45 * @cq: completion queue
46 * @entry: work completion entry to add
47 * @sig: true if @entry is a solicitated entry
49 * This may be called with qp->s_lock held.
51 void qib_cq_enter(struct qib_cq *cq, struct ib_wc *entry, int solicited)
58 spin_lock_irqsave(&cq->lock, flags);
61 * Note that the head pointer might be writable by user processes.
62 * Take care to verify it is a sane value.
66 if (head >= (unsigned) cq->ibcq.cqe) {
71 if (unlikely(next == wc->tail)) {
72 spin_unlock_irqrestore(&cq->lock, flags);
73 if (cq->ibcq.event_handler) {
76 ev.device = cq->ibcq.device;
77 ev.element.cq = &cq->ibcq;
78 ev.event = IB_EVENT_CQ_ERR;
79 cq->ibcq.event_handler(&ev, cq->ibcq.cq_context);
84 wc->uqueue[head].wr_id = entry->wr_id;
85 wc->uqueue[head].status = entry->status;
86 wc->uqueue[head].opcode = entry->opcode;
87 wc->uqueue[head].vendor_err = entry->vendor_err;
88 wc->uqueue[head].byte_len = entry->byte_len;
89 wc->uqueue[head].ex.imm_data =
90 (__u32 __force)entry->ex.imm_data;
91 wc->uqueue[head].qp_num = entry->qp->qp_num;
92 wc->uqueue[head].src_qp = entry->src_qp;
93 wc->uqueue[head].wc_flags = entry->wc_flags;
94 wc->uqueue[head].pkey_index = entry->pkey_index;
95 wc->uqueue[head].slid = entry->slid;
96 wc->uqueue[head].sl = entry->sl;
97 wc->uqueue[head].dlid_path_bits = entry->dlid_path_bits;
98 wc->uqueue[head].port_num = entry->port_num;
99 /* Make sure entry is written before the head index. */
102 wc->kqueue[head] = *entry;
105 if (cq->notify == IB_CQ_NEXT_COMP ||
106 (cq->notify == IB_CQ_SOLICITED &&
107 (solicited || entry->status != IB_WC_SUCCESS))) {
108 struct kthread_worker *worker;
110 * This will cause send_complete() to be called in
114 worker = cq->dd->worker;
115 if (likely(worker)) {
116 cq->notify = IB_CQ_NONE;
118 queue_kthread_work(worker, &cq->comptask);
122 spin_unlock_irqrestore(&cq->lock, flags);
126 * qib_poll_cq - poll for work completion entries
127 * @ibcq: the completion queue to poll
128 * @num_entries: the maximum number of entries to return
129 * @entry: pointer to array where work completions are placed
131 * Returns the number of completion entries polled.
133 * This may be called from interrupt context. Also called by ib_poll_cq()
134 * in the generic verbs code.
136 int qib_poll_cq(struct ib_cq *ibcq, int num_entries, struct ib_wc *entry)
138 struct qib_cq *cq = to_icq(ibcq);
139 struct qib_cq_wc *wc;
144 /* The kernel can only poll a kernel completion queue */
150 spin_lock_irqsave(&cq->lock, flags);
154 if (tail > (u32) cq->ibcq.cqe)
155 tail = (u32) cq->ibcq.cqe;
156 for (npolled = 0; npolled < num_entries; ++npolled, ++entry) {
157 if (tail == wc->head)
159 /* The kernel doesn't need a RMB since it has the lock. */
160 *entry = wc->kqueue[tail];
161 if (tail >= cq->ibcq.cqe)
168 spin_unlock_irqrestore(&cq->lock, flags);
174 static void send_complete(struct kthread_work *work)
176 struct qib_cq *cq = container_of(work, struct qib_cq, comptask);
179 * The completion handler will most likely rearm the notification
180 * and poll for all pending entries. If a new completion entry
181 * is added while we are in this routine, queue_work()
182 * won't call us again until we return so we check triggered to
183 * see if we need to call the handler again.
186 u8 triggered = cq->triggered;
189 * IPoIB connected mode assumes the callback is from a
190 * soft IRQ. We simulate this by blocking "bottom halves".
191 * See the implementation for ipoib_cm_handle_tx_wc(),
192 * netif_tx_lock_bh() and netif_tx_lock().
195 cq->ibcq.comp_handler(&cq->ibcq, cq->ibcq.cq_context);
198 if (cq->triggered == triggered)
204 * qib_create_cq - create a completion queue
205 * @ibdev: the device this completion queue is attached to
206 * @entries: the minimum size of the completion queue
207 * @context: unused by the QLogic_IB driver
208 * @udata: user data for libibverbs.so
210 * Returns a pointer to the completion queue or negative errno values
213 * Called by ib_create_cq() in the generic verbs code.
215 struct ib_cq *qib_create_cq(struct ib_device *ibdev, int entries,
216 int comp_vector, struct ib_ucontext *context,
217 struct ib_udata *udata)
219 struct qib_ibdev *dev = to_idev(ibdev);
221 struct qib_cq_wc *wc;
225 if (entries < 1 || entries > ib_qib_max_cqes) {
226 ret = ERR_PTR(-EINVAL);
230 /* Allocate the completion queue structure. */
231 cq = kmalloc(sizeof(*cq), GFP_KERNEL);
233 ret = ERR_PTR(-ENOMEM);
238 * Allocate the completion queue entries and head/tail pointers.
239 * This is allocated separately so that it can be resized and
240 * also mapped into user space.
241 * We need to use vmalloc() in order to support mmap and large
242 * numbers of entries.
245 if (udata && udata->outlen >= sizeof(__u64))
246 sz += sizeof(struct ib_uverbs_wc) * (entries + 1);
248 sz += sizeof(struct ib_wc) * (entries + 1);
249 wc = vmalloc_user(sz);
251 ret = ERR_PTR(-ENOMEM);
256 * Return the address of the WC as the offset to mmap.
257 * See qib_mmap() for details.
259 if (udata && udata->outlen >= sizeof(__u64)) {
262 cq->ip = qib_create_mmap_info(dev, sz, context, wc);
264 ret = ERR_PTR(-ENOMEM);
268 err = ib_copy_to_udata(udata, &cq->ip->offset,
269 sizeof(cq->ip->offset));
277 spin_lock(&dev->n_cqs_lock);
278 if (dev->n_cqs_allocated == ib_qib_max_cqs) {
279 spin_unlock(&dev->n_cqs_lock);
280 ret = ERR_PTR(-ENOMEM);
284 dev->n_cqs_allocated++;
285 spin_unlock(&dev->n_cqs_lock);
288 spin_lock_irq(&dev->pending_lock);
289 list_add(&cq->ip->pending_mmaps, &dev->pending_mmaps);
290 spin_unlock_irq(&dev->pending_lock);
294 * ib_create_cq() will initialize cq->ibcq except for cq->ibcq.cqe.
295 * The number of entries should be >= the number requested or return
298 cq->dd = dd_from_dev(dev);
299 cq->ibcq.cqe = entries;
300 cq->notify = IB_CQ_NONE;
302 spin_lock_init(&cq->lock);
303 init_kthread_work(&cq->comptask, send_complete);
323 * qib_destroy_cq - destroy a completion queue
324 * @ibcq: the completion queue to destroy.
326 * Returns 0 for success.
328 * Called by ib_destroy_cq() in the generic verbs code.
330 int qib_destroy_cq(struct ib_cq *ibcq)
332 struct qib_ibdev *dev = to_idev(ibcq->device);
333 struct qib_cq *cq = to_icq(ibcq);
335 flush_kthread_work(&cq->comptask);
336 spin_lock(&dev->n_cqs_lock);
337 dev->n_cqs_allocated--;
338 spin_unlock(&dev->n_cqs_lock);
340 kref_put(&cq->ip->ref, qib_release_mmap_info);
349 * qib_req_notify_cq - change the notification type for a completion queue
350 * @ibcq: the completion queue
351 * @notify_flags: the type of notification to request
353 * Returns 0 for success.
355 * This may be called from interrupt context. Also called by
356 * ib_req_notify_cq() in the generic verbs code.
358 int qib_req_notify_cq(struct ib_cq *ibcq, enum ib_cq_notify_flags notify_flags)
360 struct qib_cq *cq = to_icq(ibcq);
364 spin_lock_irqsave(&cq->lock, flags);
366 * Don't change IB_CQ_NEXT_COMP to IB_CQ_SOLICITED but allow
367 * any other transitions (see C11-31 and C11-32 in ch. 11.4.2.2).
369 if (cq->notify != IB_CQ_NEXT_COMP)
370 cq->notify = notify_flags & IB_CQ_SOLICITED_MASK;
372 if ((notify_flags & IB_CQ_REPORT_MISSED_EVENTS) &&
373 cq->queue->head != cq->queue->tail)
376 spin_unlock_irqrestore(&cq->lock, flags);
382 * qib_resize_cq - change the size of the CQ
383 * @ibcq: the completion queue
385 * Returns 0 for success.
387 int qib_resize_cq(struct ib_cq *ibcq, int cqe, struct ib_udata *udata)
389 struct qib_cq *cq = to_icq(ibcq);
390 struct qib_cq_wc *old_wc;
391 struct qib_cq_wc *wc;
396 if (cqe < 1 || cqe > ib_qib_max_cqes) {
402 * Need to use vmalloc() if we want to support large #s of entries.
405 if (udata && udata->outlen >= sizeof(__u64))
406 sz += sizeof(struct ib_uverbs_wc) * (cqe + 1);
408 sz += sizeof(struct ib_wc) * (cqe + 1);
409 wc = vmalloc_user(sz);
415 /* Check that we can write the offset to mmap. */
416 if (udata && udata->outlen >= sizeof(__u64)) {
419 ret = ib_copy_to_udata(udata, &offset, sizeof(offset));
424 spin_lock_irq(&cq->lock);
426 * Make sure head and tail are sane since they
427 * might be user writable.
431 if (head > (u32) cq->ibcq.cqe)
432 head = (u32) cq->ibcq.cqe;
434 if (tail > (u32) cq->ibcq.cqe)
435 tail = (u32) cq->ibcq.cqe;
437 n = cq->ibcq.cqe + 1 + head - tail;
440 if (unlikely((u32)cqe < n)) {
444 for (n = 0; tail != head; n++) {
446 wc->uqueue[n] = old_wc->uqueue[tail];
448 wc->kqueue[n] = old_wc->kqueue[tail];
449 if (tail == (u32) cq->ibcq.cqe)
458 spin_unlock_irq(&cq->lock);
463 struct qib_ibdev *dev = to_idev(ibcq->device);
464 struct qib_mmap_info *ip = cq->ip;
466 qib_update_mmap_info(dev, ip, sz, wc);
469 * Return the offset to mmap.
470 * See qib_mmap() for details.
472 if (udata && udata->outlen >= sizeof(__u64)) {
473 ret = ib_copy_to_udata(udata, &ip->offset,
479 spin_lock_irq(&dev->pending_lock);
480 if (list_empty(&ip->pending_mmaps))
481 list_add(&ip->pending_mmaps, &dev->pending_mmaps);
482 spin_unlock_irq(&dev->pending_lock);
489 spin_unlock_irq(&cq->lock);
496 int qib_cq_init(struct qib_devdata *dd)
500 struct task_struct *task;
504 dd->worker = kzalloc(sizeof(*dd->worker), GFP_KERNEL);
507 init_kthread_worker(dd->worker);
508 task = kthread_create_on_node(
511 dd->assigned_node_id,
512 "qib_cq%d", dd->unit);
515 cpu = cpumask_first(cpumask_of_node(dd->assigned_node_id));
516 kthread_bind(task, cpu);
517 wake_up_process(task);
527 void qib_cq_exit(struct qib_devdata *dd)
529 struct kthread_worker *worker;
534 /* blocks future queuing from send_complete() */
537 flush_kthread_worker(worker);
538 kthread_stop(worker->task);