xref: /freebsd/sys/dev/mlx5/mlx5_ib/mlx5_ib_srq.c (revision 5c1d97100348ef19878fa14671a9b70f3d963ed4)
1 /*-
2  * Copyright (c) 2013-2015, Mellanox Technologies, Ltd.  All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS `AS IS' AND
14  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16  * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
17  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23  * SUCH DAMAGE.
24  *
25  * $FreeBSD$
26  */
27 
28 #include <linux/module.h>
29 #include <dev/mlx5/qp.h>
30 #include <dev/mlx5/srq.h>
31 #include <linux/slab.h>
32 #include <rdma/ib_umem.h>
33 #include <rdma/ib_user_verbs.h>
34 
35 #include "mlx5_ib.h"
36 
37 /* not supported currently */
38 static int srq_signature;
39 
40 static void *get_wqe(struct mlx5_ib_srq *srq, int n)
41 {
42 	return mlx5_buf_offset(&srq->buf, n << srq->msrq.wqe_shift);
43 }
44 
45 static void mlx5_ib_srq_event(struct mlx5_core_srq *srq, int type)
46 {
47 	struct ib_event event;
48 	struct ib_srq *ibsrq = &to_mibsrq(srq)->ibsrq;
49 
50 	if (ibsrq->event_handler) {
51 		event.device      = ibsrq->device;
52 		event.element.srq = ibsrq;
53 		switch (type) {
54 		case MLX5_EVENT_TYPE_SRQ_RQ_LIMIT:
55 			event.event = IB_EVENT_SRQ_LIMIT_REACHED;
56 			break;
57 		case MLX5_EVENT_TYPE_SRQ_CATAS_ERROR:
58 			event.event = IB_EVENT_SRQ_ERR;
59 			break;
60 		default:
61 			pr_warn("mlx5_ib: Unexpected event type %d on SRQ %06x\n",
62 				type, srq->srqn);
63 			return;
64 		}
65 
66 		ibsrq->event_handler(&event, ibsrq->srq_context);
67 	}
68 }
69 
70 static int create_srq_user(struct ib_pd *pd, struct mlx5_ib_srq *srq,
71 			   struct mlx5_srq_attr *in,
72 			   struct ib_udata *udata, int buf_size)
73 {
74 	struct mlx5_ib_dev *dev = to_mdev(pd->device);
75 	struct mlx5_ib_create_srq ucmd = {};
76 	size_t ucmdlen;
77 	int err;
78 	int npages;
79 	int page_shift;
80 	int ncont;
81 	u32 offset;
82 	u32 uidx = MLX5_IB_DEFAULT_UIDX;
83 
84 	ucmdlen = min(udata->inlen, sizeof(ucmd));
85 
86 	if (ib_copy_from_udata(&ucmd, udata, ucmdlen)) {
87 		mlx5_ib_dbg(dev, "failed copy udata\n");
88 		return -EFAULT;
89 	}
90 
91 	if (ucmd.reserved0 || ucmd.reserved1)
92 		return -EINVAL;
93 
94 	if (udata->inlen > sizeof(ucmd) &&
95 	    !ib_is_udata_cleared(udata, sizeof(ucmd),
96 				 udata->inlen - sizeof(ucmd)))
97 		return -EINVAL;
98 
99 	if (in->type == IB_SRQT_XRC) {
100 		err = get_srq_user_index(to_mucontext(pd->uobject->context),
101 					 &ucmd, udata->inlen, &uidx);
102 		if (err)
103 			return err;
104 	}
105 
106 	srq->wq_sig = !!(ucmd.flags & MLX5_SRQ_FLAG_SIGNATURE);
107 
108 	srq->umem = ib_umem_get(pd->uobject->context, ucmd.buf_addr, buf_size,
109 				0, 0);
110 	if (IS_ERR(srq->umem)) {
111 		mlx5_ib_dbg(dev, "failed umem get, size %d\n", buf_size);
112 		err = PTR_ERR(srq->umem);
113 		return err;
114 	}
115 
116 	mlx5_ib_cont_pages(srq->umem, ucmd.buf_addr, &npages,
117 			   &page_shift, &ncont, NULL);
118 	err = mlx5_ib_get_buf_offset(ucmd.buf_addr, page_shift,
119 				     &offset);
120 	if (err) {
121 		mlx5_ib_warn(dev, "bad offset\n");
122 		goto err_umem;
123 	}
124 
125 	in->pas = mlx5_vzalloc(sizeof(*in->pas) * ncont);
126 	if (!in->pas) {
127 		err = -ENOMEM;
128 		goto err_umem;
129 	}
130 
131 	mlx5_ib_populate_pas(dev, srq->umem, page_shift, in->pas, 0);
132 
133 	err = mlx5_ib_db_map_user(to_mucontext(pd->uobject->context),
134 				  ucmd.db_addr, &srq->db);
135 	if (err) {
136 		mlx5_ib_dbg(dev, "map doorbell failed\n");
137 		goto err_in;
138 	}
139 
140 	in->log_page_size = page_shift - MLX5_ADAPTER_PAGE_SHIFT;
141 	in->page_offset = offset;
142 	if (MLX5_CAP_GEN(dev->mdev, cqe_version) == MLX5_CQE_VERSION_V1 &&
143 	    in->type == IB_SRQT_XRC)
144 		in->user_index = uidx;
145 	return 0;
146 
147 err_in:
148 	kvfree(in->pas);
149 
150 err_umem:
151 	ib_umem_release(srq->umem);
152 
153 	return err;
154 }
155 
156 static int create_srq_kernel(struct mlx5_ib_dev *dev, struct mlx5_ib_srq *srq,
157 			     struct mlx5_srq_attr *in, int buf_size)
158 {
159 	int err;
160 	int i;
161 	struct mlx5_wqe_srq_next_seg *next;
162 
163 	err = mlx5_db_alloc(dev->mdev, &srq->db);
164 	if (err) {
165 		mlx5_ib_warn(dev, "alloc dbell rec failed\n");
166 		return err;
167 	}
168 
169 	if (mlx5_buf_alloc(dev->mdev, buf_size, 2 * PAGE_SIZE, &srq->buf)) {
170 		mlx5_ib_dbg(dev, "buf alloc failed\n");
171 		err = -ENOMEM;
172 		goto err_db;
173 	}
174 
175 	srq->head    = 0;
176 	srq->tail    = srq->msrq.max - 1;
177 	srq->wqe_ctr = 0;
178 
179 	for (i = 0; i < srq->msrq.max; i++) {
180 		next = get_wqe(srq, i);
181 		next->next_wqe_index =
182 			cpu_to_be16((i + 1) & (srq->msrq.max - 1));
183 	}
184 
185 	mlx5_ib_dbg(dev, "srq->buf.page_shift = %d\n", srq->buf.page_shift);
186 	in->pas = mlx5_vzalloc(sizeof(*in->pas) * srq->buf.npages);
187 	if (!in->pas) {
188 		err = -ENOMEM;
189 		goto err_buf;
190 	}
191 	mlx5_fill_page_array(&srq->buf, in->pas);
192 
193 	srq->wrid = kmalloc(srq->msrq.max * sizeof(u64), GFP_KERNEL);
194 	if (!srq->wrid) {
195 		mlx5_ib_dbg(dev, "kmalloc failed %lu\n",
196 			    (unsigned long)(srq->msrq.max * sizeof(u64)));
197 		err = -ENOMEM;
198 		goto err_in;
199 	}
200 	srq->wq_sig = !!srq_signature;
201 
202 	in->log_page_size = srq->buf.page_shift - MLX5_ADAPTER_PAGE_SHIFT;
203 	if (MLX5_CAP_GEN(dev->mdev, cqe_version) == MLX5_CQE_VERSION_V1 &&
204 	    in->type == IB_SRQT_XRC)
205 		in->user_index = MLX5_IB_DEFAULT_UIDX;
206 
207 	return 0;
208 
209 err_in:
210 	kvfree(in->pas);
211 
212 err_buf:
213 	mlx5_buf_free(dev->mdev, &srq->buf);
214 
215 err_db:
216 	mlx5_db_free(dev->mdev, &srq->db);
217 	return err;
218 }
219 
220 static void destroy_srq_user(struct ib_pd *pd, struct mlx5_ib_srq *srq)
221 {
222 	mlx5_ib_db_unmap_user(to_mucontext(pd->uobject->context), &srq->db);
223 	ib_umem_release(srq->umem);
224 }
225 
226 
227 static void destroy_srq_kernel(struct mlx5_ib_dev *dev, struct mlx5_ib_srq *srq)
228 {
229 	kfree(srq->wrid);
230 	mlx5_buf_free(dev->mdev, &srq->buf);
231 	mlx5_db_free(dev->mdev, &srq->db);
232 }
233 
234 struct ib_srq *mlx5_ib_create_srq(struct ib_pd *pd,
235 				  struct ib_srq_init_attr *init_attr,
236 				  struct ib_udata *udata)
237 {
238 	struct mlx5_ib_dev *dev = to_mdev(pd->device);
239 	struct mlx5_ib_srq *srq;
240 	size_t desc_size;
241 	size_t buf_size;
242 	int err;
243 	struct mlx5_srq_attr in = {0};
244 	__u32 max_srq_wqes = 1 << MLX5_CAP_GEN(dev->mdev, log_max_srq_sz);
245 
246 	/* Sanity check SRQ size before proceeding */
247 	if (init_attr->attr.max_wr >= max_srq_wqes) {
248 		mlx5_ib_dbg(dev, "max_wr %d, cap %d\n",
249 			    init_attr->attr.max_wr,
250 			    max_srq_wqes);
251 		return ERR_PTR(-EINVAL);
252 	}
253 
254 	srq = kmalloc(sizeof(*srq), GFP_KERNEL);
255 	if (!srq)
256 		return ERR_PTR(-ENOMEM);
257 
258 	mutex_init(&srq->mutex);
259 	spin_lock_init(&srq->lock);
260 	srq->msrq.max    = roundup_pow_of_two(init_attr->attr.max_wr + 1);
261 	srq->msrq.max_gs = init_attr->attr.max_sge;
262 
263 	desc_size = sizeof(struct mlx5_wqe_srq_next_seg) +
264 		    srq->msrq.max_gs * sizeof(struct mlx5_wqe_data_seg);
265 	if (desc_size == 0 || srq->msrq.max_gs > desc_size) {
266 		err = -EINVAL;
267 		goto err_srq;
268 	}
269 	desc_size = roundup_pow_of_two(desc_size);
270 	desc_size = max_t(size_t, 32, desc_size);
271 	if (desc_size < sizeof(struct mlx5_wqe_srq_next_seg)) {
272 		err = -EINVAL;
273 		goto err_srq;
274 	}
275 	srq->msrq.max_avail_gather = (desc_size - sizeof(struct mlx5_wqe_srq_next_seg)) /
276 		sizeof(struct mlx5_wqe_data_seg);
277 	srq->msrq.wqe_shift = ilog2(desc_size);
278 	buf_size = srq->msrq.max * desc_size;
279 	if (buf_size < desc_size) {
280 		err = -EINVAL;
281 		goto err_srq;
282 	}
283 	in.type = init_attr->srq_type;
284 
285 	if (pd->uobject)
286 		err = create_srq_user(pd, srq, &in, udata, buf_size);
287 	else
288 		err = create_srq_kernel(dev, srq, &in, buf_size);
289 
290 	if (err || !in.pas) {
291 		mlx5_ib_warn(dev, "create srq %s failed, err %d\n",
292 			     pd->uobject ? "user" : "kernel", err);
293 		goto err_srq;
294 	}
295 
296 	in.log_size = ilog2(srq->msrq.max);
297 	in.wqe_shift = srq->msrq.wqe_shift - 4;
298 	if (srq->wq_sig)
299 		in.flags |= MLX5_SRQ_FLAG_WQ_SIG;
300 	if (init_attr->srq_type == IB_SRQT_XRC) {
301 		in.xrcd = to_mxrcd(init_attr->ext.xrc.xrcd)->xrcdn;
302 		in.cqn = to_mcq(init_attr->ext.xrc.cq)->mcq.cqn;
303 	} else if (init_attr->srq_type == IB_SRQT_BASIC) {
304 		in.xrcd = to_mxrcd(dev->devr.x0)->xrcdn;
305 		in.cqn = to_mcq(dev->devr.c0)->mcq.cqn;
306 	}
307 
308 	in.pd = to_mpd(pd)->pdn;
309 	in.db_record = srq->db.dma;
310 	err = mlx5_core_create_srq(dev->mdev, &srq->msrq, &in);
311 	kvfree(in.pas);
312 	if (err) {
313 		mlx5_ib_dbg(dev, "create SRQ failed, err %d\n", err);
314 		goto err_usr_kern_srq;
315 	}
316 
317 	mlx5_ib_dbg(dev, "create SRQ with srqn 0x%x\n", srq->msrq.srqn);
318 
319 	srq->msrq.event = mlx5_ib_srq_event;
320 	srq->ibsrq.ext.xrc.srq_num = srq->msrq.srqn;
321 
322 	if (pd->uobject)
323 		if (ib_copy_to_udata(udata, &srq->msrq.srqn, sizeof(__u32))) {
324 			mlx5_ib_dbg(dev, "copy to user failed\n");
325 			err = -EFAULT;
326 			goto err_core;
327 		}
328 
329 	init_attr->attr.max_wr = srq->msrq.max - 1;
330 
331 	return &srq->ibsrq;
332 
333 err_core:
334 	mlx5_core_destroy_srq(dev->mdev, &srq->msrq);
335 
336 err_usr_kern_srq:
337 	if (pd->uobject)
338 		destroy_srq_user(pd, srq);
339 	else
340 		destroy_srq_kernel(dev, srq);
341 
342 err_srq:
343 	kfree(srq);
344 
345 	return ERR_PTR(err);
346 }
347 
348 int mlx5_ib_modify_srq(struct ib_srq *ibsrq, struct ib_srq_attr *attr,
349 		       enum ib_srq_attr_mask attr_mask, struct ib_udata *udata)
350 {
351 	struct mlx5_ib_dev *dev = to_mdev(ibsrq->device);
352 	struct mlx5_ib_srq *srq = to_msrq(ibsrq);
353 	int ret;
354 
355 	/* We don't support resizing SRQs yet */
356 	if (attr_mask & IB_SRQ_MAX_WR)
357 		return -EINVAL;
358 
359 	if (attr_mask & IB_SRQ_LIMIT) {
360 		if (attr->srq_limit >= srq->msrq.max)
361 			return -EINVAL;
362 
363 		mutex_lock(&srq->mutex);
364 		ret = mlx5_core_arm_srq(dev->mdev, &srq->msrq, attr->srq_limit, 1);
365 		mutex_unlock(&srq->mutex);
366 
367 		if (ret)
368 			return ret;
369 	}
370 
371 	return 0;
372 }
373 
374 int mlx5_ib_query_srq(struct ib_srq *ibsrq, struct ib_srq_attr *srq_attr)
375 {
376 	struct mlx5_ib_dev *dev = to_mdev(ibsrq->device);
377 	struct mlx5_ib_srq *srq = to_msrq(ibsrq);
378 	int ret;
379 	struct mlx5_srq_attr *out;
380 
381 	out = kzalloc(sizeof(*out), GFP_KERNEL);
382 	if (!out)
383 		return -ENOMEM;
384 
385 	ret = mlx5_core_query_srq(dev->mdev, &srq->msrq, out);
386 	if (ret)
387 		goto out_box;
388 
389 	srq_attr->srq_limit = out->lwm;
390 	srq_attr->max_wr    = srq->msrq.max - 1;
391 	srq_attr->max_sge   = srq->msrq.max_gs;
392 
393 out_box:
394 	kfree(out);
395 	return ret;
396 }
397 
398 int mlx5_ib_destroy_srq(struct ib_srq *srq)
399 {
400 	struct mlx5_ib_dev *dev = to_mdev(srq->device);
401 	struct mlx5_ib_srq *msrq = to_msrq(srq);
402 
403 	mlx5_core_destroy_srq(dev->mdev, &msrq->msrq);
404 
405 	if (srq->uobject) {
406 		mlx5_ib_db_unmap_user(to_mucontext(srq->uobject->context), &msrq->db);
407 		ib_umem_release(msrq->umem);
408 	} else {
409 		destroy_srq_kernel(dev, msrq);
410 	}
411 
412 	kfree(srq);
413 	return 0;
414 }
415 
416 void mlx5_ib_free_srq_wqe(struct mlx5_ib_srq *srq, int wqe_index)
417 {
418 	struct mlx5_wqe_srq_next_seg *next;
419 
420 	/* always called with interrupts disabled. */
421 	spin_lock(&srq->lock);
422 
423 	next = get_wqe(srq, srq->tail);
424 	next->next_wqe_index = cpu_to_be16(wqe_index);
425 	srq->tail = wqe_index;
426 
427 	spin_unlock(&srq->lock);
428 }
429 
430 int mlx5_ib_post_srq_recv(struct ib_srq *ibsrq, struct ib_recv_wr *wr,
431 			  struct ib_recv_wr **bad_wr)
432 {
433 	struct mlx5_ib_srq *srq = to_msrq(ibsrq);
434 	struct mlx5_wqe_srq_next_seg *next;
435 	struct mlx5_wqe_data_seg *scat;
436 	struct mlx5_ib_dev *dev = to_mdev(ibsrq->device);
437 	struct mlx5_core_dev *mdev = dev->mdev;
438 	unsigned long flags;
439 	int err = 0;
440 	int nreq;
441 	int i;
442 
443 	spin_lock_irqsave(&srq->lock, flags);
444 
445 	if (mdev->state == MLX5_DEVICE_STATE_INTERNAL_ERROR) {
446 		err = -EIO;
447 		*bad_wr = wr;
448 		goto out;
449 	}
450 
451 	for (nreq = 0; wr; nreq++, wr = wr->next) {
452 		if (unlikely(wr->num_sge > srq->msrq.max_gs)) {
453 			err = -EINVAL;
454 			*bad_wr = wr;
455 			break;
456 		}
457 
458 		if (unlikely(srq->head == srq->tail)) {
459 			err = -ENOMEM;
460 			*bad_wr = wr;
461 			break;
462 		}
463 
464 		srq->wrid[srq->head] = wr->wr_id;
465 
466 		next      = get_wqe(srq, srq->head);
467 		srq->head = be16_to_cpu(next->next_wqe_index);
468 		scat      = (struct mlx5_wqe_data_seg *)(next + 1);
469 
470 		for (i = 0; i < wr->num_sge; i++) {
471 			scat[i].byte_count = cpu_to_be32(wr->sg_list[i].length);
472 			scat[i].lkey       = cpu_to_be32(wr->sg_list[i].lkey);
473 			scat[i].addr       = cpu_to_be64(wr->sg_list[i].addr);
474 		}
475 
476 		if (i < srq->msrq.max_avail_gather) {
477 			scat[i].byte_count = 0;
478 			scat[i].lkey       = cpu_to_be32(MLX5_INVALID_LKEY);
479 			scat[i].addr       = 0;
480 		}
481 	}
482 
483 	if (likely(nreq)) {
484 		srq->wqe_ctr += nreq;
485 
486 		/* Make sure that descriptors are written before
487 		 * doorbell record.
488 		 */
489 		wmb();
490 
491 		*srq->db.db = cpu_to_be32(srq->wqe_ctr);
492 	}
493 out:
494 	spin_unlock_irqrestore(&srq->lock, flags);
495 
496 	return err;
497 }
498