xref: /linux/drivers/net/ethernet/qlogic/qed/qed_dev.c (revision a6cdeeb16bff89c8486324f53577db058cbe81ba)
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 /******************** Doorbell Recovery *******************/
70 /* The doorbell recovery mechanism consists of a list of entries which represent
71  * doorbelling entities (l2 queues, roce sq/rq/cqs, the slowpath spq, etc). Each
72  * entity needs to register with the mechanism and provide the parameters
73  * describing it's doorbell, including a location where last used doorbell data
74  * can be found. The doorbell execute function will traverse the list and
75  * doorbell all of the registered entries.
76  */
77 struct qed_db_recovery_entry {
78 	struct list_head list_entry;
79 	void __iomem *db_addr;
80 	void *db_data;
81 	enum qed_db_rec_width db_width;
82 	enum qed_db_rec_space db_space;
83 	u8 hwfn_idx;
84 };
85 
86 /* Display a single doorbell recovery entry */
87 static void qed_db_recovery_dp_entry(struct qed_hwfn *p_hwfn,
88 				     struct qed_db_recovery_entry *db_entry,
89 				     char *action)
90 {
91 	DP_VERBOSE(p_hwfn,
92 		   QED_MSG_SPQ,
93 		   "(%s: db_entry %p, addr %p, data %p, width %s, %s space, hwfn %d)\n",
94 		   action,
95 		   db_entry,
96 		   db_entry->db_addr,
97 		   db_entry->db_data,
98 		   db_entry->db_width == DB_REC_WIDTH_32B ? "32b" : "64b",
99 		   db_entry->db_space == DB_REC_USER ? "user" : "kernel",
100 		   db_entry->hwfn_idx);
101 }
102 
103 /* Doorbell address sanity (address within doorbell bar range) */
104 static bool qed_db_rec_sanity(struct qed_dev *cdev,
105 			      void __iomem *db_addr,
106 			      enum qed_db_rec_width db_width,
107 			      void *db_data)
108 {
109 	u32 width = (db_width == DB_REC_WIDTH_32B) ? 32 : 64;
110 
111 	/* Make sure doorbell address is within the doorbell bar */
112 	if (db_addr < cdev->doorbells ||
113 	    (u8 __iomem *)db_addr + width >
114 	    (u8 __iomem *)cdev->doorbells + cdev->db_size) {
115 		WARN(true,
116 		     "Illegal doorbell address: %p. Legal range for doorbell addresses is [%p..%p]\n",
117 		     db_addr,
118 		     cdev->doorbells,
119 		     (u8 __iomem *)cdev->doorbells + cdev->db_size);
120 		return false;
121 	}
122 
123 	/* ake sure doorbell data pointer is not null */
124 	if (!db_data) {
125 		WARN(true, "Illegal doorbell data pointer: %p", db_data);
126 		return false;
127 	}
128 
129 	return true;
130 }
131 
132 /* Find hwfn according to the doorbell address */
133 static struct qed_hwfn *qed_db_rec_find_hwfn(struct qed_dev *cdev,
134 					     void __iomem *db_addr)
135 {
136 	struct qed_hwfn *p_hwfn;
137 
138 	/* In CMT doorbell bar is split down the middle between engine 0 and enigne 1 */
139 	if (cdev->num_hwfns > 1)
140 		p_hwfn = db_addr < cdev->hwfns[1].doorbells ?
141 		    &cdev->hwfns[0] : &cdev->hwfns[1];
142 	else
143 		p_hwfn = QED_LEADING_HWFN(cdev);
144 
145 	return p_hwfn;
146 }
147 
148 /* Add a new entry to the doorbell recovery mechanism */
149 int qed_db_recovery_add(struct qed_dev *cdev,
150 			void __iomem *db_addr,
151 			void *db_data,
152 			enum qed_db_rec_width db_width,
153 			enum qed_db_rec_space db_space)
154 {
155 	struct qed_db_recovery_entry *db_entry;
156 	struct qed_hwfn *p_hwfn;
157 
158 	/* Shortcircuit VFs, for now */
159 	if (IS_VF(cdev)) {
160 		DP_VERBOSE(cdev,
161 			   QED_MSG_IOV, "db recovery - skipping VF doorbell\n");
162 		return 0;
163 	}
164 
165 	/* Sanitize doorbell address */
166 	if (!qed_db_rec_sanity(cdev, db_addr, db_width, db_data))
167 		return -EINVAL;
168 
169 	/* Obtain hwfn from doorbell address */
170 	p_hwfn = qed_db_rec_find_hwfn(cdev, db_addr);
171 
172 	/* Create entry */
173 	db_entry = kzalloc(sizeof(*db_entry), GFP_KERNEL);
174 	if (!db_entry) {
175 		DP_NOTICE(cdev, "Failed to allocate a db recovery entry\n");
176 		return -ENOMEM;
177 	}
178 
179 	/* Populate entry */
180 	db_entry->db_addr = db_addr;
181 	db_entry->db_data = db_data;
182 	db_entry->db_width = db_width;
183 	db_entry->db_space = db_space;
184 	db_entry->hwfn_idx = p_hwfn->my_id;
185 
186 	/* Display */
187 	qed_db_recovery_dp_entry(p_hwfn, db_entry, "Adding");
188 
189 	/* Protect the list */
190 	spin_lock_bh(&p_hwfn->db_recovery_info.lock);
191 	list_add_tail(&db_entry->list_entry, &p_hwfn->db_recovery_info.list);
192 	spin_unlock_bh(&p_hwfn->db_recovery_info.lock);
193 
194 	return 0;
195 }
196 
197 /* Remove an entry from the doorbell recovery mechanism */
198 int qed_db_recovery_del(struct qed_dev *cdev,
199 			void __iomem *db_addr, void *db_data)
200 {
201 	struct qed_db_recovery_entry *db_entry = NULL;
202 	struct qed_hwfn *p_hwfn;
203 	int rc = -EINVAL;
204 
205 	/* Shortcircuit VFs, for now */
206 	if (IS_VF(cdev)) {
207 		DP_VERBOSE(cdev,
208 			   QED_MSG_IOV, "db recovery - skipping VF doorbell\n");
209 		return 0;
210 	}
211 
212 	/* Obtain hwfn from doorbell address */
213 	p_hwfn = qed_db_rec_find_hwfn(cdev, db_addr);
214 
215 	/* Protect the list */
216 	spin_lock_bh(&p_hwfn->db_recovery_info.lock);
217 	list_for_each_entry(db_entry,
218 			    &p_hwfn->db_recovery_info.list, list_entry) {
219 		/* search according to db_data addr since db_addr is not unique (roce) */
220 		if (db_entry->db_data == db_data) {
221 			qed_db_recovery_dp_entry(p_hwfn, db_entry, "Deleting");
222 			list_del(&db_entry->list_entry);
223 			rc = 0;
224 			break;
225 		}
226 	}
227 
228 	spin_unlock_bh(&p_hwfn->db_recovery_info.lock);
229 
230 	if (rc == -EINVAL)
231 
232 		DP_NOTICE(p_hwfn,
233 			  "Failed to find element in list. Key (db_data addr) was %p. db_addr was %p\n",
234 			  db_data, db_addr);
235 	else
236 		kfree(db_entry);
237 
238 	return rc;
239 }
240 
241 /* Initialize the doorbell recovery mechanism */
242 static int qed_db_recovery_setup(struct qed_hwfn *p_hwfn)
243 {
244 	DP_VERBOSE(p_hwfn, QED_MSG_SPQ, "Setting up db recovery\n");
245 
246 	/* Make sure db_size was set in cdev */
247 	if (!p_hwfn->cdev->db_size) {
248 		DP_ERR(p_hwfn->cdev, "db_size not set\n");
249 		return -EINVAL;
250 	}
251 
252 	INIT_LIST_HEAD(&p_hwfn->db_recovery_info.list);
253 	spin_lock_init(&p_hwfn->db_recovery_info.lock);
254 	p_hwfn->db_recovery_info.db_recovery_counter = 0;
255 
256 	return 0;
257 }
258 
259 /* Destroy the doorbell recovery mechanism */
260 static void qed_db_recovery_teardown(struct qed_hwfn *p_hwfn)
261 {
262 	struct qed_db_recovery_entry *db_entry = NULL;
263 
264 	DP_VERBOSE(p_hwfn, QED_MSG_SPQ, "Tearing down db recovery\n");
265 	if (!list_empty(&p_hwfn->db_recovery_info.list)) {
266 		DP_VERBOSE(p_hwfn,
267 			   QED_MSG_SPQ,
268 			   "Doorbell Recovery teardown found the doorbell recovery list was not empty (Expected in disorderly driver unload (e.g. recovery) otherwise this probably means some flow forgot to db_recovery_del). Prepare to purge doorbell recovery list...\n");
269 		while (!list_empty(&p_hwfn->db_recovery_info.list)) {
270 			db_entry =
271 			    list_first_entry(&p_hwfn->db_recovery_info.list,
272 					     struct qed_db_recovery_entry,
273 					     list_entry);
274 			qed_db_recovery_dp_entry(p_hwfn, db_entry, "Purging");
275 			list_del(&db_entry->list_entry);
276 			kfree(db_entry);
277 		}
278 	}
279 	p_hwfn->db_recovery_info.db_recovery_counter = 0;
280 }
281 
282 /* Print the content of the doorbell recovery mechanism */
283 void qed_db_recovery_dp(struct qed_hwfn *p_hwfn)
284 {
285 	struct qed_db_recovery_entry *db_entry = NULL;
286 
287 	DP_NOTICE(p_hwfn,
288 		  "Displaying doorbell recovery database. Counter was %d\n",
289 		  p_hwfn->db_recovery_info.db_recovery_counter);
290 
291 	/* Protect the list */
292 	spin_lock_bh(&p_hwfn->db_recovery_info.lock);
293 	list_for_each_entry(db_entry,
294 			    &p_hwfn->db_recovery_info.list, list_entry) {
295 		qed_db_recovery_dp_entry(p_hwfn, db_entry, "Printing");
296 	}
297 
298 	spin_unlock_bh(&p_hwfn->db_recovery_info.lock);
299 }
300 
301 /* Ring the doorbell of a single doorbell recovery entry */
302 static void qed_db_recovery_ring(struct qed_hwfn *p_hwfn,
303 				 struct qed_db_recovery_entry *db_entry)
304 {
305 	/* Print according to width */
306 	if (db_entry->db_width == DB_REC_WIDTH_32B) {
307 		DP_VERBOSE(p_hwfn, QED_MSG_SPQ,
308 			   "ringing doorbell address %p data %x\n",
309 			   db_entry->db_addr,
310 			   *(u32 *)db_entry->db_data);
311 	} else {
312 		DP_VERBOSE(p_hwfn, QED_MSG_SPQ,
313 			   "ringing doorbell address %p data %llx\n",
314 			   db_entry->db_addr,
315 			   *(u64 *)(db_entry->db_data));
316 	}
317 
318 	/* Sanity */
319 	if (!qed_db_rec_sanity(p_hwfn->cdev, db_entry->db_addr,
320 			       db_entry->db_width, db_entry->db_data))
321 		return;
322 
323 	/* Flush the write combined buffer. Since there are multiple doorbelling
324 	 * entities using the same address, if we don't flush, a transaction
325 	 * could be lost.
326 	 */
327 	wmb();
328 
329 	/* Ring the doorbell */
330 	if (db_entry->db_width == DB_REC_WIDTH_32B)
331 		DIRECT_REG_WR(db_entry->db_addr,
332 			      *(u32 *)(db_entry->db_data));
333 	else
334 		DIRECT_REG_WR64(db_entry->db_addr,
335 				*(u64 *)(db_entry->db_data));
336 
337 	/* Flush the write combined buffer. Next doorbell may come from a
338 	 * different entity to the same address...
339 	 */
340 	wmb();
341 }
342 
343 /* Traverse the doorbell recovery entry list and ring all the doorbells */
344 void qed_db_recovery_execute(struct qed_hwfn *p_hwfn)
345 {
346 	struct qed_db_recovery_entry *db_entry = NULL;
347 
348 	DP_NOTICE(p_hwfn, "Executing doorbell recovery. Counter was %d\n",
349 		  p_hwfn->db_recovery_info.db_recovery_counter);
350 
351 	/* Track amount of times recovery was executed */
352 	p_hwfn->db_recovery_info.db_recovery_counter++;
353 
354 	/* Protect the list */
355 	spin_lock_bh(&p_hwfn->db_recovery_info.lock);
356 	list_for_each_entry(db_entry,
357 			    &p_hwfn->db_recovery_info.list, list_entry)
358 		qed_db_recovery_ring(p_hwfn, db_entry);
359 	spin_unlock_bh(&p_hwfn->db_recovery_info.lock);
360 }
361 
362 /******************** Doorbell Recovery end ****************/
363 
364 /********************************** NIG LLH ***********************************/
365 
366 enum qed_llh_filter_type {
367 	QED_LLH_FILTER_TYPE_MAC,
368 	QED_LLH_FILTER_TYPE_PROTOCOL,
369 };
370 
371 struct qed_llh_mac_filter {
372 	u8 addr[ETH_ALEN];
373 };
374 
375 struct qed_llh_protocol_filter {
376 	enum qed_llh_prot_filter_type_t type;
377 	u16 source_port_or_eth_type;
378 	u16 dest_port;
379 };
380 
381 union qed_llh_filter {
382 	struct qed_llh_mac_filter mac;
383 	struct qed_llh_protocol_filter protocol;
384 };
385 
386 struct qed_llh_filter_info {
387 	bool b_enabled;
388 	u32 ref_cnt;
389 	enum qed_llh_filter_type type;
390 	union qed_llh_filter filter;
391 };
392 
393 struct qed_llh_info {
394 	/* Number of LLH filters banks */
395 	u8 num_ppfid;
396 
397 #define MAX_NUM_PPFID   8
398 	u8 ppfid_array[MAX_NUM_PPFID];
399 
400 	/* Array of filters arrays:
401 	 * "num_ppfid" elements of filters banks, where each is an array of
402 	 * "NIG_REG_LLH_FUNC_FILTER_EN_SIZE" filters.
403 	 */
404 	struct qed_llh_filter_info **pp_filters;
405 };
406 
407 static void qed_llh_free(struct qed_dev *cdev)
408 {
409 	struct qed_llh_info *p_llh_info = cdev->p_llh_info;
410 	u32 i;
411 
412 	if (p_llh_info) {
413 		if (p_llh_info->pp_filters)
414 			for (i = 0; i < p_llh_info->num_ppfid; i++)
415 				kfree(p_llh_info->pp_filters[i]);
416 
417 		kfree(p_llh_info->pp_filters);
418 	}
419 
420 	kfree(p_llh_info);
421 	cdev->p_llh_info = NULL;
422 }
423 
424 static int qed_llh_alloc(struct qed_dev *cdev)
425 {
426 	struct qed_llh_info *p_llh_info;
427 	u32 size, i;
428 
429 	p_llh_info = kzalloc(sizeof(*p_llh_info), GFP_KERNEL);
430 	if (!p_llh_info)
431 		return -ENOMEM;
432 	cdev->p_llh_info = p_llh_info;
433 
434 	for (i = 0; i < MAX_NUM_PPFID; i++) {
435 		if (!(cdev->ppfid_bitmap & (0x1 << i)))
436 			continue;
437 
438 		p_llh_info->ppfid_array[p_llh_info->num_ppfid] = i;
439 		DP_VERBOSE(cdev, QED_MSG_SP, "ppfid_array[%d] = %hhd\n",
440 			   p_llh_info->num_ppfid, i);
441 		p_llh_info->num_ppfid++;
442 	}
443 
444 	size = p_llh_info->num_ppfid * sizeof(*p_llh_info->pp_filters);
445 	p_llh_info->pp_filters = kzalloc(size, GFP_KERNEL);
446 	if (!p_llh_info->pp_filters)
447 		return -ENOMEM;
448 
449 	size = NIG_REG_LLH_FUNC_FILTER_EN_SIZE *
450 	    sizeof(**p_llh_info->pp_filters);
451 	for (i = 0; i < p_llh_info->num_ppfid; i++) {
452 		p_llh_info->pp_filters[i] = kzalloc(size, GFP_KERNEL);
453 		if (!p_llh_info->pp_filters[i])
454 			return -ENOMEM;
455 	}
456 
457 	return 0;
458 }
459 
460 static int qed_llh_shadow_sanity(struct qed_dev *cdev,
461 				 u8 ppfid, u8 filter_idx, const char *action)
462 {
463 	struct qed_llh_info *p_llh_info = cdev->p_llh_info;
464 
465 	if (ppfid >= p_llh_info->num_ppfid) {
466 		DP_NOTICE(cdev,
467 			  "LLH shadow [%s]: using ppfid %d while only %d ppfids are available\n",
468 			  action, ppfid, p_llh_info->num_ppfid);
469 		return -EINVAL;
470 	}
471 
472 	if (filter_idx >= NIG_REG_LLH_FUNC_FILTER_EN_SIZE) {
473 		DP_NOTICE(cdev,
474 			  "LLH shadow [%s]: using filter_idx %d while only %d filters are available\n",
475 			  action, filter_idx, NIG_REG_LLH_FUNC_FILTER_EN_SIZE);
476 		return -EINVAL;
477 	}
478 
479 	return 0;
480 }
481 
482 #define QED_LLH_INVALID_FILTER_IDX      0xff
483 
484 static int
485 qed_llh_shadow_search_filter(struct qed_dev *cdev,
486 			     u8 ppfid,
487 			     union qed_llh_filter *p_filter, u8 *p_filter_idx)
488 {
489 	struct qed_llh_info *p_llh_info = cdev->p_llh_info;
490 	struct qed_llh_filter_info *p_filters;
491 	int rc;
492 	u8 i;
493 
494 	rc = qed_llh_shadow_sanity(cdev, ppfid, 0, "search");
495 	if (rc)
496 		return rc;
497 
498 	*p_filter_idx = QED_LLH_INVALID_FILTER_IDX;
499 
500 	p_filters = p_llh_info->pp_filters[ppfid];
501 	for (i = 0; i < NIG_REG_LLH_FUNC_FILTER_EN_SIZE; i++) {
502 		if (!memcmp(p_filter, &p_filters[i].filter,
503 			    sizeof(*p_filter))) {
504 			*p_filter_idx = i;
505 			break;
506 		}
507 	}
508 
509 	return 0;
510 }
511 
512 static int
513 qed_llh_shadow_get_free_idx(struct qed_dev *cdev, u8 ppfid, u8 *p_filter_idx)
514 {
515 	struct qed_llh_info *p_llh_info = cdev->p_llh_info;
516 	struct qed_llh_filter_info *p_filters;
517 	int rc;
518 	u8 i;
519 
520 	rc = qed_llh_shadow_sanity(cdev, ppfid, 0, "get_free_idx");
521 	if (rc)
522 		return rc;
523 
524 	*p_filter_idx = QED_LLH_INVALID_FILTER_IDX;
525 
526 	p_filters = p_llh_info->pp_filters[ppfid];
527 	for (i = 0; i < NIG_REG_LLH_FUNC_FILTER_EN_SIZE; i++) {
528 		if (!p_filters[i].b_enabled) {
529 			*p_filter_idx = i;
530 			break;
531 		}
532 	}
533 
534 	return 0;
535 }
536 
537 static int
538 __qed_llh_shadow_add_filter(struct qed_dev *cdev,
539 			    u8 ppfid,
540 			    u8 filter_idx,
541 			    enum qed_llh_filter_type type,
542 			    union qed_llh_filter *p_filter, u32 *p_ref_cnt)
543 {
544 	struct qed_llh_info *p_llh_info = cdev->p_llh_info;
545 	struct qed_llh_filter_info *p_filters;
546 	int rc;
547 
548 	rc = qed_llh_shadow_sanity(cdev, ppfid, filter_idx, "add");
549 	if (rc)
550 		return rc;
551 
552 	p_filters = p_llh_info->pp_filters[ppfid];
553 	if (!p_filters[filter_idx].ref_cnt) {
554 		p_filters[filter_idx].b_enabled = true;
555 		p_filters[filter_idx].type = type;
556 		memcpy(&p_filters[filter_idx].filter, p_filter,
557 		       sizeof(p_filters[filter_idx].filter));
558 	}
559 
560 	*p_ref_cnt = ++p_filters[filter_idx].ref_cnt;
561 
562 	return 0;
563 }
564 
565 static int
566 qed_llh_shadow_add_filter(struct qed_dev *cdev,
567 			  u8 ppfid,
568 			  enum qed_llh_filter_type type,
569 			  union qed_llh_filter *p_filter,
570 			  u8 *p_filter_idx, u32 *p_ref_cnt)
571 {
572 	int rc;
573 
574 	/* Check if the same filter already exist */
575 	rc = qed_llh_shadow_search_filter(cdev, ppfid, p_filter, p_filter_idx);
576 	if (rc)
577 		return rc;
578 
579 	/* Find a new entry in case of a new filter */
580 	if (*p_filter_idx == QED_LLH_INVALID_FILTER_IDX) {
581 		rc = qed_llh_shadow_get_free_idx(cdev, ppfid, p_filter_idx);
582 		if (rc)
583 			return rc;
584 	}
585 
586 	/* No free entry was found */
587 	if (*p_filter_idx == QED_LLH_INVALID_FILTER_IDX) {
588 		DP_NOTICE(cdev,
589 			  "Failed to find an empty LLH filter to utilize [ppfid %d]\n",
590 			  ppfid);
591 		return -EINVAL;
592 	}
593 
594 	return __qed_llh_shadow_add_filter(cdev, ppfid, *p_filter_idx, type,
595 					   p_filter, p_ref_cnt);
596 }
597 
598 static int
599 __qed_llh_shadow_remove_filter(struct qed_dev *cdev,
600 			       u8 ppfid, u8 filter_idx, u32 *p_ref_cnt)
601 {
602 	struct qed_llh_info *p_llh_info = cdev->p_llh_info;
603 	struct qed_llh_filter_info *p_filters;
604 	int rc;
605 
606 	rc = qed_llh_shadow_sanity(cdev, ppfid, filter_idx, "remove");
607 	if (rc)
608 		return rc;
609 
610 	p_filters = p_llh_info->pp_filters[ppfid];
611 	if (!p_filters[filter_idx].ref_cnt) {
612 		DP_NOTICE(cdev,
613 			  "LLH shadow: trying to remove a filter with ref_cnt=0\n");
614 		return -EINVAL;
615 	}
616 
617 	*p_ref_cnt = --p_filters[filter_idx].ref_cnt;
618 	if (!p_filters[filter_idx].ref_cnt)
619 		memset(&p_filters[filter_idx],
620 		       0, sizeof(p_filters[filter_idx]));
621 
622 	return 0;
623 }
624 
625 static int
626 qed_llh_shadow_remove_filter(struct qed_dev *cdev,
627 			     u8 ppfid,
628 			     union qed_llh_filter *p_filter,
629 			     u8 *p_filter_idx, u32 *p_ref_cnt)
630 {
631 	int rc;
632 
633 	rc = qed_llh_shadow_search_filter(cdev, ppfid, p_filter, p_filter_idx);
634 	if (rc)
635 		return rc;
636 
637 	/* No matching filter was found */
638 	if (*p_filter_idx == QED_LLH_INVALID_FILTER_IDX) {
639 		DP_NOTICE(cdev, "Failed to find a filter in the LLH shadow\n");
640 		return -EINVAL;
641 	}
642 
643 	return __qed_llh_shadow_remove_filter(cdev, ppfid, *p_filter_idx,
644 					      p_ref_cnt);
645 }
646 
647 static int qed_llh_abs_ppfid(struct qed_dev *cdev, u8 ppfid, u8 *p_abs_ppfid)
648 {
649 	struct qed_llh_info *p_llh_info = cdev->p_llh_info;
650 
651 	if (ppfid >= p_llh_info->num_ppfid) {
652 		DP_NOTICE(cdev,
653 			  "ppfid %d is not valid, available indices are 0..%hhd\n",
654 			  ppfid, p_llh_info->num_ppfid - 1);
655 		return -EINVAL;
656 	}
657 
658 	*p_abs_ppfid = p_llh_info->ppfid_array[ppfid];
659 
660 	return 0;
661 }
662 
663 static int
664 qed_llh_set_engine_affin(struct qed_hwfn *p_hwfn, struct qed_ptt *p_ptt)
665 {
666 	struct qed_dev *cdev = p_hwfn->cdev;
667 	enum qed_eng eng;
668 	u8 ppfid;
669 	int rc;
670 
671 	rc = qed_mcp_get_engine_config(p_hwfn, p_ptt);
672 	if (rc != 0 && rc != -EOPNOTSUPP) {
673 		DP_NOTICE(p_hwfn,
674 			  "Failed to get the engine affinity configuration\n");
675 		return rc;
676 	}
677 
678 	/* RoCE PF is bound to a single engine */
679 	if (QED_IS_ROCE_PERSONALITY(p_hwfn)) {
680 		eng = cdev->fir_affin ? QED_ENG1 : QED_ENG0;
681 		rc = qed_llh_set_roce_affinity(cdev, eng);
682 		if (rc) {
683 			DP_NOTICE(cdev,
684 				  "Failed to set the RoCE engine affinity\n");
685 			return rc;
686 		}
687 
688 		DP_VERBOSE(cdev,
689 			   QED_MSG_SP,
690 			   "LLH: Set the engine affinity of RoCE packets as %d\n",
691 			   eng);
692 	}
693 
694 	/* Storage PF is bound to a single engine while L2 PF uses both */
695 	if (QED_IS_FCOE_PERSONALITY(p_hwfn) || QED_IS_ISCSI_PERSONALITY(p_hwfn))
696 		eng = cdev->fir_affin ? QED_ENG1 : QED_ENG0;
697 	else			/* L2_PERSONALITY */
698 		eng = QED_BOTH_ENG;
699 
700 	for (ppfid = 0; ppfid < cdev->p_llh_info->num_ppfid; ppfid++) {
701 		rc = qed_llh_set_ppfid_affinity(cdev, ppfid, eng);
702 		if (rc) {
703 			DP_NOTICE(cdev,
704 				  "Failed to set the engine affinity of ppfid %d\n",
705 				  ppfid);
706 			return rc;
707 		}
708 	}
709 
710 	DP_VERBOSE(cdev, QED_MSG_SP,
711 		   "LLH: Set the engine affinity of non-RoCE packets as %d\n",
712 		   eng);
713 
714 	return 0;
715 }
716 
717 static int qed_llh_hw_init_pf(struct qed_hwfn *p_hwfn,
718 			      struct qed_ptt *p_ptt)
719 {
720 	struct qed_dev *cdev = p_hwfn->cdev;
721 	u8 ppfid, abs_ppfid;
722 	int rc;
723 
724 	for (ppfid = 0; ppfid < cdev->p_llh_info->num_ppfid; ppfid++) {
725 		u32 addr;
726 
727 		rc = qed_llh_abs_ppfid(cdev, ppfid, &abs_ppfid);
728 		if (rc)
729 			return rc;
730 
731 		addr = NIG_REG_LLH_PPFID2PFID_TBL_0 + abs_ppfid * 0x4;
732 		qed_wr(p_hwfn, p_ptt, addr, p_hwfn->rel_pf_id);
733 	}
734 
735 	if (test_bit(QED_MF_LLH_MAC_CLSS, &cdev->mf_bits) &&
736 	    !QED_IS_FCOE_PERSONALITY(p_hwfn)) {
737 		rc = qed_llh_add_mac_filter(cdev, 0,
738 					    p_hwfn->hw_info.hw_mac_addr);
739 		if (rc)
740 			DP_NOTICE(cdev,
741 				  "Failed to add an LLH filter with the primary MAC\n");
742 	}
743 
744 	if (QED_IS_CMT(cdev)) {
745 		rc = qed_llh_set_engine_affin(p_hwfn, p_ptt);
746 		if (rc)
747 			return rc;
748 	}
749 
750 	return 0;
751 }
752 
753 u8 qed_llh_get_num_ppfid(struct qed_dev *cdev)
754 {
755 	return cdev->p_llh_info->num_ppfid;
756 }
757 
758 #define NIG_REG_PPF_TO_ENGINE_SEL_ROCE_MASK             0x3
759 #define NIG_REG_PPF_TO_ENGINE_SEL_ROCE_SHIFT            0
760 #define NIG_REG_PPF_TO_ENGINE_SEL_NON_ROCE_MASK         0x3
761 #define NIG_REG_PPF_TO_ENGINE_SEL_NON_ROCE_SHIFT        2
762 
763 int qed_llh_set_ppfid_affinity(struct qed_dev *cdev, u8 ppfid, enum qed_eng eng)
764 {
765 	struct qed_hwfn *p_hwfn = QED_LEADING_HWFN(cdev);
766 	struct qed_ptt *p_ptt = qed_ptt_acquire(p_hwfn);
767 	u32 addr, val, eng_sel;
768 	u8 abs_ppfid;
769 	int rc = 0;
770 
771 	if (!p_ptt)
772 		return -EAGAIN;
773 
774 	if (!QED_IS_CMT(cdev))
775 		goto out;
776 
777 	rc = qed_llh_abs_ppfid(cdev, ppfid, &abs_ppfid);
778 	if (rc)
779 		goto out;
780 
781 	switch (eng) {
782 	case QED_ENG0:
783 		eng_sel = 0;
784 		break;
785 	case QED_ENG1:
786 		eng_sel = 1;
787 		break;
788 	case QED_BOTH_ENG:
789 		eng_sel = 2;
790 		break;
791 	default:
792 		DP_NOTICE(cdev, "Invalid affinity value for ppfid [%d]\n", eng);
793 		rc = -EINVAL;
794 		goto out;
795 	}
796 
797 	addr = NIG_REG_PPF_TO_ENGINE_SEL + abs_ppfid * 0x4;
798 	val = qed_rd(p_hwfn, p_ptt, addr);
799 	SET_FIELD(val, NIG_REG_PPF_TO_ENGINE_SEL_NON_ROCE, eng_sel);
800 	qed_wr(p_hwfn, p_ptt, addr, val);
801 
802 	/* The iWARP affinity is set as the affinity of ppfid 0 */
803 	if (!ppfid && QED_IS_IWARP_PERSONALITY(p_hwfn))
804 		cdev->iwarp_affin = (eng == QED_ENG1) ? 1 : 0;
805 out:
806 	qed_ptt_release(p_hwfn, p_ptt);
807 
808 	return rc;
809 }
810 
811 int qed_llh_set_roce_affinity(struct qed_dev *cdev, enum qed_eng eng)
812 {
813 	struct qed_hwfn *p_hwfn = QED_LEADING_HWFN(cdev);
814 	struct qed_ptt *p_ptt = qed_ptt_acquire(p_hwfn);
815 	u32 addr, val, eng_sel;
816 	u8 ppfid, abs_ppfid;
817 	int rc = 0;
818 
819 	if (!p_ptt)
820 		return -EAGAIN;
821 
822 	if (!QED_IS_CMT(cdev))
823 		goto out;
824 
825 	switch (eng) {
826 	case QED_ENG0:
827 		eng_sel = 0;
828 		break;
829 	case QED_ENG1:
830 		eng_sel = 1;
831 		break;
832 	case QED_BOTH_ENG:
833 		eng_sel = 2;
834 		qed_wr(p_hwfn, p_ptt, NIG_REG_LLH_ENG_CLS_ROCE_QP_SEL,
835 		       0xf);  /* QP bit 15 */
836 		break;
837 	default:
838 		DP_NOTICE(cdev, "Invalid affinity value for RoCE [%d]\n", eng);
839 		rc = -EINVAL;
840 		goto out;
841 	}
842 
843 	for (ppfid = 0; ppfid < cdev->p_llh_info->num_ppfid; ppfid++) {
844 		rc = qed_llh_abs_ppfid(cdev, ppfid, &abs_ppfid);
845 		if (rc)
846 			goto out;
847 
848 		addr = NIG_REG_PPF_TO_ENGINE_SEL + abs_ppfid * 0x4;
849 		val = qed_rd(p_hwfn, p_ptt, addr);
850 		SET_FIELD(val, NIG_REG_PPF_TO_ENGINE_SEL_ROCE, eng_sel);
851 		qed_wr(p_hwfn, p_ptt, addr, val);
852 	}
853 out:
854 	qed_ptt_release(p_hwfn, p_ptt);
855 
856 	return rc;
857 }
858 
859 struct qed_llh_filter_details {
860 	u64 value;
861 	u32 mode;
862 	u32 protocol_type;
863 	u32 hdr_sel;
864 	u32 enable;
865 };
866 
867 static int
868 qed_llh_access_filter(struct qed_hwfn *p_hwfn,
869 		      struct qed_ptt *p_ptt,
870 		      u8 abs_ppfid,
871 		      u8 filter_idx,
872 		      struct qed_llh_filter_details *p_details)
873 {
874 	struct qed_dmae_params params = {0};
875 	u32 addr;
876 	u8 pfid;
877 	int rc;
878 
879 	/* The NIG/LLH registers that are accessed in this function have only 16
880 	 * rows which are exposed to a PF. I.e. only the 16 filters of its
881 	 * default ppfid. Accessing filters of other ppfids requires pretending
882 	 * to another PFs.
883 	 * The calculation of PPFID->PFID in AH is based on the relative index
884 	 * of a PF on its port.
885 	 * For BB the pfid is actually the abs_ppfid.
886 	 */
887 	if (QED_IS_BB(p_hwfn->cdev))
888 		pfid = abs_ppfid;
889 	else
890 		pfid = abs_ppfid * p_hwfn->cdev->num_ports_in_engine +
891 		    MFW_PORT(p_hwfn);
892 
893 	/* Filter enable - should be done first when removing a filter */
894 	if (!p_details->enable) {
895 		qed_fid_pretend(p_hwfn, p_ptt,
896 				pfid << PXP_PRETEND_CONCRETE_FID_PFID_SHIFT);
897 
898 		addr = NIG_REG_LLH_FUNC_FILTER_EN + filter_idx * 0x4;
899 		qed_wr(p_hwfn, p_ptt, addr, p_details->enable);
900 
901 		qed_fid_pretend(p_hwfn, p_ptt,
902 				p_hwfn->rel_pf_id <<
903 				PXP_PRETEND_CONCRETE_FID_PFID_SHIFT);
904 	}
905 
906 	/* Filter value */
907 	addr = NIG_REG_LLH_FUNC_FILTER_VALUE + 2 * filter_idx * 0x4;
908 
909 	params.flags = QED_DMAE_FLAG_PF_DST;
910 	params.dst_pfid = pfid;
911 	rc = qed_dmae_host2grc(p_hwfn,
912 			       p_ptt,
913 			       (u64)(uintptr_t)&p_details->value,
914 			       addr, 2 /* size_in_dwords */,
915 			       &params);
916 	if (rc)
917 		return rc;
918 
919 	qed_fid_pretend(p_hwfn, p_ptt,
920 			pfid << PXP_PRETEND_CONCRETE_FID_PFID_SHIFT);
921 
922 	/* Filter mode */
923 	addr = NIG_REG_LLH_FUNC_FILTER_MODE + filter_idx * 0x4;
924 	qed_wr(p_hwfn, p_ptt, addr, p_details->mode);
925 
926 	/* Filter protocol type */
927 	addr = NIG_REG_LLH_FUNC_FILTER_PROTOCOL_TYPE + filter_idx * 0x4;
928 	qed_wr(p_hwfn, p_ptt, addr, p_details->protocol_type);
929 
930 	/* Filter header select */
931 	addr = NIG_REG_LLH_FUNC_FILTER_HDR_SEL + filter_idx * 0x4;
932 	qed_wr(p_hwfn, p_ptt, addr, p_details->hdr_sel);
933 
934 	/* Filter enable - should be done last when adding a filter */
935 	if (p_details->enable) {
936 		addr = NIG_REG_LLH_FUNC_FILTER_EN + filter_idx * 0x4;
937 		qed_wr(p_hwfn, p_ptt, addr, p_details->enable);
938 	}
939 
940 	qed_fid_pretend(p_hwfn, p_ptt,
941 			p_hwfn->rel_pf_id <<
942 			PXP_PRETEND_CONCRETE_FID_PFID_SHIFT);
943 
944 	return 0;
945 }
946 
947 static int
948 qed_llh_add_filter(struct qed_hwfn *p_hwfn,
949 		   struct qed_ptt *p_ptt,
950 		   u8 abs_ppfid,
951 		   u8 filter_idx, u8 filter_prot_type, u32 high, u32 low)
952 {
953 	struct qed_llh_filter_details filter_details;
954 
955 	filter_details.enable = 1;
956 	filter_details.value = ((u64)high << 32) | low;
957 	filter_details.hdr_sel = 0;
958 	filter_details.protocol_type = filter_prot_type;
959 	/* Mode: 0: MAC-address classification 1: protocol classification */
960 	filter_details.mode = filter_prot_type ? 1 : 0;
961 
962 	return qed_llh_access_filter(p_hwfn, p_ptt, abs_ppfid, filter_idx,
963 				     &filter_details);
964 }
965 
966 static int
967 qed_llh_remove_filter(struct qed_hwfn *p_hwfn,
968 		      struct qed_ptt *p_ptt, u8 abs_ppfid, u8 filter_idx)
969 {
970 	struct qed_llh_filter_details filter_details = {0};
971 
972 	return qed_llh_access_filter(p_hwfn, p_ptt, abs_ppfid, filter_idx,
973 				     &filter_details);
974 }
975 
976 int qed_llh_add_mac_filter(struct qed_dev *cdev,
977 			   u8 ppfid, u8 mac_addr[ETH_ALEN])
978 {
979 	struct qed_hwfn *p_hwfn = QED_LEADING_HWFN(cdev);
980 	struct qed_ptt *p_ptt = qed_ptt_acquire(p_hwfn);
981 	union qed_llh_filter filter = {};
982 	u8 filter_idx, abs_ppfid;
983 	u32 high, low, ref_cnt;
984 	int rc = 0;
985 
986 	if (!p_ptt)
987 		return -EAGAIN;
988 
989 	if (!test_bit(QED_MF_LLH_MAC_CLSS, &cdev->mf_bits))
990 		goto out;
991 
992 	memcpy(filter.mac.addr, mac_addr, ETH_ALEN);
993 	rc = qed_llh_shadow_add_filter(cdev, ppfid,
994 				       QED_LLH_FILTER_TYPE_MAC,
995 				       &filter, &filter_idx, &ref_cnt);
996 	if (rc)
997 		goto err;
998 
999 	/* Configure the LLH only in case of a new the filter */
1000 	if (ref_cnt == 1) {
1001 		rc = qed_llh_abs_ppfid(cdev, ppfid, &abs_ppfid);
1002 		if (rc)
1003 			goto err;
1004 
1005 		high = mac_addr[1] | (mac_addr[0] << 8);
1006 		low = mac_addr[5] | (mac_addr[4] << 8) | (mac_addr[3] << 16) |
1007 		      (mac_addr[2] << 24);
1008 		rc = qed_llh_add_filter(p_hwfn, p_ptt, abs_ppfid, filter_idx,
1009 					0, high, low);
1010 		if (rc)
1011 			goto err;
1012 	}
1013 
1014 	DP_VERBOSE(cdev,
1015 		   QED_MSG_SP,
1016 		   "LLH: Added MAC filter [%pM] to ppfid %hhd [abs %hhd] at idx %hhd [ref_cnt %d]\n",
1017 		   mac_addr, ppfid, abs_ppfid, filter_idx, ref_cnt);
1018 
1019 	goto out;
1020 
1021 err:	DP_NOTICE(cdev,
1022 		  "LLH: Failed to add MAC filter [%pM] to ppfid %hhd\n",
1023 		  mac_addr, ppfid);
1024 out:
1025 	qed_ptt_release(p_hwfn, p_ptt);
1026 
1027 	return rc;
1028 }
1029 
1030 static int
1031 qed_llh_protocol_filter_stringify(struct qed_dev *cdev,
1032 				  enum qed_llh_prot_filter_type_t type,
1033 				  u16 source_port_or_eth_type,
1034 				  u16 dest_port, u8 *str, size_t str_len)
1035 {
1036 	switch (type) {
1037 	case QED_LLH_FILTER_ETHERTYPE:
1038 		snprintf(str, str_len, "Ethertype 0x%04x",
1039 			 source_port_or_eth_type);
1040 		break;
1041 	case QED_LLH_FILTER_TCP_SRC_PORT:
1042 		snprintf(str, str_len, "TCP src port 0x%04x",
1043 			 source_port_or_eth_type);
1044 		break;
1045 	case QED_LLH_FILTER_UDP_SRC_PORT:
1046 		snprintf(str, str_len, "UDP src port 0x%04x",
1047 			 source_port_or_eth_type);
1048 		break;
1049 	case QED_LLH_FILTER_TCP_DEST_PORT:
1050 		snprintf(str, str_len, "TCP dst port 0x%04x", dest_port);
1051 		break;
1052 	case QED_LLH_FILTER_UDP_DEST_PORT:
1053 		snprintf(str, str_len, "UDP dst port 0x%04x", dest_port);
1054 		break;
1055 	case QED_LLH_FILTER_TCP_SRC_AND_DEST_PORT:
1056 		snprintf(str, str_len, "TCP src/dst ports 0x%04x/0x%04x",
1057 			 source_port_or_eth_type, dest_port);
1058 		break;
1059 	case QED_LLH_FILTER_UDP_SRC_AND_DEST_PORT:
1060 		snprintf(str, str_len, "UDP src/dst ports 0x%04x/0x%04x",
1061 			 source_port_or_eth_type, dest_port);
1062 		break;
1063 	default:
1064 		DP_NOTICE(cdev,
1065 			  "Non valid LLH protocol filter type %d\n", type);
1066 		return -EINVAL;
1067 	}
1068 
1069 	return 0;
1070 }
1071 
1072 static int
1073 qed_llh_protocol_filter_to_hilo(struct qed_dev *cdev,
1074 				enum qed_llh_prot_filter_type_t type,
1075 				u16 source_port_or_eth_type,
1076 				u16 dest_port, u32 *p_high, u32 *p_low)
1077 {
1078 	*p_high = 0;
1079 	*p_low = 0;
1080 
1081 	switch (type) {
1082 	case QED_LLH_FILTER_ETHERTYPE:
1083 		*p_high = source_port_or_eth_type;
1084 		break;
1085 	case QED_LLH_FILTER_TCP_SRC_PORT:
1086 	case QED_LLH_FILTER_UDP_SRC_PORT:
1087 		*p_low = source_port_or_eth_type << 16;
1088 		break;
1089 	case QED_LLH_FILTER_TCP_DEST_PORT:
1090 	case QED_LLH_FILTER_UDP_DEST_PORT:
1091 		*p_low = dest_port;
1092 		break;
1093 	case QED_LLH_FILTER_TCP_SRC_AND_DEST_PORT:
1094 	case QED_LLH_FILTER_UDP_SRC_AND_DEST_PORT:
1095 		*p_low = (source_port_or_eth_type << 16) | dest_port;
1096 		break;
1097 	default:
1098 		DP_NOTICE(cdev,
1099 			  "Non valid LLH protocol filter type %d\n", type);
1100 		return -EINVAL;
1101 	}
1102 
1103 	return 0;
1104 }
1105 
1106 int
1107 qed_llh_add_protocol_filter(struct qed_dev *cdev,
1108 			    u8 ppfid,
1109 			    enum qed_llh_prot_filter_type_t type,
1110 			    u16 source_port_or_eth_type, u16 dest_port)
1111 {
1112 	struct qed_hwfn *p_hwfn = QED_LEADING_HWFN(cdev);
1113 	struct qed_ptt *p_ptt = qed_ptt_acquire(p_hwfn);
1114 	u8 filter_idx, abs_ppfid, str[32], type_bitmap;
1115 	union qed_llh_filter filter = {};
1116 	u32 high, low, ref_cnt;
1117 	int rc = 0;
1118 
1119 	if (!p_ptt)
1120 		return -EAGAIN;
1121 
1122 	if (!test_bit(QED_MF_LLH_PROTO_CLSS, &cdev->mf_bits))
1123 		goto out;
1124 
1125 	rc = qed_llh_protocol_filter_stringify(cdev, type,
1126 					       source_port_or_eth_type,
1127 					       dest_port, str, sizeof(str));
1128 	if (rc)
1129 		goto err;
1130 
1131 	filter.protocol.type = type;
1132 	filter.protocol.source_port_or_eth_type = source_port_or_eth_type;
1133 	filter.protocol.dest_port = dest_port;
1134 	rc = qed_llh_shadow_add_filter(cdev,
1135 				       ppfid,
1136 				       QED_LLH_FILTER_TYPE_PROTOCOL,
1137 				       &filter, &filter_idx, &ref_cnt);
1138 	if (rc)
1139 		goto err;
1140 
1141 	rc = qed_llh_abs_ppfid(cdev, ppfid, &abs_ppfid);
1142 	if (rc)
1143 		goto err;
1144 
1145 	/* Configure the LLH only in case of a new the filter */
1146 	if (ref_cnt == 1) {
1147 		rc = qed_llh_protocol_filter_to_hilo(cdev, type,
1148 						     source_port_or_eth_type,
1149 						     dest_port, &high, &low);
1150 		if (rc)
1151 			goto err;
1152 
1153 		type_bitmap = 0x1 << type;
1154 		rc = qed_llh_add_filter(p_hwfn, p_ptt, abs_ppfid,
1155 					filter_idx, type_bitmap, high, low);
1156 		if (rc)
1157 			goto err;
1158 	}
1159 
1160 	DP_VERBOSE(cdev,
1161 		   QED_MSG_SP,
1162 		   "LLH: Added protocol filter [%s] to ppfid %hhd [abs %hhd] at idx %hhd [ref_cnt %d]\n",
1163 		   str, ppfid, abs_ppfid, filter_idx, ref_cnt);
1164 
1165 	goto out;
1166 
1167 err:	DP_NOTICE(p_hwfn,
1168 		  "LLH: Failed to add protocol filter [%s] to ppfid %hhd\n",
1169 		  str, ppfid);
1170 out:
1171 	qed_ptt_release(p_hwfn, p_ptt);
1172 
1173 	return rc;
1174 }
1175 
1176 void qed_llh_remove_mac_filter(struct qed_dev *cdev,
1177 			       u8 ppfid, u8 mac_addr[ETH_ALEN])
1178 {
1179 	struct qed_hwfn *p_hwfn = QED_LEADING_HWFN(cdev);
1180 	struct qed_ptt *p_ptt = qed_ptt_acquire(p_hwfn);
1181 	union qed_llh_filter filter = {};
1182 	u8 filter_idx, abs_ppfid;
1183 	int rc = 0;
1184 	u32 ref_cnt;
1185 
1186 	if (!p_ptt)
1187 		return;
1188 
1189 	if (!test_bit(QED_MF_LLH_MAC_CLSS, &cdev->mf_bits))
1190 		goto out;
1191 
1192 	ether_addr_copy(filter.mac.addr, mac_addr);
1193 	rc = qed_llh_shadow_remove_filter(cdev, ppfid, &filter, &filter_idx,
1194 					  &ref_cnt);
1195 	if (rc)
1196 		goto err;
1197 
1198 	rc = qed_llh_abs_ppfid(cdev, ppfid, &abs_ppfid);
1199 	if (rc)
1200 		goto err;
1201 
1202 	/* Remove from the LLH in case the filter is not in use */
1203 	if (!ref_cnt) {
1204 		rc = qed_llh_remove_filter(p_hwfn, p_ptt, abs_ppfid,
1205 					   filter_idx);
1206 		if (rc)
1207 			goto err;
1208 	}
1209 
1210 	DP_VERBOSE(cdev,
1211 		   QED_MSG_SP,
1212 		   "LLH: Removed MAC filter [%pM] from ppfid %hhd [abs %hhd] at idx %hhd [ref_cnt %d]\n",
1213 		   mac_addr, ppfid, abs_ppfid, filter_idx, ref_cnt);
1214 
1215 	goto out;
1216 
1217 err:	DP_NOTICE(cdev,
1218 		  "LLH: Failed to remove MAC filter [%pM] from ppfid %hhd\n",
1219 		  mac_addr, ppfid);
1220 out:
1221 	qed_ptt_release(p_hwfn, p_ptt);
1222 }
1223 
1224 void qed_llh_remove_protocol_filter(struct qed_dev *cdev,
1225 				    u8 ppfid,
1226 				    enum qed_llh_prot_filter_type_t type,
1227 				    u16 source_port_or_eth_type, u16 dest_port)
1228 {
1229 	struct qed_hwfn *p_hwfn = QED_LEADING_HWFN(cdev);
1230 	struct qed_ptt *p_ptt = qed_ptt_acquire(p_hwfn);
1231 	u8 filter_idx, abs_ppfid, str[32];
1232 	union qed_llh_filter filter = {};
1233 	int rc = 0;
1234 	u32 ref_cnt;
1235 
1236 	if (!p_ptt)
1237 		return;
1238 
1239 	if (!test_bit(QED_MF_LLH_PROTO_CLSS, &cdev->mf_bits))
1240 		goto out;
1241 
1242 	rc = qed_llh_protocol_filter_stringify(cdev, type,
1243 					       source_port_or_eth_type,
1244 					       dest_port, str, sizeof(str));
1245 	if (rc)
1246 		goto err;
1247 
1248 	filter.protocol.type = type;
1249 	filter.protocol.source_port_or_eth_type = source_port_or_eth_type;
1250 	filter.protocol.dest_port = dest_port;
1251 	rc = qed_llh_shadow_remove_filter(cdev, ppfid, &filter, &filter_idx,
1252 					  &ref_cnt);
1253 	if (rc)
1254 		goto err;
1255 
1256 	rc = qed_llh_abs_ppfid(cdev, ppfid, &abs_ppfid);
1257 	if (rc)
1258 		goto err;
1259 
1260 	/* Remove from the LLH in case the filter is not in use */
1261 	if (!ref_cnt) {
1262 		rc = qed_llh_remove_filter(p_hwfn, p_ptt, abs_ppfid,
1263 					   filter_idx);
1264 		if (rc)
1265 			goto err;
1266 	}
1267 
1268 	DP_VERBOSE(cdev,
1269 		   QED_MSG_SP,
1270 		   "LLH: Removed protocol filter [%s] from ppfid %hhd [abs %hhd] at idx %hhd [ref_cnt %d]\n",
1271 		   str, ppfid, abs_ppfid, filter_idx, ref_cnt);
1272 
1273 	goto out;
1274 
1275 err:	DP_NOTICE(cdev,
1276 		  "LLH: Failed to remove protocol filter [%s] from ppfid %hhd\n",
1277 		  str, ppfid);
1278 out:
1279 	qed_ptt_release(p_hwfn, p_ptt);
1280 }
1281 
1282 /******************************* NIG LLH - End ********************************/
1283 
1284 #define QED_MIN_DPIS            (4)
1285 #define QED_MIN_PWM_REGION      (QED_WID_SIZE * QED_MIN_DPIS)
1286 
1287 static u32 qed_hw_bar_size(struct qed_hwfn *p_hwfn,
1288 			   struct qed_ptt *p_ptt, enum BAR_ID bar_id)
1289 {
1290 	u32 bar_reg = (bar_id == BAR_ID_0 ?
1291 		       PGLUE_B_REG_PF_BAR0_SIZE : PGLUE_B_REG_PF_BAR1_SIZE);
1292 	u32 val;
1293 
1294 	if (IS_VF(p_hwfn->cdev))
1295 		return qed_vf_hw_bar_size(p_hwfn, bar_id);
1296 
1297 	val = qed_rd(p_hwfn, p_ptt, bar_reg);
1298 	if (val)
1299 		return 1 << (val + 15);
1300 
1301 	/* Old MFW initialized above registered only conditionally */
1302 	if (p_hwfn->cdev->num_hwfns > 1) {
1303 		DP_INFO(p_hwfn,
1304 			"BAR size not configured. Assuming BAR size of 256kB for GRC and 512kB for DB\n");
1305 			return BAR_ID_0 ? 256 * 1024 : 512 * 1024;
1306 	} else {
1307 		DP_INFO(p_hwfn,
1308 			"BAR size not configured. Assuming BAR size of 512kB for GRC and 512kB for DB\n");
1309 			return 512 * 1024;
1310 	}
1311 }
1312 
1313 void qed_init_dp(struct qed_dev *cdev, u32 dp_module, u8 dp_level)
1314 {
1315 	u32 i;
1316 
1317 	cdev->dp_level = dp_level;
1318 	cdev->dp_module = dp_module;
1319 	for (i = 0; i < MAX_HWFNS_PER_DEVICE; i++) {
1320 		struct qed_hwfn *p_hwfn = &cdev->hwfns[i];
1321 
1322 		p_hwfn->dp_level = dp_level;
1323 		p_hwfn->dp_module = dp_module;
1324 	}
1325 }
1326 
1327 void qed_init_struct(struct qed_dev *cdev)
1328 {
1329 	u8 i;
1330 
1331 	for (i = 0; i < MAX_HWFNS_PER_DEVICE; i++) {
1332 		struct qed_hwfn *p_hwfn = &cdev->hwfns[i];
1333 
1334 		p_hwfn->cdev = cdev;
1335 		p_hwfn->my_id = i;
1336 		p_hwfn->b_active = false;
1337 
1338 		mutex_init(&p_hwfn->dmae_info.mutex);
1339 	}
1340 
1341 	/* hwfn 0 is always active */
1342 	cdev->hwfns[0].b_active = true;
1343 
1344 	/* set the default cache alignment to 128 */
1345 	cdev->cache_shift = 7;
1346 }
1347 
1348 static void qed_qm_info_free(struct qed_hwfn *p_hwfn)
1349 {
1350 	struct qed_qm_info *qm_info = &p_hwfn->qm_info;
1351 
1352 	kfree(qm_info->qm_pq_params);
1353 	qm_info->qm_pq_params = NULL;
1354 	kfree(qm_info->qm_vport_params);
1355 	qm_info->qm_vport_params = NULL;
1356 	kfree(qm_info->qm_port_params);
1357 	qm_info->qm_port_params = NULL;
1358 	kfree(qm_info->wfq_data);
1359 	qm_info->wfq_data = NULL;
1360 }
1361 
1362 static void qed_dbg_user_data_free(struct qed_hwfn *p_hwfn)
1363 {
1364 	kfree(p_hwfn->dbg_user_info);
1365 	p_hwfn->dbg_user_info = NULL;
1366 }
1367 
1368 void qed_resc_free(struct qed_dev *cdev)
1369 {
1370 	int i;
1371 
1372 	if (IS_VF(cdev)) {
1373 		for_each_hwfn(cdev, i)
1374 			qed_l2_free(&cdev->hwfns[i]);
1375 		return;
1376 	}
1377 
1378 	kfree(cdev->fw_data);
1379 	cdev->fw_data = NULL;
1380 
1381 	kfree(cdev->reset_stats);
1382 	cdev->reset_stats = NULL;
1383 
1384 	qed_llh_free(cdev);
1385 
1386 	for_each_hwfn(cdev, i) {
1387 		struct qed_hwfn *p_hwfn = &cdev->hwfns[i];
1388 
1389 		qed_cxt_mngr_free(p_hwfn);
1390 		qed_qm_info_free(p_hwfn);
1391 		qed_spq_free(p_hwfn);
1392 		qed_eq_free(p_hwfn);
1393 		qed_consq_free(p_hwfn);
1394 		qed_int_free(p_hwfn);
1395 #ifdef CONFIG_QED_LL2
1396 		qed_ll2_free(p_hwfn);
1397 #endif
1398 		if (p_hwfn->hw_info.personality == QED_PCI_FCOE)
1399 			qed_fcoe_free(p_hwfn);
1400 
1401 		if (p_hwfn->hw_info.personality == QED_PCI_ISCSI) {
1402 			qed_iscsi_free(p_hwfn);
1403 			qed_ooo_free(p_hwfn);
1404 		}
1405 
1406 		if (QED_IS_RDMA_PERSONALITY(p_hwfn))
1407 			qed_rdma_info_free(p_hwfn);
1408 
1409 		qed_iov_free(p_hwfn);
1410 		qed_l2_free(p_hwfn);
1411 		qed_dmae_info_free(p_hwfn);
1412 		qed_dcbx_info_free(p_hwfn);
1413 		qed_dbg_user_data_free(p_hwfn);
1414 
1415 		/* Destroy doorbell recovery mechanism */
1416 		qed_db_recovery_teardown(p_hwfn);
1417 	}
1418 }
1419 
1420 /******************** QM initialization *******************/
1421 #define ACTIVE_TCS_BMAP 0x9f
1422 #define ACTIVE_TCS_BMAP_4PORT_K2 0xf
1423 
1424 /* determines the physical queue flags for a given PF. */
1425 static u32 qed_get_pq_flags(struct qed_hwfn *p_hwfn)
1426 {
1427 	u32 flags;
1428 
1429 	/* common flags */
1430 	flags = PQ_FLAGS_LB;
1431 
1432 	/* feature flags */
1433 	if (IS_QED_SRIOV(p_hwfn->cdev))
1434 		flags |= PQ_FLAGS_VFS;
1435 
1436 	/* protocol flags */
1437 	switch (p_hwfn->hw_info.personality) {
1438 	case QED_PCI_ETH:
1439 		flags |= PQ_FLAGS_MCOS;
1440 		break;
1441 	case QED_PCI_FCOE:
1442 		flags |= PQ_FLAGS_OFLD;
1443 		break;
1444 	case QED_PCI_ISCSI:
1445 		flags |= PQ_FLAGS_ACK | PQ_FLAGS_OOO | PQ_FLAGS_OFLD;
1446 		break;
1447 	case QED_PCI_ETH_ROCE:
1448 		flags |= PQ_FLAGS_MCOS | PQ_FLAGS_OFLD | PQ_FLAGS_LLT;
1449 		if (IS_QED_MULTI_TC_ROCE(p_hwfn))
1450 			flags |= PQ_FLAGS_MTC;
1451 		break;
1452 	case QED_PCI_ETH_IWARP:
1453 		flags |= PQ_FLAGS_MCOS | PQ_FLAGS_ACK | PQ_FLAGS_OOO |
1454 		    PQ_FLAGS_OFLD;
1455 		break;
1456 	default:
1457 		DP_ERR(p_hwfn,
1458 		       "unknown personality %d\n", p_hwfn->hw_info.personality);
1459 		return 0;
1460 	}
1461 
1462 	return flags;
1463 }
1464 
1465 /* Getters for resource amounts necessary for qm initialization */
1466 static u8 qed_init_qm_get_num_tcs(struct qed_hwfn *p_hwfn)
1467 {
1468 	return p_hwfn->hw_info.num_hw_tc;
1469 }
1470 
1471 static u16 qed_init_qm_get_num_vfs(struct qed_hwfn *p_hwfn)
1472 {
1473 	return IS_QED_SRIOV(p_hwfn->cdev) ?
1474 	       p_hwfn->cdev->p_iov_info->total_vfs : 0;
1475 }
1476 
1477 static u8 qed_init_qm_get_num_mtc_tcs(struct qed_hwfn *p_hwfn)
1478 {
1479 	u32 pq_flags = qed_get_pq_flags(p_hwfn);
1480 
1481 	if (!(PQ_FLAGS_MTC & pq_flags))
1482 		return 1;
1483 
1484 	return qed_init_qm_get_num_tcs(p_hwfn);
1485 }
1486 
1487 #define NUM_DEFAULT_RLS 1
1488 
1489 static u16 qed_init_qm_get_num_pf_rls(struct qed_hwfn *p_hwfn)
1490 {
1491 	u16 num_pf_rls, num_vfs = qed_init_qm_get_num_vfs(p_hwfn);
1492 
1493 	/* num RLs can't exceed resource amount of rls or vports */
1494 	num_pf_rls = (u16) min_t(u32, RESC_NUM(p_hwfn, QED_RL),
1495 				 RESC_NUM(p_hwfn, QED_VPORT));
1496 
1497 	/* Make sure after we reserve there's something left */
1498 	if (num_pf_rls < num_vfs + NUM_DEFAULT_RLS)
1499 		return 0;
1500 
1501 	/* subtract rls necessary for VFs and one default one for the PF */
1502 	num_pf_rls -= num_vfs + NUM_DEFAULT_RLS;
1503 
1504 	return num_pf_rls;
1505 }
1506 
1507 static u16 qed_init_qm_get_num_vports(struct qed_hwfn *p_hwfn)
1508 {
1509 	u32 pq_flags = qed_get_pq_flags(p_hwfn);
1510 
1511 	/* all pqs share the same vport, except for vfs and pf_rl pqs */
1512 	return (!!(PQ_FLAGS_RLS & pq_flags)) *
1513 	       qed_init_qm_get_num_pf_rls(p_hwfn) +
1514 	       (!!(PQ_FLAGS_VFS & pq_flags)) *
1515 	       qed_init_qm_get_num_vfs(p_hwfn) + 1;
1516 }
1517 
1518 /* calc amount of PQs according to the requested flags */
1519 static u16 qed_init_qm_get_num_pqs(struct qed_hwfn *p_hwfn)
1520 {
1521 	u32 pq_flags = qed_get_pq_flags(p_hwfn);
1522 
1523 	return (!!(PQ_FLAGS_RLS & pq_flags)) *
1524 	       qed_init_qm_get_num_pf_rls(p_hwfn) +
1525 	       (!!(PQ_FLAGS_MCOS & pq_flags)) *
1526 	       qed_init_qm_get_num_tcs(p_hwfn) +
1527 	       (!!(PQ_FLAGS_LB & pq_flags)) + (!!(PQ_FLAGS_OOO & pq_flags)) +
1528 	       (!!(PQ_FLAGS_ACK & pq_flags)) +
1529 	       (!!(PQ_FLAGS_OFLD & pq_flags)) *
1530 	       qed_init_qm_get_num_mtc_tcs(p_hwfn) +
1531 	       (!!(PQ_FLAGS_LLT & pq_flags)) *
1532 	       qed_init_qm_get_num_mtc_tcs(p_hwfn) +
1533 	       (!!(PQ_FLAGS_VFS & pq_flags)) * qed_init_qm_get_num_vfs(p_hwfn);
1534 }
1535 
1536 /* initialize the top level QM params */
1537 static void qed_init_qm_params(struct qed_hwfn *p_hwfn)
1538 {
1539 	struct qed_qm_info *qm_info = &p_hwfn->qm_info;
1540 	bool four_port;
1541 
1542 	/* pq and vport bases for this PF */
1543 	qm_info->start_pq = (u16) RESC_START(p_hwfn, QED_PQ);
1544 	qm_info->start_vport = (u8) RESC_START(p_hwfn, QED_VPORT);
1545 
1546 	/* rate limiting and weighted fair queueing are always enabled */
1547 	qm_info->vport_rl_en = true;
1548 	qm_info->vport_wfq_en = true;
1549 
1550 	/* TC config is different for AH 4 port */
1551 	four_port = p_hwfn->cdev->num_ports_in_engine == MAX_NUM_PORTS_K2;
1552 
1553 	/* in AH 4 port we have fewer TCs per port */
1554 	qm_info->max_phys_tcs_per_port = four_port ? NUM_PHYS_TCS_4PORT_K2 :
1555 						     NUM_OF_PHYS_TCS;
1556 
1557 	/* unless MFW indicated otherwise, ooo_tc == 3 for
1558 	 * AH 4-port and 4 otherwise.
1559 	 */
1560 	if (!qm_info->ooo_tc)
1561 		qm_info->ooo_tc = four_port ? DCBX_TCP_OOO_K2_4PORT_TC :
1562 					      DCBX_TCP_OOO_TC;
1563 }
1564 
1565 /* initialize qm vport params */
1566 static void qed_init_qm_vport_params(struct qed_hwfn *p_hwfn)
1567 {
1568 	struct qed_qm_info *qm_info = &p_hwfn->qm_info;
1569 	u8 i;
1570 
1571 	/* all vports participate in weighted fair queueing */
1572 	for (i = 0; i < qed_init_qm_get_num_vports(p_hwfn); i++)
1573 		qm_info->qm_vport_params[i].vport_wfq = 1;
1574 }
1575 
1576 /* initialize qm port params */
1577 static void qed_init_qm_port_params(struct qed_hwfn *p_hwfn)
1578 {
1579 	/* Initialize qm port parameters */
1580 	u8 i, active_phys_tcs, num_ports = p_hwfn->cdev->num_ports_in_engine;
1581 
1582 	/* indicate how ooo and high pri traffic is dealt with */
1583 	active_phys_tcs = num_ports == MAX_NUM_PORTS_K2 ?
1584 			  ACTIVE_TCS_BMAP_4PORT_K2 :
1585 			  ACTIVE_TCS_BMAP;
1586 
1587 	for (i = 0; i < num_ports; i++) {
1588 		struct init_qm_port_params *p_qm_port =
1589 		    &p_hwfn->qm_info.qm_port_params[i];
1590 
1591 		p_qm_port->active = 1;
1592 		p_qm_port->active_phys_tcs = active_phys_tcs;
1593 		p_qm_port->num_pbf_cmd_lines = PBF_MAX_CMD_LINES / num_ports;
1594 		p_qm_port->num_btb_blocks = BTB_MAX_BLOCKS / num_ports;
1595 	}
1596 }
1597 
1598 /* Reset the params which must be reset for qm init. QM init may be called as
1599  * a result of flows other than driver load (e.g. dcbx renegotiation). Other
1600  * params may be affected by the init but would simply recalculate to the same
1601  * values. The allocations made for QM init, ports, vports, pqs and vfqs are not
1602  * affected as these amounts stay the same.
1603  */
1604 static void qed_init_qm_reset_params(struct qed_hwfn *p_hwfn)
1605 {
1606 	struct qed_qm_info *qm_info = &p_hwfn->qm_info;
1607 
1608 	qm_info->num_pqs = 0;
1609 	qm_info->num_vports = 0;
1610 	qm_info->num_pf_rls = 0;
1611 	qm_info->num_vf_pqs = 0;
1612 	qm_info->first_vf_pq = 0;
1613 	qm_info->first_mcos_pq = 0;
1614 	qm_info->first_rl_pq = 0;
1615 }
1616 
1617 static void qed_init_qm_advance_vport(struct qed_hwfn *p_hwfn)
1618 {
1619 	struct qed_qm_info *qm_info = &p_hwfn->qm_info;
1620 
1621 	qm_info->num_vports++;
1622 
1623 	if (qm_info->num_vports > qed_init_qm_get_num_vports(p_hwfn))
1624 		DP_ERR(p_hwfn,
1625 		       "vport overflow! qm_info->num_vports %d, qm_init_get_num_vports() %d\n",
1626 		       qm_info->num_vports, qed_init_qm_get_num_vports(p_hwfn));
1627 }
1628 
1629 /* initialize a single pq and manage qm_info resources accounting.
1630  * The pq_init_flags param determines whether the PQ is rate limited
1631  * (for VF or PF) and whether a new vport is allocated to the pq or not
1632  * (i.e. vport will be shared).
1633  */
1634 
1635 /* flags for pq init */
1636 #define PQ_INIT_SHARE_VPORT     (1 << 0)
1637 #define PQ_INIT_PF_RL           (1 << 1)
1638 #define PQ_INIT_VF_RL           (1 << 2)
1639 
1640 /* defines for pq init */
1641 #define PQ_INIT_DEFAULT_WRR_GROUP       1
1642 #define PQ_INIT_DEFAULT_TC              0
1643 
1644 void qed_hw_info_set_offload_tc(struct qed_hw_info *p_info, u8 tc)
1645 {
1646 	p_info->offload_tc = tc;
1647 	p_info->offload_tc_set = true;
1648 }
1649 
1650 static bool qed_is_offload_tc_set(struct qed_hwfn *p_hwfn)
1651 {
1652 	return p_hwfn->hw_info.offload_tc_set;
1653 }
1654 
1655 static u32 qed_get_offload_tc(struct qed_hwfn *p_hwfn)
1656 {
1657 	if (qed_is_offload_tc_set(p_hwfn))
1658 		return p_hwfn->hw_info.offload_tc;
1659 
1660 	return PQ_INIT_DEFAULT_TC;
1661 }
1662 
1663 static void qed_init_qm_pq(struct qed_hwfn *p_hwfn,
1664 			   struct qed_qm_info *qm_info,
1665 			   u8 tc, u32 pq_init_flags)
1666 {
1667 	u16 pq_idx = qm_info->num_pqs, max_pq = qed_init_qm_get_num_pqs(p_hwfn);
1668 
1669 	if (pq_idx > max_pq)
1670 		DP_ERR(p_hwfn,
1671 		       "pq overflow! pq %d, max pq %d\n", pq_idx, max_pq);
1672 
1673 	/* init pq params */
1674 	qm_info->qm_pq_params[pq_idx].port_id = p_hwfn->port_id;
1675 	qm_info->qm_pq_params[pq_idx].vport_id = qm_info->start_vport +
1676 	    qm_info->num_vports;
1677 	qm_info->qm_pq_params[pq_idx].tc_id = tc;
1678 	qm_info->qm_pq_params[pq_idx].wrr_group = PQ_INIT_DEFAULT_WRR_GROUP;
1679 	qm_info->qm_pq_params[pq_idx].rl_valid =
1680 	    (pq_init_flags & PQ_INIT_PF_RL || pq_init_flags & PQ_INIT_VF_RL);
1681 
1682 	/* qm params accounting */
1683 	qm_info->num_pqs++;
1684 	if (!(pq_init_flags & PQ_INIT_SHARE_VPORT))
1685 		qm_info->num_vports++;
1686 
1687 	if (pq_init_flags & PQ_INIT_PF_RL)
1688 		qm_info->num_pf_rls++;
1689 
1690 	if (qm_info->num_vports > qed_init_qm_get_num_vports(p_hwfn))
1691 		DP_ERR(p_hwfn,
1692 		       "vport overflow! qm_info->num_vports %d, qm_init_get_num_vports() %d\n",
1693 		       qm_info->num_vports, qed_init_qm_get_num_vports(p_hwfn));
1694 
1695 	if (qm_info->num_pf_rls > qed_init_qm_get_num_pf_rls(p_hwfn))
1696 		DP_ERR(p_hwfn,
1697 		       "rl overflow! qm_info->num_pf_rls %d, qm_init_get_num_pf_rls() %d\n",
1698 		       qm_info->num_pf_rls, qed_init_qm_get_num_pf_rls(p_hwfn));
1699 }
1700 
1701 /* get pq index according to PQ_FLAGS */
1702 static u16 *qed_init_qm_get_idx_from_flags(struct qed_hwfn *p_hwfn,
1703 					   unsigned long pq_flags)
1704 {
1705 	struct qed_qm_info *qm_info = &p_hwfn->qm_info;
1706 
1707 	/* Can't have multiple flags set here */
1708 	if (bitmap_weight(&pq_flags,
1709 			  sizeof(pq_flags) * BITS_PER_BYTE) > 1) {
1710 		DP_ERR(p_hwfn, "requested multiple pq flags 0x%lx\n", pq_flags);
1711 		goto err;
1712 	}
1713 
1714 	if (!(qed_get_pq_flags(p_hwfn) & pq_flags)) {
1715 		DP_ERR(p_hwfn, "pq flag 0x%lx is not set\n", pq_flags);
1716 		goto err;
1717 	}
1718 
1719 	switch (pq_flags) {
1720 	case PQ_FLAGS_RLS:
1721 		return &qm_info->first_rl_pq;
1722 	case PQ_FLAGS_MCOS:
1723 		return &qm_info->first_mcos_pq;
1724 	case PQ_FLAGS_LB:
1725 		return &qm_info->pure_lb_pq;
1726 	case PQ_FLAGS_OOO:
1727 		return &qm_info->ooo_pq;
1728 	case PQ_FLAGS_ACK:
1729 		return &qm_info->pure_ack_pq;
1730 	case PQ_FLAGS_OFLD:
1731 		return &qm_info->first_ofld_pq;
1732 	case PQ_FLAGS_LLT:
1733 		return &qm_info->first_llt_pq;
1734 	case PQ_FLAGS_VFS:
1735 		return &qm_info->first_vf_pq;
1736 	default:
1737 		goto err;
1738 	}
1739 
1740 err:
1741 	return &qm_info->start_pq;
1742 }
1743 
1744 /* save pq index in qm info */
1745 static void qed_init_qm_set_idx(struct qed_hwfn *p_hwfn,
1746 				u32 pq_flags, u16 pq_val)
1747 {
1748 	u16 *base_pq_idx = qed_init_qm_get_idx_from_flags(p_hwfn, pq_flags);
1749 
1750 	*base_pq_idx = p_hwfn->qm_info.start_pq + pq_val;
1751 }
1752 
1753 /* get tx pq index, with the PQ TX base already set (ready for context init) */
1754 u16 qed_get_cm_pq_idx(struct qed_hwfn *p_hwfn, u32 pq_flags)
1755 {
1756 	u16 *base_pq_idx = qed_init_qm_get_idx_from_flags(p_hwfn, pq_flags);
1757 
1758 	return *base_pq_idx + CM_TX_PQ_BASE;
1759 }
1760 
1761 u16 qed_get_cm_pq_idx_mcos(struct qed_hwfn *p_hwfn, u8 tc)
1762 {
1763 	u8 max_tc = qed_init_qm_get_num_tcs(p_hwfn);
1764 
1765 	if (max_tc == 0) {
1766 		DP_ERR(p_hwfn, "pq with flag 0x%lx do not exist\n",
1767 		       PQ_FLAGS_MCOS);
1768 		return p_hwfn->qm_info.start_pq;
1769 	}
1770 
1771 	if (tc > max_tc)
1772 		DP_ERR(p_hwfn, "tc %d must be smaller than %d\n", tc, max_tc);
1773 
1774 	return qed_get_cm_pq_idx(p_hwfn, PQ_FLAGS_MCOS) + (tc % max_tc);
1775 }
1776 
1777 u16 qed_get_cm_pq_idx_vf(struct qed_hwfn *p_hwfn, u16 vf)
1778 {
1779 	u16 max_vf = qed_init_qm_get_num_vfs(p_hwfn);
1780 
1781 	if (max_vf == 0) {
1782 		DP_ERR(p_hwfn, "pq with flag 0x%lx do not exist\n",
1783 		       PQ_FLAGS_VFS);
1784 		return p_hwfn->qm_info.start_pq;
1785 	}
1786 
1787 	if (vf > max_vf)
1788 		DP_ERR(p_hwfn, "vf %d must be smaller than %d\n", vf, max_vf);
1789 
1790 	return qed_get_cm_pq_idx(p_hwfn, PQ_FLAGS_VFS) + (vf % max_vf);
1791 }
1792 
1793 u16 qed_get_cm_pq_idx_ofld_mtc(struct qed_hwfn *p_hwfn, u8 tc)
1794 {
1795 	u16 first_ofld_pq, pq_offset;
1796 
1797 	first_ofld_pq = qed_get_cm_pq_idx(p_hwfn, PQ_FLAGS_OFLD);
1798 	pq_offset = (tc < qed_init_qm_get_num_mtc_tcs(p_hwfn)) ?
1799 		    tc : PQ_INIT_DEFAULT_TC;
1800 
1801 	return first_ofld_pq + pq_offset;
1802 }
1803 
1804 u16 qed_get_cm_pq_idx_llt_mtc(struct qed_hwfn *p_hwfn, u8 tc)
1805 {
1806 	u16 first_llt_pq, pq_offset;
1807 
1808 	first_llt_pq = qed_get_cm_pq_idx(p_hwfn, PQ_FLAGS_LLT);
1809 	pq_offset = (tc < qed_init_qm_get_num_mtc_tcs(p_hwfn)) ?
1810 		    tc : PQ_INIT_DEFAULT_TC;
1811 
1812 	return first_llt_pq + pq_offset;
1813 }
1814 
1815 /* Functions for creating specific types of pqs */
1816 static void qed_init_qm_lb_pq(struct qed_hwfn *p_hwfn)
1817 {
1818 	struct qed_qm_info *qm_info = &p_hwfn->qm_info;
1819 
1820 	if (!(qed_get_pq_flags(p_hwfn) & PQ_FLAGS_LB))
1821 		return;
1822 
1823 	qed_init_qm_set_idx(p_hwfn, PQ_FLAGS_LB, qm_info->num_pqs);
1824 	qed_init_qm_pq(p_hwfn, qm_info, PURE_LB_TC, PQ_INIT_SHARE_VPORT);
1825 }
1826 
1827 static void qed_init_qm_ooo_pq(struct qed_hwfn *p_hwfn)
1828 {
1829 	struct qed_qm_info *qm_info = &p_hwfn->qm_info;
1830 
1831 	if (!(qed_get_pq_flags(p_hwfn) & PQ_FLAGS_OOO))
1832 		return;
1833 
1834 	qed_init_qm_set_idx(p_hwfn, PQ_FLAGS_OOO, qm_info->num_pqs);
1835 	qed_init_qm_pq(p_hwfn, qm_info, qm_info->ooo_tc, PQ_INIT_SHARE_VPORT);
1836 }
1837 
1838 static void qed_init_qm_pure_ack_pq(struct qed_hwfn *p_hwfn)
1839 {
1840 	struct qed_qm_info *qm_info = &p_hwfn->qm_info;
1841 
1842 	if (!(qed_get_pq_flags(p_hwfn) & PQ_FLAGS_ACK))
1843 		return;
1844 
1845 	qed_init_qm_set_idx(p_hwfn, PQ_FLAGS_ACK, qm_info->num_pqs);
1846 	qed_init_qm_pq(p_hwfn, qm_info, qed_get_offload_tc(p_hwfn),
1847 		       PQ_INIT_SHARE_VPORT);
1848 }
1849 
1850 static void qed_init_qm_mtc_pqs(struct qed_hwfn *p_hwfn)
1851 {
1852 	u8 num_tcs = qed_init_qm_get_num_mtc_tcs(p_hwfn);
1853 	struct qed_qm_info *qm_info = &p_hwfn->qm_info;
1854 	u8 tc;
1855 
1856 	/* override pq's TC if offload TC is set */
1857 	for (tc = 0; tc < num_tcs; tc++)
1858 		qed_init_qm_pq(p_hwfn, qm_info,
1859 			       qed_is_offload_tc_set(p_hwfn) ?
1860 			       p_hwfn->hw_info.offload_tc : tc,
1861 			       PQ_INIT_SHARE_VPORT);
1862 }
1863 
1864 static void qed_init_qm_offload_pq(struct qed_hwfn *p_hwfn)
1865 {
1866 	struct qed_qm_info *qm_info = &p_hwfn->qm_info;
1867 
1868 	if (!(qed_get_pq_flags(p_hwfn) & PQ_FLAGS_OFLD))
1869 		return;
1870 
1871 	qed_init_qm_set_idx(p_hwfn, PQ_FLAGS_OFLD, qm_info->num_pqs);
1872 	qed_init_qm_mtc_pqs(p_hwfn);
1873 }
1874 
1875 static void qed_init_qm_low_latency_pq(struct qed_hwfn *p_hwfn)
1876 {
1877 	struct qed_qm_info *qm_info = &p_hwfn->qm_info;
1878 
1879 	if (!(qed_get_pq_flags(p_hwfn) & PQ_FLAGS_LLT))
1880 		return;
1881 
1882 	qed_init_qm_set_idx(p_hwfn, PQ_FLAGS_LLT, qm_info->num_pqs);
1883 	qed_init_qm_mtc_pqs(p_hwfn);
1884 }
1885 
1886 static void qed_init_qm_mcos_pqs(struct qed_hwfn *p_hwfn)
1887 {
1888 	struct qed_qm_info *qm_info = &p_hwfn->qm_info;
1889 	u8 tc_idx;
1890 
1891 	if (!(qed_get_pq_flags(p_hwfn) & PQ_FLAGS_MCOS))
1892 		return;
1893 
1894 	qed_init_qm_set_idx(p_hwfn, PQ_FLAGS_MCOS, qm_info->num_pqs);
1895 	for (tc_idx = 0; tc_idx < qed_init_qm_get_num_tcs(p_hwfn); tc_idx++)
1896 		qed_init_qm_pq(p_hwfn, qm_info, tc_idx, PQ_INIT_SHARE_VPORT);
1897 }
1898 
1899 static void qed_init_qm_vf_pqs(struct qed_hwfn *p_hwfn)
1900 {
1901 	struct qed_qm_info *qm_info = &p_hwfn->qm_info;
1902 	u16 vf_idx, num_vfs = qed_init_qm_get_num_vfs(p_hwfn);
1903 
1904 	if (!(qed_get_pq_flags(p_hwfn) & PQ_FLAGS_VFS))
1905 		return;
1906 
1907 	qed_init_qm_set_idx(p_hwfn, PQ_FLAGS_VFS, qm_info->num_pqs);
1908 	qm_info->num_vf_pqs = num_vfs;
1909 	for (vf_idx = 0; vf_idx < num_vfs; vf_idx++)
1910 		qed_init_qm_pq(p_hwfn,
1911 			       qm_info, PQ_INIT_DEFAULT_TC, PQ_INIT_VF_RL);
1912 }
1913 
1914 static void qed_init_qm_rl_pqs(struct qed_hwfn *p_hwfn)
1915 {
1916 	u16 pf_rls_idx, num_pf_rls = qed_init_qm_get_num_pf_rls(p_hwfn);
1917 	struct qed_qm_info *qm_info = &p_hwfn->qm_info;
1918 
1919 	if (!(qed_get_pq_flags(p_hwfn) & PQ_FLAGS_RLS))
1920 		return;
1921 
1922 	qed_init_qm_set_idx(p_hwfn, PQ_FLAGS_RLS, qm_info->num_pqs);
1923 	for (pf_rls_idx = 0; pf_rls_idx < num_pf_rls; pf_rls_idx++)
1924 		qed_init_qm_pq(p_hwfn, qm_info, qed_get_offload_tc(p_hwfn),
1925 			       PQ_INIT_PF_RL);
1926 }
1927 
1928 static void qed_init_qm_pq_params(struct qed_hwfn *p_hwfn)
1929 {
1930 	/* rate limited pqs, must come first (FW assumption) */
1931 	qed_init_qm_rl_pqs(p_hwfn);
1932 
1933 	/* pqs for multi cos */
1934 	qed_init_qm_mcos_pqs(p_hwfn);
1935 
1936 	/* pure loopback pq */
1937 	qed_init_qm_lb_pq(p_hwfn);
1938 
1939 	/* out of order pq */
1940 	qed_init_qm_ooo_pq(p_hwfn);
1941 
1942 	/* pure ack pq */
1943 	qed_init_qm_pure_ack_pq(p_hwfn);
1944 
1945 	/* pq for offloaded protocol */
1946 	qed_init_qm_offload_pq(p_hwfn);
1947 
1948 	/* low latency pq */
1949 	qed_init_qm_low_latency_pq(p_hwfn);
1950 
1951 	/* done sharing vports */
1952 	qed_init_qm_advance_vport(p_hwfn);
1953 
1954 	/* pqs for vfs */
1955 	qed_init_qm_vf_pqs(p_hwfn);
1956 }
1957 
1958 /* compare values of getters against resources amounts */
1959 static int qed_init_qm_sanity(struct qed_hwfn *p_hwfn)
1960 {
1961 	if (qed_init_qm_get_num_vports(p_hwfn) > RESC_NUM(p_hwfn, QED_VPORT)) {
1962 		DP_ERR(p_hwfn, "requested amount of vports exceeds resource\n");
1963 		return -EINVAL;
1964 	}
1965 
1966 	if (qed_init_qm_get_num_pqs(p_hwfn) <= RESC_NUM(p_hwfn, QED_PQ))
1967 		return 0;
1968 
1969 	if (QED_IS_ROCE_PERSONALITY(p_hwfn)) {
1970 		p_hwfn->hw_info.multi_tc_roce_en = 0;
1971 		DP_NOTICE(p_hwfn,
1972 			  "multi-tc roce was disabled to reduce requested amount of pqs\n");
1973 		if (qed_init_qm_get_num_pqs(p_hwfn) <= RESC_NUM(p_hwfn, QED_PQ))
1974 			return 0;
1975 	}
1976 
1977 	DP_ERR(p_hwfn, "requested amount of pqs exceeds resource\n");
1978 	return -EINVAL;
1979 }
1980 
1981 static void qed_dp_init_qm_params(struct qed_hwfn *p_hwfn)
1982 {
1983 	struct qed_qm_info *qm_info = &p_hwfn->qm_info;
1984 	struct init_qm_vport_params *vport;
1985 	struct init_qm_port_params *port;
1986 	struct init_qm_pq_params *pq;
1987 	int i, tc;
1988 
1989 	/* top level params */
1990 	DP_VERBOSE(p_hwfn,
1991 		   NETIF_MSG_HW,
1992 		   "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",
1993 		   qm_info->start_pq,
1994 		   qm_info->start_vport,
1995 		   qm_info->pure_lb_pq,
1996 		   qm_info->first_ofld_pq,
1997 		   qm_info->first_llt_pq,
1998 		   qm_info->pure_ack_pq);
1999 	DP_VERBOSE(p_hwfn,
2000 		   NETIF_MSG_HW,
2001 		   "ooo_pq %d, first_vf_pq %d, num_pqs %d, num_vf_pqs %d, num_vports %d, max_phys_tcs_per_port %d\n",
2002 		   qm_info->ooo_pq,
2003 		   qm_info->first_vf_pq,
2004 		   qm_info->num_pqs,
2005 		   qm_info->num_vf_pqs,
2006 		   qm_info->num_vports, qm_info->max_phys_tcs_per_port);
2007 	DP_VERBOSE(p_hwfn,
2008 		   NETIF_MSG_HW,
2009 		   "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",
2010 		   qm_info->pf_rl_en,
2011 		   qm_info->pf_wfq_en,
2012 		   qm_info->vport_rl_en,
2013 		   qm_info->vport_wfq_en,
2014 		   qm_info->pf_wfq,
2015 		   qm_info->pf_rl,
2016 		   qm_info->num_pf_rls, qed_get_pq_flags(p_hwfn));
2017 
2018 	/* port table */
2019 	for (i = 0; i < p_hwfn->cdev->num_ports_in_engine; i++) {
2020 		port = &(qm_info->qm_port_params[i]);
2021 		DP_VERBOSE(p_hwfn,
2022 			   NETIF_MSG_HW,
2023 			   "port idx %d, active %d, active_phys_tcs %d, num_pbf_cmd_lines %d, num_btb_blocks %d, reserved %d\n",
2024 			   i,
2025 			   port->active,
2026 			   port->active_phys_tcs,
2027 			   port->num_pbf_cmd_lines,
2028 			   port->num_btb_blocks, port->reserved);
2029 	}
2030 
2031 	/* vport table */
2032 	for (i = 0; i < qm_info->num_vports; i++) {
2033 		vport = &(qm_info->qm_vport_params[i]);
2034 		DP_VERBOSE(p_hwfn,
2035 			   NETIF_MSG_HW,
2036 			   "vport idx %d, vport_rl %d, wfq %d, first_tx_pq_id [ ",
2037 			   qm_info->start_vport + i,
2038 			   vport->vport_rl, vport->vport_wfq);
2039 		for (tc = 0; tc < NUM_OF_TCS; tc++)
2040 			DP_VERBOSE(p_hwfn,
2041 				   NETIF_MSG_HW,
2042 				   "%d ", vport->first_tx_pq_id[tc]);
2043 		DP_VERBOSE(p_hwfn, NETIF_MSG_HW, "]\n");
2044 	}
2045 
2046 	/* pq table */
2047 	for (i = 0; i < qm_info->num_pqs; i++) {
2048 		pq = &(qm_info->qm_pq_params[i]);
2049 		DP_VERBOSE(p_hwfn,
2050 			   NETIF_MSG_HW,
2051 			   "pq idx %d, port %d, vport_id %d, tc %d, wrr_grp %d, rl_valid %d\n",
2052 			   qm_info->start_pq + i,
2053 			   pq->port_id,
2054 			   pq->vport_id,
2055 			   pq->tc_id, pq->wrr_group, pq->rl_valid);
2056 	}
2057 }
2058 
2059 static void qed_init_qm_info(struct qed_hwfn *p_hwfn)
2060 {
2061 	/* reset params required for init run */
2062 	qed_init_qm_reset_params(p_hwfn);
2063 
2064 	/* init QM top level params */
2065 	qed_init_qm_params(p_hwfn);
2066 
2067 	/* init QM port params */
2068 	qed_init_qm_port_params(p_hwfn);
2069 
2070 	/* init QM vport params */
2071 	qed_init_qm_vport_params(p_hwfn);
2072 
2073 	/* init QM physical queue params */
2074 	qed_init_qm_pq_params(p_hwfn);
2075 
2076 	/* display all that init */
2077 	qed_dp_init_qm_params(p_hwfn);
2078 }
2079 
2080 /* This function reconfigures the QM pf on the fly.
2081  * For this purpose we:
2082  * 1. reconfigure the QM database
2083  * 2. set new values to runtime array
2084  * 3. send an sdm_qm_cmd through the rbc interface to stop the QM
2085  * 4. activate init tool in QM_PF stage
2086  * 5. send an sdm_qm_cmd through rbc interface to release the QM
2087  */
2088 int qed_qm_reconf(struct qed_hwfn *p_hwfn, struct qed_ptt *p_ptt)
2089 {
2090 	struct qed_qm_info *qm_info = &p_hwfn->qm_info;
2091 	bool b_rc;
2092 	int rc;
2093 
2094 	/* initialize qed's qm data structure */
2095 	qed_init_qm_info(p_hwfn);
2096 
2097 	/* stop PF's qm queues */
2098 	spin_lock_bh(&qm_lock);
2099 	b_rc = qed_send_qm_stop_cmd(p_hwfn, p_ptt, false, true,
2100 				    qm_info->start_pq, qm_info->num_pqs);
2101 	spin_unlock_bh(&qm_lock);
2102 	if (!b_rc)
2103 		return -EINVAL;
2104 
2105 	/* clear the QM_PF runtime phase leftovers from previous init */
2106 	qed_init_clear_rt_data(p_hwfn);
2107 
2108 	/* prepare QM portion of runtime array */
2109 	qed_qm_init_pf(p_hwfn, p_ptt, false);
2110 
2111 	/* activate init tool on runtime array */
2112 	rc = qed_init_run(p_hwfn, p_ptt, PHASE_QM_PF, p_hwfn->rel_pf_id,
2113 			  p_hwfn->hw_info.hw_mode);
2114 	if (rc)
2115 		return rc;
2116 
2117 	/* start PF's qm queues */
2118 	spin_lock_bh(&qm_lock);
2119 	b_rc = qed_send_qm_stop_cmd(p_hwfn, p_ptt, true, true,
2120 				    qm_info->start_pq, qm_info->num_pqs);
2121 	spin_unlock_bh(&qm_lock);
2122 	if (!b_rc)
2123 		return -EINVAL;
2124 
2125 	return 0;
2126 }
2127 
2128 static int qed_alloc_qm_data(struct qed_hwfn *p_hwfn)
2129 {
2130 	struct qed_qm_info *qm_info = &p_hwfn->qm_info;
2131 	int rc;
2132 
2133 	rc = qed_init_qm_sanity(p_hwfn);
2134 	if (rc)
2135 		goto alloc_err;
2136 
2137 	qm_info->qm_pq_params = kcalloc(qed_init_qm_get_num_pqs(p_hwfn),
2138 					sizeof(*qm_info->qm_pq_params),
2139 					GFP_KERNEL);
2140 	if (!qm_info->qm_pq_params)
2141 		goto alloc_err;
2142 
2143 	qm_info->qm_vport_params = kcalloc(qed_init_qm_get_num_vports(p_hwfn),
2144 					   sizeof(*qm_info->qm_vport_params),
2145 					   GFP_KERNEL);
2146 	if (!qm_info->qm_vport_params)
2147 		goto alloc_err;
2148 
2149 	qm_info->qm_port_params = kcalloc(p_hwfn->cdev->num_ports_in_engine,
2150 					  sizeof(*qm_info->qm_port_params),
2151 					  GFP_KERNEL);
2152 	if (!qm_info->qm_port_params)
2153 		goto alloc_err;
2154 
2155 	qm_info->wfq_data = kcalloc(qed_init_qm_get_num_vports(p_hwfn),
2156 				    sizeof(*qm_info->wfq_data),
2157 				    GFP_KERNEL);
2158 	if (!qm_info->wfq_data)
2159 		goto alloc_err;
2160 
2161 	return 0;
2162 
2163 alloc_err:
2164 	DP_NOTICE(p_hwfn, "Failed to allocate memory for QM params\n");
2165 	qed_qm_info_free(p_hwfn);
2166 	return -ENOMEM;
2167 }
2168 
2169 int qed_resc_alloc(struct qed_dev *cdev)
2170 {
2171 	u32 rdma_tasks, excess_tasks;
2172 	u32 line_count;
2173 	int i, rc = 0;
2174 
2175 	if (IS_VF(cdev)) {
2176 		for_each_hwfn(cdev, i) {
2177 			rc = qed_l2_alloc(&cdev->hwfns[i]);
2178 			if (rc)
2179 				return rc;
2180 		}
2181 		return rc;
2182 	}
2183 
2184 	cdev->fw_data = kzalloc(sizeof(*cdev->fw_data), GFP_KERNEL);
2185 	if (!cdev->fw_data)
2186 		return -ENOMEM;
2187 
2188 	for_each_hwfn(cdev, i) {
2189 		struct qed_hwfn *p_hwfn = &cdev->hwfns[i];
2190 		u32 n_eqes, num_cons;
2191 
2192 		/* Initialize the doorbell recovery mechanism */
2193 		rc = qed_db_recovery_setup(p_hwfn);
2194 		if (rc)
2195 			goto alloc_err;
2196 
2197 		/* First allocate the context manager structure */
2198 		rc = qed_cxt_mngr_alloc(p_hwfn);
2199 		if (rc)
2200 			goto alloc_err;
2201 
2202 		/* Set the HW cid/tid numbers (in the contest manager)
2203 		 * Must be done prior to any further computations.
2204 		 */
2205 		rc = qed_cxt_set_pf_params(p_hwfn, RDMA_MAX_TIDS);
2206 		if (rc)
2207 			goto alloc_err;
2208 
2209 		rc = qed_alloc_qm_data(p_hwfn);
2210 		if (rc)
2211 			goto alloc_err;
2212 
2213 		/* init qm info */
2214 		qed_init_qm_info(p_hwfn);
2215 
2216 		/* Compute the ILT client partition */
2217 		rc = qed_cxt_cfg_ilt_compute(p_hwfn, &line_count);
2218 		if (rc) {
2219 			DP_NOTICE(p_hwfn,
2220 				  "too many ILT lines; re-computing with less lines\n");
2221 			/* In case there are not enough ILT lines we reduce the
2222 			 * number of RDMA tasks and re-compute.
2223 			 */
2224 			excess_tasks =
2225 			    qed_cxt_cfg_ilt_compute_excess(p_hwfn, line_count);
2226 			if (!excess_tasks)
2227 				goto alloc_err;
2228 
2229 			rdma_tasks = RDMA_MAX_TIDS - excess_tasks;
2230 			rc = qed_cxt_set_pf_params(p_hwfn, rdma_tasks);
2231 			if (rc)
2232 				goto alloc_err;
2233 
2234 			rc = qed_cxt_cfg_ilt_compute(p_hwfn, &line_count);
2235 			if (rc) {
2236 				DP_ERR(p_hwfn,
2237 				       "failed ILT compute. Requested too many lines: %u\n",
2238 				       line_count);
2239 
2240 				goto alloc_err;
2241 			}
2242 		}
2243 
2244 		/* CID map / ILT shadow table / T2
2245 		 * The talbes sizes are determined by the computations above
2246 		 */
2247 		rc = qed_cxt_tables_alloc(p_hwfn);
2248 		if (rc)
2249 			goto alloc_err;
2250 
2251 		/* SPQ, must follow ILT because initializes SPQ context */
2252 		rc = qed_spq_alloc(p_hwfn);
2253 		if (rc)
2254 			goto alloc_err;
2255 
2256 		/* SP status block allocation */
2257 		p_hwfn->p_dpc_ptt = qed_get_reserved_ptt(p_hwfn,
2258 							 RESERVED_PTT_DPC);
2259 
2260 		rc = qed_int_alloc(p_hwfn, p_hwfn->p_main_ptt);
2261 		if (rc)
2262 			goto alloc_err;
2263 
2264 		rc = qed_iov_alloc(p_hwfn);
2265 		if (rc)
2266 			goto alloc_err;
2267 
2268 		/* EQ */
2269 		n_eqes = qed_chain_get_capacity(&p_hwfn->p_spq->chain);
2270 		if (QED_IS_RDMA_PERSONALITY(p_hwfn)) {
2271 			enum protocol_type rdma_proto;
2272 
2273 			if (QED_IS_ROCE_PERSONALITY(p_hwfn))
2274 				rdma_proto = PROTOCOLID_ROCE;
2275 			else
2276 				rdma_proto = PROTOCOLID_IWARP;
2277 
2278 			num_cons = qed_cxt_get_proto_cid_count(p_hwfn,
2279 							       rdma_proto,
2280 							       NULL) * 2;
2281 			n_eqes += num_cons + 2 * MAX_NUM_VFS_BB;
2282 		} else if (p_hwfn->hw_info.personality == QED_PCI_ISCSI) {
2283 			num_cons =
2284 			    qed_cxt_get_proto_cid_count(p_hwfn,
2285 							PROTOCOLID_ISCSI,
2286 							NULL);
2287 			n_eqes += 2 * num_cons;
2288 		}
2289 
2290 		if (n_eqes > 0xFFFF) {
2291 			DP_ERR(p_hwfn,
2292 			       "Cannot allocate 0x%x EQ elements. The maximum of a u16 chain is 0x%x\n",
2293 			       n_eqes, 0xFFFF);
2294 			goto alloc_no_mem;
2295 		}
2296 
2297 		rc = qed_eq_alloc(p_hwfn, (u16) n_eqes);
2298 		if (rc)
2299 			goto alloc_err;
2300 
2301 		rc = qed_consq_alloc(p_hwfn);
2302 		if (rc)
2303 			goto alloc_err;
2304 
2305 		rc = qed_l2_alloc(p_hwfn);
2306 		if (rc)
2307 			goto alloc_err;
2308 
2309 #ifdef CONFIG_QED_LL2
2310 		if (p_hwfn->using_ll2) {
2311 			rc = qed_ll2_alloc(p_hwfn);
2312 			if (rc)
2313 				goto alloc_err;
2314 		}
2315 #endif
2316 
2317 		if (p_hwfn->hw_info.personality == QED_PCI_FCOE) {
2318 			rc = qed_fcoe_alloc(p_hwfn);
2319 			if (rc)
2320 				goto alloc_err;
2321 		}
2322 
2323 		if (p_hwfn->hw_info.personality == QED_PCI_ISCSI) {
2324 			rc = qed_iscsi_alloc(p_hwfn);
2325 			if (rc)
2326 				goto alloc_err;
2327 			rc = qed_ooo_alloc(p_hwfn);
2328 			if (rc)
2329 				goto alloc_err;
2330 		}
2331 
2332 		if (QED_IS_RDMA_PERSONALITY(p_hwfn)) {
2333 			rc = qed_rdma_info_alloc(p_hwfn);
2334 			if (rc)
2335 				goto alloc_err;
2336 		}
2337 
2338 		/* DMA info initialization */
2339 		rc = qed_dmae_info_alloc(p_hwfn);
2340 		if (rc)
2341 			goto alloc_err;
2342 
2343 		/* DCBX initialization */
2344 		rc = qed_dcbx_info_alloc(p_hwfn);
2345 		if (rc)
2346 			goto alloc_err;
2347 
2348 		rc = qed_dbg_alloc_user_data(p_hwfn);
2349 		if (rc)
2350 			goto alloc_err;
2351 	}
2352 
2353 	rc = qed_llh_alloc(cdev);
2354 	if (rc) {
2355 		DP_NOTICE(cdev,
2356 			  "Failed to allocate memory for the llh_info structure\n");
2357 		goto alloc_err;
2358 	}
2359 
2360 	cdev->reset_stats = kzalloc(sizeof(*cdev->reset_stats), GFP_KERNEL);
2361 	if (!cdev->reset_stats)
2362 		goto alloc_no_mem;
2363 
2364 	return 0;
2365 
2366 alloc_no_mem:
2367 	rc = -ENOMEM;
2368 alloc_err:
2369 	qed_resc_free(cdev);
2370 	return rc;
2371 }
2372 
2373 void qed_resc_setup(struct qed_dev *cdev)
2374 {
2375 	int i;
2376 
2377 	if (IS_VF(cdev)) {
2378 		for_each_hwfn(cdev, i)
2379 			qed_l2_setup(&cdev->hwfns[i]);
2380 		return;
2381 	}
2382 
2383 	for_each_hwfn(cdev, i) {
2384 		struct qed_hwfn *p_hwfn = &cdev->hwfns[i];
2385 
2386 		qed_cxt_mngr_setup(p_hwfn);
2387 		qed_spq_setup(p_hwfn);
2388 		qed_eq_setup(p_hwfn);
2389 		qed_consq_setup(p_hwfn);
2390 
2391 		/* Read shadow of current MFW mailbox */
2392 		qed_mcp_read_mb(p_hwfn, p_hwfn->p_main_ptt);
2393 		memcpy(p_hwfn->mcp_info->mfw_mb_shadow,
2394 		       p_hwfn->mcp_info->mfw_mb_cur,
2395 		       p_hwfn->mcp_info->mfw_mb_length);
2396 
2397 		qed_int_setup(p_hwfn, p_hwfn->p_main_ptt);
2398 
2399 		qed_l2_setup(p_hwfn);
2400 		qed_iov_setup(p_hwfn);
2401 #ifdef CONFIG_QED_LL2
2402 		if (p_hwfn->using_ll2)
2403 			qed_ll2_setup(p_hwfn);
2404 #endif
2405 		if (p_hwfn->hw_info.personality == QED_PCI_FCOE)
2406 			qed_fcoe_setup(p_hwfn);
2407 
2408 		if (p_hwfn->hw_info.personality == QED_PCI_ISCSI) {
2409 			qed_iscsi_setup(p_hwfn);
2410 			qed_ooo_setup(p_hwfn);
2411 		}
2412 	}
2413 }
2414 
2415 #define FINAL_CLEANUP_POLL_CNT          (100)
2416 #define FINAL_CLEANUP_POLL_TIME         (10)
2417 int qed_final_cleanup(struct qed_hwfn *p_hwfn,
2418 		      struct qed_ptt *p_ptt, u16 id, bool is_vf)
2419 {
2420 	u32 command = 0, addr, count = FINAL_CLEANUP_POLL_CNT;
2421 	int rc = -EBUSY;
2422 
2423 	addr = GTT_BAR0_MAP_REG_USDM_RAM +
2424 		USTORM_FLR_FINAL_ACK_OFFSET(p_hwfn->rel_pf_id);
2425 
2426 	if (is_vf)
2427 		id += 0x10;
2428 
2429 	command |= X_FINAL_CLEANUP_AGG_INT <<
2430 		SDM_AGG_INT_COMP_PARAMS_AGG_INT_INDEX_SHIFT;
2431 	command |= 1 << SDM_AGG_INT_COMP_PARAMS_AGG_VECTOR_ENABLE_SHIFT;
2432 	command |= id << SDM_AGG_INT_COMP_PARAMS_AGG_VECTOR_BIT_SHIFT;
2433 	command |= SDM_COMP_TYPE_AGG_INT << SDM_OP_GEN_COMP_TYPE_SHIFT;
2434 
2435 	/* Make sure notification is not set before initiating final cleanup */
2436 	if (REG_RD(p_hwfn, addr)) {
2437 		DP_NOTICE(p_hwfn,
2438 			  "Unexpected; Found final cleanup notification before initiating final cleanup\n");
2439 		REG_WR(p_hwfn, addr, 0);
2440 	}
2441 
2442 	DP_VERBOSE(p_hwfn, QED_MSG_IOV,
2443 		   "Sending final cleanup for PFVF[%d] [Command %08x]\n",
2444 		   id, command);
2445 
2446 	qed_wr(p_hwfn, p_ptt, XSDM_REG_OPERATION_GEN, command);
2447 
2448 	/* Poll until completion */
2449 	while (!REG_RD(p_hwfn, addr) && count--)
2450 		msleep(FINAL_CLEANUP_POLL_TIME);
2451 
2452 	if (REG_RD(p_hwfn, addr))
2453 		rc = 0;
2454 	else
2455 		DP_NOTICE(p_hwfn,
2456 			  "Failed to receive FW final cleanup notification\n");
2457 
2458 	/* Cleanup afterwards */
2459 	REG_WR(p_hwfn, addr, 0);
2460 
2461 	return rc;
2462 }
2463 
2464 static int qed_calc_hw_mode(struct qed_hwfn *p_hwfn)
2465 {
2466 	int hw_mode = 0;
2467 
2468 	if (QED_IS_BB_B0(p_hwfn->cdev)) {
2469 		hw_mode |= 1 << MODE_BB;
2470 	} else if (QED_IS_AH(p_hwfn->cdev)) {
2471 		hw_mode |= 1 << MODE_K2;
2472 	} else {
2473 		DP_NOTICE(p_hwfn, "Unknown chip type %#x\n",
2474 			  p_hwfn->cdev->type);
2475 		return -EINVAL;
2476 	}
2477 
2478 	switch (p_hwfn->cdev->num_ports_in_engine) {
2479 	case 1:
2480 		hw_mode |= 1 << MODE_PORTS_PER_ENG_1;
2481 		break;
2482 	case 2:
2483 		hw_mode |= 1 << MODE_PORTS_PER_ENG_2;
2484 		break;
2485 	case 4:
2486 		hw_mode |= 1 << MODE_PORTS_PER_ENG_4;
2487 		break;
2488 	default:
2489 		DP_NOTICE(p_hwfn, "num_ports_in_engine = %d not supported\n",
2490 			  p_hwfn->cdev->num_ports_in_engine);
2491 		return -EINVAL;
2492 	}
2493 
2494 	if (test_bit(QED_MF_OVLAN_CLSS, &p_hwfn->cdev->mf_bits))
2495 		hw_mode |= 1 << MODE_MF_SD;
2496 	else
2497 		hw_mode |= 1 << MODE_MF_SI;
2498 
2499 	hw_mode |= 1 << MODE_ASIC;
2500 
2501 	if (p_hwfn->cdev->num_hwfns > 1)
2502 		hw_mode |= 1 << MODE_100G;
2503 
2504 	p_hwfn->hw_info.hw_mode = hw_mode;
2505 
2506 	DP_VERBOSE(p_hwfn, (NETIF_MSG_PROBE | NETIF_MSG_IFUP),
2507 		   "Configuring function for hw_mode: 0x%08x\n",
2508 		   p_hwfn->hw_info.hw_mode);
2509 
2510 	return 0;
2511 }
2512 
2513 /* Init run time data for all PFs on an engine. */
2514 static void qed_init_cau_rt_data(struct qed_dev *cdev)
2515 {
2516 	u32 offset = CAU_REG_SB_VAR_MEMORY_RT_OFFSET;
2517 	int i, igu_sb_id;
2518 
2519 	for_each_hwfn(cdev, i) {
2520 		struct qed_hwfn *p_hwfn = &cdev->hwfns[i];
2521 		struct qed_igu_info *p_igu_info;
2522 		struct qed_igu_block *p_block;
2523 		struct cau_sb_entry sb_entry;
2524 
2525 		p_igu_info = p_hwfn->hw_info.p_igu_info;
2526 
2527 		for (igu_sb_id = 0;
2528 		     igu_sb_id < QED_MAPPING_MEMORY_SIZE(cdev); igu_sb_id++) {
2529 			p_block = &p_igu_info->entry[igu_sb_id];
2530 
2531 			if (!p_block->is_pf)
2532 				continue;
2533 
2534 			qed_init_cau_sb_entry(p_hwfn, &sb_entry,
2535 					      p_block->function_id, 0, 0);
2536 			STORE_RT_REG_AGG(p_hwfn, offset + igu_sb_id * 2,
2537 					 sb_entry);
2538 		}
2539 	}
2540 }
2541 
2542 static void qed_init_cache_line_size(struct qed_hwfn *p_hwfn,
2543 				     struct qed_ptt *p_ptt)
2544 {
2545 	u32 val, wr_mbs, cache_line_size;
2546 
2547 	val = qed_rd(p_hwfn, p_ptt, PSWRQ2_REG_WR_MBS0);
2548 	switch (val) {
2549 	case 0:
2550 		wr_mbs = 128;
2551 		break;
2552 	case 1:
2553 		wr_mbs = 256;
2554 		break;
2555 	case 2:
2556 		wr_mbs = 512;
2557 		break;
2558 	default:
2559 		DP_INFO(p_hwfn,
2560 			"Unexpected value of PSWRQ2_REG_WR_MBS0 [0x%x]. Avoid configuring PGLUE_B_REG_CACHE_LINE_SIZE.\n",
2561 			val);
2562 		return;
2563 	}
2564 
2565 	cache_line_size = min_t(u32, L1_CACHE_BYTES, wr_mbs);
2566 	switch (cache_line_size) {
2567 	case 32:
2568 		val = 0;
2569 		break;
2570 	case 64:
2571 		val = 1;
2572 		break;
2573 	case 128:
2574 		val = 2;
2575 		break;
2576 	case 256:
2577 		val = 3;
2578 		break;
2579 	default:
2580 		DP_INFO(p_hwfn,
2581 			"Unexpected value of cache line size [0x%x]. Avoid configuring PGLUE_B_REG_CACHE_LINE_SIZE.\n",
2582 			cache_line_size);
2583 	}
2584 
2585 	if (L1_CACHE_BYTES > wr_mbs)
2586 		DP_INFO(p_hwfn,
2587 			"The cache line size for padding is suboptimal for performance [OS cache line size 0x%x, wr mbs 0x%x]\n",
2588 			L1_CACHE_BYTES, wr_mbs);
2589 
2590 	STORE_RT_REG(p_hwfn, PGLUE_REG_B_CACHE_LINE_SIZE_RT_OFFSET, val);
2591 	if (val > 0) {
2592 		STORE_RT_REG(p_hwfn, PSWRQ2_REG_DRAM_ALIGN_WR_RT_OFFSET, val);
2593 		STORE_RT_REG(p_hwfn, PSWRQ2_REG_DRAM_ALIGN_RD_RT_OFFSET, val);
2594 	}
2595 }
2596 
2597 static int qed_hw_init_common(struct qed_hwfn *p_hwfn,
2598 			      struct qed_ptt *p_ptt, int hw_mode)
2599 {
2600 	struct qed_qm_info *qm_info = &p_hwfn->qm_info;
2601 	struct qed_qm_common_rt_init_params params;
2602 	struct qed_dev *cdev = p_hwfn->cdev;
2603 	u8 vf_id, max_num_vfs;
2604 	u16 num_pfs, pf_id;
2605 	u32 concrete_fid;
2606 	int rc = 0;
2607 
2608 	qed_init_cau_rt_data(cdev);
2609 
2610 	/* Program GTT windows */
2611 	qed_gtt_init(p_hwfn);
2612 
2613 	if (p_hwfn->mcp_info) {
2614 		if (p_hwfn->mcp_info->func_info.bandwidth_max)
2615 			qm_info->pf_rl_en = true;
2616 		if (p_hwfn->mcp_info->func_info.bandwidth_min)
2617 			qm_info->pf_wfq_en = true;
2618 	}
2619 
2620 	memset(&params, 0, sizeof(params));
2621 	params.max_ports_per_engine = p_hwfn->cdev->num_ports_in_engine;
2622 	params.max_phys_tcs_per_port = qm_info->max_phys_tcs_per_port;
2623 	params.pf_rl_en = qm_info->pf_rl_en;
2624 	params.pf_wfq_en = qm_info->pf_wfq_en;
2625 	params.vport_rl_en = qm_info->vport_rl_en;
2626 	params.vport_wfq_en = qm_info->vport_wfq_en;
2627 	params.port_params = qm_info->qm_port_params;
2628 
2629 	qed_qm_common_rt_init(p_hwfn, &params);
2630 
2631 	qed_cxt_hw_init_common(p_hwfn);
2632 
2633 	qed_init_cache_line_size(p_hwfn, p_ptt);
2634 
2635 	rc = qed_init_run(p_hwfn, p_ptt, PHASE_ENGINE, ANY_PHASE_ID, hw_mode);
2636 	if (rc)
2637 		return rc;
2638 
2639 	qed_wr(p_hwfn, p_ptt, PSWRQ2_REG_L2P_VALIDATE_VFID, 0);
2640 	qed_wr(p_hwfn, p_ptt, PGLUE_B_REG_USE_CLIENTID_IN_TAG, 1);
2641 
2642 	if (QED_IS_BB(p_hwfn->cdev)) {
2643 		num_pfs = NUM_OF_ENG_PFS(p_hwfn->cdev);
2644 		for (pf_id = 0; pf_id < num_pfs; pf_id++) {
2645 			qed_fid_pretend(p_hwfn, p_ptt, pf_id);
2646 			qed_wr(p_hwfn, p_ptt, PRS_REG_SEARCH_ROCE, 0x0);
2647 			qed_wr(p_hwfn, p_ptt, PRS_REG_SEARCH_TCP, 0x0);
2648 		}
2649 		/* pretend to original PF */
2650 		qed_fid_pretend(p_hwfn, p_ptt, p_hwfn->rel_pf_id);
2651 	}
2652 
2653 	max_num_vfs = QED_IS_AH(cdev) ? MAX_NUM_VFS_K2 : MAX_NUM_VFS_BB;
2654 	for (vf_id = 0; vf_id < max_num_vfs; vf_id++) {
2655 		concrete_fid = qed_vfid_to_concrete(p_hwfn, vf_id);
2656 		qed_fid_pretend(p_hwfn, p_ptt, (u16) concrete_fid);
2657 		qed_wr(p_hwfn, p_ptt, CCFC_REG_STRONG_ENABLE_VF, 0x1);
2658 		qed_wr(p_hwfn, p_ptt, CCFC_REG_WEAK_ENABLE_VF, 0x0);
2659 		qed_wr(p_hwfn, p_ptt, TCFC_REG_STRONG_ENABLE_VF, 0x1);
2660 		qed_wr(p_hwfn, p_ptt, TCFC_REG_WEAK_ENABLE_VF, 0x0);
2661 	}
2662 	/* pretend to original PF */
2663 	qed_fid_pretend(p_hwfn, p_ptt, p_hwfn->rel_pf_id);
2664 
2665 	return rc;
2666 }
2667 
2668 static int
2669 qed_hw_init_dpi_size(struct qed_hwfn *p_hwfn,
2670 		     struct qed_ptt *p_ptt, u32 pwm_region_size, u32 n_cpus)
2671 {
2672 	u32 dpi_bit_shift, dpi_count, dpi_page_size;
2673 	u32 min_dpis;
2674 	u32 n_wids;
2675 
2676 	/* Calculate DPI size */
2677 	n_wids = max_t(u32, QED_MIN_WIDS, n_cpus);
2678 	dpi_page_size = QED_WID_SIZE * roundup_pow_of_two(n_wids);
2679 	dpi_page_size = (dpi_page_size + PAGE_SIZE - 1) & ~(PAGE_SIZE - 1);
2680 	dpi_bit_shift = ilog2(dpi_page_size / 4096);
2681 	dpi_count = pwm_region_size / dpi_page_size;
2682 
2683 	min_dpis = p_hwfn->pf_params.rdma_pf_params.min_dpis;
2684 	min_dpis = max_t(u32, QED_MIN_DPIS, min_dpis);
2685 
2686 	p_hwfn->dpi_size = dpi_page_size;
2687 	p_hwfn->dpi_count = dpi_count;
2688 
2689 	qed_wr(p_hwfn, p_ptt, DORQ_REG_PF_DPI_BIT_SHIFT, dpi_bit_shift);
2690 
2691 	if (dpi_count < min_dpis)
2692 		return -EINVAL;
2693 
2694 	return 0;
2695 }
2696 
2697 enum QED_ROCE_EDPM_MODE {
2698 	QED_ROCE_EDPM_MODE_ENABLE = 0,
2699 	QED_ROCE_EDPM_MODE_FORCE_ON = 1,
2700 	QED_ROCE_EDPM_MODE_DISABLE = 2,
2701 };
2702 
2703 bool qed_edpm_enabled(struct qed_hwfn *p_hwfn)
2704 {
2705 	if (p_hwfn->dcbx_no_edpm || p_hwfn->db_bar_no_edpm)
2706 		return false;
2707 
2708 	return true;
2709 }
2710 
2711 static int
2712 qed_hw_init_pf_doorbell_bar(struct qed_hwfn *p_hwfn, struct qed_ptt *p_ptt)
2713 {
2714 	u32 pwm_regsize, norm_regsize;
2715 	u32 non_pwm_conn, min_addr_reg1;
2716 	u32 db_bar_size, n_cpus = 1;
2717 	u32 roce_edpm_mode;
2718 	u32 pf_dems_shift;
2719 	int rc = 0;
2720 	u8 cond;
2721 
2722 	db_bar_size = qed_hw_bar_size(p_hwfn, p_ptt, BAR_ID_1);
2723 	if (p_hwfn->cdev->num_hwfns > 1)
2724 		db_bar_size /= 2;
2725 
2726 	/* Calculate doorbell regions */
2727 	non_pwm_conn = qed_cxt_get_proto_cid_start(p_hwfn, PROTOCOLID_CORE) +
2728 		       qed_cxt_get_proto_cid_count(p_hwfn, PROTOCOLID_CORE,
2729 						   NULL) +
2730 		       qed_cxt_get_proto_cid_count(p_hwfn, PROTOCOLID_ETH,
2731 						   NULL);
2732 	norm_regsize = roundup(QED_PF_DEMS_SIZE * non_pwm_conn, PAGE_SIZE);
2733 	min_addr_reg1 = norm_regsize / 4096;
2734 	pwm_regsize = db_bar_size - norm_regsize;
2735 
2736 	/* Check that the normal and PWM sizes are valid */
2737 	if (db_bar_size < norm_regsize) {
2738 		DP_ERR(p_hwfn->cdev,
2739 		       "Doorbell BAR size 0x%x is too small (normal region is 0x%0x )\n",
2740 		       db_bar_size, norm_regsize);
2741 		return -EINVAL;
2742 	}
2743 
2744 	if (pwm_regsize < QED_MIN_PWM_REGION) {
2745 		DP_ERR(p_hwfn->cdev,
2746 		       "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",
2747 		       pwm_regsize,
2748 		       QED_MIN_PWM_REGION, db_bar_size, norm_regsize);
2749 		return -EINVAL;
2750 	}
2751 
2752 	/* Calculate number of DPIs */
2753 	roce_edpm_mode = p_hwfn->pf_params.rdma_pf_params.roce_edpm_mode;
2754 	if ((roce_edpm_mode == QED_ROCE_EDPM_MODE_ENABLE) ||
2755 	    ((roce_edpm_mode == QED_ROCE_EDPM_MODE_FORCE_ON))) {
2756 		/* Either EDPM is mandatory, or we are attempting to allocate a
2757 		 * WID per CPU.
2758 		 */
2759 		n_cpus = num_present_cpus();
2760 		rc = qed_hw_init_dpi_size(p_hwfn, p_ptt, pwm_regsize, n_cpus);
2761 	}
2762 
2763 	cond = (rc && (roce_edpm_mode == QED_ROCE_EDPM_MODE_ENABLE)) ||
2764 	       (roce_edpm_mode == QED_ROCE_EDPM_MODE_DISABLE);
2765 	if (cond || p_hwfn->dcbx_no_edpm) {
2766 		/* Either EDPM is disabled from user configuration, or it is
2767 		 * disabled via DCBx, or it is not mandatory and we failed to
2768 		 * allocated a WID per CPU.
2769 		 */
2770 		n_cpus = 1;
2771 		rc = qed_hw_init_dpi_size(p_hwfn, p_ptt, pwm_regsize, n_cpus);
2772 
2773 		if (cond)
2774 			qed_rdma_dpm_bar(p_hwfn, p_ptt);
2775 	}
2776 
2777 	p_hwfn->wid_count = (u16) n_cpus;
2778 
2779 	DP_INFO(p_hwfn,
2780 		"doorbell bar: normal_region_size=%d, pwm_region_size=%d, dpi_size=%d, dpi_count=%d, roce_edpm=%s, page_size=%lu\n",
2781 		norm_regsize,
2782 		pwm_regsize,
2783 		p_hwfn->dpi_size,
2784 		p_hwfn->dpi_count,
2785 		(!qed_edpm_enabled(p_hwfn)) ?
2786 		"disabled" : "enabled", PAGE_SIZE);
2787 
2788 	if (rc) {
2789 		DP_ERR(p_hwfn,
2790 		       "Failed to allocate enough DPIs. Allocated %d but the current minimum is %d.\n",
2791 		       p_hwfn->dpi_count,
2792 		       p_hwfn->pf_params.rdma_pf_params.min_dpis);
2793 		return -EINVAL;
2794 	}
2795 
2796 	p_hwfn->dpi_start_offset = norm_regsize;
2797 
2798 	/* DEMS size is configured log2 of DWORDs, hence the division by 4 */
2799 	pf_dems_shift = ilog2(QED_PF_DEMS_SIZE / 4);
2800 	qed_wr(p_hwfn, p_ptt, DORQ_REG_PF_ICID_BIT_SHIFT_NORM, pf_dems_shift);
2801 	qed_wr(p_hwfn, p_ptt, DORQ_REG_PF_MIN_ADDR_REG1, min_addr_reg1);
2802 
2803 	return 0;
2804 }
2805 
2806 static int qed_hw_init_port(struct qed_hwfn *p_hwfn,
2807 			    struct qed_ptt *p_ptt, int hw_mode)
2808 {
2809 	int rc = 0;
2810 
2811 	/* In CMT the gate should be cleared by the 2nd hwfn */
2812 	if (!QED_IS_CMT(p_hwfn->cdev) || !IS_LEAD_HWFN(p_hwfn))
2813 		STORE_RT_REG(p_hwfn, NIG_REG_BRB_GATE_DNTFWD_PORT_RT_OFFSET, 0);
2814 
2815 	rc = qed_init_run(p_hwfn, p_ptt, PHASE_PORT, p_hwfn->port_id, hw_mode);
2816 	if (rc)
2817 		return rc;
2818 
2819 	qed_wr(p_hwfn, p_ptt, PGLUE_B_REG_MASTER_WRITE_PAD_ENABLE, 0);
2820 
2821 	return 0;
2822 }
2823 
2824 static int qed_hw_init_pf(struct qed_hwfn *p_hwfn,
2825 			  struct qed_ptt *p_ptt,
2826 			  struct qed_tunnel_info *p_tunn,
2827 			  int hw_mode,
2828 			  bool b_hw_start,
2829 			  enum qed_int_mode int_mode,
2830 			  bool allow_npar_tx_switch)
2831 {
2832 	u8 rel_pf_id = p_hwfn->rel_pf_id;
2833 	int rc = 0;
2834 
2835 	if (p_hwfn->mcp_info) {
2836 		struct qed_mcp_function_info *p_info;
2837 
2838 		p_info = &p_hwfn->mcp_info->func_info;
2839 		if (p_info->bandwidth_min)
2840 			p_hwfn->qm_info.pf_wfq = p_info->bandwidth_min;
2841 
2842 		/* Update rate limit once we'll actually have a link */
2843 		p_hwfn->qm_info.pf_rl = 100000;
2844 	}
2845 
2846 	qed_cxt_hw_init_pf(p_hwfn, p_ptt);
2847 
2848 	qed_int_igu_init_rt(p_hwfn);
2849 
2850 	/* Set VLAN in NIG if needed */
2851 	if (hw_mode & BIT(MODE_MF_SD)) {
2852 		DP_VERBOSE(p_hwfn, NETIF_MSG_HW, "Configuring LLH_FUNC_TAG\n");
2853 		STORE_RT_REG(p_hwfn, NIG_REG_LLH_FUNC_TAG_EN_RT_OFFSET, 1);
2854 		STORE_RT_REG(p_hwfn, NIG_REG_LLH_FUNC_TAG_VALUE_RT_OFFSET,
2855 			     p_hwfn->hw_info.ovlan);
2856 
2857 		DP_VERBOSE(p_hwfn, NETIF_MSG_HW,
2858 			   "Configuring LLH_FUNC_FILTER_HDR_SEL\n");
2859 		STORE_RT_REG(p_hwfn, NIG_REG_LLH_FUNC_FILTER_HDR_SEL_RT_OFFSET,
2860 			     1);
2861 	}
2862 
2863 	/* Enable classification by MAC if needed */
2864 	if (hw_mode & BIT(MODE_MF_SI)) {
2865 		DP_VERBOSE(p_hwfn, NETIF_MSG_HW,
2866 			   "Configuring TAGMAC_CLS_TYPE\n");
2867 		STORE_RT_REG(p_hwfn,
2868 			     NIG_REG_LLH_FUNC_TAGMAC_CLS_TYPE_RT_OFFSET, 1);
2869 	}
2870 
2871 	/* Protocol Configuration */
2872 	STORE_RT_REG(p_hwfn, PRS_REG_SEARCH_TCP_RT_OFFSET,
2873 		     (p_hwfn->hw_info.personality == QED_PCI_ISCSI) ? 1 : 0);
2874 	STORE_RT_REG(p_hwfn, PRS_REG_SEARCH_FCOE_RT_OFFSET,
2875 		     (p_hwfn->hw_info.personality == QED_PCI_FCOE) ? 1 : 0);
2876 	STORE_RT_REG(p_hwfn, PRS_REG_SEARCH_ROCE_RT_OFFSET, 0);
2877 
2878 	/* Sanity check before the PF init sequence that uses DMAE */
2879 	rc = qed_dmae_sanity(p_hwfn, p_ptt, "pf_phase");
2880 	if (rc)
2881 		return rc;
2882 
2883 	/* PF Init sequence */
2884 	rc = qed_init_run(p_hwfn, p_ptt, PHASE_PF, rel_pf_id, hw_mode);
2885 	if (rc)
2886 		return rc;
2887 
2888 	/* QM_PF Init sequence (may be invoked separately e.g. for DCB) */
2889 	rc = qed_init_run(p_hwfn, p_ptt, PHASE_QM_PF, rel_pf_id, hw_mode);
2890 	if (rc)
2891 		return rc;
2892 
2893 	/* Pure runtime initializations - directly to the HW  */
2894 	qed_int_igu_init_pure_rt(p_hwfn, p_ptt, true, true);
2895 
2896 	rc = qed_hw_init_pf_doorbell_bar(p_hwfn, p_ptt);
2897 	if (rc)
2898 		return rc;
2899 
2900 	/* Use the leading hwfn since in CMT only NIG #0 is operational */
2901 	if (IS_LEAD_HWFN(p_hwfn)) {
2902 		rc = qed_llh_hw_init_pf(p_hwfn, p_ptt);
2903 		if (rc)
2904 			return rc;
2905 	}
2906 
2907 	if (b_hw_start) {
2908 		/* enable interrupts */
2909 		qed_int_igu_enable(p_hwfn, p_ptt, int_mode);
2910 
2911 		/* send function start command */
2912 		rc = qed_sp_pf_start(p_hwfn, p_ptt, p_tunn,
2913 				     allow_npar_tx_switch);
2914 		if (rc) {
2915 			DP_NOTICE(p_hwfn, "Function start ramrod failed\n");
2916 			return rc;
2917 		}
2918 		if (p_hwfn->hw_info.personality == QED_PCI_FCOE) {
2919 			qed_wr(p_hwfn, p_ptt, PRS_REG_SEARCH_TAG1, BIT(2));
2920 			qed_wr(p_hwfn, p_ptt,
2921 			       PRS_REG_PKT_LEN_STAT_TAGS_NOT_COUNTED_FIRST,
2922 			       0x100);
2923 		}
2924 	}
2925 	return rc;
2926 }
2927 
2928 int qed_pglueb_set_pfid_enable(struct qed_hwfn *p_hwfn,
2929 			       struct qed_ptt *p_ptt, bool b_enable)
2930 {
2931 	u32 delay_idx = 0, val, set_val = b_enable ? 1 : 0;
2932 
2933 	/* Configure the PF's internal FID_enable for master transactions */
2934 	qed_wr(p_hwfn, p_ptt, PGLUE_B_REG_INTERNAL_PFID_ENABLE_MASTER, set_val);
2935 
2936 	/* Wait until value is set - try for 1 second every 50us */
2937 	for (delay_idx = 0; delay_idx < 20000; delay_idx++) {
2938 		val = qed_rd(p_hwfn, p_ptt,
2939 			     PGLUE_B_REG_INTERNAL_PFID_ENABLE_MASTER);
2940 		if (val == set_val)
2941 			break;
2942 
2943 		usleep_range(50, 60);
2944 	}
2945 
2946 	if (val != set_val) {
2947 		DP_NOTICE(p_hwfn,
2948 			  "PFID_ENABLE_MASTER wasn't changed after a second\n");
2949 		return -EAGAIN;
2950 	}
2951 
2952 	return 0;
2953 }
2954 
2955 static void qed_reset_mb_shadow(struct qed_hwfn *p_hwfn,
2956 				struct qed_ptt *p_main_ptt)
2957 {
2958 	/* Read shadow of current MFW mailbox */
2959 	qed_mcp_read_mb(p_hwfn, p_main_ptt);
2960 	memcpy(p_hwfn->mcp_info->mfw_mb_shadow,
2961 	       p_hwfn->mcp_info->mfw_mb_cur, p_hwfn->mcp_info->mfw_mb_length);
2962 }
2963 
2964 static void
2965 qed_fill_load_req_params(struct qed_load_req_params *p_load_req,
2966 			 struct qed_drv_load_params *p_drv_load)
2967 {
2968 	memset(p_load_req, 0, sizeof(*p_load_req));
2969 
2970 	p_load_req->drv_role = p_drv_load->is_crash_kernel ?
2971 			       QED_DRV_ROLE_KDUMP : QED_DRV_ROLE_OS;
2972 	p_load_req->timeout_val = p_drv_load->mfw_timeout_val;
2973 	p_load_req->avoid_eng_reset = p_drv_load->avoid_eng_reset;
2974 	p_load_req->override_force_load = p_drv_load->override_force_load;
2975 }
2976 
2977 static int qed_vf_start(struct qed_hwfn *p_hwfn,
2978 			struct qed_hw_init_params *p_params)
2979 {
2980 	if (p_params->p_tunn) {
2981 		qed_vf_set_vf_start_tunn_update_param(p_params->p_tunn);
2982 		qed_vf_pf_tunnel_param_update(p_hwfn, p_params->p_tunn);
2983 	}
2984 
2985 	p_hwfn->b_int_enabled = true;
2986 
2987 	return 0;
2988 }
2989 
2990 static void qed_pglueb_clear_err(struct qed_hwfn *p_hwfn, struct qed_ptt *p_ptt)
2991 {
2992 	qed_wr(p_hwfn, p_ptt, PGLUE_B_REG_WAS_ERROR_PF_31_0_CLR,
2993 	       BIT(p_hwfn->abs_pf_id));
2994 }
2995 
2996 int qed_hw_init(struct qed_dev *cdev, struct qed_hw_init_params *p_params)
2997 {
2998 	struct qed_load_req_params load_req_params;
2999 	u32 load_code, resp, param, drv_mb_param;
3000 	bool b_default_mtu = true;
3001 	struct qed_hwfn *p_hwfn;
3002 	int rc = 0, i;
3003 	u16 ether_type;
3004 
3005 	if ((p_params->int_mode == QED_INT_MODE_MSI) && (cdev->num_hwfns > 1)) {
3006 		DP_NOTICE(cdev, "MSI mode is not supported for CMT devices\n");
3007 		return -EINVAL;
3008 	}
3009 
3010 	if (IS_PF(cdev)) {
3011 		rc = qed_init_fw_data(cdev, p_params->bin_fw_data);
3012 		if (rc)
3013 			return rc;
3014 	}
3015 
3016 	for_each_hwfn(cdev, i) {
3017 		p_hwfn = &cdev->hwfns[i];
3018 
3019 		/* If management didn't provide a default, set one of our own */
3020 		if (!p_hwfn->hw_info.mtu) {
3021 			p_hwfn->hw_info.mtu = 1500;
3022 			b_default_mtu = false;
3023 		}
3024 
3025 		if (IS_VF(cdev)) {
3026 			qed_vf_start(p_hwfn, p_params);
3027 			continue;
3028 		}
3029 
3030 		rc = qed_calc_hw_mode(p_hwfn);
3031 		if (rc)
3032 			return rc;
3033 
3034 		if (IS_PF(cdev) && (test_bit(QED_MF_8021Q_TAGGING,
3035 					     &cdev->mf_bits) ||
3036 				    test_bit(QED_MF_8021AD_TAGGING,
3037 					     &cdev->mf_bits))) {
3038 			if (test_bit(QED_MF_8021Q_TAGGING, &cdev->mf_bits))
3039 				ether_type = ETH_P_8021Q;
3040 			else
3041 				ether_type = ETH_P_8021AD;
3042 			STORE_RT_REG(p_hwfn, PRS_REG_TAG_ETHERTYPE_0_RT_OFFSET,
3043 				     ether_type);
3044 			STORE_RT_REG(p_hwfn, NIG_REG_TAG_ETHERTYPE_0_RT_OFFSET,
3045 				     ether_type);
3046 			STORE_RT_REG(p_hwfn, PBF_REG_TAG_ETHERTYPE_0_RT_OFFSET,
3047 				     ether_type);
3048 			STORE_RT_REG(p_hwfn, DORQ_REG_TAG1_ETHERTYPE_RT_OFFSET,
3049 				     ether_type);
3050 		}
3051 
3052 		qed_fill_load_req_params(&load_req_params,
3053 					 p_params->p_drv_load_params);
3054 		rc = qed_mcp_load_req(p_hwfn, p_hwfn->p_main_ptt,
3055 				      &load_req_params);
3056 		if (rc) {
3057 			DP_NOTICE(p_hwfn, "Failed sending a LOAD_REQ command\n");
3058 			return rc;
3059 		}
3060 
3061 		load_code = load_req_params.load_code;
3062 		DP_VERBOSE(p_hwfn, QED_MSG_SP,
3063 			   "Load request was sent. Load code: 0x%x\n",
3064 			   load_code);
3065 
3066 		/* Only relevant for recovery:
3067 		 * Clear the indication after LOAD_REQ is responded by the MFW.
3068 		 */
3069 		cdev->recov_in_prog = false;
3070 
3071 		qed_mcp_set_capabilities(p_hwfn, p_hwfn->p_main_ptt);
3072 
3073 		qed_reset_mb_shadow(p_hwfn, p_hwfn->p_main_ptt);
3074 
3075 		/* Clean up chip from previous driver if such remains exist.
3076 		 * This is not needed when the PF is the first one on the
3077 		 * engine, since afterwards we are going to init the FW.
3078 		 */
3079 		if (load_code != FW_MSG_CODE_DRV_LOAD_ENGINE) {
3080 			rc = qed_final_cleanup(p_hwfn, p_hwfn->p_main_ptt,
3081 					       p_hwfn->rel_pf_id, false);
3082 			if (rc) {
3083 				DP_NOTICE(p_hwfn, "Final cleanup failed\n");
3084 				goto load_err;
3085 			}
3086 		}
3087 
3088 		/* Log and clear previous pglue_b errors if such exist */
3089 		qed_pglueb_rbc_attn_handler(p_hwfn, p_hwfn->p_main_ptt);
3090 
3091 		/* Enable the PF's internal FID_enable in the PXP */
3092 		rc = qed_pglueb_set_pfid_enable(p_hwfn, p_hwfn->p_main_ptt,
3093 						true);
3094 		if (rc)
3095 			goto load_err;
3096 
3097 		/* Clear the pglue_b was_error indication.
3098 		 * In E4 it must be done after the BME and the internal
3099 		 * FID_enable for the PF are set, since VDMs may cause the
3100 		 * indication to be set again.
3101 		 */
3102 		qed_pglueb_clear_err(p_hwfn, p_hwfn->p_main_ptt);
3103 
3104 		switch (load_code) {
3105 		case FW_MSG_CODE_DRV_LOAD_ENGINE:
3106 			rc = qed_hw_init_common(p_hwfn, p_hwfn->p_main_ptt,
3107 						p_hwfn->hw_info.hw_mode);
3108 			if (rc)
3109 				break;
3110 		/* Fall through */
3111 		case FW_MSG_CODE_DRV_LOAD_PORT:
3112 			rc = qed_hw_init_port(p_hwfn, p_hwfn->p_main_ptt,
3113 					      p_hwfn->hw_info.hw_mode);
3114 			if (rc)
3115 				break;
3116 
3117 		/* Fall through */
3118 		case FW_MSG_CODE_DRV_LOAD_FUNCTION:
3119 			rc = qed_hw_init_pf(p_hwfn, p_hwfn->p_main_ptt,
3120 					    p_params->p_tunn,
3121 					    p_hwfn->hw_info.hw_mode,
3122 					    p_params->b_hw_start,
3123 					    p_params->int_mode,
3124 					    p_params->allow_npar_tx_switch);
3125 			break;
3126 		default:
3127 			DP_NOTICE(p_hwfn,
3128 				  "Unexpected load code [0x%08x]", load_code);
3129 			rc = -EINVAL;
3130 			break;
3131 		}
3132 
3133 		if (rc) {
3134 			DP_NOTICE(p_hwfn,
3135 				  "init phase failed for loadcode 0x%x (rc %d)\n",
3136 				  load_code, rc);
3137 			goto load_err;
3138 		}
3139 
3140 		rc = qed_mcp_load_done(p_hwfn, p_hwfn->p_main_ptt);
3141 		if (rc)
3142 			return rc;
3143 
3144 		/* send DCBX attention request command */
3145 		DP_VERBOSE(p_hwfn,
3146 			   QED_MSG_DCB,
3147 			   "sending phony dcbx set command to trigger DCBx attention handling\n");
3148 		rc = qed_mcp_cmd(p_hwfn, p_hwfn->p_main_ptt,
3149 				 DRV_MSG_CODE_SET_DCBX,
3150 				 1 << DRV_MB_PARAM_DCBX_NOTIFY_SHIFT,
3151 				 &resp, &param);
3152 		if (rc) {
3153 			DP_NOTICE(p_hwfn,
3154 				  "Failed to send DCBX attention request\n");
3155 			return rc;
3156 		}
3157 
3158 		p_hwfn->hw_init_done = true;
3159 	}
3160 
3161 	if (IS_PF(cdev)) {
3162 		p_hwfn = QED_LEADING_HWFN(cdev);
3163 
3164 		/* Get pre-negotiated values for stag, bandwidth etc. */
3165 		DP_VERBOSE(p_hwfn,
3166 			   QED_MSG_SPQ,
3167 			   "Sending GET_OEM_UPDATES command to trigger stag/bandwidth attention handling\n");
3168 		drv_mb_param = 1 << DRV_MB_PARAM_DUMMY_OEM_UPDATES_OFFSET;
3169 		rc = qed_mcp_cmd(p_hwfn, p_hwfn->p_main_ptt,
3170 				 DRV_MSG_CODE_GET_OEM_UPDATES,
3171 				 drv_mb_param, &resp, &param);
3172 		if (rc)
3173 			DP_NOTICE(p_hwfn,
3174 				  "Failed to send GET_OEM_UPDATES attention request\n");
3175 
3176 		drv_mb_param = STORM_FW_VERSION;
3177 		rc = qed_mcp_cmd(p_hwfn, p_hwfn->p_main_ptt,
3178 				 DRV_MSG_CODE_OV_UPDATE_STORM_FW_VER,
3179 				 drv_mb_param, &load_code, &param);
3180 		if (rc)
3181 			DP_INFO(p_hwfn, "Failed to update firmware version\n");
3182 
3183 		if (!b_default_mtu) {
3184 			rc = qed_mcp_ov_update_mtu(p_hwfn, p_hwfn->p_main_ptt,
3185 						   p_hwfn->hw_info.mtu);
3186 			if (rc)
3187 				DP_INFO(p_hwfn,
3188 					"Failed to update default mtu\n");
3189 		}
3190 
3191 		rc = qed_mcp_ov_update_driver_state(p_hwfn,
3192 						    p_hwfn->p_main_ptt,
3193 						  QED_OV_DRIVER_STATE_DISABLED);
3194 		if (rc)
3195 			DP_INFO(p_hwfn, "Failed to update driver state\n");
3196 
3197 		rc = qed_mcp_ov_update_eswitch(p_hwfn, p_hwfn->p_main_ptt,
3198 					       QED_OV_ESWITCH_NONE);
3199 		if (rc)
3200 			DP_INFO(p_hwfn, "Failed to update eswitch mode\n");
3201 	}
3202 
3203 	return 0;
3204 
3205 load_err:
3206 	/* The MFW load lock should be released also when initialization fails.
3207 	 */
3208 	qed_mcp_load_done(p_hwfn, p_hwfn->p_main_ptt);
3209 	return rc;
3210 }
3211 
3212 #define QED_HW_STOP_RETRY_LIMIT (10)
3213 static void qed_hw_timers_stop(struct qed_dev *cdev,
3214 			       struct qed_hwfn *p_hwfn, struct qed_ptt *p_ptt)
3215 {
3216 	int i;
3217 
3218 	/* close timers */
3219 	qed_wr(p_hwfn, p_ptt, TM_REG_PF_ENABLE_CONN, 0x0);
3220 	qed_wr(p_hwfn, p_ptt, TM_REG_PF_ENABLE_TASK, 0x0);
3221 
3222 	if (cdev->recov_in_prog)
3223 		return;
3224 
3225 	for (i = 0; i < QED_HW_STOP_RETRY_LIMIT; i++) {
3226 		if ((!qed_rd(p_hwfn, p_ptt,
3227 			     TM_REG_PF_SCAN_ACTIVE_CONN)) &&
3228 		    (!qed_rd(p_hwfn, p_ptt, TM_REG_PF_SCAN_ACTIVE_TASK)))
3229 			break;
3230 
3231 		/* Dependent on number of connection/tasks, possibly
3232 		 * 1ms sleep is required between polls
3233 		 */
3234 		usleep_range(1000, 2000);
3235 	}
3236 
3237 	if (i < QED_HW_STOP_RETRY_LIMIT)
3238 		return;
3239 
3240 	DP_NOTICE(p_hwfn,
3241 		  "Timers linear scans are not over [Connection %02x Tasks %02x]\n",
3242 		  (u8)qed_rd(p_hwfn, p_ptt, TM_REG_PF_SCAN_ACTIVE_CONN),
3243 		  (u8)qed_rd(p_hwfn, p_ptt, TM_REG_PF_SCAN_ACTIVE_TASK));
3244 }
3245 
3246 void qed_hw_timers_stop_all(struct qed_dev *cdev)
3247 {
3248 	int j;
3249 
3250 	for_each_hwfn(cdev, j) {
3251 		struct qed_hwfn *p_hwfn = &cdev->hwfns[j];
3252 		struct qed_ptt *p_ptt = p_hwfn->p_main_ptt;
3253 
3254 		qed_hw_timers_stop(cdev, p_hwfn, p_ptt);
3255 	}
3256 }
3257 
3258 int qed_hw_stop(struct qed_dev *cdev)
3259 {
3260 	struct qed_hwfn *p_hwfn;
3261 	struct qed_ptt *p_ptt;
3262 	int rc, rc2 = 0;
3263 	int j;
3264 
3265 	for_each_hwfn(cdev, j) {
3266 		p_hwfn = &cdev->hwfns[j];
3267 		p_ptt = p_hwfn->p_main_ptt;
3268 
3269 		DP_VERBOSE(p_hwfn, NETIF_MSG_IFDOWN, "Stopping hw/fw\n");
3270 
3271 		if (IS_VF(cdev)) {
3272 			qed_vf_pf_int_cleanup(p_hwfn);
3273 			rc = qed_vf_pf_reset(p_hwfn);
3274 			if (rc) {
3275 				DP_NOTICE(p_hwfn,
3276 					  "qed_vf_pf_reset failed. rc = %d.\n",
3277 					  rc);
3278 				rc2 = -EINVAL;
3279 			}
3280 			continue;
3281 		}
3282 
3283 		/* mark the hw as uninitialized... */
3284 		p_hwfn->hw_init_done = false;
3285 
3286 		/* Send unload command to MCP */
3287 		if (!cdev->recov_in_prog) {
3288 			rc = qed_mcp_unload_req(p_hwfn, p_ptt);
3289 			if (rc) {
3290 				DP_NOTICE(p_hwfn,
3291 					  "Failed sending a UNLOAD_REQ command. rc = %d.\n",
3292 					  rc);
3293 				rc2 = -EINVAL;
3294 			}
3295 		}
3296 
3297 		qed_slowpath_irq_sync(p_hwfn);
3298 
3299 		/* After this point no MFW attentions are expected, e.g. prevent
3300 		 * race between pf stop and dcbx pf update.
3301 		 */
3302 		rc = qed_sp_pf_stop(p_hwfn);
3303 		if (rc) {
3304 			DP_NOTICE(p_hwfn,
3305 				  "Failed to close PF against FW [rc = %d]. Continue to stop HW to prevent illegal host access by the device.\n",
3306 				  rc);
3307 			rc2 = -EINVAL;
3308 		}
3309 
3310 		qed_wr(p_hwfn, p_ptt,
3311 		       NIG_REG_RX_LLH_BRB_GATE_DNTFWD_PERPF, 0x1);
3312 
3313 		qed_wr(p_hwfn, p_ptt, PRS_REG_SEARCH_TCP, 0x0);
3314 		qed_wr(p_hwfn, p_ptt, PRS_REG_SEARCH_UDP, 0x0);
3315 		qed_wr(p_hwfn, p_ptt, PRS_REG_SEARCH_FCOE, 0x0);
3316 		qed_wr(p_hwfn, p_ptt, PRS_REG_SEARCH_ROCE, 0x0);
3317 		qed_wr(p_hwfn, p_ptt, PRS_REG_SEARCH_OPENFLOW, 0x0);
3318 
3319 		qed_hw_timers_stop(cdev, p_hwfn, p_ptt);
3320 
3321 		/* Disable Attention Generation */
3322 		qed_int_igu_disable_int(p_hwfn, p_ptt);
3323 
3324 		qed_wr(p_hwfn, p_ptt, IGU_REG_LEADING_EDGE_LATCH, 0);
3325 		qed_wr(p_hwfn, p_ptt, IGU_REG_TRAILING_EDGE_LATCH, 0);
3326 
3327 		qed_int_igu_init_pure_rt(p_hwfn, p_ptt, false, true);
3328 
3329 		/* Need to wait 1ms to guarantee SBs are cleared */
3330 		usleep_range(1000, 2000);
3331 
3332 		/* Disable PF in HW blocks */
3333 		qed_wr(p_hwfn, p_ptt, DORQ_REG_PF_DB_ENABLE, 0);
3334 		qed_wr(p_hwfn, p_ptt, QM_REG_PF_EN, 0);
3335 
3336 		if (IS_LEAD_HWFN(p_hwfn) &&
3337 		    test_bit(QED_MF_LLH_MAC_CLSS, &cdev->mf_bits) &&
3338 		    !QED_IS_FCOE_PERSONALITY(p_hwfn))
3339 			qed_llh_remove_mac_filter(cdev, 0,
3340 						  p_hwfn->hw_info.hw_mac_addr);
3341 
3342 		if (!cdev->recov_in_prog) {
3343 			rc = qed_mcp_unload_done(p_hwfn, p_ptt);
3344 			if (rc) {
3345 				DP_NOTICE(p_hwfn,
3346 					  "Failed sending a UNLOAD_DONE command. rc = %d.\n",
3347 					  rc);
3348 				rc2 = -EINVAL;
3349 			}
3350 		}
3351 	}
3352 
3353 	if (IS_PF(cdev) && !cdev->recov_in_prog) {
3354 		p_hwfn = QED_LEADING_HWFN(cdev);
3355 		p_ptt = QED_LEADING_HWFN(cdev)->p_main_ptt;
3356 
3357 		/* Clear the PF's internal FID_enable in the PXP.
3358 		 * In CMT this should only be done for first hw-function, and
3359 		 * only after all transactions have stopped for all active
3360 		 * hw-functions.
3361 		 */
3362 		rc = qed_pglueb_set_pfid_enable(p_hwfn, p_ptt, false);
3363 		if (rc) {
3364 			DP_NOTICE(p_hwfn,
3365 				  "qed_pglueb_set_pfid_enable() failed. rc = %d.\n",
3366 				  rc);
3367 			rc2 = -EINVAL;
3368 		}
3369 	}
3370 
3371 	return rc2;
3372 }
3373 
3374 int qed_hw_stop_fastpath(struct qed_dev *cdev)
3375 {
3376 	int j;
3377 
3378 	for_each_hwfn(cdev, j) {
3379 		struct qed_hwfn *p_hwfn = &cdev->hwfns[j];
3380 		struct qed_ptt *p_ptt;
3381 
3382 		if (IS_VF(cdev)) {
3383 			qed_vf_pf_int_cleanup(p_hwfn);
3384 			continue;
3385 		}
3386 		p_ptt = qed_ptt_acquire(p_hwfn);
3387 		if (!p_ptt)
3388 			return -EAGAIN;
3389 
3390 		DP_VERBOSE(p_hwfn,
3391 			   NETIF_MSG_IFDOWN, "Shutting down the fastpath\n");
3392 
3393 		qed_wr(p_hwfn, p_ptt,
3394 		       NIG_REG_RX_LLH_BRB_GATE_DNTFWD_PERPF, 0x1);
3395 
3396 		qed_wr(p_hwfn, p_ptt, PRS_REG_SEARCH_TCP, 0x0);
3397 		qed_wr(p_hwfn, p_ptt, PRS_REG_SEARCH_UDP, 0x0);
3398 		qed_wr(p_hwfn, p_ptt, PRS_REG_SEARCH_FCOE, 0x0);
3399 		qed_wr(p_hwfn, p_ptt, PRS_REG_SEARCH_ROCE, 0x0);
3400 		qed_wr(p_hwfn, p_ptt, PRS_REG_SEARCH_OPENFLOW, 0x0);
3401 
3402 		qed_int_igu_init_pure_rt(p_hwfn, p_ptt, false, false);
3403 
3404 		/* Need to wait 1ms to guarantee SBs are cleared */
3405 		usleep_range(1000, 2000);
3406 		qed_ptt_release(p_hwfn, p_ptt);
3407 	}
3408 
3409 	return 0;
3410 }
3411 
3412 int qed_hw_start_fastpath(struct qed_hwfn *p_hwfn)
3413 {
3414 	struct qed_ptt *p_ptt;
3415 
3416 	if (IS_VF(p_hwfn->cdev))
3417 		return 0;
3418 
3419 	p_ptt = qed_ptt_acquire(p_hwfn);
3420 	if (!p_ptt)
3421 		return -EAGAIN;
3422 
3423 	if (p_hwfn->p_rdma_info &&
3424 	    p_hwfn->p_rdma_info->active && p_hwfn->b_rdma_enabled_in_prs)
3425 		qed_wr(p_hwfn, p_ptt, p_hwfn->rdma_prs_search_reg, 0x1);
3426 
3427 	/* Re-open incoming traffic */
3428 	qed_wr(p_hwfn, p_ptt, NIG_REG_RX_LLH_BRB_GATE_DNTFWD_PERPF, 0x0);
3429 	qed_ptt_release(p_hwfn, p_ptt);
3430 
3431 	return 0;
3432 }
3433 
3434 /* Free hwfn memory and resources acquired in hw_hwfn_prepare */
3435 static void qed_hw_hwfn_free(struct qed_hwfn *p_hwfn)
3436 {
3437 	qed_ptt_pool_free(p_hwfn);
3438 	kfree(p_hwfn->hw_info.p_igu_info);
3439 	p_hwfn->hw_info.p_igu_info = NULL;
3440 }
3441 
3442 /* Setup bar access */
3443 static void qed_hw_hwfn_prepare(struct qed_hwfn *p_hwfn)
3444 {
3445 	/* clear indirect access */
3446 	if (QED_IS_AH(p_hwfn->cdev)) {
3447 		qed_wr(p_hwfn, p_hwfn->p_main_ptt,
3448 		       PGLUE_B_REG_PGL_ADDR_E8_F0_K2, 0);
3449 		qed_wr(p_hwfn, p_hwfn->p_main_ptt,
3450 		       PGLUE_B_REG_PGL_ADDR_EC_F0_K2, 0);
3451 		qed_wr(p_hwfn, p_hwfn->p_main_ptt,
3452 		       PGLUE_B_REG_PGL_ADDR_F0_F0_K2, 0);
3453 		qed_wr(p_hwfn, p_hwfn->p_main_ptt,
3454 		       PGLUE_B_REG_PGL_ADDR_F4_F0_K2, 0);
3455 	} else {
3456 		qed_wr(p_hwfn, p_hwfn->p_main_ptt,
3457 		       PGLUE_B_REG_PGL_ADDR_88_F0_BB, 0);
3458 		qed_wr(p_hwfn, p_hwfn->p_main_ptt,
3459 		       PGLUE_B_REG_PGL_ADDR_8C_F0_BB, 0);
3460 		qed_wr(p_hwfn, p_hwfn->p_main_ptt,
3461 		       PGLUE_B_REG_PGL_ADDR_90_F0_BB, 0);
3462 		qed_wr(p_hwfn, p_hwfn->p_main_ptt,
3463 		       PGLUE_B_REG_PGL_ADDR_94_F0_BB, 0);
3464 	}
3465 
3466 	/* Clean previous pglue_b errors if such exist */
3467 	qed_pglueb_clear_err(p_hwfn, p_hwfn->p_main_ptt);
3468 
3469 	/* enable internal target-read */
3470 	qed_wr(p_hwfn, p_hwfn->p_main_ptt,
3471 	       PGLUE_B_REG_INTERNAL_PFID_ENABLE_TARGET_READ, 1);
3472 }
3473 
3474 static void get_function_id(struct qed_hwfn *p_hwfn)
3475 {
3476 	/* ME Register */
3477 	p_hwfn->hw_info.opaque_fid = (u16) REG_RD(p_hwfn,
3478 						  PXP_PF_ME_OPAQUE_ADDR);
3479 
3480 	p_hwfn->hw_info.concrete_fid = REG_RD(p_hwfn, PXP_PF_ME_CONCRETE_ADDR);
3481 
3482 	p_hwfn->abs_pf_id = (p_hwfn->hw_info.concrete_fid >> 16) & 0xf;
3483 	p_hwfn->rel_pf_id = GET_FIELD(p_hwfn->hw_info.concrete_fid,
3484 				      PXP_CONCRETE_FID_PFID);
3485 	p_hwfn->port_id = GET_FIELD(p_hwfn->hw_info.concrete_fid,
3486 				    PXP_CONCRETE_FID_PORT);
3487 
3488 	DP_VERBOSE(p_hwfn, NETIF_MSG_PROBE,
3489 		   "Read ME register: Concrete 0x%08x Opaque 0x%04x\n",
3490 		   p_hwfn->hw_info.concrete_fid, p_hwfn->hw_info.opaque_fid);
3491 }
3492 
3493 static void qed_hw_set_feat(struct qed_hwfn *p_hwfn)
3494 {
3495 	u32 *feat_num = p_hwfn->hw_info.feat_num;
3496 	struct qed_sb_cnt_info sb_cnt;
3497 	u32 non_l2_sbs = 0;
3498 
3499 	memset(&sb_cnt, 0, sizeof(sb_cnt));
3500 	qed_int_get_num_sbs(p_hwfn, &sb_cnt);
3501 
3502 	if (IS_ENABLED(CONFIG_QED_RDMA) &&
3503 	    QED_IS_RDMA_PERSONALITY(p_hwfn)) {
3504 		/* Roce CNQ each requires: 1 status block + 1 CNQ. We divide
3505 		 * the status blocks equally between L2 / RoCE but with
3506 		 * consideration as to how many l2 queues / cnqs we have.
3507 		 */
3508 		feat_num[QED_RDMA_CNQ] =
3509 			min_t(u32, sb_cnt.cnt / 2,
3510 			      RESC_NUM(p_hwfn, QED_RDMA_CNQ_RAM));
3511 
3512 		non_l2_sbs = feat_num[QED_RDMA_CNQ];
3513 	}
3514 	if (QED_IS_L2_PERSONALITY(p_hwfn)) {
3515 		/* Start by allocating VF queues, then PF's */
3516 		feat_num[QED_VF_L2_QUE] = min_t(u32,
3517 						RESC_NUM(p_hwfn, QED_L2_QUEUE),
3518 						sb_cnt.iov_cnt);
3519 		feat_num[QED_PF_L2_QUE] = min_t(u32,
3520 						sb_cnt.cnt - non_l2_sbs,
3521 						RESC_NUM(p_hwfn,
3522 							 QED_L2_QUEUE) -
3523 						FEAT_NUM(p_hwfn,
3524 							 QED_VF_L2_QUE));
3525 	}
3526 
3527 	if (QED_IS_FCOE_PERSONALITY(p_hwfn))
3528 		feat_num[QED_FCOE_CQ] =  min_t(u32, sb_cnt.cnt,
3529 					       RESC_NUM(p_hwfn,
3530 							QED_CMDQS_CQS));
3531 
3532 	if (QED_IS_ISCSI_PERSONALITY(p_hwfn))
3533 		feat_num[QED_ISCSI_CQ] = min_t(u32, sb_cnt.cnt,
3534 					       RESC_NUM(p_hwfn,
3535 							QED_CMDQS_CQS));
3536 	DP_VERBOSE(p_hwfn,
3537 		   NETIF_MSG_PROBE,
3538 		   "#PF_L2_QUEUES=%d VF_L2_QUEUES=%d #ROCE_CNQ=%d FCOE_CQ=%d ISCSI_CQ=%d #SBS=%d\n",
3539 		   (int)FEAT_NUM(p_hwfn, QED_PF_L2_QUE),
3540 		   (int)FEAT_NUM(p_hwfn, QED_VF_L2_QUE),
3541 		   (int)FEAT_NUM(p_hwfn, QED_RDMA_CNQ),
3542 		   (int)FEAT_NUM(p_hwfn, QED_FCOE_CQ),
3543 		   (int)FEAT_NUM(p_hwfn, QED_ISCSI_CQ),
3544 		   (int)sb_cnt.cnt);
3545 }
3546 
3547 const char *qed_hw_get_resc_name(enum qed_resources res_id)
3548 {
3549 	switch (res_id) {
3550 	case QED_L2_QUEUE:
3551 		return "L2_QUEUE";
3552 	case QED_VPORT:
3553 		return "VPORT";
3554 	case QED_RSS_ENG:
3555 		return "RSS_ENG";
3556 	case QED_PQ:
3557 		return "PQ";
3558 	case QED_RL:
3559 		return "RL";
3560 	case QED_MAC:
3561 		return "MAC";
3562 	case QED_VLAN:
3563 		return "VLAN";
3564 	case QED_RDMA_CNQ_RAM:
3565 		return "RDMA_CNQ_RAM";
3566 	case QED_ILT:
3567 		return "ILT";
3568 	case QED_LL2_QUEUE:
3569 		return "LL2_QUEUE";
3570 	case QED_CMDQS_CQS:
3571 		return "CMDQS_CQS";
3572 	case QED_RDMA_STATS_QUEUE:
3573 		return "RDMA_STATS_QUEUE";
3574 	case QED_BDQ:
3575 		return "BDQ";
3576 	case QED_SB:
3577 		return "SB";
3578 	default:
3579 		return "UNKNOWN_RESOURCE";
3580 	}
3581 }
3582 
3583 static int
3584 __qed_hw_set_soft_resc_size(struct qed_hwfn *p_hwfn,
3585 			    struct qed_ptt *p_ptt,
3586 			    enum qed_resources res_id,
3587 			    u32 resc_max_val, u32 *p_mcp_resp)
3588 {
3589 	int rc;
3590 
3591 	rc = qed_mcp_set_resc_max_val(p_hwfn, p_ptt, res_id,
3592 				      resc_max_val, p_mcp_resp);
3593 	if (rc) {
3594 		DP_NOTICE(p_hwfn,
3595 			  "MFW response failure for a max value setting of resource %d [%s]\n",
3596 			  res_id, qed_hw_get_resc_name(res_id));
3597 		return rc;
3598 	}
3599 
3600 	if (*p_mcp_resp != FW_MSG_CODE_RESOURCE_ALLOC_OK)
3601 		DP_INFO(p_hwfn,
3602 			"Failed to set the max value of resource %d [%s]. mcp_resp = 0x%08x.\n",
3603 			res_id, qed_hw_get_resc_name(res_id), *p_mcp_resp);
3604 
3605 	return 0;
3606 }
3607 
3608 static int
3609 qed_hw_set_soft_resc_size(struct qed_hwfn *p_hwfn, struct qed_ptt *p_ptt)
3610 {
3611 	bool b_ah = QED_IS_AH(p_hwfn->cdev);
3612 	u32 resc_max_val, mcp_resp;
3613 	u8 res_id;
3614 	int rc;
3615 
3616 	for (res_id = 0; res_id < QED_MAX_RESC; res_id++) {
3617 		switch (res_id) {
3618 		case QED_LL2_QUEUE:
3619 			resc_max_val = MAX_NUM_LL2_RX_QUEUES;
3620 			break;
3621 		case QED_RDMA_CNQ_RAM:
3622 			/* No need for a case for QED_CMDQS_CQS since
3623 			 * CNQ/CMDQS are the same resource.
3624 			 */
3625 			resc_max_val = NUM_OF_GLOBAL_QUEUES;
3626 			break;
3627 		case QED_RDMA_STATS_QUEUE:
3628 			resc_max_val = b_ah ? RDMA_NUM_STATISTIC_COUNTERS_K2
3629 			    : RDMA_NUM_STATISTIC_COUNTERS_BB;
3630 			break;
3631 		case QED_BDQ:
3632 			resc_max_val = BDQ_NUM_RESOURCES;
3633 			break;
3634 		default:
3635 			continue;
3636 		}
3637 
3638 		rc = __qed_hw_set_soft_resc_size(p_hwfn, p_ptt, res_id,
3639 						 resc_max_val, &mcp_resp);
3640 		if (rc)
3641 			return rc;
3642 
3643 		/* There's no point to continue to the next resource if the
3644 		 * command is not supported by the MFW.
3645 		 * We do continue if the command is supported but the resource
3646 		 * is unknown to the MFW. Such a resource will be later
3647 		 * configured with the default allocation values.
3648 		 */
3649 		if (mcp_resp == FW_MSG_CODE_UNSUPPORTED)
3650 			return -EINVAL;
3651 	}
3652 
3653 	return 0;
3654 }
3655 
3656 static
3657 int qed_hw_get_dflt_resc(struct qed_hwfn *p_hwfn,
3658 			 enum qed_resources res_id,
3659 			 u32 *p_resc_num, u32 *p_resc_start)
3660 {
3661 	u8 num_funcs = p_hwfn->num_funcs_on_engine;
3662 	bool b_ah = QED_IS_AH(p_hwfn->cdev);
3663 
3664 	switch (res_id) {
3665 	case QED_L2_QUEUE:
3666 		*p_resc_num = (b_ah ? MAX_NUM_L2_QUEUES_K2 :
3667 			       MAX_NUM_L2_QUEUES_BB) / num_funcs;
3668 		break;
3669 	case QED_VPORT:
3670 		*p_resc_num = (b_ah ? MAX_NUM_VPORTS_K2 :
3671 			       MAX_NUM_VPORTS_BB) / num_funcs;
3672 		break;
3673 	case QED_RSS_ENG:
3674 		*p_resc_num = (b_ah ? ETH_RSS_ENGINE_NUM_K2 :
3675 			       ETH_RSS_ENGINE_NUM_BB) / num_funcs;
3676 		break;
3677 	case QED_PQ:
3678 		*p_resc_num = (b_ah ? MAX_QM_TX_QUEUES_K2 :
3679 			       MAX_QM_TX_QUEUES_BB) / num_funcs;
3680 		*p_resc_num &= ~0x7;	/* The granularity of the PQs is 8 */
3681 		break;
3682 	case QED_RL:
3683 		*p_resc_num = MAX_QM_GLOBAL_RLS / num_funcs;
3684 		break;
3685 	case QED_MAC:
3686 	case QED_VLAN:
3687 		/* Each VFC resource can accommodate both a MAC and a VLAN */
3688 		*p_resc_num = ETH_NUM_MAC_FILTERS / num_funcs;
3689 		break;
3690 	case QED_ILT:
3691 		*p_resc_num = (b_ah ? PXP_NUM_ILT_RECORDS_K2 :
3692 			       PXP_NUM_ILT_RECORDS_BB) / num_funcs;
3693 		break;
3694 	case QED_LL2_QUEUE:
3695 		*p_resc_num = MAX_NUM_LL2_RX_QUEUES / num_funcs;
3696 		break;
3697 	case QED_RDMA_CNQ_RAM:
3698 	case QED_CMDQS_CQS:
3699 		/* CNQ/CMDQS are the same resource */
3700 		*p_resc_num = NUM_OF_GLOBAL_QUEUES / num_funcs;
3701 		break;
3702 	case QED_RDMA_STATS_QUEUE:
3703 		*p_resc_num = (b_ah ? RDMA_NUM_STATISTIC_COUNTERS_K2 :
3704 			       RDMA_NUM_STATISTIC_COUNTERS_BB) / num_funcs;
3705 		break;
3706 	case QED_BDQ:
3707 		if (p_hwfn->hw_info.personality != QED_PCI_ISCSI &&
3708 		    p_hwfn->hw_info.personality != QED_PCI_FCOE)
3709 			*p_resc_num = 0;
3710 		else
3711 			*p_resc_num = 1;
3712 		break;
3713 	case QED_SB:
3714 		/* Since we want its value to reflect whether MFW supports
3715 		 * the new scheme, have a default of 0.
3716 		 */
3717 		*p_resc_num = 0;
3718 		break;
3719 	default:
3720 		return -EINVAL;
3721 	}
3722 
3723 	switch (res_id) {
3724 	case QED_BDQ:
3725 		if (!*p_resc_num)
3726 			*p_resc_start = 0;
3727 		else if (p_hwfn->cdev->num_ports_in_engine == 4)
3728 			*p_resc_start = p_hwfn->port_id;
3729 		else if (p_hwfn->hw_info.personality == QED_PCI_ISCSI)
3730 			*p_resc_start = p_hwfn->port_id;
3731 		else if (p_hwfn->hw_info.personality == QED_PCI_FCOE)
3732 			*p_resc_start = p_hwfn->port_id + 2;
3733 		break;
3734 	default:
3735 		*p_resc_start = *p_resc_num * p_hwfn->enabled_func_idx;
3736 		break;
3737 	}
3738 
3739 	return 0;
3740 }
3741 
3742 static int __qed_hw_set_resc_info(struct qed_hwfn *p_hwfn,
3743 				  enum qed_resources res_id)
3744 {
3745 	u32 dflt_resc_num = 0, dflt_resc_start = 0;
3746 	u32 mcp_resp, *p_resc_num, *p_resc_start;
3747 	int rc;
3748 
3749 	p_resc_num = &RESC_NUM(p_hwfn, res_id);
3750 	p_resc_start = &RESC_START(p_hwfn, res_id);
3751 
3752 	rc = qed_hw_get_dflt_resc(p_hwfn, res_id, &dflt_resc_num,
3753 				  &dflt_resc_start);
3754 	if (rc) {
3755 		DP_ERR(p_hwfn,
3756 		       "Failed to get default amount for resource %d [%s]\n",
3757 		       res_id, qed_hw_get_resc_name(res_id));
3758 		return rc;
3759 	}
3760 
3761 	rc = qed_mcp_get_resc_info(p_hwfn, p_hwfn->p_main_ptt, res_id,
3762 				   &mcp_resp, p_resc_num, p_resc_start);
3763 	if (rc) {
3764 		DP_NOTICE(p_hwfn,
3765 			  "MFW response failure for an allocation request for resource %d [%s]\n",
3766 			  res_id, qed_hw_get_resc_name(res_id));
3767 		return rc;
3768 	}
3769 
3770 	/* Default driver values are applied in the following cases:
3771 	 * - The resource allocation MB command is not supported by the MFW
3772 	 * - There is an internal error in the MFW while processing the request
3773 	 * - The resource ID is unknown to the MFW
3774 	 */
3775 	if (mcp_resp != FW_MSG_CODE_RESOURCE_ALLOC_OK) {
3776 		DP_INFO(p_hwfn,
3777 			"Failed to receive allocation info for resource %d [%s]. mcp_resp = 0x%x. Applying default values [%d,%d].\n",
3778 			res_id,
3779 			qed_hw_get_resc_name(res_id),
3780 			mcp_resp, dflt_resc_num, dflt_resc_start);
3781 		*p_resc_num = dflt_resc_num;
3782 		*p_resc_start = dflt_resc_start;
3783 		goto out;
3784 	}
3785 
3786 out:
3787 	/* PQs have to divide by 8 [that's the HW granularity].
3788 	 * Reduce number so it would fit.
3789 	 */
3790 	if ((res_id == QED_PQ) && ((*p_resc_num % 8) || (*p_resc_start % 8))) {
3791 		DP_INFO(p_hwfn,
3792 			"PQs need to align by 8; Number %08x --> %08x, Start %08x --> %08x\n",
3793 			*p_resc_num,
3794 			(*p_resc_num) & ~0x7,
3795 			*p_resc_start, (*p_resc_start) & ~0x7);
3796 		*p_resc_num &= ~0x7;
3797 		*p_resc_start &= ~0x7;
3798 	}
3799 
3800 	return 0;
3801 }
3802 
3803 static int qed_hw_set_resc_info(struct qed_hwfn *p_hwfn)
3804 {
3805 	int rc;
3806 	u8 res_id;
3807 
3808 	for (res_id = 0; res_id < QED_MAX_RESC; res_id++) {
3809 		rc = __qed_hw_set_resc_info(p_hwfn, res_id);
3810 		if (rc)
3811 			return rc;
3812 	}
3813 
3814 	return 0;
3815 }
3816 
3817 static int qed_hw_get_ppfid_bitmap(struct qed_hwfn *p_hwfn,
3818 				   struct qed_ptt *p_ptt)
3819 {
3820 	struct qed_dev *cdev = p_hwfn->cdev;
3821 	u8 native_ppfid_idx;
3822 	int rc;
3823 
3824 	/* Calculation of BB/AH is different for native_ppfid_idx */
3825 	if (QED_IS_BB(cdev))
3826 		native_ppfid_idx = p_hwfn->rel_pf_id;
3827 	else
3828 		native_ppfid_idx = p_hwfn->rel_pf_id /
3829 		    cdev->num_ports_in_engine;
3830 
3831 	rc = qed_mcp_get_ppfid_bitmap(p_hwfn, p_ptt);
3832 	if (rc != 0 && rc != -EOPNOTSUPP)
3833 		return rc;
3834 	else if (rc == -EOPNOTSUPP)
3835 		cdev->ppfid_bitmap = 0x1 << native_ppfid_idx;
3836 
3837 	if (!(cdev->ppfid_bitmap & (0x1 << native_ppfid_idx))) {
3838 		DP_INFO(p_hwfn,
3839 			"Fix the PPFID bitmap to include the native PPFID [native_ppfid_idx %hhd, orig_bitmap 0x%hhx]\n",
3840 			native_ppfid_idx, cdev->ppfid_bitmap);
3841 		cdev->ppfid_bitmap = 0x1 << native_ppfid_idx;
3842 	}
3843 
3844 	return 0;
3845 }
3846 
3847 static int qed_hw_get_resc(struct qed_hwfn *p_hwfn, struct qed_ptt *p_ptt)
3848 {
3849 	struct qed_resc_unlock_params resc_unlock_params;
3850 	struct qed_resc_lock_params resc_lock_params;
3851 	bool b_ah = QED_IS_AH(p_hwfn->cdev);
3852 	u8 res_id;
3853 	int rc;
3854 
3855 	/* Setting the max values of the soft resources and the following
3856 	 * resources allocation queries should be atomic. Since several PFs can
3857 	 * run in parallel - a resource lock is needed.
3858 	 * If either the resource lock or resource set value commands are not
3859 	 * supported - skip the the max values setting, release the lock if
3860 	 * needed, and proceed to the queries. Other failures, including a
3861 	 * failure to acquire the lock, will cause this function to fail.
3862 	 */
3863 	qed_mcp_resc_lock_default_init(&resc_lock_params, &resc_unlock_params,
3864 				       QED_RESC_LOCK_RESC_ALLOC, false);
3865 
3866 	rc = qed_mcp_resc_lock(p_hwfn, p_ptt, &resc_lock_params);
3867 	if (rc && rc != -EINVAL) {
3868 		return rc;
3869 	} else if (rc == -EINVAL) {
3870 		DP_INFO(p_hwfn,
3871 			"Skip the max values setting of the soft resources since the resource lock is not supported by the MFW\n");
3872 	} else if (!rc && !resc_lock_params.b_granted) {
3873 		DP_NOTICE(p_hwfn,
3874 			  "Failed to acquire the resource lock for the resource allocation commands\n");
3875 		return -EBUSY;
3876 	} else {
3877 		rc = qed_hw_set_soft_resc_size(p_hwfn, p_ptt);
3878 		if (rc && rc != -EINVAL) {
3879 			DP_NOTICE(p_hwfn,
3880 				  "Failed to set the max values of the soft resources\n");
3881 			goto unlock_and_exit;
3882 		} else if (rc == -EINVAL) {
3883 			DP_INFO(p_hwfn,
3884 				"Skip the max values setting of the soft resources since it is not supported by the MFW\n");
3885 			rc = qed_mcp_resc_unlock(p_hwfn, p_ptt,
3886 						 &resc_unlock_params);
3887 			if (rc)
3888 				DP_INFO(p_hwfn,
3889 					"Failed to release the resource lock for the resource allocation commands\n");
3890 		}
3891 	}
3892 
3893 	rc = qed_hw_set_resc_info(p_hwfn);
3894 	if (rc)
3895 		goto unlock_and_exit;
3896 
3897 	if (resc_lock_params.b_granted && !resc_unlock_params.b_released) {
3898 		rc = qed_mcp_resc_unlock(p_hwfn, p_ptt, &resc_unlock_params);
3899 		if (rc)
3900 			DP_INFO(p_hwfn,
3901 				"Failed to release the resource lock for the resource allocation commands\n");
3902 	}
3903 
3904 	/* PPFID bitmap */
3905 	if (IS_LEAD_HWFN(p_hwfn)) {
3906 		rc = qed_hw_get_ppfid_bitmap(p_hwfn, p_ptt);
3907 		if (rc)
3908 			return rc;
3909 	}
3910 
3911 	/* Sanity for ILT */
3912 	if ((b_ah && (RESC_END(p_hwfn, QED_ILT) > PXP_NUM_ILT_RECORDS_K2)) ||
3913 	    (!b_ah && (RESC_END(p_hwfn, QED_ILT) > PXP_NUM_ILT_RECORDS_BB))) {
3914 		DP_NOTICE(p_hwfn, "Can't assign ILT pages [%08x,...,%08x]\n",
3915 			  RESC_START(p_hwfn, QED_ILT),
3916 			  RESC_END(p_hwfn, QED_ILT) - 1);
3917 		return -EINVAL;
3918 	}
3919 
3920 	/* This will also learn the number of SBs from MFW */
3921 	if (qed_int_igu_reset_cam(p_hwfn, p_ptt))
3922 		return -EINVAL;
3923 
3924 	qed_hw_set_feat(p_hwfn);
3925 
3926 	for (res_id = 0; res_id < QED_MAX_RESC; res_id++)
3927 		DP_VERBOSE(p_hwfn, NETIF_MSG_PROBE, "%s = %d start = %d\n",
3928 			   qed_hw_get_resc_name(res_id),
3929 			   RESC_NUM(p_hwfn, res_id),
3930 			   RESC_START(p_hwfn, res_id));
3931 
3932 	return 0;
3933 
3934 unlock_and_exit:
3935 	if (resc_lock_params.b_granted && !resc_unlock_params.b_released)
3936 		qed_mcp_resc_unlock(p_hwfn, p_ptt, &resc_unlock_params);
3937 	return rc;
3938 }
3939 
3940 static int qed_hw_get_nvm_info(struct qed_hwfn *p_hwfn, struct qed_ptt *p_ptt)
3941 {
3942 	u32 port_cfg_addr, link_temp, nvm_cfg_addr, device_capabilities;
3943 	u32 nvm_cfg1_offset, mf_mode, addr, generic_cont0, core_cfg;
3944 	struct qed_mcp_link_capabilities *p_caps;
3945 	struct qed_mcp_link_params *link;
3946 
3947 	/* Read global nvm_cfg address */
3948 	nvm_cfg_addr = qed_rd(p_hwfn, p_ptt, MISC_REG_GEN_PURP_CR0);
3949 
3950 	/* Verify MCP has initialized it */
3951 	if (!nvm_cfg_addr) {
3952 		DP_NOTICE(p_hwfn, "Shared memory not initialized\n");
3953 		return -EINVAL;
3954 	}
3955 
3956 	/* Read nvm_cfg1  (Notice this is just offset, and not offsize (TBD) */
3957 	nvm_cfg1_offset = qed_rd(p_hwfn, p_ptt, nvm_cfg_addr + 4);
3958 
3959 	addr = MCP_REG_SCRATCH + nvm_cfg1_offset +
3960 	       offsetof(struct nvm_cfg1, glob) +
3961 	       offsetof(struct nvm_cfg1_glob, core_cfg);
3962 
3963 	core_cfg = qed_rd(p_hwfn, p_ptt, addr);
3964 
3965 	switch ((core_cfg & NVM_CFG1_GLOB_NETWORK_PORT_MODE_MASK) >>
3966 		NVM_CFG1_GLOB_NETWORK_PORT_MODE_OFFSET) {
3967 	case NVM_CFG1_GLOB_NETWORK_PORT_MODE_BB_2X40G:
3968 		p_hwfn->hw_info.port_mode = QED_PORT_MODE_DE_2X40G;
3969 		break;
3970 	case NVM_CFG1_GLOB_NETWORK_PORT_MODE_2X50G:
3971 		p_hwfn->hw_info.port_mode = QED_PORT_MODE_DE_2X50G;
3972 		break;
3973 	case NVM_CFG1_GLOB_NETWORK_PORT_MODE_BB_1X100G:
3974 		p_hwfn->hw_info.port_mode = QED_PORT_MODE_DE_1X100G;
3975 		break;
3976 	case NVM_CFG1_GLOB_NETWORK_PORT_MODE_4X10G_F:
3977 		p_hwfn->hw_info.port_mode = QED_PORT_MODE_DE_4X10G_F;
3978 		break;
3979 	case NVM_CFG1_GLOB_NETWORK_PORT_MODE_BB_4X10G_E:
3980 		p_hwfn->hw_info.port_mode = QED_PORT_MODE_DE_4X10G_E;
3981 		break;
3982 	case NVM_CFG1_GLOB_NETWORK_PORT_MODE_BB_4X20G:
3983 		p_hwfn->hw_info.port_mode = QED_PORT_MODE_DE_4X20G;
3984 		break;
3985 	case NVM_CFG1_GLOB_NETWORK_PORT_MODE_1X40G:
3986 		p_hwfn->hw_info.port_mode = QED_PORT_MODE_DE_1X40G;
3987 		break;
3988 	case NVM_CFG1_GLOB_NETWORK_PORT_MODE_2X25G:
3989 		p_hwfn->hw_info.port_mode = QED_PORT_MODE_DE_2X25G;
3990 		break;
3991 	case NVM_CFG1_GLOB_NETWORK_PORT_MODE_2X10G:
3992 		p_hwfn->hw_info.port_mode = QED_PORT_MODE_DE_2X10G;
3993 		break;
3994 	case NVM_CFG1_GLOB_NETWORK_PORT_MODE_1X25G:
3995 		p_hwfn->hw_info.port_mode = QED_PORT_MODE_DE_1X25G;
3996 		break;
3997 	case NVM_CFG1_GLOB_NETWORK_PORT_MODE_4X25G:
3998 		p_hwfn->hw_info.port_mode = QED_PORT_MODE_DE_4X25G;
3999 		break;
4000 	default:
4001 		DP_NOTICE(p_hwfn, "Unknown port mode in 0x%08x\n", core_cfg);
4002 		break;
4003 	}
4004 
4005 	/* Read default link configuration */
4006 	link = &p_hwfn->mcp_info->link_input;
4007 	p_caps = &p_hwfn->mcp_info->link_capabilities;
4008 	port_cfg_addr = MCP_REG_SCRATCH + nvm_cfg1_offset +
4009 			offsetof(struct nvm_cfg1, port[MFW_PORT(p_hwfn)]);
4010 	link_temp = qed_rd(p_hwfn, p_ptt,
4011 			   port_cfg_addr +
4012 			   offsetof(struct nvm_cfg1_port, speed_cap_mask));
4013 	link_temp &= NVM_CFG1_PORT_DRV_SPEED_CAPABILITY_MASK_MASK;
4014 	link->speed.advertised_speeds = link_temp;
4015 
4016 	link_temp = link->speed.advertised_speeds;
4017 	p_hwfn->mcp_info->link_capabilities.speed_capabilities = link_temp;
4018 
4019 	link_temp = qed_rd(p_hwfn, p_ptt,
4020 			   port_cfg_addr +
4021 			   offsetof(struct nvm_cfg1_port, link_settings));
4022 	switch ((link_temp & NVM_CFG1_PORT_DRV_LINK_SPEED_MASK) >>
4023 		NVM_CFG1_PORT_DRV_LINK_SPEED_OFFSET) {
4024 	case NVM_CFG1_PORT_DRV_LINK_SPEED_AUTONEG:
4025 		link->speed.autoneg = true;
4026 		break;
4027 	case NVM_CFG1_PORT_DRV_LINK_SPEED_1G:
4028 		link->speed.forced_speed = 1000;
4029 		break;
4030 	case NVM_CFG1_PORT_DRV_LINK_SPEED_10G:
4031 		link->speed.forced_speed = 10000;
4032 		break;
4033 	case NVM_CFG1_PORT_DRV_LINK_SPEED_20G:
4034 		link->speed.forced_speed = 20000;
4035 		break;
4036 	case NVM_CFG1_PORT_DRV_LINK_SPEED_25G:
4037 		link->speed.forced_speed = 25000;
4038 		break;
4039 	case NVM_CFG1_PORT_DRV_LINK_SPEED_40G:
4040 		link->speed.forced_speed = 40000;
4041 		break;
4042 	case NVM_CFG1_PORT_DRV_LINK_SPEED_50G:
4043 		link->speed.forced_speed = 50000;
4044 		break;
4045 	case NVM_CFG1_PORT_DRV_LINK_SPEED_BB_100G:
4046 		link->speed.forced_speed = 100000;
4047 		break;
4048 	default:
4049 		DP_NOTICE(p_hwfn, "Unknown Speed in 0x%08x\n", link_temp);
4050 	}
4051 
4052 	p_hwfn->mcp_info->link_capabilities.default_speed_autoneg =
4053 		link->speed.autoneg;
4054 
4055 	link_temp &= NVM_CFG1_PORT_DRV_FLOW_CONTROL_MASK;
4056 	link_temp >>= NVM_CFG1_PORT_DRV_FLOW_CONTROL_OFFSET;
4057 	link->pause.autoneg = !!(link_temp &
4058 				 NVM_CFG1_PORT_DRV_FLOW_CONTROL_AUTONEG);
4059 	link->pause.forced_rx = !!(link_temp &
4060 				   NVM_CFG1_PORT_DRV_FLOW_CONTROL_RX);
4061 	link->pause.forced_tx = !!(link_temp &
4062 				   NVM_CFG1_PORT_DRV_FLOW_CONTROL_TX);
4063 	link->loopback_mode = 0;
4064 
4065 	if (p_hwfn->mcp_info->capabilities & FW_MB_PARAM_FEATURE_SUPPORT_EEE) {
4066 		link_temp = qed_rd(p_hwfn, p_ptt, port_cfg_addr +
4067 				   offsetof(struct nvm_cfg1_port, ext_phy));
4068 		link_temp &= NVM_CFG1_PORT_EEE_POWER_SAVING_MODE_MASK;
4069 		link_temp >>= NVM_CFG1_PORT_EEE_POWER_SAVING_MODE_OFFSET;
4070 		p_caps->default_eee = QED_MCP_EEE_ENABLED;
4071 		link->eee.enable = true;
4072 		switch (link_temp) {
4073 		case NVM_CFG1_PORT_EEE_POWER_SAVING_MODE_DISABLED:
4074 			p_caps->default_eee = QED_MCP_EEE_DISABLED;
4075 			link->eee.enable = false;
4076 			break;
4077 		case NVM_CFG1_PORT_EEE_POWER_SAVING_MODE_BALANCED:
4078 			p_caps->eee_lpi_timer = EEE_TX_TIMER_USEC_BALANCED_TIME;
4079 			break;
4080 		case NVM_CFG1_PORT_EEE_POWER_SAVING_MODE_AGGRESSIVE:
4081 			p_caps->eee_lpi_timer =
4082 			    EEE_TX_TIMER_USEC_AGGRESSIVE_TIME;
4083 			break;
4084 		case NVM_CFG1_PORT_EEE_POWER_SAVING_MODE_LOW_LATENCY:
4085 			p_caps->eee_lpi_timer = EEE_TX_TIMER_USEC_LATENCY_TIME;
4086 			break;
4087 		}
4088 
4089 		link->eee.tx_lpi_timer = p_caps->eee_lpi_timer;
4090 		link->eee.tx_lpi_enable = link->eee.enable;
4091 		link->eee.adv_caps = QED_EEE_1G_ADV | QED_EEE_10G_ADV;
4092 	} else {
4093 		p_caps->default_eee = QED_MCP_EEE_UNSUPPORTED;
4094 	}
4095 
4096 	DP_VERBOSE(p_hwfn,
4097 		   NETIF_MSG_LINK,
4098 		   "Read default link: Speed 0x%08x, Adv. Speed 0x%08x, AN: 0x%02x, PAUSE AN: 0x%02x EEE: %02x [%08x usec]\n",
4099 		   link->speed.forced_speed,
4100 		   link->speed.advertised_speeds,
4101 		   link->speed.autoneg,
4102 		   link->pause.autoneg,
4103 		   p_caps->default_eee, p_caps->eee_lpi_timer);
4104 
4105 	if (IS_LEAD_HWFN(p_hwfn)) {
4106 		struct qed_dev *cdev = p_hwfn->cdev;
4107 
4108 		/* Read Multi-function information from shmem */
4109 		addr = MCP_REG_SCRATCH + nvm_cfg1_offset +
4110 		       offsetof(struct nvm_cfg1, glob) +
4111 		       offsetof(struct nvm_cfg1_glob, generic_cont0);
4112 
4113 		generic_cont0 = qed_rd(p_hwfn, p_ptt, addr);
4114 
4115 		mf_mode = (generic_cont0 & NVM_CFG1_GLOB_MF_MODE_MASK) >>
4116 			  NVM_CFG1_GLOB_MF_MODE_OFFSET;
4117 
4118 		switch (mf_mode) {
4119 		case NVM_CFG1_GLOB_MF_MODE_MF_ALLOWED:
4120 			cdev->mf_bits = BIT(QED_MF_OVLAN_CLSS);
4121 			break;
4122 		case NVM_CFG1_GLOB_MF_MODE_UFP:
4123 			cdev->mf_bits = BIT(QED_MF_OVLAN_CLSS) |
4124 					BIT(QED_MF_LLH_PROTO_CLSS) |
4125 					BIT(QED_MF_UFP_SPECIFIC) |
4126 					BIT(QED_MF_8021Q_TAGGING) |
4127 					BIT(QED_MF_DONT_ADD_VLAN0_TAG);
4128 			break;
4129 		case NVM_CFG1_GLOB_MF_MODE_BD:
4130 			cdev->mf_bits = BIT(QED_MF_OVLAN_CLSS) |
4131 					BIT(QED_MF_LLH_PROTO_CLSS) |
4132 					BIT(QED_MF_8021AD_TAGGING) |
4133 					BIT(QED_MF_DONT_ADD_VLAN0_TAG);
4134 			break;
4135 		case NVM_CFG1_GLOB_MF_MODE_NPAR1_0:
4136 			cdev->mf_bits = BIT(QED_MF_LLH_MAC_CLSS) |
4137 					BIT(QED_MF_LLH_PROTO_CLSS) |
4138 					BIT(QED_MF_LL2_NON_UNICAST) |
4139 					BIT(QED_MF_INTER_PF_SWITCH);
4140 			break;
4141 		case NVM_CFG1_GLOB_MF_MODE_DEFAULT:
4142 			cdev->mf_bits = BIT(QED_MF_LLH_MAC_CLSS) |
4143 					BIT(QED_MF_LLH_PROTO_CLSS) |
4144 					BIT(QED_MF_LL2_NON_UNICAST);
4145 			if (QED_IS_BB(p_hwfn->cdev))
4146 				cdev->mf_bits |= BIT(QED_MF_NEED_DEF_PF);
4147 			break;
4148 		}
4149 
4150 		DP_INFO(p_hwfn, "Multi function mode is 0x%lx\n",
4151 			cdev->mf_bits);
4152 	}
4153 
4154 	DP_INFO(p_hwfn, "Multi function mode is 0x%lx\n",
4155 		p_hwfn->cdev->mf_bits);
4156 
4157 	/* Read device capabilities information from shmem */
4158 	addr = MCP_REG_SCRATCH + nvm_cfg1_offset +
4159 		offsetof(struct nvm_cfg1, glob) +
4160 		offsetof(struct nvm_cfg1_glob, device_capabilities);
4161 
4162 	device_capabilities = qed_rd(p_hwfn, p_ptt, addr);
4163 	if (device_capabilities & NVM_CFG1_GLOB_DEVICE_CAPABILITIES_ETHERNET)
4164 		__set_bit(QED_DEV_CAP_ETH,
4165 			  &p_hwfn->hw_info.device_capabilities);
4166 	if (device_capabilities & NVM_CFG1_GLOB_DEVICE_CAPABILITIES_FCOE)
4167 		__set_bit(QED_DEV_CAP_FCOE,
4168 			  &p_hwfn->hw_info.device_capabilities);
4169 	if (device_capabilities & NVM_CFG1_GLOB_DEVICE_CAPABILITIES_ISCSI)
4170 		__set_bit(QED_DEV_CAP_ISCSI,
4171 			  &p_hwfn->hw_info.device_capabilities);
4172 	if (device_capabilities & NVM_CFG1_GLOB_DEVICE_CAPABILITIES_ROCE)
4173 		__set_bit(QED_DEV_CAP_ROCE,
4174 			  &p_hwfn->hw_info.device_capabilities);
4175 
4176 	return qed_mcp_fill_shmem_func_info(p_hwfn, p_ptt);
4177 }
4178 
4179 static void qed_get_num_funcs(struct qed_hwfn *p_hwfn, struct qed_ptt *p_ptt)
4180 {
4181 	u8 num_funcs, enabled_func_idx = p_hwfn->rel_pf_id;
4182 	u32 reg_function_hide, tmp, eng_mask, low_pfs_mask;
4183 	struct qed_dev *cdev = p_hwfn->cdev;
4184 
4185 	num_funcs = QED_IS_AH(cdev) ? MAX_NUM_PFS_K2 : MAX_NUM_PFS_BB;
4186 
4187 	/* Bit 0 of MISCS_REG_FUNCTION_HIDE indicates whether the bypass values
4188 	 * in the other bits are selected.
4189 	 * Bits 1-15 are for functions 1-15, respectively, and their value is
4190 	 * '0' only for enabled functions (function 0 always exists and
4191 	 * enabled).
4192 	 * In case of CMT, only the "even" functions are enabled, and thus the
4193 	 * number of functions for both hwfns is learnt from the same bits.
4194 	 */
4195 	reg_function_hide = qed_rd(p_hwfn, p_ptt, MISCS_REG_FUNCTION_HIDE);
4196 
4197 	if (reg_function_hide & 0x1) {
4198 		if (QED_IS_BB(cdev)) {
4199 			if (QED_PATH_ID(p_hwfn) && cdev->num_hwfns == 1) {
4200 				num_funcs = 0;
4201 				eng_mask = 0xaaaa;
4202 			} else {
4203 				num_funcs = 1;
4204 				eng_mask = 0x5554;
4205 			}
4206 		} else {
4207 			num_funcs = 1;
4208 			eng_mask = 0xfffe;
4209 		}
4210 
4211 		/* Get the number of the enabled functions on the engine */
4212 		tmp = (reg_function_hide ^ 0xffffffff) & eng_mask;
4213 		while (tmp) {
4214 			if (tmp & 0x1)
4215 				num_funcs++;
4216 			tmp >>= 0x1;
4217 		}
4218 
4219 		/* Get the PF index within the enabled functions */
4220 		low_pfs_mask = (0x1 << p_hwfn->abs_pf_id) - 1;
4221 		tmp = reg_function_hide & eng_mask & low_pfs_mask;
4222 		while (tmp) {
4223 			if (tmp & 0x1)
4224 				enabled_func_idx--;
4225 			tmp >>= 0x1;
4226 		}
4227 	}
4228 
4229 	p_hwfn->num_funcs_on_engine = num_funcs;
4230 	p_hwfn->enabled_func_idx = enabled_func_idx;
4231 
4232 	DP_VERBOSE(p_hwfn,
4233 		   NETIF_MSG_PROBE,
4234 		   "PF [rel_id %d, abs_id %d] occupies index %d within the %d enabled functions on the engine\n",
4235 		   p_hwfn->rel_pf_id,
4236 		   p_hwfn->abs_pf_id,
4237 		   p_hwfn->enabled_func_idx, p_hwfn->num_funcs_on_engine);
4238 }
4239 
4240 static void qed_hw_info_port_num(struct qed_hwfn *p_hwfn, struct qed_ptt *p_ptt)
4241 {
4242 	u32 addr, global_offsize, global_addr, port_mode;
4243 	struct qed_dev *cdev = p_hwfn->cdev;
4244 
4245 	/* In CMT there is always only one port */
4246 	if (cdev->num_hwfns > 1) {
4247 		cdev->num_ports_in_engine = 1;
4248 		cdev->num_ports = 1;
4249 		return;
4250 	}
4251 
4252 	/* Determine the number of ports per engine */
4253 	port_mode = qed_rd(p_hwfn, p_ptt, MISC_REG_PORT_MODE);
4254 	switch (port_mode) {
4255 	case 0x0:
4256 		cdev->num_ports_in_engine = 1;
4257 		break;
4258 	case 0x1:
4259 		cdev->num_ports_in_engine = 2;
4260 		break;
4261 	case 0x2:
4262 		cdev->num_ports_in_engine = 4;
4263 		break;
4264 	default:
4265 		DP_NOTICE(p_hwfn, "Unknown port mode 0x%08x\n", port_mode);
4266 		cdev->num_ports_in_engine = 1;	/* Default to something */
4267 		break;
4268 	}
4269 
4270 	/* Get the total number of ports of the device */
4271 	addr = SECTION_OFFSIZE_ADDR(p_hwfn->mcp_info->public_base,
4272 				    PUBLIC_GLOBAL);
4273 	global_offsize = qed_rd(p_hwfn, p_ptt, addr);
4274 	global_addr = SECTION_ADDR(global_offsize, 0);
4275 	addr = global_addr + offsetof(struct public_global, max_ports);
4276 	cdev->num_ports = (u8)qed_rd(p_hwfn, p_ptt, addr);
4277 }
4278 
4279 static void qed_get_eee_caps(struct qed_hwfn *p_hwfn, struct qed_ptt *p_ptt)
4280 {
4281 	struct qed_mcp_link_capabilities *p_caps;
4282 	u32 eee_status;
4283 
4284 	p_caps = &p_hwfn->mcp_info->link_capabilities;
4285 	if (p_caps->default_eee == QED_MCP_EEE_UNSUPPORTED)
4286 		return;
4287 
4288 	p_caps->eee_speed_caps = 0;
4289 	eee_status = qed_rd(p_hwfn, p_ptt, p_hwfn->mcp_info->port_addr +
4290 			    offsetof(struct public_port, eee_status));
4291 	eee_status = (eee_status & EEE_SUPPORTED_SPEED_MASK) >>
4292 			EEE_SUPPORTED_SPEED_OFFSET;
4293 
4294 	if (eee_status & EEE_1G_SUPPORTED)
4295 		p_caps->eee_speed_caps |= QED_EEE_1G_ADV;
4296 	if (eee_status & EEE_10G_ADV)
4297 		p_caps->eee_speed_caps |= QED_EEE_10G_ADV;
4298 }
4299 
4300 static int
4301 qed_get_hw_info(struct qed_hwfn *p_hwfn,
4302 		struct qed_ptt *p_ptt,
4303 		enum qed_pci_personality personality)
4304 {
4305 	int rc;
4306 
4307 	/* Since all information is common, only first hwfns should do this */
4308 	if (IS_LEAD_HWFN(p_hwfn)) {
4309 		rc = qed_iov_hw_info(p_hwfn);
4310 		if (rc)
4311 			return rc;
4312 	}
4313 
4314 	if (IS_LEAD_HWFN(p_hwfn))
4315 		qed_hw_info_port_num(p_hwfn, p_ptt);
4316 
4317 	qed_mcp_get_capabilities(p_hwfn, p_ptt);
4318 
4319 	qed_hw_get_nvm_info(p_hwfn, p_ptt);
4320 
4321 	rc = qed_int_igu_read_cam(p_hwfn, p_ptt);
4322 	if (rc)
4323 		return rc;
4324 
4325 	if (qed_mcp_is_init(p_hwfn))
4326 		ether_addr_copy(p_hwfn->hw_info.hw_mac_addr,
4327 				p_hwfn->mcp_info->func_info.mac);
4328 	else
4329 		eth_random_addr(p_hwfn->hw_info.hw_mac_addr);
4330 
4331 	if (qed_mcp_is_init(p_hwfn)) {
4332 		if (p_hwfn->mcp_info->func_info.ovlan != QED_MCP_VLAN_UNSET)
4333 			p_hwfn->hw_info.ovlan =
4334 				p_hwfn->mcp_info->func_info.ovlan;
4335 
4336 		qed_mcp_cmd_port_init(p_hwfn, p_ptt);
4337 
4338 		qed_get_eee_caps(p_hwfn, p_ptt);
4339 
4340 		qed_mcp_read_ufp_config(p_hwfn, p_ptt);
4341 	}
4342 
4343 	if (qed_mcp_is_init(p_hwfn)) {
4344 		enum qed_pci_personality protocol;
4345 
4346 		protocol = p_hwfn->mcp_info->func_info.protocol;
4347 		p_hwfn->hw_info.personality = protocol;
4348 	}
4349 
4350 	if (QED_IS_ROCE_PERSONALITY(p_hwfn))
4351 		p_hwfn->hw_info.multi_tc_roce_en = 1;
4352 
4353 	p_hwfn->hw_info.num_hw_tc = NUM_PHYS_TCS_4PORT_K2;
4354 	p_hwfn->hw_info.num_active_tc = 1;
4355 
4356 	qed_get_num_funcs(p_hwfn, p_ptt);
4357 
4358 	if (qed_mcp_is_init(p_hwfn))
4359 		p_hwfn->hw_info.mtu = p_hwfn->mcp_info->func_info.mtu;
4360 
4361 	return qed_hw_get_resc(p_hwfn, p_ptt);
4362 }
4363 
4364 static int qed_get_dev_info(struct qed_hwfn *p_hwfn, struct qed_ptt *p_ptt)
4365 {
4366 	struct qed_dev *cdev = p_hwfn->cdev;
4367 	u16 device_id_mask;
4368 	u32 tmp;
4369 
4370 	/* Read Vendor Id / Device Id */
4371 	pci_read_config_word(cdev->pdev, PCI_VENDOR_ID, &cdev->vendor_id);
4372 	pci_read_config_word(cdev->pdev, PCI_DEVICE_ID, &cdev->device_id);
4373 
4374 	/* Determine type */
4375 	device_id_mask = cdev->device_id & QED_DEV_ID_MASK;
4376 	switch (device_id_mask) {
4377 	case QED_DEV_ID_MASK_BB:
4378 		cdev->type = QED_DEV_TYPE_BB;
4379 		break;
4380 	case QED_DEV_ID_MASK_AH:
4381 		cdev->type = QED_DEV_TYPE_AH;
4382 		break;
4383 	default:
4384 		DP_NOTICE(p_hwfn, "Unknown device id 0x%x\n", cdev->device_id);
4385 		return -EBUSY;
4386 	}
4387 
4388 	cdev->chip_num = (u16)qed_rd(p_hwfn, p_ptt, MISCS_REG_CHIP_NUM);
4389 	cdev->chip_rev = (u16)qed_rd(p_hwfn, p_ptt, MISCS_REG_CHIP_REV);
4390 
4391 	MASK_FIELD(CHIP_REV, cdev->chip_rev);
4392 
4393 	/* Learn number of HW-functions */
4394 	tmp = qed_rd(p_hwfn, p_ptt, MISCS_REG_CMT_ENABLED_FOR_PAIR);
4395 
4396 	if (tmp & (1 << p_hwfn->rel_pf_id)) {
4397 		DP_NOTICE(cdev->hwfns, "device in CMT mode\n");
4398 		cdev->num_hwfns = 2;
4399 	} else {
4400 		cdev->num_hwfns = 1;
4401 	}
4402 
4403 	cdev->chip_bond_id = qed_rd(p_hwfn, p_ptt,
4404 				    MISCS_REG_CHIP_TEST_REG) >> 4;
4405 	MASK_FIELD(CHIP_BOND_ID, cdev->chip_bond_id);
4406 	cdev->chip_metal = (u16)qed_rd(p_hwfn, p_ptt, MISCS_REG_CHIP_METAL);
4407 	MASK_FIELD(CHIP_METAL, cdev->chip_metal);
4408 
4409 	DP_INFO(cdev->hwfns,
4410 		"Chip details - %s %c%d, Num: %04x Rev: %04x Bond id: %04x Metal: %04x\n",
4411 		QED_IS_BB(cdev) ? "BB" : "AH",
4412 		'A' + cdev->chip_rev,
4413 		(int)cdev->chip_metal,
4414 		cdev->chip_num, cdev->chip_rev,
4415 		cdev->chip_bond_id, cdev->chip_metal);
4416 
4417 	return 0;
4418 }
4419 
4420 static void qed_nvm_info_free(struct qed_hwfn *p_hwfn)
4421 {
4422 	kfree(p_hwfn->nvm_info.image_att);
4423 	p_hwfn->nvm_info.image_att = NULL;
4424 }
4425 
4426 static int qed_hw_prepare_single(struct qed_hwfn *p_hwfn,
4427 				 void __iomem *p_regview,
4428 				 void __iomem *p_doorbells,
4429 				 u64 db_phys_addr,
4430 				 enum qed_pci_personality personality)
4431 {
4432 	struct qed_dev *cdev = p_hwfn->cdev;
4433 	int rc = 0;
4434 
4435 	/* Split PCI bars evenly between hwfns */
4436 	p_hwfn->regview = p_regview;
4437 	p_hwfn->doorbells = p_doorbells;
4438 	p_hwfn->db_phys_addr = db_phys_addr;
4439 
4440 	if (IS_VF(p_hwfn->cdev))
4441 		return qed_vf_hw_prepare(p_hwfn);
4442 
4443 	/* Validate that chip access is feasible */
4444 	if (REG_RD(p_hwfn, PXP_PF_ME_OPAQUE_ADDR) == 0xffffffff) {
4445 		DP_ERR(p_hwfn,
4446 		       "Reading the ME register returns all Fs; Preventing further chip access\n");
4447 		return -EINVAL;
4448 	}
4449 
4450 	get_function_id(p_hwfn);
4451 
4452 	/* Allocate PTT pool */
4453 	rc = qed_ptt_pool_alloc(p_hwfn);
4454 	if (rc)
4455 		goto err0;
4456 
4457 	/* Allocate the main PTT */
4458 	p_hwfn->p_main_ptt = qed_get_reserved_ptt(p_hwfn, RESERVED_PTT_MAIN);
4459 
4460 	/* First hwfn learns basic information, e.g., number of hwfns */
4461 	if (!p_hwfn->my_id) {
4462 		rc = qed_get_dev_info(p_hwfn, p_hwfn->p_main_ptt);
4463 		if (rc)
4464 			goto err1;
4465 	}
4466 
4467 	qed_hw_hwfn_prepare(p_hwfn);
4468 
4469 	/* Initialize MCP structure */
4470 	rc = qed_mcp_cmd_init(p_hwfn, p_hwfn->p_main_ptt);
4471 	if (rc) {
4472 		DP_NOTICE(p_hwfn, "Failed initializing mcp command\n");
4473 		goto err1;
4474 	}
4475 
4476 	/* Read the device configuration information from the HW and SHMEM */
4477 	rc = qed_get_hw_info(p_hwfn, p_hwfn->p_main_ptt, personality);
4478 	if (rc) {
4479 		DP_NOTICE(p_hwfn, "Failed to get HW information\n");
4480 		goto err2;
4481 	}
4482 
4483 	/* Sending a mailbox to the MFW should be done after qed_get_hw_info()
4484 	 * is called as it sets the ports number in an engine.
4485 	 */
4486 	if (IS_LEAD_HWFN(p_hwfn) && !cdev->recov_in_prog) {
4487 		rc = qed_mcp_initiate_pf_flr(p_hwfn, p_hwfn->p_main_ptt);
4488 		if (rc)
4489 			DP_NOTICE(p_hwfn, "Failed to initiate PF FLR\n");
4490 	}
4491 
4492 	/* NVRAM info initialization and population */
4493 	if (IS_LEAD_HWFN(p_hwfn)) {
4494 		rc = qed_mcp_nvm_info_populate(p_hwfn);
4495 		if (rc) {
4496 			DP_NOTICE(p_hwfn,
4497 				  "Failed to populate nvm info shadow\n");
4498 			goto err2;
4499 		}
4500 	}
4501 
4502 	/* Allocate the init RT array and initialize the init-ops engine */
4503 	rc = qed_init_alloc(p_hwfn);
4504 	if (rc)
4505 		goto err3;
4506 
4507 	return rc;
4508 err3:
4509 	if (IS_LEAD_HWFN(p_hwfn))
4510 		qed_nvm_info_free(p_hwfn);
4511 err2:
4512 	if (IS_LEAD_HWFN(p_hwfn))
4513 		qed_iov_free_hw_info(p_hwfn->cdev);
4514 	qed_mcp_free(p_hwfn);
4515 err1:
4516 	qed_hw_hwfn_free(p_hwfn);
4517 err0:
4518 	return rc;
4519 }
4520 
4521 int qed_hw_prepare(struct qed_dev *cdev,
4522 		   int personality)
4523 {
4524 	struct qed_hwfn *p_hwfn = QED_LEADING_HWFN(cdev);
4525 	int rc;
4526 
4527 	/* Store the precompiled init data ptrs */
4528 	if (IS_PF(cdev))
4529 		qed_init_iro_array(cdev);
4530 
4531 	/* Initialize the first hwfn - will learn number of hwfns */
4532 	rc = qed_hw_prepare_single(p_hwfn,
4533 				   cdev->regview,
4534 				   cdev->doorbells,
4535 				   cdev->db_phys_addr,
4536 				   personality);
4537 	if (rc)
4538 		return rc;
4539 
4540 	personality = p_hwfn->hw_info.personality;
4541 
4542 	/* Initialize the rest of the hwfns */
4543 	if (cdev->num_hwfns > 1) {
4544 		void __iomem *p_regview, *p_doorbell;
4545 		u64 db_phys_addr;
4546 		u32 offset;
4547 
4548 		/* adjust bar offset for second engine */
4549 		offset = qed_hw_bar_size(p_hwfn, p_hwfn->p_main_ptt,
4550 					 BAR_ID_0) / 2;
4551 		p_regview = cdev->regview + offset;
4552 
4553 		offset = qed_hw_bar_size(p_hwfn, p_hwfn->p_main_ptt,
4554 					 BAR_ID_1) / 2;
4555 
4556 		p_doorbell = cdev->doorbells + offset;
4557 
4558 		db_phys_addr = cdev->db_phys_addr + offset;
4559 
4560 		/* prepare second hw function */
4561 		rc = qed_hw_prepare_single(&cdev->hwfns[1], p_regview,
4562 					   p_doorbell, db_phys_addr,
4563 					   personality);
4564 
4565 		/* in case of error, need to free the previously
4566 		 * initiliazed hwfn 0.
4567 		 */
4568 		if (rc) {
4569 			if (IS_PF(cdev)) {
4570 				qed_init_free(p_hwfn);
4571 				qed_nvm_info_free(p_hwfn);
4572 				qed_mcp_free(p_hwfn);
4573 				qed_hw_hwfn_free(p_hwfn);
4574 			}
4575 		}
4576 	}
4577 
4578 	return rc;
4579 }
4580 
4581 void qed_hw_remove(struct qed_dev *cdev)
4582 {
4583 	struct qed_hwfn *p_hwfn = QED_LEADING_HWFN(cdev);
4584 	int i;
4585 
4586 	if (IS_PF(cdev))
4587 		qed_mcp_ov_update_driver_state(p_hwfn, p_hwfn->p_main_ptt,
4588 					       QED_OV_DRIVER_STATE_NOT_LOADED);
4589 
4590 	for_each_hwfn(cdev, i) {
4591 		struct qed_hwfn *p_hwfn = &cdev->hwfns[i];
4592 
4593 		if (IS_VF(cdev)) {
4594 			qed_vf_pf_release(p_hwfn);
4595 			continue;
4596 		}
4597 
4598 		qed_init_free(p_hwfn);
4599 		qed_hw_hwfn_free(p_hwfn);
4600 		qed_mcp_free(p_hwfn);
4601 	}
4602 
4603 	qed_iov_free_hw_info(cdev);
4604 
4605 	qed_nvm_info_free(p_hwfn);
4606 }
4607 
4608 static void qed_chain_free_next_ptr(struct qed_dev *cdev,
4609 				    struct qed_chain *p_chain)
4610 {
4611 	void *p_virt = p_chain->p_virt_addr, *p_virt_next = NULL;
4612 	dma_addr_t p_phys = p_chain->p_phys_addr, p_phys_next = 0;
4613 	struct qed_chain_next *p_next;
4614 	u32 size, i;
4615 
4616 	if (!p_virt)
4617 		return;
4618 
4619 	size = p_chain->elem_size * p_chain->usable_per_page;
4620 
4621 	for (i = 0; i < p_chain->page_cnt; i++) {
4622 		if (!p_virt)
4623 			break;
4624 
4625 		p_next = (struct qed_chain_next *)((u8 *)p_virt + size);
4626 		p_virt_next = p_next->next_virt;
4627 		p_phys_next = HILO_DMA_REGPAIR(p_next->next_phys);
4628 
4629 		dma_free_coherent(&cdev->pdev->dev,
4630 				  QED_CHAIN_PAGE_SIZE, p_virt, p_phys);
4631 
4632 		p_virt = p_virt_next;
4633 		p_phys = p_phys_next;
4634 	}
4635 }
4636 
4637 static void qed_chain_free_single(struct qed_dev *cdev,
4638 				  struct qed_chain *p_chain)
4639 {
4640 	if (!p_chain->p_virt_addr)
4641 		return;
4642 
4643 	dma_free_coherent(&cdev->pdev->dev,
4644 			  QED_CHAIN_PAGE_SIZE,
4645 			  p_chain->p_virt_addr, p_chain->p_phys_addr);
4646 }
4647 
4648 static void qed_chain_free_pbl(struct qed_dev *cdev, struct qed_chain *p_chain)
4649 {
4650 	void **pp_virt_addr_tbl = p_chain->pbl.pp_virt_addr_tbl;
4651 	u32 page_cnt = p_chain->page_cnt, i, pbl_size;
4652 	u8 *p_pbl_virt = p_chain->pbl_sp.p_virt_table;
4653 
4654 	if (!pp_virt_addr_tbl)
4655 		return;
4656 
4657 	if (!p_pbl_virt)
4658 		goto out;
4659 
4660 	for (i = 0; i < page_cnt; i++) {
4661 		if (!pp_virt_addr_tbl[i])
4662 			break;
4663 
4664 		dma_free_coherent(&cdev->pdev->dev,
4665 				  QED_CHAIN_PAGE_SIZE,
4666 				  pp_virt_addr_tbl[i],
4667 				  *(dma_addr_t *)p_pbl_virt);
4668 
4669 		p_pbl_virt += QED_CHAIN_PBL_ENTRY_SIZE;
4670 	}
4671 
4672 	pbl_size = page_cnt * QED_CHAIN_PBL_ENTRY_SIZE;
4673 
4674 	if (!p_chain->b_external_pbl)
4675 		dma_free_coherent(&cdev->pdev->dev,
4676 				  pbl_size,
4677 				  p_chain->pbl_sp.p_virt_table,
4678 				  p_chain->pbl_sp.p_phys_table);
4679 out:
4680 	vfree(p_chain->pbl.pp_virt_addr_tbl);
4681 	p_chain->pbl.pp_virt_addr_tbl = NULL;
4682 }
4683 
4684 void qed_chain_free(struct qed_dev *cdev, struct qed_chain *p_chain)
4685 {
4686 	switch (p_chain->mode) {
4687 	case QED_CHAIN_MODE_NEXT_PTR:
4688 		qed_chain_free_next_ptr(cdev, p_chain);
4689 		break;
4690 	case QED_CHAIN_MODE_SINGLE:
4691 		qed_chain_free_single(cdev, p_chain);
4692 		break;
4693 	case QED_CHAIN_MODE_PBL:
4694 		qed_chain_free_pbl(cdev, p_chain);
4695 		break;
4696 	}
4697 }
4698 
4699 static int
4700 qed_chain_alloc_sanity_check(struct qed_dev *cdev,
4701 			     enum qed_chain_cnt_type cnt_type,
4702 			     size_t elem_size, u32 page_cnt)
4703 {
4704 	u64 chain_size = ELEMS_PER_PAGE(elem_size) * page_cnt;
4705 
4706 	/* The actual chain size can be larger than the maximal possible value
4707 	 * after rounding up the requested elements number to pages, and after
4708 	 * taking into acount the unusuable elements (next-ptr elements).
4709 	 * The size of a "u16" chain can be (U16_MAX + 1) since the chain
4710 	 * size/capacity fields are of a u32 type.
4711 	 */
4712 	if ((cnt_type == QED_CHAIN_CNT_TYPE_U16 &&
4713 	     chain_size > ((u32)U16_MAX + 1)) ||
4714 	    (cnt_type == QED_CHAIN_CNT_TYPE_U32 && chain_size > U32_MAX)) {
4715 		DP_NOTICE(cdev,
4716 			  "The actual chain size (0x%llx) is larger than the maximal possible value\n",
4717 			  chain_size);
4718 		return -EINVAL;
4719 	}
4720 
4721 	return 0;
4722 }
4723 
4724 static int
4725 qed_chain_alloc_next_ptr(struct qed_dev *cdev, struct qed_chain *p_chain)
4726 {
4727 	void *p_virt = NULL, *p_virt_prev = NULL;
4728 	dma_addr_t p_phys = 0;
4729 	u32 i;
4730 
4731 	for (i = 0; i < p_chain->page_cnt; i++) {
4732 		p_virt = dma_alloc_coherent(&cdev->pdev->dev,
4733 					    QED_CHAIN_PAGE_SIZE,
4734 					    &p_phys, GFP_KERNEL);
4735 		if (!p_virt)
4736 			return -ENOMEM;
4737 
4738 		if (i == 0) {
4739 			qed_chain_init_mem(p_chain, p_virt, p_phys);
4740 			qed_chain_reset(p_chain);
4741 		} else {
4742 			qed_chain_init_next_ptr_elem(p_chain, p_virt_prev,
4743 						     p_virt, p_phys);
4744 		}
4745 
4746 		p_virt_prev = p_virt;
4747 	}
4748 	/* Last page's next element should point to the beginning of the
4749 	 * chain.
4750 	 */
4751 	qed_chain_init_next_ptr_elem(p_chain, p_virt_prev,
4752 				     p_chain->p_virt_addr,
4753 				     p_chain->p_phys_addr);
4754 
4755 	return 0;
4756 }
4757 
4758 static int
4759 qed_chain_alloc_single(struct qed_dev *cdev, struct qed_chain *p_chain)
4760 {
4761 	dma_addr_t p_phys = 0;
4762 	void *p_virt = NULL;
4763 
4764 	p_virt = dma_alloc_coherent(&cdev->pdev->dev,
4765 				    QED_CHAIN_PAGE_SIZE, &p_phys, GFP_KERNEL);
4766 	if (!p_virt)
4767 		return -ENOMEM;
4768 
4769 	qed_chain_init_mem(p_chain, p_virt, p_phys);
4770 	qed_chain_reset(p_chain);
4771 
4772 	return 0;
4773 }
4774 
4775 static int
4776 qed_chain_alloc_pbl(struct qed_dev *cdev,
4777 		    struct qed_chain *p_chain,
4778 		    struct qed_chain_ext_pbl *ext_pbl)
4779 {
4780 	u32 page_cnt = p_chain->page_cnt, size, i;
4781 	dma_addr_t p_phys = 0, p_pbl_phys = 0;
4782 	void **pp_virt_addr_tbl = NULL;
4783 	u8 *p_pbl_virt = NULL;
4784 	void *p_virt = NULL;
4785 
4786 	size = page_cnt * sizeof(*pp_virt_addr_tbl);
4787 	pp_virt_addr_tbl = vzalloc(size);
4788 	if (!pp_virt_addr_tbl)
4789 		return -ENOMEM;
4790 
4791 	/* The allocation of the PBL table is done with its full size, since it
4792 	 * is expected to be successive.
4793 	 * qed_chain_init_pbl_mem() is called even in a case of an allocation
4794 	 * failure, since pp_virt_addr_tbl was previously allocated, and it
4795 	 * should be saved to allow its freeing during the error flow.
4796 	 */
4797 	size = page_cnt * QED_CHAIN_PBL_ENTRY_SIZE;
4798 
4799 	if (!ext_pbl) {
4800 		p_pbl_virt = dma_alloc_coherent(&cdev->pdev->dev,
4801 						size, &p_pbl_phys, GFP_KERNEL);
4802 	} else {
4803 		p_pbl_virt = ext_pbl->p_pbl_virt;
4804 		p_pbl_phys = ext_pbl->p_pbl_phys;
4805 		p_chain->b_external_pbl = true;
4806 	}
4807 
4808 	qed_chain_init_pbl_mem(p_chain, p_pbl_virt, p_pbl_phys,
4809 			       pp_virt_addr_tbl);
4810 	if (!p_pbl_virt)
4811 		return -ENOMEM;
4812 
4813 	for (i = 0; i < page_cnt; i++) {
4814 		p_virt = dma_alloc_coherent(&cdev->pdev->dev,
4815 					    QED_CHAIN_PAGE_SIZE,
4816 					    &p_phys, GFP_KERNEL);
4817 		if (!p_virt)
4818 			return -ENOMEM;
4819 
4820 		if (i == 0) {
4821 			qed_chain_init_mem(p_chain, p_virt, p_phys);
4822 			qed_chain_reset(p_chain);
4823 		}
4824 
4825 		/* Fill the PBL table with the physical address of the page */
4826 		*(dma_addr_t *)p_pbl_virt = p_phys;
4827 		/* Keep the virtual address of the page */
4828 		p_chain->pbl.pp_virt_addr_tbl[i] = p_virt;
4829 
4830 		p_pbl_virt += QED_CHAIN_PBL_ENTRY_SIZE;
4831 	}
4832 
4833 	return 0;
4834 }
4835 
4836 int qed_chain_alloc(struct qed_dev *cdev,
4837 		    enum qed_chain_use_mode intended_use,
4838 		    enum qed_chain_mode mode,
4839 		    enum qed_chain_cnt_type cnt_type,
4840 		    u32 num_elems,
4841 		    size_t elem_size,
4842 		    struct qed_chain *p_chain,
4843 		    struct qed_chain_ext_pbl *ext_pbl)
4844 {
4845 	u32 page_cnt;
4846 	int rc = 0;
4847 
4848 	if (mode == QED_CHAIN_MODE_SINGLE)
4849 		page_cnt = 1;
4850 	else
4851 		page_cnt = QED_CHAIN_PAGE_CNT(num_elems, elem_size, mode);
4852 
4853 	rc = qed_chain_alloc_sanity_check(cdev, cnt_type, elem_size, page_cnt);
4854 	if (rc) {
4855 		DP_NOTICE(cdev,
4856 			  "Cannot allocate a chain with the given arguments:\n");
4857 		DP_NOTICE(cdev,
4858 			  "[use_mode %d, mode %d, cnt_type %d, num_elems %d, elem_size %zu]\n",
4859 			  intended_use, mode, cnt_type, num_elems, elem_size);
4860 		return rc;
4861 	}
4862 
4863 	qed_chain_init_params(p_chain, page_cnt, (u8) elem_size, intended_use,
4864 			      mode, cnt_type);
4865 
4866 	switch (mode) {
4867 	case QED_CHAIN_MODE_NEXT_PTR:
4868 		rc = qed_chain_alloc_next_ptr(cdev, p_chain);
4869 		break;
4870 	case QED_CHAIN_MODE_SINGLE:
4871 		rc = qed_chain_alloc_single(cdev, p_chain);
4872 		break;
4873 	case QED_CHAIN_MODE_PBL:
4874 		rc = qed_chain_alloc_pbl(cdev, p_chain, ext_pbl);
4875 		break;
4876 	}
4877 	if (rc)
4878 		goto nomem;
4879 
4880 	return 0;
4881 
4882 nomem:
4883 	qed_chain_free(cdev, p_chain);
4884 	return rc;
4885 }
4886 
4887 int qed_fw_l2_queue(struct qed_hwfn *p_hwfn, u16 src_id, u16 *dst_id)
4888 {
4889 	if (src_id >= RESC_NUM(p_hwfn, QED_L2_QUEUE)) {
4890 		u16 min, max;
4891 
4892 		min = (u16) RESC_START(p_hwfn, QED_L2_QUEUE);
4893 		max = min + RESC_NUM(p_hwfn, QED_L2_QUEUE);
4894 		DP_NOTICE(p_hwfn,
4895 			  "l2_queue id [%d] is not valid, available indices [%d - %d]\n",
4896 			  src_id, min, max);
4897 
4898 		return -EINVAL;
4899 	}
4900 
4901 	*dst_id = RESC_START(p_hwfn, QED_L2_QUEUE) + src_id;
4902 
4903 	return 0;
4904 }
4905 
4906 int qed_fw_vport(struct qed_hwfn *p_hwfn, u8 src_id, u8 *dst_id)
4907 {
4908 	if (src_id >= RESC_NUM(p_hwfn, QED_VPORT)) {
4909 		u8 min, max;
4910 
4911 		min = (u8)RESC_START(p_hwfn, QED_VPORT);
4912 		max = min + RESC_NUM(p_hwfn, QED_VPORT);
4913 		DP_NOTICE(p_hwfn,
4914 			  "vport id [%d] is not valid, available indices [%d - %d]\n",
4915 			  src_id, min, max);
4916 
4917 		return -EINVAL;
4918 	}
4919 
4920 	*dst_id = RESC_START(p_hwfn, QED_VPORT) + src_id;
4921 
4922 	return 0;
4923 }
4924 
4925 int qed_fw_rss_eng(struct qed_hwfn *p_hwfn, u8 src_id, u8 *dst_id)
4926 {
4927 	if (src_id >= RESC_NUM(p_hwfn, QED_RSS_ENG)) {
4928 		u8 min, max;
4929 
4930 		min = (u8)RESC_START(p_hwfn, QED_RSS_ENG);
4931 		max = min + RESC_NUM(p_hwfn, QED_RSS_ENG);
4932 		DP_NOTICE(p_hwfn,
4933 			  "rss_eng id [%d] is not valid, available indices [%d - %d]\n",
4934 			  src_id, min, max);
4935 
4936 		return -EINVAL;
4937 	}
4938 
4939 	*dst_id = RESC_START(p_hwfn, QED_RSS_ENG) + src_id;
4940 
4941 	return 0;
4942 }
4943 
4944 static int qed_set_coalesce(struct qed_hwfn *p_hwfn, struct qed_ptt *p_ptt,
4945 			    u32 hw_addr, void *p_eth_qzone,
4946 			    size_t eth_qzone_size, u8 timeset)
4947 {
4948 	struct coalescing_timeset *p_coal_timeset;
4949 
4950 	if (p_hwfn->cdev->int_coalescing_mode != QED_COAL_MODE_ENABLE) {
4951 		DP_NOTICE(p_hwfn, "Coalescing configuration not enabled\n");
4952 		return -EINVAL;
4953 	}
4954 
4955 	p_coal_timeset = p_eth_qzone;
4956 	memset(p_eth_qzone, 0, eth_qzone_size);
4957 	SET_FIELD(p_coal_timeset->value, COALESCING_TIMESET_TIMESET, timeset);
4958 	SET_FIELD(p_coal_timeset->value, COALESCING_TIMESET_VALID, 1);
4959 	qed_memcpy_to(p_hwfn, p_ptt, hw_addr, p_eth_qzone, eth_qzone_size);
4960 
4961 	return 0;
4962 }
4963 
4964 int qed_set_queue_coalesce(u16 rx_coal, u16 tx_coal, void *p_handle)
4965 {
4966 	struct qed_queue_cid *p_cid = p_handle;
4967 	struct qed_hwfn *p_hwfn;
4968 	struct qed_ptt *p_ptt;
4969 	int rc = 0;
4970 
4971 	p_hwfn = p_cid->p_owner;
4972 
4973 	if (IS_VF(p_hwfn->cdev))
4974 		return qed_vf_pf_set_coalesce(p_hwfn, rx_coal, tx_coal, p_cid);
4975 
4976 	p_ptt = qed_ptt_acquire(p_hwfn);
4977 	if (!p_ptt)
4978 		return -EAGAIN;
4979 
4980 	if (rx_coal) {
4981 		rc = qed_set_rxq_coalesce(p_hwfn, p_ptt, rx_coal, p_cid);
4982 		if (rc)
4983 			goto out;
4984 		p_hwfn->cdev->rx_coalesce_usecs = rx_coal;
4985 	}
4986 
4987 	if (tx_coal) {
4988 		rc = qed_set_txq_coalesce(p_hwfn, p_ptt, tx_coal, p_cid);
4989 		if (rc)
4990 			goto out;
4991 		p_hwfn->cdev->tx_coalesce_usecs = tx_coal;
4992 	}
4993 out:
4994 	qed_ptt_release(p_hwfn, p_ptt);
4995 	return rc;
4996 }
4997 
4998 int qed_set_rxq_coalesce(struct qed_hwfn *p_hwfn,
4999 			 struct qed_ptt *p_ptt,
5000 			 u16 coalesce, struct qed_queue_cid *p_cid)
5001 {
5002 	struct ustorm_eth_queue_zone eth_qzone;
5003 	u8 timeset, timer_res;
5004 	u32 address;
5005 	int rc;
5006 
5007 	/* Coalesce = (timeset << timer-resolution), timeset is 7bit wide */
5008 	if (coalesce <= 0x7F) {
5009 		timer_res = 0;
5010 	} else if (coalesce <= 0xFF) {
5011 		timer_res = 1;
5012 	} else if (coalesce <= 0x1FF) {
5013 		timer_res = 2;
5014 	} else {
5015 		DP_ERR(p_hwfn, "Invalid coalesce value - %d\n", coalesce);
5016 		return -EINVAL;
5017 	}
5018 	timeset = (u8)(coalesce >> timer_res);
5019 
5020 	rc = qed_int_set_timer_res(p_hwfn, p_ptt, timer_res,
5021 				   p_cid->sb_igu_id, false);
5022 	if (rc)
5023 		goto out;
5024 
5025 	address = BAR0_MAP_REG_USDM_RAM +
5026 		  USTORM_ETH_QUEUE_ZONE_OFFSET(p_cid->abs.queue_id);
5027 
5028 	rc = qed_set_coalesce(p_hwfn, p_ptt, address, &eth_qzone,
5029 			      sizeof(struct ustorm_eth_queue_zone), timeset);
5030 	if (rc)
5031 		goto out;
5032 
5033 out:
5034 	return rc;
5035 }
5036 
5037 int qed_set_txq_coalesce(struct qed_hwfn *p_hwfn,
5038 			 struct qed_ptt *p_ptt,
5039 			 u16 coalesce, struct qed_queue_cid *p_cid)
5040 {
5041 	struct xstorm_eth_queue_zone eth_qzone;
5042 	u8 timeset, timer_res;
5043 	u32 address;
5044 	int rc;
5045 
5046 	/* Coalesce = (timeset << timer-resolution), timeset is 7bit wide */
5047 	if (coalesce <= 0x7F) {
5048 		timer_res = 0;
5049 	} else if (coalesce <= 0xFF) {
5050 		timer_res = 1;
5051 	} else if (coalesce <= 0x1FF) {
5052 		timer_res = 2;
5053 	} else {
5054 		DP_ERR(p_hwfn, "Invalid coalesce value - %d\n", coalesce);
5055 		return -EINVAL;
5056 	}
5057 	timeset = (u8)(coalesce >> timer_res);
5058 
5059 	rc = qed_int_set_timer_res(p_hwfn, p_ptt, timer_res,
5060 				   p_cid->sb_igu_id, true);
5061 	if (rc)
5062 		goto out;
5063 
5064 	address = BAR0_MAP_REG_XSDM_RAM +
5065 		  XSTORM_ETH_QUEUE_ZONE_OFFSET(p_cid->abs.queue_id);
5066 
5067 	rc = qed_set_coalesce(p_hwfn, p_ptt, address, &eth_qzone,
5068 			      sizeof(struct xstorm_eth_queue_zone), timeset);
5069 out:
5070 	return rc;
5071 }
5072 
5073 /* Calculate final WFQ values for all vports and configure them.
5074  * After this configuration each vport will have
5075  * approx min rate =  min_pf_rate * (vport_wfq / QED_WFQ_UNIT)
5076  */
5077 static void qed_configure_wfq_for_all_vports(struct qed_hwfn *p_hwfn,
5078 					     struct qed_ptt *p_ptt,
5079 					     u32 min_pf_rate)
5080 {
5081 	struct init_qm_vport_params *vport_params;
5082 	int i;
5083 
5084 	vport_params = p_hwfn->qm_info.qm_vport_params;
5085 
5086 	for (i = 0; i < p_hwfn->qm_info.num_vports; i++) {
5087 		u32 wfq_speed = p_hwfn->qm_info.wfq_data[i].min_speed;
5088 
5089 		vport_params[i].vport_wfq = (wfq_speed * QED_WFQ_UNIT) /
5090 						min_pf_rate;
5091 		qed_init_vport_wfq(p_hwfn, p_ptt,
5092 				   vport_params[i].first_tx_pq_id,
5093 				   vport_params[i].vport_wfq);
5094 	}
5095 }
5096 
5097 static void qed_init_wfq_default_param(struct qed_hwfn *p_hwfn,
5098 				       u32 min_pf_rate)
5099 
5100 {
5101 	int i;
5102 
5103 	for (i = 0; i < p_hwfn->qm_info.num_vports; i++)
5104 		p_hwfn->qm_info.qm_vport_params[i].vport_wfq = 1;
5105 }
5106 
5107 static void qed_disable_wfq_for_all_vports(struct qed_hwfn *p_hwfn,
5108 					   struct qed_ptt *p_ptt,
5109 					   u32 min_pf_rate)
5110 {
5111 	struct init_qm_vport_params *vport_params;
5112 	int i;
5113 
5114 	vport_params = p_hwfn->qm_info.qm_vport_params;
5115 
5116 	for (i = 0; i < p_hwfn->qm_info.num_vports; i++) {
5117 		qed_init_wfq_default_param(p_hwfn, min_pf_rate);
5118 		qed_init_vport_wfq(p_hwfn, p_ptt,
5119 				   vport_params[i].first_tx_pq_id,
5120 				   vport_params[i].vport_wfq);
5121 	}
5122 }
5123 
5124 /* This function performs several validations for WFQ
5125  * configuration and required min rate for a given vport
5126  * 1. req_rate must be greater than one percent of min_pf_rate.
5127  * 2. req_rate should not cause other vports [not configured for WFQ explicitly]
5128  *    rates to get less than one percent of min_pf_rate.
5129  * 3. total_req_min_rate [all vports min rate sum] shouldn't exceed min_pf_rate.
5130  */
5131 static int qed_init_wfq_param(struct qed_hwfn *p_hwfn,
5132 			      u16 vport_id, u32 req_rate, u32 min_pf_rate)
5133 {
5134 	u32 total_req_min_rate = 0, total_left_rate = 0, left_rate_per_vp = 0;
5135 	int non_requested_count = 0, req_count = 0, i, num_vports;
5136 
5137 	num_vports = p_hwfn->qm_info.num_vports;
5138 
5139 	/* Accounting for the vports which are configured for WFQ explicitly */
5140 	for (i = 0; i < num_vports; i++) {
5141 		u32 tmp_speed;
5142 
5143 		if ((i != vport_id) &&
5144 		    p_hwfn->qm_info.wfq_data[i].configured) {
5145 			req_count++;
5146 			tmp_speed = p_hwfn->qm_info.wfq_data[i].min_speed;
5147 			total_req_min_rate += tmp_speed;
5148 		}
5149 	}
5150 
5151 	/* Include current vport data as well */
5152 	req_count++;
5153 	total_req_min_rate += req_rate;
5154 	non_requested_count = num_vports - req_count;
5155 
5156 	if (req_rate < min_pf_rate / QED_WFQ_UNIT) {
5157 		DP_VERBOSE(p_hwfn, NETIF_MSG_LINK,
5158 			   "Vport [%d] - Requested rate[%d Mbps] is less than one percent of configured PF min rate[%d Mbps]\n",
5159 			   vport_id, req_rate, min_pf_rate);
5160 		return -EINVAL;
5161 	}
5162 
5163 	if (num_vports > QED_WFQ_UNIT) {
5164 		DP_VERBOSE(p_hwfn, NETIF_MSG_LINK,
5165 			   "Number of vports is greater than %d\n",
5166 			   QED_WFQ_UNIT);
5167 		return -EINVAL;
5168 	}
5169 
5170 	if (total_req_min_rate > min_pf_rate) {
5171 		DP_VERBOSE(p_hwfn, NETIF_MSG_LINK,
5172 			   "Total requested min rate for all vports[%d Mbps] is greater than configured PF min rate[%d Mbps]\n",
5173 			   total_req_min_rate, min_pf_rate);
5174 		return -EINVAL;
5175 	}
5176 
5177 	total_left_rate	= min_pf_rate - total_req_min_rate;
5178 
5179 	left_rate_per_vp = total_left_rate / non_requested_count;
5180 	if (left_rate_per_vp <  min_pf_rate / QED_WFQ_UNIT) {
5181 		DP_VERBOSE(p_hwfn, NETIF_MSG_LINK,
5182 			   "Non WFQ configured vports rate [%d Mbps] is less than one percent of configured PF min rate[%d Mbps]\n",
5183 			   left_rate_per_vp, min_pf_rate);
5184 		return -EINVAL;
5185 	}
5186 
5187 	p_hwfn->qm_info.wfq_data[vport_id].min_speed = req_rate;
5188 	p_hwfn->qm_info.wfq_data[vport_id].configured = true;
5189 
5190 	for (i = 0; i < num_vports; i++) {
5191 		if (p_hwfn->qm_info.wfq_data[i].configured)
5192 			continue;
5193 
5194 		p_hwfn->qm_info.wfq_data[i].min_speed = left_rate_per_vp;
5195 	}
5196 
5197 	return 0;
5198 }
5199 
5200 static int __qed_configure_vport_wfq(struct qed_hwfn *p_hwfn,
5201 				     struct qed_ptt *p_ptt, u16 vp_id, u32 rate)
5202 {
5203 	struct qed_mcp_link_state *p_link;
5204 	int rc = 0;
5205 
5206 	p_link = &p_hwfn->cdev->hwfns[0].mcp_info->link_output;
5207 
5208 	if (!p_link->min_pf_rate) {
5209 		p_hwfn->qm_info.wfq_data[vp_id].min_speed = rate;
5210 		p_hwfn->qm_info.wfq_data[vp_id].configured = true;
5211 		return rc;
5212 	}
5213 
5214 	rc = qed_init_wfq_param(p_hwfn, vp_id, rate, p_link->min_pf_rate);
5215 
5216 	if (!rc)
5217 		qed_configure_wfq_for_all_vports(p_hwfn, p_ptt,
5218 						 p_link->min_pf_rate);
5219 	else
5220 		DP_NOTICE(p_hwfn,
5221 			  "Validation failed while configuring min rate\n");
5222 
5223 	return rc;
5224 }
5225 
5226 static int __qed_configure_vp_wfq_on_link_change(struct qed_hwfn *p_hwfn,
5227 						 struct qed_ptt *p_ptt,
5228 						 u32 min_pf_rate)
5229 {
5230 	bool use_wfq = false;
5231 	int rc = 0;
5232 	u16 i;
5233 
5234 	/* Validate all pre configured vports for wfq */
5235 	for (i = 0; i < p_hwfn->qm_info.num_vports; i++) {
5236 		u32 rate;
5237 
5238 		if (!p_hwfn->qm_info.wfq_data[i].configured)
5239 			continue;
5240 
5241 		rate = p_hwfn->qm_info.wfq_data[i].min_speed;
5242 		use_wfq = true;
5243 
5244 		rc = qed_init_wfq_param(p_hwfn, i, rate, min_pf_rate);
5245 		if (rc) {
5246 			DP_NOTICE(p_hwfn,
5247 				  "WFQ validation failed while configuring min rate\n");
5248 			break;
5249 		}
5250 	}
5251 
5252 	if (!rc && use_wfq)
5253 		qed_configure_wfq_for_all_vports(p_hwfn, p_ptt, min_pf_rate);
5254 	else
5255 		qed_disable_wfq_for_all_vports(p_hwfn, p_ptt, min_pf_rate);
5256 
5257 	return rc;
5258 }
5259 
5260 /* Main API for qed clients to configure vport min rate.
5261  * vp_id - vport id in PF Range[0 - (total_num_vports_per_pf - 1)]
5262  * rate - Speed in Mbps needs to be assigned to a given vport.
5263  */
5264 int qed_configure_vport_wfq(struct qed_dev *cdev, u16 vp_id, u32 rate)
5265 {
5266 	int i, rc = -EINVAL;
5267 
5268 	/* Currently not supported; Might change in future */
5269 	if (cdev->num_hwfns > 1) {
5270 		DP_NOTICE(cdev,
5271 			  "WFQ configuration is not supported for this device\n");
5272 		return rc;
5273 	}
5274 
5275 	for_each_hwfn(cdev, i) {
5276 		struct qed_hwfn *p_hwfn = &cdev->hwfns[i];
5277 		struct qed_ptt *p_ptt;
5278 
5279 		p_ptt = qed_ptt_acquire(p_hwfn);
5280 		if (!p_ptt)
5281 			return -EBUSY;
5282 
5283 		rc = __qed_configure_vport_wfq(p_hwfn, p_ptt, vp_id, rate);
5284 
5285 		if (rc) {
5286 			qed_ptt_release(p_hwfn, p_ptt);
5287 			return rc;
5288 		}
5289 
5290 		qed_ptt_release(p_hwfn, p_ptt);
5291 	}
5292 
5293 	return rc;
5294 }
5295 
5296 /* API to configure WFQ from mcp link change */
5297 void qed_configure_vp_wfq_on_link_change(struct qed_dev *cdev,
5298 					 struct qed_ptt *p_ptt, u32 min_pf_rate)
5299 {
5300 	int i;
5301 
5302 	if (cdev->num_hwfns > 1) {
5303 		DP_VERBOSE(cdev,
5304 			   NETIF_MSG_LINK,
5305 			   "WFQ configuration is not supported for this device\n");
5306 		return;
5307 	}
5308 
5309 	for_each_hwfn(cdev, i) {
5310 		struct qed_hwfn *p_hwfn = &cdev->hwfns[i];
5311 
5312 		__qed_configure_vp_wfq_on_link_change(p_hwfn, p_ptt,
5313 						      min_pf_rate);
5314 	}
5315 }
5316 
5317 int __qed_configure_pf_max_bandwidth(struct qed_hwfn *p_hwfn,
5318 				     struct qed_ptt *p_ptt,
5319 				     struct qed_mcp_link_state *p_link,
5320 				     u8 max_bw)
5321 {
5322 	int rc = 0;
5323 
5324 	p_hwfn->mcp_info->func_info.bandwidth_max = max_bw;
5325 
5326 	if (!p_link->line_speed && (max_bw != 100))
5327 		return rc;
5328 
5329 	p_link->speed = (p_link->line_speed * max_bw) / 100;
5330 	p_hwfn->qm_info.pf_rl = p_link->speed;
5331 
5332 	/* Since the limiter also affects Tx-switched traffic, we don't want it
5333 	 * to limit such traffic in case there's no actual limit.
5334 	 * In that case, set limit to imaginary high boundary.
5335 	 */
5336 	if (max_bw == 100)
5337 		p_hwfn->qm_info.pf_rl = 100000;
5338 
5339 	rc = qed_init_pf_rl(p_hwfn, p_ptt, p_hwfn->rel_pf_id,
5340 			    p_hwfn->qm_info.pf_rl);
5341 
5342 	DP_VERBOSE(p_hwfn, NETIF_MSG_LINK,
5343 		   "Configured MAX bandwidth to be %08x Mb/sec\n",
5344 		   p_link->speed);
5345 
5346 	return rc;
5347 }
5348 
5349 /* Main API to configure PF max bandwidth where bw range is [1 - 100] */
5350 int qed_configure_pf_max_bandwidth(struct qed_dev *cdev, u8 max_bw)
5351 {
5352 	int i, rc = -EINVAL;
5353 
5354 	if (max_bw < 1 || max_bw > 100) {
5355 		DP_NOTICE(cdev, "PF max bw valid range is [1-100]\n");
5356 		return rc;
5357 	}
5358 
5359 	for_each_hwfn(cdev, i) {
5360 		struct qed_hwfn	*p_hwfn = &cdev->hwfns[i];
5361 		struct qed_hwfn *p_lead = QED_LEADING_HWFN(cdev);
5362 		struct qed_mcp_link_state *p_link;
5363 		struct qed_ptt *p_ptt;
5364 
5365 		p_link = &p_lead->mcp_info->link_output;
5366 
5367 		p_ptt = qed_ptt_acquire(p_hwfn);
5368 		if (!p_ptt)
5369 			return -EBUSY;
5370 
5371 		rc = __qed_configure_pf_max_bandwidth(p_hwfn, p_ptt,
5372 						      p_link, max_bw);
5373 
5374 		qed_ptt_release(p_hwfn, p_ptt);
5375 
5376 		if (rc)
5377 			break;
5378 	}
5379 
5380 	return rc;
5381 }
5382 
5383 int __qed_configure_pf_min_bandwidth(struct qed_hwfn *p_hwfn,
5384 				     struct qed_ptt *p_ptt,
5385 				     struct qed_mcp_link_state *p_link,
5386 				     u8 min_bw)
5387 {
5388 	int rc = 0;
5389 
5390 	p_hwfn->mcp_info->func_info.bandwidth_min = min_bw;
5391 	p_hwfn->qm_info.pf_wfq = min_bw;
5392 
5393 	if (!p_link->line_speed)
5394 		return rc;
5395 
5396 	p_link->min_pf_rate = (p_link->line_speed * min_bw) / 100;
5397 
5398 	rc = qed_init_pf_wfq(p_hwfn, p_ptt, p_hwfn->rel_pf_id, min_bw);
5399 
5400 	DP_VERBOSE(p_hwfn, NETIF_MSG_LINK,
5401 		   "Configured MIN bandwidth to be %d Mb/sec\n",
5402 		   p_link->min_pf_rate);
5403 
5404 	return rc;
5405 }
5406 
5407 /* Main API to configure PF min bandwidth where bw range is [1-100] */
5408 int qed_configure_pf_min_bandwidth(struct qed_dev *cdev, u8 min_bw)
5409 {
5410 	int i, rc = -EINVAL;
5411 
5412 	if (min_bw < 1 || min_bw > 100) {
5413 		DP_NOTICE(cdev, "PF min bw valid range is [1-100]\n");
5414 		return rc;
5415 	}
5416 
5417 	for_each_hwfn(cdev, i) {
5418 		struct qed_hwfn *p_hwfn = &cdev->hwfns[i];
5419 		struct qed_hwfn *p_lead = QED_LEADING_HWFN(cdev);
5420 		struct qed_mcp_link_state *p_link;
5421 		struct qed_ptt *p_ptt;
5422 
5423 		p_link = &p_lead->mcp_info->link_output;
5424 
5425 		p_ptt = qed_ptt_acquire(p_hwfn);
5426 		if (!p_ptt)
5427 			return -EBUSY;
5428 
5429 		rc = __qed_configure_pf_min_bandwidth(p_hwfn, p_ptt,
5430 						      p_link, min_bw);
5431 		if (rc) {
5432 			qed_ptt_release(p_hwfn, p_ptt);
5433 			return rc;
5434 		}
5435 
5436 		if (p_link->min_pf_rate) {
5437 			u32 min_rate = p_link->min_pf_rate;
5438 
5439 			rc = __qed_configure_vp_wfq_on_link_change(p_hwfn,
5440 								   p_ptt,
5441 								   min_rate);
5442 		}
5443 
5444 		qed_ptt_release(p_hwfn, p_ptt);
5445 	}
5446 
5447 	return rc;
5448 }
5449 
5450 void qed_clean_wfq_db(struct qed_hwfn *p_hwfn, struct qed_ptt *p_ptt)
5451 {
5452 	struct qed_mcp_link_state *p_link;
5453 
5454 	p_link = &p_hwfn->mcp_info->link_output;
5455 
5456 	if (p_link->min_pf_rate)
5457 		qed_disable_wfq_for_all_vports(p_hwfn, p_ptt,
5458 					       p_link->min_pf_rate);
5459 
5460 	memset(p_hwfn->qm_info.wfq_data, 0,
5461 	       sizeof(*p_hwfn->qm_info.wfq_data) * p_hwfn->qm_info.num_vports);
5462 }
5463 
5464 int qed_device_num_ports(struct qed_dev *cdev)
5465 {
5466 	return cdev->num_ports;
5467 }
5468 
5469 void qed_set_fw_mac_addr(__le16 *fw_msb,
5470 			 __le16 *fw_mid, __le16 *fw_lsb, u8 *mac)
5471 {
5472 	((u8 *)fw_msb)[0] = mac[1];
5473 	((u8 *)fw_msb)[1] = mac[0];
5474 	((u8 *)fw_mid)[0] = mac[3];
5475 	((u8 *)fw_mid)[1] = mac[2];
5476 	((u8 *)fw_lsb)[0] = mac[5];
5477 	((u8 *)fw_lsb)[1] = mac[4];
5478 }
5479