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