2 * Copyright (c) 2013-2015, Mellanox Technologies. All rights reserved.
4 * This software is available to you under a choice of one of two
5 * licenses. You may choose to be licensed under the terms of the GNU
6 * General Public License (GPL) Version 2, available from the file
7 * COPYING in the main directory of this source tree, or the
8 * OpenIB.org BSD license below:
10 * Redistribution and use in source and binary forms, with or
11 * without modification, are permitted provided that the following
14 * - Redistributions of source code must retain the above
15 * copyright notice, this list of conditions and the following
18 * - Redistributions in binary form must reproduce the above
19 * copyright notice, this list of conditions and the following
20 * disclaimer in the documentation and/or other materials
21 * provided with the distribution.
23 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
33 #include <linux/module.h>
34 #include <linux/mlx5/qp.h>
35 #include <linux/mlx5/srq.h>
36 #include <linux/slab.h>
37 #include <rdma/ib_umem.h>
38 #include <rdma/ib_user_verbs.h>
42 /* not supported currently */
43 static int srq_signature;
45 static void *get_wqe(struct mlx5_ib_srq *srq, int n)
47 return mlx5_buf_offset(&srq->buf, n << srq->msrq.wqe_shift);
50 static void mlx5_ib_srq_event(struct mlx5_core_srq *srq, enum mlx5_event type)
52 struct ib_event event;
53 struct ib_srq *ibsrq = &to_mibsrq(srq)->ibsrq;
55 if (ibsrq->event_handler) {
56 event.device = ibsrq->device;
57 event.element.srq = ibsrq;
59 case MLX5_EVENT_TYPE_SRQ_RQ_LIMIT:
60 event.event = IB_EVENT_SRQ_LIMIT_REACHED;
62 case MLX5_EVENT_TYPE_SRQ_CATAS_ERROR:
63 event.event = IB_EVENT_SRQ_ERR;
66 pr_warn("mlx5_ib: Unexpected event type %d on SRQ %06x\n",
71 ibsrq->event_handler(&event, ibsrq->srq_context);
75 static int create_srq_user(struct ib_pd *pd, struct mlx5_ib_srq *srq,
76 struct mlx5_srq_attr *in,
77 struct ib_udata *udata, int buf_size)
79 struct mlx5_ib_dev *dev = to_mdev(pd->device);
80 struct mlx5_ib_create_srq ucmd = {};
87 u32 uidx = MLX5_IB_DEFAULT_UIDX;
89 ucmdlen = min(udata->inlen, sizeof(ucmd));
91 if (ib_copy_from_udata(&ucmd, udata, ucmdlen)) {
92 mlx5_ib_dbg(dev, "failed copy udata\n");
96 if (ucmd.reserved0 || ucmd.reserved1)
99 if (udata->inlen > sizeof(ucmd) &&
100 !ib_is_udata_cleared(udata, sizeof(ucmd),
101 udata->inlen - sizeof(ucmd)))
104 if (in->type == IB_SRQT_XRC) {
105 err = get_srq_user_index(to_mucontext(pd->uobject->context),
106 &ucmd, udata->inlen, &uidx);
111 srq->wq_sig = !!(ucmd.flags & MLX5_SRQ_FLAG_SIGNATURE);
113 srq->umem = ib_umem_get(pd->uobject->context, ucmd.buf_addr, buf_size,
115 if (IS_ERR(srq->umem)) {
116 mlx5_ib_dbg(dev, "failed umem get, size %d\n", buf_size);
117 err = PTR_ERR(srq->umem);
121 mlx5_ib_cont_pages(srq->umem, ucmd.buf_addr, &npages,
122 &page_shift, &ncont, NULL);
123 err = mlx5_ib_get_buf_offset(ucmd.buf_addr, page_shift,
126 mlx5_ib_warn(dev, "bad offset\n");
130 in->pas = mlx5_vzalloc(sizeof(*in->pas) * ncont);
136 mlx5_ib_populate_pas(dev, srq->umem, page_shift, in->pas, 0);
138 err = mlx5_ib_db_map_user(to_mucontext(pd->uobject->context),
139 ucmd.db_addr, &srq->db);
141 mlx5_ib_dbg(dev, "map doorbell failed\n");
145 in->log_page_size = page_shift - MLX5_ADAPTER_PAGE_SHIFT;
146 in->page_offset = offset;
147 if (MLX5_CAP_GEN(dev->mdev, cqe_version) == MLX5_CQE_VERSION_V1 &&
148 in->type == IB_SRQT_XRC)
149 in->user_index = uidx;
157 ib_umem_release(srq->umem);
162 static int create_srq_kernel(struct mlx5_ib_dev *dev, struct mlx5_ib_srq *srq,
163 struct mlx5_srq_attr *in, int buf_size)
167 struct mlx5_wqe_srq_next_seg *next;
169 err = mlx5_db_alloc(dev->mdev, &srq->db);
171 mlx5_ib_warn(dev, "alloc dbell rec failed\n");
175 if (mlx5_buf_alloc(dev->mdev, buf_size, &srq->buf)) {
176 mlx5_ib_dbg(dev, "buf alloc failed\n");
182 srq->tail = srq->msrq.max - 1;
185 for (i = 0; i < srq->msrq.max; i++) {
186 next = get_wqe(srq, i);
187 next->next_wqe_index =
188 cpu_to_be16((i + 1) & (srq->msrq.max - 1));
191 mlx5_ib_dbg(dev, "srq->buf.page_shift = %d\n", srq->buf.page_shift);
192 in->pas = mlx5_vzalloc(sizeof(*in->pas) * srq->buf.npages);
197 mlx5_fill_page_array(&srq->buf, in->pas);
199 srq->wrid = kmalloc(srq->msrq.max * sizeof(u64), GFP_KERNEL);
201 mlx5_ib_dbg(dev, "kmalloc failed %lu\n",
202 (unsigned long)(srq->msrq.max * sizeof(u64)));
206 srq->wq_sig = !!srq_signature;
208 in->log_page_size = srq->buf.page_shift - MLX5_ADAPTER_PAGE_SHIFT;
209 if (MLX5_CAP_GEN(dev->mdev, cqe_version) == MLX5_CQE_VERSION_V1 &&
210 in->type == IB_SRQT_XRC)
211 in->user_index = MLX5_IB_DEFAULT_UIDX;
219 mlx5_buf_free(dev->mdev, &srq->buf);
222 mlx5_db_free(dev->mdev, &srq->db);
226 static void destroy_srq_user(struct ib_pd *pd, struct mlx5_ib_srq *srq)
228 mlx5_ib_db_unmap_user(to_mucontext(pd->uobject->context), &srq->db);
229 ib_umem_release(srq->umem);
233 static void destroy_srq_kernel(struct mlx5_ib_dev *dev, struct mlx5_ib_srq *srq)
236 mlx5_buf_free(dev->mdev, &srq->buf);
237 mlx5_db_free(dev->mdev, &srq->db);
240 struct ib_srq *mlx5_ib_create_srq(struct ib_pd *pd,
241 struct ib_srq_init_attr *init_attr,
242 struct ib_udata *udata)
244 struct mlx5_ib_dev *dev = to_mdev(pd->device);
245 struct mlx5_ib_srq *srq;
249 struct mlx5_srq_attr in = {0};
250 __u32 max_srq_wqes = 1 << MLX5_CAP_GEN(dev->mdev, log_max_srq_sz);
252 /* Sanity check SRQ size before proceeding */
253 if (init_attr->attr.max_wr >= max_srq_wqes) {
254 mlx5_ib_dbg(dev, "max_wr %d, cap %d\n",
255 init_attr->attr.max_wr,
257 return ERR_PTR(-EINVAL);
260 srq = kmalloc(sizeof(*srq), GFP_KERNEL);
262 return ERR_PTR(-ENOMEM);
264 mutex_init(&srq->mutex);
265 spin_lock_init(&srq->lock);
266 srq->msrq.max = roundup_pow_of_two(init_attr->attr.max_wr + 1);
267 srq->msrq.max_gs = init_attr->attr.max_sge;
269 desc_size = sizeof(struct mlx5_wqe_srq_next_seg) +
270 srq->msrq.max_gs * sizeof(struct mlx5_wqe_data_seg);
271 if (desc_size == 0 || srq->msrq.max_gs > desc_size) {
275 desc_size = roundup_pow_of_two(desc_size);
276 desc_size = max_t(size_t, 32, desc_size);
277 if (desc_size < sizeof(struct mlx5_wqe_srq_next_seg)) {
281 srq->msrq.max_avail_gather = (desc_size - sizeof(struct mlx5_wqe_srq_next_seg)) /
282 sizeof(struct mlx5_wqe_data_seg);
283 srq->msrq.wqe_shift = ilog2(desc_size);
284 buf_size = srq->msrq.max * desc_size;
285 if (buf_size < desc_size) {
289 in.type = init_attr->srq_type;
292 err = create_srq_user(pd, srq, &in, udata, buf_size);
294 err = create_srq_kernel(dev, srq, &in, buf_size);
297 mlx5_ib_warn(dev, "create srq %s failed, err %d\n",
298 pd->uobject ? "user" : "kernel", err);
302 in.log_size = ilog2(srq->msrq.max);
303 in.wqe_shift = srq->msrq.wqe_shift - 4;
305 in.flags |= MLX5_SRQ_FLAG_WQ_SIG;
306 if (init_attr->srq_type == IB_SRQT_XRC) {
307 in.xrcd = to_mxrcd(init_attr->ext.xrc.xrcd)->xrcdn;
308 in.cqn = to_mcq(init_attr->ext.xrc.cq)->mcq.cqn;
309 } else if (init_attr->srq_type == IB_SRQT_BASIC) {
310 in.xrcd = to_mxrcd(dev->devr.x0)->xrcdn;
311 in.cqn = to_mcq(dev->devr.c0)->mcq.cqn;
314 in.pd = to_mpd(pd)->pdn;
315 in.db_record = srq->db.dma;
316 err = mlx5_core_create_srq(dev->mdev, &srq->msrq, &in);
319 mlx5_ib_dbg(dev, "create SRQ failed, err %d\n", err);
320 goto err_usr_kern_srq;
323 mlx5_ib_dbg(dev, "create SRQ with srqn 0x%x\n", srq->msrq.srqn);
325 srq->msrq.event = mlx5_ib_srq_event;
326 srq->ibsrq.ext.xrc.srq_num = srq->msrq.srqn;
329 if (ib_copy_to_udata(udata, &srq->msrq.srqn, sizeof(__u32))) {
330 mlx5_ib_dbg(dev, "copy to user failed\n");
335 init_attr->attr.max_wr = srq->msrq.max - 1;
340 mlx5_core_destroy_srq(dev->mdev, &srq->msrq);
344 destroy_srq_user(pd, srq);
346 destroy_srq_kernel(dev, srq);
354 int mlx5_ib_modify_srq(struct ib_srq *ibsrq, struct ib_srq_attr *attr,
355 enum ib_srq_attr_mask attr_mask, struct ib_udata *udata)
357 struct mlx5_ib_dev *dev = to_mdev(ibsrq->device);
358 struct mlx5_ib_srq *srq = to_msrq(ibsrq);
361 /* We don't support resizing SRQs yet */
362 if (attr_mask & IB_SRQ_MAX_WR)
365 if (attr_mask & IB_SRQ_LIMIT) {
366 if (attr->srq_limit >= srq->msrq.max)
369 mutex_lock(&srq->mutex);
370 ret = mlx5_core_arm_srq(dev->mdev, &srq->msrq, attr->srq_limit, 1);
371 mutex_unlock(&srq->mutex);
380 int mlx5_ib_query_srq(struct ib_srq *ibsrq, struct ib_srq_attr *srq_attr)
382 struct mlx5_ib_dev *dev = to_mdev(ibsrq->device);
383 struct mlx5_ib_srq *srq = to_msrq(ibsrq);
385 struct mlx5_srq_attr *out;
387 out = kzalloc(sizeof(*out), GFP_KERNEL);
391 ret = mlx5_core_query_srq(dev->mdev, &srq->msrq, out);
395 srq_attr->srq_limit = out->lwm;
396 srq_attr->max_wr = srq->msrq.max - 1;
397 srq_attr->max_sge = srq->msrq.max_gs;
404 int mlx5_ib_destroy_srq(struct ib_srq *srq)
406 struct mlx5_ib_dev *dev = to_mdev(srq->device);
407 struct mlx5_ib_srq *msrq = to_msrq(srq);
409 mlx5_core_destroy_srq(dev->mdev, &msrq->msrq);
412 mlx5_ib_db_unmap_user(to_mucontext(srq->uobject->context), &msrq->db);
413 ib_umem_release(msrq->umem);
415 destroy_srq_kernel(dev, msrq);
422 void mlx5_ib_free_srq_wqe(struct mlx5_ib_srq *srq, int wqe_index)
424 struct mlx5_wqe_srq_next_seg *next;
426 /* always called with interrupts disabled. */
427 spin_lock(&srq->lock);
429 next = get_wqe(srq, srq->tail);
430 next->next_wqe_index = cpu_to_be16(wqe_index);
431 srq->tail = wqe_index;
433 spin_unlock(&srq->lock);
436 int mlx5_ib_post_srq_recv(struct ib_srq *ibsrq, struct ib_recv_wr *wr,
437 struct ib_recv_wr **bad_wr)
439 struct mlx5_ib_srq *srq = to_msrq(ibsrq);
440 struct mlx5_wqe_srq_next_seg *next;
441 struct mlx5_wqe_data_seg *scat;
442 struct mlx5_ib_dev *dev = to_mdev(ibsrq->device);
443 struct mlx5_core_dev *mdev = dev->mdev;
449 spin_lock_irqsave(&srq->lock, flags);
451 if (mdev->state == MLX5_DEVICE_STATE_INTERNAL_ERROR) {
457 for (nreq = 0; wr; nreq++, wr = wr->next) {
458 if (unlikely(wr->num_sge > srq->msrq.max_gs)) {
464 if (unlikely(srq->head == srq->tail)) {
470 srq->wrid[srq->head] = wr->wr_id;
472 next = get_wqe(srq, srq->head);
473 srq->head = be16_to_cpu(next->next_wqe_index);
474 scat = (struct mlx5_wqe_data_seg *)(next + 1);
476 for (i = 0; i < wr->num_sge; i++) {
477 scat[i].byte_count = cpu_to_be32(wr->sg_list[i].length);
478 scat[i].lkey = cpu_to_be32(wr->sg_list[i].lkey);
479 scat[i].addr = cpu_to_be64(wr->sg_list[i].addr);
482 if (i < srq->msrq.max_avail_gather) {
483 scat[i].byte_count = 0;
484 scat[i].lkey = cpu_to_be32(MLX5_INVALID_LKEY);
490 srq->wqe_ctr += nreq;
492 /* Make sure that descriptors are written before
497 *srq->db.db = cpu_to_be32(srq->wqe_ctr);
500 spin_unlock_irqrestore(&srq->lock, flags);