xref: /openbmc/linux/drivers/net/ethernet/qlogic/qed/qed_dev.c (revision dd2934a95701576203b2f61e8ded4e4a2f9183ea)
1 /* QLogic qed NIC Driver
2  * Copyright (c) 2015-2017  QLogic Corporation
3  *
4  * This software is available to you under a choice of one of two
5  * licenses.  You may choose to be licensed under the terms of the GNU
6  * General Public License (GPL) Version 2, available from the file
7  * COPYING in the main directory of this source tree, or the
8  * OpenIB.org BSD license below:
9  *
10  *     Redistribution and use in source and binary forms, with or
11  *     without modification, are permitted provided that the following
12  *     conditions are met:
13  *
14  *      - Redistributions of source code must retain the above
15  *        copyright notice, this list of conditions and the following
16  *        disclaimer.
17  *
18  *      - Redistributions in binary form must reproduce the above
19  *        copyright notice, this list of conditions and the following
20  *        disclaimer in the documentation and /or other materials
21  *        provided with the distribution.
22  *
23  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30  * SOFTWARE.
31  */
32 
33 #include <linux/types.h>
34 #include <asm/byteorder.h>
35 #include <linux/io.h>
36 #include <linux/delay.h>
37 #include <linux/dma-mapping.h>
38 #include <linux/errno.h>
39 #include <linux/kernel.h>
40 #include <linux/mutex.h>
41 #include <linux/pci.h>
42 #include <linux/slab.h>
43 #include <linux/string.h>
44 #include <linux/vmalloc.h>
45 #include <linux/etherdevice.h>
46 #include <linux/qed/qed_chain.h>
47 #include <linux/qed/qed_if.h>
48 #include "qed.h"
49 #include "qed_cxt.h"
50 #include "qed_dcbx.h"
51 #include "qed_dev_api.h"
52 #include "qed_fcoe.h"
53 #include "qed_hsi.h"
54 #include "qed_hw.h"
55 #include "qed_init_ops.h"
56 #include "qed_int.h"
57 #include "qed_iscsi.h"
58 #include "qed_ll2.h"
59 #include "qed_mcp.h"
60 #include "qed_ooo.h"
61 #include "qed_reg_addr.h"
62 #include "qed_sp.h"
63 #include "qed_sriov.h"
64 #include "qed_vf.h"
65 #include "qed_rdma.h"
66 
67 static DEFINE_SPINLOCK(qm_lock);
68 
69 #define QED_MIN_DPIS            (4)
70 #define QED_MIN_PWM_REGION      (QED_WID_SIZE * QED_MIN_DPIS)
71 
72 static u32 qed_hw_bar_size(struct qed_hwfn *p_hwfn,
73 			   struct qed_ptt *p_ptt, enum BAR_ID bar_id)
74 {
75 	u32 bar_reg = (bar_id == BAR_ID_0 ?
76 		       PGLUE_B_REG_PF_BAR0_SIZE : PGLUE_B_REG_PF_BAR1_SIZE);
77 	u32 val;
78 
79 	if (IS_VF(p_hwfn->cdev))
80 		return qed_vf_hw_bar_size(p_hwfn, bar_id);
81 
82 	val = qed_rd(p_hwfn, p_ptt, bar_reg);
83 	if (val)
84 		return 1 << (val + 15);
85 
86 	/* Old MFW initialized above registered only conditionally */
87 	if (p_hwfn->cdev->num_hwfns > 1) {
88 		DP_INFO(p_hwfn,
89 			"BAR size not configured. Assuming BAR size of 256kB for GRC and 512kB for DB\n");
90 			return BAR_ID_0 ? 256 * 1024 : 512 * 1024;
91 	} else {
92 		DP_INFO(p_hwfn,
93 			"BAR size not configured. Assuming BAR size of 512kB for GRC and 512kB for DB\n");
94 			return 512 * 1024;
95 	}
96 }
97 
98 void qed_init_dp(struct qed_dev *cdev, u32 dp_module, u8 dp_level)
99 {
100 	u32 i;
101 
102 	cdev->dp_level = dp_level;
103 	cdev->dp_module = dp_module;
104 	for (i = 0; i < MAX_HWFNS_PER_DEVICE; i++) {
105 		struct qed_hwfn *p_hwfn = &cdev->hwfns[i];
106 
107 		p_hwfn->dp_level = dp_level;
108 		p_hwfn->dp_module = dp_module;
109 	}
110 }
111 
112 void qed_init_struct(struct qed_dev *cdev)
113 {
114 	u8 i;
115 
116 	for (i = 0; i < MAX_HWFNS_PER_DEVICE; i++) {
117 		struct qed_hwfn *p_hwfn = &cdev->hwfns[i];
118 
119 		p_hwfn->cdev = cdev;
120 		p_hwfn->my_id = i;
121 		p_hwfn->b_active = false;
122 
123 		mutex_init(&p_hwfn->dmae_info.mutex);
124 	}
125 
126 	/* hwfn 0 is always active */
127 	cdev->hwfns[0].b_active = true;
128 
129 	/* set the default cache alignment to 128 */
130 	cdev->cache_shift = 7;
131 }
132 
133 static void qed_qm_info_free(struct qed_hwfn *p_hwfn)
134 {
135 	struct qed_qm_info *qm_info = &p_hwfn->qm_info;
136 
137 	kfree(qm_info->qm_pq_params);
138 	qm_info->qm_pq_params = NULL;
139 	kfree(qm_info->qm_vport_params);
140 	qm_info->qm_vport_params = NULL;
141 	kfree(qm_info->qm_port_params);
142 	qm_info->qm_port_params = NULL;
143 	kfree(qm_info->wfq_data);
144 	qm_info->wfq_data = NULL;
145 }
146 
147 static void qed_dbg_user_data_free(struct qed_hwfn *p_hwfn)
148 {
149 	kfree(p_hwfn->dbg_user_info);
150 	p_hwfn->dbg_user_info = NULL;
151 }
152 
153 void qed_resc_free(struct qed_dev *cdev)
154 {
155 	int i;
156 
157 	if (IS_VF(cdev)) {
158 		for_each_hwfn(cdev, i)
159 			qed_l2_free(&cdev->hwfns[i]);
160 		return;
161 	}
162 
163 	kfree(cdev->fw_data);
164 	cdev->fw_data = NULL;
165 
166 	kfree(cdev->reset_stats);
167 	cdev->reset_stats = NULL;
168 
169 	for_each_hwfn(cdev, i) {
170 		struct qed_hwfn *p_hwfn = &cdev->hwfns[i];
171 
172 		qed_cxt_mngr_free(p_hwfn);
173 		qed_qm_info_free(p_hwfn);
174 		qed_spq_free(p_hwfn);
175 		qed_eq_free(p_hwfn);
176 		qed_consq_free(p_hwfn);
177 		qed_int_free(p_hwfn);
178 #ifdef CONFIG_QED_LL2
179 		qed_ll2_free(p_hwfn);
180 #endif
181 		if (p_hwfn->hw_info.personality == QED_PCI_FCOE)
182 			qed_fcoe_free(p_hwfn);
183 
184 		if (p_hwfn->hw_info.personality == QED_PCI_ISCSI) {
185 			qed_iscsi_free(p_hwfn);
186 			qed_ooo_free(p_hwfn);
187 		}
188 		qed_iov_free(p_hwfn);
189 		qed_l2_free(p_hwfn);
190 		qed_dmae_info_free(p_hwfn);
191 		qed_dcbx_info_free(p_hwfn);
192 		qed_dbg_user_data_free(p_hwfn);
193 	}
194 }
195 
196 /******************** QM initialization *******************/
197 #define ACTIVE_TCS_BMAP 0x9f
198 #define ACTIVE_TCS_BMAP_4PORT_K2 0xf
199 
200 /* determines the physical queue flags for a given PF. */
201 static u32 qed_get_pq_flags(struct qed_hwfn *p_hwfn)
202 {
203 	u32 flags;
204 
205 	/* common flags */
206 	flags = PQ_FLAGS_LB;
207 
208 	/* feature flags */
209 	if (IS_QED_SRIOV(p_hwfn->cdev))
210 		flags |= PQ_FLAGS_VFS;
211 
212 	/* protocol flags */
213 	switch (p_hwfn->hw_info.personality) {
214 	case QED_PCI_ETH:
215 		flags |= PQ_FLAGS_MCOS;
216 		break;
217 	case QED_PCI_FCOE:
218 		flags |= PQ_FLAGS_OFLD;
219 		break;
220 	case QED_PCI_ISCSI:
221 		flags |= PQ_FLAGS_ACK | PQ_FLAGS_OOO | PQ_FLAGS_OFLD;
222 		break;
223 	case QED_PCI_ETH_ROCE:
224 		flags |= PQ_FLAGS_MCOS | PQ_FLAGS_OFLD | PQ_FLAGS_LLT;
225 		if (IS_QED_MULTI_TC_ROCE(p_hwfn))
226 			flags |= PQ_FLAGS_MTC;
227 		break;
228 	case QED_PCI_ETH_IWARP:
229 		flags |= PQ_FLAGS_MCOS | PQ_FLAGS_ACK | PQ_FLAGS_OOO |
230 		    PQ_FLAGS_OFLD;
231 		break;
232 	default:
233 		DP_ERR(p_hwfn,
234 		       "unknown personality %d\n", p_hwfn->hw_info.personality);
235 		return 0;
236 	}
237 
238 	return flags;
239 }
240 
241 /* Getters for resource amounts necessary for qm initialization */
242 static u8 qed_init_qm_get_num_tcs(struct qed_hwfn *p_hwfn)
243 {
244 	return p_hwfn->hw_info.num_hw_tc;
245 }
246 
247 static u16 qed_init_qm_get_num_vfs(struct qed_hwfn *p_hwfn)
248 {
249 	return IS_QED_SRIOV(p_hwfn->cdev) ?
250 	       p_hwfn->cdev->p_iov_info->total_vfs : 0;
251 }
252 
253 static u8 qed_init_qm_get_num_mtc_tcs(struct qed_hwfn *p_hwfn)
254 {
255 	u32 pq_flags = qed_get_pq_flags(p_hwfn);
256 
257 	if (!(PQ_FLAGS_MTC & pq_flags))
258 		return 1;
259 
260 	return qed_init_qm_get_num_tcs(p_hwfn);
261 }
262 
263 #define NUM_DEFAULT_RLS 1
264 
265 static u16 qed_init_qm_get_num_pf_rls(struct qed_hwfn *p_hwfn)
266 {
267 	u16 num_pf_rls, num_vfs = qed_init_qm_get_num_vfs(p_hwfn);
268 
269 	/* num RLs can't exceed resource amount of rls or vports */
270 	num_pf_rls = (u16) min_t(u32, RESC_NUM(p_hwfn, QED_RL),
271 				 RESC_NUM(p_hwfn, QED_VPORT));
272 
273 	/* Make sure after we reserve there's something left */
274 	if (num_pf_rls < num_vfs + NUM_DEFAULT_RLS)
275 		return 0;
276 
277 	/* subtract rls necessary for VFs and one default one for the PF */
278 	num_pf_rls -= num_vfs + NUM_DEFAULT_RLS;
279 
280 	return num_pf_rls;
281 }
282 
283 static u16 qed_init_qm_get_num_vports(struct qed_hwfn *p_hwfn)
284 {
285 	u32 pq_flags = qed_get_pq_flags(p_hwfn);
286 
287 	/* all pqs share the same vport, except for vfs and pf_rl pqs */
288 	return (!!(PQ_FLAGS_RLS & pq_flags)) *
289 	       qed_init_qm_get_num_pf_rls(p_hwfn) +
290 	       (!!(PQ_FLAGS_VFS & pq_flags)) *
291 	       qed_init_qm_get_num_vfs(p_hwfn) + 1;
292 }
293 
294 /* calc amount of PQs according to the requested flags */
295 static u16 qed_init_qm_get_num_pqs(struct qed_hwfn *p_hwfn)
296 {
297 	u32 pq_flags = qed_get_pq_flags(p_hwfn);
298 
299 	return (!!(PQ_FLAGS_RLS & pq_flags)) *
300 	       qed_init_qm_get_num_pf_rls(p_hwfn) +
301 	       (!!(PQ_FLAGS_MCOS & pq_flags)) *
302 	       qed_init_qm_get_num_tcs(p_hwfn) +
303 	       (!!(PQ_FLAGS_LB & pq_flags)) + (!!(PQ_FLAGS_OOO & pq_flags)) +
304 	       (!!(PQ_FLAGS_ACK & pq_flags)) +
305 	       (!!(PQ_FLAGS_OFLD & pq_flags)) *
306 	       qed_init_qm_get_num_mtc_tcs(p_hwfn) +
307 	       (!!(PQ_FLAGS_LLT & pq_flags)) *
308 	       qed_init_qm_get_num_mtc_tcs(p_hwfn) +
309 	       (!!(PQ_FLAGS_VFS & pq_flags)) * qed_init_qm_get_num_vfs(p_hwfn);
310 }
311 
312 /* initialize the top level QM params */
313 static void qed_init_qm_params(struct qed_hwfn *p_hwfn)
314 {
315 	struct qed_qm_info *qm_info = &p_hwfn->qm_info;
316 	bool four_port;
317 
318 	/* pq and vport bases for this PF */
319 	qm_info->start_pq = (u16) RESC_START(p_hwfn, QED_PQ);
320 	qm_info->start_vport = (u8) RESC_START(p_hwfn, QED_VPORT);
321 
322 	/* rate limiting and weighted fair queueing are always enabled */
323 	qm_info->vport_rl_en = true;
324 	qm_info->vport_wfq_en = true;
325 
326 	/* TC config is different for AH 4 port */
327 	four_port = p_hwfn->cdev->num_ports_in_engine == MAX_NUM_PORTS_K2;
328 
329 	/* in AH 4 port we have fewer TCs per port */
330 	qm_info->max_phys_tcs_per_port = four_port ? NUM_PHYS_TCS_4PORT_K2 :
331 						     NUM_OF_PHYS_TCS;
332 
333 	/* unless MFW indicated otherwise, ooo_tc == 3 for
334 	 * AH 4-port and 4 otherwise.
335 	 */
336 	if (!qm_info->ooo_tc)
337 		qm_info->ooo_tc = four_port ? DCBX_TCP_OOO_K2_4PORT_TC :
338 					      DCBX_TCP_OOO_TC;
339 }
340 
341 /* initialize qm vport params */
342 static void qed_init_qm_vport_params(struct qed_hwfn *p_hwfn)
343 {
344 	struct qed_qm_info *qm_info = &p_hwfn->qm_info;
345 	u8 i;
346 
347 	/* all vports participate in weighted fair queueing */
348 	for (i = 0; i < qed_init_qm_get_num_vports(p_hwfn); i++)
349 		qm_info->qm_vport_params[i].vport_wfq = 1;
350 }
351 
352 /* initialize qm port params */
353 static void qed_init_qm_port_params(struct qed_hwfn *p_hwfn)
354 {
355 	/* Initialize qm port parameters */
356 	u8 i, active_phys_tcs, num_ports = p_hwfn->cdev->num_ports_in_engine;
357 
358 	/* indicate how ooo and high pri traffic is dealt with */
359 	active_phys_tcs = num_ports == MAX_NUM_PORTS_K2 ?
360 			  ACTIVE_TCS_BMAP_4PORT_K2 :
361 			  ACTIVE_TCS_BMAP;
362 
363 	for (i = 0; i < num_ports; i++) {
364 		struct init_qm_port_params *p_qm_port =
365 		    &p_hwfn->qm_info.qm_port_params[i];
366 
367 		p_qm_port->active = 1;
368 		p_qm_port->active_phys_tcs = active_phys_tcs;
369 		p_qm_port->num_pbf_cmd_lines = PBF_MAX_CMD_LINES / num_ports;
370 		p_qm_port->num_btb_blocks = BTB_MAX_BLOCKS / num_ports;
371 	}
372 }
373 
374 /* Reset the params which must be reset for qm init. QM init may be called as
375  * a result of flows other than driver load (e.g. dcbx renegotiation). Other
376  * params may be affected by the init but would simply recalculate to the same
377  * values. The allocations made for QM init, ports, vports, pqs and vfqs are not
378  * affected as these amounts stay the same.
379  */
380 static void qed_init_qm_reset_params(struct qed_hwfn *p_hwfn)
381 {
382 	struct qed_qm_info *qm_info = &p_hwfn->qm_info;
383 
384 	qm_info->num_pqs = 0;
385 	qm_info->num_vports = 0;
386 	qm_info->num_pf_rls = 0;
387 	qm_info->num_vf_pqs = 0;
388 	qm_info->first_vf_pq = 0;
389 	qm_info->first_mcos_pq = 0;
390 	qm_info->first_rl_pq = 0;
391 }
392 
393 static void qed_init_qm_advance_vport(struct qed_hwfn *p_hwfn)
394 {
395 	struct qed_qm_info *qm_info = &p_hwfn->qm_info;
396 
397 	qm_info->num_vports++;
398 
399 	if (qm_info->num_vports > qed_init_qm_get_num_vports(p_hwfn))
400 		DP_ERR(p_hwfn,
401 		       "vport overflow! qm_info->num_vports %d, qm_init_get_num_vports() %d\n",
402 		       qm_info->num_vports, qed_init_qm_get_num_vports(p_hwfn));
403 }
404 
405 /* initialize a single pq and manage qm_info resources accounting.
406  * The pq_init_flags param determines whether the PQ is rate limited
407  * (for VF or PF) and whether a new vport is allocated to the pq or not
408  * (i.e. vport will be shared).
409  */
410 
411 /* flags for pq init */
412 #define PQ_INIT_SHARE_VPORT     (1 << 0)
413 #define PQ_INIT_PF_RL           (1 << 1)
414 #define PQ_INIT_VF_RL           (1 << 2)
415 
416 /* defines for pq init */
417 #define PQ_INIT_DEFAULT_WRR_GROUP       1
418 #define PQ_INIT_DEFAULT_TC              0
419 
420 void qed_hw_info_set_offload_tc(struct qed_hw_info *p_info, u8 tc)
421 {
422 	p_info->offload_tc = tc;
423 	p_info->offload_tc_set = true;
424 }
425 
426 static bool qed_is_offload_tc_set(struct qed_hwfn *p_hwfn)
427 {
428 	return p_hwfn->hw_info.offload_tc_set;
429 }
430 
431 static u32 qed_get_offload_tc(struct qed_hwfn *p_hwfn)
432 {
433 	if (qed_is_offload_tc_set(p_hwfn))
434 		return p_hwfn->hw_info.offload_tc;
435 
436 	return PQ_INIT_DEFAULT_TC;
437 }
438 
439 static void qed_init_qm_pq(struct qed_hwfn *p_hwfn,
440 			   struct qed_qm_info *qm_info,
441 			   u8 tc, u32 pq_init_flags)
442 {
443 	u16 pq_idx = qm_info->num_pqs, max_pq = qed_init_qm_get_num_pqs(p_hwfn);
444 
445 	if (pq_idx > max_pq)
446 		DP_ERR(p_hwfn,
447 		       "pq overflow! pq %d, max pq %d\n", pq_idx, max_pq);
448 
449 	/* init pq params */
450 	qm_info->qm_pq_params[pq_idx].port_id = p_hwfn->port_id;
451 	qm_info->qm_pq_params[pq_idx].vport_id = qm_info->start_vport +
452 	    qm_info->num_vports;
453 	qm_info->qm_pq_params[pq_idx].tc_id = tc;
454 	qm_info->qm_pq_params[pq_idx].wrr_group = PQ_INIT_DEFAULT_WRR_GROUP;
455 	qm_info->qm_pq_params[pq_idx].rl_valid =
456 	    (pq_init_flags & PQ_INIT_PF_RL || pq_init_flags & PQ_INIT_VF_RL);
457 
458 	/* qm params accounting */
459 	qm_info->num_pqs++;
460 	if (!(pq_init_flags & PQ_INIT_SHARE_VPORT))
461 		qm_info->num_vports++;
462 
463 	if (pq_init_flags & PQ_INIT_PF_RL)
464 		qm_info->num_pf_rls++;
465 
466 	if (qm_info->num_vports > qed_init_qm_get_num_vports(p_hwfn))
467 		DP_ERR(p_hwfn,
468 		       "vport overflow! qm_info->num_vports %d, qm_init_get_num_vports() %d\n",
469 		       qm_info->num_vports, qed_init_qm_get_num_vports(p_hwfn));
470 
471 	if (qm_info->num_pf_rls > qed_init_qm_get_num_pf_rls(p_hwfn))
472 		DP_ERR(p_hwfn,
473 		       "rl overflow! qm_info->num_pf_rls %d, qm_init_get_num_pf_rls() %d\n",
474 		       qm_info->num_pf_rls, qed_init_qm_get_num_pf_rls(p_hwfn));
475 }
476 
477 /* get pq index according to PQ_FLAGS */
478 static u16 *qed_init_qm_get_idx_from_flags(struct qed_hwfn *p_hwfn,
479 					   u32 pq_flags)
480 {
481 	struct qed_qm_info *qm_info = &p_hwfn->qm_info;
482 
483 	/* Can't have multiple flags set here */
484 	if (bitmap_weight((unsigned long *)&pq_flags, sizeof(pq_flags)) > 1)
485 		goto err;
486 
487 	switch (pq_flags) {
488 	case PQ_FLAGS_RLS:
489 		return &qm_info->first_rl_pq;
490 	case PQ_FLAGS_MCOS:
491 		return &qm_info->first_mcos_pq;
492 	case PQ_FLAGS_LB:
493 		return &qm_info->pure_lb_pq;
494 	case PQ_FLAGS_OOO:
495 		return &qm_info->ooo_pq;
496 	case PQ_FLAGS_ACK:
497 		return &qm_info->pure_ack_pq;
498 	case PQ_FLAGS_OFLD:
499 		return &qm_info->first_ofld_pq;
500 	case PQ_FLAGS_LLT:
501 		return &qm_info->first_llt_pq;
502 	case PQ_FLAGS_VFS:
503 		return &qm_info->first_vf_pq;
504 	default:
505 		goto err;
506 	}
507 
508 err:
509 	DP_ERR(p_hwfn, "BAD pq flags %d\n", pq_flags);
510 	return NULL;
511 }
512 
513 /* save pq index in qm info */
514 static void qed_init_qm_set_idx(struct qed_hwfn *p_hwfn,
515 				u32 pq_flags, u16 pq_val)
516 {
517 	u16 *base_pq_idx = qed_init_qm_get_idx_from_flags(p_hwfn, pq_flags);
518 
519 	*base_pq_idx = p_hwfn->qm_info.start_pq + pq_val;
520 }
521 
522 /* get tx pq index, with the PQ TX base already set (ready for context init) */
523 u16 qed_get_cm_pq_idx(struct qed_hwfn *p_hwfn, u32 pq_flags)
524 {
525 	u16 *base_pq_idx = qed_init_qm_get_idx_from_flags(p_hwfn, pq_flags);
526 
527 	return *base_pq_idx + CM_TX_PQ_BASE;
528 }
529 
530 u16 qed_get_cm_pq_idx_mcos(struct qed_hwfn *p_hwfn, u8 tc)
531 {
532 	u8 max_tc = qed_init_qm_get_num_tcs(p_hwfn);
533 
534 	if (tc > max_tc)
535 		DP_ERR(p_hwfn, "tc %d must be smaller than %d\n", tc, max_tc);
536 
537 	return qed_get_cm_pq_idx(p_hwfn, PQ_FLAGS_MCOS) + tc;
538 }
539 
540 u16 qed_get_cm_pq_idx_vf(struct qed_hwfn *p_hwfn, u16 vf)
541 {
542 	u16 max_vf = qed_init_qm_get_num_vfs(p_hwfn);
543 
544 	if (vf > max_vf)
545 		DP_ERR(p_hwfn, "vf %d must be smaller than %d\n", vf, max_vf);
546 
547 	return qed_get_cm_pq_idx(p_hwfn, PQ_FLAGS_VFS) + vf;
548 }
549 
550 u16 qed_get_cm_pq_idx_ofld_mtc(struct qed_hwfn *p_hwfn, u8 tc)
551 {
552 	u16 first_ofld_pq, pq_offset;
553 
554 	first_ofld_pq = qed_get_cm_pq_idx(p_hwfn, PQ_FLAGS_OFLD);
555 	pq_offset = (tc < qed_init_qm_get_num_mtc_tcs(p_hwfn)) ?
556 		    tc : PQ_INIT_DEFAULT_TC;
557 
558 	return first_ofld_pq + pq_offset;
559 }
560 
561 u16 qed_get_cm_pq_idx_llt_mtc(struct qed_hwfn *p_hwfn, u8 tc)
562 {
563 	u16 first_llt_pq, pq_offset;
564 
565 	first_llt_pq = qed_get_cm_pq_idx(p_hwfn, PQ_FLAGS_LLT);
566 	pq_offset = (tc < qed_init_qm_get_num_mtc_tcs(p_hwfn)) ?
567 		    tc : PQ_INIT_DEFAULT_TC;
568 
569 	return first_llt_pq + pq_offset;
570 }
571 
572 /* Functions for creating specific types of pqs */
573 static void qed_init_qm_lb_pq(struct qed_hwfn *p_hwfn)
574 {
575 	struct qed_qm_info *qm_info = &p_hwfn->qm_info;
576 
577 	if (!(qed_get_pq_flags(p_hwfn) & PQ_FLAGS_LB))
578 		return;
579 
580 	qed_init_qm_set_idx(p_hwfn, PQ_FLAGS_LB, qm_info->num_pqs);
581 	qed_init_qm_pq(p_hwfn, qm_info, PURE_LB_TC, PQ_INIT_SHARE_VPORT);
582 }
583 
584 static void qed_init_qm_ooo_pq(struct qed_hwfn *p_hwfn)
585 {
586 	struct qed_qm_info *qm_info = &p_hwfn->qm_info;
587 
588 	if (!(qed_get_pq_flags(p_hwfn) & PQ_FLAGS_OOO))
589 		return;
590 
591 	qed_init_qm_set_idx(p_hwfn, PQ_FLAGS_OOO, qm_info->num_pqs);
592 	qed_init_qm_pq(p_hwfn, qm_info, qm_info->ooo_tc, PQ_INIT_SHARE_VPORT);
593 }
594 
595 static void qed_init_qm_pure_ack_pq(struct qed_hwfn *p_hwfn)
596 {
597 	struct qed_qm_info *qm_info = &p_hwfn->qm_info;
598 
599 	if (!(qed_get_pq_flags(p_hwfn) & PQ_FLAGS_ACK))
600 		return;
601 
602 	qed_init_qm_set_idx(p_hwfn, PQ_FLAGS_ACK, qm_info->num_pqs);
603 	qed_init_qm_pq(p_hwfn, qm_info, qed_get_offload_tc(p_hwfn),
604 		       PQ_INIT_SHARE_VPORT);
605 }
606 
607 static void qed_init_qm_mtc_pqs(struct qed_hwfn *p_hwfn)
608 {
609 	u8 num_tcs = qed_init_qm_get_num_mtc_tcs(p_hwfn);
610 	struct qed_qm_info *qm_info = &p_hwfn->qm_info;
611 	u8 tc;
612 
613 	/* override pq's TC if offload TC is set */
614 	for (tc = 0; tc < num_tcs; tc++)
615 		qed_init_qm_pq(p_hwfn, qm_info,
616 			       qed_is_offload_tc_set(p_hwfn) ?
617 			       p_hwfn->hw_info.offload_tc : tc,
618 			       PQ_INIT_SHARE_VPORT);
619 }
620 
621 static void qed_init_qm_offload_pq(struct qed_hwfn *p_hwfn)
622 {
623 	struct qed_qm_info *qm_info = &p_hwfn->qm_info;
624 
625 	if (!(qed_get_pq_flags(p_hwfn) & PQ_FLAGS_OFLD))
626 		return;
627 
628 	qed_init_qm_set_idx(p_hwfn, PQ_FLAGS_OFLD, qm_info->num_pqs);
629 	qed_init_qm_mtc_pqs(p_hwfn);
630 }
631 
632 static void qed_init_qm_low_latency_pq(struct qed_hwfn *p_hwfn)
633 {
634 	struct qed_qm_info *qm_info = &p_hwfn->qm_info;
635 
636 	if (!(qed_get_pq_flags(p_hwfn) & PQ_FLAGS_LLT))
637 		return;
638 
639 	qed_init_qm_set_idx(p_hwfn, PQ_FLAGS_LLT, qm_info->num_pqs);
640 	qed_init_qm_mtc_pqs(p_hwfn);
641 }
642 
643 static void qed_init_qm_mcos_pqs(struct qed_hwfn *p_hwfn)
644 {
645 	struct qed_qm_info *qm_info = &p_hwfn->qm_info;
646 	u8 tc_idx;
647 
648 	if (!(qed_get_pq_flags(p_hwfn) & PQ_FLAGS_MCOS))
649 		return;
650 
651 	qed_init_qm_set_idx(p_hwfn, PQ_FLAGS_MCOS, qm_info->num_pqs);
652 	for (tc_idx = 0; tc_idx < qed_init_qm_get_num_tcs(p_hwfn); tc_idx++)
653 		qed_init_qm_pq(p_hwfn, qm_info, tc_idx, PQ_INIT_SHARE_VPORT);
654 }
655 
656 static void qed_init_qm_vf_pqs(struct qed_hwfn *p_hwfn)
657 {
658 	struct qed_qm_info *qm_info = &p_hwfn->qm_info;
659 	u16 vf_idx, num_vfs = qed_init_qm_get_num_vfs(p_hwfn);
660 
661 	if (!(qed_get_pq_flags(p_hwfn) & PQ_FLAGS_VFS))
662 		return;
663 
664 	qed_init_qm_set_idx(p_hwfn, PQ_FLAGS_VFS, qm_info->num_pqs);
665 	qm_info->num_vf_pqs = num_vfs;
666 	for (vf_idx = 0; vf_idx < num_vfs; vf_idx++)
667 		qed_init_qm_pq(p_hwfn,
668 			       qm_info, PQ_INIT_DEFAULT_TC, PQ_INIT_VF_RL);
669 }
670 
671 static void qed_init_qm_rl_pqs(struct qed_hwfn *p_hwfn)
672 {
673 	u16 pf_rls_idx, num_pf_rls = qed_init_qm_get_num_pf_rls(p_hwfn);
674 	struct qed_qm_info *qm_info = &p_hwfn->qm_info;
675 
676 	if (!(qed_get_pq_flags(p_hwfn) & PQ_FLAGS_RLS))
677 		return;
678 
679 	qed_init_qm_set_idx(p_hwfn, PQ_FLAGS_RLS, qm_info->num_pqs);
680 	for (pf_rls_idx = 0; pf_rls_idx < num_pf_rls; pf_rls_idx++)
681 		qed_init_qm_pq(p_hwfn, qm_info, qed_get_offload_tc(p_hwfn),
682 			       PQ_INIT_PF_RL);
683 }
684 
685 static void qed_init_qm_pq_params(struct qed_hwfn *p_hwfn)
686 {
687 	/* rate limited pqs, must come first (FW assumption) */
688 	qed_init_qm_rl_pqs(p_hwfn);
689 
690 	/* pqs for multi cos */
691 	qed_init_qm_mcos_pqs(p_hwfn);
692 
693 	/* pure loopback pq */
694 	qed_init_qm_lb_pq(p_hwfn);
695 
696 	/* out of order pq */
697 	qed_init_qm_ooo_pq(p_hwfn);
698 
699 	/* pure ack pq */
700 	qed_init_qm_pure_ack_pq(p_hwfn);
701 
702 	/* pq for offloaded protocol */
703 	qed_init_qm_offload_pq(p_hwfn);
704 
705 	/* low latency pq */
706 	qed_init_qm_low_latency_pq(p_hwfn);
707 
708 	/* done sharing vports */
709 	qed_init_qm_advance_vport(p_hwfn);
710 
711 	/* pqs for vfs */
712 	qed_init_qm_vf_pqs(p_hwfn);
713 }
714 
715 /* compare values of getters against resources amounts */
716 static int qed_init_qm_sanity(struct qed_hwfn *p_hwfn)
717 {
718 	if (qed_init_qm_get_num_vports(p_hwfn) > RESC_NUM(p_hwfn, QED_VPORT)) {
719 		DP_ERR(p_hwfn, "requested amount of vports exceeds resource\n");
720 		return -EINVAL;
721 	}
722 
723 	if (qed_init_qm_get_num_pqs(p_hwfn) <= RESC_NUM(p_hwfn, QED_PQ))
724 		return 0;
725 
726 	if (QED_IS_ROCE_PERSONALITY(p_hwfn)) {
727 		p_hwfn->hw_info.multi_tc_roce_en = 0;
728 		DP_NOTICE(p_hwfn,
729 			  "multi-tc roce was disabled to reduce requested amount of pqs\n");
730 		if (qed_init_qm_get_num_pqs(p_hwfn) <= RESC_NUM(p_hwfn, QED_PQ))
731 			return 0;
732 	}
733 
734 	DP_ERR(p_hwfn, "requested amount of pqs exceeds resource\n");
735 	return -EINVAL;
736 }
737 
738 static void qed_dp_init_qm_params(struct qed_hwfn *p_hwfn)
739 {
740 	struct qed_qm_info *qm_info = &p_hwfn->qm_info;
741 	struct init_qm_vport_params *vport;
742 	struct init_qm_port_params *port;
743 	struct init_qm_pq_params *pq;
744 	int i, tc;
745 
746 	/* top level params */
747 	DP_VERBOSE(p_hwfn,
748 		   NETIF_MSG_HW,
749 		   "qm init top level params: start_pq %d, start_vport %d, pure_lb_pq %d, offload_pq %d, llt_pq %d, pure_ack_pq %d\n",
750 		   qm_info->start_pq,
751 		   qm_info->start_vport,
752 		   qm_info->pure_lb_pq,
753 		   qm_info->first_ofld_pq,
754 		   qm_info->first_llt_pq,
755 		   qm_info->pure_ack_pq);
756 	DP_VERBOSE(p_hwfn,
757 		   NETIF_MSG_HW,
758 		   "ooo_pq %d, first_vf_pq %d, num_pqs %d, num_vf_pqs %d, num_vports %d, max_phys_tcs_per_port %d\n",
759 		   qm_info->ooo_pq,
760 		   qm_info->first_vf_pq,
761 		   qm_info->num_pqs,
762 		   qm_info->num_vf_pqs,
763 		   qm_info->num_vports, qm_info->max_phys_tcs_per_port);
764 	DP_VERBOSE(p_hwfn,
765 		   NETIF_MSG_HW,
766 		   "pf_rl_en %d, pf_wfq_en %d, vport_rl_en %d, vport_wfq_en %d, pf_wfq %d, pf_rl %d, num_pf_rls %d, pq_flags %x\n",
767 		   qm_info->pf_rl_en,
768 		   qm_info->pf_wfq_en,
769 		   qm_info->vport_rl_en,
770 		   qm_info->vport_wfq_en,
771 		   qm_info->pf_wfq,
772 		   qm_info->pf_rl,
773 		   qm_info->num_pf_rls, qed_get_pq_flags(p_hwfn));
774 
775 	/* port table */
776 	for (i = 0; i < p_hwfn->cdev->num_ports_in_engine; i++) {
777 		port = &(qm_info->qm_port_params[i]);
778 		DP_VERBOSE(p_hwfn,
779 			   NETIF_MSG_HW,
780 			   "port idx %d, active %d, active_phys_tcs %d, num_pbf_cmd_lines %d, num_btb_blocks %d, reserved %d\n",
781 			   i,
782 			   port->active,
783 			   port->active_phys_tcs,
784 			   port->num_pbf_cmd_lines,
785 			   port->num_btb_blocks, port->reserved);
786 	}
787 
788 	/* vport table */
789 	for (i = 0; i < qm_info->num_vports; i++) {
790 		vport = &(qm_info->qm_vport_params[i]);
791 		DP_VERBOSE(p_hwfn,
792 			   NETIF_MSG_HW,
793 			   "vport idx %d, vport_rl %d, wfq %d, first_tx_pq_id [ ",
794 			   qm_info->start_vport + i,
795 			   vport->vport_rl, vport->vport_wfq);
796 		for (tc = 0; tc < NUM_OF_TCS; tc++)
797 			DP_VERBOSE(p_hwfn,
798 				   NETIF_MSG_HW,
799 				   "%d ", vport->first_tx_pq_id[tc]);
800 		DP_VERBOSE(p_hwfn, NETIF_MSG_HW, "]\n");
801 	}
802 
803 	/* pq table */
804 	for (i = 0; i < qm_info->num_pqs; i++) {
805 		pq = &(qm_info->qm_pq_params[i]);
806 		DP_VERBOSE(p_hwfn,
807 			   NETIF_MSG_HW,
808 			   "pq idx %d, port %d, vport_id %d, tc %d, wrr_grp %d, rl_valid %d\n",
809 			   qm_info->start_pq + i,
810 			   pq->port_id,
811 			   pq->vport_id,
812 			   pq->tc_id, pq->wrr_group, pq->rl_valid);
813 	}
814 }
815 
816 static void qed_init_qm_info(struct qed_hwfn *p_hwfn)
817 {
818 	/* reset params required for init run */
819 	qed_init_qm_reset_params(p_hwfn);
820 
821 	/* init QM top level params */
822 	qed_init_qm_params(p_hwfn);
823 
824 	/* init QM port params */
825 	qed_init_qm_port_params(p_hwfn);
826 
827 	/* init QM vport params */
828 	qed_init_qm_vport_params(p_hwfn);
829 
830 	/* init QM physical queue params */
831 	qed_init_qm_pq_params(p_hwfn);
832 
833 	/* display all that init */
834 	qed_dp_init_qm_params(p_hwfn);
835 }
836 
837 /* This function reconfigures the QM pf on the fly.
838  * For this purpose we:
839  * 1. reconfigure the QM database
840  * 2. set new values to runtime array
841  * 3. send an sdm_qm_cmd through the rbc interface to stop the QM
842  * 4. activate init tool in QM_PF stage
843  * 5. send an sdm_qm_cmd through rbc interface to release the QM
844  */
845 int qed_qm_reconf(struct qed_hwfn *p_hwfn, struct qed_ptt *p_ptt)
846 {
847 	struct qed_qm_info *qm_info = &p_hwfn->qm_info;
848 	bool b_rc;
849 	int rc;
850 
851 	/* initialize qed's qm data structure */
852 	qed_init_qm_info(p_hwfn);
853 
854 	/* stop PF's qm queues */
855 	spin_lock_bh(&qm_lock);
856 	b_rc = qed_send_qm_stop_cmd(p_hwfn, p_ptt, false, true,
857 				    qm_info->start_pq, qm_info->num_pqs);
858 	spin_unlock_bh(&qm_lock);
859 	if (!b_rc)
860 		return -EINVAL;
861 
862 	/* clear the QM_PF runtime phase leftovers from previous init */
863 	qed_init_clear_rt_data(p_hwfn);
864 
865 	/* prepare QM portion of runtime array */
866 	qed_qm_init_pf(p_hwfn, p_ptt, false);
867 
868 	/* activate init tool on runtime array */
869 	rc = qed_init_run(p_hwfn, p_ptt, PHASE_QM_PF, p_hwfn->rel_pf_id,
870 			  p_hwfn->hw_info.hw_mode);
871 	if (rc)
872 		return rc;
873 
874 	/* start PF's qm queues */
875 	spin_lock_bh(&qm_lock);
876 	b_rc = qed_send_qm_stop_cmd(p_hwfn, p_ptt, true, true,
877 				    qm_info->start_pq, qm_info->num_pqs);
878 	spin_unlock_bh(&qm_lock);
879 	if (!b_rc)
880 		return -EINVAL;
881 
882 	return 0;
883 }
884 
885 static int qed_alloc_qm_data(struct qed_hwfn *p_hwfn)
886 {
887 	struct qed_qm_info *qm_info = &p_hwfn->qm_info;
888 	int rc;
889 
890 	rc = qed_init_qm_sanity(p_hwfn);
891 	if (rc)
892 		goto alloc_err;
893 
894 	qm_info->qm_pq_params = kcalloc(qed_init_qm_get_num_pqs(p_hwfn),
895 					sizeof(*qm_info->qm_pq_params),
896 					GFP_KERNEL);
897 	if (!qm_info->qm_pq_params)
898 		goto alloc_err;
899 
900 	qm_info->qm_vport_params = kcalloc(qed_init_qm_get_num_vports(p_hwfn),
901 					   sizeof(*qm_info->qm_vport_params),
902 					   GFP_KERNEL);
903 	if (!qm_info->qm_vport_params)
904 		goto alloc_err;
905 
906 	qm_info->qm_port_params = kcalloc(p_hwfn->cdev->num_ports_in_engine,
907 					  sizeof(*qm_info->qm_port_params),
908 					  GFP_KERNEL);
909 	if (!qm_info->qm_port_params)
910 		goto alloc_err;
911 
912 	qm_info->wfq_data = kcalloc(qed_init_qm_get_num_vports(p_hwfn),
913 				    sizeof(*qm_info->wfq_data),
914 				    GFP_KERNEL);
915 	if (!qm_info->wfq_data)
916 		goto alloc_err;
917 
918 	return 0;
919 
920 alloc_err:
921 	DP_NOTICE(p_hwfn, "Failed to allocate memory for QM params\n");
922 	qed_qm_info_free(p_hwfn);
923 	return -ENOMEM;
924 }
925 
926 int qed_resc_alloc(struct qed_dev *cdev)
927 {
928 	u32 rdma_tasks, excess_tasks;
929 	u32 line_count;
930 	int i, rc = 0;
931 
932 	if (IS_VF(cdev)) {
933 		for_each_hwfn(cdev, i) {
934 			rc = qed_l2_alloc(&cdev->hwfns[i]);
935 			if (rc)
936 				return rc;
937 		}
938 		return rc;
939 	}
940 
941 	cdev->fw_data = kzalloc(sizeof(*cdev->fw_data), GFP_KERNEL);
942 	if (!cdev->fw_data)
943 		return -ENOMEM;
944 
945 	for_each_hwfn(cdev, i) {
946 		struct qed_hwfn *p_hwfn = &cdev->hwfns[i];
947 		u32 n_eqes, num_cons;
948 
949 		/* First allocate the context manager structure */
950 		rc = qed_cxt_mngr_alloc(p_hwfn);
951 		if (rc)
952 			goto alloc_err;
953 
954 		/* Set the HW cid/tid numbers (in the contest manager)
955 		 * Must be done prior to any further computations.
956 		 */
957 		rc = qed_cxt_set_pf_params(p_hwfn, RDMA_MAX_TIDS);
958 		if (rc)
959 			goto alloc_err;
960 
961 		rc = qed_alloc_qm_data(p_hwfn);
962 		if (rc)
963 			goto alloc_err;
964 
965 		/* init qm info */
966 		qed_init_qm_info(p_hwfn);
967 
968 		/* Compute the ILT client partition */
969 		rc = qed_cxt_cfg_ilt_compute(p_hwfn, &line_count);
970 		if (rc) {
971 			DP_NOTICE(p_hwfn,
972 				  "too many ILT lines; re-computing with less lines\n");
973 			/* In case there are not enough ILT lines we reduce the
974 			 * number of RDMA tasks and re-compute.
975 			 */
976 			excess_tasks =
977 			    qed_cxt_cfg_ilt_compute_excess(p_hwfn, line_count);
978 			if (!excess_tasks)
979 				goto alloc_err;
980 
981 			rdma_tasks = RDMA_MAX_TIDS - excess_tasks;
982 			rc = qed_cxt_set_pf_params(p_hwfn, rdma_tasks);
983 			if (rc)
984 				goto alloc_err;
985 
986 			rc = qed_cxt_cfg_ilt_compute(p_hwfn, &line_count);
987 			if (rc) {
988 				DP_ERR(p_hwfn,
989 				       "failed ILT compute. Requested too many lines: %u\n",
990 				       line_count);
991 
992 				goto alloc_err;
993 			}
994 		}
995 
996 		/* CID map / ILT shadow table / T2
997 		 * The talbes sizes are determined by the computations above
998 		 */
999 		rc = qed_cxt_tables_alloc(p_hwfn);
1000 		if (rc)
1001 			goto alloc_err;
1002 
1003 		/* SPQ, must follow ILT because initializes SPQ context */
1004 		rc = qed_spq_alloc(p_hwfn);
1005 		if (rc)
1006 			goto alloc_err;
1007 
1008 		/* SP status block allocation */
1009 		p_hwfn->p_dpc_ptt = qed_get_reserved_ptt(p_hwfn,
1010 							 RESERVED_PTT_DPC);
1011 
1012 		rc = qed_int_alloc(p_hwfn, p_hwfn->p_main_ptt);
1013 		if (rc)
1014 			goto alloc_err;
1015 
1016 		rc = qed_iov_alloc(p_hwfn);
1017 		if (rc)
1018 			goto alloc_err;
1019 
1020 		/* EQ */
1021 		n_eqes = qed_chain_get_capacity(&p_hwfn->p_spq->chain);
1022 		if (QED_IS_RDMA_PERSONALITY(p_hwfn)) {
1023 			enum protocol_type rdma_proto;
1024 
1025 			if (QED_IS_ROCE_PERSONALITY(p_hwfn))
1026 				rdma_proto = PROTOCOLID_ROCE;
1027 			else
1028 				rdma_proto = PROTOCOLID_IWARP;
1029 
1030 			num_cons = qed_cxt_get_proto_cid_count(p_hwfn,
1031 							       rdma_proto,
1032 							       NULL) * 2;
1033 			n_eqes += num_cons + 2 * MAX_NUM_VFS_BB;
1034 		} else if (p_hwfn->hw_info.personality == QED_PCI_ISCSI) {
1035 			num_cons =
1036 			    qed_cxt_get_proto_cid_count(p_hwfn,
1037 							PROTOCOLID_ISCSI,
1038 							NULL);
1039 			n_eqes += 2 * num_cons;
1040 		}
1041 
1042 		if (n_eqes > 0xFFFF) {
1043 			DP_ERR(p_hwfn,
1044 			       "Cannot allocate 0x%x EQ elements. The maximum of a u16 chain is 0x%x\n",
1045 			       n_eqes, 0xFFFF);
1046 			goto alloc_no_mem;
1047 		}
1048 
1049 		rc = qed_eq_alloc(p_hwfn, (u16) n_eqes);
1050 		if (rc)
1051 			goto alloc_err;
1052 
1053 		rc = qed_consq_alloc(p_hwfn);
1054 		if (rc)
1055 			goto alloc_err;
1056 
1057 		rc = qed_l2_alloc(p_hwfn);
1058 		if (rc)
1059 			goto alloc_err;
1060 
1061 #ifdef CONFIG_QED_LL2
1062 		if (p_hwfn->using_ll2) {
1063 			rc = qed_ll2_alloc(p_hwfn);
1064 			if (rc)
1065 				goto alloc_err;
1066 		}
1067 #endif
1068 
1069 		if (p_hwfn->hw_info.personality == QED_PCI_FCOE) {
1070 			rc = qed_fcoe_alloc(p_hwfn);
1071 			if (rc)
1072 				goto alloc_err;
1073 		}
1074 
1075 		if (p_hwfn->hw_info.personality == QED_PCI_ISCSI) {
1076 			rc = qed_iscsi_alloc(p_hwfn);
1077 			if (rc)
1078 				goto alloc_err;
1079 			rc = qed_ooo_alloc(p_hwfn);
1080 			if (rc)
1081 				goto alloc_err;
1082 		}
1083 
1084 		/* DMA info initialization */
1085 		rc = qed_dmae_info_alloc(p_hwfn);
1086 		if (rc)
1087 			goto alloc_err;
1088 
1089 		/* DCBX initialization */
1090 		rc = qed_dcbx_info_alloc(p_hwfn);
1091 		if (rc)
1092 			goto alloc_err;
1093 
1094 		rc = qed_dbg_alloc_user_data(p_hwfn);
1095 		if (rc)
1096 			goto alloc_err;
1097 	}
1098 
1099 	cdev->reset_stats = kzalloc(sizeof(*cdev->reset_stats), GFP_KERNEL);
1100 	if (!cdev->reset_stats)
1101 		goto alloc_no_mem;
1102 
1103 	return 0;
1104 
1105 alloc_no_mem:
1106 	rc = -ENOMEM;
1107 alloc_err:
1108 	qed_resc_free(cdev);
1109 	return rc;
1110 }
1111 
1112 void qed_resc_setup(struct qed_dev *cdev)
1113 {
1114 	int i;
1115 
1116 	if (IS_VF(cdev)) {
1117 		for_each_hwfn(cdev, i)
1118 			qed_l2_setup(&cdev->hwfns[i]);
1119 		return;
1120 	}
1121 
1122 	for_each_hwfn(cdev, i) {
1123 		struct qed_hwfn *p_hwfn = &cdev->hwfns[i];
1124 
1125 		qed_cxt_mngr_setup(p_hwfn);
1126 		qed_spq_setup(p_hwfn);
1127 		qed_eq_setup(p_hwfn);
1128 		qed_consq_setup(p_hwfn);
1129 
1130 		/* Read shadow of current MFW mailbox */
1131 		qed_mcp_read_mb(p_hwfn, p_hwfn->p_main_ptt);
1132 		memcpy(p_hwfn->mcp_info->mfw_mb_shadow,
1133 		       p_hwfn->mcp_info->mfw_mb_cur,
1134 		       p_hwfn->mcp_info->mfw_mb_length);
1135 
1136 		qed_int_setup(p_hwfn, p_hwfn->p_main_ptt);
1137 
1138 		qed_l2_setup(p_hwfn);
1139 		qed_iov_setup(p_hwfn);
1140 #ifdef CONFIG_QED_LL2
1141 		if (p_hwfn->using_ll2)
1142 			qed_ll2_setup(p_hwfn);
1143 #endif
1144 		if (p_hwfn->hw_info.personality == QED_PCI_FCOE)
1145 			qed_fcoe_setup(p_hwfn);
1146 
1147 		if (p_hwfn->hw_info.personality == QED_PCI_ISCSI) {
1148 			qed_iscsi_setup(p_hwfn);
1149 			qed_ooo_setup(p_hwfn);
1150 		}
1151 	}
1152 }
1153 
1154 #define FINAL_CLEANUP_POLL_CNT          (100)
1155 #define FINAL_CLEANUP_POLL_TIME         (10)
1156 int qed_final_cleanup(struct qed_hwfn *p_hwfn,
1157 		      struct qed_ptt *p_ptt, u16 id, bool is_vf)
1158 {
1159 	u32 command = 0, addr, count = FINAL_CLEANUP_POLL_CNT;
1160 	int rc = -EBUSY;
1161 
1162 	addr = GTT_BAR0_MAP_REG_USDM_RAM +
1163 		USTORM_FLR_FINAL_ACK_OFFSET(p_hwfn->rel_pf_id);
1164 
1165 	if (is_vf)
1166 		id += 0x10;
1167 
1168 	command |= X_FINAL_CLEANUP_AGG_INT <<
1169 		SDM_AGG_INT_COMP_PARAMS_AGG_INT_INDEX_SHIFT;
1170 	command |= 1 << SDM_AGG_INT_COMP_PARAMS_AGG_VECTOR_ENABLE_SHIFT;
1171 	command |= id << SDM_AGG_INT_COMP_PARAMS_AGG_VECTOR_BIT_SHIFT;
1172 	command |= SDM_COMP_TYPE_AGG_INT << SDM_OP_GEN_COMP_TYPE_SHIFT;
1173 
1174 	/* Make sure notification is not set before initiating final cleanup */
1175 	if (REG_RD(p_hwfn, addr)) {
1176 		DP_NOTICE(p_hwfn,
1177 			  "Unexpected; Found final cleanup notification before initiating final cleanup\n");
1178 		REG_WR(p_hwfn, addr, 0);
1179 	}
1180 
1181 	DP_VERBOSE(p_hwfn, QED_MSG_IOV,
1182 		   "Sending final cleanup for PFVF[%d] [Command %08x]\n",
1183 		   id, command);
1184 
1185 	qed_wr(p_hwfn, p_ptt, XSDM_REG_OPERATION_GEN, command);
1186 
1187 	/* Poll until completion */
1188 	while (!REG_RD(p_hwfn, addr) && count--)
1189 		msleep(FINAL_CLEANUP_POLL_TIME);
1190 
1191 	if (REG_RD(p_hwfn, addr))
1192 		rc = 0;
1193 	else
1194 		DP_NOTICE(p_hwfn,
1195 			  "Failed to receive FW final cleanup notification\n");
1196 
1197 	/* Cleanup afterwards */
1198 	REG_WR(p_hwfn, addr, 0);
1199 
1200 	return rc;
1201 }
1202 
1203 static int qed_calc_hw_mode(struct qed_hwfn *p_hwfn)
1204 {
1205 	int hw_mode = 0;
1206 
1207 	if (QED_IS_BB_B0(p_hwfn->cdev)) {
1208 		hw_mode |= 1 << MODE_BB;
1209 	} else if (QED_IS_AH(p_hwfn->cdev)) {
1210 		hw_mode |= 1 << MODE_K2;
1211 	} else {
1212 		DP_NOTICE(p_hwfn, "Unknown chip type %#x\n",
1213 			  p_hwfn->cdev->type);
1214 		return -EINVAL;
1215 	}
1216 
1217 	switch (p_hwfn->cdev->num_ports_in_engine) {
1218 	case 1:
1219 		hw_mode |= 1 << MODE_PORTS_PER_ENG_1;
1220 		break;
1221 	case 2:
1222 		hw_mode |= 1 << MODE_PORTS_PER_ENG_2;
1223 		break;
1224 	case 4:
1225 		hw_mode |= 1 << MODE_PORTS_PER_ENG_4;
1226 		break;
1227 	default:
1228 		DP_NOTICE(p_hwfn, "num_ports_in_engine = %d not supported\n",
1229 			  p_hwfn->cdev->num_ports_in_engine);
1230 		return -EINVAL;
1231 	}
1232 
1233 	if (test_bit(QED_MF_OVLAN_CLSS, &p_hwfn->cdev->mf_bits))
1234 		hw_mode |= 1 << MODE_MF_SD;
1235 	else
1236 		hw_mode |= 1 << MODE_MF_SI;
1237 
1238 	hw_mode |= 1 << MODE_ASIC;
1239 
1240 	if (p_hwfn->cdev->num_hwfns > 1)
1241 		hw_mode |= 1 << MODE_100G;
1242 
1243 	p_hwfn->hw_info.hw_mode = hw_mode;
1244 
1245 	DP_VERBOSE(p_hwfn, (NETIF_MSG_PROBE | NETIF_MSG_IFUP),
1246 		   "Configuring function for hw_mode: 0x%08x\n",
1247 		   p_hwfn->hw_info.hw_mode);
1248 
1249 	return 0;
1250 }
1251 
1252 /* Init run time data for all PFs on an engine. */
1253 static void qed_init_cau_rt_data(struct qed_dev *cdev)
1254 {
1255 	u32 offset = CAU_REG_SB_VAR_MEMORY_RT_OFFSET;
1256 	int i, igu_sb_id;
1257 
1258 	for_each_hwfn(cdev, i) {
1259 		struct qed_hwfn *p_hwfn = &cdev->hwfns[i];
1260 		struct qed_igu_info *p_igu_info;
1261 		struct qed_igu_block *p_block;
1262 		struct cau_sb_entry sb_entry;
1263 
1264 		p_igu_info = p_hwfn->hw_info.p_igu_info;
1265 
1266 		for (igu_sb_id = 0;
1267 		     igu_sb_id < QED_MAPPING_MEMORY_SIZE(cdev); igu_sb_id++) {
1268 			p_block = &p_igu_info->entry[igu_sb_id];
1269 
1270 			if (!p_block->is_pf)
1271 				continue;
1272 
1273 			qed_init_cau_sb_entry(p_hwfn, &sb_entry,
1274 					      p_block->function_id, 0, 0);
1275 			STORE_RT_REG_AGG(p_hwfn, offset + igu_sb_id * 2,
1276 					 sb_entry);
1277 		}
1278 	}
1279 }
1280 
1281 static void qed_init_cache_line_size(struct qed_hwfn *p_hwfn,
1282 				     struct qed_ptt *p_ptt)
1283 {
1284 	u32 val, wr_mbs, cache_line_size;
1285 
1286 	val = qed_rd(p_hwfn, p_ptt, PSWRQ2_REG_WR_MBS0);
1287 	switch (val) {
1288 	case 0:
1289 		wr_mbs = 128;
1290 		break;
1291 	case 1:
1292 		wr_mbs = 256;
1293 		break;
1294 	case 2:
1295 		wr_mbs = 512;
1296 		break;
1297 	default:
1298 		DP_INFO(p_hwfn,
1299 			"Unexpected value of PSWRQ2_REG_WR_MBS0 [0x%x]. Avoid configuring PGLUE_B_REG_CACHE_LINE_SIZE.\n",
1300 			val);
1301 		return;
1302 	}
1303 
1304 	cache_line_size = min_t(u32, L1_CACHE_BYTES, wr_mbs);
1305 	switch (cache_line_size) {
1306 	case 32:
1307 		val = 0;
1308 		break;
1309 	case 64:
1310 		val = 1;
1311 		break;
1312 	case 128:
1313 		val = 2;
1314 		break;
1315 	case 256:
1316 		val = 3;
1317 		break;
1318 	default:
1319 		DP_INFO(p_hwfn,
1320 			"Unexpected value of cache line size [0x%x]. Avoid configuring PGLUE_B_REG_CACHE_LINE_SIZE.\n",
1321 			cache_line_size);
1322 	}
1323 
1324 	if (L1_CACHE_BYTES > wr_mbs)
1325 		DP_INFO(p_hwfn,
1326 			"The cache line size for padding is suboptimal for performance [OS cache line size 0x%x, wr mbs 0x%x]\n",
1327 			L1_CACHE_BYTES, wr_mbs);
1328 
1329 	STORE_RT_REG(p_hwfn, PGLUE_REG_B_CACHE_LINE_SIZE_RT_OFFSET, val);
1330 	if (val > 0) {
1331 		STORE_RT_REG(p_hwfn, PSWRQ2_REG_DRAM_ALIGN_WR_RT_OFFSET, val);
1332 		STORE_RT_REG(p_hwfn, PSWRQ2_REG_DRAM_ALIGN_RD_RT_OFFSET, val);
1333 	}
1334 }
1335 
1336 static int qed_hw_init_common(struct qed_hwfn *p_hwfn,
1337 			      struct qed_ptt *p_ptt, int hw_mode)
1338 {
1339 	struct qed_qm_info *qm_info = &p_hwfn->qm_info;
1340 	struct qed_qm_common_rt_init_params params;
1341 	struct qed_dev *cdev = p_hwfn->cdev;
1342 	u8 vf_id, max_num_vfs;
1343 	u16 num_pfs, pf_id;
1344 	u32 concrete_fid;
1345 	int rc = 0;
1346 
1347 	qed_init_cau_rt_data(cdev);
1348 
1349 	/* Program GTT windows */
1350 	qed_gtt_init(p_hwfn);
1351 
1352 	if (p_hwfn->mcp_info) {
1353 		if (p_hwfn->mcp_info->func_info.bandwidth_max)
1354 			qm_info->pf_rl_en = true;
1355 		if (p_hwfn->mcp_info->func_info.bandwidth_min)
1356 			qm_info->pf_wfq_en = true;
1357 	}
1358 
1359 	memset(&params, 0, sizeof(params));
1360 	params.max_ports_per_engine = p_hwfn->cdev->num_ports_in_engine;
1361 	params.max_phys_tcs_per_port = qm_info->max_phys_tcs_per_port;
1362 	params.pf_rl_en = qm_info->pf_rl_en;
1363 	params.pf_wfq_en = qm_info->pf_wfq_en;
1364 	params.vport_rl_en = qm_info->vport_rl_en;
1365 	params.vport_wfq_en = qm_info->vport_wfq_en;
1366 	params.port_params = qm_info->qm_port_params;
1367 
1368 	qed_qm_common_rt_init(p_hwfn, &params);
1369 
1370 	qed_cxt_hw_init_common(p_hwfn);
1371 
1372 	qed_init_cache_line_size(p_hwfn, p_ptt);
1373 
1374 	rc = qed_init_run(p_hwfn, p_ptt, PHASE_ENGINE, ANY_PHASE_ID, hw_mode);
1375 	if (rc)
1376 		return rc;
1377 
1378 	qed_wr(p_hwfn, p_ptt, PSWRQ2_REG_L2P_VALIDATE_VFID, 0);
1379 	qed_wr(p_hwfn, p_ptt, PGLUE_B_REG_USE_CLIENTID_IN_TAG, 1);
1380 
1381 	if (QED_IS_BB(p_hwfn->cdev)) {
1382 		num_pfs = NUM_OF_ENG_PFS(p_hwfn->cdev);
1383 		for (pf_id = 0; pf_id < num_pfs; pf_id++) {
1384 			qed_fid_pretend(p_hwfn, p_ptt, pf_id);
1385 			qed_wr(p_hwfn, p_ptt, PRS_REG_SEARCH_ROCE, 0x0);
1386 			qed_wr(p_hwfn, p_ptt, PRS_REG_SEARCH_TCP, 0x0);
1387 		}
1388 		/* pretend to original PF */
1389 		qed_fid_pretend(p_hwfn, p_ptt, p_hwfn->rel_pf_id);
1390 	}
1391 
1392 	max_num_vfs = QED_IS_AH(cdev) ? MAX_NUM_VFS_K2 : MAX_NUM_VFS_BB;
1393 	for (vf_id = 0; vf_id < max_num_vfs; vf_id++) {
1394 		concrete_fid = qed_vfid_to_concrete(p_hwfn, vf_id);
1395 		qed_fid_pretend(p_hwfn, p_ptt, (u16) concrete_fid);
1396 		qed_wr(p_hwfn, p_ptt, CCFC_REG_STRONG_ENABLE_VF, 0x1);
1397 		qed_wr(p_hwfn, p_ptt, CCFC_REG_WEAK_ENABLE_VF, 0x0);
1398 		qed_wr(p_hwfn, p_ptt, TCFC_REG_STRONG_ENABLE_VF, 0x1);
1399 		qed_wr(p_hwfn, p_ptt, TCFC_REG_WEAK_ENABLE_VF, 0x0);
1400 	}
1401 	/* pretend to original PF */
1402 	qed_fid_pretend(p_hwfn, p_ptt, p_hwfn->rel_pf_id);
1403 
1404 	return rc;
1405 }
1406 
1407 static int
1408 qed_hw_init_dpi_size(struct qed_hwfn *p_hwfn,
1409 		     struct qed_ptt *p_ptt, u32 pwm_region_size, u32 n_cpus)
1410 {
1411 	u32 dpi_bit_shift, dpi_count, dpi_page_size;
1412 	u32 min_dpis;
1413 	u32 n_wids;
1414 
1415 	/* Calculate DPI size */
1416 	n_wids = max_t(u32, QED_MIN_WIDS, n_cpus);
1417 	dpi_page_size = QED_WID_SIZE * roundup_pow_of_two(n_wids);
1418 	dpi_page_size = (dpi_page_size + PAGE_SIZE - 1) & ~(PAGE_SIZE - 1);
1419 	dpi_bit_shift = ilog2(dpi_page_size / 4096);
1420 	dpi_count = pwm_region_size / dpi_page_size;
1421 
1422 	min_dpis = p_hwfn->pf_params.rdma_pf_params.min_dpis;
1423 	min_dpis = max_t(u32, QED_MIN_DPIS, min_dpis);
1424 
1425 	p_hwfn->dpi_size = dpi_page_size;
1426 	p_hwfn->dpi_count = dpi_count;
1427 
1428 	qed_wr(p_hwfn, p_ptt, DORQ_REG_PF_DPI_BIT_SHIFT, dpi_bit_shift);
1429 
1430 	if (dpi_count < min_dpis)
1431 		return -EINVAL;
1432 
1433 	return 0;
1434 }
1435 
1436 enum QED_ROCE_EDPM_MODE {
1437 	QED_ROCE_EDPM_MODE_ENABLE = 0,
1438 	QED_ROCE_EDPM_MODE_FORCE_ON = 1,
1439 	QED_ROCE_EDPM_MODE_DISABLE = 2,
1440 };
1441 
1442 static int
1443 qed_hw_init_pf_doorbell_bar(struct qed_hwfn *p_hwfn, struct qed_ptt *p_ptt)
1444 {
1445 	u32 pwm_regsize, norm_regsize;
1446 	u32 non_pwm_conn, min_addr_reg1;
1447 	u32 db_bar_size, n_cpus = 1;
1448 	u32 roce_edpm_mode;
1449 	u32 pf_dems_shift;
1450 	int rc = 0;
1451 	u8 cond;
1452 
1453 	db_bar_size = qed_hw_bar_size(p_hwfn, p_ptt, BAR_ID_1);
1454 	if (p_hwfn->cdev->num_hwfns > 1)
1455 		db_bar_size /= 2;
1456 
1457 	/* Calculate doorbell regions */
1458 	non_pwm_conn = qed_cxt_get_proto_cid_start(p_hwfn, PROTOCOLID_CORE) +
1459 		       qed_cxt_get_proto_cid_count(p_hwfn, PROTOCOLID_CORE,
1460 						   NULL) +
1461 		       qed_cxt_get_proto_cid_count(p_hwfn, PROTOCOLID_ETH,
1462 						   NULL);
1463 	norm_regsize = roundup(QED_PF_DEMS_SIZE * non_pwm_conn, PAGE_SIZE);
1464 	min_addr_reg1 = norm_regsize / 4096;
1465 	pwm_regsize = db_bar_size - norm_regsize;
1466 
1467 	/* Check that the normal and PWM sizes are valid */
1468 	if (db_bar_size < norm_regsize) {
1469 		DP_ERR(p_hwfn->cdev,
1470 		       "Doorbell BAR size 0x%x is too small (normal region is 0x%0x )\n",
1471 		       db_bar_size, norm_regsize);
1472 		return -EINVAL;
1473 	}
1474 
1475 	if (pwm_regsize < QED_MIN_PWM_REGION) {
1476 		DP_ERR(p_hwfn->cdev,
1477 		       "PWM region size 0x%0x is too small. Should be at least 0x%0x (Doorbell BAR size is 0x%x and normal region size is 0x%0x)\n",
1478 		       pwm_regsize,
1479 		       QED_MIN_PWM_REGION, db_bar_size, norm_regsize);
1480 		return -EINVAL;
1481 	}
1482 
1483 	/* Calculate number of DPIs */
1484 	roce_edpm_mode = p_hwfn->pf_params.rdma_pf_params.roce_edpm_mode;
1485 	if ((roce_edpm_mode == QED_ROCE_EDPM_MODE_ENABLE) ||
1486 	    ((roce_edpm_mode == QED_ROCE_EDPM_MODE_FORCE_ON))) {
1487 		/* Either EDPM is mandatory, or we are attempting to allocate a
1488 		 * WID per CPU.
1489 		 */
1490 		n_cpus = num_present_cpus();
1491 		rc = qed_hw_init_dpi_size(p_hwfn, p_ptt, pwm_regsize, n_cpus);
1492 	}
1493 
1494 	cond = (rc && (roce_edpm_mode == QED_ROCE_EDPM_MODE_ENABLE)) ||
1495 	       (roce_edpm_mode == QED_ROCE_EDPM_MODE_DISABLE);
1496 	if (cond || p_hwfn->dcbx_no_edpm) {
1497 		/* Either EDPM is disabled from user configuration, or it is
1498 		 * disabled via DCBx, or it is not mandatory and we failed to
1499 		 * allocated a WID per CPU.
1500 		 */
1501 		n_cpus = 1;
1502 		rc = qed_hw_init_dpi_size(p_hwfn, p_ptt, pwm_regsize, n_cpus);
1503 
1504 		if (cond)
1505 			qed_rdma_dpm_bar(p_hwfn, p_ptt);
1506 	}
1507 
1508 	p_hwfn->wid_count = (u16) n_cpus;
1509 
1510 	DP_INFO(p_hwfn,
1511 		"doorbell bar: normal_region_size=%d, pwm_region_size=%d, dpi_size=%d, dpi_count=%d, roce_edpm=%s\n",
1512 		norm_regsize,
1513 		pwm_regsize,
1514 		p_hwfn->dpi_size,
1515 		p_hwfn->dpi_count,
1516 		((p_hwfn->dcbx_no_edpm) || (p_hwfn->db_bar_no_edpm)) ?
1517 		"disabled" : "enabled");
1518 
1519 	if (rc) {
1520 		DP_ERR(p_hwfn,
1521 		       "Failed to allocate enough DPIs. Allocated %d but the current minimum is %d.\n",
1522 		       p_hwfn->dpi_count,
1523 		       p_hwfn->pf_params.rdma_pf_params.min_dpis);
1524 		return -EINVAL;
1525 	}
1526 
1527 	p_hwfn->dpi_start_offset = norm_regsize;
1528 
1529 	/* DEMS size is configured log2 of DWORDs, hence the division by 4 */
1530 	pf_dems_shift = ilog2(QED_PF_DEMS_SIZE / 4);
1531 	qed_wr(p_hwfn, p_ptt, DORQ_REG_PF_ICID_BIT_SHIFT_NORM, pf_dems_shift);
1532 	qed_wr(p_hwfn, p_ptt, DORQ_REG_PF_MIN_ADDR_REG1, min_addr_reg1);
1533 
1534 	return 0;
1535 }
1536 
1537 static int qed_hw_init_port(struct qed_hwfn *p_hwfn,
1538 			    struct qed_ptt *p_ptt, int hw_mode)
1539 {
1540 	int rc = 0;
1541 
1542 	rc = qed_init_run(p_hwfn, p_ptt, PHASE_PORT, p_hwfn->port_id, hw_mode);
1543 	if (rc)
1544 		return rc;
1545 
1546 	qed_wr(p_hwfn, p_ptt, PGLUE_B_REG_MASTER_WRITE_PAD_ENABLE, 0);
1547 
1548 	return 0;
1549 }
1550 
1551 static int qed_hw_init_pf(struct qed_hwfn *p_hwfn,
1552 			  struct qed_ptt *p_ptt,
1553 			  struct qed_tunnel_info *p_tunn,
1554 			  int hw_mode,
1555 			  bool b_hw_start,
1556 			  enum qed_int_mode int_mode,
1557 			  bool allow_npar_tx_switch)
1558 {
1559 	u8 rel_pf_id = p_hwfn->rel_pf_id;
1560 	int rc = 0;
1561 
1562 	if (p_hwfn->mcp_info) {
1563 		struct qed_mcp_function_info *p_info;
1564 
1565 		p_info = &p_hwfn->mcp_info->func_info;
1566 		if (p_info->bandwidth_min)
1567 			p_hwfn->qm_info.pf_wfq = p_info->bandwidth_min;
1568 
1569 		/* Update rate limit once we'll actually have a link */
1570 		p_hwfn->qm_info.pf_rl = 100000;
1571 	}
1572 
1573 	qed_cxt_hw_init_pf(p_hwfn, p_ptt);
1574 
1575 	qed_int_igu_init_rt(p_hwfn);
1576 
1577 	/* Set VLAN in NIG if needed */
1578 	if (hw_mode & BIT(MODE_MF_SD)) {
1579 		DP_VERBOSE(p_hwfn, NETIF_MSG_HW, "Configuring LLH_FUNC_TAG\n");
1580 		STORE_RT_REG(p_hwfn, NIG_REG_LLH_FUNC_TAG_EN_RT_OFFSET, 1);
1581 		STORE_RT_REG(p_hwfn, NIG_REG_LLH_FUNC_TAG_VALUE_RT_OFFSET,
1582 			     p_hwfn->hw_info.ovlan);
1583 
1584 		DP_VERBOSE(p_hwfn, NETIF_MSG_HW,
1585 			   "Configuring LLH_FUNC_FILTER_HDR_SEL\n");
1586 		STORE_RT_REG(p_hwfn, NIG_REG_LLH_FUNC_FILTER_HDR_SEL_RT_OFFSET,
1587 			     1);
1588 	}
1589 
1590 	/* Enable classification by MAC if needed */
1591 	if (hw_mode & BIT(MODE_MF_SI)) {
1592 		DP_VERBOSE(p_hwfn, NETIF_MSG_HW,
1593 			   "Configuring TAGMAC_CLS_TYPE\n");
1594 		STORE_RT_REG(p_hwfn,
1595 			     NIG_REG_LLH_FUNC_TAGMAC_CLS_TYPE_RT_OFFSET, 1);
1596 	}
1597 
1598 	/* Protocol Configuration */
1599 	STORE_RT_REG(p_hwfn, PRS_REG_SEARCH_TCP_RT_OFFSET,
1600 		     (p_hwfn->hw_info.personality == QED_PCI_ISCSI) ? 1 : 0);
1601 	STORE_RT_REG(p_hwfn, PRS_REG_SEARCH_FCOE_RT_OFFSET,
1602 		     (p_hwfn->hw_info.personality == QED_PCI_FCOE) ? 1 : 0);
1603 	STORE_RT_REG(p_hwfn, PRS_REG_SEARCH_ROCE_RT_OFFSET, 0);
1604 
1605 	/* Cleanup chip from previous driver if such remains exist */
1606 	rc = qed_final_cleanup(p_hwfn, p_ptt, rel_pf_id, false);
1607 	if (rc)
1608 		return rc;
1609 
1610 	/* Sanity check before the PF init sequence that uses DMAE */
1611 	rc = qed_dmae_sanity(p_hwfn, p_ptt, "pf_phase");
1612 	if (rc)
1613 		return rc;
1614 
1615 	/* PF Init sequence */
1616 	rc = qed_init_run(p_hwfn, p_ptt, PHASE_PF, rel_pf_id, hw_mode);
1617 	if (rc)
1618 		return rc;
1619 
1620 	/* QM_PF Init sequence (may be invoked separately e.g. for DCB) */
1621 	rc = qed_init_run(p_hwfn, p_ptt, PHASE_QM_PF, rel_pf_id, hw_mode);
1622 	if (rc)
1623 		return rc;
1624 
1625 	/* Pure runtime initializations - directly to the HW  */
1626 	qed_int_igu_init_pure_rt(p_hwfn, p_ptt, true, true);
1627 
1628 	rc = qed_hw_init_pf_doorbell_bar(p_hwfn, p_ptt);
1629 	if (rc)
1630 		return rc;
1631 
1632 	if (b_hw_start) {
1633 		/* enable interrupts */
1634 		qed_int_igu_enable(p_hwfn, p_ptt, int_mode);
1635 
1636 		/* send function start command */
1637 		rc = qed_sp_pf_start(p_hwfn, p_ptt, p_tunn,
1638 				     allow_npar_tx_switch);
1639 		if (rc) {
1640 			DP_NOTICE(p_hwfn, "Function start ramrod failed\n");
1641 			return rc;
1642 		}
1643 		if (p_hwfn->hw_info.personality == QED_PCI_FCOE) {
1644 			qed_wr(p_hwfn, p_ptt, PRS_REG_SEARCH_TAG1, BIT(2));
1645 			qed_wr(p_hwfn, p_ptt,
1646 			       PRS_REG_PKT_LEN_STAT_TAGS_NOT_COUNTED_FIRST,
1647 			       0x100);
1648 		}
1649 	}
1650 	return rc;
1651 }
1652 
1653 static int qed_change_pci_hwfn(struct qed_hwfn *p_hwfn,
1654 			       struct qed_ptt *p_ptt,
1655 			       u8 enable)
1656 {
1657 	u32 delay_idx = 0, val, set_val = enable ? 1 : 0;
1658 
1659 	/* Change PF in PXP */
1660 	qed_wr(p_hwfn, p_ptt,
1661 	       PGLUE_B_REG_INTERNAL_PFID_ENABLE_MASTER, set_val);
1662 
1663 	/* wait until value is set - try for 1 second every 50us */
1664 	for (delay_idx = 0; delay_idx < 20000; delay_idx++) {
1665 		val = qed_rd(p_hwfn, p_ptt,
1666 			     PGLUE_B_REG_INTERNAL_PFID_ENABLE_MASTER);
1667 		if (val == set_val)
1668 			break;
1669 
1670 		usleep_range(50, 60);
1671 	}
1672 
1673 	if (val != set_val) {
1674 		DP_NOTICE(p_hwfn,
1675 			  "PFID_ENABLE_MASTER wasn't changed after a second\n");
1676 		return -EAGAIN;
1677 	}
1678 
1679 	return 0;
1680 }
1681 
1682 static void qed_reset_mb_shadow(struct qed_hwfn *p_hwfn,
1683 				struct qed_ptt *p_main_ptt)
1684 {
1685 	/* Read shadow of current MFW mailbox */
1686 	qed_mcp_read_mb(p_hwfn, p_main_ptt);
1687 	memcpy(p_hwfn->mcp_info->mfw_mb_shadow,
1688 	       p_hwfn->mcp_info->mfw_mb_cur, p_hwfn->mcp_info->mfw_mb_length);
1689 }
1690 
1691 static void
1692 qed_fill_load_req_params(struct qed_load_req_params *p_load_req,
1693 			 struct qed_drv_load_params *p_drv_load)
1694 {
1695 	memset(p_load_req, 0, sizeof(*p_load_req));
1696 
1697 	p_load_req->drv_role = p_drv_load->is_crash_kernel ?
1698 			       QED_DRV_ROLE_KDUMP : QED_DRV_ROLE_OS;
1699 	p_load_req->timeout_val = p_drv_load->mfw_timeout_val;
1700 	p_load_req->avoid_eng_reset = p_drv_load->avoid_eng_reset;
1701 	p_load_req->override_force_load = p_drv_load->override_force_load;
1702 }
1703 
1704 static int qed_vf_start(struct qed_hwfn *p_hwfn,
1705 			struct qed_hw_init_params *p_params)
1706 {
1707 	if (p_params->p_tunn) {
1708 		qed_vf_set_vf_start_tunn_update_param(p_params->p_tunn);
1709 		qed_vf_pf_tunnel_param_update(p_hwfn, p_params->p_tunn);
1710 	}
1711 
1712 	p_hwfn->b_int_enabled = true;
1713 
1714 	return 0;
1715 }
1716 
1717 int qed_hw_init(struct qed_dev *cdev, struct qed_hw_init_params *p_params)
1718 {
1719 	struct qed_load_req_params load_req_params;
1720 	u32 load_code, param, drv_mb_param;
1721 	bool b_default_mtu = true;
1722 	struct qed_hwfn *p_hwfn;
1723 	int rc = 0, mfw_rc, i;
1724 	u16 ether_type;
1725 
1726 	if ((p_params->int_mode == QED_INT_MODE_MSI) && (cdev->num_hwfns > 1)) {
1727 		DP_NOTICE(cdev, "MSI mode is not supported for CMT devices\n");
1728 		return -EINVAL;
1729 	}
1730 
1731 	if (IS_PF(cdev)) {
1732 		rc = qed_init_fw_data(cdev, p_params->bin_fw_data);
1733 		if (rc)
1734 			return rc;
1735 	}
1736 
1737 	for_each_hwfn(cdev, i) {
1738 		struct qed_hwfn *p_hwfn = &cdev->hwfns[i];
1739 
1740 		/* If management didn't provide a default, set one of our own */
1741 		if (!p_hwfn->hw_info.mtu) {
1742 			p_hwfn->hw_info.mtu = 1500;
1743 			b_default_mtu = false;
1744 		}
1745 
1746 		if (IS_VF(cdev)) {
1747 			qed_vf_start(p_hwfn, p_params);
1748 			continue;
1749 		}
1750 
1751 		/* Enable DMAE in PXP */
1752 		rc = qed_change_pci_hwfn(p_hwfn, p_hwfn->p_main_ptt, true);
1753 
1754 		rc = qed_calc_hw_mode(p_hwfn);
1755 		if (rc)
1756 			return rc;
1757 
1758 		if (IS_PF(cdev) && (test_bit(QED_MF_8021Q_TAGGING,
1759 					     &cdev->mf_bits) ||
1760 				    test_bit(QED_MF_8021AD_TAGGING,
1761 					     &cdev->mf_bits))) {
1762 			if (test_bit(QED_MF_8021Q_TAGGING, &cdev->mf_bits))
1763 				ether_type = ETH_P_8021Q;
1764 			else
1765 				ether_type = ETH_P_8021AD;
1766 			STORE_RT_REG(p_hwfn, PRS_REG_TAG_ETHERTYPE_0_RT_OFFSET,
1767 				     ether_type);
1768 			STORE_RT_REG(p_hwfn, NIG_REG_TAG_ETHERTYPE_0_RT_OFFSET,
1769 				     ether_type);
1770 			STORE_RT_REG(p_hwfn, PBF_REG_TAG_ETHERTYPE_0_RT_OFFSET,
1771 				     ether_type);
1772 			STORE_RT_REG(p_hwfn, DORQ_REG_TAG1_ETHERTYPE_RT_OFFSET,
1773 				     ether_type);
1774 		}
1775 
1776 		qed_fill_load_req_params(&load_req_params,
1777 					 p_params->p_drv_load_params);
1778 		rc = qed_mcp_load_req(p_hwfn, p_hwfn->p_main_ptt,
1779 				      &load_req_params);
1780 		if (rc) {
1781 			DP_NOTICE(p_hwfn, "Failed sending a LOAD_REQ command\n");
1782 			return rc;
1783 		}
1784 
1785 		load_code = load_req_params.load_code;
1786 		DP_VERBOSE(p_hwfn, QED_MSG_SP,
1787 			   "Load request was sent. Load code: 0x%x\n",
1788 			   load_code);
1789 
1790 		qed_mcp_set_capabilities(p_hwfn, p_hwfn->p_main_ptt);
1791 
1792 		qed_reset_mb_shadow(p_hwfn, p_hwfn->p_main_ptt);
1793 
1794 		p_hwfn->first_on_engine = (load_code ==
1795 					   FW_MSG_CODE_DRV_LOAD_ENGINE);
1796 
1797 		switch (load_code) {
1798 		case FW_MSG_CODE_DRV_LOAD_ENGINE:
1799 			rc = qed_hw_init_common(p_hwfn, p_hwfn->p_main_ptt,
1800 						p_hwfn->hw_info.hw_mode);
1801 			if (rc)
1802 				break;
1803 		/* Fall through */
1804 		case FW_MSG_CODE_DRV_LOAD_PORT:
1805 			rc = qed_hw_init_port(p_hwfn, p_hwfn->p_main_ptt,
1806 					      p_hwfn->hw_info.hw_mode);
1807 			if (rc)
1808 				break;
1809 
1810 		/* Fall through */
1811 		case FW_MSG_CODE_DRV_LOAD_FUNCTION:
1812 			rc = qed_hw_init_pf(p_hwfn, p_hwfn->p_main_ptt,
1813 					    p_params->p_tunn,
1814 					    p_hwfn->hw_info.hw_mode,
1815 					    p_params->b_hw_start,
1816 					    p_params->int_mode,
1817 					    p_params->allow_npar_tx_switch);
1818 			break;
1819 		default:
1820 			DP_NOTICE(p_hwfn,
1821 				  "Unexpected load code [0x%08x]", load_code);
1822 			rc = -EINVAL;
1823 			break;
1824 		}
1825 
1826 		if (rc)
1827 			DP_NOTICE(p_hwfn,
1828 				  "init phase failed for loadcode 0x%x (rc %d)\n",
1829 				   load_code, rc);
1830 
1831 		/* ACK mfw regardless of success or failure of initialization */
1832 		mfw_rc = qed_mcp_cmd(p_hwfn, p_hwfn->p_main_ptt,
1833 				     DRV_MSG_CODE_LOAD_DONE,
1834 				     0, &load_code, &param);
1835 		if (rc)
1836 			return rc;
1837 		if (mfw_rc) {
1838 			DP_NOTICE(p_hwfn, "Failed sending LOAD_DONE command\n");
1839 			return mfw_rc;
1840 		}
1841 
1842 		/* Check if there is a DID mismatch between nvm-cfg/efuse */
1843 		if (param & FW_MB_PARAM_LOAD_DONE_DID_EFUSE_ERROR)
1844 			DP_NOTICE(p_hwfn,
1845 				  "warning: device configuration is not supported on this board type. The device may not function as expected.\n");
1846 
1847 		/* send DCBX attention request command */
1848 		DP_VERBOSE(p_hwfn,
1849 			   QED_MSG_DCB,
1850 			   "sending phony dcbx set command to trigger DCBx attention handling\n");
1851 		mfw_rc = qed_mcp_cmd(p_hwfn, p_hwfn->p_main_ptt,
1852 				     DRV_MSG_CODE_SET_DCBX,
1853 				     1 << DRV_MB_PARAM_DCBX_NOTIFY_SHIFT,
1854 				     &load_code, &param);
1855 		if (mfw_rc) {
1856 			DP_NOTICE(p_hwfn,
1857 				  "Failed to send DCBX attention request\n");
1858 			return mfw_rc;
1859 		}
1860 
1861 		p_hwfn->hw_init_done = true;
1862 	}
1863 
1864 	if (IS_PF(cdev)) {
1865 		p_hwfn = QED_LEADING_HWFN(cdev);
1866 		drv_mb_param = STORM_FW_VERSION;
1867 		rc = qed_mcp_cmd(p_hwfn, p_hwfn->p_main_ptt,
1868 				 DRV_MSG_CODE_OV_UPDATE_STORM_FW_VER,
1869 				 drv_mb_param, &load_code, &param);
1870 		if (rc)
1871 			DP_INFO(p_hwfn, "Failed to update firmware version\n");
1872 
1873 		if (!b_default_mtu) {
1874 			rc = qed_mcp_ov_update_mtu(p_hwfn, p_hwfn->p_main_ptt,
1875 						   p_hwfn->hw_info.mtu);
1876 			if (rc)
1877 				DP_INFO(p_hwfn,
1878 					"Failed to update default mtu\n");
1879 		}
1880 
1881 		rc = qed_mcp_ov_update_driver_state(p_hwfn,
1882 						    p_hwfn->p_main_ptt,
1883 						  QED_OV_DRIVER_STATE_DISABLED);
1884 		if (rc)
1885 			DP_INFO(p_hwfn, "Failed to update driver state\n");
1886 
1887 		rc = qed_mcp_ov_update_eswitch(p_hwfn, p_hwfn->p_main_ptt,
1888 					       QED_OV_ESWITCH_NONE);
1889 		if (rc)
1890 			DP_INFO(p_hwfn, "Failed to update eswitch mode\n");
1891 	}
1892 
1893 	return 0;
1894 }
1895 
1896 #define QED_HW_STOP_RETRY_LIMIT (10)
1897 static void qed_hw_timers_stop(struct qed_dev *cdev,
1898 			       struct qed_hwfn *p_hwfn, struct qed_ptt *p_ptt)
1899 {
1900 	int i;
1901 
1902 	/* close timers */
1903 	qed_wr(p_hwfn, p_ptt, TM_REG_PF_ENABLE_CONN, 0x0);
1904 	qed_wr(p_hwfn, p_ptt, TM_REG_PF_ENABLE_TASK, 0x0);
1905 
1906 	for (i = 0; i < QED_HW_STOP_RETRY_LIMIT; i++) {
1907 		if ((!qed_rd(p_hwfn, p_ptt,
1908 			     TM_REG_PF_SCAN_ACTIVE_CONN)) &&
1909 		    (!qed_rd(p_hwfn, p_ptt, TM_REG_PF_SCAN_ACTIVE_TASK)))
1910 			break;
1911 
1912 		/* Dependent on number of connection/tasks, possibly
1913 		 * 1ms sleep is required between polls
1914 		 */
1915 		usleep_range(1000, 2000);
1916 	}
1917 
1918 	if (i < QED_HW_STOP_RETRY_LIMIT)
1919 		return;
1920 
1921 	DP_NOTICE(p_hwfn,
1922 		  "Timers linear scans are not over [Connection %02x Tasks %02x]\n",
1923 		  (u8)qed_rd(p_hwfn, p_ptt, TM_REG_PF_SCAN_ACTIVE_CONN),
1924 		  (u8)qed_rd(p_hwfn, p_ptt, TM_REG_PF_SCAN_ACTIVE_TASK));
1925 }
1926 
1927 void qed_hw_timers_stop_all(struct qed_dev *cdev)
1928 {
1929 	int j;
1930 
1931 	for_each_hwfn(cdev, j) {
1932 		struct qed_hwfn *p_hwfn = &cdev->hwfns[j];
1933 		struct qed_ptt *p_ptt = p_hwfn->p_main_ptt;
1934 
1935 		qed_hw_timers_stop(cdev, p_hwfn, p_ptt);
1936 	}
1937 }
1938 
1939 int qed_hw_stop(struct qed_dev *cdev)
1940 {
1941 	struct qed_hwfn *p_hwfn;
1942 	struct qed_ptt *p_ptt;
1943 	int rc, rc2 = 0;
1944 	int j;
1945 
1946 	for_each_hwfn(cdev, j) {
1947 		p_hwfn = &cdev->hwfns[j];
1948 		p_ptt = p_hwfn->p_main_ptt;
1949 
1950 		DP_VERBOSE(p_hwfn, NETIF_MSG_IFDOWN, "Stopping hw/fw\n");
1951 
1952 		if (IS_VF(cdev)) {
1953 			qed_vf_pf_int_cleanup(p_hwfn);
1954 			rc = qed_vf_pf_reset(p_hwfn);
1955 			if (rc) {
1956 				DP_NOTICE(p_hwfn,
1957 					  "qed_vf_pf_reset failed. rc = %d.\n",
1958 					  rc);
1959 				rc2 = -EINVAL;
1960 			}
1961 			continue;
1962 		}
1963 
1964 		/* mark the hw as uninitialized... */
1965 		p_hwfn->hw_init_done = false;
1966 
1967 		/* Send unload command to MCP */
1968 		rc = qed_mcp_unload_req(p_hwfn, p_ptt);
1969 		if (rc) {
1970 			DP_NOTICE(p_hwfn,
1971 				  "Failed sending a UNLOAD_REQ command. rc = %d.\n",
1972 				  rc);
1973 			rc2 = -EINVAL;
1974 		}
1975 
1976 		qed_slowpath_irq_sync(p_hwfn);
1977 
1978 		/* After this point no MFW attentions are expected, e.g. prevent
1979 		 * race between pf stop and dcbx pf update.
1980 		 */
1981 		rc = qed_sp_pf_stop(p_hwfn);
1982 		if (rc) {
1983 			DP_NOTICE(p_hwfn,
1984 				  "Failed to close PF against FW [rc = %d]. Continue to stop HW to prevent illegal host access by the device.\n",
1985 				  rc);
1986 			rc2 = -EINVAL;
1987 		}
1988 
1989 		qed_wr(p_hwfn, p_ptt,
1990 		       NIG_REG_RX_LLH_BRB_GATE_DNTFWD_PERPF, 0x1);
1991 
1992 		qed_wr(p_hwfn, p_ptt, PRS_REG_SEARCH_TCP, 0x0);
1993 		qed_wr(p_hwfn, p_ptt, PRS_REG_SEARCH_UDP, 0x0);
1994 		qed_wr(p_hwfn, p_ptt, PRS_REG_SEARCH_FCOE, 0x0);
1995 		qed_wr(p_hwfn, p_ptt, PRS_REG_SEARCH_ROCE, 0x0);
1996 		qed_wr(p_hwfn, p_ptt, PRS_REG_SEARCH_OPENFLOW, 0x0);
1997 
1998 		qed_hw_timers_stop(cdev, p_hwfn, p_ptt);
1999 
2000 		/* Disable Attention Generation */
2001 		qed_int_igu_disable_int(p_hwfn, p_ptt);
2002 
2003 		qed_wr(p_hwfn, p_ptt, IGU_REG_LEADING_EDGE_LATCH, 0);
2004 		qed_wr(p_hwfn, p_ptt, IGU_REG_TRAILING_EDGE_LATCH, 0);
2005 
2006 		qed_int_igu_init_pure_rt(p_hwfn, p_ptt, false, true);
2007 
2008 		/* Need to wait 1ms to guarantee SBs are cleared */
2009 		usleep_range(1000, 2000);
2010 
2011 		/* Disable PF in HW blocks */
2012 		qed_wr(p_hwfn, p_ptt, DORQ_REG_PF_DB_ENABLE, 0);
2013 		qed_wr(p_hwfn, p_ptt, QM_REG_PF_EN, 0);
2014 
2015 		qed_mcp_unload_done(p_hwfn, p_ptt);
2016 		if (rc) {
2017 			DP_NOTICE(p_hwfn,
2018 				  "Failed sending a UNLOAD_DONE command. rc = %d.\n",
2019 				  rc);
2020 			rc2 = -EINVAL;
2021 		}
2022 	}
2023 
2024 	if (IS_PF(cdev)) {
2025 		p_hwfn = QED_LEADING_HWFN(cdev);
2026 		p_ptt = QED_LEADING_HWFN(cdev)->p_main_ptt;
2027 
2028 		/* Disable DMAE in PXP - in CMT, this should only be done for
2029 		 * first hw-function, and only after all transactions have
2030 		 * stopped for all active hw-functions.
2031 		 */
2032 		rc = qed_change_pci_hwfn(p_hwfn, p_ptt, false);
2033 		if (rc) {
2034 			DP_NOTICE(p_hwfn,
2035 				  "qed_change_pci_hwfn failed. rc = %d.\n", rc);
2036 			rc2 = -EINVAL;
2037 		}
2038 	}
2039 
2040 	return rc2;
2041 }
2042 
2043 int qed_hw_stop_fastpath(struct qed_dev *cdev)
2044 {
2045 	int j;
2046 
2047 	for_each_hwfn(cdev, j) {
2048 		struct qed_hwfn *p_hwfn = &cdev->hwfns[j];
2049 		struct qed_ptt *p_ptt;
2050 
2051 		if (IS_VF(cdev)) {
2052 			qed_vf_pf_int_cleanup(p_hwfn);
2053 			continue;
2054 		}
2055 		p_ptt = qed_ptt_acquire(p_hwfn);
2056 		if (!p_ptt)
2057 			return -EAGAIN;
2058 
2059 		DP_VERBOSE(p_hwfn,
2060 			   NETIF_MSG_IFDOWN, "Shutting down the fastpath\n");
2061 
2062 		qed_wr(p_hwfn, p_ptt,
2063 		       NIG_REG_RX_LLH_BRB_GATE_DNTFWD_PERPF, 0x1);
2064 
2065 		qed_wr(p_hwfn, p_ptt, PRS_REG_SEARCH_TCP, 0x0);
2066 		qed_wr(p_hwfn, p_ptt, PRS_REG_SEARCH_UDP, 0x0);
2067 		qed_wr(p_hwfn, p_ptt, PRS_REG_SEARCH_FCOE, 0x0);
2068 		qed_wr(p_hwfn, p_ptt, PRS_REG_SEARCH_ROCE, 0x0);
2069 		qed_wr(p_hwfn, p_ptt, PRS_REG_SEARCH_OPENFLOW, 0x0);
2070 
2071 		qed_int_igu_init_pure_rt(p_hwfn, p_ptt, false, false);
2072 
2073 		/* Need to wait 1ms to guarantee SBs are cleared */
2074 		usleep_range(1000, 2000);
2075 		qed_ptt_release(p_hwfn, p_ptt);
2076 	}
2077 
2078 	return 0;
2079 }
2080 
2081 int qed_hw_start_fastpath(struct qed_hwfn *p_hwfn)
2082 {
2083 	struct qed_ptt *p_ptt;
2084 
2085 	if (IS_VF(p_hwfn->cdev))
2086 		return 0;
2087 
2088 	p_ptt = qed_ptt_acquire(p_hwfn);
2089 	if (!p_ptt)
2090 		return -EAGAIN;
2091 
2092 	/* If roce info is allocated it means roce is initialized and should
2093 	 * be enabled in searcher.
2094 	 */
2095 	if (p_hwfn->p_rdma_info &&
2096 	    p_hwfn->b_rdma_enabled_in_prs)
2097 		qed_wr(p_hwfn, p_ptt, p_hwfn->rdma_prs_search_reg, 0x1);
2098 
2099 	/* Re-open incoming traffic */
2100 	qed_wr(p_hwfn, p_ptt, NIG_REG_RX_LLH_BRB_GATE_DNTFWD_PERPF, 0x0);
2101 	qed_ptt_release(p_hwfn, p_ptt);
2102 
2103 	return 0;
2104 }
2105 
2106 /* Free hwfn memory and resources acquired in hw_hwfn_prepare */
2107 static void qed_hw_hwfn_free(struct qed_hwfn *p_hwfn)
2108 {
2109 	qed_ptt_pool_free(p_hwfn);
2110 	kfree(p_hwfn->hw_info.p_igu_info);
2111 	p_hwfn->hw_info.p_igu_info = NULL;
2112 }
2113 
2114 /* Setup bar access */
2115 static void qed_hw_hwfn_prepare(struct qed_hwfn *p_hwfn)
2116 {
2117 	/* clear indirect access */
2118 	if (QED_IS_AH(p_hwfn->cdev)) {
2119 		qed_wr(p_hwfn, p_hwfn->p_main_ptt,
2120 		       PGLUE_B_REG_PGL_ADDR_E8_F0_K2, 0);
2121 		qed_wr(p_hwfn, p_hwfn->p_main_ptt,
2122 		       PGLUE_B_REG_PGL_ADDR_EC_F0_K2, 0);
2123 		qed_wr(p_hwfn, p_hwfn->p_main_ptt,
2124 		       PGLUE_B_REG_PGL_ADDR_F0_F0_K2, 0);
2125 		qed_wr(p_hwfn, p_hwfn->p_main_ptt,
2126 		       PGLUE_B_REG_PGL_ADDR_F4_F0_K2, 0);
2127 	} else {
2128 		qed_wr(p_hwfn, p_hwfn->p_main_ptt,
2129 		       PGLUE_B_REG_PGL_ADDR_88_F0_BB, 0);
2130 		qed_wr(p_hwfn, p_hwfn->p_main_ptt,
2131 		       PGLUE_B_REG_PGL_ADDR_8C_F0_BB, 0);
2132 		qed_wr(p_hwfn, p_hwfn->p_main_ptt,
2133 		       PGLUE_B_REG_PGL_ADDR_90_F0_BB, 0);
2134 		qed_wr(p_hwfn, p_hwfn->p_main_ptt,
2135 		       PGLUE_B_REG_PGL_ADDR_94_F0_BB, 0);
2136 	}
2137 
2138 	/* Clean Previous errors if such exist */
2139 	qed_wr(p_hwfn, p_hwfn->p_main_ptt,
2140 	       PGLUE_B_REG_WAS_ERROR_PF_31_0_CLR, 1 << p_hwfn->abs_pf_id);
2141 
2142 	/* enable internal target-read */
2143 	qed_wr(p_hwfn, p_hwfn->p_main_ptt,
2144 	       PGLUE_B_REG_INTERNAL_PFID_ENABLE_TARGET_READ, 1);
2145 }
2146 
2147 static void get_function_id(struct qed_hwfn *p_hwfn)
2148 {
2149 	/* ME Register */
2150 	p_hwfn->hw_info.opaque_fid = (u16) REG_RD(p_hwfn,
2151 						  PXP_PF_ME_OPAQUE_ADDR);
2152 
2153 	p_hwfn->hw_info.concrete_fid = REG_RD(p_hwfn, PXP_PF_ME_CONCRETE_ADDR);
2154 
2155 	p_hwfn->abs_pf_id = (p_hwfn->hw_info.concrete_fid >> 16) & 0xf;
2156 	p_hwfn->rel_pf_id = GET_FIELD(p_hwfn->hw_info.concrete_fid,
2157 				      PXP_CONCRETE_FID_PFID);
2158 	p_hwfn->port_id = GET_FIELD(p_hwfn->hw_info.concrete_fid,
2159 				    PXP_CONCRETE_FID_PORT);
2160 
2161 	DP_VERBOSE(p_hwfn, NETIF_MSG_PROBE,
2162 		   "Read ME register: Concrete 0x%08x Opaque 0x%04x\n",
2163 		   p_hwfn->hw_info.concrete_fid, p_hwfn->hw_info.opaque_fid);
2164 }
2165 
2166 static void qed_hw_set_feat(struct qed_hwfn *p_hwfn)
2167 {
2168 	u32 *feat_num = p_hwfn->hw_info.feat_num;
2169 	struct qed_sb_cnt_info sb_cnt;
2170 	u32 non_l2_sbs = 0;
2171 
2172 	memset(&sb_cnt, 0, sizeof(sb_cnt));
2173 	qed_int_get_num_sbs(p_hwfn, &sb_cnt);
2174 
2175 	if (IS_ENABLED(CONFIG_QED_RDMA) &&
2176 	    QED_IS_RDMA_PERSONALITY(p_hwfn)) {
2177 		/* Roce CNQ each requires: 1 status block + 1 CNQ. We divide
2178 		 * the status blocks equally between L2 / RoCE but with
2179 		 * consideration as to how many l2 queues / cnqs we have.
2180 		 */
2181 		feat_num[QED_RDMA_CNQ] =
2182 			min_t(u32, sb_cnt.cnt / 2,
2183 			      RESC_NUM(p_hwfn, QED_RDMA_CNQ_RAM));
2184 
2185 		non_l2_sbs = feat_num[QED_RDMA_CNQ];
2186 	}
2187 	if (QED_IS_L2_PERSONALITY(p_hwfn)) {
2188 		/* Start by allocating VF queues, then PF's */
2189 		feat_num[QED_VF_L2_QUE] = min_t(u32,
2190 						RESC_NUM(p_hwfn, QED_L2_QUEUE),
2191 						sb_cnt.iov_cnt);
2192 		feat_num[QED_PF_L2_QUE] = min_t(u32,
2193 						sb_cnt.cnt - non_l2_sbs,
2194 						RESC_NUM(p_hwfn,
2195 							 QED_L2_QUEUE) -
2196 						FEAT_NUM(p_hwfn,
2197 							 QED_VF_L2_QUE));
2198 	}
2199 
2200 	if (QED_IS_FCOE_PERSONALITY(p_hwfn))
2201 		feat_num[QED_FCOE_CQ] =  min_t(u32, sb_cnt.cnt,
2202 					       RESC_NUM(p_hwfn,
2203 							QED_CMDQS_CQS));
2204 
2205 	if (QED_IS_ISCSI_PERSONALITY(p_hwfn))
2206 		feat_num[QED_ISCSI_CQ] = min_t(u32, sb_cnt.cnt,
2207 					       RESC_NUM(p_hwfn,
2208 							QED_CMDQS_CQS));
2209 	DP_VERBOSE(p_hwfn,
2210 		   NETIF_MSG_PROBE,
2211 		   "#PF_L2_QUEUES=%d VF_L2_QUEUES=%d #ROCE_CNQ=%d FCOE_CQ=%d ISCSI_CQ=%d #SBS=%d\n",
2212 		   (int)FEAT_NUM(p_hwfn, QED_PF_L2_QUE),
2213 		   (int)FEAT_NUM(p_hwfn, QED_VF_L2_QUE),
2214 		   (int)FEAT_NUM(p_hwfn, QED_RDMA_CNQ),
2215 		   (int)FEAT_NUM(p_hwfn, QED_FCOE_CQ),
2216 		   (int)FEAT_NUM(p_hwfn, QED_ISCSI_CQ),
2217 		   (int)sb_cnt.cnt);
2218 }
2219 
2220 const char *qed_hw_get_resc_name(enum qed_resources res_id)
2221 {
2222 	switch (res_id) {
2223 	case QED_L2_QUEUE:
2224 		return "L2_QUEUE";
2225 	case QED_VPORT:
2226 		return "VPORT";
2227 	case QED_RSS_ENG:
2228 		return "RSS_ENG";
2229 	case QED_PQ:
2230 		return "PQ";
2231 	case QED_RL:
2232 		return "RL";
2233 	case QED_MAC:
2234 		return "MAC";
2235 	case QED_VLAN:
2236 		return "VLAN";
2237 	case QED_RDMA_CNQ_RAM:
2238 		return "RDMA_CNQ_RAM";
2239 	case QED_ILT:
2240 		return "ILT";
2241 	case QED_LL2_QUEUE:
2242 		return "LL2_QUEUE";
2243 	case QED_CMDQS_CQS:
2244 		return "CMDQS_CQS";
2245 	case QED_RDMA_STATS_QUEUE:
2246 		return "RDMA_STATS_QUEUE";
2247 	case QED_BDQ:
2248 		return "BDQ";
2249 	case QED_SB:
2250 		return "SB";
2251 	default:
2252 		return "UNKNOWN_RESOURCE";
2253 	}
2254 }
2255 
2256 static int
2257 __qed_hw_set_soft_resc_size(struct qed_hwfn *p_hwfn,
2258 			    struct qed_ptt *p_ptt,
2259 			    enum qed_resources res_id,
2260 			    u32 resc_max_val, u32 *p_mcp_resp)
2261 {
2262 	int rc;
2263 
2264 	rc = qed_mcp_set_resc_max_val(p_hwfn, p_ptt, res_id,
2265 				      resc_max_val, p_mcp_resp);
2266 	if (rc) {
2267 		DP_NOTICE(p_hwfn,
2268 			  "MFW response failure for a max value setting of resource %d [%s]\n",
2269 			  res_id, qed_hw_get_resc_name(res_id));
2270 		return rc;
2271 	}
2272 
2273 	if (*p_mcp_resp != FW_MSG_CODE_RESOURCE_ALLOC_OK)
2274 		DP_INFO(p_hwfn,
2275 			"Failed to set the max value of resource %d [%s]. mcp_resp = 0x%08x.\n",
2276 			res_id, qed_hw_get_resc_name(res_id), *p_mcp_resp);
2277 
2278 	return 0;
2279 }
2280 
2281 static int
2282 qed_hw_set_soft_resc_size(struct qed_hwfn *p_hwfn, struct qed_ptt *p_ptt)
2283 {
2284 	bool b_ah = QED_IS_AH(p_hwfn->cdev);
2285 	u32 resc_max_val, mcp_resp;
2286 	u8 res_id;
2287 	int rc;
2288 
2289 	for (res_id = 0; res_id < QED_MAX_RESC; res_id++) {
2290 		switch (res_id) {
2291 		case QED_LL2_QUEUE:
2292 			resc_max_val = MAX_NUM_LL2_RX_QUEUES;
2293 			break;
2294 		case QED_RDMA_CNQ_RAM:
2295 			/* No need for a case for QED_CMDQS_CQS since
2296 			 * CNQ/CMDQS are the same resource.
2297 			 */
2298 			resc_max_val = NUM_OF_GLOBAL_QUEUES;
2299 			break;
2300 		case QED_RDMA_STATS_QUEUE:
2301 			resc_max_val = b_ah ? RDMA_NUM_STATISTIC_COUNTERS_K2
2302 			    : RDMA_NUM_STATISTIC_COUNTERS_BB;
2303 			break;
2304 		case QED_BDQ:
2305 			resc_max_val = BDQ_NUM_RESOURCES;
2306 			break;
2307 		default:
2308 			continue;
2309 		}
2310 
2311 		rc = __qed_hw_set_soft_resc_size(p_hwfn, p_ptt, res_id,
2312 						 resc_max_val, &mcp_resp);
2313 		if (rc)
2314 			return rc;
2315 
2316 		/* There's no point to continue to the next resource if the
2317 		 * command is not supported by the MFW.
2318 		 * We do continue if the command is supported but the resource
2319 		 * is unknown to the MFW. Such a resource will be later
2320 		 * configured with the default allocation values.
2321 		 */
2322 		if (mcp_resp == FW_MSG_CODE_UNSUPPORTED)
2323 			return -EINVAL;
2324 	}
2325 
2326 	return 0;
2327 }
2328 
2329 static
2330 int qed_hw_get_dflt_resc(struct qed_hwfn *p_hwfn,
2331 			 enum qed_resources res_id,
2332 			 u32 *p_resc_num, u32 *p_resc_start)
2333 {
2334 	u8 num_funcs = p_hwfn->num_funcs_on_engine;
2335 	bool b_ah = QED_IS_AH(p_hwfn->cdev);
2336 
2337 	switch (res_id) {
2338 	case QED_L2_QUEUE:
2339 		*p_resc_num = (b_ah ? MAX_NUM_L2_QUEUES_K2 :
2340 			       MAX_NUM_L2_QUEUES_BB) / num_funcs;
2341 		break;
2342 	case QED_VPORT:
2343 		*p_resc_num = (b_ah ? MAX_NUM_VPORTS_K2 :
2344 			       MAX_NUM_VPORTS_BB) / num_funcs;
2345 		break;
2346 	case QED_RSS_ENG:
2347 		*p_resc_num = (b_ah ? ETH_RSS_ENGINE_NUM_K2 :
2348 			       ETH_RSS_ENGINE_NUM_BB) / num_funcs;
2349 		break;
2350 	case QED_PQ:
2351 		*p_resc_num = (b_ah ? MAX_QM_TX_QUEUES_K2 :
2352 			       MAX_QM_TX_QUEUES_BB) / num_funcs;
2353 		*p_resc_num &= ~0x7;	/* The granularity of the PQs is 8 */
2354 		break;
2355 	case QED_RL:
2356 		*p_resc_num = MAX_QM_GLOBAL_RLS / num_funcs;
2357 		break;
2358 	case QED_MAC:
2359 	case QED_VLAN:
2360 		/* Each VFC resource can accommodate both a MAC and a VLAN */
2361 		*p_resc_num = ETH_NUM_MAC_FILTERS / num_funcs;
2362 		break;
2363 	case QED_ILT:
2364 		*p_resc_num = (b_ah ? PXP_NUM_ILT_RECORDS_K2 :
2365 			       PXP_NUM_ILT_RECORDS_BB) / num_funcs;
2366 		break;
2367 	case QED_LL2_QUEUE:
2368 		*p_resc_num = MAX_NUM_LL2_RX_QUEUES / num_funcs;
2369 		break;
2370 	case QED_RDMA_CNQ_RAM:
2371 	case QED_CMDQS_CQS:
2372 		/* CNQ/CMDQS are the same resource */
2373 		*p_resc_num = NUM_OF_GLOBAL_QUEUES / num_funcs;
2374 		break;
2375 	case QED_RDMA_STATS_QUEUE:
2376 		*p_resc_num = (b_ah ? RDMA_NUM_STATISTIC_COUNTERS_K2 :
2377 			       RDMA_NUM_STATISTIC_COUNTERS_BB) / num_funcs;
2378 		break;
2379 	case QED_BDQ:
2380 		if (p_hwfn->hw_info.personality != QED_PCI_ISCSI &&
2381 		    p_hwfn->hw_info.personality != QED_PCI_FCOE)
2382 			*p_resc_num = 0;
2383 		else
2384 			*p_resc_num = 1;
2385 		break;
2386 	case QED_SB:
2387 		/* Since we want its value to reflect whether MFW supports
2388 		 * the new scheme, have a default of 0.
2389 		 */
2390 		*p_resc_num = 0;
2391 		break;
2392 	default:
2393 		return -EINVAL;
2394 	}
2395 
2396 	switch (res_id) {
2397 	case QED_BDQ:
2398 		if (!*p_resc_num)
2399 			*p_resc_start = 0;
2400 		else if (p_hwfn->cdev->num_ports_in_engine == 4)
2401 			*p_resc_start = p_hwfn->port_id;
2402 		else if (p_hwfn->hw_info.personality == QED_PCI_ISCSI)
2403 			*p_resc_start = p_hwfn->port_id;
2404 		else if (p_hwfn->hw_info.personality == QED_PCI_FCOE)
2405 			*p_resc_start = p_hwfn->port_id + 2;
2406 		break;
2407 	default:
2408 		*p_resc_start = *p_resc_num * p_hwfn->enabled_func_idx;
2409 		break;
2410 	}
2411 
2412 	return 0;
2413 }
2414 
2415 static int __qed_hw_set_resc_info(struct qed_hwfn *p_hwfn,
2416 				  enum qed_resources res_id)
2417 {
2418 	u32 dflt_resc_num = 0, dflt_resc_start = 0;
2419 	u32 mcp_resp, *p_resc_num, *p_resc_start;
2420 	int rc;
2421 
2422 	p_resc_num = &RESC_NUM(p_hwfn, res_id);
2423 	p_resc_start = &RESC_START(p_hwfn, res_id);
2424 
2425 	rc = qed_hw_get_dflt_resc(p_hwfn, res_id, &dflt_resc_num,
2426 				  &dflt_resc_start);
2427 	if (rc) {
2428 		DP_ERR(p_hwfn,
2429 		       "Failed to get default amount for resource %d [%s]\n",
2430 		       res_id, qed_hw_get_resc_name(res_id));
2431 		return rc;
2432 	}
2433 
2434 	rc = qed_mcp_get_resc_info(p_hwfn, p_hwfn->p_main_ptt, res_id,
2435 				   &mcp_resp, p_resc_num, p_resc_start);
2436 	if (rc) {
2437 		DP_NOTICE(p_hwfn,
2438 			  "MFW response failure for an allocation request for resource %d [%s]\n",
2439 			  res_id, qed_hw_get_resc_name(res_id));
2440 		return rc;
2441 	}
2442 
2443 	/* Default driver values are applied in the following cases:
2444 	 * - The resource allocation MB command is not supported by the MFW
2445 	 * - There is an internal error in the MFW while processing the request
2446 	 * - The resource ID is unknown to the MFW
2447 	 */
2448 	if (mcp_resp != FW_MSG_CODE_RESOURCE_ALLOC_OK) {
2449 		DP_INFO(p_hwfn,
2450 			"Failed to receive allocation info for resource %d [%s]. mcp_resp = 0x%x. Applying default values [%d,%d].\n",
2451 			res_id,
2452 			qed_hw_get_resc_name(res_id),
2453 			mcp_resp, dflt_resc_num, dflt_resc_start);
2454 		*p_resc_num = dflt_resc_num;
2455 		*p_resc_start = dflt_resc_start;
2456 		goto out;
2457 	}
2458 
2459 out:
2460 	/* PQs have to divide by 8 [that's the HW granularity].
2461 	 * Reduce number so it would fit.
2462 	 */
2463 	if ((res_id == QED_PQ) && ((*p_resc_num % 8) || (*p_resc_start % 8))) {
2464 		DP_INFO(p_hwfn,
2465 			"PQs need to align by 8; Number %08x --> %08x, Start %08x --> %08x\n",
2466 			*p_resc_num,
2467 			(*p_resc_num) & ~0x7,
2468 			*p_resc_start, (*p_resc_start) & ~0x7);
2469 		*p_resc_num &= ~0x7;
2470 		*p_resc_start &= ~0x7;
2471 	}
2472 
2473 	return 0;
2474 }
2475 
2476 static int qed_hw_set_resc_info(struct qed_hwfn *p_hwfn)
2477 {
2478 	int rc;
2479 	u8 res_id;
2480 
2481 	for (res_id = 0; res_id < QED_MAX_RESC; res_id++) {
2482 		rc = __qed_hw_set_resc_info(p_hwfn, res_id);
2483 		if (rc)
2484 			return rc;
2485 	}
2486 
2487 	return 0;
2488 }
2489 
2490 static int qed_hw_get_resc(struct qed_hwfn *p_hwfn, struct qed_ptt *p_ptt)
2491 {
2492 	struct qed_resc_unlock_params resc_unlock_params;
2493 	struct qed_resc_lock_params resc_lock_params;
2494 	bool b_ah = QED_IS_AH(p_hwfn->cdev);
2495 	u8 res_id;
2496 	int rc;
2497 
2498 	/* Setting the max values of the soft resources and the following
2499 	 * resources allocation queries should be atomic. Since several PFs can
2500 	 * run in parallel - a resource lock is needed.
2501 	 * If either the resource lock or resource set value commands are not
2502 	 * supported - skip the the max values setting, release the lock if
2503 	 * needed, and proceed to the queries. Other failures, including a
2504 	 * failure to acquire the lock, will cause this function to fail.
2505 	 */
2506 	qed_mcp_resc_lock_default_init(&resc_lock_params, &resc_unlock_params,
2507 				       QED_RESC_LOCK_RESC_ALLOC, false);
2508 
2509 	rc = qed_mcp_resc_lock(p_hwfn, p_ptt, &resc_lock_params);
2510 	if (rc && rc != -EINVAL) {
2511 		return rc;
2512 	} else if (rc == -EINVAL) {
2513 		DP_INFO(p_hwfn,
2514 			"Skip the max values setting of the soft resources since the resource lock is not supported by the MFW\n");
2515 	} else if (!rc && !resc_lock_params.b_granted) {
2516 		DP_NOTICE(p_hwfn,
2517 			  "Failed to acquire the resource lock for the resource allocation commands\n");
2518 		return -EBUSY;
2519 	} else {
2520 		rc = qed_hw_set_soft_resc_size(p_hwfn, p_ptt);
2521 		if (rc && rc != -EINVAL) {
2522 			DP_NOTICE(p_hwfn,
2523 				  "Failed to set the max values of the soft resources\n");
2524 			goto unlock_and_exit;
2525 		} else if (rc == -EINVAL) {
2526 			DP_INFO(p_hwfn,
2527 				"Skip the max values setting of the soft resources since it is not supported by the MFW\n");
2528 			rc = qed_mcp_resc_unlock(p_hwfn, p_ptt,
2529 						 &resc_unlock_params);
2530 			if (rc)
2531 				DP_INFO(p_hwfn,
2532 					"Failed to release the resource lock for the resource allocation commands\n");
2533 		}
2534 	}
2535 
2536 	rc = qed_hw_set_resc_info(p_hwfn);
2537 	if (rc)
2538 		goto unlock_and_exit;
2539 
2540 	if (resc_lock_params.b_granted && !resc_unlock_params.b_released) {
2541 		rc = qed_mcp_resc_unlock(p_hwfn, p_ptt, &resc_unlock_params);
2542 		if (rc)
2543 			DP_INFO(p_hwfn,
2544 				"Failed to release the resource lock for the resource allocation commands\n");
2545 	}
2546 
2547 	/* Sanity for ILT */
2548 	if ((b_ah && (RESC_END(p_hwfn, QED_ILT) > PXP_NUM_ILT_RECORDS_K2)) ||
2549 	    (!b_ah && (RESC_END(p_hwfn, QED_ILT) > PXP_NUM_ILT_RECORDS_BB))) {
2550 		DP_NOTICE(p_hwfn, "Can't assign ILT pages [%08x,...,%08x]\n",
2551 			  RESC_START(p_hwfn, QED_ILT),
2552 			  RESC_END(p_hwfn, QED_ILT) - 1);
2553 		return -EINVAL;
2554 	}
2555 
2556 	/* This will also learn the number of SBs from MFW */
2557 	if (qed_int_igu_reset_cam(p_hwfn, p_ptt))
2558 		return -EINVAL;
2559 
2560 	qed_hw_set_feat(p_hwfn);
2561 
2562 	for (res_id = 0; res_id < QED_MAX_RESC; res_id++)
2563 		DP_VERBOSE(p_hwfn, NETIF_MSG_PROBE, "%s = %d start = %d\n",
2564 			   qed_hw_get_resc_name(res_id),
2565 			   RESC_NUM(p_hwfn, res_id),
2566 			   RESC_START(p_hwfn, res_id));
2567 
2568 	return 0;
2569 
2570 unlock_and_exit:
2571 	if (resc_lock_params.b_granted && !resc_unlock_params.b_released)
2572 		qed_mcp_resc_unlock(p_hwfn, p_ptt, &resc_unlock_params);
2573 	return rc;
2574 }
2575 
2576 static int qed_hw_get_nvm_info(struct qed_hwfn *p_hwfn, struct qed_ptt *p_ptt)
2577 {
2578 	u32 port_cfg_addr, link_temp, nvm_cfg_addr, device_capabilities;
2579 	u32 nvm_cfg1_offset, mf_mode, addr, generic_cont0, core_cfg;
2580 	struct qed_mcp_link_capabilities *p_caps;
2581 	struct qed_mcp_link_params *link;
2582 
2583 	/* Read global nvm_cfg address */
2584 	nvm_cfg_addr = qed_rd(p_hwfn, p_ptt, MISC_REG_GEN_PURP_CR0);
2585 
2586 	/* Verify MCP has initialized it */
2587 	if (!nvm_cfg_addr) {
2588 		DP_NOTICE(p_hwfn, "Shared memory not initialized\n");
2589 		return -EINVAL;
2590 	}
2591 
2592 	/* Read nvm_cfg1  (Notice this is just offset, and not offsize (TBD) */
2593 	nvm_cfg1_offset = qed_rd(p_hwfn, p_ptt, nvm_cfg_addr + 4);
2594 
2595 	addr = MCP_REG_SCRATCH + nvm_cfg1_offset +
2596 	       offsetof(struct nvm_cfg1, glob) +
2597 	       offsetof(struct nvm_cfg1_glob, core_cfg);
2598 
2599 	core_cfg = qed_rd(p_hwfn, p_ptt, addr);
2600 
2601 	switch ((core_cfg & NVM_CFG1_GLOB_NETWORK_PORT_MODE_MASK) >>
2602 		NVM_CFG1_GLOB_NETWORK_PORT_MODE_OFFSET) {
2603 	case NVM_CFG1_GLOB_NETWORK_PORT_MODE_BB_2X40G:
2604 		p_hwfn->hw_info.port_mode = QED_PORT_MODE_DE_2X40G;
2605 		break;
2606 	case NVM_CFG1_GLOB_NETWORK_PORT_MODE_2X50G:
2607 		p_hwfn->hw_info.port_mode = QED_PORT_MODE_DE_2X50G;
2608 		break;
2609 	case NVM_CFG1_GLOB_NETWORK_PORT_MODE_BB_1X100G:
2610 		p_hwfn->hw_info.port_mode = QED_PORT_MODE_DE_1X100G;
2611 		break;
2612 	case NVM_CFG1_GLOB_NETWORK_PORT_MODE_4X10G_F:
2613 		p_hwfn->hw_info.port_mode = QED_PORT_MODE_DE_4X10G_F;
2614 		break;
2615 	case NVM_CFG1_GLOB_NETWORK_PORT_MODE_BB_4X10G_E:
2616 		p_hwfn->hw_info.port_mode = QED_PORT_MODE_DE_4X10G_E;
2617 		break;
2618 	case NVM_CFG1_GLOB_NETWORK_PORT_MODE_BB_4X20G:
2619 		p_hwfn->hw_info.port_mode = QED_PORT_MODE_DE_4X20G;
2620 		break;
2621 	case NVM_CFG1_GLOB_NETWORK_PORT_MODE_1X40G:
2622 		p_hwfn->hw_info.port_mode = QED_PORT_MODE_DE_1X40G;
2623 		break;
2624 	case NVM_CFG1_GLOB_NETWORK_PORT_MODE_2X25G:
2625 		p_hwfn->hw_info.port_mode = QED_PORT_MODE_DE_2X25G;
2626 		break;
2627 	case NVM_CFG1_GLOB_NETWORK_PORT_MODE_2X10G:
2628 		p_hwfn->hw_info.port_mode = QED_PORT_MODE_DE_2X10G;
2629 		break;
2630 	case NVM_CFG1_GLOB_NETWORK_PORT_MODE_1X25G:
2631 		p_hwfn->hw_info.port_mode = QED_PORT_MODE_DE_1X25G;
2632 		break;
2633 	case NVM_CFG1_GLOB_NETWORK_PORT_MODE_4X25G:
2634 		p_hwfn->hw_info.port_mode = QED_PORT_MODE_DE_4X25G;
2635 		break;
2636 	default:
2637 		DP_NOTICE(p_hwfn, "Unknown port mode in 0x%08x\n", core_cfg);
2638 		break;
2639 	}
2640 
2641 	/* Read default link configuration */
2642 	link = &p_hwfn->mcp_info->link_input;
2643 	p_caps = &p_hwfn->mcp_info->link_capabilities;
2644 	port_cfg_addr = MCP_REG_SCRATCH + nvm_cfg1_offset +
2645 			offsetof(struct nvm_cfg1, port[MFW_PORT(p_hwfn)]);
2646 	link_temp = qed_rd(p_hwfn, p_ptt,
2647 			   port_cfg_addr +
2648 			   offsetof(struct nvm_cfg1_port, speed_cap_mask));
2649 	link_temp &= NVM_CFG1_PORT_DRV_SPEED_CAPABILITY_MASK_MASK;
2650 	link->speed.advertised_speeds = link_temp;
2651 
2652 	link_temp = link->speed.advertised_speeds;
2653 	p_hwfn->mcp_info->link_capabilities.speed_capabilities = link_temp;
2654 
2655 	link_temp = qed_rd(p_hwfn, p_ptt,
2656 			   port_cfg_addr +
2657 			   offsetof(struct nvm_cfg1_port, link_settings));
2658 	switch ((link_temp & NVM_CFG1_PORT_DRV_LINK_SPEED_MASK) >>
2659 		NVM_CFG1_PORT_DRV_LINK_SPEED_OFFSET) {
2660 	case NVM_CFG1_PORT_DRV_LINK_SPEED_AUTONEG:
2661 		link->speed.autoneg = true;
2662 		break;
2663 	case NVM_CFG1_PORT_DRV_LINK_SPEED_1G:
2664 		link->speed.forced_speed = 1000;
2665 		break;
2666 	case NVM_CFG1_PORT_DRV_LINK_SPEED_10G:
2667 		link->speed.forced_speed = 10000;
2668 		break;
2669 	case NVM_CFG1_PORT_DRV_LINK_SPEED_25G:
2670 		link->speed.forced_speed = 25000;
2671 		break;
2672 	case NVM_CFG1_PORT_DRV_LINK_SPEED_40G:
2673 		link->speed.forced_speed = 40000;
2674 		break;
2675 	case NVM_CFG1_PORT_DRV_LINK_SPEED_50G:
2676 		link->speed.forced_speed = 50000;
2677 		break;
2678 	case NVM_CFG1_PORT_DRV_LINK_SPEED_BB_100G:
2679 		link->speed.forced_speed = 100000;
2680 		break;
2681 	default:
2682 		DP_NOTICE(p_hwfn, "Unknown Speed in 0x%08x\n", link_temp);
2683 	}
2684 
2685 	p_hwfn->mcp_info->link_capabilities.default_speed_autoneg =
2686 		link->speed.autoneg;
2687 
2688 	link_temp &= NVM_CFG1_PORT_DRV_FLOW_CONTROL_MASK;
2689 	link_temp >>= NVM_CFG1_PORT_DRV_FLOW_CONTROL_OFFSET;
2690 	link->pause.autoneg = !!(link_temp &
2691 				 NVM_CFG1_PORT_DRV_FLOW_CONTROL_AUTONEG);
2692 	link->pause.forced_rx = !!(link_temp &
2693 				   NVM_CFG1_PORT_DRV_FLOW_CONTROL_RX);
2694 	link->pause.forced_tx = !!(link_temp &
2695 				   NVM_CFG1_PORT_DRV_FLOW_CONTROL_TX);
2696 	link->loopback_mode = 0;
2697 
2698 	if (p_hwfn->mcp_info->capabilities & FW_MB_PARAM_FEATURE_SUPPORT_EEE) {
2699 		link_temp = qed_rd(p_hwfn, p_ptt, port_cfg_addr +
2700 				   offsetof(struct nvm_cfg1_port, ext_phy));
2701 		link_temp &= NVM_CFG1_PORT_EEE_POWER_SAVING_MODE_MASK;
2702 		link_temp >>= NVM_CFG1_PORT_EEE_POWER_SAVING_MODE_OFFSET;
2703 		p_caps->default_eee = QED_MCP_EEE_ENABLED;
2704 		link->eee.enable = true;
2705 		switch (link_temp) {
2706 		case NVM_CFG1_PORT_EEE_POWER_SAVING_MODE_DISABLED:
2707 			p_caps->default_eee = QED_MCP_EEE_DISABLED;
2708 			link->eee.enable = false;
2709 			break;
2710 		case NVM_CFG1_PORT_EEE_POWER_SAVING_MODE_BALANCED:
2711 			p_caps->eee_lpi_timer = EEE_TX_TIMER_USEC_BALANCED_TIME;
2712 			break;
2713 		case NVM_CFG1_PORT_EEE_POWER_SAVING_MODE_AGGRESSIVE:
2714 			p_caps->eee_lpi_timer =
2715 			    EEE_TX_TIMER_USEC_AGGRESSIVE_TIME;
2716 			break;
2717 		case NVM_CFG1_PORT_EEE_POWER_SAVING_MODE_LOW_LATENCY:
2718 			p_caps->eee_lpi_timer = EEE_TX_TIMER_USEC_LATENCY_TIME;
2719 			break;
2720 		}
2721 
2722 		link->eee.tx_lpi_timer = p_caps->eee_lpi_timer;
2723 		link->eee.tx_lpi_enable = link->eee.enable;
2724 		link->eee.adv_caps = QED_EEE_1G_ADV | QED_EEE_10G_ADV;
2725 	} else {
2726 		p_caps->default_eee = QED_MCP_EEE_UNSUPPORTED;
2727 	}
2728 
2729 	DP_VERBOSE(p_hwfn,
2730 		   NETIF_MSG_LINK,
2731 		   "Read default link: Speed 0x%08x, Adv. Speed 0x%08x, AN: 0x%02x, PAUSE AN: 0x%02x EEE: %02x [%08x usec]\n",
2732 		   link->speed.forced_speed,
2733 		   link->speed.advertised_speeds,
2734 		   link->speed.autoneg,
2735 		   link->pause.autoneg,
2736 		   p_caps->default_eee, p_caps->eee_lpi_timer);
2737 
2738 	if (IS_LEAD_HWFN(p_hwfn)) {
2739 		struct qed_dev *cdev = p_hwfn->cdev;
2740 
2741 		/* Read Multi-function information from shmem */
2742 		addr = MCP_REG_SCRATCH + nvm_cfg1_offset +
2743 		       offsetof(struct nvm_cfg1, glob) +
2744 		       offsetof(struct nvm_cfg1_glob, generic_cont0);
2745 
2746 		generic_cont0 = qed_rd(p_hwfn, p_ptt, addr);
2747 
2748 		mf_mode = (generic_cont0 & NVM_CFG1_GLOB_MF_MODE_MASK) >>
2749 			  NVM_CFG1_GLOB_MF_MODE_OFFSET;
2750 
2751 		switch (mf_mode) {
2752 		case NVM_CFG1_GLOB_MF_MODE_MF_ALLOWED:
2753 			cdev->mf_bits = BIT(QED_MF_OVLAN_CLSS);
2754 			break;
2755 		case NVM_CFG1_GLOB_MF_MODE_UFP:
2756 			cdev->mf_bits = BIT(QED_MF_OVLAN_CLSS) |
2757 					BIT(QED_MF_LLH_PROTO_CLSS) |
2758 					BIT(QED_MF_UFP_SPECIFIC) |
2759 					BIT(QED_MF_8021Q_TAGGING);
2760 			break;
2761 		case NVM_CFG1_GLOB_MF_MODE_BD:
2762 			cdev->mf_bits = BIT(QED_MF_OVLAN_CLSS) |
2763 					BIT(QED_MF_LLH_PROTO_CLSS) |
2764 					BIT(QED_MF_8021AD_TAGGING);
2765 			break;
2766 		case NVM_CFG1_GLOB_MF_MODE_NPAR1_0:
2767 			cdev->mf_bits = BIT(QED_MF_LLH_MAC_CLSS) |
2768 					BIT(QED_MF_LLH_PROTO_CLSS) |
2769 					BIT(QED_MF_LL2_NON_UNICAST) |
2770 					BIT(QED_MF_INTER_PF_SWITCH);
2771 			break;
2772 		case NVM_CFG1_GLOB_MF_MODE_DEFAULT:
2773 			cdev->mf_bits = BIT(QED_MF_LLH_MAC_CLSS) |
2774 					BIT(QED_MF_LLH_PROTO_CLSS) |
2775 					BIT(QED_MF_LL2_NON_UNICAST);
2776 			if (QED_IS_BB(p_hwfn->cdev))
2777 				cdev->mf_bits |= BIT(QED_MF_NEED_DEF_PF);
2778 			break;
2779 		}
2780 
2781 		DP_INFO(p_hwfn, "Multi function mode is 0x%lx\n",
2782 			cdev->mf_bits);
2783 	}
2784 
2785 	DP_INFO(p_hwfn, "Multi function mode is 0x%lx\n",
2786 		p_hwfn->cdev->mf_bits);
2787 
2788 	/* Read device capabilities information from shmem */
2789 	addr = MCP_REG_SCRATCH + nvm_cfg1_offset +
2790 		offsetof(struct nvm_cfg1, glob) +
2791 		offsetof(struct nvm_cfg1_glob, device_capabilities);
2792 
2793 	device_capabilities = qed_rd(p_hwfn, p_ptt, addr);
2794 	if (device_capabilities & NVM_CFG1_GLOB_DEVICE_CAPABILITIES_ETHERNET)
2795 		__set_bit(QED_DEV_CAP_ETH,
2796 			  &p_hwfn->hw_info.device_capabilities);
2797 	if (device_capabilities & NVM_CFG1_GLOB_DEVICE_CAPABILITIES_FCOE)
2798 		__set_bit(QED_DEV_CAP_FCOE,
2799 			  &p_hwfn->hw_info.device_capabilities);
2800 	if (device_capabilities & NVM_CFG1_GLOB_DEVICE_CAPABILITIES_ISCSI)
2801 		__set_bit(QED_DEV_CAP_ISCSI,
2802 			  &p_hwfn->hw_info.device_capabilities);
2803 	if (device_capabilities & NVM_CFG1_GLOB_DEVICE_CAPABILITIES_ROCE)
2804 		__set_bit(QED_DEV_CAP_ROCE,
2805 			  &p_hwfn->hw_info.device_capabilities);
2806 
2807 	return qed_mcp_fill_shmem_func_info(p_hwfn, p_ptt);
2808 }
2809 
2810 static void qed_get_num_funcs(struct qed_hwfn *p_hwfn, struct qed_ptt *p_ptt)
2811 {
2812 	u8 num_funcs, enabled_func_idx = p_hwfn->rel_pf_id;
2813 	u32 reg_function_hide, tmp, eng_mask, low_pfs_mask;
2814 	struct qed_dev *cdev = p_hwfn->cdev;
2815 
2816 	num_funcs = QED_IS_AH(cdev) ? MAX_NUM_PFS_K2 : MAX_NUM_PFS_BB;
2817 
2818 	/* Bit 0 of MISCS_REG_FUNCTION_HIDE indicates whether the bypass values
2819 	 * in the other bits are selected.
2820 	 * Bits 1-15 are for functions 1-15, respectively, and their value is
2821 	 * '0' only for enabled functions (function 0 always exists and
2822 	 * enabled).
2823 	 * In case of CMT, only the "even" functions are enabled, and thus the
2824 	 * number of functions for both hwfns is learnt from the same bits.
2825 	 */
2826 	reg_function_hide = qed_rd(p_hwfn, p_ptt, MISCS_REG_FUNCTION_HIDE);
2827 
2828 	if (reg_function_hide & 0x1) {
2829 		if (QED_IS_BB(cdev)) {
2830 			if (QED_PATH_ID(p_hwfn) && cdev->num_hwfns == 1) {
2831 				num_funcs = 0;
2832 				eng_mask = 0xaaaa;
2833 			} else {
2834 				num_funcs = 1;
2835 				eng_mask = 0x5554;
2836 			}
2837 		} else {
2838 			num_funcs = 1;
2839 			eng_mask = 0xfffe;
2840 		}
2841 
2842 		/* Get the number of the enabled functions on the engine */
2843 		tmp = (reg_function_hide ^ 0xffffffff) & eng_mask;
2844 		while (tmp) {
2845 			if (tmp & 0x1)
2846 				num_funcs++;
2847 			tmp >>= 0x1;
2848 		}
2849 
2850 		/* Get the PF index within the enabled functions */
2851 		low_pfs_mask = (0x1 << p_hwfn->abs_pf_id) - 1;
2852 		tmp = reg_function_hide & eng_mask & low_pfs_mask;
2853 		while (tmp) {
2854 			if (tmp & 0x1)
2855 				enabled_func_idx--;
2856 			tmp >>= 0x1;
2857 		}
2858 	}
2859 
2860 	p_hwfn->num_funcs_on_engine = num_funcs;
2861 	p_hwfn->enabled_func_idx = enabled_func_idx;
2862 
2863 	DP_VERBOSE(p_hwfn,
2864 		   NETIF_MSG_PROBE,
2865 		   "PF [rel_id %d, abs_id %d] occupies index %d within the %d enabled functions on the engine\n",
2866 		   p_hwfn->rel_pf_id,
2867 		   p_hwfn->abs_pf_id,
2868 		   p_hwfn->enabled_func_idx, p_hwfn->num_funcs_on_engine);
2869 }
2870 
2871 static void qed_hw_info_port_num_bb(struct qed_hwfn *p_hwfn,
2872 				    struct qed_ptt *p_ptt)
2873 {
2874 	u32 port_mode;
2875 
2876 	port_mode = qed_rd(p_hwfn, p_ptt, CNIG_REG_NW_PORT_MODE_BB);
2877 
2878 	if (port_mode < 3) {
2879 		p_hwfn->cdev->num_ports_in_engine = 1;
2880 	} else if (port_mode <= 5) {
2881 		p_hwfn->cdev->num_ports_in_engine = 2;
2882 	} else {
2883 		DP_NOTICE(p_hwfn, "PORT MODE: %d not supported\n",
2884 			  p_hwfn->cdev->num_ports_in_engine);
2885 
2886 		/* Default num_ports_in_engine to something */
2887 		p_hwfn->cdev->num_ports_in_engine = 1;
2888 	}
2889 }
2890 
2891 static void qed_hw_info_port_num_ah(struct qed_hwfn *p_hwfn,
2892 				    struct qed_ptt *p_ptt)
2893 {
2894 	u32 port;
2895 	int i;
2896 
2897 	p_hwfn->cdev->num_ports_in_engine = 0;
2898 
2899 	for (i = 0; i < MAX_NUM_PORTS_K2; i++) {
2900 		port = qed_rd(p_hwfn, p_ptt,
2901 			      CNIG_REG_NIG_PORT0_CONF_K2 + (i * 4));
2902 		if (port & 1)
2903 			p_hwfn->cdev->num_ports_in_engine++;
2904 	}
2905 
2906 	if (!p_hwfn->cdev->num_ports_in_engine) {
2907 		DP_NOTICE(p_hwfn, "All NIG ports are inactive\n");
2908 
2909 		/* Default num_ports_in_engine to something */
2910 		p_hwfn->cdev->num_ports_in_engine = 1;
2911 	}
2912 }
2913 
2914 static void qed_hw_info_port_num(struct qed_hwfn *p_hwfn, struct qed_ptt *p_ptt)
2915 {
2916 	if (QED_IS_BB(p_hwfn->cdev))
2917 		qed_hw_info_port_num_bb(p_hwfn, p_ptt);
2918 	else
2919 		qed_hw_info_port_num_ah(p_hwfn, p_ptt);
2920 }
2921 
2922 static void qed_get_eee_caps(struct qed_hwfn *p_hwfn, struct qed_ptt *p_ptt)
2923 {
2924 	struct qed_mcp_link_capabilities *p_caps;
2925 	u32 eee_status;
2926 
2927 	p_caps = &p_hwfn->mcp_info->link_capabilities;
2928 	if (p_caps->default_eee == QED_MCP_EEE_UNSUPPORTED)
2929 		return;
2930 
2931 	p_caps->eee_speed_caps = 0;
2932 	eee_status = qed_rd(p_hwfn, p_ptt, p_hwfn->mcp_info->port_addr +
2933 			    offsetof(struct public_port, eee_status));
2934 	eee_status = (eee_status & EEE_SUPPORTED_SPEED_MASK) >>
2935 			EEE_SUPPORTED_SPEED_OFFSET;
2936 
2937 	if (eee_status & EEE_1G_SUPPORTED)
2938 		p_caps->eee_speed_caps |= QED_EEE_1G_ADV;
2939 	if (eee_status & EEE_10G_ADV)
2940 		p_caps->eee_speed_caps |= QED_EEE_10G_ADV;
2941 }
2942 
2943 static int
2944 qed_get_hw_info(struct qed_hwfn *p_hwfn,
2945 		struct qed_ptt *p_ptt,
2946 		enum qed_pci_personality personality)
2947 {
2948 	int rc;
2949 
2950 	/* Since all information is common, only first hwfns should do this */
2951 	if (IS_LEAD_HWFN(p_hwfn)) {
2952 		rc = qed_iov_hw_info(p_hwfn);
2953 		if (rc)
2954 			return rc;
2955 	}
2956 
2957 	qed_hw_info_port_num(p_hwfn, p_ptt);
2958 
2959 	qed_mcp_get_capabilities(p_hwfn, p_ptt);
2960 
2961 	qed_hw_get_nvm_info(p_hwfn, p_ptt);
2962 
2963 	rc = qed_int_igu_read_cam(p_hwfn, p_ptt);
2964 	if (rc)
2965 		return rc;
2966 
2967 	if (qed_mcp_is_init(p_hwfn))
2968 		ether_addr_copy(p_hwfn->hw_info.hw_mac_addr,
2969 				p_hwfn->mcp_info->func_info.mac);
2970 	else
2971 		eth_random_addr(p_hwfn->hw_info.hw_mac_addr);
2972 
2973 	if (qed_mcp_is_init(p_hwfn)) {
2974 		if (p_hwfn->mcp_info->func_info.ovlan != QED_MCP_VLAN_UNSET)
2975 			p_hwfn->hw_info.ovlan =
2976 				p_hwfn->mcp_info->func_info.ovlan;
2977 
2978 		qed_mcp_cmd_port_init(p_hwfn, p_ptt);
2979 
2980 		qed_get_eee_caps(p_hwfn, p_ptt);
2981 
2982 		qed_mcp_read_ufp_config(p_hwfn, p_ptt);
2983 	}
2984 
2985 	if (qed_mcp_is_init(p_hwfn)) {
2986 		enum qed_pci_personality protocol;
2987 
2988 		protocol = p_hwfn->mcp_info->func_info.protocol;
2989 		p_hwfn->hw_info.personality = protocol;
2990 	}
2991 
2992 	if (QED_IS_ROCE_PERSONALITY(p_hwfn))
2993 		p_hwfn->hw_info.multi_tc_roce_en = 1;
2994 
2995 	p_hwfn->hw_info.num_hw_tc = NUM_PHYS_TCS_4PORT_K2;
2996 	p_hwfn->hw_info.num_active_tc = 1;
2997 
2998 	qed_get_num_funcs(p_hwfn, p_ptt);
2999 
3000 	if (qed_mcp_is_init(p_hwfn))
3001 		p_hwfn->hw_info.mtu = p_hwfn->mcp_info->func_info.mtu;
3002 
3003 	return qed_hw_get_resc(p_hwfn, p_ptt);
3004 }
3005 
3006 static int qed_get_dev_info(struct qed_hwfn *p_hwfn, struct qed_ptt *p_ptt)
3007 {
3008 	struct qed_dev *cdev = p_hwfn->cdev;
3009 	u16 device_id_mask;
3010 	u32 tmp;
3011 
3012 	/* Read Vendor Id / Device Id */
3013 	pci_read_config_word(cdev->pdev, PCI_VENDOR_ID, &cdev->vendor_id);
3014 	pci_read_config_word(cdev->pdev, PCI_DEVICE_ID, &cdev->device_id);
3015 
3016 	/* Determine type */
3017 	device_id_mask = cdev->device_id & QED_DEV_ID_MASK;
3018 	switch (device_id_mask) {
3019 	case QED_DEV_ID_MASK_BB:
3020 		cdev->type = QED_DEV_TYPE_BB;
3021 		break;
3022 	case QED_DEV_ID_MASK_AH:
3023 		cdev->type = QED_DEV_TYPE_AH;
3024 		break;
3025 	default:
3026 		DP_NOTICE(p_hwfn, "Unknown device id 0x%x\n", cdev->device_id);
3027 		return -EBUSY;
3028 	}
3029 
3030 	cdev->chip_num = (u16)qed_rd(p_hwfn, p_ptt, MISCS_REG_CHIP_NUM);
3031 	cdev->chip_rev = (u16)qed_rd(p_hwfn, p_ptt, MISCS_REG_CHIP_REV);
3032 
3033 	MASK_FIELD(CHIP_REV, cdev->chip_rev);
3034 
3035 	/* Learn number of HW-functions */
3036 	tmp = qed_rd(p_hwfn, p_ptt, MISCS_REG_CMT_ENABLED_FOR_PAIR);
3037 
3038 	if (tmp & (1 << p_hwfn->rel_pf_id)) {
3039 		DP_NOTICE(cdev->hwfns, "device in CMT mode\n");
3040 		cdev->num_hwfns = 2;
3041 	} else {
3042 		cdev->num_hwfns = 1;
3043 	}
3044 
3045 	cdev->chip_bond_id = qed_rd(p_hwfn, p_ptt,
3046 				    MISCS_REG_CHIP_TEST_REG) >> 4;
3047 	MASK_FIELD(CHIP_BOND_ID, cdev->chip_bond_id);
3048 	cdev->chip_metal = (u16)qed_rd(p_hwfn, p_ptt, MISCS_REG_CHIP_METAL);
3049 	MASK_FIELD(CHIP_METAL, cdev->chip_metal);
3050 
3051 	DP_INFO(cdev->hwfns,
3052 		"Chip details - %s %c%d, Num: %04x Rev: %04x Bond id: %04x Metal: %04x\n",
3053 		QED_IS_BB(cdev) ? "BB" : "AH",
3054 		'A' + cdev->chip_rev,
3055 		(int)cdev->chip_metal,
3056 		cdev->chip_num, cdev->chip_rev,
3057 		cdev->chip_bond_id, cdev->chip_metal);
3058 
3059 	return 0;
3060 }
3061 
3062 static void qed_nvm_info_free(struct qed_hwfn *p_hwfn)
3063 {
3064 	kfree(p_hwfn->nvm_info.image_att);
3065 	p_hwfn->nvm_info.image_att = NULL;
3066 }
3067 
3068 static int qed_hw_prepare_single(struct qed_hwfn *p_hwfn,
3069 				 void __iomem *p_regview,
3070 				 void __iomem *p_doorbells,
3071 				 enum qed_pci_personality personality)
3072 {
3073 	int rc = 0;
3074 
3075 	/* Split PCI bars evenly between hwfns */
3076 	p_hwfn->regview = p_regview;
3077 	p_hwfn->doorbells = p_doorbells;
3078 
3079 	if (IS_VF(p_hwfn->cdev))
3080 		return qed_vf_hw_prepare(p_hwfn);
3081 
3082 	/* Validate that chip access is feasible */
3083 	if (REG_RD(p_hwfn, PXP_PF_ME_OPAQUE_ADDR) == 0xffffffff) {
3084 		DP_ERR(p_hwfn,
3085 		       "Reading the ME register returns all Fs; Preventing further chip access\n");
3086 		return -EINVAL;
3087 	}
3088 
3089 	get_function_id(p_hwfn);
3090 
3091 	/* Allocate PTT pool */
3092 	rc = qed_ptt_pool_alloc(p_hwfn);
3093 	if (rc)
3094 		goto err0;
3095 
3096 	/* Allocate the main PTT */
3097 	p_hwfn->p_main_ptt = qed_get_reserved_ptt(p_hwfn, RESERVED_PTT_MAIN);
3098 
3099 	/* First hwfn learns basic information, e.g., number of hwfns */
3100 	if (!p_hwfn->my_id) {
3101 		rc = qed_get_dev_info(p_hwfn, p_hwfn->p_main_ptt);
3102 		if (rc)
3103 			goto err1;
3104 	}
3105 
3106 	qed_hw_hwfn_prepare(p_hwfn);
3107 
3108 	/* Initialize MCP structure */
3109 	rc = qed_mcp_cmd_init(p_hwfn, p_hwfn->p_main_ptt);
3110 	if (rc) {
3111 		DP_NOTICE(p_hwfn, "Failed initializing mcp command\n");
3112 		goto err1;
3113 	}
3114 
3115 	/* Read the device configuration information from the HW and SHMEM */
3116 	rc = qed_get_hw_info(p_hwfn, p_hwfn->p_main_ptt, personality);
3117 	if (rc) {
3118 		DP_NOTICE(p_hwfn, "Failed to get HW information\n");
3119 		goto err2;
3120 	}
3121 
3122 	/* Sending a mailbox to the MFW should be done after qed_get_hw_info()
3123 	 * is called as it sets the ports number in an engine.
3124 	 */
3125 	if (IS_LEAD_HWFN(p_hwfn)) {
3126 		rc = qed_mcp_initiate_pf_flr(p_hwfn, p_hwfn->p_main_ptt);
3127 		if (rc)
3128 			DP_NOTICE(p_hwfn, "Failed to initiate PF FLR\n");
3129 	}
3130 
3131 	/* NVRAM info initialization and population */
3132 	if (IS_LEAD_HWFN(p_hwfn)) {
3133 		rc = qed_mcp_nvm_info_populate(p_hwfn);
3134 		if (rc) {
3135 			DP_NOTICE(p_hwfn,
3136 				  "Failed to populate nvm info shadow\n");
3137 			goto err2;
3138 		}
3139 	}
3140 
3141 	/* Allocate the init RT array and initialize the init-ops engine */
3142 	rc = qed_init_alloc(p_hwfn);
3143 	if (rc)
3144 		goto err3;
3145 
3146 	return rc;
3147 err3:
3148 	if (IS_LEAD_HWFN(p_hwfn))
3149 		qed_nvm_info_free(p_hwfn);
3150 err2:
3151 	if (IS_LEAD_HWFN(p_hwfn))
3152 		qed_iov_free_hw_info(p_hwfn->cdev);
3153 	qed_mcp_free(p_hwfn);
3154 err1:
3155 	qed_hw_hwfn_free(p_hwfn);
3156 err0:
3157 	return rc;
3158 }
3159 
3160 int qed_hw_prepare(struct qed_dev *cdev,
3161 		   int personality)
3162 {
3163 	struct qed_hwfn *p_hwfn = QED_LEADING_HWFN(cdev);
3164 	int rc;
3165 
3166 	/* Store the precompiled init data ptrs */
3167 	if (IS_PF(cdev))
3168 		qed_init_iro_array(cdev);
3169 
3170 	/* Initialize the first hwfn - will learn number of hwfns */
3171 	rc = qed_hw_prepare_single(p_hwfn,
3172 				   cdev->regview,
3173 				   cdev->doorbells, personality);
3174 	if (rc)
3175 		return rc;
3176 
3177 	personality = p_hwfn->hw_info.personality;
3178 
3179 	/* Initialize the rest of the hwfns */
3180 	if (cdev->num_hwfns > 1) {
3181 		void __iomem *p_regview, *p_doorbell;
3182 		u8 __iomem *addr;
3183 
3184 		/* adjust bar offset for second engine */
3185 		addr = cdev->regview +
3186 		       qed_hw_bar_size(p_hwfn, p_hwfn->p_main_ptt,
3187 				       BAR_ID_0) / 2;
3188 		p_regview = addr;
3189 
3190 		addr = cdev->doorbells +
3191 		       qed_hw_bar_size(p_hwfn, p_hwfn->p_main_ptt,
3192 				       BAR_ID_1) / 2;
3193 		p_doorbell = addr;
3194 
3195 		/* prepare second hw function */
3196 		rc = qed_hw_prepare_single(&cdev->hwfns[1], p_regview,
3197 					   p_doorbell, personality);
3198 
3199 		/* in case of error, need to free the previously
3200 		 * initiliazed hwfn 0.
3201 		 */
3202 		if (rc) {
3203 			if (IS_PF(cdev)) {
3204 				qed_init_free(p_hwfn);
3205 				qed_nvm_info_free(p_hwfn);
3206 				qed_mcp_free(p_hwfn);
3207 				qed_hw_hwfn_free(p_hwfn);
3208 			}
3209 		}
3210 	}
3211 
3212 	return rc;
3213 }
3214 
3215 void qed_hw_remove(struct qed_dev *cdev)
3216 {
3217 	struct qed_hwfn *p_hwfn = QED_LEADING_HWFN(cdev);
3218 	int i;
3219 
3220 	if (IS_PF(cdev))
3221 		qed_mcp_ov_update_driver_state(p_hwfn, p_hwfn->p_main_ptt,
3222 					       QED_OV_DRIVER_STATE_NOT_LOADED);
3223 
3224 	for_each_hwfn(cdev, i) {
3225 		struct qed_hwfn *p_hwfn = &cdev->hwfns[i];
3226 
3227 		if (IS_VF(cdev)) {
3228 			qed_vf_pf_release(p_hwfn);
3229 			continue;
3230 		}
3231 
3232 		qed_init_free(p_hwfn);
3233 		qed_hw_hwfn_free(p_hwfn);
3234 		qed_mcp_free(p_hwfn);
3235 	}
3236 
3237 	qed_iov_free_hw_info(cdev);
3238 
3239 	qed_nvm_info_free(p_hwfn);
3240 }
3241 
3242 static void qed_chain_free_next_ptr(struct qed_dev *cdev,
3243 				    struct qed_chain *p_chain)
3244 {
3245 	void *p_virt = p_chain->p_virt_addr, *p_virt_next = NULL;
3246 	dma_addr_t p_phys = p_chain->p_phys_addr, p_phys_next = 0;
3247 	struct qed_chain_next *p_next;
3248 	u32 size, i;
3249 
3250 	if (!p_virt)
3251 		return;
3252 
3253 	size = p_chain->elem_size * p_chain->usable_per_page;
3254 
3255 	for (i = 0; i < p_chain->page_cnt; i++) {
3256 		if (!p_virt)
3257 			break;
3258 
3259 		p_next = (struct qed_chain_next *)((u8 *)p_virt + size);
3260 		p_virt_next = p_next->next_virt;
3261 		p_phys_next = HILO_DMA_REGPAIR(p_next->next_phys);
3262 
3263 		dma_free_coherent(&cdev->pdev->dev,
3264 				  QED_CHAIN_PAGE_SIZE, p_virt, p_phys);
3265 
3266 		p_virt = p_virt_next;
3267 		p_phys = p_phys_next;
3268 	}
3269 }
3270 
3271 static void qed_chain_free_single(struct qed_dev *cdev,
3272 				  struct qed_chain *p_chain)
3273 {
3274 	if (!p_chain->p_virt_addr)
3275 		return;
3276 
3277 	dma_free_coherent(&cdev->pdev->dev,
3278 			  QED_CHAIN_PAGE_SIZE,
3279 			  p_chain->p_virt_addr, p_chain->p_phys_addr);
3280 }
3281 
3282 static void qed_chain_free_pbl(struct qed_dev *cdev, struct qed_chain *p_chain)
3283 {
3284 	void **pp_virt_addr_tbl = p_chain->pbl.pp_virt_addr_tbl;
3285 	u32 page_cnt = p_chain->page_cnt, i, pbl_size;
3286 	u8 *p_pbl_virt = p_chain->pbl_sp.p_virt_table;
3287 
3288 	if (!pp_virt_addr_tbl)
3289 		return;
3290 
3291 	if (!p_pbl_virt)
3292 		goto out;
3293 
3294 	for (i = 0; i < page_cnt; i++) {
3295 		if (!pp_virt_addr_tbl[i])
3296 			break;
3297 
3298 		dma_free_coherent(&cdev->pdev->dev,
3299 				  QED_CHAIN_PAGE_SIZE,
3300 				  pp_virt_addr_tbl[i],
3301 				  *(dma_addr_t *)p_pbl_virt);
3302 
3303 		p_pbl_virt += QED_CHAIN_PBL_ENTRY_SIZE;
3304 	}
3305 
3306 	pbl_size = page_cnt * QED_CHAIN_PBL_ENTRY_SIZE;
3307 
3308 	if (!p_chain->b_external_pbl)
3309 		dma_free_coherent(&cdev->pdev->dev,
3310 				  pbl_size,
3311 				  p_chain->pbl_sp.p_virt_table,
3312 				  p_chain->pbl_sp.p_phys_table);
3313 out:
3314 	vfree(p_chain->pbl.pp_virt_addr_tbl);
3315 	p_chain->pbl.pp_virt_addr_tbl = NULL;
3316 }
3317 
3318 void qed_chain_free(struct qed_dev *cdev, struct qed_chain *p_chain)
3319 {
3320 	switch (p_chain->mode) {
3321 	case QED_CHAIN_MODE_NEXT_PTR:
3322 		qed_chain_free_next_ptr(cdev, p_chain);
3323 		break;
3324 	case QED_CHAIN_MODE_SINGLE:
3325 		qed_chain_free_single(cdev, p_chain);
3326 		break;
3327 	case QED_CHAIN_MODE_PBL:
3328 		qed_chain_free_pbl(cdev, p_chain);
3329 		break;
3330 	}
3331 }
3332 
3333 static int
3334 qed_chain_alloc_sanity_check(struct qed_dev *cdev,
3335 			     enum qed_chain_cnt_type cnt_type,
3336 			     size_t elem_size, u32 page_cnt)
3337 {
3338 	u64 chain_size = ELEMS_PER_PAGE(elem_size) * page_cnt;
3339 
3340 	/* The actual chain size can be larger than the maximal possible value
3341 	 * after rounding up the requested elements number to pages, and after
3342 	 * taking into acount the unusuable elements (next-ptr elements).
3343 	 * The size of a "u16" chain can be (U16_MAX + 1) since the chain
3344 	 * size/capacity fields are of a u32 type.
3345 	 */
3346 	if ((cnt_type == QED_CHAIN_CNT_TYPE_U16 &&
3347 	     chain_size > ((u32)U16_MAX + 1)) ||
3348 	    (cnt_type == QED_CHAIN_CNT_TYPE_U32 && chain_size > U32_MAX)) {
3349 		DP_NOTICE(cdev,
3350 			  "The actual chain size (0x%llx) is larger than the maximal possible value\n",
3351 			  chain_size);
3352 		return -EINVAL;
3353 	}
3354 
3355 	return 0;
3356 }
3357 
3358 static int
3359 qed_chain_alloc_next_ptr(struct qed_dev *cdev, struct qed_chain *p_chain)
3360 {
3361 	void *p_virt = NULL, *p_virt_prev = NULL;
3362 	dma_addr_t p_phys = 0;
3363 	u32 i;
3364 
3365 	for (i = 0; i < p_chain->page_cnt; i++) {
3366 		p_virt = dma_alloc_coherent(&cdev->pdev->dev,
3367 					    QED_CHAIN_PAGE_SIZE,
3368 					    &p_phys, GFP_KERNEL);
3369 		if (!p_virt)
3370 			return -ENOMEM;
3371 
3372 		if (i == 0) {
3373 			qed_chain_init_mem(p_chain, p_virt, p_phys);
3374 			qed_chain_reset(p_chain);
3375 		} else {
3376 			qed_chain_init_next_ptr_elem(p_chain, p_virt_prev,
3377 						     p_virt, p_phys);
3378 		}
3379 
3380 		p_virt_prev = p_virt;
3381 	}
3382 	/* Last page's next element should point to the beginning of the
3383 	 * chain.
3384 	 */
3385 	qed_chain_init_next_ptr_elem(p_chain, p_virt_prev,
3386 				     p_chain->p_virt_addr,
3387 				     p_chain->p_phys_addr);
3388 
3389 	return 0;
3390 }
3391 
3392 static int
3393 qed_chain_alloc_single(struct qed_dev *cdev, struct qed_chain *p_chain)
3394 {
3395 	dma_addr_t p_phys = 0;
3396 	void *p_virt = NULL;
3397 
3398 	p_virt = dma_alloc_coherent(&cdev->pdev->dev,
3399 				    QED_CHAIN_PAGE_SIZE, &p_phys, GFP_KERNEL);
3400 	if (!p_virt)
3401 		return -ENOMEM;
3402 
3403 	qed_chain_init_mem(p_chain, p_virt, p_phys);
3404 	qed_chain_reset(p_chain);
3405 
3406 	return 0;
3407 }
3408 
3409 static int
3410 qed_chain_alloc_pbl(struct qed_dev *cdev,
3411 		    struct qed_chain *p_chain,
3412 		    struct qed_chain_ext_pbl *ext_pbl)
3413 {
3414 	u32 page_cnt = p_chain->page_cnt, size, i;
3415 	dma_addr_t p_phys = 0, p_pbl_phys = 0;
3416 	void **pp_virt_addr_tbl = NULL;
3417 	u8 *p_pbl_virt = NULL;
3418 	void *p_virt = NULL;
3419 
3420 	size = page_cnt * sizeof(*pp_virt_addr_tbl);
3421 	pp_virt_addr_tbl = vzalloc(size);
3422 	if (!pp_virt_addr_tbl)
3423 		return -ENOMEM;
3424 
3425 	/* The allocation of the PBL table is done with its full size, since it
3426 	 * is expected to be successive.
3427 	 * qed_chain_init_pbl_mem() is called even in a case of an allocation
3428 	 * failure, since pp_virt_addr_tbl was previously allocated, and it
3429 	 * should be saved to allow its freeing during the error flow.
3430 	 */
3431 	size = page_cnt * QED_CHAIN_PBL_ENTRY_SIZE;
3432 
3433 	if (!ext_pbl) {
3434 		p_pbl_virt = dma_alloc_coherent(&cdev->pdev->dev,
3435 						size, &p_pbl_phys, GFP_KERNEL);
3436 	} else {
3437 		p_pbl_virt = ext_pbl->p_pbl_virt;
3438 		p_pbl_phys = ext_pbl->p_pbl_phys;
3439 		p_chain->b_external_pbl = true;
3440 	}
3441 
3442 	qed_chain_init_pbl_mem(p_chain, p_pbl_virt, p_pbl_phys,
3443 			       pp_virt_addr_tbl);
3444 	if (!p_pbl_virt)
3445 		return -ENOMEM;
3446 
3447 	for (i = 0; i < page_cnt; i++) {
3448 		p_virt = dma_alloc_coherent(&cdev->pdev->dev,
3449 					    QED_CHAIN_PAGE_SIZE,
3450 					    &p_phys, GFP_KERNEL);
3451 		if (!p_virt)
3452 			return -ENOMEM;
3453 
3454 		if (i == 0) {
3455 			qed_chain_init_mem(p_chain, p_virt, p_phys);
3456 			qed_chain_reset(p_chain);
3457 		}
3458 
3459 		/* Fill the PBL table with the physical address of the page */
3460 		*(dma_addr_t *)p_pbl_virt = p_phys;
3461 		/* Keep the virtual address of the page */
3462 		p_chain->pbl.pp_virt_addr_tbl[i] = p_virt;
3463 
3464 		p_pbl_virt += QED_CHAIN_PBL_ENTRY_SIZE;
3465 	}
3466 
3467 	return 0;
3468 }
3469 
3470 int qed_chain_alloc(struct qed_dev *cdev,
3471 		    enum qed_chain_use_mode intended_use,
3472 		    enum qed_chain_mode mode,
3473 		    enum qed_chain_cnt_type cnt_type,
3474 		    u32 num_elems,
3475 		    size_t elem_size,
3476 		    struct qed_chain *p_chain,
3477 		    struct qed_chain_ext_pbl *ext_pbl)
3478 {
3479 	u32 page_cnt;
3480 	int rc = 0;
3481 
3482 	if (mode == QED_CHAIN_MODE_SINGLE)
3483 		page_cnt = 1;
3484 	else
3485 		page_cnt = QED_CHAIN_PAGE_CNT(num_elems, elem_size, mode);
3486 
3487 	rc = qed_chain_alloc_sanity_check(cdev, cnt_type, elem_size, page_cnt);
3488 	if (rc) {
3489 		DP_NOTICE(cdev,
3490 			  "Cannot allocate a chain with the given arguments:\n");
3491 		DP_NOTICE(cdev,
3492 			  "[use_mode %d, mode %d, cnt_type %d, num_elems %d, elem_size %zu]\n",
3493 			  intended_use, mode, cnt_type, num_elems, elem_size);
3494 		return rc;
3495 	}
3496 
3497 	qed_chain_init_params(p_chain, page_cnt, (u8) elem_size, intended_use,
3498 			      mode, cnt_type);
3499 
3500 	switch (mode) {
3501 	case QED_CHAIN_MODE_NEXT_PTR:
3502 		rc = qed_chain_alloc_next_ptr(cdev, p_chain);
3503 		break;
3504 	case QED_CHAIN_MODE_SINGLE:
3505 		rc = qed_chain_alloc_single(cdev, p_chain);
3506 		break;
3507 	case QED_CHAIN_MODE_PBL:
3508 		rc = qed_chain_alloc_pbl(cdev, p_chain, ext_pbl);
3509 		break;
3510 	}
3511 	if (rc)
3512 		goto nomem;
3513 
3514 	return 0;
3515 
3516 nomem:
3517 	qed_chain_free(cdev, p_chain);
3518 	return rc;
3519 }
3520 
3521 int qed_fw_l2_queue(struct qed_hwfn *p_hwfn, u16 src_id, u16 *dst_id)
3522 {
3523 	if (src_id >= RESC_NUM(p_hwfn, QED_L2_QUEUE)) {
3524 		u16 min, max;
3525 
3526 		min = (u16) RESC_START(p_hwfn, QED_L2_QUEUE);
3527 		max = min + RESC_NUM(p_hwfn, QED_L2_QUEUE);
3528 		DP_NOTICE(p_hwfn,
3529 			  "l2_queue id [%d] is not valid, available indices [%d - %d]\n",
3530 			  src_id, min, max);
3531 
3532 		return -EINVAL;
3533 	}
3534 
3535 	*dst_id = RESC_START(p_hwfn, QED_L2_QUEUE) + src_id;
3536 
3537 	return 0;
3538 }
3539 
3540 int qed_fw_vport(struct qed_hwfn *p_hwfn, u8 src_id, u8 *dst_id)
3541 {
3542 	if (src_id >= RESC_NUM(p_hwfn, QED_VPORT)) {
3543 		u8 min, max;
3544 
3545 		min = (u8)RESC_START(p_hwfn, QED_VPORT);
3546 		max = min + RESC_NUM(p_hwfn, QED_VPORT);
3547 		DP_NOTICE(p_hwfn,
3548 			  "vport id [%d] is not valid, available indices [%d - %d]\n",
3549 			  src_id, min, max);
3550 
3551 		return -EINVAL;
3552 	}
3553 
3554 	*dst_id = RESC_START(p_hwfn, QED_VPORT) + src_id;
3555 
3556 	return 0;
3557 }
3558 
3559 int qed_fw_rss_eng(struct qed_hwfn *p_hwfn, u8 src_id, u8 *dst_id)
3560 {
3561 	if (src_id >= RESC_NUM(p_hwfn, QED_RSS_ENG)) {
3562 		u8 min, max;
3563 
3564 		min = (u8)RESC_START(p_hwfn, QED_RSS_ENG);
3565 		max = min + RESC_NUM(p_hwfn, QED_RSS_ENG);
3566 		DP_NOTICE(p_hwfn,
3567 			  "rss_eng id [%d] is not valid, available indices [%d - %d]\n",
3568 			  src_id, min, max);
3569 
3570 		return -EINVAL;
3571 	}
3572 
3573 	*dst_id = RESC_START(p_hwfn, QED_RSS_ENG) + src_id;
3574 
3575 	return 0;
3576 }
3577 
3578 static void qed_llh_mac_to_filter(u32 *p_high, u32 *p_low,
3579 				  u8 *p_filter)
3580 {
3581 	*p_high = p_filter[1] | (p_filter[0] << 8);
3582 	*p_low = p_filter[5] | (p_filter[4] << 8) |
3583 		 (p_filter[3] << 16) | (p_filter[2] << 24);
3584 }
3585 
3586 int qed_llh_add_mac_filter(struct qed_hwfn *p_hwfn,
3587 			   struct qed_ptt *p_ptt, u8 *p_filter)
3588 {
3589 	u32 high = 0, low = 0, en;
3590 	int i;
3591 
3592 	if (!test_bit(QED_MF_LLH_MAC_CLSS, &p_hwfn->cdev->mf_bits))
3593 		return 0;
3594 
3595 	qed_llh_mac_to_filter(&high, &low, p_filter);
3596 
3597 	/* Find a free entry and utilize it */
3598 	for (i = 0; i < NIG_REG_LLH_FUNC_FILTER_EN_SIZE; i++) {
3599 		en = qed_rd(p_hwfn, p_ptt,
3600 			    NIG_REG_LLH_FUNC_FILTER_EN + i * sizeof(u32));
3601 		if (en)
3602 			continue;
3603 		qed_wr(p_hwfn, p_ptt,
3604 		       NIG_REG_LLH_FUNC_FILTER_VALUE +
3605 		       2 * i * sizeof(u32), low);
3606 		qed_wr(p_hwfn, p_ptt,
3607 		       NIG_REG_LLH_FUNC_FILTER_VALUE +
3608 		       (2 * i + 1) * sizeof(u32), high);
3609 		qed_wr(p_hwfn, p_ptt,
3610 		       NIG_REG_LLH_FUNC_FILTER_MODE + i * sizeof(u32), 0);
3611 		qed_wr(p_hwfn, p_ptt,
3612 		       NIG_REG_LLH_FUNC_FILTER_PROTOCOL_TYPE +
3613 		       i * sizeof(u32), 0);
3614 		qed_wr(p_hwfn, p_ptt,
3615 		       NIG_REG_LLH_FUNC_FILTER_EN + i * sizeof(u32), 1);
3616 		break;
3617 	}
3618 	if (i >= NIG_REG_LLH_FUNC_FILTER_EN_SIZE) {
3619 		DP_NOTICE(p_hwfn,
3620 			  "Failed to find an empty LLH filter to utilize\n");
3621 		return -EINVAL;
3622 	}
3623 
3624 	DP_VERBOSE(p_hwfn, NETIF_MSG_HW,
3625 		   "mac: %pM is added at %d\n",
3626 		   p_filter, i);
3627 
3628 	return 0;
3629 }
3630 
3631 void qed_llh_remove_mac_filter(struct qed_hwfn *p_hwfn,
3632 			       struct qed_ptt *p_ptt, u8 *p_filter)
3633 {
3634 	u32 high = 0, low = 0;
3635 	int i;
3636 
3637 	if (!test_bit(QED_MF_LLH_MAC_CLSS, &p_hwfn->cdev->mf_bits))
3638 		return;
3639 
3640 	qed_llh_mac_to_filter(&high, &low, p_filter);
3641 
3642 	/* Find the entry and clean it */
3643 	for (i = 0; i < NIG_REG_LLH_FUNC_FILTER_EN_SIZE; i++) {
3644 		if (qed_rd(p_hwfn, p_ptt,
3645 			   NIG_REG_LLH_FUNC_FILTER_VALUE +
3646 			   2 * i * sizeof(u32)) != low)
3647 			continue;
3648 		if (qed_rd(p_hwfn, p_ptt,
3649 			   NIG_REG_LLH_FUNC_FILTER_VALUE +
3650 			   (2 * i + 1) * sizeof(u32)) != high)
3651 			continue;
3652 
3653 		qed_wr(p_hwfn, p_ptt,
3654 		       NIG_REG_LLH_FUNC_FILTER_EN + i * sizeof(u32), 0);
3655 		qed_wr(p_hwfn, p_ptt,
3656 		       NIG_REG_LLH_FUNC_FILTER_VALUE + 2 * i * sizeof(u32), 0);
3657 		qed_wr(p_hwfn, p_ptt,
3658 		       NIG_REG_LLH_FUNC_FILTER_VALUE +
3659 		       (2 * i + 1) * sizeof(u32), 0);
3660 
3661 		DP_VERBOSE(p_hwfn, NETIF_MSG_HW,
3662 			   "mac: %pM is removed from %d\n",
3663 			   p_filter, i);
3664 		break;
3665 	}
3666 	if (i >= NIG_REG_LLH_FUNC_FILTER_EN_SIZE)
3667 		DP_NOTICE(p_hwfn, "Tried to remove a non-configured filter\n");
3668 }
3669 
3670 int
3671 qed_llh_add_protocol_filter(struct qed_hwfn *p_hwfn,
3672 			    struct qed_ptt *p_ptt,
3673 			    u16 source_port_or_eth_type,
3674 			    u16 dest_port, enum qed_llh_port_filter_type_t type)
3675 {
3676 	u32 high = 0, low = 0, en;
3677 	int i;
3678 
3679 	if (!test_bit(QED_MF_LLH_PROTO_CLSS, &p_hwfn->cdev->mf_bits))
3680 		return 0;
3681 
3682 	switch (type) {
3683 	case QED_LLH_FILTER_ETHERTYPE:
3684 		high = source_port_or_eth_type;
3685 		break;
3686 	case QED_LLH_FILTER_TCP_SRC_PORT:
3687 	case QED_LLH_FILTER_UDP_SRC_PORT:
3688 		low = source_port_or_eth_type << 16;
3689 		break;
3690 	case QED_LLH_FILTER_TCP_DEST_PORT:
3691 	case QED_LLH_FILTER_UDP_DEST_PORT:
3692 		low = dest_port;
3693 		break;
3694 	case QED_LLH_FILTER_TCP_SRC_AND_DEST_PORT:
3695 	case QED_LLH_FILTER_UDP_SRC_AND_DEST_PORT:
3696 		low = (source_port_or_eth_type << 16) | dest_port;
3697 		break;
3698 	default:
3699 		DP_NOTICE(p_hwfn,
3700 			  "Non valid LLH protocol filter type %d\n", type);
3701 		return -EINVAL;
3702 	}
3703 	/* Find a free entry and utilize it */
3704 	for (i = 0; i < NIG_REG_LLH_FUNC_FILTER_EN_SIZE; i++) {
3705 		en = qed_rd(p_hwfn, p_ptt,
3706 			    NIG_REG_LLH_FUNC_FILTER_EN + i * sizeof(u32));
3707 		if (en)
3708 			continue;
3709 		qed_wr(p_hwfn, p_ptt,
3710 		       NIG_REG_LLH_FUNC_FILTER_VALUE +
3711 		       2 * i * sizeof(u32), low);
3712 		qed_wr(p_hwfn, p_ptt,
3713 		       NIG_REG_LLH_FUNC_FILTER_VALUE +
3714 		       (2 * i + 1) * sizeof(u32), high);
3715 		qed_wr(p_hwfn, p_ptt,
3716 		       NIG_REG_LLH_FUNC_FILTER_MODE + i * sizeof(u32), 1);
3717 		qed_wr(p_hwfn, p_ptt,
3718 		       NIG_REG_LLH_FUNC_FILTER_PROTOCOL_TYPE +
3719 		       i * sizeof(u32), 1 << type);
3720 		qed_wr(p_hwfn, p_ptt,
3721 		       NIG_REG_LLH_FUNC_FILTER_EN + i * sizeof(u32), 1);
3722 		break;
3723 	}
3724 	if (i >= NIG_REG_LLH_FUNC_FILTER_EN_SIZE) {
3725 		DP_NOTICE(p_hwfn,
3726 			  "Failed to find an empty LLH filter to utilize\n");
3727 		return -EINVAL;
3728 	}
3729 	switch (type) {
3730 	case QED_LLH_FILTER_ETHERTYPE:
3731 		DP_VERBOSE(p_hwfn, NETIF_MSG_HW,
3732 			   "ETH type %x is added at %d\n",
3733 			   source_port_or_eth_type, i);
3734 		break;
3735 	case QED_LLH_FILTER_TCP_SRC_PORT:
3736 		DP_VERBOSE(p_hwfn, NETIF_MSG_HW,
3737 			   "TCP src port %x is added at %d\n",
3738 			   source_port_or_eth_type, i);
3739 		break;
3740 	case QED_LLH_FILTER_UDP_SRC_PORT:
3741 		DP_VERBOSE(p_hwfn, NETIF_MSG_HW,
3742 			   "UDP src port %x is added at %d\n",
3743 			   source_port_or_eth_type, i);
3744 		break;
3745 	case QED_LLH_FILTER_TCP_DEST_PORT:
3746 		DP_VERBOSE(p_hwfn, NETIF_MSG_HW,
3747 			   "TCP dst port %x is added at %d\n", dest_port, i);
3748 		break;
3749 	case QED_LLH_FILTER_UDP_DEST_PORT:
3750 		DP_VERBOSE(p_hwfn, NETIF_MSG_HW,
3751 			   "UDP dst port %x is added at %d\n", dest_port, i);
3752 		break;
3753 	case QED_LLH_FILTER_TCP_SRC_AND_DEST_PORT:
3754 		DP_VERBOSE(p_hwfn, NETIF_MSG_HW,
3755 			   "TCP src/dst ports %x/%x are added at %d\n",
3756 			   source_port_or_eth_type, dest_port, i);
3757 		break;
3758 	case QED_LLH_FILTER_UDP_SRC_AND_DEST_PORT:
3759 		DP_VERBOSE(p_hwfn, NETIF_MSG_HW,
3760 			   "UDP src/dst ports %x/%x are added at %d\n",
3761 			   source_port_or_eth_type, dest_port, i);
3762 		break;
3763 	}
3764 	return 0;
3765 }
3766 
3767 void
3768 qed_llh_remove_protocol_filter(struct qed_hwfn *p_hwfn,
3769 			       struct qed_ptt *p_ptt,
3770 			       u16 source_port_or_eth_type,
3771 			       u16 dest_port,
3772 			       enum qed_llh_port_filter_type_t type)
3773 {
3774 	u32 high = 0, low = 0;
3775 	int i;
3776 
3777 	if (!test_bit(QED_MF_LLH_PROTO_CLSS, &p_hwfn->cdev->mf_bits))
3778 		return;
3779 
3780 	switch (type) {
3781 	case QED_LLH_FILTER_ETHERTYPE:
3782 		high = source_port_or_eth_type;
3783 		break;
3784 	case QED_LLH_FILTER_TCP_SRC_PORT:
3785 	case QED_LLH_FILTER_UDP_SRC_PORT:
3786 		low = source_port_or_eth_type << 16;
3787 		break;
3788 	case QED_LLH_FILTER_TCP_DEST_PORT:
3789 	case QED_LLH_FILTER_UDP_DEST_PORT:
3790 		low = dest_port;
3791 		break;
3792 	case QED_LLH_FILTER_TCP_SRC_AND_DEST_PORT:
3793 	case QED_LLH_FILTER_UDP_SRC_AND_DEST_PORT:
3794 		low = (source_port_or_eth_type << 16) | dest_port;
3795 		break;
3796 	default:
3797 		DP_NOTICE(p_hwfn,
3798 			  "Non valid LLH protocol filter type %d\n", type);
3799 		return;
3800 	}
3801 
3802 	for (i = 0; i < NIG_REG_LLH_FUNC_FILTER_EN_SIZE; i++) {
3803 		if (!qed_rd(p_hwfn, p_ptt,
3804 			    NIG_REG_LLH_FUNC_FILTER_EN + i * sizeof(u32)))
3805 			continue;
3806 		if (!qed_rd(p_hwfn, p_ptt,
3807 			    NIG_REG_LLH_FUNC_FILTER_MODE + i * sizeof(u32)))
3808 			continue;
3809 		if (!(qed_rd(p_hwfn, p_ptt,
3810 			     NIG_REG_LLH_FUNC_FILTER_PROTOCOL_TYPE +
3811 			     i * sizeof(u32)) & BIT(type)))
3812 			continue;
3813 		if (qed_rd(p_hwfn, p_ptt,
3814 			   NIG_REG_LLH_FUNC_FILTER_VALUE +
3815 			   2 * i * sizeof(u32)) != low)
3816 			continue;
3817 		if (qed_rd(p_hwfn, p_ptt,
3818 			   NIG_REG_LLH_FUNC_FILTER_VALUE +
3819 			   (2 * i + 1) * sizeof(u32)) != high)
3820 			continue;
3821 
3822 		qed_wr(p_hwfn, p_ptt,
3823 		       NIG_REG_LLH_FUNC_FILTER_EN + i * sizeof(u32), 0);
3824 		qed_wr(p_hwfn, p_ptt,
3825 		       NIG_REG_LLH_FUNC_FILTER_MODE + i * sizeof(u32), 0);
3826 		qed_wr(p_hwfn, p_ptt,
3827 		       NIG_REG_LLH_FUNC_FILTER_PROTOCOL_TYPE +
3828 		       i * sizeof(u32), 0);
3829 		qed_wr(p_hwfn, p_ptt,
3830 		       NIG_REG_LLH_FUNC_FILTER_VALUE + 2 * i * sizeof(u32), 0);
3831 		qed_wr(p_hwfn, p_ptt,
3832 		       NIG_REG_LLH_FUNC_FILTER_VALUE +
3833 		       (2 * i + 1) * sizeof(u32), 0);
3834 		break;
3835 	}
3836 
3837 	if (i >= NIG_REG_LLH_FUNC_FILTER_EN_SIZE)
3838 		DP_NOTICE(p_hwfn, "Tried to remove a non-configured filter\n");
3839 }
3840 
3841 static int qed_set_coalesce(struct qed_hwfn *p_hwfn, struct qed_ptt *p_ptt,
3842 			    u32 hw_addr, void *p_eth_qzone,
3843 			    size_t eth_qzone_size, u8 timeset)
3844 {
3845 	struct coalescing_timeset *p_coal_timeset;
3846 
3847 	if (p_hwfn->cdev->int_coalescing_mode != QED_COAL_MODE_ENABLE) {
3848 		DP_NOTICE(p_hwfn, "Coalescing configuration not enabled\n");
3849 		return -EINVAL;
3850 	}
3851 
3852 	p_coal_timeset = p_eth_qzone;
3853 	memset(p_eth_qzone, 0, eth_qzone_size);
3854 	SET_FIELD(p_coal_timeset->value, COALESCING_TIMESET_TIMESET, timeset);
3855 	SET_FIELD(p_coal_timeset->value, COALESCING_TIMESET_VALID, 1);
3856 	qed_memcpy_to(p_hwfn, p_ptt, hw_addr, p_eth_qzone, eth_qzone_size);
3857 
3858 	return 0;
3859 }
3860 
3861 int qed_set_queue_coalesce(u16 rx_coal, u16 tx_coal, void *p_handle)
3862 {
3863 	struct qed_queue_cid *p_cid = p_handle;
3864 	struct qed_hwfn *p_hwfn;
3865 	struct qed_ptt *p_ptt;
3866 	int rc = 0;
3867 
3868 	p_hwfn = p_cid->p_owner;
3869 
3870 	if (IS_VF(p_hwfn->cdev))
3871 		return qed_vf_pf_set_coalesce(p_hwfn, rx_coal, tx_coal, p_cid);
3872 
3873 	p_ptt = qed_ptt_acquire(p_hwfn);
3874 	if (!p_ptt)
3875 		return -EAGAIN;
3876 
3877 	if (rx_coal) {
3878 		rc = qed_set_rxq_coalesce(p_hwfn, p_ptt, rx_coal, p_cid);
3879 		if (rc)
3880 			goto out;
3881 		p_hwfn->cdev->rx_coalesce_usecs = rx_coal;
3882 	}
3883 
3884 	if (tx_coal) {
3885 		rc = qed_set_txq_coalesce(p_hwfn, p_ptt, tx_coal, p_cid);
3886 		if (rc)
3887 			goto out;
3888 		p_hwfn->cdev->tx_coalesce_usecs = tx_coal;
3889 	}
3890 out:
3891 	qed_ptt_release(p_hwfn, p_ptt);
3892 	return rc;
3893 }
3894 
3895 int qed_set_rxq_coalesce(struct qed_hwfn *p_hwfn,
3896 			 struct qed_ptt *p_ptt,
3897 			 u16 coalesce, struct qed_queue_cid *p_cid)
3898 {
3899 	struct ustorm_eth_queue_zone eth_qzone;
3900 	u8 timeset, timer_res;
3901 	u32 address;
3902 	int rc;
3903 
3904 	/* Coalesce = (timeset << timer-resolution), timeset is 7bit wide */
3905 	if (coalesce <= 0x7F) {
3906 		timer_res = 0;
3907 	} else if (coalesce <= 0xFF) {
3908 		timer_res = 1;
3909 	} else if (coalesce <= 0x1FF) {
3910 		timer_res = 2;
3911 	} else {
3912 		DP_ERR(p_hwfn, "Invalid coalesce value - %d\n", coalesce);
3913 		return -EINVAL;
3914 	}
3915 	timeset = (u8)(coalesce >> timer_res);
3916 
3917 	rc = qed_int_set_timer_res(p_hwfn, p_ptt, timer_res,
3918 				   p_cid->sb_igu_id, false);
3919 	if (rc)
3920 		goto out;
3921 
3922 	address = BAR0_MAP_REG_USDM_RAM +
3923 		  USTORM_ETH_QUEUE_ZONE_OFFSET(p_cid->abs.queue_id);
3924 
3925 	rc = qed_set_coalesce(p_hwfn, p_ptt, address, &eth_qzone,
3926 			      sizeof(struct ustorm_eth_queue_zone), timeset);
3927 	if (rc)
3928 		goto out;
3929 
3930 out:
3931 	return rc;
3932 }
3933 
3934 int qed_set_txq_coalesce(struct qed_hwfn *p_hwfn,
3935 			 struct qed_ptt *p_ptt,
3936 			 u16 coalesce, struct qed_queue_cid *p_cid)
3937 {
3938 	struct xstorm_eth_queue_zone eth_qzone;
3939 	u8 timeset, timer_res;
3940 	u32 address;
3941 	int rc;
3942 
3943 	/* Coalesce = (timeset << timer-resolution), timeset is 7bit wide */
3944 	if (coalesce <= 0x7F) {
3945 		timer_res = 0;
3946 	} else if (coalesce <= 0xFF) {
3947 		timer_res = 1;
3948 	} else if (coalesce <= 0x1FF) {
3949 		timer_res = 2;
3950 	} else {
3951 		DP_ERR(p_hwfn, "Invalid coalesce value - %d\n", coalesce);
3952 		return -EINVAL;
3953 	}
3954 	timeset = (u8)(coalesce >> timer_res);
3955 
3956 	rc = qed_int_set_timer_res(p_hwfn, p_ptt, timer_res,
3957 				   p_cid->sb_igu_id, true);
3958 	if (rc)
3959 		goto out;
3960 
3961 	address = BAR0_MAP_REG_XSDM_RAM +
3962 		  XSTORM_ETH_QUEUE_ZONE_OFFSET(p_cid->abs.queue_id);
3963 
3964 	rc = qed_set_coalesce(p_hwfn, p_ptt, address, &eth_qzone,
3965 			      sizeof(struct xstorm_eth_queue_zone), timeset);
3966 out:
3967 	return rc;
3968 }
3969 
3970 /* Calculate final WFQ values for all vports and configure them.
3971  * After this configuration each vport will have
3972  * approx min rate =  min_pf_rate * (vport_wfq / QED_WFQ_UNIT)
3973  */
3974 static void qed_configure_wfq_for_all_vports(struct qed_hwfn *p_hwfn,
3975 					     struct qed_ptt *p_ptt,
3976 					     u32 min_pf_rate)
3977 {
3978 	struct init_qm_vport_params *vport_params;
3979 	int i;
3980 
3981 	vport_params = p_hwfn->qm_info.qm_vport_params;
3982 
3983 	for (i = 0; i < p_hwfn->qm_info.num_vports; i++) {
3984 		u32 wfq_speed = p_hwfn->qm_info.wfq_data[i].min_speed;
3985 
3986 		vport_params[i].vport_wfq = (wfq_speed * QED_WFQ_UNIT) /
3987 						min_pf_rate;
3988 		qed_init_vport_wfq(p_hwfn, p_ptt,
3989 				   vport_params[i].first_tx_pq_id,
3990 				   vport_params[i].vport_wfq);
3991 	}
3992 }
3993 
3994 static void qed_init_wfq_default_param(struct qed_hwfn *p_hwfn,
3995 				       u32 min_pf_rate)
3996 
3997 {
3998 	int i;
3999 
4000 	for (i = 0; i < p_hwfn->qm_info.num_vports; i++)
4001 		p_hwfn->qm_info.qm_vport_params[i].vport_wfq = 1;
4002 }
4003 
4004 static void qed_disable_wfq_for_all_vports(struct qed_hwfn *p_hwfn,
4005 					   struct qed_ptt *p_ptt,
4006 					   u32 min_pf_rate)
4007 {
4008 	struct init_qm_vport_params *vport_params;
4009 	int i;
4010 
4011 	vport_params = p_hwfn->qm_info.qm_vport_params;
4012 
4013 	for (i = 0; i < p_hwfn->qm_info.num_vports; i++) {
4014 		qed_init_wfq_default_param(p_hwfn, min_pf_rate);
4015 		qed_init_vport_wfq(p_hwfn, p_ptt,
4016 				   vport_params[i].first_tx_pq_id,
4017 				   vport_params[i].vport_wfq);
4018 	}
4019 }
4020 
4021 /* This function performs several validations for WFQ
4022  * configuration and required min rate for a given vport
4023  * 1. req_rate must be greater than one percent of min_pf_rate.
4024  * 2. req_rate should not cause other vports [not configured for WFQ explicitly]
4025  *    rates to get less than one percent of min_pf_rate.
4026  * 3. total_req_min_rate [all vports min rate sum] shouldn't exceed min_pf_rate.
4027  */
4028 static int qed_init_wfq_param(struct qed_hwfn *p_hwfn,
4029 			      u16 vport_id, u32 req_rate, u32 min_pf_rate)
4030 {
4031 	u32 total_req_min_rate = 0, total_left_rate = 0, left_rate_per_vp = 0;
4032 	int non_requested_count = 0, req_count = 0, i, num_vports;
4033 
4034 	num_vports = p_hwfn->qm_info.num_vports;
4035 
4036 	/* Accounting for the vports which are configured for WFQ explicitly */
4037 	for (i = 0; i < num_vports; i++) {
4038 		u32 tmp_speed;
4039 
4040 		if ((i != vport_id) &&
4041 		    p_hwfn->qm_info.wfq_data[i].configured) {
4042 			req_count++;
4043 			tmp_speed = p_hwfn->qm_info.wfq_data[i].min_speed;
4044 			total_req_min_rate += tmp_speed;
4045 		}
4046 	}
4047 
4048 	/* Include current vport data as well */
4049 	req_count++;
4050 	total_req_min_rate += req_rate;
4051 	non_requested_count = num_vports - req_count;
4052 
4053 	if (req_rate < min_pf_rate / QED_WFQ_UNIT) {
4054 		DP_VERBOSE(p_hwfn, NETIF_MSG_LINK,
4055 			   "Vport [%d] - Requested rate[%d Mbps] is less than one percent of configured PF min rate[%d Mbps]\n",
4056 			   vport_id, req_rate, min_pf_rate);
4057 		return -EINVAL;
4058 	}
4059 
4060 	if (num_vports > QED_WFQ_UNIT) {
4061 		DP_VERBOSE(p_hwfn, NETIF_MSG_LINK,
4062 			   "Number of vports is greater than %d\n",
4063 			   QED_WFQ_UNIT);
4064 		return -EINVAL;
4065 	}
4066 
4067 	if (total_req_min_rate > min_pf_rate) {
4068 		DP_VERBOSE(p_hwfn, NETIF_MSG_LINK,
4069 			   "Total requested min rate for all vports[%d Mbps] is greater than configured PF min rate[%d Mbps]\n",
4070 			   total_req_min_rate, min_pf_rate);
4071 		return -EINVAL;
4072 	}
4073 
4074 	total_left_rate	= min_pf_rate - total_req_min_rate;
4075 
4076 	left_rate_per_vp = total_left_rate / non_requested_count;
4077 	if (left_rate_per_vp <  min_pf_rate / QED_WFQ_UNIT) {
4078 		DP_VERBOSE(p_hwfn, NETIF_MSG_LINK,
4079 			   "Non WFQ configured vports rate [%d Mbps] is less than one percent of configured PF min rate[%d Mbps]\n",
4080 			   left_rate_per_vp, min_pf_rate);
4081 		return -EINVAL;
4082 	}
4083 
4084 	p_hwfn->qm_info.wfq_data[vport_id].min_speed = req_rate;
4085 	p_hwfn->qm_info.wfq_data[vport_id].configured = true;
4086 
4087 	for (i = 0; i < num_vports; i++) {
4088 		if (p_hwfn->qm_info.wfq_data[i].configured)
4089 			continue;
4090 
4091 		p_hwfn->qm_info.wfq_data[i].min_speed = left_rate_per_vp;
4092 	}
4093 
4094 	return 0;
4095 }
4096 
4097 static int __qed_configure_vport_wfq(struct qed_hwfn *p_hwfn,
4098 				     struct qed_ptt *p_ptt, u16 vp_id, u32 rate)
4099 {
4100 	struct qed_mcp_link_state *p_link;
4101 	int rc = 0;
4102 
4103 	p_link = &p_hwfn->cdev->hwfns[0].mcp_info->link_output;
4104 
4105 	if (!p_link->min_pf_rate) {
4106 		p_hwfn->qm_info.wfq_data[vp_id].min_speed = rate;
4107 		p_hwfn->qm_info.wfq_data[vp_id].configured = true;
4108 		return rc;
4109 	}
4110 
4111 	rc = qed_init_wfq_param(p_hwfn, vp_id, rate, p_link->min_pf_rate);
4112 
4113 	if (!rc)
4114 		qed_configure_wfq_for_all_vports(p_hwfn, p_ptt,
4115 						 p_link->min_pf_rate);
4116 	else
4117 		DP_NOTICE(p_hwfn,
4118 			  "Validation failed while configuring min rate\n");
4119 
4120 	return rc;
4121 }
4122 
4123 static int __qed_configure_vp_wfq_on_link_change(struct qed_hwfn *p_hwfn,
4124 						 struct qed_ptt *p_ptt,
4125 						 u32 min_pf_rate)
4126 {
4127 	bool use_wfq = false;
4128 	int rc = 0;
4129 	u16 i;
4130 
4131 	/* Validate all pre configured vports for wfq */
4132 	for (i = 0; i < p_hwfn->qm_info.num_vports; i++) {
4133 		u32 rate;
4134 
4135 		if (!p_hwfn->qm_info.wfq_data[i].configured)
4136 			continue;
4137 
4138 		rate = p_hwfn->qm_info.wfq_data[i].min_speed;
4139 		use_wfq = true;
4140 
4141 		rc = qed_init_wfq_param(p_hwfn, i, rate, min_pf_rate);
4142 		if (rc) {
4143 			DP_NOTICE(p_hwfn,
4144 				  "WFQ validation failed while configuring min rate\n");
4145 			break;
4146 		}
4147 	}
4148 
4149 	if (!rc && use_wfq)
4150 		qed_configure_wfq_for_all_vports(p_hwfn, p_ptt, min_pf_rate);
4151 	else
4152 		qed_disable_wfq_for_all_vports(p_hwfn, p_ptt, min_pf_rate);
4153 
4154 	return rc;
4155 }
4156 
4157 /* Main API for qed clients to configure vport min rate.
4158  * vp_id - vport id in PF Range[0 - (total_num_vports_per_pf - 1)]
4159  * rate - Speed in Mbps needs to be assigned to a given vport.
4160  */
4161 int qed_configure_vport_wfq(struct qed_dev *cdev, u16 vp_id, u32 rate)
4162 {
4163 	int i, rc = -EINVAL;
4164 
4165 	/* Currently not supported; Might change in future */
4166 	if (cdev->num_hwfns > 1) {
4167 		DP_NOTICE(cdev,
4168 			  "WFQ configuration is not supported for this device\n");
4169 		return rc;
4170 	}
4171 
4172 	for_each_hwfn(cdev, i) {
4173 		struct qed_hwfn *p_hwfn = &cdev->hwfns[i];
4174 		struct qed_ptt *p_ptt;
4175 
4176 		p_ptt = qed_ptt_acquire(p_hwfn);
4177 		if (!p_ptt)
4178 			return -EBUSY;
4179 
4180 		rc = __qed_configure_vport_wfq(p_hwfn, p_ptt, vp_id, rate);
4181 
4182 		if (rc) {
4183 			qed_ptt_release(p_hwfn, p_ptt);
4184 			return rc;
4185 		}
4186 
4187 		qed_ptt_release(p_hwfn, p_ptt);
4188 	}
4189 
4190 	return rc;
4191 }
4192 
4193 /* API to configure WFQ from mcp link change */
4194 void qed_configure_vp_wfq_on_link_change(struct qed_dev *cdev,
4195 					 struct qed_ptt *p_ptt, u32 min_pf_rate)
4196 {
4197 	int i;
4198 
4199 	if (cdev->num_hwfns > 1) {
4200 		DP_VERBOSE(cdev,
4201 			   NETIF_MSG_LINK,
4202 			   "WFQ configuration is not supported for this device\n");
4203 		return;
4204 	}
4205 
4206 	for_each_hwfn(cdev, i) {
4207 		struct qed_hwfn *p_hwfn = &cdev->hwfns[i];
4208 
4209 		__qed_configure_vp_wfq_on_link_change(p_hwfn, p_ptt,
4210 						      min_pf_rate);
4211 	}
4212 }
4213 
4214 int __qed_configure_pf_max_bandwidth(struct qed_hwfn *p_hwfn,
4215 				     struct qed_ptt *p_ptt,
4216 				     struct qed_mcp_link_state *p_link,
4217 				     u8 max_bw)
4218 {
4219 	int rc = 0;
4220 
4221 	p_hwfn->mcp_info->func_info.bandwidth_max = max_bw;
4222 
4223 	if (!p_link->line_speed && (max_bw != 100))
4224 		return rc;
4225 
4226 	p_link->speed = (p_link->line_speed * max_bw) / 100;
4227 	p_hwfn->qm_info.pf_rl = p_link->speed;
4228 
4229 	/* Since the limiter also affects Tx-switched traffic, we don't want it
4230 	 * to limit such traffic in case there's no actual limit.
4231 	 * In that case, set limit to imaginary high boundary.
4232 	 */
4233 	if (max_bw == 100)
4234 		p_hwfn->qm_info.pf_rl = 100000;
4235 
4236 	rc = qed_init_pf_rl(p_hwfn, p_ptt, p_hwfn->rel_pf_id,
4237 			    p_hwfn->qm_info.pf_rl);
4238 
4239 	DP_VERBOSE(p_hwfn, NETIF_MSG_LINK,
4240 		   "Configured MAX bandwidth to be %08x Mb/sec\n",
4241 		   p_link->speed);
4242 
4243 	return rc;
4244 }
4245 
4246 /* Main API to configure PF max bandwidth where bw range is [1 - 100] */
4247 int qed_configure_pf_max_bandwidth(struct qed_dev *cdev, u8 max_bw)
4248 {
4249 	int i, rc = -EINVAL;
4250 
4251 	if (max_bw < 1 || max_bw > 100) {
4252 		DP_NOTICE(cdev, "PF max bw valid range is [1-100]\n");
4253 		return rc;
4254 	}
4255 
4256 	for_each_hwfn(cdev, i) {
4257 		struct qed_hwfn	*p_hwfn = &cdev->hwfns[i];
4258 		struct qed_hwfn *p_lead = QED_LEADING_HWFN(cdev);
4259 		struct qed_mcp_link_state *p_link;
4260 		struct qed_ptt *p_ptt;
4261 
4262 		p_link = &p_lead->mcp_info->link_output;
4263 
4264 		p_ptt = qed_ptt_acquire(p_hwfn);
4265 		if (!p_ptt)
4266 			return -EBUSY;
4267 
4268 		rc = __qed_configure_pf_max_bandwidth(p_hwfn, p_ptt,
4269 						      p_link, max_bw);
4270 
4271 		qed_ptt_release(p_hwfn, p_ptt);
4272 
4273 		if (rc)
4274 			break;
4275 	}
4276 
4277 	return rc;
4278 }
4279 
4280 int __qed_configure_pf_min_bandwidth(struct qed_hwfn *p_hwfn,
4281 				     struct qed_ptt *p_ptt,
4282 				     struct qed_mcp_link_state *p_link,
4283 				     u8 min_bw)
4284 {
4285 	int rc = 0;
4286 
4287 	p_hwfn->mcp_info->func_info.bandwidth_min = min_bw;
4288 	p_hwfn->qm_info.pf_wfq = min_bw;
4289 
4290 	if (!p_link->line_speed)
4291 		return rc;
4292 
4293 	p_link->min_pf_rate = (p_link->line_speed * min_bw) / 100;
4294 
4295 	rc = qed_init_pf_wfq(p_hwfn, p_ptt, p_hwfn->rel_pf_id, min_bw);
4296 
4297 	DP_VERBOSE(p_hwfn, NETIF_MSG_LINK,
4298 		   "Configured MIN bandwidth to be %d Mb/sec\n",
4299 		   p_link->min_pf_rate);
4300 
4301 	return rc;
4302 }
4303 
4304 /* Main API to configure PF min bandwidth where bw range is [1-100] */
4305 int qed_configure_pf_min_bandwidth(struct qed_dev *cdev, u8 min_bw)
4306 {
4307 	int i, rc = -EINVAL;
4308 
4309 	if (min_bw < 1 || min_bw > 100) {
4310 		DP_NOTICE(cdev, "PF min bw valid range is [1-100]\n");
4311 		return rc;
4312 	}
4313 
4314 	for_each_hwfn(cdev, i) {
4315 		struct qed_hwfn *p_hwfn = &cdev->hwfns[i];
4316 		struct qed_hwfn *p_lead = QED_LEADING_HWFN(cdev);
4317 		struct qed_mcp_link_state *p_link;
4318 		struct qed_ptt *p_ptt;
4319 
4320 		p_link = &p_lead->mcp_info->link_output;
4321 
4322 		p_ptt = qed_ptt_acquire(p_hwfn);
4323 		if (!p_ptt)
4324 			return -EBUSY;
4325 
4326 		rc = __qed_configure_pf_min_bandwidth(p_hwfn, p_ptt,
4327 						      p_link, min_bw);
4328 		if (rc) {
4329 			qed_ptt_release(p_hwfn, p_ptt);
4330 			return rc;
4331 		}
4332 
4333 		if (p_link->min_pf_rate) {
4334 			u32 min_rate = p_link->min_pf_rate;
4335 
4336 			rc = __qed_configure_vp_wfq_on_link_change(p_hwfn,
4337 								   p_ptt,
4338 								   min_rate);
4339 		}
4340 
4341 		qed_ptt_release(p_hwfn, p_ptt);
4342 	}
4343 
4344 	return rc;
4345 }
4346 
4347 void qed_clean_wfq_db(struct qed_hwfn *p_hwfn, struct qed_ptt *p_ptt)
4348 {
4349 	struct qed_mcp_link_state *p_link;
4350 
4351 	p_link = &p_hwfn->mcp_info->link_output;
4352 
4353 	if (p_link->min_pf_rate)
4354 		qed_disable_wfq_for_all_vports(p_hwfn, p_ptt,
4355 					       p_link->min_pf_rate);
4356 
4357 	memset(p_hwfn->qm_info.wfq_data, 0,
4358 	       sizeof(*p_hwfn->qm_info.wfq_data) * p_hwfn->qm_info.num_vports);
4359 }
4360 
4361 int qed_device_num_engines(struct qed_dev *cdev)
4362 {
4363 	return QED_IS_BB(cdev) ? 2 : 1;
4364 }
4365 
4366 static int qed_device_num_ports(struct qed_dev *cdev)
4367 {
4368 	/* in CMT always only one port */
4369 	if (cdev->num_hwfns > 1)
4370 		return 1;
4371 
4372 	return cdev->num_ports_in_engine * qed_device_num_engines(cdev);
4373 }
4374 
4375 int qed_device_get_port_id(struct qed_dev *cdev)
4376 {
4377 	return (QED_LEADING_HWFN(cdev)->abs_pf_id) % qed_device_num_ports(cdev);
4378 }
4379 
4380 void qed_set_fw_mac_addr(__le16 *fw_msb,
4381 			 __le16 *fw_mid, __le16 *fw_lsb, u8 *mac)
4382 {
4383 	((u8 *)fw_msb)[0] = mac[1];
4384 	((u8 *)fw_msb)[1] = mac[0];
4385 	((u8 *)fw_mid)[0] = mac[3];
4386 	((u8 *)fw_mid)[1] = mac[2];
4387 	((u8 *)fw_lsb)[0] = mac[5];
4388 	((u8 *)fw_lsb)[1] = mac[4];
4389 }
4390