1 // SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
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>
11 #include <net/addrconf.h>
13 #include <rdma/iw_cm.h>
14 #include <rdma/ib_verbs.h>
15 #include <rdma/ib_user_verbs.h>
16 #include <rdma/uverbs_ioctl.h>
19 #include "siw_verbs.h"
22 static int siw_qp_state_to_ib_qp_state[SIW_QP_STATE_COUNT] = {
23 [SIW_QP_STATE_IDLE] = IB_QPS_INIT,
24 [SIW_QP_STATE_RTR] = IB_QPS_RTR,
25 [SIW_QP_STATE_RTS] = IB_QPS_RTS,
26 [SIW_QP_STATE_CLOSING] = IB_QPS_SQD,
27 [SIW_QP_STATE_TERMINATE] = IB_QPS_SQE,
28 [SIW_QP_STATE_ERROR] = IB_QPS_ERR
31 static int ib_qp_state_to_siw_qp_state[IB_QPS_ERR + 1] = {
32 [IB_QPS_RESET] = SIW_QP_STATE_IDLE,
33 [IB_QPS_INIT] = SIW_QP_STATE_IDLE,
34 [IB_QPS_RTR] = SIW_QP_STATE_RTR,
35 [IB_QPS_RTS] = SIW_QP_STATE_RTS,
36 [IB_QPS_SQD] = SIW_QP_STATE_CLOSING,
37 [IB_QPS_SQE] = SIW_QP_STATE_TERMINATE,
38 [IB_QPS_ERR] = SIW_QP_STATE_ERROR
41 static char ib_qp_state_to_string[IB_QPS_ERR + 1][sizeof("RESET")] = {
42 [IB_QPS_RESET] = "RESET", [IB_QPS_INIT] = "INIT", [IB_QPS_RTR] = "RTR",
43 [IB_QPS_RTS] = "RTS", [IB_QPS_SQD] = "SQD", [IB_QPS_SQE] = "SQE",
47 void siw_mmap_free(struct rdma_user_mmap_entry *rdma_entry)
49 struct siw_user_mmap_entry *entry = to_siw_mmap_entry(rdma_entry);
54 int siw_mmap(struct ib_ucontext *ctx, struct vm_area_struct *vma)
56 struct siw_ucontext *uctx = to_siw_ctx(ctx);
57 size_t size = vma->vm_end - vma->vm_start;
58 struct rdma_user_mmap_entry *rdma_entry;
59 struct siw_user_mmap_entry *entry;
63 * Must be page aligned
65 if (vma->vm_start & (PAGE_SIZE - 1)) {
66 pr_warn("siw: mmap not page aligned\n");
69 rdma_entry = rdma_user_mmap_entry_get(&uctx->base_ucontext, vma);
71 siw_dbg(&uctx->sdev->base_dev, "mmap lookup failed: %lu, %#zx\n",
75 entry = to_siw_mmap_entry(rdma_entry);
77 rv = remap_vmalloc_range(vma, entry->address, 0);
79 pr_warn("remap_vmalloc_range failed: %lu, %zu\n", vma->vm_pgoff,
81 rdma_user_mmap_entry_put(rdma_entry);
86 int siw_alloc_ucontext(struct ib_ucontext *base_ctx, struct ib_udata *udata)
88 struct siw_device *sdev = to_siw_dev(base_ctx->device);
89 struct siw_ucontext *ctx = to_siw_ctx(base_ctx);
90 struct siw_uresp_alloc_ctx uresp = {};
93 if (atomic_inc_return(&sdev->num_ctx) > SIW_MAX_CONTEXT) {
99 uresp.dev_id = sdev->vendor_part_id;
101 if (udata->outlen < sizeof(uresp)) {
105 rv = ib_copy_to_udata(udata, &uresp, sizeof(uresp));
109 siw_dbg(base_ctx->device, "success. now %d context(s)\n",
110 atomic_read(&sdev->num_ctx));
115 atomic_dec(&sdev->num_ctx);
116 siw_dbg(base_ctx->device, "failure %d. now %d context(s)\n", rv,
117 atomic_read(&sdev->num_ctx));
122 void siw_dealloc_ucontext(struct ib_ucontext *base_ctx)
124 struct siw_ucontext *uctx = to_siw_ctx(base_ctx);
126 atomic_dec(&uctx->sdev->num_ctx);
129 int siw_query_device(struct ib_device *base_dev, struct ib_device_attr *attr,
130 struct ib_udata *udata)
132 struct siw_device *sdev = to_siw_dev(base_dev);
134 if (udata->inlen || udata->outlen)
137 memset(attr, 0, sizeof(*attr));
139 /* Revisit atomic caps if RFC 7306 gets supported */
140 attr->atomic_cap = 0;
141 attr->device_cap_flags = IB_DEVICE_MEM_MGT_EXTENSIONS;
142 attr->kernel_cap_flags = IBK_ALLOW_USER_UNREG;
143 attr->max_cq = sdev->attrs.max_cq;
144 attr->max_cqe = sdev->attrs.max_cqe;
145 attr->max_fast_reg_page_list_len = SIW_MAX_SGE_PBL;
146 attr->max_mr = sdev->attrs.max_mr;
147 attr->max_mw = sdev->attrs.max_mw;
148 attr->max_mr_size = ~0ull;
149 attr->max_pd = sdev->attrs.max_pd;
150 attr->max_qp = sdev->attrs.max_qp;
151 attr->max_qp_init_rd_atom = sdev->attrs.max_ird;
152 attr->max_qp_rd_atom = sdev->attrs.max_ord;
153 attr->max_qp_wr = sdev->attrs.max_qp_wr;
154 attr->max_recv_sge = sdev->attrs.max_sge;
155 attr->max_res_rd_atom = sdev->attrs.max_qp * sdev->attrs.max_ird;
156 attr->max_send_sge = sdev->attrs.max_sge;
157 attr->max_sge_rd = sdev->attrs.max_sge_rd;
158 attr->max_srq = sdev->attrs.max_srq;
159 attr->max_srq_sge = sdev->attrs.max_srq_sge;
160 attr->max_srq_wr = sdev->attrs.max_srq_wr;
161 attr->page_size_cap = PAGE_SIZE;
162 attr->vendor_id = SIW_VENDOR_ID;
163 attr->vendor_part_id = sdev->vendor_part_id;
165 addrconf_addr_eui48((u8 *)&attr->sys_image_guid,
171 int siw_query_port(struct ib_device *base_dev, u32 port,
172 struct ib_port_attr *attr)
174 struct net_device *ndev;
177 memset(attr, 0, sizeof(*attr));
179 rv = ib_get_eth_speed(base_dev, port, &attr->active_speed,
180 &attr->active_width);
184 ndev = ib_device_get_netdev(base_dev, SIW_PORT);
188 attr->gid_tbl_len = 1;
189 attr->max_msg_sz = -1;
190 attr->max_mtu = ib_mtu_int_to_enum(ndev->max_mtu);
191 attr->active_mtu = ib_mtu_int_to_enum(READ_ONCE(ndev->mtu));
192 attr->state = ib_get_curr_port_state(ndev);
193 attr->phys_state = attr->state == IB_PORT_ACTIVE ?
194 IB_PORT_PHYS_STATE_LINK_UP : IB_PORT_PHYS_STATE_DISABLED;
195 attr->port_cap_flags = IB_PORT_CM_SUP | IB_PORT_DEVICE_MGMT_SUP;
200 * attr->bad_pkey_cntr = 0;
201 * attr->qkey_viol_cntr = 0;
204 * attr->max_vl_num = 0;
206 * attr->subnet_timeout = 0;
207 * attr->init_type_repy = 0;
213 int siw_get_port_immutable(struct ib_device *base_dev, u32 port,
214 struct ib_port_immutable *port_immutable)
216 struct ib_port_attr attr;
217 int rv = siw_query_port(base_dev, port, &attr);
222 port_immutable->gid_tbl_len = attr.gid_tbl_len;
223 port_immutable->core_cap_flags = RDMA_CORE_PORT_IWARP;
228 int siw_query_gid(struct ib_device *base_dev, u32 port, int idx,
231 struct siw_device *sdev = to_siw_dev(base_dev);
233 /* subnet_prefix == interface_id == 0; */
234 memset(gid, 0, sizeof(*gid));
235 memcpy(gid->raw, sdev->raw_gid, ETH_ALEN);
240 int siw_alloc_pd(struct ib_pd *pd, struct ib_udata *udata)
242 struct siw_device *sdev = to_siw_dev(pd->device);
244 if (atomic_inc_return(&sdev->num_pd) > SIW_MAX_PD) {
245 atomic_dec(&sdev->num_pd);
248 siw_dbg_pd(pd, "now %d PD's(s)\n", atomic_read(&sdev->num_pd));
253 int siw_dealloc_pd(struct ib_pd *pd, struct ib_udata *udata)
255 struct siw_device *sdev = to_siw_dev(pd->device);
257 siw_dbg_pd(pd, "free PD\n");
258 atomic_dec(&sdev->num_pd);
262 void siw_qp_get_ref(struct ib_qp *base_qp)
264 siw_qp_get(to_siw_qp(base_qp));
267 void siw_qp_put_ref(struct ib_qp *base_qp)
269 siw_qp_put(to_siw_qp(base_qp));
272 static struct rdma_user_mmap_entry *
273 siw_mmap_entry_insert(struct siw_ucontext *uctx,
274 void *address, size_t length,
277 struct siw_user_mmap_entry *entry = kzalloc(sizeof(*entry), GFP_KERNEL);
280 *offset = SIW_INVAL_UOBJ_KEY;
284 entry->address = address;
286 rv = rdma_user_mmap_entry_insert(&uctx->base_ucontext,
294 *offset = rdma_user_mmap_get_offset(&entry->rdma_entry);
296 return &entry->rdma_entry;
302 * Create QP of requested size on given device.
305 * @attrs: Initial QP attributes.
306 * @udata: used to provide QP ID, SQ and RQ size back to user.
309 int siw_create_qp(struct ib_qp *ibqp, struct ib_qp_init_attr *attrs,
310 struct ib_udata *udata)
312 struct ib_pd *pd = ibqp->pd;
313 struct siw_qp *qp = to_siw_qp(ibqp);
314 struct ib_device *base_dev = pd->device;
315 struct siw_device *sdev = to_siw_dev(base_dev);
316 struct siw_ucontext *uctx =
317 rdma_udata_to_drv_context(udata, struct siw_ucontext,
320 int num_sqe, num_rqe, rv = 0;
323 siw_dbg(base_dev, "create new QP\n");
325 if (attrs->create_flags)
328 if (atomic_inc_return(&sdev->num_qp) > SIW_MAX_QP) {
329 siw_dbg(base_dev, "too many QP's\n");
333 if (attrs->qp_type != IB_QPT_RC) {
334 siw_dbg(base_dev, "only RC QP's supported\n");
338 if ((attrs->cap.max_send_wr > SIW_MAX_QP_WR) ||
339 (attrs->cap.max_recv_wr > SIW_MAX_QP_WR) ||
340 (attrs->cap.max_send_sge > SIW_MAX_SGE) ||
341 (attrs->cap.max_recv_sge > SIW_MAX_SGE)) {
342 siw_dbg(base_dev, "QP size error\n");
346 if (attrs->cap.max_inline_data > SIW_MAX_INLINE) {
347 siw_dbg(base_dev, "max inline send: %d > %d\n",
348 attrs->cap.max_inline_data, (int)SIW_MAX_INLINE);
353 * NOTE: we don't allow for a QP unable to hold any SQ WQE
355 if (attrs->cap.max_send_wr == 0) {
356 siw_dbg(base_dev, "QP must have send queue\n");
361 if (!attrs->send_cq || (!attrs->recv_cq && !attrs->srq)) {
362 siw_dbg(base_dev, "send CQ or receive CQ invalid\n");
367 init_rwsem(&qp->state_lock);
368 spin_lock_init(&qp->sq_lock);
369 spin_lock_init(&qp->rq_lock);
370 spin_lock_init(&qp->orq_lock);
372 rv = siw_qp_add(sdev, qp);
377 /* All queue indices are derived from modulo operations
378 * on a free running 'get' (consumer) and 'put' (producer)
379 * unsigned counter. Having queue sizes at power of two
380 * avoids handling counter wrap around.
382 num_sqe = roundup_pow_of_two(attrs->cap.max_send_wr);
383 num_rqe = attrs->cap.max_recv_wr;
385 num_rqe = roundup_pow_of_two(num_rqe);
388 qp->sendq = vmalloc_user(num_sqe * sizeof(struct siw_sqe));
390 qp->sendq = vcalloc(num_sqe, sizeof(struct siw_sqe));
392 if (qp->sendq == NULL) {
396 if (attrs->sq_sig_type != IB_SIGNAL_REQ_WR) {
397 if (attrs->sq_sig_type == IB_SIGNAL_ALL_WR)
398 qp->attrs.flags |= SIW_SIGNAL_ALL_WR;
405 qp->scq = to_siw_cq(attrs->send_cq);
406 qp->rcq = to_siw_cq(attrs->recv_cq);
411 * Verbs 6.3.7: ignore RQ size, if SRQ present
412 * Verbs 6.3.5: do not check PD of SRQ against PD of QP
414 qp->srq = to_siw_srq(attrs->srq);
415 qp->attrs.rq_size = 0;
416 siw_dbg(base_dev, "QP [%u]: SRQ attached\n",
418 } else if (num_rqe) {
421 vmalloc_user(num_rqe * sizeof(struct siw_rqe));
423 qp->recvq = vcalloc(num_rqe, sizeof(struct siw_rqe));
425 if (qp->recvq == NULL) {
429 qp->attrs.rq_size = num_rqe;
431 qp->attrs.sq_size = num_sqe;
432 qp->attrs.sq_max_sges = attrs->cap.max_send_sge;
433 qp->attrs.rq_max_sges = attrs->cap.max_recv_sge;
435 /* Make those two tunables fixed for now. */
436 qp->tx_ctx.gso_seg_limit = 1;
437 qp->tx_ctx.zcopy_tx = zcopy_tx;
439 qp->attrs.state = SIW_QP_STATE_IDLE;
442 struct siw_uresp_create_qp uresp = {};
444 uresp.num_sqe = num_sqe;
445 uresp.num_rqe = num_rqe;
446 uresp.qp_id = qp_id(qp);
449 length = num_sqe * sizeof(struct siw_sqe);
451 siw_mmap_entry_insert(uctx, qp->sendq,
452 length, &uresp.sq_key);
460 length = num_rqe * sizeof(struct siw_rqe);
462 siw_mmap_entry_insert(uctx, qp->recvq,
463 length, &uresp.rq_key);
465 uresp.sq_key = SIW_INVAL_UOBJ_KEY;
471 if (udata->outlen < sizeof(uresp)) {
475 rv = ib_copy_to_udata(udata, &uresp, sizeof(uresp));
479 qp->tx_cpu = siw_get_tx_cpu(sdev);
480 if (qp->tx_cpu < 0) {
484 INIT_LIST_HEAD(&qp->devq);
485 spin_lock_irqsave(&sdev->lock, flags);
486 list_add_tail(&qp->devq, &sdev->qp_list);
487 spin_unlock_irqrestore(&sdev->lock, flags);
489 init_completion(&qp->qp_free);
494 xa_erase(&sdev->qp_xa, qp_id(qp));
496 rdma_user_mmap_entry_remove(qp->sq_entry);
497 rdma_user_mmap_entry_remove(qp->rq_entry);
503 atomic_dec(&sdev->num_qp);
508 * Minimum siw_query_qp() verb interface.
510 * @qp_attr_mask is not used but all available information is provided
512 int siw_query_qp(struct ib_qp *base_qp, struct ib_qp_attr *qp_attr,
513 int qp_attr_mask, struct ib_qp_init_attr *qp_init_attr)
516 struct net_device *ndev;
518 if (base_qp && qp_attr && qp_init_attr)
519 qp = to_siw_qp(base_qp);
523 ndev = ib_device_get_netdev(base_qp->device, SIW_PORT);
527 qp_attr->qp_state = siw_qp_state_to_ib_qp_state[qp->attrs.state];
528 qp_attr->cap.max_inline_data = SIW_MAX_INLINE;
529 qp_attr->cap.max_send_wr = qp->attrs.sq_size;
530 qp_attr->cap.max_send_sge = qp->attrs.sq_max_sges;
531 qp_attr->cap.max_recv_wr = qp->attrs.rq_size;
532 qp_attr->cap.max_recv_sge = qp->attrs.rq_max_sges;
533 qp_attr->path_mtu = ib_mtu_int_to_enum(READ_ONCE(ndev->mtu));
534 qp_attr->max_rd_atomic = qp->attrs.irq_size;
535 qp_attr->max_dest_rd_atomic = qp->attrs.orq_size;
537 qp_attr->qp_access_flags = IB_ACCESS_LOCAL_WRITE |
538 IB_ACCESS_REMOTE_WRITE |
539 IB_ACCESS_REMOTE_READ;
541 qp_init_attr->qp_type = base_qp->qp_type;
542 qp_init_attr->send_cq = base_qp->send_cq;
543 qp_init_attr->recv_cq = base_qp->recv_cq;
544 qp_init_attr->srq = base_qp->srq;
546 qp_init_attr->cap = qp_attr->cap;
552 int siw_verbs_modify_qp(struct ib_qp *base_qp, struct ib_qp_attr *attr,
553 int attr_mask, struct ib_udata *udata)
555 struct siw_qp_attrs new_attrs;
556 enum siw_qp_attr_mask siw_attr_mask = 0;
557 struct siw_qp *qp = to_siw_qp(base_qp);
563 if (attr_mask & ~IB_QP_ATTR_STANDARD_BITS)
566 memset(&new_attrs, 0, sizeof(new_attrs));
568 if (attr_mask & IB_QP_ACCESS_FLAGS) {
569 siw_attr_mask = SIW_QP_ATTR_ACCESS_FLAGS;
571 if (attr->qp_access_flags & IB_ACCESS_REMOTE_READ)
572 new_attrs.flags |= SIW_RDMA_READ_ENABLED;
573 if (attr->qp_access_flags & IB_ACCESS_REMOTE_WRITE)
574 new_attrs.flags |= SIW_RDMA_WRITE_ENABLED;
575 if (attr->qp_access_flags & IB_ACCESS_MW_BIND)
576 new_attrs.flags |= SIW_RDMA_BIND_ENABLED;
578 if (attr_mask & IB_QP_STATE) {
579 siw_dbg_qp(qp, "desired IB QP state: %s\n",
580 ib_qp_state_to_string[attr->qp_state]);
582 new_attrs.state = ib_qp_state_to_siw_qp_state[attr->qp_state];
584 if (new_attrs.state > SIW_QP_STATE_RTS)
585 qp->tx_ctx.tx_suspend = 1;
587 siw_attr_mask |= SIW_QP_ATTR_STATE;
592 down_write(&qp->state_lock);
594 rv = siw_qp_modify(qp, &new_attrs, siw_attr_mask);
596 up_write(&qp->state_lock);
601 int siw_destroy_qp(struct ib_qp *base_qp, struct ib_udata *udata)
603 struct siw_qp *qp = to_siw_qp(base_qp);
604 struct siw_ucontext *uctx =
605 rdma_udata_to_drv_context(udata, struct siw_ucontext,
607 struct siw_qp_attrs qp_attrs;
609 siw_dbg_qp(qp, "state %d\n", qp->attrs.state);
612 * Mark QP as in process of destruction to prevent from
613 * any async callbacks to RDMA core
615 qp->attrs.flags |= SIW_QP_IN_DESTROY;
616 qp->rx_stream.rx_suspend = 1;
619 rdma_user_mmap_entry_remove(qp->sq_entry);
620 rdma_user_mmap_entry_remove(qp->rq_entry);
623 down_write(&qp->state_lock);
625 qp_attrs.state = SIW_QP_STATE_ERROR;
626 siw_qp_modify(qp, &qp_attrs, SIW_QP_ATTR_STATE);
629 siw_cep_put(qp->cep);
632 up_write(&qp->state_lock);
634 kfree(qp->tx_ctx.mpa_crc_hd);
635 kfree(qp->rx_stream.mpa_crc_hd);
637 qp->scq = qp->rcq = NULL;
640 wait_for_completion(&qp->qp_free);
646 * siw_copy_inline_sgl()
648 * Prepare sgl of inlined data for sending. For userland callers
649 * function checks if given buffer addresses and len's are within
650 * process context bounds.
651 * Data from all provided sge's are copied together into the wqe,
652 * referenced by a single sge.
654 static int siw_copy_inline_sgl(const struct ib_send_wr *core_wr,
657 struct ib_sge *core_sge = core_wr->sg_list;
658 void *kbuf = &sqe->sge[1];
659 int num_sge = core_wr->num_sge, bytes = 0;
661 sqe->sge[0].laddr = (uintptr_t)kbuf;
662 sqe->sge[0].lkey = 0;
665 if (!core_sge->length) {
669 bytes += core_sge->length;
670 if (bytes > SIW_MAX_INLINE) {
674 memcpy(kbuf, ib_virt_dma_to_ptr(core_sge->addr),
677 kbuf += core_sge->length;
680 sqe->sge[0].length = max(bytes, 0);
681 sqe->num_sge = bytes > 0 ? 1 : 0;
686 /* Complete SQ WR's without processing */
687 static int siw_sq_flush_wr(struct siw_qp *qp, const struct ib_send_wr *wr,
688 const struct ib_send_wr **bad_wr)
693 struct siw_sqe sqe = {};
695 switch (wr->opcode) {
696 case IB_WR_RDMA_WRITE:
697 sqe.opcode = SIW_OP_WRITE;
699 case IB_WR_RDMA_READ:
700 sqe.opcode = SIW_OP_READ;
702 case IB_WR_RDMA_READ_WITH_INV:
703 sqe.opcode = SIW_OP_READ_LOCAL_INV;
706 sqe.opcode = SIW_OP_SEND;
708 case IB_WR_SEND_WITH_IMM:
709 sqe.opcode = SIW_OP_SEND_WITH_IMM;
711 case IB_WR_SEND_WITH_INV:
712 sqe.opcode = SIW_OP_SEND_REMOTE_INV;
714 case IB_WR_LOCAL_INV:
715 sqe.opcode = SIW_OP_INVAL_STAG;
718 sqe.opcode = SIW_OP_REG_MR;
726 rv = siw_sqe_complete(qp, &sqe, 0,
727 SIW_WC_WR_FLUSH_ERR);
739 /* Complete RQ WR's without processing */
740 static int siw_rq_flush_wr(struct siw_qp *qp, const struct ib_recv_wr *wr,
741 const struct ib_recv_wr **bad_wr)
743 struct siw_rqe rqe = {};
748 rv = siw_rqe_complete(qp, &rqe, 0, 0, SIW_WC_WR_FLUSH_ERR);
762 * Post a list of S-WR's to a SQ.
764 * @base_qp: Base QP contained in siw QP
765 * @wr: Null terminated list of user WR's
766 * @bad_wr: Points to failing WR in case of synchronous failure.
768 int siw_post_send(struct ib_qp *base_qp, const struct ib_send_wr *wr,
769 const struct ib_send_wr **bad_wr)
771 struct siw_qp *qp = to_siw_qp(base_qp);
772 struct siw_wqe *wqe = tx_wqe(qp);
777 if (wr && !rdma_is_kernel_res(&qp->base_qp.res)) {
778 siw_dbg_qp(qp, "wr must be empty for user mapped sq\n");
784 * Try to acquire QP state lock. Must be non-blocking
785 * to accommodate kernel clients needs.
787 if (!down_read_trylock(&qp->state_lock)) {
788 if (qp->attrs.state == SIW_QP_STATE_ERROR) {
790 * ERROR state is final, so we can be sure
791 * this state will not change as long as the QP
794 * This handles an ib_drain_sq() call with
795 * a concurrent request to set the QP state
798 rv = siw_sq_flush_wr(qp, wr, bad_wr);
800 siw_dbg_qp(qp, "QP locked, state %d\n",
807 if (unlikely(qp->attrs.state != SIW_QP_STATE_RTS)) {
808 if (qp->attrs.state == SIW_QP_STATE_ERROR) {
810 * Immediately flush this WR to CQ, if QP
811 * is in ERROR state. SQ is guaranteed to
812 * be empty, so WR complets in-order.
814 * Typically triggered by ib_drain_sq().
816 rv = siw_sq_flush_wr(qp, wr, bad_wr);
818 siw_dbg_qp(qp, "QP out of state %d\n",
823 up_read(&qp->state_lock);
826 spin_lock_irqsave(&qp->sq_lock, flags);
829 u32 idx = qp->sq_put % qp->attrs.sq_size;
830 struct siw_sqe *sqe = &qp->sendq[idx];
833 siw_dbg_qp(qp, "sq full\n");
837 if (wr->num_sge > qp->attrs.sq_max_sges) {
838 siw_dbg_qp(qp, "too many sge's: %d\n", wr->num_sge);
844 if ((wr->send_flags & IB_SEND_SIGNALED) ||
845 (qp->attrs.flags & SIW_SIGNAL_ALL_WR))
846 sqe->flags |= SIW_WQE_SIGNALLED;
848 if (wr->send_flags & IB_SEND_FENCE)
849 sqe->flags |= SIW_WQE_READ_FENCE;
851 switch (wr->opcode) {
853 case IB_WR_SEND_WITH_INV:
854 if (wr->send_flags & IB_SEND_SOLICITED)
855 sqe->flags |= SIW_WQE_SOLICITED;
857 if (!(wr->send_flags & IB_SEND_INLINE)) {
858 siw_copy_sgl(wr->sg_list, sqe->sge,
860 sqe->num_sge = wr->num_sge;
862 rv = siw_copy_inline_sgl(wr, sqe);
867 sqe->flags |= SIW_WQE_INLINE;
870 if (wr->opcode == IB_WR_SEND)
871 sqe->opcode = SIW_OP_SEND;
873 sqe->opcode = SIW_OP_SEND_REMOTE_INV;
874 sqe->rkey = wr->ex.invalidate_rkey;
878 case IB_WR_RDMA_READ_WITH_INV:
879 case IB_WR_RDMA_READ:
881 * iWarp restricts RREAD sink to SGL containing
882 * 1 SGE only. we could relax to SGL with multiple
883 * elements referring the SAME ltag or even sending
884 * a private per-rreq tag referring to a checked
885 * local sgl with MULTIPLE ltag's.
887 if (unlikely(wr->num_sge != 1)) {
891 siw_copy_sgl(wr->sg_list, &sqe->sge[0], 1);
893 * NOTE: zero length RREAD is allowed!
895 sqe->raddr = rdma_wr(wr)->remote_addr;
896 sqe->rkey = rdma_wr(wr)->rkey;
899 if (wr->opcode == IB_WR_RDMA_READ)
900 sqe->opcode = SIW_OP_READ;
902 sqe->opcode = SIW_OP_READ_LOCAL_INV;
905 case IB_WR_RDMA_WRITE:
906 if (!(wr->send_flags & IB_SEND_INLINE)) {
907 siw_copy_sgl(wr->sg_list, &sqe->sge[0],
909 sqe->num_sge = wr->num_sge;
911 rv = siw_copy_inline_sgl(wr, sqe);
912 if (unlikely(rv < 0)) {
916 sqe->flags |= SIW_WQE_INLINE;
919 sqe->raddr = rdma_wr(wr)->remote_addr;
920 sqe->rkey = rdma_wr(wr)->rkey;
921 sqe->opcode = SIW_OP_WRITE;
925 sqe->base_mr = (uintptr_t)reg_wr(wr)->mr;
926 sqe->rkey = reg_wr(wr)->key;
927 sqe->access = reg_wr(wr)->access & IWARP_ACCESS_MASK;
928 sqe->opcode = SIW_OP_REG_MR;
931 case IB_WR_LOCAL_INV:
932 sqe->rkey = wr->ex.invalidate_rkey;
933 sqe->opcode = SIW_OP_INVAL_STAG;
937 siw_dbg_qp(qp, "ib wr type %d unsupported\n",
942 siw_dbg_qp(qp, "opcode %d, flags 0x%x, wr_id 0x%pK\n",
943 sqe->opcode, sqe->flags,
944 (void *)(uintptr_t)sqe->id);
946 if (unlikely(rv < 0))
949 /* make SQE only valid after completely written */
951 sqe->flags |= SIW_WQE_VALID;
958 * Send directly if SQ processing is not in progress.
959 * Eventual immediate errors (rv < 0) do not affect the involved
960 * RI resources (Verbs, 8.3.1) and thus do not prevent from SQ
961 * processing, if new work is already pending. But rv must be passed
964 if (wqe->wr_status != SIW_WR_IDLE) {
965 spin_unlock_irqrestore(&qp->sq_lock, flags);
966 goto skip_direct_sending;
968 rv = siw_activate_tx(qp);
969 spin_unlock_irqrestore(&qp->sq_lock, flags);
972 goto skip_direct_sending;
974 if (rdma_is_kernel_res(&qp->base_qp.res)) {
975 rv = siw_sq_start(qp);
977 qp->tx_ctx.in_syscall = 1;
979 if (siw_qp_sq_process(qp) != 0 && !(qp->tx_ctx.tx_suspend))
980 siw_qp_cm_drop(qp, 0);
982 qp->tx_ctx.in_syscall = 0;
986 up_read(&qp->state_lock);
993 siw_dbg_qp(qp, "error %d\n", rv);
1000 * siw_post_receive()
1002 * Post a list of R-WR's to a RQ.
1004 * @base_qp: Base QP contained in siw QP
1005 * @wr: Null terminated list of user WR's
1006 * @bad_wr: Points to failing WR in case of synchronous failure.
1008 int siw_post_receive(struct ib_qp *base_qp, const struct ib_recv_wr *wr,
1009 const struct ib_recv_wr **bad_wr)
1011 struct siw_qp *qp = to_siw_qp(base_qp);
1012 unsigned long flags;
1015 if (qp->srq || qp->attrs.rq_size == 0) {
1019 if (!rdma_is_kernel_res(&qp->base_qp.res)) {
1020 siw_dbg_qp(qp, "no kernel post_recv for user mapped rq\n");
1026 * Try to acquire QP state lock. Must be non-blocking
1027 * to accommodate kernel clients needs.
1029 if (!down_read_trylock(&qp->state_lock)) {
1030 if (qp->attrs.state == SIW_QP_STATE_ERROR) {
1032 * ERROR state is final, so we can be sure
1033 * this state will not change as long as the QP
1036 * This handles an ib_drain_rq() call with
1037 * a concurrent request to set the QP state
1040 rv = siw_rq_flush_wr(qp, wr, bad_wr);
1042 siw_dbg_qp(qp, "QP locked, state %d\n",
1049 if (qp->attrs.state > SIW_QP_STATE_RTS) {
1050 if (qp->attrs.state == SIW_QP_STATE_ERROR) {
1052 * Immediately flush this WR to CQ, if QP
1053 * is in ERROR state. RQ is guaranteed to
1054 * be empty, so WR complets in-order.
1056 * Typically triggered by ib_drain_rq().
1058 rv = siw_rq_flush_wr(qp, wr, bad_wr);
1060 siw_dbg_qp(qp, "QP out of state %d\n",
1065 up_read(&qp->state_lock);
1069 * Serialize potentially multiple producers.
1070 * Not needed for single threaded consumer side.
1072 spin_lock_irqsave(&qp->rq_lock, flags);
1075 u32 idx = qp->rq_put % qp->attrs.rq_size;
1076 struct siw_rqe *rqe = &qp->recvq[idx];
1079 siw_dbg_qp(qp, "RQ full\n");
1083 if (wr->num_sge > qp->attrs.rq_max_sges) {
1084 siw_dbg_qp(qp, "too many sge's: %d\n", wr->num_sge);
1088 rqe->id = wr->wr_id;
1089 rqe->num_sge = wr->num_sge;
1090 siw_copy_sgl(wr->sg_list, rqe->sge, wr->num_sge);
1092 /* make sure RQE is completely written before valid */
1095 rqe->flags = SIW_WQE_VALID;
1100 spin_unlock_irqrestore(&qp->rq_lock, flags);
1102 up_read(&qp->state_lock);
1105 siw_dbg_qp(qp, "error %d\n", rv);
1108 return rv > 0 ? 0 : rv;
1111 int siw_destroy_cq(struct ib_cq *base_cq, struct ib_udata *udata)
1113 struct siw_cq *cq = to_siw_cq(base_cq);
1114 struct siw_device *sdev = to_siw_dev(base_cq->device);
1115 struct siw_ucontext *ctx =
1116 rdma_udata_to_drv_context(udata, struct siw_ucontext,
1119 siw_dbg_cq(cq, "free CQ resources\n");
1124 rdma_user_mmap_entry_remove(cq->cq_entry);
1126 atomic_dec(&sdev->num_cq);
1135 * Populate CQ of requested size
1137 * @base_cq: CQ as allocated by RDMA midlayer
1138 * @attr: Initial CQ attributes
1139 * @attrs: uverbs bundle
1142 int siw_create_cq(struct ib_cq *base_cq, const struct ib_cq_init_attr *attr,
1143 struct uverbs_attr_bundle *attrs)
1145 struct ib_udata *udata = &attrs->driver_udata;
1146 struct siw_device *sdev = to_siw_dev(base_cq->device);
1147 struct siw_cq *cq = to_siw_cq(base_cq);
1148 int rv, size = attr->cqe;
1153 if (atomic_inc_return(&sdev->num_cq) > SIW_MAX_CQ) {
1154 siw_dbg(base_cq->device, "too many CQ's\n");
1158 if (size < 1 || size > sdev->attrs.max_cqe) {
1159 siw_dbg(base_cq->device, "CQ size error: %d\n", size);
1163 size = roundup_pow_of_two(size);
1164 cq->base_cq.cqe = size;
1168 cq->queue = vmalloc_user(size * sizeof(struct siw_cqe) +
1169 sizeof(struct siw_cq_ctrl));
1171 cq->queue = vzalloc(size * sizeof(struct siw_cqe) +
1172 sizeof(struct siw_cq_ctrl));
1174 if (cq->queue == NULL) {
1178 get_random_bytes(&cq->id, 4);
1179 siw_dbg(base_cq->device, "new CQ [%u]\n", cq->id);
1181 spin_lock_init(&cq->lock);
1183 cq->notify = (struct siw_cq_ctrl *)&cq->queue[size];
1186 struct siw_uresp_create_cq uresp = {};
1187 struct siw_ucontext *ctx =
1188 rdma_udata_to_drv_context(udata, struct siw_ucontext,
1190 size_t length = size * sizeof(struct siw_cqe) +
1191 sizeof(struct siw_cq_ctrl);
1194 siw_mmap_entry_insert(ctx, cq->queue,
1195 length, &uresp.cq_key);
1196 if (!cq->cq_entry) {
1201 uresp.cq_id = cq->id;
1202 uresp.num_cqe = size;
1204 if (udata->outlen < sizeof(uresp)) {
1208 rv = ib_copy_to_udata(udata, &uresp, sizeof(uresp));
1215 siw_dbg(base_cq->device, "CQ creation failed: %d", rv);
1218 struct siw_ucontext *ctx =
1219 rdma_udata_to_drv_context(udata, struct siw_ucontext,
1222 rdma_user_mmap_entry_remove(cq->cq_entry);
1225 atomic_dec(&sdev->num_cq);
1233 * Reap CQ entries if available and copy work completion status into
1234 * array of WC's provided by caller. Returns number of reaped CQE's.
1236 * @base_cq: Base CQ contained in siw CQ.
1237 * @num_cqe: Maximum number of CQE's to reap.
1238 * @wc: Array of work completions to be filled by siw.
1240 int siw_poll_cq(struct ib_cq *base_cq, int num_cqe, struct ib_wc *wc)
1242 struct siw_cq *cq = to_siw_cq(base_cq);
1245 for (i = 0; i < num_cqe; i++) {
1246 if (!siw_reap_cqe(cq, wc))
1254 * siw_req_notify_cq()
1256 * Request notification for new CQE's added to that CQ.
1258 * o SIW_CQ_NOTIFY_SOLICITED lets siw trigger a notification
1259 * event if a WQE with notification flag set enters the CQ
1260 * o SIW_CQ_NOTIFY_NEXT_COMP lets siw trigger a notification
1261 * event if a WQE enters the CQ.
1262 * o IB_CQ_REPORT_MISSED_EVENTS: return value will provide the
1263 * number of not reaped CQE's regardless of its notification
1264 * type and current or new CQ notification settings.
1266 * @base_cq: Base CQ contained in siw CQ.
1267 * @flags: Requested notification flags.
1269 int siw_req_notify_cq(struct ib_cq *base_cq, enum ib_cq_notify_flags flags)
1271 struct siw_cq *cq = to_siw_cq(base_cq);
1273 siw_dbg_cq(cq, "flags: 0x%02x\n", flags);
1275 if ((flags & IB_CQ_SOLICITED_MASK) == IB_CQ_SOLICITED)
1277 * Enable CQ event for next solicited completion.
1278 * and make it visible to all associated producers.
1280 smp_store_mb(cq->notify->flags, SIW_NOTIFY_SOLICITED);
1283 * Enable CQ event for any signalled completion.
1284 * and make it visible to all associated producers.
1286 smp_store_mb(cq->notify->flags, SIW_NOTIFY_ALL);
1288 if (flags & IB_CQ_REPORT_MISSED_EVENTS)
1289 return cq->cq_put - cq->cq_get;
1297 * Release Memory Region.
1299 * @base_mr: Base MR contained in siw MR.
1300 * @udata: points to user context, unused.
1302 int siw_dereg_mr(struct ib_mr *base_mr, struct ib_udata *udata)
1304 struct siw_mr *mr = to_siw_mr(base_mr);
1305 struct siw_device *sdev = to_siw_dev(base_mr->device);
1307 siw_dbg_mem(mr->mem, "deregister MR\n");
1309 atomic_dec(&sdev->num_mr);
1311 siw_mr_drop_mem(mr);
1320 * Register Memory Region.
1322 * @pd: Protection Domain
1323 * @start: starting address of MR (virtual address)
1325 * @rnic_va: not used by siw
1326 * @rights: MR access rights
1327 * @udata: user buffer to communicate STag and Key.
1329 struct ib_mr *siw_reg_user_mr(struct ib_pd *pd, u64 start, u64 len,
1330 u64 rnic_va, int rights, struct ib_udata *udata)
1332 struct siw_mr *mr = NULL;
1333 struct siw_umem *umem = NULL;
1334 struct siw_ureq_reg_mr ureq;
1335 struct siw_device *sdev = to_siw_dev(pd->device);
1338 siw_dbg_pd(pd, "start: 0x%pK, va: 0x%pK, len: %llu\n",
1339 (void *)(uintptr_t)start, (void *)(uintptr_t)rnic_va,
1340 (unsigned long long)len);
1342 if (atomic_inc_return(&sdev->num_mr) > SIW_MAX_MR) {
1343 siw_dbg_pd(pd, "too many mr's\n");
1351 umem = siw_umem_get(pd->device, start, len, rights);
1354 siw_dbg_pd(pd, "getting user memory failed: %d\n", rv);
1358 mr = kzalloc(sizeof(*mr), GFP_KERNEL);
1363 rv = siw_mr_add_mem(mr, pd, umem, start, len, rights);
1368 struct siw_uresp_reg_mr uresp = {};
1369 struct siw_mem *mem = mr->mem;
1371 if (udata->inlen < sizeof(ureq)) {
1375 rv = ib_copy_from_udata(&ureq, udata, sizeof(ureq));
1379 mr->base_mr.lkey |= ureq.stag_key;
1380 mr->base_mr.rkey |= ureq.stag_key;
1381 mem->stag |= ureq.stag_key;
1382 uresp.stag = mem->stag;
1384 if (udata->outlen < sizeof(uresp)) {
1388 rv = ib_copy_to_udata(udata, &uresp, sizeof(uresp));
1392 mr->mem->stag_valid = 1;
1394 return &mr->base_mr;
1397 atomic_dec(&sdev->num_mr);
1400 siw_mr_drop_mem(mr);
1404 siw_umem_release(umem);
1409 struct ib_mr *siw_alloc_mr(struct ib_pd *pd, enum ib_mr_type mr_type,
1412 struct siw_device *sdev = to_siw_dev(pd->device);
1413 struct siw_mr *mr = NULL;
1414 struct siw_pbl *pbl = NULL;
1417 if (atomic_inc_return(&sdev->num_mr) > SIW_MAX_MR) {
1418 siw_dbg_pd(pd, "too many mr's\n");
1422 if (mr_type != IB_MR_TYPE_MEM_REG) {
1423 siw_dbg_pd(pd, "mr type %d unsupported\n", mr_type);
1427 if (max_sge > SIW_MAX_SGE_PBL) {
1428 siw_dbg_pd(pd, "too many sge's: %d\n", max_sge);
1432 pbl = siw_pbl_alloc(max_sge);
1435 siw_dbg_pd(pd, "pbl allocation failed: %d\n", rv);
1439 mr = kzalloc(sizeof(*mr), GFP_KERNEL);
1444 rv = siw_mr_add_mem(mr, pd, pbl, 0, max_sge * PAGE_SIZE, 0);
1448 mr->mem->is_pbl = 1;
1450 siw_dbg_pd(pd, "[MEM %u]: success\n", mr->mem->stag);
1452 return &mr->base_mr;
1455 atomic_dec(&sdev->num_mr);
1461 siw_mr_drop_mem(mr);
1464 siw_dbg_pd(pd, "failed: %d\n", rv);
1469 /* Just used to count number of pages being mapped */
1470 static int siw_set_pbl_page(struct ib_mr *base_mr, u64 buf_addr)
1475 int siw_map_mr_sg(struct ib_mr *base_mr, struct scatterlist *sl, int num_sle,
1476 unsigned int *sg_off)
1478 struct scatterlist *slp;
1479 struct siw_mr *mr = to_siw_mr(base_mr);
1480 struct siw_mem *mem = mr->mem;
1481 struct siw_pbl *pbl = mem->pbl;
1482 struct siw_pble *pble;
1483 unsigned long pbl_size;
1487 siw_dbg_mem(mem, "no PBL allocated\n");
1492 if (pbl->max_buf < num_sle) {
1493 siw_dbg_mem(mem, "too many SGE's: %d > %d\n",
1494 num_sle, pbl->max_buf);
1497 for_each_sg(sl, slp, num_sle, i) {
1498 if (sg_dma_len(slp) == 0) {
1499 siw_dbg_mem(mem, "empty SGE\n");
1503 pble->addr = sg_dma_address(slp);
1504 pble->size = sg_dma_len(slp);
1506 pbl_size = pble->size;
1509 /* Merge PBL entries if adjacent */
1510 if (pble->addr + pble->size == sg_dma_address(slp)) {
1511 pble->size += sg_dma_len(slp);
1515 pble->addr = sg_dma_address(slp);
1516 pble->size = sg_dma_len(slp);
1517 pble->pbl_off = pbl_size;
1519 pbl_size += sg_dma_len(slp);
1522 "sge[%d], size %u, addr 0x%p, total %lu\n",
1523 i, pble->size, ib_virt_dma_to_ptr(pble->addr),
1526 rv = ib_sg_to_pages(base_mr, sl, num_sle, sg_off, siw_set_pbl_page);
1528 mem->len = base_mr->length;
1529 mem->va = base_mr->iova;
1531 "%llu bytes, start 0x%pK, %u SLE to %u entries\n",
1532 mem->len, (void *)(uintptr_t)mem->va, num_sle,
1541 * Create a (empty) DMA memory region, where no umem is attached.
1543 struct ib_mr *siw_get_dma_mr(struct ib_pd *pd, int rights)
1545 struct siw_device *sdev = to_siw_dev(pd->device);
1546 struct siw_mr *mr = NULL;
1549 if (atomic_inc_return(&sdev->num_mr) > SIW_MAX_MR) {
1550 siw_dbg_pd(pd, "too many mr's\n");
1554 mr = kzalloc(sizeof(*mr), GFP_KERNEL);
1559 rv = siw_mr_add_mem(mr, pd, NULL, 0, ULONG_MAX, rights);
1563 mr->mem->stag_valid = 1;
1565 siw_dbg_pd(pd, "[MEM %u]: success\n", mr->mem->stag);
1567 return &mr->base_mr;
1573 atomic_dec(&sdev->num_mr);
1581 * Create Shared Receive Queue of attributes @init_attrs
1582 * within protection domain given by @pd.
1584 * @base_srq: Base SRQ contained in siw SRQ.
1585 * @init_attrs: SRQ init attributes.
1586 * @udata: points to user context
1588 int siw_create_srq(struct ib_srq *base_srq,
1589 struct ib_srq_init_attr *init_attrs, struct ib_udata *udata)
1591 struct siw_srq *srq = to_siw_srq(base_srq);
1592 struct ib_srq_attr *attrs = &init_attrs->attr;
1593 struct siw_device *sdev = to_siw_dev(base_srq->device);
1594 struct siw_ucontext *ctx =
1595 rdma_udata_to_drv_context(udata, struct siw_ucontext,
1599 if (init_attrs->srq_type != IB_SRQT_BASIC)
1602 if (atomic_inc_return(&sdev->num_srq) > SIW_MAX_SRQ) {
1603 siw_dbg_pd(base_srq->pd, "too many SRQ's\n");
1607 if (attrs->max_wr == 0 || attrs->max_wr > SIW_MAX_SRQ_WR ||
1608 attrs->max_sge > SIW_MAX_SGE || attrs->srq_limit > attrs->max_wr) {
1612 srq->max_sge = attrs->max_sge;
1613 srq->num_rqe = roundup_pow_of_two(attrs->max_wr);
1614 srq->limit = attrs->srq_limit;
1618 srq->is_kernel_res = !udata;
1622 vmalloc_user(srq->num_rqe * sizeof(struct siw_rqe));
1624 srq->recvq = vcalloc(srq->num_rqe, sizeof(struct siw_rqe));
1626 if (srq->recvq == NULL) {
1631 struct siw_uresp_create_srq uresp = {};
1632 size_t length = srq->num_rqe * sizeof(struct siw_rqe);
1635 siw_mmap_entry_insert(ctx, srq->recvq,
1636 length, &uresp.srq_key);
1637 if (!srq->srq_entry) {
1642 uresp.num_rqe = srq->num_rqe;
1644 if (udata->outlen < sizeof(uresp)) {
1648 rv = ib_copy_to_udata(udata, &uresp, sizeof(uresp));
1652 spin_lock_init(&srq->lock);
1654 siw_dbg_pd(base_srq->pd, "[SRQ]: success\n");
1661 rdma_user_mmap_entry_remove(srq->srq_entry);
1664 atomic_dec(&sdev->num_srq);
1672 * Modify SRQ. The caller may resize SRQ and/or set/reset notification
1673 * limit and (re)arm IB_EVENT_SRQ_LIMIT_REACHED notification.
1675 * NOTE: it is unclear if RDMA core allows for changing the MAX_SGE
1676 * parameter. siw_modify_srq() does not check the attrs->max_sge param.
1678 int siw_modify_srq(struct ib_srq *base_srq, struct ib_srq_attr *attrs,
1679 enum ib_srq_attr_mask attr_mask, struct ib_udata *udata)
1681 struct siw_srq *srq = to_siw_srq(base_srq);
1682 unsigned long flags;
1685 spin_lock_irqsave(&srq->lock, flags);
1687 if (attr_mask & IB_SRQ_MAX_WR) {
1688 /* resize request not yet supported */
1692 if (attr_mask & IB_SRQ_LIMIT) {
1693 if (attrs->srq_limit) {
1694 if (unlikely(attrs->srq_limit > srq->num_rqe)) {
1702 srq->limit = attrs->srq_limit;
1705 spin_unlock_irqrestore(&srq->lock, flags);
1713 * Query SRQ attributes.
1715 int siw_query_srq(struct ib_srq *base_srq, struct ib_srq_attr *attrs)
1717 struct siw_srq *srq = to_siw_srq(base_srq);
1718 unsigned long flags;
1720 spin_lock_irqsave(&srq->lock, flags);
1722 attrs->max_wr = srq->num_rqe;
1723 attrs->max_sge = srq->max_sge;
1724 attrs->srq_limit = srq->limit;
1726 spin_unlock_irqrestore(&srq->lock, flags);
1735 * It is assumed that the SRQ is not referenced by any
1736 * QP anymore - the code trusts the RDMA core environment to keep track
1739 int siw_destroy_srq(struct ib_srq *base_srq, struct ib_udata *udata)
1741 struct siw_srq *srq = to_siw_srq(base_srq);
1742 struct siw_device *sdev = to_siw_dev(base_srq->device);
1743 struct siw_ucontext *ctx =
1744 rdma_udata_to_drv_context(udata, struct siw_ucontext,
1748 rdma_user_mmap_entry_remove(srq->srq_entry);
1750 atomic_dec(&sdev->num_srq);
1755 * siw_post_srq_recv()
1757 * Post a list of receive queue elements to SRQ.
1758 * NOTE: The function does not check or lock a certain SRQ state
1759 * during the post operation. The code simply trusts the
1760 * RDMA core environment.
1762 * @base_srq: Base SRQ contained in siw SRQ
1763 * @wr: List of R-WR's
1764 * @bad_wr: Updated to failing WR if posting fails.
1766 int siw_post_srq_recv(struct ib_srq *base_srq, const struct ib_recv_wr *wr,
1767 const struct ib_recv_wr **bad_wr)
1769 struct siw_srq *srq = to_siw_srq(base_srq);
1770 unsigned long flags;
1773 if (unlikely(!srq->is_kernel_res)) {
1774 siw_dbg_pd(base_srq->pd,
1775 "[SRQ]: no kernel post_recv for mapped srq\n");
1780 * Serialize potentially multiple producers.
1781 * Also needed to serialize potentially multiple
1784 spin_lock_irqsave(&srq->lock, flags);
1787 u32 idx = srq->rq_put % srq->num_rqe;
1788 struct siw_rqe *rqe = &srq->recvq[idx];
1791 siw_dbg_pd(base_srq->pd, "SRQ full\n");
1795 if (unlikely(wr->num_sge > srq->max_sge)) {
1796 siw_dbg_pd(base_srq->pd,
1797 "[SRQ]: too many sge's: %d\n", wr->num_sge);
1801 rqe->id = wr->wr_id;
1802 rqe->num_sge = wr->num_sge;
1803 siw_copy_sgl(wr->sg_list, rqe->sge, wr->num_sge);
1805 /* Make sure S-RQE is completely written before valid */
1808 rqe->flags = SIW_WQE_VALID;
1813 spin_unlock_irqrestore(&srq->lock, flags);
1815 if (unlikely(rv < 0)) {
1816 siw_dbg_pd(base_srq->pd, "[SRQ]: error %d\n", rv);
1822 void siw_qp_event(struct siw_qp *qp, enum ib_event_type etype)
1824 struct ib_event event;
1825 struct ib_qp *base_qp = &qp->base_qp;
1828 * Do not report asynchronous errors on QP which gets
1829 * destroyed via verbs interface (siw_destroy_qp())
1831 if (qp->attrs.flags & SIW_QP_IN_DESTROY)
1834 event.event = etype;
1835 event.device = base_qp->device;
1836 event.element.qp = base_qp;
1838 if (base_qp->event_handler) {
1839 siw_dbg_qp(qp, "reporting event %d\n", etype);
1840 base_qp->event_handler(&event, base_qp->qp_context);
1844 void siw_cq_event(struct siw_cq *cq, enum ib_event_type etype)
1846 struct ib_event event;
1847 struct ib_cq *base_cq = &cq->base_cq;
1849 event.event = etype;
1850 event.device = base_cq->device;
1851 event.element.cq = base_cq;
1853 if (base_cq->event_handler) {
1854 siw_dbg_cq(cq, "reporting CQ event %d\n", etype);
1855 base_cq->event_handler(&event, base_cq->cq_context);
1859 void siw_srq_event(struct siw_srq *srq, enum ib_event_type etype)
1861 struct ib_event event;
1862 struct ib_srq *base_srq = &srq->base_srq;
1864 event.event = etype;
1865 event.device = base_srq->device;
1866 event.element.srq = base_srq;
1868 if (base_srq->event_handler) {
1869 siw_dbg_pd(srq->base_srq.pd,
1870 "reporting SRQ event %d\n", etype);
1871 base_srq->event_handler(&event, base_srq->srq_context);
1875 void siw_port_event(struct siw_device *sdev, u32 port, enum ib_event_type etype)
1877 struct ib_event event;
1879 event.event = etype;
1880 event.device = &sdev->base_dev;
1881 event.element.port_num = port;
1883 siw_dbg(&sdev->base_dev, "reporting port event %d\n", etype);
1885 ib_dispatch_event(&event);