xref: /linux/drivers/infiniband/hw/bnxt_re/main.c (revision 8d765af51a099884bab37a51e211c7047f67f1f3)
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: Main component of the bnxt_re driver
37  */
38 
39 #include <linux/module.h>
40 #include <linux/netdevice.h>
41 #include <linux/ethtool.h>
42 #include <linux/mutex.h>
43 #include <linux/list.h>
44 #include <linux/rculist.h>
45 #include <linux/spinlock.h>
46 #include <linux/pci.h>
47 #include <net/dcbnl.h>
48 #include <net/ipv6.h>
49 #include <net/addrconf.h>
50 #include <linux/if_ether.h>
51 #include <linux/auxiliary_bus.h>
52 
53 #include <rdma/ib_verbs.h>
54 #include <rdma/ib_user_verbs.h>
55 #include <rdma/ib_umem.h>
56 #include <rdma/ib_addr.h>
57 #include <linux/hashtable.h>
58 
59 #include "bnxt_ulp.h"
60 #include "roce_hsi.h"
61 #include "qplib_res.h"
62 #include "qplib_sp.h"
63 #include "qplib_fp.h"
64 #include "qplib_rcfw.h"
65 #include "bnxt_re.h"
66 #include "ib_verbs.h"
67 #include <rdma/bnxt_re-abi.h>
68 #include "bnxt.h"
69 #include "hw_counters.h"
70 #include "debugfs.h"
71 
72 static char version[] =
73 		BNXT_RE_DESC "\n";
74 
75 MODULE_AUTHOR("Eddie Wai <eddie.wai@broadcom.com>");
76 MODULE_DESCRIPTION(BNXT_RE_DESC);
77 MODULE_LICENSE("Dual BSD/GPL");
78 
79 /* globals */
80 static DEFINE_MUTEX(bnxt_re_mutex);
81 
82 static int bnxt_re_hwrm_qcaps(struct bnxt_re_dev *rdev);
83 static int bnxt_re_query_hwrm_intf_version(struct bnxt_re_dev *rdev);
84 
85 static int bnxt_re_hwrm_qcfg(struct bnxt_re_dev *rdev, u32 *db_len,
86 			     u32 *offset);
87 static void bnxt_re_dispatch_event(struct ib_device *ibdev, struct ib_qp *qp,
88 				   u8 port_num, enum ib_event_type event);
89 static void bnxt_re_set_db_offset(struct bnxt_re_dev *rdev)
90 {
91 	struct bnxt_qplib_chip_ctx *cctx;
92 	struct bnxt_en_dev *en_dev;
93 	struct bnxt_qplib_res *res;
94 	u32 l2db_len = 0;
95 	u32 offset = 0;
96 	u32 barlen;
97 	int rc;
98 
99 	res = &rdev->qplib_res;
100 	en_dev = rdev->en_dev;
101 	cctx = rdev->chip_ctx;
102 
103 	/* Issue qcfg */
104 	rc = bnxt_re_hwrm_qcfg(rdev, &l2db_len, &offset);
105 	if (rc)
106 		dev_info(rdev_to_dev(rdev),
107 			 "Couldn't get DB bar size, Low latency framework is disabled\n");
108 	/* set register offsets for both UC and WC */
109 	if (bnxt_qplib_is_chip_gen_p7(cctx)) {
110 		res->dpi_tbl.ucreg.offset = offset;
111 		res->dpi_tbl.wcreg.offset = en_dev->l2_db_size;
112 	} else {
113 		res->dpi_tbl.ucreg.offset = res->is_vf ? BNXT_QPLIB_DBR_VF_DB_OFFSET :
114 							 BNXT_QPLIB_DBR_PF_DB_OFFSET;
115 		res->dpi_tbl.wcreg.offset = res->dpi_tbl.ucreg.offset;
116 	}
117 
118 	/* If WC mapping is disabled by L2 driver then en_dev->l2_db_size
119 	 * is equal to the DB-Bar actual size. This indicates that L2
120 	 * is mapping entire bar as UC-. RoCE driver can't enable WC mapping
121 	 * in such cases and DB-push will be disabled.
122 	 */
123 	barlen = pci_resource_len(res->pdev, RCFW_DBR_PCI_BAR_REGION);
124 	if (cctx->modes.db_push && l2db_len && en_dev->l2_db_size != barlen) {
125 		res->dpi_tbl.wcreg.offset = en_dev->l2_db_size;
126 		dev_info(rdev_to_dev(rdev),  "Low latency framework is enabled\n");
127 	}
128 }
129 
130 static void bnxt_re_set_drv_mode(struct bnxt_re_dev *rdev)
131 {
132 	struct bnxt_qplib_chip_ctx *cctx;
133 
134 	cctx = rdev->chip_ctx;
135 	cctx->modes.wqe_mode = bnxt_qplib_is_chip_gen_p7(rdev->chip_ctx) ?
136 			       BNXT_QPLIB_WQE_MODE_VARIABLE : BNXT_QPLIB_WQE_MODE_STATIC;
137 	if (bnxt_re_hwrm_qcaps(rdev))
138 		dev_err(rdev_to_dev(rdev),
139 			"Failed to query hwrm qcaps\n");
140 	if (bnxt_qplib_is_chip_gen_p7(rdev->chip_ctx)) {
141 		cctx->modes.toggle_bits |= BNXT_QPLIB_CQ_TOGGLE_BIT;
142 		cctx->modes.toggle_bits |= BNXT_QPLIB_SRQ_TOGGLE_BIT;
143 	}
144 }
145 
146 static void bnxt_re_destroy_chip_ctx(struct bnxt_re_dev *rdev)
147 {
148 	struct bnxt_qplib_chip_ctx *chip_ctx;
149 
150 	if (!rdev->chip_ctx)
151 		return;
152 
153 	kfree(rdev->dev_attr);
154 	rdev->dev_attr = NULL;
155 
156 	chip_ctx = rdev->chip_ctx;
157 	rdev->chip_ctx = NULL;
158 	rdev->rcfw.res = NULL;
159 	rdev->qplib_res.cctx = NULL;
160 	rdev->qplib_res.pdev = NULL;
161 	rdev->qplib_res.netdev = NULL;
162 	kfree(chip_ctx);
163 }
164 
165 static int bnxt_re_setup_chip_ctx(struct bnxt_re_dev *rdev)
166 {
167 	struct bnxt_qplib_chip_ctx *chip_ctx;
168 	struct bnxt_en_dev *en_dev;
169 	int rc = -ENOMEM;
170 
171 	en_dev = rdev->en_dev;
172 
173 	rdev->qplib_res.pdev = en_dev->pdev;
174 	chip_ctx = kzalloc(sizeof(*chip_ctx), GFP_KERNEL);
175 	if (!chip_ctx)
176 		return -ENOMEM;
177 	chip_ctx->chip_num = en_dev->chip_num;
178 	chip_ctx->hw_stats_size = en_dev->hw_ring_stats_size;
179 
180 	rdev->chip_ctx = chip_ctx;
181 	/* rest members to follow eventually */
182 
183 	rdev->qplib_res.cctx = rdev->chip_ctx;
184 	rdev->rcfw.res = &rdev->qplib_res;
185 	rdev->dev_attr = kzalloc(sizeof(*rdev->dev_attr), GFP_KERNEL);
186 	if (!rdev->dev_attr)
187 		goto free_chip_ctx;
188 	rdev->qplib_res.dattr = rdev->dev_attr;
189 	rdev->qplib_res.is_vf = BNXT_EN_VF(en_dev);
190 	rdev->qplib_res.en_dev = en_dev;
191 
192 	rc = bnxt_re_query_hwrm_intf_version(rdev);
193 	if (rc)
194 		goto free_dev_attr;
195 
196 	bnxt_re_set_drv_mode(rdev);
197 
198 	bnxt_re_set_db_offset(rdev);
199 	rc = bnxt_qplib_map_db_bar(&rdev->qplib_res);
200 	if (rc)
201 		goto free_dev_attr;
202 
203 	if (bnxt_qplib_determine_atomics(en_dev->pdev))
204 		ibdev_info(&rdev->ibdev,
205 			   "platform doesn't support global atomics.");
206 	return 0;
207 free_dev_attr:
208 	kfree(rdev->dev_attr);
209 	rdev->dev_attr = NULL;
210 free_chip_ctx:
211 	kfree(rdev->chip_ctx);
212 	rdev->chip_ctx = NULL;
213 	return rc;
214 }
215 
216 /* SR-IOV helper functions */
217 
218 static void bnxt_re_get_sriov_func_type(struct bnxt_re_dev *rdev)
219 {
220 	if (BNXT_EN_VF(rdev->en_dev))
221 		rdev->is_virtfn = 1;
222 }
223 
224 /* Set the maximum number of each resource that the driver actually wants
225  * to allocate. This may be up to the maximum number the firmware has
226  * reserved for the function. The driver may choose to allocate fewer
227  * resources than the firmware maximum.
228  */
229 static void bnxt_re_limit_pf_res(struct bnxt_re_dev *rdev)
230 {
231 	struct bnxt_qplib_dev_attr *attr;
232 	struct bnxt_qplib_ctx *ctx;
233 	int i;
234 
235 	attr = rdev->dev_attr;
236 	ctx = &rdev->qplib_ctx;
237 
238 	ctx->qpc_count = min_t(u32, BNXT_RE_MAX_QPC_COUNT,
239 			       attr->max_qp);
240 	ctx->mrw_count = BNXT_RE_MAX_MRW_COUNT_256K;
241 	/* Use max_mr from fw since max_mrw does not get set */
242 	ctx->mrw_count = min_t(u32, ctx->mrw_count, attr->max_mr);
243 	ctx->srqc_count = min_t(u32, BNXT_RE_MAX_SRQC_COUNT,
244 				attr->max_srq);
245 	ctx->cq_count = min_t(u32, BNXT_RE_MAX_CQ_COUNT, attr->max_cq);
246 	if (!bnxt_qplib_is_chip_gen_p5_p7(rdev->chip_ctx))
247 		for (i = 0; i < MAX_TQM_ALLOC_REQ; i++)
248 			rdev->qplib_ctx.tqm_ctx.qcount[i] =
249 			rdev->dev_attr->tqm_alloc_reqs[i];
250 }
251 
252 static void bnxt_re_limit_vf_res(struct bnxt_qplib_ctx *qplib_ctx, u32 num_vf)
253 {
254 	struct bnxt_qplib_vf_res *vf_res;
255 	u32 mrws = 0;
256 	u32 vf_pct;
257 	u32 nvfs;
258 
259 	vf_res = &qplib_ctx->vf_res;
260 	/*
261 	 * Reserve a set of resources for the PF. Divide the remaining
262 	 * resources among the VFs
263 	 */
264 	vf_pct = 100 - BNXT_RE_PCT_RSVD_FOR_PF;
265 	nvfs = num_vf;
266 	num_vf = 100 * num_vf;
267 	vf_res->max_qp_per_vf = (qplib_ctx->qpc_count * vf_pct) / num_vf;
268 	vf_res->max_srq_per_vf = (qplib_ctx->srqc_count * vf_pct) / num_vf;
269 	vf_res->max_cq_per_vf = (qplib_ctx->cq_count * vf_pct) / num_vf;
270 	/*
271 	 * The driver allows many more MRs than other resources. If the
272 	 * firmware does also, then reserve a fixed amount for the PF and
273 	 * divide the rest among VFs. VFs may use many MRs for NFS
274 	 * mounts, ISER, NVME applications, etc. If the firmware severely
275 	 * restricts the number of MRs, then let PF have half and divide
276 	 * the rest among VFs, as for the other resource types.
277 	 */
278 	if (qplib_ctx->mrw_count < BNXT_RE_MAX_MRW_COUNT_64K) {
279 		mrws = qplib_ctx->mrw_count * vf_pct;
280 		nvfs = num_vf;
281 	} else {
282 		mrws = qplib_ctx->mrw_count - BNXT_RE_RESVD_MR_FOR_PF;
283 	}
284 	vf_res->max_mrw_per_vf = (mrws / nvfs);
285 	vf_res->max_gid_per_vf = BNXT_RE_MAX_GID_PER_VF;
286 }
287 
288 static void bnxt_re_set_resource_limits(struct bnxt_re_dev *rdev)
289 {
290 	u32 num_vfs;
291 
292 	memset(&rdev->qplib_ctx.vf_res, 0, sizeof(struct bnxt_qplib_vf_res));
293 	bnxt_re_limit_pf_res(rdev);
294 
295 	num_vfs =  bnxt_qplib_is_chip_gen_p5_p7(rdev->chip_ctx) ?
296 			BNXT_RE_GEN_P5_MAX_VF : rdev->num_vfs;
297 	if (num_vfs)
298 		bnxt_re_limit_vf_res(&rdev->qplib_ctx, num_vfs);
299 }
300 
301 static void bnxt_re_vf_res_config(struct bnxt_re_dev *rdev)
302 {
303 	/*
304 	 * Use the total VF count since the actual VF count may not be
305 	 * available at this point.
306 	 */
307 	rdev->num_vfs = pci_sriov_get_totalvfs(rdev->en_dev->pdev);
308 	if (!rdev->num_vfs)
309 		return;
310 
311 	bnxt_re_set_resource_limits(rdev);
312 	bnxt_qplib_set_func_resources(&rdev->qplib_res, &rdev->rcfw,
313 				      &rdev->qplib_ctx);
314 }
315 
316 struct bnxt_re_dcb_work {
317 	struct work_struct work;
318 	struct bnxt_re_dev *rdev;
319 	struct hwrm_async_event_cmpl cmpl;
320 };
321 
322 static bool bnxt_re_is_qp1_qp(struct bnxt_re_qp *qp)
323 {
324 	return qp->ib_qp.qp_type == IB_QPT_GSI;
325 }
326 
327 static struct bnxt_re_qp *bnxt_re_get_qp1_qp(struct bnxt_re_dev *rdev)
328 {
329 	struct bnxt_re_qp *qp;
330 
331 	mutex_lock(&rdev->qp_lock);
332 	list_for_each_entry(qp, &rdev->qp_list, list) {
333 		if (bnxt_re_is_qp1_qp(qp)) {
334 			mutex_unlock(&rdev->qp_lock);
335 			return qp;
336 		}
337 	}
338 	mutex_unlock(&rdev->qp_lock);
339 	return NULL;
340 }
341 
342 static int bnxt_re_update_qp1_tos_dscp(struct bnxt_re_dev *rdev)
343 {
344 	struct bnxt_re_qp *qp;
345 
346 	if (!bnxt_qplib_is_chip_gen_p5_p7(rdev->chip_ctx))
347 		return 0;
348 
349 	qp = bnxt_re_get_qp1_qp(rdev);
350 	if (!qp)
351 		return 0;
352 
353 	qp->qplib_qp.modify_flags = CMDQ_MODIFY_QP_MODIFY_MASK_TOS_DSCP;
354 	qp->qplib_qp.tos_dscp = rdev->cc_param.qp1_tos_dscp;
355 
356 	return bnxt_qplib_modify_qp(&rdev->qplib_res, &qp->qplib_qp);
357 }
358 
359 static void bnxt_re_init_dcb_wq(struct bnxt_re_dev *rdev)
360 {
361 	rdev->dcb_wq = create_singlethread_workqueue("bnxt_re_dcb_wq");
362 }
363 
364 static void bnxt_re_uninit_dcb_wq(struct bnxt_re_dev *rdev)
365 {
366 	if (!rdev->dcb_wq)
367 		return;
368 	destroy_workqueue(rdev->dcb_wq);
369 }
370 
371 static void bnxt_re_dcb_wq_task(struct work_struct *work)
372 {
373 	struct bnxt_re_dcb_work *dcb_work =
374 		container_of(work, struct bnxt_re_dcb_work, work);
375 	struct bnxt_re_dev *rdev = dcb_work->rdev;
376 	struct bnxt_qplib_cc_param *cc_param;
377 	int rc;
378 
379 	if (!rdev)
380 		goto free_dcb;
381 
382 	cc_param = &rdev->cc_param;
383 	rc = bnxt_qplib_query_cc_param(&rdev->qplib_res, cc_param);
384 	if (rc) {
385 		ibdev_dbg(&rdev->ibdev, "Failed to query ccparam rc:%d", rc);
386 		goto free_dcb;
387 	}
388 	if (cc_param->qp1_tos_dscp != cc_param->tos_dscp) {
389 		cc_param->qp1_tos_dscp = cc_param->tos_dscp;
390 		rc = bnxt_re_update_qp1_tos_dscp(rdev);
391 		if (rc) {
392 			ibdev_dbg(&rdev->ibdev, "%s: Failed to modify QP1 rc:%d",
393 				  __func__, rc);
394 			goto free_dcb;
395 		}
396 	}
397 
398 free_dcb:
399 	kfree(dcb_work);
400 }
401 
402 static void bnxt_re_async_notifier(void *handle, struct hwrm_async_event_cmpl *cmpl)
403 {
404 	struct bnxt_re_en_dev_info *en_info = auxiliary_get_drvdata(handle);
405 	struct bnxt_re_dcb_work *dcb_work;
406 	struct bnxt_re_dev *rdev;
407 	u32 data1, data2;
408 	u16 event_id;
409 
410 	rdev = en_info->rdev;
411 	if (!rdev)
412 		return;
413 
414 	event_id = le16_to_cpu(cmpl->event_id);
415 	data1 = le32_to_cpu(cmpl->event_data1);
416 	data2 = le32_to_cpu(cmpl->event_data2);
417 
418 	ibdev_dbg(&rdev->ibdev, "Async event_id = %d data1 = %d data2 = %d",
419 		  event_id, data1, data2);
420 
421 	switch (event_id) {
422 	case ASYNC_EVENT_CMPL_EVENT_ID_DCB_CONFIG_CHANGE:
423 		dcb_work = kzalloc(sizeof(*dcb_work), GFP_ATOMIC);
424 		if (!dcb_work)
425 			break;
426 
427 		dcb_work->rdev = rdev;
428 		memcpy(&dcb_work->cmpl, cmpl, sizeof(*cmpl));
429 		INIT_WORK(&dcb_work->work, bnxt_re_dcb_wq_task);
430 		queue_work(rdev->dcb_wq, &dcb_work->work);
431 		break;
432 	default:
433 		break;
434 	}
435 }
436 
437 static void bnxt_re_stop_irq(void *handle, bool reset)
438 {
439 	struct bnxt_re_en_dev_info *en_info = auxiliary_get_drvdata(handle);
440 	struct bnxt_qplib_rcfw *rcfw;
441 	struct bnxt_re_dev *rdev;
442 	struct bnxt_qplib_nq *nq;
443 	int indx;
444 
445 	rdev = en_info->rdev;
446 	if (!rdev)
447 		return;
448 	rcfw = &rdev->rcfw;
449 
450 	if (reset) {
451 		set_bit(ERR_DEVICE_DETACHED, &rdev->rcfw.cmdq.flags);
452 		set_bit(BNXT_RE_FLAG_ERR_DEVICE_DETACHED, &rdev->flags);
453 		wake_up_all(&rdev->rcfw.cmdq.waitq);
454 		bnxt_re_dispatch_event(&rdev->ibdev, NULL, 1,
455 				       IB_EVENT_DEVICE_FATAL);
456 	}
457 
458 	for (indx = BNXT_RE_NQ_IDX; indx < rdev->nqr->num_msix; indx++) {
459 		nq = &rdev->nqr->nq[indx - 1];
460 		bnxt_qplib_nq_stop_irq(nq, false);
461 	}
462 
463 	bnxt_qplib_rcfw_stop_irq(rcfw, false);
464 }
465 
466 static void bnxt_re_start_irq(void *handle, struct bnxt_msix_entry *ent)
467 {
468 	struct bnxt_re_en_dev_info *en_info = auxiliary_get_drvdata(handle);
469 	struct bnxt_msix_entry *msix_ent;
470 	struct bnxt_qplib_rcfw *rcfw;
471 	struct bnxt_re_dev *rdev;
472 	struct bnxt_qplib_nq *nq;
473 	int indx, rc;
474 
475 	rdev = en_info->rdev;
476 	if (!rdev)
477 		return;
478 	msix_ent = rdev->nqr->msix_entries;
479 	rcfw = &rdev->rcfw;
480 	if (!ent) {
481 		/* Not setting the f/w timeout bit in rcfw.
482 		 * During the driver unload the first command
483 		 * to f/w will timeout and that will set the
484 		 * timeout bit.
485 		 */
486 		ibdev_err(&rdev->ibdev, "Failed to re-start IRQs\n");
487 		return;
488 	}
489 
490 	/* Vectors may change after restart, so update with new vectors
491 	 * in device sctructure.
492 	 */
493 	for (indx = 0; indx < rdev->nqr->num_msix; indx++)
494 		rdev->nqr->msix_entries[indx].vector = ent[indx].vector;
495 
496 	rc = bnxt_qplib_rcfw_start_irq(rcfw, msix_ent[BNXT_RE_AEQ_IDX].vector,
497 				       false);
498 	if (rc) {
499 		ibdev_warn(&rdev->ibdev, "Failed to reinit CREQ\n");
500 		return;
501 	}
502 	for (indx = BNXT_RE_NQ_IDX ; indx < rdev->nqr->num_msix; indx++) {
503 		nq = &rdev->nqr->nq[indx - 1];
504 		rc = bnxt_qplib_nq_start_irq(nq, indx - 1,
505 					     msix_ent[indx].vector, false);
506 		if (rc) {
507 			ibdev_warn(&rdev->ibdev, "Failed to reinit NQ index %d\n",
508 				   indx - 1);
509 			return;
510 		}
511 	}
512 }
513 
514 static struct bnxt_ulp_ops bnxt_re_ulp_ops = {
515 	.ulp_async_notifier = bnxt_re_async_notifier,
516 	.ulp_irq_stop = bnxt_re_stop_irq,
517 	.ulp_irq_restart = bnxt_re_start_irq
518 };
519 
520 /* RoCE -> Net driver */
521 
522 static int bnxt_re_register_netdev(struct bnxt_re_dev *rdev)
523 {
524 	struct bnxt_en_dev *en_dev;
525 
526 	en_dev = rdev->en_dev;
527 	return bnxt_register_dev(en_dev, &bnxt_re_ulp_ops, rdev->adev);
528 }
529 
530 static void bnxt_re_init_hwrm_hdr(struct input *hdr, u16 opcd)
531 {
532 	hdr->req_type = cpu_to_le16(opcd);
533 	hdr->cmpl_ring = cpu_to_le16(-1);
534 	hdr->target_id = cpu_to_le16(-1);
535 }
536 
537 static void bnxt_re_fill_fw_msg(struct bnxt_fw_msg *fw_msg, void *msg,
538 				int msg_len, void *resp, int resp_max_len,
539 				int timeout)
540 {
541 	fw_msg->msg = msg;
542 	fw_msg->msg_len = msg_len;
543 	fw_msg->resp = resp;
544 	fw_msg->resp_max_len = resp_max_len;
545 	fw_msg->timeout = timeout;
546 }
547 
548 void bnxt_re_hwrm_free_vnic(struct bnxt_re_dev *rdev)
549 {
550 	struct bnxt_en_dev *en_dev = rdev->en_dev;
551 	struct hwrm_vnic_free_input req = {};
552 	struct bnxt_fw_msg fw_msg = {};
553 	int rc;
554 
555 	bnxt_re_init_hwrm_hdr((void *)&req, HWRM_VNIC_FREE);
556 
557 	req.vnic_id = cpu_to_le32(rdev->mirror_vnic_id);
558 	bnxt_re_fill_fw_msg(&fw_msg, (void *)&req, sizeof(req), NULL,
559 			    0, BNXT_RE_HWRM_CMD_TIMEOUT(rdev));
560 	rc = bnxt_send_msg(en_dev, &fw_msg);
561 	if (rc)
562 		ibdev_dbg(&rdev->ibdev,
563 			  "Failed to free vnic, rc = %d\n", rc);
564 }
565 
566 int bnxt_re_hwrm_alloc_vnic(struct bnxt_re_dev *rdev)
567 {
568 	struct bnxt_en_dev *en_dev = rdev->en_dev;
569 	struct hwrm_vnic_alloc_output resp = {};
570 	struct hwrm_vnic_alloc_input req = {};
571 	struct bnxt_fw_msg fw_msg = {};
572 	int rc;
573 
574 	bnxt_re_init_hwrm_hdr((void *)&req, HWRM_VNIC_ALLOC);
575 
576 	req.vnic_id = cpu_to_le16(rdev->mirror_vnic_id);
577 	req.flags = cpu_to_le32(VNIC_ALLOC_REQ_FLAGS_VNIC_ID_VALID);
578 	bnxt_re_fill_fw_msg(&fw_msg, (void *)&req, sizeof(req), (void *)&resp,
579 			    sizeof(resp), BNXT_RE_HWRM_CMD_TIMEOUT(rdev));
580 	rc = bnxt_send_msg(en_dev, &fw_msg);
581 	if (rc)
582 		ibdev_dbg(&rdev->ibdev,
583 			  "Failed to alloc vnic, rc = %d\n", rc);
584 
585 	return rc;
586 }
587 
588 int bnxt_re_hwrm_cfg_vnic(struct bnxt_re_dev *rdev, u32 qp_id)
589 {
590 	struct bnxt_en_dev *en_dev = rdev->en_dev;
591 	struct hwrm_vnic_cfg_input req = {};
592 	struct bnxt_fw_msg fw_msg = {};
593 	int rc;
594 
595 	bnxt_re_init_hwrm_hdr((void *)&req, HWRM_VNIC_CFG);
596 
597 	req.flags = cpu_to_le32(VNIC_CFG_REQ_FLAGS_ROCE_ONLY_VNIC_MODE);
598 	req.enables = cpu_to_le32(VNIC_CFG_REQ_ENABLES_RAW_QP_ID |
599 				  VNIC_CFG_REQ_ENABLES_MRU);
600 	req.vnic_id = cpu_to_le16(rdev->mirror_vnic_id);
601 	req.raw_qp_id = cpu_to_le32(qp_id);
602 	req.mru = cpu_to_le16(rdev->netdev->mtu + VLAN_ETH_HLEN);
603 
604 	bnxt_re_fill_fw_msg(&fw_msg, (void *)&req, sizeof(req), NULL,
605 			    0, BNXT_RE_HWRM_CMD_TIMEOUT(rdev));
606 	rc = bnxt_send_msg(en_dev, &fw_msg);
607 	if (rc)
608 		ibdev_dbg(&rdev->ibdev,
609 			  "Failed to cfg vnic, rc = %d\n", rc);
610 
611 	return rc;
612 }
613 
614 /* Query device config using common hwrm */
615 static int bnxt_re_hwrm_qcfg(struct bnxt_re_dev *rdev, u32 *db_len,
616 			     u32 *offset)
617 {
618 	struct bnxt_en_dev *en_dev = rdev->en_dev;
619 	struct hwrm_func_qcfg_output resp = {0};
620 	struct hwrm_func_qcfg_input req = {0};
621 	struct bnxt_fw_msg fw_msg = {};
622 	int rc;
623 
624 	bnxt_re_init_hwrm_hdr((void *)&req, HWRM_FUNC_QCFG);
625 	req.fid = cpu_to_le16(0xffff);
626 	bnxt_re_fill_fw_msg(&fw_msg, (void *)&req, sizeof(req), (void *)&resp,
627 			    sizeof(resp), BNXT_RE_HWRM_CMD_TIMEOUT(rdev));
628 	rc = bnxt_send_msg(en_dev, &fw_msg);
629 	if (!rc) {
630 		*db_len = PAGE_ALIGN(le16_to_cpu(resp.l2_doorbell_bar_size_kb) * 1024);
631 		*offset = PAGE_ALIGN(le16_to_cpu(resp.legacy_l2_db_size_kb) * 1024);
632 		rdev->mirror_vnic_id = le16_to_cpu(resp.mirror_vnic_id);
633 	}
634 	return rc;
635 }
636 
637 /* Query function capabilities using common hwrm */
638 int bnxt_re_hwrm_qcaps(struct bnxt_re_dev *rdev)
639 {
640 	struct bnxt_en_dev *en_dev = rdev->en_dev;
641 	struct hwrm_func_qcaps_output resp = {};
642 	struct hwrm_func_qcaps_input req = {};
643 	struct bnxt_qplib_chip_ctx *cctx;
644 	struct bnxt_fw_msg fw_msg = {};
645 	u32 flags_ext2;
646 	int rc;
647 
648 	cctx = rdev->chip_ctx;
649 	bnxt_re_init_hwrm_hdr((void *)&req, HWRM_FUNC_QCAPS);
650 	req.fid = cpu_to_le16(0xffff);
651 	bnxt_re_fill_fw_msg(&fw_msg, (void *)&req, sizeof(req), (void *)&resp,
652 			    sizeof(resp), BNXT_RE_HWRM_CMD_TIMEOUT(rdev));
653 
654 	rc = bnxt_send_msg(en_dev, &fw_msg);
655 	if (rc)
656 		return rc;
657 	cctx->modes.db_push = le32_to_cpu(resp.flags) & FUNC_QCAPS_RESP_FLAGS_WCB_PUSH_MODE;
658 
659 	flags_ext2 = le32_to_cpu(resp.flags_ext2);
660 	cctx->modes.dbr_pacing = flags_ext2 & FUNC_QCAPS_RESP_FLAGS_EXT2_DBR_PACING_EXT_SUPPORTED ||
661 				 flags_ext2 & FUNC_QCAPS_RESP_FLAGS_EXT2_DBR_PACING_V0_SUPPORTED;
662 	cctx->modes.roce_mirror = !!(le32_to_cpu(resp.flags_ext3) &
663 				     FUNC_QCAPS_RESP_FLAGS_EXT3_MIRROR_ON_ROCE_SUPPORTED);
664 	return 0;
665 }
666 
667 static int bnxt_re_hwrm_dbr_pacing_qcfg(struct bnxt_re_dev *rdev)
668 {
669 	struct bnxt_qplib_db_pacing_data *pacing_data = rdev->qplib_res.pacing_data;
670 	struct hwrm_func_dbr_pacing_qcfg_output resp = {};
671 	struct hwrm_func_dbr_pacing_qcfg_input req = {};
672 	struct bnxt_en_dev *en_dev = rdev->en_dev;
673 	struct bnxt_qplib_chip_ctx *cctx;
674 	struct bnxt_fw_msg fw_msg = {};
675 	int rc;
676 
677 	cctx = rdev->chip_ctx;
678 	bnxt_re_init_hwrm_hdr((void *)&req, HWRM_FUNC_DBR_PACING_QCFG);
679 	bnxt_re_fill_fw_msg(&fw_msg, (void *)&req, sizeof(req), (void *)&resp,
680 			    sizeof(resp), BNXT_RE_HWRM_CMD_TIMEOUT(rdev));
681 	rc = bnxt_send_msg(en_dev, &fw_msg);
682 	if (rc)
683 		return rc;
684 
685 	if ((le32_to_cpu(resp.dbr_stat_db_fifo_reg) &
686 	    FUNC_DBR_PACING_QCFG_RESP_DBR_STAT_DB_FIFO_REG_ADDR_SPACE_MASK) ==
687 		FUNC_DBR_PACING_QCFG_RESP_DBR_STAT_DB_FIFO_REG_ADDR_SPACE_GRC)
688 		cctx->dbr_stat_db_fifo =
689 			le32_to_cpu(resp.dbr_stat_db_fifo_reg) &
690 			~FUNC_DBR_PACING_QCFG_RESP_DBR_STAT_DB_FIFO_REG_ADDR_SPACE_MASK;
691 
692 	pacing_data->fifo_max_depth = le32_to_cpu(resp.dbr_stat_db_max_fifo_depth);
693 	if (!pacing_data->fifo_max_depth)
694 		pacing_data->fifo_max_depth = BNXT_RE_MAX_FIFO_DEPTH(cctx);
695 	pacing_data->fifo_room_mask = le32_to_cpu(resp.dbr_stat_db_fifo_reg_fifo_room_mask);
696 	pacing_data->fifo_room_shift = resp.dbr_stat_db_fifo_reg_fifo_room_shift;
697 
698 	return 0;
699 }
700 
701 /* Update the pacing tunable parameters to the default values */
702 static void bnxt_re_set_default_pacing_data(struct bnxt_re_dev *rdev)
703 {
704 	struct bnxt_qplib_db_pacing_data *pacing_data = rdev->qplib_res.pacing_data;
705 
706 	pacing_data->do_pacing = rdev->pacing.dbr_def_do_pacing;
707 	pacing_data->pacing_th = rdev->pacing.pacing_algo_th;
708 	pacing_data->alarm_th =
709 		pacing_data->pacing_th * BNXT_RE_PACING_ALARM_TH_MULTIPLE;
710 }
711 
712 static u32 __get_fifo_occupancy(struct bnxt_re_dev *rdev)
713 {
714 	struct bnxt_qplib_db_pacing_data *pacing_data = rdev->qplib_res.pacing_data;
715 	u32 read_val, fifo_occup;
716 
717 	read_val = readl(rdev->en_dev->bar0 + rdev->pacing.dbr_db_fifo_reg_off);
718 	fifo_occup = pacing_data->fifo_max_depth -
719 		     ((read_val & pacing_data->fifo_room_mask) >>
720 		      pacing_data->fifo_room_shift);
721 	return fifo_occup;
722 }
723 
724 static bool is_dbr_fifo_full(struct bnxt_re_dev *rdev)
725 {
726 	u32 max_occup, fifo_occup;
727 
728 	fifo_occup = __get_fifo_occupancy(rdev);
729 	max_occup = BNXT_RE_MAX_FIFO_DEPTH(rdev->chip_ctx) - 1;
730 	if (fifo_occup == max_occup)
731 		return true;
732 
733 	return false;
734 }
735 
736 static void __wait_for_fifo_occupancy_below_th(struct bnxt_re_dev *rdev)
737 {
738 	struct bnxt_qplib_db_pacing_data *pacing_data = rdev->qplib_res.pacing_data;
739 	u32 retry_fifo_check = 1000;
740 	u32 fifo_occup;
741 
742 	/* loop shouldn't run infintely as the occupancy usually goes
743 	 * below pacing algo threshold as soon as pacing kicks in.
744 	 */
745 	while (1) {
746 		fifo_occup = __get_fifo_occupancy(rdev);
747 		/* Fifo occupancy cannot be greater the MAX FIFO depth */
748 		if (fifo_occup > pacing_data->fifo_max_depth)
749 			break;
750 
751 		if (fifo_occup < pacing_data->pacing_th)
752 			break;
753 		if (!retry_fifo_check--) {
754 			dev_info_once(rdev_to_dev(rdev),
755 				      "%s: fifo_occup = 0x%xfifo_max_depth = 0x%x pacing_th = 0x%x\n",
756 				      __func__, fifo_occup, pacing_data->fifo_max_depth,
757 					pacing_data->pacing_th);
758 			break;
759 		}
760 
761 	}
762 }
763 
764 static void bnxt_re_db_fifo_check(struct work_struct *work)
765 {
766 	struct bnxt_re_dev *rdev = container_of(work, struct bnxt_re_dev,
767 			dbq_fifo_check_work);
768 	struct bnxt_qplib_db_pacing_data *pacing_data;
769 	u32 pacing_save;
770 
771 	if (!mutex_trylock(&rdev->pacing.dbq_lock))
772 		return;
773 	pacing_data = rdev->qplib_res.pacing_data;
774 	pacing_save = rdev->pacing.do_pacing_save;
775 	__wait_for_fifo_occupancy_below_th(rdev);
776 	cancel_delayed_work_sync(&rdev->dbq_pacing_work);
777 	if (pacing_save > rdev->pacing.dbr_def_do_pacing) {
778 		/* Double the do_pacing value during the congestion */
779 		pacing_save = pacing_save << 1;
780 	} else {
781 		/*
782 		 * when a new congestion is detected increase the do_pacing
783 		 * by 8 times. And also increase the pacing_th by 4 times. The
784 		 * reason to increase pacing_th is to give more space for the
785 		 * queue to oscillate down without getting empty, but also more
786 		 * room for the queue to increase without causing another alarm.
787 		 */
788 		pacing_save = pacing_save << 3;
789 		pacing_data->pacing_th = rdev->pacing.pacing_algo_th * 4;
790 	}
791 
792 	if (pacing_save > BNXT_RE_MAX_DBR_DO_PACING)
793 		pacing_save = BNXT_RE_MAX_DBR_DO_PACING;
794 
795 	pacing_data->do_pacing = pacing_save;
796 	rdev->pacing.do_pacing_save = pacing_data->do_pacing;
797 	pacing_data->alarm_th =
798 		pacing_data->pacing_th * BNXT_RE_PACING_ALARM_TH_MULTIPLE;
799 	schedule_delayed_work(&rdev->dbq_pacing_work,
800 			      msecs_to_jiffies(rdev->pacing.dbq_pacing_time));
801 	rdev->stats.pacing.alerts++;
802 	mutex_unlock(&rdev->pacing.dbq_lock);
803 }
804 
805 static void bnxt_re_pacing_timer_exp(struct work_struct *work)
806 {
807 	struct bnxt_re_dev *rdev = container_of(work, struct bnxt_re_dev,
808 			dbq_pacing_work.work);
809 	struct bnxt_qplib_db_pacing_data *pacing_data;
810 	u32 fifo_occup;
811 
812 	if (!mutex_trylock(&rdev->pacing.dbq_lock))
813 		return;
814 
815 	pacing_data = rdev->qplib_res.pacing_data;
816 	fifo_occup = __get_fifo_occupancy(rdev);
817 
818 	if (fifo_occup > pacing_data->pacing_th)
819 		goto restart_timer;
820 
821 	/*
822 	 * Instead of immediately going back to the default do_pacing
823 	 * reduce it by 1/8 times and restart the timer.
824 	 */
825 	pacing_data->do_pacing = pacing_data->do_pacing - (pacing_data->do_pacing >> 3);
826 	pacing_data->do_pacing = max_t(u32, rdev->pacing.dbr_def_do_pacing, pacing_data->do_pacing);
827 	if (pacing_data->do_pacing <= rdev->pacing.dbr_def_do_pacing) {
828 		bnxt_re_set_default_pacing_data(rdev);
829 		rdev->stats.pacing.complete++;
830 		goto dbq_unlock;
831 	}
832 
833 restart_timer:
834 	schedule_delayed_work(&rdev->dbq_pacing_work,
835 			      msecs_to_jiffies(rdev->pacing.dbq_pacing_time));
836 	rdev->stats.pacing.resched++;
837 dbq_unlock:
838 	rdev->pacing.do_pacing_save = pacing_data->do_pacing;
839 	mutex_unlock(&rdev->pacing.dbq_lock);
840 }
841 
842 void bnxt_re_pacing_alert(struct bnxt_re_dev *rdev)
843 {
844 	struct bnxt_qplib_db_pacing_data *pacing_data;
845 
846 	if (!rdev->pacing.dbr_pacing)
847 		return;
848 	mutex_lock(&rdev->pacing.dbq_lock);
849 	pacing_data = rdev->qplib_res.pacing_data;
850 
851 	/*
852 	 * Increase the alarm_th to max so that other user lib instances do not
853 	 * keep alerting the driver.
854 	 */
855 	pacing_data->alarm_th = pacing_data->fifo_max_depth;
856 	pacing_data->do_pacing = BNXT_RE_MAX_DBR_DO_PACING;
857 	cancel_work_sync(&rdev->dbq_fifo_check_work);
858 	schedule_work(&rdev->dbq_fifo_check_work);
859 	mutex_unlock(&rdev->pacing.dbq_lock);
860 }
861 
862 static int bnxt_re_initialize_dbr_pacing(struct bnxt_re_dev *rdev)
863 {
864 	/* Allocate a page for app use */
865 	rdev->pacing.dbr_page = (void *)__get_free_page(GFP_KERNEL);
866 	if (!rdev->pacing.dbr_page)
867 		return -ENOMEM;
868 
869 	memset((u8 *)rdev->pacing.dbr_page, 0, PAGE_SIZE);
870 	rdev->qplib_res.pacing_data = (struct bnxt_qplib_db_pacing_data *)rdev->pacing.dbr_page;
871 
872 	if (bnxt_re_hwrm_dbr_pacing_qcfg(rdev)) {
873 		free_page((u64)rdev->pacing.dbr_page);
874 		rdev->pacing.dbr_page = NULL;
875 		return -EIO;
876 	}
877 
878 	/* MAP HW window 2 for reading db fifo depth */
879 	writel(rdev->chip_ctx->dbr_stat_db_fifo & BNXT_GRC_BASE_MASK,
880 	       rdev->en_dev->bar0 + BNXT_GRCPF_REG_WINDOW_BASE_OUT + 4);
881 	rdev->pacing.dbr_db_fifo_reg_off =
882 		(rdev->chip_ctx->dbr_stat_db_fifo & BNXT_GRC_OFFSET_MASK) +
883 		 BNXT_RE_GRC_FIFO_REG_BASE;
884 	rdev->pacing.dbr_bar_addr =
885 		pci_resource_start(rdev->qplib_res.pdev, 0) + rdev->pacing.dbr_db_fifo_reg_off;
886 
887 	if (is_dbr_fifo_full(rdev)) {
888 		free_page((u64)rdev->pacing.dbr_page);
889 		rdev->pacing.dbr_page = NULL;
890 		return -EIO;
891 	}
892 
893 	rdev->pacing.pacing_algo_th = BNXT_RE_PACING_ALGO_THRESHOLD;
894 	rdev->pacing.dbq_pacing_time = BNXT_RE_DBR_PACING_TIME;
895 	rdev->pacing.dbr_def_do_pacing = BNXT_RE_DBR_DO_PACING_NO_CONGESTION;
896 	rdev->pacing.do_pacing_save = rdev->pacing.dbr_def_do_pacing;
897 	rdev->qplib_res.pacing_data->grc_reg_offset = rdev->pacing.dbr_db_fifo_reg_off;
898 	bnxt_re_set_default_pacing_data(rdev);
899 	/* Initialize worker for DBR Pacing */
900 	INIT_WORK(&rdev->dbq_fifo_check_work, bnxt_re_db_fifo_check);
901 	INIT_DELAYED_WORK(&rdev->dbq_pacing_work, bnxt_re_pacing_timer_exp);
902 	return 0;
903 }
904 
905 static void bnxt_re_deinitialize_dbr_pacing(struct bnxt_re_dev *rdev)
906 {
907 	cancel_work_sync(&rdev->dbq_fifo_check_work);
908 	cancel_delayed_work_sync(&rdev->dbq_pacing_work);
909 	if (rdev->pacing.dbr_page)
910 		free_page((u64)rdev->pacing.dbr_page);
911 
912 	rdev->pacing.dbr_page = NULL;
913 	rdev->pacing.dbr_pacing = false;
914 }
915 
916 static int bnxt_re_net_ring_free(struct bnxt_re_dev *rdev,
917 				 u16 fw_ring_id, int type)
918 {
919 	struct bnxt_en_dev *en_dev = rdev->en_dev;
920 	struct hwrm_ring_free_input req = {};
921 	struct hwrm_ring_free_output resp;
922 	struct bnxt_fw_msg fw_msg = {};
923 	int rc = -EINVAL;
924 
925 	if (test_bit(BNXT_RE_FLAG_ERR_DEVICE_DETACHED, &rdev->flags))
926 		return 0;
927 
928 	bnxt_re_init_hwrm_hdr((void *)&req, HWRM_RING_FREE);
929 	req.ring_type = type;
930 	req.ring_id = cpu_to_le16(fw_ring_id);
931 	bnxt_re_fill_fw_msg(&fw_msg, (void *)&req, sizeof(req), (void *)&resp,
932 			    sizeof(resp), BNXT_RE_HWRM_CMD_TIMEOUT(rdev));
933 	rc = bnxt_send_msg(en_dev, &fw_msg);
934 	if (rc)
935 		ibdev_err(&rdev->ibdev, "Failed to free HW ring:%d :%#x",
936 			  req.ring_id, rc);
937 	return rc;
938 }
939 
940 static int bnxt_re_net_ring_alloc(struct bnxt_re_dev *rdev,
941 				  struct bnxt_re_ring_attr *ring_attr,
942 				  u16 *fw_ring_id)
943 {
944 	struct bnxt_en_dev *en_dev = rdev->en_dev;
945 	struct hwrm_ring_alloc_input req = {};
946 	struct hwrm_ring_alloc_output resp;
947 	struct bnxt_fw_msg fw_msg = {};
948 	int rc = -EINVAL;
949 
950 	bnxt_re_init_hwrm_hdr((void *)&req, HWRM_RING_ALLOC);
951 	req.enables = 0;
952 	req.page_tbl_addr =  cpu_to_le64(ring_attr->dma_arr[0]);
953 	if (ring_attr->pages > 1) {
954 		/* Page size is in log2 units */
955 		req.page_size = BNXT_PAGE_SHIFT;
956 		req.page_tbl_depth = 1;
957 	}
958 	req.fbo = 0;
959 	/* Association of ring index with doorbell index and MSIX number */
960 	req.logical_id = cpu_to_le16(ring_attr->lrid);
961 	req.length = cpu_to_le32(ring_attr->depth + 1);
962 	req.ring_type = ring_attr->type;
963 	req.int_mode = ring_attr->mode;
964 	bnxt_re_fill_fw_msg(&fw_msg, (void *)&req, sizeof(req), (void *)&resp,
965 			    sizeof(resp), BNXT_RE_HWRM_CMD_TIMEOUT(rdev));
966 	rc = bnxt_send_msg(en_dev, &fw_msg);
967 	if (!rc)
968 		*fw_ring_id = le16_to_cpu(resp.ring_id);
969 
970 	return rc;
971 }
972 
973 static int bnxt_re_net_stats_ctx_free(struct bnxt_re_dev *rdev,
974 				      u32 fw_stats_ctx_id)
975 {
976 	struct bnxt_en_dev *en_dev = rdev->en_dev;
977 	struct hwrm_stat_ctx_free_input req = {};
978 	struct hwrm_stat_ctx_free_output resp = {};
979 	struct bnxt_fw_msg fw_msg = {};
980 	int rc = -EINVAL;
981 
982 	if (test_bit(BNXT_RE_FLAG_ERR_DEVICE_DETACHED, &rdev->flags))
983 		return 0;
984 
985 	bnxt_re_init_hwrm_hdr((void *)&req, HWRM_STAT_CTX_FREE);
986 	req.stat_ctx_id = cpu_to_le32(fw_stats_ctx_id);
987 	bnxt_re_fill_fw_msg(&fw_msg, (void *)&req, sizeof(req), (void *)&resp,
988 			    sizeof(resp), BNXT_RE_HWRM_CMD_TIMEOUT(rdev));
989 	rc = bnxt_send_msg(en_dev, &fw_msg);
990 	if (rc)
991 		ibdev_err(&rdev->ibdev, "Failed to free HW stats context %#x",
992 			  rc);
993 
994 	return rc;
995 }
996 
997 static int bnxt_re_net_stats_ctx_alloc(struct bnxt_re_dev *rdev,
998 				       struct bnxt_qplib_stats *stats)
999 {
1000 	struct bnxt_qplib_chip_ctx *chip_ctx = rdev->chip_ctx;
1001 	struct hwrm_stat_ctx_alloc_output resp = {};
1002 	struct hwrm_stat_ctx_alloc_input req = {};
1003 	struct bnxt_en_dev *en_dev = rdev->en_dev;
1004 	struct bnxt_fw_msg fw_msg = {};
1005 	int rc = -EINVAL;
1006 
1007 	stats->fw_id = INVALID_STATS_CTX_ID;
1008 
1009 	bnxt_re_init_hwrm_hdr((void *)&req, HWRM_STAT_CTX_ALLOC);
1010 	req.update_period_ms = cpu_to_le32(1000);
1011 	req.stats_dma_addr = cpu_to_le64(stats->dma_map);
1012 	req.stats_dma_length = cpu_to_le16(chip_ctx->hw_stats_size);
1013 	req.stat_ctx_flags = STAT_CTX_ALLOC_REQ_STAT_CTX_FLAGS_ROCE;
1014 	bnxt_re_fill_fw_msg(&fw_msg, (void *)&req, sizeof(req), (void *)&resp,
1015 			    sizeof(resp), BNXT_RE_HWRM_CMD_TIMEOUT(rdev));
1016 	rc = bnxt_send_msg(en_dev, &fw_msg);
1017 	if (!rc)
1018 		stats->fw_id = le32_to_cpu(resp.stat_ctx_id);
1019 
1020 	return rc;
1021 }
1022 
1023 static void bnxt_re_disassociate_ucontext(struct ib_ucontext *ibcontext)
1024 {
1025 }
1026 
1027 /* Device */
1028 static ssize_t hw_rev_show(struct device *device, struct device_attribute *attr,
1029 			   char *buf)
1030 {
1031 	struct bnxt_re_dev *rdev =
1032 		rdma_device_to_drv_device(device, struct bnxt_re_dev, ibdev);
1033 
1034 	return sysfs_emit(buf, "0x%x\n", rdev->en_dev->pdev->vendor);
1035 }
1036 static DEVICE_ATTR_RO(hw_rev);
1037 
1038 static ssize_t hca_type_show(struct device *device,
1039 			     struct device_attribute *attr, char *buf)
1040 {
1041 	struct bnxt_re_dev *rdev =
1042 		rdma_device_to_drv_device(device, struct bnxt_re_dev, ibdev);
1043 
1044 	return sysfs_emit(buf, "%s\n", rdev->ibdev.node_desc);
1045 }
1046 static DEVICE_ATTR_RO(hca_type);
1047 
1048 static struct attribute *bnxt_re_attributes[] = {
1049 	&dev_attr_hw_rev.attr,
1050 	&dev_attr_hca_type.attr,
1051 	NULL
1052 };
1053 
1054 static const struct attribute_group bnxt_re_dev_attr_group = {
1055 	.attrs = bnxt_re_attributes,
1056 };
1057 
1058 static int bnxt_re_fill_res_mr_entry(struct sk_buff *msg, struct ib_mr *ib_mr)
1059 {
1060 	struct bnxt_qplib_hwq *mr_hwq;
1061 	struct nlattr *table_attr;
1062 	struct bnxt_re_mr *mr;
1063 
1064 	table_attr = nla_nest_start(msg, RDMA_NLDEV_ATTR_DRIVER);
1065 	if (!table_attr)
1066 		return -EMSGSIZE;
1067 
1068 	mr = container_of(ib_mr, struct bnxt_re_mr, ib_mr);
1069 	mr_hwq = &mr->qplib_mr.hwq;
1070 
1071 	if (rdma_nl_put_driver_u32(msg, "page_size",
1072 				   mr_hwq->qe_ppg * mr_hwq->element_size))
1073 		goto err;
1074 	if (rdma_nl_put_driver_u32(msg, "max_elements", mr_hwq->max_elements))
1075 		goto err;
1076 	if (rdma_nl_put_driver_u32(msg, "element_size", mr_hwq->element_size))
1077 		goto err;
1078 	if (rdma_nl_put_driver_u64_hex(msg, "hwq", (unsigned long)mr_hwq))
1079 		goto err;
1080 	if (rdma_nl_put_driver_u64_hex(msg, "va", mr->qplib_mr.va))
1081 		goto err;
1082 
1083 	nla_nest_end(msg, table_attr);
1084 	return 0;
1085 
1086 err:
1087 	nla_nest_cancel(msg, table_attr);
1088 	return -EMSGSIZE;
1089 }
1090 
1091 static int bnxt_re_fill_res_mr_entry_raw(struct sk_buff *msg, struct ib_mr *ib_mr)
1092 {
1093 	struct bnxt_re_dev *rdev;
1094 	struct bnxt_re_mr *mr;
1095 	int err, len;
1096 	void *data;
1097 
1098 	mr = container_of(ib_mr, struct bnxt_re_mr, ib_mr);
1099 	rdev = mr->rdev;
1100 
1101 	err = bnxt_re_read_context_allowed(rdev);
1102 	if (err)
1103 		return err;
1104 
1105 	len = bnxt_qplib_is_chip_gen_p7(rdev->chip_ctx) ? BNXT_RE_CONTEXT_TYPE_MRW_SIZE_P7 :
1106 							  BNXT_RE_CONTEXT_TYPE_MRW_SIZE_P5;
1107 	data = kzalloc(len, GFP_KERNEL);
1108 	if (!data)
1109 		return -ENOMEM;
1110 
1111 	err = bnxt_qplib_read_context(&rdev->rcfw, CMDQ_READ_CONTEXT_TYPE_MRW,
1112 				      mr->qplib_mr.lkey, len, data);
1113 	if (!err)
1114 		err = nla_put(msg, RDMA_NLDEV_ATTR_RES_RAW, len, data);
1115 
1116 	kfree(data);
1117 	return err;
1118 }
1119 
1120 static int bnxt_re_fill_res_cq_entry(struct sk_buff *msg, struct ib_cq *ib_cq)
1121 {
1122 	struct bnxt_qplib_hwq *cq_hwq;
1123 	struct nlattr *table_attr;
1124 	struct bnxt_re_cq *cq;
1125 
1126 	cq = container_of(ib_cq, struct bnxt_re_cq, ib_cq);
1127 	cq_hwq = &cq->qplib_cq.hwq;
1128 
1129 	table_attr = nla_nest_start(msg, RDMA_NLDEV_ATTR_DRIVER);
1130 	if (!table_attr)
1131 		return -EMSGSIZE;
1132 
1133 	if (rdma_nl_put_driver_u32(msg, "cq_depth", cq_hwq->depth))
1134 		goto err;
1135 	if (rdma_nl_put_driver_u32(msg, "max_elements", cq_hwq->max_elements))
1136 		goto err;
1137 	if (rdma_nl_put_driver_u32(msg, "element_size", cq_hwq->element_size))
1138 		goto err;
1139 	if (rdma_nl_put_driver_u32(msg, "max_wqe", cq->qplib_cq.max_wqe))
1140 		goto err;
1141 
1142 	nla_nest_end(msg, table_attr);
1143 	return 0;
1144 
1145 err:
1146 	nla_nest_cancel(msg, table_attr);
1147 	return -EMSGSIZE;
1148 }
1149 
1150 static int bnxt_re_fill_res_cq_entry_raw(struct sk_buff *msg, struct ib_cq *ib_cq)
1151 {
1152 	struct bnxt_re_dev *rdev;
1153 	struct bnxt_re_cq *cq;
1154 	int err, len;
1155 	void *data;
1156 
1157 	cq = container_of(ib_cq, struct bnxt_re_cq, ib_cq);
1158 	rdev = cq->rdev;
1159 
1160 	err = bnxt_re_read_context_allowed(rdev);
1161 	if (err)
1162 		return err;
1163 
1164 	len = bnxt_qplib_is_chip_gen_p7(rdev->chip_ctx) ? BNXT_RE_CONTEXT_TYPE_CQ_SIZE_P7 :
1165 					BNXT_RE_CONTEXT_TYPE_CQ_SIZE_P5;
1166 	data = kzalloc(len, GFP_KERNEL);
1167 	if (!data)
1168 		return -ENOMEM;
1169 
1170 	err = bnxt_qplib_read_context(&rdev->rcfw,
1171 				      CMDQ_READ_CONTEXT_TYPE_CQ,
1172 				      cq->qplib_cq.id, len, data);
1173 	if (!err)
1174 		err = nla_put(msg, RDMA_NLDEV_ATTR_RES_RAW, len, data);
1175 
1176 	kfree(data);
1177 	return err;
1178 }
1179 
1180 static int bnxt_re_fill_res_qp_entry(struct sk_buff *msg, struct ib_qp *ib_qp)
1181 {
1182 	struct bnxt_qplib_qp *qplib_qp;
1183 	struct nlattr *table_attr;
1184 	struct bnxt_re_qp *qp;
1185 
1186 	table_attr = nla_nest_start(msg, RDMA_NLDEV_ATTR_DRIVER);
1187 	if (!table_attr)
1188 		return -EMSGSIZE;
1189 
1190 	qp = container_of(ib_qp, struct bnxt_re_qp, ib_qp);
1191 	qplib_qp = &qp->qplib_qp;
1192 
1193 	if (rdma_nl_put_driver_u32(msg, "sq_max_wqe", qplib_qp->sq.max_wqe))
1194 		goto err;
1195 	if (rdma_nl_put_driver_u32(msg, "sq_max_sge", qplib_qp->sq.max_sge))
1196 		goto err;
1197 	if (rdma_nl_put_driver_u32(msg, "sq_wqe_size", qplib_qp->sq.wqe_size))
1198 		goto err;
1199 	if (rdma_nl_put_driver_u32(msg, "sq_swq_start", qplib_qp->sq.swq_start))
1200 		goto err;
1201 	if (rdma_nl_put_driver_u32(msg, "sq_swq_last", qplib_qp->sq.swq_last))
1202 		goto err;
1203 	if (rdma_nl_put_driver_u32(msg, "rq_max_wqe", qplib_qp->rq.max_wqe))
1204 		goto err;
1205 	if (rdma_nl_put_driver_u32(msg, "rq_max_sge", qplib_qp->rq.max_sge))
1206 		goto err;
1207 	if (rdma_nl_put_driver_u32(msg, "rq_wqe_size", qplib_qp->rq.wqe_size))
1208 		goto err;
1209 	if (rdma_nl_put_driver_u32(msg, "rq_swq_start", qplib_qp->rq.swq_start))
1210 		goto err;
1211 	if (rdma_nl_put_driver_u32(msg, "rq_swq_last", qplib_qp->rq.swq_last))
1212 		goto err;
1213 	if (rdma_nl_put_driver_u32(msg, "timeout", qplib_qp->timeout))
1214 		goto err;
1215 
1216 	nla_nest_end(msg, table_attr);
1217 	return 0;
1218 
1219 err:
1220 	nla_nest_cancel(msg, table_attr);
1221 	return -EMSGSIZE;
1222 }
1223 
1224 static int bnxt_re_fill_res_qp_entry_raw(struct sk_buff *msg, struct ib_qp *ibqp)
1225 {
1226 	struct bnxt_re_dev *rdev = to_bnxt_re_dev(ibqp->device, ibdev);
1227 	int err, len;
1228 	void *data;
1229 
1230 	err = bnxt_re_read_context_allowed(rdev);
1231 	if (err)
1232 		return err;
1233 
1234 	len = bnxt_qplib_is_chip_gen_p7(rdev->chip_ctx) ? BNXT_RE_CONTEXT_TYPE_QPC_SIZE_P7 :
1235 							  BNXT_RE_CONTEXT_TYPE_QPC_SIZE_P5;
1236 	data = kzalloc(len, GFP_KERNEL);
1237 	if (!data)
1238 		return -ENOMEM;
1239 
1240 	err = bnxt_qplib_read_context(&rdev->rcfw, CMDQ_READ_CONTEXT_TYPE_QPC,
1241 				      ibqp->qp_num, len, data);
1242 	if (!err)
1243 		err = nla_put(msg, RDMA_NLDEV_ATTR_RES_RAW, len, data);
1244 
1245 	kfree(data);
1246 	return err;
1247 }
1248 
1249 static int bnxt_re_fill_res_srq_entry(struct sk_buff *msg, struct ib_srq *ib_srq)
1250 {
1251 	struct nlattr *table_attr;
1252 	struct bnxt_re_srq *srq;
1253 
1254 	table_attr = nla_nest_start(msg, RDMA_NLDEV_ATTR_DRIVER);
1255 	if (!table_attr)
1256 		return -EMSGSIZE;
1257 
1258 	srq = container_of(ib_srq, struct bnxt_re_srq, ib_srq);
1259 
1260 	if (rdma_nl_put_driver_u32_hex(msg, "wqe_size", srq->qplib_srq.wqe_size))
1261 		goto err;
1262 	if (rdma_nl_put_driver_u32_hex(msg, "max_wqe", srq->qplib_srq.max_wqe))
1263 		goto err;
1264 	if (rdma_nl_put_driver_u32_hex(msg, "max_sge", srq->qplib_srq.max_sge))
1265 		goto err;
1266 	if (rdma_nl_put_driver_u32_hex(msg, "srq_limit", srq->qplib_srq.threshold))
1267 		goto err;
1268 
1269 	nla_nest_end(msg, table_attr);
1270 	return 0;
1271 
1272 err:
1273 	nla_nest_cancel(msg, table_attr);
1274 	return -EMSGSIZE;
1275 }
1276 
1277 static int bnxt_re_fill_res_srq_entry_raw(struct sk_buff *msg, struct ib_srq *ib_srq)
1278 {
1279 	struct bnxt_re_dev *rdev;
1280 	struct bnxt_re_srq *srq;
1281 	int err, len;
1282 	void *data;
1283 
1284 	srq = container_of(ib_srq, struct bnxt_re_srq, ib_srq);
1285 	rdev = srq->rdev;
1286 
1287 	err = bnxt_re_read_context_allowed(rdev);
1288 	if (err)
1289 		return err;
1290 
1291 	len = bnxt_qplib_is_chip_gen_p7(rdev->chip_ctx) ? BNXT_RE_CONTEXT_TYPE_SRQ_SIZE_P7 :
1292 							  BNXT_RE_CONTEXT_TYPE_SRQ_SIZE_P5;
1293 
1294 	data = kzalloc(len, GFP_KERNEL);
1295 	if (!data)
1296 		return -ENOMEM;
1297 
1298 	err = bnxt_qplib_read_context(&rdev->rcfw, CMDQ_READ_CONTEXT_TYPE_SRQ,
1299 				      srq->qplib_srq.id, len, data);
1300 	if (!err)
1301 		err = nla_put(msg, RDMA_NLDEV_ATTR_RES_RAW, len, data);
1302 
1303 	kfree(data);
1304 	return err;
1305 }
1306 
1307 static const struct ib_device_ops bnxt_re_dev_ops = {
1308 	.owner = THIS_MODULE,
1309 	.driver_id = RDMA_DRIVER_BNXT_RE,
1310 	.uverbs_abi_ver = BNXT_RE_ABI_VERSION,
1311 
1312 	.add_gid = bnxt_re_add_gid,
1313 	.alloc_hw_port_stats = bnxt_re_ib_alloc_hw_port_stats,
1314 	.alloc_mr = bnxt_re_alloc_mr,
1315 	.alloc_pd = bnxt_re_alloc_pd,
1316 	.alloc_ucontext = bnxt_re_alloc_ucontext,
1317 	.create_ah = bnxt_re_create_ah,
1318 	.create_cq = bnxt_re_create_cq,
1319 	.create_qp = bnxt_re_create_qp,
1320 	.create_srq = bnxt_re_create_srq,
1321 	.create_user_ah = bnxt_re_create_ah,
1322 	.dealloc_pd = bnxt_re_dealloc_pd,
1323 	.dealloc_ucontext = bnxt_re_dealloc_ucontext,
1324 	.del_gid = bnxt_re_del_gid,
1325 	.dereg_mr = bnxt_re_dereg_mr,
1326 	.destroy_ah = bnxt_re_destroy_ah,
1327 	.destroy_cq = bnxt_re_destroy_cq,
1328 	.destroy_qp = bnxt_re_destroy_qp,
1329 	.destroy_srq = bnxt_re_destroy_srq,
1330 	.device_group = &bnxt_re_dev_attr_group,
1331 	.disassociate_ucontext = bnxt_re_disassociate_ucontext,
1332 	.get_dev_fw_str = bnxt_re_query_fw_str,
1333 	.get_dma_mr = bnxt_re_get_dma_mr,
1334 	.get_hw_stats = bnxt_re_ib_get_hw_stats,
1335 	.get_link_layer = bnxt_re_get_link_layer,
1336 	.get_port_immutable = bnxt_re_get_port_immutable,
1337 	.map_mr_sg = bnxt_re_map_mr_sg,
1338 	.mmap = bnxt_re_mmap,
1339 	.mmap_free = bnxt_re_mmap_free,
1340 	.modify_qp = bnxt_re_modify_qp,
1341 	.modify_srq = bnxt_re_modify_srq,
1342 	.poll_cq = bnxt_re_poll_cq,
1343 	.post_recv = bnxt_re_post_recv,
1344 	.post_send = bnxt_re_post_send,
1345 	.post_srq_recv = bnxt_re_post_srq_recv,
1346 	.process_mad = bnxt_re_process_mad,
1347 	.query_ah = bnxt_re_query_ah,
1348 	.query_device = bnxt_re_query_device,
1349 	.modify_device = bnxt_re_modify_device,
1350 	.query_pkey = bnxt_re_query_pkey,
1351 	.query_port = bnxt_re_query_port,
1352 	.query_qp = bnxt_re_query_qp,
1353 	.query_srq = bnxt_re_query_srq,
1354 	.reg_user_mr = bnxt_re_reg_user_mr,
1355 	.reg_user_mr_dmabuf = bnxt_re_reg_user_mr_dmabuf,
1356 	.req_notify_cq = bnxt_re_req_notify_cq,
1357 	.resize_cq = bnxt_re_resize_cq,
1358 	.create_flow = bnxt_re_create_flow,
1359 	.destroy_flow = bnxt_re_destroy_flow,
1360 	INIT_RDMA_OBJ_SIZE(ib_ah, bnxt_re_ah, ib_ah),
1361 	INIT_RDMA_OBJ_SIZE(ib_cq, bnxt_re_cq, ib_cq),
1362 	INIT_RDMA_OBJ_SIZE(ib_pd, bnxt_re_pd, ib_pd),
1363 	INIT_RDMA_OBJ_SIZE(ib_qp, bnxt_re_qp, ib_qp),
1364 	INIT_RDMA_OBJ_SIZE(ib_srq, bnxt_re_srq, ib_srq),
1365 	INIT_RDMA_OBJ_SIZE(ib_ucontext, bnxt_re_ucontext, ib_uctx),
1366 };
1367 
1368 static const struct ib_device_ops restrack_ops = {
1369 	.fill_res_cq_entry = bnxt_re_fill_res_cq_entry,
1370 	.fill_res_cq_entry_raw = bnxt_re_fill_res_cq_entry_raw,
1371 	.fill_res_qp_entry = bnxt_re_fill_res_qp_entry,
1372 	.fill_res_qp_entry_raw = bnxt_re_fill_res_qp_entry_raw,
1373 	.fill_res_mr_entry = bnxt_re_fill_res_mr_entry,
1374 	.fill_res_mr_entry_raw = bnxt_re_fill_res_mr_entry_raw,
1375 	.fill_res_srq_entry = bnxt_re_fill_res_srq_entry,
1376 	.fill_res_srq_entry_raw = bnxt_re_fill_res_srq_entry_raw,
1377 };
1378 
1379 static int bnxt_re_register_ib(struct bnxt_re_dev *rdev)
1380 {
1381 	struct ib_device *ibdev = &rdev->ibdev;
1382 	int ret;
1383 
1384 	/* ib device init */
1385 	ibdev->node_type = RDMA_NODE_IB_CA;
1386 	strscpy(ibdev->node_desc, BNXT_RE_DESC " HCA");
1387 	ibdev->phys_port_cnt = 1;
1388 
1389 	addrconf_addr_eui48((u8 *)&ibdev->node_guid, rdev->netdev->dev_addr);
1390 
1391 	ibdev->num_comp_vectors	= rdev->nqr->num_msix - 1;
1392 	ibdev->dev.parent = &rdev->en_dev->pdev->dev;
1393 	ibdev->local_dma_lkey = BNXT_QPLIB_RSVD_LKEY;
1394 
1395 	if (IS_ENABLED(CONFIG_INFINIBAND_USER_ACCESS))
1396 		ibdev->driver_def = bnxt_re_uapi_defs;
1397 
1398 	ib_set_device_ops(ibdev, &bnxt_re_dev_ops);
1399 	ib_set_device_ops(ibdev, &restrack_ops);
1400 	ret = ib_device_set_netdev(&rdev->ibdev, rdev->netdev, 1);
1401 	if (ret)
1402 		return ret;
1403 
1404 	dma_set_max_seg_size(&rdev->en_dev->pdev->dev, UINT_MAX);
1405 	ibdev->uverbs_cmd_mask |= BIT_ULL(IB_USER_VERBS_CMD_POLL_CQ);
1406 	return ib_register_device(ibdev, "bnxt_re%d", &rdev->en_dev->pdev->dev);
1407 }
1408 
1409 static struct bnxt_re_dev *bnxt_re_dev_add(struct auxiliary_device *adev,
1410 					   struct bnxt_en_dev *en_dev)
1411 {
1412 	struct bnxt_re_dev *rdev;
1413 
1414 	/* Allocate bnxt_re_dev instance here */
1415 	rdev = ib_alloc_device(bnxt_re_dev, ibdev);
1416 	if (!rdev) {
1417 		ibdev_err(NULL, "%s: bnxt_re_dev allocation failure!",
1418 			  ROCE_DRV_MODULE_NAME);
1419 		return NULL;
1420 	}
1421 	/* Default values */
1422 	rdev->netdev = en_dev->net;
1423 	rdev->en_dev = en_dev;
1424 	rdev->adev = adev;
1425 	rdev->id = rdev->en_dev->pdev->devfn;
1426 	INIT_LIST_HEAD(&rdev->qp_list);
1427 	mutex_init(&rdev->qp_lock);
1428 	mutex_init(&rdev->pacing.dbq_lock);
1429 	atomic_set(&rdev->stats.res.qp_count, 0);
1430 	atomic_set(&rdev->stats.res.cq_count, 0);
1431 	atomic_set(&rdev->stats.res.srq_count, 0);
1432 	atomic_set(&rdev->stats.res.mr_count, 0);
1433 	atomic_set(&rdev->stats.res.mw_count, 0);
1434 	atomic_set(&rdev->stats.res.ah_count, 0);
1435 	atomic_set(&rdev->stats.res.pd_count, 0);
1436 	rdev->cosq[0] = 0xFFFF;
1437 	rdev->cosq[1] = 0xFFFF;
1438 	rdev->cq_coalescing.buf_maxtime = BNXT_QPLIB_CQ_COAL_DEF_BUF_MAXTIME;
1439 	if (bnxt_re_chip_gen_p7(en_dev->chip_num)) {
1440 		rdev->cq_coalescing.normal_maxbuf = BNXT_QPLIB_CQ_COAL_DEF_NORMAL_MAXBUF_P7;
1441 		rdev->cq_coalescing.during_maxbuf = BNXT_QPLIB_CQ_COAL_DEF_DURING_MAXBUF_P7;
1442 	} else {
1443 		rdev->cq_coalescing.normal_maxbuf = BNXT_QPLIB_CQ_COAL_DEF_NORMAL_MAXBUF_P5;
1444 		rdev->cq_coalescing.during_maxbuf = BNXT_QPLIB_CQ_COAL_DEF_DURING_MAXBUF_P5;
1445 	}
1446 	rdev->cq_coalescing.en_ring_idle_mode = BNXT_QPLIB_CQ_COAL_DEF_EN_RING_IDLE_MODE;
1447 
1448 	return rdev;
1449 }
1450 
1451 static int bnxt_re_handle_unaffi_async_event(struct creq_func_event
1452 					     *unaffi_async)
1453 {
1454 	switch (unaffi_async->event) {
1455 	case CREQ_FUNC_EVENT_EVENT_TX_WQE_ERROR:
1456 		break;
1457 	case CREQ_FUNC_EVENT_EVENT_TX_DATA_ERROR:
1458 		break;
1459 	case CREQ_FUNC_EVENT_EVENT_RX_WQE_ERROR:
1460 		break;
1461 	case CREQ_FUNC_EVENT_EVENT_RX_DATA_ERROR:
1462 		break;
1463 	case CREQ_FUNC_EVENT_EVENT_CQ_ERROR:
1464 		break;
1465 	case CREQ_FUNC_EVENT_EVENT_TQM_ERROR:
1466 		break;
1467 	case CREQ_FUNC_EVENT_EVENT_CFCQ_ERROR:
1468 		break;
1469 	case CREQ_FUNC_EVENT_EVENT_CFCS_ERROR:
1470 		break;
1471 	case CREQ_FUNC_EVENT_EVENT_CFCC_ERROR:
1472 		break;
1473 	case CREQ_FUNC_EVENT_EVENT_CFCM_ERROR:
1474 		break;
1475 	case CREQ_FUNC_EVENT_EVENT_TIM_ERROR:
1476 		break;
1477 	default:
1478 		return -EINVAL;
1479 	}
1480 	return 0;
1481 }
1482 
1483 static int bnxt_re_handle_qp_async_event(struct creq_qp_event *qp_event,
1484 					 struct bnxt_re_qp *qp)
1485 {
1486 	struct creq_qp_error_notification *err_event;
1487 	struct bnxt_re_srq *srq = NULL;
1488 	struct ib_event event = {};
1489 	unsigned int flags;
1490 
1491 	if (qp->qplib_qp.srq)
1492 		srq =  container_of(qp->qplib_qp.srq, struct bnxt_re_srq,
1493 				    qplib_srq);
1494 
1495 	if (qp->qplib_qp.state == CMDQ_MODIFY_QP_NEW_STATE_ERR &&
1496 	    rdma_is_kernel_res(&qp->ib_qp.res)) {
1497 		flags = bnxt_re_lock_cqs(qp);
1498 		bnxt_qplib_add_flush_qp(&qp->qplib_qp);
1499 		bnxt_re_unlock_cqs(qp, flags);
1500 	}
1501 
1502 	event.device = &qp->rdev->ibdev;
1503 	event.element.qp = &qp->ib_qp;
1504 	event.event = IB_EVENT_QP_FATAL;
1505 
1506 	err_event = (struct creq_qp_error_notification *)qp_event;
1507 
1508 	switch (err_event->req_err_state_reason) {
1509 	case CREQ_QP_ERROR_NOTIFICATION_REQ_ERR_STATE_REASON_REQ_OPCODE_ERROR:
1510 	case CREQ_QP_ERROR_NOTIFICATION_REQ_ERR_STATE_REASON_REQ_TIMEOUT_RETRY_LIMIT:
1511 	case CREQ_QP_ERROR_NOTIFICATION_REQ_ERR_STATE_REASON_REQ_RNR_TIMEOUT_RETRY_LIMIT:
1512 	case CREQ_QP_ERROR_NOTIFICATION_REQ_ERR_STATE_REASON_REQ_NAK_ARRIVAL_2:
1513 	case CREQ_QP_ERROR_NOTIFICATION_REQ_ERR_STATE_REASON_REQ_NAK_ARRIVAL_3:
1514 	case CREQ_QP_ERROR_NOTIFICATION_REQ_ERR_STATE_REASON_REQ_INVALID_READ_RESP:
1515 	case CREQ_QP_ERROR_NOTIFICATION_REQ_ERR_STATE_REASON_REQ_ILLEGAL_BIND:
1516 	case CREQ_QP_ERROR_NOTIFICATION_REQ_ERR_STATE_REASON_REQ_ILLEGAL_FAST_REG:
1517 	case CREQ_QP_ERROR_NOTIFICATION_REQ_ERR_STATE_REASON_REQ_ILLEGAL_INVALIDATE:
1518 	case CREQ_QP_ERROR_NOTIFICATION_REQ_ERR_STATE_REASON_REQ_RETRAN_LOCAL_ERROR:
1519 	case CREQ_QP_ERROR_NOTIFICATION_REQ_ERR_STATE_REASON_REQ_AV_DOMAIN_ERROR:
1520 	case CREQ_QP_ERROR_NOTIFICATION_REQ_ERR_STATE_REASON_REQ_PROD_WQE_MSMTCH_ERROR:
1521 	case CREQ_QP_ERROR_NOTIFICATION_REQ_ERR_STATE_REASON_REQ_PSN_RANGE_CHECK_ERROR:
1522 		event.event = IB_EVENT_QP_ACCESS_ERR;
1523 		break;
1524 	case CREQ_QP_ERROR_NOTIFICATION_REQ_ERR_STATE_REASON_REQ_NAK_ARRIVAL_1:
1525 	case CREQ_QP_ERROR_NOTIFICATION_REQ_ERR_STATE_REASON_REQ_NAK_ARRIVAL_4:
1526 	case CREQ_QP_ERROR_NOTIFICATION_REQ_ERR_STATE_REASON_REQ_READ_RESP_LENGTH:
1527 	case CREQ_QP_ERROR_NOTIFICATION_REQ_ERR_STATE_REASON_REQ_WQE_FORMAT_ERROR:
1528 	case CREQ_QP_ERROR_NOTIFICATION_REQ_ERR_STATE_REASON_REQ_ORRQ_FORMAT_ERROR:
1529 	case CREQ_QP_ERROR_NOTIFICATION_REQ_ERR_STATE_REASON_REQ_INVALID_AVID_ERROR:
1530 	case CREQ_QP_ERROR_NOTIFICATION_REQ_ERR_STATE_REASON_REQ_SERV_TYPE_ERROR:
1531 	case CREQ_QP_ERROR_NOTIFICATION_REQ_ERR_STATE_REASON_REQ_INVALID_OP_ERROR:
1532 		event.event = IB_EVENT_QP_REQ_ERR;
1533 		break;
1534 	case CREQ_QP_ERROR_NOTIFICATION_REQ_ERR_STATE_REASON_REQ_RX_MEMORY_ERROR:
1535 	case CREQ_QP_ERROR_NOTIFICATION_REQ_ERR_STATE_REASON_REQ_TX_MEMORY_ERROR:
1536 	case CREQ_QP_ERROR_NOTIFICATION_REQ_ERR_STATE_REASON_REQ_CMP_ERROR:
1537 	case CREQ_QP_ERROR_NOTIFICATION_REQ_ERR_STATE_REASON_REQ_CQ_LOAD_ERROR:
1538 	case CREQ_QP_ERROR_NOTIFICATION_REQ_ERR_STATE_REASON_REQ_TX_PCI_ERROR:
1539 	case CREQ_QP_ERROR_NOTIFICATION_REQ_ERR_STATE_REASON_REQ_RX_PCI_ERROR:
1540 	case CREQ_QP_ERROR_NOTIFICATION_REQ_ERR_STATE_REASON_REQ_RETX_SETUP_ERROR:
1541 		event.event = IB_EVENT_QP_FATAL;
1542 		break;
1543 
1544 	default:
1545 		break;
1546 	}
1547 
1548 	switch (err_event->res_err_state_reason) {
1549 	case CREQ_QP_ERROR_NOTIFICATION_RES_ERR_STATE_REASON_RES_EXCEED_MAX:
1550 	case CREQ_QP_ERROR_NOTIFICATION_RES_ERR_STATE_REASON_RES_PAYLOAD_LENGTH_MISMATCH:
1551 	case CREQ_QP_ERROR_NOTIFICATION_RES_ERR_STATE_REASON_RES_PSN_SEQ_ERROR_RETRY_LIMIT:
1552 	case CREQ_QP_ERROR_NOTIFICATION_RES_ERR_STATE_REASON_RES_RX_INVALID_R_KEY:
1553 	case CREQ_QP_ERROR_NOTIFICATION_RES_ERR_STATE_REASON_RES_RX_DOMAIN_ERROR:
1554 	case CREQ_QP_ERROR_NOTIFICATION_RES_ERR_STATE_REASON_RES_RX_NO_PERMISSION:
1555 	case CREQ_QP_ERROR_NOTIFICATION_RES_ERR_STATE_REASON_RES_RX_RANGE_ERROR:
1556 	case CREQ_QP_ERROR_NOTIFICATION_RES_ERR_STATE_REASON_RES_TX_INVALID_R_KEY:
1557 	case CREQ_QP_ERROR_NOTIFICATION_RES_ERR_STATE_REASON_RES_TX_DOMAIN_ERROR:
1558 	case CREQ_QP_ERROR_NOTIFICATION_RES_ERR_STATE_REASON_RES_TX_NO_PERMISSION:
1559 	case CREQ_QP_ERROR_NOTIFICATION_RES_ERR_STATE_REASON_RES_TX_RANGE_ERROR:
1560 	case CREQ_QP_ERROR_NOTIFICATION_RES_ERR_STATE_REASON_RES_UNALIGN_ATOMIC:
1561 	case CREQ_QP_ERROR_NOTIFICATION_RES_ERR_STATE_REASON_RES_PSN_NOT_FOUND:
1562 	case CREQ_QP_ERROR_NOTIFICATION_RES_ERR_STATE_REASON_RES_INVALID_DUP_RKEY:
1563 	case CREQ_QP_ERROR_NOTIFICATION_RES_ERR_STATE_REASON_RES_IRRQ_FORMAT_ERROR:
1564 		event.event = IB_EVENT_QP_ACCESS_ERR;
1565 		break;
1566 	case CREQ_QP_ERROR_NOTIFICATION_RES_ERR_STATE_REASON_RES_EXCEEDS_WQE:
1567 	case CREQ_QP_ERROR_NOTIFICATION_RES_ERR_STATE_REASON_RES_WQE_FORMAT_ERROR:
1568 	case CREQ_QP_ERROR_NOTIFICATION_RES_ERR_STATE_REASON_RES_UNSUPPORTED_OPCODE:
1569 	case CREQ_QP_ERROR_NOTIFICATION_RES_ERR_STATE_REASON_RES_REM_INVALIDATE:
1570 	case CREQ_QP_ERROR_NOTIFICATION_RES_ERR_STATE_REASON_RES_OPCODE_ERROR:
1571 		event.event = IB_EVENT_QP_REQ_ERR;
1572 		break;
1573 	case CREQ_QP_ERROR_NOTIFICATION_RES_ERR_STATE_REASON_RES_IRRQ_OFLOW:
1574 	case CREQ_QP_ERROR_NOTIFICATION_RES_ERR_STATE_REASON_RES_CMP_ERROR:
1575 	case CREQ_QP_ERROR_NOTIFICATION_RES_ERR_STATE_REASON_RES_CQ_LOAD_ERROR:
1576 	case CREQ_QP_ERROR_NOTIFICATION_RES_ERR_STATE_REASON_RES_TX_PCI_ERROR:
1577 	case CREQ_QP_ERROR_NOTIFICATION_RES_ERR_STATE_REASON_RES_RX_PCI_ERROR:
1578 	case CREQ_QP_ERROR_NOTIFICATION_RES_ERR_STATE_REASON_RES_MEMORY_ERROR:
1579 		event.event = IB_EVENT_QP_FATAL;
1580 		break;
1581 	case CREQ_QP_ERROR_NOTIFICATION_RES_ERR_STATE_REASON_RES_SRQ_LOAD_ERROR:
1582 	case CREQ_QP_ERROR_NOTIFICATION_RES_ERR_STATE_REASON_RES_SRQ_ERROR:
1583 		if (srq)
1584 			event.event = IB_EVENT_SRQ_ERR;
1585 		break;
1586 	default:
1587 		break;
1588 	}
1589 
1590 	if (err_event->res_err_state_reason || err_event->req_err_state_reason) {
1591 		ibdev_dbg(&qp->rdev->ibdev,
1592 			  "%s %s qp_id: %d cons (%d %d) req (%d %d) res (%d %d)\n",
1593 			   __func__, rdma_is_kernel_res(&qp->ib_qp.res) ? "kernel" : "user",
1594 			   qp->qplib_qp.id,
1595 			   err_event->sq_cons_idx,
1596 			   err_event->rq_cons_idx,
1597 			   err_event->req_slow_path_state,
1598 			   err_event->req_err_state_reason,
1599 			   err_event->res_slow_path_state,
1600 			   err_event->res_err_state_reason);
1601 	} else {
1602 		if (srq)
1603 			event.event = IB_EVENT_QP_LAST_WQE_REACHED;
1604 	}
1605 
1606 	if (event.event == IB_EVENT_SRQ_ERR && srq->ib_srq.event_handler)  {
1607 		(*srq->ib_srq.event_handler)(&event,
1608 				srq->ib_srq.srq_context);
1609 	} else if (event.device && qp->ib_qp.event_handler) {
1610 		qp->ib_qp.event_handler(&event, qp->ib_qp.qp_context);
1611 	}
1612 
1613 	return 0;
1614 }
1615 
1616 static int bnxt_re_handle_cq_async_error(void *event, struct bnxt_re_cq *cq)
1617 {
1618 	struct creq_cq_error_notification *cqerr;
1619 	struct ib_event ibevent = {};
1620 
1621 	cqerr = event;
1622 	switch (cqerr->cq_err_reason) {
1623 	case CREQ_CQ_ERROR_NOTIFICATION_CQ_ERR_REASON_REQ_CQ_INVALID_ERROR:
1624 	case CREQ_CQ_ERROR_NOTIFICATION_CQ_ERR_REASON_REQ_CQ_OVERFLOW_ERROR:
1625 	case CREQ_CQ_ERROR_NOTIFICATION_CQ_ERR_REASON_REQ_CQ_LOAD_ERROR:
1626 	case CREQ_CQ_ERROR_NOTIFICATION_CQ_ERR_REASON_RES_CQ_INVALID_ERROR:
1627 	case CREQ_CQ_ERROR_NOTIFICATION_CQ_ERR_REASON_RES_CQ_OVERFLOW_ERROR:
1628 	case CREQ_CQ_ERROR_NOTIFICATION_CQ_ERR_REASON_RES_CQ_LOAD_ERROR:
1629 		ibevent.event = IB_EVENT_CQ_ERR;
1630 		break;
1631 	default:
1632 		break;
1633 	}
1634 
1635 	if (ibevent.event == IB_EVENT_CQ_ERR && cq->ib_cq.event_handler) {
1636 		ibevent.element.cq = &cq->ib_cq;
1637 		ibevent.device = &cq->rdev->ibdev;
1638 
1639 		ibdev_dbg(&cq->rdev->ibdev,
1640 			  "%s err reason %d\n", __func__, cqerr->cq_err_reason);
1641 		cq->ib_cq.event_handler(&ibevent, cq->ib_cq.cq_context);
1642 	}
1643 
1644 	return 0;
1645 }
1646 
1647 static int bnxt_re_handle_affi_async_event(struct creq_qp_event *affi_async,
1648 					   void *obj)
1649 {
1650 	struct bnxt_qplib_qp *lib_qp;
1651 	struct bnxt_qplib_cq *lib_cq;
1652 	struct bnxt_re_qp *qp;
1653 	struct bnxt_re_cq *cq;
1654 	int rc = 0;
1655 	u8 event;
1656 
1657 	if (!obj)
1658 		return rc; /* QP was already dead, still return success */
1659 
1660 	event = affi_async->event;
1661 	switch (event) {
1662 	case CREQ_QP_EVENT_EVENT_QP_ERROR_NOTIFICATION:
1663 		lib_qp = obj;
1664 		qp = container_of(lib_qp, struct bnxt_re_qp, qplib_qp);
1665 		rc = bnxt_re_handle_qp_async_event(affi_async, qp);
1666 		break;
1667 	case CREQ_QP_EVENT_EVENT_CQ_ERROR_NOTIFICATION:
1668 		lib_cq = obj;
1669 		cq = container_of(lib_cq, struct bnxt_re_cq, qplib_cq);
1670 		rc = bnxt_re_handle_cq_async_error(affi_async, cq);
1671 		break;
1672 	default:
1673 		rc = -EINVAL;
1674 	}
1675 	return rc;
1676 }
1677 
1678 static int bnxt_re_aeq_handler(struct bnxt_qplib_rcfw *rcfw,
1679 			       void *aeqe, void *obj)
1680 {
1681 	struct creq_qp_event *affi_async;
1682 	struct creq_func_event *unaffi_async;
1683 	u8 type;
1684 	int rc;
1685 
1686 	type = ((struct creq_base *)aeqe)->type;
1687 	if (type == CREQ_BASE_TYPE_FUNC_EVENT) {
1688 		unaffi_async = aeqe;
1689 		rc = bnxt_re_handle_unaffi_async_event(unaffi_async);
1690 	} else {
1691 		affi_async = aeqe;
1692 		rc = bnxt_re_handle_affi_async_event(affi_async, obj);
1693 	}
1694 
1695 	return rc;
1696 }
1697 
1698 static int bnxt_re_srqn_handler(struct bnxt_qplib_nq *nq,
1699 				struct bnxt_qplib_srq *handle, u8 event)
1700 {
1701 	struct bnxt_re_srq *srq = container_of(handle, struct bnxt_re_srq,
1702 					       qplib_srq);
1703 	struct ib_event ib_event;
1704 
1705 	ib_event.device = &srq->rdev->ibdev;
1706 	ib_event.element.srq = &srq->ib_srq;
1707 
1708 	if (srq->ib_srq.event_handler) {
1709 		if (event == NQ_SRQ_EVENT_EVENT_SRQ_THRESHOLD_EVENT)
1710 			ib_event.event = IB_EVENT_SRQ_LIMIT_REACHED;
1711 		(*srq->ib_srq.event_handler)(&ib_event,
1712 					     srq->ib_srq.srq_context);
1713 	}
1714 	return 0;
1715 }
1716 
1717 static int bnxt_re_cqn_handler(struct bnxt_qplib_nq *nq,
1718 			       struct bnxt_qplib_cq *handle)
1719 {
1720 	struct bnxt_re_cq *cq = container_of(handle, struct bnxt_re_cq,
1721 					     qplib_cq);
1722 
1723 	if (cq->ib_cq.comp_handler)
1724 		(*cq->ib_cq.comp_handler)(&cq->ib_cq, cq->ib_cq.cq_context);
1725 
1726 	return 0;
1727 }
1728 
1729 static void bnxt_re_cleanup_res(struct bnxt_re_dev *rdev)
1730 {
1731 	int i;
1732 
1733 	for (i = 1; i < rdev->nqr->num_msix; i++)
1734 		bnxt_qplib_disable_nq(&rdev->nqr->nq[i - 1]);
1735 
1736 	if (rdev->qplib_res.rcfw)
1737 		bnxt_qplib_cleanup_res(&rdev->qplib_res);
1738 }
1739 
1740 static int bnxt_re_init_res(struct bnxt_re_dev *rdev)
1741 {
1742 	int num_vec_enabled = 0;
1743 	int rc = 0, i;
1744 	u32 db_offt;
1745 
1746 	bnxt_qplib_init_res(&rdev->qplib_res);
1747 
1748 	mutex_init(&rdev->nqr->load_lock);
1749 
1750 	for (i = 1; i < rdev->nqr->num_msix ; i++) {
1751 		db_offt = rdev->nqr->msix_entries[i].db_offset;
1752 		rc = bnxt_qplib_enable_nq(rdev->en_dev->pdev, &rdev->nqr->nq[i - 1],
1753 					  i - 1, rdev->nqr->msix_entries[i].vector,
1754 					  db_offt, &bnxt_re_cqn_handler,
1755 					  &bnxt_re_srqn_handler);
1756 		if (rc) {
1757 			ibdev_err(&rdev->ibdev,
1758 				  "Failed to enable NQ with rc = 0x%x", rc);
1759 			goto fail;
1760 		}
1761 		num_vec_enabled++;
1762 	}
1763 	return 0;
1764 fail:
1765 	for (i = num_vec_enabled; i >= 0; i--)
1766 		bnxt_qplib_disable_nq(&rdev->nqr->nq[i]);
1767 	return rc;
1768 }
1769 
1770 static void bnxt_re_free_nq_res(struct bnxt_re_dev *rdev)
1771 {
1772 	struct bnxt_qplib_nq *nq;
1773 	u8 type;
1774 	int i;
1775 
1776 	for (i = 0; i < rdev->nqr->num_msix - 1; i++) {
1777 		type = bnxt_qplib_get_ring_type(rdev->chip_ctx);
1778 		nq = &rdev->nqr->nq[i];
1779 		bnxt_re_net_ring_free(rdev, nq->ring_id, type);
1780 		bnxt_qplib_free_nq(nq);
1781 		nq->res = NULL;
1782 	}
1783 }
1784 
1785 static void bnxt_re_free_res(struct bnxt_re_dev *rdev)
1786 {
1787 	bnxt_re_free_nq_res(rdev);
1788 
1789 	if (rdev->qplib_res.dpi_tbl.max) {
1790 		bnxt_qplib_dealloc_dpi(&rdev->qplib_res,
1791 				       &rdev->dpi_privileged);
1792 	}
1793 	if (rdev->qplib_res.rcfw) {
1794 		bnxt_qplib_free_res(&rdev->qplib_res);
1795 		rdev->qplib_res.rcfw = NULL;
1796 	}
1797 }
1798 
1799 static int bnxt_re_alloc_res(struct bnxt_re_dev *rdev)
1800 {
1801 	struct bnxt_re_ring_attr rattr = {};
1802 	int num_vec_created = 0;
1803 	int rc, i;
1804 	u8 type;
1805 
1806 	/* Configure and allocate resources for qplib */
1807 	rdev->qplib_res.rcfw = &rdev->rcfw;
1808 	rc = bnxt_qplib_get_dev_attr(&rdev->rcfw);
1809 	if (rc)
1810 		goto fail;
1811 
1812 	rc = bnxt_qplib_alloc_res(&rdev->qplib_res, rdev->netdev);
1813 	if (rc)
1814 		goto fail;
1815 
1816 	rc = bnxt_qplib_alloc_dpi(&rdev->qplib_res,
1817 				  &rdev->dpi_privileged,
1818 				  rdev, BNXT_QPLIB_DPI_TYPE_KERNEL);
1819 	if (rc)
1820 		goto dealloc_res;
1821 
1822 	for (i = 0; i < rdev->nqr->num_msix - 1; i++) {
1823 		struct bnxt_qplib_nq *nq;
1824 
1825 		nq = &rdev->nqr->nq[i];
1826 		nq->hwq.max_elements = BNXT_QPLIB_NQE_MAX_CNT;
1827 		rc = bnxt_qplib_alloc_nq(&rdev->qplib_res, nq);
1828 		if (rc) {
1829 			ibdev_err(&rdev->ibdev, "Alloc Failed NQ%d rc:%#x",
1830 				  i, rc);
1831 			goto free_nq;
1832 		}
1833 		type = bnxt_qplib_get_ring_type(rdev->chip_ctx);
1834 		rattr.dma_arr = nq->hwq.pbl[PBL_LVL_0].pg_map_arr;
1835 		rattr.pages = nq->hwq.pbl[rdev->nqr->nq[i].hwq.level].pg_count;
1836 		rattr.type = type;
1837 		rattr.mode = RING_ALLOC_REQ_INT_MODE_MSIX;
1838 		rattr.depth = BNXT_QPLIB_NQE_MAX_CNT - 1;
1839 		rattr.lrid = rdev->nqr->msix_entries[i + 1].ring_idx;
1840 		rc = bnxt_re_net_ring_alloc(rdev, &rattr, &nq->ring_id);
1841 		if (rc) {
1842 			ibdev_err(&rdev->ibdev,
1843 				  "Failed to allocate NQ fw id with rc = 0x%x",
1844 				  rc);
1845 			bnxt_qplib_free_nq(nq);
1846 			goto free_nq;
1847 		}
1848 		num_vec_created++;
1849 	}
1850 	return 0;
1851 free_nq:
1852 	for (i = num_vec_created - 1; i >= 0; i--) {
1853 		type = bnxt_qplib_get_ring_type(rdev->chip_ctx);
1854 		bnxt_re_net_ring_free(rdev, rdev->nqr->nq[i].ring_id, type);
1855 		bnxt_qplib_free_nq(&rdev->nqr->nq[i]);
1856 	}
1857 	bnxt_qplib_dealloc_dpi(&rdev->qplib_res,
1858 			       &rdev->dpi_privileged);
1859 dealloc_res:
1860 	bnxt_qplib_free_res(&rdev->qplib_res);
1861 
1862 fail:
1863 	rdev->qplib_res.rcfw = NULL;
1864 	return rc;
1865 }
1866 
1867 static void bnxt_re_dispatch_event(struct ib_device *ibdev, struct ib_qp *qp,
1868 				   u8 port_num, enum ib_event_type event)
1869 {
1870 	struct ib_event ib_event;
1871 
1872 	ib_event.device = ibdev;
1873 	if (qp) {
1874 		ib_event.element.qp = qp;
1875 		ib_event.event = event;
1876 		if (qp->event_handler)
1877 			qp->event_handler(&ib_event, qp->qp_context);
1878 
1879 	} else {
1880 		ib_event.element.port_num = port_num;
1881 		ib_event.event = event;
1882 		ib_dispatch_event(&ib_event);
1883 	}
1884 }
1885 
1886 static bool bnxt_re_is_qp1_or_shadow_qp(struct bnxt_re_dev *rdev,
1887 					struct bnxt_re_qp *qp)
1888 {
1889 	return (qp->ib_qp.qp_type == IB_QPT_GSI) ||
1890 	       (qp == rdev->gsi_ctx.gsi_sqp);
1891 }
1892 
1893 static void bnxt_re_dev_stop(struct bnxt_re_dev *rdev)
1894 {
1895 	struct bnxt_re_qp *qp;
1896 
1897 	mutex_lock(&rdev->qp_lock);
1898 	list_for_each_entry(qp, &rdev->qp_list, list) {
1899 		/* Modify the state of all QPs except QP1/Shadow QP */
1900 		if (!bnxt_re_is_qp1_or_shadow_qp(rdev, qp)) {
1901 			if (qp->qplib_qp.state !=
1902 			    CMDQ_MODIFY_QP_NEW_STATE_RESET &&
1903 			    qp->qplib_qp.state !=
1904 			    CMDQ_MODIFY_QP_NEW_STATE_ERR)
1905 				bnxt_re_dispatch_event(&rdev->ibdev, &qp->ib_qp,
1906 						       1, IB_EVENT_QP_FATAL);
1907 		}
1908 	}
1909 	mutex_unlock(&rdev->qp_lock);
1910 }
1911 
1912 static int bnxt_re_update_gid(struct bnxt_re_dev *rdev)
1913 {
1914 	struct bnxt_qplib_sgid_tbl *sgid_tbl = &rdev->qplib_res.sgid_tbl;
1915 	struct bnxt_qplib_gid gid;
1916 	u16 gid_idx, index;
1917 	int rc = 0;
1918 
1919 	if (!ib_device_try_get(&rdev->ibdev))
1920 		return 0;
1921 
1922 	for (index = 0; index < sgid_tbl->active; index++) {
1923 		gid_idx = sgid_tbl->hw_id[index];
1924 
1925 		if (!memcmp(&sgid_tbl->tbl[index], &bnxt_qplib_gid_zero,
1926 			    sizeof(bnxt_qplib_gid_zero)))
1927 			continue;
1928 		/* need to modify the VLAN enable setting of non VLAN GID only
1929 		 * as setting is done for VLAN GID while adding GID
1930 		 */
1931 		if (sgid_tbl->vlan[index])
1932 			continue;
1933 
1934 		memcpy(&gid, &sgid_tbl->tbl[index], sizeof(gid));
1935 
1936 		rc = bnxt_qplib_update_sgid(sgid_tbl, &gid, gid_idx,
1937 					    rdev->qplib_res.netdev->dev_addr);
1938 	}
1939 
1940 	ib_device_put(&rdev->ibdev);
1941 	return rc;
1942 }
1943 
1944 static u32 bnxt_re_get_priority_mask(struct bnxt_re_dev *rdev)
1945 {
1946 	u32 prio_map = 0, tmp_map = 0;
1947 	struct net_device *netdev;
1948 	struct dcb_app app = {};
1949 
1950 	netdev = rdev->netdev;
1951 
1952 	app.selector = IEEE_8021QAZ_APP_SEL_ETHERTYPE;
1953 	app.protocol = ETH_P_IBOE;
1954 	tmp_map = dcb_ieee_getapp_mask(netdev, &app);
1955 	prio_map = tmp_map;
1956 
1957 	app.selector = IEEE_8021QAZ_APP_SEL_DGRAM;
1958 	app.protocol = ROCE_V2_UDP_DPORT;
1959 	tmp_map = dcb_ieee_getapp_mask(netdev, &app);
1960 	prio_map |= tmp_map;
1961 
1962 	return prio_map;
1963 }
1964 
1965 static int bnxt_re_setup_qos(struct bnxt_re_dev *rdev)
1966 {
1967 	u8 prio_map = 0;
1968 
1969 	/* Get priority for roce */
1970 	prio_map = bnxt_re_get_priority_mask(rdev);
1971 
1972 	if (prio_map == rdev->cur_prio_map)
1973 		return 0;
1974 	rdev->cur_prio_map = prio_map;
1975 	/* Actual priorities are not programmed as they are already
1976 	 * done by L2 driver; just enable or disable priority vlan tagging
1977 	 */
1978 	if ((prio_map == 0 && rdev->qplib_res.prio) ||
1979 	    (prio_map != 0 && !rdev->qplib_res.prio)) {
1980 		rdev->qplib_res.prio = prio_map;
1981 		bnxt_re_update_gid(rdev);
1982 	}
1983 
1984 	return 0;
1985 }
1986 
1987 static void bnxt_re_net_unregister_async_event(struct bnxt_re_dev *rdev)
1988 {
1989 	if (rdev->is_virtfn)
1990 		return;
1991 
1992 	memset(&rdev->event_bitmap, 0, sizeof(rdev->event_bitmap));
1993 	bnxt_register_async_events(rdev->en_dev, &rdev->event_bitmap,
1994 				   ASYNC_EVENT_CMPL_EVENT_ID_DCB_CONFIG_CHANGE);
1995 }
1996 
1997 static void bnxt_re_net_register_async_event(struct bnxt_re_dev *rdev)
1998 {
1999 	if (rdev->is_virtfn)
2000 		return;
2001 
2002 	rdev->event_bitmap |= (1 << ASYNC_EVENT_CMPL_EVENT_ID_DCB_CONFIG_CHANGE);
2003 	bnxt_register_async_events(rdev->en_dev, &rdev->event_bitmap,
2004 				   ASYNC_EVENT_CMPL_EVENT_ID_DCB_CONFIG_CHANGE);
2005 }
2006 
2007 static int bnxt_re_query_hwrm_intf_version(struct bnxt_re_dev *rdev)
2008 {
2009 	struct bnxt_en_dev *en_dev = rdev->en_dev;
2010 	struct hwrm_ver_get_output resp = {};
2011 	struct hwrm_ver_get_input req = {};
2012 	struct bnxt_qplib_chip_ctx *cctx;
2013 	struct bnxt_fw_msg fw_msg = {};
2014 	int rc;
2015 
2016 	bnxt_re_init_hwrm_hdr((void *)&req, HWRM_VER_GET);
2017 	req.hwrm_intf_maj = HWRM_VERSION_MAJOR;
2018 	req.hwrm_intf_min = HWRM_VERSION_MINOR;
2019 	req.hwrm_intf_upd = HWRM_VERSION_UPDATE;
2020 	bnxt_re_fill_fw_msg(&fw_msg, (void *)&req, sizeof(req), (void *)&resp,
2021 			    sizeof(resp), DFLT_HWRM_CMD_TIMEOUT);
2022 	rc = bnxt_send_msg(en_dev, &fw_msg);
2023 	if (rc) {
2024 		ibdev_err(&rdev->ibdev, "Failed to query HW version, rc = 0x%x",
2025 			  rc);
2026 		return rc;
2027 	}
2028 
2029 	cctx = rdev->chip_ctx;
2030 	cctx->hwrm_intf_ver =
2031 		(u64)le16_to_cpu(resp.hwrm_intf_major) << 48 |
2032 		(u64)le16_to_cpu(resp.hwrm_intf_minor) << 32 |
2033 		(u64)le16_to_cpu(resp.hwrm_intf_build) << 16 |
2034 		le16_to_cpu(resp.hwrm_intf_patch);
2035 
2036 	cctx->hwrm_cmd_max_timeout = le16_to_cpu(resp.max_req_timeout);
2037 
2038 	if (!cctx->hwrm_cmd_max_timeout)
2039 		cctx->hwrm_cmd_max_timeout = RCFW_FW_STALL_MAX_TIMEOUT;
2040 
2041 	return 0;
2042 }
2043 
2044 static int bnxt_re_ib_init(struct bnxt_re_dev *rdev)
2045 {
2046 	int rc;
2047 	u32 event;
2048 
2049 	/* Register ib dev */
2050 	rc = bnxt_re_register_ib(rdev);
2051 	if (rc) {
2052 		pr_err("Failed to register with IB: %#x\n", rc);
2053 		return rc;
2054 	}
2055 	dev_info(rdev_to_dev(rdev), "Device registered with IB successfully");
2056 	set_bit(BNXT_RE_FLAG_ISSUE_ROCE_STATS, &rdev->flags);
2057 
2058 	event = netif_running(rdev->netdev) && netif_carrier_ok(rdev->netdev) ?
2059 		IB_EVENT_PORT_ACTIVE : IB_EVENT_PORT_ERR;
2060 
2061 	bnxt_re_dispatch_event(&rdev->ibdev, NULL, 1, event);
2062 
2063 	return rc;
2064 }
2065 
2066 static int bnxt_re_alloc_nqr_mem(struct bnxt_re_dev *rdev)
2067 {
2068 	rdev->nqr = kzalloc(sizeof(*rdev->nqr), GFP_KERNEL);
2069 	if (!rdev->nqr)
2070 		return -ENOMEM;
2071 
2072 	return 0;
2073 }
2074 
2075 static void bnxt_re_free_nqr_mem(struct bnxt_re_dev *rdev)
2076 {
2077 	kfree(rdev->nqr);
2078 	rdev->nqr = NULL;
2079 }
2080 
2081 static int bnxt_re_get_stats_ctx(struct bnxt_re_dev *rdev)
2082 {
2083 	struct bnxt_qplib_ctx *hctx = &rdev->qplib_ctx;
2084 	struct bnxt_qplib_res *res = &rdev->qplib_res;
2085 	int rc;
2086 
2087 	rc = bnxt_qplib_alloc_stats_ctx(res->pdev, res->cctx, &hctx->stats);
2088 	if (rc)
2089 		return rc;
2090 
2091 	rc = bnxt_re_net_stats_ctx_alloc(rdev, &hctx->stats);
2092 	if (rc)
2093 		goto free_stat_mem;
2094 
2095 	return 0;
2096 free_stat_mem:
2097 	bnxt_qplib_free_stats_ctx(res->pdev, &hctx->stats);
2098 
2099 	return rc;
2100 }
2101 
2102 static int bnxt_re_get_stats3_ctx(struct bnxt_re_dev *rdev)
2103 {
2104 	struct bnxt_qplib_ctx *hctx = &rdev->qplib_ctx;
2105 	struct bnxt_qplib_res *res = &rdev->qplib_res;
2106 	int rc;
2107 
2108 	if (!rdev->rcfw.roce_mirror)
2109 		return 0;
2110 
2111 	rc = bnxt_qplib_alloc_stats_ctx(res->pdev, res->cctx, &hctx->stats3);
2112 	if (rc)
2113 		return rc;
2114 
2115 	rc = bnxt_re_net_stats_ctx_alloc(rdev, &hctx->stats3);
2116 	if (rc)
2117 		goto free_stat_mem;
2118 
2119 	return 0;
2120 free_stat_mem:
2121 	bnxt_qplib_free_stats_ctx(res->pdev, &hctx->stats3);
2122 
2123 	return rc;
2124 }
2125 
2126 static void bnxt_re_put_stats3_ctx(struct bnxt_re_dev *rdev)
2127 {
2128 	struct bnxt_qplib_ctx *hctx = &rdev->qplib_ctx;
2129 	struct bnxt_qplib_res *res = &rdev->qplib_res;
2130 
2131 	if (!rdev->rcfw.roce_mirror)
2132 		return;
2133 
2134 	bnxt_re_net_stats_ctx_free(rdev, hctx->stats3.fw_id);
2135 	bnxt_qplib_free_stats_ctx(res->pdev, &hctx->stats3);
2136 }
2137 
2138 static void bnxt_re_put_stats_ctx(struct bnxt_re_dev *rdev)
2139 {
2140 	struct bnxt_qplib_ctx *hctx = &rdev->qplib_ctx;
2141 	struct bnxt_qplib_res *res = &rdev->qplib_res;
2142 
2143 	bnxt_re_net_stats_ctx_free(rdev, hctx->stats.fw_id);
2144 	bnxt_qplib_free_stats_ctx(res->pdev, &hctx->stats);
2145 }
2146 
2147 static void bnxt_re_dev_uninit(struct bnxt_re_dev *rdev, u8 op_type)
2148 {
2149 	u8 type;
2150 	int rc;
2151 
2152 	bnxt_re_debugfs_rem_pdev(rdev);
2153 
2154 	bnxt_re_net_unregister_async_event(rdev);
2155 	bnxt_re_uninit_dcb_wq(rdev);
2156 
2157 	if (test_and_clear_bit(BNXT_RE_FLAG_QOS_WORK_REG, &rdev->flags))
2158 		cancel_delayed_work_sync(&rdev->worker);
2159 
2160 	bnxt_re_put_stats3_ctx(rdev);
2161 
2162 	if (test_and_clear_bit(BNXT_RE_FLAG_RESOURCES_INITIALIZED,
2163 			       &rdev->flags))
2164 		bnxt_re_cleanup_res(rdev);
2165 	if (test_and_clear_bit(BNXT_RE_FLAG_RESOURCES_ALLOCATED, &rdev->flags))
2166 		bnxt_re_free_res(rdev);
2167 
2168 	if (test_and_clear_bit(BNXT_RE_FLAG_RCFW_CHANNEL_EN, &rdev->flags)) {
2169 		rc = bnxt_qplib_deinit_rcfw(&rdev->rcfw);
2170 		if (rc)
2171 			ibdev_warn(&rdev->ibdev,
2172 				   "Failed to deinitialize RCFW: %#x", rc);
2173 		bnxt_re_put_stats_ctx(rdev);
2174 		bnxt_qplib_free_hwctx(&rdev->qplib_res, &rdev->qplib_ctx);
2175 		bnxt_qplib_disable_rcfw_channel(&rdev->rcfw);
2176 		type = bnxt_qplib_get_ring_type(rdev->chip_ctx);
2177 		bnxt_re_net_ring_free(rdev, rdev->rcfw.creq.ring_id, type);
2178 		bnxt_qplib_free_rcfw_channel(&rdev->rcfw);
2179 	}
2180 
2181 	rdev->nqr->num_msix = 0;
2182 
2183 	if (rdev->pacing.dbr_pacing)
2184 		bnxt_re_deinitialize_dbr_pacing(rdev);
2185 
2186 	bnxt_re_free_nqr_mem(rdev);
2187 	bnxt_re_destroy_chip_ctx(rdev);
2188 	if (op_type == BNXT_RE_COMPLETE_REMOVE) {
2189 		if (test_and_clear_bit(BNXT_RE_FLAG_NETDEV_REGISTERED, &rdev->flags))
2190 			bnxt_unregister_dev(rdev->en_dev);
2191 	}
2192 }
2193 
2194 /* worker thread for polling periodic events. Now used for QoS programming*/
2195 static void bnxt_re_worker(struct work_struct *work)
2196 {
2197 	struct bnxt_re_dev *rdev = container_of(work, struct bnxt_re_dev,
2198 						worker.work);
2199 
2200 	bnxt_re_setup_qos(rdev);
2201 	schedule_delayed_work(&rdev->worker, msecs_to_jiffies(30000));
2202 }
2203 
2204 static int bnxt_re_dev_init(struct bnxt_re_dev *rdev, u8 op_type)
2205 {
2206 	struct bnxt_re_ring_attr rattr = {};
2207 	struct bnxt_qplib_creq_ctx *creq;
2208 	u32 db_offt;
2209 	int vid;
2210 	u8 type;
2211 	int rc;
2212 
2213 	if (op_type == BNXT_RE_COMPLETE_INIT) {
2214 		/* Registered a new RoCE device instance to netdev */
2215 		rc = bnxt_re_register_netdev(rdev);
2216 		if (rc) {
2217 			ibdev_err(&rdev->ibdev,
2218 				  "Failed to register with Ethernet driver, rc %d\n",
2219 				  rc);
2220 			return rc;
2221 		}
2222 	}
2223 	set_bit(BNXT_RE_FLAG_NETDEV_REGISTERED, &rdev->flags);
2224 
2225 	if (rdev->en_dev->ulp_tbl->msix_requested < BNXT_RE_MIN_MSIX) {
2226 		ibdev_err(&rdev->ibdev,
2227 			  "RoCE requires minimum 2 MSI-X vectors, but only %d reserved\n",
2228 			  rdev->en_dev->ulp_tbl->msix_requested);
2229 		bnxt_unregister_dev(rdev->en_dev);
2230 		clear_bit(BNXT_RE_FLAG_NETDEV_REGISTERED, &rdev->flags);
2231 		return -EINVAL;
2232 	}
2233 	ibdev_dbg(&rdev->ibdev, "Got %d MSI-X vectors\n",
2234 		  rdev->en_dev->ulp_tbl->msix_requested);
2235 
2236 	rc = bnxt_re_setup_chip_ctx(rdev);
2237 	if (rc) {
2238 		bnxt_unregister_dev(rdev->en_dev);
2239 		clear_bit(BNXT_RE_FLAG_NETDEV_REGISTERED, &rdev->flags);
2240 		ibdev_err(&rdev->ibdev, "Failed to get chip context\n");
2241 		return -EINVAL;
2242 	}
2243 
2244 	rc = bnxt_re_alloc_nqr_mem(rdev);
2245 	if (rc) {
2246 		bnxt_re_destroy_chip_ctx(rdev);
2247 		bnxt_unregister_dev(rdev->en_dev);
2248 		clear_bit(BNXT_RE_FLAG_NETDEV_REGISTERED, &rdev->flags);
2249 		return rc;
2250 	}
2251 	rdev->nqr->num_msix = rdev->en_dev->ulp_tbl->msix_requested;
2252 	memcpy(rdev->nqr->msix_entries, rdev->en_dev->msix_entries,
2253 	       sizeof(struct bnxt_msix_entry) * rdev->nqr->num_msix);
2254 
2255 	/* Check whether VF or PF */
2256 	bnxt_re_get_sriov_func_type(rdev);
2257 
2258 	/* Establish RCFW Communication Channel to initialize the context
2259 	 * memory for the function and all child VFs
2260 	 */
2261 	rc = bnxt_qplib_alloc_rcfw_channel(&rdev->qplib_res, &rdev->rcfw,
2262 					   &rdev->qplib_ctx);
2263 	if (rc) {
2264 		ibdev_err(&rdev->ibdev,
2265 			  "Failed to allocate RCFW Channel: %#x\n", rc);
2266 		goto fail;
2267 	}
2268 
2269 	type = bnxt_qplib_get_ring_type(rdev->chip_ctx);
2270 	creq = &rdev->rcfw.creq;
2271 	rattr.dma_arr = creq->hwq.pbl[PBL_LVL_0].pg_map_arr;
2272 	rattr.pages = creq->hwq.pbl[creq->hwq.level].pg_count;
2273 	rattr.type = type;
2274 	rattr.mode = RING_ALLOC_REQ_INT_MODE_MSIX;
2275 	rattr.depth = BNXT_QPLIB_CREQE_MAX_CNT - 1;
2276 	rattr.lrid = rdev->nqr->msix_entries[BNXT_RE_AEQ_IDX].ring_idx;
2277 	rc = bnxt_re_net_ring_alloc(rdev, &rattr, &creq->ring_id);
2278 	if (rc) {
2279 		ibdev_err(&rdev->ibdev, "Failed to allocate CREQ: %#x\n", rc);
2280 		goto free_rcfw;
2281 	}
2282 	db_offt = rdev->nqr->msix_entries[BNXT_RE_AEQ_IDX].db_offset;
2283 	vid = rdev->nqr->msix_entries[BNXT_RE_AEQ_IDX].vector;
2284 	rc = bnxt_qplib_enable_rcfw_channel(&rdev->rcfw,
2285 					    vid, db_offt,
2286 					    &bnxt_re_aeq_handler);
2287 	if (rc) {
2288 		ibdev_err(&rdev->ibdev, "Failed to enable RCFW channel: %#x\n",
2289 			  rc);
2290 		goto free_ring;
2291 	}
2292 
2293 	if (bnxt_qplib_dbr_pacing_en(rdev->chip_ctx)) {
2294 		rc = bnxt_re_initialize_dbr_pacing(rdev);
2295 		if (!rc) {
2296 			rdev->pacing.dbr_pacing = true;
2297 		} else {
2298 			ibdev_err(&rdev->ibdev,
2299 				  "DBR pacing disabled with error : %d\n", rc);
2300 			rdev->pacing.dbr_pacing = false;
2301 		}
2302 	}
2303 	rc = bnxt_qplib_get_dev_attr(&rdev->rcfw);
2304 	if (rc)
2305 		goto disable_rcfw;
2306 
2307 	bnxt_qplib_query_version(&rdev->rcfw);
2308 	bnxt_re_set_resource_limits(rdev);
2309 
2310 	if (!rdev->is_virtfn &&
2311 	    !bnxt_qplib_is_chip_gen_p5_p7(rdev->chip_ctx)) {
2312 		rc = bnxt_qplib_alloc_hwctx(&rdev->qplib_res, &rdev->qplib_ctx);
2313 		if (rc) {
2314 			ibdev_err(&rdev->ibdev,
2315 				  "Failed to allocate hw context: %#x\n", rc);
2316 			goto disable_rcfw;
2317 		}
2318 	}
2319 
2320 	rc = bnxt_re_get_stats_ctx(rdev);
2321 	if (rc) {
2322 		ibdev_err(&rdev->ibdev,
2323 			  "Failed to allocate stats context: %#x\n", rc);
2324 		goto free_ctx;
2325 	}
2326 
2327 	rc = bnxt_qplib_init_rcfw(&rdev->rcfw, &rdev->qplib_ctx,
2328 				  rdev->is_virtfn);
2329 	if (rc) {
2330 		ibdev_err(&rdev->ibdev,
2331 			  "Failed to initialize RCFW: %#x\n", rc);
2332 		goto free_sctx;
2333 	}
2334 	set_bit(BNXT_RE_FLAG_RCFW_CHANNEL_EN, &rdev->flags);
2335 
2336 	/* Resources based on the 'new' device caps */
2337 	rc = bnxt_re_alloc_res(rdev);
2338 	if (rc) {
2339 		ibdev_err(&rdev->ibdev,
2340 			  "Failed to allocate resources: %#x\n", rc);
2341 		goto fail;
2342 	}
2343 	set_bit(BNXT_RE_FLAG_RESOURCES_ALLOCATED, &rdev->flags);
2344 	rc = bnxt_re_init_res(rdev);
2345 	if (rc) {
2346 		ibdev_err(&rdev->ibdev,
2347 			  "Failed to initialize resources: %#x\n", rc);
2348 		goto fail;
2349 	}
2350 
2351 	set_bit(BNXT_RE_FLAG_RESOURCES_INITIALIZED, &rdev->flags);
2352 
2353 	if (!rdev->is_virtfn) {
2354 		/* Query f/w defaults of CC params */
2355 		rc = bnxt_qplib_query_cc_param(&rdev->qplib_res, &rdev->cc_param);
2356 		if (rc)
2357 			ibdev_warn(&rdev->ibdev, "Failed to query CC defaults\n");
2358 
2359 		rc = bnxt_re_setup_qos(rdev);
2360 		if (rc)
2361 			ibdev_info(&rdev->ibdev,
2362 				   "RoCE priority not yet configured\n");
2363 
2364 		INIT_DELAYED_WORK(&rdev->worker, bnxt_re_worker);
2365 		set_bit(BNXT_RE_FLAG_QOS_WORK_REG, &rdev->flags);
2366 		schedule_delayed_work(&rdev->worker, msecs_to_jiffies(30000));
2367 
2368 		if (!(rdev->qplib_res.en_dev->flags & BNXT_EN_FLAG_ROCE_VF_RES_MGMT))
2369 			bnxt_re_vf_res_config(rdev);
2370 	}
2371 	hash_init(rdev->cq_hash);
2372 	if (rdev->chip_ctx->modes.toggle_bits & BNXT_QPLIB_SRQ_TOGGLE_BIT)
2373 		hash_init(rdev->srq_hash);
2374 
2375 	bnxt_re_debugfs_add_pdev(rdev);
2376 
2377 	bnxt_re_init_dcb_wq(rdev);
2378 	bnxt_re_net_register_async_event(rdev);
2379 
2380 	rc = bnxt_re_get_stats3_ctx(rdev);
2381 	if (rc)
2382 		goto fail;
2383 
2384 	return 0;
2385 free_sctx:
2386 	bnxt_re_net_stats_ctx_free(rdev, rdev->qplib_ctx.stats.fw_id);
2387 free_ctx:
2388 	bnxt_qplib_free_hwctx(&rdev->qplib_res, &rdev->qplib_ctx);
2389 disable_rcfw:
2390 	bnxt_qplib_disable_rcfw_channel(&rdev->rcfw);
2391 free_ring:
2392 	type = bnxt_qplib_get_ring_type(rdev->chip_ctx);
2393 	bnxt_re_net_ring_free(rdev, rdev->rcfw.creq.ring_id, type);
2394 free_rcfw:
2395 	bnxt_qplib_free_rcfw_channel(&rdev->rcfw);
2396 fail:
2397 	bnxt_re_dev_uninit(rdev, BNXT_RE_COMPLETE_REMOVE);
2398 
2399 	return rc;
2400 }
2401 
2402 static void bnxt_re_setup_cc(struct bnxt_re_dev *rdev, bool enable)
2403 {
2404 	struct bnxt_qplib_cc_param cc_param = {};
2405 
2406 	/* Do not enable congestion control on VFs */
2407 	if (rdev->is_virtfn)
2408 		return;
2409 
2410 	/* Currently enabling only for GenP5 adapters */
2411 	if (!bnxt_qplib_is_chip_gen_p5_p7(rdev->chip_ctx))
2412 		return;
2413 
2414 	if (enable) {
2415 		cc_param.enable  = 1;
2416 		cc_param.tos_ecn = 1;
2417 	}
2418 
2419 	cc_param.mask = (CMDQ_MODIFY_ROCE_CC_MODIFY_MASK_ENABLE_CC |
2420 			 CMDQ_MODIFY_ROCE_CC_MODIFY_MASK_TOS_ECN);
2421 
2422 	if (bnxt_qplib_modify_cc(&rdev->qplib_res, &cc_param))
2423 		ibdev_err(&rdev->ibdev, "Failed to setup CC enable = %d\n", enable);
2424 }
2425 
2426 static void bnxt_re_update_en_info_rdev(struct bnxt_re_dev *rdev,
2427 					struct bnxt_re_en_dev_info *en_info,
2428 					struct auxiliary_device *adev)
2429 {
2430 	/* Before updating the rdev pointer in bnxt_re_en_dev_info structure,
2431 	 * take the rtnl lock to avoid accessing invalid rdev pointer from
2432 	 * L2 ULP callbacks. This is applicable in all the places where rdev
2433 	 * pointer is updated in bnxt_re_en_dev_info.
2434 	 */
2435 	rtnl_lock();
2436 	en_info->rdev = rdev;
2437 	rtnl_unlock();
2438 }
2439 
2440 static int bnxt_re_add_device(struct auxiliary_device *adev, u8 op_type)
2441 {
2442 	struct bnxt_aux_priv *aux_priv =
2443 		container_of(adev, struct bnxt_aux_priv, aux_dev);
2444 	struct bnxt_re_en_dev_info *en_info;
2445 	struct bnxt_en_dev *en_dev;
2446 	struct bnxt_re_dev *rdev;
2447 	int rc;
2448 
2449 	en_info = auxiliary_get_drvdata(adev);
2450 	en_dev = en_info->en_dev;
2451 
2452 
2453 	rdev = bnxt_re_dev_add(adev, en_dev);
2454 	if (!rdev || !rdev_to_dev(rdev)) {
2455 		rc = -ENOMEM;
2456 		goto exit;
2457 	}
2458 
2459 	bnxt_re_update_en_info_rdev(rdev, en_info, adev);
2460 
2461 	rc = bnxt_re_dev_init(rdev, op_type);
2462 	if (rc)
2463 		goto re_dev_dealloc;
2464 
2465 	rc = bnxt_re_ib_init(rdev);
2466 	if (rc) {
2467 		pr_err("Failed to register with IB: %s",
2468 			aux_priv->aux_dev.name);
2469 		goto re_dev_uninit;
2470 	}
2471 
2472 	bnxt_re_setup_cc(rdev, true);
2473 
2474 	return 0;
2475 
2476 re_dev_uninit:
2477 	bnxt_re_update_en_info_rdev(NULL, en_info, adev);
2478 	bnxt_re_dev_uninit(rdev, BNXT_RE_COMPLETE_REMOVE);
2479 re_dev_dealloc:
2480 	ib_dealloc_device(&rdev->ibdev);
2481 exit:
2482 	return rc;
2483 }
2484 
2485 #define BNXT_ADEV_NAME "bnxt_en"
2486 
2487 static void bnxt_re_remove_device(struct bnxt_re_dev *rdev, u8 op_type,
2488 				  struct auxiliary_device *aux_dev)
2489 {
2490 	bnxt_re_setup_cc(rdev, false);
2491 	ib_unregister_device(&rdev->ibdev);
2492 	bnxt_re_dev_uninit(rdev, op_type);
2493 	ib_dealloc_device(&rdev->ibdev);
2494 }
2495 
2496 static void bnxt_re_remove(struct auxiliary_device *adev)
2497 {
2498 	struct bnxt_re_en_dev_info *en_info = auxiliary_get_drvdata(adev);
2499 	struct bnxt_re_dev *rdev;
2500 
2501 	mutex_lock(&bnxt_re_mutex);
2502 	rdev = en_info->rdev;
2503 
2504 	if (rdev)
2505 		bnxt_re_remove_device(rdev, BNXT_RE_COMPLETE_REMOVE, adev);
2506 	kfree(en_info);
2507 	mutex_unlock(&bnxt_re_mutex);
2508 }
2509 
2510 static int bnxt_re_probe(struct auxiliary_device *adev,
2511 			 const struct auxiliary_device_id *id)
2512 {
2513 	struct bnxt_aux_priv *aux_priv =
2514 		container_of(adev, struct bnxt_aux_priv, aux_dev);
2515 	struct bnxt_re_en_dev_info *en_info;
2516 	struct bnxt_en_dev *en_dev;
2517 	int rc;
2518 
2519 	en_dev = aux_priv->edev;
2520 
2521 	mutex_lock(&bnxt_re_mutex);
2522 	en_info = kzalloc(sizeof(*en_info), GFP_KERNEL);
2523 	if (!en_info) {
2524 		mutex_unlock(&bnxt_re_mutex);
2525 		return -ENOMEM;
2526 	}
2527 	en_info->en_dev = en_dev;
2528 
2529 	auxiliary_set_drvdata(adev, en_info);
2530 
2531 	rc = bnxt_re_add_device(adev, BNXT_RE_COMPLETE_INIT);
2532 	if (rc)
2533 		kfree(en_info);
2534 
2535 	mutex_unlock(&bnxt_re_mutex);
2536 
2537 	return rc;
2538 }
2539 
2540 static int bnxt_re_suspend(struct auxiliary_device *adev, pm_message_t state)
2541 {
2542 	struct bnxt_re_en_dev_info *en_info = auxiliary_get_drvdata(adev);
2543 	struct bnxt_en_dev *en_dev;
2544 	struct bnxt_re_dev *rdev;
2545 
2546 	rdev = en_info->rdev;
2547 	en_dev = en_info->en_dev;
2548 	mutex_lock(&bnxt_re_mutex);
2549 
2550 	ibdev_info(&rdev->ibdev, "Handle device suspend call");
2551 	/* Check the current device state from bnxt_en_dev and move the
2552 	 * device to detached state if FW_FATAL_COND is set.
2553 	 * This prevents more commands to HW during clean-up,
2554 	 * in case the device is already in error.
2555 	 */
2556 	if (test_bit(BNXT_STATE_FW_FATAL_COND, &rdev->en_dev->en_state)) {
2557 		set_bit(ERR_DEVICE_DETACHED, &rdev->rcfw.cmdq.flags);
2558 		set_bit(BNXT_RE_FLAG_ERR_DEVICE_DETACHED, &rdev->flags);
2559 		wake_up_all(&rdev->rcfw.cmdq.waitq);
2560 		bnxt_re_dev_stop(rdev);
2561 	}
2562 
2563 	if (rdev->pacing.dbr_pacing)
2564 		bnxt_re_set_pacing_dev_state(rdev);
2565 
2566 	ibdev_info(&rdev->ibdev, "%s: L2 driver notified to stop en_state 0x%lx",
2567 		   __func__, en_dev->en_state);
2568 	bnxt_re_remove_device(rdev, BNXT_RE_PRE_RECOVERY_REMOVE, adev);
2569 	bnxt_re_update_en_info_rdev(NULL, en_info, adev);
2570 	mutex_unlock(&bnxt_re_mutex);
2571 
2572 	return 0;
2573 }
2574 
2575 static int bnxt_re_resume(struct auxiliary_device *adev)
2576 {
2577 	struct bnxt_re_en_dev_info *en_info = auxiliary_get_drvdata(adev);
2578 	struct bnxt_re_dev *rdev;
2579 
2580 	mutex_lock(&bnxt_re_mutex);
2581 	bnxt_re_add_device(adev, BNXT_RE_POST_RECOVERY_INIT);
2582 	rdev = en_info->rdev;
2583 	ibdev_info(&rdev->ibdev, "Device resume completed");
2584 	mutex_unlock(&bnxt_re_mutex);
2585 
2586 	return 0;
2587 }
2588 
2589 static void bnxt_re_shutdown(struct auxiliary_device *adev)
2590 {
2591 	struct bnxt_re_en_dev_info *en_info = auxiliary_get_drvdata(adev);
2592 	struct bnxt_re_dev *rdev;
2593 
2594 	rdev = en_info->rdev;
2595 	ib_unregister_device(&rdev->ibdev);
2596 	bnxt_re_dev_uninit(rdev, BNXT_RE_COMPLETE_REMOVE);
2597 }
2598 
2599 static const struct auxiliary_device_id bnxt_re_id_table[] = {
2600 	{ .name = BNXT_ADEV_NAME ".rdma", },
2601 	{},
2602 };
2603 
2604 MODULE_DEVICE_TABLE(auxiliary, bnxt_re_id_table);
2605 
2606 static struct auxiliary_driver bnxt_re_driver = {
2607 	.name = "rdma",
2608 	.probe = bnxt_re_probe,
2609 	.remove = bnxt_re_remove,
2610 	.shutdown = bnxt_re_shutdown,
2611 	.suspend = bnxt_re_suspend,
2612 	.resume = bnxt_re_resume,
2613 	.id_table = bnxt_re_id_table,
2614 };
2615 
2616 static int __init bnxt_re_mod_init(void)
2617 {
2618 	int rc;
2619 
2620 	pr_info("%s: %s", ROCE_DRV_MODULE_NAME, version);
2621 	bnxt_re_register_debugfs();
2622 
2623 	rc = auxiliary_driver_register(&bnxt_re_driver);
2624 	if (rc) {
2625 		pr_err("%s: Failed to register auxiliary driver\n",
2626 			ROCE_DRV_MODULE_NAME);
2627 		goto err_debug;
2628 	}
2629 	return 0;
2630 err_debug:
2631 	bnxt_re_unregister_debugfs();
2632 	return rc;
2633 }
2634 
2635 static void __exit bnxt_re_mod_exit(void)
2636 {
2637 	auxiliary_driver_unregister(&bnxt_re_driver);
2638 	bnxt_re_unregister_debugfs();
2639 }
2640 
2641 module_init(bnxt_re_mod_init);
2642 module_exit(bnxt_re_mod_exit);
2643