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