IB/ipath: Fix mismatch in shifts and masks for printing debug info
[powerpc.git] / drivers / infiniband / hw / ipath / ipath_cq.c
1 /*
2  * Copyright (c) 2006 QLogic, Inc. 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 = cq->queue;
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         head = wc->head;
61         if (head >= (unsigned) cq->ibcq.cqe) {
62                 head = cq->ibcq.cqe;
63                 next = 0;
64         } else
65                 next = head + 1;
66         if (unlikely(next == wc->tail)) {
67                 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         }
78         wc->queue[head] = *entry;
79         wc->head = next;
80
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  */
109 int ipath_poll_cq(struct ib_cq *ibcq, int num_entries, struct ib_wc *entry)
110 {
111         struct ipath_cq *cq = to_icq(ibcq);
112         struct ipath_cq_wc *wc = cq->queue;
113         unsigned long flags;
114         int npolled;
115
116         spin_lock_irqsave(&cq->lock, flags);
117
118         for (npolled = 0; npolled < num_entries; ++npolled, ++entry) {
119                 if (wc->tail == wc->head)
120                         break;
121                 *entry = wc->queue[wc->tail];
122                 if (wc->tail >= cq->ibcq.cqe)
123                         wc->tail = 0;
124                 else
125                         wc->tail++;
126         }
127
128         spin_unlock_irqrestore(&cq->lock, flags);
129
130         return npolled;
131 }
132
133 static 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  */
166 struct ib_cq *ipath_create_cq(struct ib_device *ibdev, int entries,
167                               struct ib_ucontext *context,
168                               struct ib_udata *udata)
169 {
170         struct ipath_ibdev *dev = to_idev(ibdev);
171         struct ipath_cq *cq;
172         struct ipath_cq_wc *wc;
173         struct ib_cq *ret;
174
175         if (entries < 1 || entries > ib_ipath_max_cqes) {
176                 ret = ERR_PTR(-EINVAL);
177                 goto done;
178         }
179
180         /* Allocate the completion queue structure. */
181         cq = kmalloc(sizeof(*cq), GFP_KERNEL);
182         if (!cq) {
183                 ret = ERR_PTR(-ENOMEM);
184                 goto done;
185         }
186
187         /*
188          * 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.
193          */
194         wc = vmalloc_user(sizeof(*wc) + sizeof(struct ib_wc) * entries);
195         if (!wc) {
196                 ret = ERR_PTR(-ENOMEM);
197                 goto bail_cq;
198         }
199
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
235         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
245         /*
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);
255         wc->head = 0;
256         wc->tail = 0;
257         cq->queue = wc;
258
259         ret = &cq->ibcq;
260
261         goto done;
262
263 bail_wc:
264         vfree(wc);
265
266 bail_cq:
267         kfree(cq);
268
269 done:
270         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  */
281 int ipath_destroy_cq(struct ib_cq *ibcq)
282 {
283         struct ipath_ibdev *dev = to_idev(ibcq->device);
284         struct ipath_cq *cq = to_icq(ibcq);
285
286         tasklet_kill(&cq->comptask);
287         spin_lock(&dev->n_cqs_lock);
288         dev->n_cqs_allocated--;
289         spin_unlock(&dev->n_cqs_lock);
290         if (cq->ip)
291                 kref_put(&cq->ip->ref, ipath_release_mmap_info);
292         else
293                 vfree(cq->queue);
294         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  */
309 int 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
317          * any other transitions (see C11-31 and C11-32 in ch. 11.4.2.2).
318          */
319         if (cq->notify != IB_CQ_NEXT_COMP)
320                 cq->notify = notify;
321         spin_unlock_irqrestore(&cq->lock, flags);
322         return 0;
323 }
324
325 int ipath_resize_cq(struct ib_cq *ibcq, int cqe, struct ib_udata *udata)
326 {
327         struct ipath_cq *cq = to_icq(ibcq);
328         struct ipath_cq_wc *old_wc = cq->queue;
329         struct ipath_cq_wc *wc;
330         u32 head, tail, n;
331         int ret;
332
333         if (cqe < 1 || cqe > ib_ipath_max_cqes) {
334                 ret = -EINVAL;
335                 goto bail;
336         }
337
338         /*
339          * Need to use vmalloc() if we want to support large #s of entries.
340          */
341         wc = vmalloc_user(sizeof(*wc) + sizeof(struct ib_wc) * cqe);
342         if (!wc) {
343                 ret = -ENOMEM;
344                 goto bail;
345         }
346
347         /*
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
359         spin_lock_irq(&cq->lock);
360         /*
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;
372         else
373                 n = head - tail;
374         if (unlikely((u32)cqe < n)) {
375                 spin_unlock_irq(&cq->lock);
376                 vfree(wc);
377                 ret = -EOVERFLOW;
378                 goto bail;
379         }
380         for (n = 0; tail != head; n++) {
381                 wc->queue[n] = old_wc->queue[tail];
382                 if (tail == (u32) cq->ibcq.cqe)
383                         tail = 0;
384                 else
385                         tail++;
386         }
387         cq->ibcq.cqe = cqe;
388         wc->head = n;
389         wc->tail = 0;
390         cq->queue = wc;
391         spin_unlock_irq(&cq->lock);
392
393         vfree(old_wc);
394
395         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
408         ret = 0;
409
410 bail:
411         return ret;
412 }