blob: 03acae66ba81a5e0512307f19688c8b18c038b09 [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 (srq_init_attr->attr.max_wr == 0) {
108 ret = ERR_PTR(-EINVAL);
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_sge > ib_ipath_max_srq_sges) ||
113 (srq_init_attr->attr.max_wr > ib_ipath_max_srq_wrs)) {
Bryan O'Sullivancef1cce2006-03-29 15:23:36 -0800114 ret = ERR_PTR(-EINVAL);
Ralph Campbell373d9912006-09-22 15:22:26 -0700115 goto done;
Bryan O'Sullivancef1cce2006-03-29 15:23:36 -0800116 }
117
118 srq = kmalloc(sizeof(*srq), GFP_KERNEL);
119 if (!srq) {
120 ret = ERR_PTR(-ENOMEM);
Ralph Campbell373d9912006-09-22 15:22:26 -0700121 goto done;
Bryan O'Sullivancef1cce2006-03-29 15:23:36 -0800122 }
123
124 /*
125 * Need to use vmalloc() if we want to support large #s of entries.
126 */
127 srq->rq.size = srq_init_attr->attr.max_wr + 1;
Ralph Campbell373d9912006-09-22 15:22:26 -0700128 srq->rq.max_sge = srq_init_attr->attr.max_sge;
129 sz = sizeof(struct ib_sge) * srq->rq.max_sge +
Bryan O'Sullivancef1cce2006-03-29 15:23:36 -0800130 sizeof(struct ipath_rwqe);
Ralph Campbell373d9912006-09-22 15:22:26 -0700131 srq->rq.wq = vmalloc_user(sizeof(struct ipath_rwq) + srq->rq.size * sz);
Bryan O'Sullivancef1cce2006-03-29 15:23:36 -0800132 if (!srq->rq.wq) {
Bryan O'Sullivancef1cce2006-03-29 15:23:36 -0800133 ret = ERR_PTR(-ENOMEM);
Ralph Campbell373d9912006-09-22 15:22:26 -0700134 goto bail_srq;
Bryan O'Sullivancef1cce2006-03-29 15:23:36 -0800135 }
136
137 /*
Ralph Campbell373d9912006-09-22 15:22:26 -0700138 * Return the address of the RWQ as the offset to mmap.
139 * See ipath_mmap() for details.
140 */
141 if (udata && udata->outlen >= sizeof(__u64)) {
Ralph Campbell373d9912006-09-22 15:22:26 -0700142 int err;
Robert Walsh6b66b2d2007-04-27 21:07:23 -0700143 u32 s = sizeof(struct ipath_rwq) + srq->rq.size * sz;
Ralph Campbell373d9912006-09-22 15:22:26 -0700144
Robert Walsh6b66b2d2007-04-27 21:07:23 -0700145 srq->ip =
146 ipath_create_mmap_info(dev, s,
147 ibpd->uobject->context,
148 srq->rq.wq);
149 if (!srq->ip) {
Ralph Campbell373d9912006-09-22 15:22:26 -0700150 ret = ERR_PTR(-ENOMEM);
151 goto bail_wq;
152 }
Robert Walsh6b66b2d2007-04-27 21:07:23 -0700153
154 err = ib_copy_to_udata(udata, &srq->ip->offset,
155 sizeof(srq->ip->offset));
156 if (err) {
157 ret = ERR_PTR(err);
158 goto bail_ip;
159 }
Ralph Campbell373d9912006-09-22 15:22:26 -0700160 } else
161 srq->ip = NULL;
162
163 /*
Bryan O'Sullivancef1cce2006-03-29 15:23:36 -0800164 * ib_create_srq() will initialize srq->ibsrq.
165 */
166 spin_lock_init(&srq->rq.lock);
Ralph Campbell373d9912006-09-22 15:22:26 -0700167 srq->rq.wq->head = 0;
168 srq->rq.wq->tail = 0;
Bryan O'Sullivancef1cce2006-03-29 15:23:36 -0800169 srq->limit = srq_init_attr->attr.srq_limit;
170
Bryan O'Sullivanf3e93c72006-09-28 09:00:04 -0700171 spin_lock(&dev->n_srqs_lock);
172 if (dev->n_srqs_allocated == ib_ipath_max_srqs) {
173 spin_unlock(&dev->n_srqs_lock);
174 ret = ERR_PTR(-ENOMEM);
Robert Walsh6b66b2d2007-04-27 21:07:23 -0700175 goto bail_ip;
Bryan O'Sullivanf3e93c72006-09-28 09:00:04 -0700176 }
177
178 dev->n_srqs_allocated++;
179 spin_unlock(&dev->n_srqs_lock);
Bryan O'Sullivanfe625462006-07-01 04:35:58 -0700180
Robert Walsh6b66b2d2007-04-27 21:07:23 -0700181 if (srq->ip) {
182 spin_lock_irq(&dev->pending_lock);
183 list_add(&srq->ip->pending_mmaps, &dev->pending_mmaps);
184 spin_unlock_irq(&dev->pending_lock);
185 }
186
Ralph Campbell373d9912006-09-22 15:22:26 -0700187 ret = &srq->ibsrq;
188 goto done;
189
Robert Walsh6b66b2d2007-04-27 21:07:23 -0700190bail_ip:
191 kfree(srq->ip);
Ralph Campbell373d9912006-09-22 15:22:26 -0700192bail_wq:
193 vfree(srq->rq.wq);
Ralph Campbell373d9912006-09-22 15:22:26 -0700194bail_srq:
195 kfree(srq);
Ralph Campbell373d9912006-09-22 15:22:26 -0700196done:
Bryan O'Sullivancef1cce2006-03-29 15:23:36 -0800197 return ret;
198}
199
200/**
201 * ipath_modify_srq - modify a shared receive queue
202 * @ibsrq: the SRQ to modify
203 * @attr: the new attributes of the SRQ
204 * @attr_mask: indicates which attributes to modify
Ralph Campbell9bc57e22006-08-11 14:58:09 -0700205 * @udata: user data for ipathverbs.so
Bryan O'Sullivancef1cce2006-03-29 15:23:36 -0800206 */
207int ipath_modify_srq(struct ib_srq *ibsrq, struct ib_srq_attr *attr,
Ralph Campbell9bc57e22006-08-11 14:58:09 -0700208 enum ib_srq_attr_mask attr_mask,
209 struct ib_udata *udata)
Bryan O'Sullivancef1cce2006-03-29 15:23:36 -0800210{
211 struct ipath_srq *srq = to_isrq(ibsrq);
Ralph Campbell373d9912006-09-22 15:22:26 -0700212 int ret = 0;
Bryan O'Sullivanfe625462006-07-01 04:35:58 -0700213
214 if (attr_mask & IB_SRQ_MAX_WR) {
Ralph Campbell373d9912006-09-22 15:22:26 -0700215 struct ipath_rwq *owq;
216 struct ipath_rwq *wq;
217 struct ipath_rwqe *p;
218 u32 sz, size, n, head, tail;
219
220 /* Check that the requested sizes are below the limits. */
221 if ((attr->max_wr > ib_ipath_max_srq_wrs) ||
222 ((attr_mask & IB_SRQ_LIMIT) ?
223 attr->srq_limit : srq->limit) > attr->max_wr) {
224 ret = -EINVAL;
225 goto bail;
226 }
Bryan O'Sullivanfe625462006-07-01 04:35:58 -0700227
Bryan O'Sullivancef1cce2006-03-29 15:23:36 -0800228 sz = sizeof(struct ipath_rwqe) +
Ralph Campbell373d9912006-09-22 15:22:26 -0700229 srq->rq.max_sge * sizeof(struct ib_sge);
Bryan O'Sullivanfe625462006-07-01 04:35:58 -0700230 size = attr->max_wr + 1;
Ralph Campbell373d9912006-09-22 15:22:26 -0700231 wq = vmalloc_user(sizeof(struct ipath_rwq) + size * sz);
Bryan O'Sullivancef1cce2006-03-29 15:23:36 -0800232 if (!wq) {
233 ret = -ENOMEM;
234 goto bail;
235 }
236
Ralph Campbell373d9912006-09-22 15:22:26 -0700237 /*
238 * Return the address of the RWQ as the offset to mmap.
239 * See ipath_mmap() for details.
240 */
241 if (udata && udata->inlen >= sizeof(__u64)) {
242 __u64 offset_addr;
243 __u64 offset = (__u64) wq;
244
245 ret = ib_copy_from_udata(&offset_addr, udata,
246 sizeof(offset_addr));
247 if (ret) {
248 vfree(wq);
249 goto bail;
250 }
251 udata->outbuf = (void __user *) offset_addr;
252 ret = ib_copy_to_udata(udata, &offset,
253 sizeof(offset));
254 if (ret) {
255 vfree(wq);
256 goto bail;
257 }
258 }
259
260 spin_lock_irq(&srq->rq.lock);
261 /*
262 * validate head pointer value and compute
263 * the number of remaining WQEs.
264 */
265 owq = srq->rq.wq;
266 head = owq->head;
267 if (head >= srq->rq.size)
268 head = 0;
269 tail = owq->tail;
270 if (tail >= srq->rq.size)
271 tail = 0;
272 n = head;
273 if (n < tail)
274 n += srq->rq.size - tail;
Bryan O'Sullivancef1cce2006-03-29 15:23:36 -0800275 else
Ralph Campbell373d9912006-09-22 15:22:26 -0700276 n -= tail;
277 if (size <= n) {
278 spin_unlock_irq(&srq->rq.lock);
Bryan O'Sullivancef1cce2006-03-29 15:23:36 -0800279 vfree(wq);
280 ret = -EINVAL;
281 goto bail;
282 }
283 n = 0;
Ralph Campbell373d9912006-09-22 15:22:26 -0700284 p = wq->wq;
285 while (tail != head) {
Bryan O'Sullivancef1cce2006-03-29 15:23:36 -0800286 struct ipath_rwqe *wqe;
287 int i;
288
Ralph Campbell373d9912006-09-22 15:22:26 -0700289 wqe = get_rwqe_ptr(&srq->rq, tail);
Bryan O'Sullivancef1cce2006-03-29 15:23:36 -0800290 p->wr_id = wqe->wr_id;
Bryan O'Sullivancef1cce2006-03-29 15:23:36 -0800291 p->num_sge = wqe->num_sge;
292 for (i = 0; i < wqe->num_sge; i++)
293 p->sg_list[i] = wqe->sg_list[i];
294 n++;
295 p = (struct ipath_rwqe *)((char *) p + sz);
Ralph Campbell373d9912006-09-22 15:22:26 -0700296 if (++tail >= srq->rq.size)
297 tail = 0;
Bryan O'Sullivancef1cce2006-03-29 15:23:36 -0800298 }
Bryan O'Sullivancef1cce2006-03-29 15:23:36 -0800299 srq->rq.wq = wq;
300 srq->rq.size = size;
Ralph Campbell373d9912006-09-22 15:22:26 -0700301 wq->head = n;
302 wq->tail = 0;
303 if (attr_mask & IB_SRQ_LIMIT)
304 srq->limit = attr->srq_limit;
305 spin_unlock_irq(&srq->rq.lock);
Bryan O'Sullivancef1cce2006-03-29 15:23:36 -0800306
Ralph Campbell373d9912006-09-22 15:22:26 -0700307 vfree(owq);
308
309 if (srq->ip) {
310 struct ipath_mmap_info *ip = srq->ip;
311 struct ipath_ibdev *dev = to_idev(srq->ibsrq.device);
Robert Walsh6b66b2d2007-04-27 21:07:23 -0700312 u32 s = sizeof(struct ipath_rwq) + size * sz;
Ralph Campbell373d9912006-09-22 15:22:26 -0700313
Robert Walsh6b66b2d2007-04-27 21:07:23 -0700314 ipath_update_mmap_info(dev, ip, s, wq);
Ralph Campbell373d9912006-09-22 15:22:26 -0700315 spin_lock_irq(&dev->pending_lock);
Robert Walsh6b66b2d2007-04-27 21:07:23 -0700316 if (list_empty(&ip->pending_mmaps))
317 list_add(&ip->pending_mmaps,
318 &dev->pending_mmaps);
Ralph Campbell373d9912006-09-22 15:22:26 -0700319 spin_unlock_irq(&dev->pending_lock);
320 }
321 } else if (attr_mask & IB_SRQ_LIMIT) {
322 spin_lock_irq(&srq->rq.lock);
323 if (attr->srq_limit >= srq->rq.size)
324 ret = -EINVAL;
325 else
326 srq->limit = attr->srq_limit;
327 spin_unlock_irq(&srq->rq.lock);
Bryan O'Sullivanfe625462006-07-01 04:35:58 -0700328 }
Bryan O'Sullivancef1cce2006-03-29 15:23:36 -0800329
330bail:
331 return ret;
332}
333
334int ipath_query_srq(struct ib_srq *ibsrq, struct ib_srq_attr *attr)
335{
336 struct ipath_srq *srq = to_isrq(ibsrq);
337
338 attr->max_wr = srq->rq.size - 1;
339 attr->max_sge = srq->rq.max_sge;
340 attr->srq_limit = srq->limit;
341 return 0;
342}
343
344/**
345 * ipath_destroy_srq - destroy a shared receive queue
346 * @ibsrq: the SRQ to destroy
347 */
348int ipath_destroy_srq(struct ib_srq *ibsrq)
349{
350 struct ipath_srq *srq = to_isrq(ibsrq);
Bryan O'Sullivanfe625462006-07-01 04:35:58 -0700351 struct ipath_ibdev *dev = to_idev(ibsrq->device);
Bryan O'Sullivancef1cce2006-03-29 15:23:36 -0800352
Bryan O'Sullivanf3e93c72006-09-28 09:00:04 -0700353 spin_lock(&dev->n_srqs_lock);
Bryan O'Sullivanfe625462006-07-01 04:35:58 -0700354 dev->n_srqs_allocated--;
Bryan O'Sullivanf3e93c72006-09-28 09:00:04 -0700355 spin_unlock(&dev->n_srqs_lock);
356 if (srq->ip)
357 kref_put(&srq->ip->ref, ipath_release_mmap_info);
358 else
359 vfree(srq->rq.wq);
Bryan O'Sullivancef1cce2006-03-29 15:23:36 -0800360 kfree(srq);
361
362 return 0;
363}