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