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