blob: 941e866d9517ba84edb6078db94565f5090f2429 [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_post_srq_receive - post a receive on a shared receive queue
41 * @ibsrq: the SRQ to post the receive on
42 * @wr: the list of work requests to post
43 * @bad_wr: the first WR to cause a problem is put here
44 *
45 * This may be called from interrupt context.
46 */
47int ipath_post_srq_receive(struct ib_srq *ibsrq, struct ib_recv_wr *wr,
48 struct ib_recv_wr **bad_wr)
49{
50 struct ipath_srq *srq = to_isrq(ibsrq);
Ralph Campbell373d9912006-09-22 15:22:26 -070051 struct ipath_rwq *wq;
Bryan O'Sullivancef1cce2006-03-29 15:23:36 -080052 unsigned long flags;
53 int ret;
54
55 for (; wr; wr = wr->next) {
56 struct ipath_rwqe *wqe;
57 u32 next;
Ralph Campbell373d9912006-09-22 15:22:26 -070058 int i;
Bryan O'Sullivancef1cce2006-03-29 15:23:36 -080059
Ralph Campbell373d9912006-09-22 15:22:26 -070060 if ((unsigned) wr->num_sge > srq->rq.max_sge) {
Bryan O'Sullivancef1cce2006-03-29 15:23:36 -080061 *bad_wr = wr;
62 ret = -ENOMEM;
63 goto bail;
64 }
65
66 spin_lock_irqsave(&srq->rq.lock, flags);
Ralph Campbell373d9912006-09-22 15:22:26 -070067 wq = srq->rq.wq;
68 next = wq->head + 1;
Bryan O'Sullivancef1cce2006-03-29 15:23:36 -080069 if (next >= srq->rq.size)
70 next = 0;
Ralph Campbell373d9912006-09-22 15:22:26 -070071 if (next == wq->tail) {
Bryan O'Sullivancef1cce2006-03-29 15:23:36 -080072 spin_unlock_irqrestore(&srq->rq.lock, flags);
73 *bad_wr = wr;
74 ret = -ENOMEM;
75 goto bail;
76 }
77
Ralph Campbell373d9912006-09-22 15:22:26 -070078 wqe = get_rwqe_ptr(&srq->rq, wq->head);
Bryan O'Sullivancef1cce2006-03-29 15:23:36 -080079 wqe->wr_id = wr->wr_id;
Ralph Campbell373d9912006-09-22 15:22:26 -070080 wqe->num_sge = wr->num_sge;
81 for (i = 0; i < wr->num_sge; i++)
82 wqe->sg_list[i] = wr->sg_list[i];
83 wq->head = next;
Bryan O'Sullivancef1cce2006-03-29 15:23:36 -080084 spin_unlock_irqrestore(&srq->rq.lock, flags);
85 }
86 ret = 0;
87
88bail:
89 return ret;
90}
91
92/**
93 * ipath_create_srq - create a shared receive queue
94 * @ibpd: the protection domain of the SRQ to create
95 * @attr: the attributes of the SRQ
96 * @udata: not used by the InfiniPath verbs driver
97 */
98struct ib_srq *ipath_create_srq(struct ib_pd *ibpd,
99 struct ib_srq_init_attr *srq_init_attr,
100 struct ib_udata *udata)
101{
Bryan O'Sullivanfe625462006-07-01 04:35:58 -0700102 struct ipath_ibdev *dev = to_idev(ibpd->device);
Bryan O'Sullivancef1cce2006-03-29 15:23:36 -0800103 struct ipath_srq *srq;
104 u32 sz;
105 struct ib_srq *ret;
106
Bryan O'Sullivanfe625462006-07-01 04:35:58 -0700107 if (dev->n_srqs_allocated == ib_ipath_max_srqs) {
108 ret = ERR_PTR(-ENOMEM);
Ralph Campbell373d9912006-09-22 15:22:26 -0700109 goto done;
Bryan O'Sullivanfe625462006-07-01 04:35:58 -0700110 }
111
112 if (srq_init_attr->attr.max_wr == 0) {
113 ret = ERR_PTR(-EINVAL);
Ralph Campbell373d9912006-09-22 15:22:26 -0700114 goto done;
Bryan O'Sullivanfe625462006-07-01 04:35:58 -0700115 }
116
117 if ((srq_init_attr->attr.max_sge > ib_ipath_max_srq_sges) ||
118 (srq_init_attr->attr.max_wr > ib_ipath_max_srq_wrs)) {
Bryan O'Sullivancef1cce2006-03-29 15:23:36 -0800119 ret = ERR_PTR(-EINVAL);
Ralph Campbell373d9912006-09-22 15:22:26 -0700120 goto done;
Bryan O'Sullivancef1cce2006-03-29 15:23:36 -0800121 }
122
123 srq = kmalloc(sizeof(*srq), GFP_KERNEL);
124 if (!srq) {
125 ret = ERR_PTR(-ENOMEM);
Ralph Campbell373d9912006-09-22 15:22:26 -0700126 goto done;
Bryan O'Sullivancef1cce2006-03-29 15:23:36 -0800127 }
128
129 /*
130 * Need to use vmalloc() if we want to support large #s of entries.
131 */
132 srq->rq.size = srq_init_attr->attr.max_wr + 1;
Ralph Campbell373d9912006-09-22 15:22:26 -0700133 srq->rq.max_sge = srq_init_attr->attr.max_sge;
134 sz = sizeof(struct ib_sge) * srq->rq.max_sge +
Bryan O'Sullivancef1cce2006-03-29 15:23:36 -0800135 sizeof(struct ipath_rwqe);
Ralph Campbell373d9912006-09-22 15:22:26 -0700136 srq->rq.wq = vmalloc_user(sizeof(struct ipath_rwq) + srq->rq.size * sz);
Bryan O'Sullivancef1cce2006-03-29 15:23:36 -0800137 if (!srq->rq.wq) {
Bryan O'Sullivancef1cce2006-03-29 15:23:36 -0800138 ret = ERR_PTR(-ENOMEM);
Ralph Campbell373d9912006-09-22 15:22:26 -0700139 goto bail_srq;
Bryan O'Sullivancef1cce2006-03-29 15:23:36 -0800140 }
141
142 /*
Ralph Campbell373d9912006-09-22 15:22:26 -0700143 * Return the address of the RWQ as the offset to mmap.
144 * See ipath_mmap() for details.
145 */
146 if (udata && udata->outlen >= sizeof(__u64)) {
147 struct ipath_mmap_info *ip;
148 __u64 offset = (__u64) srq->rq.wq;
149 int err;
150
151 err = ib_copy_to_udata(udata, &offset, sizeof(offset));
152 if (err) {
153 ret = ERR_PTR(err);
154 goto bail_wq;
155 }
156
157 /* Allocate info for ipath_mmap(). */
158 ip = kmalloc(sizeof(*ip), GFP_KERNEL);
159 if (!ip) {
160 ret = ERR_PTR(-ENOMEM);
161 goto bail_wq;
162 }
163 srq->ip = ip;
164 ip->context = ibpd->uobject->context;
165 ip->obj = srq->rq.wq;
166 kref_init(&ip->ref);
167 ip->mmap_cnt = 0;
168 ip->size = PAGE_ALIGN(sizeof(struct ipath_rwq) +
169 srq->rq.size * sz);
170 spin_lock_irq(&dev->pending_lock);
171 ip->next = dev->pending_mmaps;
172 dev->pending_mmaps = ip;
173 spin_unlock_irq(&dev->pending_lock);
174 } else
175 srq->ip = NULL;
176
177 /*
Bryan O'Sullivancef1cce2006-03-29 15:23:36 -0800178 * ib_create_srq() will initialize srq->ibsrq.
179 */
180 spin_lock_init(&srq->rq.lock);
Ralph Campbell373d9912006-09-22 15:22:26 -0700181 srq->rq.wq->head = 0;
182 srq->rq.wq->tail = 0;
Bryan O'Sullivancef1cce2006-03-29 15:23:36 -0800183 srq->rq.max_sge = srq_init_attr->attr.max_sge;
184 srq->limit = srq_init_attr->attr.srq_limit;
185
Bryan O'Sullivanfe625462006-07-01 04:35:58 -0700186 dev->n_srqs_allocated++;
187
Ralph Campbell373d9912006-09-22 15:22:26 -0700188 ret = &srq->ibsrq;
189 goto done;
190
191bail_wq:
192 vfree(srq->rq.wq);
193
194bail_srq:
195 kfree(srq);
196
197done:
Bryan O'Sullivancef1cce2006-03-29 15:23:36 -0800198 return ret;
199}
200
201/**
202 * ipath_modify_srq - modify a shared receive queue
203 * @ibsrq: the SRQ to modify
204 * @attr: the new attributes of the SRQ
205 * @attr_mask: indicates which attributes to modify
Ralph Campbell9bc57e22006-08-11 14:58:09 -0700206 * @udata: user data for ipathverbs.so
Bryan O'Sullivancef1cce2006-03-29 15:23:36 -0800207 */
208int ipath_modify_srq(struct ib_srq *ibsrq, struct ib_srq_attr *attr,
Ralph Campbell9bc57e22006-08-11 14:58:09 -0700209 enum ib_srq_attr_mask attr_mask,
210 struct ib_udata *udata)
Bryan O'Sullivancef1cce2006-03-29 15:23:36 -0800211{
212 struct ipath_srq *srq = to_isrq(ibsrq);
Ralph Campbell373d9912006-09-22 15:22:26 -0700213 int ret = 0;
Bryan O'Sullivanfe625462006-07-01 04:35:58 -0700214
215 if (attr_mask & IB_SRQ_MAX_WR) {
Ralph Campbell373d9912006-09-22 15:22:26 -0700216 struct ipath_rwq *owq;
217 struct ipath_rwq *wq;
218 struct ipath_rwqe *p;
219 u32 sz, size, n, head, tail;
220
221 /* Check that the requested sizes are below the limits. */
222 if ((attr->max_wr > ib_ipath_max_srq_wrs) ||
223 ((attr_mask & IB_SRQ_LIMIT) ?
224 attr->srq_limit : srq->limit) > attr->max_wr) {
225 ret = -EINVAL;
226 goto bail;
227 }
Bryan O'Sullivanfe625462006-07-01 04:35:58 -0700228
Bryan O'Sullivancef1cce2006-03-29 15:23:36 -0800229 sz = sizeof(struct ipath_rwqe) +
Ralph Campbell373d9912006-09-22 15:22:26 -0700230 srq->rq.max_sge * sizeof(struct ib_sge);
Bryan O'Sullivanfe625462006-07-01 04:35:58 -0700231 size = attr->max_wr + 1;
Ralph Campbell373d9912006-09-22 15:22:26 -0700232 wq = vmalloc_user(sizeof(struct ipath_rwq) + size * sz);
Bryan O'Sullivancef1cce2006-03-29 15:23:36 -0800233 if (!wq) {
234 ret = -ENOMEM;
235 goto bail;
236 }
237
Ralph Campbell373d9912006-09-22 15:22:26 -0700238 /*
239 * Return the address of the RWQ as the offset to mmap.
240 * See ipath_mmap() for details.
241 */
242 if (udata && udata->inlen >= sizeof(__u64)) {
243 __u64 offset_addr;
244 __u64 offset = (__u64) wq;
245
246 ret = ib_copy_from_udata(&offset_addr, udata,
247 sizeof(offset_addr));
248 if (ret) {
249 vfree(wq);
250 goto bail;
251 }
252 udata->outbuf = (void __user *) offset_addr;
253 ret = ib_copy_to_udata(udata, &offset,
254 sizeof(offset));
255 if (ret) {
256 vfree(wq);
257 goto bail;
258 }
259 }
260
261 spin_lock_irq(&srq->rq.lock);
262 /*
263 * validate head pointer value and compute
264 * the number of remaining WQEs.
265 */
266 owq = srq->rq.wq;
267 head = owq->head;
268 if (head >= srq->rq.size)
269 head = 0;
270 tail = owq->tail;
271 if (tail >= srq->rq.size)
272 tail = 0;
273 n = head;
274 if (n < tail)
275 n += srq->rq.size - tail;
Bryan O'Sullivancef1cce2006-03-29 15:23:36 -0800276 else
Ralph Campbell373d9912006-09-22 15:22:26 -0700277 n -= tail;
278 if (size <= n) {
279 spin_unlock_irq(&srq->rq.lock);
Bryan O'Sullivancef1cce2006-03-29 15:23:36 -0800280 vfree(wq);
281 ret = -EINVAL;
282 goto bail;
283 }
284 n = 0;
Ralph Campbell373d9912006-09-22 15:22:26 -0700285 p = wq->wq;
286 while (tail != head) {
Bryan O'Sullivancef1cce2006-03-29 15:23:36 -0800287 struct ipath_rwqe *wqe;
288 int i;
289
Ralph Campbell373d9912006-09-22 15:22:26 -0700290 wqe = get_rwqe_ptr(&srq->rq, tail);
Bryan O'Sullivancef1cce2006-03-29 15:23:36 -0800291 p->wr_id = wqe->wr_id;
Bryan O'Sullivancef1cce2006-03-29 15:23:36 -0800292 p->num_sge = wqe->num_sge;
293 for (i = 0; i < wqe->num_sge; i++)
294 p->sg_list[i] = wqe->sg_list[i];
295 n++;
296 p = (struct ipath_rwqe *)((char *) p + sz);
Ralph Campbell373d9912006-09-22 15:22:26 -0700297 if (++tail >= srq->rq.size)
298 tail = 0;
Bryan O'Sullivancef1cce2006-03-29 15:23:36 -0800299 }
Bryan O'Sullivancef1cce2006-03-29 15:23:36 -0800300 srq->rq.wq = wq;
301 srq->rq.size = size;
Ralph Campbell373d9912006-09-22 15:22:26 -0700302 wq->head = n;
303 wq->tail = 0;
304 if (attr_mask & IB_SRQ_LIMIT)
305 srq->limit = attr->srq_limit;
306 spin_unlock_irq(&srq->rq.lock);
Bryan O'Sullivancef1cce2006-03-29 15:23:36 -0800307
Ralph Campbell373d9912006-09-22 15:22:26 -0700308 vfree(owq);
309
310 if (srq->ip) {
311 struct ipath_mmap_info *ip = srq->ip;
312 struct ipath_ibdev *dev = to_idev(srq->ibsrq.device);
313
314 ip->obj = wq;
315 ip->size = PAGE_ALIGN(sizeof(struct ipath_rwq) +
316 size * sz);
317 spin_lock_irq(&dev->pending_lock);
318 ip->next = dev->pending_mmaps;
319 dev->pending_mmaps = ip;
320 spin_unlock_irq(&dev->pending_lock);
321 }
322 } else if (attr_mask & IB_SRQ_LIMIT) {
323 spin_lock_irq(&srq->rq.lock);
324 if (attr->srq_limit >= srq->rq.size)
325 ret = -EINVAL;
326 else
327 srq->limit = attr->srq_limit;
328 spin_unlock_irq(&srq->rq.lock);
Bryan O'Sullivanfe625462006-07-01 04:35:58 -0700329 }
Bryan O'Sullivancef1cce2006-03-29 15:23:36 -0800330
331bail:
332 return ret;
333}
334
335int ipath_query_srq(struct ib_srq *ibsrq, struct ib_srq_attr *attr)
336{
337 struct ipath_srq *srq = to_isrq(ibsrq);
338
339 attr->max_wr = srq->rq.size - 1;
340 attr->max_sge = srq->rq.max_sge;
341 attr->srq_limit = srq->limit;
342 return 0;
343}
344
345/**
346 * ipath_destroy_srq - destroy a shared receive queue
347 * @ibsrq: the SRQ to destroy
348 */
349int ipath_destroy_srq(struct ib_srq *ibsrq)
350{
351 struct ipath_srq *srq = to_isrq(ibsrq);
Bryan O'Sullivanfe625462006-07-01 04:35:58 -0700352 struct ipath_ibdev *dev = to_idev(ibsrq->device);
Bryan O'Sullivancef1cce2006-03-29 15:23:36 -0800353
Bryan O'Sullivanfe625462006-07-01 04:35:58 -0700354 dev->n_srqs_allocated--;
Bryan O'Sullivancef1cce2006-03-29 15:23:36 -0800355 vfree(srq->rq.wq);
356 kfree(srq);
357
358 return 0;
359}