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