1 // SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB
3 * Copyright (c) 2013-2018, Mellanox Technologies inc. All rights reserved.
6 #include <linux/module.h>
7 #include <linux/mlx5/qp.h>
8 #include <linux/slab.h>
9 #include <rdma/ib_umem.h>
10 #include <rdma/ib_user_verbs.h>
14 static void *get_wqe(struct mlx5_ib_srq
*srq
, int n
)
16 return mlx5_frag_buf_get_wqe(&srq
->fbc
, n
);
19 static void mlx5_ib_srq_event(struct mlx5_core_srq
*srq
, enum mlx5_event type
)
21 struct ib_event event
;
22 struct ib_srq
*ibsrq
= &to_mibsrq(srq
)->ibsrq
;
24 if (ibsrq
->event_handler
) {
25 event
.device
= ibsrq
->device
;
26 event
.element
.srq
= ibsrq
;
28 case MLX5_EVENT_TYPE_SRQ_RQ_LIMIT
:
29 event
.event
= IB_EVENT_SRQ_LIMIT_REACHED
;
31 case MLX5_EVENT_TYPE_SRQ_CATAS_ERROR
:
32 event
.event
= IB_EVENT_SRQ_ERR
;
35 pr_warn("mlx5_ib: Unexpected event type %d on SRQ %06x\n",
40 ibsrq
->event_handler(&event
, ibsrq
->srq_context
);
44 static int create_srq_user(struct ib_pd
*pd
, struct mlx5_ib_srq
*srq
,
45 struct mlx5_srq_attr
*in
,
46 struct ib_udata
*udata
, int buf_size
)
48 struct mlx5_ib_dev
*dev
= to_mdev(pd
->device
);
49 struct mlx5_ib_create_srq ucmd
= {};
50 struct mlx5_ib_ucontext
*ucontext
= rdma_udata_to_drv_context(
51 udata
, struct mlx5_ib_ucontext
, ibucontext
);
54 u32 uidx
= MLX5_IB_DEFAULT_UIDX
;
56 ucmdlen
= min(udata
->inlen
, sizeof(ucmd
));
58 if (ib_copy_from_udata(&ucmd
, udata
, ucmdlen
)) {
59 mlx5_ib_dbg(dev
, "failed copy udata\n");
63 if (ucmd
.reserved0
|| ucmd
.reserved1
)
66 if (udata
->inlen
> sizeof(ucmd
) &&
67 !ib_is_udata_cleared(udata
, sizeof(ucmd
),
68 udata
->inlen
- sizeof(ucmd
)))
71 if (in
->type
!= IB_SRQT_BASIC
) {
72 err
= get_srq_user_index(ucontext
, &ucmd
, udata
->inlen
, &uidx
);
77 srq
->wq_sig
= !!(ucmd
.flags
& MLX5_SRQ_FLAG_SIGNATURE
);
79 srq
->umem
= ib_umem_get(pd
->device
, ucmd
.buf_addr
, buf_size
, 0);
80 if (IS_ERR(srq
->umem
)) {
81 mlx5_ib_dbg(dev
, "failed umem get, size %d\n", buf_size
);
82 err
= PTR_ERR(srq
->umem
);
87 err
= mlx5_ib_db_map_user(ucontext
, udata
, ucmd
.db_addr
, &srq
->db
);
89 mlx5_ib_dbg(dev
, "map doorbell failed\n");
93 in
->uid
= (in
->type
!= IB_SRQT_XRC
) ? to_mpd(pd
)->uid
: 0;
94 if (MLX5_CAP_GEN(dev
->mdev
, cqe_version
) == MLX5_CQE_VERSION_V1
&&
95 in
->type
!= IB_SRQT_BASIC
)
96 in
->user_index
= uidx
;
101 ib_umem_release(srq
->umem
);
106 static int create_srq_kernel(struct mlx5_ib_dev
*dev
, struct mlx5_ib_srq
*srq
,
107 struct mlx5_srq_attr
*in
, int buf_size
)
111 struct mlx5_wqe_srq_next_seg
*next
;
113 err
= mlx5_db_alloc(dev
->mdev
, &srq
->db
);
115 mlx5_ib_warn(dev
, "alloc dbell rec failed\n");
119 if (mlx5_frag_buf_alloc_node(dev
->mdev
, buf_size
, &srq
->buf
,
120 dev
->mdev
->priv
.numa_node
)) {
121 mlx5_ib_dbg(dev
, "buf alloc failed\n");
126 mlx5_init_fbc(srq
->buf
.frags
, srq
->msrq
.wqe_shift
, ilog2(srq
->msrq
.max
),
130 srq
->tail
= srq
->msrq
.max
- 1;
133 for (i
= 0; i
< srq
->msrq
.max
; i
++) {
134 next
= get_wqe(srq
, i
);
135 next
->next_wqe_index
=
136 cpu_to_be16((i
+ 1) & (srq
->msrq
.max
- 1));
139 mlx5_ib_dbg(dev
, "srq->buf.page_shift = %d\n", srq
->buf
.page_shift
);
140 in
->pas
= kvcalloc(srq
->buf
.npages
, sizeof(*in
->pas
), GFP_KERNEL
);
145 mlx5_fill_page_frag_array(&srq
->buf
, in
->pas
);
147 srq
->wrid
= kvmalloc_array(srq
->msrq
.max
, sizeof(u64
), GFP_KERNEL
);
154 in
->log_page_size
= srq
->buf
.page_shift
- MLX5_ADAPTER_PAGE_SHIFT
;
155 if (MLX5_CAP_GEN(dev
->mdev
, cqe_version
) == MLX5_CQE_VERSION_V1
&&
156 in
->type
!= IB_SRQT_BASIC
)
157 in
->user_index
= MLX5_IB_DEFAULT_UIDX
;
165 mlx5_frag_buf_free(dev
->mdev
, &srq
->buf
);
168 mlx5_db_free(dev
->mdev
, &srq
->db
);
172 static void destroy_srq_user(struct ib_pd
*pd
, struct mlx5_ib_srq
*srq
,
173 struct ib_udata
*udata
)
175 mlx5_ib_db_unmap_user(
176 rdma_udata_to_drv_context(
178 struct mlx5_ib_ucontext
,
181 ib_umem_release(srq
->umem
);
185 static void destroy_srq_kernel(struct mlx5_ib_dev
*dev
, struct mlx5_ib_srq
*srq
)
188 mlx5_frag_buf_free(dev
->mdev
, &srq
->buf
);
189 mlx5_db_free(dev
->mdev
, &srq
->db
);
192 int mlx5_ib_create_srq(struct ib_srq
*ib_srq
,
193 struct ib_srq_init_attr
*init_attr
,
194 struct ib_udata
*udata
)
196 struct mlx5_ib_dev
*dev
= to_mdev(ib_srq
->device
);
197 struct mlx5_ib_srq
*srq
= to_msrq(ib_srq
);
201 struct mlx5_srq_attr in
= {};
202 __u32 max_srq_wqes
= 1 << MLX5_CAP_GEN(dev
->mdev
, log_max_srq_sz
);
204 if (init_attr
->srq_type
!= IB_SRQT_BASIC
&&
205 init_attr
->srq_type
!= IB_SRQT_XRC
&&
206 init_attr
->srq_type
!= IB_SRQT_TM
)
209 /* Sanity check SRQ size before proceeding */
210 if (init_attr
->attr
.max_wr
>= max_srq_wqes
) {
211 mlx5_ib_dbg(dev
, "max_wr %d, cap %d\n",
212 init_attr
->attr
.max_wr
,
217 mutex_init(&srq
->mutex
);
218 spin_lock_init(&srq
->lock
);
219 srq
->msrq
.max
= roundup_pow_of_two(init_attr
->attr
.max_wr
+ 1);
220 srq
->msrq
.max_gs
= init_attr
->attr
.max_sge
;
222 desc_size
= sizeof(struct mlx5_wqe_srq_next_seg
) +
223 srq
->msrq
.max_gs
* sizeof(struct mlx5_wqe_data_seg
);
224 if (desc_size
== 0 || srq
->msrq
.max_gs
> desc_size
)
227 desc_size
= roundup_pow_of_two(desc_size
);
228 desc_size
= max_t(size_t, 32, desc_size
);
229 if (desc_size
< sizeof(struct mlx5_wqe_srq_next_seg
))
232 srq
->msrq
.max_avail_gather
= (desc_size
- sizeof(struct mlx5_wqe_srq_next_seg
)) /
233 sizeof(struct mlx5_wqe_data_seg
);
234 srq
->msrq
.wqe_shift
= ilog2(desc_size
);
235 buf_size
= srq
->msrq
.max
* desc_size
;
236 if (buf_size
< desc_size
)
239 in
.type
= init_attr
->srq_type
;
242 err
= create_srq_user(ib_srq
->pd
, srq
, &in
, udata
, buf_size
);
244 err
= create_srq_kernel(dev
, srq
, &in
, buf_size
);
247 mlx5_ib_warn(dev
, "create srq %s failed, err %d\n",
248 udata
? "user" : "kernel", err
);
252 in
.log_size
= ilog2(srq
->msrq
.max
);
253 in
.wqe_shift
= srq
->msrq
.wqe_shift
- 4;
255 in
.flags
|= MLX5_SRQ_FLAG_WQ_SIG
;
257 if (init_attr
->srq_type
== IB_SRQT_XRC
&& init_attr
->ext
.xrc
.xrcd
)
258 in
.xrcd
= to_mxrcd(init_attr
->ext
.xrc
.xrcd
)->xrcdn
;
260 in
.xrcd
= dev
->devr
.xrcdn0
;
262 if (init_attr
->srq_type
== IB_SRQT_TM
) {
263 in
.tm_log_list_size
=
264 ilog2(init_attr
->ext
.tag_matching
.max_num_tags
) + 1;
265 if (in
.tm_log_list_size
>
266 MLX5_CAP_GEN(dev
->mdev
, log_tag_matching_list_sz
)) {
267 mlx5_ib_dbg(dev
, "TM SRQ max_num_tags exceeding limit\n");
269 goto err_usr_kern_srq
;
271 in
.flags
|= MLX5_SRQ_FLAG_RNDV
;
274 if (ib_srq_has_cq(init_attr
->srq_type
))
275 in
.cqn
= to_mcq(init_attr
->ext
.cq
)->mcq
.cqn
;
277 in
.cqn
= to_mcq(dev
->devr
.c0
)->mcq
.cqn
;
279 in
.pd
= to_mpd(ib_srq
->pd
)->pdn
;
280 in
.db_record
= srq
->db
.dma
;
281 err
= mlx5_cmd_create_srq(dev
, &srq
->msrq
, &in
);
284 mlx5_ib_dbg(dev
, "create SRQ failed, err %d\n", err
);
285 goto err_usr_kern_srq
;
288 mlx5_ib_dbg(dev
, "create SRQ with srqn 0x%x\n", srq
->msrq
.srqn
);
290 srq
->msrq
.event
= mlx5_ib_srq_event
;
291 srq
->ibsrq
.ext
.xrc
.srq_num
= srq
->msrq
.srqn
;
294 struct mlx5_ib_create_srq_resp resp
= {
295 .srqn
= srq
->msrq
.srqn
,
298 if (ib_copy_to_udata(udata
, &resp
, min(udata
->outlen
,
300 mlx5_ib_dbg(dev
, "copy to user failed\n");
306 init_attr
->attr
.max_wr
= srq
->msrq
.max
- 1;
311 mlx5_cmd_destroy_srq(dev
, &srq
->msrq
);
315 destroy_srq_user(ib_srq
->pd
, srq
, udata
);
317 destroy_srq_kernel(dev
, srq
);
322 int mlx5_ib_modify_srq(struct ib_srq
*ibsrq
, struct ib_srq_attr
*attr
,
323 enum ib_srq_attr_mask attr_mask
, struct ib_udata
*udata
)
325 struct mlx5_ib_dev
*dev
= to_mdev(ibsrq
->device
);
326 struct mlx5_ib_srq
*srq
= to_msrq(ibsrq
);
329 /* We don't support resizing SRQs yet */
330 if (attr_mask
& IB_SRQ_MAX_WR
)
333 if (attr_mask
& IB_SRQ_LIMIT
) {
334 if (attr
->srq_limit
>= srq
->msrq
.max
)
337 mutex_lock(&srq
->mutex
);
338 ret
= mlx5_cmd_arm_srq(dev
, &srq
->msrq
, attr
->srq_limit
, 1);
339 mutex_unlock(&srq
->mutex
);
348 int mlx5_ib_query_srq(struct ib_srq
*ibsrq
, struct ib_srq_attr
*srq_attr
)
350 struct mlx5_ib_dev
*dev
= to_mdev(ibsrq
->device
);
351 struct mlx5_ib_srq
*srq
= to_msrq(ibsrq
);
353 struct mlx5_srq_attr
*out
;
355 out
= kzalloc(sizeof(*out
), GFP_KERNEL
);
359 ret
= mlx5_cmd_query_srq(dev
, &srq
->msrq
, out
);
363 srq_attr
->srq_limit
= out
->lwm
;
364 srq_attr
->max_wr
= srq
->msrq
.max
- 1;
365 srq_attr
->max_sge
= srq
->msrq
.max_gs
;
372 int mlx5_ib_destroy_srq(struct ib_srq
*srq
, struct ib_udata
*udata
)
374 struct mlx5_ib_dev
*dev
= to_mdev(srq
->device
);
375 struct mlx5_ib_srq
*msrq
= to_msrq(srq
);
378 ret
= mlx5_cmd_destroy_srq(dev
, &msrq
->msrq
);
383 destroy_srq_user(srq
->pd
, msrq
, udata
);
385 destroy_srq_kernel(dev
, msrq
);
389 void mlx5_ib_free_srq_wqe(struct mlx5_ib_srq
*srq
, int wqe_index
)
391 struct mlx5_wqe_srq_next_seg
*next
;
393 /* always called with interrupts disabled. */
394 spin_lock(&srq
->lock
);
396 next
= get_wqe(srq
, srq
->tail
);
397 next
->next_wqe_index
= cpu_to_be16(wqe_index
);
398 srq
->tail
= wqe_index
;
400 spin_unlock(&srq
->lock
);
403 int mlx5_ib_post_srq_recv(struct ib_srq
*ibsrq
, const struct ib_recv_wr
*wr
,
404 const struct ib_recv_wr
**bad_wr
)
406 struct mlx5_ib_srq
*srq
= to_msrq(ibsrq
);
407 struct mlx5_wqe_srq_next_seg
*next
;
408 struct mlx5_wqe_data_seg
*scat
;
409 struct mlx5_ib_dev
*dev
= to_mdev(ibsrq
->device
);
410 struct mlx5_core_dev
*mdev
= dev
->mdev
;
416 spin_lock_irqsave(&srq
->lock
, flags
);
418 if (mdev
->state
== MLX5_DEVICE_STATE_INTERNAL_ERROR
) {
424 for (nreq
= 0; wr
; nreq
++, wr
= wr
->next
) {
425 if (unlikely(wr
->num_sge
> srq
->msrq
.max_gs
)) {
431 if (unlikely(srq
->head
== srq
->tail
)) {
437 srq
->wrid
[srq
->head
] = wr
->wr_id
;
439 next
= get_wqe(srq
, srq
->head
);
440 srq
->head
= be16_to_cpu(next
->next_wqe_index
);
441 scat
= (struct mlx5_wqe_data_seg
*)(next
+ 1);
443 for (i
= 0; i
< wr
->num_sge
; i
++) {
444 scat
[i
].byte_count
= cpu_to_be32(wr
->sg_list
[i
].length
);
445 scat
[i
].lkey
= cpu_to_be32(wr
->sg_list
[i
].lkey
);
446 scat
[i
].addr
= cpu_to_be64(wr
->sg_list
[i
].addr
);
449 if (i
< srq
->msrq
.max_avail_gather
) {
450 scat
[i
].byte_count
= 0;
451 scat
[i
].lkey
= cpu_to_be32(MLX5_INVALID_LKEY
);
457 srq
->wqe_ctr
+= nreq
;
459 /* Make sure that descriptors are written before
464 *srq
->db
.db
= cpu_to_be32(srq
->wqe_ctr
);
467 spin_unlock_irqrestore(&srq
->lock
, flags
);