xref: /openbmc/linux/drivers/infiniband/sw/siw/siw_verbs.c (revision 514aee660df493cd673154a6ba6bab745ec47b8c)
1303ae1cdSBernard Metzler // SPDX-License-Identifier: GPL-2.0 or BSD-3-Clause
2303ae1cdSBernard Metzler 
3303ae1cdSBernard Metzler /* Authors: Bernard Metzler <bmt@zurich.ibm.com> */
4303ae1cdSBernard Metzler /* Copyright (c) 2008-2019, IBM Corporation */
5303ae1cdSBernard Metzler 
6303ae1cdSBernard Metzler #include <linux/errno.h>
7303ae1cdSBernard Metzler #include <linux/types.h>
8303ae1cdSBernard Metzler #include <linux/uaccess.h>
9303ae1cdSBernard Metzler #include <linux/vmalloc.h>
10303ae1cdSBernard Metzler #include <linux/xarray.h>
11303ae1cdSBernard Metzler 
12303ae1cdSBernard Metzler #include <rdma/iw_cm.h>
13303ae1cdSBernard Metzler #include <rdma/ib_verbs.h>
14303ae1cdSBernard Metzler #include <rdma/ib_user_verbs.h>
15303ae1cdSBernard Metzler #include <rdma/uverbs_ioctl.h>
16303ae1cdSBernard Metzler 
17303ae1cdSBernard Metzler #include "siw.h"
18303ae1cdSBernard Metzler #include "siw_verbs.h"
19303ae1cdSBernard Metzler #include "siw_mem.h"
20303ae1cdSBernard Metzler 
21303ae1cdSBernard Metzler static int ib_qp_state_to_siw_qp_state[IB_QPS_ERR + 1] = {
22303ae1cdSBernard Metzler 	[IB_QPS_RESET] = SIW_QP_STATE_IDLE,
23303ae1cdSBernard Metzler 	[IB_QPS_INIT] = SIW_QP_STATE_IDLE,
24303ae1cdSBernard Metzler 	[IB_QPS_RTR] = SIW_QP_STATE_RTR,
25303ae1cdSBernard Metzler 	[IB_QPS_RTS] = SIW_QP_STATE_RTS,
26303ae1cdSBernard Metzler 	[IB_QPS_SQD] = SIW_QP_STATE_CLOSING,
27303ae1cdSBernard Metzler 	[IB_QPS_SQE] = SIW_QP_STATE_TERMINATE,
28303ae1cdSBernard Metzler 	[IB_QPS_ERR] = SIW_QP_STATE_ERROR
29303ae1cdSBernard Metzler };
30303ae1cdSBernard Metzler 
31303ae1cdSBernard Metzler static char ib_qp_state_to_string[IB_QPS_ERR + 1][sizeof("RESET")] = {
32303ae1cdSBernard Metzler 	[IB_QPS_RESET] = "RESET", [IB_QPS_INIT] = "INIT", [IB_QPS_RTR] = "RTR",
33303ae1cdSBernard Metzler 	[IB_QPS_RTS] = "RTS",     [IB_QPS_SQD] = "SQD",   [IB_QPS_SQE] = "SQE",
34303ae1cdSBernard Metzler 	[IB_QPS_ERR] = "ERR"
35303ae1cdSBernard Metzler };
36303ae1cdSBernard Metzler 
3711f1a755SMichal Kalderon void siw_mmap_free(struct rdma_user_mmap_entry *rdma_entry)
38303ae1cdSBernard Metzler {
3911f1a755SMichal Kalderon 	struct siw_user_mmap_entry *entry = to_siw_mmap_entry(rdma_entry);
40303ae1cdSBernard Metzler 
4111f1a755SMichal Kalderon 	kfree(entry);
42303ae1cdSBernard Metzler }
43303ae1cdSBernard Metzler 
44303ae1cdSBernard Metzler int siw_mmap(struct ib_ucontext *ctx, struct vm_area_struct *vma)
45303ae1cdSBernard Metzler {
46303ae1cdSBernard Metzler 	struct siw_ucontext *uctx = to_siw_ctx(ctx);
4711f1a755SMichal Kalderon 	size_t size = vma->vm_end - vma->vm_start;
4811f1a755SMichal Kalderon 	struct rdma_user_mmap_entry *rdma_entry;
4911f1a755SMichal Kalderon 	struct siw_user_mmap_entry *entry;
50303ae1cdSBernard Metzler 	int rv = -EINVAL;
51303ae1cdSBernard Metzler 
52303ae1cdSBernard Metzler 	/*
53303ae1cdSBernard Metzler 	 * Must be page aligned
54303ae1cdSBernard Metzler 	 */
55303ae1cdSBernard Metzler 	if (vma->vm_start & (PAGE_SIZE - 1)) {
56303ae1cdSBernard Metzler 		pr_warn("siw: mmap not page aligned\n");
5711f1a755SMichal Kalderon 		return -EINVAL;
5811f1a755SMichal Kalderon 	}
5911f1a755SMichal Kalderon 	rdma_entry = rdma_user_mmap_entry_get(&uctx->base_ucontext, vma);
6011f1a755SMichal Kalderon 	if (!rdma_entry) {
6111f1a755SMichal Kalderon 		siw_dbg(&uctx->sdev->base_dev, "mmap lookup failed: %lu, %#zx\n",
6211f1a755SMichal Kalderon 			vma->vm_pgoff, size);
6311f1a755SMichal Kalderon 		return -EINVAL;
6411f1a755SMichal Kalderon 	}
6511f1a755SMichal Kalderon 	entry = to_siw_mmap_entry(rdma_entry);
6611f1a755SMichal Kalderon 
6711f1a755SMichal Kalderon 	rv = remap_vmalloc_range(vma, entry->address, 0);
6811f1a755SMichal Kalderon 	if (rv) {
6911f1a755SMichal Kalderon 		pr_warn("remap_vmalloc_range failed: %lu, %zu\n", vma->vm_pgoff,
7011f1a755SMichal Kalderon 			size);
71303ae1cdSBernard Metzler 		goto out;
72303ae1cdSBernard Metzler 	}
73303ae1cdSBernard Metzler out:
7411f1a755SMichal Kalderon 	rdma_user_mmap_entry_put(rdma_entry);
7511f1a755SMichal Kalderon 
76303ae1cdSBernard Metzler 	return rv;
77303ae1cdSBernard Metzler }
78303ae1cdSBernard Metzler 
79303ae1cdSBernard Metzler int siw_alloc_ucontext(struct ib_ucontext *base_ctx, struct ib_udata *udata)
80303ae1cdSBernard Metzler {
81303ae1cdSBernard Metzler 	struct siw_device *sdev = to_siw_dev(base_ctx->device);
82303ae1cdSBernard Metzler 	struct siw_ucontext *ctx = to_siw_ctx(base_ctx);
83303ae1cdSBernard Metzler 	struct siw_uresp_alloc_ctx uresp = {};
84303ae1cdSBernard Metzler 	int rv;
85303ae1cdSBernard Metzler 
86303ae1cdSBernard Metzler 	if (atomic_inc_return(&sdev->num_ctx) > SIW_MAX_CONTEXT) {
87303ae1cdSBernard Metzler 		rv = -ENOMEM;
88303ae1cdSBernard Metzler 		goto err_out;
89303ae1cdSBernard Metzler 	}
90303ae1cdSBernard Metzler 	ctx->sdev = sdev;
91303ae1cdSBernard Metzler 
92303ae1cdSBernard Metzler 	uresp.dev_id = sdev->vendor_part_id;
93303ae1cdSBernard Metzler 
94303ae1cdSBernard Metzler 	if (udata->outlen < sizeof(uresp)) {
95303ae1cdSBernard Metzler 		rv = -EINVAL;
96303ae1cdSBernard Metzler 		goto err_out;
97303ae1cdSBernard Metzler 	}
98303ae1cdSBernard Metzler 	rv = ib_copy_to_udata(udata, &uresp, sizeof(uresp));
99303ae1cdSBernard Metzler 	if (rv)
100303ae1cdSBernard Metzler 		goto err_out;
101303ae1cdSBernard Metzler 
102303ae1cdSBernard Metzler 	siw_dbg(base_ctx->device, "success. now %d context(s)\n",
103303ae1cdSBernard Metzler 		atomic_read(&sdev->num_ctx));
104303ae1cdSBernard Metzler 
105303ae1cdSBernard Metzler 	return 0;
106303ae1cdSBernard Metzler 
107303ae1cdSBernard Metzler err_out:
108303ae1cdSBernard Metzler 	atomic_dec(&sdev->num_ctx);
109303ae1cdSBernard Metzler 	siw_dbg(base_ctx->device, "failure %d. now %d context(s)\n", rv,
110303ae1cdSBernard Metzler 		atomic_read(&sdev->num_ctx));
111303ae1cdSBernard Metzler 
112303ae1cdSBernard Metzler 	return rv;
113303ae1cdSBernard Metzler }
114303ae1cdSBernard Metzler 
115303ae1cdSBernard Metzler void siw_dealloc_ucontext(struct ib_ucontext *base_ctx)
116303ae1cdSBernard Metzler {
117303ae1cdSBernard Metzler 	struct siw_ucontext *uctx = to_siw_ctx(base_ctx);
118303ae1cdSBernard Metzler 
119303ae1cdSBernard Metzler 	atomic_dec(&uctx->sdev->num_ctx);
120303ae1cdSBernard Metzler }
121303ae1cdSBernard Metzler 
122303ae1cdSBernard Metzler int siw_query_device(struct ib_device *base_dev, struct ib_device_attr *attr,
123303ae1cdSBernard Metzler 		     struct ib_udata *udata)
124303ae1cdSBernard Metzler {
125303ae1cdSBernard Metzler 	struct siw_device *sdev = to_siw_dev(base_dev);
126303ae1cdSBernard Metzler 
127303ae1cdSBernard Metzler 	if (udata->inlen || udata->outlen)
128303ae1cdSBernard Metzler 		return -EINVAL;
129303ae1cdSBernard Metzler 
130303ae1cdSBernard Metzler 	memset(attr, 0, sizeof(*attr));
131303ae1cdSBernard Metzler 
132303ae1cdSBernard Metzler 	/* Revisit atomic caps if RFC 7306 gets supported */
133303ae1cdSBernard Metzler 	attr->atomic_cap = 0;
134303ae1cdSBernard Metzler 	attr->device_cap_flags =
135303ae1cdSBernard Metzler 		IB_DEVICE_MEM_MGT_EXTENSIONS | IB_DEVICE_ALLOW_USER_UNREG;
136303ae1cdSBernard Metzler 	attr->max_cq = sdev->attrs.max_cq;
137303ae1cdSBernard Metzler 	attr->max_cqe = sdev->attrs.max_cqe;
138303ae1cdSBernard Metzler 	attr->max_fast_reg_page_list_len = SIW_MAX_SGE_PBL;
139303ae1cdSBernard Metzler 	attr->max_mr = sdev->attrs.max_mr;
140303ae1cdSBernard Metzler 	attr->max_mw = sdev->attrs.max_mw;
141303ae1cdSBernard Metzler 	attr->max_mr_size = ~0ull;
142303ae1cdSBernard Metzler 	attr->max_pd = sdev->attrs.max_pd;
143303ae1cdSBernard Metzler 	attr->max_qp = sdev->attrs.max_qp;
144303ae1cdSBernard Metzler 	attr->max_qp_init_rd_atom = sdev->attrs.max_ird;
145303ae1cdSBernard Metzler 	attr->max_qp_rd_atom = sdev->attrs.max_ord;
146303ae1cdSBernard Metzler 	attr->max_qp_wr = sdev->attrs.max_qp_wr;
147303ae1cdSBernard Metzler 	attr->max_recv_sge = sdev->attrs.max_sge;
148303ae1cdSBernard Metzler 	attr->max_res_rd_atom = sdev->attrs.max_qp * sdev->attrs.max_ird;
149303ae1cdSBernard Metzler 	attr->max_send_sge = sdev->attrs.max_sge;
150303ae1cdSBernard Metzler 	attr->max_sge_rd = sdev->attrs.max_sge_rd;
151303ae1cdSBernard Metzler 	attr->max_srq = sdev->attrs.max_srq;
152303ae1cdSBernard Metzler 	attr->max_srq_sge = sdev->attrs.max_srq_sge;
153303ae1cdSBernard Metzler 	attr->max_srq_wr = sdev->attrs.max_srq_wr;
154303ae1cdSBernard Metzler 	attr->page_size_cap = PAGE_SIZE;
155303ae1cdSBernard Metzler 	attr->vendor_id = SIW_VENDOR_ID;
156303ae1cdSBernard Metzler 	attr->vendor_part_id = sdev->vendor_part_id;
157303ae1cdSBernard Metzler 
158303ae1cdSBernard Metzler 	memcpy(&attr->sys_image_guid, sdev->netdev->dev_addr, 6);
159303ae1cdSBernard Metzler 
160303ae1cdSBernard Metzler 	return 0;
161303ae1cdSBernard Metzler }
162303ae1cdSBernard Metzler 
1631fb7f897SMark Bloch int siw_query_port(struct ib_device *base_dev, u32 port,
164303ae1cdSBernard Metzler 		   struct ib_port_attr *attr)
165303ae1cdSBernard Metzler {
166303ae1cdSBernard Metzler 	struct siw_device *sdev = to_siw_dev(base_dev);
16725baba21SKamal Heib 	int rv;
168303ae1cdSBernard Metzler 
169303ae1cdSBernard Metzler 	memset(attr, 0, sizeof(*attr));
170303ae1cdSBernard Metzler 
17125baba21SKamal Heib 	rv = ib_get_eth_speed(base_dev, port, &attr->active_speed,
17225baba21SKamal Heib 			 &attr->active_width);
173303ae1cdSBernard Metzler 	attr->gid_tbl_len = 1;
174303ae1cdSBernard Metzler 	attr->max_msg_sz = -1;
175303ae1cdSBernard Metzler 	attr->max_mtu = ib_mtu_int_to_enum(sdev->netdev->mtu);
176beb205ddSKamal Heib 	attr->active_mtu = ib_mtu_int_to_enum(sdev->netdev->mtu);
17772a7720fSKamal Heib 	attr->phys_state = sdev->state == IB_PORT_ACTIVE ?
17872a7720fSKamal Heib 		IB_PORT_PHYS_STATE_LINK_UP : IB_PORT_PHYS_STATE_DISABLED;
179303ae1cdSBernard Metzler 	attr->port_cap_flags = IB_PORT_CM_SUP | IB_PORT_DEVICE_MGMT_SUP;
180303ae1cdSBernard Metzler 	attr->state = sdev->state;
181303ae1cdSBernard Metzler 	/*
182303ae1cdSBernard Metzler 	 * All zero
183303ae1cdSBernard Metzler 	 *
184303ae1cdSBernard Metzler 	 * attr->lid = 0;
185303ae1cdSBernard Metzler 	 * attr->bad_pkey_cntr = 0;
186303ae1cdSBernard Metzler 	 * attr->qkey_viol_cntr = 0;
187303ae1cdSBernard Metzler 	 * attr->sm_lid = 0;
188303ae1cdSBernard Metzler 	 * attr->lmc = 0;
189303ae1cdSBernard Metzler 	 * attr->max_vl_num = 0;
190303ae1cdSBernard Metzler 	 * attr->sm_sl = 0;
191303ae1cdSBernard Metzler 	 * attr->subnet_timeout = 0;
192303ae1cdSBernard Metzler 	 * attr->init_type_repy = 0;
193303ae1cdSBernard Metzler 	 */
19425baba21SKamal Heib 	return rv;
195303ae1cdSBernard Metzler }
196303ae1cdSBernard Metzler 
1971fb7f897SMark Bloch int siw_get_port_immutable(struct ib_device *base_dev, u32 port,
198303ae1cdSBernard Metzler 			   struct ib_port_immutable *port_immutable)
199303ae1cdSBernard Metzler {
200303ae1cdSBernard Metzler 	struct ib_port_attr attr;
201303ae1cdSBernard Metzler 	int rv = siw_query_port(base_dev, port, &attr);
202303ae1cdSBernard Metzler 
203303ae1cdSBernard Metzler 	if (rv)
204303ae1cdSBernard Metzler 		return rv;
205303ae1cdSBernard Metzler 
206303ae1cdSBernard Metzler 	port_immutable->gid_tbl_len = attr.gid_tbl_len;
207303ae1cdSBernard Metzler 	port_immutable->core_cap_flags = RDMA_CORE_PORT_IWARP;
208303ae1cdSBernard Metzler 
209303ae1cdSBernard Metzler 	return 0;
210303ae1cdSBernard Metzler }
211303ae1cdSBernard Metzler 
2121fb7f897SMark Bloch int siw_query_gid(struct ib_device *base_dev, u32 port, int idx,
213303ae1cdSBernard Metzler 		  union ib_gid *gid)
214303ae1cdSBernard Metzler {
215303ae1cdSBernard Metzler 	struct siw_device *sdev = to_siw_dev(base_dev);
216303ae1cdSBernard Metzler 
217303ae1cdSBernard Metzler 	/* subnet_prefix == interface_id == 0; */
218303ae1cdSBernard Metzler 	memset(gid, 0, sizeof(*gid));
219303ae1cdSBernard Metzler 	memcpy(&gid->raw[0], sdev->netdev->dev_addr, 6);
220303ae1cdSBernard Metzler 
221303ae1cdSBernard Metzler 	return 0;
222303ae1cdSBernard Metzler }
223303ae1cdSBernard Metzler 
224303ae1cdSBernard Metzler int siw_alloc_pd(struct ib_pd *pd, struct ib_udata *udata)
225303ae1cdSBernard Metzler {
226303ae1cdSBernard Metzler 	struct siw_device *sdev = to_siw_dev(pd->device);
227303ae1cdSBernard Metzler 
228303ae1cdSBernard Metzler 	if (atomic_inc_return(&sdev->num_pd) > SIW_MAX_PD) {
229303ae1cdSBernard Metzler 		atomic_dec(&sdev->num_pd);
230303ae1cdSBernard Metzler 		return -ENOMEM;
231303ae1cdSBernard Metzler 	}
232303ae1cdSBernard Metzler 	siw_dbg_pd(pd, "now %d PD's(s)\n", atomic_read(&sdev->num_pd));
233303ae1cdSBernard Metzler 
234303ae1cdSBernard Metzler 	return 0;
235303ae1cdSBernard Metzler }
236303ae1cdSBernard Metzler 
23791a7c58fSLeon Romanovsky int siw_dealloc_pd(struct ib_pd *pd, struct ib_udata *udata)
238303ae1cdSBernard Metzler {
239303ae1cdSBernard Metzler 	struct siw_device *sdev = to_siw_dev(pd->device);
240303ae1cdSBernard Metzler 
241303ae1cdSBernard Metzler 	siw_dbg_pd(pd, "free PD\n");
242303ae1cdSBernard Metzler 	atomic_dec(&sdev->num_pd);
24391a7c58fSLeon Romanovsky 	return 0;
244303ae1cdSBernard Metzler }
245303ae1cdSBernard Metzler 
246303ae1cdSBernard Metzler void siw_qp_get_ref(struct ib_qp *base_qp)
247303ae1cdSBernard Metzler {
248303ae1cdSBernard Metzler 	siw_qp_get(to_siw_qp(base_qp));
249303ae1cdSBernard Metzler }
250303ae1cdSBernard Metzler 
251303ae1cdSBernard Metzler void siw_qp_put_ref(struct ib_qp *base_qp)
252303ae1cdSBernard Metzler {
253303ae1cdSBernard Metzler 	siw_qp_put(to_siw_qp(base_qp));
254303ae1cdSBernard Metzler }
255303ae1cdSBernard Metzler 
25611f1a755SMichal Kalderon static struct rdma_user_mmap_entry *
25711f1a755SMichal Kalderon siw_mmap_entry_insert(struct siw_ucontext *uctx,
25811f1a755SMichal Kalderon 		      void *address, size_t length,
25911f1a755SMichal Kalderon 		      u64 *offset)
26011f1a755SMichal Kalderon {
26111f1a755SMichal Kalderon 	struct siw_user_mmap_entry *entry = kzalloc(sizeof(*entry), GFP_KERNEL);
26211f1a755SMichal Kalderon 	int rv;
26311f1a755SMichal Kalderon 
26411f1a755SMichal Kalderon 	*offset = SIW_INVAL_UOBJ_KEY;
26511f1a755SMichal Kalderon 	if (!entry)
26611f1a755SMichal Kalderon 		return NULL;
26711f1a755SMichal Kalderon 
26811f1a755SMichal Kalderon 	entry->address = address;
26911f1a755SMichal Kalderon 
27011f1a755SMichal Kalderon 	rv = rdma_user_mmap_entry_insert(&uctx->base_ucontext,
27111f1a755SMichal Kalderon 					 &entry->rdma_entry,
27211f1a755SMichal Kalderon 					 length);
27311f1a755SMichal Kalderon 	if (rv) {
27411f1a755SMichal Kalderon 		kfree(entry);
27511f1a755SMichal Kalderon 		return NULL;
27611f1a755SMichal Kalderon 	}
27711f1a755SMichal Kalderon 
27811f1a755SMichal Kalderon 	*offset = rdma_user_mmap_get_offset(&entry->rdma_entry);
27911f1a755SMichal Kalderon 
28011f1a755SMichal Kalderon 	return &entry->rdma_entry;
28111f1a755SMichal Kalderon }
28211f1a755SMichal Kalderon 
283303ae1cdSBernard Metzler /*
284303ae1cdSBernard Metzler  * siw_create_qp()
285303ae1cdSBernard Metzler  *
286303ae1cdSBernard Metzler  * Create QP of requested size on given device.
287303ae1cdSBernard Metzler  *
288*514aee66SLeon Romanovsky  * @qp:		Queue pait
289303ae1cdSBernard Metzler  * @attrs:	Initial QP attributes.
290303ae1cdSBernard Metzler  * @udata:	used to provide QP ID, SQ and RQ size back to user.
291303ae1cdSBernard Metzler  */
292303ae1cdSBernard Metzler 
293*514aee66SLeon Romanovsky int siw_create_qp(struct ib_qp *ibqp, struct ib_qp_init_attr *attrs,
294303ae1cdSBernard Metzler 		  struct ib_udata *udata)
295303ae1cdSBernard Metzler {
296*514aee66SLeon Romanovsky 	struct ib_pd *pd = ibqp->pd;
297*514aee66SLeon Romanovsky 	struct siw_qp *qp = to_siw_qp(ibqp);
298303ae1cdSBernard Metzler 	struct ib_device *base_dev = pd->device;
299303ae1cdSBernard Metzler 	struct siw_device *sdev = to_siw_dev(base_dev);
300303ae1cdSBernard Metzler 	struct siw_ucontext *uctx =
301303ae1cdSBernard Metzler 		rdma_udata_to_drv_context(udata, struct siw_ucontext,
302303ae1cdSBernard Metzler 					  base_ucontext);
303303ae1cdSBernard Metzler 	unsigned long flags;
304303ae1cdSBernard Metzler 	int num_sqe, num_rqe, rv = 0;
30511f1a755SMichal Kalderon 	size_t length;
306303ae1cdSBernard Metzler 
307303ae1cdSBernard Metzler 	siw_dbg(base_dev, "create new QP\n");
308303ae1cdSBernard Metzler 
3091f11a761SJason Gunthorpe 	if (attrs->create_flags)
310*514aee66SLeon Romanovsky 		return -EOPNOTSUPP;
3111f11a761SJason Gunthorpe 
312303ae1cdSBernard Metzler 	if (atomic_inc_return(&sdev->num_qp) > SIW_MAX_QP) {
313303ae1cdSBernard Metzler 		siw_dbg(base_dev, "too many QP's\n");
314*514aee66SLeon Romanovsky 		return -ENOMEM;
315303ae1cdSBernard Metzler 	}
316303ae1cdSBernard Metzler 	if (attrs->qp_type != IB_QPT_RC) {
317303ae1cdSBernard Metzler 		siw_dbg(base_dev, "only RC QP's supported\n");
318bb8865f4SKamal Heib 		rv = -EOPNOTSUPP;
319*514aee66SLeon Romanovsky 		goto err_atomic;
320303ae1cdSBernard Metzler 	}
321303ae1cdSBernard Metzler 	if ((attrs->cap.max_send_wr > SIW_MAX_QP_WR) ||
322303ae1cdSBernard Metzler 	    (attrs->cap.max_recv_wr > SIW_MAX_QP_WR) ||
323303ae1cdSBernard Metzler 	    (attrs->cap.max_send_sge > SIW_MAX_SGE) ||
324303ae1cdSBernard Metzler 	    (attrs->cap.max_recv_sge > SIW_MAX_SGE)) {
325303ae1cdSBernard Metzler 		siw_dbg(base_dev, "QP size error\n");
326303ae1cdSBernard Metzler 		rv = -EINVAL;
327*514aee66SLeon Romanovsky 		goto err_atomic;
328303ae1cdSBernard Metzler 	}
329303ae1cdSBernard Metzler 	if (attrs->cap.max_inline_data > SIW_MAX_INLINE) {
330303ae1cdSBernard Metzler 		siw_dbg(base_dev, "max inline send: %d > %d\n",
331303ae1cdSBernard Metzler 			attrs->cap.max_inline_data, (int)SIW_MAX_INLINE);
332303ae1cdSBernard Metzler 		rv = -EINVAL;
333*514aee66SLeon Romanovsky 		goto err_atomic;
334303ae1cdSBernard Metzler 	}
335303ae1cdSBernard Metzler 	/*
336303ae1cdSBernard Metzler 	 * NOTE: we allow for zero element SQ and RQ WQE's SGL's
337303ae1cdSBernard Metzler 	 * but not for a QP unable to hold any WQE (SQ + RQ)
338303ae1cdSBernard Metzler 	 */
339303ae1cdSBernard Metzler 	if (attrs->cap.max_send_wr + attrs->cap.max_recv_wr == 0) {
340303ae1cdSBernard Metzler 		siw_dbg(base_dev, "QP must have send or receive queue\n");
341303ae1cdSBernard Metzler 		rv = -EINVAL;
342*514aee66SLeon Romanovsky 		goto err_atomic;
343303ae1cdSBernard Metzler 	}
344303ae1cdSBernard Metzler 
345a568814aSLeon Romanovsky 	if (!attrs->send_cq || (!attrs->recv_cq && !attrs->srq)) {
346303ae1cdSBernard Metzler 		siw_dbg(base_dev, "send CQ or receive CQ invalid\n");
347303ae1cdSBernard Metzler 		rv = -EINVAL;
348*514aee66SLeon Romanovsky 		goto err_atomic;
349303ae1cdSBernard Metzler 	}
350*514aee66SLeon Romanovsky 
351303ae1cdSBernard Metzler 	init_rwsem(&qp->state_lock);
352303ae1cdSBernard Metzler 	spin_lock_init(&qp->sq_lock);
353303ae1cdSBernard Metzler 	spin_lock_init(&qp->rq_lock);
354303ae1cdSBernard Metzler 	spin_lock_init(&qp->orq_lock);
355303ae1cdSBernard Metzler 
356303ae1cdSBernard Metzler 	rv = siw_qp_add(sdev, qp);
357303ae1cdSBernard Metzler 	if (rv)
358*514aee66SLeon Romanovsky 		goto err_atomic;
359303ae1cdSBernard Metzler 
360661f3859SBernard Metzler 	num_sqe = attrs->cap.max_send_wr;
361661f3859SBernard Metzler 	num_rqe = attrs->cap.max_recv_wr;
362661f3859SBernard Metzler 
363303ae1cdSBernard Metzler 	/* All queue indices are derived from modulo operations
364303ae1cdSBernard Metzler 	 * on a free running 'get' (consumer) and 'put' (producer)
365303ae1cdSBernard Metzler 	 * unsigned counter. Having queue sizes at power of two
366303ae1cdSBernard Metzler 	 * avoids handling counter wrap around.
367303ae1cdSBernard Metzler 	 */
368661f3859SBernard Metzler 	if (num_sqe)
369661f3859SBernard Metzler 		num_sqe = roundup_pow_of_two(num_sqe);
370661f3859SBernard Metzler 	else {
371661f3859SBernard Metzler 		/* Zero sized SQ is not supported */
372661f3859SBernard Metzler 		rv = -EINVAL;
373a3d83276SLeon Romanovsky 		goto err_out_xa;
374661f3859SBernard Metzler 	}
375661f3859SBernard Metzler 	if (num_rqe)
376661f3859SBernard Metzler 		num_rqe = roundup_pow_of_two(num_rqe);
377303ae1cdSBernard Metzler 
37858fb0b56SBernard Metzler 	if (udata)
379303ae1cdSBernard Metzler 		qp->sendq = vmalloc_user(num_sqe * sizeof(struct siw_sqe));
38058fb0b56SBernard Metzler 	else
38158fb0b56SBernard Metzler 		qp->sendq = vzalloc(num_sqe * sizeof(struct siw_sqe));
382303ae1cdSBernard Metzler 
383303ae1cdSBernard Metzler 	if (qp->sendq == NULL) {
384303ae1cdSBernard Metzler 		rv = -ENOMEM;
385303ae1cdSBernard Metzler 		goto err_out_xa;
386303ae1cdSBernard Metzler 	}
387303ae1cdSBernard Metzler 	if (attrs->sq_sig_type != IB_SIGNAL_REQ_WR) {
388303ae1cdSBernard Metzler 		if (attrs->sq_sig_type == IB_SIGNAL_ALL_WR)
389303ae1cdSBernard Metzler 			qp->attrs.flags |= SIW_SIGNAL_ALL_WR;
390303ae1cdSBernard Metzler 		else {
391303ae1cdSBernard Metzler 			rv = -EINVAL;
392303ae1cdSBernard Metzler 			goto err_out_xa;
393303ae1cdSBernard Metzler 		}
394303ae1cdSBernard Metzler 	}
395303ae1cdSBernard Metzler 	qp->pd = pd;
396a568814aSLeon Romanovsky 	qp->scq = to_siw_cq(attrs->send_cq);
397a568814aSLeon Romanovsky 	qp->rcq = to_siw_cq(attrs->recv_cq);
398303ae1cdSBernard Metzler 
399303ae1cdSBernard Metzler 	if (attrs->srq) {
400303ae1cdSBernard Metzler 		/*
401303ae1cdSBernard Metzler 		 * SRQ support.
402303ae1cdSBernard Metzler 		 * Verbs 6.3.7: ignore RQ size, if SRQ present
403303ae1cdSBernard Metzler 		 * Verbs 6.3.5: do not check PD of SRQ against PD of QP
404303ae1cdSBernard Metzler 		 */
405303ae1cdSBernard Metzler 		qp->srq = to_siw_srq(attrs->srq);
406303ae1cdSBernard Metzler 		qp->attrs.rq_size = 0;
40758fb0b56SBernard Metzler 		siw_dbg(base_dev, "QP [%u]: SRQ attached\n",
40858fb0b56SBernard Metzler 			qp->base_qp.qp_num);
409303ae1cdSBernard Metzler 	} else if (num_rqe) {
41058fb0b56SBernard Metzler 		if (udata)
411303ae1cdSBernard Metzler 			qp->recvq =
412303ae1cdSBernard Metzler 				vmalloc_user(num_rqe * sizeof(struct siw_rqe));
41358fb0b56SBernard Metzler 		else
41458fb0b56SBernard Metzler 			qp->recvq = vzalloc(num_rqe * sizeof(struct siw_rqe));
415303ae1cdSBernard Metzler 
416303ae1cdSBernard Metzler 		if (qp->recvq == NULL) {
417303ae1cdSBernard Metzler 			rv = -ENOMEM;
418303ae1cdSBernard Metzler 			goto err_out_xa;
419303ae1cdSBernard Metzler 		}
420303ae1cdSBernard Metzler 		qp->attrs.rq_size = num_rqe;
421303ae1cdSBernard Metzler 	}
422303ae1cdSBernard Metzler 	qp->attrs.sq_size = num_sqe;
423303ae1cdSBernard Metzler 	qp->attrs.sq_max_sges = attrs->cap.max_send_sge;
424303ae1cdSBernard Metzler 	qp->attrs.rq_max_sges = attrs->cap.max_recv_sge;
425303ae1cdSBernard Metzler 
426303ae1cdSBernard Metzler 	/* Make those two tunables fixed for now. */
427303ae1cdSBernard Metzler 	qp->tx_ctx.gso_seg_limit = 1;
428303ae1cdSBernard Metzler 	qp->tx_ctx.zcopy_tx = zcopy_tx;
429303ae1cdSBernard Metzler 
430303ae1cdSBernard Metzler 	qp->attrs.state = SIW_QP_STATE_IDLE;
431303ae1cdSBernard Metzler 
432303ae1cdSBernard Metzler 	if (udata) {
433303ae1cdSBernard Metzler 		struct siw_uresp_create_qp uresp = {};
434303ae1cdSBernard Metzler 
435303ae1cdSBernard Metzler 		uresp.num_sqe = num_sqe;
436303ae1cdSBernard Metzler 		uresp.num_rqe = num_rqe;
437303ae1cdSBernard Metzler 		uresp.qp_id = qp_id(qp);
438303ae1cdSBernard Metzler 
439303ae1cdSBernard Metzler 		if (qp->sendq) {
44011f1a755SMichal Kalderon 			length = num_sqe * sizeof(struct siw_sqe);
44111f1a755SMichal Kalderon 			qp->sq_entry =
44211f1a755SMichal Kalderon 				siw_mmap_entry_insert(uctx, qp->sendq,
44311f1a755SMichal Kalderon 						      length, &uresp.sq_key);
44411f1a755SMichal Kalderon 			if (!qp->sq_entry) {
445303ae1cdSBernard Metzler 				rv = -ENOMEM;
446303ae1cdSBernard Metzler 				goto err_out_xa;
447303ae1cdSBernard Metzler 			}
44811f1a755SMichal Kalderon 		}
44911f1a755SMichal Kalderon 
45011f1a755SMichal Kalderon 		if (qp->recvq) {
45111f1a755SMichal Kalderon 			length = num_rqe * sizeof(struct siw_rqe);
45211f1a755SMichal Kalderon 			qp->rq_entry =
45311f1a755SMichal Kalderon 				siw_mmap_entry_insert(uctx, qp->recvq,
45411f1a755SMichal Kalderon 						      length, &uresp.rq_key);
45511f1a755SMichal Kalderon 			if (!qp->rq_entry) {
45611f1a755SMichal Kalderon 				uresp.sq_key = SIW_INVAL_UOBJ_KEY;
45711f1a755SMichal Kalderon 				rv = -ENOMEM;
45811f1a755SMichal Kalderon 				goto err_out_xa;
45911f1a755SMichal Kalderon 			}
46011f1a755SMichal Kalderon 		}
461303ae1cdSBernard Metzler 
462303ae1cdSBernard Metzler 		if (udata->outlen < sizeof(uresp)) {
463303ae1cdSBernard Metzler 			rv = -EINVAL;
464303ae1cdSBernard Metzler 			goto err_out_xa;
465303ae1cdSBernard Metzler 		}
466303ae1cdSBernard Metzler 		rv = ib_copy_to_udata(udata, &uresp, sizeof(uresp));
467303ae1cdSBernard Metzler 		if (rv)
468303ae1cdSBernard Metzler 			goto err_out_xa;
469303ae1cdSBernard Metzler 	}
470303ae1cdSBernard Metzler 	qp->tx_cpu = siw_get_tx_cpu(sdev);
471303ae1cdSBernard Metzler 	if (qp->tx_cpu < 0) {
472303ae1cdSBernard Metzler 		rv = -EINVAL;
473303ae1cdSBernard Metzler 		goto err_out_xa;
474303ae1cdSBernard Metzler 	}
475303ae1cdSBernard Metzler 	INIT_LIST_HEAD(&qp->devq);
476303ae1cdSBernard Metzler 	spin_lock_irqsave(&sdev->lock, flags);
477303ae1cdSBernard Metzler 	list_add_tail(&qp->devq, &sdev->qp_list);
478303ae1cdSBernard Metzler 	spin_unlock_irqrestore(&sdev->lock, flags);
479303ae1cdSBernard Metzler 
480*514aee66SLeon Romanovsky 	return 0;
481303ae1cdSBernard Metzler 
482303ae1cdSBernard Metzler err_out_xa:
483303ae1cdSBernard Metzler 	xa_erase(&sdev->qp_xa, qp_id(qp));
48411f1a755SMichal Kalderon 	if (uctx) {
48511f1a755SMichal Kalderon 		rdma_user_mmap_entry_remove(qp->sq_entry);
48611f1a755SMichal Kalderon 		rdma_user_mmap_entry_remove(qp->rq_entry);
48711f1a755SMichal Kalderon 	}
488303ae1cdSBernard Metzler 	vfree(qp->sendq);
489303ae1cdSBernard Metzler 	vfree(qp->recvq);
490303ae1cdSBernard Metzler 
491*514aee66SLeon Romanovsky err_atomic:
492*514aee66SLeon Romanovsky 	atomic_dec(&sdev->num_qp);
493*514aee66SLeon Romanovsky 	return rv;
494303ae1cdSBernard Metzler }
495303ae1cdSBernard Metzler 
496303ae1cdSBernard Metzler /*
497303ae1cdSBernard Metzler  * Minimum siw_query_qp() verb interface.
498303ae1cdSBernard Metzler  *
499303ae1cdSBernard Metzler  * @qp_attr_mask is not used but all available information is provided
500303ae1cdSBernard Metzler  */
501303ae1cdSBernard Metzler int siw_query_qp(struct ib_qp *base_qp, struct ib_qp_attr *qp_attr,
502303ae1cdSBernard Metzler 		 int qp_attr_mask, struct ib_qp_init_attr *qp_init_attr)
503303ae1cdSBernard Metzler {
504303ae1cdSBernard Metzler 	struct siw_qp *qp;
505303ae1cdSBernard Metzler 	struct siw_device *sdev;
506303ae1cdSBernard Metzler 
507303ae1cdSBernard Metzler 	if (base_qp && qp_attr && qp_init_attr) {
508303ae1cdSBernard Metzler 		qp = to_siw_qp(base_qp);
509303ae1cdSBernard Metzler 		sdev = to_siw_dev(base_qp->device);
510303ae1cdSBernard Metzler 	} else {
511303ae1cdSBernard Metzler 		return -EINVAL;
512303ae1cdSBernard Metzler 	}
513303ae1cdSBernard Metzler 	qp_attr->cap.max_inline_data = SIW_MAX_INLINE;
514303ae1cdSBernard Metzler 	qp_attr->cap.max_send_wr = qp->attrs.sq_size;
515303ae1cdSBernard Metzler 	qp_attr->cap.max_send_sge = qp->attrs.sq_max_sges;
516303ae1cdSBernard Metzler 	qp_attr->cap.max_recv_wr = qp->attrs.rq_size;
517303ae1cdSBernard Metzler 	qp_attr->cap.max_recv_sge = qp->attrs.rq_max_sges;
518303ae1cdSBernard Metzler 	qp_attr->path_mtu = ib_mtu_int_to_enum(sdev->netdev->mtu);
519303ae1cdSBernard Metzler 	qp_attr->max_rd_atomic = qp->attrs.irq_size;
520303ae1cdSBernard Metzler 	qp_attr->max_dest_rd_atomic = qp->attrs.orq_size;
521303ae1cdSBernard Metzler 
522303ae1cdSBernard Metzler 	qp_attr->qp_access_flags = IB_ACCESS_LOCAL_WRITE |
523303ae1cdSBernard Metzler 				   IB_ACCESS_REMOTE_WRITE |
524303ae1cdSBernard Metzler 				   IB_ACCESS_REMOTE_READ;
525303ae1cdSBernard Metzler 
526303ae1cdSBernard Metzler 	qp_init_attr->qp_type = base_qp->qp_type;
527303ae1cdSBernard Metzler 	qp_init_attr->send_cq = base_qp->send_cq;
528303ae1cdSBernard Metzler 	qp_init_attr->recv_cq = base_qp->recv_cq;
529303ae1cdSBernard Metzler 	qp_init_attr->srq = base_qp->srq;
530303ae1cdSBernard Metzler 
531303ae1cdSBernard Metzler 	qp_init_attr->cap = qp_attr->cap;
532303ae1cdSBernard Metzler 
533303ae1cdSBernard Metzler 	return 0;
534303ae1cdSBernard Metzler }
535303ae1cdSBernard Metzler 
536303ae1cdSBernard Metzler int siw_verbs_modify_qp(struct ib_qp *base_qp, struct ib_qp_attr *attr,
537303ae1cdSBernard Metzler 			int attr_mask, struct ib_udata *udata)
538303ae1cdSBernard Metzler {
539303ae1cdSBernard Metzler 	struct siw_qp_attrs new_attrs;
540303ae1cdSBernard Metzler 	enum siw_qp_attr_mask siw_attr_mask = 0;
541303ae1cdSBernard Metzler 	struct siw_qp *qp = to_siw_qp(base_qp);
542303ae1cdSBernard Metzler 	int rv = 0;
543303ae1cdSBernard Metzler 
544303ae1cdSBernard Metzler 	if (!attr_mask)
545303ae1cdSBernard Metzler 		return 0;
546303ae1cdSBernard Metzler 
54726e990baSJason Gunthorpe 	if (attr_mask & ~IB_QP_ATTR_STANDARD_BITS)
54826e990baSJason Gunthorpe 		return -EOPNOTSUPP;
54926e990baSJason Gunthorpe 
550303ae1cdSBernard Metzler 	memset(&new_attrs, 0, sizeof(new_attrs));
551303ae1cdSBernard Metzler 
552303ae1cdSBernard Metzler 	if (attr_mask & IB_QP_ACCESS_FLAGS) {
553303ae1cdSBernard Metzler 		siw_attr_mask = SIW_QP_ATTR_ACCESS_FLAGS;
554303ae1cdSBernard Metzler 
555303ae1cdSBernard Metzler 		if (attr->qp_access_flags & IB_ACCESS_REMOTE_READ)
556303ae1cdSBernard Metzler 			new_attrs.flags |= SIW_RDMA_READ_ENABLED;
557303ae1cdSBernard Metzler 		if (attr->qp_access_flags & IB_ACCESS_REMOTE_WRITE)
558303ae1cdSBernard Metzler 			new_attrs.flags |= SIW_RDMA_WRITE_ENABLED;
559303ae1cdSBernard Metzler 		if (attr->qp_access_flags & IB_ACCESS_MW_BIND)
560303ae1cdSBernard Metzler 			new_attrs.flags |= SIW_RDMA_BIND_ENABLED;
561303ae1cdSBernard Metzler 	}
562303ae1cdSBernard Metzler 	if (attr_mask & IB_QP_STATE) {
563303ae1cdSBernard Metzler 		siw_dbg_qp(qp, "desired IB QP state: %s\n",
564303ae1cdSBernard Metzler 			   ib_qp_state_to_string[attr->qp_state]);
565303ae1cdSBernard Metzler 
566303ae1cdSBernard Metzler 		new_attrs.state = ib_qp_state_to_siw_qp_state[attr->qp_state];
567303ae1cdSBernard Metzler 
568303ae1cdSBernard Metzler 		if (new_attrs.state > SIW_QP_STATE_RTS)
569303ae1cdSBernard Metzler 			qp->tx_ctx.tx_suspend = 1;
570303ae1cdSBernard Metzler 
571303ae1cdSBernard Metzler 		siw_attr_mask |= SIW_QP_ATTR_STATE;
572303ae1cdSBernard Metzler 	}
573303ae1cdSBernard Metzler 	if (!siw_attr_mask)
574303ae1cdSBernard Metzler 		goto out;
575303ae1cdSBernard Metzler 
576303ae1cdSBernard Metzler 	down_write(&qp->state_lock);
577303ae1cdSBernard Metzler 
578303ae1cdSBernard Metzler 	rv = siw_qp_modify(qp, &new_attrs, siw_attr_mask);
579303ae1cdSBernard Metzler 
580303ae1cdSBernard Metzler 	up_write(&qp->state_lock);
581303ae1cdSBernard Metzler out:
582303ae1cdSBernard Metzler 	return rv;
583303ae1cdSBernard Metzler }
584303ae1cdSBernard Metzler 
585303ae1cdSBernard Metzler int siw_destroy_qp(struct ib_qp *base_qp, struct ib_udata *udata)
586303ae1cdSBernard Metzler {
587303ae1cdSBernard Metzler 	struct siw_qp *qp = to_siw_qp(base_qp);
588303ae1cdSBernard Metzler 	struct siw_ucontext *uctx =
589303ae1cdSBernard Metzler 		rdma_udata_to_drv_context(udata, struct siw_ucontext,
590303ae1cdSBernard Metzler 					  base_ucontext);
591303ae1cdSBernard Metzler 	struct siw_qp_attrs qp_attrs;
592303ae1cdSBernard Metzler 
593c536277eSBernard Metzler 	siw_dbg_qp(qp, "state %d\n", qp->attrs.state);
594303ae1cdSBernard Metzler 
595303ae1cdSBernard Metzler 	/*
596303ae1cdSBernard Metzler 	 * Mark QP as in process of destruction to prevent from
597303ae1cdSBernard Metzler 	 * any async callbacks to RDMA core
598303ae1cdSBernard Metzler 	 */
599303ae1cdSBernard Metzler 	qp->attrs.flags |= SIW_QP_IN_DESTROY;
600303ae1cdSBernard Metzler 	qp->rx_stream.rx_suspend = 1;
601303ae1cdSBernard Metzler 
60211f1a755SMichal Kalderon 	if (uctx) {
60311f1a755SMichal Kalderon 		rdma_user_mmap_entry_remove(qp->sq_entry);
60411f1a755SMichal Kalderon 		rdma_user_mmap_entry_remove(qp->rq_entry);
60511f1a755SMichal Kalderon 	}
606303ae1cdSBernard Metzler 
607303ae1cdSBernard Metzler 	down_write(&qp->state_lock);
608303ae1cdSBernard Metzler 
609303ae1cdSBernard Metzler 	qp_attrs.state = SIW_QP_STATE_ERROR;
610303ae1cdSBernard Metzler 	siw_qp_modify(qp, &qp_attrs, SIW_QP_ATTR_STATE);
611303ae1cdSBernard Metzler 
612303ae1cdSBernard Metzler 	if (qp->cep) {
613303ae1cdSBernard Metzler 		siw_cep_put(qp->cep);
614303ae1cdSBernard Metzler 		qp->cep = NULL;
615303ae1cdSBernard Metzler 	}
616303ae1cdSBernard Metzler 	up_write(&qp->state_lock);
617303ae1cdSBernard Metzler 
618303ae1cdSBernard Metzler 	kfree(qp->tx_ctx.mpa_crc_hd);
619303ae1cdSBernard Metzler 	kfree(qp->rx_stream.mpa_crc_hd);
620303ae1cdSBernard Metzler 
621303ae1cdSBernard Metzler 	qp->scq = qp->rcq = NULL;
622303ae1cdSBernard Metzler 
623303ae1cdSBernard Metzler 	siw_qp_put(qp);
624303ae1cdSBernard Metzler 
625303ae1cdSBernard Metzler 	return 0;
626303ae1cdSBernard Metzler }
627303ae1cdSBernard Metzler 
628303ae1cdSBernard Metzler /*
629303ae1cdSBernard Metzler  * siw_copy_inline_sgl()
630303ae1cdSBernard Metzler  *
631303ae1cdSBernard Metzler  * Prepare sgl of inlined data for sending. For userland callers
632303ae1cdSBernard Metzler  * function checks if given buffer addresses and len's are within
633303ae1cdSBernard Metzler  * process context bounds.
634303ae1cdSBernard Metzler  * Data from all provided sge's are copied together into the wqe,
635303ae1cdSBernard Metzler  * referenced by a single sge.
636303ae1cdSBernard Metzler  */
637303ae1cdSBernard Metzler static int siw_copy_inline_sgl(const struct ib_send_wr *core_wr,
638303ae1cdSBernard Metzler 			       struct siw_sqe *sqe)
639303ae1cdSBernard Metzler {
640303ae1cdSBernard Metzler 	struct ib_sge *core_sge = core_wr->sg_list;
641303ae1cdSBernard Metzler 	void *kbuf = &sqe->sge[1];
642303ae1cdSBernard Metzler 	int num_sge = core_wr->num_sge, bytes = 0;
643303ae1cdSBernard Metzler 
644c536277eSBernard Metzler 	sqe->sge[0].laddr = (uintptr_t)kbuf;
645303ae1cdSBernard Metzler 	sqe->sge[0].lkey = 0;
646303ae1cdSBernard Metzler 
647303ae1cdSBernard Metzler 	while (num_sge--) {
648303ae1cdSBernard Metzler 		if (!core_sge->length) {
649303ae1cdSBernard Metzler 			core_sge++;
650303ae1cdSBernard Metzler 			continue;
651303ae1cdSBernard Metzler 		}
652303ae1cdSBernard Metzler 		bytes += core_sge->length;
653303ae1cdSBernard Metzler 		if (bytes > SIW_MAX_INLINE) {
654303ae1cdSBernard Metzler 			bytes = -EINVAL;
655303ae1cdSBernard Metzler 			break;
656303ae1cdSBernard Metzler 		}
657303ae1cdSBernard Metzler 		memcpy(kbuf, (void *)(uintptr_t)core_sge->addr,
658303ae1cdSBernard Metzler 		       core_sge->length);
659303ae1cdSBernard Metzler 
660303ae1cdSBernard Metzler 		kbuf += core_sge->length;
661303ae1cdSBernard Metzler 		core_sge++;
662303ae1cdSBernard Metzler 	}
663303ae1cdSBernard Metzler 	sqe->sge[0].length = bytes > 0 ? bytes : 0;
664303ae1cdSBernard Metzler 	sqe->num_sge = bytes > 0 ? 1 : 0;
665303ae1cdSBernard Metzler 
666303ae1cdSBernard Metzler 	return bytes;
667303ae1cdSBernard Metzler }
668303ae1cdSBernard Metzler 
669cf049bb3SBernard Metzler /* Complete SQ WR's without processing */
670cf049bb3SBernard Metzler static int siw_sq_flush_wr(struct siw_qp *qp, const struct ib_send_wr *wr,
671cf049bb3SBernard Metzler 			   const struct ib_send_wr **bad_wr)
672cf049bb3SBernard Metzler {
673cf049bb3SBernard Metzler 	struct siw_sqe sqe = {};
674cf049bb3SBernard Metzler 	int rv = 0;
675cf049bb3SBernard Metzler 
676cf049bb3SBernard Metzler 	while (wr) {
677cf049bb3SBernard Metzler 		sqe.id = wr->wr_id;
678cf049bb3SBernard Metzler 		sqe.opcode = wr->opcode;
679cf049bb3SBernard Metzler 		rv = siw_sqe_complete(qp, &sqe, 0, SIW_WC_WR_FLUSH_ERR);
680cf049bb3SBernard Metzler 		if (rv) {
681cf049bb3SBernard Metzler 			if (bad_wr)
682cf049bb3SBernard Metzler 				*bad_wr = wr;
683cf049bb3SBernard Metzler 			break;
684cf049bb3SBernard Metzler 		}
685cf049bb3SBernard Metzler 		wr = wr->next;
686cf049bb3SBernard Metzler 	}
687cf049bb3SBernard Metzler 	return rv;
688cf049bb3SBernard Metzler }
689cf049bb3SBernard Metzler 
690cf049bb3SBernard Metzler /* Complete RQ WR's without processing */
691cf049bb3SBernard Metzler static int siw_rq_flush_wr(struct siw_qp *qp, const struct ib_recv_wr *wr,
692cf049bb3SBernard Metzler 			   const struct ib_recv_wr **bad_wr)
693cf049bb3SBernard Metzler {
694cf049bb3SBernard Metzler 	struct siw_rqe rqe = {};
695cf049bb3SBernard Metzler 	int rv = 0;
696cf049bb3SBernard Metzler 
697cf049bb3SBernard Metzler 	while (wr) {
698cf049bb3SBernard Metzler 		rqe.id = wr->wr_id;
699cf049bb3SBernard Metzler 		rv = siw_rqe_complete(qp, &rqe, 0, 0, SIW_WC_WR_FLUSH_ERR);
700cf049bb3SBernard Metzler 		if (rv) {
701cf049bb3SBernard Metzler 			if (bad_wr)
702cf049bb3SBernard Metzler 				*bad_wr = wr;
703cf049bb3SBernard Metzler 			break;
704cf049bb3SBernard Metzler 		}
705cf049bb3SBernard Metzler 		wr = wr->next;
706cf049bb3SBernard Metzler 	}
707cf049bb3SBernard Metzler 	return rv;
708cf049bb3SBernard Metzler }
709cf049bb3SBernard Metzler 
710303ae1cdSBernard Metzler /*
711303ae1cdSBernard Metzler  * siw_post_send()
712303ae1cdSBernard Metzler  *
713303ae1cdSBernard Metzler  * Post a list of S-WR's to a SQ.
714303ae1cdSBernard Metzler  *
715303ae1cdSBernard Metzler  * @base_qp:	Base QP contained in siw QP
716303ae1cdSBernard Metzler  * @wr:		Null terminated list of user WR's
717303ae1cdSBernard Metzler  * @bad_wr:	Points to failing WR in case of synchronous failure.
718303ae1cdSBernard Metzler  */
719303ae1cdSBernard Metzler int siw_post_send(struct ib_qp *base_qp, const struct ib_send_wr *wr,
720303ae1cdSBernard Metzler 		  const struct ib_send_wr **bad_wr)
721303ae1cdSBernard Metzler {
722303ae1cdSBernard Metzler 	struct siw_qp *qp = to_siw_qp(base_qp);
723303ae1cdSBernard Metzler 	struct siw_wqe *wqe = tx_wqe(qp);
724303ae1cdSBernard Metzler 
725303ae1cdSBernard Metzler 	unsigned long flags;
726303ae1cdSBernard Metzler 	int rv = 0;
727303ae1cdSBernard Metzler 
72858fb0b56SBernard Metzler 	if (wr && !rdma_is_kernel_res(&qp->base_qp.res)) {
729cf049bb3SBernard Metzler 		siw_dbg_qp(qp, "wr must be empty for user mapped sq\n");
730cf049bb3SBernard Metzler 		*bad_wr = wr;
731cf049bb3SBernard Metzler 		return -EINVAL;
732cf049bb3SBernard Metzler 	}
733cf049bb3SBernard Metzler 
734303ae1cdSBernard Metzler 	/*
735303ae1cdSBernard Metzler 	 * Try to acquire QP state lock. Must be non-blocking
736303ae1cdSBernard Metzler 	 * to accommodate kernel clients needs.
737303ae1cdSBernard Metzler 	 */
738303ae1cdSBernard Metzler 	if (!down_read_trylock(&qp->state_lock)) {
739cf049bb3SBernard Metzler 		if (qp->attrs.state == SIW_QP_STATE_ERROR) {
740cf049bb3SBernard Metzler 			/*
741cf049bb3SBernard Metzler 			 * ERROR state is final, so we can be sure
742cf049bb3SBernard Metzler 			 * this state will not change as long as the QP
743cf049bb3SBernard Metzler 			 * exists.
744cf049bb3SBernard Metzler 			 *
745cf049bb3SBernard Metzler 			 * This handles an ib_drain_sq() call with
746cf049bb3SBernard Metzler 			 * a concurrent request to set the QP state
747cf049bb3SBernard Metzler 			 * to ERROR.
748cf049bb3SBernard Metzler 			 */
749cf049bb3SBernard Metzler 			rv = siw_sq_flush_wr(qp, wr, bad_wr);
750cf049bb3SBernard Metzler 		} else {
751cf049bb3SBernard Metzler 			siw_dbg_qp(qp, "QP locked, state %d\n",
752cf049bb3SBernard Metzler 				   qp->attrs.state);
753303ae1cdSBernard Metzler 			*bad_wr = wr;
754cf049bb3SBernard Metzler 			rv = -ENOTCONN;
755cf049bb3SBernard Metzler 		}
756cf049bb3SBernard Metzler 		return rv;
757303ae1cdSBernard Metzler 	}
758303ae1cdSBernard Metzler 	if (unlikely(qp->attrs.state != SIW_QP_STATE_RTS)) {
759cf049bb3SBernard Metzler 		if (qp->attrs.state == SIW_QP_STATE_ERROR) {
760cf049bb3SBernard Metzler 			/*
761cf049bb3SBernard Metzler 			 * Immediately flush this WR to CQ, if QP
762cf049bb3SBernard Metzler 			 * is in ERROR state. SQ is guaranteed to
763cf049bb3SBernard Metzler 			 * be empty, so WR complets in-order.
764cf049bb3SBernard Metzler 			 *
765cf049bb3SBernard Metzler 			 * Typically triggered by ib_drain_sq().
766cf049bb3SBernard Metzler 			 */
767cf049bb3SBernard Metzler 			rv = siw_sq_flush_wr(qp, wr, bad_wr);
768cf049bb3SBernard Metzler 		} else {
769cf049bb3SBernard Metzler 			siw_dbg_qp(qp, "QP out of state %d\n",
770cf049bb3SBernard Metzler 				   qp->attrs.state);
771303ae1cdSBernard Metzler 			*bad_wr = wr;
772cf049bb3SBernard Metzler 			rv = -ENOTCONN;
773303ae1cdSBernard Metzler 		}
774303ae1cdSBernard Metzler 		up_read(&qp->state_lock);
775cf049bb3SBernard Metzler 		return rv;
776303ae1cdSBernard Metzler 	}
777303ae1cdSBernard Metzler 	spin_lock_irqsave(&qp->sq_lock, flags);
778303ae1cdSBernard Metzler 
779303ae1cdSBernard Metzler 	while (wr) {
780303ae1cdSBernard Metzler 		u32 idx = qp->sq_put % qp->attrs.sq_size;
781303ae1cdSBernard Metzler 		struct siw_sqe *sqe = &qp->sendq[idx];
782303ae1cdSBernard Metzler 
783303ae1cdSBernard Metzler 		if (sqe->flags) {
784303ae1cdSBernard Metzler 			siw_dbg_qp(qp, "sq full\n");
785303ae1cdSBernard Metzler 			rv = -ENOMEM;
786303ae1cdSBernard Metzler 			break;
787303ae1cdSBernard Metzler 		}
788303ae1cdSBernard Metzler 		if (wr->num_sge > qp->attrs.sq_max_sges) {
789303ae1cdSBernard Metzler 			siw_dbg_qp(qp, "too many sge's: %d\n", wr->num_sge);
790303ae1cdSBernard Metzler 			rv = -EINVAL;
791303ae1cdSBernard Metzler 			break;
792303ae1cdSBernard Metzler 		}
793303ae1cdSBernard Metzler 		sqe->id = wr->wr_id;
794303ae1cdSBernard Metzler 
795303ae1cdSBernard Metzler 		if ((wr->send_flags & IB_SEND_SIGNALED) ||
796303ae1cdSBernard Metzler 		    (qp->attrs.flags & SIW_SIGNAL_ALL_WR))
797303ae1cdSBernard Metzler 			sqe->flags |= SIW_WQE_SIGNALLED;
798303ae1cdSBernard Metzler 
799303ae1cdSBernard Metzler 		if (wr->send_flags & IB_SEND_FENCE)
800303ae1cdSBernard Metzler 			sqe->flags |= SIW_WQE_READ_FENCE;
801303ae1cdSBernard Metzler 
802303ae1cdSBernard Metzler 		switch (wr->opcode) {
803303ae1cdSBernard Metzler 		case IB_WR_SEND:
804303ae1cdSBernard Metzler 		case IB_WR_SEND_WITH_INV:
805303ae1cdSBernard Metzler 			if (wr->send_flags & IB_SEND_SOLICITED)
806303ae1cdSBernard Metzler 				sqe->flags |= SIW_WQE_SOLICITED;
807303ae1cdSBernard Metzler 
808303ae1cdSBernard Metzler 			if (!(wr->send_flags & IB_SEND_INLINE)) {
809303ae1cdSBernard Metzler 				siw_copy_sgl(wr->sg_list, sqe->sge,
810303ae1cdSBernard Metzler 					     wr->num_sge);
811303ae1cdSBernard Metzler 				sqe->num_sge = wr->num_sge;
812303ae1cdSBernard Metzler 			} else {
813303ae1cdSBernard Metzler 				rv = siw_copy_inline_sgl(wr, sqe);
814303ae1cdSBernard Metzler 				if (rv <= 0) {
815303ae1cdSBernard Metzler 					rv = -EINVAL;
816303ae1cdSBernard Metzler 					break;
817303ae1cdSBernard Metzler 				}
818303ae1cdSBernard Metzler 				sqe->flags |= SIW_WQE_INLINE;
819303ae1cdSBernard Metzler 				sqe->num_sge = 1;
820303ae1cdSBernard Metzler 			}
821303ae1cdSBernard Metzler 			if (wr->opcode == IB_WR_SEND)
822303ae1cdSBernard Metzler 				sqe->opcode = SIW_OP_SEND;
823303ae1cdSBernard Metzler 			else {
824303ae1cdSBernard Metzler 				sqe->opcode = SIW_OP_SEND_REMOTE_INV;
825303ae1cdSBernard Metzler 				sqe->rkey = wr->ex.invalidate_rkey;
826303ae1cdSBernard Metzler 			}
827303ae1cdSBernard Metzler 			break;
828303ae1cdSBernard Metzler 
829303ae1cdSBernard Metzler 		case IB_WR_RDMA_READ_WITH_INV:
830303ae1cdSBernard Metzler 		case IB_WR_RDMA_READ:
831303ae1cdSBernard Metzler 			/*
832303ae1cdSBernard Metzler 			 * iWarp restricts RREAD sink to SGL containing
833303ae1cdSBernard Metzler 			 * 1 SGE only. we could relax to SGL with multiple
834303ae1cdSBernard Metzler 			 * elements referring the SAME ltag or even sending
835303ae1cdSBernard Metzler 			 * a private per-rreq tag referring to a checked
836303ae1cdSBernard Metzler 			 * local sgl with MULTIPLE ltag's.
837303ae1cdSBernard Metzler 			 */
838303ae1cdSBernard Metzler 			if (unlikely(wr->num_sge != 1)) {
839303ae1cdSBernard Metzler 				rv = -EINVAL;
840303ae1cdSBernard Metzler 				break;
841303ae1cdSBernard Metzler 			}
842303ae1cdSBernard Metzler 			siw_copy_sgl(wr->sg_list, &sqe->sge[0], 1);
843303ae1cdSBernard Metzler 			/*
844303ae1cdSBernard Metzler 			 * NOTE: zero length RREAD is allowed!
845303ae1cdSBernard Metzler 			 */
846303ae1cdSBernard Metzler 			sqe->raddr = rdma_wr(wr)->remote_addr;
847303ae1cdSBernard Metzler 			sqe->rkey = rdma_wr(wr)->rkey;
848303ae1cdSBernard Metzler 			sqe->num_sge = 1;
849303ae1cdSBernard Metzler 
850303ae1cdSBernard Metzler 			if (wr->opcode == IB_WR_RDMA_READ)
851303ae1cdSBernard Metzler 				sqe->opcode = SIW_OP_READ;
852303ae1cdSBernard Metzler 			else
853303ae1cdSBernard Metzler 				sqe->opcode = SIW_OP_READ_LOCAL_INV;
854303ae1cdSBernard Metzler 			break;
855303ae1cdSBernard Metzler 
856303ae1cdSBernard Metzler 		case IB_WR_RDMA_WRITE:
857303ae1cdSBernard Metzler 			if (!(wr->send_flags & IB_SEND_INLINE)) {
858303ae1cdSBernard Metzler 				siw_copy_sgl(wr->sg_list, &sqe->sge[0],
859303ae1cdSBernard Metzler 					     wr->num_sge);
860303ae1cdSBernard Metzler 				sqe->num_sge = wr->num_sge;
861303ae1cdSBernard Metzler 			} else {
862303ae1cdSBernard Metzler 				rv = siw_copy_inline_sgl(wr, sqe);
863303ae1cdSBernard Metzler 				if (unlikely(rv < 0)) {
864303ae1cdSBernard Metzler 					rv = -EINVAL;
865303ae1cdSBernard Metzler 					break;
866303ae1cdSBernard Metzler 				}
867303ae1cdSBernard Metzler 				sqe->flags |= SIW_WQE_INLINE;
868303ae1cdSBernard Metzler 				sqe->num_sge = 1;
869303ae1cdSBernard Metzler 			}
870303ae1cdSBernard Metzler 			sqe->raddr = rdma_wr(wr)->remote_addr;
871303ae1cdSBernard Metzler 			sqe->rkey = rdma_wr(wr)->rkey;
872303ae1cdSBernard Metzler 			sqe->opcode = SIW_OP_WRITE;
873303ae1cdSBernard Metzler 			break;
874303ae1cdSBernard Metzler 
875303ae1cdSBernard Metzler 		case IB_WR_REG_MR:
876c536277eSBernard Metzler 			sqe->base_mr = (uintptr_t)reg_wr(wr)->mr;
877303ae1cdSBernard Metzler 			sqe->rkey = reg_wr(wr)->key;
878303ae1cdSBernard Metzler 			sqe->access = reg_wr(wr)->access & IWARP_ACCESS_MASK;
879303ae1cdSBernard Metzler 			sqe->opcode = SIW_OP_REG_MR;
880303ae1cdSBernard Metzler 			break;
881303ae1cdSBernard Metzler 
882303ae1cdSBernard Metzler 		case IB_WR_LOCAL_INV:
883303ae1cdSBernard Metzler 			sqe->rkey = wr->ex.invalidate_rkey;
884303ae1cdSBernard Metzler 			sqe->opcode = SIW_OP_INVAL_STAG;
885303ae1cdSBernard Metzler 			break;
886303ae1cdSBernard Metzler 
887303ae1cdSBernard Metzler 		default:
888303ae1cdSBernard Metzler 			siw_dbg_qp(qp, "ib wr type %d unsupported\n",
889303ae1cdSBernard Metzler 				   wr->opcode);
890303ae1cdSBernard Metzler 			rv = -EINVAL;
891303ae1cdSBernard Metzler 			break;
892303ae1cdSBernard Metzler 		}
893c536277eSBernard Metzler 		siw_dbg_qp(qp, "opcode %d, flags 0x%x, wr_id 0x%pK\n",
894c536277eSBernard Metzler 			   sqe->opcode, sqe->flags,
895c536277eSBernard Metzler 			   (void *)(uintptr_t)sqe->id);
896303ae1cdSBernard Metzler 
897303ae1cdSBernard Metzler 		if (unlikely(rv < 0))
898303ae1cdSBernard Metzler 			break;
899303ae1cdSBernard Metzler 
900303ae1cdSBernard Metzler 		/* make SQE only valid after completely written */
901303ae1cdSBernard Metzler 		smp_wmb();
902303ae1cdSBernard Metzler 		sqe->flags |= SIW_WQE_VALID;
903303ae1cdSBernard Metzler 
904303ae1cdSBernard Metzler 		qp->sq_put++;
905303ae1cdSBernard Metzler 		wr = wr->next;
906303ae1cdSBernard Metzler 	}
907303ae1cdSBernard Metzler 
908303ae1cdSBernard Metzler 	/*
909303ae1cdSBernard Metzler 	 * Send directly if SQ processing is not in progress.
910303ae1cdSBernard Metzler 	 * Eventual immediate errors (rv < 0) do not affect the involved
911303ae1cdSBernard Metzler 	 * RI resources (Verbs, 8.3.1) and thus do not prevent from SQ
912303ae1cdSBernard Metzler 	 * processing, if new work is already pending. But rv must be passed
913303ae1cdSBernard Metzler 	 * to caller.
914303ae1cdSBernard Metzler 	 */
915303ae1cdSBernard Metzler 	if (wqe->wr_status != SIW_WR_IDLE) {
916303ae1cdSBernard Metzler 		spin_unlock_irqrestore(&qp->sq_lock, flags);
917303ae1cdSBernard Metzler 		goto skip_direct_sending;
918303ae1cdSBernard Metzler 	}
919303ae1cdSBernard Metzler 	rv = siw_activate_tx(qp);
920303ae1cdSBernard Metzler 	spin_unlock_irqrestore(&qp->sq_lock, flags);
921303ae1cdSBernard Metzler 
922303ae1cdSBernard Metzler 	if (rv <= 0)
923303ae1cdSBernard Metzler 		goto skip_direct_sending;
924303ae1cdSBernard Metzler 
92558fb0b56SBernard Metzler 	if (rdma_is_kernel_res(&qp->base_qp.res)) {
926303ae1cdSBernard Metzler 		rv = siw_sq_start(qp);
927303ae1cdSBernard Metzler 	} else {
928303ae1cdSBernard Metzler 		qp->tx_ctx.in_syscall = 1;
929303ae1cdSBernard Metzler 
930303ae1cdSBernard Metzler 		if (siw_qp_sq_process(qp) != 0 && !(qp->tx_ctx.tx_suspend))
931303ae1cdSBernard Metzler 			siw_qp_cm_drop(qp, 0);
932303ae1cdSBernard Metzler 
933303ae1cdSBernard Metzler 		qp->tx_ctx.in_syscall = 0;
934303ae1cdSBernard Metzler 	}
935303ae1cdSBernard Metzler skip_direct_sending:
936303ae1cdSBernard Metzler 
937303ae1cdSBernard Metzler 	up_read(&qp->state_lock);
938303ae1cdSBernard Metzler 
939303ae1cdSBernard Metzler 	if (rv >= 0)
940303ae1cdSBernard Metzler 		return 0;
941303ae1cdSBernard Metzler 	/*
942303ae1cdSBernard Metzler 	 * Immediate error
943303ae1cdSBernard Metzler 	 */
944303ae1cdSBernard Metzler 	siw_dbg_qp(qp, "error %d\n", rv);
945303ae1cdSBernard Metzler 
946303ae1cdSBernard Metzler 	*bad_wr = wr;
947303ae1cdSBernard Metzler 	return rv;
948303ae1cdSBernard Metzler }
949303ae1cdSBernard Metzler 
950303ae1cdSBernard Metzler /*
951303ae1cdSBernard Metzler  * siw_post_receive()
952303ae1cdSBernard Metzler  *
953303ae1cdSBernard Metzler  * Post a list of R-WR's to a RQ.
954303ae1cdSBernard Metzler  *
955303ae1cdSBernard Metzler  * @base_qp:	Base QP contained in siw QP
956303ae1cdSBernard Metzler  * @wr:		Null terminated list of user WR's
957303ae1cdSBernard Metzler  * @bad_wr:	Points to failing WR in case of synchronous failure.
958303ae1cdSBernard Metzler  */
959303ae1cdSBernard Metzler int siw_post_receive(struct ib_qp *base_qp, const struct ib_recv_wr *wr,
960303ae1cdSBernard Metzler 		     const struct ib_recv_wr **bad_wr)
961303ae1cdSBernard Metzler {
962303ae1cdSBernard Metzler 	struct siw_qp *qp = to_siw_qp(base_qp);
963303ae1cdSBernard Metzler 	unsigned long flags;
964303ae1cdSBernard Metzler 	int rv = 0;
965303ae1cdSBernard Metzler 
966661f3859SBernard Metzler 	if (qp->srq || qp->attrs.rq_size == 0) {
967303ae1cdSBernard Metzler 		*bad_wr = wr;
968661f3859SBernard Metzler 		return -EINVAL;
969303ae1cdSBernard Metzler 	}
97058fb0b56SBernard Metzler 	if (!rdma_is_kernel_res(&qp->base_qp.res)) {
97158fb0b56SBernard Metzler 		siw_dbg_qp(qp, "no kernel post_recv for user mapped rq\n");
972303ae1cdSBernard Metzler 		*bad_wr = wr;
973303ae1cdSBernard Metzler 		return -EINVAL;
974303ae1cdSBernard Metzler 	}
975cf049bb3SBernard Metzler 
976303ae1cdSBernard Metzler 	/*
977303ae1cdSBernard Metzler 	 * Try to acquire QP state lock. Must be non-blocking
978303ae1cdSBernard Metzler 	 * to accommodate kernel clients needs.
979303ae1cdSBernard Metzler 	 */
980303ae1cdSBernard Metzler 	if (!down_read_trylock(&qp->state_lock)) {
981cf049bb3SBernard Metzler 		if (qp->attrs.state == SIW_QP_STATE_ERROR) {
982cf049bb3SBernard Metzler 			/*
983cf049bb3SBernard Metzler 			 * ERROR state is final, so we can be sure
984cf049bb3SBernard Metzler 			 * this state will not change as long as the QP
985cf049bb3SBernard Metzler 			 * exists.
986cf049bb3SBernard Metzler 			 *
987cf049bb3SBernard Metzler 			 * This handles an ib_drain_rq() call with
988cf049bb3SBernard Metzler 			 * a concurrent request to set the QP state
989cf049bb3SBernard Metzler 			 * to ERROR.
990cf049bb3SBernard Metzler 			 */
991cf049bb3SBernard Metzler 			rv = siw_rq_flush_wr(qp, wr, bad_wr);
992cf049bb3SBernard Metzler 		} else {
993cf049bb3SBernard Metzler 			siw_dbg_qp(qp, "QP locked, state %d\n",
994cf049bb3SBernard Metzler 				   qp->attrs.state);
995303ae1cdSBernard Metzler 			*bad_wr = wr;
996cf049bb3SBernard Metzler 			rv = -ENOTCONN;
997303ae1cdSBernard Metzler 		}
998cf049bb3SBernard Metzler 		return rv;
999303ae1cdSBernard Metzler 	}
1000303ae1cdSBernard Metzler 	if (qp->attrs.state > SIW_QP_STATE_RTS) {
1001cf049bb3SBernard Metzler 		if (qp->attrs.state == SIW_QP_STATE_ERROR) {
1002cf049bb3SBernard Metzler 			/*
1003cf049bb3SBernard Metzler 			 * Immediately flush this WR to CQ, if QP
1004cf049bb3SBernard Metzler 			 * is in ERROR state. RQ is guaranteed to
1005cf049bb3SBernard Metzler 			 * be empty, so WR complets in-order.
1006cf049bb3SBernard Metzler 			 *
1007cf049bb3SBernard Metzler 			 * Typically triggered by ib_drain_rq().
1008cf049bb3SBernard Metzler 			 */
1009cf049bb3SBernard Metzler 			rv = siw_rq_flush_wr(qp, wr, bad_wr);
1010cf049bb3SBernard Metzler 		} else {
1011cf049bb3SBernard Metzler 			siw_dbg_qp(qp, "QP out of state %d\n",
1012cf049bb3SBernard Metzler 				   qp->attrs.state);
1013303ae1cdSBernard Metzler 			*bad_wr = wr;
1014cf049bb3SBernard Metzler 			rv = -ENOTCONN;
1015cf049bb3SBernard Metzler 		}
1016cf049bb3SBernard Metzler 		up_read(&qp->state_lock);
1017cf049bb3SBernard Metzler 		return rv;
1018303ae1cdSBernard Metzler 	}
1019303ae1cdSBernard Metzler 	/*
1020303ae1cdSBernard Metzler 	 * Serialize potentially multiple producers.
1021303ae1cdSBernard Metzler 	 * Not needed for single threaded consumer side.
1022303ae1cdSBernard Metzler 	 */
1023303ae1cdSBernard Metzler 	spin_lock_irqsave(&qp->rq_lock, flags);
1024303ae1cdSBernard Metzler 
1025303ae1cdSBernard Metzler 	while (wr) {
1026303ae1cdSBernard Metzler 		u32 idx = qp->rq_put % qp->attrs.rq_size;
1027303ae1cdSBernard Metzler 		struct siw_rqe *rqe = &qp->recvq[idx];
1028303ae1cdSBernard Metzler 
1029303ae1cdSBernard Metzler 		if (rqe->flags) {
1030303ae1cdSBernard Metzler 			siw_dbg_qp(qp, "RQ full\n");
1031303ae1cdSBernard Metzler 			rv = -ENOMEM;
1032303ae1cdSBernard Metzler 			break;
1033303ae1cdSBernard Metzler 		}
1034303ae1cdSBernard Metzler 		if (wr->num_sge > qp->attrs.rq_max_sges) {
1035303ae1cdSBernard Metzler 			siw_dbg_qp(qp, "too many sge's: %d\n", wr->num_sge);
1036303ae1cdSBernard Metzler 			rv = -EINVAL;
1037303ae1cdSBernard Metzler 			break;
1038303ae1cdSBernard Metzler 		}
1039303ae1cdSBernard Metzler 		rqe->id = wr->wr_id;
1040303ae1cdSBernard Metzler 		rqe->num_sge = wr->num_sge;
1041303ae1cdSBernard Metzler 		siw_copy_sgl(wr->sg_list, rqe->sge, wr->num_sge);
1042303ae1cdSBernard Metzler 
1043303ae1cdSBernard Metzler 		/* make sure RQE is completely written before valid */
1044303ae1cdSBernard Metzler 		smp_wmb();
1045303ae1cdSBernard Metzler 
1046303ae1cdSBernard Metzler 		rqe->flags = SIW_WQE_VALID;
1047303ae1cdSBernard Metzler 
1048303ae1cdSBernard Metzler 		qp->rq_put++;
1049303ae1cdSBernard Metzler 		wr = wr->next;
1050303ae1cdSBernard Metzler 	}
1051303ae1cdSBernard Metzler 	spin_unlock_irqrestore(&qp->rq_lock, flags);
1052303ae1cdSBernard Metzler 
1053303ae1cdSBernard Metzler 	up_read(&qp->state_lock);
1054303ae1cdSBernard Metzler 
1055303ae1cdSBernard Metzler 	if (rv < 0) {
1056303ae1cdSBernard Metzler 		siw_dbg_qp(qp, "error %d\n", rv);
1057303ae1cdSBernard Metzler 		*bad_wr = wr;
1058303ae1cdSBernard Metzler 	}
1059303ae1cdSBernard Metzler 	return rv > 0 ? 0 : rv;
1060303ae1cdSBernard Metzler }
1061303ae1cdSBernard Metzler 
106243d781b9SLeon Romanovsky int siw_destroy_cq(struct ib_cq *base_cq, struct ib_udata *udata)
1063303ae1cdSBernard Metzler {
1064303ae1cdSBernard Metzler 	struct siw_cq *cq = to_siw_cq(base_cq);
1065303ae1cdSBernard Metzler 	struct siw_device *sdev = to_siw_dev(base_cq->device);
1066303ae1cdSBernard Metzler 	struct siw_ucontext *ctx =
1067303ae1cdSBernard Metzler 		rdma_udata_to_drv_context(udata, struct siw_ucontext,
1068303ae1cdSBernard Metzler 					  base_ucontext);
1069303ae1cdSBernard Metzler 
1070303ae1cdSBernard Metzler 	siw_dbg_cq(cq, "free CQ resources\n");
1071303ae1cdSBernard Metzler 
1072303ae1cdSBernard Metzler 	siw_cq_flush(cq);
1073303ae1cdSBernard Metzler 
107411f1a755SMichal Kalderon 	if (ctx)
107511f1a755SMichal Kalderon 		rdma_user_mmap_entry_remove(cq->cq_entry);
1076303ae1cdSBernard Metzler 
1077303ae1cdSBernard Metzler 	atomic_dec(&sdev->num_cq);
1078303ae1cdSBernard Metzler 
1079303ae1cdSBernard Metzler 	vfree(cq->queue);
108043d781b9SLeon Romanovsky 	return 0;
1081303ae1cdSBernard Metzler }
1082303ae1cdSBernard Metzler 
1083303ae1cdSBernard Metzler /*
1084303ae1cdSBernard Metzler  * siw_create_cq()
1085303ae1cdSBernard Metzler  *
1086303ae1cdSBernard Metzler  * Populate CQ of requested size
1087303ae1cdSBernard Metzler  *
1088303ae1cdSBernard Metzler  * @base_cq: CQ as allocated by RDMA midlayer
1089303ae1cdSBernard Metzler  * @attr: Initial CQ attributes
1090303ae1cdSBernard Metzler  * @udata: relates to user context
1091303ae1cdSBernard Metzler  */
1092303ae1cdSBernard Metzler 
1093303ae1cdSBernard Metzler int siw_create_cq(struct ib_cq *base_cq, const struct ib_cq_init_attr *attr,
1094303ae1cdSBernard Metzler 		  struct ib_udata *udata)
1095303ae1cdSBernard Metzler {
1096303ae1cdSBernard Metzler 	struct siw_device *sdev = to_siw_dev(base_cq->device);
1097303ae1cdSBernard Metzler 	struct siw_cq *cq = to_siw_cq(base_cq);
1098303ae1cdSBernard Metzler 	int rv, size = attr->cqe;
1099303ae1cdSBernard Metzler 
11001c407cb5SJason Gunthorpe 	if (attr->flags)
11011c407cb5SJason Gunthorpe 		return -EOPNOTSUPP;
11021c407cb5SJason Gunthorpe 
1103303ae1cdSBernard Metzler 	if (atomic_inc_return(&sdev->num_cq) > SIW_MAX_CQ) {
1104303ae1cdSBernard Metzler 		siw_dbg(base_cq->device, "too many CQ's\n");
1105303ae1cdSBernard Metzler 		rv = -ENOMEM;
1106303ae1cdSBernard Metzler 		goto err_out;
1107303ae1cdSBernard Metzler 	}
1108303ae1cdSBernard Metzler 	if (size < 1 || size > sdev->attrs.max_cqe) {
1109303ae1cdSBernard Metzler 		siw_dbg(base_cq->device, "CQ size error: %d\n", size);
1110303ae1cdSBernard Metzler 		rv = -EINVAL;
1111303ae1cdSBernard Metzler 		goto err_out;
1112303ae1cdSBernard Metzler 	}
1113303ae1cdSBernard Metzler 	size = roundup_pow_of_two(size);
1114303ae1cdSBernard Metzler 	cq->base_cq.cqe = size;
1115303ae1cdSBernard Metzler 	cq->num_cqe = size;
1116303ae1cdSBernard Metzler 
111758fb0b56SBernard Metzler 	if (udata)
1118303ae1cdSBernard Metzler 		cq->queue = vmalloc_user(size * sizeof(struct siw_cqe) +
1119303ae1cdSBernard Metzler 					 sizeof(struct siw_cq_ctrl));
112058fb0b56SBernard Metzler 	else
112158fb0b56SBernard Metzler 		cq->queue = vzalloc(size * sizeof(struct siw_cqe) +
112258fb0b56SBernard Metzler 				    sizeof(struct siw_cq_ctrl));
112358fb0b56SBernard Metzler 
1124303ae1cdSBernard Metzler 	if (cq->queue == NULL) {
1125303ae1cdSBernard Metzler 		rv = -ENOMEM;
1126303ae1cdSBernard Metzler 		goto err_out;
1127303ae1cdSBernard Metzler 	}
1128303ae1cdSBernard Metzler 	get_random_bytes(&cq->id, 4);
1129303ae1cdSBernard Metzler 	siw_dbg(base_cq->device, "new CQ [%u]\n", cq->id);
1130303ae1cdSBernard Metzler 
1131303ae1cdSBernard Metzler 	spin_lock_init(&cq->lock);
1132303ae1cdSBernard Metzler 
11332c8ccb37SBernard Metzler 	cq->notify = (struct siw_cq_ctrl *)&cq->queue[size];
1134303ae1cdSBernard Metzler 
1135303ae1cdSBernard Metzler 	if (udata) {
1136303ae1cdSBernard Metzler 		struct siw_uresp_create_cq uresp = {};
1137303ae1cdSBernard Metzler 		struct siw_ucontext *ctx =
1138303ae1cdSBernard Metzler 			rdma_udata_to_drv_context(udata, struct siw_ucontext,
1139303ae1cdSBernard Metzler 						  base_ucontext);
114011f1a755SMichal Kalderon 		size_t length = size * sizeof(struct siw_cqe) +
114111f1a755SMichal Kalderon 			sizeof(struct siw_cq_ctrl);
1142303ae1cdSBernard Metzler 
114311f1a755SMichal Kalderon 		cq->cq_entry =
114411f1a755SMichal Kalderon 			siw_mmap_entry_insert(ctx, cq->queue,
114511f1a755SMichal Kalderon 					      length, &uresp.cq_key);
114611f1a755SMichal Kalderon 		if (!cq->cq_entry) {
1147303ae1cdSBernard Metzler 			rv = -ENOMEM;
1148303ae1cdSBernard Metzler 			goto err_out;
1149303ae1cdSBernard Metzler 		}
115011f1a755SMichal Kalderon 
1151303ae1cdSBernard Metzler 		uresp.cq_id = cq->id;
1152303ae1cdSBernard Metzler 		uresp.num_cqe = size;
1153303ae1cdSBernard Metzler 
1154303ae1cdSBernard Metzler 		if (udata->outlen < sizeof(uresp)) {
1155303ae1cdSBernard Metzler 			rv = -EINVAL;
1156303ae1cdSBernard Metzler 			goto err_out;
1157303ae1cdSBernard Metzler 		}
1158303ae1cdSBernard Metzler 		rv = ib_copy_to_udata(udata, &uresp, sizeof(uresp));
1159303ae1cdSBernard Metzler 		if (rv)
1160303ae1cdSBernard Metzler 			goto err_out;
1161303ae1cdSBernard Metzler 	}
1162303ae1cdSBernard Metzler 	return 0;
1163303ae1cdSBernard Metzler 
1164303ae1cdSBernard Metzler err_out:
1165303ae1cdSBernard Metzler 	siw_dbg(base_cq->device, "CQ creation failed: %d", rv);
1166303ae1cdSBernard Metzler 
1167303ae1cdSBernard Metzler 	if (cq && cq->queue) {
1168303ae1cdSBernard Metzler 		struct siw_ucontext *ctx =
1169303ae1cdSBernard Metzler 			rdma_udata_to_drv_context(udata, struct siw_ucontext,
1170303ae1cdSBernard Metzler 						  base_ucontext);
117111f1a755SMichal Kalderon 		if (ctx)
117211f1a755SMichal Kalderon 			rdma_user_mmap_entry_remove(cq->cq_entry);
1173303ae1cdSBernard Metzler 		vfree(cq->queue);
1174303ae1cdSBernard Metzler 	}
1175303ae1cdSBernard Metzler 	atomic_dec(&sdev->num_cq);
1176303ae1cdSBernard Metzler 
1177303ae1cdSBernard Metzler 	return rv;
1178303ae1cdSBernard Metzler }
1179303ae1cdSBernard Metzler 
1180303ae1cdSBernard Metzler /*
1181303ae1cdSBernard Metzler  * siw_poll_cq()
1182303ae1cdSBernard Metzler  *
1183303ae1cdSBernard Metzler  * Reap CQ entries if available and copy work completion status into
1184303ae1cdSBernard Metzler  * array of WC's provided by caller. Returns number of reaped CQE's.
1185303ae1cdSBernard Metzler  *
1186303ae1cdSBernard Metzler  * @base_cq:	Base CQ contained in siw CQ.
1187303ae1cdSBernard Metzler  * @num_cqe:	Maximum number of CQE's to reap.
1188303ae1cdSBernard Metzler  * @wc:		Array of work completions to be filled by siw.
1189303ae1cdSBernard Metzler  */
1190303ae1cdSBernard Metzler int siw_poll_cq(struct ib_cq *base_cq, int num_cqe, struct ib_wc *wc)
1191303ae1cdSBernard Metzler {
1192303ae1cdSBernard Metzler 	struct siw_cq *cq = to_siw_cq(base_cq);
1193303ae1cdSBernard Metzler 	int i;
1194303ae1cdSBernard Metzler 
1195303ae1cdSBernard Metzler 	for (i = 0; i < num_cqe; i++) {
1196303ae1cdSBernard Metzler 		if (!siw_reap_cqe(cq, wc))
1197303ae1cdSBernard Metzler 			break;
1198303ae1cdSBernard Metzler 		wc++;
1199303ae1cdSBernard Metzler 	}
1200303ae1cdSBernard Metzler 	return i;
1201303ae1cdSBernard Metzler }
1202303ae1cdSBernard Metzler 
1203303ae1cdSBernard Metzler /*
1204303ae1cdSBernard Metzler  * siw_req_notify_cq()
1205303ae1cdSBernard Metzler  *
1206303ae1cdSBernard Metzler  * Request notification for new CQE's added to that CQ.
1207303ae1cdSBernard Metzler  * Defined flags:
1208303ae1cdSBernard Metzler  * o SIW_CQ_NOTIFY_SOLICITED lets siw trigger a notification
1209303ae1cdSBernard Metzler  *   event if a WQE with notification flag set enters the CQ
1210303ae1cdSBernard Metzler  * o SIW_CQ_NOTIFY_NEXT_COMP lets siw trigger a notification
1211303ae1cdSBernard Metzler  *   event if a WQE enters the CQ.
1212303ae1cdSBernard Metzler  * o IB_CQ_REPORT_MISSED_EVENTS: return value will provide the
1213303ae1cdSBernard Metzler  *   number of not reaped CQE's regardless of its notification
1214303ae1cdSBernard Metzler  *   type and current or new CQ notification settings.
1215303ae1cdSBernard Metzler  *
1216303ae1cdSBernard Metzler  * @base_cq:	Base CQ contained in siw CQ.
1217303ae1cdSBernard Metzler  * @flags:	Requested notification flags.
1218303ae1cdSBernard Metzler  */
1219303ae1cdSBernard Metzler int siw_req_notify_cq(struct ib_cq *base_cq, enum ib_cq_notify_flags flags)
1220303ae1cdSBernard Metzler {
1221303ae1cdSBernard Metzler 	struct siw_cq *cq = to_siw_cq(base_cq);
1222303ae1cdSBernard Metzler 
1223303ae1cdSBernard Metzler 	siw_dbg_cq(cq, "flags: 0x%02x\n", flags);
1224303ae1cdSBernard Metzler 
1225303ae1cdSBernard Metzler 	if ((flags & IB_CQ_SOLICITED_MASK) == IB_CQ_SOLICITED)
12262c8ccb37SBernard Metzler 		/*
12272c8ccb37SBernard Metzler 		 * Enable CQ event for next solicited completion.
12282c8ccb37SBernard Metzler 		 * and make it visible to all associated producers.
12292c8ccb37SBernard Metzler 		 */
12302c8ccb37SBernard Metzler 		smp_store_mb(cq->notify->flags, SIW_NOTIFY_SOLICITED);
1231303ae1cdSBernard Metzler 	else
12322c8ccb37SBernard Metzler 		/*
12332c8ccb37SBernard Metzler 		 * Enable CQ event for any signalled completion.
12342c8ccb37SBernard Metzler 		 * and make it visible to all associated producers.
12352c8ccb37SBernard Metzler 		 */
12362c8ccb37SBernard Metzler 		smp_store_mb(cq->notify->flags, SIW_NOTIFY_ALL);
1237303ae1cdSBernard Metzler 
1238303ae1cdSBernard Metzler 	if (flags & IB_CQ_REPORT_MISSED_EVENTS)
1239303ae1cdSBernard Metzler 		return cq->cq_put - cq->cq_get;
1240303ae1cdSBernard Metzler 
1241303ae1cdSBernard Metzler 	return 0;
1242303ae1cdSBernard Metzler }
1243303ae1cdSBernard Metzler 
1244303ae1cdSBernard Metzler /*
1245303ae1cdSBernard Metzler  * siw_dereg_mr()
1246303ae1cdSBernard Metzler  *
1247303ae1cdSBernard Metzler  * Release Memory Region.
1248303ae1cdSBernard Metzler  *
1249303ae1cdSBernard Metzler  * @base_mr: Base MR contained in siw MR.
1250303ae1cdSBernard Metzler  * @udata: points to user context, unused.
1251303ae1cdSBernard Metzler  */
1252303ae1cdSBernard Metzler int siw_dereg_mr(struct ib_mr *base_mr, struct ib_udata *udata)
1253303ae1cdSBernard Metzler {
1254303ae1cdSBernard Metzler 	struct siw_mr *mr = to_siw_mr(base_mr);
1255303ae1cdSBernard Metzler 	struct siw_device *sdev = to_siw_dev(base_mr->device);
1256303ae1cdSBernard Metzler 
1257303ae1cdSBernard Metzler 	siw_dbg_mem(mr->mem, "deregister MR\n");
1258303ae1cdSBernard Metzler 
1259303ae1cdSBernard Metzler 	atomic_dec(&sdev->num_mr);
1260303ae1cdSBernard Metzler 
1261303ae1cdSBernard Metzler 	siw_mr_drop_mem(mr);
1262303ae1cdSBernard Metzler 	kfree_rcu(mr, rcu);
1263303ae1cdSBernard Metzler 
1264303ae1cdSBernard Metzler 	return 0;
1265303ae1cdSBernard Metzler }
1266303ae1cdSBernard Metzler 
1267303ae1cdSBernard Metzler /*
1268303ae1cdSBernard Metzler  * siw_reg_user_mr()
1269303ae1cdSBernard Metzler  *
1270303ae1cdSBernard Metzler  * Register Memory Region.
1271303ae1cdSBernard Metzler  *
1272303ae1cdSBernard Metzler  * @pd:		Protection Domain
1273303ae1cdSBernard Metzler  * @start:	starting address of MR (virtual address)
1274303ae1cdSBernard Metzler  * @len:	len of MR
1275303ae1cdSBernard Metzler  * @rnic_va:	not used by siw
1276303ae1cdSBernard Metzler  * @rights:	MR access rights
1277303ae1cdSBernard Metzler  * @udata:	user buffer to communicate STag and Key.
1278303ae1cdSBernard Metzler  */
1279303ae1cdSBernard Metzler struct ib_mr *siw_reg_user_mr(struct ib_pd *pd, u64 start, u64 len,
1280303ae1cdSBernard Metzler 			      u64 rnic_va, int rights, struct ib_udata *udata)
1281303ae1cdSBernard Metzler {
1282303ae1cdSBernard Metzler 	struct siw_mr *mr = NULL;
1283303ae1cdSBernard Metzler 	struct siw_umem *umem = NULL;
1284303ae1cdSBernard Metzler 	struct siw_ureq_reg_mr ureq;
1285303ae1cdSBernard Metzler 	struct siw_device *sdev = to_siw_dev(pd->device);
1286303ae1cdSBernard Metzler 
1287303ae1cdSBernard Metzler 	unsigned long mem_limit = rlimit(RLIMIT_MEMLOCK);
1288303ae1cdSBernard Metzler 	int rv;
1289303ae1cdSBernard Metzler 
1290c536277eSBernard Metzler 	siw_dbg_pd(pd, "start: 0x%pK, va: 0x%pK, len: %llu\n",
1291c536277eSBernard Metzler 		   (void *)(uintptr_t)start, (void *)(uintptr_t)rnic_va,
1292303ae1cdSBernard Metzler 		   (unsigned long long)len);
1293303ae1cdSBernard Metzler 
1294303ae1cdSBernard Metzler 	if (atomic_inc_return(&sdev->num_mr) > SIW_MAX_MR) {
1295303ae1cdSBernard Metzler 		siw_dbg_pd(pd, "too many mr's\n");
1296303ae1cdSBernard Metzler 		rv = -ENOMEM;
1297303ae1cdSBernard Metzler 		goto err_out;
1298303ae1cdSBernard Metzler 	}
1299303ae1cdSBernard Metzler 	if (!len) {
1300303ae1cdSBernard Metzler 		rv = -EINVAL;
1301303ae1cdSBernard Metzler 		goto err_out;
1302303ae1cdSBernard Metzler 	}
1303303ae1cdSBernard Metzler 	if (mem_limit != RLIM_INFINITY) {
1304303ae1cdSBernard Metzler 		unsigned long num_pages =
1305303ae1cdSBernard Metzler 			(PAGE_ALIGN(len + (start & ~PAGE_MASK))) >> PAGE_SHIFT;
1306303ae1cdSBernard Metzler 		mem_limit >>= PAGE_SHIFT;
1307303ae1cdSBernard Metzler 
1308303ae1cdSBernard Metzler 		if (num_pages > mem_limit - current->mm->locked_vm) {
1309303ae1cdSBernard Metzler 			siw_dbg_pd(pd, "pages req %lu, max %lu, lock %lu\n",
1310303ae1cdSBernard Metzler 				   num_pages, mem_limit,
1311303ae1cdSBernard Metzler 				   current->mm->locked_vm);
1312303ae1cdSBernard Metzler 			rv = -ENOMEM;
1313303ae1cdSBernard Metzler 			goto err_out;
1314303ae1cdSBernard Metzler 		}
1315303ae1cdSBernard Metzler 	}
1316303ae1cdSBernard Metzler 	umem = siw_umem_get(start, len, ib_access_writable(rights));
1317303ae1cdSBernard Metzler 	if (IS_ERR(umem)) {
1318303ae1cdSBernard Metzler 		rv = PTR_ERR(umem);
1319303ae1cdSBernard Metzler 		siw_dbg_pd(pd, "getting user memory failed: %d\n", rv);
1320303ae1cdSBernard Metzler 		umem = NULL;
1321303ae1cdSBernard Metzler 		goto err_out;
1322303ae1cdSBernard Metzler 	}
1323303ae1cdSBernard Metzler 	mr = kzalloc(sizeof(*mr), GFP_KERNEL);
1324303ae1cdSBernard Metzler 	if (!mr) {
1325303ae1cdSBernard Metzler 		rv = -ENOMEM;
1326303ae1cdSBernard Metzler 		goto err_out;
1327303ae1cdSBernard Metzler 	}
1328303ae1cdSBernard Metzler 	rv = siw_mr_add_mem(mr, pd, umem, start, len, rights);
1329303ae1cdSBernard Metzler 	if (rv)
1330303ae1cdSBernard Metzler 		goto err_out;
1331303ae1cdSBernard Metzler 
1332303ae1cdSBernard Metzler 	if (udata) {
1333303ae1cdSBernard Metzler 		struct siw_uresp_reg_mr uresp = {};
1334303ae1cdSBernard Metzler 		struct siw_mem *mem = mr->mem;
1335303ae1cdSBernard Metzler 
1336303ae1cdSBernard Metzler 		if (udata->inlen < sizeof(ureq)) {
1337303ae1cdSBernard Metzler 			rv = -EINVAL;
1338303ae1cdSBernard Metzler 			goto err_out;
1339303ae1cdSBernard Metzler 		}
1340303ae1cdSBernard Metzler 		rv = ib_copy_from_udata(&ureq, udata, sizeof(ureq));
1341303ae1cdSBernard Metzler 		if (rv)
1342303ae1cdSBernard Metzler 			goto err_out;
1343303ae1cdSBernard Metzler 
1344303ae1cdSBernard Metzler 		mr->base_mr.lkey |= ureq.stag_key;
1345303ae1cdSBernard Metzler 		mr->base_mr.rkey |= ureq.stag_key;
1346303ae1cdSBernard Metzler 		mem->stag |= ureq.stag_key;
1347303ae1cdSBernard Metzler 		uresp.stag = mem->stag;
1348303ae1cdSBernard Metzler 
1349303ae1cdSBernard Metzler 		if (udata->outlen < sizeof(uresp)) {
1350303ae1cdSBernard Metzler 			rv = -EINVAL;
1351303ae1cdSBernard Metzler 			goto err_out;
1352303ae1cdSBernard Metzler 		}
1353303ae1cdSBernard Metzler 		rv = ib_copy_to_udata(udata, &uresp, sizeof(uresp));
1354303ae1cdSBernard Metzler 		if (rv)
1355303ae1cdSBernard Metzler 			goto err_out;
1356303ae1cdSBernard Metzler 	}
1357303ae1cdSBernard Metzler 	mr->mem->stag_valid = 1;
1358303ae1cdSBernard Metzler 
1359303ae1cdSBernard Metzler 	return &mr->base_mr;
1360303ae1cdSBernard Metzler 
1361303ae1cdSBernard Metzler err_out:
1362303ae1cdSBernard Metzler 	atomic_dec(&sdev->num_mr);
1363303ae1cdSBernard Metzler 	if (mr) {
1364303ae1cdSBernard Metzler 		if (mr->mem)
1365303ae1cdSBernard Metzler 			siw_mr_drop_mem(mr);
1366303ae1cdSBernard Metzler 		kfree_rcu(mr, rcu);
1367303ae1cdSBernard Metzler 	} else {
1368303ae1cdSBernard Metzler 		if (umem)
1369303ae1cdSBernard Metzler 			siw_umem_release(umem, false);
1370303ae1cdSBernard Metzler 	}
1371303ae1cdSBernard Metzler 	return ERR_PTR(rv);
1372303ae1cdSBernard Metzler }
1373303ae1cdSBernard Metzler 
1374303ae1cdSBernard Metzler struct ib_mr *siw_alloc_mr(struct ib_pd *pd, enum ib_mr_type mr_type,
137542a3b153SGal Pressman 			   u32 max_sge)
1376303ae1cdSBernard Metzler {
1377303ae1cdSBernard Metzler 	struct siw_device *sdev = to_siw_dev(pd->device);
1378303ae1cdSBernard Metzler 	struct siw_mr *mr = NULL;
1379303ae1cdSBernard Metzler 	struct siw_pbl *pbl = NULL;
1380303ae1cdSBernard Metzler 	int rv;
1381303ae1cdSBernard Metzler 
1382303ae1cdSBernard Metzler 	if (atomic_inc_return(&sdev->num_mr) > SIW_MAX_MR) {
1383303ae1cdSBernard Metzler 		siw_dbg_pd(pd, "too many mr's\n");
1384303ae1cdSBernard Metzler 		rv = -ENOMEM;
1385303ae1cdSBernard Metzler 		goto err_out;
1386303ae1cdSBernard Metzler 	}
1387303ae1cdSBernard Metzler 	if (mr_type != IB_MR_TYPE_MEM_REG) {
1388303ae1cdSBernard Metzler 		siw_dbg_pd(pd, "mr type %d unsupported\n", mr_type);
1389303ae1cdSBernard Metzler 		rv = -EOPNOTSUPP;
1390303ae1cdSBernard Metzler 		goto err_out;
1391303ae1cdSBernard Metzler 	}
1392303ae1cdSBernard Metzler 	if (max_sge > SIW_MAX_SGE_PBL) {
1393303ae1cdSBernard Metzler 		siw_dbg_pd(pd, "too many sge's: %d\n", max_sge);
1394303ae1cdSBernard Metzler 		rv = -ENOMEM;
1395303ae1cdSBernard Metzler 		goto err_out;
1396303ae1cdSBernard Metzler 	}
1397303ae1cdSBernard Metzler 	pbl = siw_pbl_alloc(max_sge);
1398303ae1cdSBernard Metzler 	if (IS_ERR(pbl)) {
1399303ae1cdSBernard Metzler 		rv = PTR_ERR(pbl);
1400303ae1cdSBernard Metzler 		siw_dbg_pd(pd, "pbl allocation failed: %d\n", rv);
1401303ae1cdSBernard Metzler 		pbl = NULL;
1402303ae1cdSBernard Metzler 		goto err_out;
1403303ae1cdSBernard Metzler 	}
1404303ae1cdSBernard Metzler 	mr = kzalloc(sizeof(*mr), GFP_KERNEL);
1405303ae1cdSBernard Metzler 	if (!mr) {
1406303ae1cdSBernard Metzler 		rv = -ENOMEM;
1407303ae1cdSBernard Metzler 		goto err_out;
1408303ae1cdSBernard Metzler 	}
1409303ae1cdSBernard Metzler 	rv = siw_mr_add_mem(mr, pd, pbl, 0, max_sge * PAGE_SIZE, 0);
1410303ae1cdSBernard Metzler 	if (rv)
1411303ae1cdSBernard Metzler 		goto err_out;
1412303ae1cdSBernard Metzler 
1413303ae1cdSBernard Metzler 	mr->mem->is_pbl = 1;
1414303ae1cdSBernard Metzler 
1415303ae1cdSBernard Metzler 	siw_dbg_pd(pd, "[MEM %u]: success\n", mr->mem->stag);
1416303ae1cdSBernard Metzler 
1417303ae1cdSBernard Metzler 	return &mr->base_mr;
1418303ae1cdSBernard Metzler 
1419303ae1cdSBernard Metzler err_out:
1420303ae1cdSBernard Metzler 	atomic_dec(&sdev->num_mr);
1421303ae1cdSBernard Metzler 
1422303ae1cdSBernard Metzler 	if (!mr) {
1423303ae1cdSBernard Metzler 		kfree(pbl);
1424303ae1cdSBernard Metzler 	} else {
1425303ae1cdSBernard Metzler 		if (mr->mem)
1426303ae1cdSBernard Metzler 			siw_mr_drop_mem(mr);
1427303ae1cdSBernard Metzler 		kfree_rcu(mr, rcu);
1428303ae1cdSBernard Metzler 	}
1429303ae1cdSBernard Metzler 	siw_dbg_pd(pd, "failed: %d\n", rv);
1430303ae1cdSBernard Metzler 
1431303ae1cdSBernard Metzler 	return ERR_PTR(rv);
1432303ae1cdSBernard Metzler }
1433303ae1cdSBernard Metzler 
1434303ae1cdSBernard Metzler /* Just used to count number of pages being mapped */
1435303ae1cdSBernard Metzler static int siw_set_pbl_page(struct ib_mr *base_mr, u64 buf_addr)
1436303ae1cdSBernard Metzler {
1437303ae1cdSBernard Metzler 	return 0;
1438303ae1cdSBernard Metzler }
1439303ae1cdSBernard Metzler 
1440303ae1cdSBernard Metzler int siw_map_mr_sg(struct ib_mr *base_mr, struct scatterlist *sl, int num_sle,
1441303ae1cdSBernard Metzler 		  unsigned int *sg_off)
1442303ae1cdSBernard Metzler {
1443303ae1cdSBernard Metzler 	struct scatterlist *slp;
1444303ae1cdSBernard Metzler 	struct siw_mr *mr = to_siw_mr(base_mr);
1445303ae1cdSBernard Metzler 	struct siw_mem *mem = mr->mem;
1446303ae1cdSBernard Metzler 	struct siw_pbl *pbl = mem->pbl;
1447303ae1cdSBernard Metzler 	struct siw_pble *pble;
1448c536277eSBernard Metzler 	unsigned long pbl_size;
1449303ae1cdSBernard Metzler 	int i, rv;
1450303ae1cdSBernard Metzler 
1451303ae1cdSBernard Metzler 	if (!pbl) {
1452303ae1cdSBernard Metzler 		siw_dbg_mem(mem, "no PBL allocated\n");
1453303ae1cdSBernard Metzler 		return -EINVAL;
1454303ae1cdSBernard Metzler 	}
1455303ae1cdSBernard Metzler 	pble = pbl->pbe;
1456303ae1cdSBernard Metzler 
1457303ae1cdSBernard Metzler 	if (pbl->max_buf < num_sle) {
1458303ae1cdSBernard Metzler 		siw_dbg_mem(mem, "too many SGE's: %d > %d\n",
1459303ae1cdSBernard Metzler 			    mem->pbl->max_buf, num_sle);
1460303ae1cdSBernard Metzler 		return -ENOMEM;
1461303ae1cdSBernard Metzler 	}
1462303ae1cdSBernard Metzler 	for_each_sg(sl, slp, num_sle, i) {
1463303ae1cdSBernard Metzler 		if (sg_dma_len(slp) == 0) {
1464303ae1cdSBernard Metzler 			siw_dbg_mem(mem, "empty SGE\n");
1465303ae1cdSBernard Metzler 			return -EINVAL;
1466303ae1cdSBernard Metzler 		}
1467303ae1cdSBernard Metzler 		if (i == 0) {
1468303ae1cdSBernard Metzler 			pble->addr = sg_dma_address(slp);
1469303ae1cdSBernard Metzler 			pble->size = sg_dma_len(slp);
1470303ae1cdSBernard Metzler 			pble->pbl_off = 0;
1471303ae1cdSBernard Metzler 			pbl_size = pble->size;
1472303ae1cdSBernard Metzler 			pbl->num_buf = 1;
1473303ae1cdSBernard Metzler 		} else {
1474303ae1cdSBernard Metzler 			/* Merge PBL entries if adjacent */
1475303ae1cdSBernard Metzler 			if (pble->addr + pble->size == sg_dma_address(slp)) {
1476303ae1cdSBernard Metzler 				pble->size += sg_dma_len(slp);
1477303ae1cdSBernard Metzler 			} else {
1478303ae1cdSBernard Metzler 				pble++;
1479303ae1cdSBernard Metzler 				pbl->num_buf++;
1480303ae1cdSBernard Metzler 				pble->addr = sg_dma_address(slp);
1481303ae1cdSBernard Metzler 				pble->size = sg_dma_len(slp);
1482303ae1cdSBernard Metzler 				pble->pbl_off = pbl_size;
1483303ae1cdSBernard Metzler 			}
1484303ae1cdSBernard Metzler 			pbl_size += sg_dma_len(slp);
1485303ae1cdSBernard Metzler 		}
1486303ae1cdSBernard Metzler 		siw_dbg_mem(mem,
1487c536277eSBernard Metzler 			"sge[%d], size %u, addr 0x%p, total %lu\n",
1488c536277eSBernard Metzler 			i, pble->size, (void *)(uintptr_t)pble->addr,
1489c536277eSBernard Metzler 			pbl_size);
1490303ae1cdSBernard Metzler 	}
1491303ae1cdSBernard Metzler 	rv = ib_sg_to_pages(base_mr, sl, num_sle, sg_off, siw_set_pbl_page);
1492303ae1cdSBernard Metzler 	if (rv > 0) {
1493303ae1cdSBernard Metzler 		mem->len = base_mr->length;
1494303ae1cdSBernard Metzler 		mem->va = base_mr->iova;
1495303ae1cdSBernard Metzler 		siw_dbg_mem(mem,
1496c536277eSBernard Metzler 			"%llu bytes, start 0x%pK, %u SLE to %u entries\n",
1497c536277eSBernard Metzler 			mem->len, (void *)(uintptr_t)mem->va, num_sle,
1498c536277eSBernard Metzler 			pbl->num_buf);
1499303ae1cdSBernard Metzler 	}
1500303ae1cdSBernard Metzler 	return rv;
1501303ae1cdSBernard Metzler }
1502303ae1cdSBernard Metzler 
1503303ae1cdSBernard Metzler /*
1504303ae1cdSBernard Metzler  * siw_get_dma_mr()
1505303ae1cdSBernard Metzler  *
1506303ae1cdSBernard Metzler  * Create a (empty) DMA memory region, where no umem is attached.
1507303ae1cdSBernard Metzler  */
1508303ae1cdSBernard Metzler struct ib_mr *siw_get_dma_mr(struct ib_pd *pd, int rights)
1509303ae1cdSBernard Metzler {
1510303ae1cdSBernard Metzler 	struct siw_device *sdev = to_siw_dev(pd->device);
1511303ae1cdSBernard Metzler 	struct siw_mr *mr = NULL;
1512303ae1cdSBernard Metzler 	int rv;
1513303ae1cdSBernard Metzler 
1514303ae1cdSBernard Metzler 	if (atomic_inc_return(&sdev->num_mr) > SIW_MAX_MR) {
1515303ae1cdSBernard Metzler 		siw_dbg_pd(pd, "too many mr's\n");
1516303ae1cdSBernard Metzler 		rv = -ENOMEM;
1517303ae1cdSBernard Metzler 		goto err_out;
1518303ae1cdSBernard Metzler 	}
1519303ae1cdSBernard Metzler 	mr = kzalloc(sizeof(*mr), GFP_KERNEL);
1520303ae1cdSBernard Metzler 	if (!mr) {
1521303ae1cdSBernard Metzler 		rv = -ENOMEM;
1522303ae1cdSBernard Metzler 		goto err_out;
1523303ae1cdSBernard Metzler 	}
1524303ae1cdSBernard Metzler 	rv = siw_mr_add_mem(mr, pd, NULL, 0, ULONG_MAX, rights);
1525303ae1cdSBernard Metzler 	if (rv)
1526303ae1cdSBernard Metzler 		goto err_out;
1527303ae1cdSBernard Metzler 
1528303ae1cdSBernard Metzler 	mr->mem->stag_valid = 1;
1529303ae1cdSBernard Metzler 
1530303ae1cdSBernard Metzler 	siw_dbg_pd(pd, "[MEM %u]: success\n", mr->mem->stag);
1531303ae1cdSBernard Metzler 
1532303ae1cdSBernard Metzler 	return &mr->base_mr;
1533303ae1cdSBernard Metzler 
1534303ae1cdSBernard Metzler err_out:
1535303ae1cdSBernard Metzler 	if (rv)
1536303ae1cdSBernard Metzler 		kfree(mr);
1537303ae1cdSBernard Metzler 
1538303ae1cdSBernard Metzler 	atomic_dec(&sdev->num_mr);
1539303ae1cdSBernard Metzler 
1540303ae1cdSBernard Metzler 	return ERR_PTR(rv);
1541303ae1cdSBernard Metzler }
1542303ae1cdSBernard Metzler 
1543303ae1cdSBernard Metzler /*
1544303ae1cdSBernard Metzler  * siw_create_srq()
1545303ae1cdSBernard Metzler  *
1546303ae1cdSBernard Metzler  * Create Shared Receive Queue of attributes @init_attrs
1547303ae1cdSBernard Metzler  * within protection domain given by @pd.
1548303ae1cdSBernard Metzler  *
1549303ae1cdSBernard Metzler  * @base_srq:	Base SRQ contained in siw SRQ.
1550303ae1cdSBernard Metzler  * @init_attrs:	SRQ init attributes.
1551303ae1cdSBernard Metzler  * @udata:	points to user context
1552303ae1cdSBernard Metzler  */
1553303ae1cdSBernard Metzler int siw_create_srq(struct ib_srq *base_srq,
1554303ae1cdSBernard Metzler 		   struct ib_srq_init_attr *init_attrs, struct ib_udata *udata)
1555303ae1cdSBernard Metzler {
1556303ae1cdSBernard Metzler 	struct siw_srq *srq = to_siw_srq(base_srq);
1557303ae1cdSBernard Metzler 	struct ib_srq_attr *attrs = &init_attrs->attr;
1558303ae1cdSBernard Metzler 	struct siw_device *sdev = to_siw_dev(base_srq->device);
1559303ae1cdSBernard Metzler 	struct siw_ucontext *ctx =
1560303ae1cdSBernard Metzler 		rdma_udata_to_drv_context(udata, struct siw_ucontext,
1561303ae1cdSBernard Metzler 					  base_ucontext);
1562303ae1cdSBernard Metzler 	int rv;
1563303ae1cdSBernard Metzler 
1564652caba5SJason Gunthorpe 	if (init_attrs->srq_type != IB_SRQT_BASIC)
1565652caba5SJason Gunthorpe 		return -EOPNOTSUPP;
1566652caba5SJason Gunthorpe 
1567303ae1cdSBernard Metzler 	if (atomic_inc_return(&sdev->num_srq) > SIW_MAX_SRQ) {
1568303ae1cdSBernard Metzler 		siw_dbg_pd(base_srq->pd, "too many SRQ's\n");
1569303ae1cdSBernard Metzler 		rv = -ENOMEM;
1570303ae1cdSBernard Metzler 		goto err_out;
1571303ae1cdSBernard Metzler 	}
1572303ae1cdSBernard Metzler 	if (attrs->max_wr == 0 || attrs->max_wr > SIW_MAX_SRQ_WR ||
1573303ae1cdSBernard Metzler 	    attrs->max_sge > SIW_MAX_SGE || attrs->srq_limit > attrs->max_wr) {
1574303ae1cdSBernard Metzler 		rv = -EINVAL;
1575303ae1cdSBernard Metzler 		goto err_out;
1576303ae1cdSBernard Metzler 	}
1577303ae1cdSBernard Metzler 	srq->max_sge = attrs->max_sge;
1578303ae1cdSBernard Metzler 	srq->num_rqe = roundup_pow_of_two(attrs->max_wr);
1579303ae1cdSBernard Metzler 	srq->limit = attrs->srq_limit;
1580303ae1cdSBernard Metzler 	if (srq->limit)
158158fb0b56SBernard Metzler 		srq->armed = true;
1582303ae1cdSBernard Metzler 
158358fb0b56SBernard Metzler 	srq->is_kernel_res = !udata;
1584303ae1cdSBernard Metzler 
1585303ae1cdSBernard Metzler 	if (udata)
1586303ae1cdSBernard Metzler 		srq->recvq =
1587303ae1cdSBernard Metzler 			vmalloc_user(srq->num_rqe * sizeof(struct siw_rqe));
1588303ae1cdSBernard Metzler 	else
1589303ae1cdSBernard Metzler 		srq->recvq = vzalloc(srq->num_rqe * sizeof(struct siw_rqe));
1590303ae1cdSBernard Metzler 
1591303ae1cdSBernard Metzler 	if (srq->recvq == NULL) {
1592303ae1cdSBernard Metzler 		rv = -ENOMEM;
1593303ae1cdSBernard Metzler 		goto err_out;
1594303ae1cdSBernard Metzler 	}
1595303ae1cdSBernard Metzler 	if (udata) {
1596303ae1cdSBernard Metzler 		struct siw_uresp_create_srq uresp = {};
159711f1a755SMichal Kalderon 		size_t length = srq->num_rqe * sizeof(struct siw_rqe);
1598303ae1cdSBernard Metzler 
159911f1a755SMichal Kalderon 		srq->srq_entry =
160011f1a755SMichal Kalderon 			siw_mmap_entry_insert(ctx, srq->recvq,
160111f1a755SMichal Kalderon 					      length, &uresp.srq_key);
160211f1a755SMichal Kalderon 		if (!srq->srq_entry) {
1603303ae1cdSBernard Metzler 			rv = -ENOMEM;
1604303ae1cdSBernard Metzler 			goto err_out;
1605303ae1cdSBernard Metzler 		}
160611f1a755SMichal Kalderon 
1607303ae1cdSBernard Metzler 		uresp.num_rqe = srq->num_rqe;
1608303ae1cdSBernard Metzler 
1609303ae1cdSBernard Metzler 		if (udata->outlen < sizeof(uresp)) {
1610303ae1cdSBernard Metzler 			rv = -EINVAL;
1611303ae1cdSBernard Metzler 			goto err_out;
1612303ae1cdSBernard Metzler 		}
1613303ae1cdSBernard Metzler 		rv = ib_copy_to_udata(udata, &uresp, sizeof(uresp));
1614303ae1cdSBernard Metzler 		if (rv)
1615303ae1cdSBernard Metzler 			goto err_out;
1616303ae1cdSBernard Metzler 	}
1617303ae1cdSBernard Metzler 	spin_lock_init(&srq->lock);
1618303ae1cdSBernard Metzler 
1619c536277eSBernard Metzler 	siw_dbg_pd(base_srq->pd, "[SRQ]: success\n");
1620303ae1cdSBernard Metzler 
1621303ae1cdSBernard Metzler 	return 0;
1622303ae1cdSBernard Metzler 
1623303ae1cdSBernard Metzler err_out:
1624303ae1cdSBernard Metzler 	if (srq->recvq) {
162511f1a755SMichal Kalderon 		if (ctx)
162611f1a755SMichal Kalderon 			rdma_user_mmap_entry_remove(srq->srq_entry);
1627303ae1cdSBernard Metzler 		vfree(srq->recvq);
1628303ae1cdSBernard Metzler 	}
1629303ae1cdSBernard Metzler 	atomic_dec(&sdev->num_srq);
1630303ae1cdSBernard Metzler 
1631303ae1cdSBernard Metzler 	return rv;
1632303ae1cdSBernard Metzler }
1633303ae1cdSBernard Metzler 
1634303ae1cdSBernard Metzler /*
1635303ae1cdSBernard Metzler  * siw_modify_srq()
1636303ae1cdSBernard Metzler  *
1637303ae1cdSBernard Metzler  * Modify SRQ. The caller may resize SRQ and/or set/reset notification
1638303ae1cdSBernard Metzler  * limit and (re)arm IB_EVENT_SRQ_LIMIT_REACHED notification.
1639303ae1cdSBernard Metzler  *
1640303ae1cdSBernard Metzler  * NOTE: it is unclear if RDMA core allows for changing the MAX_SGE
1641303ae1cdSBernard Metzler  * parameter. siw_modify_srq() does not check the attrs->max_sge param.
1642303ae1cdSBernard Metzler  */
1643303ae1cdSBernard Metzler int siw_modify_srq(struct ib_srq *base_srq, struct ib_srq_attr *attrs,
1644303ae1cdSBernard Metzler 		   enum ib_srq_attr_mask attr_mask, struct ib_udata *udata)
1645303ae1cdSBernard Metzler {
1646303ae1cdSBernard Metzler 	struct siw_srq *srq = to_siw_srq(base_srq);
1647303ae1cdSBernard Metzler 	unsigned long flags;
1648303ae1cdSBernard Metzler 	int rv = 0;
1649303ae1cdSBernard Metzler 
1650303ae1cdSBernard Metzler 	spin_lock_irqsave(&srq->lock, flags);
1651303ae1cdSBernard Metzler 
1652303ae1cdSBernard Metzler 	if (attr_mask & IB_SRQ_MAX_WR) {
1653303ae1cdSBernard Metzler 		/* resize request not yet supported */
1654303ae1cdSBernard Metzler 		rv = -EOPNOTSUPP;
1655303ae1cdSBernard Metzler 		goto out;
1656303ae1cdSBernard Metzler 	}
1657303ae1cdSBernard Metzler 	if (attr_mask & IB_SRQ_LIMIT) {
1658303ae1cdSBernard Metzler 		if (attrs->srq_limit) {
1659303ae1cdSBernard Metzler 			if (unlikely(attrs->srq_limit > srq->num_rqe)) {
1660303ae1cdSBernard Metzler 				rv = -EINVAL;
1661303ae1cdSBernard Metzler 				goto out;
1662303ae1cdSBernard Metzler 			}
166358fb0b56SBernard Metzler 			srq->armed = true;
1664303ae1cdSBernard Metzler 		} else {
166558fb0b56SBernard Metzler 			srq->armed = false;
1666303ae1cdSBernard Metzler 		}
1667303ae1cdSBernard Metzler 		srq->limit = attrs->srq_limit;
1668303ae1cdSBernard Metzler 	}
1669303ae1cdSBernard Metzler out:
1670303ae1cdSBernard Metzler 	spin_unlock_irqrestore(&srq->lock, flags);
1671303ae1cdSBernard Metzler 
1672303ae1cdSBernard Metzler 	return rv;
1673303ae1cdSBernard Metzler }
1674303ae1cdSBernard Metzler 
1675303ae1cdSBernard Metzler /*
1676303ae1cdSBernard Metzler  * siw_query_srq()
1677303ae1cdSBernard Metzler  *
1678303ae1cdSBernard Metzler  * Query SRQ attributes.
1679303ae1cdSBernard Metzler  */
1680303ae1cdSBernard Metzler int siw_query_srq(struct ib_srq *base_srq, struct ib_srq_attr *attrs)
1681303ae1cdSBernard Metzler {
1682303ae1cdSBernard Metzler 	struct siw_srq *srq = to_siw_srq(base_srq);
1683303ae1cdSBernard Metzler 	unsigned long flags;
1684303ae1cdSBernard Metzler 
1685303ae1cdSBernard Metzler 	spin_lock_irqsave(&srq->lock, flags);
1686303ae1cdSBernard Metzler 
1687303ae1cdSBernard Metzler 	attrs->max_wr = srq->num_rqe;
1688303ae1cdSBernard Metzler 	attrs->max_sge = srq->max_sge;
1689303ae1cdSBernard Metzler 	attrs->srq_limit = srq->limit;
1690303ae1cdSBernard Metzler 
1691303ae1cdSBernard Metzler 	spin_unlock_irqrestore(&srq->lock, flags);
1692303ae1cdSBernard Metzler 
1693303ae1cdSBernard Metzler 	return 0;
1694303ae1cdSBernard Metzler }
1695303ae1cdSBernard Metzler 
1696303ae1cdSBernard Metzler /*
1697303ae1cdSBernard Metzler  * siw_destroy_srq()
1698303ae1cdSBernard Metzler  *
1699303ae1cdSBernard Metzler  * Destroy SRQ.
1700303ae1cdSBernard Metzler  * It is assumed that the SRQ is not referenced by any
1701303ae1cdSBernard Metzler  * QP anymore - the code trusts the RDMA core environment to keep track
1702303ae1cdSBernard Metzler  * of QP references.
1703303ae1cdSBernard Metzler  */
1704119181d1SLeon Romanovsky int siw_destroy_srq(struct ib_srq *base_srq, struct ib_udata *udata)
1705303ae1cdSBernard Metzler {
1706303ae1cdSBernard Metzler 	struct siw_srq *srq = to_siw_srq(base_srq);
1707303ae1cdSBernard Metzler 	struct siw_device *sdev = to_siw_dev(base_srq->device);
1708303ae1cdSBernard Metzler 	struct siw_ucontext *ctx =
1709303ae1cdSBernard Metzler 		rdma_udata_to_drv_context(udata, struct siw_ucontext,
1710303ae1cdSBernard Metzler 					  base_ucontext);
1711303ae1cdSBernard Metzler 
171211f1a755SMichal Kalderon 	if (ctx)
171311f1a755SMichal Kalderon 		rdma_user_mmap_entry_remove(srq->srq_entry);
1714303ae1cdSBernard Metzler 	vfree(srq->recvq);
1715303ae1cdSBernard Metzler 	atomic_dec(&sdev->num_srq);
1716119181d1SLeon Romanovsky 	return 0;
1717303ae1cdSBernard Metzler }
1718303ae1cdSBernard Metzler 
1719303ae1cdSBernard Metzler /*
1720303ae1cdSBernard Metzler  * siw_post_srq_recv()
1721303ae1cdSBernard Metzler  *
1722303ae1cdSBernard Metzler  * Post a list of receive queue elements to SRQ.
1723303ae1cdSBernard Metzler  * NOTE: The function does not check or lock a certain SRQ state
1724303ae1cdSBernard Metzler  *       during the post operation. The code simply trusts the
1725303ae1cdSBernard Metzler  *       RDMA core environment.
1726303ae1cdSBernard Metzler  *
1727303ae1cdSBernard Metzler  * @base_srq:	Base SRQ contained in siw SRQ
1728303ae1cdSBernard Metzler  * @wr:		List of R-WR's
1729303ae1cdSBernard Metzler  * @bad_wr:	Updated to failing WR if posting fails.
1730303ae1cdSBernard Metzler  */
1731303ae1cdSBernard Metzler int siw_post_srq_recv(struct ib_srq *base_srq, const struct ib_recv_wr *wr,
1732303ae1cdSBernard Metzler 		      const struct ib_recv_wr **bad_wr)
1733303ae1cdSBernard Metzler {
1734303ae1cdSBernard Metzler 	struct siw_srq *srq = to_siw_srq(base_srq);
1735303ae1cdSBernard Metzler 	unsigned long flags;
1736303ae1cdSBernard Metzler 	int rv = 0;
1737303ae1cdSBernard Metzler 
173858fb0b56SBernard Metzler 	if (unlikely(!srq->is_kernel_res)) {
1739303ae1cdSBernard Metzler 		siw_dbg_pd(base_srq->pd,
1740c536277eSBernard Metzler 			   "[SRQ]: no kernel post_recv for mapped srq\n");
1741303ae1cdSBernard Metzler 		rv = -EINVAL;
1742303ae1cdSBernard Metzler 		goto out;
1743303ae1cdSBernard Metzler 	}
1744303ae1cdSBernard Metzler 	/*
1745303ae1cdSBernard Metzler 	 * Serialize potentially multiple producers.
1746303ae1cdSBernard Metzler 	 * Also needed to serialize potentially multiple
1747303ae1cdSBernard Metzler 	 * consumers.
1748303ae1cdSBernard Metzler 	 */
1749303ae1cdSBernard Metzler 	spin_lock_irqsave(&srq->lock, flags);
1750303ae1cdSBernard Metzler 
1751303ae1cdSBernard Metzler 	while (wr) {
1752303ae1cdSBernard Metzler 		u32 idx = srq->rq_put % srq->num_rqe;
1753303ae1cdSBernard Metzler 		struct siw_rqe *rqe = &srq->recvq[idx];
1754303ae1cdSBernard Metzler 
1755303ae1cdSBernard Metzler 		if (rqe->flags) {
1756303ae1cdSBernard Metzler 			siw_dbg_pd(base_srq->pd, "SRQ full\n");
1757303ae1cdSBernard Metzler 			rv = -ENOMEM;
1758303ae1cdSBernard Metzler 			break;
1759303ae1cdSBernard Metzler 		}
1760303ae1cdSBernard Metzler 		if (unlikely(wr->num_sge > srq->max_sge)) {
1761303ae1cdSBernard Metzler 			siw_dbg_pd(base_srq->pd,
1762c536277eSBernard Metzler 				   "[SRQ]: too many sge's: %d\n", wr->num_sge);
1763303ae1cdSBernard Metzler 			rv = -EINVAL;
1764303ae1cdSBernard Metzler 			break;
1765303ae1cdSBernard Metzler 		}
1766303ae1cdSBernard Metzler 		rqe->id = wr->wr_id;
1767303ae1cdSBernard Metzler 		rqe->num_sge = wr->num_sge;
1768303ae1cdSBernard Metzler 		siw_copy_sgl(wr->sg_list, rqe->sge, wr->num_sge);
1769303ae1cdSBernard Metzler 
1770303ae1cdSBernard Metzler 		/* Make sure S-RQE is completely written before valid */
1771303ae1cdSBernard Metzler 		smp_wmb();
1772303ae1cdSBernard Metzler 
1773303ae1cdSBernard Metzler 		rqe->flags = SIW_WQE_VALID;
1774303ae1cdSBernard Metzler 
1775303ae1cdSBernard Metzler 		srq->rq_put++;
1776303ae1cdSBernard Metzler 		wr = wr->next;
1777303ae1cdSBernard Metzler 	}
1778303ae1cdSBernard Metzler 	spin_unlock_irqrestore(&srq->lock, flags);
1779303ae1cdSBernard Metzler out:
1780303ae1cdSBernard Metzler 	if (unlikely(rv < 0)) {
1781c536277eSBernard Metzler 		siw_dbg_pd(base_srq->pd, "[SRQ]: error %d\n", rv);
1782303ae1cdSBernard Metzler 		*bad_wr = wr;
1783303ae1cdSBernard Metzler 	}
1784303ae1cdSBernard Metzler 	return rv;
1785303ae1cdSBernard Metzler }
1786303ae1cdSBernard Metzler 
1787303ae1cdSBernard Metzler void siw_qp_event(struct siw_qp *qp, enum ib_event_type etype)
1788303ae1cdSBernard Metzler {
1789303ae1cdSBernard Metzler 	struct ib_event event;
179058fb0b56SBernard Metzler 	struct ib_qp *base_qp = &qp->base_qp;
1791303ae1cdSBernard Metzler 
1792303ae1cdSBernard Metzler 	/*
1793303ae1cdSBernard Metzler 	 * Do not report asynchronous errors on QP which gets
1794303ae1cdSBernard Metzler 	 * destroyed via verbs interface (siw_destroy_qp())
1795303ae1cdSBernard Metzler 	 */
1796303ae1cdSBernard Metzler 	if (qp->attrs.flags & SIW_QP_IN_DESTROY)
1797303ae1cdSBernard Metzler 		return;
1798303ae1cdSBernard Metzler 
1799303ae1cdSBernard Metzler 	event.event = etype;
1800303ae1cdSBernard Metzler 	event.device = base_qp->device;
1801303ae1cdSBernard Metzler 	event.element.qp = base_qp;
1802303ae1cdSBernard Metzler 
1803303ae1cdSBernard Metzler 	if (base_qp->event_handler) {
1804303ae1cdSBernard Metzler 		siw_dbg_qp(qp, "reporting event %d\n", etype);
1805303ae1cdSBernard Metzler 		base_qp->event_handler(&event, base_qp->qp_context);
1806303ae1cdSBernard Metzler 	}
1807303ae1cdSBernard Metzler }
1808303ae1cdSBernard Metzler 
1809303ae1cdSBernard Metzler void siw_cq_event(struct siw_cq *cq, enum ib_event_type etype)
1810303ae1cdSBernard Metzler {
1811303ae1cdSBernard Metzler 	struct ib_event event;
1812303ae1cdSBernard Metzler 	struct ib_cq *base_cq = &cq->base_cq;
1813303ae1cdSBernard Metzler 
1814303ae1cdSBernard Metzler 	event.event = etype;
1815303ae1cdSBernard Metzler 	event.device = base_cq->device;
1816303ae1cdSBernard Metzler 	event.element.cq = base_cq;
1817303ae1cdSBernard Metzler 
1818303ae1cdSBernard Metzler 	if (base_cq->event_handler) {
1819303ae1cdSBernard Metzler 		siw_dbg_cq(cq, "reporting CQ event %d\n", etype);
1820303ae1cdSBernard Metzler 		base_cq->event_handler(&event, base_cq->cq_context);
1821303ae1cdSBernard Metzler 	}
1822303ae1cdSBernard Metzler }
1823303ae1cdSBernard Metzler 
1824303ae1cdSBernard Metzler void siw_srq_event(struct siw_srq *srq, enum ib_event_type etype)
1825303ae1cdSBernard Metzler {
1826303ae1cdSBernard Metzler 	struct ib_event event;
1827303ae1cdSBernard Metzler 	struct ib_srq *base_srq = &srq->base_srq;
1828303ae1cdSBernard Metzler 
1829303ae1cdSBernard Metzler 	event.event = etype;
1830303ae1cdSBernard Metzler 	event.device = base_srq->device;
1831303ae1cdSBernard Metzler 	event.element.srq = base_srq;
1832303ae1cdSBernard Metzler 
1833303ae1cdSBernard Metzler 	if (base_srq->event_handler) {
1834303ae1cdSBernard Metzler 		siw_dbg_pd(srq->base_srq.pd,
1835303ae1cdSBernard Metzler 			   "reporting SRQ event %d\n", etype);
1836303ae1cdSBernard Metzler 		base_srq->event_handler(&event, base_srq->srq_context);
1837303ae1cdSBernard Metzler 	}
1838303ae1cdSBernard Metzler }
1839303ae1cdSBernard Metzler 
18401fb7f897SMark Bloch void siw_port_event(struct siw_device *sdev, u32 port, enum ib_event_type etype)
1841303ae1cdSBernard Metzler {
1842303ae1cdSBernard Metzler 	struct ib_event event;
1843303ae1cdSBernard Metzler 
1844303ae1cdSBernard Metzler 	event.event = etype;
1845303ae1cdSBernard Metzler 	event.device = &sdev->base_dev;
1846303ae1cdSBernard Metzler 	event.element.port_num = port;
1847303ae1cdSBernard Metzler 
1848303ae1cdSBernard Metzler 	siw_dbg(&sdev->base_dev, "reporting port event %d\n", etype);
1849303ae1cdSBernard Metzler 
1850303ae1cdSBernard Metzler 	ib_dispatch_event(&event);
1851303ae1cdSBernard Metzler }
1852