blob: f760434660bd1e589d666f9d31ab09b6d51b9fa7 [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);
51 struct ipath_ibdev *dev = to_idev(ibsrq->device);
52 unsigned long flags;
53 int ret;
54
55 for (; wr; wr = wr->next) {
56 struct ipath_rwqe *wqe;
57 u32 next;
58 int i, j;
59
60 if (wr->num_sge > srq->rq.max_sge) {
61 *bad_wr = wr;
62 ret = -ENOMEM;
63 goto bail;
64 }
65
66 spin_lock_irqsave(&srq->rq.lock, flags);
67 next = srq->rq.head + 1;
68 if (next >= srq->rq.size)
69 next = 0;
70 if (next == srq->rq.tail) {
71 spin_unlock_irqrestore(&srq->rq.lock, flags);
72 *bad_wr = wr;
73 ret = -ENOMEM;
74 goto bail;
75 }
76
77 wqe = get_rwqe_ptr(&srq->rq, srq->rq.head);
78 wqe->wr_id = wr->wr_id;
79 wqe->sg_list[0].mr = NULL;
80 wqe->sg_list[0].vaddr = NULL;
81 wqe->sg_list[0].length = 0;
82 wqe->sg_list[0].sge_length = 0;
83 wqe->length = 0;
84 for (i = 0, j = 0; i < wr->num_sge; i++) {
85 /* Check LKEY */
86 if (to_ipd(srq->ibsrq.pd)->user &&
87 wr->sg_list[i].lkey == 0) {
88 spin_unlock_irqrestore(&srq->rq.lock,
89 flags);
90 *bad_wr = wr;
91 ret = -EINVAL;
92 goto bail;
93 }
94 if (wr->sg_list[i].length == 0)
95 continue;
96 if (!ipath_lkey_ok(&dev->lk_table,
97 &wqe->sg_list[j],
98 &wr->sg_list[i],
99 IB_ACCESS_LOCAL_WRITE)) {
100 spin_unlock_irqrestore(&srq->rq.lock,
101 flags);
102 *bad_wr = wr;
103 ret = -EINVAL;
104 goto bail;
105 }
106 wqe->length += wr->sg_list[i].length;
107 j++;
108 }
109 wqe->num_sge = j;
110 srq->rq.head = next;
111 spin_unlock_irqrestore(&srq->rq.lock, flags);
112 }
113 ret = 0;
114
115bail:
116 return ret;
117}
118
119/**
120 * ipath_create_srq - create a shared receive queue
121 * @ibpd: the protection domain of the SRQ to create
122 * @attr: the attributes of the SRQ
123 * @udata: not used by the InfiniPath verbs driver
124 */
125struct ib_srq *ipath_create_srq(struct ib_pd *ibpd,
126 struct ib_srq_init_attr *srq_init_attr,
127 struct ib_udata *udata)
128{
Bryan O'Sullivanfe625462006-07-01 04:35:58 -0700129 struct ipath_ibdev *dev = to_idev(ibpd->device);
Bryan O'Sullivancef1cce2006-03-29 15:23:36 -0800130 struct ipath_srq *srq;
131 u32 sz;
132 struct ib_srq *ret;
133
Bryan O'Sullivanfe625462006-07-01 04:35:58 -0700134 if (dev->n_srqs_allocated == ib_ipath_max_srqs) {
135 ret = ERR_PTR(-ENOMEM);
136 goto bail;
137 }
138
139 if (srq_init_attr->attr.max_wr == 0) {
140 ret = ERR_PTR(-EINVAL);
141 goto bail;
142 }
143
144 if ((srq_init_attr->attr.max_sge > ib_ipath_max_srq_sges) ||
145 (srq_init_attr->attr.max_wr > ib_ipath_max_srq_wrs)) {
Bryan O'Sullivancef1cce2006-03-29 15:23:36 -0800146 ret = ERR_PTR(-EINVAL);
147 goto bail;
148 }
149
150 srq = kmalloc(sizeof(*srq), GFP_KERNEL);
151 if (!srq) {
152 ret = ERR_PTR(-ENOMEM);
153 goto bail;
154 }
155
156 /*
157 * Need to use vmalloc() if we want to support large #s of entries.
158 */
159 srq->rq.size = srq_init_attr->attr.max_wr + 1;
160 sz = sizeof(struct ipath_sge) * srq_init_attr->attr.max_sge +
161 sizeof(struct ipath_rwqe);
162 srq->rq.wq = vmalloc(srq->rq.size * sz);
163 if (!srq->rq.wq) {
164 kfree(srq);
165 ret = ERR_PTR(-ENOMEM);
166 goto bail;
167 }
168
169 /*
170 * ib_create_srq() will initialize srq->ibsrq.
171 */
172 spin_lock_init(&srq->rq.lock);
173 srq->rq.head = 0;
174 srq->rq.tail = 0;
175 srq->rq.max_sge = srq_init_attr->attr.max_sge;
176 srq->limit = srq_init_attr->attr.srq_limit;
177
178 ret = &srq->ibsrq;
179
Bryan O'Sullivanfe625462006-07-01 04:35:58 -0700180 dev->n_srqs_allocated++;
181
Bryan O'Sullivancef1cce2006-03-29 15:23:36 -0800182bail:
183 return ret;
184}
185
186/**
187 * ipath_modify_srq - modify a shared receive queue
188 * @ibsrq: the SRQ to modify
189 * @attr: the new attributes of the SRQ
190 * @attr_mask: indicates which attributes to modify
191 */
192int ipath_modify_srq(struct ib_srq *ibsrq, struct ib_srq_attr *attr,
193 enum ib_srq_attr_mask attr_mask)
194{
195 struct ipath_srq *srq = to_isrq(ibsrq);
196 unsigned long flags;
197 int ret;
198
Bryan O'Sullivanfe625462006-07-01 04:35:58 -0700199 if (attr_mask & IB_SRQ_MAX_WR)
200 if ((attr->max_wr > ib_ipath_max_srq_wrs) ||
201 (attr->max_sge > srq->rq.max_sge)) {
Bryan O'Sullivancef1cce2006-03-29 15:23:36 -0800202 ret = -EINVAL;
203 goto bail;
204 }
205
Bryan O'Sullivanfe625462006-07-01 04:35:58 -0700206 if (attr_mask & IB_SRQ_LIMIT)
207 if (attr->srq_limit >= srq->rq.size) {
208 ret = -EINVAL;
209 goto bail;
210 }
211
212 if (attr_mask & IB_SRQ_MAX_WR) {
213 struct ipath_rwqe *wq, *p;
214 u32 sz, size, n;
215
Bryan O'Sullivancef1cce2006-03-29 15:23:36 -0800216 sz = sizeof(struct ipath_rwqe) +
217 attr->max_sge * sizeof(struct ipath_sge);
Bryan O'Sullivanfe625462006-07-01 04:35:58 -0700218 size = attr->max_wr + 1;
Bryan O'Sullivancef1cce2006-03-29 15:23:36 -0800219 wq = vmalloc(size * sz);
220 if (!wq) {
221 ret = -ENOMEM;
222 goto bail;
223 }
224
225 spin_lock_irqsave(&srq->rq.lock, flags);
226 if (srq->rq.head < srq->rq.tail)
227 n = srq->rq.size + srq->rq.head - srq->rq.tail;
228 else
229 n = srq->rq.head - srq->rq.tail;
230 if (size <= n || size <= srq->limit) {
231 spin_unlock_irqrestore(&srq->rq.lock, flags);
232 vfree(wq);
233 ret = -EINVAL;
234 goto bail;
235 }
236 n = 0;
237 p = wq;
238 while (srq->rq.tail != srq->rq.head) {
239 struct ipath_rwqe *wqe;
240 int i;
241
242 wqe = get_rwqe_ptr(&srq->rq, srq->rq.tail);
243 p->wr_id = wqe->wr_id;
244 p->length = wqe->length;
245 p->num_sge = wqe->num_sge;
246 for (i = 0; i < wqe->num_sge; i++)
247 p->sg_list[i] = wqe->sg_list[i];
248 n++;
249 p = (struct ipath_rwqe *)((char *) p + sz);
250 if (++srq->rq.tail >= srq->rq.size)
251 srq->rq.tail = 0;
252 }
253 vfree(srq->rq.wq);
254 srq->rq.wq = wq;
255 srq->rq.size = size;
256 srq->rq.head = n;
257 srq->rq.tail = 0;
258 srq->rq.max_sge = attr->max_sge;
259 spin_unlock_irqrestore(&srq->rq.lock, flags);
260 }
261
Bryan O'Sullivanfe625462006-07-01 04:35:58 -0700262 if (attr_mask & IB_SRQ_LIMIT) {
263 spin_lock_irqsave(&srq->rq.lock, flags);
264 srq->limit = attr->srq_limit;
265 spin_unlock_irqrestore(&srq->rq.lock, flags);
266 }
Bryan O'Sullivancef1cce2006-03-29 15:23:36 -0800267 ret = 0;
268
269bail:
270 return ret;
271}
272
273int ipath_query_srq(struct ib_srq *ibsrq, struct ib_srq_attr *attr)
274{
275 struct ipath_srq *srq = to_isrq(ibsrq);
276
277 attr->max_wr = srq->rq.size - 1;
278 attr->max_sge = srq->rq.max_sge;
279 attr->srq_limit = srq->limit;
280 return 0;
281}
282
283/**
284 * ipath_destroy_srq - destroy a shared receive queue
285 * @ibsrq: the SRQ to destroy
286 */
287int ipath_destroy_srq(struct ib_srq *ibsrq)
288{
289 struct ipath_srq *srq = to_isrq(ibsrq);
Bryan O'Sullivanfe625462006-07-01 04:35:58 -0700290 struct ipath_ibdev *dev = to_idev(ibsrq->device);
Bryan O'Sullivancef1cce2006-03-29 15:23:36 -0800291
Bryan O'Sullivanfe625462006-07-01 04:35:58 -0700292 dev->n_srqs_allocated--;
Bryan O'Sullivancef1cce2006-03-29 15:23:36 -0800293 vfree(srq->rq.wq);
294 kfree(srq);
295
296 return 0;
297}