srq.c revision 219820
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
40static 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
45static 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
70struct ib_srq *mlx4_ib_create_xrc_srq(struct ib_pd *pd,
71				      struct ib_cq *xrc_cq,
72				      struct ib_xrcd *xrcd,
73				      struct ib_srq_init_attr *init_attr,
74				      struct ib_udata *udata)
75{
76	struct mlx4_ib_dev *dev = to_mdev(pd->device);
77	struct mlx4_ib_srq *srq;
78	struct mlx4_wqe_srq_next_seg *next;
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		mlx4_ib_dbg("a size param is out of range. "
90			    "max_wr = 0x%x, max_sge = 0x%x",
91			    init_attr->attr.max_wr, init_attr->attr.max_sge);
92		return ERR_PTR(-EINVAL);
93	}
94
95	srq = kzalloc(sizeof *srq, GFP_KERNEL);
96	if (!srq)
97		return ERR_PTR(-ENOMEM);
98
99	mutex_init(&srq->mutex);
100	spin_lock_init(&srq->lock);
101	srq->msrq.max    = roundup_pow_of_two(init_attr->attr.max_wr + 1);
102	srq->msrq.max_gs = init_attr->attr.max_sge;
103
104	desc_size = max(32UL,
105			roundup_pow_of_two(sizeof (struct mlx4_wqe_srq_next_seg) +
106					   srq->msrq.max_gs *
107					   sizeof (struct mlx4_wqe_data_seg)));
108	srq->msrq.wqe_shift = ilog2(desc_size);
109
110	buf_size = srq->msrq.max * desc_size;
111
112	if (pd->uobject) {
113		struct mlx4_ib_create_srq ucmd;
114
115		if (ib_copy_from_udata(&ucmd, udata, sizeof ucmd)) {
116			err = -EFAULT;
117			goto err_srq;
118		}
119
120		srq->umem = ib_umem_get(pd->uobject->context, ucmd.buf_addr,
121					buf_size, 0, 0);
122		if (IS_ERR(srq->umem)) {
123			err = PTR_ERR(srq->umem);
124			goto err_srq;
125		}
126
127		err = mlx4_mtt_init(dev->dev, ib_umem_page_count(srq->umem),
128				    ilog2(srq->umem->page_size), &srq->mtt);
129		if (err)
130			goto err_buf;
131
132		err = mlx4_ib_umem_write_mtt(dev, &srq->mtt, srq->umem);
133		if (err)
134			goto err_mtt;
135
136		err = mlx4_ib_db_map_user(to_mucontext(pd->uobject->context),
137					  ucmd.db_addr, &srq->db);
138		if (err)
139			goto err_mtt;
140	} else {
141		struct mlx4_wqe_data_seg *scatter;
142
143		err = mlx4_db_alloc(dev->dev, &srq->db, 0);
144		if (err)
145			goto err_srq;
146
147		*srq->db.db = 0;
148
149		if (mlx4_buf_alloc(dev->dev, buf_size, PAGE_SIZE * 2, &srq->buf)) {
150			err = -ENOMEM;
151			goto err_db;
152		}
153
154		srq->head    = 0;
155		srq->tail    = srq->msrq.max - 1;
156		srq->wqe_ctr = 0;
157
158		for (i = 0; i < srq->msrq.max; ++i) {
159			next = get_wqe(srq, i);
160			next->next_wqe_index =
161				cpu_to_be16((i + 1) & (srq->msrq.max - 1));
162
163			for (scatter = (void *) (next + 1);
164			     (void *) scatter < (void *) next + desc_size;
165			     ++scatter)
166				scatter->lkey = cpu_to_be32(MLX4_INVALID_LKEY);
167		}
168
169		err = mlx4_mtt_init(dev->dev, srq->buf.npages, srq->buf.page_shift,
170				    &srq->mtt);
171		if (err)
172			goto err_buf;
173
174		err = mlx4_buf_write_mtt(dev->dev, &srq->mtt, &srq->buf);
175		if (err)
176			goto err_mtt;
177
178		srq->wrid = kmalloc(srq->msrq.max * sizeof (u64), GFP_KERNEL);
179		if (!srq->wrid) {
180			err = -ENOMEM;
181			goto err_mtt;
182		}
183	}
184
185	cqn = xrc_cq ? (u32) (to_mcq(xrc_cq)->mcq.cqn) : 0;
186	xrcdn = xrcd ? (u16) (to_mxrcd(xrcd)->xrcdn) :
187		(u16) dev->dev->caps.reserved_xrcds;
188
189	err = mlx4_srq_alloc(dev->dev, to_mpd(pd)->pdn, cqn, xrcdn, &srq->mtt,
190			     srq->db.dma, &srq->msrq);
191	if (err)
192		goto err_wrid;
193
194	srq->msrq.event = mlx4_ib_srq_event;
195
196	if (pd->uobject) {
197		if (ib_copy_to_udata(udata, &srq->msrq.srqn, sizeof (__u32))) {
198			err = -EFAULT;
199			goto err_wrid;
200		}
201	} else
202		srq->ibsrq.xrc_srq_num = srq->msrq.srqn;
203
204	init_attr->attr.max_wr = srq->msrq.max - 1;
205
206	return &srq->ibsrq;
207
208err_wrid:
209	if (pd->uobject)
210		mlx4_ib_db_unmap_user(to_mucontext(pd->uobject->context), &srq->db);
211	else
212		kfree(srq->wrid);
213
214err_mtt:
215	mlx4_mtt_cleanup(dev->dev, &srq->mtt);
216
217err_buf:
218	if (pd->uobject)
219		ib_umem_release(srq->umem);
220	else
221		mlx4_buf_free(dev->dev, buf_size, &srq->buf);
222
223err_db:
224	if (!pd->uobject)
225		mlx4_db_free(dev->dev, &srq->db);
226
227err_srq:
228	kfree(srq);
229
230	return ERR_PTR(err);
231}
232
233int 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		mlx4_ib_dbg("resize not yet supported");
243		return -EINVAL;
244	}
245
246	if (attr_mask & IB_SRQ_LIMIT) {
247		if (attr->srq_limit >= srq->msrq.max){
248			mlx4_ib_dbg("limit (0x%x) too high", attr->srq_limit);
249			return -EINVAL;
250		}
251
252		mutex_lock(&srq->mutex);
253		ret = mlx4_srq_arm(dev->dev, &srq->msrq, attr->srq_limit);
254		mutex_unlock(&srq->mutex);
255
256		if (ret)
257			return ret;
258	}
259
260	return 0;
261}
262
263struct ib_srq *mlx4_ib_create_srq(struct ib_pd *pd,
264				  struct ib_srq_init_attr *init_attr,
265				  struct ib_udata *udata)
266{
267	return mlx4_ib_create_xrc_srq(pd, NULL, NULL, init_attr, udata);
268}
269
270int mlx4_ib_query_srq(struct ib_srq *ibsrq, struct ib_srq_attr *srq_attr)
271{
272	struct mlx4_ib_dev *dev = to_mdev(ibsrq->device);
273	struct mlx4_ib_srq *srq = to_msrq(ibsrq);
274	int ret;
275	int limit_watermark;
276
277	ret = mlx4_srq_query(dev->dev, &srq->msrq, &limit_watermark);
278	if (ret)
279		return ret;
280
281	srq_attr->srq_limit = limit_watermark;
282	srq_attr->max_wr    = srq->msrq.max - 1;
283	srq_attr->max_sge   = srq->msrq.max_gs;
284
285	return 0;
286}
287
288int mlx4_ib_destroy_srq(struct ib_srq *srq)
289{
290	struct mlx4_ib_dev *dev = to_mdev(srq->device);
291	struct mlx4_ib_srq *msrq = to_msrq(srq);
292	struct mlx4_ib_cq *cq;
293
294	mlx4_srq_invalidate(dev->dev, &msrq->msrq);
295
296	if (srq->xrc_cq && !srq->uobject) {
297		cq = to_mcq(srq->xrc_cq);
298		spin_lock_irq(&cq->lock);
299		__mlx4_ib_cq_clean(cq, -1, msrq);
300		mlx4_srq_remove(dev->dev, &msrq->msrq);
301		spin_unlock_irq(&cq->lock);
302	} else
303		mlx4_srq_remove(dev->dev, &msrq->msrq);
304
305	mlx4_srq_free(dev->dev, &msrq->msrq);
306	mlx4_mtt_cleanup(dev->dev, &msrq->mtt);
307
308	if (srq->uobject) {
309		mlx4_ib_db_unmap_user(to_mucontext(srq->uobject->context), &msrq->db);
310		ib_umem_release(msrq->umem);
311	} else {
312		kfree(msrq->wrid);
313		mlx4_buf_free(dev->dev, msrq->msrq.max << msrq->msrq.wqe_shift,
314			      &msrq->buf);
315		mlx4_db_free(dev->dev, &msrq->db);
316	}
317
318	kfree(msrq);
319
320	return 0;
321}
322
323void mlx4_ib_free_srq_wqe(struct mlx4_ib_srq *srq, int wqe_index)
324{
325	struct mlx4_wqe_srq_next_seg *next;
326
327	/* always called with interrupts disabled. */
328	spin_lock(&srq->lock);
329
330	next = get_wqe(srq, srq->tail);
331	next->next_wqe_index = cpu_to_be16(wqe_index);
332	srq->tail = wqe_index;
333
334	spin_unlock(&srq->lock);
335}
336
337int mlx4_ib_post_srq_recv(struct ib_srq *ibsrq, struct ib_recv_wr *wr,
338			  struct ib_recv_wr **bad_wr)
339{
340	struct mlx4_ib_srq *srq = to_msrq(ibsrq);
341	struct mlx4_wqe_srq_next_seg *next;
342	struct mlx4_wqe_data_seg *scat;
343	unsigned long flags;
344	int err = 0;
345	int nreq;
346	int i;
347
348	spin_lock_irqsave(&srq->lock, flags);
349
350	for (nreq = 0; wr; ++nreq, wr = wr->next) {
351		if (unlikely(wr->num_sge > srq->msrq.max_gs)) {
352			mlx4_ib_dbg("srq num 0x%x: num s/g entries too large (%d)",
353				    srq->msrq.srqn, wr->num_sge);
354			err = -EINVAL;
355			*bad_wr = wr;
356			break;
357		}
358
359		if (unlikely(srq->head == srq->tail)) {
360			mlx4_ib_dbg("srq num 0x%x: No entries available to post.",
361				    srq->msrq.srqn);
362			err = -ENOMEM;
363			*bad_wr = wr;
364			break;
365		}
366
367		srq->wrid[srq->head] = wr->wr_id;
368
369		next      = get_wqe(srq, srq->head);
370		srq->head = be16_to_cpu(next->next_wqe_index);
371		scat      = (struct mlx4_wqe_data_seg *) (next + 1);
372
373		for (i = 0; i < wr->num_sge; ++i) {
374			scat[i].byte_count = cpu_to_be32(wr->sg_list[i].length);
375			scat[i].lkey       = cpu_to_be32(wr->sg_list[i].lkey);
376			scat[i].addr       = cpu_to_be64(wr->sg_list[i].addr);
377		}
378
379		if (i < srq->msrq.max_gs) {
380			scat[i].byte_count = 0;
381			scat[i].lkey       = cpu_to_be32(MLX4_INVALID_LKEY);
382			scat[i].addr       = 0;
383		}
384	}
385
386	if (likely(nreq)) {
387		srq->wqe_ctr += nreq;
388
389		/*
390		 * Make sure that descriptors are written before
391		 * doorbell record.
392		 */
393		wmb();
394
395		*srq->db.db = cpu_to_be32(srq->wqe_ctr);
396	}
397
398	spin_unlock_irqrestore(&srq->lock, flags);
399
400	return err;
401}
402