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->phys_state = (netif_running(ndev) && netif_carrier_ok(ndev)) ?
193 IB_PORT_PHYS_STATE_LINK_UP : IB_PORT_PHYS_STATE_DISABLED;
194 attr->state = attr->phys_state == IB_PORT_PHYS_STATE_LINK_UP ?
195 IB_PORT_ACTIVE : IB_PORT_DOWN;
196 attr->port_cap_flags = IB_PORT_CM_SUP | IB_PORT_DEVICE_MGMT_SUP;
201 * attr->bad_pkey_cntr = 0;
202 * attr->qkey_viol_cntr = 0;
205 * attr->max_vl_num = 0;
207 * attr->subnet_timeout = 0;
208 * attr->init_type_repy = 0;
214 int siw_get_port_immutable(struct ib_device *base_dev, u32 port,
215 struct ib_port_immutable *port_immutable)
217 struct ib_port_attr attr;
218 int rv = siw_query_port(base_dev, port, &attr);
223 port_immutable->gid_tbl_len = attr.gid_tbl_len;
224 port_immutable->core_cap_flags = RDMA_CORE_PORT_IWARP;
229 int siw_query_gid(struct ib_device *base_dev, u32 port, int idx,
232 struct siw_device *sdev = to_siw_dev(base_dev);
234 /* subnet_prefix == interface_id == 0; */
235 memset(gid, 0, sizeof(*gid));
236 memcpy(gid->raw, sdev->raw_gid, ETH_ALEN);
241 int siw_alloc_pd(struct ib_pd *pd, struct ib_udata *udata)
243 struct siw_device *sdev = to_siw_dev(pd->device);
245 if (atomic_inc_return(&sdev->num_pd) > SIW_MAX_PD) {
246 atomic_dec(&sdev->num_pd);
249 siw_dbg_pd(pd, "now %d PD's(s)\n", atomic_read(&sdev->num_pd));
254 int siw_dealloc_pd(struct ib_pd *pd, struct ib_udata *udata)
256 struct siw_device *sdev = to_siw_dev(pd->device);
258 siw_dbg_pd(pd, "free PD\n");
259 atomic_dec(&sdev->num_pd);
263 void siw_qp_get_ref(struct ib_qp *base_qp)
265 siw_qp_get(to_siw_qp(base_qp));
268 void siw_qp_put_ref(struct ib_qp *base_qp)
270 siw_qp_put(to_siw_qp(base_qp));
273 static struct rdma_user_mmap_entry *
274 siw_mmap_entry_insert(struct siw_ucontext *uctx,
275 void *address, size_t length,
278 struct siw_user_mmap_entry *entry = kzalloc(sizeof(*entry), GFP_KERNEL);
281 *offset = SIW_INVAL_UOBJ_KEY;
285 entry->address = address;
287 rv = rdma_user_mmap_entry_insert(&uctx->base_ucontext,
295 *offset = rdma_user_mmap_get_offset(&entry->rdma_entry);
297 return &entry->rdma_entry;
303 * Create QP of requested size on given device.
306 * @attrs: Initial QP attributes.
307 * @udata: used to provide QP ID, SQ and RQ size back to user.
310 int siw_create_qp(struct ib_qp *ibqp, struct ib_qp_init_attr *attrs,
311 struct ib_udata *udata)
313 struct ib_pd *pd = ibqp->pd;
314 struct siw_qp *qp = to_siw_qp(ibqp);
315 struct ib_device *base_dev = pd->device;
316 struct siw_device *sdev = to_siw_dev(base_dev);
317 struct siw_ucontext *uctx =
318 rdma_udata_to_drv_context(udata, struct siw_ucontext,
321 int num_sqe, num_rqe, rv = 0;
324 siw_dbg(base_dev, "create new QP\n");
326 if (attrs->create_flags)
329 if (atomic_inc_return(&sdev->num_qp) > SIW_MAX_QP) {
330 siw_dbg(base_dev, "too many QP's\n");
334 if (attrs->qp_type != IB_QPT_RC) {
335 siw_dbg(base_dev, "only RC QP's supported\n");
339 if ((attrs->cap.max_send_wr > SIW_MAX_QP_WR) ||
340 (attrs->cap.max_recv_wr > SIW_MAX_QP_WR) ||
341 (attrs->cap.max_send_sge > SIW_MAX_SGE) ||
342 (attrs->cap.max_recv_sge > SIW_MAX_SGE)) {
343 siw_dbg(base_dev, "QP size error\n");
347 if (attrs->cap.max_inline_data > SIW_MAX_INLINE) {
348 siw_dbg(base_dev, "max inline send: %d > %d\n",
349 attrs->cap.max_inline_data, (int)SIW_MAX_INLINE);
354 * NOTE: we don't allow for a QP unable to hold any SQ WQE
356 if (attrs->cap.max_send_wr == 0) {
357 siw_dbg(base_dev, "QP must have send queue\n");
362 if (!attrs->send_cq || (!attrs->recv_cq && !attrs->srq)) {
363 siw_dbg(base_dev, "send CQ or receive CQ invalid\n");
368 init_rwsem(&qp->state_lock);
369 spin_lock_init(&qp->sq_lock);
370 spin_lock_init(&qp->rq_lock);
371 spin_lock_init(&qp->orq_lock);
373 rv = siw_qp_add(sdev, qp);
378 /* All queue indices are derived from modulo operations
379 * on a free running 'get' (consumer) and 'put' (producer)
380 * unsigned counter. Having queue sizes at power of two
381 * avoids handling counter wrap around.
383 num_sqe = roundup_pow_of_two(attrs->cap.max_send_wr);
384 num_rqe = attrs->cap.max_recv_wr;
386 num_rqe = roundup_pow_of_two(num_rqe);
389 qp->sendq = vmalloc_user(num_sqe * sizeof(struct siw_sqe));
391 qp->sendq = vcalloc(num_sqe, sizeof(struct siw_sqe));
393 if (qp->sendq == NULL) {
397 if (attrs->sq_sig_type != IB_SIGNAL_REQ_WR) {
398 if (attrs->sq_sig_type == IB_SIGNAL_ALL_WR)
399 qp->attrs.flags |= SIW_SIGNAL_ALL_WR;
406 qp->scq = to_siw_cq(attrs->send_cq);
407 qp->rcq = to_siw_cq(attrs->recv_cq);
412 * Verbs 6.3.7: ignore RQ size, if SRQ present
413 * Verbs 6.3.5: do not check PD of SRQ against PD of QP
415 qp->srq = to_siw_srq(attrs->srq);
416 qp->attrs.rq_size = 0;
417 siw_dbg(base_dev, "QP [%u]: SRQ attached\n",
419 } else if (num_rqe) {
422 vmalloc_user(num_rqe * sizeof(struct siw_rqe));
424 qp->recvq = vcalloc(num_rqe, sizeof(struct siw_rqe));
426 if (qp->recvq == NULL) {
430 qp->attrs.rq_size = num_rqe;
432 qp->attrs.sq_size = num_sqe;
433 qp->attrs.sq_max_sges = attrs->cap.max_send_sge;
434 qp->attrs.rq_max_sges = attrs->cap.max_recv_sge;
436 /* Make those two tunables fixed for now. */
437 qp->tx_ctx.gso_seg_limit = 1;
438 qp->tx_ctx.zcopy_tx = zcopy_tx;
440 qp->attrs.state = SIW_QP_STATE_IDLE;
443 struct siw_uresp_create_qp uresp = {};
445 uresp.num_sqe = num_sqe;
446 uresp.num_rqe = num_rqe;
447 uresp.qp_id = qp_id(qp);
450 length = num_sqe * sizeof(struct siw_sqe);
452 siw_mmap_entry_insert(uctx, qp->sendq,
453 length, &uresp.sq_key);
461 length = num_rqe * sizeof(struct siw_rqe);
463 siw_mmap_entry_insert(uctx, qp->recvq,
464 length, &uresp.rq_key);
466 uresp.sq_key = SIW_INVAL_UOBJ_KEY;
472 if (udata->outlen < sizeof(uresp)) {
476 rv = ib_copy_to_udata(udata, &uresp, sizeof(uresp));
480 qp->tx_cpu = siw_get_tx_cpu(sdev);
481 if (qp->tx_cpu < 0) {
485 INIT_LIST_HEAD(&qp->devq);
486 spin_lock_irqsave(&sdev->lock, flags);
487 list_add_tail(&qp->devq, &sdev->qp_list);
488 spin_unlock_irqrestore(&sdev->lock, flags);
490 init_completion(&qp->qp_free);
495 xa_erase(&sdev->qp_xa, qp_id(qp));
497 rdma_user_mmap_entry_remove(qp->sq_entry);
498 rdma_user_mmap_entry_remove(qp->rq_entry);
504 atomic_dec(&sdev->num_qp);
509 * Minimum siw_query_qp() verb interface.
511 * @qp_attr_mask is not used but all available information is provided
513 int siw_query_qp(struct ib_qp *base_qp, struct ib_qp_attr *qp_attr,
514 int qp_attr_mask, struct ib_qp_init_attr *qp_init_attr)
517 struct net_device *ndev;
519 if (base_qp && qp_attr && qp_init_attr)
520 qp = to_siw_qp(base_qp);
524 ndev = ib_device_get_netdev(base_qp->device, SIW_PORT);
528 qp_attr->qp_state = siw_qp_state_to_ib_qp_state[qp->attrs.state];
529 qp_attr->cap.max_inline_data = SIW_MAX_INLINE;
530 qp_attr->cap.max_send_wr = qp->attrs.sq_size;
531 qp_attr->cap.max_send_sge = qp->attrs.sq_max_sges;
532 qp_attr->cap.max_recv_wr = qp->attrs.rq_size;
533 qp_attr->cap.max_recv_sge = qp->attrs.rq_max_sges;
534 qp_attr->path_mtu = ib_mtu_int_to_enum(READ_ONCE(ndev->mtu));
535 qp_attr->max_rd_atomic = qp->attrs.irq_size;
536 qp_attr->max_dest_rd_atomic = qp->attrs.orq_size;
538 qp_attr->qp_access_flags = IB_ACCESS_LOCAL_WRITE |
539 IB_ACCESS_REMOTE_WRITE |
540 IB_ACCESS_REMOTE_READ;
542 qp_init_attr->qp_type = base_qp->qp_type;
543 qp_init_attr->send_cq = base_qp->send_cq;
544 qp_init_attr->recv_cq = base_qp->recv_cq;
545 qp_init_attr->srq = base_qp->srq;
547 qp_init_attr->cap = qp_attr->cap;
553 int siw_verbs_modify_qp(struct ib_qp *base_qp, struct ib_qp_attr *attr,
554 int attr_mask, struct ib_udata *udata)
556 struct siw_qp_attrs new_attrs;
557 enum siw_qp_attr_mask siw_attr_mask = 0;
558 struct siw_qp *qp = to_siw_qp(base_qp);
564 if (attr_mask & ~IB_QP_ATTR_STANDARD_BITS)
567 memset(&new_attrs, 0, sizeof(new_attrs));
569 if (attr_mask & IB_QP_ACCESS_FLAGS) {
570 siw_attr_mask = SIW_QP_ATTR_ACCESS_FLAGS;
572 if (attr->qp_access_flags & IB_ACCESS_REMOTE_READ)
573 new_attrs.flags |= SIW_RDMA_READ_ENABLED;
574 if (attr->qp_access_flags & IB_ACCESS_REMOTE_WRITE)
575 new_attrs.flags |= SIW_RDMA_WRITE_ENABLED;
576 if (attr->qp_access_flags & IB_ACCESS_MW_BIND)
577 new_attrs.flags |= SIW_RDMA_BIND_ENABLED;
579 if (attr_mask & IB_QP_STATE) {
580 siw_dbg_qp(qp, "desired IB QP state: %s\n",
581 ib_qp_state_to_string[attr->qp_state]);
583 new_attrs.state = ib_qp_state_to_siw_qp_state[attr->qp_state];
585 if (new_attrs.state > SIW_QP_STATE_RTS)
586 qp->tx_ctx.tx_suspend = 1;
588 siw_attr_mask |= SIW_QP_ATTR_STATE;
593 down_write(&qp->state_lock);
595 rv = siw_qp_modify(qp, &new_attrs, siw_attr_mask);
597 up_write(&qp->state_lock);
602 int siw_destroy_qp(struct ib_qp *base_qp, struct ib_udata *udata)
604 struct siw_qp *qp = to_siw_qp(base_qp);
605 struct siw_ucontext *uctx =
606 rdma_udata_to_drv_context(udata, struct siw_ucontext,
608 struct siw_qp_attrs qp_attrs;
610 siw_dbg_qp(qp, "state %d\n", qp->attrs.state);
613 * Mark QP as in process of destruction to prevent from
614 * any async callbacks to RDMA core
616 qp->attrs.flags |= SIW_QP_IN_DESTROY;
617 qp->rx_stream.rx_suspend = 1;
620 rdma_user_mmap_entry_remove(qp->sq_entry);
621 rdma_user_mmap_entry_remove(qp->rq_entry);
624 down_write(&qp->state_lock);
626 qp_attrs.state = SIW_QP_STATE_ERROR;
627 siw_qp_modify(qp, &qp_attrs, SIW_QP_ATTR_STATE);
630 siw_cep_put(qp->cep);
633 up_write(&qp->state_lock);
635 kfree(qp->tx_ctx.mpa_crc_hd);
636 kfree(qp->rx_stream.mpa_crc_hd);
638 qp->scq = qp->rcq = NULL;
641 wait_for_completion(&qp->qp_free);
647 * siw_copy_inline_sgl()
649 * Prepare sgl of inlined data for sending. For userland callers
650 * function checks if given buffer addresses and len's are within
651 * process context bounds.
652 * Data from all provided sge's are copied together into the wqe,
653 * referenced by a single sge.
655 static int siw_copy_inline_sgl(const struct ib_send_wr *core_wr,
658 struct ib_sge *core_sge = core_wr->sg_list;
659 void *kbuf = &sqe->sge[1];
660 int num_sge = core_wr->num_sge, bytes = 0;
662 sqe->sge[0].laddr = (uintptr_t)kbuf;
663 sqe->sge[0].lkey = 0;
666 if (!core_sge->length) {
670 bytes += core_sge->length;
671 if (bytes > SIW_MAX_INLINE) {
675 memcpy(kbuf, ib_virt_dma_to_ptr(core_sge->addr),
678 kbuf += core_sge->length;
681 sqe->sge[0].length = max(bytes, 0);
682 sqe->num_sge = bytes > 0 ? 1 : 0;
687 /* Complete SQ WR's without processing */
688 static int siw_sq_flush_wr(struct siw_qp *qp, const struct ib_send_wr *wr,
689 const struct ib_send_wr **bad_wr)
694 struct siw_sqe sqe = {};
696 switch (wr->opcode) {
697 case IB_WR_RDMA_WRITE:
698 sqe.opcode = SIW_OP_WRITE;
700 case IB_WR_RDMA_READ:
701 sqe.opcode = SIW_OP_READ;
703 case IB_WR_RDMA_READ_WITH_INV:
704 sqe.opcode = SIW_OP_READ_LOCAL_INV;
707 sqe.opcode = SIW_OP_SEND;
709 case IB_WR_SEND_WITH_IMM:
710 sqe.opcode = SIW_OP_SEND_WITH_IMM;
712 case IB_WR_SEND_WITH_INV:
713 sqe.opcode = SIW_OP_SEND_REMOTE_INV;
715 case IB_WR_LOCAL_INV:
716 sqe.opcode = SIW_OP_INVAL_STAG;
719 sqe.opcode = SIW_OP_REG_MR;
727 rv = siw_sqe_complete(qp, &sqe, 0,
728 SIW_WC_WR_FLUSH_ERR);
740 /* Complete RQ WR's without processing */
741 static int siw_rq_flush_wr(struct siw_qp *qp, const struct ib_recv_wr *wr,
742 const struct ib_recv_wr **bad_wr)
744 struct siw_rqe rqe = {};
749 rv = siw_rqe_complete(qp, &rqe, 0, 0, SIW_WC_WR_FLUSH_ERR);
763 * Post a list of S-WR's to a SQ.
765 * @base_qp: Base QP contained in siw QP
766 * @wr: Null terminated list of user WR's
767 * @bad_wr: Points to failing WR in case of synchronous failure.
769 int siw_post_send(struct ib_qp *base_qp, const struct ib_send_wr *wr,
770 const struct ib_send_wr **bad_wr)
772 struct siw_qp *qp = to_siw_qp(base_qp);
773 struct siw_wqe *wqe = tx_wqe(qp);
778 if (wr && !rdma_is_kernel_res(&qp->base_qp.res)) {
779 siw_dbg_qp(qp, "wr must be empty for user mapped sq\n");
785 * Try to acquire QP state lock. Must be non-blocking
786 * to accommodate kernel clients needs.
788 if (!down_read_trylock(&qp->state_lock)) {
789 if (qp->attrs.state == SIW_QP_STATE_ERROR) {
791 * ERROR state is final, so we can be sure
792 * this state will not change as long as the QP
795 * This handles an ib_drain_sq() call with
796 * a concurrent request to set the QP state
799 rv = siw_sq_flush_wr(qp, wr, bad_wr);
801 siw_dbg_qp(qp, "QP locked, state %d\n",
808 if (unlikely(qp->attrs.state != SIW_QP_STATE_RTS)) {
809 if (qp->attrs.state == SIW_QP_STATE_ERROR) {
811 * Immediately flush this WR to CQ, if QP
812 * is in ERROR state. SQ is guaranteed to
813 * be empty, so WR complets in-order.
815 * Typically triggered by ib_drain_sq().
817 rv = siw_sq_flush_wr(qp, wr, bad_wr);
819 siw_dbg_qp(qp, "QP out of state %d\n",
824 up_read(&qp->state_lock);
827 spin_lock_irqsave(&qp->sq_lock, flags);
830 u32 idx = qp->sq_put % qp->attrs.sq_size;
831 struct siw_sqe *sqe = &qp->sendq[idx];
834 siw_dbg_qp(qp, "sq full\n");
838 if (wr->num_sge > qp->attrs.sq_max_sges) {
839 siw_dbg_qp(qp, "too many sge's: %d\n", wr->num_sge);
845 if ((wr->send_flags & IB_SEND_SIGNALED) ||
846 (qp->attrs.flags & SIW_SIGNAL_ALL_WR))
847 sqe->flags |= SIW_WQE_SIGNALLED;
849 if (wr->send_flags & IB_SEND_FENCE)
850 sqe->flags |= SIW_WQE_READ_FENCE;
852 switch (wr->opcode) {
854 case IB_WR_SEND_WITH_INV:
855 if (wr->send_flags & IB_SEND_SOLICITED)
856 sqe->flags |= SIW_WQE_SOLICITED;
858 if (!(wr->send_flags & IB_SEND_INLINE)) {
859 siw_copy_sgl(wr->sg_list, sqe->sge,
861 sqe->num_sge = wr->num_sge;
863 rv = siw_copy_inline_sgl(wr, sqe);
868 sqe->flags |= SIW_WQE_INLINE;
871 if (wr->opcode == IB_WR_SEND)
872 sqe->opcode = SIW_OP_SEND;
874 sqe->opcode = SIW_OP_SEND_REMOTE_INV;
875 sqe->rkey = wr->ex.invalidate_rkey;
879 case IB_WR_RDMA_READ_WITH_INV:
880 case IB_WR_RDMA_READ:
882 * iWarp restricts RREAD sink to SGL containing
883 * 1 SGE only. we could relax to SGL with multiple
884 * elements referring the SAME ltag or even sending
885 * a private per-rreq tag referring to a checked
886 * local sgl with MULTIPLE ltag's.
888 if (unlikely(wr->num_sge != 1)) {
892 siw_copy_sgl(wr->sg_list, &sqe->sge[0], 1);
894 * NOTE: zero length RREAD is allowed!
896 sqe->raddr = rdma_wr(wr)->remote_addr;
897 sqe->rkey = rdma_wr(wr)->rkey;
900 if (wr->opcode == IB_WR_RDMA_READ)
901 sqe->opcode = SIW_OP_READ;
903 sqe->opcode = SIW_OP_READ_LOCAL_INV;
906 case IB_WR_RDMA_WRITE:
907 if (!(wr->send_flags & IB_SEND_INLINE)) {
908 siw_copy_sgl(wr->sg_list, &sqe->sge[0],
910 sqe->num_sge = wr->num_sge;
912 rv = siw_copy_inline_sgl(wr, sqe);
913 if (unlikely(rv < 0)) {
917 sqe->flags |= SIW_WQE_INLINE;
920 sqe->raddr = rdma_wr(wr)->remote_addr;
921 sqe->rkey = rdma_wr(wr)->rkey;
922 sqe->opcode = SIW_OP_WRITE;
926 sqe->base_mr = (uintptr_t)reg_wr(wr)->mr;
927 sqe->rkey = reg_wr(wr)->key;
928 sqe->access = reg_wr(wr)->access & IWARP_ACCESS_MASK;
929 sqe->opcode = SIW_OP_REG_MR;
932 case IB_WR_LOCAL_INV:
933 sqe->rkey = wr->ex.invalidate_rkey;
934 sqe->opcode = SIW_OP_INVAL_STAG;
938 siw_dbg_qp(qp, "ib wr type %d unsupported\n",
943 siw_dbg_qp(qp, "opcode %d, flags 0x%x, wr_id 0x%pK\n",
944 sqe->opcode, sqe->flags,
945 (void *)(uintptr_t)sqe->id);
947 if (unlikely(rv < 0))
950 /* make SQE only valid after completely written */
952 sqe->flags |= SIW_WQE_VALID;
959 * Send directly if SQ processing is not in progress.
960 * Eventual immediate errors (rv < 0) do not affect the involved
961 * RI resources (Verbs, 8.3.1) and thus do not prevent from SQ
962 * processing, if new work is already pending. But rv must be passed
965 if (wqe->wr_status != SIW_WR_IDLE) {
966 spin_unlock_irqrestore(&qp->sq_lock, flags);
967 goto skip_direct_sending;
969 rv = siw_activate_tx(qp);
970 spin_unlock_irqrestore(&qp->sq_lock, flags);
973 goto skip_direct_sending;
975 if (rdma_is_kernel_res(&qp->base_qp.res)) {
976 rv = siw_sq_start(qp);
978 qp->tx_ctx.in_syscall = 1;
980 if (siw_qp_sq_process(qp) != 0 && !(qp->tx_ctx.tx_suspend))
981 siw_qp_cm_drop(qp, 0);
983 qp->tx_ctx.in_syscall = 0;
987 up_read(&qp->state_lock);
994 siw_dbg_qp(qp, "error %d\n", rv);
1001 * siw_post_receive()
1003 * Post a list of R-WR's to a RQ.
1005 * @base_qp: Base QP contained in siw QP
1006 * @wr: Null terminated list of user WR's
1007 * @bad_wr: Points to failing WR in case of synchronous failure.
1009 int siw_post_receive(struct ib_qp *base_qp, const struct ib_recv_wr *wr,
1010 const struct ib_recv_wr **bad_wr)
1012 struct siw_qp *qp = to_siw_qp(base_qp);
1013 unsigned long flags;
1016 if (qp->srq || qp->attrs.rq_size == 0) {
1020 if (!rdma_is_kernel_res(&qp->base_qp.res)) {
1021 siw_dbg_qp(qp, "no kernel post_recv for user mapped rq\n");
1027 * Try to acquire QP state lock. Must be non-blocking
1028 * to accommodate kernel clients needs.
1030 if (!down_read_trylock(&qp->state_lock)) {
1031 if (qp->attrs.state == SIW_QP_STATE_ERROR) {
1033 * ERROR state is final, so we can be sure
1034 * this state will not change as long as the QP
1037 * This handles an ib_drain_rq() call with
1038 * a concurrent request to set the QP state
1041 rv = siw_rq_flush_wr(qp, wr, bad_wr);
1043 siw_dbg_qp(qp, "QP locked, state %d\n",
1050 if (qp->attrs.state > SIW_QP_STATE_RTS) {
1051 if (qp->attrs.state == SIW_QP_STATE_ERROR) {
1053 * Immediately flush this WR to CQ, if QP
1054 * is in ERROR state. RQ is guaranteed to
1055 * be empty, so WR complets in-order.
1057 * Typically triggered by ib_drain_rq().
1059 rv = siw_rq_flush_wr(qp, wr, bad_wr);
1061 siw_dbg_qp(qp, "QP out of state %d\n",
1066 up_read(&qp->state_lock);
1070 * Serialize potentially multiple producers.
1071 * Not needed for single threaded consumer side.
1073 spin_lock_irqsave(&qp->rq_lock, flags);
1076 u32 idx = qp->rq_put % qp->attrs.rq_size;
1077 struct siw_rqe *rqe = &qp->recvq[idx];
1080 siw_dbg_qp(qp, "RQ full\n");
1084 if (wr->num_sge > qp->attrs.rq_max_sges) {
1085 siw_dbg_qp(qp, "too many sge's: %d\n", wr->num_sge);
1089 rqe->id = wr->wr_id;
1090 rqe->num_sge = wr->num_sge;
1091 siw_copy_sgl(wr->sg_list, rqe->sge, wr->num_sge);
1093 /* make sure RQE is completely written before valid */
1096 rqe->flags = SIW_WQE_VALID;
1101 spin_unlock_irqrestore(&qp->rq_lock, flags);
1103 up_read(&qp->state_lock);
1106 siw_dbg_qp(qp, "error %d\n", rv);
1109 return rv > 0 ? 0 : rv;
1112 int siw_destroy_cq(struct ib_cq *base_cq, struct ib_udata *udata)
1114 struct siw_cq *cq = to_siw_cq(base_cq);
1115 struct siw_device *sdev = to_siw_dev(base_cq->device);
1116 struct siw_ucontext *ctx =
1117 rdma_udata_to_drv_context(udata, struct siw_ucontext,
1120 siw_dbg_cq(cq, "free CQ resources\n");
1125 rdma_user_mmap_entry_remove(cq->cq_entry);
1127 atomic_dec(&sdev->num_cq);
1136 * Populate CQ of requested size
1138 * @base_cq: CQ as allocated by RDMA midlayer
1139 * @attr: Initial CQ attributes
1140 * @attrs: uverbs bundle
1143 int siw_create_cq(struct ib_cq *base_cq, const struct ib_cq_init_attr *attr,
1144 struct uverbs_attr_bundle *attrs)
1146 struct ib_udata *udata = &attrs->driver_udata;
1147 struct siw_device *sdev = to_siw_dev(base_cq->device);
1148 struct siw_cq *cq = to_siw_cq(base_cq);
1149 int rv, size = attr->cqe;
1154 if (atomic_inc_return(&sdev->num_cq) > SIW_MAX_CQ) {
1155 siw_dbg(base_cq->device, "too many CQ's\n");
1159 if (size < 1 || size > sdev->attrs.max_cqe) {
1160 siw_dbg(base_cq->device, "CQ size error: %d\n", size);
1164 size = roundup_pow_of_two(size);
1165 cq->base_cq.cqe = size;
1169 cq->queue = vmalloc_user(size * sizeof(struct siw_cqe) +
1170 sizeof(struct siw_cq_ctrl));
1172 cq->queue = vzalloc(size * sizeof(struct siw_cqe) +
1173 sizeof(struct siw_cq_ctrl));
1175 if (cq->queue == NULL) {
1179 get_random_bytes(&cq->id, 4);
1180 siw_dbg(base_cq->device, "new CQ [%u]\n", cq->id);
1182 spin_lock_init(&cq->lock);
1184 cq->notify = (struct siw_cq_ctrl *)&cq->queue[size];
1187 struct siw_uresp_create_cq uresp = {};
1188 struct siw_ucontext *ctx =
1189 rdma_udata_to_drv_context(udata, struct siw_ucontext,
1191 size_t length = size * sizeof(struct siw_cqe) +
1192 sizeof(struct siw_cq_ctrl);
1195 siw_mmap_entry_insert(ctx, cq->queue,
1196 length, &uresp.cq_key);
1197 if (!cq->cq_entry) {
1202 uresp.cq_id = cq->id;
1203 uresp.num_cqe = size;
1205 if (udata->outlen < sizeof(uresp)) {
1209 rv = ib_copy_to_udata(udata, &uresp, sizeof(uresp));
1216 siw_dbg(base_cq->device, "CQ creation failed: %d", rv);
1219 struct siw_ucontext *ctx =
1220 rdma_udata_to_drv_context(udata, struct siw_ucontext,
1223 rdma_user_mmap_entry_remove(cq->cq_entry);
1226 atomic_dec(&sdev->num_cq);
1234 * Reap CQ entries if available and copy work completion status into
1235 * array of WC's provided by caller. Returns number of reaped CQE's.
1237 * @base_cq: Base CQ contained in siw CQ.
1238 * @num_cqe: Maximum number of CQE's to reap.
1239 * @wc: Array of work completions to be filled by siw.
1241 int siw_poll_cq(struct ib_cq *base_cq, int num_cqe, struct ib_wc *wc)
1243 struct siw_cq *cq = to_siw_cq(base_cq);
1246 for (i = 0; i < num_cqe; i++) {
1247 if (!siw_reap_cqe(cq, wc))
1255 * siw_req_notify_cq()
1257 * Request notification for new CQE's added to that CQ.
1259 * o SIW_CQ_NOTIFY_SOLICITED lets siw trigger a notification
1260 * event if a WQE with notification flag set enters the CQ
1261 * o SIW_CQ_NOTIFY_NEXT_COMP lets siw trigger a notification
1262 * event if a WQE enters the CQ.
1263 * o IB_CQ_REPORT_MISSED_EVENTS: return value will provide the
1264 * number of not reaped CQE's regardless of its notification
1265 * type and current or new CQ notification settings.
1267 * @base_cq: Base CQ contained in siw CQ.
1268 * @flags: Requested notification flags.
1270 int siw_req_notify_cq(struct ib_cq *base_cq, enum ib_cq_notify_flags flags)
1272 struct siw_cq *cq = to_siw_cq(base_cq);
1274 siw_dbg_cq(cq, "flags: 0x%02x\n", flags);
1276 if ((flags & IB_CQ_SOLICITED_MASK) == IB_CQ_SOLICITED)
1278 * Enable CQ event for next solicited completion.
1279 * and make it visible to all associated producers.
1281 smp_store_mb(cq->notify->flags, SIW_NOTIFY_SOLICITED);
1284 * Enable CQ event for any signalled completion.
1285 * and make it visible to all associated producers.
1287 smp_store_mb(cq->notify->flags, SIW_NOTIFY_ALL);
1289 if (flags & IB_CQ_REPORT_MISSED_EVENTS)
1290 return cq->cq_put - cq->cq_get;
1298 * Release Memory Region.
1300 * @base_mr: Base MR contained in siw MR.
1301 * @udata: points to user context, unused.
1303 int siw_dereg_mr(struct ib_mr *base_mr, struct ib_udata *udata)
1305 struct siw_mr *mr = to_siw_mr(base_mr);
1306 struct siw_device *sdev = to_siw_dev(base_mr->device);
1308 siw_dbg_mem(mr->mem, "deregister MR\n");
1310 atomic_dec(&sdev->num_mr);
1312 siw_mr_drop_mem(mr);
1321 * Register Memory Region.
1323 * @pd: Protection Domain
1324 * @start: starting address of MR (virtual address)
1326 * @rnic_va: not used by siw
1327 * @rights: MR access rights
1328 * @udata: user buffer to communicate STag and Key.
1330 struct ib_mr *siw_reg_user_mr(struct ib_pd *pd, u64 start, u64 len,
1331 u64 rnic_va, int rights, struct ib_udata *udata)
1333 struct siw_mr *mr = NULL;
1334 struct siw_umem *umem = NULL;
1335 struct siw_ureq_reg_mr ureq;
1336 struct siw_device *sdev = to_siw_dev(pd->device);
1339 siw_dbg_pd(pd, "start: 0x%pK, va: 0x%pK, len: %llu\n",
1340 (void *)(uintptr_t)start, (void *)(uintptr_t)rnic_va,
1341 (unsigned long long)len);
1343 if (atomic_inc_return(&sdev->num_mr) > SIW_MAX_MR) {
1344 siw_dbg_pd(pd, "too many mr's\n");
1352 umem = siw_umem_get(pd->device, start, len, rights);
1355 siw_dbg_pd(pd, "getting user memory failed: %d\n", rv);
1359 mr = kzalloc(sizeof(*mr), GFP_KERNEL);
1364 rv = siw_mr_add_mem(mr, pd, umem, start, len, rights);
1369 struct siw_uresp_reg_mr uresp = {};
1370 struct siw_mem *mem = mr->mem;
1372 if (udata->inlen < sizeof(ureq)) {
1376 rv = ib_copy_from_udata(&ureq, udata, sizeof(ureq));
1380 mr->base_mr.lkey |= ureq.stag_key;
1381 mr->base_mr.rkey |= ureq.stag_key;
1382 mem->stag |= ureq.stag_key;
1383 uresp.stag = mem->stag;
1385 if (udata->outlen < sizeof(uresp)) {
1389 rv = ib_copy_to_udata(udata, &uresp, sizeof(uresp));
1393 mr->mem->stag_valid = 1;
1395 return &mr->base_mr;
1398 atomic_dec(&sdev->num_mr);
1401 siw_mr_drop_mem(mr);
1405 siw_umem_release(umem);
1410 struct ib_mr *siw_alloc_mr(struct ib_pd *pd, enum ib_mr_type mr_type,
1413 struct siw_device *sdev = to_siw_dev(pd->device);
1414 struct siw_mr *mr = NULL;
1415 struct siw_pbl *pbl = NULL;
1418 if (atomic_inc_return(&sdev->num_mr) > SIW_MAX_MR) {
1419 siw_dbg_pd(pd, "too many mr's\n");
1423 if (mr_type != IB_MR_TYPE_MEM_REG) {
1424 siw_dbg_pd(pd, "mr type %d unsupported\n", mr_type);
1428 if (max_sge > SIW_MAX_SGE_PBL) {
1429 siw_dbg_pd(pd, "too many sge's: %d\n", max_sge);
1433 pbl = siw_pbl_alloc(max_sge);
1436 siw_dbg_pd(pd, "pbl allocation failed: %d\n", rv);
1440 mr = kzalloc(sizeof(*mr), GFP_KERNEL);
1445 rv = siw_mr_add_mem(mr, pd, pbl, 0, max_sge * PAGE_SIZE, 0);
1449 mr->mem->is_pbl = 1;
1451 siw_dbg_pd(pd, "[MEM %u]: success\n", mr->mem->stag);
1453 return &mr->base_mr;
1456 atomic_dec(&sdev->num_mr);
1462 siw_mr_drop_mem(mr);
1465 siw_dbg_pd(pd, "failed: %d\n", rv);
1470 /* Just used to count number of pages being mapped */
1471 static int siw_set_pbl_page(struct ib_mr *base_mr, u64 buf_addr)
1476 int siw_map_mr_sg(struct ib_mr *base_mr, struct scatterlist *sl, int num_sle,
1477 unsigned int *sg_off)
1479 struct scatterlist *slp;
1480 struct siw_mr *mr = to_siw_mr(base_mr);
1481 struct siw_mem *mem = mr->mem;
1482 struct siw_pbl *pbl = mem->pbl;
1483 struct siw_pble *pble;
1484 unsigned long pbl_size;
1488 siw_dbg_mem(mem, "no PBL allocated\n");
1493 if (pbl->max_buf < num_sle) {
1494 siw_dbg_mem(mem, "too many SGE's: %d > %d\n",
1495 num_sle, pbl->max_buf);
1498 for_each_sg(sl, slp, num_sle, i) {
1499 if (sg_dma_len(slp) == 0) {
1500 siw_dbg_mem(mem, "empty SGE\n");
1504 pble->addr = sg_dma_address(slp);
1505 pble->size = sg_dma_len(slp);
1507 pbl_size = pble->size;
1510 /* Merge PBL entries if adjacent */
1511 if (pble->addr + pble->size == sg_dma_address(slp)) {
1512 pble->size += sg_dma_len(slp);
1516 pble->addr = sg_dma_address(slp);
1517 pble->size = sg_dma_len(slp);
1518 pble->pbl_off = pbl_size;
1520 pbl_size += sg_dma_len(slp);
1523 "sge[%d], size %u, addr 0x%p, total %lu\n",
1524 i, pble->size, ib_virt_dma_to_ptr(pble->addr),
1527 rv = ib_sg_to_pages(base_mr, sl, num_sle, sg_off, siw_set_pbl_page);
1529 mem->len = base_mr->length;
1530 mem->va = base_mr->iova;
1532 "%llu bytes, start 0x%pK, %u SLE to %u entries\n",
1533 mem->len, (void *)(uintptr_t)mem->va, num_sle,
1542 * Create a (empty) DMA memory region, where no umem is attached.
1544 struct ib_mr *siw_get_dma_mr(struct ib_pd *pd, int rights)
1546 struct siw_device *sdev = to_siw_dev(pd->device);
1547 struct siw_mr *mr = NULL;
1550 if (atomic_inc_return(&sdev->num_mr) > SIW_MAX_MR) {
1551 siw_dbg_pd(pd, "too many mr's\n");
1555 mr = kzalloc(sizeof(*mr), GFP_KERNEL);
1560 rv = siw_mr_add_mem(mr, pd, NULL, 0, ULONG_MAX, rights);
1564 mr->mem->stag_valid = 1;
1566 siw_dbg_pd(pd, "[MEM %u]: success\n", mr->mem->stag);
1568 return &mr->base_mr;
1574 atomic_dec(&sdev->num_mr);
1582 * Create Shared Receive Queue of attributes @init_attrs
1583 * within protection domain given by @pd.
1585 * @base_srq: Base SRQ contained in siw SRQ.
1586 * @init_attrs: SRQ init attributes.
1587 * @udata: points to user context
1589 int siw_create_srq(struct ib_srq *base_srq,
1590 struct ib_srq_init_attr *init_attrs, struct ib_udata *udata)
1592 struct siw_srq *srq = to_siw_srq(base_srq);
1593 struct ib_srq_attr *attrs = &init_attrs->attr;
1594 struct siw_device *sdev = to_siw_dev(base_srq->device);
1595 struct siw_ucontext *ctx =
1596 rdma_udata_to_drv_context(udata, struct siw_ucontext,
1600 if (init_attrs->srq_type != IB_SRQT_BASIC)
1603 if (atomic_inc_return(&sdev->num_srq) > SIW_MAX_SRQ) {
1604 siw_dbg_pd(base_srq->pd, "too many SRQ's\n");
1608 if (attrs->max_wr == 0 || attrs->max_wr > SIW_MAX_SRQ_WR ||
1609 attrs->max_sge > SIW_MAX_SGE || attrs->srq_limit > attrs->max_wr) {
1613 srq->max_sge = attrs->max_sge;
1614 srq->num_rqe = roundup_pow_of_two(attrs->max_wr);
1615 srq->limit = attrs->srq_limit;
1619 srq->is_kernel_res = !udata;
1623 vmalloc_user(srq->num_rqe * sizeof(struct siw_rqe));
1625 srq->recvq = vcalloc(srq->num_rqe, sizeof(struct siw_rqe));
1627 if (srq->recvq == NULL) {
1632 struct siw_uresp_create_srq uresp = {};
1633 size_t length = srq->num_rqe * sizeof(struct siw_rqe);
1636 siw_mmap_entry_insert(ctx, srq->recvq,
1637 length, &uresp.srq_key);
1638 if (!srq->srq_entry) {
1643 uresp.num_rqe = srq->num_rqe;
1645 if (udata->outlen < sizeof(uresp)) {
1649 rv = ib_copy_to_udata(udata, &uresp, sizeof(uresp));
1653 spin_lock_init(&srq->lock);
1655 siw_dbg_pd(base_srq->pd, "[SRQ]: success\n");
1662 rdma_user_mmap_entry_remove(srq->srq_entry);
1665 atomic_dec(&sdev->num_srq);
1673 * Modify SRQ. The caller may resize SRQ and/or set/reset notification
1674 * limit and (re)arm IB_EVENT_SRQ_LIMIT_REACHED notification.
1676 * NOTE: it is unclear if RDMA core allows for changing the MAX_SGE
1677 * parameter. siw_modify_srq() does not check the attrs->max_sge param.
1679 int siw_modify_srq(struct ib_srq *base_srq, struct ib_srq_attr *attrs,
1680 enum ib_srq_attr_mask attr_mask, struct ib_udata *udata)
1682 struct siw_srq *srq = to_siw_srq(base_srq);
1683 unsigned long flags;
1686 spin_lock_irqsave(&srq->lock, flags);
1688 if (attr_mask & IB_SRQ_MAX_WR) {
1689 /* resize request not yet supported */
1693 if (attr_mask & IB_SRQ_LIMIT) {
1694 if (attrs->srq_limit) {
1695 if (unlikely(attrs->srq_limit > srq->num_rqe)) {
1703 srq->limit = attrs->srq_limit;
1706 spin_unlock_irqrestore(&srq->lock, flags);
1714 * Query SRQ attributes.
1716 int siw_query_srq(struct ib_srq *base_srq, struct ib_srq_attr *attrs)
1718 struct siw_srq *srq = to_siw_srq(base_srq);
1719 unsigned long flags;
1721 spin_lock_irqsave(&srq->lock, flags);
1723 attrs->max_wr = srq->num_rqe;
1724 attrs->max_sge = srq->max_sge;
1725 attrs->srq_limit = srq->limit;
1727 spin_unlock_irqrestore(&srq->lock, flags);
1736 * It is assumed that the SRQ is not referenced by any
1737 * QP anymore - the code trusts the RDMA core environment to keep track
1740 int siw_destroy_srq(struct ib_srq *base_srq, struct ib_udata *udata)
1742 struct siw_srq *srq = to_siw_srq(base_srq);
1743 struct siw_device *sdev = to_siw_dev(base_srq->device);
1744 struct siw_ucontext *ctx =
1745 rdma_udata_to_drv_context(udata, struct siw_ucontext,
1749 rdma_user_mmap_entry_remove(srq->srq_entry);
1751 atomic_dec(&sdev->num_srq);
1756 * siw_post_srq_recv()
1758 * Post a list of receive queue elements to SRQ.
1759 * NOTE: The function does not check or lock a certain SRQ state
1760 * during the post operation. The code simply trusts the
1761 * RDMA core environment.
1763 * @base_srq: Base SRQ contained in siw SRQ
1764 * @wr: List of R-WR's
1765 * @bad_wr: Updated to failing WR if posting fails.
1767 int siw_post_srq_recv(struct ib_srq *base_srq, const struct ib_recv_wr *wr,
1768 const struct ib_recv_wr **bad_wr)
1770 struct siw_srq *srq = to_siw_srq(base_srq);
1771 unsigned long flags;
1774 if (unlikely(!srq->is_kernel_res)) {
1775 siw_dbg_pd(base_srq->pd,
1776 "[SRQ]: no kernel post_recv for mapped srq\n");
1781 * Serialize potentially multiple producers.
1782 * Also needed to serialize potentially multiple
1785 spin_lock_irqsave(&srq->lock, flags);
1788 u32 idx = srq->rq_put % srq->num_rqe;
1789 struct siw_rqe *rqe = &srq->recvq[idx];
1792 siw_dbg_pd(base_srq->pd, "SRQ full\n");
1796 if (unlikely(wr->num_sge > srq->max_sge)) {
1797 siw_dbg_pd(base_srq->pd,
1798 "[SRQ]: too many sge's: %d\n", wr->num_sge);
1802 rqe->id = wr->wr_id;
1803 rqe->num_sge = wr->num_sge;
1804 siw_copy_sgl(wr->sg_list, rqe->sge, wr->num_sge);
1806 /* Make sure S-RQE is completely written before valid */
1809 rqe->flags = SIW_WQE_VALID;
1814 spin_unlock_irqrestore(&srq->lock, flags);
1816 if (unlikely(rv < 0)) {
1817 siw_dbg_pd(base_srq->pd, "[SRQ]: error %d\n", rv);
1823 void siw_qp_event(struct siw_qp *qp, enum ib_event_type etype)
1825 struct ib_event event;
1826 struct ib_qp *base_qp = &qp->base_qp;
1829 * Do not report asynchronous errors on QP which gets
1830 * destroyed via verbs interface (siw_destroy_qp())
1832 if (qp->attrs.flags & SIW_QP_IN_DESTROY)
1835 event.event = etype;
1836 event.device = base_qp->device;
1837 event.element.qp = base_qp;
1839 if (base_qp->event_handler) {
1840 siw_dbg_qp(qp, "reporting event %d\n", etype);
1841 base_qp->event_handler(&event, base_qp->qp_context);
1845 void siw_cq_event(struct siw_cq *cq, enum ib_event_type etype)
1847 struct ib_event event;
1848 struct ib_cq *base_cq = &cq->base_cq;
1850 event.event = etype;
1851 event.device = base_cq->device;
1852 event.element.cq = base_cq;
1854 if (base_cq->event_handler) {
1855 siw_dbg_cq(cq, "reporting CQ event %d\n", etype);
1856 base_cq->event_handler(&event, base_cq->cq_context);
1860 void siw_srq_event(struct siw_srq *srq, enum ib_event_type etype)
1862 struct ib_event event;
1863 struct ib_srq *base_srq = &srq->base_srq;
1865 event.event = etype;
1866 event.device = base_srq->device;
1867 event.element.srq = base_srq;
1869 if (base_srq->event_handler) {
1870 siw_dbg_pd(srq->base_srq.pd,
1871 "reporting SRQ event %d\n", etype);
1872 base_srq->event_handler(&event, base_srq->srq_context);
1876 void siw_port_event(struct siw_device *sdev, u32 port, enum ib_event_type etype)
1878 struct ib_event event;
1880 event.event = etype;
1881 event.device = &sdev->base_dev;
1882 event.element.port_num = port;
1884 siw_dbg(&sdev->base_dev, "reporting port event %d\n", etype);
1886 ib_dispatch_event(&event);