xref: /openbmc/linux/drivers/infiniband/hw/bnxt_re/main.c (revision d10cd7bf574ead01fae140ce117a11bcdacbe6a8)
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 
58 #include "bnxt_ulp.h"
59 #include "roce_hsi.h"
60 #include "qplib_res.h"
61 #include "qplib_sp.h"
62 #include "qplib_fp.h"
63 #include "qplib_rcfw.h"
64 #include "bnxt_re.h"
65 #include "ib_verbs.h"
66 #include <rdma/bnxt_re-abi.h>
67 #include "bnxt.h"
68 #include "hw_counters.h"
69 
70 static char version[] =
71 		BNXT_RE_DESC "\n";
72 
73 MODULE_AUTHOR("Eddie Wai <eddie.wai@broadcom.com>");
74 MODULE_DESCRIPTION(BNXT_RE_DESC);
75 MODULE_LICENSE("Dual BSD/GPL");
76 
77 /* globals */
78 static DEFINE_MUTEX(bnxt_re_mutex);
79 
80 static void bnxt_re_stop_irq(void *handle);
81 static void bnxt_re_dev_stop(struct bnxt_re_dev *rdev);
82 static int bnxt_re_netdev_event(struct notifier_block *notifier,
83 				unsigned long event, void *ptr);
84 static struct bnxt_re_dev *bnxt_re_from_netdev(struct net_device *netdev);
85 static void bnxt_re_dev_uninit(struct bnxt_re_dev *rdev);
86 static int bnxt_re_hwrm_qcaps(struct bnxt_re_dev *rdev);
87 
88 static int bnxt_re_hwrm_qcfg(struct bnxt_re_dev *rdev, u32 *db_len,
89 			     u32 *offset);
90 static void bnxt_re_set_db_offset(struct bnxt_re_dev *rdev)
91 {
92 	struct bnxt_qplib_chip_ctx *cctx;
93 	struct bnxt_en_dev *en_dev;
94 	struct bnxt_qplib_res *res;
95 	u32 l2db_len = 0;
96 	u32 offset = 0;
97 	u32 barlen;
98 	int rc;
99 
100 	res = &rdev->qplib_res;
101 	en_dev = rdev->en_dev;
102 	cctx = rdev->chip_ctx;
103 
104 	/* Issue qcfg */
105 	rc = bnxt_re_hwrm_qcfg(rdev, &l2db_len, &offset);
106 	if (rc)
107 		dev_info(rdev_to_dev(rdev),
108 			 "Couldn't get DB bar size, Low latency framework is disabled\n");
109 	/* set register offsets for both UC and WC */
110 	if (bnxt_qplib_is_chip_gen_p7(cctx))
111 		res->dpi_tbl.ucreg.offset = offset;
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 	/* If WC mapping is disabled by L2 driver then en_dev->l2_db_size
118 	 * is equal to the DB-Bar actual size. This indicates that L2
119 	 * is mapping entire bar as UC-. RoCE driver can't enable WC mapping
120 	 * in such cases and DB-push will be disabled.
121 	 */
122 	barlen = pci_resource_len(res->pdev, RCFW_DBR_PCI_BAR_REGION);
123 	if (cctx->modes.db_push && l2db_len && en_dev->l2_db_size != barlen) {
124 		res->dpi_tbl.wcreg.offset = en_dev->l2_db_size;
125 		dev_info(rdev_to_dev(rdev),  "Low latency framework is enabled\n");
126 	}
127 }
128 
129 static void bnxt_re_set_drv_mode(struct bnxt_re_dev *rdev, u8 mode)
130 {
131 	struct bnxt_qplib_chip_ctx *cctx;
132 
133 	cctx = rdev->chip_ctx;
134 	cctx->modes.wqe_mode = bnxt_qplib_is_chip_gen_p5_p7(rdev->chip_ctx) ?
135 			       mode : BNXT_QPLIB_WQE_MODE_STATIC;
136 	if (bnxt_re_hwrm_qcaps(rdev))
137 		dev_err(rdev_to_dev(rdev),
138 			"Failed to query hwrm qcaps\n");
139 }
140 
141 static void bnxt_re_destroy_chip_ctx(struct bnxt_re_dev *rdev)
142 {
143 	struct bnxt_qplib_chip_ctx *chip_ctx;
144 
145 	if (!rdev->chip_ctx)
146 		return;
147 	chip_ctx = rdev->chip_ctx;
148 	rdev->chip_ctx = NULL;
149 	rdev->rcfw.res = NULL;
150 	rdev->qplib_res.cctx = NULL;
151 	rdev->qplib_res.pdev = NULL;
152 	rdev->qplib_res.netdev = NULL;
153 	kfree(chip_ctx);
154 }
155 
156 static int bnxt_re_setup_chip_ctx(struct bnxt_re_dev *rdev, u8 wqe_mode)
157 {
158 	struct bnxt_qplib_chip_ctx *chip_ctx;
159 	struct bnxt_en_dev *en_dev;
160 	int rc;
161 
162 	en_dev = rdev->en_dev;
163 
164 	chip_ctx = kzalloc(sizeof(*chip_ctx), GFP_KERNEL);
165 	if (!chip_ctx)
166 		return -ENOMEM;
167 	chip_ctx->chip_num = en_dev->chip_num;
168 	chip_ctx->hw_stats_size = en_dev->hw_ring_stats_size;
169 
170 	rdev->chip_ctx = chip_ctx;
171 	/* rest members to follow eventually */
172 
173 	rdev->qplib_res.cctx = rdev->chip_ctx;
174 	rdev->rcfw.res = &rdev->qplib_res;
175 	rdev->qplib_res.dattr = &rdev->dev_attr;
176 	rdev->qplib_res.is_vf = BNXT_EN_VF(en_dev);
177 
178 	bnxt_re_set_drv_mode(rdev, wqe_mode);
179 
180 	bnxt_re_set_db_offset(rdev);
181 	rc = bnxt_qplib_map_db_bar(&rdev->qplib_res);
182 	if (rc) {
183 		kfree(rdev->chip_ctx);
184 		rdev->chip_ctx = NULL;
185 		return rc;
186 	}
187 
188 	if (bnxt_qplib_determine_atomics(en_dev->pdev))
189 		ibdev_info(&rdev->ibdev,
190 			   "platform doesn't support global atomics.");
191 	return 0;
192 }
193 
194 /* SR-IOV helper functions */
195 
196 static void bnxt_re_get_sriov_func_type(struct bnxt_re_dev *rdev)
197 {
198 	if (BNXT_EN_VF(rdev->en_dev))
199 		rdev->is_virtfn = 1;
200 }
201 
202 /* Set the maximum number of each resource that the driver actually wants
203  * to allocate. This may be up to the maximum number the firmware has
204  * reserved for the function. The driver may choose to allocate fewer
205  * resources than the firmware maximum.
206  */
207 static void bnxt_re_limit_pf_res(struct bnxt_re_dev *rdev)
208 {
209 	struct bnxt_qplib_dev_attr *attr;
210 	struct bnxt_qplib_ctx *ctx;
211 	int i;
212 
213 	attr = &rdev->dev_attr;
214 	ctx = &rdev->qplib_ctx;
215 
216 	ctx->qpc_count = min_t(u32, BNXT_RE_MAX_QPC_COUNT,
217 			       attr->max_qp);
218 	ctx->mrw_count = BNXT_RE_MAX_MRW_COUNT_256K;
219 	/* Use max_mr from fw since max_mrw does not get set */
220 	ctx->mrw_count = min_t(u32, ctx->mrw_count, attr->max_mr);
221 	ctx->srqc_count = min_t(u32, BNXT_RE_MAX_SRQC_COUNT,
222 				attr->max_srq);
223 	ctx->cq_count = min_t(u32, BNXT_RE_MAX_CQ_COUNT, attr->max_cq);
224 	if (!bnxt_qplib_is_chip_gen_p5_p7(rdev->chip_ctx))
225 		for (i = 0; i < MAX_TQM_ALLOC_REQ; i++)
226 			rdev->qplib_ctx.tqm_ctx.qcount[i] =
227 			rdev->dev_attr.tqm_alloc_reqs[i];
228 }
229 
230 static void bnxt_re_limit_vf_res(struct bnxt_qplib_ctx *qplib_ctx, u32 num_vf)
231 {
232 	struct bnxt_qplib_vf_res *vf_res;
233 	u32 mrws = 0;
234 	u32 vf_pct;
235 	u32 nvfs;
236 
237 	vf_res = &qplib_ctx->vf_res;
238 	/*
239 	 * Reserve a set of resources for the PF. Divide the remaining
240 	 * resources among the VFs
241 	 */
242 	vf_pct = 100 - BNXT_RE_PCT_RSVD_FOR_PF;
243 	nvfs = num_vf;
244 	num_vf = 100 * num_vf;
245 	vf_res->max_qp_per_vf = (qplib_ctx->qpc_count * vf_pct) / num_vf;
246 	vf_res->max_srq_per_vf = (qplib_ctx->srqc_count * vf_pct) / num_vf;
247 	vf_res->max_cq_per_vf = (qplib_ctx->cq_count * vf_pct) / num_vf;
248 	/*
249 	 * The driver allows many more MRs than other resources. If the
250 	 * firmware does also, then reserve a fixed amount for the PF and
251 	 * divide the rest among VFs. VFs may use many MRs for NFS
252 	 * mounts, ISER, NVME applications, etc. If the firmware severely
253 	 * restricts the number of MRs, then let PF have half and divide
254 	 * the rest among VFs, as for the other resource types.
255 	 */
256 	if (qplib_ctx->mrw_count < BNXT_RE_MAX_MRW_COUNT_64K) {
257 		mrws = qplib_ctx->mrw_count * vf_pct;
258 		nvfs = num_vf;
259 	} else {
260 		mrws = qplib_ctx->mrw_count - BNXT_RE_RESVD_MR_FOR_PF;
261 	}
262 	vf_res->max_mrw_per_vf = (mrws / nvfs);
263 	vf_res->max_gid_per_vf = BNXT_RE_MAX_GID_PER_VF;
264 }
265 
266 static void bnxt_re_set_resource_limits(struct bnxt_re_dev *rdev)
267 {
268 	u32 num_vfs;
269 
270 	memset(&rdev->qplib_ctx.vf_res, 0, sizeof(struct bnxt_qplib_vf_res));
271 	bnxt_re_limit_pf_res(rdev);
272 
273 	num_vfs =  bnxt_qplib_is_chip_gen_p5_p7(rdev->chip_ctx) ?
274 			BNXT_RE_GEN_P5_MAX_VF : rdev->num_vfs;
275 	if (num_vfs)
276 		bnxt_re_limit_vf_res(&rdev->qplib_ctx, num_vfs);
277 }
278 
279 static void bnxt_re_vf_res_config(struct bnxt_re_dev *rdev)
280 {
281 
282 	if (test_bit(BNXT_RE_FLAG_ERR_DEVICE_DETACHED, &rdev->flags))
283 		return;
284 	rdev->num_vfs = pci_sriov_get_totalvfs(rdev->en_dev->pdev);
285 	if (!bnxt_qplib_is_chip_gen_p5_p7(rdev->chip_ctx)) {
286 		bnxt_re_set_resource_limits(rdev);
287 		bnxt_qplib_set_func_resources(&rdev->qplib_res, &rdev->rcfw,
288 					      &rdev->qplib_ctx);
289 	}
290 }
291 
292 static void bnxt_re_shutdown(struct auxiliary_device *adev)
293 {
294 	struct bnxt_re_dev *rdev = auxiliary_get_drvdata(adev);
295 
296 	if (!rdev)
297 		return;
298 	ib_unregister_device(&rdev->ibdev);
299 	bnxt_re_dev_uninit(rdev);
300 }
301 
302 static void bnxt_re_stop_irq(void *handle)
303 {
304 	struct bnxt_re_dev *rdev = (struct bnxt_re_dev *)handle;
305 	struct bnxt_qplib_rcfw *rcfw = &rdev->rcfw;
306 	struct bnxt_qplib_nq *nq;
307 	int indx;
308 
309 	for (indx = BNXT_RE_NQ_IDX; indx < rdev->num_msix; indx++) {
310 		nq = &rdev->nq[indx - 1];
311 		bnxt_qplib_nq_stop_irq(nq, false);
312 	}
313 
314 	bnxt_qplib_rcfw_stop_irq(rcfw, false);
315 }
316 
317 static void bnxt_re_start_irq(void *handle, struct bnxt_msix_entry *ent)
318 {
319 	struct bnxt_re_dev *rdev = (struct bnxt_re_dev *)handle;
320 	struct bnxt_msix_entry *msix_ent = rdev->en_dev->msix_entries;
321 	struct bnxt_qplib_rcfw *rcfw = &rdev->rcfw;
322 	struct bnxt_qplib_nq *nq;
323 	int indx, rc;
324 
325 	if (!ent) {
326 		/* Not setting the f/w timeout bit in rcfw.
327 		 * During the driver unload the first command
328 		 * to f/w will timeout and that will set the
329 		 * timeout bit.
330 		 */
331 		ibdev_err(&rdev->ibdev, "Failed to re-start IRQs\n");
332 		return;
333 	}
334 
335 	/* Vectors may change after restart, so update with new vectors
336 	 * in device sctructure.
337 	 */
338 	for (indx = 0; indx < rdev->num_msix; indx++)
339 		rdev->en_dev->msix_entries[indx].vector = ent[indx].vector;
340 
341 	rc = bnxt_qplib_rcfw_start_irq(rcfw, msix_ent[BNXT_RE_AEQ_IDX].vector,
342 				       false);
343 	if (rc) {
344 		ibdev_warn(&rdev->ibdev, "Failed to reinit CREQ\n");
345 		return;
346 	}
347 	for (indx = BNXT_RE_NQ_IDX ; indx < rdev->num_msix; indx++) {
348 		nq = &rdev->nq[indx - 1];
349 		rc = bnxt_qplib_nq_start_irq(nq, indx - 1,
350 					     msix_ent[indx].vector, false);
351 		if (rc) {
352 			ibdev_warn(&rdev->ibdev, "Failed to reinit NQ index %d\n",
353 				   indx - 1);
354 			return;
355 		}
356 	}
357 }
358 
359 static struct bnxt_ulp_ops bnxt_re_ulp_ops = {
360 	.ulp_irq_stop = bnxt_re_stop_irq,
361 	.ulp_irq_restart = bnxt_re_start_irq
362 };
363 
364 /* RoCE -> Net driver */
365 
366 static int bnxt_re_register_netdev(struct bnxt_re_dev *rdev)
367 {
368 	struct bnxt_en_dev *en_dev;
369 	int rc;
370 
371 	en_dev = rdev->en_dev;
372 
373 	rc = bnxt_register_dev(en_dev, &bnxt_re_ulp_ops, rdev);
374 	if (!rc)
375 		rdev->qplib_res.pdev = rdev->en_dev->pdev;
376 	return rc;
377 }
378 
379 static void bnxt_re_init_hwrm_hdr(struct input *hdr, u16 opcd)
380 {
381 	hdr->req_type = cpu_to_le16(opcd);
382 	hdr->cmpl_ring = cpu_to_le16(-1);
383 	hdr->target_id = cpu_to_le16(-1);
384 }
385 
386 static void bnxt_re_fill_fw_msg(struct bnxt_fw_msg *fw_msg, void *msg,
387 				int msg_len, void *resp, int resp_max_len,
388 				int timeout)
389 {
390 	fw_msg->msg = msg;
391 	fw_msg->msg_len = msg_len;
392 	fw_msg->resp = resp;
393 	fw_msg->resp_max_len = resp_max_len;
394 	fw_msg->timeout = timeout;
395 }
396 
397 /* Query device config using common hwrm */
398 static int bnxt_re_hwrm_qcfg(struct bnxt_re_dev *rdev, u32 *db_len,
399 			     u32 *offset)
400 {
401 	struct bnxt_en_dev *en_dev = rdev->en_dev;
402 	struct hwrm_func_qcfg_output resp = {0};
403 	struct hwrm_func_qcfg_input req = {0};
404 	struct bnxt_fw_msg fw_msg = {};
405 	int rc;
406 
407 	bnxt_re_init_hwrm_hdr((void *)&req, HWRM_FUNC_QCFG);
408 	req.fid = cpu_to_le16(0xffff);
409 	bnxt_re_fill_fw_msg(&fw_msg, (void *)&req, sizeof(req), (void *)&resp,
410 			    sizeof(resp), DFLT_HWRM_CMD_TIMEOUT);
411 	rc = bnxt_send_msg(en_dev, &fw_msg);
412 	if (!rc) {
413 		*db_len = PAGE_ALIGN(le16_to_cpu(resp.l2_doorbell_bar_size_kb) * 1024);
414 		*offset = PAGE_ALIGN(le16_to_cpu(resp.legacy_l2_db_size_kb) * 1024);
415 	}
416 	return rc;
417 }
418 
419 /* Query function capabilities using common hwrm */
420 int bnxt_re_hwrm_qcaps(struct bnxt_re_dev *rdev)
421 {
422 	struct bnxt_en_dev *en_dev = rdev->en_dev;
423 	struct hwrm_func_qcaps_output resp = {};
424 	struct hwrm_func_qcaps_input req = {};
425 	struct bnxt_qplib_chip_ctx *cctx;
426 	struct bnxt_fw_msg fw_msg = {};
427 	int rc;
428 
429 	cctx = rdev->chip_ctx;
430 	bnxt_re_init_hwrm_hdr((void *)&req, HWRM_FUNC_QCAPS);
431 	req.fid = cpu_to_le16(0xffff);
432 	bnxt_re_fill_fw_msg(&fw_msg, (void *)&req, sizeof(req), (void *)&resp,
433 			    sizeof(resp), DFLT_HWRM_CMD_TIMEOUT);
434 
435 	rc = bnxt_send_msg(en_dev, &fw_msg);
436 	if (rc)
437 		return rc;
438 	cctx->modes.db_push = le32_to_cpu(resp.flags) & FUNC_QCAPS_RESP_FLAGS_WCB_PUSH_MODE;
439 
440 	cctx->modes.dbr_pacing =
441 		le32_to_cpu(resp.flags_ext2) &
442 		FUNC_QCAPS_RESP_FLAGS_EXT2_DBR_PACING_EXT_SUPPORTED;
443 	return 0;
444 }
445 
446 static int bnxt_re_hwrm_dbr_pacing_qcfg(struct bnxt_re_dev *rdev)
447 {
448 	struct hwrm_func_dbr_pacing_qcfg_output resp = {};
449 	struct hwrm_func_dbr_pacing_qcfg_input req = {};
450 	struct bnxt_en_dev *en_dev = rdev->en_dev;
451 	struct bnxt_qplib_chip_ctx *cctx;
452 	struct bnxt_fw_msg fw_msg = {};
453 	int rc;
454 
455 	cctx = rdev->chip_ctx;
456 	bnxt_re_init_hwrm_hdr((void *)&req, HWRM_FUNC_DBR_PACING_QCFG);
457 	bnxt_re_fill_fw_msg(&fw_msg, (void *)&req, sizeof(req), (void *)&resp,
458 			    sizeof(resp), DFLT_HWRM_CMD_TIMEOUT);
459 	rc = bnxt_send_msg(en_dev, &fw_msg);
460 	if (rc)
461 		return rc;
462 
463 	if ((le32_to_cpu(resp.dbr_stat_db_fifo_reg) &
464 	    FUNC_DBR_PACING_QCFG_RESP_DBR_STAT_DB_FIFO_REG_ADDR_SPACE_MASK) ==
465 		FUNC_DBR_PACING_QCFG_RESP_DBR_STAT_DB_FIFO_REG_ADDR_SPACE_GRC)
466 		cctx->dbr_stat_db_fifo =
467 			le32_to_cpu(resp.dbr_stat_db_fifo_reg) &
468 			~FUNC_DBR_PACING_QCFG_RESP_DBR_STAT_DB_FIFO_REG_ADDR_SPACE_MASK;
469 	return 0;
470 }
471 
472 /* Update the pacing tunable parameters to the default values */
473 static void bnxt_re_set_default_pacing_data(struct bnxt_re_dev *rdev)
474 {
475 	struct bnxt_qplib_db_pacing_data *pacing_data = rdev->qplib_res.pacing_data;
476 
477 	pacing_data->do_pacing = rdev->pacing.dbr_def_do_pacing;
478 	pacing_data->pacing_th = rdev->pacing.pacing_algo_th;
479 	pacing_data->alarm_th =
480 		pacing_data->pacing_th * BNXT_RE_PACING_ALARM_TH_MULTIPLE;
481 }
482 
483 static void __wait_for_fifo_occupancy_below_th(struct bnxt_re_dev *rdev)
484 {
485 	u32 read_val, fifo_occup;
486 
487 	/* loop shouldn't run infintely as the occupancy usually goes
488 	 * below pacing algo threshold as soon as pacing kicks in.
489 	 */
490 	while (1) {
491 		read_val = readl(rdev->en_dev->bar0 + rdev->pacing.dbr_db_fifo_reg_off);
492 		fifo_occup = BNXT_RE_MAX_FIFO_DEPTH -
493 			((read_val & BNXT_RE_DB_FIFO_ROOM_MASK) >>
494 			 BNXT_RE_DB_FIFO_ROOM_SHIFT);
495 		/* Fifo occupancy cannot be greater the MAX FIFO depth */
496 		if (fifo_occup > BNXT_RE_MAX_FIFO_DEPTH)
497 			break;
498 
499 		if (fifo_occup < rdev->qplib_res.pacing_data->pacing_th)
500 			break;
501 	}
502 }
503 
504 static void bnxt_re_db_fifo_check(struct work_struct *work)
505 {
506 	struct bnxt_re_dev *rdev = container_of(work, struct bnxt_re_dev,
507 			dbq_fifo_check_work);
508 	struct bnxt_qplib_db_pacing_data *pacing_data;
509 	u32 pacing_save;
510 
511 	if (!mutex_trylock(&rdev->pacing.dbq_lock))
512 		return;
513 	pacing_data = rdev->qplib_res.pacing_data;
514 	pacing_save = rdev->pacing.do_pacing_save;
515 	__wait_for_fifo_occupancy_below_th(rdev);
516 	cancel_delayed_work_sync(&rdev->dbq_pacing_work);
517 	if (pacing_save > rdev->pacing.dbr_def_do_pacing) {
518 		/* Double the do_pacing value during the congestion */
519 		pacing_save = pacing_save << 1;
520 	} else {
521 		/*
522 		 * when a new congestion is detected increase the do_pacing
523 		 * by 8 times. And also increase the pacing_th by 4 times. The
524 		 * reason to increase pacing_th is to give more space for the
525 		 * queue to oscillate down without getting empty, but also more
526 		 * room for the queue to increase without causing another alarm.
527 		 */
528 		pacing_save = pacing_save << 3;
529 		pacing_data->pacing_th = rdev->pacing.pacing_algo_th * 4;
530 	}
531 
532 	if (pacing_save > BNXT_RE_MAX_DBR_DO_PACING)
533 		pacing_save = BNXT_RE_MAX_DBR_DO_PACING;
534 
535 	pacing_data->do_pacing = pacing_save;
536 	rdev->pacing.do_pacing_save = pacing_data->do_pacing;
537 	pacing_data->alarm_th =
538 		pacing_data->pacing_th * BNXT_RE_PACING_ALARM_TH_MULTIPLE;
539 	schedule_delayed_work(&rdev->dbq_pacing_work,
540 			      msecs_to_jiffies(rdev->pacing.dbq_pacing_time));
541 	rdev->stats.pacing.alerts++;
542 	mutex_unlock(&rdev->pacing.dbq_lock);
543 }
544 
545 static void bnxt_re_pacing_timer_exp(struct work_struct *work)
546 {
547 	struct bnxt_re_dev *rdev = container_of(work, struct bnxt_re_dev,
548 			dbq_pacing_work.work);
549 	struct bnxt_qplib_db_pacing_data *pacing_data;
550 	u32 read_val, fifo_occup;
551 
552 	if (!mutex_trylock(&rdev->pacing.dbq_lock))
553 		return;
554 
555 	pacing_data = rdev->qplib_res.pacing_data;
556 	read_val = readl(rdev->en_dev->bar0 + rdev->pacing.dbr_db_fifo_reg_off);
557 	fifo_occup = BNXT_RE_MAX_FIFO_DEPTH -
558 		((read_val & BNXT_RE_DB_FIFO_ROOM_MASK) >>
559 		 BNXT_RE_DB_FIFO_ROOM_SHIFT);
560 
561 	if (fifo_occup > pacing_data->pacing_th)
562 		goto restart_timer;
563 
564 	/*
565 	 * Instead of immediately going back to the default do_pacing
566 	 * reduce it by 1/8 times and restart the timer.
567 	 */
568 	pacing_data->do_pacing = pacing_data->do_pacing - (pacing_data->do_pacing >> 3);
569 	pacing_data->do_pacing = max_t(u32, rdev->pacing.dbr_def_do_pacing, pacing_data->do_pacing);
570 	if (pacing_data->do_pacing <= rdev->pacing.dbr_def_do_pacing) {
571 		bnxt_re_set_default_pacing_data(rdev);
572 		rdev->stats.pacing.complete++;
573 		goto dbq_unlock;
574 	}
575 
576 restart_timer:
577 	schedule_delayed_work(&rdev->dbq_pacing_work,
578 			      msecs_to_jiffies(rdev->pacing.dbq_pacing_time));
579 	rdev->stats.pacing.resched++;
580 dbq_unlock:
581 	rdev->pacing.do_pacing_save = pacing_data->do_pacing;
582 	mutex_unlock(&rdev->pacing.dbq_lock);
583 }
584 
585 void bnxt_re_pacing_alert(struct bnxt_re_dev *rdev)
586 {
587 	struct bnxt_qplib_db_pacing_data *pacing_data;
588 
589 	if (!rdev->pacing.dbr_pacing)
590 		return;
591 	mutex_lock(&rdev->pacing.dbq_lock);
592 	pacing_data = rdev->qplib_res.pacing_data;
593 
594 	/*
595 	 * Increase the alarm_th to max so that other user lib instances do not
596 	 * keep alerting the driver.
597 	 */
598 	pacing_data->alarm_th = BNXT_RE_MAX_FIFO_DEPTH;
599 	pacing_data->do_pacing = BNXT_RE_MAX_DBR_DO_PACING;
600 	cancel_work_sync(&rdev->dbq_fifo_check_work);
601 	schedule_work(&rdev->dbq_fifo_check_work);
602 	mutex_unlock(&rdev->pacing.dbq_lock);
603 }
604 
605 static int bnxt_re_initialize_dbr_pacing(struct bnxt_re_dev *rdev)
606 {
607 	if (bnxt_re_hwrm_dbr_pacing_qcfg(rdev))
608 		return -EIO;
609 
610 	/* Allocate a page for app use */
611 	rdev->pacing.dbr_page = (void *)__get_free_page(GFP_KERNEL);
612 	if (!rdev->pacing.dbr_page)
613 		return -ENOMEM;
614 
615 	memset((u8 *)rdev->pacing.dbr_page, 0, PAGE_SIZE);
616 	rdev->qplib_res.pacing_data = (struct bnxt_qplib_db_pacing_data *)rdev->pacing.dbr_page;
617 
618 	/* MAP HW window 2 for reading db fifo depth */
619 	writel(rdev->chip_ctx->dbr_stat_db_fifo & BNXT_GRC_BASE_MASK,
620 	       rdev->en_dev->bar0 + BNXT_GRCPF_REG_WINDOW_BASE_OUT + 4);
621 	rdev->pacing.dbr_db_fifo_reg_off =
622 		(rdev->chip_ctx->dbr_stat_db_fifo & BNXT_GRC_OFFSET_MASK) +
623 		 BNXT_RE_GRC_FIFO_REG_BASE;
624 	rdev->pacing.dbr_bar_addr =
625 		pci_resource_start(rdev->qplib_res.pdev, 0) + rdev->pacing.dbr_db_fifo_reg_off;
626 
627 	rdev->pacing.pacing_algo_th = BNXT_RE_PACING_ALGO_THRESHOLD;
628 	rdev->pacing.dbq_pacing_time = BNXT_RE_DBR_PACING_TIME;
629 	rdev->pacing.dbr_def_do_pacing = BNXT_RE_DBR_DO_PACING_NO_CONGESTION;
630 	rdev->pacing.do_pacing_save = rdev->pacing.dbr_def_do_pacing;
631 	rdev->qplib_res.pacing_data->fifo_max_depth = BNXT_RE_MAX_FIFO_DEPTH;
632 	rdev->qplib_res.pacing_data->fifo_room_mask = BNXT_RE_DB_FIFO_ROOM_MASK;
633 	rdev->qplib_res.pacing_data->fifo_room_shift = BNXT_RE_DB_FIFO_ROOM_SHIFT;
634 	rdev->qplib_res.pacing_data->grc_reg_offset = rdev->pacing.dbr_db_fifo_reg_off;
635 	bnxt_re_set_default_pacing_data(rdev);
636 	/* Initialize worker for DBR Pacing */
637 	INIT_WORK(&rdev->dbq_fifo_check_work, bnxt_re_db_fifo_check);
638 	INIT_DELAYED_WORK(&rdev->dbq_pacing_work, bnxt_re_pacing_timer_exp);
639 	return 0;
640 }
641 
642 static void bnxt_re_deinitialize_dbr_pacing(struct bnxt_re_dev *rdev)
643 {
644 	cancel_work_sync(&rdev->dbq_fifo_check_work);
645 	cancel_delayed_work_sync(&rdev->dbq_pacing_work);
646 	if (rdev->pacing.dbr_page)
647 		free_page((u64)rdev->pacing.dbr_page);
648 
649 	rdev->pacing.dbr_page = NULL;
650 	rdev->pacing.dbr_pacing = false;
651 }
652 
653 static int bnxt_re_net_ring_free(struct bnxt_re_dev *rdev,
654 				 u16 fw_ring_id, int type)
655 {
656 	struct bnxt_en_dev *en_dev;
657 	struct hwrm_ring_free_input req = {};
658 	struct hwrm_ring_free_output resp;
659 	struct bnxt_fw_msg fw_msg = {};
660 	int rc = -EINVAL;
661 
662 	if (!rdev)
663 		return rc;
664 
665 	en_dev = rdev->en_dev;
666 
667 	if (!en_dev)
668 		return rc;
669 
670 	if (test_bit(BNXT_RE_FLAG_ERR_DEVICE_DETACHED, &rdev->flags))
671 		return 0;
672 
673 	bnxt_re_init_hwrm_hdr((void *)&req, HWRM_RING_FREE);
674 	req.ring_type = type;
675 	req.ring_id = cpu_to_le16(fw_ring_id);
676 	bnxt_re_fill_fw_msg(&fw_msg, (void *)&req, sizeof(req), (void *)&resp,
677 			    sizeof(resp), DFLT_HWRM_CMD_TIMEOUT);
678 	rc = bnxt_send_msg(en_dev, &fw_msg);
679 	if (rc)
680 		ibdev_err(&rdev->ibdev, "Failed to free HW ring:%d :%#x",
681 			  req.ring_id, rc);
682 	return rc;
683 }
684 
685 static int bnxt_re_net_ring_alloc(struct bnxt_re_dev *rdev,
686 				  struct bnxt_re_ring_attr *ring_attr,
687 				  u16 *fw_ring_id)
688 {
689 	struct bnxt_en_dev *en_dev = rdev->en_dev;
690 	struct hwrm_ring_alloc_input req = {};
691 	struct hwrm_ring_alloc_output resp;
692 	struct bnxt_fw_msg fw_msg = {};
693 	int rc = -EINVAL;
694 
695 	if (!en_dev)
696 		return rc;
697 
698 	bnxt_re_init_hwrm_hdr((void *)&req, HWRM_RING_ALLOC);
699 	req.enables = 0;
700 	req.page_tbl_addr =  cpu_to_le64(ring_attr->dma_arr[0]);
701 	if (ring_attr->pages > 1) {
702 		/* Page size is in log2 units */
703 		req.page_size = BNXT_PAGE_SHIFT;
704 		req.page_tbl_depth = 1;
705 	}
706 	req.fbo = 0;
707 	/* Association of ring index with doorbell index and MSIX number */
708 	req.logical_id = cpu_to_le16(ring_attr->lrid);
709 	req.length = cpu_to_le32(ring_attr->depth + 1);
710 	req.ring_type = ring_attr->type;
711 	req.int_mode = ring_attr->mode;
712 	bnxt_re_fill_fw_msg(&fw_msg, (void *)&req, sizeof(req), (void *)&resp,
713 			    sizeof(resp), DFLT_HWRM_CMD_TIMEOUT);
714 	rc = bnxt_send_msg(en_dev, &fw_msg);
715 	if (!rc)
716 		*fw_ring_id = le16_to_cpu(resp.ring_id);
717 
718 	return rc;
719 }
720 
721 static int bnxt_re_net_stats_ctx_free(struct bnxt_re_dev *rdev,
722 				      u32 fw_stats_ctx_id)
723 {
724 	struct bnxt_en_dev *en_dev = rdev->en_dev;
725 	struct hwrm_stat_ctx_free_input req = {};
726 	struct hwrm_stat_ctx_free_output resp = {};
727 	struct bnxt_fw_msg fw_msg = {};
728 	int rc = -EINVAL;
729 
730 	if (!en_dev)
731 		return rc;
732 
733 	if (test_bit(BNXT_RE_FLAG_ERR_DEVICE_DETACHED, &rdev->flags))
734 		return 0;
735 
736 	bnxt_re_init_hwrm_hdr((void *)&req, HWRM_STAT_CTX_FREE);
737 	req.stat_ctx_id = cpu_to_le32(fw_stats_ctx_id);
738 	bnxt_re_fill_fw_msg(&fw_msg, (void *)&req, sizeof(req), (void *)&resp,
739 			    sizeof(resp), DFLT_HWRM_CMD_TIMEOUT);
740 	rc = bnxt_send_msg(en_dev, &fw_msg);
741 	if (rc)
742 		ibdev_err(&rdev->ibdev, "Failed to free HW stats context %#x",
743 			  rc);
744 
745 	return rc;
746 }
747 
748 static int bnxt_re_net_stats_ctx_alloc(struct bnxt_re_dev *rdev,
749 				       dma_addr_t dma_map,
750 				       u32 *fw_stats_ctx_id)
751 {
752 	struct bnxt_qplib_chip_ctx *chip_ctx = rdev->chip_ctx;
753 	struct hwrm_stat_ctx_alloc_output resp = {};
754 	struct hwrm_stat_ctx_alloc_input req = {};
755 	struct bnxt_en_dev *en_dev = rdev->en_dev;
756 	struct bnxt_fw_msg fw_msg = {};
757 	int rc = -EINVAL;
758 
759 	*fw_stats_ctx_id = INVALID_STATS_CTX_ID;
760 
761 	if (!en_dev)
762 		return rc;
763 
764 	bnxt_re_init_hwrm_hdr((void *)&req, HWRM_STAT_CTX_ALLOC);
765 	req.update_period_ms = cpu_to_le32(1000);
766 	req.stats_dma_addr = cpu_to_le64(dma_map);
767 	req.stats_dma_length = cpu_to_le16(chip_ctx->hw_stats_size);
768 	req.stat_ctx_flags = STAT_CTX_ALLOC_REQ_STAT_CTX_FLAGS_ROCE;
769 	bnxt_re_fill_fw_msg(&fw_msg, (void *)&req, sizeof(req), (void *)&resp,
770 			    sizeof(resp), DFLT_HWRM_CMD_TIMEOUT);
771 	rc = bnxt_send_msg(en_dev, &fw_msg);
772 	if (!rc)
773 		*fw_stats_ctx_id = le32_to_cpu(resp.stat_ctx_id);
774 
775 	return rc;
776 }
777 
778 static void bnxt_re_disassociate_ucontext(struct ib_ucontext *ibcontext)
779 {
780 }
781 
782 /* Device */
783 
784 static struct bnxt_re_dev *bnxt_re_from_netdev(struct net_device *netdev)
785 {
786 	struct ib_device *ibdev =
787 		ib_device_get_by_netdev(netdev, RDMA_DRIVER_BNXT_RE);
788 	if (!ibdev)
789 		return NULL;
790 
791 	return container_of(ibdev, struct bnxt_re_dev, ibdev);
792 }
793 
794 static ssize_t hw_rev_show(struct device *device, struct device_attribute *attr,
795 			   char *buf)
796 {
797 	struct bnxt_re_dev *rdev =
798 		rdma_device_to_drv_device(device, struct bnxt_re_dev, ibdev);
799 
800 	return sysfs_emit(buf, "0x%x\n", rdev->en_dev->pdev->vendor);
801 }
802 static DEVICE_ATTR_RO(hw_rev);
803 
804 static ssize_t hca_type_show(struct device *device,
805 			     struct device_attribute *attr, char *buf)
806 {
807 	struct bnxt_re_dev *rdev =
808 		rdma_device_to_drv_device(device, struct bnxt_re_dev, ibdev);
809 
810 	return sysfs_emit(buf, "%s\n", rdev->ibdev.node_desc);
811 }
812 static DEVICE_ATTR_RO(hca_type);
813 
814 static struct attribute *bnxt_re_attributes[] = {
815 	&dev_attr_hw_rev.attr,
816 	&dev_attr_hca_type.attr,
817 	NULL
818 };
819 
820 static const struct attribute_group bnxt_re_dev_attr_group = {
821 	.attrs = bnxt_re_attributes,
822 };
823 
824 static const struct ib_device_ops bnxt_re_dev_ops = {
825 	.owner = THIS_MODULE,
826 	.driver_id = RDMA_DRIVER_BNXT_RE,
827 	.uverbs_abi_ver = BNXT_RE_ABI_VERSION,
828 
829 	.add_gid = bnxt_re_add_gid,
830 	.alloc_hw_port_stats = bnxt_re_ib_alloc_hw_port_stats,
831 	.alloc_mr = bnxt_re_alloc_mr,
832 	.alloc_pd = bnxt_re_alloc_pd,
833 	.alloc_ucontext = bnxt_re_alloc_ucontext,
834 	.create_ah = bnxt_re_create_ah,
835 	.create_cq = bnxt_re_create_cq,
836 	.create_qp = bnxt_re_create_qp,
837 	.create_srq = bnxt_re_create_srq,
838 	.create_user_ah = bnxt_re_create_ah,
839 	.dealloc_pd = bnxt_re_dealloc_pd,
840 	.dealloc_ucontext = bnxt_re_dealloc_ucontext,
841 	.del_gid = bnxt_re_del_gid,
842 	.dereg_mr = bnxt_re_dereg_mr,
843 	.destroy_ah = bnxt_re_destroy_ah,
844 	.destroy_cq = bnxt_re_destroy_cq,
845 	.destroy_qp = bnxt_re_destroy_qp,
846 	.destroy_srq = bnxt_re_destroy_srq,
847 	.device_group = &bnxt_re_dev_attr_group,
848 	.disassociate_ucontext = bnxt_re_disassociate_ucontext,
849 	.get_dev_fw_str = bnxt_re_query_fw_str,
850 	.get_dma_mr = bnxt_re_get_dma_mr,
851 	.get_hw_stats = bnxt_re_ib_get_hw_stats,
852 	.get_link_layer = bnxt_re_get_link_layer,
853 	.get_port_immutable = bnxt_re_get_port_immutable,
854 	.map_mr_sg = bnxt_re_map_mr_sg,
855 	.mmap = bnxt_re_mmap,
856 	.mmap_free = bnxt_re_mmap_free,
857 	.modify_qp = bnxt_re_modify_qp,
858 	.modify_srq = bnxt_re_modify_srq,
859 	.poll_cq = bnxt_re_poll_cq,
860 	.post_recv = bnxt_re_post_recv,
861 	.post_send = bnxt_re_post_send,
862 	.post_srq_recv = bnxt_re_post_srq_recv,
863 	.query_ah = bnxt_re_query_ah,
864 	.query_device = bnxt_re_query_device,
865 	.query_pkey = bnxt_re_query_pkey,
866 	.query_port = bnxt_re_query_port,
867 	.query_qp = bnxt_re_query_qp,
868 	.query_srq = bnxt_re_query_srq,
869 	.reg_user_mr = bnxt_re_reg_user_mr,
870 	.reg_user_mr_dmabuf = bnxt_re_reg_user_mr_dmabuf,
871 	.req_notify_cq = bnxt_re_req_notify_cq,
872 	.resize_cq = bnxt_re_resize_cq,
873 	INIT_RDMA_OBJ_SIZE(ib_ah, bnxt_re_ah, ib_ah),
874 	INIT_RDMA_OBJ_SIZE(ib_cq, bnxt_re_cq, ib_cq),
875 	INIT_RDMA_OBJ_SIZE(ib_pd, bnxt_re_pd, ib_pd),
876 	INIT_RDMA_OBJ_SIZE(ib_qp, bnxt_re_qp, ib_qp),
877 	INIT_RDMA_OBJ_SIZE(ib_srq, bnxt_re_srq, ib_srq),
878 	INIT_RDMA_OBJ_SIZE(ib_ucontext, bnxt_re_ucontext, ib_uctx),
879 };
880 
881 static int bnxt_re_register_ib(struct bnxt_re_dev *rdev)
882 {
883 	struct ib_device *ibdev = &rdev->ibdev;
884 	int ret;
885 
886 	/* ib device init */
887 	ibdev->node_type = RDMA_NODE_IB_CA;
888 	strscpy(ibdev->node_desc, BNXT_RE_DESC " HCA",
889 		strlen(BNXT_RE_DESC) + 5);
890 	ibdev->phys_port_cnt = 1;
891 
892 	addrconf_addr_eui48((u8 *)&ibdev->node_guid, rdev->netdev->dev_addr);
893 
894 	ibdev->num_comp_vectors	= rdev->num_msix - 1;
895 	ibdev->dev.parent = &rdev->en_dev->pdev->dev;
896 	ibdev->local_dma_lkey = BNXT_QPLIB_RSVD_LKEY;
897 
898 	if (IS_ENABLED(CONFIG_INFINIBAND_USER_ACCESS))
899 		ibdev->driver_def = bnxt_re_uapi_defs;
900 
901 	ib_set_device_ops(ibdev, &bnxt_re_dev_ops);
902 	ret = ib_device_set_netdev(&rdev->ibdev, rdev->netdev, 1);
903 	if (ret)
904 		return ret;
905 
906 	dma_set_max_seg_size(&rdev->en_dev->pdev->dev, UINT_MAX);
907 	ibdev->uverbs_cmd_mask |= BIT_ULL(IB_USER_VERBS_CMD_POLL_CQ);
908 	return ib_register_device(ibdev, "bnxt_re%d", &rdev->en_dev->pdev->dev);
909 }
910 
911 static struct bnxt_re_dev *bnxt_re_dev_add(struct bnxt_aux_priv *aux_priv,
912 					   struct bnxt_en_dev *en_dev)
913 {
914 	struct bnxt_re_dev *rdev;
915 
916 	/* Allocate bnxt_re_dev instance here */
917 	rdev = ib_alloc_device(bnxt_re_dev, ibdev);
918 	if (!rdev) {
919 		ibdev_err(NULL, "%s: bnxt_re_dev allocation failure!",
920 			  ROCE_DRV_MODULE_NAME);
921 		return NULL;
922 	}
923 	/* Default values */
924 	rdev->nb.notifier_call = NULL;
925 	rdev->netdev = en_dev->net;
926 	rdev->en_dev = en_dev;
927 	rdev->id = rdev->en_dev->pdev->devfn;
928 	INIT_LIST_HEAD(&rdev->qp_list);
929 	mutex_init(&rdev->qp_lock);
930 	mutex_init(&rdev->pacing.dbq_lock);
931 	atomic_set(&rdev->stats.res.qp_count, 0);
932 	atomic_set(&rdev->stats.res.cq_count, 0);
933 	atomic_set(&rdev->stats.res.srq_count, 0);
934 	atomic_set(&rdev->stats.res.mr_count, 0);
935 	atomic_set(&rdev->stats.res.mw_count, 0);
936 	atomic_set(&rdev->stats.res.ah_count, 0);
937 	atomic_set(&rdev->stats.res.pd_count, 0);
938 	rdev->cosq[0] = 0xFFFF;
939 	rdev->cosq[1] = 0xFFFF;
940 
941 	return rdev;
942 }
943 
944 static int bnxt_re_handle_unaffi_async_event(struct creq_func_event
945 					     *unaffi_async)
946 {
947 	switch (unaffi_async->event) {
948 	case CREQ_FUNC_EVENT_EVENT_TX_WQE_ERROR:
949 		break;
950 	case CREQ_FUNC_EVENT_EVENT_TX_DATA_ERROR:
951 		break;
952 	case CREQ_FUNC_EVENT_EVENT_RX_WQE_ERROR:
953 		break;
954 	case CREQ_FUNC_EVENT_EVENT_RX_DATA_ERROR:
955 		break;
956 	case CREQ_FUNC_EVENT_EVENT_CQ_ERROR:
957 		break;
958 	case CREQ_FUNC_EVENT_EVENT_TQM_ERROR:
959 		break;
960 	case CREQ_FUNC_EVENT_EVENT_CFCQ_ERROR:
961 		break;
962 	case CREQ_FUNC_EVENT_EVENT_CFCS_ERROR:
963 		break;
964 	case CREQ_FUNC_EVENT_EVENT_CFCC_ERROR:
965 		break;
966 	case CREQ_FUNC_EVENT_EVENT_CFCM_ERROR:
967 		break;
968 	case CREQ_FUNC_EVENT_EVENT_TIM_ERROR:
969 		break;
970 	default:
971 		return -EINVAL;
972 	}
973 	return 0;
974 }
975 
976 static int bnxt_re_handle_qp_async_event(struct creq_qp_event *qp_event,
977 					 struct bnxt_re_qp *qp)
978 {
979 	struct ib_event event = {};
980 	unsigned int flags;
981 
982 	if (qp->qplib_qp.state == CMDQ_MODIFY_QP_NEW_STATE_ERR &&
983 	    rdma_is_kernel_res(&qp->ib_qp.res)) {
984 		flags = bnxt_re_lock_cqs(qp);
985 		bnxt_qplib_add_flush_qp(&qp->qplib_qp);
986 		bnxt_re_unlock_cqs(qp, flags);
987 	}
988 
989 	if (qp->qplib_qp.srq) {
990 		event.device = &qp->rdev->ibdev;
991 		event.element.qp = &qp->ib_qp;
992 		event.event = IB_EVENT_QP_LAST_WQE_REACHED;
993 	}
994 
995 	if (event.device && qp->ib_qp.event_handler)
996 		qp->ib_qp.event_handler(&event, qp->ib_qp.qp_context);
997 
998 	return 0;
999 }
1000 
1001 static int bnxt_re_handle_affi_async_event(struct creq_qp_event *affi_async,
1002 					   void *obj)
1003 {
1004 	int rc = 0;
1005 	u8 event;
1006 
1007 	if (!obj)
1008 		return rc; /* QP was already dead, still return success */
1009 
1010 	event = affi_async->event;
1011 	if (event == CREQ_QP_EVENT_EVENT_QP_ERROR_NOTIFICATION) {
1012 		struct bnxt_qplib_qp *lib_qp = obj;
1013 		struct bnxt_re_qp *qp = container_of(lib_qp, struct bnxt_re_qp,
1014 						     qplib_qp);
1015 		rc = bnxt_re_handle_qp_async_event(affi_async, qp);
1016 	}
1017 	return rc;
1018 }
1019 
1020 static int bnxt_re_aeq_handler(struct bnxt_qplib_rcfw *rcfw,
1021 			       void *aeqe, void *obj)
1022 {
1023 	struct creq_qp_event *affi_async;
1024 	struct creq_func_event *unaffi_async;
1025 	u8 type;
1026 	int rc;
1027 
1028 	type = ((struct creq_base *)aeqe)->type;
1029 	if (type == CREQ_BASE_TYPE_FUNC_EVENT) {
1030 		unaffi_async = aeqe;
1031 		rc = bnxt_re_handle_unaffi_async_event(unaffi_async);
1032 	} else {
1033 		affi_async = aeqe;
1034 		rc = bnxt_re_handle_affi_async_event(affi_async, obj);
1035 	}
1036 
1037 	return rc;
1038 }
1039 
1040 static int bnxt_re_srqn_handler(struct bnxt_qplib_nq *nq,
1041 				struct bnxt_qplib_srq *handle, u8 event)
1042 {
1043 	struct bnxt_re_srq *srq = container_of(handle, struct bnxt_re_srq,
1044 					       qplib_srq);
1045 	struct ib_event ib_event;
1046 
1047 	ib_event.device = &srq->rdev->ibdev;
1048 	ib_event.element.srq = &srq->ib_srq;
1049 	if (event == NQ_SRQ_EVENT_EVENT_SRQ_THRESHOLD_EVENT)
1050 		ib_event.event = IB_EVENT_SRQ_LIMIT_REACHED;
1051 	else
1052 		ib_event.event = IB_EVENT_SRQ_ERR;
1053 
1054 	if (srq->ib_srq.event_handler) {
1055 		/* Lock event_handler? */
1056 		(*srq->ib_srq.event_handler)(&ib_event,
1057 					     srq->ib_srq.srq_context);
1058 	}
1059 	return 0;
1060 }
1061 
1062 static int bnxt_re_cqn_handler(struct bnxt_qplib_nq *nq,
1063 			       struct bnxt_qplib_cq *handle)
1064 {
1065 	struct bnxt_re_cq *cq = container_of(handle, struct bnxt_re_cq,
1066 					     qplib_cq);
1067 
1068 	if (cq->ib_cq.comp_handler) {
1069 		/* Lock comp_handler? */
1070 		(*cq->ib_cq.comp_handler)(&cq->ib_cq, cq->ib_cq.cq_context);
1071 	}
1072 
1073 	return 0;
1074 }
1075 
1076 static void bnxt_re_cleanup_res(struct bnxt_re_dev *rdev)
1077 {
1078 	int i;
1079 
1080 	for (i = 1; i < rdev->num_msix; i++)
1081 		bnxt_qplib_disable_nq(&rdev->nq[i - 1]);
1082 
1083 	if (rdev->qplib_res.rcfw)
1084 		bnxt_qplib_cleanup_res(&rdev->qplib_res);
1085 }
1086 
1087 static int bnxt_re_init_res(struct bnxt_re_dev *rdev)
1088 {
1089 	int num_vec_enabled = 0;
1090 	int rc = 0, i;
1091 	u32 db_offt;
1092 
1093 	bnxt_qplib_init_res(&rdev->qplib_res);
1094 
1095 	for (i = 1; i < rdev->num_msix ; i++) {
1096 		db_offt = rdev->en_dev->msix_entries[i].db_offset;
1097 		rc = bnxt_qplib_enable_nq(rdev->en_dev->pdev, &rdev->nq[i - 1],
1098 					  i - 1, rdev->en_dev->msix_entries[i].vector,
1099 					  db_offt, &bnxt_re_cqn_handler,
1100 					  &bnxt_re_srqn_handler);
1101 		if (rc) {
1102 			ibdev_err(&rdev->ibdev,
1103 				  "Failed to enable NQ with rc = 0x%x", rc);
1104 			goto fail;
1105 		}
1106 		num_vec_enabled++;
1107 	}
1108 	return 0;
1109 fail:
1110 	for (i = num_vec_enabled; i >= 0; i--)
1111 		bnxt_qplib_disable_nq(&rdev->nq[i]);
1112 	return rc;
1113 }
1114 
1115 static void bnxt_re_free_nq_res(struct bnxt_re_dev *rdev)
1116 {
1117 	u8 type;
1118 	int i;
1119 
1120 	for (i = 0; i < rdev->num_msix - 1; i++) {
1121 		type = bnxt_qplib_get_ring_type(rdev->chip_ctx);
1122 		bnxt_re_net_ring_free(rdev, rdev->nq[i].ring_id, type);
1123 		bnxt_qplib_free_nq(&rdev->nq[i]);
1124 		rdev->nq[i].res = NULL;
1125 	}
1126 }
1127 
1128 static void bnxt_re_free_res(struct bnxt_re_dev *rdev)
1129 {
1130 	bnxt_re_free_nq_res(rdev);
1131 
1132 	if (rdev->qplib_res.dpi_tbl.max) {
1133 		bnxt_qplib_dealloc_dpi(&rdev->qplib_res,
1134 				       &rdev->dpi_privileged);
1135 	}
1136 	if (rdev->qplib_res.rcfw) {
1137 		bnxt_qplib_free_res(&rdev->qplib_res);
1138 		rdev->qplib_res.rcfw = NULL;
1139 	}
1140 }
1141 
1142 static int bnxt_re_alloc_res(struct bnxt_re_dev *rdev)
1143 {
1144 	struct bnxt_re_ring_attr rattr = {};
1145 	int num_vec_created = 0;
1146 	int rc, i;
1147 	u8 type;
1148 
1149 	/* Configure and allocate resources for qplib */
1150 	rdev->qplib_res.rcfw = &rdev->rcfw;
1151 	rc = bnxt_qplib_get_dev_attr(&rdev->rcfw, &rdev->dev_attr);
1152 	if (rc)
1153 		goto fail;
1154 
1155 	rc = bnxt_qplib_alloc_res(&rdev->qplib_res, rdev->en_dev->pdev,
1156 				  rdev->netdev, &rdev->dev_attr);
1157 	if (rc)
1158 		goto fail;
1159 
1160 	rc = bnxt_qplib_alloc_dpi(&rdev->qplib_res,
1161 				  &rdev->dpi_privileged,
1162 				  rdev, BNXT_QPLIB_DPI_TYPE_KERNEL);
1163 	if (rc)
1164 		goto dealloc_res;
1165 
1166 	for (i = 0; i < rdev->num_msix - 1; i++) {
1167 		struct bnxt_qplib_nq *nq;
1168 
1169 		nq = &rdev->nq[i];
1170 		nq->hwq.max_elements = BNXT_QPLIB_NQE_MAX_CNT;
1171 		rc = bnxt_qplib_alloc_nq(&rdev->qplib_res, &rdev->nq[i]);
1172 		if (rc) {
1173 			ibdev_err(&rdev->ibdev, "Alloc Failed NQ%d rc:%#x",
1174 				  i, rc);
1175 			goto free_nq;
1176 		}
1177 		type = bnxt_qplib_get_ring_type(rdev->chip_ctx);
1178 		rattr.dma_arr = nq->hwq.pbl[PBL_LVL_0].pg_map_arr;
1179 		rattr.pages = nq->hwq.pbl[rdev->nq[i].hwq.level].pg_count;
1180 		rattr.type = type;
1181 		rattr.mode = RING_ALLOC_REQ_INT_MODE_MSIX;
1182 		rattr.depth = BNXT_QPLIB_NQE_MAX_CNT - 1;
1183 		rattr.lrid = rdev->en_dev->msix_entries[i + 1].ring_idx;
1184 		rc = bnxt_re_net_ring_alloc(rdev, &rattr, &nq->ring_id);
1185 		if (rc) {
1186 			ibdev_err(&rdev->ibdev,
1187 				  "Failed to allocate NQ fw id with rc = 0x%x",
1188 				  rc);
1189 			bnxt_qplib_free_nq(&rdev->nq[i]);
1190 			goto free_nq;
1191 		}
1192 		num_vec_created++;
1193 	}
1194 	return 0;
1195 free_nq:
1196 	for (i = num_vec_created - 1; i >= 0; i--) {
1197 		type = bnxt_qplib_get_ring_type(rdev->chip_ctx);
1198 		bnxt_re_net_ring_free(rdev, rdev->nq[i].ring_id, type);
1199 		bnxt_qplib_free_nq(&rdev->nq[i]);
1200 	}
1201 	bnxt_qplib_dealloc_dpi(&rdev->qplib_res,
1202 			       &rdev->dpi_privileged);
1203 dealloc_res:
1204 	bnxt_qplib_free_res(&rdev->qplib_res);
1205 
1206 fail:
1207 	rdev->qplib_res.rcfw = NULL;
1208 	return rc;
1209 }
1210 
1211 static void bnxt_re_dispatch_event(struct ib_device *ibdev, struct ib_qp *qp,
1212 				   u8 port_num, enum ib_event_type event)
1213 {
1214 	struct ib_event ib_event;
1215 
1216 	ib_event.device = ibdev;
1217 	if (qp) {
1218 		ib_event.element.qp = qp;
1219 		ib_event.event = event;
1220 		if (qp->event_handler)
1221 			qp->event_handler(&ib_event, qp->qp_context);
1222 
1223 	} else {
1224 		ib_event.element.port_num = port_num;
1225 		ib_event.event = event;
1226 		ib_dispatch_event(&ib_event);
1227 	}
1228 }
1229 
1230 static bool bnxt_re_is_qp1_or_shadow_qp(struct bnxt_re_dev *rdev,
1231 					struct bnxt_re_qp *qp)
1232 {
1233 	return (qp->ib_qp.qp_type == IB_QPT_GSI) ||
1234 	       (qp == rdev->gsi_ctx.gsi_sqp);
1235 }
1236 
1237 static void bnxt_re_dev_stop(struct bnxt_re_dev *rdev)
1238 {
1239 	int mask = IB_QP_STATE;
1240 	struct ib_qp_attr qp_attr;
1241 	struct bnxt_re_qp *qp;
1242 
1243 	qp_attr.qp_state = IB_QPS_ERR;
1244 	mutex_lock(&rdev->qp_lock);
1245 	list_for_each_entry(qp, &rdev->qp_list, list) {
1246 		/* Modify the state of all QPs except QP1/Shadow QP */
1247 		if (!bnxt_re_is_qp1_or_shadow_qp(rdev, qp)) {
1248 			if (qp->qplib_qp.state !=
1249 			    CMDQ_MODIFY_QP_NEW_STATE_RESET &&
1250 			    qp->qplib_qp.state !=
1251 			    CMDQ_MODIFY_QP_NEW_STATE_ERR) {
1252 				bnxt_re_dispatch_event(&rdev->ibdev, &qp->ib_qp,
1253 						       1, IB_EVENT_QP_FATAL);
1254 				bnxt_re_modify_qp(&qp->ib_qp, &qp_attr, mask,
1255 						  NULL);
1256 			}
1257 		}
1258 	}
1259 	mutex_unlock(&rdev->qp_lock);
1260 }
1261 
1262 static int bnxt_re_update_gid(struct bnxt_re_dev *rdev)
1263 {
1264 	struct bnxt_qplib_sgid_tbl *sgid_tbl = &rdev->qplib_res.sgid_tbl;
1265 	struct bnxt_qplib_gid gid;
1266 	u16 gid_idx, index;
1267 	int rc = 0;
1268 
1269 	if (!ib_device_try_get(&rdev->ibdev))
1270 		return 0;
1271 
1272 	for (index = 0; index < sgid_tbl->active; index++) {
1273 		gid_idx = sgid_tbl->hw_id[index];
1274 
1275 		if (!memcmp(&sgid_tbl->tbl[index], &bnxt_qplib_gid_zero,
1276 			    sizeof(bnxt_qplib_gid_zero)))
1277 			continue;
1278 		/* need to modify the VLAN enable setting of non VLAN GID only
1279 		 * as setting is done for VLAN GID while adding GID
1280 		 */
1281 		if (sgid_tbl->vlan[index])
1282 			continue;
1283 
1284 		memcpy(&gid, &sgid_tbl->tbl[index], sizeof(gid));
1285 
1286 		rc = bnxt_qplib_update_sgid(sgid_tbl, &gid, gid_idx,
1287 					    rdev->qplib_res.netdev->dev_addr);
1288 	}
1289 
1290 	ib_device_put(&rdev->ibdev);
1291 	return rc;
1292 }
1293 
1294 static u32 bnxt_re_get_priority_mask(struct bnxt_re_dev *rdev)
1295 {
1296 	u32 prio_map = 0, tmp_map = 0;
1297 	struct net_device *netdev;
1298 	struct dcb_app app = {};
1299 
1300 	netdev = rdev->netdev;
1301 
1302 	app.selector = IEEE_8021QAZ_APP_SEL_ETHERTYPE;
1303 	app.protocol = ETH_P_IBOE;
1304 	tmp_map = dcb_ieee_getapp_mask(netdev, &app);
1305 	prio_map = tmp_map;
1306 
1307 	app.selector = IEEE_8021QAZ_APP_SEL_DGRAM;
1308 	app.protocol = ROCE_V2_UDP_DPORT;
1309 	tmp_map = dcb_ieee_getapp_mask(netdev, &app);
1310 	prio_map |= tmp_map;
1311 
1312 	return prio_map;
1313 }
1314 
1315 static int bnxt_re_setup_qos(struct bnxt_re_dev *rdev)
1316 {
1317 	u8 prio_map = 0;
1318 
1319 	/* Get priority for roce */
1320 	prio_map = bnxt_re_get_priority_mask(rdev);
1321 
1322 	if (prio_map == rdev->cur_prio_map)
1323 		return 0;
1324 	rdev->cur_prio_map = prio_map;
1325 	/* Actual priorities are not programmed as they are already
1326 	 * done by L2 driver; just enable or disable priority vlan tagging
1327 	 */
1328 	if ((prio_map == 0 && rdev->qplib_res.prio) ||
1329 	    (prio_map != 0 && !rdev->qplib_res.prio)) {
1330 		rdev->qplib_res.prio = prio_map;
1331 		bnxt_re_update_gid(rdev);
1332 	}
1333 
1334 	return 0;
1335 }
1336 
1337 static void bnxt_re_query_hwrm_intf_version(struct bnxt_re_dev *rdev)
1338 {
1339 	struct bnxt_en_dev *en_dev = rdev->en_dev;
1340 	struct hwrm_ver_get_output resp = {};
1341 	struct hwrm_ver_get_input req = {};
1342 	struct bnxt_qplib_chip_ctx *cctx;
1343 	struct bnxt_fw_msg fw_msg = {};
1344 	int rc;
1345 
1346 	bnxt_re_init_hwrm_hdr((void *)&req, HWRM_VER_GET);
1347 	req.hwrm_intf_maj = HWRM_VERSION_MAJOR;
1348 	req.hwrm_intf_min = HWRM_VERSION_MINOR;
1349 	req.hwrm_intf_upd = HWRM_VERSION_UPDATE;
1350 	bnxt_re_fill_fw_msg(&fw_msg, (void *)&req, sizeof(req), (void *)&resp,
1351 			    sizeof(resp), DFLT_HWRM_CMD_TIMEOUT);
1352 	rc = bnxt_send_msg(en_dev, &fw_msg);
1353 	if (rc) {
1354 		ibdev_err(&rdev->ibdev, "Failed to query HW version, rc = 0x%x",
1355 			  rc);
1356 		return;
1357 	}
1358 
1359 	cctx = rdev->chip_ctx;
1360 	cctx->hwrm_intf_ver =
1361 		(u64)le16_to_cpu(resp.hwrm_intf_major) << 48 |
1362 		(u64)le16_to_cpu(resp.hwrm_intf_minor) << 32 |
1363 		(u64)le16_to_cpu(resp.hwrm_intf_build) << 16 |
1364 		le16_to_cpu(resp.hwrm_intf_patch);
1365 
1366 	cctx->hwrm_cmd_max_timeout = le16_to_cpu(resp.max_req_timeout);
1367 
1368 	if (!cctx->hwrm_cmd_max_timeout)
1369 		cctx->hwrm_cmd_max_timeout = RCFW_FW_STALL_MAX_TIMEOUT;
1370 }
1371 
1372 static int bnxt_re_ib_init(struct bnxt_re_dev *rdev)
1373 {
1374 	int rc;
1375 	u32 event;
1376 
1377 	/* Register ib dev */
1378 	rc = bnxt_re_register_ib(rdev);
1379 	if (rc) {
1380 		pr_err("Failed to register with IB: %#x\n", rc);
1381 		return rc;
1382 	}
1383 	dev_info(rdev_to_dev(rdev), "Device registered with IB successfully");
1384 	set_bit(BNXT_RE_FLAG_ISSUE_ROCE_STATS, &rdev->flags);
1385 
1386 	event = netif_running(rdev->netdev) && netif_carrier_ok(rdev->netdev) ?
1387 		IB_EVENT_PORT_ACTIVE : IB_EVENT_PORT_ERR;
1388 
1389 	bnxt_re_dispatch_event(&rdev->ibdev, NULL, 1, event);
1390 
1391 	return rc;
1392 }
1393 
1394 static void bnxt_re_dev_uninit(struct bnxt_re_dev *rdev)
1395 {
1396 	u8 type;
1397 	int rc;
1398 
1399 	if (test_and_clear_bit(BNXT_RE_FLAG_QOS_WORK_REG, &rdev->flags))
1400 		cancel_delayed_work_sync(&rdev->worker);
1401 
1402 	if (test_and_clear_bit(BNXT_RE_FLAG_RESOURCES_INITIALIZED,
1403 			       &rdev->flags))
1404 		bnxt_re_cleanup_res(rdev);
1405 	if (test_and_clear_bit(BNXT_RE_FLAG_RESOURCES_ALLOCATED, &rdev->flags))
1406 		bnxt_re_free_res(rdev);
1407 
1408 	if (test_and_clear_bit(BNXT_RE_FLAG_RCFW_CHANNEL_EN, &rdev->flags)) {
1409 		rc = bnxt_qplib_deinit_rcfw(&rdev->rcfw);
1410 		if (rc)
1411 			ibdev_warn(&rdev->ibdev,
1412 				   "Failed to deinitialize RCFW: %#x", rc);
1413 		bnxt_re_net_stats_ctx_free(rdev, rdev->qplib_ctx.stats.fw_id);
1414 		bnxt_qplib_free_ctx(&rdev->qplib_res, &rdev->qplib_ctx);
1415 		bnxt_qplib_disable_rcfw_channel(&rdev->rcfw);
1416 		type = bnxt_qplib_get_ring_type(rdev->chip_ctx);
1417 		bnxt_re_net_ring_free(rdev, rdev->rcfw.creq.ring_id, type);
1418 		bnxt_qplib_free_rcfw_channel(&rdev->rcfw);
1419 	}
1420 
1421 	rdev->num_msix = 0;
1422 
1423 	if (rdev->pacing.dbr_pacing)
1424 		bnxt_re_deinitialize_dbr_pacing(rdev);
1425 
1426 	bnxt_re_destroy_chip_ctx(rdev);
1427 	if (test_and_clear_bit(BNXT_RE_FLAG_NETDEV_REGISTERED, &rdev->flags))
1428 		bnxt_unregister_dev(rdev->en_dev);
1429 }
1430 
1431 /* worker thread for polling periodic events. Now used for QoS programming*/
1432 static void bnxt_re_worker(struct work_struct *work)
1433 {
1434 	struct bnxt_re_dev *rdev = container_of(work, struct bnxt_re_dev,
1435 						worker.work);
1436 
1437 	bnxt_re_setup_qos(rdev);
1438 	schedule_delayed_work(&rdev->worker, msecs_to_jiffies(30000));
1439 }
1440 
1441 static int bnxt_re_dev_init(struct bnxt_re_dev *rdev, u8 wqe_mode)
1442 {
1443 	struct bnxt_re_ring_attr rattr = {};
1444 	struct bnxt_qplib_creq_ctx *creq;
1445 	u32 db_offt;
1446 	int vid;
1447 	u8 type;
1448 	int rc;
1449 
1450 	/* Registered a new RoCE device instance to netdev */
1451 	rc = bnxt_re_register_netdev(rdev);
1452 	if (rc) {
1453 		ibdev_err(&rdev->ibdev,
1454 			  "Failed to register with netedev: %#x\n", rc);
1455 		return -EINVAL;
1456 	}
1457 	set_bit(BNXT_RE_FLAG_NETDEV_REGISTERED, &rdev->flags);
1458 
1459 	rc = bnxt_re_setup_chip_ctx(rdev, wqe_mode);
1460 	if (rc) {
1461 		bnxt_unregister_dev(rdev->en_dev);
1462 		clear_bit(BNXT_RE_FLAG_NETDEV_REGISTERED, &rdev->flags);
1463 		ibdev_err(&rdev->ibdev, "Failed to get chip context\n");
1464 		return -EINVAL;
1465 	}
1466 
1467 	/* Check whether VF or PF */
1468 	bnxt_re_get_sriov_func_type(rdev);
1469 
1470 	if (!rdev->en_dev->ulp_tbl->msix_requested) {
1471 		ibdev_err(&rdev->ibdev,
1472 			  "Failed to get MSI-X vectors: %#x\n", rc);
1473 		rc = -EINVAL;
1474 		goto fail;
1475 	}
1476 	ibdev_dbg(&rdev->ibdev, "Got %d MSI-X vectors\n",
1477 		  rdev->en_dev->ulp_tbl->msix_requested);
1478 	rdev->num_msix = rdev->en_dev->ulp_tbl->msix_requested;
1479 
1480 	bnxt_re_query_hwrm_intf_version(rdev);
1481 
1482 	/* Establish RCFW Communication Channel to initialize the context
1483 	 * memory for the function and all child VFs
1484 	 */
1485 	rc = bnxt_qplib_alloc_rcfw_channel(&rdev->qplib_res, &rdev->rcfw,
1486 					   &rdev->qplib_ctx,
1487 					   BNXT_RE_MAX_QPC_COUNT);
1488 	if (rc) {
1489 		ibdev_err(&rdev->ibdev,
1490 			  "Failed to allocate RCFW Channel: %#x\n", rc);
1491 		goto fail;
1492 	}
1493 
1494 	type = bnxt_qplib_get_ring_type(rdev->chip_ctx);
1495 	creq = &rdev->rcfw.creq;
1496 	rattr.dma_arr = creq->hwq.pbl[PBL_LVL_0].pg_map_arr;
1497 	rattr.pages = creq->hwq.pbl[creq->hwq.level].pg_count;
1498 	rattr.type = type;
1499 	rattr.mode = RING_ALLOC_REQ_INT_MODE_MSIX;
1500 	rattr.depth = BNXT_QPLIB_CREQE_MAX_CNT - 1;
1501 	rattr.lrid = rdev->en_dev->msix_entries[BNXT_RE_AEQ_IDX].ring_idx;
1502 	rc = bnxt_re_net_ring_alloc(rdev, &rattr, &creq->ring_id);
1503 	if (rc) {
1504 		ibdev_err(&rdev->ibdev, "Failed to allocate CREQ: %#x\n", rc);
1505 		goto free_rcfw;
1506 	}
1507 	db_offt = rdev->en_dev->msix_entries[BNXT_RE_AEQ_IDX].db_offset;
1508 	vid = rdev->en_dev->msix_entries[BNXT_RE_AEQ_IDX].vector;
1509 	rc = bnxt_qplib_enable_rcfw_channel(&rdev->rcfw,
1510 					    vid, db_offt,
1511 					    &bnxt_re_aeq_handler);
1512 	if (rc) {
1513 		ibdev_err(&rdev->ibdev, "Failed to enable RCFW channel: %#x\n",
1514 			  rc);
1515 		goto free_ring;
1516 	}
1517 
1518 	if (bnxt_qplib_dbr_pacing_en(rdev->chip_ctx)) {
1519 		rc = bnxt_re_initialize_dbr_pacing(rdev);
1520 		if (!rc) {
1521 			rdev->pacing.dbr_pacing = true;
1522 		} else {
1523 			ibdev_err(&rdev->ibdev,
1524 				  "DBR pacing disabled with error : %d\n", rc);
1525 			rdev->pacing.dbr_pacing = false;
1526 		}
1527 	}
1528 	rc = bnxt_qplib_get_dev_attr(&rdev->rcfw, &rdev->dev_attr);
1529 	if (rc)
1530 		goto disable_rcfw;
1531 
1532 	bnxt_re_set_resource_limits(rdev);
1533 
1534 	rc = bnxt_qplib_alloc_ctx(&rdev->qplib_res, &rdev->qplib_ctx, 0,
1535 				  bnxt_qplib_is_chip_gen_p5_p7(rdev->chip_ctx));
1536 	if (rc) {
1537 		ibdev_err(&rdev->ibdev,
1538 			  "Failed to allocate QPLIB context: %#x\n", rc);
1539 		goto disable_rcfw;
1540 	}
1541 	rc = bnxt_re_net_stats_ctx_alloc(rdev,
1542 					 rdev->qplib_ctx.stats.dma_map,
1543 					 &rdev->qplib_ctx.stats.fw_id);
1544 	if (rc) {
1545 		ibdev_err(&rdev->ibdev,
1546 			  "Failed to allocate stats context: %#x\n", rc);
1547 		goto free_ctx;
1548 	}
1549 
1550 	rc = bnxt_qplib_init_rcfw(&rdev->rcfw, &rdev->qplib_ctx,
1551 				  rdev->is_virtfn);
1552 	if (rc) {
1553 		ibdev_err(&rdev->ibdev,
1554 			  "Failed to initialize RCFW: %#x\n", rc);
1555 		goto free_sctx;
1556 	}
1557 	set_bit(BNXT_RE_FLAG_RCFW_CHANNEL_EN, &rdev->flags);
1558 
1559 	/* Resources based on the 'new' device caps */
1560 	rc = bnxt_re_alloc_res(rdev);
1561 	if (rc) {
1562 		ibdev_err(&rdev->ibdev,
1563 			  "Failed to allocate resources: %#x\n", rc);
1564 		goto fail;
1565 	}
1566 	set_bit(BNXT_RE_FLAG_RESOURCES_ALLOCATED, &rdev->flags);
1567 	rc = bnxt_re_init_res(rdev);
1568 	if (rc) {
1569 		ibdev_err(&rdev->ibdev,
1570 			  "Failed to initialize resources: %#x\n", rc);
1571 		goto fail;
1572 	}
1573 
1574 	set_bit(BNXT_RE_FLAG_RESOURCES_INITIALIZED, &rdev->flags);
1575 
1576 	if (!rdev->is_virtfn) {
1577 		rc = bnxt_re_setup_qos(rdev);
1578 		if (rc)
1579 			ibdev_info(&rdev->ibdev,
1580 				   "RoCE priority not yet configured\n");
1581 
1582 		INIT_DELAYED_WORK(&rdev->worker, bnxt_re_worker);
1583 		set_bit(BNXT_RE_FLAG_QOS_WORK_REG, &rdev->flags);
1584 		schedule_delayed_work(&rdev->worker, msecs_to_jiffies(30000));
1585 		/*
1586 		 * Use the total VF count since the actual VF count may not be
1587 		 * available at this point.
1588 		 */
1589 		bnxt_re_vf_res_config(rdev);
1590 	}
1591 
1592 	return 0;
1593 free_sctx:
1594 	bnxt_re_net_stats_ctx_free(rdev, rdev->qplib_ctx.stats.fw_id);
1595 free_ctx:
1596 	bnxt_qplib_free_ctx(&rdev->qplib_res, &rdev->qplib_ctx);
1597 disable_rcfw:
1598 	bnxt_qplib_disable_rcfw_channel(&rdev->rcfw);
1599 free_ring:
1600 	type = bnxt_qplib_get_ring_type(rdev->chip_ctx);
1601 	bnxt_re_net_ring_free(rdev, rdev->rcfw.creq.ring_id, type);
1602 free_rcfw:
1603 	bnxt_qplib_free_rcfw_channel(&rdev->rcfw);
1604 fail:
1605 	bnxt_re_dev_uninit(rdev);
1606 
1607 	return rc;
1608 }
1609 
1610 static int bnxt_re_add_device(struct auxiliary_device *adev, u8 wqe_mode)
1611 {
1612 	struct bnxt_aux_priv *aux_priv =
1613 		container_of(adev, struct bnxt_aux_priv, aux_dev);
1614 	struct bnxt_en_dev *en_dev;
1615 	struct bnxt_re_dev *rdev;
1616 	int rc;
1617 
1618 	/* en_dev should never be NULL as long as adev and aux_dev are valid. */
1619 	en_dev = aux_priv->edev;
1620 
1621 	rdev = bnxt_re_dev_add(aux_priv, en_dev);
1622 	if (!rdev || !rdev_to_dev(rdev)) {
1623 		rc = -ENOMEM;
1624 		goto exit;
1625 	}
1626 
1627 	rc = bnxt_re_dev_init(rdev, wqe_mode);
1628 	if (rc)
1629 		goto re_dev_dealloc;
1630 
1631 	rc = bnxt_re_ib_init(rdev);
1632 	if (rc) {
1633 		pr_err("Failed to register with IB: %s",
1634 			aux_priv->aux_dev.name);
1635 		goto re_dev_uninit;
1636 	}
1637 	auxiliary_set_drvdata(adev, rdev);
1638 
1639 	return 0;
1640 
1641 re_dev_uninit:
1642 	bnxt_re_dev_uninit(rdev);
1643 re_dev_dealloc:
1644 	ib_dealloc_device(&rdev->ibdev);
1645 exit:
1646 	return rc;
1647 }
1648 
1649 static void bnxt_re_setup_cc(struct bnxt_re_dev *rdev, bool enable)
1650 {
1651 	struct bnxt_qplib_cc_param cc_param = {};
1652 
1653 	/* Do not enable congestion control on VFs */
1654 	if (rdev->is_virtfn)
1655 		return;
1656 
1657 	/* Currently enabling only for GenP5 adapters */
1658 	if (!bnxt_qplib_is_chip_gen_p5_p7(rdev->chip_ctx))
1659 		return;
1660 
1661 	if (enable) {
1662 		cc_param.enable  = 1;
1663 		cc_param.cc_mode = CMDQ_MODIFY_ROCE_CC_CC_MODE_PROBABILISTIC_CC_MODE;
1664 	}
1665 
1666 	cc_param.mask = (CMDQ_MODIFY_ROCE_CC_MODIFY_MASK_CC_MODE |
1667 			 CMDQ_MODIFY_ROCE_CC_MODIFY_MASK_ENABLE_CC |
1668 			 CMDQ_MODIFY_ROCE_CC_MODIFY_MASK_TOS_ECN);
1669 
1670 	if (bnxt_qplib_modify_cc(&rdev->qplib_res, &cc_param))
1671 		ibdev_err(&rdev->ibdev, "Failed to setup CC enable = %d\n", enable);
1672 }
1673 
1674 /*
1675  * "Notifier chain callback can be invoked for the same chain from
1676  * different CPUs at the same time".
1677  *
1678  * For cases when the netdev is already present, our call to the
1679  * register_netdevice_notifier() will actually get the rtnl_lock()
1680  * before sending NETDEV_REGISTER and (if up) NETDEV_UP
1681  * events.
1682  *
1683  * But for cases when the netdev is not already present, the notifier
1684  * chain is subjected to be invoked from different CPUs simultaneously.
1685  *
1686  * This is protected by the netdev_mutex.
1687  */
1688 static int bnxt_re_netdev_event(struct notifier_block *notifier,
1689 				unsigned long event, void *ptr)
1690 {
1691 	struct net_device *real_dev, *netdev = netdev_notifier_info_to_dev(ptr);
1692 	struct bnxt_re_dev *rdev;
1693 
1694 	real_dev = rdma_vlan_dev_real_dev(netdev);
1695 	if (!real_dev)
1696 		real_dev = netdev;
1697 
1698 	if (real_dev != netdev)
1699 		goto exit;
1700 
1701 	rdev = bnxt_re_from_netdev(real_dev);
1702 	if (!rdev)
1703 		return NOTIFY_DONE;
1704 
1705 
1706 	switch (event) {
1707 	case NETDEV_UP:
1708 	case NETDEV_DOWN:
1709 	case NETDEV_CHANGE:
1710 		bnxt_re_dispatch_event(&rdev->ibdev, NULL, 1,
1711 					netif_carrier_ok(real_dev) ?
1712 					IB_EVENT_PORT_ACTIVE :
1713 					IB_EVENT_PORT_ERR);
1714 		break;
1715 	default:
1716 		break;
1717 	}
1718 	ib_device_put(&rdev->ibdev);
1719 exit:
1720 	return NOTIFY_DONE;
1721 }
1722 
1723 #define BNXT_ADEV_NAME "bnxt_en"
1724 
1725 static void bnxt_re_remove(struct auxiliary_device *adev)
1726 {
1727 	struct bnxt_re_dev *rdev = auxiliary_get_drvdata(adev);
1728 
1729 	if (!rdev)
1730 		return;
1731 
1732 	mutex_lock(&bnxt_re_mutex);
1733 	if (rdev->nb.notifier_call) {
1734 		unregister_netdevice_notifier(&rdev->nb);
1735 		rdev->nb.notifier_call = NULL;
1736 	} else {
1737 		/* If notifier is null, we should have already done a
1738 		 * clean up before coming here.
1739 		 */
1740 		goto skip_remove;
1741 	}
1742 	bnxt_re_setup_cc(rdev, false);
1743 	ib_unregister_device(&rdev->ibdev);
1744 	bnxt_re_dev_uninit(rdev);
1745 	ib_dealloc_device(&rdev->ibdev);
1746 skip_remove:
1747 	mutex_unlock(&bnxt_re_mutex);
1748 }
1749 
1750 static int bnxt_re_probe(struct auxiliary_device *adev,
1751 			 const struct auxiliary_device_id *id)
1752 {
1753 	struct bnxt_re_dev *rdev;
1754 	int rc;
1755 
1756 	mutex_lock(&bnxt_re_mutex);
1757 	rc = bnxt_re_add_device(adev, BNXT_QPLIB_WQE_MODE_STATIC);
1758 	if (rc) {
1759 		mutex_unlock(&bnxt_re_mutex);
1760 		return rc;
1761 	}
1762 
1763 	rdev = auxiliary_get_drvdata(adev);
1764 
1765 	rdev->nb.notifier_call = bnxt_re_netdev_event;
1766 	rc = register_netdevice_notifier(&rdev->nb);
1767 	if (rc) {
1768 		rdev->nb.notifier_call = NULL;
1769 		pr_err("%s: Cannot register to netdevice_notifier",
1770 		       ROCE_DRV_MODULE_NAME);
1771 		goto err;
1772 	}
1773 
1774 	bnxt_re_setup_cc(rdev, true);
1775 	mutex_unlock(&bnxt_re_mutex);
1776 	return 0;
1777 
1778 err:
1779 	mutex_unlock(&bnxt_re_mutex);
1780 	bnxt_re_remove(adev);
1781 
1782 	return rc;
1783 }
1784 
1785 static int bnxt_re_suspend(struct auxiliary_device *adev, pm_message_t state)
1786 {
1787 	struct bnxt_re_dev *rdev = auxiliary_get_drvdata(adev);
1788 
1789 	if (!rdev)
1790 		return 0;
1791 
1792 	mutex_lock(&bnxt_re_mutex);
1793 	/* L2 driver may invoke this callback during device error/crash or device
1794 	 * reset. Current RoCE driver doesn't recover the device in case of
1795 	 * error. Handle the error by dispatching fatal events to all qps
1796 	 * ie. by calling bnxt_re_dev_stop and release the MSIx vectors as
1797 	 * L2 driver want to modify the MSIx table.
1798 	 */
1799 
1800 	ibdev_info(&rdev->ibdev, "Handle device suspend call");
1801 	/* Check the current device state from bnxt_en_dev and move the
1802 	 * device to detached state if FW_FATAL_COND is set.
1803 	 * This prevents more commands to HW during clean-up,
1804 	 * in case the device is already in error.
1805 	 */
1806 	if (test_bit(BNXT_STATE_FW_FATAL_COND, &rdev->en_dev->en_state))
1807 		set_bit(ERR_DEVICE_DETACHED, &rdev->rcfw.cmdq.flags);
1808 
1809 	bnxt_re_dev_stop(rdev);
1810 	bnxt_re_stop_irq(rdev);
1811 	/* Move the device states to detached and  avoid sending any more
1812 	 * commands to HW
1813 	 */
1814 	set_bit(BNXT_RE_FLAG_ERR_DEVICE_DETACHED, &rdev->flags);
1815 	set_bit(ERR_DEVICE_DETACHED, &rdev->rcfw.cmdq.flags);
1816 	wake_up_all(&rdev->rcfw.cmdq.waitq);
1817 	mutex_unlock(&bnxt_re_mutex);
1818 
1819 	return 0;
1820 }
1821 
1822 static int bnxt_re_resume(struct auxiliary_device *adev)
1823 {
1824 	struct bnxt_re_dev *rdev = auxiliary_get_drvdata(adev);
1825 
1826 	if (!rdev)
1827 		return 0;
1828 
1829 	mutex_lock(&bnxt_re_mutex);
1830 	/* L2 driver may invoke this callback during device recovery, resume.
1831 	 * reset. Current RoCE driver doesn't recover the device in case of
1832 	 * error. Handle the error by dispatching fatal events to all qps
1833 	 * ie. by calling bnxt_re_dev_stop and release the MSIx vectors as
1834 	 * L2 driver want to modify the MSIx table.
1835 	 */
1836 
1837 	ibdev_info(&rdev->ibdev, "Handle device resume call");
1838 	mutex_unlock(&bnxt_re_mutex);
1839 
1840 	return 0;
1841 }
1842 
1843 static const struct auxiliary_device_id bnxt_re_id_table[] = {
1844 	{ .name = BNXT_ADEV_NAME ".rdma", },
1845 	{},
1846 };
1847 
1848 MODULE_DEVICE_TABLE(auxiliary, bnxt_re_id_table);
1849 
1850 static struct auxiliary_driver bnxt_re_driver = {
1851 	.name = "rdma",
1852 	.probe = bnxt_re_probe,
1853 	.remove = bnxt_re_remove,
1854 	.shutdown = bnxt_re_shutdown,
1855 	.suspend = bnxt_re_suspend,
1856 	.resume = bnxt_re_resume,
1857 	.id_table = bnxt_re_id_table,
1858 };
1859 
1860 static int __init bnxt_re_mod_init(void)
1861 {
1862 	int rc;
1863 
1864 	pr_info("%s: %s", ROCE_DRV_MODULE_NAME, version);
1865 	rc = auxiliary_driver_register(&bnxt_re_driver);
1866 	if (rc) {
1867 		pr_err("%s: Failed to register auxiliary driver\n",
1868 			ROCE_DRV_MODULE_NAME);
1869 		return rc;
1870 	}
1871 	return 0;
1872 }
1873 
1874 static void __exit bnxt_re_mod_exit(void)
1875 {
1876 	auxiliary_driver_unregister(&bnxt_re_driver);
1877 }
1878 
1879 module_init(bnxt_re_mod_init);
1880 module_exit(bnxt_re_mod_exit);
1881