1 /* bnx2x_main.c: QLogic Everest network driver.
2  *
3  * Copyright (c) 2007-2013 Broadcom Corporation
4  * Copyright (c) 2014 QLogic Corporation
5  * All rights reserved
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation.
10  *
11  * Maintained by: Ariel Elior <ariel.elior@qlogic.com>
12  * Written by: Eliezer Tamir
13  * Based on code from Michael Chan's bnx2 driver
14  * UDP CSUM errata workaround by Arik Gendelman
15  * Slowpath and fastpath rework by Vladislav Zolotarov
16  * Statistics and Link management by Yitchak Gertner
17  *
18  */
19 
20 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
21 
22 #include <linux/module.h>
23 #include <linux/moduleparam.h>
24 #include <linux/kernel.h>
25 #include <linux/device.h>  /* for dev_info() */
26 #include <linux/timer.h>
27 #include <linux/errno.h>
28 #include <linux/ioport.h>
29 #include <linux/slab.h>
30 #include <linux/interrupt.h>
31 #include <linux/pci.h>
32 #include <linux/aer.h>
33 #include <linux/init.h>
34 #include <linux/netdevice.h>
35 #include <linux/etherdevice.h>
36 #include <linux/skbuff.h>
37 #include <linux/dma-mapping.h>
38 #include <linux/bitops.h>
39 #include <linux/irq.h>
40 #include <linux/delay.h>
41 #include <asm/byteorder.h>
42 #include <linux/time.h>
43 #include <linux/ethtool.h>
44 #include <linux/mii.h>
45 #include <linux/if_vlan.h>
46 #include <linux/crash_dump.h>
47 #include <net/ip.h>
48 #include <net/ipv6.h>
49 #include <net/tcp.h>
50 #include <net/vxlan.h>
51 #include <net/checksum.h>
52 #include <net/ip6_checksum.h>
53 #include <linux/workqueue.h>
54 #include <linux/crc32.h>
55 #include <linux/crc32c.h>
56 #include <linux/prefetch.h>
57 #include <linux/zlib.h>
58 #include <linux/io.h>
59 #include <linux/semaphore.h>
60 #include <linux/stringify.h>
61 #include <linux/vmalloc.h>
62 #include "bnx2x.h"
63 #include "bnx2x_init.h"
64 #include "bnx2x_init_ops.h"
65 #include "bnx2x_cmn.h"
66 #include "bnx2x_vfpf.h"
67 #include "bnx2x_dcb.h"
68 #include "bnx2x_sp.h"
69 #include <linux/firmware.h>
70 #include "bnx2x_fw_file_hdr.h"
71 /* FW files */
72 #define FW_FILE_VERSION					\
73 	__stringify(BCM_5710_FW_MAJOR_VERSION) "."	\
74 	__stringify(BCM_5710_FW_MINOR_VERSION) "."	\
75 	__stringify(BCM_5710_FW_REVISION_VERSION) "."	\
76 	__stringify(BCM_5710_FW_ENGINEERING_VERSION)
77 #define FW_FILE_NAME_E1		"bnx2x/bnx2x-e1-" FW_FILE_VERSION ".fw"
78 #define FW_FILE_NAME_E1H	"bnx2x/bnx2x-e1h-" FW_FILE_VERSION ".fw"
79 #define FW_FILE_NAME_E2		"bnx2x/bnx2x-e2-" FW_FILE_VERSION ".fw"
80 
81 /* Time in jiffies before concluding the transmitter is hung */
82 #define TX_TIMEOUT		(5*HZ)
83 
84 MODULE_AUTHOR("Eliezer Tamir");
85 MODULE_DESCRIPTION("QLogic "
86 		   "BCM57710/57711/57711E/"
87 		   "57712/57712_MF/57800/57800_MF/57810/57810_MF/"
88 		   "57840/57840_MF Driver");
89 MODULE_LICENSE("GPL");
90 MODULE_FIRMWARE(FW_FILE_NAME_E1);
91 MODULE_FIRMWARE(FW_FILE_NAME_E1H);
92 MODULE_FIRMWARE(FW_FILE_NAME_E2);
93 
94 int bnx2x_num_queues;
95 module_param_named(num_queues, bnx2x_num_queues, int, 0444);
96 MODULE_PARM_DESC(num_queues,
97 		 " Set number of queues (default is as a number of CPUs)");
98 
99 static int disable_tpa;
100 module_param(disable_tpa, int, 0444);
101 MODULE_PARM_DESC(disable_tpa, " Disable the TPA (LRO) feature");
102 
103 static int int_mode;
104 module_param(int_mode, int, 0444);
105 MODULE_PARM_DESC(int_mode, " Force interrupt mode other than MSI-X "
106 				"(1 INT#x; 2 MSI)");
107 
108 static int dropless_fc;
109 module_param(dropless_fc, int, 0444);
110 MODULE_PARM_DESC(dropless_fc, " Pause on exhausted host ring");
111 
112 static int mrrs = -1;
113 module_param(mrrs, int, 0444);
114 MODULE_PARM_DESC(mrrs, " Force Max Read Req Size (0..3) (for debug)");
115 
116 static int debug;
117 module_param(debug, int, 0444);
118 MODULE_PARM_DESC(debug, " Default debug msglevel");
119 
120 static struct workqueue_struct *bnx2x_wq;
121 struct workqueue_struct *bnx2x_iov_wq;
122 
123 struct bnx2x_mac_vals {
124 	u32 xmac_addr;
125 	u32 xmac_val;
126 	u32 emac_addr;
127 	u32 emac_val;
128 	u32 umac_addr[2];
129 	u32 umac_val[2];
130 	u32 bmac_addr;
131 	u32 bmac_val[2];
132 };
133 
134 enum bnx2x_board_type {
135 	BCM57710 = 0,
136 	BCM57711,
137 	BCM57711E,
138 	BCM57712,
139 	BCM57712_MF,
140 	BCM57712_VF,
141 	BCM57800,
142 	BCM57800_MF,
143 	BCM57800_VF,
144 	BCM57810,
145 	BCM57810_MF,
146 	BCM57810_VF,
147 	BCM57840_4_10,
148 	BCM57840_2_20,
149 	BCM57840_MF,
150 	BCM57840_VF,
151 	BCM57811,
152 	BCM57811_MF,
153 	BCM57840_O,
154 	BCM57840_MFO,
155 	BCM57811_VF
156 };
157 
158 /* indexed by board_type, above */
159 static struct {
160 	char *name;
161 } board_info[] = {
162 	[BCM57710]	= { "QLogic BCM57710 10 Gigabit PCIe [Everest]" },
163 	[BCM57711]	= { "QLogic BCM57711 10 Gigabit PCIe" },
164 	[BCM57711E]	= { "QLogic BCM57711E 10 Gigabit PCIe" },
165 	[BCM57712]	= { "QLogic BCM57712 10 Gigabit Ethernet" },
166 	[BCM57712_MF]	= { "QLogic BCM57712 10 Gigabit Ethernet Multi Function" },
167 	[BCM57712_VF]	= { "QLogic BCM57712 10 Gigabit Ethernet Virtual Function" },
168 	[BCM57800]	= { "QLogic BCM57800 10 Gigabit Ethernet" },
169 	[BCM57800_MF]	= { "QLogic BCM57800 10 Gigabit Ethernet Multi Function" },
170 	[BCM57800_VF]	= { "QLogic BCM57800 10 Gigabit Ethernet Virtual Function" },
171 	[BCM57810]	= { "QLogic BCM57810 10 Gigabit Ethernet" },
172 	[BCM57810_MF]	= { "QLogic BCM57810 10 Gigabit Ethernet Multi Function" },
173 	[BCM57810_VF]	= { "QLogic BCM57810 10 Gigabit Ethernet Virtual Function" },
174 	[BCM57840_4_10]	= { "QLogic BCM57840 10 Gigabit Ethernet" },
175 	[BCM57840_2_20]	= { "QLogic BCM57840 20 Gigabit Ethernet" },
176 	[BCM57840_MF]	= { "QLogic BCM57840 10/20 Gigabit Ethernet Multi Function" },
177 	[BCM57840_VF]	= { "QLogic BCM57840 10/20 Gigabit Ethernet Virtual Function" },
178 	[BCM57811]	= { "QLogic BCM57811 10 Gigabit Ethernet" },
179 	[BCM57811_MF]	= { "QLogic BCM57811 10 Gigabit Ethernet Multi Function" },
180 	[BCM57840_O]	= { "QLogic BCM57840 10/20 Gigabit Ethernet" },
181 	[BCM57840_MFO]	= { "QLogic BCM57840 10/20 Gigabit Ethernet Multi Function" },
182 	[BCM57811_VF]	= { "QLogic BCM57840 10/20 Gigabit Ethernet Virtual Function" }
183 };
184 
185 #ifndef PCI_DEVICE_ID_NX2_57710
186 #define PCI_DEVICE_ID_NX2_57710		CHIP_NUM_57710
187 #endif
188 #ifndef PCI_DEVICE_ID_NX2_57711
189 #define PCI_DEVICE_ID_NX2_57711		CHIP_NUM_57711
190 #endif
191 #ifndef PCI_DEVICE_ID_NX2_57711E
192 #define PCI_DEVICE_ID_NX2_57711E	CHIP_NUM_57711E
193 #endif
194 #ifndef PCI_DEVICE_ID_NX2_57712
195 #define PCI_DEVICE_ID_NX2_57712		CHIP_NUM_57712
196 #endif
197 #ifndef PCI_DEVICE_ID_NX2_57712_MF
198 #define PCI_DEVICE_ID_NX2_57712_MF	CHIP_NUM_57712_MF
199 #endif
200 #ifndef PCI_DEVICE_ID_NX2_57712_VF
201 #define PCI_DEVICE_ID_NX2_57712_VF	CHIP_NUM_57712_VF
202 #endif
203 #ifndef PCI_DEVICE_ID_NX2_57800
204 #define PCI_DEVICE_ID_NX2_57800		CHIP_NUM_57800
205 #endif
206 #ifndef PCI_DEVICE_ID_NX2_57800_MF
207 #define PCI_DEVICE_ID_NX2_57800_MF	CHIP_NUM_57800_MF
208 #endif
209 #ifndef PCI_DEVICE_ID_NX2_57800_VF
210 #define PCI_DEVICE_ID_NX2_57800_VF	CHIP_NUM_57800_VF
211 #endif
212 #ifndef PCI_DEVICE_ID_NX2_57810
213 #define PCI_DEVICE_ID_NX2_57810		CHIP_NUM_57810
214 #endif
215 #ifndef PCI_DEVICE_ID_NX2_57810_MF
216 #define PCI_DEVICE_ID_NX2_57810_MF	CHIP_NUM_57810_MF
217 #endif
218 #ifndef PCI_DEVICE_ID_NX2_57840_O
219 #define PCI_DEVICE_ID_NX2_57840_O	CHIP_NUM_57840_OBSOLETE
220 #endif
221 #ifndef PCI_DEVICE_ID_NX2_57810_VF
222 #define PCI_DEVICE_ID_NX2_57810_VF	CHIP_NUM_57810_VF
223 #endif
224 #ifndef PCI_DEVICE_ID_NX2_57840_4_10
225 #define PCI_DEVICE_ID_NX2_57840_4_10	CHIP_NUM_57840_4_10
226 #endif
227 #ifndef PCI_DEVICE_ID_NX2_57840_2_20
228 #define PCI_DEVICE_ID_NX2_57840_2_20	CHIP_NUM_57840_2_20
229 #endif
230 #ifndef PCI_DEVICE_ID_NX2_57840_MFO
231 #define PCI_DEVICE_ID_NX2_57840_MFO	CHIP_NUM_57840_MF_OBSOLETE
232 #endif
233 #ifndef PCI_DEVICE_ID_NX2_57840_MF
234 #define PCI_DEVICE_ID_NX2_57840_MF	CHIP_NUM_57840_MF
235 #endif
236 #ifndef PCI_DEVICE_ID_NX2_57840_VF
237 #define PCI_DEVICE_ID_NX2_57840_VF	CHIP_NUM_57840_VF
238 #endif
239 #ifndef PCI_DEVICE_ID_NX2_57811
240 #define PCI_DEVICE_ID_NX2_57811		CHIP_NUM_57811
241 #endif
242 #ifndef PCI_DEVICE_ID_NX2_57811_MF
243 #define PCI_DEVICE_ID_NX2_57811_MF	CHIP_NUM_57811_MF
244 #endif
245 #ifndef PCI_DEVICE_ID_NX2_57811_VF
246 #define PCI_DEVICE_ID_NX2_57811_VF	CHIP_NUM_57811_VF
247 #endif
248 
249 static const struct pci_device_id bnx2x_pci_tbl[] = {
250 	{ PCI_VDEVICE(BROADCOM, PCI_DEVICE_ID_NX2_57710), BCM57710 },
251 	{ PCI_VDEVICE(BROADCOM, PCI_DEVICE_ID_NX2_57711), BCM57711 },
252 	{ PCI_VDEVICE(BROADCOM, PCI_DEVICE_ID_NX2_57711E), BCM57711E },
253 	{ PCI_VDEVICE(BROADCOM, PCI_DEVICE_ID_NX2_57712), BCM57712 },
254 	{ PCI_VDEVICE(BROADCOM, PCI_DEVICE_ID_NX2_57712_MF), BCM57712_MF },
255 	{ PCI_VDEVICE(BROADCOM, PCI_DEVICE_ID_NX2_57712_VF), BCM57712_VF },
256 	{ PCI_VDEVICE(BROADCOM, PCI_DEVICE_ID_NX2_57800), BCM57800 },
257 	{ PCI_VDEVICE(BROADCOM, PCI_DEVICE_ID_NX2_57800_MF), BCM57800_MF },
258 	{ PCI_VDEVICE(BROADCOM, PCI_DEVICE_ID_NX2_57800_VF), BCM57800_VF },
259 	{ PCI_VDEVICE(BROADCOM, PCI_DEVICE_ID_NX2_57810), BCM57810 },
260 	{ PCI_VDEVICE(BROADCOM, PCI_DEVICE_ID_NX2_57810_MF), BCM57810_MF },
261 	{ PCI_VDEVICE(BROADCOM, PCI_DEVICE_ID_NX2_57840_O), BCM57840_O },
262 	{ PCI_VDEVICE(BROADCOM, PCI_DEVICE_ID_NX2_57840_4_10), BCM57840_4_10 },
263 	{ PCI_VDEVICE(QLOGIC,	PCI_DEVICE_ID_NX2_57840_4_10), BCM57840_4_10 },
264 	{ PCI_VDEVICE(BROADCOM, PCI_DEVICE_ID_NX2_57840_2_20), BCM57840_2_20 },
265 	{ PCI_VDEVICE(BROADCOM, PCI_DEVICE_ID_NX2_57810_VF), BCM57810_VF },
266 	{ PCI_VDEVICE(BROADCOM, PCI_DEVICE_ID_NX2_57840_MFO), BCM57840_MFO },
267 	{ PCI_VDEVICE(BROADCOM, PCI_DEVICE_ID_NX2_57840_MF), BCM57840_MF },
268 	{ PCI_VDEVICE(QLOGIC,	PCI_DEVICE_ID_NX2_57840_MF), BCM57840_MF },
269 	{ PCI_VDEVICE(BROADCOM, PCI_DEVICE_ID_NX2_57840_VF), BCM57840_VF },
270 	{ PCI_VDEVICE(QLOGIC,	PCI_DEVICE_ID_NX2_57840_VF), BCM57840_VF },
271 	{ PCI_VDEVICE(BROADCOM, PCI_DEVICE_ID_NX2_57811), BCM57811 },
272 	{ PCI_VDEVICE(BROADCOM, PCI_DEVICE_ID_NX2_57811_MF), BCM57811_MF },
273 	{ PCI_VDEVICE(BROADCOM, PCI_DEVICE_ID_NX2_57811_VF), BCM57811_VF },
274 	{ 0 }
275 };
276 
277 MODULE_DEVICE_TABLE(pci, bnx2x_pci_tbl);
278 
279 /* Global resources for unloading a previously loaded device */
280 #define BNX2X_PREV_WAIT_NEEDED 1
281 static DEFINE_SEMAPHORE(bnx2x_prev_sem);
282 static LIST_HEAD(bnx2x_prev_list);
283 
284 /* Forward declaration */
285 static struct cnic_eth_dev *bnx2x_cnic_probe(struct net_device *dev);
286 static u32 bnx2x_rx_ustorm_prods_offset(struct bnx2x_fastpath *fp);
287 static int bnx2x_set_storm_rx_mode(struct bnx2x *bp);
288 
289 /****************************************************************************
290 * General service functions
291 ****************************************************************************/
292 
293 static int bnx2x_hwtstamp_ioctl(struct bnx2x *bp, struct ifreq *ifr);
294 
295 static void __storm_memset_dma_mapping(struct bnx2x *bp,
296 				       u32 addr, dma_addr_t mapping)
297 {
298 	REG_WR(bp,  addr, U64_LO(mapping));
299 	REG_WR(bp,  addr + 4, U64_HI(mapping));
300 }
301 
302 static void storm_memset_spq_addr(struct bnx2x *bp,
303 				  dma_addr_t mapping, u16 abs_fid)
304 {
305 	u32 addr = XSEM_REG_FAST_MEMORY +
306 			XSTORM_SPQ_PAGE_BASE_OFFSET(abs_fid);
307 
308 	__storm_memset_dma_mapping(bp, addr, mapping);
309 }
310 
311 static void storm_memset_vf_to_pf(struct bnx2x *bp, u16 abs_fid,
312 				  u16 pf_id)
313 {
314 	REG_WR8(bp, BAR_XSTRORM_INTMEM + XSTORM_VF_TO_PF_OFFSET(abs_fid),
315 		pf_id);
316 	REG_WR8(bp, BAR_CSTRORM_INTMEM + CSTORM_VF_TO_PF_OFFSET(abs_fid),
317 		pf_id);
318 	REG_WR8(bp, BAR_TSTRORM_INTMEM + TSTORM_VF_TO_PF_OFFSET(abs_fid),
319 		pf_id);
320 	REG_WR8(bp, BAR_USTRORM_INTMEM + USTORM_VF_TO_PF_OFFSET(abs_fid),
321 		pf_id);
322 }
323 
324 static void storm_memset_func_en(struct bnx2x *bp, u16 abs_fid,
325 				 u8 enable)
326 {
327 	REG_WR8(bp, BAR_XSTRORM_INTMEM + XSTORM_FUNC_EN_OFFSET(abs_fid),
328 		enable);
329 	REG_WR8(bp, BAR_CSTRORM_INTMEM + CSTORM_FUNC_EN_OFFSET(abs_fid),
330 		enable);
331 	REG_WR8(bp, BAR_TSTRORM_INTMEM + TSTORM_FUNC_EN_OFFSET(abs_fid),
332 		enable);
333 	REG_WR8(bp, BAR_USTRORM_INTMEM + USTORM_FUNC_EN_OFFSET(abs_fid),
334 		enable);
335 }
336 
337 static void storm_memset_eq_data(struct bnx2x *bp,
338 				 struct event_ring_data *eq_data,
339 				u16 pfid)
340 {
341 	size_t size = sizeof(struct event_ring_data);
342 
343 	u32 addr = BAR_CSTRORM_INTMEM + CSTORM_EVENT_RING_DATA_OFFSET(pfid);
344 
345 	__storm_memset_struct(bp, addr, size, (u32 *)eq_data);
346 }
347 
348 static void storm_memset_eq_prod(struct bnx2x *bp, u16 eq_prod,
349 				 u16 pfid)
350 {
351 	u32 addr = BAR_CSTRORM_INTMEM + CSTORM_EVENT_RING_PROD_OFFSET(pfid);
352 	REG_WR16(bp, addr, eq_prod);
353 }
354 
355 /* used only at init
356  * locking is done by mcp
357  */
358 static void bnx2x_reg_wr_ind(struct bnx2x *bp, u32 addr, u32 val)
359 {
360 	pci_write_config_dword(bp->pdev, PCICFG_GRC_ADDRESS, addr);
361 	pci_write_config_dword(bp->pdev, PCICFG_GRC_DATA, val);
362 	pci_write_config_dword(bp->pdev, PCICFG_GRC_ADDRESS,
363 			       PCICFG_VENDOR_ID_OFFSET);
364 }
365 
366 static u32 bnx2x_reg_rd_ind(struct bnx2x *bp, u32 addr)
367 {
368 	u32 val;
369 
370 	pci_write_config_dword(bp->pdev, PCICFG_GRC_ADDRESS, addr);
371 	pci_read_config_dword(bp->pdev, PCICFG_GRC_DATA, &val);
372 	pci_write_config_dword(bp->pdev, PCICFG_GRC_ADDRESS,
373 			       PCICFG_VENDOR_ID_OFFSET);
374 
375 	return val;
376 }
377 
378 #define DMAE_DP_SRC_GRC		"grc src_addr [%08x]"
379 #define DMAE_DP_SRC_PCI		"pci src_addr [%x:%08x]"
380 #define DMAE_DP_DST_GRC		"grc dst_addr [%08x]"
381 #define DMAE_DP_DST_PCI		"pci dst_addr [%x:%08x]"
382 #define DMAE_DP_DST_NONE	"dst_addr [none]"
383 
384 static void bnx2x_dp_dmae(struct bnx2x *bp,
385 			  struct dmae_command *dmae, int msglvl)
386 {
387 	u32 src_type = dmae->opcode & DMAE_COMMAND_SRC;
388 	int i;
389 
390 	switch (dmae->opcode & DMAE_COMMAND_DST) {
391 	case DMAE_CMD_DST_PCI:
392 		if (src_type == DMAE_CMD_SRC_PCI)
393 			DP(msglvl, "DMAE: opcode 0x%08x\n"
394 			   "src [%x:%08x], len [%d*4], dst [%x:%08x]\n"
395 			   "comp_addr [%x:%08x], comp_val 0x%08x\n",
396 			   dmae->opcode, dmae->src_addr_hi, dmae->src_addr_lo,
397 			   dmae->len, dmae->dst_addr_hi, dmae->dst_addr_lo,
398 			   dmae->comp_addr_hi, dmae->comp_addr_lo,
399 			   dmae->comp_val);
400 		else
401 			DP(msglvl, "DMAE: opcode 0x%08x\n"
402 			   "src [%08x], len [%d*4], dst [%x:%08x]\n"
403 			   "comp_addr [%x:%08x], comp_val 0x%08x\n",
404 			   dmae->opcode, dmae->src_addr_lo >> 2,
405 			   dmae->len, dmae->dst_addr_hi, dmae->dst_addr_lo,
406 			   dmae->comp_addr_hi, dmae->comp_addr_lo,
407 			   dmae->comp_val);
408 		break;
409 	case DMAE_CMD_DST_GRC:
410 		if (src_type == DMAE_CMD_SRC_PCI)
411 			DP(msglvl, "DMAE: opcode 0x%08x\n"
412 			   "src [%x:%08x], len [%d*4], dst_addr [%08x]\n"
413 			   "comp_addr [%x:%08x], comp_val 0x%08x\n",
414 			   dmae->opcode, dmae->src_addr_hi, dmae->src_addr_lo,
415 			   dmae->len, dmae->dst_addr_lo >> 2,
416 			   dmae->comp_addr_hi, dmae->comp_addr_lo,
417 			   dmae->comp_val);
418 		else
419 			DP(msglvl, "DMAE: opcode 0x%08x\n"
420 			   "src [%08x], len [%d*4], dst [%08x]\n"
421 			   "comp_addr [%x:%08x], comp_val 0x%08x\n",
422 			   dmae->opcode, dmae->src_addr_lo >> 2,
423 			   dmae->len, dmae->dst_addr_lo >> 2,
424 			   dmae->comp_addr_hi, dmae->comp_addr_lo,
425 			   dmae->comp_val);
426 		break;
427 	default:
428 		if (src_type == DMAE_CMD_SRC_PCI)
429 			DP(msglvl, "DMAE: opcode 0x%08x\n"
430 			   "src_addr [%x:%08x]  len [%d * 4]  dst_addr [none]\n"
431 			   "comp_addr [%x:%08x]  comp_val 0x%08x\n",
432 			   dmae->opcode, dmae->src_addr_hi, dmae->src_addr_lo,
433 			   dmae->len, dmae->comp_addr_hi, dmae->comp_addr_lo,
434 			   dmae->comp_val);
435 		else
436 			DP(msglvl, "DMAE: opcode 0x%08x\n"
437 			   "src_addr [%08x]  len [%d * 4]  dst_addr [none]\n"
438 			   "comp_addr [%x:%08x]  comp_val 0x%08x\n",
439 			   dmae->opcode, dmae->src_addr_lo >> 2,
440 			   dmae->len, dmae->comp_addr_hi, dmae->comp_addr_lo,
441 			   dmae->comp_val);
442 		break;
443 	}
444 
445 	for (i = 0; i < (sizeof(struct dmae_command)/4); i++)
446 		DP(msglvl, "DMAE RAW [%02d]: 0x%08x\n",
447 		   i, *(((u32 *)dmae) + i));
448 }
449 
450 /* copy command into DMAE command memory and set DMAE command go */
451 void bnx2x_post_dmae(struct bnx2x *bp, struct dmae_command *dmae, int idx)
452 {
453 	u32 cmd_offset;
454 	int i;
455 
456 	cmd_offset = (DMAE_REG_CMD_MEM + sizeof(struct dmae_command) * idx);
457 	for (i = 0; i < (sizeof(struct dmae_command)/4); i++) {
458 		REG_WR(bp, cmd_offset + i*4, *(((u32 *)dmae) + i));
459 	}
460 	REG_WR(bp, dmae_reg_go_c[idx], 1);
461 }
462 
463 u32 bnx2x_dmae_opcode_add_comp(u32 opcode, u8 comp_type)
464 {
465 	return opcode | ((comp_type << DMAE_COMMAND_C_DST_SHIFT) |
466 			   DMAE_CMD_C_ENABLE);
467 }
468 
469 u32 bnx2x_dmae_opcode_clr_src_reset(u32 opcode)
470 {
471 	return opcode & ~DMAE_CMD_SRC_RESET;
472 }
473 
474 u32 bnx2x_dmae_opcode(struct bnx2x *bp, u8 src_type, u8 dst_type,
475 			     bool with_comp, u8 comp_type)
476 {
477 	u32 opcode = 0;
478 
479 	opcode |= ((src_type << DMAE_COMMAND_SRC_SHIFT) |
480 		   (dst_type << DMAE_COMMAND_DST_SHIFT));
481 
482 	opcode |= (DMAE_CMD_SRC_RESET | DMAE_CMD_DST_RESET);
483 
484 	opcode |= (BP_PORT(bp) ? DMAE_CMD_PORT_1 : DMAE_CMD_PORT_0);
485 	opcode |= ((BP_VN(bp) << DMAE_CMD_E1HVN_SHIFT) |
486 		   (BP_VN(bp) << DMAE_COMMAND_DST_VN_SHIFT));
487 	opcode |= (DMAE_COM_SET_ERR << DMAE_COMMAND_ERR_POLICY_SHIFT);
488 
489 #ifdef __BIG_ENDIAN
490 	opcode |= DMAE_CMD_ENDIANITY_B_DW_SWAP;
491 #else
492 	opcode |= DMAE_CMD_ENDIANITY_DW_SWAP;
493 #endif
494 	if (with_comp)
495 		opcode = bnx2x_dmae_opcode_add_comp(opcode, comp_type);
496 	return opcode;
497 }
498 
499 void bnx2x_prep_dmae_with_comp(struct bnx2x *bp,
500 				      struct dmae_command *dmae,
501 				      u8 src_type, u8 dst_type)
502 {
503 	memset(dmae, 0, sizeof(struct dmae_command));
504 
505 	/* set the opcode */
506 	dmae->opcode = bnx2x_dmae_opcode(bp, src_type, dst_type,
507 					 true, DMAE_COMP_PCI);
508 
509 	/* fill in the completion parameters */
510 	dmae->comp_addr_lo = U64_LO(bnx2x_sp_mapping(bp, wb_comp));
511 	dmae->comp_addr_hi = U64_HI(bnx2x_sp_mapping(bp, wb_comp));
512 	dmae->comp_val = DMAE_COMP_VAL;
513 }
514 
515 /* issue a dmae command over the init-channel and wait for completion */
516 int bnx2x_issue_dmae_with_comp(struct bnx2x *bp, struct dmae_command *dmae,
517 			       u32 *comp)
518 {
519 	int cnt = CHIP_REV_IS_SLOW(bp) ? (400000) : 4000;
520 	int rc = 0;
521 
522 	bnx2x_dp_dmae(bp, dmae, BNX2X_MSG_DMAE);
523 
524 	/* Lock the dmae channel. Disable BHs to prevent a dead-lock
525 	 * as long as this code is called both from syscall context and
526 	 * from ndo_set_rx_mode() flow that may be called from BH.
527 	 */
528 
529 	spin_lock_bh(&bp->dmae_lock);
530 
531 	/* reset completion */
532 	*comp = 0;
533 
534 	/* post the command on the channel used for initializations */
535 	bnx2x_post_dmae(bp, dmae, INIT_DMAE_C(bp));
536 
537 	/* wait for completion */
538 	udelay(5);
539 	while ((*comp & ~DMAE_PCI_ERR_FLAG) != DMAE_COMP_VAL) {
540 
541 		if (!cnt ||
542 		    (bp->recovery_state != BNX2X_RECOVERY_DONE &&
543 		     bp->recovery_state != BNX2X_RECOVERY_NIC_LOADING)) {
544 			BNX2X_ERR("DMAE timeout!\n");
545 			rc = DMAE_TIMEOUT;
546 			goto unlock;
547 		}
548 		cnt--;
549 		udelay(50);
550 	}
551 	if (*comp & DMAE_PCI_ERR_FLAG) {
552 		BNX2X_ERR("DMAE PCI error!\n");
553 		rc = DMAE_PCI_ERROR;
554 	}
555 
556 unlock:
557 
558 	spin_unlock_bh(&bp->dmae_lock);
559 
560 	return rc;
561 }
562 
563 void bnx2x_write_dmae(struct bnx2x *bp, dma_addr_t dma_addr, u32 dst_addr,
564 		      u32 len32)
565 {
566 	int rc;
567 	struct dmae_command dmae;
568 
569 	if (!bp->dmae_ready) {
570 		u32 *data = bnx2x_sp(bp, wb_data[0]);
571 
572 		if (CHIP_IS_E1(bp))
573 			bnx2x_init_ind_wr(bp, dst_addr, data, len32);
574 		else
575 			bnx2x_init_str_wr(bp, dst_addr, data, len32);
576 		return;
577 	}
578 
579 	/* set opcode and fixed command fields */
580 	bnx2x_prep_dmae_with_comp(bp, &dmae, DMAE_SRC_PCI, DMAE_DST_GRC);
581 
582 	/* fill in addresses and len */
583 	dmae.src_addr_lo = U64_LO(dma_addr);
584 	dmae.src_addr_hi = U64_HI(dma_addr);
585 	dmae.dst_addr_lo = dst_addr >> 2;
586 	dmae.dst_addr_hi = 0;
587 	dmae.len = len32;
588 
589 	/* issue the command and wait for completion */
590 	rc = bnx2x_issue_dmae_with_comp(bp, &dmae, bnx2x_sp(bp, wb_comp));
591 	if (rc) {
592 		BNX2X_ERR("DMAE returned failure %d\n", rc);
593 #ifdef BNX2X_STOP_ON_ERROR
594 		bnx2x_panic();
595 #endif
596 	}
597 }
598 
599 void bnx2x_read_dmae(struct bnx2x *bp, u32 src_addr, u32 len32)
600 {
601 	int rc;
602 	struct dmae_command dmae;
603 
604 	if (!bp->dmae_ready) {
605 		u32 *data = bnx2x_sp(bp, wb_data[0]);
606 		int i;
607 
608 		if (CHIP_IS_E1(bp))
609 			for (i = 0; i < len32; i++)
610 				data[i] = bnx2x_reg_rd_ind(bp, src_addr + i*4);
611 		else
612 			for (i = 0; i < len32; i++)
613 				data[i] = REG_RD(bp, src_addr + i*4);
614 
615 		return;
616 	}
617 
618 	/* set opcode and fixed command fields */
619 	bnx2x_prep_dmae_with_comp(bp, &dmae, DMAE_SRC_GRC, DMAE_DST_PCI);
620 
621 	/* fill in addresses and len */
622 	dmae.src_addr_lo = src_addr >> 2;
623 	dmae.src_addr_hi = 0;
624 	dmae.dst_addr_lo = U64_LO(bnx2x_sp_mapping(bp, wb_data));
625 	dmae.dst_addr_hi = U64_HI(bnx2x_sp_mapping(bp, wb_data));
626 	dmae.len = len32;
627 
628 	/* issue the command and wait for completion */
629 	rc = bnx2x_issue_dmae_with_comp(bp, &dmae, bnx2x_sp(bp, wb_comp));
630 	if (rc) {
631 		BNX2X_ERR("DMAE returned failure %d\n", rc);
632 #ifdef BNX2X_STOP_ON_ERROR
633 		bnx2x_panic();
634 #endif
635 	}
636 }
637 
638 static void bnx2x_write_dmae_phys_len(struct bnx2x *bp, dma_addr_t phys_addr,
639 				      u32 addr, u32 len)
640 {
641 	int dmae_wr_max = DMAE_LEN32_WR_MAX(bp);
642 	int offset = 0;
643 
644 	while (len > dmae_wr_max) {
645 		bnx2x_write_dmae(bp, phys_addr + offset,
646 				 addr + offset, dmae_wr_max);
647 		offset += dmae_wr_max * 4;
648 		len -= dmae_wr_max;
649 	}
650 
651 	bnx2x_write_dmae(bp, phys_addr + offset, addr + offset, len);
652 }
653 
654 enum storms {
655 	   XSTORM,
656 	   TSTORM,
657 	   CSTORM,
658 	   USTORM,
659 	   MAX_STORMS
660 };
661 
662 #define STORMS_NUM 4
663 #define REGS_IN_ENTRY 4
664 
665 static inline int bnx2x_get_assert_list_entry(struct bnx2x *bp,
666 					      enum storms storm,
667 					      int entry)
668 {
669 	switch (storm) {
670 	case XSTORM:
671 		return XSTORM_ASSERT_LIST_OFFSET(entry);
672 	case TSTORM:
673 		return TSTORM_ASSERT_LIST_OFFSET(entry);
674 	case CSTORM:
675 		return CSTORM_ASSERT_LIST_OFFSET(entry);
676 	case USTORM:
677 		return USTORM_ASSERT_LIST_OFFSET(entry);
678 	case MAX_STORMS:
679 	default:
680 		BNX2X_ERR("unknown storm\n");
681 	}
682 	return -EINVAL;
683 }
684 
685 static int bnx2x_mc_assert(struct bnx2x *bp)
686 {
687 	char last_idx;
688 	int i, j, rc = 0;
689 	enum storms storm;
690 	u32 regs[REGS_IN_ENTRY];
691 	u32 bar_storm_intmem[STORMS_NUM] = {
692 		BAR_XSTRORM_INTMEM,
693 		BAR_TSTRORM_INTMEM,
694 		BAR_CSTRORM_INTMEM,
695 		BAR_USTRORM_INTMEM
696 	};
697 	u32 storm_assert_list_index[STORMS_NUM] = {
698 		XSTORM_ASSERT_LIST_INDEX_OFFSET,
699 		TSTORM_ASSERT_LIST_INDEX_OFFSET,
700 		CSTORM_ASSERT_LIST_INDEX_OFFSET,
701 		USTORM_ASSERT_LIST_INDEX_OFFSET
702 	};
703 	char *storms_string[STORMS_NUM] = {
704 		"XSTORM",
705 		"TSTORM",
706 		"CSTORM",
707 		"USTORM"
708 	};
709 
710 	for (storm = XSTORM; storm < MAX_STORMS; storm++) {
711 		last_idx = REG_RD8(bp, bar_storm_intmem[storm] +
712 				   storm_assert_list_index[storm]);
713 		if (last_idx)
714 			BNX2X_ERR("%s_ASSERT_LIST_INDEX 0x%x\n",
715 				  storms_string[storm], last_idx);
716 
717 		/* print the asserts */
718 		for (i = 0; i < STROM_ASSERT_ARRAY_SIZE; i++) {
719 			/* read a single assert entry */
720 			for (j = 0; j < REGS_IN_ENTRY; j++)
721 				regs[j] = REG_RD(bp, bar_storm_intmem[storm] +
722 					  bnx2x_get_assert_list_entry(bp,
723 								      storm,
724 								      i) +
725 					  sizeof(u32) * j);
726 
727 			/* log entry if it contains a valid assert */
728 			if (regs[0] != COMMON_ASM_INVALID_ASSERT_OPCODE) {
729 				BNX2X_ERR("%s_ASSERT_INDEX 0x%x = 0x%08x 0x%08x 0x%08x 0x%08x\n",
730 					  storms_string[storm], i, regs[3],
731 					  regs[2], regs[1], regs[0]);
732 				rc++;
733 			} else {
734 				break;
735 			}
736 		}
737 	}
738 
739 	BNX2X_ERR("Chip Revision: %s, FW Version: %d_%d_%d\n",
740 		  CHIP_IS_E1(bp) ? "everest1" :
741 		  CHIP_IS_E1H(bp) ? "everest1h" :
742 		  CHIP_IS_E2(bp) ? "everest2" : "everest3",
743 		  BCM_5710_FW_MAJOR_VERSION,
744 		  BCM_5710_FW_MINOR_VERSION,
745 		  BCM_5710_FW_REVISION_VERSION);
746 
747 	return rc;
748 }
749 
750 #define MCPR_TRACE_BUFFER_SIZE	(0x800)
751 #define SCRATCH_BUFFER_SIZE(bp)	\
752 	(CHIP_IS_E1(bp) ? 0x10000 : (CHIP_IS_E1H(bp) ? 0x20000 : 0x28000))
753 
754 void bnx2x_fw_dump_lvl(struct bnx2x *bp, const char *lvl)
755 {
756 	u32 addr, val;
757 	u32 mark, offset;
758 	__be32 data[9];
759 	int word;
760 	u32 trace_shmem_base;
761 	if (BP_NOMCP(bp)) {
762 		BNX2X_ERR("NO MCP - can not dump\n");
763 		return;
764 	}
765 	netdev_printk(lvl, bp->dev, "bc %d.%d.%d\n",
766 		(bp->common.bc_ver & 0xff0000) >> 16,
767 		(bp->common.bc_ver & 0xff00) >> 8,
768 		(bp->common.bc_ver & 0xff));
769 
770 	if (pci_channel_offline(bp->pdev)) {
771 		BNX2X_ERR("Cannot dump MCP info while in PCI error\n");
772 		return;
773 	}
774 
775 	val = REG_RD(bp, MCP_REG_MCPR_CPU_PROGRAM_COUNTER);
776 	if (val == REG_RD(bp, MCP_REG_MCPR_CPU_PROGRAM_COUNTER))
777 		BNX2X_ERR("%s" "MCP PC at 0x%x\n", lvl, val);
778 
779 	if (BP_PATH(bp) == 0)
780 		trace_shmem_base = bp->common.shmem_base;
781 	else
782 		trace_shmem_base = SHMEM2_RD(bp, other_shmem_base_addr);
783 
784 	/* sanity */
785 	if (trace_shmem_base < MCPR_SCRATCH_BASE(bp) + MCPR_TRACE_BUFFER_SIZE ||
786 	    trace_shmem_base >= MCPR_SCRATCH_BASE(bp) +
787 				SCRATCH_BUFFER_SIZE(bp)) {
788 		BNX2X_ERR("Unable to dump trace buffer (mark %x)\n",
789 			  trace_shmem_base);
790 		return;
791 	}
792 
793 	addr = trace_shmem_base - MCPR_TRACE_BUFFER_SIZE;
794 
795 	/* validate TRCB signature */
796 	mark = REG_RD(bp, addr);
797 	if (mark != MFW_TRACE_SIGNATURE) {
798 		BNX2X_ERR("Trace buffer signature is missing.");
799 		return ;
800 	}
801 
802 	/* read cyclic buffer pointer */
803 	addr += 4;
804 	mark = REG_RD(bp, addr);
805 	mark = MCPR_SCRATCH_BASE(bp) + ((mark + 0x3) & ~0x3) - 0x08000000;
806 	if (mark >= trace_shmem_base || mark < addr + 4) {
807 		BNX2X_ERR("Mark doesn't fall inside Trace Buffer\n");
808 		return;
809 	}
810 	printk("%s" "begin fw dump (mark 0x%x)\n", lvl, mark);
811 
812 	printk("%s", lvl);
813 
814 	/* dump buffer after the mark */
815 	for (offset = mark; offset < trace_shmem_base; offset += 0x8*4) {
816 		for (word = 0; word < 8; word++)
817 			data[word] = htonl(REG_RD(bp, offset + 4*word));
818 		data[8] = 0x0;
819 		pr_cont("%s", (char *)data);
820 	}
821 
822 	/* dump buffer before the mark */
823 	for (offset = addr + 4; offset <= mark; offset += 0x8*4) {
824 		for (word = 0; word < 8; word++)
825 			data[word] = htonl(REG_RD(bp, offset + 4*word));
826 		data[8] = 0x0;
827 		pr_cont("%s", (char *)data);
828 	}
829 	printk("%s" "end of fw dump\n", lvl);
830 }
831 
832 static void bnx2x_fw_dump(struct bnx2x *bp)
833 {
834 	bnx2x_fw_dump_lvl(bp, KERN_ERR);
835 }
836 
837 static void bnx2x_hc_int_disable(struct bnx2x *bp)
838 {
839 	int port = BP_PORT(bp);
840 	u32 addr = port ? HC_REG_CONFIG_1 : HC_REG_CONFIG_0;
841 	u32 val = REG_RD(bp, addr);
842 
843 	/* in E1 we must use only PCI configuration space to disable
844 	 * MSI/MSIX capability
845 	 * It's forbidden to disable IGU_PF_CONF_MSI_MSIX_EN in HC block
846 	 */
847 	if (CHIP_IS_E1(bp)) {
848 		/* Since IGU_PF_CONF_MSI_MSIX_EN still always on
849 		 * Use mask register to prevent from HC sending interrupts
850 		 * after we exit the function
851 		 */
852 		REG_WR(bp, HC_REG_INT_MASK + port*4, 0);
853 
854 		val &= ~(HC_CONFIG_0_REG_SINGLE_ISR_EN_0 |
855 			 HC_CONFIG_0_REG_INT_LINE_EN_0 |
856 			 HC_CONFIG_0_REG_ATTN_BIT_EN_0);
857 	} else
858 		val &= ~(HC_CONFIG_0_REG_SINGLE_ISR_EN_0 |
859 			 HC_CONFIG_0_REG_MSI_MSIX_INT_EN_0 |
860 			 HC_CONFIG_0_REG_INT_LINE_EN_0 |
861 			 HC_CONFIG_0_REG_ATTN_BIT_EN_0);
862 
863 	DP(NETIF_MSG_IFDOWN,
864 	   "write %x to HC %d (addr 0x%x)\n",
865 	   val, port, addr);
866 
867 	REG_WR(bp, addr, val);
868 	if (REG_RD(bp, addr) != val)
869 		BNX2X_ERR("BUG! Proper val not read from IGU!\n");
870 }
871 
872 static void bnx2x_igu_int_disable(struct bnx2x *bp)
873 {
874 	u32 val = REG_RD(bp, IGU_REG_PF_CONFIGURATION);
875 
876 	val &= ~(IGU_PF_CONF_MSI_MSIX_EN |
877 		 IGU_PF_CONF_INT_LINE_EN |
878 		 IGU_PF_CONF_ATTN_BIT_EN);
879 
880 	DP(NETIF_MSG_IFDOWN, "write %x to IGU\n", val);
881 
882 	REG_WR(bp, IGU_REG_PF_CONFIGURATION, val);
883 	if (REG_RD(bp, IGU_REG_PF_CONFIGURATION) != val)
884 		BNX2X_ERR("BUG! Proper val not read from IGU!\n");
885 }
886 
887 static void bnx2x_int_disable(struct bnx2x *bp)
888 {
889 	if (bp->common.int_block == INT_BLOCK_HC)
890 		bnx2x_hc_int_disable(bp);
891 	else
892 		bnx2x_igu_int_disable(bp);
893 }
894 
895 void bnx2x_panic_dump(struct bnx2x *bp, bool disable_int)
896 {
897 	int i;
898 	u16 j;
899 	struct hc_sp_status_block_data sp_sb_data;
900 	int func = BP_FUNC(bp);
901 #ifdef BNX2X_STOP_ON_ERROR
902 	u16 start = 0, end = 0;
903 	u8 cos;
904 #endif
905 	if (IS_PF(bp) && disable_int)
906 		bnx2x_int_disable(bp);
907 
908 	bp->stats_state = STATS_STATE_DISABLED;
909 	bp->eth_stats.unrecoverable_error++;
910 	DP(BNX2X_MSG_STATS, "stats_state - DISABLED\n");
911 
912 	BNX2X_ERR("begin crash dump -----------------\n");
913 
914 	/* Indices */
915 	/* Common */
916 	if (IS_PF(bp)) {
917 		struct host_sp_status_block *def_sb = bp->def_status_blk;
918 		int data_size, cstorm_offset;
919 
920 		BNX2X_ERR("def_idx(0x%x)  def_att_idx(0x%x)  attn_state(0x%x)  spq_prod_idx(0x%x) next_stats_cnt(0x%x)\n",
921 			  bp->def_idx, bp->def_att_idx, bp->attn_state,
922 			  bp->spq_prod_idx, bp->stats_counter);
923 		BNX2X_ERR("DSB: attn bits(0x%x)  ack(0x%x)  id(0x%x)  idx(0x%x)\n",
924 			  def_sb->atten_status_block.attn_bits,
925 			  def_sb->atten_status_block.attn_bits_ack,
926 			  def_sb->atten_status_block.status_block_id,
927 			  def_sb->atten_status_block.attn_bits_index);
928 		BNX2X_ERR("     def (");
929 		for (i = 0; i < HC_SP_SB_MAX_INDICES; i++)
930 			pr_cont("0x%x%s",
931 				def_sb->sp_sb.index_values[i],
932 				(i == HC_SP_SB_MAX_INDICES - 1) ? ")  " : " ");
933 
934 		data_size = sizeof(struct hc_sp_status_block_data) /
935 			    sizeof(u32);
936 		cstorm_offset = CSTORM_SP_STATUS_BLOCK_DATA_OFFSET(func);
937 		for (i = 0; i < data_size; i++)
938 			*((u32 *)&sp_sb_data + i) =
939 				REG_RD(bp, BAR_CSTRORM_INTMEM + cstorm_offset +
940 					   i * sizeof(u32));
941 
942 		pr_cont("igu_sb_id(0x%x)  igu_seg_id(0x%x) pf_id(0x%x)  vnic_id(0x%x)  vf_id(0x%x)  vf_valid (0x%x) state(0x%x)\n",
943 			sp_sb_data.igu_sb_id,
944 			sp_sb_data.igu_seg_id,
945 			sp_sb_data.p_func.pf_id,
946 			sp_sb_data.p_func.vnic_id,
947 			sp_sb_data.p_func.vf_id,
948 			sp_sb_data.p_func.vf_valid,
949 			sp_sb_data.state);
950 	}
951 
952 	for_each_eth_queue(bp, i) {
953 		struct bnx2x_fastpath *fp = &bp->fp[i];
954 		int loop;
955 		struct hc_status_block_data_e2 sb_data_e2;
956 		struct hc_status_block_data_e1x sb_data_e1x;
957 		struct hc_status_block_sm  *hc_sm_p =
958 			CHIP_IS_E1x(bp) ?
959 			sb_data_e1x.common.state_machine :
960 			sb_data_e2.common.state_machine;
961 		struct hc_index_data *hc_index_p =
962 			CHIP_IS_E1x(bp) ?
963 			sb_data_e1x.index_data :
964 			sb_data_e2.index_data;
965 		u8 data_size, cos;
966 		u32 *sb_data_p;
967 		struct bnx2x_fp_txdata txdata;
968 
969 		if (!bp->fp)
970 			break;
971 
972 		if (!fp->rx_cons_sb)
973 			continue;
974 
975 		/* Rx */
976 		BNX2X_ERR("fp%d: rx_bd_prod(0x%x)  rx_bd_cons(0x%x)  rx_comp_prod(0x%x)  rx_comp_cons(0x%x)  *rx_cons_sb(0x%x)\n",
977 			  i, fp->rx_bd_prod, fp->rx_bd_cons,
978 			  fp->rx_comp_prod,
979 			  fp->rx_comp_cons, le16_to_cpu(*fp->rx_cons_sb));
980 		BNX2X_ERR("     rx_sge_prod(0x%x)  last_max_sge(0x%x)  fp_hc_idx(0x%x)\n",
981 			  fp->rx_sge_prod, fp->last_max_sge,
982 			  le16_to_cpu(fp->fp_hc_idx));
983 
984 		/* Tx */
985 		for_each_cos_in_tx_queue(fp, cos)
986 		{
987 			if (!fp->txdata_ptr[cos])
988 				break;
989 
990 			txdata = *fp->txdata_ptr[cos];
991 
992 			if (!txdata.tx_cons_sb)
993 				continue;
994 
995 			BNX2X_ERR("fp%d: tx_pkt_prod(0x%x)  tx_pkt_cons(0x%x)  tx_bd_prod(0x%x)  tx_bd_cons(0x%x)  *tx_cons_sb(0x%x)\n",
996 				  i, txdata.tx_pkt_prod,
997 				  txdata.tx_pkt_cons, txdata.tx_bd_prod,
998 				  txdata.tx_bd_cons,
999 				  le16_to_cpu(*txdata.tx_cons_sb));
1000 		}
1001 
1002 		loop = CHIP_IS_E1x(bp) ?
1003 			HC_SB_MAX_INDICES_E1X : HC_SB_MAX_INDICES_E2;
1004 
1005 		/* host sb data */
1006 
1007 		if (IS_FCOE_FP(fp))
1008 			continue;
1009 
1010 		BNX2X_ERR("     run indexes (");
1011 		for (j = 0; j < HC_SB_MAX_SM; j++)
1012 			pr_cont("0x%x%s",
1013 			       fp->sb_running_index[j],
1014 			       (j == HC_SB_MAX_SM - 1) ? ")" : " ");
1015 
1016 		BNX2X_ERR("     indexes (");
1017 		for (j = 0; j < loop; j++)
1018 			pr_cont("0x%x%s",
1019 			       fp->sb_index_values[j],
1020 			       (j == loop - 1) ? ")" : " ");
1021 
1022 		/* VF cannot access FW refelection for status block */
1023 		if (IS_VF(bp))
1024 			continue;
1025 
1026 		/* fw sb data */
1027 		data_size = CHIP_IS_E1x(bp) ?
1028 			sizeof(struct hc_status_block_data_e1x) :
1029 			sizeof(struct hc_status_block_data_e2);
1030 		data_size /= sizeof(u32);
1031 		sb_data_p = CHIP_IS_E1x(bp) ?
1032 			(u32 *)&sb_data_e1x :
1033 			(u32 *)&sb_data_e2;
1034 		/* copy sb data in here */
1035 		for (j = 0; j < data_size; j++)
1036 			*(sb_data_p + j) = REG_RD(bp, BAR_CSTRORM_INTMEM +
1037 				CSTORM_STATUS_BLOCK_DATA_OFFSET(fp->fw_sb_id) +
1038 				j * sizeof(u32));
1039 
1040 		if (!CHIP_IS_E1x(bp)) {
1041 			pr_cont("pf_id(0x%x)  vf_id(0x%x)  vf_valid(0x%x) vnic_id(0x%x)  same_igu_sb_1b(0x%x) state(0x%x)\n",
1042 				sb_data_e2.common.p_func.pf_id,
1043 				sb_data_e2.common.p_func.vf_id,
1044 				sb_data_e2.common.p_func.vf_valid,
1045 				sb_data_e2.common.p_func.vnic_id,
1046 				sb_data_e2.common.same_igu_sb_1b,
1047 				sb_data_e2.common.state);
1048 		} else {
1049 			pr_cont("pf_id(0x%x)  vf_id(0x%x)  vf_valid(0x%x) vnic_id(0x%x)  same_igu_sb_1b(0x%x) state(0x%x)\n",
1050 				sb_data_e1x.common.p_func.pf_id,
1051 				sb_data_e1x.common.p_func.vf_id,
1052 				sb_data_e1x.common.p_func.vf_valid,
1053 				sb_data_e1x.common.p_func.vnic_id,
1054 				sb_data_e1x.common.same_igu_sb_1b,
1055 				sb_data_e1x.common.state);
1056 		}
1057 
1058 		/* SB_SMs data */
1059 		for (j = 0; j < HC_SB_MAX_SM; j++) {
1060 			pr_cont("SM[%d] __flags (0x%x) igu_sb_id (0x%x)  igu_seg_id(0x%x) time_to_expire (0x%x) timer_value(0x%x)\n",
1061 				j, hc_sm_p[j].__flags,
1062 				hc_sm_p[j].igu_sb_id,
1063 				hc_sm_p[j].igu_seg_id,
1064 				hc_sm_p[j].time_to_expire,
1065 				hc_sm_p[j].timer_value);
1066 		}
1067 
1068 		/* Indices data */
1069 		for (j = 0; j < loop; j++) {
1070 			pr_cont("INDEX[%d] flags (0x%x) timeout (0x%x)\n", j,
1071 			       hc_index_p[j].flags,
1072 			       hc_index_p[j].timeout);
1073 		}
1074 	}
1075 
1076 #ifdef BNX2X_STOP_ON_ERROR
1077 	if (IS_PF(bp)) {
1078 		/* event queue */
1079 		BNX2X_ERR("eq cons %x prod %x\n", bp->eq_cons, bp->eq_prod);
1080 		for (i = 0; i < NUM_EQ_DESC; i++) {
1081 			u32 *data = (u32 *)&bp->eq_ring[i].message.data;
1082 
1083 			BNX2X_ERR("event queue [%d]: header: opcode %d, error %d\n",
1084 				  i, bp->eq_ring[i].message.opcode,
1085 				  bp->eq_ring[i].message.error);
1086 			BNX2X_ERR("data: %x %x %x\n",
1087 				  data[0], data[1], data[2]);
1088 		}
1089 	}
1090 
1091 	/* Rings */
1092 	/* Rx */
1093 	for_each_valid_rx_queue(bp, i) {
1094 		struct bnx2x_fastpath *fp = &bp->fp[i];
1095 
1096 		if (!bp->fp)
1097 			break;
1098 
1099 		if (!fp->rx_cons_sb)
1100 			continue;
1101 
1102 		start = RX_BD(le16_to_cpu(*fp->rx_cons_sb) - 10);
1103 		end = RX_BD(le16_to_cpu(*fp->rx_cons_sb) + 503);
1104 		for (j = start; j != end; j = RX_BD(j + 1)) {
1105 			u32 *rx_bd = (u32 *)&fp->rx_desc_ring[j];
1106 			struct sw_rx_bd *sw_bd = &fp->rx_buf_ring[j];
1107 
1108 			BNX2X_ERR("fp%d: rx_bd[%x]=[%x:%x]  sw_bd=[%p]\n",
1109 				  i, j, rx_bd[1], rx_bd[0], sw_bd->data);
1110 		}
1111 
1112 		start = RX_SGE(fp->rx_sge_prod);
1113 		end = RX_SGE(fp->last_max_sge);
1114 		for (j = start; j != end; j = RX_SGE(j + 1)) {
1115 			u32 *rx_sge = (u32 *)&fp->rx_sge_ring[j];
1116 			struct sw_rx_page *sw_page = &fp->rx_page_ring[j];
1117 
1118 			BNX2X_ERR("fp%d: rx_sge[%x]=[%x:%x]  sw_page=[%p]\n",
1119 				  i, j, rx_sge[1], rx_sge[0], sw_page->page);
1120 		}
1121 
1122 		start = RCQ_BD(fp->rx_comp_cons - 10);
1123 		end = RCQ_BD(fp->rx_comp_cons + 503);
1124 		for (j = start; j != end; j = RCQ_BD(j + 1)) {
1125 			u32 *cqe = (u32 *)&fp->rx_comp_ring[j];
1126 
1127 			BNX2X_ERR("fp%d: cqe[%x]=[%x:%x:%x:%x]\n",
1128 				  i, j, cqe[0], cqe[1], cqe[2], cqe[3]);
1129 		}
1130 	}
1131 
1132 	/* Tx */
1133 	for_each_valid_tx_queue(bp, i) {
1134 		struct bnx2x_fastpath *fp = &bp->fp[i];
1135 
1136 		if (!bp->fp)
1137 			break;
1138 
1139 		for_each_cos_in_tx_queue(fp, cos) {
1140 			struct bnx2x_fp_txdata *txdata = fp->txdata_ptr[cos];
1141 
1142 			if (!fp->txdata_ptr[cos])
1143 				break;
1144 
1145 			if (!txdata->tx_cons_sb)
1146 				continue;
1147 
1148 			start = TX_BD(le16_to_cpu(*txdata->tx_cons_sb) - 10);
1149 			end = TX_BD(le16_to_cpu(*txdata->tx_cons_sb) + 245);
1150 			for (j = start; j != end; j = TX_BD(j + 1)) {
1151 				struct sw_tx_bd *sw_bd =
1152 					&txdata->tx_buf_ring[j];
1153 
1154 				BNX2X_ERR("fp%d: txdata %d, packet[%x]=[%p,%x]\n",
1155 					  i, cos, j, sw_bd->skb,
1156 					  sw_bd->first_bd);
1157 			}
1158 
1159 			start = TX_BD(txdata->tx_bd_cons - 10);
1160 			end = TX_BD(txdata->tx_bd_cons + 254);
1161 			for (j = start; j != end; j = TX_BD(j + 1)) {
1162 				u32 *tx_bd = (u32 *)&txdata->tx_desc_ring[j];
1163 
1164 				BNX2X_ERR("fp%d: txdata %d, tx_bd[%x]=[%x:%x:%x:%x]\n",
1165 					  i, cos, j, tx_bd[0], tx_bd[1],
1166 					  tx_bd[2], tx_bd[3]);
1167 			}
1168 		}
1169 	}
1170 #endif
1171 	if (IS_PF(bp)) {
1172 		bnx2x_fw_dump(bp);
1173 		bnx2x_mc_assert(bp);
1174 	}
1175 	BNX2X_ERR("end crash dump -----------------\n");
1176 }
1177 
1178 /*
1179  * FLR Support for E2
1180  *
1181  * bnx2x_pf_flr_clnup() is called during nic_load in the per function HW
1182  * initialization.
1183  */
1184 #define FLR_WAIT_USEC		10000	/* 10 milliseconds */
1185 #define FLR_WAIT_INTERVAL	50	/* usec */
1186 #define	FLR_POLL_CNT		(FLR_WAIT_USEC/FLR_WAIT_INTERVAL) /* 200 */
1187 
1188 struct pbf_pN_buf_regs {
1189 	int pN;
1190 	u32 init_crd;
1191 	u32 crd;
1192 	u32 crd_freed;
1193 };
1194 
1195 struct pbf_pN_cmd_regs {
1196 	int pN;
1197 	u32 lines_occup;
1198 	u32 lines_freed;
1199 };
1200 
1201 static void bnx2x_pbf_pN_buf_flushed(struct bnx2x *bp,
1202 				     struct pbf_pN_buf_regs *regs,
1203 				     u32 poll_count)
1204 {
1205 	u32 init_crd, crd, crd_start, crd_freed, crd_freed_start;
1206 	u32 cur_cnt = poll_count;
1207 
1208 	crd_freed = crd_freed_start = REG_RD(bp, regs->crd_freed);
1209 	crd = crd_start = REG_RD(bp, regs->crd);
1210 	init_crd = REG_RD(bp, regs->init_crd);
1211 
1212 	DP(BNX2X_MSG_SP, "INIT CREDIT[%d] : %x\n", regs->pN, init_crd);
1213 	DP(BNX2X_MSG_SP, "CREDIT[%d]      : s:%x\n", regs->pN, crd);
1214 	DP(BNX2X_MSG_SP, "CREDIT_FREED[%d]: s:%x\n", regs->pN, crd_freed);
1215 
1216 	while ((crd != init_crd) && ((u32)SUB_S32(crd_freed, crd_freed_start) <
1217 	       (init_crd - crd_start))) {
1218 		if (cur_cnt--) {
1219 			udelay(FLR_WAIT_INTERVAL);
1220 			crd = REG_RD(bp, regs->crd);
1221 			crd_freed = REG_RD(bp, regs->crd_freed);
1222 		} else {
1223 			DP(BNX2X_MSG_SP, "PBF tx buffer[%d] timed out\n",
1224 			   regs->pN);
1225 			DP(BNX2X_MSG_SP, "CREDIT[%d]      : c:%x\n",
1226 			   regs->pN, crd);
1227 			DP(BNX2X_MSG_SP, "CREDIT_FREED[%d]: c:%x\n",
1228 			   regs->pN, crd_freed);
1229 			break;
1230 		}
1231 	}
1232 	DP(BNX2X_MSG_SP, "Waited %d*%d usec for PBF tx buffer[%d]\n",
1233 	   poll_count-cur_cnt, FLR_WAIT_INTERVAL, regs->pN);
1234 }
1235 
1236 static void bnx2x_pbf_pN_cmd_flushed(struct bnx2x *bp,
1237 				     struct pbf_pN_cmd_regs *regs,
1238 				     u32 poll_count)
1239 {
1240 	u32 occup, to_free, freed, freed_start;
1241 	u32 cur_cnt = poll_count;
1242 
1243 	occup = to_free = REG_RD(bp, regs->lines_occup);
1244 	freed = freed_start = REG_RD(bp, regs->lines_freed);
1245 
1246 	DP(BNX2X_MSG_SP, "OCCUPANCY[%d]   : s:%x\n", regs->pN, occup);
1247 	DP(BNX2X_MSG_SP, "LINES_FREED[%d] : s:%x\n", regs->pN, freed);
1248 
1249 	while (occup && ((u32)SUB_S32(freed, freed_start) < to_free)) {
1250 		if (cur_cnt--) {
1251 			udelay(FLR_WAIT_INTERVAL);
1252 			occup = REG_RD(bp, regs->lines_occup);
1253 			freed = REG_RD(bp, regs->lines_freed);
1254 		} else {
1255 			DP(BNX2X_MSG_SP, "PBF cmd queue[%d] timed out\n",
1256 			   regs->pN);
1257 			DP(BNX2X_MSG_SP, "OCCUPANCY[%d]   : s:%x\n",
1258 			   regs->pN, occup);
1259 			DP(BNX2X_MSG_SP, "LINES_FREED[%d] : s:%x\n",
1260 			   regs->pN, freed);
1261 			break;
1262 		}
1263 	}
1264 	DP(BNX2X_MSG_SP, "Waited %d*%d usec for PBF cmd queue[%d]\n",
1265 	   poll_count-cur_cnt, FLR_WAIT_INTERVAL, regs->pN);
1266 }
1267 
1268 static u32 bnx2x_flr_clnup_reg_poll(struct bnx2x *bp, u32 reg,
1269 				    u32 expected, u32 poll_count)
1270 {
1271 	u32 cur_cnt = poll_count;
1272 	u32 val;
1273 
1274 	while ((val = REG_RD(bp, reg)) != expected && cur_cnt--)
1275 		udelay(FLR_WAIT_INTERVAL);
1276 
1277 	return val;
1278 }
1279 
1280 int bnx2x_flr_clnup_poll_hw_counter(struct bnx2x *bp, u32 reg,
1281 				    char *msg, u32 poll_cnt)
1282 {
1283 	u32 val = bnx2x_flr_clnup_reg_poll(bp, reg, 0, poll_cnt);
1284 	if (val != 0) {
1285 		BNX2X_ERR("%s usage count=%d\n", msg, val);
1286 		return 1;
1287 	}
1288 	return 0;
1289 }
1290 
1291 /* Common routines with VF FLR cleanup */
1292 u32 bnx2x_flr_clnup_poll_count(struct bnx2x *bp)
1293 {
1294 	/* adjust polling timeout */
1295 	if (CHIP_REV_IS_EMUL(bp))
1296 		return FLR_POLL_CNT * 2000;
1297 
1298 	if (CHIP_REV_IS_FPGA(bp))
1299 		return FLR_POLL_CNT * 120;
1300 
1301 	return FLR_POLL_CNT;
1302 }
1303 
1304 void bnx2x_tx_hw_flushed(struct bnx2x *bp, u32 poll_count)
1305 {
1306 	struct pbf_pN_cmd_regs cmd_regs[] = {
1307 		{0, (CHIP_IS_E3B0(bp)) ?
1308 			PBF_REG_TQ_OCCUPANCY_Q0 :
1309 			PBF_REG_P0_TQ_OCCUPANCY,
1310 		    (CHIP_IS_E3B0(bp)) ?
1311 			PBF_REG_TQ_LINES_FREED_CNT_Q0 :
1312 			PBF_REG_P0_TQ_LINES_FREED_CNT},
1313 		{1, (CHIP_IS_E3B0(bp)) ?
1314 			PBF_REG_TQ_OCCUPANCY_Q1 :
1315 			PBF_REG_P1_TQ_OCCUPANCY,
1316 		    (CHIP_IS_E3B0(bp)) ?
1317 			PBF_REG_TQ_LINES_FREED_CNT_Q1 :
1318 			PBF_REG_P1_TQ_LINES_FREED_CNT},
1319 		{4, (CHIP_IS_E3B0(bp)) ?
1320 			PBF_REG_TQ_OCCUPANCY_LB_Q :
1321 			PBF_REG_P4_TQ_OCCUPANCY,
1322 		    (CHIP_IS_E3B0(bp)) ?
1323 			PBF_REG_TQ_LINES_FREED_CNT_LB_Q :
1324 			PBF_REG_P4_TQ_LINES_FREED_CNT}
1325 	};
1326 
1327 	struct pbf_pN_buf_regs buf_regs[] = {
1328 		{0, (CHIP_IS_E3B0(bp)) ?
1329 			PBF_REG_INIT_CRD_Q0 :
1330 			PBF_REG_P0_INIT_CRD ,
1331 		    (CHIP_IS_E3B0(bp)) ?
1332 			PBF_REG_CREDIT_Q0 :
1333 			PBF_REG_P0_CREDIT,
1334 		    (CHIP_IS_E3B0(bp)) ?
1335 			PBF_REG_INTERNAL_CRD_FREED_CNT_Q0 :
1336 			PBF_REG_P0_INTERNAL_CRD_FREED_CNT},
1337 		{1, (CHIP_IS_E3B0(bp)) ?
1338 			PBF_REG_INIT_CRD_Q1 :
1339 			PBF_REG_P1_INIT_CRD,
1340 		    (CHIP_IS_E3B0(bp)) ?
1341 			PBF_REG_CREDIT_Q1 :
1342 			PBF_REG_P1_CREDIT,
1343 		    (CHIP_IS_E3B0(bp)) ?
1344 			PBF_REG_INTERNAL_CRD_FREED_CNT_Q1 :
1345 			PBF_REG_P1_INTERNAL_CRD_FREED_CNT},
1346 		{4, (CHIP_IS_E3B0(bp)) ?
1347 			PBF_REG_INIT_CRD_LB_Q :
1348 			PBF_REG_P4_INIT_CRD,
1349 		    (CHIP_IS_E3B0(bp)) ?
1350 			PBF_REG_CREDIT_LB_Q :
1351 			PBF_REG_P4_CREDIT,
1352 		    (CHIP_IS_E3B0(bp)) ?
1353 			PBF_REG_INTERNAL_CRD_FREED_CNT_LB_Q :
1354 			PBF_REG_P4_INTERNAL_CRD_FREED_CNT},
1355 	};
1356 
1357 	int i;
1358 
1359 	/* Verify the command queues are flushed P0, P1, P4 */
1360 	for (i = 0; i < ARRAY_SIZE(cmd_regs); i++)
1361 		bnx2x_pbf_pN_cmd_flushed(bp, &cmd_regs[i], poll_count);
1362 
1363 	/* Verify the transmission buffers are flushed P0, P1, P4 */
1364 	for (i = 0; i < ARRAY_SIZE(buf_regs); i++)
1365 		bnx2x_pbf_pN_buf_flushed(bp, &buf_regs[i], poll_count);
1366 }
1367 
1368 #define OP_GEN_PARAM(param) \
1369 	(((param) << SDM_OP_GEN_COMP_PARAM_SHIFT) & SDM_OP_GEN_COMP_PARAM)
1370 
1371 #define OP_GEN_TYPE(type) \
1372 	(((type) << SDM_OP_GEN_COMP_TYPE_SHIFT) & SDM_OP_GEN_COMP_TYPE)
1373 
1374 #define OP_GEN_AGG_VECT(index) \
1375 	(((index) << SDM_OP_GEN_AGG_VECT_IDX_SHIFT) & SDM_OP_GEN_AGG_VECT_IDX)
1376 
1377 int bnx2x_send_final_clnup(struct bnx2x *bp, u8 clnup_func, u32 poll_cnt)
1378 {
1379 	u32 op_gen_command = 0;
1380 	u32 comp_addr = BAR_CSTRORM_INTMEM +
1381 			CSTORM_FINAL_CLEANUP_COMPLETE_OFFSET(clnup_func);
1382 	int ret = 0;
1383 
1384 	if (REG_RD(bp, comp_addr)) {
1385 		BNX2X_ERR("Cleanup complete was not 0 before sending\n");
1386 		return 1;
1387 	}
1388 
1389 	op_gen_command |= OP_GEN_PARAM(XSTORM_AGG_INT_FINAL_CLEANUP_INDEX);
1390 	op_gen_command |= OP_GEN_TYPE(XSTORM_AGG_INT_FINAL_CLEANUP_COMP_TYPE);
1391 	op_gen_command |= OP_GEN_AGG_VECT(clnup_func);
1392 	op_gen_command |= 1 << SDM_OP_GEN_AGG_VECT_IDX_VALID_SHIFT;
1393 
1394 	DP(BNX2X_MSG_SP, "sending FW Final cleanup\n");
1395 	REG_WR(bp, XSDM_REG_OPERATION_GEN, op_gen_command);
1396 
1397 	if (bnx2x_flr_clnup_reg_poll(bp, comp_addr, 1, poll_cnt) != 1) {
1398 		BNX2X_ERR("FW final cleanup did not succeed\n");
1399 		DP(BNX2X_MSG_SP, "At timeout completion address contained %x\n",
1400 		   (REG_RD(bp, comp_addr)));
1401 		bnx2x_panic();
1402 		return 1;
1403 	}
1404 	/* Zero completion for next FLR */
1405 	REG_WR(bp, comp_addr, 0);
1406 
1407 	return ret;
1408 }
1409 
1410 u8 bnx2x_is_pcie_pending(struct pci_dev *dev)
1411 {
1412 	u16 status;
1413 
1414 	pcie_capability_read_word(dev, PCI_EXP_DEVSTA, &status);
1415 	return status & PCI_EXP_DEVSTA_TRPND;
1416 }
1417 
1418 /* PF FLR specific routines
1419 */
1420 static int bnx2x_poll_hw_usage_counters(struct bnx2x *bp, u32 poll_cnt)
1421 {
1422 	/* wait for CFC PF usage-counter to zero (includes all the VFs) */
1423 	if (bnx2x_flr_clnup_poll_hw_counter(bp,
1424 			CFC_REG_NUM_LCIDS_INSIDE_PF,
1425 			"CFC PF usage counter timed out",
1426 			poll_cnt))
1427 		return 1;
1428 
1429 	/* Wait for DQ PF usage-counter to zero (until DQ cleanup) */
1430 	if (bnx2x_flr_clnup_poll_hw_counter(bp,
1431 			DORQ_REG_PF_USAGE_CNT,
1432 			"DQ PF usage counter timed out",
1433 			poll_cnt))
1434 		return 1;
1435 
1436 	/* Wait for QM PF usage-counter to zero (until DQ cleanup) */
1437 	if (bnx2x_flr_clnup_poll_hw_counter(bp,
1438 			QM_REG_PF_USG_CNT_0 + 4*BP_FUNC(bp),
1439 			"QM PF usage counter timed out",
1440 			poll_cnt))
1441 		return 1;
1442 
1443 	/* Wait for Timer PF usage-counters to zero (until DQ cleanup) */
1444 	if (bnx2x_flr_clnup_poll_hw_counter(bp,
1445 			TM_REG_LIN0_VNIC_UC + 4*BP_PORT(bp),
1446 			"Timers VNIC usage counter timed out",
1447 			poll_cnt))
1448 		return 1;
1449 	if (bnx2x_flr_clnup_poll_hw_counter(bp,
1450 			TM_REG_LIN0_NUM_SCANS + 4*BP_PORT(bp),
1451 			"Timers NUM_SCANS usage counter timed out",
1452 			poll_cnt))
1453 		return 1;
1454 
1455 	/* Wait DMAE PF usage counter to zero */
1456 	if (bnx2x_flr_clnup_poll_hw_counter(bp,
1457 			dmae_reg_go_c[INIT_DMAE_C(bp)],
1458 			"DMAE command register timed out",
1459 			poll_cnt))
1460 		return 1;
1461 
1462 	return 0;
1463 }
1464 
1465 static void bnx2x_hw_enable_status(struct bnx2x *bp)
1466 {
1467 	u32 val;
1468 
1469 	val = REG_RD(bp, CFC_REG_WEAK_ENABLE_PF);
1470 	DP(BNX2X_MSG_SP, "CFC_REG_WEAK_ENABLE_PF is 0x%x\n", val);
1471 
1472 	val = REG_RD(bp, PBF_REG_DISABLE_PF);
1473 	DP(BNX2X_MSG_SP, "PBF_REG_DISABLE_PF is 0x%x\n", val);
1474 
1475 	val = REG_RD(bp, IGU_REG_PCI_PF_MSI_EN);
1476 	DP(BNX2X_MSG_SP, "IGU_REG_PCI_PF_MSI_EN is 0x%x\n", val);
1477 
1478 	val = REG_RD(bp, IGU_REG_PCI_PF_MSIX_EN);
1479 	DP(BNX2X_MSG_SP, "IGU_REG_PCI_PF_MSIX_EN is 0x%x\n", val);
1480 
1481 	val = REG_RD(bp, IGU_REG_PCI_PF_MSIX_FUNC_MASK);
1482 	DP(BNX2X_MSG_SP, "IGU_REG_PCI_PF_MSIX_FUNC_MASK is 0x%x\n", val);
1483 
1484 	val = REG_RD(bp, PGLUE_B_REG_SHADOW_BME_PF_7_0_CLR);
1485 	DP(BNX2X_MSG_SP, "PGLUE_B_REG_SHADOW_BME_PF_7_0_CLR is 0x%x\n", val);
1486 
1487 	val = REG_RD(bp, PGLUE_B_REG_FLR_REQUEST_PF_7_0_CLR);
1488 	DP(BNX2X_MSG_SP, "PGLUE_B_REG_FLR_REQUEST_PF_7_0_CLR is 0x%x\n", val);
1489 
1490 	val = REG_RD(bp, PGLUE_B_REG_INTERNAL_PFID_ENABLE_MASTER);
1491 	DP(BNX2X_MSG_SP, "PGLUE_B_REG_INTERNAL_PFID_ENABLE_MASTER is 0x%x\n",
1492 	   val);
1493 }
1494 
1495 static int bnx2x_pf_flr_clnup(struct bnx2x *bp)
1496 {
1497 	u32 poll_cnt = bnx2x_flr_clnup_poll_count(bp);
1498 
1499 	DP(BNX2X_MSG_SP, "Cleanup after FLR PF[%d]\n", BP_ABS_FUNC(bp));
1500 
1501 	/* Re-enable PF target read access */
1502 	REG_WR(bp, PGLUE_B_REG_INTERNAL_PFID_ENABLE_TARGET_READ, 1);
1503 
1504 	/* Poll HW usage counters */
1505 	DP(BNX2X_MSG_SP, "Polling usage counters\n");
1506 	if (bnx2x_poll_hw_usage_counters(bp, poll_cnt))
1507 		return -EBUSY;
1508 
1509 	/* Zero the igu 'trailing edge' and 'leading edge' */
1510 
1511 	/* Send the FW cleanup command */
1512 	if (bnx2x_send_final_clnup(bp, (u8)BP_FUNC(bp), poll_cnt))
1513 		return -EBUSY;
1514 
1515 	/* ATC cleanup */
1516 
1517 	/* Verify TX hw is flushed */
1518 	bnx2x_tx_hw_flushed(bp, poll_cnt);
1519 
1520 	/* Wait 100ms (not adjusted according to platform) */
1521 	msleep(100);
1522 
1523 	/* Verify no pending pci transactions */
1524 	if (bnx2x_is_pcie_pending(bp->pdev))
1525 		BNX2X_ERR("PCIE Transactions still pending\n");
1526 
1527 	/* Debug */
1528 	bnx2x_hw_enable_status(bp);
1529 
1530 	/*
1531 	 * Master enable - Due to WB DMAE writes performed before this
1532 	 * register is re-initialized as part of the regular function init
1533 	 */
1534 	REG_WR(bp, PGLUE_B_REG_INTERNAL_PFID_ENABLE_MASTER, 1);
1535 
1536 	return 0;
1537 }
1538 
1539 static void bnx2x_hc_int_enable(struct bnx2x *bp)
1540 {
1541 	int port = BP_PORT(bp);
1542 	u32 addr = port ? HC_REG_CONFIG_1 : HC_REG_CONFIG_0;
1543 	u32 val = REG_RD(bp, addr);
1544 	bool msix = (bp->flags & USING_MSIX_FLAG) ? true : false;
1545 	bool single_msix = (bp->flags & USING_SINGLE_MSIX_FLAG) ? true : false;
1546 	bool msi = (bp->flags & USING_MSI_FLAG) ? true : false;
1547 
1548 	if (msix) {
1549 		val &= ~(HC_CONFIG_0_REG_SINGLE_ISR_EN_0 |
1550 			 HC_CONFIG_0_REG_INT_LINE_EN_0);
1551 		val |= (HC_CONFIG_0_REG_MSI_MSIX_INT_EN_0 |
1552 			HC_CONFIG_0_REG_ATTN_BIT_EN_0);
1553 		if (single_msix)
1554 			val |= HC_CONFIG_0_REG_SINGLE_ISR_EN_0;
1555 	} else if (msi) {
1556 		val &= ~HC_CONFIG_0_REG_INT_LINE_EN_0;
1557 		val |= (HC_CONFIG_0_REG_SINGLE_ISR_EN_0 |
1558 			HC_CONFIG_0_REG_MSI_MSIX_INT_EN_0 |
1559 			HC_CONFIG_0_REG_ATTN_BIT_EN_0);
1560 	} else {
1561 		val |= (HC_CONFIG_0_REG_SINGLE_ISR_EN_0 |
1562 			HC_CONFIG_0_REG_MSI_MSIX_INT_EN_0 |
1563 			HC_CONFIG_0_REG_INT_LINE_EN_0 |
1564 			HC_CONFIG_0_REG_ATTN_BIT_EN_0);
1565 
1566 		if (!CHIP_IS_E1(bp)) {
1567 			DP(NETIF_MSG_IFUP,
1568 			   "write %x to HC %d (addr 0x%x)\n", val, port, addr);
1569 
1570 			REG_WR(bp, addr, val);
1571 
1572 			val &= ~HC_CONFIG_0_REG_MSI_MSIX_INT_EN_0;
1573 		}
1574 	}
1575 
1576 	if (CHIP_IS_E1(bp))
1577 		REG_WR(bp, HC_REG_INT_MASK + port*4, 0x1FFFF);
1578 
1579 	DP(NETIF_MSG_IFUP,
1580 	   "write %x to HC %d (addr 0x%x) mode %s\n", val, port, addr,
1581 	   (msix ? "MSI-X" : (msi ? "MSI" : "INTx")));
1582 
1583 	REG_WR(bp, addr, val);
1584 	/*
1585 	 * Ensure that HC_CONFIG is written before leading/trailing edge config
1586 	 */
1587 	barrier();
1588 
1589 	if (!CHIP_IS_E1(bp)) {
1590 		/* init leading/trailing edge */
1591 		if (IS_MF(bp)) {
1592 			val = (0xee0f | (1 << (BP_VN(bp) + 4)));
1593 			if (bp->port.pmf)
1594 				/* enable nig and gpio3 attention */
1595 				val |= 0x1100;
1596 		} else
1597 			val = 0xffff;
1598 
1599 		REG_WR(bp, HC_REG_TRAILING_EDGE_0 + port*8, val);
1600 		REG_WR(bp, HC_REG_LEADING_EDGE_0 + port*8, val);
1601 	}
1602 }
1603 
1604 static void bnx2x_igu_int_enable(struct bnx2x *bp)
1605 {
1606 	u32 val;
1607 	bool msix = (bp->flags & USING_MSIX_FLAG) ? true : false;
1608 	bool single_msix = (bp->flags & USING_SINGLE_MSIX_FLAG) ? true : false;
1609 	bool msi = (bp->flags & USING_MSI_FLAG) ? true : false;
1610 
1611 	val = REG_RD(bp, IGU_REG_PF_CONFIGURATION);
1612 
1613 	if (msix) {
1614 		val &= ~(IGU_PF_CONF_INT_LINE_EN |
1615 			 IGU_PF_CONF_SINGLE_ISR_EN);
1616 		val |= (IGU_PF_CONF_MSI_MSIX_EN |
1617 			IGU_PF_CONF_ATTN_BIT_EN);
1618 
1619 		if (single_msix)
1620 			val |= IGU_PF_CONF_SINGLE_ISR_EN;
1621 	} else if (msi) {
1622 		val &= ~IGU_PF_CONF_INT_LINE_EN;
1623 		val |= (IGU_PF_CONF_MSI_MSIX_EN |
1624 			IGU_PF_CONF_ATTN_BIT_EN |
1625 			IGU_PF_CONF_SINGLE_ISR_EN);
1626 	} else {
1627 		val &= ~IGU_PF_CONF_MSI_MSIX_EN;
1628 		val |= (IGU_PF_CONF_INT_LINE_EN |
1629 			IGU_PF_CONF_ATTN_BIT_EN |
1630 			IGU_PF_CONF_SINGLE_ISR_EN);
1631 	}
1632 
1633 	/* Clean previous status - need to configure igu prior to ack*/
1634 	if ((!msix) || single_msix) {
1635 		REG_WR(bp, IGU_REG_PF_CONFIGURATION, val);
1636 		bnx2x_ack_int(bp);
1637 	}
1638 
1639 	val |= IGU_PF_CONF_FUNC_EN;
1640 
1641 	DP(NETIF_MSG_IFUP, "write 0x%x to IGU  mode %s\n",
1642 	   val, (msix ? "MSI-X" : (msi ? "MSI" : "INTx")));
1643 
1644 	REG_WR(bp, IGU_REG_PF_CONFIGURATION, val);
1645 
1646 	if (val & IGU_PF_CONF_INT_LINE_EN)
1647 		pci_intx(bp->pdev, true);
1648 
1649 	barrier();
1650 
1651 	/* init leading/trailing edge */
1652 	if (IS_MF(bp)) {
1653 		val = (0xee0f | (1 << (BP_VN(bp) + 4)));
1654 		if (bp->port.pmf)
1655 			/* enable nig and gpio3 attention */
1656 			val |= 0x1100;
1657 	} else
1658 		val = 0xffff;
1659 
1660 	REG_WR(bp, IGU_REG_TRAILING_EDGE_LATCH, val);
1661 	REG_WR(bp, IGU_REG_LEADING_EDGE_LATCH, val);
1662 }
1663 
1664 void bnx2x_int_enable(struct bnx2x *bp)
1665 {
1666 	if (bp->common.int_block == INT_BLOCK_HC)
1667 		bnx2x_hc_int_enable(bp);
1668 	else
1669 		bnx2x_igu_int_enable(bp);
1670 }
1671 
1672 void bnx2x_int_disable_sync(struct bnx2x *bp, int disable_hw)
1673 {
1674 	int msix = (bp->flags & USING_MSIX_FLAG) ? 1 : 0;
1675 	int i, offset;
1676 
1677 	if (disable_hw)
1678 		/* prevent the HW from sending interrupts */
1679 		bnx2x_int_disable(bp);
1680 
1681 	/* make sure all ISRs are done */
1682 	if (msix) {
1683 		synchronize_irq(bp->msix_table[0].vector);
1684 		offset = 1;
1685 		if (CNIC_SUPPORT(bp))
1686 			offset++;
1687 		for_each_eth_queue(bp, i)
1688 			synchronize_irq(bp->msix_table[offset++].vector);
1689 	} else
1690 		synchronize_irq(bp->pdev->irq);
1691 
1692 	/* make sure sp_task is not running */
1693 	cancel_delayed_work(&bp->sp_task);
1694 	cancel_delayed_work(&bp->period_task);
1695 	flush_workqueue(bnx2x_wq);
1696 }
1697 
1698 /* fast path */
1699 
1700 /*
1701  * General service functions
1702  */
1703 
1704 /* Return true if succeeded to acquire the lock */
1705 static bool bnx2x_trylock_hw_lock(struct bnx2x *bp, u32 resource)
1706 {
1707 	u32 lock_status;
1708 	u32 resource_bit = (1 << resource);
1709 	int func = BP_FUNC(bp);
1710 	u32 hw_lock_control_reg;
1711 
1712 	DP(NETIF_MSG_HW | NETIF_MSG_IFUP,
1713 	   "Trying to take a lock on resource %d\n", resource);
1714 
1715 	/* Validating that the resource is within range */
1716 	if (resource > HW_LOCK_MAX_RESOURCE_VALUE) {
1717 		DP(NETIF_MSG_HW | NETIF_MSG_IFUP,
1718 		   "resource(0x%x) > HW_LOCK_MAX_RESOURCE_VALUE(0x%x)\n",
1719 		   resource, HW_LOCK_MAX_RESOURCE_VALUE);
1720 		return false;
1721 	}
1722 
1723 	if (func <= 5)
1724 		hw_lock_control_reg = (MISC_REG_DRIVER_CONTROL_1 + func*8);
1725 	else
1726 		hw_lock_control_reg =
1727 				(MISC_REG_DRIVER_CONTROL_7 + (func - 6)*8);
1728 
1729 	/* Try to acquire the lock */
1730 	REG_WR(bp, hw_lock_control_reg + 4, resource_bit);
1731 	lock_status = REG_RD(bp, hw_lock_control_reg);
1732 	if (lock_status & resource_bit)
1733 		return true;
1734 
1735 	DP(NETIF_MSG_HW | NETIF_MSG_IFUP,
1736 	   "Failed to get a lock on resource %d\n", resource);
1737 	return false;
1738 }
1739 
1740 /**
1741  * bnx2x_get_leader_lock_resource - get the recovery leader resource id
1742  *
1743  * @bp:	driver handle
1744  *
1745  * Returns the recovery leader resource id according to the engine this function
1746  * belongs to. Currently only only 2 engines is supported.
1747  */
1748 static int bnx2x_get_leader_lock_resource(struct bnx2x *bp)
1749 {
1750 	if (BP_PATH(bp))
1751 		return HW_LOCK_RESOURCE_RECOVERY_LEADER_1;
1752 	else
1753 		return HW_LOCK_RESOURCE_RECOVERY_LEADER_0;
1754 }
1755 
1756 /**
1757  * bnx2x_trylock_leader_lock- try to acquire a leader lock.
1758  *
1759  * @bp: driver handle
1760  *
1761  * Tries to acquire a leader lock for current engine.
1762  */
1763 static bool bnx2x_trylock_leader_lock(struct bnx2x *bp)
1764 {
1765 	return bnx2x_trylock_hw_lock(bp, bnx2x_get_leader_lock_resource(bp));
1766 }
1767 
1768 static void bnx2x_cnic_cfc_comp(struct bnx2x *bp, int cid, u8 err);
1769 
1770 /* schedule the sp task and mark that interrupt occurred (runs from ISR) */
1771 static int bnx2x_schedule_sp_task(struct bnx2x *bp)
1772 {
1773 	/* Set the interrupt occurred bit for the sp-task to recognize it
1774 	 * must ack the interrupt and transition according to the IGU
1775 	 * state machine.
1776 	 */
1777 	atomic_set(&bp->interrupt_occurred, 1);
1778 
1779 	/* The sp_task must execute only after this bit
1780 	 * is set, otherwise we will get out of sync and miss all
1781 	 * further interrupts. Hence, the barrier.
1782 	 */
1783 	smp_wmb();
1784 
1785 	/* schedule sp_task to workqueue */
1786 	return queue_delayed_work(bnx2x_wq, &bp->sp_task, 0);
1787 }
1788 
1789 void bnx2x_sp_event(struct bnx2x_fastpath *fp, union eth_rx_cqe *rr_cqe)
1790 {
1791 	struct bnx2x *bp = fp->bp;
1792 	int cid = SW_CID(rr_cqe->ramrod_cqe.conn_and_cmd_data);
1793 	int command = CQE_CMD(rr_cqe->ramrod_cqe.conn_and_cmd_data);
1794 	enum bnx2x_queue_cmd drv_cmd = BNX2X_Q_CMD_MAX;
1795 	struct bnx2x_queue_sp_obj *q_obj = &bnx2x_sp_obj(bp, fp).q_obj;
1796 
1797 	DP(BNX2X_MSG_SP,
1798 	   "fp %d  cid %d  got ramrod #%d  state is %x  type is %d\n",
1799 	   fp->index, cid, command, bp->state,
1800 	   rr_cqe->ramrod_cqe.ramrod_type);
1801 
1802 	/* If cid is within VF range, replace the slowpath object with the
1803 	 * one corresponding to this VF
1804 	 */
1805 	if (cid >= BNX2X_FIRST_VF_CID  &&
1806 	    cid < BNX2X_FIRST_VF_CID + BNX2X_VF_CIDS)
1807 		bnx2x_iov_set_queue_sp_obj(bp, cid, &q_obj);
1808 
1809 	switch (command) {
1810 	case (RAMROD_CMD_ID_ETH_CLIENT_UPDATE):
1811 		DP(BNX2X_MSG_SP, "got UPDATE ramrod. CID %d\n", cid);
1812 		drv_cmd = BNX2X_Q_CMD_UPDATE;
1813 		break;
1814 
1815 	case (RAMROD_CMD_ID_ETH_CLIENT_SETUP):
1816 		DP(BNX2X_MSG_SP, "got MULTI[%d] setup ramrod\n", cid);
1817 		drv_cmd = BNX2X_Q_CMD_SETUP;
1818 		break;
1819 
1820 	case (RAMROD_CMD_ID_ETH_TX_QUEUE_SETUP):
1821 		DP(BNX2X_MSG_SP, "got MULTI[%d] tx-only setup ramrod\n", cid);
1822 		drv_cmd = BNX2X_Q_CMD_SETUP_TX_ONLY;
1823 		break;
1824 
1825 	case (RAMROD_CMD_ID_ETH_HALT):
1826 		DP(BNX2X_MSG_SP, "got MULTI[%d] halt ramrod\n", cid);
1827 		drv_cmd = BNX2X_Q_CMD_HALT;
1828 		break;
1829 
1830 	case (RAMROD_CMD_ID_ETH_TERMINATE):
1831 		DP(BNX2X_MSG_SP, "got MULTI[%d] terminate ramrod\n", cid);
1832 		drv_cmd = BNX2X_Q_CMD_TERMINATE;
1833 		break;
1834 
1835 	case (RAMROD_CMD_ID_ETH_EMPTY):
1836 		DP(BNX2X_MSG_SP, "got MULTI[%d] empty ramrod\n", cid);
1837 		drv_cmd = BNX2X_Q_CMD_EMPTY;
1838 		break;
1839 
1840 	case (RAMROD_CMD_ID_ETH_TPA_UPDATE):
1841 		DP(BNX2X_MSG_SP, "got tpa update ramrod CID=%d\n", cid);
1842 		drv_cmd = BNX2X_Q_CMD_UPDATE_TPA;
1843 		break;
1844 
1845 	default:
1846 		BNX2X_ERR("unexpected MC reply (%d) on fp[%d]\n",
1847 			  command, fp->index);
1848 		return;
1849 	}
1850 
1851 	if ((drv_cmd != BNX2X_Q_CMD_MAX) &&
1852 	    q_obj->complete_cmd(bp, q_obj, drv_cmd))
1853 		/* q_obj->complete_cmd() failure means that this was
1854 		 * an unexpected completion.
1855 		 *
1856 		 * In this case we don't want to increase the bp->spq_left
1857 		 * because apparently we haven't sent this command the first
1858 		 * place.
1859 		 */
1860 #ifdef BNX2X_STOP_ON_ERROR
1861 		bnx2x_panic();
1862 #else
1863 		return;
1864 #endif
1865 
1866 	smp_mb__before_atomic();
1867 	atomic_inc(&bp->cq_spq_left);
1868 	/* push the change in bp->spq_left and towards the memory */
1869 	smp_mb__after_atomic();
1870 
1871 	DP(BNX2X_MSG_SP, "bp->cq_spq_left %x\n", atomic_read(&bp->cq_spq_left));
1872 
1873 	if ((drv_cmd == BNX2X_Q_CMD_UPDATE) && (IS_FCOE_FP(fp)) &&
1874 	    (!!test_bit(BNX2X_AFEX_FCOE_Q_UPDATE_PENDING, &bp->sp_state))) {
1875 		/* if Q update ramrod is completed for last Q in AFEX vif set
1876 		 * flow, then ACK MCP at the end
1877 		 *
1878 		 * mark pending ACK to MCP bit.
1879 		 * prevent case that both bits are cleared.
1880 		 * At the end of load/unload driver checks that
1881 		 * sp_state is cleared, and this order prevents
1882 		 * races
1883 		 */
1884 		smp_mb__before_atomic();
1885 		set_bit(BNX2X_AFEX_PENDING_VIFSET_MCP_ACK, &bp->sp_state);
1886 		wmb();
1887 		clear_bit(BNX2X_AFEX_FCOE_Q_UPDATE_PENDING, &bp->sp_state);
1888 		smp_mb__after_atomic();
1889 
1890 		/* schedule the sp task as mcp ack is required */
1891 		bnx2x_schedule_sp_task(bp);
1892 	}
1893 
1894 	return;
1895 }
1896 
1897 irqreturn_t bnx2x_interrupt(int irq, void *dev_instance)
1898 {
1899 	struct bnx2x *bp = netdev_priv(dev_instance);
1900 	u16 status = bnx2x_ack_int(bp);
1901 	u16 mask;
1902 	int i;
1903 	u8 cos;
1904 
1905 	/* Return here if interrupt is shared and it's not for us */
1906 	if (unlikely(status == 0)) {
1907 		DP(NETIF_MSG_INTR, "not our interrupt!\n");
1908 		return IRQ_NONE;
1909 	}
1910 	DP(NETIF_MSG_INTR, "got an interrupt  status 0x%x\n", status);
1911 
1912 #ifdef BNX2X_STOP_ON_ERROR
1913 	if (unlikely(bp->panic))
1914 		return IRQ_HANDLED;
1915 #endif
1916 
1917 	for_each_eth_queue(bp, i) {
1918 		struct bnx2x_fastpath *fp = &bp->fp[i];
1919 
1920 		mask = 0x2 << (fp->index + CNIC_SUPPORT(bp));
1921 		if (status & mask) {
1922 			/* Handle Rx or Tx according to SB id */
1923 			for_each_cos_in_tx_queue(fp, cos)
1924 				prefetch(fp->txdata_ptr[cos]->tx_cons_sb);
1925 			prefetch(&fp->sb_running_index[SM_RX_ID]);
1926 			napi_schedule_irqoff(&bnx2x_fp(bp, fp->index, napi));
1927 			status &= ~mask;
1928 		}
1929 	}
1930 
1931 	if (CNIC_SUPPORT(bp)) {
1932 		mask = 0x2;
1933 		if (status & (mask | 0x1)) {
1934 			struct cnic_ops *c_ops = NULL;
1935 
1936 			rcu_read_lock();
1937 			c_ops = rcu_dereference(bp->cnic_ops);
1938 			if (c_ops && (bp->cnic_eth_dev.drv_state &
1939 				      CNIC_DRV_STATE_HANDLES_IRQ))
1940 				c_ops->cnic_handler(bp->cnic_data, NULL);
1941 			rcu_read_unlock();
1942 
1943 			status &= ~mask;
1944 		}
1945 	}
1946 
1947 	if (unlikely(status & 0x1)) {
1948 
1949 		/* schedule sp task to perform default status block work, ack
1950 		 * attentions and enable interrupts.
1951 		 */
1952 		bnx2x_schedule_sp_task(bp);
1953 
1954 		status &= ~0x1;
1955 		if (!status)
1956 			return IRQ_HANDLED;
1957 	}
1958 
1959 	if (unlikely(status))
1960 		DP(NETIF_MSG_INTR, "got an unknown interrupt! (status 0x%x)\n",
1961 		   status);
1962 
1963 	return IRQ_HANDLED;
1964 }
1965 
1966 /* Link */
1967 
1968 /*
1969  * General service functions
1970  */
1971 
1972 int bnx2x_acquire_hw_lock(struct bnx2x *bp, u32 resource)
1973 {
1974 	u32 lock_status;
1975 	u32 resource_bit = (1 << resource);
1976 	int func = BP_FUNC(bp);
1977 	u32 hw_lock_control_reg;
1978 	int cnt;
1979 
1980 	/* Validating that the resource is within range */
1981 	if (resource > HW_LOCK_MAX_RESOURCE_VALUE) {
1982 		BNX2X_ERR("resource(0x%x) > HW_LOCK_MAX_RESOURCE_VALUE(0x%x)\n",
1983 		   resource, HW_LOCK_MAX_RESOURCE_VALUE);
1984 		return -EINVAL;
1985 	}
1986 
1987 	if (func <= 5) {
1988 		hw_lock_control_reg = (MISC_REG_DRIVER_CONTROL_1 + func*8);
1989 	} else {
1990 		hw_lock_control_reg =
1991 				(MISC_REG_DRIVER_CONTROL_7 + (func - 6)*8);
1992 	}
1993 
1994 	/* Validating that the resource is not already taken */
1995 	lock_status = REG_RD(bp, hw_lock_control_reg);
1996 	if (lock_status & resource_bit) {
1997 		BNX2X_ERR("lock_status 0x%x  resource_bit 0x%x\n",
1998 		   lock_status, resource_bit);
1999 		return -EEXIST;
2000 	}
2001 
2002 	/* Try for 5 second every 5ms */
2003 	for (cnt = 0; cnt < 1000; cnt++) {
2004 		/* Try to acquire the lock */
2005 		REG_WR(bp, hw_lock_control_reg + 4, resource_bit);
2006 		lock_status = REG_RD(bp, hw_lock_control_reg);
2007 		if (lock_status & resource_bit)
2008 			return 0;
2009 
2010 		usleep_range(5000, 10000);
2011 	}
2012 	BNX2X_ERR("Timeout\n");
2013 	return -EAGAIN;
2014 }
2015 
2016 int bnx2x_release_leader_lock(struct bnx2x *bp)
2017 {
2018 	return bnx2x_release_hw_lock(bp, bnx2x_get_leader_lock_resource(bp));
2019 }
2020 
2021 int bnx2x_release_hw_lock(struct bnx2x *bp, u32 resource)
2022 {
2023 	u32 lock_status;
2024 	u32 resource_bit = (1 << resource);
2025 	int func = BP_FUNC(bp);
2026 	u32 hw_lock_control_reg;
2027 
2028 	/* Validating that the resource is within range */
2029 	if (resource > HW_LOCK_MAX_RESOURCE_VALUE) {
2030 		BNX2X_ERR("resource(0x%x) > HW_LOCK_MAX_RESOURCE_VALUE(0x%x)\n",
2031 		   resource, HW_LOCK_MAX_RESOURCE_VALUE);
2032 		return -EINVAL;
2033 	}
2034 
2035 	if (func <= 5) {
2036 		hw_lock_control_reg = (MISC_REG_DRIVER_CONTROL_1 + func*8);
2037 	} else {
2038 		hw_lock_control_reg =
2039 				(MISC_REG_DRIVER_CONTROL_7 + (func - 6)*8);
2040 	}
2041 
2042 	/* Validating that the resource is currently taken */
2043 	lock_status = REG_RD(bp, hw_lock_control_reg);
2044 	if (!(lock_status & resource_bit)) {
2045 		BNX2X_ERR("lock_status 0x%x resource_bit 0x%x. Unlock was called but lock wasn't taken!\n",
2046 			  lock_status, resource_bit);
2047 		return -EFAULT;
2048 	}
2049 
2050 	REG_WR(bp, hw_lock_control_reg, resource_bit);
2051 	return 0;
2052 }
2053 
2054 int bnx2x_get_gpio(struct bnx2x *bp, int gpio_num, u8 port)
2055 {
2056 	/* The GPIO should be swapped if swap register is set and active */
2057 	int gpio_port = (REG_RD(bp, NIG_REG_PORT_SWAP) &&
2058 			 REG_RD(bp, NIG_REG_STRAP_OVERRIDE)) ^ port;
2059 	int gpio_shift = gpio_num +
2060 			(gpio_port ? MISC_REGISTERS_GPIO_PORT_SHIFT : 0);
2061 	u32 gpio_mask = (1 << gpio_shift);
2062 	u32 gpio_reg;
2063 	int value;
2064 
2065 	if (gpio_num > MISC_REGISTERS_GPIO_3) {
2066 		BNX2X_ERR("Invalid GPIO %d\n", gpio_num);
2067 		return -EINVAL;
2068 	}
2069 
2070 	/* read GPIO value */
2071 	gpio_reg = REG_RD(bp, MISC_REG_GPIO);
2072 
2073 	/* get the requested pin value */
2074 	if ((gpio_reg & gpio_mask) == gpio_mask)
2075 		value = 1;
2076 	else
2077 		value = 0;
2078 
2079 	return value;
2080 }
2081 
2082 int bnx2x_set_gpio(struct bnx2x *bp, int gpio_num, u32 mode, u8 port)
2083 {
2084 	/* The GPIO should be swapped if swap register is set and active */
2085 	int gpio_port = (REG_RD(bp, NIG_REG_PORT_SWAP) &&
2086 			 REG_RD(bp, NIG_REG_STRAP_OVERRIDE)) ^ port;
2087 	int gpio_shift = gpio_num +
2088 			(gpio_port ? MISC_REGISTERS_GPIO_PORT_SHIFT : 0);
2089 	u32 gpio_mask = (1 << gpio_shift);
2090 	u32 gpio_reg;
2091 
2092 	if (gpio_num > MISC_REGISTERS_GPIO_3) {
2093 		BNX2X_ERR("Invalid GPIO %d\n", gpio_num);
2094 		return -EINVAL;
2095 	}
2096 
2097 	bnx2x_acquire_hw_lock(bp, HW_LOCK_RESOURCE_GPIO);
2098 	/* read GPIO and mask except the float bits */
2099 	gpio_reg = (REG_RD(bp, MISC_REG_GPIO) & MISC_REGISTERS_GPIO_FLOAT);
2100 
2101 	switch (mode) {
2102 	case MISC_REGISTERS_GPIO_OUTPUT_LOW:
2103 		DP(NETIF_MSG_LINK,
2104 		   "Set GPIO %d (shift %d) -> output low\n",
2105 		   gpio_num, gpio_shift);
2106 		/* clear FLOAT and set CLR */
2107 		gpio_reg &= ~(gpio_mask << MISC_REGISTERS_GPIO_FLOAT_POS);
2108 		gpio_reg |=  (gpio_mask << MISC_REGISTERS_GPIO_CLR_POS);
2109 		break;
2110 
2111 	case MISC_REGISTERS_GPIO_OUTPUT_HIGH:
2112 		DP(NETIF_MSG_LINK,
2113 		   "Set GPIO %d (shift %d) -> output high\n",
2114 		   gpio_num, gpio_shift);
2115 		/* clear FLOAT and set SET */
2116 		gpio_reg &= ~(gpio_mask << MISC_REGISTERS_GPIO_FLOAT_POS);
2117 		gpio_reg |=  (gpio_mask << MISC_REGISTERS_GPIO_SET_POS);
2118 		break;
2119 
2120 	case MISC_REGISTERS_GPIO_INPUT_HI_Z:
2121 		DP(NETIF_MSG_LINK,
2122 		   "Set GPIO %d (shift %d) -> input\n",
2123 		   gpio_num, gpio_shift);
2124 		/* set FLOAT */
2125 		gpio_reg |= (gpio_mask << MISC_REGISTERS_GPIO_FLOAT_POS);
2126 		break;
2127 
2128 	default:
2129 		break;
2130 	}
2131 
2132 	REG_WR(bp, MISC_REG_GPIO, gpio_reg);
2133 	bnx2x_release_hw_lock(bp, HW_LOCK_RESOURCE_GPIO);
2134 
2135 	return 0;
2136 }
2137 
2138 int bnx2x_set_mult_gpio(struct bnx2x *bp, u8 pins, u32 mode)
2139 {
2140 	u32 gpio_reg = 0;
2141 	int rc = 0;
2142 
2143 	/* Any port swapping should be handled by caller. */
2144 
2145 	bnx2x_acquire_hw_lock(bp, HW_LOCK_RESOURCE_GPIO);
2146 	/* read GPIO and mask except the float bits */
2147 	gpio_reg = REG_RD(bp, MISC_REG_GPIO);
2148 	gpio_reg &= ~(pins << MISC_REGISTERS_GPIO_FLOAT_POS);
2149 	gpio_reg &= ~(pins << MISC_REGISTERS_GPIO_CLR_POS);
2150 	gpio_reg &= ~(pins << MISC_REGISTERS_GPIO_SET_POS);
2151 
2152 	switch (mode) {
2153 	case MISC_REGISTERS_GPIO_OUTPUT_LOW:
2154 		DP(NETIF_MSG_LINK, "Set GPIO 0x%x -> output low\n", pins);
2155 		/* set CLR */
2156 		gpio_reg |= (pins << MISC_REGISTERS_GPIO_CLR_POS);
2157 		break;
2158 
2159 	case MISC_REGISTERS_GPIO_OUTPUT_HIGH:
2160 		DP(NETIF_MSG_LINK, "Set GPIO 0x%x -> output high\n", pins);
2161 		/* set SET */
2162 		gpio_reg |= (pins << MISC_REGISTERS_GPIO_SET_POS);
2163 		break;
2164 
2165 	case MISC_REGISTERS_GPIO_INPUT_HI_Z:
2166 		DP(NETIF_MSG_LINK, "Set GPIO 0x%x -> input\n", pins);
2167 		/* set FLOAT */
2168 		gpio_reg |= (pins << MISC_REGISTERS_GPIO_FLOAT_POS);
2169 		break;
2170 
2171 	default:
2172 		BNX2X_ERR("Invalid GPIO mode assignment %d\n", mode);
2173 		rc = -EINVAL;
2174 		break;
2175 	}
2176 
2177 	if (rc == 0)
2178 		REG_WR(bp, MISC_REG_GPIO, gpio_reg);
2179 
2180 	bnx2x_release_hw_lock(bp, HW_LOCK_RESOURCE_GPIO);
2181 
2182 	return rc;
2183 }
2184 
2185 int bnx2x_set_gpio_int(struct bnx2x *bp, int gpio_num, u32 mode, u8 port)
2186 {
2187 	/* The GPIO should be swapped if swap register is set and active */
2188 	int gpio_port = (REG_RD(bp, NIG_REG_PORT_SWAP) &&
2189 			 REG_RD(bp, NIG_REG_STRAP_OVERRIDE)) ^ port;
2190 	int gpio_shift = gpio_num +
2191 			(gpio_port ? MISC_REGISTERS_GPIO_PORT_SHIFT : 0);
2192 	u32 gpio_mask = (1 << gpio_shift);
2193 	u32 gpio_reg;
2194 
2195 	if (gpio_num > MISC_REGISTERS_GPIO_3) {
2196 		BNX2X_ERR("Invalid GPIO %d\n", gpio_num);
2197 		return -EINVAL;
2198 	}
2199 
2200 	bnx2x_acquire_hw_lock(bp, HW_LOCK_RESOURCE_GPIO);
2201 	/* read GPIO int */
2202 	gpio_reg = REG_RD(bp, MISC_REG_GPIO_INT);
2203 
2204 	switch (mode) {
2205 	case MISC_REGISTERS_GPIO_INT_OUTPUT_CLR:
2206 		DP(NETIF_MSG_LINK,
2207 		   "Clear GPIO INT %d (shift %d) -> output low\n",
2208 		   gpio_num, gpio_shift);
2209 		/* clear SET and set CLR */
2210 		gpio_reg &= ~(gpio_mask << MISC_REGISTERS_GPIO_INT_SET_POS);
2211 		gpio_reg |=  (gpio_mask << MISC_REGISTERS_GPIO_INT_CLR_POS);
2212 		break;
2213 
2214 	case MISC_REGISTERS_GPIO_INT_OUTPUT_SET:
2215 		DP(NETIF_MSG_LINK,
2216 		   "Set GPIO INT %d (shift %d) -> output high\n",
2217 		   gpio_num, gpio_shift);
2218 		/* clear CLR and set SET */
2219 		gpio_reg &= ~(gpio_mask << MISC_REGISTERS_GPIO_INT_CLR_POS);
2220 		gpio_reg |=  (gpio_mask << MISC_REGISTERS_GPIO_INT_SET_POS);
2221 		break;
2222 
2223 	default:
2224 		break;
2225 	}
2226 
2227 	REG_WR(bp, MISC_REG_GPIO_INT, gpio_reg);
2228 	bnx2x_release_hw_lock(bp, HW_LOCK_RESOURCE_GPIO);
2229 
2230 	return 0;
2231 }
2232 
2233 static int bnx2x_set_spio(struct bnx2x *bp, int spio, u32 mode)
2234 {
2235 	u32 spio_reg;
2236 
2237 	/* Only 2 SPIOs are configurable */
2238 	if ((spio != MISC_SPIO_SPIO4) && (spio != MISC_SPIO_SPIO5)) {
2239 		BNX2X_ERR("Invalid SPIO 0x%x\n", spio);
2240 		return -EINVAL;
2241 	}
2242 
2243 	bnx2x_acquire_hw_lock(bp, HW_LOCK_RESOURCE_SPIO);
2244 	/* read SPIO and mask except the float bits */
2245 	spio_reg = (REG_RD(bp, MISC_REG_SPIO) & MISC_SPIO_FLOAT);
2246 
2247 	switch (mode) {
2248 	case MISC_SPIO_OUTPUT_LOW:
2249 		DP(NETIF_MSG_HW, "Set SPIO 0x%x -> output low\n", spio);
2250 		/* clear FLOAT and set CLR */
2251 		spio_reg &= ~(spio << MISC_SPIO_FLOAT_POS);
2252 		spio_reg |=  (spio << MISC_SPIO_CLR_POS);
2253 		break;
2254 
2255 	case MISC_SPIO_OUTPUT_HIGH:
2256 		DP(NETIF_MSG_HW, "Set SPIO 0x%x -> output high\n", spio);
2257 		/* clear FLOAT and set SET */
2258 		spio_reg &= ~(spio << MISC_SPIO_FLOAT_POS);
2259 		spio_reg |=  (spio << MISC_SPIO_SET_POS);
2260 		break;
2261 
2262 	case MISC_SPIO_INPUT_HI_Z:
2263 		DP(NETIF_MSG_HW, "Set SPIO 0x%x -> input\n", spio);
2264 		/* set FLOAT */
2265 		spio_reg |= (spio << MISC_SPIO_FLOAT_POS);
2266 		break;
2267 
2268 	default:
2269 		break;
2270 	}
2271 
2272 	REG_WR(bp, MISC_REG_SPIO, spio_reg);
2273 	bnx2x_release_hw_lock(bp, HW_LOCK_RESOURCE_SPIO);
2274 
2275 	return 0;
2276 }
2277 
2278 void bnx2x_calc_fc_adv(struct bnx2x *bp)
2279 {
2280 	u8 cfg_idx = bnx2x_get_link_cfg_idx(bp);
2281 
2282 	bp->port.advertising[cfg_idx] &= ~(ADVERTISED_Asym_Pause |
2283 					   ADVERTISED_Pause);
2284 	switch (bp->link_vars.ieee_fc &
2285 		MDIO_COMBO_IEEE0_AUTO_NEG_ADV_PAUSE_MASK) {
2286 	case MDIO_COMBO_IEEE0_AUTO_NEG_ADV_PAUSE_BOTH:
2287 		bp->port.advertising[cfg_idx] |= (ADVERTISED_Asym_Pause |
2288 						  ADVERTISED_Pause);
2289 		break;
2290 
2291 	case MDIO_COMBO_IEEE0_AUTO_NEG_ADV_PAUSE_ASYMMETRIC:
2292 		bp->port.advertising[cfg_idx] |= ADVERTISED_Asym_Pause;
2293 		break;
2294 
2295 	default:
2296 		break;
2297 	}
2298 }
2299 
2300 static void bnx2x_set_requested_fc(struct bnx2x *bp)
2301 {
2302 	/* Initialize link parameters structure variables
2303 	 * It is recommended to turn off RX FC for jumbo frames
2304 	 *  for better performance
2305 	 */
2306 	if (CHIP_IS_E1x(bp) && (bp->dev->mtu > 5000))
2307 		bp->link_params.req_fc_auto_adv = BNX2X_FLOW_CTRL_TX;
2308 	else
2309 		bp->link_params.req_fc_auto_adv = BNX2X_FLOW_CTRL_BOTH;
2310 }
2311 
2312 static void bnx2x_init_dropless_fc(struct bnx2x *bp)
2313 {
2314 	u32 pause_enabled = 0;
2315 
2316 	if (!CHIP_IS_E1(bp) && bp->dropless_fc && bp->link_vars.link_up) {
2317 		if (bp->link_vars.flow_ctrl & BNX2X_FLOW_CTRL_TX)
2318 			pause_enabled = 1;
2319 
2320 		REG_WR(bp, BAR_USTRORM_INTMEM +
2321 			   USTORM_ETH_PAUSE_ENABLED_OFFSET(BP_PORT(bp)),
2322 		       pause_enabled);
2323 	}
2324 
2325 	DP(NETIF_MSG_IFUP | NETIF_MSG_LINK, "dropless_fc is %s\n",
2326 	   pause_enabled ? "enabled" : "disabled");
2327 }
2328 
2329 int bnx2x_initial_phy_init(struct bnx2x *bp, int load_mode)
2330 {
2331 	int rc, cfx_idx = bnx2x_get_link_cfg_idx(bp);
2332 	u16 req_line_speed = bp->link_params.req_line_speed[cfx_idx];
2333 
2334 	if (!BP_NOMCP(bp)) {
2335 		bnx2x_set_requested_fc(bp);
2336 		bnx2x_acquire_phy_lock(bp);
2337 
2338 		if (load_mode == LOAD_DIAG) {
2339 			struct link_params *lp = &bp->link_params;
2340 			lp->loopback_mode = LOOPBACK_XGXS;
2341 			/* Prefer doing PHY loopback at highest speed */
2342 			if (lp->req_line_speed[cfx_idx] < SPEED_20000) {
2343 				if (lp->speed_cap_mask[cfx_idx] &
2344 				    PORT_HW_CFG_SPEED_CAPABILITY_D0_20G)
2345 					lp->req_line_speed[cfx_idx] =
2346 					SPEED_20000;
2347 				else if (lp->speed_cap_mask[cfx_idx] &
2348 					    PORT_HW_CFG_SPEED_CAPABILITY_D0_10G)
2349 						lp->req_line_speed[cfx_idx] =
2350 						SPEED_10000;
2351 				else
2352 					lp->req_line_speed[cfx_idx] =
2353 					SPEED_1000;
2354 			}
2355 		}
2356 
2357 		if (load_mode == LOAD_LOOPBACK_EXT) {
2358 			struct link_params *lp = &bp->link_params;
2359 			lp->loopback_mode = LOOPBACK_EXT;
2360 		}
2361 
2362 		rc = bnx2x_phy_init(&bp->link_params, &bp->link_vars);
2363 
2364 		bnx2x_release_phy_lock(bp);
2365 
2366 		bnx2x_init_dropless_fc(bp);
2367 
2368 		bnx2x_calc_fc_adv(bp);
2369 
2370 		if (bp->link_vars.link_up) {
2371 			bnx2x_stats_handle(bp, STATS_EVENT_LINK_UP);
2372 			bnx2x_link_report(bp);
2373 		}
2374 		queue_delayed_work(bnx2x_wq, &bp->period_task, 0);
2375 		bp->link_params.req_line_speed[cfx_idx] = req_line_speed;
2376 		return rc;
2377 	}
2378 	BNX2X_ERR("Bootcode is missing - can not initialize link\n");
2379 	return -EINVAL;
2380 }
2381 
2382 void bnx2x_link_set(struct bnx2x *bp)
2383 {
2384 	if (!BP_NOMCP(bp)) {
2385 		bnx2x_acquire_phy_lock(bp);
2386 		bnx2x_phy_init(&bp->link_params, &bp->link_vars);
2387 		bnx2x_release_phy_lock(bp);
2388 
2389 		bnx2x_init_dropless_fc(bp);
2390 
2391 		bnx2x_calc_fc_adv(bp);
2392 	} else
2393 		BNX2X_ERR("Bootcode is missing - can not set link\n");
2394 }
2395 
2396 static void bnx2x__link_reset(struct bnx2x *bp)
2397 {
2398 	if (!BP_NOMCP(bp)) {
2399 		bnx2x_acquire_phy_lock(bp);
2400 		bnx2x_lfa_reset(&bp->link_params, &bp->link_vars);
2401 		bnx2x_release_phy_lock(bp);
2402 	} else
2403 		BNX2X_ERR("Bootcode is missing - can not reset link\n");
2404 }
2405 
2406 void bnx2x_force_link_reset(struct bnx2x *bp)
2407 {
2408 	bnx2x_acquire_phy_lock(bp);
2409 	bnx2x_link_reset(&bp->link_params, &bp->link_vars, 1);
2410 	bnx2x_release_phy_lock(bp);
2411 }
2412 
2413 u8 bnx2x_link_test(struct bnx2x *bp, u8 is_serdes)
2414 {
2415 	u8 rc = 0;
2416 
2417 	if (!BP_NOMCP(bp)) {
2418 		bnx2x_acquire_phy_lock(bp);
2419 		rc = bnx2x_test_link(&bp->link_params, &bp->link_vars,
2420 				     is_serdes);
2421 		bnx2x_release_phy_lock(bp);
2422 	} else
2423 		BNX2X_ERR("Bootcode is missing - can not test link\n");
2424 
2425 	return rc;
2426 }
2427 
2428 /* Calculates the sum of vn_min_rates.
2429    It's needed for further normalizing of the min_rates.
2430    Returns:
2431      sum of vn_min_rates.
2432        or
2433      0 - if all the min_rates are 0.
2434      In the later case fairness algorithm should be deactivated.
2435      If not all min_rates are zero then those that are zeroes will be set to 1.
2436  */
2437 static void bnx2x_calc_vn_min(struct bnx2x *bp,
2438 				      struct cmng_init_input *input)
2439 {
2440 	int all_zero = 1;
2441 	int vn;
2442 
2443 	for (vn = VN_0; vn < BP_MAX_VN_NUM(bp); vn++) {
2444 		u32 vn_cfg = bp->mf_config[vn];
2445 		u32 vn_min_rate = ((vn_cfg & FUNC_MF_CFG_MIN_BW_MASK) >>
2446 				   FUNC_MF_CFG_MIN_BW_SHIFT) * 100;
2447 
2448 		/* Skip hidden vns */
2449 		if (vn_cfg & FUNC_MF_CFG_FUNC_HIDE)
2450 			vn_min_rate = 0;
2451 		/* If min rate is zero - set it to 1 */
2452 		else if (!vn_min_rate)
2453 			vn_min_rate = DEF_MIN_RATE;
2454 		else
2455 			all_zero = 0;
2456 
2457 		input->vnic_min_rate[vn] = vn_min_rate;
2458 	}
2459 
2460 	/* if ETS or all min rates are zeros - disable fairness */
2461 	if (BNX2X_IS_ETS_ENABLED(bp)) {
2462 		input->flags.cmng_enables &=
2463 					~CMNG_FLAGS_PER_PORT_FAIRNESS_VN;
2464 		DP(NETIF_MSG_IFUP, "Fairness will be disabled due to ETS\n");
2465 	} else if (all_zero) {
2466 		input->flags.cmng_enables &=
2467 					~CMNG_FLAGS_PER_PORT_FAIRNESS_VN;
2468 		DP(NETIF_MSG_IFUP,
2469 		   "All MIN values are zeroes fairness will be disabled\n");
2470 	} else
2471 		input->flags.cmng_enables |=
2472 					CMNG_FLAGS_PER_PORT_FAIRNESS_VN;
2473 }
2474 
2475 static void bnx2x_calc_vn_max(struct bnx2x *bp, int vn,
2476 				    struct cmng_init_input *input)
2477 {
2478 	u16 vn_max_rate;
2479 	u32 vn_cfg = bp->mf_config[vn];
2480 
2481 	if (vn_cfg & FUNC_MF_CFG_FUNC_HIDE)
2482 		vn_max_rate = 0;
2483 	else {
2484 		u32 maxCfg = bnx2x_extract_max_cfg(bp, vn_cfg);
2485 
2486 		if (IS_MF_PERCENT_BW(bp)) {
2487 			/* maxCfg in percents of linkspeed */
2488 			vn_max_rate = (bp->link_vars.line_speed * maxCfg) / 100;
2489 		} else /* SD modes */
2490 			/* maxCfg is absolute in 100Mb units */
2491 			vn_max_rate = maxCfg * 100;
2492 	}
2493 
2494 	DP(NETIF_MSG_IFUP, "vn %d: vn_max_rate %d\n", vn, vn_max_rate);
2495 
2496 	input->vnic_max_rate[vn] = vn_max_rate;
2497 }
2498 
2499 static int bnx2x_get_cmng_fns_mode(struct bnx2x *bp)
2500 {
2501 	if (CHIP_REV_IS_SLOW(bp))
2502 		return CMNG_FNS_NONE;
2503 	if (IS_MF(bp))
2504 		return CMNG_FNS_MINMAX;
2505 
2506 	return CMNG_FNS_NONE;
2507 }
2508 
2509 void bnx2x_read_mf_cfg(struct bnx2x *bp)
2510 {
2511 	int vn, n = (CHIP_MODE_IS_4_PORT(bp) ? 2 : 1);
2512 
2513 	if (BP_NOMCP(bp))
2514 		return; /* what should be the default value in this case */
2515 
2516 	/* For 2 port configuration the absolute function number formula
2517 	 * is:
2518 	 *      abs_func = 2 * vn + BP_PORT + BP_PATH
2519 	 *
2520 	 *      and there are 4 functions per port
2521 	 *
2522 	 * For 4 port configuration it is
2523 	 *      abs_func = 4 * vn + 2 * BP_PORT + BP_PATH
2524 	 *
2525 	 *      and there are 2 functions per port
2526 	 */
2527 	for (vn = VN_0; vn < BP_MAX_VN_NUM(bp); vn++) {
2528 		int /*abs*/func = n * (2 * vn + BP_PORT(bp)) + BP_PATH(bp);
2529 
2530 		if (func >= E1H_FUNC_MAX)
2531 			break;
2532 
2533 		bp->mf_config[vn] =
2534 			MF_CFG_RD(bp, func_mf_config[func].config);
2535 	}
2536 	if (bp->mf_config[BP_VN(bp)] & FUNC_MF_CFG_FUNC_DISABLED) {
2537 		DP(NETIF_MSG_IFUP, "mf_cfg function disabled\n");
2538 		bp->flags |= MF_FUNC_DIS;
2539 	} else {
2540 		DP(NETIF_MSG_IFUP, "mf_cfg function enabled\n");
2541 		bp->flags &= ~MF_FUNC_DIS;
2542 	}
2543 }
2544 
2545 static void bnx2x_cmng_fns_init(struct bnx2x *bp, u8 read_cfg, u8 cmng_type)
2546 {
2547 	struct cmng_init_input input;
2548 	memset(&input, 0, sizeof(struct cmng_init_input));
2549 
2550 	input.port_rate = bp->link_vars.line_speed;
2551 
2552 	if (cmng_type == CMNG_FNS_MINMAX && input.port_rate) {
2553 		int vn;
2554 
2555 		/* read mf conf from shmem */
2556 		if (read_cfg)
2557 			bnx2x_read_mf_cfg(bp);
2558 
2559 		/* vn_weight_sum and enable fairness if not 0 */
2560 		bnx2x_calc_vn_min(bp, &input);
2561 
2562 		/* calculate and set min-max rate for each vn */
2563 		if (bp->port.pmf)
2564 			for (vn = VN_0; vn < BP_MAX_VN_NUM(bp); vn++)
2565 				bnx2x_calc_vn_max(bp, vn, &input);
2566 
2567 		/* always enable rate shaping and fairness */
2568 		input.flags.cmng_enables |=
2569 					CMNG_FLAGS_PER_PORT_RATE_SHAPING_VN;
2570 
2571 		bnx2x_init_cmng(&input, &bp->cmng);
2572 		return;
2573 	}
2574 
2575 	/* rate shaping and fairness are disabled */
2576 	DP(NETIF_MSG_IFUP,
2577 	   "rate shaping and fairness are disabled\n");
2578 }
2579 
2580 static void storm_memset_cmng(struct bnx2x *bp,
2581 			      struct cmng_init *cmng,
2582 			      u8 port)
2583 {
2584 	int vn;
2585 	size_t size = sizeof(struct cmng_struct_per_port);
2586 
2587 	u32 addr = BAR_XSTRORM_INTMEM +
2588 			XSTORM_CMNG_PER_PORT_VARS_OFFSET(port);
2589 
2590 	__storm_memset_struct(bp, addr, size, (u32 *)&cmng->port);
2591 
2592 	for (vn = VN_0; vn < BP_MAX_VN_NUM(bp); vn++) {
2593 		int func = func_by_vn(bp, vn);
2594 
2595 		addr = BAR_XSTRORM_INTMEM +
2596 		       XSTORM_RATE_SHAPING_PER_VN_VARS_OFFSET(func);
2597 		size = sizeof(struct rate_shaping_vars_per_vn);
2598 		__storm_memset_struct(bp, addr, size,
2599 				      (u32 *)&cmng->vnic.vnic_max_rate[vn]);
2600 
2601 		addr = BAR_XSTRORM_INTMEM +
2602 		       XSTORM_FAIRNESS_PER_VN_VARS_OFFSET(func);
2603 		size = sizeof(struct fairness_vars_per_vn);
2604 		__storm_memset_struct(bp, addr, size,
2605 				      (u32 *)&cmng->vnic.vnic_min_rate[vn]);
2606 	}
2607 }
2608 
2609 /* init cmng mode in HW according to local configuration */
2610 void bnx2x_set_local_cmng(struct bnx2x *bp)
2611 {
2612 	int cmng_fns = bnx2x_get_cmng_fns_mode(bp);
2613 
2614 	if (cmng_fns != CMNG_FNS_NONE) {
2615 		bnx2x_cmng_fns_init(bp, false, cmng_fns);
2616 		storm_memset_cmng(bp, &bp->cmng, BP_PORT(bp));
2617 	} else {
2618 		/* rate shaping and fairness are disabled */
2619 		DP(NETIF_MSG_IFUP,
2620 		   "single function mode without fairness\n");
2621 	}
2622 }
2623 
2624 /* This function is called upon link interrupt */
2625 static void bnx2x_link_attn(struct bnx2x *bp)
2626 {
2627 	/* Make sure that we are synced with the current statistics */
2628 	bnx2x_stats_handle(bp, STATS_EVENT_STOP);
2629 
2630 	bnx2x_link_update(&bp->link_params, &bp->link_vars);
2631 
2632 	bnx2x_init_dropless_fc(bp);
2633 
2634 	if (bp->link_vars.link_up) {
2635 
2636 		if (bp->link_vars.mac_type != MAC_TYPE_EMAC) {
2637 			struct host_port_stats *pstats;
2638 
2639 			pstats = bnx2x_sp(bp, port_stats);
2640 			/* reset old mac stats */
2641 			memset(&(pstats->mac_stx[0]), 0,
2642 			       sizeof(struct mac_stx));
2643 		}
2644 		if (bp->state == BNX2X_STATE_OPEN)
2645 			bnx2x_stats_handle(bp, STATS_EVENT_LINK_UP);
2646 	}
2647 
2648 	if (bp->link_vars.link_up && bp->link_vars.line_speed)
2649 		bnx2x_set_local_cmng(bp);
2650 
2651 	__bnx2x_link_report(bp);
2652 
2653 	if (IS_MF(bp))
2654 		bnx2x_link_sync_notify(bp);
2655 }
2656 
2657 void bnx2x__link_status_update(struct bnx2x *bp)
2658 {
2659 	if (bp->state != BNX2X_STATE_OPEN)
2660 		return;
2661 
2662 	/* read updated dcb configuration */
2663 	if (IS_PF(bp)) {
2664 		bnx2x_dcbx_pmf_update(bp);
2665 		bnx2x_link_status_update(&bp->link_params, &bp->link_vars);
2666 		if (bp->link_vars.link_up)
2667 			bnx2x_stats_handle(bp, STATS_EVENT_LINK_UP);
2668 		else
2669 			bnx2x_stats_handle(bp, STATS_EVENT_STOP);
2670 			/* indicate link status */
2671 		bnx2x_link_report(bp);
2672 
2673 	} else { /* VF */
2674 		bp->port.supported[0] |= (SUPPORTED_10baseT_Half |
2675 					  SUPPORTED_10baseT_Full |
2676 					  SUPPORTED_100baseT_Half |
2677 					  SUPPORTED_100baseT_Full |
2678 					  SUPPORTED_1000baseT_Full |
2679 					  SUPPORTED_2500baseX_Full |
2680 					  SUPPORTED_10000baseT_Full |
2681 					  SUPPORTED_TP |
2682 					  SUPPORTED_FIBRE |
2683 					  SUPPORTED_Autoneg |
2684 					  SUPPORTED_Pause |
2685 					  SUPPORTED_Asym_Pause);
2686 		bp->port.advertising[0] = bp->port.supported[0];
2687 
2688 		bp->link_params.bp = bp;
2689 		bp->link_params.port = BP_PORT(bp);
2690 		bp->link_params.req_duplex[0] = DUPLEX_FULL;
2691 		bp->link_params.req_flow_ctrl[0] = BNX2X_FLOW_CTRL_NONE;
2692 		bp->link_params.req_line_speed[0] = SPEED_10000;
2693 		bp->link_params.speed_cap_mask[0] = 0x7f0000;
2694 		bp->link_params.switch_cfg = SWITCH_CFG_10G;
2695 		bp->link_vars.mac_type = MAC_TYPE_BMAC;
2696 		bp->link_vars.line_speed = SPEED_10000;
2697 		bp->link_vars.link_status =
2698 			(LINK_STATUS_LINK_UP |
2699 			 LINK_STATUS_SPEED_AND_DUPLEX_10GTFD);
2700 		bp->link_vars.link_up = 1;
2701 		bp->link_vars.duplex = DUPLEX_FULL;
2702 		bp->link_vars.flow_ctrl = BNX2X_FLOW_CTRL_NONE;
2703 		__bnx2x_link_report(bp);
2704 
2705 		bnx2x_sample_bulletin(bp);
2706 
2707 		/* if bulletin board did not have an update for link status
2708 		 * __bnx2x_link_report will report current status
2709 		 * but it will NOT duplicate report in case of already reported
2710 		 * during sampling bulletin board.
2711 		 */
2712 		bnx2x_stats_handle(bp, STATS_EVENT_LINK_UP);
2713 	}
2714 }
2715 
2716 static int bnx2x_afex_func_update(struct bnx2x *bp, u16 vifid,
2717 				  u16 vlan_val, u8 allowed_prio)
2718 {
2719 	struct bnx2x_func_state_params func_params = {NULL};
2720 	struct bnx2x_func_afex_update_params *f_update_params =
2721 		&func_params.params.afex_update;
2722 
2723 	func_params.f_obj = &bp->func_obj;
2724 	func_params.cmd = BNX2X_F_CMD_AFEX_UPDATE;
2725 
2726 	/* no need to wait for RAMROD completion, so don't
2727 	 * set RAMROD_COMP_WAIT flag
2728 	 */
2729 
2730 	f_update_params->vif_id = vifid;
2731 	f_update_params->afex_default_vlan = vlan_val;
2732 	f_update_params->allowed_priorities = allowed_prio;
2733 
2734 	/* if ramrod can not be sent, response to MCP immediately */
2735 	if (bnx2x_func_state_change(bp, &func_params) < 0)
2736 		bnx2x_fw_command(bp, DRV_MSG_CODE_AFEX_VIFSET_ACK, 0);
2737 
2738 	return 0;
2739 }
2740 
2741 static int bnx2x_afex_handle_vif_list_cmd(struct bnx2x *bp, u8 cmd_type,
2742 					  u16 vif_index, u8 func_bit_map)
2743 {
2744 	struct bnx2x_func_state_params func_params = {NULL};
2745 	struct bnx2x_func_afex_viflists_params *update_params =
2746 		&func_params.params.afex_viflists;
2747 	int rc;
2748 	u32 drv_msg_code;
2749 
2750 	/* validate only LIST_SET and LIST_GET are received from switch */
2751 	if ((cmd_type != VIF_LIST_RULE_GET) && (cmd_type != VIF_LIST_RULE_SET))
2752 		BNX2X_ERR("BUG! afex_handle_vif_list_cmd invalid type 0x%x\n",
2753 			  cmd_type);
2754 
2755 	func_params.f_obj = &bp->func_obj;
2756 	func_params.cmd = BNX2X_F_CMD_AFEX_VIFLISTS;
2757 
2758 	/* set parameters according to cmd_type */
2759 	update_params->afex_vif_list_command = cmd_type;
2760 	update_params->vif_list_index = vif_index;
2761 	update_params->func_bit_map =
2762 		(cmd_type == VIF_LIST_RULE_GET) ? 0 : func_bit_map;
2763 	update_params->func_to_clear = 0;
2764 	drv_msg_code =
2765 		(cmd_type == VIF_LIST_RULE_GET) ?
2766 		DRV_MSG_CODE_AFEX_LISTGET_ACK :
2767 		DRV_MSG_CODE_AFEX_LISTSET_ACK;
2768 
2769 	/* if ramrod can not be sent, respond to MCP immediately for
2770 	 * SET and GET requests (other are not triggered from MCP)
2771 	 */
2772 	rc = bnx2x_func_state_change(bp, &func_params);
2773 	if (rc < 0)
2774 		bnx2x_fw_command(bp, drv_msg_code, 0);
2775 
2776 	return 0;
2777 }
2778 
2779 static void bnx2x_handle_afex_cmd(struct bnx2x *bp, u32 cmd)
2780 {
2781 	struct afex_stats afex_stats;
2782 	u32 func = BP_ABS_FUNC(bp);
2783 	u32 mf_config;
2784 	u16 vlan_val;
2785 	u32 vlan_prio;
2786 	u16 vif_id;
2787 	u8 allowed_prio;
2788 	u8 vlan_mode;
2789 	u32 addr_to_write, vifid, addrs, stats_type, i;
2790 
2791 	if (cmd & DRV_STATUS_AFEX_LISTGET_REQ) {
2792 		vifid = SHMEM2_RD(bp, afex_param1_to_driver[BP_FW_MB_IDX(bp)]);
2793 		DP(BNX2X_MSG_MCP,
2794 		   "afex: got MCP req LISTGET_REQ for vifid 0x%x\n", vifid);
2795 		bnx2x_afex_handle_vif_list_cmd(bp, VIF_LIST_RULE_GET, vifid, 0);
2796 	}
2797 
2798 	if (cmd & DRV_STATUS_AFEX_LISTSET_REQ) {
2799 		vifid = SHMEM2_RD(bp, afex_param1_to_driver[BP_FW_MB_IDX(bp)]);
2800 		addrs = SHMEM2_RD(bp, afex_param2_to_driver[BP_FW_MB_IDX(bp)]);
2801 		DP(BNX2X_MSG_MCP,
2802 		   "afex: got MCP req LISTSET_REQ for vifid 0x%x addrs 0x%x\n",
2803 		   vifid, addrs);
2804 		bnx2x_afex_handle_vif_list_cmd(bp, VIF_LIST_RULE_SET, vifid,
2805 					       addrs);
2806 	}
2807 
2808 	if (cmd & DRV_STATUS_AFEX_STATSGET_REQ) {
2809 		addr_to_write = SHMEM2_RD(bp,
2810 			afex_scratchpad_addr_to_write[BP_FW_MB_IDX(bp)]);
2811 		stats_type = SHMEM2_RD(bp,
2812 			afex_param1_to_driver[BP_FW_MB_IDX(bp)]);
2813 
2814 		DP(BNX2X_MSG_MCP,
2815 		   "afex: got MCP req STATSGET_REQ, write to addr 0x%x\n",
2816 		   addr_to_write);
2817 
2818 		bnx2x_afex_collect_stats(bp, (void *)&afex_stats, stats_type);
2819 
2820 		/* write response to scratchpad, for MCP */
2821 		for (i = 0; i < (sizeof(struct afex_stats)/sizeof(u32)); i++)
2822 			REG_WR(bp, addr_to_write + i*sizeof(u32),
2823 			       *(((u32 *)(&afex_stats))+i));
2824 
2825 		/* send ack message to MCP */
2826 		bnx2x_fw_command(bp, DRV_MSG_CODE_AFEX_STATSGET_ACK, 0);
2827 	}
2828 
2829 	if (cmd & DRV_STATUS_AFEX_VIFSET_REQ) {
2830 		mf_config = MF_CFG_RD(bp, func_mf_config[func].config);
2831 		bp->mf_config[BP_VN(bp)] = mf_config;
2832 		DP(BNX2X_MSG_MCP,
2833 		   "afex: got MCP req VIFSET_REQ, mf_config 0x%x\n",
2834 		   mf_config);
2835 
2836 		/* if VIF_SET is "enabled" */
2837 		if (!(mf_config & FUNC_MF_CFG_FUNC_DISABLED)) {
2838 			/* set rate limit directly to internal RAM */
2839 			struct cmng_init_input cmng_input;
2840 			struct rate_shaping_vars_per_vn m_rs_vn;
2841 			size_t size = sizeof(struct rate_shaping_vars_per_vn);
2842 			u32 addr = BAR_XSTRORM_INTMEM +
2843 			    XSTORM_RATE_SHAPING_PER_VN_VARS_OFFSET(BP_FUNC(bp));
2844 
2845 			bp->mf_config[BP_VN(bp)] = mf_config;
2846 
2847 			bnx2x_calc_vn_max(bp, BP_VN(bp), &cmng_input);
2848 			m_rs_vn.vn_counter.rate =
2849 				cmng_input.vnic_max_rate[BP_VN(bp)];
2850 			m_rs_vn.vn_counter.quota =
2851 				(m_rs_vn.vn_counter.rate *
2852 				 RS_PERIODIC_TIMEOUT_USEC) / 8;
2853 
2854 			__storm_memset_struct(bp, addr, size, (u32 *)&m_rs_vn);
2855 
2856 			/* read relevant values from mf_cfg struct in shmem */
2857 			vif_id =
2858 				(MF_CFG_RD(bp, func_mf_config[func].e1hov_tag) &
2859 				 FUNC_MF_CFG_E1HOV_TAG_MASK) >>
2860 				FUNC_MF_CFG_E1HOV_TAG_SHIFT;
2861 			vlan_val =
2862 				(MF_CFG_RD(bp, func_mf_config[func].e1hov_tag) &
2863 				 FUNC_MF_CFG_AFEX_VLAN_MASK) >>
2864 				FUNC_MF_CFG_AFEX_VLAN_SHIFT;
2865 			vlan_prio = (mf_config &
2866 				     FUNC_MF_CFG_TRANSMIT_PRIORITY_MASK) >>
2867 				    FUNC_MF_CFG_TRANSMIT_PRIORITY_SHIFT;
2868 			vlan_val |= (vlan_prio << VLAN_PRIO_SHIFT);
2869 			vlan_mode =
2870 				(MF_CFG_RD(bp,
2871 					   func_mf_config[func].afex_config) &
2872 				 FUNC_MF_CFG_AFEX_VLAN_MODE_MASK) >>
2873 				FUNC_MF_CFG_AFEX_VLAN_MODE_SHIFT;
2874 			allowed_prio =
2875 				(MF_CFG_RD(bp,
2876 					   func_mf_config[func].afex_config) &
2877 				 FUNC_MF_CFG_AFEX_COS_FILTER_MASK) >>
2878 				FUNC_MF_CFG_AFEX_COS_FILTER_SHIFT;
2879 
2880 			/* send ramrod to FW, return in case of failure */
2881 			if (bnx2x_afex_func_update(bp, vif_id, vlan_val,
2882 						   allowed_prio))
2883 				return;
2884 
2885 			bp->afex_def_vlan_tag = vlan_val;
2886 			bp->afex_vlan_mode = vlan_mode;
2887 		} else {
2888 			/* notify link down because BP->flags is disabled */
2889 			bnx2x_link_report(bp);
2890 
2891 			/* send INVALID VIF ramrod to FW */
2892 			bnx2x_afex_func_update(bp, 0xFFFF, 0, 0);
2893 
2894 			/* Reset the default afex VLAN */
2895 			bp->afex_def_vlan_tag = -1;
2896 		}
2897 	}
2898 }
2899 
2900 static void bnx2x_handle_update_svid_cmd(struct bnx2x *bp)
2901 {
2902 	struct bnx2x_func_switch_update_params *switch_update_params;
2903 	struct bnx2x_func_state_params func_params;
2904 
2905 	memset(&func_params, 0, sizeof(struct bnx2x_func_state_params));
2906 	switch_update_params = &func_params.params.switch_update;
2907 	func_params.f_obj = &bp->func_obj;
2908 	func_params.cmd = BNX2X_F_CMD_SWITCH_UPDATE;
2909 
2910 	/* Prepare parameters for function state transitions */
2911 	__set_bit(RAMROD_COMP_WAIT, &func_params.ramrod_flags);
2912 	__set_bit(RAMROD_RETRY, &func_params.ramrod_flags);
2913 
2914 	if (IS_MF_UFP(bp) || IS_MF_BD(bp)) {
2915 		int func = BP_ABS_FUNC(bp);
2916 		u32 val;
2917 
2918 		/* Re-learn the S-tag from shmem */
2919 		val = MF_CFG_RD(bp, func_mf_config[func].e1hov_tag) &
2920 				FUNC_MF_CFG_E1HOV_TAG_MASK;
2921 		if (val != FUNC_MF_CFG_E1HOV_TAG_DEFAULT) {
2922 			bp->mf_ov = val;
2923 		} else {
2924 			BNX2X_ERR("Got an SVID event, but no tag is configured in shmem\n");
2925 			goto fail;
2926 		}
2927 
2928 		/* Configure new S-tag in LLH */
2929 		REG_WR(bp, NIG_REG_LLH0_FUNC_VLAN_ID + BP_PORT(bp) * 8,
2930 		       bp->mf_ov);
2931 
2932 		/* Send Ramrod to update FW of change */
2933 		__set_bit(BNX2X_F_UPDATE_SD_VLAN_TAG_CHNG,
2934 			  &switch_update_params->changes);
2935 		switch_update_params->vlan = bp->mf_ov;
2936 
2937 		if (bnx2x_func_state_change(bp, &func_params) < 0) {
2938 			BNX2X_ERR("Failed to configure FW of S-tag Change to %02x\n",
2939 				  bp->mf_ov);
2940 			goto fail;
2941 		} else {
2942 			DP(BNX2X_MSG_MCP, "Configured S-tag %02x\n",
2943 			   bp->mf_ov);
2944 		}
2945 	} else {
2946 		goto fail;
2947 	}
2948 
2949 	bnx2x_fw_command(bp, DRV_MSG_CODE_OEM_UPDATE_SVID_OK, 0);
2950 	return;
2951 fail:
2952 	bnx2x_fw_command(bp, DRV_MSG_CODE_OEM_UPDATE_SVID_FAILURE, 0);
2953 }
2954 
2955 static void bnx2x_pmf_update(struct bnx2x *bp)
2956 {
2957 	int port = BP_PORT(bp);
2958 	u32 val;
2959 
2960 	bp->port.pmf = 1;
2961 	DP(BNX2X_MSG_MCP, "pmf %d\n", bp->port.pmf);
2962 
2963 	/*
2964 	 * We need the mb() to ensure the ordering between the writing to
2965 	 * bp->port.pmf here and reading it from the bnx2x_periodic_task().
2966 	 */
2967 	smp_mb();
2968 
2969 	/* queue a periodic task */
2970 	queue_delayed_work(bnx2x_wq, &bp->period_task, 0);
2971 
2972 	bnx2x_dcbx_pmf_update(bp);
2973 
2974 	/* enable nig attention */
2975 	val = (0xff0f | (1 << (BP_VN(bp) + 4)));
2976 	if (bp->common.int_block == INT_BLOCK_HC) {
2977 		REG_WR(bp, HC_REG_TRAILING_EDGE_0 + port*8, val);
2978 		REG_WR(bp, HC_REG_LEADING_EDGE_0 + port*8, val);
2979 	} else if (!CHIP_IS_E1x(bp)) {
2980 		REG_WR(bp, IGU_REG_TRAILING_EDGE_LATCH, val);
2981 		REG_WR(bp, IGU_REG_LEADING_EDGE_LATCH, val);
2982 	}
2983 
2984 	bnx2x_stats_handle(bp, STATS_EVENT_PMF);
2985 }
2986 
2987 /* end of Link */
2988 
2989 /* slow path */
2990 
2991 /*
2992  * General service functions
2993  */
2994 
2995 /* send the MCP a request, block until there is a reply */
2996 u32 bnx2x_fw_command(struct bnx2x *bp, u32 command, u32 param)
2997 {
2998 	int mb_idx = BP_FW_MB_IDX(bp);
2999 	u32 seq;
3000 	u32 rc = 0;
3001 	u32 cnt = 1;
3002 	u8 delay = CHIP_REV_IS_SLOW(bp) ? 100 : 10;
3003 
3004 	mutex_lock(&bp->fw_mb_mutex);
3005 	seq = ++bp->fw_seq;
3006 	SHMEM_WR(bp, func_mb[mb_idx].drv_mb_param, param);
3007 	SHMEM_WR(bp, func_mb[mb_idx].drv_mb_header, (command | seq));
3008 
3009 	DP(BNX2X_MSG_MCP, "wrote command (%x) to FW MB param 0x%08x\n",
3010 			(command | seq), param);
3011 
3012 	do {
3013 		/* let the FW do it's magic ... */
3014 		msleep(delay);
3015 
3016 		rc = SHMEM_RD(bp, func_mb[mb_idx].fw_mb_header);
3017 
3018 		/* Give the FW up to 5 second (500*10ms) */
3019 	} while ((seq != (rc & FW_MSG_SEQ_NUMBER_MASK)) && (cnt++ < 500));
3020 
3021 	DP(BNX2X_MSG_MCP, "[after %d ms] read (%x) seq is (%x) from FW MB\n",
3022 	   cnt*delay, rc, seq);
3023 
3024 	/* is this a reply to our command? */
3025 	if (seq == (rc & FW_MSG_SEQ_NUMBER_MASK))
3026 		rc &= FW_MSG_CODE_MASK;
3027 	else {
3028 		/* FW BUG! */
3029 		BNX2X_ERR("FW failed to respond!\n");
3030 		bnx2x_fw_dump(bp);
3031 		rc = 0;
3032 	}
3033 	mutex_unlock(&bp->fw_mb_mutex);
3034 
3035 	return rc;
3036 }
3037 
3038 static void storm_memset_func_cfg(struct bnx2x *bp,
3039 				 struct tstorm_eth_function_common_config *tcfg,
3040 				 u16 abs_fid)
3041 {
3042 	size_t size = sizeof(struct tstorm_eth_function_common_config);
3043 
3044 	u32 addr = BAR_TSTRORM_INTMEM +
3045 			TSTORM_FUNCTION_COMMON_CONFIG_OFFSET(abs_fid);
3046 
3047 	__storm_memset_struct(bp, addr, size, (u32 *)tcfg);
3048 }
3049 
3050 void bnx2x_func_init(struct bnx2x *bp, struct bnx2x_func_init_params *p)
3051 {
3052 	if (CHIP_IS_E1x(bp)) {
3053 		struct tstorm_eth_function_common_config tcfg = {0};
3054 
3055 		storm_memset_func_cfg(bp, &tcfg, p->func_id);
3056 	}
3057 
3058 	/* Enable the function in the FW */
3059 	storm_memset_vf_to_pf(bp, p->func_id, p->pf_id);
3060 	storm_memset_func_en(bp, p->func_id, 1);
3061 
3062 	/* spq */
3063 	if (p->spq_active) {
3064 		storm_memset_spq_addr(bp, p->spq_map, p->func_id);
3065 		REG_WR(bp, XSEM_REG_FAST_MEMORY +
3066 		       XSTORM_SPQ_PROD_OFFSET(p->func_id), p->spq_prod);
3067 	}
3068 }
3069 
3070 /**
3071  * bnx2x_get_common_flags - Return common flags
3072  *
3073  * @bp		device handle
3074  * @fp		queue handle
3075  * @zero_stats	TRUE if statistics zeroing is needed
3076  *
3077  * Return the flags that are common for the Tx-only and not normal connections.
3078  */
3079 static unsigned long bnx2x_get_common_flags(struct bnx2x *bp,
3080 					    struct bnx2x_fastpath *fp,
3081 					    bool zero_stats)
3082 {
3083 	unsigned long flags = 0;
3084 
3085 	/* PF driver will always initialize the Queue to an ACTIVE state */
3086 	__set_bit(BNX2X_Q_FLG_ACTIVE, &flags);
3087 
3088 	/* tx only connections collect statistics (on the same index as the
3089 	 * parent connection). The statistics are zeroed when the parent
3090 	 * connection is initialized.
3091 	 */
3092 
3093 	__set_bit(BNX2X_Q_FLG_STATS, &flags);
3094 	if (zero_stats)
3095 		__set_bit(BNX2X_Q_FLG_ZERO_STATS, &flags);
3096 
3097 	if (bp->flags & TX_SWITCHING)
3098 		__set_bit(BNX2X_Q_FLG_TX_SWITCH, &flags);
3099 
3100 	__set_bit(BNX2X_Q_FLG_PCSUM_ON_PKT, &flags);
3101 	__set_bit(BNX2X_Q_FLG_TUN_INC_INNER_IP_ID, &flags);
3102 
3103 #ifdef BNX2X_STOP_ON_ERROR
3104 	__set_bit(BNX2X_Q_FLG_TX_SEC, &flags);
3105 #endif
3106 
3107 	return flags;
3108 }
3109 
3110 static unsigned long bnx2x_get_q_flags(struct bnx2x *bp,
3111 				       struct bnx2x_fastpath *fp,
3112 				       bool leading)
3113 {
3114 	unsigned long flags = 0;
3115 
3116 	/* calculate other queue flags */
3117 	if (IS_MF_SD(bp))
3118 		__set_bit(BNX2X_Q_FLG_OV, &flags);
3119 
3120 	if (IS_FCOE_FP(fp)) {
3121 		__set_bit(BNX2X_Q_FLG_FCOE, &flags);
3122 		/* For FCoE - force usage of default priority (for afex) */
3123 		__set_bit(BNX2X_Q_FLG_FORCE_DEFAULT_PRI, &flags);
3124 	}
3125 
3126 	if (fp->mode != TPA_MODE_DISABLED) {
3127 		__set_bit(BNX2X_Q_FLG_TPA, &flags);
3128 		__set_bit(BNX2X_Q_FLG_TPA_IPV6, &flags);
3129 		if (fp->mode == TPA_MODE_GRO)
3130 			__set_bit(BNX2X_Q_FLG_TPA_GRO, &flags);
3131 	}
3132 
3133 	if (leading) {
3134 		__set_bit(BNX2X_Q_FLG_LEADING_RSS, &flags);
3135 		__set_bit(BNX2X_Q_FLG_MCAST, &flags);
3136 	}
3137 
3138 	/* Always set HW VLAN stripping */
3139 	__set_bit(BNX2X_Q_FLG_VLAN, &flags);
3140 
3141 	/* configure silent vlan removal */
3142 	if (IS_MF_AFEX(bp))
3143 		__set_bit(BNX2X_Q_FLG_SILENT_VLAN_REM, &flags);
3144 
3145 	return flags | bnx2x_get_common_flags(bp, fp, true);
3146 }
3147 
3148 static void bnx2x_pf_q_prep_general(struct bnx2x *bp,
3149 	struct bnx2x_fastpath *fp, struct bnx2x_general_setup_params *gen_init,
3150 	u8 cos)
3151 {
3152 	gen_init->stat_id = bnx2x_stats_id(fp);
3153 	gen_init->spcl_id = fp->cl_id;
3154 
3155 	/* Always use mini-jumbo MTU for FCoE L2 ring */
3156 	if (IS_FCOE_FP(fp))
3157 		gen_init->mtu = BNX2X_FCOE_MINI_JUMBO_MTU;
3158 	else
3159 		gen_init->mtu = bp->dev->mtu;
3160 
3161 	gen_init->cos = cos;
3162 
3163 	gen_init->fp_hsi = ETH_FP_HSI_VERSION;
3164 }
3165 
3166 static void bnx2x_pf_rx_q_prep(struct bnx2x *bp,
3167 	struct bnx2x_fastpath *fp, struct rxq_pause_params *pause,
3168 	struct bnx2x_rxq_setup_params *rxq_init)
3169 {
3170 	u8 max_sge = 0;
3171 	u16 sge_sz = 0;
3172 	u16 tpa_agg_size = 0;
3173 
3174 	if (fp->mode != TPA_MODE_DISABLED) {
3175 		pause->sge_th_lo = SGE_TH_LO(bp);
3176 		pause->sge_th_hi = SGE_TH_HI(bp);
3177 
3178 		/* validate SGE ring has enough to cross high threshold */
3179 		WARN_ON(bp->dropless_fc &&
3180 				pause->sge_th_hi + FW_PREFETCH_CNT >
3181 				MAX_RX_SGE_CNT * NUM_RX_SGE_PAGES);
3182 
3183 		tpa_agg_size = TPA_AGG_SIZE;
3184 		max_sge = SGE_PAGE_ALIGN(bp->dev->mtu) >>
3185 			SGE_PAGE_SHIFT;
3186 		max_sge = ((max_sge + PAGES_PER_SGE - 1) &
3187 			  (~(PAGES_PER_SGE-1))) >> PAGES_PER_SGE_SHIFT;
3188 		sge_sz = (u16)min_t(u32, SGE_PAGES, 0xffff);
3189 	}
3190 
3191 	/* pause - not for e1 */
3192 	if (!CHIP_IS_E1(bp)) {
3193 		pause->bd_th_lo = BD_TH_LO(bp);
3194 		pause->bd_th_hi = BD_TH_HI(bp);
3195 
3196 		pause->rcq_th_lo = RCQ_TH_LO(bp);
3197 		pause->rcq_th_hi = RCQ_TH_HI(bp);
3198 		/*
3199 		 * validate that rings have enough entries to cross
3200 		 * high thresholds
3201 		 */
3202 		WARN_ON(bp->dropless_fc &&
3203 				pause->bd_th_hi + FW_PREFETCH_CNT >
3204 				bp->rx_ring_size);
3205 		WARN_ON(bp->dropless_fc &&
3206 				pause->rcq_th_hi + FW_PREFETCH_CNT >
3207 				NUM_RCQ_RINGS * MAX_RCQ_DESC_CNT);
3208 
3209 		pause->pri_map = 1;
3210 	}
3211 
3212 	/* rxq setup */
3213 	rxq_init->dscr_map = fp->rx_desc_mapping;
3214 	rxq_init->sge_map = fp->rx_sge_mapping;
3215 	rxq_init->rcq_map = fp->rx_comp_mapping;
3216 	rxq_init->rcq_np_map = fp->rx_comp_mapping + BCM_PAGE_SIZE;
3217 
3218 	/* This should be a maximum number of data bytes that may be
3219 	 * placed on the BD (not including paddings).
3220 	 */
3221 	rxq_init->buf_sz = fp->rx_buf_size - BNX2X_FW_RX_ALIGN_START -
3222 			   BNX2X_FW_RX_ALIGN_END - IP_HEADER_ALIGNMENT_PADDING;
3223 
3224 	rxq_init->cl_qzone_id = fp->cl_qzone_id;
3225 	rxq_init->tpa_agg_sz = tpa_agg_size;
3226 	rxq_init->sge_buf_sz = sge_sz;
3227 	rxq_init->max_sges_pkt = max_sge;
3228 	rxq_init->rss_engine_id = BP_FUNC(bp);
3229 	rxq_init->mcast_engine_id = BP_FUNC(bp);
3230 
3231 	/* Maximum number or simultaneous TPA aggregation for this Queue.
3232 	 *
3233 	 * For PF Clients it should be the maximum available number.
3234 	 * VF driver(s) may want to define it to a smaller value.
3235 	 */
3236 	rxq_init->max_tpa_queues = MAX_AGG_QS(bp);
3237 
3238 	rxq_init->cache_line_log = BNX2X_RX_ALIGN_SHIFT;
3239 	rxq_init->fw_sb_id = fp->fw_sb_id;
3240 
3241 	if (IS_FCOE_FP(fp))
3242 		rxq_init->sb_cq_index = HC_SP_INDEX_ETH_FCOE_RX_CQ_CONS;
3243 	else
3244 		rxq_init->sb_cq_index = HC_INDEX_ETH_RX_CQ_CONS;
3245 	/* configure silent vlan removal
3246 	 * if multi function mode is afex, then mask default vlan
3247 	 */
3248 	if (IS_MF_AFEX(bp)) {
3249 		rxq_init->silent_removal_value = bp->afex_def_vlan_tag;
3250 		rxq_init->silent_removal_mask = VLAN_VID_MASK;
3251 	}
3252 }
3253 
3254 static void bnx2x_pf_tx_q_prep(struct bnx2x *bp,
3255 	struct bnx2x_fastpath *fp, struct bnx2x_txq_setup_params *txq_init,
3256 	u8 cos)
3257 {
3258 	txq_init->dscr_map = fp->txdata_ptr[cos]->tx_desc_mapping;
3259 	txq_init->sb_cq_index = HC_INDEX_ETH_FIRST_TX_CQ_CONS + cos;
3260 	txq_init->traffic_type = LLFC_TRAFFIC_TYPE_NW;
3261 	txq_init->fw_sb_id = fp->fw_sb_id;
3262 
3263 	/*
3264 	 * set the tss leading client id for TX classification ==
3265 	 * leading RSS client id
3266 	 */
3267 	txq_init->tss_leading_cl_id = bnx2x_fp(bp, 0, cl_id);
3268 
3269 	if (IS_FCOE_FP(fp)) {
3270 		txq_init->sb_cq_index = HC_SP_INDEX_ETH_FCOE_TX_CQ_CONS;
3271 		txq_init->traffic_type = LLFC_TRAFFIC_TYPE_FCOE;
3272 	}
3273 }
3274 
3275 static void bnx2x_pf_init(struct bnx2x *bp)
3276 {
3277 	struct bnx2x_func_init_params func_init = {0};
3278 	struct event_ring_data eq_data = { {0} };
3279 
3280 	if (!CHIP_IS_E1x(bp)) {
3281 		/* reset IGU PF statistics: MSIX + ATTN */
3282 		/* PF */
3283 		REG_WR(bp, IGU_REG_STATISTIC_NUM_MESSAGE_SENT +
3284 			   BNX2X_IGU_STAS_MSG_VF_CNT*4 +
3285 			   (CHIP_MODE_IS_4_PORT(bp) ?
3286 				BP_FUNC(bp) : BP_VN(bp))*4, 0);
3287 		/* ATTN */
3288 		REG_WR(bp, IGU_REG_STATISTIC_NUM_MESSAGE_SENT +
3289 			   BNX2X_IGU_STAS_MSG_VF_CNT*4 +
3290 			   BNX2X_IGU_STAS_MSG_PF_CNT*4 +
3291 			   (CHIP_MODE_IS_4_PORT(bp) ?
3292 				BP_FUNC(bp) : BP_VN(bp))*4, 0);
3293 	}
3294 
3295 	func_init.spq_active = true;
3296 	func_init.pf_id = BP_FUNC(bp);
3297 	func_init.func_id = BP_FUNC(bp);
3298 	func_init.spq_map = bp->spq_mapping;
3299 	func_init.spq_prod = bp->spq_prod_idx;
3300 
3301 	bnx2x_func_init(bp, &func_init);
3302 
3303 	memset(&(bp->cmng), 0, sizeof(struct cmng_struct_per_port));
3304 
3305 	/*
3306 	 * Congestion management values depend on the link rate
3307 	 * There is no active link so initial link rate is set to 10 Gbps.
3308 	 * When the link comes up The congestion management values are
3309 	 * re-calculated according to the actual link rate.
3310 	 */
3311 	bp->link_vars.line_speed = SPEED_10000;
3312 	bnx2x_cmng_fns_init(bp, true, bnx2x_get_cmng_fns_mode(bp));
3313 
3314 	/* Only the PMF sets the HW */
3315 	if (bp->port.pmf)
3316 		storm_memset_cmng(bp, &bp->cmng, BP_PORT(bp));
3317 
3318 	/* init Event Queue - PCI bus guarantees correct endianity*/
3319 	eq_data.base_addr.hi = U64_HI(bp->eq_mapping);
3320 	eq_data.base_addr.lo = U64_LO(bp->eq_mapping);
3321 	eq_data.producer = bp->eq_prod;
3322 	eq_data.index_id = HC_SP_INDEX_EQ_CONS;
3323 	eq_data.sb_id = DEF_SB_ID;
3324 	storm_memset_eq_data(bp, &eq_data, BP_FUNC(bp));
3325 }
3326 
3327 static void bnx2x_e1h_disable(struct bnx2x *bp)
3328 {
3329 	int port = BP_PORT(bp);
3330 
3331 	bnx2x_tx_disable(bp);
3332 
3333 	REG_WR(bp, NIG_REG_LLH0_FUNC_EN + port*8, 0);
3334 }
3335 
3336 static void bnx2x_e1h_enable(struct bnx2x *bp)
3337 {
3338 	int port = BP_PORT(bp);
3339 
3340 	if (!(IS_MF_UFP(bp) && BNX2X_IS_MF_SD_PROTOCOL_FCOE(bp)))
3341 		REG_WR(bp, NIG_REG_LLH0_FUNC_EN + port * 8, 1);
3342 
3343 	/* Tx queue should be only re-enabled */
3344 	netif_tx_wake_all_queues(bp->dev);
3345 
3346 	/*
3347 	 * Should not call netif_carrier_on since it will be called if the link
3348 	 * is up when checking for link state
3349 	 */
3350 }
3351 
3352 #define DRV_INFO_ETH_STAT_NUM_MACS_REQUIRED 3
3353 
3354 static void bnx2x_drv_info_ether_stat(struct bnx2x *bp)
3355 {
3356 	struct eth_stats_info *ether_stat =
3357 		&bp->slowpath->drv_info_to_mcp.ether_stat;
3358 	struct bnx2x_vlan_mac_obj *mac_obj =
3359 		&bp->sp_objs->mac_obj;
3360 	int i;
3361 
3362 	strlcpy(ether_stat->version, DRV_MODULE_VERSION,
3363 		ETH_STAT_INFO_VERSION_LEN);
3364 
3365 	/* get DRV_INFO_ETH_STAT_NUM_MACS_REQUIRED macs, placing them in the
3366 	 * mac_local field in ether_stat struct. The base address is offset by 2
3367 	 * bytes to account for the field being 8 bytes but a mac address is
3368 	 * only 6 bytes. Likewise, the stride for the get_n_elements function is
3369 	 * 2 bytes to compensate from the 6 bytes of a mac to the 8 bytes
3370 	 * allocated by the ether_stat struct, so the macs will land in their
3371 	 * proper positions.
3372 	 */
3373 	for (i = 0; i < DRV_INFO_ETH_STAT_NUM_MACS_REQUIRED; i++)
3374 		memset(ether_stat->mac_local + i, 0,
3375 		       sizeof(ether_stat->mac_local[0]));
3376 	mac_obj->get_n_elements(bp, &bp->sp_objs[0].mac_obj,
3377 				DRV_INFO_ETH_STAT_NUM_MACS_REQUIRED,
3378 				ether_stat->mac_local + MAC_PAD, MAC_PAD,
3379 				ETH_ALEN);
3380 	ether_stat->mtu_size = bp->dev->mtu;
3381 	if (bp->dev->features & NETIF_F_RXCSUM)
3382 		ether_stat->feature_flags |= FEATURE_ETH_CHKSUM_OFFLOAD_MASK;
3383 	if (bp->dev->features & NETIF_F_TSO)
3384 		ether_stat->feature_flags |= FEATURE_ETH_LSO_MASK;
3385 	ether_stat->feature_flags |= bp->common.boot_mode;
3386 
3387 	ether_stat->promiscuous_mode = (bp->dev->flags & IFF_PROMISC) ? 1 : 0;
3388 
3389 	ether_stat->txq_size = bp->tx_ring_size;
3390 	ether_stat->rxq_size = bp->rx_ring_size;
3391 
3392 #ifdef CONFIG_BNX2X_SRIOV
3393 	ether_stat->vf_cnt = IS_SRIOV(bp) ? bp->vfdb->sriov.nr_virtfn : 0;
3394 #endif
3395 }
3396 
3397 static void bnx2x_drv_info_fcoe_stat(struct bnx2x *bp)
3398 {
3399 	struct bnx2x_dcbx_app_params *app = &bp->dcbx_port_params.app;
3400 	struct fcoe_stats_info *fcoe_stat =
3401 		&bp->slowpath->drv_info_to_mcp.fcoe_stat;
3402 
3403 	if (!CNIC_LOADED(bp))
3404 		return;
3405 
3406 	memcpy(fcoe_stat->mac_local + MAC_PAD, bp->fip_mac, ETH_ALEN);
3407 
3408 	fcoe_stat->qos_priority =
3409 		app->traffic_type_priority[LLFC_TRAFFIC_TYPE_FCOE];
3410 
3411 	/* insert FCoE stats from ramrod response */
3412 	if (!NO_FCOE(bp)) {
3413 		struct tstorm_per_queue_stats *fcoe_q_tstorm_stats =
3414 			&bp->fw_stats_data->queue_stats[FCOE_IDX(bp)].
3415 			tstorm_queue_statistics;
3416 
3417 		struct xstorm_per_queue_stats *fcoe_q_xstorm_stats =
3418 			&bp->fw_stats_data->queue_stats[FCOE_IDX(bp)].
3419 			xstorm_queue_statistics;
3420 
3421 		struct fcoe_statistics_params *fw_fcoe_stat =
3422 			&bp->fw_stats_data->fcoe;
3423 
3424 		ADD_64_LE(fcoe_stat->rx_bytes_hi, LE32_0,
3425 			  fcoe_stat->rx_bytes_lo,
3426 			  fw_fcoe_stat->rx_stat0.fcoe_rx_byte_cnt);
3427 
3428 		ADD_64_LE(fcoe_stat->rx_bytes_hi,
3429 			  fcoe_q_tstorm_stats->rcv_ucast_bytes.hi,
3430 			  fcoe_stat->rx_bytes_lo,
3431 			  fcoe_q_tstorm_stats->rcv_ucast_bytes.lo);
3432 
3433 		ADD_64_LE(fcoe_stat->rx_bytes_hi,
3434 			  fcoe_q_tstorm_stats->rcv_bcast_bytes.hi,
3435 			  fcoe_stat->rx_bytes_lo,
3436 			  fcoe_q_tstorm_stats->rcv_bcast_bytes.lo);
3437 
3438 		ADD_64_LE(fcoe_stat->rx_bytes_hi,
3439 			  fcoe_q_tstorm_stats->rcv_mcast_bytes.hi,
3440 			  fcoe_stat->rx_bytes_lo,
3441 			  fcoe_q_tstorm_stats->rcv_mcast_bytes.lo);
3442 
3443 		ADD_64_LE(fcoe_stat->rx_frames_hi, LE32_0,
3444 			  fcoe_stat->rx_frames_lo,
3445 			  fw_fcoe_stat->rx_stat0.fcoe_rx_pkt_cnt);
3446 
3447 		ADD_64_LE(fcoe_stat->rx_frames_hi, LE32_0,
3448 			  fcoe_stat->rx_frames_lo,
3449 			  fcoe_q_tstorm_stats->rcv_ucast_pkts);
3450 
3451 		ADD_64_LE(fcoe_stat->rx_frames_hi, LE32_0,
3452 			  fcoe_stat->rx_frames_lo,
3453 			  fcoe_q_tstorm_stats->rcv_bcast_pkts);
3454 
3455 		ADD_64_LE(fcoe_stat->rx_frames_hi, LE32_0,
3456 			  fcoe_stat->rx_frames_lo,
3457 			  fcoe_q_tstorm_stats->rcv_mcast_pkts);
3458 
3459 		ADD_64_LE(fcoe_stat->tx_bytes_hi, LE32_0,
3460 			  fcoe_stat->tx_bytes_lo,
3461 			  fw_fcoe_stat->tx_stat.fcoe_tx_byte_cnt);
3462 
3463 		ADD_64_LE(fcoe_stat->tx_bytes_hi,
3464 			  fcoe_q_xstorm_stats->ucast_bytes_sent.hi,
3465 			  fcoe_stat->tx_bytes_lo,
3466 			  fcoe_q_xstorm_stats->ucast_bytes_sent.lo);
3467 
3468 		ADD_64_LE(fcoe_stat->tx_bytes_hi,
3469 			  fcoe_q_xstorm_stats->bcast_bytes_sent.hi,
3470 			  fcoe_stat->tx_bytes_lo,
3471 			  fcoe_q_xstorm_stats->bcast_bytes_sent.lo);
3472 
3473 		ADD_64_LE(fcoe_stat->tx_bytes_hi,
3474 			  fcoe_q_xstorm_stats->mcast_bytes_sent.hi,
3475 			  fcoe_stat->tx_bytes_lo,
3476 			  fcoe_q_xstorm_stats->mcast_bytes_sent.lo);
3477 
3478 		ADD_64_LE(fcoe_stat->tx_frames_hi, LE32_0,
3479 			  fcoe_stat->tx_frames_lo,
3480 			  fw_fcoe_stat->tx_stat.fcoe_tx_pkt_cnt);
3481 
3482 		ADD_64_LE(fcoe_stat->tx_frames_hi, LE32_0,
3483 			  fcoe_stat->tx_frames_lo,
3484 			  fcoe_q_xstorm_stats->ucast_pkts_sent);
3485 
3486 		ADD_64_LE(fcoe_stat->tx_frames_hi, LE32_0,
3487 			  fcoe_stat->tx_frames_lo,
3488 			  fcoe_q_xstorm_stats->bcast_pkts_sent);
3489 
3490 		ADD_64_LE(fcoe_stat->tx_frames_hi, LE32_0,
3491 			  fcoe_stat->tx_frames_lo,
3492 			  fcoe_q_xstorm_stats->mcast_pkts_sent);
3493 	}
3494 
3495 	/* ask L5 driver to add data to the struct */
3496 	bnx2x_cnic_notify(bp, CNIC_CTL_FCOE_STATS_GET_CMD);
3497 }
3498 
3499 static void bnx2x_drv_info_iscsi_stat(struct bnx2x *bp)
3500 {
3501 	struct bnx2x_dcbx_app_params *app = &bp->dcbx_port_params.app;
3502 	struct iscsi_stats_info *iscsi_stat =
3503 		&bp->slowpath->drv_info_to_mcp.iscsi_stat;
3504 
3505 	if (!CNIC_LOADED(bp))
3506 		return;
3507 
3508 	memcpy(iscsi_stat->mac_local + MAC_PAD, bp->cnic_eth_dev.iscsi_mac,
3509 	       ETH_ALEN);
3510 
3511 	iscsi_stat->qos_priority =
3512 		app->traffic_type_priority[LLFC_TRAFFIC_TYPE_ISCSI];
3513 
3514 	/* ask L5 driver to add data to the struct */
3515 	bnx2x_cnic_notify(bp, CNIC_CTL_ISCSI_STATS_GET_CMD);
3516 }
3517 
3518 /* called due to MCP event (on pmf):
3519  *	reread new bandwidth configuration
3520  *	configure FW
3521  *	notify others function about the change
3522  */
3523 static void bnx2x_config_mf_bw(struct bnx2x *bp)
3524 {
3525 	/* Workaround for MFW bug.
3526 	 * MFW is not supposed to generate BW attention in
3527 	 * single function mode.
3528 	 */
3529 	if (!IS_MF(bp)) {
3530 		DP(BNX2X_MSG_MCP,
3531 		   "Ignoring MF BW config in single function mode\n");
3532 		return;
3533 	}
3534 
3535 	if (bp->link_vars.link_up) {
3536 		bnx2x_cmng_fns_init(bp, true, CMNG_FNS_MINMAX);
3537 		bnx2x_link_sync_notify(bp);
3538 	}
3539 	storm_memset_cmng(bp, &bp->cmng, BP_PORT(bp));
3540 }
3541 
3542 static void bnx2x_set_mf_bw(struct bnx2x *bp)
3543 {
3544 	bnx2x_config_mf_bw(bp);
3545 	bnx2x_fw_command(bp, DRV_MSG_CODE_SET_MF_BW_ACK, 0);
3546 }
3547 
3548 static void bnx2x_handle_eee_event(struct bnx2x *bp)
3549 {
3550 	DP(BNX2X_MSG_MCP, "EEE - LLDP event\n");
3551 	bnx2x_fw_command(bp, DRV_MSG_CODE_EEE_RESULTS_ACK, 0);
3552 }
3553 
3554 #define BNX2X_UPDATE_DRV_INFO_IND_LENGTH	(20)
3555 #define BNX2X_UPDATE_DRV_INFO_IND_COUNT		(25)
3556 
3557 static void bnx2x_handle_drv_info_req(struct bnx2x *bp)
3558 {
3559 	enum drv_info_opcode op_code;
3560 	u32 drv_info_ctl = SHMEM2_RD(bp, drv_info_control);
3561 	bool release = false;
3562 	int wait;
3563 
3564 	/* if drv_info version supported by MFW doesn't match - send NACK */
3565 	if ((drv_info_ctl & DRV_INFO_CONTROL_VER_MASK) != DRV_INFO_CUR_VER) {
3566 		bnx2x_fw_command(bp, DRV_MSG_CODE_DRV_INFO_NACK, 0);
3567 		return;
3568 	}
3569 
3570 	op_code = (drv_info_ctl & DRV_INFO_CONTROL_OP_CODE_MASK) >>
3571 		  DRV_INFO_CONTROL_OP_CODE_SHIFT;
3572 
3573 	/* Must prevent other flows from accessing drv_info_to_mcp */
3574 	mutex_lock(&bp->drv_info_mutex);
3575 
3576 	memset(&bp->slowpath->drv_info_to_mcp, 0,
3577 	       sizeof(union drv_info_to_mcp));
3578 
3579 	switch (op_code) {
3580 	case ETH_STATS_OPCODE:
3581 		bnx2x_drv_info_ether_stat(bp);
3582 		break;
3583 	case FCOE_STATS_OPCODE:
3584 		bnx2x_drv_info_fcoe_stat(bp);
3585 		break;
3586 	case ISCSI_STATS_OPCODE:
3587 		bnx2x_drv_info_iscsi_stat(bp);
3588 		break;
3589 	default:
3590 		/* if op code isn't supported - send NACK */
3591 		bnx2x_fw_command(bp, DRV_MSG_CODE_DRV_INFO_NACK, 0);
3592 		goto out;
3593 	}
3594 
3595 	/* if we got drv_info attn from MFW then these fields are defined in
3596 	 * shmem2 for sure
3597 	 */
3598 	SHMEM2_WR(bp, drv_info_host_addr_lo,
3599 		U64_LO(bnx2x_sp_mapping(bp, drv_info_to_mcp)));
3600 	SHMEM2_WR(bp, drv_info_host_addr_hi,
3601 		U64_HI(bnx2x_sp_mapping(bp, drv_info_to_mcp)));
3602 
3603 	bnx2x_fw_command(bp, DRV_MSG_CODE_DRV_INFO_ACK, 0);
3604 
3605 	/* Since possible management wants both this and get_driver_version
3606 	 * need to wait until management notifies us it finished utilizing
3607 	 * the buffer.
3608 	 */
3609 	if (!SHMEM2_HAS(bp, mfw_drv_indication)) {
3610 		DP(BNX2X_MSG_MCP, "Management does not support indication\n");
3611 	} else if (!bp->drv_info_mng_owner) {
3612 		u32 bit = MFW_DRV_IND_READ_DONE_OFFSET((BP_ABS_FUNC(bp) >> 1));
3613 
3614 		for (wait = 0; wait < BNX2X_UPDATE_DRV_INFO_IND_COUNT; wait++) {
3615 			u32 indication = SHMEM2_RD(bp, mfw_drv_indication);
3616 
3617 			/* Management is done; need to clear indication */
3618 			if (indication & bit) {
3619 				SHMEM2_WR(bp, mfw_drv_indication,
3620 					  indication & ~bit);
3621 				release = true;
3622 				break;
3623 			}
3624 
3625 			msleep(BNX2X_UPDATE_DRV_INFO_IND_LENGTH);
3626 		}
3627 	}
3628 	if (!release) {
3629 		DP(BNX2X_MSG_MCP, "Management did not release indication\n");
3630 		bp->drv_info_mng_owner = true;
3631 	}
3632 
3633 out:
3634 	mutex_unlock(&bp->drv_info_mutex);
3635 }
3636 
3637 static u32 bnx2x_update_mng_version_utility(u8 *version, bool bnx2x_format)
3638 {
3639 	u8 vals[4];
3640 	int i = 0;
3641 
3642 	if (bnx2x_format) {
3643 		i = sscanf(version, "1.%c%hhd.%hhd.%hhd",
3644 			   &vals[0], &vals[1], &vals[2], &vals[3]);
3645 		if (i > 0)
3646 			vals[0] -= '0';
3647 	} else {
3648 		i = sscanf(version, "%hhd.%hhd.%hhd.%hhd",
3649 			   &vals[0], &vals[1], &vals[2], &vals[3]);
3650 	}
3651 
3652 	while (i < 4)
3653 		vals[i++] = 0;
3654 
3655 	return (vals[0] << 24) | (vals[1] << 16) | (vals[2] << 8) | vals[3];
3656 }
3657 
3658 void bnx2x_update_mng_version(struct bnx2x *bp)
3659 {
3660 	u32 iscsiver = DRV_VER_NOT_LOADED;
3661 	u32 fcoever = DRV_VER_NOT_LOADED;
3662 	u32 ethver = DRV_VER_NOT_LOADED;
3663 	int idx = BP_FW_MB_IDX(bp);
3664 	u8 *version;
3665 
3666 	if (!SHMEM2_HAS(bp, func_os_drv_ver))
3667 		return;
3668 
3669 	mutex_lock(&bp->drv_info_mutex);
3670 	/* Must not proceed when `bnx2x_handle_drv_info_req' is feasible */
3671 	if (bp->drv_info_mng_owner)
3672 		goto out;
3673 
3674 	if (bp->state != BNX2X_STATE_OPEN)
3675 		goto out;
3676 
3677 	/* Parse ethernet driver version */
3678 	ethver = bnx2x_update_mng_version_utility(DRV_MODULE_VERSION, true);
3679 	if (!CNIC_LOADED(bp))
3680 		goto out;
3681 
3682 	/* Try getting storage driver version via cnic */
3683 	memset(&bp->slowpath->drv_info_to_mcp, 0,
3684 	       sizeof(union drv_info_to_mcp));
3685 	bnx2x_drv_info_iscsi_stat(bp);
3686 	version = bp->slowpath->drv_info_to_mcp.iscsi_stat.version;
3687 	iscsiver = bnx2x_update_mng_version_utility(version, false);
3688 
3689 	memset(&bp->slowpath->drv_info_to_mcp, 0,
3690 	       sizeof(union drv_info_to_mcp));
3691 	bnx2x_drv_info_fcoe_stat(bp);
3692 	version = bp->slowpath->drv_info_to_mcp.fcoe_stat.version;
3693 	fcoever = bnx2x_update_mng_version_utility(version, false);
3694 
3695 out:
3696 	SHMEM2_WR(bp, func_os_drv_ver[idx].versions[DRV_PERS_ETHERNET], ethver);
3697 	SHMEM2_WR(bp, func_os_drv_ver[idx].versions[DRV_PERS_ISCSI], iscsiver);
3698 	SHMEM2_WR(bp, func_os_drv_ver[idx].versions[DRV_PERS_FCOE], fcoever);
3699 
3700 	mutex_unlock(&bp->drv_info_mutex);
3701 
3702 	DP(BNX2X_MSG_MCP, "Setting driver version: ETH [%08x] iSCSI [%08x] FCoE [%08x]\n",
3703 	   ethver, iscsiver, fcoever);
3704 }
3705 
3706 void bnx2x_update_mfw_dump(struct bnx2x *bp)
3707 {
3708 	u32 drv_ver;
3709 	u32 valid_dump;
3710 
3711 	if (!SHMEM2_HAS(bp, drv_info))
3712 		return;
3713 
3714 	/* Update Driver load time, possibly broken in y2038 */
3715 	SHMEM2_WR(bp, drv_info.epoc, (u32)ktime_get_real_seconds());
3716 
3717 	drv_ver = bnx2x_update_mng_version_utility(DRV_MODULE_VERSION, true);
3718 	SHMEM2_WR(bp, drv_info.drv_ver, drv_ver);
3719 
3720 	SHMEM2_WR(bp, drv_info.fw_ver, REG_RD(bp, XSEM_REG_PRAM));
3721 
3722 	/* Check & notify On-Chip dump. */
3723 	valid_dump = SHMEM2_RD(bp, drv_info.valid_dump);
3724 
3725 	if (valid_dump & FIRST_DUMP_VALID)
3726 		DP(NETIF_MSG_IFUP, "A valid On-Chip MFW dump found on 1st partition\n");
3727 
3728 	if (valid_dump & SECOND_DUMP_VALID)
3729 		DP(NETIF_MSG_IFUP, "A valid On-Chip MFW dump found on 2nd partition\n");
3730 }
3731 
3732 static void bnx2x_oem_event(struct bnx2x *bp, u32 event)
3733 {
3734 	u32 cmd_ok, cmd_fail;
3735 
3736 	/* sanity */
3737 	if (event & DRV_STATUS_DCC_EVENT_MASK &&
3738 	    event & DRV_STATUS_OEM_EVENT_MASK) {
3739 		BNX2X_ERR("Received simultaneous events %08x\n", event);
3740 		return;
3741 	}
3742 
3743 	if (event & DRV_STATUS_DCC_EVENT_MASK) {
3744 		cmd_fail = DRV_MSG_CODE_DCC_FAILURE;
3745 		cmd_ok = DRV_MSG_CODE_DCC_OK;
3746 	} else /* if (event & DRV_STATUS_OEM_EVENT_MASK) */ {
3747 		cmd_fail = DRV_MSG_CODE_OEM_FAILURE;
3748 		cmd_ok = DRV_MSG_CODE_OEM_OK;
3749 	}
3750 
3751 	DP(BNX2X_MSG_MCP, "oem_event 0x%x\n", event);
3752 
3753 	if (event & (DRV_STATUS_DCC_DISABLE_ENABLE_PF |
3754 		     DRV_STATUS_OEM_DISABLE_ENABLE_PF)) {
3755 		/* This is the only place besides the function initialization
3756 		 * where the bp->flags can change so it is done without any
3757 		 * locks
3758 		 */
3759 		if (bp->mf_config[BP_VN(bp)] & FUNC_MF_CFG_FUNC_DISABLED) {
3760 			DP(BNX2X_MSG_MCP, "mf_cfg function disabled\n");
3761 			bp->flags |= MF_FUNC_DIS;
3762 
3763 			bnx2x_e1h_disable(bp);
3764 		} else {
3765 			DP(BNX2X_MSG_MCP, "mf_cfg function enabled\n");
3766 			bp->flags &= ~MF_FUNC_DIS;
3767 
3768 			bnx2x_e1h_enable(bp);
3769 		}
3770 		event &= ~(DRV_STATUS_DCC_DISABLE_ENABLE_PF |
3771 			   DRV_STATUS_OEM_DISABLE_ENABLE_PF);
3772 	}
3773 
3774 	if (event & (DRV_STATUS_DCC_BANDWIDTH_ALLOCATION |
3775 		     DRV_STATUS_OEM_BANDWIDTH_ALLOCATION)) {
3776 		bnx2x_config_mf_bw(bp);
3777 		event &= ~(DRV_STATUS_DCC_BANDWIDTH_ALLOCATION |
3778 			   DRV_STATUS_OEM_BANDWIDTH_ALLOCATION);
3779 	}
3780 
3781 	/* Report results to MCP */
3782 	if (event)
3783 		bnx2x_fw_command(bp, cmd_fail, 0);
3784 	else
3785 		bnx2x_fw_command(bp, cmd_ok, 0);
3786 }
3787 
3788 /* must be called under the spq lock */
3789 static struct eth_spe *bnx2x_sp_get_next(struct bnx2x *bp)
3790 {
3791 	struct eth_spe *next_spe = bp->spq_prod_bd;
3792 
3793 	if (bp->spq_prod_bd == bp->spq_last_bd) {
3794 		bp->spq_prod_bd = bp->spq;
3795 		bp->spq_prod_idx = 0;
3796 		DP(BNX2X_MSG_SP, "end of spq\n");
3797 	} else {
3798 		bp->spq_prod_bd++;
3799 		bp->spq_prod_idx++;
3800 	}
3801 	return next_spe;
3802 }
3803 
3804 /* must be called under the spq lock */
3805 static void bnx2x_sp_prod_update(struct bnx2x *bp)
3806 {
3807 	int func = BP_FUNC(bp);
3808 
3809 	/*
3810 	 * Make sure that BD data is updated before writing the producer:
3811 	 * BD data is written to the memory, the producer is read from the
3812 	 * memory, thus we need a full memory barrier to ensure the ordering.
3813 	 */
3814 	mb();
3815 
3816 	REG_WR16_RELAXED(bp, BAR_XSTRORM_INTMEM + XSTORM_SPQ_PROD_OFFSET(func),
3817 			 bp->spq_prod_idx);
3818 }
3819 
3820 /**
3821  * bnx2x_is_contextless_ramrod - check if the current command ends on EQ
3822  *
3823  * @cmd:	command to check
3824  * @cmd_type:	command type
3825  */
3826 static bool bnx2x_is_contextless_ramrod(int cmd, int cmd_type)
3827 {
3828 	if ((cmd_type == NONE_CONNECTION_TYPE) ||
3829 	    (cmd == RAMROD_CMD_ID_ETH_FORWARD_SETUP) ||
3830 	    (cmd == RAMROD_CMD_ID_ETH_CLASSIFICATION_RULES) ||
3831 	    (cmd == RAMROD_CMD_ID_ETH_FILTER_RULES) ||
3832 	    (cmd == RAMROD_CMD_ID_ETH_MULTICAST_RULES) ||
3833 	    (cmd == RAMROD_CMD_ID_ETH_SET_MAC) ||
3834 	    (cmd == RAMROD_CMD_ID_ETH_RSS_UPDATE))
3835 		return true;
3836 	else
3837 		return false;
3838 }
3839 
3840 /**
3841  * bnx2x_sp_post - place a single command on an SP ring
3842  *
3843  * @bp:		driver handle
3844  * @command:	command to place (e.g. SETUP, FILTER_RULES, etc.)
3845  * @cid:	SW CID the command is related to
3846  * @data_hi:	command private data address (high 32 bits)
3847  * @data_lo:	command private data address (low 32 bits)
3848  * @cmd_type:	command type (e.g. NONE, ETH)
3849  *
3850  * SP data is handled as if it's always an address pair, thus data fields are
3851  * not swapped to little endian in upper functions. Instead this function swaps
3852  * data as if it's two u32 fields.
3853  */
3854 int bnx2x_sp_post(struct bnx2x *bp, int command, int cid,
3855 		  u32 data_hi, u32 data_lo, int cmd_type)
3856 {
3857 	struct eth_spe *spe;
3858 	u16 type;
3859 	bool common = bnx2x_is_contextless_ramrod(command, cmd_type);
3860 
3861 #ifdef BNX2X_STOP_ON_ERROR
3862 	if (unlikely(bp->panic)) {
3863 		BNX2X_ERR("Can't post SP when there is panic\n");
3864 		return -EIO;
3865 	}
3866 #endif
3867 
3868 	spin_lock_bh(&bp->spq_lock);
3869 
3870 	if (common) {
3871 		if (!atomic_read(&bp->eq_spq_left)) {
3872 			BNX2X_ERR("BUG! EQ ring full!\n");
3873 			spin_unlock_bh(&bp->spq_lock);
3874 			bnx2x_panic();
3875 			return -EBUSY;
3876 		}
3877 	} else if (!atomic_read(&bp->cq_spq_left)) {
3878 			BNX2X_ERR("BUG! SPQ ring full!\n");
3879 			spin_unlock_bh(&bp->spq_lock);
3880 			bnx2x_panic();
3881 			return -EBUSY;
3882 	}
3883 
3884 	spe = bnx2x_sp_get_next(bp);
3885 
3886 	/* CID needs port number to be encoded int it */
3887 	spe->hdr.conn_and_cmd_data =
3888 			cpu_to_le32((command << SPE_HDR_CMD_ID_SHIFT) |
3889 				    HW_CID(bp, cid));
3890 
3891 	/* In some cases, type may already contain the func-id
3892 	 * mainly in SRIOV related use cases, so we add it here only
3893 	 * if it's not already set.
3894 	 */
3895 	if (!(cmd_type & SPE_HDR_FUNCTION_ID)) {
3896 		type = (cmd_type << SPE_HDR_CONN_TYPE_SHIFT) &
3897 			SPE_HDR_CONN_TYPE;
3898 		type |= ((BP_FUNC(bp) << SPE_HDR_FUNCTION_ID_SHIFT) &
3899 			 SPE_HDR_FUNCTION_ID);
3900 	} else {
3901 		type = cmd_type;
3902 	}
3903 
3904 	spe->hdr.type = cpu_to_le16(type);
3905 
3906 	spe->data.update_data_addr.hi = cpu_to_le32(data_hi);
3907 	spe->data.update_data_addr.lo = cpu_to_le32(data_lo);
3908 
3909 	/*
3910 	 * It's ok if the actual decrement is issued towards the memory
3911 	 * somewhere between the spin_lock and spin_unlock. Thus no
3912 	 * more explicit memory barrier is needed.
3913 	 */
3914 	if (common)
3915 		atomic_dec(&bp->eq_spq_left);
3916 	else
3917 		atomic_dec(&bp->cq_spq_left);
3918 
3919 	DP(BNX2X_MSG_SP,
3920 	   "SPQE[%x] (%x:%x)  (cmd, common?) (%d,%d)  hw_cid %x  data (%x:%x) type(0x%x) left (CQ, EQ) (%x,%x)\n",
3921 	   bp->spq_prod_idx, (u32)U64_HI(bp->spq_mapping),
3922 	   (u32)(U64_LO(bp->spq_mapping) +
3923 	   (void *)bp->spq_prod_bd - (void *)bp->spq), command, common,
3924 	   HW_CID(bp, cid), data_hi, data_lo, type,
3925 	   atomic_read(&bp->cq_spq_left), atomic_read(&bp->eq_spq_left));
3926 
3927 	bnx2x_sp_prod_update(bp);
3928 	spin_unlock_bh(&bp->spq_lock);
3929 	return 0;
3930 }
3931 
3932 /* acquire split MCP access lock register */
3933 static int bnx2x_acquire_alr(struct bnx2x *bp)
3934 {
3935 	u32 j, val;
3936 	int rc = 0;
3937 
3938 	might_sleep();
3939 	for (j = 0; j < 1000; j++) {
3940 		REG_WR(bp, MCP_REG_MCPR_ACCESS_LOCK, MCPR_ACCESS_LOCK_LOCK);
3941 		val = REG_RD(bp, MCP_REG_MCPR_ACCESS_LOCK);
3942 		if (val & MCPR_ACCESS_LOCK_LOCK)
3943 			break;
3944 
3945 		usleep_range(5000, 10000);
3946 	}
3947 	if (!(val & MCPR_ACCESS_LOCK_LOCK)) {
3948 		BNX2X_ERR("Cannot acquire MCP access lock register\n");
3949 		rc = -EBUSY;
3950 	}
3951 
3952 	return rc;
3953 }
3954 
3955 /* release split MCP access lock register */
3956 static void bnx2x_release_alr(struct bnx2x *bp)
3957 {
3958 	REG_WR(bp, MCP_REG_MCPR_ACCESS_LOCK, 0);
3959 }
3960 
3961 #define BNX2X_DEF_SB_ATT_IDX	0x0001
3962 #define BNX2X_DEF_SB_IDX	0x0002
3963 
3964 static u16 bnx2x_update_dsb_idx(struct bnx2x *bp)
3965 {
3966 	struct host_sp_status_block *def_sb = bp->def_status_blk;
3967 	u16 rc = 0;
3968 
3969 	barrier(); /* status block is written to by the chip */
3970 	if (bp->def_att_idx != def_sb->atten_status_block.attn_bits_index) {
3971 		bp->def_att_idx = def_sb->atten_status_block.attn_bits_index;
3972 		rc |= BNX2X_DEF_SB_ATT_IDX;
3973 	}
3974 
3975 	if (bp->def_idx != def_sb->sp_sb.running_index) {
3976 		bp->def_idx = def_sb->sp_sb.running_index;
3977 		rc |= BNX2X_DEF_SB_IDX;
3978 	}
3979 
3980 	/* Do not reorder: indices reading should complete before handling */
3981 	barrier();
3982 	return rc;
3983 }
3984 
3985 /*
3986  * slow path service functions
3987  */
3988 
3989 static void bnx2x_attn_int_asserted(struct bnx2x *bp, u32 asserted)
3990 {
3991 	int port = BP_PORT(bp);
3992 	u32 aeu_addr = port ? MISC_REG_AEU_MASK_ATTN_FUNC_1 :
3993 			      MISC_REG_AEU_MASK_ATTN_FUNC_0;
3994 	u32 nig_int_mask_addr = port ? NIG_REG_MASK_INTERRUPT_PORT1 :
3995 				       NIG_REG_MASK_INTERRUPT_PORT0;
3996 	u32 aeu_mask;
3997 	u32 nig_mask = 0;
3998 	u32 reg_addr;
3999 
4000 	if (bp->attn_state & asserted)
4001 		BNX2X_ERR("IGU ERROR\n");
4002 
4003 	bnx2x_acquire_hw_lock(bp, HW_LOCK_RESOURCE_PORT0_ATT_MASK + port);
4004 	aeu_mask = REG_RD(bp, aeu_addr);
4005 
4006 	DP(NETIF_MSG_HW, "aeu_mask %x  newly asserted %x\n",
4007 	   aeu_mask, asserted);
4008 	aeu_mask &= ~(asserted & 0x3ff);
4009 	DP(NETIF_MSG_HW, "new mask %x\n", aeu_mask);
4010 
4011 	REG_WR(bp, aeu_addr, aeu_mask);
4012 	bnx2x_release_hw_lock(bp, HW_LOCK_RESOURCE_PORT0_ATT_MASK + port);
4013 
4014 	DP(NETIF_MSG_HW, "attn_state %x\n", bp->attn_state);
4015 	bp->attn_state |= asserted;
4016 	DP(NETIF_MSG_HW, "new state %x\n", bp->attn_state);
4017 
4018 	if (asserted & ATTN_HARD_WIRED_MASK) {
4019 		if (asserted & ATTN_NIG_FOR_FUNC) {
4020 
4021 			bnx2x_acquire_phy_lock(bp);
4022 
4023 			/* save nig interrupt mask */
4024 			nig_mask = REG_RD(bp, nig_int_mask_addr);
4025 
4026 			/* If nig_mask is not set, no need to call the update
4027 			 * function.
4028 			 */
4029 			if (nig_mask) {
4030 				REG_WR(bp, nig_int_mask_addr, 0);
4031 
4032 				bnx2x_link_attn(bp);
4033 			}
4034 
4035 			/* handle unicore attn? */
4036 		}
4037 		if (asserted & ATTN_SW_TIMER_4_FUNC)
4038 			DP(NETIF_MSG_HW, "ATTN_SW_TIMER_4_FUNC!\n");
4039 
4040 		if (asserted & GPIO_2_FUNC)
4041 			DP(NETIF_MSG_HW, "GPIO_2_FUNC!\n");
4042 
4043 		if (asserted & GPIO_3_FUNC)
4044 			DP(NETIF_MSG_HW, "GPIO_3_FUNC!\n");
4045 
4046 		if (asserted & GPIO_4_FUNC)
4047 			DP(NETIF_MSG_HW, "GPIO_4_FUNC!\n");
4048 
4049 		if (port == 0) {
4050 			if (asserted & ATTN_GENERAL_ATTN_1) {
4051 				DP(NETIF_MSG_HW, "ATTN_GENERAL_ATTN_1!\n");
4052 				REG_WR(bp, MISC_REG_AEU_GENERAL_ATTN_1, 0x0);
4053 			}
4054 			if (asserted & ATTN_GENERAL_ATTN_2) {
4055 				DP(NETIF_MSG_HW, "ATTN_GENERAL_ATTN_2!\n");
4056 				REG_WR(bp, MISC_REG_AEU_GENERAL_ATTN_2, 0x0);
4057 			}
4058 			if (asserted & ATTN_GENERAL_ATTN_3) {
4059 				DP(NETIF_MSG_HW, "ATTN_GENERAL_ATTN_3!\n");
4060 				REG_WR(bp, MISC_REG_AEU_GENERAL_ATTN_3, 0x0);
4061 			}
4062 		} else {
4063 			if (asserted & ATTN_GENERAL_ATTN_4) {
4064 				DP(NETIF_MSG_HW, "ATTN_GENERAL_ATTN_4!\n");
4065 				REG_WR(bp, MISC_REG_AEU_GENERAL_ATTN_4, 0x0);
4066 			}
4067 			if (asserted & ATTN_GENERAL_ATTN_5) {
4068 				DP(NETIF_MSG_HW, "ATTN_GENERAL_ATTN_5!\n");
4069 				REG_WR(bp, MISC_REG_AEU_GENERAL_ATTN_5, 0x0);
4070 			}
4071 			if (asserted & ATTN_GENERAL_ATTN_6) {
4072 				DP(NETIF_MSG_HW, "ATTN_GENERAL_ATTN_6!\n");
4073 				REG_WR(bp, MISC_REG_AEU_GENERAL_ATTN_6, 0x0);
4074 			}
4075 		}
4076 
4077 	} /* if hardwired */
4078 
4079 	if (bp->common.int_block == INT_BLOCK_HC)
4080 		reg_addr = (HC_REG_COMMAND_REG + port*32 +
4081 			    COMMAND_REG_ATTN_BITS_SET);
4082 	else
4083 		reg_addr = (BAR_IGU_INTMEM + IGU_CMD_ATTN_BIT_SET_UPPER*8);
4084 
4085 	DP(NETIF_MSG_HW, "about to mask 0x%08x at %s addr 0x%x\n", asserted,
4086 	   (bp->common.int_block == INT_BLOCK_HC) ? "HC" : "IGU", reg_addr);
4087 	REG_WR(bp, reg_addr, asserted);
4088 
4089 	/* now set back the mask */
4090 	if (asserted & ATTN_NIG_FOR_FUNC) {
4091 		/* Verify that IGU ack through BAR was written before restoring
4092 		 * NIG mask. This loop should exit after 2-3 iterations max.
4093 		 */
4094 		if (bp->common.int_block != INT_BLOCK_HC) {
4095 			u32 cnt = 0, igu_acked;
4096 			do {
4097 				igu_acked = REG_RD(bp,
4098 						   IGU_REG_ATTENTION_ACK_BITS);
4099 			} while (((igu_acked & ATTN_NIG_FOR_FUNC) == 0) &&
4100 				 (++cnt < MAX_IGU_ATTN_ACK_TO));
4101 			if (!igu_acked)
4102 				DP(NETIF_MSG_HW,
4103 				   "Failed to verify IGU ack on time\n");
4104 			barrier();
4105 		}
4106 		REG_WR(bp, nig_int_mask_addr, nig_mask);
4107 		bnx2x_release_phy_lock(bp);
4108 	}
4109 }
4110 
4111 static void bnx2x_fan_failure(struct bnx2x *bp)
4112 {
4113 	int port = BP_PORT(bp);
4114 	u32 ext_phy_config;
4115 	/* mark the failure */
4116 	ext_phy_config =
4117 		SHMEM_RD(bp,
4118 			 dev_info.port_hw_config[port].external_phy_config);
4119 
4120 	ext_phy_config &= ~PORT_HW_CFG_XGXS_EXT_PHY_TYPE_MASK;
4121 	ext_phy_config |= PORT_HW_CFG_XGXS_EXT_PHY_TYPE_FAILURE;
4122 	SHMEM_WR(bp, dev_info.port_hw_config[port].external_phy_config,
4123 		 ext_phy_config);
4124 
4125 	/* log the failure */
4126 	netdev_err(bp->dev, "Fan Failure on Network Controller has caused the driver to shutdown the card to prevent permanent damage.\n"
4127 			    "Please contact OEM Support for assistance\n");
4128 
4129 	/* Schedule device reset (unload)
4130 	 * This is due to some boards consuming sufficient power when driver is
4131 	 * up to overheat if fan fails.
4132 	 */
4133 	bnx2x_schedule_sp_rtnl(bp, BNX2X_SP_RTNL_FAN_FAILURE, 0);
4134 }
4135 
4136 static void bnx2x_attn_int_deasserted0(struct bnx2x *bp, u32 attn)
4137 {
4138 	int port = BP_PORT(bp);
4139 	int reg_offset;
4140 	u32 val;
4141 
4142 	reg_offset = (port ? MISC_REG_AEU_ENABLE1_FUNC_1_OUT_0 :
4143 			     MISC_REG_AEU_ENABLE1_FUNC_0_OUT_0);
4144 
4145 	if (attn & AEU_INPUTS_ATTN_BITS_SPIO5) {
4146 
4147 		val = REG_RD(bp, reg_offset);
4148 		val &= ~AEU_INPUTS_ATTN_BITS_SPIO5;
4149 		REG_WR(bp, reg_offset, val);
4150 
4151 		BNX2X_ERR("SPIO5 hw attention\n");
4152 
4153 		/* Fan failure attention */
4154 		bnx2x_hw_reset_phy(&bp->link_params);
4155 		bnx2x_fan_failure(bp);
4156 	}
4157 
4158 	if ((attn & bp->link_vars.aeu_int_mask) && bp->port.pmf) {
4159 		bnx2x_acquire_phy_lock(bp);
4160 		bnx2x_handle_module_detect_int(&bp->link_params);
4161 		bnx2x_release_phy_lock(bp);
4162 	}
4163 
4164 	if (attn & HW_INTERRUPT_ASSERT_SET_0) {
4165 
4166 		val = REG_RD(bp, reg_offset);
4167 		val &= ~(attn & HW_INTERRUPT_ASSERT_SET_0);
4168 		REG_WR(bp, reg_offset, val);
4169 
4170 		BNX2X_ERR("FATAL HW block attention set0 0x%x\n",
4171 			  (u32)(attn & HW_INTERRUPT_ASSERT_SET_0));
4172 		bnx2x_panic();
4173 	}
4174 }
4175 
4176 static void bnx2x_attn_int_deasserted1(struct bnx2x *bp, u32 attn)
4177 {
4178 	u32 val;
4179 
4180 	if (attn & AEU_INPUTS_ATTN_BITS_DOORBELLQ_HW_INTERRUPT) {
4181 
4182 		val = REG_RD(bp, DORQ_REG_DORQ_INT_STS_CLR);
4183 		BNX2X_ERR("DB hw attention 0x%x\n", val);
4184 		/* DORQ discard attention */
4185 		if (val & 0x2)
4186 			BNX2X_ERR("FATAL error from DORQ\n");
4187 	}
4188 
4189 	if (attn & HW_INTERRUPT_ASSERT_SET_1) {
4190 
4191 		int port = BP_PORT(bp);
4192 		int reg_offset;
4193 
4194 		reg_offset = (port ? MISC_REG_AEU_ENABLE1_FUNC_1_OUT_1 :
4195 				     MISC_REG_AEU_ENABLE1_FUNC_0_OUT_1);
4196 
4197 		val = REG_RD(bp, reg_offset);
4198 		val &= ~(attn & HW_INTERRUPT_ASSERT_SET_1);
4199 		REG_WR(bp, reg_offset, val);
4200 
4201 		BNX2X_ERR("FATAL HW block attention set1 0x%x\n",
4202 			  (u32)(attn & HW_INTERRUPT_ASSERT_SET_1));
4203 		bnx2x_panic();
4204 	}
4205 }
4206 
4207 static void bnx2x_attn_int_deasserted2(struct bnx2x *bp, u32 attn)
4208 {
4209 	u32 val;
4210 
4211 	if (attn & AEU_INPUTS_ATTN_BITS_CFC_HW_INTERRUPT) {
4212 
4213 		val = REG_RD(bp, CFC_REG_CFC_INT_STS_CLR);
4214 		BNX2X_ERR("CFC hw attention 0x%x\n", val);
4215 		/* CFC error attention */
4216 		if (val & 0x2)
4217 			BNX2X_ERR("FATAL error from CFC\n");
4218 	}
4219 
4220 	if (attn & AEU_INPUTS_ATTN_BITS_PXP_HW_INTERRUPT) {
4221 		val = REG_RD(bp, PXP_REG_PXP_INT_STS_CLR_0);
4222 		BNX2X_ERR("PXP hw attention-0 0x%x\n", val);
4223 		/* RQ_USDMDP_FIFO_OVERFLOW */
4224 		if (val & 0x18000)
4225 			BNX2X_ERR("FATAL error from PXP\n");
4226 
4227 		if (!CHIP_IS_E1x(bp)) {
4228 			val = REG_RD(bp, PXP_REG_PXP_INT_STS_CLR_1);
4229 			BNX2X_ERR("PXP hw attention-1 0x%x\n", val);
4230 		}
4231 	}
4232 
4233 	if (attn & HW_INTERRUPT_ASSERT_SET_2) {
4234 
4235 		int port = BP_PORT(bp);
4236 		int reg_offset;
4237 
4238 		reg_offset = (port ? MISC_REG_AEU_ENABLE1_FUNC_1_OUT_2 :
4239 				     MISC_REG_AEU_ENABLE1_FUNC_0_OUT_2);
4240 
4241 		val = REG_RD(bp, reg_offset);
4242 		val &= ~(attn & HW_INTERRUPT_ASSERT_SET_2);
4243 		REG_WR(bp, reg_offset, val);
4244 
4245 		BNX2X_ERR("FATAL HW block attention set2 0x%x\n",
4246 			  (u32)(attn & HW_INTERRUPT_ASSERT_SET_2));
4247 		bnx2x_panic();
4248 	}
4249 }
4250 
4251 static void bnx2x_attn_int_deasserted3(struct bnx2x *bp, u32 attn)
4252 {
4253 	u32 val;
4254 
4255 	if (attn & EVEREST_GEN_ATTN_IN_USE_MASK) {
4256 
4257 		if (attn & BNX2X_PMF_LINK_ASSERT) {
4258 			int func = BP_FUNC(bp);
4259 
4260 			REG_WR(bp, MISC_REG_AEU_GENERAL_ATTN_12 + func*4, 0);
4261 			bnx2x_read_mf_cfg(bp);
4262 			bp->mf_config[BP_VN(bp)] = MF_CFG_RD(bp,
4263 					func_mf_config[BP_ABS_FUNC(bp)].config);
4264 			val = SHMEM_RD(bp,
4265 				       func_mb[BP_FW_MB_IDX(bp)].drv_status);
4266 
4267 			if (val & (DRV_STATUS_DCC_EVENT_MASK |
4268 				   DRV_STATUS_OEM_EVENT_MASK))
4269 				bnx2x_oem_event(bp,
4270 					(val & (DRV_STATUS_DCC_EVENT_MASK |
4271 						DRV_STATUS_OEM_EVENT_MASK)));
4272 
4273 			if (val & DRV_STATUS_SET_MF_BW)
4274 				bnx2x_set_mf_bw(bp);
4275 
4276 			if (val & DRV_STATUS_DRV_INFO_REQ)
4277 				bnx2x_handle_drv_info_req(bp);
4278 
4279 			if (val & DRV_STATUS_VF_DISABLED)
4280 				bnx2x_schedule_iov_task(bp,
4281 							BNX2X_IOV_HANDLE_FLR);
4282 
4283 			if ((bp->port.pmf == 0) && (val & DRV_STATUS_PMF))
4284 				bnx2x_pmf_update(bp);
4285 
4286 			if (bp->port.pmf &&
4287 			    (val & DRV_STATUS_DCBX_NEGOTIATION_RESULTS) &&
4288 				bp->dcbx_enabled > 0)
4289 				/* start dcbx state machine */
4290 				bnx2x_dcbx_set_params(bp,
4291 					BNX2X_DCBX_STATE_NEG_RECEIVED);
4292 			if (val & DRV_STATUS_AFEX_EVENT_MASK)
4293 				bnx2x_handle_afex_cmd(bp,
4294 					val & DRV_STATUS_AFEX_EVENT_MASK);
4295 			if (val & DRV_STATUS_EEE_NEGOTIATION_RESULTS)
4296 				bnx2x_handle_eee_event(bp);
4297 
4298 			if (val & DRV_STATUS_OEM_UPDATE_SVID)
4299 				bnx2x_schedule_sp_rtnl(bp,
4300 					BNX2X_SP_RTNL_UPDATE_SVID, 0);
4301 
4302 			if (bp->link_vars.periodic_flags &
4303 			    PERIODIC_FLAGS_LINK_EVENT) {
4304 				/*  sync with link */
4305 				bnx2x_acquire_phy_lock(bp);
4306 				bp->link_vars.periodic_flags &=
4307 					~PERIODIC_FLAGS_LINK_EVENT;
4308 				bnx2x_release_phy_lock(bp);
4309 				if (IS_MF(bp))
4310 					bnx2x_link_sync_notify(bp);
4311 				bnx2x_link_report(bp);
4312 			}
4313 			/* Always call it here: bnx2x_link_report() will
4314 			 * prevent the link indication duplication.
4315 			 */
4316 			bnx2x__link_status_update(bp);
4317 		} else if (attn & BNX2X_MC_ASSERT_BITS) {
4318 
4319 			BNX2X_ERR("MC assert!\n");
4320 			bnx2x_mc_assert(bp);
4321 			REG_WR(bp, MISC_REG_AEU_GENERAL_ATTN_10, 0);
4322 			REG_WR(bp, MISC_REG_AEU_GENERAL_ATTN_9, 0);
4323 			REG_WR(bp, MISC_REG_AEU_GENERAL_ATTN_8, 0);
4324 			REG_WR(bp, MISC_REG_AEU_GENERAL_ATTN_7, 0);
4325 			bnx2x_panic();
4326 
4327 		} else if (attn & BNX2X_MCP_ASSERT) {
4328 
4329 			BNX2X_ERR("MCP assert!\n");
4330 			REG_WR(bp, MISC_REG_AEU_GENERAL_ATTN_11, 0);
4331 			bnx2x_fw_dump(bp);
4332 
4333 		} else
4334 			BNX2X_ERR("Unknown HW assert! (attn 0x%x)\n", attn);
4335 	}
4336 
4337 	if (attn & EVEREST_LATCHED_ATTN_IN_USE_MASK) {
4338 		BNX2X_ERR("LATCHED attention 0x%08x (masked)\n", attn);
4339 		if (attn & BNX2X_GRC_TIMEOUT) {
4340 			val = CHIP_IS_E1(bp) ? 0 :
4341 					REG_RD(bp, MISC_REG_GRC_TIMEOUT_ATTN);
4342 			BNX2X_ERR("GRC time-out 0x%08x\n", val);
4343 		}
4344 		if (attn & BNX2X_GRC_RSV) {
4345 			val = CHIP_IS_E1(bp) ? 0 :
4346 					REG_RD(bp, MISC_REG_GRC_RSV_ATTN);
4347 			BNX2X_ERR("GRC reserved 0x%08x\n", val);
4348 		}
4349 		REG_WR(bp, MISC_REG_AEU_CLR_LATCH_SIGNAL, 0x7ff);
4350 	}
4351 }
4352 
4353 /*
4354  * Bits map:
4355  * 0-7   - Engine0 load counter.
4356  * 8-15  - Engine1 load counter.
4357  * 16    - Engine0 RESET_IN_PROGRESS bit.
4358  * 17    - Engine1 RESET_IN_PROGRESS bit.
4359  * 18    - Engine0 ONE_IS_LOADED. Set when there is at least one active function
4360  *         on the engine
4361  * 19    - Engine1 ONE_IS_LOADED.
4362  * 20    - Chip reset flow bit. When set none-leader must wait for both engines
4363  *         leader to complete (check for both RESET_IN_PROGRESS bits and not for
4364  *         just the one belonging to its engine).
4365  *
4366  */
4367 #define BNX2X_RECOVERY_GLOB_REG		MISC_REG_GENERIC_POR_1
4368 
4369 #define BNX2X_PATH0_LOAD_CNT_MASK	0x000000ff
4370 #define BNX2X_PATH0_LOAD_CNT_SHIFT	0
4371 #define BNX2X_PATH1_LOAD_CNT_MASK	0x0000ff00
4372 #define BNX2X_PATH1_LOAD_CNT_SHIFT	8
4373 #define BNX2X_PATH0_RST_IN_PROG_BIT	0x00010000
4374 #define BNX2X_PATH1_RST_IN_PROG_BIT	0x00020000
4375 #define BNX2X_GLOBAL_RESET_BIT		0x00040000
4376 
4377 /*
4378  * Set the GLOBAL_RESET bit.
4379  *
4380  * Should be run under rtnl lock
4381  */
4382 void bnx2x_set_reset_global(struct bnx2x *bp)
4383 {
4384 	u32 val;
4385 	bnx2x_acquire_hw_lock(bp, HW_LOCK_RESOURCE_RECOVERY_REG);
4386 	val = REG_RD(bp, BNX2X_RECOVERY_GLOB_REG);
4387 	REG_WR(bp, BNX2X_RECOVERY_GLOB_REG, val | BNX2X_GLOBAL_RESET_BIT);
4388 	bnx2x_release_hw_lock(bp, HW_LOCK_RESOURCE_RECOVERY_REG);
4389 }
4390 
4391 /*
4392  * Clear the GLOBAL_RESET bit.
4393  *
4394  * Should be run under rtnl lock
4395  */
4396 static void bnx2x_clear_reset_global(struct bnx2x *bp)
4397 {
4398 	u32 val;
4399 	bnx2x_acquire_hw_lock(bp, HW_LOCK_RESOURCE_RECOVERY_REG);
4400 	val = REG_RD(bp, BNX2X_RECOVERY_GLOB_REG);
4401 	REG_WR(bp, BNX2X_RECOVERY_GLOB_REG, val & (~BNX2X_GLOBAL_RESET_BIT));
4402 	bnx2x_release_hw_lock(bp, HW_LOCK_RESOURCE_RECOVERY_REG);
4403 }
4404 
4405 /*
4406  * Checks the GLOBAL_RESET bit.
4407  *
4408  * should be run under rtnl lock
4409  */
4410 static bool bnx2x_reset_is_global(struct bnx2x *bp)
4411 {
4412 	u32 val = REG_RD(bp, BNX2X_RECOVERY_GLOB_REG);
4413 
4414 	DP(NETIF_MSG_HW, "GEN_REG_VAL=0x%08x\n", val);
4415 	return (val & BNX2X_GLOBAL_RESET_BIT) ? true : false;
4416 }
4417 
4418 /*
4419  * Clear RESET_IN_PROGRESS bit for the current engine.
4420  *
4421  * Should be run under rtnl lock
4422  */
4423 static void bnx2x_set_reset_done(struct bnx2x *bp)
4424 {
4425 	u32 val;
4426 	u32 bit = BP_PATH(bp) ?
4427 		BNX2X_PATH1_RST_IN_PROG_BIT : BNX2X_PATH0_RST_IN_PROG_BIT;
4428 	bnx2x_acquire_hw_lock(bp, HW_LOCK_RESOURCE_RECOVERY_REG);
4429 	val = REG_RD(bp, BNX2X_RECOVERY_GLOB_REG);
4430 
4431 	/* Clear the bit */
4432 	val &= ~bit;
4433 	REG_WR(bp, BNX2X_RECOVERY_GLOB_REG, val);
4434 
4435 	bnx2x_release_hw_lock(bp, HW_LOCK_RESOURCE_RECOVERY_REG);
4436 }
4437 
4438 /*
4439  * Set RESET_IN_PROGRESS for the current engine.
4440  *
4441  * should be run under rtnl lock
4442  */
4443 void bnx2x_set_reset_in_progress(struct bnx2x *bp)
4444 {
4445 	u32 val;
4446 	u32 bit = BP_PATH(bp) ?
4447 		BNX2X_PATH1_RST_IN_PROG_BIT : BNX2X_PATH0_RST_IN_PROG_BIT;
4448 	bnx2x_acquire_hw_lock(bp, HW_LOCK_RESOURCE_RECOVERY_REG);
4449 	val = REG_RD(bp, BNX2X_RECOVERY_GLOB_REG);
4450 
4451 	/* Set the bit */
4452 	val |= bit;
4453 	REG_WR(bp, BNX2X_RECOVERY_GLOB_REG, val);
4454 	bnx2x_release_hw_lock(bp, HW_LOCK_RESOURCE_RECOVERY_REG);
4455 }
4456 
4457 /*
4458  * Checks the RESET_IN_PROGRESS bit for the given engine.
4459  * should be run under rtnl lock
4460  */
4461 bool bnx2x_reset_is_done(struct bnx2x *bp, int engine)
4462 {
4463 	u32 val = REG_RD(bp, BNX2X_RECOVERY_GLOB_REG);
4464 	u32 bit = engine ?
4465 		BNX2X_PATH1_RST_IN_PROG_BIT : BNX2X_PATH0_RST_IN_PROG_BIT;
4466 
4467 	/* return false if bit is set */
4468 	return (val & bit) ? false : true;
4469 }
4470 
4471 /*
4472  * set pf load for the current pf.
4473  *
4474  * should be run under rtnl lock
4475  */
4476 void bnx2x_set_pf_load(struct bnx2x *bp)
4477 {
4478 	u32 val1, val;
4479 	u32 mask = BP_PATH(bp) ? BNX2X_PATH1_LOAD_CNT_MASK :
4480 			     BNX2X_PATH0_LOAD_CNT_MASK;
4481 	u32 shift = BP_PATH(bp) ? BNX2X_PATH1_LOAD_CNT_SHIFT :
4482 			     BNX2X_PATH0_LOAD_CNT_SHIFT;
4483 
4484 	bnx2x_acquire_hw_lock(bp, HW_LOCK_RESOURCE_RECOVERY_REG);
4485 	val = REG_RD(bp, BNX2X_RECOVERY_GLOB_REG);
4486 
4487 	DP(NETIF_MSG_IFUP, "Old GEN_REG_VAL=0x%08x\n", val);
4488 
4489 	/* get the current counter value */
4490 	val1 = (val & mask) >> shift;
4491 
4492 	/* set bit of that PF */
4493 	val1 |= (1 << bp->pf_num);
4494 
4495 	/* clear the old value */
4496 	val &= ~mask;
4497 
4498 	/* set the new one */
4499 	val |= ((val1 << shift) & mask);
4500 
4501 	REG_WR(bp, BNX2X_RECOVERY_GLOB_REG, val);
4502 	bnx2x_release_hw_lock(bp, HW_LOCK_RESOURCE_RECOVERY_REG);
4503 }
4504 
4505 /**
4506  * bnx2x_clear_pf_load - clear pf load mark
4507  *
4508  * @bp:		driver handle
4509  *
4510  * Should be run under rtnl lock.
4511  * Decrements the load counter for the current engine. Returns
4512  * whether other functions are still loaded
4513  */
4514 bool bnx2x_clear_pf_load(struct bnx2x *bp)
4515 {
4516 	u32 val1, val;
4517 	u32 mask = BP_PATH(bp) ? BNX2X_PATH1_LOAD_CNT_MASK :
4518 			     BNX2X_PATH0_LOAD_CNT_MASK;
4519 	u32 shift = BP_PATH(bp) ? BNX2X_PATH1_LOAD_CNT_SHIFT :
4520 			     BNX2X_PATH0_LOAD_CNT_SHIFT;
4521 
4522 	bnx2x_acquire_hw_lock(bp, HW_LOCK_RESOURCE_RECOVERY_REG);
4523 	val = REG_RD(bp, BNX2X_RECOVERY_GLOB_REG);
4524 	DP(NETIF_MSG_IFDOWN, "Old GEN_REG_VAL=0x%08x\n", val);
4525 
4526 	/* get the current counter value */
4527 	val1 = (val & mask) >> shift;
4528 
4529 	/* clear bit of that PF */
4530 	val1 &= ~(1 << bp->pf_num);
4531 
4532 	/* clear the old value */
4533 	val &= ~mask;
4534 
4535 	/* set the new one */
4536 	val |= ((val1 << shift) & mask);
4537 
4538 	REG_WR(bp, BNX2X_RECOVERY_GLOB_REG, val);
4539 	bnx2x_release_hw_lock(bp, HW_LOCK_RESOURCE_RECOVERY_REG);
4540 	return val1 != 0;
4541 }
4542 
4543 /*
4544  * Read the load status for the current engine.
4545  *
4546  * should be run under rtnl lock
4547  */
4548 static bool bnx2x_get_load_status(struct bnx2x *bp, int engine)
4549 {
4550 	u32 mask = (engine ? BNX2X_PATH1_LOAD_CNT_MASK :
4551 			     BNX2X_PATH0_LOAD_CNT_MASK);
4552 	u32 shift = (engine ? BNX2X_PATH1_LOAD_CNT_SHIFT :
4553 			     BNX2X_PATH0_LOAD_CNT_SHIFT);
4554 	u32 val = REG_RD(bp, BNX2X_RECOVERY_GLOB_REG);
4555 
4556 	DP(NETIF_MSG_HW | NETIF_MSG_IFUP, "GLOB_REG=0x%08x\n", val);
4557 
4558 	val = (val & mask) >> shift;
4559 
4560 	DP(NETIF_MSG_HW | NETIF_MSG_IFUP, "load mask for engine %d = 0x%x\n",
4561 	   engine, val);
4562 
4563 	return val != 0;
4564 }
4565 
4566 static void _print_parity(struct bnx2x *bp, u32 reg)
4567 {
4568 	pr_cont(" [0x%08x] ", REG_RD(bp, reg));
4569 }
4570 
4571 static void _print_next_block(int idx, const char *blk)
4572 {
4573 	pr_cont("%s%s", idx ? ", " : "", blk);
4574 }
4575 
4576 static bool bnx2x_check_blocks_with_parity0(struct bnx2x *bp, u32 sig,
4577 					    int *par_num, bool print)
4578 {
4579 	u32 cur_bit;
4580 	bool res;
4581 	int i;
4582 
4583 	res = false;
4584 
4585 	for (i = 0; sig; i++) {
4586 		cur_bit = (0x1UL << i);
4587 		if (sig & cur_bit) {
4588 			res |= true; /* Each bit is real error! */
4589 
4590 			if (print) {
4591 				switch (cur_bit) {
4592 				case AEU_INPUTS_ATTN_BITS_BRB_PARITY_ERROR:
4593 					_print_next_block((*par_num)++, "BRB");
4594 					_print_parity(bp,
4595 						      BRB1_REG_BRB1_PRTY_STS);
4596 					break;
4597 				case AEU_INPUTS_ATTN_BITS_PARSER_PARITY_ERROR:
4598 					_print_next_block((*par_num)++,
4599 							  "PARSER");
4600 					_print_parity(bp, PRS_REG_PRS_PRTY_STS);
4601 					break;
4602 				case AEU_INPUTS_ATTN_BITS_TSDM_PARITY_ERROR:
4603 					_print_next_block((*par_num)++, "TSDM");
4604 					_print_parity(bp,
4605 						      TSDM_REG_TSDM_PRTY_STS);
4606 					break;
4607 				case AEU_INPUTS_ATTN_BITS_SEARCHER_PARITY_ERROR:
4608 					_print_next_block((*par_num)++,
4609 							  "SEARCHER");
4610 					_print_parity(bp, SRC_REG_SRC_PRTY_STS);
4611 					break;
4612 				case AEU_INPUTS_ATTN_BITS_TCM_PARITY_ERROR:
4613 					_print_next_block((*par_num)++, "TCM");
4614 					_print_parity(bp, TCM_REG_TCM_PRTY_STS);
4615 					break;
4616 				case AEU_INPUTS_ATTN_BITS_TSEMI_PARITY_ERROR:
4617 					_print_next_block((*par_num)++,
4618 							  "TSEMI");
4619 					_print_parity(bp,
4620 						      TSEM_REG_TSEM_PRTY_STS_0);
4621 					_print_parity(bp,
4622 						      TSEM_REG_TSEM_PRTY_STS_1);
4623 					break;
4624 				case AEU_INPUTS_ATTN_BITS_PBCLIENT_PARITY_ERROR:
4625 					_print_next_block((*par_num)++, "XPB");
4626 					_print_parity(bp, GRCBASE_XPB +
4627 							  PB_REG_PB_PRTY_STS);
4628 					break;
4629 				}
4630 			}
4631 
4632 			/* Clear the bit */
4633 			sig &= ~cur_bit;
4634 		}
4635 	}
4636 
4637 	return res;
4638 }
4639 
4640 static bool bnx2x_check_blocks_with_parity1(struct bnx2x *bp, u32 sig,
4641 					    int *par_num, bool *global,
4642 					    bool print)
4643 {
4644 	u32 cur_bit;
4645 	bool res;
4646 	int i;
4647 
4648 	res = false;
4649 
4650 	for (i = 0; sig; i++) {
4651 		cur_bit = (0x1UL << i);
4652 		if (sig & cur_bit) {
4653 			res |= true; /* Each bit is real error! */
4654 			switch (cur_bit) {
4655 			case AEU_INPUTS_ATTN_BITS_PBF_PARITY_ERROR:
4656 				if (print) {
4657 					_print_next_block((*par_num)++, "PBF");
4658 					_print_parity(bp, PBF_REG_PBF_PRTY_STS);
4659 				}
4660 				break;
4661 			case AEU_INPUTS_ATTN_BITS_QM_PARITY_ERROR:
4662 				if (print) {
4663 					_print_next_block((*par_num)++, "QM");
4664 					_print_parity(bp, QM_REG_QM_PRTY_STS);
4665 				}
4666 				break;
4667 			case AEU_INPUTS_ATTN_BITS_TIMERS_PARITY_ERROR:
4668 				if (print) {
4669 					_print_next_block((*par_num)++, "TM");
4670 					_print_parity(bp, TM_REG_TM_PRTY_STS);
4671 				}
4672 				break;
4673 			case AEU_INPUTS_ATTN_BITS_XSDM_PARITY_ERROR:
4674 				if (print) {
4675 					_print_next_block((*par_num)++, "XSDM");
4676 					_print_parity(bp,
4677 						      XSDM_REG_XSDM_PRTY_STS);
4678 				}
4679 				break;
4680 			case AEU_INPUTS_ATTN_BITS_XCM_PARITY_ERROR:
4681 				if (print) {
4682 					_print_next_block((*par_num)++, "XCM");
4683 					_print_parity(bp, XCM_REG_XCM_PRTY_STS);
4684 				}
4685 				break;
4686 			case AEU_INPUTS_ATTN_BITS_XSEMI_PARITY_ERROR:
4687 				if (print) {
4688 					_print_next_block((*par_num)++,
4689 							  "XSEMI");
4690 					_print_parity(bp,
4691 						      XSEM_REG_XSEM_PRTY_STS_0);
4692 					_print_parity(bp,
4693 						      XSEM_REG_XSEM_PRTY_STS_1);
4694 				}
4695 				break;
4696 			case AEU_INPUTS_ATTN_BITS_DOORBELLQ_PARITY_ERROR:
4697 				if (print) {
4698 					_print_next_block((*par_num)++,
4699 							  "DOORBELLQ");
4700 					_print_parity(bp,
4701 						      DORQ_REG_DORQ_PRTY_STS);
4702 				}
4703 				break;
4704 			case AEU_INPUTS_ATTN_BITS_NIG_PARITY_ERROR:
4705 				if (print) {
4706 					_print_next_block((*par_num)++, "NIG");
4707 					if (CHIP_IS_E1x(bp)) {
4708 						_print_parity(bp,
4709 							NIG_REG_NIG_PRTY_STS);
4710 					} else {
4711 						_print_parity(bp,
4712 							NIG_REG_NIG_PRTY_STS_0);
4713 						_print_parity(bp,
4714 							NIG_REG_NIG_PRTY_STS_1);
4715 					}
4716 				}
4717 				break;
4718 			case AEU_INPUTS_ATTN_BITS_VAUX_PCI_CORE_PARITY_ERROR:
4719 				if (print)
4720 					_print_next_block((*par_num)++,
4721 							  "VAUX PCI CORE");
4722 				*global = true;
4723 				break;
4724 			case AEU_INPUTS_ATTN_BITS_DEBUG_PARITY_ERROR:
4725 				if (print) {
4726 					_print_next_block((*par_num)++,
4727 							  "DEBUG");
4728 					_print_parity(bp, DBG_REG_DBG_PRTY_STS);
4729 				}
4730 				break;
4731 			case AEU_INPUTS_ATTN_BITS_USDM_PARITY_ERROR:
4732 				if (print) {
4733 					_print_next_block((*par_num)++, "USDM");
4734 					_print_parity(bp,
4735 						      USDM_REG_USDM_PRTY_STS);
4736 				}
4737 				break;
4738 			case AEU_INPUTS_ATTN_BITS_UCM_PARITY_ERROR:
4739 				if (print) {
4740 					_print_next_block((*par_num)++, "UCM");
4741 					_print_parity(bp, UCM_REG_UCM_PRTY_STS);
4742 				}
4743 				break;
4744 			case AEU_INPUTS_ATTN_BITS_USEMI_PARITY_ERROR:
4745 				if (print) {
4746 					_print_next_block((*par_num)++,
4747 							  "USEMI");
4748 					_print_parity(bp,
4749 						      USEM_REG_USEM_PRTY_STS_0);
4750 					_print_parity(bp,
4751 						      USEM_REG_USEM_PRTY_STS_1);
4752 				}
4753 				break;
4754 			case AEU_INPUTS_ATTN_BITS_UPB_PARITY_ERROR:
4755 				if (print) {
4756 					_print_next_block((*par_num)++, "UPB");
4757 					_print_parity(bp, GRCBASE_UPB +
4758 							  PB_REG_PB_PRTY_STS);
4759 				}
4760 				break;
4761 			case AEU_INPUTS_ATTN_BITS_CSDM_PARITY_ERROR:
4762 				if (print) {
4763 					_print_next_block((*par_num)++, "CSDM");
4764 					_print_parity(bp,
4765 						      CSDM_REG_CSDM_PRTY_STS);
4766 				}
4767 				break;
4768 			case AEU_INPUTS_ATTN_BITS_CCM_PARITY_ERROR:
4769 				if (print) {
4770 					_print_next_block((*par_num)++, "CCM");
4771 					_print_parity(bp, CCM_REG_CCM_PRTY_STS);
4772 				}
4773 				break;
4774 			}
4775 
4776 			/* Clear the bit */
4777 			sig &= ~cur_bit;
4778 		}
4779 	}
4780 
4781 	return res;
4782 }
4783 
4784 static bool bnx2x_check_blocks_with_parity2(struct bnx2x *bp, u32 sig,
4785 					    int *par_num, bool print)
4786 {
4787 	u32 cur_bit;
4788 	bool res;
4789 	int i;
4790 
4791 	res = false;
4792 
4793 	for (i = 0; sig; i++) {
4794 		cur_bit = (0x1UL << i);
4795 		if (sig & cur_bit) {
4796 			res = true; /* Each bit is real error! */
4797 			if (print) {
4798 				switch (cur_bit) {
4799 				case AEU_INPUTS_ATTN_BITS_CSEMI_PARITY_ERROR:
4800 					_print_next_block((*par_num)++,
4801 							  "CSEMI");
4802 					_print_parity(bp,
4803 						      CSEM_REG_CSEM_PRTY_STS_0);
4804 					_print_parity(bp,
4805 						      CSEM_REG_CSEM_PRTY_STS_1);
4806 					break;
4807 				case AEU_INPUTS_ATTN_BITS_PXP_PARITY_ERROR:
4808 					_print_next_block((*par_num)++, "PXP");
4809 					_print_parity(bp, PXP_REG_PXP_PRTY_STS);
4810 					_print_parity(bp,
4811 						      PXP2_REG_PXP2_PRTY_STS_0);
4812 					_print_parity(bp,
4813 						      PXP2_REG_PXP2_PRTY_STS_1);
4814 					break;
4815 				case AEU_IN_ATTN_BITS_PXPPCICLOCKCLIENT_PARITY_ERROR:
4816 					_print_next_block((*par_num)++,
4817 							  "PXPPCICLOCKCLIENT");
4818 					break;
4819 				case AEU_INPUTS_ATTN_BITS_CFC_PARITY_ERROR:
4820 					_print_next_block((*par_num)++, "CFC");
4821 					_print_parity(bp,
4822 						      CFC_REG_CFC_PRTY_STS);
4823 					break;
4824 				case AEU_INPUTS_ATTN_BITS_CDU_PARITY_ERROR:
4825 					_print_next_block((*par_num)++, "CDU");
4826 					_print_parity(bp, CDU_REG_CDU_PRTY_STS);
4827 					break;
4828 				case AEU_INPUTS_ATTN_BITS_DMAE_PARITY_ERROR:
4829 					_print_next_block((*par_num)++, "DMAE");
4830 					_print_parity(bp,
4831 						      DMAE_REG_DMAE_PRTY_STS);
4832 					break;
4833 				case AEU_INPUTS_ATTN_BITS_IGU_PARITY_ERROR:
4834 					_print_next_block((*par_num)++, "IGU");
4835 					if (CHIP_IS_E1x(bp))
4836 						_print_parity(bp,
4837 							HC_REG_HC_PRTY_STS);
4838 					else
4839 						_print_parity(bp,
4840 							IGU_REG_IGU_PRTY_STS);
4841 					break;
4842 				case AEU_INPUTS_ATTN_BITS_MISC_PARITY_ERROR:
4843 					_print_next_block((*par_num)++, "MISC");
4844 					_print_parity(bp,
4845 						      MISC_REG_MISC_PRTY_STS);
4846 					break;
4847 				}
4848 			}
4849 
4850 			/* Clear the bit */
4851 			sig &= ~cur_bit;
4852 		}
4853 	}
4854 
4855 	return res;
4856 }
4857 
4858 static bool bnx2x_check_blocks_with_parity3(struct bnx2x *bp, u32 sig,
4859 					    int *par_num, bool *global,
4860 					    bool print)
4861 {
4862 	bool res = false;
4863 	u32 cur_bit;
4864 	int i;
4865 
4866 	for (i = 0; sig; i++) {
4867 		cur_bit = (0x1UL << i);
4868 		if (sig & cur_bit) {
4869 			switch (cur_bit) {
4870 			case AEU_INPUTS_ATTN_BITS_MCP_LATCHED_ROM_PARITY:
4871 				if (print)
4872 					_print_next_block((*par_num)++,
4873 							  "MCP ROM");
4874 				*global = true;
4875 				res = true;
4876 				break;
4877 			case AEU_INPUTS_ATTN_BITS_MCP_LATCHED_UMP_RX_PARITY:
4878 				if (print)
4879 					_print_next_block((*par_num)++,
4880 							  "MCP UMP RX");
4881 				*global = true;
4882 				res = true;
4883 				break;
4884 			case AEU_INPUTS_ATTN_BITS_MCP_LATCHED_UMP_TX_PARITY:
4885 				if (print)
4886 					_print_next_block((*par_num)++,
4887 							  "MCP UMP TX");
4888 				*global = true;
4889 				res = true;
4890 				break;
4891 			case AEU_INPUTS_ATTN_BITS_MCP_LATCHED_SCPAD_PARITY:
4892 				(*par_num)++;
4893 				/* clear latched SCPAD PATIRY from MCP */
4894 				REG_WR(bp, MISC_REG_AEU_CLR_LATCH_SIGNAL,
4895 				       1UL << 10);
4896 				break;
4897 			}
4898 
4899 			/* Clear the bit */
4900 			sig &= ~cur_bit;
4901 		}
4902 	}
4903 
4904 	return res;
4905 }
4906 
4907 static bool bnx2x_check_blocks_with_parity4(struct bnx2x *bp, u32 sig,
4908 					    int *par_num, bool print)
4909 {
4910 	u32 cur_bit;
4911 	bool res;
4912 	int i;
4913 
4914 	res = false;
4915 
4916 	for (i = 0; sig; i++) {
4917 		cur_bit = (0x1UL << i);
4918 		if (sig & cur_bit) {
4919 			res = true; /* Each bit is real error! */
4920 			if (print) {
4921 				switch (cur_bit) {
4922 				case AEU_INPUTS_ATTN_BITS_PGLUE_PARITY_ERROR:
4923 					_print_next_block((*par_num)++,
4924 							  "PGLUE_B");
4925 					_print_parity(bp,
4926 						      PGLUE_B_REG_PGLUE_B_PRTY_STS);
4927 					break;
4928 				case AEU_INPUTS_ATTN_BITS_ATC_PARITY_ERROR:
4929 					_print_next_block((*par_num)++, "ATC");
4930 					_print_parity(bp,
4931 						      ATC_REG_ATC_PRTY_STS);
4932 					break;
4933 				}
4934 			}
4935 			/* Clear the bit */
4936 			sig &= ~cur_bit;
4937 		}
4938 	}
4939 
4940 	return res;
4941 }
4942 
4943 static bool bnx2x_parity_attn(struct bnx2x *bp, bool *global, bool print,
4944 			      u32 *sig)
4945 {
4946 	bool res = false;
4947 
4948 	if ((sig[0] & HW_PRTY_ASSERT_SET_0) ||
4949 	    (sig[1] & HW_PRTY_ASSERT_SET_1) ||
4950 	    (sig[2] & HW_PRTY_ASSERT_SET_2) ||
4951 	    (sig[3] & HW_PRTY_ASSERT_SET_3) ||
4952 	    (sig[4] & HW_PRTY_ASSERT_SET_4)) {
4953 		int par_num = 0;
4954 
4955 		DP(NETIF_MSG_HW, "Was parity error: HW block parity attention:\n"
4956 				 "[0]:0x%08x [1]:0x%08x [2]:0x%08x [3]:0x%08x [4]:0x%08x\n",
4957 			  sig[0] & HW_PRTY_ASSERT_SET_0,
4958 			  sig[1] & HW_PRTY_ASSERT_SET_1,
4959 			  sig[2] & HW_PRTY_ASSERT_SET_2,
4960 			  sig[3] & HW_PRTY_ASSERT_SET_3,
4961 			  sig[4] & HW_PRTY_ASSERT_SET_4);
4962 		if (print) {
4963 			if (((sig[0] & HW_PRTY_ASSERT_SET_0) ||
4964 			     (sig[1] & HW_PRTY_ASSERT_SET_1) ||
4965 			     (sig[2] & HW_PRTY_ASSERT_SET_2) ||
4966 			     (sig[4] & HW_PRTY_ASSERT_SET_4)) ||
4967 			     (sig[3] & HW_PRTY_ASSERT_SET_3_WITHOUT_SCPAD)) {
4968 				netdev_err(bp->dev,
4969 					   "Parity errors detected in blocks: ");
4970 			} else {
4971 				print = false;
4972 			}
4973 		}
4974 		res |= bnx2x_check_blocks_with_parity0(bp,
4975 			sig[0] & HW_PRTY_ASSERT_SET_0, &par_num, print);
4976 		res |= bnx2x_check_blocks_with_parity1(bp,
4977 			sig[1] & HW_PRTY_ASSERT_SET_1, &par_num, global, print);
4978 		res |= bnx2x_check_blocks_with_parity2(bp,
4979 			sig[2] & HW_PRTY_ASSERT_SET_2, &par_num, print);
4980 		res |= bnx2x_check_blocks_with_parity3(bp,
4981 			sig[3] & HW_PRTY_ASSERT_SET_3, &par_num, global, print);
4982 		res |= bnx2x_check_blocks_with_parity4(bp,
4983 			sig[4] & HW_PRTY_ASSERT_SET_4, &par_num, print);
4984 
4985 		if (print)
4986 			pr_cont("\n");
4987 	}
4988 
4989 	return res;
4990 }
4991 
4992 /**
4993  * bnx2x_chk_parity_attn - checks for parity attentions.
4994  *
4995  * @bp:		driver handle
4996  * @global:	true if there was a global attention
4997  * @print:	show parity attention in syslog
4998  */
4999 bool bnx2x_chk_parity_attn(struct bnx2x *bp, bool *global, bool print)
5000 {
5001 	struct attn_route attn = { {0} };
5002 	int port = BP_PORT(bp);
5003 
5004 	attn.sig[0] = REG_RD(bp,
5005 		MISC_REG_AEU_AFTER_INVERT_1_FUNC_0 +
5006 			     port*4);
5007 	attn.sig[1] = REG_RD(bp,
5008 		MISC_REG_AEU_AFTER_INVERT_2_FUNC_0 +
5009 			     port*4);
5010 	attn.sig[2] = REG_RD(bp,
5011 		MISC_REG_AEU_AFTER_INVERT_3_FUNC_0 +
5012 			     port*4);
5013 	attn.sig[3] = REG_RD(bp,
5014 		MISC_REG_AEU_AFTER_INVERT_4_FUNC_0 +
5015 			     port*4);
5016 	/* Since MCP attentions can't be disabled inside the block, we need to
5017 	 * read AEU registers to see whether they're currently disabled
5018 	 */
5019 	attn.sig[3] &= ((REG_RD(bp,
5020 				!port ? MISC_REG_AEU_ENABLE4_FUNC_0_OUT_0
5021 				      : MISC_REG_AEU_ENABLE4_FUNC_1_OUT_0) &
5022 			 MISC_AEU_ENABLE_MCP_PRTY_BITS) |
5023 			~MISC_AEU_ENABLE_MCP_PRTY_BITS);
5024 
5025 	if (!CHIP_IS_E1x(bp))
5026 		attn.sig[4] = REG_RD(bp,
5027 			MISC_REG_AEU_AFTER_INVERT_5_FUNC_0 +
5028 				     port*4);
5029 
5030 	return bnx2x_parity_attn(bp, global, print, attn.sig);
5031 }
5032 
5033 static void bnx2x_attn_int_deasserted4(struct bnx2x *bp, u32 attn)
5034 {
5035 	u32 val;
5036 	if (attn & AEU_INPUTS_ATTN_BITS_PGLUE_HW_INTERRUPT) {
5037 
5038 		val = REG_RD(bp, PGLUE_B_REG_PGLUE_B_INT_STS_CLR);
5039 		BNX2X_ERR("PGLUE hw attention 0x%x\n", val);
5040 		if (val & PGLUE_B_PGLUE_B_INT_STS_REG_ADDRESS_ERROR)
5041 			BNX2X_ERR("PGLUE_B_PGLUE_B_INT_STS_REG_ADDRESS_ERROR\n");
5042 		if (val & PGLUE_B_PGLUE_B_INT_STS_REG_INCORRECT_RCV_BEHAVIOR)
5043 			BNX2X_ERR("PGLUE_B_PGLUE_B_INT_STS_REG_INCORRECT_RCV_BEHAVIOR\n");
5044 		if (val & PGLUE_B_PGLUE_B_INT_STS_REG_WAS_ERROR_ATTN)
5045 			BNX2X_ERR("PGLUE_B_PGLUE_B_INT_STS_REG_WAS_ERROR_ATTN\n");
5046 		if (val & PGLUE_B_PGLUE_B_INT_STS_REG_VF_LENGTH_VIOLATION_ATTN)
5047 			BNX2X_ERR("PGLUE_B_PGLUE_B_INT_STS_REG_VF_LENGTH_VIOLATION_ATTN\n");
5048 		if (val &
5049 		    PGLUE_B_PGLUE_B_INT_STS_REG_VF_GRC_SPACE_VIOLATION_ATTN)
5050 			BNX2X_ERR("PGLUE_B_PGLUE_B_INT_STS_REG_VF_GRC_SPACE_VIOLATION_ATTN\n");
5051 		if (val &
5052 		    PGLUE_B_PGLUE_B_INT_STS_REG_VF_MSIX_BAR_VIOLATION_ATTN)
5053 			BNX2X_ERR("PGLUE_B_PGLUE_B_INT_STS_REG_VF_MSIX_BAR_VIOLATION_ATTN\n");
5054 		if (val & PGLUE_B_PGLUE_B_INT_STS_REG_TCPL_ERROR_ATTN)
5055 			BNX2X_ERR("PGLUE_B_PGLUE_B_INT_STS_REG_TCPL_ERROR_ATTN\n");
5056 		if (val & PGLUE_B_PGLUE_B_INT_STS_REG_TCPL_IN_TWO_RCBS_ATTN)
5057 			BNX2X_ERR("PGLUE_B_PGLUE_B_INT_STS_REG_TCPL_IN_TWO_RCBS_ATTN\n");
5058 		if (val & PGLUE_B_PGLUE_B_INT_STS_REG_CSSNOOP_FIFO_OVERFLOW)
5059 			BNX2X_ERR("PGLUE_B_PGLUE_B_INT_STS_REG_CSSNOOP_FIFO_OVERFLOW\n");
5060 	}
5061 	if (attn & AEU_INPUTS_ATTN_BITS_ATC_HW_INTERRUPT) {
5062 		val = REG_RD(bp, ATC_REG_ATC_INT_STS_CLR);
5063 		BNX2X_ERR("ATC hw attention 0x%x\n", val);
5064 		if (val & ATC_ATC_INT_STS_REG_ADDRESS_ERROR)
5065 			BNX2X_ERR("ATC_ATC_INT_STS_REG_ADDRESS_ERROR\n");
5066 		if (val & ATC_ATC_INT_STS_REG_ATC_TCPL_TO_NOT_PEND)
5067 			BNX2X_ERR("ATC_ATC_INT_STS_REG_ATC_TCPL_TO_NOT_PEND\n");
5068 		if (val & ATC_ATC_INT_STS_REG_ATC_GPA_MULTIPLE_HITS)
5069 			BNX2X_ERR("ATC_ATC_INT_STS_REG_ATC_GPA_MULTIPLE_HITS\n");
5070 		if (val & ATC_ATC_INT_STS_REG_ATC_RCPL_TO_EMPTY_CNT)
5071 			BNX2X_ERR("ATC_ATC_INT_STS_REG_ATC_RCPL_TO_EMPTY_CNT\n");
5072 		if (val & ATC_ATC_INT_STS_REG_ATC_TCPL_ERROR)
5073 			BNX2X_ERR("ATC_ATC_INT_STS_REG_ATC_TCPL_ERROR\n");
5074 		if (val & ATC_ATC_INT_STS_REG_ATC_IREQ_LESS_THAN_STU)
5075 			BNX2X_ERR("ATC_ATC_INT_STS_REG_ATC_IREQ_LESS_THAN_STU\n");
5076 	}
5077 
5078 	if (attn & (AEU_INPUTS_ATTN_BITS_PGLUE_PARITY_ERROR |
5079 		    AEU_INPUTS_ATTN_BITS_ATC_PARITY_ERROR)) {
5080 		BNX2X_ERR("FATAL parity attention set4 0x%x\n",
5081 		(u32)(attn & (AEU_INPUTS_ATTN_BITS_PGLUE_PARITY_ERROR |
5082 		    AEU_INPUTS_ATTN_BITS_ATC_PARITY_ERROR)));
5083 	}
5084 }
5085 
5086 static void bnx2x_attn_int_deasserted(struct bnx2x *bp, u32 deasserted)
5087 {
5088 	struct attn_route attn, *group_mask;
5089 	int port = BP_PORT(bp);
5090 	int index;
5091 	u32 reg_addr;
5092 	u32 val;
5093 	u32 aeu_mask;
5094 	bool global = false;
5095 
5096 	/* need to take HW lock because MCP or other port might also
5097 	   try to handle this event */
5098 	bnx2x_acquire_alr(bp);
5099 
5100 	if (bnx2x_chk_parity_attn(bp, &global, true)) {
5101 #ifndef BNX2X_STOP_ON_ERROR
5102 		bp->recovery_state = BNX2X_RECOVERY_INIT;
5103 		schedule_delayed_work(&bp->sp_rtnl_task, 0);
5104 		/* Disable HW interrupts */
5105 		bnx2x_int_disable(bp);
5106 		/* In case of parity errors don't handle attentions so that
5107 		 * other function would "see" parity errors.
5108 		 */
5109 #else
5110 		bnx2x_panic();
5111 #endif
5112 		bnx2x_release_alr(bp);
5113 		return;
5114 	}
5115 
5116 	attn.sig[0] = REG_RD(bp, MISC_REG_AEU_AFTER_INVERT_1_FUNC_0 + port*4);
5117 	attn.sig[1] = REG_RD(bp, MISC_REG_AEU_AFTER_INVERT_2_FUNC_0 + port*4);
5118 	attn.sig[2] = REG_RD(bp, MISC_REG_AEU_AFTER_INVERT_3_FUNC_0 + port*4);
5119 	attn.sig[3] = REG_RD(bp, MISC_REG_AEU_AFTER_INVERT_4_FUNC_0 + port*4);
5120 	if (!CHIP_IS_E1x(bp))
5121 		attn.sig[4] =
5122 		      REG_RD(bp, MISC_REG_AEU_AFTER_INVERT_5_FUNC_0 + port*4);
5123 	else
5124 		attn.sig[4] = 0;
5125 
5126 	DP(NETIF_MSG_HW, "attn: %08x %08x %08x %08x %08x\n",
5127 	   attn.sig[0], attn.sig[1], attn.sig[2], attn.sig[3], attn.sig[4]);
5128 
5129 	for (index = 0; index < MAX_DYNAMIC_ATTN_GRPS; index++) {
5130 		if (deasserted & (1 << index)) {
5131 			group_mask = &bp->attn_group[index];
5132 
5133 			DP(NETIF_MSG_HW, "group[%d]: %08x %08x %08x %08x %08x\n",
5134 			   index,
5135 			   group_mask->sig[0], group_mask->sig[1],
5136 			   group_mask->sig[2], group_mask->sig[3],
5137 			   group_mask->sig[4]);
5138 
5139 			bnx2x_attn_int_deasserted4(bp,
5140 					attn.sig[4] & group_mask->sig[4]);
5141 			bnx2x_attn_int_deasserted3(bp,
5142 					attn.sig[3] & group_mask->sig[3]);
5143 			bnx2x_attn_int_deasserted1(bp,
5144 					attn.sig[1] & group_mask->sig[1]);
5145 			bnx2x_attn_int_deasserted2(bp,
5146 					attn.sig[2] & group_mask->sig[2]);
5147 			bnx2x_attn_int_deasserted0(bp,
5148 					attn.sig[0] & group_mask->sig[0]);
5149 		}
5150 	}
5151 
5152 	bnx2x_release_alr(bp);
5153 
5154 	if (bp->common.int_block == INT_BLOCK_HC)
5155 		reg_addr = (HC_REG_COMMAND_REG + port*32 +
5156 			    COMMAND_REG_ATTN_BITS_CLR);
5157 	else
5158 		reg_addr = (BAR_IGU_INTMEM + IGU_CMD_ATTN_BIT_CLR_UPPER*8);
5159 
5160 	val = ~deasserted;
5161 	DP(NETIF_MSG_HW, "about to mask 0x%08x at %s addr 0x%x\n", val,
5162 	   (bp->common.int_block == INT_BLOCK_HC) ? "HC" : "IGU", reg_addr);
5163 	REG_WR(bp, reg_addr, val);
5164 
5165 	if (~bp->attn_state & deasserted)
5166 		BNX2X_ERR("IGU ERROR\n");
5167 
5168 	reg_addr = port ? MISC_REG_AEU_MASK_ATTN_FUNC_1 :
5169 			  MISC_REG_AEU_MASK_ATTN_FUNC_0;
5170 
5171 	bnx2x_acquire_hw_lock(bp, HW_LOCK_RESOURCE_PORT0_ATT_MASK + port);
5172 	aeu_mask = REG_RD(bp, reg_addr);
5173 
5174 	DP(NETIF_MSG_HW, "aeu_mask %x  newly deasserted %x\n",
5175 	   aeu_mask, deasserted);
5176 	aeu_mask |= (deasserted & 0x3ff);
5177 	DP(NETIF_MSG_HW, "new mask %x\n", aeu_mask);
5178 
5179 	REG_WR(bp, reg_addr, aeu_mask);
5180 	bnx2x_release_hw_lock(bp, HW_LOCK_RESOURCE_PORT0_ATT_MASK + port);
5181 
5182 	DP(NETIF_MSG_HW, "attn_state %x\n", bp->attn_state);
5183 	bp->attn_state &= ~deasserted;
5184 	DP(NETIF_MSG_HW, "new state %x\n", bp->attn_state);
5185 }
5186 
5187 static void bnx2x_attn_int(struct bnx2x *bp)
5188 {
5189 	/* read local copy of bits */
5190 	u32 attn_bits = le32_to_cpu(bp->def_status_blk->atten_status_block.
5191 								attn_bits);
5192 	u32 attn_ack = le32_to_cpu(bp->def_status_blk->atten_status_block.
5193 								attn_bits_ack);
5194 	u32 attn_state = bp->attn_state;
5195 
5196 	/* look for changed bits */
5197 	u32 asserted   =  attn_bits & ~attn_ack & ~attn_state;
5198 	u32 deasserted = ~attn_bits &  attn_ack &  attn_state;
5199 
5200 	DP(NETIF_MSG_HW,
5201 	   "attn_bits %x  attn_ack %x  asserted %x  deasserted %x\n",
5202 	   attn_bits, attn_ack, asserted, deasserted);
5203 
5204 	if (~(attn_bits ^ attn_ack) & (attn_bits ^ attn_state))
5205 		BNX2X_ERR("BAD attention state\n");
5206 
5207 	/* handle bits that were raised */
5208 	if (asserted)
5209 		bnx2x_attn_int_asserted(bp, asserted);
5210 
5211 	if (deasserted)
5212 		bnx2x_attn_int_deasserted(bp, deasserted);
5213 }
5214 
5215 void bnx2x_igu_ack_sb(struct bnx2x *bp, u8 igu_sb_id, u8 segment,
5216 		      u16 index, u8 op, u8 update)
5217 {
5218 	u32 igu_addr = bp->igu_base_addr;
5219 	igu_addr += (IGU_CMD_INT_ACK_BASE + igu_sb_id)*8;
5220 	bnx2x_igu_ack_sb_gen(bp, igu_sb_id, segment, index, op, update,
5221 			     igu_addr);
5222 }
5223 
5224 static void bnx2x_update_eq_prod(struct bnx2x *bp, u16 prod)
5225 {
5226 	/* No memory barriers */
5227 	storm_memset_eq_prod(bp, prod, BP_FUNC(bp));
5228 }
5229 
5230 static int  bnx2x_cnic_handle_cfc_del(struct bnx2x *bp, u32 cid,
5231 				      union event_ring_elem *elem)
5232 {
5233 	u8 err = elem->message.error;
5234 
5235 	if (!bp->cnic_eth_dev.starting_cid  ||
5236 	    (cid < bp->cnic_eth_dev.starting_cid &&
5237 	    cid != bp->cnic_eth_dev.iscsi_l2_cid))
5238 		return 1;
5239 
5240 	DP(BNX2X_MSG_SP, "got delete ramrod for CNIC CID %d\n", cid);
5241 
5242 	if (unlikely(err)) {
5243 
5244 		BNX2X_ERR("got delete ramrod for CNIC CID %d with error!\n",
5245 			  cid);
5246 		bnx2x_panic_dump(bp, false);
5247 	}
5248 	bnx2x_cnic_cfc_comp(bp, cid, err);
5249 	return 0;
5250 }
5251 
5252 static void bnx2x_handle_mcast_eqe(struct bnx2x *bp)
5253 {
5254 	struct bnx2x_mcast_ramrod_params rparam;
5255 	int rc;
5256 
5257 	memset(&rparam, 0, sizeof(rparam));
5258 
5259 	rparam.mcast_obj = &bp->mcast_obj;
5260 
5261 	netif_addr_lock_bh(bp->dev);
5262 
5263 	/* Clear pending state for the last command */
5264 	bp->mcast_obj.raw.clear_pending(&bp->mcast_obj.raw);
5265 
5266 	/* If there are pending mcast commands - send them */
5267 	if (bp->mcast_obj.check_pending(&bp->mcast_obj)) {
5268 		rc = bnx2x_config_mcast(bp, &rparam, BNX2X_MCAST_CMD_CONT);
5269 		if (rc < 0)
5270 			BNX2X_ERR("Failed to send pending mcast commands: %d\n",
5271 				  rc);
5272 	}
5273 
5274 	netif_addr_unlock_bh(bp->dev);
5275 }
5276 
5277 static void bnx2x_handle_classification_eqe(struct bnx2x *bp,
5278 					    union event_ring_elem *elem)
5279 {
5280 	unsigned long ramrod_flags = 0;
5281 	int rc = 0;
5282 	u32 echo = le32_to_cpu(elem->message.data.eth_event.echo);
5283 	u32 cid = echo & BNX2X_SWCID_MASK;
5284 	struct bnx2x_vlan_mac_obj *vlan_mac_obj;
5285 
5286 	/* Always push next commands out, don't wait here */
5287 	__set_bit(RAMROD_CONT, &ramrod_flags);
5288 
5289 	switch (echo >> BNX2X_SWCID_SHIFT) {
5290 	case BNX2X_FILTER_MAC_PENDING:
5291 		DP(BNX2X_MSG_SP, "Got SETUP_MAC completions\n");
5292 		if (CNIC_LOADED(bp) && (cid == BNX2X_ISCSI_ETH_CID(bp)))
5293 			vlan_mac_obj = &bp->iscsi_l2_mac_obj;
5294 		else
5295 			vlan_mac_obj = &bp->sp_objs[cid].mac_obj;
5296 
5297 		break;
5298 	case BNX2X_FILTER_VLAN_PENDING:
5299 		DP(BNX2X_MSG_SP, "Got SETUP_VLAN completions\n");
5300 		vlan_mac_obj = &bp->sp_objs[cid].vlan_obj;
5301 		break;
5302 	case BNX2X_FILTER_MCAST_PENDING:
5303 		DP(BNX2X_MSG_SP, "Got SETUP_MCAST completions\n");
5304 		/* This is only relevant for 57710 where multicast MACs are
5305 		 * configured as unicast MACs using the same ramrod.
5306 		 */
5307 		bnx2x_handle_mcast_eqe(bp);
5308 		return;
5309 	default:
5310 		BNX2X_ERR("Unsupported classification command: 0x%x\n", echo);
5311 		return;
5312 	}
5313 
5314 	rc = vlan_mac_obj->complete(bp, vlan_mac_obj, elem, &ramrod_flags);
5315 
5316 	if (rc < 0)
5317 		BNX2X_ERR("Failed to schedule new commands: %d\n", rc);
5318 	else if (rc > 0)
5319 		DP(BNX2X_MSG_SP, "Scheduled next pending commands...\n");
5320 }
5321 
5322 static void bnx2x_set_iscsi_eth_rx_mode(struct bnx2x *bp, bool start);
5323 
5324 static void bnx2x_handle_rx_mode_eqe(struct bnx2x *bp)
5325 {
5326 	netif_addr_lock_bh(bp->dev);
5327 
5328 	clear_bit(BNX2X_FILTER_RX_MODE_PENDING, &bp->sp_state);
5329 
5330 	/* Send rx_mode command again if was requested */
5331 	if (test_and_clear_bit(BNX2X_FILTER_RX_MODE_SCHED, &bp->sp_state))
5332 		bnx2x_set_storm_rx_mode(bp);
5333 	else if (test_and_clear_bit(BNX2X_FILTER_ISCSI_ETH_START_SCHED,
5334 				    &bp->sp_state))
5335 		bnx2x_set_iscsi_eth_rx_mode(bp, true);
5336 	else if (test_and_clear_bit(BNX2X_FILTER_ISCSI_ETH_STOP_SCHED,
5337 				    &bp->sp_state))
5338 		bnx2x_set_iscsi_eth_rx_mode(bp, false);
5339 
5340 	netif_addr_unlock_bh(bp->dev);
5341 }
5342 
5343 static void bnx2x_after_afex_vif_lists(struct bnx2x *bp,
5344 					      union event_ring_elem *elem)
5345 {
5346 	if (elem->message.data.vif_list_event.echo == VIF_LIST_RULE_GET) {
5347 		DP(BNX2X_MSG_SP,
5348 		   "afex: ramrod completed VIF LIST_GET, addrs 0x%x\n",
5349 		   elem->message.data.vif_list_event.func_bit_map);
5350 		bnx2x_fw_command(bp, DRV_MSG_CODE_AFEX_LISTGET_ACK,
5351 			elem->message.data.vif_list_event.func_bit_map);
5352 	} else if (elem->message.data.vif_list_event.echo ==
5353 		   VIF_LIST_RULE_SET) {
5354 		DP(BNX2X_MSG_SP, "afex: ramrod completed VIF LIST_SET\n");
5355 		bnx2x_fw_command(bp, DRV_MSG_CODE_AFEX_LISTSET_ACK, 0);
5356 	}
5357 }
5358 
5359 /* called with rtnl_lock */
5360 static void bnx2x_after_function_update(struct bnx2x *bp)
5361 {
5362 	int q, rc;
5363 	struct bnx2x_fastpath *fp;
5364 	struct bnx2x_queue_state_params queue_params = {NULL};
5365 	struct bnx2x_queue_update_params *q_update_params =
5366 		&queue_params.params.update;
5367 
5368 	/* Send Q update command with afex vlan removal values for all Qs */
5369 	queue_params.cmd = BNX2X_Q_CMD_UPDATE;
5370 
5371 	/* set silent vlan removal values according to vlan mode */
5372 	__set_bit(BNX2X_Q_UPDATE_SILENT_VLAN_REM_CHNG,
5373 		  &q_update_params->update_flags);
5374 	__set_bit(BNX2X_Q_UPDATE_SILENT_VLAN_REM,
5375 		  &q_update_params->update_flags);
5376 	__set_bit(RAMROD_COMP_WAIT, &queue_params.ramrod_flags);
5377 
5378 	/* in access mode mark mask and value are 0 to strip all vlans */
5379 	if (bp->afex_vlan_mode == FUNC_MF_CFG_AFEX_VLAN_ACCESS_MODE) {
5380 		q_update_params->silent_removal_value = 0;
5381 		q_update_params->silent_removal_mask = 0;
5382 	} else {
5383 		q_update_params->silent_removal_value =
5384 			(bp->afex_def_vlan_tag & VLAN_VID_MASK);
5385 		q_update_params->silent_removal_mask = VLAN_VID_MASK;
5386 	}
5387 
5388 	for_each_eth_queue(bp, q) {
5389 		/* Set the appropriate Queue object */
5390 		fp = &bp->fp[q];
5391 		queue_params.q_obj = &bnx2x_sp_obj(bp, fp).q_obj;
5392 
5393 		/* send the ramrod */
5394 		rc = bnx2x_queue_state_change(bp, &queue_params);
5395 		if (rc < 0)
5396 			BNX2X_ERR("Failed to config silent vlan rem for Q %d\n",
5397 				  q);
5398 	}
5399 
5400 	if (!NO_FCOE(bp) && CNIC_ENABLED(bp)) {
5401 		fp = &bp->fp[FCOE_IDX(bp)];
5402 		queue_params.q_obj = &bnx2x_sp_obj(bp, fp).q_obj;
5403 
5404 		/* clear pending completion bit */
5405 		__clear_bit(RAMROD_COMP_WAIT, &queue_params.ramrod_flags);
5406 
5407 		/* mark latest Q bit */
5408 		smp_mb__before_atomic();
5409 		set_bit(BNX2X_AFEX_FCOE_Q_UPDATE_PENDING, &bp->sp_state);
5410 		smp_mb__after_atomic();
5411 
5412 		/* send Q update ramrod for FCoE Q */
5413 		rc = bnx2x_queue_state_change(bp, &queue_params);
5414 		if (rc < 0)
5415 			BNX2X_ERR("Failed to config silent vlan rem for Q %d\n",
5416 				  q);
5417 	} else {
5418 		/* If no FCoE ring - ACK MCP now */
5419 		bnx2x_link_report(bp);
5420 		bnx2x_fw_command(bp, DRV_MSG_CODE_AFEX_VIFSET_ACK, 0);
5421 	}
5422 }
5423 
5424 static struct bnx2x_queue_sp_obj *bnx2x_cid_to_q_obj(
5425 	struct bnx2x *bp, u32 cid)
5426 {
5427 	DP(BNX2X_MSG_SP, "retrieving fp from cid %d\n", cid);
5428 
5429 	if (CNIC_LOADED(bp) && (cid == BNX2X_FCOE_ETH_CID(bp)))
5430 		return &bnx2x_fcoe_sp_obj(bp, q_obj);
5431 	else
5432 		return &bp->sp_objs[CID_TO_FP(cid, bp)].q_obj;
5433 }
5434 
5435 static void bnx2x_eq_int(struct bnx2x *bp)
5436 {
5437 	u16 hw_cons, sw_cons, sw_prod;
5438 	union event_ring_elem *elem;
5439 	u8 echo;
5440 	u32 cid;
5441 	u8 opcode;
5442 	int rc, spqe_cnt = 0;
5443 	struct bnx2x_queue_sp_obj *q_obj;
5444 	struct bnx2x_func_sp_obj *f_obj = &bp->func_obj;
5445 	struct bnx2x_raw_obj *rss_raw = &bp->rss_conf_obj.raw;
5446 
5447 	hw_cons = le16_to_cpu(*bp->eq_cons_sb);
5448 
5449 	/* The hw_cos range is 1-255, 257 - the sw_cons range is 0-254, 256.
5450 	 * when we get the next-page we need to adjust so the loop
5451 	 * condition below will be met. The next element is the size of a
5452 	 * regular element and hence incrementing by 1
5453 	 */
5454 	if ((hw_cons & EQ_DESC_MAX_PAGE) == EQ_DESC_MAX_PAGE)
5455 		hw_cons++;
5456 
5457 	/* This function may never run in parallel with itself for a
5458 	 * specific bp, thus there is no need in "paired" read memory
5459 	 * barrier here.
5460 	 */
5461 	sw_cons = bp->eq_cons;
5462 	sw_prod = bp->eq_prod;
5463 
5464 	DP(BNX2X_MSG_SP, "EQ:  hw_cons %u  sw_cons %u bp->eq_spq_left %x\n",
5465 			hw_cons, sw_cons, atomic_read(&bp->eq_spq_left));
5466 
5467 	for (; sw_cons != hw_cons;
5468 	      sw_prod = NEXT_EQ_IDX(sw_prod), sw_cons = NEXT_EQ_IDX(sw_cons)) {
5469 
5470 		elem = &bp->eq_ring[EQ_DESC(sw_cons)];
5471 
5472 		rc = bnx2x_iov_eq_sp_event(bp, elem);
5473 		if (!rc) {
5474 			DP(BNX2X_MSG_IOV, "bnx2x_iov_eq_sp_event returned %d\n",
5475 			   rc);
5476 			goto next_spqe;
5477 		}
5478 
5479 		opcode = elem->message.opcode;
5480 
5481 		/* handle eq element */
5482 		switch (opcode) {
5483 		case EVENT_RING_OPCODE_VF_PF_CHANNEL:
5484 			bnx2x_vf_mbx_schedule(bp,
5485 					      &elem->message.data.vf_pf_event);
5486 			continue;
5487 
5488 		case EVENT_RING_OPCODE_STAT_QUERY:
5489 			DP_AND((BNX2X_MSG_SP | BNX2X_MSG_STATS),
5490 			       "got statistics comp event %d\n",
5491 			       bp->stats_comp++);
5492 			/* nothing to do with stats comp */
5493 			goto next_spqe;
5494 
5495 		case EVENT_RING_OPCODE_CFC_DEL:
5496 			/* handle according to cid range */
5497 			/*
5498 			 * we may want to verify here that the bp state is
5499 			 * HALTING
5500 			 */
5501 
5502 			/* elem CID originates from FW; actually LE */
5503 			cid = SW_CID(elem->message.data.cfc_del_event.cid);
5504 
5505 			DP(BNX2X_MSG_SP,
5506 			   "got delete ramrod for MULTI[%d]\n", cid);
5507 
5508 			if (CNIC_LOADED(bp) &&
5509 			    !bnx2x_cnic_handle_cfc_del(bp, cid, elem))
5510 				goto next_spqe;
5511 
5512 			q_obj = bnx2x_cid_to_q_obj(bp, cid);
5513 
5514 			if (q_obj->complete_cmd(bp, q_obj, BNX2X_Q_CMD_CFC_DEL))
5515 				break;
5516 
5517 			goto next_spqe;
5518 
5519 		case EVENT_RING_OPCODE_STOP_TRAFFIC:
5520 			DP(BNX2X_MSG_SP | BNX2X_MSG_DCB, "got STOP TRAFFIC\n");
5521 			bnx2x_dcbx_set_params(bp, BNX2X_DCBX_STATE_TX_PAUSED);
5522 			if (f_obj->complete_cmd(bp, f_obj,
5523 						BNX2X_F_CMD_TX_STOP))
5524 				break;
5525 			goto next_spqe;
5526 
5527 		case EVENT_RING_OPCODE_START_TRAFFIC:
5528 			DP(BNX2X_MSG_SP | BNX2X_MSG_DCB, "got START TRAFFIC\n");
5529 			bnx2x_dcbx_set_params(bp, BNX2X_DCBX_STATE_TX_RELEASED);
5530 			if (f_obj->complete_cmd(bp, f_obj,
5531 						BNX2X_F_CMD_TX_START))
5532 				break;
5533 			goto next_spqe;
5534 
5535 		case EVENT_RING_OPCODE_FUNCTION_UPDATE:
5536 			echo = elem->message.data.function_update_event.echo;
5537 			if (echo == SWITCH_UPDATE) {
5538 				DP(BNX2X_MSG_SP | NETIF_MSG_IFUP,
5539 				   "got FUNC_SWITCH_UPDATE ramrod\n");
5540 				if (f_obj->complete_cmd(
5541 					bp, f_obj, BNX2X_F_CMD_SWITCH_UPDATE))
5542 					break;
5543 
5544 			} else {
5545 				int cmd = BNX2X_SP_RTNL_AFEX_F_UPDATE;
5546 
5547 				DP(BNX2X_MSG_SP | BNX2X_MSG_MCP,
5548 				   "AFEX: ramrod completed FUNCTION_UPDATE\n");
5549 				f_obj->complete_cmd(bp, f_obj,
5550 						    BNX2X_F_CMD_AFEX_UPDATE);
5551 
5552 				/* We will perform the Queues update from
5553 				 * sp_rtnl task as all Queue SP operations
5554 				 * should run under rtnl_lock.
5555 				 */
5556 				bnx2x_schedule_sp_rtnl(bp, cmd, 0);
5557 			}
5558 
5559 			goto next_spqe;
5560 
5561 		case EVENT_RING_OPCODE_AFEX_VIF_LISTS:
5562 			f_obj->complete_cmd(bp, f_obj,
5563 					    BNX2X_F_CMD_AFEX_VIFLISTS);
5564 			bnx2x_after_afex_vif_lists(bp, elem);
5565 			goto next_spqe;
5566 		case EVENT_RING_OPCODE_FUNCTION_START:
5567 			DP(BNX2X_MSG_SP | NETIF_MSG_IFUP,
5568 			   "got FUNC_START ramrod\n");
5569 			if (f_obj->complete_cmd(bp, f_obj, BNX2X_F_CMD_START))
5570 				break;
5571 
5572 			goto next_spqe;
5573 
5574 		case EVENT_RING_OPCODE_FUNCTION_STOP:
5575 			DP(BNX2X_MSG_SP | NETIF_MSG_IFUP,
5576 			   "got FUNC_STOP ramrod\n");
5577 			if (f_obj->complete_cmd(bp, f_obj, BNX2X_F_CMD_STOP))
5578 				break;
5579 
5580 			goto next_spqe;
5581 
5582 		case EVENT_RING_OPCODE_SET_TIMESYNC:
5583 			DP(BNX2X_MSG_SP | BNX2X_MSG_PTP,
5584 			   "got set_timesync ramrod completion\n");
5585 			if (f_obj->complete_cmd(bp, f_obj,
5586 						BNX2X_F_CMD_SET_TIMESYNC))
5587 				break;
5588 			goto next_spqe;
5589 		}
5590 
5591 		switch (opcode | bp->state) {
5592 		case (EVENT_RING_OPCODE_RSS_UPDATE_RULES |
5593 		      BNX2X_STATE_OPEN):
5594 		case (EVENT_RING_OPCODE_RSS_UPDATE_RULES |
5595 		      BNX2X_STATE_OPENING_WAIT4_PORT):
5596 		case (EVENT_RING_OPCODE_RSS_UPDATE_RULES |
5597 		      BNX2X_STATE_CLOSING_WAIT4_HALT):
5598 			DP(BNX2X_MSG_SP, "got RSS_UPDATE ramrod. CID %d\n",
5599 			   SW_CID(elem->message.data.eth_event.echo));
5600 			rss_raw->clear_pending(rss_raw);
5601 			break;
5602 
5603 		case (EVENT_RING_OPCODE_SET_MAC | BNX2X_STATE_OPEN):
5604 		case (EVENT_RING_OPCODE_SET_MAC | BNX2X_STATE_DIAG):
5605 		case (EVENT_RING_OPCODE_SET_MAC |
5606 		      BNX2X_STATE_CLOSING_WAIT4_HALT):
5607 		case (EVENT_RING_OPCODE_CLASSIFICATION_RULES |
5608 		      BNX2X_STATE_OPEN):
5609 		case (EVENT_RING_OPCODE_CLASSIFICATION_RULES |
5610 		      BNX2X_STATE_DIAG):
5611 		case (EVENT_RING_OPCODE_CLASSIFICATION_RULES |
5612 		      BNX2X_STATE_CLOSING_WAIT4_HALT):
5613 			DP(BNX2X_MSG_SP, "got (un)set vlan/mac ramrod\n");
5614 			bnx2x_handle_classification_eqe(bp, elem);
5615 			break;
5616 
5617 		case (EVENT_RING_OPCODE_MULTICAST_RULES |
5618 		      BNX2X_STATE_OPEN):
5619 		case (EVENT_RING_OPCODE_MULTICAST_RULES |
5620 		      BNX2X_STATE_DIAG):
5621 		case (EVENT_RING_OPCODE_MULTICAST_RULES |
5622 		      BNX2X_STATE_CLOSING_WAIT4_HALT):
5623 			DP(BNX2X_MSG_SP, "got mcast ramrod\n");
5624 			bnx2x_handle_mcast_eqe(bp);
5625 			break;
5626 
5627 		case (EVENT_RING_OPCODE_FILTERS_RULES |
5628 		      BNX2X_STATE_OPEN):
5629 		case (EVENT_RING_OPCODE_FILTERS_RULES |
5630 		      BNX2X_STATE_DIAG):
5631 		case (EVENT_RING_OPCODE_FILTERS_RULES |
5632 		      BNX2X_STATE_CLOSING_WAIT4_HALT):
5633 			DP(BNX2X_MSG_SP, "got rx_mode ramrod\n");
5634 			bnx2x_handle_rx_mode_eqe(bp);
5635 			break;
5636 		default:
5637 			/* unknown event log error and continue */
5638 			BNX2X_ERR("Unknown EQ event %d, bp->state 0x%x\n",
5639 				  elem->message.opcode, bp->state);
5640 		}
5641 next_spqe:
5642 		spqe_cnt++;
5643 	} /* for */
5644 
5645 	smp_mb__before_atomic();
5646 	atomic_add(spqe_cnt, &bp->eq_spq_left);
5647 
5648 	bp->eq_cons = sw_cons;
5649 	bp->eq_prod = sw_prod;
5650 	/* Make sure that above mem writes were issued towards the memory */
5651 	smp_wmb();
5652 
5653 	/* update producer */
5654 	bnx2x_update_eq_prod(bp, bp->eq_prod);
5655 }
5656 
5657 static void bnx2x_sp_task(struct work_struct *work)
5658 {
5659 	struct bnx2x *bp = container_of(work, struct bnx2x, sp_task.work);
5660 
5661 	DP(BNX2X_MSG_SP, "sp task invoked\n");
5662 
5663 	/* make sure the atomic interrupt_occurred has been written */
5664 	smp_rmb();
5665 	if (atomic_read(&bp->interrupt_occurred)) {
5666 
5667 		/* what work needs to be performed? */
5668 		u16 status = bnx2x_update_dsb_idx(bp);
5669 
5670 		DP(BNX2X_MSG_SP, "status %x\n", status);
5671 		DP(BNX2X_MSG_SP, "setting interrupt_occurred to 0\n");
5672 		atomic_set(&bp->interrupt_occurred, 0);
5673 
5674 		/* HW attentions */
5675 		if (status & BNX2X_DEF_SB_ATT_IDX) {
5676 			bnx2x_attn_int(bp);
5677 			status &= ~BNX2X_DEF_SB_ATT_IDX;
5678 		}
5679 
5680 		/* SP events: STAT_QUERY and others */
5681 		if (status & BNX2X_DEF_SB_IDX) {
5682 			struct bnx2x_fastpath *fp = bnx2x_fcoe_fp(bp);
5683 
5684 			if (FCOE_INIT(bp) &&
5685 			    (bnx2x_has_rx_work(fp) || bnx2x_has_tx_work(fp))) {
5686 				/* Prevent local bottom-halves from running as
5687 				 * we are going to change the local NAPI list.
5688 				 */
5689 				local_bh_disable();
5690 				napi_schedule(&bnx2x_fcoe(bp, napi));
5691 				local_bh_enable();
5692 			}
5693 
5694 			/* Handle EQ completions */
5695 			bnx2x_eq_int(bp);
5696 			bnx2x_ack_sb(bp, bp->igu_dsb_id, USTORM_ID,
5697 				     le16_to_cpu(bp->def_idx), IGU_INT_NOP, 1);
5698 
5699 			status &= ~BNX2X_DEF_SB_IDX;
5700 		}
5701 
5702 		/* if status is non zero then perhaps something went wrong */
5703 		if (unlikely(status))
5704 			DP(BNX2X_MSG_SP,
5705 			   "got an unknown interrupt! (status 0x%x)\n", status);
5706 
5707 		/* ack status block only if something was actually handled */
5708 		bnx2x_ack_sb(bp, bp->igu_dsb_id, ATTENTION_ID,
5709 			     le16_to_cpu(bp->def_att_idx), IGU_INT_ENABLE, 1);
5710 	}
5711 
5712 	/* afex - poll to check if VIFSET_ACK should be sent to MFW */
5713 	if (test_and_clear_bit(BNX2X_AFEX_PENDING_VIFSET_MCP_ACK,
5714 			       &bp->sp_state)) {
5715 		bnx2x_link_report(bp);
5716 		bnx2x_fw_command(bp, DRV_MSG_CODE_AFEX_VIFSET_ACK, 0);
5717 	}
5718 }
5719 
5720 irqreturn_t bnx2x_msix_sp_int(int irq, void *dev_instance)
5721 {
5722 	struct net_device *dev = dev_instance;
5723 	struct bnx2x *bp = netdev_priv(dev);
5724 
5725 	bnx2x_ack_sb(bp, bp->igu_dsb_id, USTORM_ID, 0,
5726 		     IGU_INT_DISABLE, 0);
5727 
5728 #ifdef BNX2X_STOP_ON_ERROR
5729 	if (unlikely(bp->panic))
5730 		return IRQ_HANDLED;
5731 #endif
5732 
5733 	if (CNIC_LOADED(bp)) {
5734 		struct cnic_ops *c_ops;
5735 
5736 		rcu_read_lock();
5737 		c_ops = rcu_dereference(bp->cnic_ops);
5738 		if (c_ops)
5739 			c_ops->cnic_handler(bp->cnic_data, NULL);
5740 		rcu_read_unlock();
5741 	}
5742 
5743 	/* schedule sp task to perform default status block work, ack
5744 	 * attentions and enable interrupts.
5745 	 */
5746 	bnx2x_schedule_sp_task(bp);
5747 
5748 	return IRQ_HANDLED;
5749 }
5750 
5751 /* end of slow path */
5752 
5753 void bnx2x_drv_pulse(struct bnx2x *bp)
5754 {
5755 	SHMEM_WR(bp, func_mb[BP_FW_MB_IDX(bp)].drv_pulse_mb,
5756 		 bp->fw_drv_pulse_wr_seq);
5757 }
5758 
5759 static void bnx2x_timer(struct timer_list *t)
5760 {
5761 	struct bnx2x *bp = from_timer(bp, t, timer);
5762 
5763 	if (!netif_running(bp->dev))
5764 		return;
5765 
5766 	if (IS_PF(bp) &&
5767 	    !BP_NOMCP(bp)) {
5768 		int mb_idx = BP_FW_MB_IDX(bp);
5769 		u16 drv_pulse;
5770 		u16 mcp_pulse;
5771 
5772 		++bp->fw_drv_pulse_wr_seq;
5773 		bp->fw_drv_pulse_wr_seq &= DRV_PULSE_SEQ_MASK;
5774 		drv_pulse = bp->fw_drv_pulse_wr_seq;
5775 		bnx2x_drv_pulse(bp);
5776 
5777 		mcp_pulse = (SHMEM_RD(bp, func_mb[mb_idx].mcp_pulse_mb) &
5778 			     MCP_PULSE_SEQ_MASK);
5779 		/* The delta between driver pulse and mcp response
5780 		 * should not get too big. If the MFW is more than 5 pulses
5781 		 * behind, we should worry about it enough to generate an error
5782 		 * log.
5783 		 */
5784 		if (((drv_pulse - mcp_pulse) & MCP_PULSE_SEQ_MASK) > 5)
5785 			BNX2X_ERR("MFW seems hanged: drv_pulse (0x%x) != mcp_pulse (0x%x)\n",
5786 				  drv_pulse, mcp_pulse);
5787 	}
5788 
5789 	if (bp->state == BNX2X_STATE_OPEN)
5790 		bnx2x_stats_handle(bp, STATS_EVENT_UPDATE);
5791 
5792 	/* sample pf vf bulletin board for new posts from pf */
5793 	if (IS_VF(bp))
5794 		bnx2x_timer_sriov(bp);
5795 
5796 	mod_timer(&bp->timer, jiffies + bp->current_interval);
5797 }
5798 
5799 /* end of Statistics */
5800 
5801 /* nic init */
5802 
5803 /*
5804  * nic init service functions
5805  */
5806 
5807 static void bnx2x_fill(struct bnx2x *bp, u32 addr, int fill, u32 len)
5808 {
5809 	u32 i;
5810 	if (!(len%4) && !(addr%4))
5811 		for (i = 0; i < len; i += 4)
5812 			REG_WR(bp, addr + i, fill);
5813 	else
5814 		for (i = 0; i < len; i++)
5815 			REG_WR8(bp, addr + i, fill);
5816 }
5817 
5818 /* helper: writes FP SP data to FW - data_size in dwords */
5819 static void bnx2x_wr_fp_sb_data(struct bnx2x *bp,
5820 				int fw_sb_id,
5821 				u32 *sb_data_p,
5822 				u32 data_size)
5823 {
5824 	int index;
5825 	for (index = 0; index < data_size; index++)
5826 		REG_WR(bp, BAR_CSTRORM_INTMEM +
5827 			CSTORM_STATUS_BLOCK_DATA_OFFSET(fw_sb_id) +
5828 			sizeof(u32)*index,
5829 			*(sb_data_p + index));
5830 }
5831 
5832 static void bnx2x_zero_fp_sb(struct bnx2x *bp, int fw_sb_id)
5833 {
5834 	u32 *sb_data_p;
5835 	u32 data_size = 0;
5836 	struct hc_status_block_data_e2 sb_data_e2;
5837 	struct hc_status_block_data_e1x sb_data_e1x;
5838 
5839 	/* disable the function first */
5840 	if (!CHIP_IS_E1x(bp)) {
5841 		memset(&sb_data_e2, 0, sizeof(struct hc_status_block_data_e2));
5842 		sb_data_e2.common.state = SB_DISABLED;
5843 		sb_data_e2.common.p_func.vf_valid = false;
5844 		sb_data_p = (u32 *)&sb_data_e2;
5845 		data_size = sizeof(struct hc_status_block_data_e2)/sizeof(u32);
5846 	} else {
5847 		memset(&sb_data_e1x, 0,
5848 		       sizeof(struct hc_status_block_data_e1x));
5849 		sb_data_e1x.common.state = SB_DISABLED;
5850 		sb_data_e1x.common.p_func.vf_valid = false;
5851 		sb_data_p = (u32 *)&sb_data_e1x;
5852 		data_size = sizeof(struct hc_status_block_data_e1x)/sizeof(u32);
5853 	}
5854 	bnx2x_wr_fp_sb_data(bp, fw_sb_id, sb_data_p, data_size);
5855 
5856 	bnx2x_fill(bp, BAR_CSTRORM_INTMEM +
5857 			CSTORM_STATUS_BLOCK_OFFSET(fw_sb_id), 0,
5858 			CSTORM_STATUS_BLOCK_SIZE);
5859 	bnx2x_fill(bp, BAR_CSTRORM_INTMEM +
5860 			CSTORM_SYNC_BLOCK_OFFSET(fw_sb_id), 0,
5861 			CSTORM_SYNC_BLOCK_SIZE);
5862 }
5863 
5864 /* helper:  writes SP SB data to FW */
5865 static void bnx2x_wr_sp_sb_data(struct bnx2x *bp,
5866 		struct hc_sp_status_block_data *sp_sb_data)
5867 {
5868 	int func = BP_FUNC(bp);
5869 	int i;
5870 	for (i = 0; i < sizeof(struct hc_sp_status_block_data)/sizeof(u32); i++)
5871 		REG_WR(bp, BAR_CSTRORM_INTMEM +
5872 			CSTORM_SP_STATUS_BLOCK_DATA_OFFSET(func) +
5873 			i*sizeof(u32),
5874 			*((u32 *)sp_sb_data + i));
5875 }
5876 
5877 static void bnx2x_zero_sp_sb(struct bnx2x *bp)
5878 {
5879 	int func = BP_FUNC(bp);
5880 	struct hc_sp_status_block_data sp_sb_data;
5881 	memset(&sp_sb_data, 0, sizeof(struct hc_sp_status_block_data));
5882 
5883 	sp_sb_data.state = SB_DISABLED;
5884 	sp_sb_data.p_func.vf_valid = false;
5885 
5886 	bnx2x_wr_sp_sb_data(bp, &sp_sb_data);
5887 
5888 	bnx2x_fill(bp, BAR_CSTRORM_INTMEM +
5889 			CSTORM_SP_STATUS_BLOCK_OFFSET(func), 0,
5890 			CSTORM_SP_STATUS_BLOCK_SIZE);
5891 	bnx2x_fill(bp, BAR_CSTRORM_INTMEM +
5892 			CSTORM_SP_SYNC_BLOCK_OFFSET(func), 0,
5893 			CSTORM_SP_SYNC_BLOCK_SIZE);
5894 }
5895 
5896 static void bnx2x_setup_ndsb_state_machine(struct hc_status_block_sm *hc_sm,
5897 					   int igu_sb_id, int igu_seg_id)
5898 {
5899 	hc_sm->igu_sb_id = igu_sb_id;
5900 	hc_sm->igu_seg_id = igu_seg_id;
5901 	hc_sm->timer_value = 0xFF;
5902 	hc_sm->time_to_expire = 0xFFFFFFFF;
5903 }
5904 
5905 /* allocates state machine ids. */
5906 static void bnx2x_map_sb_state_machines(struct hc_index_data *index_data)
5907 {
5908 	/* zero out state machine indices */
5909 	/* rx indices */
5910 	index_data[HC_INDEX_ETH_RX_CQ_CONS].flags &= ~HC_INDEX_DATA_SM_ID;
5911 
5912 	/* tx indices */
5913 	index_data[HC_INDEX_OOO_TX_CQ_CONS].flags &= ~HC_INDEX_DATA_SM_ID;
5914 	index_data[HC_INDEX_ETH_TX_CQ_CONS_COS0].flags &= ~HC_INDEX_DATA_SM_ID;
5915 	index_data[HC_INDEX_ETH_TX_CQ_CONS_COS1].flags &= ~HC_INDEX_DATA_SM_ID;
5916 	index_data[HC_INDEX_ETH_TX_CQ_CONS_COS2].flags &= ~HC_INDEX_DATA_SM_ID;
5917 
5918 	/* map indices */
5919 	/* rx indices */
5920 	index_data[HC_INDEX_ETH_RX_CQ_CONS].flags |=
5921 		SM_RX_ID << HC_INDEX_DATA_SM_ID_SHIFT;
5922 
5923 	/* tx indices */
5924 	index_data[HC_INDEX_OOO_TX_CQ_CONS].flags |=
5925 		SM_TX_ID << HC_INDEX_DATA_SM_ID_SHIFT;
5926 	index_data[HC_INDEX_ETH_TX_CQ_CONS_COS0].flags |=
5927 		SM_TX_ID << HC_INDEX_DATA_SM_ID_SHIFT;
5928 	index_data[HC_INDEX_ETH_TX_CQ_CONS_COS1].flags |=
5929 		SM_TX_ID << HC_INDEX_DATA_SM_ID_SHIFT;
5930 	index_data[HC_INDEX_ETH_TX_CQ_CONS_COS2].flags |=
5931 		SM_TX_ID << HC_INDEX_DATA_SM_ID_SHIFT;
5932 }
5933 
5934 void bnx2x_init_sb(struct bnx2x *bp, dma_addr_t mapping, int vfid,
5935 			  u8 vf_valid, int fw_sb_id, int igu_sb_id)
5936 {
5937 	int igu_seg_id;
5938 
5939 	struct hc_status_block_data_e2 sb_data_e2;
5940 	struct hc_status_block_data_e1x sb_data_e1x;
5941 	struct hc_status_block_sm  *hc_sm_p;
5942 	int data_size;
5943 	u32 *sb_data_p;
5944 
5945 	if (CHIP_INT_MODE_IS_BC(bp))
5946 		igu_seg_id = HC_SEG_ACCESS_NORM;
5947 	else
5948 		igu_seg_id = IGU_SEG_ACCESS_NORM;
5949 
5950 	bnx2x_zero_fp_sb(bp, fw_sb_id);
5951 
5952 	if (!CHIP_IS_E1x(bp)) {
5953 		memset(&sb_data_e2, 0, sizeof(struct hc_status_block_data_e2));
5954 		sb_data_e2.common.state = SB_ENABLED;
5955 		sb_data_e2.common.p_func.pf_id = BP_FUNC(bp);
5956 		sb_data_e2.common.p_func.vf_id = vfid;
5957 		sb_data_e2.common.p_func.vf_valid = vf_valid;
5958 		sb_data_e2.common.p_func.vnic_id = BP_VN(bp);
5959 		sb_data_e2.common.same_igu_sb_1b = true;
5960 		sb_data_e2.common.host_sb_addr.hi = U64_HI(mapping);
5961 		sb_data_e2.common.host_sb_addr.lo = U64_LO(mapping);
5962 		hc_sm_p = sb_data_e2.common.state_machine;
5963 		sb_data_p = (u32 *)&sb_data_e2;
5964 		data_size = sizeof(struct hc_status_block_data_e2)/sizeof(u32);
5965 		bnx2x_map_sb_state_machines(sb_data_e2.index_data);
5966 	} else {
5967 		memset(&sb_data_e1x, 0,
5968 		       sizeof(struct hc_status_block_data_e1x));
5969 		sb_data_e1x.common.state = SB_ENABLED;
5970 		sb_data_e1x.common.p_func.pf_id = BP_FUNC(bp);
5971 		sb_data_e1x.common.p_func.vf_id = 0xff;
5972 		sb_data_e1x.common.p_func.vf_valid = false;
5973 		sb_data_e1x.common.p_func.vnic_id = BP_VN(bp);
5974 		sb_data_e1x.common.same_igu_sb_1b = true;
5975 		sb_data_e1x.common.host_sb_addr.hi = U64_HI(mapping);
5976 		sb_data_e1x.common.host_sb_addr.lo = U64_LO(mapping);
5977 		hc_sm_p = sb_data_e1x.common.state_machine;
5978 		sb_data_p = (u32 *)&sb_data_e1x;
5979 		data_size = sizeof(struct hc_status_block_data_e1x)/sizeof(u32);
5980 		bnx2x_map_sb_state_machines(sb_data_e1x.index_data);
5981 	}
5982 
5983 	bnx2x_setup_ndsb_state_machine(&hc_sm_p[SM_RX_ID],
5984 				       igu_sb_id, igu_seg_id);
5985 	bnx2x_setup_ndsb_state_machine(&hc_sm_p[SM_TX_ID],
5986 				       igu_sb_id, igu_seg_id);
5987 
5988 	DP(NETIF_MSG_IFUP, "Init FW SB %d\n", fw_sb_id);
5989 
5990 	/* write indices to HW - PCI guarantees endianity of regpairs */
5991 	bnx2x_wr_fp_sb_data(bp, fw_sb_id, sb_data_p, data_size);
5992 }
5993 
5994 static void bnx2x_update_coalesce_sb(struct bnx2x *bp, u8 fw_sb_id,
5995 				     u16 tx_usec, u16 rx_usec)
5996 {
5997 	bnx2x_update_coalesce_sb_index(bp, fw_sb_id, HC_INDEX_ETH_RX_CQ_CONS,
5998 				    false, rx_usec);
5999 	bnx2x_update_coalesce_sb_index(bp, fw_sb_id,
6000 				       HC_INDEX_ETH_TX_CQ_CONS_COS0, false,
6001 				       tx_usec);
6002 	bnx2x_update_coalesce_sb_index(bp, fw_sb_id,
6003 				       HC_INDEX_ETH_TX_CQ_CONS_COS1, false,
6004 				       tx_usec);
6005 	bnx2x_update_coalesce_sb_index(bp, fw_sb_id,
6006 				       HC_INDEX_ETH_TX_CQ_CONS_COS2, false,
6007 				       tx_usec);
6008 }
6009 
6010 static void bnx2x_init_def_sb(struct bnx2x *bp)
6011 {
6012 	struct host_sp_status_block *def_sb = bp->def_status_blk;
6013 	dma_addr_t mapping = bp->def_status_blk_mapping;
6014 	int igu_sp_sb_index;
6015 	int igu_seg_id;
6016 	int port = BP_PORT(bp);
6017 	int func = BP_FUNC(bp);
6018 	int reg_offset, reg_offset_en5;
6019 	u64 section;
6020 	int index;
6021 	struct hc_sp_status_block_data sp_sb_data;
6022 	memset(&sp_sb_data, 0, sizeof(struct hc_sp_status_block_data));
6023 
6024 	if (CHIP_INT_MODE_IS_BC(bp)) {
6025 		igu_sp_sb_index = DEF_SB_IGU_ID;
6026 		igu_seg_id = HC_SEG_ACCESS_DEF;
6027 	} else {
6028 		igu_sp_sb_index = bp->igu_dsb_id;
6029 		igu_seg_id = IGU_SEG_ACCESS_DEF;
6030 	}
6031 
6032 	/* ATTN */
6033 	section = ((u64)mapping) + offsetof(struct host_sp_status_block,
6034 					    atten_status_block);
6035 	def_sb->atten_status_block.status_block_id = igu_sp_sb_index;
6036 
6037 	bp->attn_state = 0;
6038 
6039 	reg_offset = (port ? MISC_REG_AEU_ENABLE1_FUNC_1_OUT_0 :
6040 			     MISC_REG_AEU_ENABLE1_FUNC_0_OUT_0);
6041 	reg_offset_en5 = (port ? MISC_REG_AEU_ENABLE5_FUNC_1_OUT_0 :
6042 				 MISC_REG_AEU_ENABLE5_FUNC_0_OUT_0);
6043 	for (index = 0; index < MAX_DYNAMIC_ATTN_GRPS; index++) {
6044 		int sindex;
6045 		/* take care of sig[0]..sig[4] */
6046 		for (sindex = 0; sindex < 4; sindex++)
6047 			bp->attn_group[index].sig[sindex] =
6048 			   REG_RD(bp, reg_offset + sindex*0x4 + 0x10*index);
6049 
6050 		if (!CHIP_IS_E1x(bp))
6051 			/*
6052 			 * enable5 is separate from the rest of the registers,
6053 			 * and therefore the address skip is 4
6054 			 * and not 16 between the different groups
6055 			 */
6056 			bp->attn_group[index].sig[4] = REG_RD(bp,
6057 					reg_offset_en5 + 0x4*index);
6058 		else
6059 			bp->attn_group[index].sig[4] = 0;
6060 	}
6061 
6062 	if (bp->common.int_block == INT_BLOCK_HC) {
6063 		reg_offset = (port ? HC_REG_ATTN_MSG1_ADDR_L :
6064 				     HC_REG_ATTN_MSG0_ADDR_L);
6065 
6066 		REG_WR(bp, reg_offset, U64_LO(section));
6067 		REG_WR(bp, reg_offset + 4, U64_HI(section));
6068 	} else if (!CHIP_IS_E1x(bp)) {
6069 		REG_WR(bp, IGU_REG_ATTN_MSG_ADDR_L, U64_LO(section));
6070 		REG_WR(bp, IGU_REG_ATTN_MSG_ADDR_H, U64_HI(section));
6071 	}
6072 
6073 	section = ((u64)mapping) + offsetof(struct host_sp_status_block,
6074 					    sp_sb);
6075 
6076 	bnx2x_zero_sp_sb(bp);
6077 
6078 	/* PCI guarantees endianity of regpairs */
6079 	sp_sb_data.state		= SB_ENABLED;
6080 	sp_sb_data.host_sb_addr.lo	= U64_LO(section);
6081 	sp_sb_data.host_sb_addr.hi	= U64_HI(section);
6082 	sp_sb_data.igu_sb_id		= igu_sp_sb_index;
6083 	sp_sb_data.igu_seg_id		= igu_seg_id;
6084 	sp_sb_data.p_func.pf_id		= func;
6085 	sp_sb_data.p_func.vnic_id	= BP_VN(bp);
6086 	sp_sb_data.p_func.vf_id		= 0xff;
6087 
6088 	bnx2x_wr_sp_sb_data(bp, &sp_sb_data);
6089 
6090 	bnx2x_ack_sb(bp, bp->igu_dsb_id, USTORM_ID, 0, IGU_INT_ENABLE, 0);
6091 }
6092 
6093 void bnx2x_update_coalesce(struct bnx2x *bp)
6094 {
6095 	int i;
6096 
6097 	for_each_eth_queue(bp, i)
6098 		bnx2x_update_coalesce_sb(bp, bp->fp[i].fw_sb_id,
6099 					 bp->tx_ticks, bp->rx_ticks);
6100 }
6101 
6102 static void bnx2x_init_sp_ring(struct bnx2x *bp)
6103 {
6104 	spin_lock_init(&bp->spq_lock);
6105 	atomic_set(&bp->cq_spq_left, MAX_SPQ_PENDING);
6106 
6107 	bp->spq_prod_idx = 0;
6108 	bp->dsb_sp_prod = BNX2X_SP_DSB_INDEX;
6109 	bp->spq_prod_bd = bp->spq;
6110 	bp->spq_last_bd = bp->spq_prod_bd + MAX_SP_DESC_CNT;
6111 }
6112 
6113 static void bnx2x_init_eq_ring(struct bnx2x *bp)
6114 {
6115 	int i;
6116 	for (i = 1; i <= NUM_EQ_PAGES; i++) {
6117 		union event_ring_elem *elem =
6118 			&bp->eq_ring[EQ_DESC_CNT_PAGE * i - 1];
6119 
6120 		elem->next_page.addr.hi =
6121 			cpu_to_le32(U64_HI(bp->eq_mapping +
6122 				   BCM_PAGE_SIZE * (i % NUM_EQ_PAGES)));
6123 		elem->next_page.addr.lo =
6124 			cpu_to_le32(U64_LO(bp->eq_mapping +
6125 				   BCM_PAGE_SIZE*(i % NUM_EQ_PAGES)));
6126 	}
6127 	bp->eq_cons = 0;
6128 	bp->eq_prod = NUM_EQ_DESC;
6129 	bp->eq_cons_sb = BNX2X_EQ_INDEX;
6130 	/* we want a warning message before it gets wrought... */
6131 	atomic_set(&bp->eq_spq_left,
6132 		min_t(int, MAX_SP_DESC_CNT - MAX_SPQ_PENDING, NUM_EQ_DESC) - 1);
6133 }
6134 
6135 /* called with netif_addr_lock_bh() */
6136 static int bnx2x_set_q_rx_mode(struct bnx2x *bp, u8 cl_id,
6137 			       unsigned long rx_mode_flags,
6138 			       unsigned long rx_accept_flags,
6139 			       unsigned long tx_accept_flags,
6140 			       unsigned long ramrod_flags)
6141 {
6142 	struct bnx2x_rx_mode_ramrod_params ramrod_param;
6143 	int rc;
6144 
6145 	memset(&ramrod_param, 0, sizeof(ramrod_param));
6146 
6147 	/* Prepare ramrod parameters */
6148 	ramrod_param.cid = 0;
6149 	ramrod_param.cl_id = cl_id;
6150 	ramrod_param.rx_mode_obj = &bp->rx_mode_obj;
6151 	ramrod_param.func_id = BP_FUNC(bp);
6152 
6153 	ramrod_param.pstate = &bp->sp_state;
6154 	ramrod_param.state = BNX2X_FILTER_RX_MODE_PENDING;
6155 
6156 	ramrod_param.rdata = bnx2x_sp(bp, rx_mode_rdata);
6157 	ramrod_param.rdata_mapping = bnx2x_sp_mapping(bp, rx_mode_rdata);
6158 
6159 	set_bit(BNX2X_FILTER_RX_MODE_PENDING, &bp->sp_state);
6160 
6161 	ramrod_param.ramrod_flags = ramrod_flags;
6162 	ramrod_param.rx_mode_flags = rx_mode_flags;
6163 
6164 	ramrod_param.rx_accept_flags = rx_accept_flags;
6165 	ramrod_param.tx_accept_flags = tx_accept_flags;
6166 
6167 	rc = bnx2x_config_rx_mode(bp, &ramrod_param);
6168 	if (rc < 0) {
6169 		BNX2X_ERR("Set rx_mode %d failed\n", bp->rx_mode);
6170 		return rc;
6171 	}
6172 
6173 	return 0;
6174 }
6175 
6176 static int bnx2x_fill_accept_flags(struct bnx2x *bp, u32 rx_mode,
6177 				   unsigned long *rx_accept_flags,
6178 				   unsigned long *tx_accept_flags)
6179 {
6180 	/* Clear the flags first */
6181 	*rx_accept_flags = 0;
6182 	*tx_accept_flags = 0;
6183 
6184 	switch (rx_mode) {
6185 	case BNX2X_RX_MODE_NONE:
6186 		/*
6187 		 * 'drop all' supersedes any accept flags that may have been
6188 		 * passed to the function.
6189 		 */
6190 		break;
6191 	case BNX2X_RX_MODE_NORMAL:
6192 		__set_bit(BNX2X_ACCEPT_UNICAST, rx_accept_flags);
6193 		__set_bit(BNX2X_ACCEPT_MULTICAST, rx_accept_flags);
6194 		__set_bit(BNX2X_ACCEPT_BROADCAST, rx_accept_flags);
6195 
6196 		/* internal switching mode */
6197 		__set_bit(BNX2X_ACCEPT_UNICAST, tx_accept_flags);
6198 		__set_bit(BNX2X_ACCEPT_MULTICAST, tx_accept_flags);
6199 		__set_bit(BNX2X_ACCEPT_BROADCAST, tx_accept_flags);
6200 
6201 		if (bp->accept_any_vlan) {
6202 			__set_bit(BNX2X_ACCEPT_ANY_VLAN, rx_accept_flags);
6203 			__set_bit(BNX2X_ACCEPT_ANY_VLAN, tx_accept_flags);
6204 		}
6205 
6206 		break;
6207 	case BNX2X_RX_MODE_ALLMULTI:
6208 		__set_bit(BNX2X_ACCEPT_UNICAST, rx_accept_flags);
6209 		__set_bit(BNX2X_ACCEPT_ALL_MULTICAST, rx_accept_flags);
6210 		__set_bit(BNX2X_ACCEPT_BROADCAST, rx_accept_flags);
6211 
6212 		/* internal switching mode */
6213 		__set_bit(BNX2X_ACCEPT_UNICAST, tx_accept_flags);
6214 		__set_bit(BNX2X_ACCEPT_ALL_MULTICAST, tx_accept_flags);
6215 		__set_bit(BNX2X_ACCEPT_BROADCAST, tx_accept_flags);
6216 
6217 		if (bp->accept_any_vlan) {
6218 			__set_bit(BNX2X_ACCEPT_ANY_VLAN, rx_accept_flags);
6219 			__set_bit(BNX2X_ACCEPT_ANY_VLAN, tx_accept_flags);
6220 		}
6221 
6222 		break;
6223 	case BNX2X_RX_MODE_PROMISC:
6224 		/* According to definition of SI mode, iface in promisc mode
6225 		 * should receive matched and unmatched (in resolution of port)
6226 		 * unicast packets.
6227 		 */
6228 		__set_bit(BNX2X_ACCEPT_UNMATCHED, rx_accept_flags);
6229 		__set_bit(BNX2X_ACCEPT_UNICAST, rx_accept_flags);
6230 		__set_bit(BNX2X_ACCEPT_ALL_MULTICAST, rx_accept_flags);
6231 		__set_bit(BNX2X_ACCEPT_BROADCAST, rx_accept_flags);
6232 
6233 		/* internal switching mode */
6234 		__set_bit(BNX2X_ACCEPT_ALL_MULTICAST, tx_accept_flags);
6235 		__set_bit(BNX2X_ACCEPT_BROADCAST, tx_accept_flags);
6236 
6237 		if (IS_MF_SI(bp))
6238 			__set_bit(BNX2X_ACCEPT_ALL_UNICAST, tx_accept_flags);
6239 		else
6240 			__set_bit(BNX2X_ACCEPT_UNICAST, tx_accept_flags);
6241 
6242 		__set_bit(BNX2X_ACCEPT_ANY_VLAN, rx_accept_flags);
6243 		__set_bit(BNX2X_ACCEPT_ANY_VLAN, tx_accept_flags);
6244 
6245 		break;
6246 	default:
6247 		BNX2X_ERR("Unknown rx_mode: %d\n", rx_mode);
6248 		return -EINVAL;
6249 	}
6250 
6251 	return 0;
6252 }
6253 
6254 /* called with netif_addr_lock_bh() */
6255 static int bnx2x_set_storm_rx_mode(struct bnx2x *bp)
6256 {
6257 	unsigned long rx_mode_flags = 0, ramrod_flags = 0;
6258 	unsigned long rx_accept_flags = 0, tx_accept_flags = 0;
6259 	int rc;
6260 
6261 	if (!NO_FCOE(bp))
6262 		/* Configure rx_mode of FCoE Queue */
6263 		__set_bit(BNX2X_RX_MODE_FCOE_ETH, &rx_mode_flags);
6264 
6265 	rc = bnx2x_fill_accept_flags(bp, bp->rx_mode, &rx_accept_flags,
6266 				     &tx_accept_flags);
6267 	if (rc)
6268 		return rc;
6269 
6270 	__set_bit(RAMROD_RX, &ramrod_flags);
6271 	__set_bit(RAMROD_TX, &ramrod_flags);
6272 
6273 	return bnx2x_set_q_rx_mode(bp, bp->fp->cl_id, rx_mode_flags,
6274 				   rx_accept_flags, tx_accept_flags,
6275 				   ramrod_flags);
6276 }
6277 
6278 static void bnx2x_init_internal_common(struct bnx2x *bp)
6279 {
6280 	int i;
6281 
6282 	/* Zero this manually as its initialization is
6283 	   currently missing in the initTool */
6284 	for (i = 0; i < (USTORM_AGG_DATA_SIZE >> 2); i++)
6285 		REG_WR(bp, BAR_USTRORM_INTMEM +
6286 		       USTORM_AGG_DATA_OFFSET + i * 4, 0);
6287 	if (!CHIP_IS_E1x(bp)) {
6288 		REG_WR8(bp, BAR_CSTRORM_INTMEM + CSTORM_IGU_MODE_OFFSET,
6289 			CHIP_INT_MODE_IS_BC(bp) ?
6290 			HC_IGU_BC_MODE : HC_IGU_NBC_MODE);
6291 	}
6292 }
6293 
6294 static void bnx2x_init_internal(struct bnx2x *bp, u32 load_code)
6295 {
6296 	switch (load_code) {
6297 	case FW_MSG_CODE_DRV_LOAD_COMMON:
6298 	case FW_MSG_CODE_DRV_LOAD_COMMON_CHIP:
6299 		bnx2x_init_internal_common(bp);
6300 		/* no break */
6301 
6302 	case FW_MSG_CODE_DRV_LOAD_PORT:
6303 		/* nothing to do */
6304 		/* no break */
6305 
6306 	case FW_MSG_CODE_DRV_LOAD_FUNCTION:
6307 		/* internal memory per function is
6308 		   initialized inside bnx2x_pf_init */
6309 		break;
6310 
6311 	default:
6312 		BNX2X_ERR("Unknown load_code (0x%x) from MCP\n", load_code);
6313 		break;
6314 	}
6315 }
6316 
6317 static inline u8 bnx2x_fp_igu_sb_id(struct bnx2x_fastpath *fp)
6318 {
6319 	return fp->bp->igu_base_sb + fp->index + CNIC_SUPPORT(fp->bp);
6320 }
6321 
6322 static inline u8 bnx2x_fp_fw_sb_id(struct bnx2x_fastpath *fp)
6323 {
6324 	return fp->bp->base_fw_ndsb + fp->index + CNIC_SUPPORT(fp->bp);
6325 }
6326 
6327 static u8 bnx2x_fp_cl_id(struct bnx2x_fastpath *fp)
6328 {
6329 	if (CHIP_IS_E1x(fp->bp))
6330 		return BP_L_ID(fp->bp) + fp->index;
6331 	else	/* We want Client ID to be the same as IGU SB ID for 57712 */
6332 		return bnx2x_fp_igu_sb_id(fp);
6333 }
6334 
6335 static void bnx2x_init_eth_fp(struct bnx2x *bp, int fp_idx)
6336 {
6337 	struct bnx2x_fastpath *fp = &bp->fp[fp_idx];
6338 	u8 cos;
6339 	unsigned long q_type = 0;
6340 	u32 cids[BNX2X_MULTI_TX_COS] = { 0 };
6341 	fp->rx_queue = fp_idx;
6342 	fp->cid = fp_idx;
6343 	fp->cl_id = bnx2x_fp_cl_id(fp);
6344 	fp->fw_sb_id = bnx2x_fp_fw_sb_id(fp);
6345 	fp->igu_sb_id = bnx2x_fp_igu_sb_id(fp);
6346 	/* qZone id equals to FW (per path) client id */
6347 	fp->cl_qzone_id  = bnx2x_fp_qzone_id(fp);
6348 
6349 	/* init shortcut */
6350 	fp->ustorm_rx_prods_offset = bnx2x_rx_ustorm_prods_offset(fp);
6351 
6352 	/* Setup SB indices */
6353 	fp->rx_cons_sb = BNX2X_RX_SB_INDEX;
6354 
6355 	/* Configure Queue State object */
6356 	__set_bit(BNX2X_Q_TYPE_HAS_RX, &q_type);
6357 	__set_bit(BNX2X_Q_TYPE_HAS_TX, &q_type);
6358 
6359 	BUG_ON(fp->max_cos > BNX2X_MULTI_TX_COS);
6360 
6361 	/* init tx data */
6362 	for_each_cos_in_tx_queue(fp, cos) {
6363 		bnx2x_init_txdata(bp, fp->txdata_ptr[cos],
6364 				  CID_COS_TO_TX_ONLY_CID(fp->cid, cos, bp),
6365 				  FP_COS_TO_TXQ(fp, cos, bp),
6366 				  BNX2X_TX_SB_INDEX_BASE + cos, fp);
6367 		cids[cos] = fp->txdata_ptr[cos]->cid;
6368 	}
6369 
6370 	/* nothing more for vf to do here */
6371 	if (IS_VF(bp))
6372 		return;
6373 
6374 	bnx2x_init_sb(bp, fp->status_blk_mapping, BNX2X_VF_ID_INVALID, false,
6375 		      fp->fw_sb_id, fp->igu_sb_id);
6376 	bnx2x_update_fpsb_idx(fp);
6377 	bnx2x_init_queue_obj(bp, &bnx2x_sp_obj(bp, fp).q_obj, fp->cl_id, cids,
6378 			     fp->max_cos, BP_FUNC(bp), bnx2x_sp(bp, q_rdata),
6379 			     bnx2x_sp_mapping(bp, q_rdata), q_type);
6380 
6381 	/**
6382 	 * Configure classification DBs: Always enable Tx switching
6383 	 */
6384 	bnx2x_init_vlan_mac_fp_objs(fp, BNX2X_OBJ_TYPE_RX_TX);
6385 
6386 	DP(NETIF_MSG_IFUP,
6387 	   "queue[%d]:  bnx2x_init_sb(%p,%p)  cl_id %d  fw_sb %d  igu_sb %d\n",
6388 	   fp_idx, bp, fp->status_blk.e2_sb, fp->cl_id, fp->fw_sb_id,
6389 	   fp->igu_sb_id);
6390 }
6391 
6392 static void bnx2x_init_tx_ring_one(struct bnx2x_fp_txdata *txdata)
6393 {
6394 	int i;
6395 
6396 	for (i = 1; i <= NUM_TX_RINGS; i++) {
6397 		struct eth_tx_next_bd *tx_next_bd =
6398 			&txdata->tx_desc_ring[TX_DESC_CNT * i - 1].next_bd;
6399 
6400 		tx_next_bd->addr_hi =
6401 			cpu_to_le32(U64_HI(txdata->tx_desc_mapping +
6402 				    BCM_PAGE_SIZE*(i % NUM_TX_RINGS)));
6403 		tx_next_bd->addr_lo =
6404 			cpu_to_le32(U64_LO(txdata->tx_desc_mapping +
6405 				    BCM_PAGE_SIZE*(i % NUM_TX_RINGS)));
6406 	}
6407 
6408 	*txdata->tx_cons_sb = cpu_to_le16(0);
6409 
6410 	SET_FLAG(txdata->tx_db.data.header.header, DOORBELL_HDR_DB_TYPE, 1);
6411 	txdata->tx_db.data.zero_fill1 = 0;
6412 	txdata->tx_db.data.prod = 0;
6413 
6414 	txdata->tx_pkt_prod = 0;
6415 	txdata->tx_pkt_cons = 0;
6416 	txdata->tx_bd_prod = 0;
6417 	txdata->tx_bd_cons = 0;
6418 	txdata->tx_pkt = 0;
6419 }
6420 
6421 static void bnx2x_init_tx_rings_cnic(struct bnx2x *bp)
6422 {
6423 	int i;
6424 
6425 	for_each_tx_queue_cnic(bp, i)
6426 		bnx2x_init_tx_ring_one(bp->fp[i].txdata_ptr[0]);
6427 }
6428 
6429 static void bnx2x_init_tx_rings(struct bnx2x *bp)
6430 {
6431 	int i;
6432 	u8 cos;
6433 
6434 	for_each_eth_queue(bp, i)
6435 		for_each_cos_in_tx_queue(&bp->fp[i], cos)
6436 			bnx2x_init_tx_ring_one(bp->fp[i].txdata_ptr[cos]);
6437 }
6438 
6439 static void bnx2x_init_fcoe_fp(struct bnx2x *bp)
6440 {
6441 	struct bnx2x_fastpath *fp = bnx2x_fcoe_fp(bp);
6442 	unsigned long q_type = 0;
6443 
6444 	bnx2x_fcoe(bp, rx_queue) = BNX2X_NUM_ETH_QUEUES(bp);
6445 	bnx2x_fcoe(bp, cl_id) = bnx2x_cnic_eth_cl_id(bp,
6446 						     BNX2X_FCOE_ETH_CL_ID_IDX);
6447 	bnx2x_fcoe(bp, cid) = BNX2X_FCOE_ETH_CID(bp);
6448 	bnx2x_fcoe(bp, fw_sb_id) = DEF_SB_ID;
6449 	bnx2x_fcoe(bp, igu_sb_id) = bp->igu_dsb_id;
6450 	bnx2x_fcoe(bp, rx_cons_sb) = BNX2X_FCOE_L2_RX_INDEX;
6451 	bnx2x_init_txdata(bp, bnx2x_fcoe(bp, txdata_ptr[0]),
6452 			  fp->cid, FCOE_TXQ_IDX(bp), BNX2X_FCOE_L2_TX_INDEX,
6453 			  fp);
6454 
6455 	DP(NETIF_MSG_IFUP, "created fcoe tx data (fp index %d)\n", fp->index);
6456 
6457 	/* qZone id equals to FW (per path) client id */
6458 	bnx2x_fcoe(bp, cl_qzone_id) = bnx2x_fp_qzone_id(fp);
6459 	/* init shortcut */
6460 	bnx2x_fcoe(bp, ustorm_rx_prods_offset) =
6461 		bnx2x_rx_ustorm_prods_offset(fp);
6462 
6463 	/* Configure Queue State object */
6464 	__set_bit(BNX2X_Q_TYPE_HAS_RX, &q_type);
6465 	__set_bit(BNX2X_Q_TYPE_HAS_TX, &q_type);
6466 
6467 	/* No multi-CoS for FCoE L2 client */
6468 	BUG_ON(fp->max_cos != 1);
6469 
6470 	bnx2x_init_queue_obj(bp, &bnx2x_sp_obj(bp, fp).q_obj, fp->cl_id,
6471 			     &fp->cid, 1, BP_FUNC(bp), bnx2x_sp(bp, q_rdata),
6472 			     bnx2x_sp_mapping(bp, q_rdata), q_type);
6473 
6474 	DP(NETIF_MSG_IFUP,
6475 	   "queue[%d]: bnx2x_init_sb(%p,%p) cl_id %d fw_sb %d igu_sb %d\n",
6476 	   fp->index, bp, fp->status_blk.e2_sb, fp->cl_id, fp->fw_sb_id,
6477 	   fp->igu_sb_id);
6478 }
6479 
6480 void bnx2x_nic_init_cnic(struct bnx2x *bp)
6481 {
6482 	if (!NO_FCOE(bp))
6483 		bnx2x_init_fcoe_fp(bp);
6484 
6485 	bnx2x_init_sb(bp, bp->cnic_sb_mapping,
6486 		      BNX2X_VF_ID_INVALID, false,
6487 		      bnx2x_cnic_fw_sb_id(bp), bnx2x_cnic_igu_sb_id(bp));
6488 
6489 	/* ensure status block indices were read */
6490 	rmb();
6491 	bnx2x_init_rx_rings_cnic(bp);
6492 	bnx2x_init_tx_rings_cnic(bp);
6493 
6494 	/* flush all */
6495 	mb();
6496 }
6497 
6498 void bnx2x_pre_irq_nic_init(struct bnx2x *bp)
6499 {
6500 	int i;
6501 
6502 	/* Setup NIC internals and enable interrupts */
6503 	for_each_eth_queue(bp, i)
6504 		bnx2x_init_eth_fp(bp, i);
6505 
6506 	/* ensure status block indices were read */
6507 	rmb();
6508 	bnx2x_init_rx_rings(bp);
6509 	bnx2x_init_tx_rings(bp);
6510 
6511 	if (IS_PF(bp)) {
6512 		/* Initialize MOD_ABS interrupts */
6513 		bnx2x_init_mod_abs_int(bp, &bp->link_vars, bp->common.chip_id,
6514 				       bp->common.shmem_base,
6515 				       bp->common.shmem2_base, BP_PORT(bp));
6516 
6517 		/* initialize the default status block and sp ring */
6518 		bnx2x_init_def_sb(bp);
6519 		bnx2x_update_dsb_idx(bp);
6520 		bnx2x_init_sp_ring(bp);
6521 	} else {
6522 		bnx2x_memset_stats(bp);
6523 	}
6524 }
6525 
6526 void bnx2x_post_irq_nic_init(struct bnx2x *bp, u32 load_code)
6527 {
6528 	bnx2x_init_eq_ring(bp);
6529 	bnx2x_init_internal(bp, load_code);
6530 	bnx2x_pf_init(bp);
6531 	bnx2x_stats_init(bp);
6532 
6533 	/* flush all before enabling interrupts */
6534 	mb();
6535 
6536 	bnx2x_int_enable(bp);
6537 
6538 	/* Check for SPIO5 */
6539 	bnx2x_attn_int_deasserted0(bp,
6540 		REG_RD(bp, MISC_REG_AEU_AFTER_INVERT_1_FUNC_0 + BP_PORT(bp)*4) &
6541 				   AEU_INPUTS_ATTN_BITS_SPIO5);
6542 }
6543 
6544 /* gzip service functions */
6545 static int bnx2x_gunzip_init(struct bnx2x *bp)
6546 {
6547 	bp->gunzip_buf = dma_alloc_coherent(&bp->pdev->dev, FW_BUF_SIZE,
6548 					    &bp->gunzip_mapping, GFP_KERNEL);
6549 	if (bp->gunzip_buf  == NULL)
6550 		goto gunzip_nomem1;
6551 
6552 	bp->strm = kmalloc(sizeof(*bp->strm), GFP_KERNEL);
6553 	if (bp->strm  == NULL)
6554 		goto gunzip_nomem2;
6555 
6556 	bp->strm->workspace = vmalloc(zlib_inflate_workspacesize());
6557 	if (bp->strm->workspace == NULL)
6558 		goto gunzip_nomem3;
6559 
6560 	return 0;
6561 
6562 gunzip_nomem3:
6563 	kfree(bp->strm);
6564 	bp->strm = NULL;
6565 
6566 gunzip_nomem2:
6567 	dma_free_coherent(&bp->pdev->dev, FW_BUF_SIZE, bp->gunzip_buf,
6568 			  bp->gunzip_mapping);
6569 	bp->gunzip_buf = NULL;
6570 
6571 gunzip_nomem1:
6572 	BNX2X_ERR("Cannot allocate firmware buffer for un-compression\n");
6573 	return -ENOMEM;
6574 }
6575 
6576 static void bnx2x_gunzip_end(struct bnx2x *bp)
6577 {
6578 	if (bp->strm) {
6579 		vfree(bp->strm->workspace);
6580 		kfree(bp->strm);
6581 		bp->strm = NULL;
6582 	}
6583 
6584 	if (bp->gunzip_buf) {
6585 		dma_free_coherent(&bp->pdev->dev, FW_BUF_SIZE, bp->gunzip_buf,
6586 				  bp->gunzip_mapping);
6587 		bp->gunzip_buf = NULL;
6588 	}
6589 }
6590 
6591 static int bnx2x_gunzip(struct bnx2x *bp, const u8 *zbuf, int len)
6592 {
6593 	int n, rc;
6594 
6595 	/* check gzip header */
6596 	if ((zbuf[0] != 0x1f) || (zbuf[1] != 0x8b) || (zbuf[2] != Z_DEFLATED)) {
6597 		BNX2X_ERR("Bad gzip header\n");
6598 		return -EINVAL;
6599 	}
6600 
6601 	n = 10;
6602 
6603 #define FNAME				0x8
6604 
6605 	if (zbuf[3] & FNAME)
6606 		while ((zbuf[n++] != 0) && (n < len));
6607 
6608 	bp->strm->next_in = (typeof(bp->strm->next_in))zbuf + n;
6609 	bp->strm->avail_in = len - n;
6610 	bp->strm->next_out = bp->gunzip_buf;
6611 	bp->strm->avail_out = FW_BUF_SIZE;
6612 
6613 	rc = zlib_inflateInit2(bp->strm, -MAX_WBITS);
6614 	if (rc != Z_OK)
6615 		return rc;
6616 
6617 	rc = zlib_inflate(bp->strm, Z_FINISH);
6618 	if ((rc != Z_OK) && (rc != Z_STREAM_END))
6619 		netdev_err(bp->dev, "Firmware decompression error: %s\n",
6620 			   bp->strm->msg);
6621 
6622 	bp->gunzip_outlen = (FW_BUF_SIZE - bp->strm->avail_out);
6623 	if (bp->gunzip_outlen & 0x3)
6624 		netdev_err(bp->dev,
6625 			   "Firmware decompression error: gunzip_outlen (%d) not aligned\n",
6626 				bp->gunzip_outlen);
6627 	bp->gunzip_outlen >>= 2;
6628 
6629 	zlib_inflateEnd(bp->strm);
6630 
6631 	if (rc == Z_STREAM_END)
6632 		return 0;
6633 
6634 	return rc;
6635 }
6636 
6637 /* nic load/unload */
6638 
6639 /*
6640  * General service functions
6641  */
6642 
6643 /* send a NIG loopback debug packet */
6644 static void bnx2x_lb_pckt(struct bnx2x *bp)
6645 {
6646 	u32 wb_write[3];
6647 
6648 	/* Ethernet source and destination addresses */
6649 	wb_write[0] = 0x55555555;
6650 	wb_write[1] = 0x55555555;
6651 	wb_write[2] = 0x20;		/* SOP */
6652 	REG_WR_DMAE(bp, NIG_REG_DEBUG_PACKET_LB, wb_write, 3);
6653 
6654 	/* NON-IP protocol */
6655 	wb_write[0] = 0x09000000;
6656 	wb_write[1] = 0x55555555;
6657 	wb_write[2] = 0x10;		/* EOP, eop_bvalid = 0 */
6658 	REG_WR_DMAE(bp, NIG_REG_DEBUG_PACKET_LB, wb_write, 3);
6659 }
6660 
6661 /* some of the internal memories
6662  * are not directly readable from the driver
6663  * to test them we send debug packets
6664  */
6665 static int bnx2x_int_mem_test(struct bnx2x *bp)
6666 {
6667 	int factor;
6668 	int count, i;
6669 	u32 val = 0;
6670 
6671 	if (CHIP_REV_IS_FPGA(bp))
6672 		factor = 120;
6673 	else if (CHIP_REV_IS_EMUL(bp))
6674 		factor = 200;
6675 	else
6676 		factor = 1;
6677 
6678 	/* Disable inputs of parser neighbor blocks */
6679 	REG_WR(bp, TSDM_REG_ENABLE_IN1, 0x0);
6680 	REG_WR(bp, TCM_REG_PRS_IFEN, 0x0);
6681 	REG_WR(bp, CFC_REG_DEBUG0, 0x1);
6682 	REG_WR(bp, NIG_REG_PRS_REQ_IN_EN, 0x0);
6683 
6684 	/*  Write 0 to parser credits for CFC search request */
6685 	REG_WR(bp, PRS_REG_CFC_SEARCH_INITIAL_CREDIT, 0x0);
6686 
6687 	/* send Ethernet packet */
6688 	bnx2x_lb_pckt(bp);
6689 
6690 	/* TODO do i reset NIG statistic? */
6691 	/* Wait until NIG register shows 1 packet of size 0x10 */
6692 	count = 1000 * factor;
6693 	while (count) {
6694 
6695 		bnx2x_read_dmae(bp, NIG_REG_STAT2_BRB_OCTET, 2);
6696 		val = *bnx2x_sp(bp, wb_data[0]);
6697 		if (val == 0x10)
6698 			break;
6699 
6700 		usleep_range(10000, 20000);
6701 		count--;
6702 	}
6703 	if (val != 0x10) {
6704 		BNX2X_ERR("NIG timeout  val = 0x%x\n", val);
6705 		return -1;
6706 	}
6707 
6708 	/* Wait until PRS register shows 1 packet */
6709 	count = 1000 * factor;
6710 	while (count) {
6711 		val = REG_RD(bp, PRS_REG_NUM_OF_PACKETS);
6712 		if (val == 1)
6713 			break;
6714 
6715 		usleep_range(10000, 20000);
6716 		count--;
6717 	}
6718 	if (val != 0x1) {
6719 		BNX2X_ERR("PRS timeout val = 0x%x\n", val);
6720 		return -2;
6721 	}
6722 
6723 	/* Reset and init BRB, PRS */
6724 	REG_WR(bp, GRCBASE_MISC + MISC_REGISTERS_RESET_REG_1_CLEAR, 0x03);
6725 	msleep(50);
6726 	REG_WR(bp, GRCBASE_MISC + MISC_REGISTERS_RESET_REG_1_SET, 0x03);
6727 	msleep(50);
6728 	bnx2x_init_block(bp, BLOCK_BRB1, PHASE_COMMON);
6729 	bnx2x_init_block(bp, BLOCK_PRS, PHASE_COMMON);
6730 
6731 	DP(NETIF_MSG_HW, "part2\n");
6732 
6733 	/* Disable inputs of parser neighbor blocks */
6734 	REG_WR(bp, TSDM_REG_ENABLE_IN1, 0x0);
6735 	REG_WR(bp, TCM_REG_PRS_IFEN, 0x0);
6736 	REG_WR(bp, CFC_REG_DEBUG0, 0x1);
6737 	REG_WR(bp, NIG_REG_PRS_REQ_IN_EN, 0x0);
6738 
6739 	/* Write 0 to parser credits for CFC search request */
6740 	REG_WR(bp, PRS_REG_CFC_SEARCH_INITIAL_CREDIT, 0x0);
6741 
6742 	/* send 10 Ethernet packets */
6743 	for (i = 0; i < 10; i++)
6744 		bnx2x_lb_pckt(bp);
6745 
6746 	/* Wait until NIG register shows 10 + 1
6747 	   packets of size 11*0x10 = 0xb0 */
6748 	count = 1000 * factor;
6749 	while (count) {
6750 
6751 		bnx2x_read_dmae(bp, NIG_REG_STAT2_BRB_OCTET, 2);
6752 		val = *bnx2x_sp(bp, wb_data[0]);
6753 		if (val == 0xb0)
6754 			break;
6755 
6756 		usleep_range(10000, 20000);
6757 		count--;
6758 	}
6759 	if (val != 0xb0) {
6760 		BNX2X_ERR("NIG timeout  val = 0x%x\n", val);
6761 		return -3;
6762 	}
6763 
6764 	/* Wait until PRS register shows 2 packets */
6765 	val = REG_RD(bp, PRS_REG_NUM_OF_PACKETS);
6766 	if (val != 2)
6767 		BNX2X_ERR("PRS timeout  val = 0x%x\n", val);
6768 
6769 	/* Write 1 to parser credits for CFC search request */
6770 	REG_WR(bp, PRS_REG_CFC_SEARCH_INITIAL_CREDIT, 0x1);
6771 
6772 	/* Wait until PRS register shows 3 packets */
6773 	msleep(10 * factor);
6774 	/* Wait until NIG register shows 1 packet of size 0x10 */
6775 	val = REG_RD(bp, PRS_REG_NUM_OF_PACKETS);
6776 	if (val != 3)
6777 		BNX2X_ERR("PRS timeout  val = 0x%x\n", val);
6778 
6779 	/* clear NIG EOP FIFO */
6780 	for (i = 0; i < 11; i++)
6781 		REG_RD(bp, NIG_REG_INGRESS_EOP_LB_FIFO);
6782 	val = REG_RD(bp, NIG_REG_INGRESS_EOP_LB_EMPTY);
6783 	if (val != 1) {
6784 		BNX2X_ERR("clear of NIG failed\n");
6785 		return -4;
6786 	}
6787 
6788 	/* Reset and init BRB, PRS, NIG */
6789 	REG_WR(bp, GRCBASE_MISC + MISC_REGISTERS_RESET_REG_1_CLEAR, 0x03);
6790 	msleep(50);
6791 	REG_WR(bp, GRCBASE_MISC + MISC_REGISTERS_RESET_REG_1_SET, 0x03);
6792 	msleep(50);
6793 	bnx2x_init_block(bp, BLOCK_BRB1, PHASE_COMMON);
6794 	bnx2x_init_block(bp, BLOCK_PRS, PHASE_COMMON);
6795 	if (!CNIC_SUPPORT(bp))
6796 		/* set NIC mode */
6797 		REG_WR(bp, PRS_REG_NIC_MODE, 1);
6798 
6799 	/* Enable inputs of parser neighbor blocks */
6800 	REG_WR(bp, TSDM_REG_ENABLE_IN1, 0x7fffffff);
6801 	REG_WR(bp, TCM_REG_PRS_IFEN, 0x1);
6802 	REG_WR(bp, CFC_REG_DEBUG0, 0x0);
6803 	REG_WR(bp, NIG_REG_PRS_REQ_IN_EN, 0x1);
6804 
6805 	DP(NETIF_MSG_HW, "done\n");
6806 
6807 	return 0; /* OK */
6808 }
6809 
6810 static void bnx2x_enable_blocks_attention(struct bnx2x *bp)
6811 {
6812 	u32 val;
6813 
6814 	REG_WR(bp, PXP_REG_PXP_INT_MASK_0, 0);
6815 	if (!CHIP_IS_E1x(bp))
6816 		REG_WR(bp, PXP_REG_PXP_INT_MASK_1, 0x40);
6817 	else
6818 		REG_WR(bp, PXP_REG_PXP_INT_MASK_1, 0);
6819 	REG_WR(bp, DORQ_REG_DORQ_INT_MASK, 0);
6820 	REG_WR(bp, CFC_REG_CFC_INT_MASK, 0);
6821 	/*
6822 	 * mask read length error interrupts in brb for parser
6823 	 * (parsing unit and 'checksum and crc' unit)
6824 	 * these errors are legal (PU reads fixed length and CAC can cause
6825 	 * read length error on truncated packets)
6826 	 */
6827 	REG_WR(bp, BRB1_REG_BRB1_INT_MASK, 0xFC00);
6828 	REG_WR(bp, QM_REG_QM_INT_MASK, 0);
6829 	REG_WR(bp, TM_REG_TM_INT_MASK, 0);
6830 	REG_WR(bp, XSDM_REG_XSDM_INT_MASK_0, 0);
6831 	REG_WR(bp, XSDM_REG_XSDM_INT_MASK_1, 0);
6832 	REG_WR(bp, XCM_REG_XCM_INT_MASK, 0);
6833 /*	REG_WR(bp, XSEM_REG_XSEM_INT_MASK_0, 0); */
6834 /*	REG_WR(bp, XSEM_REG_XSEM_INT_MASK_1, 0); */
6835 	REG_WR(bp, USDM_REG_USDM_INT_MASK_0, 0);
6836 	REG_WR(bp, USDM_REG_USDM_INT_MASK_1, 0);
6837 	REG_WR(bp, UCM_REG_UCM_INT_MASK, 0);
6838 /*	REG_WR(bp, USEM_REG_USEM_INT_MASK_0, 0); */
6839 /*	REG_WR(bp, USEM_REG_USEM_INT_MASK_1, 0); */
6840 	REG_WR(bp, GRCBASE_UPB + PB_REG_PB_INT_MASK, 0);
6841 	REG_WR(bp, CSDM_REG_CSDM_INT_MASK_0, 0);
6842 	REG_WR(bp, CSDM_REG_CSDM_INT_MASK_1, 0);
6843 	REG_WR(bp, CCM_REG_CCM_INT_MASK, 0);
6844 /*	REG_WR(bp, CSEM_REG_CSEM_INT_MASK_0, 0); */
6845 /*	REG_WR(bp, CSEM_REG_CSEM_INT_MASK_1, 0); */
6846 
6847 	val = PXP2_PXP2_INT_MASK_0_REG_PGL_CPL_AFT  |
6848 		PXP2_PXP2_INT_MASK_0_REG_PGL_CPL_OF |
6849 		PXP2_PXP2_INT_MASK_0_REG_PGL_PCIE_ATTN;
6850 	if (!CHIP_IS_E1x(bp))
6851 		val |= PXP2_PXP2_INT_MASK_0_REG_PGL_READ_BLOCKED |
6852 			PXP2_PXP2_INT_MASK_0_REG_PGL_WRITE_BLOCKED;
6853 	REG_WR(bp, PXP2_REG_PXP2_INT_MASK_0, val);
6854 
6855 	REG_WR(bp, TSDM_REG_TSDM_INT_MASK_0, 0);
6856 	REG_WR(bp, TSDM_REG_TSDM_INT_MASK_1, 0);
6857 	REG_WR(bp, TCM_REG_TCM_INT_MASK, 0);
6858 /*	REG_WR(bp, TSEM_REG_TSEM_INT_MASK_0, 0); */
6859 
6860 	if (!CHIP_IS_E1x(bp))
6861 		/* enable VFC attentions: bits 11 and 12, bits 31:13 reserved */
6862 		REG_WR(bp, TSEM_REG_TSEM_INT_MASK_1, 0x07ff);
6863 
6864 	REG_WR(bp, CDU_REG_CDU_INT_MASK, 0);
6865 	REG_WR(bp, DMAE_REG_DMAE_INT_MASK, 0);
6866 /*	REG_WR(bp, MISC_REG_MISC_INT_MASK, 0); */
6867 	REG_WR(bp, PBF_REG_PBF_INT_MASK, 0x18);		/* bit 3,4 masked */
6868 }
6869 
6870 static void bnx2x_reset_common(struct bnx2x *bp)
6871 {
6872 	u32 val = 0x1400;
6873 
6874 	/* reset_common */
6875 	REG_WR(bp, GRCBASE_MISC + MISC_REGISTERS_RESET_REG_1_CLEAR,
6876 	       0xd3ffff7f);
6877 
6878 	if (CHIP_IS_E3(bp)) {
6879 		val |= MISC_REGISTERS_RESET_REG_2_MSTAT0;
6880 		val |= MISC_REGISTERS_RESET_REG_2_MSTAT1;
6881 	}
6882 
6883 	REG_WR(bp, GRCBASE_MISC + MISC_REGISTERS_RESET_REG_2_CLEAR, val);
6884 }
6885 
6886 static void bnx2x_setup_dmae(struct bnx2x *bp)
6887 {
6888 	bp->dmae_ready = 0;
6889 	spin_lock_init(&bp->dmae_lock);
6890 }
6891 
6892 static void bnx2x_init_pxp(struct bnx2x *bp)
6893 {
6894 	u16 devctl;
6895 	int r_order, w_order;
6896 
6897 	pcie_capability_read_word(bp->pdev, PCI_EXP_DEVCTL, &devctl);
6898 	DP(NETIF_MSG_HW, "read 0x%x from devctl\n", devctl);
6899 	w_order = ((devctl & PCI_EXP_DEVCTL_PAYLOAD) >> 5);
6900 	if (bp->mrrs == -1)
6901 		r_order = ((devctl & PCI_EXP_DEVCTL_READRQ) >> 12);
6902 	else {
6903 		DP(NETIF_MSG_HW, "force read order to %d\n", bp->mrrs);
6904 		r_order = bp->mrrs;
6905 	}
6906 
6907 	bnx2x_init_pxp_arb(bp, r_order, w_order);
6908 }
6909 
6910 static void bnx2x_setup_fan_failure_detection(struct bnx2x *bp)
6911 {
6912 	int is_required;
6913 	u32 val;
6914 	int port;
6915 
6916 	if (BP_NOMCP(bp))
6917 		return;
6918 
6919 	is_required = 0;
6920 	val = SHMEM_RD(bp, dev_info.shared_hw_config.config2) &
6921 	      SHARED_HW_CFG_FAN_FAILURE_MASK;
6922 
6923 	if (val == SHARED_HW_CFG_FAN_FAILURE_ENABLED)
6924 		is_required = 1;
6925 
6926 	/*
6927 	 * The fan failure mechanism is usually related to the PHY type since
6928 	 * the power consumption of the board is affected by the PHY. Currently,
6929 	 * fan is required for most designs with SFX7101, BCM8727 and BCM8481.
6930 	 */
6931 	else if (val == SHARED_HW_CFG_FAN_FAILURE_PHY_TYPE)
6932 		for (port = PORT_0; port < PORT_MAX; port++) {
6933 			is_required |=
6934 				bnx2x_fan_failure_det_req(
6935 					bp,
6936 					bp->common.shmem_base,
6937 					bp->common.shmem2_base,
6938 					port);
6939 		}
6940 
6941 	DP(NETIF_MSG_HW, "fan detection setting: %d\n", is_required);
6942 
6943 	if (is_required == 0)
6944 		return;
6945 
6946 	/* Fan failure is indicated by SPIO 5 */
6947 	bnx2x_set_spio(bp, MISC_SPIO_SPIO5, MISC_SPIO_INPUT_HI_Z);
6948 
6949 	/* set to active low mode */
6950 	val = REG_RD(bp, MISC_REG_SPIO_INT);
6951 	val |= (MISC_SPIO_SPIO5 << MISC_SPIO_INT_OLD_SET_POS);
6952 	REG_WR(bp, MISC_REG_SPIO_INT, val);
6953 
6954 	/* enable interrupt to signal the IGU */
6955 	val = REG_RD(bp, MISC_REG_SPIO_EVENT_EN);
6956 	val |= MISC_SPIO_SPIO5;
6957 	REG_WR(bp, MISC_REG_SPIO_EVENT_EN, val);
6958 }
6959 
6960 void bnx2x_pf_disable(struct bnx2x *bp)
6961 {
6962 	u32 val = REG_RD(bp, IGU_REG_PF_CONFIGURATION);
6963 	val &= ~IGU_PF_CONF_FUNC_EN;
6964 
6965 	REG_WR(bp, IGU_REG_PF_CONFIGURATION, val);
6966 	REG_WR(bp, PGLUE_B_REG_INTERNAL_PFID_ENABLE_MASTER, 0);
6967 	REG_WR(bp, CFC_REG_WEAK_ENABLE_PF, 0);
6968 }
6969 
6970 static void bnx2x__common_init_phy(struct bnx2x *bp)
6971 {
6972 	u32 shmem_base[2], shmem2_base[2];
6973 	/* Avoid common init in case MFW supports LFA */
6974 	if (SHMEM2_RD(bp, size) >
6975 	    (u32)offsetof(struct shmem2_region, lfa_host_addr[BP_PORT(bp)]))
6976 		return;
6977 	shmem_base[0] =  bp->common.shmem_base;
6978 	shmem2_base[0] = bp->common.shmem2_base;
6979 	if (!CHIP_IS_E1x(bp)) {
6980 		shmem_base[1] =
6981 			SHMEM2_RD(bp, other_shmem_base_addr);
6982 		shmem2_base[1] =
6983 			SHMEM2_RD(bp, other_shmem2_base_addr);
6984 	}
6985 	bnx2x_acquire_phy_lock(bp);
6986 	bnx2x_common_init_phy(bp, shmem_base, shmem2_base,
6987 			      bp->common.chip_id);
6988 	bnx2x_release_phy_lock(bp);
6989 }
6990 
6991 static void bnx2x_config_endianity(struct bnx2x *bp, u32 val)
6992 {
6993 	REG_WR(bp, PXP2_REG_RQ_QM_ENDIAN_M, val);
6994 	REG_WR(bp, PXP2_REG_RQ_TM_ENDIAN_M, val);
6995 	REG_WR(bp, PXP2_REG_RQ_SRC_ENDIAN_M, val);
6996 	REG_WR(bp, PXP2_REG_RQ_CDU_ENDIAN_M, val);
6997 	REG_WR(bp, PXP2_REG_RQ_DBG_ENDIAN_M, val);
6998 
6999 	/* make sure this value is 0 */
7000 	REG_WR(bp, PXP2_REG_RQ_HC_ENDIAN_M, 0);
7001 
7002 	REG_WR(bp, PXP2_REG_RD_QM_SWAP_MODE, val);
7003 	REG_WR(bp, PXP2_REG_RD_TM_SWAP_MODE, val);
7004 	REG_WR(bp, PXP2_REG_RD_SRC_SWAP_MODE, val);
7005 	REG_WR(bp, PXP2_REG_RD_CDURD_SWAP_MODE, val);
7006 }
7007 
7008 static void bnx2x_set_endianity(struct bnx2x *bp)
7009 {
7010 #ifdef __BIG_ENDIAN
7011 	bnx2x_config_endianity(bp, 1);
7012 #else
7013 	bnx2x_config_endianity(bp, 0);
7014 #endif
7015 }
7016 
7017 static void bnx2x_reset_endianity(struct bnx2x *bp)
7018 {
7019 	bnx2x_config_endianity(bp, 0);
7020 }
7021 
7022 /**
7023  * bnx2x_init_hw_common - initialize the HW at the COMMON phase.
7024  *
7025  * @bp:		driver handle
7026  */
7027 static int bnx2x_init_hw_common(struct bnx2x *bp)
7028 {
7029 	u32 val;
7030 
7031 	DP(NETIF_MSG_HW, "starting common init  func %d\n", BP_ABS_FUNC(bp));
7032 
7033 	/*
7034 	 * take the RESET lock to protect undi_unload flow from accessing
7035 	 * registers while we're resetting the chip
7036 	 */
7037 	bnx2x_acquire_hw_lock(bp, HW_LOCK_RESOURCE_RESET);
7038 
7039 	bnx2x_reset_common(bp);
7040 	REG_WR(bp, GRCBASE_MISC + MISC_REGISTERS_RESET_REG_1_SET, 0xffffffff);
7041 
7042 	val = 0xfffc;
7043 	if (CHIP_IS_E3(bp)) {
7044 		val |= MISC_REGISTERS_RESET_REG_2_MSTAT0;
7045 		val |= MISC_REGISTERS_RESET_REG_2_MSTAT1;
7046 	}
7047 	REG_WR(bp, GRCBASE_MISC + MISC_REGISTERS_RESET_REG_2_SET, val);
7048 
7049 	bnx2x_release_hw_lock(bp, HW_LOCK_RESOURCE_RESET);
7050 
7051 	bnx2x_init_block(bp, BLOCK_MISC, PHASE_COMMON);
7052 
7053 	if (!CHIP_IS_E1x(bp)) {
7054 		u8 abs_func_id;
7055 
7056 		/**
7057 		 * 4-port mode or 2-port mode we need to turn of master-enable
7058 		 * for everyone, after that, turn it back on for self.
7059 		 * so, we disregard multi-function or not, and always disable
7060 		 * for all functions on the given path, this means 0,2,4,6 for
7061 		 * path 0 and 1,3,5,7 for path 1
7062 		 */
7063 		for (abs_func_id = BP_PATH(bp);
7064 		     abs_func_id < E2_FUNC_MAX*2; abs_func_id += 2) {
7065 			if (abs_func_id == BP_ABS_FUNC(bp)) {
7066 				REG_WR(bp,
7067 				    PGLUE_B_REG_INTERNAL_PFID_ENABLE_MASTER,
7068 				    1);
7069 				continue;
7070 			}
7071 
7072 			bnx2x_pretend_func(bp, abs_func_id);
7073 			/* clear pf enable */
7074 			bnx2x_pf_disable(bp);
7075 			bnx2x_pretend_func(bp, BP_ABS_FUNC(bp));
7076 		}
7077 	}
7078 
7079 	bnx2x_init_block(bp, BLOCK_PXP, PHASE_COMMON);
7080 	if (CHIP_IS_E1(bp)) {
7081 		/* enable HW interrupt from PXP on USDM overflow
7082 		   bit 16 on INT_MASK_0 */
7083 		REG_WR(bp, PXP_REG_PXP_INT_MASK_0, 0);
7084 	}
7085 
7086 	bnx2x_init_block(bp, BLOCK_PXP2, PHASE_COMMON);
7087 	bnx2x_init_pxp(bp);
7088 	bnx2x_set_endianity(bp);
7089 	bnx2x_ilt_init_page_size(bp, INITOP_SET);
7090 
7091 	if (CHIP_REV_IS_FPGA(bp) && CHIP_IS_E1H(bp))
7092 		REG_WR(bp, PXP2_REG_PGL_TAGS_LIMIT, 0x1);
7093 
7094 	/* let the HW do it's magic ... */
7095 	msleep(100);
7096 	/* finish PXP init */
7097 	val = REG_RD(bp, PXP2_REG_RQ_CFG_DONE);
7098 	if (val != 1) {
7099 		BNX2X_ERR("PXP2 CFG failed\n");
7100 		return -EBUSY;
7101 	}
7102 	val = REG_RD(bp, PXP2_REG_RD_INIT_DONE);
7103 	if (val != 1) {
7104 		BNX2X_ERR("PXP2 RD_INIT failed\n");
7105 		return -EBUSY;
7106 	}
7107 
7108 	/* Timers bug workaround E2 only. We need to set the entire ILT to
7109 	 * have entries with value "0" and valid bit on.
7110 	 * This needs to be done by the first PF that is loaded in a path
7111 	 * (i.e. common phase)
7112 	 */
7113 	if (!CHIP_IS_E1x(bp)) {
7114 /* In E2 there is a bug in the timers block that can cause function 6 / 7
7115  * (i.e. vnic3) to start even if it is marked as "scan-off".
7116  * This occurs when a different function (func2,3) is being marked
7117  * as "scan-off". Real-life scenario for example: if a driver is being
7118  * load-unloaded while func6,7 are down. This will cause the timer to access
7119  * the ilt, translate to a logical address and send a request to read/write.
7120  * Since the ilt for the function that is down is not valid, this will cause
7121  * a translation error which is unrecoverable.
7122  * The Workaround is intended to make sure that when this happens nothing fatal
7123  * will occur. The workaround:
7124  *	1.  First PF driver which loads on a path will:
7125  *		a.  After taking the chip out of reset, by using pretend,
7126  *		    it will write "0" to the following registers of
7127  *		    the other vnics.
7128  *		    REG_WR(pdev, PGLUE_B_REG_INTERNAL_PFID_ENABLE_MASTER, 0);
7129  *		    REG_WR(pdev, CFC_REG_WEAK_ENABLE_PF,0);
7130  *		    REG_WR(pdev, CFC_REG_STRONG_ENABLE_PF,0);
7131  *		    And for itself it will write '1' to
7132  *		    PGLUE_B_REG_INTERNAL_PFID_ENABLE_MASTER to enable
7133  *		    dmae-operations (writing to pram for example.)
7134  *		    note: can be done for only function 6,7 but cleaner this
7135  *			  way.
7136  *		b.  Write zero+valid to the entire ILT.
7137  *		c.  Init the first_timers_ilt_entry, last_timers_ilt_entry of
7138  *		    VNIC3 (of that port). The range allocated will be the
7139  *		    entire ILT. This is needed to prevent  ILT range error.
7140  *	2.  Any PF driver load flow:
7141  *		a.  ILT update with the physical addresses of the allocated
7142  *		    logical pages.
7143  *		b.  Wait 20msec. - note that this timeout is needed to make
7144  *		    sure there are no requests in one of the PXP internal
7145  *		    queues with "old" ILT addresses.
7146  *		c.  PF enable in the PGLC.
7147  *		d.  Clear the was_error of the PF in the PGLC. (could have
7148  *		    occurred while driver was down)
7149  *		e.  PF enable in the CFC (WEAK + STRONG)
7150  *		f.  Timers scan enable
7151  *	3.  PF driver unload flow:
7152  *		a.  Clear the Timers scan_en.
7153  *		b.  Polling for scan_on=0 for that PF.
7154  *		c.  Clear the PF enable bit in the PXP.
7155  *		d.  Clear the PF enable in the CFC (WEAK + STRONG)
7156  *		e.  Write zero+valid to all ILT entries (The valid bit must
7157  *		    stay set)
7158  *		f.  If this is VNIC 3 of a port then also init
7159  *		    first_timers_ilt_entry to zero and last_timers_ilt_entry
7160  *		    to the last entry in the ILT.
7161  *
7162  *	Notes:
7163  *	Currently the PF error in the PGLC is non recoverable.
7164  *	In the future the there will be a recovery routine for this error.
7165  *	Currently attention is masked.
7166  *	Having an MCP lock on the load/unload process does not guarantee that
7167  *	there is no Timer disable during Func6/7 enable. This is because the
7168  *	Timers scan is currently being cleared by the MCP on FLR.
7169  *	Step 2.d can be done only for PF6/7 and the driver can also check if
7170  *	there is error before clearing it. But the flow above is simpler and
7171  *	more general.
7172  *	All ILT entries are written by zero+valid and not just PF6/7
7173  *	ILT entries since in the future the ILT entries allocation for
7174  *	PF-s might be dynamic.
7175  */
7176 		struct ilt_client_info ilt_cli;
7177 		struct bnx2x_ilt ilt;
7178 		memset(&ilt_cli, 0, sizeof(struct ilt_client_info));
7179 		memset(&ilt, 0, sizeof(struct bnx2x_ilt));
7180 
7181 		/* initialize dummy TM client */
7182 		ilt_cli.start = 0;
7183 		ilt_cli.end = ILT_NUM_PAGE_ENTRIES - 1;
7184 		ilt_cli.client_num = ILT_CLIENT_TM;
7185 
7186 		/* Step 1: set zeroes to all ilt page entries with valid bit on
7187 		 * Step 2: set the timers first/last ilt entry to point
7188 		 * to the entire range to prevent ILT range error for 3rd/4th
7189 		 * vnic	(this code assumes existence of the vnic)
7190 		 *
7191 		 * both steps performed by call to bnx2x_ilt_client_init_op()
7192 		 * with dummy TM client
7193 		 *
7194 		 * we must use pretend since PXP2_REG_RQ_##blk##_FIRST_ILT
7195 		 * and his brother are split registers
7196 		 */
7197 		bnx2x_pretend_func(bp, (BP_PATH(bp) + 6));
7198 		bnx2x_ilt_client_init_op_ilt(bp, &ilt, &ilt_cli, INITOP_CLEAR);
7199 		bnx2x_pretend_func(bp, BP_ABS_FUNC(bp));
7200 
7201 		REG_WR(bp, PXP2_REG_RQ_DRAM_ALIGN, BNX2X_PXP_DRAM_ALIGN);
7202 		REG_WR(bp, PXP2_REG_RQ_DRAM_ALIGN_RD, BNX2X_PXP_DRAM_ALIGN);
7203 		REG_WR(bp, PXP2_REG_RQ_DRAM_ALIGN_SEL, 1);
7204 	}
7205 
7206 	REG_WR(bp, PXP2_REG_RQ_DISABLE_INPUTS, 0);
7207 	REG_WR(bp, PXP2_REG_RD_DISABLE_INPUTS, 0);
7208 
7209 	if (!CHIP_IS_E1x(bp)) {
7210 		int factor = CHIP_REV_IS_EMUL(bp) ? 1000 :
7211 				(CHIP_REV_IS_FPGA(bp) ? 400 : 0);
7212 		bnx2x_init_block(bp, BLOCK_PGLUE_B, PHASE_COMMON);
7213 
7214 		bnx2x_init_block(bp, BLOCK_ATC, PHASE_COMMON);
7215 
7216 		/* let the HW do it's magic ... */
7217 		do {
7218 			msleep(200);
7219 			val = REG_RD(bp, ATC_REG_ATC_INIT_DONE);
7220 		} while (factor-- && (val != 1));
7221 
7222 		if (val != 1) {
7223 			BNX2X_ERR("ATC_INIT failed\n");
7224 			return -EBUSY;
7225 		}
7226 	}
7227 
7228 	bnx2x_init_block(bp, BLOCK_DMAE, PHASE_COMMON);
7229 
7230 	bnx2x_iov_init_dmae(bp);
7231 
7232 	/* clean the DMAE memory */
7233 	bp->dmae_ready = 1;
7234 	bnx2x_init_fill(bp, TSEM_REG_PRAM, 0, 8, 1);
7235 
7236 	bnx2x_init_block(bp, BLOCK_TCM, PHASE_COMMON);
7237 
7238 	bnx2x_init_block(bp, BLOCK_UCM, PHASE_COMMON);
7239 
7240 	bnx2x_init_block(bp, BLOCK_CCM, PHASE_COMMON);
7241 
7242 	bnx2x_init_block(bp, BLOCK_XCM, PHASE_COMMON);
7243 
7244 	bnx2x_read_dmae(bp, XSEM_REG_PASSIVE_BUFFER, 3);
7245 	bnx2x_read_dmae(bp, CSEM_REG_PASSIVE_BUFFER, 3);
7246 	bnx2x_read_dmae(bp, TSEM_REG_PASSIVE_BUFFER, 3);
7247 	bnx2x_read_dmae(bp, USEM_REG_PASSIVE_BUFFER, 3);
7248 
7249 	bnx2x_init_block(bp, BLOCK_QM, PHASE_COMMON);
7250 
7251 	/* QM queues pointers table */
7252 	bnx2x_qm_init_ptr_table(bp, bp->qm_cid_count, INITOP_SET);
7253 
7254 	/* soft reset pulse */
7255 	REG_WR(bp, QM_REG_SOFT_RESET, 1);
7256 	REG_WR(bp, QM_REG_SOFT_RESET, 0);
7257 
7258 	if (CNIC_SUPPORT(bp))
7259 		bnx2x_init_block(bp, BLOCK_TM, PHASE_COMMON);
7260 
7261 	bnx2x_init_block(bp, BLOCK_DORQ, PHASE_COMMON);
7262 
7263 	if (!CHIP_REV_IS_SLOW(bp))
7264 		/* enable hw interrupt from doorbell Q */
7265 		REG_WR(bp, DORQ_REG_DORQ_INT_MASK, 0);
7266 
7267 	bnx2x_init_block(bp, BLOCK_BRB1, PHASE_COMMON);
7268 
7269 	bnx2x_init_block(bp, BLOCK_PRS, PHASE_COMMON);
7270 	REG_WR(bp, PRS_REG_A_PRSU_20, 0xf);
7271 
7272 	if (!CHIP_IS_E1(bp))
7273 		REG_WR(bp, PRS_REG_E1HOV_MODE, bp->path_has_ovlan);
7274 
7275 	if (!CHIP_IS_E1x(bp) && !CHIP_IS_E3B0(bp)) {
7276 		if (IS_MF_AFEX(bp)) {
7277 			/* configure that VNTag and VLAN headers must be
7278 			 * received in afex mode
7279 			 */
7280 			REG_WR(bp, PRS_REG_HDRS_AFTER_BASIC, 0xE);
7281 			REG_WR(bp, PRS_REG_MUST_HAVE_HDRS, 0xA);
7282 			REG_WR(bp, PRS_REG_HDRS_AFTER_TAG_0, 0x6);
7283 			REG_WR(bp, PRS_REG_TAG_ETHERTYPE_0, 0x8926);
7284 			REG_WR(bp, PRS_REG_TAG_LEN_0, 0x4);
7285 		} else {
7286 			/* Bit-map indicating which L2 hdrs may appear
7287 			 * after the basic Ethernet header
7288 			 */
7289 			REG_WR(bp, PRS_REG_HDRS_AFTER_BASIC,
7290 			       bp->path_has_ovlan ? 7 : 6);
7291 		}
7292 	}
7293 
7294 	bnx2x_init_block(bp, BLOCK_TSDM, PHASE_COMMON);
7295 	bnx2x_init_block(bp, BLOCK_CSDM, PHASE_COMMON);
7296 	bnx2x_init_block(bp, BLOCK_USDM, PHASE_COMMON);
7297 	bnx2x_init_block(bp, BLOCK_XSDM, PHASE_COMMON);
7298 
7299 	if (!CHIP_IS_E1x(bp)) {
7300 		/* reset VFC memories */
7301 		REG_WR(bp, TSEM_REG_FAST_MEMORY + VFC_REG_MEMORIES_RST,
7302 			   VFC_MEMORIES_RST_REG_CAM_RST |
7303 			   VFC_MEMORIES_RST_REG_RAM_RST);
7304 		REG_WR(bp, XSEM_REG_FAST_MEMORY + VFC_REG_MEMORIES_RST,
7305 			   VFC_MEMORIES_RST_REG_CAM_RST |
7306 			   VFC_MEMORIES_RST_REG_RAM_RST);
7307 
7308 		msleep(20);
7309 	}
7310 
7311 	bnx2x_init_block(bp, BLOCK_TSEM, PHASE_COMMON);
7312 	bnx2x_init_block(bp, BLOCK_USEM, PHASE_COMMON);
7313 	bnx2x_init_block(bp, BLOCK_CSEM, PHASE_COMMON);
7314 	bnx2x_init_block(bp, BLOCK_XSEM, PHASE_COMMON);
7315 
7316 	/* sync semi rtc */
7317 	REG_WR(bp, GRCBASE_MISC + MISC_REGISTERS_RESET_REG_1_CLEAR,
7318 	       0x80000000);
7319 	REG_WR(bp, GRCBASE_MISC + MISC_REGISTERS_RESET_REG_1_SET,
7320 	       0x80000000);
7321 
7322 	bnx2x_init_block(bp, BLOCK_UPB, PHASE_COMMON);
7323 	bnx2x_init_block(bp, BLOCK_XPB, PHASE_COMMON);
7324 	bnx2x_init_block(bp, BLOCK_PBF, PHASE_COMMON);
7325 
7326 	if (!CHIP_IS_E1x(bp)) {
7327 		if (IS_MF_AFEX(bp)) {
7328 			/* configure that VNTag and VLAN headers must be
7329 			 * sent in afex mode
7330 			 */
7331 			REG_WR(bp, PBF_REG_HDRS_AFTER_BASIC, 0xE);
7332 			REG_WR(bp, PBF_REG_MUST_HAVE_HDRS, 0xA);
7333 			REG_WR(bp, PBF_REG_HDRS_AFTER_TAG_0, 0x6);
7334 			REG_WR(bp, PBF_REG_TAG_ETHERTYPE_0, 0x8926);
7335 			REG_WR(bp, PBF_REG_TAG_LEN_0, 0x4);
7336 		} else {
7337 			REG_WR(bp, PBF_REG_HDRS_AFTER_BASIC,
7338 			       bp->path_has_ovlan ? 7 : 6);
7339 		}
7340 	}
7341 
7342 	REG_WR(bp, SRC_REG_SOFT_RST, 1);
7343 
7344 	bnx2x_init_block(bp, BLOCK_SRC, PHASE_COMMON);
7345 
7346 	if (CNIC_SUPPORT(bp)) {
7347 		REG_WR(bp, SRC_REG_KEYSEARCH_0, 0x63285672);
7348 		REG_WR(bp, SRC_REG_KEYSEARCH_1, 0x24b8f2cc);
7349 		REG_WR(bp, SRC_REG_KEYSEARCH_2, 0x223aef9b);
7350 		REG_WR(bp, SRC_REG_KEYSEARCH_3, 0x26001e3a);
7351 		REG_WR(bp, SRC_REG_KEYSEARCH_4, 0x7ae91116);
7352 		REG_WR(bp, SRC_REG_KEYSEARCH_5, 0x5ce5230b);
7353 		REG_WR(bp, SRC_REG_KEYSEARCH_6, 0x298d8adf);
7354 		REG_WR(bp, SRC_REG_KEYSEARCH_7, 0x6eb0ff09);
7355 		REG_WR(bp, SRC_REG_KEYSEARCH_8, 0x1830f82f);
7356 		REG_WR(bp, SRC_REG_KEYSEARCH_9, 0x01e46be7);
7357 	}
7358 	REG_WR(bp, SRC_REG_SOFT_RST, 0);
7359 
7360 	if (sizeof(union cdu_context) != 1024)
7361 		/* we currently assume that a context is 1024 bytes */
7362 		dev_alert(&bp->pdev->dev,
7363 			  "please adjust the size of cdu_context(%ld)\n",
7364 			  (long)sizeof(union cdu_context));
7365 
7366 	bnx2x_init_block(bp, BLOCK_CDU, PHASE_COMMON);
7367 	val = (4 << 24) + (0 << 12) + 1024;
7368 	REG_WR(bp, CDU_REG_CDU_GLOBAL_PARAMS, val);
7369 
7370 	bnx2x_init_block(bp, BLOCK_CFC, PHASE_COMMON);
7371 	REG_WR(bp, CFC_REG_INIT_REG, 0x7FF);
7372 	/* enable context validation interrupt from CFC */
7373 	REG_WR(bp, CFC_REG_CFC_INT_MASK, 0);
7374 
7375 	/* set the thresholds to prevent CFC/CDU race */
7376 	REG_WR(bp, CFC_REG_DEBUG0, 0x20020000);
7377 
7378 	bnx2x_init_block(bp, BLOCK_HC, PHASE_COMMON);
7379 
7380 	if (!CHIP_IS_E1x(bp) && BP_NOMCP(bp))
7381 		REG_WR(bp, IGU_REG_RESET_MEMORIES, 0x36);
7382 
7383 	bnx2x_init_block(bp, BLOCK_IGU, PHASE_COMMON);
7384 	bnx2x_init_block(bp, BLOCK_MISC_AEU, PHASE_COMMON);
7385 
7386 	/* Reset PCIE errors for debug */
7387 	REG_WR(bp, 0x2814, 0xffffffff);
7388 	REG_WR(bp, 0x3820, 0xffffffff);
7389 
7390 	if (!CHIP_IS_E1x(bp)) {
7391 		REG_WR(bp, PCICFG_OFFSET + PXPCS_TL_CONTROL_5,
7392 			   (PXPCS_TL_CONTROL_5_ERR_UNSPPORT1 |
7393 				PXPCS_TL_CONTROL_5_ERR_UNSPPORT));
7394 		REG_WR(bp, PCICFG_OFFSET + PXPCS_TL_FUNC345_STAT,
7395 			   (PXPCS_TL_FUNC345_STAT_ERR_UNSPPORT4 |
7396 				PXPCS_TL_FUNC345_STAT_ERR_UNSPPORT3 |
7397 				PXPCS_TL_FUNC345_STAT_ERR_UNSPPORT2));
7398 		REG_WR(bp, PCICFG_OFFSET + PXPCS_TL_FUNC678_STAT,
7399 			   (PXPCS_TL_FUNC678_STAT_ERR_UNSPPORT7 |
7400 				PXPCS_TL_FUNC678_STAT_ERR_UNSPPORT6 |
7401 				PXPCS_TL_FUNC678_STAT_ERR_UNSPPORT5));
7402 	}
7403 
7404 	bnx2x_init_block(bp, BLOCK_NIG, PHASE_COMMON);
7405 	if (!CHIP_IS_E1(bp)) {
7406 		/* in E3 this done in per-port section */
7407 		if (!CHIP_IS_E3(bp))
7408 			REG_WR(bp, NIG_REG_LLH_MF_MODE, IS_MF(bp));
7409 	}
7410 	if (CHIP_IS_E1H(bp))
7411 		/* not applicable for E2 (and above ...) */
7412 		REG_WR(bp, NIG_REG_LLH_E1HOV_MODE, IS_MF_SD(bp));
7413 
7414 	if (CHIP_REV_IS_SLOW(bp))
7415 		msleep(200);
7416 
7417 	/* finish CFC init */
7418 	val = reg_poll(bp, CFC_REG_LL_INIT_DONE, 1, 100, 10);
7419 	if (val != 1) {
7420 		BNX2X_ERR("CFC LL_INIT failed\n");
7421 		return -EBUSY;
7422 	}
7423 	val = reg_poll(bp, CFC_REG_AC_INIT_DONE, 1, 100, 10);
7424 	if (val != 1) {
7425 		BNX2X_ERR("CFC AC_INIT failed\n");
7426 		return -EBUSY;
7427 	}
7428 	val = reg_poll(bp, CFC_REG_CAM_INIT_DONE, 1, 100, 10);
7429 	if (val != 1) {
7430 		BNX2X_ERR("CFC CAM_INIT failed\n");
7431 		return -EBUSY;
7432 	}
7433 	REG_WR(bp, CFC_REG_DEBUG0, 0);
7434 
7435 	if (CHIP_IS_E1(bp)) {
7436 		/* read NIG statistic
7437 		   to see if this is our first up since powerup */
7438 		bnx2x_read_dmae(bp, NIG_REG_STAT2_BRB_OCTET, 2);
7439 		val = *bnx2x_sp(bp, wb_data[0]);
7440 
7441 		/* do internal memory self test */
7442 		if ((val == 0) && bnx2x_int_mem_test(bp)) {
7443 			BNX2X_ERR("internal mem self test failed\n");
7444 			return -EBUSY;
7445 		}
7446 	}
7447 
7448 	bnx2x_setup_fan_failure_detection(bp);
7449 
7450 	/* clear PXP2 attentions */
7451 	REG_RD(bp, PXP2_REG_PXP2_INT_STS_CLR_0);
7452 
7453 	bnx2x_enable_blocks_attention(bp);
7454 	bnx2x_enable_blocks_parity(bp);
7455 
7456 	if (!BP_NOMCP(bp)) {
7457 		if (CHIP_IS_E1x(bp))
7458 			bnx2x__common_init_phy(bp);
7459 	} else
7460 		BNX2X_ERR("Bootcode is missing - can not initialize link\n");
7461 
7462 	if (SHMEM2_HAS(bp, netproc_fw_ver))
7463 		SHMEM2_WR(bp, netproc_fw_ver, REG_RD(bp, XSEM_REG_PRAM));
7464 
7465 	return 0;
7466 }
7467 
7468 /**
7469  * bnx2x_init_hw_common_chip - init HW at the COMMON_CHIP phase.
7470  *
7471  * @bp:		driver handle
7472  */
7473 static int bnx2x_init_hw_common_chip(struct bnx2x *bp)
7474 {
7475 	int rc = bnx2x_init_hw_common(bp);
7476 
7477 	if (rc)
7478 		return rc;
7479 
7480 	/* In E2 2-PORT mode, same ext phy is used for the two paths */
7481 	if (!BP_NOMCP(bp))
7482 		bnx2x__common_init_phy(bp);
7483 
7484 	return 0;
7485 }
7486 
7487 static int bnx2x_init_hw_port(struct bnx2x *bp)
7488 {
7489 	int port = BP_PORT(bp);
7490 	int init_phase = port ? PHASE_PORT1 : PHASE_PORT0;
7491 	u32 low, high;
7492 	u32 val, reg;
7493 
7494 	DP(NETIF_MSG_HW, "starting port init  port %d\n", port);
7495 
7496 	REG_WR(bp, NIG_REG_MASK_INTERRUPT_PORT0 + port*4, 0);
7497 
7498 	bnx2x_init_block(bp, BLOCK_MISC, init_phase);
7499 	bnx2x_init_block(bp, BLOCK_PXP, init_phase);
7500 	bnx2x_init_block(bp, BLOCK_PXP2, init_phase);
7501 
7502 	/* Timers bug workaround: disables the pf_master bit in pglue at
7503 	 * common phase, we need to enable it here before any dmae access are
7504 	 * attempted. Therefore we manually added the enable-master to the
7505 	 * port phase (it also happens in the function phase)
7506 	 */
7507 	if (!CHIP_IS_E1x(bp))
7508 		REG_WR(bp, PGLUE_B_REG_INTERNAL_PFID_ENABLE_MASTER, 1);
7509 
7510 	bnx2x_init_block(bp, BLOCK_ATC, init_phase);
7511 	bnx2x_init_block(bp, BLOCK_DMAE, init_phase);
7512 	bnx2x_init_block(bp, BLOCK_PGLUE_B, init_phase);
7513 	bnx2x_init_block(bp, BLOCK_QM, init_phase);
7514 
7515 	bnx2x_init_block(bp, BLOCK_TCM, init_phase);
7516 	bnx2x_init_block(bp, BLOCK_UCM, init_phase);
7517 	bnx2x_init_block(bp, BLOCK_CCM, init_phase);
7518 	bnx2x_init_block(bp, BLOCK_XCM, init_phase);
7519 
7520 	/* QM cid (connection) count */
7521 	bnx2x_qm_init_cid_count(bp, bp->qm_cid_count, INITOP_SET);
7522 
7523 	if (CNIC_SUPPORT(bp)) {
7524 		bnx2x_init_block(bp, BLOCK_TM, init_phase);
7525 		REG_WR(bp, TM_REG_LIN0_SCAN_TIME + port*4, 20);
7526 		REG_WR(bp, TM_REG_LIN0_MAX_ACTIVE_CID + port*4, 31);
7527 	}
7528 
7529 	bnx2x_init_block(bp, BLOCK_DORQ, init_phase);
7530 
7531 	bnx2x_init_block(bp, BLOCK_BRB1, init_phase);
7532 
7533 	if (CHIP_IS_E1(bp) || CHIP_IS_E1H(bp)) {
7534 
7535 		if (IS_MF(bp))
7536 			low = ((bp->flags & ONE_PORT_FLAG) ? 160 : 246);
7537 		else if (bp->dev->mtu > 4096) {
7538 			if (bp->flags & ONE_PORT_FLAG)
7539 				low = 160;
7540 			else {
7541 				val = bp->dev->mtu;
7542 				/* (24*1024 + val*4)/256 */
7543 				low = 96 + (val/64) +
7544 						((val % 64) ? 1 : 0);
7545 			}
7546 		} else
7547 			low = ((bp->flags & ONE_PORT_FLAG) ? 80 : 160);
7548 		high = low + 56;	/* 14*1024/256 */
7549 		REG_WR(bp, BRB1_REG_PAUSE_LOW_THRESHOLD_0 + port*4, low);
7550 		REG_WR(bp, BRB1_REG_PAUSE_HIGH_THRESHOLD_0 + port*4, high);
7551 	}
7552 
7553 	if (CHIP_MODE_IS_4_PORT(bp))
7554 		REG_WR(bp, (BP_PORT(bp) ?
7555 			    BRB1_REG_MAC_GUARANTIED_1 :
7556 			    BRB1_REG_MAC_GUARANTIED_0), 40);
7557 
7558 	bnx2x_init_block(bp, BLOCK_PRS, init_phase);
7559 	if (CHIP_IS_E3B0(bp)) {
7560 		if (IS_MF_AFEX(bp)) {
7561 			/* configure headers for AFEX mode */
7562 			REG_WR(bp, BP_PORT(bp) ?
7563 			       PRS_REG_HDRS_AFTER_BASIC_PORT_1 :
7564 			       PRS_REG_HDRS_AFTER_BASIC_PORT_0, 0xE);
7565 			REG_WR(bp, BP_PORT(bp) ?
7566 			       PRS_REG_HDRS_AFTER_TAG_0_PORT_1 :
7567 			       PRS_REG_HDRS_AFTER_TAG_0_PORT_0, 0x6);
7568 			REG_WR(bp, BP_PORT(bp) ?
7569 			       PRS_REG_MUST_HAVE_HDRS_PORT_1 :
7570 			       PRS_REG_MUST_HAVE_HDRS_PORT_0, 0xA);
7571 		} else {
7572 			/* Ovlan exists only if we are in multi-function +
7573 			 * switch-dependent mode, in switch-independent there
7574 			 * is no ovlan headers
7575 			 */
7576 			REG_WR(bp, BP_PORT(bp) ?
7577 			       PRS_REG_HDRS_AFTER_BASIC_PORT_1 :
7578 			       PRS_REG_HDRS_AFTER_BASIC_PORT_0,
7579 			       (bp->path_has_ovlan ? 7 : 6));
7580 		}
7581 	}
7582 
7583 	bnx2x_init_block(bp, BLOCK_TSDM, init_phase);
7584 	bnx2x_init_block(bp, BLOCK_CSDM, init_phase);
7585 	bnx2x_init_block(bp, BLOCK_USDM, init_phase);
7586 	bnx2x_init_block(bp, BLOCK_XSDM, init_phase);
7587 
7588 	bnx2x_init_block(bp, BLOCK_TSEM, init_phase);
7589 	bnx2x_init_block(bp, BLOCK_USEM, init_phase);
7590 	bnx2x_init_block(bp, BLOCK_CSEM, init_phase);
7591 	bnx2x_init_block(bp, BLOCK_XSEM, init_phase);
7592 
7593 	bnx2x_init_block(bp, BLOCK_UPB, init_phase);
7594 	bnx2x_init_block(bp, BLOCK_XPB, init_phase);
7595 
7596 	bnx2x_init_block(bp, BLOCK_PBF, init_phase);
7597 
7598 	if (CHIP_IS_E1x(bp)) {
7599 		/* configure PBF to work without PAUSE mtu 9000 */
7600 		REG_WR(bp, PBF_REG_P0_PAUSE_ENABLE + port*4, 0);
7601 
7602 		/* update threshold */
7603 		REG_WR(bp, PBF_REG_P0_ARB_THRSH + port*4, (9040/16));
7604 		/* update init credit */
7605 		REG_WR(bp, PBF_REG_P0_INIT_CRD + port*4, (9040/16) + 553 - 22);
7606 
7607 		/* probe changes */
7608 		REG_WR(bp, PBF_REG_INIT_P0 + port*4, 1);
7609 		udelay(50);
7610 		REG_WR(bp, PBF_REG_INIT_P0 + port*4, 0);
7611 	}
7612 
7613 	if (CNIC_SUPPORT(bp))
7614 		bnx2x_init_block(bp, BLOCK_SRC, init_phase);
7615 
7616 	bnx2x_init_block(bp, BLOCK_CDU, init_phase);
7617 	bnx2x_init_block(bp, BLOCK_CFC, init_phase);
7618 
7619 	if (CHIP_IS_E1(bp)) {
7620 		REG_WR(bp, HC_REG_LEADING_EDGE_0 + port*8, 0);
7621 		REG_WR(bp, HC_REG_TRAILING_EDGE_0 + port*8, 0);
7622 	}
7623 	bnx2x_init_block(bp, BLOCK_HC, init_phase);
7624 
7625 	bnx2x_init_block(bp, BLOCK_IGU, init_phase);
7626 
7627 	bnx2x_init_block(bp, BLOCK_MISC_AEU, init_phase);
7628 	/* init aeu_mask_attn_func_0/1:
7629 	 *  - SF mode: bits 3-7 are masked. Only bits 0-2 are in use
7630 	 *  - MF mode: bit 3 is masked. Bits 0-2 are in use as in SF
7631 	 *             bits 4-7 are used for "per vn group attention" */
7632 	val = IS_MF(bp) ? 0xF7 : 0x7;
7633 	/* Enable DCBX attention for all but E1 */
7634 	val |= CHIP_IS_E1(bp) ? 0 : 0x10;
7635 	REG_WR(bp, MISC_REG_AEU_MASK_ATTN_FUNC_0 + port*4, val);
7636 
7637 	/* SCPAD_PARITY should NOT trigger close the gates */
7638 	reg = port ? MISC_REG_AEU_ENABLE4_NIG_1 : MISC_REG_AEU_ENABLE4_NIG_0;
7639 	REG_WR(bp, reg,
7640 	       REG_RD(bp, reg) &
7641 	       ~AEU_INPUTS_ATTN_BITS_MCP_LATCHED_SCPAD_PARITY);
7642 
7643 	reg = port ? MISC_REG_AEU_ENABLE4_PXP_1 : MISC_REG_AEU_ENABLE4_PXP_0;
7644 	REG_WR(bp, reg,
7645 	       REG_RD(bp, reg) &
7646 	       ~AEU_INPUTS_ATTN_BITS_MCP_LATCHED_SCPAD_PARITY);
7647 
7648 	bnx2x_init_block(bp, BLOCK_NIG, init_phase);
7649 
7650 	if (!CHIP_IS_E1x(bp)) {
7651 		/* Bit-map indicating which L2 hdrs may appear after the
7652 		 * basic Ethernet header
7653 		 */
7654 		if (IS_MF_AFEX(bp))
7655 			REG_WR(bp, BP_PORT(bp) ?
7656 			       NIG_REG_P1_HDRS_AFTER_BASIC :
7657 			       NIG_REG_P0_HDRS_AFTER_BASIC, 0xE);
7658 		else
7659 			REG_WR(bp, BP_PORT(bp) ?
7660 			       NIG_REG_P1_HDRS_AFTER_BASIC :
7661 			       NIG_REG_P0_HDRS_AFTER_BASIC,
7662 			       IS_MF_SD(bp) ? 7 : 6);
7663 
7664 		if (CHIP_IS_E3(bp))
7665 			REG_WR(bp, BP_PORT(bp) ?
7666 				   NIG_REG_LLH1_MF_MODE :
7667 				   NIG_REG_LLH_MF_MODE, IS_MF(bp));
7668 	}
7669 	if (!CHIP_IS_E3(bp))
7670 		REG_WR(bp, NIG_REG_XGXS_SERDES0_MODE_SEL + port*4, 1);
7671 
7672 	if (!CHIP_IS_E1(bp)) {
7673 		/* 0x2 disable mf_ov, 0x1 enable */
7674 		REG_WR(bp, NIG_REG_LLH0_BRB1_DRV_MASK_MF + port*4,
7675 		       (IS_MF_SD(bp) ? 0x1 : 0x2));
7676 
7677 		if (!CHIP_IS_E1x(bp)) {
7678 			val = 0;
7679 			switch (bp->mf_mode) {
7680 			case MULTI_FUNCTION_SD:
7681 				val = 1;
7682 				break;
7683 			case MULTI_FUNCTION_SI:
7684 			case MULTI_FUNCTION_AFEX:
7685 				val = 2;
7686 				break;
7687 			}
7688 
7689 			REG_WR(bp, (BP_PORT(bp) ? NIG_REG_LLH1_CLS_TYPE :
7690 						  NIG_REG_LLH0_CLS_TYPE), val);
7691 		}
7692 		{
7693 			REG_WR(bp, NIG_REG_LLFC_ENABLE_0 + port*4, 0);
7694 			REG_WR(bp, NIG_REG_LLFC_OUT_EN_0 + port*4, 0);
7695 			REG_WR(bp, NIG_REG_PAUSE_ENABLE_0 + port*4, 1);
7696 		}
7697 	}
7698 
7699 	/* If SPIO5 is set to generate interrupts, enable it for this port */
7700 	val = REG_RD(bp, MISC_REG_SPIO_EVENT_EN);
7701 	if (val & MISC_SPIO_SPIO5) {
7702 		u32 reg_addr = (port ? MISC_REG_AEU_ENABLE1_FUNC_1_OUT_0 :
7703 				       MISC_REG_AEU_ENABLE1_FUNC_0_OUT_0);
7704 		val = REG_RD(bp, reg_addr);
7705 		val |= AEU_INPUTS_ATTN_BITS_SPIO5;
7706 		REG_WR(bp, reg_addr, val);
7707 	}
7708 
7709 	if (CHIP_IS_E3B0(bp))
7710 		bp->flags |= PTP_SUPPORTED;
7711 
7712 	return 0;
7713 }
7714 
7715 static void bnx2x_ilt_wr(struct bnx2x *bp, u32 index, dma_addr_t addr)
7716 {
7717 	int reg;
7718 	u32 wb_write[2];
7719 
7720 	if (CHIP_IS_E1(bp))
7721 		reg = PXP2_REG_RQ_ONCHIP_AT + index*8;
7722 	else
7723 		reg = PXP2_REG_RQ_ONCHIP_AT_B0 + index*8;
7724 
7725 	wb_write[0] = ONCHIP_ADDR1(addr);
7726 	wb_write[1] = ONCHIP_ADDR2(addr);
7727 	REG_WR_DMAE(bp, reg, wb_write, 2);
7728 }
7729 
7730 void bnx2x_igu_clear_sb_gen(struct bnx2x *bp, u8 func, u8 idu_sb_id, bool is_pf)
7731 {
7732 	u32 data, ctl, cnt = 100;
7733 	u32 igu_addr_data = IGU_REG_COMMAND_REG_32LSB_DATA;
7734 	u32 igu_addr_ctl = IGU_REG_COMMAND_REG_CTRL;
7735 	u32 igu_addr_ack = IGU_REG_CSTORM_TYPE_0_SB_CLEANUP + (idu_sb_id/32)*4;
7736 	u32 sb_bit =  1 << (idu_sb_id%32);
7737 	u32 func_encode = func | (is_pf ? 1 : 0) << IGU_FID_ENCODE_IS_PF_SHIFT;
7738 	u32 addr_encode = IGU_CMD_E2_PROD_UPD_BASE + idu_sb_id;
7739 
7740 	/* Not supported in BC mode */
7741 	if (CHIP_INT_MODE_IS_BC(bp))
7742 		return;
7743 
7744 	data = (IGU_USE_REGISTER_cstorm_type_0_sb_cleanup
7745 			<< IGU_REGULAR_CLEANUP_TYPE_SHIFT)	|
7746 		IGU_REGULAR_CLEANUP_SET				|
7747 		IGU_REGULAR_BCLEANUP;
7748 
7749 	ctl = addr_encode << IGU_CTRL_REG_ADDRESS_SHIFT		|
7750 	      func_encode << IGU_CTRL_REG_FID_SHIFT		|
7751 	      IGU_CTRL_CMD_TYPE_WR << IGU_CTRL_REG_TYPE_SHIFT;
7752 
7753 	DP(NETIF_MSG_HW, "write 0x%08x to IGU(via GRC) addr 0x%x\n",
7754 			 data, igu_addr_data);
7755 	REG_WR(bp, igu_addr_data, data);
7756 	barrier();
7757 	DP(NETIF_MSG_HW, "write 0x%08x to IGU(via GRC) addr 0x%x\n",
7758 			  ctl, igu_addr_ctl);
7759 	REG_WR(bp, igu_addr_ctl, ctl);
7760 	barrier();
7761 
7762 	/* wait for clean up to finish */
7763 	while (!(REG_RD(bp, igu_addr_ack) & sb_bit) && --cnt)
7764 		msleep(20);
7765 
7766 	if (!(REG_RD(bp, igu_addr_ack) & sb_bit)) {
7767 		DP(NETIF_MSG_HW,
7768 		   "Unable to finish IGU cleanup: idu_sb_id %d offset %d bit %d (cnt %d)\n",
7769 			  idu_sb_id, idu_sb_id/32, idu_sb_id%32, cnt);
7770 	}
7771 }
7772 
7773 static void bnx2x_igu_clear_sb(struct bnx2x *bp, u8 idu_sb_id)
7774 {
7775 	bnx2x_igu_clear_sb_gen(bp, BP_FUNC(bp), idu_sb_id, true /*PF*/);
7776 }
7777 
7778 static void bnx2x_clear_func_ilt(struct bnx2x *bp, u32 func)
7779 {
7780 	u32 i, base = FUNC_ILT_BASE(func);
7781 	for (i = base; i < base + ILT_PER_FUNC; i++)
7782 		bnx2x_ilt_wr(bp, i, 0);
7783 }
7784 
7785 static void bnx2x_init_searcher(struct bnx2x *bp)
7786 {
7787 	int port = BP_PORT(bp);
7788 	bnx2x_src_init_t2(bp, bp->t2, bp->t2_mapping, SRC_CONN_NUM);
7789 	/* T1 hash bits value determines the T1 number of entries */
7790 	REG_WR(bp, SRC_REG_NUMBER_HASH_BITS0 + port*4, SRC_HASH_BITS);
7791 }
7792 
7793 static inline int bnx2x_func_switch_update(struct bnx2x *bp, int suspend)
7794 {
7795 	int rc;
7796 	struct bnx2x_func_state_params func_params = {NULL};
7797 	struct bnx2x_func_switch_update_params *switch_update_params =
7798 		&func_params.params.switch_update;
7799 
7800 	/* Prepare parameters for function state transitions */
7801 	__set_bit(RAMROD_COMP_WAIT, &func_params.ramrod_flags);
7802 	__set_bit(RAMROD_RETRY, &func_params.ramrod_flags);
7803 
7804 	func_params.f_obj = &bp->func_obj;
7805 	func_params.cmd = BNX2X_F_CMD_SWITCH_UPDATE;
7806 
7807 	/* Function parameters */
7808 	__set_bit(BNX2X_F_UPDATE_TX_SWITCH_SUSPEND_CHNG,
7809 		  &switch_update_params->changes);
7810 	if (suspend)
7811 		__set_bit(BNX2X_F_UPDATE_TX_SWITCH_SUSPEND,
7812 			  &switch_update_params->changes);
7813 
7814 	rc = bnx2x_func_state_change(bp, &func_params);
7815 
7816 	return rc;
7817 }
7818 
7819 static int bnx2x_reset_nic_mode(struct bnx2x *bp)
7820 {
7821 	int rc, i, port = BP_PORT(bp);
7822 	int vlan_en = 0, mac_en[NUM_MACS];
7823 
7824 	/* Close input from network */
7825 	if (bp->mf_mode == SINGLE_FUNCTION) {
7826 		bnx2x_set_rx_filter(&bp->link_params, 0);
7827 	} else {
7828 		vlan_en = REG_RD(bp, port ? NIG_REG_LLH1_FUNC_EN :
7829 				   NIG_REG_LLH0_FUNC_EN);
7830 		REG_WR(bp, port ? NIG_REG_LLH1_FUNC_EN :
7831 			  NIG_REG_LLH0_FUNC_EN, 0);
7832 		for (i = 0; i < NUM_MACS; i++) {
7833 			mac_en[i] = REG_RD(bp, port ?
7834 					     (NIG_REG_LLH1_FUNC_MEM_ENABLE +
7835 					      4 * i) :
7836 					     (NIG_REG_LLH0_FUNC_MEM_ENABLE +
7837 					      4 * i));
7838 			REG_WR(bp, port ? (NIG_REG_LLH1_FUNC_MEM_ENABLE +
7839 					      4 * i) :
7840 				  (NIG_REG_LLH0_FUNC_MEM_ENABLE + 4 * i), 0);
7841 		}
7842 	}
7843 
7844 	/* Close BMC to host */
7845 	REG_WR(bp, port ? NIG_REG_P0_TX_MNG_HOST_ENABLE :
7846 	       NIG_REG_P1_TX_MNG_HOST_ENABLE, 0);
7847 
7848 	/* Suspend Tx switching to the PF. Completion of this ramrod
7849 	 * further guarantees that all the packets of that PF / child
7850 	 * VFs in BRB were processed by the Parser, so it is safe to
7851 	 * change the NIC_MODE register.
7852 	 */
7853 	rc = bnx2x_func_switch_update(bp, 1);
7854 	if (rc) {
7855 		BNX2X_ERR("Can't suspend tx-switching!\n");
7856 		return rc;
7857 	}
7858 
7859 	/* Change NIC_MODE register */
7860 	REG_WR(bp, PRS_REG_NIC_MODE, 0);
7861 
7862 	/* Open input from network */
7863 	if (bp->mf_mode == SINGLE_FUNCTION) {
7864 		bnx2x_set_rx_filter(&bp->link_params, 1);
7865 	} else {
7866 		REG_WR(bp, port ? NIG_REG_LLH1_FUNC_EN :
7867 			  NIG_REG_LLH0_FUNC_EN, vlan_en);
7868 		for (i = 0; i < NUM_MACS; i++) {
7869 			REG_WR(bp, port ? (NIG_REG_LLH1_FUNC_MEM_ENABLE +
7870 					      4 * i) :
7871 				  (NIG_REG_LLH0_FUNC_MEM_ENABLE + 4 * i),
7872 				  mac_en[i]);
7873 		}
7874 	}
7875 
7876 	/* Enable BMC to host */
7877 	REG_WR(bp, port ? NIG_REG_P0_TX_MNG_HOST_ENABLE :
7878 	       NIG_REG_P1_TX_MNG_HOST_ENABLE, 1);
7879 
7880 	/* Resume Tx switching to the PF */
7881 	rc = bnx2x_func_switch_update(bp, 0);
7882 	if (rc) {
7883 		BNX2X_ERR("Can't resume tx-switching!\n");
7884 		return rc;
7885 	}
7886 
7887 	DP(NETIF_MSG_IFUP, "NIC MODE disabled\n");
7888 	return 0;
7889 }
7890 
7891 int bnx2x_init_hw_func_cnic(struct bnx2x *bp)
7892 {
7893 	int rc;
7894 
7895 	bnx2x_ilt_init_op_cnic(bp, INITOP_SET);
7896 
7897 	if (CONFIGURE_NIC_MODE(bp)) {
7898 		/* Configure searcher as part of function hw init */
7899 		bnx2x_init_searcher(bp);
7900 
7901 		/* Reset NIC mode */
7902 		rc = bnx2x_reset_nic_mode(bp);
7903 		if (rc)
7904 			BNX2X_ERR("Can't change NIC mode!\n");
7905 		return rc;
7906 	}
7907 
7908 	return 0;
7909 }
7910 
7911 /* previous driver DMAE transaction may have occurred when pre-boot stage ended
7912  * and boot began, or when kdump kernel was loaded. Either case would invalidate
7913  * the addresses of the transaction, resulting in was-error bit set in the pci
7914  * causing all hw-to-host pcie transactions to timeout. If this happened we want
7915  * to clear the interrupt which detected this from the pglueb and the was done
7916  * bit
7917  */
7918 static void bnx2x_clean_pglue_errors(struct bnx2x *bp)
7919 {
7920 	if (!CHIP_IS_E1x(bp))
7921 		REG_WR(bp, PGLUE_B_REG_WAS_ERROR_PF_7_0_CLR,
7922 		       1 << BP_ABS_FUNC(bp));
7923 }
7924 
7925 static int bnx2x_init_hw_func(struct bnx2x *bp)
7926 {
7927 	int port = BP_PORT(bp);
7928 	int func = BP_FUNC(bp);
7929 	int init_phase = PHASE_PF0 + func;
7930 	struct bnx2x_ilt *ilt = BP_ILT(bp);
7931 	u16 cdu_ilt_start;
7932 	u32 addr, val;
7933 	u32 main_mem_base, main_mem_size, main_mem_prty_clr;
7934 	int i, main_mem_width, rc;
7935 
7936 	DP(NETIF_MSG_HW, "starting func init  func %d\n", func);
7937 
7938 	/* FLR cleanup - hmmm */
7939 	if (!CHIP_IS_E1x(bp)) {
7940 		rc = bnx2x_pf_flr_clnup(bp);
7941 		if (rc) {
7942 			bnx2x_fw_dump(bp);
7943 			return rc;
7944 		}
7945 	}
7946 
7947 	/* set MSI reconfigure capability */
7948 	if (bp->common.int_block == INT_BLOCK_HC) {
7949 		addr = (port ? HC_REG_CONFIG_1 : HC_REG_CONFIG_0);
7950 		val = REG_RD(bp, addr);
7951 		val |= HC_CONFIG_0_REG_MSI_ATTN_EN_0;
7952 		REG_WR(bp, addr, val);
7953 	}
7954 
7955 	bnx2x_init_block(bp, BLOCK_PXP, init_phase);
7956 	bnx2x_init_block(bp, BLOCK_PXP2, init_phase);
7957 
7958 	ilt = BP_ILT(bp);
7959 	cdu_ilt_start = ilt->clients[ILT_CLIENT_CDU].start;
7960 
7961 	if (IS_SRIOV(bp))
7962 		cdu_ilt_start += BNX2X_FIRST_VF_CID/ILT_PAGE_CIDS;
7963 	cdu_ilt_start = bnx2x_iov_init_ilt(bp, cdu_ilt_start);
7964 
7965 	/* since BNX2X_FIRST_VF_CID > 0 the PF L2 cids precedes
7966 	 * those of the VFs, so start line should be reset
7967 	 */
7968 	cdu_ilt_start = ilt->clients[ILT_CLIENT_CDU].start;
7969 	for (i = 0; i < L2_ILT_LINES(bp); i++) {
7970 		ilt->lines[cdu_ilt_start + i].page = bp->context[i].vcxt;
7971 		ilt->lines[cdu_ilt_start + i].page_mapping =
7972 			bp->context[i].cxt_mapping;
7973 		ilt->lines[cdu_ilt_start + i].size = bp->context[i].size;
7974 	}
7975 
7976 	bnx2x_ilt_init_op(bp, INITOP_SET);
7977 
7978 	if (!CONFIGURE_NIC_MODE(bp)) {
7979 		bnx2x_init_searcher(bp);
7980 		REG_WR(bp, PRS_REG_NIC_MODE, 0);
7981 		DP(NETIF_MSG_IFUP, "NIC MODE disabled\n");
7982 	} else {
7983 		/* Set NIC mode */
7984 		REG_WR(bp, PRS_REG_NIC_MODE, 1);
7985 		DP(NETIF_MSG_IFUP, "NIC MODE configured\n");
7986 	}
7987 
7988 	if (!CHIP_IS_E1x(bp)) {
7989 		u32 pf_conf = IGU_PF_CONF_FUNC_EN;
7990 
7991 		/* Turn on a single ISR mode in IGU if driver is going to use
7992 		 * INT#x or MSI
7993 		 */
7994 		if (!(bp->flags & USING_MSIX_FLAG))
7995 			pf_conf |= IGU_PF_CONF_SINGLE_ISR_EN;
7996 		/*
7997 		 * Timers workaround bug: function init part.
7998 		 * Need to wait 20msec after initializing ILT,
7999 		 * needed to make sure there are no requests in
8000 		 * one of the PXP internal queues with "old" ILT addresses
8001 		 */
8002 		msleep(20);
8003 		/*
8004 		 * Master enable - Due to WB DMAE writes performed before this
8005 		 * register is re-initialized as part of the regular function
8006 		 * init
8007 		 */
8008 		REG_WR(bp, PGLUE_B_REG_INTERNAL_PFID_ENABLE_MASTER, 1);
8009 		/* Enable the function in IGU */
8010 		REG_WR(bp, IGU_REG_PF_CONFIGURATION, pf_conf);
8011 	}
8012 
8013 	bp->dmae_ready = 1;
8014 
8015 	bnx2x_init_block(bp, BLOCK_PGLUE_B, init_phase);
8016 
8017 	bnx2x_clean_pglue_errors(bp);
8018 
8019 	bnx2x_init_block(bp, BLOCK_ATC, init_phase);
8020 	bnx2x_init_block(bp, BLOCK_DMAE, init_phase);
8021 	bnx2x_init_block(bp, BLOCK_NIG, init_phase);
8022 	bnx2x_init_block(bp, BLOCK_SRC, init_phase);
8023 	bnx2x_init_block(bp, BLOCK_MISC, init_phase);
8024 	bnx2x_init_block(bp, BLOCK_TCM, init_phase);
8025 	bnx2x_init_block(bp, BLOCK_UCM, init_phase);
8026 	bnx2x_init_block(bp, BLOCK_CCM, init_phase);
8027 	bnx2x_init_block(bp, BLOCK_XCM, init_phase);
8028 	bnx2x_init_block(bp, BLOCK_TSEM, init_phase);
8029 	bnx2x_init_block(bp, BLOCK_USEM, init_phase);
8030 	bnx2x_init_block(bp, BLOCK_CSEM, init_phase);
8031 	bnx2x_init_block(bp, BLOCK_XSEM, init_phase);
8032 
8033 	if (!CHIP_IS_E1x(bp))
8034 		REG_WR(bp, QM_REG_PF_EN, 1);
8035 
8036 	if (!CHIP_IS_E1x(bp)) {
8037 		REG_WR(bp, TSEM_REG_VFPF_ERR_NUM, BNX2X_MAX_NUM_OF_VFS + func);
8038 		REG_WR(bp, USEM_REG_VFPF_ERR_NUM, BNX2X_MAX_NUM_OF_VFS + func);
8039 		REG_WR(bp, CSEM_REG_VFPF_ERR_NUM, BNX2X_MAX_NUM_OF_VFS + func);
8040 		REG_WR(bp, XSEM_REG_VFPF_ERR_NUM, BNX2X_MAX_NUM_OF_VFS + func);
8041 	}
8042 	bnx2x_init_block(bp, BLOCK_QM, init_phase);
8043 
8044 	bnx2x_init_block(bp, BLOCK_TM, init_phase);
8045 	bnx2x_init_block(bp, BLOCK_DORQ, init_phase);
8046 	REG_WR(bp, DORQ_REG_MODE_ACT, 1); /* no dpm */
8047 
8048 	bnx2x_iov_init_dq(bp);
8049 
8050 	bnx2x_init_block(bp, BLOCK_BRB1, init_phase);
8051 	bnx2x_init_block(bp, BLOCK_PRS, init_phase);
8052 	bnx2x_init_block(bp, BLOCK_TSDM, init_phase);
8053 	bnx2x_init_block(bp, BLOCK_CSDM, init_phase);
8054 	bnx2x_init_block(bp, BLOCK_USDM, init_phase);
8055 	bnx2x_init_block(bp, BLOCK_XSDM, init_phase);
8056 	bnx2x_init_block(bp, BLOCK_UPB, init_phase);
8057 	bnx2x_init_block(bp, BLOCK_XPB, init_phase);
8058 	bnx2x_init_block(bp, BLOCK_PBF, init_phase);
8059 	if (!CHIP_IS_E1x(bp))
8060 		REG_WR(bp, PBF_REG_DISABLE_PF, 0);
8061 
8062 	bnx2x_init_block(bp, BLOCK_CDU, init_phase);
8063 
8064 	bnx2x_init_block(bp, BLOCK_CFC, init_phase);
8065 
8066 	if (!CHIP_IS_E1x(bp))
8067 		REG_WR(bp, CFC_REG_WEAK_ENABLE_PF, 1);
8068 
8069 	if (IS_MF(bp)) {
8070 		if (!(IS_MF_UFP(bp) && BNX2X_IS_MF_SD_PROTOCOL_FCOE(bp))) {
8071 			REG_WR(bp, NIG_REG_LLH0_FUNC_EN + port * 8, 1);
8072 			REG_WR(bp, NIG_REG_LLH0_FUNC_VLAN_ID + port * 8,
8073 			       bp->mf_ov);
8074 		}
8075 	}
8076 
8077 	bnx2x_init_block(bp, BLOCK_MISC_AEU, init_phase);
8078 
8079 	/* HC init per function */
8080 	if (bp->common.int_block == INT_BLOCK_HC) {
8081 		if (CHIP_IS_E1H(bp)) {
8082 			REG_WR(bp, MISC_REG_AEU_GENERAL_ATTN_12 + func*4, 0);
8083 
8084 			REG_WR(bp, HC_REG_LEADING_EDGE_0 + port*8, 0);
8085 			REG_WR(bp, HC_REG_TRAILING_EDGE_0 + port*8, 0);
8086 		}
8087 		bnx2x_init_block(bp, BLOCK_HC, init_phase);
8088 
8089 	} else {
8090 		int num_segs, sb_idx, prod_offset;
8091 
8092 		REG_WR(bp, MISC_REG_AEU_GENERAL_ATTN_12 + func*4, 0);
8093 
8094 		if (!CHIP_IS_E1x(bp)) {
8095 			REG_WR(bp, IGU_REG_LEADING_EDGE_LATCH, 0);
8096 			REG_WR(bp, IGU_REG_TRAILING_EDGE_LATCH, 0);
8097 		}
8098 
8099 		bnx2x_init_block(bp, BLOCK_IGU, init_phase);
8100 
8101 		if (!CHIP_IS_E1x(bp)) {
8102 			int dsb_idx = 0;
8103 			/**
8104 			 * Producer memory:
8105 			 * E2 mode: address 0-135 match to the mapping memory;
8106 			 * 136 - PF0 default prod; 137 - PF1 default prod;
8107 			 * 138 - PF2 default prod; 139 - PF3 default prod;
8108 			 * 140 - PF0 attn prod;    141 - PF1 attn prod;
8109 			 * 142 - PF2 attn prod;    143 - PF3 attn prod;
8110 			 * 144-147 reserved.
8111 			 *
8112 			 * E1.5 mode - In backward compatible mode;
8113 			 * for non default SB; each even line in the memory
8114 			 * holds the U producer and each odd line hold
8115 			 * the C producer. The first 128 producers are for
8116 			 * NDSB (PF0 - 0-31; PF1 - 32-63 and so on). The last 20
8117 			 * producers are for the DSB for each PF.
8118 			 * Each PF has five segments: (the order inside each
8119 			 * segment is PF0; PF1; PF2; PF3) - 128-131 U prods;
8120 			 * 132-135 C prods; 136-139 X prods; 140-143 T prods;
8121 			 * 144-147 attn prods;
8122 			 */
8123 			/* non-default-status-blocks */
8124 			num_segs = CHIP_INT_MODE_IS_BC(bp) ?
8125 				IGU_BC_NDSB_NUM_SEGS : IGU_NORM_NDSB_NUM_SEGS;
8126 			for (sb_idx = 0; sb_idx < bp->igu_sb_cnt; sb_idx++) {
8127 				prod_offset = (bp->igu_base_sb + sb_idx) *
8128 					num_segs;
8129 
8130 				for (i = 0; i < num_segs; i++) {
8131 					addr = IGU_REG_PROD_CONS_MEMORY +
8132 							(prod_offset + i) * 4;
8133 					REG_WR(bp, addr, 0);
8134 				}
8135 				/* send consumer update with value 0 */
8136 				bnx2x_ack_sb(bp, bp->igu_base_sb + sb_idx,
8137 					     USTORM_ID, 0, IGU_INT_NOP, 1);
8138 				bnx2x_igu_clear_sb(bp,
8139 						   bp->igu_base_sb + sb_idx);
8140 			}
8141 
8142 			/* default-status-blocks */
8143 			num_segs = CHIP_INT_MODE_IS_BC(bp) ?
8144 				IGU_BC_DSB_NUM_SEGS : IGU_NORM_DSB_NUM_SEGS;
8145 
8146 			if (CHIP_MODE_IS_4_PORT(bp))
8147 				dsb_idx = BP_FUNC(bp);
8148 			else
8149 				dsb_idx = BP_VN(bp);
8150 
8151 			prod_offset = (CHIP_INT_MODE_IS_BC(bp) ?
8152 				       IGU_BC_BASE_DSB_PROD + dsb_idx :
8153 				       IGU_NORM_BASE_DSB_PROD + dsb_idx);
8154 
8155 			/*
8156 			 * igu prods come in chunks of E1HVN_MAX (4) -
8157 			 * does not matters what is the current chip mode
8158 			 */
8159 			for (i = 0; i < (num_segs * E1HVN_MAX);
8160 			     i += E1HVN_MAX) {
8161 				addr = IGU_REG_PROD_CONS_MEMORY +
8162 							(prod_offset + i)*4;
8163 				REG_WR(bp, addr, 0);
8164 			}
8165 			/* send consumer update with 0 */
8166 			if (CHIP_INT_MODE_IS_BC(bp)) {
8167 				bnx2x_ack_sb(bp, bp->igu_dsb_id,
8168 					     USTORM_ID, 0, IGU_INT_NOP, 1);
8169 				bnx2x_ack_sb(bp, bp->igu_dsb_id,
8170 					     CSTORM_ID, 0, IGU_INT_NOP, 1);
8171 				bnx2x_ack_sb(bp, bp->igu_dsb_id,
8172 					     XSTORM_ID, 0, IGU_INT_NOP, 1);
8173 				bnx2x_ack_sb(bp, bp->igu_dsb_id,
8174 					     TSTORM_ID, 0, IGU_INT_NOP, 1);
8175 				bnx2x_ack_sb(bp, bp->igu_dsb_id,
8176 					     ATTENTION_ID, 0, IGU_INT_NOP, 1);
8177 			} else {
8178 				bnx2x_ack_sb(bp, bp->igu_dsb_id,
8179 					     USTORM_ID, 0, IGU_INT_NOP, 1);
8180 				bnx2x_ack_sb(bp, bp->igu_dsb_id,
8181 					     ATTENTION_ID, 0, IGU_INT_NOP, 1);
8182 			}
8183 			bnx2x_igu_clear_sb(bp, bp->igu_dsb_id);
8184 
8185 			/* !!! These should become driver const once
8186 			   rf-tool supports split-68 const */
8187 			REG_WR(bp, IGU_REG_SB_INT_BEFORE_MASK_LSB, 0);
8188 			REG_WR(bp, IGU_REG_SB_INT_BEFORE_MASK_MSB, 0);
8189 			REG_WR(bp, IGU_REG_SB_MASK_LSB, 0);
8190 			REG_WR(bp, IGU_REG_SB_MASK_MSB, 0);
8191 			REG_WR(bp, IGU_REG_PBA_STATUS_LSB, 0);
8192 			REG_WR(bp, IGU_REG_PBA_STATUS_MSB, 0);
8193 		}
8194 	}
8195 
8196 	/* Reset PCIE errors for debug */
8197 	REG_WR(bp, 0x2114, 0xffffffff);
8198 	REG_WR(bp, 0x2120, 0xffffffff);
8199 
8200 	if (CHIP_IS_E1x(bp)) {
8201 		main_mem_size = HC_REG_MAIN_MEMORY_SIZE / 2; /*dwords*/
8202 		main_mem_base = HC_REG_MAIN_MEMORY +
8203 				BP_PORT(bp) * (main_mem_size * 4);
8204 		main_mem_prty_clr = HC_REG_HC_PRTY_STS_CLR;
8205 		main_mem_width = 8;
8206 
8207 		val = REG_RD(bp, main_mem_prty_clr);
8208 		if (val)
8209 			DP(NETIF_MSG_HW,
8210 			   "Hmmm... Parity errors in HC block during function init (0x%x)!\n",
8211 			   val);
8212 
8213 		/* Clear "false" parity errors in MSI-X table */
8214 		for (i = main_mem_base;
8215 		     i < main_mem_base + main_mem_size * 4;
8216 		     i += main_mem_width) {
8217 			bnx2x_read_dmae(bp, i, main_mem_width / 4);
8218 			bnx2x_write_dmae(bp, bnx2x_sp_mapping(bp, wb_data),
8219 					 i, main_mem_width / 4);
8220 		}
8221 		/* Clear HC parity attention */
8222 		REG_RD(bp, main_mem_prty_clr);
8223 	}
8224 
8225 #ifdef BNX2X_STOP_ON_ERROR
8226 	/* Enable STORMs SP logging */
8227 	REG_WR8(bp, BAR_USTRORM_INTMEM +
8228 	       USTORM_RECORD_SLOW_PATH_OFFSET(BP_FUNC(bp)), 1);
8229 	REG_WR8(bp, BAR_TSTRORM_INTMEM +
8230 	       TSTORM_RECORD_SLOW_PATH_OFFSET(BP_FUNC(bp)), 1);
8231 	REG_WR8(bp, BAR_CSTRORM_INTMEM +
8232 	       CSTORM_RECORD_SLOW_PATH_OFFSET(BP_FUNC(bp)), 1);
8233 	REG_WR8(bp, BAR_XSTRORM_INTMEM +
8234 	       XSTORM_RECORD_SLOW_PATH_OFFSET(BP_FUNC(bp)), 1);
8235 #endif
8236 
8237 	bnx2x_phy_probe(&bp->link_params);
8238 
8239 	return 0;
8240 }
8241 
8242 void bnx2x_free_mem_cnic(struct bnx2x *bp)
8243 {
8244 	bnx2x_ilt_mem_op_cnic(bp, ILT_MEMOP_FREE);
8245 
8246 	if (!CHIP_IS_E1x(bp))
8247 		BNX2X_PCI_FREE(bp->cnic_sb.e2_sb, bp->cnic_sb_mapping,
8248 			       sizeof(struct host_hc_status_block_e2));
8249 	else
8250 		BNX2X_PCI_FREE(bp->cnic_sb.e1x_sb, bp->cnic_sb_mapping,
8251 			       sizeof(struct host_hc_status_block_e1x));
8252 
8253 	BNX2X_PCI_FREE(bp->t2, bp->t2_mapping, SRC_T2_SZ);
8254 }
8255 
8256 void bnx2x_free_mem(struct bnx2x *bp)
8257 {
8258 	int i;
8259 
8260 	BNX2X_PCI_FREE(bp->fw_stats, bp->fw_stats_mapping,
8261 		       bp->fw_stats_data_sz + bp->fw_stats_req_sz);
8262 
8263 	if (IS_VF(bp))
8264 		return;
8265 
8266 	BNX2X_PCI_FREE(bp->def_status_blk, bp->def_status_blk_mapping,
8267 		       sizeof(struct host_sp_status_block));
8268 
8269 	BNX2X_PCI_FREE(bp->slowpath, bp->slowpath_mapping,
8270 		       sizeof(struct bnx2x_slowpath));
8271 
8272 	for (i = 0; i < L2_ILT_LINES(bp); i++)
8273 		BNX2X_PCI_FREE(bp->context[i].vcxt, bp->context[i].cxt_mapping,
8274 			       bp->context[i].size);
8275 	bnx2x_ilt_mem_op(bp, ILT_MEMOP_FREE);
8276 
8277 	BNX2X_FREE(bp->ilt->lines);
8278 
8279 	BNX2X_PCI_FREE(bp->spq, bp->spq_mapping, BCM_PAGE_SIZE);
8280 
8281 	BNX2X_PCI_FREE(bp->eq_ring, bp->eq_mapping,
8282 		       BCM_PAGE_SIZE * NUM_EQ_PAGES);
8283 
8284 	BNX2X_PCI_FREE(bp->t2, bp->t2_mapping, SRC_T2_SZ);
8285 
8286 	bnx2x_iov_free_mem(bp);
8287 }
8288 
8289 int bnx2x_alloc_mem_cnic(struct bnx2x *bp)
8290 {
8291 	if (!CHIP_IS_E1x(bp)) {
8292 		/* size = the status block + ramrod buffers */
8293 		bp->cnic_sb.e2_sb = BNX2X_PCI_ALLOC(&bp->cnic_sb_mapping,
8294 						    sizeof(struct host_hc_status_block_e2));
8295 		if (!bp->cnic_sb.e2_sb)
8296 			goto alloc_mem_err;
8297 	} else {
8298 		bp->cnic_sb.e1x_sb = BNX2X_PCI_ALLOC(&bp->cnic_sb_mapping,
8299 						     sizeof(struct host_hc_status_block_e1x));
8300 		if (!bp->cnic_sb.e1x_sb)
8301 			goto alloc_mem_err;
8302 	}
8303 
8304 	if (CONFIGURE_NIC_MODE(bp) && !bp->t2) {
8305 		/* allocate searcher T2 table, as it wasn't allocated before */
8306 		bp->t2 = BNX2X_PCI_ALLOC(&bp->t2_mapping, SRC_T2_SZ);
8307 		if (!bp->t2)
8308 			goto alloc_mem_err;
8309 	}
8310 
8311 	/* write address to which L5 should insert its values */
8312 	bp->cnic_eth_dev.addr_drv_info_to_mcp =
8313 		&bp->slowpath->drv_info_to_mcp;
8314 
8315 	if (bnx2x_ilt_mem_op_cnic(bp, ILT_MEMOP_ALLOC))
8316 		goto alloc_mem_err;
8317 
8318 	return 0;
8319 
8320 alloc_mem_err:
8321 	bnx2x_free_mem_cnic(bp);
8322 	BNX2X_ERR("Can't allocate memory\n");
8323 	return -ENOMEM;
8324 }
8325 
8326 int bnx2x_alloc_mem(struct bnx2x *bp)
8327 {
8328 	int i, allocated, context_size;
8329 
8330 	if (!CONFIGURE_NIC_MODE(bp) && !bp->t2) {
8331 		/* allocate searcher T2 table */
8332 		bp->t2 = BNX2X_PCI_ALLOC(&bp->t2_mapping, SRC_T2_SZ);
8333 		if (!bp->t2)
8334 			goto alloc_mem_err;
8335 	}
8336 
8337 	bp->def_status_blk = BNX2X_PCI_ALLOC(&bp->def_status_blk_mapping,
8338 					     sizeof(struct host_sp_status_block));
8339 	if (!bp->def_status_blk)
8340 		goto alloc_mem_err;
8341 
8342 	bp->slowpath = BNX2X_PCI_ALLOC(&bp->slowpath_mapping,
8343 				       sizeof(struct bnx2x_slowpath));
8344 	if (!bp->slowpath)
8345 		goto alloc_mem_err;
8346 
8347 	/* Allocate memory for CDU context:
8348 	 * This memory is allocated separately and not in the generic ILT
8349 	 * functions because CDU differs in few aspects:
8350 	 * 1. There are multiple entities allocating memory for context -
8351 	 * 'regular' driver, CNIC and SRIOV driver. Each separately controls
8352 	 * its own ILT lines.
8353 	 * 2. Since CDU page-size is not a single 4KB page (which is the case
8354 	 * for the other ILT clients), to be efficient we want to support
8355 	 * allocation of sub-page-size in the last entry.
8356 	 * 3. Context pointers are used by the driver to pass to FW / update
8357 	 * the context (for the other ILT clients the pointers are used just to
8358 	 * free the memory during unload).
8359 	 */
8360 	context_size = sizeof(union cdu_context) * BNX2X_L2_CID_COUNT(bp);
8361 
8362 	for (i = 0, allocated = 0; allocated < context_size; i++) {
8363 		bp->context[i].size = min(CDU_ILT_PAGE_SZ,
8364 					  (context_size - allocated));
8365 		bp->context[i].vcxt = BNX2X_PCI_ALLOC(&bp->context[i].cxt_mapping,
8366 						      bp->context[i].size);
8367 		if (!bp->context[i].vcxt)
8368 			goto alloc_mem_err;
8369 		allocated += bp->context[i].size;
8370 	}
8371 	bp->ilt->lines = kcalloc(ILT_MAX_LINES, sizeof(struct ilt_line),
8372 				 GFP_KERNEL);
8373 	if (!bp->ilt->lines)
8374 		goto alloc_mem_err;
8375 
8376 	if (bnx2x_ilt_mem_op(bp, ILT_MEMOP_ALLOC))
8377 		goto alloc_mem_err;
8378 
8379 	if (bnx2x_iov_alloc_mem(bp))
8380 		goto alloc_mem_err;
8381 
8382 	/* Slow path ring */
8383 	bp->spq = BNX2X_PCI_ALLOC(&bp->spq_mapping, BCM_PAGE_SIZE);
8384 	if (!bp->spq)
8385 		goto alloc_mem_err;
8386 
8387 	/* EQ */
8388 	bp->eq_ring = BNX2X_PCI_ALLOC(&bp->eq_mapping,
8389 				      BCM_PAGE_SIZE * NUM_EQ_PAGES);
8390 	if (!bp->eq_ring)
8391 		goto alloc_mem_err;
8392 
8393 	return 0;
8394 
8395 alloc_mem_err:
8396 	bnx2x_free_mem(bp);
8397 	BNX2X_ERR("Can't allocate memory\n");
8398 	return -ENOMEM;
8399 }
8400 
8401 /*
8402  * Init service functions
8403  */
8404 
8405 int bnx2x_set_mac_one(struct bnx2x *bp, u8 *mac,
8406 		      struct bnx2x_vlan_mac_obj *obj, bool set,
8407 		      int mac_type, unsigned long *ramrod_flags)
8408 {
8409 	int rc;
8410 	struct bnx2x_vlan_mac_ramrod_params ramrod_param;
8411 
8412 	memset(&ramrod_param, 0, sizeof(ramrod_param));
8413 
8414 	/* Fill general parameters */
8415 	ramrod_param.vlan_mac_obj = obj;
8416 	ramrod_param.ramrod_flags = *ramrod_flags;
8417 
8418 	/* Fill a user request section if needed */
8419 	if (!test_bit(RAMROD_CONT, ramrod_flags)) {
8420 		memcpy(ramrod_param.user_req.u.mac.mac, mac, ETH_ALEN);
8421 
8422 		__set_bit(mac_type, &ramrod_param.user_req.vlan_mac_flags);
8423 
8424 		/* Set the command: ADD or DEL */
8425 		if (set)
8426 			ramrod_param.user_req.cmd = BNX2X_VLAN_MAC_ADD;
8427 		else
8428 			ramrod_param.user_req.cmd = BNX2X_VLAN_MAC_DEL;
8429 	}
8430 
8431 	rc = bnx2x_config_vlan_mac(bp, &ramrod_param);
8432 
8433 	if (rc == -EEXIST) {
8434 		DP(BNX2X_MSG_SP, "Failed to schedule ADD operations: %d\n", rc);
8435 		/* do not treat adding same MAC as error */
8436 		rc = 0;
8437 	} else if (rc < 0)
8438 		BNX2X_ERR("%s MAC failed\n", (set ? "Set" : "Del"));
8439 
8440 	return rc;
8441 }
8442 
8443 int bnx2x_set_vlan_one(struct bnx2x *bp, u16 vlan,
8444 		       struct bnx2x_vlan_mac_obj *obj, bool set,
8445 		       unsigned long *ramrod_flags)
8446 {
8447 	int rc;
8448 	struct bnx2x_vlan_mac_ramrod_params ramrod_param;
8449 
8450 	memset(&ramrod_param, 0, sizeof(ramrod_param));
8451 
8452 	/* Fill general parameters */
8453 	ramrod_param.vlan_mac_obj = obj;
8454 	ramrod_param.ramrod_flags = *ramrod_flags;
8455 
8456 	/* Fill a user request section if needed */
8457 	if (!test_bit(RAMROD_CONT, ramrod_flags)) {
8458 		ramrod_param.user_req.u.vlan.vlan = vlan;
8459 		__set_bit(BNX2X_VLAN, &ramrod_param.user_req.vlan_mac_flags);
8460 		/* Set the command: ADD or DEL */
8461 		if (set)
8462 			ramrod_param.user_req.cmd = BNX2X_VLAN_MAC_ADD;
8463 		else
8464 			ramrod_param.user_req.cmd = BNX2X_VLAN_MAC_DEL;
8465 	}
8466 
8467 	rc = bnx2x_config_vlan_mac(bp, &ramrod_param);
8468 
8469 	if (rc == -EEXIST) {
8470 		/* Do not treat adding same vlan as error. */
8471 		DP(BNX2X_MSG_SP, "Failed to schedule ADD operations: %d\n", rc);
8472 		rc = 0;
8473 	} else if (rc < 0) {
8474 		BNX2X_ERR("%s VLAN failed\n", (set ? "Set" : "Del"));
8475 	}
8476 
8477 	return rc;
8478 }
8479 
8480 void bnx2x_clear_vlan_info(struct bnx2x *bp)
8481 {
8482 	struct bnx2x_vlan_entry *vlan;
8483 
8484 	/* Mark that hw forgot all entries */
8485 	list_for_each_entry(vlan, &bp->vlan_reg, link)
8486 		vlan->hw = false;
8487 
8488 	bp->vlan_cnt = 0;
8489 }
8490 
8491 static int bnx2x_del_all_vlans(struct bnx2x *bp)
8492 {
8493 	struct bnx2x_vlan_mac_obj *vlan_obj = &bp->sp_objs[0].vlan_obj;
8494 	unsigned long ramrod_flags = 0, vlan_flags = 0;
8495 	int rc;
8496 
8497 	__set_bit(RAMROD_COMP_WAIT, &ramrod_flags);
8498 	__set_bit(BNX2X_VLAN, &vlan_flags);
8499 	rc = vlan_obj->delete_all(bp, vlan_obj, &vlan_flags, &ramrod_flags);
8500 	if (rc)
8501 		return rc;
8502 
8503 	bnx2x_clear_vlan_info(bp);
8504 
8505 	return 0;
8506 }
8507 
8508 int bnx2x_del_all_macs(struct bnx2x *bp,
8509 		       struct bnx2x_vlan_mac_obj *mac_obj,
8510 		       int mac_type, bool wait_for_comp)
8511 {
8512 	int rc;
8513 	unsigned long ramrod_flags = 0, vlan_mac_flags = 0;
8514 
8515 	/* Wait for completion of requested */
8516 	if (wait_for_comp)
8517 		__set_bit(RAMROD_COMP_WAIT, &ramrod_flags);
8518 
8519 	/* Set the mac type of addresses we want to clear */
8520 	__set_bit(mac_type, &vlan_mac_flags);
8521 
8522 	rc = mac_obj->delete_all(bp, mac_obj, &vlan_mac_flags, &ramrod_flags);
8523 	if (rc < 0)
8524 		BNX2X_ERR("Failed to delete MACs: %d\n", rc);
8525 
8526 	return rc;
8527 }
8528 
8529 int bnx2x_set_eth_mac(struct bnx2x *bp, bool set)
8530 {
8531 	if (IS_PF(bp)) {
8532 		unsigned long ramrod_flags = 0;
8533 
8534 		DP(NETIF_MSG_IFUP, "Adding Eth MAC\n");
8535 		__set_bit(RAMROD_COMP_WAIT, &ramrod_flags);
8536 		return bnx2x_set_mac_one(bp, bp->dev->dev_addr,
8537 					 &bp->sp_objs->mac_obj, set,
8538 					 BNX2X_ETH_MAC, &ramrod_flags);
8539 	} else { /* vf */
8540 		return bnx2x_vfpf_config_mac(bp, bp->dev->dev_addr,
8541 					     bp->fp->index, set);
8542 	}
8543 }
8544 
8545 int bnx2x_setup_leading(struct bnx2x *bp)
8546 {
8547 	if (IS_PF(bp))
8548 		return bnx2x_setup_queue(bp, &bp->fp[0], true);
8549 	else /* VF */
8550 		return bnx2x_vfpf_setup_q(bp, &bp->fp[0], true);
8551 }
8552 
8553 /**
8554  * bnx2x_set_int_mode - configure interrupt mode
8555  *
8556  * @bp:		driver handle
8557  *
8558  * In case of MSI-X it will also try to enable MSI-X.
8559  */
8560 int bnx2x_set_int_mode(struct bnx2x *bp)
8561 {
8562 	int rc = 0;
8563 
8564 	if (IS_VF(bp) && int_mode != BNX2X_INT_MODE_MSIX) {
8565 		BNX2X_ERR("VF not loaded since interrupt mode not msix\n");
8566 		return -EINVAL;
8567 	}
8568 
8569 	switch (int_mode) {
8570 	case BNX2X_INT_MODE_MSIX:
8571 		/* attempt to enable msix */
8572 		rc = bnx2x_enable_msix(bp);
8573 
8574 		/* msix attained */
8575 		if (!rc)
8576 			return 0;
8577 
8578 		/* vfs use only msix */
8579 		if (rc && IS_VF(bp))
8580 			return rc;
8581 
8582 		/* failed to enable multiple MSI-X */
8583 		BNX2X_DEV_INFO("Failed to enable multiple MSI-X (%d), set number of queues to %d\n",
8584 			       bp->num_queues,
8585 			       1 + bp->num_cnic_queues);
8586 
8587 		/* fall through */
8588 	case BNX2X_INT_MODE_MSI:
8589 		bnx2x_enable_msi(bp);
8590 
8591 		/* fall through */
8592 	case BNX2X_INT_MODE_INTX:
8593 		bp->num_ethernet_queues = 1;
8594 		bp->num_queues = bp->num_ethernet_queues + bp->num_cnic_queues;
8595 		BNX2X_DEV_INFO("set number of queues to 1\n");
8596 		break;
8597 	default:
8598 		BNX2X_DEV_INFO("unknown value in int_mode module parameter\n");
8599 		return -EINVAL;
8600 	}
8601 	return 0;
8602 }
8603 
8604 /* must be called prior to any HW initializations */
8605 static inline u16 bnx2x_cid_ilt_lines(struct bnx2x *bp)
8606 {
8607 	if (IS_SRIOV(bp))
8608 		return (BNX2X_FIRST_VF_CID + BNX2X_VF_CIDS)/ILT_PAGE_CIDS;
8609 	return L2_ILT_LINES(bp);
8610 }
8611 
8612 void bnx2x_ilt_set_info(struct bnx2x *bp)
8613 {
8614 	struct ilt_client_info *ilt_client;
8615 	struct bnx2x_ilt *ilt = BP_ILT(bp);
8616 	u16 line = 0;
8617 
8618 	ilt->start_line = FUNC_ILT_BASE(BP_FUNC(bp));
8619 	DP(BNX2X_MSG_SP, "ilt starts at line %d\n", ilt->start_line);
8620 
8621 	/* CDU */
8622 	ilt_client = &ilt->clients[ILT_CLIENT_CDU];
8623 	ilt_client->client_num = ILT_CLIENT_CDU;
8624 	ilt_client->page_size = CDU_ILT_PAGE_SZ;
8625 	ilt_client->flags = ILT_CLIENT_SKIP_MEM;
8626 	ilt_client->start = line;
8627 	line += bnx2x_cid_ilt_lines(bp);
8628 
8629 	if (CNIC_SUPPORT(bp))
8630 		line += CNIC_ILT_LINES;
8631 	ilt_client->end = line - 1;
8632 
8633 	DP(NETIF_MSG_IFUP, "ilt client[CDU]: start %d, end %d, psz 0x%x, flags 0x%x, hw psz %d\n",
8634 	   ilt_client->start,
8635 	   ilt_client->end,
8636 	   ilt_client->page_size,
8637 	   ilt_client->flags,
8638 	   ilog2(ilt_client->page_size >> 12));
8639 
8640 	/* QM */
8641 	if (QM_INIT(bp->qm_cid_count)) {
8642 		ilt_client = &ilt->clients[ILT_CLIENT_QM];
8643 		ilt_client->client_num = ILT_CLIENT_QM;
8644 		ilt_client->page_size = QM_ILT_PAGE_SZ;
8645 		ilt_client->flags = 0;
8646 		ilt_client->start = line;
8647 
8648 		/* 4 bytes for each cid */
8649 		line += DIV_ROUND_UP(bp->qm_cid_count * QM_QUEUES_PER_FUNC * 4,
8650 							 QM_ILT_PAGE_SZ);
8651 
8652 		ilt_client->end = line - 1;
8653 
8654 		DP(NETIF_MSG_IFUP,
8655 		   "ilt client[QM]: start %d, end %d, psz 0x%x, flags 0x%x, hw psz %d\n",
8656 		   ilt_client->start,
8657 		   ilt_client->end,
8658 		   ilt_client->page_size,
8659 		   ilt_client->flags,
8660 		   ilog2(ilt_client->page_size >> 12));
8661 	}
8662 
8663 	if (CNIC_SUPPORT(bp)) {
8664 		/* SRC */
8665 		ilt_client = &ilt->clients[ILT_CLIENT_SRC];
8666 		ilt_client->client_num = ILT_CLIENT_SRC;
8667 		ilt_client->page_size = SRC_ILT_PAGE_SZ;
8668 		ilt_client->flags = 0;
8669 		ilt_client->start = line;
8670 		line += SRC_ILT_LINES;
8671 		ilt_client->end = line - 1;
8672 
8673 		DP(NETIF_MSG_IFUP,
8674 		   "ilt client[SRC]: start %d, end %d, psz 0x%x, flags 0x%x, hw psz %d\n",
8675 		   ilt_client->start,
8676 		   ilt_client->end,
8677 		   ilt_client->page_size,
8678 		   ilt_client->flags,
8679 		   ilog2(ilt_client->page_size >> 12));
8680 
8681 		/* TM */
8682 		ilt_client = &ilt->clients[ILT_CLIENT_TM];
8683 		ilt_client->client_num = ILT_CLIENT_TM;
8684 		ilt_client->page_size = TM_ILT_PAGE_SZ;
8685 		ilt_client->flags = 0;
8686 		ilt_client->start = line;
8687 		line += TM_ILT_LINES;
8688 		ilt_client->end = line - 1;
8689 
8690 		DP(NETIF_MSG_IFUP,
8691 		   "ilt client[TM]: start %d, end %d, psz 0x%x, flags 0x%x, hw psz %d\n",
8692 		   ilt_client->start,
8693 		   ilt_client->end,
8694 		   ilt_client->page_size,
8695 		   ilt_client->flags,
8696 		   ilog2(ilt_client->page_size >> 12));
8697 	}
8698 
8699 	BUG_ON(line > ILT_MAX_LINES);
8700 }
8701 
8702 /**
8703  * bnx2x_pf_q_prep_init - prepare INIT transition parameters
8704  *
8705  * @bp:			driver handle
8706  * @fp:			pointer to fastpath
8707  * @init_params:	pointer to parameters structure
8708  *
8709  * parameters configured:
8710  *      - HC configuration
8711  *      - Queue's CDU context
8712  */
8713 static void bnx2x_pf_q_prep_init(struct bnx2x *bp,
8714 	struct bnx2x_fastpath *fp, struct bnx2x_queue_init_params *init_params)
8715 {
8716 	u8 cos;
8717 	int cxt_index, cxt_offset;
8718 
8719 	/* FCoE Queue uses Default SB, thus has no HC capabilities */
8720 	if (!IS_FCOE_FP(fp)) {
8721 		__set_bit(BNX2X_Q_FLG_HC, &init_params->rx.flags);
8722 		__set_bit(BNX2X_Q_FLG_HC, &init_params->tx.flags);
8723 
8724 		/* If HC is supported, enable host coalescing in the transition
8725 		 * to INIT state.
8726 		 */
8727 		__set_bit(BNX2X_Q_FLG_HC_EN, &init_params->rx.flags);
8728 		__set_bit(BNX2X_Q_FLG_HC_EN, &init_params->tx.flags);
8729 
8730 		/* HC rate */
8731 		init_params->rx.hc_rate = bp->rx_ticks ?
8732 			(1000000 / bp->rx_ticks) : 0;
8733 		init_params->tx.hc_rate = bp->tx_ticks ?
8734 			(1000000 / bp->tx_ticks) : 0;
8735 
8736 		/* FW SB ID */
8737 		init_params->rx.fw_sb_id = init_params->tx.fw_sb_id =
8738 			fp->fw_sb_id;
8739 
8740 		/*
8741 		 * CQ index among the SB indices: FCoE clients uses the default
8742 		 * SB, therefore it's different.
8743 		 */
8744 		init_params->rx.sb_cq_index = HC_INDEX_ETH_RX_CQ_CONS;
8745 		init_params->tx.sb_cq_index = HC_INDEX_ETH_FIRST_TX_CQ_CONS;
8746 	}
8747 
8748 	/* set maximum number of COSs supported by this queue */
8749 	init_params->max_cos = fp->max_cos;
8750 
8751 	DP(NETIF_MSG_IFUP, "fp: %d setting queue params max cos to: %d\n",
8752 	    fp->index, init_params->max_cos);
8753 
8754 	/* set the context pointers queue object */
8755 	for (cos = FIRST_TX_COS_INDEX; cos < init_params->max_cos; cos++) {
8756 		cxt_index = fp->txdata_ptr[cos]->cid / ILT_PAGE_CIDS;
8757 		cxt_offset = fp->txdata_ptr[cos]->cid - (cxt_index *
8758 				ILT_PAGE_CIDS);
8759 		init_params->cxts[cos] =
8760 			&bp->context[cxt_index].vcxt[cxt_offset].eth;
8761 	}
8762 }
8763 
8764 static int bnx2x_setup_tx_only(struct bnx2x *bp, struct bnx2x_fastpath *fp,
8765 			struct bnx2x_queue_state_params *q_params,
8766 			struct bnx2x_queue_setup_tx_only_params *tx_only_params,
8767 			int tx_index, bool leading)
8768 {
8769 	memset(tx_only_params, 0, sizeof(*tx_only_params));
8770 
8771 	/* Set the command */
8772 	q_params->cmd = BNX2X_Q_CMD_SETUP_TX_ONLY;
8773 
8774 	/* Set tx-only QUEUE flags: don't zero statistics */
8775 	tx_only_params->flags = bnx2x_get_common_flags(bp, fp, false);
8776 
8777 	/* choose the index of the cid to send the slow path on */
8778 	tx_only_params->cid_index = tx_index;
8779 
8780 	/* Set general TX_ONLY_SETUP parameters */
8781 	bnx2x_pf_q_prep_general(bp, fp, &tx_only_params->gen_params, tx_index);
8782 
8783 	/* Set Tx TX_ONLY_SETUP parameters */
8784 	bnx2x_pf_tx_q_prep(bp, fp, &tx_only_params->txq_params, tx_index);
8785 
8786 	DP(NETIF_MSG_IFUP,
8787 	   "preparing to send tx-only ramrod for connection: cos %d, primary cid %d, cid %d, client id %d, sp-client id %d, flags %lx\n",
8788 	   tx_index, q_params->q_obj->cids[FIRST_TX_COS_INDEX],
8789 	   q_params->q_obj->cids[tx_index], q_params->q_obj->cl_id,
8790 	   tx_only_params->gen_params.spcl_id, tx_only_params->flags);
8791 
8792 	/* send the ramrod */
8793 	return bnx2x_queue_state_change(bp, q_params);
8794 }
8795 
8796 /**
8797  * bnx2x_setup_queue - setup queue
8798  *
8799  * @bp:		driver handle
8800  * @fp:		pointer to fastpath
8801  * @leading:	is leading
8802  *
8803  * This function performs 2 steps in a Queue state machine
8804  *      actually: 1) RESET->INIT 2) INIT->SETUP
8805  */
8806 
8807 int bnx2x_setup_queue(struct bnx2x *bp, struct bnx2x_fastpath *fp,
8808 		       bool leading)
8809 {
8810 	struct bnx2x_queue_state_params q_params = {NULL};
8811 	struct bnx2x_queue_setup_params *setup_params =
8812 						&q_params.params.setup;
8813 	struct bnx2x_queue_setup_tx_only_params *tx_only_params =
8814 						&q_params.params.tx_only;
8815 	int rc;
8816 	u8 tx_index;
8817 
8818 	DP(NETIF_MSG_IFUP, "setting up queue %d\n", fp->index);
8819 
8820 	/* reset IGU state skip FCoE L2 queue */
8821 	if (!IS_FCOE_FP(fp))
8822 		bnx2x_ack_sb(bp, fp->igu_sb_id, USTORM_ID, 0,
8823 			     IGU_INT_ENABLE, 0);
8824 
8825 	q_params.q_obj = &bnx2x_sp_obj(bp, fp).q_obj;
8826 	/* We want to wait for completion in this context */
8827 	__set_bit(RAMROD_COMP_WAIT, &q_params.ramrod_flags);
8828 
8829 	/* Prepare the INIT parameters */
8830 	bnx2x_pf_q_prep_init(bp, fp, &q_params.params.init);
8831 
8832 	/* Set the command */
8833 	q_params.cmd = BNX2X_Q_CMD_INIT;
8834 
8835 	/* Change the state to INIT */
8836 	rc = bnx2x_queue_state_change(bp, &q_params);
8837 	if (rc) {
8838 		BNX2X_ERR("Queue(%d) INIT failed\n", fp->index);
8839 		return rc;
8840 	}
8841 
8842 	DP(NETIF_MSG_IFUP, "init complete\n");
8843 
8844 	/* Now move the Queue to the SETUP state... */
8845 	memset(setup_params, 0, sizeof(*setup_params));
8846 
8847 	/* Set QUEUE flags */
8848 	setup_params->flags = bnx2x_get_q_flags(bp, fp, leading);
8849 
8850 	/* Set general SETUP parameters */
8851 	bnx2x_pf_q_prep_general(bp, fp, &setup_params->gen_params,
8852 				FIRST_TX_COS_INDEX);
8853 
8854 	bnx2x_pf_rx_q_prep(bp, fp, &setup_params->pause_params,
8855 			    &setup_params->rxq_params);
8856 
8857 	bnx2x_pf_tx_q_prep(bp, fp, &setup_params->txq_params,
8858 			   FIRST_TX_COS_INDEX);
8859 
8860 	/* Set the command */
8861 	q_params.cmd = BNX2X_Q_CMD_SETUP;
8862 
8863 	if (IS_FCOE_FP(fp))
8864 		bp->fcoe_init = true;
8865 
8866 	/* Change the state to SETUP */
8867 	rc = bnx2x_queue_state_change(bp, &q_params);
8868 	if (rc) {
8869 		BNX2X_ERR("Queue(%d) SETUP failed\n", fp->index);
8870 		return rc;
8871 	}
8872 
8873 	/* loop through the relevant tx-only indices */
8874 	for (tx_index = FIRST_TX_ONLY_COS_INDEX;
8875 	      tx_index < fp->max_cos;
8876 	      tx_index++) {
8877 
8878 		/* prepare and send tx-only ramrod*/
8879 		rc = bnx2x_setup_tx_only(bp, fp, &q_params,
8880 					  tx_only_params, tx_index, leading);
8881 		if (rc) {
8882 			BNX2X_ERR("Queue(%d.%d) TX_ONLY_SETUP failed\n",
8883 				  fp->index, tx_index);
8884 			return rc;
8885 		}
8886 	}
8887 
8888 	return rc;
8889 }
8890 
8891 static int bnx2x_stop_queue(struct bnx2x *bp, int index)
8892 {
8893 	struct bnx2x_fastpath *fp = &bp->fp[index];
8894 	struct bnx2x_fp_txdata *txdata;
8895 	struct bnx2x_queue_state_params q_params = {NULL};
8896 	int rc, tx_index;
8897 
8898 	DP(NETIF_MSG_IFDOWN, "stopping queue %d cid %d\n", index, fp->cid);
8899 
8900 	q_params.q_obj = &bnx2x_sp_obj(bp, fp).q_obj;
8901 	/* We want to wait for completion in this context */
8902 	__set_bit(RAMROD_COMP_WAIT, &q_params.ramrod_flags);
8903 
8904 	/* close tx-only connections */
8905 	for (tx_index = FIRST_TX_ONLY_COS_INDEX;
8906 	     tx_index < fp->max_cos;
8907 	     tx_index++){
8908 
8909 		/* ascertain this is a normal queue*/
8910 		txdata = fp->txdata_ptr[tx_index];
8911 
8912 		DP(NETIF_MSG_IFDOWN, "stopping tx-only queue %d\n",
8913 							txdata->txq_index);
8914 
8915 		/* send halt terminate on tx-only connection */
8916 		q_params.cmd = BNX2X_Q_CMD_TERMINATE;
8917 		memset(&q_params.params.terminate, 0,
8918 		       sizeof(q_params.params.terminate));
8919 		q_params.params.terminate.cid_index = tx_index;
8920 
8921 		rc = bnx2x_queue_state_change(bp, &q_params);
8922 		if (rc)
8923 			return rc;
8924 
8925 		/* send halt terminate on tx-only connection */
8926 		q_params.cmd = BNX2X_Q_CMD_CFC_DEL;
8927 		memset(&q_params.params.cfc_del, 0,
8928 		       sizeof(q_params.params.cfc_del));
8929 		q_params.params.cfc_del.cid_index = tx_index;
8930 		rc = bnx2x_queue_state_change(bp, &q_params);
8931 		if (rc)
8932 			return rc;
8933 	}
8934 	/* Stop the primary connection: */
8935 	/* ...halt the connection */
8936 	q_params.cmd = BNX2X_Q_CMD_HALT;
8937 	rc = bnx2x_queue_state_change(bp, &q_params);
8938 	if (rc)
8939 		return rc;
8940 
8941 	/* ...terminate the connection */
8942 	q_params.cmd = BNX2X_Q_CMD_TERMINATE;
8943 	memset(&q_params.params.terminate, 0,
8944 	       sizeof(q_params.params.terminate));
8945 	q_params.params.terminate.cid_index = FIRST_TX_COS_INDEX;
8946 	rc = bnx2x_queue_state_change(bp, &q_params);
8947 	if (rc)
8948 		return rc;
8949 	/* ...delete cfc entry */
8950 	q_params.cmd = BNX2X_Q_CMD_CFC_DEL;
8951 	memset(&q_params.params.cfc_del, 0,
8952 	       sizeof(q_params.params.cfc_del));
8953 	q_params.params.cfc_del.cid_index = FIRST_TX_COS_INDEX;
8954 	return bnx2x_queue_state_change(bp, &q_params);
8955 }
8956 
8957 static void bnx2x_reset_func(struct bnx2x *bp)
8958 {
8959 	int port = BP_PORT(bp);
8960 	int func = BP_FUNC(bp);
8961 	int i;
8962 
8963 	/* Disable the function in the FW */
8964 	REG_WR8(bp, BAR_XSTRORM_INTMEM + XSTORM_FUNC_EN_OFFSET(func), 0);
8965 	REG_WR8(bp, BAR_CSTRORM_INTMEM + CSTORM_FUNC_EN_OFFSET(func), 0);
8966 	REG_WR8(bp, BAR_TSTRORM_INTMEM + TSTORM_FUNC_EN_OFFSET(func), 0);
8967 	REG_WR8(bp, BAR_USTRORM_INTMEM + USTORM_FUNC_EN_OFFSET(func), 0);
8968 
8969 	/* FP SBs */
8970 	for_each_eth_queue(bp, i) {
8971 		struct bnx2x_fastpath *fp = &bp->fp[i];
8972 		REG_WR8(bp, BAR_CSTRORM_INTMEM +
8973 			   CSTORM_STATUS_BLOCK_DATA_STATE_OFFSET(fp->fw_sb_id),
8974 			   SB_DISABLED);
8975 	}
8976 
8977 	if (CNIC_LOADED(bp))
8978 		/* CNIC SB */
8979 		REG_WR8(bp, BAR_CSTRORM_INTMEM +
8980 			CSTORM_STATUS_BLOCK_DATA_STATE_OFFSET
8981 			(bnx2x_cnic_fw_sb_id(bp)), SB_DISABLED);
8982 
8983 	/* SP SB */
8984 	REG_WR8(bp, BAR_CSTRORM_INTMEM +
8985 		CSTORM_SP_STATUS_BLOCK_DATA_STATE_OFFSET(func),
8986 		SB_DISABLED);
8987 
8988 	for (i = 0; i < XSTORM_SPQ_DATA_SIZE / 4; i++)
8989 		REG_WR(bp, BAR_XSTRORM_INTMEM + XSTORM_SPQ_DATA_OFFSET(func),
8990 		       0);
8991 
8992 	/* Configure IGU */
8993 	if (bp->common.int_block == INT_BLOCK_HC) {
8994 		REG_WR(bp, HC_REG_LEADING_EDGE_0 + port*8, 0);
8995 		REG_WR(bp, HC_REG_TRAILING_EDGE_0 + port*8, 0);
8996 	} else {
8997 		REG_WR(bp, IGU_REG_LEADING_EDGE_LATCH, 0);
8998 		REG_WR(bp, IGU_REG_TRAILING_EDGE_LATCH, 0);
8999 	}
9000 
9001 	if (CNIC_LOADED(bp)) {
9002 		/* Disable Timer scan */
9003 		REG_WR(bp, TM_REG_EN_LINEAR0_TIMER + port*4, 0);
9004 		/*
9005 		 * Wait for at least 10ms and up to 2 second for the timers
9006 		 * scan to complete
9007 		 */
9008 		for (i = 0; i < 200; i++) {
9009 			usleep_range(10000, 20000);
9010 			if (!REG_RD(bp, TM_REG_LIN0_SCAN_ON + port*4))
9011 				break;
9012 		}
9013 	}
9014 	/* Clear ILT */
9015 	bnx2x_clear_func_ilt(bp, func);
9016 
9017 	/* Timers workaround bug for E2: if this is vnic-3,
9018 	 * we need to set the entire ilt range for this timers.
9019 	 */
9020 	if (!CHIP_IS_E1x(bp) && BP_VN(bp) == 3) {
9021 		struct ilt_client_info ilt_cli;
9022 		/* use dummy TM client */
9023 		memset(&ilt_cli, 0, sizeof(struct ilt_client_info));
9024 		ilt_cli.start = 0;
9025 		ilt_cli.end = ILT_NUM_PAGE_ENTRIES - 1;
9026 		ilt_cli.client_num = ILT_CLIENT_TM;
9027 
9028 		bnx2x_ilt_boundry_init_op(bp, &ilt_cli, 0, INITOP_CLEAR);
9029 	}
9030 
9031 	/* this assumes that reset_port() called before reset_func()*/
9032 	if (!CHIP_IS_E1x(bp))
9033 		bnx2x_pf_disable(bp);
9034 
9035 	bp->dmae_ready = 0;
9036 }
9037 
9038 static void bnx2x_reset_port(struct bnx2x *bp)
9039 {
9040 	int port = BP_PORT(bp);
9041 	u32 val;
9042 
9043 	/* Reset physical Link */
9044 	bnx2x__link_reset(bp);
9045 
9046 	REG_WR(bp, NIG_REG_MASK_INTERRUPT_PORT0 + port*4, 0);
9047 
9048 	/* Do not rcv packets to BRB */
9049 	REG_WR(bp, NIG_REG_LLH0_BRB1_DRV_MASK + port*4, 0x0);
9050 	/* Do not direct rcv packets that are not for MCP to the BRB */
9051 	REG_WR(bp, (port ? NIG_REG_LLH1_BRB1_NOT_MCP :
9052 			   NIG_REG_LLH0_BRB1_NOT_MCP), 0x0);
9053 
9054 	/* Configure AEU */
9055 	REG_WR(bp, MISC_REG_AEU_MASK_ATTN_FUNC_0 + port*4, 0);
9056 
9057 	msleep(100);
9058 	/* Check for BRB port occupancy */
9059 	val = REG_RD(bp, BRB1_REG_PORT_NUM_OCC_BLOCKS_0 + port*4);
9060 	if (val)
9061 		DP(NETIF_MSG_IFDOWN,
9062 		   "BRB1 is not empty  %d blocks are occupied\n", val);
9063 
9064 	/* TODO: Close Doorbell port? */
9065 }
9066 
9067 static int bnx2x_reset_hw(struct bnx2x *bp, u32 load_code)
9068 {
9069 	struct bnx2x_func_state_params func_params = {NULL};
9070 
9071 	/* Prepare parameters for function state transitions */
9072 	__set_bit(RAMROD_COMP_WAIT, &func_params.ramrod_flags);
9073 
9074 	func_params.f_obj = &bp->func_obj;
9075 	func_params.cmd = BNX2X_F_CMD_HW_RESET;
9076 
9077 	func_params.params.hw_init.load_phase = load_code;
9078 
9079 	return bnx2x_func_state_change(bp, &func_params);
9080 }
9081 
9082 static int bnx2x_func_stop(struct bnx2x *bp)
9083 {
9084 	struct bnx2x_func_state_params func_params = {NULL};
9085 	int rc;
9086 
9087 	/* Prepare parameters for function state transitions */
9088 	__set_bit(RAMROD_COMP_WAIT, &func_params.ramrod_flags);
9089 	func_params.f_obj = &bp->func_obj;
9090 	func_params.cmd = BNX2X_F_CMD_STOP;
9091 
9092 	/*
9093 	 * Try to stop the function the 'good way'. If fails (in case
9094 	 * of a parity error during bnx2x_chip_cleanup()) and we are
9095 	 * not in a debug mode, perform a state transaction in order to
9096 	 * enable further HW_RESET transaction.
9097 	 */
9098 	rc = bnx2x_func_state_change(bp, &func_params);
9099 	if (rc) {
9100 #ifdef BNX2X_STOP_ON_ERROR
9101 		return rc;
9102 #else
9103 		BNX2X_ERR("FUNC_STOP ramrod failed. Running a dry transaction\n");
9104 		__set_bit(RAMROD_DRV_CLR_ONLY, &func_params.ramrod_flags);
9105 		return bnx2x_func_state_change(bp, &func_params);
9106 #endif
9107 	}
9108 
9109 	return 0;
9110 }
9111 
9112 /**
9113  * bnx2x_send_unload_req - request unload mode from the MCP.
9114  *
9115  * @bp:			driver handle
9116  * @unload_mode:	requested function's unload mode
9117  *
9118  * Return unload mode returned by the MCP: COMMON, PORT or FUNC.
9119  */
9120 u32 bnx2x_send_unload_req(struct bnx2x *bp, int unload_mode)
9121 {
9122 	u32 reset_code = 0;
9123 	int port = BP_PORT(bp);
9124 
9125 	/* Select the UNLOAD request mode */
9126 	if (unload_mode == UNLOAD_NORMAL)
9127 		reset_code = DRV_MSG_CODE_UNLOAD_REQ_WOL_DIS;
9128 
9129 	else if (bp->flags & NO_WOL_FLAG)
9130 		reset_code = DRV_MSG_CODE_UNLOAD_REQ_WOL_MCP;
9131 
9132 	else if (bp->wol) {
9133 		u32 emac_base = port ? GRCBASE_EMAC1 : GRCBASE_EMAC0;
9134 		u8 *mac_addr = bp->dev->dev_addr;
9135 		struct pci_dev *pdev = bp->pdev;
9136 		u32 val;
9137 		u16 pmc;
9138 
9139 		/* The mac address is written to entries 1-4 to
9140 		 * preserve entry 0 which is used by the PMF
9141 		 */
9142 		u8 entry = (BP_VN(bp) + 1)*8;
9143 
9144 		val = (mac_addr[0] << 8) | mac_addr[1];
9145 		EMAC_WR(bp, EMAC_REG_EMAC_MAC_MATCH + entry, val);
9146 
9147 		val = (mac_addr[2] << 24) | (mac_addr[3] << 16) |
9148 		      (mac_addr[4] << 8) | mac_addr[5];
9149 		EMAC_WR(bp, EMAC_REG_EMAC_MAC_MATCH + entry + 4, val);
9150 
9151 		/* Enable the PME and clear the status */
9152 		pci_read_config_word(pdev, pdev->pm_cap + PCI_PM_CTRL, &pmc);
9153 		pmc |= PCI_PM_CTRL_PME_ENABLE | PCI_PM_CTRL_PME_STATUS;
9154 		pci_write_config_word(pdev, pdev->pm_cap + PCI_PM_CTRL, pmc);
9155 
9156 		reset_code = DRV_MSG_CODE_UNLOAD_REQ_WOL_EN;
9157 
9158 	} else
9159 		reset_code = DRV_MSG_CODE_UNLOAD_REQ_WOL_DIS;
9160 
9161 	/* Send the request to the MCP */
9162 	if (!BP_NOMCP(bp))
9163 		reset_code = bnx2x_fw_command(bp, reset_code, 0);
9164 	else {
9165 		int path = BP_PATH(bp);
9166 
9167 		DP(NETIF_MSG_IFDOWN, "NO MCP - load counts[%d]      %d, %d, %d\n",
9168 		   path, bnx2x_load_count[path][0], bnx2x_load_count[path][1],
9169 		   bnx2x_load_count[path][2]);
9170 		bnx2x_load_count[path][0]--;
9171 		bnx2x_load_count[path][1 + port]--;
9172 		DP(NETIF_MSG_IFDOWN, "NO MCP - new load counts[%d]  %d, %d, %d\n",
9173 		   path, bnx2x_load_count[path][0], bnx2x_load_count[path][1],
9174 		   bnx2x_load_count[path][2]);
9175 		if (bnx2x_load_count[path][0] == 0)
9176 			reset_code = FW_MSG_CODE_DRV_UNLOAD_COMMON;
9177 		else if (bnx2x_load_count[path][1 + port] == 0)
9178 			reset_code = FW_MSG_CODE_DRV_UNLOAD_PORT;
9179 		else
9180 			reset_code = FW_MSG_CODE_DRV_UNLOAD_FUNCTION;
9181 	}
9182 
9183 	return reset_code;
9184 }
9185 
9186 /**
9187  * bnx2x_send_unload_done - send UNLOAD_DONE command to the MCP.
9188  *
9189  * @bp:		driver handle
9190  * @keep_link:		true iff link should be kept up
9191  */
9192 void bnx2x_send_unload_done(struct bnx2x *bp, bool keep_link)
9193 {
9194 	u32 reset_param = keep_link ? DRV_MSG_CODE_UNLOAD_SKIP_LINK_RESET : 0;
9195 
9196 	/* Report UNLOAD_DONE to MCP */
9197 	if (!BP_NOMCP(bp))
9198 		bnx2x_fw_command(bp, DRV_MSG_CODE_UNLOAD_DONE, reset_param);
9199 }
9200 
9201 static int bnx2x_func_wait_started(struct bnx2x *bp)
9202 {
9203 	int tout = 50;
9204 	int msix = (bp->flags & USING_MSIX_FLAG) ? 1 : 0;
9205 
9206 	if (!bp->port.pmf)
9207 		return 0;
9208 
9209 	/*
9210 	 * (assumption: No Attention from MCP at this stage)
9211 	 * PMF probably in the middle of TX disable/enable transaction
9212 	 * 1. Sync IRS for default SB
9213 	 * 2. Sync SP queue - this guarantees us that attention handling started
9214 	 * 3. Wait, that TX disable/enable transaction completes
9215 	 *
9216 	 * 1+2 guarantee that if DCBx attention was scheduled it already changed
9217 	 * pending bit of transaction from STARTED-->TX_STOPPED, if we already
9218 	 * received completion for the transaction the state is TX_STOPPED.
9219 	 * State will return to STARTED after completion of TX_STOPPED-->STARTED
9220 	 * transaction.
9221 	 */
9222 
9223 	/* make sure default SB ISR is done */
9224 	if (msix)
9225 		synchronize_irq(bp->msix_table[0].vector);
9226 	else
9227 		synchronize_irq(bp->pdev->irq);
9228 
9229 	flush_workqueue(bnx2x_wq);
9230 	flush_workqueue(bnx2x_iov_wq);
9231 
9232 	while (bnx2x_func_get_state(bp, &bp->func_obj) !=
9233 				BNX2X_F_STATE_STARTED && tout--)
9234 		msleep(20);
9235 
9236 	if (bnx2x_func_get_state(bp, &bp->func_obj) !=
9237 						BNX2X_F_STATE_STARTED) {
9238 #ifdef BNX2X_STOP_ON_ERROR
9239 		BNX2X_ERR("Wrong function state\n");
9240 		return -EBUSY;
9241 #else
9242 		/*
9243 		 * Failed to complete the transaction in a "good way"
9244 		 * Force both transactions with CLR bit
9245 		 */
9246 		struct bnx2x_func_state_params func_params = {NULL};
9247 
9248 		DP(NETIF_MSG_IFDOWN,
9249 		   "Hmmm... Unexpected function state! Forcing STARTED-->TX_STOPPED-->STARTED\n");
9250 
9251 		func_params.f_obj = &bp->func_obj;
9252 		__set_bit(RAMROD_DRV_CLR_ONLY,
9253 					&func_params.ramrod_flags);
9254 
9255 		/* STARTED-->TX_ST0PPED */
9256 		func_params.cmd = BNX2X_F_CMD_TX_STOP;
9257 		bnx2x_func_state_change(bp, &func_params);
9258 
9259 		/* TX_ST0PPED-->STARTED */
9260 		func_params.cmd = BNX2X_F_CMD_TX_START;
9261 		return bnx2x_func_state_change(bp, &func_params);
9262 #endif
9263 	}
9264 
9265 	return 0;
9266 }
9267 
9268 static void bnx2x_disable_ptp(struct bnx2x *bp)
9269 {
9270 	int port = BP_PORT(bp);
9271 
9272 	/* Disable sending PTP packets to host */
9273 	REG_WR(bp, port ? NIG_REG_P1_LLH_PTP_TO_HOST :
9274 	       NIG_REG_P0_LLH_PTP_TO_HOST, 0x0);
9275 
9276 	/* Reset PTP event detection rules */
9277 	REG_WR(bp, port ? NIG_REG_P1_LLH_PTP_PARAM_MASK :
9278 	       NIG_REG_P0_LLH_PTP_PARAM_MASK, 0x7FF);
9279 	REG_WR(bp, port ? NIG_REG_P1_LLH_PTP_RULE_MASK :
9280 	       NIG_REG_P0_LLH_PTP_RULE_MASK, 0x3FFF);
9281 	REG_WR(bp, port ? NIG_REG_P1_TLLH_PTP_PARAM_MASK :
9282 	       NIG_REG_P0_TLLH_PTP_PARAM_MASK, 0x7FF);
9283 	REG_WR(bp, port ? NIG_REG_P1_TLLH_PTP_RULE_MASK :
9284 	       NIG_REG_P0_TLLH_PTP_RULE_MASK, 0x3FFF);
9285 
9286 	/* Disable the PTP feature */
9287 	REG_WR(bp, port ? NIG_REG_P1_PTP_EN :
9288 	       NIG_REG_P0_PTP_EN, 0x0);
9289 }
9290 
9291 /* Called during unload, to stop PTP-related stuff */
9292 static void bnx2x_stop_ptp(struct bnx2x *bp)
9293 {
9294 	/* Cancel PTP work queue. Should be done after the Tx queues are
9295 	 * drained to prevent additional scheduling.
9296 	 */
9297 	cancel_work_sync(&bp->ptp_task);
9298 
9299 	if (bp->ptp_tx_skb) {
9300 		dev_kfree_skb_any(bp->ptp_tx_skb);
9301 		bp->ptp_tx_skb = NULL;
9302 	}
9303 
9304 	/* Disable PTP in HW */
9305 	bnx2x_disable_ptp(bp);
9306 
9307 	DP(BNX2X_MSG_PTP, "PTP stop ended successfully\n");
9308 }
9309 
9310 void bnx2x_chip_cleanup(struct bnx2x *bp, int unload_mode, bool keep_link)
9311 {
9312 	int port = BP_PORT(bp);
9313 	int i, rc = 0;
9314 	u8 cos;
9315 	struct bnx2x_mcast_ramrod_params rparam = {NULL};
9316 	u32 reset_code;
9317 
9318 	/* Wait until tx fastpath tasks complete */
9319 	for_each_tx_queue(bp, i) {
9320 		struct bnx2x_fastpath *fp = &bp->fp[i];
9321 
9322 		for_each_cos_in_tx_queue(fp, cos)
9323 			rc = bnx2x_clean_tx_queue(bp, fp->txdata_ptr[cos]);
9324 #ifdef BNX2X_STOP_ON_ERROR
9325 		if (rc)
9326 			return;
9327 #endif
9328 	}
9329 
9330 	/* Give HW time to discard old tx messages */
9331 	usleep_range(1000, 2000);
9332 
9333 	/* Clean all ETH MACs */
9334 	rc = bnx2x_del_all_macs(bp, &bp->sp_objs[0].mac_obj, BNX2X_ETH_MAC,
9335 				false);
9336 	if (rc < 0)
9337 		BNX2X_ERR("Failed to delete all ETH macs: %d\n", rc);
9338 
9339 	/* Clean up UC list  */
9340 	rc = bnx2x_del_all_macs(bp, &bp->sp_objs[0].mac_obj, BNX2X_UC_LIST_MAC,
9341 				true);
9342 	if (rc < 0)
9343 		BNX2X_ERR("Failed to schedule DEL commands for UC MACs list: %d\n",
9344 			  rc);
9345 
9346 	/* The whole *vlan_obj structure may be not initialized if VLAN
9347 	 * filtering offload is not supported by hardware. Currently this is
9348 	 * true for all hardware covered by CHIP_IS_E1x().
9349 	 */
9350 	if (!CHIP_IS_E1x(bp)) {
9351 		/* Remove all currently configured VLANs */
9352 		rc = bnx2x_del_all_vlans(bp);
9353 		if (rc < 0)
9354 			BNX2X_ERR("Failed to delete all VLANs\n");
9355 	}
9356 
9357 	/* Disable LLH */
9358 	if (!CHIP_IS_E1(bp))
9359 		REG_WR(bp, NIG_REG_LLH0_FUNC_EN + port*8, 0);
9360 
9361 	/* Set "drop all" (stop Rx).
9362 	 * We need to take a netif_addr_lock() here in order to prevent
9363 	 * a race between the completion code and this code.
9364 	 */
9365 	netif_addr_lock_bh(bp->dev);
9366 	/* Schedule the rx_mode command */
9367 	if (test_bit(BNX2X_FILTER_RX_MODE_PENDING, &bp->sp_state))
9368 		set_bit(BNX2X_FILTER_RX_MODE_SCHED, &bp->sp_state);
9369 	else if (bp->slowpath)
9370 		bnx2x_set_storm_rx_mode(bp);
9371 
9372 	/* Cleanup multicast configuration */
9373 	rparam.mcast_obj = &bp->mcast_obj;
9374 	rc = bnx2x_config_mcast(bp, &rparam, BNX2X_MCAST_CMD_DEL);
9375 	if (rc < 0)
9376 		BNX2X_ERR("Failed to send DEL multicast command: %d\n", rc);
9377 
9378 	netif_addr_unlock_bh(bp->dev);
9379 
9380 	bnx2x_iov_chip_cleanup(bp);
9381 
9382 	/*
9383 	 * Send the UNLOAD_REQUEST to the MCP. This will return if
9384 	 * this function should perform FUNC, PORT or COMMON HW
9385 	 * reset.
9386 	 */
9387 	reset_code = bnx2x_send_unload_req(bp, unload_mode);
9388 
9389 	/*
9390 	 * (assumption: No Attention from MCP at this stage)
9391 	 * PMF probably in the middle of TX disable/enable transaction
9392 	 */
9393 	rc = bnx2x_func_wait_started(bp);
9394 	if (rc) {
9395 		BNX2X_ERR("bnx2x_func_wait_started failed\n");
9396 #ifdef BNX2X_STOP_ON_ERROR
9397 		return;
9398 #endif
9399 	}
9400 
9401 	/* Close multi and leading connections
9402 	 * Completions for ramrods are collected in a synchronous way
9403 	 */
9404 	for_each_eth_queue(bp, i)
9405 		if (bnx2x_stop_queue(bp, i))
9406 #ifdef BNX2X_STOP_ON_ERROR
9407 			return;
9408 #else
9409 			goto unload_error;
9410 #endif
9411 
9412 	if (CNIC_LOADED(bp)) {
9413 		for_each_cnic_queue(bp, i)
9414 			if (bnx2x_stop_queue(bp, i))
9415 #ifdef BNX2X_STOP_ON_ERROR
9416 				return;
9417 #else
9418 				goto unload_error;
9419 #endif
9420 	}
9421 
9422 	/* If SP settings didn't get completed so far - something
9423 	 * very wrong has happen.
9424 	 */
9425 	if (!bnx2x_wait_sp_comp(bp, ~0x0UL))
9426 		BNX2X_ERR("Hmmm... Common slow path ramrods got stuck!\n");
9427 
9428 #ifndef BNX2X_STOP_ON_ERROR
9429 unload_error:
9430 #endif
9431 	rc = bnx2x_func_stop(bp);
9432 	if (rc) {
9433 		BNX2X_ERR("Function stop failed!\n");
9434 #ifdef BNX2X_STOP_ON_ERROR
9435 		return;
9436 #endif
9437 	}
9438 
9439 	/* stop_ptp should be after the Tx queues are drained to prevent
9440 	 * scheduling to the cancelled PTP work queue. It should also be after
9441 	 * function stop ramrod is sent, since as part of this ramrod FW access
9442 	 * PTP registers.
9443 	 */
9444 	if (bp->flags & PTP_SUPPORTED) {
9445 		bnx2x_stop_ptp(bp);
9446 		if (bp->ptp_clock) {
9447 			ptp_clock_unregister(bp->ptp_clock);
9448 			bp->ptp_clock = NULL;
9449 		}
9450 	}
9451 
9452 	/* Disable HW interrupts, NAPI */
9453 	bnx2x_netif_stop(bp, 1);
9454 	/* Delete all NAPI objects */
9455 	bnx2x_del_all_napi(bp);
9456 	if (CNIC_LOADED(bp))
9457 		bnx2x_del_all_napi_cnic(bp);
9458 
9459 	/* Release IRQs */
9460 	bnx2x_free_irq(bp);
9461 
9462 	/* Reset the chip, unless PCI function is offline. If we reach this
9463 	 * point following a PCI error handling, it means device is really
9464 	 * in a bad state and we're about to remove it, so reset the chip
9465 	 * is not a good idea.
9466 	 */
9467 	if (!pci_channel_offline(bp->pdev)) {
9468 		rc = bnx2x_reset_hw(bp, reset_code);
9469 		if (rc)
9470 			BNX2X_ERR("HW_RESET failed\n");
9471 	}
9472 
9473 	/* Report UNLOAD_DONE to MCP */
9474 	bnx2x_send_unload_done(bp, keep_link);
9475 }
9476 
9477 void bnx2x_disable_close_the_gate(struct bnx2x *bp)
9478 {
9479 	u32 val;
9480 
9481 	DP(NETIF_MSG_IFDOWN, "Disabling \"close the gates\"\n");
9482 
9483 	if (CHIP_IS_E1(bp)) {
9484 		int port = BP_PORT(bp);
9485 		u32 addr = port ? MISC_REG_AEU_MASK_ATTN_FUNC_1 :
9486 			MISC_REG_AEU_MASK_ATTN_FUNC_0;
9487 
9488 		val = REG_RD(bp, addr);
9489 		val &= ~(0x300);
9490 		REG_WR(bp, addr, val);
9491 	} else {
9492 		val = REG_RD(bp, MISC_REG_AEU_GENERAL_MASK);
9493 		val &= ~(MISC_AEU_GENERAL_MASK_REG_AEU_PXP_CLOSE_MASK |
9494 			 MISC_AEU_GENERAL_MASK_REG_AEU_NIG_CLOSE_MASK);
9495 		REG_WR(bp, MISC_REG_AEU_GENERAL_MASK, val);
9496 	}
9497 }
9498 
9499 /* Close gates #2, #3 and #4: */
9500 static void bnx2x_set_234_gates(struct bnx2x *bp, bool close)
9501 {
9502 	u32 val;
9503 
9504 	/* Gates #2 and #4a are closed/opened for "not E1" only */
9505 	if (!CHIP_IS_E1(bp)) {
9506 		/* #4 */
9507 		REG_WR(bp, PXP_REG_HST_DISCARD_DOORBELLS, !!close);
9508 		/* #2 */
9509 		REG_WR(bp, PXP_REG_HST_DISCARD_INTERNAL_WRITES, !!close);
9510 	}
9511 
9512 	/* #3 */
9513 	if (CHIP_IS_E1x(bp)) {
9514 		/* Prevent interrupts from HC on both ports */
9515 		val = REG_RD(bp, HC_REG_CONFIG_1);
9516 		REG_WR(bp, HC_REG_CONFIG_1,
9517 		       (!close) ? (val | HC_CONFIG_1_REG_BLOCK_DISABLE_1) :
9518 		       (val & ~(u32)HC_CONFIG_1_REG_BLOCK_DISABLE_1));
9519 
9520 		val = REG_RD(bp, HC_REG_CONFIG_0);
9521 		REG_WR(bp, HC_REG_CONFIG_0,
9522 		       (!close) ? (val | HC_CONFIG_0_REG_BLOCK_DISABLE_0) :
9523 		       (val & ~(u32)HC_CONFIG_0_REG_BLOCK_DISABLE_0));
9524 	} else {
9525 		/* Prevent incoming interrupts in IGU */
9526 		val = REG_RD(bp, IGU_REG_BLOCK_CONFIGURATION);
9527 
9528 		REG_WR(bp, IGU_REG_BLOCK_CONFIGURATION,
9529 		       (!close) ?
9530 		       (val | IGU_BLOCK_CONFIGURATION_REG_BLOCK_ENABLE) :
9531 		       (val & ~(u32)IGU_BLOCK_CONFIGURATION_REG_BLOCK_ENABLE));
9532 	}
9533 
9534 	DP(NETIF_MSG_HW | NETIF_MSG_IFUP, "%s gates #2, #3 and #4\n",
9535 		close ? "closing" : "opening");
9536 }
9537 
9538 #define SHARED_MF_CLP_MAGIC  0x80000000 /* `magic' bit */
9539 
9540 static void bnx2x_clp_reset_prep(struct bnx2x *bp, u32 *magic_val)
9541 {
9542 	/* Do some magic... */
9543 	u32 val = MF_CFG_RD(bp, shared_mf_config.clp_mb);
9544 	*magic_val = val & SHARED_MF_CLP_MAGIC;
9545 	MF_CFG_WR(bp, shared_mf_config.clp_mb, val | SHARED_MF_CLP_MAGIC);
9546 }
9547 
9548 /**
9549  * bnx2x_clp_reset_done - restore the value of the `magic' bit.
9550  *
9551  * @bp:		driver handle
9552  * @magic_val:	old value of the `magic' bit.
9553  */
9554 static void bnx2x_clp_reset_done(struct bnx2x *bp, u32 magic_val)
9555 {
9556 	/* Restore the `magic' bit value... */
9557 	u32 val = MF_CFG_RD(bp, shared_mf_config.clp_mb);
9558 	MF_CFG_WR(bp, shared_mf_config.clp_mb,
9559 		(val & (~SHARED_MF_CLP_MAGIC)) | magic_val);
9560 }
9561 
9562 /**
9563  * bnx2x_reset_mcp_prep - prepare for MCP reset.
9564  *
9565  * @bp:		driver handle
9566  * @magic_val:	old value of 'magic' bit.
9567  *
9568  * Takes care of CLP configurations.
9569  */
9570 static void bnx2x_reset_mcp_prep(struct bnx2x *bp, u32 *magic_val)
9571 {
9572 	u32 shmem;
9573 	u32 validity_offset;
9574 
9575 	DP(NETIF_MSG_HW | NETIF_MSG_IFUP, "Starting\n");
9576 
9577 	/* Set `magic' bit in order to save MF config */
9578 	if (!CHIP_IS_E1(bp))
9579 		bnx2x_clp_reset_prep(bp, magic_val);
9580 
9581 	/* Get shmem offset */
9582 	shmem = REG_RD(bp, MISC_REG_SHARED_MEM_ADDR);
9583 	validity_offset =
9584 		offsetof(struct shmem_region, validity_map[BP_PORT(bp)]);
9585 
9586 	/* Clear validity map flags */
9587 	if (shmem > 0)
9588 		REG_WR(bp, shmem + validity_offset, 0);
9589 }
9590 
9591 #define MCP_TIMEOUT      5000   /* 5 seconds (in ms) */
9592 #define MCP_ONE_TIMEOUT  100    /* 100 ms */
9593 
9594 /**
9595  * bnx2x_mcp_wait_one - wait for MCP_ONE_TIMEOUT
9596  *
9597  * @bp:	driver handle
9598  */
9599 static void bnx2x_mcp_wait_one(struct bnx2x *bp)
9600 {
9601 	/* special handling for emulation and FPGA,
9602 	   wait 10 times longer */
9603 	if (CHIP_REV_IS_SLOW(bp))
9604 		msleep(MCP_ONE_TIMEOUT*10);
9605 	else
9606 		msleep(MCP_ONE_TIMEOUT);
9607 }
9608 
9609 /*
9610  * initializes bp->common.shmem_base and waits for validity signature to appear
9611  */
9612 static int bnx2x_init_shmem(struct bnx2x *bp)
9613 {
9614 	int cnt = 0;
9615 	u32 val = 0;
9616 
9617 	do {
9618 		bp->common.shmem_base = REG_RD(bp, MISC_REG_SHARED_MEM_ADDR);
9619 
9620 		/* If we read all 0xFFs, means we are in PCI error state and
9621 		 * should bail out to avoid crashes on adapter's FW reads.
9622 		 */
9623 		if (bp->common.shmem_base == 0xFFFFFFFF) {
9624 			bp->flags |= NO_MCP_FLAG;
9625 			return -ENODEV;
9626 		}
9627 
9628 		if (bp->common.shmem_base) {
9629 			val = SHMEM_RD(bp, validity_map[BP_PORT(bp)]);
9630 			if (val & SHR_MEM_VALIDITY_MB)
9631 				return 0;
9632 		}
9633 
9634 		bnx2x_mcp_wait_one(bp);
9635 
9636 	} while (cnt++ < (MCP_TIMEOUT / MCP_ONE_TIMEOUT));
9637 
9638 	BNX2X_ERR("BAD MCP validity signature\n");
9639 
9640 	return -ENODEV;
9641 }
9642 
9643 static int bnx2x_reset_mcp_comp(struct bnx2x *bp, u32 magic_val)
9644 {
9645 	int rc = bnx2x_init_shmem(bp);
9646 
9647 	/* Restore the `magic' bit value */
9648 	if (!CHIP_IS_E1(bp))
9649 		bnx2x_clp_reset_done(bp, magic_val);
9650 
9651 	return rc;
9652 }
9653 
9654 static void bnx2x_pxp_prep(struct bnx2x *bp)
9655 {
9656 	if (!CHIP_IS_E1(bp)) {
9657 		REG_WR(bp, PXP2_REG_RD_START_INIT, 0);
9658 		REG_WR(bp, PXP2_REG_RQ_RBC_DONE, 0);
9659 	}
9660 }
9661 
9662 /*
9663  * Reset the whole chip except for:
9664  *      - PCIE core
9665  *      - PCI Glue, PSWHST, PXP/PXP2 RF (all controlled by
9666  *              one reset bit)
9667  *      - IGU
9668  *      - MISC (including AEU)
9669  *      - GRC
9670  *      - RBCN, RBCP
9671  */
9672 static void bnx2x_process_kill_chip_reset(struct bnx2x *bp, bool global)
9673 {
9674 	u32 not_reset_mask1, reset_mask1, not_reset_mask2, reset_mask2;
9675 	u32 global_bits2, stay_reset2;
9676 
9677 	/*
9678 	 * Bits that have to be set in reset_mask2 if we want to reset 'global'
9679 	 * (per chip) blocks.
9680 	 */
9681 	global_bits2 =
9682 		MISC_REGISTERS_RESET_REG_2_RST_MCP_N_RESET_CMN_CPU |
9683 		MISC_REGISTERS_RESET_REG_2_RST_MCP_N_RESET_CMN_CORE;
9684 
9685 	/* Don't reset the following blocks.
9686 	 * Important: per port blocks (such as EMAC, BMAC, UMAC) can't be
9687 	 *            reset, as in 4 port device they might still be owned
9688 	 *            by the MCP (there is only one leader per path).
9689 	 */
9690 	not_reset_mask1 =
9691 		MISC_REGISTERS_RESET_REG_1_RST_HC |
9692 		MISC_REGISTERS_RESET_REG_1_RST_PXPV |
9693 		MISC_REGISTERS_RESET_REG_1_RST_PXP;
9694 
9695 	not_reset_mask2 =
9696 		MISC_REGISTERS_RESET_REG_2_RST_PCI_MDIO |
9697 		MISC_REGISTERS_RESET_REG_2_RST_EMAC0_HARD_CORE |
9698 		MISC_REGISTERS_RESET_REG_2_RST_EMAC1_HARD_CORE |
9699 		MISC_REGISTERS_RESET_REG_2_RST_MISC_CORE |
9700 		MISC_REGISTERS_RESET_REG_2_RST_RBCN |
9701 		MISC_REGISTERS_RESET_REG_2_RST_GRC  |
9702 		MISC_REGISTERS_RESET_REG_2_RST_MCP_N_RESET_REG_HARD_CORE |
9703 		MISC_REGISTERS_RESET_REG_2_RST_MCP_N_HARD_CORE_RST_B |
9704 		MISC_REGISTERS_RESET_REG_2_RST_ATC |
9705 		MISC_REGISTERS_RESET_REG_2_PGLC |
9706 		MISC_REGISTERS_RESET_REG_2_RST_BMAC0 |
9707 		MISC_REGISTERS_RESET_REG_2_RST_BMAC1 |
9708 		MISC_REGISTERS_RESET_REG_2_RST_EMAC0 |
9709 		MISC_REGISTERS_RESET_REG_2_RST_EMAC1 |
9710 		MISC_REGISTERS_RESET_REG_2_UMAC0 |
9711 		MISC_REGISTERS_RESET_REG_2_UMAC1;
9712 
9713 	/*
9714 	 * Keep the following blocks in reset:
9715 	 *  - all xxMACs are handled by the bnx2x_link code.
9716 	 */
9717 	stay_reset2 =
9718 		MISC_REGISTERS_RESET_REG_2_XMAC |
9719 		MISC_REGISTERS_RESET_REG_2_XMAC_SOFT;
9720 
9721 	/* Full reset masks according to the chip */
9722 	reset_mask1 = 0xffffffff;
9723 
9724 	if (CHIP_IS_E1(bp))
9725 		reset_mask2 = 0xffff;
9726 	else if (CHIP_IS_E1H(bp))
9727 		reset_mask2 = 0x1ffff;
9728 	else if (CHIP_IS_E2(bp))
9729 		reset_mask2 = 0xfffff;
9730 	else /* CHIP_IS_E3 */
9731 		reset_mask2 = 0x3ffffff;
9732 
9733 	/* Don't reset global blocks unless we need to */
9734 	if (!global)
9735 		reset_mask2 &= ~global_bits2;
9736 
9737 	/*
9738 	 * In case of attention in the QM, we need to reset PXP
9739 	 * (MISC_REGISTERS_RESET_REG_2_RST_PXP_RQ_RD_WR) before QM
9740 	 * because otherwise QM reset would release 'close the gates' shortly
9741 	 * before resetting the PXP, then the PSWRQ would send a write
9742 	 * request to PGLUE. Then when PXP is reset, PGLUE would try to
9743 	 * read the payload data from PSWWR, but PSWWR would not
9744 	 * respond. The write queue in PGLUE would stuck, dmae commands
9745 	 * would not return. Therefore it's important to reset the second
9746 	 * reset register (containing the
9747 	 * MISC_REGISTERS_RESET_REG_2_RST_PXP_RQ_RD_WR bit) before the
9748 	 * first one (containing the MISC_REGISTERS_RESET_REG_1_RST_QM
9749 	 * bit).
9750 	 */
9751 	REG_WR(bp, GRCBASE_MISC + MISC_REGISTERS_RESET_REG_2_CLEAR,
9752 	       reset_mask2 & (~not_reset_mask2));
9753 
9754 	REG_WR(bp, GRCBASE_MISC + MISC_REGISTERS_RESET_REG_1_CLEAR,
9755 	       reset_mask1 & (~not_reset_mask1));
9756 
9757 	barrier();
9758 
9759 	REG_WR(bp, GRCBASE_MISC + MISC_REGISTERS_RESET_REG_2_SET,
9760 	       reset_mask2 & (~stay_reset2));
9761 
9762 	barrier();
9763 
9764 	REG_WR(bp, GRCBASE_MISC + MISC_REGISTERS_RESET_REG_1_SET, reset_mask1);
9765 }
9766 
9767 /**
9768  * bnx2x_er_poll_igu_vq - poll for pending writes bit.
9769  * It should get cleared in no more than 1s.
9770  *
9771  * @bp:	driver handle
9772  *
9773  * It should get cleared in no more than 1s. Returns 0 if
9774  * pending writes bit gets cleared.
9775  */
9776 static int bnx2x_er_poll_igu_vq(struct bnx2x *bp)
9777 {
9778 	u32 cnt = 1000;
9779 	u32 pend_bits = 0;
9780 
9781 	do {
9782 		pend_bits  = REG_RD(bp, IGU_REG_PENDING_BITS_STATUS);
9783 
9784 		if (pend_bits == 0)
9785 			break;
9786 
9787 		usleep_range(1000, 2000);
9788 	} while (cnt-- > 0);
9789 
9790 	if (cnt <= 0) {
9791 		BNX2X_ERR("Still pending IGU requests pend_bits=%x!\n",
9792 			  pend_bits);
9793 		return -EBUSY;
9794 	}
9795 
9796 	return 0;
9797 }
9798 
9799 static int bnx2x_process_kill(struct bnx2x *bp, bool global)
9800 {
9801 	int cnt = 1000;
9802 	u32 val = 0;
9803 	u32 sr_cnt, blk_cnt, port_is_idle_0, port_is_idle_1, pgl_exp_rom2;
9804 	u32 tags_63_32 = 0;
9805 
9806 	/* Empty the Tetris buffer, wait for 1s */
9807 	do {
9808 		sr_cnt  = REG_RD(bp, PXP2_REG_RD_SR_CNT);
9809 		blk_cnt = REG_RD(bp, PXP2_REG_RD_BLK_CNT);
9810 		port_is_idle_0 = REG_RD(bp, PXP2_REG_RD_PORT_IS_IDLE_0);
9811 		port_is_idle_1 = REG_RD(bp, PXP2_REG_RD_PORT_IS_IDLE_1);
9812 		pgl_exp_rom2 = REG_RD(bp, PXP2_REG_PGL_EXP_ROM2);
9813 		if (CHIP_IS_E3(bp))
9814 			tags_63_32 = REG_RD(bp, PGLUE_B_REG_TAGS_63_32);
9815 
9816 		if ((sr_cnt == 0x7e) && (blk_cnt == 0xa0) &&
9817 		    ((port_is_idle_0 & 0x1) == 0x1) &&
9818 		    ((port_is_idle_1 & 0x1) == 0x1) &&
9819 		    (pgl_exp_rom2 == 0xffffffff) &&
9820 		    (!CHIP_IS_E3(bp) || (tags_63_32 == 0xffffffff)))
9821 			break;
9822 		usleep_range(1000, 2000);
9823 	} while (cnt-- > 0);
9824 
9825 	if (cnt <= 0) {
9826 		BNX2X_ERR("Tetris buffer didn't get empty or there are still outstanding read requests after 1s!\n");
9827 		BNX2X_ERR("sr_cnt=0x%08x, blk_cnt=0x%08x, port_is_idle_0=0x%08x, port_is_idle_1=0x%08x, pgl_exp_rom2=0x%08x\n",
9828 			  sr_cnt, blk_cnt, port_is_idle_0, port_is_idle_1,
9829 			  pgl_exp_rom2);
9830 		return -EAGAIN;
9831 	}
9832 
9833 	barrier();
9834 
9835 	/* Close gates #2, #3 and #4 */
9836 	bnx2x_set_234_gates(bp, true);
9837 
9838 	/* Poll for IGU VQs for 57712 and newer chips */
9839 	if (!CHIP_IS_E1x(bp) && bnx2x_er_poll_igu_vq(bp))
9840 		return -EAGAIN;
9841 
9842 	/* TBD: Indicate that "process kill" is in progress to MCP */
9843 
9844 	/* Clear "unprepared" bit */
9845 	REG_WR(bp, MISC_REG_UNPREPARED, 0);
9846 	barrier();
9847 
9848 	/* Wait for 1ms to empty GLUE and PCI-E core queues,
9849 	 * PSWHST, GRC and PSWRD Tetris buffer.
9850 	 */
9851 	usleep_range(1000, 2000);
9852 
9853 	/* Prepare to chip reset: */
9854 	/* MCP */
9855 	if (global)
9856 		bnx2x_reset_mcp_prep(bp, &val);
9857 
9858 	/* PXP */
9859 	bnx2x_pxp_prep(bp);
9860 	barrier();
9861 
9862 	/* reset the chip */
9863 	bnx2x_process_kill_chip_reset(bp, global);
9864 	barrier();
9865 
9866 	/* clear errors in PGB */
9867 	if (!CHIP_IS_E1x(bp))
9868 		REG_WR(bp, PGLUE_B_REG_LATCHED_ERRORS_CLR, 0x7f);
9869 
9870 	/* Recover after reset: */
9871 	/* MCP */
9872 	if (global && bnx2x_reset_mcp_comp(bp, val))
9873 		return -EAGAIN;
9874 
9875 	/* TBD: Add resetting the NO_MCP mode DB here */
9876 
9877 	/* Open the gates #2, #3 and #4 */
9878 	bnx2x_set_234_gates(bp, false);
9879 
9880 	/* TBD: IGU/AEU preparation bring back the AEU/IGU to a
9881 	 * reset state, re-enable attentions. */
9882 
9883 	return 0;
9884 }
9885 
9886 static int bnx2x_leader_reset(struct bnx2x *bp)
9887 {
9888 	int rc = 0;
9889 	bool global = bnx2x_reset_is_global(bp);
9890 	u32 load_code;
9891 
9892 	/* if not going to reset MCP - load "fake" driver to reset HW while
9893 	 * driver is owner of the HW
9894 	 */
9895 	if (!global && !BP_NOMCP(bp)) {
9896 		load_code = bnx2x_fw_command(bp, DRV_MSG_CODE_LOAD_REQ,
9897 					     DRV_MSG_CODE_LOAD_REQ_WITH_LFA);
9898 		if (!load_code) {
9899 			BNX2X_ERR("MCP response failure, aborting\n");
9900 			rc = -EAGAIN;
9901 			goto exit_leader_reset;
9902 		}
9903 		if ((load_code != FW_MSG_CODE_DRV_LOAD_COMMON_CHIP) &&
9904 		    (load_code != FW_MSG_CODE_DRV_LOAD_COMMON)) {
9905 			BNX2X_ERR("MCP unexpected resp, aborting\n");
9906 			rc = -EAGAIN;
9907 			goto exit_leader_reset2;
9908 		}
9909 		load_code = bnx2x_fw_command(bp, DRV_MSG_CODE_LOAD_DONE, 0);
9910 		if (!load_code) {
9911 			BNX2X_ERR("MCP response failure, aborting\n");
9912 			rc = -EAGAIN;
9913 			goto exit_leader_reset2;
9914 		}
9915 	}
9916 
9917 	/* Try to recover after the failure */
9918 	if (bnx2x_process_kill(bp, global)) {
9919 		BNX2X_ERR("Something bad had happen on engine %d! Aii!\n",
9920 			  BP_PATH(bp));
9921 		rc = -EAGAIN;
9922 		goto exit_leader_reset2;
9923 	}
9924 
9925 	/*
9926 	 * Clear RESET_IN_PROGRES and RESET_GLOBAL bits and update the driver
9927 	 * state.
9928 	 */
9929 	bnx2x_set_reset_done(bp);
9930 	if (global)
9931 		bnx2x_clear_reset_global(bp);
9932 
9933 exit_leader_reset2:
9934 	/* unload "fake driver" if it was loaded */
9935 	if (!global && !BP_NOMCP(bp)) {
9936 		bnx2x_fw_command(bp, DRV_MSG_CODE_UNLOAD_REQ_WOL_MCP, 0);
9937 		bnx2x_fw_command(bp, DRV_MSG_CODE_UNLOAD_DONE, 0);
9938 	}
9939 exit_leader_reset:
9940 	bp->is_leader = 0;
9941 	bnx2x_release_leader_lock(bp);
9942 	smp_mb();
9943 	return rc;
9944 }
9945 
9946 static void bnx2x_recovery_failed(struct bnx2x *bp)
9947 {
9948 	netdev_err(bp->dev, "Recovery has failed. Power cycle is needed.\n");
9949 
9950 	/* Disconnect this device */
9951 	netif_device_detach(bp->dev);
9952 
9953 	/*
9954 	 * Block ifup for all function on this engine until "process kill"
9955 	 * or power cycle.
9956 	 */
9957 	bnx2x_set_reset_in_progress(bp);
9958 
9959 	/* Shut down the power */
9960 	bnx2x_set_power_state(bp, PCI_D3hot);
9961 
9962 	bp->recovery_state = BNX2X_RECOVERY_FAILED;
9963 
9964 	smp_mb();
9965 }
9966 
9967 /*
9968  * Assumption: runs under rtnl lock. This together with the fact
9969  * that it's called only from bnx2x_sp_rtnl() ensure that it
9970  * will never be called when netif_running(bp->dev) is false.
9971  */
9972 static void bnx2x_parity_recover(struct bnx2x *bp)
9973 {
9974 	u32 error_recovered, error_unrecovered;
9975 	bool is_parity, global = false;
9976 #ifdef CONFIG_BNX2X_SRIOV
9977 	int vf_idx;
9978 
9979 	for (vf_idx = 0; vf_idx < bp->requested_nr_virtfn; vf_idx++) {
9980 		struct bnx2x_virtf *vf = BP_VF(bp, vf_idx);
9981 
9982 		if (vf)
9983 			vf->state = VF_LOST;
9984 	}
9985 #endif
9986 	DP(NETIF_MSG_HW, "Handling parity\n");
9987 	while (1) {
9988 		switch (bp->recovery_state) {
9989 		case BNX2X_RECOVERY_INIT:
9990 			DP(NETIF_MSG_HW, "State is BNX2X_RECOVERY_INIT\n");
9991 			is_parity = bnx2x_chk_parity_attn(bp, &global, false);
9992 			WARN_ON(!is_parity);
9993 
9994 			/* Try to get a LEADER_LOCK HW lock */
9995 			if (bnx2x_trylock_leader_lock(bp)) {
9996 				bnx2x_set_reset_in_progress(bp);
9997 				/*
9998 				 * Check if there is a global attention and if
9999 				 * there was a global attention, set the global
10000 				 * reset bit.
10001 				 */
10002 
10003 				if (global)
10004 					bnx2x_set_reset_global(bp);
10005 
10006 				bp->is_leader = 1;
10007 			}
10008 
10009 			/* Stop the driver */
10010 			/* If interface has been removed - break */
10011 			if (bnx2x_nic_unload(bp, UNLOAD_RECOVERY, false))
10012 				return;
10013 
10014 			bp->recovery_state = BNX2X_RECOVERY_WAIT;
10015 
10016 			/* Ensure "is_leader", MCP command sequence and
10017 			 * "recovery_state" update values are seen on other
10018 			 * CPUs.
10019 			 */
10020 			smp_mb();
10021 			break;
10022 
10023 		case BNX2X_RECOVERY_WAIT:
10024 			DP(NETIF_MSG_HW, "State is BNX2X_RECOVERY_WAIT\n");
10025 			if (bp->is_leader) {
10026 				int other_engine = BP_PATH(bp) ? 0 : 1;
10027 				bool other_load_status =
10028 					bnx2x_get_load_status(bp, other_engine);
10029 				bool load_status =
10030 					bnx2x_get_load_status(bp, BP_PATH(bp));
10031 				global = bnx2x_reset_is_global(bp);
10032 
10033 				/*
10034 				 * In case of a parity in a global block, let
10035 				 * the first leader that performs a
10036 				 * leader_reset() reset the global blocks in
10037 				 * order to clear global attentions. Otherwise
10038 				 * the gates will remain closed for that
10039 				 * engine.
10040 				 */
10041 				if (load_status ||
10042 				    (global && other_load_status)) {
10043 					/* Wait until all other functions get
10044 					 * down.
10045 					 */
10046 					schedule_delayed_work(&bp->sp_rtnl_task,
10047 								HZ/10);
10048 					return;
10049 				} else {
10050 					/* If all other functions got down -
10051 					 * try to bring the chip back to
10052 					 * normal. In any case it's an exit
10053 					 * point for a leader.
10054 					 */
10055 					if (bnx2x_leader_reset(bp)) {
10056 						bnx2x_recovery_failed(bp);
10057 						return;
10058 					}
10059 
10060 					/* If we are here, means that the
10061 					 * leader has succeeded and doesn't
10062 					 * want to be a leader any more. Try
10063 					 * to continue as a none-leader.
10064 					 */
10065 					break;
10066 				}
10067 			} else { /* non-leader */
10068 				if (!bnx2x_reset_is_done(bp, BP_PATH(bp))) {
10069 					/* Try to get a LEADER_LOCK HW lock as
10070 					 * long as a former leader may have
10071 					 * been unloaded by the user or
10072 					 * released a leadership by another
10073 					 * reason.
10074 					 */
10075 					if (bnx2x_trylock_leader_lock(bp)) {
10076 						/* I'm a leader now! Restart a
10077 						 * switch case.
10078 						 */
10079 						bp->is_leader = 1;
10080 						break;
10081 					}
10082 
10083 					schedule_delayed_work(&bp->sp_rtnl_task,
10084 								HZ/10);
10085 					return;
10086 
10087 				} else {
10088 					/*
10089 					 * If there was a global attention, wait
10090 					 * for it to be cleared.
10091 					 */
10092 					if (bnx2x_reset_is_global(bp)) {
10093 						schedule_delayed_work(
10094 							&bp->sp_rtnl_task,
10095 							HZ/10);
10096 						return;
10097 					}
10098 
10099 					error_recovered =
10100 					  bp->eth_stats.recoverable_error;
10101 					error_unrecovered =
10102 					  bp->eth_stats.unrecoverable_error;
10103 					bp->recovery_state =
10104 						BNX2X_RECOVERY_NIC_LOADING;
10105 					if (bnx2x_nic_load(bp, LOAD_NORMAL)) {
10106 						error_unrecovered++;
10107 						netdev_err(bp->dev,
10108 							   "Recovery failed. Power cycle needed\n");
10109 						/* Disconnect this device */
10110 						netif_device_detach(bp->dev);
10111 						/* Shut down the power */
10112 						bnx2x_set_power_state(
10113 							bp, PCI_D3hot);
10114 						smp_mb();
10115 					} else {
10116 						bp->recovery_state =
10117 							BNX2X_RECOVERY_DONE;
10118 						error_recovered++;
10119 						smp_mb();
10120 					}
10121 					bp->eth_stats.recoverable_error =
10122 						error_recovered;
10123 					bp->eth_stats.unrecoverable_error =
10124 						error_unrecovered;
10125 
10126 					return;
10127 				}
10128 			}
10129 		default:
10130 			return;
10131 		}
10132 	}
10133 }
10134 
10135 static int bnx2x_udp_port_update(struct bnx2x *bp)
10136 {
10137 	struct bnx2x_func_switch_update_params *switch_update_params;
10138 	struct bnx2x_func_state_params func_params = {NULL};
10139 	struct bnx2x_udp_tunnel *udp_tunnel;
10140 	u16 vxlan_port = 0, geneve_port = 0;
10141 	int rc;
10142 
10143 	switch_update_params = &func_params.params.switch_update;
10144 
10145 	/* Prepare parameters for function state transitions */
10146 	__set_bit(RAMROD_COMP_WAIT, &func_params.ramrod_flags);
10147 	__set_bit(RAMROD_RETRY, &func_params.ramrod_flags);
10148 
10149 	func_params.f_obj = &bp->func_obj;
10150 	func_params.cmd = BNX2X_F_CMD_SWITCH_UPDATE;
10151 
10152 	/* Function parameters */
10153 	__set_bit(BNX2X_F_UPDATE_TUNNEL_CFG_CHNG,
10154 		  &switch_update_params->changes);
10155 
10156 	if (bp->udp_tunnel_ports[BNX2X_UDP_PORT_GENEVE].count) {
10157 		udp_tunnel = &bp->udp_tunnel_ports[BNX2X_UDP_PORT_GENEVE];
10158 		geneve_port = udp_tunnel->dst_port;
10159 		switch_update_params->geneve_dst_port = geneve_port;
10160 	}
10161 
10162 	if (bp->udp_tunnel_ports[BNX2X_UDP_PORT_VXLAN].count) {
10163 		udp_tunnel = &bp->udp_tunnel_ports[BNX2X_UDP_PORT_VXLAN];
10164 		vxlan_port = udp_tunnel->dst_port;
10165 		switch_update_params->vxlan_dst_port = vxlan_port;
10166 	}
10167 
10168 	/* Re-enable inner-rss for the offloaded UDP tunnels */
10169 	__set_bit(BNX2X_F_UPDATE_TUNNEL_INNER_RSS,
10170 		  &switch_update_params->changes);
10171 
10172 	rc = bnx2x_func_state_change(bp, &func_params);
10173 	if (rc)
10174 		BNX2X_ERR("failed to set UDP dst port to %04x %04x (rc = 0x%x)\n",
10175 			  vxlan_port, geneve_port, rc);
10176 	else
10177 		DP(BNX2X_MSG_SP,
10178 		   "Configured UDP ports: Vxlan [%04x] Geneve [%04x]\n",
10179 		   vxlan_port, geneve_port);
10180 
10181 	return rc;
10182 }
10183 
10184 static void __bnx2x_add_udp_port(struct bnx2x *bp, u16 port,
10185 				 enum bnx2x_udp_port_type type)
10186 {
10187 	struct bnx2x_udp_tunnel *udp_port = &bp->udp_tunnel_ports[type];
10188 
10189 	if (!netif_running(bp->dev) || !IS_PF(bp) || CHIP_IS_E1x(bp))
10190 		return;
10191 
10192 	if (udp_port->count && udp_port->dst_port == port) {
10193 		udp_port->count++;
10194 		return;
10195 	}
10196 
10197 	if (udp_port->count) {
10198 		DP(BNX2X_MSG_SP,
10199 		   "UDP tunnel [%d] -  destination port limit reached\n",
10200 		   type);
10201 		return;
10202 	}
10203 
10204 	udp_port->dst_port = port;
10205 	udp_port->count = 1;
10206 	bnx2x_schedule_sp_rtnl(bp, BNX2X_SP_RTNL_CHANGE_UDP_PORT, 0);
10207 }
10208 
10209 static void __bnx2x_del_udp_port(struct bnx2x *bp, u16 port,
10210 				 enum bnx2x_udp_port_type type)
10211 {
10212 	struct bnx2x_udp_tunnel *udp_port = &bp->udp_tunnel_ports[type];
10213 
10214 	if (!IS_PF(bp) || CHIP_IS_E1x(bp))
10215 		return;
10216 
10217 	if (!udp_port->count || udp_port->dst_port != port) {
10218 		DP(BNX2X_MSG_SP, "Invalid UDP tunnel [%d] port\n",
10219 		   type);
10220 		return;
10221 	}
10222 
10223 	/* Remove reference, and make certain it's no longer in use */
10224 	udp_port->count--;
10225 	if (udp_port->count)
10226 		return;
10227 	udp_port->dst_port = 0;
10228 
10229 	if (netif_running(bp->dev))
10230 		bnx2x_schedule_sp_rtnl(bp, BNX2X_SP_RTNL_CHANGE_UDP_PORT, 0);
10231 	else
10232 		DP(BNX2X_MSG_SP, "Deleted UDP tunnel [%d] port %d\n",
10233 		   type, port);
10234 }
10235 
10236 static void bnx2x_udp_tunnel_add(struct net_device *netdev,
10237 				 struct udp_tunnel_info *ti)
10238 {
10239 	struct bnx2x *bp = netdev_priv(netdev);
10240 	u16 t_port = ntohs(ti->port);
10241 
10242 	switch (ti->type) {
10243 	case UDP_TUNNEL_TYPE_VXLAN:
10244 		__bnx2x_add_udp_port(bp, t_port, BNX2X_UDP_PORT_VXLAN);
10245 		break;
10246 	case UDP_TUNNEL_TYPE_GENEVE:
10247 		__bnx2x_add_udp_port(bp, t_port, BNX2X_UDP_PORT_GENEVE);
10248 		break;
10249 	default:
10250 		break;
10251 	}
10252 }
10253 
10254 static void bnx2x_udp_tunnel_del(struct net_device *netdev,
10255 				 struct udp_tunnel_info *ti)
10256 {
10257 	struct bnx2x *bp = netdev_priv(netdev);
10258 	u16 t_port = ntohs(ti->port);
10259 
10260 	switch (ti->type) {
10261 	case UDP_TUNNEL_TYPE_VXLAN:
10262 		__bnx2x_del_udp_port(bp, t_port, BNX2X_UDP_PORT_VXLAN);
10263 		break;
10264 	case UDP_TUNNEL_TYPE_GENEVE:
10265 		__bnx2x_del_udp_port(bp, t_port, BNX2X_UDP_PORT_GENEVE);
10266 		break;
10267 	default:
10268 		break;
10269 	}
10270 }
10271 
10272 static int bnx2x_close(struct net_device *dev);
10273 
10274 /* bnx2x_nic_unload() flushes the bnx2x_wq, thus reset task is
10275  * scheduled on a general queue in order to prevent a dead lock.
10276  */
10277 static void bnx2x_sp_rtnl_task(struct work_struct *work)
10278 {
10279 	struct bnx2x *bp = container_of(work, struct bnx2x, sp_rtnl_task.work);
10280 
10281 	rtnl_lock();
10282 
10283 	if (!netif_running(bp->dev)) {
10284 		rtnl_unlock();
10285 		return;
10286 	}
10287 
10288 	if (unlikely(bp->recovery_state != BNX2X_RECOVERY_DONE)) {
10289 #ifdef BNX2X_STOP_ON_ERROR
10290 		BNX2X_ERR("recovery flow called but STOP_ON_ERROR defined so reset not done to allow debug dump,\n"
10291 			  "you will need to reboot when done\n");
10292 		goto sp_rtnl_not_reset;
10293 #endif
10294 		/*
10295 		 * Clear all pending SP commands as we are going to reset the
10296 		 * function anyway.
10297 		 */
10298 		bp->sp_rtnl_state = 0;
10299 		smp_mb();
10300 
10301 		bnx2x_parity_recover(bp);
10302 
10303 		rtnl_unlock();
10304 		return;
10305 	}
10306 
10307 	if (test_and_clear_bit(BNX2X_SP_RTNL_TX_TIMEOUT, &bp->sp_rtnl_state)) {
10308 #ifdef BNX2X_STOP_ON_ERROR
10309 		BNX2X_ERR("recovery flow called but STOP_ON_ERROR defined so reset not done to allow debug dump,\n"
10310 			  "you will need to reboot when done\n");
10311 		goto sp_rtnl_not_reset;
10312 #endif
10313 
10314 		/*
10315 		 * Clear all pending SP commands as we are going to reset the
10316 		 * function anyway.
10317 		 */
10318 		bp->sp_rtnl_state = 0;
10319 		smp_mb();
10320 
10321 		/* Immediately indicate link as down */
10322 		bp->link_vars.link_up = 0;
10323 		bp->force_link_down = true;
10324 		netif_carrier_off(bp->dev);
10325 		BNX2X_ERR("Indicating link is down due to Tx-timeout\n");
10326 
10327 		bnx2x_nic_unload(bp, UNLOAD_NORMAL, true);
10328 		/* When ret value shows failure of allocation failure,
10329 		 * the nic is rebooted again. If open still fails, a error
10330 		 * message to notify the user.
10331 		 */
10332 		if (bnx2x_nic_load(bp, LOAD_NORMAL) == -ENOMEM) {
10333 			bnx2x_nic_unload(bp, UNLOAD_NORMAL, true);
10334 			if (bnx2x_nic_load(bp, LOAD_NORMAL))
10335 				BNX2X_ERR("Open the NIC fails again!\n");
10336 		}
10337 		rtnl_unlock();
10338 		return;
10339 	}
10340 #ifdef BNX2X_STOP_ON_ERROR
10341 sp_rtnl_not_reset:
10342 #endif
10343 	if (test_and_clear_bit(BNX2X_SP_RTNL_SETUP_TC, &bp->sp_rtnl_state))
10344 		bnx2x_setup_tc(bp->dev, bp->dcbx_port_params.ets.num_of_cos);
10345 	if (test_and_clear_bit(BNX2X_SP_RTNL_AFEX_F_UPDATE, &bp->sp_rtnl_state))
10346 		bnx2x_after_function_update(bp);
10347 	/*
10348 	 * in case of fan failure we need to reset id if the "stop on error"
10349 	 * debug flag is set, since we trying to prevent permanent overheating
10350 	 * damage
10351 	 */
10352 	if (test_and_clear_bit(BNX2X_SP_RTNL_FAN_FAILURE, &bp->sp_rtnl_state)) {
10353 		DP(NETIF_MSG_HW, "fan failure detected. Unloading driver\n");
10354 		netif_device_detach(bp->dev);
10355 		bnx2x_close(bp->dev);
10356 		rtnl_unlock();
10357 		return;
10358 	}
10359 
10360 	if (test_and_clear_bit(BNX2X_SP_RTNL_VFPF_MCAST, &bp->sp_rtnl_state)) {
10361 		DP(BNX2X_MSG_SP,
10362 		   "sending set mcast vf pf channel message from rtnl sp-task\n");
10363 		bnx2x_vfpf_set_mcast(bp->dev);
10364 	}
10365 	if (test_and_clear_bit(BNX2X_SP_RTNL_VFPF_CHANNEL_DOWN,
10366 			       &bp->sp_rtnl_state)){
10367 		if (netif_carrier_ok(bp->dev)) {
10368 			bnx2x_tx_disable(bp);
10369 			BNX2X_ERR("PF indicated channel is not servicable anymore. This means this VF device is no longer operational\n");
10370 		}
10371 	}
10372 
10373 	if (test_and_clear_bit(BNX2X_SP_RTNL_RX_MODE, &bp->sp_rtnl_state)) {
10374 		DP(BNX2X_MSG_SP, "Handling Rx Mode setting\n");
10375 		bnx2x_set_rx_mode_inner(bp);
10376 	}
10377 
10378 	if (test_and_clear_bit(BNX2X_SP_RTNL_HYPERVISOR_VLAN,
10379 			       &bp->sp_rtnl_state))
10380 		bnx2x_pf_set_vfs_vlan(bp);
10381 
10382 	if (test_and_clear_bit(BNX2X_SP_RTNL_TX_STOP, &bp->sp_rtnl_state)) {
10383 		bnx2x_dcbx_stop_hw_tx(bp);
10384 		bnx2x_dcbx_resume_hw_tx(bp);
10385 	}
10386 
10387 	if (test_and_clear_bit(BNX2X_SP_RTNL_GET_DRV_VERSION,
10388 			       &bp->sp_rtnl_state))
10389 		bnx2x_update_mng_version(bp);
10390 
10391 	if (test_and_clear_bit(BNX2X_SP_RTNL_UPDATE_SVID, &bp->sp_rtnl_state))
10392 		bnx2x_handle_update_svid_cmd(bp);
10393 
10394 	if (test_and_clear_bit(BNX2X_SP_RTNL_CHANGE_UDP_PORT,
10395 			       &bp->sp_rtnl_state)) {
10396 		if (bnx2x_udp_port_update(bp)) {
10397 			/* On error, forget configuration */
10398 			memset(bp->udp_tunnel_ports, 0,
10399 			       sizeof(struct bnx2x_udp_tunnel) *
10400 			       BNX2X_UDP_PORT_MAX);
10401 		} else {
10402 			/* Since we don't store additional port information,
10403 			 * if no ports are configured for any feature ask for
10404 			 * information about currently configured ports.
10405 			 */
10406 			if (!bp->udp_tunnel_ports[BNX2X_UDP_PORT_VXLAN].count &&
10407 			    !bp->udp_tunnel_ports[BNX2X_UDP_PORT_GENEVE].count)
10408 				udp_tunnel_get_rx_info(bp->dev);
10409 		}
10410 	}
10411 
10412 	/* work which needs rtnl lock not-taken (as it takes the lock itself and
10413 	 * can be called from other contexts as well)
10414 	 */
10415 	rtnl_unlock();
10416 
10417 	/* enable SR-IOV if applicable */
10418 	if (IS_SRIOV(bp) && test_and_clear_bit(BNX2X_SP_RTNL_ENABLE_SRIOV,
10419 					       &bp->sp_rtnl_state)) {
10420 		bnx2x_disable_sriov(bp);
10421 		bnx2x_enable_sriov(bp);
10422 	}
10423 }
10424 
10425 static void bnx2x_period_task(struct work_struct *work)
10426 {
10427 	struct bnx2x *bp = container_of(work, struct bnx2x, period_task.work);
10428 
10429 	if (!netif_running(bp->dev))
10430 		goto period_task_exit;
10431 
10432 	if (CHIP_REV_IS_SLOW(bp)) {
10433 		BNX2X_ERR("period task called on emulation, ignoring\n");
10434 		goto period_task_exit;
10435 	}
10436 
10437 	bnx2x_acquire_phy_lock(bp);
10438 	/*
10439 	 * The barrier is needed to ensure the ordering between the writing to
10440 	 * the bp->port.pmf in the bnx2x_nic_load() or bnx2x_pmf_update() and
10441 	 * the reading here.
10442 	 */
10443 	smp_mb();
10444 	if (bp->port.pmf) {
10445 		bnx2x_period_func(&bp->link_params, &bp->link_vars);
10446 
10447 		/* Re-queue task in 1 sec */
10448 		queue_delayed_work(bnx2x_wq, &bp->period_task, 1*HZ);
10449 	}
10450 
10451 	bnx2x_release_phy_lock(bp);
10452 period_task_exit:
10453 	return;
10454 }
10455 
10456 /*
10457  * Init service functions
10458  */
10459 
10460 static u32 bnx2x_get_pretend_reg(struct bnx2x *bp)
10461 {
10462 	u32 base = PXP2_REG_PGL_PRETEND_FUNC_F0;
10463 	u32 stride = PXP2_REG_PGL_PRETEND_FUNC_F1 - base;
10464 	return base + (BP_ABS_FUNC(bp)) * stride;
10465 }
10466 
10467 static bool bnx2x_prev_unload_close_umac(struct bnx2x *bp,
10468 					 u8 port, u32 reset_reg,
10469 					 struct bnx2x_mac_vals *vals)
10470 {
10471 	u32 mask = MISC_REGISTERS_RESET_REG_2_UMAC0 << port;
10472 	u32 base_addr;
10473 
10474 	if (!(mask & reset_reg))
10475 		return false;
10476 
10477 	BNX2X_DEV_INFO("Disable umac Rx %02x\n", port);
10478 	base_addr = port ? GRCBASE_UMAC1 : GRCBASE_UMAC0;
10479 	vals->umac_addr[port] = base_addr + UMAC_REG_COMMAND_CONFIG;
10480 	vals->umac_val[port] = REG_RD(bp, vals->umac_addr[port]);
10481 	REG_WR(bp, vals->umac_addr[port], 0);
10482 
10483 	return true;
10484 }
10485 
10486 static void bnx2x_prev_unload_close_mac(struct bnx2x *bp,
10487 					struct bnx2x_mac_vals *vals)
10488 {
10489 	u32 val, base_addr, offset, mask, reset_reg;
10490 	bool mac_stopped = false;
10491 	u8 port = BP_PORT(bp);
10492 
10493 	/* reset addresses as they also mark which values were changed */
10494 	memset(vals, 0, sizeof(*vals));
10495 
10496 	reset_reg = REG_RD(bp, MISC_REG_RESET_REG_2);
10497 
10498 	if (!CHIP_IS_E3(bp)) {
10499 		val = REG_RD(bp, NIG_REG_BMAC0_REGS_OUT_EN + port * 4);
10500 		mask = MISC_REGISTERS_RESET_REG_2_RST_BMAC0 << port;
10501 		if ((mask & reset_reg) && val) {
10502 			u32 wb_data[2];
10503 			BNX2X_DEV_INFO("Disable bmac Rx\n");
10504 			base_addr = BP_PORT(bp) ? NIG_REG_INGRESS_BMAC1_MEM
10505 						: NIG_REG_INGRESS_BMAC0_MEM;
10506 			offset = CHIP_IS_E2(bp) ? BIGMAC2_REGISTER_BMAC_CONTROL
10507 						: BIGMAC_REGISTER_BMAC_CONTROL;
10508 
10509 			/*
10510 			 * use rd/wr since we cannot use dmae. This is safe
10511 			 * since MCP won't access the bus due to the request
10512 			 * to unload, and no function on the path can be
10513 			 * loaded at this time.
10514 			 */
10515 			wb_data[0] = REG_RD(bp, base_addr + offset);
10516 			wb_data[1] = REG_RD(bp, base_addr + offset + 0x4);
10517 			vals->bmac_addr = base_addr + offset;
10518 			vals->bmac_val[0] = wb_data[0];
10519 			vals->bmac_val[1] = wb_data[1];
10520 			wb_data[0] &= ~BMAC_CONTROL_RX_ENABLE;
10521 			REG_WR(bp, vals->bmac_addr, wb_data[0]);
10522 			REG_WR(bp, vals->bmac_addr + 0x4, wb_data[1]);
10523 		}
10524 		BNX2X_DEV_INFO("Disable emac Rx\n");
10525 		vals->emac_addr = NIG_REG_NIG_EMAC0_EN + BP_PORT(bp)*4;
10526 		vals->emac_val = REG_RD(bp, vals->emac_addr);
10527 		REG_WR(bp, vals->emac_addr, 0);
10528 		mac_stopped = true;
10529 	} else {
10530 		if (reset_reg & MISC_REGISTERS_RESET_REG_2_XMAC) {
10531 			BNX2X_DEV_INFO("Disable xmac Rx\n");
10532 			base_addr = BP_PORT(bp) ? GRCBASE_XMAC1 : GRCBASE_XMAC0;
10533 			val = REG_RD(bp, base_addr + XMAC_REG_PFC_CTRL_HI);
10534 			REG_WR(bp, base_addr + XMAC_REG_PFC_CTRL_HI,
10535 			       val & ~(1 << 1));
10536 			REG_WR(bp, base_addr + XMAC_REG_PFC_CTRL_HI,
10537 			       val | (1 << 1));
10538 			vals->xmac_addr = base_addr + XMAC_REG_CTRL;
10539 			vals->xmac_val = REG_RD(bp, vals->xmac_addr);
10540 			REG_WR(bp, vals->xmac_addr, 0);
10541 			mac_stopped = true;
10542 		}
10543 
10544 		mac_stopped |= bnx2x_prev_unload_close_umac(bp, 0,
10545 							    reset_reg, vals);
10546 		mac_stopped |= bnx2x_prev_unload_close_umac(bp, 1,
10547 							    reset_reg, vals);
10548 	}
10549 
10550 	if (mac_stopped)
10551 		msleep(20);
10552 }
10553 
10554 #define BNX2X_PREV_UNDI_PROD_ADDR(p) (BAR_TSTRORM_INTMEM + 0x1508 + ((p) << 4))
10555 #define BNX2X_PREV_UNDI_PROD_ADDR_H(f) (BAR_TSTRORM_INTMEM + \
10556 					0x1848 + ((f) << 4))
10557 #define BNX2X_PREV_UNDI_RCQ(val)	((val) & 0xffff)
10558 #define BNX2X_PREV_UNDI_BD(val)		((val) >> 16 & 0xffff)
10559 #define BNX2X_PREV_UNDI_PROD(rcq, bd)	((bd) << 16 | (rcq))
10560 
10561 #define BCM_5710_UNDI_FW_MF_MAJOR	(0x07)
10562 #define BCM_5710_UNDI_FW_MF_MINOR	(0x08)
10563 #define BCM_5710_UNDI_FW_MF_VERS	(0x05)
10564 
10565 static bool bnx2x_prev_is_after_undi(struct bnx2x *bp)
10566 {
10567 	/* UNDI marks its presence in DORQ -
10568 	 * it initializes CID offset for normal bell to 0x7
10569 	 */
10570 	if (!(REG_RD(bp, MISC_REG_RESET_REG_1) &
10571 	    MISC_REGISTERS_RESET_REG_1_RST_DORQ))
10572 		return false;
10573 
10574 	if (REG_RD(bp, DORQ_REG_NORM_CID_OFST) == 0x7) {
10575 		BNX2X_DEV_INFO("UNDI previously loaded\n");
10576 		return true;
10577 	}
10578 
10579 	return false;
10580 }
10581 
10582 static void bnx2x_prev_unload_undi_inc(struct bnx2x *bp, u8 inc)
10583 {
10584 	u16 rcq, bd;
10585 	u32 addr, tmp_reg;
10586 
10587 	if (BP_FUNC(bp) < 2)
10588 		addr = BNX2X_PREV_UNDI_PROD_ADDR(BP_PORT(bp));
10589 	else
10590 		addr = BNX2X_PREV_UNDI_PROD_ADDR_H(BP_FUNC(bp) - 2);
10591 
10592 	tmp_reg = REG_RD(bp, addr);
10593 	rcq = BNX2X_PREV_UNDI_RCQ(tmp_reg) + inc;
10594 	bd = BNX2X_PREV_UNDI_BD(tmp_reg) + inc;
10595 
10596 	tmp_reg = BNX2X_PREV_UNDI_PROD(rcq, bd);
10597 	REG_WR(bp, addr, tmp_reg);
10598 
10599 	BNX2X_DEV_INFO("UNDI producer [%d/%d][%08x] rings bd -> 0x%04x, rcq -> 0x%04x\n",
10600 		       BP_PORT(bp), BP_FUNC(bp), addr, bd, rcq);
10601 }
10602 
10603 static int bnx2x_prev_mcp_done(struct bnx2x *bp)
10604 {
10605 	u32 rc = bnx2x_fw_command(bp, DRV_MSG_CODE_UNLOAD_DONE,
10606 				  DRV_MSG_CODE_UNLOAD_SKIP_LINK_RESET);
10607 	if (!rc) {
10608 		BNX2X_ERR("MCP response failure, aborting\n");
10609 		return -EBUSY;
10610 	}
10611 
10612 	return 0;
10613 }
10614 
10615 static struct bnx2x_prev_path_list *
10616 		bnx2x_prev_path_get_entry(struct bnx2x *bp)
10617 {
10618 	struct bnx2x_prev_path_list *tmp_list;
10619 
10620 	list_for_each_entry(tmp_list, &bnx2x_prev_list, list)
10621 		if (PCI_SLOT(bp->pdev->devfn) == tmp_list->slot &&
10622 		    bp->pdev->bus->number == tmp_list->bus &&
10623 		    BP_PATH(bp) == tmp_list->path)
10624 			return tmp_list;
10625 
10626 	return NULL;
10627 }
10628 
10629 static int bnx2x_prev_path_mark_eeh(struct bnx2x *bp)
10630 {
10631 	struct bnx2x_prev_path_list *tmp_list;
10632 	int rc;
10633 
10634 	rc = down_interruptible(&bnx2x_prev_sem);
10635 	if (rc) {
10636 		BNX2X_ERR("Received %d when tried to take lock\n", rc);
10637 		return rc;
10638 	}
10639 
10640 	tmp_list = bnx2x_prev_path_get_entry(bp);
10641 	if (tmp_list) {
10642 		tmp_list->aer = 1;
10643 		rc = 0;
10644 	} else {
10645 		BNX2X_ERR("path %d: Entry does not exist for eeh; Flow occurs before initial insmod is over ?\n",
10646 			  BP_PATH(bp));
10647 	}
10648 
10649 	up(&bnx2x_prev_sem);
10650 
10651 	return rc;
10652 }
10653 
10654 static bool bnx2x_prev_is_path_marked(struct bnx2x *bp)
10655 {
10656 	struct bnx2x_prev_path_list *tmp_list;
10657 	bool rc = false;
10658 
10659 	if (down_trylock(&bnx2x_prev_sem))
10660 		return false;
10661 
10662 	tmp_list = bnx2x_prev_path_get_entry(bp);
10663 	if (tmp_list) {
10664 		if (tmp_list->aer) {
10665 			DP(NETIF_MSG_HW, "Path %d was marked by AER\n",
10666 			   BP_PATH(bp));
10667 		} else {
10668 			rc = true;
10669 			BNX2X_DEV_INFO("Path %d was already cleaned from previous drivers\n",
10670 				       BP_PATH(bp));
10671 		}
10672 	}
10673 
10674 	up(&bnx2x_prev_sem);
10675 
10676 	return rc;
10677 }
10678 
10679 bool bnx2x_port_after_undi(struct bnx2x *bp)
10680 {
10681 	struct bnx2x_prev_path_list *entry;
10682 	bool val;
10683 
10684 	down(&bnx2x_prev_sem);
10685 
10686 	entry = bnx2x_prev_path_get_entry(bp);
10687 	val = !!(entry && (entry->undi & (1 << BP_PORT(bp))));
10688 
10689 	up(&bnx2x_prev_sem);
10690 
10691 	return val;
10692 }
10693 
10694 static int bnx2x_prev_mark_path(struct bnx2x *bp, bool after_undi)
10695 {
10696 	struct bnx2x_prev_path_list *tmp_list;
10697 	int rc;
10698 
10699 	rc = down_interruptible(&bnx2x_prev_sem);
10700 	if (rc) {
10701 		BNX2X_ERR("Received %d when tried to take lock\n", rc);
10702 		return rc;
10703 	}
10704 
10705 	/* Check whether the entry for this path already exists */
10706 	tmp_list = bnx2x_prev_path_get_entry(bp);
10707 	if (tmp_list) {
10708 		if (!tmp_list->aer) {
10709 			BNX2X_ERR("Re-Marking the path.\n");
10710 		} else {
10711 			DP(NETIF_MSG_HW, "Removing AER indication from path %d\n",
10712 			   BP_PATH(bp));
10713 			tmp_list->aer = 0;
10714 		}
10715 		up(&bnx2x_prev_sem);
10716 		return 0;
10717 	}
10718 	up(&bnx2x_prev_sem);
10719 
10720 	/* Create an entry for this path and add it */
10721 	tmp_list = kmalloc(sizeof(struct bnx2x_prev_path_list), GFP_KERNEL);
10722 	if (!tmp_list) {
10723 		BNX2X_ERR("Failed to allocate 'bnx2x_prev_path_list'\n");
10724 		return -ENOMEM;
10725 	}
10726 
10727 	tmp_list->bus = bp->pdev->bus->number;
10728 	tmp_list->slot = PCI_SLOT(bp->pdev->devfn);
10729 	tmp_list->path = BP_PATH(bp);
10730 	tmp_list->aer = 0;
10731 	tmp_list->undi = after_undi ? (1 << BP_PORT(bp)) : 0;
10732 
10733 	rc = down_interruptible(&bnx2x_prev_sem);
10734 	if (rc) {
10735 		BNX2X_ERR("Received %d when tried to take lock\n", rc);
10736 		kfree(tmp_list);
10737 	} else {
10738 		DP(NETIF_MSG_HW, "Marked path [%d] - finished previous unload\n",
10739 		   BP_PATH(bp));
10740 		list_add(&tmp_list->list, &bnx2x_prev_list);
10741 		up(&bnx2x_prev_sem);
10742 	}
10743 
10744 	return rc;
10745 }
10746 
10747 static int bnx2x_do_flr(struct bnx2x *bp)
10748 {
10749 	struct pci_dev *dev = bp->pdev;
10750 
10751 	if (CHIP_IS_E1x(bp)) {
10752 		BNX2X_DEV_INFO("FLR not supported in E1/E1H\n");
10753 		return -EINVAL;
10754 	}
10755 
10756 	/* only bootcode REQ_BC_VER_4_INITIATE_FLR and onwards support flr */
10757 	if (bp->common.bc_ver < REQ_BC_VER_4_INITIATE_FLR) {
10758 		BNX2X_ERR("FLR not supported by BC_VER: 0x%x\n",
10759 			  bp->common.bc_ver);
10760 		return -EINVAL;
10761 	}
10762 
10763 	if (!pci_wait_for_pending_transaction(dev))
10764 		dev_err(&dev->dev, "transaction is not cleared; proceeding with reset anyway\n");
10765 
10766 	BNX2X_DEV_INFO("Initiating FLR\n");
10767 	bnx2x_fw_command(bp, DRV_MSG_CODE_INITIATE_FLR, 0);
10768 
10769 	return 0;
10770 }
10771 
10772 static int bnx2x_prev_unload_uncommon(struct bnx2x *bp)
10773 {
10774 	int rc;
10775 
10776 	BNX2X_DEV_INFO("Uncommon unload Flow\n");
10777 
10778 	/* Test if previous unload process was already finished for this path */
10779 	if (bnx2x_prev_is_path_marked(bp))
10780 		return bnx2x_prev_mcp_done(bp);
10781 
10782 	BNX2X_DEV_INFO("Path is unmarked\n");
10783 
10784 	/* Cannot proceed with FLR if UNDI is loaded, since FW does not match */
10785 	if (bnx2x_prev_is_after_undi(bp))
10786 		goto out;
10787 
10788 	/* If function has FLR capabilities, and existing FW version matches
10789 	 * the one required, then FLR will be sufficient to clean any residue
10790 	 * left by previous driver
10791 	 */
10792 	rc = bnx2x_compare_fw_ver(bp, FW_MSG_CODE_DRV_LOAD_FUNCTION, false);
10793 
10794 	if (!rc) {
10795 		/* fw version is good */
10796 		BNX2X_DEV_INFO("FW version matches our own. Attempting FLR\n");
10797 		rc = bnx2x_do_flr(bp);
10798 	}
10799 
10800 	if (!rc) {
10801 		/* FLR was performed */
10802 		BNX2X_DEV_INFO("FLR successful\n");
10803 		return 0;
10804 	}
10805 
10806 	BNX2X_DEV_INFO("Could not FLR\n");
10807 
10808 out:
10809 	/* Close the MCP request, return failure*/
10810 	rc = bnx2x_prev_mcp_done(bp);
10811 	if (!rc)
10812 		rc = BNX2X_PREV_WAIT_NEEDED;
10813 
10814 	return rc;
10815 }
10816 
10817 static int bnx2x_prev_unload_common(struct bnx2x *bp)
10818 {
10819 	u32 reset_reg, tmp_reg = 0, rc;
10820 	bool prev_undi = false;
10821 	struct bnx2x_mac_vals mac_vals;
10822 
10823 	/* It is possible a previous function received 'common' answer,
10824 	 * but hasn't loaded yet, therefore creating a scenario of
10825 	 * multiple functions receiving 'common' on the same path.
10826 	 */
10827 	BNX2X_DEV_INFO("Common unload Flow\n");
10828 
10829 	memset(&mac_vals, 0, sizeof(mac_vals));
10830 
10831 	if (bnx2x_prev_is_path_marked(bp))
10832 		return bnx2x_prev_mcp_done(bp);
10833 
10834 	reset_reg = REG_RD(bp, MISC_REG_RESET_REG_1);
10835 
10836 	/* Reset should be performed after BRB is emptied */
10837 	if (reset_reg & MISC_REGISTERS_RESET_REG_1_RST_BRB1) {
10838 		u32 timer_count = 1000;
10839 
10840 		/* Close the MAC Rx to prevent BRB from filling up */
10841 		bnx2x_prev_unload_close_mac(bp, &mac_vals);
10842 
10843 		/* close LLH filters for both ports towards the BRB */
10844 		bnx2x_set_rx_filter(&bp->link_params, 0);
10845 		bp->link_params.port ^= 1;
10846 		bnx2x_set_rx_filter(&bp->link_params, 0);
10847 		bp->link_params.port ^= 1;
10848 
10849 		/* Check if the UNDI driver was previously loaded */
10850 		if (bnx2x_prev_is_after_undi(bp)) {
10851 			prev_undi = true;
10852 			/* clear the UNDI indication */
10853 			REG_WR(bp, DORQ_REG_NORM_CID_OFST, 0);
10854 			/* clear possible idle check errors */
10855 			REG_RD(bp, NIG_REG_NIG_INT_STS_CLR_0);
10856 		}
10857 		if (!CHIP_IS_E1x(bp))
10858 			/* block FW from writing to host */
10859 			REG_WR(bp, PGLUE_B_REG_INTERNAL_PFID_ENABLE_MASTER, 0);
10860 
10861 		/* wait until BRB is empty */
10862 		tmp_reg = REG_RD(bp, BRB1_REG_NUM_OF_FULL_BLOCKS);
10863 		while (timer_count) {
10864 			u32 prev_brb = tmp_reg;
10865 
10866 			tmp_reg = REG_RD(bp, BRB1_REG_NUM_OF_FULL_BLOCKS);
10867 			if (!tmp_reg)
10868 				break;
10869 
10870 			BNX2X_DEV_INFO("BRB still has 0x%08x\n", tmp_reg);
10871 
10872 			/* reset timer as long as BRB actually gets emptied */
10873 			if (prev_brb > tmp_reg)
10874 				timer_count = 1000;
10875 			else
10876 				timer_count--;
10877 
10878 			/* If UNDI resides in memory, manually increment it */
10879 			if (prev_undi)
10880 				bnx2x_prev_unload_undi_inc(bp, 1);
10881 
10882 			udelay(10);
10883 		}
10884 
10885 		if (!timer_count)
10886 			BNX2X_ERR("Failed to empty BRB, hope for the best\n");
10887 	}
10888 
10889 	/* No packets are in the pipeline, path is ready for reset */
10890 	bnx2x_reset_common(bp);
10891 
10892 	if (mac_vals.xmac_addr)
10893 		REG_WR(bp, mac_vals.xmac_addr, mac_vals.xmac_val);
10894 	if (mac_vals.umac_addr[0])
10895 		REG_WR(bp, mac_vals.umac_addr[0], mac_vals.umac_val[0]);
10896 	if (mac_vals.umac_addr[1])
10897 		REG_WR(bp, mac_vals.umac_addr[1], mac_vals.umac_val[1]);
10898 	if (mac_vals.emac_addr)
10899 		REG_WR(bp, mac_vals.emac_addr, mac_vals.emac_val);
10900 	if (mac_vals.bmac_addr) {
10901 		REG_WR(bp, mac_vals.bmac_addr, mac_vals.bmac_val[0]);
10902 		REG_WR(bp, mac_vals.bmac_addr + 4, mac_vals.bmac_val[1]);
10903 	}
10904 
10905 	rc = bnx2x_prev_mark_path(bp, prev_undi);
10906 	if (rc) {
10907 		bnx2x_prev_mcp_done(bp);
10908 		return rc;
10909 	}
10910 
10911 	return bnx2x_prev_mcp_done(bp);
10912 }
10913 
10914 static int bnx2x_prev_unload(struct bnx2x *bp)
10915 {
10916 	int time_counter = 10;
10917 	u32 rc, fw, hw_lock_reg, hw_lock_val;
10918 	BNX2X_DEV_INFO("Entering Previous Unload Flow\n");
10919 
10920 	/* clear hw from errors which may have resulted from an interrupted
10921 	 * dmae transaction.
10922 	 */
10923 	bnx2x_clean_pglue_errors(bp);
10924 
10925 	/* Release previously held locks */
10926 	hw_lock_reg = (BP_FUNC(bp) <= 5) ?
10927 		      (MISC_REG_DRIVER_CONTROL_1 + BP_FUNC(bp) * 8) :
10928 		      (MISC_REG_DRIVER_CONTROL_7 + (BP_FUNC(bp) - 6) * 8);
10929 
10930 	hw_lock_val = REG_RD(bp, hw_lock_reg);
10931 	if (hw_lock_val) {
10932 		if (hw_lock_val & HW_LOCK_RESOURCE_NVRAM) {
10933 			BNX2X_DEV_INFO("Release Previously held NVRAM lock\n");
10934 			REG_WR(bp, MCP_REG_MCPR_NVM_SW_ARB,
10935 			       (MCPR_NVM_SW_ARB_ARB_REQ_CLR1 << BP_PORT(bp)));
10936 		}
10937 
10938 		BNX2X_DEV_INFO("Release Previously held hw lock\n");
10939 		REG_WR(bp, hw_lock_reg, 0xffffffff);
10940 	} else
10941 		BNX2X_DEV_INFO("No need to release hw/nvram locks\n");
10942 
10943 	if (MCPR_ACCESS_LOCK_LOCK & REG_RD(bp, MCP_REG_MCPR_ACCESS_LOCK)) {
10944 		BNX2X_DEV_INFO("Release previously held alr\n");
10945 		bnx2x_release_alr(bp);
10946 	}
10947 
10948 	do {
10949 		int aer = 0;
10950 		/* Lock MCP using an unload request */
10951 		fw = bnx2x_fw_command(bp, DRV_MSG_CODE_UNLOAD_REQ_WOL_DIS, 0);
10952 		if (!fw) {
10953 			BNX2X_ERR("MCP response failure, aborting\n");
10954 			rc = -EBUSY;
10955 			break;
10956 		}
10957 
10958 		rc = down_interruptible(&bnx2x_prev_sem);
10959 		if (rc) {
10960 			BNX2X_ERR("Cannot check for AER; Received %d when tried to take lock\n",
10961 				  rc);
10962 		} else {
10963 			/* If Path is marked by EEH, ignore unload status */
10964 			aer = !!(bnx2x_prev_path_get_entry(bp) &&
10965 				 bnx2x_prev_path_get_entry(bp)->aer);
10966 			up(&bnx2x_prev_sem);
10967 		}
10968 
10969 		if (fw == FW_MSG_CODE_DRV_UNLOAD_COMMON || aer) {
10970 			rc = bnx2x_prev_unload_common(bp);
10971 			break;
10972 		}
10973 
10974 		/* non-common reply from MCP might require looping */
10975 		rc = bnx2x_prev_unload_uncommon(bp);
10976 		if (rc != BNX2X_PREV_WAIT_NEEDED)
10977 			break;
10978 
10979 		msleep(20);
10980 	} while (--time_counter);
10981 
10982 	if (!time_counter || rc) {
10983 		BNX2X_DEV_INFO("Unloading previous driver did not occur, Possibly due to MF UNDI\n");
10984 		rc = -EPROBE_DEFER;
10985 	}
10986 
10987 	/* Mark function if its port was used to boot from SAN */
10988 	if (bnx2x_port_after_undi(bp))
10989 		bp->link_params.feature_config_flags |=
10990 			FEATURE_CONFIG_BOOT_FROM_SAN;
10991 
10992 	BNX2X_DEV_INFO("Finished Previous Unload Flow [%d]\n", rc);
10993 
10994 	return rc;
10995 }
10996 
10997 static void bnx2x_get_common_hwinfo(struct bnx2x *bp)
10998 {
10999 	u32 val, val2, val3, val4, id, boot_mode;
11000 	u16 pmc;
11001 
11002 	/* Get the chip revision id and number. */
11003 	/* chip num:16-31, rev:12-15, metal:4-11, bond_id:0-3 */
11004 	val = REG_RD(bp, MISC_REG_CHIP_NUM);
11005 	id = ((val & 0xffff) << 16);
11006 	val = REG_RD(bp, MISC_REG_CHIP_REV);
11007 	id |= ((val & 0xf) << 12);
11008 
11009 	/* Metal is read from PCI regs, but we can't access >=0x400 from
11010 	 * the configuration space (so we need to reg_rd)
11011 	 */
11012 	val = REG_RD(bp, PCICFG_OFFSET + PCI_ID_VAL3);
11013 	id |= (((val >> 24) & 0xf) << 4);
11014 	val = REG_RD(bp, MISC_REG_BOND_ID);
11015 	id |= (val & 0xf);
11016 	bp->common.chip_id = id;
11017 
11018 	/* force 57811 according to MISC register */
11019 	if (REG_RD(bp, MISC_REG_CHIP_TYPE) & MISC_REG_CHIP_TYPE_57811_MASK) {
11020 		if (CHIP_IS_57810(bp))
11021 			bp->common.chip_id = (CHIP_NUM_57811 << 16) |
11022 				(bp->common.chip_id & 0x0000FFFF);
11023 		else if (CHIP_IS_57810_MF(bp))
11024 			bp->common.chip_id = (CHIP_NUM_57811_MF << 16) |
11025 				(bp->common.chip_id & 0x0000FFFF);
11026 		bp->common.chip_id |= 0x1;
11027 	}
11028 
11029 	/* Set doorbell size */
11030 	bp->db_size = (1 << BNX2X_DB_SHIFT);
11031 
11032 	if (!CHIP_IS_E1x(bp)) {
11033 		val = REG_RD(bp, MISC_REG_PORT4MODE_EN_OVWR);
11034 		if ((val & 1) == 0)
11035 			val = REG_RD(bp, MISC_REG_PORT4MODE_EN);
11036 		else
11037 			val = (val >> 1) & 1;
11038 		BNX2X_DEV_INFO("chip is in %s\n", val ? "4_PORT_MODE" :
11039 						       "2_PORT_MODE");
11040 		bp->common.chip_port_mode = val ? CHIP_4_PORT_MODE :
11041 						 CHIP_2_PORT_MODE;
11042 
11043 		if (CHIP_MODE_IS_4_PORT(bp))
11044 			bp->pfid = (bp->pf_num >> 1);	/* 0..3 */
11045 		else
11046 			bp->pfid = (bp->pf_num & 0x6);	/* 0, 2, 4, 6 */
11047 	} else {
11048 		bp->common.chip_port_mode = CHIP_PORT_MODE_NONE; /* N/A */
11049 		bp->pfid = bp->pf_num;			/* 0..7 */
11050 	}
11051 
11052 	BNX2X_DEV_INFO("pf_id: %x", bp->pfid);
11053 
11054 	bp->link_params.chip_id = bp->common.chip_id;
11055 	BNX2X_DEV_INFO("chip ID is 0x%x\n", id);
11056 
11057 	val = (REG_RD(bp, 0x2874) & 0x55);
11058 	if ((bp->common.chip_id & 0x1) ||
11059 	    (CHIP_IS_E1(bp) && val) || (CHIP_IS_E1H(bp) && (val == 0x55))) {
11060 		bp->flags |= ONE_PORT_FLAG;
11061 		BNX2X_DEV_INFO("single port device\n");
11062 	}
11063 
11064 	val = REG_RD(bp, MCP_REG_MCPR_NVM_CFG4);
11065 	bp->common.flash_size = (BNX2X_NVRAM_1MB_SIZE <<
11066 				 (val & MCPR_NVM_CFG4_FLASH_SIZE));
11067 	BNX2X_DEV_INFO("flash_size 0x%x (%d)\n",
11068 		       bp->common.flash_size, bp->common.flash_size);
11069 
11070 	bnx2x_init_shmem(bp);
11071 
11072 	bp->common.shmem2_base = REG_RD(bp, (BP_PATH(bp) ?
11073 					MISC_REG_GENERIC_CR_1 :
11074 					MISC_REG_GENERIC_CR_0));
11075 
11076 	bp->link_params.shmem_base = bp->common.shmem_base;
11077 	bp->link_params.shmem2_base = bp->common.shmem2_base;
11078 	if (SHMEM2_RD(bp, size) >
11079 	    (u32)offsetof(struct shmem2_region, lfa_host_addr[BP_PORT(bp)]))
11080 		bp->link_params.lfa_base =
11081 		REG_RD(bp, bp->common.shmem2_base +
11082 		       (u32)offsetof(struct shmem2_region,
11083 				     lfa_host_addr[BP_PORT(bp)]));
11084 	else
11085 		bp->link_params.lfa_base = 0;
11086 	BNX2X_DEV_INFO("shmem offset 0x%x  shmem2 offset 0x%x\n",
11087 		       bp->common.shmem_base, bp->common.shmem2_base);
11088 
11089 	if (!bp->common.shmem_base) {
11090 		BNX2X_DEV_INFO("MCP not active\n");
11091 		bp->flags |= NO_MCP_FLAG;
11092 		return;
11093 	}
11094 
11095 	bp->common.hw_config = SHMEM_RD(bp, dev_info.shared_hw_config.config);
11096 	BNX2X_DEV_INFO("hw_config 0x%08x\n", bp->common.hw_config);
11097 
11098 	bp->link_params.hw_led_mode = ((bp->common.hw_config &
11099 					SHARED_HW_CFG_LED_MODE_MASK) >>
11100 				       SHARED_HW_CFG_LED_MODE_SHIFT);
11101 
11102 	bp->link_params.feature_config_flags = 0;
11103 	val = SHMEM_RD(bp, dev_info.shared_feature_config.config);
11104 	if (val & SHARED_FEAT_CFG_OVERRIDE_PREEMPHASIS_CFG_ENABLED)
11105 		bp->link_params.feature_config_flags |=
11106 				FEATURE_CONFIG_OVERRIDE_PREEMPHASIS_ENABLED;
11107 	else
11108 		bp->link_params.feature_config_flags &=
11109 				~FEATURE_CONFIG_OVERRIDE_PREEMPHASIS_ENABLED;
11110 
11111 	val = SHMEM_RD(bp, dev_info.bc_rev) >> 8;
11112 	bp->common.bc_ver = val;
11113 	BNX2X_DEV_INFO("bc_ver %X\n", val);
11114 	if (val < BNX2X_BC_VER) {
11115 		/* for now only warn
11116 		 * later we might need to enforce this */
11117 		BNX2X_ERR("This driver needs bc_ver %X but found %X, please upgrade BC\n",
11118 			  BNX2X_BC_VER, val);
11119 	}
11120 	bp->link_params.feature_config_flags |=
11121 				(val >= REQ_BC_VER_4_VRFY_FIRST_PHY_OPT_MDL) ?
11122 				FEATURE_CONFIG_BC_SUPPORTS_OPT_MDL_VRFY : 0;
11123 
11124 	bp->link_params.feature_config_flags |=
11125 		(val >= REQ_BC_VER_4_VRFY_SPECIFIC_PHY_OPT_MDL) ?
11126 		FEATURE_CONFIG_BC_SUPPORTS_DUAL_PHY_OPT_MDL_VRFY : 0;
11127 	bp->link_params.feature_config_flags |=
11128 		(val >= REQ_BC_VER_4_VRFY_AFEX_SUPPORTED) ?
11129 		FEATURE_CONFIG_BC_SUPPORTS_AFEX : 0;
11130 	bp->link_params.feature_config_flags |=
11131 		(val >= REQ_BC_VER_4_SFP_TX_DISABLE_SUPPORTED) ?
11132 		FEATURE_CONFIG_BC_SUPPORTS_SFP_TX_DISABLED : 0;
11133 
11134 	bp->link_params.feature_config_flags |=
11135 		(val >= REQ_BC_VER_4_MT_SUPPORTED) ?
11136 		FEATURE_CONFIG_MT_SUPPORT : 0;
11137 
11138 	bp->flags |= (val >= REQ_BC_VER_4_PFC_STATS_SUPPORTED) ?
11139 			BC_SUPPORTS_PFC_STATS : 0;
11140 
11141 	bp->flags |= (val >= REQ_BC_VER_4_FCOE_FEATURES) ?
11142 			BC_SUPPORTS_FCOE_FEATURES : 0;
11143 
11144 	bp->flags |= (val >= REQ_BC_VER_4_DCBX_ADMIN_MSG_NON_PMF) ?
11145 			BC_SUPPORTS_DCBX_MSG_NON_PMF : 0;
11146 
11147 	bp->flags |= (val >= REQ_BC_VER_4_RMMOD_CMD) ?
11148 			BC_SUPPORTS_RMMOD_CMD : 0;
11149 
11150 	boot_mode = SHMEM_RD(bp,
11151 			dev_info.port_feature_config[BP_PORT(bp)].mba_config) &
11152 			PORT_FEATURE_MBA_BOOT_AGENT_TYPE_MASK;
11153 	switch (boot_mode) {
11154 	case PORT_FEATURE_MBA_BOOT_AGENT_TYPE_PXE:
11155 		bp->common.boot_mode = FEATURE_ETH_BOOTMODE_PXE;
11156 		break;
11157 	case PORT_FEATURE_MBA_BOOT_AGENT_TYPE_ISCSIB:
11158 		bp->common.boot_mode = FEATURE_ETH_BOOTMODE_ISCSI;
11159 		break;
11160 	case PORT_FEATURE_MBA_BOOT_AGENT_TYPE_FCOE_BOOT:
11161 		bp->common.boot_mode = FEATURE_ETH_BOOTMODE_FCOE;
11162 		break;
11163 	case PORT_FEATURE_MBA_BOOT_AGENT_TYPE_NONE:
11164 		bp->common.boot_mode = FEATURE_ETH_BOOTMODE_NONE;
11165 		break;
11166 	}
11167 
11168 	pci_read_config_word(bp->pdev, bp->pdev->pm_cap + PCI_PM_PMC, &pmc);
11169 	bp->flags |= (pmc & PCI_PM_CAP_PME_D3cold) ? 0 : NO_WOL_FLAG;
11170 
11171 	BNX2X_DEV_INFO("%sWoL capable\n",
11172 		       (bp->flags & NO_WOL_FLAG) ? "not " : "");
11173 
11174 	val = SHMEM_RD(bp, dev_info.shared_hw_config.part_num);
11175 	val2 = SHMEM_RD(bp, dev_info.shared_hw_config.part_num[4]);
11176 	val3 = SHMEM_RD(bp, dev_info.shared_hw_config.part_num[8]);
11177 	val4 = SHMEM_RD(bp, dev_info.shared_hw_config.part_num[12]);
11178 
11179 	dev_info(&bp->pdev->dev, "part number %X-%X-%X-%X\n",
11180 		 val, val2, val3, val4);
11181 }
11182 
11183 #define IGU_FID(val)	GET_FIELD((val), IGU_REG_MAPPING_MEMORY_FID)
11184 #define IGU_VEC(val)	GET_FIELD((val), IGU_REG_MAPPING_MEMORY_VECTOR)
11185 
11186 static int bnx2x_get_igu_cam_info(struct bnx2x *bp)
11187 {
11188 	int pfid = BP_FUNC(bp);
11189 	int igu_sb_id;
11190 	u32 val;
11191 	u8 fid, igu_sb_cnt = 0;
11192 
11193 	bp->igu_base_sb = 0xff;
11194 	if (CHIP_INT_MODE_IS_BC(bp)) {
11195 		int vn = BP_VN(bp);
11196 		igu_sb_cnt = bp->igu_sb_cnt;
11197 		bp->igu_base_sb = (CHIP_MODE_IS_4_PORT(bp) ? pfid : vn) *
11198 			FP_SB_MAX_E1x;
11199 
11200 		bp->igu_dsb_id =  E1HVN_MAX * FP_SB_MAX_E1x +
11201 			(CHIP_MODE_IS_4_PORT(bp) ? pfid : vn);
11202 
11203 		return 0;
11204 	}
11205 
11206 	/* IGU in normal mode - read CAM */
11207 	for (igu_sb_id = 0; igu_sb_id < IGU_REG_MAPPING_MEMORY_SIZE;
11208 	     igu_sb_id++) {
11209 		val = REG_RD(bp, IGU_REG_MAPPING_MEMORY + igu_sb_id * 4);
11210 		if (!(val & IGU_REG_MAPPING_MEMORY_VALID))
11211 			continue;
11212 		fid = IGU_FID(val);
11213 		if ((fid & IGU_FID_ENCODE_IS_PF)) {
11214 			if ((fid & IGU_FID_PF_NUM_MASK) != pfid)
11215 				continue;
11216 			if (IGU_VEC(val) == 0)
11217 				/* default status block */
11218 				bp->igu_dsb_id = igu_sb_id;
11219 			else {
11220 				if (bp->igu_base_sb == 0xff)
11221 					bp->igu_base_sb = igu_sb_id;
11222 				igu_sb_cnt++;
11223 			}
11224 		}
11225 	}
11226 
11227 #ifdef CONFIG_PCI_MSI
11228 	/* Due to new PF resource allocation by MFW T7.4 and above, it's
11229 	 * optional that number of CAM entries will not be equal to the value
11230 	 * advertised in PCI.
11231 	 * Driver should use the minimal value of both as the actual status
11232 	 * block count
11233 	 */
11234 	bp->igu_sb_cnt = min_t(int, bp->igu_sb_cnt, igu_sb_cnt);
11235 #endif
11236 
11237 	if (igu_sb_cnt == 0) {
11238 		BNX2X_ERR("CAM configuration error\n");
11239 		return -EINVAL;
11240 	}
11241 
11242 	return 0;
11243 }
11244 
11245 static void bnx2x_link_settings_supported(struct bnx2x *bp, u32 switch_cfg)
11246 {
11247 	int cfg_size = 0, idx, port = BP_PORT(bp);
11248 
11249 	/* Aggregation of supported attributes of all external phys */
11250 	bp->port.supported[0] = 0;
11251 	bp->port.supported[1] = 0;
11252 	switch (bp->link_params.num_phys) {
11253 	case 1:
11254 		bp->port.supported[0] = bp->link_params.phy[INT_PHY].supported;
11255 		cfg_size = 1;
11256 		break;
11257 	case 2:
11258 		bp->port.supported[0] = bp->link_params.phy[EXT_PHY1].supported;
11259 		cfg_size = 1;
11260 		break;
11261 	case 3:
11262 		if (bp->link_params.multi_phy_config &
11263 		    PORT_HW_CFG_PHY_SWAPPED_ENABLED) {
11264 			bp->port.supported[1] =
11265 				bp->link_params.phy[EXT_PHY1].supported;
11266 			bp->port.supported[0] =
11267 				bp->link_params.phy[EXT_PHY2].supported;
11268 		} else {
11269 			bp->port.supported[0] =
11270 				bp->link_params.phy[EXT_PHY1].supported;
11271 			bp->port.supported[1] =
11272 				bp->link_params.phy[EXT_PHY2].supported;
11273 		}
11274 		cfg_size = 2;
11275 		break;
11276 	}
11277 
11278 	if (!(bp->port.supported[0] || bp->port.supported[1])) {
11279 		BNX2X_ERR("NVRAM config error. BAD phy config. PHY1 config 0x%x, PHY2 config 0x%x\n",
11280 			   SHMEM_RD(bp,
11281 			   dev_info.port_hw_config[port].external_phy_config),
11282 			   SHMEM_RD(bp,
11283 			   dev_info.port_hw_config[port].external_phy_config2));
11284 		return;
11285 	}
11286 
11287 	if (CHIP_IS_E3(bp))
11288 		bp->port.phy_addr = REG_RD(bp, MISC_REG_WC0_CTRL_PHY_ADDR);
11289 	else {
11290 		switch (switch_cfg) {
11291 		case SWITCH_CFG_1G:
11292 			bp->port.phy_addr = REG_RD(
11293 				bp, NIG_REG_SERDES0_CTRL_PHY_ADDR + port*0x10);
11294 			break;
11295 		case SWITCH_CFG_10G:
11296 			bp->port.phy_addr = REG_RD(
11297 				bp, NIG_REG_XGXS0_CTRL_PHY_ADDR + port*0x18);
11298 			break;
11299 		default:
11300 			BNX2X_ERR("BAD switch_cfg link_config 0x%x\n",
11301 				  bp->port.link_config[0]);
11302 			return;
11303 		}
11304 	}
11305 	BNX2X_DEV_INFO("phy_addr 0x%x\n", bp->port.phy_addr);
11306 	/* mask what we support according to speed_cap_mask per configuration */
11307 	for (idx = 0; idx < cfg_size; idx++) {
11308 		if (!(bp->link_params.speed_cap_mask[idx] &
11309 				PORT_HW_CFG_SPEED_CAPABILITY_D0_10M_HALF))
11310 			bp->port.supported[idx] &= ~SUPPORTED_10baseT_Half;
11311 
11312 		if (!(bp->link_params.speed_cap_mask[idx] &
11313 				PORT_HW_CFG_SPEED_CAPABILITY_D0_10M_FULL))
11314 			bp->port.supported[idx] &= ~SUPPORTED_10baseT_Full;
11315 
11316 		if (!(bp->link_params.speed_cap_mask[idx] &
11317 				PORT_HW_CFG_SPEED_CAPABILITY_D0_100M_HALF))
11318 			bp->port.supported[idx] &= ~SUPPORTED_100baseT_Half;
11319 
11320 		if (!(bp->link_params.speed_cap_mask[idx] &
11321 				PORT_HW_CFG_SPEED_CAPABILITY_D0_100M_FULL))
11322 			bp->port.supported[idx] &= ~SUPPORTED_100baseT_Full;
11323 
11324 		if (!(bp->link_params.speed_cap_mask[idx] &
11325 					PORT_HW_CFG_SPEED_CAPABILITY_D0_1G))
11326 			bp->port.supported[idx] &= ~(SUPPORTED_1000baseT_Half |
11327 						     SUPPORTED_1000baseT_Full);
11328 
11329 		if (!(bp->link_params.speed_cap_mask[idx] &
11330 					PORT_HW_CFG_SPEED_CAPABILITY_D0_2_5G))
11331 			bp->port.supported[idx] &= ~SUPPORTED_2500baseX_Full;
11332 
11333 		if (!(bp->link_params.speed_cap_mask[idx] &
11334 					PORT_HW_CFG_SPEED_CAPABILITY_D0_10G))
11335 			bp->port.supported[idx] &= ~SUPPORTED_10000baseT_Full;
11336 
11337 		if (!(bp->link_params.speed_cap_mask[idx] &
11338 					PORT_HW_CFG_SPEED_CAPABILITY_D0_20G))
11339 			bp->port.supported[idx] &= ~SUPPORTED_20000baseKR2_Full;
11340 	}
11341 
11342 	BNX2X_DEV_INFO("supported 0x%x 0x%x\n", bp->port.supported[0],
11343 		       bp->port.supported[1]);
11344 }
11345 
11346 static void bnx2x_link_settings_requested(struct bnx2x *bp)
11347 {
11348 	u32 link_config, idx, cfg_size = 0;
11349 	bp->port.advertising[0] = 0;
11350 	bp->port.advertising[1] = 0;
11351 	switch (bp->link_params.num_phys) {
11352 	case 1:
11353 	case 2:
11354 		cfg_size = 1;
11355 		break;
11356 	case 3:
11357 		cfg_size = 2;
11358 		break;
11359 	}
11360 	for (idx = 0; idx < cfg_size; idx++) {
11361 		bp->link_params.req_duplex[idx] = DUPLEX_FULL;
11362 		link_config = bp->port.link_config[idx];
11363 		switch (link_config & PORT_FEATURE_LINK_SPEED_MASK) {
11364 		case PORT_FEATURE_LINK_SPEED_AUTO:
11365 			if (bp->port.supported[idx] & SUPPORTED_Autoneg) {
11366 				bp->link_params.req_line_speed[idx] =
11367 					SPEED_AUTO_NEG;
11368 				bp->port.advertising[idx] |=
11369 					bp->port.supported[idx];
11370 				if (bp->link_params.phy[EXT_PHY1].type ==
11371 				    PORT_HW_CFG_XGXS_EXT_PHY_TYPE_BCM84833)
11372 					bp->port.advertising[idx] |=
11373 					(SUPPORTED_100baseT_Half |
11374 					 SUPPORTED_100baseT_Full);
11375 			} else {
11376 				/* force 10G, no AN */
11377 				bp->link_params.req_line_speed[idx] =
11378 					SPEED_10000;
11379 				bp->port.advertising[idx] |=
11380 					(ADVERTISED_10000baseT_Full |
11381 					 ADVERTISED_FIBRE);
11382 				continue;
11383 			}
11384 			break;
11385 
11386 		case PORT_FEATURE_LINK_SPEED_10M_FULL:
11387 			if (bp->port.supported[idx] & SUPPORTED_10baseT_Full) {
11388 				bp->link_params.req_line_speed[idx] =
11389 					SPEED_10;
11390 				bp->port.advertising[idx] |=
11391 					(ADVERTISED_10baseT_Full |
11392 					 ADVERTISED_TP);
11393 			} else {
11394 				BNX2X_ERR("NVRAM config error. Invalid link_config 0x%x  speed_cap_mask 0x%x\n",
11395 					    link_config,
11396 				    bp->link_params.speed_cap_mask[idx]);
11397 				return;
11398 			}
11399 			break;
11400 
11401 		case PORT_FEATURE_LINK_SPEED_10M_HALF:
11402 			if (bp->port.supported[idx] & SUPPORTED_10baseT_Half) {
11403 				bp->link_params.req_line_speed[idx] =
11404 					SPEED_10;
11405 				bp->link_params.req_duplex[idx] =
11406 					DUPLEX_HALF;
11407 				bp->port.advertising[idx] |=
11408 					(ADVERTISED_10baseT_Half |
11409 					 ADVERTISED_TP);
11410 			} else {
11411 				BNX2X_ERR("NVRAM config error. Invalid link_config 0x%x  speed_cap_mask 0x%x\n",
11412 					    link_config,
11413 					  bp->link_params.speed_cap_mask[idx]);
11414 				return;
11415 			}
11416 			break;
11417 
11418 		case PORT_FEATURE_LINK_SPEED_100M_FULL:
11419 			if (bp->port.supported[idx] &
11420 			    SUPPORTED_100baseT_Full) {
11421 				bp->link_params.req_line_speed[idx] =
11422 					SPEED_100;
11423 				bp->port.advertising[idx] |=
11424 					(ADVERTISED_100baseT_Full |
11425 					 ADVERTISED_TP);
11426 			} else {
11427 				BNX2X_ERR("NVRAM config error. Invalid link_config 0x%x  speed_cap_mask 0x%x\n",
11428 					    link_config,
11429 					  bp->link_params.speed_cap_mask[idx]);
11430 				return;
11431 			}
11432 			break;
11433 
11434 		case PORT_FEATURE_LINK_SPEED_100M_HALF:
11435 			if (bp->port.supported[idx] &
11436 			    SUPPORTED_100baseT_Half) {
11437 				bp->link_params.req_line_speed[idx] =
11438 								SPEED_100;
11439 				bp->link_params.req_duplex[idx] =
11440 								DUPLEX_HALF;
11441 				bp->port.advertising[idx] |=
11442 					(ADVERTISED_100baseT_Half |
11443 					 ADVERTISED_TP);
11444 			} else {
11445 				BNX2X_ERR("NVRAM config error. Invalid link_config 0x%x  speed_cap_mask 0x%x\n",
11446 				    link_config,
11447 				    bp->link_params.speed_cap_mask[idx]);
11448 				return;
11449 			}
11450 			break;
11451 
11452 		case PORT_FEATURE_LINK_SPEED_1G:
11453 			if (bp->port.supported[idx] &
11454 			    SUPPORTED_1000baseT_Full) {
11455 				bp->link_params.req_line_speed[idx] =
11456 					SPEED_1000;
11457 				bp->port.advertising[idx] |=
11458 					(ADVERTISED_1000baseT_Full |
11459 					 ADVERTISED_TP);
11460 			} else if (bp->port.supported[idx] &
11461 				   SUPPORTED_1000baseKX_Full) {
11462 				bp->link_params.req_line_speed[idx] =
11463 					SPEED_1000;
11464 				bp->port.advertising[idx] |=
11465 					ADVERTISED_1000baseKX_Full;
11466 			} else {
11467 				BNX2X_ERR("NVRAM config error. Invalid link_config 0x%x  speed_cap_mask 0x%x\n",
11468 				    link_config,
11469 				    bp->link_params.speed_cap_mask[idx]);
11470 				return;
11471 			}
11472 			break;
11473 
11474 		case PORT_FEATURE_LINK_SPEED_2_5G:
11475 			if (bp->port.supported[idx] &
11476 			    SUPPORTED_2500baseX_Full) {
11477 				bp->link_params.req_line_speed[idx] =
11478 					SPEED_2500;
11479 				bp->port.advertising[idx] |=
11480 					(ADVERTISED_2500baseX_Full |
11481 						ADVERTISED_TP);
11482 			} else {
11483 				BNX2X_ERR("NVRAM config error. Invalid link_config 0x%x  speed_cap_mask 0x%x\n",
11484 				    link_config,
11485 				    bp->link_params.speed_cap_mask[idx]);
11486 				return;
11487 			}
11488 			break;
11489 
11490 		case PORT_FEATURE_LINK_SPEED_10G_CX4:
11491 			if (bp->port.supported[idx] &
11492 			    SUPPORTED_10000baseT_Full) {
11493 				bp->link_params.req_line_speed[idx] =
11494 					SPEED_10000;
11495 				bp->port.advertising[idx] |=
11496 					(ADVERTISED_10000baseT_Full |
11497 						ADVERTISED_FIBRE);
11498 			} else if (bp->port.supported[idx] &
11499 				   SUPPORTED_10000baseKR_Full) {
11500 				bp->link_params.req_line_speed[idx] =
11501 					SPEED_10000;
11502 				bp->port.advertising[idx] |=
11503 					(ADVERTISED_10000baseKR_Full |
11504 						ADVERTISED_FIBRE);
11505 			} else {
11506 				BNX2X_ERR("NVRAM config error. Invalid link_config 0x%x  speed_cap_mask 0x%x\n",
11507 				    link_config,
11508 				    bp->link_params.speed_cap_mask[idx]);
11509 				return;
11510 			}
11511 			break;
11512 		case PORT_FEATURE_LINK_SPEED_20G:
11513 			bp->link_params.req_line_speed[idx] = SPEED_20000;
11514 
11515 			break;
11516 		default:
11517 			BNX2X_ERR("NVRAM config error. BAD link speed link_config 0x%x\n",
11518 				  link_config);
11519 				bp->link_params.req_line_speed[idx] =
11520 							SPEED_AUTO_NEG;
11521 				bp->port.advertising[idx] =
11522 						bp->port.supported[idx];
11523 			break;
11524 		}
11525 
11526 		bp->link_params.req_flow_ctrl[idx] = (link_config &
11527 					 PORT_FEATURE_FLOW_CONTROL_MASK);
11528 		if (bp->link_params.req_flow_ctrl[idx] ==
11529 		    BNX2X_FLOW_CTRL_AUTO) {
11530 			if (!(bp->port.supported[idx] & SUPPORTED_Autoneg))
11531 				bp->link_params.req_flow_ctrl[idx] =
11532 							BNX2X_FLOW_CTRL_NONE;
11533 			else
11534 				bnx2x_set_requested_fc(bp);
11535 		}
11536 
11537 		BNX2X_DEV_INFO("req_line_speed %d  req_duplex %d req_flow_ctrl 0x%x advertising 0x%x\n",
11538 			       bp->link_params.req_line_speed[idx],
11539 			       bp->link_params.req_duplex[idx],
11540 			       bp->link_params.req_flow_ctrl[idx],
11541 			       bp->port.advertising[idx]);
11542 	}
11543 }
11544 
11545 static void bnx2x_set_mac_buf(u8 *mac_buf, u32 mac_lo, u16 mac_hi)
11546 {
11547 	__be16 mac_hi_be = cpu_to_be16(mac_hi);
11548 	__be32 mac_lo_be = cpu_to_be32(mac_lo);
11549 	memcpy(mac_buf, &mac_hi_be, sizeof(mac_hi_be));
11550 	memcpy(mac_buf + sizeof(mac_hi_be), &mac_lo_be, sizeof(mac_lo_be));
11551 }
11552 
11553 static void bnx2x_get_port_hwinfo(struct bnx2x *bp)
11554 {
11555 	int port = BP_PORT(bp);
11556 	u32 config;
11557 	u32 ext_phy_type, ext_phy_config, eee_mode;
11558 
11559 	bp->link_params.bp = bp;
11560 	bp->link_params.port = port;
11561 
11562 	bp->link_params.lane_config =
11563 		SHMEM_RD(bp, dev_info.port_hw_config[port].lane_config);
11564 
11565 	bp->link_params.speed_cap_mask[0] =
11566 		SHMEM_RD(bp,
11567 			 dev_info.port_hw_config[port].speed_capability_mask) &
11568 		PORT_HW_CFG_SPEED_CAPABILITY_D0_MASK;
11569 	bp->link_params.speed_cap_mask[1] =
11570 		SHMEM_RD(bp,
11571 			 dev_info.port_hw_config[port].speed_capability_mask2) &
11572 		PORT_HW_CFG_SPEED_CAPABILITY_D0_MASK;
11573 	bp->port.link_config[0] =
11574 		SHMEM_RD(bp, dev_info.port_feature_config[port].link_config);
11575 
11576 	bp->port.link_config[1] =
11577 		SHMEM_RD(bp, dev_info.port_feature_config[port].link_config2);
11578 
11579 	bp->link_params.multi_phy_config =
11580 		SHMEM_RD(bp, dev_info.port_hw_config[port].multi_phy_config);
11581 	/* If the device is capable of WoL, set the default state according
11582 	 * to the HW
11583 	 */
11584 	config = SHMEM_RD(bp, dev_info.port_feature_config[port].config);
11585 	bp->wol = (!(bp->flags & NO_WOL_FLAG) &&
11586 		   (config & PORT_FEATURE_WOL_ENABLED));
11587 
11588 	if ((config & PORT_FEAT_CFG_STORAGE_PERSONALITY_MASK) ==
11589 	    PORT_FEAT_CFG_STORAGE_PERSONALITY_FCOE && !IS_MF(bp))
11590 		bp->flags |= NO_ISCSI_FLAG;
11591 	if ((config & PORT_FEAT_CFG_STORAGE_PERSONALITY_MASK) ==
11592 	    PORT_FEAT_CFG_STORAGE_PERSONALITY_ISCSI && !(IS_MF(bp)))
11593 		bp->flags |= NO_FCOE_FLAG;
11594 
11595 	BNX2X_DEV_INFO("lane_config 0x%08x  speed_cap_mask0 0x%08x  link_config0 0x%08x\n",
11596 		       bp->link_params.lane_config,
11597 		       bp->link_params.speed_cap_mask[0],
11598 		       bp->port.link_config[0]);
11599 
11600 	bp->link_params.switch_cfg = (bp->port.link_config[0] &
11601 				      PORT_FEATURE_CONNECTED_SWITCH_MASK);
11602 	bnx2x_phy_probe(&bp->link_params);
11603 	bnx2x_link_settings_supported(bp, bp->link_params.switch_cfg);
11604 
11605 	bnx2x_link_settings_requested(bp);
11606 
11607 	/*
11608 	 * If connected directly, work with the internal PHY, otherwise, work
11609 	 * with the external PHY
11610 	 */
11611 	ext_phy_config =
11612 		SHMEM_RD(bp,
11613 			 dev_info.port_hw_config[port].external_phy_config);
11614 	ext_phy_type = XGXS_EXT_PHY_TYPE(ext_phy_config);
11615 	if (ext_phy_type == PORT_HW_CFG_XGXS_EXT_PHY_TYPE_DIRECT)
11616 		bp->mdio.prtad = bp->port.phy_addr;
11617 
11618 	else if ((ext_phy_type != PORT_HW_CFG_XGXS_EXT_PHY_TYPE_FAILURE) &&
11619 		 (ext_phy_type != PORT_HW_CFG_XGXS_EXT_PHY_TYPE_NOT_CONN))
11620 		bp->mdio.prtad =
11621 			XGXS_EXT_PHY_ADDR(ext_phy_config);
11622 
11623 	/* Configure link feature according to nvram value */
11624 	eee_mode = (((SHMEM_RD(bp, dev_info.
11625 		      port_feature_config[port].eee_power_mode)) &
11626 		     PORT_FEAT_CFG_EEE_POWER_MODE_MASK) >>
11627 		    PORT_FEAT_CFG_EEE_POWER_MODE_SHIFT);
11628 	if (eee_mode != PORT_FEAT_CFG_EEE_POWER_MODE_DISABLED) {
11629 		bp->link_params.eee_mode = EEE_MODE_ADV_LPI |
11630 					   EEE_MODE_ENABLE_LPI |
11631 					   EEE_MODE_OUTPUT_TIME;
11632 	} else {
11633 		bp->link_params.eee_mode = 0;
11634 	}
11635 }
11636 
11637 void bnx2x_get_iscsi_info(struct bnx2x *bp)
11638 {
11639 	u32 no_flags = NO_ISCSI_FLAG;
11640 	int port = BP_PORT(bp);
11641 	u32 max_iscsi_conn = FW_ENCODE_32BIT_PATTERN ^ SHMEM_RD(bp,
11642 				drv_lic_key[port].max_iscsi_conn);
11643 
11644 	if (!CNIC_SUPPORT(bp)) {
11645 		bp->flags |= no_flags;
11646 		return;
11647 	}
11648 
11649 	/* Get the number of maximum allowed iSCSI connections */
11650 	bp->cnic_eth_dev.max_iscsi_conn =
11651 		(max_iscsi_conn & BNX2X_MAX_ISCSI_INIT_CONN_MASK) >>
11652 		BNX2X_MAX_ISCSI_INIT_CONN_SHIFT;
11653 
11654 	BNX2X_DEV_INFO("max_iscsi_conn 0x%x\n",
11655 		       bp->cnic_eth_dev.max_iscsi_conn);
11656 
11657 	/*
11658 	 * If maximum allowed number of connections is zero -
11659 	 * disable the feature.
11660 	 */
11661 	if (!bp->cnic_eth_dev.max_iscsi_conn)
11662 		bp->flags |= no_flags;
11663 }
11664 
11665 static void bnx2x_get_ext_wwn_info(struct bnx2x *bp, int func)
11666 {
11667 	/* Port info */
11668 	bp->cnic_eth_dev.fcoe_wwn_port_name_hi =
11669 		MF_CFG_RD(bp, func_ext_config[func].fcoe_wwn_port_name_upper);
11670 	bp->cnic_eth_dev.fcoe_wwn_port_name_lo =
11671 		MF_CFG_RD(bp, func_ext_config[func].fcoe_wwn_port_name_lower);
11672 
11673 	/* Node info */
11674 	bp->cnic_eth_dev.fcoe_wwn_node_name_hi =
11675 		MF_CFG_RD(bp, func_ext_config[func].fcoe_wwn_node_name_upper);
11676 	bp->cnic_eth_dev.fcoe_wwn_node_name_lo =
11677 		MF_CFG_RD(bp, func_ext_config[func].fcoe_wwn_node_name_lower);
11678 }
11679 
11680 static int bnx2x_shared_fcoe_funcs(struct bnx2x *bp)
11681 {
11682 	u8 count = 0;
11683 
11684 	if (IS_MF(bp)) {
11685 		u8 fid;
11686 
11687 		/* iterate over absolute function ids for this path: */
11688 		for (fid = BP_PATH(bp); fid < E2_FUNC_MAX * 2; fid += 2) {
11689 			if (IS_MF_SD(bp)) {
11690 				u32 cfg = MF_CFG_RD(bp,
11691 						    func_mf_config[fid].config);
11692 
11693 				if (!(cfg & FUNC_MF_CFG_FUNC_HIDE) &&
11694 				    ((cfg & FUNC_MF_CFG_PROTOCOL_MASK) ==
11695 					    FUNC_MF_CFG_PROTOCOL_FCOE))
11696 					count++;
11697 			} else {
11698 				u32 cfg = MF_CFG_RD(bp,
11699 						    func_ext_config[fid].
11700 								      func_cfg);
11701 
11702 				if ((cfg & MACP_FUNC_CFG_FLAGS_ENABLED) &&
11703 				    (cfg & MACP_FUNC_CFG_FLAGS_FCOE_OFFLOAD))
11704 					count++;
11705 			}
11706 		}
11707 	} else { /* SF */
11708 		int port, port_cnt = CHIP_MODE_IS_4_PORT(bp) ? 2 : 1;
11709 
11710 		for (port = 0; port < port_cnt; port++) {
11711 			u32 lic = SHMEM_RD(bp,
11712 					   drv_lic_key[port].max_fcoe_conn) ^
11713 				  FW_ENCODE_32BIT_PATTERN;
11714 			if (lic)
11715 				count++;
11716 		}
11717 	}
11718 
11719 	return count;
11720 }
11721 
11722 static void bnx2x_get_fcoe_info(struct bnx2x *bp)
11723 {
11724 	int port = BP_PORT(bp);
11725 	int func = BP_ABS_FUNC(bp);
11726 	u32 max_fcoe_conn = FW_ENCODE_32BIT_PATTERN ^ SHMEM_RD(bp,
11727 				drv_lic_key[port].max_fcoe_conn);
11728 	u8 num_fcoe_func = bnx2x_shared_fcoe_funcs(bp);
11729 
11730 	if (!CNIC_SUPPORT(bp)) {
11731 		bp->flags |= NO_FCOE_FLAG;
11732 		return;
11733 	}
11734 
11735 	/* Get the number of maximum allowed FCoE connections */
11736 	bp->cnic_eth_dev.max_fcoe_conn =
11737 		(max_fcoe_conn & BNX2X_MAX_FCOE_INIT_CONN_MASK) >>
11738 		BNX2X_MAX_FCOE_INIT_CONN_SHIFT;
11739 
11740 	/* Calculate the number of maximum allowed FCoE tasks */
11741 	bp->cnic_eth_dev.max_fcoe_exchanges = MAX_NUM_FCOE_TASKS_PER_ENGINE;
11742 
11743 	/* check if FCoE resources must be shared between different functions */
11744 	if (num_fcoe_func)
11745 		bp->cnic_eth_dev.max_fcoe_exchanges /= num_fcoe_func;
11746 
11747 	/* Read the WWN: */
11748 	if (!IS_MF(bp)) {
11749 		/* Port info */
11750 		bp->cnic_eth_dev.fcoe_wwn_port_name_hi =
11751 			SHMEM_RD(bp,
11752 				 dev_info.port_hw_config[port].
11753 				 fcoe_wwn_port_name_upper);
11754 		bp->cnic_eth_dev.fcoe_wwn_port_name_lo =
11755 			SHMEM_RD(bp,
11756 				 dev_info.port_hw_config[port].
11757 				 fcoe_wwn_port_name_lower);
11758 
11759 		/* Node info */
11760 		bp->cnic_eth_dev.fcoe_wwn_node_name_hi =
11761 			SHMEM_RD(bp,
11762 				 dev_info.port_hw_config[port].
11763 				 fcoe_wwn_node_name_upper);
11764 		bp->cnic_eth_dev.fcoe_wwn_node_name_lo =
11765 			SHMEM_RD(bp,
11766 				 dev_info.port_hw_config[port].
11767 				 fcoe_wwn_node_name_lower);
11768 	} else if (!IS_MF_SD(bp)) {
11769 		/* Read the WWN info only if the FCoE feature is enabled for
11770 		 * this function.
11771 		 */
11772 		if (BNX2X_HAS_MF_EXT_PROTOCOL_FCOE(bp))
11773 			bnx2x_get_ext_wwn_info(bp, func);
11774 	} else {
11775 		if (BNX2X_IS_MF_SD_PROTOCOL_FCOE(bp) && !CHIP_IS_E1x(bp))
11776 			bnx2x_get_ext_wwn_info(bp, func);
11777 	}
11778 
11779 	BNX2X_DEV_INFO("max_fcoe_conn 0x%x\n", bp->cnic_eth_dev.max_fcoe_conn);
11780 
11781 	/*
11782 	 * If maximum allowed number of connections is zero -
11783 	 * disable the feature.
11784 	 */
11785 	if (!bp->cnic_eth_dev.max_fcoe_conn) {
11786 		bp->flags |= NO_FCOE_FLAG;
11787 		eth_zero_addr(bp->fip_mac);
11788 	}
11789 }
11790 
11791 static void bnx2x_get_cnic_info(struct bnx2x *bp)
11792 {
11793 	/*
11794 	 * iSCSI may be dynamically disabled but reading
11795 	 * info here we will decrease memory usage by driver
11796 	 * if the feature is disabled for good
11797 	 */
11798 	bnx2x_get_iscsi_info(bp);
11799 	bnx2x_get_fcoe_info(bp);
11800 }
11801 
11802 static void bnx2x_get_cnic_mac_hwinfo(struct bnx2x *bp)
11803 {
11804 	u32 val, val2;
11805 	int func = BP_ABS_FUNC(bp);
11806 	int port = BP_PORT(bp);
11807 	u8 *iscsi_mac = bp->cnic_eth_dev.iscsi_mac;
11808 	u8 *fip_mac = bp->fip_mac;
11809 
11810 	if (IS_MF(bp)) {
11811 		/* iSCSI and FCoE NPAR MACs: if there is no either iSCSI or
11812 		 * FCoE MAC then the appropriate feature should be disabled.
11813 		 * In non SD mode features configuration comes from struct
11814 		 * func_ext_config.
11815 		 */
11816 		if (!IS_MF_SD(bp)) {
11817 			u32 cfg = MF_CFG_RD(bp, func_ext_config[func].func_cfg);
11818 			if (cfg & MACP_FUNC_CFG_FLAGS_ISCSI_OFFLOAD) {
11819 				val2 = MF_CFG_RD(bp, func_ext_config[func].
11820 						 iscsi_mac_addr_upper);
11821 				val = MF_CFG_RD(bp, func_ext_config[func].
11822 						iscsi_mac_addr_lower);
11823 				bnx2x_set_mac_buf(iscsi_mac, val, val2);
11824 				BNX2X_DEV_INFO
11825 					("Read iSCSI MAC: %pM\n", iscsi_mac);
11826 			} else {
11827 				bp->flags |= NO_ISCSI_OOO_FLAG | NO_ISCSI_FLAG;
11828 			}
11829 
11830 			if (cfg & MACP_FUNC_CFG_FLAGS_FCOE_OFFLOAD) {
11831 				val2 = MF_CFG_RD(bp, func_ext_config[func].
11832 						 fcoe_mac_addr_upper);
11833 				val = MF_CFG_RD(bp, func_ext_config[func].
11834 						fcoe_mac_addr_lower);
11835 				bnx2x_set_mac_buf(fip_mac, val, val2);
11836 				BNX2X_DEV_INFO
11837 					("Read FCoE L2 MAC: %pM\n", fip_mac);
11838 			} else {
11839 				bp->flags |= NO_FCOE_FLAG;
11840 			}
11841 
11842 			bp->mf_ext_config = cfg;
11843 
11844 		} else { /* SD MODE */
11845 			if (BNX2X_IS_MF_SD_PROTOCOL_ISCSI(bp)) {
11846 				/* use primary mac as iscsi mac */
11847 				memcpy(iscsi_mac, bp->dev->dev_addr, ETH_ALEN);
11848 
11849 				BNX2X_DEV_INFO("SD ISCSI MODE\n");
11850 				BNX2X_DEV_INFO
11851 					("Read iSCSI MAC: %pM\n", iscsi_mac);
11852 			} else if (BNX2X_IS_MF_SD_PROTOCOL_FCOE(bp)) {
11853 				/* use primary mac as fip mac */
11854 				memcpy(fip_mac, bp->dev->dev_addr, ETH_ALEN);
11855 				BNX2X_DEV_INFO("SD FCoE MODE\n");
11856 				BNX2X_DEV_INFO
11857 					("Read FIP MAC: %pM\n", fip_mac);
11858 			}
11859 		}
11860 
11861 		/* If this is a storage-only interface, use SAN mac as
11862 		 * primary MAC. Notice that for SD this is already the case,
11863 		 * as the SAN mac was copied from the primary MAC.
11864 		 */
11865 		if (IS_MF_FCOE_AFEX(bp))
11866 			memcpy(bp->dev->dev_addr, fip_mac, ETH_ALEN);
11867 	} else {
11868 		val2 = SHMEM_RD(bp, dev_info.port_hw_config[port].
11869 				iscsi_mac_upper);
11870 		val = SHMEM_RD(bp, dev_info.port_hw_config[port].
11871 			       iscsi_mac_lower);
11872 		bnx2x_set_mac_buf(iscsi_mac, val, val2);
11873 
11874 		val2 = SHMEM_RD(bp, dev_info.port_hw_config[port].
11875 				fcoe_fip_mac_upper);
11876 		val = SHMEM_RD(bp, dev_info.port_hw_config[port].
11877 			       fcoe_fip_mac_lower);
11878 		bnx2x_set_mac_buf(fip_mac, val, val2);
11879 	}
11880 
11881 	/* Disable iSCSI OOO if MAC configuration is invalid. */
11882 	if (!is_valid_ether_addr(iscsi_mac)) {
11883 		bp->flags |= NO_ISCSI_OOO_FLAG | NO_ISCSI_FLAG;
11884 		eth_zero_addr(iscsi_mac);
11885 	}
11886 
11887 	/* Disable FCoE if MAC configuration is invalid. */
11888 	if (!is_valid_ether_addr(fip_mac)) {
11889 		bp->flags |= NO_FCOE_FLAG;
11890 		eth_zero_addr(bp->fip_mac);
11891 	}
11892 }
11893 
11894 static void bnx2x_get_mac_hwinfo(struct bnx2x *bp)
11895 {
11896 	u32 val, val2;
11897 	int func = BP_ABS_FUNC(bp);
11898 	int port = BP_PORT(bp);
11899 
11900 	/* Zero primary MAC configuration */
11901 	eth_zero_addr(bp->dev->dev_addr);
11902 
11903 	if (BP_NOMCP(bp)) {
11904 		BNX2X_ERROR("warning: random MAC workaround active\n");
11905 		eth_hw_addr_random(bp->dev);
11906 	} else if (IS_MF(bp)) {
11907 		val2 = MF_CFG_RD(bp, func_mf_config[func].mac_upper);
11908 		val = MF_CFG_RD(bp, func_mf_config[func].mac_lower);
11909 		if ((val2 != FUNC_MF_CFG_UPPERMAC_DEFAULT) &&
11910 		    (val != FUNC_MF_CFG_LOWERMAC_DEFAULT))
11911 			bnx2x_set_mac_buf(bp->dev->dev_addr, val, val2);
11912 
11913 		if (CNIC_SUPPORT(bp))
11914 			bnx2x_get_cnic_mac_hwinfo(bp);
11915 	} else {
11916 		/* in SF read MACs from port configuration */
11917 		val2 = SHMEM_RD(bp, dev_info.port_hw_config[port].mac_upper);
11918 		val = SHMEM_RD(bp, dev_info.port_hw_config[port].mac_lower);
11919 		bnx2x_set_mac_buf(bp->dev->dev_addr, val, val2);
11920 
11921 		if (CNIC_SUPPORT(bp))
11922 			bnx2x_get_cnic_mac_hwinfo(bp);
11923 	}
11924 
11925 	if (!BP_NOMCP(bp)) {
11926 		/* Read physical port identifier from shmem */
11927 		val2 = SHMEM_RD(bp, dev_info.port_hw_config[port].mac_upper);
11928 		val = SHMEM_RD(bp, dev_info.port_hw_config[port].mac_lower);
11929 		bnx2x_set_mac_buf(bp->phys_port_id, val, val2);
11930 		bp->flags |= HAS_PHYS_PORT_ID;
11931 	}
11932 
11933 	memcpy(bp->link_params.mac_addr, bp->dev->dev_addr, ETH_ALEN);
11934 
11935 	if (!is_valid_ether_addr(bp->dev->dev_addr))
11936 		dev_err(&bp->pdev->dev,
11937 			"bad Ethernet MAC address configuration: %pM\n"
11938 			"change it manually before bringing up the appropriate network interface\n",
11939 			bp->dev->dev_addr);
11940 }
11941 
11942 static bool bnx2x_get_dropless_info(struct bnx2x *bp)
11943 {
11944 	int tmp;
11945 	u32 cfg;
11946 
11947 	if (IS_VF(bp))
11948 		return false;
11949 
11950 	if (IS_MF(bp) && !CHIP_IS_E1x(bp)) {
11951 		/* Take function: tmp = func */
11952 		tmp = BP_ABS_FUNC(bp);
11953 		cfg = MF_CFG_RD(bp, func_ext_config[tmp].func_cfg);
11954 		cfg = !!(cfg & MACP_FUNC_CFG_PAUSE_ON_HOST_RING);
11955 	} else {
11956 		/* Take port: tmp = port */
11957 		tmp = BP_PORT(bp);
11958 		cfg = SHMEM_RD(bp,
11959 			       dev_info.port_hw_config[tmp].generic_features);
11960 		cfg = !!(cfg & PORT_HW_CFG_PAUSE_ON_HOST_RING_ENABLED);
11961 	}
11962 	return cfg;
11963 }
11964 
11965 static void validate_set_si_mode(struct bnx2x *bp)
11966 {
11967 	u8 func = BP_ABS_FUNC(bp);
11968 	u32 val;
11969 
11970 	val = MF_CFG_RD(bp, func_mf_config[func].mac_upper);
11971 
11972 	/* check for legal mac (upper bytes) */
11973 	if (val != 0xffff) {
11974 		bp->mf_mode = MULTI_FUNCTION_SI;
11975 		bp->mf_config[BP_VN(bp)] =
11976 			MF_CFG_RD(bp, func_mf_config[func].config);
11977 	} else
11978 		BNX2X_DEV_INFO("illegal MAC address for SI\n");
11979 }
11980 
11981 static int bnx2x_get_hwinfo(struct bnx2x *bp)
11982 {
11983 	int /*abs*/func = BP_ABS_FUNC(bp);
11984 	int vn;
11985 	u32 val = 0, val2 = 0;
11986 	int rc = 0;
11987 
11988 	/* Validate that chip access is feasible */
11989 	if (REG_RD(bp, MISC_REG_CHIP_NUM) == 0xffffffff) {
11990 		dev_err(&bp->pdev->dev,
11991 			"Chip read returns all Fs. Preventing probe from continuing\n");
11992 		return -EINVAL;
11993 	}
11994 
11995 	bnx2x_get_common_hwinfo(bp);
11996 
11997 	/*
11998 	 * initialize IGU parameters
11999 	 */
12000 	if (CHIP_IS_E1x(bp)) {
12001 		bp->common.int_block = INT_BLOCK_HC;
12002 
12003 		bp->igu_dsb_id = DEF_SB_IGU_ID;
12004 		bp->igu_base_sb = 0;
12005 	} else {
12006 		bp->common.int_block = INT_BLOCK_IGU;
12007 
12008 		/* do not allow device reset during IGU info processing */
12009 		bnx2x_acquire_hw_lock(bp, HW_LOCK_RESOURCE_RESET);
12010 
12011 		val = REG_RD(bp, IGU_REG_BLOCK_CONFIGURATION);
12012 
12013 		if (val & IGU_BLOCK_CONFIGURATION_REG_BACKWARD_COMP_EN) {
12014 			int tout = 5000;
12015 
12016 			BNX2X_DEV_INFO("FORCING Normal Mode\n");
12017 
12018 			val &= ~(IGU_BLOCK_CONFIGURATION_REG_BACKWARD_COMP_EN);
12019 			REG_WR(bp, IGU_REG_BLOCK_CONFIGURATION, val);
12020 			REG_WR(bp, IGU_REG_RESET_MEMORIES, 0x7f);
12021 
12022 			while (tout && REG_RD(bp, IGU_REG_RESET_MEMORIES)) {
12023 				tout--;
12024 				usleep_range(1000, 2000);
12025 			}
12026 
12027 			if (REG_RD(bp, IGU_REG_RESET_MEMORIES)) {
12028 				dev_err(&bp->pdev->dev,
12029 					"FORCING Normal Mode failed!!!\n");
12030 				bnx2x_release_hw_lock(bp,
12031 						      HW_LOCK_RESOURCE_RESET);
12032 				return -EPERM;
12033 			}
12034 		}
12035 
12036 		if (val & IGU_BLOCK_CONFIGURATION_REG_BACKWARD_COMP_EN) {
12037 			BNX2X_DEV_INFO("IGU Backward Compatible Mode\n");
12038 			bp->common.int_block |= INT_BLOCK_MODE_BW_COMP;
12039 		} else
12040 			BNX2X_DEV_INFO("IGU Normal Mode\n");
12041 
12042 		rc = bnx2x_get_igu_cam_info(bp);
12043 		bnx2x_release_hw_lock(bp, HW_LOCK_RESOURCE_RESET);
12044 		if (rc)
12045 			return rc;
12046 	}
12047 
12048 	/*
12049 	 * set base FW non-default (fast path) status block id, this value is
12050 	 * used to initialize the fw_sb_id saved on the fp/queue structure to
12051 	 * determine the id used by the FW.
12052 	 */
12053 	if (CHIP_IS_E1x(bp))
12054 		bp->base_fw_ndsb = BP_PORT(bp) * FP_SB_MAX_E1x + BP_L_ID(bp);
12055 	else /*
12056 	      * 57712 - we currently use one FW SB per IGU SB (Rx and Tx of
12057 	      * the same queue are indicated on the same IGU SB). So we prefer
12058 	      * FW and IGU SBs to be the same value.
12059 	      */
12060 		bp->base_fw_ndsb = bp->igu_base_sb;
12061 
12062 	BNX2X_DEV_INFO("igu_dsb_id %d  igu_base_sb %d  igu_sb_cnt %d\n"
12063 		       "base_fw_ndsb %d\n", bp->igu_dsb_id, bp->igu_base_sb,
12064 		       bp->igu_sb_cnt, bp->base_fw_ndsb);
12065 
12066 	/*
12067 	 * Initialize MF configuration
12068 	 */
12069 	bp->mf_ov = 0;
12070 	bp->mf_mode = 0;
12071 	bp->mf_sub_mode = 0;
12072 	vn = BP_VN(bp);
12073 
12074 	if (!CHIP_IS_E1(bp) && !BP_NOMCP(bp)) {
12075 		BNX2X_DEV_INFO("shmem2base 0x%x, size %d, mfcfg offset %d\n",
12076 			       bp->common.shmem2_base, SHMEM2_RD(bp, size),
12077 			      (u32)offsetof(struct shmem2_region, mf_cfg_addr));
12078 
12079 		if (SHMEM2_HAS(bp, mf_cfg_addr))
12080 			bp->common.mf_cfg_base = SHMEM2_RD(bp, mf_cfg_addr);
12081 		else
12082 			bp->common.mf_cfg_base = bp->common.shmem_base +
12083 				offsetof(struct shmem_region, func_mb) +
12084 				E1H_FUNC_MAX * sizeof(struct drv_func_mb);
12085 		/*
12086 		 * get mf configuration:
12087 		 * 1. Existence of MF configuration
12088 		 * 2. MAC address must be legal (check only upper bytes)
12089 		 *    for  Switch-Independent mode;
12090 		 *    OVLAN must be legal for Switch-Dependent mode
12091 		 * 3. SF_MODE configures specific MF mode
12092 		 */
12093 		if (bp->common.mf_cfg_base != SHMEM_MF_CFG_ADDR_NONE) {
12094 			/* get mf configuration */
12095 			val = SHMEM_RD(bp,
12096 				       dev_info.shared_feature_config.config);
12097 			val &= SHARED_FEAT_CFG_FORCE_SF_MODE_MASK;
12098 
12099 			switch (val) {
12100 			case SHARED_FEAT_CFG_FORCE_SF_MODE_SWITCH_INDEPT:
12101 				validate_set_si_mode(bp);
12102 				break;
12103 			case SHARED_FEAT_CFG_FORCE_SF_MODE_AFEX_MODE:
12104 				if ((!CHIP_IS_E1x(bp)) &&
12105 				    (MF_CFG_RD(bp, func_mf_config[func].
12106 					       mac_upper) != 0xffff) &&
12107 				    (SHMEM2_HAS(bp,
12108 						afex_driver_support))) {
12109 					bp->mf_mode = MULTI_FUNCTION_AFEX;
12110 					bp->mf_config[vn] = MF_CFG_RD(bp,
12111 						func_mf_config[func].config);
12112 				} else {
12113 					BNX2X_DEV_INFO("can not configure afex mode\n");
12114 				}
12115 				break;
12116 			case SHARED_FEAT_CFG_FORCE_SF_MODE_MF_ALLOWED:
12117 				/* get OV configuration */
12118 				val = MF_CFG_RD(bp,
12119 					func_mf_config[FUNC_0].e1hov_tag);
12120 				val &= FUNC_MF_CFG_E1HOV_TAG_MASK;
12121 
12122 				if (val != FUNC_MF_CFG_E1HOV_TAG_DEFAULT) {
12123 					bp->mf_mode = MULTI_FUNCTION_SD;
12124 					bp->mf_config[vn] = MF_CFG_RD(bp,
12125 						func_mf_config[func].config);
12126 				} else
12127 					BNX2X_DEV_INFO("illegal OV for SD\n");
12128 				break;
12129 			case SHARED_FEAT_CFG_FORCE_SF_MODE_BD_MODE:
12130 				bp->mf_mode = MULTI_FUNCTION_SD;
12131 				bp->mf_sub_mode = SUB_MF_MODE_BD;
12132 				bp->mf_config[vn] =
12133 					MF_CFG_RD(bp,
12134 						  func_mf_config[func].config);
12135 
12136 				if (SHMEM2_HAS(bp, mtu_size)) {
12137 					int mtu_idx = BP_FW_MB_IDX(bp);
12138 					u16 mtu_size;
12139 					u32 mtu;
12140 
12141 					mtu = SHMEM2_RD(bp, mtu_size[mtu_idx]);
12142 					mtu_size = (u16)mtu;
12143 					DP(NETIF_MSG_IFUP, "Read MTU size %04x [%08x]\n",
12144 					   mtu_size, mtu);
12145 
12146 					/* if valid: update device mtu */
12147 					if ((mtu_size >= ETH_MIN_PACKET_SIZE) &&
12148 					    (mtu_size <=
12149 					     ETH_MAX_JUMBO_PACKET_SIZE))
12150 						bp->dev->mtu = mtu_size;
12151 				}
12152 				break;
12153 			case SHARED_FEAT_CFG_FORCE_SF_MODE_UFP_MODE:
12154 				bp->mf_mode = MULTI_FUNCTION_SD;
12155 				bp->mf_sub_mode = SUB_MF_MODE_UFP;
12156 				bp->mf_config[vn] =
12157 					MF_CFG_RD(bp,
12158 						  func_mf_config[func].config);
12159 				break;
12160 			case SHARED_FEAT_CFG_FORCE_SF_MODE_FORCED_SF:
12161 				bp->mf_config[vn] = 0;
12162 				break;
12163 			case SHARED_FEAT_CFG_FORCE_SF_MODE_EXTENDED_MODE:
12164 				val2 = SHMEM_RD(bp,
12165 					dev_info.shared_hw_config.config_3);
12166 				val2 &= SHARED_HW_CFG_EXTENDED_MF_MODE_MASK;
12167 				switch (val2) {
12168 				case SHARED_HW_CFG_EXTENDED_MF_MODE_NPAR1_DOT_5:
12169 					validate_set_si_mode(bp);
12170 					bp->mf_sub_mode =
12171 							SUB_MF_MODE_NPAR1_DOT_5;
12172 					break;
12173 				default:
12174 					/* Unknown configuration */
12175 					bp->mf_config[vn] = 0;
12176 					BNX2X_DEV_INFO("unknown extended MF mode 0x%x\n",
12177 						       val);
12178 				}
12179 				break;
12180 			default:
12181 				/* Unknown configuration: reset mf_config */
12182 				bp->mf_config[vn] = 0;
12183 				BNX2X_DEV_INFO("unknown MF mode 0x%x\n", val);
12184 			}
12185 		}
12186 
12187 		BNX2X_DEV_INFO("%s function mode\n",
12188 			       IS_MF(bp) ? "multi" : "single");
12189 
12190 		switch (bp->mf_mode) {
12191 		case MULTI_FUNCTION_SD:
12192 			val = MF_CFG_RD(bp, func_mf_config[func].e1hov_tag) &
12193 			      FUNC_MF_CFG_E1HOV_TAG_MASK;
12194 			if (val != FUNC_MF_CFG_E1HOV_TAG_DEFAULT) {
12195 				bp->mf_ov = val;
12196 				bp->path_has_ovlan = true;
12197 
12198 				BNX2X_DEV_INFO("MF OV for func %d is %d (0x%04x)\n",
12199 					       func, bp->mf_ov, bp->mf_ov);
12200 			} else if ((bp->mf_sub_mode == SUB_MF_MODE_UFP) ||
12201 				   (bp->mf_sub_mode == SUB_MF_MODE_BD)) {
12202 				dev_err(&bp->pdev->dev,
12203 					"Unexpected - no valid MF OV for func %d in UFP/BD mode\n",
12204 					func);
12205 				bp->path_has_ovlan = true;
12206 			} else {
12207 				dev_err(&bp->pdev->dev,
12208 					"No valid MF OV for func %d, aborting\n",
12209 					func);
12210 				return -EPERM;
12211 			}
12212 			break;
12213 		case MULTI_FUNCTION_AFEX:
12214 			BNX2X_DEV_INFO("func %d is in MF afex mode\n", func);
12215 			break;
12216 		case MULTI_FUNCTION_SI:
12217 			BNX2X_DEV_INFO("func %d is in MF switch-independent mode\n",
12218 				       func);
12219 			break;
12220 		default:
12221 			if (vn) {
12222 				dev_err(&bp->pdev->dev,
12223 					"VN %d is in a single function mode, aborting\n",
12224 					vn);
12225 				return -EPERM;
12226 			}
12227 			break;
12228 		}
12229 
12230 		/* check if other port on the path needs ovlan:
12231 		 * Since MF configuration is shared between ports
12232 		 * Possible mixed modes are only
12233 		 * {SF, SI} {SF, SD} {SD, SF} {SI, SF}
12234 		 */
12235 		if (CHIP_MODE_IS_4_PORT(bp) &&
12236 		    !bp->path_has_ovlan &&
12237 		    !IS_MF(bp) &&
12238 		    bp->common.mf_cfg_base != SHMEM_MF_CFG_ADDR_NONE) {
12239 			u8 other_port = !BP_PORT(bp);
12240 			u8 other_func = BP_PATH(bp) + 2*other_port;
12241 			val = MF_CFG_RD(bp,
12242 					func_mf_config[other_func].e1hov_tag);
12243 			if (val != FUNC_MF_CFG_E1HOV_TAG_DEFAULT)
12244 				bp->path_has_ovlan = true;
12245 		}
12246 	}
12247 
12248 	/* adjust igu_sb_cnt to MF for E1H */
12249 	if (CHIP_IS_E1H(bp) && IS_MF(bp))
12250 		bp->igu_sb_cnt = min_t(u8, bp->igu_sb_cnt, E1H_MAX_MF_SB_COUNT);
12251 
12252 	/* port info */
12253 	bnx2x_get_port_hwinfo(bp);
12254 
12255 	/* Get MAC addresses */
12256 	bnx2x_get_mac_hwinfo(bp);
12257 
12258 	bnx2x_get_cnic_info(bp);
12259 
12260 	return rc;
12261 }
12262 
12263 static void bnx2x_read_fwinfo(struct bnx2x *bp)
12264 {
12265 	int cnt, i, block_end, rodi;
12266 	char vpd_start[BNX2X_VPD_LEN+1];
12267 	char str_id_reg[VENDOR_ID_LEN+1];
12268 	char str_id_cap[VENDOR_ID_LEN+1];
12269 	char *vpd_data;
12270 	char *vpd_extended_data = NULL;
12271 	u8 len;
12272 
12273 	cnt = pci_read_vpd(bp->pdev, 0, BNX2X_VPD_LEN, vpd_start);
12274 	memset(bp->fw_ver, 0, sizeof(bp->fw_ver));
12275 
12276 	if (cnt < BNX2X_VPD_LEN)
12277 		goto out_not_found;
12278 
12279 	/* VPD RO tag should be first tag after identifier string, hence
12280 	 * we should be able to find it in first BNX2X_VPD_LEN chars
12281 	 */
12282 	i = pci_vpd_find_tag(vpd_start, 0, BNX2X_VPD_LEN,
12283 			     PCI_VPD_LRDT_RO_DATA);
12284 	if (i < 0)
12285 		goto out_not_found;
12286 
12287 	block_end = i + PCI_VPD_LRDT_TAG_SIZE +
12288 		    pci_vpd_lrdt_size(&vpd_start[i]);
12289 
12290 	i += PCI_VPD_LRDT_TAG_SIZE;
12291 
12292 	if (block_end > BNX2X_VPD_LEN) {
12293 		vpd_extended_data = kmalloc(block_end, GFP_KERNEL);
12294 		if (vpd_extended_data  == NULL)
12295 			goto out_not_found;
12296 
12297 		/* read rest of vpd image into vpd_extended_data */
12298 		memcpy(vpd_extended_data, vpd_start, BNX2X_VPD_LEN);
12299 		cnt = pci_read_vpd(bp->pdev, BNX2X_VPD_LEN,
12300 				   block_end - BNX2X_VPD_LEN,
12301 				   vpd_extended_data + BNX2X_VPD_LEN);
12302 		if (cnt < (block_end - BNX2X_VPD_LEN))
12303 			goto out_not_found;
12304 		vpd_data = vpd_extended_data;
12305 	} else
12306 		vpd_data = vpd_start;
12307 
12308 	/* now vpd_data holds full vpd content in both cases */
12309 
12310 	rodi = pci_vpd_find_info_keyword(vpd_data, i, block_end,
12311 				   PCI_VPD_RO_KEYWORD_MFR_ID);
12312 	if (rodi < 0)
12313 		goto out_not_found;
12314 
12315 	len = pci_vpd_info_field_size(&vpd_data[rodi]);
12316 
12317 	if (len != VENDOR_ID_LEN)
12318 		goto out_not_found;
12319 
12320 	rodi += PCI_VPD_INFO_FLD_HDR_SIZE;
12321 
12322 	/* vendor specific info */
12323 	snprintf(str_id_reg, VENDOR_ID_LEN + 1, "%04x", PCI_VENDOR_ID_DELL);
12324 	snprintf(str_id_cap, VENDOR_ID_LEN + 1, "%04X", PCI_VENDOR_ID_DELL);
12325 	if (!strncmp(str_id_reg, &vpd_data[rodi], VENDOR_ID_LEN) ||
12326 	    !strncmp(str_id_cap, &vpd_data[rodi], VENDOR_ID_LEN)) {
12327 
12328 		rodi = pci_vpd_find_info_keyword(vpd_data, i, block_end,
12329 						PCI_VPD_RO_KEYWORD_VENDOR0);
12330 		if (rodi >= 0) {
12331 			len = pci_vpd_info_field_size(&vpd_data[rodi]);
12332 
12333 			rodi += PCI_VPD_INFO_FLD_HDR_SIZE;
12334 
12335 			if (len < 32 && (len + rodi) <= BNX2X_VPD_LEN) {
12336 				memcpy(bp->fw_ver, &vpd_data[rodi], len);
12337 				bp->fw_ver[len] = ' ';
12338 			}
12339 		}
12340 		kfree(vpd_extended_data);
12341 		return;
12342 	}
12343 out_not_found:
12344 	kfree(vpd_extended_data);
12345 	return;
12346 }
12347 
12348 static void bnx2x_set_modes_bitmap(struct bnx2x *bp)
12349 {
12350 	u32 flags = 0;
12351 
12352 	if (CHIP_REV_IS_FPGA(bp))
12353 		SET_FLAGS(flags, MODE_FPGA);
12354 	else if (CHIP_REV_IS_EMUL(bp))
12355 		SET_FLAGS(flags, MODE_EMUL);
12356 	else
12357 		SET_FLAGS(flags, MODE_ASIC);
12358 
12359 	if (CHIP_MODE_IS_4_PORT(bp))
12360 		SET_FLAGS(flags, MODE_PORT4);
12361 	else
12362 		SET_FLAGS(flags, MODE_PORT2);
12363 
12364 	if (CHIP_IS_E2(bp))
12365 		SET_FLAGS(flags, MODE_E2);
12366 	else if (CHIP_IS_E3(bp)) {
12367 		SET_FLAGS(flags, MODE_E3);
12368 		if (CHIP_REV(bp) == CHIP_REV_Ax)
12369 			SET_FLAGS(flags, MODE_E3_A0);
12370 		else /*if (CHIP_REV(bp) == CHIP_REV_Bx)*/
12371 			SET_FLAGS(flags, MODE_E3_B0 | MODE_COS3);
12372 	}
12373 
12374 	if (IS_MF(bp)) {
12375 		SET_FLAGS(flags, MODE_MF);
12376 		switch (bp->mf_mode) {
12377 		case MULTI_FUNCTION_SD:
12378 			SET_FLAGS(flags, MODE_MF_SD);
12379 			break;
12380 		case MULTI_FUNCTION_SI:
12381 			SET_FLAGS(flags, MODE_MF_SI);
12382 			break;
12383 		case MULTI_FUNCTION_AFEX:
12384 			SET_FLAGS(flags, MODE_MF_AFEX);
12385 			break;
12386 		}
12387 	} else
12388 		SET_FLAGS(flags, MODE_SF);
12389 
12390 #if defined(__LITTLE_ENDIAN)
12391 	SET_FLAGS(flags, MODE_LITTLE_ENDIAN);
12392 #else /*(__BIG_ENDIAN)*/
12393 	SET_FLAGS(flags, MODE_BIG_ENDIAN);
12394 #endif
12395 	INIT_MODE_FLAGS(bp) = flags;
12396 }
12397 
12398 static int bnx2x_init_bp(struct bnx2x *bp)
12399 {
12400 	int func;
12401 	int rc;
12402 
12403 	mutex_init(&bp->port.phy_mutex);
12404 	mutex_init(&bp->fw_mb_mutex);
12405 	mutex_init(&bp->drv_info_mutex);
12406 	sema_init(&bp->stats_lock, 1);
12407 	bp->drv_info_mng_owner = false;
12408 	INIT_LIST_HEAD(&bp->vlan_reg);
12409 
12410 	INIT_DELAYED_WORK(&bp->sp_task, bnx2x_sp_task);
12411 	INIT_DELAYED_WORK(&bp->sp_rtnl_task, bnx2x_sp_rtnl_task);
12412 	INIT_DELAYED_WORK(&bp->period_task, bnx2x_period_task);
12413 	INIT_DELAYED_WORK(&bp->iov_task, bnx2x_iov_task);
12414 	if (IS_PF(bp)) {
12415 		rc = bnx2x_get_hwinfo(bp);
12416 		if (rc)
12417 			return rc;
12418 	} else {
12419 		eth_zero_addr(bp->dev->dev_addr);
12420 	}
12421 
12422 	bnx2x_set_modes_bitmap(bp);
12423 
12424 	rc = bnx2x_alloc_mem_bp(bp);
12425 	if (rc)
12426 		return rc;
12427 
12428 	bnx2x_read_fwinfo(bp);
12429 
12430 	func = BP_FUNC(bp);
12431 
12432 	/* need to reset chip if undi was active */
12433 	if (IS_PF(bp) && !BP_NOMCP(bp)) {
12434 		/* init fw_seq */
12435 		bp->fw_seq =
12436 			SHMEM_RD(bp, func_mb[BP_FW_MB_IDX(bp)].drv_mb_header) &
12437 							DRV_MSG_SEQ_NUMBER_MASK;
12438 		BNX2X_DEV_INFO("fw_seq 0x%08x\n", bp->fw_seq);
12439 
12440 		rc = bnx2x_prev_unload(bp);
12441 		if (rc) {
12442 			bnx2x_free_mem_bp(bp);
12443 			return rc;
12444 		}
12445 	}
12446 
12447 	if (CHIP_REV_IS_FPGA(bp))
12448 		dev_err(&bp->pdev->dev, "FPGA detected\n");
12449 
12450 	if (BP_NOMCP(bp) && (func == 0))
12451 		dev_err(&bp->pdev->dev, "MCP disabled, must load devices in order!\n");
12452 
12453 	bp->disable_tpa = disable_tpa;
12454 	bp->disable_tpa |= !!IS_MF_STORAGE_ONLY(bp);
12455 	/* Reduce memory usage in kdump environment by disabling TPA */
12456 	bp->disable_tpa |= is_kdump_kernel();
12457 
12458 	/* Set TPA flags */
12459 	if (bp->disable_tpa) {
12460 		bp->dev->hw_features &= ~(NETIF_F_LRO | NETIF_F_GRO_HW);
12461 		bp->dev->features &= ~(NETIF_F_LRO | NETIF_F_GRO_HW);
12462 	}
12463 
12464 	if (CHIP_IS_E1(bp))
12465 		bp->dropless_fc = 0;
12466 	else
12467 		bp->dropless_fc = dropless_fc | bnx2x_get_dropless_info(bp);
12468 
12469 	bp->mrrs = mrrs;
12470 
12471 	bp->tx_ring_size = IS_MF_STORAGE_ONLY(bp) ? 0 : MAX_TX_AVAIL;
12472 	if (IS_VF(bp))
12473 		bp->rx_ring_size = MAX_RX_AVAIL;
12474 
12475 	/* make sure that the numbers are in the right granularity */
12476 	bp->tx_ticks = (50 / BNX2X_BTR) * BNX2X_BTR;
12477 	bp->rx_ticks = (25 / BNX2X_BTR) * BNX2X_BTR;
12478 
12479 	bp->current_interval = CHIP_REV_IS_SLOW(bp) ? 5*HZ : HZ;
12480 
12481 	timer_setup(&bp->timer, bnx2x_timer, 0);
12482 	bp->timer.expires = jiffies + bp->current_interval;
12483 
12484 	if (SHMEM2_HAS(bp, dcbx_lldp_params_offset) &&
12485 	    SHMEM2_HAS(bp, dcbx_lldp_dcbx_stat_offset) &&
12486 	    SHMEM2_HAS(bp, dcbx_en) &&
12487 	    SHMEM2_RD(bp, dcbx_lldp_params_offset) &&
12488 	    SHMEM2_RD(bp, dcbx_lldp_dcbx_stat_offset) &&
12489 	    SHMEM2_RD(bp, dcbx_en[BP_PORT(bp)])) {
12490 		bnx2x_dcbx_set_state(bp, true, BNX2X_DCBX_ENABLED_ON_NEG_ON);
12491 		bnx2x_dcbx_init_params(bp);
12492 	} else {
12493 		bnx2x_dcbx_set_state(bp, false, BNX2X_DCBX_ENABLED_OFF);
12494 	}
12495 
12496 	if (CHIP_IS_E1x(bp))
12497 		bp->cnic_base_cl_id = FP_SB_MAX_E1x;
12498 	else
12499 		bp->cnic_base_cl_id = FP_SB_MAX_E2;
12500 
12501 	/* multiple tx priority */
12502 	if (IS_VF(bp))
12503 		bp->max_cos = 1;
12504 	else if (CHIP_IS_E1x(bp))
12505 		bp->max_cos = BNX2X_MULTI_TX_COS_E1X;
12506 	else if (CHIP_IS_E2(bp) || CHIP_IS_E3A0(bp))
12507 		bp->max_cos = BNX2X_MULTI_TX_COS_E2_E3A0;
12508 	else if (CHIP_IS_E3B0(bp))
12509 		bp->max_cos = BNX2X_MULTI_TX_COS_E3B0;
12510 	else
12511 		BNX2X_ERR("unknown chip %x revision %x\n",
12512 			  CHIP_NUM(bp), CHIP_REV(bp));
12513 	BNX2X_DEV_INFO("set bp->max_cos to %d\n", bp->max_cos);
12514 
12515 	/* We need at least one default status block for slow-path events,
12516 	 * second status block for the L2 queue, and a third status block for
12517 	 * CNIC if supported.
12518 	 */
12519 	if (IS_VF(bp))
12520 		bp->min_msix_vec_cnt = 1;
12521 	else if (CNIC_SUPPORT(bp))
12522 		bp->min_msix_vec_cnt = 3;
12523 	else /* PF w/o cnic */
12524 		bp->min_msix_vec_cnt = 2;
12525 	BNX2X_DEV_INFO("bp->min_msix_vec_cnt %d", bp->min_msix_vec_cnt);
12526 
12527 	bp->dump_preset_idx = 1;
12528 
12529 	return rc;
12530 }
12531 
12532 /****************************************************************************
12533 * General service functions
12534 ****************************************************************************/
12535 
12536 /*
12537  * net_device service functions
12538  */
12539 
12540 /* called with rtnl_lock */
12541 static int bnx2x_open(struct net_device *dev)
12542 {
12543 	struct bnx2x *bp = netdev_priv(dev);
12544 	int rc;
12545 
12546 	bp->stats_init = true;
12547 
12548 	netif_carrier_off(dev);
12549 
12550 	bnx2x_set_power_state(bp, PCI_D0);
12551 
12552 	/* If parity had happen during the unload, then attentions
12553 	 * and/or RECOVERY_IN_PROGRES may still be set. In this case we
12554 	 * want the first function loaded on the current engine to
12555 	 * complete the recovery.
12556 	 * Parity recovery is only relevant for PF driver.
12557 	 */
12558 	if (IS_PF(bp)) {
12559 		int other_engine = BP_PATH(bp) ? 0 : 1;
12560 		bool other_load_status, load_status;
12561 		bool global = false;
12562 
12563 		other_load_status = bnx2x_get_load_status(bp, other_engine);
12564 		load_status = bnx2x_get_load_status(bp, BP_PATH(bp));
12565 		if (!bnx2x_reset_is_done(bp, BP_PATH(bp)) ||
12566 		    bnx2x_chk_parity_attn(bp, &global, true)) {
12567 			do {
12568 				/* If there are attentions and they are in a
12569 				 * global blocks, set the GLOBAL_RESET bit
12570 				 * regardless whether it will be this function
12571 				 * that will complete the recovery or not.
12572 				 */
12573 				if (global)
12574 					bnx2x_set_reset_global(bp);
12575 
12576 				/* Only the first function on the current
12577 				 * engine should try to recover in open. In case
12578 				 * of attentions in global blocks only the first
12579 				 * in the chip should try to recover.
12580 				 */
12581 				if ((!load_status &&
12582 				     (!global || !other_load_status)) &&
12583 				      bnx2x_trylock_leader_lock(bp) &&
12584 				      !bnx2x_leader_reset(bp)) {
12585 					netdev_info(bp->dev,
12586 						    "Recovered in open\n");
12587 					break;
12588 				}
12589 
12590 				/* recovery has failed... */
12591 				bnx2x_set_power_state(bp, PCI_D3hot);
12592 				bp->recovery_state = BNX2X_RECOVERY_FAILED;
12593 
12594 				BNX2X_ERR("Recovery flow hasn't been properly completed yet. Try again later.\n"
12595 					  "If you still see this message after a few retries then power cycle is required.\n");
12596 
12597 				return -EAGAIN;
12598 			} while (0);
12599 		}
12600 	}
12601 
12602 	bp->recovery_state = BNX2X_RECOVERY_DONE;
12603 	rc = bnx2x_nic_load(bp, LOAD_OPEN);
12604 	if (rc)
12605 		return rc;
12606 
12607 	if (IS_PF(bp))
12608 		udp_tunnel_get_rx_info(dev);
12609 
12610 	return 0;
12611 }
12612 
12613 /* called with rtnl_lock */
12614 static int bnx2x_close(struct net_device *dev)
12615 {
12616 	struct bnx2x *bp = netdev_priv(dev);
12617 
12618 	/* Unload the driver, release IRQs */
12619 	bnx2x_nic_unload(bp, UNLOAD_CLOSE, false);
12620 
12621 	return 0;
12622 }
12623 
12624 struct bnx2x_mcast_list_elem_group
12625 {
12626 	struct list_head mcast_group_link;
12627 	struct bnx2x_mcast_list_elem mcast_elems[];
12628 };
12629 
12630 #define MCAST_ELEMS_PER_PG \
12631 	((PAGE_SIZE - sizeof(struct bnx2x_mcast_list_elem_group)) / \
12632 	sizeof(struct bnx2x_mcast_list_elem))
12633 
12634 static void bnx2x_free_mcast_macs_list(struct list_head *mcast_group_list)
12635 {
12636 	struct bnx2x_mcast_list_elem_group *current_mcast_group;
12637 
12638 	while (!list_empty(mcast_group_list)) {
12639 		current_mcast_group = list_first_entry(mcast_group_list,
12640 				      struct bnx2x_mcast_list_elem_group,
12641 				      mcast_group_link);
12642 		list_del(&current_mcast_group->mcast_group_link);
12643 		free_page((unsigned long)current_mcast_group);
12644 	}
12645 }
12646 
12647 static int bnx2x_init_mcast_macs_list(struct bnx2x *bp,
12648 				      struct bnx2x_mcast_ramrod_params *p,
12649 				      struct list_head *mcast_group_list)
12650 {
12651 	struct bnx2x_mcast_list_elem *mc_mac;
12652 	struct netdev_hw_addr *ha;
12653 	struct bnx2x_mcast_list_elem_group *current_mcast_group = NULL;
12654 	int mc_count = netdev_mc_count(bp->dev);
12655 	int offset = 0;
12656 
12657 	INIT_LIST_HEAD(&p->mcast_list);
12658 	netdev_for_each_mc_addr(ha, bp->dev) {
12659 		if (!offset) {
12660 			current_mcast_group =
12661 				(struct bnx2x_mcast_list_elem_group *)
12662 				__get_free_page(GFP_ATOMIC);
12663 			if (!current_mcast_group) {
12664 				bnx2x_free_mcast_macs_list(mcast_group_list);
12665 				BNX2X_ERR("Failed to allocate mc MAC list\n");
12666 				return -ENOMEM;
12667 			}
12668 			list_add(&current_mcast_group->mcast_group_link,
12669 				 mcast_group_list);
12670 		}
12671 		mc_mac = &current_mcast_group->mcast_elems[offset];
12672 		mc_mac->mac = bnx2x_mc_addr(ha);
12673 		list_add_tail(&mc_mac->link, &p->mcast_list);
12674 		offset++;
12675 		if (offset == MCAST_ELEMS_PER_PG)
12676 			offset = 0;
12677 	}
12678 	p->mcast_list_len = mc_count;
12679 	return 0;
12680 }
12681 
12682 /**
12683  * bnx2x_set_uc_list - configure a new unicast MACs list.
12684  *
12685  * @bp: driver handle
12686  *
12687  * We will use zero (0) as a MAC type for these MACs.
12688  */
12689 static int bnx2x_set_uc_list(struct bnx2x *bp)
12690 {
12691 	int rc;
12692 	struct net_device *dev = bp->dev;
12693 	struct netdev_hw_addr *ha;
12694 	struct bnx2x_vlan_mac_obj *mac_obj = &bp->sp_objs->mac_obj;
12695 	unsigned long ramrod_flags = 0;
12696 
12697 	/* First schedule a cleanup up of old configuration */
12698 	rc = bnx2x_del_all_macs(bp, mac_obj, BNX2X_UC_LIST_MAC, false);
12699 	if (rc < 0) {
12700 		BNX2X_ERR("Failed to schedule DELETE operations: %d\n", rc);
12701 		return rc;
12702 	}
12703 
12704 	netdev_for_each_uc_addr(ha, dev) {
12705 		rc = bnx2x_set_mac_one(bp, bnx2x_uc_addr(ha), mac_obj, true,
12706 				       BNX2X_UC_LIST_MAC, &ramrod_flags);
12707 		if (rc == -EEXIST) {
12708 			DP(BNX2X_MSG_SP,
12709 			   "Failed to schedule ADD operations: %d\n", rc);
12710 			/* do not treat adding same MAC as error */
12711 			rc = 0;
12712 
12713 		} else if (rc < 0) {
12714 
12715 			BNX2X_ERR("Failed to schedule ADD operations: %d\n",
12716 				  rc);
12717 			return rc;
12718 		}
12719 	}
12720 
12721 	/* Execute the pending commands */
12722 	__set_bit(RAMROD_CONT, &ramrod_flags);
12723 	return bnx2x_set_mac_one(bp, NULL, mac_obj, false /* don't care */,
12724 				 BNX2X_UC_LIST_MAC, &ramrod_flags);
12725 }
12726 
12727 static int bnx2x_set_mc_list_e1x(struct bnx2x *bp)
12728 {
12729 	LIST_HEAD(mcast_group_list);
12730 	struct net_device *dev = bp->dev;
12731 	struct bnx2x_mcast_ramrod_params rparam = {NULL};
12732 	int rc = 0;
12733 
12734 	rparam.mcast_obj = &bp->mcast_obj;
12735 
12736 	/* first, clear all configured multicast MACs */
12737 	rc = bnx2x_config_mcast(bp, &rparam, BNX2X_MCAST_CMD_DEL);
12738 	if (rc < 0) {
12739 		BNX2X_ERR("Failed to clear multicast configuration: %d\n", rc);
12740 		return rc;
12741 	}
12742 
12743 	/* then, configure a new MACs list */
12744 	if (netdev_mc_count(dev)) {
12745 		rc = bnx2x_init_mcast_macs_list(bp, &rparam, &mcast_group_list);
12746 		if (rc)
12747 			return rc;
12748 
12749 		/* Now add the new MACs */
12750 		rc = bnx2x_config_mcast(bp, &rparam,
12751 					BNX2X_MCAST_CMD_ADD);
12752 		if (rc < 0)
12753 			BNX2X_ERR("Failed to set a new multicast configuration: %d\n",
12754 				  rc);
12755 
12756 		bnx2x_free_mcast_macs_list(&mcast_group_list);
12757 	}
12758 
12759 	return rc;
12760 }
12761 
12762 static int bnx2x_set_mc_list(struct bnx2x *bp)
12763 {
12764 	LIST_HEAD(mcast_group_list);
12765 	struct bnx2x_mcast_ramrod_params rparam = {NULL};
12766 	struct net_device *dev = bp->dev;
12767 	int rc = 0;
12768 
12769 	/* On older adapters, we need to flush and re-add filters */
12770 	if (CHIP_IS_E1x(bp))
12771 		return bnx2x_set_mc_list_e1x(bp);
12772 
12773 	rparam.mcast_obj = &bp->mcast_obj;
12774 
12775 	if (netdev_mc_count(dev)) {
12776 		rc = bnx2x_init_mcast_macs_list(bp, &rparam, &mcast_group_list);
12777 		if (rc)
12778 			return rc;
12779 
12780 		/* Override the curently configured set of mc filters */
12781 		rc = bnx2x_config_mcast(bp, &rparam,
12782 					BNX2X_MCAST_CMD_SET);
12783 		if (rc < 0)
12784 			BNX2X_ERR("Failed to set a new multicast configuration: %d\n",
12785 				  rc);
12786 
12787 		bnx2x_free_mcast_macs_list(&mcast_group_list);
12788 	} else {
12789 		/* If no mc addresses are required, flush the configuration */
12790 		rc = bnx2x_config_mcast(bp, &rparam, BNX2X_MCAST_CMD_DEL);
12791 		if (rc < 0)
12792 			BNX2X_ERR("Failed to clear multicast configuration %d\n",
12793 				  rc);
12794 	}
12795 
12796 	return rc;
12797 }
12798 
12799 /* If bp->state is OPEN, should be called with netif_addr_lock_bh() */
12800 static void bnx2x_set_rx_mode(struct net_device *dev)
12801 {
12802 	struct bnx2x *bp = netdev_priv(dev);
12803 
12804 	if (bp->state != BNX2X_STATE_OPEN) {
12805 		DP(NETIF_MSG_IFUP, "state is %x, returning\n", bp->state);
12806 		return;
12807 	} else {
12808 		/* Schedule an SP task to handle rest of change */
12809 		bnx2x_schedule_sp_rtnl(bp, BNX2X_SP_RTNL_RX_MODE,
12810 				       NETIF_MSG_IFUP);
12811 	}
12812 }
12813 
12814 void bnx2x_set_rx_mode_inner(struct bnx2x *bp)
12815 {
12816 	u32 rx_mode = BNX2X_RX_MODE_NORMAL;
12817 
12818 	DP(NETIF_MSG_IFUP, "dev->flags = %x\n", bp->dev->flags);
12819 
12820 	netif_addr_lock_bh(bp->dev);
12821 
12822 	if (bp->dev->flags & IFF_PROMISC) {
12823 		rx_mode = BNX2X_RX_MODE_PROMISC;
12824 	} else if ((bp->dev->flags & IFF_ALLMULTI) ||
12825 		   ((netdev_mc_count(bp->dev) > BNX2X_MAX_MULTICAST) &&
12826 		    CHIP_IS_E1(bp))) {
12827 		rx_mode = BNX2X_RX_MODE_ALLMULTI;
12828 	} else {
12829 		if (IS_PF(bp)) {
12830 			/* some multicasts */
12831 			if (bnx2x_set_mc_list(bp) < 0)
12832 				rx_mode = BNX2X_RX_MODE_ALLMULTI;
12833 
12834 			/* release bh lock, as bnx2x_set_uc_list might sleep */
12835 			netif_addr_unlock_bh(bp->dev);
12836 			if (bnx2x_set_uc_list(bp) < 0)
12837 				rx_mode = BNX2X_RX_MODE_PROMISC;
12838 			netif_addr_lock_bh(bp->dev);
12839 		} else {
12840 			/* configuring mcast to a vf involves sleeping (when we
12841 			 * wait for the pf's response).
12842 			 */
12843 			bnx2x_schedule_sp_rtnl(bp,
12844 					       BNX2X_SP_RTNL_VFPF_MCAST, 0);
12845 		}
12846 	}
12847 
12848 	bp->rx_mode = rx_mode;
12849 	/* handle ISCSI SD mode */
12850 	if (IS_MF_ISCSI_ONLY(bp))
12851 		bp->rx_mode = BNX2X_RX_MODE_NONE;
12852 
12853 	/* Schedule the rx_mode command */
12854 	if (test_bit(BNX2X_FILTER_RX_MODE_PENDING, &bp->sp_state)) {
12855 		set_bit(BNX2X_FILTER_RX_MODE_SCHED, &bp->sp_state);
12856 		netif_addr_unlock_bh(bp->dev);
12857 		return;
12858 	}
12859 
12860 	if (IS_PF(bp)) {
12861 		bnx2x_set_storm_rx_mode(bp);
12862 		netif_addr_unlock_bh(bp->dev);
12863 	} else {
12864 		/* VF will need to request the PF to make this change, and so
12865 		 * the VF needs to release the bottom-half lock prior to the
12866 		 * request (as it will likely require sleep on the VF side)
12867 		 */
12868 		netif_addr_unlock_bh(bp->dev);
12869 		bnx2x_vfpf_storm_rx_mode(bp);
12870 	}
12871 }
12872 
12873 /* called with rtnl_lock */
12874 static int bnx2x_mdio_read(struct net_device *netdev, int prtad,
12875 			   int devad, u16 addr)
12876 {
12877 	struct bnx2x *bp = netdev_priv(netdev);
12878 	u16 value;
12879 	int rc;
12880 
12881 	DP(NETIF_MSG_LINK, "mdio_read: prtad 0x%x, devad 0x%x, addr 0x%x\n",
12882 	   prtad, devad, addr);
12883 
12884 	/* The HW expects different devad if CL22 is used */
12885 	devad = (devad == MDIO_DEVAD_NONE) ? DEFAULT_PHY_DEV_ADDR : devad;
12886 
12887 	bnx2x_acquire_phy_lock(bp);
12888 	rc = bnx2x_phy_read(&bp->link_params, prtad, devad, addr, &value);
12889 	bnx2x_release_phy_lock(bp);
12890 	DP(NETIF_MSG_LINK, "mdio_read_val 0x%x rc = 0x%x\n", value, rc);
12891 
12892 	if (!rc)
12893 		rc = value;
12894 	return rc;
12895 }
12896 
12897 /* called with rtnl_lock */
12898 static int bnx2x_mdio_write(struct net_device *netdev, int prtad, int devad,
12899 			    u16 addr, u16 value)
12900 {
12901 	struct bnx2x *bp = netdev_priv(netdev);
12902 	int rc;
12903 
12904 	DP(NETIF_MSG_LINK,
12905 	   "mdio_write: prtad 0x%x, devad 0x%x, addr 0x%x, value 0x%x\n",
12906 	   prtad, devad, addr, value);
12907 
12908 	/* The HW expects different devad if CL22 is used */
12909 	devad = (devad == MDIO_DEVAD_NONE) ? DEFAULT_PHY_DEV_ADDR : devad;
12910 
12911 	bnx2x_acquire_phy_lock(bp);
12912 	rc = bnx2x_phy_write(&bp->link_params, prtad, devad, addr, value);
12913 	bnx2x_release_phy_lock(bp);
12914 	return rc;
12915 }
12916 
12917 /* called with rtnl_lock */
12918 static int bnx2x_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
12919 {
12920 	struct bnx2x *bp = netdev_priv(dev);
12921 	struct mii_ioctl_data *mdio = if_mii(ifr);
12922 
12923 	if (!netif_running(dev))
12924 		return -EAGAIN;
12925 
12926 	switch (cmd) {
12927 	case SIOCSHWTSTAMP:
12928 		return bnx2x_hwtstamp_ioctl(bp, ifr);
12929 	default:
12930 		DP(NETIF_MSG_LINK, "ioctl: phy id 0x%x, reg 0x%x, val_in 0x%x\n",
12931 		   mdio->phy_id, mdio->reg_num, mdio->val_in);
12932 		return mdio_mii_ioctl(&bp->mdio, mdio, cmd);
12933 	}
12934 }
12935 
12936 static int bnx2x_validate_addr(struct net_device *dev)
12937 {
12938 	struct bnx2x *bp = netdev_priv(dev);
12939 
12940 	/* query the bulletin board for mac address configured by the PF */
12941 	if (IS_VF(bp))
12942 		bnx2x_sample_bulletin(bp);
12943 
12944 	if (!is_valid_ether_addr(dev->dev_addr)) {
12945 		BNX2X_ERR("Non-valid Ethernet address\n");
12946 		return -EADDRNOTAVAIL;
12947 	}
12948 	return 0;
12949 }
12950 
12951 static int bnx2x_get_phys_port_id(struct net_device *netdev,
12952 				  struct netdev_phys_item_id *ppid)
12953 {
12954 	struct bnx2x *bp = netdev_priv(netdev);
12955 
12956 	if (!(bp->flags & HAS_PHYS_PORT_ID))
12957 		return -EOPNOTSUPP;
12958 
12959 	ppid->id_len = sizeof(bp->phys_port_id);
12960 	memcpy(ppid->id, bp->phys_port_id, ppid->id_len);
12961 
12962 	return 0;
12963 }
12964 
12965 static netdev_features_t bnx2x_features_check(struct sk_buff *skb,
12966 					      struct net_device *dev,
12967 					      netdev_features_t features)
12968 {
12969 	/*
12970 	 * A skb with gso_size + header length > 9700 will cause a
12971 	 * firmware panic. Drop GSO support.
12972 	 *
12973 	 * Eventually the upper layer should not pass these packets down.
12974 	 *
12975 	 * For speed, if the gso_size is <= 9000, assume there will
12976 	 * not be 700 bytes of headers and pass it through. Only do a
12977 	 * full (slow) validation if the gso_size is > 9000.
12978 	 *
12979 	 * (Due to the way SKB_BY_FRAGS works this will also do a full
12980 	 * validation in that case.)
12981 	 */
12982 	if (unlikely(skb_is_gso(skb) &&
12983 		     (skb_shinfo(skb)->gso_size > 9000) &&
12984 		     !skb_gso_validate_mac_len(skb, 9700)))
12985 		features &= ~NETIF_F_GSO_MASK;
12986 
12987 	features = vlan_features_check(skb, features);
12988 	return vxlan_features_check(skb, features);
12989 }
12990 
12991 static int __bnx2x_vlan_configure_vid(struct bnx2x *bp, u16 vid, bool add)
12992 {
12993 	int rc;
12994 
12995 	if (IS_PF(bp)) {
12996 		unsigned long ramrod_flags = 0;
12997 
12998 		__set_bit(RAMROD_COMP_WAIT, &ramrod_flags);
12999 		rc = bnx2x_set_vlan_one(bp, vid, &bp->sp_objs->vlan_obj,
13000 					add, &ramrod_flags);
13001 	} else {
13002 		rc = bnx2x_vfpf_update_vlan(bp, vid, bp->fp->index, add);
13003 	}
13004 
13005 	return rc;
13006 }
13007 
13008 static int bnx2x_vlan_configure_vid_list(struct bnx2x *bp)
13009 {
13010 	struct bnx2x_vlan_entry *vlan;
13011 	int rc = 0;
13012 
13013 	/* Configure all non-configured entries */
13014 	list_for_each_entry(vlan, &bp->vlan_reg, link) {
13015 		if (vlan->hw)
13016 			continue;
13017 
13018 		if (bp->vlan_cnt >= bp->vlan_credit)
13019 			return -ENOBUFS;
13020 
13021 		rc = __bnx2x_vlan_configure_vid(bp, vlan->vid, true);
13022 		if (rc) {
13023 			BNX2X_ERR("Unable to config VLAN %d\n", vlan->vid);
13024 			return rc;
13025 		}
13026 
13027 		DP(NETIF_MSG_IFUP, "HW configured for VLAN %d\n", vlan->vid);
13028 		vlan->hw = true;
13029 		bp->vlan_cnt++;
13030 	}
13031 
13032 	return 0;
13033 }
13034 
13035 static void bnx2x_vlan_configure(struct bnx2x *bp, bool set_rx_mode)
13036 {
13037 	bool need_accept_any_vlan;
13038 
13039 	need_accept_any_vlan = !!bnx2x_vlan_configure_vid_list(bp);
13040 
13041 	if (bp->accept_any_vlan != need_accept_any_vlan) {
13042 		bp->accept_any_vlan = need_accept_any_vlan;
13043 		DP(NETIF_MSG_IFUP, "Accept all VLAN %s\n",
13044 		   bp->accept_any_vlan ? "raised" : "cleared");
13045 		if (set_rx_mode) {
13046 			if (IS_PF(bp))
13047 				bnx2x_set_rx_mode_inner(bp);
13048 			else
13049 				bnx2x_vfpf_storm_rx_mode(bp);
13050 		}
13051 	}
13052 }
13053 
13054 int bnx2x_vlan_reconfigure_vid(struct bnx2x *bp)
13055 {
13056 	/* Don't set rx mode here. Our caller will do it. */
13057 	bnx2x_vlan_configure(bp, false);
13058 
13059 	return 0;
13060 }
13061 
13062 static int bnx2x_vlan_rx_add_vid(struct net_device *dev, __be16 proto, u16 vid)
13063 {
13064 	struct bnx2x *bp = netdev_priv(dev);
13065 	struct bnx2x_vlan_entry *vlan;
13066 
13067 	DP(NETIF_MSG_IFUP, "Adding VLAN %d\n", vid);
13068 
13069 	vlan = kmalloc(sizeof(*vlan), GFP_KERNEL);
13070 	if (!vlan)
13071 		return -ENOMEM;
13072 
13073 	vlan->vid = vid;
13074 	vlan->hw = false;
13075 	list_add_tail(&vlan->link, &bp->vlan_reg);
13076 
13077 	if (netif_running(dev))
13078 		bnx2x_vlan_configure(bp, true);
13079 
13080 	return 0;
13081 }
13082 
13083 static int bnx2x_vlan_rx_kill_vid(struct net_device *dev, __be16 proto, u16 vid)
13084 {
13085 	struct bnx2x *bp = netdev_priv(dev);
13086 	struct bnx2x_vlan_entry *vlan;
13087 	bool found = false;
13088 	int rc = 0;
13089 
13090 	DP(NETIF_MSG_IFUP, "Removing VLAN %d\n", vid);
13091 
13092 	list_for_each_entry(vlan, &bp->vlan_reg, link)
13093 		if (vlan->vid == vid) {
13094 			found = true;
13095 			break;
13096 		}
13097 
13098 	if (!found) {
13099 		BNX2X_ERR("Unable to kill VLAN %d - not found\n", vid);
13100 		return -EINVAL;
13101 	}
13102 
13103 	if (netif_running(dev) && vlan->hw) {
13104 		rc = __bnx2x_vlan_configure_vid(bp, vid, false);
13105 		DP(NETIF_MSG_IFUP, "HW deconfigured for VLAN %d\n", vid);
13106 		bp->vlan_cnt--;
13107 	}
13108 
13109 	list_del(&vlan->link);
13110 	kfree(vlan);
13111 
13112 	if (netif_running(dev))
13113 		bnx2x_vlan_configure(bp, true);
13114 
13115 	DP(NETIF_MSG_IFUP, "Removing VLAN result %d\n", rc);
13116 
13117 	return rc;
13118 }
13119 
13120 static const struct net_device_ops bnx2x_netdev_ops = {
13121 	.ndo_open		= bnx2x_open,
13122 	.ndo_stop		= bnx2x_close,
13123 	.ndo_start_xmit		= bnx2x_start_xmit,
13124 	.ndo_select_queue	= bnx2x_select_queue,
13125 	.ndo_set_rx_mode	= bnx2x_set_rx_mode,
13126 	.ndo_set_mac_address	= bnx2x_change_mac_addr,
13127 	.ndo_validate_addr	= bnx2x_validate_addr,
13128 	.ndo_do_ioctl		= bnx2x_ioctl,
13129 	.ndo_change_mtu		= bnx2x_change_mtu,
13130 	.ndo_fix_features	= bnx2x_fix_features,
13131 	.ndo_set_features	= bnx2x_set_features,
13132 	.ndo_tx_timeout		= bnx2x_tx_timeout,
13133 	.ndo_vlan_rx_add_vid	= bnx2x_vlan_rx_add_vid,
13134 	.ndo_vlan_rx_kill_vid	= bnx2x_vlan_rx_kill_vid,
13135 	.ndo_setup_tc		= __bnx2x_setup_tc,
13136 #ifdef CONFIG_BNX2X_SRIOV
13137 	.ndo_set_vf_mac		= bnx2x_set_vf_mac,
13138 	.ndo_set_vf_vlan	= bnx2x_set_vf_vlan,
13139 	.ndo_get_vf_config	= bnx2x_get_vf_config,
13140 	.ndo_set_vf_spoofchk	= bnx2x_set_vf_spoofchk,
13141 #endif
13142 #ifdef NETDEV_FCOE_WWNN
13143 	.ndo_fcoe_get_wwn	= bnx2x_fcoe_get_wwn,
13144 #endif
13145 
13146 	.ndo_get_phys_port_id	= bnx2x_get_phys_port_id,
13147 	.ndo_set_vf_link_state	= bnx2x_set_vf_link_state,
13148 	.ndo_features_check	= bnx2x_features_check,
13149 	.ndo_udp_tunnel_add	= bnx2x_udp_tunnel_add,
13150 	.ndo_udp_tunnel_del	= bnx2x_udp_tunnel_del,
13151 };
13152 
13153 static int bnx2x_set_coherency_mask(struct bnx2x *bp)
13154 {
13155 	struct device *dev = &bp->pdev->dev;
13156 
13157 	if (dma_set_mask_and_coherent(dev, DMA_BIT_MASK(64)) != 0 &&
13158 	    dma_set_mask_and_coherent(dev, DMA_BIT_MASK(32)) != 0) {
13159 		dev_err(dev, "System does not support DMA, aborting\n");
13160 		return -EIO;
13161 	}
13162 
13163 	return 0;
13164 }
13165 
13166 static void bnx2x_disable_pcie_error_reporting(struct bnx2x *bp)
13167 {
13168 	if (bp->flags & AER_ENABLED) {
13169 		pci_disable_pcie_error_reporting(bp->pdev);
13170 		bp->flags &= ~AER_ENABLED;
13171 	}
13172 }
13173 
13174 static int bnx2x_init_dev(struct bnx2x *bp, struct pci_dev *pdev,
13175 			  struct net_device *dev, unsigned long board_type)
13176 {
13177 	int rc;
13178 	u32 pci_cfg_dword;
13179 	bool chip_is_e1x = (board_type == BCM57710 ||
13180 			    board_type == BCM57711 ||
13181 			    board_type == BCM57711E);
13182 
13183 	SET_NETDEV_DEV(dev, &pdev->dev);
13184 
13185 	bp->dev = dev;
13186 	bp->pdev = pdev;
13187 
13188 	rc = pci_enable_device(pdev);
13189 	if (rc) {
13190 		dev_err(&bp->pdev->dev,
13191 			"Cannot enable PCI device, aborting\n");
13192 		goto err_out;
13193 	}
13194 
13195 	if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM)) {
13196 		dev_err(&bp->pdev->dev,
13197 			"Cannot find PCI device base address, aborting\n");
13198 		rc = -ENODEV;
13199 		goto err_out_disable;
13200 	}
13201 
13202 	if (IS_PF(bp) && !(pci_resource_flags(pdev, 2) & IORESOURCE_MEM)) {
13203 		dev_err(&bp->pdev->dev, "Cannot find second PCI device base address, aborting\n");
13204 		rc = -ENODEV;
13205 		goto err_out_disable;
13206 	}
13207 
13208 	pci_read_config_dword(pdev, PCICFG_REVISION_ID_OFFSET, &pci_cfg_dword);
13209 	if ((pci_cfg_dword & PCICFG_REVESION_ID_MASK) ==
13210 	    PCICFG_REVESION_ID_ERROR_VAL) {
13211 		pr_err("PCI device error, probably due to fan failure, aborting\n");
13212 		rc = -ENODEV;
13213 		goto err_out_disable;
13214 	}
13215 
13216 	if (atomic_read(&pdev->enable_cnt) == 1) {
13217 		rc = pci_request_regions(pdev, DRV_MODULE_NAME);
13218 		if (rc) {
13219 			dev_err(&bp->pdev->dev,
13220 				"Cannot obtain PCI resources, aborting\n");
13221 			goto err_out_disable;
13222 		}
13223 
13224 		pci_set_master(pdev);
13225 		pci_save_state(pdev);
13226 	}
13227 
13228 	if (IS_PF(bp)) {
13229 		if (!pdev->pm_cap) {
13230 			dev_err(&bp->pdev->dev,
13231 				"Cannot find power management capability, aborting\n");
13232 			rc = -EIO;
13233 			goto err_out_release;
13234 		}
13235 	}
13236 
13237 	if (!pci_is_pcie(pdev)) {
13238 		dev_err(&bp->pdev->dev, "Not PCI Express, aborting\n");
13239 		rc = -EIO;
13240 		goto err_out_release;
13241 	}
13242 
13243 	rc = bnx2x_set_coherency_mask(bp);
13244 	if (rc)
13245 		goto err_out_release;
13246 
13247 	dev->mem_start = pci_resource_start(pdev, 0);
13248 	dev->base_addr = dev->mem_start;
13249 	dev->mem_end = pci_resource_end(pdev, 0);
13250 
13251 	dev->irq = pdev->irq;
13252 
13253 	bp->regview = pci_ioremap_bar(pdev, 0);
13254 	if (!bp->regview) {
13255 		dev_err(&bp->pdev->dev,
13256 			"Cannot map register space, aborting\n");
13257 		rc = -ENOMEM;
13258 		goto err_out_release;
13259 	}
13260 
13261 	/* In E1/E1H use pci device function given by kernel.
13262 	 * In E2/E3 read physical function from ME register since these chips
13263 	 * support Physical Device Assignment where kernel BDF maybe arbitrary
13264 	 * (depending on hypervisor).
13265 	 */
13266 	if (chip_is_e1x) {
13267 		bp->pf_num = PCI_FUNC(pdev->devfn);
13268 	} else {
13269 		/* chip is E2/3*/
13270 		pci_read_config_dword(bp->pdev,
13271 				      PCICFG_ME_REGISTER, &pci_cfg_dword);
13272 		bp->pf_num = (u8)((pci_cfg_dword & ME_REG_ABS_PF_NUM) >>
13273 				  ME_REG_ABS_PF_NUM_SHIFT);
13274 	}
13275 	BNX2X_DEV_INFO("me reg PF num: %d\n", bp->pf_num);
13276 
13277 	/* clean indirect addresses */
13278 	pci_write_config_dword(bp->pdev, PCICFG_GRC_ADDRESS,
13279 			       PCICFG_VENDOR_ID_OFFSET);
13280 
13281 	/* Set PCIe reset type to fundamental for EEH recovery */
13282 	pdev->needs_freset = 1;
13283 
13284 	/* AER (Advanced Error reporting) configuration */
13285 	rc = pci_enable_pcie_error_reporting(pdev);
13286 	if (!rc)
13287 		bp->flags |= AER_ENABLED;
13288 	else
13289 		BNX2X_DEV_INFO("Failed To configure PCIe AER [%d]\n", rc);
13290 
13291 	/*
13292 	 * Clean the following indirect addresses for all functions since it
13293 	 * is not used by the driver.
13294 	 */
13295 	if (IS_PF(bp)) {
13296 		REG_WR(bp, PXP2_REG_PGL_ADDR_88_F0, 0);
13297 		REG_WR(bp, PXP2_REG_PGL_ADDR_8C_F0, 0);
13298 		REG_WR(bp, PXP2_REG_PGL_ADDR_90_F0, 0);
13299 		REG_WR(bp, PXP2_REG_PGL_ADDR_94_F0, 0);
13300 
13301 		if (chip_is_e1x) {
13302 			REG_WR(bp, PXP2_REG_PGL_ADDR_88_F1, 0);
13303 			REG_WR(bp, PXP2_REG_PGL_ADDR_8C_F1, 0);
13304 			REG_WR(bp, PXP2_REG_PGL_ADDR_90_F1, 0);
13305 			REG_WR(bp, PXP2_REG_PGL_ADDR_94_F1, 0);
13306 		}
13307 
13308 		/* Enable internal target-read (in case we are probed after PF
13309 		 * FLR). Must be done prior to any BAR read access. Only for
13310 		 * 57712 and up
13311 		 */
13312 		if (!chip_is_e1x)
13313 			REG_WR(bp,
13314 			       PGLUE_B_REG_INTERNAL_PFID_ENABLE_TARGET_READ, 1);
13315 	}
13316 
13317 	dev->watchdog_timeo = TX_TIMEOUT;
13318 
13319 	dev->netdev_ops = &bnx2x_netdev_ops;
13320 	bnx2x_set_ethtool_ops(bp, dev);
13321 
13322 	dev->priv_flags |= IFF_UNICAST_FLT;
13323 
13324 	dev->hw_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM |
13325 		NETIF_F_TSO | NETIF_F_TSO_ECN | NETIF_F_TSO6 |
13326 		NETIF_F_RXCSUM | NETIF_F_LRO | NETIF_F_GRO | NETIF_F_GRO_HW |
13327 		NETIF_F_RXHASH | NETIF_F_HW_VLAN_CTAG_TX;
13328 	if (!chip_is_e1x) {
13329 		dev->hw_features |= NETIF_F_GSO_GRE | NETIF_F_GSO_GRE_CSUM |
13330 				    NETIF_F_GSO_IPXIP4 |
13331 				    NETIF_F_GSO_UDP_TUNNEL |
13332 				    NETIF_F_GSO_UDP_TUNNEL_CSUM |
13333 				    NETIF_F_GSO_PARTIAL;
13334 
13335 		dev->hw_enc_features =
13336 			NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM | NETIF_F_SG |
13337 			NETIF_F_TSO | NETIF_F_TSO_ECN | NETIF_F_TSO6 |
13338 			NETIF_F_GSO_IPXIP4 |
13339 			NETIF_F_GSO_GRE | NETIF_F_GSO_GRE_CSUM |
13340 			NETIF_F_GSO_UDP_TUNNEL | NETIF_F_GSO_UDP_TUNNEL_CSUM |
13341 			NETIF_F_GSO_PARTIAL;
13342 
13343 		dev->gso_partial_features = NETIF_F_GSO_GRE_CSUM |
13344 					    NETIF_F_GSO_UDP_TUNNEL_CSUM;
13345 	}
13346 
13347 	dev->vlan_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM |
13348 		NETIF_F_TSO | NETIF_F_TSO_ECN | NETIF_F_TSO6 | NETIF_F_HIGHDMA;
13349 
13350 	if (IS_PF(bp)) {
13351 		if (chip_is_e1x)
13352 			bp->accept_any_vlan = true;
13353 		else
13354 			dev->hw_features |= NETIF_F_HW_VLAN_CTAG_FILTER;
13355 	}
13356 	/* For VF we'll know whether to enable VLAN filtering after
13357 	 * getting a response to CHANNEL_TLV_ACQUIRE from PF.
13358 	 */
13359 
13360 	dev->features |= dev->hw_features | NETIF_F_HW_VLAN_CTAG_RX;
13361 	dev->features |= NETIF_F_HIGHDMA;
13362 	if (dev->features & NETIF_F_LRO)
13363 		dev->features &= ~NETIF_F_GRO_HW;
13364 
13365 	/* Add Loopback capability to the device */
13366 	dev->hw_features |= NETIF_F_LOOPBACK;
13367 
13368 #ifdef BCM_DCBNL
13369 	dev->dcbnl_ops = &bnx2x_dcbnl_ops;
13370 #endif
13371 
13372 	/* MTU range, 46 - 9600 */
13373 	dev->min_mtu = ETH_MIN_PACKET_SIZE;
13374 	dev->max_mtu = ETH_MAX_JUMBO_PACKET_SIZE;
13375 
13376 	/* get_port_hwinfo() will set prtad and mmds properly */
13377 	bp->mdio.prtad = MDIO_PRTAD_NONE;
13378 	bp->mdio.mmds = 0;
13379 	bp->mdio.mode_support = MDIO_SUPPORTS_C45 | MDIO_EMULATE_C22;
13380 	bp->mdio.dev = dev;
13381 	bp->mdio.mdio_read = bnx2x_mdio_read;
13382 	bp->mdio.mdio_write = bnx2x_mdio_write;
13383 
13384 	return 0;
13385 
13386 err_out_release:
13387 	if (atomic_read(&pdev->enable_cnt) == 1)
13388 		pci_release_regions(pdev);
13389 
13390 err_out_disable:
13391 	pci_disable_device(pdev);
13392 
13393 err_out:
13394 	return rc;
13395 }
13396 
13397 static int bnx2x_check_firmware(struct bnx2x *bp)
13398 {
13399 	const struct firmware *firmware = bp->firmware;
13400 	struct bnx2x_fw_file_hdr *fw_hdr;
13401 	struct bnx2x_fw_file_section *sections;
13402 	u32 offset, len, num_ops;
13403 	__be16 *ops_offsets;
13404 	int i;
13405 	const u8 *fw_ver;
13406 
13407 	if (firmware->size < sizeof(struct bnx2x_fw_file_hdr)) {
13408 		BNX2X_ERR("Wrong FW size\n");
13409 		return -EINVAL;
13410 	}
13411 
13412 	fw_hdr = (struct bnx2x_fw_file_hdr *)firmware->data;
13413 	sections = (struct bnx2x_fw_file_section *)fw_hdr;
13414 
13415 	/* Make sure none of the offsets and sizes make us read beyond
13416 	 * the end of the firmware data */
13417 	for (i = 0; i < sizeof(*fw_hdr) / sizeof(*sections); i++) {
13418 		offset = be32_to_cpu(sections[i].offset);
13419 		len = be32_to_cpu(sections[i].len);
13420 		if (offset + len > firmware->size) {
13421 			BNX2X_ERR("Section %d length is out of bounds\n", i);
13422 			return -EINVAL;
13423 		}
13424 	}
13425 
13426 	/* Likewise for the init_ops offsets */
13427 	offset = be32_to_cpu(fw_hdr->init_ops_offsets.offset);
13428 	ops_offsets = (__force __be16 *)(firmware->data + offset);
13429 	num_ops = be32_to_cpu(fw_hdr->init_ops.len) / sizeof(struct raw_op);
13430 
13431 	for (i = 0; i < be32_to_cpu(fw_hdr->init_ops_offsets.len) / 2; i++) {
13432 		if (be16_to_cpu(ops_offsets[i]) > num_ops) {
13433 			BNX2X_ERR("Section offset %d is out of bounds\n", i);
13434 			return -EINVAL;
13435 		}
13436 	}
13437 
13438 	/* Check FW version */
13439 	offset = be32_to_cpu(fw_hdr->fw_version.offset);
13440 	fw_ver = firmware->data + offset;
13441 	if ((fw_ver[0] != BCM_5710_FW_MAJOR_VERSION) ||
13442 	    (fw_ver[1] != BCM_5710_FW_MINOR_VERSION) ||
13443 	    (fw_ver[2] != BCM_5710_FW_REVISION_VERSION) ||
13444 	    (fw_ver[3] != BCM_5710_FW_ENGINEERING_VERSION)) {
13445 		BNX2X_ERR("Bad FW version:%d.%d.%d.%d. Should be %d.%d.%d.%d\n",
13446 		       fw_ver[0], fw_ver[1], fw_ver[2], fw_ver[3],
13447 		       BCM_5710_FW_MAJOR_VERSION,
13448 		       BCM_5710_FW_MINOR_VERSION,
13449 		       BCM_5710_FW_REVISION_VERSION,
13450 		       BCM_5710_FW_ENGINEERING_VERSION);
13451 		return -EINVAL;
13452 	}
13453 
13454 	return 0;
13455 }
13456 
13457 static void be32_to_cpu_n(const u8 *_source, u8 *_target, u32 n)
13458 {
13459 	const __be32 *source = (const __be32 *)_source;
13460 	u32 *target = (u32 *)_target;
13461 	u32 i;
13462 
13463 	for (i = 0; i < n/4; i++)
13464 		target[i] = be32_to_cpu(source[i]);
13465 }
13466 
13467 /*
13468    Ops array is stored in the following format:
13469    {op(8bit), offset(24bit, big endian), data(32bit, big endian)}
13470  */
13471 static void bnx2x_prep_ops(const u8 *_source, u8 *_target, u32 n)
13472 {
13473 	const __be32 *source = (const __be32 *)_source;
13474 	struct raw_op *target = (struct raw_op *)_target;
13475 	u32 i, j, tmp;
13476 
13477 	for (i = 0, j = 0; i < n/8; i++, j += 2) {
13478 		tmp = be32_to_cpu(source[j]);
13479 		target[i].op = (tmp >> 24) & 0xff;
13480 		target[i].offset = tmp & 0xffffff;
13481 		target[i].raw_data = be32_to_cpu(source[j + 1]);
13482 	}
13483 }
13484 
13485 /* IRO array is stored in the following format:
13486  * {base(24bit), m1(16bit), m2(16bit), m3(16bit), size(16bit) }
13487  */
13488 static void bnx2x_prep_iro(const u8 *_source, u8 *_target, u32 n)
13489 {
13490 	const __be32 *source = (const __be32 *)_source;
13491 	struct iro *target = (struct iro *)_target;
13492 	u32 i, j, tmp;
13493 
13494 	for (i = 0, j = 0; i < n/sizeof(struct iro); i++) {
13495 		target[i].base = be32_to_cpu(source[j]);
13496 		j++;
13497 		tmp = be32_to_cpu(source[j]);
13498 		target[i].m1 = (tmp >> 16) & 0xffff;
13499 		target[i].m2 = tmp & 0xffff;
13500 		j++;
13501 		tmp = be32_to_cpu(source[j]);
13502 		target[i].m3 = (tmp >> 16) & 0xffff;
13503 		target[i].size = tmp & 0xffff;
13504 		j++;
13505 	}
13506 }
13507 
13508 static void be16_to_cpu_n(const u8 *_source, u8 *_target, u32 n)
13509 {
13510 	const __be16 *source = (const __be16 *)_source;
13511 	u16 *target = (u16 *)_target;
13512 	u32 i;
13513 
13514 	for (i = 0; i < n/2; i++)
13515 		target[i] = be16_to_cpu(source[i]);
13516 }
13517 
13518 #define BNX2X_ALLOC_AND_SET(arr, lbl, func)				\
13519 do {									\
13520 	u32 len = be32_to_cpu(fw_hdr->arr.len);				\
13521 	bp->arr = kmalloc(len, GFP_KERNEL);				\
13522 	if (!bp->arr)							\
13523 		goto lbl;						\
13524 	func(bp->firmware->data + be32_to_cpu(fw_hdr->arr.offset),	\
13525 	     (u8 *)bp->arr, len);					\
13526 } while (0)
13527 
13528 static int bnx2x_init_firmware(struct bnx2x *bp)
13529 {
13530 	const char *fw_file_name;
13531 	struct bnx2x_fw_file_hdr *fw_hdr;
13532 	int rc;
13533 
13534 	if (bp->firmware)
13535 		return 0;
13536 
13537 	if (CHIP_IS_E1(bp))
13538 		fw_file_name = FW_FILE_NAME_E1;
13539 	else if (CHIP_IS_E1H(bp))
13540 		fw_file_name = FW_FILE_NAME_E1H;
13541 	else if (!CHIP_IS_E1x(bp))
13542 		fw_file_name = FW_FILE_NAME_E2;
13543 	else {
13544 		BNX2X_ERR("Unsupported chip revision\n");
13545 		return -EINVAL;
13546 	}
13547 	BNX2X_DEV_INFO("Loading %s\n", fw_file_name);
13548 
13549 	rc = request_firmware(&bp->firmware, fw_file_name, &bp->pdev->dev);
13550 	if (rc) {
13551 		BNX2X_ERR("Can't load firmware file %s\n",
13552 			  fw_file_name);
13553 		goto request_firmware_exit;
13554 	}
13555 
13556 	rc = bnx2x_check_firmware(bp);
13557 	if (rc) {
13558 		BNX2X_ERR("Corrupt firmware file %s\n", fw_file_name);
13559 		goto request_firmware_exit;
13560 	}
13561 
13562 	fw_hdr = (struct bnx2x_fw_file_hdr *)bp->firmware->data;
13563 
13564 	/* Initialize the pointers to the init arrays */
13565 	/* Blob */
13566 	rc = -ENOMEM;
13567 	BNX2X_ALLOC_AND_SET(init_data, request_firmware_exit, be32_to_cpu_n);
13568 
13569 	/* Opcodes */
13570 	BNX2X_ALLOC_AND_SET(init_ops, init_ops_alloc_err, bnx2x_prep_ops);
13571 
13572 	/* Offsets */
13573 	BNX2X_ALLOC_AND_SET(init_ops_offsets, init_offsets_alloc_err,
13574 			    be16_to_cpu_n);
13575 
13576 	/* STORMs firmware */
13577 	INIT_TSEM_INT_TABLE_DATA(bp) = bp->firmware->data +
13578 			be32_to_cpu(fw_hdr->tsem_int_table_data.offset);
13579 	INIT_TSEM_PRAM_DATA(bp)      = bp->firmware->data +
13580 			be32_to_cpu(fw_hdr->tsem_pram_data.offset);
13581 	INIT_USEM_INT_TABLE_DATA(bp) = bp->firmware->data +
13582 			be32_to_cpu(fw_hdr->usem_int_table_data.offset);
13583 	INIT_USEM_PRAM_DATA(bp)      = bp->firmware->data +
13584 			be32_to_cpu(fw_hdr->usem_pram_data.offset);
13585 	INIT_XSEM_INT_TABLE_DATA(bp) = bp->firmware->data +
13586 			be32_to_cpu(fw_hdr->xsem_int_table_data.offset);
13587 	INIT_XSEM_PRAM_DATA(bp)      = bp->firmware->data +
13588 			be32_to_cpu(fw_hdr->xsem_pram_data.offset);
13589 	INIT_CSEM_INT_TABLE_DATA(bp) = bp->firmware->data +
13590 			be32_to_cpu(fw_hdr->csem_int_table_data.offset);
13591 	INIT_CSEM_PRAM_DATA(bp)      = bp->firmware->data +
13592 			be32_to_cpu(fw_hdr->csem_pram_data.offset);
13593 	/* IRO */
13594 	BNX2X_ALLOC_AND_SET(iro_arr, iro_alloc_err, bnx2x_prep_iro);
13595 
13596 	return 0;
13597 
13598 iro_alloc_err:
13599 	kfree(bp->init_ops_offsets);
13600 init_offsets_alloc_err:
13601 	kfree(bp->init_ops);
13602 init_ops_alloc_err:
13603 	kfree(bp->init_data);
13604 request_firmware_exit:
13605 	release_firmware(bp->firmware);
13606 	bp->firmware = NULL;
13607 
13608 	return rc;
13609 }
13610 
13611 static void bnx2x_release_firmware(struct bnx2x *bp)
13612 {
13613 	kfree(bp->init_ops_offsets);
13614 	kfree(bp->init_ops);
13615 	kfree(bp->init_data);
13616 	release_firmware(bp->firmware);
13617 	bp->firmware = NULL;
13618 }
13619 
13620 static struct bnx2x_func_sp_drv_ops bnx2x_func_sp_drv = {
13621 	.init_hw_cmn_chip = bnx2x_init_hw_common_chip,
13622 	.init_hw_cmn      = bnx2x_init_hw_common,
13623 	.init_hw_port     = bnx2x_init_hw_port,
13624 	.init_hw_func     = bnx2x_init_hw_func,
13625 
13626 	.reset_hw_cmn     = bnx2x_reset_common,
13627 	.reset_hw_port    = bnx2x_reset_port,
13628 	.reset_hw_func    = bnx2x_reset_func,
13629 
13630 	.gunzip_init      = bnx2x_gunzip_init,
13631 	.gunzip_end       = bnx2x_gunzip_end,
13632 
13633 	.init_fw          = bnx2x_init_firmware,
13634 	.release_fw       = bnx2x_release_firmware,
13635 };
13636 
13637 void bnx2x__init_func_obj(struct bnx2x *bp)
13638 {
13639 	/* Prepare DMAE related driver resources */
13640 	bnx2x_setup_dmae(bp);
13641 
13642 	bnx2x_init_func_obj(bp, &bp->func_obj,
13643 			    bnx2x_sp(bp, func_rdata),
13644 			    bnx2x_sp_mapping(bp, func_rdata),
13645 			    bnx2x_sp(bp, func_afex_rdata),
13646 			    bnx2x_sp_mapping(bp, func_afex_rdata),
13647 			    &bnx2x_func_sp_drv);
13648 }
13649 
13650 /* must be called after sriov-enable */
13651 static int bnx2x_set_qm_cid_count(struct bnx2x *bp)
13652 {
13653 	int cid_count = BNX2X_L2_MAX_CID(bp);
13654 
13655 	if (IS_SRIOV(bp))
13656 		cid_count += BNX2X_VF_CIDS;
13657 
13658 	if (CNIC_SUPPORT(bp))
13659 		cid_count += CNIC_CID_MAX;
13660 
13661 	return roundup(cid_count, QM_CID_ROUND);
13662 }
13663 
13664 /**
13665  * bnx2x_get_num_none_def_sbs - return the number of none default SBs
13666  *
13667  * @dev:	pci device
13668  *
13669  */
13670 static int bnx2x_get_num_non_def_sbs(struct pci_dev *pdev, int cnic_cnt)
13671 {
13672 	int index;
13673 	u16 control = 0;
13674 
13675 	/*
13676 	 * If MSI-X is not supported - return number of SBs needed to support
13677 	 * one fast path queue: one FP queue + SB for CNIC
13678 	 */
13679 	if (!pdev->msix_cap) {
13680 		dev_info(&pdev->dev, "no msix capability found\n");
13681 		return 1 + cnic_cnt;
13682 	}
13683 	dev_info(&pdev->dev, "msix capability found\n");
13684 
13685 	/*
13686 	 * The value in the PCI configuration space is the index of the last
13687 	 * entry, namely one less than the actual size of the table, which is
13688 	 * exactly what we want to return from this function: number of all SBs
13689 	 * without the default SB.
13690 	 * For VFs there is no default SB, then we return (index+1).
13691 	 */
13692 	pci_read_config_word(pdev, pdev->msix_cap + PCI_MSIX_FLAGS, &control);
13693 
13694 	index = control & PCI_MSIX_FLAGS_QSIZE;
13695 
13696 	return index;
13697 }
13698 
13699 static int set_max_cos_est(int chip_id)
13700 {
13701 	switch (chip_id) {
13702 	case BCM57710:
13703 	case BCM57711:
13704 	case BCM57711E:
13705 		return BNX2X_MULTI_TX_COS_E1X;
13706 	case BCM57712:
13707 	case BCM57712_MF:
13708 		return BNX2X_MULTI_TX_COS_E2_E3A0;
13709 	case BCM57800:
13710 	case BCM57800_MF:
13711 	case BCM57810:
13712 	case BCM57810_MF:
13713 	case BCM57840_4_10:
13714 	case BCM57840_2_20:
13715 	case BCM57840_O:
13716 	case BCM57840_MFO:
13717 	case BCM57840_MF:
13718 	case BCM57811:
13719 	case BCM57811_MF:
13720 		return BNX2X_MULTI_TX_COS_E3B0;
13721 	case BCM57712_VF:
13722 	case BCM57800_VF:
13723 	case BCM57810_VF:
13724 	case BCM57840_VF:
13725 	case BCM57811_VF:
13726 		return 1;
13727 	default:
13728 		pr_err("Unknown board_type (%d), aborting\n", chip_id);
13729 		return -ENODEV;
13730 	}
13731 }
13732 
13733 static int set_is_vf(int chip_id)
13734 {
13735 	switch (chip_id) {
13736 	case BCM57712_VF:
13737 	case BCM57800_VF:
13738 	case BCM57810_VF:
13739 	case BCM57840_VF:
13740 	case BCM57811_VF:
13741 		return true;
13742 	default:
13743 		return false;
13744 	}
13745 }
13746 
13747 /* nig_tsgen registers relative address */
13748 #define tsgen_ctrl 0x0
13749 #define tsgen_freecount 0x10
13750 #define tsgen_synctime_t0 0x20
13751 #define tsgen_offset_t0 0x28
13752 #define tsgen_drift_t0 0x30
13753 #define tsgen_synctime_t1 0x58
13754 #define tsgen_offset_t1 0x60
13755 #define tsgen_drift_t1 0x68
13756 
13757 /* FW workaround for setting drift */
13758 static int bnx2x_send_update_drift_ramrod(struct bnx2x *bp, int drift_dir,
13759 					  int best_val, int best_period)
13760 {
13761 	struct bnx2x_func_state_params func_params = {NULL};
13762 	struct bnx2x_func_set_timesync_params *set_timesync_params =
13763 		&func_params.params.set_timesync;
13764 
13765 	/* Prepare parameters for function state transitions */
13766 	__set_bit(RAMROD_COMP_WAIT, &func_params.ramrod_flags);
13767 	__set_bit(RAMROD_RETRY, &func_params.ramrod_flags);
13768 
13769 	func_params.f_obj = &bp->func_obj;
13770 	func_params.cmd = BNX2X_F_CMD_SET_TIMESYNC;
13771 
13772 	/* Function parameters */
13773 	set_timesync_params->drift_adjust_cmd = TS_DRIFT_ADJUST_SET;
13774 	set_timesync_params->offset_cmd = TS_OFFSET_KEEP;
13775 	set_timesync_params->add_sub_drift_adjust_value =
13776 		drift_dir ? TS_ADD_VALUE : TS_SUB_VALUE;
13777 	set_timesync_params->drift_adjust_value = best_val;
13778 	set_timesync_params->drift_adjust_period = best_period;
13779 
13780 	return bnx2x_func_state_change(bp, &func_params);
13781 }
13782 
13783 static int bnx2x_ptp_adjfreq(struct ptp_clock_info *ptp, s32 ppb)
13784 {
13785 	struct bnx2x *bp = container_of(ptp, struct bnx2x, ptp_clock_info);
13786 	int rc;
13787 	int drift_dir = 1;
13788 	int val, period, period1, period2, dif, dif1, dif2;
13789 	int best_dif = BNX2X_MAX_PHC_DRIFT, best_period = 0, best_val = 0;
13790 
13791 	DP(BNX2X_MSG_PTP, "PTP adjfreq called, ppb = %d\n", ppb);
13792 
13793 	if (!netif_running(bp->dev)) {
13794 		DP(BNX2X_MSG_PTP,
13795 		   "PTP adjfreq called while the interface is down\n");
13796 		return -ENETDOWN;
13797 	}
13798 
13799 	if (ppb < 0) {
13800 		ppb = -ppb;
13801 		drift_dir = 0;
13802 	}
13803 
13804 	if (ppb == 0) {
13805 		best_val = 1;
13806 		best_period = 0x1FFFFFF;
13807 	} else if (ppb >= BNX2X_MAX_PHC_DRIFT) {
13808 		best_val = 31;
13809 		best_period = 1;
13810 	} else {
13811 		/* Changed not to allow val = 8, 16, 24 as these values
13812 		 * are not supported in workaround.
13813 		 */
13814 		for (val = 0; val <= 31; val++) {
13815 			if ((val & 0x7) == 0)
13816 				continue;
13817 			period1 = val * 1000000 / ppb;
13818 			period2 = period1 + 1;
13819 			if (period1 != 0)
13820 				dif1 = ppb - (val * 1000000 / period1);
13821 			else
13822 				dif1 = BNX2X_MAX_PHC_DRIFT;
13823 			if (dif1 < 0)
13824 				dif1 = -dif1;
13825 			dif2 = ppb - (val * 1000000 / period2);
13826 			if (dif2 < 0)
13827 				dif2 = -dif2;
13828 			dif = (dif1 < dif2) ? dif1 : dif2;
13829 			period = (dif1 < dif2) ? period1 : period2;
13830 			if (dif < best_dif) {
13831 				best_dif = dif;
13832 				best_val = val;
13833 				best_period = period;
13834 			}
13835 		}
13836 	}
13837 
13838 	rc = bnx2x_send_update_drift_ramrod(bp, drift_dir, best_val,
13839 					    best_period);
13840 	if (rc) {
13841 		BNX2X_ERR("Failed to set drift\n");
13842 		return -EFAULT;
13843 	}
13844 
13845 	DP(BNX2X_MSG_PTP, "Configured val = %d, period = %d\n", best_val,
13846 	   best_period);
13847 
13848 	return 0;
13849 }
13850 
13851 static int bnx2x_ptp_adjtime(struct ptp_clock_info *ptp, s64 delta)
13852 {
13853 	struct bnx2x *bp = container_of(ptp, struct bnx2x, ptp_clock_info);
13854 
13855 	if (!netif_running(bp->dev)) {
13856 		DP(BNX2X_MSG_PTP,
13857 		   "PTP adjtime called while the interface is down\n");
13858 		return -ENETDOWN;
13859 	}
13860 
13861 	DP(BNX2X_MSG_PTP, "PTP adjtime called, delta = %llx\n", delta);
13862 
13863 	timecounter_adjtime(&bp->timecounter, delta);
13864 
13865 	return 0;
13866 }
13867 
13868 static int bnx2x_ptp_gettime(struct ptp_clock_info *ptp, struct timespec64 *ts)
13869 {
13870 	struct bnx2x *bp = container_of(ptp, struct bnx2x, ptp_clock_info);
13871 	u64 ns;
13872 
13873 	if (!netif_running(bp->dev)) {
13874 		DP(BNX2X_MSG_PTP,
13875 		   "PTP gettime called while the interface is down\n");
13876 		return -ENETDOWN;
13877 	}
13878 
13879 	ns = timecounter_read(&bp->timecounter);
13880 
13881 	DP(BNX2X_MSG_PTP, "PTP gettime called, ns = %llu\n", ns);
13882 
13883 	*ts = ns_to_timespec64(ns);
13884 
13885 	return 0;
13886 }
13887 
13888 static int bnx2x_ptp_settime(struct ptp_clock_info *ptp,
13889 			     const struct timespec64 *ts)
13890 {
13891 	struct bnx2x *bp = container_of(ptp, struct bnx2x, ptp_clock_info);
13892 	u64 ns;
13893 
13894 	if (!netif_running(bp->dev)) {
13895 		DP(BNX2X_MSG_PTP,
13896 		   "PTP settime called while the interface is down\n");
13897 		return -ENETDOWN;
13898 	}
13899 
13900 	ns = timespec64_to_ns(ts);
13901 
13902 	DP(BNX2X_MSG_PTP, "PTP settime called, ns = %llu\n", ns);
13903 
13904 	/* Re-init the timecounter */
13905 	timecounter_init(&bp->timecounter, &bp->cyclecounter, ns);
13906 
13907 	return 0;
13908 }
13909 
13910 /* Enable (or disable) ancillary features of the phc subsystem */
13911 static int bnx2x_ptp_enable(struct ptp_clock_info *ptp,
13912 			    struct ptp_clock_request *rq, int on)
13913 {
13914 	struct bnx2x *bp = container_of(ptp, struct bnx2x, ptp_clock_info);
13915 
13916 	BNX2X_ERR("PHC ancillary features are not supported\n");
13917 	return -ENOTSUPP;
13918 }
13919 
13920 void bnx2x_register_phc(struct bnx2x *bp)
13921 {
13922 	/* Fill the ptp_clock_info struct and register PTP clock*/
13923 	bp->ptp_clock_info.owner = THIS_MODULE;
13924 	snprintf(bp->ptp_clock_info.name, 16, "%s", bp->dev->name);
13925 	bp->ptp_clock_info.max_adj = BNX2X_MAX_PHC_DRIFT; /* In PPB */
13926 	bp->ptp_clock_info.n_alarm = 0;
13927 	bp->ptp_clock_info.n_ext_ts = 0;
13928 	bp->ptp_clock_info.n_per_out = 0;
13929 	bp->ptp_clock_info.pps = 0;
13930 	bp->ptp_clock_info.adjfreq = bnx2x_ptp_adjfreq;
13931 	bp->ptp_clock_info.adjtime = bnx2x_ptp_adjtime;
13932 	bp->ptp_clock_info.gettime64 = bnx2x_ptp_gettime;
13933 	bp->ptp_clock_info.settime64 = bnx2x_ptp_settime;
13934 	bp->ptp_clock_info.enable = bnx2x_ptp_enable;
13935 
13936 	bp->ptp_clock = ptp_clock_register(&bp->ptp_clock_info, &bp->pdev->dev);
13937 	if (IS_ERR(bp->ptp_clock)) {
13938 		bp->ptp_clock = NULL;
13939 		BNX2X_ERR("PTP clock registration failed\n");
13940 	}
13941 }
13942 
13943 static int bnx2x_init_one(struct pci_dev *pdev,
13944 				    const struct pci_device_id *ent)
13945 {
13946 	struct net_device *dev = NULL;
13947 	struct bnx2x *bp;
13948 	int rc, max_non_def_sbs;
13949 	int rx_count, tx_count, rss_count, doorbell_size;
13950 	int max_cos_est;
13951 	bool is_vf;
13952 	int cnic_cnt;
13953 
13954 	/* Management FW 'remembers' living interfaces. Allow it some time
13955 	 * to forget previously living interfaces, allowing a proper re-load.
13956 	 */
13957 	if (is_kdump_kernel()) {
13958 		ktime_t now = ktime_get_boottime();
13959 		ktime_t fw_ready_time = ktime_set(5, 0);
13960 
13961 		if (ktime_before(now, fw_ready_time))
13962 			msleep(ktime_ms_delta(fw_ready_time, now));
13963 	}
13964 
13965 	/* An estimated maximum supported CoS number according to the chip
13966 	 * version.
13967 	 * We will try to roughly estimate the maximum number of CoSes this chip
13968 	 * may support in order to minimize the memory allocated for Tx
13969 	 * netdev_queue's. This number will be accurately calculated during the
13970 	 * initialization of bp->max_cos based on the chip versions AND chip
13971 	 * revision in the bnx2x_init_bp().
13972 	 */
13973 	max_cos_est = set_max_cos_est(ent->driver_data);
13974 	if (max_cos_est < 0)
13975 		return max_cos_est;
13976 	is_vf = set_is_vf(ent->driver_data);
13977 	cnic_cnt = is_vf ? 0 : 1;
13978 
13979 	max_non_def_sbs = bnx2x_get_num_non_def_sbs(pdev, cnic_cnt);
13980 
13981 	/* add another SB for VF as it has no default SB */
13982 	max_non_def_sbs += is_vf ? 1 : 0;
13983 
13984 	/* Maximum number of RSS queues: one IGU SB goes to CNIC */
13985 	rss_count = max_non_def_sbs - cnic_cnt;
13986 
13987 	if (rss_count < 1)
13988 		return -EINVAL;
13989 
13990 	/* Maximum number of netdev Rx queues: RSS + FCoE L2 */
13991 	rx_count = rss_count + cnic_cnt;
13992 
13993 	/* Maximum number of netdev Tx queues:
13994 	 * Maximum TSS queues * Maximum supported number of CoS  + FCoE L2
13995 	 */
13996 	tx_count = rss_count * max_cos_est + cnic_cnt;
13997 
13998 	/* dev zeroed in init_etherdev */
13999 	dev = alloc_etherdev_mqs(sizeof(*bp), tx_count, rx_count);
14000 	if (!dev)
14001 		return -ENOMEM;
14002 
14003 	bp = netdev_priv(dev);
14004 
14005 	bp->flags = 0;
14006 	if (is_vf)
14007 		bp->flags |= IS_VF_FLAG;
14008 
14009 	bp->igu_sb_cnt = max_non_def_sbs;
14010 	bp->igu_base_addr = IS_VF(bp) ? PXP_VF_ADDR_IGU_START : BAR_IGU_INTMEM;
14011 	bp->msg_enable = debug;
14012 	bp->cnic_support = cnic_cnt;
14013 	bp->cnic_probe = bnx2x_cnic_probe;
14014 
14015 	pci_set_drvdata(pdev, dev);
14016 
14017 	rc = bnx2x_init_dev(bp, pdev, dev, ent->driver_data);
14018 	if (rc < 0) {
14019 		free_netdev(dev);
14020 		return rc;
14021 	}
14022 
14023 	BNX2X_DEV_INFO("This is a %s function\n",
14024 		       IS_PF(bp) ? "physical" : "virtual");
14025 	BNX2X_DEV_INFO("Cnic support is %s\n", CNIC_SUPPORT(bp) ? "on" : "off");
14026 	BNX2X_DEV_INFO("Max num of status blocks %d\n", max_non_def_sbs);
14027 	BNX2X_DEV_INFO("Allocated netdev with %d tx and %d rx queues\n",
14028 		       tx_count, rx_count);
14029 
14030 	rc = bnx2x_init_bp(bp);
14031 	if (rc)
14032 		goto init_one_exit;
14033 
14034 	/* Map doorbells here as we need the real value of bp->max_cos which
14035 	 * is initialized in bnx2x_init_bp() to determine the number of
14036 	 * l2 connections.
14037 	 */
14038 	if (IS_VF(bp)) {
14039 		bp->doorbells = bnx2x_vf_doorbells(bp);
14040 		rc = bnx2x_vf_pci_alloc(bp);
14041 		if (rc)
14042 			goto init_one_freemem;
14043 	} else {
14044 		doorbell_size = BNX2X_L2_MAX_CID(bp) * (1 << BNX2X_DB_SHIFT);
14045 		if (doorbell_size > pci_resource_len(pdev, 2)) {
14046 			dev_err(&bp->pdev->dev,
14047 				"Cannot map doorbells, bar size too small, aborting\n");
14048 			rc = -ENOMEM;
14049 			goto init_one_freemem;
14050 		}
14051 		bp->doorbells = ioremap(pci_resource_start(pdev, 2),
14052 						doorbell_size);
14053 	}
14054 	if (!bp->doorbells) {
14055 		dev_err(&bp->pdev->dev,
14056 			"Cannot map doorbell space, aborting\n");
14057 		rc = -ENOMEM;
14058 		goto init_one_freemem;
14059 	}
14060 
14061 	if (IS_VF(bp)) {
14062 		rc = bnx2x_vfpf_acquire(bp, tx_count, rx_count);
14063 		if (rc)
14064 			goto init_one_freemem;
14065 
14066 #ifdef CONFIG_BNX2X_SRIOV
14067 		/* VF with OLD Hypervisor or old PF do not support filtering */
14068 		if (bp->acquire_resp.pfdev_info.pf_cap & PFVF_CAP_VLAN_FILTER) {
14069 			dev->hw_features |= NETIF_F_HW_VLAN_CTAG_FILTER;
14070 			dev->features |= NETIF_F_HW_VLAN_CTAG_FILTER;
14071 		}
14072 #endif
14073 	}
14074 
14075 	/* Enable SRIOV if capability found in configuration space */
14076 	rc = bnx2x_iov_init_one(bp, int_mode, BNX2X_MAX_NUM_OF_VFS);
14077 	if (rc)
14078 		goto init_one_freemem;
14079 
14080 	/* calc qm_cid_count */
14081 	bp->qm_cid_count = bnx2x_set_qm_cid_count(bp);
14082 	BNX2X_DEV_INFO("qm_cid_count %d\n", bp->qm_cid_count);
14083 
14084 	/* disable FCOE L2 queue for E1x*/
14085 	if (CHIP_IS_E1x(bp))
14086 		bp->flags |= NO_FCOE_FLAG;
14087 
14088 	/* Set bp->num_queues for MSI-X mode*/
14089 	bnx2x_set_num_queues(bp);
14090 
14091 	/* Configure interrupt mode: try to enable MSI-X/MSI if
14092 	 * needed.
14093 	 */
14094 	rc = bnx2x_set_int_mode(bp);
14095 	if (rc) {
14096 		dev_err(&pdev->dev, "Cannot set interrupts\n");
14097 		goto init_one_freemem;
14098 	}
14099 	BNX2X_DEV_INFO("set interrupts successfully\n");
14100 
14101 	/* register the net device */
14102 	rc = register_netdev(dev);
14103 	if (rc) {
14104 		dev_err(&pdev->dev, "Cannot register net device\n");
14105 		goto init_one_freemem;
14106 	}
14107 	BNX2X_DEV_INFO("device name after netdev register %s\n", dev->name);
14108 
14109 	if (!NO_FCOE(bp)) {
14110 		/* Add storage MAC address */
14111 		rtnl_lock();
14112 		dev_addr_add(bp->dev, bp->fip_mac, NETDEV_HW_ADDR_T_SAN);
14113 		rtnl_unlock();
14114 	}
14115 	BNX2X_DEV_INFO(
14116 	       "%s (%c%d) PCI-E found at mem %lx, IRQ %d, node addr %pM\n",
14117 	       board_info[ent->driver_data].name,
14118 	       (CHIP_REV(bp) >> 12) + 'A', (CHIP_METAL(bp) >> 4),
14119 	       dev->base_addr, bp->pdev->irq, dev->dev_addr);
14120 	pcie_print_link_status(bp->pdev);
14121 
14122 	if (!IS_MF_SD_STORAGE_PERSONALITY_ONLY(bp))
14123 		bnx2x_set_os_driver_state(bp, OS_DRIVER_STATE_DISABLED);
14124 
14125 	return 0;
14126 
14127 init_one_freemem:
14128 	bnx2x_free_mem_bp(bp);
14129 
14130 init_one_exit:
14131 	bnx2x_disable_pcie_error_reporting(bp);
14132 
14133 	if (bp->regview)
14134 		iounmap(bp->regview);
14135 
14136 	if (IS_PF(bp) && bp->doorbells)
14137 		iounmap(bp->doorbells);
14138 
14139 	free_netdev(dev);
14140 
14141 	if (atomic_read(&pdev->enable_cnt) == 1)
14142 		pci_release_regions(pdev);
14143 
14144 	pci_disable_device(pdev);
14145 
14146 	return rc;
14147 }
14148 
14149 static void __bnx2x_remove(struct pci_dev *pdev,
14150 			   struct net_device *dev,
14151 			   struct bnx2x *bp,
14152 			   bool remove_netdev)
14153 {
14154 	/* Delete storage MAC address */
14155 	if (!NO_FCOE(bp)) {
14156 		rtnl_lock();
14157 		dev_addr_del(bp->dev, bp->fip_mac, NETDEV_HW_ADDR_T_SAN);
14158 		rtnl_unlock();
14159 	}
14160 
14161 #ifdef BCM_DCBNL
14162 	/* Delete app tlvs from dcbnl */
14163 	bnx2x_dcbnl_update_applist(bp, true);
14164 #endif
14165 
14166 	if (IS_PF(bp) &&
14167 	    !BP_NOMCP(bp) &&
14168 	    (bp->flags & BC_SUPPORTS_RMMOD_CMD))
14169 		bnx2x_fw_command(bp, DRV_MSG_CODE_RMMOD, 0);
14170 
14171 	/* Close the interface - either directly or implicitly */
14172 	if (remove_netdev) {
14173 		unregister_netdev(dev);
14174 	} else {
14175 		rtnl_lock();
14176 		dev_close(dev);
14177 		rtnl_unlock();
14178 	}
14179 
14180 	bnx2x_iov_remove_one(bp);
14181 
14182 	/* Power on: we can't let PCI layer write to us while we are in D3 */
14183 	if (IS_PF(bp)) {
14184 		bnx2x_set_power_state(bp, PCI_D0);
14185 		bnx2x_set_os_driver_state(bp, OS_DRIVER_STATE_NOT_LOADED);
14186 
14187 		/* Set endianity registers to reset values in case next driver
14188 		 * boots in different endianty environment.
14189 		 */
14190 		bnx2x_reset_endianity(bp);
14191 	}
14192 
14193 	/* Disable MSI/MSI-X */
14194 	bnx2x_disable_msi(bp);
14195 
14196 	/* Power off */
14197 	if (IS_PF(bp))
14198 		bnx2x_set_power_state(bp, PCI_D3hot);
14199 
14200 	/* Make sure RESET task is not scheduled before continuing */
14201 	cancel_delayed_work_sync(&bp->sp_rtnl_task);
14202 
14203 	/* send message via vfpf channel to release the resources of this vf */
14204 	if (IS_VF(bp))
14205 		bnx2x_vfpf_release(bp);
14206 
14207 	/* Assumes no further PCIe PM changes will occur */
14208 	if (system_state == SYSTEM_POWER_OFF) {
14209 		pci_wake_from_d3(pdev, bp->wol);
14210 		pci_set_power_state(pdev, PCI_D3hot);
14211 	}
14212 
14213 	bnx2x_disable_pcie_error_reporting(bp);
14214 	if (remove_netdev) {
14215 		if (bp->regview)
14216 			iounmap(bp->regview);
14217 
14218 		/* For vfs, doorbells are part of the regview and were unmapped
14219 		 * along with it. FW is only loaded by PF.
14220 		 */
14221 		if (IS_PF(bp)) {
14222 			if (bp->doorbells)
14223 				iounmap(bp->doorbells);
14224 
14225 			bnx2x_release_firmware(bp);
14226 		} else {
14227 			bnx2x_vf_pci_dealloc(bp);
14228 		}
14229 		bnx2x_free_mem_bp(bp);
14230 
14231 		free_netdev(dev);
14232 
14233 		if (atomic_read(&pdev->enable_cnt) == 1)
14234 			pci_release_regions(pdev);
14235 
14236 		pci_disable_device(pdev);
14237 	}
14238 }
14239 
14240 static void bnx2x_remove_one(struct pci_dev *pdev)
14241 {
14242 	struct net_device *dev = pci_get_drvdata(pdev);
14243 	struct bnx2x *bp;
14244 
14245 	if (!dev) {
14246 		dev_err(&pdev->dev, "BAD net device from bnx2x_init_one\n");
14247 		return;
14248 	}
14249 	bp = netdev_priv(dev);
14250 
14251 	__bnx2x_remove(pdev, dev, bp, true);
14252 }
14253 
14254 static int bnx2x_eeh_nic_unload(struct bnx2x *bp)
14255 {
14256 	bp->state = BNX2X_STATE_CLOSING_WAIT4_HALT;
14257 
14258 	bp->rx_mode = BNX2X_RX_MODE_NONE;
14259 
14260 	if (CNIC_LOADED(bp))
14261 		bnx2x_cnic_notify(bp, CNIC_CTL_STOP_CMD);
14262 
14263 	/* Stop Tx */
14264 	bnx2x_tx_disable(bp);
14265 	/* Delete all NAPI objects */
14266 	bnx2x_del_all_napi(bp);
14267 	if (CNIC_LOADED(bp))
14268 		bnx2x_del_all_napi_cnic(bp);
14269 	netdev_reset_tc(bp->dev);
14270 
14271 	del_timer_sync(&bp->timer);
14272 	cancel_delayed_work_sync(&bp->sp_task);
14273 	cancel_delayed_work_sync(&bp->period_task);
14274 
14275 	if (!down_timeout(&bp->stats_lock, HZ / 10)) {
14276 		bp->stats_state = STATS_STATE_DISABLED;
14277 		up(&bp->stats_lock);
14278 	}
14279 
14280 	bnx2x_save_statistics(bp);
14281 
14282 	netif_carrier_off(bp->dev);
14283 
14284 	return 0;
14285 }
14286 
14287 /**
14288  * bnx2x_io_error_detected - called when PCI error is detected
14289  * @pdev: Pointer to PCI device
14290  * @state: The current pci connection state
14291  *
14292  * This function is called after a PCI bus error affecting
14293  * this device has been detected.
14294  */
14295 static pci_ers_result_t bnx2x_io_error_detected(struct pci_dev *pdev,
14296 						pci_channel_state_t state)
14297 {
14298 	struct net_device *dev = pci_get_drvdata(pdev);
14299 	struct bnx2x *bp = netdev_priv(dev);
14300 
14301 	rtnl_lock();
14302 
14303 	BNX2X_ERR("IO error detected\n");
14304 
14305 	netif_device_detach(dev);
14306 
14307 	if (state == pci_channel_io_perm_failure) {
14308 		rtnl_unlock();
14309 		return PCI_ERS_RESULT_DISCONNECT;
14310 	}
14311 
14312 	if (netif_running(dev))
14313 		bnx2x_eeh_nic_unload(bp);
14314 
14315 	bnx2x_prev_path_mark_eeh(bp);
14316 
14317 	pci_disable_device(pdev);
14318 
14319 	rtnl_unlock();
14320 
14321 	/* Request a slot reset */
14322 	return PCI_ERS_RESULT_NEED_RESET;
14323 }
14324 
14325 /**
14326  * bnx2x_io_slot_reset - called after the PCI bus has been reset
14327  * @pdev: Pointer to PCI device
14328  *
14329  * Restart the card from scratch, as if from a cold-boot.
14330  */
14331 static pci_ers_result_t bnx2x_io_slot_reset(struct pci_dev *pdev)
14332 {
14333 	struct net_device *dev = pci_get_drvdata(pdev);
14334 	struct bnx2x *bp = netdev_priv(dev);
14335 	int i;
14336 
14337 	rtnl_lock();
14338 	BNX2X_ERR("IO slot reset initializing...\n");
14339 	if (pci_enable_device(pdev)) {
14340 		dev_err(&pdev->dev,
14341 			"Cannot re-enable PCI device after reset\n");
14342 		rtnl_unlock();
14343 		return PCI_ERS_RESULT_DISCONNECT;
14344 	}
14345 
14346 	pci_set_master(pdev);
14347 	pci_restore_state(pdev);
14348 	pci_save_state(pdev);
14349 
14350 	if (netif_running(dev))
14351 		bnx2x_set_power_state(bp, PCI_D0);
14352 
14353 	if (netif_running(dev)) {
14354 		BNX2X_ERR("IO slot reset --> driver unload\n");
14355 
14356 		/* MCP should have been reset; Need to wait for validity */
14357 		if (bnx2x_init_shmem(bp)) {
14358 			rtnl_unlock();
14359 			return PCI_ERS_RESULT_DISCONNECT;
14360 		}
14361 
14362 		if (IS_PF(bp) && SHMEM2_HAS(bp, drv_capabilities_flag)) {
14363 			u32 v;
14364 
14365 			v = SHMEM2_RD(bp,
14366 				      drv_capabilities_flag[BP_FW_MB_IDX(bp)]);
14367 			SHMEM2_WR(bp, drv_capabilities_flag[BP_FW_MB_IDX(bp)],
14368 				  v & ~DRV_FLAGS_CAPABILITIES_LOADED_L2);
14369 		}
14370 		bnx2x_drain_tx_queues(bp);
14371 		bnx2x_send_unload_req(bp, UNLOAD_RECOVERY);
14372 		bnx2x_netif_stop(bp, 1);
14373 		bnx2x_free_irq(bp);
14374 
14375 		/* Report UNLOAD_DONE to MCP */
14376 		bnx2x_send_unload_done(bp, true);
14377 
14378 		bp->sp_state = 0;
14379 		bp->port.pmf = 0;
14380 
14381 		bnx2x_prev_unload(bp);
14382 
14383 		/* We should have reseted the engine, so It's fair to
14384 		 * assume the FW will no longer write to the bnx2x driver.
14385 		 */
14386 		bnx2x_squeeze_objects(bp);
14387 		bnx2x_free_skbs(bp);
14388 		for_each_rx_queue(bp, i)
14389 			bnx2x_free_rx_sge_range(bp, bp->fp + i, NUM_RX_SGE);
14390 		bnx2x_free_fp_mem(bp);
14391 		bnx2x_free_mem(bp);
14392 
14393 		bp->state = BNX2X_STATE_CLOSED;
14394 	}
14395 
14396 	rtnl_unlock();
14397 
14398 	return PCI_ERS_RESULT_RECOVERED;
14399 }
14400 
14401 /**
14402  * bnx2x_io_resume - called when traffic can start flowing again
14403  * @pdev: Pointer to PCI device
14404  *
14405  * This callback is called when the error recovery driver tells us that
14406  * its OK to resume normal operation.
14407  */
14408 static void bnx2x_io_resume(struct pci_dev *pdev)
14409 {
14410 	struct net_device *dev = pci_get_drvdata(pdev);
14411 	struct bnx2x *bp = netdev_priv(dev);
14412 
14413 	if (bp->recovery_state != BNX2X_RECOVERY_DONE) {
14414 		netdev_err(bp->dev, "Handling parity error recovery. Try again later\n");
14415 		return;
14416 	}
14417 
14418 	rtnl_lock();
14419 
14420 	bp->fw_seq = SHMEM_RD(bp, func_mb[BP_FW_MB_IDX(bp)].drv_mb_header) &
14421 							DRV_MSG_SEQ_NUMBER_MASK;
14422 
14423 	if (netif_running(dev))
14424 		bnx2x_nic_load(bp, LOAD_NORMAL);
14425 
14426 	netif_device_attach(dev);
14427 
14428 	rtnl_unlock();
14429 }
14430 
14431 static const struct pci_error_handlers bnx2x_err_handler = {
14432 	.error_detected = bnx2x_io_error_detected,
14433 	.slot_reset     = bnx2x_io_slot_reset,
14434 	.resume         = bnx2x_io_resume,
14435 };
14436 
14437 static void bnx2x_shutdown(struct pci_dev *pdev)
14438 {
14439 	struct net_device *dev = pci_get_drvdata(pdev);
14440 	struct bnx2x *bp;
14441 
14442 	if (!dev)
14443 		return;
14444 
14445 	bp = netdev_priv(dev);
14446 	if (!bp)
14447 		return;
14448 
14449 	rtnl_lock();
14450 	netif_device_detach(dev);
14451 	rtnl_unlock();
14452 
14453 	/* Don't remove the netdevice, as there are scenarios which will cause
14454 	 * the kernel to hang, e.g., when trying to remove bnx2i while the
14455 	 * rootfs is mounted from SAN.
14456 	 */
14457 	__bnx2x_remove(pdev, dev, bp, false);
14458 }
14459 
14460 static struct pci_driver bnx2x_pci_driver = {
14461 	.name        = DRV_MODULE_NAME,
14462 	.id_table    = bnx2x_pci_tbl,
14463 	.probe       = bnx2x_init_one,
14464 	.remove      = bnx2x_remove_one,
14465 	.suspend     = bnx2x_suspend,
14466 	.resume      = bnx2x_resume,
14467 	.err_handler = &bnx2x_err_handler,
14468 #ifdef CONFIG_BNX2X_SRIOV
14469 	.sriov_configure = bnx2x_sriov_configure,
14470 #endif
14471 	.shutdown    = bnx2x_shutdown,
14472 };
14473 
14474 static int __init bnx2x_init(void)
14475 {
14476 	int ret;
14477 
14478 	bnx2x_wq = create_singlethread_workqueue("bnx2x");
14479 	if (bnx2x_wq == NULL) {
14480 		pr_err("Cannot create workqueue\n");
14481 		return -ENOMEM;
14482 	}
14483 	bnx2x_iov_wq = create_singlethread_workqueue("bnx2x_iov");
14484 	if (!bnx2x_iov_wq) {
14485 		pr_err("Cannot create iov workqueue\n");
14486 		destroy_workqueue(bnx2x_wq);
14487 		return -ENOMEM;
14488 	}
14489 
14490 	ret = pci_register_driver(&bnx2x_pci_driver);
14491 	if (ret) {
14492 		pr_err("Cannot register driver\n");
14493 		destroy_workqueue(bnx2x_wq);
14494 		destroy_workqueue(bnx2x_iov_wq);
14495 	}
14496 	return ret;
14497 }
14498 
14499 static void __exit bnx2x_cleanup(void)
14500 {
14501 	struct list_head *pos, *q;
14502 
14503 	pci_unregister_driver(&bnx2x_pci_driver);
14504 
14505 	destroy_workqueue(bnx2x_wq);
14506 	destroy_workqueue(bnx2x_iov_wq);
14507 
14508 	/* Free globally allocated resources */
14509 	list_for_each_safe(pos, q, &bnx2x_prev_list) {
14510 		struct bnx2x_prev_path_list *tmp =
14511 			list_entry(pos, struct bnx2x_prev_path_list, list);
14512 		list_del(pos);
14513 		kfree(tmp);
14514 	}
14515 }
14516 
14517 void bnx2x_notify_link_changed(struct bnx2x *bp)
14518 {
14519 	REG_WR(bp, MISC_REG_AEU_GENERAL_ATTN_12 + BP_FUNC(bp)*sizeof(u32), 1);
14520 }
14521 
14522 module_init(bnx2x_init);
14523 module_exit(bnx2x_cleanup);
14524 
14525 /**
14526  * bnx2x_set_iscsi_eth_mac_addr - set iSCSI MAC(s).
14527  *
14528  * @bp:		driver handle
14529  * @set:	set or clear the CAM entry
14530  *
14531  * This function will wait until the ramrod completion returns.
14532  * Return 0 if success, -ENODEV if ramrod doesn't return.
14533  */
14534 static int bnx2x_set_iscsi_eth_mac_addr(struct bnx2x *bp)
14535 {
14536 	unsigned long ramrod_flags = 0;
14537 
14538 	__set_bit(RAMROD_COMP_WAIT, &ramrod_flags);
14539 	return bnx2x_set_mac_one(bp, bp->cnic_eth_dev.iscsi_mac,
14540 				 &bp->iscsi_l2_mac_obj, true,
14541 				 BNX2X_ISCSI_ETH_MAC, &ramrod_flags);
14542 }
14543 
14544 /* count denotes the number of new completions we have seen */
14545 static void bnx2x_cnic_sp_post(struct bnx2x *bp, int count)
14546 {
14547 	struct eth_spe *spe;
14548 	int cxt_index, cxt_offset;
14549 
14550 #ifdef BNX2X_STOP_ON_ERROR
14551 	if (unlikely(bp->panic))
14552 		return;
14553 #endif
14554 
14555 	spin_lock_bh(&bp->spq_lock);
14556 	BUG_ON(bp->cnic_spq_pending < count);
14557 	bp->cnic_spq_pending -= count;
14558 
14559 	for (; bp->cnic_kwq_pending; bp->cnic_kwq_pending--) {
14560 		u16 type =  (le16_to_cpu(bp->cnic_kwq_cons->hdr.type)
14561 				& SPE_HDR_CONN_TYPE) >>
14562 				SPE_HDR_CONN_TYPE_SHIFT;
14563 		u8 cmd = (le32_to_cpu(bp->cnic_kwq_cons->hdr.conn_and_cmd_data)
14564 				>> SPE_HDR_CMD_ID_SHIFT) & 0xff;
14565 
14566 		/* Set validation for iSCSI L2 client before sending SETUP
14567 		 *  ramrod
14568 		 */
14569 		if (type == ETH_CONNECTION_TYPE) {
14570 			if (cmd == RAMROD_CMD_ID_ETH_CLIENT_SETUP) {
14571 				cxt_index = BNX2X_ISCSI_ETH_CID(bp) /
14572 					ILT_PAGE_CIDS;
14573 				cxt_offset = BNX2X_ISCSI_ETH_CID(bp) -
14574 					(cxt_index * ILT_PAGE_CIDS);
14575 				bnx2x_set_ctx_validation(bp,
14576 					&bp->context[cxt_index].
14577 							 vcxt[cxt_offset].eth,
14578 					BNX2X_ISCSI_ETH_CID(bp));
14579 			}
14580 		}
14581 
14582 		/*
14583 		 * There may be not more than 8 L2, not more than 8 L5 SPEs
14584 		 * and in the air. We also check that number of outstanding
14585 		 * COMMON ramrods is not more than the EQ and SPQ can
14586 		 * accommodate.
14587 		 */
14588 		if (type == ETH_CONNECTION_TYPE) {
14589 			if (!atomic_read(&bp->cq_spq_left))
14590 				break;
14591 			else
14592 				atomic_dec(&bp->cq_spq_left);
14593 		} else if (type == NONE_CONNECTION_TYPE) {
14594 			if (!atomic_read(&bp->eq_spq_left))
14595 				break;
14596 			else
14597 				atomic_dec(&bp->eq_spq_left);
14598 		} else if ((type == ISCSI_CONNECTION_TYPE) ||
14599 			   (type == FCOE_CONNECTION_TYPE)) {
14600 			if (bp->cnic_spq_pending >=
14601 			    bp->cnic_eth_dev.max_kwqe_pending)
14602 				break;
14603 			else
14604 				bp->cnic_spq_pending++;
14605 		} else {
14606 			BNX2X_ERR("Unknown SPE type: %d\n", type);
14607 			bnx2x_panic();
14608 			break;
14609 		}
14610 
14611 		spe = bnx2x_sp_get_next(bp);
14612 		*spe = *bp->cnic_kwq_cons;
14613 
14614 		DP(BNX2X_MSG_SP, "pending on SPQ %d, on KWQ %d count %d\n",
14615 		   bp->cnic_spq_pending, bp->cnic_kwq_pending, count);
14616 
14617 		if (bp->cnic_kwq_cons == bp->cnic_kwq_last)
14618 			bp->cnic_kwq_cons = bp->cnic_kwq;
14619 		else
14620 			bp->cnic_kwq_cons++;
14621 	}
14622 	bnx2x_sp_prod_update(bp);
14623 	spin_unlock_bh(&bp->spq_lock);
14624 }
14625 
14626 static int bnx2x_cnic_sp_queue(struct net_device *dev,
14627 			       struct kwqe_16 *kwqes[], u32 count)
14628 {
14629 	struct bnx2x *bp = netdev_priv(dev);
14630 	int i;
14631 
14632 #ifdef BNX2X_STOP_ON_ERROR
14633 	if (unlikely(bp->panic)) {
14634 		BNX2X_ERR("Can't post to SP queue while panic\n");
14635 		return -EIO;
14636 	}
14637 #endif
14638 
14639 	if ((bp->recovery_state != BNX2X_RECOVERY_DONE) &&
14640 	    (bp->recovery_state != BNX2X_RECOVERY_NIC_LOADING)) {
14641 		BNX2X_ERR("Handling parity error recovery. Try again later\n");
14642 		return -EAGAIN;
14643 	}
14644 
14645 	spin_lock_bh(&bp->spq_lock);
14646 
14647 	for (i = 0; i < count; i++) {
14648 		struct eth_spe *spe = (struct eth_spe *)kwqes[i];
14649 
14650 		if (bp->cnic_kwq_pending == MAX_SP_DESC_CNT)
14651 			break;
14652 
14653 		*bp->cnic_kwq_prod = *spe;
14654 
14655 		bp->cnic_kwq_pending++;
14656 
14657 		DP(BNX2X_MSG_SP, "L5 SPQE %x %x %x:%x pos %d\n",
14658 		   spe->hdr.conn_and_cmd_data, spe->hdr.type,
14659 		   spe->data.update_data_addr.hi,
14660 		   spe->data.update_data_addr.lo,
14661 		   bp->cnic_kwq_pending);
14662 
14663 		if (bp->cnic_kwq_prod == bp->cnic_kwq_last)
14664 			bp->cnic_kwq_prod = bp->cnic_kwq;
14665 		else
14666 			bp->cnic_kwq_prod++;
14667 	}
14668 
14669 	spin_unlock_bh(&bp->spq_lock);
14670 
14671 	if (bp->cnic_spq_pending < bp->cnic_eth_dev.max_kwqe_pending)
14672 		bnx2x_cnic_sp_post(bp, 0);
14673 
14674 	return i;
14675 }
14676 
14677 static int bnx2x_cnic_ctl_send(struct bnx2x *bp, struct cnic_ctl_info *ctl)
14678 {
14679 	struct cnic_ops *c_ops;
14680 	int rc = 0;
14681 
14682 	mutex_lock(&bp->cnic_mutex);
14683 	c_ops = rcu_dereference_protected(bp->cnic_ops,
14684 					  lockdep_is_held(&bp->cnic_mutex));
14685 	if (c_ops)
14686 		rc = c_ops->cnic_ctl(bp->cnic_data, ctl);
14687 	mutex_unlock(&bp->cnic_mutex);
14688 
14689 	return rc;
14690 }
14691 
14692 static int bnx2x_cnic_ctl_send_bh(struct bnx2x *bp, struct cnic_ctl_info *ctl)
14693 {
14694 	struct cnic_ops *c_ops;
14695 	int rc = 0;
14696 
14697 	rcu_read_lock();
14698 	c_ops = rcu_dereference(bp->cnic_ops);
14699 	if (c_ops)
14700 		rc = c_ops->cnic_ctl(bp->cnic_data, ctl);
14701 	rcu_read_unlock();
14702 
14703 	return rc;
14704 }
14705 
14706 /*
14707  * for commands that have no data
14708  */
14709 int bnx2x_cnic_notify(struct bnx2x *bp, int cmd)
14710 {
14711 	struct cnic_ctl_info ctl = {0};
14712 
14713 	ctl.cmd = cmd;
14714 
14715 	return bnx2x_cnic_ctl_send(bp, &ctl);
14716 }
14717 
14718 static void bnx2x_cnic_cfc_comp(struct bnx2x *bp, int cid, u8 err)
14719 {
14720 	struct cnic_ctl_info ctl = {0};
14721 
14722 	/* first we tell CNIC and only then we count this as a completion */
14723 	ctl.cmd = CNIC_CTL_COMPLETION_CMD;
14724 	ctl.data.comp.cid = cid;
14725 	ctl.data.comp.error = err;
14726 
14727 	bnx2x_cnic_ctl_send_bh(bp, &ctl);
14728 	bnx2x_cnic_sp_post(bp, 0);
14729 }
14730 
14731 /* Called with netif_addr_lock_bh() taken.
14732  * Sets an rx_mode config for an iSCSI ETH client.
14733  * Doesn't block.
14734  * Completion should be checked outside.
14735  */
14736 static void bnx2x_set_iscsi_eth_rx_mode(struct bnx2x *bp, bool start)
14737 {
14738 	unsigned long accept_flags = 0, ramrod_flags = 0;
14739 	u8 cl_id = bnx2x_cnic_eth_cl_id(bp, BNX2X_ISCSI_ETH_CL_ID_IDX);
14740 	int sched_state = BNX2X_FILTER_ISCSI_ETH_STOP_SCHED;
14741 
14742 	if (start) {
14743 		/* Start accepting on iSCSI L2 ring. Accept all multicasts
14744 		 * because it's the only way for UIO Queue to accept
14745 		 * multicasts (in non-promiscuous mode only one Queue per
14746 		 * function will receive multicast packets (leading in our
14747 		 * case).
14748 		 */
14749 		__set_bit(BNX2X_ACCEPT_UNICAST, &accept_flags);
14750 		__set_bit(BNX2X_ACCEPT_ALL_MULTICAST, &accept_flags);
14751 		__set_bit(BNX2X_ACCEPT_BROADCAST, &accept_flags);
14752 		__set_bit(BNX2X_ACCEPT_ANY_VLAN, &accept_flags);
14753 
14754 		/* Clear STOP_PENDING bit if START is requested */
14755 		clear_bit(BNX2X_FILTER_ISCSI_ETH_STOP_SCHED, &bp->sp_state);
14756 
14757 		sched_state = BNX2X_FILTER_ISCSI_ETH_START_SCHED;
14758 	} else
14759 		/* Clear START_PENDING bit if STOP is requested */
14760 		clear_bit(BNX2X_FILTER_ISCSI_ETH_START_SCHED, &bp->sp_state);
14761 
14762 	if (test_bit(BNX2X_FILTER_RX_MODE_PENDING, &bp->sp_state))
14763 		set_bit(sched_state, &bp->sp_state);
14764 	else {
14765 		__set_bit(RAMROD_RX, &ramrod_flags);
14766 		bnx2x_set_q_rx_mode(bp, cl_id, 0, accept_flags, 0,
14767 				    ramrod_flags);
14768 	}
14769 }
14770 
14771 static int bnx2x_drv_ctl(struct net_device *dev, struct drv_ctl_info *ctl)
14772 {
14773 	struct bnx2x *bp = netdev_priv(dev);
14774 	int rc = 0;
14775 
14776 	switch (ctl->cmd) {
14777 	case DRV_CTL_CTXTBL_WR_CMD: {
14778 		u32 index = ctl->data.io.offset;
14779 		dma_addr_t addr = ctl->data.io.dma_addr;
14780 
14781 		bnx2x_ilt_wr(bp, index, addr);
14782 		break;
14783 	}
14784 
14785 	case DRV_CTL_RET_L5_SPQ_CREDIT_CMD: {
14786 		int count = ctl->data.credit.credit_count;
14787 
14788 		bnx2x_cnic_sp_post(bp, count);
14789 		break;
14790 	}
14791 
14792 	/* rtnl_lock is held.  */
14793 	case DRV_CTL_START_L2_CMD: {
14794 		struct cnic_eth_dev *cp = &bp->cnic_eth_dev;
14795 		unsigned long sp_bits = 0;
14796 
14797 		/* Configure the iSCSI classification object */
14798 		bnx2x_init_mac_obj(bp, &bp->iscsi_l2_mac_obj,
14799 				   cp->iscsi_l2_client_id,
14800 				   cp->iscsi_l2_cid, BP_FUNC(bp),
14801 				   bnx2x_sp(bp, mac_rdata),
14802 				   bnx2x_sp_mapping(bp, mac_rdata),
14803 				   BNX2X_FILTER_MAC_PENDING,
14804 				   &bp->sp_state, BNX2X_OBJ_TYPE_RX,
14805 				   &bp->macs_pool);
14806 
14807 		/* Set iSCSI MAC address */
14808 		rc = bnx2x_set_iscsi_eth_mac_addr(bp);
14809 		if (rc)
14810 			break;
14811 
14812 		barrier();
14813 
14814 		/* Start accepting on iSCSI L2 ring */
14815 
14816 		netif_addr_lock_bh(dev);
14817 		bnx2x_set_iscsi_eth_rx_mode(bp, true);
14818 		netif_addr_unlock_bh(dev);
14819 
14820 		/* bits to wait on */
14821 		__set_bit(BNX2X_FILTER_RX_MODE_PENDING, &sp_bits);
14822 		__set_bit(BNX2X_FILTER_ISCSI_ETH_START_SCHED, &sp_bits);
14823 
14824 		if (!bnx2x_wait_sp_comp(bp, sp_bits))
14825 			BNX2X_ERR("rx_mode completion timed out!\n");
14826 
14827 		break;
14828 	}
14829 
14830 	/* rtnl_lock is held.  */
14831 	case DRV_CTL_STOP_L2_CMD: {
14832 		unsigned long sp_bits = 0;
14833 
14834 		/* Stop accepting on iSCSI L2 ring */
14835 		netif_addr_lock_bh(dev);
14836 		bnx2x_set_iscsi_eth_rx_mode(bp, false);
14837 		netif_addr_unlock_bh(dev);
14838 
14839 		/* bits to wait on */
14840 		__set_bit(BNX2X_FILTER_RX_MODE_PENDING, &sp_bits);
14841 		__set_bit(BNX2X_FILTER_ISCSI_ETH_STOP_SCHED, &sp_bits);
14842 
14843 		if (!bnx2x_wait_sp_comp(bp, sp_bits))
14844 			BNX2X_ERR("rx_mode completion timed out!\n");
14845 
14846 		barrier();
14847 
14848 		/* Unset iSCSI L2 MAC */
14849 		rc = bnx2x_del_all_macs(bp, &bp->iscsi_l2_mac_obj,
14850 					BNX2X_ISCSI_ETH_MAC, true);
14851 		break;
14852 	}
14853 	case DRV_CTL_RET_L2_SPQ_CREDIT_CMD: {
14854 		int count = ctl->data.credit.credit_count;
14855 
14856 		smp_mb__before_atomic();
14857 		atomic_add(count, &bp->cq_spq_left);
14858 		smp_mb__after_atomic();
14859 		break;
14860 	}
14861 	case DRV_CTL_ULP_REGISTER_CMD: {
14862 		int ulp_type = ctl->data.register_data.ulp_type;
14863 
14864 		if (CHIP_IS_E3(bp)) {
14865 			int idx = BP_FW_MB_IDX(bp);
14866 			u32 cap = SHMEM2_RD(bp, drv_capabilities_flag[idx]);
14867 			int path = BP_PATH(bp);
14868 			int port = BP_PORT(bp);
14869 			int i;
14870 			u32 scratch_offset;
14871 			u32 *host_addr;
14872 
14873 			/* first write capability to shmem2 */
14874 			if (ulp_type == CNIC_ULP_ISCSI)
14875 				cap |= DRV_FLAGS_CAPABILITIES_LOADED_ISCSI;
14876 			else if (ulp_type == CNIC_ULP_FCOE)
14877 				cap |= DRV_FLAGS_CAPABILITIES_LOADED_FCOE;
14878 			SHMEM2_WR(bp, drv_capabilities_flag[idx], cap);
14879 
14880 			if ((ulp_type != CNIC_ULP_FCOE) ||
14881 			    (!SHMEM2_HAS(bp, ncsi_oem_data_addr)) ||
14882 			    (!(bp->flags &  BC_SUPPORTS_FCOE_FEATURES)))
14883 				break;
14884 
14885 			/* if reached here - should write fcoe capabilities */
14886 			scratch_offset = SHMEM2_RD(bp, ncsi_oem_data_addr);
14887 			if (!scratch_offset)
14888 				break;
14889 			scratch_offset += offsetof(struct glob_ncsi_oem_data,
14890 						   fcoe_features[path][port]);
14891 			host_addr = (u32 *) &(ctl->data.register_data.
14892 					      fcoe_features);
14893 			for (i = 0; i < sizeof(struct fcoe_capabilities);
14894 			     i += 4)
14895 				REG_WR(bp, scratch_offset + i,
14896 				       *(host_addr + i/4));
14897 		}
14898 		bnx2x_schedule_sp_rtnl(bp, BNX2X_SP_RTNL_GET_DRV_VERSION, 0);
14899 		break;
14900 	}
14901 
14902 	case DRV_CTL_ULP_UNREGISTER_CMD: {
14903 		int ulp_type = ctl->data.ulp_type;
14904 
14905 		if (CHIP_IS_E3(bp)) {
14906 			int idx = BP_FW_MB_IDX(bp);
14907 			u32 cap;
14908 
14909 			cap = SHMEM2_RD(bp, drv_capabilities_flag[idx]);
14910 			if (ulp_type == CNIC_ULP_ISCSI)
14911 				cap &= ~DRV_FLAGS_CAPABILITIES_LOADED_ISCSI;
14912 			else if (ulp_type == CNIC_ULP_FCOE)
14913 				cap &= ~DRV_FLAGS_CAPABILITIES_LOADED_FCOE;
14914 			SHMEM2_WR(bp, drv_capabilities_flag[idx], cap);
14915 		}
14916 		bnx2x_schedule_sp_rtnl(bp, BNX2X_SP_RTNL_GET_DRV_VERSION, 0);
14917 		break;
14918 	}
14919 
14920 	default:
14921 		BNX2X_ERR("unknown command %x\n", ctl->cmd);
14922 		rc = -EINVAL;
14923 	}
14924 
14925 	/* For storage-only interfaces, change driver state */
14926 	if (IS_MF_SD_STORAGE_PERSONALITY_ONLY(bp)) {
14927 		switch (ctl->drv_state) {
14928 		case DRV_NOP:
14929 			break;
14930 		case DRV_ACTIVE:
14931 			bnx2x_set_os_driver_state(bp,
14932 						  OS_DRIVER_STATE_ACTIVE);
14933 			break;
14934 		case DRV_INACTIVE:
14935 			bnx2x_set_os_driver_state(bp,
14936 						  OS_DRIVER_STATE_DISABLED);
14937 			break;
14938 		case DRV_UNLOADED:
14939 			bnx2x_set_os_driver_state(bp,
14940 						  OS_DRIVER_STATE_NOT_LOADED);
14941 			break;
14942 		default:
14943 		BNX2X_ERR("Unknown cnic driver state: %d\n", ctl->drv_state);
14944 		}
14945 	}
14946 
14947 	return rc;
14948 }
14949 
14950 static int bnx2x_get_fc_npiv(struct net_device *dev,
14951 			     struct cnic_fc_npiv_tbl *cnic_tbl)
14952 {
14953 	struct bnx2x *bp = netdev_priv(dev);
14954 	struct bdn_fc_npiv_tbl *tbl = NULL;
14955 	u32 offset, entries;
14956 	int rc = -EINVAL;
14957 	int i;
14958 
14959 	if (!SHMEM2_HAS(bp, fc_npiv_nvram_tbl_addr[0]))
14960 		goto out;
14961 
14962 	DP(BNX2X_MSG_MCP, "About to read the FC-NPIV table\n");
14963 
14964 	tbl = kmalloc(sizeof(*tbl), GFP_KERNEL);
14965 	if (!tbl) {
14966 		BNX2X_ERR("Failed to allocate fc_npiv table\n");
14967 		goto out;
14968 	}
14969 
14970 	offset = SHMEM2_RD(bp, fc_npiv_nvram_tbl_addr[BP_PORT(bp)]);
14971 	if (!offset) {
14972 		DP(BNX2X_MSG_MCP, "No FC-NPIV in NVRAM\n");
14973 		goto out;
14974 	}
14975 	DP(BNX2X_MSG_MCP, "Offset of FC-NPIV in NVRAM: %08x\n", offset);
14976 
14977 	/* Read the table contents from nvram */
14978 	if (bnx2x_nvram_read(bp, offset, (u8 *)tbl, sizeof(*tbl))) {
14979 		BNX2X_ERR("Failed to read FC-NPIV table\n");
14980 		goto out;
14981 	}
14982 
14983 	/* Since bnx2x_nvram_read() returns data in be32, we need to convert
14984 	 * the number of entries back to cpu endianness.
14985 	 */
14986 	entries = tbl->fc_npiv_cfg.num_of_npiv;
14987 	entries = (__force u32)be32_to_cpu((__force __be32)entries);
14988 	tbl->fc_npiv_cfg.num_of_npiv = entries;
14989 
14990 	if (!tbl->fc_npiv_cfg.num_of_npiv) {
14991 		DP(BNX2X_MSG_MCP,
14992 		   "No FC-NPIV table [valid, simply not present]\n");
14993 		goto out;
14994 	} else if (tbl->fc_npiv_cfg.num_of_npiv > MAX_NUMBER_NPIV) {
14995 		BNX2X_ERR("FC-NPIV table with bad length 0x%08x\n",
14996 			  tbl->fc_npiv_cfg.num_of_npiv);
14997 		goto out;
14998 	} else {
14999 		DP(BNX2X_MSG_MCP, "Read 0x%08x entries from NVRAM\n",
15000 		   tbl->fc_npiv_cfg.num_of_npiv);
15001 	}
15002 
15003 	/* Copy the data into cnic-provided struct */
15004 	cnic_tbl->count = tbl->fc_npiv_cfg.num_of_npiv;
15005 	for (i = 0; i < cnic_tbl->count; i++) {
15006 		memcpy(cnic_tbl->wwpn[i], tbl->settings[i].npiv_wwpn, 8);
15007 		memcpy(cnic_tbl->wwnn[i], tbl->settings[i].npiv_wwnn, 8);
15008 	}
15009 
15010 	rc = 0;
15011 out:
15012 	kfree(tbl);
15013 	return rc;
15014 }
15015 
15016 void bnx2x_setup_cnic_irq_info(struct bnx2x *bp)
15017 {
15018 	struct cnic_eth_dev *cp = &bp->cnic_eth_dev;
15019 
15020 	if (bp->flags & USING_MSIX_FLAG) {
15021 		cp->drv_state |= CNIC_DRV_STATE_USING_MSIX;
15022 		cp->irq_arr[0].irq_flags |= CNIC_IRQ_FL_MSIX;
15023 		cp->irq_arr[0].vector = bp->msix_table[1].vector;
15024 	} else {
15025 		cp->drv_state &= ~CNIC_DRV_STATE_USING_MSIX;
15026 		cp->irq_arr[0].irq_flags &= ~CNIC_IRQ_FL_MSIX;
15027 	}
15028 	if (!CHIP_IS_E1x(bp))
15029 		cp->irq_arr[0].status_blk = (void *)bp->cnic_sb.e2_sb;
15030 	else
15031 		cp->irq_arr[0].status_blk = (void *)bp->cnic_sb.e1x_sb;
15032 
15033 	cp->irq_arr[0].status_blk_num =  bnx2x_cnic_fw_sb_id(bp);
15034 	cp->irq_arr[0].status_blk_num2 = bnx2x_cnic_igu_sb_id(bp);
15035 	cp->irq_arr[1].status_blk = bp->def_status_blk;
15036 	cp->irq_arr[1].status_blk_num = DEF_SB_ID;
15037 	cp->irq_arr[1].status_blk_num2 = DEF_SB_IGU_ID;
15038 
15039 	cp->num_irq = 2;
15040 }
15041 
15042 void bnx2x_setup_cnic_info(struct bnx2x *bp)
15043 {
15044 	struct cnic_eth_dev *cp = &bp->cnic_eth_dev;
15045 
15046 	cp->ctx_tbl_offset = FUNC_ILT_BASE(BP_FUNC(bp)) +
15047 			     bnx2x_cid_ilt_lines(bp);
15048 	cp->starting_cid = bnx2x_cid_ilt_lines(bp) * ILT_PAGE_CIDS;
15049 	cp->fcoe_init_cid = BNX2X_FCOE_ETH_CID(bp);
15050 	cp->iscsi_l2_cid = BNX2X_ISCSI_ETH_CID(bp);
15051 
15052 	DP(NETIF_MSG_IFUP, "BNX2X_1st_NON_L2_ETH_CID(bp) %x, cp->starting_cid %x, cp->fcoe_init_cid %x, cp->iscsi_l2_cid %x\n",
15053 	   BNX2X_1st_NON_L2_ETH_CID(bp), cp->starting_cid, cp->fcoe_init_cid,
15054 	   cp->iscsi_l2_cid);
15055 
15056 	if (NO_ISCSI_OOO(bp))
15057 		cp->drv_state |= CNIC_DRV_STATE_NO_ISCSI_OOO;
15058 }
15059 
15060 static int bnx2x_register_cnic(struct net_device *dev, struct cnic_ops *ops,
15061 			       void *data)
15062 {
15063 	struct bnx2x *bp = netdev_priv(dev);
15064 	struct cnic_eth_dev *cp = &bp->cnic_eth_dev;
15065 	int rc;
15066 
15067 	DP(NETIF_MSG_IFUP, "Register_cnic called\n");
15068 
15069 	if (ops == NULL) {
15070 		BNX2X_ERR("NULL ops received\n");
15071 		return -EINVAL;
15072 	}
15073 
15074 	if (!CNIC_SUPPORT(bp)) {
15075 		BNX2X_ERR("Can't register CNIC when not supported\n");
15076 		return -EOPNOTSUPP;
15077 	}
15078 
15079 	if (!CNIC_LOADED(bp)) {
15080 		rc = bnx2x_load_cnic(bp);
15081 		if (rc) {
15082 			BNX2X_ERR("CNIC-related load failed\n");
15083 			return rc;
15084 		}
15085 	}
15086 
15087 	bp->cnic_enabled = true;
15088 
15089 	bp->cnic_kwq = kzalloc(PAGE_SIZE, GFP_KERNEL);
15090 	if (!bp->cnic_kwq)
15091 		return -ENOMEM;
15092 
15093 	bp->cnic_kwq_cons = bp->cnic_kwq;
15094 	bp->cnic_kwq_prod = bp->cnic_kwq;
15095 	bp->cnic_kwq_last = bp->cnic_kwq + MAX_SP_DESC_CNT;
15096 
15097 	bp->cnic_spq_pending = 0;
15098 	bp->cnic_kwq_pending = 0;
15099 
15100 	bp->cnic_data = data;
15101 
15102 	cp->num_irq = 0;
15103 	cp->drv_state |= CNIC_DRV_STATE_REGD;
15104 	cp->iro_arr = bp->iro_arr;
15105 
15106 	bnx2x_setup_cnic_irq_info(bp);
15107 
15108 	rcu_assign_pointer(bp->cnic_ops, ops);
15109 
15110 	/* Schedule driver to read CNIC driver versions */
15111 	bnx2x_schedule_sp_rtnl(bp, BNX2X_SP_RTNL_GET_DRV_VERSION, 0);
15112 
15113 	return 0;
15114 }
15115 
15116 static int bnx2x_unregister_cnic(struct net_device *dev)
15117 {
15118 	struct bnx2x *bp = netdev_priv(dev);
15119 	struct cnic_eth_dev *cp = &bp->cnic_eth_dev;
15120 
15121 	mutex_lock(&bp->cnic_mutex);
15122 	cp->drv_state = 0;
15123 	RCU_INIT_POINTER(bp->cnic_ops, NULL);
15124 	mutex_unlock(&bp->cnic_mutex);
15125 	synchronize_rcu();
15126 	bp->cnic_enabled = false;
15127 	kfree(bp->cnic_kwq);
15128 	bp->cnic_kwq = NULL;
15129 
15130 	return 0;
15131 }
15132 
15133 static struct cnic_eth_dev *bnx2x_cnic_probe(struct net_device *dev)
15134 {
15135 	struct bnx2x *bp = netdev_priv(dev);
15136 	struct cnic_eth_dev *cp = &bp->cnic_eth_dev;
15137 
15138 	/* If both iSCSI and FCoE are disabled - return NULL in
15139 	 * order to indicate CNIC that it should not try to work
15140 	 * with this device.
15141 	 */
15142 	if (NO_ISCSI(bp) && NO_FCOE(bp))
15143 		return NULL;
15144 
15145 	cp->drv_owner = THIS_MODULE;
15146 	cp->chip_id = CHIP_ID(bp);
15147 	cp->pdev = bp->pdev;
15148 	cp->io_base = bp->regview;
15149 	cp->io_base2 = bp->doorbells;
15150 	cp->max_kwqe_pending = 8;
15151 	cp->ctx_blk_size = CDU_ILT_PAGE_SZ;
15152 	cp->ctx_tbl_offset = FUNC_ILT_BASE(BP_FUNC(bp)) +
15153 			     bnx2x_cid_ilt_lines(bp);
15154 	cp->ctx_tbl_len = CNIC_ILT_LINES;
15155 	cp->starting_cid = bnx2x_cid_ilt_lines(bp) * ILT_PAGE_CIDS;
15156 	cp->drv_submit_kwqes_16 = bnx2x_cnic_sp_queue;
15157 	cp->drv_ctl = bnx2x_drv_ctl;
15158 	cp->drv_get_fc_npiv_tbl = bnx2x_get_fc_npiv;
15159 	cp->drv_register_cnic = bnx2x_register_cnic;
15160 	cp->drv_unregister_cnic = bnx2x_unregister_cnic;
15161 	cp->fcoe_init_cid = BNX2X_FCOE_ETH_CID(bp);
15162 	cp->iscsi_l2_client_id =
15163 		bnx2x_cnic_eth_cl_id(bp, BNX2X_ISCSI_ETH_CL_ID_IDX);
15164 	cp->iscsi_l2_cid = BNX2X_ISCSI_ETH_CID(bp);
15165 
15166 	if (NO_ISCSI_OOO(bp))
15167 		cp->drv_state |= CNIC_DRV_STATE_NO_ISCSI_OOO;
15168 
15169 	if (NO_ISCSI(bp))
15170 		cp->drv_state |= CNIC_DRV_STATE_NO_ISCSI;
15171 
15172 	if (NO_FCOE(bp))
15173 		cp->drv_state |= CNIC_DRV_STATE_NO_FCOE;
15174 
15175 	BNX2X_DEV_INFO(
15176 		"page_size %d, tbl_offset %d, tbl_lines %d, starting cid %d\n",
15177 	   cp->ctx_blk_size,
15178 	   cp->ctx_tbl_offset,
15179 	   cp->ctx_tbl_len,
15180 	   cp->starting_cid);
15181 	return cp;
15182 }
15183 
15184 static u32 bnx2x_rx_ustorm_prods_offset(struct bnx2x_fastpath *fp)
15185 {
15186 	struct bnx2x *bp = fp->bp;
15187 	u32 offset = BAR_USTRORM_INTMEM;
15188 
15189 	if (IS_VF(bp))
15190 		return bnx2x_vf_ustorm_prods_offset(bp, fp);
15191 	else if (!CHIP_IS_E1x(bp))
15192 		offset += USTORM_RX_PRODS_E2_OFFSET(fp->cl_qzone_id);
15193 	else
15194 		offset += USTORM_RX_PRODS_E1X_OFFSET(BP_PORT(bp), fp->cl_id);
15195 
15196 	return offset;
15197 }
15198 
15199 /* called only on E1H or E2.
15200  * When pretending to be PF, the pretend value is the function number 0...7
15201  * When pretending to be VF, the pretend val is the PF-num:VF-valid:ABS-VFID
15202  * combination
15203  */
15204 int bnx2x_pretend_func(struct bnx2x *bp, u16 pretend_func_val)
15205 {
15206 	u32 pretend_reg;
15207 
15208 	if (CHIP_IS_E1H(bp) && pretend_func_val >= E1H_FUNC_MAX)
15209 		return -1;
15210 
15211 	/* get my own pretend register */
15212 	pretend_reg = bnx2x_get_pretend_reg(bp);
15213 	REG_WR(bp, pretend_reg, pretend_func_val);
15214 	REG_RD(bp, pretend_reg);
15215 	return 0;
15216 }
15217 
15218 static void bnx2x_ptp_task(struct work_struct *work)
15219 {
15220 	struct bnx2x *bp = container_of(work, struct bnx2x, ptp_task);
15221 	int port = BP_PORT(bp);
15222 	u32 val_seq;
15223 	u64 timestamp, ns;
15224 	struct skb_shared_hwtstamps shhwtstamps;
15225 	bool bail = true;
15226 	int i;
15227 
15228 	/* FW may take a while to complete timestamping; try a bit and if it's
15229 	 * still not complete, may indicate an error state - bail out then.
15230 	 */
15231 	for (i = 0; i < 10; i++) {
15232 		/* Read Tx timestamp registers */
15233 		val_seq = REG_RD(bp, port ? NIG_REG_P1_TLLH_PTP_BUF_SEQID :
15234 				 NIG_REG_P0_TLLH_PTP_BUF_SEQID);
15235 		if (val_seq & 0x10000) {
15236 			bail = false;
15237 			break;
15238 		}
15239 		msleep(1 << i);
15240 	}
15241 
15242 	if (!bail) {
15243 		/* There is a valid timestamp value */
15244 		timestamp = REG_RD(bp, port ? NIG_REG_P1_TLLH_PTP_BUF_TS_MSB :
15245 				   NIG_REG_P0_TLLH_PTP_BUF_TS_MSB);
15246 		timestamp <<= 32;
15247 		timestamp |= REG_RD(bp, port ? NIG_REG_P1_TLLH_PTP_BUF_TS_LSB :
15248 				    NIG_REG_P0_TLLH_PTP_BUF_TS_LSB);
15249 		/* Reset timestamp register to allow new timestamp */
15250 		REG_WR(bp, port ? NIG_REG_P1_TLLH_PTP_BUF_SEQID :
15251 		       NIG_REG_P0_TLLH_PTP_BUF_SEQID, 0x10000);
15252 		ns = timecounter_cyc2time(&bp->timecounter, timestamp);
15253 
15254 		memset(&shhwtstamps, 0, sizeof(shhwtstamps));
15255 		shhwtstamps.hwtstamp = ns_to_ktime(ns);
15256 		skb_tstamp_tx(bp->ptp_tx_skb, &shhwtstamps);
15257 
15258 		DP(BNX2X_MSG_PTP, "Tx timestamp, timestamp cycles = %llu, ns = %llu\n",
15259 		   timestamp, ns);
15260 	} else {
15261 		DP(BNX2X_MSG_PTP,
15262 		   "Tx timestamp is not recorded (register read=%u)\n",
15263 		   val_seq);
15264 		bp->eth_stats.ptp_skip_tx_ts++;
15265 	}
15266 
15267 	dev_kfree_skb_any(bp->ptp_tx_skb);
15268 	bp->ptp_tx_skb = NULL;
15269 }
15270 
15271 void bnx2x_set_rx_ts(struct bnx2x *bp, struct sk_buff *skb)
15272 {
15273 	int port = BP_PORT(bp);
15274 	u64 timestamp, ns;
15275 
15276 	timestamp = REG_RD(bp, port ? NIG_REG_P1_LLH_PTP_HOST_BUF_TS_MSB :
15277 			    NIG_REG_P0_LLH_PTP_HOST_BUF_TS_MSB);
15278 	timestamp <<= 32;
15279 	timestamp |= REG_RD(bp, port ? NIG_REG_P1_LLH_PTP_HOST_BUF_TS_LSB :
15280 			    NIG_REG_P0_LLH_PTP_HOST_BUF_TS_LSB);
15281 
15282 	/* Reset timestamp register to allow new timestamp */
15283 	REG_WR(bp, port ? NIG_REG_P1_LLH_PTP_HOST_BUF_SEQID :
15284 	       NIG_REG_P0_LLH_PTP_HOST_BUF_SEQID, 0x10000);
15285 
15286 	ns = timecounter_cyc2time(&bp->timecounter, timestamp);
15287 
15288 	skb_hwtstamps(skb)->hwtstamp = ns_to_ktime(ns);
15289 
15290 	DP(BNX2X_MSG_PTP, "Rx timestamp, timestamp cycles = %llu, ns = %llu\n",
15291 	   timestamp, ns);
15292 }
15293 
15294 /* Read the PHC */
15295 static u64 bnx2x_cyclecounter_read(const struct cyclecounter *cc)
15296 {
15297 	struct bnx2x *bp = container_of(cc, struct bnx2x, cyclecounter);
15298 	int port = BP_PORT(bp);
15299 	u32 wb_data[2];
15300 	u64 phc_cycles;
15301 
15302 	REG_RD_DMAE(bp, port ? NIG_REG_TIMESYNC_GEN_REG + tsgen_synctime_t1 :
15303 		    NIG_REG_TIMESYNC_GEN_REG + tsgen_synctime_t0, wb_data, 2);
15304 	phc_cycles = wb_data[1];
15305 	phc_cycles = (phc_cycles << 32) + wb_data[0];
15306 
15307 	DP(BNX2X_MSG_PTP, "PHC read cycles = %llu\n", phc_cycles);
15308 
15309 	return phc_cycles;
15310 }
15311 
15312 static void bnx2x_init_cyclecounter(struct bnx2x *bp)
15313 {
15314 	memset(&bp->cyclecounter, 0, sizeof(bp->cyclecounter));
15315 	bp->cyclecounter.read = bnx2x_cyclecounter_read;
15316 	bp->cyclecounter.mask = CYCLECOUNTER_MASK(64);
15317 	bp->cyclecounter.shift = 0;
15318 	bp->cyclecounter.mult = 1;
15319 }
15320 
15321 static int bnx2x_send_reset_timesync_ramrod(struct bnx2x *bp)
15322 {
15323 	struct bnx2x_func_state_params func_params = {NULL};
15324 	struct bnx2x_func_set_timesync_params *set_timesync_params =
15325 		&func_params.params.set_timesync;
15326 
15327 	/* Prepare parameters for function state transitions */
15328 	__set_bit(RAMROD_COMP_WAIT, &func_params.ramrod_flags);
15329 	__set_bit(RAMROD_RETRY, &func_params.ramrod_flags);
15330 
15331 	func_params.f_obj = &bp->func_obj;
15332 	func_params.cmd = BNX2X_F_CMD_SET_TIMESYNC;
15333 
15334 	/* Function parameters */
15335 	set_timesync_params->drift_adjust_cmd = TS_DRIFT_ADJUST_RESET;
15336 	set_timesync_params->offset_cmd = TS_OFFSET_KEEP;
15337 
15338 	return bnx2x_func_state_change(bp, &func_params);
15339 }
15340 
15341 static int bnx2x_enable_ptp_packets(struct bnx2x *bp)
15342 {
15343 	struct bnx2x_queue_state_params q_params;
15344 	int rc, i;
15345 
15346 	/* send queue update ramrod to enable PTP packets */
15347 	memset(&q_params, 0, sizeof(q_params));
15348 	__set_bit(RAMROD_COMP_WAIT, &q_params.ramrod_flags);
15349 	q_params.cmd = BNX2X_Q_CMD_UPDATE;
15350 	__set_bit(BNX2X_Q_UPDATE_PTP_PKTS_CHNG,
15351 		  &q_params.params.update.update_flags);
15352 	__set_bit(BNX2X_Q_UPDATE_PTP_PKTS,
15353 		  &q_params.params.update.update_flags);
15354 
15355 	/* send the ramrod on all the queues of the PF */
15356 	for_each_eth_queue(bp, i) {
15357 		struct bnx2x_fastpath *fp = &bp->fp[i];
15358 
15359 		/* Set the appropriate Queue object */
15360 		q_params.q_obj = &bnx2x_sp_obj(bp, fp).q_obj;
15361 
15362 		/* Update the Queue state */
15363 		rc = bnx2x_queue_state_change(bp, &q_params);
15364 		if (rc) {
15365 			BNX2X_ERR("Failed to enable PTP packets\n");
15366 			return rc;
15367 		}
15368 	}
15369 
15370 	return 0;
15371 }
15372 
15373 #define BNX2X_P2P_DETECT_PARAM_MASK 0x5F5
15374 #define BNX2X_P2P_DETECT_RULE_MASK 0x3DBB
15375 #define BNX2X_PTP_TX_ON_PARAM_MASK (BNX2X_P2P_DETECT_PARAM_MASK & 0x6AA)
15376 #define BNX2X_PTP_TX_ON_RULE_MASK (BNX2X_P2P_DETECT_RULE_MASK & 0x3EEE)
15377 #define BNX2X_PTP_V1_L4_PARAM_MASK (BNX2X_P2P_DETECT_PARAM_MASK & 0x7EE)
15378 #define BNX2X_PTP_V1_L4_RULE_MASK (BNX2X_P2P_DETECT_RULE_MASK & 0x3FFE)
15379 #define BNX2X_PTP_V2_L4_PARAM_MASK (BNX2X_P2P_DETECT_PARAM_MASK & 0x7EA)
15380 #define BNX2X_PTP_V2_L4_RULE_MASK (BNX2X_P2P_DETECT_RULE_MASK & 0x3FEE)
15381 #define BNX2X_PTP_V2_L2_PARAM_MASK (BNX2X_P2P_DETECT_PARAM_MASK & 0x6BF)
15382 #define BNX2X_PTP_V2_L2_RULE_MASK (BNX2X_P2P_DETECT_RULE_MASK & 0x3EFF)
15383 #define BNX2X_PTP_V2_PARAM_MASK (BNX2X_P2P_DETECT_PARAM_MASK & 0x6AA)
15384 #define BNX2X_PTP_V2_RULE_MASK (BNX2X_P2P_DETECT_RULE_MASK & 0x3EEE)
15385 
15386 int bnx2x_configure_ptp_filters(struct bnx2x *bp)
15387 {
15388 	int port = BP_PORT(bp);
15389 	u32 param, rule;
15390 	int rc;
15391 
15392 	if (!bp->hwtstamp_ioctl_called)
15393 		return 0;
15394 
15395 	param = port ? NIG_REG_P1_TLLH_PTP_PARAM_MASK :
15396 		NIG_REG_P0_TLLH_PTP_PARAM_MASK;
15397 	rule = port ? NIG_REG_P1_TLLH_PTP_RULE_MASK :
15398 		NIG_REG_P0_TLLH_PTP_RULE_MASK;
15399 	switch (bp->tx_type) {
15400 	case HWTSTAMP_TX_ON:
15401 		bp->flags |= TX_TIMESTAMPING_EN;
15402 		REG_WR(bp, param, BNX2X_PTP_TX_ON_PARAM_MASK);
15403 		REG_WR(bp, rule, BNX2X_PTP_TX_ON_RULE_MASK);
15404 		break;
15405 	case HWTSTAMP_TX_ONESTEP_SYNC:
15406 	case HWTSTAMP_TX_ONESTEP_P2P:
15407 		BNX2X_ERR("One-step timestamping is not supported\n");
15408 		return -ERANGE;
15409 	}
15410 
15411 	param = port ? NIG_REG_P1_LLH_PTP_PARAM_MASK :
15412 		NIG_REG_P0_LLH_PTP_PARAM_MASK;
15413 	rule = port ? NIG_REG_P1_LLH_PTP_RULE_MASK :
15414 		NIG_REG_P0_LLH_PTP_RULE_MASK;
15415 	switch (bp->rx_filter) {
15416 	case HWTSTAMP_FILTER_NONE:
15417 		break;
15418 	case HWTSTAMP_FILTER_ALL:
15419 	case HWTSTAMP_FILTER_SOME:
15420 	case HWTSTAMP_FILTER_NTP_ALL:
15421 		bp->rx_filter = HWTSTAMP_FILTER_NONE;
15422 		break;
15423 	case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
15424 	case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
15425 	case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
15426 		bp->rx_filter = HWTSTAMP_FILTER_PTP_V1_L4_EVENT;
15427 		/* Initialize PTP detection for UDP/IPv4 events */
15428 		REG_WR(bp, param, BNX2X_PTP_V1_L4_PARAM_MASK);
15429 		REG_WR(bp, rule, BNX2X_PTP_V1_L4_RULE_MASK);
15430 		break;
15431 	case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
15432 	case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
15433 	case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
15434 		bp->rx_filter = HWTSTAMP_FILTER_PTP_V2_L4_EVENT;
15435 		/* Initialize PTP detection for UDP/IPv4 or UDP/IPv6 events */
15436 		REG_WR(bp, param, BNX2X_PTP_V2_L4_PARAM_MASK);
15437 		REG_WR(bp, rule, BNX2X_PTP_V2_L4_RULE_MASK);
15438 		break;
15439 	case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
15440 	case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
15441 	case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
15442 		bp->rx_filter = HWTSTAMP_FILTER_PTP_V2_L2_EVENT;
15443 		/* Initialize PTP detection L2 events */
15444 		REG_WR(bp, param, BNX2X_PTP_V2_L2_PARAM_MASK);
15445 		REG_WR(bp, rule, BNX2X_PTP_V2_L2_RULE_MASK);
15446 
15447 		break;
15448 	case HWTSTAMP_FILTER_PTP_V2_EVENT:
15449 	case HWTSTAMP_FILTER_PTP_V2_SYNC:
15450 	case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
15451 		bp->rx_filter = HWTSTAMP_FILTER_PTP_V2_EVENT;
15452 		/* Initialize PTP detection L2, UDP/IPv4 or UDP/IPv6 events */
15453 		REG_WR(bp, param, BNX2X_PTP_V2_PARAM_MASK);
15454 		REG_WR(bp, rule, BNX2X_PTP_V2_RULE_MASK);
15455 		break;
15456 	}
15457 
15458 	/* Indicate to FW that this PF expects recorded PTP packets */
15459 	rc = bnx2x_enable_ptp_packets(bp);
15460 	if (rc)
15461 		return rc;
15462 
15463 	/* Enable sending PTP packets to host */
15464 	REG_WR(bp, port ? NIG_REG_P1_LLH_PTP_TO_HOST :
15465 	       NIG_REG_P0_LLH_PTP_TO_HOST, 0x1);
15466 
15467 	return 0;
15468 }
15469 
15470 static int bnx2x_hwtstamp_ioctl(struct bnx2x *bp, struct ifreq *ifr)
15471 {
15472 	struct hwtstamp_config config;
15473 	int rc;
15474 
15475 	DP(BNX2X_MSG_PTP, "HWTSTAMP IOCTL called\n");
15476 
15477 	if (copy_from_user(&config, ifr->ifr_data, sizeof(config)))
15478 		return -EFAULT;
15479 
15480 	DP(BNX2X_MSG_PTP, "Requested tx_type: %d, requested rx_filters = %d\n",
15481 	   config.tx_type, config.rx_filter);
15482 
15483 	if (config.flags) {
15484 		BNX2X_ERR("config.flags is reserved for future use\n");
15485 		return -EINVAL;
15486 	}
15487 
15488 	bp->hwtstamp_ioctl_called = 1;
15489 	bp->tx_type = config.tx_type;
15490 	bp->rx_filter = config.rx_filter;
15491 
15492 	rc = bnx2x_configure_ptp_filters(bp);
15493 	if (rc)
15494 		return rc;
15495 
15496 	config.rx_filter = bp->rx_filter;
15497 
15498 	return copy_to_user(ifr->ifr_data, &config, sizeof(config)) ?
15499 		-EFAULT : 0;
15500 }
15501 
15502 /* Configures HW for PTP */
15503 static int bnx2x_configure_ptp(struct bnx2x *bp)
15504 {
15505 	int rc, port = BP_PORT(bp);
15506 	u32 wb_data[2];
15507 
15508 	/* Reset PTP event detection rules - will be configured in the IOCTL */
15509 	REG_WR(bp, port ? NIG_REG_P1_LLH_PTP_PARAM_MASK :
15510 	       NIG_REG_P0_LLH_PTP_PARAM_MASK, 0x7FF);
15511 	REG_WR(bp, port ? NIG_REG_P1_LLH_PTP_RULE_MASK :
15512 	       NIG_REG_P0_LLH_PTP_RULE_MASK, 0x3FFF);
15513 	REG_WR(bp, port ? NIG_REG_P1_TLLH_PTP_PARAM_MASK :
15514 	       NIG_REG_P0_TLLH_PTP_PARAM_MASK, 0x7FF);
15515 	REG_WR(bp, port ? NIG_REG_P1_TLLH_PTP_RULE_MASK :
15516 	       NIG_REG_P0_TLLH_PTP_RULE_MASK, 0x3FFF);
15517 
15518 	/* Disable PTP packets to host - will be configured in the IOCTL*/
15519 	REG_WR(bp, port ? NIG_REG_P1_LLH_PTP_TO_HOST :
15520 	       NIG_REG_P0_LLH_PTP_TO_HOST, 0x0);
15521 
15522 	/* Enable the PTP feature */
15523 	REG_WR(bp, port ? NIG_REG_P1_PTP_EN :
15524 	       NIG_REG_P0_PTP_EN, 0x3F);
15525 
15526 	/* Enable the free-running counter */
15527 	wb_data[0] = 0;
15528 	wb_data[1] = 0;
15529 	REG_WR_DMAE(bp, NIG_REG_TIMESYNC_GEN_REG + tsgen_ctrl, wb_data, 2);
15530 
15531 	/* Reset drift register (offset register is not reset) */
15532 	rc = bnx2x_send_reset_timesync_ramrod(bp);
15533 	if (rc) {
15534 		BNX2X_ERR("Failed to reset PHC drift register\n");
15535 		return -EFAULT;
15536 	}
15537 
15538 	/* Reset possibly old timestamps */
15539 	REG_WR(bp, port ? NIG_REG_P1_LLH_PTP_HOST_BUF_SEQID :
15540 	       NIG_REG_P0_LLH_PTP_HOST_BUF_SEQID, 0x10000);
15541 	REG_WR(bp, port ? NIG_REG_P1_TLLH_PTP_BUF_SEQID :
15542 	       NIG_REG_P0_TLLH_PTP_BUF_SEQID, 0x10000);
15543 
15544 	return 0;
15545 }
15546 
15547 /* Called during load, to initialize PTP-related stuff */
15548 void bnx2x_init_ptp(struct bnx2x *bp)
15549 {
15550 	int rc;
15551 
15552 	/* Configure PTP in HW */
15553 	rc = bnx2x_configure_ptp(bp);
15554 	if (rc) {
15555 		BNX2X_ERR("Stopping PTP initialization\n");
15556 		return;
15557 	}
15558 
15559 	/* Init work queue for Tx timestamping */
15560 	INIT_WORK(&bp->ptp_task, bnx2x_ptp_task);
15561 
15562 	/* Init cyclecounter and timecounter. This is done only in the first
15563 	 * load. If done in every load, PTP application will fail when doing
15564 	 * unload / load (e.g. MTU change) while it is running.
15565 	 */
15566 	if (!bp->timecounter_init_done) {
15567 		bnx2x_init_cyclecounter(bp);
15568 		timecounter_init(&bp->timecounter, &bp->cyclecounter,
15569 				 ktime_to_ns(ktime_get_real()));
15570 		bp->timecounter_init_done = 1;
15571 	}
15572 
15573 	DP(BNX2X_MSG_PTP, "PTP initialization ended successfully\n");
15574 }
15575