2 * Copyright (c) 2006 QLogic, Inc. All rights reserved.
3 * Copyright (c) 2005, 2006 PathScale, Inc. All rights reserved.
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:
11 * Redistribution and use in source and binary forms, with or
12 * without modification, are permitted provided that the following
15 * - Redistributions of source code must retain the above
16 * copyright notice, this list of conditions and the following
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.
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
34 #include <linux/err.h>
35 #include <linux/vmalloc.h>
37 #include "ipath_verbs.h"
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
45 * This may be called from interrupt context.
47 int ipath_post_srq_receive(struct ib_srq *ibsrq, struct ib_recv_wr *wr,
48 struct ib_recv_wr **bad_wr)
50 struct ipath_srq *srq = to_isrq(ibsrq);
55 for (; wr; wr = wr->next) {
56 struct ipath_rwqe *wqe;
60 if ((unsigned) wr->num_sge > srq->rq.max_sge) {
66 spin_lock_irqsave(&srq->rq.lock, flags);
69 if (next >= srq->rq.size)
71 if (next == wq->tail) {
72 spin_unlock_irqrestore(&srq->rq.lock, flags);
78 wqe = get_rwqe_ptr(&srq->rq, wq->head);
79 wqe->wr_id = wr->wr_id;
80 wqe->num_sge = wr->num_sge;
81 for (i = 0; i < wr->num_sge; i++)
82 wqe->sg_list[i] = wr->sg_list[i];
84 spin_unlock_irqrestore(&srq->rq.lock, flags);
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
98 struct ib_srq *ipath_create_srq(struct ib_pd *ibpd,
99 struct ib_srq_init_attr *srq_init_attr,
100 struct ib_udata *udata)
102 struct ipath_ibdev *dev = to_idev(ibpd->device);
103 struct ipath_srq *srq;
107 if (dev->n_srqs_allocated == ib_ipath_max_srqs) {
108 ret = ERR_PTR(-ENOMEM);
112 if (srq_init_attr->attr.max_wr == 0) {
113 ret = ERR_PTR(-EINVAL);
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)) {
119 ret = ERR_PTR(-EINVAL);
123 srq = kmalloc(sizeof(*srq), GFP_KERNEL);
125 ret = ERR_PTR(-ENOMEM);
130 * Need to use vmalloc() if we want to support large #s of entries.
132 srq->rq.size = srq_init_attr->attr.max_wr + 1;
133 srq->rq.max_sge = srq_init_attr->attr.max_sge;
134 sz = sizeof(struct ib_sge) * srq->rq.max_sge +
135 sizeof(struct ipath_rwqe);
136 srq->rq.wq = vmalloc_user(sizeof(struct ipath_rwq) + srq->rq.size * sz);
138 ret = ERR_PTR(-ENOMEM);
143 * Return the address of the RWQ as the offset to mmap.
144 * See ipath_mmap() for details.
146 if (udata && udata->outlen >= sizeof(__u64)) {
147 struct ipath_mmap_info *ip;
148 __u64 offset = (__u64) srq->rq.wq;
151 err = ib_copy_to_udata(udata, &offset, sizeof(offset));
157 /* Allocate info for ipath_mmap(). */
158 ip = kmalloc(sizeof(*ip), GFP_KERNEL);
160 ret = ERR_PTR(-ENOMEM);
164 ip->context = ibpd->uobject->context;
165 ip->obj = srq->rq.wq;
168 ip->size = PAGE_ALIGN(sizeof(struct ipath_rwq) +
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);
178 * ib_create_srq() will initialize srq->ibsrq.
180 spin_lock_init(&srq->rq.lock);
181 srq->rq.wq->head = 0;
182 srq->rq.wq->tail = 0;
183 srq->rq.max_sge = srq_init_attr->attr.max_sge;
184 srq->limit = srq_init_attr->attr.srq_limit;
186 dev->n_srqs_allocated++;
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
206 * @udata: user data for ipathverbs.so
208 int ipath_modify_srq(struct ib_srq *ibsrq, struct ib_srq_attr *attr,
209 enum ib_srq_attr_mask attr_mask,
210 struct ib_udata *udata)
212 struct ipath_srq *srq = to_isrq(ibsrq);
215 if (attr_mask & IB_SRQ_MAX_WR) {
216 struct ipath_rwq *owq;
217 struct ipath_rwq *wq;
218 struct ipath_rwqe *p;
219 u32 sz, size, n, head, tail;
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) {
229 sz = sizeof(struct ipath_rwqe) +
230 srq->rq.max_sge * sizeof(struct ib_sge);
231 size = attr->max_wr + 1;
232 wq = vmalloc_user(sizeof(struct ipath_rwq) + size * sz);
239 * Return the address of the RWQ as the offset to mmap.
240 * See ipath_mmap() for details.
242 if (udata && udata->inlen >= sizeof(__u64)) {
244 __u64 offset = (__u64) wq;
246 ret = ib_copy_from_udata(&offset_addr, udata,
247 sizeof(offset_addr));
252 udata->outbuf = (void __user *) offset_addr;
253 ret = ib_copy_to_udata(udata, &offset,
261 spin_lock_irq(&srq->rq.lock);
263 * validate head pointer value and compute
264 * the number of remaining WQEs.
268 if (head >= srq->rq.size)
271 if (tail >= srq->rq.size)
275 n += srq->rq.size - tail;
279 spin_unlock_irq(&srq->rq.lock);
286 while (tail != head) {
287 struct ipath_rwqe *wqe;
290 wqe = get_rwqe_ptr(&srq->rq, tail);
291 p->wr_id = wqe->wr_id;
292 p->num_sge = wqe->num_sge;
293 for (i = 0; i < wqe->num_sge; i++)
294 p->sg_list[i] = wqe->sg_list[i];
296 p = (struct ipath_rwqe *)((char *) p + sz);
297 if (++tail >= srq->rq.size)
304 if (attr_mask & IB_SRQ_LIMIT)
305 srq->limit = attr->srq_limit;
306 spin_unlock_irq(&srq->rq.lock);
311 struct ipath_mmap_info *ip = srq->ip;
312 struct ipath_ibdev *dev = to_idev(srq->ibsrq.device);
315 ip->size = PAGE_ALIGN(sizeof(struct ipath_rwq) +
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);
322 } else if (attr_mask & IB_SRQ_LIMIT) {
323 spin_lock_irq(&srq->rq.lock);
324 if (attr->srq_limit >= srq->rq.size)
327 srq->limit = attr->srq_limit;
328 spin_unlock_irq(&srq->rq.lock);
335 int ipath_query_srq(struct ib_srq *ibsrq, struct ib_srq_attr *attr)
337 struct ipath_srq *srq = to_isrq(ibsrq);
339 attr->max_wr = srq->rq.size - 1;
340 attr->max_sge = srq->rq.max_sge;
341 attr->srq_limit = srq->limit;
346 * ipath_destroy_srq - destroy a shared receive queue
347 * @ibsrq: the SRQ to destroy
349 int ipath_destroy_srq(struct ib_srq *ibsrq)
351 struct ipath_srq *srq = to_isrq(ibsrq);
352 struct ipath_ibdev *dev = to_idev(ibsrq->device);
354 dev->n_srqs_allocated--;