xref: /linux/drivers/infiniband/hw/mlx4/srq.c (revision 1f2367a39f17bd553a75e179a747f9b257bc9478)
1 /*
2  * Copyright (c) 2007 Cisco Systems, Inc. All rights reserved.
3  * Copyright (c) 2007, 2008 Mellanox Technologies. 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/mlx4/qp.h>
35 #include <linux/mlx4/srq.h>
36 #include <linux/slab.h>
37 
38 #include "mlx4_ib.h"
39 #include <rdma/mlx4-abi.h>
40 #include <rdma/uverbs_ioctl.h>
41 
42 static void *get_wqe(struct mlx4_ib_srq *srq, int n)
43 {
44 	return mlx4_buf_offset(&srq->buf, n << srq->msrq.wqe_shift);
45 }
46 
47 static void mlx4_ib_srq_event(struct mlx4_srq *srq, enum mlx4_event type)
48 {
49 	struct ib_event event;
50 	struct ib_srq *ibsrq = &to_mibsrq(srq)->ibsrq;
51 
52 	if (ibsrq->event_handler) {
53 		event.device      = ibsrq->device;
54 		event.element.srq = ibsrq;
55 		switch (type) {
56 		case MLX4_EVENT_TYPE_SRQ_LIMIT:
57 			event.event = IB_EVENT_SRQ_LIMIT_REACHED;
58 			break;
59 		case MLX4_EVENT_TYPE_SRQ_CATAS_ERROR:
60 			event.event = IB_EVENT_SRQ_ERR;
61 			break;
62 		default:
63 			pr_warn("Unexpected event type %d "
64 			       "on SRQ %06x\n", type, srq->srqn);
65 			return;
66 		}
67 
68 		ibsrq->event_handler(&event, ibsrq->srq_context);
69 	}
70 }
71 
72 struct ib_srq *mlx4_ib_create_srq(struct ib_pd *pd,
73 				  struct ib_srq_init_attr *init_attr,
74 				  struct ib_udata *udata)
75 {
76 	struct mlx4_ib_dev *dev = to_mdev(pd->device);
77 	struct mlx4_ib_ucontext *ucontext = rdma_udata_to_drv_context(
78 		udata, struct mlx4_ib_ucontext, ibucontext);
79 	struct mlx4_ib_srq *srq;
80 	struct mlx4_wqe_srq_next_seg *next;
81 	struct mlx4_wqe_data_seg *scatter;
82 	u32 cqn;
83 	u16 xrcdn;
84 	int desc_size;
85 	int buf_size;
86 	int err;
87 	int i;
88 
89 	/* Sanity check SRQ size before proceeding */
90 	if (init_attr->attr.max_wr  >= dev->dev->caps.max_srq_wqes ||
91 	    init_attr->attr.max_sge >  dev->dev->caps.max_srq_sge)
92 		return ERR_PTR(-EINVAL);
93 
94 	srq = kmalloc(sizeof *srq, GFP_KERNEL);
95 	if (!srq)
96 		return ERR_PTR(-ENOMEM);
97 
98 	mutex_init(&srq->mutex);
99 	spin_lock_init(&srq->lock);
100 	srq->msrq.max    = roundup_pow_of_two(init_attr->attr.max_wr + 1);
101 	srq->msrq.max_gs = init_attr->attr.max_sge;
102 
103 	desc_size = max(32UL,
104 			roundup_pow_of_two(sizeof (struct mlx4_wqe_srq_next_seg) +
105 					   srq->msrq.max_gs *
106 					   sizeof (struct mlx4_wqe_data_seg)));
107 	srq->msrq.wqe_shift = ilog2(desc_size);
108 
109 	buf_size = srq->msrq.max * desc_size;
110 
111 	if (udata) {
112 		struct mlx4_ib_create_srq ucmd;
113 
114 		if (ib_copy_from_udata(&ucmd, udata, sizeof ucmd)) {
115 			err = -EFAULT;
116 			goto err_srq;
117 		}
118 
119 		srq->umem = ib_umem_get(udata, ucmd.buf_addr, buf_size, 0, 0);
120 		if (IS_ERR(srq->umem)) {
121 			err = PTR_ERR(srq->umem);
122 			goto err_srq;
123 		}
124 
125 		err = mlx4_mtt_init(dev->dev, ib_umem_page_count(srq->umem),
126 				    srq->umem->page_shift, &srq->mtt);
127 		if (err)
128 			goto err_buf;
129 
130 		err = mlx4_ib_umem_write_mtt(dev, &srq->mtt, srq->umem);
131 		if (err)
132 			goto err_mtt;
133 
134 		err = mlx4_ib_db_map_user(ucontext, udata, ucmd.db_addr,
135 					  &srq->db);
136 		if (err)
137 			goto err_mtt;
138 	} else {
139 		err = mlx4_db_alloc(dev->dev, &srq->db, 0);
140 		if (err)
141 			goto err_srq;
142 
143 		*srq->db.db = 0;
144 
145 		if (mlx4_buf_alloc(dev->dev, buf_size, PAGE_SIZE * 2,
146 				   &srq->buf)) {
147 			err = -ENOMEM;
148 			goto err_db;
149 		}
150 
151 		srq->head    = 0;
152 		srq->tail    = srq->msrq.max - 1;
153 		srq->wqe_ctr = 0;
154 
155 		for (i = 0; i < srq->msrq.max; ++i) {
156 			next = get_wqe(srq, i);
157 			next->next_wqe_index =
158 				cpu_to_be16((i + 1) & (srq->msrq.max - 1));
159 
160 			for (scatter = (void *) (next + 1);
161 			     (void *) scatter < (void *) next + desc_size;
162 			     ++scatter)
163 				scatter->lkey = cpu_to_be32(MLX4_INVALID_LKEY);
164 		}
165 
166 		err = mlx4_mtt_init(dev->dev, srq->buf.npages, srq->buf.page_shift,
167 				    &srq->mtt);
168 		if (err)
169 			goto err_buf;
170 
171 		err = mlx4_buf_write_mtt(dev->dev, &srq->mtt, &srq->buf);
172 		if (err)
173 			goto err_mtt;
174 
175 		srq->wrid = kvmalloc_array(srq->msrq.max,
176 					   sizeof(u64), GFP_KERNEL);
177 		if (!srq->wrid) {
178 			err = -ENOMEM;
179 			goto err_mtt;
180 		}
181 	}
182 
183 	cqn = ib_srq_has_cq(init_attr->srq_type) ?
184 		to_mcq(init_attr->ext.cq)->mcq.cqn : 0;
185 	xrcdn = (init_attr->srq_type == IB_SRQT_XRC) ?
186 		to_mxrcd(init_attr->ext.xrc.xrcd)->xrcdn :
187 		(u16) dev->dev->caps.reserved_xrcds;
188 	err = mlx4_srq_alloc(dev->dev, to_mpd(pd)->pdn, cqn, xrcdn, &srq->mtt,
189 			     srq->db.dma, &srq->msrq);
190 	if (err)
191 		goto err_wrid;
192 
193 	srq->msrq.event = mlx4_ib_srq_event;
194 	srq->ibsrq.ext.xrc.srq_num = srq->msrq.srqn;
195 
196 	if (udata)
197 		if (ib_copy_to_udata(udata, &srq->msrq.srqn, sizeof (__u32))) {
198 			err = -EFAULT;
199 			goto err_wrid;
200 		}
201 
202 	init_attr->attr.max_wr = srq->msrq.max - 1;
203 
204 	return &srq->ibsrq;
205 
206 err_wrid:
207 	if (udata)
208 		mlx4_ib_db_unmap_user(ucontext, &srq->db);
209 	else
210 		kvfree(srq->wrid);
211 
212 err_mtt:
213 	mlx4_mtt_cleanup(dev->dev, &srq->mtt);
214 
215 err_buf:
216 	if (srq->umem)
217 		ib_umem_release(srq->umem);
218 	else
219 		mlx4_buf_free(dev->dev, buf_size, &srq->buf);
220 
221 err_db:
222 	if (!udata)
223 		mlx4_db_free(dev->dev, &srq->db);
224 
225 err_srq:
226 	kfree(srq);
227 
228 	return ERR_PTR(err);
229 }
230 
231 int mlx4_ib_modify_srq(struct ib_srq *ibsrq, struct ib_srq_attr *attr,
232 		       enum ib_srq_attr_mask attr_mask, struct ib_udata *udata)
233 {
234 	struct mlx4_ib_dev *dev = to_mdev(ibsrq->device);
235 	struct mlx4_ib_srq *srq = to_msrq(ibsrq);
236 	int ret;
237 
238 	/* We don't support resizing SRQs (yet?) */
239 	if (attr_mask & IB_SRQ_MAX_WR)
240 		return -EINVAL;
241 
242 	if (attr_mask & IB_SRQ_LIMIT) {
243 		if (attr->srq_limit >= srq->msrq.max)
244 			return -EINVAL;
245 
246 		mutex_lock(&srq->mutex);
247 		ret = mlx4_srq_arm(dev->dev, &srq->msrq, attr->srq_limit);
248 		mutex_unlock(&srq->mutex);
249 
250 		if (ret)
251 			return ret;
252 	}
253 
254 	return 0;
255 }
256 
257 int mlx4_ib_query_srq(struct ib_srq *ibsrq, struct ib_srq_attr *srq_attr)
258 {
259 	struct mlx4_ib_dev *dev = to_mdev(ibsrq->device);
260 	struct mlx4_ib_srq *srq = to_msrq(ibsrq);
261 	int ret;
262 	int limit_watermark;
263 
264 	ret = mlx4_srq_query(dev->dev, &srq->msrq, &limit_watermark);
265 	if (ret)
266 		return ret;
267 
268 	srq_attr->srq_limit = limit_watermark;
269 	srq_attr->max_wr    = srq->msrq.max - 1;
270 	srq_attr->max_sge   = srq->msrq.max_gs;
271 
272 	return 0;
273 }
274 
275 int mlx4_ib_destroy_srq(struct ib_srq *srq)
276 {
277 	struct mlx4_ib_dev *dev = to_mdev(srq->device);
278 	struct mlx4_ib_srq *msrq = to_msrq(srq);
279 
280 	mlx4_srq_free(dev->dev, &msrq->msrq);
281 	mlx4_mtt_cleanup(dev->dev, &msrq->mtt);
282 
283 	if (srq->uobject) {
284 		mlx4_ib_db_unmap_user(to_mucontext(srq->uobject->context), &msrq->db);
285 		ib_umem_release(msrq->umem);
286 	} else {
287 		kvfree(msrq->wrid);
288 		mlx4_buf_free(dev->dev, msrq->msrq.max << msrq->msrq.wqe_shift,
289 			      &msrq->buf);
290 		mlx4_db_free(dev->dev, &msrq->db);
291 	}
292 
293 	kfree(msrq);
294 
295 	return 0;
296 }
297 
298 void mlx4_ib_free_srq_wqe(struct mlx4_ib_srq *srq, int wqe_index)
299 {
300 	struct mlx4_wqe_srq_next_seg *next;
301 
302 	/* always called with interrupts disabled. */
303 	spin_lock(&srq->lock);
304 
305 	next = get_wqe(srq, srq->tail);
306 	next->next_wqe_index = cpu_to_be16(wqe_index);
307 	srq->tail = wqe_index;
308 
309 	spin_unlock(&srq->lock);
310 }
311 
312 int mlx4_ib_post_srq_recv(struct ib_srq *ibsrq, const struct ib_recv_wr *wr,
313 			  const struct ib_recv_wr **bad_wr)
314 {
315 	struct mlx4_ib_srq *srq = to_msrq(ibsrq);
316 	struct mlx4_wqe_srq_next_seg *next;
317 	struct mlx4_wqe_data_seg *scat;
318 	unsigned long flags;
319 	int err = 0;
320 	int nreq;
321 	int i;
322 	struct mlx4_ib_dev *mdev = to_mdev(ibsrq->device);
323 
324 	spin_lock_irqsave(&srq->lock, flags);
325 	if (mdev->dev->persist->state & MLX4_DEVICE_STATE_INTERNAL_ERROR) {
326 		err = -EIO;
327 		*bad_wr = wr;
328 		nreq = 0;
329 		goto out;
330 	}
331 
332 	for (nreq = 0; wr; ++nreq, wr = wr->next) {
333 		if (unlikely(wr->num_sge > srq->msrq.max_gs)) {
334 			err = -EINVAL;
335 			*bad_wr = wr;
336 			break;
337 		}
338 
339 		if (unlikely(srq->head == srq->tail)) {
340 			err = -ENOMEM;
341 			*bad_wr = wr;
342 			break;
343 		}
344 
345 		srq->wrid[srq->head] = wr->wr_id;
346 
347 		next      = get_wqe(srq, srq->head);
348 		srq->head = be16_to_cpu(next->next_wqe_index);
349 		scat      = (struct mlx4_wqe_data_seg *) (next + 1);
350 
351 		for (i = 0; i < wr->num_sge; ++i) {
352 			scat[i].byte_count = cpu_to_be32(wr->sg_list[i].length);
353 			scat[i].lkey       = cpu_to_be32(wr->sg_list[i].lkey);
354 			scat[i].addr       = cpu_to_be64(wr->sg_list[i].addr);
355 		}
356 
357 		if (i < srq->msrq.max_gs) {
358 			scat[i].byte_count = 0;
359 			scat[i].lkey       = cpu_to_be32(MLX4_INVALID_LKEY);
360 			scat[i].addr       = 0;
361 		}
362 	}
363 
364 	if (likely(nreq)) {
365 		srq->wqe_ctr += nreq;
366 
367 		/*
368 		 * Make sure that descriptors are written before
369 		 * doorbell record.
370 		 */
371 		wmb();
372 
373 		*srq->db.db = cpu_to_be32(srq->wqe_ctr);
374 	}
375 out:
376 
377 	spin_unlock_irqrestore(&srq->lock, flags);
378 
379 	return err;
380 }
381