bnx2x: Add a periodic task for link PHY events
[firefly-linux-kernel-4.4.55.git] / drivers / net / bnx2x / bnx2x_main.c
1 /* bnx2x_main.c: Broadcom Everest network driver.
2  *
3  * Copyright (c) 2007-2011 Broadcom Corporation
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation.
8  *
9  * Maintained by: Eilon Greenstein <eilong@broadcom.com>
10  * Written by: Eliezer Tamir
11  * Based on code from Michael Chan's bnx2 driver
12  * UDP CSUM errata workaround by Arik Gendelman
13  * Slowpath and fastpath rework by Vladislav Zolotarov
14  * Statistics and Link management by Yitchak Gertner
15  *
16  */
17
18 #include <linux/module.h>
19 #include <linux/moduleparam.h>
20 #include <linux/kernel.h>
21 #include <linux/device.h>  /* for dev_info() */
22 #include <linux/timer.h>
23 #include <linux/errno.h>
24 #include <linux/ioport.h>
25 #include <linux/slab.h>
26 #include <linux/interrupt.h>
27 #include <linux/pci.h>
28 #include <linux/init.h>
29 #include <linux/netdevice.h>
30 #include <linux/etherdevice.h>
31 #include <linux/skbuff.h>
32 #include <linux/dma-mapping.h>
33 #include <linux/bitops.h>
34 #include <linux/irq.h>
35 #include <linux/delay.h>
36 #include <asm/byteorder.h>
37 #include <linux/time.h>
38 #include <linux/ethtool.h>
39 #include <linux/mii.h>
40 #include <linux/if_vlan.h>
41 #include <net/ip.h>
42 #include <net/ipv6.h>
43 #include <net/tcp.h>
44 #include <net/checksum.h>
45 #include <net/ip6_checksum.h>
46 #include <linux/workqueue.h>
47 #include <linux/crc32.h>
48 #include <linux/crc32c.h>
49 #include <linux/prefetch.h>
50 #include <linux/zlib.h>
51 #include <linux/io.h>
52 #include <linux/stringify.h>
53
54 #include "bnx2x.h"
55 #include "bnx2x_init.h"
56 #include "bnx2x_init_ops.h"
57 #include "bnx2x_cmn.h"
58 #include "bnx2x_dcb.h"
59 #include "bnx2x_sp.h"
60
61 #include <linux/firmware.h>
62 #include "bnx2x_fw_file_hdr.h"
63 /* FW files */
64 #define FW_FILE_VERSION                                 \
65         __stringify(BCM_5710_FW_MAJOR_VERSION) "."      \
66         __stringify(BCM_5710_FW_MINOR_VERSION) "."      \
67         __stringify(BCM_5710_FW_REVISION_VERSION) "."   \
68         __stringify(BCM_5710_FW_ENGINEERING_VERSION)
69 #define FW_FILE_NAME_E1         "bnx2x/bnx2x-e1-" FW_FILE_VERSION ".fw"
70 #define FW_FILE_NAME_E1H        "bnx2x/bnx2x-e1h-" FW_FILE_VERSION ".fw"
71 #define FW_FILE_NAME_E2         "bnx2x/bnx2x-e2-" FW_FILE_VERSION ".fw"
72
73 /* Time in jiffies before concluding the transmitter is hung */
74 #define TX_TIMEOUT              (5*HZ)
75
76 static char version[] __devinitdata =
77         "Broadcom NetXtreme II 5771x/578xx 10/20-Gigabit Ethernet Driver "
78         DRV_MODULE_NAME " " DRV_MODULE_VERSION " (" DRV_MODULE_RELDATE ")\n";
79
80 MODULE_AUTHOR("Eliezer Tamir");
81 MODULE_DESCRIPTION("Broadcom NetXtreme II "
82                    "BCM57710/57711/57711E/"
83                    "57712/57712_MF/57800/57800_MF/57810/57810_MF/"
84                    "57840/57840_MF Driver");
85 MODULE_LICENSE("GPL");
86 MODULE_VERSION(DRV_MODULE_VERSION);
87 MODULE_FIRMWARE(FW_FILE_NAME_E1);
88 MODULE_FIRMWARE(FW_FILE_NAME_E1H);
89 MODULE_FIRMWARE(FW_FILE_NAME_E2);
90
91 static int multi_mode = 1;
92 module_param(multi_mode, int, 0);
93 MODULE_PARM_DESC(multi_mode, " Multi queue mode "
94                              "(0 Disable; 1 Enable (default))");
95
96 int num_queues;
97 module_param(num_queues, int, 0);
98 MODULE_PARM_DESC(num_queues, " Number of queues for multi_mode=1"
99                                 " (default is as a number of CPUs)");
100
101 static int disable_tpa;
102 module_param(disable_tpa, int, 0);
103 MODULE_PARM_DESC(disable_tpa, " Disable the TPA (LRO) feature");
104
105 #define INT_MODE_INTx                   1
106 #define INT_MODE_MSI                    2
107 static int int_mode;
108 module_param(int_mode, int, 0);
109 MODULE_PARM_DESC(int_mode, " Force interrupt mode other than MSI-X "
110                                 "(1 INT#x; 2 MSI)");
111
112 static int dropless_fc;
113 module_param(dropless_fc, int, 0);
114 MODULE_PARM_DESC(dropless_fc, " Pause on exhausted host ring");
115
116 static int poll;
117 module_param(poll, int, 0);
118 MODULE_PARM_DESC(poll, " Use polling (for debug)");
119
120 static int mrrs = -1;
121 module_param(mrrs, int, 0);
122 MODULE_PARM_DESC(mrrs, " Force Max Read Req Size (0..3) (for debug)");
123
124 static int debug;
125 module_param(debug, int, 0);
126 MODULE_PARM_DESC(debug, " Default debug msglevel");
127
128
129
130 struct workqueue_struct *bnx2x_wq;
131
132 enum bnx2x_board_type {
133         BCM57710 = 0,
134         BCM57711,
135         BCM57711E,
136         BCM57712,
137         BCM57712_MF,
138         BCM57800,
139         BCM57800_MF,
140         BCM57810,
141         BCM57810_MF,
142         BCM57840,
143         BCM57840_MF
144 };
145
146 /* indexed by board_type, above */
147 static struct {
148         char *name;
149 } board_info[] __devinitdata = {
150         { "Broadcom NetXtreme II BCM57710 10 Gigabit PCIe [Everest]" },
151         { "Broadcom NetXtreme II BCM57711 10 Gigabit PCIe" },
152         { "Broadcom NetXtreme II BCM57711E 10 Gigabit PCIe" },
153         { "Broadcom NetXtreme II BCM57712 10 Gigabit Ethernet" },
154         { "Broadcom NetXtreme II BCM57712 10 Gigabit Ethernet Multi Function" },
155         { "Broadcom NetXtreme II BCM57800 10 Gigabit Ethernet" },
156         { "Broadcom NetXtreme II BCM57800 10 Gigabit Ethernet Multi Function" },
157         { "Broadcom NetXtreme II BCM57810 10 Gigabit Ethernet" },
158         { "Broadcom NetXtreme II BCM57810 10 Gigabit Ethernet Multi Function" },
159         { "Broadcom NetXtreme II BCM57840 10/20 Gigabit Ethernet" },
160         { "Broadcom NetXtreme II BCM57840 10/20 Gigabit "
161                                                 "Ethernet Multi Function"}
162 };
163
164 #ifndef PCI_DEVICE_ID_NX2_57710
165 #define PCI_DEVICE_ID_NX2_57710         CHIP_NUM_57710
166 #endif
167 #ifndef PCI_DEVICE_ID_NX2_57711
168 #define PCI_DEVICE_ID_NX2_57711         CHIP_NUM_57711
169 #endif
170 #ifndef PCI_DEVICE_ID_NX2_57711E
171 #define PCI_DEVICE_ID_NX2_57711E        CHIP_NUM_57711E
172 #endif
173 #ifndef PCI_DEVICE_ID_NX2_57712
174 #define PCI_DEVICE_ID_NX2_57712         CHIP_NUM_57712
175 #endif
176 #ifndef PCI_DEVICE_ID_NX2_57712_MF
177 #define PCI_DEVICE_ID_NX2_57712_MF      CHIP_NUM_57712_MF
178 #endif
179 #ifndef PCI_DEVICE_ID_NX2_57800
180 #define PCI_DEVICE_ID_NX2_57800         CHIP_NUM_57800
181 #endif
182 #ifndef PCI_DEVICE_ID_NX2_57800_MF
183 #define PCI_DEVICE_ID_NX2_57800_MF      CHIP_NUM_57800_MF
184 #endif
185 #ifndef PCI_DEVICE_ID_NX2_57810
186 #define PCI_DEVICE_ID_NX2_57810         CHIP_NUM_57810
187 #endif
188 #ifndef PCI_DEVICE_ID_NX2_57810_MF
189 #define PCI_DEVICE_ID_NX2_57810_MF      CHIP_NUM_57810_MF
190 #endif
191 #ifndef PCI_DEVICE_ID_NX2_57840
192 #define PCI_DEVICE_ID_NX2_57840         CHIP_NUM_57840
193 #endif
194 #ifndef PCI_DEVICE_ID_NX2_57840_MF
195 #define PCI_DEVICE_ID_NX2_57840_MF      CHIP_NUM_57840_MF
196 #endif
197 static DEFINE_PCI_DEVICE_TABLE(bnx2x_pci_tbl) = {
198         { PCI_VDEVICE(BROADCOM, PCI_DEVICE_ID_NX2_57710), BCM57710 },
199         { PCI_VDEVICE(BROADCOM, PCI_DEVICE_ID_NX2_57711), BCM57711 },
200         { PCI_VDEVICE(BROADCOM, PCI_DEVICE_ID_NX2_57711E), BCM57711E },
201         { PCI_VDEVICE(BROADCOM, PCI_DEVICE_ID_NX2_57712), BCM57712 },
202         { PCI_VDEVICE(BROADCOM, PCI_DEVICE_ID_NX2_57712_MF), BCM57712_MF },
203         { PCI_VDEVICE(BROADCOM, PCI_DEVICE_ID_NX2_57800), BCM57800 },
204         { PCI_VDEVICE(BROADCOM, PCI_DEVICE_ID_NX2_57800_MF), BCM57800_MF },
205         { PCI_VDEVICE(BROADCOM, PCI_DEVICE_ID_NX2_57810), BCM57810 },
206         { PCI_VDEVICE(BROADCOM, PCI_DEVICE_ID_NX2_57810_MF), BCM57810_MF },
207         { PCI_VDEVICE(BROADCOM, PCI_DEVICE_ID_NX2_57840), BCM57840 },
208         { PCI_VDEVICE(BROADCOM, PCI_DEVICE_ID_NX2_57840_MF), BCM57840_MF },
209         { 0 }
210 };
211
212 MODULE_DEVICE_TABLE(pci, bnx2x_pci_tbl);
213
214 /****************************************************************************
215 * General service functions
216 ****************************************************************************/
217
218 static inline void __storm_memset_dma_mapping(struct bnx2x *bp,
219                                        u32 addr, dma_addr_t mapping)
220 {
221         REG_WR(bp,  addr, U64_LO(mapping));
222         REG_WR(bp,  addr + 4, U64_HI(mapping));
223 }
224
225 static inline void storm_memset_spq_addr(struct bnx2x *bp,
226                                          dma_addr_t mapping, u16 abs_fid)
227 {
228         u32 addr = XSEM_REG_FAST_MEMORY +
229                         XSTORM_SPQ_PAGE_BASE_OFFSET(abs_fid);
230
231         __storm_memset_dma_mapping(bp, addr, mapping);
232 }
233
234 static inline void storm_memset_vf_to_pf(struct bnx2x *bp, u16 abs_fid,
235                                          u16 pf_id)
236 {
237         REG_WR8(bp, BAR_XSTRORM_INTMEM + XSTORM_VF_TO_PF_OFFSET(abs_fid),
238                 pf_id);
239         REG_WR8(bp, BAR_CSTRORM_INTMEM + CSTORM_VF_TO_PF_OFFSET(abs_fid),
240                 pf_id);
241         REG_WR8(bp, BAR_TSTRORM_INTMEM + TSTORM_VF_TO_PF_OFFSET(abs_fid),
242                 pf_id);
243         REG_WR8(bp, BAR_USTRORM_INTMEM + USTORM_VF_TO_PF_OFFSET(abs_fid),
244                 pf_id);
245 }
246
247 static inline void storm_memset_func_en(struct bnx2x *bp, u16 abs_fid,
248                                         u8 enable)
249 {
250         REG_WR8(bp, BAR_XSTRORM_INTMEM + XSTORM_FUNC_EN_OFFSET(abs_fid),
251                 enable);
252         REG_WR8(bp, BAR_CSTRORM_INTMEM + CSTORM_FUNC_EN_OFFSET(abs_fid),
253                 enable);
254         REG_WR8(bp, BAR_TSTRORM_INTMEM + TSTORM_FUNC_EN_OFFSET(abs_fid),
255                 enable);
256         REG_WR8(bp, BAR_USTRORM_INTMEM + USTORM_FUNC_EN_OFFSET(abs_fid),
257                 enable);
258 }
259
260 static inline void storm_memset_eq_data(struct bnx2x *bp,
261                                 struct event_ring_data *eq_data,
262                                 u16 pfid)
263 {
264         size_t size = sizeof(struct event_ring_data);
265
266         u32 addr = BAR_CSTRORM_INTMEM + CSTORM_EVENT_RING_DATA_OFFSET(pfid);
267
268         __storm_memset_struct(bp, addr, size, (u32 *)eq_data);
269 }
270
271 static inline void storm_memset_eq_prod(struct bnx2x *bp, u16 eq_prod,
272                                         u16 pfid)
273 {
274         u32 addr = BAR_CSTRORM_INTMEM + CSTORM_EVENT_RING_PROD_OFFSET(pfid);
275         REG_WR16(bp, addr, eq_prod);
276 }
277
278 /* used only at init
279  * locking is done by mcp
280  */
281 static void bnx2x_reg_wr_ind(struct bnx2x *bp, u32 addr, u32 val)
282 {
283         pci_write_config_dword(bp->pdev, PCICFG_GRC_ADDRESS, addr);
284         pci_write_config_dword(bp->pdev, PCICFG_GRC_DATA, val);
285         pci_write_config_dword(bp->pdev, PCICFG_GRC_ADDRESS,
286                                PCICFG_VENDOR_ID_OFFSET);
287 }
288
289 static u32 bnx2x_reg_rd_ind(struct bnx2x *bp, u32 addr)
290 {
291         u32 val;
292
293         pci_write_config_dword(bp->pdev, PCICFG_GRC_ADDRESS, addr);
294         pci_read_config_dword(bp->pdev, PCICFG_GRC_DATA, &val);
295         pci_write_config_dword(bp->pdev, PCICFG_GRC_ADDRESS,
296                                PCICFG_VENDOR_ID_OFFSET);
297
298         return val;
299 }
300
301 #define DMAE_DP_SRC_GRC         "grc src_addr [%08x]"
302 #define DMAE_DP_SRC_PCI         "pci src_addr [%x:%08x]"
303 #define DMAE_DP_DST_GRC         "grc dst_addr [%08x]"
304 #define DMAE_DP_DST_PCI         "pci dst_addr [%x:%08x]"
305 #define DMAE_DP_DST_NONE        "dst_addr [none]"
306
307 static void bnx2x_dp_dmae(struct bnx2x *bp, struct dmae_command *dmae,
308                           int msglvl)
309 {
310         u32 src_type = dmae->opcode & DMAE_COMMAND_SRC;
311
312         switch (dmae->opcode & DMAE_COMMAND_DST) {
313         case DMAE_CMD_DST_PCI:
314                 if (src_type == DMAE_CMD_SRC_PCI)
315                         DP(msglvl, "DMAE: opcode 0x%08x\n"
316                            "src [%x:%08x], len [%d*4], dst [%x:%08x]\n"
317                            "comp_addr [%x:%08x], comp_val 0x%08x\n",
318                            dmae->opcode, dmae->src_addr_hi, dmae->src_addr_lo,
319                            dmae->len, dmae->dst_addr_hi, dmae->dst_addr_lo,
320                            dmae->comp_addr_hi, dmae->comp_addr_lo,
321                            dmae->comp_val);
322                 else
323                         DP(msglvl, "DMAE: opcode 0x%08x\n"
324                            "src [%08x], len [%d*4], dst [%x:%08x]\n"
325                            "comp_addr [%x:%08x], comp_val 0x%08x\n",
326                            dmae->opcode, dmae->src_addr_lo >> 2,
327                            dmae->len, dmae->dst_addr_hi, dmae->dst_addr_lo,
328                            dmae->comp_addr_hi, dmae->comp_addr_lo,
329                            dmae->comp_val);
330                 break;
331         case DMAE_CMD_DST_GRC:
332                 if (src_type == DMAE_CMD_SRC_PCI)
333                         DP(msglvl, "DMAE: opcode 0x%08x\n"
334                            "src [%x:%08x], len [%d*4], dst_addr [%08x]\n"
335                            "comp_addr [%x:%08x], comp_val 0x%08x\n",
336                            dmae->opcode, dmae->src_addr_hi, dmae->src_addr_lo,
337                            dmae->len, dmae->dst_addr_lo >> 2,
338                            dmae->comp_addr_hi, dmae->comp_addr_lo,
339                            dmae->comp_val);
340                 else
341                         DP(msglvl, "DMAE: opcode 0x%08x\n"
342                            "src [%08x], len [%d*4], dst [%08x]\n"
343                            "comp_addr [%x:%08x], comp_val 0x%08x\n",
344                            dmae->opcode, dmae->src_addr_lo >> 2,
345                            dmae->len, dmae->dst_addr_lo >> 2,
346                            dmae->comp_addr_hi, dmae->comp_addr_lo,
347                            dmae->comp_val);
348                 break;
349         default:
350                 if (src_type == DMAE_CMD_SRC_PCI)
351                         DP(msglvl, "DMAE: opcode 0x%08x\n"
352                            DP_LEVEL "src_addr [%x:%08x]  len [%d * 4]  "
353                                     "dst_addr [none]\n"
354                            DP_LEVEL "comp_addr [%x:%08x]  comp_val 0x%08x\n",
355                            dmae->opcode, dmae->src_addr_hi, dmae->src_addr_lo,
356                            dmae->len, dmae->comp_addr_hi, dmae->comp_addr_lo,
357                            dmae->comp_val);
358                 else
359                         DP(msglvl, "DMAE: opcode 0x%08x\n"
360                            DP_LEVEL "src_addr [%08x]  len [%d * 4]  "
361                                     "dst_addr [none]\n"
362                            DP_LEVEL "comp_addr [%x:%08x]  comp_val 0x%08x\n",
363                            dmae->opcode, dmae->src_addr_lo >> 2,
364                            dmae->len, dmae->comp_addr_hi, dmae->comp_addr_lo,
365                            dmae->comp_val);
366                 break;
367         }
368
369 }
370
371 /* copy command into DMAE command memory and set DMAE command go */
372 void bnx2x_post_dmae(struct bnx2x *bp, struct dmae_command *dmae, int idx)
373 {
374         u32 cmd_offset;
375         int i;
376
377         cmd_offset = (DMAE_REG_CMD_MEM + sizeof(struct dmae_command) * idx);
378         for (i = 0; i < (sizeof(struct dmae_command)/4); i++) {
379                 REG_WR(bp, cmd_offset + i*4, *(((u32 *)dmae) + i));
380
381                 DP(BNX2X_MSG_OFF, "DMAE cmd[%d].%d (0x%08x) : 0x%08x\n",
382                    idx, i, cmd_offset + i*4, *(((u32 *)dmae) + i));
383         }
384         REG_WR(bp, dmae_reg_go_c[idx], 1);
385 }
386
387 u32 bnx2x_dmae_opcode_add_comp(u32 opcode, u8 comp_type)
388 {
389         return opcode | ((comp_type << DMAE_COMMAND_C_DST_SHIFT) |
390                            DMAE_CMD_C_ENABLE);
391 }
392
393 u32 bnx2x_dmae_opcode_clr_src_reset(u32 opcode)
394 {
395         return opcode & ~DMAE_CMD_SRC_RESET;
396 }
397
398 u32 bnx2x_dmae_opcode(struct bnx2x *bp, u8 src_type, u8 dst_type,
399                              bool with_comp, u8 comp_type)
400 {
401         u32 opcode = 0;
402
403         opcode |= ((src_type << DMAE_COMMAND_SRC_SHIFT) |
404                    (dst_type << DMAE_COMMAND_DST_SHIFT));
405
406         opcode |= (DMAE_CMD_SRC_RESET | DMAE_CMD_DST_RESET);
407
408         opcode |= (BP_PORT(bp) ? DMAE_CMD_PORT_1 : DMAE_CMD_PORT_0);
409         opcode |= ((BP_E1HVN(bp) << DMAE_CMD_E1HVN_SHIFT) |
410                    (BP_E1HVN(bp) << DMAE_COMMAND_DST_VN_SHIFT));
411         opcode |= (DMAE_COM_SET_ERR << DMAE_COMMAND_ERR_POLICY_SHIFT);
412
413 #ifdef __BIG_ENDIAN
414         opcode |= DMAE_CMD_ENDIANITY_B_DW_SWAP;
415 #else
416         opcode |= DMAE_CMD_ENDIANITY_DW_SWAP;
417 #endif
418         if (with_comp)
419                 opcode = bnx2x_dmae_opcode_add_comp(opcode, comp_type);
420         return opcode;
421 }
422
423 static void bnx2x_prep_dmae_with_comp(struct bnx2x *bp,
424                                       struct dmae_command *dmae,
425                                       u8 src_type, u8 dst_type)
426 {
427         memset(dmae, 0, sizeof(struct dmae_command));
428
429         /* set the opcode */
430         dmae->opcode = bnx2x_dmae_opcode(bp, src_type, dst_type,
431                                          true, DMAE_COMP_PCI);
432
433         /* fill in the completion parameters */
434         dmae->comp_addr_lo = U64_LO(bnx2x_sp_mapping(bp, wb_comp));
435         dmae->comp_addr_hi = U64_HI(bnx2x_sp_mapping(bp, wb_comp));
436         dmae->comp_val = DMAE_COMP_VAL;
437 }
438
439 /* issue a dmae command over the init-channel and wailt for completion */
440 static int bnx2x_issue_dmae_with_comp(struct bnx2x *bp,
441                                       struct dmae_command *dmae)
442 {
443         u32 *wb_comp = bnx2x_sp(bp, wb_comp);
444         int cnt = CHIP_REV_IS_SLOW(bp) ? (400000) : 4000;
445         int rc = 0;
446
447         DP(BNX2X_MSG_OFF, "data before [0x%08x 0x%08x 0x%08x 0x%08x]\n",
448            bp->slowpath->wb_data[0], bp->slowpath->wb_data[1],
449            bp->slowpath->wb_data[2], bp->slowpath->wb_data[3]);
450
451         /*
452          * Lock the dmae channel. Disable BHs to prevent a dead-lock
453          * as long as this code is called both from syscall context and
454          * from ndo_set_rx_mode() flow that may be called from BH.
455          */
456         spin_lock_bh(&bp->dmae_lock);
457
458         /* reset completion */
459         *wb_comp = 0;
460
461         /* post the command on the channel used for initializations */
462         bnx2x_post_dmae(bp, dmae, INIT_DMAE_C(bp));
463
464         /* wait for completion */
465         udelay(5);
466         while ((*wb_comp & ~DMAE_PCI_ERR_FLAG) != DMAE_COMP_VAL) {
467                 DP(BNX2X_MSG_OFF, "wb_comp 0x%08x\n", *wb_comp);
468
469                 if (!cnt) {
470                         BNX2X_ERR("DMAE timeout!\n");
471                         rc = DMAE_TIMEOUT;
472                         goto unlock;
473                 }
474                 cnt--;
475                 udelay(50);
476         }
477         if (*wb_comp & DMAE_PCI_ERR_FLAG) {
478                 BNX2X_ERR("DMAE PCI error!\n");
479                 rc = DMAE_PCI_ERROR;
480         }
481
482         DP(BNX2X_MSG_OFF, "data after [0x%08x 0x%08x 0x%08x 0x%08x]\n",
483            bp->slowpath->wb_data[0], bp->slowpath->wb_data[1],
484            bp->slowpath->wb_data[2], bp->slowpath->wb_data[3]);
485
486 unlock:
487         spin_unlock_bh(&bp->dmae_lock);
488         return rc;
489 }
490
491 void bnx2x_write_dmae(struct bnx2x *bp, dma_addr_t dma_addr, u32 dst_addr,
492                       u32 len32)
493 {
494         struct dmae_command dmae;
495
496         if (!bp->dmae_ready) {
497                 u32 *data = bnx2x_sp(bp, wb_data[0]);
498
499                 DP(BNX2X_MSG_OFF, "DMAE is not ready (dst_addr %08x  len32 %d)"
500                    "  using indirect\n", dst_addr, len32);
501                 bnx2x_init_ind_wr(bp, dst_addr, data, len32);
502                 return;
503         }
504
505         /* set opcode and fixed command fields */
506         bnx2x_prep_dmae_with_comp(bp, &dmae, DMAE_SRC_PCI, DMAE_DST_GRC);
507
508         /* fill in addresses and len */
509         dmae.src_addr_lo = U64_LO(dma_addr);
510         dmae.src_addr_hi = U64_HI(dma_addr);
511         dmae.dst_addr_lo = dst_addr >> 2;
512         dmae.dst_addr_hi = 0;
513         dmae.len = len32;
514
515         bnx2x_dp_dmae(bp, &dmae, BNX2X_MSG_OFF);
516
517         /* issue the command and wait for completion */
518         bnx2x_issue_dmae_with_comp(bp, &dmae);
519 }
520
521 void bnx2x_read_dmae(struct bnx2x *bp, u32 src_addr, u32 len32)
522 {
523         struct dmae_command dmae;
524
525         if (!bp->dmae_ready) {
526                 u32 *data = bnx2x_sp(bp, wb_data[0]);
527                 int i;
528
529                 DP(BNX2X_MSG_OFF, "DMAE is not ready (src_addr %08x  len32 %d)"
530                    "  using indirect\n", src_addr, len32);
531                 for (i = 0; i < len32; i++)
532                         data[i] = bnx2x_reg_rd_ind(bp, src_addr + i*4);
533                 return;
534         }
535
536         /* set opcode and fixed command fields */
537         bnx2x_prep_dmae_with_comp(bp, &dmae, DMAE_SRC_GRC, DMAE_DST_PCI);
538
539         /* fill in addresses and len */
540         dmae.src_addr_lo = src_addr >> 2;
541         dmae.src_addr_hi = 0;
542         dmae.dst_addr_lo = U64_LO(bnx2x_sp_mapping(bp, wb_data));
543         dmae.dst_addr_hi = U64_HI(bnx2x_sp_mapping(bp, wb_data));
544         dmae.len = len32;
545
546         bnx2x_dp_dmae(bp, &dmae, BNX2X_MSG_OFF);
547
548         /* issue the command and wait for completion */
549         bnx2x_issue_dmae_with_comp(bp, &dmae);
550 }
551
552 static void bnx2x_write_dmae_phys_len(struct bnx2x *bp, dma_addr_t phys_addr,
553                                       u32 addr, u32 len)
554 {
555         int dmae_wr_max = DMAE_LEN32_WR_MAX(bp);
556         int offset = 0;
557
558         while (len > dmae_wr_max) {
559                 bnx2x_write_dmae(bp, phys_addr + offset,
560                                  addr + offset, dmae_wr_max);
561                 offset += dmae_wr_max * 4;
562                 len -= dmae_wr_max;
563         }
564
565         bnx2x_write_dmae(bp, phys_addr + offset, addr + offset, len);
566 }
567
568 /* used only for slowpath so not inlined */
569 static void bnx2x_wb_wr(struct bnx2x *bp, int reg, u32 val_hi, u32 val_lo)
570 {
571         u32 wb_write[2];
572
573         wb_write[0] = val_hi;
574         wb_write[1] = val_lo;
575         REG_WR_DMAE(bp, reg, wb_write, 2);
576 }
577
578 #ifdef USE_WB_RD
579 static u64 bnx2x_wb_rd(struct bnx2x *bp, int reg)
580 {
581         u32 wb_data[2];
582
583         REG_RD_DMAE(bp, reg, wb_data, 2);
584
585         return HILO_U64(wb_data[0], wb_data[1]);
586 }
587 #endif
588
589 static int bnx2x_mc_assert(struct bnx2x *bp)
590 {
591         char last_idx;
592         int i, rc = 0;
593         u32 row0, row1, row2, row3;
594
595         /* XSTORM */
596         last_idx = REG_RD8(bp, BAR_XSTRORM_INTMEM +
597                            XSTORM_ASSERT_LIST_INDEX_OFFSET);
598         if (last_idx)
599                 BNX2X_ERR("XSTORM_ASSERT_LIST_INDEX 0x%x\n", last_idx);
600
601         /* print the asserts */
602         for (i = 0; i < STROM_ASSERT_ARRAY_SIZE; i++) {
603
604                 row0 = REG_RD(bp, BAR_XSTRORM_INTMEM +
605                               XSTORM_ASSERT_LIST_OFFSET(i));
606                 row1 = REG_RD(bp, BAR_XSTRORM_INTMEM +
607                               XSTORM_ASSERT_LIST_OFFSET(i) + 4);
608                 row2 = REG_RD(bp, BAR_XSTRORM_INTMEM +
609                               XSTORM_ASSERT_LIST_OFFSET(i) + 8);
610                 row3 = REG_RD(bp, BAR_XSTRORM_INTMEM +
611                               XSTORM_ASSERT_LIST_OFFSET(i) + 12);
612
613                 if (row0 != COMMON_ASM_INVALID_ASSERT_OPCODE) {
614                         BNX2X_ERR("XSTORM_ASSERT_INDEX 0x%x = 0x%08x"
615                                   " 0x%08x 0x%08x 0x%08x\n",
616                                   i, row3, row2, row1, row0);
617                         rc++;
618                 } else {
619                         break;
620                 }
621         }
622
623         /* TSTORM */
624         last_idx = REG_RD8(bp, BAR_TSTRORM_INTMEM +
625                            TSTORM_ASSERT_LIST_INDEX_OFFSET);
626         if (last_idx)
627                 BNX2X_ERR("TSTORM_ASSERT_LIST_INDEX 0x%x\n", last_idx);
628
629         /* print the asserts */
630         for (i = 0; i < STROM_ASSERT_ARRAY_SIZE; i++) {
631
632                 row0 = REG_RD(bp, BAR_TSTRORM_INTMEM +
633                               TSTORM_ASSERT_LIST_OFFSET(i));
634                 row1 = REG_RD(bp, BAR_TSTRORM_INTMEM +
635                               TSTORM_ASSERT_LIST_OFFSET(i) + 4);
636                 row2 = REG_RD(bp, BAR_TSTRORM_INTMEM +
637                               TSTORM_ASSERT_LIST_OFFSET(i) + 8);
638                 row3 = REG_RD(bp, BAR_TSTRORM_INTMEM +
639                               TSTORM_ASSERT_LIST_OFFSET(i) + 12);
640
641                 if (row0 != COMMON_ASM_INVALID_ASSERT_OPCODE) {
642                         BNX2X_ERR("TSTORM_ASSERT_INDEX 0x%x = 0x%08x"
643                                   " 0x%08x 0x%08x 0x%08x\n",
644                                   i, row3, row2, row1, row0);
645                         rc++;
646                 } else {
647                         break;
648                 }
649         }
650
651         /* CSTORM */
652         last_idx = REG_RD8(bp, BAR_CSTRORM_INTMEM +
653                            CSTORM_ASSERT_LIST_INDEX_OFFSET);
654         if (last_idx)
655                 BNX2X_ERR("CSTORM_ASSERT_LIST_INDEX 0x%x\n", last_idx);
656
657         /* print the asserts */
658         for (i = 0; i < STROM_ASSERT_ARRAY_SIZE; i++) {
659
660                 row0 = REG_RD(bp, BAR_CSTRORM_INTMEM +
661                               CSTORM_ASSERT_LIST_OFFSET(i));
662                 row1 = REG_RD(bp, BAR_CSTRORM_INTMEM +
663                               CSTORM_ASSERT_LIST_OFFSET(i) + 4);
664                 row2 = REG_RD(bp, BAR_CSTRORM_INTMEM +
665                               CSTORM_ASSERT_LIST_OFFSET(i) + 8);
666                 row3 = REG_RD(bp, BAR_CSTRORM_INTMEM +
667                               CSTORM_ASSERT_LIST_OFFSET(i) + 12);
668
669                 if (row0 != COMMON_ASM_INVALID_ASSERT_OPCODE) {
670                         BNX2X_ERR("CSTORM_ASSERT_INDEX 0x%x = 0x%08x"
671                                   " 0x%08x 0x%08x 0x%08x\n",
672                                   i, row3, row2, row1, row0);
673                         rc++;
674                 } else {
675                         break;
676                 }
677         }
678
679         /* USTORM */
680         last_idx = REG_RD8(bp, BAR_USTRORM_INTMEM +
681                            USTORM_ASSERT_LIST_INDEX_OFFSET);
682         if (last_idx)
683                 BNX2X_ERR("USTORM_ASSERT_LIST_INDEX 0x%x\n", last_idx);
684
685         /* print the asserts */
686         for (i = 0; i < STROM_ASSERT_ARRAY_SIZE; i++) {
687
688                 row0 = REG_RD(bp, BAR_USTRORM_INTMEM +
689                               USTORM_ASSERT_LIST_OFFSET(i));
690                 row1 = REG_RD(bp, BAR_USTRORM_INTMEM +
691                               USTORM_ASSERT_LIST_OFFSET(i) + 4);
692                 row2 = REG_RD(bp, BAR_USTRORM_INTMEM +
693                               USTORM_ASSERT_LIST_OFFSET(i) + 8);
694                 row3 = REG_RD(bp, BAR_USTRORM_INTMEM +
695                               USTORM_ASSERT_LIST_OFFSET(i) + 12);
696
697                 if (row0 != COMMON_ASM_INVALID_ASSERT_OPCODE) {
698                         BNX2X_ERR("USTORM_ASSERT_INDEX 0x%x = 0x%08x"
699                                   " 0x%08x 0x%08x 0x%08x\n",
700                                   i, row3, row2, row1, row0);
701                         rc++;
702                 } else {
703                         break;
704                 }
705         }
706
707         return rc;
708 }
709
710 void bnx2x_fw_dump_lvl(struct bnx2x *bp, const char *lvl)
711 {
712         u32 addr, val;
713         u32 mark, offset;
714         __be32 data[9];
715         int word;
716         u32 trace_shmem_base;
717         if (BP_NOMCP(bp)) {
718                 BNX2X_ERR("NO MCP - can not dump\n");
719                 return;
720         }
721         netdev_printk(lvl, bp->dev, "bc %d.%d.%d\n",
722                 (bp->common.bc_ver & 0xff0000) >> 16,
723                 (bp->common.bc_ver & 0xff00) >> 8,
724                 (bp->common.bc_ver & 0xff));
725
726         val = REG_RD(bp, MCP_REG_MCPR_CPU_PROGRAM_COUNTER);
727         if (val == REG_RD(bp, MCP_REG_MCPR_CPU_PROGRAM_COUNTER))
728                 printk("%s" "MCP PC at 0x%x\n", lvl, val);
729
730         if (BP_PATH(bp) == 0)
731                 trace_shmem_base = bp->common.shmem_base;
732         else
733                 trace_shmem_base = SHMEM2_RD(bp, other_shmem_base_addr);
734         addr = trace_shmem_base - 0x0800 + 4;
735         mark = REG_RD(bp, addr);
736         mark = (CHIP_IS_E1x(bp) ? MCP_REG_MCPR_SCRATCH : MCP_A_REG_MCPR_SCRATCH)
737                         + ((mark + 0x3) & ~0x3) - 0x08000000;
738         printk("%s" "begin fw dump (mark 0x%x)\n", lvl, mark);
739
740         printk("%s", lvl);
741         for (offset = mark; offset <= trace_shmem_base; offset += 0x8*4) {
742                 for (word = 0; word < 8; word++)
743                         data[word] = htonl(REG_RD(bp, offset + 4*word));
744                 data[8] = 0x0;
745                 pr_cont("%s", (char *)data);
746         }
747         for (offset = addr + 4; offset <= mark; offset += 0x8*4) {
748                 for (word = 0; word < 8; word++)
749                         data[word] = htonl(REG_RD(bp, offset + 4*word));
750                 data[8] = 0x0;
751                 pr_cont("%s", (char *)data);
752         }
753         printk("%s" "end of fw dump\n", lvl);
754 }
755
756 static inline void bnx2x_fw_dump(struct bnx2x *bp)
757 {
758         bnx2x_fw_dump_lvl(bp, KERN_ERR);
759 }
760
761 void bnx2x_panic_dump(struct bnx2x *bp)
762 {
763         int i;
764         u16 j;
765         struct hc_sp_status_block_data sp_sb_data;
766         int func = BP_FUNC(bp);
767 #ifdef BNX2X_STOP_ON_ERROR
768         u16 start = 0, end = 0;
769 #endif
770
771         bp->stats_state = STATS_STATE_DISABLED;
772         DP(BNX2X_MSG_STATS, "stats_state - DISABLED\n");
773
774         BNX2X_ERR("begin crash dump -----------------\n");
775
776         /* Indices */
777         /* Common */
778         BNX2X_ERR("def_idx(0x%x)  def_att_idx(0x%x)  attn_state(0x%x)"
779                   "  spq_prod_idx(0x%x) next_stats_cnt(0x%x)\n",
780                   bp->def_idx, bp->def_att_idx, bp->attn_state,
781                   bp->spq_prod_idx, bp->stats_counter);
782         BNX2X_ERR("DSB: attn bits(0x%x)  ack(0x%x)  id(0x%x)  idx(0x%x)\n",
783                   bp->def_status_blk->atten_status_block.attn_bits,
784                   bp->def_status_blk->atten_status_block.attn_bits_ack,
785                   bp->def_status_blk->atten_status_block.status_block_id,
786                   bp->def_status_blk->atten_status_block.attn_bits_index);
787         BNX2X_ERR("     def (");
788         for (i = 0; i < HC_SP_SB_MAX_INDICES; i++)
789                 pr_cont("0x%x%s",
790                        bp->def_status_blk->sp_sb.index_values[i],
791                        (i == HC_SP_SB_MAX_INDICES - 1) ? ")  " : " ");
792
793         for (i = 0; i < sizeof(struct hc_sp_status_block_data)/sizeof(u32); i++)
794                 *((u32 *)&sp_sb_data + i) = REG_RD(bp, BAR_CSTRORM_INTMEM +
795                         CSTORM_SP_STATUS_BLOCK_DATA_OFFSET(func) +
796                         i*sizeof(u32));
797
798         pr_cont("igu_sb_id(0x%x)  igu_seg_id(0x%x) "
799                          "pf_id(0x%x)  vnic_id(0x%x)  "
800                          "vf_id(0x%x)  vf_valid (0x%x) "
801                          "state(0x%x)\n",
802                sp_sb_data.igu_sb_id,
803                sp_sb_data.igu_seg_id,
804                sp_sb_data.p_func.pf_id,
805                sp_sb_data.p_func.vnic_id,
806                sp_sb_data.p_func.vf_id,
807                sp_sb_data.p_func.vf_valid,
808                sp_sb_data.state);
809
810
811         for_each_eth_queue(bp, i) {
812                 struct bnx2x_fastpath *fp = &bp->fp[i];
813                 int loop;
814                 struct hc_status_block_data_e2 sb_data_e2;
815                 struct hc_status_block_data_e1x sb_data_e1x;
816                 struct hc_status_block_sm  *hc_sm_p =
817                         CHIP_IS_E1x(bp) ?
818                         sb_data_e1x.common.state_machine :
819                         sb_data_e2.common.state_machine;
820                 struct hc_index_data *hc_index_p =
821                         CHIP_IS_E1x(bp) ?
822                         sb_data_e1x.index_data :
823                         sb_data_e2.index_data;
824                 int data_size;
825                 u32 *sb_data_p;
826
827                 /* Rx */
828                 BNX2X_ERR("fp%d: rx_bd_prod(0x%x)  rx_bd_cons(0x%x)"
829                           "  rx_comp_prod(0x%x)"
830                           "  rx_comp_cons(0x%x)  *rx_cons_sb(0x%x)\n",
831                           i, fp->rx_bd_prod, fp->rx_bd_cons,
832                           fp->rx_comp_prod,
833                           fp->rx_comp_cons, le16_to_cpu(*fp->rx_cons_sb));
834                 BNX2X_ERR("     rx_sge_prod(0x%x)  last_max_sge(0x%x)"
835                           "  fp_hc_idx(0x%x)\n",
836                           fp->rx_sge_prod, fp->last_max_sge,
837                           le16_to_cpu(fp->fp_hc_idx));
838
839                 /* Tx */
840                 BNX2X_ERR("fp%d: tx_pkt_prod(0x%x)  tx_pkt_cons(0x%x)"
841                           "  tx_bd_prod(0x%x)  tx_bd_cons(0x%x)"
842                           "  *tx_cons_sb(0x%x)\n",
843                           i, fp->tx_pkt_prod, fp->tx_pkt_cons, fp->tx_bd_prod,
844                           fp->tx_bd_cons, le16_to_cpu(*fp->tx_cons_sb));
845
846                 loop = CHIP_IS_E1x(bp) ?
847                         HC_SB_MAX_INDICES_E1X : HC_SB_MAX_INDICES_E2;
848
849                 /* host sb data */
850
851 #ifdef BCM_CNIC
852                 if (IS_FCOE_FP(fp))
853                         continue;
854 #endif
855                 BNX2X_ERR("     run indexes (");
856                 for (j = 0; j < HC_SB_MAX_SM; j++)
857                         pr_cont("0x%x%s",
858                                fp->sb_running_index[j],
859                                (j == HC_SB_MAX_SM - 1) ? ")" : " ");
860
861                 BNX2X_ERR("     indexes (");
862                 for (j = 0; j < loop; j++)
863                         pr_cont("0x%x%s",
864                                fp->sb_index_values[j],
865                                (j == loop - 1) ? ")" : " ");
866                 /* fw sb data */
867                 data_size = CHIP_IS_E1x(bp) ?
868                         sizeof(struct hc_status_block_data_e1x) :
869                         sizeof(struct hc_status_block_data_e2);
870                 data_size /= sizeof(u32);
871                 sb_data_p = CHIP_IS_E1x(bp) ?
872                         (u32 *)&sb_data_e1x :
873                         (u32 *)&sb_data_e2;
874                 /* copy sb data in here */
875                 for (j = 0; j < data_size; j++)
876                         *(sb_data_p + j) = REG_RD(bp, BAR_CSTRORM_INTMEM +
877                                 CSTORM_STATUS_BLOCK_DATA_OFFSET(fp->fw_sb_id) +
878                                 j * sizeof(u32));
879
880                 if (!CHIP_IS_E1x(bp)) {
881                         pr_cont("pf_id(0x%x)  vf_id(0x%x)  vf_valid(0x%x) "
882                                 "vnic_id(0x%x)  same_igu_sb_1b(0x%x) "
883                                 "state(0x%x)\n",
884                                 sb_data_e2.common.p_func.pf_id,
885                                 sb_data_e2.common.p_func.vf_id,
886                                 sb_data_e2.common.p_func.vf_valid,
887                                 sb_data_e2.common.p_func.vnic_id,
888                                 sb_data_e2.common.same_igu_sb_1b,
889                                 sb_data_e2.common.state);
890                 } else {
891                         pr_cont("pf_id(0x%x)  vf_id(0x%x)  vf_valid(0x%x) "
892                                 "vnic_id(0x%x)  same_igu_sb_1b(0x%x) "
893                                 "state(0x%x)\n",
894                                 sb_data_e1x.common.p_func.pf_id,
895                                 sb_data_e1x.common.p_func.vf_id,
896                                 sb_data_e1x.common.p_func.vf_valid,
897                                 sb_data_e1x.common.p_func.vnic_id,
898                                 sb_data_e1x.common.same_igu_sb_1b,
899                                 sb_data_e1x.common.state);
900                 }
901
902                 /* SB_SMs data */
903                 for (j = 0; j < HC_SB_MAX_SM; j++) {
904                         pr_cont("SM[%d] __flags (0x%x) "
905                                "igu_sb_id (0x%x)  igu_seg_id(0x%x) "
906                                "time_to_expire (0x%x) "
907                                "timer_value(0x%x)\n", j,
908                                hc_sm_p[j].__flags,
909                                hc_sm_p[j].igu_sb_id,
910                                hc_sm_p[j].igu_seg_id,
911                                hc_sm_p[j].time_to_expire,
912                                hc_sm_p[j].timer_value);
913                 }
914
915                 /* Indecies data */
916                 for (j = 0; j < loop; j++) {
917                         pr_cont("INDEX[%d] flags (0x%x) "
918                                          "timeout (0x%x)\n", j,
919                                hc_index_p[j].flags,
920                                hc_index_p[j].timeout);
921                 }
922         }
923
924 #ifdef BNX2X_STOP_ON_ERROR
925         /* Rings */
926         /* Rx */
927         for_each_rx_queue(bp, i) {
928                 struct bnx2x_fastpath *fp = &bp->fp[i];
929
930                 start = RX_BD(le16_to_cpu(*fp->rx_cons_sb) - 10);
931                 end = RX_BD(le16_to_cpu(*fp->rx_cons_sb) + 503);
932                 for (j = start; j != end; j = RX_BD(j + 1)) {
933                         u32 *rx_bd = (u32 *)&fp->rx_desc_ring[j];
934                         struct sw_rx_bd *sw_bd = &fp->rx_buf_ring[j];
935
936                         BNX2X_ERR("fp%d: rx_bd[%x]=[%x:%x]  sw_bd=[%p]\n",
937                                   i, j, rx_bd[1], rx_bd[0], sw_bd->skb);
938                 }
939
940                 start = RX_SGE(fp->rx_sge_prod);
941                 end = RX_SGE(fp->last_max_sge);
942                 for (j = start; j != end; j = RX_SGE(j + 1)) {
943                         u32 *rx_sge = (u32 *)&fp->rx_sge_ring[j];
944                         struct sw_rx_page *sw_page = &fp->rx_page_ring[j];
945
946                         BNX2X_ERR("fp%d: rx_sge[%x]=[%x:%x]  sw_page=[%p]\n",
947                                   i, j, rx_sge[1], rx_sge[0], sw_page->page);
948                 }
949
950                 start = RCQ_BD(fp->rx_comp_cons - 10);
951                 end = RCQ_BD(fp->rx_comp_cons + 503);
952                 for (j = start; j != end; j = RCQ_BD(j + 1)) {
953                         u32 *cqe = (u32 *)&fp->rx_comp_ring[j];
954
955                         BNX2X_ERR("fp%d: cqe[%x]=[%x:%x:%x:%x]\n",
956                                   i, j, cqe[0], cqe[1], cqe[2], cqe[3]);
957                 }
958         }
959
960         /* Tx */
961         for_each_tx_queue(bp, i) {
962                 struct bnx2x_fastpath *fp = &bp->fp[i];
963
964                 start = TX_BD(le16_to_cpu(*fp->tx_cons_sb) - 10);
965                 end = TX_BD(le16_to_cpu(*fp->tx_cons_sb) + 245);
966                 for (j = start; j != end; j = TX_BD(j + 1)) {
967                         struct sw_tx_bd *sw_bd = &fp->tx_buf_ring[j];
968
969                         BNX2X_ERR("fp%d: packet[%x]=[%p,%x]\n",
970                                   i, j, sw_bd->skb, sw_bd->first_bd);
971                 }
972
973                 start = TX_BD(fp->tx_bd_cons - 10);
974                 end = TX_BD(fp->tx_bd_cons + 254);
975                 for (j = start; j != end; j = TX_BD(j + 1)) {
976                         u32 *tx_bd = (u32 *)&fp->tx_desc_ring[j];
977
978                         BNX2X_ERR("fp%d: tx_bd[%x]=[%x:%x:%x:%x]\n",
979                                   i, j, tx_bd[0], tx_bd[1], tx_bd[2], tx_bd[3]);
980                 }
981         }
982 #endif
983         bnx2x_fw_dump(bp);
984         bnx2x_mc_assert(bp);
985         BNX2X_ERR("end crash dump -----------------\n");
986 }
987
988 /*
989  * FLR Support for E2
990  *
991  * bnx2x_pf_flr_clnup() is called during nic_load in the per function HW
992  * initialization.
993  */
994 #define FLR_WAIT_USEC           10000   /* 10 miliseconds */
995 #define FLR_WAIT_INTERAVAL      50      /* usec */
996 #define FLR_POLL_CNT            (FLR_WAIT_USEC/FLR_WAIT_INTERAVAL) /* 200 */
997
998 struct pbf_pN_buf_regs {
999         int pN;
1000         u32 init_crd;
1001         u32 crd;
1002         u32 crd_freed;
1003 };
1004
1005 struct pbf_pN_cmd_regs {
1006         int pN;
1007         u32 lines_occup;
1008         u32 lines_freed;
1009 };
1010
1011 static void bnx2x_pbf_pN_buf_flushed(struct bnx2x *bp,
1012                                      struct pbf_pN_buf_regs *regs,
1013                                      u32 poll_count)
1014 {
1015         u32 init_crd, crd, crd_start, crd_freed, crd_freed_start;
1016         u32 cur_cnt = poll_count;
1017
1018         crd_freed = crd_freed_start = REG_RD(bp, regs->crd_freed);
1019         crd = crd_start = REG_RD(bp, regs->crd);
1020         init_crd = REG_RD(bp, regs->init_crd);
1021
1022         DP(BNX2X_MSG_SP, "INIT CREDIT[%d] : %x\n", regs->pN, init_crd);
1023         DP(BNX2X_MSG_SP, "CREDIT[%d]      : s:%x\n", regs->pN, crd);
1024         DP(BNX2X_MSG_SP, "CREDIT_FREED[%d]: s:%x\n", regs->pN, crd_freed);
1025
1026         while ((crd != init_crd) && ((u32)SUB_S32(crd_freed, crd_freed_start) <
1027                (init_crd - crd_start))) {
1028                 if (cur_cnt--) {
1029                         udelay(FLR_WAIT_INTERAVAL);
1030                         crd = REG_RD(bp, regs->crd);
1031                         crd_freed = REG_RD(bp, regs->crd_freed);
1032                 } else {
1033                         DP(BNX2X_MSG_SP, "PBF tx buffer[%d] timed out\n",
1034                            regs->pN);
1035                         DP(BNX2X_MSG_SP, "CREDIT[%d]      : c:%x\n",
1036                            regs->pN, crd);
1037                         DP(BNX2X_MSG_SP, "CREDIT_FREED[%d]: c:%x\n",
1038                            regs->pN, crd_freed);
1039                         break;
1040                 }
1041         }
1042         DP(BNX2X_MSG_SP, "Waited %d*%d usec for PBF tx buffer[%d]\n",
1043            poll_count-cur_cnt, FLR_WAIT_INTERAVAL, regs->pN);
1044 }
1045
1046 static void bnx2x_pbf_pN_cmd_flushed(struct bnx2x *bp,
1047                                      struct pbf_pN_cmd_regs *regs,
1048                                      u32 poll_count)
1049 {
1050         u32 occup, to_free, freed, freed_start;
1051         u32 cur_cnt = poll_count;
1052
1053         occup = to_free = REG_RD(bp, regs->lines_occup);
1054         freed = freed_start = REG_RD(bp, regs->lines_freed);
1055
1056         DP(BNX2X_MSG_SP, "OCCUPANCY[%d]   : s:%x\n", regs->pN, occup);
1057         DP(BNX2X_MSG_SP, "LINES_FREED[%d] : s:%x\n", regs->pN, freed);
1058
1059         while (occup && ((u32)SUB_S32(freed, freed_start) < to_free)) {
1060                 if (cur_cnt--) {
1061                         udelay(FLR_WAIT_INTERAVAL);
1062                         occup = REG_RD(bp, regs->lines_occup);
1063                         freed = REG_RD(bp, regs->lines_freed);
1064                 } else {
1065                         DP(BNX2X_MSG_SP, "PBF cmd queue[%d] timed out\n",
1066                            regs->pN);
1067                         DP(BNX2X_MSG_SP, "OCCUPANCY[%d]   : s:%x\n",
1068                            regs->pN, occup);
1069                         DP(BNX2X_MSG_SP, "LINES_FREED[%d] : s:%x\n",
1070                            regs->pN, freed);
1071                         break;
1072                 }
1073         }
1074         DP(BNX2X_MSG_SP, "Waited %d*%d usec for PBF cmd queue[%d]\n",
1075            poll_count-cur_cnt, FLR_WAIT_INTERAVAL, regs->pN);
1076 }
1077
1078 static inline u32 bnx2x_flr_clnup_reg_poll(struct bnx2x *bp, u32 reg,
1079                                      u32 expected, u32 poll_count)
1080 {
1081         u32 cur_cnt = poll_count;
1082         u32 val;
1083
1084         while ((val = REG_RD(bp, reg)) != expected && cur_cnt--)
1085                 udelay(FLR_WAIT_INTERAVAL);
1086
1087         return val;
1088 }
1089
1090 static inline int bnx2x_flr_clnup_poll_hw_counter(struct bnx2x *bp, u32 reg,
1091                                                   char *msg, u32 poll_cnt)
1092 {
1093         u32 val = bnx2x_flr_clnup_reg_poll(bp, reg, 0, poll_cnt);
1094         if (val != 0) {
1095                 BNX2X_ERR("%s usage count=%d\n", msg, val);
1096                 return 1;
1097         }
1098         return 0;
1099 }
1100
1101 static u32 bnx2x_flr_clnup_poll_count(struct bnx2x *bp)
1102 {
1103         /* adjust polling timeout */
1104         if (CHIP_REV_IS_EMUL(bp))
1105                 return FLR_POLL_CNT * 2000;
1106
1107         if (CHIP_REV_IS_FPGA(bp))
1108                 return FLR_POLL_CNT * 120;
1109
1110         return FLR_POLL_CNT;
1111 }
1112
1113 static void bnx2x_tx_hw_flushed(struct bnx2x *bp, u32 poll_count)
1114 {
1115         struct pbf_pN_cmd_regs cmd_regs[] = {
1116                 {0, (CHIP_IS_E3B0(bp)) ?
1117                         PBF_REG_TQ_OCCUPANCY_Q0 :
1118                         PBF_REG_P0_TQ_OCCUPANCY,
1119                     (CHIP_IS_E3B0(bp)) ?
1120                         PBF_REG_TQ_LINES_FREED_CNT_Q0 :
1121                         PBF_REG_P0_TQ_LINES_FREED_CNT},
1122                 {1, (CHIP_IS_E3B0(bp)) ?
1123                         PBF_REG_TQ_OCCUPANCY_Q1 :
1124                         PBF_REG_P1_TQ_OCCUPANCY,
1125                     (CHIP_IS_E3B0(bp)) ?
1126                         PBF_REG_TQ_LINES_FREED_CNT_Q1 :
1127                         PBF_REG_P1_TQ_LINES_FREED_CNT},
1128                 {4, (CHIP_IS_E3B0(bp)) ?
1129                         PBF_REG_TQ_OCCUPANCY_LB_Q :
1130                         PBF_REG_P4_TQ_OCCUPANCY,
1131                     (CHIP_IS_E3B0(bp)) ?
1132                         PBF_REG_TQ_LINES_FREED_CNT_LB_Q :
1133                         PBF_REG_P4_TQ_LINES_FREED_CNT}
1134         };
1135
1136         struct pbf_pN_buf_regs buf_regs[] = {
1137                 {0, (CHIP_IS_E3B0(bp)) ?
1138                         PBF_REG_INIT_CRD_Q0 :
1139                         PBF_REG_P0_INIT_CRD ,
1140                     (CHIP_IS_E3B0(bp)) ?
1141                         PBF_REG_CREDIT_Q0 :
1142                         PBF_REG_P0_CREDIT,
1143                     (CHIP_IS_E3B0(bp)) ?
1144                         PBF_REG_INTERNAL_CRD_FREED_CNT_Q0 :
1145                         PBF_REG_P0_INTERNAL_CRD_FREED_CNT},
1146                 {1, (CHIP_IS_E3B0(bp)) ?
1147                         PBF_REG_INIT_CRD_Q1 :
1148                         PBF_REG_P1_INIT_CRD,
1149                     (CHIP_IS_E3B0(bp)) ?
1150                         PBF_REG_CREDIT_Q1 :
1151                         PBF_REG_P1_CREDIT,
1152                     (CHIP_IS_E3B0(bp)) ?
1153                         PBF_REG_INTERNAL_CRD_FREED_CNT_Q1 :
1154                         PBF_REG_P1_INTERNAL_CRD_FREED_CNT},
1155                 {4, (CHIP_IS_E3B0(bp)) ?
1156                         PBF_REG_INIT_CRD_LB_Q :
1157                         PBF_REG_P4_INIT_CRD,
1158                     (CHIP_IS_E3B0(bp)) ?
1159                         PBF_REG_CREDIT_LB_Q :
1160                         PBF_REG_P4_CREDIT,
1161                     (CHIP_IS_E3B0(bp)) ?
1162                         PBF_REG_INTERNAL_CRD_FREED_CNT_LB_Q :
1163                         PBF_REG_P4_INTERNAL_CRD_FREED_CNT},
1164         };
1165
1166         int i;
1167
1168         /* Verify the command queues are flushed P0, P1, P4 */
1169         for (i = 0; i < ARRAY_SIZE(cmd_regs); i++)
1170                 bnx2x_pbf_pN_cmd_flushed(bp, &cmd_regs[i], poll_count);
1171
1172
1173         /* Verify the transmission buffers are flushed P0, P1, P4 */
1174         for (i = 0; i < ARRAY_SIZE(buf_regs); i++)
1175                 bnx2x_pbf_pN_buf_flushed(bp, &buf_regs[i], poll_count);
1176 }
1177
1178 #define OP_GEN_PARAM(param) \
1179         (((param) << SDM_OP_GEN_COMP_PARAM_SHIFT) & SDM_OP_GEN_COMP_PARAM)
1180
1181 #define OP_GEN_TYPE(type) \
1182         (((type) << SDM_OP_GEN_COMP_TYPE_SHIFT) & SDM_OP_GEN_COMP_TYPE)
1183
1184 #define OP_GEN_AGG_VECT(index) \
1185         (((index) << SDM_OP_GEN_AGG_VECT_IDX_SHIFT) & SDM_OP_GEN_AGG_VECT_IDX)
1186
1187
1188 static inline int bnx2x_send_final_clnup(struct bnx2x *bp, u8 clnup_func,
1189                                          u32 poll_cnt)
1190 {
1191         struct sdm_op_gen op_gen = {0};
1192
1193         u32 comp_addr = BAR_CSTRORM_INTMEM +
1194                         CSTORM_FINAL_CLEANUP_COMPLETE_OFFSET(clnup_func);
1195         int ret = 0;
1196
1197         if (REG_RD(bp, comp_addr)) {
1198                 BNX2X_ERR("Cleanup complete is not 0\n");
1199                 return 1;
1200         }
1201
1202         op_gen.command |= OP_GEN_PARAM(XSTORM_AGG_INT_FINAL_CLEANUP_INDEX);
1203         op_gen.command |= OP_GEN_TYPE(XSTORM_AGG_INT_FINAL_CLEANUP_COMP_TYPE);
1204         op_gen.command |= OP_GEN_AGG_VECT(clnup_func);
1205         op_gen.command |= 1 << SDM_OP_GEN_AGG_VECT_IDX_VALID_SHIFT;
1206
1207         DP(BNX2X_MSG_SP, "FW Final cleanup\n");
1208         REG_WR(bp, XSDM_REG_OPERATION_GEN, op_gen.command);
1209
1210         if (bnx2x_flr_clnup_reg_poll(bp, comp_addr, 1, poll_cnt) != 1) {
1211                 BNX2X_ERR("FW final cleanup did not succeed\n");
1212                 ret = 1;
1213         }
1214         /* Zero completion for nxt FLR */
1215         REG_WR(bp, comp_addr, 0);
1216
1217         return ret;
1218 }
1219
1220 static inline u8 bnx2x_is_pcie_pending(struct pci_dev *dev)
1221 {
1222         int pos;
1223         u16 status;
1224
1225         pos = pci_find_capability(dev, PCI_CAP_ID_EXP);
1226         if (!pos)
1227                 return false;
1228
1229         pci_read_config_word(dev, pos + PCI_EXP_DEVSTA, &status);
1230         return status & PCI_EXP_DEVSTA_TRPND;
1231 }
1232
1233 /* PF FLR specific routines
1234 */
1235 static int bnx2x_poll_hw_usage_counters(struct bnx2x *bp, u32 poll_cnt)
1236 {
1237
1238         /* wait for CFC PF usage-counter to zero (includes all the VFs) */
1239         if (bnx2x_flr_clnup_poll_hw_counter(bp,
1240                         CFC_REG_NUM_LCIDS_INSIDE_PF,
1241                         "CFC PF usage counter timed out",
1242                         poll_cnt))
1243                 return 1;
1244
1245
1246         /* Wait for DQ PF usage-counter to zero (until DQ cleanup) */
1247         if (bnx2x_flr_clnup_poll_hw_counter(bp,
1248                         DORQ_REG_PF_USAGE_CNT,
1249                         "DQ PF usage counter timed out",
1250                         poll_cnt))
1251                 return 1;
1252
1253         /* Wait for QM PF usage-counter to zero (until DQ cleanup) */
1254         if (bnx2x_flr_clnup_poll_hw_counter(bp,
1255                         QM_REG_PF_USG_CNT_0 + 4*BP_FUNC(bp),
1256                         "QM PF usage counter timed out",
1257                         poll_cnt))
1258                 return 1;
1259
1260         /* Wait for Timer PF usage-counters to zero (until DQ cleanup) */
1261         if (bnx2x_flr_clnup_poll_hw_counter(bp,
1262                         TM_REG_LIN0_VNIC_UC + 4*BP_PORT(bp),
1263                         "Timers VNIC usage counter timed out",
1264                         poll_cnt))
1265                 return 1;
1266         if (bnx2x_flr_clnup_poll_hw_counter(bp,
1267                         TM_REG_LIN0_NUM_SCANS + 4*BP_PORT(bp),
1268                         "Timers NUM_SCANS usage counter timed out",
1269                         poll_cnt))
1270                 return 1;
1271
1272         /* Wait DMAE PF usage counter to zero */
1273         if (bnx2x_flr_clnup_poll_hw_counter(bp,
1274                         dmae_reg_go_c[INIT_DMAE_C(bp)],
1275                         "DMAE dommand register timed out",
1276                         poll_cnt))
1277                 return 1;
1278
1279         return 0;
1280 }
1281
1282 static void bnx2x_hw_enable_status(struct bnx2x *bp)
1283 {
1284         u32 val;
1285
1286         val = REG_RD(bp, CFC_REG_WEAK_ENABLE_PF);
1287         DP(BNX2X_MSG_SP, "CFC_REG_WEAK_ENABLE_PF is 0x%x\n", val);
1288
1289         val = REG_RD(bp, PBF_REG_DISABLE_PF);
1290         DP(BNX2X_MSG_SP, "PBF_REG_DISABLE_PF is 0x%x\n", val);
1291
1292         val = REG_RD(bp, IGU_REG_PCI_PF_MSI_EN);
1293         DP(BNX2X_MSG_SP, "IGU_REG_PCI_PF_MSI_EN is 0x%x\n", val);
1294
1295         val = REG_RD(bp, IGU_REG_PCI_PF_MSIX_EN);
1296         DP(BNX2X_MSG_SP, "IGU_REG_PCI_PF_MSIX_EN is 0x%x\n", val);
1297
1298         val = REG_RD(bp, IGU_REG_PCI_PF_MSIX_FUNC_MASK);
1299         DP(BNX2X_MSG_SP, "IGU_REG_PCI_PF_MSIX_FUNC_MASK is 0x%x\n", val);
1300
1301         val = REG_RD(bp, PGLUE_B_REG_SHADOW_BME_PF_7_0_CLR);
1302         DP(BNX2X_MSG_SP, "PGLUE_B_REG_SHADOW_BME_PF_7_0_CLR is 0x%x\n", val);
1303
1304         val = REG_RD(bp, PGLUE_B_REG_FLR_REQUEST_PF_7_0_CLR);
1305         DP(BNX2X_MSG_SP, "PGLUE_B_REG_FLR_REQUEST_PF_7_0_CLR is 0x%x\n", val);
1306
1307         val = REG_RD(bp, PGLUE_B_REG_INTERNAL_PFID_ENABLE_MASTER);
1308         DP(BNX2X_MSG_SP, "PGLUE_B_REG_INTERNAL_PFID_ENABLE_MASTER is 0x%x\n",
1309            val);
1310 }
1311
1312 static int bnx2x_pf_flr_clnup(struct bnx2x *bp)
1313 {
1314         u32 poll_cnt = bnx2x_flr_clnup_poll_count(bp);
1315
1316         DP(BNX2X_MSG_SP, "Cleanup after FLR PF[%d]\n", BP_ABS_FUNC(bp));
1317
1318         /* Re-enable PF target read access */
1319         REG_WR(bp, PGLUE_B_REG_INTERNAL_PFID_ENABLE_TARGET_READ, 1);
1320
1321         /* Poll HW usage counters */
1322         if (bnx2x_poll_hw_usage_counters(bp, poll_cnt))
1323                 return -EBUSY;
1324
1325         /* Zero the igu 'trailing edge' and 'leading edge' */
1326
1327         /* Send the FW cleanup command */
1328         if (bnx2x_send_final_clnup(bp, (u8)BP_FUNC(bp), poll_cnt))
1329                 return -EBUSY;
1330
1331         /* ATC cleanup */
1332
1333         /* Verify TX hw is flushed */
1334         bnx2x_tx_hw_flushed(bp, poll_cnt);
1335
1336         /* Wait 100ms (not adjusted according to platform) */
1337         msleep(100);
1338
1339         /* Verify no pending pci transactions */
1340         if (bnx2x_is_pcie_pending(bp->pdev))
1341                 BNX2X_ERR("PCIE Transactions still pending\n");
1342
1343         /* Debug */
1344         bnx2x_hw_enable_status(bp);
1345
1346         /*
1347          * Master enable - Due to WB DMAE writes performed before this
1348          * register is re-initialized as part of the regular function init
1349          */
1350         REG_WR(bp, PGLUE_B_REG_INTERNAL_PFID_ENABLE_MASTER, 1);
1351
1352         return 0;
1353 }
1354
1355 static void bnx2x_hc_int_enable(struct bnx2x *bp)
1356 {
1357         int port = BP_PORT(bp);
1358         u32 addr = port ? HC_REG_CONFIG_1 : HC_REG_CONFIG_0;
1359         u32 val = REG_RD(bp, addr);
1360         int msix = (bp->flags & USING_MSIX_FLAG) ? 1 : 0;
1361         int msi = (bp->flags & USING_MSI_FLAG) ? 1 : 0;
1362
1363         if (msix) {
1364                 val &= ~(HC_CONFIG_0_REG_SINGLE_ISR_EN_0 |
1365                          HC_CONFIG_0_REG_INT_LINE_EN_0);
1366                 val |= (HC_CONFIG_0_REG_MSI_MSIX_INT_EN_0 |
1367                         HC_CONFIG_0_REG_ATTN_BIT_EN_0);
1368         } else if (msi) {
1369                 val &= ~HC_CONFIG_0_REG_INT_LINE_EN_0;
1370                 val |= (HC_CONFIG_0_REG_SINGLE_ISR_EN_0 |
1371                         HC_CONFIG_0_REG_MSI_MSIX_INT_EN_0 |
1372                         HC_CONFIG_0_REG_ATTN_BIT_EN_0);
1373         } else {
1374                 val |= (HC_CONFIG_0_REG_SINGLE_ISR_EN_0 |
1375                         HC_CONFIG_0_REG_MSI_MSIX_INT_EN_0 |
1376                         HC_CONFIG_0_REG_INT_LINE_EN_0 |
1377                         HC_CONFIG_0_REG_ATTN_BIT_EN_0);
1378
1379                 if (!CHIP_IS_E1(bp)) {
1380                         DP(NETIF_MSG_INTR, "write %x to HC %d (addr 0x%x)\n",
1381                            val, port, addr);
1382
1383                         REG_WR(bp, addr, val);
1384
1385                         val &= ~HC_CONFIG_0_REG_MSI_MSIX_INT_EN_0;
1386                 }
1387         }
1388
1389         if (CHIP_IS_E1(bp))
1390                 REG_WR(bp, HC_REG_INT_MASK + port*4, 0x1FFFF);
1391
1392         DP(NETIF_MSG_INTR, "write %x to HC %d (addr 0x%x)  mode %s\n",
1393            val, port, addr, (msix ? "MSI-X" : (msi ? "MSI" : "INTx")));
1394
1395         REG_WR(bp, addr, val);
1396         /*
1397          * Ensure that HC_CONFIG is written before leading/trailing edge config
1398          */
1399         mmiowb();
1400         barrier();
1401
1402         if (!CHIP_IS_E1(bp)) {
1403                 /* init leading/trailing edge */
1404                 if (IS_MF(bp)) {
1405                         val = (0xee0f | (1 << (BP_E1HVN(bp) + 4)));
1406                         if (bp->port.pmf)
1407                                 /* enable nig and gpio3 attention */
1408                                 val |= 0x1100;
1409                 } else
1410                         val = 0xffff;
1411
1412                 REG_WR(bp, HC_REG_TRAILING_EDGE_0 + port*8, val);
1413                 REG_WR(bp, HC_REG_LEADING_EDGE_0 + port*8, val);
1414         }
1415
1416         /* Make sure that interrupts are indeed enabled from here on */
1417         mmiowb();
1418 }
1419
1420 static void bnx2x_igu_int_enable(struct bnx2x *bp)
1421 {
1422         u32 val;
1423         int msix = (bp->flags & USING_MSIX_FLAG) ? 1 : 0;
1424         int msi = (bp->flags & USING_MSI_FLAG) ? 1 : 0;
1425
1426         val = REG_RD(bp, IGU_REG_PF_CONFIGURATION);
1427
1428         if (msix) {
1429                 val &= ~(IGU_PF_CONF_INT_LINE_EN |
1430                          IGU_PF_CONF_SINGLE_ISR_EN);
1431                 val |= (IGU_PF_CONF_FUNC_EN |
1432                         IGU_PF_CONF_MSI_MSIX_EN |
1433                         IGU_PF_CONF_ATTN_BIT_EN);
1434         } else if (msi) {
1435                 val &= ~IGU_PF_CONF_INT_LINE_EN;
1436                 val |= (IGU_PF_CONF_FUNC_EN |
1437                         IGU_PF_CONF_MSI_MSIX_EN |
1438                         IGU_PF_CONF_ATTN_BIT_EN |
1439                         IGU_PF_CONF_SINGLE_ISR_EN);
1440         } else {
1441                 val &= ~IGU_PF_CONF_MSI_MSIX_EN;
1442                 val |= (IGU_PF_CONF_FUNC_EN |
1443                         IGU_PF_CONF_INT_LINE_EN |
1444                         IGU_PF_CONF_ATTN_BIT_EN |
1445                         IGU_PF_CONF_SINGLE_ISR_EN);
1446         }
1447
1448         DP(NETIF_MSG_INTR, "write 0x%x to IGU  mode %s\n",
1449            val, (msix ? "MSI-X" : (msi ? "MSI" : "INTx")));
1450
1451         REG_WR(bp, IGU_REG_PF_CONFIGURATION, val);
1452
1453         barrier();
1454
1455         /* init leading/trailing edge */
1456         if (IS_MF(bp)) {
1457                 val = (0xee0f | (1 << (BP_E1HVN(bp) + 4)));
1458                 if (bp->port.pmf)
1459                         /* enable nig and gpio3 attention */
1460                         val |= 0x1100;
1461         } else
1462                 val = 0xffff;
1463
1464         REG_WR(bp, IGU_REG_TRAILING_EDGE_LATCH, val);
1465         REG_WR(bp, IGU_REG_LEADING_EDGE_LATCH, val);
1466
1467         /* Make sure that interrupts are indeed enabled from here on */
1468         mmiowb();
1469 }
1470
1471 void bnx2x_int_enable(struct bnx2x *bp)
1472 {
1473         if (bp->common.int_block == INT_BLOCK_HC)
1474                 bnx2x_hc_int_enable(bp);
1475         else
1476                 bnx2x_igu_int_enable(bp);
1477 }
1478
1479 static void bnx2x_hc_int_disable(struct bnx2x *bp)
1480 {
1481         int port = BP_PORT(bp);
1482         u32 addr = port ? HC_REG_CONFIG_1 : HC_REG_CONFIG_0;
1483         u32 val = REG_RD(bp, addr);
1484
1485         /*
1486          * in E1 we must use only PCI configuration space to disable
1487          * MSI/MSIX capablility
1488          * It's forbitten to disable IGU_PF_CONF_MSI_MSIX_EN in HC block
1489          */
1490         if (CHIP_IS_E1(bp)) {
1491                 /*  Since IGU_PF_CONF_MSI_MSIX_EN still always on
1492                  *  Use mask register to prevent from HC sending interrupts
1493                  *  after we exit the function
1494                  */
1495                 REG_WR(bp, HC_REG_INT_MASK + port*4, 0);
1496
1497                 val &= ~(HC_CONFIG_0_REG_SINGLE_ISR_EN_0 |
1498                          HC_CONFIG_0_REG_INT_LINE_EN_0 |
1499                          HC_CONFIG_0_REG_ATTN_BIT_EN_0);
1500         } else
1501                 val &= ~(HC_CONFIG_0_REG_SINGLE_ISR_EN_0 |
1502                          HC_CONFIG_0_REG_MSI_MSIX_INT_EN_0 |
1503                          HC_CONFIG_0_REG_INT_LINE_EN_0 |
1504                          HC_CONFIG_0_REG_ATTN_BIT_EN_0);
1505
1506         DP(NETIF_MSG_INTR, "write %x to HC %d (addr 0x%x)\n",
1507            val, port, addr);
1508
1509         /* flush all outstanding writes */
1510         mmiowb();
1511
1512         REG_WR(bp, addr, val);
1513         if (REG_RD(bp, addr) != val)
1514                 BNX2X_ERR("BUG! proper val not read from IGU!\n");
1515 }
1516
1517 static void bnx2x_igu_int_disable(struct bnx2x *bp)
1518 {
1519         u32 val = REG_RD(bp, IGU_REG_PF_CONFIGURATION);
1520
1521         val &= ~(IGU_PF_CONF_MSI_MSIX_EN |
1522                  IGU_PF_CONF_INT_LINE_EN |
1523                  IGU_PF_CONF_ATTN_BIT_EN);
1524
1525         DP(NETIF_MSG_INTR, "write %x to IGU\n", val);
1526
1527         /* flush all outstanding writes */
1528         mmiowb();
1529
1530         REG_WR(bp, IGU_REG_PF_CONFIGURATION, val);
1531         if (REG_RD(bp, IGU_REG_PF_CONFIGURATION) != val)
1532                 BNX2X_ERR("BUG! proper val not read from IGU!\n");
1533 }
1534
1535 static void bnx2x_int_disable(struct bnx2x *bp)
1536 {
1537         if (bp->common.int_block == INT_BLOCK_HC)
1538                 bnx2x_hc_int_disable(bp);
1539         else
1540                 bnx2x_igu_int_disable(bp);
1541 }
1542
1543 void bnx2x_int_disable_sync(struct bnx2x *bp, int disable_hw)
1544 {
1545         int msix = (bp->flags & USING_MSIX_FLAG) ? 1 : 0;
1546         int i, offset;
1547
1548         if (disable_hw)
1549                 /* prevent the HW from sending interrupts */
1550                 bnx2x_int_disable(bp);
1551
1552         /* make sure all ISRs are done */
1553         if (msix) {
1554                 synchronize_irq(bp->msix_table[0].vector);
1555                 offset = 1;
1556 #ifdef BCM_CNIC
1557                 offset++;
1558 #endif
1559                 for_each_eth_queue(bp, i)
1560                         synchronize_irq(bp->msix_table[offset++].vector);
1561         } else
1562                 synchronize_irq(bp->pdev->irq);
1563
1564         /* make sure sp_task is not running */
1565         cancel_delayed_work(&bp->sp_task);
1566         cancel_delayed_work(&bp->period_task);
1567         flush_workqueue(bnx2x_wq);
1568 }
1569
1570 /* fast path */
1571
1572 /*
1573  * General service functions
1574  */
1575
1576 /* Return true if succeeded to acquire the lock */
1577 static bool bnx2x_trylock_hw_lock(struct bnx2x *bp, u32 resource)
1578 {
1579         u32 lock_status;
1580         u32 resource_bit = (1 << resource);
1581         int func = BP_FUNC(bp);
1582         u32 hw_lock_control_reg;
1583
1584         DP(NETIF_MSG_HW, "Trying to take a lock on resource %d\n", resource);
1585
1586         /* Validating that the resource is within range */
1587         if (resource > HW_LOCK_MAX_RESOURCE_VALUE) {
1588                 DP(NETIF_MSG_HW,
1589                    "resource(0x%x) > HW_LOCK_MAX_RESOURCE_VALUE(0x%x)\n",
1590                    resource, HW_LOCK_MAX_RESOURCE_VALUE);
1591                 return false;
1592         }
1593
1594         if (func <= 5)
1595                 hw_lock_control_reg = (MISC_REG_DRIVER_CONTROL_1 + func*8);
1596         else
1597                 hw_lock_control_reg =
1598                                 (MISC_REG_DRIVER_CONTROL_7 + (func - 6)*8);
1599
1600         /* Try to acquire the lock */
1601         REG_WR(bp, hw_lock_control_reg + 4, resource_bit);
1602         lock_status = REG_RD(bp, hw_lock_control_reg);
1603         if (lock_status & resource_bit)
1604                 return true;
1605
1606         DP(NETIF_MSG_HW, "Failed to get a lock on resource %d\n", resource);
1607         return false;
1608 }
1609
1610 /**
1611  * bnx2x_get_leader_lock_resource - get the recovery leader resource id
1612  *
1613  * @bp: driver handle
1614  *
1615  * Returns the recovery leader resource id according to the engine this function
1616  * belongs to. Currently only only 2 engines is supported.
1617  */
1618 static inline int bnx2x_get_leader_lock_resource(struct bnx2x *bp)
1619 {
1620         if (BP_PATH(bp))
1621                 return HW_LOCK_RESOURCE_RECOVERY_LEADER_1;
1622         else
1623                 return HW_LOCK_RESOURCE_RECOVERY_LEADER_0;
1624 }
1625
1626 /**
1627  * bnx2x_trylock_leader_lock- try to aquire a leader lock.
1628  *
1629  * @bp: driver handle
1630  *
1631  * Tries to aquire a leader lock for cuurent engine.
1632  */
1633 static inline bool bnx2x_trylock_leader_lock(struct bnx2x *bp)
1634 {
1635         return bnx2x_trylock_hw_lock(bp, bnx2x_get_leader_lock_resource(bp));
1636 }
1637
1638 #ifdef BCM_CNIC
1639 static void bnx2x_cnic_cfc_comp(struct bnx2x *bp, int cid, u8 err);
1640 #endif
1641
1642 void bnx2x_sp_event(struct bnx2x_fastpath *fp, union eth_rx_cqe *rr_cqe)
1643 {
1644         struct bnx2x *bp = fp->bp;
1645         int cid = SW_CID(rr_cqe->ramrod_cqe.conn_and_cmd_data);
1646         int command = CQE_CMD(rr_cqe->ramrod_cqe.conn_and_cmd_data);
1647         enum bnx2x_queue_cmd drv_cmd = BNX2X_Q_CMD_MAX;
1648         struct bnx2x_queue_sp_obj *q_obj = &fp->q_obj;
1649
1650         DP(BNX2X_MSG_SP,
1651            "fp %d  cid %d  got ramrod #%d  state is %x  type is %d\n",
1652            fp->index, cid, command, bp->state,
1653            rr_cqe->ramrod_cqe.ramrod_type);
1654
1655         switch (command) {
1656         case (RAMROD_CMD_ID_ETH_CLIENT_UPDATE):
1657                 DP(NETIF_MSG_IFUP, "got UPDATE ramrod. CID %d\n", cid);
1658                 drv_cmd = BNX2X_Q_CMD_UPDATE;
1659                 break;
1660         case (RAMROD_CMD_ID_ETH_CLIENT_SETUP):
1661                 DP(NETIF_MSG_IFUP, "got MULTI[%d] setup ramrod\n", cid);
1662                 drv_cmd = BNX2X_Q_CMD_SETUP;
1663                 break;
1664
1665         case (RAMROD_CMD_ID_ETH_HALT):
1666                 DP(NETIF_MSG_IFDOWN, "got MULTI[%d] halt ramrod\n", cid);
1667                 drv_cmd = BNX2X_Q_CMD_HALT;
1668                 break;
1669
1670         case (RAMROD_CMD_ID_ETH_TERMINATE):
1671                 DP(NETIF_MSG_IFDOWN, "got MULTI[%d] teminate ramrod\n", cid);
1672                 drv_cmd = BNX2X_Q_CMD_TERMINATE;
1673                 break;
1674
1675         case (RAMROD_CMD_ID_ETH_EMPTY):
1676                 DP(NETIF_MSG_IFDOWN, "got MULTI[%d] empty ramrod\n", cid);
1677                 drv_cmd = BNX2X_Q_CMD_EMPTY;
1678                 break;
1679
1680         default:
1681                 BNX2X_ERR("unexpected MC reply (%d) on fp[%d]\n",
1682                           command, fp->index);
1683                 return;
1684         }
1685
1686         if ((drv_cmd != BNX2X_Q_CMD_MAX) &&
1687             q_obj->complete_cmd(bp, q_obj, drv_cmd))
1688                 /* q_obj->complete_cmd() failure means that this was
1689                  * an unexpected completion.
1690                  *
1691                  * In this case we don't want to increase the bp->spq_left
1692                  * because apparently we haven't sent this command the first
1693                  * place.
1694                  */
1695 #ifdef BNX2X_STOP_ON_ERROR
1696                 bnx2x_panic();
1697 #else
1698                 return;
1699 #endif
1700
1701         smp_mb__before_atomic_inc();
1702         atomic_inc(&bp->cq_spq_left);
1703         /* push the change in bp->spq_left and towards the memory */
1704         smp_mb__after_atomic_inc();
1705
1706         return;
1707 }
1708
1709 void bnx2x_update_rx_prod(struct bnx2x *bp, struct bnx2x_fastpath *fp,
1710                         u16 bd_prod, u16 rx_comp_prod, u16 rx_sge_prod)
1711 {
1712         u32 start = BAR_USTRORM_INTMEM + fp->ustorm_rx_prods_offset;
1713
1714         bnx2x_update_rx_prod_gen(bp, fp, bd_prod, rx_comp_prod, rx_sge_prod,
1715                                  start);
1716 }
1717
1718 irqreturn_t bnx2x_interrupt(int irq, void *dev_instance)
1719 {
1720         struct bnx2x *bp = netdev_priv(dev_instance);
1721         u16 status = bnx2x_ack_int(bp);
1722         u16 mask;
1723         int i;
1724
1725         /* Return here if interrupt is shared and it's not for us */
1726         if (unlikely(status == 0)) {
1727                 DP(NETIF_MSG_INTR, "not our interrupt!\n");
1728                 return IRQ_NONE;
1729         }
1730         DP(NETIF_MSG_INTR, "got an interrupt  status 0x%x\n", status);
1731
1732 #ifdef BNX2X_STOP_ON_ERROR
1733         if (unlikely(bp->panic))
1734                 return IRQ_HANDLED;
1735 #endif
1736
1737         for_each_eth_queue(bp, i) {
1738                 struct bnx2x_fastpath *fp = &bp->fp[i];
1739
1740                 mask = 0x2 << (fp->index + CNIC_CONTEXT_USE);
1741                 if (status & mask) {
1742                         /* Handle Rx or Tx according to SB id */
1743                         prefetch(fp->rx_cons_sb);
1744                         prefetch(fp->tx_cons_sb);
1745                         prefetch(&fp->sb_running_index[SM_RX_ID]);
1746                         napi_schedule(&bnx2x_fp(bp, fp->index, napi));
1747                         status &= ~mask;
1748                 }
1749         }
1750
1751 #ifdef BCM_CNIC
1752         mask = 0x2;
1753         if (status & (mask | 0x1)) {
1754                 struct cnic_ops *c_ops = NULL;
1755
1756                 if (likely(bp->state == BNX2X_STATE_OPEN)) {
1757                         rcu_read_lock();
1758                         c_ops = rcu_dereference(bp->cnic_ops);
1759                         if (c_ops)
1760                                 c_ops->cnic_handler(bp->cnic_data, NULL);
1761                         rcu_read_unlock();
1762                 }
1763
1764                 status &= ~mask;
1765         }
1766 #endif
1767
1768         if (unlikely(status & 0x1)) {
1769                 queue_delayed_work(bnx2x_wq, &bp->sp_task, 0);
1770
1771                 status &= ~0x1;
1772                 if (!status)
1773                         return IRQ_HANDLED;
1774         }
1775
1776         if (unlikely(status))
1777                 DP(NETIF_MSG_INTR, "got an unknown interrupt! (status 0x%x)\n",
1778                    status);
1779
1780         return IRQ_HANDLED;
1781 }
1782
1783 /* Link */
1784
1785 /*
1786  * General service functions
1787  */
1788
1789 int bnx2x_acquire_hw_lock(struct bnx2x *bp, u32 resource)
1790 {
1791         u32 lock_status;
1792         u32 resource_bit = (1 << resource);
1793         int func = BP_FUNC(bp);
1794         u32 hw_lock_control_reg;
1795         int cnt;
1796
1797         /* Validating that the resource is within range */
1798         if (resource > HW_LOCK_MAX_RESOURCE_VALUE) {
1799                 DP(NETIF_MSG_HW,
1800                    "resource(0x%x) > HW_LOCK_MAX_RESOURCE_VALUE(0x%x)\n",
1801                    resource, HW_LOCK_MAX_RESOURCE_VALUE);
1802                 return -EINVAL;
1803         }
1804
1805         if (func <= 5) {
1806                 hw_lock_control_reg = (MISC_REG_DRIVER_CONTROL_1 + func*8);
1807         } else {
1808                 hw_lock_control_reg =
1809                                 (MISC_REG_DRIVER_CONTROL_7 + (func - 6)*8);
1810         }
1811
1812         /* Validating that the resource is not already taken */
1813         lock_status = REG_RD(bp, hw_lock_control_reg);
1814         if (lock_status & resource_bit) {
1815                 DP(NETIF_MSG_HW, "lock_status 0x%x  resource_bit 0x%x\n",
1816                    lock_status, resource_bit);
1817                 return -EEXIST;
1818         }
1819
1820         /* Try for 5 second every 5ms */
1821         for (cnt = 0; cnt < 1000; cnt++) {
1822                 /* Try to acquire the lock */
1823                 REG_WR(bp, hw_lock_control_reg + 4, resource_bit);
1824                 lock_status = REG_RD(bp, hw_lock_control_reg);
1825                 if (lock_status & resource_bit)
1826                         return 0;
1827
1828                 msleep(5);
1829         }
1830         DP(NETIF_MSG_HW, "Timeout\n");
1831         return -EAGAIN;
1832 }
1833
1834 int bnx2x_release_leader_lock(struct bnx2x *bp)
1835 {
1836         return bnx2x_release_hw_lock(bp, bnx2x_get_leader_lock_resource(bp));
1837 }
1838
1839 int bnx2x_release_hw_lock(struct bnx2x *bp, u32 resource)
1840 {
1841         u32 lock_status;
1842         u32 resource_bit = (1 << resource);
1843         int func = BP_FUNC(bp);
1844         u32 hw_lock_control_reg;
1845
1846         DP(NETIF_MSG_HW, "Releasing a lock on resource %d\n", resource);
1847
1848         /* Validating that the resource is within range */
1849         if (resource > HW_LOCK_MAX_RESOURCE_VALUE) {
1850                 DP(NETIF_MSG_HW,
1851                    "resource(0x%x) > HW_LOCK_MAX_RESOURCE_VALUE(0x%x)\n",
1852                    resource, HW_LOCK_MAX_RESOURCE_VALUE);
1853                 return -EINVAL;
1854         }
1855
1856         if (func <= 5) {
1857                 hw_lock_control_reg = (MISC_REG_DRIVER_CONTROL_1 + func*8);
1858         } else {
1859                 hw_lock_control_reg =
1860                                 (MISC_REG_DRIVER_CONTROL_7 + (func - 6)*8);
1861         }
1862
1863         /* Validating that the resource is currently taken */
1864         lock_status = REG_RD(bp, hw_lock_control_reg);
1865         if (!(lock_status & resource_bit)) {
1866                 DP(NETIF_MSG_HW, "lock_status 0x%x  resource_bit 0x%x\n",
1867                    lock_status, resource_bit);
1868                 return -EFAULT;
1869         }
1870
1871         REG_WR(bp, hw_lock_control_reg, resource_bit);
1872         return 0;
1873 }
1874
1875
1876 int bnx2x_get_gpio(struct bnx2x *bp, int gpio_num, u8 port)
1877 {
1878         /* The GPIO should be swapped if swap register is set and active */
1879         int gpio_port = (REG_RD(bp, NIG_REG_PORT_SWAP) &&
1880                          REG_RD(bp, NIG_REG_STRAP_OVERRIDE)) ^ port;
1881         int gpio_shift = gpio_num +
1882                         (gpio_port ? MISC_REGISTERS_GPIO_PORT_SHIFT : 0);
1883         u32 gpio_mask = (1 << gpio_shift);
1884         u32 gpio_reg;
1885         int value;
1886
1887         if (gpio_num > MISC_REGISTERS_GPIO_3) {
1888                 BNX2X_ERR("Invalid GPIO %d\n", gpio_num);
1889                 return -EINVAL;
1890         }
1891
1892         /* read GPIO value */
1893         gpio_reg = REG_RD(bp, MISC_REG_GPIO);
1894
1895         /* get the requested pin value */
1896         if ((gpio_reg & gpio_mask) == gpio_mask)
1897                 value = 1;
1898         else
1899                 value = 0;
1900
1901         DP(NETIF_MSG_LINK, "pin %d  value 0x%x\n", gpio_num, value);
1902
1903         return value;
1904 }
1905
1906 int bnx2x_set_gpio(struct bnx2x *bp, int gpio_num, u32 mode, u8 port)
1907 {
1908         /* The GPIO should be swapped if swap register is set and active */
1909         int gpio_port = (REG_RD(bp, NIG_REG_PORT_SWAP) &&
1910                          REG_RD(bp, NIG_REG_STRAP_OVERRIDE)) ^ port;
1911         int gpio_shift = gpio_num +
1912                         (gpio_port ? MISC_REGISTERS_GPIO_PORT_SHIFT : 0);
1913         u32 gpio_mask = (1 << gpio_shift);
1914         u32 gpio_reg;
1915
1916         if (gpio_num > MISC_REGISTERS_GPIO_3) {
1917                 BNX2X_ERR("Invalid GPIO %d\n", gpio_num);
1918                 return -EINVAL;
1919         }
1920
1921         bnx2x_acquire_hw_lock(bp, HW_LOCK_RESOURCE_GPIO);
1922         /* read GPIO and mask except the float bits */
1923         gpio_reg = (REG_RD(bp, MISC_REG_GPIO) & MISC_REGISTERS_GPIO_FLOAT);
1924
1925         switch (mode) {
1926         case MISC_REGISTERS_GPIO_OUTPUT_LOW:
1927                 DP(NETIF_MSG_LINK, "Set GPIO %d (shift %d) -> output low\n",
1928                    gpio_num, gpio_shift);
1929                 /* clear FLOAT and set CLR */
1930                 gpio_reg &= ~(gpio_mask << MISC_REGISTERS_GPIO_FLOAT_POS);
1931                 gpio_reg |=  (gpio_mask << MISC_REGISTERS_GPIO_CLR_POS);
1932                 break;
1933
1934         case MISC_REGISTERS_GPIO_OUTPUT_HIGH:
1935                 DP(NETIF_MSG_LINK, "Set GPIO %d (shift %d) -> output high\n",
1936                    gpio_num, gpio_shift);
1937                 /* clear FLOAT and set SET */
1938                 gpio_reg &= ~(gpio_mask << MISC_REGISTERS_GPIO_FLOAT_POS);
1939                 gpio_reg |=  (gpio_mask << MISC_REGISTERS_GPIO_SET_POS);
1940                 break;
1941
1942         case MISC_REGISTERS_GPIO_INPUT_HI_Z:
1943                 DP(NETIF_MSG_LINK, "Set GPIO %d (shift %d) -> input\n",
1944                    gpio_num, gpio_shift);
1945                 /* set FLOAT */
1946                 gpio_reg |= (gpio_mask << MISC_REGISTERS_GPIO_FLOAT_POS);
1947                 break;
1948
1949         default:
1950                 break;
1951         }
1952
1953         REG_WR(bp, MISC_REG_GPIO, gpio_reg);
1954         bnx2x_release_hw_lock(bp, HW_LOCK_RESOURCE_GPIO);
1955
1956         return 0;
1957 }
1958
1959 int bnx2x_set_mult_gpio(struct bnx2x *bp, u8 pins, u32 mode)
1960 {
1961         u32 gpio_reg = 0;
1962         int rc = 0;
1963
1964         /* Any port swapping should be handled by caller. */
1965
1966         bnx2x_acquire_hw_lock(bp, HW_LOCK_RESOURCE_GPIO);
1967         /* read GPIO and mask except the float bits */
1968         gpio_reg = REG_RD(bp, MISC_REG_GPIO);
1969         gpio_reg &= ~(pins << MISC_REGISTERS_GPIO_FLOAT_POS);
1970         gpio_reg &= ~(pins << MISC_REGISTERS_GPIO_CLR_POS);
1971         gpio_reg &= ~(pins << MISC_REGISTERS_GPIO_SET_POS);
1972
1973         switch (mode) {
1974         case MISC_REGISTERS_GPIO_OUTPUT_LOW:
1975                 DP(NETIF_MSG_LINK, "Set GPIO 0x%x -> output low\n", pins);
1976                 /* set CLR */
1977                 gpio_reg |= (pins << MISC_REGISTERS_GPIO_CLR_POS);
1978                 break;
1979
1980         case MISC_REGISTERS_GPIO_OUTPUT_HIGH:
1981                 DP(NETIF_MSG_LINK, "Set GPIO 0x%x -> output high\n", pins);
1982                 /* set SET */
1983                 gpio_reg |= (pins << MISC_REGISTERS_GPIO_SET_POS);
1984                 break;
1985
1986         case MISC_REGISTERS_GPIO_INPUT_HI_Z:
1987                 DP(NETIF_MSG_LINK, "Set GPIO 0x%x -> input\n", pins);
1988                 /* set FLOAT */
1989                 gpio_reg |= (pins << MISC_REGISTERS_GPIO_FLOAT_POS);
1990                 break;
1991
1992         default:
1993                 BNX2X_ERR("Invalid GPIO mode assignment %d\n", mode);
1994                 rc = -EINVAL;
1995                 break;
1996         }
1997
1998         if (rc == 0)
1999                 REG_WR(bp, MISC_REG_GPIO, gpio_reg);
2000
2001         bnx2x_release_hw_lock(bp, HW_LOCK_RESOURCE_GPIO);
2002
2003         return rc;
2004 }
2005
2006 int bnx2x_set_gpio_int(struct bnx2x *bp, int gpio_num, u32 mode, u8 port)
2007 {
2008         /* The GPIO should be swapped if swap register is set and active */
2009         int gpio_port = (REG_RD(bp, NIG_REG_PORT_SWAP) &&
2010                          REG_RD(bp, NIG_REG_STRAP_OVERRIDE)) ^ port;
2011         int gpio_shift = gpio_num +
2012                         (gpio_port ? MISC_REGISTERS_GPIO_PORT_SHIFT : 0);
2013         u32 gpio_mask = (1 << gpio_shift);
2014         u32 gpio_reg;
2015
2016         if (gpio_num > MISC_REGISTERS_GPIO_3) {
2017                 BNX2X_ERR("Invalid GPIO %d\n", gpio_num);
2018                 return -EINVAL;
2019         }
2020
2021         bnx2x_acquire_hw_lock(bp, HW_LOCK_RESOURCE_GPIO);
2022         /* read GPIO int */
2023         gpio_reg = REG_RD(bp, MISC_REG_GPIO_INT);
2024
2025         switch (mode) {
2026         case MISC_REGISTERS_GPIO_INT_OUTPUT_CLR:
2027                 DP(NETIF_MSG_LINK, "Clear GPIO INT %d (shift %d) -> "
2028                                    "output low\n", gpio_num, gpio_shift);
2029                 /* clear SET and set CLR */
2030                 gpio_reg &= ~(gpio_mask << MISC_REGISTERS_GPIO_INT_SET_POS);
2031                 gpio_reg |=  (gpio_mask << MISC_REGISTERS_GPIO_INT_CLR_POS);
2032                 break;
2033
2034         case MISC_REGISTERS_GPIO_INT_OUTPUT_SET:
2035                 DP(NETIF_MSG_LINK, "Set GPIO INT %d (shift %d) -> "
2036                                    "output high\n", gpio_num, gpio_shift);
2037                 /* clear CLR and set SET */
2038                 gpio_reg &= ~(gpio_mask << MISC_REGISTERS_GPIO_INT_CLR_POS);
2039                 gpio_reg |=  (gpio_mask << MISC_REGISTERS_GPIO_INT_SET_POS);
2040                 break;
2041
2042         default:
2043                 break;
2044         }
2045
2046         REG_WR(bp, MISC_REG_GPIO_INT, gpio_reg);
2047         bnx2x_release_hw_lock(bp, HW_LOCK_RESOURCE_GPIO);
2048
2049         return 0;
2050 }
2051
2052 static int bnx2x_set_spio(struct bnx2x *bp, int spio_num, u32 mode)
2053 {
2054         u32 spio_mask = (1 << spio_num);
2055         u32 spio_reg;
2056
2057         if ((spio_num < MISC_REGISTERS_SPIO_4) ||
2058             (spio_num > MISC_REGISTERS_SPIO_7)) {
2059                 BNX2X_ERR("Invalid SPIO %d\n", spio_num);
2060                 return -EINVAL;
2061         }
2062
2063         bnx2x_acquire_hw_lock(bp, HW_LOCK_RESOURCE_SPIO);
2064         /* read SPIO and mask except the float bits */
2065         spio_reg = (REG_RD(bp, MISC_REG_SPIO) & MISC_REGISTERS_SPIO_FLOAT);
2066
2067         switch (mode) {
2068         case MISC_REGISTERS_SPIO_OUTPUT_LOW:
2069                 DP(NETIF_MSG_LINK, "Set SPIO %d -> output low\n", spio_num);
2070                 /* clear FLOAT and set CLR */
2071                 spio_reg &= ~(spio_mask << MISC_REGISTERS_SPIO_FLOAT_POS);
2072                 spio_reg |=  (spio_mask << MISC_REGISTERS_SPIO_CLR_POS);
2073                 break;
2074
2075         case MISC_REGISTERS_SPIO_OUTPUT_HIGH:
2076                 DP(NETIF_MSG_LINK, "Set SPIO %d -> output high\n", spio_num);
2077                 /* clear FLOAT and set SET */
2078                 spio_reg &= ~(spio_mask << MISC_REGISTERS_SPIO_FLOAT_POS);
2079                 spio_reg |=  (spio_mask << MISC_REGISTERS_SPIO_SET_POS);
2080                 break;
2081
2082         case MISC_REGISTERS_SPIO_INPUT_HI_Z:
2083                 DP(NETIF_MSG_LINK, "Set SPIO %d -> input\n", spio_num);
2084                 /* set FLOAT */
2085                 spio_reg |= (spio_mask << MISC_REGISTERS_SPIO_FLOAT_POS);
2086                 break;
2087
2088         default:
2089                 break;
2090         }
2091
2092         REG_WR(bp, MISC_REG_SPIO, spio_reg);
2093         bnx2x_release_hw_lock(bp, HW_LOCK_RESOURCE_SPIO);
2094
2095         return 0;
2096 }
2097
2098 void bnx2x_calc_fc_adv(struct bnx2x *bp)
2099 {
2100         u8 cfg_idx = bnx2x_get_link_cfg_idx(bp);
2101         switch (bp->link_vars.ieee_fc &
2102                 MDIO_COMBO_IEEE0_AUTO_NEG_ADV_PAUSE_MASK) {
2103         case MDIO_COMBO_IEEE0_AUTO_NEG_ADV_PAUSE_NONE:
2104                 bp->port.advertising[cfg_idx] &= ~(ADVERTISED_Asym_Pause |
2105                                                    ADVERTISED_Pause);
2106                 break;
2107
2108         case MDIO_COMBO_IEEE0_AUTO_NEG_ADV_PAUSE_BOTH:
2109                 bp->port.advertising[cfg_idx] |= (ADVERTISED_Asym_Pause |
2110                                                   ADVERTISED_Pause);
2111                 break;
2112
2113         case MDIO_COMBO_IEEE0_AUTO_NEG_ADV_PAUSE_ASYMMETRIC:
2114                 bp->port.advertising[cfg_idx] |= ADVERTISED_Asym_Pause;
2115                 break;
2116
2117         default:
2118                 bp->port.advertising[cfg_idx] &= ~(ADVERTISED_Asym_Pause |
2119                                                    ADVERTISED_Pause);
2120                 break;
2121         }
2122 }
2123
2124 u8 bnx2x_initial_phy_init(struct bnx2x *bp, int load_mode)
2125 {
2126         if (!BP_NOMCP(bp)) {
2127                 u8 rc;
2128                 int cfx_idx = bnx2x_get_link_cfg_idx(bp);
2129                 u16 req_line_speed = bp->link_params.req_line_speed[cfx_idx];
2130                 /* Initialize link parameters structure variables */
2131                 /* It is recommended to turn off RX FC for jumbo frames
2132                    for better performance */
2133                 if ((CHIP_IS_E1x(bp)) && (bp->dev->mtu > 5000))
2134                         bp->link_params.req_fc_auto_adv = BNX2X_FLOW_CTRL_TX;
2135                 else
2136                         bp->link_params.req_fc_auto_adv = BNX2X_FLOW_CTRL_BOTH;
2137
2138                 bnx2x_acquire_phy_lock(bp);
2139
2140                 if (load_mode == LOAD_DIAG) {
2141                         bp->link_params.loopback_mode = LOOPBACK_XGXS;
2142                         bp->link_params.req_line_speed[cfx_idx] = SPEED_10000;
2143                 }
2144
2145                 rc = bnx2x_phy_init(&bp->link_params, &bp->link_vars);
2146
2147                 bnx2x_release_phy_lock(bp);
2148
2149                 bnx2x_calc_fc_adv(bp);
2150
2151                 if (CHIP_REV_IS_SLOW(bp) && bp->link_vars.link_up) {
2152                         bnx2x_stats_handle(bp, STATS_EVENT_LINK_UP);
2153                         bnx2x_link_report(bp);
2154                 } else
2155                         queue_delayed_work(bnx2x_wq, &bp->period_task, 0);
2156                 bp->link_params.req_line_speed[cfx_idx] = req_line_speed;
2157                 return rc;
2158         }
2159         BNX2X_ERR("Bootcode is missing - can not initialize link\n");
2160         return -EINVAL;
2161 }
2162
2163 void bnx2x_link_set(struct bnx2x *bp)
2164 {
2165         if (!BP_NOMCP(bp)) {
2166                 bnx2x_acquire_phy_lock(bp);
2167                 bnx2x_link_reset(&bp->link_params, &bp->link_vars, 1);
2168                 bnx2x_phy_init(&bp->link_params, &bp->link_vars);
2169                 bnx2x_release_phy_lock(bp);
2170
2171                 bnx2x_calc_fc_adv(bp);
2172         } else
2173                 BNX2X_ERR("Bootcode is missing - can not set link\n");
2174 }
2175
2176 static void bnx2x__link_reset(struct bnx2x *bp)
2177 {
2178         if (!BP_NOMCP(bp)) {
2179                 bnx2x_acquire_phy_lock(bp);
2180                 bnx2x_link_reset(&bp->link_params, &bp->link_vars, 1);
2181                 bnx2x_release_phy_lock(bp);
2182         } else
2183                 BNX2X_ERR("Bootcode is missing - can not reset link\n");
2184 }
2185
2186 u8 bnx2x_link_test(struct bnx2x *bp, u8 is_serdes)
2187 {
2188         u8 rc = 0;
2189
2190         if (!BP_NOMCP(bp)) {
2191                 bnx2x_acquire_phy_lock(bp);
2192                 rc = bnx2x_test_link(&bp->link_params, &bp->link_vars,
2193                                      is_serdes);
2194                 bnx2x_release_phy_lock(bp);
2195         } else
2196                 BNX2X_ERR("Bootcode is missing - can not test link\n");
2197
2198         return rc;
2199 }
2200
2201 static void bnx2x_init_port_minmax(struct bnx2x *bp)
2202 {
2203         u32 r_param = bp->link_vars.line_speed / 8;
2204         u32 fair_periodic_timeout_usec;
2205         u32 t_fair;
2206
2207         memset(&(bp->cmng.rs_vars), 0,
2208                sizeof(struct rate_shaping_vars_per_port));
2209         memset(&(bp->cmng.fair_vars), 0, sizeof(struct fairness_vars_per_port));
2210
2211         /* 100 usec in SDM ticks = 25 since each tick is 4 usec */
2212         bp->cmng.rs_vars.rs_periodic_timeout = RS_PERIODIC_TIMEOUT_USEC / 4;
2213
2214         /* this is the threshold below which no timer arming will occur
2215            1.25 coefficient is for the threshold to be a little bigger
2216            than the real time, to compensate for timer in-accuracy */
2217         bp->cmng.rs_vars.rs_threshold =
2218                                 (RS_PERIODIC_TIMEOUT_USEC * r_param * 5) / 4;
2219
2220         /* resolution of fairness timer */
2221         fair_periodic_timeout_usec = QM_ARB_BYTES / r_param;
2222         /* for 10G it is 1000usec. for 1G it is 10000usec. */
2223         t_fair = T_FAIR_COEF / bp->link_vars.line_speed;
2224
2225         /* this is the threshold below which we won't arm the timer anymore */
2226         bp->cmng.fair_vars.fair_threshold = QM_ARB_BYTES;
2227
2228         /* we multiply by 1e3/8 to get bytes/msec.
2229            We don't want the credits to pass a credit
2230            of the t_fair*FAIR_MEM (algorithm resolution) */
2231         bp->cmng.fair_vars.upper_bound = r_param * t_fair * FAIR_MEM;
2232         /* since each tick is 4 usec */
2233         bp->cmng.fair_vars.fairness_timeout = fair_periodic_timeout_usec / 4;
2234 }
2235
2236 /* Calculates the sum of vn_min_rates.
2237    It's needed for further normalizing of the min_rates.
2238    Returns:
2239      sum of vn_min_rates.
2240        or
2241      0 - if all the min_rates are 0.
2242      In the later case fainess algorithm should be deactivated.
2243      If not all min_rates are zero then those that are zeroes will be set to 1.
2244  */
2245 static void bnx2x_calc_vn_weight_sum(struct bnx2x *bp)
2246 {
2247         int all_zero = 1;
2248         int vn;
2249
2250         bp->vn_weight_sum = 0;
2251         for (vn = VN_0; vn < E1HVN_MAX; vn++) {
2252                 u32 vn_cfg = bp->mf_config[vn];
2253                 u32 vn_min_rate = ((vn_cfg & FUNC_MF_CFG_MIN_BW_MASK) >>
2254                                    FUNC_MF_CFG_MIN_BW_SHIFT) * 100;
2255
2256                 /* Skip hidden vns */
2257                 if (vn_cfg & FUNC_MF_CFG_FUNC_HIDE)
2258                         continue;
2259
2260                 /* If min rate is zero - set it to 1 */
2261                 if (!vn_min_rate)
2262                         vn_min_rate = DEF_MIN_RATE;
2263                 else
2264                         all_zero = 0;
2265
2266                 bp->vn_weight_sum += vn_min_rate;
2267         }
2268
2269         /* if ETS or all min rates are zeros - disable fairness */
2270         if (BNX2X_IS_ETS_ENABLED(bp)) {
2271                 bp->cmng.flags.cmng_enables &=
2272                                         ~CMNG_FLAGS_PER_PORT_FAIRNESS_VN;
2273                 DP(NETIF_MSG_IFUP, "Fairness will be disabled due to ETS\n");
2274         } else if (all_zero) {
2275                 bp->cmng.flags.cmng_enables &=
2276                                         ~CMNG_FLAGS_PER_PORT_FAIRNESS_VN;
2277                 DP(NETIF_MSG_IFUP, "All MIN values are zeroes"
2278                    "  fairness will be disabled\n");
2279         } else
2280                 bp->cmng.flags.cmng_enables |=
2281                                         CMNG_FLAGS_PER_PORT_FAIRNESS_VN;
2282 }
2283
2284 static void bnx2x_init_vn_minmax(struct bnx2x *bp, int vn)
2285 {
2286         struct rate_shaping_vars_per_vn m_rs_vn;
2287         struct fairness_vars_per_vn m_fair_vn;
2288         u32 vn_cfg = bp->mf_config[vn];
2289         int func = 2*vn + BP_PORT(bp);
2290         u16 vn_min_rate, vn_max_rate;
2291         int i;
2292
2293         /* If function is hidden - set min and max to zeroes */
2294         if (vn_cfg & FUNC_MF_CFG_FUNC_HIDE) {
2295                 vn_min_rate = 0;
2296                 vn_max_rate = 0;
2297
2298         } else {
2299                 u32 maxCfg = bnx2x_extract_max_cfg(bp, vn_cfg);
2300
2301                 vn_min_rate = ((vn_cfg & FUNC_MF_CFG_MIN_BW_MASK) >>
2302                                 FUNC_MF_CFG_MIN_BW_SHIFT) * 100;
2303                 /* If fairness is enabled (not all min rates are zeroes) and
2304                    if current min rate is zero - set it to 1.
2305                    This is a requirement of the algorithm. */
2306                 if (bp->vn_weight_sum && (vn_min_rate == 0))
2307                         vn_min_rate = DEF_MIN_RATE;
2308
2309                 if (IS_MF_SI(bp))
2310                         /* maxCfg in percents of linkspeed */
2311                         vn_max_rate = (bp->link_vars.line_speed * maxCfg) / 100;
2312                 else
2313                         /* maxCfg is absolute in 100Mb units */
2314                         vn_max_rate = maxCfg * 100;
2315         }
2316
2317         DP(NETIF_MSG_IFUP,
2318            "func %d: vn_min_rate %d  vn_max_rate %d  vn_weight_sum %d\n",
2319            func, vn_min_rate, vn_max_rate, bp->vn_weight_sum);
2320
2321         memset(&m_rs_vn, 0, sizeof(struct rate_shaping_vars_per_vn));
2322         memset(&m_fair_vn, 0, sizeof(struct fairness_vars_per_vn));
2323
2324         /* global vn counter - maximal Mbps for this vn */
2325         m_rs_vn.vn_counter.rate = vn_max_rate;
2326
2327         /* quota - number of bytes transmitted in this period */
2328         m_rs_vn.vn_counter.quota =
2329                                 (vn_max_rate * RS_PERIODIC_TIMEOUT_USEC) / 8;
2330
2331         if (bp->vn_weight_sum) {
2332                 /* credit for each period of the fairness algorithm:
2333                    number of bytes in T_FAIR (the vn share the port rate).
2334                    vn_weight_sum should not be larger than 10000, thus
2335                    T_FAIR_COEF / (8 * vn_weight_sum) will always be greater
2336                    than zero */
2337                 m_fair_vn.vn_credit_delta =
2338                         max_t(u32, (vn_min_rate * (T_FAIR_COEF /
2339                                                    (8 * bp->vn_weight_sum))),
2340                               (bp->cmng.fair_vars.fair_threshold +
2341                                                         MIN_ABOVE_THRESH));
2342                 DP(NETIF_MSG_IFUP, "m_fair_vn.vn_credit_delta %d\n",
2343                    m_fair_vn.vn_credit_delta);
2344         }
2345
2346         /* Store it to internal memory */
2347         for (i = 0; i < sizeof(struct rate_shaping_vars_per_vn)/4; i++)
2348                 REG_WR(bp, BAR_XSTRORM_INTMEM +
2349                        XSTORM_RATE_SHAPING_PER_VN_VARS_OFFSET(func) + i * 4,
2350                        ((u32 *)(&m_rs_vn))[i]);
2351
2352         for (i = 0; i < sizeof(struct fairness_vars_per_vn)/4; i++)
2353                 REG_WR(bp, BAR_XSTRORM_INTMEM +
2354                        XSTORM_FAIRNESS_PER_VN_VARS_OFFSET(func) + i * 4,
2355                        ((u32 *)(&m_fair_vn))[i]);
2356 }
2357
2358 static int bnx2x_get_cmng_fns_mode(struct bnx2x *bp)
2359 {
2360         if (CHIP_REV_IS_SLOW(bp))
2361                 return CMNG_FNS_NONE;
2362         if (IS_MF(bp))
2363                 return CMNG_FNS_MINMAX;
2364
2365         return CMNG_FNS_NONE;
2366 }
2367
2368 void bnx2x_read_mf_cfg(struct bnx2x *bp)
2369 {
2370         int vn, n = (CHIP_MODE_IS_4_PORT(bp) ? 2 : 1);
2371
2372         if (BP_NOMCP(bp))
2373                 return; /* what should be the default bvalue in this case */
2374
2375         /* For 2 port configuration the absolute function number formula
2376          * is:
2377          *      abs_func = 2 * vn + BP_PORT + BP_PATH
2378          *
2379          *      and there are 4 functions per port
2380          *
2381          * For 4 port configuration it is
2382          *      abs_func = 4 * vn + 2 * BP_PORT + BP_PATH
2383          *
2384          *      and there are 2 functions per port
2385          */
2386         for (vn = VN_0; vn < E1HVN_MAX; vn++) {
2387                 int /*abs*/func = n * (2 * vn + BP_PORT(bp)) + BP_PATH(bp);
2388
2389                 if (func >= E1H_FUNC_MAX)
2390                         break;
2391
2392                 bp->mf_config[vn] =
2393                         MF_CFG_RD(bp, func_mf_config[func].config);
2394         }
2395 }
2396
2397 static void bnx2x_cmng_fns_init(struct bnx2x *bp, u8 read_cfg, u8 cmng_type)
2398 {
2399
2400         if (cmng_type == CMNG_FNS_MINMAX) {
2401                 int vn;
2402
2403                 /* clear cmng_enables */
2404                 bp->cmng.flags.cmng_enables = 0;
2405
2406                 /* read mf conf from shmem */
2407                 if (read_cfg)
2408                         bnx2x_read_mf_cfg(bp);
2409
2410                 /* Init rate shaping and fairness contexts */
2411                 bnx2x_init_port_minmax(bp);
2412
2413                 /* vn_weight_sum and enable fairness if not 0 */
2414                 bnx2x_calc_vn_weight_sum(bp);
2415
2416                 /* calculate and set min-max rate for each vn */
2417                 if (bp->port.pmf)
2418                         for (vn = VN_0; vn < E1HVN_MAX; vn++)
2419                                 bnx2x_init_vn_minmax(bp, vn);
2420
2421                 /* always enable rate shaping and fairness */
2422                 bp->cmng.flags.cmng_enables |=
2423                                         CMNG_FLAGS_PER_PORT_RATE_SHAPING_VN;
2424                 if (!bp->vn_weight_sum)
2425                         DP(NETIF_MSG_IFUP, "All MIN values are zeroes"
2426                                    "  fairness will be disabled\n");
2427                 return;
2428         }
2429
2430         /* rate shaping and fairness are disabled */
2431         DP(NETIF_MSG_IFUP,
2432            "rate shaping and fairness are disabled\n");
2433 }
2434
2435 static inline void bnx2x_link_sync_notify(struct bnx2x *bp)
2436 {
2437         int port = BP_PORT(bp);
2438         int func;
2439         int vn;
2440
2441         /* Set the attention towards other drivers on the same port */
2442         for (vn = VN_0; vn < E1HVN_MAX; vn++) {
2443                 if (vn == BP_E1HVN(bp))
2444                         continue;
2445
2446                 func = ((vn << 1) | port);
2447                 REG_WR(bp, MISC_REG_AEU_GENERAL_ATTN_0 +
2448                        (LINK_SYNC_ATTENTION_BIT_FUNC_0 + func)*4, 1);
2449         }
2450 }
2451
2452 /* This function is called upon link interrupt */
2453 static void bnx2x_link_attn(struct bnx2x *bp)
2454 {
2455         /* Make sure that we are synced with the current statistics */
2456         bnx2x_stats_handle(bp, STATS_EVENT_STOP);
2457
2458         bnx2x_link_update(&bp->link_params, &bp->link_vars);
2459
2460         if (bp->link_vars.link_up) {
2461
2462                 /* dropless flow control */
2463                 if (!CHIP_IS_E1(bp) && bp->dropless_fc) {
2464                         int port = BP_PORT(bp);
2465                         u32 pause_enabled = 0;
2466
2467                         if (bp->link_vars.flow_ctrl & BNX2X_FLOW_CTRL_TX)
2468                                 pause_enabled = 1;
2469
2470                         REG_WR(bp, BAR_USTRORM_INTMEM +
2471                                USTORM_ETH_PAUSE_ENABLED_OFFSET(port),
2472                                pause_enabled);
2473                 }
2474
2475                 if (bp->link_vars.mac_type != MAC_TYPE_EMAC) {
2476                         struct host_port_stats *pstats;
2477
2478                         pstats = bnx2x_sp(bp, port_stats);
2479                         /* reset old mac stats */
2480                         memset(&(pstats->mac_stx[0]), 0,
2481                                sizeof(struct mac_stx));
2482                 }
2483                 if (bp->state == BNX2X_STATE_OPEN)
2484                         bnx2x_stats_handle(bp, STATS_EVENT_LINK_UP);
2485         }
2486
2487         if (bp->link_vars.link_up && bp->link_vars.line_speed) {
2488                 int cmng_fns = bnx2x_get_cmng_fns_mode(bp);
2489
2490                 if (cmng_fns != CMNG_FNS_NONE) {
2491                         bnx2x_cmng_fns_init(bp, false, cmng_fns);
2492                         storm_memset_cmng(bp, &bp->cmng, BP_PORT(bp));
2493                 } else
2494                         /* rate shaping and fairness are disabled */
2495                         DP(NETIF_MSG_IFUP,
2496                            "single function mode without fairness\n");
2497         }
2498
2499         __bnx2x_link_report(bp);
2500
2501         if (IS_MF(bp))
2502                 bnx2x_link_sync_notify(bp);
2503 }
2504
2505 void bnx2x__link_status_update(struct bnx2x *bp)
2506 {
2507         if (bp->state != BNX2X_STATE_OPEN)
2508                 return;
2509
2510         bnx2x_link_status_update(&bp->link_params, &bp->link_vars);
2511
2512         if (bp->link_vars.link_up)
2513                 bnx2x_stats_handle(bp, STATS_EVENT_LINK_UP);
2514         else
2515                 bnx2x_stats_handle(bp, STATS_EVENT_STOP);
2516
2517         /* indicate link status */
2518         bnx2x_link_report(bp);
2519 }
2520
2521 static void bnx2x_pmf_update(struct bnx2x *bp)
2522 {
2523         int port = BP_PORT(bp);
2524         u32 val;
2525
2526         bp->port.pmf = 1;
2527         DP(NETIF_MSG_LINK, "pmf %d\n", bp->port.pmf);
2528
2529         /*
2530          * We need the mb() to ensure the ordering between the writing to
2531          * bp->port.pmf here and reading it from the bnx2x_periodic_task().
2532          */
2533         smp_mb();
2534
2535         /* queue a periodic task */
2536         queue_delayed_work(bnx2x_wq, &bp->period_task, 0);
2537
2538         bnx2x_dcbx_pmf_update(bp);
2539
2540         /* enable nig attention */
2541         val = (0xff0f | (1 << (BP_E1HVN(bp) + 4)));
2542         if (bp->common.int_block == INT_BLOCK_HC) {
2543                 REG_WR(bp, HC_REG_TRAILING_EDGE_0 + port*8, val);
2544                 REG_WR(bp, HC_REG_LEADING_EDGE_0 + port*8, val);
2545         } else if (!CHIP_IS_E1x(bp)) {
2546                 REG_WR(bp, IGU_REG_TRAILING_EDGE_LATCH, val);
2547                 REG_WR(bp, IGU_REG_LEADING_EDGE_LATCH, val);
2548         }
2549
2550         bnx2x_stats_handle(bp, STATS_EVENT_PMF);
2551 }
2552
2553 /* end of Link */
2554
2555 /* slow path */
2556
2557 /*
2558  * General service functions
2559  */
2560
2561 /* send the MCP a request, block until there is a reply */
2562 u32 bnx2x_fw_command(struct bnx2x *bp, u32 command, u32 param)
2563 {
2564         int mb_idx = BP_FW_MB_IDX(bp);
2565         u32 seq;
2566         u32 rc = 0;
2567         u32 cnt = 1;
2568         u8 delay = CHIP_REV_IS_SLOW(bp) ? 100 : 10;
2569
2570         mutex_lock(&bp->fw_mb_mutex);
2571         seq = ++bp->fw_seq;
2572         SHMEM_WR(bp, func_mb[mb_idx].drv_mb_param, param);
2573         SHMEM_WR(bp, func_mb[mb_idx].drv_mb_header, (command | seq));
2574
2575         DP(BNX2X_MSG_MCP, "wrote command (%x) to FW MB param 0x%08x\n",
2576                         (command | seq), param);
2577
2578         do {
2579                 /* let the FW do it's magic ... */
2580                 msleep(delay);
2581
2582                 rc = SHMEM_RD(bp, func_mb[mb_idx].fw_mb_header);
2583
2584                 /* Give the FW up to 5 second (500*10ms) */
2585         } while ((seq != (rc & FW_MSG_SEQ_NUMBER_MASK)) && (cnt++ < 500));
2586
2587         DP(BNX2X_MSG_MCP, "[after %d ms] read (%x) seq is (%x) from FW MB\n",
2588            cnt*delay, rc, seq);
2589
2590         /* is this a reply to our command? */
2591         if (seq == (rc & FW_MSG_SEQ_NUMBER_MASK))
2592                 rc &= FW_MSG_CODE_MASK;
2593         else {
2594                 /* FW BUG! */
2595                 BNX2X_ERR("FW failed to respond!\n");
2596                 bnx2x_fw_dump(bp);
2597                 rc = 0;
2598         }
2599         mutex_unlock(&bp->fw_mb_mutex);
2600
2601         return rc;
2602 }
2603
2604 static u8 stat_counter_valid(struct bnx2x *bp, struct bnx2x_fastpath *fp)
2605 {
2606 #ifdef BCM_CNIC
2607         /* Statistics are not supported for CNIC Clients at the moment */
2608         if (IS_FCOE_FP(fp))
2609                 return false;
2610 #endif
2611         return true;
2612 }
2613
2614 void bnx2x_func_init(struct bnx2x *bp, struct bnx2x_func_init_params *p)
2615 {
2616         if (CHIP_IS_E1x(bp)) {
2617                 struct tstorm_eth_function_common_config tcfg = {0};
2618
2619                 storm_memset_func_cfg(bp, &tcfg, p->func_id);
2620         }
2621
2622         /* Enable the function in the FW */
2623         storm_memset_vf_to_pf(bp, p->func_id, p->pf_id);
2624         storm_memset_func_en(bp, p->func_id, 1);
2625
2626         /* spq */
2627         if (p->func_flgs & FUNC_FLG_SPQ) {
2628                 storm_memset_spq_addr(bp, p->spq_map, p->func_id);
2629                 REG_WR(bp, XSEM_REG_FAST_MEMORY +
2630                        XSTORM_SPQ_PROD_OFFSET(p->func_id), p->spq_prod);
2631         }
2632 }
2633
2634 static inline unsigned long bnx2x_get_q_flags(struct bnx2x *bp,
2635                                               struct bnx2x_fastpath *fp,
2636                                               bool leading)
2637 {
2638         unsigned long flags = 0;
2639
2640         /* PF driver will always initialize the Queue to an ACTIVE state */
2641         __set_bit(BNX2X_Q_FLG_ACTIVE, &flags);
2642
2643         /* calculate other queue flags */
2644         if (IS_MF_SD(bp))
2645                 __set_bit(BNX2X_Q_FLG_OV, &flags);
2646
2647         if (IS_FCOE_FP(fp))
2648                 __set_bit(BNX2X_Q_FLG_FCOE, &flags);
2649
2650         if (!fp->disable_tpa)
2651                 __set_bit(BNX2X_Q_FLG_TPA, &flags);
2652
2653         if (stat_counter_valid(bp, fp)) {
2654                 __set_bit(BNX2X_Q_FLG_STATS, &flags);
2655                 __set_bit(BNX2X_Q_FLG_ZERO_STATS, &flags);
2656         }
2657
2658         if (leading) {
2659                 __set_bit(BNX2X_Q_FLG_LEADING_RSS, &flags);
2660                 __set_bit(BNX2X_Q_FLG_MCAST, &flags);
2661         }
2662
2663         /* Always set HW VLAN stripping */
2664         __set_bit(BNX2X_Q_FLG_VLAN, &flags);
2665
2666         return flags;
2667 }
2668
2669 static void bnx2x_pf_q_prep_general(struct bnx2x *bp,
2670         struct bnx2x_fastpath *fp, struct bnx2x_general_setup_params *gen_init)
2671 {
2672         gen_init->stat_id = bnx2x_stats_id(fp);
2673         gen_init->spcl_id = fp->cl_id;
2674
2675         /* Always use mini-jumbo MTU for FCoE L2 ring */
2676         if (IS_FCOE_FP(fp))
2677                 gen_init->mtu = BNX2X_FCOE_MINI_JUMBO_MTU;
2678         else
2679                 gen_init->mtu = bp->dev->mtu;
2680 }
2681
2682 static void bnx2x_pf_rx_q_prep(struct bnx2x *bp,
2683         struct bnx2x_fastpath *fp, struct rxq_pause_params *pause,
2684         struct bnx2x_rxq_setup_params *rxq_init)
2685 {
2686         u8 max_sge = 0;
2687         u16 sge_sz = 0;
2688         u16 tpa_agg_size = 0;
2689
2690         if (!fp->disable_tpa) {
2691                 pause->sge_th_hi = 250;
2692                 pause->sge_th_lo = 150;
2693                 tpa_agg_size = min_t(u32,
2694                         (min_t(u32, 8, MAX_SKB_FRAGS) *
2695                         SGE_PAGE_SIZE * PAGES_PER_SGE), 0xffff);
2696                 max_sge = SGE_PAGE_ALIGN(bp->dev->mtu) >>
2697                         SGE_PAGE_SHIFT;
2698                 max_sge = ((max_sge + PAGES_PER_SGE - 1) &
2699                           (~(PAGES_PER_SGE-1))) >> PAGES_PER_SGE_SHIFT;
2700                 sge_sz = (u16)min_t(u32, SGE_PAGE_SIZE * PAGES_PER_SGE,
2701                                     0xffff);
2702         }
2703
2704         /* pause - not for e1 */
2705         if (!CHIP_IS_E1(bp)) {
2706                 pause->bd_th_hi = 350;
2707                 pause->bd_th_lo = 250;
2708                 pause->rcq_th_hi = 350;
2709                 pause->rcq_th_lo = 250;
2710
2711                 pause->pri_map = 1;
2712         }
2713
2714         /* rxq setup */
2715         rxq_init->dscr_map = fp->rx_desc_mapping;
2716         rxq_init->sge_map = fp->rx_sge_mapping;
2717         rxq_init->rcq_map = fp->rx_comp_mapping;
2718         rxq_init->rcq_np_map = fp->rx_comp_mapping + BCM_PAGE_SIZE;
2719
2720         /* This should be a maximum number of data bytes that may be
2721          * placed on the BD (not including paddings).
2722          */
2723         rxq_init->buf_sz = fp->rx_buf_size - BNX2X_FW_RX_ALIGN -
2724                 IP_HEADER_ALIGNMENT_PADDING;
2725
2726         rxq_init->cl_qzone_id = fp->cl_qzone_id;
2727         rxq_init->tpa_agg_sz = tpa_agg_size;
2728         rxq_init->sge_buf_sz = sge_sz;
2729         rxq_init->max_sges_pkt = max_sge;
2730         rxq_init->rss_engine_id = BP_FUNC(bp);
2731
2732         /* Maximum number or simultaneous TPA aggregation for this Queue.
2733          *
2734          * For PF Clients it should be the maximum avaliable number.
2735          * VF driver(s) may want to define it to a smaller value.
2736          */
2737         rxq_init->max_tpa_queues =
2738                 (CHIP_IS_E1(bp) ? ETH_MAX_AGGREGATION_QUEUES_E1 :
2739                 ETH_MAX_AGGREGATION_QUEUES_E1H_E2);
2740
2741         rxq_init->cache_line_log = BNX2X_RX_ALIGN_SHIFT;
2742         rxq_init->fw_sb_id = fp->fw_sb_id;
2743
2744         if (IS_FCOE_FP(fp))
2745                 rxq_init->sb_cq_index = HC_SP_INDEX_ETH_FCOE_RX_CQ_CONS;
2746         else
2747                 rxq_init->sb_cq_index = U_SB_ETH_RX_CQ_INDEX;
2748 }
2749
2750 static void bnx2x_pf_tx_q_prep(struct bnx2x *bp,
2751         struct bnx2x_fastpath *fp, struct bnx2x_txq_setup_params *txq_init)
2752 {
2753         txq_init->dscr_map = fp->tx_desc_mapping;
2754         txq_init->sb_cq_index = C_SB_ETH_TX_CQ_INDEX;
2755         txq_init->traffic_type = LLFC_TRAFFIC_TYPE_NW;
2756         txq_init->fw_sb_id = fp->fw_sb_id;
2757
2758         /*
2759          * set the tss leading client id for TX classfication ==
2760          * leading RSS client id
2761          */
2762         txq_init->tss_leading_cl_id = bnx2x_fp(bp, 0, cl_id);
2763
2764         if (IS_FCOE_FP(fp)) {
2765                 txq_init->sb_cq_index = HC_SP_INDEX_ETH_FCOE_TX_CQ_CONS;
2766                 txq_init->traffic_type = LLFC_TRAFFIC_TYPE_FCOE;
2767         }
2768 }
2769
2770 static void bnx2x_pf_init(struct bnx2x *bp)
2771 {
2772         struct bnx2x_func_init_params func_init = {0};
2773         struct event_ring_data eq_data = { {0} };
2774         u16 flags;
2775
2776         if (!CHIP_IS_E1x(bp)) {
2777                 /* reset IGU PF statistics: MSIX + ATTN */
2778                 /* PF */
2779                 REG_WR(bp, IGU_REG_STATISTIC_NUM_MESSAGE_SENT +
2780                            BNX2X_IGU_STAS_MSG_VF_CNT*4 +
2781                            (CHIP_MODE_IS_4_PORT(bp) ?
2782                                 BP_FUNC(bp) : BP_VN(bp))*4, 0);
2783                 /* ATTN */
2784                 REG_WR(bp, IGU_REG_STATISTIC_NUM_MESSAGE_SENT +
2785                            BNX2X_IGU_STAS_MSG_VF_CNT*4 +
2786                            BNX2X_IGU_STAS_MSG_PF_CNT*4 +
2787                            (CHIP_MODE_IS_4_PORT(bp) ?
2788                                 BP_FUNC(bp) : BP_VN(bp))*4, 0);
2789         }
2790
2791         /* function setup flags */
2792         flags = (FUNC_FLG_STATS | FUNC_FLG_LEADING | FUNC_FLG_SPQ);
2793
2794         /* This flag is relevant for E1x only.
2795          * E2 doesn't have a TPA configuration in a function level.
2796          */
2797         flags |= (bp->flags & TPA_ENABLE_FLAG) ? FUNC_FLG_TPA : 0;
2798
2799         func_init.func_flgs = flags;
2800         func_init.pf_id = BP_FUNC(bp);
2801         func_init.func_id = BP_FUNC(bp);
2802         func_init.spq_map = bp->spq_mapping;
2803         func_init.spq_prod = bp->spq_prod_idx;
2804
2805         bnx2x_func_init(bp, &func_init);
2806
2807         memset(&(bp->cmng), 0, sizeof(struct cmng_struct_per_port));
2808
2809         /*
2810          * Congestion management values depend on the link rate
2811          * There is no active link so initial link rate is set to 10 Gbps.
2812          * When the link comes up The congestion management values are
2813          * re-calculated according to the actual link rate.
2814          */
2815         bp->link_vars.line_speed = SPEED_10000;
2816         bnx2x_cmng_fns_init(bp, true, bnx2x_get_cmng_fns_mode(bp));
2817
2818         /* Only the PMF sets the HW */
2819         if (bp->port.pmf)
2820                 storm_memset_cmng(bp, &bp->cmng, BP_PORT(bp));
2821
2822         /* init Event Queue */
2823         eq_data.base_addr.hi = U64_HI(bp->eq_mapping);
2824         eq_data.base_addr.lo = U64_LO(bp->eq_mapping);
2825         eq_data.producer = bp->eq_prod;
2826         eq_data.index_id = HC_SP_INDEX_EQ_CONS;
2827         eq_data.sb_id = DEF_SB_ID;
2828         storm_memset_eq_data(bp, &eq_data, BP_FUNC(bp));
2829 }
2830
2831
2832 static void bnx2x_e1h_disable(struct bnx2x *bp)
2833 {
2834         int port = BP_PORT(bp);
2835
2836         bnx2x_tx_disable(bp);
2837
2838         REG_WR(bp, NIG_REG_LLH0_FUNC_EN + port*8, 0);
2839 }
2840
2841 static void bnx2x_e1h_enable(struct bnx2x *bp)
2842 {
2843         int port = BP_PORT(bp);
2844
2845         REG_WR(bp, NIG_REG_LLH0_FUNC_EN + port*8, 1);
2846
2847         /* Tx queue should be only reenabled */
2848         netif_tx_wake_all_queues(bp->dev);
2849
2850         /*
2851          * Should not call netif_carrier_on since it will be called if the link
2852          * is up when checking for link state
2853          */
2854 }
2855
2856 /* called due to MCP event (on pmf):
2857  *      reread new bandwidth configuration
2858  *      configure FW
2859  *      notify others function about the change
2860  */
2861 static inline void bnx2x_config_mf_bw(struct bnx2x *bp)
2862 {
2863         if (bp->link_vars.link_up) {
2864                 bnx2x_cmng_fns_init(bp, true, CMNG_FNS_MINMAX);
2865                 bnx2x_link_sync_notify(bp);
2866         }
2867         storm_memset_cmng(bp, &bp->cmng, BP_PORT(bp));
2868 }
2869
2870 static inline void bnx2x_set_mf_bw(struct bnx2x *bp)
2871 {
2872         bnx2x_config_mf_bw(bp);
2873         bnx2x_fw_command(bp, DRV_MSG_CODE_SET_MF_BW_ACK, 0);
2874 }
2875
2876 static void bnx2x_dcc_event(struct bnx2x *bp, u32 dcc_event)
2877 {
2878         DP(BNX2X_MSG_MCP, "dcc_event 0x%x\n", dcc_event);
2879
2880         if (dcc_event & DRV_STATUS_DCC_DISABLE_ENABLE_PF) {
2881
2882                 /*
2883                  * This is the only place besides the function initialization
2884                  * where the bp->flags can change so it is done without any
2885                  * locks
2886                  */
2887                 if (bp->mf_config[BP_VN(bp)] & FUNC_MF_CFG_FUNC_DISABLED) {
2888                         DP(NETIF_MSG_IFDOWN, "mf_cfg function disabled\n");
2889                         bp->flags |= MF_FUNC_DIS;
2890
2891                         bnx2x_e1h_disable(bp);
2892                 } else {
2893                         DP(NETIF_MSG_IFUP, "mf_cfg function enabled\n");
2894                         bp->flags &= ~MF_FUNC_DIS;
2895
2896                         bnx2x_e1h_enable(bp);
2897                 }
2898                 dcc_event &= ~DRV_STATUS_DCC_DISABLE_ENABLE_PF;
2899         }
2900         if (dcc_event & DRV_STATUS_DCC_BANDWIDTH_ALLOCATION) {
2901                 bnx2x_config_mf_bw(bp);
2902                 dcc_event &= ~DRV_STATUS_DCC_BANDWIDTH_ALLOCATION;
2903         }
2904
2905         /* Report results to MCP */
2906         if (dcc_event)
2907                 bnx2x_fw_command(bp, DRV_MSG_CODE_DCC_FAILURE, 0);
2908         else
2909                 bnx2x_fw_command(bp, DRV_MSG_CODE_DCC_OK, 0);
2910 }
2911
2912 /* must be called under the spq lock */
2913 static inline struct eth_spe *bnx2x_sp_get_next(struct bnx2x *bp)
2914 {
2915         struct eth_spe *next_spe = bp->spq_prod_bd;
2916
2917         if (bp->spq_prod_bd == bp->spq_last_bd) {
2918                 bp->spq_prod_bd = bp->spq;
2919                 bp->spq_prod_idx = 0;
2920                 DP(NETIF_MSG_TIMER, "end of spq\n");
2921         } else {
2922                 bp->spq_prod_bd++;
2923                 bp->spq_prod_idx++;
2924         }
2925         return next_spe;
2926 }
2927
2928 /* must be called under the spq lock */
2929 static inline void bnx2x_sp_prod_update(struct bnx2x *bp)
2930 {
2931         int func = BP_FUNC(bp);
2932
2933         /* Make sure that BD data is updated before writing the producer */
2934         wmb();
2935
2936         REG_WR16(bp, BAR_XSTRORM_INTMEM + XSTORM_SPQ_PROD_OFFSET(func),
2937                  bp->spq_prod_idx);
2938         mmiowb();
2939 }
2940
2941 /**
2942  * bnx2x_is_contextless_ramrod - check if the current command ends on EQ
2943  *
2944  * @cmd:        command to check
2945  * @cmd_type:   command type
2946  */
2947 static inline bool bnx2x_is_contextless_ramrod(int cmd, int cmd_type)
2948 {
2949         if ((cmd_type == NONE_CONNECTION_TYPE) ||
2950             (cmd == RAMROD_CMD_ID_ETH_CLASSIFICATION_RULES) ||
2951             (cmd == RAMROD_CMD_ID_ETH_FILTER_RULES) ||
2952             (cmd == RAMROD_CMD_ID_ETH_MULTICAST_RULES) ||
2953             (cmd == RAMROD_CMD_ID_ETH_SET_MAC) ||
2954             (cmd == RAMROD_CMD_ID_ETH_RSS_UPDATE))
2955                 return true;
2956         else
2957                 return false;
2958
2959 }
2960
2961
2962 /**
2963  * bnx2x_sp_post - place a single command on an SP ring
2964  *
2965  * @bp:         driver handle
2966  * @command:    command to place (e.g. SETUP, FILTER_RULES, etc.)
2967  * @cid:        SW CID the command is related to
2968  * @data_hi:    command private data address (high 32 bits)
2969  * @data_lo:    command private data address (low 32 bits)
2970  * @cmd_type:   command type (e.g. NONE, ETH)
2971  *
2972  * SP data is handled as if it's always an address pair, thus data fields are
2973  * not swapped to little endian in upper functions. Instead this function swaps
2974  * data as if it's two u32 fields.
2975  */
2976 int bnx2x_sp_post(struct bnx2x *bp, int command, int cid,
2977                   u32 data_hi, u32 data_lo, int cmd_type)
2978 {
2979         struct eth_spe *spe;
2980         u16 type;
2981         bool common = bnx2x_is_contextless_ramrod(command, cmd_type);
2982
2983 #ifdef BNX2X_STOP_ON_ERROR
2984         if (unlikely(bp->panic))
2985                 return -EIO;
2986 #endif
2987
2988         spin_lock_bh(&bp->spq_lock);
2989
2990         if (common) {
2991                 if (!atomic_read(&bp->eq_spq_left)) {
2992                         BNX2X_ERR("BUG! EQ ring full!\n");
2993                         spin_unlock_bh(&bp->spq_lock);
2994                         bnx2x_panic();
2995                         return -EBUSY;
2996                 }
2997         } else if (!atomic_read(&bp->cq_spq_left)) {
2998                         BNX2X_ERR("BUG! SPQ ring full!\n");
2999                         spin_unlock_bh(&bp->spq_lock);
3000                         bnx2x_panic();
3001                         return -EBUSY;
3002         }
3003
3004         spe = bnx2x_sp_get_next(bp);
3005
3006         /* CID needs port number to be encoded int it */
3007         spe->hdr.conn_and_cmd_data =
3008                         cpu_to_le32((command << SPE_HDR_CMD_ID_SHIFT) |
3009                                     HW_CID(bp, cid));
3010
3011         type = (cmd_type << SPE_HDR_CONN_TYPE_SHIFT) & SPE_HDR_CONN_TYPE;
3012
3013         type |= ((BP_FUNC(bp) << SPE_HDR_FUNCTION_ID_SHIFT) &
3014                  SPE_HDR_FUNCTION_ID);
3015
3016         spe->hdr.type = cpu_to_le16(type);
3017
3018         spe->data.update_data_addr.hi = cpu_to_le32(data_hi);
3019         spe->data.update_data_addr.lo = cpu_to_le32(data_lo);
3020
3021         /* stats ramrod has it's own slot on the spq */
3022         if (command != RAMROD_CMD_ID_COMMON_STAT_QUERY) {
3023                 /*
3024                  * It's ok if the actual decrement is issued towards the memory
3025                  * somewhere between the spin_lock and spin_unlock. Thus no
3026                  * more explict memory barrier is needed.
3027                  */
3028                 if (common)
3029                         atomic_dec(&bp->eq_spq_left);
3030                 else
3031                         atomic_dec(&bp->cq_spq_left);
3032         }
3033
3034
3035         DP(BNX2X_MSG_SP/*NETIF_MSG_TIMER*/,
3036            "SPQE[%x] (%x:%x)  command %d  hw_cid %x  data (%x:%x) "
3037            "type(0x%x) left (ETH, COMMON) (%x,%x)\n",
3038            bp->spq_prod_idx, (u32)U64_HI(bp->spq_mapping),
3039            (u32)(U64_LO(bp->spq_mapping) +
3040            (void *)bp->spq_prod_bd - (void *)bp->spq), command,
3041            HW_CID(bp, cid), data_hi, data_lo, type,
3042            atomic_read(&bp->cq_spq_left), atomic_read(&bp->eq_spq_left));
3043
3044         bnx2x_sp_prod_update(bp);
3045         spin_unlock_bh(&bp->spq_lock);
3046         return 0;
3047 }
3048
3049 /* acquire split MCP access lock register */
3050 static int bnx2x_acquire_alr(struct bnx2x *bp)
3051 {
3052         u32 j, val;
3053         int rc = 0;
3054
3055         might_sleep();
3056         for (j = 0; j < 1000; j++) {
3057                 val = (1UL << 31);
3058                 REG_WR(bp, GRCBASE_MCP + 0x9c, val);
3059                 val = REG_RD(bp, GRCBASE_MCP + 0x9c);
3060                 if (val & (1L << 31))
3061                         break;
3062
3063                 msleep(5);
3064         }
3065         if (!(val & (1L << 31))) {
3066                 BNX2X_ERR("Cannot acquire MCP access lock register\n");
3067                 rc = -EBUSY;
3068         }
3069
3070         return rc;
3071 }
3072
3073 /* release split MCP access lock register */
3074 static void bnx2x_release_alr(struct bnx2x *bp)
3075 {
3076         REG_WR(bp, GRCBASE_MCP + 0x9c, 0);
3077 }
3078
3079 #define BNX2X_DEF_SB_ATT_IDX    0x0001
3080 #define BNX2X_DEF_SB_IDX        0x0002
3081
3082 static inline u16 bnx2x_update_dsb_idx(struct bnx2x *bp)
3083 {
3084         struct host_sp_status_block *def_sb = bp->def_status_blk;
3085         u16 rc = 0;
3086
3087         barrier(); /* status block is written to by the chip */
3088         if (bp->def_att_idx != def_sb->atten_status_block.attn_bits_index) {
3089                 bp->def_att_idx = def_sb->atten_status_block.attn_bits_index;
3090                 rc |= BNX2X_DEF_SB_ATT_IDX;
3091         }
3092
3093         if (bp->def_idx != def_sb->sp_sb.running_index) {
3094                 bp->def_idx = def_sb->sp_sb.running_index;
3095                 rc |= BNX2X_DEF_SB_IDX;
3096         }
3097
3098         /* Do not reorder: indecies reading should complete before handling */
3099         barrier();
3100         return rc;
3101 }
3102
3103 /*
3104  * slow path service functions
3105  */
3106
3107 static void bnx2x_attn_int_asserted(struct bnx2x *bp, u32 asserted)
3108 {
3109         int port = BP_PORT(bp);
3110         u32 aeu_addr = port ? MISC_REG_AEU_MASK_ATTN_FUNC_1 :
3111                               MISC_REG_AEU_MASK_ATTN_FUNC_0;
3112         u32 nig_int_mask_addr = port ? NIG_REG_MASK_INTERRUPT_PORT1 :
3113                                        NIG_REG_MASK_INTERRUPT_PORT0;
3114         u32 aeu_mask;
3115         u32 nig_mask = 0;
3116         u32 reg_addr;
3117
3118         if (bp->attn_state & asserted)
3119                 BNX2X_ERR("IGU ERROR\n");
3120
3121         bnx2x_acquire_hw_lock(bp, HW_LOCK_RESOURCE_PORT0_ATT_MASK + port);
3122         aeu_mask = REG_RD(bp, aeu_addr);
3123
3124         DP(NETIF_MSG_HW, "aeu_mask %x  newly asserted %x\n",
3125            aeu_mask, asserted);
3126         aeu_mask &= ~(asserted & 0x3ff);
3127         DP(NETIF_MSG_HW, "new mask %x\n", aeu_mask);
3128
3129         REG_WR(bp, aeu_addr, aeu_mask);
3130         bnx2x_release_hw_lock(bp, HW_LOCK_RESOURCE_PORT0_ATT_MASK + port);
3131
3132         DP(NETIF_MSG_HW, "attn_state %x\n", bp->attn_state);
3133         bp->attn_state |= asserted;
3134         DP(NETIF_MSG_HW, "new state %x\n", bp->attn_state);
3135
3136         if (asserted & ATTN_HARD_WIRED_MASK) {
3137                 if (asserted & ATTN_NIG_FOR_FUNC) {
3138
3139                         bnx2x_acquire_phy_lock(bp);
3140
3141                         /* save nig interrupt mask */
3142                         nig_mask = REG_RD(bp, nig_int_mask_addr);
3143
3144                         /* If nig_mask is not set, no need to call the update
3145                          * function.
3146                          */
3147                         if (nig_mask) {
3148                                 REG_WR(bp, nig_int_mask_addr, 0);
3149
3150                                 bnx2x_link_attn(bp);
3151                         }
3152
3153                         /* handle unicore attn? */
3154                 }
3155                 if (asserted & ATTN_SW_TIMER_4_FUNC)
3156                         DP(NETIF_MSG_HW, "ATTN_SW_TIMER_4_FUNC!\n");
3157
3158                 if (asserted & GPIO_2_FUNC)
3159                         DP(NETIF_MSG_HW, "GPIO_2_FUNC!\n");
3160
3161                 if (asserted & GPIO_3_FUNC)
3162                         DP(NETIF_MSG_HW, "GPIO_3_FUNC!\n");
3163
3164                 if (asserted & GPIO_4_FUNC)
3165                         DP(NETIF_MSG_HW, "GPIO_4_FUNC!\n");
3166
3167                 if (port == 0) {
3168                         if (asserted & ATTN_GENERAL_ATTN_1) {
3169                                 DP(NETIF_MSG_HW, "ATTN_GENERAL_ATTN_1!\n");
3170                                 REG_WR(bp, MISC_REG_AEU_GENERAL_ATTN_1, 0x0);
3171                         }
3172                         if (asserted & ATTN_GENERAL_ATTN_2) {
3173                                 DP(NETIF_MSG_HW, "ATTN_GENERAL_ATTN_2!\n");
3174                                 REG_WR(bp, MISC_REG_AEU_GENERAL_ATTN_2, 0x0);
3175                         }
3176                         if (asserted & ATTN_GENERAL_ATTN_3) {
3177                                 DP(NETIF_MSG_HW, "ATTN_GENERAL_ATTN_3!\n");
3178                                 REG_WR(bp, MISC_REG_AEU_GENERAL_ATTN_3, 0x0);
3179                         }
3180                 } else {
3181                         if (asserted & ATTN_GENERAL_ATTN_4) {
3182                                 DP(NETIF_MSG_HW, "ATTN_GENERAL_ATTN_4!\n");
3183                                 REG_WR(bp, MISC_REG_AEU_GENERAL_ATTN_4, 0x0);
3184                         }
3185                         if (asserted & ATTN_GENERAL_ATTN_5) {
3186                                 DP(NETIF_MSG_HW, "ATTN_GENERAL_ATTN_5!\n");
3187                                 REG_WR(bp, MISC_REG_AEU_GENERAL_ATTN_5, 0x0);
3188                         }
3189                         if (asserted & ATTN_GENERAL_ATTN_6) {
3190                                 DP(NETIF_MSG_HW, "ATTN_GENERAL_ATTN_6!\n");
3191                                 REG_WR(bp, MISC_REG_AEU_GENERAL_ATTN_6, 0x0);
3192                         }
3193                 }
3194
3195         } /* if hardwired */
3196
3197         if (bp->common.int_block == INT_BLOCK_HC)
3198                 reg_addr = (HC_REG_COMMAND_REG + port*32 +
3199                             COMMAND_REG_ATTN_BITS_SET);
3200         else
3201                 reg_addr = (BAR_IGU_INTMEM + IGU_CMD_ATTN_BIT_SET_UPPER*8);
3202
3203         DP(NETIF_MSG_HW, "about to mask 0x%08x at %s addr 0x%x\n", asserted,
3204            (bp->common.int_block == INT_BLOCK_HC) ? "HC" : "IGU", reg_addr);
3205         REG_WR(bp, reg_addr, asserted);
3206
3207         /* now set back the mask */
3208         if (asserted & ATTN_NIG_FOR_FUNC) {
3209                 REG_WR(bp, nig_int_mask_addr, nig_mask);
3210                 bnx2x_release_phy_lock(bp);
3211         }
3212 }
3213
3214 static inline void bnx2x_fan_failure(struct bnx2x *bp)
3215 {
3216         int port = BP_PORT(bp);
3217         u32 ext_phy_config;
3218         /* mark the failure */
3219         ext_phy_config =
3220                 SHMEM_RD(bp,
3221                          dev_info.port_hw_config[port].external_phy_config);
3222
3223         ext_phy_config &= ~PORT_HW_CFG_XGXS_EXT_PHY_TYPE_MASK;
3224         ext_phy_config |= PORT_HW_CFG_XGXS_EXT_PHY_TYPE_FAILURE;
3225         SHMEM_WR(bp, dev_info.port_hw_config[port].external_phy_config,
3226                  ext_phy_config);
3227
3228         /* log the failure */
3229         netdev_err(bp->dev, "Fan Failure on Network Controller has caused"
3230                " the driver to shutdown the card to prevent permanent"
3231                " damage.  Please contact OEM Support for assistance\n");
3232 }
3233
3234 static inline void bnx2x_attn_int_deasserted0(struct bnx2x *bp, u32 attn)
3235 {
3236         int port = BP_PORT(bp);
3237         int reg_offset;
3238         u32 val;
3239
3240         reg_offset = (port ? MISC_REG_AEU_ENABLE1_FUNC_1_OUT_0 :
3241                              MISC_REG_AEU_ENABLE1_FUNC_0_OUT_0);
3242
3243         if (attn & AEU_INPUTS_ATTN_BITS_SPIO5) {
3244
3245                 val = REG_RD(bp, reg_offset);
3246                 val &= ~AEU_INPUTS_ATTN_BITS_SPIO5;
3247                 REG_WR(bp, reg_offset, val);
3248
3249                 BNX2X_ERR("SPIO5 hw attention\n");
3250
3251                 /* Fan failure attention */
3252                 bnx2x_hw_reset_phy(&bp->link_params);
3253                 bnx2x_fan_failure(bp);
3254         }
3255
3256         if ((attn & bp->link_vars.aeu_int_mask) && bp->port.pmf) {
3257                 bnx2x_acquire_phy_lock(bp);
3258                 bnx2x_handle_module_detect_int(&bp->link_params);
3259                 bnx2x_release_phy_lock(bp);
3260         }
3261
3262         if (attn & HW_INTERRUT_ASSERT_SET_0) {
3263
3264                 val = REG_RD(bp, reg_offset);
3265                 val &= ~(attn & HW_INTERRUT_ASSERT_SET_0);
3266                 REG_WR(bp, reg_offset, val);
3267
3268                 BNX2X_ERR("FATAL HW block attention set0 0x%x\n",
3269                           (u32)(attn & HW_INTERRUT_ASSERT_SET_0));
3270                 bnx2x_panic();
3271         }
3272 }
3273
3274 static inline void bnx2x_attn_int_deasserted1(struct bnx2x *bp, u32 attn)
3275 {
3276         u32 val;
3277
3278         if (attn & AEU_INPUTS_ATTN_BITS_DOORBELLQ_HW_INTERRUPT) {
3279
3280                 val = REG_RD(bp, DORQ_REG_DORQ_INT_STS_CLR);
3281                 BNX2X_ERR("DB hw attention 0x%x\n", val);
3282                 /* DORQ discard attention */
3283                 if (val & 0x2)
3284                         BNX2X_ERR("FATAL error from DORQ\n");
3285         }
3286
3287         if (attn & HW_INTERRUT_ASSERT_SET_1) {
3288
3289                 int port = BP_PORT(bp);
3290                 int reg_offset;
3291
3292                 reg_offset = (port ? MISC_REG_AEU_ENABLE1_FUNC_1_OUT_1 :
3293                                      MISC_REG_AEU_ENABLE1_FUNC_0_OUT_1);
3294
3295                 val = REG_RD(bp, reg_offset);
3296                 val &= ~(attn & HW_INTERRUT_ASSERT_SET_1);
3297                 REG_WR(bp, reg_offset, val);
3298
3299                 BNX2X_ERR("FATAL HW block attention set1 0x%x\n",
3300                           (u32)(attn & HW_INTERRUT_ASSERT_SET_1));
3301                 bnx2x_panic();
3302         }
3303 }
3304
3305 static inline void bnx2x_attn_int_deasserted2(struct bnx2x *bp, u32 attn)
3306 {
3307         u32 val;
3308
3309         if (attn & AEU_INPUTS_ATTN_BITS_CFC_HW_INTERRUPT) {
3310
3311                 val = REG_RD(bp, CFC_REG_CFC_INT_STS_CLR);
3312                 BNX2X_ERR("CFC hw attention 0x%x\n", val);
3313                 /* CFC error attention */
3314                 if (val & 0x2)
3315                         BNX2X_ERR("FATAL error from CFC\n");
3316         }
3317
3318         if (attn & AEU_INPUTS_ATTN_BITS_PXP_HW_INTERRUPT) {
3319                 val = REG_RD(bp, PXP_REG_PXP_INT_STS_CLR_0);
3320                 BNX2X_ERR("PXP hw attention-0 0x%x\n", val);
3321                 /* RQ_USDMDP_FIFO_OVERFLOW */
3322                 if (val & 0x18000)
3323                         BNX2X_ERR("FATAL error from PXP\n");
3324
3325                 if (!CHIP_IS_E1x(bp)) {
3326                         val = REG_RD(bp, PXP_REG_PXP_INT_STS_CLR_1);
3327                         BNX2X_ERR("PXP hw attention-1 0x%x\n", val);
3328                 }
3329         }
3330
3331         if (attn & HW_INTERRUT_ASSERT_SET_2) {
3332
3333                 int port = BP_PORT(bp);
3334                 int reg_offset;
3335
3336                 reg_offset = (port ? MISC_REG_AEU_ENABLE1_FUNC_1_OUT_2 :
3337                                      MISC_REG_AEU_ENABLE1_FUNC_0_OUT_2);
3338
3339                 val = REG_RD(bp, reg_offset);
3340                 val &= ~(attn & HW_INTERRUT_ASSERT_SET_2);
3341                 REG_WR(bp, reg_offset, val);
3342
3343                 BNX2X_ERR("FATAL HW block attention set2 0x%x\n",
3344                           (u32)(attn & HW_INTERRUT_ASSERT_SET_2));
3345                 bnx2x_panic();
3346         }
3347 }
3348
3349 static inline void bnx2x_attn_int_deasserted3(struct bnx2x *bp, u32 attn)
3350 {
3351         u32 val;
3352
3353         if (attn & EVEREST_GEN_ATTN_IN_USE_MASK) {
3354
3355                 if (attn & BNX2X_PMF_LINK_ASSERT) {
3356                         int func = BP_FUNC(bp);
3357
3358                         REG_WR(bp, MISC_REG_AEU_GENERAL_ATTN_12 + func*4, 0);
3359                         bp->mf_config[BP_VN(bp)] = MF_CFG_RD(bp,
3360                                         func_mf_config[BP_ABS_FUNC(bp)].config);
3361                         val = SHMEM_RD(bp,
3362                                        func_mb[BP_FW_MB_IDX(bp)].drv_status);
3363                         if (val & DRV_STATUS_DCC_EVENT_MASK)
3364                                 bnx2x_dcc_event(bp,
3365                                             (val & DRV_STATUS_DCC_EVENT_MASK));
3366
3367                         if (val & DRV_STATUS_SET_MF_BW)
3368                                 bnx2x_set_mf_bw(bp);
3369
3370                         if ((bp->port.pmf == 0) && (val & DRV_STATUS_PMF))
3371                                 bnx2x_pmf_update(bp);
3372
3373                         if (bp->port.pmf &&
3374                             (val & DRV_STATUS_DCBX_NEGOTIATION_RESULTS) &&
3375                                 bp->dcbx_enabled > 0)
3376                                 /* start dcbx state machine */
3377                                 bnx2x_dcbx_set_params(bp,
3378                                         BNX2X_DCBX_STATE_NEG_RECEIVED);
3379                         if (bp->link_vars.periodic_flags &
3380                             PERIODIC_FLAGS_LINK_EVENT) {
3381                                 /*  sync with link */
3382                                 bnx2x_acquire_phy_lock(bp);
3383                                 bp->link_vars.periodic_flags &=
3384                                         ~PERIODIC_FLAGS_LINK_EVENT;
3385                                 bnx2x_release_phy_lock(bp);
3386                                 if (IS_MF(bp))
3387                                         bnx2x_link_sync_notify(bp);
3388                                 bnx2x_link_report(bp);
3389                         }
3390                         /* Always call it here: bnx2x_link_report() will
3391                          * prevent the link indication duplication.
3392                          */
3393                         bnx2x__link_status_update(bp);
3394                 } else if (attn & BNX2X_MC_ASSERT_BITS) {
3395
3396                         BNX2X_ERR("MC assert!\n");
3397                         REG_WR(bp, MISC_REG_AEU_GENERAL_ATTN_10, 0);
3398                         REG_WR(bp, MISC_REG_AEU_GENERAL_ATTN_9, 0);
3399                         REG_WR(bp, MISC_REG_AEU_GENERAL_ATTN_8, 0);
3400                         REG_WR(bp, MISC_REG_AEU_GENERAL_ATTN_7, 0);
3401                         bnx2x_panic();
3402
3403                 } else if (attn & BNX2X_MCP_ASSERT) {
3404
3405                         BNX2X_ERR("MCP assert!\n");
3406                         REG_WR(bp, MISC_REG_AEU_GENERAL_ATTN_11, 0);
3407                         bnx2x_fw_dump(bp);
3408
3409                 } else
3410                         BNX2X_ERR("Unknown HW assert! (attn 0x%x)\n", attn);
3411         }
3412
3413         if (attn & EVEREST_LATCHED_ATTN_IN_USE_MASK) {
3414                 BNX2X_ERR("LATCHED attention 0x%08x (masked)\n", attn);
3415                 if (attn & BNX2X_GRC_TIMEOUT) {
3416                         val = CHIP_IS_E1(bp) ? 0 :
3417                                         REG_RD(bp, MISC_REG_GRC_TIMEOUT_ATTN);
3418                         BNX2X_ERR("GRC time-out 0x%08x\n", val);
3419                 }
3420                 if (attn & BNX2X_GRC_RSV) {
3421                         val = CHIP_IS_E1(bp) ? 0 :
3422                                         REG_RD(bp, MISC_REG_GRC_RSV_ATTN);
3423                         BNX2X_ERR("GRC reserved 0x%08x\n", val);
3424                 }
3425                 REG_WR(bp, MISC_REG_AEU_CLR_LATCH_SIGNAL, 0x7ff);
3426         }
3427 }
3428
3429 /*
3430  * Bits map:
3431  * 0-7   - Engine0 load counter.
3432  * 8-15  - Engine1 load counter.
3433  * 16    - Engine0 RESET_IN_PROGRESS bit.
3434  * 17    - Engine1 RESET_IN_PROGRESS bit.
3435  * 18    - Engine0 ONE_IS_LOADED. Set when there is at least one active function
3436  *         on the engine
3437  * 19    - Engine1 ONE_IS_LOADED.
3438  * 20    - Chip reset flow bit. When set none-leader must wait for both engines
3439  *         leader to complete (check for both RESET_IN_PROGRESS bits and not for
3440  *         just the one belonging to its engine).
3441  *
3442  */
3443 #define BNX2X_RECOVERY_GLOB_REG         MISC_REG_GENERIC_POR_1
3444
3445 #define BNX2X_PATH0_LOAD_CNT_MASK       0x000000ff
3446 #define BNX2X_PATH0_LOAD_CNT_SHIFT      0
3447 #define BNX2X_PATH1_LOAD_CNT_MASK       0x0000ff00
3448 #define BNX2X_PATH1_LOAD_CNT_SHIFT      8
3449 #define BNX2X_PATH0_RST_IN_PROG_BIT     0x00010000
3450 #define BNX2X_PATH1_RST_IN_PROG_BIT     0x00020000
3451 #define BNX2X_GLOBAL_RESET_BIT          0x00040000
3452
3453 /*
3454  * Set the GLOBAL_RESET bit.
3455  *
3456  * Should be run under rtnl lock
3457  */
3458 void bnx2x_set_reset_global(struct bnx2x *bp)
3459 {
3460         u32 val = REG_RD(bp, BNX2X_RECOVERY_GLOB_REG);
3461
3462         REG_WR(bp, BNX2X_RECOVERY_GLOB_REG, val | BNX2X_GLOBAL_RESET_BIT);
3463         barrier();
3464         mmiowb();
3465 }
3466
3467 /*
3468  * Clear the GLOBAL_RESET bit.
3469  *
3470  * Should be run under rtnl lock
3471  */
3472 static inline void bnx2x_clear_reset_global(struct bnx2x *bp)
3473 {
3474         u32 val = REG_RD(bp, BNX2X_RECOVERY_GLOB_REG);
3475
3476         REG_WR(bp, BNX2X_RECOVERY_GLOB_REG, val & (~BNX2X_GLOBAL_RESET_BIT));
3477         barrier();
3478         mmiowb();
3479 }
3480
3481 /*
3482  * Checks the GLOBAL_RESET bit.
3483  *
3484  * should be run under rtnl lock
3485  */
3486 static inline bool bnx2x_reset_is_global(struct bnx2x *bp)
3487 {
3488         u32 val = REG_RD(bp, BNX2X_RECOVERY_GLOB_REG);
3489
3490         DP(NETIF_MSG_HW, "GEN_REG_VAL=0x%08x\n", val);
3491         return (val & BNX2X_GLOBAL_RESET_BIT) ? true : false;
3492 }
3493
3494 /*
3495  * Clear RESET_IN_PROGRESS bit for the current engine.
3496  *
3497  * Should be run under rtnl lock
3498  */
3499 static inline void bnx2x_set_reset_done(struct bnx2x *bp)
3500 {
3501         u32 val = REG_RD(bp, BNX2X_RECOVERY_GLOB_REG);
3502         u32 bit = BP_PATH(bp) ?
3503                 BNX2X_PATH1_RST_IN_PROG_BIT : BNX2X_PATH0_RST_IN_PROG_BIT;
3504
3505         /* Clear the bit */
3506         val &= ~bit;
3507         REG_WR(bp, BNX2X_RECOVERY_GLOB_REG, val);
3508         barrier();
3509         mmiowb();
3510 }
3511
3512 /*
3513  * Set RESET_IN_PROGRESS for the current engine.
3514  *
3515  * should be run under rtnl lock
3516  */
3517 void bnx2x_set_reset_in_progress(struct bnx2x *bp)
3518 {
3519         u32 val = REG_RD(bp, BNX2X_RECOVERY_GLOB_REG);
3520         u32 bit = BP_PATH(bp) ?
3521                 BNX2X_PATH1_RST_IN_PROG_BIT : BNX2X_PATH0_RST_IN_PROG_BIT;
3522
3523         /* Set the bit */
3524         val |= bit;
3525         REG_WR(bp, BNX2X_RECOVERY_GLOB_REG, val);
3526         barrier();
3527         mmiowb();
3528 }
3529
3530 /*
3531  * Checks the RESET_IN_PROGRESS bit for the given engine.
3532  * should be run under rtnl lock
3533  */
3534 bool bnx2x_reset_is_done(struct bnx2x *bp, int engine)
3535 {
3536         u32 val = REG_RD(bp, BNX2X_RECOVERY_GLOB_REG);
3537         u32 bit = engine ?
3538                 BNX2X_PATH1_RST_IN_PROG_BIT : BNX2X_PATH0_RST_IN_PROG_BIT;
3539
3540         /* return false if bit is set */
3541         return (val & bit) ? false : true;
3542 }
3543
3544 /*
3545  * Increment the load counter for the current engine.
3546  *
3547  * should be run under rtnl lock
3548  */
3549 void bnx2x_inc_load_cnt(struct bnx2x *bp)
3550 {
3551         u32 val1, val = REG_RD(bp, BNX2X_RECOVERY_GLOB_REG);
3552         u32 mask = BP_PATH(bp) ? BNX2X_PATH1_LOAD_CNT_MASK :
3553                              BNX2X_PATH0_LOAD_CNT_MASK;
3554         u32 shift = BP_PATH(bp) ? BNX2X_PATH1_LOAD_CNT_SHIFT :
3555                              BNX2X_PATH0_LOAD_CNT_SHIFT;
3556
3557         DP(NETIF_MSG_HW, "Old GEN_REG_VAL=0x%08x\n", val);
3558
3559         /* get the current counter value */
3560         val1 = (val & mask) >> shift;
3561
3562         /* increment... */
3563         val1++;
3564
3565         /* clear the old value */
3566         val &= ~mask;
3567
3568         /* set the new one */
3569         val |= ((val1 << shift) & mask);
3570
3571         REG_WR(bp, BNX2X_RECOVERY_GLOB_REG, val);
3572         barrier();
3573         mmiowb();
3574 }
3575
3576 /**
3577  * bnx2x_dec_load_cnt - decrement the load counter
3578  *
3579  * @bp:         driver handle
3580  *
3581  * Should be run under rtnl lock.
3582  * Decrements the load counter for the current engine. Returns
3583  * the new counter value.
3584  */
3585 u32 bnx2x_dec_load_cnt(struct bnx2x *bp)
3586 {
3587         u32 val1, val = REG_RD(bp, BNX2X_RECOVERY_GLOB_REG);
3588         u32 mask = BP_PATH(bp) ? BNX2X_PATH1_LOAD_CNT_MASK :
3589                              BNX2X_PATH0_LOAD_CNT_MASK;
3590         u32 shift = BP_PATH(bp) ? BNX2X_PATH1_LOAD_CNT_SHIFT :
3591                              BNX2X_PATH0_LOAD_CNT_SHIFT;
3592
3593         DP(NETIF_MSG_HW, "Old GEN_REG_VAL=0x%08x\n", val);
3594
3595         /* get the current counter value */
3596         val1 = (val & mask) >> shift;
3597
3598         /* decrement... */
3599         val1--;
3600
3601         /* clear the old value */
3602         val &= ~mask;
3603
3604         /* set the new one */
3605         val |= ((val1 << shift) & mask);
3606
3607         REG_WR(bp, BNX2X_RECOVERY_GLOB_REG, val);
3608         barrier();
3609         mmiowb();
3610
3611         return val1;
3612 }
3613
3614 /*
3615  * Read the load counter for the current engine.
3616  *
3617  * should be run under rtnl lock
3618  */
3619 static inline u32 bnx2x_get_load_cnt(struct bnx2x *bp, int engine)
3620 {
3621         u32 mask = (engine ? BNX2X_PATH1_LOAD_CNT_MASK :
3622                              BNX2X_PATH0_LOAD_CNT_MASK);
3623         u32 shift = (engine ? BNX2X_PATH1_LOAD_CNT_SHIFT :
3624                              BNX2X_PATH0_LOAD_CNT_SHIFT);
3625         u32 val = REG_RD(bp, BNX2X_RECOVERY_GLOB_REG);
3626
3627         DP(NETIF_MSG_HW, "GLOB_REG=0x%08x\n", val);
3628
3629         val = (val & mask) >> shift;
3630
3631         DP(NETIF_MSG_HW, "load_cnt for engine %d = %d\n", engine, val);
3632
3633         return val;
3634 }
3635
3636 /*
3637  * Reset the load counter for the current engine.
3638  *
3639  * should be run under rtnl lock
3640  */
3641 static inline void bnx2x_clear_load_cnt(struct bnx2x *bp)
3642 {
3643         u32 val = REG_RD(bp, BNX2X_RECOVERY_GLOB_REG);
3644         u32 mask = (BP_PATH(bp) ? BNX2X_PATH1_LOAD_CNT_MASK :
3645                              BNX2X_PATH0_LOAD_CNT_MASK);
3646
3647         REG_WR(bp, BNX2X_RECOVERY_GLOB_REG, val & (~mask));
3648 }
3649
3650 static inline void _print_next_block(int idx, const char *blk)
3651 {
3652         if (idx)
3653                 pr_cont(", ");
3654         pr_cont("%s", blk);
3655 }
3656
3657 static inline int bnx2x_check_blocks_with_parity0(u32 sig, int par_num,
3658                                                   bool print)
3659 {
3660         int i = 0;
3661         u32 cur_bit = 0;
3662         for (i = 0; sig; i++) {
3663                 cur_bit = ((u32)0x1 << i);
3664                 if (sig & cur_bit) {
3665                         switch (cur_bit) {
3666                         case AEU_INPUTS_ATTN_BITS_BRB_PARITY_ERROR:
3667                                 if (print)
3668                                         _print_next_block(par_num++, "BRB");
3669                                 break;
3670                         case AEU_INPUTS_ATTN_BITS_PARSER_PARITY_ERROR:
3671                                 if (print)
3672                                         _print_next_block(par_num++, "PARSER");
3673                                 break;
3674                         case AEU_INPUTS_ATTN_BITS_TSDM_PARITY_ERROR:
3675                                 if (print)
3676                                         _print_next_block(par_num++, "TSDM");
3677                                 break;
3678                         case AEU_INPUTS_ATTN_BITS_SEARCHER_PARITY_ERROR:
3679                                 if (print)
3680                                         _print_next_block(par_num++,
3681                                                           "SEARCHER");
3682                                 break;
3683                         case AEU_INPUTS_ATTN_BITS_TCM_PARITY_ERROR:
3684                                 if (print)
3685                                         _print_next_block(par_num++, "TCM");
3686                                 break;
3687                         case AEU_INPUTS_ATTN_BITS_TSEMI_PARITY_ERROR:
3688                                 if (print)
3689                                         _print_next_block(par_num++, "TSEMI");
3690                                 break;
3691                         case AEU_INPUTS_ATTN_BITS_PBCLIENT_PARITY_ERROR:
3692                                 if (print)
3693                                         _print_next_block(par_num++, "XPB");
3694                                 break;
3695                         }
3696
3697                         /* Clear the bit */
3698                         sig &= ~cur_bit;
3699                 }
3700         }
3701
3702         return par_num;
3703 }
3704
3705 static inline int bnx2x_check_blocks_with_parity1(u32 sig, int par_num,
3706                                                   bool *global, bool print)
3707 {
3708         int i = 0;
3709         u32 cur_bit = 0;
3710         for (i = 0; sig; i++) {
3711                 cur_bit = ((u32)0x1 << i);
3712                 if (sig & cur_bit) {
3713                         switch (cur_bit) {
3714                         case AEU_INPUTS_ATTN_BITS_PBF_PARITY_ERROR:
3715                                 if (print)
3716                                         _print_next_block(par_num++, "PBF");
3717                                 break;
3718                         case AEU_INPUTS_ATTN_BITS_QM_PARITY_ERROR:
3719                                 if (print)
3720                                         _print_next_block(par_num++, "QM");
3721                                 break;
3722                         case AEU_INPUTS_ATTN_BITS_TIMERS_PARITY_ERROR:
3723                                 if (print)
3724                                         _print_next_block(par_num++, "TM");
3725                                 break;
3726                         case AEU_INPUTS_ATTN_BITS_XSDM_PARITY_ERROR:
3727                                 if (print)
3728                                         _print_next_block(par_num++, "XSDM");
3729                                 break;
3730                         case AEU_INPUTS_ATTN_BITS_XCM_PARITY_ERROR:
3731                                 if (print)
3732                                         _print_next_block(par_num++, "XCM");
3733                                 break;
3734                         case AEU_INPUTS_ATTN_BITS_XSEMI_PARITY_ERROR:
3735                                 if (print)
3736                                         _print_next_block(par_num++, "XSEMI");
3737                                 break;
3738                         case AEU_INPUTS_ATTN_BITS_DOORBELLQ_PARITY_ERROR:
3739                                 if (print)
3740                                         _print_next_block(par_num++,
3741                                                           "DOORBELLQ");
3742                                 break;
3743                         case AEU_INPUTS_ATTN_BITS_NIG_PARITY_ERROR:
3744                                 if (print)
3745                                         _print_next_block(par_num++, "NIG");
3746                                 break;
3747                         case AEU_INPUTS_ATTN_BITS_VAUX_PCI_CORE_PARITY_ERROR:
3748                                 if (print)
3749                                         _print_next_block(par_num++,
3750                                                           "VAUX PCI CORE");
3751                                 *global = true;
3752                                 break;
3753                         case AEU_INPUTS_ATTN_BITS_DEBUG_PARITY_ERROR:
3754                                 if (print)
3755                                         _print_next_block(par_num++, "DEBUG");
3756                                 break;
3757                         case AEU_INPUTS_ATTN_BITS_USDM_PARITY_ERROR:
3758                                 if (print)
3759                                         _print_next_block(par_num++, "USDM");
3760                                 break;
3761                         case AEU_INPUTS_ATTN_BITS_USEMI_PARITY_ERROR:
3762                                 if (print)
3763                                         _print_next_block(par_num++, "USEMI");
3764                                 break;
3765                         case AEU_INPUTS_ATTN_BITS_UPB_PARITY_ERROR:
3766                                 if (print)
3767                                         _print_next_block(par_num++, "UPB");
3768                                 break;
3769                         case AEU_INPUTS_ATTN_BITS_CSDM_PARITY_ERROR:
3770                                 if (print)
3771                                         _print_next_block(par_num++, "CSDM");
3772                                 break;
3773                         }
3774
3775                         /* Clear the bit */
3776                         sig &= ~cur_bit;
3777                 }
3778         }
3779
3780         return par_num;
3781 }
3782
3783 static inline int bnx2x_check_blocks_with_parity2(u32 sig, int par_num,
3784                                                   bool print)
3785 {
3786         int i = 0;
3787         u32 cur_bit = 0;
3788         for (i = 0; sig; i++) {
3789                 cur_bit = ((u32)0x1 << i);
3790                 if (sig & cur_bit) {
3791                         switch (cur_bit) {
3792                         case AEU_INPUTS_ATTN_BITS_CSEMI_PARITY_ERROR:
3793                                 if (print)
3794                                         _print_next_block(par_num++, "CSEMI");
3795                                 break;
3796                         case AEU_INPUTS_ATTN_BITS_PXP_PARITY_ERROR:
3797                                 if (print)
3798                                         _print_next_block(par_num++, "PXP");
3799                                 break;
3800                         case AEU_IN_ATTN_BITS_PXPPCICLOCKCLIENT_PARITY_ERROR:
3801                                 if (print)
3802                                         _print_next_block(par_num++,
3803                                         "PXPPCICLOCKCLIENT");
3804                                 break;
3805                         case AEU_INPUTS_ATTN_BITS_CFC_PARITY_ERROR:
3806                                 if (print)
3807                                         _print_next_block(par_num++, "CFC");
3808                                 break;
3809                         case AEU_INPUTS_ATTN_BITS_CDU_PARITY_ERROR:
3810                                 if (print)
3811                                         _print_next_block(par_num++, "CDU");
3812                                 break;
3813                         case AEU_INPUTS_ATTN_BITS_DMAE_PARITY_ERROR:
3814                                 if (print)
3815                                         _print_next_block(par_num++, "DMAE");
3816                                 break;
3817                         case AEU_INPUTS_ATTN_BITS_IGU_PARITY_ERROR:
3818                                 if (print)
3819                                         _print_next_block(par_num++, "IGU");
3820                                 break;
3821                         case AEU_INPUTS_ATTN_BITS_MISC_PARITY_ERROR:
3822                                 if (print)
3823                                         _print_next_block(par_num++, "MISC");
3824                                 break;
3825                         }
3826
3827                         /* Clear the bit */
3828                         sig &= ~cur_bit;
3829                 }
3830         }
3831
3832         return par_num;
3833 }
3834
3835 static inline int bnx2x_check_blocks_with_parity3(u32 sig, int par_num,
3836                                                   bool *global, bool print)
3837 {
3838         int i = 0;
3839         u32 cur_bit = 0;
3840         for (i = 0; sig; i++) {
3841                 cur_bit = ((u32)0x1 << i);
3842                 if (sig & cur_bit) {
3843                         switch (cur_bit) {
3844                         case AEU_INPUTS_ATTN_BITS_MCP_LATCHED_ROM_PARITY:
3845                                 if (print)
3846                                         _print_next_block(par_num++, "MCP ROM");
3847                                 *global = true;
3848                                 break;
3849                         case AEU_INPUTS_ATTN_BITS_MCP_LATCHED_UMP_RX_PARITY:
3850                                 if (print)
3851                                         _print_next_block(par_num++,
3852                                                           "MCP UMP RX");
3853                                 *global = true;
3854                                 break;
3855                         case AEU_INPUTS_ATTN_BITS_MCP_LATCHED_UMP_TX_PARITY:
3856                                 if (print)
3857                                         _print_next_block(par_num++,
3858                                                           "MCP UMP TX");
3859                                 *global = true;
3860                                 break;
3861                         case AEU_INPUTS_ATTN_BITS_MCP_LATCHED_SCPAD_PARITY:
3862                                 if (print)
3863                                         _print_next_block(par_num++,
3864                                                           "MCP SCPAD");
3865                                 *global = true;
3866                                 break;
3867                         }
3868
3869                         /* Clear the bit */
3870                         sig &= ~cur_bit;
3871                 }
3872         }
3873
3874         return par_num;
3875 }
3876
3877 static inline bool bnx2x_parity_attn(struct bnx2x *bp, bool *global, bool print,
3878                                      u32 sig0, u32 sig1, u32 sig2, u32 sig3)
3879 {
3880         if ((sig0 & HW_PRTY_ASSERT_SET_0) || (sig1 & HW_PRTY_ASSERT_SET_1) ||
3881             (sig2 & HW_PRTY_ASSERT_SET_2) || (sig3 & HW_PRTY_ASSERT_SET_3)) {
3882                 int par_num = 0;
3883                 DP(NETIF_MSG_HW, "Was parity error: HW block parity attention: "
3884                         "[0]:0x%08x [1]:0x%08x "
3885                         "[2]:0x%08x [3]:0x%08x\n",
3886                           sig0 & HW_PRTY_ASSERT_SET_0,
3887                           sig1 & HW_PRTY_ASSERT_SET_1,
3888                           sig2 & HW_PRTY_ASSERT_SET_2,
3889                           sig3 & HW_PRTY_ASSERT_SET_3);
3890                 if (print)
3891                         netdev_err(bp->dev,
3892                                    "Parity errors detected in blocks: ");
3893                 par_num = bnx2x_check_blocks_with_parity0(
3894                         sig0 & HW_PRTY_ASSERT_SET_0, par_num, print);
3895                 par_num = bnx2x_check_blocks_with_parity1(
3896                         sig1 & HW_PRTY_ASSERT_SET_1, par_num, global, print);
3897                 par_num = bnx2x_check_blocks_with_parity2(
3898                         sig2 & HW_PRTY_ASSERT_SET_2, par_num, print);
3899                 par_num = bnx2x_check_blocks_with_parity3(
3900                         sig3 & HW_PRTY_ASSERT_SET_3, par_num, global, print);
3901                 if (print)
3902                         pr_cont("\n");
3903                 return true;
3904         } else
3905                 return false;
3906 }
3907
3908 /**
3909  * bnx2x_chk_parity_attn - checks for parity attentions.
3910  *
3911  * @bp:         driver handle
3912  * @global:     true if there was a global attention
3913  * @print:      show parity attention in syslog
3914  */
3915 bool bnx2x_chk_parity_attn(struct bnx2x *bp, bool *global, bool print)
3916 {
3917         struct attn_route attn;
3918         int port = BP_PORT(bp);
3919
3920         attn.sig[0] = REG_RD(bp,
3921                 MISC_REG_AEU_AFTER_INVERT_1_FUNC_0 +
3922                              port*4);
3923         attn.sig[1] = REG_RD(bp,
3924                 MISC_REG_AEU_AFTER_INVERT_2_FUNC_0 +
3925                              port*4);
3926         attn.sig[2] = REG_RD(bp,
3927                 MISC_REG_AEU_AFTER_INVERT_3_FUNC_0 +
3928                              port*4);
3929         attn.sig[3] = REG_RD(bp,
3930                 MISC_REG_AEU_AFTER_INVERT_4_FUNC_0 +
3931                              port*4);
3932
3933         return bnx2x_parity_attn(bp, global, print, attn.sig[0], attn.sig[1],
3934                                  attn.sig[2], attn.sig[3]);
3935 }
3936
3937
3938 static inline void bnx2x_attn_int_deasserted4(struct bnx2x *bp, u32 attn)
3939 {
3940         u32 val;
3941         if (attn & AEU_INPUTS_ATTN_BITS_PGLUE_HW_INTERRUPT) {
3942
3943                 val = REG_RD(bp, PGLUE_B_REG_PGLUE_B_INT_STS_CLR);
3944                 BNX2X_ERR("PGLUE hw attention 0x%x\n", val);
3945                 if (val & PGLUE_B_PGLUE_B_INT_STS_REG_ADDRESS_ERROR)
3946                         BNX2X_ERR("PGLUE_B_PGLUE_B_INT_STS_REG_"
3947                                   "ADDRESS_ERROR\n");
3948                 if (val & PGLUE_B_PGLUE_B_INT_STS_REG_INCORRECT_RCV_BEHAVIOR)
3949                         BNX2X_ERR("PGLUE_B_PGLUE_B_INT_STS_REG_"
3950                                   "INCORRECT_RCV_BEHAVIOR\n");
3951                 if (val & PGLUE_B_PGLUE_B_INT_STS_REG_WAS_ERROR_ATTN)
3952                         BNX2X_ERR("PGLUE_B_PGLUE_B_INT_STS_REG_"
3953                                   "WAS_ERROR_ATTN\n");
3954                 if (val & PGLUE_B_PGLUE_B_INT_STS_REG_VF_LENGTH_VIOLATION_ATTN)
3955                         BNX2X_ERR("PGLUE_B_PGLUE_B_INT_STS_REG_"
3956                                   "VF_LENGTH_VIOLATION_ATTN\n");
3957                 if (val &
3958                     PGLUE_B_PGLUE_B_INT_STS_REG_VF_GRC_SPACE_VIOLATION_ATTN)
3959                         BNX2X_ERR("PGLUE_B_PGLUE_B_INT_STS_REG_"
3960                                   "VF_GRC_SPACE_VIOLATION_ATTN\n");
3961                 if (val &
3962                     PGLUE_B_PGLUE_B_INT_STS_REG_VF_MSIX_BAR_VIOLATION_ATTN)
3963                         BNX2X_ERR("PGLUE_B_PGLUE_B_INT_STS_REG_"
3964                                   "VF_MSIX_BAR_VIOLATION_ATTN\n");
3965                 if (val & PGLUE_B_PGLUE_B_INT_STS_REG_TCPL_ERROR_ATTN)
3966                         BNX2X_ERR("PGLUE_B_PGLUE_B_INT_STS_REG_"
3967                                   "TCPL_ERROR_ATTN\n");
3968                 if (val & PGLUE_B_PGLUE_B_INT_STS_REG_TCPL_IN_TWO_RCBS_ATTN)
3969                         BNX2X_ERR("PGLUE_B_PGLUE_B_INT_STS_REG_"
3970                                   "TCPL_IN_TWO_RCBS_ATTN\n");
3971                 if (val & PGLUE_B_PGLUE_B_INT_STS_REG_CSSNOOP_FIFO_OVERFLOW)
3972                         BNX2X_ERR("PGLUE_B_PGLUE_B_INT_STS_REG_"
3973                                   "CSSNOOP_FIFO_OVERFLOW\n");
3974         }
3975         if (attn & AEU_INPUTS_ATTN_BITS_ATC_HW_INTERRUPT) {
3976                 val = REG_RD(bp, ATC_REG_ATC_INT_STS_CLR);
3977                 BNX2X_ERR("ATC hw attention 0x%x\n", val);
3978                 if (val & ATC_ATC_INT_STS_REG_ADDRESS_ERROR)
3979                         BNX2X_ERR("ATC_ATC_INT_STS_REG_ADDRESS_ERROR\n");
3980                 if (val & ATC_ATC_INT_STS_REG_ATC_TCPL_TO_NOT_PEND)
3981                         BNX2X_ERR("ATC_ATC_INT_STS_REG"
3982                                   "_ATC_TCPL_TO_NOT_PEND\n");
3983                 if (val & ATC_ATC_INT_STS_REG_ATC_GPA_MULTIPLE_HITS)
3984                         BNX2X_ERR("ATC_ATC_INT_STS_REG_"
3985                                   "ATC_GPA_MULTIPLE_HITS\n");
3986                 if (val & ATC_ATC_INT_STS_REG_ATC_RCPL_TO_EMPTY_CNT)
3987                         BNX2X_ERR("ATC_ATC_INT_STS_REG_"
3988                                   "ATC_RCPL_TO_EMPTY_CNT\n");
3989                 if (val & ATC_ATC_INT_STS_REG_ATC_TCPL_ERROR)
3990                         BNX2X_ERR("ATC_ATC_INT_STS_REG_ATC_TCPL_ERROR\n");
3991                 if (val & ATC_ATC_INT_STS_REG_ATC_IREQ_LESS_THAN_STU)
3992                         BNX2X_ERR("ATC_ATC_INT_STS_REG_"
3993                                   "ATC_IREQ_LESS_THAN_STU\n");
3994         }
3995
3996         if (attn & (AEU_INPUTS_ATTN_BITS_PGLUE_PARITY_ERROR |
3997                     AEU_INPUTS_ATTN_BITS_ATC_PARITY_ERROR)) {
3998                 BNX2X_ERR("FATAL parity attention set4 0x%x\n",
3999                 (u32)(attn & (AEU_INPUTS_ATTN_BITS_PGLUE_PARITY_ERROR |
4000                     AEU_INPUTS_ATTN_BITS_ATC_PARITY_ERROR)));
4001         }
4002
4003 }
4004
4005 static void bnx2x_attn_int_deasserted(struct bnx2x *bp, u32 deasserted)
4006 {
4007         struct attn_route attn, *group_mask;
4008         int port = BP_PORT(bp);
4009         int index;
4010         u32 reg_addr;
4011         u32 val;
4012         u32 aeu_mask;
4013         bool global = false;
4014
4015         /* need to take HW lock because MCP or other port might also
4016            try to handle this event */
4017         bnx2x_acquire_alr(bp);
4018
4019         if (bnx2x_chk_parity_attn(bp, &global, true)) {
4020 #ifndef BNX2X_STOP_ON_ERROR
4021                 bp->recovery_state = BNX2X_RECOVERY_INIT;
4022                 schedule_delayed_work(&bp->reset_task, 0);
4023                 /* Disable HW interrupts */
4024                 bnx2x_int_disable(bp);
4025                 /* In case of parity errors don't handle attentions so that
4026                  * other function would "see" parity errors.
4027                  */
4028 #else
4029                 bnx2x_panic();
4030 #endif
4031                 bnx2x_release_alr(bp);
4032                 return;
4033         }
4034
4035         attn.sig[0] = REG_RD(bp, MISC_REG_AEU_AFTER_INVERT_1_FUNC_0 + port*4);
4036         attn.sig[1] = REG_RD(bp, MISC_REG_AEU_AFTER_INVERT_2_FUNC_0 + port*4);
4037         attn.sig[2] = REG_RD(bp, MISC_REG_AEU_AFTER_INVERT_3_FUNC_0 + port*4);
4038         attn.sig[3] = REG_RD(bp, MISC_REG_AEU_AFTER_INVERT_4_FUNC_0 + port*4);
4039         if (!CHIP_IS_E1x(bp))
4040                 attn.sig[4] =
4041                       REG_RD(bp, MISC_REG_AEU_AFTER_INVERT_5_FUNC_0 + port*4);
4042         else
4043                 attn.sig[4] = 0;
4044
4045         DP(NETIF_MSG_HW, "attn: %08x %08x %08x %08x %08x\n",
4046            attn.sig[0], attn.sig[1], attn.sig[2], attn.sig[3], attn.sig[4]);
4047
4048         for (index = 0; index < MAX_DYNAMIC_ATTN_GRPS; index++) {
4049                 if (deasserted & (1 << index)) {
4050                         group_mask = &bp->attn_group[index];
4051
4052                         DP(NETIF_MSG_HW, "group[%d]: %08x %08x "
4053                                          "%08x %08x %08x\n",
4054                            index,
4055                            group_mask->sig[0], group_mask->sig[1],
4056                            group_mask->sig[2], group_mask->sig[3],
4057                            group_mask->sig[4]);
4058
4059                         bnx2x_attn_int_deasserted4(bp,
4060                                         attn.sig[4] & group_mask->sig[4]);
4061                         bnx2x_attn_int_deasserted3(bp,
4062                                         attn.sig[3] & group_mask->sig[3]);
4063                         bnx2x_attn_int_deasserted1(bp,
4064                                         attn.sig[1] & group_mask->sig[1]);
4065                         bnx2x_attn_int_deasserted2(bp,
4066                                         attn.sig[2] & group_mask->sig[2]);
4067                         bnx2x_attn_int_deasserted0(bp,
4068                                         attn.sig[0] & group_mask->sig[0]);
4069                 }
4070         }
4071
4072         bnx2x_release_alr(bp);
4073
4074         if (bp->common.int_block == INT_BLOCK_HC)
4075                 reg_addr = (HC_REG_COMMAND_REG + port*32 +
4076                             COMMAND_REG_ATTN_BITS_CLR);
4077         else
4078                 reg_addr = (BAR_IGU_INTMEM + IGU_CMD_ATTN_BIT_CLR_UPPER*8);
4079
4080         val = ~deasserted;
4081         DP(NETIF_MSG_HW, "about to mask 0x%08x at %s addr 0x%x\n", val,
4082            (bp->common.int_block == INT_BLOCK_HC) ? "HC" : "IGU", reg_addr);
4083         REG_WR(bp, reg_addr, val);
4084
4085         if (~bp->attn_state & deasserted)
4086                 BNX2X_ERR("IGU ERROR\n");
4087
4088         reg_addr = port ? MISC_REG_AEU_MASK_ATTN_FUNC_1 :
4089                           MISC_REG_AEU_MASK_ATTN_FUNC_0;
4090
4091         bnx2x_acquire_hw_lock(bp, HW_LOCK_RESOURCE_PORT0_ATT_MASK + port);
4092         aeu_mask = REG_RD(bp, reg_addr);
4093
4094         DP(NETIF_MSG_HW, "aeu_mask %x  newly deasserted %x\n",
4095            aeu_mask, deasserted);
4096         aeu_mask |= (deasserted & 0x3ff);
4097         DP(NETIF_MSG_HW, "new mask %x\n", aeu_mask);
4098
4099         REG_WR(bp, reg_addr, aeu_mask);
4100         bnx2x_release_hw_lock(bp, HW_LOCK_RESOURCE_PORT0_ATT_MASK + port);
4101
4102         DP(NETIF_MSG_HW, "attn_state %x\n", bp->attn_state);
4103         bp->attn_state &= ~deasserted;
4104         DP(NETIF_MSG_HW, "new state %x\n", bp->attn_state);
4105 }
4106
4107 static void bnx2x_attn_int(struct bnx2x *bp)
4108 {
4109         /* read local copy of bits */
4110         u32 attn_bits = le32_to_cpu(bp->def_status_blk->atten_status_block.
4111                                                                 attn_bits);
4112         u32 attn_ack = le32_to_cpu(bp->def_status_blk->atten_status_block.
4113                                                                 attn_bits_ack);
4114         u32 attn_state = bp->attn_state;
4115
4116         /* look for changed bits */
4117         u32 asserted   =  attn_bits & ~attn_ack & ~attn_state;
4118         u32 deasserted = ~attn_bits &  attn_ack &  attn_state;
4119
4120         DP(NETIF_MSG_HW,
4121            "attn_bits %x  attn_ack %x  asserted %x  deasserted %x\n",
4122            attn_bits, attn_ack, asserted, deasserted);
4123
4124         if (~(attn_bits ^ attn_ack) & (attn_bits ^ attn_state))
4125                 BNX2X_ERR("BAD attention state\n");
4126
4127         /* handle bits that were raised */
4128         if (asserted)
4129                 bnx2x_attn_int_asserted(bp, asserted);
4130
4131         if (deasserted)
4132                 bnx2x_attn_int_deasserted(bp, deasserted);
4133 }
4134
4135 void bnx2x_igu_ack_sb(struct bnx2x *bp, u8 igu_sb_id, u8 segment,
4136                       u16 index, u8 op, u8 update)
4137 {
4138         u32 igu_addr = BAR_IGU_INTMEM + (IGU_CMD_INT_ACK_BASE + igu_sb_id)*8;
4139
4140         bnx2x_igu_ack_sb_gen(bp, igu_sb_id, segment, index, op, update,
4141                              igu_addr);
4142 }
4143
4144 static inline void bnx2x_update_eq_prod(struct bnx2x *bp, u16 prod)
4145 {
4146         /* No memory barriers */
4147         storm_memset_eq_prod(bp, prod, BP_FUNC(bp));
4148         mmiowb(); /* keep prod updates ordered */
4149 }
4150
4151 #ifdef BCM_CNIC
4152 static int  bnx2x_cnic_handle_cfc_del(struct bnx2x *bp, u32 cid,
4153                                       union event_ring_elem *elem)
4154 {
4155         u8 err = elem->message.error;
4156
4157         if (!bp->cnic_eth_dev.starting_cid  ||
4158             (cid < bp->cnic_eth_dev.starting_cid &&
4159             cid != bp->cnic_eth_dev.iscsi_l2_cid))
4160                 return 1;
4161
4162         DP(BNX2X_MSG_SP, "got delete ramrod for CNIC CID %d\n", cid);
4163
4164         if (unlikely(err)) {
4165
4166                 BNX2X_ERR("got delete ramrod for CNIC CID %d with error!\n",
4167                           cid);
4168                 bnx2x_panic_dump(bp);
4169         }
4170         bnx2x_cnic_cfc_comp(bp, cid, err);
4171         return 0;
4172 }
4173 #endif
4174
4175 static inline void bnx2x_handle_mcast_eqe(struct bnx2x *bp)
4176 {
4177         struct bnx2x_mcast_ramrod_params rparam;
4178         int rc;
4179
4180         memset(&rparam, 0, sizeof(rparam));
4181
4182         rparam.mcast_obj = &bp->mcast_obj;
4183
4184         netif_addr_lock_bh(bp->dev);
4185
4186         /* Clear pending state for the last command */
4187         bp->mcast_obj.raw.clear_pending(&bp->mcast_obj.raw);
4188
4189         /* If there are pending mcast commands - send them */
4190         if (bp->mcast_obj.check_pending(&bp->mcast_obj)) {
4191                 rc = bnx2x_config_mcast(bp, &rparam, BNX2X_MCAST_CMD_CONT);
4192                 if (rc < 0)
4193                         BNX2X_ERR("Failed to send pending mcast commands: %d\n",
4194                                   rc);
4195         }
4196
4197         netif_addr_unlock_bh(bp->dev);
4198 }
4199
4200 static inline void bnx2x_handle_classification_eqe(struct bnx2x *bp,
4201                                                    union event_ring_elem *elem)
4202 {
4203         unsigned long ramrod_flags = 0;
4204         int rc = 0;
4205         u32 cid = elem->message.data.eth_event.echo & BNX2X_SWCID_MASK;
4206         struct bnx2x_vlan_mac_obj *vlan_mac_obj;
4207
4208         /* Always push next commands out, don't wait here */
4209         __set_bit(RAMROD_CONT, &ramrod_flags);
4210
4211         switch (elem->message.data.eth_event.echo >> BNX2X_SWCID_SHIFT) {
4212         case BNX2X_FILTER_MAC_PENDING:
4213 #ifdef BCM_CNIC
4214                 if (cid == BNX2X_ISCSI_ETH_CID)
4215                         vlan_mac_obj = &bp->iscsi_l2_mac_obj;
4216                 else
4217 #endif
4218                         vlan_mac_obj = &bp->fp[cid].mac_obj;
4219
4220                 break;
4221                 vlan_mac_obj = &bp->fp[cid].mac_obj;
4222
4223         case BNX2X_FILTER_MCAST_PENDING:
4224                 /* This is only relevant for 57710 where multicast MACs are
4225                  * configured as unicast MACs using the same ramrod.
4226                  */
4227                 bnx2x_handle_mcast_eqe(bp);
4228                 return;
4229         default:
4230                 BNX2X_ERR("Unsupported classification command: %d\n",
4231                           elem->message.data.eth_event.echo);
4232                 return;
4233         }
4234
4235         rc = vlan_mac_obj->complete(bp, vlan_mac_obj, elem, &ramrod_flags);
4236
4237         if (rc < 0)
4238                 BNX2X_ERR("Failed to schedule new commands: %d\n", rc);
4239         else if (rc > 0)
4240                 DP(BNX2X_MSG_SP, "Scheduled next pending commands...\n");
4241
4242 }
4243
4244 #ifdef BCM_CNIC
4245 static void bnx2x_set_iscsi_eth_rx_mode(struct bnx2x *bp, bool start);
4246 #endif
4247
4248 static inline void bnx2x_handle_rx_mode_eqe(struct bnx2x *bp)
4249 {
4250         netif_addr_lock_bh(bp->dev);
4251
4252         clear_bit(BNX2X_FILTER_RX_MODE_PENDING, &bp->sp_state);
4253
4254         /* Send rx_mode command again if was requested */
4255         if (test_and_clear_bit(BNX2X_FILTER_RX_MODE_SCHED, &bp->sp_state))
4256                 bnx2x_set_storm_rx_mode(bp);
4257 #ifdef BCM_CNIC
4258         else if (test_and_clear_bit(BNX2X_FILTER_ISCSI_ETH_START_SCHED,
4259                                     &bp->sp_state))
4260                 bnx2x_set_iscsi_eth_rx_mode(bp, true);
4261         else if (test_and_clear_bit(BNX2X_FILTER_ISCSI_ETH_STOP_SCHED,
4262                                     &bp->sp_state))
4263                 bnx2x_set_iscsi_eth_rx_mode(bp, false);
4264 #endif
4265
4266         netif_addr_unlock_bh(bp->dev);
4267 }
4268
4269 static inline struct bnx2x_queue_sp_obj *bnx2x_cid_to_q_obj(
4270         struct bnx2x *bp, u32 cid)
4271 {
4272 #ifdef BCM_CNIC
4273         if (cid == BNX2X_FCOE_ETH_CID)
4274                 return &bnx2x_fcoe(bp, q_obj);
4275         else
4276 #endif
4277                 return &bnx2x_fp(bp, cid, q_obj);
4278 }
4279
4280 static void bnx2x_eq_int(struct bnx2x *bp)
4281 {
4282         u16 hw_cons, sw_cons, sw_prod;
4283         union event_ring_elem *elem;
4284         u32 cid;
4285         u8 opcode;
4286         int spqe_cnt = 0;
4287         struct bnx2x_queue_sp_obj *q_obj;
4288         struct bnx2x_func_sp_obj *f_obj = &bp->func_obj;
4289         struct bnx2x_raw_obj *rss_raw = &bp->rss_conf_obj.raw;
4290
4291         hw_cons = le16_to_cpu(*bp->eq_cons_sb);
4292
4293         /* The hw_cos range is 1-255, 257 - the sw_cons range is 0-254, 256.
4294          * when we get the the next-page we nned to adjust so the loop
4295          * condition below will be met. The next element is the size of a
4296          * regular element and hence incrementing by 1
4297          */
4298         if ((hw_cons & EQ_DESC_MAX_PAGE) == EQ_DESC_MAX_PAGE)
4299                 hw_cons++;
4300
4301         /* This function may never run in parallel with itself for a
4302          * specific bp, thus there is no need in "paired" read memory
4303          * barrier here.
4304          */
4305         sw_cons = bp->eq_cons;
4306         sw_prod = bp->eq_prod;
4307
4308         DP(BNX2X_MSG_SP, "EQ:  hw_cons %u  sw_cons %u bp->cq_spq_left %u\n",
4309                         hw_cons, sw_cons, atomic_read(&bp->eq_spq_left));
4310
4311         for (; sw_cons != hw_cons;
4312               sw_prod = NEXT_EQ_IDX(sw_prod), sw_cons = NEXT_EQ_IDX(sw_cons)) {
4313
4314
4315                 elem = &bp->eq_ring[EQ_DESC(sw_cons)];
4316
4317                 cid = SW_CID(elem->message.data.cfc_del_event.cid);
4318                 opcode = elem->message.opcode;
4319
4320
4321                 /* handle eq element */
4322                 switch (opcode) {
4323                 case EVENT_RING_OPCODE_STAT_QUERY:
4324                         DP(NETIF_MSG_TIMER, "got statistics comp event %d\n",
4325                            bp->stats_comp++);
4326                         /* nothing to do with stats comp */
4327                         continue;
4328
4329                 case EVENT_RING_OPCODE_CFC_DEL:
4330                         /* handle according to cid range */
4331                         /*
4332                          * we may want to verify here that the bp state is
4333                          * HALTING
4334                          */
4335                         DP(NETIF_MSG_IFDOWN,
4336                            "got delete ramrod for MULTI[%d]\n", cid);
4337 #ifdef BCM_CNIC
4338                         if (!bnx2x_cnic_handle_cfc_del(bp, cid, elem))
4339                                 goto next_spqe;
4340 #endif
4341                         q_obj = bnx2x_cid_to_q_obj(bp, cid);
4342
4343                         if (q_obj->complete_cmd(bp, q_obj, BNX2X_Q_CMD_CFC_DEL))
4344                                 break;
4345
4346
4347
4348                         goto next_spqe;
4349
4350                 case EVENT_RING_OPCODE_STOP_TRAFFIC:
4351                         DP(NETIF_MSG_IFUP, "got STOP TRAFFIC\n");
4352                         bnx2x_dcbx_set_params(bp, BNX2X_DCBX_STATE_TX_PAUSED);
4353                         goto next_spqe;
4354
4355                 case EVENT_RING_OPCODE_START_TRAFFIC:
4356                         DP(NETIF_MSG_IFUP, "got START TRAFFIC\n");
4357                         bnx2x_dcbx_set_params(bp, BNX2X_DCBX_STATE_TX_RELEASED);
4358                         goto next_spqe;
4359                 case EVENT_RING_OPCODE_FUNCTION_START:
4360                         DP(NETIF_MSG_IFUP, "got FUNC_START ramrod\n");
4361                         if (f_obj->complete_cmd(bp, f_obj, BNX2X_F_CMD_START))
4362                                 break;
4363
4364                         goto next_spqe;
4365
4366                 case EVENT_RING_OPCODE_FUNCTION_STOP:
4367                         DP(NETIF_MSG_IFDOWN, "got FUNC_STOP ramrod\n");
4368                         if (f_obj->complete_cmd(bp, f_obj, BNX2X_F_CMD_STOP))
4369                                 break;
4370
4371                         goto next_spqe;
4372                 }
4373
4374                 switch (opcode | bp->state) {
4375                 case (EVENT_RING_OPCODE_RSS_UPDATE_RULES |
4376                       BNX2X_STATE_OPEN):
4377                 case (EVENT_RING_OPCODE_RSS_UPDATE_RULES |
4378                       BNX2X_STATE_OPENING_WAIT4_PORT):
4379                         cid = elem->message.data.eth_event.echo &
4380                                 BNX2X_SWCID_MASK;
4381                         DP(NETIF_MSG_IFUP, "got RSS_UPDATE ramrod. CID %d\n",
4382                            cid);
4383                         rss_raw->clear_pending(rss_raw);
4384                         break;
4385
4386                 case (EVENT_RING_OPCODE_SET_MAC | BNX2X_STATE_OPEN):
4387                 case (EVENT_RING_OPCODE_SET_MAC | BNX2X_STATE_DIAG):
4388                 case (EVENT_RING_OPCODE_SET_MAC |
4389                       BNX2X_STATE_CLOSING_WAIT4_HALT):
4390                 case (EVENT_RING_OPCODE_CLASSIFICATION_RULES |
4391                       BNX2X_STATE_OPEN):
4392                 case (EVENT_RING_OPCODE_CLASSIFICATION_RULES |
4393                       BNX2X_STATE_DIAG):
4394                 case (EVENT_RING_OPCODE_CLASSIFICATION_RULES |
4395                       BNX2X_STATE_CLOSING_WAIT4_HALT):
4396                         DP(NETIF_MSG_IFUP, "got (un)set mac ramrod\n");
4397                         bnx2x_handle_classification_eqe(bp, elem);
4398                         break;
4399
4400                 case (EVENT_RING_OPCODE_MULTICAST_RULES |
4401                       BNX2X_STATE_OPEN):
4402                 case (EVENT_RING_OPCODE_MULTICAST_RULES |
4403                       BNX2X_STATE_DIAG):
4404                 case (EVENT_RING_OPCODE_MULTICAST_RULES |
4405                       BNX2X_STATE_CLOSING_WAIT4_HALT):
4406                         DP(NETIF_MSG_IFUP, "got mcast ramrod\n");
4407                         bnx2x_handle_mcast_eqe(bp);
4408                         break;
4409
4410                 case (EVENT_RING_OPCODE_FILTERS_RULES |
4411                       BNX2X_STATE_OPEN):
4412                 case (EVENT_RING_OPCODE_FILTERS_RULES |
4413                       BNX2X_STATE_DIAG):
4414                 case (EVENT_RING_OPCODE_FILTERS_RULES |
4415                       BNX2X_STATE_CLOSING_WAIT4_HALT):
4416                         DP(NETIF_MSG_IFUP, "got rx_mode ramrod\n");
4417                         bnx2x_handle_rx_mode_eqe(bp);
4418                         break;
4419                 default:
4420                         /* unknown event log error and continue */
4421                         BNX2X_ERR("Unknown EQ event %d, bp->state 0x%x\n",
4422                                   elem->message.opcode, bp->state);
4423                 }
4424 next_spqe:
4425                 spqe_cnt++;
4426         } /* for */
4427
4428         smp_mb__before_atomic_inc();
4429         atomic_add(spqe_cnt, &bp->eq_spq_left);
4430
4431         bp->eq_cons = sw_cons;
4432         bp->eq_prod = sw_prod;
4433         /* Make sure that above mem writes were issued towards the memory */
4434         smp_wmb();
4435
4436         /* update producer */
4437         bnx2x_update_eq_prod(bp, bp->eq_prod);
4438 }
4439
4440 static void bnx2x_sp_task(struct work_struct *work)
4441 {
4442         struct bnx2x *bp = container_of(work, struct bnx2x, sp_task.work);
4443         u16 status;
4444
4445         status = bnx2x_update_dsb_idx(bp);
4446 /*      if (status == 0)                                     */
4447 /*              BNX2X_ERR("spurious slowpath interrupt!\n"); */
4448
4449         DP(NETIF_MSG_INTR, "got a slowpath interrupt (status 0x%x)\n", status);
4450
4451         /* HW attentions */
4452         if (status & BNX2X_DEF_SB_ATT_IDX) {
4453                 bnx2x_attn_int(bp);
4454                 status &= ~BNX2X_DEF_SB_ATT_IDX;
4455         }
4456
4457         /* SP events: STAT_QUERY and others */
4458         if (status & BNX2X_DEF_SB_IDX) {
4459 #ifdef BCM_CNIC
4460                 struct bnx2x_fastpath *fp = bnx2x_fcoe_fp(bp);
4461
4462                 if ((!NO_FCOE(bp)) &&
4463                         (bnx2x_has_rx_work(fp) || bnx2x_has_tx_work(fp)))
4464                         napi_schedule(&bnx2x_fcoe(bp, napi));
4465 #endif
4466                 /* Handle EQ completions */
4467                 bnx2x_eq_int(bp);
4468
4469                 bnx2x_ack_sb(bp, bp->igu_dsb_id, USTORM_ID,
4470                         le16_to_cpu(bp->def_idx), IGU_INT_NOP, 1);
4471
4472                 status &= ~BNX2X_DEF_SB_IDX;
4473         }
4474
4475         if (unlikely(status))
4476                 DP(NETIF_MSG_INTR, "got an unknown interrupt! (status 0x%x)\n",
4477                    status);
4478
4479         bnx2x_ack_sb(bp, bp->igu_dsb_id, ATTENTION_ID,
4480              le16_to_cpu(bp->def_att_idx), IGU_INT_ENABLE, 1);
4481 }
4482
4483 irqreturn_t bnx2x_msix_sp_int(int irq, void *dev_instance)
4484 {
4485         struct net_device *dev = dev_instance;
4486         struct bnx2x *bp = netdev_priv(dev);
4487
4488         bnx2x_ack_sb(bp, bp->igu_dsb_id, USTORM_ID, 0,
4489                      IGU_INT_DISABLE, 0);
4490
4491 #ifdef BNX2X_STOP_ON_ERROR
4492         if (unlikely(bp->panic))
4493                 return IRQ_HANDLED;
4494 #endif
4495
4496 #ifdef BCM_CNIC
4497         {
4498                 struct cnic_ops *c_ops;
4499
4500                 rcu_read_lock();
4501                 c_ops = rcu_dereference(bp->cnic_ops);
4502                 if (c_ops)
4503                         c_ops->cnic_handler(bp->cnic_data, NULL);
4504                 rcu_read_unlock();
4505         }
4506 #endif
4507         queue_delayed_work(bnx2x_wq, &bp->sp_task, 0);
4508
4509         return IRQ_HANDLED;
4510 }
4511
4512 /* end of slow path */
4513
4514
4515 void bnx2x_drv_pulse(struct bnx2x *bp)
4516 {
4517         SHMEM_WR(bp, func_mb[BP_FW_MB_IDX(bp)].drv_pulse_mb,
4518                  bp->fw_drv_pulse_wr_seq);
4519 }
4520
4521
4522 static void bnx2x_timer(unsigned long data)
4523 {
4524         struct bnx2x *bp = (struct bnx2x *) data;
4525
4526         if (!netif_running(bp->dev))
4527                 return;
4528
4529         if (poll) {
4530                 struct bnx2x_fastpath *fp = &bp->fp[0];
4531
4532                 bnx2x_tx_int(fp);
4533                 bnx2x_rx_int(fp, 1000);
4534         }
4535
4536         if (!BP_NOMCP(bp)) {
4537                 int mb_idx = BP_FW_MB_IDX(bp);
4538                 u32 drv_pulse;
4539                 u32 mcp_pulse;
4540
4541                 ++bp->fw_drv_pulse_wr_seq;
4542                 bp->fw_drv_pulse_wr_seq &= DRV_PULSE_SEQ_MASK;
4543                 /* TBD - add SYSTEM_TIME */
4544                 drv_pulse = bp->fw_drv_pulse_wr_seq;
4545                 bnx2x_drv_pulse(bp);
4546
4547                 mcp_pulse = (SHMEM_RD(bp, func_mb[mb_idx].mcp_pulse_mb) &
4548                              MCP_PULSE_SEQ_MASK);
4549                 /* The delta between driver pulse and mcp response
4550                  * should be 1 (before mcp response) or 0 (after mcp response)
4551                  */
4552                 if ((drv_pulse != mcp_pulse) &&
4553                     (drv_pulse != ((mcp_pulse + 1) & MCP_PULSE_SEQ_MASK))) {
4554                         /* someone lost a heartbeat... */
4555                         BNX2X_ERR("drv_pulse (0x%x) != mcp_pulse (0x%x)\n",
4556                                   drv_pulse, mcp_pulse);
4557                 }
4558         }
4559
4560         if (bp->state == BNX2X_STATE_OPEN)
4561                 bnx2x_stats_handle(bp, STATS_EVENT_UPDATE);
4562
4563         mod_timer(&bp->timer, jiffies + bp->current_interval);
4564 }
4565
4566 /* end of Statistics */
4567
4568 /* nic init */
4569
4570 /*
4571  * nic init service functions
4572  */
4573
4574 static inline void bnx2x_fill(struct bnx2x *bp, u32 addr, int fill, u32 len)
4575 {
4576         u32 i;
4577         if (!(len%4) && !(addr%4))
4578                 for (i = 0; i < len; i += 4)
4579                         REG_WR(bp, addr + i, fill);
4580         else
4581                 for (i = 0; i < len; i++)
4582                         REG_WR8(bp, addr + i, fill);
4583
4584 }
4585
4586 /* helper: writes FP SP data to FW - data_size in dwords */
4587 static inline void bnx2x_wr_fp_sb_data(struct bnx2x *bp,
4588                                        int fw_sb_id,
4589                                        u32 *sb_data_p,
4590                                        u32 data_size)
4591 {
4592         int index;
4593         for (index = 0; index < data_size; index++)
4594                 REG_WR(bp, BAR_CSTRORM_INTMEM +
4595                         CSTORM_STATUS_BLOCK_DATA_OFFSET(fw_sb_id) +
4596                         sizeof(u32)*index,
4597                         *(sb_data_p + index));
4598 }
4599
4600 static inline void bnx2x_zero_fp_sb(struct bnx2x *bp, int fw_sb_id)
4601 {
4602         u32 *sb_data_p;
4603         u32 data_size = 0;
4604         struct hc_status_block_data_e2 sb_data_e2;
4605         struct hc_status_block_data_e1x sb_data_e1x;
4606
4607         /* disable the function first */
4608         if (!CHIP_IS_E1x(bp)) {
4609                 memset(&sb_data_e2, 0, sizeof(struct hc_status_block_data_e2));
4610                 sb_data_e2.common.state = SB_DISABLED;
4611                 sb_data_e2.common.p_func.vf_valid = false;
4612                 sb_data_p = (u32 *)&sb_data_e2;
4613                 data_size = sizeof(struct hc_status_block_data_e2)/sizeof(u32);
4614         } else {
4615                 memset(&sb_data_e1x, 0,
4616                        sizeof(struct hc_status_block_data_e1x));
4617                 sb_data_e1x.common.state = SB_DISABLED;
4618                 sb_data_e1x.common.p_func.vf_valid = false;
4619                 sb_data_p = (u32 *)&sb_data_e1x;
4620                 data_size = sizeof(struct hc_status_block_data_e1x)/sizeof(u32);
4621         }
4622         bnx2x_wr_fp_sb_data(bp, fw_sb_id, sb_data_p, data_size);
4623
4624         bnx2x_fill(bp, BAR_CSTRORM_INTMEM +
4625                         CSTORM_STATUS_BLOCK_OFFSET(fw_sb_id), 0,
4626                         CSTORM_STATUS_BLOCK_SIZE);
4627         bnx2x_fill(bp, BAR_CSTRORM_INTMEM +
4628                         CSTORM_SYNC_BLOCK_OFFSET(fw_sb_id), 0,
4629                         CSTORM_SYNC_BLOCK_SIZE);
4630 }
4631
4632 /* helper:  writes SP SB data to FW */
4633 static inline void bnx2x_wr_sp_sb_data(struct bnx2x *bp,
4634                 struct hc_sp_status_block_data *sp_sb_data)
4635 {
4636         int func = BP_FUNC(bp);
4637         int i;
4638         for (i = 0; i < sizeof(struct hc_sp_status_block_data)/sizeof(u32); i++)
4639                 REG_WR(bp, BAR_CSTRORM_INTMEM +
4640                         CSTORM_SP_STATUS_BLOCK_DATA_OFFSET(func) +
4641                         i*sizeof(u32),
4642                         *((u32 *)sp_sb_data + i));
4643 }
4644
4645 static inline void bnx2x_zero_sp_sb(struct bnx2x *bp)
4646 {
4647         int func = BP_FUNC(bp);
4648         struct hc_sp_status_block_data sp_sb_data;
4649         memset(&sp_sb_data, 0, sizeof(struct hc_sp_status_block_data));
4650
4651         sp_sb_data.state = SB_DISABLED;
4652         sp_sb_data.p_func.vf_valid = false;
4653
4654         bnx2x_wr_sp_sb_data(bp, &sp_sb_data);
4655
4656         bnx2x_fill(bp, BAR_CSTRORM_INTMEM +
4657                         CSTORM_SP_STATUS_BLOCK_OFFSET(func), 0,
4658                         CSTORM_SP_STATUS_BLOCK_SIZE);
4659         bnx2x_fill(bp, BAR_CSTRORM_INTMEM +
4660                         CSTORM_SP_SYNC_BLOCK_OFFSET(func), 0,
4661                         CSTORM_SP_SYNC_BLOCK_SIZE);
4662
4663 }
4664
4665
4666 static inline
4667 void bnx2x_setup_ndsb_state_machine(struct hc_status_block_sm *hc_sm,
4668                                            int igu_sb_id, int igu_seg_id)
4669 {
4670         hc_sm->igu_sb_id = igu_sb_id;
4671         hc_sm->igu_seg_id = igu_seg_id;
4672         hc_sm->timer_value = 0xFF;
4673         hc_sm->time_to_expire = 0xFFFFFFFF;
4674 }
4675
4676 static void bnx2x_init_sb(struct bnx2x *bp, dma_addr_t mapping, int vfid,
4677                           u8 vf_valid, int fw_sb_id, int igu_sb_id)
4678 {
4679         int igu_seg_id;
4680
4681         struct hc_status_block_data_e2 sb_data_e2;
4682         struct hc_status_block_data_e1x sb_data_e1x;
4683         struct hc_status_block_sm  *hc_sm_p;
4684         int data_size;
4685         u32 *sb_data_p;
4686
4687         if (CHIP_INT_MODE_IS_BC(bp))
4688                 igu_seg_id = HC_SEG_ACCESS_NORM;
4689         else
4690                 igu_seg_id = IGU_SEG_ACCESS_NORM;
4691
4692         bnx2x_zero_fp_sb(bp, fw_sb_id);
4693
4694         if (!CHIP_IS_E1x(bp)) {
4695                 memset(&sb_data_e2, 0, sizeof(struct hc_status_block_data_e2));
4696                 sb_data_e2.common.state = SB_ENABLED;
4697                 sb_data_e2.common.p_func.pf_id = BP_FUNC(bp);
4698                 sb_data_e2.common.p_func.vf_id = vfid;
4699                 sb_data_e2.common.p_func.vf_valid = vf_valid;
4700                 sb_data_e2.common.p_func.vnic_id = BP_VN(bp);
4701                 sb_data_e2.common.same_igu_sb_1b = true;
4702                 sb_data_e2.common.host_sb_addr.hi = U64_HI(mapping);
4703                 sb_data_e2.common.host_sb_addr.lo = U64_LO(mapping);
4704                 hc_sm_p = sb_data_e2.common.state_machine;
4705                 sb_data_p = (u32 *)&sb_data_e2;
4706                 data_size = sizeof(struct hc_status_block_data_e2)/sizeof(u32);
4707         } else {
4708                 memset(&sb_data_e1x, 0,
4709                        sizeof(struct hc_status_block_data_e1x));
4710                 sb_data_e1x.common.state = SB_ENABLED;
4711                 sb_data_e1x.common.p_func.pf_id = BP_FUNC(bp);
4712                 sb_data_e1x.common.p_func.vf_id = 0xff;
4713                 sb_data_e1x.common.p_func.vf_valid = false;
4714                 sb_data_e1x.common.p_func.vnic_id = BP_VN(bp);
4715                 sb_data_e1x.common.same_igu_sb_1b = true;
4716                 sb_data_e1x.common.host_sb_addr.hi = U64_HI(mapping);
4717                 sb_data_e1x.common.host_sb_addr.lo = U64_LO(mapping);
4718                 hc_sm_p = sb_data_e1x.common.state_machine;
4719                 sb_data_p = (u32 *)&sb_data_e1x;
4720                 data_size = sizeof(struct hc_status_block_data_e1x)/sizeof(u32);
4721         }
4722
4723         bnx2x_setup_ndsb_state_machine(&hc_sm_p[SM_RX_ID],
4724                                        igu_sb_id, igu_seg_id);
4725         bnx2x_setup_ndsb_state_machine(&hc_sm_p[SM_TX_ID],
4726                                        igu_sb_id, igu_seg_id);
4727
4728         DP(NETIF_MSG_HW, "Init FW SB %d\n", fw_sb_id);
4729
4730         /* write indecies to HW */
4731         bnx2x_wr_fp_sb_data(bp, fw_sb_id, sb_data_p, data_size);
4732 }
4733
4734 static void bnx2x_update_coalesce_sb(struct bnx2x *bp, u8 fw_sb_id,
4735                                      u16 tx_usec, u16 rx_usec)
4736 {
4737         bnx2x_update_coalesce_sb_index(bp, fw_sb_id, U_SB_ETH_RX_CQ_INDEX,
4738                                     false, rx_usec);
4739         bnx2x_update_coalesce_sb_index(bp, fw_sb_id, C_SB_ETH_TX_CQ_INDEX,
4740                                     false, tx_usec);
4741 }
4742
4743 static void bnx2x_init_def_sb(struct bnx2x *bp)
4744 {
4745         struct host_sp_status_block *def_sb = bp->def_status_blk;
4746         dma_addr_t mapping = bp->def_status_blk_mapping;
4747         int igu_sp_sb_index;
4748         int igu_seg_id;
4749         int port = BP_PORT(bp);
4750         int func = BP_FUNC(bp);
4751         int reg_offset;
4752         u64 section;
4753         int index;
4754         struct hc_sp_status_block_data sp_sb_data;
4755         memset(&sp_sb_data, 0, sizeof(struct hc_sp_status_block_data));
4756
4757         if (CHIP_INT_MODE_IS_BC(bp)) {
4758                 igu_sp_sb_index = DEF_SB_IGU_ID;
4759                 igu_seg_id = HC_SEG_ACCESS_DEF;
4760         } else {
4761                 igu_sp_sb_index = bp->igu_dsb_id;
4762                 igu_seg_id = IGU_SEG_ACCESS_DEF;
4763         }
4764
4765         /* ATTN */
4766         section = ((u64)mapping) + offsetof(struct host_sp_status_block,
4767                                             atten_status_block);
4768         def_sb->atten_status_block.status_block_id = igu_sp_sb_index;
4769
4770         bp->attn_state = 0;
4771
4772         reg_offset = (port ? MISC_REG_AEU_ENABLE1_FUNC_1_OUT_0 :
4773                              MISC_REG_AEU_ENABLE1_FUNC_0_OUT_0);
4774         for (index = 0; index < MAX_DYNAMIC_ATTN_GRPS; index++) {
4775                 int sindex;
4776                 /* take care of sig[0]..sig[4] */
4777                 for (sindex = 0; sindex < 4; sindex++)
4778                         bp->attn_group[index].sig[sindex] =
4779                            REG_RD(bp, reg_offset + sindex*0x4 + 0x10*index);
4780
4781                 if (!CHIP_IS_E1x(bp))
4782                         /*
4783                          * enable5 is separate from the rest of the registers,
4784                          * and therefore the address skip is 4
4785                          * and not 16 between the different groups
4786                          */
4787                         bp->attn_group[index].sig[4] = REG_RD(bp,
4788                                         reg_offset + 0x10 + 0x4*index);
4789                 else
4790                         bp->attn_group[index].sig[4] = 0;
4791         }
4792
4793         if (bp->common.int_block == INT_BLOCK_HC) {
4794                 reg_offset = (port ? HC_REG_ATTN_MSG1_ADDR_L :
4795                                      HC_REG_ATTN_MSG0_ADDR_L);
4796
4797                 REG_WR(bp, reg_offset, U64_LO(section));
4798                 REG_WR(bp, reg_offset + 4, U64_HI(section));
4799         } else if (!CHIP_IS_E1x(bp)) {
4800                 REG_WR(bp, IGU_REG_ATTN_MSG_ADDR_L, U64_LO(section));
4801                 REG_WR(bp, IGU_REG_ATTN_MSG_ADDR_H, U64_HI(section));
4802         }
4803
4804         section = ((u64)mapping) + offsetof(struct host_sp_status_block,
4805                                             sp_sb);
4806
4807         bnx2x_zero_sp_sb(bp);
4808
4809         sp_sb_data.state                = SB_ENABLED;
4810         sp_sb_data.host_sb_addr.lo      = U64_LO(section);
4811         sp_sb_data.host_sb_addr.hi      = U64_HI(section);
4812         sp_sb_data.igu_sb_id            = igu_sp_sb_index;
4813         sp_sb_data.igu_seg_id           = igu_seg_id;
4814         sp_sb_data.p_func.pf_id         = func;
4815         sp_sb_data.p_func.vnic_id       = BP_VN(bp);
4816         sp_sb_data.p_func.vf_id         = 0xff;
4817
4818         bnx2x_wr_sp_sb_data(bp, &sp_sb_data);
4819
4820         bnx2x_ack_sb(bp, bp->igu_dsb_id, USTORM_ID, 0, IGU_INT_ENABLE, 0);
4821 }
4822
4823 void bnx2x_update_coalesce(struct bnx2x *bp)
4824 {
4825         int i;
4826
4827         for_each_eth_queue(bp, i)
4828                 bnx2x_update_coalesce_sb(bp, bp->fp[i].fw_sb_id,
4829                                          bp->tx_ticks, bp->rx_ticks);
4830 }
4831
4832 static void bnx2x_init_sp_ring(struct bnx2x *bp)
4833 {
4834         spin_lock_init(&bp->spq_lock);
4835         atomic_set(&bp->cq_spq_left, MAX_SPQ_PENDING);
4836
4837         bp->spq_prod_idx = 0;
4838         bp->dsb_sp_prod = BNX2X_SP_DSB_INDEX;
4839         bp->spq_prod_bd = bp->spq;
4840         bp->spq_last_bd = bp->spq_prod_bd + MAX_SP_DESC_CNT;
4841 }
4842
4843 static void bnx2x_init_eq_ring(struct bnx2x *bp)
4844 {
4845         int i;
4846         for (i = 1; i <= NUM_EQ_PAGES; i++) {
4847                 union event_ring_elem *elem =
4848                         &bp->eq_ring[EQ_DESC_CNT_PAGE * i - 1];
4849
4850                 elem->next_page.addr.hi =
4851                         cpu_to_le32(U64_HI(bp->eq_mapping +
4852                                    BCM_PAGE_SIZE * (i % NUM_EQ_PAGES)));
4853                 elem->next_page.addr.lo =
4854                         cpu_to_le32(U64_LO(bp->eq_mapping +
4855                                    BCM_PAGE_SIZE*(i % NUM_EQ_PAGES)));
4856         }
4857         bp->eq_cons = 0;
4858         bp->eq_prod = NUM_EQ_DESC;
4859         bp->eq_cons_sb = BNX2X_EQ_INDEX;
4860         /* we want a warning message before it gets rought... */
4861         atomic_set(&bp->eq_spq_left,
4862                 min_t(int, MAX_SP_DESC_CNT - MAX_SPQ_PENDING, NUM_EQ_DESC) - 1);
4863 }
4864
4865
4866 /* called with netif_addr_lock_bh() */
4867 void bnx2x_set_q_rx_mode(struct bnx2x *bp, u8 cl_id,
4868                          unsigned long rx_mode_flags,
4869                          unsigned long rx_accept_flags,
4870                          unsigned long tx_accept_flags,
4871                          unsigned long ramrod_flags)
4872 {
4873         struct bnx2x_rx_mode_ramrod_params ramrod_param;
4874         int rc;
4875
4876         memset(&ramrod_param, 0, sizeof(ramrod_param));
4877
4878         /* Prepare ramrod parameters */
4879         ramrod_param.cid = 0;
4880         ramrod_param.cl_id = cl_id;
4881         ramrod_param.rx_mode_obj = &bp->rx_mode_obj;
4882         ramrod_param.func_id = BP_FUNC(bp);
4883
4884         ramrod_param.pstate = &bp->sp_state;
4885         ramrod_param.state = BNX2X_FILTER_RX_MODE_PENDING;
4886
4887         ramrod_param.rdata = bnx2x_sp(bp, rx_mode_rdata);
4888         ramrod_param.rdata_mapping = bnx2x_sp_mapping(bp, rx_mode_rdata);
4889
4890         set_bit(BNX2X_FILTER_RX_MODE_PENDING, &bp->sp_state);
4891
4892         ramrod_param.ramrod_flags = ramrod_flags;
4893         ramrod_param.rx_mode_flags = rx_mode_flags;
4894
4895         ramrod_param.rx_accept_flags = rx_accept_flags;
4896         ramrod_param.tx_accept_flags = tx_accept_flags;
4897
4898         rc = bnx2x_config_rx_mode(bp, &ramrod_param);
4899         if (rc < 0) {
4900                 BNX2X_ERR("Set rx_mode %d failed\n", bp->rx_mode);
4901                 return;
4902         }
4903 }
4904
4905 /* called with netif_addr_lock_bh() */
4906 void bnx2x_set_storm_rx_mode(struct bnx2x *bp)
4907 {
4908         unsigned long rx_mode_flags = 0, ramrod_flags = 0;
4909         unsigned long rx_accept_flags = 0, tx_accept_flags = 0;
4910
4911 #ifdef BCM_CNIC
4912         if (!NO_FCOE(bp))
4913
4914                 /* Configure rx_mode of FCoE Queue */
4915                 __set_bit(BNX2X_RX_MODE_FCOE_ETH, &rx_mode_flags);
4916 #endif
4917
4918         switch (bp->rx_mode) {
4919         case BNX2X_RX_MODE_NONE:
4920                 /*
4921                  * 'drop all' supersedes any accept flags that may have been
4922                  * passed to the function.
4923                  */
4924                 break;
4925         case BNX2X_RX_MODE_NORMAL:
4926                 __set_bit(BNX2X_ACCEPT_UNICAST, &rx_accept_flags);
4927                 __set_bit(BNX2X_ACCEPT_MULTICAST, &rx_accept_flags);
4928                 __set_bit(BNX2X_ACCEPT_BROADCAST, &rx_accept_flags);
4929
4930                 /* internal switching mode */
4931                 __set_bit(BNX2X_ACCEPT_UNICAST, &tx_accept_flags);
4932                 __set_bit(BNX2X_ACCEPT_MULTICAST, &tx_accept_flags);
4933                 __set_bit(BNX2X_ACCEPT_BROADCAST, &tx_accept_flags);
4934
4935                 break;
4936         case BNX2X_RX_MODE_ALLMULTI:
4937                 __set_bit(BNX2X_ACCEPT_UNICAST, &rx_accept_flags);
4938                 __set_bit(BNX2X_ACCEPT_ALL_MULTICAST, &rx_accept_flags);
4939                 __set_bit(BNX2X_ACCEPT_BROADCAST, &rx_accept_flags);
4940
4941                 /* internal switching mode */
4942                 __set_bit(BNX2X_ACCEPT_UNICAST, &tx_accept_flags);
4943                 __set_bit(BNX2X_ACCEPT_ALL_MULTICAST, &tx_accept_flags);
4944                 __set_bit(BNX2X_ACCEPT_BROADCAST, &tx_accept_flags);
4945
4946                 break;
4947         case BNX2X_RX_MODE_PROMISC:
4948                 /* According to deffinition of SI mode, iface in promisc mode
4949                  * should receive matched and unmatched (in resolution of port)
4950                  * unicast packets.
4951                  */
4952                 __set_bit(BNX2X_ACCEPT_UNMATCHED, &rx_accept_flags);
4953                 __set_bit(BNX2X_ACCEPT_UNICAST, &rx_accept_flags);
4954                 __set_bit(BNX2X_ACCEPT_ALL_MULTICAST, &rx_accept_flags);
4955                 __set_bit(BNX2X_ACCEPT_BROADCAST, &rx_accept_flags);
4956
4957                 /* internal switching mode */
4958                 __set_bit(BNX2X_ACCEPT_ALL_MULTICAST, &tx_accept_flags);
4959                 __set_bit(BNX2X_ACCEPT_BROADCAST, &tx_accept_flags);
4960
4961                 if (IS_MF_SI(bp))
4962                         __set_bit(BNX2X_ACCEPT_ALL_UNICAST, &tx_accept_flags);
4963                 else
4964                         __set_bit(BNX2X_ACCEPT_UNICAST, &tx_accept_flags);
4965
4966                 break;
4967         default:
4968                 BNX2X_ERR("Unknown rx_mode: %d\n", bp->rx_mode);
4969                 return;
4970         }
4971
4972         if (bp->rx_mode != BNX2X_RX_MODE_NONE) {
4973                 __set_bit(BNX2X_ACCEPT_ANY_VLAN, &rx_accept_flags);
4974                 __set_bit(BNX2X_ACCEPT_ANY_VLAN, &tx_accept_flags);
4975         }
4976
4977         __set_bit(RAMROD_RX, &ramrod_flags);
4978         __set_bit(RAMROD_TX, &ramrod_flags);
4979
4980         bnx2x_set_q_rx_mode(bp, bp->fp->cl_id, rx_mode_flags, rx_accept_flags,
4981                             tx_accept_flags, ramrod_flags);
4982 }
4983
4984 static void bnx2x_init_internal_common(struct bnx2x *bp)
4985 {
4986         int i;
4987
4988         if (IS_MF_SI(bp))
4989                 /*
4990                  * In switch independent mode, the TSTORM needs to accept
4991                  * packets that failed classification, since approximate match
4992                  * mac addresses aren't written to NIG LLH
4993                  */
4994                 REG_WR8(bp, BAR_TSTRORM_INTMEM +
4995                             TSTORM_ACCEPT_CLASSIFY_FAILED_OFFSET, 2);
4996         else if (!CHIP_IS_E1(bp)) /* 57710 doesn't support MF */
4997                 REG_WR8(bp, BAR_TSTRORM_INTMEM +
4998                             TSTORM_ACCEPT_CLASSIFY_FAILED_OFFSET, 0);
4999
5000         /* Zero this manually as its initialization is
5001            currently missing in the initTool */
5002         for (i = 0; i < (USTORM_AGG_DATA_SIZE >> 2); i++)
5003                 REG_WR(bp, BAR_USTRORM_INTMEM +
5004                        USTORM_AGG_DATA_OFFSET + i * 4, 0);
5005         if (!CHIP_IS_E1x(bp)) {
5006                 REG_WR8(bp, BAR_CSTRORM_INTMEM + CSTORM_IGU_MODE_OFFSET,
5007                         CHIP_INT_MODE_IS_BC(bp) ?
5008                         HC_IGU_BC_MODE : HC_IGU_NBC_MODE);
5009         }
5010 }
5011
5012 static void bnx2x_init_internal(struct bnx2x *bp, u32 load_code)
5013 {
5014         switch (load_code) {
5015         case FW_MSG_CODE_DRV_LOAD_COMMON:
5016         case FW_MSG_CODE_DRV_LOAD_COMMON_CHIP:
5017                 bnx2x_init_internal_common(bp);
5018                 /* no break */
5019
5020         case FW_MSG_CODE_DRV_LOAD_PORT:
5021                 /* nothing to do */
5022                 /* no break */
5023
5024         case FW_MSG_CODE_DRV_LOAD_FUNCTION:
5025                 /* internal memory per function is
5026                    initialized inside bnx2x_pf_init */
5027                 break;
5028
5029         default:
5030                 BNX2X_ERR("Unknown load_code (0x%x) from MCP\n", load_code);
5031                 break;
5032         }
5033 }
5034
5035 static inline u8 bnx2x_fp_igu_sb_id(struct bnx2x_fastpath *fp)
5036 {
5037         return fp->bp->igu_base_sb + fp->index + CNIC_CONTEXT_USE;
5038 }
5039
5040 static inline u8 bnx2x_fp_fw_sb_id(struct bnx2x_fastpath *fp)
5041 {
5042         return fp->bp->base_fw_ndsb + fp->index + CNIC_CONTEXT_USE;
5043 }
5044
5045 static inline u8 bnx2x_fp_cl_id(struct bnx2x_fastpath *fp)
5046 {
5047         if (CHIP_IS_E1x(fp->bp))
5048                 return BP_L_ID(fp->bp) + fp->index;
5049         else    /* We want Client ID to be the same as IGU SB ID for 57712 */
5050                 return bnx2x_fp_igu_sb_id(fp);
5051 }
5052
5053 static void bnx2x_init_fp(struct bnx2x *bp, int fp_idx)
5054 {
5055         struct bnx2x_fastpath *fp = &bp->fp[fp_idx];
5056         unsigned long q_type = 0;
5057
5058         fp->cid = fp_idx;
5059         fp->cl_id = bnx2x_fp_cl_id(fp);
5060         fp->fw_sb_id = bnx2x_fp_fw_sb_id(fp);
5061         fp->igu_sb_id = bnx2x_fp_igu_sb_id(fp);
5062         /* qZone id equals to FW (per path) client id */
5063         fp->cl_qzone_id  = bnx2x_fp_qzone_id(fp);
5064
5065         /* init shortcut */
5066         fp->ustorm_rx_prods_offset = bnx2x_rx_ustorm_prods_offset(fp);
5067         /* Setup SB indicies */
5068         fp->rx_cons_sb = BNX2X_RX_SB_INDEX;
5069         fp->tx_cons_sb = BNX2X_TX_SB_INDEX;
5070
5071         /* Configure Queue State object */
5072         __set_bit(BNX2X_Q_TYPE_HAS_RX, &q_type);
5073         __set_bit(BNX2X_Q_TYPE_HAS_TX, &q_type);
5074         bnx2x_init_queue_obj(bp, &fp->q_obj, fp->cl_id, fp->cid, BP_FUNC(bp),
5075                 bnx2x_sp(bp, q_rdata), bnx2x_sp_mapping(bp, q_rdata),
5076                               q_type);
5077
5078         /**
5079          * Configure classification DBs: Always enable Tx switching
5080          */
5081         bnx2x_init_vlan_mac_fp_objs(fp, BNX2X_OBJ_TYPE_RX_TX);
5082
5083         DP(NETIF_MSG_IFUP, "queue[%d]:  bnx2x_init_sb(%p,%p)  "
5084                                    "cl_id %d  fw_sb %d  igu_sb %d\n",
5085                    fp_idx, bp, fp->status_blk.e2_sb, fp->cl_id, fp->fw_sb_id,
5086                    fp->igu_sb_id);
5087         bnx2x_init_sb(bp, fp->status_blk_mapping, BNX2X_VF_ID_INVALID, false,
5088                       fp->fw_sb_id, fp->igu_sb_id);
5089
5090         bnx2x_update_fpsb_idx(fp);
5091 }
5092
5093 void bnx2x_nic_init(struct bnx2x *bp, u32 load_code)
5094 {
5095         int i;
5096
5097         for_each_eth_queue(bp, i)
5098                 bnx2x_init_fp(bp, i);
5099 #ifdef BCM_CNIC
5100         if (!NO_FCOE(bp))
5101                 bnx2x_init_fcoe_fp(bp);
5102
5103         bnx2x_init_sb(bp, bp->cnic_sb_mapping,
5104                       BNX2X_VF_ID_INVALID, false,
5105                       bnx2x_cnic_fw_sb_id(bp), bnx2x_cnic_igu_sb_id(bp));
5106
5107 #endif
5108
5109         /* Initialize MOD_ABS interrupts */
5110         bnx2x_init_mod_abs_int(bp, &bp->link_vars, bp->common.chip_id,
5111                                bp->common.shmem_base, bp->common.shmem2_base,
5112                                BP_PORT(bp));
5113         /* ensure status block indices were read */
5114         rmb();
5115
5116         bnx2x_init_def_sb(bp);
5117         bnx2x_update_dsb_idx(bp);
5118         bnx2x_init_rx_rings(bp);
5119         bnx2x_init_tx_rings(bp);
5120         bnx2x_init_sp_ring(bp);
5121         bnx2x_init_eq_ring(bp);
5122         bnx2x_init_internal(bp, load_code);
5123         bnx2x_pf_init(bp);
5124         bnx2x_stats_init(bp);
5125
5126         /* flush all before enabling interrupts */
5127         mb();
5128         mmiowb();
5129
5130         bnx2x_int_enable(bp);
5131
5132         /* Check for SPIO5 */
5133         bnx2x_attn_int_deasserted0(bp,
5134                 REG_RD(bp, MISC_REG_AEU_AFTER_INVERT_1_FUNC_0 + BP_PORT(bp)*4) &
5135                                    AEU_INPUTS_ATTN_BITS_SPIO5);
5136 }
5137
5138 /* end of nic init */
5139
5140 /*
5141  * gzip service functions
5142  */
5143
5144 static int bnx2x_gunzip_init(struct bnx2x *bp)
5145 {
5146         bp->gunzip_buf = dma_alloc_coherent(&bp->pdev->dev, FW_BUF_SIZE,
5147                                             &bp->gunzip_mapping, GFP_KERNEL);
5148         if (bp->gunzip_buf  == NULL)
5149                 goto gunzip_nomem1;
5150
5151         bp->strm = kmalloc(sizeof(*bp->strm), GFP_KERNEL);
5152         if (bp->strm  == NULL)
5153                 goto gunzip_nomem2;
5154
5155         bp->strm->workspace = kmalloc(zlib_inflate_workspacesize(),
5156                                       GFP_KERNEL);
5157         if (bp->strm->workspace == NULL)
5158                 goto gunzip_nomem3;
5159
5160         return 0;
5161
5162 gunzip_nomem3:
5163         kfree(bp->strm);
5164         bp->strm = NULL;
5165
5166 gunzip_nomem2:
5167         dma_free_coherent(&bp->pdev->dev, FW_BUF_SIZE, bp->gunzip_buf,
5168                           bp->gunzip_mapping);
5169         bp->gunzip_buf = NULL;
5170
5171 gunzip_nomem1:
5172         netdev_err(bp->dev, "Cannot allocate firmware buffer for"
5173                " un-compression\n");
5174         return -ENOMEM;
5175 }
5176
5177 static void bnx2x_gunzip_end(struct bnx2x *bp)
5178 {
5179         if (bp->strm) {
5180                 kfree(bp->strm->workspace);
5181                 kfree(bp->strm);
5182                 bp->strm = NULL;
5183         }
5184
5185         if (bp->gunzip_buf) {
5186                 dma_free_coherent(&bp->pdev->dev, FW_BUF_SIZE, bp->gunzip_buf,
5187                                   bp->gunzip_mapping);
5188                 bp->gunzip_buf = NULL;
5189         }
5190 }
5191
5192 static int bnx2x_gunzip(struct bnx2x *bp, const u8 *zbuf, int len)
5193 {
5194         int n, rc;
5195
5196         /* check gzip header */
5197         if ((zbuf[0] != 0x1f) || (zbuf[1] != 0x8b) || (zbuf[2] != Z_DEFLATED)) {
5198                 BNX2X_ERR("Bad gzip header\n");
5199                 return -EINVAL;
5200         }
5201
5202         n = 10;
5203
5204 #define FNAME                           0x8
5205
5206         if (zbuf[3] & FNAME)
5207                 while ((zbuf[n++] != 0) && (n < len));
5208
5209         bp->strm->next_in = (typeof(bp->strm->next_in))zbuf + n;
5210         bp->strm->avail_in = len - n;
5211         bp->strm->next_out = bp->gunzip_buf;
5212         bp->strm->avail_out = FW_BUF_SIZE;
5213
5214         rc = zlib_inflateInit2(bp->strm, -MAX_WBITS);
5215         if (rc != Z_OK)
5216                 return rc;
5217
5218         rc = zlib_inflate(bp->strm, Z_FINISH);
5219         if ((rc != Z_OK) && (rc != Z_STREAM_END))
5220                 netdev_err(bp->dev, "Firmware decompression error: %s\n",
5221                            bp->strm->msg);
5222
5223         bp->gunzip_outlen = (FW_BUF_SIZE - bp->strm->avail_out);
5224         if (bp->gunzip_outlen & 0x3)
5225                 netdev_err(bp->dev, "Firmware decompression error:"
5226                                     " gunzip_outlen (%d) not aligned\n",
5227                                 bp->gunzip_outlen);
5228         bp->gunzip_outlen >>= 2;
5229
5230         zlib_inflateEnd(bp->strm);
5231
5232         if (rc == Z_STREAM_END)
5233                 return 0;
5234
5235         return rc;
5236 }
5237
5238 /* nic load/unload */
5239
5240 /*
5241  * General service functions
5242  */
5243
5244 /* send a NIG loopback debug packet */
5245 static void bnx2x_lb_pckt(struct bnx2x *bp)
5246 {
5247         u32 wb_write[3];
5248
5249         /* Ethernet source and destination addresses */
5250         wb_write[0] = 0x55555555;
5251         wb_write[1] = 0x55555555;
5252         wb_write[2] = 0x20;             /* SOP */
5253         REG_WR_DMAE(bp, NIG_REG_DEBUG_PACKET_LB, wb_write, 3);
5254
5255         /* NON-IP protocol */
5256         wb_write[0] = 0x09000000;
5257         wb_write[1] = 0x55555555;
5258         wb_write[2] = 0x10;             /* EOP, eop_bvalid = 0 */
5259         REG_WR_DMAE(bp, NIG_REG_DEBUG_PACKET_LB, wb_write, 3);
5260 }
5261
5262 /* some of the internal memories
5263  * are not directly readable from the driver
5264  * to test them we send debug packets
5265  */
5266 static int bnx2x_int_mem_test(struct bnx2x *bp)
5267 {
5268         int factor;
5269         int count, i;
5270         u32 val = 0;
5271
5272         if (CHIP_REV_IS_FPGA(bp))
5273                 factor = 120;
5274         else if (CHIP_REV_IS_EMUL(bp))
5275                 factor = 200;
5276         else
5277                 factor = 1;
5278
5279         /* Disable inputs of parser neighbor blocks */
5280         REG_WR(bp, TSDM_REG_ENABLE_IN1, 0x0);
5281         REG_WR(bp, TCM_REG_PRS_IFEN, 0x0);
5282         REG_WR(bp, CFC_REG_DEBUG0, 0x1);
5283         REG_WR(bp, NIG_REG_PRS_REQ_IN_EN, 0x0);
5284
5285         /*  Write 0 to parser credits for CFC search request */
5286         REG_WR(bp, PRS_REG_CFC_SEARCH_INITIAL_CREDIT, 0x0);
5287
5288         /* send Ethernet packet */
5289         bnx2x_lb_pckt(bp);
5290
5291         /* TODO do i reset NIG statistic? */
5292         /* Wait until NIG register shows 1 packet of size 0x10 */
5293         count = 1000 * factor;
5294         while (count) {
5295
5296                 bnx2x_read_dmae(bp, NIG_REG_STAT2_BRB_OCTET, 2);
5297                 val = *bnx2x_sp(bp, wb_data[0]);
5298                 if (val == 0x10)
5299                         break;
5300
5301                 msleep(10);
5302                 count--;
5303         }
5304         if (val != 0x10) {
5305                 BNX2X_ERR("NIG timeout  val = 0x%x\n", val);
5306                 return -1;
5307         }
5308
5309         /* Wait until PRS register shows 1 packet */
5310         count = 1000 * factor;
5311         while (count) {
5312                 val = REG_RD(bp, PRS_REG_NUM_OF_PACKETS);
5313                 if (val == 1)
5314                         break;
5315
5316                 msleep(10);
5317                 count--;
5318         }
5319         if (val != 0x1) {
5320                 BNX2X_ERR("PRS timeout val = 0x%x\n", val);
5321                 return -2;
5322         }
5323
5324         /* Reset and init BRB, PRS */
5325         REG_WR(bp, GRCBASE_MISC + MISC_REGISTERS_RESET_REG_1_CLEAR, 0x03);
5326         msleep(50);
5327         REG_WR(bp, GRCBASE_MISC + MISC_REGISTERS_RESET_REG_1_SET, 0x03);
5328         msleep(50);
5329         bnx2x_init_block(bp, BLOCK_BRB1, PHASE_COMMON);
5330         bnx2x_init_block(bp, BLOCK_PRS, PHASE_COMMON);
5331
5332         DP(NETIF_MSG_HW, "part2\n");
5333
5334         /* Disable inputs of parser neighbor blocks */
5335         REG_WR(bp, TSDM_REG_ENABLE_IN1, 0x0);
5336         REG_WR(bp, TCM_REG_PRS_IFEN, 0x0);
5337         REG_WR(bp, CFC_REG_DEBUG0, 0x1);
5338         REG_WR(bp, NIG_REG_PRS_REQ_IN_EN, 0x0);
5339
5340         /* Write 0 to parser credits for CFC search request */
5341         REG_WR(bp, PRS_REG_CFC_SEARCH_INITIAL_CREDIT, 0x0);
5342
5343         /* send 10 Ethernet packets */
5344         for (i = 0; i < 10; i++)
5345                 bnx2x_lb_pckt(bp);
5346
5347         /* Wait until NIG register shows 10 + 1
5348            packets of size 11*0x10 = 0xb0 */
5349         count = 1000 * factor;
5350         while (count) {
5351
5352                 bnx2x_read_dmae(bp, NIG_REG_STAT2_BRB_OCTET, 2);
5353                 val = *bnx2x_sp(bp, wb_data[0]);
5354                 if (val == 0xb0)
5355                         break;
5356
5357                 msleep(10);
5358                 count--;
5359         }
5360         if (val != 0xb0) {
5361                 BNX2X_ERR("NIG timeout  val = 0x%x\n", val);
5362                 return -3;
5363         }
5364
5365         /* Wait until PRS register shows 2 packets */
5366         val = REG_RD(bp, PRS_REG_NUM_OF_PACKETS);
5367         if (val != 2)
5368                 BNX2X_ERR("PRS timeout  val = 0x%x\n", val);
5369
5370         /* Write 1 to parser credits for CFC search request */
5371         REG_WR(bp, PRS_REG_CFC_SEARCH_INITIAL_CREDIT, 0x1);
5372
5373         /* Wait until PRS register shows 3 packets */
5374         msleep(10 * factor);
5375         /* Wait until NIG register shows 1 packet of size 0x10 */
5376         val = REG_RD(bp, PRS_REG_NUM_OF_PACKETS);
5377         if (val != 3)
5378                 BNX2X_ERR("PRS timeout  val = 0x%x\n", val);
5379
5380         /* clear NIG EOP FIFO */
5381         for (i = 0; i < 11; i++)
5382                 REG_RD(bp, NIG_REG_INGRESS_EOP_LB_FIFO);
5383         val = REG_RD(bp, NIG_REG_INGRESS_EOP_LB_EMPTY);
5384         if (val != 1) {
5385                 BNX2X_ERR("clear of NIG failed\n");
5386                 return -4;
5387         }
5388
5389         /* Reset and init BRB, PRS, NIG */
5390         REG_WR(bp, GRCBASE_MISC + MISC_REGISTERS_RESET_REG_1_CLEAR, 0x03);
5391         msleep(50);
5392         REG_WR(bp, GRCBASE_MISC + MISC_REGISTERS_RESET_REG_1_SET, 0x03);
5393         msleep(50);
5394         bnx2x_init_block(bp, BLOCK_BRB1, PHASE_COMMON);
5395         bnx2x_init_block(bp, BLOCK_PRS, PHASE_COMMON);
5396 #ifndef BCM_CNIC
5397         /* set NIC mode */
5398         REG_WR(bp, PRS_REG_NIC_MODE, 1);
5399 #endif
5400
5401         /* Enable inputs of parser neighbor blocks */
5402         REG_WR(bp, TSDM_REG_ENABLE_IN1, 0x7fffffff);
5403         REG_WR(bp, TCM_REG_PRS_IFEN, 0x1);
5404         REG_WR(bp, CFC_REG_DEBUG0, 0x0);
5405         REG_WR(bp, NIG_REG_PRS_REQ_IN_EN, 0x1);
5406
5407         DP(NETIF_MSG_HW, "done\n");
5408
5409         return 0; /* OK */
5410 }
5411
5412 static void bnx2x_enable_blocks_attention(struct bnx2x *bp)
5413 {
5414         REG_WR(bp, PXP_REG_PXP_INT_MASK_0, 0);
5415         if (!CHIP_IS_E1x(bp))
5416                 REG_WR(bp, PXP_REG_PXP_INT_MASK_1, 0x40);
5417         else
5418                 REG_WR(bp, PXP_REG_PXP_INT_MASK_1, 0);
5419         REG_WR(bp, DORQ_REG_DORQ_INT_MASK, 0);
5420         REG_WR(bp, CFC_REG_CFC_INT_MASK, 0);
5421         /*
5422          * mask read length error interrupts in brb for parser
5423          * (parsing unit and 'checksum and crc' unit)
5424          * these errors are legal (PU reads fixed length and CAC can cause
5425          * read length error on truncated packets)
5426          */
5427         REG_WR(bp, BRB1_REG_BRB1_INT_MASK, 0xFC00);
5428         REG_WR(bp, QM_REG_QM_INT_MASK, 0);
5429         REG_WR(bp, TM_REG_TM_INT_MASK, 0);
5430         REG_WR(bp, XSDM_REG_XSDM_INT_MASK_0, 0);
5431         REG_WR(bp, XSDM_REG_XSDM_INT_MASK_1, 0);
5432         REG_WR(bp, XCM_REG_XCM_INT_MASK, 0);
5433 /*      REG_WR(bp, XSEM_REG_XSEM_INT_MASK_0, 0); */
5434 /*      REG_WR(bp, XSEM_REG_XSEM_INT_MASK_1, 0); */
5435         REG_WR(bp, USDM_REG_USDM_INT_MASK_0, 0);
5436         REG_WR(bp, USDM_REG_USDM_INT_MASK_1, 0);
5437         REG_WR(bp, UCM_REG_UCM_INT_MASK, 0);
5438 /*      REG_WR(bp, USEM_REG_USEM_INT_MASK_0, 0); */
5439 /*      REG_WR(bp, USEM_REG_USEM_INT_MASK_1, 0); */
5440         REG_WR(bp, GRCBASE_UPB + PB_REG_PB_INT_MASK, 0);
5441         REG_WR(bp, CSDM_REG_CSDM_INT_MASK_0, 0);
5442         REG_WR(bp, CSDM_REG_CSDM_INT_MASK_1, 0);
5443         REG_WR(bp, CCM_REG_CCM_INT_MASK, 0);
5444 /*      REG_WR(bp, CSEM_REG_CSEM_INT_MASK_0, 0); */
5445 /*      REG_WR(bp, CSEM_REG_CSEM_INT_MASK_1, 0); */
5446
5447         if (CHIP_REV_IS_FPGA(bp))
5448                 REG_WR(bp, PXP2_REG_PXP2_INT_MASK_0, 0x580000);
5449         else if (!CHIP_IS_E1x(bp))
5450                 REG_WR(bp, PXP2_REG_PXP2_INT_MASK_0,
5451                            (PXP2_PXP2_INT_MASK_0_REG_PGL_CPL_OF
5452                                 | PXP2_PXP2_INT_MASK_0_REG_PGL_CPL_AFT
5453                                 | PXP2_PXP2_INT_MASK_0_REG_PGL_PCIE_ATTN
5454                                 | PXP2_PXP2_INT_MASK_0_REG_PGL_READ_BLOCKED
5455                                 | PXP2_PXP2_INT_MASK_0_REG_PGL_WRITE_BLOCKED));
5456         else
5457                 REG_WR(bp, PXP2_REG_PXP2_INT_MASK_0, 0x480000);
5458         REG_WR(bp, TSDM_REG_TSDM_INT_MASK_0, 0);
5459         REG_WR(bp, TSDM_REG_TSDM_INT_MASK_1, 0);
5460         REG_WR(bp, TCM_REG_TCM_INT_MASK, 0);
5461 /*      REG_WR(bp, TSEM_REG_TSEM_INT_MASK_0, 0); */
5462
5463         if (!CHIP_IS_E1x(bp))
5464                 /* enable VFC attentions: bits 11 and 12, bits 31:13 reserved */
5465                 REG_WR(bp, TSEM_REG_TSEM_INT_MASK_1, 0x07ff);
5466
5467         REG_WR(bp, CDU_REG_CDU_INT_MASK, 0);
5468         REG_WR(bp, DMAE_REG_DMAE_INT_MASK, 0);
5469 /*      REG_WR(bp, MISC_REG_MISC_INT_MASK, 0); */
5470         REG_WR(bp, PBF_REG_PBF_INT_MASK, 0x18);         /* bit 3,4 masked */
5471 }
5472
5473 static void bnx2x_reset_common(struct bnx2x *bp)
5474 {
5475         u32 val = 0x1400;
5476
5477         /* reset_common */
5478         REG_WR(bp, GRCBASE_MISC + MISC_REGISTERS_RESET_REG_1_CLEAR,
5479                0xd3ffff7f);
5480
5481         if (CHIP_IS_E3(bp)) {
5482                 val |= MISC_REGISTERS_RESET_REG_2_MSTAT0;
5483                 val |= MISC_REGISTERS_RESET_REG_2_MSTAT1;
5484         }
5485
5486         REG_WR(bp, GRCBASE_MISC + MISC_REGISTERS_RESET_REG_2_CLEAR, val);
5487 }
5488
5489 static void bnx2x_setup_dmae(struct bnx2x *bp)
5490 {
5491         bp->dmae_ready = 0;
5492         spin_lock_init(&bp->dmae_lock);
5493 }
5494
5495 static void bnx2x_init_pxp(struct bnx2x *bp)
5496 {
5497         u16 devctl;
5498         int r_order, w_order;
5499
5500         pci_read_config_word(bp->pdev,
5501                              bp->pcie_cap + PCI_EXP_DEVCTL, &devctl);
5502         DP(NETIF_MSG_HW, "read 0x%x from devctl\n", devctl);
5503         w_order = ((devctl & PCI_EXP_DEVCTL_PAYLOAD) >> 5);
5504         if (bp->mrrs == -1)
5505                 r_order = ((devctl & PCI_EXP_DEVCTL_READRQ) >> 12);
5506         else {
5507                 DP(NETIF_MSG_HW, "force read order to %d\n", bp->mrrs);
5508                 r_order = bp->mrrs;
5509         }
5510
5511         bnx2x_init_pxp_arb(bp, r_order, w_order);
5512 }
5513
5514 static void bnx2x_setup_fan_failure_detection(struct bnx2x *bp)
5515 {
5516         int is_required;
5517         u32 val;
5518         int port;
5519
5520         if (BP_NOMCP(bp))
5521                 return;
5522
5523         is_required = 0;
5524         val = SHMEM_RD(bp, dev_info.shared_hw_config.config2) &
5525               SHARED_HW_CFG_FAN_FAILURE_MASK;
5526
5527         if (val == SHARED_HW_CFG_FAN_FAILURE_ENABLED)
5528                 is_required = 1;
5529
5530         /*
5531          * The fan failure mechanism is usually related to the PHY type since
5532          * the power consumption of the board is affected by the PHY. Currently,
5533          * fan is required for most designs with SFX7101, BCM8727 and BCM8481.
5534          */
5535         else if (val == SHARED_HW_CFG_FAN_FAILURE_PHY_TYPE)
5536                 for (port = PORT_0; port < PORT_MAX; port++) {
5537                         is_required |=
5538                                 bnx2x_fan_failure_det_req(
5539                                         bp,
5540                                         bp->common.shmem_base,
5541                                         bp->common.shmem2_base,
5542                                         port);
5543                 }
5544
5545         DP(NETIF_MSG_HW, "fan detection setting: %d\n", is_required);
5546
5547         if (is_required == 0)
5548                 return;
5549
5550         /* Fan failure is indicated by SPIO 5 */
5551         bnx2x_set_spio(bp, MISC_REGISTERS_SPIO_5,
5552                        MISC_REGISTERS_SPIO_INPUT_HI_Z);
5553
5554         /* set to active low mode */
5555         val = REG_RD(bp, MISC_REG_SPIO_INT);
5556         val |= ((1 << MISC_REGISTERS_SPIO_5) <<
5557                                         MISC_REGISTERS_SPIO_INT_OLD_SET_POS);
5558         REG_WR(bp, MISC_REG_SPIO_INT, val);
5559
5560         /* enable interrupt to signal the IGU */
5561         val = REG_RD(bp, MISC_REG_SPIO_EVENT_EN);
5562         val |= (1 << MISC_REGISTERS_SPIO_5);
5563         REG_WR(bp, MISC_REG_SPIO_EVENT_EN, val);
5564 }
5565
5566 static void bnx2x_pretend_func(struct bnx2x *bp, u8 pretend_func_num)
5567 {
5568         u32 offset = 0;
5569
5570         if (CHIP_IS_E1(bp))
5571                 return;
5572         if (CHIP_IS_E1H(bp) && (pretend_func_num >= E1H_FUNC_MAX))
5573                 return;
5574
5575         switch (BP_ABS_FUNC(bp)) {
5576         case 0:
5577                 offset = PXP2_REG_PGL_PRETEND_FUNC_F0;
5578                 break;
5579         case 1:
5580                 offset = PXP2_REG_PGL_PRETEND_FUNC_F1;
5581                 break;
5582         case 2:
5583                 offset = PXP2_REG_PGL_PRETEND_FUNC_F2;
5584                 break;
5585         case 3:
5586                 offset = PXP2_REG_PGL_PRETEND_FUNC_F3;
5587                 break;
5588         case 4:
5589                 offset = PXP2_REG_PGL_PRETEND_FUNC_F4;
5590                 break;
5591         case 5:
5592                 offset = PXP2_REG_PGL_PRETEND_FUNC_F5;
5593                 break;
5594         case 6:
5595                 offset = PXP2_REG_PGL_PRETEND_FUNC_F6;
5596                 break;
5597         case 7:
5598                 offset = PXP2_REG_PGL_PRETEND_FUNC_F7;
5599                 break;
5600         default:
5601                 return;
5602         }
5603
5604         REG_WR(bp, offset, pretend_func_num);
5605         REG_RD(bp, offset);
5606         DP(NETIF_MSG_HW, "Pretending to func %d\n", pretend_func_num);
5607 }
5608
5609 void bnx2x_pf_disable(struct bnx2x *bp)
5610 {
5611         u32 val = REG_RD(bp, IGU_REG_PF_CONFIGURATION);
5612         val &= ~IGU_PF_CONF_FUNC_EN;
5613
5614         REG_WR(bp, IGU_REG_PF_CONFIGURATION, val);
5615         REG_WR(bp, PGLUE_B_REG_INTERNAL_PFID_ENABLE_MASTER, 0);
5616         REG_WR(bp, CFC_REG_WEAK_ENABLE_PF, 0);
5617 }
5618
5619 static inline void bnx2x__common_init_phy(struct bnx2x *bp)
5620 {
5621         u32 shmem_base[2], shmem2_base[2];
5622         shmem_base[0] =  bp->common.shmem_base;
5623         shmem2_base[0] = bp->common.shmem2_base;
5624         if (!CHIP_IS_E1x(bp)) {
5625                 shmem_base[1] =
5626                         SHMEM2_RD(bp, other_shmem_base_addr);
5627                 shmem2_base[1] =
5628                         SHMEM2_RD(bp, other_shmem2_base_addr);
5629         }
5630         bnx2x_acquire_phy_lock(bp);
5631         bnx2x_common_init_phy(bp, shmem_base, shmem2_base,
5632                               bp->common.chip_id);
5633         bnx2x_release_phy_lock(bp);
5634 }
5635
5636 /**
5637  * bnx2x_init_hw_common - initialize the HW at the COMMON phase.
5638  *
5639  * @bp:         driver handle
5640  */
5641 static int bnx2x_init_hw_common(struct bnx2x *bp)
5642 {
5643         u32 val;
5644
5645         DP(BNX2X_MSG_MCP, "starting common init  func %d\n", BP_ABS_FUNC(bp));
5646
5647         bnx2x_reset_common(bp);
5648         REG_WR(bp, GRCBASE_MISC + MISC_REGISTERS_RESET_REG_1_SET, 0xffffffff);
5649
5650         val = 0xfffc;
5651         if (CHIP_IS_E3(bp)) {
5652                 val |= MISC_REGISTERS_RESET_REG_2_MSTAT0;
5653                 val |= MISC_REGISTERS_RESET_REG_2_MSTAT1;
5654         }
5655         REG_WR(bp, GRCBASE_MISC + MISC_REGISTERS_RESET_REG_2_SET, val);
5656
5657         bnx2x_init_block(bp, BLOCK_MISC, PHASE_COMMON);
5658
5659         if (!CHIP_IS_E1x(bp)) {
5660                 u8 abs_func_id;
5661
5662                 /**
5663                  * 4-port mode or 2-port mode we need to turn of master-enable
5664                  * for everyone, after that, turn it back on for self.
5665                  * so, we disregard multi-function or not, and always disable
5666                  * for all functions on the given path, this means 0,2,4,6 for
5667                  * path 0 and 1,3,5,7 for path 1
5668                  */
5669                 for (abs_func_id = BP_PATH(bp);
5670                      abs_func_id < E2_FUNC_MAX*2; abs_func_id += 2) {
5671                         if (abs_func_id == BP_ABS_FUNC(bp)) {
5672                                 REG_WR(bp,
5673                                     PGLUE_B_REG_INTERNAL_PFID_ENABLE_MASTER,
5674                                     1);
5675                                 continue;
5676                         }
5677
5678                         bnx2x_pretend_func(bp, abs_func_id);
5679                         /* clear pf enable */
5680                         bnx2x_pf_disable(bp);
5681                         bnx2x_pretend_func(bp, BP_ABS_FUNC(bp));
5682                 }
5683         }
5684
5685         bnx2x_init_block(bp, BLOCK_PXP, PHASE_COMMON);
5686         if (CHIP_IS_E1(bp)) {
5687                 /* enable HW interrupt from PXP on USDM overflow
5688                    bit 16 on INT_MASK_0 */
5689                 REG_WR(bp, PXP_REG_PXP_INT_MASK_0, 0);
5690         }
5691
5692         bnx2x_init_block(bp, BLOCK_PXP2, PHASE_COMMON);
5693         bnx2x_init_pxp(bp);
5694
5695 #ifdef __BIG_ENDIAN
5696         REG_WR(bp, PXP2_REG_RQ_QM_ENDIAN_M, 1);
5697         REG_WR(bp, PXP2_REG_RQ_TM_ENDIAN_M, 1);
5698         REG_WR(bp, PXP2_REG_RQ_SRC_ENDIAN_M, 1);
5699         REG_WR(bp, PXP2_REG_RQ_CDU_ENDIAN_M, 1);
5700         REG_WR(bp, PXP2_REG_RQ_DBG_ENDIAN_M, 1);
5701         /* make sure this value is 0 */
5702         REG_WR(bp, PXP2_REG_RQ_HC_ENDIAN_M, 0);
5703
5704 /*      REG_WR(bp, PXP2_REG_RD_PBF_SWAP_MODE, 1); */
5705         REG_WR(bp, PXP2_REG_RD_QM_SWAP_MODE, 1);
5706         REG_WR(bp, PXP2_REG_RD_TM_SWAP_MODE, 1);
5707         REG_WR(bp, PXP2_REG_RD_SRC_SWAP_MODE, 1);
5708         REG_WR(bp, PXP2_REG_RD_CDURD_SWAP_MODE, 1);
5709 #endif
5710
5711         bnx2x_ilt_init_page_size(bp, INITOP_SET);
5712
5713         if (CHIP_REV_IS_FPGA(bp) && CHIP_IS_E1H(bp))
5714                 REG_WR(bp, PXP2_REG_PGL_TAGS_LIMIT, 0x1);
5715
5716         /* let the HW do it's magic ... */
5717         msleep(100);
5718         /* finish PXP init */
5719         val = REG_RD(bp, PXP2_REG_RQ_CFG_DONE);
5720         if (val != 1) {
5721                 BNX2X_ERR("PXP2 CFG failed\n");
5722                 return -EBUSY;
5723         }
5724         val = REG_RD(bp, PXP2_REG_RD_INIT_DONE);
5725         if (val != 1) {
5726                 BNX2X_ERR("PXP2 RD_INIT failed\n");
5727                 return -EBUSY;
5728         }
5729
5730         /* Timers bug workaround E2 only. We need to set the entire ILT to
5731          * have entries with value "0" and valid bit on.
5732          * This needs to be done by the first PF that is loaded in a path
5733          * (i.e. common phase)
5734          */
5735         if (!CHIP_IS_E1x(bp)) {
5736 /* In E2 there is a bug in the timers block that can cause function 6 / 7
5737  * (i.e. vnic3) to start even if it is marked as "scan-off".
5738  * This occurs when a different function (func2,3) is being marked
5739  * as "scan-off". Real-life scenario for example: if a driver is being
5740  * load-unloaded while func6,7 are down. This will cause the timer to access
5741  * the ilt, translate to a logical address and send a request to read/write.
5742  * Since the ilt for the function that is down is not valid, this will cause
5743  * a translation error which is unrecoverable.
5744  * The Workaround is intended to make sure that when this happens nothing fatal
5745  * will occur. The workaround:
5746  *      1.  First PF driver which loads on a path will:
5747  *              a.  After taking the chip out of reset, by using pretend,
5748  *                  it will write "0" to the following registers of
5749  *                  the other vnics.
5750  *                  REG_WR(pdev, PGLUE_B_REG_INTERNAL_PFID_ENABLE_MASTER, 0);
5751  *                  REG_WR(pdev, CFC_REG_WEAK_ENABLE_PF,0);
5752  *                  REG_WR(pdev, CFC_REG_STRONG_ENABLE_PF,0);
5753  *                  And for itself it will write '1' to
5754  *                  PGLUE_B_REG_INTERNAL_PFID_ENABLE_MASTER to enable
5755  *                  dmae-operations (writing to pram for example.)
5756  *                  note: can be done for only function 6,7 but cleaner this
5757  *                        way.
5758  *              b.  Write zero+valid to the entire ILT.
5759  *              c.  Init the first_timers_ilt_entry, last_timers_ilt_entry of
5760  *                  VNIC3 (of that port). The range allocated will be the
5761  *                  entire ILT. This is needed to prevent  ILT range error.
5762  *      2.  Any PF driver load flow:
5763  *              a.  ILT update with the physical addresses of the allocated
5764  *                  logical pages.
5765  *              b.  Wait 20msec. - note that this timeout is needed to make
5766  *                  sure there are no requests in one of the PXP internal
5767  *                  queues with "old" ILT addresses.
5768  *              c.  PF enable in the PGLC.
5769  *              d.  Clear the was_error of the PF in the PGLC. (could have
5770  *                  occured while driver was down)
5771  *              e.  PF enable in the CFC (WEAK + STRONG)
5772  *              f.  Timers scan enable
5773  *      3.  PF driver unload flow:
5774  *              a.  Clear the Timers scan_en.
5775  *              b.  Polling for scan_on=0 for that PF.
5776  *              c.  Clear the PF enable bit in the PXP.
5777  *              d.  Clear the PF enable in the CFC (WEAK + STRONG)
5778  *              e.  Write zero+valid to all ILT entries (The valid bit must
5779  *                  stay set)
5780  *              f.  If this is VNIC 3 of a port then also init
5781  *                  first_timers_ilt_entry to zero and last_timers_ilt_entry
5782  *                  to the last enrty in the ILT.
5783  *
5784  *      Notes:
5785  *      Currently the PF error in the PGLC is non recoverable.
5786  *      In the future the there will be a recovery routine for this error.
5787  *      Currently attention is masked.
5788  *      Having an MCP lock on the load/unload process does not guarantee that
5789  *      there is no Timer disable during Func6/7 enable. This is because the
5790  *      Timers scan is currently being cleared by the MCP on FLR.
5791  *      Step 2.d can be done only for PF6/7 and the driver can also check if
5792  *      there is error before clearing it. But the flow above is simpler and
5793  *      more general.
5794  *      All ILT entries are written by zero+valid and not just PF6/7
5795  *      ILT entries since in the future the ILT entries allocation for
5796  *      PF-s might be dynamic.
5797  */
5798                 struct ilt_client_info ilt_cli;
5799                 struct bnx2x_ilt ilt;
5800                 memset(&ilt_cli, 0, sizeof(struct ilt_client_info));
5801                 memset(&ilt, 0, sizeof(struct bnx2x_ilt));
5802
5803                 /* initialize dummy TM client */
5804                 ilt_cli.start = 0;
5805                 ilt_cli.end = ILT_NUM_PAGE_ENTRIES - 1;
5806                 ilt_cli.client_num = ILT_CLIENT_TM;
5807
5808                 /* Step 1: set zeroes to all ilt page entries with valid bit on
5809                  * Step 2: set the timers first/last ilt entry to point
5810                  * to the entire range to prevent ILT range error for 3rd/4th
5811                  * vnic (this code assumes existance of the vnic)
5812                  *
5813                  * both steps performed by call to bnx2x_ilt_client_init_op()
5814                  * with dummy TM client
5815                  *
5816                  * we must use pretend since PXP2_REG_RQ_##blk##_FIRST_ILT
5817                  * and his brother are split registers
5818                  */
5819                 bnx2x_pretend_func(bp, (BP_PATH(bp) + 6));
5820                 bnx2x_ilt_client_init_op_ilt(bp, &ilt, &ilt_cli, INITOP_CLEAR);
5821                 bnx2x_pretend_func(bp, BP_ABS_FUNC(bp));
5822
5823                 REG_WR(bp, PXP2_REG_RQ_DRAM_ALIGN, BNX2X_PXP_DRAM_ALIGN);
5824                 REG_WR(bp, PXP2_REG_RQ_DRAM_ALIGN_RD, BNX2X_PXP_DRAM_ALIGN);
5825                 REG_WR(bp, PXP2_REG_RQ_DRAM_ALIGN_SEL, 1);
5826         }
5827
5828
5829         REG_WR(bp, PXP2_REG_RQ_DISABLE_INPUTS, 0);
5830         REG_WR(bp, PXP2_REG_RD_DISABLE_INPUTS, 0);
5831
5832         if (!CHIP_IS_E1x(bp)) {
5833                 int factor = CHIP_REV_IS_EMUL(bp) ? 1000 :
5834                                 (CHIP_REV_IS_FPGA(bp) ? 400 : 0);
5835                 bnx2x_init_block(bp, BLOCK_PGLUE_B, PHASE_COMMON);
5836
5837                 bnx2x_init_block(bp, BLOCK_ATC, PHASE_COMMON);
5838
5839                 /* let the HW do it's magic ... */
5840                 do {
5841                         msleep(200);
5842                         val = REG_RD(bp, ATC_REG_ATC_INIT_DONE);
5843                 } while (factor-- && (val != 1));
5844
5845                 if (val != 1) {
5846                         BNX2X_ERR("ATC_INIT failed\n");
5847                         return -EBUSY;
5848                 }
5849         }
5850
5851         bnx2x_init_block(bp, BLOCK_DMAE, PHASE_COMMON);
5852
5853         /* clean the DMAE memory */
5854         bp->dmae_ready = 1;
5855         bnx2x_init_fill(bp, TSEM_REG_PRAM, 0, 8, 1);
5856
5857         bnx2x_init_block(bp, BLOCK_TCM, PHASE_COMMON);
5858
5859         bnx2x_init_block(bp, BLOCK_UCM, PHASE_COMMON);
5860
5861         bnx2x_init_block(bp, BLOCK_CCM, PHASE_COMMON);
5862
5863         bnx2x_init_block(bp, BLOCK_XCM, PHASE_COMMON);
5864
5865         bnx2x_read_dmae(bp, XSEM_REG_PASSIVE_BUFFER, 3);
5866         bnx2x_read_dmae(bp, CSEM_REG_PASSIVE_BUFFER, 3);
5867         bnx2x_read_dmae(bp, TSEM_REG_PASSIVE_BUFFER, 3);
5868         bnx2x_read_dmae(bp, USEM_REG_PASSIVE_BUFFER, 3);
5869
5870         bnx2x_init_block(bp, BLOCK_QM, PHASE_COMMON);
5871
5872
5873         /* QM queues pointers table */
5874         bnx2x_qm_init_ptr_table(bp, bp->qm_cid_count, INITOP_SET);
5875
5876         /* soft reset pulse */
5877         REG_WR(bp, QM_REG_SOFT_RESET, 1);
5878         REG_WR(bp, QM_REG_SOFT_RESET, 0);
5879
5880 #ifdef BCM_CNIC
5881         bnx2x_init_block(bp, BLOCK_TM, PHASE_COMMON);
5882 #endif
5883
5884         bnx2x_init_block(bp, BLOCK_DORQ, PHASE_COMMON);
5885         REG_WR(bp, DORQ_REG_DPM_CID_OFST, BNX2X_DB_SHIFT);
5886         if (!CHIP_REV_IS_SLOW(bp))
5887                 /* enable hw interrupt from doorbell Q */
5888                 REG_WR(bp, DORQ_REG_DORQ_INT_MASK, 0);
5889
5890         bnx2x_init_block(bp, BLOCK_BRB1, PHASE_COMMON);
5891
5892         bnx2x_init_block(bp, BLOCK_PRS, PHASE_COMMON);
5893         REG_WR(bp, PRS_REG_A_PRSU_20, 0xf);
5894
5895         if (!CHIP_IS_E1(bp))
5896                 REG_WR(bp, PRS_REG_E1HOV_MODE, bp->path_has_ovlan);
5897
5898         if (!CHIP_IS_E1x(bp) && !CHIP_IS_E3B0(bp))
5899                 /* Bit-map indicating which L2 hdrs may appear
5900                  * after the basic Ethernet header
5901                  */
5902                 REG_WR(bp, PRS_REG_HDRS_AFTER_BASIC,
5903                        bp->path_has_ovlan ? 7 : 6);
5904
5905         bnx2x_init_block(bp, BLOCK_TSDM, PHASE_COMMON);
5906         bnx2x_init_block(bp, BLOCK_CSDM, PHASE_COMMON);
5907         bnx2x_init_block(bp, BLOCK_USDM, PHASE_COMMON);
5908         bnx2x_init_block(bp, BLOCK_XSDM, PHASE_COMMON);
5909
5910         if (!CHIP_IS_E1x(bp)) {
5911                 /* reset VFC memories */
5912                 REG_WR(bp, TSEM_REG_FAST_MEMORY + VFC_REG_MEMORIES_RST,
5913                            VFC_MEMORIES_RST_REG_CAM_RST |
5914                            VFC_MEMORIES_RST_REG_RAM_RST);
5915                 REG_WR(bp, XSEM_REG_FAST_MEMORY + VFC_REG_MEMORIES_RST,
5916                            VFC_MEMORIES_RST_REG_CAM_RST |
5917                            VFC_MEMORIES_RST_REG_RAM_RST);
5918
5919                 msleep(20);
5920         }
5921
5922         bnx2x_init_block(bp, BLOCK_TSEM, PHASE_COMMON);
5923         bnx2x_init_block(bp, BLOCK_USEM, PHASE_COMMON);
5924         bnx2x_init_block(bp, BLOCK_CSEM, PHASE_COMMON);
5925         bnx2x_init_block(bp, BLOCK_XSEM, PHASE_COMMON);
5926
5927         /* sync semi rtc */
5928         REG_WR(bp, GRCBASE_MISC + MISC_REGISTERS_RESET_REG_1_CLEAR,
5929                0x80000000);
5930         REG_WR(bp, GRCBASE_MISC + MISC_REGISTERS_RESET_REG_1_SET,
5931                0x80000000);
5932
5933         bnx2x_init_block(bp, BLOCK_UPB, PHASE_COMMON);
5934         bnx2x_init_block(bp, BLOCK_XPB, PHASE_COMMON);
5935         bnx2x_init_block(bp, BLOCK_PBF, PHASE_COMMON);
5936
5937         if (!CHIP_IS_E1x(bp))
5938                 REG_WR(bp, PBF_REG_HDRS_AFTER_BASIC,
5939                        bp->path_has_ovlan ? 7 : 6);
5940
5941         REG_WR(bp, SRC_REG_SOFT_RST, 1);
5942
5943         bnx2x_init_block(bp, BLOCK_SRC, PHASE_COMMON);
5944
5945 #ifdef BCM_CNIC
5946         REG_WR(bp, SRC_REG_KEYSEARCH_0, 0x63285672);
5947         REG_WR(bp, SRC_REG_KEYSEARCH_1, 0x24b8f2cc);
5948         REG_WR(bp, SRC_REG_KEYSEARCH_2, 0x223aef9b);
5949         REG_WR(bp, SRC_REG_KEYSEARCH_3, 0x26001e3a);
5950         REG_WR(bp, SRC_REG_KEYSEARCH_4, 0x7ae91116);
5951         REG_WR(bp, SRC_REG_KEYSEARCH_5, 0x5ce5230b);
5952         REG_WR(bp, SRC_REG_KEYSEARCH_6, 0x298d8adf);
5953         REG_WR(bp, SRC_REG_KEYSEARCH_7, 0x6eb0ff09);
5954         REG_WR(bp, SRC_REG_KEYSEARCH_8, 0x1830f82f);
5955         REG_WR(bp, SRC_REG_KEYSEARCH_9, 0x01e46be7);
5956 #endif
5957         REG_WR(bp, SRC_REG_SOFT_RST, 0);
5958
5959         if (sizeof(union cdu_context) != 1024)
5960                 /* we currently assume that a context is 1024 bytes */
5961                 dev_alert(&bp->pdev->dev, "please adjust the size "
5962                                           "of cdu_context(%ld)\n",
5963                          (long)sizeof(union cdu_context));
5964
5965         bnx2x_init_block(bp, BLOCK_CDU, PHASE_COMMON);
5966         val = (4 << 24) + (0 << 12) + 1024;
5967         REG_WR(bp, CDU_REG_CDU_GLOBAL_PARAMS, val);
5968
5969         bnx2x_init_block(bp, BLOCK_CFC, PHASE_COMMON);
5970         REG_WR(bp, CFC_REG_INIT_REG, 0x7FF);
5971         /* enable context validation interrupt from CFC */
5972         REG_WR(bp, CFC_REG_CFC_INT_MASK, 0);
5973
5974         /* set the thresholds to prevent CFC/CDU race */
5975         REG_WR(bp, CFC_REG_DEBUG0, 0x20020000);
5976
5977         bnx2x_init_block(bp, BLOCK_HC, PHASE_COMMON);
5978
5979         if (!CHIP_IS_E1x(bp) && BP_NOMCP(bp))
5980                 REG_WR(bp, IGU_REG_RESET_MEMORIES, 0x36);
5981
5982         bnx2x_init_block(bp, BLOCK_IGU, PHASE_COMMON);
5983         bnx2x_init_block(bp, BLOCK_MISC_AEU, PHASE_COMMON);
5984
5985         /* Reset PCIE errors for debug */
5986         REG_WR(bp, 0x2814, 0xffffffff);
5987         REG_WR(bp, 0x3820, 0xffffffff);
5988
5989         if (!CHIP_IS_E1x(bp)) {
5990                 REG_WR(bp, PCICFG_OFFSET + PXPCS_TL_CONTROL_5,
5991                            (PXPCS_TL_CONTROL_5_ERR_UNSPPORT1 |
5992                                 PXPCS_TL_CONTROL_5_ERR_UNSPPORT));
5993                 REG_WR(bp, PCICFG_OFFSET + PXPCS_TL_FUNC345_STAT,
5994                            (PXPCS_TL_FUNC345_STAT_ERR_UNSPPORT4 |
5995                                 PXPCS_TL_FUNC345_STAT_ERR_UNSPPORT3 |
5996                                 PXPCS_TL_FUNC345_STAT_ERR_UNSPPORT2));
5997                 REG_WR(bp, PCICFG_OFFSET + PXPCS_TL_FUNC678_STAT,
5998                            (PXPCS_TL_FUNC678_STAT_ERR_UNSPPORT7 |
5999                                 PXPCS_TL_FUNC678_STAT_ERR_UNSPPORT6 |
6000                                 PXPCS_TL_FUNC678_STAT_ERR_UNSPPORT5));
6001         }
6002
6003         bnx2x_init_block(bp, BLOCK_NIG, PHASE_COMMON);
6004         if (!CHIP_IS_E1(bp)) {
6005                 /* in E3 this done in per-port section */
6006                 if (!CHIP_IS_E3(bp))
6007                         REG_WR(bp, NIG_REG_LLH_MF_MODE, IS_MF(bp));
6008         }
6009         if (CHIP_IS_E1H(bp))
6010                 /* not applicable for E2 (and above ...) */
6011                 REG_WR(bp, NIG_REG_LLH_E1HOV_MODE, IS_MF_SD(bp));
6012
6013         if (CHIP_REV_IS_SLOW(bp))
6014                 msleep(200);
6015
6016         /* finish CFC init */
6017         val = reg_poll(bp, CFC_REG_LL_INIT_DONE, 1, 100, 10);
6018         if (val != 1) {
6019                 BNX2X_ERR("CFC LL_INIT failed\n");
6020                 return -EBUSY;
6021         }
6022         val = reg_poll(bp, CFC_REG_AC_INIT_DONE, 1, 100, 10);
6023         if (val != 1) {
6024                 BNX2X_ERR("CFC AC_INIT failed\n");
6025                 return -EBUSY;
6026         }
6027         val = reg_poll(bp, CFC_REG_CAM_INIT_DONE, 1, 100, 10);
6028         if (val != 1) {
6029                 BNX2X_ERR("CFC CAM_INIT failed\n");
6030                 return -EBUSY;
6031         }
6032         REG_WR(bp, CFC_REG_DEBUG0, 0);
6033
6034         if (CHIP_IS_E1(bp)) {
6035                 /* read NIG statistic
6036                    to see if this is our first up since powerup */
6037                 bnx2x_read_dmae(bp, NIG_REG_STAT2_BRB_OCTET, 2);
6038                 val = *bnx2x_sp(bp, wb_data[0]);
6039
6040                 /* do internal memory self test */
6041                 if ((val == 0) && bnx2x_int_mem_test(bp)) {
6042                         BNX2X_ERR("internal mem self test failed\n");
6043                         return -EBUSY;
6044                 }
6045         }
6046
6047         bnx2x_setup_fan_failure_detection(bp);
6048
6049         /* clear PXP2 attentions */
6050         REG_RD(bp, PXP2_REG_PXP2_INT_STS_CLR_0);
6051
6052         bnx2x_enable_blocks_attention(bp);
6053         bnx2x_enable_blocks_parity(bp);
6054
6055         if (!BP_NOMCP(bp)) {
6056                 if (CHIP_IS_E1x(bp))
6057                         bnx2x__common_init_phy(bp);
6058         } else
6059                 BNX2X_ERR("Bootcode is missing - can not initialize link\n");
6060
6061         return 0;
6062 }
6063
6064 /**
6065  * bnx2x_init_hw_common_chip - init HW at the COMMON_CHIP phase.
6066  *
6067  * @bp:         driver handle
6068  */
6069 static int bnx2x_init_hw_common_chip(struct bnx2x *bp)
6070 {
6071         int rc = bnx2x_init_hw_common(bp);
6072
6073         if (rc)
6074                 return rc;
6075
6076         /* In E2 2-PORT mode, same ext phy is used for the two paths */
6077         if (!BP_NOMCP(bp))
6078                 bnx2x__common_init_phy(bp);
6079
6080         return 0;
6081 }
6082
6083 static int bnx2x_init_hw_port(struct bnx2x *bp)
6084 {
6085         int port = BP_PORT(bp);
6086         int init_phase = port ? PHASE_PORT1 : PHASE_PORT0;
6087         u32 low, high;
6088         u32 val;
6089
6090         bnx2x__link_reset(bp);
6091
6092         DP(BNX2X_MSG_MCP, "starting port init  port %d\n", port);
6093
6094         REG_WR(bp, NIG_REG_MASK_INTERRUPT_PORT0 + port*4, 0);
6095
6096         bnx2x_init_block(bp, BLOCK_MISC, init_phase);
6097         bnx2x_init_block(bp, BLOCK_PXP, init_phase);
6098         bnx2x_init_block(bp, BLOCK_PXP2, init_phase);
6099
6100         /* Timers bug workaround: disables the pf_master bit in pglue at
6101          * common phase, we need to enable it here before any dmae access are
6102          * attempted. Therefore we manually added the enable-master to the
6103          * port phase (it also happens in the function phase)
6104          */
6105         if (!CHIP_IS_E1x(bp))
6106                 REG_WR(bp, PGLUE_B_REG_INTERNAL_PFID_ENABLE_MASTER, 1);
6107
6108         bnx2x_init_block(bp, BLOCK_ATC, init_phase);
6109         bnx2x_init_block(bp, BLOCK_DMAE, init_phase);
6110         bnx2x_init_block(bp, BLOCK_PGLUE_B, init_phase);
6111         bnx2x_init_block(bp, BLOCK_QM, init_phase);
6112
6113         bnx2x_init_block(bp, BLOCK_TCM, init_phase);
6114         bnx2x_init_block(bp, BLOCK_UCM, init_phase);
6115         bnx2x_init_block(bp, BLOCK_CCM, init_phase);
6116         bnx2x_init_block(bp, BLOCK_XCM, init_phase);
6117
6118         /* QM cid (connection) count */
6119         bnx2x_qm_init_cid_count(bp, bp->qm_cid_count, INITOP_SET);
6120
6121 #ifdef BCM_CNIC
6122         bnx2x_init_block(bp, BLOCK_TM, init_phase);
6123         REG_WR(bp, TM_REG_LIN0_SCAN_TIME + port*4, 20);
6124         REG_WR(bp, TM_REG_LIN0_MAX_ACTIVE_CID + port*4, 31);
6125 #endif
6126
6127         bnx2x_init_block(bp, BLOCK_DORQ, init_phase);
6128
6129         if (CHIP_IS_E1(bp) || CHIP_IS_E1H(bp)) {
6130                 bnx2x_init_block(bp, BLOCK_BRB1, init_phase);
6131
6132                 if (IS_MF(bp))
6133                         low = ((bp->flags & ONE_PORT_FLAG) ? 160 : 246);
6134                 else if (bp->dev->mtu > 4096) {
6135                         if (bp->flags & ONE_PORT_FLAG)
6136                                 low = 160;
6137                         else {
6138                                 val = bp->dev->mtu;
6139                                 /* (24*1024 + val*4)/256 */
6140                                 low = 96 + (val/64) +
6141                                                 ((val % 64) ? 1 : 0);
6142                         }
6143                 } else
6144                         low = ((bp->flags & ONE_PORT_FLAG) ? 80 : 160);
6145                 high = low + 56;        /* 14*1024/256 */
6146                 REG_WR(bp, BRB1_REG_PAUSE_LOW_THRESHOLD_0 + port*4, low);
6147                 REG_WR(bp, BRB1_REG_PAUSE_HIGH_THRESHOLD_0 + port*4, high);
6148         }
6149
6150         if (CHIP_MODE_IS_4_PORT(bp))
6151                 REG_WR(bp, (BP_PORT(bp) ?
6152                             BRB1_REG_MAC_GUARANTIED_1 :
6153                             BRB1_REG_MAC_GUARANTIED_0), 40);
6154
6155
6156         bnx2x_init_block(bp, BLOCK_PRS, init_phase);
6157         if (CHIP_IS_E3B0(bp))
6158                 /* Ovlan exists only if we are in multi-function +
6159                  * switch-dependent mode, in switch-independent there
6160                  * is no ovlan headers
6161                  */
6162                 REG_WR(bp, BP_PORT(bp) ?
6163                        PRS_REG_HDRS_AFTER_BASIC_PORT_1 :
6164                        PRS_REG_HDRS_AFTER_BASIC_PORT_0,
6165                        (bp->path_has_ovlan ? 7 : 6));
6166
6167         bnx2x_init_block(bp, BLOCK_TSDM, init_phase);
6168         bnx2x_init_block(bp, BLOCK_CSDM, init_phase);
6169         bnx2x_init_block(bp, BLOCK_USDM, init_phase);
6170         bnx2x_init_block(bp, BLOCK_XSDM, init_phase);
6171
6172         bnx2x_init_block(bp, BLOCK_TSEM, init_phase);
6173         bnx2x_init_block(bp, BLOCK_USEM, init_phase);
6174         bnx2x_init_block(bp, BLOCK_CSEM, init_phase);
6175         bnx2x_init_block(bp, BLOCK_XSEM, init_phase);
6176
6177         bnx2x_init_block(bp, BLOCK_UPB, init_phase);
6178         bnx2x_init_block(bp, BLOCK_XPB, init_phase);
6179
6180         bnx2x_init_block(bp, BLOCK_PBF, init_phase);
6181
6182         if (CHIP_IS_E1x(bp)) {
6183                 /* configure PBF to work without PAUSE mtu 9000 */
6184                 REG_WR(bp, PBF_REG_P0_PAUSE_ENABLE + port*4, 0);
6185
6186                 /* update threshold */
6187                 REG_WR(bp, PBF_REG_P0_ARB_THRSH + port*4, (9040/16));
6188                 /* update init credit */
6189                 REG_WR(bp, PBF_REG_P0_INIT_CRD + port*4, (9040/16) + 553 - 22);
6190
6191                 /* probe changes */
6192                 REG_WR(bp, PBF_REG_INIT_P0 + port*4, 1);
6193                 udelay(50);
6194                 REG_WR(bp, PBF_REG_INIT_P0 + port*4, 0);
6195         }
6196
6197 #ifdef BCM_CNIC
6198         bnx2x_init_block(bp, BLOCK_SRC, init_phase);
6199 #endif
6200         bnx2x_init_block(bp, BLOCK_CDU, init_phase);
6201         bnx2x_init_block(bp, BLOCK_CFC, init_phase);
6202
6203         if (CHIP_IS_E1(bp)) {
6204                 REG_WR(bp, HC_REG_LEADING_EDGE_0 + port*8, 0);
6205                 REG_WR(bp, HC_REG_TRAILING_EDGE_0 + port*8, 0);
6206         }
6207         bnx2x_init_block(bp, BLOCK_HC, init_phase);
6208
6209         bnx2x_init_block(bp, BLOCK_IGU, init_phase);
6210
6211         bnx2x_init_block(bp, BLOCK_MISC_AEU, init_phase);
6212         /* init aeu_mask_attn_func_0/1:
6213          *  - SF mode: bits 3-7 are masked. only bits 0-2 are in use
6214          *  - MF mode: bit 3 is masked. bits 0-2 are in use as in SF
6215          *             bits 4-7 are used for "per vn group attention" */
6216         val = IS_MF(bp) ? 0xF7 : 0x7;
6217         /* Enable DCBX attention for all but E1 */
6218         val |= CHIP_IS_E1(bp) ? 0 : 0x10;
6219         REG_WR(bp, MISC_REG_AEU_MASK_ATTN_FUNC_0 + port*4, val);
6220
6221         bnx2x_init_block(bp, BLOCK_NIG, init_phase);
6222
6223         if (!CHIP_IS_E1x(bp)) {
6224                 /* Bit-map indicating which L2 hdrs may appear after the
6225                  * basic Ethernet header
6226                  */
6227                 REG_WR(bp, BP_PORT(bp) ?
6228                            NIG_REG_P1_HDRS_AFTER_BASIC :
6229                            NIG_REG_P0_HDRS_AFTER_BASIC,
6230                            IS_MF_SD(bp) ? 7 : 6);
6231
6232                 if (CHIP_IS_E3(bp))
6233                         REG_WR(bp, BP_PORT(bp) ?
6234                                    NIG_REG_LLH1_MF_MODE :
6235                                    NIG_REG_LLH_MF_MODE, IS_MF(bp));
6236         }
6237         if (!CHIP_IS_E3(bp))
6238                 REG_WR(bp, NIG_REG_XGXS_SERDES0_MODE_SEL + port*4, 1);
6239
6240         if (!CHIP_IS_E1(bp)) {
6241                 /* 0x2 disable mf_ov, 0x1 enable */
6242                 REG_WR(bp, NIG_REG_LLH0_BRB1_DRV_MASK_MF + port*4,
6243                        (IS_MF_SD(bp) ? 0x1 : 0x2));
6244
6245                 if (!CHIP_IS_E1x(bp)) {
6246                         val = 0;
6247                         switch (bp->mf_mode) {
6248                         case MULTI_FUNCTION_SD:
6249                                 val = 1;
6250                                 break;
6251                         case MULTI_FUNCTION_SI:
6252                                 val = 2;
6253                                 break;
6254                         }
6255
6256                         REG_WR(bp, (BP_PORT(bp) ? NIG_REG_LLH1_CLS_TYPE :
6257                                                   NIG_REG_LLH0_CLS_TYPE), val);
6258                 }
6259                 {
6260                         REG_WR(bp, NIG_REG_LLFC_ENABLE_0 + port*4, 0);
6261                         REG_WR(bp, NIG_REG_LLFC_OUT_EN_0 + port*4, 0);
6262                         REG_WR(bp, NIG_REG_PAUSE_ENABLE_0 + port*4, 1);
6263                 }
6264         }
6265
6266
6267         /* If SPIO5 is set to generate interrupts, enable it for this port */
6268         val = REG_RD(bp, MISC_REG_SPIO_EVENT_EN);
6269         if (val & (1 << MISC_REGISTERS_SPIO_5)) {
6270                 u32 reg_addr = (port ? MISC_REG_AEU_ENABLE1_FUNC_1_OUT_0 :
6271                                        MISC_REG_AEU_ENABLE1_FUNC_0_OUT_0);
6272                 val = REG_RD(bp, reg_addr);
6273                 val |= AEU_INPUTS_ATTN_BITS_SPIO5;
6274                 REG_WR(bp, reg_addr, val);
6275         }
6276
6277         return 0;
6278 }
6279
6280 static void bnx2x_ilt_wr(struct bnx2x *bp, u32 index, dma_addr_t addr)
6281 {
6282         int reg;
6283
6284         if (CHIP_IS_E1(bp))
6285                 reg = PXP2_REG_RQ_ONCHIP_AT + index*8;
6286         else
6287                 reg = PXP2_REG_RQ_ONCHIP_AT_B0 + index*8;
6288
6289         bnx2x_wb_wr(bp, reg, ONCHIP_ADDR1(addr), ONCHIP_ADDR2(addr));
6290 }
6291
6292 static inline void bnx2x_igu_clear_sb(struct bnx2x *bp, u8 idu_sb_id)
6293 {
6294         bnx2x_igu_clear_sb_gen(bp, BP_FUNC(bp), idu_sb_id, true /*PF*/);
6295 }
6296
6297 static inline void bnx2x_clear_func_ilt(struct bnx2x *bp, u32 func)
6298 {
6299         u32 i, base = FUNC_ILT_BASE(func);
6300         for (i = base; i < base + ILT_PER_FUNC; i++)
6301                 bnx2x_ilt_wr(bp, i, 0);
6302 }
6303
6304 static int bnx2x_init_hw_func(struct bnx2x *bp)
6305 {
6306         int port = BP_PORT(bp);
6307         int func = BP_FUNC(bp);
6308         int init_phase = PHASE_PF0 + func;
6309         struct bnx2x_ilt *ilt = BP_ILT(bp);
6310         u16 cdu_ilt_start;
6311         u32 addr, val;
6312         u32 main_mem_base, main_mem_size, main_mem_prty_clr;
6313         int i, main_mem_width;
6314
6315         DP(BNX2X_MSG_MCP, "starting func init  func %d\n", func);
6316
6317         /* FLR cleanup - hmmm */
6318         if (!CHIP_IS_E1x(bp))
6319                 bnx2x_pf_flr_clnup(bp);
6320
6321         /* set MSI reconfigure capability */
6322         if (bp->common.int_block == INT_BLOCK_HC) {
6323                 addr = (port ? HC_REG_CONFIG_1 : HC_REG_CONFIG_0);
6324                 val = REG_RD(bp, addr);
6325                 val |= HC_CONFIG_0_REG_MSI_ATTN_EN_0;
6326                 REG_WR(bp, addr, val);
6327         }
6328
6329         bnx2x_init_block(bp, BLOCK_PXP, init_phase);
6330         bnx2x_init_block(bp, BLOCK_PXP2, init_phase);
6331
6332         ilt = BP_ILT(bp);
6333         cdu_ilt_start = ilt->clients[ILT_CLIENT_CDU].start;
6334
6335         for (i = 0; i < L2_ILT_LINES(bp); i++) {
6336                 ilt->lines[cdu_ilt_start + i].page =
6337                         bp->context.vcxt + (ILT_PAGE_CIDS * i);
6338                 ilt->lines[cdu_ilt_start + i].page_mapping =
6339                         bp->context.cxt_mapping + (CDU_ILT_PAGE_SZ * i);
6340                 /* cdu ilt pages are allocated manually so there's no need to
6341                 set the size */
6342         }
6343         bnx2x_ilt_init_op(bp, INITOP_SET);
6344
6345 #ifdef BCM_CNIC
6346         bnx2x_src_init_t2(bp, bp->t2, bp->t2_mapping, SRC_CONN_NUM);
6347
6348         /* T1 hash bits value determines the T1 number of entries */
6349         REG_WR(bp, SRC_REG_NUMBER_HASH_BITS0 + port*4, SRC_HASH_BITS);
6350 #endif
6351
6352 #ifndef BCM_CNIC
6353         /* set NIC mode */
6354         REG_WR(bp, PRS_REG_NIC_MODE, 1);
6355 #endif  /* BCM_CNIC */
6356
6357         if (!CHIP_IS_E1x(bp)) {
6358                 u32 pf_conf = IGU_PF_CONF_FUNC_EN;
6359
6360                 /* Turn on a single ISR mode in IGU if driver is going to use
6361                  * INT#x or MSI
6362                  */
6363                 if (!(bp->flags & USING_MSIX_FLAG))
6364                         pf_conf |= IGU_PF_CONF_SINGLE_ISR_EN;
6365                 /*
6366                  * Timers workaround bug: function init part.
6367                  * Need to wait 20msec after initializing ILT,
6368                  * needed to make sure there are no requests in
6369                  * one of the PXP internal queues with "old" ILT addresses
6370                  */
6371                 msleep(20);
6372                 /*
6373                  * Master enable - Due to WB DMAE writes performed before this
6374                  * register is re-initialized as part of the regular function
6375                  * init
6376                  */
6377                 REG_WR(bp, PGLUE_B_REG_INTERNAL_PFID_ENABLE_MASTER, 1);
6378                 /* Enable the function in IGU */
6379                 REG_WR(bp, IGU_REG_PF_CONFIGURATION, pf_conf);
6380         }
6381
6382         bp->dmae_ready = 1;
6383
6384         bnx2x_init_block(bp, BLOCK_PGLUE_B, init_phase);
6385
6386         if (!CHIP_IS_E1x(bp))
6387                 REG_WR(bp, PGLUE_B_REG_WAS_ERROR_PF_7_0_CLR, func);
6388
6389         bnx2x_init_block(bp, BLOCK_ATC, init_phase);
6390         bnx2x_init_block(bp, BLOCK_DMAE, init_phase);
6391         bnx2x_init_block(bp, BLOCK_NIG, init_phase);
6392         bnx2x_init_block(bp, BLOCK_SRC, init_phase);
6393         bnx2x_init_block(bp, BLOCK_MISC, init_phase);
6394         bnx2x_init_block(bp, BLOCK_TCM, init_phase);
6395         bnx2x_init_block(bp, BLOCK_UCM, init_phase);
6396         bnx2x_init_block(bp, BLOCK_CCM, init_phase);
6397         bnx2x_init_block(bp, BLOCK_XCM, init_phase);
6398         bnx2x_init_block(bp, BLOCK_TSEM, init_phase);
6399         bnx2x_init_block(bp, BLOCK_USEM, init_phase);
6400         bnx2x_init_block(bp, BLOCK_CSEM, init_phase);
6401         bnx2x_init_block(bp, BLOCK_XSEM, init_phase);
6402
6403         if (!CHIP_IS_E1x(bp))
6404                 REG_WR(bp, QM_REG_PF_EN, 1);
6405
6406         if (!CHIP_IS_E1x(bp)) {
6407                 REG_WR(bp, TSEM_REG_VFPF_ERR_NUM, BNX2X_MAX_NUM_OF_VFS + func);
6408                 REG_WR(bp, USEM_REG_VFPF_ERR_NUM, BNX2X_MAX_NUM_OF_VFS + func);
6409                 REG_WR(bp, CSEM_REG_VFPF_ERR_NUM, BNX2X_MAX_NUM_OF_VFS + func);
6410                 REG_WR(bp, XSEM_REG_VFPF_ERR_NUM, BNX2X_MAX_NUM_OF_VFS + func);
6411         }
6412         bnx2x_init_block(bp, BLOCK_QM, init_phase);
6413
6414         bnx2x_init_block(bp, BLOCK_TM, init_phase);
6415         bnx2x_init_block(bp, BLOCK_DORQ, init_phase);
6416         bnx2x_init_block(bp, BLOCK_BRB1, init_phase);
6417         bnx2x_init_block(bp, BLOCK_PRS, init_phase);
6418         bnx2x_init_block(bp, BLOCK_TSDM, init_phase);
6419         bnx2x_init_block(bp, BLOCK_CSDM, init_phase);
6420         bnx2x_init_block(bp, BLOCK_USDM, init_phase);
6421         bnx2x_init_block(bp, BLOCK_XSDM, init_phase);
6422         bnx2x_init_block(bp, BLOCK_UPB, init_phase);
6423         bnx2x_init_block(bp, BLOCK_XPB, init_phase);
6424         bnx2x_init_block(bp, BLOCK_PBF, init_phase);
6425         if (!CHIP_IS_E1x(bp))
6426                 REG_WR(bp, PBF_REG_DISABLE_PF, 0);
6427
6428         bnx2x_init_block(bp, BLOCK_CDU, init_phase);
6429
6430         bnx2x_init_block(bp, BLOCK_CFC, init_phase);
6431
6432         if (!CHIP_IS_E1x(bp))
6433                 REG_WR(bp, CFC_REG_WEAK_ENABLE_PF, 1);
6434
6435         if (IS_MF(bp)) {
6436                 REG_WR(bp, NIG_REG_LLH0_FUNC_EN + port*8, 1);
6437                 REG_WR(bp, NIG_REG_LLH0_FUNC_VLAN_ID + port*8, bp->mf_ov);
6438         }
6439
6440         bnx2x_init_block(bp, BLOCK_MISC_AEU, init_phase);
6441
6442         /* HC init per function */
6443         if (bp->common.int_block == INT_BLOCK_HC) {
6444                 if (CHIP_IS_E1H(bp)) {
6445                         REG_WR(bp, MISC_REG_AEU_GENERAL_ATTN_12 + func*4, 0);
6446
6447                         REG_WR(bp, HC_REG_LEADING_EDGE_0 + port*8, 0);
6448                         REG_WR(bp, HC_REG_TRAILING_EDGE_0 + port*8, 0);
6449                 }
6450                 bnx2x_init_block(bp, BLOCK_HC, init_phase);
6451
6452         } else {
6453                 int num_segs, sb_idx, prod_offset;
6454
6455                 REG_WR(bp, MISC_REG_AEU_GENERAL_ATTN_12 + func*4, 0);
6456
6457                 if (!CHIP_IS_E1x(bp)) {
6458                         REG_WR(bp, IGU_REG_LEADING_EDGE_LATCH, 0);
6459                         REG_WR(bp, IGU_REG_TRAILING_EDGE_LATCH, 0);
6460                 }
6461
6462                 bnx2x_init_block(bp, BLOCK_IGU, init_phase);
6463
6464                 if (!CHIP_IS_E1x(bp)) {
6465                         int dsb_idx = 0;
6466                         /**
6467                          * Producer memory:
6468                          * E2 mode: address 0-135 match to the mapping memory;
6469                          * 136 - PF0 default prod; 137 - PF1 default prod;
6470                          * 138 - PF2 default prod; 139 - PF3 default prod;
6471                          * 140 - PF0 attn prod;    141 - PF1 attn prod;
6472                          * 142 - PF2 attn prod;    143 - PF3 attn prod;
6473                          * 144-147 reserved.
6474                          *
6475                          * E1.5 mode - In backward compatible mode;
6476                          * for non default SB; each even line in the memory
6477                          * holds the U producer and each odd line hold
6478                          * the C producer. The first 128 producers are for
6479                          * NDSB (PF0 - 0-31; PF1 - 32-63 and so on). The last 20
6480                          * producers are for the DSB for each PF.
6481                          * Each PF has five segments: (the order inside each
6482                          * segment is PF0; PF1; PF2; PF3) - 128-131 U prods;
6483                          * 132-135 C prods; 136-139 X prods; 140-143 T prods;
6484                          * 144-147 attn prods;
6485                          */
6486                         /* non-default-status-blocks */
6487                         num_segs = CHIP_INT_MODE_IS_BC(bp) ?
6488                                 IGU_BC_NDSB_NUM_SEGS : IGU_NORM_NDSB_NUM_SEGS;
6489                         for (sb_idx = 0; sb_idx < bp->igu_sb_cnt; sb_idx++) {
6490                                 prod_offset = (bp->igu_base_sb + sb_idx) *
6491                                         num_segs;
6492
6493                                 for (i = 0; i < num_segs; i++) {
6494                                         addr = IGU_REG_PROD_CONS_MEMORY +
6495                                                         (prod_offset + i) * 4;
6496                                         REG_WR(bp, addr, 0);
6497                                 }
6498                                 /* send consumer update with value 0 */
6499                                 bnx2x_ack_sb(bp, bp->igu_base_sb + sb_idx,
6500                                              USTORM_ID, 0, IGU_INT_NOP, 1);
6501                                 bnx2x_igu_clear_sb(bp,
6502                                                    bp->igu_base_sb + sb_idx);
6503                         }
6504
6505                         /* default-status-blocks */
6506                         num_segs = CHIP_INT_MODE_IS_BC(bp) ?
6507                                 IGU_BC_DSB_NUM_SEGS : IGU_NORM_DSB_NUM_SEGS;
6508
6509                         if (CHIP_MODE_IS_4_PORT(bp))
6510                                 dsb_idx = BP_FUNC(bp);
6511                         else
6512                                 dsb_idx = BP_E1HVN(bp);
6513
6514                         prod_offset = (CHIP_INT_MODE_IS_BC(bp) ?
6515                                        IGU_BC_BASE_DSB_PROD + dsb_idx :
6516                                        IGU_NORM_BASE_DSB_PROD + dsb_idx);
6517
6518                         for (i = 0; i < (num_segs * E1HVN_MAX);
6519                              i += E1HVN_MAX) {
6520                                 addr = IGU_REG_PROD_CONS_MEMORY +
6521                                                         (prod_offset + i)*4;
6522                                 REG_WR(bp, addr, 0);
6523                         }
6524                         /* send consumer update with 0 */
6525                         if (CHIP_INT_MODE_IS_BC(bp)) {
6526                                 bnx2x_ack_sb(bp, bp->igu_dsb_id,
6527                                              USTORM_ID, 0, IGU_INT_NOP, 1);
6528                                 bnx2x_ack_sb(bp, bp->igu_dsb_id,
6529                                              CSTORM_ID, 0, IGU_INT_NOP, 1);
6530                                 bnx2x_ack_sb(bp, bp->igu_dsb_id,
6531                                              XSTORM_ID, 0, IGU_INT_NOP, 1);
6532                                 bnx2x_ack_sb(bp, bp->igu_dsb_id,
6533                                              TSTORM_ID, 0, IGU_INT_NOP, 1);
6534                                 bnx2x_ack_sb(bp, bp->igu_dsb_id,
6535                                              ATTENTION_ID, 0, IGU_INT_NOP, 1);
6536                         } else {
6537                                 bnx2x_ack_sb(bp, bp->igu_dsb_id,
6538                                              USTORM_ID, 0, IGU_INT_NOP, 1);
6539                                 bnx2x_ack_sb(bp, bp->igu_dsb_id,
6540                                              ATTENTION_ID, 0, IGU_INT_NOP, 1);
6541                         }
6542                         bnx2x_igu_clear_sb(bp, bp->igu_dsb_id);
6543
6544                         /* !!! these should become driver const once
6545                            rf-tool supports split-68 const */
6546                         REG_WR(bp, IGU_REG_SB_INT_BEFORE_MASK_LSB, 0);
6547                         REG_WR(bp, IGU_REG_SB_INT_BEFORE_MASK_MSB, 0);
6548                         REG_WR(bp, IGU_REG_SB_MASK_LSB, 0);
6549                         REG_WR(bp, IGU_REG_SB_MASK_MSB, 0);
6550                         REG_WR(bp, IGU_REG_PBA_STATUS_LSB, 0);
6551                         REG_WR(bp, IGU_REG_PBA_STATUS_MSB, 0);
6552                 }
6553         }
6554
6555         /* Reset PCIE errors for debug */
6556         REG_WR(bp, 0x2114, 0xffffffff);
6557         REG_WR(bp, 0x2120, 0xffffffff);
6558
6559         if (CHIP_IS_E1x(bp)) {
6560                 main_mem_size = HC_REG_MAIN_MEMORY_SIZE / 2; /*dwords*/
6561                 main_mem_base = HC_REG_MAIN_MEMORY +
6562                                 BP_PORT(bp) * (main_mem_size * 4);
6563                 main_mem_prty_clr = HC_REG_HC_PRTY_STS_CLR;
6564                 main_mem_width = 8;
6565
6566                 val = REG_RD(bp, main_mem_prty_clr);
6567                 if (val)
6568                         DP(BNX2X_MSG_MCP, "Hmmm... Parity errors in HC "
6569                                           "block during "
6570                                           "function init (0x%x)!\n", val);
6571
6572                 /* Clear "false" parity errors in MSI-X table */
6573                 for (i = main_mem_base;
6574                      i < main_mem_base + main_mem_size * 4;
6575                      i += main_mem_width) {
6576                         bnx2x_read_dmae(bp, i, main_mem_width / 4);
6577                         bnx2x_write_dmae(bp, bnx2x_sp_mapping(bp, wb_data),
6578                                          i, main_mem_width / 4);
6579                 }
6580                 /* Clear HC parity attention */
6581                 REG_RD(bp, main_mem_prty_clr);
6582         }
6583
6584 #ifdef BNX2X_STOP_ON_ERROR
6585         /* Enable STORMs SP logging */
6586         REG_WR8(bp, BAR_USTRORM_INTMEM +
6587                USTORM_RECORD_SLOW_PATH_OFFSET(BP_FUNC(bp)), 1);
6588         REG_WR8(bp, BAR_TSTRORM_INTMEM +
6589                TSTORM_RECORD_SLOW_PATH_OFFSET(BP_FUNC(bp)), 1);
6590         REG_WR8(bp, BAR_CSTRORM_INTMEM +
6591                CSTORM_RECORD_SLOW_PATH_OFFSET(BP_FUNC(bp)), 1);
6592         REG_WR8(bp, BAR_XSTRORM_INTMEM +
6593                XSTORM_RECORD_SLOW_PATH_OFFSET(BP_FUNC(bp)), 1);
6594 #endif
6595
6596         bnx2x_phy_probe(&bp->link_params);
6597
6598         return 0;
6599 }
6600
6601
6602 void bnx2x_free_mem(struct bnx2x *bp)
6603 {
6604         /* fastpath */
6605         bnx2x_free_fp_mem(bp);
6606         /* end of fastpath */
6607
6608         BNX2X_PCI_FREE(bp->def_status_blk, bp->def_status_blk_mapping,
6609                        sizeof(struct host_sp_status_block));
6610
6611         BNX2X_PCI_FREE(bp->fw_stats, bp->fw_stats_mapping,
6612                        bp->fw_stats_data_sz + bp->fw_stats_req_sz);
6613
6614         BNX2X_PCI_FREE(bp->slowpath, bp->slowpath_mapping,
6615                        sizeof(struct bnx2x_slowpath));
6616
6617         BNX2X_PCI_FREE(bp->context.vcxt, bp->context.cxt_mapping,
6618                        bp->context.size);
6619
6620         bnx2x_ilt_mem_op(bp, ILT_MEMOP_FREE);
6621
6622         BNX2X_FREE(bp->ilt->lines);
6623
6624 #ifdef BCM_CNIC
6625         if (!CHIP_IS_E1x(bp))
6626                 BNX2X_PCI_FREE(bp->cnic_sb.e2_sb, bp->cnic_sb_mapping,
6627                                sizeof(struct host_hc_status_block_e2));
6628         else
6629                 BNX2X_PCI_FREE(bp->cnic_sb.e1x_sb, bp->cnic_sb_mapping,
6630                                sizeof(struct host_hc_status_block_e1x));
6631
6632         BNX2X_PCI_FREE(bp->t2, bp->t2_mapping, SRC_T2_SZ);
6633 #endif
6634
6635         BNX2X_PCI_FREE(bp->spq, bp->spq_mapping, BCM_PAGE_SIZE);
6636
6637         BNX2X_PCI_FREE(bp->eq_ring, bp->eq_mapping,
6638                        BCM_PAGE_SIZE * NUM_EQ_PAGES);
6639 }
6640
6641 static inline int bnx2x_alloc_fw_stats_mem(struct bnx2x *bp)
6642 {
6643         int num_groups;
6644
6645         /* number of eth_queues */
6646         u8 num_queue_stats = BNX2X_NUM_ETH_QUEUES(bp);
6647
6648         /* Total number of FW statistics requests =
6649          * 1 for port stats + 1 for PF stats + num_eth_queues */
6650         bp->fw_stats_num = 2 + num_queue_stats;
6651
6652
6653         /* Request is built from stats_query_header and an array of
6654          * stats_query_cmd_group each of which contains
6655          * STATS_QUERY_CMD_COUNT rules. The real number or requests is
6656          * configured in the stats_query_header.
6657          */
6658         num_groups = (2 + num_queue_stats) / STATS_QUERY_CMD_COUNT +
6659                 (((2 + num_queue_stats) % STATS_QUERY_CMD_COUNT) ? 1 : 0);
6660
6661         bp->fw_stats_req_sz = sizeof(struct stats_query_header) +
6662                         num_groups * sizeof(struct stats_query_cmd_group);
6663
6664         /* Data for statistics requests + stats_conter
6665          *
6666          * stats_counter holds per-STORM counters that are incremented
6667          * when STORM has finished with the current request.
6668          */
6669         bp->fw_stats_data_sz = sizeof(struct per_port_stats) +
6670                 sizeof(struct per_pf_stats) +
6671                 sizeof(struct per_queue_stats) * num_queue_stats +
6672                 sizeof(struct stats_counter);
6673
6674         BNX2X_PCI_ALLOC(bp->fw_stats, &bp->fw_stats_mapping,
6675                         bp->fw_stats_data_sz + bp->fw_stats_req_sz);
6676
6677         /* Set shortcuts */
6678         bp->fw_stats_req = (struct bnx2x_fw_stats_req *)bp->fw_stats;
6679         bp->fw_stats_req_mapping = bp->fw_stats_mapping;
6680
6681         bp->fw_stats_data = (struct bnx2x_fw_stats_data *)
6682                 ((u8 *)bp->fw_stats + bp->fw_stats_req_sz);
6683
6684         bp->fw_stats_data_mapping = bp->fw_stats_mapping +
6685                                    bp->fw_stats_req_sz;
6686         return 0;
6687
6688 alloc_mem_err:
6689         BNX2X_PCI_FREE(bp->fw_stats, bp->fw_stats_mapping,
6690                        bp->fw_stats_data_sz + bp->fw_stats_req_sz);
6691         return -ENOMEM;
6692 }
6693
6694
6695 int bnx2x_alloc_mem(struct bnx2x *bp)
6696 {
6697 #ifdef BCM_CNIC
6698         if (!CHIP_IS_E1x(bp))
6699                 /* size = the status block + ramrod buffers */
6700                 BNX2X_PCI_ALLOC(bp->cnic_sb.e2_sb, &bp->cnic_sb_mapping,
6701                                 sizeof(struct host_hc_status_block_e2));
6702         else
6703                 BNX2X_PCI_ALLOC(bp->cnic_sb.e1x_sb, &bp->cnic_sb_mapping,
6704                                 sizeof(struct host_hc_status_block_e1x));
6705
6706         /* allocate searcher T2 table */
6707         BNX2X_PCI_ALLOC(bp->t2, &bp->t2_mapping, SRC_T2_SZ);
6708 #endif
6709
6710
6711         BNX2X_PCI_ALLOC(bp->def_status_blk, &bp->def_status_blk_mapping,
6712                         sizeof(struct host_sp_status_block));
6713
6714         BNX2X_PCI_ALLOC(bp->slowpath, &bp->slowpath_mapping,
6715                         sizeof(struct bnx2x_slowpath));
6716
6717         /* Allocated memory for FW statistics  */
6718         if (bnx2x_alloc_fw_stats_mem(bp))
6719                 goto alloc_mem_err;
6720
6721         bp->context.size = sizeof(union cdu_context) * bp->l2_cid_count;
6722
6723         BNX2X_PCI_ALLOC(bp->context.vcxt, &bp->context.cxt_mapping,
6724                         bp->context.size);
6725
6726         BNX2X_ALLOC(bp->ilt->lines, sizeof(struct ilt_line) * ILT_MAX_LINES);
6727
6728         if (bnx2x_ilt_mem_op(bp, ILT_MEMOP_ALLOC))
6729                 goto alloc_mem_err;
6730
6731         /* Slow path ring */
6732         BNX2X_PCI_ALLOC(bp->spq, &bp->spq_mapping, BCM_PAGE_SIZE);
6733
6734         /* EQ */
6735         BNX2X_PCI_ALLOC(bp->eq_ring, &bp->eq_mapping,
6736                         BCM_PAGE_SIZE * NUM_EQ_PAGES);
6737
6738
6739         /* fastpath */
6740         /* need to be done at the end, since it's self adjusting to amount
6741          * of memory available for RSS queues
6742          */
6743         if (bnx2x_alloc_fp_mem(bp))
6744                 goto alloc_mem_err;
6745         return 0;
6746
6747 alloc_mem_err:
6748         bnx2x_free_mem(bp);
6749         return -ENOMEM;
6750 }
6751
6752 /*
6753  * Init service functions
6754  */
6755
6756 int bnx2x_set_mac_one(struct bnx2x *bp, u8 *mac,
6757                       struct bnx2x_vlan_mac_obj *obj, bool set,
6758                       int mac_type, unsigned long *ramrod_flags)
6759 {
6760         int rc;
6761         struct bnx2x_vlan_mac_ramrod_params ramrod_param;
6762
6763         memset(&ramrod_param, 0, sizeof(ramrod_param));
6764
6765         /* Fill general parameters */
6766         ramrod_param.vlan_mac_obj = obj;
6767         ramrod_param.ramrod_flags = *ramrod_flags;
6768
6769         /* Fill a user request section if needed */
6770         if (!test_bit(RAMROD_CONT, ramrod_flags)) {
6771                 memcpy(ramrod_param.user_req.u.mac.mac, mac, ETH_ALEN);
6772
6773                 __set_bit(mac_type, &ramrod_param.user_req.vlan_mac_flags);
6774
6775                 /* Set the command: ADD or DEL */
6776                 if (set)
6777                         ramrod_param.user_req.cmd = BNX2X_VLAN_MAC_ADD;
6778                 else
6779                         ramrod_param.user_req.cmd = BNX2X_VLAN_MAC_DEL;
6780         }
6781
6782         rc = bnx2x_config_vlan_mac(bp, &ramrod_param);
6783         if (rc < 0)
6784                 BNX2X_ERR("%s MAC failed\n", (set ? "Set" : "Del"));
6785         return rc;
6786 }
6787
6788 int bnx2x_del_all_macs(struct bnx2x *bp,
6789                        struct bnx2x_vlan_mac_obj *mac_obj,
6790                        int mac_type, bool wait_for_comp)
6791 {
6792         int rc;
6793         unsigned long ramrod_flags = 0, vlan_mac_flags = 0;
6794
6795         /* Wait for completion of requested */
6796         if (wait_for_comp)
6797                 __set_bit(RAMROD_COMP_WAIT, &ramrod_flags);
6798
6799         /* Set the mac type of addresses we want to clear */
6800         __set_bit(mac_type, &vlan_mac_flags);
6801
6802         rc = mac_obj->delete_all(bp, mac_obj, &vlan_mac_flags, &ramrod_flags);
6803         if (rc < 0)
6804                 BNX2X_ERR("Failed to delete MACs: %d\n", rc);
6805
6806         return rc;
6807 }
6808
6809 int bnx2x_set_eth_mac(struct bnx2x *bp, bool set)
6810 {
6811         unsigned long ramrod_flags = 0;
6812
6813         DP(NETIF_MSG_IFUP, "Adding Eth MAC\n");
6814
6815         __set_bit(RAMROD_COMP_WAIT, &ramrod_flags);
6816         /* Eth MAC is set on RSS leading client (fp[0]) */
6817         return bnx2x_set_mac_one(bp, bp->dev->dev_addr, &bp->fp->mac_obj, set,
6818                                  BNX2X_ETH_MAC, &ramrod_flags);
6819 }
6820
6821 int bnx2x_setup_leading(struct bnx2x *bp)
6822 {
6823         return bnx2x_setup_queue(bp, &bp->fp[0], 1);
6824 }
6825
6826 /**
6827  * bnx2x_set_int_mode - configure interrupt mode
6828  *
6829  * @bp:         driver handle
6830  *
6831  * In case of MSI-X it will also try to enable MSI-X.
6832  */
6833 static void __devinit bnx2x_set_int_mode(struct bnx2x *bp)
6834 {
6835         switch (int_mode) {
6836         case INT_MODE_MSI:
6837                 bnx2x_enable_msi(bp);
6838                 /* falling through... */
6839         case INT_MODE_INTx:
6840                 bp->num_queues = 1 + NONE_ETH_CONTEXT_USE;
6841                 DP(NETIF_MSG_IFUP, "set number of queues to 1\n");
6842                 break;
6843         default:
6844                 /* Set number of queues according to bp->multi_mode value */
6845                 bnx2x_set_num_queues(bp);
6846
6847                 DP(NETIF_MSG_IFUP, "set number of queues to %d\n",
6848                    bp->num_queues);
6849
6850                 /* if we can't use MSI-X we only need one fp,
6851                  * so try to enable MSI-X with the requested number of fp's
6852                  * and fallback to MSI or legacy INTx with one fp
6853                  */
6854                 if (bnx2x_enable_msix(bp)) {
6855                         /* failed to enable MSI-X */
6856                         if (bp->multi_mode)
6857                                 DP(NETIF_MSG_IFUP,
6858                                           "Multi requested but failed to "
6859                                           "enable MSI-X (%d), "
6860                                           "set number of queues to %d\n",
6861                                    bp->num_queues,
6862                                    1 + NONE_ETH_CONTEXT_USE);
6863                         bp->num_queues = 1 + NONE_ETH_CONTEXT_USE;
6864
6865                         /* Try to enable MSI */
6866                         if (!(bp->flags & DISABLE_MSI_FLAG))
6867                                 bnx2x_enable_msi(bp);
6868                 }
6869                 break;
6870         }
6871 }
6872
6873 /* must be called prioir to any HW initializations */
6874 static inline u16 bnx2x_cid_ilt_lines(struct bnx2x *bp)
6875 {
6876         return L2_ILT_LINES(bp);
6877 }
6878
6879 void bnx2x_ilt_set_info(struct bnx2x *bp)
6880 {
6881         struct ilt_client_info *ilt_client;
6882         struct bnx2x_ilt *ilt = BP_ILT(bp);
6883         u16 line = 0;
6884
6885         ilt->start_line = FUNC_ILT_BASE(BP_FUNC(bp));
6886         DP(BNX2X_MSG_SP, "ilt starts at line %d\n", ilt->start_line);
6887
6888         /* CDU */
6889         ilt_client = &ilt->clients[ILT_CLIENT_CDU];
6890         ilt_client->client_num = ILT_CLIENT_CDU;
6891         ilt_client->page_size = CDU_ILT_PAGE_SZ;
6892         ilt_client->flags = ILT_CLIENT_SKIP_MEM;
6893         ilt_client->start = line;
6894         line += bnx2x_cid_ilt_lines(bp);
6895 #ifdef BCM_CNIC
6896         line += CNIC_ILT_LINES;
6897 #endif
6898         ilt_client->end = line - 1;
6899
6900         DP(BNX2X_MSG_SP, "ilt client[CDU]: start %d, end %d, psz 0x%x, "
6901                                          "flags 0x%x, hw psz %d\n",
6902            ilt_client->start,
6903            ilt_client->end,
6904            ilt_client->page_size,
6905            ilt_client->flags,
6906            ilog2(ilt_client->page_size >> 12));
6907
6908         /* QM */
6909         if (QM_INIT(bp->qm_cid_count)) {
6910                 ilt_client = &ilt->clients[ILT_CLIENT_QM];
6911                 ilt_client->client_num = ILT_CLIENT_QM;
6912                 ilt_client->page_size = QM_ILT_PAGE_SZ;
6913                 ilt_client->flags = 0;
6914                 ilt_client->start = line;
6915
6916                 /* 4 bytes for each cid */
6917                 line += DIV_ROUND_UP(bp->qm_cid_count * QM_QUEUES_PER_FUNC * 4,
6918                                                          QM_ILT_PAGE_SZ);
6919
6920                 ilt_client->end = line - 1;
6921
6922                 DP(BNX2X_MSG_SP, "ilt client[QM]: start %d, end %d, psz 0x%x, "
6923                                                  "flags 0x%x, hw psz %d\n",
6924                    ilt_client->start,
6925                    ilt_client->end,
6926                    ilt_client->page_size,
6927                    ilt_client->flags,
6928                    ilog2(ilt_client->page_size >> 12));
6929
6930         }
6931         /* SRC */
6932         ilt_client = &ilt->clients[ILT_CLIENT_SRC];
6933 #ifdef BCM_CNIC
6934         ilt_client->client_num = ILT_CLIENT_SRC;
6935         ilt_client->page_size = SRC_ILT_PAGE_SZ;
6936         ilt_client->flags = 0;
6937         ilt_client->start = line;
6938         line += SRC_ILT_LINES;
6939         ilt_client->end = line - 1;
6940
6941         DP(BNX2X_MSG_SP, "ilt client[SRC]: start %d, end %d, psz 0x%x, "
6942                                          "flags 0x%x, hw psz %d\n",
6943            ilt_client->start,
6944            ilt_client->end,
6945            ilt_client->page_size,
6946            ilt_client->flags,
6947            ilog2(ilt_client->page_size >> 12));
6948
6949 #else
6950         ilt_client->flags = (ILT_CLIENT_SKIP_INIT | ILT_CLIENT_SKIP_MEM);
6951 #endif
6952
6953         /* TM */
6954         ilt_client = &ilt->clients[ILT_CLIENT_TM];
6955 #ifdef BCM_CNIC
6956         ilt_client->client_num = ILT_CLIENT_TM;
6957         ilt_client->page_size = TM_ILT_PAGE_SZ;
6958         ilt_client->flags = 0;
6959         ilt_client->start = line;
6960         line += TM_ILT_LINES;
6961         ilt_client->end = line - 1;
6962
6963         DP(BNX2X_MSG_SP, "ilt client[TM]: start %d, end %d, psz 0x%x, "
6964                                          "flags 0x%x, hw psz %d\n",
6965            ilt_client->start,
6966            ilt_client->end,
6967            ilt_client->page_size,
6968            ilt_client->flags,
6969            ilog2(ilt_client->page_size >> 12));
6970
6971 #else
6972         ilt_client->flags = (ILT_CLIENT_SKIP_INIT | ILT_CLIENT_SKIP_MEM);
6973 #endif
6974         BUG_ON(line > ILT_MAX_LINES);
6975 }
6976
6977 /**
6978  * bnx2x_pf_q_prep_init - prepare INIT transition parameters
6979  *
6980  * @bp:                 driver handle
6981  * @fp:                 pointer to fastpath
6982  * @init_params:        pointer to parameters structure
6983  *
6984  * parameters configured:
6985  *      - HC configuration
6986  *      - Queue's CDU context
6987  */
6988 static inline void bnx2x_pf_q_prep_init(struct bnx2x *bp,
6989         struct bnx2x_fastpath *fp, struct bnx2x_queue_init_params *init_params)
6990 {
6991         /* FCoE Queue uses Default SB, thus has no HC capabilities */
6992         if (!IS_FCOE_FP(fp)) {
6993                 __set_bit(BNX2X_Q_FLG_HC, &init_params->rx.flags);
6994                 __set_bit(BNX2X_Q_FLG_HC, &init_params->tx.flags);
6995
6996                 /* If HC is supporterd, enable host coalescing in the transition
6997                  * to INIT state.
6998                  */
6999                 __set_bit(BNX2X_Q_FLG_HC_EN, &init_params->rx.flags);
7000                 __set_bit(BNX2X_Q_FLG_HC_EN, &init_params->tx.flags);
7001
7002                 /* HC rate */
7003                 init_params->rx.hc_rate = bp->rx_ticks ?
7004                         (1000000 / bp->rx_ticks) : 0;
7005                 init_params->tx.hc_rate = bp->tx_ticks ?
7006                         (1000000 / bp->tx_ticks) : 0;
7007
7008                 /* FW SB ID */
7009                 init_params->rx.fw_sb_id = init_params->tx.fw_sb_id =
7010                         fp->fw_sb_id;
7011
7012                 /*
7013                  * CQ index among the SB indices: FCoE clients uses the default
7014                  * SB, therefore it's different.
7015                  */
7016                 init_params->rx.sb_cq_index = U_SB_ETH_RX_CQ_INDEX;
7017                 init_params->tx.sb_cq_index = C_SB_ETH_TX_CQ_INDEX;
7018         }
7019
7020         init_params->cxt = &bp->context.vcxt[fp->cid].eth;
7021 }
7022
7023 /**
7024  * bnx2x_setup_queue - setup queue
7025  *
7026  * @bp:         driver handle
7027  * @fp:         pointer to fastpath
7028  * @leading:    is leading
7029  *
7030  * This function performs 2 steps in a Queue state machine
7031  *      actually: 1) RESET->INIT 2) INIT->SETUP
7032  */
7033
7034 int bnx2x_setup_queue(struct bnx2x *bp, struct bnx2x_fastpath *fp,
7035                        bool leading)
7036 {
7037         struct bnx2x_queue_state_params q_params = {0};
7038         struct bnx2x_queue_setup_params *setup_params =
7039                                                 &q_params.params.setup;
7040         int rc;
7041
7042         /* reset IGU state skip FCoE L2 queue */
7043         if (!IS_FCOE_FP(fp))
7044                 bnx2x_ack_sb(bp, fp->igu_sb_id, USTORM_ID, 0,
7045                              IGU_INT_ENABLE, 0);
7046
7047         q_params.q_obj = &fp->q_obj;
7048         /* We want to wait for completion in this context */
7049         __set_bit(RAMROD_COMP_WAIT, &q_params.ramrod_flags);
7050
7051         /* Prepare the INIT parameters */
7052         bnx2x_pf_q_prep_init(bp, fp, &q_params.params.init);
7053
7054         /* Set the command */
7055         q_params.cmd = BNX2X_Q_CMD_INIT;
7056
7057         /* Change the state to INIT */
7058         rc = bnx2x_queue_state_change(bp, &q_params);
7059         if (rc) {
7060                 BNX2X_ERR("Queue INIT failed\n");
7061                 return rc;
7062         }
7063
7064         /* Now move the Queue to the SETUP state... */
7065         memset(setup_params, 0, sizeof(*setup_params));
7066
7067         /* Set QUEUE flags */
7068         setup_params->flags = bnx2x_get_q_flags(bp, fp, leading);
7069
7070         /* Set general SETUP parameters */
7071         bnx2x_pf_q_prep_general(bp, fp, &setup_params->gen_params);
7072
7073         bnx2x_pf_rx_q_prep(bp, fp, &setup_params->pause,
7074                             &setup_params->rxq_params);
7075
7076         bnx2x_pf_tx_q_prep(bp, fp, &setup_params->txq_params);
7077
7078         /* Set the command */
7079         q_params.cmd = BNX2X_Q_CMD_SETUP;
7080
7081         /* Change the state to SETUP */
7082         rc = bnx2x_queue_state_change(bp, &q_params);
7083         if (rc)
7084                 BNX2X_ERR("Queue SETUP failed\n");
7085
7086         return rc;
7087 }
7088
7089 static int bnx2x_stop_queue(struct bnx2x *bp, int index)
7090 {
7091         struct bnx2x_fastpath *fp = &bp->fp[index];
7092         struct bnx2x_queue_state_params q_params = {0};
7093         int rc;
7094
7095         q_params.q_obj = &fp->q_obj;
7096         /* We want to wait for completion in this context */
7097         __set_bit(RAMROD_COMP_WAIT, &q_params.ramrod_flags);
7098
7099         /* halt the connection */
7100         q_params.cmd = BNX2X_Q_CMD_HALT;
7101         rc = bnx2x_queue_state_change(bp, &q_params);
7102         if (rc)
7103                 return rc;
7104
7105         /* terminate the connection */
7106         q_params.cmd = BNX2X_Q_CMD_TERMINATE;
7107         rc = bnx2x_queue_state_change(bp, &q_params);
7108         if (rc)
7109                 return rc;
7110
7111         /* delete cfc entry */
7112         q_params.cmd = BNX2X_Q_CMD_CFC_DEL;
7113         return bnx2x_queue_state_change(bp, &q_params);
7114 }
7115
7116
7117 static void bnx2x_reset_func(struct bnx2x *bp)
7118 {
7119         int port = BP_PORT(bp);
7120         int func = BP_FUNC(bp);
7121         int i;
7122
7123         /* Disable the function in the FW */
7124         REG_WR8(bp, BAR_XSTRORM_INTMEM + XSTORM_FUNC_EN_OFFSET(func), 0);
7125         REG_WR8(bp, BAR_CSTRORM_INTMEM + CSTORM_FUNC_EN_OFFSET(func), 0);
7126         REG_WR8(bp, BAR_TSTRORM_INTMEM + TSTORM_FUNC_EN_OFFSET(func), 0);
7127         REG_WR8(bp, BAR_USTRORM_INTMEM + USTORM_FUNC_EN_OFFSET(func), 0);
7128
7129         /* FP SBs */
7130         for_each_eth_queue(bp, i) {
7131                 struct bnx2x_fastpath *fp = &bp->fp[i];
7132                 REG_WR8(bp, BAR_CSTRORM_INTMEM +
7133                         CSTORM_STATUS_BLOCK_DATA_STATE_OFFSET(fp->fw_sb_id),
7134                         SB_DISABLED);
7135         }
7136
7137 #ifdef BCM_CNIC
7138         /* CNIC SB */
7139         REG_WR8(bp, BAR_CSTRORM_INTMEM +
7140                 CSTORM_STATUS_BLOCK_DATA_STATE_OFFSET(bnx2x_cnic_fw_sb_id(bp)),
7141                 SB_DISABLED);
7142 #endif
7143         /* SP SB */
7144         REG_WR8(bp, BAR_CSTRORM_INTMEM +
7145                 CSTORM_SP_STATUS_BLOCK_DATA_STATE_OFFSET(func),
7146                 SB_DISABLED);
7147
7148         for (i = 0; i < XSTORM_SPQ_DATA_SIZE / 4; i++)
7149                 REG_WR(bp, BAR_XSTRORM_INTMEM + XSTORM_SPQ_DATA_OFFSET(func),
7150                        0);
7151
7152         /* Configure IGU */
7153         if (bp->common.int_block == INT_BLOCK_HC) {
7154                 REG_WR(bp, HC_REG_LEADING_EDGE_0 + port*8, 0);
7155                 REG_WR(bp, HC_REG_TRAILING_EDGE_0 + port*8, 0);
7156         } else {
7157                 REG_WR(bp, IGU_REG_LEADING_EDGE_LATCH, 0);
7158                 REG_WR(bp, IGU_REG_TRAILING_EDGE_LATCH, 0);
7159         }
7160
7161 #ifdef BCM_CNIC
7162         /* Disable Timer scan */
7163         REG_WR(bp, TM_REG_EN_LINEAR0_TIMER + port*4, 0);
7164         /*
7165          * Wait for at least 10ms and up to 2 second for the timers scan to
7166          * complete
7167          */
7168         for (i = 0; i < 200; i++) {
7169                 msleep(10);
7170                 if (!REG_RD(bp, TM_REG_LIN0_SCAN_ON + port*4))
7171                         break;
7172         }
7173 #endif
7174         /* Clear ILT */
7175         bnx2x_clear_func_ilt(bp, func);
7176
7177         /* Timers workaround bug for E2: if this is vnic-3,
7178          * we need to set the entire ilt range for this timers.
7179          */
7180         if (!CHIP_IS_E1x(bp) && BP_VN(bp) == 3) {
7181                 struct ilt_client_info ilt_cli;
7182                 /* use dummy TM client */
7183                 memset(&ilt_cli, 0, sizeof(struct ilt_client_info));
7184                 ilt_cli.start = 0;
7185                 ilt_cli.end = ILT_NUM_PAGE_ENTRIES - 1;
7186                 ilt_cli.client_num = ILT_CLIENT_TM;
7187
7188                 bnx2x_ilt_boundry_init_op(bp, &ilt_cli, 0, INITOP_CLEAR);
7189         }
7190
7191         /* this assumes that reset_port() called before reset_func()*/
7192         if (!CHIP_IS_E1x(bp))
7193                 bnx2x_pf_disable(bp);
7194
7195         bp->dmae_ready = 0;
7196 }
7197
7198 static void bnx2x_reset_port(struct bnx2x *bp)
7199 {
7200         int port = BP_PORT(bp);
7201         u32 val;
7202
7203         /* Reset physical Link */
7204         bnx2x__link_reset(bp);
7205
7206         REG_WR(bp, NIG_REG_MASK_INTERRUPT_PORT0 + port*4, 0);
7207
7208         /* Do not rcv packets to BRB */
7209         REG_WR(bp, NIG_REG_LLH0_BRB1_DRV_MASK + port*4, 0x0);
7210         /* Do not direct rcv packets that are not for MCP to the BRB */
7211         REG_WR(bp, (port ? NIG_REG_LLH1_BRB1_NOT_MCP :
7212                            NIG_REG_LLH0_BRB1_NOT_MCP), 0x0);
7213
7214         /* Configure AEU */
7215         REG_WR(bp, MISC_REG_AEU_MASK_ATTN_FUNC_0 + port*4, 0);
7216
7217         msleep(100);
7218         /* Check for BRB port occupancy */
7219         val = REG_RD(bp, BRB1_REG_PORT_NUM_OCC_BLOCKS_0 + port*4);
7220         if (val)
7221                 DP(NETIF_MSG_IFDOWN,
7222                    "BRB1 is not empty  %d blocks are occupied\n", val);
7223
7224         /* TODO: Close Doorbell port? */
7225 }
7226
7227 static inline int bnx2x_reset_hw(struct bnx2x *bp, u32 load_code)
7228 {
7229         struct bnx2x_func_state_params func_params = {0};
7230
7231         /* Prepare parameters for function state transitions */
7232         __set_bit(RAMROD_COMP_WAIT, &func_params.ramrod_flags);
7233
7234         func_params.f_obj = &bp->func_obj;
7235         func_params.cmd = BNX2X_F_CMD_HW_RESET;
7236
7237         func_params.params.hw_init.load_phase = load_code;
7238
7239         return bnx2x_func_state_change(bp, &func_params);
7240 }
7241
7242 static inline int bnx2x_func_stop(struct bnx2x *bp)
7243 {
7244         struct bnx2x_func_state_params func_params = {0};
7245         int rc;
7246
7247         /* Prepare parameters for function state transitions */
7248         __set_bit(RAMROD_COMP_WAIT, &func_params.ramrod_flags);
7249         func_params.f_obj = &bp->func_obj;
7250         func_params.cmd = BNX2X_F_CMD_STOP;
7251
7252         /*
7253          * Try to stop the function the 'good way'. If fails (in case
7254          * of a parity error during bnx2x_chip_cleanup()) and we are
7255          * not in a debug mode, perform a state transaction in order to
7256          * enable further HW_RESET transaction.
7257          */
7258         rc = bnx2x_func_state_change(bp, &func_params);
7259         if (rc) {
7260 #ifdef BNX2X_STOP_ON_ERROR
7261                 return rc;
7262 #else
7263                 BNX2X_ERR("FUNC_STOP ramrod failed. Running a dry "
7264                           "transaction\n");
7265                 __set_bit(RAMROD_DRV_CLR_ONLY, &func_params.ramrod_flags);
7266                 return bnx2x_func_state_change(bp, &func_params);
7267 #endif
7268         }
7269
7270         return 0;
7271 }
7272
7273 /**
7274  * bnx2x_send_unload_req - request unload mode from the MCP.
7275  *
7276  * @bp:                 driver handle
7277  * @unload_mode:        requested function's unload mode
7278  *
7279  * Return unload mode returned by the MCP: COMMON, PORT or FUNC.
7280  */
7281 u32 bnx2x_send_unload_req(struct bnx2x *bp, int unload_mode)
7282 {
7283         u32 reset_code = 0;
7284         int port = BP_PORT(bp);
7285
7286         /* Select the UNLOAD request mode */
7287         if (unload_mode == UNLOAD_NORMAL)
7288                 reset_code = DRV_MSG_CODE_UNLOAD_REQ_WOL_DIS;
7289
7290         else if (bp->flags & NO_WOL_FLAG)
7291                 reset_code = DRV_MSG_CODE_UNLOAD_REQ_WOL_MCP;
7292
7293         else if (bp->wol) {
7294                 u32 emac_base = port ? GRCBASE_EMAC1 : GRCBASE_EMAC0;
7295                 u8 *mac_addr = bp->dev->dev_addr;
7296                 u32 val;
7297                 /* The mac address is written to entries 1-4 to
7298                    preserve entry 0 which is used by the PMF */
7299                 u8 entry = (BP_E1HVN(bp) + 1)*8;
7300
7301                 val = (mac_addr[0] << 8) | mac_addr[1];
7302                 EMAC_WR(bp, EMAC_REG_EMAC_MAC_MATCH + entry, val);
7303
7304                 val = (mac_addr[2] << 24) | (mac_addr[3] << 16) |
7305                       (mac_addr[4] << 8) | mac_addr[5];
7306                 EMAC_WR(bp, EMAC_REG_EMAC_MAC_MATCH + entry + 4, val);
7307
7308                 reset_code = DRV_MSG_CODE_UNLOAD_REQ_WOL_EN;
7309
7310         } else
7311                 reset_code = DRV_MSG_CODE_UNLOAD_REQ_WOL_DIS;
7312
7313         /* Send the request to the MCP */
7314         if (!BP_NOMCP(bp))
7315                 reset_code = bnx2x_fw_command(bp, reset_code, 0);
7316         else {
7317                 int path = BP_PATH(bp);
7318
7319                 DP(NETIF_MSG_IFDOWN, "NO MCP - load counts[%d]      "
7320                                      "%d, %d, %d\n",
7321                    path, load_count[path][0], load_count[path][1],
7322                    load_count[path][2]);
7323                 load_count[path][0]--;
7324                 load_count[path][1 + port]--;
7325                 DP(NETIF_MSG_IFDOWN, "NO MCP - new load counts[%d]  "
7326                                      "%d, %d, %d\n",
7327                    path, load_count[path][0], load_count[path][1],
7328                    load_count[path][2]);
7329                 if (load_count[path][0] == 0)
7330                         reset_code = FW_MSG_CODE_DRV_UNLOAD_COMMON;
7331                 else if (load_count[path][1 + port] == 0)
7332                         reset_code = FW_MSG_CODE_DRV_UNLOAD_PORT;
7333                 else
7334                         reset_code = FW_MSG_CODE_DRV_UNLOAD_FUNCTION;
7335         }
7336
7337         return reset_code;
7338 }
7339
7340 /**
7341  * bnx2x_send_unload_done - send UNLOAD_DONE command to the MCP.
7342  *
7343  * @bp:         driver handle
7344  */
7345 void bnx2x_send_unload_done(struct bnx2x *bp)
7346 {
7347         /* Report UNLOAD_DONE to MCP */
7348         if (!BP_NOMCP(bp))
7349                 bnx2x_fw_command(bp, DRV_MSG_CODE_UNLOAD_DONE, 0);
7350 }
7351
7352 void bnx2x_chip_cleanup(struct bnx2x *bp, int unload_mode)
7353 {
7354         int port = BP_PORT(bp);
7355         int i, rc;
7356         struct bnx2x_mcast_ramrod_params rparam = {0};
7357         u32 reset_code;
7358
7359         /* Wait until tx fastpath tasks complete */
7360         for_each_tx_queue(bp, i) {
7361                 struct bnx2x_fastpath *fp = &bp->fp[i];
7362
7363                 rc = bnx2x_clean_tx_queue(bp, fp);
7364 #ifdef BNX2X_STOP_ON_ERROR
7365                 if (rc)
7366                         return;
7367 #endif
7368         }
7369
7370         /* Give HW time to discard old tx messages */
7371         usleep_range(1000, 1000);
7372
7373         /* Clean all ETH MACs */
7374         rc = bnx2x_del_all_macs(bp, &bp->fp[0].mac_obj, BNX2X_ETH_MAC, false);
7375         if (rc < 0)
7376                 BNX2X_ERR("Failed to delete all ETH macs: %d\n", rc);
7377
7378         /* Clean up UC list  */
7379         rc = bnx2x_del_all_macs(bp, &bp->fp[0].mac_obj, BNX2X_UC_LIST_MAC,
7380                                 true);
7381         if (rc < 0)
7382                 BNX2X_ERR("Failed to schedule DEL commands for UC MACs list: "
7383                           "%d\n", rc);
7384
7385         /* Disable LLH */
7386         if (!CHIP_IS_E1(bp))
7387                 REG_WR(bp, NIG_REG_LLH0_FUNC_EN + port*8, 0);
7388
7389         /* Set "drop all" (stop Rx).
7390          * We need to take a netif_addr_lock() here in order to prevent
7391          * a race between the completion code and this code.
7392          */
7393         netif_addr_lock_bh(bp->dev);
7394         /* Schedule the rx_mode command */
7395         if (test_bit(BNX2X_FILTER_RX_MODE_PENDING, &bp->sp_state))
7396                 set_bit(BNX2X_FILTER_RX_MODE_SCHED, &bp->sp_state);
7397         else
7398                 bnx2x_set_storm_rx_mode(bp);
7399
7400         /* Cleanup multicast configuration */
7401         rparam.mcast_obj = &bp->mcast_obj;
7402         rc = bnx2x_config_mcast(bp, &rparam, BNX2X_MCAST_CMD_DEL);
7403         if (rc < 0)
7404                 BNX2X_ERR("Failed to send DEL multicast command: %d\n", rc);
7405
7406         netif_addr_unlock_bh(bp->dev);
7407
7408
7409         /* Close multi and leading connections
7410          * Completions for ramrods are collected in a synchronous way
7411          */
7412         for_each_queue(bp, i)
7413                 if (bnx2x_stop_queue(bp, i))
7414 #ifdef BNX2X_STOP_ON_ERROR
7415                         return;
7416 #else
7417                         goto unload_error;
7418 #endif
7419         /* If SP settings didn't get completed so far - something
7420          * very wrong has happen.
7421          */
7422         if (!bnx2x_wait_sp_comp(bp, ~0x0UL))
7423                 BNX2X_ERR("Hmmm... Common slow path ramrods got stuck!\n");
7424
7425 #ifndef BNX2X_STOP_ON_ERROR
7426 unload_error:
7427 #endif
7428         rc = bnx2x_func_stop(bp);
7429         if (rc) {
7430                 BNX2X_ERR("Function stop failed!\n");
7431 #ifdef BNX2X_STOP_ON_ERROR
7432                 return;
7433 #endif
7434         }
7435
7436         /*
7437          * Send the UNLOAD_REQUEST to the MCP. This will return if
7438          * this function should perform FUNC, PORT or COMMON HW
7439          * reset.
7440          */
7441         reset_code = bnx2x_send_unload_req(bp, unload_mode);
7442
7443         /* Disable HW interrupts, NAPI */
7444         bnx2x_netif_stop(bp, 1);
7445
7446         /* Release IRQs */
7447         bnx2x_free_irq(bp);
7448
7449         /* Reset the chip */
7450         rc = bnx2x_reset_hw(bp, reset_code);
7451         if (rc)
7452                 BNX2X_ERR("HW_RESET failed\n");
7453
7454
7455         /* Report UNLOAD_DONE to MCP */
7456         bnx2x_send_unload_done(bp);
7457 }
7458
7459 void bnx2x_disable_close_the_gate(struct bnx2x *bp)
7460 {
7461         u32 val;
7462
7463         DP(NETIF_MSG_HW, "Disabling \"close the gates\"\n");
7464
7465         if (CHIP_IS_E1(bp)) {
7466                 int port = BP_PORT(bp);
7467                 u32 addr = port ? MISC_REG_AEU_MASK_ATTN_FUNC_1 :
7468                         MISC_REG_AEU_MASK_ATTN_FUNC_0;
7469
7470                 val = REG_RD(bp, addr);
7471                 val &= ~(0x300);
7472                 REG_WR(bp, addr, val);
7473         } else {
7474                 val = REG_RD(bp, MISC_REG_AEU_GENERAL_MASK);
7475                 val &= ~(MISC_AEU_GENERAL_MASK_REG_AEU_PXP_CLOSE_MASK |
7476                          MISC_AEU_GENERAL_MASK_REG_AEU_NIG_CLOSE_MASK);
7477                 REG_WR(bp, MISC_REG_AEU_GENERAL_MASK, val);
7478         }
7479 }
7480
7481 /* Close gates #2, #3 and #4: */
7482 static void bnx2x_set_234_gates(struct bnx2x *bp, bool close)
7483 {
7484         u32 val;
7485
7486         /* Gates #2 and #4a are closed/opened for "not E1" only */
7487         if (!CHIP_IS_E1(bp)) {
7488                 /* #4 */
7489                 REG_WR(bp, PXP_REG_HST_DISCARD_DOORBELLS, !!close);
7490                 /* #2 */
7491                 REG_WR(bp, PXP_REG_HST_DISCARD_INTERNAL_WRITES, !!close);
7492         }
7493
7494         /* #3 */
7495         if (CHIP_IS_E1x(bp)) {
7496                 /* Prevent interrupts from HC on both ports */
7497                 val = REG_RD(bp, HC_REG_CONFIG_1);
7498                 REG_WR(bp, HC_REG_CONFIG_1,
7499                        (!close) ? (val | HC_CONFIG_1_REG_BLOCK_DISABLE_1) :
7500                        (val & ~(u32)HC_CONFIG_1_REG_BLOCK_DISABLE_1));
7501
7502                 val = REG_RD(bp, HC_REG_CONFIG_0);
7503                 REG_WR(bp, HC_REG_CONFIG_0,
7504                        (!close) ? (val | HC_CONFIG_0_REG_BLOCK_DISABLE_0) :
7505                        (val & ~(u32)HC_CONFIG_0_REG_BLOCK_DISABLE_0));
7506         } else {
7507                 /* Prevent incomming interrupts in IGU */
7508                 val = REG_RD(bp, IGU_REG_BLOCK_CONFIGURATION);
7509
7510                 REG_WR(bp, IGU_REG_BLOCK_CONFIGURATION,
7511                        (!close) ?
7512                        (val | IGU_BLOCK_CONFIGURATION_REG_BLOCK_ENABLE) :
7513                        (val & ~(u32)IGU_BLOCK_CONFIGURATION_REG_BLOCK_ENABLE));
7514         }
7515
7516         DP(NETIF_MSG_HW, "%s gates #2, #3 and #4\n",
7517                 close ? "closing" : "opening");
7518         mmiowb();
7519 }
7520
7521 #define SHARED_MF_CLP_MAGIC  0x80000000 /* `magic' bit */
7522
7523 static void bnx2x_clp_reset_prep(struct bnx2x *bp, u32 *magic_val)
7524 {
7525         /* Do some magic... */
7526         u32 val = MF_CFG_RD(bp, shared_mf_config.clp_mb);
7527         *magic_val = val & SHARED_MF_CLP_MAGIC;
7528         MF_CFG_WR(bp, shared_mf_config.clp_mb, val | SHARED_MF_CLP_MAGIC);
7529 }
7530
7531 /**
7532  * bnx2x_clp_reset_done - restore the value of the `magic' bit.
7533  *
7534  * @bp:         driver handle
7535  * @magic_val:  old value of the `magic' bit.
7536  */
7537 static void bnx2x_clp_reset_done(struct bnx2x *bp, u32 magic_val)
7538 {
7539         /* Restore the `magic' bit value... */
7540         u32 val = MF_CFG_RD(bp, shared_mf_config.clp_mb);
7541         MF_CFG_WR(bp, shared_mf_config.clp_mb,
7542                 (val & (~SHARED_MF_CLP_MAGIC)) | magic_val);
7543 }
7544
7545 /**
7546  * bnx2x_reset_mcp_prep - prepare for MCP reset.
7547  *
7548  * @bp:         driver handle
7549  * @magic_val:  old value of 'magic' bit.
7550  *
7551  * Takes care of CLP configurations.
7552  */
7553 static void bnx2x_reset_mcp_prep(struct bnx2x *bp, u32 *magic_val)
7554 {
7555         u32 shmem;
7556         u32 validity_offset;
7557
7558         DP(NETIF_MSG_HW, "Starting\n");
7559
7560         /* Set `magic' bit in order to save MF config */
7561         if (!CHIP_IS_E1(bp))
7562                 bnx2x_clp_reset_prep(bp, magic_val);
7563
7564         /* Get shmem offset */
7565         shmem = REG_RD(bp, MISC_REG_SHARED_MEM_ADDR);
7566         validity_offset = offsetof(struct shmem_region, validity_map[0]);
7567
7568         /* Clear validity map flags */
7569         if (shmem > 0)
7570                 REG_WR(bp, shmem + validity_offset, 0);
7571 }
7572
7573 #define MCP_TIMEOUT      5000   /* 5 seconds (in ms) */
7574 #define MCP_ONE_TIMEOUT  100    /* 100 ms */
7575
7576 /**
7577  * bnx2x_mcp_wait_one - wait for MCP_ONE_TIMEOUT
7578  *
7579  * @bp: driver handle
7580  */
7581 static inline void bnx2x_mcp_wait_one(struct bnx2x *bp)
7582 {
7583         /* special handling for emulation and FPGA,
7584            wait 10 times longer */
7585         if (CHIP_REV_IS_SLOW(bp))
7586                 msleep(MCP_ONE_TIMEOUT*10);
7587         else
7588                 msleep(MCP_ONE_TIMEOUT);
7589 }
7590
7591 /*
7592  * initializes bp->common.shmem_base and waits for validity signature to appear
7593  */
7594 static int bnx2x_init_shmem(struct bnx2x *bp)
7595 {
7596         int cnt = 0;
7597         u32 val = 0;
7598
7599         do {
7600                 bp->common.shmem_base = REG_RD(bp, MISC_REG_SHARED_MEM_ADDR);
7601                 if (bp->common.shmem_base) {
7602                         val = SHMEM_RD(bp, validity_map[BP_PORT(bp)]);
7603                         if (val & SHR_MEM_VALIDITY_MB)
7604                                 return 0;
7605                 }
7606
7607                 bnx2x_mcp_wait_one(bp);
7608
7609         } while (cnt++ < (MCP_TIMEOUT / MCP_ONE_TIMEOUT));
7610
7611         BNX2X_ERR("BAD MCP validity signature\n");
7612
7613         return -ENODEV;
7614 }
7615
7616 static int bnx2x_reset_mcp_comp(struct bnx2x *bp, u32 magic_val)
7617 {
7618         int rc = bnx2x_init_shmem(bp);
7619
7620         /* Restore the `magic' bit value */
7621         if (!CHIP_IS_E1(bp))
7622                 bnx2x_clp_reset_done(bp, magic_val);
7623
7624         return rc;
7625 }
7626
7627 static void bnx2x_pxp_prep(struct bnx2x *bp)
7628 {
7629         if (!CHIP_IS_E1(bp)) {
7630                 REG_WR(bp, PXP2_REG_RD_START_INIT, 0);
7631                 REG_WR(bp, PXP2_REG_RQ_RBC_DONE, 0);
7632                 mmiowb();
7633         }
7634 }
7635
7636 /*
7637  * Reset the whole chip except for:
7638  *      - PCIE core
7639  *      - PCI Glue, PSWHST, PXP/PXP2 RF (all controlled by
7640  *              one reset bit)
7641  *      - IGU
7642  *      - MISC (including AEU)
7643  *      - GRC
7644  *      - RBCN, RBCP
7645  */
7646 static void bnx2x_process_kill_chip_reset(struct bnx2x *bp, bool global)
7647 {
7648         u32 not_reset_mask1, reset_mask1, not_reset_mask2, reset_mask2;
7649         u32 global_bits2;
7650
7651         /*
7652          * Bits that have to be set in reset_mask2 if we want to reset 'global'
7653          * (per chip) blocks.
7654          */
7655         global_bits2 =
7656                 MISC_REGISTERS_RESET_REG_2_RST_MCP_N_RESET_CMN_CPU |
7657                 MISC_REGISTERS_RESET_REG_2_RST_MCP_N_RESET_CMN_CORE;
7658
7659         not_reset_mask1 =
7660                 MISC_REGISTERS_RESET_REG_1_RST_HC |
7661                 MISC_REGISTERS_RESET_REG_1_RST_PXPV |
7662                 MISC_REGISTERS_RESET_REG_1_RST_PXP;
7663
7664         not_reset_mask2 =
7665                 MISC_REGISTERS_RESET_REG_2_RST_PCI_MDIO |
7666                 MISC_REGISTERS_RESET_REG_2_RST_EMAC0_HARD_CORE |
7667                 MISC_REGISTERS_RESET_REG_2_RST_EMAC1_HARD_CORE |
7668                 MISC_REGISTERS_RESET_REG_2_RST_MISC_CORE |
7669                 MISC_REGISTERS_RESET_REG_2_RST_RBCN |
7670                 MISC_REGISTERS_RESET_REG_2_RST_GRC  |
7671                 MISC_REGISTERS_RESET_REG_2_RST_MCP_N_RESET_REG_HARD_CORE |
7672                 MISC_REGISTERS_RESET_REG_2_RST_MCP_N_HARD_CORE_RST_B;
7673
7674         reset_mask1 = 0xffffffff;
7675
7676         if (CHIP_IS_E1(bp))
7677                 reset_mask2 = 0xffff;
7678         else
7679                 reset_mask2 = 0x1ffff;
7680
7681         if (CHIP_IS_E3(bp)) {
7682                 reset_mask2 |= MISC_REGISTERS_RESET_REG_2_MSTAT0;
7683                 reset_mask2 |= MISC_REGISTERS_RESET_REG_2_MSTAT1;
7684         }
7685
7686         /* Don't reset global blocks unless we need to */
7687         if (!global)
7688                 reset_mask2 &= ~global_bits2;
7689
7690         /*
7691          * In case of attention in the QM, we need to reset PXP
7692          * (MISC_REGISTERS_RESET_REG_2_RST_PXP_RQ_RD_WR) before QM
7693          * because otherwise QM reset would release 'close the gates' shortly
7694          * before resetting the PXP, then the PSWRQ would send a write
7695          * request to PGLUE. Then when PXP is reset, PGLUE would try to
7696          * read the payload data from PSWWR, but PSWWR would not
7697          * respond. The write queue in PGLUE would stuck, dmae commands
7698          * would not return. Therefore it's important to reset the second
7699          * reset register (containing the
7700          * MISC_REGISTERS_RESET_REG_2_RST_PXP_RQ_RD_WR bit) before the
7701          * first one (containing the MISC_REGISTERS_RESET_REG_1_RST_QM
7702          * bit).
7703          */
7704         REG_WR(bp, GRCBASE_MISC + MISC_REGISTERS_RESET_REG_2_CLEAR,
7705                reset_mask2 & (~not_reset_mask2));
7706
7707         REG_WR(bp, GRCBASE_MISC + MISC_REGISTERS_RESET_REG_1_CLEAR,
7708                reset_mask1 & (~not_reset_mask1));
7709
7710         barrier();
7711         mmiowb();
7712
7713         REG_WR(bp, GRCBASE_MISC + MISC_REGISTERS_RESET_REG_2_SET, reset_mask2);
7714         REG_WR(bp, GRCBASE_MISC + MISC_REGISTERS_RESET_REG_1_SET, reset_mask1);
7715         mmiowb();
7716 }
7717
7718 /**
7719  * bnx2x_er_poll_igu_vq - poll for pending writes bit.
7720  * It should get cleared in no more than 1s.
7721  *
7722  * @bp: driver handle
7723  *
7724  * It should get cleared in no more than 1s. Returns 0 if
7725  * pending writes bit gets cleared.
7726  */
7727 static int bnx2x_er_poll_igu_vq(struct bnx2x *bp)
7728 {
7729         u32 cnt = 1000;
7730         u32 pend_bits = 0;
7731
7732         do {
7733                 pend_bits  = REG_RD(bp, IGU_REG_PENDING_BITS_STATUS);
7734
7735                 if (pend_bits == 0)
7736                         break;
7737
7738                 usleep_range(1000, 1000);
7739         } while (cnt-- > 0);
7740
7741         if (cnt <= 0) {
7742                 BNX2X_ERR("Still pending IGU requests pend_bits=%x!\n",
7743                           pend_bits);
7744                 return -EBUSY;
7745         }
7746
7747         return 0;
7748 }
7749
7750 static int bnx2x_process_kill(struct bnx2x *bp, bool global)
7751 {
7752         int cnt = 1000;
7753         u32 val = 0;
7754         u32 sr_cnt, blk_cnt, port_is_idle_0, port_is_idle_1, pgl_exp_rom2;
7755
7756
7757         /* Empty the Tetris buffer, wait for 1s */
7758         do {
7759                 sr_cnt  = REG_RD(bp, PXP2_REG_RD_SR_CNT);
7760                 blk_cnt = REG_RD(bp, PXP2_REG_RD_BLK_CNT);
7761                 port_is_idle_0 = REG_RD(bp, PXP2_REG_RD_PORT_IS_IDLE_0);
7762                 port_is_idle_1 = REG_RD(bp, PXP2_REG_RD_PORT_IS_IDLE_1);
7763                 pgl_exp_rom2 = REG_RD(bp, PXP2_REG_PGL_EXP_ROM2);
7764                 if ((sr_cnt == 0x7e) && (blk_cnt == 0xa0) &&
7765                     ((port_is_idle_0 & 0x1) == 0x1) &&
7766                     ((port_is_idle_1 & 0x1) == 0x1) &&
7767                     (pgl_exp_rom2 == 0xffffffff))
7768                         break;
7769                 usleep_range(1000, 1000);
7770         } while (cnt-- > 0);
7771
7772         if (cnt <= 0) {
7773                 DP(NETIF_MSG_HW, "Tetris buffer didn't get empty or there"
7774                           " are still"
7775                           " outstanding read requests after 1s!\n");
7776                 DP(NETIF_MSG_HW, "sr_cnt=0x%08x, blk_cnt=0x%08x,"
7777                           " port_is_idle_0=0x%08x,"
7778                           " port_is_idle_1=0x%08x, pgl_exp_rom2=0x%08x\n",
7779                           sr_cnt, blk_cnt, port_is_idle_0, port_is_idle_1,
7780                           pgl_exp_rom2);
7781                 return -EAGAIN;
7782         }
7783
7784         barrier();
7785
7786         /* Close gates #2, #3 and #4 */
7787         bnx2x_set_234_gates(bp, true);
7788
7789         /* Poll for IGU VQs for 57712 and newer chips */
7790         if (!CHIP_IS_E1x(bp) && bnx2x_er_poll_igu_vq(bp))
7791                 return -EAGAIN;
7792
7793
7794         /* TBD: Indicate that "process kill" is in progress to MCP */
7795
7796         /* Clear "unprepared" bit */
7797         REG_WR(bp, MISC_REG_UNPREPARED, 0);
7798         barrier();
7799
7800         /* Make sure all is written to the chip before the reset */
7801         mmiowb();
7802
7803         /* Wait for 1ms to empty GLUE and PCI-E core queues,
7804          * PSWHST, GRC and PSWRD Tetris buffer.
7805          */
7806         usleep_range(1000, 1000);
7807
7808         /* Prepare to chip reset: */
7809         /* MCP */
7810         if (global)
7811                 bnx2x_reset_mcp_prep(bp, &val);
7812
7813         /* PXP */
7814         bnx2x_pxp_prep(bp);
7815         barrier();
7816
7817         /* reset the chip */
7818         bnx2x_process_kill_chip_reset(bp, global);
7819         barrier();
7820
7821         /* Recover after reset: */
7822         /* MCP */
7823         if (global && bnx2x_reset_mcp_comp(bp, val))
7824                 return -EAGAIN;
7825
7826         /* TBD: Add resetting the NO_MCP mode DB here */
7827
7828         /* PXP */
7829         bnx2x_pxp_prep(bp);
7830
7831         /* Open the gates #2, #3 and #4 */
7832         bnx2x_set_234_gates(bp, false);
7833
7834         /* TBD: IGU/AEU preparation bring back the AEU/IGU to a
7835          * reset state, re-enable attentions. */
7836
7837         return 0;
7838 }
7839
7840 int bnx2x_leader_reset(struct bnx2x *bp)
7841 {
7842         int rc = 0;
7843         bool global = bnx2x_reset_is_global(bp);
7844
7845         /* Try to recover after the failure */
7846         if (bnx2x_process_kill(bp, global)) {
7847                 netdev_err(bp->dev, "Something bad had happen on engine %d! "
7848                                     "Aii!\n", BP_PATH(bp));
7849                 rc = -EAGAIN;
7850                 goto exit_leader_reset;
7851         }
7852
7853         /*
7854          * Clear RESET_IN_PROGRES and RESET_GLOBAL bits and update the driver
7855          * state.
7856          */
7857         bnx2x_set_reset_done(bp);
7858         if (global)
7859                 bnx2x_clear_reset_global(bp);
7860
7861 exit_leader_reset:
7862         bp->is_leader = 0;
7863         bnx2x_release_leader_lock(bp);
7864         smp_mb();
7865         return rc;
7866 }
7867
7868 static inline void bnx2x_recovery_failed(struct bnx2x *bp)
7869 {
7870         netdev_err(bp->dev, "Recovery has failed. Power cycle is needed.\n");
7871
7872         /* Disconnect this device */
7873         netif_device_detach(bp->dev);
7874
7875         /*
7876          * Block ifup for all function on this engine until "process kill"
7877          * or power cycle.
7878          */
7879         bnx2x_set_reset_in_progress(bp);
7880
7881         /* Shut down the power */
7882         bnx2x_set_power_state(bp, PCI_D3hot);
7883
7884         bp->recovery_state = BNX2X_RECOVERY_FAILED;
7885
7886         smp_mb();
7887 }
7888
7889 /*
7890  * Assumption: runs under rtnl lock. This together with the fact
7891  * that it's called only from bnx2x_reset_task() ensure that it
7892  * will never be called when netif_running(bp->dev) is false.
7893  */
7894 static void bnx2x_parity_recover(struct bnx2x *bp)
7895 {
7896         bool global = false;
7897
7898         DP(NETIF_MSG_HW, "Handling parity\n");
7899         while (1) {
7900                 switch (bp->recovery_state) {
7901                 case BNX2X_RECOVERY_INIT:
7902                         DP(NETIF_MSG_HW, "State is BNX2X_RECOVERY_INIT\n");
7903                         bnx2x_chk_parity_attn(bp, &global, false);
7904
7905                         /* Try to get a LEADER_LOCK HW lock */
7906                         if (bnx2x_trylock_leader_lock(bp)) {
7907                                 bnx2x_set_reset_in_progress(bp);
7908                                 /*
7909                                  * Check if there is a global attention and if
7910                                  * there was a global attention, set the global
7911                                  * reset bit.
7912                                  */
7913
7914                                 if (global)
7915                                         bnx2x_set_reset_global(bp);
7916
7917                                 bp->is_leader = 1;
7918                         }
7919
7920                         /* Stop the driver */
7921                         /* If interface has been removed - break */
7922                         if (bnx2x_nic_unload(bp, UNLOAD_RECOVERY))
7923                                 return;
7924
7925                         bp->recovery_state = BNX2X_RECOVERY_WAIT;
7926
7927                         /*
7928                          * Reset MCP command sequence number and MCP mail box
7929                          * sequence as we are going to reset the MCP.
7930                          */
7931                         if (global) {
7932                                 bp->fw_seq = 0;
7933                                 bp->fw_drv_pulse_wr_seq = 0;
7934                         }
7935
7936                         /* Ensure "is_leader", MCP command sequence and
7937                          * "recovery_state" update values are seen on other
7938                          * CPUs.
7939                          */
7940                         smp_mb();
7941                         break;
7942
7943                 case BNX2X_RECOVERY_WAIT:
7944                         DP(NETIF_MSG_HW, "State is BNX2X_RECOVERY_WAIT\n");
7945                         if (bp->is_leader) {
7946                                 int other_engine = BP_PATH(bp) ? 0 : 1;
7947                                 u32 other_load_counter =
7948                                         bnx2x_get_load_cnt(bp, other_engine);
7949                                 u32 load_counter =
7950                                         bnx2x_get_load_cnt(bp, BP_PATH(bp));
7951                                 global = bnx2x_reset_is_global(bp);
7952
7953                                 /*
7954                                  * In case of a parity in a global block, let
7955                                  * the first leader that performs a
7956                                  * leader_reset() reset the global blocks in
7957                                  * order to clear global attentions. Otherwise
7958                                  * the the gates will remain closed for that
7959                                  * engine.
7960                                  */
7961                                 if (load_counter ||
7962                                     (global && other_load_counter)) {
7963                                         /* Wait until all other functions get
7964                                          * down.
7965                                          */
7966                                         schedule_delayed_work(&bp->reset_task,
7967                                                                 HZ/10);
7968                                         return;
7969                                 } else {
7970                                         /* If all other functions got down -
7971                                          * try to bring the chip back to
7972                                          * normal. In any case it's an exit
7973                                          * point for a leader.
7974                                          */
7975                                         if (bnx2x_leader_reset(bp)) {
7976                                                 bnx2x_recovery_failed(bp);
7977                                                 return;
7978                                         }
7979
7980                                         /* If we are here, means that the
7981                                          * leader has succeeded and doesn't
7982                                          * want to be a leader any more. Try
7983                                          * to continue as a none-leader.
7984                                          */
7985                                         break;
7986                                 }
7987                         } else { /* non-leader */
7988                                 if (!bnx2x_reset_is_done(bp, BP_PATH(bp))) {
7989                                         /* Try to get a LEADER_LOCK HW lock as
7990                                          * long as a former leader may have
7991                                          * been unloaded by the user or
7992                                          * released a leadership by another
7993                                          * reason.
7994                                          */
7995                                         if (bnx2x_trylock_leader_lock(bp)) {
7996                                                 /* I'm a leader now! Restart a
7997                                                  * switch case.
7998                                                  */
7999                                                 bp->is_leader = 1;
8000                                                 break;
8001                                         }
8002
8003                                         schedule_delayed_work(&bp->reset_task,
8004                                                                 HZ/10);
8005                                         return;
8006
8007                                 } else {
8008                                         /*
8009                                          * If there was a global attention, wait
8010                                          * for it to be cleared.
8011                                          */
8012                                         if (bnx2x_reset_is_global(bp)) {
8013                                                 schedule_delayed_work(
8014                                                         &bp->reset_task, HZ/10);
8015                                                 return;
8016                                         }
8017
8018                                         if (bnx2x_nic_load(bp, LOAD_NORMAL))
8019                                                 bnx2x_recovery_failed(bp);
8020                                         else {
8021                                                 bp->recovery_state =
8022                                                         BNX2X_RECOVERY_DONE;
8023                                                 smp_mb();
8024                                         }
8025
8026                                         return;
8027                                 }
8028                         }
8029                 default:
8030                         return;
8031                 }
8032         }
8033 }
8034
8035 /* bnx2x_nic_unload() flushes the bnx2x_wq, thus reset task is
8036  * scheduled on a general queue in order to prevent a dead lock.
8037  */
8038 static void bnx2x_reset_task(struct work_struct *work)
8039 {
8040         struct bnx2x *bp = container_of(work, struct bnx2x, reset_task.work);
8041
8042 #ifdef BNX2X_STOP_ON_ERROR
8043         BNX2X_ERR("reset task called but STOP_ON_ERROR defined"
8044                   " so reset not done to allow debug dump,\n"
8045          KERN_ERR " you will need to reboot when done\n");
8046         return;
8047 #endif
8048
8049         rtnl_lock();
8050
8051         if (!netif_running(bp->dev))
8052                 goto reset_task_exit;
8053
8054         if (unlikely(bp->recovery_state != BNX2X_RECOVERY_DONE))
8055                 bnx2x_parity_recover(bp);
8056         else {
8057                 bnx2x_nic_unload(bp, UNLOAD_NORMAL);
8058                 bnx2x_nic_load(bp, LOAD_NORMAL);
8059         }
8060
8061 reset_task_exit:
8062         rtnl_unlock();
8063 }
8064
8065 /* end of nic load/unload */
8066
8067 static void bnx2x_period_task(struct work_struct *work)
8068 {
8069         struct bnx2x *bp = container_of(work, struct bnx2x, period_task.work);
8070
8071         if (!netif_running(bp->dev))
8072                 goto period_task_exit;
8073
8074         if (CHIP_REV_IS_SLOW(bp)) {
8075                 BNX2X_ERR("period task called on emulation, ignoring\n");
8076                 goto period_task_exit;
8077         }
8078
8079         bnx2x_acquire_phy_lock(bp);
8080         /*
8081          * The barrier is needed to ensure the ordering between the writing to
8082          * the bp->port.pmf in the bnx2x_nic_load() or bnx2x_pmf_update() and
8083          * the reading here.
8084          */
8085         smp_mb();
8086         if (bp->port.pmf) {
8087                 bnx2x_period_func(&bp->link_params, &bp->link_vars);
8088
8089                 /* Re-queue task in 1 sec */
8090                 queue_delayed_work(bnx2x_wq, &bp->period_task, 1*HZ);
8091         }
8092
8093         bnx2x_release_phy_lock(bp);
8094 period_task_exit:
8095         return;
8096 }
8097
8098 /*
8099  * Init service functions
8100  */
8101
8102 static u32 bnx2x_get_pretend_reg(struct bnx2x *bp)
8103 {
8104         u32 base = PXP2_REG_PGL_PRETEND_FUNC_F0;
8105         u32 stride = PXP2_REG_PGL_PRETEND_FUNC_F1 - base;
8106         return base + (BP_ABS_FUNC(bp)) * stride;
8107 }
8108
8109 static void bnx2x_undi_int_disable_e1h(struct bnx2x *bp)
8110 {
8111         u32 reg = bnx2x_get_pretend_reg(bp);
8112
8113         /* Flush all outstanding writes */
8114         mmiowb();
8115
8116         /* Pretend to be function 0 */
8117         REG_WR(bp, reg, 0);
8118         REG_RD(bp, reg);        /* Flush the GRC transaction (in the chip) */
8119
8120         /* From now we are in the "like-E1" mode */
8121         bnx2x_int_disable(bp);
8122
8123         /* Flush all outstanding writes */
8124         mmiowb();
8125
8126         /* Restore the original function */
8127         REG_WR(bp, reg, BP_ABS_FUNC(bp));
8128         REG_RD(bp, reg);
8129 }
8130
8131 static inline void bnx2x_undi_int_disable(struct bnx2x *bp)
8132 {
8133         if (CHIP_IS_E1(bp))
8134                 bnx2x_int_disable(bp);
8135         else
8136                 bnx2x_undi_int_disable_e1h(bp);
8137 }
8138
8139 static void __devinit bnx2x_undi_unload(struct bnx2x *bp)
8140 {
8141         u32 val;
8142
8143         /* Check if there is any driver already loaded */
8144         val = REG_RD(bp, MISC_REG_UNPREPARED);
8145         if (val == 0x1) {
8146                 /* Check if it is the UNDI driver
8147                  * UNDI driver initializes CID offset for normal bell to 0x7
8148                  */
8149                 bnx2x_acquire_hw_lock(bp, HW_LOCK_RESOURCE_UNDI);
8150                 val = REG_RD(bp, DORQ_REG_NORM_CID_OFST);
8151                 if (val == 0x7) {
8152                         u32 reset_code = DRV_MSG_CODE_UNLOAD_REQ_WOL_DIS;
8153                         /* save our pf_num */
8154                         int orig_pf_num = bp->pf_num;
8155                         int port;
8156                         u32 swap_en, swap_val, value;
8157
8158                         /* clear the UNDI indication */
8159                         REG_WR(bp, DORQ_REG_NORM_CID_OFST, 0);
8160
8161                         BNX2X_DEV_INFO("UNDI is active! reset device\n");
8162
8163                         /* try unload UNDI on port 0 */
8164                         bp->pf_num = 0;
8165                         bp->fw_seq =
8166                               (SHMEM_RD(bp, func_mb[bp->pf_num].drv_mb_header) &
8167                                 DRV_MSG_SEQ_NUMBER_MASK);
8168                         reset_code = bnx2x_fw_command(bp, reset_code, 0);
8169
8170                         /* if UNDI is loaded on the other port */
8171                         if (reset_code != FW_MSG_CODE_DRV_UNLOAD_COMMON) {
8172
8173                                 /* send "DONE" for previous unload */
8174                                 bnx2x_fw_command(bp,
8175                                                  DRV_MSG_CODE_UNLOAD_DONE, 0);
8176
8177                                 /* unload UNDI on port 1 */
8178                                 bp->pf_num = 1;
8179                                 bp->fw_seq =
8180                               (SHMEM_RD(bp, func_mb[bp->pf_num].drv_mb_header) &
8181                                         DRV_MSG_SEQ_NUMBER_MASK);
8182                                 reset_code = DRV_MSG_CODE_UNLOAD_REQ_WOL_DIS;
8183
8184                                 bnx2x_fw_command(bp, reset_code, 0);
8185                         }
8186
8187                         /* now it's safe to release the lock */
8188                         bnx2x_release_hw_lock(bp, HW_LOCK_RESOURCE_UNDI);
8189
8190                         bnx2x_undi_int_disable(bp);
8191                         port = BP_PORT(bp);
8192
8193                         /* close input traffic and wait for it */
8194                         /* Do not rcv packets to BRB */
8195                         REG_WR(bp, (port ? NIG_REG_LLH1_BRB1_DRV_MASK :
8196                                            NIG_REG_LLH0_BRB1_DRV_MASK), 0x0);
8197                         /* Do not direct rcv packets that are not for MCP to
8198                          * the BRB */
8199                         REG_WR(bp, (port ? NIG_REG_LLH1_BRB1_NOT_MCP :
8200                                            NIG_REG_LLH0_BRB1_NOT_MCP), 0x0);
8201                         /* clear AEU */
8202                         REG_WR(bp, (port ? MISC_REG_AEU_MASK_ATTN_FUNC_1 :
8203                                            MISC_REG_AEU_MASK_ATTN_FUNC_0), 0);
8204                         msleep(10);
8205
8206                         /* save NIG port swap info */
8207                         swap_val = REG_RD(bp, NIG_REG_PORT_SWAP);
8208                         swap_en = REG_RD(bp, NIG_REG_STRAP_OVERRIDE);
8209                         /* reset device */
8210                         REG_WR(bp,
8211                                GRCBASE_MISC + MISC_REGISTERS_RESET_REG_1_CLEAR,
8212                                0xd3ffffff);
8213
8214                         value = 0x1400;
8215                         if (CHIP_IS_E3(bp)) {
8216                                 value |= MISC_REGISTERS_RESET_REG_2_MSTAT0;
8217                                 value |= MISC_REGISTERS_RESET_REG_2_MSTAT1;
8218                         }
8219
8220                         REG_WR(bp,
8221                                GRCBASE_MISC + MISC_REGISTERS_RESET_REG_2_CLEAR,
8222                                value);
8223
8224                         /* take the NIG out of reset and restore swap values */
8225                         REG_WR(bp,
8226                                GRCBASE_MISC + MISC_REGISTERS_RESET_REG_1_SET,
8227                                MISC_REGISTERS_RESET_REG_1_RST_NIG);
8228                         REG_WR(bp, NIG_REG_PORT_SWAP, swap_val);
8229                         REG_WR(bp, NIG_REG_STRAP_OVERRIDE, swap_en);
8230
8231                         /* send unload done to the MCP */
8232                         bnx2x_fw_command(bp, DRV_MSG_CODE_UNLOAD_DONE, 0);
8233
8234                         /* restore our func and fw_seq */
8235                         bp->pf_num = orig_pf_num;
8236                         bp->fw_seq =
8237                               (SHMEM_RD(bp, func_mb[bp->pf_num].drv_mb_header) &
8238                                 DRV_MSG_SEQ_NUMBER_MASK);
8239                 } else
8240                         bnx2x_release_hw_lock(bp, HW_LOCK_RESOURCE_UNDI);
8241         }
8242 }
8243
8244 static void __devinit bnx2x_get_common_hwinfo(struct bnx2x *bp)
8245 {
8246         u32 val, val2, val3, val4, id;
8247         u16 pmc;
8248
8249         /* Get the chip revision id and number. */
8250         /* chip num:16-31, rev:12-15, metal:4-11, bond_id:0-3 */
8251         val = REG_RD(bp, MISC_REG_CHIP_NUM);
8252         id = ((val & 0xffff) << 16);
8253         val = REG_RD(bp, MISC_REG_CHIP_REV);
8254         id |= ((val & 0xf) << 12);
8255         val = REG_RD(bp, MISC_REG_CHIP_METAL);
8256         id |= ((val & 0xff) << 4);
8257         val = REG_RD(bp, MISC_REG_BOND_ID);
8258         id |= (val & 0xf);
8259         bp->common.chip_id = id;
8260
8261         /* Set doorbell size */
8262         bp->db_size = (1 << BNX2X_DB_SHIFT);
8263
8264         if (!CHIP_IS_E1x(bp)) {
8265                 val = REG_RD(bp, MISC_REG_PORT4MODE_EN_OVWR);
8266                 if ((val & 1) == 0)
8267                         val = REG_RD(bp, MISC_REG_PORT4MODE_EN);
8268                 else
8269                         val = (val >> 1) & 1;
8270                 BNX2X_DEV_INFO("chip is in %s\n", val ? "4_PORT_MODE" :
8271                                                        "2_PORT_MODE");
8272                 bp->common.chip_port_mode = val ? CHIP_4_PORT_MODE :
8273                                                  CHIP_2_PORT_MODE;
8274
8275                 if (CHIP_MODE_IS_4_PORT(bp))
8276                         bp->pfid = (bp->pf_num >> 1);   /* 0..3 */
8277                 else
8278                         bp->pfid = (bp->pf_num & 0x6);  /* 0, 2, 4, 6 */
8279         } else {
8280                 bp->common.chip_port_mode = CHIP_PORT_MODE_NONE; /* N/A */
8281                 bp->pfid = bp->pf_num;                  /* 0..7 */
8282         }
8283
8284         bp->link_params.chip_id = bp->common.chip_id;
8285         BNX2X_DEV_INFO("chip ID is 0x%x\n", id);
8286
8287         val = (REG_RD(bp, 0x2874) & 0x55);
8288         if ((bp->common.chip_id & 0x1) ||
8289             (CHIP_IS_E1(bp) && val) || (CHIP_IS_E1H(bp) && (val == 0x55))) {
8290                 bp->flags |= ONE_PORT_FLAG;
8291                 BNX2X_DEV_INFO("single port device\n");
8292         }
8293
8294         val = REG_RD(bp, MCP_REG_MCPR_NVM_CFG4);
8295         bp->common.flash_size = (BNX2X_NVRAM_1MB_SIZE <<
8296                                  (val & MCPR_NVM_CFG4_FLASH_SIZE));
8297         BNX2X_DEV_INFO("flash_size 0x%x (%d)\n",
8298                        bp->common.flash_size, bp->common.flash_size);
8299
8300         bnx2x_init_shmem(bp);
8301
8302
8303
8304         bp->common.shmem2_base = REG_RD(bp, (BP_PATH(bp) ?
8305                                         MISC_REG_GENERIC_CR_1 :
8306                                         MISC_REG_GENERIC_CR_0));
8307
8308         bp->link_params.shmem_base = bp->common.shmem_base;
8309         bp->link_params.shmem2_base = bp->common.shmem2_base;
8310         BNX2X_DEV_INFO("shmem offset 0x%x  shmem2 offset 0x%x\n",
8311                        bp->common.shmem_base, bp->common.shmem2_base);
8312
8313         if (!bp->common.shmem_base) {
8314                 BNX2X_DEV_INFO("MCP not active\n");
8315                 bp->flags |= NO_MCP_FLAG;
8316                 return;
8317         }
8318
8319         bp->common.hw_config = SHMEM_RD(bp, dev_info.shared_hw_config.config);
8320         BNX2X_DEV_INFO("hw_config 0x%08x\n", bp->common.hw_config);
8321
8322         bp->link_params.hw_led_mode = ((bp->common.hw_config &
8323                                         SHARED_HW_CFG_LED_MODE_MASK) >>
8324                                        SHARED_HW_CFG_LED_MODE_SHIFT);
8325
8326         bp->link_params.feature_config_flags = 0;
8327         val = SHMEM_RD(bp, dev_info.shared_feature_config.config);
8328         if (val & SHARED_FEAT_CFG_OVERRIDE_PREEMPHASIS_CFG_ENABLED)
8329                 bp->link_params.feature_config_flags |=
8330                                 FEATURE_CONFIG_OVERRIDE_PREEMPHASIS_ENABLED;
8331         else
8332                 bp->link_params.feature_config_flags &=
8333                                 ~FEATURE_CONFIG_OVERRIDE_PREEMPHASIS_ENABLED;
8334
8335         val = SHMEM_RD(bp, dev_info.bc_rev) >> 8;
8336         bp->common.bc_ver = val;
8337         BNX2X_DEV_INFO("bc_ver %X\n", val);
8338         if (val < BNX2X_BC_VER) {
8339                 /* for now only warn
8340                  * later we might need to enforce this */
8341                 BNX2X_ERR("This driver needs bc_ver %X but found %X, "
8342                           "please upgrade BC\n", BNX2X_BC_VER, val);
8343         }
8344         bp->link_params.feature_config_flags |=
8345                                 (val >= REQ_BC_VER_4_VRFY_FIRST_PHY_OPT_MDL) ?
8346                                 FEATURE_CONFIG_BC_SUPPORTS_OPT_MDL_VRFY : 0;
8347
8348         bp->link_params.feature_config_flags |=
8349                 (val >= REQ_BC_VER_4_VRFY_SPECIFIC_PHY_OPT_MDL) ?
8350                 FEATURE_CONFIG_BC_SUPPORTS_DUAL_PHY_OPT_MDL_VRFY : 0;
8351
8352         pci_read_config_word(bp->pdev, bp->pm_cap + PCI_PM_PMC, &pmc);
8353         bp->flags |= (pmc & PCI_PM_CAP_PME_D3cold) ? 0 : NO_WOL_FLAG;
8354
8355         BNX2X_DEV_INFO("%sWoL capable\n",
8356                        (bp->flags & NO_WOL_FLAG) ? "not " : "");
8357
8358         val = SHMEM_RD(bp, dev_info.shared_hw_config.part_num);
8359         val2 = SHMEM_RD(bp, dev_info.shared_hw_config.part_num[4]);
8360         val3 = SHMEM_RD(bp, dev_info.shared_hw_config.part_num[8]);
8361         val4 = SHMEM_RD(bp, dev_info.shared_hw_config.part_num[12]);
8362
8363         dev_info(&bp->pdev->dev, "part number %X-%X-%X-%X\n",
8364                  val, val2, val3, val4);
8365 }
8366
8367 #define IGU_FID(val)    GET_FIELD((val), IGU_REG_MAPPING_MEMORY_FID)
8368 #define IGU_VEC(val)    GET_FIELD((val), IGU_REG_MAPPING_MEMORY_VECTOR)
8369
8370 static void __devinit bnx2x_get_igu_cam_info(struct bnx2x *bp)
8371 {
8372         int pfid = BP_FUNC(bp);
8373         int vn = BP_E1HVN(bp);
8374         int igu_sb_id;
8375         u32 val;
8376         u8 fid;
8377
8378         bp->igu_base_sb = 0xff;
8379         bp->igu_sb_cnt = 0;
8380         if (CHIP_INT_MODE_IS_BC(bp)) {
8381                 bp->igu_sb_cnt = min_t(u8, FP_SB_MAX_E1x,
8382                                        NUM_IGU_SB_REQUIRED(bp->l2_cid_count));
8383
8384                 bp->igu_base_sb = (CHIP_MODE_IS_4_PORT(bp) ? pfid : vn) *
8385                         FP_SB_MAX_E1x;
8386
8387                 bp->igu_dsb_id =  E1HVN_MAX * FP_SB_MAX_E1x +
8388                         (CHIP_MODE_IS_4_PORT(bp) ? pfid : vn);
8389
8390                 return;
8391         }
8392
8393         /* IGU in normal mode - read CAM */
8394         for (igu_sb_id = 0; igu_sb_id < IGU_REG_MAPPING_MEMORY_SIZE;
8395              igu_sb_id++) {
8396                 val = REG_RD(bp, IGU_REG_MAPPING_MEMORY + igu_sb_id * 4);
8397                 if (!(val & IGU_REG_MAPPING_MEMORY_VALID))
8398                         continue;
8399                 fid = IGU_FID(val);
8400                 if ((fid & IGU_FID_ENCODE_IS_PF)) {
8401                         if ((fid & IGU_FID_PF_NUM_MASK) != pfid)
8402                                 continue;
8403                         if (IGU_VEC(val) == 0)
8404                                 /* default status block */
8405                                 bp->igu_dsb_id = igu_sb_id;
8406                         else {
8407                                 if (bp->igu_base_sb == 0xff)
8408                                         bp->igu_base_sb = igu_sb_id;
8409                                 bp->igu_sb_cnt++;
8410                         }
8411                 }
8412         }
8413
8414         /* It's expected that number of CAM entries for this
8415          * functions is equal to the MSI-X table size (which was a
8416          * used during bp->l2_cid_count value calculation.
8417          * We want a harsh warning if these values are different!
8418          */
8419         WARN_ON(bp->igu_sb_cnt != NUM_IGU_SB_REQUIRED(bp->l2_cid_count));
8420
8421         if (bp->igu_sb_cnt == 0)
8422                 BNX2X_ERR("CAM configuration error\n");
8423 }
8424
8425 static void __devinit bnx2x_link_settings_supported(struct bnx2x *bp,
8426                                                     u32 switch_cfg)
8427 {
8428         int cfg_size = 0, idx, port = BP_PORT(bp);
8429
8430         /* Aggregation of supported attributes of all external phys */
8431         bp->port.supported[0] = 0;
8432         bp->port.supported[1] = 0;
8433         switch (bp->link_params.num_phys) {
8434         case 1:
8435                 bp->port.supported[0] = bp->link_params.phy[INT_PHY].supported;
8436                 cfg_size = 1;
8437                 break;
8438         case 2:
8439                 bp->port.supported[0] = bp->link_params.phy[EXT_PHY1].supported;
8440                 cfg_size = 1;
8441                 break;
8442         case 3:
8443                 if (bp->link_params.multi_phy_config &
8444                     PORT_HW_CFG_PHY_SWAPPED_ENABLED) {
8445                         bp->port.supported[1] =
8446                                 bp->link_params.phy[EXT_PHY1].supported;
8447                         bp->port.supported[0] =
8448                                 bp->link_params.phy[EXT_PHY2].supported;
8449                 } else {
8450                         bp->port.supported[0] =
8451                                 bp->link_params.phy[EXT_PHY1].supported;
8452                         bp->port.supported[1] =
8453                                 bp->link_params.phy[EXT_PHY2].supported;
8454                 }
8455                 cfg_size = 2;
8456                 break;
8457         }
8458
8459         if (!(bp->port.supported[0] || bp->port.supported[1])) {
8460                 BNX2X_ERR("NVRAM config error. BAD phy config."
8461                           "PHY1 config 0x%x, PHY2 config 0x%x\n",
8462                            SHMEM_RD(bp,
8463                            dev_info.port_hw_config[port].external_phy_config),
8464                            SHMEM_RD(bp,
8465                            dev_info.port_hw_config[port].external_phy_config2));
8466                         return;
8467         }
8468
8469         if (CHIP_IS_E3(bp))
8470                 bp->port.phy_addr = REG_RD(bp, MISC_REG_WC0_CTRL_PHY_ADDR);
8471         else {
8472                 switch (switch_cfg) {
8473                 case SWITCH_CFG_1G:
8474                         bp->port.phy_addr = REG_RD(
8475                                 bp, NIG_REG_SERDES0_CTRL_PHY_ADDR + port*0x10);
8476                         break;
8477                 case SWITCH_CFG_10G:
8478                         bp->port.phy_addr = REG_RD(
8479                                 bp, NIG_REG_XGXS0_CTRL_PHY_ADDR + port*0x18);
8480                         break;
8481                 default:
8482                         BNX2X_ERR("BAD switch_cfg link_config 0x%x\n",
8483                                   bp->port.link_config[0]);
8484                         return;
8485                 }
8486         }
8487         BNX2X_DEV_INFO("phy_addr 0x%x\n", bp->port.phy_addr);
8488         /* mask what we support according to speed_cap_mask per configuration */
8489         for (idx = 0; idx < cfg_size; idx++) {
8490                 if (!(bp->link_params.speed_cap_mask[idx] &
8491                                 PORT_HW_CFG_SPEED_CAPABILITY_D0_10M_HALF))
8492                         bp->port.supported[idx] &= ~SUPPORTED_10baseT_Half;
8493
8494                 if (!(bp->link_params.speed_cap_mask[idx] &
8495                                 PORT_HW_CFG_SPEED_CAPABILITY_D0_10M_FULL))
8496                         bp->port.supported[idx] &= ~SUPPORTED_10baseT_Full;
8497
8498                 if (!(bp->link_params.speed_cap_mask[idx] &
8499                                 PORT_HW_CFG_SPEED_CAPABILITY_D0_100M_HALF))
8500                         bp->port.supported[idx] &= ~SUPPORTED_100baseT_Half;
8501
8502                 if (!(bp->link_params.speed_cap_mask[idx] &
8503                                 PORT_HW_CFG_SPEED_CAPABILITY_D0_100M_FULL))
8504                         bp->port.supported[idx] &= ~SUPPORTED_100baseT_Full;
8505
8506                 if (!(bp->link_params.speed_cap_mask[idx] &
8507                                         PORT_HW_CFG_SPEED_CAPABILITY_D0_1G))
8508                         bp->port.supported[idx] &= ~(SUPPORTED_1000baseT_Half |
8509                                                      SUPPORTED_1000baseT_Full);
8510
8511                 if (!(bp->link_params.speed_cap_mask[idx] &
8512                                         PORT_HW_CFG_SPEED_CAPABILITY_D0_2_5G))
8513                         bp->port.supported[idx] &= ~SUPPORTED_2500baseX_Full;
8514
8515                 if (!(bp->link_params.speed_cap_mask[idx] &
8516                                         PORT_HW_CFG_SPEED_CAPABILITY_D0_10G))
8517                         bp->port.supported[idx] &= ~SUPPORTED_10000baseT_Full;
8518
8519         }
8520
8521         BNX2X_DEV_INFO("supported 0x%x 0x%x\n", bp->port.supported[0],
8522                        bp->port.supported[1]);
8523 }
8524
8525 static void __devinit bnx2x_link_settings_requested(struct bnx2x *bp)
8526 {
8527         u32 link_config, idx, cfg_size = 0;
8528         bp->port.advertising[0] = 0;
8529         bp->port.advertising[1] = 0;
8530         switch (bp->link_params.num_phys) {
8531         case 1:
8532         case 2:
8533                 cfg_size = 1;
8534                 break;
8535         case 3:
8536                 cfg_size = 2;
8537                 break;
8538         }
8539         for (idx = 0; idx < cfg_size; idx++) {
8540                 bp->link_params.req_duplex[idx] = DUPLEX_FULL;
8541                 link_config = bp->port.link_config[idx];
8542                 switch (link_config & PORT_FEATURE_LINK_SPEED_MASK) {
8543                 case PORT_FEATURE_LINK_SPEED_AUTO:
8544                         if (bp->port.supported[idx] & SUPPORTED_Autoneg) {
8545                                 bp->link_params.req_line_speed[idx] =
8546                                         SPEED_AUTO_NEG;
8547                                 bp->port.advertising[idx] |=
8548                                         bp->port.supported[idx];
8549                         } else {
8550                                 /* force 10G, no AN */
8551                                 bp->link_params.req_line_speed[idx] =
8552                                         SPEED_10000;
8553                                 bp->port.advertising[idx] |=
8554                                         (ADVERTISED_10000baseT_Full |
8555                                          ADVERTISED_FIBRE);
8556                                 continue;
8557                         }
8558                         break;
8559
8560                 case PORT_FEATURE_LINK_SPEED_10M_FULL:
8561                         if (bp->port.supported[idx] & SUPPORTED_10baseT_Full) {
8562                                 bp->link_params.req_line_speed[idx] =
8563                                         SPEED_10;
8564                                 bp->port.advertising[idx] |=
8565                                         (ADVERTISED_10baseT_Full |
8566                                          ADVERTISED_TP);
8567                         } else {
8568                                 BNX2X_ERR("NVRAM config error. "
8569                                             "Invalid link_config 0x%x"
8570                                             "  speed_cap_mask 0x%x\n",
8571                                             link_config,
8572                                     bp->link_params.speed_cap_mask[idx]);
8573                                 return;
8574                         }
8575                         break;
8576
8577                 case PORT_FEATURE_LINK_SPEED_10M_HALF:
8578                         if (bp->port.supported[idx] & SUPPORTED_10baseT_Half) {
8579                                 bp->link_params.req_line_speed[idx] =
8580                                         SPEED_10;
8581                                 bp->link_params.req_duplex[idx] =
8582                                         DUPLEX_HALF;
8583                                 bp->port.advertising[idx] |=
8584                                         (ADVERTISED_10baseT_Half |
8585                                          ADVERTISED_TP);
8586                         } else {
8587                                 BNX2X_ERR("NVRAM config error. "
8588                                             "Invalid link_config 0x%x"
8589                                             "  speed_cap_mask 0x%x\n",
8590                                             link_config,
8591                                           bp->link_params.speed_cap_mask[idx]);
8592                                 return;
8593                         }
8594                         break;
8595
8596                 case PORT_FEATURE_LINK_SPEED_100M_FULL:
8597                         if (bp->port.supported[idx] &
8598                             SUPPORTED_100baseT_Full) {
8599                                 bp->link_params.req_line_speed[idx] =
8600                                         SPEED_100;
8601                                 bp->port.advertising[idx] |=
8602                                         (ADVERTISED_100baseT_Full |
8603                                          ADVERTISED_TP);
8604                         } else {
8605                                 BNX2X_ERR("NVRAM config error. "
8606                                             "Invalid link_config 0x%x"
8607                                             "  speed_cap_mask 0x%x\n",
8608                                             link_config,
8609                                           bp->link_params.speed_cap_mask[idx]);
8610                                 return;
8611                         }
8612                         break;
8613
8614                 case PORT_FEATURE_LINK_SPEED_100M_HALF:
8615                         if (bp->port.supported[idx] &
8616                             SUPPORTED_100baseT_Half) {
8617                                 bp->link_params.req_line_speed[idx] =
8618                                                                 SPEED_100;
8619                                 bp->link_params.req_duplex[idx] =
8620                                                                 DUPLEX_HALF;
8621                                 bp->port.advertising[idx] |=
8622                                         (ADVERTISED_100baseT_Half |
8623                                          ADVERTISED_TP);
8624                         } else {
8625                                 BNX2X_ERR("NVRAM config error. "
8626                                     "Invalid link_config 0x%x"
8627                                     "  speed_cap_mask 0x%x\n",
8628                                     link_config,
8629                                     bp->link_params.speed_cap_mask[idx]);
8630                                 return;
8631                         }
8632                         break;
8633
8634                 case PORT_FEATURE_LINK_SPEED_1G:
8635                         if (bp->port.supported[idx] &
8636                             SUPPORTED_1000baseT_Full) {
8637                                 bp->link_params.req_line_speed[idx] =
8638                                         SPEED_1000;
8639                                 bp->port.advertising[idx] |=
8640                                         (ADVERTISED_1000baseT_Full |
8641                                          ADVERTISED_TP);
8642                         } else {
8643                                 BNX2X_ERR("NVRAM config error. "
8644                                     "Invalid link_config 0x%x"
8645                                     "  speed_cap_mask 0x%x\n",
8646                                     link_config,
8647                                     bp->link_params.speed_cap_mask[idx]);
8648                                 return;
8649                         }
8650                         break;
8651
8652                 case PORT_FEATURE_LINK_SPEED_2_5G:
8653                         if (bp->port.supported[idx] &
8654                             SUPPORTED_2500baseX_Full) {
8655                                 bp->link_params.req_line_speed[idx] =
8656                                         SPEED_2500;
8657                                 bp->port.advertising[idx] |=
8658                                         (ADVERTISED_2500baseX_Full |
8659                                                 ADVERTISED_TP);
8660                         } else {
8661                                 BNX2X_ERR("NVRAM config error. "
8662                                     "Invalid link_config 0x%x"
8663                                     "  speed_cap_mask 0x%x\n",
8664                                     link_config,
8665                                     bp->link_params.speed_cap_mask[idx]);
8666                                 return;
8667                         }
8668                         break;
8669
8670                 case PORT_FEATURE_LINK_SPEED_10G_CX4:
8671                         if (bp->port.supported[idx] &
8672                             SUPPORTED_10000baseT_Full) {
8673                                 bp->link_params.req_line_speed[idx] =
8674                                         SPEED_10000;
8675                                 bp->port.advertising[idx] |=
8676                                         (ADVERTISED_10000baseT_Full |
8677                                                 ADVERTISED_FIBRE);
8678                         } else {
8679                                 BNX2X_ERR("NVRAM config error. "
8680                                     "Invalid link_config 0x%x"
8681                                     "  speed_cap_mask 0x%x\n",
8682                                     link_config,
8683                                     bp->link_params.speed_cap_mask[idx]);
8684                                 return;
8685                         }
8686                         break;
8687                 case PORT_FEATURE_LINK_SPEED_20G:
8688                         bp->link_params.req_line_speed[idx] = SPEED_20000;
8689
8690                         break;
8691                 default:
8692                         BNX2X_ERR("NVRAM config error. "
8693                                   "BAD link speed link_config 0x%x\n",
8694                                   link_config);
8695                                 bp->link_params.req_line_speed[idx] =
8696                                                         SPEED_AUTO_NEG;
8697                                 bp->port.advertising[idx] =
8698                                                 bp->port.supported[idx];
8699                         break;
8700                 }
8701
8702                 bp->link_params.req_flow_ctrl[idx] = (link_config &
8703                                          PORT_FEATURE_FLOW_CONTROL_MASK);
8704                 if ((bp->link_params.req_flow_ctrl[idx] ==
8705                      BNX2X_FLOW_CTRL_AUTO) &&
8706                     !(bp->port.supported[idx] & SUPPORTED_Autoneg)) {
8707                         bp->link_params.req_flow_ctrl[idx] =
8708                                 BNX2X_FLOW_CTRL_NONE;
8709                 }
8710
8711                 BNX2X_DEV_INFO("req_line_speed %d  req_duplex %d req_flow_ctrl"
8712                                " 0x%x advertising 0x%x\n",
8713                                bp->link_params.req_line_speed[idx],
8714                                bp->link_params.req_duplex[idx],
8715                                bp->link_params.req_flow_ctrl[idx],
8716                                bp->port.advertising[idx]);
8717         }
8718 }
8719
8720 static void __devinit bnx2x_set_mac_buf(u8 *mac_buf, u32 mac_lo, u16 mac_hi)
8721 {
8722         mac_hi = cpu_to_be16(mac_hi);
8723         mac_lo = cpu_to_be32(mac_lo);
8724         memcpy(mac_buf, &mac_hi, sizeof(mac_hi));
8725         memcpy(mac_buf + sizeof(mac_hi), &mac_lo, sizeof(mac_lo));
8726 }
8727
8728 static void __devinit bnx2x_get_port_hwinfo(struct bnx2x *bp)
8729 {
8730         int port = BP_PORT(bp);
8731         u32 config;
8732         u32 ext_phy_type, ext_phy_config;
8733
8734         bp->link_params.bp = bp;
8735         bp->link_params.port = port;
8736
8737         bp->link_params.lane_config =
8738                 SHMEM_RD(bp, dev_info.port_hw_config[port].lane_config);
8739
8740         bp->link_params.speed_cap_mask[0] =
8741                 SHMEM_RD(bp,
8742                          dev_info.port_hw_config[port].speed_capability_mask);
8743         bp->link_params.speed_cap_mask[1] =
8744                 SHMEM_RD(bp,
8745                          dev_info.port_hw_config[port].speed_capability_mask2);
8746         bp->port.link_config[0] =
8747                 SHMEM_RD(bp, dev_info.port_feature_config[port].link_config);
8748
8749         bp->port.link_config[1] =
8750                 SHMEM_RD(bp, dev_info.port_feature_config[port].link_config2);
8751
8752         bp->link_params.multi_phy_config =
8753                 SHMEM_RD(bp, dev_info.port_hw_config[port].multi_phy_config);
8754         /* If the device is capable of WoL, set the default state according
8755          * to the HW
8756          */
8757         config = SHMEM_RD(bp, dev_info.port_feature_config[port].config);
8758         bp->wol = (!(bp->flags & NO_WOL_FLAG) &&
8759                    (config & PORT_FEATURE_WOL_ENABLED));
8760
8761         BNX2X_DEV_INFO("lane_config 0x%08x  "
8762                        "speed_cap_mask0 0x%08x  link_config0 0x%08x\n",
8763                        bp->link_params.lane_config,
8764                        bp->link_params.speed_cap_mask[0],
8765                        bp->port.link_config[0]);
8766
8767         bp->link_params.switch_cfg = (bp->port.link_config[0] &
8768                                       PORT_FEATURE_CONNECTED_SWITCH_MASK);
8769         bnx2x_phy_probe(&bp->link_params);
8770         bnx2x_link_settings_supported(bp, bp->link_params.switch_cfg);
8771
8772         bnx2x_link_settings_requested(bp);
8773
8774         /*
8775          * If connected directly, work with the internal PHY, otherwise, work
8776          * with the external PHY
8777          */
8778         ext_phy_config =
8779                 SHMEM_RD(bp,
8780                          dev_info.port_hw_config[port].external_phy_config);
8781         ext_phy_type = XGXS_EXT_PHY_TYPE(ext_phy_config);
8782         if (ext_phy_type == PORT_HW_CFG_XGXS_EXT_PHY_TYPE_DIRECT)
8783                 bp->mdio.prtad = bp->port.phy_addr;
8784
8785         else if ((ext_phy_type != PORT_HW_CFG_XGXS_EXT_PHY_TYPE_FAILURE) &&
8786                  (ext_phy_type != PORT_HW_CFG_XGXS_EXT_PHY_TYPE_NOT_CONN))
8787                 bp->mdio.prtad =
8788                         XGXS_EXT_PHY_ADDR(ext_phy_config);
8789
8790         /*
8791          * Check if hw lock is required to access MDC/MDIO bus to the PHY(s)
8792          * In MF mode, it is set to cover self test cases
8793          */
8794         if (IS_MF(bp))
8795                 bp->port.need_hw_lock = 1;
8796         else
8797                 bp->port.need_hw_lock = bnx2x_hw_lock_required(bp,
8798                                                         bp->common.shmem_base,
8799                                                         bp->common.shmem2_base);
8800 }
8801
8802 #ifdef BCM_CNIC
8803 static void __devinit bnx2x_get_cnic_info(struct bnx2x *bp)
8804 {
8805         u32 max_iscsi_conn = FW_ENCODE_32BIT_PATTERN ^ SHMEM_RD(bp,
8806                                 drv_lic_key[BP_PORT(bp)].max_iscsi_conn);
8807         u32 max_fcoe_conn = FW_ENCODE_32BIT_PATTERN ^ SHMEM_RD(bp,
8808                                 drv_lic_key[BP_PORT(bp)].max_fcoe_conn);
8809
8810         /* Get the number of maximum allowed iSCSI and FCoE connections */
8811         bp->cnic_eth_dev.max_iscsi_conn =
8812                 (max_iscsi_conn & BNX2X_MAX_ISCSI_INIT_CONN_MASK) >>
8813                 BNX2X_MAX_ISCSI_INIT_CONN_SHIFT;
8814
8815         bp->cnic_eth_dev.max_fcoe_conn =
8816                 (max_fcoe_conn & BNX2X_MAX_FCOE_INIT_CONN_MASK) >>
8817                 BNX2X_MAX_FCOE_INIT_CONN_SHIFT;
8818
8819         BNX2X_DEV_INFO("max_iscsi_conn 0x%x max_fcoe_conn 0x%x\n",
8820                        bp->cnic_eth_dev.max_iscsi_conn,
8821                        bp->cnic_eth_dev.max_fcoe_conn);
8822
8823         /* If mamimum allowed number of connections is zero -
8824          * disable the feature.
8825          */
8826         if (!bp->cnic_eth_dev.max_iscsi_conn)
8827                 bp->flags |= NO_ISCSI_OOO_FLAG | NO_ISCSI_FLAG;
8828
8829         if (!bp->cnic_eth_dev.max_fcoe_conn)
8830                 bp->flags |= NO_FCOE_FLAG;
8831 }
8832 #endif
8833
8834 static void __devinit bnx2x_get_mac_hwinfo(struct bnx2x *bp)
8835 {
8836         u32 val, val2;
8837         int func = BP_ABS_FUNC(bp);
8838         int port = BP_PORT(bp);
8839 #ifdef BCM_CNIC
8840         u8 *iscsi_mac = bp->cnic_eth_dev.iscsi_mac;
8841         u8 *fip_mac = bp->fip_mac;
8842 #endif
8843
8844         /* Zero primary MAC configuration */
8845         memset(bp->dev->dev_addr, 0, ETH_ALEN);
8846
8847         if (BP_NOMCP(bp)) {
8848                 BNX2X_ERROR("warning: random MAC workaround active\n");
8849                 random_ether_addr(bp->dev->dev_addr);
8850         } else if (IS_MF(bp)) {
8851                 val2 = MF_CFG_RD(bp, func_mf_config[func].mac_upper);
8852                 val = MF_CFG_RD(bp, func_mf_config[func].mac_lower);
8853                 if ((val2 != FUNC_MF_CFG_UPPERMAC_DEFAULT) &&
8854                     (val != FUNC_MF_CFG_LOWERMAC_DEFAULT))
8855                         bnx2x_set_mac_buf(bp->dev->dev_addr, val, val2);
8856
8857 #ifdef BCM_CNIC
8858                 /* iSCSI and FCoE NPAR MACs: if there is no either iSCSI or
8859                  * FCoE MAC then the appropriate feature should be disabled.
8860                  */
8861                 if (IS_MF_SI(bp)) {
8862                         u32 cfg = MF_CFG_RD(bp, func_ext_config[func].func_cfg);
8863                         if (cfg & MACP_FUNC_CFG_FLAGS_ISCSI_OFFLOAD) {
8864                                 val2 = MF_CFG_RD(bp, func_ext_config[func].
8865                                                      iscsi_mac_addr_upper);
8866                                 val = MF_CFG_RD(bp, func_ext_config[func].
8867                                                     iscsi_mac_addr_lower);
8868                                 bnx2x_set_mac_buf(iscsi_mac, val, val2);
8869                                 BNX2X_DEV_INFO("Read iSCSI MAC: "
8870                                                BNX2X_MAC_FMT"\n",
8871                                                BNX2X_MAC_PRN_LIST(iscsi_mac));
8872                         } else
8873                                 bp->flags |= NO_ISCSI_OOO_FLAG | NO_ISCSI_FLAG;
8874
8875                         if (cfg & MACP_FUNC_CFG_FLAGS_FCOE_OFFLOAD) {
8876                                 val2 = MF_CFG_RD(bp, func_ext_config[func].
8877                                                      fcoe_mac_addr_upper);
8878                                 val = MF_CFG_RD(bp, func_ext_config[func].
8879                                                     fcoe_mac_addr_lower);
8880                                 bnx2x_set_mac_buf(fip_mac, val, val2);
8881                                 BNX2X_DEV_INFO("Read FCoE L2 MAC to "
8882                                                BNX2X_MAC_FMT"\n",
8883                                                BNX2X_MAC_PRN_LIST(fip_mac));
8884
8885                         } else
8886                                 bp->flags |= NO_FCOE_FLAG;
8887                 }
8888 #endif
8889         } else {
8890                 /* in SF read MACs from port configuration */
8891                 val2 = SHMEM_RD(bp, dev_info.port_hw_config[port].mac_upper);
8892                 val = SHMEM_RD(bp, dev_info.port_hw_config[port].mac_lower);
8893                 bnx2x_set_mac_buf(bp->dev->dev_addr, val, val2);
8894
8895 #ifdef BCM_CNIC
8896                 val2 = SHMEM_RD(bp, dev_info.port_hw_config[port].
8897                                     iscsi_mac_upper);
8898                 val = SHMEM_RD(bp, dev_info.port_hw_config[port].
8899                                    iscsi_mac_lower);
8900                 bnx2x_set_mac_buf(iscsi_mac, val, val2);
8901 #endif
8902         }
8903
8904         memcpy(bp->link_params.mac_addr, bp->dev->dev_addr, ETH_ALEN);
8905         memcpy(bp->dev->perm_addr, bp->dev->dev_addr, ETH_ALEN);
8906
8907 #ifdef BCM_CNIC
8908         /* Set the FCoE MAC in modes other then MF_SI */
8909         if (!CHIP_IS_E1x(bp)) {
8910                 if (IS_MF_SD(bp))
8911                         memcpy(fip_mac, bp->dev->dev_addr, ETH_ALEN);
8912                 else if (!IS_MF(bp))
8913                         memcpy(fip_mac, iscsi_mac, ETH_ALEN);
8914         }
8915
8916         /* Disable iSCSI if MAC configuration is
8917          * invalid.
8918          */
8919         if (!is_valid_ether_addr(iscsi_mac)) {
8920                 bp->flags |= NO_ISCSI_FLAG;
8921                 memset(iscsi_mac, 0, ETH_ALEN);
8922         }
8923
8924         /* Disable FCoE if MAC configuration is
8925          * invalid.
8926          */
8927         if (!is_valid_ether_addr(fip_mac)) {
8928                 bp->flags |= NO_FCOE_FLAG;
8929                 memset(bp->fip_mac, 0, ETH_ALEN);
8930         }
8931 #endif
8932
8933         if (!is_valid_ether_addr(bp->dev->dev_addr))
8934                 dev_err(&bp->pdev->dev,
8935                         "bad Ethernet MAC address configuration: "
8936                         BNX2X_MAC_FMT", change it manually before bringing up "
8937                         "the appropriate network interface\n",
8938                         BNX2X_MAC_PRN_LIST(bp->dev->dev_addr));
8939 }
8940
8941 static int __devinit bnx2x_get_hwinfo(struct bnx2x *bp)
8942 {
8943         int /*abs*/func = BP_ABS_FUNC(bp);
8944         int vn;
8945         u32 val = 0;
8946         int rc = 0;
8947
8948         bnx2x_get_common_hwinfo(bp);
8949
8950         if (CHIP_IS_E1x(bp)) {
8951                 bp->common.int_block = INT_BLOCK_HC;
8952
8953                 bp->igu_dsb_id = DEF_SB_IGU_ID;
8954                 bp->igu_base_sb = 0;
8955                 bp->igu_sb_cnt = min_t(u8, FP_SB_MAX_E1x,
8956                                        NUM_IGU_SB_REQUIRED(bp->l2_cid_count));
8957         } else {
8958                 bp->common.int_block = INT_BLOCK_IGU;
8959                 val = REG_RD(bp, IGU_REG_BLOCK_CONFIGURATION);
8960
8961                 if (val & IGU_BLOCK_CONFIGURATION_REG_BACKWARD_COMP_EN) {
8962                         int tout = 5000;
8963
8964                         BNX2X_DEV_INFO("FORCING Normal Mode\n");
8965
8966                         val &= ~(IGU_BLOCK_CONFIGURATION_REG_BACKWARD_COMP_EN);
8967                         REG_WR(bp, IGU_REG_BLOCK_CONFIGURATION, val);
8968                         REG_WR(bp, IGU_REG_RESET_MEMORIES, 0x7f);
8969
8970                         while (tout && REG_RD(bp, IGU_REG_RESET_MEMORIES)) {
8971                                 tout--;
8972                                 usleep_range(1000, 1000);
8973                         }
8974
8975                         if (REG_RD(bp, IGU_REG_RESET_MEMORIES)) {
8976                                 dev_err(&bp->pdev->dev,
8977                                         "FORCING Normal Mode failed!!!\n");
8978                                 return -EPERM;
8979                         }
8980                 }
8981
8982                 if (val & IGU_BLOCK_CONFIGURATION_REG_BACKWARD_COMP_EN) {
8983                         BNX2X_DEV_INFO("IGU Backward Compatible Mode\n");
8984                         bp->common.int_block |= INT_BLOCK_MODE_BW_COMP;
8985                 } else
8986                         BNX2X_DEV_INFO("IGU Normal Mode\n");
8987
8988                 bnx2x_get_igu_cam_info(bp);
8989
8990         }
8991
8992         /*
8993          * set base FW non-default (fast path) status block id, this value is
8994          * used to initialize the fw_sb_id saved on the fp/queue structure to
8995          * determine the id used by the FW.
8996          */
8997         if (CHIP_IS_E1x(bp))
8998                 bp->base_fw_ndsb = BP_PORT(bp) * FP_SB_MAX_E1x + BP_L_ID(bp);
8999         else /*
9000               * 57712 - we currently use one FW SB per IGU SB (Rx and Tx of
9001               * the same queue are indicated on the same IGU SB). So we prefer
9002               * FW and IGU SBs to be the same value.
9003               */
9004                 bp->base_fw_ndsb = bp->igu_base_sb;
9005
9006         BNX2X_DEV_INFO("igu_dsb_id %d  igu_base_sb %d  igu_sb_cnt %d\n"
9007                        "base_fw_ndsb %d\n", bp->igu_dsb_id, bp->igu_base_sb,
9008                        bp->igu_sb_cnt, bp->base_fw_ndsb);
9009
9010         /*
9011          * Initialize MF configuration
9012          */
9013
9014         bp->mf_ov = 0;
9015         bp->mf_mode = 0;
9016         vn = BP_E1HVN(bp);
9017
9018         if (!CHIP_IS_E1(bp) && !BP_NOMCP(bp)) {
9019                 BNX2X_DEV_INFO("shmem2base 0x%x, size %d, mfcfg offset %d\n",
9020                                bp->common.shmem2_base, SHMEM2_RD(bp, size),
9021                               (u32)offsetof(struct shmem2_region, mf_cfg_addr));
9022
9023                 if (SHMEM2_HAS(bp, mf_cfg_addr))
9024                         bp->common.mf_cfg_base = SHMEM2_RD(bp, mf_cfg_addr);
9025                 else
9026                         bp->common.mf_cfg_base = bp->common.shmem_base +
9027                                 offsetof(struct shmem_region, func_mb) +
9028                                 E1H_FUNC_MAX * sizeof(struct drv_func_mb);
9029                 /*
9030                  * get mf configuration:
9031                  * 1. existence of MF configuration
9032                  * 2. MAC address must be legal (check only upper bytes)
9033                  *    for  Switch-Independent mode;
9034                  *    OVLAN must be legal for Switch-Dependent mode
9035                  * 3. SF_MODE configures specific MF mode
9036                  */
9037                 if (bp->common.mf_cfg_base != SHMEM_MF_CFG_ADDR_NONE) {
9038                         /* get mf configuration */
9039                         val = SHMEM_RD(bp,
9040                                        dev_info.shared_feature_config.config);
9041                         val &= SHARED_FEAT_CFG_FORCE_SF_MODE_MASK;
9042
9043                         switch (val) {
9044                         case SHARED_FEAT_CFG_FORCE_SF_MODE_SWITCH_INDEPT:
9045                                 val = MF_CFG_RD(bp, func_mf_config[func].
9046                                                 mac_upper);
9047                                 /* check for legal mac (upper bytes)*/
9048                                 if (val != 0xffff) {
9049                                         bp->mf_mode = MULTI_FUNCTION_SI;
9050                                         bp->mf_config[vn] = MF_CFG_RD(bp,
9051                                                    func_mf_config[func].config);
9052                                 } else
9053                                         BNX2X_DEV_INFO("illegal MAC address "
9054                                                        "for SI\n");
9055                                 break;
9056                         case SHARED_FEAT_CFG_FORCE_SF_MODE_MF_ALLOWED:
9057                                 /* get OV configuration */
9058                                 val = MF_CFG_RD(bp,
9059                                         func_mf_config[FUNC_0].e1hov_tag);
9060                                 val &= FUNC_MF_CFG_E1HOV_TAG_MASK;
9061
9062                                 if (val != FUNC_MF_CFG_E1HOV_TAG_DEFAULT) {
9063                                         bp->mf_mode = MULTI_FUNCTION_SD;
9064                                         bp->mf_config[vn] = MF_CFG_RD(bp,
9065                                                 func_mf_config[func].config);
9066                                 } else
9067                                         BNX2X_DEV_INFO("illegal OV for SD\n");
9068                                 break;
9069                         default:
9070                                 /* Unknown configuration: reset mf_config */
9071                                 bp->mf_config[vn] = 0;
9072                                 BNX2X_DEV_INFO("unkown MF mode 0x%x\n", val);
9073                         }
9074                 }
9075
9076                 BNX2X_DEV_INFO("%s function mode\n",
9077                                IS_MF(bp) ? "multi" : "single");
9078
9079                 switch (bp->mf_mode) {
9080                 case MULTI_FUNCTION_SD:
9081                         val = MF_CFG_RD(bp, func_mf_config[func].e1hov_tag) &
9082                               FUNC_MF_CFG_E1HOV_TAG_MASK;
9083                         if (val != FUNC_MF_CFG_E1HOV_TAG_DEFAULT) {
9084                                 bp->mf_ov = val;
9085                                 bp->path_has_ovlan = true;
9086
9087                                 BNX2X_DEV_INFO("MF OV for func %d is %d "
9088                                                "(0x%04x)\n", func, bp->mf_ov,
9089                                                bp->mf_ov);
9090                         } else {
9091                                 dev_err(&bp->pdev->dev,
9092                                         "No valid MF OV for func %d, "
9093                                         "aborting\n", func);
9094                                 return -EPERM;
9095                         }
9096                         break;
9097                 case MULTI_FUNCTION_SI:
9098                         BNX2X_DEV_INFO("func %d is in MF "
9099                                        "switch-independent mode\n", func);
9100                         break;
9101                 default:
9102                         if (vn) {
9103                                 dev_err(&bp->pdev->dev,
9104                                         "VN %d is in a single function mode, "
9105                                         "aborting\n", vn);
9106                                 return -EPERM;
9107                         }
9108                         break;
9109                 }
9110
9111                 /* check if other port on the path needs ovlan:
9112                  * Since MF configuration is shared between ports
9113                  * Possible mixed modes are only
9114                  * {SF, SI} {SF, SD} {SD, SF} {SI, SF}
9115                  */
9116                 if (CHIP_MODE_IS_4_PORT(bp) &&
9117                     !bp->path_has_ovlan &&
9118                     !IS_MF(bp) &&
9119                     bp->common.mf_cfg_base != SHMEM_MF_CFG_ADDR_NONE) {
9120                         u8 other_port = !BP_PORT(bp);
9121                         u8 other_func = BP_PATH(bp) + 2*other_port;
9122                         val = MF_CFG_RD(bp,
9123                                         func_mf_config[other_func].e1hov_tag);
9124                         if (val != FUNC_MF_CFG_E1HOV_TAG_DEFAULT)
9125                                 bp->path_has_ovlan = true;
9126                 }
9127         }
9128
9129         /* adjust igu_sb_cnt to MF for E1x */
9130         if (CHIP_IS_E1x(bp) && IS_MF(bp))
9131                 bp->igu_sb_cnt /= E1HVN_MAX;
9132
9133         /* port info */
9134         bnx2x_get_port_hwinfo(bp);
9135
9136         if (!BP_NOMCP(bp)) {
9137                 bp->fw_seq =
9138                         (SHMEM_RD(bp, func_mb[BP_FW_MB_IDX(bp)].drv_mb_header) &
9139                          DRV_MSG_SEQ_NUMBER_MASK);
9140                 BNX2X_DEV_INFO("fw_seq 0x%08x\n", bp->fw_seq);
9141         }
9142
9143         /* Get MAC addresses */
9144         bnx2x_get_mac_hwinfo(bp);
9145
9146 #ifdef BCM_CNIC
9147         bnx2x_get_cnic_info(bp);
9148 #endif
9149
9150         /* Get current FW pulse sequence */
9151         if (!BP_NOMCP(bp)) {
9152                 int mb_idx = BP_FW_MB_IDX(bp);
9153
9154                 bp->fw_drv_pulse_wr_seq =
9155                                 (SHMEM_RD(bp, func_mb[mb_idx].drv_pulse_mb) &
9156                                  DRV_PULSE_SEQ_MASK);
9157                 BNX2X_DEV_INFO("drv_pulse 0x%x\n", bp->fw_drv_pulse_wr_seq);
9158         }
9159
9160         return rc;
9161 }
9162
9163 static void __devinit bnx2x_read_fwinfo(struct bnx2x *bp)
9164 {
9165         int cnt, i, block_end, rodi;
9166         char vpd_data[BNX2X_VPD_LEN+1];
9167         char str_id_reg[VENDOR_ID_LEN+1];
9168         char str_id_cap[VENDOR_ID_LEN+1];
9169         u8 len;
9170
9171         cnt = pci_read_vpd(bp->pdev, 0, BNX2X_VPD_LEN, vpd_data);
9172         memset(bp->fw_ver, 0, sizeof(bp->fw_ver));
9173
9174         if (cnt < BNX2X_VPD_LEN)
9175                 goto out_not_found;
9176
9177         i = pci_vpd_find_tag(vpd_data, 0, BNX2X_VPD_LEN,
9178                              PCI_VPD_LRDT_RO_DATA);
9179         if (i < 0)
9180                 goto out_not_found;
9181
9182
9183         block_end = i + PCI_VPD_LRDT_TAG_SIZE +
9184                     pci_vpd_lrdt_size(&vpd_data[i]);
9185
9186         i += PCI_VPD_LRDT_TAG_SIZE;
9187
9188         if (block_end > BNX2X_VPD_LEN)
9189                 goto out_not_found;
9190
9191         rodi = pci_vpd_find_info_keyword(vpd_data, i, block_end,
9192                                    PCI_VPD_RO_KEYWORD_MFR_ID);
9193         if (rodi < 0)
9194                 goto out_not_found;
9195
9196         len = pci_vpd_info_field_size(&vpd_data[rodi]);
9197
9198         if (len != VENDOR_ID_LEN)
9199                 goto out_not_found;
9200
9201         rodi += PCI_VPD_INFO_FLD_HDR_SIZE;
9202
9203         /* vendor specific info */
9204         snprintf(str_id_reg, VENDOR_ID_LEN + 1, "%04x", PCI_VENDOR_ID_DELL);
9205         snprintf(str_id_cap, VENDOR_ID_LEN + 1, "%04X", PCI_VENDOR_ID_DELL);
9206         if (!strncmp(str_id_reg, &vpd_data[rodi], VENDOR_ID_LEN) ||
9207             !strncmp(str_id_cap, &vpd_data[rodi], VENDOR_ID_LEN)) {
9208
9209                 rodi = pci_vpd_find_info_keyword(vpd_data, i, block_end,
9210                                                 PCI_VPD_RO_KEYWORD_VENDOR0);
9211                 if (rodi >= 0) {
9212                         len = pci_vpd_info_field_size(&vpd_data[rodi]);
9213
9214                         rodi += PCI_VPD_INFO_FLD_HDR_SIZE;
9215
9216                         if (len < 32 && (len + rodi) <= BNX2X_VPD_LEN) {
9217                                 memcpy(bp->fw_ver, &vpd_data[rodi], len);
9218                                 bp->fw_ver[len] = ' ';
9219                         }
9220                 }
9221                 return;
9222         }
9223 out_not_found:
9224         return;
9225 }
9226
9227 static void __devinit bnx2x_set_modes_bitmap(struct bnx2x *bp)
9228 {
9229         u32 flags = 0;
9230
9231         if (CHIP_REV_IS_FPGA(bp))
9232                 SET_FLAGS(flags, MODE_FPGA);
9233         else if (CHIP_REV_IS_EMUL(bp))
9234                 SET_FLAGS(flags, MODE_EMUL);
9235         else
9236                 SET_FLAGS(flags, MODE_ASIC);
9237
9238         if (CHIP_MODE_IS_4_PORT(bp))
9239                 SET_FLAGS(flags, MODE_PORT4);
9240         else
9241                 SET_FLAGS(flags, MODE_PORT2);
9242
9243         if (CHIP_IS_E2(bp))
9244                 SET_FLAGS(flags, MODE_E2);
9245         else if (CHIP_IS_E3(bp)) {
9246                 SET_FLAGS(flags, MODE_E3);
9247                 if (CHIP_REV(bp) == CHIP_REV_Ax)
9248                         SET_FLAGS(flags, MODE_E3_A0);
9249                 else {/*if (CHIP_REV(bp) == CHIP_REV_Bx)*/
9250                         SET_FLAGS(flags, MODE_E3_B0);
9251                         SET_FLAGS(flags, MODE_COS_BC);
9252                 }
9253         }
9254
9255         if (IS_MF(bp)) {
9256                 SET_FLAGS(flags, MODE_MF);
9257                 switch (bp->mf_mode) {
9258                 case MULTI_FUNCTION_SD:
9259                         SET_FLAGS(flags, MODE_MF_SD);
9260                         break;
9261                 case MULTI_FUNCTION_SI:
9262                         SET_FLAGS(flags, MODE_MF_SI);
9263                         break;
9264                 }
9265         } else
9266                 SET_FLAGS(flags, MODE_SF);
9267
9268 #if defined(__LITTLE_ENDIAN)
9269         SET_FLAGS(flags, MODE_LITTLE_ENDIAN);
9270 #else /*(__BIG_ENDIAN)*/
9271         SET_FLAGS(flags, MODE_BIG_ENDIAN);
9272 #endif
9273         INIT_MODE_FLAGS(bp) = flags;
9274 }
9275
9276 static int __devinit bnx2x_init_bp(struct bnx2x *bp)
9277 {
9278         int func;
9279         int timer_interval;
9280         int rc;
9281
9282         mutex_init(&bp->port.phy_mutex);
9283         mutex_init(&bp->fw_mb_mutex);
9284         spin_lock_init(&bp->stats_lock);
9285 #ifdef BCM_CNIC
9286         mutex_init(&bp->cnic_mutex);
9287 #endif
9288
9289         INIT_DELAYED_WORK(&bp->sp_task, bnx2x_sp_task);
9290         INIT_DELAYED_WORK(&bp->reset_task, bnx2x_reset_task);
9291         INIT_DELAYED_WORK(&bp->period_task, bnx2x_period_task);
9292         rc = bnx2x_get_hwinfo(bp);
9293         if (rc)
9294                 return rc;
9295
9296         bnx2x_set_modes_bitmap(bp);
9297
9298         rc = bnx2x_alloc_mem_bp(bp);
9299         if (rc)
9300                 return rc;
9301
9302         bnx2x_read_fwinfo(bp);
9303
9304         func = BP_FUNC(bp);
9305
9306         /* need to reset chip if undi was active */
9307         if (!BP_NOMCP(bp))
9308                 bnx2x_undi_unload(bp);
9309
9310         if (CHIP_REV_IS_FPGA(bp))
9311                 dev_err(&bp->pdev->dev, "FPGA detected\n");
9312
9313         if (BP_NOMCP(bp) && (func == 0))
9314                 dev_err(&bp->pdev->dev, "MCP disabled, "
9315                                         "must load devices in order!\n");
9316
9317         bp->multi_mode = multi_mode;
9318
9319         /* Set TPA flags */
9320         if (disable_tpa) {
9321                 bp->flags &= ~TPA_ENABLE_FLAG;
9322                 bp->dev->features &= ~NETIF_F_LRO;
9323         } else {
9324                 bp->flags |= TPA_ENABLE_FLAG;
9325                 bp->dev->features |= NETIF_F_LRO;
9326         }
9327         bp->disable_tpa = disable_tpa;
9328
9329         if (CHIP_IS_E1(bp))
9330                 bp->dropless_fc = 0;
9331         else
9332                 bp->dropless_fc = dropless_fc;
9333
9334         bp->mrrs = mrrs;
9335
9336         bp->tx_ring_size = MAX_TX_AVAIL;
9337
9338         /* make sure that the numbers are in the right granularity */
9339         bp->tx_ticks = (50 / BNX2X_BTR) * BNX2X_BTR;
9340         bp->rx_ticks = (25 / BNX2X_BTR) * BNX2X_BTR;
9341
9342         timer_interval = (CHIP_REV_IS_SLOW(bp) ? 5*HZ : HZ);
9343         bp->current_interval = (poll ? poll : timer_interval);
9344
9345         init_timer(&bp->timer);
9346         bp->timer.expires = jiffies + bp->current_interval;
9347         bp->timer.data = (unsigned long) bp;
9348         bp->timer.function = bnx2x_timer;
9349
9350         bnx2x_dcbx_set_state(bp, true, BNX2X_DCBX_ENABLED_ON_NEG_ON);
9351         bnx2x_dcbx_init_params(bp);
9352
9353 #ifdef BCM_CNIC
9354         if (CHIP_IS_E1x(bp))
9355                 bp->cnic_base_cl_id = FP_SB_MAX_E1x;
9356         else
9357                 bp->cnic_base_cl_id = FP_SB_MAX_E2;
9358 #endif
9359
9360         return rc;
9361 }
9362
9363
9364 /****************************************************************************
9365 * General service functions
9366 ****************************************************************************/
9367
9368 /*
9369  * net_device service functions
9370  */
9371
9372 /* called with rtnl_lock */
9373 static int bnx2x_open(struct net_device *dev)
9374 {
9375         struct bnx2x *bp = netdev_priv(dev);
9376         bool global = false;
9377         int other_engine = BP_PATH(bp) ? 0 : 1;
9378         u32 other_load_counter, load_counter;
9379
9380         netif_carrier_off(dev);
9381
9382         bnx2x_set_power_state(bp, PCI_D0);
9383
9384         other_load_counter = bnx2x_get_load_cnt(bp, other_engine);
9385         load_counter = bnx2x_get_load_cnt(bp, BP_PATH(bp));
9386
9387         /*
9388          * If parity had happen during the unload, then attentions
9389          * and/or RECOVERY_IN_PROGRES may still be set. In this case we
9390          * want the first function loaded on the current engine to
9391          * complete the recovery.
9392          */
9393         if (!bnx2x_reset_is_done(bp, BP_PATH(bp)) ||
9394             bnx2x_chk_parity_attn(bp, &global, true))
9395                 do {
9396                         /*
9397                          * If there are attentions and they are in a global
9398                          * blocks, set the GLOBAL_RESET bit regardless whether
9399                          * it will be this function that will complete the
9400                          * recovery or not.
9401                          */
9402                         if (global)
9403                                 bnx2x_set_reset_global(bp);
9404
9405                         /*
9406                          * Only the first function on the current engine should
9407                          * try to recover in open. In case of attentions in
9408                          * global blocks only the first in the chip should try
9409                          * to recover.
9410                          */
9411                         if ((!load_counter &&
9412                              (!global || !other_load_counter)) &&
9413                             bnx2x_trylock_leader_lock(bp) &&
9414                             !bnx2x_leader_reset(bp)) {
9415                                 netdev_info(bp->dev, "Recovered in open\n");
9416                                 break;
9417                         }
9418
9419                         /* recovery has failed... */
9420                         bnx2x_set_power_state(bp, PCI_D3hot);
9421                         bp->recovery_state = BNX2X_RECOVERY_FAILED;
9422
9423                         netdev_err(bp->dev, "Recovery flow hasn't been properly"
9424                         " completed yet. Try again later. If u still see this"
9425                         " message after a few retries then power cycle is"
9426                         " required.\n");
9427
9428                         return -EAGAIN;
9429                 } while (0);
9430
9431         bp->recovery_state = BNX2X_RECOVERY_DONE;
9432         return bnx2x_nic_load(bp, LOAD_OPEN);
9433 }
9434
9435 /* called with rtnl_lock */
9436 static int bnx2x_close(struct net_device *dev)
9437 {
9438         struct bnx2x *bp = netdev_priv(dev);
9439
9440         /* Unload the driver, release IRQs */
9441         bnx2x_nic_unload(bp, UNLOAD_CLOSE);
9442
9443         /* Power off */
9444         bnx2x_set_power_state(bp, PCI_D3hot);
9445
9446         return 0;
9447 }
9448
9449 static inline int bnx2x_init_mcast_macs_list(struct bnx2x *bp,
9450                                          struct bnx2x_mcast_ramrod_params *p)
9451 {
9452         int mc_count = netdev_mc_count(bp->dev);
9453         struct bnx2x_mcast_list_elem *mc_mac =
9454                 kzalloc(sizeof(*mc_mac) * mc_count, GFP_ATOMIC);
9455         struct netdev_hw_addr *ha;
9456
9457         if (!mc_mac)
9458                 return -ENOMEM;
9459
9460         INIT_LIST_HEAD(&p->mcast_list);
9461
9462         netdev_for_each_mc_addr(ha, bp->dev) {
9463                 mc_mac->mac = bnx2x_mc_addr(ha);
9464                 list_add_tail(&mc_mac->link, &p->mcast_list);
9465                 mc_mac++;
9466         }
9467
9468         p->mcast_list_len = mc_count;
9469
9470         return 0;
9471 }
9472
9473 static inline void bnx2x_free_mcast_macs_list(
9474         struct bnx2x_mcast_ramrod_params *p)
9475 {
9476         struct bnx2x_mcast_list_elem *mc_mac =
9477                 list_first_entry(&p->mcast_list, struct bnx2x_mcast_list_elem,
9478                                  link);
9479
9480         WARN_ON(!mc_mac);
9481         kfree(mc_mac);
9482 }
9483
9484 /**
9485  * bnx2x_set_uc_list - configure a new unicast MACs list.
9486  *
9487  * @bp: driver handle
9488  *
9489  * We will use zero (0) as a MAC type for these MACs.
9490  */
9491 static inline int bnx2x_set_uc_list(struct bnx2x *bp)
9492 {
9493         int rc;
9494         struct net_device *dev = bp->dev;
9495         struct netdev_hw_addr *ha;
9496         struct bnx2x_vlan_mac_obj *mac_obj = &bp->fp->mac_obj;
9497         unsigned long ramrod_flags = 0;
9498
9499         /* First schedule a cleanup up of old configuration */
9500         rc = bnx2x_del_all_macs(bp, mac_obj, BNX2X_UC_LIST_MAC, false);
9501         if (rc < 0) {
9502                 BNX2X_ERR("Failed to schedule DELETE operations: %d\n", rc);
9503                 return rc;
9504         }
9505
9506         netdev_for_each_uc_addr(ha, dev) {
9507                 rc = bnx2x_set_mac_one(bp, bnx2x_uc_addr(ha), mac_obj, true,
9508                                        BNX2X_UC_LIST_MAC, &ramrod_flags);
9509                 if (rc < 0) {
9510                         BNX2X_ERR("Failed to schedule ADD operations: %d\n",
9511                                   rc);
9512                         return rc;
9513                 }
9514         }
9515
9516         /* Execute the pending commands */
9517         __set_bit(RAMROD_CONT, &ramrod_flags);
9518         return bnx2x_set_mac_one(bp, NULL, mac_obj, false /* don't care */,
9519                                  BNX2X_UC_LIST_MAC, &ramrod_flags);
9520 }
9521
9522 static inline int bnx2x_set_mc_list(struct bnx2x *bp)
9523 {
9524         struct net_device *dev = bp->dev;
9525         struct bnx2x_mcast_ramrod_params rparam = {0};
9526         int rc = 0;
9527
9528         rparam.mcast_obj = &bp->mcast_obj;
9529
9530         /* first, clear all configured multicast MACs */
9531         rc = bnx2x_config_mcast(bp, &rparam, BNX2X_MCAST_CMD_DEL);
9532         if (rc < 0) {
9533                 BNX2X_ERR("Failed to clear multicast "
9534                           "configuration: %d\n", rc);
9535                 return rc;
9536         }
9537
9538         /* then, configure a new MACs list */
9539         if (netdev_mc_count(dev)) {
9540                 rc = bnx2x_init_mcast_macs_list(bp, &rparam);
9541                 if (rc) {
9542                         BNX2X_ERR("Failed to create multicast MACs "
9543                                   "list: %d\n", rc);
9544                         return rc;
9545                 }
9546
9547                 /* Now add the new MACs */
9548                 rc = bnx2x_config_mcast(bp, &rparam,
9549                                         BNX2X_MCAST_CMD_ADD);
9550                 if (rc < 0)
9551                         BNX2X_ERR("Failed to set a new multicast "
9552                                   "configuration: %d\n", rc);
9553
9554                 bnx2x_free_mcast_macs_list(&rparam);
9555         }
9556
9557         return rc;
9558 }
9559
9560
9561 /* If bp->state is OPEN, should be called with netif_addr_lock_bh() */
9562 void bnx2x_set_rx_mode(struct net_device *dev)
9563 {
9564         struct bnx2x *bp = netdev_priv(dev);
9565         u32 rx_mode = BNX2X_RX_MODE_NORMAL;
9566
9567         if (bp->state != BNX2X_STATE_OPEN) {
9568                 DP(NETIF_MSG_IFUP, "state is %x, returning\n", bp->state);
9569                 return;
9570         }
9571
9572         DP(NETIF_MSG_IFUP, "dev->flags = %x\n", bp->dev->flags);
9573
9574         if (dev->flags & IFF_PROMISC)
9575                 rx_mode = BNX2X_RX_MODE_PROMISC;
9576         else if ((dev->flags & IFF_ALLMULTI) ||
9577                  ((netdev_mc_count(dev) > BNX2X_MAX_MULTICAST) &&
9578                   CHIP_IS_E1(bp)))
9579                 rx_mode = BNX2X_RX_MODE_ALLMULTI;
9580         else {
9581                 /* some multicasts */
9582                 if (bnx2x_set_mc_list(bp) < 0)
9583                         rx_mode = BNX2X_RX_MODE_ALLMULTI;
9584
9585                 if (bnx2x_set_uc_list(bp) < 0)
9586                         rx_mode = BNX2X_RX_MODE_PROMISC;
9587         }
9588
9589         bp->rx_mode = rx_mode;
9590
9591         /* Schedule the rx_mode command */
9592         if (test_bit(BNX2X_FILTER_RX_MODE_PENDING, &bp->sp_state)) {
9593                 set_bit(BNX2X_FILTER_RX_MODE_SCHED, &bp->sp_state);
9594                 return;
9595         }
9596
9597         bnx2x_set_storm_rx_mode(bp);
9598 }
9599
9600 /* called with rtnl_lock */
9601 static int bnx2x_mdio_read(struct net_device *netdev, int prtad,
9602                            int devad, u16 addr)
9603 {
9604         struct bnx2x *bp = netdev_priv(netdev);
9605         u16 value;
9606         int rc;
9607
9608         DP(NETIF_MSG_LINK, "mdio_read: prtad 0x%x, devad 0x%x, addr 0x%x\n",
9609            prtad, devad, addr);
9610
9611         /* The HW expects different devad if CL22 is used */
9612         devad = (devad == MDIO_DEVAD_NONE) ? DEFAULT_PHY_DEV_ADDR : devad;
9613
9614         bnx2x_acquire_phy_lock(bp);
9615         rc = bnx2x_phy_read(&bp->link_params, prtad, devad, addr, &value);
9616         bnx2x_release_phy_lock(bp);
9617         DP(NETIF_MSG_LINK, "mdio_read_val 0x%x rc = 0x%x\n", value, rc);
9618
9619         if (!rc)
9620                 rc = value;
9621         return rc;
9622 }
9623
9624 /* called with rtnl_lock */
9625 static int bnx2x_mdio_write(struct net_device *netdev, int prtad, int devad,
9626                             u16 addr, u16 value)
9627 {
9628         struct bnx2x *bp = netdev_priv(netdev);
9629         int rc;
9630
9631         DP(NETIF_MSG_LINK, "mdio_write: prtad 0x%x, devad 0x%x, addr 0x%x,"
9632                            " value 0x%x\n", prtad, devad, addr, value);
9633
9634         /* The HW expects different devad if CL22 is used */
9635         devad = (devad == MDIO_DEVAD_NONE) ? DEFAULT_PHY_DEV_ADDR : devad;
9636
9637         bnx2x_acquire_phy_lock(bp);
9638         rc = bnx2x_phy_write(&bp->link_params, prtad, devad, addr, value);
9639         bnx2x_release_phy_lock(bp);
9640         return rc;
9641 }
9642
9643 /* called with rtnl_lock */
9644 static int bnx2x_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
9645 {
9646         struct bnx2x *bp = netdev_priv(dev);
9647         struct mii_ioctl_data *mdio = if_mii(ifr);
9648
9649         DP(NETIF_MSG_LINK, "ioctl: phy id 0x%x, reg 0x%x, val_in 0x%x\n",
9650            mdio->phy_id, mdio->reg_num, mdio->val_in);
9651
9652         if (!netif_running(dev))
9653                 return -EAGAIN;
9654
9655         return mdio_mii_ioctl(&bp->mdio, mdio, cmd);
9656 }
9657
9658 #ifdef CONFIG_NET_POLL_CONTROLLER
9659 static void poll_bnx2x(struct net_device *dev)
9660 {
9661         struct bnx2x *bp = netdev_priv(dev);
9662
9663         disable_irq(bp->pdev->irq);
9664         bnx2x_interrupt(bp->pdev->irq, dev);
9665         enable_irq(bp->pdev->irq);
9666 }
9667 #endif
9668
9669 static const struct net_device_ops bnx2x_netdev_ops = {
9670         .ndo_open               = bnx2x_open,
9671         .ndo_stop               = bnx2x_close,
9672         .ndo_start_xmit         = bnx2x_start_xmit,
9673         .ndo_select_queue       = bnx2x_select_queue,
9674         .ndo_set_rx_mode        = bnx2x_set_rx_mode,
9675         .ndo_set_mac_address    = bnx2x_change_mac_addr,
9676         .ndo_validate_addr      = eth_validate_addr,
9677         .ndo_do_ioctl           = bnx2x_ioctl,
9678         .ndo_change_mtu         = bnx2x_change_mtu,
9679         .ndo_fix_features       = bnx2x_fix_features,
9680         .ndo_set_features       = bnx2x_set_features,
9681         .ndo_tx_timeout         = bnx2x_tx_timeout,
9682 #ifdef CONFIG_NET_POLL_CONTROLLER
9683         .ndo_poll_controller    = poll_bnx2x,
9684 #endif
9685 };
9686
9687 static inline int bnx2x_set_coherency_mask(struct bnx2x *bp)
9688 {
9689         struct device *dev = &bp->pdev->dev;
9690
9691         if (dma_set_mask(dev, DMA_BIT_MASK(64)) == 0) {
9692                 bp->flags |= USING_DAC_FLAG;
9693                 if (dma_set_coherent_mask(dev, DMA_BIT_MASK(64)) != 0) {
9694                         dev_err(dev, "dma_set_coherent_mask failed, "
9695                                      "aborting\n");
9696                         return -EIO;
9697                 }
9698         } else if (dma_set_mask(dev, DMA_BIT_MASK(32)) != 0) {
9699                 dev_err(dev, "System does not support DMA, aborting\n");
9700                 return -EIO;
9701         }
9702
9703         return 0;
9704 }
9705
9706 static int __devinit bnx2x_init_dev(struct pci_dev *pdev,
9707                                     struct net_device *dev,
9708                                     unsigned long board_type)
9709 {
9710         struct bnx2x *bp;
9711         int rc;
9712
9713         SET_NETDEV_DEV(dev, &pdev->dev);
9714         bp = netdev_priv(dev);
9715
9716         bp->dev = dev;
9717         bp->pdev = pdev;
9718         bp->flags = 0;
9719         bp->pf_num = PCI_FUNC(pdev->devfn);
9720
9721         rc = pci_enable_device(pdev);
9722         if (rc) {
9723                 dev_err(&bp->pdev->dev,
9724                         "Cannot enable PCI device, aborting\n");
9725                 goto err_out;
9726         }
9727
9728         if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM)) {
9729                 dev_err(&bp->pdev->dev,
9730                         "Cannot find PCI device base address, aborting\n");
9731                 rc = -ENODEV;
9732                 goto err_out_disable;
9733         }
9734
9735         if (!(pci_resource_flags(pdev, 2) & IORESOURCE_MEM)) {
9736                 dev_err(&bp->pdev->dev, "Cannot find second PCI device"
9737                        " base address, aborting\n");
9738                 rc = -ENODEV;
9739                 goto err_out_disable;
9740         }
9741
9742         if (atomic_read(&pdev->enable_cnt) == 1) {
9743                 rc = pci_request_regions(pdev, DRV_MODULE_NAME);
9744                 if (rc) {
9745                         dev_err(&bp->pdev->dev,
9746                                 "Cannot obtain PCI resources, aborting\n");
9747                         goto err_out_disable;
9748                 }
9749
9750                 pci_set_master(pdev);
9751                 pci_save_state(pdev);
9752         }
9753
9754         bp->pm_cap = pci_find_capability(pdev, PCI_CAP_ID_PM);
9755         if (bp->pm_cap == 0) {
9756                 dev_err(&bp->pdev->dev,
9757                         "Cannot find power management capability, aborting\n");
9758                 rc = -EIO;
9759                 goto err_out_release;
9760         }
9761
9762         bp->pcie_cap = pci_find_capability(pdev, PCI_CAP_ID_EXP);
9763         if (bp->pcie_cap == 0) {
9764                 dev_err(&bp->pdev->dev,
9765                         "Cannot find PCI Express capability, aborting\n");
9766                 rc = -EIO;
9767                 goto err_out_release;
9768         }
9769
9770         rc = bnx2x_set_coherency_mask(bp);
9771         if (rc)
9772                 goto err_out_release;
9773
9774         dev->mem_start = pci_resource_start(pdev, 0);
9775         dev->base_addr = dev->mem_start;
9776         dev->mem_end = pci_resource_end(pdev, 0);
9777
9778         dev->irq = pdev->irq;
9779
9780         bp->regview = pci_ioremap_bar(pdev, 0);
9781         if (!bp->regview) {
9782                 dev_err(&bp->pdev->dev,
9783                         "Cannot map register space, aborting\n");
9784                 rc = -ENOMEM;
9785                 goto err_out_release;
9786         }
9787
9788         bp->doorbells = ioremap_nocache(pci_resource_start(pdev, 2),
9789                                         min_t(u64, BNX2X_DB_SIZE(bp),
9790                                               pci_resource_len(pdev, 2)));
9791         if (!bp->doorbells) {
9792                 dev_err(&bp->pdev->dev,
9793                         "Cannot map doorbell space, aborting\n");
9794                 rc = -ENOMEM;
9795                 goto err_out_unmap;
9796         }
9797
9798         bnx2x_set_power_state(bp, PCI_D0);
9799
9800         /* clean indirect addresses */
9801         pci_write_config_dword(bp->pdev, PCICFG_GRC_ADDRESS,
9802                                PCICFG_VENDOR_ID_OFFSET);
9803         REG_WR(bp, PXP2_REG_PGL_ADDR_88_F0 + BP_PORT(bp)*16, 0);
9804         REG_WR(bp, PXP2_REG_PGL_ADDR_8C_F0 + BP_PORT(bp)*16, 0);
9805         REG_WR(bp, PXP2_REG_PGL_ADDR_90_F0 + BP_PORT(bp)*16, 0);
9806         REG_WR(bp, PXP2_REG_PGL_ADDR_94_F0 + BP_PORT(bp)*16, 0);
9807
9808         /**
9809          * Enable internal target-read (in case we are probed after PF FLR).
9810          * Must be done prior to any BAR read access
9811          */
9812         REG_WR(bp, PGLUE_B_REG_INTERNAL_PFID_ENABLE_TARGET_READ, 1);
9813
9814         /* Reset the load counter */
9815         bnx2x_clear_load_cnt(bp);
9816
9817         dev->watchdog_timeo = TX_TIMEOUT;
9818
9819         dev->netdev_ops = &bnx2x_netdev_ops;
9820         bnx2x_set_ethtool_ops(dev);
9821
9822         dev->hw_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM |
9823                 NETIF_F_TSO | NETIF_F_TSO_ECN | NETIF_F_TSO6 |
9824                 NETIF_F_RXCSUM | NETIF_F_LRO | NETIF_F_HW_VLAN_TX;
9825
9826         dev->vlan_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM |
9827                 NETIF_F_TSO | NETIF_F_TSO_ECN | NETIF_F_TSO6 | NETIF_F_HIGHDMA;
9828
9829         dev->features |= dev->hw_features | NETIF_F_HW_VLAN_RX;
9830         if (bp->flags & USING_DAC_FLAG)
9831                 dev->features |= NETIF_F_HIGHDMA;
9832
9833         /* Add Loopback capability to the device */
9834         dev->hw_features |= NETIF_F_LOOPBACK;
9835
9836 #ifdef BCM_DCBNL
9837         dev->dcbnl_ops = &bnx2x_dcbnl_ops;
9838 #endif
9839
9840         /* get_port_hwinfo() will set prtad and mmds properly */
9841         bp->mdio.prtad = MDIO_PRTAD_NONE;
9842         bp->mdio.mmds = 0;
9843         bp->mdio.mode_support = MDIO_SUPPORTS_C45 | MDIO_EMULATE_C22;
9844         bp->mdio.dev = dev;
9845         bp->mdio.mdio_read = bnx2x_mdio_read;
9846         bp->mdio.mdio_write = bnx2x_mdio_write;
9847
9848         return 0;
9849
9850 err_out_unmap:
9851         if (bp->regview) {
9852                 iounmap(bp->regview);
9853                 bp->regview = NULL;
9854         }
9855         if (bp->doorbells) {
9856                 iounmap(bp->doorbells);
9857                 bp->doorbells = NULL;
9858         }
9859
9860 err_out_release:
9861         if (atomic_read(&pdev->enable_cnt) == 1)
9862                 pci_release_regions(pdev);
9863
9864 err_out_disable:
9865         pci_disable_device(pdev);
9866         pci_set_drvdata(pdev, NULL);
9867
9868 err_out:
9869         return rc;
9870 }
9871
9872 static void __devinit bnx2x_get_pcie_width_speed(struct bnx2x *bp,
9873                                                  int *width, int *speed)
9874 {
9875         u32 val = REG_RD(bp, PCICFG_OFFSET + PCICFG_LINK_CONTROL);
9876
9877         *width = (val & PCICFG_LINK_WIDTH) >> PCICFG_LINK_WIDTH_SHIFT;
9878
9879         /* return value of 1=2.5GHz 2=5GHz */
9880         *speed = (val & PCICFG_LINK_SPEED) >> PCICFG_LINK_SPEED_SHIFT;
9881 }
9882
9883 static int bnx2x_check_firmware(struct bnx2x *bp)
9884 {
9885         const struct firmware *firmware = bp->firmware;
9886         struct bnx2x_fw_file_hdr *fw_hdr;
9887         struct bnx2x_fw_file_section *sections;
9888         u32 offset, len, num_ops;
9889         u16 *ops_offsets;
9890         int i;
9891         const u8 *fw_ver;
9892
9893         if (firmware->size < sizeof(struct bnx2x_fw_file_hdr))
9894                 return -EINVAL;
9895
9896         fw_hdr = (struct bnx2x_fw_file_hdr *)firmware->data;
9897         sections = (struct bnx2x_fw_file_section *)fw_hdr;
9898
9899         /* Make sure none of the offsets and sizes make us read beyond
9900          * the end of the firmware data */
9901         for (i = 0; i < sizeof(*fw_hdr) / sizeof(*sections); i++) {
9902                 offset = be32_to_cpu(sections[i].offset);
9903                 len = be32_to_cpu(sections[i].len);
9904                 if (offset + len > firmware->size) {
9905                         dev_err(&bp->pdev->dev,
9906                                 "Section %d length is out of bounds\n", i);
9907                         return -EINVAL;
9908                 }
9909         }
9910
9911         /* Likewise for the init_ops offsets */
9912         offset = be32_to_cpu(fw_hdr->init_ops_offsets.offset);
9913         ops_offsets = (u16 *)(firmware->data + offset);
9914         num_ops = be32_to_cpu(fw_hdr->init_ops.len) / sizeof(struct raw_op);
9915
9916         for (i = 0; i < be32_to_cpu(fw_hdr->init_ops_offsets.len) / 2; i++) {
9917                 if (be16_to_cpu(ops_offsets[i]) > num_ops) {
9918                         dev_err(&bp->pdev->dev,
9919                                 "Section offset %d is out of bounds\n", i);
9920                         return -EINVAL;
9921                 }
9922         }
9923
9924         /* Check FW version */
9925         offset = be32_to_cpu(fw_hdr->fw_version.offset);
9926         fw_ver = firmware->data + offset;
9927         if ((fw_ver[0] != BCM_5710_FW_MAJOR_VERSION) ||
9928             (fw_ver[1] != BCM_5710_FW_MINOR_VERSION) ||
9929             (fw_ver[2] != BCM_5710_FW_REVISION_VERSION) ||
9930             (fw_ver[3] != BCM_5710_FW_ENGINEERING_VERSION)) {
9931                 dev_err(&bp->pdev->dev,
9932                         "Bad FW version:%d.%d.%d.%d. Should be %d.%d.%d.%d\n",
9933                        fw_ver[0], fw_ver[1], fw_ver[2],
9934                        fw_ver[3], BCM_5710_FW_MAJOR_VERSION,
9935                        BCM_5710_FW_MINOR_VERSION,
9936                        BCM_5710_FW_REVISION_VERSION,
9937                        BCM_5710_FW_ENGINEERING_VERSION);
9938                 return -EINVAL;
9939         }
9940
9941         return 0;
9942 }
9943
9944 static inline void be32_to_cpu_n(const u8 *_source, u8 *_target, u32 n)
9945 {
9946         const __be32 *source = (const __be32 *)_source;
9947         u32 *target = (u32 *)_target;
9948         u32 i;
9949
9950         for (i = 0; i < n/4; i++)
9951                 target[i] = be32_to_cpu(source[i]);
9952 }
9953
9954 /*
9955    Ops array is stored in the following format:
9956    {op(8bit), offset(24bit, big endian), data(32bit, big endian)}
9957  */
9958 static inline void bnx2x_prep_ops(const u8 *_source, u8 *_target, u32 n)
9959 {
9960         const __be32 *source = (const __be32 *)_source;
9961         struct raw_op *target = (struct raw_op *)_target;
9962         u32 i, j, tmp;
9963
9964         for (i = 0, j = 0; i < n/8; i++, j += 2) {
9965                 tmp = be32_to_cpu(source[j]);
9966                 target[i].op = (tmp >> 24) & 0xff;
9967                 target[i].offset = tmp & 0xffffff;
9968                 target[i].raw_data = be32_to_cpu(source[j + 1]);
9969         }
9970 }
9971
9972 /**
9973  * IRO array is stored in the following format:
9974  * {base(24bit), m1(16bit), m2(16bit), m3(16bit), size(16bit) }
9975  */
9976 static inline void bnx2x_prep_iro(const u8 *_source, u8 *_target, u32 n)
9977 {
9978         const __be32 *source = (const __be32 *)_source;
9979         struct iro *target = (struct iro *)_target;
9980         u32 i, j, tmp;
9981
9982         for (i = 0, j = 0; i < n/sizeof(struct iro); i++) {
9983                 target[i].base = be32_to_cpu(source[j]);
9984                 j++;
9985                 tmp = be32_to_cpu(source[j]);
9986                 target[i].m1 = (tmp >> 16) & 0xffff;
9987                 target[i].m2 = tmp & 0xffff;
9988                 j++;
9989                 tmp = be32_to_cpu(source[j]);
9990                 target[i].m3 = (tmp >> 16) & 0xffff;
9991                 target[i].size = tmp & 0xffff;
9992                 j++;
9993         }
9994 }
9995
9996 static inline void be16_to_cpu_n(const u8 *_source, u8 *_target, u32 n)
9997 {
9998         const __be16 *source = (const __be16 *)_source;
9999         u16 *target = (u16 *)_target;
10000         u32 i;
10001
10002         for (i = 0; i < n/2; i++)
10003                 target[i] = be16_to_cpu(source[i]);
10004 }
10005
10006 #define BNX2X_ALLOC_AND_SET(arr, lbl, func)                             \
10007 do {                                                                    \
10008         u32 len = be32_to_cpu(fw_hdr->arr.len);                         \
10009         bp->arr = kmalloc(len, GFP_KERNEL);                             \
10010         if (!bp->arr) {                                                 \
10011                 pr_err("Failed to allocate %d bytes for "#arr"\n", len); \
10012                 goto lbl;                                               \
10013         }                                                               \
10014         func(bp->firmware->data + be32_to_cpu(fw_hdr->arr.offset),      \
10015              (u8 *)bp->arr, len);                                       \
10016 } while (0)
10017
10018 int bnx2x_init_firmware(struct bnx2x *bp)
10019 {
10020         const char *fw_file_name;
10021         struct bnx2x_fw_file_hdr *fw_hdr;
10022         int rc;
10023
10024         if (CHIP_IS_E1(bp))
10025                 fw_file_name = FW_FILE_NAME_E1;
10026         else if (CHIP_IS_E1H(bp))
10027                 fw_file_name = FW_FILE_NAME_E1H;
10028         else if (!CHIP_IS_E1x(bp))
10029                 fw_file_name = FW_FILE_NAME_E2;
10030         else {
10031                 BNX2X_ERR("Unsupported chip revision\n");
10032                 return -EINVAL;
10033         }
10034
10035         BNX2X_DEV_INFO("Loading %s\n", fw_file_name);
10036
10037         rc = request_firmware(&bp->firmware, fw_file_name, &bp->pdev->dev);
10038         if (rc) {
10039                 BNX2X_ERR("Can't load firmware file %s\n", fw_file_name);
10040                 goto request_firmware_exit;
10041         }
10042
10043         rc = bnx2x_check_firmware(bp);
10044         if (rc) {
10045                 BNX2X_ERR("Corrupt firmware file %s\n", fw_file_name);
10046                 goto request_firmware_exit;
10047         }
10048
10049         fw_hdr = (struct bnx2x_fw_file_hdr *)bp->firmware->data;
10050
10051         /* Initialize the pointers to the init arrays */
10052         /* Blob */
10053         BNX2X_ALLOC_AND_SET(init_data, request_firmware_exit, be32_to_cpu_n);
10054
10055         /* Opcodes */
10056         BNX2X_ALLOC_AND_SET(init_ops, init_ops_alloc_err, bnx2x_prep_ops);
10057
10058         /* Offsets */
10059         BNX2X_ALLOC_AND_SET(init_ops_offsets, init_offsets_alloc_err,
10060                             be16_to_cpu_n);
10061
10062         /* STORMs firmware */
10063         INIT_TSEM_INT_TABLE_DATA(bp) = bp->firmware->data +
10064                         be32_to_cpu(fw_hdr->tsem_int_table_data.offset);
10065         INIT_TSEM_PRAM_DATA(bp)      = bp->firmware->data +
10066                         be32_to_cpu(fw_hdr->tsem_pram_data.offset);
10067         INIT_USEM_INT_TABLE_DATA(bp) = bp->firmware->data +
10068                         be32_to_cpu(fw_hdr->usem_int_table_data.offset);
10069         INIT_USEM_PRAM_DATA(bp)      = bp->firmware->data +
10070                         be32_to_cpu(fw_hdr->usem_pram_data.offset);
10071         INIT_XSEM_INT_TABLE_DATA(bp) = bp->firmware->data +
10072                         be32_to_cpu(fw_hdr->xsem_int_table_data.offset);
10073         INIT_XSEM_PRAM_DATA(bp)      = bp->firmware->data +
10074                         be32_to_cpu(fw_hdr->xsem_pram_data.offset);
10075         INIT_CSEM_INT_TABLE_DATA(bp) = bp->firmware->data +
10076                         be32_to_cpu(fw_hdr->csem_int_table_data.offset);
10077         INIT_CSEM_PRAM_DATA(bp)      = bp->firmware->data +
10078                         be32_to_cpu(fw_hdr->csem_pram_data.offset);
10079         /* IRO */
10080         BNX2X_ALLOC_AND_SET(iro_arr, iro_alloc_err, bnx2x_prep_iro);
10081
10082         return 0;
10083
10084 iro_alloc_err:
10085         kfree(bp->init_ops_offsets);
10086 init_offsets_alloc_err:
10087         kfree(bp->init_ops);
10088 init_ops_alloc_err:
10089         kfree(bp->init_data);
10090 request_firmware_exit:
10091         release_firmware(bp->firmware);
10092
10093         return rc;
10094 }
10095
10096 static void bnx2x_release_firmware(struct bnx2x *bp)
10097 {
10098         kfree(bp->init_ops_offsets);
10099         kfree(bp->init_ops);
10100         kfree(bp->init_data);
10101         release_firmware(bp->firmware);
10102 }
10103
10104
10105 static struct bnx2x_func_sp_drv_ops bnx2x_func_sp_drv = {
10106         .init_hw_cmn_chip = bnx2x_init_hw_common_chip,
10107         .init_hw_cmn      = bnx2x_init_hw_common,
10108         .init_hw_port     = bnx2x_init_hw_port,
10109         .init_hw_func     = bnx2x_init_hw_func,
10110
10111         .reset_hw_cmn     = bnx2x_reset_common,
10112         .reset_hw_port    = bnx2x_reset_port,
10113         .reset_hw_func    = bnx2x_reset_func,
10114
10115         .gunzip_init      = bnx2x_gunzip_init,
10116         .gunzip_end       = bnx2x_gunzip_end,
10117
10118         .init_fw          = bnx2x_init_firmware,
10119         .release_fw       = bnx2x_release_firmware,
10120 };
10121
10122 void bnx2x__init_func_obj(struct bnx2x *bp)
10123 {
10124         /* Prepare DMAE related driver resources */
10125         bnx2x_setup_dmae(bp);
10126
10127         bnx2x_init_func_obj(bp, &bp->func_obj,
10128                             bnx2x_sp(bp, func_rdata),
10129                             bnx2x_sp_mapping(bp, func_rdata),
10130                             &bnx2x_func_sp_drv);
10131 }
10132
10133 /* must be called after sriov-enable */
10134 static inline int bnx2x_set_qm_cid_count(struct bnx2x *bp, int l2_cid_count)
10135 {
10136         int cid_count = L2_FP_COUNT(l2_cid_count);
10137
10138 #ifdef BCM_CNIC
10139         cid_count += CNIC_CID_MAX;
10140 #endif
10141         return roundup(cid_count, QM_CID_ROUND);
10142 }
10143
10144 /**
10145  * bnx2x_pci_msix_table_size - get the size of the MSI-X table.
10146  *
10147  * @dev:        pci device
10148  *
10149  */
10150 static inline int bnx2x_pci_msix_table_size(struct pci_dev *pdev)
10151 {
10152         int pos;
10153         u16 control;
10154
10155         pos = pci_find_capability(pdev, PCI_CAP_ID_MSIX);
10156         if (!pos)
10157                 return 0;
10158
10159         pci_read_config_word(pdev, pos  + PCI_MSI_FLAGS, &control);
10160         return (control & PCI_MSIX_FLAGS_QSIZE) + 1;
10161 }
10162
10163 static int __devinit bnx2x_init_one(struct pci_dev *pdev,
10164                                     const struct pci_device_id *ent)
10165 {
10166         struct net_device *dev = NULL;
10167         struct bnx2x *bp;
10168         int pcie_width, pcie_speed;
10169         int rc, cid_count;
10170
10171         switch (ent->driver_data) {
10172         case BCM57710:
10173         case BCM57711:
10174         case BCM57711E:
10175         case BCM57712:
10176         case BCM57712_MF:
10177         case BCM57800:
10178         case BCM57800_MF:
10179         case BCM57810:
10180         case BCM57810_MF:
10181         case BCM57840:
10182         case BCM57840_MF:
10183                 /* The size requested for the MSI-X table corresponds to the
10184                  * actual amount of avaliable IGU/HC status blocks. It includes
10185                  * the default SB vector but we want cid_count to contain the
10186                  * amount of only non-default SBs, that's what '-1' stands for.
10187                  */
10188                 cid_count = bnx2x_pci_msix_table_size(pdev) - 1;
10189
10190                 /* do not allow initial cid_count grow above 16
10191                  * since Special CIDs starts from this number
10192                  * use old FP_SB_MAX_E1x define for this matter
10193                  */
10194                 cid_count = min_t(int, FP_SB_MAX_E1x, cid_count);
10195
10196                 WARN_ON(!cid_count);
10197                 break;
10198
10199         default:
10200                 pr_err("Unknown board_type (%ld), aborting\n",
10201                            ent->driver_data);
10202                 return -ENODEV;
10203         }
10204
10205         cid_count += FCOE_CONTEXT_USE;
10206
10207         /* dev zeroed in init_etherdev */
10208         dev = alloc_etherdev_mq(sizeof(*bp), cid_count);
10209         if (!dev) {
10210                 dev_err(&pdev->dev, "Cannot allocate net device\n");
10211                 return -ENOMEM;
10212         }
10213
10214         /* We don't need a Tx queue for a CNIC and an OOO Rx-only ring,
10215          * so update a cid_count after a netdev allocation.
10216          */
10217         cid_count += CNIC_CONTEXT_USE;
10218
10219         bp = netdev_priv(dev);
10220         bp->msg_enable = debug;
10221
10222         pci_set_drvdata(pdev, dev);
10223
10224         bp->l2_cid_count = cid_count;
10225
10226         rc = bnx2x_init_dev(pdev, dev, ent->driver_data);
10227         if (rc < 0) {
10228                 free_netdev(dev);
10229                 return rc;
10230         }
10231
10232         BNX2X_DEV_INFO("cid_count=%d\n", cid_count);
10233
10234         rc = bnx2x_init_bp(bp);
10235         if (rc)
10236                 goto init_one_exit;
10237
10238         /* calc qm_cid_count */
10239         bp->qm_cid_count = bnx2x_set_qm_cid_count(bp, cid_count);
10240
10241 #ifdef BCM_CNIC
10242         /* disable FCOE L2 queue for E1x*/
10243         if (CHIP_IS_E1x(bp))
10244                 bp->flags |= NO_FCOE_FLAG;
10245
10246 #endif
10247
10248         /* Configure interrupt mode: try to enable MSI-X/MSI if
10249          * needed, set bp->num_queues appropriately.
10250          */
10251         bnx2x_set_int_mode(bp);
10252
10253         /* Add all NAPI objects */
10254         bnx2x_add_all_napi(bp);
10255
10256         rc = register_netdev(dev);
10257         if (rc) {
10258                 dev_err(&pdev->dev, "Cannot register net device\n");
10259                 goto init_one_exit;
10260         }
10261
10262 #ifdef BCM_CNIC
10263         if (!NO_FCOE(bp)) {
10264                 /* Add storage MAC address */
10265                 rtnl_lock();
10266                 dev_addr_add(bp->dev, bp->fip_mac, NETDEV_HW_ADDR_T_SAN);
10267                 rtnl_unlock();
10268         }
10269 #endif
10270
10271         bnx2x_get_pcie_width_speed(bp, &pcie_width, &pcie_speed);
10272
10273         netdev_info(dev, "%s (%c%d) PCI-E x%d %s found at mem %lx,"
10274                " IRQ %d, ", board_info[ent->driver_data].name,
10275                (CHIP_REV(bp) >> 12) + 'A', (CHIP_METAL(bp) >> 4),
10276                pcie_width,
10277                ((!CHIP_IS_E2(bp) && pcie_speed == 2) ||
10278                  (CHIP_IS_E2(bp) && pcie_speed == 1)) ?
10279                                                 "5GHz (Gen2)" : "2.5GHz",
10280                dev->base_addr, bp->pdev->irq);
10281         pr_cont("node addr %pM\n", dev->dev_addr);
10282
10283         return 0;
10284
10285 init_one_exit:
10286         if (bp->regview)
10287                 iounmap(bp->regview);
10288
10289         if (bp->doorbells)
10290                 iounmap(bp->doorbells);
10291
10292         free_netdev(dev);
10293
10294         if (atomic_read(&pdev->enable_cnt) == 1)
10295                 pci_release_regions(pdev);
10296
10297         pci_disable_device(pdev);
10298         pci_set_drvdata(pdev, NULL);
10299
10300         return rc;
10301 }
10302
10303 static void __devexit bnx2x_remove_one(struct pci_dev *pdev)
10304 {
10305         struct net_device *dev = pci_get_drvdata(pdev);
10306         struct bnx2x *bp;
10307
10308         if (!dev) {
10309                 dev_err(&pdev->dev, "BAD net device from bnx2x_init_one\n");
10310                 return;
10311         }
10312         bp = netdev_priv(dev);
10313
10314 #ifdef BCM_CNIC
10315         /* Delete storage MAC address */
10316         if (!NO_FCOE(bp)) {
10317                 rtnl_lock();
10318                 dev_addr_del(bp->dev, bp->fip_mac, NETDEV_HW_ADDR_T_SAN);
10319                 rtnl_unlock();
10320         }
10321 #endif
10322
10323 #ifdef BCM_DCBNL
10324         /* Delete app tlvs from dcbnl */
10325         bnx2x_dcbnl_update_applist(bp, true);
10326 #endif
10327
10328         unregister_netdev(dev);
10329
10330         /* Delete all NAPI objects */
10331         bnx2x_del_all_napi(bp);
10332
10333         /* Power on: we can't let PCI layer write to us while we are in D3 */
10334         bnx2x_set_power_state(bp, PCI_D0);
10335
10336         /* Disable MSI/MSI-X */
10337         bnx2x_disable_msi(bp);
10338
10339         /* Power off */
10340         bnx2x_set_power_state(bp, PCI_D3hot);
10341
10342         /* Make sure RESET task is not scheduled before continuing */
10343         cancel_delayed_work_sync(&bp->reset_task);
10344
10345         if (bp->regview)
10346                 iounmap(bp->regview);
10347
10348         if (bp->doorbells)
10349                 iounmap(bp->doorbells);
10350
10351         bnx2x_free_mem_bp(bp);
10352
10353         free_netdev(dev);
10354
10355         if (atomic_read(&pdev->enable_cnt) == 1)
10356                 pci_release_regions(pdev);
10357
10358         pci_disable_device(pdev);
10359         pci_set_drvdata(pdev, NULL);
10360 }
10361
10362 static int bnx2x_eeh_nic_unload(struct bnx2x *bp)
10363 {
10364         int i;
10365
10366         bp->state = BNX2X_STATE_ERROR;
10367
10368         bp->rx_mode = BNX2X_RX_MODE_NONE;
10369
10370 #ifdef BCM_CNIC
10371         bnx2x_cnic_notify(bp, CNIC_CTL_STOP_CMD);
10372 #endif
10373         /* Stop Tx */
10374         bnx2x_tx_disable(bp);
10375
10376         bnx2x_netif_stop(bp, 0);
10377
10378         del_timer_sync(&bp->timer);
10379
10380         bnx2x_stats_handle(bp, STATS_EVENT_STOP);
10381
10382         /* Release IRQs */
10383         bnx2x_free_irq(bp);
10384
10385         /* Free SKBs, SGEs, TPA pool and driver internals */
10386         bnx2x_free_skbs(bp);
10387
10388         for_each_rx_queue(bp, i)
10389                 bnx2x_free_rx_sge_range(bp, bp->fp + i, NUM_RX_SGE);
10390
10391         bnx2x_free_mem(bp);
10392
10393         bp->state = BNX2X_STATE_CLOSED;
10394
10395         netif_carrier_off(bp->dev);
10396
10397         return 0;
10398 }
10399
10400 static void bnx2x_eeh_recover(struct bnx2x *bp)
10401 {
10402         u32 val;
10403
10404         mutex_init(&bp->port.phy_mutex);
10405
10406         bp->common.shmem_base = REG_RD(bp, MISC_REG_SHARED_MEM_ADDR);
10407         bp->link_params.shmem_base = bp->common.shmem_base;
10408         BNX2X_DEV_INFO("shmem offset is 0x%x\n", bp->common.shmem_base);
10409
10410         if (!bp->common.shmem_base ||
10411             (bp->common.shmem_base < 0xA0000) ||
10412             (bp->common.shmem_base >= 0xC0000)) {
10413                 BNX2X_DEV_INFO("MCP not active\n");
10414                 bp->flags |= NO_MCP_FLAG;
10415                 return;
10416         }
10417
10418         val = SHMEM_RD(bp, validity_map[BP_PORT(bp)]);
10419         if ((val & (SHR_MEM_VALIDITY_DEV_INFO | SHR_MEM_VALIDITY_MB))
10420                 != (SHR_MEM_VALIDITY_DEV_INFO | SHR_MEM_VALIDITY_MB))
10421                 BNX2X_ERR("BAD MCP validity signature\n");
10422
10423         if (!BP_NOMCP(bp)) {
10424                 bp->fw_seq =
10425                     (SHMEM_RD(bp, func_mb[BP_FW_MB_IDX(bp)].drv_mb_header) &
10426                     DRV_MSG_SEQ_NUMBER_MASK);
10427                 BNX2X_DEV_INFO("fw_seq 0x%08x\n", bp->fw_seq);
10428         }
10429 }
10430
10431 /**
10432  * bnx2x_io_error_detected - called when PCI error is detected
10433  * @pdev: Pointer to PCI device
10434  * @state: The current pci connection state
10435  *
10436  * This function is called after a PCI bus error affecting
10437  * this device has been detected.
10438  */
10439 static pci_ers_result_t bnx2x_io_error_detected(struct pci_dev *pdev,
10440                                                 pci_channel_state_t state)
10441 {
10442         struct net_device *dev = pci_get_drvdata(pdev);
10443         struct bnx2x *bp = netdev_priv(dev);
10444
10445         rtnl_lock();
10446
10447         netif_device_detach(dev);
10448
10449         if (state == pci_channel_io_perm_failure) {
10450                 rtnl_unlock();
10451                 return PCI_ERS_RESULT_DISCONNECT;
10452         }
10453
10454         if (netif_running(dev))
10455                 bnx2x_eeh_nic_unload(bp);
10456
10457         pci_disable_device(pdev);
10458
10459         rtnl_unlock();
10460
10461         /* Request a slot reset */
10462         return PCI_ERS_RESULT_NEED_RESET;
10463 }
10464
10465 /**
10466  * bnx2x_io_slot_reset - called after the PCI bus has been reset
10467  * @pdev: Pointer to PCI device
10468  *
10469  * Restart the card from scratch, as if from a cold-boot.
10470  */
10471 static pci_ers_result_t bnx2x_io_slot_reset(struct pci_dev *pdev)
10472 {
10473         struct net_device *dev = pci_get_drvdata(pdev);
10474         struct bnx2x *bp = netdev_priv(dev);
10475
10476         rtnl_lock();
10477
10478         if (pci_enable_device(pdev)) {
10479                 dev_err(&pdev->dev,
10480                         "Cannot re-enable PCI device after reset\n");
10481                 rtnl_unlock();
10482                 return PCI_ERS_RESULT_DISCONNECT;
10483         }
10484
10485         pci_set_master(pdev);
10486         pci_restore_state(pdev);
10487
10488         if (netif_running(dev))
10489                 bnx2x_set_power_state(bp, PCI_D0);
10490
10491         rtnl_unlock();
10492
10493         return PCI_ERS_RESULT_RECOVERED;
10494 }
10495
10496 /**
10497  * bnx2x_io_resume - called when traffic can start flowing again
10498  * @pdev: Pointer to PCI device
10499  *
10500  * This callback is called when the error recovery driver tells us that
10501  * its OK to resume normal operation.
10502  */
10503 static void bnx2x_io_resume(struct pci_dev *pdev)
10504 {
10505         struct net_device *dev = pci_get_drvdata(pdev);
10506         struct bnx2x *bp = netdev_priv(dev);
10507
10508         if (bp->recovery_state != BNX2X_RECOVERY_DONE) {
10509                 netdev_err(bp->dev, "Handling parity error recovery. "
10510                                     "Try again later\n");
10511                 return;
10512         }
10513
10514         rtnl_lock();
10515
10516         bnx2x_eeh_recover(bp);
10517
10518         if (netif_running(dev))
10519                 bnx2x_nic_load(bp, LOAD_NORMAL);
10520
10521         netif_device_attach(dev);
10522
10523         rtnl_unlock();
10524 }
10525
10526 static struct pci_error_handlers bnx2x_err_handler = {
10527         .error_detected = bnx2x_io_error_detected,
10528         .slot_reset     = bnx2x_io_slot_reset,
10529         .resume         = bnx2x_io_resume,
10530 };
10531
10532 static struct pci_driver bnx2x_pci_driver = {
10533         .name        = DRV_MODULE_NAME,
10534         .id_table    = bnx2x_pci_tbl,
10535         .probe       = bnx2x_init_one,
10536         .remove      = __devexit_p(bnx2x_remove_one),
10537         .suspend     = bnx2x_suspend,
10538         .resume      = bnx2x_resume,
10539         .err_handler = &bnx2x_err_handler,
10540 };
10541
10542 static int __init bnx2x_init(void)
10543 {
10544         int ret;
10545
10546         pr_info("%s", version);
10547
10548         bnx2x_wq = create_singlethread_workqueue("bnx2x");
10549         if (bnx2x_wq == NULL) {
10550                 pr_err("Cannot create workqueue\n");
10551                 return -ENOMEM;
10552         }
10553
10554         ret = pci_register_driver(&bnx2x_pci_driver);
10555         if (ret) {
10556                 pr_err("Cannot register driver\n");
10557                 destroy_workqueue(bnx2x_wq);
10558         }
10559         return ret;
10560 }
10561
10562 static void __exit bnx2x_cleanup(void)
10563 {
10564         pci_unregister_driver(&bnx2x_pci_driver);
10565
10566         destroy_workqueue(bnx2x_wq);
10567 }
10568
10569 void bnx2x_notify_link_changed(struct bnx2x *bp)
10570 {
10571         REG_WR(bp, MISC_REG_AEU_GENERAL_ATTN_12 + BP_FUNC(bp)*sizeof(u32), 1);
10572 }
10573
10574 module_init(bnx2x_init);
10575 module_exit(bnx2x_cleanup);
10576
10577 #ifdef BCM_CNIC
10578 /**
10579  * bnx2x_set_iscsi_eth_mac_addr - set iSCSI MAC(s).
10580  *
10581  * @bp:         driver handle
10582  * @set:        set or clear the CAM entry
10583  *
10584  * This function will wait until the ramdord completion returns.
10585  * Return 0 if success, -ENODEV if ramrod doesn't return.
10586  */
10587 static inline int bnx2x_set_iscsi_eth_mac_addr(struct bnx2x *bp)
10588 {
10589         unsigned long ramrod_flags = 0;
10590
10591         __set_bit(RAMROD_COMP_WAIT, &ramrod_flags);
10592         return bnx2x_set_mac_one(bp, bp->cnic_eth_dev.iscsi_mac,
10593                                  &bp->iscsi_l2_mac_obj, true,
10594                                  BNX2X_ISCSI_ETH_MAC, &ramrod_flags);
10595 }
10596
10597 /* count denotes the number of new completions we have seen */
10598 static void bnx2x_cnic_sp_post(struct bnx2x *bp, int count)
10599 {
10600         struct eth_spe *spe;
10601
10602 #ifdef BNX2X_STOP_ON_ERROR
10603         if (unlikely(bp->panic))
10604                 return;
10605 #endif
10606
10607         spin_lock_bh(&bp->spq_lock);
10608         BUG_ON(bp->cnic_spq_pending < count);
10609         bp->cnic_spq_pending -= count;
10610
10611
10612         for (; bp->cnic_kwq_pending; bp->cnic_kwq_pending--) {
10613                 u16 type =  (le16_to_cpu(bp->cnic_kwq_cons->hdr.type)
10614                                 & SPE_HDR_CONN_TYPE) >>
10615                                 SPE_HDR_CONN_TYPE_SHIFT;
10616                 u8 cmd = (le32_to_cpu(bp->cnic_kwq_cons->hdr.conn_and_cmd_data)
10617                                 >> SPE_HDR_CMD_ID_SHIFT) & 0xff;
10618
10619                 /* Set validation for iSCSI L2 client before sending SETUP
10620                  *  ramrod
10621                  */
10622                 if (type == ETH_CONNECTION_TYPE) {
10623                         if (cmd == RAMROD_CMD_ID_ETH_CLIENT_SETUP)
10624                                 bnx2x_set_ctx_validation(bp, &bp->context.
10625                                         vcxt[BNX2X_ISCSI_ETH_CID].eth,
10626                                         BNX2X_ISCSI_ETH_CID);
10627                 }
10628
10629                 /*
10630                  * There may be not more than 8 L2, not more than 8 L5 SPEs
10631                  * and in the air. We also check that number of outstanding
10632                  * COMMON ramrods is not more than the EQ and SPQ can
10633                  * accommodate.
10634                  */
10635                 if (type == ETH_CONNECTION_TYPE) {
10636                         if (!atomic_read(&bp->cq_spq_left))
10637                                 break;
10638                         else
10639                                 atomic_dec(&bp->cq_spq_left);
10640                 } else if (type == NONE_CONNECTION_TYPE) {
10641                         if (!atomic_read(&bp->eq_spq_left))
10642                                 break;
10643                         else
10644                                 atomic_dec(&bp->eq_spq_left);
10645                 } else if ((type == ISCSI_CONNECTION_TYPE) ||
10646                            (type == FCOE_CONNECTION_TYPE)) {
10647                         if (bp->cnic_spq_pending >=
10648                             bp->cnic_eth_dev.max_kwqe_pending)
10649                                 break;
10650                         else
10651                                 bp->cnic_spq_pending++;
10652                 } else {
10653                         BNX2X_ERR("Unknown SPE type: %d\n", type);
10654                         bnx2x_panic();
10655                         break;
10656                 }
10657
10658                 spe = bnx2x_sp_get_next(bp);
10659                 *spe = *bp->cnic_kwq_cons;
10660
10661                 DP(NETIF_MSG_TIMER, "pending on SPQ %d, on KWQ %d count %d\n",
10662                    bp->cnic_spq_pending, bp->cnic_kwq_pending, count);
10663
10664                 if (bp->cnic_kwq_cons == bp->cnic_kwq_last)
10665                         bp->cnic_kwq_cons = bp->cnic_kwq;
10666                 else
10667                         bp->cnic_kwq_cons++;
10668         }
10669         bnx2x_sp_prod_update(bp);
10670         spin_unlock_bh(&bp->spq_lock);
10671 }
10672
10673 static int bnx2x_cnic_sp_queue(struct net_device *dev,
10674                                struct kwqe_16 *kwqes[], u32 count)
10675 {
10676         struct bnx2x *bp = netdev_priv(dev);
10677         int i;
10678
10679 #ifdef BNX2X_STOP_ON_ERROR
10680         if (unlikely(bp->panic))
10681                 return -EIO;
10682 #endif
10683
10684         spin_lock_bh(&bp->spq_lock);
10685
10686         for (i = 0; i < count; i++) {
10687                 struct eth_spe *spe = (struct eth_spe *)kwqes[i];
10688
10689                 if (bp->cnic_kwq_pending == MAX_SP_DESC_CNT)
10690                         break;
10691
10692                 *bp->cnic_kwq_prod = *spe;
10693
10694                 bp->cnic_kwq_pending++;
10695
10696                 DP(NETIF_MSG_TIMER, "L5 SPQE %x %x %x:%x pos %d\n",
10697                    spe->hdr.conn_and_cmd_data, spe->hdr.type,
10698                    spe->data.update_data_addr.hi,
10699                    spe->data.update_data_addr.lo,
10700                    bp->cnic_kwq_pending);
10701
10702                 if (bp->cnic_kwq_prod == bp->cnic_kwq_last)
10703                         bp->cnic_kwq_prod = bp->cnic_kwq;
10704                 else
10705                         bp->cnic_kwq_prod++;
10706         }
10707
10708         spin_unlock_bh(&bp->spq_lock);
10709
10710         if (bp->cnic_spq_pending < bp->cnic_eth_dev.max_kwqe_pending)
10711                 bnx2x_cnic_sp_post(bp, 0);
10712
10713         return i;
10714 }
10715
10716 static int bnx2x_cnic_ctl_send(struct bnx2x *bp, struct cnic_ctl_info *ctl)
10717 {
10718         struct cnic_ops *c_ops;
10719         int rc = 0;
10720
10721         mutex_lock(&bp->cnic_mutex);
10722         c_ops = rcu_dereference_protected(bp->cnic_ops,
10723                                           lockdep_is_held(&bp->cnic_mutex));
10724         if (c_ops)
10725                 rc = c_ops->cnic_ctl(bp->cnic_data, ctl);
10726         mutex_unlock(&bp->cnic_mutex);
10727
10728         return rc;
10729 }
10730
10731 static int bnx2x_cnic_ctl_send_bh(struct bnx2x *bp, struct cnic_ctl_info *ctl)
10732 {
10733         struct cnic_ops *c_ops;
10734         int rc = 0;
10735
10736         rcu_read_lock();
10737         c_ops = rcu_dereference(bp->cnic_ops);
10738         if (c_ops)
10739                 rc = c_ops->cnic_ctl(bp->cnic_data, ctl);
10740         rcu_read_unlock();
10741
10742         return rc;
10743 }
10744
10745 /*
10746  * for commands that have no data
10747  */
10748 int bnx2x_cnic_notify(struct bnx2x *bp, int cmd)
10749 {
10750         struct cnic_ctl_info ctl = {0};
10751
10752         ctl.cmd = cmd;
10753
10754         return bnx2x_cnic_ctl_send(bp, &ctl);
10755 }
10756
10757 static void bnx2x_cnic_cfc_comp(struct bnx2x *bp, int cid, u8 err)
10758 {
10759         struct cnic_ctl_info ctl = {0};
10760
10761         /* first we tell CNIC and only then we count this as a completion */
10762         ctl.cmd = CNIC_CTL_COMPLETION_CMD;
10763         ctl.data.comp.cid = cid;
10764         ctl.data.comp.error = err;
10765
10766         bnx2x_cnic_ctl_send_bh(bp, &ctl);
10767         bnx2x_cnic_sp_post(bp, 0);
10768 }
10769
10770
10771 /* Called with netif_addr_lock_bh() taken.
10772  * Sets an rx_mode config for an iSCSI ETH client.
10773  * Doesn't block.
10774  * Completion should be checked outside.
10775  */
10776 static void bnx2x_set_iscsi_eth_rx_mode(struct bnx2x *bp, bool start)
10777 {
10778         unsigned long accept_flags = 0, ramrod_flags = 0;
10779         u8 cl_id = bnx2x_cnic_eth_cl_id(bp, BNX2X_ISCSI_ETH_CL_ID_IDX);
10780         int sched_state = BNX2X_FILTER_ISCSI_ETH_STOP_SCHED;
10781
10782         if (start) {
10783                 /* Start accepting on iSCSI L2 ring. Accept all multicasts
10784                  * because it's the only way for UIO Queue to accept
10785                  * multicasts (in non-promiscuous mode only one Queue per
10786                  * function will receive multicast packets (leading in our
10787                  * case).
10788                  */
10789                 __set_bit(BNX2X_ACCEPT_UNICAST, &accept_flags);
10790                 __set_bit(BNX2X_ACCEPT_ALL_MULTICAST, &accept_flags);
10791                 __set_bit(BNX2X_ACCEPT_BROADCAST, &accept_flags);
10792                 __set_bit(BNX2X_ACCEPT_ANY_VLAN, &accept_flags);
10793
10794                 /* Clear STOP_PENDING bit if START is requested */
10795                 clear_bit(BNX2X_FILTER_ISCSI_ETH_STOP_SCHED, &bp->sp_state);
10796
10797                 sched_state = BNX2X_FILTER_ISCSI_ETH_START_SCHED;
10798         } else
10799                 /* Clear START_PENDING bit if STOP is requested */
10800                 clear_bit(BNX2X_FILTER_ISCSI_ETH_START_SCHED, &bp->sp_state);
10801
10802         if (test_bit(BNX2X_FILTER_RX_MODE_PENDING, &bp->sp_state))
10803                 set_bit(sched_state, &bp->sp_state);
10804         else {
10805                 __set_bit(RAMROD_RX, &ramrod_flags);
10806                 bnx2x_set_q_rx_mode(bp, cl_id, 0, accept_flags, 0,
10807                                     ramrod_flags);
10808         }
10809 }
10810
10811
10812 static int bnx2x_drv_ctl(struct net_device *dev, struct drv_ctl_info *ctl)
10813 {
10814         struct bnx2x *bp = netdev_priv(dev);
10815         int rc = 0;
10816
10817         switch (ctl->cmd) {
10818         case DRV_CTL_CTXTBL_WR_CMD: {
10819                 u32 index = ctl->data.io.offset;
10820                 dma_addr_t addr = ctl->data.io.dma_addr;
10821
10822                 bnx2x_ilt_wr(bp, index, addr);
10823                 break;
10824         }
10825
10826         case DRV_CTL_RET_L5_SPQ_CREDIT_CMD: {
10827                 int count = ctl->data.credit.credit_count;
10828
10829                 bnx2x_cnic_sp_post(bp, count);
10830                 break;
10831         }
10832
10833         /* rtnl_lock is held.  */
10834         case DRV_CTL_START_L2_CMD: {
10835                 struct cnic_eth_dev *cp = &bp->cnic_eth_dev;
10836                 unsigned long sp_bits = 0;
10837
10838                 /* Configure the iSCSI classification object */
10839                 bnx2x_init_mac_obj(bp, &bp->iscsi_l2_mac_obj,
10840                                    cp->iscsi_l2_client_id,
10841                                    cp->iscsi_l2_cid, BP_FUNC(bp),
10842                                    bnx2x_sp(bp, mac_rdata),
10843                                    bnx2x_sp_mapping(bp, mac_rdata),
10844                                    BNX2X_FILTER_MAC_PENDING,
10845                                    &bp->sp_state, BNX2X_OBJ_TYPE_RX,
10846                                    &bp->macs_pool);
10847
10848                 /* Set iSCSI MAC address */
10849                 rc = bnx2x_set_iscsi_eth_mac_addr(bp);
10850                 if (rc)
10851                         break;
10852
10853                 mmiowb();
10854                 barrier();
10855
10856                 /* Start accepting on iSCSI L2 ring */
10857
10858                 netif_addr_lock_bh(dev);
10859                 bnx2x_set_iscsi_eth_rx_mode(bp, true);
10860                 netif_addr_unlock_bh(dev);
10861
10862                 /* bits to wait on */
10863                 __set_bit(BNX2X_FILTER_RX_MODE_PENDING, &sp_bits);
10864                 __set_bit(BNX2X_FILTER_ISCSI_ETH_START_SCHED, &sp_bits);
10865
10866                 if (!bnx2x_wait_sp_comp(bp, sp_bits))
10867                         BNX2X_ERR("rx_mode completion timed out!\n");
10868
10869                 break;
10870         }
10871
10872         /* rtnl_lock is held.  */
10873         case DRV_CTL_STOP_L2_CMD: {
10874                 unsigned long sp_bits = 0;
10875
10876                 /* Stop accepting on iSCSI L2 ring */
10877                 netif_addr_lock_bh(dev);
10878                 bnx2x_set_iscsi_eth_rx_mode(bp, false);
10879                 netif_addr_unlock_bh(dev);
10880
10881                 /* bits to wait on */
10882                 __set_bit(BNX2X_FILTER_RX_MODE_PENDING, &sp_bits);
10883                 __set_bit(BNX2X_FILTER_ISCSI_ETH_STOP_SCHED, &sp_bits);
10884
10885                 if (!bnx2x_wait_sp_comp(bp, sp_bits))
10886                         BNX2X_ERR("rx_mode completion timed out!\n");
10887
10888                 mmiowb();
10889                 barrier();
10890
10891                 /* Unset iSCSI L2 MAC */
10892                 rc = bnx2x_del_all_macs(bp, &bp->iscsi_l2_mac_obj,
10893                                         BNX2X_ISCSI_ETH_MAC, true);
10894                 break;
10895         }
10896         case DRV_CTL_RET_L2_SPQ_CREDIT_CMD: {
10897                 int count = ctl->data.credit.credit_count;
10898
10899                 smp_mb__before_atomic_inc();
10900                 atomic_add(count, &bp->cq_spq_left);
10901                 smp_mb__after_atomic_inc();
10902                 break;
10903         }
10904
10905         default:
10906                 BNX2X_ERR("unknown command %x\n", ctl->cmd);
10907                 rc = -EINVAL;
10908         }
10909
10910         return rc;
10911 }
10912
10913 void bnx2x_setup_cnic_irq_info(struct bnx2x *bp)
10914 {
10915         struct cnic_eth_dev *cp = &bp->cnic_eth_dev;
10916
10917         if (bp->flags & USING_MSIX_FLAG) {
10918                 cp->drv_state |= CNIC_DRV_STATE_USING_MSIX;
10919                 cp->irq_arr[0].irq_flags |= CNIC_IRQ_FL_MSIX;
10920                 cp->irq_arr[0].vector = bp->msix_table[1].vector;
10921         } else {
10922                 cp->drv_state &= ~CNIC_DRV_STATE_USING_MSIX;
10923                 cp->irq_arr[0].irq_flags &= ~CNIC_IRQ_FL_MSIX;
10924         }
10925         if (!CHIP_IS_E1x(bp))
10926                 cp->irq_arr[0].status_blk = (void *)bp->cnic_sb.e2_sb;
10927         else
10928                 cp->irq_arr[0].status_blk = (void *)bp->cnic_sb.e1x_sb;
10929
10930         cp->irq_arr[0].status_blk_num =  bnx2x_cnic_fw_sb_id(bp);
10931         cp->irq_arr[0].status_blk_num2 = bnx2x_cnic_igu_sb_id(bp);
10932         cp->irq_arr[1].status_blk = bp->def_status_blk;
10933         cp->irq_arr[1].status_blk_num = DEF_SB_ID;
10934         cp->irq_arr[1].status_blk_num2 = DEF_SB_IGU_ID;
10935
10936         cp->num_irq = 2;
10937 }
10938
10939 static int bnx2x_register_cnic(struct net_device *dev, struct cnic_ops *ops,
10940                                void *data)
10941 {
10942         struct bnx2x *bp = netdev_priv(dev);
10943         struct cnic_eth_dev *cp = &bp->cnic_eth_dev;
10944
10945         if (ops == NULL)
10946                 return -EINVAL;
10947
10948         bp->cnic_kwq = kzalloc(PAGE_SIZE, GFP_KERNEL);
10949         if (!bp->cnic_kwq)
10950                 return -ENOMEM;
10951
10952         bp->cnic_kwq_cons = bp->cnic_kwq;
10953         bp->cnic_kwq_prod = bp->cnic_kwq;
10954         bp->cnic_kwq_last = bp->cnic_kwq + MAX_SP_DESC_CNT;
10955
10956         bp->cnic_spq_pending = 0;
10957         bp->cnic_kwq_pending = 0;
10958
10959         bp->cnic_data = data;
10960
10961         cp->num_irq = 0;
10962         cp->drv_state |= CNIC_DRV_STATE_REGD;
10963         cp->iro_arr = bp->iro_arr;
10964
10965         bnx2x_setup_cnic_irq_info(bp);
10966
10967         rcu_assign_pointer(bp->cnic_ops, ops);
10968
10969         return 0;
10970 }
10971
10972 static int bnx2x_unregister_cnic(struct net_device *dev)
10973 {
10974         struct bnx2x *bp = netdev_priv(dev);
10975         struct cnic_eth_dev *cp = &bp->cnic_eth_dev;
10976
10977         mutex_lock(&bp->cnic_mutex);
10978         cp->drv_state = 0;
10979         rcu_assign_pointer(bp->cnic_ops, NULL);
10980         mutex_unlock(&bp->cnic_mutex);
10981         synchronize_rcu();
10982         kfree(bp->cnic_kwq);
10983         bp->cnic_kwq = NULL;
10984
10985         return 0;
10986 }
10987
10988 struct cnic_eth_dev *bnx2x_cnic_probe(struct net_device *dev)
10989 {
10990         struct bnx2x *bp = netdev_priv(dev);
10991         struct cnic_eth_dev *cp = &bp->cnic_eth_dev;
10992
10993         /* If both iSCSI and FCoE are disabled - return NULL in
10994          * order to indicate CNIC that it should not try to work
10995          * with this device.
10996          */
10997         if (NO_ISCSI(bp) && NO_FCOE(bp))
10998                 return NULL;
10999
11000         cp->drv_owner = THIS_MODULE;
11001         cp->chip_id = CHIP_ID(bp);
11002         cp->pdev = bp->pdev;
11003         cp->io_base = bp->regview;
11004         cp->io_base2 = bp->doorbells;
11005         cp->max_kwqe_pending = 8;
11006         cp->ctx_blk_size = CDU_ILT_PAGE_SZ;
11007         cp->ctx_tbl_offset = FUNC_ILT_BASE(BP_FUNC(bp)) +
11008                              bnx2x_cid_ilt_lines(bp);
11009         cp->ctx_tbl_len = CNIC_ILT_LINES;
11010         cp->starting_cid = bnx2x_cid_ilt_lines(bp) * ILT_PAGE_CIDS;
11011         cp->drv_submit_kwqes_16 = bnx2x_cnic_sp_queue;
11012         cp->drv_ctl = bnx2x_drv_ctl;
11013         cp->drv_register_cnic = bnx2x_register_cnic;
11014         cp->drv_unregister_cnic = bnx2x_unregister_cnic;
11015         cp->fcoe_init_cid = BNX2X_FCOE_ETH_CID;
11016         cp->iscsi_l2_client_id =
11017                 bnx2x_cnic_eth_cl_id(bp, BNX2X_ISCSI_ETH_CL_ID_IDX);
11018         cp->iscsi_l2_cid = BNX2X_ISCSI_ETH_CID;
11019
11020         if (NO_ISCSI_OOO(bp))
11021                 cp->drv_state |= CNIC_DRV_STATE_NO_ISCSI_OOO;
11022
11023         if (NO_ISCSI(bp))
11024                 cp->drv_state |= CNIC_DRV_STATE_NO_ISCSI;
11025
11026         if (NO_FCOE(bp))
11027                 cp->drv_state |= CNIC_DRV_STATE_NO_FCOE;
11028
11029         DP(BNX2X_MSG_SP, "page_size %d, tbl_offset %d, tbl_lines %d, "
11030                          "starting cid %d\n",
11031            cp->ctx_blk_size,
11032            cp->ctx_tbl_offset,
11033            cp->ctx_tbl_len,
11034            cp->starting_cid);
11035         return cp;
11036 }
11037 EXPORT_SYMBOL(bnx2x_cnic_probe);
11038
11039 #endif /* BCM_CNIC */
11040