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