xref: /freebsd/sys/dev/mlx4/mlx4_ib/mlx4_ib_srq.c (revision 7ef62cebc2f965b0f640263e179276928885e33d)
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 <dev/mlx4/qp.h>
35 #include <dev/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 int mlx4_ib_create_srq(struct ib_srq *ib_srq,
73 		       struct ib_srq_init_attr *init_attr,
74 		       struct ib_udata *udata)
75 {
76 	struct mlx4_ib_dev *dev = to_mdev(ib_srq->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 = to_msrq(ib_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 -EINVAL;
93 
94 	mutex_init(&srq->mutex);
95 	spin_lock_init(&srq->lock);
96 	srq->msrq.max    = roundup_pow_of_two(init_attr->attr.max_wr + 1);
97 	srq->msrq.max_gs = init_attr->attr.max_sge;
98 
99 	desc_size = max(32UL,
100 			roundup_pow_of_two(sizeof (struct mlx4_wqe_srq_next_seg) +
101 					   srq->msrq.max_gs *
102 					   sizeof (struct mlx4_wqe_data_seg)));
103 	srq->msrq.wqe_shift = ilog2(desc_size);
104 
105 	buf_size = srq->msrq.max * desc_size;
106 
107 	if (udata) {
108 		struct mlx4_ib_create_srq ucmd;
109 
110 		if (ib_copy_from_udata(&ucmd, udata, sizeof(ucmd)))
111 			return -EFAULT;
112 
113 		srq->umem =
114 			ib_umem_get(&ucontext->ibucontext, ucmd.buf_addr, buf_size, 0, 0);
115 		if (IS_ERR(srq->umem))
116 			return PTR_ERR(srq->umem);
117 
118 		err = mlx4_mtt_init(dev->dev, ib_umem_page_count(srq->umem),
119 				    ilog2(srq->umem->page_size), &srq->mtt);
120 		if (err)
121 			goto err_buf;
122 
123 		err = mlx4_ib_umem_write_mtt(dev, &srq->mtt, srq->umem);
124 		if (err)
125 			goto err_mtt;
126 
127 		err = mlx4_ib_db_map_user(ucontext,
128 					  ucmd.db_addr, &srq->db);
129 		if (err)
130 			goto err_mtt;
131 	} else {
132 		err = mlx4_db_alloc(dev->dev, &srq->db, 0, GFP_KERNEL);
133 		if (err)
134 			return err;
135 
136 		*srq->db.db = 0;
137 
138 		if (mlx4_buf_alloc(dev->dev, buf_size, PAGE_SIZE * 2, &srq->buf,
139 				   GFP_KERNEL)) {
140 			err = -ENOMEM;
141 			goto err_db;
142 		}
143 
144 		srq->head    = 0;
145 		srq->tail    = srq->msrq.max - 1;
146 		srq->wqe_ctr = 0;
147 
148 		for (i = 0; i < srq->msrq.max; ++i) {
149 			next = get_wqe(srq, i);
150 			next->next_wqe_index =
151 				cpu_to_be16((i + 1) & (srq->msrq.max - 1));
152 
153 			for (scatter = (void *) (next + 1);
154 			     (void *) scatter < (void *) next + desc_size;
155 			     ++scatter)
156 				scatter->lkey = cpu_to_be32(MLX4_INVALID_LKEY);
157 		}
158 
159 		err = mlx4_mtt_init(dev->dev, srq->buf.npages, srq->buf.page_shift,
160 				    &srq->mtt);
161 		if (err)
162 			goto err_buf;
163 
164 		err = mlx4_buf_write_mtt(dev->dev, &srq->mtt, &srq->buf, GFP_KERNEL);
165 		if (err)
166 			goto err_mtt;
167 
168 		srq->wrid = kmalloc_array(srq->msrq.max, sizeof(u64),
169 					GFP_KERNEL | __GFP_NOWARN);
170 		if (!srq->wrid) {
171 			srq->wrid = __vmalloc(srq->msrq.max * sizeof(u64),
172 					      GFP_KERNEL, 0 /*PAGE_KERNEL*/);
173 			if (!srq->wrid) {
174 				err = -ENOMEM;
175 				goto err_mtt;
176 			}
177 		}
178 	}
179 
180 	cqn = (init_attr->srq_type == IB_SRQT_XRC) ?
181 		to_mcq(init_attr->ext.cq)->mcq.cqn : 0;
182 	xrcdn = (init_attr->srq_type == IB_SRQT_XRC) ?
183 		to_mxrcd(init_attr->ext.xrc.xrcd)->xrcdn :
184 		(u16) dev->dev->caps.reserved_xrcds;
185 	err = mlx4_srq_alloc(dev->dev, to_mpd(ib_srq->pd)->pdn, cqn, xrcdn,
186 			     &srq->mtt, srq->db.dma, &srq->msrq);
187 	if (err)
188 		goto err_wrid;
189 
190 	srq->msrq.event = mlx4_ib_srq_event;
191 	srq->ibsrq.ext.xrc.srq_num = srq->msrq.srqn;
192 
193 	if (udata)
194 		if (ib_copy_to_udata(udata, &srq->msrq.srqn, sizeof (__u32))) {
195 			err = -EFAULT;
196 			goto err_wrid;
197 		}
198 
199 	init_attr->attr.max_wr = srq->msrq.max - 1;
200 
201 	return 0;
202 
203 err_wrid:
204 	if (udata)
205 		mlx4_ib_db_unmap_user(ucontext, &srq->db);
206 	else
207 		kvfree(srq->wrid);
208 
209 err_mtt:
210 	mlx4_mtt_cleanup(dev->dev, &srq->mtt);
211 
212 err_buf:
213 	if (!srq->umem)
214 		mlx4_buf_free(dev->dev, buf_size, &srq->buf);
215 	ib_umem_release(srq->umem);
216 
217 err_db:
218 	if (!udata)
219 		mlx4_db_free(dev->dev, &srq->db);
220 
221 	return err;
222 }
223 
224 int mlx4_ib_modify_srq(struct ib_srq *ibsrq, struct ib_srq_attr *attr,
225 		       enum ib_srq_attr_mask attr_mask, struct ib_udata *udata)
226 {
227 	struct mlx4_ib_dev *dev = to_mdev(ibsrq->device);
228 	struct mlx4_ib_srq *srq = to_msrq(ibsrq);
229 	int ret;
230 
231 	/* We don't support resizing SRQs (yet?) */
232 	if (attr_mask & IB_SRQ_MAX_WR)
233 		return -EINVAL;
234 
235 	if (attr_mask & IB_SRQ_LIMIT) {
236 		if (attr->srq_limit >= srq->msrq.max)
237 			return -EINVAL;
238 
239 		mutex_lock(&srq->mutex);
240 		ret = mlx4_srq_arm(dev->dev, &srq->msrq, attr->srq_limit);
241 		mutex_unlock(&srq->mutex);
242 
243 		if (ret)
244 			return ret;
245 	}
246 
247 	return 0;
248 }
249 
250 int mlx4_ib_query_srq(struct ib_srq *ibsrq, struct ib_srq_attr *srq_attr)
251 {
252 	struct mlx4_ib_dev *dev = to_mdev(ibsrq->device);
253 	struct mlx4_ib_srq *srq = to_msrq(ibsrq);
254 	int ret;
255 	int limit_watermark;
256 
257 	ret = mlx4_srq_query(dev->dev, &srq->msrq, &limit_watermark);
258 	if (ret)
259 		return ret;
260 
261 	srq_attr->srq_limit = limit_watermark;
262 	srq_attr->max_wr    = srq->msrq.max - 1;
263 	srq_attr->max_sge   = srq->msrq.max_gs;
264 
265 	return 0;
266 }
267 
268 void mlx4_ib_destroy_srq(struct ib_srq *srq, struct ib_udata *udata)
269 {
270 	struct mlx4_ib_dev *dev = to_mdev(srq->device);
271 	struct mlx4_ib_srq *msrq = to_msrq(srq);
272 
273 	mlx4_srq_free(dev->dev, &msrq->msrq);
274 	mlx4_mtt_cleanup(dev->dev, &msrq->mtt);
275 
276 	if (udata) {
277 		mlx4_ib_db_unmap_user(
278 			rdma_udata_to_drv_context(
279 				udata,
280 				struct mlx4_ib_ucontext,
281 				ibucontext),
282 			&msrq->db);
283 	} else {
284 		kvfree(msrq->wrid);
285 		mlx4_buf_free(dev->dev, msrq->msrq.max << msrq->msrq.wqe_shift,
286 			      &msrq->buf);
287 		mlx4_db_free(dev->dev, &msrq->db);
288 	}
289 	ib_umem_release(msrq->umem);
290 }
291 
292 void mlx4_ib_free_srq_wqe(struct mlx4_ib_srq *srq, int wqe_index)
293 {
294 	struct mlx4_wqe_srq_next_seg *next;
295 
296 	/* always called with interrupts disabled. */
297 	spin_lock(&srq->lock);
298 
299 	next = get_wqe(srq, srq->tail);
300 	next->next_wqe_index = cpu_to_be16(wqe_index);
301 	srq->tail = wqe_index;
302 
303 	spin_unlock(&srq->lock);
304 }
305 
306 int mlx4_ib_post_srq_recv(struct ib_srq *ibsrq, const struct ib_recv_wr *wr,
307 			  const struct ib_recv_wr **bad_wr)
308 {
309 	struct mlx4_ib_srq *srq = to_msrq(ibsrq);
310 	struct mlx4_wqe_srq_next_seg *next;
311 	struct mlx4_wqe_data_seg *scat;
312 	unsigned long flags;
313 	int err = 0;
314 	int nreq;
315 	int i;
316 	struct mlx4_ib_dev *mdev = to_mdev(ibsrq->device);
317 
318 	spin_lock_irqsave(&srq->lock, flags);
319 	if (mdev->dev->persist->state & MLX4_DEVICE_STATE_INTERNAL_ERROR) {
320 		err = -EIO;
321 		*bad_wr = wr;
322 		nreq = 0;
323 		goto out;
324 	}
325 
326 	for (nreq = 0; wr; ++nreq, wr = wr->next) {
327 		if (unlikely(wr->num_sge > srq->msrq.max_gs)) {
328 			err = -EINVAL;
329 			*bad_wr = wr;
330 			break;
331 		}
332 
333 		if (unlikely(srq->head == srq->tail)) {
334 			err = -ENOMEM;
335 			*bad_wr = wr;
336 			break;
337 		}
338 
339 		srq->wrid[srq->head] = wr->wr_id;
340 
341 		next      = get_wqe(srq, srq->head);
342 		srq->head = be16_to_cpu(next->next_wqe_index);
343 		scat      = (struct mlx4_wqe_data_seg *) (next + 1);
344 
345 		for (i = 0; i < wr->num_sge; ++i) {
346 			scat[i].byte_count = cpu_to_be32(wr->sg_list[i].length);
347 			scat[i].lkey       = cpu_to_be32(wr->sg_list[i].lkey);
348 			scat[i].addr       = cpu_to_be64(wr->sg_list[i].addr);
349 		}
350 
351 		if (i < srq->msrq.max_gs) {
352 			scat[i].byte_count = 0;
353 			scat[i].lkey       = cpu_to_be32(MLX4_INVALID_LKEY);
354 			scat[i].addr       = 0;
355 		}
356 	}
357 
358 	if (likely(nreq)) {
359 		srq->wqe_ctr += nreq;
360 
361 		/*
362 		 * Make sure that descriptors are written before
363 		 * doorbell record.
364 		 */
365 		wmb();
366 
367 		*srq->db.db = cpu_to_be32(srq->wqe_ctr);
368 	}
369 out:
370 
371 	spin_unlock_irqrestore(&srq->lock, flags);
372 
373 	return err;
374 }
375