xref: /src/sys/dev/bnxt/bnxt_re/main.c (revision 823f158a01330484f83f69c31d695035c607f8ee)
1 /*
2  * Copyright (c) 2015-2024, Broadcom. All rights reserved.  The term
3  * Broadcom refers to Broadcom Limited and/or its subsidiaries.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in
13  *    the documentation and/or other materials provided with the
14  *    distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS''
17  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
18  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS
20  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
23  * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
24  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
25  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
26  * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  *
28  * Description: Main component of the bnxt_re driver
29  */
30 
31 #include <linux/if_ether.h>
32 #include <linux/module.h>
33 #include <linux/errno.h>
34 #include <linux/pci.h>
35 #include <linux/dma-mapping.h>
36 #include <linux/slab.h>
37 #include <linux/sched.h>
38 #include <linux/delay.h>
39 #include <linux/fs.h>
40 #include <rdma/ib_user_verbs.h>
41 #include <rdma/ib_addr.h>
42 #include <rdma/ib_cache.h>
43 #include <dev/mlx5/port.h>
44 #include <dev/mlx5/vport.h>
45 #include <linux/list.h>
46 #include <rdma/ib_smi.h>
47 #include <rdma/ib_umem.h>
48 #include <linux/in.h>
49 #include <linux/etherdevice.h>
50 
51 #include "bnxt_re.h"
52 #include "ib_verbs.h"
53 #include "bnxt_re-abi.h"
54 #include "bnxt.h"
55 
56 static char drv_version[] =
57 		"Broadcom NetXtreme-C/E RoCE Driver " ROCE_DRV_MODULE_NAME \
58 		" v" ROCE_DRV_MODULE_VERSION " (" ROCE_DRV_MODULE_RELDATE ")\n";
59 
60 #define BNXT_RE_DESC	"Broadcom NetXtreme RoCE"
61 #define BNXT_ADEV_NAME "if_bnxt"
62 
63 MODULE_DESCRIPTION("Broadcom NetXtreme-C/E RoCE Driver");
64 MODULE_LICENSE("Dual BSD/GPL");
65 MODULE_DEPEND(bnxt_re, linuxkpi, 1, 1, 1);
66 MODULE_DEPEND(bnxt_re, ibcore, 1, 1, 1);
67 MODULE_DEPEND(bnxt_re, if_bnxt, 1, 1, 1);
68 MODULE_VERSION(bnxt_re, 1);
69 
70 
71 DEFINE_MUTEX(bnxt_re_mutex); /* mutex lock for driver */
72 
73 static unsigned int restrict_mrs = 0;
74 module_param(restrict_mrs, uint, 0);
75 MODULE_PARM_DESC(restrict_mrs, " Restrict the no. of MRs 0 = 256K , 1 = 64K");
76 
77 unsigned int restrict_stats = 0;
78 module_param(restrict_stats, uint, 0);
79 MODULE_PARM_DESC(restrict_stats, "Restrict stats query frequency to ethtool coalesce value. Disabled by default");
80 
81 unsigned int enable_fc = 1;
82 module_param(enable_fc, uint, 0);
83 MODULE_PARM_DESC(enable_fc, "Enable default PFC, CC,ETS during driver load. 1 - fc enable, 0 - fc disable - Default is 1");
84 
85 unsigned int min_tx_depth = 1;
86 module_param(min_tx_depth, uint, 0);
87 MODULE_PARM_DESC(min_tx_depth, "Minimum TX depth - Default is 1");
88 
89 static uint8_t max_msix_vec[BNXT_RE_MAX_DEVICES] = {0};
90 static unsigned int max_msix_vec_argc;
91 module_param_array(max_msix_vec, byte, &max_msix_vec_argc, 0444);
92 MODULE_PARM_DESC(max_msix_vec, "Max MSI-x vectors per PF (2 - 64) - Default is 64");
93 
94 unsigned int cmdq_shadow_qd = RCFW_CMD_NON_BLOCKING_SHADOW_QD;
95 module_param_named(cmdq_shadow_qd, cmdq_shadow_qd, uint, 0644);
96 MODULE_PARM_DESC(cmdq_shadow_qd, "Perf Stat Debug: Shadow QD Range (1-64) - Default is 64");
97 
98 
99 /* globals */
100 struct list_head bnxt_re_dev_list = LINUX_LIST_HEAD_INIT(bnxt_re_dev_list);
101 static int bnxt_re_probe_count;
102 
103 DEFINE_MUTEX(bnxt_re_dev_lock);
104 static u32 gmod_exit;
105 static u32 gadd_dev_inprogress;
106 
107 static void bnxt_re_task(struct work_struct *work_task);
108 static struct workqueue_struct *bnxt_re_wq;
109 static int bnxt_re_query_hwrm_intf_version(struct bnxt_re_dev *rdev);
110 static int bnxt_re_hwrm_qcfg(struct bnxt_re_dev *rdev, u32 *db_len,
111 			     u32 *offset);
112 static int bnxt_re_ib_init(struct bnxt_re_dev *rdev);
113 static void bnxt_re_ib_init_2(struct bnxt_re_dev *rdev);
114 void _bnxt_re_remove(struct auxiliary_device *adev);
115 void writel_fbsd(struct bnxt_softc *bp, u32, u8, u32);
116 u32 readl_fbsd(struct bnxt_softc *bp, u32, u8);
117 static int bnxt_re_hwrm_dbr_pacing_qcfg(struct bnxt_re_dev *rdev);
118 
bnxt_re_register_netdevice_notifier(struct notifier_block * nb)119 int bnxt_re_register_netdevice_notifier(struct notifier_block *nb)
120 {
121 	int rc;
122 	rc = register_netdevice_notifier(nb);
123 	return rc;
124 }
125 
bnxt_re_unregister_netdevice_notifier(struct notifier_block * nb)126 int bnxt_re_unregister_netdevice_notifier(struct notifier_block *nb)
127 {
128 	int rc;
129 	rc = unregister_netdevice_notifier(nb);
130 	return rc;
131 }
132 
bnxt_re_set_dma_device(struct ib_device * ibdev,struct bnxt_re_dev * rdev)133 void bnxt_re_set_dma_device(struct ib_device *ibdev, struct bnxt_re_dev *rdev)
134 {
135 	ibdev->dma_device = &rdev->en_dev->pdev->dev;
136 }
137 
bnxt_re_init_resolve_wq(struct bnxt_re_dev * rdev)138 void bnxt_re_init_resolve_wq(struct bnxt_re_dev *rdev)
139 {
140 	rdev->resolve_wq = create_singlethread_workqueue("bnxt_re_resolve_wq");
141 	 INIT_LIST_HEAD(&rdev->mac_wq_list);
142 }
143 
bnxt_re_uninit_resolve_wq(struct bnxt_re_dev * rdev)144 void bnxt_re_uninit_resolve_wq(struct bnxt_re_dev *rdev)
145 {
146 	struct bnxt_re_resolve_dmac_work *tmp_work = NULL, *tmp_st;
147 	if (!rdev->resolve_wq)
148 		return;
149 	flush_workqueue(rdev->resolve_wq);
150 	list_for_each_entry_safe(tmp_work, tmp_st, &rdev->mac_wq_list, list) {
151 			list_del(&tmp_work->list);
152 			kfree(tmp_work);
153 	}
154 	destroy_workqueue(rdev->resolve_wq);
155 	rdev->resolve_wq = NULL;
156 }
157 
readl_fbsd(struct bnxt_softc * bp,u32 reg_off,u8 bar_idx)158 u32 readl_fbsd(struct bnxt_softc *bp, u32 reg_off, u8 bar_idx)
159 {
160 
161 	if (bar_idx)
162 		return bus_space_read_8(bp->doorbell_bar.tag, bp->doorbell_bar.handle, reg_off);
163 	else
164 		return bus_space_read_8(bp->hwrm_bar.tag, bp->hwrm_bar.handle, reg_off);
165 }
166 
writel_fbsd(struct bnxt_softc * bp,u32 reg_off,u8 bar_idx,u32 val)167 void writel_fbsd(struct bnxt_softc *bp, u32 reg_off, u8 bar_idx, u32 val)
168 {
169 	if (bar_idx)
170 		bus_space_write_8(bp->doorbell_bar.tag, bp->doorbell_bar.handle, reg_off, htole32(val));
171 	else
172 		bus_space_write_8(bp->hwrm_bar.tag, bp->hwrm_bar.handle, reg_off, htole32(val));
173 }
174 
bnxt_re_update_fifo_occup_slabs(struct bnxt_re_dev * rdev,u32 fifo_occup)175 static void bnxt_re_update_fifo_occup_slabs(struct bnxt_re_dev *rdev,
176 					    u32 fifo_occup)
177 {
178 	if (fifo_occup > rdev->dbg_stats->dbq.fifo_occup_water_mark)
179 		rdev->dbg_stats->dbq.fifo_occup_water_mark = fifo_occup;
180 
181 	if (fifo_occup > 8 * rdev->pacing_algo_th)
182 		rdev->dbg_stats->dbq.fifo_occup_slab_4++;
183 	else if (fifo_occup > 4 * rdev->pacing_algo_th)
184 		rdev->dbg_stats->dbq.fifo_occup_slab_3++;
185 	else if (fifo_occup > 2 * rdev->pacing_algo_th)
186 		rdev->dbg_stats->dbq.fifo_occup_slab_2++;
187 	else if (fifo_occup > rdev->pacing_algo_th)
188 		rdev->dbg_stats->dbq.fifo_occup_slab_1++;
189 }
190 
bnxt_re_update_do_pacing_slabs(struct bnxt_re_dev * rdev)191 static void bnxt_re_update_do_pacing_slabs(struct bnxt_re_dev *rdev)
192 {
193 	struct bnxt_qplib_db_pacing_data *pacing_data = rdev->qplib_res.pacing_data;
194 
195 	if (pacing_data->do_pacing > rdev->dbg_stats->dbq.do_pacing_water_mark)
196 		rdev->dbg_stats->dbq.do_pacing_water_mark = pacing_data->do_pacing;
197 
198 	if (pacing_data->do_pacing > 16 * rdev->dbr_def_do_pacing)
199 		rdev->dbg_stats->dbq.do_pacing_slab_5++;
200 	else if (pacing_data->do_pacing > 8 * rdev->dbr_def_do_pacing)
201 		rdev->dbg_stats->dbq.do_pacing_slab_4++;
202 	else if (pacing_data->do_pacing > 4 * rdev->dbr_def_do_pacing)
203 		rdev->dbg_stats->dbq.do_pacing_slab_3++;
204 	else if (pacing_data->do_pacing > 2 * rdev->dbr_def_do_pacing)
205 		rdev->dbg_stats->dbq.do_pacing_slab_2++;
206 	else if (pacing_data->do_pacing > rdev->dbr_def_do_pacing)
207 		rdev->dbg_stats->dbq.do_pacing_slab_1++;
208 }
209 
bnxt_re_is_qp1_qp(struct bnxt_re_qp * qp)210 static bool bnxt_re_is_qp1_qp(struct bnxt_re_qp *qp)
211 {
212 	return qp->ib_qp.qp_type == IB_QPT_GSI;
213 }
214 
bnxt_re_get_qp1_qp(struct bnxt_re_dev * rdev)215 static struct bnxt_re_qp *bnxt_re_get_qp1_qp(struct bnxt_re_dev *rdev)
216 {
217 	struct bnxt_re_qp *qp;
218 
219 	mutex_lock(&rdev->qp_lock);
220 	list_for_each_entry(qp, &rdev->qp_list, list) {
221 		if (bnxt_re_is_qp1_qp(qp)) {
222 			mutex_unlock(&rdev->qp_lock);
223 			return qp;
224 		}
225 	}
226 	mutex_unlock(&rdev->qp_lock);
227 	return NULL;
228 }
229 
230 /* Set the maximum number of each resource that the driver actually wants
231  * to allocate. This may be up to the maximum number the firmware has
232  * reserved for the function. The driver may choose to allocate fewer
233  * resources than the firmware maximum.
234  */
bnxt_re_limit_pf_res(struct bnxt_re_dev * rdev)235 static void bnxt_re_limit_pf_res(struct bnxt_re_dev *rdev)
236 {
237 	struct bnxt_qplib_max_res dev_res = {};
238 	struct bnxt_qplib_chip_ctx *cctx;
239 	struct bnxt_qplib_dev_attr *attr;
240 	struct bnxt_qplib_ctx *hctx;
241 	int i;
242 
243 	attr = rdev->dev_attr;
244 	hctx = rdev->qplib_res.hctx;
245 	cctx = rdev->chip_ctx;
246 
247 	bnxt_qplib_max_res_supported(cctx, &rdev->qplib_res, &dev_res, false);
248 	if (!_is_chip_gen_p5_p7(cctx)) {
249 		hctx->qp_ctx.max = min_t(u32, dev_res.max_qp, attr->max_qp);
250 		hctx->mrw_ctx.max = min_t(u32, dev_res.max_mr, attr->max_mr);
251 		/* To accommodate 16k MRs and 16k AHs,
252 		 * driver has to allocate 32k backing store memory
253 		 */
254 		hctx->mrw_ctx.max *= 2;
255 		hctx->srq_ctx.max = min_t(u32, dev_res.max_srq, attr->max_srq);
256 		hctx->cq_ctx.max = min_t(u32, dev_res.max_cq, attr->max_cq);
257 		for (i = 0; i < MAX_TQM_ALLOC_REQ; i++)
258 			hctx->tqm_ctx.qcount[i] = attr->tqm_alloc_reqs[i];
259 	} else {
260 		hctx->qp_ctx.max = attr->max_qp ? attr->max_qp : dev_res.max_qp;
261 		hctx->mrw_ctx.max = attr->max_mr ? attr->max_mr : dev_res.max_mr;
262 		hctx->srq_ctx.max = attr->max_srq ? attr->max_srq : dev_res.max_srq;
263 		hctx->cq_ctx.max = attr->max_cq ? attr->max_cq : dev_res.max_cq;
264 	}
265 }
266 
bnxt_re_limit_vf_res(struct bnxt_re_dev * rdev,struct bnxt_qplib_vf_res * vf_res,u32 num_vf)267 static void bnxt_re_limit_vf_res(struct bnxt_re_dev *rdev,
268 				 struct bnxt_qplib_vf_res *vf_res,
269 				 u32 num_vf)
270 {
271 	struct bnxt_qplib_chip_ctx *cctx = rdev->chip_ctx;
272 	struct bnxt_qplib_max_res dev_res = {};
273 
274 	bnxt_qplib_max_res_supported(cctx, &rdev->qplib_res, &dev_res, true);
275 	vf_res->max_qp = dev_res.max_qp / num_vf;
276 	vf_res->max_srq = dev_res.max_srq / num_vf;
277 	vf_res->max_cq = dev_res.max_cq / num_vf;
278 	/*
279 	 * MR and AH shares the same backing store, the value specified
280 	 * for max_mrw is split into half by the FW for MR and AH
281 	 */
282 	vf_res->max_mrw = dev_res.max_mr * 2 / num_vf;
283 	vf_res->max_gid = BNXT_RE_MAX_GID_PER_VF;
284 }
285 
bnxt_re_set_resource_limits(struct bnxt_re_dev * rdev)286 static void bnxt_re_set_resource_limits(struct bnxt_re_dev *rdev)
287 {
288 	struct bnxt_qplib_ctx *hctx;
289 
290 	hctx = rdev->qplib_res.hctx;
291 	memset(&hctx->vf_res, 0, sizeof(struct bnxt_qplib_vf_res));
292 	bnxt_re_limit_pf_res(rdev);
293 
294 	if (rdev->num_vfs)
295 		bnxt_re_limit_vf_res(rdev, &hctx->vf_res, rdev->num_vfs);
296 }
297 
bnxt_re_dettach_irq(struct bnxt_re_dev * rdev)298 static void bnxt_re_dettach_irq(struct bnxt_re_dev *rdev)
299 {
300 	struct bnxt_qplib_rcfw *rcfw = NULL;
301 	struct bnxt_qplib_nq *nq;
302 	int indx;
303 
304 	rcfw = &rdev->rcfw;
305 	for (indx = 0; indx < rdev->nqr.max_init; indx++) {
306 		nq = &rdev->nqr.nq[indx];
307 		mutex_lock(&nq->lock);
308 		bnxt_qplib_nq_stop_irq(nq, false);
309 		mutex_unlock(&nq->lock);
310 	}
311 
312 	bnxt_qplib_rcfw_stop_irq(rcfw, false);
313 }
314 
bnxt_re_detach_err_device(struct bnxt_re_dev * rdev)315 static void bnxt_re_detach_err_device(struct bnxt_re_dev *rdev)
316 {
317 	/* Free the MSIx vectors only so that L2 can proceed with MSIx disable */
318 	bnxt_re_dettach_irq(rdev);
319 
320 	/* Set the state as detached to prevent sending any more commands */
321 	set_bit(ERR_DEVICE_DETACHED, &rdev->rcfw.cmdq.flags);
322 	set_bit(BNXT_RE_FLAG_ERR_DEVICE_DETACHED, &rdev->flags);
323 	wake_up_all(&rdev->rcfw.cmdq.waitq);
324 }
325 
326 #define MAX_DSCP_PRI_TUPLE	64
327 
328 struct bnxt_re_dcb_work {
329 	struct work_struct work;
330 	struct bnxt_re_dev *rdev;
331 	struct hwrm_async_event_cmpl cmpl;
332 };
333 
bnxt_re_init_dcb_wq(struct bnxt_re_dev * rdev)334 static void bnxt_re_init_dcb_wq(struct bnxt_re_dev *rdev)
335 {
336 	rdev->dcb_wq = create_singlethread_workqueue("bnxt_re_dcb_wq");
337 }
338 
bnxt_re_uninit_dcb_wq(struct bnxt_re_dev * rdev)339 static void bnxt_re_uninit_dcb_wq(struct bnxt_re_dev *rdev)
340 {
341 	if (!rdev->dcb_wq)
342 		return;
343 	flush_workqueue(rdev->dcb_wq);
344 	destroy_workqueue(rdev->dcb_wq);
345 	rdev->dcb_wq = NULL;
346 }
347 
bnxt_re_init_aer_wq(struct bnxt_re_dev * rdev)348 static void bnxt_re_init_aer_wq(struct bnxt_re_dev *rdev)
349 {
350 	rdev->aer_wq = create_singlethread_workqueue("bnxt_re_aer_wq");
351 }
352 
bnxt_re_uninit_aer_wq(struct bnxt_re_dev * rdev)353 static void bnxt_re_uninit_aer_wq(struct bnxt_re_dev *rdev)
354 {
355 	if (!rdev->aer_wq)
356 		return;
357 	flush_workqueue(rdev->aer_wq);
358 	destroy_workqueue(rdev->aer_wq);
359 	rdev->aer_wq = NULL;
360 }
361 
bnxt_re_update_qp1_tos_dscp(struct bnxt_re_dev * rdev)362 static int bnxt_re_update_qp1_tos_dscp(struct bnxt_re_dev *rdev)
363 {
364 	struct bnxt_re_qp *qp;
365 
366 	if (!_is_chip_gen_p5_p7(rdev->chip_ctx))
367 		return 0;
368 
369 	qp = bnxt_re_get_qp1_qp(rdev);
370 	if (!qp)
371 		return 0;
372 
373 	qp->qplib_qp.modify_flags = CMDQ_MODIFY_QP_MODIFY_MASK_TOS_DSCP;
374 	qp->qplib_qp.tos_dscp = rdev->cc_param.qp1_tos_dscp;
375 
376 	return bnxt_qplib_modify_qp(&rdev->qplib_res, &qp->qplib_qp);
377 }
378 
bnxt_re_reconfigure_dscp(struct bnxt_re_dev * rdev)379 static void bnxt_re_reconfigure_dscp(struct bnxt_re_dev *rdev)
380 {
381 	struct bnxt_qplib_cc_param *cc_param;
382 	struct bnxt_re_tc_rec *tc_rec;
383 	bool update_cc = false;
384 	u8 dscp_user;
385 	int rc;
386 
387 	cc_param = &rdev->cc_param;
388 	tc_rec = &rdev->tc_rec[0];
389 
390 	if (!(cc_param->roce_dscp_user || cc_param->cnp_dscp_user))
391 		return;
392 
393 	if (cc_param->cnp_dscp_user) {
394 		dscp_user = (cc_param->cnp_dscp_user & 0x3f);
395 		if ((tc_rec->cnp_dscp_bv & (1ul << dscp_user)) &&
396 		    (cc_param->alt_tos_dscp != dscp_user)) {
397 			cc_param->alt_tos_dscp = dscp_user;
398 			cc_param->mask |= CMDQ_MODIFY_ROCE_CC_MODIFY_MASK_ALT_TOS_DSCP;
399 			update_cc = true;
400 		}
401 	}
402 
403 	if (cc_param->roce_dscp_user) {
404 		dscp_user = (cc_param->roce_dscp_user & 0x3f);
405 		if ((tc_rec->roce_dscp_bv & (1ul << dscp_user)) &&
406 		    (cc_param->tos_dscp != dscp_user)) {
407 			cc_param->tos_dscp = dscp_user;
408 			cc_param->mask |= CMDQ_MODIFY_ROCE_CC_MODIFY_MASK_TOS_DSCP;
409 			update_cc = true;
410 		}
411 	}
412 
413 	if (update_cc) {
414 		rc = bnxt_qplib_modify_cc(&rdev->qplib_res, cc_param);
415 		if (rc)
416 			dev_err(rdev_to_dev(rdev), "Failed to apply cc settings\n");
417 	}
418 }
419 
bnxt_re_dcb_wq_task(struct work_struct * work)420 static void bnxt_re_dcb_wq_task(struct work_struct *work)
421 {
422 	struct bnxt_qplib_cc_param *cc_param;
423 	struct bnxt_re_tc_rec *tc_rec;
424 	struct bnxt_re_dev *rdev;
425 	struct bnxt_re_dcb_work *dcb_work =
426 			container_of(work, struct bnxt_re_dcb_work, work);
427 	int rc;
428 
429 	rdev = dcb_work->rdev;
430 	if (!rdev)
431 		goto exit;
432 
433 	mutex_lock(&rdev->cc_lock);
434 
435 	cc_param = &rdev->cc_param;
436 	rc = bnxt_qplib_query_cc_param(&rdev->qplib_res, cc_param);
437 	if (rc) {
438 		dev_err(rdev_to_dev(rdev), "Failed to query ccparam rc:%d", rc);
439 		goto fail;
440 	}
441 	tc_rec = &rdev->tc_rec[0];
442 	/*
443 	 * Upon the receival of DCB Async event:
444 	 *   If roce_dscp or cnp_dscp or both (which user configured using configfs)
445 	 *   is in the list, re-program the value using modify_roce_cc command
446 	 */
447 	bnxt_re_reconfigure_dscp(rdev);
448 
449 	cc_param->roce_pri = tc_rec->roce_prio;
450 	if (cc_param->qp1_tos_dscp != cc_param->tos_dscp) {
451 		cc_param->qp1_tos_dscp = cc_param->tos_dscp;
452 		rc = bnxt_re_update_qp1_tos_dscp(rdev);
453 		if (rc) {
454 			dev_err(rdev_to_dev(rdev), "%s:Failed to modify QP1 rc:%d",
455 				__func__, rc);
456 			goto fail;
457 		}
458 	}
459 
460 fail:
461 	mutex_unlock(&rdev->cc_lock);
462 exit:
463 	kfree(dcb_work);
464 }
465 
bnxt_re_hwrm_dbr_pacing_broadcast_event(struct bnxt_re_dev * rdev)466 static int bnxt_re_hwrm_dbr_pacing_broadcast_event(struct bnxt_re_dev *rdev)
467 {
468 	struct hwrm_func_dbr_pacing_broadcast_event_output resp = {0};
469 	struct hwrm_func_dbr_pacing_broadcast_event_input req = {0};
470 	struct bnxt_en_dev *en_dev = rdev->en_dev;
471 	struct bnxt_fw_msg fw_msg;
472 	int rc;
473 
474 	memset(&fw_msg, 0, sizeof(fw_msg));
475 	bnxt_re_init_hwrm_hdr(rdev, (void *)&req,
476 			      HWRM_FUNC_DBR_PACING_BROADCAST_EVENT, -1, -1);
477 	bnxt_re_fill_fw_msg(&fw_msg, (void *)&req, sizeof(req), (void *)&resp,
478 			    sizeof(resp), BNXT_RE_HWRM_CMD_TIMEOUT(rdev));
479 	rc = en_dev->en_ops->bnxt_send_fw_msg(en_dev, BNXT_ROCE_ULP, &fw_msg);
480 	if (rc) {
481 		dev_dbg(rdev_to_dev(rdev),
482 			"Failed to send dbr pacing broadcast event rc:%d", rc);
483 		return rc;
484 	}
485 	return 0;
486 }
487 
bnxt_re_hwrm_dbr_pacing_nqlist_query(struct bnxt_re_dev * rdev)488 static int bnxt_re_hwrm_dbr_pacing_nqlist_query(struct bnxt_re_dev *rdev)
489 {
490 	struct hwrm_func_dbr_pacing_nqlist_query_output resp = {0};
491 	struct hwrm_func_dbr_pacing_nqlist_query_input req = {0};
492 	struct bnxt_dbq_nq_list *nq_list = &rdev->nq_list;
493 	struct bnxt_en_dev *en_dev = rdev->en_dev;
494 	bool primary_found = false;
495 	struct bnxt_fw_msg fw_msg;
496 	struct bnxt_qplib_nq *nq;
497 	int rc, i, j = 1;
498 	u16 *nql_ptr;
499 
500 	nq = &rdev->nqr.nq[0];
501 
502 	memset(&fw_msg, 0, sizeof(fw_msg));
503 	bnxt_re_init_hwrm_hdr(rdev, (void *)&req,
504 			      HWRM_FUNC_DBR_PACING_NQLIST_QUERY, -1, -1);
505 	bnxt_re_fill_fw_msg(&fw_msg, (void *)&req, sizeof(req), (void *)&resp,
506 			    sizeof(resp), BNXT_RE_HWRM_CMD_TIMEOUT(rdev));
507 	rc = en_dev->en_ops->bnxt_send_fw_msg(en_dev, BNXT_ROCE_ULP, &fw_msg);
508 	if (rc) {
509 		dev_err(rdev_to_dev(rdev), "Failed to send dbr pacing nq list query rc:%d", rc);
510 		return rc;
511 	}
512 	nq_list->num_nql_entries = le32_to_cpu(resp.num_nqs);
513 	nql_ptr = &resp.nq_ring_id0;
514 	/* populate the nq_list of the primary function with list received
515 	 * from FW. Fill the NQ IDs of secondary functions from index 1 to
516 	 * num_nql_entries - 1. Fill the  nq_list->nq_id[0] with the
517 	 * nq_id of the primary pf
518 	 */
519 	for (i = 0; i < nq_list->num_nql_entries; i++) {
520 		u16 nq_id = *nql_ptr;
521 
522 		dev_dbg(rdev_to_dev(rdev),
523 			"nq_list->nq_id[%d] = %d\n", i, nq_id);
524 		if (nq_id != nq->ring_id) {
525 			nq_list->nq_id[j] = nq_id;
526 			j++;
527 		} else {
528 			primary_found = true;
529 			nq_list->nq_id[0] = nq->ring_id;
530 		}
531 		nql_ptr++;
532 	}
533 	if (primary_found)
534 		bnxt_qplib_dbr_pacing_set_primary_pf(rdev->chip_ctx, 1);
535 
536 	return 0;
537 }
538 
__wait_for_fifo_occupancy_below_th(struct bnxt_re_dev * rdev)539 static void __wait_for_fifo_occupancy_below_th(struct bnxt_re_dev *rdev)
540 {
541 	struct bnxt_qplib_db_pacing_data *pacing_data = rdev->qplib_res.pacing_data;
542 	u32 read_val, fifo_occup;
543 	bool first_read = true;
544 
545 	/* loop shouldn't run infintely as the occupancy usually goes
546 	 * below pacing algo threshold as soon as pacing kicks in.
547 	 */
548 	while (1) {
549 		read_val = readl_fbsd(rdev->en_dev->softc, rdev->dbr_db_fifo_reg_off, 0);
550 		fifo_occup = pacing_data->fifo_max_depth -
551 			     ((read_val & pacing_data->fifo_room_mask) >>
552 			      pacing_data->fifo_room_shift);
553 		/* Fifo occupancy cannot be greater the MAX FIFO depth */
554 		if (fifo_occup > pacing_data->fifo_max_depth)
555 			break;
556 
557 		if (first_read) {
558 			bnxt_re_update_fifo_occup_slabs(rdev, fifo_occup);
559 			first_read = false;
560 		}
561 		if (fifo_occup < pacing_data->pacing_th)
562 			break;
563 	}
564 }
565 
bnxt_re_set_default_pacing_data(struct bnxt_re_dev * rdev)566 static void bnxt_re_set_default_pacing_data(struct bnxt_re_dev *rdev)
567 {
568 	struct bnxt_qplib_db_pacing_data *pacing_data = rdev->qplib_res.pacing_data;
569 
570 	pacing_data->do_pacing = rdev->dbr_def_do_pacing;
571 	pacing_data->pacing_th = rdev->pacing_algo_th;
572 	pacing_data->alarm_th =
573 		pacing_data->pacing_th * BNXT_RE_PACING_ALARM_TH_MULTIPLE(rdev->chip_ctx);
574 }
575 
576 #define CAG_RING_MASK 0x7FF
577 #define CAG_RING_SHIFT 17
578 #define WATERMARK_MASK 0xFFF
579 #define WATERMARK_SHIFT	0
580 
bnxt_re_check_if_dbq_intr_triggered(struct bnxt_re_dev * rdev)581 static bool bnxt_re_check_if_dbq_intr_triggered(struct bnxt_re_dev *rdev)
582 {
583 	u32 read_val;
584 	int j;
585 
586 	for (j = 0; j < 10; j++) {
587 		read_val = readl_fbsd(rdev->en_dev->softc, rdev->dbr_aeq_arm_reg_off, 0);
588 		dev_dbg(rdev_to_dev(rdev), "AEQ ARM status = 0x%x\n",
589 			read_val);
590 		if (!read_val)
591 			return true;
592 	}
593 	return false;
594 }
595 
bnxt_re_set_dbq_throttling_reg(struct bnxt_re_dev * rdev,u16 nq_id,u32 throttle)596 int bnxt_re_set_dbq_throttling_reg(struct bnxt_re_dev *rdev, u16 nq_id, u32 throttle)
597 {
598 	u32 cag_ring_water_mark = 0, read_val;
599 	u32 throttle_val;
600 
601 	/* Convert throttle percentage to value */
602 	throttle_val = (rdev->qplib_res.pacing_data->fifo_max_depth * throttle) / 100;
603 
604 	if (bnxt_qplib_dbr_pacing_ext_en(rdev->chip_ctx)) {
605 		cag_ring_water_mark = (nq_id & CAG_RING_MASK) << CAG_RING_SHIFT |
606 				      (throttle_val & WATERMARK_MASK);
607 		writel_fbsd(rdev->en_dev->softc,  rdev->dbr_throttling_reg_off, 0, cag_ring_water_mark);
608 		read_val = readl_fbsd(rdev->en_dev->softc , rdev->dbr_throttling_reg_off, 0);
609 		dev_dbg(rdev_to_dev(rdev),
610 			"%s: dbr_throttling_reg_off read_val = 0x%x\n",
611 			__func__, read_val);
612 		if (read_val != cag_ring_water_mark) {
613 			dev_dbg(rdev_to_dev(rdev),
614 				"nq_id = %d write_val=0x%x read_val=0x%x\n",
615 				nq_id, cag_ring_water_mark, read_val);
616 			return 1;
617 		}
618 	}
619 	writel_fbsd(rdev->en_dev->softc,  rdev->dbr_aeq_arm_reg_off, 0, 1);
620 	return 0;
621 }
622 
bnxt_re_set_dbq_throttling_for_non_primary(struct bnxt_re_dev * rdev)623 static void bnxt_re_set_dbq_throttling_for_non_primary(struct bnxt_re_dev *rdev)
624 {
625 	struct bnxt_dbq_nq_list *nq_list;
626 	struct bnxt_qplib_nq *nq;
627 	int i;
628 
629 	nq_list = &rdev->nq_list;
630 	/* Run a loop for other Active functions if this is primary function */
631 	if (bnxt_qplib_dbr_pacing_is_primary_pf(rdev->chip_ctx)) {
632 		dev_dbg(rdev_to_dev(rdev), "%s:  nq_list->num_nql_entries= %d\n",
633 			__func__, nq_list->num_nql_entries);
634 		nq = &rdev->nqr.nq[0];
635 		for (i = nq_list->num_nql_entries - 1; i > 0; i--) {
636 			u16 nq_id = nq_list->nq_id[i];
637 			if (nq)
638 				dev_dbg(rdev_to_dev(rdev),
639 					"%s: nq_id = %d cur_fn_ring_id = %d\n",
640 					__func__, nq_id, nq->ring_id);
641 			if (bnxt_re_set_dbq_throttling_reg
642 					(rdev, nq_id, 0))
643 				break;
644 			bnxt_re_check_if_dbq_intr_triggered(rdev);
645 		}
646 	}
647 }
648 
bnxt_re_handle_dbr_nq_pacing_notification(struct bnxt_re_dev * rdev)649 static void bnxt_re_handle_dbr_nq_pacing_notification(struct bnxt_re_dev *rdev)
650 {
651 	struct bnxt_qplib_nq *nq;
652 	int rc = 0;
653 
654 	nq = &rdev->nqr.nq[0];
655 
656 	/* Query the NQ list*/
657 	rc = bnxt_re_hwrm_dbr_pacing_nqlist_query(rdev);
658 	if (rc) {
659 		dev_err(rdev_to_dev(rdev),
660 			"Failed to Query NQ list rc= %d", rc);
661 		return;
662 	}
663 	/*Configure GRC access for Throttling and aeq_arm register */
664 	writel_fbsd(rdev->en_dev->softc,  BNXT_GRCPF_REG_WINDOW_BASE_OUT + 28, 0,
665 		    rdev->chip_ctx->dbr_aeq_arm_reg & BNXT_GRC_BASE_MASK);
666 
667 	rdev->dbr_throttling_reg_off =
668 		(rdev->chip_ctx->dbr_throttling_reg &
669 		 BNXT_GRC_OFFSET_MASK) + 0x8000;
670 	rdev->dbr_aeq_arm_reg_off =
671 		(rdev->chip_ctx->dbr_aeq_arm_reg &
672 		 BNXT_GRC_OFFSET_MASK) + 0x8000;
673 
674 	bnxt_re_set_dbq_throttling_reg(rdev, nq->ring_id, rdev->dbq_watermark);
675 }
676 
bnxt_re_dbq_wq_task(struct work_struct * work)677 static void bnxt_re_dbq_wq_task(struct work_struct *work)
678 {
679 	struct bnxt_re_dbq_work *dbq_work =
680 			container_of(work, struct bnxt_re_dbq_work, work);
681 	struct bnxt_re_dev *rdev;
682 
683 	rdev = dbq_work->rdev;
684 
685 	if (!rdev)
686 		goto exit;
687 	switch (dbq_work->event) {
688 	case BNXT_RE_DBQ_EVENT_SCHED:
689 		dev_dbg(rdev_to_dev(rdev), "%s: Handle DBQ Pacing event\n",
690 			__func__);
691 		if (!bnxt_qplib_dbr_pacing_ext_en(rdev->chip_ctx))
692 			bnxt_re_hwrm_dbr_pacing_broadcast_event(rdev);
693 		else
694 			bnxt_re_pacing_alert(rdev);
695 		break;
696 	case BNXT_RE_DBR_PACING_EVENT:
697 		dev_dbg(rdev_to_dev(rdev), "%s: Sched interrupt/pacing worker\n",
698 			__func__);
699 		if (_is_chip_p7(rdev->chip_ctx))
700 			bnxt_re_pacing_alert(rdev);
701 		else if (!rdev->chip_ctx->modes.dbr_pacing_v0)
702 			bnxt_re_hwrm_dbr_pacing_qcfg(rdev);
703 		break;
704 	case BNXT_RE_DBR_NQ_PACING_NOTIFICATION:
705 		bnxt_re_handle_dbr_nq_pacing_notification(rdev);
706 		/* Issue a broadcast event to notify other functions
707 		 * that primary changed
708 		 */
709 		bnxt_re_hwrm_dbr_pacing_broadcast_event(rdev);
710 		break;
711 	}
712 exit:
713 	kfree(dbq_work);
714 }
715 
bnxt_re_async_notifier(void * handle,struct hwrm_async_event_cmpl * cmpl)716 static void bnxt_re_async_notifier(void *handle, struct hwrm_async_event_cmpl *cmpl)
717 {
718 	struct bnxt_re_en_dev_info *en_info = auxiliary_get_drvdata(handle);
719 	struct bnxt_re_dcb_work *dcb_work;
720 	struct bnxt_re_dbq_work *dbq_work;
721 	struct bnxt_re_dev *rdev;
722 	u16 event_id;
723 	u32 data1;
724 	u32 data2 = 0;
725 
726 	if (!cmpl) {
727 		pr_err("Async event, bad completion\n");
728 		return;
729 	}
730 
731 	if (!en_info || !en_info->en_dev) {
732 		pr_err("Async event, bad en_info or en_dev\n");
733 		return;
734 	}
735 	rdev = en_info->rdev;
736 
737 	event_id = le16_to_cpu(cmpl->event_id);
738 	data1 = le32_to_cpu(cmpl->event_data1);
739 	data2 = le32_to_cpu(cmpl->event_data2);
740 
741 	if (!rdev || !rdev_to_dev(rdev)) {
742 		dev_dbg(NULL, "Async event, bad rdev or netdev\n");
743 		return;
744 	}
745 
746 	if (test_bit(BNXT_RE_FLAG_ERR_DEVICE_DETACHED, &rdev->flags) ||
747 	    !test_bit(BNXT_RE_FLAG_NETDEV_REGISTERED, &rdev->flags)) {
748 		dev_dbg(NULL, "Async event, device already detached\n");
749 		return;
750 	}
751 	if (data2 >= 0)
752 		dev_dbg(rdev_to_dev(rdev), "Async event_id = %d data1 = %d data2 = %d",
753 			event_id, data1, data2);
754 
755 	switch (event_id) {
756 	case HWRM_ASYNC_EVENT_CMPL_EVENT_ID_DCB_CONFIG_CHANGE:
757 		/* Not handling the event in older FWs */
758 		if (!is_qport_service_type_supported(rdev))
759 			break;
760 		if (!rdev->dcb_wq)
761 			break;
762 		dcb_work = kzalloc(sizeof(*dcb_work), GFP_ATOMIC);
763 		if (!dcb_work)
764 			break;
765 
766 		dcb_work->rdev = rdev;
767 		memcpy(&dcb_work->cmpl, cmpl, sizeof(*cmpl));
768 		INIT_WORK(&dcb_work->work, bnxt_re_dcb_wq_task);
769 		queue_work(rdev->dcb_wq, &dcb_work->work);
770 		break;
771 	case HWRM_ASYNC_EVENT_CMPL_EVENT_ID_RESET_NOTIFY:
772 		if (EVENT_DATA1_RESET_NOTIFY_FATAL(data1)) {
773 			/* Set rcfw flag to control commands send to Bono */
774 			set_bit(ERR_DEVICE_DETACHED, &rdev->rcfw.cmdq.flags);
775 			/* Set bnxt_re flag to control commands send via L2 driver */
776 			set_bit(BNXT_RE_FLAG_ERR_DEVICE_DETACHED, &rdev->flags);
777 			wake_up_all(&rdev->rcfw.cmdq.waitq);
778 		}
779 		break;
780 	case HWRM_ASYNC_EVENT_CMPL_EVENT_ID_DOORBELL_PACING_THRESHOLD:
781 		if (!rdev->dbr_pacing)
782 			break;
783 		dbq_work = kzalloc(sizeof(*dbq_work), GFP_ATOMIC);
784 		if (!dbq_work)
785 			goto unlock;
786 		dbq_work->rdev = rdev;
787 		dbq_work->event = BNXT_RE_DBR_PACING_EVENT;
788 		INIT_WORK(&dbq_work->work, bnxt_re_dbq_wq_task);
789 		queue_work(rdev->dbq_wq, &dbq_work->work);
790 		rdev->dbr_sw_stats->dbq_int_recv++;
791 		break;
792 	case HWRM_ASYNC_EVENT_CMPL_EVENT_ID_DOORBELL_PACING_NQ_UPDATE:
793 		if (!rdev->dbr_pacing)
794 			break;
795 
796 		dbq_work = kzalloc(sizeof(*dbq_work), GFP_ATOMIC);
797 		if (!dbq_work)
798 			goto unlock;
799 		dbq_work->rdev = rdev;
800 		dbq_work->event = BNXT_RE_DBR_NQ_PACING_NOTIFICATION;
801 		INIT_WORK(&dbq_work->work, bnxt_re_dbq_wq_task);
802 		queue_work(rdev->dbq_wq, &dbq_work->work);
803 		break;
804 
805 	default:
806 		break;
807 	}
808 unlock:
809 	return;
810 }
811 
bnxt_re_db_fifo_check(struct work_struct * work)812 static void bnxt_re_db_fifo_check(struct work_struct *work)
813 {
814 	struct bnxt_re_dev *rdev = container_of(work, struct bnxt_re_dev,
815 						dbq_fifo_check_work);
816 	struct bnxt_qplib_db_pacing_data *pacing_data;
817 	u32 pacing_save;
818 
819 	if (!mutex_trylock(&rdev->dbq_lock))
820 		return;
821 	pacing_data = rdev->qplib_res.pacing_data;
822 	pacing_save = rdev->do_pacing_save;
823 	__wait_for_fifo_occupancy_below_th(rdev);
824 	cancel_delayed_work_sync(&rdev->dbq_pacing_work);
825 	if (rdev->dbr_recovery_on)
826 		goto recovery_on;
827 	if (pacing_save > rdev->dbr_def_do_pacing) {
828 		/* Double the do_pacing value during the congestion */
829 		pacing_save = pacing_save << 1;
830 	} else {
831 		/*
832 		 * when a new congestion is detected increase the do_pacing
833 		 * by 8 times. And also increase the pacing_th by 4 times. The
834 		 * reason to increase pacing_th is to give more space for the
835 		 * queue to oscillate down without getting empty, but also more
836 		 * room for the queue to increase without causing another alarm.
837 		 */
838 		pacing_save = pacing_save << 3;
839 		pacing_data->pacing_th = rdev->pacing_algo_th * 4;
840 	}
841 
842 	if (pacing_save > BNXT_RE_MAX_DBR_DO_PACING)
843 		pacing_save = BNXT_RE_MAX_DBR_DO_PACING;
844 
845 	pacing_data->do_pacing = pacing_save;
846 	rdev->do_pacing_save = pacing_data->do_pacing;
847 	pacing_data->alarm_th =
848 		pacing_data->pacing_th * BNXT_RE_PACING_ALARM_TH_MULTIPLE(rdev->chip_ctx);
849 recovery_on:
850 	schedule_delayed_work(&rdev->dbq_pacing_work,
851 			      msecs_to_jiffies(rdev->dbq_pacing_time));
852 	rdev->dbr_sw_stats->dbq_pacing_alerts++;
853 	mutex_unlock(&rdev->dbq_lock);
854 }
855 
bnxt_re_pacing_timer_exp(struct work_struct * work)856 static void bnxt_re_pacing_timer_exp(struct work_struct *work)
857 {
858 	struct bnxt_re_dev *rdev = container_of(work, struct bnxt_re_dev,
859 						dbq_pacing_work.work);
860 	struct bnxt_qplib_db_pacing_data *pacing_data;
861 	u32 read_val, fifo_occup;
862 	struct bnxt_qplib_nq *nq;
863 
864 	if (!mutex_trylock(&rdev->dbq_lock))
865 		return;
866 
867 	pacing_data = rdev->qplib_res.pacing_data;
868 	read_val = readl_fbsd(rdev->en_dev->softc , rdev->dbr_db_fifo_reg_off, 0);
869 	fifo_occup = pacing_data->fifo_max_depth -
870 		     ((read_val & pacing_data->fifo_room_mask) >>
871 		      pacing_data->fifo_room_shift);
872 
873 	if (fifo_occup > pacing_data->pacing_th)
874 		goto restart_timer;
875 
876 	/*
877 	 * Instead of immediately going back to the default do_pacing
878 	 * reduce it by 1/8 times and restart the timer.
879 	 */
880 	pacing_data->do_pacing = pacing_data->do_pacing - (pacing_data->do_pacing >> 3);
881 	pacing_data->do_pacing = max_t(u32, rdev->dbr_def_do_pacing, pacing_data->do_pacing);
882 	/*
883 	 * If the fifo_occup is less than the interrupt enable threshold
884 	 * enable the interrupt on the primary PF.
885 	 */
886 	if (rdev->dbq_int_disable && fifo_occup < rdev->pacing_en_int_th) {
887 		if (bnxt_qplib_dbr_pacing_is_primary_pf(rdev->chip_ctx)) {
888 			if (!rdev->chip_ctx->modes.dbr_pacing_v0) {
889 				nq = &rdev->nqr.nq[0];
890 				bnxt_re_set_dbq_throttling_reg(rdev, nq->ring_id,
891 							       rdev->dbq_watermark);
892 				rdev->dbr_sw_stats->dbq_int_en++;
893 				rdev->dbq_int_disable = false;
894 			}
895 		}
896 	}
897 	if (pacing_data->do_pacing <= rdev->dbr_def_do_pacing) {
898 		bnxt_re_set_default_pacing_data(rdev);
899 		rdev->dbr_sw_stats->dbq_pacing_complete++;
900 		goto dbq_unlock;
901 	}
902 restart_timer:
903 	schedule_delayed_work(&rdev->dbq_pacing_work,
904 			      msecs_to_jiffies(rdev->dbq_pacing_time));
905 	bnxt_re_update_do_pacing_slabs(rdev);
906 	rdev->dbr_sw_stats->dbq_pacing_resched++;
907 dbq_unlock:
908 	rdev->do_pacing_save = pacing_data->do_pacing;
909 	mutex_unlock(&rdev->dbq_lock);
910 }
911 
bnxt_re_pacing_alert(struct bnxt_re_dev * rdev)912 void bnxt_re_pacing_alert(struct bnxt_re_dev *rdev)
913 {
914 	struct bnxt_qplib_db_pacing_data *pacing_data;
915 
916 	if (!rdev->dbr_pacing)
917 		return;
918 	mutex_lock(&rdev->dbq_lock);
919 	pacing_data = rdev->qplib_res.pacing_data;
920 
921 	/*
922 	 * Increase the alarm_th to max so that other user lib instances do not
923 	 * keep alerting the driver.
924 	 */
925 	pacing_data->alarm_th = pacing_data->fifo_max_depth;
926 	pacing_data->do_pacing = BNXT_RE_MAX_DBR_DO_PACING;
927 	cancel_work_sync(&rdev->dbq_fifo_check_work);
928 	schedule_work(&rdev->dbq_fifo_check_work);
929 	mutex_unlock(&rdev->dbq_lock);
930 }
931 
bnxt_re_schedule_dbq_event(struct bnxt_qplib_res * res)932 void bnxt_re_schedule_dbq_event(struct bnxt_qplib_res *res)
933 {
934 	struct bnxt_re_dbq_work *dbq_work;
935 	struct bnxt_re_dev *rdev;
936 
937 	rdev = container_of(res, struct bnxt_re_dev, qplib_res);
938 
939 	atomic_set(&rdev->dbq_intr_running, 1);
940 
941 	if (test_bit(BNXT_RE_FLAG_ERR_DEVICE_DETACHED, &rdev->flags))
942 		goto exit;
943 	/* Run the loop to send dbq event to other functions
944 	 * for newer FW
945 	 */
946 	if (bnxt_qplib_dbr_pacing_ext_en(rdev->chip_ctx) &&
947 	    !rdev->chip_ctx->modes.dbr_pacing_v0)
948 		bnxt_re_set_dbq_throttling_for_non_primary(rdev);
949 
950 	dbq_work = kzalloc(sizeof(*dbq_work), GFP_ATOMIC);
951 	if (!dbq_work)
952 		goto exit;
953 	dbq_work->rdev = rdev;
954 	dbq_work->event = BNXT_RE_DBQ_EVENT_SCHED;
955 	INIT_WORK(&dbq_work->work, bnxt_re_dbq_wq_task);
956 	queue_work(rdev->dbq_wq, &dbq_work->work);
957 	rdev->dbr_sw_stats->dbq_int_recv++;
958 	rdev->dbq_int_disable = true;
959 exit:
960 	atomic_set(&rdev->dbq_intr_running, 0);
961 }
962 
bnxt_re_free_msix(struct bnxt_re_dev * rdev)963 static void bnxt_re_free_msix(struct bnxt_re_dev *rdev)
964 {
965 	struct bnxt_en_dev *en_dev = rdev->en_dev;
966 	int rc;
967 
968 	rc = en_dev->en_ops->bnxt_free_msix(rdev->en_dev, BNXT_ROCE_ULP);
969 	if (rc)
970 		dev_err(rdev_to_dev(rdev), "netdev %p free_msix failed! rc = 0x%x",
971 			rdev->netdev, rc);
972 }
973 
bnxt_re_request_msix(struct bnxt_re_dev * rdev)974 static int bnxt_re_request_msix(struct bnxt_re_dev *rdev)
975 {
976 	struct bnxt_en_dev *en_dev = rdev->en_dev;
977 	int rc = 0, num_msix_want, num_msix_got;
978 	struct bnxt_msix_entry *entry;
979 
980 	/*
981 	 * Request MSIx based on the function type. This is
982 	 * a temporory solution to enable max VFs when NPAR is
983 	 * enabled.
984 	 * TODO - change the scheme with an adapter specific check
985 	 * as the latest adapters can support more NQs. For now
986 	 * this change satisfy all adapter versions.
987 	 */
988 
989 	if (rdev->is_virtfn)
990 		num_msix_want = BNXT_RE_MAX_MSIX_VF;
991 	else if (BNXT_EN_NPAR(en_dev))
992 		num_msix_want = BNXT_RE_MAX_MSIX_NPAR_PF;
993 	else if (_is_chip_gen_p5_p7(rdev->chip_ctx))
994 		num_msix_want = rdev->num_msix_requested ?: BNXT_RE_MAX_MSIX_GEN_P5_PF;
995 	else
996 		num_msix_want = BNXT_RE_MAX_MSIX_PF;
997 
998 	/*
999 	 * Since MSIX vectors are used for both NQs and CREQ, we should try to
1000 	 * allocate num_online_cpus + 1 by taking into account the CREQ. This
1001 	 * leaves the number of MSIX vectors for NQs match the number of CPUs
1002 	 * and allows the system to be fully utilized
1003 	 */
1004 	num_msix_want = min_t(u32, num_msix_want, num_online_cpus() + 1);
1005 	num_msix_want = min_t(u32, num_msix_want, BNXT_RE_MAX_MSIX);
1006 	num_msix_want = max_t(u32, num_msix_want, BNXT_RE_MIN_MSIX);
1007 
1008 	entry = rdev->nqr.msix_entries;
1009 
1010 	num_msix_got = en_dev->en_ops->bnxt_request_msix(en_dev, BNXT_ROCE_ULP,
1011 							 entry, num_msix_want);
1012 	if (num_msix_got < BNXT_RE_MIN_MSIX) {
1013 		rc = -EINVAL;
1014 		goto done;
1015 	}
1016 	if (num_msix_got != num_msix_want)
1017 		dev_warn(rdev_to_dev(rdev),
1018 			 "bnxt_request_msix: wanted %d vectors, got %d\n",
1019 			 num_msix_want, num_msix_got);
1020 
1021 	rdev->nqr.num_msix = num_msix_got;
1022 	return 0;
1023 done:
1024 	if (num_msix_got)
1025 		bnxt_re_free_msix(rdev);
1026 	return rc;
1027 }
1028 
__wait_for_ib_unregister(struct bnxt_re_dev * rdev,struct bnxt_re_en_dev_info * en_info)1029 static int  __wait_for_ib_unregister(struct bnxt_re_dev *rdev,
1030 				     struct bnxt_re_en_dev_info *en_info)
1031 {
1032 	u64 timeout = 0;
1033 	u32 cur_prod = 0, cur_cons = 0;
1034 	int retry = 0, rc = 0, ret = 0;
1035 
1036 	cur_prod = rdev->rcfw.cmdq.hwq.prod;
1037 	cur_cons = rdev->rcfw.cmdq.hwq.cons;
1038 	timeout = msecs_to_jiffies(BNXT_RE_RECOVERY_IB_UNINIT_WAIT_TIME_MS);
1039 	retry = BNXT_RE_RECOVERY_IB_UNINIT_WAIT_RETRY;
1040 	/* During module exit, increase timeout ten-fold to 100 mins to wait
1041 	 * as long as possible for ib_unregister() to complete
1042 	 */
1043 	if (rdev->mod_exit)
1044 		retry *= 10;
1045 	do {
1046 		/*
1047 		 * Since the caller of this function invokes with bnxt_re_mutex held,
1048 		 * release it to avoid holding a lock while in wait / sleep mode.
1049 		 */
1050 		mutex_unlock(&bnxt_re_mutex);
1051 		rc = wait_event_timeout(en_info->waitq,
1052 					en_info->ib_uninit_done,
1053 					timeout);
1054 		mutex_lock(&bnxt_re_mutex);
1055 
1056 		if (!bnxt_re_is_rdev_valid(rdev))
1057 			break;
1058 
1059 		if (rc)
1060 			break;
1061 
1062 		if (!RCFW_NO_FW_ACCESS(&rdev->rcfw)) {
1063 			/* No need to check for cmdq stall during module exit,
1064 			 * wait for ib unregister to complete
1065 			 */
1066 			if (!rdev->mod_exit)
1067 				ret = __check_cmdq_stall(&rdev->rcfw, &cur_prod, &cur_cons);
1068 			if (ret || en_info->ib_uninit_done)
1069 				break;
1070 		}
1071 	} while (retry--);
1072 
1073 	return rc;
1074 }
1075 
bnxt_re_handle_start(struct auxiliary_device * adev)1076 static int bnxt_re_handle_start(struct auxiliary_device *adev)
1077 {
1078 	struct bnxt_re_en_dev_info *en_info = auxiliary_get_drvdata(adev);
1079 	struct bnxt_re_dev *rdev = NULL;
1080 	struct ifnet *real_dev;
1081 	struct bnxt_en_dev *en_dev;
1082 	struct ifnet *netdev;
1083 	int rc = 0;
1084 
1085 	if (!en_info || !en_info->en_dev) {
1086 		pr_err("Start, bad en_info or en_dev\n");
1087 		return -EINVAL;
1088 	}
1089 	netdev = en_info->en_dev->net;
1090 	if (en_info->rdev) {
1091 		dev_info(rdev_to_dev(en_info->rdev),
1092 			 "%s: Device is already added adev %p rdev: %p\n",
1093 			 __func__, adev, en_info->rdev);
1094 		return 0;
1095 	}
1096 
1097 	en_dev = en_info->en_dev;
1098 	real_dev = rdma_vlan_dev_real_dev(netdev);
1099 	if (!real_dev)
1100 		real_dev = netdev;
1101 	rc = bnxt_re_add_device(&rdev, real_dev,
1102 				en_info->gsi_mode,
1103 				BNXT_RE_POST_RECOVERY_INIT,
1104 				en_info->num_msix_requested, adev);
1105 	if (rc) {
1106 		/* Add device failed. Unregister the device.
1107 		 * This has to be done explicitly as
1108 		 * bnxt_re_stop would not have unregistered
1109 		 */
1110 		rtnl_lock();
1111 		en_dev->en_ops->bnxt_unregister_device(en_dev, BNXT_ROCE_ULP);
1112 		rtnl_unlock();
1113 		mutex_lock(&bnxt_re_dev_lock);
1114 		gadd_dev_inprogress--;
1115 		mutex_unlock(&bnxt_re_dev_lock);
1116 		return rc;
1117 	}
1118 	rdev->adev = adev;
1119 	rtnl_lock();
1120 	bnxt_re_get_link_speed(rdev);
1121 	rtnl_unlock();
1122 	rc = bnxt_re_ib_init(rdev);
1123 	if (rc) {
1124 		dev_err(rdev_to_dev(rdev), "Failed ib_init\n");
1125 		return rc;
1126 	}
1127 	bnxt_re_ib_init_2(rdev);
1128 
1129 	return rc;
1130 }
1131 
bnxt_re_stop(void * handle)1132 static void bnxt_re_stop(void *handle)
1133 {
1134 	struct bnxt_re_en_dev_info *en_info = auxiliary_get_drvdata(handle);
1135 	struct ifnet *netdev;
1136 	struct bnxt_re_dev *rdev;
1137 	struct bnxt_en_dev *en_dev;
1138 	int rc = 0;
1139 
1140 	rtnl_unlock();
1141 	mutex_lock(&bnxt_re_mutex);
1142 	if (!en_info || !en_info->en_dev) {
1143 		pr_err("Stop, bad en_info or en_dev\n");
1144 		goto exit;
1145 	}
1146 	netdev = en_info->en_dev->net;
1147 	rdev = en_info->rdev;
1148 	if (!rdev)
1149 		goto exit;
1150 
1151 	if (!bnxt_re_is_rdev_valid(rdev))
1152 		goto exit;
1153 
1154 	/*
1155 	 * Check if fw has undergone reset or is in a fatal condition.
1156 	 * If so, set flags so that no further commands are sent down to FW
1157 	 */
1158 	en_dev = rdev->en_dev;
1159 	if (en_dev->en_state & BNXT_STATE_FW_FATAL_COND ||
1160 	    en_dev->en_state & BNXT_STATE_FW_RESET_DET) {
1161 		/* Set rcfw flag to control commands send to Bono */
1162 		set_bit(ERR_DEVICE_DETACHED, &rdev->rcfw.cmdq.flags);
1163 		/* Set bnxt_re flag to control commands send via L2 driver */
1164 		set_bit(BNXT_RE_FLAG_ERR_DEVICE_DETACHED, &rdev->flags);
1165 		wake_up_all(&rdev->rcfw.cmdq.waitq);
1166 	}
1167 
1168 	if (test_bit(BNXT_RE_FLAG_STOP_IN_PROGRESS, &rdev->flags))
1169 		goto exit;
1170 	set_bit(BNXT_RE_FLAG_STOP_IN_PROGRESS, &rdev->flags);
1171 
1172 	en_info->wqe_mode = rdev->chip_ctx->modes.wqe_mode;
1173 	en_info->gsi_mode = rdev->gsi_ctx.gsi_qp_mode;
1174 	en_info->num_msix_requested = rdev->num_msix_requested;
1175 	en_info->ib_uninit_done = false;
1176 
1177 	if (rdev->dbr_pacing)
1178 		bnxt_re_set_pacing_dev_state(rdev);
1179 
1180 	dev_info(rdev_to_dev(rdev), "%s: L2 driver notified to stop."
1181 		 "Attempting to stop and Dispatching event "
1182 		 "to inform the stack\n", __func__);
1183 	init_waitqueue_head(&en_info->waitq);
1184 	/* Schedule a work item to handle IB UNINIT for recovery */
1185 	bnxt_re_schedule_work(rdev, NETDEV_UNREGISTER,
1186 			      NULL, netdev, rdev->adev);
1187 	rc = __wait_for_ib_unregister(rdev, en_info);
1188 	if (!bnxt_re_is_rdev_valid(rdev))
1189 		goto exit;
1190 	if (!rc) {
1191 		dev_info(rdev_to_dev(rdev), "%s: Attempt to stop failed\n",
1192 			 __func__);
1193 		bnxt_re_detach_err_device(rdev);
1194 		goto exit;
1195 	}
1196 	bnxt_re_remove_device(rdev, BNXT_RE_PRE_RECOVERY_REMOVE, rdev->adev);
1197 exit:
1198 	mutex_unlock(&bnxt_re_mutex);
1199 	/* Take rtnl_lock before return, bnxt_re_stop is called with rtnl_lock */
1200 	rtnl_lock();
1201 
1202 	return;
1203 }
1204 
bnxt_re_start(void * handle)1205 static void bnxt_re_start(void *handle)
1206 {
1207 	rtnl_unlock();
1208 	mutex_lock(&bnxt_re_mutex);
1209 	if (bnxt_re_handle_start((struct auxiliary_device *)handle))
1210 		pr_err("Failed to start RoCE device");
1211 	mutex_unlock(&bnxt_re_mutex);
1212 	/* Take rtnl_lock before return, bnxt_re_start is called with rtnl_lock */
1213 	rtnl_lock();
1214 	return;
1215 }
1216 
bnxt_re_shutdown(void * p)1217 static void bnxt_re_shutdown(void *p)
1218 {
1219 	struct bnxt_re_en_dev_info *en_info = auxiliary_get_drvdata(p);
1220 	struct bnxt_re_dev *rdev;
1221 
1222 	if (!en_info) {
1223 		pr_err("Shutdown, bad en_info\n");
1224 		return;
1225 	}
1226 	rtnl_unlock();
1227 	mutex_lock(&bnxt_re_mutex);
1228 	rdev = en_info->rdev;
1229 	if (!rdev || !bnxt_re_is_rdev_valid(rdev))
1230 		goto exit;
1231 
1232 	/* rtnl_lock held by L2 before coming here */
1233 	bnxt_re_stopqps_and_ib_uninit(rdev);
1234 	bnxt_re_remove_device(rdev, BNXT_RE_COMPLETE_REMOVE, rdev->adev);
1235 exit:
1236 	mutex_unlock(&bnxt_re_mutex);
1237 	rtnl_lock();
1238 	return;
1239 }
1240 
bnxt_re_stop_irq(void * handle)1241 static void bnxt_re_stop_irq(void *handle)
1242 {
1243 	struct bnxt_re_en_dev_info *en_info = auxiliary_get_drvdata(handle);
1244 	struct bnxt_qplib_rcfw *rcfw = NULL;
1245 	struct bnxt_re_dev *rdev;
1246 	struct bnxt_qplib_nq *nq;
1247 	int indx;
1248 
1249 	if (!en_info) {
1250 		pr_err("Stop irq, bad en_info\n");
1251 		return;
1252 	}
1253 	rdev = en_info->rdev;
1254 
1255 	if (!rdev)
1256 		return;
1257 
1258 	rcfw = &rdev->rcfw;
1259 	for (indx = 0; indx < rdev->nqr.max_init; indx++) {
1260 		nq = &rdev->nqr.nq[indx];
1261 		mutex_lock(&nq->lock);
1262 		bnxt_qplib_nq_stop_irq(nq, false);
1263 		mutex_unlock(&nq->lock);
1264 	}
1265 
1266 	if (test_bit(BNXT_RE_FLAG_ALLOC_RCFW, &rdev->flags))
1267 		bnxt_qplib_rcfw_stop_irq(rcfw, false);
1268 }
1269 
bnxt_re_start_irq(void * handle,struct bnxt_msix_entry * ent)1270 static void bnxt_re_start_irq(void *handle, struct bnxt_msix_entry *ent)
1271 {
1272 	struct bnxt_re_en_dev_info *en_info = auxiliary_get_drvdata(handle);
1273 	struct bnxt_msix_entry *msix_ent = NULL;
1274 	struct bnxt_qplib_rcfw *rcfw = NULL;
1275 	struct bnxt_re_dev *rdev;
1276 	struct bnxt_qplib_nq *nq;
1277 	int indx, rc, vec;
1278 
1279 	if (!en_info) {
1280 		pr_err("Start irq, bad en_info\n");
1281 		return;
1282 	}
1283 	rdev = en_info->rdev;
1284 	if (!rdev)
1285 		return;
1286 	if (test_bit(BNXT_RE_FLAG_ERR_DEVICE_DETACHED, &rdev->flags))
1287 		return;
1288 	msix_ent = rdev->nqr.msix_entries;
1289 	rcfw = &rdev->rcfw;
1290 
1291 	if (!ent) {
1292 		/* Not setting the f/w timeout bit in rcfw.
1293 		 * During the driver unload the first command
1294 		 * to f/w will timeout and that will set the
1295 		 * timeout bit.
1296 		 */
1297 		dev_err(rdev_to_dev(rdev), "Failed to re-start IRQs\n");
1298 		return;
1299 	}
1300 
1301 	/* Vectors may change after restart, so update with new vectors
1302 	 * in device structure.
1303 	 */
1304 	for (indx = 0; indx < rdev->nqr.num_msix; indx++)
1305 		rdev->nqr.msix_entries[indx].vector = ent[indx].vector;
1306 
1307 	if (test_bit(BNXT_RE_FLAG_ALLOC_RCFW, &rdev->flags)) {
1308 		rc = bnxt_qplib_rcfw_start_irq(rcfw, msix_ent[BNXT_RE_AEQ_IDX].vector,
1309 					       false);
1310 		if (rc) {
1311 			dev_warn(rdev_to_dev(rdev),
1312 				 "Failed to reinit CREQ\n");
1313 			return;
1314 		}
1315 	}
1316 	for (indx = 0 ; indx < rdev->nqr.max_init; indx++) {
1317 		nq = &rdev->nqr.nq[indx];
1318 		vec = indx + 1;
1319 		rc = bnxt_qplib_nq_start_irq(nq, indx, msix_ent[vec].vector,
1320 					     false);
1321 		if (rc) {
1322 			dev_warn(rdev_to_dev(rdev),
1323 				 "Failed to reinit NQ index %d\n", indx);
1324 			return;
1325 		}
1326 	}
1327 }
1328 
1329 /*
1330  * Except for ulp_async_notifier, the remaining ulp_ops
1331  * below are called with rtnl_lock held
1332  */
1333 static struct bnxt_ulp_ops bnxt_re_ulp_ops = {
1334 	.ulp_async_notifier = bnxt_re_async_notifier,
1335 	.ulp_stop = bnxt_re_stop,
1336 	.ulp_start = bnxt_re_start,
1337 	.ulp_shutdown = bnxt_re_shutdown,
1338 	.ulp_irq_stop = bnxt_re_stop_irq,
1339 	.ulp_irq_restart = bnxt_re_start_irq,
1340 };
1341 
bnxt_re_netevent(unsigned long event)1342 static inline const char *bnxt_re_netevent(unsigned long event)
1343 {
1344 	BNXT_RE_NETDEV_EVENT(event, NETDEV_UP);
1345 	BNXT_RE_NETDEV_EVENT(event, NETDEV_DOWN);
1346 	BNXT_RE_NETDEV_EVENT(event, NETDEV_CHANGE);
1347 	BNXT_RE_NETDEV_EVENT(event, NETDEV_REGISTER);
1348 	BNXT_RE_NETDEV_EVENT(event, NETDEV_UNREGISTER);
1349 	BNXT_RE_NETDEV_EVENT(event, NETDEV_CHANGEADDR);
1350 	return "Unknown";
1351 }
1352 
1353 /* RoCE -> Net driver */
1354 
1355 /* Driver registration routines used to let the networking driver (bnxt_en)
1356  * to know that the RoCE driver is now installed */
bnxt_re_unregister_netdev(struct bnxt_re_dev * rdev)1357 static void bnxt_re_unregister_netdev(struct bnxt_re_dev *rdev)
1358 {
1359 	struct bnxt_en_dev *en_dev = rdev->en_dev;
1360 	int rc;
1361 
1362 	rtnl_lock();
1363 	rc = en_dev->en_ops->bnxt_unregister_device(rdev->en_dev,
1364 						    BNXT_ROCE_ULP);
1365 	rtnl_unlock();
1366 	if (rc)
1367 		dev_err(rdev_to_dev(rdev), "netdev %p unregister failed! rc = 0x%x",
1368 			rdev->en_dev->net, rc);
1369 
1370 	clear_bit(BNXT_RE_FLAG_NETDEV_REGISTERED, &rdev->flags);
1371 }
1372 
bnxt_re_register_netdev(struct bnxt_re_dev * rdev)1373 static int bnxt_re_register_netdev(struct bnxt_re_dev *rdev)
1374 {
1375 	struct bnxt_en_dev *en_dev = rdev->en_dev;
1376 	int rc = 0;
1377 
1378 	rtnl_lock();
1379 	rc = en_dev->en_ops->bnxt_register_device(en_dev,
1380 						  BNXT_ROCE_ULP,
1381 						  &bnxt_re_ulp_ops,
1382 						  rdev->adev);
1383 	rtnl_unlock();
1384 	if (rc) {
1385 		dev_err(rdev_to_dev(rdev), "netdev %p register failed! rc = 0x%x",
1386 			rdev->netdev, rc);
1387 		return rc;
1388 	}
1389 
1390 	return rc;
1391 }
1392 
bnxt_re_set_db_offset(struct bnxt_re_dev * rdev)1393 static void bnxt_re_set_db_offset(struct bnxt_re_dev *rdev)
1394 {
1395 	struct bnxt_qplib_chip_ctx *cctx;
1396 	struct bnxt_en_dev *en_dev;
1397 	struct bnxt_qplib_res *res;
1398 	u32 l2db_len = 0;
1399 	u32 offset = 0;
1400 	u32 barlen;
1401 	int rc;
1402 
1403 	res = &rdev->qplib_res;
1404 	en_dev = rdev->en_dev;
1405 	cctx = rdev->chip_ctx;
1406 
1407 	/* Issue qcfg */
1408 	rc = bnxt_re_hwrm_qcfg(rdev, &l2db_len, &offset);
1409 	if (rc)
1410 		dev_info(rdev_to_dev(rdev),
1411 			 "Couldn't get DB bar size, Low latency framework is disabled\n");
1412 	/* set register offsets for both UC and WC */
1413 	if (_is_chip_p7(cctx)) {
1414 		res->dpi_tbl.ucreg.offset = en_dev->l2_db_offset;
1415 		res->dpi_tbl.wcreg.offset = en_dev->l2_db_size;
1416 	} else {
1417 		res->dpi_tbl.ucreg.offset = res->is_vf ? BNXT_QPLIB_DBR_VF_DB_OFFSET :
1418 							 BNXT_QPLIB_DBR_PF_DB_OFFSET;
1419 		res->dpi_tbl.wcreg.offset = res->dpi_tbl.ucreg.offset;
1420 	}
1421 
1422 	/* If WC mapping is disabled by L2 driver then en_dev->l2_db_size
1423 	 * is equal to the DB-Bar actual size. This indicates that L2
1424 	 * is mapping entire bar as UC-. RoCE driver can't enable WC mapping
1425 	 * in such cases and DB-push will be disabled.
1426 	 */
1427 	barlen = pci_resource_len(res->pdev, RCFW_DBR_PCI_BAR_REGION);
1428 	if (cctx->modes.db_push && l2db_len && en_dev->l2_db_size != barlen) {
1429 		res->dpi_tbl.wcreg.offset = en_dev->l2_db_size;
1430 		dev_info(rdev_to_dev(rdev),
1431 			 "Low latency framework is enabled\n");
1432 	}
1433 
1434 	return;
1435 }
1436 
bnxt_re_set_drv_mode(struct bnxt_re_dev * rdev)1437 static void bnxt_re_set_drv_mode(struct bnxt_re_dev *rdev)
1438 {
1439 	struct bnxt_qplib_chip_ctx *cctx;
1440 	struct bnxt_en_dev *en_dev;
1441 
1442 	en_dev = rdev->en_dev;
1443 	cctx = rdev->chip_ctx;
1444 	cctx->modes.wqe_mode = _is_chip_p7(rdev->chip_ctx) ?
1445 			BNXT_QPLIB_WQE_MODE_VARIABLE : BNXT_QPLIB_WQE_MODE_STATIC;
1446 	cctx->modes.te_bypass = false;
1447 	if (bnxt_re_hwrm_qcaps(rdev))
1448 		dev_err(rdev_to_dev(rdev),
1449 			"Failed to query hwrm qcaps\n");
1450 	 /*
1451 	  * TODO: Need a better mechanism for spreading of the
1452 	  * 512 extended PPP pages in the presence of VF and
1453 	  * NPAR, until then not enabling push
1454 	  */
1455 	if (_is_chip_p7(rdev->chip_ctx) && cctx->modes.db_push) {
1456 		if (rdev->is_virtfn || BNXT_EN_NPAR(en_dev))
1457 			cctx->modes.db_push = false;
1458 	}
1459 
1460 	rdev->roce_mode = en_dev->flags & BNXT_EN_FLAG_ROCE_CAP;
1461 	dev_dbg(rdev_to_dev(rdev),
1462 		"RoCE is supported on the device - caps:0x%x",
1463 		rdev->roce_mode);
1464 	if (!_is_chip_gen_p5_p7(rdev->chip_ctx))
1465 		rdev->roce_mode = BNXT_RE_FLAG_ROCEV2_CAP;
1466 	cctx->hw_stats_size = en_dev->hw_ring_stats_size;
1467 }
1468 
bnxt_re_destroy_chip_ctx(struct bnxt_re_dev * rdev)1469 static void bnxt_re_destroy_chip_ctx(struct bnxt_re_dev *rdev)
1470 {
1471 	struct bnxt_qplib_chip_ctx *chip_ctx;
1472 	struct bnxt_qplib_res *res;
1473 
1474 	if (!rdev->chip_ctx)
1475 		return;
1476 
1477 	res = &rdev->qplib_res;
1478 	bnxt_qplib_unmap_db_bar(res);
1479 
1480 	kfree(res->hctx);
1481 	res->rcfw = NULL;
1482 	kfree(rdev->dev_attr);
1483 	rdev->dev_attr = NULL;
1484 
1485 	chip_ctx = rdev->chip_ctx;
1486 	rdev->chip_ctx = NULL;
1487 	res->cctx = NULL;
1488 	res->hctx = NULL;
1489 	res->pdev = NULL;
1490 	res->netdev = NULL;
1491 	kfree(chip_ctx);
1492 }
1493 
bnxt_re_setup_chip_ctx(struct bnxt_re_dev * rdev)1494 static int bnxt_re_setup_chip_ctx(struct bnxt_re_dev *rdev)
1495 {
1496 	struct bnxt_qplib_chip_ctx *chip_ctx;
1497 	struct bnxt_en_dev *en_dev;
1498 	int rc;
1499 
1500 	en_dev = rdev->en_dev;
1501 	/* Supply pci device to qplib */
1502 	rdev->qplib_res.pdev = en_dev->pdev;
1503 	rdev->qplib_res.netdev = rdev->netdev;
1504 	rdev->qplib_res.en_dev = en_dev;
1505 
1506 	chip_ctx = kzalloc(sizeof(*chip_ctx), GFP_KERNEL);
1507 	if (!chip_ctx)
1508 		return -ENOMEM;
1509 	rdev->chip_ctx = chip_ctx;
1510 	rdev->qplib_res.cctx = chip_ctx;
1511 	rc = bnxt_re_query_hwrm_intf_version(rdev);
1512 	if (rc)
1513 		goto fail;
1514 	rdev->dev_attr = kzalloc(sizeof(*rdev->dev_attr), GFP_KERNEL);
1515 	if (!rdev->dev_attr) {
1516 		rc = -ENOMEM;
1517 		goto fail;
1518 	}
1519 	rdev->qplib_res.dattr = rdev->dev_attr;
1520 	rdev->qplib_res.rcfw = &rdev->rcfw;
1521 	rdev->qplib_res.is_vf = rdev->is_virtfn;
1522 
1523 	rdev->qplib_res.hctx = kzalloc(sizeof(*rdev->qplib_res.hctx),
1524 				       GFP_KERNEL);
1525 	if (!rdev->qplib_res.hctx) {
1526 		rc = -ENOMEM;
1527 		goto fail;
1528 	}
1529 	bnxt_re_set_drv_mode(rdev);
1530 
1531 	bnxt_re_set_db_offset(rdev);
1532 	rc = bnxt_qplib_map_db_bar(&rdev->qplib_res);
1533 	if (rc)
1534 		goto fail;
1535 
1536 	rc = bnxt_qplib_enable_atomic_ops_to_root(en_dev->pdev);
1537 	if (rc)
1538 		dev_dbg(rdev_to_dev(rdev),
1539 			"platform doesn't support global atomics");
1540 
1541 	return 0;
1542 fail:
1543 	kfree(rdev->chip_ctx);
1544 	rdev->chip_ctx = NULL;
1545 
1546 	kfree(rdev->dev_attr);
1547 	rdev->dev_attr = NULL;
1548 
1549 	kfree(rdev->qplib_res.hctx);
1550 	rdev->qplib_res.hctx = NULL;
1551 	return rc;
1552 }
1553 
bnxt_re_get_rtype(struct bnxt_re_dev * rdev)1554 static u16 bnxt_re_get_rtype(struct bnxt_re_dev *rdev) {
1555 	return _is_chip_gen_p5_p7(rdev->chip_ctx) ?
1556 	       HWRM_RING_ALLOC_INPUT_RING_TYPE_NQ :
1557 	       HWRM_RING_ALLOC_INPUT_RING_TYPE_ROCE_CMPL;
1558 }
1559 
bnxt_re_net_ring_free(struct bnxt_re_dev * rdev,u16 fw_ring_id)1560 static int bnxt_re_net_ring_free(struct bnxt_re_dev *rdev, u16 fw_ring_id)
1561 {
1562 	int rc = -EINVAL;
1563 	struct hwrm_ring_free_input req = {0};
1564 	struct hwrm_ring_free_output resp;
1565 	struct bnxt_en_dev *en_dev = rdev->en_dev;
1566 	struct bnxt_fw_msg fw_msg;
1567 
1568 	if (!en_dev)
1569 		return rc;
1570 
1571 	/* To avoid unnecessary error messages during recovery.
1572 	 * HW is anyway in error state. So dont send down the command */
1573 	if (test_bit(BNXT_RE_FLAG_ERR_DEVICE_DETACHED, &rdev->flags))
1574 		return 0;
1575 
1576 	/* allocation had failed, no need to issue hwrm */
1577 	if (fw_ring_id == 0xffff)
1578 		return 0;
1579 
1580 	memset(&fw_msg, 0, sizeof(fw_msg));
1581 
1582 	bnxt_re_init_hwrm_hdr(rdev, (void *)&req, HWRM_RING_FREE, -1, -1);
1583 	req.ring_type = bnxt_re_get_rtype(rdev);
1584 	req.ring_id = cpu_to_le16(fw_ring_id);
1585 	bnxt_re_fill_fw_msg(&fw_msg, (void *)&req, sizeof(req), (void *)&resp,
1586 			    sizeof(resp), DFLT_HWRM_CMD_TIMEOUT);
1587 	rc = en_dev->en_ops->bnxt_send_fw_msg(en_dev, BNXT_ROCE_ULP, &fw_msg);
1588 	if (rc) {
1589 		dev_err(rdev_to_dev(rdev),
1590 			"Failed to free HW ring with rc = 0x%x", rc);
1591 		return rc;
1592 	}
1593 	dev_dbg(rdev_to_dev(rdev), "HW ring freed with id = 0x%x\n",
1594 		fw_ring_id);
1595 
1596 	return rc;
1597 }
1598 
bnxt_re_net_ring_alloc(struct bnxt_re_dev * rdev,struct bnxt_re_ring_attr * ring_attr,u16 * fw_ring_id)1599 static int bnxt_re_net_ring_alloc(struct bnxt_re_dev *rdev,
1600 				  struct bnxt_re_ring_attr *ring_attr,
1601 				  u16 *fw_ring_id)
1602 {
1603 	int rc = -EINVAL;
1604 	struct hwrm_ring_alloc_input req = {0};
1605 	struct hwrm_ring_alloc_output resp;
1606 	struct bnxt_en_dev *en_dev = rdev->en_dev;
1607 	struct bnxt_fw_msg fw_msg;
1608 
1609 	if (!en_dev)
1610 		return rc;
1611 
1612 	memset(&fw_msg, 0, sizeof(fw_msg));
1613 	bnxt_re_init_hwrm_hdr(rdev, (void *)&req, HWRM_RING_ALLOC, -1, -1);
1614 	req.flags = cpu_to_le16(ring_attr->flags);
1615 	req.enables = 0;
1616 	req.page_tbl_addr =  cpu_to_le64(ring_attr->dma_arr[0]);
1617 	if (ring_attr->pages > 1) {
1618 		/* Page size is in log2 units */
1619 		req.page_size = BNXT_PAGE_SHIFT;
1620 		req.page_tbl_depth = 1;
1621 	} else {
1622 		req.page_size = 4;
1623 		req.page_tbl_depth = 0;
1624 	}
1625 
1626 	req.fbo = 0;
1627 	/* Association of ring index with doorbell index and MSIX number */
1628 	req.logical_id = cpu_to_le16(ring_attr->lrid);
1629 	req.length = cpu_to_le32(ring_attr->depth + 1);
1630 	req.ring_type = ring_attr->type;
1631 	req.int_mode = ring_attr->mode;
1632 	bnxt_re_fill_fw_msg(&fw_msg, (void *)&req, sizeof(req), (void *)&resp,
1633 			    sizeof(resp), DFLT_HWRM_CMD_TIMEOUT);
1634 	rc = en_dev->en_ops->bnxt_send_fw_msg(en_dev, BNXT_ROCE_ULP, &fw_msg);
1635 	if (rc) {
1636 		dev_err(rdev_to_dev(rdev),
1637 			"Failed to allocate HW ring with rc = 0x%x", rc);
1638 		return rc;
1639 	}
1640 	*fw_ring_id = le16_to_cpu(resp.ring_id);
1641 	dev_dbg(rdev_to_dev(rdev),
1642 		"HW ring allocated with id = 0x%x at slot 0x%x",
1643 		resp.ring_id, ring_attr->lrid);
1644 
1645 	return rc;
1646 }
1647 
bnxt_re_net_stats_ctx_free(struct bnxt_re_dev * rdev,u32 fw_stats_ctx_id,u16 tid)1648 static int bnxt_re_net_stats_ctx_free(struct bnxt_re_dev *rdev,
1649 				      u32 fw_stats_ctx_id, u16 tid)
1650 {
1651 	struct bnxt_en_dev *en_dev = rdev->en_dev;
1652 	struct hwrm_stat_ctx_free_input req = {0};
1653 	struct hwrm_stat_ctx_free_output resp;
1654 	struct bnxt_fw_msg fw_msg;
1655 	int rc = -EINVAL;
1656 
1657 	if (!en_dev)
1658 		return rc;
1659 
1660 	/* To avoid unnecessary error messages during recovery.
1661 	 * HW is anyway in error state. So dont send down the command */
1662 	if (test_bit(BNXT_RE_FLAG_ERR_DEVICE_DETACHED, &rdev->flags))
1663 		return 0;
1664 	memset(&fw_msg, 0, sizeof(fw_msg));
1665 	bnxt_re_init_hwrm_hdr(rdev, (void *)&req, HWRM_STAT_CTX_FREE, -1, tid);
1666 	req.stat_ctx_id = cpu_to_le32(fw_stats_ctx_id);
1667 	bnxt_re_fill_fw_msg(&fw_msg, (void *)&req, sizeof(req), (void *)&resp,
1668 			    sizeof(resp), DFLT_HWRM_CMD_TIMEOUT);
1669 	rc = en_dev->en_ops->bnxt_send_fw_msg(en_dev, BNXT_ROCE_ULP, &fw_msg);
1670 	if (rc) {
1671 		dev_err(rdev_to_dev(rdev),
1672 			"Failed to free HW stats ctx with rc = 0x%x", rc);
1673 		return rc;
1674 	}
1675 	dev_dbg(rdev_to_dev(rdev),
1676 		"HW stats ctx freed with id = 0x%x", fw_stats_ctx_id);
1677 
1678 	return rc;
1679 }
1680 
bnxt_re_net_stats_ctx_alloc(struct bnxt_re_dev * rdev,u16 tid)1681 static int bnxt_re_net_stats_ctx_alloc(struct bnxt_re_dev *rdev, u16 tid)
1682 {
1683 	struct hwrm_stat_ctx_alloc_output resp = {};
1684 	struct hwrm_stat_ctx_alloc_input req = {};
1685 	struct bnxt_en_dev *en_dev = rdev->en_dev;
1686 	struct bnxt_qplib_stats *stat;
1687 	struct bnxt_qplib_ctx *hctx;
1688 	struct bnxt_fw_msg fw_msg;
1689 	int rc = 0;
1690 
1691 	hctx = rdev->qplib_res.hctx;
1692 	stat = (tid == 0xffff) ? &hctx->stats : &hctx->stats2;
1693 	stat->fw_id = INVALID_STATS_CTX_ID;
1694 
1695 	if (!en_dev)
1696 		return -EINVAL;
1697 
1698 	memset(&fw_msg, 0, sizeof(fw_msg));
1699 	bnxt_re_init_hwrm_hdr(rdev, (void *)&req,
1700 			      HWRM_STAT_CTX_ALLOC, -1, tid);
1701 	req.update_period_ms = cpu_to_le32(1000);
1702 	req.stats_dma_length = rdev->chip_ctx->hw_stats_size;
1703 	req.stats_dma_addr = cpu_to_le64(stat->dma_map);
1704 	req.stat_ctx_flags = HWRM_STAT_CTX_ALLOC_INPUT_STAT_CTX_FLAGS_ROCE;
1705 	bnxt_re_fill_fw_msg(&fw_msg, (void *)&req, sizeof(req), (void *)&resp,
1706 			    sizeof(resp), DFLT_HWRM_CMD_TIMEOUT);
1707 	rc = en_dev->en_ops->bnxt_send_fw_msg(en_dev, BNXT_ROCE_ULP, &fw_msg);
1708 	if (rc) {
1709 		dev_err(rdev_to_dev(rdev),
1710 			"Failed to allocate HW stats ctx, rc = 0x%x", rc);
1711 		return rc;
1712 	}
1713 	stat->fw_id = le32_to_cpu(resp.stat_ctx_id);
1714 	dev_dbg(rdev_to_dev(rdev), "HW stats ctx allocated with id = 0x%x",
1715 		stat->fw_id);
1716 
1717 	return rc;
1718 }
1719 
bnxt_re_net_unregister_async_event(struct bnxt_re_dev * rdev)1720 static void bnxt_re_net_unregister_async_event(struct bnxt_re_dev *rdev)
1721 {
1722 	const struct bnxt_en_ops *en_ops;
1723 
1724 	if (rdev->is_virtfn ||
1725 	    test_bit(BNXT_RE_FLAG_ERR_DEVICE_DETACHED, &rdev->flags))
1726 		return;
1727 
1728 	memset(rdev->event_bitmap, 0, sizeof(rdev->event_bitmap));
1729 	en_ops = rdev->en_dev->en_ops;
1730 	if (en_ops->bnxt_register_fw_async_events
1731 	    (rdev->en_dev, BNXT_ROCE_ULP,
1732 	     (unsigned long *)rdev->event_bitmap,
1733 	      HWRM_ASYNC_EVENT_CMPL_EVENT_ID_DOORBELL_PACING_NQ_UPDATE))
1734 		dev_err(rdev_to_dev(rdev),
1735 			"Failed to unregister async event");
1736 }
1737 
bnxt_re_net_register_async_event(struct bnxt_re_dev * rdev)1738 static void bnxt_re_net_register_async_event(struct bnxt_re_dev *rdev)
1739 {
1740 	const struct bnxt_en_ops *en_ops;
1741 
1742 	if (rdev->is_virtfn)
1743 		return;
1744 
1745 	rdev->event_bitmap[0] |=
1746 		BIT(HWRM_ASYNC_EVENT_CMPL_EVENT_ID_DCB_CONFIG_CHANGE) |
1747 		BIT(HWRM_ASYNC_EVENT_CMPL_EVENT_ID_RESET_NOTIFY);
1748 
1749 	rdev->event_bitmap[2] |=
1750 	   BIT(HWRM_ASYNC_EVENT_CMPL_EVENT_ID_ERROR_REPORT - 64);
1751 	rdev->event_bitmap[2] |=
1752 		BIT(HWRM_ASYNC_EVENT_CMPL_EVENT_ID_DOORBELL_PACING_THRESHOLD - 64) |
1753 		BIT(HWRM_ASYNC_EVENT_CMPL_EVENT_ID_DOORBELL_PACING_NQ_UPDATE - 64);
1754 	en_ops = rdev->en_dev->en_ops;
1755 	if (en_ops->bnxt_register_fw_async_events
1756 	    (rdev->en_dev, BNXT_ROCE_ULP,
1757 	     (unsigned long *)rdev->event_bitmap,
1758 	      HWRM_ASYNC_EVENT_CMPL_EVENT_ID_DOORBELL_PACING_NQ_UPDATE))
1759 		dev_err(rdev_to_dev(rdev),
1760 			"Failed to reg Async event");
1761 }
1762 
bnxt_re_query_hwrm_intf_version(struct bnxt_re_dev * rdev)1763 static int bnxt_re_query_hwrm_intf_version(struct bnxt_re_dev *rdev)
1764 {
1765 	struct bnxt_en_dev *en_dev = rdev->en_dev;
1766 	struct hwrm_ver_get_output resp = {0};
1767 	struct hwrm_ver_get_input req = {0};
1768 	struct bnxt_qplib_chip_ctx *cctx;
1769 	struct bnxt_fw_msg fw_msg;
1770 	int rc = 0;
1771 
1772 	memset(&fw_msg, 0, sizeof(fw_msg));
1773 	bnxt_re_init_hwrm_hdr(rdev, (void *)&req,
1774 			      HWRM_VER_GET, -1, -1);
1775 	req.hwrm_intf_maj = HWRM_VERSION_MAJOR;
1776 	req.hwrm_intf_min = HWRM_VERSION_MINOR;
1777 	req.hwrm_intf_upd = HWRM_VERSION_UPDATE;
1778 	bnxt_re_fill_fw_msg(&fw_msg, (void *)&req, sizeof(req), (void *)&resp,
1779 			    sizeof(resp), DFLT_HWRM_CMD_TIMEOUT);
1780 	rc = en_dev->en_ops->bnxt_send_fw_msg(en_dev, BNXT_ROCE_ULP, &fw_msg);
1781 	if (rc) {
1782 		dev_err(rdev_to_dev(rdev),
1783 			"Failed to query HW version, rc = 0x%x", rc);
1784 		return rc;
1785 	}
1786 	cctx = rdev->chip_ctx;
1787 	cctx->hwrm_intf_ver = (u64) le16_to_cpu(resp.hwrm_intf_major) << 48 |
1788 			      (u64) le16_to_cpu(resp.hwrm_intf_minor) << 32 |
1789 			      (u64) le16_to_cpu(resp.hwrm_intf_build) << 16 |
1790 				    le16_to_cpu(resp.hwrm_intf_patch);
1791 
1792 	cctx->hwrm_cmd_max_timeout = le16_to_cpu(resp.max_req_timeout);
1793 
1794 	if (!cctx->hwrm_cmd_max_timeout)
1795 		cctx->hwrm_cmd_max_timeout = RCFW_FW_STALL_MAX_TIMEOUT;
1796 
1797 	cctx->chip_num = le16_to_cpu(resp.chip_num);
1798 	cctx->chip_rev = resp.chip_rev;
1799 	cctx->chip_metal = resp.chip_metal;
1800 	return 0;
1801 }
1802 
1803 /* Query device config using common hwrm */
bnxt_re_hwrm_qcfg(struct bnxt_re_dev * rdev,u32 * db_len,u32 * offset)1804 static int bnxt_re_hwrm_qcfg(struct bnxt_re_dev *rdev, u32 *db_len,
1805 			     u32 *offset)
1806 {
1807 	struct bnxt_en_dev *en_dev = rdev->en_dev;
1808 	struct hwrm_func_qcfg_output resp = {0};
1809 	struct hwrm_func_qcfg_input req = {0};
1810 	struct bnxt_fw_msg fw_msg;
1811 	int rc;
1812 
1813 	memset(&fw_msg, 0, sizeof(fw_msg));
1814 	bnxt_re_init_hwrm_hdr(rdev, (void *)&req,
1815 			      HWRM_FUNC_QCFG, -1, -1);
1816 	req.fid = cpu_to_le16(0xffff);
1817 	bnxt_re_fill_fw_msg(&fw_msg, (void *)&req, sizeof(req), (void *)&resp,
1818 			    sizeof(resp), DFLT_HWRM_CMD_TIMEOUT);
1819 	rc = en_dev->en_ops->bnxt_send_fw_msg(en_dev, BNXT_ROCE_ULP, &fw_msg);
1820 	if (rc) {
1821 		dev_err(rdev_to_dev(rdev),
1822 			"Failed to query config, rc = %#x", rc);
1823 		return rc;
1824 	}
1825 
1826 	*db_len = PAGE_ALIGN(le16_to_cpu(resp.l2_doorbell_bar_size_kb) * 1024);
1827 	*offset = PAGE_ALIGN(le16_to_cpu(resp.legacy_l2_db_size_kb) * 1024);
1828 	return 0;
1829 }
1830 
1831 /* Query function capabilities using common hwrm */
bnxt_re_hwrm_qcaps(struct bnxt_re_dev * rdev)1832 int bnxt_re_hwrm_qcaps(struct bnxt_re_dev *rdev)
1833 {
1834 	struct bnxt_en_dev *en_dev = rdev->en_dev;
1835 	struct hwrm_func_qcaps_output resp = {0};
1836 	struct hwrm_func_qcaps_input req = {0};
1837 	struct bnxt_qplib_chip_ctx *cctx;
1838 	struct bnxt_fw_msg fw_msg;
1839 	u8 push_enable = false;
1840 	int rc;
1841 
1842 	cctx = rdev->chip_ctx;
1843 	memset(&fw_msg, 0, sizeof(fw_msg));
1844 	bnxt_re_init_hwrm_hdr(rdev, (void *)&req,
1845 			      HWRM_FUNC_QCAPS, -1, -1);
1846 	req.fid = cpu_to_le16(0xffff);
1847 	bnxt_re_fill_fw_msg(&fw_msg, (void *)&req, sizeof(req), (void *)&resp,
1848 			    sizeof(resp), DFLT_HWRM_CMD_TIMEOUT);
1849 	rc = en_dev->en_ops->bnxt_send_fw_msg(en_dev, BNXT_ROCE_ULP, &fw_msg);
1850 	if (rc) {
1851 		dev_err(rdev_to_dev(rdev),
1852 			"Failed to query capabilities, rc = %#x", rc);
1853 		return rc;
1854 	}
1855 	if (_is_chip_p7(rdev->chip_ctx))
1856 		push_enable =
1857 			(resp.flags_ext &
1858 			 HWRM_FUNC_QCAPS_OUTPUT_FLAGS_EXT_PPP_PUSH_MODE_SUPPORTED) ?
1859 			 true : false;
1860 	else
1861 		push_enable =
1862 			(resp.flags & HWRM_FUNC_QCAPS_OUTPUT_FLAGS_WCB_PUSH_MODE) ?
1863 			 true : false;
1864 	cctx->modes.db_push = push_enable;
1865 
1866 	cctx->modes.dbr_pacing =
1867 		resp.flags_ext & HWRM_FUNC_QCAPS_OUTPUT_FLAGS_EXT_DBR_PACING_SUPPORTED ?
1868 			true : false;
1869 	cctx->modes.dbr_pacing_ext =
1870 		resp.flags_ext2 &
1871 			HWRM_FUNC_QCAPS_OUTPUT_FLAGS_EXT2_DBR_PACING_EXT_SUPPORTED ?
1872 			true : false;
1873 	cctx->modes.dbr_drop_recov =
1874 		(resp.flags_ext2 &
1875 		 HWRM_FUNC_QCAPS_OUTPUT_FLAGS_EXT2_SW_DBR_DROP_RECOVERY_SUPPORTED) ?
1876 			true : false;
1877 	cctx->modes.dbr_pacing_v0 =
1878 		(resp.flags_ext2 &
1879 		 HWRM_FUNC_QCAPS_OUTPUT_FLAGS_EXT2_DBR_PACING_V0_SUPPORTED) ?
1880 			true : false;
1881 	dev_dbg(rdev_to_dev(rdev),
1882 		"%s: cctx->modes.dbr_pacing = %d cctx->modes.dbr_pacing_ext = %d, dbr_drop_recov %d\n",
1883 		__func__, cctx->modes.dbr_pacing, cctx->modes.dbr_pacing_ext, cctx->modes.dbr_drop_recov);
1884 
1885 	return 0;
1886 }
1887 
bnxt_re_hwrm_dbr_pacing_qcfg(struct bnxt_re_dev * rdev)1888 static int bnxt_re_hwrm_dbr_pacing_qcfg(struct bnxt_re_dev *rdev)
1889 {
1890 	struct bnxt_qplib_db_pacing_data *pacing_data = rdev->qplib_res.pacing_data;
1891 	struct hwrm_func_dbr_pacing_qcfg_output resp = {0};
1892 	struct hwrm_func_dbr_pacing_qcfg_input req = {0};
1893 	struct bnxt_en_dev *en_dev = rdev->en_dev;
1894 	struct bnxt_qplib_chip_ctx *cctx;
1895 	struct bnxt_fw_msg fw_msg;
1896 	u32 primary_nq_id;
1897 	int rc;
1898 
1899 	cctx = rdev->chip_ctx;
1900 	memset(&fw_msg, 0, sizeof(fw_msg));
1901 	bnxt_re_init_hwrm_hdr(rdev, (void *)&req,
1902 			      HWRM_FUNC_DBR_PACING_QCFG, -1, -1);
1903 	bnxt_re_fill_fw_msg(&fw_msg, (void *)&req, sizeof(req), (void *)&resp,
1904 			    sizeof(resp), BNXT_RE_HWRM_CMD_TIMEOUT(rdev));
1905 	rc = en_dev->en_ops->bnxt_send_fw_msg(en_dev, BNXT_ROCE_ULP, &fw_msg);
1906 	if (rc) {
1907 		dev_dbg(rdev_to_dev(rdev),
1908 			"Failed to query dbr pacing config, rc = %#x", rc);
1909 		return rc;
1910 	}
1911 
1912 	primary_nq_id = le32_to_cpu(resp.primary_nq_id);
1913 	if (primary_nq_id == 0xffffffff &&
1914 	    !bnxt_qplib_dbr_pacing_ext_en(rdev->chip_ctx)) {
1915 		dev_err(rdev_to_dev(rdev), "%s:%d Invoke bnxt_qplib_dbr_pacing_set_primary_pf with 1\n",
1916 			__func__, __LINE__);
1917 		bnxt_qplib_dbr_pacing_set_primary_pf(rdev->chip_ctx, 1);
1918 	}
1919 
1920 	if (bnxt_qplib_dbr_pacing_ext_en(rdev->chip_ctx)) {
1921 		struct bnxt_qplib_nq *nq;
1922 
1923 		nq = &rdev->nqr.nq[0];
1924 		/* Reset the primary capability */
1925 		if (nq->ring_id != primary_nq_id)
1926 			bnxt_qplib_dbr_pacing_set_primary_pf(rdev->chip_ctx, 0);
1927 	}
1928 
1929 	if ((resp.dbr_stat_db_fifo_reg &
1930 	     HWRM_FUNC_DBR_PACING_QCFG_OUTPUT_DBR_STAT_DB_FIFO_REG_ADDR_SPACE_MASK) ==
1931 	    HWRM_FUNC_DBR_PACING_QCFG_OUTPUT_DBR_STAT_DB_FIFO_REG_ADDR_SPACE_GRC)
1932 		cctx->dbr_stat_db_fifo =
1933 		resp.dbr_stat_db_fifo_reg &
1934 		~HWRM_FUNC_DBR_PACING_QCFG_OUTPUT_DBR_STAT_DB_FIFO_REG_ADDR_SPACE_MASK;
1935 
1936 	if ((resp.dbr_throttling_aeq_arm_reg &
1937 	    HWRM_FUNC_DBR_PACING_QCFG_OUTPUT_DBR_THROTTLING_AEQ_ARM_REG_ADDR_SPACE_MASK)
1938 	    == HWRM_FUNC_DBR_PACING_QCFG_OUTPUT_DBR_THROTTLING_AEQ_ARM_REG_ADDR_SPACE_GRC) {
1939 		cctx->dbr_aeq_arm_reg = resp.dbr_throttling_aeq_arm_reg &
1940 			~HWRM_FUNC_DBR_PACING_QCFG_OUTPUT_DBR_STAT_DB_FIFO_REG_ADDR_SPACE_MASK;
1941 		cctx->dbr_throttling_reg = cctx->dbr_aeq_arm_reg - 4;
1942 	}
1943 	pacing_data->fifo_max_depth = le32_to_cpu(resp.dbr_stat_db_max_fifo_depth);
1944 	if (!pacing_data->fifo_max_depth)
1945 		pacing_data->fifo_max_depth = BNXT_RE_MAX_FIFO_DEPTH(cctx);
1946 	pacing_data->fifo_room_mask = le32_to_cpu(resp.dbr_stat_db_fifo_reg_fifo_room_mask);
1947 	pacing_data->fifo_room_shift = resp.dbr_stat_db_fifo_reg_fifo_room_shift;
1948 	dev_dbg(rdev_to_dev(rdev),
1949 		"%s: nq:0x%x primary_pf:%d db_fifo:0x%x aeq_arm:0x%x i"
1950 		"fifo_max_depth 0x%x , resp.dbr_stat_db_max_fifo_depth 0x%x);\n",
1951 		__func__, resp.primary_nq_id, cctx->modes.dbr_primary_pf,
1952 		 cctx->dbr_stat_db_fifo, cctx->dbr_aeq_arm_reg,
1953 		 pacing_data->fifo_max_depth,
1954 		le32_to_cpu(resp.dbr_stat_db_max_fifo_depth));
1955 	return 0;
1956 }
1957 
bnxt_re_hwrm_dbr_pacing_cfg(struct bnxt_re_dev * rdev,bool enable)1958 static int bnxt_re_hwrm_dbr_pacing_cfg(struct bnxt_re_dev *rdev, bool enable)
1959 {
1960 	struct hwrm_func_dbr_pacing_cfg_output resp = {0};
1961 	struct hwrm_func_dbr_pacing_cfg_input req = {0};
1962 	struct bnxt_en_dev *en_dev = rdev->en_dev;
1963 	struct bnxt_fw_msg fw_msg;
1964 	int rc;
1965 
1966 	if (test_bit(BNXT_RE_FLAG_ERR_DEVICE_DETACHED, &rdev->flags))
1967 		return 0;
1968 
1969 	memset(&fw_msg, 0, sizeof(fw_msg));
1970 	bnxt_re_init_hwrm_hdr(rdev, (void *)&req,
1971 			      HWRM_FUNC_DBR_PACING_CFG, -1, -1);
1972 	if (enable) {
1973 		req.flags = HWRM_FUNC_DBR_PACING_CFG_INPUT_FLAGS_DBR_NQ_EVENT_ENABLE;
1974 		req.enables =
1975 		cpu_to_le32(HWRM_FUNC_DBR_PACING_CFG_INPUT_ENABLES_PRIMARY_NQ_ID_VALID |
1976 			    HWRM_FUNC_DBR_PACING_CFG_INPUT_ENABLES_PACING_THRESHOLD_VALID);
1977 	} else {
1978 		req.flags = HWRM_FUNC_DBR_PACING_CFG_INPUT_FLAGS_DBR_NQ_EVENT_DISABLE;
1979 	}
1980 	req.primary_nq_id = cpu_to_le32(rdev->dbq_nq_id);
1981 	req.pacing_threshold = cpu_to_le32(rdev->dbq_watermark);
1982 	dev_dbg(rdev_to_dev(rdev), "%s: nq_id = 0x%x pacing_threshold = 0x%x",
1983 		__func__, req.primary_nq_id, req.pacing_threshold);
1984 	bnxt_re_fill_fw_msg(&fw_msg, (void *)&req, sizeof(req), (void *)&resp,
1985 			    sizeof(resp), BNXT_RE_HWRM_CMD_TIMEOUT(rdev));
1986 	rc = en_dev->en_ops->bnxt_send_fw_msg(en_dev, BNXT_ROCE_ULP, &fw_msg);
1987 	if (rc) {
1988 		dev_dbg(rdev_to_dev(rdev),
1989 			"Failed to set dbr pacing config, rc = %#x", rc);
1990 		return rc;
1991 	}
1992 	return 0;
1993 }
1994 
1995 /* Net -> RoCE driver */
1996 
1997 /* Device */
bnxt_re_from_netdev(struct ifnet * netdev)1998 struct bnxt_re_dev *bnxt_re_from_netdev(struct ifnet *netdev)
1999 {
2000 	struct bnxt_re_dev *rdev;
2001 
2002 	rcu_read_lock();
2003 	list_for_each_entry_rcu(rdev, &bnxt_re_dev_list, list) {
2004 		if (rdev->netdev == netdev) {
2005 			rcu_read_unlock();
2006 			dev_dbg(rdev_to_dev(rdev),
2007 				"netdev (%p) found, ref_count = 0x%x",
2008 				netdev, atomic_read(&rdev->ref_count));
2009 			return rdev;
2010 		}
2011 	}
2012 	rcu_read_unlock();
2013 	return NULL;
2014 }
2015 
show_rev(struct device * device,struct device_attribute * attr,char * buf)2016 static ssize_t show_rev(struct device *device, struct device_attribute *attr,
2017 			char *buf)
2018 {
2019 	struct bnxt_re_dev *rdev = to_bnxt_re_dev(device, ibdev.dev);
2020 
2021 	return scnprintf(buf, PAGE_SIZE, "0x%x\n", rdev->en_dev->pdev->vendor);
2022 }
2023 
2024 
show_hca(struct device * device,struct device_attribute * attr,char * buf)2025 static ssize_t show_hca(struct device *device, struct device_attribute *attr,
2026 			char *buf)
2027 {
2028 	struct bnxt_re_dev *rdev = to_bnxt_re_dev(device, ibdev.dev);
2029 
2030 	return scnprintf(buf, PAGE_SIZE, "%s\n", rdev->ibdev.node_desc);
2031 }
2032 
show_board_id(struct device * device,struct device_attribute * attr,char * buf)2033 static ssize_t show_board_id(struct device *device, struct device_attribute *attr,
2034 				char *buf)
2035 {
2036 	struct bnxt_re_dev *rdev = to_bnxt_re_dev(device, ibdev.dev);
2037 	char buffer[BNXT_VPD_PN_FLD_LEN] = {};
2038 
2039 	if (!rdev->is_virtfn)
2040 		memcpy(buffer, rdev->en_dev->board_part_number,
2041 			BNXT_VPD_PN_FLD_LEN - 1);
2042 	else
2043 		scnprintf(buffer, BNXT_VPD_PN_FLD_LEN,
2044 			"0x%x-VF", rdev->en_dev->pdev->device);
2045 
2046 	return scnprintf(buf, PAGE_SIZE, "%s\n", buffer);
2047 }
2048 
2049 static DEVICE_ATTR(hw_rev, 0444, show_rev, NULL);
2050 static DEVICE_ATTR(hca_type, 0444, show_hca, NULL);
2051 static DEVICE_ATTR(board_id, 0444, show_board_id, NULL);
2052 
2053 static struct device_attribute *bnxt_re_attributes[] = {
2054 	&dev_attr_hw_rev,
2055 	&dev_attr_hca_type,
2056 	&dev_attr_board_id
2057 };
2058 
ib_register_device_compat(struct bnxt_re_dev * rdev)2059 int ib_register_device_compat(struct bnxt_re_dev *rdev)
2060 {
2061 	struct ib_device *ibdev = &rdev->ibdev;
2062 	char name[IB_DEVICE_NAME_MAX];
2063 
2064 	memset(name, 0, IB_DEVICE_NAME_MAX);
2065 	strlcpy(name, "bnxt_re%d", IB_DEVICE_NAME_MAX);
2066 
2067 	strlcpy(ibdev->name, name, IB_DEVICE_NAME_MAX);
2068 
2069 	return ib_register_device(ibdev, NULL);
2070 }
2071 
bnxt_re_register_ib(struct bnxt_re_dev * rdev)2072 static int bnxt_re_register_ib(struct bnxt_re_dev *rdev)
2073 {
2074 	struct ib_device *ibdev = &rdev->ibdev;
2075 	int ret = 0;
2076 
2077 	/* ib device init */
2078 	ibdev->owner = THIS_MODULE;
2079 	ibdev->uverbs_abi_ver = BNXT_RE_ABI_VERSION;
2080 	ibdev->node_type = RDMA_NODE_IB_CA;
2081 	strlcpy(ibdev->node_desc, BNXT_RE_DESC " HCA",
2082 		strlen(BNXT_RE_DESC) + 5);
2083 	ibdev->phys_port_cnt = 1;
2084 
2085 	bnxt_qplib_get_guid(rdev->dev_addr, (u8 *)&ibdev->node_guid);
2086 
2087 	/* Data path irqs is one less than the max msix vectors */
2088 	ibdev->num_comp_vectors	= rdev->nqr.num_msix - 1;
2089 	bnxt_re_set_dma_device(ibdev, rdev);
2090 	ibdev->local_dma_lkey = BNXT_QPLIB_RSVD_LKEY;
2091 
2092 	/* User space */
2093 	ibdev->uverbs_cmd_mask =
2094 			(1ull << IB_USER_VERBS_CMD_GET_CONTEXT)		|
2095 			(1ull << IB_USER_VERBS_CMD_QUERY_DEVICE)	|
2096 			(1ull << IB_USER_VERBS_CMD_QUERY_PORT)		|
2097 			(1ull << IB_USER_VERBS_CMD_ALLOC_PD)		|
2098 			(1ull << IB_USER_VERBS_CMD_DEALLOC_PD)		|
2099 			(1ull << IB_USER_VERBS_CMD_REG_MR)		|
2100 			(1ull << IB_USER_VERBS_CMD_DEREG_MR)		|
2101 			(1ull << IB_USER_VERBS_CMD_CREATE_COMP_CHANNEL) |
2102 			(1ull << IB_USER_VERBS_CMD_CREATE_CQ)		|
2103 			(1ull << IB_USER_VERBS_CMD_DESTROY_CQ)		|
2104 			(1ull << IB_USER_VERBS_CMD_CREATE_QP)		|
2105 			(1ull << IB_USER_VERBS_CMD_MODIFY_QP)		|
2106 			(1ull << IB_USER_VERBS_CMD_QUERY_QP)		|
2107 			(1ull << IB_USER_VERBS_CMD_DESTROY_QP)		|
2108 			(1ull << IB_USER_VERBS_CMD_REREG_MR)		|
2109 			(1ull << IB_USER_VERBS_CMD_RESIZE_CQ)		|
2110 			(1ull << IB_USER_VERBS_CMD_CREATE_SRQ)		|
2111 			(1ull << IB_USER_VERBS_CMD_MODIFY_SRQ)		|
2112 			(1ull << IB_USER_VERBS_CMD_QUERY_SRQ)		|
2113 			(1ull << IB_USER_VERBS_CMD_DESTROY_SRQ)		|
2114 			(1ull << IB_USER_VERBS_CMD_ALLOC_MW)		|
2115 			(1ull << IB_USER_VERBS_CMD_DEALLOC_MW)		|
2116 			(1ull << IB_USER_VERBS_CMD_CREATE_AH)		|
2117 			(1ull << IB_USER_VERBS_CMD_MODIFY_AH)		|
2118 			(1ull << IB_USER_VERBS_CMD_QUERY_AH)		|
2119 			(1ull << IB_USER_VERBS_CMD_DESTROY_AH);
2120 
2121 	ibdev->uverbs_ex_cmd_mask = (1ull << IB_USER_VERBS_EX_CMD_MODIFY_QP);
2122 	ibdev->uverbs_cmd_mask |= (1ull << IB_USER_VERBS_CMD_POLL_CQ);
2123 
2124 #define bnxt_re_ib_ah bnxt_re_ah
2125 #define bnxt_re_ib_cq bnxt_re_cq
2126 #define bnxt_re_ib_pd bnxt_re_pd
2127 #define bnxt_re_ib_srq bnxt_re_srq
2128 #define bnxt_re_ib_ucontext bnxt_re_ucontext
2129 	INIT_IB_DEVICE_OPS(&ibdev->ops, bnxt_re, BNXT_RE);
2130 
2131 	ibdev->query_device		= bnxt_re_query_device;
2132 	ibdev->modify_device		= bnxt_re_modify_device;
2133 	ibdev->query_port		= bnxt_re_query_port;
2134 	ibdev->modify_port		= bnxt_re_modify_port;
2135 	ibdev->get_port_immutable	= bnxt_re_get_port_immutable;
2136 	ibdev->query_pkey		= bnxt_re_query_pkey;
2137 	ibdev->query_gid		= bnxt_re_query_gid;
2138 	ibdev->get_netdev		= bnxt_re_get_netdev;
2139 	ibdev->add_gid			= bnxt_re_add_gid;
2140 	ibdev->del_gid			= bnxt_re_del_gid;
2141 	ibdev->get_link_layer		= bnxt_re_get_link_layer;
2142 	ibdev->alloc_pd			= bnxt_re_alloc_pd;
2143 	ibdev->dealloc_pd		= bnxt_re_dealloc_pd;
2144 	ibdev->create_ah		= bnxt_re_create_ah;
2145 	ibdev->modify_ah		= bnxt_re_modify_ah;
2146 	ibdev->query_ah			= bnxt_re_query_ah;
2147 	ibdev->destroy_ah		= bnxt_re_destroy_ah;
2148 	ibdev->create_srq		= bnxt_re_create_srq;
2149 	ibdev->modify_srq		= bnxt_re_modify_srq;
2150 	ibdev->query_srq		= bnxt_re_query_srq;
2151 	ibdev->destroy_srq		= bnxt_re_destroy_srq;
2152 	ibdev->post_srq_recv		= bnxt_re_post_srq_recv;
2153 	ibdev->create_qp		= bnxt_re_create_qp;
2154 	ibdev->modify_qp		= bnxt_re_modify_qp;
2155 	ibdev->query_qp			= bnxt_re_query_qp;
2156 	ibdev->destroy_qp		= bnxt_re_destroy_qp;
2157 	ibdev->post_send		= bnxt_re_post_send;
2158 	ibdev->post_recv		= bnxt_re_post_recv;
2159 	ibdev->create_cq		= bnxt_re_create_cq;
2160 	ibdev->modify_cq		= bnxt_re_modify_cq;
2161 	ibdev->destroy_cq		= bnxt_re_destroy_cq;
2162 	ibdev->resize_cq		= bnxt_re_resize_cq;
2163 	ibdev->poll_cq			= bnxt_re_poll_cq;
2164 	ibdev->req_notify_cq		= bnxt_re_req_notify_cq;
2165 	ibdev->get_dma_mr		= bnxt_re_get_dma_mr;
2166 	ibdev->get_hw_stats		= bnxt_re_get_hw_stats;
2167 	ibdev->alloc_hw_stats		= bnxt_re_alloc_hw_port_stats;
2168 	ibdev->dereg_mr			= bnxt_re_dereg_mr;
2169 	ibdev->alloc_mr			= bnxt_re_alloc_mr;
2170 	ibdev->map_mr_sg		= bnxt_re_map_mr_sg;
2171 	ibdev->alloc_mw			= bnxt_re_alloc_mw;
2172 	ibdev->dealloc_mw		= bnxt_re_dealloc_mw;
2173 	ibdev->reg_user_mr		= bnxt_re_reg_user_mr;
2174 	ibdev->rereg_user_mr		= bnxt_re_rereg_user_mr;
2175 	ibdev->disassociate_ucontext	= bnxt_re_disassociate_ucntx;
2176 	ibdev->alloc_ucontext		= bnxt_re_alloc_ucontext;
2177 	ibdev->dealloc_ucontext		= bnxt_re_dealloc_ucontext;
2178 	ibdev->mmap			= bnxt_re_mmap;
2179 	ibdev->process_mad		= bnxt_re_process_mad;
2180 
2181 	ret = ib_register_device_compat(rdev);
2182 	return ret;
2183 }
2184 
bnxt_re_dev_dealloc(struct bnxt_re_dev * rdev)2185 static void bnxt_re_dev_dealloc(struct bnxt_re_dev *rdev)
2186 {
2187 	int i = BNXT_RE_REF_WAIT_COUNT;
2188 
2189 	dev_dbg(rdev_to_dev(rdev), "%s:Remove the device %p\n", __func__, rdev);
2190 	/* Wait for rdev refcount to come down */
2191 	while ((atomic_read(&rdev->ref_count) > 1) && i--)
2192 		msleep(100);
2193 
2194 	if (atomic_read(&rdev->ref_count) > 1)
2195 		dev_err(rdev_to_dev(rdev),
2196 			"Failed waiting for ref count to deplete %d",
2197 			atomic_read(&rdev->ref_count));
2198 
2199 	atomic_set(&rdev->ref_count, 0);
2200 	if_rele(rdev->netdev);
2201 	rdev->netdev = NULL;
2202 	synchronize_rcu();
2203 
2204 	kfree(rdev->gid_map);
2205 	kfree(rdev->dbg_stats);
2206 	ib_dealloc_device(&rdev->ibdev);
2207 }
2208 
bnxt_re_dev_alloc(struct ifnet * netdev,struct bnxt_en_dev * en_dev)2209 static struct bnxt_re_dev *bnxt_re_dev_alloc(struct ifnet *netdev,
2210 					   struct bnxt_en_dev *en_dev)
2211 {
2212 	struct bnxt_re_dev *rdev;
2213 	u32 count;
2214 
2215 	/* Allocate bnxt_re_dev instance here */
2216 	rdev = (struct bnxt_re_dev *)compat_ib_alloc_device(sizeof(*rdev));
2217 	if (!rdev) {
2218 		pr_err("%s: bnxt_re_dev allocation failure!",
2219 			ROCE_DRV_MODULE_NAME);
2220 		return NULL;
2221 	}
2222 	/* Default values */
2223 	atomic_set(&rdev->ref_count, 0);
2224 	rdev->netdev = netdev;
2225 	dev_hold(rdev->netdev);
2226 	rdev->en_dev = en_dev;
2227 	rdev->id = rdev->en_dev->pdev->devfn;
2228 	INIT_LIST_HEAD(&rdev->qp_list);
2229 	mutex_init(&rdev->qp_lock);
2230 	mutex_init(&rdev->cc_lock);
2231 	mutex_init(&rdev->dbq_lock);
2232 	bnxt_re_clear_rsors_stat(&rdev->stats.rsors);
2233 	rdev->cosq[0] = rdev->cosq[1] = 0xFFFF;
2234 	rdev->min_tx_depth = 1;
2235 	rdev->stats.stats_query_sec = 1;
2236 	/* Disable priority vlan as the default mode is DSCP based PFC */
2237 	rdev->cc_param.disable_prio_vlan_tx = 1;
2238 
2239 	/* Initialize worker for DBR Pacing */
2240 	INIT_WORK(&rdev->dbq_fifo_check_work, bnxt_re_db_fifo_check);
2241 	INIT_DELAYED_WORK(&rdev->dbq_pacing_work, bnxt_re_pacing_timer_exp);
2242 	rdev->gid_map = kzalloc(sizeof(*(rdev->gid_map)) *
2243 				  BNXT_RE_MAX_SGID_ENTRIES,
2244 				  GFP_KERNEL);
2245 	if (!rdev->gid_map) {
2246 		ib_dealloc_device(&rdev->ibdev);
2247 		return NULL;
2248 	}
2249 	for(count = 0; count < BNXT_RE_MAX_SGID_ENTRIES; count++)
2250 		rdev->gid_map[count] = -1;
2251 
2252 	rdev->dbg_stats = kzalloc(sizeof(*rdev->dbg_stats), GFP_KERNEL);
2253 	if (!rdev->dbg_stats) {
2254 		ib_dealloc_device(&rdev->ibdev);
2255 		return NULL;
2256 	}
2257 
2258 	return rdev;
2259 }
2260 
bnxt_re_handle_unaffi_async_event(struct creq_func_event * unaffi_async)2261 static int bnxt_re_handle_unaffi_async_event(
2262 		struct creq_func_event *unaffi_async)
2263 {
2264 	switch (unaffi_async->event) {
2265 	case CREQ_FUNC_EVENT_EVENT_TX_WQE_ERROR:
2266 	case CREQ_FUNC_EVENT_EVENT_TX_DATA_ERROR:
2267 	case CREQ_FUNC_EVENT_EVENT_RX_WQE_ERROR:
2268 	case CREQ_FUNC_EVENT_EVENT_RX_DATA_ERROR:
2269 	case CREQ_FUNC_EVENT_EVENT_CQ_ERROR:
2270 	case CREQ_FUNC_EVENT_EVENT_TQM_ERROR:
2271 	case CREQ_FUNC_EVENT_EVENT_CFCQ_ERROR:
2272 	case CREQ_FUNC_EVENT_EVENT_CFCS_ERROR:
2273 	case CREQ_FUNC_EVENT_EVENT_CFCC_ERROR:
2274 	case CREQ_FUNC_EVENT_EVENT_CFCM_ERROR:
2275 	case CREQ_FUNC_EVENT_EVENT_TIM_ERROR:
2276 		break;
2277 	default:
2278 		return -EINVAL;
2279 	}
2280 	return 0;
2281 }
2282 
bnxt_re_handle_qp_async_event(void * qp_event,struct bnxt_re_qp * qp)2283 static int bnxt_re_handle_qp_async_event(void *qp_event, struct bnxt_re_qp *qp)
2284 {
2285 	struct creq_qp_error_notification *err_event;
2286 	struct ib_event event;
2287 	unsigned int flags;
2288 
2289 	if (qp->qplib_qp.state == CMDQ_MODIFY_QP_NEW_STATE_ERR &&
2290 	    !qp->qplib_qp.is_user) {
2291 		flags = bnxt_re_lock_cqs(qp);
2292 		bnxt_qplib_add_flush_qp(&qp->qplib_qp);
2293 		bnxt_re_unlock_cqs(qp, flags);
2294 	}
2295 	memset(&event, 0, sizeof(event));
2296 	event.device = &qp->rdev->ibdev;
2297 	event.element.qp = &qp->ib_qp;
2298 	event.event = IB_EVENT_QP_FATAL;
2299 
2300 	err_event = qp_event;
2301 	switch(err_event->res_err_state_reason) {
2302 	case CFCQ_RES_ERR_STATE_REASON_RES_EXCEED_MAX:
2303 	case CFCQ_RES_ERR_STATE_REASON_RES_PAYLOAD_LENGTH_MISMATCH:
2304 	case CFCQ_RES_ERR_STATE_REASON_RES_OPCODE_ERROR:
2305 	case CFCQ_RES_ERR_STATE_REASON_RES_PSN_SEQ_ERROR_RETRY_LIMIT:
2306 	case CFCQ_RES_ERR_STATE_REASON_RES_RX_INVALID_R_KEY:
2307 	case CFCQ_RES_ERR_STATE_REASON_RES_RX_DOMAIN_ERROR:
2308 	case CFCQ_RES_ERR_STATE_REASON_RES_RX_NO_PERMISSION:
2309 	case CFCQ_RES_ERR_STATE_REASON_RES_RX_RANGE_ERROR:
2310 	case CFCQ_RES_ERR_STATE_REASON_RES_TX_INVALID_R_KEY:
2311 	case CFCQ_RES_ERR_STATE_REASON_RES_TX_DOMAIN_ERROR:
2312 	case CFCQ_RES_ERR_STATE_REASON_RES_TX_NO_PERMISSION:
2313 	case CFCQ_RES_ERR_STATE_REASON_RES_TX_RANGE_ERROR:
2314 	case CFCQ_RES_ERR_STATE_REASON_RES_IVALID_DUP_RKEY:
2315 	case CFCQ_RES_ERR_STATE_REASON_RES_UNALIGN_ATOMIC:
2316 		event.event = IB_EVENT_QP_ACCESS_ERR;
2317 		break;
2318 	case CFCQ_RES_ERR_STATE_REASON_RES_EXCEEDS_WQE:
2319 	case CFCQ_RES_ERR_STATE_REASON_RES_WQE_FORMAT_ERROR:
2320 	case CFCQ_RES_ERR_STATE_REASON_RES_SRQ_LOAD_ERROR:
2321 	case CFCQ_RES_ERR_STATE_REASON_RES_UNSUPPORTED_OPCODE:
2322 	case CFCQ_RES_ERR_STATE_REASON_RES_REM_INVALIDATE:
2323 		event.event = IB_EVENT_QP_REQ_ERR;
2324 		break;
2325 	case CFCQ_RES_ERR_STATE_REASON_RES_IRRQ_OFLOW:
2326 	case CFCQ_RES_ERR_STATE_REASON_RES_CMP_ERROR:
2327 	case CFCQ_RES_ERR_STATE_REASON_RES_CQ_LOAD_ERROR:
2328 	case CFCQ_RES_ERR_STATE_REASON_RES_TX_PCI_ERROR:
2329 	case CFCQ_RES_ERR_STATE_REASON_RES_RX_PCI_ERROR:
2330 	case CFCQ_RES_ERR_STATE_REASON_RES_MEMORY_ERROR:
2331 	case CFCQ_RES_ERR_STATE_REASON_RES_SRQ_ERROR:
2332 		event.event = IB_EVENT_QP_FATAL;
2333 		break;
2334 	default:
2335 		if (qp->qplib_qp.srq)
2336 			event.event = IB_EVENT_QP_LAST_WQE_REACHED;
2337 		break;
2338 	}
2339 
2340 	if (err_event->res_err_state_reason)
2341 		dev_err(rdev_to_dev(qp->rdev),
2342 			"%s %s qp_id: %d cons (%d %d) req (%d %d) res (%d %d)\n",
2343 			__func__,  qp->qplib_qp.is_user ? "user" : "kernel",
2344 			qp->qplib_qp.id,
2345 			err_event->sq_cons_idx,
2346 			err_event->rq_cons_idx,
2347 			err_event->req_slow_path_state,
2348 			err_event->req_err_state_reason,
2349 			err_event->res_slow_path_state,
2350 			err_event->res_err_state_reason);
2351 
2352 	if (event.device && qp->ib_qp.event_handler)
2353 		qp->ib_qp.event_handler(&event, qp->ib_qp.qp_context);
2354 
2355 	return 0;
2356 }
2357 
bnxt_re_handle_cq_async_error(void * event,struct bnxt_re_cq * cq)2358 static int bnxt_re_handle_cq_async_error(void *event, struct bnxt_re_cq *cq)
2359 {
2360 	struct creq_cq_error_notification *cqerr;
2361 	bool send = false;
2362 
2363 	cqerr = event;
2364 	switch (cqerr->cq_err_reason) {
2365 	case CREQ_CQ_ERROR_NOTIFICATION_CQ_ERR_REASON_REQ_CQ_INVALID_ERROR:
2366 	case CREQ_CQ_ERROR_NOTIFICATION_CQ_ERR_REASON_REQ_CQ_OVERFLOW_ERROR:
2367 	case CREQ_CQ_ERROR_NOTIFICATION_CQ_ERR_REASON_REQ_CQ_LOAD_ERROR:
2368 	case CREQ_CQ_ERROR_NOTIFICATION_CQ_ERR_REASON_RES_CQ_INVALID_ERROR:
2369 	case CREQ_CQ_ERROR_NOTIFICATION_CQ_ERR_REASON_RES_CQ_OVERFLOW_ERROR:
2370 	case CREQ_CQ_ERROR_NOTIFICATION_CQ_ERR_REASON_RES_CQ_LOAD_ERROR:
2371 		send = true;
2372 	default:
2373 		break;
2374 	}
2375 
2376 	if (send && cq->ibcq.event_handler) {
2377 		struct ib_event ibevent = {};
2378 
2379 		ibevent.event = IB_EVENT_CQ_ERR;
2380 		ibevent.element.cq = &cq->ibcq;
2381 		ibevent.device = &cq->rdev->ibdev;
2382 
2383 		dev_err(rdev_to_dev(cq->rdev),
2384 			"%s err reason %d\n", __func__, cqerr->cq_err_reason);
2385 		cq->ibcq.event_handler(&ibevent, cq->ibcq.cq_context);
2386 	}
2387 
2388 	cq->qplib_cq.is_cq_err_event = true;
2389 
2390 	return 0;
2391 }
2392 
bnxt_re_handle_affi_async_event(struct creq_qp_event * affi_async,void * obj)2393 static int bnxt_re_handle_affi_async_event(struct creq_qp_event *affi_async,
2394 					   void *obj)
2395 {
2396 	struct bnxt_qplib_qp *qplqp;
2397 	struct bnxt_qplib_cq *qplcq;
2398 	struct bnxt_re_qp *qp;
2399 	struct bnxt_re_cq *cq;
2400 	int rc = 0;
2401 	u8 event;
2402 
2403 	if (!obj)
2404 		return rc; /* QP was already dead, still return success */
2405 
2406 	event = affi_async->event;
2407 	switch (event) {
2408 	case CREQ_QP_EVENT_EVENT_QP_ERROR_NOTIFICATION:
2409 		qplqp = obj;
2410 		qp = container_of(qplqp, struct bnxt_re_qp, qplib_qp);
2411 		rc = bnxt_re_handle_qp_async_event(affi_async, qp);
2412 		break;
2413 	case CREQ_QP_EVENT_EVENT_CQ_ERROR_NOTIFICATION:
2414 		qplcq = obj;
2415 		cq = container_of(qplcq, struct bnxt_re_cq, qplib_cq);
2416 		rc = bnxt_re_handle_cq_async_error(affi_async, cq);
2417 		break;
2418 	default:
2419 		rc = -EINVAL;
2420 	}
2421 
2422 	return rc;
2423 }
2424 
bnxt_re_aeq_handler(struct bnxt_qplib_rcfw * rcfw,void * aeqe,void * obj)2425 static int bnxt_re_aeq_handler(struct bnxt_qplib_rcfw *rcfw,
2426 			       void *aeqe, void *obj)
2427 {
2428 	struct creq_func_event *unaffi_async;
2429 	struct creq_qp_event *affi_async;
2430 	u8 type;
2431 	int rc;
2432 
2433 	type = ((struct creq_base *)aeqe)->type;
2434 	if (type == CREQ_BASE_TYPE_FUNC_EVENT) {
2435 		unaffi_async = aeqe;
2436 		rc = bnxt_re_handle_unaffi_async_event(unaffi_async);
2437 	} else {
2438 		affi_async = aeqe;
2439 		rc = bnxt_re_handle_affi_async_event(affi_async, obj);
2440 	}
2441 
2442 	return rc;
2443 }
2444 
bnxt_re_srqn_handler(struct bnxt_qplib_nq * nq,struct bnxt_qplib_srq * handle,u8 event)2445 static int bnxt_re_srqn_handler(struct bnxt_qplib_nq *nq,
2446 				struct bnxt_qplib_srq *handle, u8 event)
2447 {
2448 	struct bnxt_re_srq *srq = to_bnxt_re(handle, struct bnxt_re_srq,
2449 					     qplib_srq);
2450 	struct ib_event ib_event;
2451 
2452 	if (srq == NULL) {
2453 		pr_err("%s: SRQ is NULL, SRQN not handled",
2454 			ROCE_DRV_MODULE_NAME);
2455 		return -EINVAL;
2456 	}
2457 	ib_event.device = &srq->rdev->ibdev;
2458 	ib_event.element.srq = &srq->ibsrq;
2459 	if (event == NQ_SRQ_EVENT_EVENT_SRQ_THRESHOLD_EVENT)
2460 		ib_event.event = IB_EVENT_SRQ_LIMIT_REACHED;
2461 	else
2462 		ib_event.event = IB_EVENT_SRQ_ERR;
2463 
2464 	if (srq->ibsrq.event_handler) {
2465 		/* Lock event_handler? */
2466 		(*srq->ibsrq.event_handler)(&ib_event,
2467 					     srq->ibsrq.srq_context);
2468 	}
2469 	return 0;
2470 }
2471 
bnxt_re_cqn_handler(struct bnxt_qplib_nq * nq,struct bnxt_qplib_cq * handle)2472 static int bnxt_re_cqn_handler(struct bnxt_qplib_nq *nq,
2473 			       struct bnxt_qplib_cq *handle)
2474 {
2475 	struct bnxt_re_cq *cq = to_bnxt_re(handle, struct bnxt_re_cq,
2476 					   qplib_cq);
2477 	u32 *cq_ptr;
2478 
2479 	if (cq == NULL) {
2480 		pr_err("%s: CQ is NULL, CQN not handled",
2481 			ROCE_DRV_MODULE_NAME);
2482 		return -EINVAL;
2483 	}
2484 	/* CQ already in destroy path. Do not handle any more events */
2485 	if (handle->destroyed || !atomic_read(&cq->ibcq.usecnt)) {
2486 		if (!handle->destroyed)
2487 			dev_dbg(NULL, "%s: CQ being destroyed, CQN not handled",
2488 				ROCE_DRV_MODULE_NAME);
2489 		return 0;
2490 	}
2491 
2492 	if (cq->ibcq.comp_handler) {
2493 		if (cq->uctx_cq_page) {
2494 			cq_ptr = (u32 *)cq->uctx_cq_page;
2495 			*cq_ptr = cq->qplib_cq.toggle;
2496 		}
2497 		/* Lock comp_handler? */
2498 		(*cq->ibcq.comp_handler)(&cq->ibcq, cq->ibcq.cq_context);
2499 	}
2500 
2501 	return 0;
2502 }
2503 
bnxt_re_get_nq(struct bnxt_re_dev * rdev)2504 struct bnxt_qplib_nq *bnxt_re_get_nq(struct bnxt_re_dev *rdev)
2505 {
2506 	int min, indx;
2507 
2508 	mutex_lock(&rdev->nqr.load_lock);
2509 	for (indx = 0, min = 0; indx < (rdev->nqr.num_msix - 1); indx++) {
2510 		if (rdev->nqr.nq[min].load > rdev->nqr.nq[indx].load)
2511 			min = indx;
2512 	}
2513 	rdev->nqr.nq[min].load++;
2514 	mutex_unlock(&rdev->nqr.load_lock);
2515 
2516 	return &rdev->nqr.nq[min];
2517 }
2518 
bnxt_re_put_nq(struct bnxt_re_dev * rdev,struct bnxt_qplib_nq * nq)2519 void bnxt_re_put_nq(struct bnxt_re_dev *rdev, struct bnxt_qplib_nq *nq)
2520 {
2521 	mutex_lock(&rdev->nqr.load_lock);
2522 	nq->load--;
2523 	mutex_unlock(&rdev->nqr.load_lock);
2524 }
2525 
bnxt_re_check_min_attr(struct bnxt_re_dev * rdev)2526 static bool bnxt_re_check_min_attr(struct bnxt_re_dev *rdev)
2527 {
2528 	struct bnxt_qplib_dev_attr *attr;
2529 	bool rc = true;
2530 
2531 	attr = rdev->dev_attr;
2532 
2533 	if (!attr->max_cq || !attr->max_qp ||
2534 	    !attr->max_sgid || !attr->max_mr) {
2535 		dev_err(rdev_to_dev(rdev),"Insufficient RoCE resources");
2536 		dev_dbg(rdev_to_dev(rdev),
2537 			"max_cq = %d, max_qp = %d, max_dpi = %d, max_sgid = %d, max_mr = %d",
2538 			attr->max_cq, attr->max_qp, attr->max_dpi,
2539 			attr->max_sgid, attr->max_mr);
2540 		rc = false;
2541 	}
2542 	return rc;
2543 }
2544 
bnxt_re_dispatch_event(struct ib_device * ibdev,struct ib_qp * qp,u8 port_num,enum ib_event_type event)2545 static void bnxt_re_dispatch_event(struct ib_device *ibdev, struct ib_qp *qp,
2546 				   u8 port_num, enum ib_event_type event)
2547 {
2548 	struct ib_event ib_event;
2549 
2550 	ib_event.device = ibdev;
2551 	if (qp) {
2552 		ib_event.element.qp = qp;
2553 		ib_event.event = event;
2554 		if (qp->event_handler)
2555 			qp->event_handler(&ib_event, qp->qp_context);
2556 	} else {
2557 		ib_event.element.port_num = port_num;
2558 		ib_event.event = event;
2559 		ib_dispatch_event(&ib_event);
2560 	}
2561 
2562 	dev_dbg(rdev_to_dev(to_bnxt_re_dev(ibdev, ibdev)),
2563 		"ibdev %p Event 0x%x port_num 0x%x", ibdev, event, port_num);
2564 }
2565 
bnxt_re_is_qp1_or_shadow_qp(struct bnxt_re_dev * rdev,struct bnxt_re_qp * qp)2566 static bool bnxt_re_is_qp1_or_shadow_qp(struct bnxt_re_dev *rdev,
2567 					struct bnxt_re_qp *qp)
2568 {
2569 	if (rdev->gsi_ctx.gsi_qp_mode == BNXT_RE_GSI_MODE_ALL)
2570 		return (qp->ib_qp.qp_type == IB_QPT_GSI) ||
2571 			(qp == rdev->gsi_ctx.gsi_sqp);
2572 	else
2573 		return (qp->ib_qp.qp_type == IB_QPT_GSI);
2574 }
2575 
bnxt_re_stop_all_nonqp1_nonshadow_qps(struct bnxt_re_dev * rdev)2576 static void bnxt_re_stop_all_nonqp1_nonshadow_qps(struct bnxt_re_dev *rdev)
2577 {
2578 	struct bnxt_qplib_qp *qpl_qp;
2579 	bool dev_detached = false;
2580 	struct ib_qp_attr qp_attr;
2581 	int num_qps_stopped = 0;
2582 	int mask = IB_QP_STATE;
2583 	struct bnxt_re_qp *qp;
2584 	unsigned long flags;
2585 
2586 	if (!rdev)
2587 		return;
2588 
2589 restart:
2590 	if (test_bit(BNXT_RE_FLAG_ERR_DEVICE_DETACHED, &rdev->flags))
2591 		dev_detached = true;
2592 
2593 	qp_attr.qp_state = IB_QPS_ERR;
2594 	mutex_lock(&rdev->qp_lock);
2595 	list_for_each_entry(qp, &rdev->qp_list, list) {
2596 		qpl_qp = &qp->qplib_qp;
2597 		if (dev_detached || !bnxt_re_is_qp1_or_shadow_qp(rdev, qp)) {
2598 			if (qpl_qp->state !=
2599 			    CMDQ_MODIFY_QP_NEW_STATE_RESET &&
2600 			    qpl_qp->state !=
2601 			    CMDQ_MODIFY_QP_NEW_STATE_ERR) {
2602 				if (dev_detached) {
2603 					/*
2604 					 * Cant actually send the command down,
2605 					 * marking the state for bookkeeping
2606 					 */
2607 					qpl_qp->state =
2608 						CMDQ_MODIFY_QP_NEW_STATE_ERR;
2609 					qpl_qp->cur_qp_state = qpl_qp->state;
2610 					if (!qpl_qp->is_user) {
2611 						/* Add to flush list */
2612 						flags = bnxt_re_lock_cqs(qp);
2613 						bnxt_qplib_add_flush_qp(qpl_qp);
2614 						bnxt_re_unlock_cqs(qp, flags);
2615 					}
2616 				} else {
2617 					num_qps_stopped++;
2618 					bnxt_re_modify_qp(&qp->ib_qp,
2619 							  &qp_attr, mask,
2620 							  NULL);
2621 				}
2622 
2623 				bnxt_re_dispatch_event(&rdev->ibdev, &qp->ib_qp,
2624 						       1, IB_EVENT_QP_FATAL);
2625 				/*
2626 				 * 1. Release qp_lock after a budget to unblock other verb
2627 				 *    requests (like qp_destroy) from stack.
2628 				 * 2. Traverse through the qp_list freshly as addition / deletion
2629 				 *    might have happened since qp_lock is getting released here.
2630 				 */
2631 				if (num_qps_stopped % BNXT_RE_STOP_QPS_BUDGET == 0) {
2632 					mutex_unlock(&rdev->qp_lock);
2633 					goto restart;
2634 				}
2635 			}
2636 		}
2637 	}
2638 
2639 	mutex_unlock(&rdev->qp_lock);
2640 }
2641 
bnxt_re_update_gid(struct bnxt_re_dev * rdev)2642 static int bnxt_re_update_gid(struct bnxt_re_dev *rdev)
2643 {
2644 	struct bnxt_qplib_sgid_tbl *sgid_tbl = &rdev->qplib_res.sgid_tbl;
2645 	struct bnxt_qplib_gid gid;
2646 	u16 gid_idx, index;
2647 	int rc = 0;
2648 
2649 	if (!test_bit(BNXT_RE_FLAG_IBDEV_REGISTERED, &rdev->flags))
2650 		return 0;
2651 
2652 	if (sgid_tbl == NULL) {
2653 		dev_err(rdev_to_dev(rdev), "QPLIB: SGID table not allocated");
2654 		return -EINVAL;
2655 	}
2656 
2657 	for (index = 0; index < sgid_tbl->active; index++) {
2658 		gid_idx = sgid_tbl->hw_id[index];
2659 
2660 		if (!memcmp(&sgid_tbl->tbl[index], &bnxt_qplib_gid_zero,
2661 			    sizeof(bnxt_qplib_gid_zero)))
2662 			continue;
2663 		/* Need to modify the VLAN enable setting of non VLAN GID only
2664 		 * as setting is done for VLAN GID while adding GID
2665 		 *
2666 		 * If disable_prio_vlan_tx is enable, then we'll need to remove the
2667 		 * vlan entry from the sgid_tbl.
2668 		 */
2669 		if (sgid_tbl->vlan[index] == true)
2670 			continue;
2671 
2672 		memcpy(&gid, &sgid_tbl->tbl[index], sizeof(gid));
2673 
2674 		rc = bnxt_qplib_update_sgid(sgid_tbl, &gid, gid_idx,
2675 					    rdev->dev_addr);
2676 	}
2677 
2678 	return rc;
2679 }
2680 
bnxt_re_clear_cc(struct bnxt_re_dev * rdev)2681 static void bnxt_re_clear_cc(struct bnxt_re_dev *rdev)
2682 {
2683 	struct bnxt_qplib_cc_param *cc_param = &rdev->cc_param;
2684 
2685 	if (_is_chip_p7(rdev->chip_ctx)) {
2686 		cc_param->mask = CMDQ_MODIFY_ROCE_CC_MODIFY_MASK_TOS_DSCP;
2687 	} else {
2688 		cc_param->mask = (CMDQ_MODIFY_ROCE_CC_MODIFY_MASK_CC_MODE |
2689 				  CMDQ_MODIFY_ROCE_CC_MODIFY_MASK_ENABLE_CC |
2690 				  CMDQ_MODIFY_ROCE_CC_MODIFY_MASK_TOS_ECN);
2691 
2692 		if (!is_qport_service_type_supported(rdev))
2693 			cc_param->mask |=
2694 			(CMDQ_MODIFY_ROCE_CC_MODIFY_MASK_ALT_VLAN_PCP |
2695 			 CMDQ_MODIFY_ROCE_CC_MODIFY_MASK_ALT_TOS_DSCP |
2696 			 CMDQ_MODIFY_ROCE_CC_MODIFY_MASK_TOS_DSCP);
2697 	}
2698 
2699 	cc_param->cur_mask  = cc_param->mask;
2700 
2701 	if (bnxt_qplib_modify_cc(&rdev->qplib_res, cc_param))
2702 		dev_err(rdev_to_dev(rdev), "Failed to modify cc\n");
2703 }
2704 
bnxt_re_setup_cc(struct bnxt_re_dev * rdev)2705 static int bnxt_re_setup_cc(struct bnxt_re_dev *rdev)
2706 {
2707 	struct bnxt_qplib_cc_param *cc_param = &rdev->cc_param;
2708 	int rc;
2709 
2710 	if (_is_chip_p7(rdev->chip_ctx)) {
2711 		cc_param->enable = 0x0;
2712 		cc_param->mask = CMDQ_MODIFY_ROCE_CC_MODIFY_MASK_TOS_DSCP;
2713 	} else {
2714 		cc_param->enable = 0x1;
2715 		cc_param->mask = (CMDQ_MODIFY_ROCE_CC_MODIFY_MASK_CC_MODE |
2716 				  CMDQ_MODIFY_ROCE_CC_MODIFY_MASK_ENABLE_CC |
2717 				  CMDQ_MODIFY_ROCE_CC_MODIFY_MASK_TOS_ECN);
2718 
2719 		if (!is_qport_service_type_supported(rdev))
2720 			cc_param->mask |=
2721 			(CMDQ_MODIFY_ROCE_CC_MODIFY_MASK_ALT_VLAN_PCP |
2722 			 CMDQ_MODIFY_ROCE_CC_MODIFY_MASK_ALT_TOS_DSCP |
2723 			 CMDQ_MODIFY_ROCE_CC_MODIFY_MASK_TOS_DSCP);
2724 	}
2725 
2726 	cc_param->cur_mask  = cc_param->mask;
2727 
2728 	rc = bnxt_qplib_modify_cc(&rdev->qplib_res, cc_param);
2729 	if (rc) {
2730 		dev_err(rdev_to_dev(rdev), "Failed to modify cc\n");
2731 		return rc;
2732 	}
2733 	/* Reset the programming mask */
2734 	cc_param->mask = 0;
2735 	if (cc_param->qp1_tos_dscp != cc_param->tos_dscp) {
2736 		cc_param->qp1_tos_dscp = cc_param->tos_dscp;
2737 		rc = bnxt_re_update_qp1_tos_dscp(rdev);
2738 		if (rc) {
2739 			dev_err(rdev_to_dev(rdev), "%s:Failed to modify QP1:%d",
2740 				__func__, rc);
2741 			goto clear;
2742 		}
2743 	}
2744 	return 0;
2745 
2746 clear:
2747 	bnxt_re_clear_cc(rdev);
2748 	return rc;
2749 }
2750 
bnxt_re_query_hwrm_dscp2pri(struct bnxt_re_dev * rdev,struct bnxt_re_dscp2pri * d2p,u16 * count,u16 target_id)2751 int bnxt_re_query_hwrm_dscp2pri(struct bnxt_re_dev *rdev,
2752 				struct bnxt_re_dscp2pri *d2p, u16 *count,
2753 				u16 target_id)
2754 {
2755 	struct bnxt_en_dev *en_dev = rdev->en_dev;
2756 	struct hwrm_queue_dscp2pri_qcfg_input req;
2757 	struct hwrm_queue_dscp2pri_qcfg_output resp;
2758 	struct bnxt_re_dscp2pri *dscp2pri;
2759 	struct bnxt_fw_msg fw_msg;
2760 	u16 in_count = *count;
2761 	dma_addr_t dma_handle;
2762 	int rc = 0, i;
2763 	u16 data_len;
2764 	u8 *kmem;
2765 
2766 	data_len = *count * sizeof(*dscp2pri);
2767 	memset(&fw_msg, 0, sizeof(fw_msg));
2768 	memset(&req, 0, sizeof(req));
2769 	bnxt_re_init_hwrm_hdr(rdev, (void *)&req,
2770 			      HWRM_QUEUE_DSCP2PRI_QCFG, -1, target_id);
2771 	req.port_id = (target_id == 0xFFFF) ? en_dev->pf_port_id : 1;
2772 
2773 	kmem = dma_zalloc_coherent(&en_dev->pdev->dev, data_len, &dma_handle,
2774 				   GFP_KERNEL);
2775 	if (!kmem) {
2776 		dev_err(rdev_to_dev(rdev),
2777 			"dma_zalloc_coherent failure, length = %u\n",
2778 			(unsigned)data_len);
2779 		return -ENOMEM;
2780 	}
2781 	req.dest_data_addr = cpu_to_le64(dma_handle);
2782 	req.dest_data_buffer_size = cpu_to_le16(data_len);
2783 	bnxt_re_fill_fw_msg(&fw_msg, (void *)&req, sizeof(req), (void *)&resp,
2784 			    sizeof(resp), DFLT_HWRM_CMD_TIMEOUT);
2785 	rc = en_dev->en_ops->bnxt_send_fw_msg(en_dev, BNXT_ROCE_ULP, &fw_msg);
2786 	if (rc)
2787 		goto out;
2788 
2789 	/* Upload the DSCP-MASK-PRI tuple(s) */
2790 	dscp2pri = (struct bnxt_re_dscp2pri *)kmem;
2791 	for (i = 0; i < le16_to_cpu(resp.entry_cnt) && i < in_count; i++) {
2792 		d2p[i].dscp = dscp2pri->dscp;
2793 		d2p[i].mask = dscp2pri->mask;
2794 		d2p[i].pri = dscp2pri->pri;
2795 		dscp2pri++;
2796 	}
2797 	*count = le16_to_cpu(resp.entry_cnt);
2798 out:
2799 	dma_free_coherent(&en_dev->pdev->dev, data_len, kmem, dma_handle);
2800 	return rc;
2801 }
2802 
bnxt_re_prio_vlan_tx_update(struct bnxt_re_dev * rdev)2803 int bnxt_re_prio_vlan_tx_update(struct bnxt_re_dev *rdev)
2804 {
2805 	/* Remove the VLAN from the GID entry */
2806 	if (rdev->cc_param.disable_prio_vlan_tx)
2807 		rdev->qplib_res.prio = false;
2808 	else
2809 		rdev->qplib_res.prio = true;
2810 
2811 	return bnxt_re_update_gid(rdev);
2812 }
2813 
bnxt_re_set_hwrm_dscp2pri(struct bnxt_re_dev * rdev,struct bnxt_re_dscp2pri * d2p,u16 count,u16 target_id)2814 int bnxt_re_set_hwrm_dscp2pri(struct bnxt_re_dev *rdev,
2815 			      struct bnxt_re_dscp2pri *d2p, u16 count,
2816 			      u16 target_id)
2817 {
2818 	struct bnxt_en_dev *en_dev = rdev->en_dev;
2819 	struct hwrm_queue_dscp2pri_cfg_input req;
2820 	struct hwrm_queue_dscp2pri_cfg_output resp;
2821 	struct bnxt_fw_msg fw_msg;
2822 	struct bnxt_re_dscp2pri *dscp2pri;
2823 	int i, rc, data_len = 3 * 256;
2824 	dma_addr_t dma_handle;
2825 	u8 *kmem;
2826 
2827 	memset(&req, 0, sizeof(req));
2828 	memset(&fw_msg, 0, sizeof(fw_msg));
2829 	bnxt_re_init_hwrm_hdr(rdev, (void *)&req,
2830 			      HWRM_QUEUE_DSCP2PRI_CFG, -1, target_id);
2831 	req.port_id = (target_id == 0xFFFF) ? en_dev->pf_port_id : 1;
2832 
2833 	kmem = dma_alloc_coherent(&en_dev->pdev->dev, data_len, &dma_handle,
2834 				  GFP_KERNEL);
2835 	if (!kmem) {
2836 		dev_err(rdev_to_dev(rdev),
2837 			"dma_alloc_coherent failure, length = %u\n",
2838 			(unsigned)data_len);
2839 		return -ENOMEM;
2840 	}
2841 	req.src_data_addr = cpu_to_le64(dma_handle);
2842 
2843 	/* Download the DSCP-MASK-PRI tuple(s) */
2844 	dscp2pri = (struct bnxt_re_dscp2pri *)kmem;
2845 	for (i = 0; i < count; i++) {
2846 		dscp2pri->dscp = d2p[i].dscp;
2847 		dscp2pri->mask = d2p[i].mask;
2848 		dscp2pri->pri = d2p[i].pri;
2849 		dscp2pri++;
2850 	}
2851 
2852 	req.entry_cnt = cpu_to_le16(count);
2853 	bnxt_re_fill_fw_msg(&fw_msg, (void *)&req, sizeof(req), (void *)&resp,
2854 			    sizeof(resp), DFLT_HWRM_CMD_TIMEOUT);
2855 	rc = en_dev->en_ops->bnxt_send_fw_msg(en_dev, BNXT_ROCE_ULP, &fw_msg);
2856 	dma_free_coherent(&en_dev->pdev->dev, data_len, kmem, dma_handle);
2857 	return rc;
2858 }
2859 
bnxt_re_query_hwrm_qportcfg(struct bnxt_re_dev * rdev,struct bnxt_re_tc_rec * tc_rec,u16 tid)2860 int bnxt_re_query_hwrm_qportcfg(struct bnxt_re_dev *rdev,
2861 			struct bnxt_re_tc_rec *tc_rec, u16 tid)
2862 {
2863 	u8 max_tc, tc, *qptr, *type_ptr0, *type_ptr1;
2864 	struct hwrm_queue_qportcfg_output resp = {0};
2865 	struct hwrm_queue_qportcfg_input req = {0};
2866 	struct bnxt_en_dev *en_dev = rdev->en_dev;
2867 	struct bnxt_fw_msg fw_msg;
2868 	bool def_init = false;
2869 	u8 *tmp_type;
2870 	u8 cos_id;
2871 	int rc;
2872 
2873 	memset(&fw_msg, 0, sizeof(fw_msg));
2874 	bnxt_re_init_hwrm_hdr(rdev, (void *)&req, HWRM_QUEUE_QPORTCFG,
2875 			      -1, tid);
2876 	req.port_id = (tid == 0xFFFF) ? en_dev->pf_port_id : 1;
2877 	if (BNXT_EN_ASYM_Q(en_dev))
2878 		req.flags = htole32(HWRM_QUEUE_QPORTCFG_INPUT_FLAGS_PATH_RX);
2879 
2880 	bnxt_re_fill_fw_msg(&fw_msg, (void *)&req, sizeof(req), (void *)&resp,
2881 			    sizeof(resp), DFLT_HWRM_CMD_TIMEOUT);
2882 	rc = en_dev->en_ops->bnxt_send_fw_msg(en_dev, BNXT_ROCE_ULP, &fw_msg);
2883 	if (rc)
2884 		return rc;
2885 
2886 	if (!resp.max_configurable_queues)
2887 		return -EINVAL;
2888 
2889 	max_tc = resp.max_configurable_queues;
2890 	tc_rec->max_tc = max_tc;
2891 
2892 	if (resp.queue_cfg_info & HWRM_QUEUE_QPORTCFG_OUTPUT_QUEUE_CFG_INFO_USE_PROFILE_TYPE)
2893 		tc_rec->serv_type_enabled = true;
2894 
2895 	qptr = &resp.queue_id0;
2896 	type_ptr0 = &resp.queue_id0_service_profile_type;
2897 	type_ptr1 = &resp.queue_id1_service_profile_type;
2898 	for (tc = 0; tc < max_tc; tc++) {
2899 		tmp_type = tc ? type_ptr1 + (tc - 1) : type_ptr0;
2900 
2901 		cos_id = *qptr++;
2902 		/* RoCE CoS queue is the first cos queue.
2903 		 * For MP12 and MP17 order is 405 and 141015.
2904 		 */
2905 		if (is_bnxt_roce_queue(rdev, *qptr, *tmp_type)) {
2906 			tc_rec->cos_id_roce = cos_id;
2907 			tc_rec->tc_roce = tc;
2908 		} else if (is_bnxt_cnp_queue(rdev, *qptr, *tmp_type)) {
2909 			tc_rec->cos_id_cnp = cos_id;
2910 			tc_rec->tc_cnp = tc;
2911 		} else if (!def_init) {
2912 			def_init = true;
2913 			tc_rec->tc_def = tc;
2914 			tc_rec->cos_id_def = cos_id;
2915 		}
2916 		qptr++;
2917 	}
2918 
2919 	return rc;
2920 }
2921 
bnxt_re_hwrm_cos2bw_qcfg(struct bnxt_re_dev * rdev,u16 target_id,struct bnxt_re_cos2bw_cfg * cfg)2922 int bnxt_re_hwrm_cos2bw_qcfg(struct bnxt_re_dev *rdev, u16 target_id,
2923 			     struct bnxt_re_cos2bw_cfg *cfg)
2924 {
2925 	struct bnxt_en_dev *en_dev = rdev->en_dev;
2926 	struct hwrm_queue_cos2bw_qcfg_output resp;
2927 	struct hwrm_queue_cos2bw_qcfg_input req = {0};
2928 	struct bnxt_fw_msg fw_msg;
2929 	int rc, indx;
2930 	void *data;
2931 
2932 	memset(&fw_msg, 0, sizeof(fw_msg));
2933 	bnxt_re_init_hwrm_hdr(rdev, (void *)&req,
2934 			      HWRM_QUEUE_COS2BW_QCFG, -1, target_id);
2935 	req.port_id = (target_id == 0xFFFF) ? en_dev->pf_port_id : 1;
2936 
2937 	bnxt_re_fill_fw_msg(&fw_msg, (void *)&req, sizeof(req), (void *)&resp,
2938 			    sizeof(resp), DFLT_HWRM_CMD_TIMEOUT);
2939 	rc = en_dev->en_ops->bnxt_send_fw_msg(en_dev, BNXT_ROCE_ULP, &fw_msg);
2940 	if (rc)
2941 		return rc;
2942 	data = &resp.queue_id0 + offsetof(struct bnxt_re_cos2bw_cfg,
2943 					  queue_id);
2944 	for (indx = 0; indx < 8; indx++, data += (sizeof(cfg->cfg))) {
2945 		memcpy(&cfg->cfg, data, sizeof(cfg->cfg));
2946 		if (indx == 0)
2947 			cfg->queue_id = resp.queue_id0;
2948 		cfg++;
2949 	}
2950 
2951 	return rc;
2952 }
2953 
bnxt_re_hwrm_cos2bw_cfg(struct bnxt_re_dev * rdev,u16 target_id,struct bnxt_re_cos2bw_cfg * cfg)2954 int bnxt_re_hwrm_cos2bw_cfg(struct bnxt_re_dev *rdev, u16 target_id,
2955 			    struct bnxt_re_cos2bw_cfg *cfg)
2956 {
2957 	struct bnxt_en_dev *en_dev = rdev->en_dev;
2958 	struct hwrm_queue_cos2bw_cfg_input req = {0};
2959 	struct hwrm_queue_cos2bw_cfg_output resp = {0};
2960 	struct bnxt_fw_msg fw_msg;
2961 	void *data;
2962 	int indx;
2963 	int rc;
2964 
2965 	memset(&fw_msg, 0, sizeof(fw_msg));
2966 	bnxt_re_init_hwrm_hdr(rdev, (void *)&req,
2967 			      HWRM_QUEUE_COS2BW_CFG, -1, target_id);
2968 	req.port_id = (target_id == 0xFFFF) ? en_dev->pf_port_id : 1;
2969 
2970 	/* Chimp wants enable bit to retain previous
2971 	 * config done by L2 driver
2972 	 */
2973 	for (indx = 0; indx < 8; indx++) {
2974 		if (cfg[indx].queue_id < 40) {
2975 			req.enables |= cpu_to_le32(
2976 				HWRM_QUEUE_COS2BW_CFG_INPUT_ENABLES_COS_QUEUE_ID0_VALID <<
2977 				indx);
2978 		}
2979 
2980 		data = (char *)&req.unused_0 + indx * (sizeof(*cfg) - 4);
2981 		memcpy(data, &cfg[indx].queue_id, sizeof(*cfg) - 4);
2982 		if (indx == 0) {
2983 			req.queue_id0 = cfg[0].queue_id;
2984 			req.unused_0 = 0;
2985 		}
2986 	}
2987 
2988 	memset(&resp, 0, sizeof(resp));
2989 	bnxt_re_fill_fw_msg(&fw_msg, (void *)&req, sizeof(req), (void *)&resp,
2990 			    sizeof(resp), DFLT_HWRM_CMD_TIMEOUT);
2991 	rc = en_dev->en_ops->bnxt_send_fw_msg(en_dev, BNXT_ROCE_ULP, &fw_msg);
2992 	return rc;
2993 }
2994 
bnxt_re_host_pf_id_query(struct bnxt_re_dev * rdev,struct bnxt_qplib_query_fn_info * fn_info,u32 * pf_mask,u32 * first_pf)2995 int bnxt_re_host_pf_id_query(struct bnxt_re_dev *rdev,
2996 			     struct bnxt_qplib_query_fn_info *fn_info,
2997 			     u32 *pf_mask, u32 *first_pf)
2998 {
2999 	struct hwrm_func_host_pf_ids_query_output resp = {0};
3000 	struct hwrm_func_host_pf_ids_query_input req;
3001 	struct bnxt_en_dev *en_dev = rdev->en_dev;
3002 	struct bnxt_fw_msg fw_msg;
3003 	int rc;
3004 
3005 	memset(&fw_msg, 0, sizeof(fw_msg));
3006 	memset(&req, 0, sizeof(req));
3007 	bnxt_re_init_hwrm_hdr(rdev, (void *)&req,
3008 			      HWRM_FUNC_HOST_PF_IDS_QUERY, -1, -1);
3009 	/* To query the info from the host EPs */
3010 	switch (fn_info->host) {
3011 		case HWRM_FUNC_HOST_PF_IDS_QUERY_INPUT_HOST_SOC:
3012 		case HWRM_FUNC_HOST_PF_IDS_QUERY_INPUT_HOST_EP_0:
3013 		case HWRM_FUNC_HOST_PF_IDS_QUERY_INPUT_HOST_EP_1:
3014 		case HWRM_FUNC_HOST_PF_IDS_QUERY_INPUT_HOST_EP_2:
3015 		case HWRM_FUNC_HOST_PF_IDS_QUERY_INPUT_HOST_EP_3:
3016 			req.host = fn_info->host;
3017 		break;
3018 		default:
3019 			req.host = HWRM_FUNC_HOST_PF_IDS_QUERY_INPUT_HOST_EP_0;
3020 		break;
3021 	}
3022 
3023 	req.filter = fn_info->filter;
3024 	if (req.filter > HWRM_FUNC_HOST_PF_IDS_QUERY_INPUT_FILTER_ROCE)
3025 		req.filter = HWRM_FUNC_HOST_PF_IDS_QUERY_INPUT_FILTER_ALL;
3026 
3027 	bnxt_re_fill_fw_msg(&fw_msg, (void *)&req, sizeof(req), (void *)&resp,
3028 			    sizeof(resp), DFLT_HWRM_CMD_TIMEOUT);
3029 	rc = en_dev->en_ops->bnxt_send_fw_msg(en_dev, BNXT_ROCE_ULP, &fw_msg);
3030 
3031 
3032 	*first_pf = le16_to_cpu(resp.first_pf_id);
3033 	*pf_mask = le16_to_cpu(resp.pf_ordinal_mask);
3034 
3035 	return rc;
3036 }
3037 
bnxt_re_put_stats_ctx(struct bnxt_re_dev * rdev)3038 static void bnxt_re_put_stats_ctx(struct bnxt_re_dev *rdev)
3039 {
3040 	struct bnxt_qplib_ctx *hctx;
3041 	struct bnxt_qplib_res *res;
3042 	u16 tid = 0xffff;
3043 
3044 	res = &rdev->qplib_res;
3045 	hctx = res->hctx;
3046 
3047 	if (test_and_clear_bit(BNXT_RE_FLAG_STATS_CTX_ALLOC, &rdev->flags)) {
3048 		bnxt_re_net_stats_ctx_free(rdev, hctx->stats.fw_id, tid);
3049 		bnxt_qplib_free_stat_mem(res, &hctx->stats);
3050 	}
3051 }
3052 
bnxt_re_put_stats2_ctx(struct bnxt_re_dev * rdev)3053 static void bnxt_re_put_stats2_ctx(struct bnxt_re_dev *rdev)
3054 {
3055 	test_and_clear_bit(BNXT_RE_FLAG_STATS_CTX2_ALLOC, &rdev->flags);
3056 }
3057 
bnxt_re_get_stats_ctx(struct bnxt_re_dev * rdev)3058 static int bnxt_re_get_stats_ctx(struct bnxt_re_dev *rdev)
3059 {
3060 	struct bnxt_qplib_ctx *hctx;
3061 	struct bnxt_qplib_res *res;
3062 	u16 tid = 0xffff;
3063 	int rc;
3064 
3065 	res = &rdev->qplib_res;
3066 	hctx = res->hctx;
3067 
3068 	rc = bnxt_qplib_alloc_stat_mem(res->pdev, rdev->chip_ctx, &hctx->stats);
3069 	if (rc)
3070 		return -ENOMEM;
3071 	rc = bnxt_re_net_stats_ctx_alloc(rdev, tid);
3072 	if (rc)
3073 		goto free_stat_mem;
3074 	set_bit(BNXT_RE_FLAG_STATS_CTX_ALLOC, &rdev->flags);
3075 
3076 	return 0;
3077 
3078 free_stat_mem:
3079 	bnxt_qplib_free_stat_mem(res, &hctx->stats);
3080 
3081 	return rc;
3082 }
3083 
bnxt_re_update_dev_attr(struct bnxt_re_dev * rdev)3084 static int bnxt_re_update_dev_attr(struct bnxt_re_dev *rdev)
3085 {
3086 	int rc;
3087 
3088 	rc = bnxt_qplib_get_dev_attr(&rdev->rcfw);
3089 	if (rc)
3090 		return rc;
3091 	if (!bnxt_re_check_min_attr(rdev))
3092 		return -EINVAL;
3093 	return 0;
3094 }
3095 
bnxt_re_free_tbls(struct bnxt_re_dev * rdev)3096 static void bnxt_re_free_tbls(struct bnxt_re_dev *rdev)
3097 {
3098 	bnxt_qplib_clear_tbls(&rdev->qplib_res);
3099 	bnxt_qplib_free_tbls(&rdev->qplib_res);
3100 }
3101 
bnxt_re_alloc_init_tbls(struct bnxt_re_dev * rdev)3102 static int bnxt_re_alloc_init_tbls(struct bnxt_re_dev *rdev)
3103 {
3104 	struct bnxt_qplib_chip_ctx *chip_ctx = rdev->chip_ctx;
3105 	u8 pppp_factor = 0;
3106 	int rc;
3107 
3108 	 /*
3109 	  * TODO: Need a better mechanism for spreading of the
3110 	  * 512 extended PPP pages. For now, spreading it
3111 	  * based on port_count
3112 	  */
3113 	if (_is_chip_p7(chip_ctx) && chip_ctx->modes.db_push)
3114 		pppp_factor = rdev->en_dev->port_count;
3115 	rc = bnxt_qplib_alloc_tbls(&rdev->qplib_res, pppp_factor);
3116 	if (rc)
3117 		return rc;
3118 	bnxt_qplib_init_tbls(&rdev->qplib_res);
3119 	set_bit(BNXT_RE_FLAG_TBLS_ALLOCINIT, &rdev->flags);
3120 
3121 	return 0;
3122 }
3123 
bnxt_re_clean_nqs(struct bnxt_re_dev * rdev)3124 static void bnxt_re_clean_nqs(struct bnxt_re_dev *rdev)
3125 {
3126 	struct bnxt_qplib_nq *nq;
3127 	int i;
3128 
3129 	if (!rdev->nqr.max_init)
3130 		return;
3131 
3132 	for (i = (rdev->nqr.max_init - 1) ; i >= 0; i--) {
3133 		nq = &rdev->nqr.nq[i];
3134 		bnxt_qplib_disable_nq(nq);
3135 		bnxt_re_net_ring_free(rdev, nq->ring_id);
3136 		bnxt_qplib_free_nq_mem(nq);
3137 	}
3138 	rdev->nqr.max_init = 0;
3139 }
3140 
bnxt_re_setup_nqs(struct bnxt_re_dev * rdev)3141 static int bnxt_re_setup_nqs(struct bnxt_re_dev *rdev)
3142 {
3143 	struct bnxt_re_ring_attr rattr = {};
3144 	struct bnxt_qplib_nq *nq;
3145 	int rc, i;
3146 	int depth;
3147 	u32 offt;
3148 	u16 vec;
3149 
3150 	mutex_init(&rdev->nqr.load_lock);
3151 	/*
3152 	 * TODO: Optimize the depth based on the
3153 	 * number of NQs.
3154 	 */
3155 	depth = BNXT_QPLIB_NQE_MAX_CNT;
3156 	for (i = 0; i < rdev->nqr.num_msix - 1; i++) {
3157 		nq = &rdev->nqr.nq[i];
3158 		vec = rdev->nqr.msix_entries[i + 1].vector;
3159 		offt = rdev->nqr.msix_entries[i + 1].db_offset;
3160 		nq->hwq.max_elements = depth;
3161 		rc = bnxt_qplib_alloc_nq_mem(&rdev->qplib_res, nq);
3162 		if (rc) {
3163 			dev_err(rdev_to_dev(rdev),
3164 				"Failed to get mem for NQ %d, rc = 0x%x",
3165 				i, rc);
3166 			goto fail_mem;
3167 		}
3168 
3169 		rattr.dma_arr = nq->hwq.pbl[PBL_LVL_0].pg_map_arr;
3170 		rattr.pages = nq->hwq.pbl[rdev->nqr.nq[i].hwq.level].pg_count;
3171 		rattr.type = bnxt_re_get_rtype(rdev);
3172 		rattr.mode = HWRM_RING_ALLOC_INPUT_INT_MODE_MSIX;
3173 		rattr.depth = nq->hwq.max_elements - 1;
3174 		rattr.lrid = rdev->nqr.msix_entries[i + 1].ring_idx;
3175 
3176 		/* Set DBR pacing capability on the first NQ ring only */
3177 		if (!i && bnxt_qplib_dbr_pacing_ext_en(rdev->chip_ctx))
3178 			rattr.flags = HWRM_RING_ALLOC_INPUT_FLAGS_NQ_DBR_PACING;
3179 		else
3180 			rattr.flags = 0;
3181 
3182 		rc = bnxt_re_net_ring_alloc(rdev, &rattr, &nq->ring_id);
3183 		if (rc) {
3184 			nq->ring_id = 0xffff; /* Invalid ring-id */
3185 			dev_err(rdev_to_dev(rdev),
3186 				"Failed to get fw id for NQ %d, rc = 0x%x",
3187 				i, rc);
3188 			goto fail_ring;
3189 		}
3190 
3191 		rc = bnxt_qplib_enable_nq(nq, i, vec, offt,
3192 					  &bnxt_re_cqn_handler,
3193 					  &bnxt_re_srqn_handler);
3194 		if (rc) {
3195 			dev_err(rdev_to_dev(rdev),
3196 				"Failed to enable NQ %d, rc = 0x%x", i, rc);
3197 			goto fail_en;
3198 		}
3199 	}
3200 
3201 	rdev->nqr.max_init = i;
3202 	return 0;
3203 fail_en:
3204 	/* *nq was i'th nq */
3205 	bnxt_re_net_ring_free(rdev, nq->ring_id);
3206 fail_ring:
3207 	bnxt_qplib_free_nq_mem(nq);
3208 fail_mem:
3209 	rdev->nqr.max_init = i;
3210 	return rc;
3211 }
3212 
bnxt_re_sysfs_destroy_file(struct bnxt_re_dev * rdev)3213 static void bnxt_re_sysfs_destroy_file(struct bnxt_re_dev *rdev)
3214 {
3215 	int i;
3216 
3217 	for (i = 0; i < ARRAY_SIZE(bnxt_re_attributes); i++)
3218 		device_remove_file(&rdev->ibdev.dev, bnxt_re_attributes[i]);
3219 }
3220 
bnxt_re_sysfs_create_file(struct bnxt_re_dev * rdev)3221 static int bnxt_re_sysfs_create_file(struct bnxt_re_dev *rdev)
3222 {
3223 	int i, j, rc = 0;
3224 
3225 	for (i = 0; i < ARRAY_SIZE(bnxt_re_attributes); i++) {
3226 		rc = device_create_file(&rdev->ibdev.dev,
3227 					bnxt_re_attributes[i]);
3228 		if (rc) {
3229 			dev_err(rdev_to_dev(rdev),
3230 				"Failed to create IB sysfs with rc = 0x%x", rc);
3231 			/* Must clean up all created device files */
3232 			for (j = 0; j < i; j++)
3233 				device_remove_file(&rdev->ibdev.dev,
3234 						   bnxt_re_attributes[j]);
3235 			clear_bit(BNXT_RE_FLAG_IBDEV_REGISTERED, &rdev->flags);
3236 			ib_unregister_device(&rdev->ibdev);
3237 			return 1;
3238 		}
3239 	}
3240 	return 0;
3241 }
3242 
3243 /* worker thread for polling periodic events. Now used for QoS programming*/
bnxt_re_worker(struct work_struct * work)3244 static void bnxt_re_worker(struct work_struct *work)
3245 {
3246 	struct bnxt_re_dev *rdev = container_of(work, struct bnxt_re_dev,
3247 						worker.work);
3248 	int rc;
3249 
3250 	/* QoS is in 30s cadence for PFs*/
3251 	if (!rdev->is_virtfn && !rdev->worker_30s--)
3252 		rdev->worker_30s = 30;
3253 	/* Use trylock for  bnxt_re_dev_lock as this can be
3254 	 * held for long time by debugfs show path while issuing
3255 	 * HWRMS. If the debugfs name update is not done in this
3256 	 * iteration, the driver will check for the same in the
3257 	 * next schedule of the worker i.e after 1 sec.
3258 	 */
3259 	if (mutex_trylock(&bnxt_re_dev_lock))
3260 		mutex_unlock(&bnxt_re_dev_lock);
3261 
3262 	if (!rdev->stats.stats_query_sec)
3263 		goto resched;
3264 
3265 	if (test_bit(BNXT_RE_FLAG_ISSUE_CFA_FLOW_STATS, &rdev->flags) &&
3266 	    (rdev->is_virtfn ||
3267 	    !_is_ext_stats_supported(rdev->dev_attr->dev_cap_flags))) {
3268 		if (!(rdev->stats.stats_query_counter++ %
3269 		      rdev->stats.stats_query_sec)) {
3270 			rc = bnxt_re_get_qos_stats(rdev);
3271 			if (rc && rc != -ENOMEM)
3272 				clear_bit(BNXT_RE_FLAG_ISSUE_CFA_FLOW_STATS,
3273 					  &rdev->flags);
3274 			}
3275 	}
3276 
3277 resched:
3278 	schedule_delayed_work(&rdev->worker, msecs_to_jiffies(1000));
3279 }
3280 
bnxt_re_alloc_dbr_sw_stats_mem(struct bnxt_re_dev * rdev)3281 static int bnxt_re_alloc_dbr_sw_stats_mem(struct bnxt_re_dev *rdev)
3282 {
3283 	if (!(rdev->dbr_drop_recov || rdev->dbr_pacing))
3284 		return 0;
3285 
3286 	rdev->dbr_sw_stats = kzalloc(sizeof(*rdev->dbr_sw_stats), GFP_KERNEL);
3287 	if (!rdev->dbr_sw_stats)
3288 		return -ENOMEM;
3289 
3290 	return 0;
3291 }
3292 
bnxt_re_free_dbr_sw_stats_mem(struct bnxt_re_dev * rdev)3293 static void bnxt_re_free_dbr_sw_stats_mem(struct bnxt_re_dev *rdev)
3294 {
3295 	kfree(rdev->dbr_sw_stats);
3296 	rdev->dbr_sw_stats = NULL;
3297 }
3298 
bnxt_re_initialize_dbr_drop_recov(struct bnxt_re_dev * rdev)3299 static int bnxt_re_initialize_dbr_drop_recov(struct bnxt_re_dev *rdev)
3300 {
3301 	rdev->dbr_drop_recov_wq =
3302 		create_singlethread_workqueue("bnxt_re_dbr_drop_recov");
3303 	if (!rdev->dbr_drop_recov_wq) {
3304 		dev_err(rdev_to_dev(rdev), "DBR Drop Revov wq alloc failed!");
3305 		return -EINVAL;
3306 	}
3307 	rdev->dbr_drop_recov = true;
3308 
3309 	/* Enable configfs setting dbr_drop_recov by default*/
3310 	rdev->user_dbr_drop_recov = true;
3311 
3312 	rdev->user_dbr_drop_recov_timeout = BNXT_RE_DBR_RECOV_USERLAND_TIMEOUT;
3313 	return 0;
3314 }
3315 
bnxt_re_deinitialize_dbr_drop_recov(struct bnxt_re_dev * rdev)3316 static void bnxt_re_deinitialize_dbr_drop_recov(struct bnxt_re_dev *rdev)
3317 {
3318 	if (rdev->dbr_drop_recov_wq) {
3319 		flush_workqueue(rdev->dbr_drop_recov_wq);
3320 		destroy_workqueue(rdev->dbr_drop_recov_wq);
3321 		rdev->dbr_drop_recov_wq = NULL;
3322 	}
3323 	rdev->dbr_drop_recov = false;
3324 }
3325 
bnxt_re_initialize_dbr_pacing(struct bnxt_re_dev * rdev)3326 static int bnxt_re_initialize_dbr_pacing(struct bnxt_re_dev *rdev)
3327 {
3328 	int rc;
3329 
3330 	/* Allocate a page for app use */
3331 	rdev->dbr_page = (void *)__get_free_page(GFP_KERNEL);
3332 	if (!rdev->dbr_page) {
3333 		dev_err(rdev_to_dev(rdev), "DBR page allocation failed!");
3334 		return -ENOMEM;
3335 	}
3336 	memset((u8 *)rdev->dbr_page, 0, PAGE_SIZE);
3337 	rdev->qplib_res.pacing_data = (struct bnxt_qplib_db_pacing_data *)rdev->dbr_page;
3338 	rc = bnxt_re_hwrm_dbr_pacing_qcfg(rdev);
3339 	if (rc) {
3340 		dev_err(rdev_to_dev(rdev),
3341 			"Failed to query dbr pacing config %d\n", rc);
3342 		goto fail;
3343 	}
3344 	/* Create a work queue for scheduling dbq event */
3345 	rdev->dbq_wq = create_singlethread_workqueue("bnxt_re_dbq");
3346 	if (!rdev->dbq_wq) {
3347 		dev_err(rdev_to_dev(rdev), "DBQ wq alloc failed!");
3348 		rc = -ENOMEM;
3349 		goto fail;
3350 	}
3351 	/* MAP grc window 2 for reading db fifo depth */
3352 	writel_fbsd(rdev->en_dev->softc,  BNXT_GRCPF_REG_WINDOW_BASE_OUT + 4, 0,
3353 			rdev->chip_ctx->dbr_stat_db_fifo & BNXT_GRC_BASE_MASK);
3354 	rdev->dbr_db_fifo_reg_off =
3355 		(rdev->chip_ctx->dbr_stat_db_fifo & BNXT_GRC_OFFSET_MASK) +
3356 		0x2000;
3357 	rdev->qplib_res.pacing_data->grc_reg_offset = rdev->dbr_db_fifo_reg_off;
3358 
3359 	rdev->dbr_bar_addr =
3360 		pci_resource_start(rdev->qplib_res.pdev, 0) +
3361 		rdev->dbr_db_fifo_reg_off;
3362 
3363 	/* Percentage of DB FIFO */
3364 	rdev->dbq_watermark = BNXT_RE_PACING_DBQ_THRESHOLD;
3365 	rdev->pacing_en_int_th = BNXT_RE_PACING_EN_INT_THRESHOLD;
3366 	rdev->pacing_algo_th = BNXT_RE_PACING_ALGO_THRESHOLD;
3367 	rdev->dbq_pacing_time = BNXT_RE_DBR_INT_TIME;
3368 	rdev->dbr_def_do_pacing = BNXT_RE_DBR_DO_PACING_NO_CONGESTION;
3369 	rdev->do_pacing_save = rdev->dbr_def_do_pacing;
3370 	bnxt_re_set_default_pacing_data(rdev);
3371 	dev_dbg(rdev_to_dev(rdev), "Initialized db pacing\n");
3372 
3373 	return 0;
3374 fail:
3375 	free_page((u64)rdev->dbr_page);
3376 	rdev->dbr_page = NULL;
3377 	return rc;
3378 }
3379 
bnxt_re_deinitialize_dbr_pacing(struct bnxt_re_dev * rdev)3380 static void bnxt_re_deinitialize_dbr_pacing(struct bnxt_re_dev *rdev)
3381 {
3382 	if (rdev->dbq_wq)
3383 		flush_workqueue(rdev->dbq_wq);
3384 
3385 	cancel_work_sync(&rdev->dbq_fifo_check_work);
3386 	cancel_delayed_work_sync(&rdev->dbq_pacing_work);
3387 
3388 	if (rdev->dbq_wq) {
3389 		destroy_workqueue(rdev->dbq_wq);
3390 		rdev->dbq_wq = NULL;
3391 	}
3392 
3393 	if (rdev->dbr_page)
3394 		free_page((u64)rdev->dbr_page);
3395 	rdev->dbr_page = NULL;
3396 	rdev->dbr_pacing = false;
3397 }
3398 
3399 /* enable_dbr_pacing needs to be done only for older FWs
3400  * where host selects primary function. ie. pacing_ext
3401  * flags is not set.
3402  */
bnxt_re_enable_dbr_pacing(struct bnxt_re_dev * rdev)3403 int bnxt_re_enable_dbr_pacing(struct bnxt_re_dev *rdev)
3404 {
3405 	struct bnxt_qplib_nq *nq;
3406 
3407 	nq = &rdev->nqr.nq[0];
3408 	rdev->dbq_nq_id = nq->ring_id;
3409 
3410 	if (!bnxt_qplib_dbr_pacing_ext_en(rdev->chip_ctx) &&
3411 	    bnxt_qplib_dbr_pacing_is_primary_pf(rdev->chip_ctx)) {
3412 		if (bnxt_re_hwrm_dbr_pacing_cfg(rdev, true)) {
3413 			dev_err(rdev_to_dev(rdev),
3414 					"Failed to set dbr pacing config\n");
3415 			return -EIO;
3416 		}
3417 		/* MAP grc window 8 for ARMing the NQ DBQ */
3418 		writel_fbsd(rdev->en_dev->softc, BNXT_GRCPF_REG_WINDOW_BASE_OUT + 28 , 0,
3419 			    rdev->chip_ctx->dbr_aeq_arm_reg & BNXT_GRC_BASE_MASK);
3420 		rdev->dbr_aeq_arm_reg_off =
3421 			(rdev->chip_ctx->dbr_aeq_arm_reg &
3422 			 BNXT_GRC_OFFSET_MASK) + 0x8000;
3423 		writel_fbsd(rdev->en_dev->softc, rdev->dbr_aeq_arm_reg_off , 0, 1);
3424 	}
3425 
3426 	return 0;
3427 }
3428 
3429 /* disable_dbr_pacing needs to be done only for older FWs
3430  * where host selects primary function. ie. pacing_ext
3431  * flags is not set.
3432  */
3433 
bnxt_re_disable_dbr_pacing(struct bnxt_re_dev * rdev)3434 int bnxt_re_disable_dbr_pacing(struct bnxt_re_dev *rdev)
3435 {
3436 	int rc = 0;
3437 
3438 	if (!bnxt_qplib_dbr_pacing_ext_en(rdev->chip_ctx) &&
3439 	    bnxt_qplib_dbr_pacing_is_primary_pf(rdev->chip_ctx))
3440 		rc = bnxt_re_hwrm_dbr_pacing_cfg(rdev, false);
3441 
3442 	return rc;
3443 }
3444 
bnxt_re_ib_uninit(struct bnxt_re_dev * rdev)3445 static void bnxt_re_ib_uninit(struct bnxt_re_dev *rdev)
3446 {
3447 	if (test_bit(BNXT_RE_FLAG_IBDEV_REGISTERED, &rdev->flags)) {
3448 		bnxt_re_sysfs_destroy_file(rdev);
3449 		/* Cleanup ib dev */
3450 		ib_unregister_device(&rdev->ibdev);
3451 		clear_bit(BNXT_RE_FLAG_IBDEV_REGISTERED, &rdev->flags);
3452 		return;
3453 	}
3454 }
3455 
bnxt_re_dev_uninit(struct bnxt_re_dev * rdev,u8 op_type)3456 static void bnxt_re_dev_uninit(struct bnxt_re_dev *rdev, u8 op_type)
3457 {
3458 	struct bnxt_qplib_dpi *kdpi;
3459 	int rc, wait_count = BNXT_RE_RES_FREE_WAIT_COUNT;
3460 
3461 	bnxt_re_net_unregister_async_event(rdev);
3462 
3463 	bnxt_re_put_stats2_ctx(rdev);
3464 	if (test_and_clear_bit(BNXT_RE_FLAG_DEV_LIST_INITIALIZED,
3465 			       &rdev->flags)) {
3466 		/* did the caller hold the lock? */
3467 		mutex_lock(&bnxt_re_dev_lock);
3468 		list_del_rcu(&rdev->list);
3469 		mutex_unlock(&bnxt_re_dev_lock);
3470 	}
3471 
3472 	bnxt_re_uninit_resolve_wq(rdev);
3473 	bnxt_re_uninit_dcb_wq(rdev);
3474 	bnxt_re_uninit_aer_wq(rdev);
3475 
3476 	bnxt_re_deinitialize_dbr_drop_recov(rdev);
3477 
3478 	if (bnxt_qplib_dbr_pacing_en(rdev->chip_ctx))
3479 		(void)bnxt_re_disable_dbr_pacing(rdev);
3480 
3481 	if (test_and_clear_bit(BNXT_RE_FLAG_WORKER_REG, &rdev->flags)) {
3482 		cancel_delayed_work_sync(&rdev->worker);
3483 	}
3484 
3485 	/* Wait for ULPs to release references */
3486 	while (atomic_read(&rdev->stats.rsors.cq_count) && --wait_count)
3487 		usleep_range(500, 1000);
3488 	if (!wait_count)
3489 		dev_err(rdev_to_dev(rdev),
3490 			"CQ resources not freed by stack, count = 0x%x",
3491 			atomic_read(&rdev->stats.rsors.cq_count));
3492 
3493 	kdpi = &rdev->dpi_privileged;
3494 	if (kdpi->umdbr) { /* kernel DPI was allocated with success */
3495 		(void)bnxt_qplib_dealloc_dpi(&rdev->qplib_res, kdpi);
3496 		/*
3497 		 * Driver just need to know no command had failed
3498 		 * during driver load sequence and below command is
3499 		 * required indeed. Piggybacking dpi allocation status.
3500 		 */
3501 	}
3502 
3503 	/* Protect the device uninitialization and start_irq/stop_irq L2
3504 	 * callbacks with rtnl lock to avoid race condition between these calls
3505 	 */
3506 	rtnl_lock();
3507 	if (test_and_clear_bit(BNXT_RE_FLAG_SETUP_NQ, &rdev->flags))
3508 		bnxt_re_clean_nqs(rdev);
3509 	rtnl_unlock();
3510 
3511 	if (test_and_clear_bit(BNXT_RE_FLAG_TBLS_ALLOCINIT, &rdev->flags))
3512 		bnxt_re_free_tbls(rdev);
3513 	if (test_and_clear_bit(BNXT_RE_FLAG_RCFW_CHANNEL_INIT, &rdev->flags)) {
3514 		rc = bnxt_qplib_deinit_rcfw(&rdev->rcfw);
3515 		if (rc)
3516 			dev_warn(rdev_to_dev(rdev),
3517 				 "Failed to deinitialize fw, rc = 0x%x", rc);
3518 	}
3519 
3520 	bnxt_re_put_stats_ctx(rdev);
3521 
3522 	if (test_and_clear_bit(BNXT_RE_FLAG_ALLOC_CTX, &rdev->flags))
3523 		bnxt_qplib_free_hwctx(&rdev->qplib_res);
3524 
3525 	rtnl_lock();
3526 	if (test_and_clear_bit(BNXT_RE_FLAG_RCFW_CHANNEL_EN, &rdev->flags))
3527 		bnxt_qplib_disable_rcfw_channel(&rdev->rcfw);
3528 
3529 	if (rdev->dbr_pacing)
3530 		bnxt_re_deinitialize_dbr_pacing(rdev);
3531 
3532 	bnxt_re_free_dbr_sw_stats_mem(rdev);
3533 
3534 	if (test_and_clear_bit(BNXT_RE_FLAG_NET_RING_ALLOC, &rdev->flags))
3535 		bnxt_re_net_ring_free(rdev, rdev->rcfw.creq.ring_id);
3536 
3537 	if (test_and_clear_bit(BNXT_RE_FLAG_ALLOC_RCFW, &rdev->flags))
3538 		bnxt_qplib_free_rcfw_channel(&rdev->qplib_res);
3539 
3540 	if (test_and_clear_bit(BNXT_RE_FLAG_GOT_MSIX, &rdev->flags))
3541 		bnxt_re_free_msix(rdev);
3542 	rtnl_unlock();
3543 
3544 	bnxt_re_destroy_chip_ctx(rdev);
3545 
3546 	if (op_type != BNXT_RE_PRE_RECOVERY_REMOVE) {
3547 		if (test_and_clear_bit(BNXT_RE_FLAG_NETDEV_REGISTERED,
3548 				       &rdev->flags))
3549 			bnxt_re_unregister_netdev(rdev);
3550 	}
3551 }
3552 
bnxt_re_dev_init(struct bnxt_re_dev * rdev,u8 op_type)3553 static int bnxt_re_dev_init(struct bnxt_re_dev *rdev, u8 op_type)
3554 {
3555 	struct bnxt_re_ring_attr rattr = {};
3556 	struct bnxt_qplib_creq_ctx *creq;
3557 	int vec, offset;
3558 	int rc = 0;
3559 
3560 	if (op_type != BNXT_RE_POST_RECOVERY_INIT) {
3561 		/* Registered a new RoCE device instance to netdev */
3562 		rc = bnxt_re_register_netdev(rdev);
3563 		if (rc)
3564 			return -EINVAL;
3565 	}
3566 	set_bit(BNXT_RE_FLAG_NETDEV_REGISTERED, &rdev->flags);
3567 
3568 	rc = bnxt_re_setup_chip_ctx(rdev);
3569 	if (rc) {
3570 		dev_err(rdev_to_dev(rdev), "Failed to get chip context rc 0x%x", rc);
3571 		bnxt_re_unregister_netdev(rdev);
3572 		clear_bit(BNXT_RE_FLAG_NETDEV_REGISTERED, &rdev->flags);
3573 		rc = -EINVAL;
3574 		return rc;
3575 	}
3576 
3577 	/* Protect the device initialization and start_irq/stop_irq L2 callbacks
3578 	 * with rtnl lock to avoid race condition between these calls
3579 	 */
3580 	rtnl_lock();
3581 	rc = bnxt_re_request_msix(rdev);
3582 	if (rc) {
3583 		dev_err(rdev_to_dev(rdev),
3584 			"Requesting MSI-X vectors failed with rc = 0x%x", rc);
3585 		rc = -EINVAL;
3586 		goto release_rtnl;
3587 	}
3588 	set_bit(BNXT_RE_FLAG_GOT_MSIX, &rdev->flags);
3589 
3590 	/* Establish RCFW Communication Channel to initialize the context
3591 	   memory for the function and all child VFs */
3592 	rc = bnxt_qplib_alloc_rcfw_channel(&rdev->qplib_res);
3593 	if (rc) {
3594 		dev_err(rdev_to_dev(rdev),
3595 			"Failed to alloc mem for rcfw, rc = %#x\n", rc);
3596 		goto release_rtnl;
3597 	}
3598 	set_bit(BNXT_RE_FLAG_ALLOC_RCFW, &rdev->flags);
3599 
3600 	creq = &rdev->rcfw.creq;
3601 	rattr.dma_arr = creq->hwq.pbl[PBL_LVL_0].pg_map_arr;
3602 	rattr.pages = creq->hwq.pbl[creq->hwq.level].pg_count;
3603 	rattr.type = bnxt_re_get_rtype(rdev);
3604 	rattr.mode = HWRM_RING_ALLOC_INPUT_INT_MODE_MSIX;
3605 	rattr.depth = BNXT_QPLIB_CREQE_MAX_CNT - 1;
3606 	rattr.lrid = rdev->nqr.msix_entries[BNXT_RE_AEQ_IDX].ring_idx;
3607 	rc = bnxt_re_net_ring_alloc(rdev, &rattr, &creq->ring_id);
3608 	if (rc) {
3609 		creq->ring_id = 0xffff;
3610 		dev_err(rdev_to_dev(rdev),
3611 			"Failed to allocate CREQ fw id with rc = 0x%x", rc);
3612 		goto release_rtnl;
3613 	}
3614 
3615 	set_bit(BNXT_RE_FLAG_NET_RING_ALLOC, &rdev->flags);
3616 
3617 	if (!rdev->chip_ctx)
3618 		goto release_rtnl;
3619 
3620 	if (!(_is_chip_p7(rdev->chip_ctx))) {
3621 		/* Program the NQ ID for DBQ notification */
3622 		if (rdev->chip_ctx->modes.dbr_pacing_v0 ||
3623 		    bnxt_qplib_dbr_pacing_en(rdev->chip_ctx) ||
3624 		    bnxt_qplib_dbr_pacing_ext_en(rdev->chip_ctx)) {
3625 			rc = bnxt_re_initialize_dbr_pacing(rdev);
3626 			if (!rc)
3627 				rdev->dbr_pacing = true;
3628 			else
3629 				rdev->dbr_pacing = false;
3630 			dev_dbg(rdev_to_dev(rdev), "%s: initialize db pacing ret %d\n",
3631 				__func__, rc);
3632 		}
3633 	}
3634 
3635 	vec = rdev->nqr.msix_entries[BNXT_RE_AEQ_IDX].vector;
3636 	offset = rdev->nqr.msix_entries[BNXT_RE_AEQ_IDX].db_offset;
3637 	rc = bnxt_qplib_enable_rcfw_channel(&rdev->rcfw, vec, offset,
3638 					    &bnxt_re_aeq_handler);
3639 	if (rc) {
3640 		dev_err(rdev_to_dev(rdev),
3641 			"Failed to enable RCFW channel with rc = 0x%x", rc);
3642 		goto release_rtnl;
3643 	}
3644 	set_bit(BNXT_RE_FLAG_RCFW_CHANNEL_EN, &rdev->flags);
3645 
3646 	rc = bnxt_re_update_dev_attr(rdev);
3647 	if (rc)
3648 		goto release_rtnl;
3649 	bnxt_re_set_resource_limits(rdev);
3650 	if (!rdev->is_virtfn && !_is_chip_gen_p5_p7(rdev->chip_ctx)) {
3651 		rc = bnxt_qplib_alloc_hwctx(&rdev->qplib_res);
3652 		if (rc) {
3653 			dev_err(rdev_to_dev(rdev),
3654 				"Failed to alloc hw contexts, rc = 0x%x", rc);
3655 			goto release_rtnl;
3656 		}
3657 		set_bit(BNXT_RE_FLAG_ALLOC_CTX, &rdev->flags);
3658 	}
3659 
3660 	rc = bnxt_re_get_stats_ctx(rdev);
3661 	if (rc)
3662 		goto release_rtnl;
3663 
3664 	rc = bnxt_qplib_init_rcfw(&rdev->rcfw, rdev->is_virtfn);
3665 	if (rc) {
3666 		dev_err(rdev_to_dev(rdev),
3667 			"Failed to initialize fw with rc = 0x%x", rc);
3668 		goto release_rtnl;
3669 	}
3670 	set_bit(BNXT_RE_FLAG_RCFW_CHANNEL_INIT, &rdev->flags);
3671 
3672 	/* Based resource count on the 'new' device caps */
3673 	rc = bnxt_re_update_dev_attr(rdev);
3674 	if (rc)
3675 		goto release_rtnl;
3676 	rc = bnxt_re_alloc_init_tbls(rdev);
3677 	if (rc) {
3678 		dev_err(rdev_to_dev(rdev), "tbls alloc-init failed rc = %#x",
3679 			rc);
3680 		goto release_rtnl;
3681 	}
3682 	rc = bnxt_re_setup_nqs(rdev);
3683 	if (rc) {
3684 		dev_err(rdev_to_dev(rdev), "NQs alloc-init failed rc = %#x\n",
3685 			rc);
3686 		if (rdev->nqr.max_init == 0)
3687 			goto release_rtnl;
3688 
3689 		dev_warn(rdev_to_dev(rdev),
3690 			"expected nqs %d available nqs %d\n",
3691 			rdev->nqr.num_msix, rdev->nqr.max_init);
3692 	}
3693 	set_bit(BNXT_RE_FLAG_SETUP_NQ, &rdev->flags);
3694 	rtnl_unlock();
3695 
3696 	rc = bnxt_qplib_alloc_dpi(&rdev->qplib_res, &rdev->dpi_privileged,
3697 				  rdev, BNXT_QPLIB_DPI_TYPE_KERNEL);
3698 	if (rc)
3699 		goto fail;
3700 
3701 	if (rdev->dbr_pacing)
3702 		bnxt_re_enable_dbr_pacing(rdev);
3703 
3704 	if (rdev->chip_ctx->modes.dbr_drop_recov)
3705 		bnxt_re_initialize_dbr_drop_recov(rdev);
3706 
3707 	rc = bnxt_re_alloc_dbr_sw_stats_mem(rdev);
3708 	if (rc)
3709 		goto fail;
3710 
3711 	/* This block of code is needed for error recovery support */
3712 	if (!rdev->is_virtfn) {
3713 		struct bnxt_re_tc_rec *tc_rec;
3714 
3715 		tc_rec = &rdev->tc_rec[0];
3716 		rc =  bnxt_re_query_hwrm_qportcfg(rdev, tc_rec, 0xFFFF);
3717 		if (rc) {
3718 			dev_err(rdev_to_dev(rdev),
3719 				"Failed to query port config rc:%d", rc);
3720 			return rc;
3721 		}
3722 
3723 		/* Query f/w defaults of CC params */
3724 		rc = bnxt_qplib_query_cc_param(&rdev->qplib_res, &rdev->cc_param);
3725 		if (rc)
3726 			dev_warn(rdev_to_dev(rdev),
3727 				"Failed to query CC defaults\n");
3728 		if (1) {
3729 			rdev->num_vfs = pci_num_vf(rdev->en_dev->pdev);
3730 			if (rdev->num_vfs) {
3731 				bnxt_re_set_resource_limits(rdev);
3732 				bnxt_qplib_set_func_resources(&rdev->qplib_res);
3733 			}
3734 		}
3735 	}
3736 	INIT_DELAYED_WORK(&rdev->worker, bnxt_re_worker);
3737 	set_bit(BNXT_RE_FLAG_WORKER_REG, &rdev->flags);
3738 	schedule_delayed_work(&rdev->worker, msecs_to_jiffies(1000));
3739 
3740 	bnxt_re_init_dcb_wq(rdev);
3741 	bnxt_re_init_aer_wq(rdev);
3742 	bnxt_re_init_resolve_wq(rdev);
3743 	mutex_lock(&bnxt_re_dev_lock);
3744 	list_add_tail_rcu(&rdev->list, &bnxt_re_dev_list);
3745 	/* Added to the list, not in progress anymore */
3746 	gadd_dev_inprogress--;
3747 	set_bit(BNXT_RE_FLAG_DEV_LIST_INITIALIZED, &rdev->flags);
3748 	mutex_unlock(&bnxt_re_dev_lock);
3749 
3750 
3751 	return rc;
3752 release_rtnl:
3753 	rtnl_unlock();
3754 fail:
3755 	bnxt_re_dev_uninit(rdev, BNXT_RE_COMPLETE_REMOVE);
3756 
3757 	return rc;
3758 }
3759 
bnxt_re_ib_init(struct bnxt_re_dev * rdev)3760 static int bnxt_re_ib_init(struct bnxt_re_dev *rdev)
3761 {
3762 	int rc = 0;
3763 
3764 	rc = bnxt_re_register_ib(rdev);
3765 	if (rc) {
3766 		dev_err(rdev_to_dev(rdev),
3767 			"Register IB failed with rc = 0x%x", rc);
3768 		goto fail;
3769 	}
3770 	if (bnxt_re_sysfs_create_file(rdev)) {
3771 		bnxt_re_stopqps_and_ib_uninit(rdev);
3772 		goto fail;
3773 	}
3774 
3775 	set_bit(BNXT_RE_FLAG_IBDEV_REGISTERED, &rdev->flags);
3776 	set_bit(BNXT_RE_FLAG_ISSUE_ROCE_STATS, &rdev->flags);
3777 	set_bit(BNXT_RE_FLAG_ISSUE_CFA_FLOW_STATS, &rdev->flags);
3778 	bnxt_re_dispatch_event(&rdev->ibdev, NULL, 1, IB_EVENT_PORT_ACTIVE);
3779 	bnxt_re_dispatch_event(&rdev->ibdev, NULL, 1, IB_EVENT_GID_CHANGE);
3780 
3781 	return rc;
3782 fail:
3783 	bnxt_re_dev_uninit(rdev, BNXT_RE_COMPLETE_REMOVE);
3784 	return rc;
3785 }
3786 
3787 /* wrapper for ib_init funcs */
_bnxt_re_ib_init(struct bnxt_re_dev * rdev)3788 int _bnxt_re_ib_init(struct bnxt_re_dev *rdev)
3789 {
3790 	return bnxt_re_ib_init(rdev);
3791 }
3792 
3793 /* wrapper for aux init funcs */
_bnxt_re_ib_init2(struct bnxt_re_dev * rdev)3794 int _bnxt_re_ib_init2(struct bnxt_re_dev *rdev)
3795 {
3796 	bnxt_re_ib_init_2(rdev);
3797 	return 0; /* add return for future proof */
3798 }
3799 
bnxt_re_dev_unreg(struct bnxt_re_dev * rdev)3800 static void bnxt_re_dev_unreg(struct bnxt_re_dev *rdev)
3801 {
3802 	bnxt_re_dev_dealloc(rdev);
3803 }
3804 
3805 
bnxt_re_dev_reg(struct bnxt_re_dev ** rdev,struct ifnet * netdev,struct bnxt_en_dev * en_dev)3806 static int bnxt_re_dev_reg(struct bnxt_re_dev **rdev, struct ifnet *netdev,
3807 			   struct bnxt_en_dev *en_dev)
3808 {
3809 	struct ifnet *realdev = NULL;
3810 
3811 	realdev = netdev;
3812 	if (realdev)
3813 		dev_dbg(NULL, "%s: realdev = %p netdev = %p\n", __func__,
3814 			realdev, netdev);
3815 	/*
3816 	 * Note:
3817 	 * The first argument to bnxt_re_dev_alloc() is 'netdev' and
3818 	 * not 'realdev', since in the case of bonding we want to
3819 	 * register the bonded virtual netdev (master) to the ib stack.
3820 	 * And 'en_dev' (for L2/PCI communication) is the first slave
3821 	 * device (PF0 on the card).
3822 	 * In the case of a regular netdev, both netdev and the en_dev
3823 	 * correspond to the same device.
3824 	 */
3825 	*rdev = bnxt_re_dev_alloc(netdev, en_dev);
3826 	if (!*rdev) {
3827 		pr_err("%s: netdev %p not handled",
3828 			ROCE_DRV_MODULE_NAME, netdev);
3829 		return -ENOMEM;
3830 	}
3831 	bnxt_re_hold(*rdev);
3832 
3833 	return 0;
3834 }
3835 
bnxt_re_get_link_speed(struct bnxt_re_dev * rdev)3836 void bnxt_re_get_link_speed(struct bnxt_re_dev *rdev)
3837 {
3838 	rdev->espeed = rdev->en_dev->espeed;
3839 	rdev->lanes = rdev->en_dev->lanes;
3840 	return;
3841 }
3842 
bnxt_re_stopqps_and_ib_uninit(struct bnxt_re_dev * rdev)3843 void bnxt_re_stopqps_and_ib_uninit(struct bnxt_re_dev *rdev)
3844 {
3845 	dev_dbg(rdev_to_dev(rdev), "%s: Stopping QPs, IB uninit on rdev: %p\n",
3846 		__func__, rdev);
3847 	bnxt_re_stop_all_nonqp1_nonshadow_qps(rdev);
3848 	bnxt_re_ib_uninit(rdev);
3849 }
3850 
bnxt_re_remove_device(struct bnxt_re_dev * rdev,u8 op_type,struct auxiliary_device * aux_dev)3851 void bnxt_re_remove_device(struct bnxt_re_dev *rdev, u8 op_type,
3852 			   struct auxiliary_device *aux_dev)
3853 {
3854 	struct bnxt_re_en_dev_info *en_info;
3855 	struct bnxt_qplib_cmdq_ctx *cmdq;
3856 	struct bnxt_qplib_rcfw *rcfw;
3857 
3858 	rcfw = &rdev->rcfw;
3859 	cmdq = &rcfw->cmdq;
3860 	if (test_bit(FIRMWARE_STALL_DETECTED, &cmdq->flags))
3861 		set_bit(BNXT_RE_FLAG_ERR_DEVICE_DETACHED, &rdev->flags);
3862 
3863 	dev_dbg(rdev_to_dev(rdev), "%s: Removing rdev: %p\n", __func__, rdev);
3864 	bnxt_re_dev_uninit(rdev, op_type);
3865 	en_info = auxiliary_get_drvdata(aux_dev);
3866 	if (en_info) {
3867 		rtnl_lock();
3868 		en_info->rdev = NULL;
3869 		rtnl_unlock();
3870 		if (op_type != BNXT_RE_PRE_RECOVERY_REMOVE) {
3871 			clear_bit(BNXT_RE_FLAG_EN_DEV_PRIMARY_DEV, &en_info->flags);
3872 			clear_bit(BNXT_RE_FLAG_EN_DEV_SECONDARY_DEV, &en_info->flags);
3873 			clear_bit(BNXT_RE_FLAG_EN_DEV_NETDEV_REG, &en_info->flags);
3874 		}
3875 	}
3876 	bnxt_re_dev_unreg(rdev);
3877 }
3878 
bnxt_re_add_device(struct bnxt_re_dev ** rdev,struct ifnet * netdev,u8 qp_mode,u8 op_type,u32 num_msix_requested,struct auxiliary_device * aux_dev)3879 int bnxt_re_add_device(struct bnxt_re_dev **rdev,
3880 		       struct ifnet *netdev,
3881 		       u8 qp_mode, u8 op_type,
3882 		       u32 num_msix_requested,
3883 		       struct auxiliary_device *aux_dev)
3884 {
3885 	struct bnxt_re_en_dev_info *en_info;
3886 	struct bnxt_en_dev *en_dev;
3887 	int rc = 0;
3888 
3889 	en_info = auxiliary_get_drvdata(aux_dev);
3890 	en_dev = en_info->en_dev;
3891 
3892 	mutex_lock(&bnxt_re_dev_lock);
3893 	/* Check if driver already in mod exit and aux_dev is valid */
3894 	if (gmod_exit || !aux_dev) {
3895 		mutex_unlock(&bnxt_re_dev_lock);
3896 		return -ENODEV;
3897 	}
3898 	/* Add device in progress */
3899 	gadd_dev_inprogress++;
3900 	mutex_unlock(&bnxt_re_dev_lock);
3901 
3902 	rc = bnxt_re_dev_reg(rdev, netdev, en_dev);
3903 	if (rc) {
3904 		dev_dbg(NULL, "Failed to create add device for netdev %p\n",
3905 			netdev);
3906 		/*
3907 		 * For BNXT_RE_POST_RECOVERY_INIT special case
3908 		 * called from bnxt_re_start, the work is
3909 		 * complete only after, bnxt_re_start completes
3910 		 * bnxt_unregister_device in case of failure.
3911 		 * So bnxt_re_start will decrement gadd_dev_inprogress
3912 		 * in case of failure.
3913 		 */
3914 		if (op_type != BNXT_RE_POST_RECOVERY_INIT) {
3915 			mutex_lock(&bnxt_re_dev_lock);
3916 			gadd_dev_inprogress--;
3917 			mutex_unlock(&bnxt_re_dev_lock);
3918 		}
3919 		return rc;
3920 	}
3921 
3922 	if (rc != 0)
3923 		goto ref_error;
3924 
3925 	/*
3926 	 *  num_msix_requested = BNXT_RE_MSIX_FROM_MOD_PARAM indicates fresh driver load.
3927 	 *  Otherwaise, this invocation can be the result of lag create / destroy,
3928 	 *  err revovery, hot fw upgrade, etc..
3929 	 */
3930 	if (num_msix_requested == BNXT_RE_MSIX_FROM_MOD_PARAM) {
3931 		if (bnxt_re_probe_count < BNXT_RE_MAX_DEVICES)
3932 			num_msix_requested = max_msix_vec[bnxt_re_probe_count++];
3933 		else
3934 			/* Consider as default when probe_count exceeds its limit */
3935 			num_msix_requested = 0;
3936 
3937 		/* if user specifies only one value, use the same for all PFs */
3938 		if (max_msix_vec_argc == 1)
3939 			num_msix_requested = max_msix_vec[0];
3940 	}
3941 
3942 	(*rdev)->num_msix_requested = num_msix_requested;
3943 	(*rdev)->gsi_ctx.gsi_qp_mode = qp_mode;
3944 	(*rdev)->adev = aux_dev;
3945 	(*rdev)->dev_addr = en_dev->softc->func.mac_addr;
3946 	/* Before updating the rdev pointer in bnxt_re_en_dev_info structure,
3947 	 * take the rtnl lock to avoid accessing invalid rdev pointer from
3948 	 * L2 ULP callbacks. This is applicable in all the places where rdev
3949 	 * pointer is updated in bnxt_re_en_dev_info.
3950 	 */
3951 	rtnl_lock();
3952 	en_info->rdev = *rdev;
3953 	rtnl_unlock();
3954 	rc = bnxt_re_dev_init(*rdev, op_type);
3955 	if (rc) {
3956 ref_error:
3957 		bnxt_re_dev_unreg(*rdev);
3958 		*rdev = NULL;
3959 		/*
3960 		 * For BNXT_RE_POST_RECOVERY_INIT special case
3961 		 * called from bnxt_re_start, the work is
3962 		 * complete only after, bnxt_re_start completes
3963 		 * bnxt_unregister_device in case of failure.
3964 		 * So bnxt_re_start will decrement gadd_dev_inprogress
3965 		 * in case of failure.
3966 		 */
3967 		if (op_type != BNXT_RE_POST_RECOVERY_INIT) {
3968 			mutex_lock(&bnxt_re_dev_lock);
3969 			gadd_dev_inprogress--;
3970 			mutex_unlock(&bnxt_re_dev_lock);
3971 		}
3972 	}
3973 	dev_dbg(rdev_to_dev(*rdev), "%s: Adding rdev: %p\n", __func__, *rdev);
3974 	if (!rc) {
3975 		set_bit(BNXT_RE_FLAG_EN_DEV_NETDEV_REG, &en_info->flags);
3976 	}
3977 	return rc;
3978 }
3979 
bnxt_re_get_peer_pf(struct bnxt_re_dev * rdev)3980 struct bnxt_re_dev *bnxt_re_get_peer_pf(struct bnxt_re_dev *rdev)
3981 {
3982 	struct pci_dev *pdev_in = rdev->en_dev->pdev;
3983 	int tmp_bus_num, bus_num = pdev_in->bus->number;
3984 	int tmp_dev_num, dev_num = PCI_SLOT(pdev_in->devfn);
3985 	int tmp_func_num, func_num = PCI_FUNC(pdev_in->devfn);
3986 	struct bnxt_re_dev *tmp_rdev;
3987 
3988 	rcu_read_lock();
3989 	list_for_each_entry_rcu(tmp_rdev, &bnxt_re_dev_list, list) {
3990 		tmp_bus_num = tmp_rdev->en_dev->pdev->bus->number;
3991 		tmp_dev_num = PCI_SLOT(tmp_rdev->en_dev->pdev->devfn);
3992 		tmp_func_num = PCI_FUNC(tmp_rdev->en_dev->pdev->devfn);
3993 
3994 		if (bus_num == tmp_bus_num && dev_num == tmp_dev_num &&
3995 		    func_num != tmp_func_num) {
3996 			rcu_read_unlock();
3997 			return tmp_rdev;
3998 		}
3999 	}
4000 	rcu_read_unlock();
4001 	return NULL;
4002 }
4003 
4004 
bnxt_re_schedule_work(struct bnxt_re_dev * rdev,unsigned long event,struct ifnet * vlan_dev,struct ifnet * netdev,struct auxiliary_device * adev)4005 int bnxt_re_schedule_work(struct bnxt_re_dev *rdev, unsigned long event,
4006 			  struct ifnet *vlan_dev,
4007 			  struct ifnet *netdev,
4008 			  struct auxiliary_device *adev)
4009 {
4010 	struct bnxt_re_work *re_work;
4011 
4012 	/* Allocate for the deferred task */
4013 	re_work = kzalloc(sizeof(*re_work), GFP_KERNEL);
4014 	if (!re_work)
4015 		return -ENOMEM;
4016 
4017 	re_work->rdev = rdev;
4018 	re_work->event = event;
4019 	re_work->vlan_dev = vlan_dev;
4020 	re_work->adev = adev;
4021 	INIT_WORK(&re_work->work, bnxt_re_task);
4022 	if (rdev)
4023 		atomic_inc(&rdev->sched_count);
4024 	re_work->netdev = netdev;
4025 	queue_work(bnxt_re_wq, &re_work->work);
4026 
4027 	return 0;
4028 }
4029 
4030 
bnxt_re_get_slot_pf_count(struct bnxt_re_dev * rdev)4031 int bnxt_re_get_slot_pf_count(struct bnxt_re_dev *rdev)
4032 {
4033 	struct pci_dev *pdev_in = rdev->en_dev->pdev;
4034 	int tmp_bus_num, bus_num = pdev_in->bus->number;
4035 	int tmp_dev_num, dev_num = PCI_SLOT(pdev_in->devfn);
4036 	struct bnxt_re_dev *tmp_rdev;
4037 	int pf_cnt = 0;
4038 
4039 	rcu_read_lock();
4040 	list_for_each_entry_rcu(tmp_rdev, &bnxt_re_dev_list, list) {
4041 		tmp_bus_num = tmp_rdev->en_dev->pdev->bus->number;
4042 		tmp_dev_num = PCI_SLOT(tmp_rdev->en_dev->pdev->devfn);
4043 
4044 		if (bus_num == tmp_bus_num && dev_num == tmp_dev_num)
4045 			pf_cnt++;
4046 	}
4047 	rcu_read_unlock();
4048 	return pf_cnt;
4049 }
4050 
4051 /* Handle all deferred netevents tasks */
bnxt_re_task(struct work_struct * work)4052 static void bnxt_re_task(struct work_struct *work)
4053 {
4054 	struct bnxt_re_en_dev_info *en_info;
4055 	struct auxiliary_device *aux_dev;
4056 	struct bnxt_re_work *re_work;
4057 	struct bnxt_re_dev *rdev;
4058 
4059 	re_work = container_of(work, struct bnxt_re_work, work);
4060 
4061 	mutex_lock(&bnxt_re_mutex);
4062 	rdev = re_work->rdev;
4063 
4064 	/*
4065 	 * If the previous rdev is deleted due to bond creation
4066 	 * do not handle the event
4067 	 */
4068 	if (!bnxt_re_is_rdev_valid(rdev))
4069 		goto exit;
4070 
4071 	/* Ignore the event, if the device is not registred with IB stack. This
4072 	 * is to avoid handling any event while the device is added/removed.
4073 	 */
4074 	if (rdev && !test_bit(BNXT_RE_FLAG_IBDEV_REGISTERED, &rdev->flags)) {
4075 		dev_dbg(rdev_to_dev(rdev), "%s: Ignoring netdev event 0x%lx",
4076 			__func__, re_work->event);
4077 		goto done;
4078 	}
4079 
4080 	/* Extra check to silence coverity. We shouldn't handle any event
4081 	 * when rdev is NULL.
4082 	 */
4083 	if (!rdev)
4084 		goto exit;
4085 
4086 	dev_dbg(rdev_to_dev(rdev), "Scheduled work for event 0x%lx",
4087 		re_work->event);
4088 
4089 	switch (re_work->event) {
4090 	case NETDEV_UP:
4091 		bnxt_re_dispatch_event(&rdev->ibdev, NULL, 1,
4092 				       IB_EVENT_PORT_ACTIVE);
4093 		bnxt_re_net_register_async_event(rdev);
4094 		break;
4095 
4096 	case NETDEV_DOWN:
4097 		bnxt_qplib_dbr_pacing_set_primary_pf(rdev->chip_ctx, 0);
4098 		bnxt_re_stop_all_nonqp1_nonshadow_qps(rdev);
4099 		bnxt_re_dispatch_event(&rdev->ibdev, NULL, 1,
4100 				       IB_EVENT_PORT_ERR);
4101 		break;
4102 
4103 	case NETDEV_CHANGE:
4104 		if (bnxt_re_get_link_state(rdev) == IB_PORT_DOWN) {
4105 			bnxt_re_stop_all_nonqp1_nonshadow_qps(rdev);
4106 			bnxt_re_dispatch_event(&rdev->ibdev, NULL, 1,
4107 					       IB_EVENT_PORT_ERR);
4108 			break;
4109 		} else if (bnxt_re_get_link_state(rdev) == IB_PORT_ACTIVE) {
4110 			bnxt_re_dispatch_event(&rdev->ibdev, NULL, 1,
4111 					       IB_EVENT_PORT_ACTIVE);
4112 		}
4113 
4114 		/* temporarily disable the check for SR2 */
4115 		if (!bnxt_qplib_query_cc_param(&rdev->qplib_res,
4116 					       &rdev->cc_param) &&
4117 		    !_is_chip_p7(rdev->chip_ctx)) {
4118 			/*
4119 			 *  Disable CC for 10G speed
4120 			 * for non p5 devices
4121 			 */
4122 			if (rdev->sl_espeed == SPEED_10000 &&
4123 			    !_is_chip_gen_p5_p7(rdev->chip_ctx)) {
4124 				if (rdev->cc_param.enable)
4125 					bnxt_re_clear_cc(rdev);
4126 			} else {
4127 				if (!rdev->cc_param.enable &&
4128 				    rdev->cc_param.admin_enable)
4129 					bnxt_re_setup_cc(rdev);
4130 			}
4131 		}
4132 		break;
4133 
4134 	case NETDEV_UNREGISTER:
4135 		bnxt_re_stopqps_and_ib_uninit(rdev);
4136 		aux_dev = rdev->adev;
4137 		if (re_work->adev)
4138 			goto done;
4139 
4140 		bnxt_re_remove_device(rdev, BNXT_RE_COMPLETE_REMOVE, aux_dev);
4141 
4142 		break;
4143 
4144 	default:
4145 		break;
4146 	}
4147 done:
4148 	if (rdev) {
4149 		/* memory barrier to guarantee task completion
4150 		 * before decrementing sched count
4151 		 */
4152 		mmiowb();
4153 		atomic_dec(&rdev->sched_count);
4154 	}
4155 exit:
4156 	if (re_work->adev && re_work->event == NETDEV_UNREGISTER) {
4157 		en_info = auxiliary_get_drvdata(re_work->adev);
4158 		en_info->ib_uninit_done = true;
4159 		wake_up(&en_info->waitq);
4160 	}
4161 	kfree(re_work);
4162 	mutex_unlock(&bnxt_re_mutex);
4163 }
4164 
4165 /*
4166     "Notifier chain callback can be invoked for the same chain from
4167     different CPUs at the same time".
4168 
4169     For cases when the netdev is already present, our call to the
4170     register_netdevice_notifier() will actually get the rtnl_lock()
4171     before sending NETDEV_REGISTER and (if up) NETDEV_UP
4172     events.
4173 
4174     But for cases when the netdev is not already present, the notifier
4175     chain is subjected to be invoked from different CPUs simultaneously.
4176 
4177     This is protected by the netdev_mutex.
4178 */
bnxt_re_netdev_event(struct notifier_block * notifier,unsigned long event,void * ptr)4179 static int bnxt_re_netdev_event(struct notifier_block *notifier,
4180 				unsigned long event, void *ptr)
4181 {
4182 	struct ifnet *real_dev, *netdev;
4183 	struct bnxt_re_dev *rdev = NULL;
4184 
4185 	netdev = netdev_notifier_info_to_ifp(ptr);
4186 	real_dev = rdma_vlan_dev_real_dev(netdev);
4187 	if (!real_dev)
4188 		real_dev = netdev;
4189 	/* In case of bonding,this will be bond's rdev */
4190 	rdev = bnxt_re_from_netdev(real_dev);
4191 
4192 	if (!rdev)
4193 		goto exit;
4194 
4195 	dev_info(rdev_to_dev(rdev), "%s: Event = %s (0x%lx), rdev %s (real_dev %s)\n",
4196 		 __func__, bnxt_re_netevent(event), event,
4197 		 rdev ? rdev->netdev ? if_getdname(rdev->netdev) : "->netdev = NULL" : "= NULL",
4198 		 (real_dev == netdev) ? "= netdev" : if_getdname(real_dev));
4199 
4200 	if (!test_bit(BNXT_RE_FLAG_IBDEV_REGISTERED, &rdev->flags))
4201 		goto exit;
4202 
4203 	bnxt_re_hold(rdev);
4204 
4205 	if (real_dev != netdev) {
4206 		switch (event) {
4207 		case NETDEV_UP:
4208 			bnxt_re_schedule_work(rdev, event, netdev,
4209 					      NULL, NULL);
4210 			break;
4211 		case NETDEV_DOWN:
4212 			break;
4213 		default:
4214 			break;
4215 		}
4216 		goto done;
4217 	}
4218 
4219 	switch (event) {
4220 	case NETDEV_CHANGEADDR:
4221 		if (!_is_chip_gen_p5_p7(rdev->chip_ctx))
4222 			bnxt_re_update_shadow_ah(rdev);
4223 		bnxt_qplib_get_guid(rdev->dev_addr,
4224 				    (u8 *)&rdev->ibdev.node_guid);
4225 		break;
4226 
4227 	case NETDEV_CHANGE:
4228 		bnxt_re_get_link_speed(rdev);
4229 		bnxt_re_schedule_work(rdev, event, NULL, NULL, NULL);
4230 		break;
4231 	case NETDEV_UNREGISTER:
4232 		/* netdev notifier will call NETDEV_UNREGISTER again later since
4233 		 * we are still holding the reference to the netdev
4234 		 */
4235 
4236 		/*
4237 		 *  Workaround to avoid ib_unregister hang. Check for module
4238 		 *  reference and dont free up the device if the reference
4239 		 *  is non zero. Checking only for PF functions.
4240 		 */
4241 
4242 		if (rdev) {
4243 			dev_info(rdev_to_dev(rdev),
4244 				 "bnxt_re:Unreg recvd when module refcnt > 0");
4245 			dev_info(rdev_to_dev(rdev),
4246 				 "bnxt_re:Close all apps using bnxt_re devs");
4247 			dev_info(rdev_to_dev(rdev),
4248 				 "bnxt_re:Remove the configfs entry created for the device");
4249 			dev_info(rdev_to_dev(rdev),
4250 				 "bnxt_re:Refer documentation for details");
4251 			goto done;
4252 		}
4253 
4254 		if (atomic_read(&rdev->sched_count) > 0)
4255 			goto done;
4256 		if (!rdev->unreg_sched) {
4257 			bnxt_re_schedule_work(rdev, NETDEV_UNREGISTER,
4258 					      NULL, NULL, NULL);
4259 			rdev->unreg_sched = true;
4260 			goto done;
4261 		}
4262 
4263 		break;
4264 	default:
4265 		break;
4266 	}
4267 done:
4268 	if (rdev)
4269 		bnxt_re_put(rdev);
4270 exit:
4271 	return NOTIFY_DONE;
4272 }
4273 
4274 static struct notifier_block bnxt_re_netdev_notifier = {
4275 	.notifier_call = bnxt_re_netdev_event
4276 };
4277 
bnxt_re_remove_base_interface(struct bnxt_re_dev * rdev,struct auxiliary_device * adev)4278 static void bnxt_re_remove_base_interface(struct bnxt_re_dev *rdev,
4279 					  struct auxiliary_device *adev)
4280 {
4281 	bnxt_re_stopqps_and_ib_uninit(rdev);
4282 	bnxt_re_remove_device(rdev, BNXT_RE_COMPLETE_REMOVE, adev);
4283 	auxiliary_set_drvdata(adev, NULL);
4284 }
4285 
4286 /*
4287  *  bnxt_re_remove  -	Removes the roce aux device
4288  *  @adev  -  aux device pointer
4289  *
4290  * This function removes the roce device. This gets
4291  * called in the mod exit path and pci unbind path.
4292  * If the rdev is bond interace, destroys the lag
4293  * in module exit path, and in pci unbind case
4294  * destroys the lag and recreates other base interface.
4295  * If the device is already removed in error recovery
4296  * path, it just unregister with the L2.
4297  */
bnxt_re_remove(struct auxiliary_device * adev)4298 static void bnxt_re_remove(struct auxiliary_device *adev)
4299 {
4300 	struct bnxt_re_en_dev_info *en_info = auxiliary_get_drvdata(adev);
4301 	struct bnxt_en_dev *en_dev;
4302 	struct bnxt_re_dev *rdev;
4303 	bool primary_dev = false;
4304 	bool secondary_dev = false;
4305 
4306 	if (!en_info)
4307 		return;
4308 
4309 	mutex_lock(&bnxt_re_mutex);
4310 	en_dev = en_info->en_dev;
4311 
4312 	rdev = en_info->rdev;
4313 
4314 	if (rdev && bnxt_re_is_rdev_valid(rdev)) {
4315 		if (pci_channel_offline(rdev->rcfw.pdev))
4316 			set_bit(ERR_DEVICE_DETACHED, &rdev->rcfw.cmdq.flags);
4317 
4318 		if (test_bit(BNXT_RE_FLAG_EN_DEV_PRIMARY_DEV, &en_info->flags))
4319 			primary_dev = true;
4320 		if (test_bit(BNXT_RE_FLAG_EN_DEV_SECONDARY_DEV, &en_info->flags))
4321 			secondary_dev = true;
4322 
4323 		/*
4324 		 * en_dev_info of primary device and secondary device have the
4325 		 * same rdev pointer when LAG is configured. This rdev pointer
4326 		 * is rdev of bond interface.
4327 		 */
4328 		if (!primary_dev && !secondary_dev) {
4329 			/* removal of non bond interface */
4330 			bnxt_re_remove_base_interface(rdev, adev);
4331 		} else {
4332 			/*
4333 			 * removal of bond primary/secondary interface. In this
4334 			 * case bond device is already removed, so rdev->binfo
4335 			 * is NULL.
4336 			 */
4337 			auxiliary_set_drvdata(adev, NULL);
4338 		}
4339 	} else {
4340 		/* device is removed from ulp stop, unregister the net dev */
4341 		if (test_bit(BNXT_RE_FLAG_EN_DEV_NETDEV_REG, &en_info->flags)) {
4342 			rtnl_lock();
4343 			en_dev->en_ops->bnxt_unregister_device(en_dev,
4344 							       BNXT_ROCE_ULP);
4345 			rtnl_unlock();
4346 		}
4347 	}
4348 	mutex_unlock(&bnxt_re_mutex);
4349 	return;
4350 }
4351 
4352 /* wrapper for all external user context callers */
_bnxt_re_remove(struct auxiliary_device * adev)4353 void _bnxt_re_remove(struct auxiliary_device *adev)
4354 {
4355 	bnxt_re_remove(adev);
4356 }
4357 
bnxt_re_ib_init_2(struct bnxt_re_dev * rdev)4358 static void bnxt_re_ib_init_2(struct bnxt_re_dev *rdev)
4359 {
4360 	int rc;
4361 
4362 	rc = bnxt_re_get_device_stats(rdev);
4363 	if (rc)
4364 		dev_err(rdev_to_dev(rdev),
4365 			"Failed initial device stat query");
4366 
4367 	bnxt_re_net_register_async_event(rdev);
4368 }
4369 
bnxt_re_probe(struct auxiliary_device * adev,const struct auxiliary_device_id * id)4370 static int bnxt_re_probe(struct auxiliary_device *adev,
4371 			 const struct auxiliary_device_id *id)
4372 {
4373 	struct bnxt_aux_dev *aux_dev =
4374 		container_of(adev, struct bnxt_aux_dev, aux_dev);
4375 	struct bnxt_re_en_dev_info *en_info;
4376 	struct bnxt_en_dev *en_dev = NULL;
4377 	struct bnxt_re_dev *rdev;
4378 	int rc = -ENODEV;
4379 
4380 	if (aux_dev)
4381 		en_dev = aux_dev->edev;
4382 
4383 	if (!en_dev)
4384 		return rc;
4385 
4386 	if (en_dev->ulp_version != BNXT_ULP_VERSION) {
4387 		pr_err("%s: probe error: bnxt_en ulp version magic %x is not compatible!\n",
4388 			ROCE_DRV_MODULE_NAME, en_dev->ulp_version);
4389 		return -EINVAL;
4390 	}
4391 
4392 	en_info = kzalloc(sizeof(*en_info), GFP_KERNEL);
4393 	if (!en_info)
4394 		return -ENOMEM;
4395 	memset(en_info, 0, sizeof(struct bnxt_re_en_dev_info));
4396 	en_info->en_dev = en_dev;
4397 	auxiliary_set_drvdata(adev, en_info);
4398 
4399 	mutex_lock(&bnxt_re_mutex);
4400 	rc = bnxt_re_add_device(&rdev, en_dev->net,
4401 				BNXT_RE_GSI_MODE_ALL,
4402 				BNXT_RE_COMPLETE_INIT,
4403 				BNXT_RE_MSIX_FROM_MOD_PARAM, adev);
4404 	if (rc) {
4405 		mutex_unlock(&bnxt_re_mutex);
4406 		return rc;
4407 	}
4408 
4409 	rc = bnxt_re_ib_init(rdev);
4410 	if (rc)
4411 		goto err;
4412 
4413 	bnxt_re_ib_init_2(rdev);
4414 
4415 	dev_dbg(rdev_to_dev(rdev), "%s: adev: %p\n", __func__, adev);
4416 	rdev->adev = adev;
4417 
4418 	mutex_unlock(&bnxt_re_mutex);
4419 
4420 	return 0;
4421 
4422 err:
4423 	mutex_unlock(&bnxt_re_mutex);
4424 	bnxt_re_remove(adev);
4425 
4426 	return rc;
4427 }
4428 
4429 static const struct auxiliary_device_id bnxt_re_id_table[] = {
4430 	{ .name = BNXT_ADEV_NAME ".rdma", },
4431 	{},
4432 };
4433 
4434 MODULE_DEVICE_TABLE(auxiliary, bnxt_re_id_table);
4435 
4436 static struct auxiliary_driver bnxt_re_driver = {
4437 	.name = "rdma",
4438 	.probe = bnxt_re_probe,
4439 	.remove = bnxt_re_remove,
4440 	.id_table = bnxt_re_id_table,
4441 };
4442 
bnxt_re_mod_init(void)4443 static int __init bnxt_re_mod_init(void)
4444 {
4445 	int rc = 0;
4446 
4447 	pr_info("%s: %s", ROCE_DRV_MODULE_NAME, drv_version);
4448 
4449 	bnxt_re_wq = create_singlethread_workqueue("bnxt_re");
4450 	if (!bnxt_re_wq)
4451 		return -ENOMEM;
4452 
4453 	rc = bnxt_re_register_netdevice_notifier(&bnxt_re_netdev_notifier);
4454 	if (rc) {
4455 		pr_err("%s: Cannot register to netdevice_notifier",
4456 			ROCE_DRV_MODULE_NAME);
4457 		goto err_netdev;
4458 	}
4459 
4460 	INIT_LIST_HEAD(&bnxt_re_dev_list);
4461 
4462 	rc = auxiliary_driver_register(&bnxt_re_driver);
4463 	if (rc) {
4464 		pr_err("%s: Failed to register auxiliary driver\n",
4465 		       ROCE_DRV_MODULE_NAME);
4466 		goto err_auxdrv;
4467 	}
4468 
4469 	return 0;
4470 
4471 err_auxdrv:
4472 	bnxt_re_unregister_netdevice_notifier(&bnxt_re_netdev_notifier);
4473 
4474 err_netdev:
4475 	destroy_workqueue(bnxt_re_wq);
4476 
4477 	return rc;
4478 }
4479 
bnxt_re_mod_exit(void)4480 static void __exit bnxt_re_mod_exit(void)
4481 {
4482 	gmod_exit = 1;
4483 	auxiliary_driver_unregister(&bnxt_re_driver);
4484 
4485 	bnxt_re_unregister_netdevice_notifier(&bnxt_re_netdev_notifier);
4486 
4487 	if (bnxt_re_wq)
4488 		destroy_workqueue(bnxt_re_wq);
4489 }
4490 
4491 module_init(bnxt_re_mod_init);
4492 module_exit(bnxt_re_mod_exit);
4493