blob: 00440d5c91e0049b50e4bd1b6d9a2e530e37a644 [file] [log] [blame]
Bryan O'Sullivancef1cce2006-03-29 15:23:36 -08001/*
Bryan O'Sullivan759d5762006-07-01 04:35:49 -07002 * Copyright (c) 2006 QLogic, Inc. 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>
35#include <linux/vmalloc.h>
36
37#include "ipath_verbs.h"
38
39/**
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
44 *
Ralph Campbell373d9912006-09-22 15:22:26 -070045 * This may be called with qp->s_lock held.
Bryan O'Sullivancef1cce2006-03-29 15:23:36 -080046 */
47void ipath_cq_enter(struct ipath_cq *cq, struct ib_wc *entry, int solicited)
48{
Ralph Campbell373d9912006-09-22 15:22:26 -070049 struct ipath_cq_wc *wc = cq->queue;
Bryan O'Sullivancef1cce2006-03-29 15:23:36 -080050 unsigned long flags;
Ralph Campbell373d9912006-09-22 15:22:26 -070051 u32 head;
Bryan O'Sullivancef1cce2006-03-29 15:23:36 -080052 u32 next;
53
54 spin_lock_irqsave(&cq->lock, flags);
55
Ralph Campbell373d9912006-09-22 15:22:26 -070056 /*
57 * Note that the head pointer might be writable by user processes.
58 * Take care to verify it is a sane value.
59 */
60 head = wc->head;
61 if (head >= (unsigned) cq->ibcq.cqe) {
62 head = cq->ibcq.cqe;
Bryan O'Sullivancef1cce2006-03-29 15:23:36 -080063 next = 0;
Ralph Campbell373d9912006-09-22 15:22:26 -070064 } else
65 next = head + 1;
66 if (unlikely(next == wc->tail)) {
Bryan O'Sullivancef1cce2006-03-29 15:23:36 -080067 spin_unlock_irqrestore(&cq->lock, flags);
68 if (cq->ibcq.event_handler) {
69 struct ib_event ev;
70
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);
75 }
76 return;
77 }
Ralph Campbell373d9912006-09-22 15:22:26 -070078 wc->queue[head] = *entry;
79 wc->head = next;
Bryan O'Sullivancef1cce2006-03-29 15:23:36 -080080
81 if (cq->notify == IB_CQ_NEXT_COMP ||
82 (cq->notify == IB_CQ_SOLICITED && solicited)) {
83 cq->notify = IB_CQ_NONE;
84 cq->triggered++;
85 /*
86 * This will cause send_complete() to be called in
87 * another thread.
88 */
89 tasklet_hi_schedule(&cq->comptask);
90 }
91
92 spin_unlock_irqrestore(&cq->lock, flags);
93
94 if (entry->status != IB_WC_SUCCESS)
95 to_idev(cq->ibcq.device)->n_wqe_errs++;
96}
97
98/**
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
103 *
104 * Returns the number of completion entries polled.
105 *
106 * This may be called from interrupt context. Also called by ib_poll_cq()
107 * in the generic verbs code.
108 */
109int ipath_poll_cq(struct ib_cq *ibcq, int num_entries, struct ib_wc *entry)
110{
111 struct ipath_cq *cq = to_icq(ibcq);
Ralph Campbell373d9912006-09-22 15:22:26 -0700112 struct ipath_cq_wc *wc = cq->queue;
Bryan O'Sullivancef1cce2006-03-29 15:23:36 -0800113 unsigned long flags;
114 int npolled;
115
116 spin_lock_irqsave(&cq->lock, flags);
117
118 for (npolled = 0; npolled < num_entries; ++npolled, ++entry) {
Ralph Campbell373d9912006-09-22 15:22:26 -0700119 if (wc->tail == wc->head)
Bryan O'Sullivancef1cce2006-03-29 15:23:36 -0800120 break;
Ralph Campbell373d9912006-09-22 15:22:26 -0700121 *entry = wc->queue[wc->tail];
122 if (wc->tail >= cq->ibcq.cqe)
123 wc->tail = 0;
Bryan O'Sullivancef1cce2006-03-29 15:23:36 -0800124 else
Ralph Campbell373d9912006-09-22 15:22:26 -0700125 wc->tail++;
Bryan O'Sullivancef1cce2006-03-29 15:23:36 -0800126 }
127
128 spin_unlock_irqrestore(&cq->lock, flags);
129
130 return npolled;
131}
132
133static void send_complete(unsigned long data)
134{
135 struct ipath_cq *cq = (struct ipath_cq *)data;
136
137 /*
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.
143 */
144 for (;;) {
145 u8 triggered = cq->triggered;
146
147 cq->ibcq.comp_handler(&cq->ibcq, cq->ibcq.cq_context);
148
149 if (cq->triggered == triggered)
150 return;
151 }
152}
153
154/**
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
160 *
161 * Returns a pointer to the completion queue or negative errno values
162 * for failure.
163 *
164 * Called by ib_create_cq() in the generic verbs code.
165 */
166struct ib_cq *ipath_create_cq(struct ib_device *ibdev, int entries,
167 struct ib_ucontext *context,
168 struct ib_udata *udata)
169{
Bryan O'Sullivanfe625462006-07-01 04:35:58 -0700170 struct ipath_ibdev *dev = to_idev(ibdev);
Bryan O'Sullivancef1cce2006-03-29 15:23:36 -0800171 struct ipath_cq *cq;
Ralph Campbell373d9912006-09-22 15:22:26 -0700172 struct ipath_cq_wc *wc;
Bryan O'Sullivancef1cce2006-03-29 15:23:36 -0800173 struct ib_cq *ret;
174
Bryan O'Sullivaneae33d42006-08-25 11:24:37 -0700175 if (entries < 1 || entries > ib_ipath_max_cqes) {
Bryan O'Sullivanfe625462006-07-01 04:35:58 -0700176 ret = ERR_PTR(-EINVAL);
Ralph Campbell373d9912006-09-22 15:22:26 -0700177 goto done;
Bryan O'Sullivanfe625462006-07-01 04:35:58 -0700178 }
179
Ralph Campbell373d9912006-09-22 15:22:26 -0700180 /* Allocate the completion queue structure. */
Bryan O'Sullivancef1cce2006-03-29 15:23:36 -0800181 cq = kmalloc(sizeof(*cq), GFP_KERNEL);
182 if (!cq) {
183 ret = ERR_PTR(-ENOMEM);
Ralph Campbell373d9912006-09-22 15:22:26 -0700184 goto done;
Bryan O'Sullivancef1cce2006-03-29 15:23:36 -0800185 }
186
187 /*
Ralph Campbell373d9912006-09-22 15:22:26 -0700188 * Allocate the completion queue entries and head/tail pointers.
189 * This is allocated separately so that it can be resized and
190 * also mapped into user space.
191 * We need to use vmalloc() in order to support mmap and large
192 * numbers of entries.
Bryan O'Sullivancef1cce2006-03-29 15:23:36 -0800193 */
Ralph Campbell373d9912006-09-22 15:22:26 -0700194 wc = vmalloc_user(sizeof(*wc) + sizeof(struct ib_wc) * entries);
Bryan O'Sullivancef1cce2006-03-29 15:23:36 -0800195 if (!wc) {
Bryan O'Sullivancef1cce2006-03-29 15:23:36 -0800196 ret = ERR_PTR(-ENOMEM);
Ralph Campbell373d9912006-09-22 15:22:26 -0700197 goto bail_cq;
Bryan O'Sullivancef1cce2006-03-29 15:23:36 -0800198 }
Ralph Campbell373d9912006-09-22 15:22:26 -0700199
200 /*
201 * Return the address of the WC as the offset to mmap.
202 * See ipath_mmap() for details.
203 */
204 if (udata && udata->outlen >= sizeof(__u64)) {
205 struct ipath_mmap_info *ip;
206 __u64 offset = (__u64) wc;
207 int err;
208
209 err = ib_copy_to_udata(udata, &offset, sizeof(offset));
210 if (err) {
211 ret = ERR_PTR(err);
212 goto bail_wc;
213 }
214
215 /* Allocate info for ipath_mmap(). */
216 ip = kmalloc(sizeof(*ip), GFP_KERNEL);
217 if (!ip) {
218 ret = ERR_PTR(-ENOMEM);
219 goto bail_wc;
220 }
221 cq->ip = ip;
222 ip->context = context;
223 ip->obj = wc;
224 kref_init(&ip->ref);
225 ip->mmap_cnt = 0;
226 ip->size = PAGE_ALIGN(sizeof(*wc) +
227 sizeof(struct ib_wc) * entries);
228 spin_lock_irq(&dev->pending_lock);
229 ip->next = dev->pending_mmaps;
230 dev->pending_mmaps = ip;
231 spin_unlock_irq(&dev->pending_lock);
232 } else
233 cq->ip = NULL;
234
Bryan O'Sullivanaa4eaed2006-09-28 09:00:03 -0700235 spin_lock(&dev->n_cqs_lock);
236 if (dev->n_cqs_allocated == ib_ipath_max_cqs) {
237 spin_unlock(&dev->n_cqs_lock);
238 ret = ERR_PTR(-ENOMEM);
239 goto bail_wc;
240 }
241
242 dev->n_cqs_allocated++;
243 spin_unlock(&dev->n_cqs_lock);
244
Bryan O'Sullivancef1cce2006-03-29 15:23:36 -0800245 /*
246 * ib_create_cq() will initialize cq->ibcq except for cq->ibcq.cqe.
247 * The number of entries should be >= the number requested or return
248 * an error.
249 */
250 cq->ibcq.cqe = entries;
251 cq->notify = IB_CQ_NONE;
252 cq->triggered = 0;
253 spin_lock_init(&cq->lock);
254 tasklet_init(&cq->comptask, send_complete, (unsigned long)cq);
Ralph Campbell373d9912006-09-22 15:22:26 -0700255 wc->head = 0;
256 wc->tail = 0;
Bryan O'Sullivancef1cce2006-03-29 15:23:36 -0800257 cq->queue = wc;
258
259 ret = &cq->ibcq;
260
Ralph Campbell373d9912006-09-22 15:22:26 -0700261 goto done;
Bryan O'Sullivanfe625462006-07-01 04:35:58 -0700262
Ralph Campbell373d9912006-09-22 15:22:26 -0700263bail_wc:
264 vfree(wc);
265
266bail_cq:
267 kfree(cq);
268
269done:
Bryan O'Sullivancef1cce2006-03-29 15:23:36 -0800270 return ret;
271}
272
273/**
274 * ipath_destroy_cq - destroy a completion queue
275 * @ibcq: the completion queue to destroy.
276 *
277 * Returns 0 for success.
278 *
279 * Called by ib_destroy_cq() in the generic verbs code.
280 */
281int ipath_destroy_cq(struct ib_cq *ibcq)
282{
Bryan O'Sullivanfe625462006-07-01 04:35:58 -0700283 struct ipath_ibdev *dev = to_idev(ibcq->device);
Bryan O'Sullivancef1cce2006-03-29 15:23:36 -0800284 struct ipath_cq *cq = to_icq(ibcq);
285
286 tasklet_kill(&cq->comptask);
Bryan O'Sullivanaa4eaed2006-09-28 09:00:03 -0700287 spin_lock(&dev->n_cqs_lock);
Bryan O'Sullivanfe625462006-07-01 04:35:58 -0700288 dev->n_cqs_allocated--;
Bryan O'Sullivanaa4eaed2006-09-28 09:00:03 -0700289 spin_unlock(&dev->n_cqs_lock);
Ralph Campbell373d9912006-09-22 15:22:26 -0700290 if (cq->ip)
291 kref_put(&cq->ip->ref, ipath_release_mmap_info);
292 else
293 vfree(cq->queue);
Bryan O'Sullivancef1cce2006-03-29 15:23:36 -0800294 kfree(cq);
295
296 return 0;
297}
298
299/**
300 * ipath_req_notify_cq - change the notification type for a completion queue
301 * @ibcq: the completion queue
302 * @notify: the type of notification to request
303 *
304 * Returns 0 for success.
305 *
306 * This may be called from interrupt context. Also called by
307 * ib_req_notify_cq() in the generic verbs code.
308 */
309int ipath_req_notify_cq(struct ib_cq *ibcq, enum ib_cq_notify notify)
310{
311 struct ipath_cq *cq = to_icq(ibcq);
312 unsigned long flags;
313
314 spin_lock_irqsave(&cq->lock, flags);
315 /*
316 * Don't change IB_CQ_NEXT_COMP to IB_CQ_SOLICITED but allow
Ralph Campbell373d9912006-09-22 15:22:26 -0700317 * any other transitions (see C11-31 and C11-32 in ch. 11.4.2.2).
Bryan O'Sullivancef1cce2006-03-29 15:23:36 -0800318 */
319 if (cq->notify != IB_CQ_NEXT_COMP)
320 cq->notify = notify;
321 spin_unlock_irqrestore(&cq->lock, flags);
322 return 0;
323}
324
325int ipath_resize_cq(struct ib_cq *ibcq, int cqe, struct ib_udata *udata)
326{
327 struct ipath_cq *cq = to_icq(ibcq);
Ralph Campbell373d9912006-09-22 15:22:26 -0700328 struct ipath_cq_wc *old_wc = cq->queue;
329 struct ipath_cq_wc *wc;
330 u32 head, tail, n;
Bryan O'Sullivancef1cce2006-03-29 15:23:36 -0800331 int ret;
332
Bryan O'Sullivaneae33d42006-08-25 11:24:37 -0700333 if (cqe < 1 || cqe > ib_ipath_max_cqes) {
334 ret = -EINVAL;
335 goto bail;
336 }
337
Bryan O'Sullivancef1cce2006-03-29 15:23:36 -0800338 /*
339 * Need to use vmalloc() if we want to support large #s of entries.
340 */
Ralph Campbell373d9912006-09-22 15:22:26 -0700341 wc = vmalloc_user(sizeof(*wc) + sizeof(struct ib_wc) * cqe);
Bryan O'Sullivancef1cce2006-03-29 15:23:36 -0800342 if (!wc) {
343 ret = -ENOMEM;
344 goto bail;
345 }
346
Ralph Campbell373d9912006-09-22 15:22:26 -0700347 /*
348 * Return the address of the WC as the offset to mmap.
349 * See ipath_mmap() for details.
350 */
351 if (udata && udata->outlen >= sizeof(__u64)) {
352 __u64 offset = (__u64) wc;
353
354 ret = ib_copy_to_udata(udata, &offset, sizeof(offset));
355 if (ret)
356 goto bail;
357 }
358
Bryan O'Sullivancef1cce2006-03-29 15:23:36 -0800359 spin_lock_irq(&cq->lock);
Ralph Campbell373d9912006-09-22 15:22:26 -0700360 /*
361 * Make sure head and tail are sane since they
362 * might be user writable.
363 */
364 head = old_wc->head;
365 if (head > (u32) cq->ibcq.cqe)
366 head = (u32) cq->ibcq.cqe;
367 tail = old_wc->tail;
368 if (tail > (u32) cq->ibcq.cqe)
369 tail = (u32) cq->ibcq.cqe;
370 if (head < tail)
371 n = cq->ibcq.cqe + 1 + head - tail;
Bryan O'Sullivancef1cce2006-03-29 15:23:36 -0800372 else
Ralph Campbell373d9912006-09-22 15:22:26 -0700373 n = head - tail;
Bryan O'Sullivancef1cce2006-03-29 15:23:36 -0800374 if (unlikely((u32)cqe < n)) {
375 spin_unlock_irq(&cq->lock);
376 vfree(wc);
377 ret = -EOVERFLOW;
378 goto bail;
379 }
Ralph Campbell373d9912006-09-22 15:22:26 -0700380 for (n = 0; tail != head; n++) {
381 wc->queue[n] = old_wc->queue[tail];
382 if (tail == (u32) cq->ibcq.cqe)
383 tail = 0;
Bryan O'Sullivancef1cce2006-03-29 15:23:36 -0800384 else
Ralph Campbell373d9912006-09-22 15:22:26 -0700385 tail++;
Bryan O'Sullivancef1cce2006-03-29 15:23:36 -0800386 }
387 cq->ibcq.cqe = cqe;
Ralph Campbell373d9912006-09-22 15:22:26 -0700388 wc->head = n;
389 wc->tail = 0;
Bryan O'Sullivancef1cce2006-03-29 15:23:36 -0800390 cq->queue = wc;
391 spin_unlock_irq(&cq->lock);
392
393 vfree(old_wc);
394
Ralph Campbell373d9912006-09-22 15:22:26 -0700395 if (cq->ip) {
396 struct ipath_ibdev *dev = to_idev(ibcq->device);
397 struct ipath_mmap_info *ip = cq->ip;
398
399 ip->obj = wc;
400 ip->size = PAGE_ALIGN(sizeof(*wc) +
401 sizeof(struct ib_wc) * cqe);
402 spin_lock_irq(&dev->pending_lock);
403 ip->next = dev->pending_mmaps;
404 dev->pending_mmaps = ip;
405 spin_unlock_irq(&dev->pending_lock);
406 }
407
Bryan O'Sullivancef1cce2006-03-29 15:23:36 -0800408 ret = 0;
409
410bail:
411 return ret;
412}