2 * Copyright (c) 2006 QLogic, Inc. All rights reserved.
3 * Copyright (c) 2005, 2006 PathScale, Inc. All rights reserved.
5 * This software is available to you under a choice of one of two
6 * licenses. You may choose to be licensed under the terms of the GNU
7 * General Public License (GPL) Version 2, available from the file
8 * COPYING in the main directory of this source tree, or the
9 * OpenIB.org BSD license below:
11 * Redistribution and use in source and binary forms, with or
12 * without modification, are permitted provided that the following
15 * - Redistributions of source code must retain the above
16 * copyright notice, this list of conditions and the following
19 * - Redistributions in binary form must reproduce the above
20 * copyright notice, this list of conditions and the following
21 * disclaimer in the documentation and/or other materials
22 * provided with the distribution.
24 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
28 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
29 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
30 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
34 #include <linux/err.h>
35 #include <linux/vmalloc.h>
37 #include "ipath_verbs.h"
40 * ipath_cq_enter - add a new entry to the completion queue
41 * @cq: completion queue
42 * @entry: work completion entry to add
43 * @sig: true if @entry is a solicitated entry
45 * This may be called with qp->s_lock held.
47 void ipath_cq_enter(struct ipath_cq *cq, struct ib_wc *entry, int solicited)
49 struct ipath_cq_wc *wc = cq->queue;
54 spin_lock_irqsave(&cq->lock, flags);
57 * Note that the head pointer might be writable by user processes.
58 * Take care to verify it is a sane value.
61 if (head >= (unsigned) cq->ibcq.cqe) {
66 if (unlikely(next == wc->tail)) {
67 spin_unlock_irqrestore(&cq->lock, flags);
68 if (cq->ibcq.event_handler) {
71 ev.device = cq->ibcq.device;
72 ev.element.cq = &cq->ibcq;
73 ev.event = IB_EVENT_CQ_ERR;
74 cq->ibcq.event_handler(&ev, cq->ibcq.cq_context);
78 wc->queue[head] = *entry;
81 if (cq->notify == IB_CQ_NEXT_COMP ||
82 (cq->notify == IB_CQ_SOLICITED && solicited)) {
83 cq->notify = IB_CQ_NONE;
86 * This will cause send_complete() to be called in
89 tasklet_hi_schedule(&cq->comptask);
92 spin_unlock_irqrestore(&cq->lock, flags);
94 if (entry->status != IB_WC_SUCCESS)
95 to_idev(cq->ibcq.device)->n_wqe_errs++;
99 * ipath_poll_cq - poll for work completion entries
100 * @ibcq: the completion queue to poll
101 * @num_entries: the maximum number of entries to return
102 * @entry: pointer to array where work completions are placed
104 * Returns the number of completion entries polled.
106 * This may be called from interrupt context. Also called by ib_poll_cq()
107 * in the generic verbs code.
109 int ipath_poll_cq(struct ib_cq *ibcq, int num_entries, struct ib_wc *entry)
111 struct ipath_cq *cq = to_icq(ibcq);
112 struct ipath_cq_wc *wc = cq->queue;
116 spin_lock_irqsave(&cq->lock, flags);
118 for (npolled = 0; npolled < num_entries; ++npolled, ++entry) {
119 if (wc->tail == wc->head)
121 *entry = wc->queue[wc->tail];
122 if (wc->tail >= cq->ibcq.cqe)
128 spin_unlock_irqrestore(&cq->lock, flags);
133 static void send_complete(unsigned long data)
135 struct ipath_cq *cq = (struct ipath_cq *)data;
138 * The completion handler will most likely rearm the notification
139 * and poll for all pending entries. If a new completion entry
140 * is added while we are in this routine, tasklet_hi_schedule()
141 * won't call us again until we return so we check triggered to
142 * see if we need to call the handler again.
145 u8 triggered = cq->triggered;
147 cq->ibcq.comp_handler(&cq->ibcq, cq->ibcq.cq_context);
149 if (cq->triggered == triggered)
155 * ipath_create_cq - create a completion queue
156 * @ibdev: the device this completion queue is attached to
157 * @entries: the minimum size of the completion queue
158 * @context: unused by the InfiniPath driver
159 * @udata: unused by the InfiniPath driver
161 * Returns a pointer to the completion queue or negative errno values
164 * Called by ib_create_cq() in the generic verbs code.
166 struct ib_cq *ipath_create_cq(struct ib_device *ibdev, int entries,
167 struct ib_ucontext *context,
168 struct ib_udata *udata)
170 struct ipath_ibdev *dev = to_idev(ibdev);
172 struct ipath_cq_wc *wc;
175 if (entries < 1 || entries > ib_ipath_max_cqes) {
176 ret = ERR_PTR(-EINVAL);
180 if (dev->n_cqs_allocated == ib_ipath_max_cqs) {
181 ret = ERR_PTR(-ENOMEM);
185 /* Allocate the completion queue structure. */
186 cq = kmalloc(sizeof(*cq), GFP_KERNEL);
188 ret = ERR_PTR(-ENOMEM);
193 * Allocate the completion queue entries and head/tail pointers.
194 * This is allocated separately so that it can be resized and
195 * also mapped into user space.
196 * We need to use vmalloc() in order to support mmap and large
197 * numbers of entries.
199 wc = vmalloc_user(sizeof(*wc) + sizeof(struct ib_wc) * entries);
201 ret = ERR_PTR(-ENOMEM);
206 * Return the address of the WC as the offset to mmap.
207 * See ipath_mmap() for details.
209 if (udata && udata->outlen >= sizeof(__u64)) {
210 struct ipath_mmap_info *ip;
211 __u64 offset = (__u64) wc;
214 err = ib_copy_to_udata(udata, &offset, sizeof(offset));
220 /* Allocate info for ipath_mmap(). */
221 ip = kmalloc(sizeof(*ip), GFP_KERNEL);
223 ret = ERR_PTR(-ENOMEM);
227 ip->context = context;
231 ip->size = PAGE_ALIGN(sizeof(*wc) +
232 sizeof(struct ib_wc) * entries);
233 spin_lock_irq(&dev->pending_lock);
234 ip->next = dev->pending_mmaps;
235 dev->pending_mmaps = ip;
236 spin_unlock_irq(&dev->pending_lock);
241 * ib_create_cq() will initialize cq->ibcq except for cq->ibcq.cqe.
242 * The number of entries should be >= the number requested or return
245 cq->ibcq.cqe = entries;
246 cq->notify = IB_CQ_NONE;
248 spin_lock_init(&cq->lock);
249 tasklet_init(&cq->comptask, send_complete, (unsigned long)cq);
256 dev->n_cqs_allocated++;
270 * ipath_destroy_cq - destroy a completion queue
271 * @ibcq: the completion queue to destroy.
273 * Returns 0 for success.
275 * Called by ib_destroy_cq() in the generic verbs code.
277 int ipath_destroy_cq(struct ib_cq *ibcq)
279 struct ipath_ibdev *dev = to_idev(ibcq->device);
280 struct ipath_cq *cq = to_icq(ibcq);
282 tasklet_kill(&cq->comptask);
283 dev->n_cqs_allocated--;
285 kref_put(&cq->ip->ref, ipath_release_mmap_info);
294 * ipath_req_notify_cq - change the notification type for a completion queue
295 * @ibcq: the completion queue
296 * @notify: the type of notification to request
298 * Returns 0 for success.
300 * This may be called from interrupt context. Also called by
301 * ib_req_notify_cq() in the generic verbs code.
303 int ipath_req_notify_cq(struct ib_cq *ibcq, enum ib_cq_notify notify)
305 struct ipath_cq *cq = to_icq(ibcq);
308 spin_lock_irqsave(&cq->lock, flags);
310 * Don't change IB_CQ_NEXT_COMP to IB_CQ_SOLICITED but allow
311 * any other transitions (see C11-31 and C11-32 in ch. 11.4.2.2).
313 if (cq->notify != IB_CQ_NEXT_COMP)
315 spin_unlock_irqrestore(&cq->lock, flags);
319 int ipath_resize_cq(struct ib_cq *ibcq, int cqe, struct ib_udata *udata)
321 struct ipath_cq *cq = to_icq(ibcq);
322 struct ipath_cq_wc *old_wc = cq->queue;
323 struct ipath_cq_wc *wc;
327 if (cqe < 1 || cqe > ib_ipath_max_cqes) {
333 * Need to use vmalloc() if we want to support large #s of entries.
335 wc = vmalloc_user(sizeof(*wc) + sizeof(struct ib_wc) * cqe);
342 * Return the address of the WC as the offset to mmap.
343 * See ipath_mmap() for details.
345 if (udata && udata->outlen >= sizeof(__u64)) {
346 __u64 offset = (__u64) wc;
348 ret = ib_copy_to_udata(udata, &offset, sizeof(offset));
353 spin_lock_irq(&cq->lock);
355 * Make sure head and tail are sane since they
356 * might be user writable.
359 if (head > (u32) cq->ibcq.cqe)
360 head = (u32) cq->ibcq.cqe;
362 if (tail > (u32) cq->ibcq.cqe)
363 tail = (u32) cq->ibcq.cqe;
365 n = cq->ibcq.cqe + 1 + head - tail;
368 if (unlikely((u32)cqe < n)) {
369 spin_unlock_irq(&cq->lock);
374 for (n = 0; tail != head; n++) {
375 wc->queue[n] = old_wc->queue[tail];
376 if (tail == (u32) cq->ibcq.cqe)
385 spin_unlock_irq(&cq->lock);
390 struct ipath_ibdev *dev = to_idev(ibcq->device);
391 struct ipath_mmap_info *ip = cq->ip;
394 ip->size = PAGE_ALIGN(sizeof(*wc) +
395 sizeof(struct ib_wc) * cqe);
396 spin_lock_irq(&dev->pending_lock);
397 ip->next = dev->pending_mmaps;
398 dev->pending_mmaps = ip;
399 spin_unlock_irq(&dev->pending_lock);