xref: /freebsd/sys/dev/mlx5/mlx5_ib/mlx5_ib_srq.c (revision 5bf5ca772c6de2d53344a78cf461447cc322ccea)
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_create_srq_mbox_in **in,
72 			   struct ib_udata *udata, int buf_size, int *inlen,
73 			   int type)
74 {
75 	struct mlx5_ib_dev *dev = to_mdev(pd->device);
76 	struct mlx5_ib_create_srq ucmd = {};
77 	size_t ucmdlen;
78 	int err;
79 	int npages;
80 	int page_shift;
81 	int ncont;
82 	u32 offset;
83 	u32 uidx = MLX5_IB_DEFAULT_UIDX;
84 
85 	ucmdlen = min(udata->inlen, sizeof(ucmd));
86 
87 	if (ib_copy_from_udata(&ucmd, udata, ucmdlen)) {
88 		mlx5_ib_dbg(dev, "failed copy udata\n");
89 		return -EFAULT;
90 	}
91 
92 	if (ucmd.reserved0 || ucmd.reserved1)
93 		return -EINVAL;
94 
95 	if (udata->inlen > sizeof(ucmd) &&
96 	    !ib_is_udata_cleared(udata, sizeof(ucmd),
97 				 udata->inlen - sizeof(ucmd)))
98 		return -EINVAL;
99 
100 	if (type == IB_SRQT_XRC) {
101 		err = get_srq_user_index(to_mucontext(pd->uobject->context),
102 					 &ucmd, udata->inlen, &uidx);
103 		if (err)
104 			return err;
105 	}
106 
107 	srq->wq_sig = !!(ucmd.flags & MLX5_SRQ_FLAG_SIGNATURE);
108 
109 	srq->umem = ib_umem_get(pd->uobject->context, ucmd.buf_addr, buf_size,
110 				0, 0);
111 	if (IS_ERR(srq->umem)) {
112 		mlx5_ib_dbg(dev, "failed umem get, size %d\n", buf_size);
113 		err = PTR_ERR(srq->umem);
114 		return err;
115 	}
116 
117 	mlx5_ib_cont_pages(srq->umem, ucmd.buf_addr, &npages,
118 			   &page_shift, &ncont, NULL);
119 	err = mlx5_ib_get_buf_offset(ucmd.buf_addr, page_shift,
120 				     &offset);
121 	if (err) {
122 		mlx5_ib_warn(dev, "bad offset\n");
123 		goto err_umem;
124 	}
125 
126 	*inlen = sizeof(**in) + sizeof(*(*in)->pas) * ncont;
127 	*in = mlx5_vzalloc(*inlen);
128 	if (!(*in)) {
129 		err = -ENOMEM;
130 		goto err_umem;
131 	}
132 
133 	mlx5_ib_populate_pas(dev, srq->umem, page_shift, (*in)->pas, 0);
134 
135 	err = mlx5_ib_db_map_user(to_mucontext(pd->uobject->context),
136 				  ucmd.db_addr, &srq->db);
137 	if (err) {
138 		mlx5_ib_dbg(dev, "map doorbell failed\n");
139 		goto err_in;
140 	}
141 
142 	(*in)->ctx.log_pg_sz = page_shift - MLX5_ADAPTER_PAGE_SHIFT;
143 	(*in)->ctx.pgoff_cqn = cpu_to_be32(offset << 26);
144 
145 	if (MLX5_CAP_GEN(dev->mdev, cqe_version) == MLX5_CQE_VERSION_V1 &&
146 	    type == IB_SRQT_XRC) {
147 		void *xsrqc = MLX5_ADDR_OF(create_xrc_srq_in, *in,
148 					   xrc_srq_context_entry);
149 		MLX5_SET(xrc_srqc, xsrqc, user_index, uidx);
150 	}
151 
152 	return 0;
153 
154 err_in:
155 	kvfree(*in);
156 
157 err_umem:
158 	ib_umem_release(srq->umem);
159 
160 	return err;
161 }
162 
163 static int create_srq_kernel(struct mlx5_ib_dev *dev, struct mlx5_ib_srq *srq,
164 			     struct mlx5_create_srq_mbox_in **in, int buf_size,
165 			     int *inlen, int type)
166 {
167 	int err;
168 	int i;
169 	struct mlx5_wqe_srq_next_seg *next;
170 	int page_shift;
171 	int npages;
172 
173 	err = mlx5_db_alloc(dev->mdev, &srq->db);
174 	if (err) {
175 		mlx5_ib_warn(dev, "alloc dbell rec failed\n");
176 		return err;
177 	}
178 
179 	if (mlx5_buf_alloc(dev->mdev, buf_size, 2 * PAGE_SIZE, &srq->buf)) {
180 		mlx5_ib_dbg(dev, "buf alloc failed\n");
181 		err = -ENOMEM;
182 		goto err_db;
183 	}
184 	page_shift = srq->buf.page_shift;
185 
186 	srq->head    = 0;
187 	srq->tail    = srq->msrq.max - 1;
188 	srq->wqe_ctr = 0;
189 
190 	for (i = 0; i < srq->msrq.max; i++) {
191 		next = get_wqe(srq, i);
192 		next->next_wqe_index =
193 			cpu_to_be16((i + 1) & (srq->msrq.max - 1));
194 	}
195 
196 	npages = DIV_ROUND_UP(srq->buf.npages, 1 << (page_shift - PAGE_SHIFT));
197 	mlx5_ib_dbg(dev, "buf_size %d, page_shift %d, npages %d, calc npages %d\n",
198 		    buf_size, page_shift, srq->buf.npages, npages);
199 	*inlen = sizeof(**in) + sizeof(*(*in)->pas) * npages;
200 	*in = mlx5_vzalloc(*inlen);
201 	if (!*in) {
202 		err = -ENOMEM;
203 		goto err_buf;
204 	}
205 	mlx5_fill_page_array(&srq->buf, (*in)->pas);
206 
207 	srq->wrid = kmalloc(srq->msrq.max * sizeof(u64), GFP_KERNEL);
208 	if (!srq->wrid) {
209 		mlx5_ib_dbg(dev, "kmalloc failed %lu\n",
210 			    (unsigned long)(srq->msrq.max * sizeof(u64)));
211 		err = -ENOMEM;
212 		goto err_in;
213 	}
214 	srq->wq_sig = !!srq_signature;
215 
216 	(*in)->ctx.log_pg_sz = page_shift - MLX5_ADAPTER_PAGE_SHIFT;
217 
218 	if (MLX5_CAP_GEN(dev->mdev, cqe_version) == MLX5_CQE_VERSION_V1 &&
219 	    type == IB_SRQT_XRC) {
220 		void *xsrqc = MLX5_ADDR_OF(create_xrc_srq_in, *in,
221 					   xrc_srq_context_entry);
222 		MLX5_SET(xrc_srqc, xsrqc, user_index, MLX5_IB_DEFAULT_UIDX);
223 	}
224 
225 	return 0;
226 
227 err_in:
228 	kvfree(*in);
229 
230 err_buf:
231 	mlx5_buf_free(dev->mdev, &srq->buf);
232 
233 err_db:
234 	mlx5_db_free(dev->mdev, &srq->db);
235 	return err;
236 }
237 
238 static void destroy_srq_user(struct ib_pd *pd, struct mlx5_ib_srq *srq)
239 {
240 	mlx5_ib_db_unmap_user(to_mucontext(pd->uobject->context), &srq->db);
241 	ib_umem_release(srq->umem);
242 }
243 
244 
245 static void destroy_srq_kernel(struct mlx5_ib_dev *dev, struct mlx5_ib_srq *srq)
246 {
247 	kfree(srq->wrid);
248 	mlx5_buf_free(dev->mdev, &srq->buf);
249 	mlx5_db_free(dev->mdev, &srq->db);
250 }
251 
252 struct ib_srq *mlx5_ib_create_srq(struct ib_pd *pd,
253 				  struct ib_srq_init_attr *init_attr,
254 				  struct ib_udata *udata)
255 {
256 	struct mlx5_ib_dev *dev = to_mdev(pd->device);
257 	struct mlx5_ib_srq *srq;
258 	int desc_size;
259 	int buf_size;
260 	int err;
261 	struct mlx5_create_srq_mbox_in *uninitialized_var(in);
262 	int uninitialized_var(inlen);
263 	int is_xrc;
264 	u32 flgs, xrcdn;
265 	__u32 max_srq_wqes = 1 << MLX5_CAP_GEN(dev->mdev, log_max_srq_sz);
266 
267 	/* Sanity check SRQ size before proceeding */
268 	if (init_attr->attr.max_wr >= max_srq_wqes) {
269 		mlx5_ib_dbg(dev, "max_wr %d, cap %d\n",
270 			    init_attr->attr.max_wr,
271 			    max_srq_wqes);
272 		return ERR_PTR(-EINVAL);
273 	}
274 
275 	srq = kmalloc(sizeof(*srq), GFP_KERNEL);
276 	if (!srq)
277 		return ERR_PTR(-ENOMEM);
278 
279 	mutex_init(&srq->mutex);
280 	spin_lock_init(&srq->lock);
281 	srq->msrq.max    = roundup_pow_of_two(init_attr->attr.max_wr + 1);
282 	srq->msrq.max_gs = init_attr->attr.max_sge;
283 
284 	desc_size = sizeof(struct mlx5_wqe_srq_next_seg) +
285 		    srq->msrq.max_gs * sizeof(struct mlx5_wqe_data_seg);
286 	desc_size = roundup_pow_of_two(desc_size);
287 	desc_size = max_t(int, 32, desc_size);
288 	srq->msrq.max_avail_gather = (desc_size - sizeof(struct mlx5_wqe_srq_next_seg)) /
289 		sizeof(struct mlx5_wqe_data_seg);
290 	srq->msrq.wqe_shift = ilog2(desc_size);
291 	buf_size = srq->msrq.max * desc_size;
292 	mlx5_ib_dbg(dev, "desc_size 0x%x, req wr 0x%x, srq size 0x%x, max_gs 0x%x, max_avail_gather 0x%x\n",
293 		    desc_size, init_attr->attr.max_wr, srq->msrq.max, srq->msrq.max_gs,
294 		    srq->msrq.max_avail_gather);
295 
296 	if (pd->uobject)
297 		err = create_srq_user(pd, srq, &in, udata, buf_size, &inlen, init_attr->srq_type);
298 	else
299 		err = create_srq_kernel(dev, srq, &in, buf_size, &inlen, init_attr->srq_type);
300 
301 	if (err) {
302 		mlx5_ib_warn(dev, "create srq %s failed, err %d\n",
303 			     pd->uobject ? "user" : "kernel", err);
304 		goto err_srq;
305 	}
306 
307 	is_xrc = (init_attr->srq_type == IB_SRQT_XRC);
308 	in->ctx.state_log_sz = ilog2(srq->msrq.max);
309 	flgs = ((srq->msrq.wqe_shift - 4) | (is_xrc << 5) | (srq->wq_sig << 7)) << 24;
310 	xrcdn = 0;
311 	if (is_xrc) {
312 		xrcdn = to_mxrcd(init_attr->ext.xrc.xrcd)->xrcdn;
313 		in->ctx.pgoff_cqn |= cpu_to_be32(to_mcq(init_attr->ext.xrc.cq)->mcq.cqn);
314 	} else if (init_attr->srq_type == IB_SRQT_BASIC) {
315 		xrcdn = to_mxrcd(dev->devr.x0)->xrcdn;
316 		in->ctx.pgoff_cqn |= cpu_to_be32(to_mcq(dev->devr.c0)->mcq.cqn);
317 	}
318 
319 	in->ctx.flags_xrcd = cpu_to_be32((flgs & 0xFF000000) | (xrcdn & 0xFFFFFF));
320 
321 	in->ctx.pd = cpu_to_be32(to_mpd(pd)->pdn);
322 	in->ctx.db_record = cpu_to_be64(srq->db.dma);
323 	err = mlx5_core_create_srq(dev->mdev, &srq->msrq, in, inlen, is_xrc);
324 	kvfree(in);
325 	if (err) {
326 		mlx5_ib_dbg(dev, "create SRQ failed, err %d\n", err);
327 		goto err_usr_kern_srq;
328 	}
329 
330 	mlx5_ib_dbg(dev, "create SRQ with srqn 0x%x\n", srq->msrq.srqn);
331 
332 	srq->msrq.event = mlx5_ib_srq_event;
333 	srq->ibsrq.ext.xrc.srq_num = srq->msrq.srqn;
334 
335 	if (pd->uobject)
336 		if (ib_copy_to_udata(udata, &srq->msrq.srqn, sizeof(__u32))) {
337 			mlx5_ib_dbg(dev, "copy to user failed\n");
338 			err = -EFAULT;
339 			goto err_core;
340 		}
341 
342 	init_attr->attr.max_wr = srq->msrq.max - 1;
343 
344 	return &srq->ibsrq;
345 
346 err_core:
347 	mlx5_core_destroy_srq(dev->mdev, &srq->msrq);
348 
349 err_usr_kern_srq:
350 	if (pd->uobject)
351 		destroy_srq_user(pd, srq);
352 	else
353 		destroy_srq_kernel(dev, srq);
354 
355 err_srq:
356 	kfree(srq);
357 
358 	return ERR_PTR(err);
359 }
360 
361 int mlx5_ib_modify_srq(struct ib_srq *ibsrq, struct ib_srq_attr *attr,
362 		       enum ib_srq_attr_mask attr_mask, struct ib_udata *udata)
363 {
364 	struct mlx5_ib_dev *dev = to_mdev(ibsrq->device);
365 	struct mlx5_ib_srq *srq = to_msrq(ibsrq);
366 	int ret;
367 
368 	/* We don't support resizing SRQs yet */
369 	if (attr_mask & IB_SRQ_MAX_WR)
370 		return -EINVAL;
371 
372 	if (attr_mask & IB_SRQ_LIMIT) {
373 		if (attr->srq_limit >= srq->msrq.max)
374 			return -EINVAL;
375 
376 		mutex_lock(&srq->mutex);
377 		ret = mlx5_core_arm_srq(dev->mdev, &srq->msrq, attr->srq_limit, 1);
378 		mutex_unlock(&srq->mutex);
379 
380 		if (ret)
381 			return ret;
382 	}
383 
384 	return 0;
385 }
386 
387 int mlx5_ib_query_srq(struct ib_srq *ibsrq, struct ib_srq_attr *srq_attr)
388 {
389 	struct mlx5_ib_dev *dev = to_mdev(ibsrq->device);
390 	struct mlx5_ib_srq *srq = to_msrq(ibsrq);
391 	int ret;
392 	struct mlx5_query_srq_mbox_out *out;
393 
394 	out = kzalloc(sizeof(*out), GFP_KERNEL);
395 	if (!out)
396 		return -ENOMEM;
397 
398 	ret = mlx5_core_query_srq(dev->mdev, &srq->msrq, out);
399 	if (ret)
400 		goto out_box;
401 
402 	srq_attr->srq_limit = be16_to_cpu(out->ctx.lwm);
403 	srq_attr->max_wr    = srq->msrq.max - 1;
404 	srq_attr->max_sge   = srq->msrq.max_gs;
405 
406 out_box:
407 	kfree(out);
408 	return ret;
409 }
410 
411 int mlx5_ib_destroy_srq(struct ib_srq *srq)
412 {
413 	struct mlx5_ib_dev *dev = to_mdev(srq->device);
414 	struct mlx5_ib_srq *msrq = to_msrq(srq);
415 
416 	mlx5_core_destroy_srq(dev->mdev, &msrq->msrq);
417 
418 	if (srq->uobject) {
419 		mlx5_ib_db_unmap_user(to_mucontext(srq->uobject->context), &msrq->db);
420 		ib_umem_release(msrq->umem);
421 	} else {
422 		destroy_srq_kernel(dev, msrq);
423 	}
424 
425 	kfree(srq);
426 	return 0;
427 }
428 
429 void mlx5_ib_free_srq_wqe(struct mlx5_ib_srq *srq, int wqe_index)
430 {
431 	struct mlx5_wqe_srq_next_seg *next;
432 
433 	/* always called with interrupts disabled. */
434 	spin_lock(&srq->lock);
435 
436 	next = get_wqe(srq, srq->tail);
437 	next->next_wqe_index = cpu_to_be16(wqe_index);
438 	srq->tail = wqe_index;
439 
440 	spin_unlock(&srq->lock);
441 }
442 
443 int mlx5_ib_post_srq_recv(struct ib_srq *ibsrq, struct ib_recv_wr *wr,
444 			  struct ib_recv_wr **bad_wr)
445 {
446 	struct mlx5_ib_srq *srq = to_msrq(ibsrq);
447 	struct mlx5_wqe_srq_next_seg *next;
448 	struct mlx5_wqe_data_seg *scat;
449 	struct mlx5_ib_dev *dev = to_mdev(ibsrq->device);
450 	struct mlx5_core_dev *mdev = dev->mdev;
451 	unsigned long flags;
452 	int err = 0;
453 	int nreq;
454 	int i;
455 
456 	spin_lock_irqsave(&srq->lock, flags);
457 
458 	if (mdev->state == MLX5_DEVICE_STATE_INTERNAL_ERROR) {
459 		err = -EIO;
460 		*bad_wr = wr;
461 		goto out;
462 	}
463 
464 	for (nreq = 0; wr; nreq++, wr = wr->next) {
465 		if (unlikely(wr->num_sge > srq->msrq.max_gs)) {
466 			err = -EINVAL;
467 			*bad_wr = wr;
468 			break;
469 		}
470 
471 		if (unlikely(srq->head == srq->tail)) {
472 			err = -ENOMEM;
473 			*bad_wr = wr;
474 			break;
475 		}
476 
477 		srq->wrid[srq->head] = wr->wr_id;
478 
479 		next      = get_wqe(srq, srq->head);
480 		srq->head = be16_to_cpu(next->next_wqe_index);
481 		scat      = (struct mlx5_wqe_data_seg *)(next + 1);
482 
483 		for (i = 0; i < wr->num_sge; i++) {
484 			scat[i].byte_count = cpu_to_be32(wr->sg_list[i].length);
485 			scat[i].lkey       = cpu_to_be32(wr->sg_list[i].lkey);
486 			scat[i].addr       = cpu_to_be64(wr->sg_list[i].addr);
487 		}
488 
489 		if (i < srq->msrq.max_avail_gather) {
490 			scat[i].byte_count = 0;
491 			scat[i].lkey       = cpu_to_be32(MLX5_INVALID_LKEY);
492 			scat[i].addr       = 0;
493 		}
494 	}
495 
496 	if (likely(nreq)) {
497 		srq->wqe_ctr += nreq;
498 
499 		/* Make sure that descriptors are written before
500 		 * doorbell record.
501 		 */
502 		wmb();
503 
504 		*srq->db.db = cpu_to_be32(srq->wqe_ctr);
505 	}
506 out:
507 	spin_unlock_irqrestore(&srq->lock, flags);
508 
509 	return err;
510 }
511