1 // SPDX-License-Identifier: GPL-2.0 or BSD-3-Clause
3 /* Authors: Bernard Metzler <bmt@zurich.ibm.com> */
4 /* Copyright (c) 2008-2019, IBM Corporation */
6 #include <linux/errno.h>
7 #include <linux/types.h>
8 #include <linux/uaccess.h>
9 #include <linux/vmalloc.h>
10 #include <linux/xarray.h>
12 #include <rdma/iw_cm.h>
13 #include <rdma/ib_verbs.h>
14 #include <rdma/ib_user_verbs.h>
15 #include <rdma/uverbs_ioctl.h>
18 #include "siw_verbs.h"
21 static int ib_qp_state_to_siw_qp_state
[IB_QPS_ERR
+ 1] = {
22 [IB_QPS_RESET
] = SIW_QP_STATE_IDLE
,
23 [IB_QPS_INIT
] = SIW_QP_STATE_IDLE
,
24 [IB_QPS_RTR
] = SIW_QP_STATE_RTR
,
25 [IB_QPS_RTS
] = SIW_QP_STATE_RTS
,
26 [IB_QPS_SQD
] = SIW_QP_STATE_CLOSING
,
27 [IB_QPS_SQE
] = SIW_QP_STATE_TERMINATE
,
28 [IB_QPS_ERR
] = SIW_QP_STATE_ERROR
31 static char ib_qp_state_to_string
[IB_QPS_ERR
+ 1][sizeof("RESET")] = {
32 [IB_QPS_RESET
] = "RESET", [IB_QPS_INIT
] = "INIT", [IB_QPS_RTR
] = "RTR",
33 [IB_QPS_RTS
] = "RTS", [IB_QPS_SQD
] = "SQD", [IB_QPS_SQE
] = "SQE",
37 void siw_mmap_free(struct rdma_user_mmap_entry
*rdma_entry
)
39 struct siw_user_mmap_entry
*entry
= to_siw_mmap_entry(rdma_entry
);
44 int siw_mmap(struct ib_ucontext
*ctx
, struct vm_area_struct
*vma
)
46 struct siw_ucontext
*uctx
= to_siw_ctx(ctx
);
47 size_t size
= vma
->vm_end
- vma
->vm_start
;
48 struct rdma_user_mmap_entry
*rdma_entry
;
49 struct siw_user_mmap_entry
*entry
;
53 * Must be page aligned
55 if (vma
->vm_start
& (PAGE_SIZE
- 1)) {
56 pr_warn("siw: mmap not page aligned\n");
59 rdma_entry
= rdma_user_mmap_entry_get(&uctx
->base_ucontext
, vma
);
61 siw_dbg(&uctx
->sdev
->base_dev
, "mmap lookup failed: %lu, %#zx\n",
65 entry
= to_siw_mmap_entry(rdma_entry
);
67 rv
= remap_vmalloc_range(vma
, entry
->address
, 0);
69 pr_warn("remap_vmalloc_range failed: %lu, %zu\n", vma
->vm_pgoff
,
74 rdma_user_mmap_entry_put(rdma_entry
);
79 int siw_alloc_ucontext(struct ib_ucontext
*base_ctx
, struct ib_udata
*udata
)
81 struct siw_device
*sdev
= to_siw_dev(base_ctx
->device
);
82 struct siw_ucontext
*ctx
= to_siw_ctx(base_ctx
);
83 struct siw_uresp_alloc_ctx uresp
= {};
86 if (atomic_inc_return(&sdev
->num_ctx
) > SIW_MAX_CONTEXT
) {
92 uresp
.dev_id
= sdev
->vendor_part_id
;
94 if (udata
->outlen
< sizeof(uresp
)) {
98 rv
= ib_copy_to_udata(udata
, &uresp
, sizeof(uresp
));
102 siw_dbg(base_ctx
->device
, "success. now %d context(s)\n",
103 atomic_read(&sdev
->num_ctx
));
108 atomic_dec(&sdev
->num_ctx
);
109 siw_dbg(base_ctx
->device
, "failure %d. now %d context(s)\n", rv
,
110 atomic_read(&sdev
->num_ctx
));
115 void siw_dealloc_ucontext(struct ib_ucontext
*base_ctx
)
117 struct siw_ucontext
*uctx
= to_siw_ctx(base_ctx
);
119 atomic_dec(&uctx
->sdev
->num_ctx
);
122 int siw_query_device(struct ib_device
*base_dev
, struct ib_device_attr
*attr
,
123 struct ib_udata
*udata
)
125 struct siw_device
*sdev
= to_siw_dev(base_dev
);
127 if (udata
->inlen
|| udata
->outlen
)
130 memset(attr
, 0, sizeof(*attr
));
132 /* Revisit atomic caps if RFC 7306 gets supported */
133 attr
->atomic_cap
= 0;
134 attr
->device_cap_flags
=
135 IB_DEVICE_MEM_MGT_EXTENSIONS
| IB_DEVICE_ALLOW_USER_UNREG
;
136 attr
->max_cq
= sdev
->attrs
.max_cq
;
137 attr
->max_cqe
= sdev
->attrs
.max_cqe
;
138 attr
->max_fast_reg_page_list_len
= SIW_MAX_SGE_PBL
;
139 attr
->max_mr
= sdev
->attrs
.max_mr
;
140 attr
->max_mw
= sdev
->attrs
.max_mw
;
141 attr
->max_mr_size
= ~0ull;
142 attr
->max_pd
= sdev
->attrs
.max_pd
;
143 attr
->max_qp
= sdev
->attrs
.max_qp
;
144 attr
->max_qp_init_rd_atom
= sdev
->attrs
.max_ird
;
145 attr
->max_qp_rd_atom
= sdev
->attrs
.max_ord
;
146 attr
->max_qp_wr
= sdev
->attrs
.max_qp_wr
;
147 attr
->max_recv_sge
= sdev
->attrs
.max_sge
;
148 attr
->max_res_rd_atom
= sdev
->attrs
.max_qp
* sdev
->attrs
.max_ird
;
149 attr
->max_send_sge
= sdev
->attrs
.max_sge
;
150 attr
->max_sge_rd
= sdev
->attrs
.max_sge_rd
;
151 attr
->max_srq
= sdev
->attrs
.max_srq
;
152 attr
->max_srq_sge
= sdev
->attrs
.max_srq_sge
;
153 attr
->max_srq_wr
= sdev
->attrs
.max_srq_wr
;
154 attr
->page_size_cap
= PAGE_SIZE
;
155 attr
->vendor_id
= SIW_VENDOR_ID
;
156 attr
->vendor_part_id
= sdev
->vendor_part_id
;
158 memcpy(&attr
->sys_image_guid
, sdev
->netdev
->dev_addr
, 6);
163 int siw_query_port(struct ib_device
*base_dev
, u8 port
,
164 struct ib_port_attr
*attr
)
166 struct siw_device
*sdev
= to_siw_dev(base_dev
);
169 memset(attr
, 0, sizeof(*attr
));
171 rv
= ib_get_eth_speed(base_dev
, port
, &attr
->active_speed
,
172 &attr
->active_width
);
173 attr
->gid_tbl_len
= 1;
174 attr
->max_msg_sz
= -1;
175 attr
->max_mtu
= ib_mtu_int_to_enum(sdev
->netdev
->mtu
);
176 attr
->active_mtu
= ib_mtu_int_to_enum(sdev
->netdev
->mtu
);
177 attr
->phys_state
= sdev
->state
== IB_PORT_ACTIVE
?
178 IB_PORT_PHYS_STATE_LINK_UP
: IB_PORT_PHYS_STATE_DISABLED
;
179 attr
->port_cap_flags
= IB_PORT_CM_SUP
| IB_PORT_DEVICE_MGMT_SUP
;
180 attr
->state
= sdev
->state
;
185 * attr->bad_pkey_cntr = 0;
186 * attr->qkey_viol_cntr = 0;
189 * attr->max_vl_num = 0;
191 * attr->subnet_timeout = 0;
192 * attr->init_type_repy = 0;
197 int siw_get_port_immutable(struct ib_device
*base_dev
, u8 port
,
198 struct ib_port_immutable
*port_immutable
)
200 struct ib_port_attr attr
;
201 int rv
= siw_query_port(base_dev
, port
, &attr
);
206 port_immutable
->gid_tbl_len
= attr
.gid_tbl_len
;
207 port_immutable
->core_cap_flags
= RDMA_CORE_PORT_IWARP
;
212 int siw_query_gid(struct ib_device
*base_dev
, u8 port
, int idx
,
215 struct siw_device
*sdev
= to_siw_dev(base_dev
);
217 /* subnet_prefix == interface_id == 0; */
218 memset(gid
, 0, sizeof(*gid
));
219 memcpy(&gid
->raw
[0], sdev
->netdev
->dev_addr
, 6);
224 int siw_alloc_pd(struct ib_pd
*pd
, struct ib_udata
*udata
)
226 struct siw_device
*sdev
= to_siw_dev(pd
->device
);
228 if (atomic_inc_return(&sdev
->num_pd
) > SIW_MAX_PD
) {
229 atomic_dec(&sdev
->num_pd
);
232 siw_dbg_pd(pd
, "now %d PD's(s)\n", atomic_read(&sdev
->num_pd
));
237 int siw_dealloc_pd(struct ib_pd
*pd
, struct ib_udata
*udata
)
239 struct siw_device
*sdev
= to_siw_dev(pd
->device
);
241 siw_dbg_pd(pd
, "free PD\n");
242 atomic_dec(&sdev
->num_pd
);
246 void siw_qp_get_ref(struct ib_qp
*base_qp
)
248 siw_qp_get(to_siw_qp(base_qp
));
251 void siw_qp_put_ref(struct ib_qp
*base_qp
)
253 siw_qp_put(to_siw_qp(base_qp
));
256 static struct rdma_user_mmap_entry
*
257 siw_mmap_entry_insert(struct siw_ucontext
*uctx
,
258 void *address
, size_t length
,
261 struct siw_user_mmap_entry
*entry
= kzalloc(sizeof(*entry
), GFP_KERNEL
);
264 *offset
= SIW_INVAL_UOBJ_KEY
;
268 entry
->address
= address
;
270 rv
= rdma_user_mmap_entry_insert(&uctx
->base_ucontext
,
278 *offset
= rdma_user_mmap_get_offset(&entry
->rdma_entry
);
280 return &entry
->rdma_entry
;
286 * Create QP of requested size on given device.
288 * @pd: Protection Domain
289 * @attrs: Initial QP attributes.
290 * @udata: used to provide QP ID, SQ and RQ size back to user.
293 struct ib_qp
*siw_create_qp(struct ib_pd
*pd
,
294 struct ib_qp_init_attr
*attrs
,
295 struct ib_udata
*udata
)
297 struct siw_qp
*qp
= NULL
;
298 struct ib_device
*base_dev
= pd
->device
;
299 struct siw_device
*sdev
= to_siw_dev(base_dev
);
300 struct siw_ucontext
*uctx
=
301 rdma_udata_to_drv_context(udata
, struct siw_ucontext
,
303 struct siw_cq
*scq
= NULL
, *rcq
= NULL
;
305 int num_sqe
, num_rqe
, rv
= 0;
308 siw_dbg(base_dev
, "create new QP\n");
310 if (attrs
->create_flags
)
311 return ERR_PTR(-EOPNOTSUPP
);
313 if (atomic_inc_return(&sdev
->num_qp
) > SIW_MAX_QP
) {
314 siw_dbg(base_dev
, "too many QP's\n");
318 if (attrs
->qp_type
!= IB_QPT_RC
) {
319 siw_dbg(base_dev
, "only RC QP's supported\n");
323 if ((attrs
->cap
.max_send_wr
> SIW_MAX_QP_WR
) ||
324 (attrs
->cap
.max_recv_wr
> SIW_MAX_QP_WR
) ||
325 (attrs
->cap
.max_send_sge
> SIW_MAX_SGE
) ||
326 (attrs
->cap
.max_recv_sge
> SIW_MAX_SGE
)) {
327 siw_dbg(base_dev
, "QP size error\n");
331 if (attrs
->cap
.max_inline_data
> SIW_MAX_INLINE
) {
332 siw_dbg(base_dev
, "max inline send: %d > %d\n",
333 attrs
->cap
.max_inline_data
, (int)SIW_MAX_INLINE
);
338 * NOTE: we allow for zero element SQ and RQ WQE's SGL's
339 * but not for a QP unable to hold any WQE (SQ + RQ)
341 if (attrs
->cap
.max_send_wr
+ attrs
->cap
.max_recv_wr
== 0) {
342 siw_dbg(base_dev
, "QP must have send or receive queue\n");
346 scq
= to_siw_cq(attrs
->send_cq
);
347 rcq
= to_siw_cq(attrs
->recv_cq
);
349 if (!scq
|| (!rcq
&& !attrs
->srq
)) {
350 siw_dbg(base_dev
, "send CQ or receive CQ invalid\n");
354 qp
= kzalloc(sizeof(*qp
), GFP_KERNEL
);
359 init_rwsem(&qp
->state_lock
);
360 spin_lock_init(&qp
->sq_lock
);
361 spin_lock_init(&qp
->rq_lock
);
362 spin_lock_init(&qp
->orq_lock
);
364 rv
= siw_qp_add(sdev
, qp
);
368 /* All queue indices are derived from modulo operations
369 * on a free running 'get' (consumer) and 'put' (producer)
370 * unsigned counter. Having queue sizes at power of two
371 * avoids handling counter wrap around.
373 num_sqe
= roundup_pow_of_two(attrs
->cap
.max_send_wr
);
374 num_rqe
= roundup_pow_of_two(attrs
->cap
.max_recv_wr
);
377 qp
->sendq
= vmalloc_user(num_sqe
* sizeof(struct siw_sqe
));
379 qp
->sendq
= vzalloc(num_sqe
* sizeof(struct siw_sqe
));
381 if (qp
->sendq
== NULL
) {
382 siw_dbg(base_dev
, "SQ size %d alloc failed\n", num_sqe
);
386 if (attrs
->sq_sig_type
!= IB_SIGNAL_REQ_WR
) {
387 if (attrs
->sq_sig_type
== IB_SIGNAL_ALL_WR
)
388 qp
->attrs
.flags
|= SIW_SIGNAL_ALL_WR
;
401 * Verbs 6.3.7: ignore RQ size, if SRQ present
402 * Verbs 6.3.5: do not check PD of SRQ against PD of QP
404 qp
->srq
= to_siw_srq(attrs
->srq
);
405 qp
->attrs
.rq_size
= 0;
406 siw_dbg(base_dev
, "QP [%u]: SRQ attached\n",
408 } else if (num_rqe
) {
411 vmalloc_user(num_rqe
* sizeof(struct siw_rqe
));
413 qp
->recvq
= vzalloc(num_rqe
* sizeof(struct siw_rqe
));
415 if (qp
->recvq
== NULL
) {
416 siw_dbg(base_dev
, "RQ size %d alloc failed\n", num_rqe
);
420 qp
->attrs
.rq_size
= num_rqe
;
422 qp
->attrs
.sq_size
= num_sqe
;
423 qp
->attrs
.sq_max_sges
= attrs
->cap
.max_send_sge
;
424 qp
->attrs
.rq_max_sges
= attrs
->cap
.max_recv_sge
;
426 /* Make those two tunables fixed for now. */
427 qp
->tx_ctx
.gso_seg_limit
= 1;
428 qp
->tx_ctx
.zcopy_tx
= zcopy_tx
;
430 qp
->attrs
.state
= SIW_QP_STATE_IDLE
;
433 struct siw_uresp_create_qp uresp
= {};
435 uresp
.num_sqe
= num_sqe
;
436 uresp
.num_rqe
= num_rqe
;
437 uresp
.qp_id
= qp_id(qp
);
440 length
= num_sqe
* sizeof(struct siw_sqe
);
442 siw_mmap_entry_insert(uctx
, qp
->sendq
,
443 length
, &uresp
.sq_key
);
451 length
= num_rqe
* sizeof(struct siw_rqe
);
453 siw_mmap_entry_insert(uctx
, qp
->recvq
,
454 length
, &uresp
.rq_key
);
456 uresp
.sq_key
= SIW_INVAL_UOBJ_KEY
;
462 if (udata
->outlen
< sizeof(uresp
)) {
466 rv
= ib_copy_to_udata(udata
, &uresp
, sizeof(uresp
));
470 qp
->tx_cpu
= siw_get_tx_cpu(sdev
);
471 if (qp
->tx_cpu
< 0) {
475 INIT_LIST_HEAD(&qp
->devq
);
476 spin_lock_irqsave(&sdev
->lock
, flags
);
477 list_add_tail(&qp
->devq
, &sdev
->qp_list
);
478 spin_unlock_irqrestore(&sdev
->lock
, flags
);
483 xa_erase(&sdev
->qp_xa
, qp_id(qp
));
487 rdma_user_mmap_entry_remove(qp
->sq_entry
);
488 rdma_user_mmap_entry_remove(qp
->rq_entry
);
494 atomic_dec(&sdev
->num_qp
);
500 * Minimum siw_query_qp() verb interface.
502 * @qp_attr_mask is not used but all available information is provided
504 int siw_query_qp(struct ib_qp
*base_qp
, struct ib_qp_attr
*qp_attr
,
505 int qp_attr_mask
, struct ib_qp_init_attr
*qp_init_attr
)
508 struct siw_device
*sdev
;
510 if (base_qp
&& qp_attr
&& qp_init_attr
) {
511 qp
= to_siw_qp(base_qp
);
512 sdev
= to_siw_dev(base_qp
->device
);
516 qp_attr
->cap
.max_inline_data
= SIW_MAX_INLINE
;
517 qp_attr
->cap
.max_send_wr
= qp
->attrs
.sq_size
;
518 qp_attr
->cap
.max_send_sge
= qp
->attrs
.sq_max_sges
;
519 qp_attr
->cap
.max_recv_wr
= qp
->attrs
.rq_size
;
520 qp_attr
->cap
.max_recv_sge
= qp
->attrs
.rq_max_sges
;
521 qp_attr
->path_mtu
= ib_mtu_int_to_enum(sdev
->netdev
->mtu
);
522 qp_attr
->max_rd_atomic
= qp
->attrs
.irq_size
;
523 qp_attr
->max_dest_rd_atomic
= qp
->attrs
.orq_size
;
525 qp_attr
->qp_access_flags
= IB_ACCESS_LOCAL_WRITE
|
526 IB_ACCESS_REMOTE_WRITE
|
527 IB_ACCESS_REMOTE_READ
;
529 qp_init_attr
->qp_type
= base_qp
->qp_type
;
530 qp_init_attr
->send_cq
= base_qp
->send_cq
;
531 qp_init_attr
->recv_cq
= base_qp
->recv_cq
;
532 qp_init_attr
->srq
= base_qp
->srq
;
534 qp_init_attr
->cap
= qp_attr
->cap
;
539 int siw_verbs_modify_qp(struct ib_qp
*base_qp
, struct ib_qp_attr
*attr
,
540 int attr_mask
, struct ib_udata
*udata
)
542 struct siw_qp_attrs new_attrs
;
543 enum siw_qp_attr_mask siw_attr_mask
= 0;
544 struct siw_qp
*qp
= to_siw_qp(base_qp
);
550 if (attr_mask
& ~IB_QP_ATTR_STANDARD_BITS
)
553 memset(&new_attrs
, 0, sizeof(new_attrs
));
555 if (attr_mask
& IB_QP_ACCESS_FLAGS
) {
556 siw_attr_mask
= SIW_QP_ATTR_ACCESS_FLAGS
;
558 if (attr
->qp_access_flags
& IB_ACCESS_REMOTE_READ
)
559 new_attrs
.flags
|= SIW_RDMA_READ_ENABLED
;
560 if (attr
->qp_access_flags
& IB_ACCESS_REMOTE_WRITE
)
561 new_attrs
.flags
|= SIW_RDMA_WRITE_ENABLED
;
562 if (attr
->qp_access_flags
& IB_ACCESS_MW_BIND
)
563 new_attrs
.flags
|= SIW_RDMA_BIND_ENABLED
;
565 if (attr_mask
& IB_QP_STATE
) {
566 siw_dbg_qp(qp
, "desired IB QP state: %s\n",
567 ib_qp_state_to_string
[attr
->qp_state
]);
569 new_attrs
.state
= ib_qp_state_to_siw_qp_state
[attr
->qp_state
];
571 if (new_attrs
.state
> SIW_QP_STATE_RTS
)
572 qp
->tx_ctx
.tx_suspend
= 1;
574 siw_attr_mask
|= SIW_QP_ATTR_STATE
;
579 down_write(&qp
->state_lock
);
581 rv
= siw_qp_modify(qp
, &new_attrs
, siw_attr_mask
);
583 up_write(&qp
->state_lock
);
588 int siw_destroy_qp(struct ib_qp
*base_qp
, struct ib_udata
*udata
)
590 struct siw_qp
*qp
= to_siw_qp(base_qp
);
591 struct siw_ucontext
*uctx
=
592 rdma_udata_to_drv_context(udata
, struct siw_ucontext
,
594 struct siw_qp_attrs qp_attrs
;
596 siw_dbg_qp(qp
, "state %d\n", qp
->attrs
.state
);
599 * Mark QP as in process of destruction to prevent from
600 * any async callbacks to RDMA core
602 qp
->attrs
.flags
|= SIW_QP_IN_DESTROY
;
603 qp
->rx_stream
.rx_suspend
= 1;
606 rdma_user_mmap_entry_remove(qp
->sq_entry
);
607 rdma_user_mmap_entry_remove(qp
->rq_entry
);
610 down_write(&qp
->state_lock
);
612 qp_attrs
.state
= SIW_QP_STATE_ERROR
;
613 siw_qp_modify(qp
, &qp_attrs
, SIW_QP_ATTR_STATE
);
616 siw_cep_put(qp
->cep
);
619 up_write(&qp
->state_lock
);
621 kfree(qp
->tx_ctx
.mpa_crc_hd
);
622 kfree(qp
->rx_stream
.mpa_crc_hd
);
624 qp
->scq
= qp
->rcq
= NULL
;
632 * siw_copy_inline_sgl()
634 * Prepare sgl of inlined data for sending. For userland callers
635 * function checks if given buffer addresses and len's are within
636 * process context bounds.
637 * Data from all provided sge's are copied together into the wqe,
638 * referenced by a single sge.
640 static int siw_copy_inline_sgl(const struct ib_send_wr
*core_wr
,
643 struct ib_sge
*core_sge
= core_wr
->sg_list
;
644 void *kbuf
= &sqe
->sge
[1];
645 int num_sge
= core_wr
->num_sge
, bytes
= 0;
647 sqe
->sge
[0].laddr
= (uintptr_t)kbuf
;
648 sqe
->sge
[0].lkey
= 0;
651 if (!core_sge
->length
) {
655 bytes
+= core_sge
->length
;
656 if (bytes
> SIW_MAX_INLINE
) {
660 memcpy(kbuf
, (void *)(uintptr_t)core_sge
->addr
,
663 kbuf
+= core_sge
->length
;
666 sqe
->sge
[0].length
= bytes
> 0 ? bytes
: 0;
667 sqe
->num_sge
= bytes
> 0 ? 1 : 0;
672 /* Complete SQ WR's without processing */
673 static int siw_sq_flush_wr(struct siw_qp
*qp
, const struct ib_send_wr
*wr
,
674 const struct ib_send_wr
**bad_wr
)
676 struct siw_sqe sqe
= {};
681 sqe
.opcode
= wr
->opcode
;
682 rv
= siw_sqe_complete(qp
, &sqe
, 0, SIW_WC_WR_FLUSH_ERR
);
693 /* Complete RQ WR's without processing */
694 static int siw_rq_flush_wr(struct siw_qp
*qp
, const struct ib_recv_wr
*wr
,
695 const struct ib_recv_wr
**bad_wr
)
697 struct siw_rqe rqe
= {};
702 rv
= siw_rqe_complete(qp
, &rqe
, 0, 0, SIW_WC_WR_FLUSH_ERR
);
716 * Post a list of S-WR's to a SQ.
718 * @base_qp: Base QP contained in siw QP
719 * @wr: Null terminated list of user WR's
720 * @bad_wr: Points to failing WR in case of synchronous failure.
722 int siw_post_send(struct ib_qp
*base_qp
, const struct ib_send_wr
*wr
,
723 const struct ib_send_wr
**bad_wr
)
725 struct siw_qp
*qp
= to_siw_qp(base_qp
);
726 struct siw_wqe
*wqe
= tx_wqe(qp
);
731 if (wr
&& !rdma_is_kernel_res(&qp
->base_qp
.res
)) {
732 siw_dbg_qp(qp
, "wr must be empty for user mapped sq\n");
738 * Try to acquire QP state lock. Must be non-blocking
739 * to accommodate kernel clients needs.
741 if (!down_read_trylock(&qp
->state_lock
)) {
742 if (qp
->attrs
.state
== SIW_QP_STATE_ERROR
) {
744 * ERROR state is final, so we can be sure
745 * this state will not change as long as the QP
748 * This handles an ib_drain_sq() call with
749 * a concurrent request to set the QP state
752 rv
= siw_sq_flush_wr(qp
, wr
, bad_wr
);
754 siw_dbg_qp(qp
, "QP locked, state %d\n",
761 if (unlikely(qp
->attrs
.state
!= SIW_QP_STATE_RTS
)) {
762 if (qp
->attrs
.state
== SIW_QP_STATE_ERROR
) {
764 * Immediately flush this WR to CQ, if QP
765 * is in ERROR state. SQ is guaranteed to
766 * be empty, so WR complets in-order.
768 * Typically triggered by ib_drain_sq().
770 rv
= siw_sq_flush_wr(qp
, wr
, bad_wr
);
772 siw_dbg_qp(qp
, "QP out of state %d\n",
777 up_read(&qp
->state_lock
);
780 spin_lock_irqsave(&qp
->sq_lock
, flags
);
783 u32 idx
= qp
->sq_put
% qp
->attrs
.sq_size
;
784 struct siw_sqe
*sqe
= &qp
->sendq
[idx
];
787 siw_dbg_qp(qp
, "sq full\n");
791 if (wr
->num_sge
> qp
->attrs
.sq_max_sges
) {
792 siw_dbg_qp(qp
, "too many sge's: %d\n", wr
->num_sge
);
798 if ((wr
->send_flags
& IB_SEND_SIGNALED
) ||
799 (qp
->attrs
.flags
& SIW_SIGNAL_ALL_WR
))
800 sqe
->flags
|= SIW_WQE_SIGNALLED
;
802 if (wr
->send_flags
& IB_SEND_FENCE
)
803 sqe
->flags
|= SIW_WQE_READ_FENCE
;
805 switch (wr
->opcode
) {
807 case IB_WR_SEND_WITH_INV
:
808 if (wr
->send_flags
& IB_SEND_SOLICITED
)
809 sqe
->flags
|= SIW_WQE_SOLICITED
;
811 if (!(wr
->send_flags
& IB_SEND_INLINE
)) {
812 siw_copy_sgl(wr
->sg_list
, sqe
->sge
,
814 sqe
->num_sge
= wr
->num_sge
;
816 rv
= siw_copy_inline_sgl(wr
, sqe
);
821 sqe
->flags
|= SIW_WQE_INLINE
;
824 if (wr
->opcode
== IB_WR_SEND
)
825 sqe
->opcode
= SIW_OP_SEND
;
827 sqe
->opcode
= SIW_OP_SEND_REMOTE_INV
;
828 sqe
->rkey
= wr
->ex
.invalidate_rkey
;
832 case IB_WR_RDMA_READ_WITH_INV
:
833 case IB_WR_RDMA_READ
:
835 * iWarp restricts RREAD sink to SGL containing
836 * 1 SGE only. we could relax to SGL with multiple
837 * elements referring the SAME ltag or even sending
838 * a private per-rreq tag referring to a checked
839 * local sgl with MULTIPLE ltag's.
841 if (unlikely(wr
->num_sge
!= 1)) {
845 siw_copy_sgl(wr
->sg_list
, &sqe
->sge
[0], 1);
847 * NOTE: zero length RREAD is allowed!
849 sqe
->raddr
= rdma_wr(wr
)->remote_addr
;
850 sqe
->rkey
= rdma_wr(wr
)->rkey
;
853 if (wr
->opcode
== IB_WR_RDMA_READ
)
854 sqe
->opcode
= SIW_OP_READ
;
856 sqe
->opcode
= SIW_OP_READ_LOCAL_INV
;
859 case IB_WR_RDMA_WRITE
:
860 if (!(wr
->send_flags
& IB_SEND_INLINE
)) {
861 siw_copy_sgl(wr
->sg_list
, &sqe
->sge
[0],
863 sqe
->num_sge
= wr
->num_sge
;
865 rv
= siw_copy_inline_sgl(wr
, sqe
);
866 if (unlikely(rv
< 0)) {
870 sqe
->flags
|= SIW_WQE_INLINE
;
873 sqe
->raddr
= rdma_wr(wr
)->remote_addr
;
874 sqe
->rkey
= rdma_wr(wr
)->rkey
;
875 sqe
->opcode
= SIW_OP_WRITE
;
879 sqe
->base_mr
= (uintptr_t)reg_wr(wr
)->mr
;
880 sqe
->rkey
= reg_wr(wr
)->key
;
881 sqe
->access
= reg_wr(wr
)->access
& IWARP_ACCESS_MASK
;
882 sqe
->opcode
= SIW_OP_REG_MR
;
885 case IB_WR_LOCAL_INV
:
886 sqe
->rkey
= wr
->ex
.invalidate_rkey
;
887 sqe
->opcode
= SIW_OP_INVAL_STAG
;
891 siw_dbg_qp(qp
, "ib wr type %d unsupported\n",
896 siw_dbg_qp(qp
, "opcode %d, flags 0x%x, wr_id 0x%pK\n",
897 sqe
->opcode
, sqe
->flags
,
898 (void *)(uintptr_t)sqe
->id
);
900 if (unlikely(rv
< 0))
903 /* make SQE only valid after completely written */
905 sqe
->flags
|= SIW_WQE_VALID
;
912 * Send directly if SQ processing is not in progress.
913 * Eventual immediate errors (rv < 0) do not affect the involved
914 * RI resources (Verbs, 8.3.1) and thus do not prevent from SQ
915 * processing, if new work is already pending. But rv must be passed
918 if (wqe
->wr_status
!= SIW_WR_IDLE
) {
919 spin_unlock_irqrestore(&qp
->sq_lock
, flags
);
920 goto skip_direct_sending
;
922 rv
= siw_activate_tx(qp
);
923 spin_unlock_irqrestore(&qp
->sq_lock
, flags
);
926 goto skip_direct_sending
;
928 if (rdma_is_kernel_res(&qp
->base_qp
.res
)) {
929 rv
= siw_sq_start(qp
);
931 qp
->tx_ctx
.in_syscall
= 1;
933 if (siw_qp_sq_process(qp
) != 0 && !(qp
->tx_ctx
.tx_suspend
))
934 siw_qp_cm_drop(qp
, 0);
936 qp
->tx_ctx
.in_syscall
= 0;
940 up_read(&qp
->state_lock
);
947 siw_dbg_qp(qp
, "error %d\n", rv
);
956 * Post a list of R-WR's to a RQ.
958 * @base_qp: Base QP contained in siw QP
959 * @wr: Null terminated list of user WR's
960 * @bad_wr: Points to failing WR in case of synchronous failure.
962 int siw_post_receive(struct ib_qp
*base_qp
, const struct ib_recv_wr
*wr
,
963 const struct ib_recv_wr
**bad_wr
)
965 struct siw_qp
*qp
= to_siw_qp(base_qp
);
971 return -EOPNOTSUPP
; /* what else from errno.h? */
973 if (!rdma_is_kernel_res(&qp
->base_qp
.res
)) {
974 siw_dbg_qp(qp
, "no kernel post_recv for user mapped rq\n");
980 * Try to acquire QP state lock. Must be non-blocking
981 * to accommodate kernel clients needs.
983 if (!down_read_trylock(&qp
->state_lock
)) {
984 if (qp
->attrs
.state
== SIW_QP_STATE_ERROR
) {
986 * ERROR state is final, so we can be sure
987 * this state will not change as long as the QP
990 * This handles an ib_drain_rq() call with
991 * a concurrent request to set the QP state
994 rv
= siw_rq_flush_wr(qp
, wr
, bad_wr
);
996 siw_dbg_qp(qp
, "QP locked, state %d\n",
1003 if (qp
->attrs
.state
> SIW_QP_STATE_RTS
) {
1004 if (qp
->attrs
.state
== SIW_QP_STATE_ERROR
) {
1006 * Immediately flush this WR to CQ, if QP
1007 * is in ERROR state. RQ is guaranteed to
1008 * be empty, so WR complets in-order.
1010 * Typically triggered by ib_drain_rq().
1012 rv
= siw_rq_flush_wr(qp
, wr
, bad_wr
);
1014 siw_dbg_qp(qp
, "QP out of state %d\n",
1019 up_read(&qp
->state_lock
);
1023 * Serialize potentially multiple producers.
1024 * Not needed for single threaded consumer side.
1026 spin_lock_irqsave(&qp
->rq_lock
, flags
);
1029 u32 idx
= qp
->rq_put
% qp
->attrs
.rq_size
;
1030 struct siw_rqe
*rqe
= &qp
->recvq
[idx
];
1033 siw_dbg_qp(qp
, "RQ full\n");
1037 if (wr
->num_sge
> qp
->attrs
.rq_max_sges
) {
1038 siw_dbg_qp(qp
, "too many sge's: %d\n", wr
->num_sge
);
1042 rqe
->id
= wr
->wr_id
;
1043 rqe
->num_sge
= wr
->num_sge
;
1044 siw_copy_sgl(wr
->sg_list
, rqe
->sge
, wr
->num_sge
);
1046 /* make sure RQE is completely written before valid */
1049 rqe
->flags
= SIW_WQE_VALID
;
1054 spin_unlock_irqrestore(&qp
->rq_lock
, flags
);
1056 up_read(&qp
->state_lock
);
1059 siw_dbg_qp(qp
, "error %d\n", rv
);
1062 return rv
> 0 ? 0 : rv
;
1065 int siw_destroy_cq(struct ib_cq
*base_cq
, struct ib_udata
*udata
)
1067 struct siw_cq
*cq
= to_siw_cq(base_cq
);
1068 struct siw_device
*sdev
= to_siw_dev(base_cq
->device
);
1069 struct siw_ucontext
*ctx
=
1070 rdma_udata_to_drv_context(udata
, struct siw_ucontext
,
1073 siw_dbg_cq(cq
, "free CQ resources\n");
1078 rdma_user_mmap_entry_remove(cq
->cq_entry
);
1080 atomic_dec(&sdev
->num_cq
);
1089 * Populate CQ of requested size
1091 * @base_cq: CQ as allocated by RDMA midlayer
1092 * @attr: Initial CQ attributes
1093 * @udata: relates to user context
1096 int siw_create_cq(struct ib_cq
*base_cq
, const struct ib_cq_init_attr
*attr
,
1097 struct ib_udata
*udata
)
1099 struct siw_device
*sdev
= to_siw_dev(base_cq
->device
);
1100 struct siw_cq
*cq
= to_siw_cq(base_cq
);
1101 int rv
, size
= attr
->cqe
;
1106 if (atomic_inc_return(&sdev
->num_cq
) > SIW_MAX_CQ
) {
1107 siw_dbg(base_cq
->device
, "too many CQ's\n");
1111 if (size
< 1 || size
> sdev
->attrs
.max_cqe
) {
1112 siw_dbg(base_cq
->device
, "CQ size error: %d\n", size
);
1116 size
= roundup_pow_of_two(size
);
1117 cq
->base_cq
.cqe
= size
;
1121 cq
->queue
= vmalloc_user(size
* sizeof(struct siw_cqe
) +
1122 sizeof(struct siw_cq_ctrl
));
1124 cq
->queue
= vzalloc(size
* sizeof(struct siw_cqe
) +
1125 sizeof(struct siw_cq_ctrl
));
1127 if (cq
->queue
== NULL
) {
1131 get_random_bytes(&cq
->id
, 4);
1132 siw_dbg(base_cq
->device
, "new CQ [%u]\n", cq
->id
);
1134 spin_lock_init(&cq
->lock
);
1136 cq
->notify
= (struct siw_cq_ctrl
*)&cq
->queue
[size
];
1139 struct siw_uresp_create_cq uresp
= {};
1140 struct siw_ucontext
*ctx
=
1141 rdma_udata_to_drv_context(udata
, struct siw_ucontext
,
1143 size_t length
= size
* sizeof(struct siw_cqe
) +
1144 sizeof(struct siw_cq_ctrl
);
1147 siw_mmap_entry_insert(ctx
, cq
->queue
,
1148 length
, &uresp
.cq_key
);
1149 if (!cq
->cq_entry
) {
1154 uresp
.cq_id
= cq
->id
;
1155 uresp
.num_cqe
= size
;
1157 if (udata
->outlen
< sizeof(uresp
)) {
1161 rv
= ib_copy_to_udata(udata
, &uresp
, sizeof(uresp
));
1168 siw_dbg(base_cq
->device
, "CQ creation failed: %d", rv
);
1170 if (cq
&& cq
->queue
) {
1171 struct siw_ucontext
*ctx
=
1172 rdma_udata_to_drv_context(udata
, struct siw_ucontext
,
1175 rdma_user_mmap_entry_remove(cq
->cq_entry
);
1178 atomic_dec(&sdev
->num_cq
);
1186 * Reap CQ entries if available and copy work completion status into
1187 * array of WC's provided by caller. Returns number of reaped CQE's.
1189 * @base_cq: Base CQ contained in siw CQ.
1190 * @num_cqe: Maximum number of CQE's to reap.
1191 * @wc: Array of work completions to be filled by siw.
1193 int siw_poll_cq(struct ib_cq
*base_cq
, int num_cqe
, struct ib_wc
*wc
)
1195 struct siw_cq
*cq
= to_siw_cq(base_cq
);
1198 for (i
= 0; i
< num_cqe
; i
++) {
1199 if (!siw_reap_cqe(cq
, wc
))
1207 * siw_req_notify_cq()
1209 * Request notification for new CQE's added to that CQ.
1211 * o SIW_CQ_NOTIFY_SOLICITED lets siw trigger a notification
1212 * event if a WQE with notification flag set enters the CQ
1213 * o SIW_CQ_NOTIFY_NEXT_COMP lets siw trigger a notification
1214 * event if a WQE enters the CQ.
1215 * o IB_CQ_REPORT_MISSED_EVENTS: return value will provide the
1216 * number of not reaped CQE's regardless of its notification
1217 * type and current or new CQ notification settings.
1219 * @base_cq: Base CQ contained in siw CQ.
1220 * @flags: Requested notification flags.
1222 int siw_req_notify_cq(struct ib_cq
*base_cq
, enum ib_cq_notify_flags flags
)
1224 struct siw_cq
*cq
= to_siw_cq(base_cq
);
1226 siw_dbg_cq(cq
, "flags: 0x%02x\n", flags
);
1228 if ((flags
& IB_CQ_SOLICITED_MASK
) == IB_CQ_SOLICITED
)
1230 * Enable CQ event for next solicited completion.
1231 * and make it visible to all associated producers.
1233 smp_store_mb(cq
->notify
->flags
, SIW_NOTIFY_SOLICITED
);
1236 * Enable CQ event for any signalled completion.
1237 * and make it visible to all associated producers.
1239 smp_store_mb(cq
->notify
->flags
, SIW_NOTIFY_ALL
);
1241 if (flags
& IB_CQ_REPORT_MISSED_EVENTS
)
1242 return cq
->cq_put
- cq
->cq_get
;
1250 * Release Memory Region.
1252 * @base_mr: Base MR contained in siw MR.
1253 * @udata: points to user context, unused.
1255 int siw_dereg_mr(struct ib_mr
*base_mr
, struct ib_udata
*udata
)
1257 struct siw_mr
*mr
= to_siw_mr(base_mr
);
1258 struct siw_device
*sdev
= to_siw_dev(base_mr
->device
);
1260 siw_dbg_mem(mr
->mem
, "deregister MR\n");
1262 atomic_dec(&sdev
->num_mr
);
1264 siw_mr_drop_mem(mr
);
1273 * Register Memory Region.
1275 * @pd: Protection Domain
1276 * @start: starting address of MR (virtual address)
1278 * @rnic_va: not used by siw
1279 * @rights: MR access rights
1280 * @udata: user buffer to communicate STag and Key.
1282 struct ib_mr
*siw_reg_user_mr(struct ib_pd
*pd
, u64 start
, u64 len
,
1283 u64 rnic_va
, int rights
, struct ib_udata
*udata
)
1285 struct siw_mr
*mr
= NULL
;
1286 struct siw_umem
*umem
= NULL
;
1287 struct siw_ureq_reg_mr ureq
;
1288 struct siw_device
*sdev
= to_siw_dev(pd
->device
);
1290 unsigned long mem_limit
= rlimit(RLIMIT_MEMLOCK
);
1293 siw_dbg_pd(pd
, "start: 0x%pK, va: 0x%pK, len: %llu\n",
1294 (void *)(uintptr_t)start
, (void *)(uintptr_t)rnic_va
,
1295 (unsigned long long)len
);
1297 if (atomic_inc_return(&sdev
->num_mr
) > SIW_MAX_MR
) {
1298 siw_dbg_pd(pd
, "too many mr's\n");
1306 if (mem_limit
!= RLIM_INFINITY
) {
1307 unsigned long num_pages
=
1308 (PAGE_ALIGN(len
+ (start
& ~PAGE_MASK
))) >> PAGE_SHIFT
;
1309 mem_limit
>>= PAGE_SHIFT
;
1311 if (num_pages
> mem_limit
- current
->mm
->locked_vm
) {
1312 siw_dbg_pd(pd
, "pages req %lu, max %lu, lock %lu\n",
1313 num_pages
, mem_limit
,
1314 current
->mm
->locked_vm
);
1319 umem
= siw_umem_get(start
, len
, ib_access_writable(rights
));
1322 siw_dbg_pd(pd
, "getting user memory failed: %d\n", rv
);
1326 mr
= kzalloc(sizeof(*mr
), GFP_KERNEL
);
1331 rv
= siw_mr_add_mem(mr
, pd
, umem
, start
, len
, rights
);
1336 struct siw_uresp_reg_mr uresp
= {};
1337 struct siw_mem
*mem
= mr
->mem
;
1339 if (udata
->inlen
< sizeof(ureq
)) {
1343 rv
= ib_copy_from_udata(&ureq
, udata
, sizeof(ureq
));
1347 mr
->base_mr
.lkey
|= ureq
.stag_key
;
1348 mr
->base_mr
.rkey
|= ureq
.stag_key
;
1349 mem
->stag
|= ureq
.stag_key
;
1350 uresp
.stag
= mem
->stag
;
1352 if (udata
->outlen
< sizeof(uresp
)) {
1356 rv
= ib_copy_to_udata(udata
, &uresp
, sizeof(uresp
));
1360 mr
->mem
->stag_valid
= 1;
1362 return &mr
->base_mr
;
1365 atomic_dec(&sdev
->num_mr
);
1368 siw_mr_drop_mem(mr
);
1372 siw_umem_release(umem
, false);
1377 struct ib_mr
*siw_alloc_mr(struct ib_pd
*pd
, enum ib_mr_type mr_type
,
1380 struct siw_device
*sdev
= to_siw_dev(pd
->device
);
1381 struct siw_mr
*mr
= NULL
;
1382 struct siw_pbl
*pbl
= NULL
;
1385 if (atomic_inc_return(&sdev
->num_mr
) > SIW_MAX_MR
) {
1386 siw_dbg_pd(pd
, "too many mr's\n");
1390 if (mr_type
!= IB_MR_TYPE_MEM_REG
) {
1391 siw_dbg_pd(pd
, "mr type %d unsupported\n", mr_type
);
1395 if (max_sge
> SIW_MAX_SGE_PBL
) {
1396 siw_dbg_pd(pd
, "too many sge's: %d\n", max_sge
);
1400 pbl
= siw_pbl_alloc(max_sge
);
1403 siw_dbg_pd(pd
, "pbl allocation failed: %d\n", rv
);
1407 mr
= kzalloc(sizeof(*mr
), GFP_KERNEL
);
1412 rv
= siw_mr_add_mem(mr
, pd
, pbl
, 0, max_sge
* PAGE_SIZE
, 0);
1416 mr
->mem
->is_pbl
= 1;
1418 siw_dbg_pd(pd
, "[MEM %u]: success\n", mr
->mem
->stag
);
1420 return &mr
->base_mr
;
1423 atomic_dec(&sdev
->num_mr
);
1429 siw_mr_drop_mem(mr
);
1432 siw_dbg_pd(pd
, "failed: %d\n", rv
);
1437 /* Just used to count number of pages being mapped */
1438 static int siw_set_pbl_page(struct ib_mr
*base_mr
, u64 buf_addr
)
1443 int siw_map_mr_sg(struct ib_mr
*base_mr
, struct scatterlist
*sl
, int num_sle
,
1444 unsigned int *sg_off
)
1446 struct scatterlist
*slp
;
1447 struct siw_mr
*mr
= to_siw_mr(base_mr
);
1448 struct siw_mem
*mem
= mr
->mem
;
1449 struct siw_pbl
*pbl
= mem
->pbl
;
1450 struct siw_pble
*pble
;
1451 unsigned long pbl_size
;
1455 siw_dbg_mem(mem
, "no PBL allocated\n");
1460 if (pbl
->max_buf
< num_sle
) {
1461 siw_dbg_mem(mem
, "too many SGE's: %d > %d\n",
1462 mem
->pbl
->max_buf
, num_sle
);
1465 for_each_sg(sl
, slp
, num_sle
, i
) {
1466 if (sg_dma_len(slp
) == 0) {
1467 siw_dbg_mem(mem
, "empty SGE\n");
1471 pble
->addr
= sg_dma_address(slp
);
1472 pble
->size
= sg_dma_len(slp
);
1474 pbl_size
= pble
->size
;
1477 /* Merge PBL entries if adjacent */
1478 if (pble
->addr
+ pble
->size
== sg_dma_address(slp
)) {
1479 pble
->size
+= sg_dma_len(slp
);
1483 pble
->addr
= sg_dma_address(slp
);
1484 pble
->size
= sg_dma_len(slp
);
1485 pble
->pbl_off
= pbl_size
;
1487 pbl_size
+= sg_dma_len(slp
);
1490 "sge[%d], size %u, addr 0x%p, total %lu\n",
1491 i
, pble
->size
, (void *)(uintptr_t)pble
->addr
,
1494 rv
= ib_sg_to_pages(base_mr
, sl
, num_sle
, sg_off
, siw_set_pbl_page
);
1496 mem
->len
= base_mr
->length
;
1497 mem
->va
= base_mr
->iova
;
1499 "%llu bytes, start 0x%pK, %u SLE to %u entries\n",
1500 mem
->len
, (void *)(uintptr_t)mem
->va
, num_sle
,
1509 * Create a (empty) DMA memory region, where no umem is attached.
1511 struct ib_mr
*siw_get_dma_mr(struct ib_pd
*pd
, int rights
)
1513 struct siw_device
*sdev
= to_siw_dev(pd
->device
);
1514 struct siw_mr
*mr
= NULL
;
1517 if (atomic_inc_return(&sdev
->num_mr
) > SIW_MAX_MR
) {
1518 siw_dbg_pd(pd
, "too many mr's\n");
1522 mr
= kzalloc(sizeof(*mr
), GFP_KERNEL
);
1527 rv
= siw_mr_add_mem(mr
, pd
, NULL
, 0, ULONG_MAX
, rights
);
1531 mr
->mem
->stag_valid
= 1;
1533 siw_dbg_pd(pd
, "[MEM %u]: success\n", mr
->mem
->stag
);
1535 return &mr
->base_mr
;
1541 atomic_dec(&sdev
->num_mr
);
1549 * Create Shared Receive Queue of attributes @init_attrs
1550 * within protection domain given by @pd.
1552 * @base_srq: Base SRQ contained in siw SRQ.
1553 * @init_attrs: SRQ init attributes.
1554 * @udata: points to user context
1556 int siw_create_srq(struct ib_srq
*base_srq
,
1557 struct ib_srq_init_attr
*init_attrs
, struct ib_udata
*udata
)
1559 struct siw_srq
*srq
= to_siw_srq(base_srq
);
1560 struct ib_srq_attr
*attrs
= &init_attrs
->attr
;
1561 struct siw_device
*sdev
= to_siw_dev(base_srq
->device
);
1562 struct siw_ucontext
*ctx
=
1563 rdma_udata_to_drv_context(udata
, struct siw_ucontext
,
1567 if (init_attrs
->srq_type
!= IB_SRQT_BASIC
)
1570 if (atomic_inc_return(&sdev
->num_srq
) > SIW_MAX_SRQ
) {
1571 siw_dbg_pd(base_srq
->pd
, "too many SRQ's\n");
1575 if (attrs
->max_wr
== 0 || attrs
->max_wr
> SIW_MAX_SRQ_WR
||
1576 attrs
->max_sge
> SIW_MAX_SGE
|| attrs
->srq_limit
> attrs
->max_wr
) {
1580 srq
->max_sge
= attrs
->max_sge
;
1581 srq
->num_rqe
= roundup_pow_of_two(attrs
->max_wr
);
1582 srq
->limit
= attrs
->srq_limit
;
1586 srq
->is_kernel_res
= !udata
;
1590 vmalloc_user(srq
->num_rqe
* sizeof(struct siw_rqe
));
1592 srq
->recvq
= vzalloc(srq
->num_rqe
* sizeof(struct siw_rqe
));
1594 if (srq
->recvq
== NULL
) {
1599 struct siw_uresp_create_srq uresp
= {};
1600 size_t length
= srq
->num_rqe
* sizeof(struct siw_rqe
);
1603 siw_mmap_entry_insert(ctx
, srq
->recvq
,
1604 length
, &uresp
.srq_key
);
1605 if (!srq
->srq_entry
) {
1610 uresp
.num_rqe
= srq
->num_rqe
;
1612 if (udata
->outlen
< sizeof(uresp
)) {
1616 rv
= ib_copy_to_udata(udata
, &uresp
, sizeof(uresp
));
1620 spin_lock_init(&srq
->lock
);
1622 siw_dbg_pd(base_srq
->pd
, "[SRQ]: success\n");
1629 rdma_user_mmap_entry_remove(srq
->srq_entry
);
1632 atomic_dec(&sdev
->num_srq
);
1640 * Modify SRQ. The caller may resize SRQ and/or set/reset notification
1641 * limit and (re)arm IB_EVENT_SRQ_LIMIT_REACHED notification.
1643 * NOTE: it is unclear if RDMA core allows for changing the MAX_SGE
1644 * parameter. siw_modify_srq() does not check the attrs->max_sge param.
1646 int siw_modify_srq(struct ib_srq
*base_srq
, struct ib_srq_attr
*attrs
,
1647 enum ib_srq_attr_mask attr_mask
, struct ib_udata
*udata
)
1649 struct siw_srq
*srq
= to_siw_srq(base_srq
);
1650 unsigned long flags
;
1653 spin_lock_irqsave(&srq
->lock
, flags
);
1655 if (attr_mask
& IB_SRQ_MAX_WR
) {
1656 /* resize request not yet supported */
1660 if (attr_mask
& IB_SRQ_LIMIT
) {
1661 if (attrs
->srq_limit
) {
1662 if (unlikely(attrs
->srq_limit
> srq
->num_rqe
)) {
1670 srq
->limit
= attrs
->srq_limit
;
1673 spin_unlock_irqrestore(&srq
->lock
, flags
);
1681 * Query SRQ attributes.
1683 int siw_query_srq(struct ib_srq
*base_srq
, struct ib_srq_attr
*attrs
)
1685 struct siw_srq
*srq
= to_siw_srq(base_srq
);
1686 unsigned long flags
;
1688 spin_lock_irqsave(&srq
->lock
, flags
);
1690 attrs
->max_wr
= srq
->num_rqe
;
1691 attrs
->max_sge
= srq
->max_sge
;
1692 attrs
->srq_limit
= srq
->limit
;
1694 spin_unlock_irqrestore(&srq
->lock
, flags
);
1703 * It is assumed that the SRQ is not referenced by any
1704 * QP anymore - the code trusts the RDMA core environment to keep track
1707 int siw_destroy_srq(struct ib_srq
*base_srq
, struct ib_udata
*udata
)
1709 struct siw_srq
*srq
= to_siw_srq(base_srq
);
1710 struct siw_device
*sdev
= to_siw_dev(base_srq
->device
);
1711 struct siw_ucontext
*ctx
=
1712 rdma_udata_to_drv_context(udata
, struct siw_ucontext
,
1716 rdma_user_mmap_entry_remove(srq
->srq_entry
);
1718 atomic_dec(&sdev
->num_srq
);
1723 * siw_post_srq_recv()
1725 * Post a list of receive queue elements to SRQ.
1726 * NOTE: The function does not check or lock a certain SRQ state
1727 * during the post operation. The code simply trusts the
1728 * RDMA core environment.
1730 * @base_srq: Base SRQ contained in siw SRQ
1731 * @wr: List of R-WR's
1732 * @bad_wr: Updated to failing WR if posting fails.
1734 int siw_post_srq_recv(struct ib_srq
*base_srq
, const struct ib_recv_wr
*wr
,
1735 const struct ib_recv_wr
**bad_wr
)
1737 struct siw_srq
*srq
= to_siw_srq(base_srq
);
1738 unsigned long flags
;
1741 if (unlikely(!srq
->is_kernel_res
)) {
1742 siw_dbg_pd(base_srq
->pd
,
1743 "[SRQ]: no kernel post_recv for mapped srq\n");
1748 * Serialize potentially multiple producers.
1749 * Also needed to serialize potentially multiple
1752 spin_lock_irqsave(&srq
->lock
, flags
);
1755 u32 idx
= srq
->rq_put
% srq
->num_rqe
;
1756 struct siw_rqe
*rqe
= &srq
->recvq
[idx
];
1759 siw_dbg_pd(base_srq
->pd
, "SRQ full\n");
1763 if (unlikely(wr
->num_sge
> srq
->max_sge
)) {
1764 siw_dbg_pd(base_srq
->pd
,
1765 "[SRQ]: too many sge's: %d\n", wr
->num_sge
);
1769 rqe
->id
= wr
->wr_id
;
1770 rqe
->num_sge
= wr
->num_sge
;
1771 siw_copy_sgl(wr
->sg_list
, rqe
->sge
, wr
->num_sge
);
1773 /* Make sure S-RQE is completely written before valid */
1776 rqe
->flags
= SIW_WQE_VALID
;
1781 spin_unlock_irqrestore(&srq
->lock
, flags
);
1783 if (unlikely(rv
< 0)) {
1784 siw_dbg_pd(base_srq
->pd
, "[SRQ]: error %d\n", rv
);
1790 void siw_qp_event(struct siw_qp
*qp
, enum ib_event_type etype
)
1792 struct ib_event event
;
1793 struct ib_qp
*base_qp
= &qp
->base_qp
;
1796 * Do not report asynchronous errors on QP which gets
1797 * destroyed via verbs interface (siw_destroy_qp())
1799 if (qp
->attrs
.flags
& SIW_QP_IN_DESTROY
)
1802 event
.event
= etype
;
1803 event
.device
= base_qp
->device
;
1804 event
.element
.qp
= base_qp
;
1806 if (base_qp
->event_handler
) {
1807 siw_dbg_qp(qp
, "reporting event %d\n", etype
);
1808 base_qp
->event_handler(&event
, base_qp
->qp_context
);
1812 void siw_cq_event(struct siw_cq
*cq
, enum ib_event_type etype
)
1814 struct ib_event event
;
1815 struct ib_cq
*base_cq
= &cq
->base_cq
;
1817 event
.event
= etype
;
1818 event
.device
= base_cq
->device
;
1819 event
.element
.cq
= base_cq
;
1821 if (base_cq
->event_handler
) {
1822 siw_dbg_cq(cq
, "reporting CQ event %d\n", etype
);
1823 base_cq
->event_handler(&event
, base_cq
->cq_context
);
1827 void siw_srq_event(struct siw_srq
*srq
, enum ib_event_type etype
)
1829 struct ib_event event
;
1830 struct ib_srq
*base_srq
= &srq
->base_srq
;
1832 event
.event
= etype
;
1833 event
.device
= base_srq
->device
;
1834 event
.element
.srq
= base_srq
;
1836 if (base_srq
->event_handler
) {
1837 siw_dbg_pd(srq
->base_srq
.pd
,
1838 "reporting SRQ event %d\n", etype
);
1839 base_srq
->event_handler(&event
, base_srq
->srq_context
);
1843 void siw_port_event(struct siw_device
*sdev
, u8 port
, enum ib_event_type etype
)
1845 struct ib_event event
;
1847 event
.event
= etype
;
1848 event
.device
= &sdev
->base_dev
;
1849 event
.element
.port_num
= port
;
1851 siw_dbg(&sdev
->base_dev
, "reporting port event %d\n", etype
);
1853 ib_dispatch_event(&event
);