blob: 0416c6c0e126f3dd8728fe2518b0d1ce018ce6dc [file] [log] [blame]
Bryan O'Sullivancef1cce2006-03-29 15:23:36 -08001/*
John Gregor87427da2007-06-11 10:21:14 -07002 * Copyright (c) 2006, 2007 QLogic Corporation. All rights reserved.
Bryan O'Sullivancef1cce2006-03-29 15:23:36 -08003 * Copyright (c) 2005, 2006 PathScale, Inc. All rights reserved.
4 *
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:
10 *
11 * Redistribution and use in source and binary forms, with or
12 * without modification, are permitted provided that the following
13 * conditions are met:
14 *
15 * - Redistributions of source code must retain the above
16 * copyright notice, this list of conditions and the following
17 * disclaimer.
18 *
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.
23 *
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
31 * SOFTWARE.
32 */
33
34#include <linux/err.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090035#include <linux/slab.h>
Bryan O'Sullivancef1cce2006-03-29 15:23:36 -080036#include <linux/vmalloc.h>
37
38#include "ipath_verbs.h"
39
40/**
41 * ipath_cq_enter - add a new entry to the completion queue
42 * @cq: completion queue
43 * @entry: work completion entry to add
44 * @sig: true if @entry is a solicitated entry
45 *
Ralph Campbell373d9912006-09-22 15:22:26 -070046 * This may be called with qp->s_lock held.
Bryan O'Sullivancef1cce2006-03-29 15:23:36 -080047 */
48void ipath_cq_enter(struct ipath_cq *cq, struct ib_wc *entry, int solicited)
49{
Bryan O'Sullivan7a26c472006-09-28 09:00:23 -070050 struct ipath_cq_wc *wc;
Bryan O'Sullivancef1cce2006-03-29 15:23:36 -080051 unsigned long flags;
Ralph Campbell373d9912006-09-22 15:22:26 -070052 u32 head;
Bryan O'Sullivancef1cce2006-03-29 15:23:36 -080053 u32 next;
54
55 spin_lock_irqsave(&cq->lock, flags);
56
Ralph Campbell373d9912006-09-22 15:22:26 -070057 /*
58 * Note that the head pointer might be writable by user processes.
59 * Take care to verify it is a sane value.
60 */
Bryan O'Sullivan7a26c472006-09-28 09:00:23 -070061 wc = cq->queue;
Ralph Campbell373d9912006-09-22 15:22:26 -070062 head = wc->head;
63 if (head >= (unsigned) cq->ibcq.cqe) {
64 head = cq->ibcq.cqe;
Bryan O'Sullivancef1cce2006-03-29 15:23:36 -080065 next = 0;
Ralph Campbell373d9912006-09-22 15:22:26 -070066 } else
67 next = head + 1;
68 if (unlikely(next == wc->tail)) {
Bryan O'Sullivancef1cce2006-03-29 15:23:36 -080069 spin_unlock_irqrestore(&cq->lock, flags);
70 if (cq->ibcq.event_handler) {
71 struct ib_event ev;
72
73 ev.device = cq->ibcq.device;
74 ev.element.cq = &cq->ibcq;
75 ev.event = IB_EVENT_CQ_ERR;
76 cq->ibcq.event_handler(&ev, cq->ibcq.cq_context);
77 }
78 return;
79 }
Ralph Campbell6cff2fa2007-09-07 16:54:01 -070080 if (cq->ip) {
81 wc->uqueue[head].wr_id = entry->wr_id;
82 wc->uqueue[head].status = entry->status;
83 wc->uqueue[head].opcode = entry->opcode;
84 wc->uqueue[head].vendor_err = entry->vendor_err;
85 wc->uqueue[head].byte_len = entry->byte_len;
Steve Wise00f7ec32008-07-14 23:48:45 -070086 wc->uqueue[head].ex.imm_data = (__u32 __force) entry->ex.imm_data;
Ralph Campbell6cff2fa2007-09-07 16:54:01 -070087 wc->uqueue[head].qp_num = entry->qp->qp_num;
88 wc->uqueue[head].src_qp = entry->src_qp;
89 wc->uqueue[head].wc_flags = entry->wc_flags;
90 wc->uqueue[head].pkey_index = entry->pkey_index;
91 wc->uqueue[head].slid = entry->slid;
92 wc->uqueue[head].sl = entry->sl;
93 wc->uqueue[head].dlid_path_bits = entry->dlid_path_bits;
94 wc->uqueue[head].port_num = entry->port_num;
95 /* Make sure entry is written before the head index. */
96 smp_wmb();
97 } else
98 wc->kqueue[head] = *entry;
Ralph Campbell373d9912006-09-22 15:22:26 -070099 wc->head = next;
Bryan O'Sullivancef1cce2006-03-29 15:23:36 -0800100
101 if (cq->notify == IB_CQ_NEXT_COMP ||
102 (cq->notify == IB_CQ_SOLICITED && solicited)) {
103 cq->notify = IB_CQ_NONE;
104 cq->triggered++;
105 /*
106 * This will cause send_complete() to be called in
107 * another thread.
108 */
109 tasklet_hi_schedule(&cq->comptask);
110 }
111
112 spin_unlock_irqrestore(&cq->lock, flags);
113
114 if (entry->status != IB_WC_SUCCESS)
115 to_idev(cq->ibcq.device)->n_wqe_errs++;
116}
117
118/**
119 * ipath_poll_cq - poll for work completion entries
120 * @ibcq: the completion queue to poll
121 * @num_entries: the maximum number of entries to return
122 * @entry: pointer to array where work completions are placed
123 *
124 * Returns the number of completion entries polled.
125 *
126 * This may be called from interrupt context. Also called by ib_poll_cq()
127 * in the generic verbs code.
128 */
129int ipath_poll_cq(struct ib_cq *ibcq, int num_entries, struct ib_wc *entry)
130{
131 struct ipath_cq *cq = to_icq(ibcq);
Bryan O'Sullivan7a26c472006-09-28 09:00:23 -0700132 struct ipath_cq_wc *wc;
Bryan O'Sullivancef1cce2006-03-29 15:23:36 -0800133 unsigned long flags;
134 int npolled;
Bryan O'Sullivan7a26c472006-09-28 09:00:23 -0700135 u32 tail;
Bryan O'Sullivancef1cce2006-03-29 15:23:36 -0800136
Ralph Campbell6cff2fa2007-09-07 16:54:01 -0700137 /* The kernel can only poll a kernel completion queue */
138 if (cq->ip) {
139 npolled = -EINVAL;
140 goto bail;
141 }
142
Bryan O'Sullivancef1cce2006-03-29 15:23:36 -0800143 spin_lock_irqsave(&cq->lock, flags);
144
Bryan O'Sullivan7a26c472006-09-28 09:00:23 -0700145 wc = cq->queue;
146 tail = wc->tail;
147 if (tail > (u32) cq->ibcq.cqe)
148 tail = (u32) cq->ibcq.cqe;
Bryan O'Sullivancef1cce2006-03-29 15:23:36 -0800149 for (npolled = 0; npolled < num_entries; ++npolled, ++entry) {
Bryan O'Sullivan7a26c472006-09-28 09:00:23 -0700150 if (tail == wc->head)
Bryan O'Sullivancef1cce2006-03-29 15:23:36 -0800151 break;
Ralph Campbell6cff2fa2007-09-07 16:54:01 -0700152 /* The kernel doesn't need a RMB since it has the lock. */
153 *entry = wc->kqueue[tail];
Bryan O'Sullivan7a26c472006-09-28 09:00:23 -0700154 if (tail >= cq->ibcq.cqe)
155 tail = 0;
Bryan O'Sullivancef1cce2006-03-29 15:23:36 -0800156 else
Bryan O'Sullivan7a26c472006-09-28 09:00:23 -0700157 tail++;
Bryan O'Sullivancef1cce2006-03-29 15:23:36 -0800158 }
Bryan O'Sullivan7a26c472006-09-28 09:00:23 -0700159 wc->tail = tail;
Bryan O'Sullivancef1cce2006-03-29 15:23:36 -0800160
161 spin_unlock_irqrestore(&cq->lock, flags);
162
Ralph Campbell6cff2fa2007-09-07 16:54:01 -0700163bail:
Bryan O'Sullivancef1cce2006-03-29 15:23:36 -0800164 return npolled;
165}
166
167static void send_complete(unsigned long data)
168{
169 struct ipath_cq *cq = (struct ipath_cq *)data;
170
171 /*
172 * The completion handler will most likely rearm the notification
173 * and poll for all pending entries. If a new completion entry
174 * is added while we are in this routine, tasklet_hi_schedule()
175 * won't call us again until we return so we check triggered to
176 * see if we need to call the handler again.
177 */
178 for (;;) {
179 u8 triggered = cq->triggered;
180
181 cq->ibcq.comp_handler(&cq->ibcq, cq->ibcq.cq_context);
182
183 if (cq->triggered == triggered)
184 return;
185 }
186}
187
188/**
189 * ipath_create_cq - create a completion queue
190 * @ibdev: the device this completion queue is attached to
191 * @entries: the minimum size of the completion queue
192 * @context: unused by the InfiniPath driver
193 * @udata: unused by the InfiniPath driver
194 *
195 * Returns a pointer to the completion queue or negative errno values
196 * for failure.
197 *
198 * Called by ib_create_cq() in the generic verbs code.
199 */
Michael S. Tsirkinf4fd0b22007-05-03 13:48:47 +0300200struct ib_cq *ipath_create_cq(struct ib_device *ibdev, int entries, int comp_vector,
Bryan O'Sullivancef1cce2006-03-29 15:23:36 -0800201 struct ib_ucontext *context,
202 struct ib_udata *udata)
203{
Bryan O'Sullivanfe625462006-07-01 04:35:58 -0700204 struct ipath_ibdev *dev = to_idev(ibdev);
Bryan O'Sullivancef1cce2006-03-29 15:23:36 -0800205 struct ipath_cq *cq;
Ralph Campbell373d9912006-09-22 15:22:26 -0700206 struct ipath_cq_wc *wc;
Bryan O'Sullivancef1cce2006-03-29 15:23:36 -0800207 struct ib_cq *ret;
Ralph Campbell6cff2fa2007-09-07 16:54:01 -0700208 u32 sz;
Bryan O'Sullivancef1cce2006-03-29 15:23:36 -0800209
Bryan O'Sullivaneae33d42006-08-25 11:24:37 -0700210 if (entries < 1 || entries > ib_ipath_max_cqes) {
Bryan O'Sullivanfe625462006-07-01 04:35:58 -0700211 ret = ERR_PTR(-EINVAL);
Ralph Campbell373d9912006-09-22 15:22:26 -0700212 goto done;
Bryan O'Sullivanfe625462006-07-01 04:35:58 -0700213 }
214
Ralph Campbell373d9912006-09-22 15:22:26 -0700215 /* Allocate the completion queue structure. */
Bryan O'Sullivancef1cce2006-03-29 15:23:36 -0800216 cq = kmalloc(sizeof(*cq), GFP_KERNEL);
217 if (!cq) {
218 ret = ERR_PTR(-ENOMEM);
Ralph Campbell373d9912006-09-22 15:22:26 -0700219 goto done;
Bryan O'Sullivancef1cce2006-03-29 15:23:36 -0800220 }
221
222 /*
Ralph Campbell373d9912006-09-22 15:22:26 -0700223 * Allocate the completion queue entries and head/tail pointers.
224 * This is allocated separately so that it can be resized and
225 * also mapped into user space.
226 * We need to use vmalloc() in order to support mmap and large
227 * numbers of entries.
Bryan O'Sullivancef1cce2006-03-29 15:23:36 -0800228 */
Ralph Campbell6cff2fa2007-09-07 16:54:01 -0700229 sz = sizeof(*wc);
230 if (udata && udata->outlen >= sizeof(__u64))
231 sz += sizeof(struct ib_uverbs_wc) * (entries + 1);
232 else
233 sz += sizeof(struct ib_wc) * (entries + 1);
234 wc = vmalloc_user(sz);
Bryan O'Sullivancef1cce2006-03-29 15:23:36 -0800235 if (!wc) {
Bryan O'Sullivancef1cce2006-03-29 15:23:36 -0800236 ret = ERR_PTR(-ENOMEM);
Ralph Campbell373d9912006-09-22 15:22:26 -0700237 goto bail_cq;
Bryan O'Sullivancef1cce2006-03-29 15:23:36 -0800238 }
Ralph Campbell373d9912006-09-22 15:22:26 -0700239
240 /*
241 * Return the address of the WC as the offset to mmap.
242 * See ipath_mmap() for details.
243 */
244 if (udata && udata->outlen >= sizeof(__u64)) {
Ralph Campbell373d9912006-09-22 15:22:26 -0700245 int err;
246
Ralph Campbell6cff2fa2007-09-07 16:54:01 -0700247 cq->ip = ipath_create_mmap_info(dev, sz, context, wc);
Robert Walsh6b66b2d2007-04-27 21:07:23 -0700248 if (!cq->ip) {
Ralph Campbell373d9912006-09-22 15:22:26 -0700249 ret = ERR_PTR(-ENOMEM);
250 goto bail_wc;
251 }
Robert Walsh6b66b2d2007-04-27 21:07:23 -0700252
253 err = ib_copy_to_udata(udata, &cq->ip->offset,
254 sizeof(cq->ip->offset));
255 if (err) {
256 ret = ERR_PTR(err);
257 goto bail_ip;
258 }
Ralph Campbell373d9912006-09-22 15:22:26 -0700259 } else
260 cq->ip = NULL;
261
Bryan O'Sullivanaa4eaed2006-09-28 09:00:03 -0700262 spin_lock(&dev->n_cqs_lock);
263 if (dev->n_cqs_allocated == ib_ipath_max_cqs) {
264 spin_unlock(&dev->n_cqs_lock);
265 ret = ERR_PTR(-ENOMEM);
Robert Walsh6b66b2d2007-04-27 21:07:23 -0700266 goto bail_ip;
Bryan O'Sullivanaa4eaed2006-09-28 09:00:03 -0700267 }
268
269 dev->n_cqs_allocated++;
270 spin_unlock(&dev->n_cqs_lock);
271
Robert Walsh6b66b2d2007-04-27 21:07:23 -0700272 if (cq->ip) {
273 spin_lock_irq(&dev->pending_lock);
274 list_add(&cq->ip->pending_mmaps, &dev->pending_mmaps);
275 spin_unlock_irq(&dev->pending_lock);
276 }
277
Bryan O'Sullivancef1cce2006-03-29 15:23:36 -0800278 /*
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
281 * an error.
282 */
283 cq->ibcq.cqe = entries;
284 cq->notify = IB_CQ_NONE;
285 cq->triggered = 0;
286 spin_lock_init(&cq->lock);
287 tasklet_init(&cq->comptask, send_complete, (unsigned long)cq);
Ralph Campbell373d9912006-09-22 15:22:26 -0700288 wc->head = 0;
289 wc->tail = 0;
Bryan O'Sullivancef1cce2006-03-29 15:23:36 -0800290 cq->queue = wc;
291
292 ret = &cq->ibcq;
293
Ralph Campbell373d9912006-09-22 15:22:26 -0700294 goto done;
Bryan O'Sullivanfe625462006-07-01 04:35:58 -0700295
Robert Walsh6b66b2d2007-04-27 21:07:23 -0700296bail_ip:
297 kfree(cq->ip);
Ralph Campbell373d9912006-09-22 15:22:26 -0700298bail_wc:
299 vfree(wc);
Ralph Campbell373d9912006-09-22 15:22:26 -0700300bail_cq:
301 kfree(cq);
Ralph Campbell373d9912006-09-22 15:22:26 -0700302done:
Bryan O'Sullivancef1cce2006-03-29 15:23:36 -0800303 return ret;
304}
305
306/**
307 * ipath_destroy_cq - destroy a completion queue
308 * @ibcq: the completion queue to destroy.
309 *
310 * Returns 0 for success.
311 *
312 * Called by ib_destroy_cq() in the generic verbs code.
313 */
314int ipath_destroy_cq(struct ib_cq *ibcq)
315{
Bryan O'Sullivanfe625462006-07-01 04:35:58 -0700316 struct ipath_ibdev *dev = to_idev(ibcq->device);
Bryan O'Sullivancef1cce2006-03-29 15:23:36 -0800317 struct ipath_cq *cq = to_icq(ibcq);
318
319 tasklet_kill(&cq->comptask);
Bryan O'Sullivanaa4eaed2006-09-28 09:00:03 -0700320 spin_lock(&dev->n_cqs_lock);
Bryan O'Sullivanfe625462006-07-01 04:35:58 -0700321 dev->n_cqs_allocated--;
Bryan O'Sullivanaa4eaed2006-09-28 09:00:03 -0700322 spin_unlock(&dev->n_cqs_lock);
Ralph Campbell373d9912006-09-22 15:22:26 -0700323 if (cq->ip)
324 kref_put(&cq->ip->ref, ipath_release_mmap_info);
325 else
326 vfree(cq->queue);
Bryan O'Sullivancef1cce2006-03-29 15:23:36 -0800327 kfree(cq);
328
329 return 0;
330}
331
332/**
333 * ipath_req_notify_cq - change the notification type for a completion queue
334 * @ibcq: the completion queue
Roland Dreiered23a722007-05-06 21:02:48 -0700335 * @notify_flags: the type of notification to request
Bryan O'Sullivancef1cce2006-03-29 15:23:36 -0800336 *
337 * Returns 0 for success.
338 *
339 * This may be called from interrupt context. Also called by
340 * ib_req_notify_cq() in the generic verbs code.
341 */
Roland Dreiered23a722007-05-06 21:02:48 -0700342int ipath_req_notify_cq(struct ib_cq *ibcq, enum ib_cq_notify_flags notify_flags)
Bryan O'Sullivancef1cce2006-03-29 15:23:36 -0800343{
344 struct ipath_cq *cq = to_icq(ibcq);
345 unsigned long flags;
Roland Dreiered23a722007-05-06 21:02:48 -0700346 int ret = 0;
Bryan O'Sullivancef1cce2006-03-29 15:23:36 -0800347
348 spin_lock_irqsave(&cq->lock, flags);
349 /*
350 * Don't change IB_CQ_NEXT_COMP to IB_CQ_SOLICITED but allow
Ralph Campbell373d9912006-09-22 15:22:26 -0700351 * any other transitions (see C11-31 and C11-32 in ch. 11.4.2.2).
Bryan O'Sullivancef1cce2006-03-29 15:23:36 -0800352 */
353 if (cq->notify != IB_CQ_NEXT_COMP)
Roland Dreiered23a722007-05-06 21:02:48 -0700354 cq->notify = notify_flags & IB_CQ_SOLICITED_MASK;
355
356 if ((notify_flags & IB_CQ_REPORT_MISSED_EVENTS) &&
357 cq->queue->head != cq->queue->tail)
358 ret = 1;
359
Bryan O'Sullivancef1cce2006-03-29 15:23:36 -0800360 spin_unlock_irqrestore(&cq->lock, flags);
Roland Dreiered23a722007-05-06 21:02:48 -0700361
362 return ret;
Bryan O'Sullivancef1cce2006-03-29 15:23:36 -0800363}
364
Bryan O'Sullivan7a26c472006-09-28 09:00:23 -0700365/**
366 * ipath_resize_cq - change the size of the CQ
367 * @ibcq: the completion queue
368 *
369 * Returns 0 for success.
370 */
Bryan O'Sullivancef1cce2006-03-29 15:23:36 -0800371int ipath_resize_cq(struct ib_cq *ibcq, int cqe, struct ib_udata *udata)
372{
373 struct ipath_cq *cq = to_icq(ibcq);
Bryan O'Sullivan7a26c472006-09-28 09:00:23 -0700374 struct ipath_cq_wc *old_wc;
Ralph Campbell373d9912006-09-22 15:22:26 -0700375 struct ipath_cq_wc *wc;
376 u32 head, tail, n;
Bryan O'Sullivancef1cce2006-03-29 15:23:36 -0800377 int ret;
Ralph Campbell6cff2fa2007-09-07 16:54:01 -0700378 u32 sz;
Bryan O'Sullivancef1cce2006-03-29 15:23:36 -0800379
Bryan O'Sullivaneae33d42006-08-25 11:24:37 -0700380 if (cqe < 1 || cqe > ib_ipath_max_cqes) {
381 ret = -EINVAL;
382 goto bail;
383 }
384
Bryan O'Sullivancef1cce2006-03-29 15:23:36 -0800385 /*
386 * Need to use vmalloc() if we want to support large #s of entries.
387 */
Ralph Campbell6cff2fa2007-09-07 16:54:01 -0700388 sz = sizeof(*wc);
389 if (udata && udata->outlen >= sizeof(__u64))
390 sz += sizeof(struct ib_uverbs_wc) * (cqe + 1);
391 else
392 sz += sizeof(struct ib_wc) * (cqe + 1);
393 wc = vmalloc_user(sz);
Bryan O'Sullivancef1cce2006-03-29 15:23:36 -0800394 if (!wc) {
395 ret = -ENOMEM;
396 goto bail;
397 }
398
Ralph Campbellfb74dac2007-11-06 17:51:38 -0800399 /* Check that we can write the offset to mmap. */
Ralph Campbell373d9912006-09-22 15:22:26 -0700400 if (udata && udata->outlen >= sizeof(__u64)) {
Ralph Campbellfb74dac2007-11-06 17:51:38 -0800401 __u64 offset = 0;
Ralph Campbell373d9912006-09-22 15:22:26 -0700402
403 ret = ib_copy_to_udata(udata, &offset, sizeof(offset));
404 if (ret)
Ralph Campbella6e75502007-10-24 15:49:39 -0700405 goto bail_free;
Ralph Campbell373d9912006-09-22 15:22:26 -0700406 }
407
Bryan O'Sullivancef1cce2006-03-29 15:23:36 -0800408 spin_lock_irq(&cq->lock);
Ralph Campbell373d9912006-09-22 15:22:26 -0700409 /*
410 * Make sure head and tail are sane since they
411 * might be user writable.
412 */
Bryan O'Sullivan7a26c472006-09-28 09:00:23 -0700413 old_wc = cq->queue;
Ralph Campbell373d9912006-09-22 15:22:26 -0700414 head = old_wc->head;
415 if (head > (u32) cq->ibcq.cqe)
416 head = (u32) cq->ibcq.cqe;
417 tail = old_wc->tail;
418 if (tail > (u32) cq->ibcq.cqe)
419 tail = (u32) cq->ibcq.cqe;
420 if (head < tail)
421 n = cq->ibcq.cqe + 1 + head - tail;
Bryan O'Sullivancef1cce2006-03-29 15:23:36 -0800422 else
Ralph Campbell373d9912006-09-22 15:22:26 -0700423 n = head - tail;
Bryan O'Sullivancef1cce2006-03-29 15:23:36 -0800424 if (unlikely((u32)cqe < n)) {
Ralph Campbell733d1282007-11-08 19:53:01 -0800425 ret = -EINVAL;
Ralph Campbella6e75502007-10-24 15:49:39 -0700426 goto bail_unlock;
Bryan O'Sullivancef1cce2006-03-29 15:23:36 -0800427 }
Ralph Campbell373d9912006-09-22 15:22:26 -0700428 for (n = 0; tail != head; n++) {
Ralph Campbell6cff2fa2007-09-07 16:54:01 -0700429 if (cq->ip)
430 wc->uqueue[n] = old_wc->uqueue[tail];
431 else
432 wc->kqueue[n] = old_wc->kqueue[tail];
Ralph Campbell373d9912006-09-22 15:22:26 -0700433 if (tail == (u32) cq->ibcq.cqe)
434 tail = 0;
Bryan O'Sullivancef1cce2006-03-29 15:23:36 -0800435 else
Ralph Campbell373d9912006-09-22 15:22:26 -0700436 tail++;
Bryan O'Sullivancef1cce2006-03-29 15:23:36 -0800437 }
438 cq->ibcq.cqe = cqe;
Ralph Campbell373d9912006-09-22 15:22:26 -0700439 wc->head = n;
440 wc->tail = 0;
Bryan O'Sullivancef1cce2006-03-29 15:23:36 -0800441 cq->queue = wc;
442 spin_unlock_irq(&cq->lock);
443
444 vfree(old_wc);
445
Ralph Campbell373d9912006-09-22 15:22:26 -0700446 if (cq->ip) {
447 struct ipath_ibdev *dev = to_idev(ibcq->device);
448 struct ipath_mmap_info *ip = cq->ip;
449
Ralph Campbell6cff2fa2007-09-07 16:54:01 -0700450 ipath_update_mmap_info(dev, ip, sz, wc);
Ralph Campbellfb74dac2007-11-06 17:51:38 -0800451
452 /*
453 * Return the offset to mmap.
454 * See ipath_mmap() for details.
455 */
456 if (udata && udata->outlen >= sizeof(__u64)) {
457 ret = ib_copy_to_udata(udata, &ip->offset,
458 sizeof(ip->offset));
459 if (ret)
460 goto bail;
461 }
462
Ralph Campbell373d9912006-09-22 15:22:26 -0700463 spin_lock_irq(&dev->pending_lock);
Robert Walsh6b66b2d2007-04-27 21:07:23 -0700464 if (list_empty(&ip->pending_mmaps))
465 list_add(&ip->pending_mmaps, &dev->pending_mmaps);
Ralph Campbell373d9912006-09-22 15:22:26 -0700466 spin_unlock_irq(&dev->pending_lock);
467 }
468
Bryan O'Sullivancef1cce2006-03-29 15:23:36 -0800469 ret = 0;
Ralph Campbella6e75502007-10-24 15:49:39 -0700470 goto bail;
Bryan O'Sullivancef1cce2006-03-29 15:23:36 -0800471
Ralph Campbella6e75502007-10-24 15:49:39 -0700472bail_unlock:
473 spin_unlock_irq(&cq->lock);
474bail_free:
475 vfree(wc);
Bryan O'Sullivancef1cce2006-03-29 15:23:36 -0800476bail:
477 return ret;
478}