blob: 98c492797c539b3595d65c361f3680a28e248fbb [file] [log] [blame]
Dennis Dalessandroaad91582016-01-06 09:57:58 -08001/*
Dennis Dalessandrofe314192016-01-22 13:04:58 -08002 * Copyright(c) 2016 Intel Corporation.
Dennis Dalessandroaad91582016-01-06 09:57:58 -08003 *
4 * This file is provided under a dual BSD/GPLv2 license. When using or
5 * redistributing this file, you may do so under either license.
6 *
7 * GPL LICENSE SUMMARY
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of version 2 of the GNU General Public License as
11 * published by the Free Software Foundation.
12 *
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
17 *
18 * BSD LICENSE
19 *
20 * Redistribution and use in source and binary forms, with or without
21 * modification, are permitted provided that the following conditions
22 * are met:
23 *
24 * - Redistributions of source code must retain the above copyright
25 * notice, this list of conditions and the following disclaimer.
26 * - Redistributions in binary form must reproduce the above copyright
27 * notice, this list of conditions and the following disclaimer in
28 * the documentation and/or other materials provided with the
29 * distribution.
30 * - Neither the name of Intel Corporation nor the names of its
31 * contributors may be used to endorse or promote products derived
32 * from this software without specific prior written permission.
33 *
34 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
35 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
36 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
37 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
38 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
39 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
40 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
41 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
42 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
43 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
44 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
45 *
46 */
47
Jubin Johnb8f881b2016-02-03 14:14:36 -080048#include <linux/err.h>
49#include <linux/slab.h>
50#include <linux/vmalloc.h>
51
Dennis Dalessandroaad91582016-01-06 09:57:58 -080052#include "srq.h"
53
Dennis Dalessandro90793f72016-02-14 12:10:29 -080054/**
55 * rvt_driver_srq_init - init srq resources on a per driver basis
56 * @rdi: rvt dev structure
57 *
Jubin Johnb8f881b2016-02-03 14:14:36 -080058 * Do any initialization needed when a driver registers with rdmavt.
59 */
60void rvt_driver_srq_init(struct rvt_dev_info *rdi)
61{
62 spin_lock_init(&rdi->n_srqs_lock);
63 rdi->n_srqs_allocated = 0;
64}
65
Dennis Dalessandroaad91582016-01-06 09:57:58 -080066/**
67 * rvt_create_srq - create a shared receive queue
68 * @ibpd: the protection domain of the SRQ to create
69 * @srq_init_attr: the attributes of the SRQ
70 * @udata: data from libibverbs when creating a user SRQ
Dennis Dalessandro90793f72016-02-14 12:10:29 -080071 *
72 * Return: Allocated srq object
Dennis Dalessandroaad91582016-01-06 09:57:58 -080073 */
74struct ib_srq *rvt_create_srq(struct ib_pd *ibpd,
75 struct ib_srq_init_attr *srq_init_attr,
76 struct ib_udata *udata)
77{
Jubin Johnb8f881b2016-02-03 14:14:36 -080078 struct rvt_dev_info *dev = ib_to_rvt(ibpd->device);
79 struct rvt_srq *srq;
80 u32 sz;
81 struct ib_srq *ret;
82
83 if (srq_init_attr->srq_type != IB_SRQT_BASIC)
84 return ERR_PTR(-ENOSYS);
85
86 if (srq_init_attr->attr.max_sge == 0 ||
87 srq_init_attr->attr.max_sge > dev->dparms.props.max_srq_sge ||
88 srq_init_attr->attr.max_wr == 0 ||
89 srq_init_attr->attr.max_wr > dev->dparms.props.max_srq_wr)
90 return ERR_PTR(-EINVAL);
91
92 srq = kmalloc(sizeof(*srq), GFP_KERNEL);
93 if (!srq)
94 return ERR_PTR(-ENOMEM);
95
96 /*
97 * Need to use vmalloc() if we want to support large #s of entries.
98 */
99 srq->rq.size = srq_init_attr->attr.max_wr + 1;
100 srq->rq.max_sge = srq_init_attr->attr.max_sge;
101 sz = sizeof(struct ib_sge) * srq->rq.max_sge +
102 sizeof(struct rvt_rwqe);
103 srq->rq.wq = vmalloc_user(sizeof(struct rvt_rwq) + srq->rq.size * sz);
104 if (!srq->rq.wq) {
105 ret = ERR_PTR(-ENOMEM);
106 goto bail_srq;
107 }
108
109 /*
110 * Return the address of the RWQ as the offset to mmap.
111 * See rvt_mmap() for details.
112 */
113 if (udata && udata->outlen >= sizeof(__u64)) {
114 int err;
115 u32 s = sizeof(struct rvt_rwq) + srq->rq.size * sz;
116
117 srq->ip =
118 rvt_create_mmap_info(dev, s, ibpd->uobject->context,
119 srq->rq.wq);
120 if (!srq->ip) {
121 ret = ERR_PTR(-ENOMEM);
122 goto bail_wq;
123 }
124
125 err = ib_copy_to_udata(udata, &srq->ip->offset,
126 sizeof(srq->ip->offset));
127 if (err) {
128 ret = ERR_PTR(err);
129 goto bail_ip;
130 }
131 } else {
132 srq->ip = NULL;
133 }
134
135 /*
136 * ib_create_srq() will initialize srq->ibsrq.
137 */
138 spin_lock_init(&srq->rq.lock);
139 srq->rq.wq->head = 0;
140 srq->rq.wq->tail = 0;
141 srq->limit = srq_init_attr->attr.srq_limit;
142
143 spin_lock(&dev->n_srqs_lock);
144 if (dev->n_srqs_allocated == dev->dparms.props.max_srq) {
145 spin_unlock(&dev->n_srqs_lock);
146 ret = ERR_PTR(-ENOMEM);
147 goto bail_ip;
148 }
149
150 dev->n_srqs_allocated++;
151 spin_unlock(&dev->n_srqs_lock);
152
153 if (srq->ip) {
154 spin_lock_irq(&dev->pending_lock);
155 list_add(&srq->ip->pending_mmaps, &dev->pending_mmaps);
156 spin_unlock_irq(&dev->pending_lock);
157 }
158
159 return &srq->ibsrq;
160
161bail_ip:
162 kfree(srq->ip);
163bail_wq:
164 vfree(srq->rq.wq);
165bail_srq:
166 kfree(srq);
167 return ret;
Dennis Dalessandroaad91582016-01-06 09:57:58 -0800168}
169
170/**
171 * rvt_modify_srq - modify a shared receive queue
172 * @ibsrq: the SRQ to modify
173 * @attr: the new attributes of the SRQ
174 * @attr_mask: indicates which attributes to modify
175 * @udata: user data for libibverbs.so
Dennis Dalessandro90793f72016-02-14 12:10:29 -0800176 *
177 * Return: 0 on success
Dennis Dalessandroaad91582016-01-06 09:57:58 -0800178 */
179int rvt_modify_srq(struct ib_srq *ibsrq, struct ib_srq_attr *attr,
180 enum ib_srq_attr_mask attr_mask,
181 struct ib_udata *udata)
182{
Jubin Johnb8f881b2016-02-03 14:14:36 -0800183 struct rvt_srq *srq = ibsrq_to_rvtsrq(ibsrq);
184 struct rvt_dev_info *dev = ib_to_rvt(ibsrq->device);
185 struct rvt_rwq *wq;
186 int ret = 0;
187
188 if (attr_mask & IB_SRQ_MAX_WR) {
189 struct rvt_rwq *owq;
190 struct rvt_rwqe *p;
191 u32 sz, size, n, head, tail;
192
193 /* Check that the requested sizes are below the limits. */
194 if ((attr->max_wr > dev->dparms.props.max_srq_wr) ||
195 ((attr_mask & IB_SRQ_LIMIT) ?
196 attr->srq_limit : srq->limit) > attr->max_wr)
197 return -EINVAL;
198
199 sz = sizeof(struct rvt_rwqe) +
200 srq->rq.max_sge * sizeof(struct ib_sge);
201 size = attr->max_wr + 1;
202 wq = vmalloc_user(sizeof(struct rvt_rwq) + size * sz);
203 if (!wq)
204 return -ENOMEM;
205
206 /* Check that we can write the offset to mmap. */
207 if (udata && udata->inlen >= sizeof(__u64)) {
208 __u64 offset_addr;
209 __u64 offset = 0;
210
211 ret = ib_copy_from_udata(&offset_addr, udata,
212 sizeof(offset_addr));
213 if (ret)
214 goto bail_free;
215 udata->outbuf = (void __user *)
216 (unsigned long)offset_addr;
217 ret = ib_copy_to_udata(udata, &offset,
218 sizeof(offset));
219 if (ret)
220 goto bail_free;
221 }
222
223 spin_lock_irq(&srq->rq.lock);
224 /*
225 * validate head and tail pointer values and compute
226 * the number of remaining WQEs.
227 */
228 owq = srq->rq.wq;
229 head = owq->head;
230 tail = owq->tail;
231 if (head >= srq->rq.size || tail >= srq->rq.size) {
232 ret = -EINVAL;
233 goto bail_unlock;
234 }
235 n = head;
236 if (n < tail)
237 n += srq->rq.size - tail;
238 else
239 n -= tail;
240 if (size <= n) {
241 ret = -EINVAL;
242 goto bail_unlock;
243 }
244 n = 0;
245 p = wq->wq;
246 while (tail != head) {
247 struct rvt_rwqe *wqe;
248 int i;
249
250 wqe = rvt_get_rwqe_ptr(&srq->rq, tail);
251 p->wr_id = wqe->wr_id;
252 p->num_sge = wqe->num_sge;
253 for (i = 0; i < wqe->num_sge; i++)
254 p->sg_list[i] = wqe->sg_list[i];
255 n++;
256 p = (struct rvt_rwqe *)((char *)p + sz);
257 if (++tail >= srq->rq.size)
258 tail = 0;
259 }
260 srq->rq.wq = wq;
261 srq->rq.size = size;
262 wq->head = n;
263 wq->tail = 0;
264 if (attr_mask & IB_SRQ_LIMIT)
265 srq->limit = attr->srq_limit;
266 spin_unlock_irq(&srq->rq.lock);
267
268 vfree(owq);
269
270 if (srq->ip) {
271 struct rvt_mmap_info *ip = srq->ip;
272 struct rvt_dev_info *dev = ib_to_rvt(srq->ibsrq.device);
273 u32 s = sizeof(struct rvt_rwq) + size * sz;
274
275 rvt_update_mmap_info(dev, ip, s, wq);
276
277 /*
278 * Return the offset to mmap.
279 * See rvt_mmap() for details.
280 */
281 if (udata && udata->inlen >= sizeof(__u64)) {
282 ret = ib_copy_to_udata(udata, &ip->offset,
283 sizeof(ip->offset));
284 if (ret)
285 return ret;
286 }
287
288 /*
289 * Put user mapping info onto the pending list
290 * unless it already is on the list.
291 */
292 spin_lock_irq(&dev->pending_lock);
293 if (list_empty(&ip->pending_mmaps))
294 list_add(&ip->pending_mmaps,
295 &dev->pending_mmaps);
296 spin_unlock_irq(&dev->pending_lock);
297 }
298 } else if (attr_mask & IB_SRQ_LIMIT) {
299 spin_lock_irq(&srq->rq.lock);
300 if (attr->srq_limit >= srq->rq.size)
301 ret = -EINVAL;
302 else
303 srq->limit = attr->srq_limit;
304 spin_unlock_irq(&srq->rq.lock);
305 }
306 return ret;
307
308bail_unlock:
309 spin_unlock_irq(&srq->rq.lock);
310bail_free:
311 vfree(wq);
312 return ret;
Dennis Dalessandroaad91582016-01-06 09:57:58 -0800313}
314
Dennis Dalessandro90793f72016-02-14 12:10:29 -0800315/** rvt_query_srq - query srq data
316 * @ibsrq: srq to query
317 * @attr: return info in attr
318 *
319 * Return: always 0
320 */
Dennis Dalessandroaad91582016-01-06 09:57:58 -0800321int rvt_query_srq(struct ib_srq *ibsrq, struct ib_srq_attr *attr)
322{
Jubin Johnb8f881b2016-02-03 14:14:36 -0800323 struct rvt_srq *srq = ibsrq_to_rvtsrq(ibsrq);
324
325 attr->max_wr = srq->rq.size - 1;
326 attr->max_sge = srq->rq.max_sge;
327 attr->srq_limit = srq->limit;
328 return 0;
Dennis Dalessandroaad91582016-01-06 09:57:58 -0800329}
330
Dennis Dalessandro90793f72016-02-14 12:10:29 -0800331/**
332 * rvt_destroy_srq - destory an srq
333 * @ibsrq: srq object to destroy
334 *
335 * Return always 0
336 */
Dennis Dalessandroaad91582016-01-06 09:57:58 -0800337int rvt_destroy_srq(struct ib_srq *ibsrq)
338{
Jubin Johnb8f881b2016-02-03 14:14:36 -0800339 struct rvt_srq *srq = ibsrq_to_rvtsrq(ibsrq);
340 struct rvt_dev_info *dev = ib_to_rvt(ibsrq->device);
Dennis Dalessandroaad91582016-01-06 09:57:58 -0800341
Jubin Johnb8f881b2016-02-03 14:14:36 -0800342 spin_lock(&dev->n_srqs_lock);
343 dev->n_srqs_allocated--;
344 spin_unlock(&dev->n_srqs_lock);
345 if (srq->ip)
346 kref_put(&srq->ip->ref, rvt_release_mmap_info);
347 else
348 vfree(srq->rq.wq);
349 kfree(srq);
350
351 return 0;
352}