112515907SHans Petter Selasky /*-
2b633e08cSHans Petter Selasky * Copyright (c) 2013-2020, Mellanox Technologies, Ltd. All rights reserved.
312515907SHans Petter Selasky *
412515907SHans Petter Selasky * Redistribution and use in source and binary forms, with or without
512515907SHans Petter Selasky * modification, are permitted provided that the following conditions
612515907SHans Petter Selasky * are met:
712515907SHans Petter Selasky * 1. Redistributions of source code must retain the above copyright
812515907SHans Petter Selasky * notice, this list of conditions and the following disclaimer.
912515907SHans Petter Selasky * 2. Redistributions in binary form must reproduce the above copyright
1012515907SHans Petter Selasky * notice, this list of conditions and the following disclaimer in the
1112515907SHans Petter Selasky * documentation and/or other materials provided with the distribution.
1212515907SHans Petter Selasky *
1312515907SHans Petter Selasky * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS `AS IS' AND
1412515907SHans Petter Selasky * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1512515907SHans Petter Selasky * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1612515907SHans Petter Selasky * ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
1712515907SHans Petter Selasky * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1812515907SHans Petter Selasky * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
1912515907SHans Petter Selasky * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2012515907SHans Petter Selasky * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2112515907SHans Petter Selasky * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2212515907SHans Petter Selasky * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2312515907SHans Petter Selasky * SUCH DAMAGE.
2412515907SHans Petter Selasky */
2512515907SHans Petter Selasky
2670600979SKonstantin Belousov #include "opt_rss.h"
2770600979SKonstantin Belousov #include "opt_ratelimit.h"
2870600979SKonstantin Belousov
2912515907SHans Petter Selasky #include <linux/module.h>
3012515907SHans Petter Selasky #include <dev/mlx5/qp.h>
3112515907SHans Petter Selasky #include <dev/mlx5/srq.h>
3212515907SHans Petter Selasky #include <linux/slab.h>
3312515907SHans Petter Selasky #include <rdma/ib_umem.h>
3412515907SHans Petter Selasky #include <rdma/ib_user_verbs.h>
35b633e08cSHans Petter Selasky #include <rdma/uverbs_ioctl.h>
3612515907SHans Petter Selasky
37*028130b8SKonstantin Belousov #include <dev/mlx5/mlx5_ib/mlx5_ib.h>
3812515907SHans Petter Selasky
get_wqe(struct mlx5_ib_srq * srq,int n)3912515907SHans Petter Selasky static void *get_wqe(struct mlx5_ib_srq *srq, int n)
4012515907SHans Petter Selasky {
4112515907SHans Petter Selasky return mlx5_buf_offset(&srq->buf, n << srq->msrq.wqe_shift);
4212515907SHans Petter Selasky }
4312515907SHans Petter Selasky
mlx5_ib_srq_event(struct mlx5_core_srq * srq,int type)4412515907SHans Petter Selasky static void mlx5_ib_srq_event(struct mlx5_core_srq *srq, int type)
4512515907SHans Petter Selasky {
4612515907SHans Petter Selasky struct ib_event event;
4712515907SHans Petter Selasky struct ib_srq *ibsrq = &to_mibsrq(srq)->ibsrq;
4812515907SHans Petter Selasky
4912515907SHans Petter Selasky if (ibsrq->event_handler) {
5012515907SHans Petter Selasky event.device = ibsrq->device;
5112515907SHans Petter Selasky event.element.srq = ibsrq;
5212515907SHans Petter Selasky switch (type) {
5312515907SHans Petter Selasky case MLX5_EVENT_TYPE_SRQ_RQ_LIMIT:
5412515907SHans Petter Selasky event.event = IB_EVENT_SRQ_LIMIT_REACHED;
5512515907SHans Petter Selasky break;
5612515907SHans Petter Selasky case MLX5_EVENT_TYPE_SRQ_CATAS_ERROR:
5712515907SHans Petter Selasky event.event = IB_EVENT_SRQ_ERR;
5812515907SHans Petter Selasky break;
5912515907SHans Petter Selasky default:
608e6e287fSHans Petter Selasky pr_warn("mlx5_ib: Unexpected event type %d on SRQ %06x\n",
618e6e287fSHans Petter Selasky type, srq->srqn);
6212515907SHans Petter Selasky return;
6312515907SHans Petter Selasky }
6412515907SHans Petter Selasky
6512515907SHans Petter Selasky ibsrq->event_handler(&event, ibsrq->srq_context);
6612515907SHans Petter Selasky }
6712515907SHans Petter Selasky }
6812515907SHans Petter Selasky
create_srq_user(struct ib_pd * pd,struct mlx5_ib_srq * srq,struct mlx5_srq_attr * in,struct ib_udata * udata,int buf_size)6912515907SHans Petter Selasky static int create_srq_user(struct ib_pd *pd, struct mlx5_ib_srq *srq,
70788333d9SHans Petter Selasky struct mlx5_srq_attr *in,
71788333d9SHans Petter Selasky struct ib_udata *udata, int buf_size)
7212515907SHans Petter Selasky {
7312515907SHans Petter Selasky struct mlx5_ib_dev *dev = to_mdev(pd->device);
748e6e287fSHans Petter Selasky struct mlx5_ib_create_srq ucmd = {};
75b633e08cSHans Petter Selasky struct mlx5_ib_ucontext *ucontext = rdma_udata_to_drv_context(
76b633e08cSHans Petter Selasky udata, struct mlx5_ib_ucontext, ibucontext);
7712515907SHans Petter Selasky size_t ucmdlen;
7812515907SHans Petter Selasky int err;
7912515907SHans Petter Selasky int npages;
8012515907SHans Petter Selasky int page_shift;
8112515907SHans Petter Selasky int ncont;
8212515907SHans Petter Selasky u32 offset;
838e6e287fSHans Petter Selasky u32 uidx = MLX5_IB_DEFAULT_UIDX;
8412515907SHans Petter Selasky
858e6e287fSHans Petter Selasky ucmdlen = min(udata->inlen, sizeof(ucmd));
8612515907SHans Petter Selasky
8712515907SHans Petter Selasky if (ib_copy_from_udata(&ucmd, udata, ucmdlen)) {
888e6e287fSHans Petter Selasky mlx5_ib_dbg(dev, "failed copy udata\n");
8912515907SHans Petter Selasky return -EFAULT;
9012515907SHans Petter Selasky }
9112515907SHans Petter Selasky
928e6e287fSHans Petter Selasky if (ucmd.reserved0 || ucmd.reserved1)
9312515907SHans Petter Selasky return -EINVAL;
948e6e287fSHans Petter Selasky
958e6e287fSHans Petter Selasky if (udata->inlen > sizeof(ucmd) &&
968e6e287fSHans Petter Selasky !ib_is_udata_cleared(udata, sizeof(ucmd),
978e6e287fSHans Petter Selasky udata->inlen - sizeof(ucmd)))
988e6e287fSHans Petter Selasky return -EINVAL;
998e6e287fSHans Petter Selasky
100b633e08cSHans Petter Selasky if (in->type != IB_SRQT_BASIC) {
101b633e08cSHans Petter Selasky err = get_srq_user_index(ucontext, &ucmd, udata->inlen, &uidx);
1028e6e287fSHans Petter Selasky if (err)
1038e6e287fSHans Petter Selasky return err;
10412515907SHans Petter Selasky }
10512515907SHans Petter Selasky
10612515907SHans Petter Selasky srq->wq_sig = !!(ucmd.flags & MLX5_SRQ_FLAG_SIGNATURE);
10712515907SHans Petter Selasky
108b633e08cSHans Petter Selasky srq->umem = ib_umem_get(&ucontext->ibucontext, ucmd.buf_addr, buf_size, 0, 0);
10912515907SHans Petter Selasky if (IS_ERR(srq->umem)) {
1108e6e287fSHans Petter Selasky mlx5_ib_dbg(dev, "failed umem get, size %d\n", buf_size);
11112515907SHans Petter Selasky err = PTR_ERR(srq->umem);
11212515907SHans Petter Selasky return err;
11312515907SHans Petter Selasky }
11412515907SHans Petter Selasky
115565cb4e8SHans Petter Selasky mlx5_ib_cont_pages(srq->umem, ucmd.buf_addr, 0, &npages,
11612515907SHans Petter Selasky &page_shift, &ncont, NULL);
11712515907SHans Petter Selasky err = mlx5_ib_get_buf_offset(ucmd.buf_addr, page_shift,
11812515907SHans Petter Selasky &offset);
11912515907SHans Petter Selasky if (err) {
12012515907SHans Petter Selasky mlx5_ib_warn(dev, "bad offset\n");
12112515907SHans Petter Selasky goto err_umem;
12212515907SHans Petter Selasky }
12312515907SHans Petter Selasky
124788333d9SHans Petter Selasky in->pas = mlx5_vzalloc(sizeof(*in->pas) * ncont);
125788333d9SHans Petter Selasky if (!in->pas) {
12612515907SHans Petter Selasky err = -ENOMEM;
12712515907SHans Petter Selasky goto err_umem;
12812515907SHans Petter Selasky }
12912515907SHans Petter Selasky
130788333d9SHans Petter Selasky mlx5_ib_populate_pas(dev, srq->umem, page_shift, in->pas, 0);
13112515907SHans Petter Selasky
132b633e08cSHans Petter Selasky err = mlx5_ib_db_map_user(ucontext, ucmd.db_addr, &srq->db);
13312515907SHans Petter Selasky if (err) {
1348e6e287fSHans Petter Selasky mlx5_ib_dbg(dev, "map doorbell failed\n");
13512515907SHans Petter Selasky goto err_in;
13612515907SHans Petter Selasky }
13712515907SHans Petter Selasky
138788333d9SHans Petter Selasky in->log_page_size = page_shift - MLX5_ADAPTER_PAGE_SHIFT;
139788333d9SHans Petter Selasky in->page_offset = offset;
140b633e08cSHans Petter Selasky in->uid = (in->type != IB_SRQT_XRC) ? to_mpd(pd)->uid : 0;
1418e6e287fSHans Petter Selasky if (MLX5_CAP_GEN(dev->mdev, cqe_version) == MLX5_CQE_VERSION_V1 &&
142b633e08cSHans Petter Selasky in->type != IB_SRQT_BASIC)
143788333d9SHans Petter Selasky in->user_index = uidx;
144b633e08cSHans Petter Selasky
14512515907SHans Petter Selasky return 0;
14612515907SHans Petter Selasky
14712515907SHans Petter Selasky err_in:
148788333d9SHans Petter Selasky kvfree(in->pas);
14912515907SHans Petter Selasky
15012515907SHans Petter Selasky err_umem:
15112515907SHans Petter Selasky ib_umem_release(srq->umem);
15212515907SHans Petter Selasky
15312515907SHans Petter Selasky return err;
15412515907SHans Petter Selasky }
15512515907SHans Petter Selasky
create_srq_kernel(struct mlx5_ib_dev * dev,struct mlx5_ib_srq * srq,struct mlx5_srq_attr * in,int buf_size)15612515907SHans Petter Selasky static int create_srq_kernel(struct mlx5_ib_dev *dev, struct mlx5_ib_srq *srq,
157788333d9SHans Petter Selasky struct mlx5_srq_attr *in, int buf_size)
15812515907SHans Petter Selasky {
15912515907SHans Petter Selasky int err;
16012515907SHans Petter Selasky int i;
16112515907SHans Petter Selasky struct mlx5_wqe_srq_next_seg *next;
16212515907SHans Petter Selasky
16312515907SHans Petter Selasky err = mlx5_db_alloc(dev->mdev, &srq->db);
16412515907SHans Petter Selasky if (err) {
16512515907SHans Petter Selasky mlx5_ib_warn(dev, "alloc dbell rec failed\n");
16612515907SHans Petter Selasky return err;
16712515907SHans Petter Selasky }
16812515907SHans Petter Selasky
1698e6e287fSHans Petter Selasky if (mlx5_buf_alloc(dev->mdev, buf_size, 2 * PAGE_SIZE, &srq->buf)) {
1708e6e287fSHans Petter Selasky mlx5_ib_dbg(dev, "buf alloc failed\n");
17112515907SHans Petter Selasky err = -ENOMEM;
17212515907SHans Petter Selasky goto err_db;
17312515907SHans Petter Selasky }
17412515907SHans Petter Selasky
17512515907SHans Petter Selasky srq->head = 0;
17612515907SHans Petter Selasky srq->tail = srq->msrq.max - 1;
17712515907SHans Petter Selasky srq->wqe_ctr = 0;
17812515907SHans Petter Selasky
17912515907SHans Petter Selasky for (i = 0; i < srq->msrq.max; i++) {
18012515907SHans Petter Selasky next = get_wqe(srq, i);
18112515907SHans Petter Selasky next->next_wqe_index =
18212515907SHans Petter Selasky cpu_to_be16((i + 1) & (srq->msrq.max - 1));
18312515907SHans Petter Selasky }
18412515907SHans Petter Selasky
185abb28d28SSlava Shwartsman mlx5_ib_dbg(dev, "srq->buf.page_shift = %d\n", srq->buf.page_shift);
186abb28d28SSlava Shwartsman in->pas = mlx5_vzalloc(sizeof(*in->pas) * srq->buf.npages);
187788333d9SHans Petter Selasky if (!in->pas) {
18812515907SHans Petter Selasky err = -ENOMEM;
18912515907SHans Petter Selasky goto err_buf;
19012515907SHans Petter Selasky }
191788333d9SHans Petter Selasky mlx5_fill_page_array(&srq->buf, in->pas);
19212515907SHans Petter Selasky
19312515907SHans Petter Selasky srq->wrid = kmalloc(srq->msrq.max * sizeof(u64), GFP_KERNEL);
19412515907SHans Petter Selasky if (!srq->wrid) {
19512515907SHans Petter Selasky err = -ENOMEM;
19612515907SHans Petter Selasky goto err_in;
19712515907SHans Petter Selasky }
198b633e08cSHans Petter Selasky srq->wq_sig = 0;
19912515907SHans Petter Selasky
200abb28d28SSlava Shwartsman in->log_page_size = srq->buf.page_shift - MLX5_ADAPTER_PAGE_SHIFT;
2018e6e287fSHans Petter Selasky if (MLX5_CAP_GEN(dev->mdev, cqe_version) == MLX5_CQE_VERSION_V1 &&
202b633e08cSHans Petter Selasky in->type != IB_SRQT_BASIC)
203788333d9SHans Petter Selasky in->user_index = MLX5_IB_DEFAULT_UIDX;
20412515907SHans Petter Selasky
20512515907SHans Petter Selasky return 0;
20612515907SHans Petter Selasky
20712515907SHans Petter Selasky err_in:
208788333d9SHans Petter Selasky kvfree(in->pas);
20912515907SHans Petter Selasky
21012515907SHans Petter Selasky err_buf:
21112515907SHans Petter Selasky mlx5_buf_free(dev->mdev, &srq->buf);
21212515907SHans Petter Selasky
21312515907SHans Petter Selasky err_db:
21412515907SHans Petter Selasky mlx5_db_free(dev->mdev, &srq->db);
21512515907SHans Petter Selasky return err;
21612515907SHans Petter Selasky }
21712515907SHans Petter Selasky
destroy_srq_user(struct ib_pd * pd,struct mlx5_ib_srq * srq,struct ib_udata * udata)218b633e08cSHans Petter Selasky static void destroy_srq_user(struct ib_pd *pd, struct mlx5_ib_srq *srq,
219b633e08cSHans Petter Selasky struct ib_udata *udata)
22012515907SHans Petter Selasky {
221b633e08cSHans Petter Selasky mlx5_ib_db_unmap_user(
222b633e08cSHans Petter Selasky rdma_udata_to_drv_context(
223b633e08cSHans Petter Selasky udata,
224b633e08cSHans Petter Selasky struct mlx5_ib_ucontext,
225b633e08cSHans Petter Selasky ibucontext),
226b633e08cSHans Petter Selasky &srq->db);
22712515907SHans Petter Selasky ib_umem_release(srq->umem);
22812515907SHans Petter Selasky }
22912515907SHans Petter Selasky
23012515907SHans Petter Selasky
destroy_srq_kernel(struct mlx5_ib_dev * dev,struct mlx5_ib_srq * srq)23112515907SHans Petter Selasky static void destroy_srq_kernel(struct mlx5_ib_dev *dev, struct mlx5_ib_srq *srq)
23212515907SHans Petter Selasky {
23312515907SHans Petter Selasky kfree(srq->wrid);
23412515907SHans Petter Selasky mlx5_buf_free(dev->mdev, &srq->buf);
23512515907SHans Petter Selasky mlx5_db_free(dev->mdev, &srq->db);
23612515907SHans Petter Selasky }
23712515907SHans Petter Selasky
mlx5_ib_create_srq(struct ib_srq * ib_srq,struct ib_srq_init_attr * init_attr,struct ib_udata * udata)238b633e08cSHans Petter Selasky int mlx5_ib_create_srq(struct ib_srq *ib_srq,
23912515907SHans Petter Selasky struct ib_srq_init_attr *init_attr,
24012515907SHans Petter Selasky struct ib_udata *udata)
24112515907SHans Petter Selasky {
242b633e08cSHans Petter Selasky struct mlx5_ib_dev *dev = to_mdev(ib_srq->device);
243b633e08cSHans Petter Selasky struct mlx5_ib_srq *srq = to_msrq(ib_srq);
244abb28d28SSlava Shwartsman size_t desc_size;
245abb28d28SSlava Shwartsman size_t buf_size;
24612515907SHans Petter Selasky int err;
247b633e08cSHans Petter Selasky struct mlx5_srq_attr in = {};
24812515907SHans Petter Selasky __u32 max_srq_wqes = 1 << MLX5_CAP_GEN(dev->mdev, log_max_srq_sz);
24912515907SHans Petter Selasky
25012515907SHans Petter Selasky /* Sanity check SRQ size before proceeding */
25112515907SHans Petter Selasky if (init_attr->attr.max_wr >= max_srq_wqes) {
2528e6e287fSHans Petter Selasky mlx5_ib_dbg(dev, "max_wr %d, cap %d\n",
25312515907SHans Petter Selasky init_attr->attr.max_wr,
25412515907SHans Petter Selasky max_srq_wqes);
255b633e08cSHans Petter Selasky return -EINVAL;
25612515907SHans Petter Selasky }
25712515907SHans Petter Selasky
25812515907SHans Petter Selasky mutex_init(&srq->mutex);
25912515907SHans Petter Selasky spin_lock_init(&srq->lock);
26012515907SHans Petter Selasky srq->msrq.max = roundup_pow_of_two(init_attr->attr.max_wr + 1);
26112515907SHans Petter Selasky srq->msrq.max_gs = init_attr->attr.max_sge;
26212515907SHans Petter Selasky
26312515907SHans Petter Selasky desc_size = sizeof(struct mlx5_wqe_srq_next_seg) +
26412515907SHans Petter Selasky srq->msrq.max_gs * sizeof(struct mlx5_wqe_data_seg);
265b633e08cSHans Petter Selasky if (desc_size == 0 || srq->msrq.max_gs > desc_size)
266b633e08cSHans Petter Selasky return -EINVAL;
267b633e08cSHans Petter Selasky
26812515907SHans Petter Selasky desc_size = roundup_pow_of_two(desc_size);
269abb28d28SSlava Shwartsman desc_size = max_t(size_t, 32, desc_size);
270b633e08cSHans Petter Selasky if (desc_size < sizeof(struct mlx5_wqe_srq_next_seg))
271b633e08cSHans Petter Selasky return -EINVAL;
272b633e08cSHans Petter Selasky
27312515907SHans Petter Selasky srq->msrq.max_avail_gather = (desc_size - sizeof(struct mlx5_wqe_srq_next_seg)) /
27412515907SHans Petter Selasky sizeof(struct mlx5_wqe_data_seg);
27512515907SHans Petter Selasky srq->msrq.wqe_shift = ilog2(desc_size);
27612515907SHans Petter Selasky buf_size = srq->msrq.max * desc_size;
277b633e08cSHans Petter Selasky if (buf_size < desc_size)
278b633e08cSHans Petter Selasky return -EINVAL;
279b633e08cSHans Petter Selasky
280abb28d28SSlava Shwartsman in.type = init_attr->srq_type;
28112515907SHans Petter Selasky
282b633e08cSHans Petter Selasky if (udata)
283b633e08cSHans Petter Selasky err = create_srq_user(ib_srq->pd, srq, &in, udata, buf_size);
28412515907SHans Petter Selasky else
285788333d9SHans Petter Selasky err = create_srq_kernel(dev, srq, &in, buf_size);
28612515907SHans Petter Selasky
287b633e08cSHans Petter Selasky if (err) {
28812515907SHans Petter Selasky mlx5_ib_warn(dev, "create srq %s failed, err %d\n",
289b633e08cSHans Petter Selasky udata ? "user" : "kernel", err);
290b633e08cSHans Petter Selasky return err;
29112515907SHans Petter Selasky }
29212515907SHans Petter Selasky
293788333d9SHans Petter Selasky in.log_size = ilog2(srq->msrq.max);
294788333d9SHans Petter Selasky in.wqe_shift = srq->msrq.wqe_shift - 4;
295788333d9SHans Petter Selasky if (srq->wq_sig)
296788333d9SHans Petter Selasky in.flags |= MLX5_SRQ_FLAG_WQ_SIG;
29712515907SHans Petter Selasky
298b633e08cSHans Petter Selasky if (init_attr->srq_type == IB_SRQT_XRC)
299b633e08cSHans Petter Selasky in.xrcd = to_mxrcd(init_attr->ext.xrc.xrcd)->xrcdn;
300b633e08cSHans Petter Selasky else
301b633e08cSHans Petter Selasky in.xrcd = to_mxrcd(dev->devr.x0)->xrcdn;
302b633e08cSHans Petter Selasky
303b633e08cSHans Petter Selasky if (ib_srq_has_cq(init_attr->srq_type))
304b633e08cSHans Petter Selasky in.cqn = to_mcq(init_attr->ext.cq)->mcq.cqn;
305b633e08cSHans Petter Selasky else
306b633e08cSHans Petter Selasky in.cqn = to_mcq(dev->devr.c0)->mcq.cqn;
307b633e08cSHans Petter Selasky
308b633e08cSHans Petter Selasky in.pd = to_mpd(ib_srq->pd)->pdn;
309788333d9SHans Petter Selasky in.db_record = srq->db.dma;
310788333d9SHans Petter Selasky err = mlx5_core_create_srq(dev->mdev, &srq->msrq, &in);
311788333d9SHans Petter Selasky kvfree(in.pas);
31212515907SHans Petter Selasky if (err) {
3138e6e287fSHans Petter Selasky mlx5_ib_dbg(dev, "create SRQ failed, err %d\n", err);
31412515907SHans Petter Selasky goto err_usr_kern_srq;
31512515907SHans Petter Selasky }
31612515907SHans Petter Selasky
31712515907SHans Petter Selasky mlx5_ib_dbg(dev, "create SRQ with srqn 0x%x\n", srq->msrq.srqn);
31812515907SHans Petter Selasky
31912515907SHans Petter Selasky srq->msrq.event = mlx5_ib_srq_event;
32012515907SHans Petter Selasky srq->ibsrq.ext.xrc.srq_num = srq->msrq.srqn;
32112515907SHans Petter Selasky
322b633e08cSHans Petter Selasky if (udata)
32312515907SHans Petter Selasky if (ib_copy_to_udata(udata, &srq->msrq.srqn, sizeof(__u32))) {
3248e6e287fSHans Petter Selasky mlx5_ib_dbg(dev, "copy to user failed\n");
32512515907SHans Petter Selasky err = -EFAULT;
32612515907SHans Petter Selasky goto err_core;
32712515907SHans Petter Selasky }
32812515907SHans Petter Selasky
32912515907SHans Petter Selasky init_attr->attr.max_wr = srq->msrq.max - 1;
33012515907SHans Petter Selasky
331b633e08cSHans Petter Selasky return 0;
33212515907SHans Petter Selasky
33312515907SHans Petter Selasky err_core:
33412515907SHans Petter Selasky mlx5_core_destroy_srq(dev->mdev, &srq->msrq);
33512515907SHans Petter Selasky
33612515907SHans Petter Selasky err_usr_kern_srq:
337b633e08cSHans Petter Selasky if (udata)
338b633e08cSHans Petter Selasky destroy_srq_user(ib_srq->pd, srq, udata);
33912515907SHans Petter Selasky else
34012515907SHans Petter Selasky destroy_srq_kernel(dev, srq);
34112515907SHans Petter Selasky
342b633e08cSHans Petter Selasky return err;
34312515907SHans Petter Selasky }
34412515907SHans Petter Selasky
mlx5_ib_modify_srq(struct ib_srq * ibsrq,struct ib_srq_attr * attr,enum ib_srq_attr_mask attr_mask,struct ib_udata * udata)34512515907SHans Petter Selasky int mlx5_ib_modify_srq(struct ib_srq *ibsrq, struct ib_srq_attr *attr,
34612515907SHans Petter Selasky enum ib_srq_attr_mask attr_mask, struct ib_udata *udata)
34712515907SHans Petter Selasky {
34812515907SHans Petter Selasky struct mlx5_ib_dev *dev = to_mdev(ibsrq->device);
34912515907SHans Petter Selasky struct mlx5_ib_srq *srq = to_msrq(ibsrq);
35012515907SHans Petter Selasky int ret;
35112515907SHans Petter Selasky
35212515907SHans Petter Selasky /* We don't support resizing SRQs yet */
35312515907SHans Petter Selasky if (attr_mask & IB_SRQ_MAX_WR)
35412515907SHans Petter Selasky return -EINVAL;
35512515907SHans Petter Selasky
35612515907SHans Petter Selasky if (attr_mask & IB_SRQ_LIMIT) {
35712515907SHans Petter Selasky if (attr->srq_limit >= srq->msrq.max)
35812515907SHans Petter Selasky return -EINVAL;
35912515907SHans Petter Selasky
36012515907SHans Petter Selasky mutex_lock(&srq->mutex);
36112515907SHans Petter Selasky ret = mlx5_core_arm_srq(dev->mdev, &srq->msrq, attr->srq_limit, 1);
36212515907SHans Petter Selasky mutex_unlock(&srq->mutex);
36312515907SHans Petter Selasky
36412515907SHans Petter Selasky if (ret)
36512515907SHans Petter Selasky return ret;
36612515907SHans Petter Selasky }
36712515907SHans Petter Selasky
36812515907SHans Petter Selasky return 0;
36912515907SHans Petter Selasky }
37012515907SHans Petter Selasky
mlx5_ib_query_srq(struct ib_srq * ibsrq,struct ib_srq_attr * srq_attr)37112515907SHans Petter Selasky int mlx5_ib_query_srq(struct ib_srq *ibsrq, struct ib_srq_attr *srq_attr)
37212515907SHans Petter Selasky {
37312515907SHans Petter Selasky struct mlx5_ib_dev *dev = to_mdev(ibsrq->device);
37412515907SHans Petter Selasky struct mlx5_ib_srq *srq = to_msrq(ibsrq);
37512515907SHans Petter Selasky int ret;
376788333d9SHans Petter Selasky struct mlx5_srq_attr *out;
37712515907SHans Petter Selasky
37812515907SHans Petter Selasky out = kzalloc(sizeof(*out), GFP_KERNEL);
37912515907SHans Petter Selasky if (!out)
38012515907SHans Petter Selasky return -ENOMEM;
38112515907SHans Petter Selasky
38212515907SHans Petter Selasky ret = mlx5_core_query_srq(dev->mdev, &srq->msrq, out);
38312515907SHans Petter Selasky if (ret)
38412515907SHans Petter Selasky goto out_box;
38512515907SHans Petter Selasky
386788333d9SHans Petter Selasky srq_attr->srq_limit = out->lwm;
38712515907SHans Petter Selasky srq_attr->max_wr = srq->msrq.max - 1;
38812515907SHans Petter Selasky srq_attr->max_sge = srq->msrq.max_gs;
38912515907SHans Petter Selasky
39012515907SHans Petter Selasky out_box:
39112515907SHans Petter Selasky kfree(out);
39212515907SHans Petter Selasky return ret;
39312515907SHans Petter Selasky }
39412515907SHans Petter Selasky
mlx5_ib_destroy_srq(struct ib_srq * srq,struct ib_udata * udata)395b633e08cSHans Petter Selasky void mlx5_ib_destroy_srq(struct ib_srq *srq, struct ib_udata *udata)
39612515907SHans Petter Selasky {
39712515907SHans Petter Selasky struct mlx5_ib_dev *dev = to_mdev(srq->device);
39812515907SHans Petter Selasky struct mlx5_ib_srq *msrq = to_msrq(srq);
39912515907SHans Petter Selasky
40012515907SHans Petter Selasky mlx5_core_destroy_srq(dev->mdev, &msrq->msrq);
40112515907SHans Petter Selasky
40212515907SHans Petter Selasky if (srq->uobject) {
403b633e08cSHans Petter Selasky mlx5_ib_db_unmap_user(
404b633e08cSHans Petter Selasky rdma_udata_to_drv_context(
405b633e08cSHans Petter Selasky udata,
406b633e08cSHans Petter Selasky struct mlx5_ib_ucontext,
407b633e08cSHans Petter Selasky ibucontext),
408b633e08cSHans Petter Selasky &msrq->db);
40912515907SHans Petter Selasky ib_umem_release(msrq->umem);
41012515907SHans Petter Selasky } else {
41112515907SHans Petter Selasky destroy_srq_kernel(dev, msrq);
41212515907SHans Petter Selasky }
41312515907SHans Petter Selasky }
41412515907SHans Petter Selasky
mlx5_ib_free_srq_wqe(struct mlx5_ib_srq * srq,int wqe_index)41512515907SHans Petter Selasky void mlx5_ib_free_srq_wqe(struct mlx5_ib_srq *srq, int wqe_index)
41612515907SHans Petter Selasky {
41712515907SHans Petter Selasky struct mlx5_wqe_srq_next_seg *next;
41812515907SHans Petter Selasky
41912515907SHans Petter Selasky /* always called with interrupts disabled. */
42012515907SHans Petter Selasky spin_lock(&srq->lock);
42112515907SHans Petter Selasky
42212515907SHans Petter Selasky next = get_wqe(srq, srq->tail);
42312515907SHans Petter Selasky next->next_wqe_index = cpu_to_be16(wqe_index);
42412515907SHans Petter Selasky srq->tail = wqe_index;
42512515907SHans Petter Selasky
42612515907SHans Petter Selasky spin_unlock(&srq->lock);
42712515907SHans Petter Selasky }
42812515907SHans Petter Selasky
mlx5_ib_post_srq_recv(struct ib_srq * ibsrq,const struct ib_recv_wr * wr,const struct ib_recv_wr ** bad_wr)429c3987b8eSHans Petter Selasky int mlx5_ib_post_srq_recv(struct ib_srq *ibsrq, const struct ib_recv_wr *wr,
430c3987b8eSHans Petter Selasky const struct ib_recv_wr **bad_wr)
43112515907SHans Petter Selasky {
43212515907SHans Petter Selasky struct mlx5_ib_srq *srq = to_msrq(ibsrq);
43312515907SHans Petter Selasky struct mlx5_wqe_srq_next_seg *next;
43412515907SHans Petter Selasky struct mlx5_wqe_data_seg *scat;
43512515907SHans Petter Selasky struct mlx5_ib_dev *dev = to_mdev(ibsrq->device);
43612515907SHans Petter Selasky struct mlx5_core_dev *mdev = dev->mdev;
43712515907SHans Petter Selasky unsigned long flags;
43812515907SHans Petter Selasky int err = 0;
43912515907SHans Petter Selasky int nreq;
44012515907SHans Petter Selasky int i;
44112515907SHans Petter Selasky
44212515907SHans Petter Selasky spin_lock_irqsave(&srq->lock, flags);
44312515907SHans Petter Selasky
44412515907SHans Petter Selasky if (mdev->state == MLX5_DEVICE_STATE_INTERNAL_ERROR) {
44512515907SHans Petter Selasky err = -EIO;
44612515907SHans Petter Selasky *bad_wr = wr;
44712515907SHans Petter Selasky goto out;
44812515907SHans Petter Selasky }
44912515907SHans Petter Selasky
45012515907SHans Petter Selasky for (nreq = 0; wr; nreq++, wr = wr->next) {
45112515907SHans Petter Selasky if (unlikely(wr->num_sge > srq->msrq.max_gs)) {
45212515907SHans Petter Selasky err = -EINVAL;
45312515907SHans Petter Selasky *bad_wr = wr;
45412515907SHans Petter Selasky break;
45512515907SHans Petter Selasky }
45612515907SHans Petter Selasky
45712515907SHans Petter Selasky if (unlikely(srq->head == srq->tail)) {
45812515907SHans Petter Selasky err = -ENOMEM;
45912515907SHans Petter Selasky *bad_wr = wr;
46012515907SHans Petter Selasky break;
46112515907SHans Petter Selasky }
46212515907SHans Petter Selasky
46312515907SHans Petter Selasky srq->wrid[srq->head] = wr->wr_id;
46412515907SHans Petter Selasky
46512515907SHans Petter Selasky next = get_wqe(srq, srq->head);
46612515907SHans Petter Selasky srq->head = be16_to_cpu(next->next_wqe_index);
46712515907SHans Petter Selasky scat = (struct mlx5_wqe_data_seg *)(next + 1);
46812515907SHans Petter Selasky
46912515907SHans Petter Selasky for (i = 0; i < wr->num_sge; i++) {
47012515907SHans Petter Selasky scat[i].byte_count = cpu_to_be32(wr->sg_list[i].length);
47112515907SHans Petter Selasky scat[i].lkey = cpu_to_be32(wr->sg_list[i].lkey);
47212515907SHans Petter Selasky scat[i].addr = cpu_to_be64(wr->sg_list[i].addr);
47312515907SHans Petter Selasky }
47412515907SHans Petter Selasky
47512515907SHans Petter Selasky if (i < srq->msrq.max_avail_gather) {
47612515907SHans Petter Selasky scat[i].byte_count = 0;
47712515907SHans Petter Selasky scat[i].lkey = cpu_to_be32(MLX5_INVALID_LKEY);
47812515907SHans Petter Selasky scat[i].addr = 0;
47912515907SHans Petter Selasky }
48012515907SHans Petter Selasky }
48112515907SHans Petter Selasky
48212515907SHans Petter Selasky if (likely(nreq)) {
48312515907SHans Petter Selasky srq->wqe_ctr += nreq;
48412515907SHans Petter Selasky
48512515907SHans Petter Selasky /* Make sure that descriptors are written before
48612515907SHans Petter Selasky * doorbell record.
48712515907SHans Petter Selasky */
48812515907SHans Petter Selasky wmb();
48912515907SHans Petter Selasky
49012515907SHans Petter Selasky *srq->db.db = cpu_to_be32(srq->wqe_ctr);
49112515907SHans Petter Selasky }
49212515907SHans Petter Selasky out:
49312515907SHans Petter Selasky spin_unlock_irqrestore(&srq->lock, flags);
49412515907SHans Petter Selasky
49512515907SHans Petter Selasky return err;
49612515907SHans Petter Selasky }
497