1 /*
2  * Broadcom NetXtreme-E RoCE driver.
3  *
4  * Copyright (c) 2016 - 2017, Broadcom. All rights reserved.  The term
5  * Broadcom refers to Broadcom Limited and/or its subsidiaries.
6  *
7  * This software is available to you under a choice of one of two
8  * licenses.  You may choose to be licensed under the terms of the GNU
9  * General Public License (GPL) Version 2, available from the file
10  * COPYING in the main directory of this source tree, or the
11  * BSD license below:
12  *
13  * Redistribution and use in source and binary forms, with or without
14  * modification, are permitted provided that the following conditions
15  * are met:
16  *
17  * 1. Redistributions of source code must retain the above copyright
18  *    notice, this list of conditions and the following disclaimer.
19  * 2. Redistributions in binary form must reproduce the above copyright
20  *    notice, this list of conditions and the following disclaimer in
21  *    the documentation and/or other materials provided with the
22  *    distribution.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS''
25  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
26  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
27  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS
28  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
29  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
30  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
31  * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
32  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
33  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
34  * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35  *
36  * Description: IB Verbs interpreter
37  */
38 
39 #include <linux/interrupt.h>
40 #include <linux/types.h>
41 #include <linux/pci.h>
42 #include <linux/netdevice.h>
43 #include <linux/if_ether.h>
44 #include <net/addrconf.h>
45 
46 #include <rdma/ib_verbs.h>
47 #include <rdma/ib_user_verbs.h>
48 #include <rdma/ib_umem.h>
49 #include <rdma/ib_addr.h>
50 #include <rdma/ib_mad.h>
51 #include <rdma/ib_cache.h>
52 #include <rdma/uverbs_ioctl.h>
53 #include <linux/hashtable.h>
54 
55 #include "roce_hsi.h"
56 #include "qplib_res.h"
57 #include "qplib_sp.h"
58 #include "qplib_fp.h"
59 #include "qplib_rcfw.h"
60 
61 #include "bnxt_re.h"
62 #include "ib_verbs.h"
63 #include "debugfs.h"
64 
65 #include <rdma/uverbs_types.h>
66 #include <rdma/uverbs_std_types.h>
67 
68 #include <rdma/ib_user_ioctl_cmds.h>
69 
70 #define UVERBS_MODULE_NAME bnxt_re
71 #include <rdma/uverbs_named_ioctl.h>
72 
73 #include <rdma/bnxt_re-abi.h>
74 
__from_ib_access_flags(int iflags)75 static int __from_ib_access_flags(int iflags)
76 {
77 	int qflags = 0;
78 
79 	if (iflags & IB_ACCESS_LOCAL_WRITE)
80 		qflags |= BNXT_QPLIB_ACCESS_LOCAL_WRITE;
81 	if (iflags & IB_ACCESS_REMOTE_READ)
82 		qflags |= BNXT_QPLIB_ACCESS_REMOTE_READ;
83 	if (iflags & IB_ACCESS_REMOTE_WRITE)
84 		qflags |= BNXT_QPLIB_ACCESS_REMOTE_WRITE;
85 	if (iflags & IB_ACCESS_REMOTE_ATOMIC)
86 		qflags |= BNXT_QPLIB_ACCESS_REMOTE_ATOMIC;
87 	if (iflags & IB_ACCESS_MW_BIND)
88 		qflags |= BNXT_QPLIB_ACCESS_MW_BIND;
89 	if (iflags & IB_ZERO_BASED)
90 		qflags |= BNXT_QPLIB_ACCESS_ZERO_BASED;
91 	if (iflags & IB_ACCESS_ON_DEMAND)
92 		qflags |= BNXT_QPLIB_ACCESS_ON_DEMAND;
93 	return qflags;
94 };
95 
__to_ib_access_flags(int qflags)96 static int __to_ib_access_flags(int qflags)
97 {
98 	int iflags = 0;
99 
100 	if (qflags & BNXT_QPLIB_ACCESS_LOCAL_WRITE)
101 		iflags |= IB_ACCESS_LOCAL_WRITE;
102 	if (qflags & BNXT_QPLIB_ACCESS_REMOTE_WRITE)
103 		iflags |= IB_ACCESS_REMOTE_WRITE;
104 	if (qflags & BNXT_QPLIB_ACCESS_REMOTE_READ)
105 		iflags |= IB_ACCESS_REMOTE_READ;
106 	if (qflags & BNXT_QPLIB_ACCESS_REMOTE_ATOMIC)
107 		iflags |= IB_ACCESS_REMOTE_ATOMIC;
108 	if (qflags & BNXT_QPLIB_ACCESS_MW_BIND)
109 		iflags |= IB_ACCESS_MW_BIND;
110 	if (qflags & BNXT_QPLIB_ACCESS_ZERO_BASED)
111 		iflags |= IB_ZERO_BASED;
112 	if (qflags & BNXT_QPLIB_ACCESS_ON_DEMAND)
113 		iflags |= IB_ACCESS_ON_DEMAND;
114 	return iflags;
115 }
116 
__qp_access_flags_from_ib(struct bnxt_qplib_chip_ctx * cctx,int iflags)117 static u8 __qp_access_flags_from_ib(struct bnxt_qplib_chip_ctx *cctx, int iflags)
118 {
119 	u8 qflags = 0;
120 
121 	if (!bnxt_qplib_is_chip_gen_p5_p7(cctx))
122 		/* For Wh+ */
123 		return (u8)__from_ib_access_flags(iflags);
124 
125 	/* For P5, P7 and later chips */
126 	if (iflags & IB_ACCESS_LOCAL_WRITE)
127 		qflags |= CMDQ_MODIFY_QP_ACCESS_LOCAL_WRITE;
128 	if (iflags & IB_ACCESS_REMOTE_WRITE)
129 		qflags |= CMDQ_MODIFY_QP_ACCESS_REMOTE_WRITE;
130 	if (iflags & IB_ACCESS_REMOTE_READ)
131 		qflags |= CMDQ_MODIFY_QP_ACCESS_REMOTE_READ;
132 	if (iflags & IB_ACCESS_REMOTE_ATOMIC)
133 		qflags |= CMDQ_MODIFY_QP_ACCESS_REMOTE_ATOMIC;
134 
135 	return qflags;
136 }
137 
__qp_access_flags_to_ib(struct bnxt_qplib_chip_ctx * cctx,u8 qflags)138 static int __qp_access_flags_to_ib(struct bnxt_qplib_chip_ctx *cctx, u8 qflags)
139 {
140 	int iflags = 0;
141 
142 	if (!bnxt_qplib_is_chip_gen_p5_p7(cctx))
143 		/* For Wh+ */
144 		return __to_ib_access_flags(qflags);
145 
146 	/* For P5, P7 and later chips */
147 	if (qflags & CMDQ_MODIFY_QP_ACCESS_LOCAL_WRITE)
148 		iflags |= IB_ACCESS_LOCAL_WRITE;
149 	if (qflags & CMDQ_MODIFY_QP_ACCESS_REMOTE_WRITE)
150 		iflags |= IB_ACCESS_REMOTE_WRITE;
151 	if (qflags & CMDQ_MODIFY_QP_ACCESS_REMOTE_READ)
152 		iflags |= IB_ACCESS_REMOTE_READ;
153 	if (qflags & CMDQ_MODIFY_QP_ACCESS_REMOTE_ATOMIC)
154 		iflags |= IB_ACCESS_REMOTE_ATOMIC;
155 
156 	return iflags;
157 }
158 
bnxt_re_check_and_set_relaxed_ordering(struct bnxt_re_dev * rdev,struct bnxt_qplib_mrw * qplib_mr)159 static void bnxt_re_check_and_set_relaxed_ordering(struct bnxt_re_dev *rdev,
160 						   struct bnxt_qplib_mrw *qplib_mr)
161 {
162 	if (_is_relaxed_ordering_supported(rdev->dev_attr->dev_cap_flags2) &&
163 	    pcie_relaxed_ordering_enabled(rdev->en_dev->pdev))
164 		qplib_mr->flags |= CMDQ_REGISTER_MR_FLAGS_ENABLE_RO;
165 }
166 
bnxt_re_build_sgl(struct ib_sge * ib_sg_list,struct bnxt_qplib_sge * sg_list,int num)167 static int bnxt_re_build_sgl(struct ib_sge *ib_sg_list,
168 			     struct bnxt_qplib_sge *sg_list, int num)
169 {
170 	int i, total = 0;
171 
172 	for (i = 0; i < num; i++) {
173 		sg_list[i].addr = ib_sg_list[i].addr;
174 		sg_list[i].lkey = ib_sg_list[i].lkey;
175 		sg_list[i].size = ib_sg_list[i].length;
176 		total += sg_list[i].size;
177 	}
178 	return total;
179 }
180 
181 /* Device */
bnxt_re_query_device(struct ib_device * ibdev,struct ib_device_attr * ib_attr,struct ib_udata * udata)182 int bnxt_re_query_device(struct ib_device *ibdev,
183 			 struct ib_device_attr *ib_attr,
184 			 struct ib_udata *udata)
185 {
186 	struct bnxt_re_dev *rdev = to_bnxt_re_dev(ibdev, ibdev);
187 	struct bnxt_qplib_dev_attr *dev_attr = rdev->dev_attr;
188 
189 	memset(ib_attr, 0, sizeof(*ib_attr));
190 	memcpy(&ib_attr->fw_ver, dev_attr->fw_ver,
191 	       min(sizeof(dev_attr->fw_ver),
192 		   sizeof(ib_attr->fw_ver)));
193 	addrconf_addr_eui48((u8 *)&ib_attr->sys_image_guid,
194 			    rdev->netdev->dev_addr);
195 	ib_attr->max_mr_size = BNXT_RE_MAX_MR_SIZE;
196 	ib_attr->page_size_cap = BNXT_RE_PAGE_SIZE_SUPPORTED;
197 
198 	ib_attr->vendor_id = rdev->en_dev->pdev->vendor;
199 	ib_attr->vendor_part_id = rdev->en_dev->pdev->device;
200 	ib_attr->hw_ver = rdev->en_dev->pdev->revision;
201 	ib_attr->max_qp = dev_attr->max_qp;
202 	ib_attr->max_qp_wr = dev_attr->max_qp_wqes;
203 	ib_attr->device_cap_flags =
204 				    IB_DEVICE_CURR_QP_STATE_MOD
205 				    | IB_DEVICE_RC_RNR_NAK_GEN
206 				    | IB_DEVICE_SHUTDOWN_PORT
207 				    | IB_DEVICE_SYS_IMAGE_GUID
208 				    | IB_DEVICE_RESIZE_MAX_WR
209 				    | IB_DEVICE_PORT_ACTIVE_EVENT
210 				    | IB_DEVICE_N_NOTIFY_CQ
211 				    | IB_DEVICE_MEM_WINDOW
212 				    | IB_DEVICE_MEM_WINDOW_TYPE_2B
213 				    | IB_DEVICE_MEM_MGT_EXTENSIONS;
214 	ib_attr->kernel_cap_flags = IBK_LOCAL_DMA_LKEY;
215 	ib_attr->max_send_sge = dev_attr->max_qp_sges;
216 	ib_attr->max_recv_sge = dev_attr->max_qp_sges;
217 	ib_attr->max_sge_rd = dev_attr->max_qp_sges;
218 	ib_attr->max_cq = dev_attr->max_cq;
219 	ib_attr->max_cqe = dev_attr->max_cq_wqes;
220 	ib_attr->max_mr = dev_attr->max_mr;
221 	ib_attr->max_pd = dev_attr->max_pd;
222 	ib_attr->max_qp_rd_atom = dev_attr->max_qp_rd_atom;
223 	ib_attr->max_qp_init_rd_atom = dev_attr->max_qp_init_rd_atom;
224 	ib_attr->atomic_cap = IB_ATOMIC_NONE;
225 	ib_attr->masked_atomic_cap = IB_ATOMIC_NONE;
226 	if (dev_attr->is_atomic) {
227 		ib_attr->atomic_cap = IB_ATOMIC_GLOB;
228 		ib_attr->masked_atomic_cap = IB_ATOMIC_GLOB;
229 	}
230 
231 	ib_attr->max_ee_rd_atom = 0;
232 	ib_attr->max_res_rd_atom = 0;
233 	ib_attr->max_ee_init_rd_atom = 0;
234 	ib_attr->max_ee = 0;
235 	ib_attr->max_rdd = 0;
236 	ib_attr->max_mw = dev_attr->max_mw;
237 	ib_attr->max_raw_ipv6_qp = 0;
238 	ib_attr->max_raw_ethy_qp = dev_attr->max_raw_ethy_qp;
239 	ib_attr->max_mcast_grp = 0;
240 	ib_attr->max_mcast_qp_attach = 0;
241 	ib_attr->max_total_mcast_qp_attach = 0;
242 	ib_attr->max_ah = dev_attr->max_ah;
243 
244 	ib_attr->max_srq = dev_attr->max_srq;
245 	ib_attr->max_srq_wr = dev_attr->max_srq_wqes;
246 	ib_attr->max_srq_sge = dev_attr->max_srq_sges;
247 
248 	ib_attr->max_fast_reg_page_list_len = MAX_PBL_LVL_1_PGS;
249 
250 	ib_attr->max_pkeys = 1;
251 	ib_attr->local_ca_ack_delay = BNXT_RE_DEFAULT_ACK_DELAY;
252 	return 0;
253 }
254 
bnxt_re_modify_device(struct ib_device * ibdev,int device_modify_mask,struct ib_device_modify * device_modify)255 int bnxt_re_modify_device(struct ib_device *ibdev,
256 			  int device_modify_mask,
257 			  struct ib_device_modify *device_modify)
258 {
259 	ibdev_dbg(ibdev, "Modify device with mask 0x%x", device_modify_mask);
260 
261 	if (device_modify_mask & ~IB_DEVICE_MODIFY_NODE_DESC)
262 		return -EOPNOTSUPP;
263 
264 	if (!(device_modify_mask & IB_DEVICE_MODIFY_NODE_DESC))
265 		return 0;
266 
267 	memcpy(ibdev->node_desc, device_modify->node_desc, IB_DEVICE_NODE_DESC_MAX);
268 	return 0;
269 }
270 
271 /* Port */
bnxt_re_query_port(struct ib_device * ibdev,u32 port_num,struct ib_port_attr * port_attr)272 int bnxt_re_query_port(struct ib_device *ibdev, u32 port_num,
273 		       struct ib_port_attr *port_attr)
274 {
275 	struct bnxt_re_dev *rdev = to_bnxt_re_dev(ibdev, ibdev);
276 	struct bnxt_qplib_dev_attr *dev_attr = rdev->dev_attr;
277 	int rc;
278 
279 	memset(port_attr, 0, sizeof(*port_attr));
280 
281 	if (netif_running(rdev->netdev) && netif_carrier_ok(rdev->netdev)) {
282 		port_attr->state = IB_PORT_ACTIVE;
283 		port_attr->phys_state = IB_PORT_PHYS_STATE_LINK_UP;
284 	} else {
285 		port_attr->state = IB_PORT_DOWN;
286 		port_attr->phys_state = IB_PORT_PHYS_STATE_DISABLED;
287 	}
288 	port_attr->max_mtu = IB_MTU_4096;
289 	port_attr->active_mtu = iboe_get_mtu(rdev->netdev->mtu);
290 	port_attr->gid_tbl_len = dev_attr->max_sgid;
291 	port_attr->port_cap_flags = IB_PORT_CM_SUP | IB_PORT_REINIT_SUP |
292 				    IB_PORT_DEVICE_MGMT_SUP |
293 				    IB_PORT_VENDOR_CLASS_SUP;
294 	port_attr->ip_gids = true;
295 
296 	port_attr->max_msg_sz = (u32)BNXT_RE_MAX_MR_SIZE_LOW;
297 	port_attr->bad_pkey_cntr = 0;
298 	port_attr->qkey_viol_cntr = 0;
299 	port_attr->pkey_tbl_len = dev_attr->max_pkey;
300 	port_attr->lid = 0;
301 	port_attr->sm_lid = 0;
302 	port_attr->lmc = 0;
303 	port_attr->max_vl_num = 4;
304 	port_attr->sm_sl = 0;
305 	port_attr->subnet_timeout = 0;
306 	port_attr->init_type_reply = 0;
307 	rc = ib_get_eth_speed(&rdev->ibdev, port_num, &port_attr->active_speed,
308 			      &port_attr->active_width);
309 
310 	return rc;
311 }
312 
bnxt_re_get_port_immutable(struct ib_device * ibdev,u32 port_num,struct ib_port_immutable * immutable)313 int bnxt_re_get_port_immutable(struct ib_device *ibdev, u32 port_num,
314 			       struct ib_port_immutable *immutable)
315 {
316 	struct ib_port_attr port_attr;
317 
318 	if (bnxt_re_query_port(ibdev, port_num, &port_attr))
319 		return -EINVAL;
320 
321 	immutable->pkey_tbl_len = port_attr.pkey_tbl_len;
322 	immutable->gid_tbl_len = port_attr.gid_tbl_len;
323 	immutable->core_cap_flags = RDMA_CORE_PORT_IBA_ROCE;
324 	immutable->core_cap_flags |= RDMA_CORE_CAP_PROT_ROCE_UDP_ENCAP;
325 	immutable->max_mad_size = IB_MGMT_MAD_SIZE;
326 	return 0;
327 }
328 
bnxt_re_query_fw_str(struct ib_device * ibdev,char * str)329 void bnxt_re_query_fw_str(struct ib_device *ibdev, char *str)
330 {
331 	struct bnxt_re_dev *rdev = to_bnxt_re_dev(ibdev, ibdev);
332 
333 	snprintf(str, IB_FW_VERSION_NAME_MAX, "%d.%d.%d.%d",
334 		 rdev->dev_attr->fw_ver[0], rdev->dev_attr->fw_ver[1],
335 		 rdev->dev_attr->fw_ver[2], rdev->dev_attr->fw_ver[3]);
336 }
337 
bnxt_re_query_pkey(struct ib_device * ibdev,u32 port_num,u16 index,u16 * pkey)338 int bnxt_re_query_pkey(struct ib_device *ibdev, u32 port_num,
339 		       u16 index, u16 *pkey)
340 {
341 	if (index > 0)
342 		return -EINVAL;
343 
344 	*pkey = IB_DEFAULT_PKEY_FULL;
345 
346 	return 0;
347 }
348 
bnxt_re_query_gid(struct ib_device * ibdev,u32 port_num,int index,union ib_gid * gid)349 int bnxt_re_query_gid(struct ib_device *ibdev, u32 port_num,
350 		      int index, union ib_gid *gid)
351 {
352 	struct bnxt_re_dev *rdev = to_bnxt_re_dev(ibdev, ibdev);
353 	int rc;
354 
355 	/* Ignore port_num */
356 	memset(gid, 0, sizeof(*gid));
357 	rc = bnxt_qplib_get_sgid(&rdev->qplib_res,
358 				 &rdev->qplib_res.sgid_tbl, index,
359 				 (struct bnxt_qplib_gid *)gid);
360 	return rc;
361 }
362 
bnxt_re_del_gid(const struct ib_gid_attr * attr,void ** context)363 int bnxt_re_del_gid(const struct ib_gid_attr *attr, void **context)
364 {
365 	int rc = 0;
366 	struct bnxt_re_gid_ctx *ctx, **ctx_tbl;
367 	struct bnxt_re_dev *rdev = to_bnxt_re_dev(attr->device, ibdev);
368 	struct bnxt_qplib_sgid_tbl *sgid_tbl = &rdev->qplib_res.sgid_tbl;
369 	struct bnxt_qplib_gid *gid_to_del;
370 	u16 vlan_id = 0xFFFF;
371 
372 	/* Delete the entry from the hardware */
373 	ctx = *context;
374 	if (!ctx)
375 		return -EINVAL;
376 
377 	if (sgid_tbl && sgid_tbl->active) {
378 		if (ctx->idx >= sgid_tbl->max)
379 			return -EINVAL;
380 		gid_to_del = &sgid_tbl->tbl[ctx->idx].gid;
381 		vlan_id = sgid_tbl->tbl[ctx->idx].vlan_id;
382 		/* DEL_GID is called in WQ context(netdevice_event_work_handler)
383 		 * or via the ib_unregister_device path. In the former case QP1
384 		 * may not be destroyed yet, in which case just return as FW
385 		 * needs that entry to be present and will fail it's deletion.
386 		 * We could get invoked again after QP1 is destroyed OR get an
387 		 * ADD_GID call with a different GID value for the same index
388 		 * where we issue MODIFY_GID cmd to update the GID entry -- TBD
389 		 */
390 		if (ctx->idx == 0 &&
391 		    rdma_link_local_addr((struct in6_addr *)gid_to_del) &&
392 		    ctx->refcnt == 1 && rdev->gsi_ctx.gsi_sqp) {
393 			ibdev_dbg(&rdev->ibdev,
394 				  "Trying to delete GID0 while QP1 is alive\n");
395 			return -EFAULT;
396 		}
397 		ctx->refcnt--;
398 		if (!ctx->refcnt) {
399 			rc = bnxt_qplib_del_sgid(sgid_tbl, gid_to_del,
400 						 vlan_id,  true);
401 			if (rc) {
402 				ibdev_err(&rdev->ibdev,
403 					  "Failed to remove GID: %#x", rc);
404 			} else {
405 				ctx_tbl = sgid_tbl->ctx;
406 				ctx_tbl[ctx->idx] = NULL;
407 				kfree(ctx);
408 			}
409 		}
410 	} else {
411 		return -EINVAL;
412 	}
413 	return rc;
414 }
415 
bnxt_re_add_gid(const struct ib_gid_attr * attr,void ** context)416 int bnxt_re_add_gid(const struct ib_gid_attr *attr, void **context)
417 {
418 	int rc;
419 	u32 tbl_idx = 0;
420 	u16 vlan_id = 0xFFFF;
421 	struct bnxt_re_gid_ctx *ctx, **ctx_tbl;
422 	struct bnxt_re_dev *rdev = to_bnxt_re_dev(attr->device, ibdev);
423 	struct bnxt_qplib_sgid_tbl *sgid_tbl = &rdev->qplib_res.sgid_tbl;
424 
425 	rc = rdma_read_gid_l2_fields(attr, &vlan_id, NULL);
426 	if (rc)
427 		return rc;
428 
429 	rc = bnxt_qplib_add_sgid(sgid_tbl, (struct bnxt_qplib_gid *)&attr->gid,
430 				 rdev->qplib_res.netdev->dev_addr,
431 				 vlan_id, true, &tbl_idx);
432 	if (rc == -EALREADY) {
433 		ctx_tbl = sgid_tbl->ctx;
434 		ctx_tbl[tbl_idx]->refcnt++;
435 		*context = ctx_tbl[tbl_idx];
436 		return 0;
437 	}
438 
439 	if (rc < 0) {
440 		ibdev_err(&rdev->ibdev, "Failed to add GID: %#x", rc);
441 		return rc;
442 	}
443 
444 	ctx = kmalloc(sizeof(*ctx), GFP_KERNEL);
445 	if (!ctx)
446 		return -ENOMEM;
447 	ctx_tbl = sgid_tbl->ctx;
448 	ctx->idx = tbl_idx;
449 	ctx->refcnt = 1;
450 	ctx_tbl[tbl_idx] = ctx;
451 	*context = ctx;
452 
453 	return rc;
454 }
455 
bnxt_re_get_link_layer(struct ib_device * ibdev,u32 port_num)456 enum rdma_link_layer bnxt_re_get_link_layer(struct ib_device *ibdev,
457 					    u32 port_num)
458 {
459 	return IB_LINK_LAYER_ETHERNET;
460 }
461 
462 #define	BNXT_RE_FENCE_PBL_SIZE	DIV_ROUND_UP(BNXT_RE_FENCE_BYTES, PAGE_SIZE)
463 
bnxt_re_create_fence_wqe(struct bnxt_re_pd * pd)464 static void bnxt_re_create_fence_wqe(struct bnxt_re_pd *pd)
465 {
466 	struct bnxt_re_fence_data *fence = &pd->fence;
467 	struct ib_mr *ib_mr = &fence->mr->ib_mr;
468 	struct bnxt_qplib_swqe *wqe = &fence->bind_wqe;
469 	struct bnxt_re_dev *rdev = pd->rdev;
470 
471 	if (bnxt_qplib_is_chip_gen_p5_p7(rdev->chip_ctx))
472 		return;
473 
474 	memset(wqe, 0, sizeof(*wqe));
475 	wqe->type = BNXT_QPLIB_SWQE_TYPE_BIND_MW;
476 	wqe->wr_id = BNXT_QPLIB_FENCE_WRID;
477 	wqe->flags |= BNXT_QPLIB_SWQE_FLAGS_SIGNAL_COMP;
478 	wqe->flags |= BNXT_QPLIB_SWQE_FLAGS_UC_FENCE;
479 	wqe->bind.zero_based = false;
480 	wqe->bind.parent_l_key = ib_mr->lkey;
481 	wqe->bind.va = (u64)(unsigned long)fence->va;
482 	wqe->bind.length = fence->size;
483 	wqe->bind.access_cntl = __from_ib_access_flags(IB_ACCESS_REMOTE_READ);
484 	wqe->bind.mw_type = SQ_BIND_MW_TYPE_TYPE1;
485 
486 	/* Save the initial rkey in fence structure for now;
487 	 * wqe->bind.r_key will be set at (re)bind time.
488 	 */
489 	fence->bind_rkey = ib_inc_rkey(fence->mw->rkey);
490 }
491 
bnxt_re_bind_fence_mw(struct bnxt_qplib_qp * qplib_qp)492 static int bnxt_re_bind_fence_mw(struct bnxt_qplib_qp *qplib_qp)
493 {
494 	struct bnxt_re_qp *qp = container_of(qplib_qp, struct bnxt_re_qp,
495 					     qplib_qp);
496 	struct ib_pd *ib_pd = qp->ib_qp.pd;
497 	struct bnxt_re_pd *pd = container_of(ib_pd, struct bnxt_re_pd, ib_pd);
498 	struct bnxt_re_fence_data *fence = &pd->fence;
499 	struct bnxt_qplib_swqe *fence_wqe = &fence->bind_wqe;
500 	struct bnxt_qplib_swqe wqe;
501 	int rc;
502 
503 	memcpy(&wqe, fence_wqe, sizeof(wqe));
504 	wqe.bind.r_key = fence->bind_rkey;
505 	fence->bind_rkey = ib_inc_rkey(fence->bind_rkey);
506 
507 	ibdev_dbg(&qp->rdev->ibdev,
508 		  "Posting bind fence-WQE: rkey: %#x QP: %d PD: %p\n",
509 		wqe.bind.r_key, qp->qplib_qp.id, pd);
510 	rc = bnxt_qplib_post_send(&qp->qplib_qp, &wqe);
511 	if (rc) {
512 		ibdev_err(&qp->rdev->ibdev, "Failed to bind fence-WQE\n");
513 		return rc;
514 	}
515 	bnxt_qplib_post_send_db(&qp->qplib_qp);
516 
517 	return rc;
518 }
519 
bnxt_re_destroy_fence_mr(struct bnxt_re_pd * pd)520 static void bnxt_re_destroy_fence_mr(struct bnxt_re_pd *pd)
521 {
522 	struct bnxt_re_fence_data *fence = &pd->fence;
523 	struct bnxt_re_dev *rdev = pd->rdev;
524 	struct device *dev = &rdev->en_dev->pdev->dev;
525 	struct bnxt_re_mr *mr = fence->mr;
526 
527 	if (bnxt_qplib_is_chip_gen_p5_p7(rdev->chip_ctx))
528 		return;
529 
530 	if (fence->mw) {
531 		bnxt_re_dealloc_mw(fence->mw);
532 		fence->mw = NULL;
533 	}
534 	if (mr) {
535 		if (mr->ib_mr.rkey)
536 			bnxt_qplib_dereg_mrw(&rdev->qplib_res, &mr->qplib_mr,
537 					     true);
538 		if (mr->ib_mr.lkey)
539 			bnxt_qplib_free_mrw(&rdev->qplib_res, &mr->qplib_mr);
540 		kfree(mr);
541 		fence->mr = NULL;
542 	}
543 	if (fence->dma_addr) {
544 		dma_unmap_single(dev, fence->dma_addr, BNXT_RE_FENCE_BYTES,
545 				 DMA_BIDIRECTIONAL);
546 		fence->dma_addr = 0;
547 	}
548 }
549 
bnxt_re_create_fence_mr(struct bnxt_re_pd * pd)550 static int bnxt_re_create_fence_mr(struct bnxt_re_pd *pd)
551 {
552 	int mr_access_flags = IB_ACCESS_LOCAL_WRITE | IB_ACCESS_MW_BIND;
553 	struct bnxt_re_fence_data *fence = &pd->fence;
554 	struct bnxt_re_dev *rdev = pd->rdev;
555 	struct device *dev = &rdev->en_dev->pdev->dev;
556 	struct bnxt_re_mr *mr = NULL;
557 	dma_addr_t dma_addr = 0;
558 	struct ib_mw *mw;
559 	int rc;
560 
561 	if (bnxt_qplib_is_chip_gen_p5_p7(rdev->chip_ctx))
562 		return 0;
563 
564 	dma_addr = dma_map_single(dev, fence->va, BNXT_RE_FENCE_BYTES,
565 				  DMA_BIDIRECTIONAL);
566 	rc = dma_mapping_error(dev, dma_addr);
567 	if (rc) {
568 		ibdev_err(&rdev->ibdev, "Failed to dma-map fence-MR-mem\n");
569 		rc = -EIO;
570 		fence->dma_addr = 0;
571 		goto fail;
572 	}
573 	fence->dma_addr = dma_addr;
574 
575 	/* Allocate a MR */
576 	mr = kzalloc(sizeof(*mr), GFP_KERNEL);
577 	if (!mr) {
578 		rc = -ENOMEM;
579 		goto fail;
580 	}
581 	fence->mr = mr;
582 	mr->rdev = rdev;
583 	mr->qplib_mr.pd = &pd->qplib_pd;
584 	mr->qplib_mr.type = CMDQ_ALLOCATE_MRW_MRW_FLAGS_PMR;
585 	mr->qplib_mr.access_flags = __from_ib_access_flags(mr_access_flags);
586 	if (!_is_alloc_mr_unified(rdev->dev_attr->dev_cap_flags)) {
587 		rc = bnxt_qplib_alloc_mrw(&rdev->qplib_res, &mr->qplib_mr);
588 		if (rc) {
589 			ibdev_err(&rdev->ibdev, "Failed to alloc fence-HW-MR\n");
590 			goto fail;
591 		}
592 
593 		/* Register MR */
594 		mr->ib_mr.lkey = mr->qplib_mr.lkey;
595 	} else {
596 		mr->qplib_mr.flags = CMDQ_REGISTER_MR_FLAGS_ALLOC_MR;
597 	}
598 	mr->qplib_mr.va = (u64)(unsigned long)fence->va;
599 	mr->qplib_mr.total_size = BNXT_RE_FENCE_BYTES;
600 	rc = bnxt_qplib_reg_mr(&rdev->qplib_res, &mr->qplib_mr, NULL,
601 			       BNXT_RE_FENCE_PBL_SIZE, PAGE_SIZE);
602 	if (rc) {
603 		ibdev_err(&rdev->ibdev, "Failed to register fence-MR\n");
604 		goto fail;
605 	}
606 	mr->ib_mr.rkey = mr->qplib_mr.rkey;
607 
608 	/* Create a fence MW only for kernel consumers */
609 	mw = bnxt_re_alloc_mw(&pd->ib_pd, IB_MW_TYPE_1, NULL);
610 	if (IS_ERR(mw)) {
611 		ibdev_err(&rdev->ibdev,
612 			  "Failed to create fence-MW for PD: %p\n", pd);
613 		rc = PTR_ERR(mw);
614 		goto fail;
615 	}
616 	fence->mw = mw;
617 
618 	bnxt_re_create_fence_wqe(pd);
619 	return 0;
620 
621 fail:
622 	bnxt_re_destroy_fence_mr(pd);
623 	return rc;
624 }
625 
626 static struct bnxt_re_user_mmap_entry*
bnxt_re_mmap_entry_insert(struct bnxt_re_ucontext * uctx,u64 mem_offset,enum bnxt_re_mmap_flag mmap_flag,u64 * offset)627 bnxt_re_mmap_entry_insert(struct bnxt_re_ucontext *uctx, u64 mem_offset,
628 			  enum bnxt_re_mmap_flag mmap_flag, u64 *offset)
629 {
630 	struct bnxt_re_user_mmap_entry *entry;
631 	int ret;
632 
633 	entry = kzalloc(sizeof(*entry), GFP_KERNEL);
634 	if (!entry)
635 		return NULL;
636 
637 	entry->mem_offset = mem_offset;
638 	entry->mmap_flag = mmap_flag;
639 	entry->uctx = uctx;
640 
641 	switch (mmap_flag) {
642 	case BNXT_RE_MMAP_SH_PAGE:
643 		ret = rdma_user_mmap_entry_insert_exact(&uctx->ib_uctx,
644 							&entry->rdma_entry, PAGE_SIZE, 0);
645 		break;
646 	case BNXT_RE_MMAP_UC_DB:
647 	case BNXT_RE_MMAP_WC_DB:
648 	case BNXT_RE_MMAP_DBR_BAR:
649 	case BNXT_RE_MMAP_DBR_PAGE:
650 	case BNXT_RE_MMAP_TOGGLE_PAGE:
651 		ret = rdma_user_mmap_entry_insert(&uctx->ib_uctx,
652 						  &entry->rdma_entry, PAGE_SIZE);
653 		break;
654 	default:
655 		ret = -EINVAL;
656 		break;
657 	}
658 
659 	if (ret) {
660 		kfree(entry);
661 		return NULL;
662 	}
663 	if (offset)
664 		*offset = rdma_user_mmap_get_offset(&entry->rdma_entry);
665 
666 	return entry;
667 }
668 
669 /* Protection Domains */
bnxt_re_dealloc_pd(struct ib_pd * ib_pd,struct ib_udata * udata)670 int bnxt_re_dealloc_pd(struct ib_pd *ib_pd, struct ib_udata *udata)
671 {
672 	struct bnxt_re_pd *pd = container_of(ib_pd, struct bnxt_re_pd, ib_pd);
673 	struct bnxt_re_dev *rdev = pd->rdev;
674 
675 	if (udata) {
676 		rdma_user_mmap_entry_remove(pd->pd_db_mmap);
677 		pd->pd_db_mmap = NULL;
678 	}
679 
680 	bnxt_re_destroy_fence_mr(pd);
681 
682 	if (pd->qplib_pd.id) {
683 		if (!bnxt_qplib_dealloc_pd(&rdev->qplib_res,
684 					   &rdev->qplib_res.pd_tbl,
685 					   &pd->qplib_pd))
686 			atomic_dec(&rdev->stats.res.pd_count);
687 	}
688 	return 0;
689 }
690 
bnxt_re_alloc_pd(struct ib_pd * ibpd,struct ib_udata * udata)691 int bnxt_re_alloc_pd(struct ib_pd *ibpd, struct ib_udata *udata)
692 {
693 	struct ib_device *ibdev = ibpd->device;
694 	struct bnxt_re_dev *rdev = to_bnxt_re_dev(ibdev, ibdev);
695 	struct bnxt_re_ucontext *ucntx = rdma_udata_to_drv_context(
696 		udata, struct bnxt_re_ucontext, ib_uctx);
697 	struct bnxt_re_pd *pd = container_of(ibpd, struct bnxt_re_pd, ib_pd);
698 	struct bnxt_re_user_mmap_entry *entry = NULL;
699 	u32 active_pds;
700 	int rc = 0;
701 
702 	pd->rdev = rdev;
703 	if (bnxt_qplib_alloc_pd(&rdev->qplib_res, &pd->qplib_pd)) {
704 		ibdev_err(&rdev->ibdev, "Failed to allocate HW PD");
705 		rc = -ENOMEM;
706 		goto fail;
707 	}
708 
709 	if (udata) {
710 		struct bnxt_re_pd_resp resp = {};
711 
712 		if (!ucntx->dpi.dbr) {
713 			/* Allocate DPI in alloc_pd to avoid failing of
714 			 * ibv_devinfo and family of application when DPIs
715 			 * are depleted.
716 			 */
717 			if (bnxt_qplib_alloc_dpi(&rdev->qplib_res,
718 						 &ucntx->dpi, ucntx, BNXT_QPLIB_DPI_TYPE_UC)) {
719 				rc = -ENOMEM;
720 				goto dbfail;
721 			}
722 		}
723 
724 		resp.pdid = pd->qplib_pd.id;
725 		/* Still allow mapping this DBR to the new user PD. */
726 		resp.dpi = ucntx->dpi.dpi;
727 
728 		entry = bnxt_re_mmap_entry_insert(ucntx, (u64)ucntx->dpi.umdbr,
729 						  BNXT_RE_MMAP_UC_DB, &resp.dbr);
730 
731 		if (!entry) {
732 			rc = -ENOMEM;
733 			goto dbfail;
734 		}
735 
736 		pd->pd_db_mmap = &entry->rdma_entry;
737 
738 		rc = ib_copy_to_udata(udata, &resp, min(sizeof(resp), udata->outlen));
739 		if (rc) {
740 			rdma_user_mmap_entry_remove(pd->pd_db_mmap);
741 			rc = -EFAULT;
742 			goto dbfail;
743 		}
744 	}
745 
746 	if (!udata)
747 		if (bnxt_re_create_fence_mr(pd))
748 			ibdev_warn(&rdev->ibdev,
749 				   "Failed to create Fence-MR\n");
750 	active_pds = atomic_inc_return(&rdev->stats.res.pd_count);
751 	if (active_pds > rdev->stats.res.pd_watermark)
752 		rdev->stats.res.pd_watermark = active_pds;
753 
754 	return 0;
755 dbfail:
756 	bnxt_qplib_dealloc_pd(&rdev->qplib_res, &rdev->qplib_res.pd_tbl,
757 			      &pd->qplib_pd);
758 fail:
759 	return rc;
760 }
761 
762 /* Address Handles */
bnxt_re_destroy_ah(struct ib_ah * ib_ah,u32 flags)763 int bnxt_re_destroy_ah(struct ib_ah *ib_ah, u32 flags)
764 {
765 	struct bnxt_re_ah *ah = container_of(ib_ah, struct bnxt_re_ah, ib_ah);
766 	struct bnxt_re_dev *rdev = ah->rdev;
767 	bool block = true;
768 	int rc;
769 
770 	block = !(flags & RDMA_DESTROY_AH_SLEEPABLE);
771 	rc = bnxt_qplib_destroy_ah(&rdev->qplib_res, &ah->qplib_ah, block);
772 	if (BNXT_RE_CHECK_RC(rc)) {
773 		if (rc == -ETIMEDOUT)
774 			rc = 0;
775 		else
776 			goto fail;
777 	}
778 	atomic_dec(&rdev->stats.res.ah_count);
779 fail:
780 	return rc;
781 }
782 
bnxt_re_stack_to_dev_nw_type(enum rdma_network_type ntype)783 static u8 bnxt_re_stack_to_dev_nw_type(enum rdma_network_type ntype)
784 {
785 	u8 nw_type;
786 
787 	switch (ntype) {
788 	case RDMA_NETWORK_IPV4:
789 		nw_type = CMDQ_CREATE_AH_TYPE_V2IPV4;
790 		break;
791 	case RDMA_NETWORK_IPV6:
792 		nw_type = CMDQ_CREATE_AH_TYPE_V2IPV6;
793 		break;
794 	default:
795 		nw_type = CMDQ_CREATE_AH_TYPE_V1;
796 		break;
797 	}
798 	return nw_type;
799 }
800 
bnxt_re_create_ah(struct ib_ah * ib_ah,struct rdma_ah_init_attr * init_attr,struct ib_udata * udata)801 int bnxt_re_create_ah(struct ib_ah *ib_ah, struct rdma_ah_init_attr *init_attr,
802 		      struct ib_udata *udata)
803 {
804 	struct ib_pd *ib_pd = ib_ah->pd;
805 	struct bnxt_re_pd *pd = container_of(ib_pd, struct bnxt_re_pd, ib_pd);
806 	struct rdma_ah_attr *ah_attr = init_attr->ah_attr;
807 	const struct ib_global_route *grh = rdma_ah_read_grh(ah_attr);
808 	struct bnxt_re_dev *rdev = pd->rdev;
809 	const struct ib_gid_attr *sgid_attr;
810 	struct bnxt_re_gid_ctx *ctx;
811 	struct bnxt_re_ah *ah = container_of(ib_ah, struct bnxt_re_ah, ib_ah);
812 	u32 active_ahs;
813 	u8 nw_type;
814 	int rc;
815 
816 	if (!(rdma_ah_get_ah_flags(ah_attr) & IB_AH_GRH)) {
817 		ibdev_err(&rdev->ibdev, "Failed to alloc AH: GRH not set");
818 		return -EINVAL;
819 	}
820 
821 	ah->rdev = rdev;
822 	ah->qplib_ah.pd = &pd->qplib_pd;
823 
824 	/* Supply the configuration for the HW */
825 	memcpy(ah->qplib_ah.dgid.data, grh->dgid.raw,
826 	       sizeof(union ib_gid));
827 	sgid_attr = grh->sgid_attr;
828 	/* Get the HW context of the GID. The reference
829 	 * of GID table entry is already taken by the caller.
830 	 */
831 	ctx = rdma_read_gid_hw_context(sgid_attr);
832 	ah->qplib_ah.sgid_index = ctx->idx;
833 	ah->qplib_ah.host_sgid_index = grh->sgid_index;
834 	ah->qplib_ah.traffic_class = grh->traffic_class;
835 	ah->qplib_ah.flow_label = grh->flow_label;
836 	ah->qplib_ah.hop_limit = grh->hop_limit;
837 	ah->qplib_ah.sl = rdma_ah_get_sl(ah_attr);
838 
839 	/* Get network header type for this GID */
840 	nw_type = rdma_gid_attr_network_type(sgid_attr);
841 	ah->qplib_ah.nw_type = bnxt_re_stack_to_dev_nw_type(nw_type);
842 
843 	memcpy(ah->qplib_ah.dmac, ah_attr->roce.dmac, ETH_ALEN);
844 	rc = bnxt_qplib_create_ah(&rdev->qplib_res, &ah->qplib_ah,
845 				  !(init_attr->flags &
846 				    RDMA_CREATE_AH_SLEEPABLE));
847 	if (rc) {
848 		ibdev_err(&rdev->ibdev, "Failed to allocate HW AH");
849 		return rc;
850 	}
851 
852 	/* Write AVID to shared page. */
853 	if (udata) {
854 		struct bnxt_re_ucontext *uctx = rdma_udata_to_drv_context(
855 			udata, struct bnxt_re_ucontext, ib_uctx);
856 		unsigned long flag;
857 		u32 *wrptr;
858 
859 		spin_lock_irqsave(&uctx->sh_lock, flag);
860 		wrptr = (u32 *)(uctx->shpg + BNXT_RE_AVID_OFFT);
861 		*wrptr = ah->qplib_ah.id;
862 		wmb(); /* make sure cache is updated. */
863 		spin_unlock_irqrestore(&uctx->sh_lock, flag);
864 	}
865 	active_ahs = atomic_inc_return(&rdev->stats.res.ah_count);
866 	if (active_ahs > rdev->stats.res.ah_watermark)
867 		rdev->stats.res.ah_watermark = active_ahs;
868 
869 	return 0;
870 }
871 
bnxt_re_query_ah(struct ib_ah * ib_ah,struct rdma_ah_attr * ah_attr)872 int bnxt_re_query_ah(struct ib_ah *ib_ah, struct rdma_ah_attr *ah_attr)
873 {
874 	struct bnxt_re_ah *ah = container_of(ib_ah, struct bnxt_re_ah, ib_ah);
875 
876 	ah_attr->type = ib_ah->type;
877 	rdma_ah_set_sl(ah_attr, ah->qplib_ah.sl);
878 	memcpy(ah_attr->roce.dmac, ah->qplib_ah.dmac, ETH_ALEN);
879 	rdma_ah_set_grh(ah_attr, NULL, 0,
880 			ah->qplib_ah.host_sgid_index,
881 			0, ah->qplib_ah.traffic_class);
882 	rdma_ah_set_dgid_raw(ah_attr, ah->qplib_ah.dgid.data);
883 	rdma_ah_set_port_num(ah_attr, 1);
884 	rdma_ah_set_static_rate(ah_attr, 0);
885 	return 0;
886 }
887 
bnxt_re_lock_cqs(struct bnxt_re_qp * qp)888 unsigned long bnxt_re_lock_cqs(struct bnxt_re_qp *qp)
889 	__acquires(&qp->scq->cq_lock) __acquires(&qp->rcq->cq_lock)
890 {
891 	unsigned long flags;
892 
893 	spin_lock_irqsave(&qp->scq->cq_lock, flags);
894 	if (qp->rcq != qp->scq)
895 		spin_lock(&qp->rcq->cq_lock);
896 	else
897 		__acquire(&qp->rcq->cq_lock);
898 
899 	return flags;
900 }
901 
bnxt_re_unlock_cqs(struct bnxt_re_qp * qp,unsigned long flags)902 void bnxt_re_unlock_cqs(struct bnxt_re_qp *qp,
903 			unsigned long flags)
904 	__releases(&qp->scq->cq_lock) __releases(&qp->rcq->cq_lock)
905 {
906 	if (qp->rcq != qp->scq)
907 		spin_unlock(&qp->rcq->cq_lock);
908 	else
909 		__release(&qp->rcq->cq_lock);
910 	spin_unlock_irqrestore(&qp->scq->cq_lock, flags);
911 }
912 
bnxt_re_destroy_gsi_sqp(struct bnxt_re_qp * qp)913 static int bnxt_re_destroy_gsi_sqp(struct bnxt_re_qp *qp)
914 {
915 	struct bnxt_re_qp *gsi_sqp;
916 	struct bnxt_re_ah *gsi_sah;
917 	struct bnxt_re_dev *rdev;
918 	int rc;
919 
920 	rdev = qp->rdev;
921 	gsi_sqp = rdev->gsi_ctx.gsi_sqp;
922 	gsi_sah = rdev->gsi_ctx.gsi_sah;
923 
924 	ibdev_dbg(&rdev->ibdev, "Destroy the shadow AH\n");
925 	bnxt_qplib_destroy_ah(&rdev->qplib_res,
926 			      &gsi_sah->qplib_ah,
927 			      true);
928 	atomic_dec(&rdev->stats.res.ah_count);
929 	bnxt_qplib_clean_qp(&qp->qplib_qp);
930 
931 	ibdev_dbg(&rdev->ibdev, "Destroy the shadow QP\n");
932 	rc = bnxt_qplib_destroy_qp(&rdev->qplib_res, &gsi_sqp->qplib_qp);
933 	if (rc) {
934 		ibdev_err(&rdev->ibdev, "Destroy Shadow QP failed");
935 		goto fail;
936 	}
937 	bnxt_qplib_free_qp_res(&rdev->qplib_res, &gsi_sqp->qplib_qp);
938 
939 	/* remove from active qp list */
940 	mutex_lock(&rdev->qp_lock);
941 	list_del(&gsi_sqp->list);
942 	mutex_unlock(&rdev->qp_lock);
943 	atomic_dec(&rdev->stats.res.qp_count);
944 
945 	kfree(rdev->gsi_ctx.sqp_tbl);
946 	kfree(gsi_sah);
947 	kfree(gsi_sqp);
948 	rdev->gsi_ctx.gsi_sqp = NULL;
949 	rdev->gsi_ctx.gsi_sah = NULL;
950 	rdev->gsi_ctx.sqp_tbl = NULL;
951 
952 	return 0;
953 fail:
954 	return rc;
955 }
956 
957 /* Queue Pairs */
bnxt_re_destroy_qp(struct ib_qp * ib_qp,struct ib_udata * udata)958 int bnxt_re_destroy_qp(struct ib_qp *ib_qp, struct ib_udata *udata)
959 {
960 	struct bnxt_re_qp *qp = container_of(ib_qp, struct bnxt_re_qp, ib_qp);
961 	struct bnxt_qplib_qp *qplib_qp = &qp->qplib_qp;
962 	struct bnxt_re_dev *rdev = qp->rdev;
963 	struct bnxt_qplib_nq *scq_nq = NULL;
964 	struct bnxt_qplib_nq *rcq_nq = NULL;
965 	unsigned int flags;
966 	int rc;
967 
968 	bnxt_re_debug_rem_qpinfo(rdev, qp);
969 
970 	bnxt_qplib_flush_cqn_wq(&qp->qplib_qp);
971 
972 	rc = bnxt_qplib_destroy_qp(&rdev->qplib_res, &qp->qplib_qp);
973 	if (rc)
974 		ibdev_err(&rdev->ibdev, "Failed to destroy HW QP");
975 
976 	if (rdma_is_kernel_res(&qp->ib_qp.res)) {
977 		flags = bnxt_re_lock_cqs(qp);
978 		bnxt_qplib_clean_qp(&qp->qplib_qp);
979 		bnxt_re_unlock_cqs(qp, flags);
980 	}
981 
982 	bnxt_qplib_free_qp_res(&rdev->qplib_res, &qp->qplib_qp);
983 
984 	if (ib_qp->qp_type == IB_QPT_GSI && rdev->gsi_ctx.gsi_sqp)
985 		bnxt_re_destroy_gsi_sqp(qp);
986 
987 	mutex_lock(&rdev->qp_lock);
988 	list_del(&qp->list);
989 	mutex_unlock(&rdev->qp_lock);
990 	atomic_dec(&rdev->stats.res.qp_count);
991 	if (qp->qplib_qp.type == CMDQ_CREATE_QP_TYPE_RC)
992 		atomic_dec(&rdev->stats.res.rc_qp_count);
993 	else if (qp->qplib_qp.type == CMDQ_CREATE_QP_TYPE_UD)
994 		atomic_dec(&rdev->stats.res.ud_qp_count);
995 
996 	ib_umem_release(qp->rumem);
997 	ib_umem_release(qp->sumem);
998 
999 	/* Flush all the entries of notification queue associated with
1000 	 * given qp.
1001 	 */
1002 	scq_nq = qplib_qp->scq->nq;
1003 	rcq_nq = qplib_qp->rcq->nq;
1004 	bnxt_re_synchronize_nq(scq_nq);
1005 	if (scq_nq != rcq_nq)
1006 		bnxt_re_synchronize_nq(rcq_nq);
1007 
1008 	return 0;
1009 }
1010 
__from_ib_qp_type(enum ib_qp_type type)1011 static u8 __from_ib_qp_type(enum ib_qp_type type)
1012 {
1013 	switch (type) {
1014 	case IB_QPT_GSI:
1015 		return CMDQ_CREATE_QP1_TYPE_GSI;
1016 	case IB_QPT_RC:
1017 		return CMDQ_CREATE_QP_TYPE_RC;
1018 	case IB_QPT_UD:
1019 		return CMDQ_CREATE_QP_TYPE_UD;
1020 	default:
1021 		return IB_QPT_MAX;
1022 	}
1023 }
1024 
bnxt_re_setup_rwqe_size(struct bnxt_qplib_qp * qplqp,int rsge,int max)1025 static u16 bnxt_re_setup_rwqe_size(struct bnxt_qplib_qp *qplqp,
1026 				   int rsge, int max)
1027 {
1028 	if (qplqp->wqe_mode == BNXT_QPLIB_WQE_MODE_STATIC)
1029 		rsge = max;
1030 	return bnxt_re_get_rwqe_size(rsge);
1031 }
1032 
bnxt_re_get_wqe_size(int ilsize,int nsge)1033 static u16 bnxt_re_get_wqe_size(int ilsize, int nsge)
1034 {
1035 	u16 wqe_size, calc_ils;
1036 
1037 	wqe_size = bnxt_re_get_swqe_size(nsge);
1038 	if (ilsize) {
1039 		calc_ils = sizeof(struct sq_send_hdr) + ilsize;
1040 		wqe_size = max_t(u16, calc_ils, wqe_size);
1041 		wqe_size = ALIGN(wqe_size, sizeof(struct sq_send_hdr));
1042 	}
1043 	return wqe_size;
1044 }
1045 
bnxt_re_setup_swqe_size(struct bnxt_re_qp * qp,struct ib_qp_init_attr * init_attr)1046 static int bnxt_re_setup_swqe_size(struct bnxt_re_qp *qp,
1047 				   struct ib_qp_init_attr *init_attr)
1048 {
1049 	struct bnxt_qplib_dev_attr *dev_attr;
1050 	struct bnxt_qplib_qp *qplqp;
1051 	struct bnxt_re_dev *rdev;
1052 	struct bnxt_qplib_q *sq;
1053 	int align, ilsize;
1054 
1055 	rdev = qp->rdev;
1056 	qplqp = &qp->qplib_qp;
1057 	sq = &qplqp->sq;
1058 	dev_attr = rdev->dev_attr;
1059 
1060 	align = sizeof(struct sq_send_hdr);
1061 	ilsize = ALIGN(init_attr->cap.max_inline_data, align);
1062 
1063 	/* For gen p4 and gen p5 fixed wqe compatibility mode
1064 	 * wqe size is fixed to 128 bytes - ie 6 SGEs
1065 	 */
1066 	if (qplqp->wqe_mode == BNXT_QPLIB_WQE_MODE_STATIC) {
1067 		sq->wqe_size = bnxt_re_get_swqe_size(BNXT_STATIC_MAX_SGE);
1068 		sq->max_sge = BNXT_STATIC_MAX_SGE;
1069 	} else {
1070 		sq->wqe_size = bnxt_re_get_wqe_size(ilsize, sq->max_sge);
1071 		if (sq->wqe_size > bnxt_re_get_swqe_size(dev_attr->max_qp_sges))
1072 			return -EINVAL;
1073 	}
1074 
1075 	if (init_attr->cap.max_inline_data) {
1076 		qplqp->max_inline_data = sq->wqe_size -
1077 			sizeof(struct sq_send_hdr);
1078 		init_attr->cap.max_inline_data = qplqp->max_inline_data;
1079 	}
1080 
1081 	return 0;
1082 }
1083 
bnxt_re_init_user_qp(struct bnxt_re_dev * rdev,struct bnxt_re_pd * pd,struct bnxt_re_qp * qp,struct bnxt_re_ucontext * cntx,struct bnxt_re_qp_req * ureq)1084 static int bnxt_re_init_user_qp(struct bnxt_re_dev *rdev, struct bnxt_re_pd *pd,
1085 				struct bnxt_re_qp *qp, struct bnxt_re_ucontext *cntx,
1086 				struct bnxt_re_qp_req *ureq)
1087 {
1088 	struct bnxt_qplib_qp *qplib_qp;
1089 	int bytes = 0, psn_sz;
1090 	struct ib_umem *umem;
1091 	int psn_nume;
1092 
1093 	qplib_qp = &qp->qplib_qp;
1094 
1095 	bytes = (qplib_qp->sq.max_wqe * qplib_qp->sq.wqe_size);
1096 	/* Consider mapping PSN search memory only for RC QPs. */
1097 	if (qplib_qp->type == CMDQ_CREATE_QP_TYPE_RC) {
1098 		psn_sz = bnxt_qplib_is_chip_gen_p5_p7(rdev->chip_ctx) ?
1099 						   sizeof(struct sq_psn_search_ext) :
1100 						   sizeof(struct sq_psn_search);
1101 		if (cntx && bnxt_re_is_var_size_supported(rdev, cntx)) {
1102 			psn_nume = ureq->sq_slots;
1103 		} else {
1104 			psn_nume = (qplib_qp->wqe_mode == BNXT_QPLIB_WQE_MODE_STATIC) ?
1105 			qplib_qp->sq.max_wqe : ((qplib_qp->sq.max_wqe * qplib_qp->sq.wqe_size) /
1106 				 sizeof(struct bnxt_qplib_sge));
1107 		}
1108 		if (_is_host_msn_table(rdev->qplib_res.dattr->dev_cap_flags2))
1109 			psn_nume = roundup_pow_of_two(psn_nume);
1110 		bytes += (psn_nume * psn_sz);
1111 	}
1112 
1113 	bytes = PAGE_ALIGN(bytes);
1114 	umem = ib_umem_get(&rdev->ibdev, ureq->qpsva, bytes,
1115 			   IB_ACCESS_LOCAL_WRITE);
1116 	if (IS_ERR(umem))
1117 		return PTR_ERR(umem);
1118 
1119 	qp->sumem = umem;
1120 	qplib_qp->sq.sg_info.umem = umem;
1121 	qplib_qp->sq.sg_info.pgsize = PAGE_SIZE;
1122 	qplib_qp->sq.sg_info.pgshft = PAGE_SHIFT;
1123 	qplib_qp->qp_handle = ureq->qp_handle;
1124 
1125 	if (!qp->qplib_qp.srq) {
1126 		bytes = (qplib_qp->rq.max_wqe * qplib_qp->rq.wqe_size);
1127 		bytes = PAGE_ALIGN(bytes);
1128 		umem = ib_umem_get(&rdev->ibdev, ureq->qprva, bytes,
1129 				   IB_ACCESS_LOCAL_WRITE);
1130 		if (IS_ERR(umem))
1131 			goto rqfail;
1132 		qp->rumem = umem;
1133 		qplib_qp->rq.sg_info.umem = umem;
1134 		qplib_qp->rq.sg_info.pgsize = PAGE_SIZE;
1135 		qplib_qp->rq.sg_info.pgshft = PAGE_SHIFT;
1136 	}
1137 
1138 	qplib_qp->dpi = &cntx->dpi;
1139 	return 0;
1140 rqfail:
1141 	ib_umem_release(qp->sumem);
1142 	qp->sumem = NULL;
1143 	memset(&qplib_qp->sq.sg_info, 0, sizeof(qplib_qp->sq.sg_info));
1144 
1145 	return PTR_ERR(umem);
1146 }
1147 
bnxt_re_create_shadow_qp_ah(struct bnxt_re_pd * pd,struct bnxt_qplib_res * qp1_res,struct bnxt_qplib_qp * qp1_qp)1148 static struct bnxt_re_ah *bnxt_re_create_shadow_qp_ah
1149 				(struct bnxt_re_pd *pd,
1150 				 struct bnxt_qplib_res *qp1_res,
1151 				 struct bnxt_qplib_qp *qp1_qp)
1152 {
1153 	struct bnxt_re_dev *rdev = pd->rdev;
1154 	struct bnxt_re_ah *ah;
1155 	union ib_gid sgid;
1156 	int rc;
1157 
1158 	ah = kzalloc(sizeof(*ah), GFP_KERNEL);
1159 	if (!ah)
1160 		return NULL;
1161 
1162 	ah->rdev = rdev;
1163 	ah->qplib_ah.pd = &pd->qplib_pd;
1164 
1165 	rc = bnxt_re_query_gid(&rdev->ibdev, 1, 0, &sgid);
1166 	if (rc)
1167 		goto fail;
1168 
1169 	/* supply the dgid data same as sgid */
1170 	memcpy(ah->qplib_ah.dgid.data, &sgid.raw,
1171 	       sizeof(union ib_gid));
1172 	ah->qplib_ah.sgid_index = 0;
1173 
1174 	ah->qplib_ah.traffic_class = 0;
1175 	ah->qplib_ah.flow_label = 0;
1176 	ah->qplib_ah.hop_limit = 1;
1177 	ah->qplib_ah.sl = 0;
1178 	/* Have DMAC same as SMAC */
1179 	ether_addr_copy(ah->qplib_ah.dmac, rdev->netdev->dev_addr);
1180 
1181 	rc = bnxt_qplib_create_ah(&rdev->qplib_res, &ah->qplib_ah, false);
1182 	if (rc) {
1183 		ibdev_err(&rdev->ibdev,
1184 			  "Failed to allocate HW AH for Shadow QP");
1185 		goto fail;
1186 	}
1187 	atomic_inc(&rdev->stats.res.ah_count);
1188 
1189 	return ah;
1190 
1191 fail:
1192 	kfree(ah);
1193 	return NULL;
1194 }
1195 
bnxt_re_create_shadow_qp(struct bnxt_re_pd * pd,struct bnxt_qplib_res * qp1_res,struct bnxt_qplib_qp * qp1_qp)1196 static struct bnxt_re_qp *bnxt_re_create_shadow_qp
1197 				(struct bnxt_re_pd *pd,
1198 				 struct bnxt_qplib_res *qp1_res,
1199 				 struct bnxt_qplib_qp *qp1_qp)
1200 {
1201 	struct bnxt_re_dev *rdev = pd->rdev;
1202 	struct bnxt_re_qp *qp;
1203 	int rc;
1204 
1205 	qp = kzalloc(sizeof(*qp), GFP_KERNEL);
1206 	if (!qp)
1207 		return NULL;
1208 
1209 	qp->rdev = rdev;
1210 
1211 	/* Initialize the shadow QP structure from the QP1 values */
1212 	ether_addr_copy(qp->qplib_qp.smac, rdev->netdev->dev_addr);
1213 
1214 	qp->qplib_qp.pd = &pd->qplib_pd;
1215 	qp->qplib_qp.qp_handle = (u64)(unsigned long)(&qp->qplib_qp);
1216 	qp->qplib_qp.type = IB_QPT_UD;
1217 
1218 	qp->qplib_qp.max_inline_data = 0;
1219 	qp->qplib_qp.sig_type = true;
1220 
1221 	/* Shadow QP SQ depth should be same as QP1 RQ depth */
1222 	qp->qplib_qp.sq.wqe_size = bnxt_re_get_wqe_size(0, 6);
1223 	qp->qplib_qp.sq.max_wqe = qp1_qp->rq.max_wqe;
1224 	qp->qplib_qp.sq.max_sw_wqe = qp1_qp->rq.max_wqe;
1225 	qp->qplib_qp.sq.max_sge = 2;
1226 	/* Q full delta can be 1 since it is internal QP */
1227 	qp->qplib_qp.sq.q_full_delta = 1;
1228 	qp->qplib_qp.sq.sg_info.pgsize = PAGE_SIZE;
1229 	qp->qplib_qp.sq.sg_info.pgshft = PAGE_SHIFT;
1230 
1231 	qp->qplib_qp.scq = qp1_qp->scq;
1232 	qp->qplib_qp.rcq = qp1_qp->rcq;
1233 
1234 	qp->qplib_qp.rq.wqe_size = bnxt_re_get_rwqe_size(6);
1235 	qp->qplib_qp.rq.max_wqe = qp1_qp->rq.max_wqe;
1236 	qp->qplib_qp.rq.max_sw_wqe = qp1_qp->rq.max_wqe;
1237 	qp->qplib_qp.rq.max_sge = qp1_qp->rq.max_sge;
1238 	/* Q full delta can be 1 since it is internal QP */
1239 	qp->qplib_qp.rq.q_full_delta = 1;
1240 	qp->qplib_qp.rq.sg_info.pgsize = PAGE_SIZE;
1241 	qp->qplib_qp.rq.sg_info.pgshft = PAGE_SHIFT;
1242 
1243 	qp->qplib_qp.mtu = qp1_qp->mtu;
1244 
1245 	qp->qplib_qp.sq_hdr_buf_size = 0;
1246 	qp->qplib_qp.rq_hdr_buf_size = BNXT_QPLIB_MAX_GRH_HDR_SIZE_IPV6;
1247 	qp->qplib_qp.dpi = &rdev->dpi_privileged;
1248 
1249 	rc = bnxt_qplib_create_qp(qp1_res, &qp->qplib_qp);
1250 	if (rc)
1251 		goto fail;
1252 
1253 	spin_lock_init(&qp->sq_lock);
1254 	INIT_LIST_HEAD(&qp->list);
1255 	mutex_lock(&rdev->qp_lock);
1256 	list_add_tail(&qp->list, &rdev->qp_list);
1257 	atomic_inc(&rdev->stats.res.qp_count);
1258 	mutex_unlock(&rdev->qp_lock);
1259 	return qp;
1260 fail:
1261 	kfree(qp);
1262 	return NULL;
1263 }
1264 
bnxt_re_init_rq_attr(struct bnxt_re_qp * qp,struct ib_qp_init_attr * init_attr,struct bnxt_re_ucontext * uctx)1265 static int bnxt_re_init_rq_attr(struct bnxt_re_qp *qp,
1266 				struct ib_qp_init_attr *init_attr,
1267 				struct bnxt_re_ucontext *uctx)
1268 {
1269 	struct bnxt_qplib_dev_attr *dev_attr;
1270 	struct bnxt_qplib_qp *qplqp;
1271 	struct bnxt_re_dev *rdev;
1272 	struct bnxt_qplib_q *rq;
1273 	int entries;
1274 
1275 	rdev = qp->rdev;
1276 	qplqp = &qp->qplib_qp;
1277 	rq = &qplqp->rq;
1278 	dev_attr = rdev->dev_attr;
1279 
1280 	if (init_attr->srq) {
1281 		struct bnxt_re_srq *srq;
1282 
1283 		srq = container_of(init_attr->srq, struct bnxt_re_srq, ib_srq);
1284 		qplqp->srq = &srq->qplib_srq;
1285 		rq->max_wqe = 0;
1286 	} else {
1287 		rq->max_sge = init_attr->cap.max_recv_sge;
1288 		if (rq->max_sge > dev_attr->max_qp_sges)
1289 			rq->max_sge = dev_attr->max_qp_sges;
1290 		init_attr->cap.max_recv_sge = rq->max_sge;
1291 		rq->wqe_size = bnxt_re_setup_rwqe_size(qplqp, rq->max_sge,
1292 						       dev_attr->max_qp_sges);
1293 		/* Allocate 1 more than what's provided so posting max doesn't
1294 		 * mean empty.
1295 		 */
1296 		entries = bnxt_re_init_depth(init_attr->cap.max_recv_wr + 1, uctx);
1297 		rq->max_wqe = min_t(u32, entries, dev_attr->max_qp_wqes + 1);
1298 		rq->max_sw_wqe = rq->max_wqe;
1299 		rq->q_full_delta = 0;
1300 		rq->sg_info.pgsize = PAGE_SIZE;
1301 		rq->sg_info.pgshft = PAGE_SHIFT;
1302 	}
1303 
1304 	return 0;
1305 }
1306 
bnxt_re_adjust_gsi_rq_attr(struct bnxt_re_qp * qp)1307 static void bnxt_re_adjust_gsi_rq_attr(struct bnxt_re_qp *qp)
1308 {
1309 	struct bnxt_qplib_dev_attr *dev_attr;
1310 	struct bnxt_qplib_qp *qplqp;
1311 	struct bnxt_re_dev *rdev;
1312 
1313 	rdev = qp->rdev;
1314 	qplqp = &qp->qplib_qp;
1315 	dev_attr = rdev->dev_attr;
1316 
1317 	if (!bnxt_qplib_is_chip_gen_p5_p7(rdev->chip_ctx)) {
1318 		qplqp->rq.max_sge = dev_attr->max_qp_sges;
1319 		if (qplqp->rq.max_sge > dev_attr->max_qp_sges)
1320 			qplqp->rq.max_sge = dev_attr->max_qp_sges;
1321 		qplqp->rq.max_sge = 6;
1322 	}
1323 }
1324 
bnxt_re_init_sq_attr(struct bnxt_re_qp * qp,struct ib_qp_init_attr * init_attr,struct bnxt_re_ucontext * uctx,struct bnxt_re_qp_req * ureq)1325 static int bnxt_re_init_sq_attr(struct bnxt_re_qp *qp,
1326 				struct ib_qp_init_attr *init_attr,
1327 				struct bnxt_re_ucontext *uctx,
1328 				struct bnxt_re_qp_req *ureq)
1329 {
1330 	struct bnxt_qplib_dev_attr *dev_attr;
1331 	struct bnxt_qplib_qp *qplqp;
1332 	struct bnxt_re_dev *rdev;
1333 	struct bnxt_qplib_q *sq;
1334 	int diff = 0;
1335 	int entries;
1336 	int rc;
1337 
1338 	rdev = qp->rdev;
1339 	qplqp = &qp->qplib_qp;
1340 	sq = &qplqp->sq;
1341 	dev_attr = rdev->dev_attr;
1342 
1343 	sq->max_sge = init_attr->cap.max_send_sge;
1344 	entries = init_attr->cap.max_send_wr;
1345 	if (uctx && qplqp->wqe_mode == BNXT_QPLIB_WQE_MODE_VARIABLE) {
1346 		sq->max_wqe = ureq->sq_slots;
1347 		sq->max_sw_wqe = ureq->sq_slots;
1348 		sq->wqe_size = sizeof(struct sq_sge);
1349 	} else {
1350 		if (sq->max_sge > dev_attr->max_qp_sges) {
1351 			sq->max_sge = dev_attr->max_qp_sges;
1352 			init_attr->cap.max_send_sge = sq->max_sge;
1353 		}
1354 
1355 		rc = bnxt_re_setup_swqe_size(qp, init_attr);
1356 		if (rc)
1357 			return rc;
1358 
1359 		/* Allocate 128 + 1 more than what's provided */
1360 		diff = (qplqp->wqe_mode == BNXT_QPLIB_WQE_MODE_VARIABLE) ?
1361 			0 : BNXT_QPLIB_RESERVED_QP_WRS;
1362 		entries = bnxt_re_init_depth(entries + diff + 1, uctx);
1363 		sq->max_wqe = min_t(u32, entries, dev_attr->max_qp_wqes + diff + 1);
1364 		if (qplqp->wqe_mode == BNXT_QPLIB_WQE_MODE_VARIABLE)
1365 			sq->max_sw_wqe = bnxt_qplib_get_depth(sq, qplqp->wqe_mode, true);
1366 		else
1367 			sq->max_sw_wqe = sq->max_wqe;
1368 
1369 	}
1370 	sq->q_full_delta = diff + 1;
1371 	/*
1372 	 * Reserving one slot for Phantom WQE. Application can
1373 	 * post one extra entry in this case. But allowing this to avoid
1374 	 * unexpected Queue full condition
1375 	 */
1376 	qplqp->sq.q_full_delta -= 1;
1377 	qplqp->sq.sg_info.pgsize = PAGE_SIZE;
1378 	qplqp->sq.sg_info.pgshft = PAGE_SHIFT;
1379 
1380 	return 0;
1381 }
1382 
bnxt_re_adjust_gsi_sq_attr(struct bnxt_re_qp * qp,struct ib_qp_init_attr * init_attr,struct bnxt_re_ucontext * uctx)1383 static void bnxt_re_adjust_gsi_sq_attr(struct bnxt_re_qp *qp,
1384 				       struct ib_qp_init_attr *init_attr,
1385 				       struct bnxt_re_ucontext *uctx)
1386 {
1387 	struct bnxt_qplib_dev_attr *dev_attr;
1388 	struct bnxt_qplib_qp *qplqp;
1389 	struct bnxt_re_dev *rdev;
1390 	int entries;
1391 
1392 	rdev = qp->rdev;
1393 	qplqp = &qp->qplib_qp;
1394 	dev_attr = rdev->dev_attr;
1395 
1396 	if (!bnxt_qplib_is_chip_gen_p5_p7(rdev->chip_ctx)) {
1397 		entries = bnxt_re_init_depth(init_attr->cap.max_send_wr + 1, uctx);
1398 		qplqp->sq.max_wqe = min_t(u32, entries,
1399 					  dev_attr->max_qp_wqes + 1);
1400 		qplqp->sq.q_full_delta = qplqp->sq.max_wqe -
1401 			init_attr->cap.max_send_wr;
1402 		qplqp->sq.max_sge++; /* Need one extra sge to put UD header */
1403 		if (qplqp->sq.max_sge > dev_attr->max_qp_sges)
1404 			qplqp->sq.max_sge = dev_attr->max_qp_sges;
1405 	}
1406 }
1407 
bnxt_re_init_qp_type(struct bnxt_re_dev * rdev,struct ib_qp_init_attr * init_attr)1408 static int bnxt_re_init_qp_type(struct bnxt_re_dev *rdev,
1409 				struct ib_qp_init_attr *init_attr)
1410 {
1411 	struct bnxt_qplib_chip_ctx *chip_ctx;
1412 	int qptype;
1413 
1414 	chip_ctx = rdev->chip_ctx;
1415 
1416 	qptype = __from_ib_qp_type(init_attr->qp_type);
1417 	if (qptype == IB_QPT_MAX) {
1418 		ibdev_err(&rdev->ibdev, "QP type 0x%x not supported", qptype);
1419 		qptype = -EOPNOTSUPP;
1420 		goto out;
1421 	}
1422 
1423 	if (bnxt_qplib_is_chip_gen_p5_p7(chip_ctx) &&
1424 	    init_attr->qp_type == IB_QPT_GSI)
1425 		qptype = CMDQ_CREATE_QP_TYPE_GSI;
1426 out:
1427 	return qptype;
1428 }
1429 
bnxt_re_init_qp_attr(struct bnxt_re_qp * qp,struct bnxt_re_pd * pd,struct ib_qp_init_attr * init_attr,struct bnxt_re_ucontext * uctx,struct bnxt_re_qp_req * ureq)1430 static int bnxt_re_init_qp_attr(struct bnxt_re_qp *qp, struct bnxt_re_pd *pd,
1431 				struct ib_qp_init_attr *init_attr,
1432 				struct bnxt_re_ucontext *uctx,
1433 				struct bnxt_re_qp_req *ureq)
1434 {
1435 	struct bnxt_qplib_dev_attr *dev_attr;
1436 	struct bnxt_qplib_qp *qplqp;
1437 	struct bnxt_re_dev *rdev;
1438 	struct bnxt_re_cq *cq;
1439 	int rc = 0, qptype;
1440 
1441 	rdev = qp->rdev;
1442 	qplqp = &qp->qplib_qp;
1443 	dev_attr = rdev->dev_attr;
1444 
1445 	/* Setup misc params */
1446 	ether_addr_copy(qplqp->smac, rdev->netdev->dev_addr);
1447 	qplqp->pd = &pd->qplib_pd;
1448 	qplqp->qp_handle = (u64)qplqp;
1449 	qplqp->max_inline_data = init_attr->cap.max_inline_data;
1450 	qplqp->sig_type = init_attr->sq_sig_type == IB_SIGNAL_ALL_WR;
1451 	qptype = bnxt_re_init_qp_type(rdev, init_attr);
1452 	if (qptype < 0) {
1453 		rc = qptype;
1454 		goto out;
1455 	}
1456 	qplqp->type = (u8)qptype;
1457 	qplqp->wqe_mode = bnxt_re_is_var_size_supported(rdev, uctx);
1458 	if (init_attr->qp_type == IB_QPT_RC) {
1459 		qplqp->max_rd_atomic = dev_attr->max_qp_rd_atom;
1460 		qplqp->max_dest_rd_atomic = dev_attr->max_qp_init_rd_atom;
1461 	}
1462 	qplqp->mtu = ib_mtu_enum_to_int(iboe_get_mtu(rdev->netdev->mtu));
1463 	qplqp->dpi = &rdev->dpi_privileged; /* Doorbell page */
1464 	if (init_attr->create_flags) {
1465 		ibdev_dbg(&rdev->ibdev,
1466 			  "QP create flags 0x%x not supported",
1467 			  init_attr->create_flags);
1468 		return -EOPNOTSUPP;
1469 	}
1470 
1471 	/* Setup CQs */
1472 	if (init_attr->send_cq) {
1473 		cq = container_of(init_attr->send_cq, struct bnxt_re_cq, ib_cq);
1474 		qplqp->scq = &cq->qplib_cq;
1475 		qp->scq = cq;
1476 	}
1477 
1478 	if (init_attr->recv_cq) {
1479 		cq = container_of(init_attr->recv_cq, struct bnxt_re_cq, ib_cq);
1480 		qplqp->rcq = &cq->qplib_cq;
1481 		qp->rcq = cq;
1482 	}
1483 
1484 	/* Setup RQ/SRQ */
1485 	rc = bnxt_re_init_rq_attr(qp, init_attr, uctx);
1486 	if (rc)
1487 		goto out;
1488 	if (init_attr->qp_type == IB_QPT_GSI)
1489 		bnxt_re_adjust_gsi_rq_attr(qp);
1490 
1491 	/* Setup SQ */
1492 	rc = bnxt_re_init_sq_attr(qp, init_attr, uctx, ureq);
1493 	if (rc)
1494 		goto out;
1495 	if (init_attr->qp_type == IB_QPT_GSI)
1496 		bnxt_re_adjust_gsi_sq_attr(qp, init_attr, uctx);
1497 
1498 	if (uctx) /* This will update DPI and qp_handle */
1499 		rc = bnxt_re_init_user_qp(rdev, pd, qp, uctx, ureq);
1500 out:
1501 	return rc;
1502 }
1503 
bnxt_re_create_shadow_gsi(struct bnxt_re_qp * qp,struct bnxt_re_pd * pd)1504 static int bnxt_re_create_shadow_gsi(struct bnxt_re_qp *qp,
1505 				     struct bnxt_re_pd *pd)
1506 {
1507 	struct bnxt_re_sqp_entries *sqp_tbl;
1508 	struct bnxt_re_dev *rdev;
1509 	struct bnxt_re_qp *sqp;
1510 	struct bnxt_re_ah *sah;
1511 	int rc = 0;
1512 
1513 	rdev = qp->rdev;
1514 	/* Create a shadow QP to handle the QP1 traffic */
1515 	sqp_tbl = kcalloc(BNXT_RE_MAX_GSI_SQP_ENTRIES, sizeof(*sqp_tbl),
1516 			  GFP_KERNEL);
1517 	if (!sqp_tbl)
1518 		return -ENOMEM;
1519 	rdev->gsi_ctx.sqp_tbl = sqp_tbl;
1520 
1521 	sqp = bnxt_re_create_shadow_qp(pd, &rdev->qplib_res, &qp->qplib_qp);
1522 	if (!sqp) {
1523 		rc = -ENODEV;
1524 		ibdev_err(&rdev->ibdev, "Failed to create Shadow QP for QP1");
1525 		goto out;
1526 	}
1527 	rdev->gsi_ctx.gsi_sqp = sqp;
1528 
1529 	sqp->rcq = qp->rcq;
1530 	sqp->scq = qp->scq;
1531 	sah = bnxt_re_create_shadow_qp_ah(pd, &rdev->qplib_res,
1532 					  &qp->qplib_qp);
1533 	if (!sah) {
1534 		bnxt_qplib_destroy_qp(&rdev->qplib_res,
1535 				      &sqp->qplib_qp);
1536 		rc = -ENODEV;
1537 		ibdev_err(&rdev->ibdev,
1538 			  "Failed to create AH entry for ShadowQP");
1539 		goto out;
1540 	}
1541 	rdev->gsi_ctx.gsi_sah = sah;
1542 
1543 	return 0;
1544 out:
1545 	kfree(sqp_tbl);
1546 	return rc;
1547 }
1548 
bnxt_re_create_gsi_qp(struct bnxt_re_qp * qp,struct bnxt_re_pd * pd,struct ib_qp_init_attr * init_attr)1549 static int bnxt_re_create_gsi_qp(struct bnxt_re_qp *qp, struct bnxt_re_pd *pd,
1550 				 struct ib_qp_init_attr *init_attr)
1551 {
1552 	struct bnxt_re_dev *rdev;
1553 	struct bnxt_qplib_qp *qplqp;
1554 	int rc;
1555 
1556 	rdev = qp->rdev;
1557 	qplqp = &qp->qplib_qp;
1558 
1559 	qplqp->rq_hdr_buf_size = BNXT_QPLIB_MAX_QP1_RQ_HDR_SIZE_V2;
1560 	qplqp->sq_hdr_buf_size = BNXT_QPLIB_MAX_QP1_SQ_HDR_SIZE_V2;
1561 
1562 	rc = bnxt_qplib_create_qp1(&rdev->qplib_res, qplqp);
1563 	if (rc) {
1564 		ibdev_err(&rdev->ibdev, "create HW QP1 failed!");
1565 		goto out;
1566 	}
1567 
1568 	rc = bnxt_re_create_shadow_gsi(qp, pd);
1569 out:
1570 	return rc;
1571 }
1572 
bnxt_re_test_qp_limits(struct bnxt_re_dev * rdev,struct ib_qp_init_attr * init_attr,struct bnxt_qplib_dev_attr * dev_attr)1573 static bool bnxt_re_test_qp_limits(struct bnxt_re_dev *rdev,
1574 				   struct ib_qp_init_attr *init_attr,
1575 				   struct bnxt_qplib_dev_attr *dev_attr)
1576 {
1577 	bool rc = true;
1578 
1579 	if (init_attr->cap.max_send_wr > dev_attr->max_qp_wqes ||
1580 	    init_attr->cap.max_recv_wr > dev_attr->max_qp_wqes ||
1581 	    init_attr->cap.max_send_sge > dev_attr->max_qp_sges ||
1582 	    init_attr->cap.max_recv_sge > dev_attr->max_qp_sges ||
1583 	    init_attr->cap.max_inline_data > dev_attr->max_inline_data) {
1584 		ibdev_err(&rdev->ibdev,
1585 			  "Create QP failed - max exceeded! 0x%x/0x%x 0x%x/0x%x 0x%x/0x%x 0x%x/0x%x 0x%x/0x%x",
1586 			  init_attr->cap.max_send_wr, dev_attr->max_qp_wqes,
1587 			  init_attr->cap.max_recv_wr, dev_attr->max_qp_wqes,
1588 			  init_attr->cap.max_send_sge, dev_attr->max_qp_sges,
1589 			  init_attr->cap.max_recv_sge, dev_attr->max_qp_sges,
1590 			  init_attr->cap.max_inline_data,
1591 			  dev_attr->max_inline_data);
1592 		rc = false;
1593 	}
1594 	return rc;
1595 }
1596 
bnxt_re_create_qp(struct ib_qp * ib_qp,struct ib_qp_init_attr * qp_init_attr,struct ib_udata * udata)1597 int bnxt_re_create_qp(struct ib_qp *ib_qp, struct ib_qp_init_attr *qp_init_attr,
1598 		      struct ib_udata *udata)
1599 {
1600 	struct bnxt_qplib_dev_attr *dev_attr;
1601 	struct bnxt_re_ucontext *uctx;
1602 	struct bnxt_re_qp_req ureq;
1603 	struct bnxt_re_dev *rdev;
1604 	struct bnxt_re_pd *pd;
1605 	struct bnxt_re_qp *qp;
1606 	struct ib_pd *ib_pd;
1607 	u32 active_qps;
1608 	int rc;
1609 
1610 	ib_pd = ib_qp->pd;
1611 	pd = container_of(ib_pd, struct bnxt_re_pd, ib_pd);
1612 	rdev = pd->rdev;
1613 	dev_attr = rdev->dev_attr;
1614 	qp = container_of(ib_qp, struct bnxt_re_qp, ib_qp);
1615 
1616 	uctx = rdma_udata_to_drv_context(udata, struct bnxt_re_ucontext, ib_uctx);
1617 	if (udata)
1618 		if (ib_copy_from_udata(&ureq, udata,  min(udata->inlen, sizeof(ureq))))
1619 			return -EFAULT;
1620 
1621 	rc = bnxt_re_test_qp_limits(rdev, qp_init_attr, dev_attr);
1622 	if (!rc) {
1623 		rc = -EINVAL;
1624 		goto fail;
1625 	}
1626 
1627 	qp->rdev = rdev;
1628 	rc = bnxt_re_init_qp_attr(qp, pd, qp_init_attr, uctx, &ureq);
1629 	if (rc)
1630 		goto fail;
1631 
1632 	if (qp_init_attr->qp_type == IB_QPT_GSI &&
1633 	    !(bnxt_qplib_is_chip_gen_p5_p7(rdev->chip_ctx))) {
1634 		rc = bnxt_re_create_gsi_qp(qp, pd, qp_init_attr);
1635 		if (rc == -ENODEV)
1636 			goto qp_destroy;
1637 		if (rc)
1638 			goto fail;
1639 	} else {
1640 		rc = bnxt_qplib_create_qp(&rdev->qplib_res, &qp->qplib_qp);
1641 		if (rc) {
1642 			ibdev_err(&rdev->ibdev, "Failed to create HW QP");
1643 			goto free_umem;
1644 		}
1645 		if (udata) {
1646 			struct bnxt_re_qp_resp resp;
1647 
1648 			resp.qpid = qp->qplib_qp.id;
1649 			resp.rsvd = 0;
1650 			rc = ib_copy_to_udata(udata, &resp, sizeof(resp));
1651 			if (rc) {
1652 				ibdev_err(&rdev->ibdev, "Failed to copy QP udata");
1653 				goto qp_destroy;
1654 			}
1655 		}
1656 	}
1657 
1658 	qp->ib_qp.qp_num = qp->qplib_qp.id;
1659 	if (qp_init_attr->qp_type == IB_QPT_GSI)
1660 		rdev->gsi_ctx.gsi_qp = qp;
1661 	spin_lock_init(&qp->sq_lock);
1662 	spin_lock_init(&qp->rq_lock);
1663 	INIT_LIST_HEAD(&qp->list);
1664 	mutex_lock(&rdev->qp_lock);
1665 	list_add_tail(&qp->list, &rdev->qp_list);
1666 	mutex_unlock(&rdev->qp_lock);
1667 	active_qps = atomic_inc_return(&rdev->stats.res.qp_count);
1668 	if (active_qps > rdev->stats.res.qp_watermark)
1669 		rdev->stats.res.qp_watermark = active_qps;
1670 	if (qp_init_attr->qp_type == IB_QPT_RC) {
1671 		active_qps = atomic_inc_return(&rdev->stats.res.rc_qp_count);
1672 		if (active_qps > rdev->stats.res.rc_qp_watermark)
1673 			rdev->stats.res.rc_qp_watermark = active_qps;
1674 	} else if (qp_init_attr->qp_type == IB_QPT_UD) {
1675 		active_qps = atomic_inc_return(&rdev->stats.res.ud_qp_count);
1676 		if (active_qps > rdev->stats.res.ud_qp_watermark)
1677 			rdev->stats.res.ud_qp_watermark = active_qps;
1678 	}
1679 	bnxt_re_debug_add_qpinfo(rdev, qp);
1680 
1681 	return 0;
1682 qp_destroy:
1683 	bnxt_qplib_destroy_qp(&rdev->qplib_res, &qp->qplib_qp);
1684 free_umem:
1685 	ib_umem_release(qp->rumem);
1686 	ib_umem_release(qp->sumem);
1687 fail:
1688 	return rc;
1689 }
1690 
__from_ib_qp_state(enum ib_qp_state state)1691 static u8 __from_ib_qp_state(enum ib_qp_state state)
1692 {
1693 	switch (state) {
1694 	case IB_QPS_RESET:
1695 		return CMDQ_MODIFY_QP_NEW_STATE_RESET;
1696 	case IB_QPS_INIT:
1697 		return CMDQ_MODIFY_QP_NEW_STATE_INIT;
1698 	case IB_QPS_RTR:
1699 		return CMDQ_MODIFY_QP_NEW_STATE_RTR;
1700 	case IB_QPS_RTS:
1701 		return CMDQ_MODIFY_QP_NEW_STATE_RTS;
1702 	case IB_QPS_SQD:
1703 		return CMDQ_MODIFY_QP_NEW_STATE_SQD;
1704 	case IB_QPS_SQE:
1705 		return CMDQ_MODIFY_QP_NEW_STATE_SQE;
1706 	case IB_QPS_ERR:
1707 	default:
1708 		return CMDQ_MODIFY_QP_NEW_STATE_ERR;
1709 	}
1710 }
1711 
__to_ib_qp_state(u8 state)1712 static enum ib_qp_state __to_ib_qp_state(u8 state)
1713 {
1714 	switch (state) {
1715 	case CMDQ_MODIFY_QP_NEW_STATE_RESET:
1716 		return IB_QPS_RESET;
1717 	case CMDQ_MODIFY_QP_NEW_STATE_INIT:
1718 		return IB_QPS_INIT;
1719 	case CMDQ_MODIFY_QP_NEW_STATE_RTR:
1720 		return IB_QPS_RTR;
1721 	case CMDQ_MODIFY_QP_NEW_STATE_RTS:
1722 		return IB_QPS_RTS;
1723 	case CMDQ_MODIFY_QP_NEW_STATE_SQD:
1724 		return IB_QPS_SQD;
1725 	case CMDQ_MODIFY_QP_NEW_STATE_SQE:
1726 		return IB_QPS_SQE;
1727 	case CMDQ_MODIFY_QP_NEW_STATE_ERR:
1728 	default:
1729 		return IB_QPS_ERR;
1730 	}
1731 }
1732 
__from_ib_mtu(enum ib_mtu mtu)1733 static u32 __from_ib_mtu(enum ib_mtu mtu)
1734 {
1735 	switch (mtu) {
1736 	case IB_MTU_256:
1737 		return CMDQ_MODIFY_QP_PATH_MTU_MTU_256;
1738 	case IB_MTU_512:
1739 		return CMDQ_MODIFY_QP_PATH_MTU_MTU_512;
1740 	case IB_MTU_1024:
1741 		return CMDQ_MODIFY_QP_PATH_MTU_MTU_1024;
1742 	case IB_MTU_2048:
1743 		return CMDQ_MODIFY_QP_PATH_MTU_MTU_2048;
1744 	case IB_MTU_4096:
1745 		return CMDQ_MODIFY_QP_PATH_MTU_MTU_4096;
1746 	default:
1747 		return CMDQ_MODIFY_QP_PATH_MTU_MTU_2048;
1748 	}
1749 }
1750 
__to_ib_mtu(u32 mtu)1751 static enum ib_mtu __to_ib_mtu(u32 mtu)
1752 {
1753 	switch (mtu & CREQ_QUERY_QP_RESP_SB_PATH_MTU_MASK) {
1754 	case CMDQ_MODIFY_QP_PATH_MTU_MTU_256:
1755 		return IB_MTU_256;
1756 	case CMDQ_MODIFY_QP_PATH_MTU_MTU_512:
1757 		return IB_MTU_512;
1758 	case CMDQ_MODIFY_QP_PATH_MTU_MTU_1024:
1759 		return IB_MTU_1024;
1760 	case CMDQ_MODIFY_QP_PATH_MTU_MTU_2048:
1761 		return IB_MTU_2048;
1762 	case CMDQ_MODIFY_QP_PATH_MTU_MTU_4096:
1763 		return IB_MTU_4096;
1764 	default:
1765 		return IB_MTU_2048;
1766 	}
1767 }
1768 
1769 /* Shared Receive Queues */
bnxt_re_destroy_srq(struct ib_srq * ib_srq,struct ib_udata * udata)1770 int bnxt_re_destroy_srq(struct ib_srq *ib_srq, struct ib_udata *udata)
1771 {
1772 	struct bnxt_re_srq *srq = container_of(ib_srq, struct bnxt_re_srq,
1773 					       ib_srq);
1774 	struct bnxt_re_dev *rdev = srq->rdev;
1775 	struct bnxt_qplib_srq *qplib_srq = &srq->qplib_srq;
1776 
1777 	if (rdev->chip_ctx->modes.toggle_bits & BNXT_QPLIB_SRQ_TOGGLE_BIT) {
1778 		free_page((unsigned long)srq->uctx_srq_page);
1779 		hash_del(&srq->hash_entry);
1780 	}
1781 	bnxt_qplib_destroy_srq(&rdev->qplib_res, qplib_srq);
1782 	ib_umem_release(srq->umem);
1783 	atomic_dec(&rdev->stats.res.srq_count);
1784 	return 0;
1785 }
1786 
bnxt_re_init_user_srq(struct bnxt_re_dev * rdev,struct bnxt_re_pd * pd,struct bnxt_re_srq * srq,struct ib_udata * udata)1787 static int bnxt_re_init_user_srq(struct bnxt_re_dev *rdev,
1788 				 struct bnxt_re_pd *pd,
1789 				 struct bnxt_re_srq *srq,
1790 				 struct ib_udata *udata)
1791 {
1792 	struct bnxt_re_srq_req ureq;
1793 	struct bnxt_qplib_srq *qplib_srq = &srq->qplib_srq;
1794 	struct ib_umem *umem;
1795 	int bytes = 0;
1796 	struct bnxt_re_ucontext *cntx = rdma_udata_to_drv_context(
1797 		udata, struct bnxt_re_ucontext, ib_uctx);
1798 
1799 	if (ib_copy_from_udata(&ureq, udata, sizeof(ureq)))
1800 		return -EFAULT;
1801 
1802 	bytes = (qplib_srq->max_wqe * qplib_srq->wqe_size);
1803 	bytes = PAGE_ALIGN(bytes);
1804 	umem = ib_umem_get(&rdev->ibdev, ureq.srqva, bytes,
1805 			   IB_ACCESS_LOCAL_WRITE);
1806 	if (IS_ERR(umem))
1807 		return PTR_ERR(umem);
1808 
1809 	srq->umem = umem;
1810 	qplib_srq->sg_info.umem = umem;
1811 	qplib_srq->sg_info.pgsize = PAGE_SIZE;
1812 	qplib_srq->sg_info.pgshft = PAGE_SHIFT;
1813 	qplib_srq->srq_handle = ureq.srq_handle;
1814 	qplib_srq->dpi = &cntx->dpi;
1815 
1816 	return 0;
1817 }
1818 
bnxt_re_create_srq(struct ib_srq * ib_srq,struct ib_srq_init_attr * srq_init_attr,struct ib_udata * udata)1819 int bnxt_re_create_srq(struct ib_srq *ib_srq,
1820 		       struct ib_srq_init_attr *srq_init_attr,
1821 		       struct ib_udata *udata)
1822 {
1823 	struct bnxt_qplib_dev_attr *dev_attr;
1824 	struct bnxt_re_ucontext *uctx;
1825 	struct bnxt_re_dev *rdev;
1826 	struct bnxt_re_srq *srq;
1827 	struct bnxt_re_pd *pd;
1828 	struct ib_pd *ib_pd;
1829 	u32 active_srqs;
1830 	int rc, entries;
1831 
1832 	ib_pd = ib_srq->pd;
1833 	pd = container_of(ib_pd, struct bnxt_re_pd, ib_pd);
1834 	rdev = pd->rdev;
1835 	dev_attr = rdev->dev_attr;
1836 	srq = container_of(ib_srq, struct bnxt_re_srq, ib_srq);
1837 
1838 	if (srq_init_attr->attr.max_wr >= dev_attr->max_srq_wqes) {
1839 		ibdev_err(&rdev->ibdev, "Create CQ failed - max exceeded");
1840 		rc = -EINVAL;
1841 		goto exit;
1842 	}
1843 
1844 	if (srq_init_attr->srq_type != IB_SRQT_BASIC) {
1845 		rc = -EOPNOTSUPP;
1846 		goto exit;
1847 	}
1848 
1849 	uctx = rdma_udata_to_drv_context(udata, struct bnxt_re_ucontext, ib_uctx);
1850 	srq->rdev = rdev;
1851 	srq->qplib_srq.pd = &pd->qplib_pd;
1852 	srq->qplib_srq.dpi = &rdev->dpi_privileged;
1853 	/* Allocate 1 more than what's provided so posting max doesn't
1854 	 * mean empty
1855 	 */
1856 	entries = bnxt_re_init_depth(srq_init_attr->attr.max_wr + 1, uctx);
1857 	if (entries > dev_attr->max_srq_wqes + 1)
1858 		entries = dev_attr->max_srq_wqes + 1;
1859 	srq->qplib_srq.max_wqe = entries;
1860 
1861 	srq->qplib_srq.max_sge = srq_init_attr->attr.max_sge;
1862 	 /* 128 byte wqe size for SRQ . So use max sges */
1863 	srq->qplib_srq.wqe_size = bnxt_re_get_rwqe_size(dev_attr->max_srq_sges);
1864 	srq->qplib_srq.threshold = srq_init_attr->attr.srq_limit;
1865 	srq->srq_limit = srq_init_attr->attr.srq_limit;
1866 	srq->qplib_srq.eventq_hw_ring_id = rdev->nqr->nq[0].ring_id;
1867 	srq->qplib_srq.sg_info.pgsize = PAGE_SIZE;
1868 	srq->qplib_srq.sg_info.pgshft = PAGE_SHIFT;
1869 
1870 	if (udata) {
1871 		rc = bnxt_re_init_user_srq(rdev, pd, srq, udata);
1872 		if (rc)
1873 			goto fail;
1874 	}
1875 
1876 	rc = bnxt_qplib_create_srq(&rdev->qplib_res, &srq->qplib_srq);
1877 	if (rc) {
1878 		ibdev_err(&rdev->ibdev, "Create HW SRQ failed!");
1879 		goto fail;
1880 	}
1881 
1882 	if (udata) {
1883 		struct bnxt_re_srq_resp resp = {};
1884 
1885 		resp.srqid = srq->qplib_srq.id;
1886 		if (rdev->chip_ctx->modes.toggle_bits & BNXT_QPLIB_SRQ_TOGGLE_BIT) {
1887 			hash_add(rdev->srq_hash, &srq->hash_entry, srq->qplib_srq.id);
1888 			srq->uctx_srq_page = (void *)get_zeroed_page(GFP_KERNEL);
1889 			if (!srq->uctx_srq_page) {
1890 				rc = -ENOMEM;
1891 				goto fail;
1892 			}
1893 			resp.comp_mask |= BNXT_RE_SRQ_TOGGLE_PAGE_SUPPORT;
1894 		}
1895 		rc = ib_copy_to_udata(udata, &resp, sizeof(resp));
1896 		if (rc) {
1897 			ibdev_err(&rdev->ibdev, "SRQ copy to udata failed!");
1898 			bnxt_qplib_destroy_srq(&rdev->qplib_res,
1899 					       &srq->qplib_srq);
1900 			goto fail;
1901 		}
1902 	}
1903 	active_srqs = atomic_inc_return(&rdev->stats.res.srq_count);
1904 	if (active_srqs > rdev->stats.res.srq_watermark)
1905 		rdev->stats.res.srq_watermark = active_srqs;
1906 	spin_lock_init(&srq->lock);
1907 
1908 	return 0;
1909 
1910 fail:
1911 	ib_umem_release(srq->umem);
1912 exit:
1913 	return rc;
1914 }
1915 
bnxt_re_modify_srq(struct ib_srq * ib_srq,struct ib_srq_attr * srq_attr,enum ib_srq_attr_mask srq_attr_mask,struct ib_udata * udata)1916 int bnxt_re_modify_srq(struct ib_srq *ib_srq, struct ib_srq_attr *srq_attr,
1917 		       enum ib_srq_attr_mask srq_attr_mask,
1918 		       struct ib_udata *udata)
1919 {
1920 	struct bnxt_re_srq *srq = container_of(ib_srq, struct bnxt_re_srq,
1921 					       ib_srq);
1922 	struct bnxt_re_dev *rdev = srq->rdev;
1923 	int rc;
1924 
1925 	switch (srq_attr_mask) {
1926 	case IB_SRQ_MAX_WR:
1927 		/* SRQ resize is not supported */
1928 		return -EINVAL;
1929 	case IB_SRQ_LIMIT:
1930 		/* Change the SRQ threshold */
1931 		if (srq_attr->srq_limit > srq->qplib_srq.max_wqe)
1932 			return -EINVAL;
1933 
1934 		srq->qplib_srq.threshold = srq_attr->srq_limit;
1935 		rc = bnxt_qplib_modify_srq(&rdev->qplib_res, &srq->qplib_srq);
1936 		if (rc) {
1937 			ibdev_err(&rdev->ibdev, "Modify HW SRQ failed!");
1938 			return rc;
1939 		}
1940 		/* On success, update the shadow */
1941 		srq->srq_limit = srq_attr->srq_limit;
1942 		/* No need to Build and send response back to udata */
1943 		return 0;
1944 	default:
1945 		ibdev_err(&rdev->ibdev,
1946 			  "Unsupported srq_attr_mask 0x%x", srq_attr_mask);
1947 		return -EINVAL;
1948 	}
1949 }
1950 
bnxt_re_query_srq(struct ib_srq * ib_srq,struct ib_srq_attr * srq_attr)1951 int bnxt_re_query_srq(struct ib_srq *ib_srq, struct ib_srq_attr *srq_attr)
1952 {
1953 	struct bnxt_re_srq *srq = container_of(ib_srq, struct bnxt_re_srq,
1954 					       ib_srq);
1955 	struct bnxt_re_srq tsrq;
1956 	struct bnxt_re_dev *rdev = srq->rdev;
1957 	int rc;
1958 
1959 	/* Get live SRQ attr */
1960 	tsrq.qplib_srq.id = srq->qplib_srq.id;
1961 	rc = bnxt_qplib_query_srq(&rdev->qplib_res, &tsrq.qplib_srq);
1962 	if (rc) {
1963 		ibdev_err(&rdev->ibdev, "Query HW SRQ failed!");
1964 		return rc;
1965 	}
1966 	srq_attr->max_wr = srq->qplib_srq.max_wqe;
1967 	srq_attr->max_sge = srq->qplib_srq.max_sge;
1968 	srq_attr->srq_limit = tsrq.qplib_srq.threshold;
1969 
1970 	return 0;
1971 }
1972 
bnxt_re_post_srq_recv(struct ib_srq * ib_srq,const struct ib_recv_wr * wr,const struct ib_recv_wr ** bad_wr)1973 int bnxt_re_post_srq_recv(struct ib_srq *ib_srq, const struct ib_recv_wr *wr,
1974 			  const struct ib_recv_wr **bad_wr)
1975 {
1976 	struct bnxt_re_srq *srq = container_of(ib_srq, struct bnxt_re_srq,
1977 					       ib_srq);
1978 	struct bnxt_qplib_swqe wqe;
1979 	unsigned long flags;
1980 	int rc = 0;
1981 
1982 	spin_lock_irqsave(&srq->lock, flags);
1983 	while (wr) {
1984 		/* Transcribe each ib_recv_wr to qplib_swqe */
1985 		wqe.num_sge = wr->num_sge;
1986 		bnxt_re_build_sgl(wr->sg_list, wqe.sg_list, wr->num_sge);
1987 		wqe.wr_id = wr->wr_id;
1988 		wqe.type = BNXT_QPLIB_SWQE_TYPE_RECV;
1989 
1990 		rc = bnxt_qplib_post_srq_recv(&srq->qplib_srq, &wqe);
1991 		if (rc) {
1992 			*bad_wr = wr;
1993 			break;
1994 		}
1995 		wr = wr->next;
1996 	}
1997 	spin_unlock_irqrestore(&srq->lock, flags);
1998 
1999 	return rc;
2000 }
bnxt_re_modify_shadow_qp(struct bnxt_re_dev * rdev,struct bnxt_re_qp * qp1_qp,int qp_attr_mask)2001 static int bnxt_re_modify_shadow_qp(struct bnxt_re_dev *rdev,
2002 				    struct bnxt_re_qp *qp1_qp,
2003 				    int qp_attr_mask)
2004 {
2005 	struct bnxt_re_qp *qp = rdev->gsi_ctx.gsi_sqp;
2006 	int rc;
2007 
2008 	if (qp_attr_mask & IB_QP_STATE) {
2009 		qp->qplib_qp.modify_flags |= CMDQ_MODIFY_QP_MODIFY_MASK_STATE;
2010 		qp->qplib_qp.state = qp1_qp->qplib_qp.state;
2011 	}
2012 	if (qp_attr_mask & IB_QP_PKEY_INDEX) {
2013 		qp->qplib_qp.modify_flags |= CMDQ_MODIFY_QP_MODIFY_MASK_PKEY;
2014 		qp->qplib_qp.pkey_index = qp1_qp->qplib_qp.pkey_index;
2015 	}
2016 
2017 	if (qp_attr_mask & IB_QP_QKEY) {
2018 		qp->qplib_qp.modify_flags |= CMDQ_MODIFY_QP_MODIFY_MASK_QKEY;
2019 		/* Using a Random  QKEY */
2020 		qp->qplib_qp.qkey = 0x81818181;
2021 	}
2022 	if (qp_attr_mask & IB_QP_SQ_PSN) {
2023 		qp->qplib_qp.modify_flags |= CMDQ_MODIFY_QP_MODIFY_MASK_SQ_PSN;
2024 		qp->qplib_qp.sq.psn = qp1_qp->qplib_qp.sq.psn;
2025 	}
2026 
2027 	rc = bnxt_qplib_modify_qp(&rdev->qplib_res, &qp->qplib_qp);
2028 	if (rc)
2029 		ibdev_err(&rdev->ibdev, "Failed to modify Shadow QP for QP1");
2030 	return rc;
2031 }
2032 
bnxt_re_modify_qp(struct ib_qp * ib_qp,struct ib_qp_attr * qp_attr,int qp_attr_mask,struct ib_udata * udata)2033 int bnxt_re_modify_qp(struct ib_qp *ib_qp, struct ib_qp_attr *qp_attr,
2034 		      int qp_attr_mask, struct ib_udata *udata)
2035 {
2036 	struct bnxt_re_qp *qp = container_of(ib_qp, struct bnxt_re_qp, ib_qp);
2037 	struct bnxt_re_dev *rdev = qp->rdev;
2038 	struct bnxt_qplib_dev_attr *dev_attr = rdev->dev_attr;
2039 	enum ib_qp_state curr_qp_state, new_qp_state;
2040 	int rc, entries;
2041 	unsigned int flags;
2042 	u8 nw_type;
2043 
2044 	if (qp_attr_mask & ~IB_QP_ATTR_STANDARD_BITS)
2045 		return -EOPNOTSUPP;
2046 
2047 	qp->qplib_qp.modify_flags = 0;
2048 	if (qp_attr_mask & IB_QP_STATE) {
2049 		curr_qp_state = __to_ib_qp_state(qp->qplib_qp.cur_qp_state);
2050 		new_qp_state = qp_attr->qp_state;
2051 		if (!ib_modify_qp_is_ok(curr_qp_state, new_qp_state,
2052 					ib_qp->qp_type, qp_attr_mask)) {
2053 			ibdev_err(&rdev->ibdev,
2054 				  "Invalid attribute mask: %#x specified ",
2055 				  qp_attr_mask);
2056 			ibdev_err(&rdev->ibdev,
2057 				  "for qpn: %#x type: %#x",
2058 				  ib_qp->qp_num, ib_qp->qp_type);
2059 			ibdev_err(&rdev->ibdev,
2060 				  "curr_qp_state=0x%x, new_qp_state=0x%x\n",
2061 				  curr_qp_state, new_qp_state);
2062 			return -EINVAL;
2063 		}
2064 		qp->qplib_qp.modify_flags |= CMDQ_MODIFY_QP_MODIFY_MASK_STATE;
2065 		qp->qplib_qp.state = __from_ib_qp_state(qp_attr->qp_state);
2066 
2067 		if (!qp->sumem &&
2068 		    qp->qplib_qp.state == CMDQ_MODIFY_QP_NEW_STATE_ERR) {
2069 			ibdev_dbg(&rdev->ibdev,
2070 				  "Move QP = %p to flush list\n", qp);
2071 			flags = bnxt_re_lock_cqs(qp);
2072 			bnxt_qplib_add_flush_qp(&qp->qplib_qp);
2073 			bnxt_re_unlock_cqs(qp, flags);
2074 		}
2075 		if (!qp->sumem &&
2076 		    qp->qplib_qp.state == CMDQ_MODIFY_QP_NEW_STATE_RESET) {
2077 			ibdev_dbg(&rdev->ibdev,
2078 				  "Move QP = %p out of flush list\n", qp);
2079 			flags = bnxt_re_lock_cqs(qp);
2080 			bnxt_qplib_clean_qp(&qp->qplib_qp);
2081 			bnxt_re_unlock_cqs(qp, flags);
2082 		}
2083 	}
2084 	if (qp_attr_mask & IB_QP_EN_SQD_ASYNC_NOTIFY) {
2085 		qp->qplib_qp.modify_flags |=
2086 				CMDQ_MODIFY_QP_MODIFY_MASK_EN_SQD_ASYNC_NOTIFY;
2087 		qp->qplib_qp.en_sqd_async_notify = true;
2088 	}
2089 	if (qp_attr_mask & IB_QP_ACCESS_FLAGS) {
2090 		qp->qplib_qp.modify_flags |= CMDQ_MODIFY_QP_MODIFY_MASK_ACCESS;
2091 		qp->qplib_qp.access =
2092 			__qp_access_flags_from_ib(qp->qplib_qp.cctx,
2093 						  qp_attr->qp_access_flags);
2094 		/* LOCAL_WRITE access must be set to allow RC receive */
2095 		qp->qplib_qp.access |= CMDQ_MODIFY_QP_ACCESS_LOCAL_WRITE;
2096 	}
2097 	if (qp_attr_mask & IB_QP_PKEY_INDEX) {
2098 		qp->qplib_qp.modify_flags |= CMDQ_MODIFY_QP_MODIFY_MASK_PKEY;
2099 		qp->qplib_qp.pkey_index = qp_attr->pkey_index;
2100 	}
2101 	if (qp_attr_mask & IB_QP_QKEY) {
2102 		qp->qplib_qp.modify_flags |= CMDQ_MODIFY_QP_MODIFY_MASK_QKEY;
2103 		qp->qplib_qp.qkey = qp_attr->qkey;
2104 	}
2105 	if (qp_attr_mask & IB_QP_AV) {
2106 		const struct ib_global_route *grh =
2107 			rdma_ah_read_grh(&qp_attr->ah_attr);
2108 		const struct ib_gid_attr *sgid_attr;
2109 		struct bnxt_re_gid_ctx *ctx;
2110 
2111 		qp->qplib_qp.modify_flags |= CMDQ_MODIFY_QP_MODIFY_MASK_DGID |
2112 				     CMDQ_MODIFY_QP_MODIFY_MASK_FLOW_LABEL |
2113 				     CMDQ_MODIFY_QP_MODIFY_MASK_SGID_INDEX |
2114 				     CMDQ_MODIFY_QP_MODIFY_MASK_HOP_LIMIT |
2115 				     CMDQ_MODIFY_QP_MODIFY_MASK_TRAFFIC_CLASS |
2116 				     CMDQ_MODIFY_QP_MODIFY_MASK_DEST_MAC |
2117 				     CMDQ_MODIFY_QP_MODIFY_MASK_VLAN_ID;
2118 		memcpy(qp->qplib_qp.ah.dgid.data, grh->dgid.raw,
2119 		       sizeof(qp->qplib_qp.ah.dgid.data));
2120 		qp->qplib_qp.ah.flow_label = grh->flow_label;
2121 		sgid_attr = grh->sgid_attr;
2122 		/* Get the HW context of the GID. The reference
2123 		 * of GID table entry is already taken by the caller.
2124 		 */
2125 		ctx = rdma_read_gid_hw_context(sgid_attr);
2126 		qp->qplib_qp.ah.sgid_index = ctx->idx;
2127 		qp->qplib_qp.ah.host_sgid_index = grh->sgid_index;
2128 		qp->qplib_qp.ah.hop_limit = grh->hop_limit;
2129 		qp->qplib_qp.ah.traffic_class = grh->traffic_class >> 2;
2130 		qp->qplib_qp.ah.sl = rdma_ah_get_sl(&qp_attr->ah_attr);
2131 		ether_addr_copy(qp->qplib_qp.ah.dmac,
2132 				qp_attr->ah_attr.roce.dmac);
2133 
2134 		rc = rdma_read_gid_l2_fields(sgid_attr, NULL,
2135 					     &qp->qplib_qp.smac[0]);
2136 		if (rc)
2137 			return rc;
2138 
2139 		nw_type = rdma_gid_attr_network_type(sgid_attr);
2140 		switch (nw_type) {
2141 		case RDMA_NETWORK_IPV4:
2142 			qp->qplib_qp.nw_type =
2143 				CMDQ_MODIFY_QP_NETWORK_TYPE_ROCEV2_IPV4;
2144 			break;
2145 		case RDMA_NETWORK_IPV6:
2146 			qp->qplib_qp.nw_type =
2147 				CMDQ_MODIFY_QP_NETWORK_TYPE_ROCEV2_IPV6;
2148 			break;
2149 		default:
2150 			qp->qplib_qp.nw_type =
2151 				CMDQ_MODIFY_QP_NETWORK_TYPE_ROCEV1;
2152 			break;
2153 		}
2154 	}
2155 
2156 	if (qp_attr->qp_state == IB_QPS_RTR) {
2157 		enum ib_mtu qpmtu;
2158 
2159 		qpmtu = iboe_get_mtu(rdev->netdev->mtu);
2160 		if (qp_attr_mask & IB_QP_PATH_MTU) {
2161 			if (ib_mtu_enum_to_int(qp_attr->path_mtu) >
2162 			    ib_mtu_enum_to_int(qpmtu))
2163 				return -EINVAL;
2164 			qpmtu = qp_attr->path_mtu;
2165 		}
2166 
2167 		qp->qplib_qp.modify_flags |= CMDQ_MODIFY_QP_MODIFY_MASK_PATH_MTU;
2168 		qp->qplib_qp.path_mtu = __from_ib_mtu(qpmtu);
2169 		qp->qplib_qp.mtu = ib_mtu_enum_to_int(qpmtu);
2170 	}
2171 
2172 	if (qp_attr_mask & IB_QP_TIMEOUT) {
2173 		qp->qplib_qp.modify_flags |= CMDQ_MODIFY_QP_MODIFY_MASK_TIMEOUT;
2174 		qp->qplib_qp.timeout = qp_attr->timeout;
2175 	}
2176 	if (qp_attr_mask & IB_QP_RETRY_CNT) {
2177 		qp->qplib_qp.modify_flags |=
2178 				CMDQ_MODIFY_QP_MODIFY_MASK_RETRY_CNT;
2179 		qp->qplib_qp.retry_cnt = qp_attr->retry_cnt;
2180 	}
2181 	if (qp_attr_mask & IB_QP_RNR_RETRY) {
2182 		qp->qplib_qp.modify_flags |=
2183 				CMDQ_MODIFY_QP_MODIFY_MASK_RNR_RETRY;
2184 		qp->qplib_qp.rnr_retry = qp_attr->rnr_retry;
2185 	}
2186 	if (qp_attr_mask & IB_QP_MIN_RNR_TIMER) {
2187 		qp->qplib_qp.modify_flags |=
2188 				CMDQ_MODIFY_QP_MODIFY_MASK_MIN_RNR_TIMER;
2189 		qp->qplib_qp.min_rnr_timer = qp_attr->min_rnr_timer;
2190 	}
2191 	if (qp_attr_mask & IB_QP_RQ_PSN) {
2192 		qp->qplib_qp.modify_flags |= CMDQ_MODIFY_QP_MODIFY_MASK_RQ_PSN;
2193 		qp->qplib_qp.rq.psn = qp_attr->rq_psn;
2194 	}
2195 	if (qp_attr_mask & IB_QP_MAX_QP_RD_ATOMIC) {
2196 		qp->qplib_qp.modify_flags |=
2197 				CMDQ_MODIFY_QP_MODIFY_MASK_MAX_RD_ATOMIC;
2198 		/* Cap the max_rd_atomic to device max */
2199 		qp->qplib_qp.max_rd_atomic = min_t(u32, qp_attr->max_rd_atomic,
2200 						   dev_attr->max_qp_rd_atom);
2201 	}
2202 	if (qp_attr_mask & IB_QP_SQ_PSN) {
2203 		qp->qplib_qp.modify_flags |= CMDQ_MODIFY_QP_MODIFY_MASK_SQ_PSN;
2204 		qp->qplib_qp.sq.psn = qp_attr->sq_psn;
2205 	}
2206 	if (qp_attr_mask & IB_QP_MAX_DEST_RD_ATOMIC) {
2207 		if (qp_attr->max_dest_rd_atomic >
2208 		    dev_attr->max_qp_init_rd_atom) {
2209 			ibdev_err(&rdev->ibdev,
2210 				  "max_dest_rd_atomic requested%d is > dev_max%d",
2211 				  qp_attr->max_dest_rd_atomic,
2212 				  dev_attr->max_qp_init_rd_atom);
2213 			return -EINVAL;
2214 		}
2215 
2216 		qp->qplib_qp.modify_flags |=
2217 				CMDQ_MODIFY_QP_MODIFY_MASK_MAX_DEST_RD_ATOMIC;
2218 		qp->qplib_qp.max_dest_rd_atomic = qp_attr->max_dest_rd_atomic;
2219 	}
2220 	if (qp_attr_mask & IB_QP_CAP) {
2221 		struct bnxt_re_ucontext *uctx =
2222 			rdma_udata_to_drv_context(udata, struct bnxt_re_ucontext, ib_uctx);
2223 
2224 		qp->qplib_qp.modify_flags |=
2225 				CMDQ_MODIFY_QP_MODIFY_MASK_SQ_SIZE |
2226 				CMDQ_MODIFY_QP_MODIFY_MASK_RQ_SIZE |
2227 				CMDQ_MODIFY_QP_MODIFY_MASK_SQ_SGE |
2228 				CMDQ_MODIFY_QP_MODIFY_MASK_RQ_SGE |
2229 				CMDQ_MODIFY_QP_MODIFY_MASK_MAX_INLINE_DATA;
2230 		if ((qp_attr->cap.max_send_wr >= dev_attr->max_qp_wqes) ||
2231 		    (qp_attr->cap.max_recv_wr >= dev_attr->max_qp_wqes) ||
2232 		    (qp_attr->cap.max_send_sge >= dev_attr->max_qp_sges) ||
2233 		    (qp_attr->cap.max_recv_sge >= dev_attr->max_qp_sges) ||
2234 		    (qp_attr->cap.max_inline_data >=
2235 						dev_attr->max_inline_data)) {
2236 			ibdev_err(&rdev->ibdev,
2237 				  "Create QP failed - max exceeded");
2238 			return -EINVAL;
2239 		}
2240 		entries = bnxt_re_init_depth(qp_attr->cap.max_send_wr, uctx);
2241 		qp->qplib_qp.sq.max_wqe = min_t(u32, entries,
2242 						dev_attr->max_qp_wqes + 1);
2243 		qp->qplib_qp.sq.q_full_delta = qp->qplib_qp.sq.max_wqe -
2244 						qp_attr->cap.max_send_wr;
2245 		/*
2246 		 * Reserving one slot for Phantom WQE. Some application can
2247 		 * post one extra entry in this case. Allowing this to avoid
2248 		 * unexpected Queue full condition
2249 		 */
2250 		qp->qplib_qp.sq.q_full_delta -= 1;
2251 		qp->qplib_qp.sq.max_sge = qp_attr->cap.max_send_sge;
2252 		if (qp->qplib_qp.rq.max_wqe) {
2253 			entries = bnxt_re_init_depth(qp_attr->cap.max_recv_wr, uctx);
2254 			qp->qplib_qp.rq.max_wqe =
2255 				min_t(u32, entries, dev_attr->max_qp_wqes + 1);
2256 			qp->qplib_qp.rq.max_sw_wqe = qp->qplib_qp.rq.max_wqe;
2257 			qp->qplib_qp.rq.q_full_delta = qp->qplib_qp.rq.max_wqe -
2258 						       qp_attr->cap.max_recv_wr;
2259 			qp->qplib_qp.rq.max_sge = qp_attr->cap.max_recv_sge;
2260 		} else {
2261 			/* SRQ was used prior, just ignore the RQ caps */
2262 		}
2263 	}
2264 	if (qp_attr_mask & IB_QP_DEST_QPN) {
2265 		qp->qplib_qp.modify_flags |=
2266 				CMDQ_MODIFY_QP_MODIFY_MASK_DEST_QP_ID;
2267 		qp->qplib_qp.dest_qpn = qp_attr->dest_qp_num;
2268 	}
2269 	rc = bnxt_qplib_modify_qp(&rdev->qplib_res, &qp->qplib_qp);
2270 	if (rc) {
2271 		ibdev_err(&rdev->ibdev, "Failed to modify HW QP");
2272 		return rc;
2273 	}
2274 	if (ib_qp->qp_type == IB_QPT_GSI && rdev->gsi_ctx.gsi_sqp)
2275 		rc = bnxt_re_modify_shadow_qp(rdev, qp, qp_attr_mask);
2276 	return rc;
2277 }
2278 
bnxt_re_query_qp(struct ib_qp * ib_qp,struct ib_qp_attr * qp_attr,int qp_attr_mask,struct ib_qp_init_attr * qp_init_attr)2279 int bnxt_re_query_qp(struct ib_qp *ib_qp, struct ib_qp_attr *qp_attr,
2280 		     int qp_attr_mask, struct ib_qp_init_attr *qp_init_attr)
2281 {
2282 	struct bnxt_re_qp *qp = container_of(ib_qp, struct bnxt_re_qp, ib_qp);
2283 	struct bnxt_re_dev *rdev = qp->rdev;
2284 	struct bnxt_qplib_qp *qplib_qp;
2285 	int rc;
2286 
2287 	qplib_qp = kzalloc(sizeof(*qplib_qp), GFP_KERNEL);
2288 	if (!qplib_qp)
2289 		return -ENOMEM;
2290 
2291 	qplib_qp->id = qp->qplib_qp.id;
2292 	qplib_qp->ah.host_sgid_index = qp->qplib_qp.ah.host_sgid_index;
2293 
2294 	rc = bnxt_qplib_query_qp(&rdev->qplib_res, qplib_qp);
2295 	if (rc) {
2296 		ibdev_err(&rdev->ibdev, "Failed to query HW QP");
2297 		goto out;
2298 	}
2299 	qp_attr->qp_state = __to_ib_qp_state(qplib_qp->state);
2300 	qp_attr->cur_qp_state = __to_ib_qp_state(qplib_qp->cur_qp_state);
2301 	qp_attr->en_sqd_async_notify = qplib_qp->en_sqd_async_notify ? 1 : 0;
2302 	qp_attr->qp_access_flags = __qp_access_flags_to_ib(qp->qplib_qp.cctx,
2303 							   qplib_qp->access);
2304 	qp_attr->pkey_index = qplib_qp->pkey_index;
2305 	qp_attr->qkey = qplib_qp->qkey;
2306 	qp_attr->ah_attr.type = RDMA_AH_ATTR_TYPE_ROCE;
2307 	rdma_ah_set_grh(&qp_attr->ah_attr, NULL, qplib_qp->ah.flow_label,
2308 			qplib_qp->ah.host_sgid_index,
2309 			qplib_qp->ah.hop_limit,
2310 			qplib_qp->ah.traffic_class);
2311 	rdma_ah_set_dgid_raw(&qp_attr->ah_attr, qplib_qp->ah.dgid.data);
2312 	rdma_ah_set_sl(&qp_attr->ah_attr, qplib_qp->ah.sl);
2313 	ether_addr_copy(qp_attr->ah_attr.roce.dmac, qplib_qp->ah.dmac);
2314 	qp_attr->path_mtu = __to_ib_mtu(qplib_qp->path_mtu);
2315 	qp_attr->timeout = qplib_qp->timeout;
2316 	qp_attr->retry_cnt = qplib_qp->retry_cnt;
2317 	qp_attr->rnr_retry = qplib_qp->rnr_retry;
2318 	qp_attr->min_rnr_timer = qplib_qp->min_rnr_timer;
2319 	qp_attr->port_num = __to_ib_port_num(qplib_qp->port_id);
2320 	qp_attr->rq_psn = qplib_qp->rq.psn;
2321 	qp_attr->max_rd_atomic = qplib_qp->max_rd_atomic;
2322 	qp_attr->sq_psn = qplib_qp->sq.psn;
2323 	qp_attr->max_dest_rd_atomic = qplib_qp->max_dest_rd_atomic;
2324 	qp_init_attr->sq_sig_type = qplib_qp->sig_type ? IB_SIGNAL_ALL_WR :
2325 							 IB_SIGNAL_REQ_WR;
2326 	qp_attr->dest_qp_num = qplib_qp->dest_qpn;
2327 
2328 	qp_attr->cap.max_send_wr = qp->qplib_qp.sq.max_wqe;
2329 	qp_attr->cap.max_send_sge = qp->qplib_qp.sq.max_sge;
2330 	qp_attr->cap.max_recv_wr = qp->qplib_qp.rq.max_wqe;
2331 	qp_attr->cap.max_recv_sge = qp->qplib_qp.rq.max_sge;
2332 	qp_attr->cap.max_inline_data = qp->qplib_qp.max_inline_data;
2333 	qp_init_attr->cap = qp_attr->cap;
2334 
2335 out:
2336 	kfree(qplib_qp);
2337 	return rc;
2338 }
2339 
2340 /* Routine for sending QP1 packets for RoCE V1 an V2
2341  */
bnxt_re_build_qp1_send_v2(struct bnxt_re_qp * qp,const struct ib_send_wr * wr,struct bnxt_qplib_swqe * wqe,int payload_size)2342 static int bnxt_re_build_qp1_send_v2(struct bnxt_re_qp *qp,
2343 				     const struct ib_send_wr *wr,
2344 				     struct bnxt_qplib_swqe *wqe,
2345 				     int payload_size)
2346 {
2347 	struct bnxt_re_ah *ah = container_of(ud_wr(wr)->ah, struct bnxt_re_ah,
2348 					     ib_ah);
2349 	struct bnxt_qplib_ah *qplib_ah = &ah->qplib_ah;
2350 	const struct ib_gid_attr *sgid_attr = ah->ib_ah.sgid_attr;
2351 	struct bnxt_qplib_sge sge;
2352 	u8 nw_type;
2353 	u16 ether_type;
2354 	union ib_gid dgid;
2355 	bool is_eth = false;
2356 	bool is_vlan = false;
2357 	bool is_grh = false;
2358 	bool is_udp = false;
2359 	u8 ip_version = 0;
2360 	u16 vlan_id = 0xFFFF;
2361 	void *buf;
2362 	int i, rc;
2363 
2364 	memset(&qp->qp1_hdr, 0, sizeof(qp->qp1_hdr));
2365 
2366 	rc = rdma_read_gid_l2_fields(sgid_attr, &vlan_id, NULL);
2367 	if (rc)
2368 		return rc;
2369 
2370 	/* Get network header type for this GID */
2371 	nw_type = rdma_gid_attr_network_type(sgid_attr);
2372 	switch (nw_type) {
2373 	case RDMA_NETWORK_IPV4:
2374 		nw_type = BNXT_RE_ROCEV2_IPV4_PACKET;
2375 		break;
2376 	case RDMA_NETWORK_IPV6:
2377 		nw_type = BNXT_RE_ROCEV2_IPV6_PACKET;
2378 		break;
2379 	default:
2380 		nw_type = BNXT_RE_ROCE_V1_PACKET;
2381 		break;
2382 	}
2383 	memcpy(&dgid.raw, &qplib_ah->dgid, 16);
2384 	is_udp = sgid_attr->gid_type == IB_GID_TYPE_ROCE_UDP_ENCAP;
2385 	if (is_udp) {
2386 		if (ipv6_addr_v4mapped((struct in6_addr *)&sgid_attr->gid)) {
2387 			ip_version = 4;
2388 			ether_type = ETH_P_IP;
2389 		} else {
2390 			ip_version = 6;
2391 			ether_type = ETH_P_IPV6;
2392 		}
2393 		is_grh = false;
2394 	} else {
2395 		ether_type = ETH_P_IBOE;
2396 		is_grh = true;
2397 	}
2398 
2399 	is_eth = true;
2400 	is_vlan = vlan_id && (vlan_id < 0x1000);
2401 
2402 	ib_ud_header_init(payload_size, !is_eth, is_eth, is_vlan, is_grh,
2403 			  ip_version, is_udp, 0, &qp->qp1_hdr);
2404 
2405 	/* ETH */
2406 	ether_addr_copy(qp->qp1_hdr.eth.dmac_h, ah->qplib_ah.dmac);
2407 	ether_addr_copy(qp->qp1_hdr.eth.smac_h, qp->qplib_qp.smac);
2408 
2409 	/* For vlan, check the sgid for vlan existence */
2410 
2411 	if (!is_vlan) {
2412 		qp->qp1_hdr.eth.type = cpu_to_be16(ether_type);
2413 	} else {
2414 		qp->qp1_hdr.vlan.type = cpu_to_be16(ether_type);
2415 		qp->qp1_hdr.vlan.tag = cpu_to_be16(vlan_id);
2416 	}
2417 
2418 	if (is_grh || (ip_version == 6)) {
2419 		memcpy(qp->qp1_hdr.grh.source_gid.raw, sgid_attr->gid.raw,
2420 		       sizeof(sgid_attr->gid));
2421 		memcpy(qp->qp1_hdr.grh.destination_gid.raw, qplib_ah->dgid.data,
2422 		       sizeof(sgid_attr->gid));
2423 		qp->qp1_hdr.grh.hop_limit     = qplib_ah->hop_limit;
2424 	}
2425 
2426 	if (ip_version == 4) {
2427 		qp->qp1_hdr.ip4.tos = 0;
2428 		qp->qp1_hdr.ip4.id = 0;
2429 		qp->qp1_hdr.ip4.frag_off = htons(IP_DF);
2430 		qp->qp1_hdr.ip4.ttl = qplib_ah->hop_limit;
2431 
2432 		memcpy(&qp->qp1_hdr.ip4.saddr, sgid_attr->gid.raw + 12, 4);
2433 		memcpy(&qp->qp1_hdr.ip4.daddr, qplib_ah->dgid.data + 12, 4);
2434 		qp->qp1_hdr.ip4.check = ib_ud_ip4_csum(&qp->qp1_hdr);
2435 	}
2436 
2437 	if (is_udp) {
2438 		qp->qp1_hdr.udp.dport = htons(ROCE_V2_UDP_DPORT);
2439 		qp->qp1_hdr.udp.sport = htons(0x8CD1);
2440 		qp->qp1_hdr.udp.csum = 0;
2441 	}
2442 
2443 	/* BTH */
2444 	if (wr->opcode == IB_WR_SEND_WITH_IMM) {
2445 		qp->qp1_hdr.bth.opcode = IB_OPCODE_UD_SEND_ONLY_WITH_IMMEDIATE;
2446 		qp->qp1_hdr.immediate_present = 1;
2447 	} else {
2448 		qp->qp1_hdr.bth.opcode = IB_OPCODE_UD_SEND_ONLY;
2449 	}
2450 	if (wr->send_flags & IB_SEND_SOLICITED)
2451 		qp->qp1_hdr.bth.solicited_event = 1;
2452 	/* pad_count */
2453 	qp->qp1_hdr.bth.pad_count = (4 - payload_size) & 3;
2454 
2455 	/* P_key for QP1 is for all members */
2456 	qp->qp1_hdr.bth.pkey = cpu_to_be16(0xFFFF);
2457 	qp->qp1_hdr.bth.destination_qpn = IB_QP1;
2458 	qp->qp1_hdr.bth.ack_req = 0;
2459 	qp->send_psn++;
2460 	qp->send_psn &= BTH_PSN_MASK;
2461 	qp->qp1_hdr.bth.psn = cpu_to_be32(qp->send_psn);
2462 	/* DETH */
2463 	/* Use the priviledged Q_Key for QP1 */
2464 	qp->qp1_hdr.deth.qkey = cpu_to_be32(IB_QP1_QKEY);
2465 	qp->qp1_hdr.deth.source_qpn = IB_QP1;
2466 
2467 	/* Pack the QP1 to the transmit buffer */
2468 	buf = bnxt_qplib_get_qp1_sq_buf(&qp->qplib_qp, &sge);
2469 	if (buf) {
2470 		ib_ud_header_pack(&qp->qp1_hdr, buf);
2471 		for (i = wqe->num_sge; i; i--) {
2472 			wqe->sg_list[i].addr = wqe->sg_list[i - 1].addr;
2473 			wqe->sg_list[i].lkey = wqe->sg_list[i - 1].lkey;
2474 			wqe->sg_list[i].size = wqe->sg_list[i - 1].size;
2475 		}
2476 
2477 		/*
2478 		 * Max Header buf size for IPV6 RoCE V2 is 86,
2479 		 * which is same as the QP1 SQ header buffer.
2480 		 * Header buf size for IPV4 RoCE V2 can be 66.
2481 		 * ETH(14) + VLAN(4)+ IP(20) + UDP (8) + BTH(20).
2482 		 * Subtract 20 bytes from QP1 SQ header buf size
2483 		 */
2484 		if (is_udp && ip_version == 4)
2485 			sge.size -= 20;
2486 		/*
2487 		 * Max Header buf size for RoCE V1 is 78.
2488 		 * ETH(14) + VLAN(4) + GRH(40) + BTH(20).
2489 		 * Subtract 8 bytes from QP1 SQ header buf size
2490 		 */
2491 		if (!is_udp)
2492 			sge.size -= 8;
2493 
2494 		/* Subtract 4 bytes for non vlan packets */
2495 		if (!is_vlan)
2496 			sge.size -= 4;
2497 
2498 		wqe->sg_list[0].addr = sge.addr;
2499 		wqe->sg_list[0].lkey = sge.lkey;
2500 		wqe->sg_list[0].size = sge.size;
2501 		wqe->num_sge++;
2502 
2503 	} else {
2504 		ibdev_err(&qp->rdev->ibdev, "QP1 buffer is empty!");
2505 		rc = -ENOMEM;
2506 	}
2507 	return rc;
2508 }
2509 
2510 /* For the MAD layer, it only provides the recv SGE the size of
2511  * ib_grh + MAD datagram.  No Ethernet headers, Ethertype, BTH, DETH,
2512  * nor RoCE iCRC.  The Cu+ solution must provide buffer for the entire
2513  * receive packet (334 bytes) with no VLAN and then copy the GRH
2514  * and the MAD datagram out to the provided SGE.
2515  */
bnxt_re_build_qp1_shadow_qp_recv(struct bnxt_re_qp * qp,const struct ib_recv_wr * wr,struct bnxt_qplib_swqe * wqe,int payload_size)2516 static int bnxt_re_build_qp1_shadow_qp_recv(struct bnxt_re_qp *qp,
2517 					    const struct ib_recv_wr *wr,
2518 					    struct bnxt_qplib_swqe *wqe,
2519 					    int payload_size)
2520 {
2521 	struct bnxt_re_sqp_entries *sqp_entry;
2522 	struct bnxt_qplib_sge ref, sge;
2523 	struct bnxt_re_dev *rdev;
2524 	u32 rq_prod_index;
2525 
2526 	rdev = qp->rdev;
2527 
2528 	rq_prod_index = bnxt_qplib_get_rq_prod_index(&qp->qplib_qp);
2529 
2530 	if (!bnxt_qplib_get_qp1_rq_buf(&qp->qplib_qp, &sge))
2531 		return -ENOMEM;
2532 
2533 	/* Create 1 SGE to receive the entire
2534 	 * ethernet packet
2535 	 */
2536 	/* Save the reference from ULP */
2537 	ref.addr = wqe->sg_list[0].addr;
2538 	ref.lkey = wqe->sg_list[0].lkey;
2539 	ref.size = wqe->sg_list[0].size;
2540 
2541 	sqp_entry = &rdev->gsi_ctx.sqp_tbl[rq_prod_index];
2542 
2543 	/* SGE 1 */
2544 	wqe->sg_list[0].addr = sge.addr;
2545 	wqe->sg_list[0].lkey = sge.lkey;
2546 	wqe->sg_list[0].size = BNXT_QPLIB_MAX_QP1_RQ_HDR_SIZE_V2;
2547 	sge.size -= wqe->sg_list[0].size;
2548 
2549 	sqp_entry->sge.addr = ref.addr;
2550 	sqp_entry->sge.lkey = ref.lkey;
2551 	sqp_entry->sge.size = ref.size;
2552 	/* Store the wrid for reporting completion */
2553 	sqp_entry->wrid = wqe->wr_id;
2554 	/* change the wqe->wrid to table index */
2555 	wqe->wr_id = rq_prod_index;
2556 	return 0;
2557 }
2558 
is_ud_qp(struct bnxt_re_qp * qp)2559 static int is_ud_qp(struct bnxt_re_qp *qp)
2560 {
2561 	return (qp->qplib_qp.type == CMDQ_CREATE_QP_TYPE_UD ||
2562 		qp->qplib_qp.type == CMDQ_CREATE_QP_TYPE_GSI);
2563 }
2564 
bnxt_re_build_send_wqe(struct bnxt_re_qp * qp,const struct ib_send_wr * wr,struct bnxt_qplib_swqe * wqe)2565 static int bnxt_re_build_send_wqe(struct bnxt_re_qp *qp,
2566 				  const struct ib_send_wr *wr,
2567 				  struct bnxt_qplib_swqe *wqe)
2568 {
2569 	struct bnxt_re_ah *ah = NULL;
2570 
2571 	if (is_ud_qp(qp)) {
2572 		ah = container_of(ud_wr(wr)->ah, struct bnxt_re_ah, ib_ah);
2573 		wqe->send.q_key = ud_wr(wr)->remote_qkey;
2574 		wqe->send.dst_qp = ud_wr(wr)->remote_qpn;
2575 		wqe->send.avid = ah->qplib_ah.id;
2576 	}
2577 	switch (wr->opcode) {
2578 	case IB_WR_SEND:
2579 		wqe->type = BNXT_QPLIB_SWQE_TYPE_SEND;
2580 		break;
2581 	case IB_WR_SEND_WITH_IMM:
2582 		wqe->type = BNXT_QPLIB_SWQE_TYPE_SEND_WITH_IMM;
2583 		wqe->send.imm_data = be32_to_cpu(wr->ex.imm_data);
2584 		break;
2585 	case IB_WR_SEND_WITH_INV:
2586 		wqe->type = BNXT_QPLIB_SWQE_TYPE_SEND_WITH_INV;
2587 		wqe->send.inv_key = wr->ex.invalidate_rkey;
2588 		break;
2589 	default:
2590 		return -EINVAL;
2591 	}
2592 	if (wr->send_flags & IB_SEND_SIGNALED)
2593 		wqe->flags |= BNXT_QPLIB_SWQE_FLAGS_SIGNAL_COMP;
2594 	if (wr->send_flags & IB_SEND_FENCE)
2595 		wqe->flags |= BNXT_QPLIB_SWQE_FLAGS_UC_FENCE;
2596 	if (wr->send_flags & IB_SEND_SOLICITED)
2597 		wqe->flags |= BNXT_QPLIB_SWQE_FLAGS_SOLICIT_EVENT;
2598 	if (wr->send_flags & IB_SEND_INLINE)
2599 		wqe->flags |= BNXT_QPLIB_SWQE_FLAGS_INLINE;
2600 
2601 	return 0;
2602 }
2603 
bnxt_re_build_rdma_wqe(const struct ib_send_wr * wr,struct bnxt_qplib_swqe * wqe)2604 static int bnxt_re_build_rdma_wqe(const struct ib_send_wr *wr,
2605 				  struct bnxt_qplib_swqe *wqe)
2606 {
2607 	switch (wr->opcode) {
2608 	case IB_WR_RDMA_WRITE:
2609 		wqe->type = BNXT_QPLIB_SWQE_TYPE_RDMA_WRITE;
2610 		break;
2611 	case IB_WR_RDMA_WRITE_WITH_IMM:
2612 		wqe->type = BNXT_QPLIB_SWQE_TYPE_RDMA_WRITE_WITH_IMM;
2613 		wqe->rdma.imm_data = be32_to_cpu(wr->ex.imm_data);
2614 		break;
2615 	case IB_WR_RDMA_READ:
2616 		wqe->type = BNXT_QPLIB_SWQE_TYPE_RDMA_READ;
2617 		wqe->rdma.inv_key = wr->ex.invalidate_rkey;
2618 		break;
2619 	default:
2620 		return -EINVAL;
2621 	}
2622 	wqe->rdma.remote_va = rdma_wr(wr)->remote_addr;
2623 	wqe->rdma.r_key = rdma_wr(wr)->rkey;
2624 	if (wr->send_flags & IB_SEND_SIGNALED)
2625 		wqe->flags |= BNXT_QPLIB_SWQE_FLAGS_SIGNAL_COMP;
2626 	if (wr->send_flags & IB_SEND_FENCE)
2627 		wqe->flags |= BNXT_QPLIB_SWQE_FLAGS_UC_FENCE;
2628 	if (wr->send_flags & IB_SEND_SOLICITED)
2629 		wqe->flags |= BNXT_QPLIB_SWQE_FLAGS_SOLICIT_EVENT;
2630 	if (wr->send_flags & IB_SEND_INLINE)
2631 		wqe->flags |= BNXT_QPLIB_SWQE_FLAGS_INLINE;
2632 
2633 	return 0;
2634 }
2635 
bnxt_re_build_atomic_wqe(const struct ib_send_wr * wr,struct bnxt_qplib_swqe * wqe)2636 static int bnxt_re_build_atomic_wqe(const struct ib_send_wr *wr,
2637 				    struct bnxt_qplib_swqe *wqe)
2638 {
2639 	switch (wr->opcode) {
2640 	case IB_WR_ATOMIC_CMP_AND_SWP:
2641 		wqe->type = BNXT_QPLIB_SWQE_TYPE_ATOMIC_CMP_AND_SWP;
2642 		wqe->atomic.cmp_data = atomic_wr(wr)->compare_add;
2643 		wqe->atomic.swap_data = atomic_wr(wr)->swap;
2644 		break;
2645 	case IB_WR_ATOMIC_FETCH_AND_ADD:
2646 		wqe->type = BNXT_QPLIB_SWQE_TYPE_ATOMIC_FETCH_AND_ADD;
2647 		wqe->atomic.cmp_data = atomic_wr(wr)->compare_add;
2648 		break;
2649 	default:
2650 		return -EINVAL;
2651 	}
2652 	wqe->atomic.remote_va = atomic_wr(wr)->remote_addr;
2653 	wqe->atomic.r_key = atomic_wr(wr)->rkey;
2654 	if (wr->send_flags & IB_SEND_SIGNALED)
2655 		wqe->flags |= BNXT_QPLIB_SWQE_FLAGS_SIGNAL_COMP;
2656 	if (wr->send_flags & IB_SEND_FENCE)
2657 		wqe->flags |= BNXT_QPLIB_SWQE_FLAGS_UC_FENCE;
2658 	if (wr->send_flags & IB_SEND_SOLICITED)
2659 		wqe->flags |= BNXT_QPLIB_SWQE_FLAGS_SOLICIT_EVENT;
2660 	return 0;
2661 }
2662 
bnxt_re_build_inv_wqe(const struct ib_send_wr * wr,struct bnxt_qplib_swqe * wqe)2663 static int bnxt_re_build_inv_wqe(const struct ib_send_wr *wr,
2664 				 struct bnxt_qplib_swqe *wqe)
2665 {
2666 	wqe->type = BNXT_QPLIB_SWQE_TYPE_LOCAL_INV;
2667 	wqe->local_inv.inv_l_key = wr->ex.invalidate_rkey;
2668 
2669 	if (wr->send_flags & IB_SEND_SIGNALED)
2670 		wqe->flags |= BNXT_QPLIB_SWQE_FLAGS_SIGNAL_COMP;
2671 	if (wr->send_flags & IB_SEND_SOLICITED)
2672 		wqe->flags |= BNXT_QPLIB_SWQE_FLAGS_SOLICIT_EVENT;
2673 
2674 	return 0;
2675 }
2676 
bnxt_re_build_reg_wqe(const struct ib_reg_wr * wr,struct bnxt_qplib_swqe * wqe)2677 static int bnxt_re_build_reg_wqe(const struct ib_reg_wr *wr,
2678 				 struct bnxt_qplib_swqe *wqe)
2679 {
2680 	struct bnxt_re_mr *mr = container_of(wr->mr, struct bnxt_re_mr, ib_mr);
2681 	struct bnxt_qplib_frpl *qplib_frpl = &mr->qplib_frpl;
2682 	int access = wr->access;
2683 
2684 	wqe->frmr.pbl_ptr = (__le64 *)qplib_frpl->hwq.pbl_ptr[0];
2685 	wqe->frmr.pbl_dma_ptr = qplib_frpl->hwq.pbl_dma_ptr[0];
2686 	wqe->frmr.page_list = mr->pages;
2687 	wqe->frmr.page_list_len = mr->npages;
2688 	wqe->frmr.levels = qplib_frpl->hwq.level;
2689 	wqe->type = BNXT_QPLIB_SWQE_TYPE_REG_MR;
2690 
2691 	if (wr->wr.send_flags & IB_SEND_SIGNALED)
2692 		wqe->flags |= BNXT_QPLIB_SWQE_FLAGS_SIGNAL_COMP;
2693 
2694 	if (access & IB_ACCESS_LOCAL_WRITE)
2695 		wqe->frmr.access_cntl |= SQ_FR_PMR_ACCESS_CNTL_LOCAL_WRITE;
2696 	if (access & IB_ACCESS_REMOTE_READ)
2697 		wqe->frmr.access_cntl |= SQ_FR_PMR_ACCESS_CNTL_REMOTE_READ;
2698 	if (access & IB_ACCESS_REMOTE_WRITE)
2699 		wqe->frmr.access_cntl |= SQ_FR_PMR_ACCESS_CNTL_REMOTE_WRITE;
2700 	if (access & IB_ACCESS_REMOTE_ATOMIC)
2701 		wqe->frmr.access_cntl |= SQ_FR_PMR_ACCESS_CNTL_REMOTE_ATOMIC;
2702 	if (access & IB_ACCESS_MW_BIND)
2703 		wqe->frmr.access_cntl |= SQ_FR_PMR_ACCESS_CNTL_WINDOW_BIND;
2704 
2705 	wqe->frmr.l_key = wr->key;
2706 	wqe->frmr.length = wr->mr->length;
2707 	wqe->frmr.pbl_pg_sz_log = ilog2(PAGE_SIZE >> PAGE_SHIFT_4K);
2708 	wqe->frmr.pg_sz_log = ilog2(wr->mr->page_size >> PAGE_SHIFT_4K);
2709 	wqe->frmr.va = wr->mr->iova;
2710 	return 0;
2711 }
2712 
bnxt_re_copy_inline_data(struct bnxt_re_dev * rdev,const struct ib_send_wr * wr,struct bnxt_qplib_swqe * wqe)2713 static int bnxt_re_copy_inline_data(struct bnxt_re_dev *rdev,
2714 				    const struct ib_send_wr *wr,
2715 				    struct bnxt_qplib_swqe *wqe)
2716 {
2717 	/*  Copy the inline data to the data  field */
2718 	u8 *in_data;
2719 	u32 i, sge_len;
2720 	void *sge_addr;
2721 
2722 	in_data = wqe->inline_data;
2723 	for (i = 0; i < wr->num_sge; i++) {
2724 		sge_addr = (void *)(unsigned long)
2725 				wr->sg_list[i].addr;
2726 		sge_len = wr->sg_list[i].length;
2727 
2728 		if ((sge_len + wqe->inline_len) >
2729 		    BNXT_QPLIB_SWQE_MAX_INLINE_LENGTH) {
2730 			ibdev_err(&rdev->ibdev,
2731 				  "Inline data size requested > supported value");
2732 			return -EINVAL;
2733 		}
2734 		sge_len = wr->sg_list[i].length;
2735 
2736 		memcpy(in_data, sge_addr, sge_len);
2737 		in_data += wr->sg_list[i].length;
2738 		wqe->inline_len += wr->sg_list[i].length;
2739 	}
2740 	return wqe->inline_len;
2741 }
2742 
bnxt_re_copy_wr_payload(struct bnxt_re_dev * rdev,const struct ib_send_wr * wr,struct bnxt_qplib_swqe * wqe)2743 static int bnxt_re_copy_wr_payload(struct bnxt_re_dev *rdev,
2744 				   const struct ib_send_wr *wr,
2745 				   struct bnxt_qplib_swqe *wqe)
2746 {
2747 	int payload_sz = 0;
2748 
2749 	if (wr->send_flags & IB_SEND_INLINE)
2750 		payload_sz = bnxt_re_copy_inline_data(rdev, wr, wqe);
2751 	else
2752 		payload_sz = bnxt_re_build_sgl(wr->sg_list, wqe->sg_list,
2753 					       wqe->num_sge);
2754 
2755 	return payload_sz;
2756 }
2757 
bnxt_ud_qp_hw_stall_workaround(struct bnxt_re_qp * qp)2758 static void bnxt_ud_qp_hw_stall_workaround(struct bnxt_re_qp *qp)
2759 {
2760 	if ((qp->ib_qp.qp_type == IB_QPT_UD ||
2761 	     qp->ib_qp.qp_type == IB_QPT_GSI ||
2762 	     qp->ib_qp.qp_type == IB_QPT_RAW_ETHERTYPE) &&
2763 	     qp->qplib_qp.wqe_cnt == BNXT_RE_UD_QP_HW_STALL) {
2764 		int qp_attr_mask;
2765 		struct ib_qp_attr qp_attr;
2766 
2767 		qp_attr_mask = IB_QP_STATE;
2768 		qp_attr.qp_state = IB_QPS_RTS;
2769 		bnxt_re_modify_qp(&qp->ib_qp, &qp_attr, qp_attr_mask, NULL);
2770 		qp->qplib_qp.wqe_cnt = 0;
2771 	}
2772 }
2773 
bnxt_re_post_send_shadow_qp(struct bnxt_re_dev * rdev,struct bnxt_re_qp * qp,const struct ib_send_wr * wr)2774 static int bnxt_re_post_send_shadow_qp(struct bnxt_re_dev *rdev,
2775 				       struct bnxt_re_qp *qp,
2776 				       const struct ib_send_wr *wr)
2777 {
2778 	int rc = 0, payload_sz = 0;
2779 	unsigned long flags;
2780 
2781 	spin_lock_irqsave(&qp->sq_lock, flags);
2782 	while (wr) {
2783 		struct bnxt_qplib_swqe wqe = {};
2784 
2785 		/* Common */
2786 		wqe.num_sge = wr->num_sge;
2787 		if (wr->num_sge > qp->qplib_qp.sq.max_sge) {
2788 			ibdev_err(&rdev->ibdev,
2789 				  "Limit exceeded for Send SGEs");
2790 			rc = -EINVAL;
2791 			goto bad;
2792 		}
2793 
2794 		payload_sz = bnxt_re_copy_wr_payload(qp->rdev, wr, &wqe);
2795 		if (payload_sz < 0) {
2796 			rc = -EINVAL;
2797 			goto bad;
2798 		}
2799 		wqe.wr_id = wr->wr_id;
2800 
2801 		wqe.type = BNXT_QPLIB_SWQE_TYPE_SEND;
2802 
2803 		rc = bnxt_re_build_send_wqe(qp, wr, &wqe);
2804 		if (!rc)
2805 			rc = bnxt_qplib_post_send(&qp->qplib_qp, &wqe);
2806 bad:
2807 		if (rc) {
2808 			ibdev_err(&rdev->ibdev,
2809 				  "Post send failed opcode = %#x rc = %d",
2810 				  wr->opcode, rc);
2811 			break;
2812 		}
2813 		wr = wr->next;
2814 	}
2815 	bnxt_qplib_post_send_db(&qp->qplib_qp);
2816 	if (!bnxt_qplib_is_chip_gen_p5_p7(qp->rdev->chip_ctx))
2817 		bnxt_ud_qp_hw_stall_workaround(qp);
2818 	spin_unlock_irqrestore(&qp->sq_lock, flags);
2819 	return rc;
2820 }
2821 
bnxt_re_legacy_set_uc_fence(struct bnxt_qplib_swqe * wqe)2822 static void bnxt_re_legacy_set_uc_fence(struct bnxt_qplib_swqe *wqe)
2823 {
2824 	/* Need unconditional fence for non-wire memory opcode
2825 	 * to work as expected.
2826 	 */
2827 	if (wqe->type == BNXT_QPLIB_SWQE_TYPE_LOCAL_INV ||
2828 	    wqe->type == BNXT_QPLIB_SWQE_TYPE_FAST_REG_MR ||
2829 	    wqe->type == BNXT_QPLIB_SWQE_TYPE_REG_MR ||
2830 	    wqe->type == BNXT_QPLIB_SWQE_TYPE_BIND_MW)
2831 		wqe->flags |= BNXT_QPLIB_SWQE_FLAGS_UC_FENCE;
2832 }
2833 
bnxt_re_post_send(struct ib_qp * ib_qp,const struct ib_send_wr * wr,const struct ib_send_wr ** bad_wr)2834 int bnxt_re_post_send(struct ib_qp *ib_qp, const struct ib_send_wr *wr,
2835 		      const struct ib_send_wr **bad_wr)
2836 {
2837 	struct bnxt_re_qp *qp = container_of(ib_qp, struct bnxt_re_qp, ib_qp);
2838 	struct bnxt_qplib_swqe wqe;
2839 	int rc = 0, payload_sz = 0;
2840 	unsigned long flags;
2841 
2842 	spin_lock_irqsave(&qp->sq_lock, flags);
2843 	while (wr) {
2844 		/* House keeping */
2845 		memset(&wqe, 0, sizeof(wqe));
2846 
2847 		/* Common */
2848 		wqe.num_sge = wr->num_sge;
2849 		if (wr->num_sge > qp->qplib_qp.sq.max_sge) {
2850 			ibdev_err(&qp->rdev->ibdev,
2851 				  "Limit exceeded for Send SGEs");
2852 			rc = -EINVAL;
2853 			goto bad;
2854 		}
2855 
2856 		payload_sz = bnxt_re_copy_wr_payload(qp->rdev, wr, &wqe);
2857 		if (payload_sz < 0) {
2858 			rc = -EINVAL;
2859 			goto bad;
2860 		}
2861 		wqe.wr_id = wr->wr_id;
2862 
2863 		switch (wr->opcode) {
2864 		case IB_WR_SEND:
2865 		case IB_WR_SEND_WITH_IMM:
2866 			if (qp->qplib_qp.type == CMDQ_CREATE_QP1_TYPE_GSI) {
2867 				rc = bnxt_re_build_qp1_send_v2(qp, wr, &wqe,
2868 							       payload_sz);
2869 				if (rc)
2870 					goto bad;
2871 				wqe.rawqp1.lflags |=
2872 					SQ_SEND_RAWETH_QP1_LFLAGS_ROCE_CRC;
2873 			}
2874 			switch (wr->send_flags) {
2875 			case IB_SEND_IP_CSUM:
2876 				wqe.rawqp1.lflags |=
2877 					SQ_SEND_RAWETH_QP1_LFLAGS_IP_CHKSUM;
2878 				break;
2879 			default:
2880 				break;
2881 			}
2882 			fallthrough;
2883 		case IB_WR_SEND_WITH_INV:
2884 			rc = bnxt_re_build_send_wqe(qp, wr, &wqe);
2885 			break;
2886 		case IB_WR_RDMA_WRITE:
2887 		case IB_WR_RDMA_WRITE_WITH_IMM:
2888 		case IB_WR_RDMA_READ:
2889 			rc = bnxt_re_build_rdma_wqe(wr, &wqe);
2890 			break;
2891 		case IB_WR_ATOMIC_CMP_AND_SWP:
2892 		case IB_WR_ATOMIC_FETCH_AND_ADD:
2893 			rc = bnxt_re_build_atomic_wqe(wr, &wqe);
2894 			break;
2895 		case IB_WR_RDMA_READ_WITH_INV:
2896 			ibdev_err(&qp->rdev->ibdev,
2897 				  "RDMA Read with Invalidate is not supported");
2898 			rc = -EINVAL;
2899 			goto bad;
2900 		case IB_WR_LOCAL_INV:
2901 			rc = bnxt_re_build_inv_wqe(wr, &wqe);
2902 			break;
2903 		case IB_WR_REG_MR:
2904 			rc = bnxt_re_build_reg_wqe(reg_wr(wr), &wqe);
2905 			break;
2906 		default:
2907 			/* Unsupported WRs */
2908 			ibdev_err(&qp->rdev->ibdev,
2909 				  "WR (%#x) is not supported", wr->opcode);
2910 			rc = -EINVAL;
2911 			goto bad;
2912 		}
2913 		if (!rc) {
2914 			if (!bnxt_qplib_is_chip_gen_p5_p7(qp->rdev->chip_ctx))
2915 				bnxt_re_legacy_set_uc_fence(&wqe);
2916 			rc = bnxt_qplib_post_send(&qp->qplib_qp, &wqe);
2917 		}
2918 bad:
2919 		if (rc) {
2920 			ibdev_err(&qp->rdev->ibdev,
2921 				  "post_send failed op:%#x qps = %#x rc = %d\n",
2922 				  wr->opcode, qp->qplib_qp.state, rc);
2923 			*bad_wr = wr;
2924 			break;
2925 		}
2926 		wr = wr->next;
2927 	}
2928 	bnxt_qplib_post_send_db(&qp->qplib_qp);
2929 	if (!bnxt_qplib_is_chip_gen_p5_p7(qp->rdev->chip_ctx))
2930 		bnxt_ud_qp_hw_stall_workaround(qp);
2931 	spin_unlock_irqrestore(&qp->sq_lock, flags);
2932 
2933 	return rc;
2934 }
2935 
bnxt_re_post_recv_shadow_qp(struct bnxt_re_dev * rdev,struct bnxt_re_qp * qp,const struct ib_recv_wr * wr)2936 static int bnxt_re_post_recv_shadow_qp(struct bnxt_re_dev *rdev,
2937 				       struct bnxt_re_qp *qp,
2938 				       const struct ib_recv_wr *wr)
2939 {
2940 	struct bnxt_qplib_swqe wqe;
2941 	int rc = 0;
2942 
2943 	while (wr) {
2944 		/* House keeping */
2945 		memset(&wqe, 0, sizeof(wqe));
2946 
2947 		/* Common */
2948 		wqe.num_sge = wr->num_sge;
2949 		if (wr->num_sge > qp->qplib_qp.rq.max_sge) {
2950 			ibdev_err(&rdev->ibdev,
2951 				  "Limit exceeded for Receive SGEs");
2952 			rc = -EINVAL;
2953 			break;
2954 		}
2955 		bnxt_re_build_sgl(wr->sg_list, wqe.sg_list, wr->num_sge);
2956 		wqe.wr_id = wr->wr_id;
2957 		wqe.type = BNXT_QPLIB_SWQE_TYPE_RECV;
2958 
2959 		rc = bnxt_qplib_post_recv(&qp->qplib_qp, &wqe);
2960 		if (rc)
2961 			break;
2962 
2963 		wr = wr->next;
2964 	}
2965 	if (!rc)
2966 		bnxt_qplib_post_recv_db(&qp->qplib_qp);
2967 	return rc;
2968 }
2969 
bnxt_re_post_recv(struct ib_qp * ib_qp,const struct ib_recv_wr * wr,const struct ib_recv_wr ** bad_wr)2970 int bnxt_re_post_recv(struct ib_qp *ib_qp, const struct ib_recv_wr *wr,
2971 		      const struct ib_recv_wr **bad_wr)
2972 {
2973 	struct bnxt_re_qp *qp = container_of(ib_qp, struct bnxt_re_qp, ib_qp);
2974 	struct bnxt_qplib_swqe wqe;
2975 	int rc = 0, payload_sz = 0;
2976 	unsigned long flags;
2977 	u32 count = 0;
2978 
2979 	spin_lock_irqsave(&qp->rq_lock, flags);
2980 	while (wr) {
2981 		/* House keeping */
2982 		memset(&wqe, 0, sizeof(wqe));
2983 
2984 		/* Common */
2985 		wqe.num_sge = wr->num_sge;
2986 		if (wr->num_sge > qp->qplib_qp.rq.max_sge) {
2987 			ibdev_err(&qp->rdev->ibdev,
2988 				  "Limit exceeded for Receive SGEs");
2989 			rc = -EINVAL;
2990 			*bad_wr = wr;
2991 			break;
2992 		}
2993 
2994 		payload_sz = bnxt_re_build_sgl(wr->sg_list, wqe.sg_list,
2995 					       wr->num_sge);
2996 		wqe.wr_id = wr->wr_id;
2997 		wqe.type = BNXT_QPLIB_SWQE_TYPE_RECV;
2998 
2999 		if (ib_qp->qp_type == IB_QPT_GSI &&
3000 		    qp->qplib_qp.type != CMDQ_CREATE_QP_TYPE_GSI)
3001 			rc = bnxt_re_build_qp1_shadow_qp_recv(qp, wr, &wqe,
3002 							      payload_sz);
3003 		if (!rc)
3004 			rc = bnxt_qplib_post_recv(&qp->qplib_qp, &wqe);
3005 		if (rc) {
3006 			*bad_wr = wr;
3007 			break;
3008 		}
3009 
3010 		/* Ring DB if the RQEs posted reaches a threshold value */
3011 		if (++count >= BNXT_RE_RQ_WQE_THRESHOLD) {
3012 			bnxt_qplib_post_recv_db(&qp->qplib_qp);
3013 			count = 0;
3014 		}
3015 
3016 		wr = wr->next;
3017 	}
3018 
3019 	if (count)
3020 		bnxt_qplib_post_recv_db(&qp->qplib_qp);
3021 
3022 	spin_unlock_irqrestore(&qp->rq_lock, flags);
3023 
3024 	return rc;
3025 }
3026 
bnxt_re_get_nq(struct bnxt_re_dev * rdev)3027 static struct bnxt_qplib_nq *bnxt_re_get_nq(struct bnxt_re_dev *rdev)
3028 {
3029 	int min, indx;
3030 
3031 	mutex_lock(&rdev->nqr->load_lock);
3032 	for (indx = 0, min = 0; indx < (rdev->nqr->num_msix - 1); indx++) {
3033 		if (rdev->nqr->nq[min].load > rdev->nqr->nq[indx].load)
3034 			min = indx;
3035 	}
3036 	rdev->nqr->nq[min].load++;
3037 	mutex_unlock(&rdev->nqr->load_lock);
3038 
3039 	return &rdev->nqr->nq[min];
3040 }
3041 
bnxt_re_put_nq(struct bnxt_re_dev * rdev,struct bnxt_qplib_nq * nq)3042 static void bnxt_re_put_nq(struct bnxt_re_dev *rdev, struct bnxt_qplib_nq *nq)
3043 {
3044 	mutex_lock(&rdev->nqr->load_lock);
3045 	nq->load--;
3046 	mutex_unlock(&rdev->nqr->load_lock);
3047 }
3048 
3049 /* Completion Queues */
bnxt_re_destroy_cq(struct ib_cq * ib_cq,struct ib_udata * udata)3050 int bnxt_re_destroy_cq(struct ib_cq *ib_cq, struct ib_udata *udata)
3051 {
3052 	struct bnxt_qplib_chip_ctx *cctx;
3053 	struct bnxt_qplib_nq *nq;
3054 	struct bnxt_re_dev *rdev;
3055 	struct bnxt_re_cq *cq;
3056 
3057 	cq = container_of(ib_cq, struct bnxt_re_cq, ib_cq);
3058 	rdev = cq->rdev;
3059 	nq = cq->qplib_cq.nq;
3060 	cctx = rdev->chip_ctx;
3061 
3062 	if (cctx->modes.toggle_bits & BNXT_QPLIB_CQ_TOGGLE_BIT) {
3063 		free_page((unsigned long)cq->uctx_cq_page);
3064 		hash_del(&cq->hash_entry);
3065 	}
3066 	bnxt_qplib_destroy_cq(&rdev->qplib_res, &cq->qplib_cq);
3067 
3068 	bnxt_re_put_nq(rdev, nq);
3069 	ib_umem_release(cq->umem);
3070 
3071 	atomic_dec(&rdev->stats.res.cq_count);
3072 	kfree(cq->cql);
3073 	return 0;
3074 }
3075 
bnxt_re_create_cq(struct ib_cq * ibcq,const struct ib_cq_init_attr * attr,struct uverbs_attr_bundle * attrs)3076 int bnxt_re_create_cq(struct ib_cq *ibcq, const struct ib_cq_init_attr *attr,
3077 		      struct uverbs_attr_bundle *attrs)
3078 {
3079 	struct bnxt_re_cq *cq = container_of(ibcq, struct bnxt_re_cq, ib_cq);
3080 	struct bnxt_re_dev *rdev = to_bnxt_re_dev(ibcq->device, ibdev);
3081 	struct ib_udata *udata = &attrs->driver_udata;
3082 	struct bnxt_re_ucontext *uctx =
3083 		rdma_udata_to_drv_context(udata, struct bnxt_re_ucontext, ib_uctx);
3084 	struct bnxt_qplib_dev_attr *dev_attr = rdev->dev_attr;
3085 	struct bnxt_qplib_chip_ctx *cctx;
3086 	int cqe = attr->cqe;
3087 	int rc, entries;
3088 	u32 active_cqs;
3089 
3090 	if (attr->flags)
3091 		return -EOPNOTSUPP;
3092 
3093 	/* Validate CQ fields */
3094 	if (cqe < 1 || cqe > dev_attr->max_cq_wqes) {
3095 		ibdev_err(&rdev->ibdev, "Failed to create CQ -max exceeded");
3096 		return -EINVAL;
3097 	}
3098 
3099 	cq->rdev = rdev;
3100 	cctx = rdev->chip_ctx;
3101 	cq->qplib_cq.cq_handle = (u64)(unsigned long)(&cq->qplib_cq);
3102 
3103 	entries = bnxt_re_init_depth(cqe + 1, uctx);
3104 	if (entries > dev_attr->max_cq_wqes + 1)
3105 		entries = dev_attr->max_cq_wqes + 1;
3106 
3107 	cq->qplib_cq.sg_info.pgsize = PAGE_SIZE;
3108 	cq->qplib_cq.sg_info.pgshft = PAGE_SHIFT;
3109 	if (udata) {
3110 		struct bnxt_re_cq_req req;
3111 		if (ib_copy_from_udata(&req, udata, sizeof(req))) {
3112 			rc = -EFAULT;
3113 			goto fail;
3114 		}
3115 
3116 		cq->umem = ib_umem_get(&rdev->ibdev, req.cq_va,
3117 				       entries * sizeof(struct cq_base),
3118 				       IB_ACCESS_LOCAL_WRITE);
3119 		if (IS_ERR(cq->umem)) {
3120 			rc = PTR_ERR(cq->umem);
3121 			goto fail;
3122 		}
3123 		cq->qplib_cq.sg_info.umem = cq->umem;
3124 		cq->qplib_cq.dpi = &uctx->dpi;
3125 	} else {
3126 		cq->max_cql = min_t(u32, entries, MAX_CQL_PER_POLL);
3127 		cq->cql = kcalloc(cq->max_cql, sizeof(struct bnxt_qplib_cqe),
3128 				  GFP_KERNEL);
3129 		if (!cq->cql) {
3130 			rc = -ENOMEM;
3131 			goto fail;
3132 		}
3133 
3134 		cq->qplib_cq.dpi = &rdev->dpi_privileged;
3135 	}
3136 	cq->qplib_cq.max_wqe = entries;
3137 	cq->qplib_cq.coalescing = &rdev->cq_coalescing;
3138 	cq->qplib_cq.nq = bnxt_re_get_nq(rdev);
3139 	cq->qplib_cq.cnq_hw_ring_id = cq->qplib_cq.nq->ring_id;
3140 
3141 	rc = bnxt_qplib_create_cq(&rdev->qplib_res, &cq->qplib_cq);
3142 	if (rc) {
3143 		ibdev_err(&rdev->ibdev, "Failed to create HW CQ");
3144 		goto fail;
3145 	}
3146 
3147 	cq->ib_cq.cqe = entries;
3148 	cq->cq_period = cq->qplib_cq.period;
3149 
3150 	active_cqs = atomic_inc_return(&rdev->stats.res.cq_count);
3151 	if (active_cqs > rdev->stats.res.cq_watermark)
3152 		rdev->stats.res.cq_watermark = active_cqs;
3153 	spin_lock_init(&cq->cq_lock);
3154 
3155 	if (udata) {
3156 		struct bnxt_re_cq_resp resp = {};
3157 
3158 		if (cctx->modes.toggle_bits & BNXT_QPLIB_CQ_TOGGLE_BIT) {
3159 			hash_add(rdev->cq_hash, &cq->hash_entry, cq->qplib_cq.id);
3160 			/* Allocate a page */
3161 			cq->uctx_cq_page = (void *)get_zeroed_page(GFP_KERNEL);
3162 			if (!cq->uctx_cq_page) {
3163 				rc = -ENOMEM;
3164 				goto c2fail;
3165 			}
3166 			resp.comp_mask |= BNXT_RE_CQ_TOGGLE_PAGE_SUPPORT;
3167 		}
3168 		resp.cqid = cq->qplib_cq.id;
3169 		resp.tail = cq->qplib_cq.hwq.cons;
3170 		resp.phase = cq->qplib_cq.period;
3171 		resp.rsvd = 0;
3172 		rc = ib_copy_to_udata(udata, &resp, min(sizeof(resp), udata->outlen));
3173 		if (rc) {
3174 			ibdev_err(&rdev->ibdev, "Failed to copy CQ udata");
3175 			bnxt_qplib_destroy_cq(&rdev->qplib_res, &cq->qplib_cq);
3176 			goto free_mem;
3177 		}
3178 	}
3179 
3180 	return 0;
3181 
3182 free_mem:
3183 	free_page((unsigned long)cq->uctx_cq_page);
3184 c2fail:
3185 	ib_umem_release(cq->umem);
3186 fail:
3187 	kfree(cq->cql);
3188 	return rc;
3189 }
3190 
bnxt_re_resize_cq_complete(struct bnxt_re_cq * cq)3191 static void bnxt_re_resize_cq_complete(struct bnxt_re_cq *cq)
3192 {
3193 	struct bnxt_re_dev *rdev = cq->rdev;
3194 
3195 	bnxt_qplib_resize_cq_complete(&rdev->qplib_res, &cq->qplib_cq);
3196 
3197 	cq->qplib_cq.max_wqe = cq->resize_cqe;
3198 	if (cq->resize_umem) {
3199 		ib_umem_release(cq->umem);
3200 		cq->umem = cq->resize_umem;
3201 		cq->resize_umem = NULL;
3202 		cq->resize_cqe = 0;
3203 	}
3204 }
3205 
bnxt_re_resize_cq(struct ib_cq * ibcq,int cqe,struct ib_udata * udata)3206 int bnxt_re_resize_cq(struct ib_cq *ibcq, int cqe, struct ib_udata *udata)
3207 {
3208 	struct bnxt_qplib_sg_info sg_info = {};
3209 	struct bnxt_qplib_dpi *orig_dpi = NULL;
3210 	struct bnxt_qplib_dev_attr *dev_attr;
3211 	struct bnxt_re_ucontext *uctx = NULL;
3212 	struct bnxt_re_resize_cq_req req;
3213 	struct bnxt_re_dev *rdev;
3214 	struct bnxt_re_cq *cq;
3215 	int rc, entries;
3216 
3217 	cq =  container_of(ibcq, struct bnxt_re_cq, ib_cq);
3218 	rdev = cq->rdev;
3219 	dev_attr = rdev->dev_attr;
3220 	if (!ibcq->uobject) {
3221 		ibdev_err(&rdev->ibdev, "Kernel CQ Resize not supported");
3222 		return -EOPNOTSUPP;
3223 	}
3224 
3225 	if (cq->resize_umem) {
3226 		ibdev_err(&rdev->ibdev, "Resize CQ %#x failed - Busy",
3227 			  cq->qplib_cq.id);
3228 		return -EBUSY;
3229 	}
3230 
3231 	/* Check the requested cq depth out of supported depth */
3232 	if (cqe < 1 || cqe > dev_attr->max_cq_wqes) {
3233 		ibdev_err(&rdev->ibdev, "Resize CQ %#x failed - out of range cqe %d",
3234 			  cq->qplib_cq.id, cqe);
3235 		return -EINVAL;
3236 	}
3237 
3238 	uctx = rdma_udata_to_drv_context(udata, struct bnxt_re_ucontext, ib_uctx);
3239 	entries = bnxt_re_init_depth(cqe + 1, uctx);
3240 	if (entries > dev_attr->max_cq_wqes + 1)
3241 		entries = dev_attr->max_cq_wqes + 1;
3242 
3243 	/* uverbs consumer */
3244 	if (ib_copy_from_udata(&req, udata, sizeof(req))) {
3245 		rc = -EFAULT;
3246 		goto fail;
3247 	}
3248 
3249 	cq->resize_umem = ib_umem_get(&rdev->ibdev, req.cq_va,
3250 				      entries * sizeof(struct cq_base),
3251 				      IB_ACCESS_LOCAL_WRITE);
3252 	if (IS_ERR(cq->resize_umem)) {
3253 		rc = PTR_ERR(cq->resize_umem);
3254 		cq->resize_umem = NULL;
3255 		ibdev_err(&rdev->ibdev, "%s: ib_umem_get failed! rc = %d\n",
3256 			  __func__, rc);
3257 		goto fail;
3258 	}
3259 	cq->resize_cqe = entries;
3260 	memcpy(&sg_info, &cq->qplib_cq.sg_info, sizeof(sg_info));
3261 	orig_dpi = cq->qplib_cq.dpi;
3262 
3263 	cq->qplib_cq.sg_info.umem = cq->resize_umem;
3264 	cq->qplib_cq.sg_info.pgsize = PAGE_SIZE;
3265 	cq->qplib_cq.sg_info.pgshft = PAGE_SHIFT;
3266 	cq->qplib_cq.dpi = &uctx->dpi;
3267 
3268 	rc = bnxt_qplib_resize_cq(&rdev->qplib_res, &cq->qplib_cq, entries);
3269 	if (rc) {
3270 		ibdev_err(&rdev->ibdev, "Resize HW CQ %#x failed!",
3271 			  cq->qplib_cq.id);
3272 		goto fail;
3273 	}
3274 
3275 	cq->ib_cq.cqe = cq->resize_cqe;
3276 	atomic_inc(&rdev->stats.res.resize_count);
3277 
3278 	return 0;
3279 
3280 fail:
3281 	if (cq->resize_umem) {
3282 		ib_umem_release(cq->resize_umem);
3283 		cq->resize_umem = NULL;
3284 		cq->resize_cqe = 0;
3285 		memcpy(&cq->qplib_cq.sg_info, &sg_info, sizeof(sg_info));
3286 		cq->qplib_cq.dpi = orig_dpi;
3287 	}
3288 	return rc;
3289 }
3290 
__req_to_ib_wc_status(u8 qstatus)3291 static u8 __req_to_ib_wc_status(u8 qstatus)
3292 {
3293 	switch (qstatus) {
3294 	case CQ_REQ_STATUS_OK:
3295 		return IB_WC_SUCCESS;
3296 	case CQ_REQ_STATUS_BAD_RESPONSE_ERR:
3297 		return IB_WC_BAD_RESP_ERR;
3298 	case CQ_REQ_STATUS_LOCAL_LENGTH_ERR:
3299 		return IB_WC_LOC_LEN_ERR;
3300 	case CQ_REQ_STATUS_LOCAL_QP_OPERATION_ERR:
3301 		return IB_WC_LOC_QP_OP_ERR;
3302 	case CQ_REQ_STATUS_LOCAL_PROTECTION_ERR:
3303 		return IB_WC_LOC_PROT_ERR;
3304 	case CQ_REQ_STATUS_MEMORY_MGT_OPERATION_ERR:
3305 		return IB_WC_GENERAL_ERR;
3306 	case CQ_REQ_STATUS_REMOTE_INVALID_REQUEST_ERR:
3307 		return IB_WC_REM_INV_REQ_ERR;
3308 	case CQ_REQ_STATUS_REMOTE_ACCESS_ERR:
3309 		return IB_WC_REM_ACCESS_ERR;
3310 	case CQ_REQ_STATUS_REMOTE_OPERATION_ERR:
3311 		return IB_WC_REM_OP_ERR;
3312 	case CQ_REQ_STATUS_RNR_NAK_RETRY_CNT_ERR:
3313 		return IB_WC_RNR_RETRY_EXC_ERR;
3314 	case CQ_REQ_STATUS_TRANSPORT_RETRY_CNT_ERR:
3315 		return IB_WC_RETRY_EXC_ERR;
3316 	case CQ_REQ_STATUS_WORK_REQUEST_FLUSHED_ERR:
3317 		return IB_WC_WR_FLUSH_ERR;
3318 	default:
3319 		return IB_WC_GENERAL_ERR;
3320 	}
3321 	return 0;
3322 }
3323 
__rawqp1_to_ib_wc_status(u8 qstatus)3324 static u8 __rawqp1_to_ib_wc_status(u8 qstatus)
3325 {
3326 	switch (qstatus) {
3327 	case CQ_RES_RAWETH_QP1_STATUS_OK:
3328 		return IB_WC_SUCCESS;
3329 	case CQ_RES_RAWETH_QP1_STATUS_LOCAL_ACCESS_ERROR:
3330 		return IB_WC_LOC_ACCESS_ERR;
3331 	case CQ_RES_RAWETH_QP1_STATUS_HW_LOCAL_LENGTH_ERR:
3332 		return IB_WC_LOC_LEN_ERR;
3333 	case CQ_RES_RAWETH_QP1_STATUS_LOCAL_PROTECTION_ERR:
3334 		return IB_WC_LOC_PROT_ERR;
3335 	case CQ_RES_RAWETH_QP1_STATUS_LOCAL_QP_OPERATION_ERR:
3336 		return IB_WC_LOC_QP_OP_ERR;
3337 	case CQ_RES_RAWETH_QP1_STATUS_MEMORY_MGT_OPERATION_ERR:
3338 		return IB_WC_GENERAL_ERR;
3339 	case CQ_RES_RAWETH_QP1_STATUS_WORK_REQUEST_FLUSHED_ERR:
3340 		return IB_WC_WR_FLUSH_ERR;
3341 	case CQ_RES_RAWETH_QP1_STATUS_HW_FLUSH_ERR:
3342 		return IB_WC_WR_FLUSH_ERR;
3343 	default:
3344 		return IB_WC_GENERAL_ERR;
3345 	}
3346 }
3347 
__rc_to_ib_wc_status(u8 qstatus)3348 static u8 __rc_to_ib_wc_status(u8 qstatus)
3349 {
3350 	switch (qstatus) {
3351 	case CQ_RES_RC_STATUS_OK:
3352 		return IB_WC_SUCCESS;
3353 	case CQ_RES_RC_STATUS_LOCAL_ACCESS_ERROR:
3354 		return IB_WC_LOC_ACCESS_ERR;
3355 	case CQ_RES_RC_STATUS_LOCAL_LENGTH_ERR:
3356 		return IB_WC_LOC_LEN_ERR;
3357 	case CQ_RES_RC_STATUS_LOCAL_PROTECTION_ERR:
3358 		return IB_WC_LOC_PROT_ERR;
3359 	case CQ_RES_RC_STATUS_LOCAL_QP_OPERATION_ERR:
3360 		return IB_WC_LOC_QP_OP_ERR;
3361 	case CQ_RES_RC_STATUS_MEMORY_MGT_OPERATION_ERR:
3362 		return IB_WC_GENERAL_ERR;
3363 	case CQ_RES_RC_STATUS_REMOTE_INVALID_REQUEST_ERR:
3364 		return IB_WC_REM_INV_REQ_ERR;
3365 	case CQ_RES_RC_STATUS_WORK_REQUEST_FLUSHED_ERR:
3366 		return IB_WC_WR_FLUSH_ERR;
3367 	case CQ_RES_RC_STATUS_HW_FLUSH_ERR:
3368 		return IB_WC_WR_FLUSH_ERR;
3369 	default:
3370 		return IB_WC_GENERAL_ERR;
3371 	}
3372 }
3373 
bnxt_re_process_req_wc(struct ib_wc * wc,struct bnxt_qplib_cqe * cqe)3374 static void bnxt_re_process_req_wc(struct ib_wc *wc, struct bnxt_qplib_cqe *cqe)
3375 {
3376 	switch (cqe->type) {
3377 	case BNXT_QPLIB_SWQE_TYPE_SEND:
3378 		wc->opcode = IB_WC_SEND;
3379 		break;
3380 	case BNXT_QPLIB_SWQE_TYPE_SEND_WITH_IMM:
3381 		wc->opcode = IB_WC_SEND;
3382 		wc->wc_flags |= IB_WC_WITH_IMM;
3383 		break;
3384 	case BNXT_QPLIB_SWQE_TYPE_SEND_WITH_INV:
3385 		wc->opcode = IB_WC_SEND;
3386 		wc->wc_flags |= IB_WC_WITH_INVALIDATE;
3387 		break;
3388 	case BNXT_QPLIB_SWQE_TYPE_RDMA_WRITE:
3389 		wc->opcode = IB_WC_RDMA_WRITE;
3390 		break;
3391 	case BNXT_QPLIB_SWQE_TYPE_RDMA_WRITE_WITH_IMM:
3392 		wc->opcode = IB_WC_RDMA_WRITE;
3393 		wc->wc_flags |= IB_WC_WITH_IMM;
3394 		break;
3395 	case BNXT_QPLIB_SWQE_TYPE_RDMA_READ:
3396 		wc->opcode = IB_WC_RDMA_READ;
3397 		break;
3398 	case BNXT_QPLIB_SWQE_TYPE_ATOMIC_CMP_AND_SWP:
3399 		wc->opcode = IB_WC_COMP_SWAP;
3400 		break;
3401 	case BNXT_QPLIB_SWQE_TYPE_ATOMIC_FETCH_AND_ADD:
3402 		wc->opcode = IB_WC_FETCH_ADD;
3403 		break;
3404 	case BNXT_QPLIB_SWQE_TYPE_LOCAL_INV:
3405 		wc->opcode = IB_WC_LOCAL_INV;
3406 		break;
3407 	case BNXT_QPLIB_SWQE_TYPE_REG_MR:
3408 		wc->opcode = IB_WC_REG_MR;
3409 		break;
3410 	default:
3411 		wc->opcode = IB_WC_SEND;
3412 		break;
3413 	}
3414 
3415 	wc->status = __req_to_ib_wc_status(cqe->status);
3416 }
3417 
bnxt_re_check_packet_type(u16 raweth_qp1_flags,u16 raweth_qp1_flags2)3418 static int bnxt_re_check_packet_type(u16 raweth_qp1_flags,
3419 				     u16 raweth_qp1_flags2)
3420 {
3421 	bool is_ipv6 = false, is_ipv4 = false;
3422 
3423 	/* raweth_qp1_flags Bit 9-6 indicates itype */
3424 	if ((raweth_qp1_flags & CQ_RES_RAWETH_QP1_RAWETH_QP1_FLAGS_ITYPE_ROCE)
3425 	    != CQ_RES_RAWETH_QP1_RAWETH_QP1_FLAGS_ITYPE_ROCE)
3426 		return -1;
3427 
3428 	if (raweth_qp1_flags2 &
3429 	    CQ_RES_RAWETH_QP1_RAWETH_QP1_FLAGS2_IP_CS_CALC &&
3430 	    raweth_qp1_flags2 &
3431 	    CQ_RES_RAWETH_QP1_RAWETH_QP1_FLAGS2_L4_CS_CALC) {
3432 		/* raweth_qp1_flags2 Bit 8 indicates ip_type. 0-v4 1 - v6 */
3433 		(raweth_qp1_flags2 &
3434 		 CQ_RES_RAWETH_QP1_RAWETH_QP1_FLAGS2_IP_TYPE) ?
3435 			(is_ipv6 = true) : (is_ipv4 = true);
3436 		return ((is_ipv6) ?
3437 			 BNXT_RE_ROCEV2_IPV6_PACKET :
3438 			 BNXT_RE_ROCEV2_IPV4_PACKET);
3439 	} else {
3440 		return BNXT_RE_ROCE_V1_PACKET;
3441 	}
3442 }
3443 
bnxt_re_to_ib_nw_type(int nw_type)3444 static int bnxt_re_to_ib_nw_type(int nw_type)
3445 {
3446 	u8 nw_hdr_type = 0xFF;
3447 
3448 	switch (nw_type) {
3449 	case BNXT_RE_ROCE_V1_PACKET:
3450 		nw_hdr_type = RDMA_NETWORK_ROCE_V1;
3451 		break;
3452 	case BNXT_RE_ROCEV2_IPV4_PACKET:
3453 		nw_hdr_type = RDMA_NETWORK_IPV4;
3454 		break;
3455 	case BNXT_RE_ROCEV2_IPV6_PACKET:
3456 		nw_hdr_type = RDMA_NETWORK_IPV6;
3457 		break;
3458 	}
3459 	return nw_hdr_type;
3460 }
3461 
bnxt_re_is_loopback_packet(struct bnxt_re_dev * rdev,void * rq_hdr_buf)3462 static bool bnxt_re_is_loopback_packet(struct bnxt_re_dev *rdev,
3463 				       void *rq_hdr_buf)
3464 {
3465 	u8 *tmp_buf = NULL;
3466 	struct ethhdr *eth_hdr;
3467 	u16 eth_type;
3468 	bool rc = false;
3469 
3470 	tmp_buf = (u8 *)rq_hdr_buf;
3471 	/*
3472 	 * If dest mac is not same as I/F mac, this could be a
3473 	 * loopback address or multicast address, check whether
3474 	 * it is a loopback packet
3475 	 */
3476 	if (!ether_addr_equal(tmp_buf, rdev->netdev->dev_addr)) {
3477 		tmp_buf += 4;
3478 		/* Check the  ether type */
3479 		eth_hdr = (struct ethhdr *)tmp_buf;
3480 		eth_type = ntohs(eth_hdr->h_proto);
3481 		switch (eth_type) {
3482 		case ETH_P_IBOE:
3483 			rc = true;
3484 			break;
3485 		case ETH_P_IP:
3486 		case ETH_P_IPV6: {
3487 			u32 len;
3488 			struct udphdr *udp_hdr;
3489 
3490 			len = (eth_type == ETH_P_IP ? sizeof(struct iphdr) :
3491 						      sizeof(struct ipv6hdr));
3492 			tmp_buf += sizeof(struct ethhdr) + len;
3493 			udp_hdr = (struct udphdr *)tmp_buf;
3494 			if (ntohs(udp_hdr->dest) ==
3495 				    ROCE_V2_UDP_DPORT)
3496 				rc = true;
3497 			break;
3498 			}
3499 		default:
3500 			break;
3501 		}
3502 	}
3503 
3504 	return rc;
3505 }
3506 
bnxt_re_process_raw_qp_pkt_rx(struct bnxt_re_qp * gsi_qp,struct bnxt_qplib_cqe * cqe)3507 static int bnxt_re_process_raw_qp_pkt_rx(struct bnxt_re_qp *gsi_qp,
3508 					 struct bnxt_qplib_cqe *cqe)
3509 {
3510 	struct bnxt_re_dev *rdev = gsi_qp->rdev;
3511 	struct bnxt_re_sqp_entries *sqp_entry = NULL;
3512 	struct bnxt_re_qp *gsi_sqp = rdev->gsi_ctx.gsi_sqp;
3513 	dma_addr_t shrq_hdr_buf_map;
3514 	struct ib_sge s_sge[2] = {};
3515 	struct ib_sge r_sge[2] = {};
3516 	struct bnxt_re_ah *gsi_sah;
3517 	struct ib_recv_wr rwr = {};
3518 	dma_addr_t rq_hdr_buf_map;
3519 	struct ib_ud_wr udwr = {};
3520 	struct ib_send_wr *swr;
3521 	u32 skip_bytes = 0;
3522 	int pkt_type = 0;
3523 	void *rq_hdr_buf;
3524 	u32 offset = 0;
3525 	u32 tbl_idx;
3526 	int rc;
3527 
3528 	swr = &udwr.wr;
3529 	tbl_idx = cqe->wr_id;
3530 
3531 	rq_hdr_buf = gsi_qp->qplib_qp.rq_hdr_buf +
3532 			(tbl_idx * gsi_qp->qplib_qp.rq_hdr_buf_size);
3533 	rq_hdr_buf_map = bnxt_qplib_get_qp_buf_from_index(&gsi_qp->qplib_qp,
3534 							  tbl_idx);
3535 
3536 	/* Shadow QP header buffer */
3537 	shrq_hdr_buf_map = bnxt_qplib_get_qp_buf_from_index(&gsi_qp->qplib_qp,
3538 							    tbl_idx);
3539 	sqp_entry = &rdev->gsi_ctx.sqp_tbl[tbl_idx];
3540 
3541 	/* Store this cqe */
3542 	memcpy(&sqp_entry->cqe, cqe, sizeof(struct bnxt_qplib_cqe));
3543 	sqp_entry->qp1_qp = gsi_qp;
3544 
3545 	/* Find packet type from the cqe */
3546 
3547 	pkt_type = bnxt_re_check_packet_type(cqe->raweth_qp1_flags,
3548 					     cqe->raweth_qp1_flags2);
3549 	if (pkt_type < 0) {
3550 		ibdev_err(&rdev->ibdev, "Invalid packet\n");
3551 		return -EINVAL;
3552 	}
3553 
3554 	/* Adjust the offset for the user buffer and post in the rq */
3555 
3556 	if (pkt_type == BNXT_RE_ROCEV2_IPV4_PACKET)
3557 		offset = 20;
3558 
3559 	/*
3560 	 * QP1 loopback packet has 4 bytes of internal header before
3561 	 * ether header. Skip these four bytes.
3562 	 */
3563 	if (bnxt_re_is_loopback_packet(rdev, rq_hdr_buf))
3564 		skip_bytes = 4;
3565 
3566 	/* First send SGE . Skip the ether header*/
3567 	s_sge[0].addr = rq_hdr_buf_map + BNXT_QPLIB_MAX_QP1_RQ_ETH_HDR_SIZE
3568 			+ skip_bytes;
3569 	s_sge[0].lkey = 0xFFFFFFFF;
3570 	s_sge[0].length = offset ? BNXT_QPLIB_MAX_GRH_HDR_SIZE_IPV4 :
3571 				BNXT_QPLIB_MAX_GRH_HDR_SIZE_IPV6;
3572 
3573 	/* Second Send SGE */
3574 	s_sge[1].addr = s_sge[0].addr + s_sge[0].length +
3575 			BNXT_QPLIB_MAX_QP1_RQ_BDETH_HDR_SIZE;
3576 	if (pkt_type != BNXT_RE_ROCE_V1_PACKET)
3577 		s_sge[1].addr += 8;
3578 	s_sge[1].lkey = 0xFFFFFFFF;
3579 	s_sge[1].length = 256;
3580 
3581 	/* First recv SGE */
3582 
3583 	r_sge[0].addr = shrq_hdr_buf_map;
3584 	r_sge[0].lkey = 0xFFFFFFFF;
3585 	r_sge[0].length = 40;
3586 
3587 	r_sge[1].addr = sqp_entry->sge.addr + offset;
3588 	r_sge[1].lkey = sqp_entry->sge.lkey;
3589 	r_sge[1].length = BNXT_QPLIB_MAX_GRH_HDR_SIZE_IPV6 + 256 - offset;
3590 
3591 	/* Create receive work request */
3592 	rwr.num_sge = 2;
3593 	rwr.sg_list = r_sge;
3594 	rwr.wr_id = tbl_idx;
3595 	rwr.next = NULL;
3596 
3597 	rc = bnxt_re_post_recv_shadow_qp(rdev, gsi_sqp, &rwr);
3598 	if (rc) {
3599 		ibdev_err(&rdev->ibdev,
3600 			  "Failed to post Rx buffers to shadow QP");
3601 		return -ENOMEM;
3602 	}
3603 
3604 	swr->num_sge = 2;
3605 	swr->sg_list = s_sge;
3606 	swr->wr_id = tbl_idx;
3607 	swr->opcode = IB_WR_SEND;
3608 	swr->next = NULL;
3609 	gsi_sah = rdev->gsi_ctx.gsi_sah;
3610 	udwr.ah = &gsi_sah->ib_ah;
3611 	udwr.remote_qpn = gsi_sqp->qplib_qp.id;
3612 	udwr.remote_qkey = gsi_sqp->qplib_qp.qkey;
3613 
3614 	/* post data received  in the send queue */
3615 	return bnxt_re_post_send_shadow_qp(rdev, gsi_sqp, swr);
3616 }
3617 
bnxt_re_process_res_rawqp1_wc(struct ib_wc * wc,struct bnxt_qplib_cqe * cqe)3618 static void bnxt_re_process_res_rawqp1_wc(struct ib_wc *wc,
3619 					  struct bnxt_qplib_cqe *cqe)
3620 {
3621 	wc->opcode = IB_WC_RECV;
3622 	wc->status = __rawqp1_to_ib_wc_status(cqe->status);
3623 	wc->wc_flags |= IB_WC_GRH;
3624 }
3625 
bnxt_re_check_if_vlan_valid(struct bnxt_re_dev * rdev,u16 vlan_id)3626 static bool bnxt_re_check_if_vlan_valid(struct bnxt_re_dev *rdev,
3627 					u16 vlan_id)
3628 {
3629 	/*
3630 	 * Check if the vlan is configured in the host.  If not configured, it
3631 	 * can be a transparent VLAN. So dont report the vlan id.
3632 	 */
3633 	if (!__vlan_find_dev_deep_rcu(rdev->netdev,
3634 				      htons(ETH_P_8021Q), vlan_id))
3635 		return false;
3636 	return true;
3637 }
3638 
bnxt_re_is_vlan_pkt(struct bnxt_qplib_cqe * orig_cqe,u16 * vid,u8 * sl)3639 static bool bnxt_re_is_vlan_pkt(struct bnxt_qplib_cqe *orig_cqe,
3640 				u16 *vid, u8 *sl)
3641 {
3642 	bool ret = false;
3643 	u32 metadata;
3644 	u16 tpid;
3645 
3646 	metadata = orig_cqe->raweth_qp1_metadata;
3647 	if (orig_cqe->raweth_qp1_flags2 &
3648 		CQ_RES_RAWETH_QP1_RAWETH_QP1_FLAGS2_META_FORMAT_VLAN) {
3649 		tpid = ((metadata &
3650 			 CQ_RES_RAWETH_QP1_RAWETH_QP1_METADATA_TPID_MASK) >>
3651 			 CQ_RES_RAWETH_QP1_RAWETH_QP1_METADATA_TPID_SFT);
3652 		if (tpid == ETH_P_8021Q) {
3653 			*vid = metadata &
3654 			       CQ_RES_RAWETH_QP1_RAWETH_QP1_METADATA_VID_MASK;
3655 			*sl = (metadata &
3656 			       CQ_RES_RAWETH_QP1_RAWETH_QP1_METADATA_PRI_MASK) >>
3657 			       CQ_RES_RAWETH_QP1_RAWETH_QP1_METADATA_PRI_SFT;
3658 			ret = true;
3659 		}
3660 	}
3661 
3662 	return ret;
3663 }
3664 
bnxt_re_process_res_rc_wc(struct ib_wc * wc,struct bnxt_qplib_cqe * cqe)3665 static void bnxt_re_process_res_rc_wc(struct ib_wc *wc,
3666 				      struct bnxt_qplib_cqe *cqe)
3667 {
3668 	wc->opcode = IB_WC_RECV;
3669 	wc->status = __rc_to_ib_wc_status(cqe->status);
3670 
3671 	if (cqe->flags & CQ_RES_RC_FLAGS_IMM)
3672 		wc->wc_flags |= IB_WC_WITH_IMM;
3673 	if (cqe->flags & CQ_RES_RC_FLAGS_INV)
3674 		wc->wc_flags |= IB_WC_WITH_INVALIDATE;
3675 	if ((cqe->flags & (CQ_RES_RC_FLAGS_RDMA | CQ_RES_RC_FLAGS_IMM)) ==
3676 	    (CQ_RES_RC_FLAGS_RDMA | CQ_RES_RC_FLAGS_IMM))
3677 		wc->opcode = IB_WC_RECV_RDMA_WITH_IMM;
3678 }
3679 
bnxt_re_process_res_shadow_qp_wc(struct bnxt_re_qp * gsi_sqp,struct ib_wc * wc,struct bnxt_qplib_cqe * cqe)3680 static void bnxt_re_process_res_shadow_qp_wc(struct bnxt_re_qp *gsi_sqp,
3681 					     struct ib_wc *wc,
3682 					     struct bnxt_qplib_cqe *cqe)
3683 {
3684 	struct bnxt_re_dev *rdev = gsi_sqp->rdev;
3685 	struct bnxt_re_qp *gsi_qp = NULL;
3686 	struct bnxt_qplib_cqe *orig_cqe = NULL;
3687 	struct bnxt_re_sqp_entries *sqp_entry = NULL;
3688 	int nw_type;
3689 	u32 tbl_idx;
3690 	u16 vlan_id;
3691 	u8 sl;
3692 
3693 	tbl_idx = cqe->wr_id;
3694 
3695 	sqp_entry = &rdev->gsi_ctx.sqp_tbl[tbl_idx];
3696 	gsi_qp = sqp_entry->qp1_qp;
3697 	orig_cqe = &sqp_entry->cqe;
3698 
3699 	wc->wr_id = sqp_entry->wrid;
3700 	wc->byte_len = orig_cqe->length;
3701 	wc->qp = &gsi_qp->ib_qp;
3702 
3703 	wc->ex.imm_data = cpu_to_be32(orig_cqe->immdata);
3704 	wc->src_qp = orig_cqe->src_qp;
3705 	memcpy(wc->smac, orig_cqe->smac, ETH_ALEN);
3706 	if (bnxt_re_is_vlan_pkt(orig_cqe, &vlan_id, &sl)) {
3707 		if (bnxt_re_check_if_vlan_valid(rdev, vlan_id)) {
3708 			wc->vlan_id = vlan_id;
3709 			wc->sl = sl;
3710 			wc->wc_flags |= IB_WC_WITH_VLAN;
3711 		}
3712 	}
3713 	wc->port_num = 1;
3714 	wc->vendor_err = orig_cqe->status;
3715 
3716 	wc->opcode = IB_WC_RECV;
3717 	wc->status = __rawqp1_to_ib_wc_status(orig_cqe->status);
3718 	wc->wc_flags |= IB_WC_GRH;
3719 
3720 	nw_type = bnxt_re_check_packet_type(orig_cqe->raweth_qp1_flags,
3721 					    orig_cqe->raweth_qp1_flags2);
3722 	if (nw_type >= 0) {
3723 		wc->network_hdr_type = bnxt_re_to_ib_nw_type(nw_type);
3724 		wc->wc_flags |= IB_WC_WITH_NETWORK_HDR_TYPE;
3725 	}
3726 }
3727 
bnxt_re_process_res_ud_wc(struct bnxt_re_qp * qp,struct ib_wc * wc,struct bnxt_qplib_cqe * cqe)3728 static void bnxt_re_process_res_ud_wc(struct bnxt_re_qp *qp,
3729 				      struct ib_wc *wc,
3730 				      struct bnxt_qplib_cqe *cqe)
3731 {
3732 	struct bnxt_re_dev *rdev;
3733 	u16 vlan_id = 0;
3734 	u8 nw_type;
3735 
3736 	rdev = qp->rdev;
3737 	wc->opcode = IB_WC_RECV;
3738 	wc->status = __rc_to_ib_wc_status(cqe->status);
3739 
3740 	if (cqe->flags & CQ_RES_UD_FLAGS_IMM)
3741 		wc->wc_flags |= IB_WC_WITH_IMM;
3742 	/* report only on GSI QP for Thor */
3743 	if (qp->qplib_qp.type == CMDQ_CREATE_QP_TYPE_GSI) {
3744 		wc->wc_flags |= IB_WC_GRH;
3745 		memcpy(wc->smac, cqe->smac, ETH_ALEN);
3746 		wc->wc_flags |= IB_WC_WITH_SMAC;
3747 		if (cqe->flags & CQ_RES_UD_FLAGS_META_FORMAT_VLAN) {
3748 			vlan_id = (cqe->cfa_meta & 0xFFF);
3749 		}
3750 		/* Mark only if vlan_id is non zero */
3751 		if (vlan_id && bnxt_re_check_if_vlan_valid(rdev, vlan_id)) {
3752 			wc->vlan_id = vlan_id;
3753 			wc->wc_flags |= IB_WC_WITH_VLAN;
3754 		}
3755 		nw_type = (cqe->flags & CQ_RES_UD_FLAGS_ROCE_IP_VER_MASK) >>
3756 			   CQ_RES_UD_FLAGS_ROCE_IP_VER_SFT;
3757 		wc->network_hdr_type = bnxt_re_to_ib_nw_type(nw_type);
3758 		wc->wc_flags |= IB_WC_WITH_NETWORK_HDR_TYPE;
3759 	}
3760 
3761 }
3762 
send_phantom_wqe(struct bnxt_re_qp * qp)3763 static int send_phantom_wqe(struct bnxt_re_qp *qp)
3764 {
3765 	struct bnxt_qplib_qp *lib_qp = &qp->qplib_qp;
3766 	unsigned long flags;
3767 	int rc;
3768 
3769 	spin_lock_irqsave(&qp->sq_lock, flags);
3770 
3771 	rc = bnxt_re_bind_fence_mw(lib_qp);
3772 	if (!rc) {
3773 		lib_qp->sq.phantom_wqe_cnt++;
3774 		ibdev_dbg(&qp->rdev->ibdev,
3775 			  "qp %#x sq->prod %#x sw_prod %#x phantom_wqe_cnt %d\n",
3776 			  lib_qp->id, lib_qp->sq.hwq.prod,
3777 			  HWQ_CMP(lib_qp->sq.hwq.prod, &lib_qp->sq.hwq),
3778 			  lib_qp->sq.phantom_wqe_cnt);
3779 	}
3780 
3781 	spin_unlock_irqrestore(&qp->sq_lock, flags);
3782 	return rc;
3783 }
3784 
bnxt_re_poll_cq(struct ib_cq * ib_cq,int num_entries,struct ib_wc * wc)3785 int bnxt_re_poll_cq(struct ib_cq *ib_cq, int num_entries, struct ib_wc *wc)
3786 {
3787 	struct bnxt_re_cq *cq = container_of(ib_cq, struct bnxt_re_cq, ib_cq);
3788 	struct bnxt_re_qp *qp, *sh_qp;
3789 	struct bnxt_qplib_cqe *cqe;
3790 	int i, ncqe, budget;
3791 	struct bnxt_qplib_q *sq;
3792 	struct bnxt_qplib_qp *lib_qp;
3793 	u32 tbl_idx;
3794 	struct bnxt_re_sqp_entries *sqp_entry = NULL;
3795 	unsigned long flags;
3796 
3797 	/* User CQ; the only processing we do is to
3798 	 * complete any pending CQ resize operation.
3799 	 */
3800 	if (cq->umem) {
3801 		if (cq->resize_umem)
3802 			bnxt_re_resize_cq_complete(cq);
3803 		return 0;
3804 	}
3805 
3806 	spin_lock_irqsave(&cq->cq_lock, flags);
3807 	budget = min_t(u32, num_entries, cq->max_cql);
3808 	num_entries = budget;
3809 	if (!cq->cql) {
3810 		ibdev_err(&cq->rdev->ibdev, "POLL CQ : no CQL to use");
3811 		goto exit;
3812 	}
3813 	cqe = &cq->cql[0];
3814 	while (budget) {
3815 		lib_qp = NULL;
3816 		ncqe = bnxt_qplib_poll_cq(&cq->qplib_cq, cqe, budget, &lib_qp);
3817 		if (lib_qp) {
3818 			sq = &lib_qp->sq;
3819 			if (sq->send_phantom) {
3820 				qp = container_of(lib_qp,
3821 						  struct bnxt_re_qp, qplib_qp);
3822 				if (send_phantom_wqe(qp) == -ENOMEM)
3823 					ibdev_err(&cq->rdev->ibdev,
3824 						  "Phantom failed! Scheduled to send again\n");
3825 				else
3826 					sq->send_phantom = false;
3827 			}
3828 		}
3829 		if (ncqe < budget)
3830 			ncqe += bnxt_qplib_process_flush_list(&cq->qplib_cq,
3831 							      cqe + ncqe,
3832 							      budget - ncqe);
3833 
3834 		if (!ncqe)
3835 			break;
3836 
3837 		for (i = 0; i < ncqe; i++, cqe++) {
3838 			/* Transcribe each qplib_wqe back to ib_wc */
3839 			memset(wc, 0, sizeof(*wc));
3840 
3841 			wc->wr_id = cqe->wr_id;
3842 			wc->byte_len = cqe->length;
3843 			qp = container_of
3844 				((struct bnxt_qplib_qp *)
3845 				 (unsigned long)(cqe->qp_handle),
3846 				 struct bnxt_re_qp, qplib_qp);
3847 			wc->qp = &qp->ib_qp;
3848 			if (cqe->flags & CQ_RES_RC_FLAGS_IMM)
3849 				wc->ex.imm_data = cpu_to_be32(cqe->immdata);
3850 			else
3851 				wc->ex.invalidate_rkey = cqe->invrkey;
3852 			wc->src_qp = cqe->src_qp;
3853 			memcpy(wc->smac, cqe->smac, ETH_ALEN);
3854 			wc->port_num = 1;
3855 			wc->vendor_err = cqe->status;
3856 
3857 			switch (cqe->opcode) {
3858 			case CQ_BASE_CQE_TYPE_REQ:
3859 				sh_qp = qp->rdev->gsi_ctx.gsi_sqp;
3860 				if (sh_qp &&
3861 				    qp->qplib_qp.id == sh_qp->qplib_qp.id) {
3862 					/* Handle this completion with
3863 					 * the stored completion
3864 					 */
3865 					memset(wc, 0, sizeof(*wc));
3866 					continue;
3867 				}
3868 				bnxt_re_process_req_wc(wc, cqe);
3869 				break;
3870 			case CQ_BASE_CQE_TYPE_RES_RAWETH_QP1:
3871 				if (!cqe->status) {
3872 					int rc = 0;
3873 
3874 					rc = bnxt_re_process_raw_qp_pkt_rx
3875 								(qp, cqe);
3876 					if (!rc) {
3877 						memset(wc, 0, sizeof(*wc));
3878 						continue;
3879 					}
3880 					cqe->status = -1;
3881 				}
3882 				/* Errors need not be looped back.
3883 				 * But change the wr_id to the one
3884 				 * stored in the table
3885 				 */
3886 				tbl_idx = cqe->wr_id;
3887 				sqp_entry = &cq->rdev->gsi_ctx.sqp_tbl[tbl_idx];
3888 				wc->wr_id = sqp_entry->wrid;
3889 				bnxt_re_process_res_rawqp1_wc(wc, cqe);
3890 				break;
3891 			case CQ_BASE_CQE_TYPE_RES_RC:
3892 				bnxt_re_process_res_rc_wc(wc, cqe);
3893 				break;
3894 			case CQ_BASE_CQE_TYPE_RES_UD:
3895 				sh_qp = qp->rdev->gsi_ctx.gsi_sqp;
3896 				if (sh_qp &&
3897 				    qp->qplib_qp.id == sh_qp->qplib_qp.id) {
3898 					/* Handle this completion with
3899 					 * the stored completion
3900 					 */
3901 					if (cqe->status) {
3902 						continue;
3903 					} else {
3904 						bnxt_re_process_res_shadow_qp_wc
3905 								(qp, wc, cqe);
3906 						break;
3907 					}
3908 				}
3909 				bnxt_re_process_res_ud_wc(qp, wc, cqe);
3910 				break;
3911 			default:
3912 				ibdev_err(&cq->rdev->ibdev,
3913 					  "POLL CQ : type 0x%x not handled",
3914 					  cqe->opcode);
3915 				continue;
3916 			}
3917 			wc++;
3918 			budget--;
3919 		}
3920 	}
3921 exit:
3922 	spin_unlock_irqrestore(&cq->cq_lock, flags);
3923 	return num_entries - budget;
3924 }
3925 
bnxt_re_req_notify_cq(struct ib_cq * ib_cq,enum ib_cq_notify_flags ib_cqn_flags)3926 int bnxt_re_req_notify_cq(struct ib_cq *ib_cq,
3927 			  enum ib_cq_notify_flags ib_cqn_flags)
3928 {
3929 	struct bnxt_re_cq *cq = container_of(ib_cq, struct bnxt_re_cq, ib_cq);
3930 	int type = 0, rc = 0;
3931 	unsigned long flags;
3932 
3933 	spin_lock_irqsave(&cq->cq_lock, flags);
3934 	/* Trigger on the very next completion */
3935 	if (ib_cqn_flags & IB_CQ_NEXT_COMP)
3936 		type = DBC_DBC_TYPE_CQ_ARMALL;
3937 	/* Trigger on the next solicited completion */
3938 	else if (ib_cqn_flags & IB_CQ_SOLICITED)
3939 		type = DBC_DBC_TYPE_CQ_ARMSE;
3940 
3941 	/* Poll to see if there are missed events */
3942 	if ((ib_cqn_flags & IB_CQ_REPORT_MISSED_EVENTS) &&
3943 	    !(bnxt_qplib_is_cq_empty(&cq->qplib_cq))) {
3944 		rc = 1;
3945 		goto exit;
3946 	}
3947 	bnxt_qplib_req_notify_cq(&cq->qplib_cq, type);
3948 
3949 exit:
3950 	spin_unlock_irqrestore(&cq->cq_lock, flags);
3951 	return rc;
3952 }
3953 
3954 /* Memory Regions */
bnxt_re_get_dma_mr(struct ib_pd * ib_pd,int mr_access_flags)3955 struct ib_mr *bnxt_re_get_dma_mr(struct ib_pd *ib_pd, int mr_access_flags)
3956 {
3957 	struct bnxt_re_pd *pd = container_of(ib_pd, struct bnxt_re_pd, ib_pd);
3958 	struct bnxt_re_dev *rdev = pd->rdev;
3959 	struct bnxt_re_mr *mr;
3960 	u32 active_mrs;
3961 	int rc;
3962 
3963 	mr = kzalloc(sizeof(*mr), GFP_KERNEL);
3964 	if (!mr)
3965 		return ERR_PTR(-ENOMEM);
3966 
3967 	mr->rdev = rdev;
3968 	mr->qplib_mr.pd = &pd->qplib_pd;
3969 	mr->qplib_mr.access_flags = __from_ib_access_flags(mr_access_flags);
3970 	mr->qplib_mr.type = CMDQ_ALLOCATE_MRW_MRW_FLAGS_PMR;
3971 
3972 	if (mr_access_flags & IB_ACCESS_RELAXED_ORDERING)
3973 		bnxt_re_check_and_set_relaxed_ordering(rdev, &mr->qplib_mr);
3974 
3975 	/* Allocate and register 0 as the address */
3976 	rc = bnxt_qplib_alloc_mrw(&rdev->qplib_res, &mr->qplib_mr);
3977 	if (rc)
3978 		goto fail;
3979 
3980 	mr->qplib_mr.hwq.level = PBL_LVL_MAX;
3981 	mr->qplib_mr.total_size = -1; /* Infinte length */
3982 	rc = bnxt_qplib_reg_mr(&rdev->qplib_res, &mr->qplib_mr, NULL, 0,
3983 			       PAGE_SIZE);
3984 	if (rc)
3985 		goto fail_mr;
3986 
3987 	mr->ib_mr.lkey = mr->qplib_mr.lkey;
3988 	if (mr_access_flags & (IB_ACCESS_REMOTE_WRITE | IB_ACCESS_REMOTE_READ |
3989 			       IB_ACCESS_REMOTE_ATOMIC))
3990 		mr->ib_mr.rkey = mr->ib_mr.lkey;
3991 	active_mrs = atomic_inc_return(&rdev->stats.res.mr_count);
3992 	if (active_mrs > rdev->stats.res.mr_watermark)
3993 		rdev->stats.res.mr_watermark = active_mrs;
3994 
3995 	return &mr->ib_mr;
3996 
3997 fail_mr:
3998 	bnxt_qplib_free_mrw(&rdev->qplib_res, &mr->qplib_mr);
3999 fail:
4000 	kfree(mr);
4001 	return ERR_PTR(rc);
4002 }
4003 
bnxt_re_dereg_mr(struct ib_mr * ib_mr,struct ib_udata * udata)4004 int bnxt_re_dereg_mr(struct ib_mr *ib_mr, struct ib_udata *udata)
4005 {
4006 	struct bnxt_re_mr *mr = container_of(ib_mr, struct bnxt_re_mr, ib_mr);
4007 	struct bnxt_re_dev *rdev = mr->rdev;
4008 	int rc;
4009 
4010 	rc = bnxt_qplib_free_mrw(&rdev->qplib_res, &mr->qplib_mr);
4011 	if (rc) {
4012 		ibdev_err(&rdev->ibdev, "Dereg MR failed: %#x\n", rc);
4013 		return rc;
4014 	}
4015 
4016 	if (mr->pages) {
4017 		rc = bnxt_qplib_free_fast_reg_page_list(&rdev->qplib_res,
4018 							&mr->qplib_frpl);
4019 		kfree(mr->pages);
4020 		mr->npages = 0;
4021 		mr->pages = NULL;
4022 	}
4023 	ib_umem_release(mr->ib_umem);
4024 
4025 	kfree(mr);
4026 	atomic_dec(&rdev->stats.res.mr_count);
4027 	return rc;
4028 }
4029 
bnxt_re_set_page(struct ib_mr * ib_mr,u64 addr)4030 static int bnxt_re_set_page(struct ib_mr *ib_mr, u64 addr)
4031 {
4032 	struct bnxt_re_mr *mr = container_of(ib_mr, struct bnxt_re_mr, ib_mr);
4033 
4034 	if (unlikely(mr->npages == mr->qplib_frpl.max_pg_ptrs))
4035 		return -ENOMEM;
4036 
4037 	mr->pages[mr->npages++] = addr;
4038 	return 0;
4039 }
4040 
bnxt_re_map_mr_sg(struct ib_mr * ib_mr,struct scatterlist * sg,int sg_nents,unsigned int * sg_offset)4041 int bnxt_re_map_mr_sg(struct ib_mr *ib_mr, struct scatterlist *sg, int sg_nents,
4042 		      unsigned int *sg_offset)
4043 {
4044 	struct bnxt_re_mr *mr = container_of(ib_mr, struct bnxt_re_mr, ib_mr);
4045 
4046 	mr->npages = 0;
4047 	return ib_sg_to_pages(ib_mr, sg, sg_nents, sg_offset, bnxt_re_set_page);
4048 }
4049 
bnxt_re_alloc_mr(struct ib_pd * ib_pd,enum ib_mr_type type,u32 max_num_sg)4050 struct ib_mr *bnxt_re_alloc_mr(struct ib_pd *ib_pd, enum ib_mr_type type,
4051 			       u32 max_num_sg)
4052 {
4053 	struct bnxt_re_pd *pd = container_of(ib_pd, struct bnxt_re_pd, ib_pd);
4054 	struct bnxt_re_dev *rdev = pd->rdev;
4055 	struct bnxt_re_mr *mr = NULL;
4056 	u32 active_mrs;
4057 	int rc;
4058 
4059 	if (type != IB_MR_TYPE_MEM_REG) {
4060 		ibdev_dbg(&rdev->ibdev, "MR type 0x%x not supported", type);
4061 		return ERR_PTR(-EINVAL);
4062 	}
4063 	if (max_num_sg > MAX_PBL_LVL_1_PGS)
4064 		return ERR_PTR(-EINVAL);
4065 
4066 	mr = kzalloc(sizeof(*mr), GFP_KERNEL);
4067 	if (!mr)
4068 		return ERR_PTR(-ENOMEM);
4069 
4070 	mr->rdev = rdev;
4071 	mr->qplib_mr.pd = &pd->qplib_pd;
4072 	mr->qplib_mr.access_flags = BNXT_QPLIB_FR_PMR;
4073 	mr->qplib_mr.type = CMDQ_ALLOCATE_MRW_MRW_FLAGS_PMR;
4074 
4075 	rc = bnxt_qplib_alloc_mrw(&rdev->qplib_res, &mr->qplib_mr);
4076 	if (rc)
4077 		goto bail;
4078 
4079 	mr->ib_mr.lkey = mr->qplib_mr.lkey;
4080 	mr->ib_mr.rkey = mr->ib_mr.lkey;
4081 
4082 	mr->pages = kcalloc(max_num_sg, sizeof(u64), GFP_KERNEL);
4083 	if (!mr->pages) {
4084 		rc = -ENOMEM;
4085 		goto fail;
4086 	}
4087 	rc = bnxt_qplib_alloc_fast_reg_page_list(&rdev->qplib_res,
4088 						 &mr->qplib_frpl, max_num_sg);
4089 	if (rc) {
4090 		ibdev_err(&rdev->ibdev,
4091 			  "Failed to allocate HW FR page list");
4092 		goto fail_mr;
4093 	}
4094 
4095 	active_mrs = atomic_inc_return(&rdev->stats.res.mr_count);
4096 	if (active_mrs > rdev->stats.res.mr_watermark)
4097 		rdev->stats.res.mr_watermark = active_mrs;
4098 	return &mr->ib_mr;
4099 
4100 fail_mr:
4101 	kfree(mr->pages);
4102 fail:
4103 	bnxt_qplib_free_mrw(&rdev->qplib_res, &mr->qplib_mr);
4104 bail:
4105 	kfree(mr);
4106 	return ERR_PTR(rc);
4107 }
4108 
bnxt_re_alloc_mw(struct ib_pd * ib_pd,enum ib_mw_type type,struct ib_udata * udata)4109 struct ib_mw *bnxt_re_alloc_mw(struct ib_pd *ib_pd, enum ib_mw_type type,
4110 			       struct ib_udata *udata)
4111 {
4112 	struct bnxt_re_pd *pd = container_of(ib_pd, struct bnxt_re_pd, ib_pd);
4113 	struct bnxt_re_dev *rdev = pd->rdev;
4114 	struct bnxt_re_mw *mw;
4115 	u32 active_mws;
4116 	int rc;
4117 
4118 	mw = kzalloc(sizeof(*mw), GFP_KERNEL);
4119 	if (!mw)
4120 		return ERR_PTR(-ENOMEM);
4121 	mw->rdev = rdev;
4122 	mw->qplib_mw.pd = &pd->qplib_pd;
4123 
4124 	mw->qplib_mw.type = (type == IB_MW_TYPE_1 ?
4125 			       CMDQ_ALLOCATE_MRW_MRW_FLAGS_MW_TYPE1 :
4126 			       CMDQ_ALLOCATE_MRW_MRW_FLAGS_MW_TYPE2B);
4127 	rc = bnxt_qplib_alloc_mrw(&rdev->qplib_res, &mw->qplib_mw);
4128 	if (rc) {
4129 		ibdev_err(&rdev->ibdev, "Allocate MW failed!");
4130 		goto fail;
4131 	}
4132 	mw->ib_mw.rkey = mw->qplib_mw.rkey;
4133 
4134 	active_mws = atomic_inc_return(&rdev->stats.res.mw_count);
4135 	if (active_mws > rdev->stats.res.mw_watermark)
4136 		rdev->stats.res.mw_watermark = active_mws;
4137 	return &mw->ib_mw;
4138 
4139 fail:
4140 	kfree(mw);
4141 	return ERR_PTR(rc);
4142 }
4143 
bnxt_re_dealloc_mw(struct ib_mw * ib_mw)4144 int bnxt_re_dealloc_mw(struct ib_mw *ib_mw)
4145 {
4146 	struct bnxt_re_mw *mw = container_of(ib_mw, struct bnxt_re_mw, ib_mw);
4147 	struct bnxt_re_dev *rdev = mw->rdev;
4148 	int rc;
4149 
4150 	rc = bnxt_qplib_free_mrw(&rdev->qplib_res, &mw->qplib_mw);
4151 	if (rc) {
4152 		ibdev_err(&rdev->ibdev, "Free MW failed: %#x\n", rc);
4153 		return rc;
4154 	}
4155 
4156 	kfree(mw);
4157 	atomic_dec(&rdev->stats.res.mw_count);
4158 	return rc;
4159 }
4160 
__bnxt_re_user_reg_mr(struct ib_pd * ib_pd,u64 length,u64 virt_addr,int mr_access_flags,struct ib_umem * umem)4161 static struct ib_mr *__bnxt_re_user_reg_mr(struct ib_pd *ib_pd, u64 length, u64 virt_addr,
4162 					   int mr_access_flags, struct ib_umem *umem)
4163 {
4164 	struct bnxt_re_pd *pd = container_of(ib_pd, struct bnxt_re_pd, ib_pd);
4165 	struct bnxt_re_dev *rdev = pd->rdev;
4166 	unsigned long page_size;
4167 	struct bnxt_re_mr *mr;
4168 	int umem_pgs, rc;
4169 	u32 active_mrs;
4170 
4171 	if (length > BNXT_RE_MAX_MR_SIZE) {
4172 		ibdev_err(&rdev->ibdev, "MR Size: %lld > Max supported:%lld\n",
4173 			  length, BNXT_RE_MAX_MR_SIZE);
4174 		return ERR_PTR(-ENOMEM);
4175 	}
4176 
4177 	page_size = ib_umem_find_best_pgsz(umem, BNXT_RE_PAGE_SIZE_SUPPORTED, virt_addr);
4178 	if (!page_size) {
4179 		ibdev_err(&rdev->ibdev, "umem page size unsupported!");
4180 		return ERR_PTR(-EINVAL);
4181 	}
4182 
4183 	mr = kzalloc(sizeof(*mr), GFP_KERNEL);
4184 	if (!mr)
4185 		return ERR_PTR(-ENOMEM);
4186 
4187 	mr->rdev = rdev;
4188 	mr->qplib_mr.pd = &pd->qplib_pd;
4189 	mr->qplib_mr.access_flags = __from_ib_access_flags(mr_access_flags);
4190 	mr->qplib_mr.type = CMDQ_ALLOCATE_MRW_MRW_FLAGS_MR;
4191 
4192 	if (!_is_alloc_mr_unified(rdev->dev_attr->dev_cap_flags)) {
4193 		rc = bnxt_qplib_alloc_mrw(&rdev->qplib_res, &mr->qplib_mr);
4194 		if (rc) {
4195 			ibdev_err(&rdev->ibdev, "Failed to allocate MR rc = %d", rc);
4196 			rc = -EIO;
4197 			goto free_mr;
4198 		}
4199 		/* The fixed portion of the rkey is the same as the lkey */
4200 		mr->ib_mr.rkey = mr->qplib_mr.rkey;
4201 	} else {
4202 		mr->qplib_mr.flags = CMDQ_REGISTER_MR_FLAGS_ALLOC_MR;
4203 	}
4204 	mr->ib_umem = umem;
4205 	mr->qplib_mr.va = virt_addr;
4206 	mr->qplib_mr.total_size = length;
4207 
4208 	if (mr_access_flags & IB_ACCESS_RELAXED_ORDERING)
4209 		bnxt_re_check_and_set_relaxed_ordering(rdev, &mr->qplib_mr);
4210 
4211 	umem_pgs = ib_umem_num_dma_blocks(umem, page_size);
4212 	rc = bnxt_qplib_reg_mr(&rdev->qplib_res, &mr->qplib_mr, umem,
4213 			       umem_pgs, page_size);
4214 	if (rc) {
4215 		ibdev_err(&rdev->ibdev, "Failed to register user MR - rc = %d\n", rc);
4216 		rc = -EIO;
4217 		goto free_mrw;
4218 	}
4219 
4220 	mr->ib_mr.lkey = mr->qplib_mr.lkey;
4221 	mr->ib_mr.rkey = mr->qplib_mr.lkey;
4222 	active_mrs = atomic_inc_return(&rdev->stats.res.mr_count);
4223 	if (active_mrs > rdev->stats.res.mr_watermark)
4224 		rdev->stats.res.mr_watermark = active_mrs;
4225 
4226 	return &mr->ib_mr;
4227 
4228 free_mrw:
4229 	bnxt_qplib_free_mrw(&rdev->qplib_res, &mr->qplib_mr);
4230 free_mr:
4231 	kfree(mr);
4232 	return ERR_PTR(rc);
4233 }
4234 
bnxt_re_reg_user_mr(struct ib_pd * ib_pd,u64 start,u64 length,u64 virt_addr,int mr_access_flags,struct ib_udata * udata)4235 struct ib_mr *bnxt_re_reg_user_mr(struct ib_pd *ib_pd, u64 start, u64 length,
4236 				  u64 virt_addr, int mr_access_flags,
4237 				  struct ib_udata *udata)
4238 {
4239 	struct bnxt_re_pd *pd = container_of(ib_pd, struct bnxt_re_pd, ib_pd);
4240 	struct bnxt_re_dev *rdev = pd->rdev;
4241 	struct ib_umem *umem;
4242 	struct ib_mr *ib_mr;
4243 
4244 	umem = ib_umem_get(&rdev->ibdev, start, length, mr_access_flags);
4245 	if (IS_ERR(umem))
4246 		return ERR_CAST(umem);
4247 
4248 	ib_mr = __bnxt_re_user_reg_mr(ib_pd, length, virt_addr, mr_access_flags, umem);
4249 	if (IS_ERR(ib_mr))
4250 		ib_umem_release(umem);
4251 	return ib_mr;
4252 }
4253 
bnxt_re_reg_user_mr_dmabuf(struct ib_pd * ib_pd,u64 start,u64 length,u64 virt_addr,int fd,int mr_access_flags,struct uverbs_attr_bundle * attrs)4254 struct ib_mr *bnxt_re_reg_user_mr_dmabuf(struct ib_pd *ib_pd, u64 start,
4255 					 u64 length, u64 virt_addr, int fd,
4256 					 int mr_access_flags,
4257 					 struct uverbs_attr_bundle *attrs)
4258 {
4259 	struct bnxt_re_pd *pd = container_of(ib_pd, struct bnxt_re_pd, ib_pd);
4260 	struct bnxt_re_dev *rdev = pd->rdev;
4261 	struct ib_umem_dmabuf *umem_dmabuf;
4262 	struct ib_umem *umem;
4263 	struct ib_mr *ib_mr;
4264 
4265 	umem_dmabuf = ib_umem_dmabuf_get_pinned(&rdev->ibdev, start, length,
4266 						fd, mr_access_flags);
4267 	if (IS_ERR(umem_dmabuf))
4268 		return ERR_CAST(umem_dmabuf);
4269 
4270 	umem = &umem_dmabuf->umem;
4271 
4272 	ib_mr = __bnxt_re_user_reg_mr(ib_pd, length, virt_addr, mr_access_flags, umem);
4273 	if (IS_ERR(ib_mr))
4274 		ib_umem_release(umem);
4275 	return ib_mr;
4276 }
4277 
bnxt_re_alloc_ucontext(struct ib_ucontext * ctx,struct ib_udata * udata)4278 int bnxt_re_alloc_ucontext(struct ib_ucontext *ctx, struct ib_udata *udata)
4279 {
4280 	struct ib_device *ibdev = ctx->device;
4281 	struct bnxt_re_ucontext *uctx =
4282 		container_of(ctx, struct bnxt_re_ucontext, ib_uctx);
4283 	struct bnxt_re_dev *rdev = to_bnxt_re_dev(ibdev, ibdev);
4284 	struct bnxt_qplib_dev_attr *dev_attr = rdev->dev_attr;
4285 	struct bnxt_re_user_mmap_entry *entry;
4286 	struct bnxt_re_uctx_resp resp = {};
4287 	struct bnxt_re_uctx_req ureq = {};
4288 	u32 chip_met_rev_num = 0;
4289 	int rc;
4290 
4291 	ibdev_dbg(ibdev, "ABI version requested %u", ibdev->ops.uverbs_abi_ver);
4292 
4293 	if (ibdev->ops.uverbs_abi_ver != BNXT_RE_ABI_VERSION) {
4294 		ibdev_dbg(ibdev, " is different from the device %d ",
4295 			  BNXT_RE_ABI_VERSION);
4296 		return -EPERM;
4297 	}
4298 
4299 	uctx->rdev = rdev;
4300 
4301 	uctx->shpg = (void *)__get_free_page(GFP_KERNEL);
4302 	if (!uctx->shpg) {
4303 		rc = -ENOMEM;
4304 		goto fail;
4305 	}
4306 	spin_lock_init(&uctx->sh_lock);
4307 
4308 	resp.comp_mask = BNXT_RE_UCNTX_CMASK_HAVE_CCTX;
4309 	chip_met_rev_num = rdev->chip_ctx->chip_num;
4310 	chip_met_rev_num |= ((u32)rdev->chip_ctx->chip_rev & 0xFF) <<
4311 			     BNXT_RE_CHIP_ID0_CHIP_REV_SFT;
4312 	chip_met_rev_num |= ((u32)rdev->chip_ctx->chip_metal & 0xFF) <<
4313 			     BNXT_RE_CHIP_ID0_CHIP_MET_SFT;
4314 	resp.chip_id0 = chip_met_rev_num;
4315 	/*Temp, Use xa_alloc instead */
4316 	resp.dev_id = rdev->en_dev->pdev->devfn;
4317 	resp.max_qp = rdev->qplib_ctx.qpc_count;
4318 	resp.pg_size = PAGE_SIZE;
4319 	resp.cqe_sz = sizeof(struct cq_base);
4320 	resp.max_cqd = dev_attr->max_cq_wqes;
4321 
4322 	if (rdev->chip_ctx->modes.db_push)
4323 		resp.comp_mask |= BNXT_RE_UCNTX_CMASK_WC_DPI_ENABLED;
4324 
4325 	entry = bnxt_re_mmap_entry_insert(uctx, 0, BNXT_RE_MMAP_SH_PAGE, NULL);
4326 	if (!entry) {
4327 		rc = -ENOMEM;
4328 		goto cfail;
4329 	}
4330 	uctx->shpage_mmap = &entry->rdma_entry;
4331 	if (rdev->pacing.dbr_pacing)
4332 		resp.comp_mask |= BNXT_RE_UCNTX_CMASK_DBR_PACING_ENABLED;
4333 
4334 	if (_is_host_msn_table(rdev->qplib_res.dattr->dev_cap_flags2))
4335 		resp.comp_mask |= BNXT_RE_UCNTX_CMASK_MSN_TABLE_ENABLED;
4336 
4337 	if (udata->inlen >= sizeof(ureq)) {
4338 		rc = ib_copy_from_udata(&ureq, udata, min(udata->inlen, sizeof(ureq)));
4339 		if (rc)
4340 			goto cfail;
4341 		if (ureq.comp_mask & BNXT_RE_COMP_MASK_REQ_UCNTX_POW2_SUPPORT) {
4342 			resp.comp_mask |= BNXT_RE_UCNTX_CMASK_POW2_DISABLED;
4343 			uctx->cmask |= BNXT_RE_UCNTX_CAP_POW2_DISABLED;
4344 		}
4345 		if (ureq.comp_mask & BNXT_RE_COMP_MASK_REQ_UCNTX_VAR_WQE_SUPPORT) {
4346 			resp.comp_mask |= BNXT_RE_UCNTX_CMASK_HAVE_MODE;
4347 			resp.mode = rdev->chip_ctx->modes.wqe_mode;
4348 			if (resp.mode == BNXT_QPLIB_WQE_MODE_VARIABLE)
4349 				uctx->cmask |= BNXT_RE_UCNTX_CAP_VAR_WQE_ENABLED;
4350 		}
4351 	}
4352 
4353 	rc = ib_copy_to_udata(udata, &resp, min(udata->outlen, sizeof(resp)));
4354 	if (rc) {
4355 		ibdev_err(ibdev, "Failed to copy user context");
4356 		rc = -EFAULT;
4357 		goto cfail;
4358 	}
4359 
4360 	return 0;
4361 cfail:
4362 	free_page((unsigned long)uctx->shpg);
4363 	uctx->shpg = NULL;
4364 fail:
4365 	return rc;
4366 }
4367 
bnxt_re_dealloc_ucontext(struct ib_ucontext * ib_uctx)4368 void bnxt_re_dealloc_ucontext(struct ib_ucontext *ib_uctx)
4369 {
4370 	struct bnxt_re_ucontext *uctx = container_of(ib_uctx,
4371 						   struct bnxt_re_ucontext,
4372 						   ib_uctx);
4373 
4374 	struct bnxt_re_dev *rdev = uctx->rdev;
4375 
4376 	rdma_user_mmap_entry_remove(uctx->shpage_mmap);
4377 	uctx->shpage_mmap = NULL;
4378 	if (uctx->shpg)
4379 		free_page((unsigned long)uctx->shpg);
4380 
4381 	if (uctx->dpi.dbr) {
4382 		/* Free DPI only if this is the first PD allocated by the
4383 		 * application and mark the context dpi as NULL
4384 		 */
4385 		bnxt_qplib_dealloc_dpi(&rdev->qplib_res, &uctx->dpi);
4386 		uctx->dpi.dbr = NULL;
4387 	}
4388 }
4389 
bnxt_re_search_for_cq(struct bnxt_re_dev * rdev,u32 cq_id)4390 static struct bnxt_re_cq *bnxt_re_search_for_cq(struct bnxt_re_dev *rdev, u32 cq_id)
4391 {
4392 	struct bnxt_re_cq *cq = NULL, *tmp_cq;
4393 
4394 	hash_for_each_possible(rdev->cq_hash, tmp_cq, hash_entry, cq_id) {
4395 		if (tmp_cq->qplib_cq.id == cq_id) {
4396 			cq = tmp_cq;
4397 			break;
4398 		}
4399 	}
4400 	return cq;
4401 }
4402 
bnxt_re_search_for_srq(struct bnxt_re_dev * rdev,u32 srq_id)4403 static struct bnxt_re_srq *bnxt_re_search_for_srq(struct bnxt_re_dev *rdev, u32 srq_id)
4404 {
4405 	struct bnxt_re_srq *srq = NULL, *tmp_srq;
4406 
4407 	hash_for_each_possible(rdev->srq_hash, tmp_srq, hash_entry, srq_id) {
4408 		if (tmp_srq->qplib_srq.id == srq_id) {
4409 			srq = tmp_srq;
4410 			break;
4411 		}
4412 	}
4413 	return srq;
4414 }
4415 
4416 /* Helper function to mmap the virtual memory from user app */
bnxt_re_mmap(struct ib_ucontext * ib_uctx,struct vm_area_struct * vma)4417 int bnxt_re_mmap(struct ib_ucontext *ib_uctx, struct vm_area_struct *vma)
4418 {
4419 	struct bnxt_re_ucontext *uctx = container_of(ib_uctx,
4420 						   struct bnxt_re_ucontext,
4421 						   ib_uctx);
4422 	struct bnxt_re_user_mmap_entry *bnxt_entry;
4423 	struct rdma_user_mmap_entry *rdma_entry;
4424 	int ret = 0;
4425 	u64 pfn;
4426 
4427 	rdma_entry = rdma_user_mmap_entry_get(&uctx->ib_uctx, vma);
4428 	if (!rdma_entry)
4429 		return -EINVAL;
4430 
4431 	bnxt_entry = container_of(rdma_entry, struct bnxt_re_user_mmap_entry,
4432 				  rdma_entry);
4433 
4434 	switch (bnxt_entry->mmap_flag) {
4435 	case BNXT_RE_MMAP_WC_DB:
4436 		pfn = bnxt_entry->mem_offset >> PAGE_SHIFT;
4437 		ret = rdma_user_mmap_io(ib_uctx, vma, pfn, PAGE_SIZE,
4438 					pgprot_writecombine(vma->vm_page_prot),
4439 					rdma_entry);
4440 		break;
4441 	case BNXT_RE_MMAP_UC_DB:
4442 		pfn = bnxt_entry->mem_offset >> PAGE_SHIFT;
4443 		ret = rdma_user_mmap_io(ib_uctx, vma, pfn, PAGE_SIZE,
4444 					pgprot_noncached(vma->vm_page_prot),
4445 				rdma_entry);
4446 		break;
4447 	case BNXT_RE_MMAP_SH_PAGE:
4448 		ret = vm_insert_page(vma, vma->vm_start, virt_to_page(uctx->shpg));
4449 		break;
4450 	case BNXT_RE_MMAP_DBR_BAR:
4451 		pfn = bnxt_entry->mem_offset >> PAGE_SHIFT;
4452 		ret = rdma_user_mmap_io(ib_uctx, vma, pfn, PAGE_SIZE,
4453 					pgprot_noncached(vma->vm_page_prot),
4454 					rdma_entry);
4455 		break;
4456 	case BNXT_RE_MMAP_DBR_PAGE:
4457 	case BNXT_RE_MMAP_TOGGLE_PAGE:
4458 		/* Driver doesn't expect write access for user space */
4459 		if (vma->vm_flags & VM_WRITE)
4460 			ret = -EFAULT;
4461 		else
4462 			ret = vm_insert_page(vma, vma->vm_start,
4463 					     virt_to_page((void *)bnxt_entry->mem_offset));
4464 		break;
4465 	default:
4466 		ret = -EINVAL;
4467 		break;
4468 	}
4469 
4470 	rdma_user_mmap_entry_put(rdma_entry);
4471 	return ret;
4472 }
4473 
bnxt_re_mmap_free(struct rdma_user_mmap_entry * rdma_entry)4474 void bnxt_re_mmap_free(struct rdma_user_mmap_entry *rdma_entry)
4475 {
4476 	struct bnxt_re_user_mmap_entry *bnxt_entry;
4477 
4478 	bnxt_entry = container_of(rdma_entry, struct bnxt_re_user_mmap_entry,
4479 				  rdma_entry);
4480 
4481 	kfree(bnxt_entry);
4482 }
4483 
UVERBS_HANDLER(BNXT_RE_METHOD_NOTIFY_DRV)4484 static int UVERBS_HANDLER(BNXT_RE_METHOD_NOTIFY_DRV)(struct uverbs_attr_bundle *attrs)
4485 {
4486 	struct bnxt_re_ucontext *uctx;
4487 
4488 	uctx = container_of(ib_uverbs_get_ucontext(attrs), struct bnxt_re_ucontext, ib_uctx);
4489 	bnxt_re_pacing_alert(uctx->rdev);
4490 	return 0;
4491 }
4492 
UVERBS_HANDLER(BNXT_RE_METHOD_ALLOC_PAGE)4493 static int UVERBS_HANDLER(BNXT_RE_METHOD_ALLOC_PAGE)(struct uverbs_attr_bundle *attrs)
4494 {
4495 	struct ib_uobject *uobj = uverbs_attr_get_uobject(attrs, BNXT_RE_ALLOC_PAGE_HANDLE);
4496 	enum bnxt_re_alloc_page_type alloc_type;
4497 	struct bnxt_re_user_mmap_entry *entry;
4498 	enum bnxt_re_mmap_flag mmap_flag;
4499 	struct bnxt_qplib_chip_ctx *cctx;
4500 	struct bnxt_re_ucontext *uctx;
4501 	struct bnxt_re_dev *rdev;
4502 	u64 mmap_offset;
4503 	u32 length;
4504 	u32 dpi;
4505 	u64 addr;
4506 	int err;
4507 
4508 	uctx = container_of(ib_uverbs_get_ucontext(attrs), struct bnxt_re_ucontext, ib_uctx);
4509 	if (IS_ERR(uctx))
4510 		return PTR_ERR(uctx);
4511 
4512 	err = uverbs_get_const(&alloc_type, attrs, BNXT_RE_ALLOC_PAGE_TYPE);
4513 	if (err)
4514 		return err;
4515 
4516 	rdev = uctx->rdev;
4517 	cctx = rdev->chip_ctx;
4518 
4519 	switch (alloc_type) {
4520 	case BNXT_RE_ALLOC_WC_PAGE:
4521 		if (cctx->modes.db_push)  {
4522 			if (bnxt_qplib_alloc_dpi(&rdev->qplib_res, &uctx->wcdpi,
4523 						 uctx, BNXT_QPLIB_DPI_TYPE_WC))
4524 				return -ENOMEM;
4525 			length = PAGE_SIZE;
4526 			dpi = uctx->wcdpi.dpi;
4527 			addr = (u64)uctx->wcdpi.umdbr;
4528 			mmap_flag = BNXT_RE_MMAP_WC_DB;
4529 		} else {
4530 			return -EINVAL;
4531 		}
4532 
4533 		break;
4534 	case BNXT_RE_ALLOC_DBR_BAR_PAGE:
4535 		length = PAGE_SIZE;
4536 		addr = (u64)rdev->pacing.dbr_bar_addr;
4537 		mmap_flag = BNXT_RE_MMAP_DBR_BAR;
4538 		break;
4539 
4540 	case BNXT_RE_ALLOC_DBR_PAGE:
4541 		length = PAGE_SIZE;
4542 		addr = (u64)rdev->pacing.dbr_page;
4543 		mmap_flag = BNXT_RE_MMAP_DBR_PAGE;
4544 		break;
4545 
4546 	default:
4547 		return -EOPNOTSUPP;
4548 	}
4549 
4550 	entry = bnxt_re_mmap_entry_insert(uctx, addr, mmap_flag, &mmap_offset);
4551 	if (!entry)
4552 		return -ENOMEM;
4553 
4554 	uobj->object = entry;
4555 	uverbs_finalize_uobj_create(attrs, BNXT_RE_ALLOC_PAGE_HANDLE);
4556 	err = uverbs_copy_to(attrs, BNXT_RE_ALLOC_PAGE_MMAP_OFFSET,
4557 			     &mmap_offset, sizeof(mmap_offset));
4558 	if (err)
4559 		return err;
4560 
4561 	err = uverbs_copy_to(attrs, BNXT_RE_ALLOC_PAGE_MMAP_LENGTH,
4562 			     &length, sizeof(length));
4563 	if (err)
4564 		return err;
4565 
4566 	err = uverbs_copy_to(attrs, BNXT_RE_ALLOC_PAGE_DPI,
4567 			     &dpi, sizeof(length));
4568 	if (err)
4569 		return err;
4570 
4571 	return 0;
4572 }
4573 
alloc_page_obj_cleanup(struct ib_uobject * uobject,enum rdma_remove_reason why,struct uverbs_attr_bundle * attrs)4574 static int alloc_page_obj_cleanup(struct ib_uobject *uobject,
4575 				  enum rdma_remove_reason why,
4576 			    struct uverbs_attr_bundle *attrs)
4577 {
4578 	struct  bnxt_re_user_mmap_entry *entry = uobject->object;
4579 	struct bnxt_re_ucontext *uctx = entry->uctx;
4580 
4581 	switch (entry->mmap_flag) {
4582 	case BNXT_RE_MMAP_WC_DB:
4583 		if (uctx && uctx->wcdpi.dbr) {
4584 			struct bnxt_re_dev *rdev = uctx->rdev;
4585 
4586 			bnxt_qplib_dealloc_dpi(&rdev->qplib_res, &uctx->wcdpi);
4587 			uctx->wcdpi.dbr = NULL;
4588 		}
4589 		break;
4590 	case BNXT_RE_MMAP_DBR_BAR:
4591 	case BNXT_RE_MMAP_DBR_PAGE:
4592 		break;
4593 	default:
4594 		goto exit;
4595 	}
4596 	rdma_user_mmap_entry_remove(&entry->rdma_entry);
4597 exit:
4598 	return 0;
4599 }
4600 
4601 DECLARE_UVERBS_NAMED_METHOD(BNXT_RE_METHOD_ALLOC_PAGE,
4602 			    UVERBS_ATTR_IDR(BNXT_RE_ALLOC_PAGE_HANDLE,
4603 					    BNXT_RE_OBJECT_ALLOC_PAGE,
4604 					    UVERBS_ACCESS_NEW,
4605 					    UA_MANDATORY),
4606 			    UVERBS_ATTR_CONST_IN(BNXT_RE_ALLOC_PAGE_TYPE,
4607 						 enum bnxt_re_alloc_page_type,
4608 						 UA_MANDATORY),
4609 			    UVERBS_ATTR_PTR_OUT(BNXT_RE_ALLOC_PAGE_MMAP_OFFSET,
4610 						UVERBS_ATTR_TYPE(u64),
4611 						UA_MANDATORY),
4612 			    UVERBS_ATTR_PTR_OUT(BNXT_RE_ALLOC_PAGE_MMAP_LENGTH,
4613 						UVERBS_ATTR_TYPE(u32),
4614 						UA_MANDATORY),
4615 			    UVERBS_ATTR_PTR_OUT(BNXT_RE_ALLOC_PAGE_DPI,
4616 						UVERBS_ATTR_TYPE(u32),
4617 						UA_MANDATORY));
4618 
4619 DECLARE_UVERBS_NAMED_METHOD_DESTROY(BNXT_RE_METHOD_DESTROY_PAGE,
4620 				    UVERBS_ATTR_IDR(BNXT_RE_DESTROY_PAGE_HANDLE,
4621 						    BNXT_RE_OBJECT_ALLOC_PAGE,
4622 						    UVERBS_ACCESS_DESTROY,
4623 						    UA_MANDATORY));
4624 
4625 DECLARE_UVERBS_NAMED_OBJECT(BNXT_RE_OBJECT_ALLOC_PAGE,
4626 			    UVERBS_TYPE_ALLOC_IDR(alloc_page_obj_cleanup),
4627 			    &UVERBS_METHOD(BNXT_RE_METHOD_ALLOC_PAGE),
4628 			    &UVERBS_METHOD(BNXT_RE_METHOD_DESTROY_PAGE));
4629 
4630 DECLARE_UVERBS_NAMED_METHOD(BNXT_RE_METHOD_NOTIFY_DRV);
4631 
4632 DECLARE_UVERBS_GLOBAL_METHODS(BNXT_RE_OBJECT_NOTIFY_DRV,
4633 			      &UVERBS_METHOD(BNXT_RE_METHOD_NOTIFY_DRV));
4634 
4635 /* Toggle MEM */
UVERBS_HANDLER(BNXT_RE_METHOD_GET_TOGGLE_MEM)4636 static int UVERBS_HANDLER(BNXT_RE_METHOD_GET_TOGGLE_MEM)(struct uverbs_attr_bundle *attrs)
4637 {
4638 	struct ib_uobject *uobj = uverbs_attr_get_uobject(attrs, BNXT_RE_TOGGLE_MEM_HANDLE);
4639 	enum bnxt_re_mmap_flag mmap_flag = BNXT_RE_MMAP_TOGGLE_PAGE;
4640 	enum bnxt_re_get_toggle_mem_type res_type;
4641 	struct bnxt_re_user_mmap_entry *entry;
4642 	struct bnxt_re_ucontext *uctx;
4643 	struct ib_ucontext *ib_uctx;
4644 	struct bnxt_re_dev *rdev;
4645 	struct bnxt_re_srq *srq;
4646 	u32 length = PAGE_SIZE;
4647 	struct bnxt_re_cq *cq;
4648 	u64 mem_offset;
4649 	u32 offset = 0;
4650 	u64 addr = 0;
4651 	u32 res_id;
4652 	int err;
4653 
4654 	ib_uctx = ib_uverbs_get_ucontext(attrs);
4655 	if (IS_ERR(ib_uctx))
4656 		return PTR_ERR(ib_uctx);
4657 
4658 	err = uverbs_get_const(&res_type, attrs, BNXT_RE_TOGGLE_MEM_TYPE);
4659 	if (err)
4660 		return err;
4661 
4662 	uctx = container_of(ib_uctx, struct bnxt_re_ucontext, ib_uctx);
4663 	rdev = uctx->rdev;
4664 	err = uverbs_copy_from(&res_id, attrs, BNXT_RE_TOGGLE_MEM_RES_ID);
4665 	if (err)
4666 		return err;
4667 
4668 	switch (res_type) {
4669 	case BNXT_RE_CQ_TOGGLE_MEM:
4670 		cq = bnxt_re_search_for_cq(rdev, res_id);
4671 		if (!cq)
4672 			return -EINVAL;
4673 
4674 		addr = (u64)cq->uctx_cq_page;
4675 		break;
4676 	case BNXT_RE_SRQ_TOGGLE_MEM:
4677 		srq = bnxt_re_search_for_srq(rdev, res_id);
4678 		if (!srq)
4679 			return -EINVAL;
4680 
4681 		addr = (u64)srq->uctx_srq_page;
4682 		break;
4683 
4684 	default:
4685 		return -EOPNOTSUPP;
4686 	}
4687 
4688 	entry = bnxt_re_mmap_entry_insert(uctx, addr, mmap_flag, &mem_offset);
4689 	if (!entry)
4690 		return -ENOMEM;
4691 
4692 	uobj->object = entry;
4693 	uverbs_finalize_uobj_create(attrs, BNXT_RE_TOGGLE_MEM_HANDLE);
4694 	err = uverbs_copy_to(attrs, BNXT_RE_TOGGLE_MEM_MMAP_PAGE,
4695 			     &mem_offset, sizeof(mem_offset));
4696 	if (err)
4697 		return err;
4698 
4699 	err = uverbs_copy_to(attrs, BNXT_RE_TOGGLE_MEM_MMAP_LENGTH,
4700 			     &length, sizeof(length));
4701 	if (err)
4702 		return err;
4703 
4704 	err = uverbs_copy_to(attrs, BNXT_RE_TOGGLE_MEM_MMAP_OFFSET,
4705 			     &offset, sizeof(length));
4706 	if (err)
4707 		return err;
4708 
4709 	return 0;
4710 }
4711 
get_toggle_mem_obj_cleanup(struct ib_uobject * uobject,enum rdma_remove_reason why,struct uverbs_attr_bundle * attrs)4712 static int get_toggle_mem_obj_cleanup(struct ib_uobject *uobject,
4713 				      enum rdma_remove_reason why,
4714 				      struct uverbs_attr_bundle *attrs)
4715 {
4716 	struct  bnxt_re_user_mmap_entry *entry = uobject->object;
4717 
4718 	rdma_user_mmap_entry_remove(&entry->rdma_entry);
4719 	return 0;
4720 }
4721 
4722 DECLARE_UVERBS_NAMED_METHOD(BNXT_RE_METHOD_GET_TOGGLE_MEM,
4723 			    UVERBS_ATTR_IDR(BNXT_RE_TOGGLE_MEM_HANDLE,
4724 					    BNXT_RE_OBJECT_GET_TOGGLE_MEM,
4725 					    UVERBS_ACCESS_NEW,
4726 					    UA_MANDATORY),
4727 			    UVERBS_ATTR_CONST_IN(BNXT_RE_TOGGLE_MEM_TYPE,
4728 						 enum bnxt_re_get_toggle_mem_type,
4729 						 UA_MANDATORY),
4730 			    UVERBS_ATTR_PTR_IN(BNXT_RE_TOGGLE_MEM_RES_ID,
4731 					       UVERBS_ATTR_TYPE(u32),
4732 					       UA_MANDATORY),
4733 			    UVERBS_ATTR_PTR_OUT(BNXT_RE_TOGGLE_MEM_MMAP_PAGE,
4734 						UVERBS_ATTR_TYPE(u64),
4735 						UA_MANDATORY),
4736 			    UVERBS_ATTR_PTR_OUT(BNXT_RE_TOGGLE_MEM_MMAP_OFFSET,
4737 						UVERBS_ATTR_TYPE(u32),
4738 						UA_MANDATORY),
4739 			    UVERBS_ATTR_PTR_OUT(BNXT_RE_TOGGLE_MEM_MMAP_LENGTH,
4740 						UVERBS_ATTR_TYPE(u32),
4741 						UA_MANDATORY));
4742 
4743 DECLARE_UVERBS_NAMED_METHOD_DESTROY(BNXT_RE_METHOD_RELEASE_TOGGLE_MEM,
4744 				    UVERBS_ATTR_IDR(BNXT_RE_RELEASE_TOGGLE_MEM_HANDLE,
4745 						    BNXT_RE_OBJECT_GET_TOGGLE_MEM,
4746 						    UVERBS_ACCESS_DESTROY,
4747 						    UA_MANDATORY));
4748 
4749 DECLARE_UVERBS_NAMED_OBJECT(BNXT_RE_OBJECT_GET_TOGGLE_MEM,
4750 			    UVERBS_TYPE_ALLOC_IDR(get_toggle_mem_obj_cleanup),
4751 			    &UVERBS_METHOD(BNXT_RE_METHOD_GET_TOGGLE_MEM),
4752 			    &UVERBS_METHOD(BNXT_RE_METHOD_RELEASE_TOGGLE_MEM));
4753 
4754 const struct uapi_definition bnxt_re_uapi_defs[] = {
4755 	UAPI_DEF_CHAIN_OBJ_TREE_NAMED(BNXT_RE_OBJECT_ALLOC_PAGE),
4756 	UAPI_DEF_CHAIN_OBJ_TREE_NAMED(BNXT_RE_OBJECT_NOTIFY_DRV),
4757 	UAPI_DEF_CHAIN_OBJ_TREE_NAMED(BNXT_RE_OBJECT_GET_TOGGLE_MEM),
4758 	{}
4759 };
4760