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