Pull video into release branch
[pandora-kernel.git] / drivers / infiniband / hw / ipath / ipath_cq.c
1 /*
2  * Copyright (c) 2006, 2007 QLogic Corporation. All rights reserved.
3  * 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  *
45  * This may be called with qp->s_lock held.
46  */
47 void ipath_cq_enter(struct ipath_cq *cq, struct ib_wc *entry, int solicited)
48 {
49         struct ipath_cq_wc *wc;
50         unsigned long flags;
51         u32 head;
52         u32 next;
53
54         spin_lock_irqsave(&cq->lock, flags);
55
56         /*
57          * Note that the head pointer might be writable by user processes.
58          * Take care to verify it is a sane value.
59          */
60         wc = cq->queue;
61         head = wc->head;
62         if (head >= (unsigned) cq->ibcq.cqe) {
63                 head = cq->ibcq.cqe;
64                 next = 0;
65         } else
66                 next = head + 1;
67         if (unlikely(next == wc->tail)) {
68                 spin_unlock_irqrestore(&cq->lock, flags);
69                 if (cq->ibcq.event_handler) {
70                         struct ib_event ev;
71
72                         ev.device = cq->ibcq.device;
73                         ev.element.cq = &cq->ibcq;
74                         ev.event = IB_EVENT_CQ_ERR;
75                         cq->ibcq.event_handler(&ev, cq->ibcq.cq_context);
76                 }
77                 return;
78         }
79         wc->queue[head].wr_id = entry->wr_id;
80         wc->queue[head].status = entry->status;
81         wc->queue[head].opcode = entry->opcode;
82         wc->queue[head].vendor_err = entry->vendor_err;
83         wc->queue[head].byte_len = entry->byte_len;
84         wc->queue[head].imm_data = (__u32 __force)entry->imm_data;
85         wc->queue[head].qp_num = entry->qp->qp_num;
86         wc->queue[head].src_qp = entry->src_qp;
87         wc->queue[head].wc_flags = entry->wc_flags;
88         wc->queue[head].pkey_index = entry->pkey_index;
89         wc->queue[head].slid = entry->slid;
90         wc->queue[head].sl = entry->sl;
91         wc->queue[head].dlid_path_bits = entry->dlid_path_bits;
92         wc->queue[head].port_num = entry->port_num;
93         /* Make sure queue entry is written before the head index. */
94         smp_wmb();
95         wc->head = next;
96
97         if (cq->notify == IB_CQ_NEXT_COMP ||
98             (cq->notify == IB_CQ_SOLICITED && solicited)) {
99                 cq->notify = IB_CQ_NONE;
100                 cq->triggered++;
101                 /*
102                  * This will cause send_complete() to be called in
103                  * another thread.
104                  */
105                 tasklet_hi_schedule(&cq->comptask);
106         }
107
108         spin_unlock_irqrestore(&cq->lock, flags);
109
110         if (entry->status != IB_WC_SUCCESS)
111                 to_idev(cq->ibcq.device)->n_wqe_errs++;
112 }
113
114 /**
115  * ipath_poll_cq - poll for work completion entries
116  * @ibcq: the completion queue to poll
117  * @num_entries: the maximum number of entries to return
118  * @entry: pointer to array where work completions are placed
119  *
120  * Returns the number of completion entries polled.
121  *
122  * This may be called from interrupt context.  Also called by ib_poll_cq()
123  * in the generic verbs code.
124  */
125 int ipath_poll_cq(struct ib_cq *ibcq, int num_entries, struct ib_wc *entry)
126 {
127         struct ipath_cq *cq = to_icq(ibcq);
128         struct ipath_cq_wc *wc;
129         unsigned long flags;
130         int npolled;
131         u32 tail;
132
133         spin_lock_irqsave(&cq->lock, flags);
134
135         wc = cq->queue;
136         tail = wc->tail;
137         if (tail > (u32) cq->ibcq.cqe)
138                 tail = (u32) cq->ibcq.cqe;
139         for (npolled = 0; npolled < num_entries; ++npolled, ++entry) {
140                 struct ipath_qp *qp;
141
142                 if (tail == wc->head)
143                         break;
144                 /* Make sure entry is read after head index is read. */
145                 smp_rmb();
146                 qp = ipath_lookup_qpn(&to_idev(cq->ibcq.device)->qp_table,
147                                       wc->queue[tail].qp_num);
148                 entry->qp = &qp->ibqp;
149                 if (atomic_dec_and_test(&qp->refcount))
150                         wake_up(&qp->wait);
151
152                 entry->wr_id = wc->queue[tail].wr_id;
153                 entry->status = wc->queue[tail].status;
154                 entry->opcode = wc->queue[tail].opcode;
155                 entry->vendor_err = wc->queue[tail].vendor_err;
156                 entry->byte_len = wc->queue[tail].byte_len;
157                 entry->imm_data = wc->queue[tail].imm_data;
158                 entry->src_qp = wc->queue[tail].src_qp;
159                 entry->wc_flags = wc->queue[tail].wc_flags;
160                 entry->pkey_index = wc->queue[tail].pkey_index;
161                 entry->slid = wc->queue[tail].slid;
162                 entry->sl = wc->queue[tail].sl;
163                 entry->dlid_path_bits = wc->queue[tail].dlid_path_bits;
164                 entry->port_num = wc->queue[tail].port_num;
165                 if (tail >= cq->ibcq.cqe)
166                         tail = 0;
167                 else
168                         tail++;
169         }
170         wc->tail = tail;
171
172         spin_unlock_irqrestore(&cq->lock, flags);
173
174         return npolled;
175 }
176
177 static void send_complete(unsigned long data)
178 {
179         struct ipath_cq *cq = (struct ipath_cq *)data;
180
181         /*
182          * The completion handler will most likely rearm the notification
183          * and poll for all pending entries.  If a new completion entry
184          * is added while we are in this routine, tasklet_hi_schedule()
185          * won't call us again until we return so we check triggered to
186          * see if we need to call the handler again.
187          */
188         for (;;) {
189                 u8 triggered = cq->triggered;
190
191                 cq->ibcq.comp_handler(&cq->ibcq, cq->ibcq.cq_context);
192
193                 if (cq->triggered == triggered)
194                         return;
195         }
196 }
197
198 /**
199  * ipath_create_cq - create a completion queue
200  * @ibdev: the device this completion queue is attached to
201  * @entries: the minimum size of the completion queue
202  * @context: unused by the InfiniPath driver
203  * @udata: unused by the InfiniPath driver
204  *
205  * Returns a pointer to the completion queue or negative errno values
206  * for failure.
207  *
208  * Called by ib_create_cq() in the generic verbs code.
209  */
210 struct ib_cq *ipath_create_cq(struct ib_device *ibdev, int entries, int comp_vector,
211                               struct ib_ucontext *context,
212                               struct ib_udata *udata)
213 {
214         struct ipath_ibdev *dev = to_idev(ibdev);
215         struct ipath_cq *cq;
216         struct ipath_cq_wc *wc;
217         struct ib_cq *ret;
218
219         if (entries < 1 || entries > ib_ipath_max_cqes) {
220                 ret = ERR_PTR(-EINVAL);
221                 goto done;
222         }
223
224         /* Allocate the completion queue structure. */
225         cq = kmalloc(sizeof(*cq), GFP_KERNEL);
226         if (!cq) {
227                 ret = ERR_PTR(-ENOMEM);
228                 goto done;
229         }
230
231         /*
232          * Allocate the completion queue entries and head/tail pointers.
233          * This is allocated separately so that it can be resized and
234          * also mapped into user space.
235          * We need to use vmalloc() in order to support mmap and large
236          * numbers of entries.
237          */
238         wc = vmalloc_user(sizeof(*wc) + sizeof(struct ib_wc) * entries);
239         if (!wc) {
240                 ret = ERR_PTR(-ENOMEM);
241                 goto bail_cq;
242         }
243
244         /*
245          * Return the address of the WC as the offset to mmap.
246          * See ipath_mmap() for details.
247          */
248         if (udata && udata->outlen >= sizeof(__u64)) {
249                 int err;
250                 u32 s = sizeof *wc + sizeof(struct ib_wc) * entries;
251
252                 cq->ip = ipath_create_mmap_info(dev, s, context, wc);
253                 if (!cq->ip) {
254                         ret = ERR_PTR(-ENOMEM);
255                         goto bail_wc;
256                 }
257
258                 err = ib_copy_to_udata(udata, &cq->ip->offset,
259                                        sizeof(cq->ip->offset));
260                 if (err) {
261                         ret = ERR_PTR(err);
262                         goto bail_ip;
263                 }
264         } else
265                 cq->ip = NULL;
266
267         spin_lock(&dev->n_cqs_lock);
268         if (dev->n_cqs_allocated == ib_ipath_max_cqs) {
269                 spin_unlock(&dev->n_cqs_lock);
270                 ret = ERR_PTR(-ENOMEM);
271                 goto bail_ip;
272         }
273
274         dev->n_cqs_allocated++;
275         spin_unlock(&dev->n_cqs_lock);
276
277         if (cq->ip) {
278                 spin_lock_irq(&dev->pending_lock);
279                 list_add(&cq->ip->pending_mmaps, &dev->pending_mmaps);
280                 spin_unlock_irq(&dev->pending_lock);
281         }
282
283         /*
284          * ib_create_cq() will initialize cq->ibcq except for cq->ibcq.cqe.
285          * The number of entries should be >= the number requested or return
286          * an error.
287          */
288         cq->ibcq.cqe = entries;
289         cq->notify = IB_CQ_NONE;
290         cq->triggered = 0;
291         spin_lock_init(&cq->lock);
292         tasklet_init(&cq->comptask, send_complete, (unsigned long)cq);
293         wc->head = 0;
294         wc->tail = 0;
295         cq->queue = wc;
296
297         ret = &cq->ibcq;
298
299         goto done;
300
301 bail_ip:
302         kfree(cq->ip);
303 bail_wc:
304         vfree(wc);
305 bail_cq:
306         kfree(cq);
307 done:
308         return ret;
309 }
310
311 /**
312  * ipath_destroy_cq - destroy a completion queue
313  * @ibcq: the completion queue to destroy.
314  *
315  * Returns 0 for success.
316  *
317  * Called by ib_destroy_cq() in the generic verbs code.
318  */
319 int ipath_destroy_cq(struct ib_cq *ibcq)
320 {
321         struct ipath_ibdev *dev = to_idev(ibcq->device);
322         struct ipath_cq *cq = to_icq(ibcq);
323
324         tasklet_kill(&cq->comptask);
325         spin_lock(&dev->n_cqs_lock);
326         dev->n_cqs_allocated--;
327         spin_unlock(&dev->n_cqs_lock);
328         if (cq->ip)
329                 kref_put(&cq->ip->ref, ipath_release_mmap_info);
330         else
331                 vfree(cq->queue);
332         kfree(cq);
333
334         return 0;
335 }
336
337 /**
338  * ipath_req_notify_cq - change the notification type for a completion queue
339  * @ibcq: the completion queue
340  * @notify_flags: the type of notification to request
341  *
342  * Returns 0 for success.
343  *
344  * This may be called from interrupt context.  Also called by
345  * ib_req_notify_cq() in the generic verbs code.
346  */
347 int ipath_req_notify_cq(struct ib_cq *ibcq, enum ib_cq_notify_flags notify_flags)
348 {
349         struct ipath_cq *cq = to_icq(ibcq);
350         unsigned long flags;
351         int ret = 0;
352
353         spin_lock_irqsave(&cq->lock, flags);
354         /*
355          * Don't change IB_CQ_NEXT_COMP to IB_CQ_SOLICITED but allow
356          * any other transitions (see C11-31 and C11-32 in ch. 11.4.2.2).
357          */
358         if (cq->notify != IB_CQ_NEXT_COMP)
359                 cq->notify = notify_flags & IB_CQ_SOLICITED_MASK;
360
361         if ((notify_flags & IB_CQ_REPORT_MISSED_EVENTS) &&
362             cq->queue->head != cq->queue->tail)
363                 ret = 1;
364
365         spin_unlock_irqrestore(&cq->lock, flags);
366
367         return ret;
368 }
369
370 /**
371  * ipath_resize_cq - change the size of the CQ
372  * @ibcq: the completion queue
373  *
374  * Returns 0 for success.
375  */
376 int ipath_resize_cq(struct ib_cq *ibcq, int cqe, struct ib_udata *udata)
377 {
378         struct ipath_cq *cq = to_icq(ibcq);
379         struct ipath_cq_wc *old_wc;
380         struct ipath_cq_wc *wc;
381         u32 head, tail, n;
382         int ret;
383
384         if (cqe < 1 || cqe > ib_ipath_max_cqes) {
385                 ret = -EINVAL;
386                 goto bail;
387         }
388
389         /*
390          * Need to use vmalloc() if we want to support large #s of entries.
391          */
392         wc = vmalloc_user(sizeof(*wc) + sizeof(struct ib_wc) * cqe);
393         if (!wc) {
394                 ret = -ENOMEM;
395                 goto bail;
396         }
397
398         /*
399          * Return the address of the WC as the offset to mmap.
400          * See ipath_mmap() for details.
401          */
402         if (udata && udata->outlen >= sizeof(__u64)) {
403                 __u64 offset = (__u64) wc;
404
405                 ret = ib_copy_to_udata(udata, &offset, sizeof(offset));
406                 if (ret)
407                         goto bail;
408         }
409
410         spin_lock_irq(&cq->lock);
411         /*
412          * Make sure head and tail are sane since they
413          * might be user writable.
414          */
415         old_wc = cq->queue;
416         head = old_wc->head;
417         if (head > (u32) cq->ibcq.cqe)
418                 head = (u32) cq->ibcq.cqe;
419         tail = old_wc->tail;
420         if (tail > (u32) cq->ibcq.cqe)
421                 tail = (u32) cq->ibcq.cqe;
422         if (head < tail)
423                 n = cq->ibcq.cqe + 1 + head - tail;
424         else
425                 n = head - tail;
426         if (unlikely((u32)cqe < n)) {
427                 spin_unlock_irq(&cq->lock);
428                 vfree(wc);
429                 ret = -EOVERFLOW;
430                 goto bail;
431         }
432         for (n = 0; tail != head; n++) {
433                 wc->queue[n] = old_wc->queue[tail];
434                 if (tail == (u32) cq->ibcq.cqe)
435                         tail = 0;
436                 else
437                         tail++;
438         }
439         cq->ibcq.cqe = cqe;
440         wc->head = n;
441         wc->tail = 0;
442         cq->queue = wc;
443         spin_unlock_irq(&cq->lock);
444
445         vfree(old_wc);
446
447         if (cq->ip) {
448                 struct ipath_ibdev *dev = to_idev(ibcq->device);
449                 struct ipath_mmap_info *ip = cq->ip;
450                 u32 s = sizeof *wc + sizeof(struct ib_wc) * cqe;
451
452                 ipath_update_mmap_info(dev, ip, s, wc);
453                 spin_lock_irq(&dev->pending_lock);
454                 if (list_empty(&ip->pending_mmaps))
455                         list_add(&ip->pending_mmaps, &dev->pending_mmaps);
456                 spin_unlock_irq(&dev->pending_lock);
457         }
458
459         ret = 0;
460
461 bail:
462         return ret;
463 }