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>
43 /* not supported currently */
44 static int srq_signature
;
46 static void *get_wqe(struct mlx5_ib_srq
*srq
, int n
)
48 return mlx5_buf_offset(&srq
->buf
, n
<< srq
->msrq
.wqe_shift
);
51 static void mlx5_ib_srq_event(struct mlx5_core_srq
*srq
, enum mlx5_event type
)
53 struct ib_event event
;
54 struct ib_srq
*ibsrq
= &to_mibsrq(srq
)->ibsrq
;
56 if (ibsrq
->event_handler
) {
57 event
.device
= ibsrq
->device
;
58 event
.element
.srq
= ibsrq
;
60 case MLX5_EVENT_TYPE_SRQ_RQ_LIMIT
:
61 event
.event
= IB_EVENT_SRQ_LIMIT_REACHED
;
63 case MLX5_EVENT_TYPE_SRQ_CATAS_ERROR
:
64 event
.event
= IB_EVENT_SRQ_ERR
;
67 pr_warn("mlx5_ib: Unexpected event type %d on SRQ %06x\n",
72 ibsrq
->event_handler(&event
, ibsrq
->srq_context
);
76 static int create_srq_user(struct ib_pd
*pd
, struct mlx5_ib_srq
*srq
,
77 struct mlx5_create_srq_mbox_in
**in
,
78 struct ib_udata
*udata
, int buf_size
, int *inlen
,
81 struct mlx5_ib_dev
*dev
= to_mdev(pd
->device
);
82 struct mlx5_ib_create_srq ucmd
= {};
90 u32 uidx
= MLX5_IB_DEFAULT_UIDX
;
92 ucmdlen
= min(udata
->inlen
, sizeof(ucmd
));
94 if (ib_copy_from_udata(&ucmd
, udata
, ucmdlen
)) {
95 mlx5_ib_dbg(dev
, "failed copy udata\n");
99 if (ucmd
.reserved0
|| ucmd
.reserved1
)
102 if (udata
->inlen
> sizeof(ucmd
) &&
103 !ib_is_udata_cleared(udata
, sizeof(ucmd
),
104 udata
->inlen
- sizeof(ucmd
)))
108 err
= get_srq_user_index(to_mucontext(pd
->uobject
->context
),
109 &ucmd
, udata
->inlen
, &uidx
);
114 srq
->wq_sig
= !!(ucmd
.flags
& MLX5_SRQ_FLAG_SIGNATURE
);
116 srq
->umem
= ib_umem_get(pd
->uobject
->context
, ucmd
.buf_addr
, buf_size
,
118 if (IS_ERR(srq
->umem
)) {
119 mlx5_ib_dbg(dev
, "failed umem get, size %d\n", buf_size
);
120 err
= PTR_ERR(srq
->umem
);
124 mlx5_ib_cont_pages(srq
->umem
, ucmd
.buf_addr
, &npages
,
125 &page_shift
, &ncont
, NULL
);
126 err
= mlx5_ib_get_buf_offset(ucmd
.buf_addr
, page_shift
,
129 mlx5_ib_warn(dev
, "bad offset\n");
133 *inlen
= sizeof(**in
) + sizeof(*(*in
)->pas
) * ncont
;
134 *in
= mlx5_vzalloc(*inlen
);
140 mlx5_ib_populate_pas(dev
, srq
->umem
, page_shift
, (*in
)->pas
, 0);
142 err
= mlx5_ib_db_map_user(to_mucontext(pd
->uobject
->context
),
143 ucmd
.db_addr
, &srq
->db
);
145 mlx5_ib_dbg(dev
, "map doorbell failed\n");
149 (*in
)->ctx
.log_pg_sz
= page_shift
- MLX5_ADAPTER_PAGE_SHIFT
;
150 (*in
)->ctx
.pgoff_cqn
= cpu_to_be32(offset
<< 26);
152 if ((MLX5_CAP_GEN(dev
->mdev
, cqe_version
) == MLX5_CQE_VERSION_V1
) &&
154 xsrqc
= MLX5_ADDR_OF(create_xrc_srq_in
, *in
,
155 xrc_srq_context_entry
);
156 MLX5_SET(xrc_srqc
, xsrqc
, user_index
, uidx
);
165 ib_umem_release(srq
->umem
);
170 static int create_srq_kernel(struct mlx5_ib_dev
*dev
, struct mlx5_ib_srq
*srq
,
171 struct mlx5_create_srq_mbox_in
**in
, int buf_size
,
172 int *inlen
, int is_xrc
)
176 struct mlx5_wqe_srq_next_seg
*next
;
181 err
= mlx5_db_alloc(dev
->mdev
, &srq
->db
);
183 mlx5_ib_warn(dev
, "alloc dbell rec failed\n");
187 if (mlx5_buf_alloc(dev
->mdev
, buf_size
, &srq
->buf
)) {
188 mlx5_ib_dbg(dev
, "buf alloc failed\n");
192 page_shift
= srq
->buf
.page_shift
;
195 srq
->tail
= srq
->msrq
.max
- 1;
198 for (i
= 0; i
< srq
->msrq
.max
; i
++) {
199 next
= get_wqe(srq
, i
);
200 next
->next_wqe_index
=
201 cpu_to_be16((i
+ 1) & (srq
->msrq
.max
- 1));
204 npages
= DIV_ROUND_UP(srq
->buf
.npages
, 1 << (page_shift
- PAGE_SHIFT
));
205 mlx5_ib_dbg(dev
, "buf_size %d, page_shift %d, npages %d, calc npages %d\n",
206 buf_size
, page_shift
, srq
->buf
.npages
, npages
);
207 *inlen
= sizeof(**in
) + sizeof(*(*in
)->pas
) * npages
;
208 *in
= mlx5_vzalloc(*inlen
);
213 mlx5_fill_page_array(&srq
->buf
, (*in
)->pas
);
215 srq
->wrid
= kmalloc(srq
->msrq
.max
* sizeof(u64
), GFP_KERNEL
);
217 mlx5_ib_dbg(dev
, "kmalloc failed %lu\n",
218 (unsigned long)(srq
->msrq
.max
* sizeof(u64
)));
222 srq
->wq_sig
= !!srq_signature
;
224 (*in
)->ctx
.log_pg_sz
= page_shift
- MLX5_ADAPTER_PAGE_SHIFT
;
226 if ((MLX5_CAP_GEN(dev
->mdev
, cqe_version
) == MLX5_CQE_VERSION_V1
) &&
228 xsrqc
= MLX5_ADDR_OF(create_xrc_srq_in
, *in
,
229 xrc_srq_context_entry
);
230 /* 0xffffff means we ask to work with cqe version 0 */
231 MLX5_SET(xrc_srqc
, xsrqc
, user_index
, MLX5_IB_DEFAULT_UIDX
);
240 mlx5_buf_free(dev
->mdev
, &srq
->buf
);
243 mlx5_db_free(dev
->mdev
, &srq
->db
);
247 static void destroy_srq_user(struct ib_pd
*pd
, struct mlx5_ib_srq
*srq
)
249 mlx5_ib_db_unmap_user(to_mucontext(pd
->uobject
->context
), &srq
->db
);
250 ib_umem_release(srq
->umem
);
254 static void destroy_srq_kernel(struct mlx5_ib_dev
*dev
, struct mlx5_ib_srq
*srq
)
257 mlx5_buf_free(dev
->mdev
, &srq
->buf
);
258 mlx5_db_free(dev
->mdev
, &srq
->db
);
261 struct ib_srq
*mlx5_ib_create_srq(struct ib_pd
*pd
,
262 struct ib_srq_init_attr
*init_attr
,
263 struct ib_udata
*udata
)
265 struct mlx5_ib_dev
*dev
= to_mdev(pd
->device
);
266 struct mlx5_ib_srq
*srq
;
270 struct mlx5_create_srq_mbox_in
*uninitialized_var(in
);
271 int uninitialized_var(inlen
);
274 __u32 max_srq_wqes
= 1 << MLX5_CAP_GEN(dev
->mdev
, log_max_srq_sz
);
276 /* Sanity check SRQ size before proceeding */
277 if (init_attr
->attr
.max_wr
>= max_srq_wqes
) {
278 mlx5_ib_dbg(dev
, "max_wr %d, cap %d\n",
279 init_attr
->attr
.max_wr
,
281 return ERR_PTR(-EINVAL
);
284 srq
= kmalloc(sizeof(*srq
), GFP_KERNEL
);
286 return ERR_PTR(-ENOMEM
);
288 mutex_init(&srq
->mutex
);
289 spin_lock_init(&srq
->lock
);
290 srq
->msrq
.max
= roundup_pow_of_two(init_attr
->attr
.max_wr
+ 1);
291 srq
->msrq
.max_gs
= init_attr
->attr
.max_sge
;
293 desc_size
= sizeof(struct mlx5_wqe_srq_next_seg
) +
294 srq
->msrq
.max_gs
* sizeof(struct mlx5_wqe_data_seg
);
295 desc_size
= roundup_pow_of_two(desc_size
);
296 desc_size
= max_t(int, 32, desc_size
);
297 srq
->msrq
.max_avail_gather
= (desc_size
- sizeof(struct mlx5_wqe_srq_next_seg
)) /
298 sizeof(struct mlx5_wqe_data_seg
);
299 srq
->msrq
.wqe_shift
= ilog2(desc_size
);
300 buf_size
= srq
->msrq
.max
* desc_size
;
301 mlx5_ib_dbg(dev
, "desc_size 0x%x, req wr 0x%x, srq size 0x%x, max_gs 0x%x, max_avail_gather 0x%x\n",
302 desc_size
, init_attr
->attr
.max_wr
, srq
->msrq
.max
, srq
->msrq
.max_gs
,
303 srq
->msrq
.max_avail_gather
);
305 is_xrc
= (init_attr
->srq_type
== IB_SRQT_XRC
);
308 err
= create_srq_user(pd
, srq
, &in
, udata
, buf_size
, &inlen
,
311 err
= create_srq_kernel(dev
, srq
, &in
, buf_size
, &inlen
,
315 mlx5_ib_warn(dev
, "create srq %s failed, err %d\n",
316 pd
->uobject
? "user" : "kernel", err
);
320 in
->ctx
.state_log_sz
= ilog2(srq
->msrq
.max
);
321 flgs
= ((srq
->msrq
.wqe_shift
- 4) | (is_xrc
<< 5) | (srq
->wq_sig
<< 7)) << 24;
324 xrcdn
= to_mxrcd(init_attr
->ext
.xrc
.xrcd
)->xrcdn
;
325 in
->ctx
.pgoff_cqn
|= cpu_to_be32(to_mcq(init_attr
->ext
.xrc
.cq
)->mcq
.cqn
);
326 } else if (init_attr
->srq_type
== IB_SRQT_BASIC
) {
327 xrcdn
= to_mxrcd(dev
->devr
.x0
)->xrcdn
;
328 in
->ctx
.pgoff_cqn
|= cpu_to_be32(to_mcq(dev
->devr
.c0
)->mcq
.cqn
);
331 in
->ctx
.flags_xrcd
= cpu_to_be32((flgs
& 0xFF000000) | (xrcdn
& 0xFFFFFF));
333 in
->ctx
.pd
= cpu_to_be32(to_mpd(pd
)->pdn
);
334 in
->ctx
.db_record
= cpu_to_be64(srq
->db
.dma
);
335 err
= mlx5_core_create_srq(dev
->mdev
, &srq
->msrq
, in
, inlen
, is_xrc
);
338 mlx5_ib_dbg(dev
, "create SRQ failed, err %d\n", err
);
339 goto err_usr_kern_srq
;
342 mlx5_ib_dbg(dev
, "create SRQ with srqn 0x%x\n", srq
->msrq
.srqn
);
344 srq
->msrq
.event
= mlx5_ib_srq_event
;
345 srq
->ibsrq
.ext
.xrc
.srq_num
= srq
->msrq
.srqn
;
348 if (ib_copy_to_udata(udata
, &srq
->msrq
.srqn
, sizeof(__u32
))) {
349 mlx5_ib_dbg(dev
, "copy to user failed\n");
354 init_attr
->attr
.max_wr
= srq
->msrq
.max
- 1;
359 mlx5_core_destroy_srq(dev
->mdev
, &srq
->msrq
);
363 destroy_srq_user(pd
, srq
);
365 destroy_srq_kernel(dev
, srq
);
373 int mlx5_ib_modify_srq(struct ib_srq
*ibsrq
, struct ib_srq_attr
*attr
,
374 enum ib_srq_attr_mask attr_mask
, struct ib_udata
*udata
)
376 struct mlx5_ib_dev
*dev
= to_mdev(ibsrq
->device
);
377 struct mlx5_ib_srq
*srq
= to_msrq(ibsrq
);
380 /* We don't support resizing SRQs yet */
381 if (attr_mask
& IB_SRQ_MAX_WR
)
384 if (attr_mask
& IB_SRQ_LIMIT
) {
385 if (attr
->srq_limit
>= srq
->msrq
.max
)
388 mutex_lock(&srq
->mutex
);
389 ret
= mlx5_core_arm_srq(dev
->mdev
, &srq
->msrq
, attr
->srq_limit
, 1);
390 mutex_unlock(&srq
->mutex
);
399 int mlx5_ib_query_srq(struct ib_srq
*ibsrq
, struct ib_srq_attr
*srq_attr
)
401 struct mlx5_ib_dev
*dev
= to_mdev(ibsrq
->device
);
402 struct mlx5_ib_srq
*srq
= to_msrq(ibsrq
);
404 struct mlx5_query_srq_mbox_out
*out
;
406 out
= kzalloc(sizeof(*out
), GFP_KERNEL
);
410 ret
= mlx5_core_query_srq(dev
->mdev
, &srq
->msrq
, out
);
414 srq_attr
->srq_limit
= be16_to_cpu(out
->ctx
.lwm
);
415 srq_attr
->max_wr
= srq
->msrq
.max
- 1;
416 srq_attr
->max_sge
= srq
->msrq
.max_gs
;
423 int mlx5_ib_destroy_srq(struct ib_srq
*srq
)
425 struct mlx5_ib_dev
*dev
= to_mdev(srq
->device
);
426 struct mlx5_ib_srq
*msrq
= to_msrq(srq
);
428 mlx5_core_destroy_srq(dev
->mdev
, &msrq
->msrq
);
431 mlx5_ib_db_unmap_user(to_mucontext(srq
->uobject
->context
), &msrq
->db
);
432 ib_umem_release(msrq
->umem
);
434 destroy_srq_kernel(dev
, msrq
);
441 void mlx5_ib_free_srq_wqe(struct mlx5_ib_srq
*srq
, int wqe_index
)
443 struct mlx5_wqe_srq_next_seg
*next
;
445 /* always called with interrupts disabled. */
446 spin_lock(&srq
->lock
);
448 next
= get_wqe(srq
, srq
->tail
);
449 next
->next_wqe_index
= cpu_to_be16(wqe_index
);
450 srq
->tail
= wqe_index
;
452 spin_unlock(&srq
->lock
);
455 int mlx5_ib_post_srq_recv(struct ib_srq
*ibsrq
, struct ib_recv_wr
*wr
,
456 struct ib_recv_wr
**bad_wr
)
458 struct mlx5_ib_srq
*srq
= to_msrq(ibsrq
);
459 struct mlx5_wqe_srq_next_seg
*next
;
460 struct mlx5_wqe_data_seg
*scat
;
466 spin_lock_irqsave(&srq
->lock
, flags
);
468 for (nreq
= 0; wr
; nreq
++, wr
= wr
->next
) {
469 if (unlikely(wr
->num_sge
> srq
->msrq
.max_gs
)) {
475 if (unlikely(srq
->head
== srq
->tail
)) {
481 srq
->wrid
[srq
->head
] = wr
->wr_id
;
483 next
= get_wqe(srq
, srq
->head
);
484 srq
->head
= be16_to_cpu(next
->next_wqe_index
);
485 scat
= (struct mlx5_wqe_data_seg
*)(next
+ 1);
487 for (i
= 0; i
< wr
->num_sge
; i
++) {
488 scat
[i
].byte_count
= cpu_to_be32(wr
->sg_list
[i
].length
);
489 scat
[i
].lkey
= cpu_to_be32(wr
->sg_list
[i
].lkey
);
490 scat
[i
].addr
= cpu_to_be64(wr
->sg_list
[i
].addr
);
493 if (i
< srq
->msrq
.max_avail_gather
) {
494 scat
[i
].byte_count
= 0;
495 scat
[i
].lkey
= cpu_to_be32(MLX5_INVALID_LKEY
);
501 srq
->wqe_ctr
+= nreq
;
503 /* Make sure that descriptors are written before
508 *srq
->db
.db
= cpu_to_be32(srq
->wqe_ctr
);
511 spin_unlock_irqrestore(&srq
->lock
, flags
);