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