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