2 * Copyright (c) 2007 Cisco Systems, Inc. All rights reserved.
3 * Copyright (c) 2007, 2008 Mellanox Technologies. All rights reserved.
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:
11 * Redistribution and use in source and binary forms, with or
12 * without modification, are permitted provided that the following
15 * - Redistributions of source code must retain the above
16 * copyright notice, this list of conditions and the following
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.
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
34 #include <linux/mlx4/qp.h>
35 #include <linux/mlx4/srq.h>
36 #include <linux/slab.h>
39 #include <rdma/mlx4-abi.h>
40 #include <rdma/uverbs_ioctl.h>
42 static void *get_wqe(struct mlx4_ib_srq *srq, int n)
44 return mlx4_buf_offset(&srq->buf, n << srq->msrq.wqe_shift);
47 static void mlx4_ib_srq_event(struct mlx4_srq *srq, enum mlx4_event type)
49 struct ib_event event;
50 struct ib_srq *ibsrq = &to_mibsrq(srq)->ibsrq;
52 if (ibsrq->event_handler) {
53 event.device = ibsrq->device;
54 event.element.srq = ibsrq;
56 case MLX4_EVENT_TYPE_SRQ_LIMIT:
57 event.event = IB_EVENT_SRQ_LIMIT_REACHED;
59 case MLX4_EVENT_TYPE_SRQ_CATAS_ERROR:
60 event.event = IB_EVENT_SRQ_ERR;
63 pr_warn("Unexpected event type %d "
64 "on SRQ %06x\n", type, srq->srqn);
68 ibsrq->event_handler(&event, ibsrq->srq_context);
72 int mlx4_ib_create_srq(struct ib_srq *ib_srq,
73 struct ib_srq_init_attr *init_attr,
74 struct ib_udata *udata)
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;
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)
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;
100 roundup_pow_of_two(sizeof (struct mlx4_wqe_srq_next_seg) +
102 sizeof (struct mlx4_wqe_data_seg)));
103 srq->msrq.wqe_shift = ilog2(desc_size);
105 buf_size = srq->msrq.max * desc_size;
108 struct mlx4_ib_create_srq ucmd;
110 if (ib_copy_from_udata(&ucmd, udata, sizeof(ucmd)))
114 ib_umem_get(ib_srq->device, ucmd.buf_addr, buf_size, 0);
115 if (IS_ERR(srq->umem))
116 return PTR_ERR(srq->umem);
119 dev->dev, ib_umem_num_dma_blocks(srq->umem, PAGE_SIZE),
120 PAGE_SHIFT, &srq->mtt);
124 err = mlx4_ib_umem_write_mtt(dev, &srq->mtt, srq->umem);
128 err = mlx4_ib_db_map_user(udata, ucmd.db_addr, &srq->db);
132 err = mlx4_db_alloc(dev->dev, &srq->db, 0);
138 if (mlx4_buf_alloc(dev->dev, buf_size, PAGE_SIZE * 2,
145 srq->tail = srq->msrq.max - 1;
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));
153 for (scatter = (void *) (next + 1);
154 (void *) scatter < (void *) next + desc_size;
156 scatter->lkey = cpu_to_be32(MLX4_INVALID_LKEY);
159 err = mlx4_mtt_init(dev->dev, srq->buf.npages, srq->buf.page_shift,
164 err = mlx4_buf_write_mtt(dev->dev, &srq->mtt, &srq->buf);
168 srq->wrid = kvmalloc_array(srq->msrq.max,
169 sizeof(u64), GFP_KERNEL);
176 cqn = ib_srq_has_cq(init_attr->srq_type) ?
177 to_mcq(init_attr->ext.cq)->mcq.cqn : 0;
178 xrcdn = (init_attr->srq_type == IB_SRQT_XRC) ?
179 to_mxrcd(init_attr->ext.xrc.xrcd)->xrcdn :
180 (u16) dev->dev->caps.reserved_xrcds;
181 err = mlx4_srq_alloc(dev->dev, to_mpd(ib_srq->pd)->pdn, cqn, xrcdn,
182 &srq->mtt, srq->db.dma, &srq->msrq);
186 srq->msrq.event = mlx4_ib_srq_event;
187 srq->ibsrq.ext.xrc.srq_num = srq->msrq.srqn;
190 if (ib_copy_to_udata(udata, &srq->msrq.srqn, sizeof (__u32))) {
195 init_attr->attr.max_wr = srq->msrq.max - 1;
201 mlx4_ib_db_unmap_user(ucontext, &srq->db);
206 mlx4_mtt_cleanup(dev->dev, &srq->mtt);
210 mlx4_buf_free(dev->dev, buf_size, &srq->buf);
211 ib_umem_release(srq->umem);
215 mlx4_db_free(dev->dev, &srq->db);
220 int mlx4_ib_modify_srq(struct ib_srq *ibsrq, struct ib_srq_attr *attr,
221 enum ib_srq_attr_mask attr_mask, struct ib_udata *udata)
223 struct mlx4_ib_dev *dev = to_mdev(ibsrq->device);
224 struct mlx4_ib_srq *srq = to_msrq(ibsrq);
227 /* We don't support resizing SRQs (yet?) */
228 if (attr_mask & IB_SRQ_MAX_WR)
231 if (attr_mask & IB_SRQ_LIMIT) {
232 if (attr->srq_limit >= srq->msrq.max)
235 mutex_lock(&srq->mutex);
236 ret = mlx4_srq_arm(dev->dev, &srq->msrq, attr->srq_limit);
237 mutex_unlock(&srq->mutex);
246 int mlx4_ib_query_srq(struct ib_srq *ibsrq, struct ib_srq_attr *srq_attr)
248 struct mlx4_ib_dev *dev = to_mdev(ibsrq->device);
249 struct mlx4_ib_srq *srq = to_msrq(ibsrq);
253 ret = mlx4_srq_query(dev->dev, &srq->msrq, &limit_watermark);
257 srq_attr->srq_limit = limit_watermark;
258 srq_attr->max_wr = srq->msrq.max - 1;
259 srq_attr->max_sge = srq->msrq.max_gs;
264 int mlx4_ib_destroy_srq(struct ib_srq *srq, struct ib_udata *udata)
266 struct mlx4_ib_dev *dev = to_mdev(srq->device);
267 struct mlx4_ib_srq *msrq = to_msrq(srq);
269 mlx4_srq_free(dev->dev, &msrq->msrq);
270 mlx4_mtt_cleanup(dev->dev, &msrq->mtt);
273 mlx4_ib_db_unmap_user(
274 rdma_udata_to_drv_context(
276 struct mlx4_ib_ucontext,
281 mlx4_buf_free(dev->dev, msrq->msrq.max << msrq->msrq.wqe_shift,
283 mlx4_db_free(dev->dev, &msrq->db);
285 ib_umem_release(msrq->umem);
289 void mlx4_ib_free_srq_wqe(struct mlx4_ib_srq *srq, int wqe_index)
291 struct mlx4_wqe_srq_next_seg *next;
293 /* always called with interrupts disabled. */
294 spin_lock(&srq->lock);
296 next = get_wqe(srq, srq->tail);
297 next->next_wqe_index = cpu_to_be16(wqe_index);
298 srq->tail = wqe_index;
300 spin_unlock(&srq->lock);
303 int mlx4_ib_post_srq_recv(struct ib_srq *ibsrq, const struct ib_recv_wr *wr,
304 const struct ib_recv_wr **bad_wr)
306 struct mlx4_ib_srq *srq = to_msrq(ibsrq);
307 struct mlx4_wqe_srq_next_seg *next;
308 struct mlx4_wqe_data_seg *scat;
313 struct mlx4_ib_dev *mdev = to_mdev(ibsrq->device);
315 spin_lock_irqsave(&srq->lock, flags);
316 if (mdev->dev->persist->state & MLX4_DEVICE_STATE_INTERNAL_ERROR) {
323 for (nreq = 0; wr; ++nreq, wr = wr->next) {
324 if (unlikely(wr->num_sge > srq->msrq.max_gs)) {
330 if (unlikely(srq->head == srq->tail)) {
336 srq->wrid[srq->head] = wr->wr_id;
338 next = get_wqe(srq, srq->head);
339 srq->head = be16_to_cpu(next->next_wqe_index);
340 scat = (struct mlx4_wqe_data_seg *) (next + 1);
342 for (i = 0; i < wr->num_sge; ++i) {
343 scat[i].byte_count = cpu_to_be32(wr->sg_list[i].length);
344 scat[i].lkey = cpu_to_be32(wr->sg_list[i].lkey);
345 scat[i].addr = cpu_to_be64(wr->sg_list[i].addr);
348 if (i < srq->msrq.max_gs) {
349 scat[i].byte_count = 0;
350 scat[i].lkey = cpu_to_be32(MLX4_INVALID_LKEY);
356 srq->wqe_ctr += nreq;
359 * Make sure that descriptors are written before
364 *srq->db.db = cpu_to_be32(srq->wqe_ctr);
368 spin_unlock_irqrestore(&srq->lock, flags);