blob: 2fef36f4b675a8d0022bb1819bc4d560d977a0e2 [file] [log] [blame]
Bryan O'Sullivancef1cce2006-03-29 15:23:36 -08001/*
John Gregor87427da2007-06-11 10:21:14 -07002 * Copyright (c) 2006, 2007 QLogic Corporation. 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;
Ralph Campbell4187b912007-11-14 13:34:14 -080062 ret = -EINVAL;
Bryan O'Sullivancef1cce2006-03-29 15:23:36 -080063 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];
Ralph Campbell4fc570b2007-07-06 12:48:23 -070083 /* Make sure queue entry is written before the head index. */
84 smp_wmb();
Ralph Campbell373d9912006-09-22 15:22:26 -070085 wq->head = next;
Bryan O'Sullivancef1cce2006-03-29 15:23:36 -080086 spin_unlock_irqrestore(&srq->rq.lock, flags);
87 }
88 ret = 0;
89
90bail:
91 return ret;
92}
93
94/**
95 * ipath_create_srq - create a shared receive queue
96 * @ibpd: the protection domain of the SRQ to create
97 * @attr: the attributes of the SRQ
98 * @udata: not used by the InfiniPath verbs driver
99 */
100struct ib_srq *ipath_create_srq(struct ib_pd *ibpd,
101 struct ib_srq_init_attr *srq_init_attr,
102 struct ib_udata *udata)
103{
Bryan O'Sullivanfe625462006-07-01 04:35:58 -0700104 struct ipath_ibdev *dev = to_idev(ibpd->device);
Bryan O'Sullivancef1cce2006-03-29 15:23:36 -0800105 struct ipath_srq *srq;
106 u32 sz;
107 struct ib_srq *ret;
108
Bryan O'Sullivanfe625462006-07-01 04:35:58 -0700109 if (srq_init_attr->attr.max_wr == 0) {
110 ret = ERR_PTR(-EINVAL);
Ralph Campbell373d9912006-09-22 15:22:26 -0700111 goto done;
Bryan O'Sullivanfe625462006-07-01 04:35:58 -0700112 }
113
114 if ((srq_init_attr->attr.max_sge > ib_ipath_max_srq_sges) ||
115 (srq_init_attr->attr.max_wr > ib_ipath_max_srq_wrs)) {
Bryan O'Sullivancef1cce2006-03-29 15:23:36 -0800116 ret = ERR_PTR(-EINVAL);
Ralph Campbell373d9912006-09-22 15:22:26 -0700117 goto done;
Bryan O'Sullivancef1cce2006-03-29 15:23:36 -0800118 }
119
120 srq = kmalloc(sizeof(*srq), GFP_KERNEL);
121 if (!srq) {
122 ret = ERR_PTR(-ENOMEM);
Ralph Campbell373d9912006-09-22 15:22:26 -0700123 goto done;
Bryan O'Sullivancef1cce2006-03-29 15:23:36 -0800124 }
125
126 /*
127 * Need to use vmalloc() if we want to support large #s of entries.
128 */
129 srq->rq.size = srq_init_attr->attr.max_wr + 1;
Ralph Campbell373d9912006-09-22 15:22:26 -0700130 srq->rq.max_sge = srq_init_attr->attr.max_sge;
131 sz = sizeof(struct ib_sge) * srq->rq.max_sge +
Bryan O'Sullivancef1cce2006-03-29 15:23:36 -0800132 sizeof(struct ipath_rwqe);
Ralph Campbell373d9912006-09-22 15:22:26 -0700133 srq->rq.wq = vmalloc_user(sizeof(struct ipath_rwq) + srq->rq.size * sz);
Bryan O'Sullivancef1cce2006-03-29 15:23:36 -0800134 if (!srq->rq.wq) {
Bryan O'Sullivancef1cce2006-03-29 15:23:36 -0800135 ret = ERR_PTR(-ENOMEM);
Ralph Campbell373d9912006-09-22 15:22:26 -0700136 goto bail_srq;
Bryan O'Sullivancef1cce2006-03-29 15:23:36 -0800137 }
138
139 /*
Ralph Campbell373d9912006-09-22 15:22:26 -0700140 * Return the address of the RWQ as the offset to mmap.
141 * See ipath_mmap() for details.
142 */
143 if (udata && udata->outlen >= sizeof(__u64)) {
Ralph Campbell373d9912006-09-22 15:22:26 -0700144 int err;
Robert Walsh6b66b2d2007-04-27 21:07:23 -0700145 u32 s = sizeof(struct ipath_rwq) + srq->rq.size * sz;
Ralph Campbell373d9912006-09-22 15:22:26 -0700146
Robert Walsh6b66b2d2007-04-27 21:07:23 -0700147 srq->ip =
148 ipath_create_mmap_info(dev, s,
149 ibpd->uobject->context,
150 srq->rq.wq);
151 if (!srq->ip) {
Ralph Campbell373d9912006-09-22 15:22:26 -0700152 ret = ERR_PTR(-ENOMEM);
153 goto bail_wq;
154 }
Robert Walsh6b66b2d2007-04-27 21:07:23 -0700155
156 err = ib_copy_to_udata(udata, &srq->ip->offset,
157 sizeof(srq->ip->offset));
158 if (err) {
159 ret = ERR_PTR(err);
160 goto bail_ip;
161 }
Ralph Campbell373d9912006-09-22 15:22:26 -0700162 } else
163 srq->ip = NULL;
164
165 /*
Bryan O'Sullivancef1cce2006-03-29 15:23:36 -0800166 * ib_create_srq() will initialize srq->ibsrq.
167 */
168 spin_lock_init(&srq->rq.lock);
Ralph Campbell373d9912006-09-22 15:22:26 -0700169 srq->rq.wq->head = 0;
170 srq->rq.wq->tail = 0;
Bryan O'Sullivancef1cce2006-03-29 15:23:36 -0800171 srq->limit = srq_init_attr->attr.srq_limit;
172
Bryan O'Sullivanf3e93c72006-09-28 09:00:04 -0700173 spin_lock(&dev->n_srqs_lock);
174 if (dev->n_srqs_allocated == ib_ipath_max_srqs) {
175 spin_unlock(&dev->n_srqs_lock);
176 ret = ERR_PTR(-ENOMEM);
Robert Walsh6b66b2d2007-04-27 21:07:23 -0700177 goto bail_ip;
Bryan O'Sullivanf3e93c72006-09-28 09:00:04 -0700178 }
179
180 dev->n_srqs_allocated++;
181 spin_unlock(&dev->n_srqs_lock);
Bryan O'Sullivanfe625462006-07-01 04:35:58 -0700182
Robert Walsh6b66b2d2007-04-27 21:07:23 -0700183 if (srq->ip) {
184 spin_lock_irq(&dev->pending_lock);
185 list_add(&srq->ip->pending_mmaps, &dev->pending_mmaps);
186 spin_unlock_irq(&dev->pending_lock);
187 }
188
Ralph Campbell373d9912006-09-22 15:22:26 -0700189 ret = &srq->ibsrq;
190 goto done;
191
Robert Walsh6b66b2d2007-04-27 21:07:23 -0700192bail_ip:
193 kfree(srq->ip);
Ralph Campbell373d9912006-09-22 15:22:26 -0700194bail_wq:
195 vfree(srq->rq.wq);
Ralph Campbell373d9912006-09-22 15:22:26 -0700196bail_srq:
197 kfree(srq);
Ralph Campbell373d9912006-09-22 15:22:26 -0700198done:
Bryan O'Sullivancef1cce2006-03-29 15:23:36 -0800199 return ret;
200}
201
202/**
203 * ipath_modify_srq - modify a shared receive queue
204 * @ibsrq: the SRQ to modify
205 * @attr: the new attributes of the SRQ
206 * @attr_mask: indicates which attributes to modify
Ralph Campbell9bc57e22006-08-11 14:58:09 -0700207 * @udata: user data for ipathverbs.so
Bryan O'Sullivancef1cce2006-03-29 15:23:36 -0800208 */
209int ipath_modify_srq(struct ib_srq *ibsrq, struct ib_srq_attr *attr,
Ralph Campbell9bc57e22006-08-11 14:58:09 -0700210 enum ib_srq_attr_mask attr_mask,
211 struct ib_udata *udata)
Bryan O'Sullivancef1cce2006-03-29 15:23:36 -0800212{
213 struct ipath_srq *srq = to_isrq(ibsrq);
Ralph Campbell14de9862007-11-09 15:22:31 -0800214 struct ipath_rwq *wq;
Ralph Campbell373d9912006-09-22 15:22:26 -0700215 int ret = 0;
Bryan O'Sullivanfe625462006-07-01 04:35:58 -0700216
217 if (attr_mask & IB_SRQ_MAX_WR) {
Ralph Campbell373d9912006-09-22 15:22:26 -0700218 struct ipath_rwq *owq;
Ralph Campbell373d9912006-09-22 15:22:26 -0700219 struct ipath_rwqe *p;
220 u32 sz, size, n, head, tail;
221
222 /* Check that the requested sizes are below the limits. */
223 if ((attr->max_wr > ib_ipath_max_srq_wrs) ||
224 ((attr_mask & IB_SRQ_LIMIT) ?
225 attr->srq_limit : srq->limit) > attr->max_wr) {
226 ret = -EINVAL;
227 goto bail;
228 }
Bryan O'Sullivanfe625462006-07-01 04:35:58 -0700229
Bryan O'Sullivancef1cce2006-03-29 15:23:36 -0800230 sz = sizeof(struct ipath_rwqe) +
Ralph Campbell373d9912006-09-22 15:22:26 -0700231 srq->rq.max_sge * sizeof(struct ib_sge);
Bryan O'Sullivanfe625462006-07-01 04:35:58 -0700232 size = attr->max_wr + 1;
Ralph Campbell373d9912006-09-22 15:22:26 -0700233 wq = vmalloc_user(sizeof(struct ipath_rwq) + size * sz);
Bryan O'Sullivancef1cce2006-03-29 15:23:36 -0800234 if (!wq) {
235 ret = -ENOMEM;
236 goto bail;
237 }
238
Ralph Campbell14de9862007-11-09 15:22:31 -0800239 /* Check that we can write the offset to mmap. */
Ralph Campbell373d9912006-09-22 15:22:26 -0700240 if (udata && udata->inlen >= sizeof(__u64)) {
241 __u64 offset_addr;
Ralph Campbell14de9862007-11-09 15:22:31 -0800242 __u64 offset = 0;
Ralph Campbell373d9912006-09-22 15:22:26 -0700243
244 ret = ib_copy_from_udata(&offset_addr, udata,
245 sizeof(offset_addr));
Ralph Campbell14de9862007-11-09 15:22:31 -0800246 if (ret)
247 goto bail_free;
Ralph Campbell373d9912006-09-22 15:22:26 -0700248 udata->outbuf = (void __user *) offset_addr;
249 ret = ib_copy_to_udata(udata, &offset,
250 sizeof(offset));
Ralph Campbell14de9862007-11-09 15:22:31 -0800251 if (ret)
252 goto bail_free;
Ralph Campbell373d9912006-09-22 15:22:26 -0700253 }
254
255 spin_lock_irq(&srq->rq.lock);
256 /*
257 * validate head pointer value and compute
258 * the number of remaining WQEs.
259 */
260 owq = srq->rq.wq;
261 head = owq->head;
262 if (head >= srq->rq.size)
263 head = 0;
264 tail = owq->tail;
265 if (tail >= srq->rq.size)
266 tail = 0;
267 n = head;
268 if (n < tail)
269 n += srq->rq.size - tail;
Bryan O'Sullivancef1cce2006-03-29 15:23:36 -0800270 else
Ralph Campbell373d9912006-09-22 15:22:26 -0700271 n -= tail;
272 if (size <= n) {
Bryan O'Sullivancef1cce2006-03-29 15:23:36 -0800273 ret = -EINVAL;
Ralph Campbell14de9862007-11-09 15:22:31 -0800274 goto bail_unlock;
Bryan O'Sullivancef1cce2006-03-29 15:23:36 -0800275 }
276 n = 0;
Ralph Campbell373d9912006-09-22 15:22:26 -0700277 p = wq->wq;
278 while (tail != head) {
Bryan O'Sullivancef1cce2006-03-29 15:23:36 -0800279 struct ipath_rwqe *wqe;
280 int i;
281
Ralph Campbell373d9912006-09-22 15:22:26 -0700282 wqe = get_rwqe_ptr(&srq->rq, tail);
Bryan O'Sullivancef1cce2006-03-29 15:23:36 -0800283 p->wr_id = wqe->wr_id;
Bryan O'Sullivancef1cce2006-03-29 15:23:36 -0800284 p->num_sge = wqe->num_sge;
285 for (i = 0; i < wqe->num_sge; i++)
286 p->sg_list[i] = wqe->sg_list[i];
287 n++;
288 p = (struct ipath_rwqe *)((char *) p + sz);
Ralph Campbell373d9912006-09-22 15:22:26 -0700289 if (++tail >= srq->rq.size)
290 tail = 0;
Bryan O'Sullivancef1cce2006-03-29 15:23:36 -0800291 }
Bryan O'Sullivancef1cce2006-03-29 15:23:36 -0800292 srq->rq.wq = wq;
293 srq->rq.size = size;
Ralph Campbell373d9912006-09-22 15:22:26 -0700294 wq->head = n;
295 wq->tail = 0;
296 if (attr_mask & IB_SRQ_LIMIT)
297 srq->limit = attr->srq_limit;
298 spin_unlock_irq(&srq->rq.lock);
Bryan O'Sullivancef1cce2006-03-29 15:23:36 -0800299
Ralph Campbell373d9912006-09-22 15:22:26 -0700300 vfree(owq);
301
302 if (srq->ip) {
303 struct ipath_mmap_info *ip = srq->ip;
304 struct ipath_ibdev *dev = to_idev(srq->ibsrq.device);
Robert Walsh6b66b2d2007-04-27 21:07:23 -0700305 u32 s = sizeof(struct ipath_rwq) + size * sz;
Ralph Campbell373d9912006-09-22 15:22:26 -0700306
Robert Walsh6b66b2d2007-04-27 21:07:23 -0700307 ipath_update_mmap_info(dev, ip, s, wq);
Ralph Campbell14de9862007-11-09 15:22:31 -0800308
309 /*
310 * Return the offset to mmap.
311 * See ipath_mmap() for details.
312 */
313 if (udata && udata->inlen >= sizeof(__u64)) {
314 ret = ib_copy_to_udata(udata, &ip->offset,
315 sizeof(ip->offset));
316 if (ret)
317 goto bail;
318 }
319
Ralph Campbell373d9912006-09-22 15:22:26 -0700320 spin_lock_irq(&dev->pending_lock);
Robert Walsh6b66b2d2007-04-27 21:07:23 -0700321 if (list_empty(&ip->pending_mmaps))
322 list_add(&ip->pending_mmaps,
323 &dev->pending_mmaps);
Ralph Campbell373d9912006-09-22 15:22:26 -0700324 spin_unlock_irq(&dev->pending_lock);
325 }
326 } else if (attr_mask & IB_SRQ_LIMIT) {
327 spin_lock_irq(&srq->rq.lock);
328 if (attr->srq_limit >= srq->rq.size)
329 ret = -EINVAL;
330 else
331 srq->limit = attr->srq_limit;
332 spin_unlock_irq(&srq->rq.lock);
Bryan O'Sullivanfe625462006-07-01 04:35:58 -0700333 }
Ralph Campbell14de9862007-11-09 15:22:31 -0800334 goto bail;
Bryan O'Sullivancef1cce2006-03-29 15:23:36 -0800335
Ralph Campbell14de9862007-11-09 15:22:31 -0800336bail_unlock:
337 spin_unlock_irq(&srq->rq.lock);
338bail_free:
339 vfree(wq);
Bryan O'Sullivancef1cce2006-03-29 15:23:36 -0800340bail:
341 return ret;
342}
343
344int ipath_query_srq(struct ib_srq *ibsrq, struct ib_srq_attr *attr)
345{
346 struct ipath_srq *srq = to_isrq(ibsrq);
347
348 attr->max_wr = srq->rq.size - 1;
349 attr->max_sge = srq->rq.max_sge;
350 attr->srq_limit = srq->limit;
351 return 0;
352}
353
354/**
355 * ipath_destroy_srq - destroy a shared receive queue
356 * @ibsrq: the SRQ to destroy
357 */
358int ipath_destroy_srq(struct ib_srq *ibsrq)
359{
360 struct ipath_srq *srq = to_isrq(ibsrq);
Bryan O'Sullivanfe625462006-07-01 04:35:58 -0700361 struct ipath_ibdev *dev = to_idev(ibsrq->device);
Bryan O'Sullivancef1cce2006-03-29 15:23:36 -0800362
Bryan O'Sullivanf3e93c72006-09-28 09:00:04 -0700363 spin_lock(&dev->n_srqs_lock);
Bryan O'Sullivanfe625462006-07-01 04:35:58 -0700364 dev->n_srqs_allocated--;
Bryan O'Sullivanf3e93c72006-09-28 09:00:04 -0700365 spin_unlock(&dev->n_srqs_lock);
366 if (srq->ip)
367 kref_put(&srq->ip->ref, ipath_release_mmap_info);
368 else
369 vfree(srq->rq.wq);
Bryan O'Sullivancef1cce2006-03-29 15:23:36 -0800370 kfree(srq);
371
372 return 0;
373}