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 * @attr: creation attributes
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,
216 const struct ib_cq_init_attr *attr,
217 struct ib_ucontext *context,
218 struct ib_udata *udata)
220 int entries = attr->cqe;
221 struct qib_ibdev *dev = to_idev(ibdev);
223 struct qib_cq_wc *wc;
228 return ERR_PTR(-EINVAL);
230 if (entries < 1 || entries > ib_qib_max_cqes) {
231 ret = ERR_PTR(-EINVAL);
235 /* Allocate the completion queue structure. */
236 cq = kmalloc(sizeof(*cq), GFP_KERNEL);
238 ret = ERR_PTR(-ENOMEM);
243 * Allocate the completion queue entries and head/tail pointers.
244 * This is allocated separately so that it can be resized and
245 * also mapped into user space.
246 * We need to use vmalloc() in order to support mmap and large
247 * numbers of entries.
250 if (udata && udata->outlen >= sizeof(__u64))
251 sz += sizeof(struct ib_uverbs_wc) * (entries + 1);
253 sz += sizeof(struct ib_wc) * (entries + 1);
254 wc = vmalloc_user(sz);
256 ret = ERR_PTR(-ENOMEM);
261 * Return the address of the WC as the offset to mmap.
262 * See qib_mmap() for details.
264 if (udata && udata->outlen >= sizeof(__u64)) {
267 cq->ip = qib_create_mmap_info(dev, sz, context, wc);
269 ret = ERR_PTR(-ENOMEM);
273 err = ib_copy_to_udata(udata, &cq->ip->offset,
274 sizeof(cq->ip->offset));
282 spin_lock(&dev->n_cqs_lock);
283 if (dev->n_cqs_allocated == ib_qib_max_cqs) {
284 spin_unlock(&dev->n_cqs_lock);
285 ret = ERR_PTR(-ENOMEM);
289 dev->n_cqs_allocated++;
290 spin_unlock(&dev->n_cqs_lock);
293 spin_lock_irq(&dev->pending_lock);
294 list_add(&cq->ip->pending_mmaps, &dev->pending_mmaps);
295 spin_unlock_irq(&dev->pending_lock);
299 * ib_create_cq() will initialize cq->ibcq except for cq->ibcq.cqe.
300 * The number of entries should be >= the number requested or return
303 cq->dd = dd_from_dev(dev);
304 cq->ibcq.cqe = entries;
305 cq->notify = IB_CQ_NONE;
307 spin_lock_init(&cq->lock);
308 init_kthread_work(&cq->comptask, send_complete);
328 * qib_destroy_cq - destroy a completion queue
329 * @ibcq: the completion queue to destroy.
331 * Returns 0 for success.
333 * Called by ib_destroy_cq() in the generic verbs code.
335 int qib_destroy_cq(struct ib_cq *ibcq)
337 struct qib_ibdev *dev = to_idev(ibcq->device);
338 struct qib_cq *cq = to_icq(ibcq);
340 flush_kthread_work(&cq->comptask);
341 spin_lock(&dev->n_cqs_lock);
342 dev->n_cqs_allocated--;
343 spin_unlock(&dev->n_cqs_lock);
345 kref_put(&cq->ip->ref, qib_release_mmap_info);
354 * qib_req_notify_cq - change the notification type for a completion queue
355 * @ibcq: the completion queue
356 * @notify_flags: the type of notification to request
358 * Returns 0 for success.
360 * This may be called from interrupt context. Also called by
361 * ib_req_notify_cq() in the generic verbs code.
363 int qib_req_notify_cq(struct ib_cq *ibcq, enum ib_cq_notify_flags notify_flags)
365 struct qib_cq *cq = to_icq(ibcq);
369 spin_lock_irqsave(&cq->lock, flags);
371 * Don't change IB_CQ_NEXT_COMP to IB_CQ_SOLICITED but allow
372 * any other transitions (see C11-31 and C11-32 in ch. 11.4.2.2).
374 if (cq->notify != IB_CQ_NEXT_COMP)
375 cq->notify = notify_flags & IB_CQ_SOLICITED_MASK;
377 if ((notify_flags & IB_CQ_REPORT_MISSED_EVENTS) &&
378 cq->queue->head != cq->queue->tail)
381 spin_unlock_irqrestore(&cq->lock, flags);
387 * qib_resize_cq - change the size of the CQ
388 * @ibcq: the completion queue
390 * Returns 0 for success.
392 int qib_resize_cq(struct ib_cq *ibcq, int cqe, struct ib_udata *udata)
394 struct qib_cq *cq = to_icq(ibcq);
395 struct qib_cq_wc *old_wc;
396 struct qib_cq_wc *wc;
401 if (cqe < 1 || cqe > ib_qib_max_cqes) {
407 * Need to use vmalloc() if we want to support large #s of entries.
410 if (udata && udata->outlen >= sizeof(__u64))
411 sz += sizeof(struct ib_uverbs_wc) * (cqe + 1);
413 sz += sizeof(struct ib_wc) * (cqe + 1);
414 wc = vmalloc_user(sz);
420 /* Check that we can write the offset to mmap. */
421 if (udata && udata->outlen >= sizeof(__u64)) {
424 ret = ib_copy_to_udata(udata, &offset, sizeof(offset));
429 spin_lock_irq(&cq->lock);
431 * Make sure head and tail are sane since they
432 * might be user writable.
436 if (head > (u32) cq->ibcq.cqe)
437 head = (u32) cq->ibcq.cqe;
439 if (tail > (u32) cq->ibcq.cqe)
440 tail = (u32) cq->ibcq.cqe;
442 n = cq->ibcq.cqe + 1 + head - tail;
445 if (unlikely((u32)cqe < n)) {
449 for (n = 0; tail != head; n++) {
451 wc->uqueue[n] = old_wc->uqueue[tail];
453 wc->kqueue[n] = old_wc->kqueue[tail];
454 if (tail == (u32) cq->ibcq.cqe)
463 spin_unlock_irq(&cq->lock);
468 struct qib_ibdev *dev = to_idev(ibcq->device);
469 struct qib_mmap_info *ip = cq->ip;
471 qib_update_mmap_info(dev, ip, sz, wc);
474 * Return the offset to mmap.
475 * See qib_mmap() for details.
477 if (udata && udata->outlen >= sizeof(__u64)) {
478 ret = ib_copy_to_udata(udata, &ip->offset,
484 spin_lock_irq(&dev->pending_lock);
485 if (list_empty(&ip->pending_mmaps))
486 list_add(&ip->pending_mmaps, &dev->pending_mmaps);
487 spin_unlock_irq(&dev->pending_lock);
494 spin_unlock_irq(&cq->lock);
501 int qib_cq_init(struct qib_devdata *dd)
505 struct task_struct *task;
509 dd->worker = kzalloc(sizeof(*dd->worker), GFP_KERNEL);
512 init_kthread_worker(dd->worker);
513 task = kthread_create_on_node(
516 dd->assigned_node_id,
517 "qib_cq%d", dd->unit);
520 cpu = cpumask_first(cpumask_of_node(dd->assigned_node_id));
521 kthread_bind(task, cpu);
522 wake_up_process(task);
532 void qib_cq_exit(struct qib_devdata *dd)
534 struct kthread_worker *worker;
539 /* blocks future queuing from send_complete() */
542 flush_kthread_worker(worker);
543 kthread_stop(worker->task);