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