[PATCH] IB/ipath: fix shared receive queues for RC
[linux-2.6-block.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 one of the qp->s_lock or qp->r_rq.lock held.
46  */
47 void ipath_cq_enter(struct ipath_cq *cq, struct ib_wc *entry, int solicited)
48 {
49         unsigned long flags;
50         u32 next;
51
52         spin_lock_irqsave(&cq->lock, flags);
53
54         if (cq->head == cq->ibcq.cqe)
55                 next = 0;
56         else
57                 next = cq->head + 1;
58         if (unlikely(next == cq->tail)) {
59                 spin_unlock_irqrestore(&cq->lock, flags);
60                 if (cq->ibcq.event_handler) {
61                         struct ib_event ev;
62
63                         ev.device = cq->ibcq.device;
64                         ev.element.cq = &cq->ibcq;
65                         ev.event = IB_EVENT_CQ_ERR;
66                         cq->ibcq.event_handler(&ev, cq->ibcq.cq_context);
67                 }
68                 return;
69         }
70         cq->queue[cq->head] = *entry;
71         cq->head = next;
72
73         if (cq->notify == IB_CQ_NEXT_COMP ||
74             (cq->notify == IB_CQ_SOLICITED && solicited)) {
75                 cq->notify = IB_CQ_NONE;
76                 cq->triggered++;
77                 /*
78                  * This will cause send_complete() to be called in
79                  * another thread.
80                  */
81                 tasklet_hi_schedule(&cq->comptask);
82         }
83
84         spin_unlock_irqrestore(&cq->lock, flags);
85
86         if (entry->status != IB_WC_SUCCESS)
87                 to_idev(cq->ibcq.device)->n_wqe_errs++;
88 }
89
90 /**
91  * ipath_poll_cq - poll for work completion entries
92  * @ibcq: the completion queue to poll
93  * @num_entries: the maximum number of entries to return
94  * @entry: pointer to array where work completions are placed
95  *
96  * Returns the number of completion entries polled.
97  *
98  * This may be called from interrupt context.  Also called by ib_poll_cq()
99  * in the generic verbs code.
100  */
101 int ipath_poll_cq(struct ib_cq *ibcq, int num_entries, struct ib_wc *entry)
102 {
103         struct ipath_cq *cq = to_icq(ibcq);
104         unsigned long flags;
105         int npolled;
106
107         spin_lock_irqsave(&cq->lock, flags);
108
109         for (npolled = 0; npolled < num_entries; ++npolled, ++entry) {
110                 if (cq->tail == cq->head)
111                         break;
112                 *entry = cq->queue[cq->tail];
113                 if (cq->tail == cq->ibcq.cqe)
114                         cq->tail = 0;
115                 else
116                         cq->tail++;
117         }
118
119         spin_unlock_irqrestore(&cq->lock, flags);
120
121         return npolled;
122 }
123
124 static void send_complete(unsigned long data)
125 {
126         struct ipath_cq *cq = (struct ipath_cq *)data;
127
128         /*
129          * The completion handler will most likely rearm the notification
130          * and poll for all pending entries.  If a new completion entry
131          * is added while we are in this routine, tasklet_hi_schedule()
132          * won't call us again until we return so we check triggered to
133          * see if we need to call the handler again.
134          */
135         for (;;) {
136                 u8 triggered = cq->triggered;
137
138                 cq->ibcq.comp_handler(&cq->ibcq, cq->ibcq.cq_context);
139
140                 if (cq->triggered == triggered)
141                         return;
142         }
143 }
144
145 /**
146  * ipath_create_cq - create a completion queue
147  * @ibdev: the device this completion queue is attached to
148  * @entries: the minimum size of the completion queue
149  * @context: unused by the InfiniPath driver
150  * @udata: unused by the InfiniPath driver
151  *
152  * Returns a pointer to the completion queue or negative errno values
153  * for failure.
154  *
155  * Called by ib_create_cq() in the generic verbs code.
156  */
157 struct ib_cq *ipath_create_cq(struct ib_device *ibdev, int entries,
158                               struct ib_ucontext *context,
159                               struct ib_udata *udata)
160 {
161         struct ipath_cq *cq;
162         struct ib_wc *wc;
163         struct ib_cq *ret;
164
165         /*
166          * Need to use vmalloc() if we want to support large #s of
167          * entries.
168          */
169         cq = kmalloc(sizeof(*cq), GFP_KERNEL);
170         if (!cq) {
171                 ret = ERR_PTR(-ENOMEM);
172                 goto bail;
173         }
174
175         /*
176          * Need to use vmalloc() if we want to support large #s of entries.
177          */
178         wc = vmalloc(sizeof(*wc) * (entries + 1));
179         if (!wc) {
180                 kfree(cq);
181                 ret = ERR_PTR(-ENOMEM);
182                 goto bail;
183         }
184         /*
185          * ib_create_cq() will initialize cq->ibcq except for cq->ibcq.cqe.
186          * The number of entries should be >= the number requested or return
187          * an error.
188          */
189         cq->ibcq.cqe = entries;
190         cq->notify = IB_CQ_NONE;
191         cq->triggered = 0;
192         spin_lock_init(&cq->lock);
193         tasklet_init(&cq->comptask, send_complete, (unsigned long)cq);
194         cq->head = 0;
195         cq->tail = 0;
196         cq->queue = wc;
197
198         ret = &cq->ibcq;
199
200 bail:
201         return ret;
202 }
203
204 /**
205  * ipath_destroy_cq - destroy a completion queue
206  * @ibcq: the completion queue to destroy.
207  *
208  * Returns 0 for success.
209  *
210  * Called by ib_destroy_cq() in the generic verbs code.
211  */
212 int ipath_destroy_cq(struct ib_cq *ibcq)
213 {
214         struct ipath_cq *cq = to_icq(ibcq);
215
216         tasklet_kill(&cq->comptask);
217         vfree(cq->queue);
218         kfree(cq);
219
220         return 0;
221 }
222
223 /**
224  * ipath_req_notify_cq - change the notification type for a completion queue
225  * @ibcq: the completion queue
226  * @notify: the type of notification to request
227  *
228  * Returns 0 for success.
229  *
230  * This may be called from interrupt context.  Also called by
231  * ib_req_notify_cq() in the generic verbs code.
232  */
233 int ipath_req_notify_cq(struct ib_cq *ibcq, enum ib_cq_notify notify)
234 {
235         struct ipath_cq *cq = to_icq(ibcq);
236         unsigned long flags;
237
238         spin_lock_irqsave(&cq->lock, flags);
239         /*
240          * Don't change IB_CQ_NEXT_COMP to IB_CQ_SOLICITED but allow
241          * any other transitions.
242          */
243         if (cq->notify != IB_CQ_NEXT_COMP)
244                 cq->notify = notify;
245         spin_unlock_irqrestore(&cq->lock, flags);
246         return 0;
247 }
248
249 int ipath_resize_cq(struct ib_cq *ibcq, int cqe, struct ib_udata *udata)
250 {
251         struct ipath_cq *cq = to_icq(ibcq);
252         struct ib_wc *wc, *old_wc;
253         u32 n;
254         int ret;
255
256         /*
257          * Need to use vmalloc() if we want to support large #s of entries.
258          */
259         wc = vmalloc(sizeof(*wc) * (cqe + 1));
260         if (!wc) {
261                 ret = -ENOMEM;
262                 goto bail;
263         }
264
265         spin_lock_irq(&cq->lock);
266         if (cq->head < cq->tail)
267                 n = cq->ibcq.cqe + 1 + cq->head - cq->tail;
268         else
269                 n = cq->head - cq->tail;
270         if (unlikely((u32)cqe < n)) {
271                 spin_unlock_irq(&cq->lock);
272                 vfree(wc);
273                 ret = -EOVERFLOW;
274                 goto bail;
275         }
276         for (n = 0; cq->tail != cq->head; n++) {
277                 wc[n] = cq->queue[cq->tail];
278                 if (cq->tail == cq->ibcq.cqe)
279                         cq->tail = 0;
280                 else
281                         cq->tail++;
282         }
283         cq->ibcq.cqe = cqe;
284         cq->head = n;
285         cq->tail = 0;
286         old_wc = cq->queue;
287         cq->queue = wc;
288         spin_unlock_irq(&cq->lock);
289
290         vfree(old_wc);
291
292         ret = 0;
293
294 bail:
295         return ret;
296 }