32b6b1033a3b4f367f4a479a8fc5b8d3c58032f9
[linux-3.10.git] / drivers / net / bnx2x / bnx2x_stats.c
1 /* bnx2x_stats.c: Broadcom Everest network driver.
2  *
3  * Copyright (c) 2007-2010 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  #include "bnx2x_cmn.h"
18  #include "bnx2x_stats.h"
19
20 /* Statistics */
21
22 /****************************************************************************
23 * Macros
24 ****************************************************************************/
25
26 /* sum[hi:lo] += add[hi:lo] */
27 #define ADD_64(s_hi, a_hi, s_lo, a_lo) \
28         do { \
29                 s_lo += a_lo; \
30                 s_hi += a_hi + ((s_lo < a_lo) ? 1 : 0); \
31         } while (0)
32
33 /* difference = minuend - subtrahend */
34 #define DIFF_64(d_hi, m_hi, s_hi, d_lo, m_lo, s_lo) \
35         do { \
36                 if (m_lo < s_lo) { \
37                         /* underflow */ \
38                         d_hi = m_hi - s_hi; \
39                         if (d_hi > 0) { \
40                                 /* we can 'loan' 1 */ \
41                                 d_hi--; \
42                                 d_lo = m_lo + (UINT_MAX - s_lo) + 1; \
43                         } else { \
44                                 /* m_hi <= s_hi */ \
45                                 d_hi = 0; \
46                                 d_lo = 0; \
47                         } \
48                 } else { \
49                         /* m_lo >= s_lo */ \
50                         if (m_hi < s_hi) { \
51                                 d_hi = 0; \
52                                 d_lo = 0; \
53                         } else { \
54                                 /* m_hi >= s_hi */ \
55                                 d_hi = m_hi - s_hi; \
56                                 d_lo = m_lo - s_lo; \
57                         } \
58                 } \
59         } while (0)
60
61 #define UPDATE_STAT64(s, t) \
62         do { \
63                 DIFF_64(diff.hi, new->s##_hi, pstats->mac_stx[0].t##_hi, \
64                         diff.lo, new->s##_lo, pstats->mac_stx[0].t##_lo); \
65                 pstats->mac_stx[0].t##_hi = new->s##_hi; \
66                 pstats->mac_stx[0].t##_lo = new->s##_lo; \
67                 ADD_64(pstats->mac_stx[1].t##_hi, diff.hi, \
68                        pstats->mac_stx[1].t##_lo, diff.lo); \
69         } while (0)
70
71 #define UPDATE_STAT64_NIG(s, t) \
72         do { \
73                 DIFF_64(diff.hi, new->s##_hi, old->s##_hi, \
74                         diff.lo, new->s##_lo, old->s##_lo); \
75                 ADD_64(estats->t##_hi, diff.hi, \
76                        estats->t##_lo, diff.lo); \
77         } while (0)
78
79 /* sum[hi:lo] += add */
80 #define ADD_EXTEND_64(s_hi, s_lo, a) \
81         do { \
82                 s_lo += a; \
83                 s_hi += (s_lo < a) ? 1 : 0; \
84         } while (0)
85
86 #define UPDATE_EXTEND_STAT(s) \
87         do { \
88                 ADD_EXTEND_64(pstats->mac_stx[1].s##_hi, \
89                               pstats->mac_stx[1].s##_lo, \
90                               new->s); \
91         } while (0)
92
93 #define UPDATE_EXTEND_TSTAT(s, t) \
94         do { \
95                 diff = le32_to_cpu(tclient->s) - le32_to_cpu(old_tclient->s); \
96                 old_tclient->s = tclient->s; \
97                 ADD_EXTEND_64(qstats->t##_hi, qstats->t##_lo, diff); \
98         } while (0)
99
100 #define UPDATE_EXTEND_USTAT(s, t) \
101         do { \
102                 diff = le32_to_cpu(uclient->s) - le32_to_cpu(old_uclient->s); \
103                 old_uclient->s = uclient->s; \
104                 ADD_EXTEND_64(qstats->t##_hi, qstats->t##_lo, diff); \
105         } while (0)
106
107 #define UPDATE_EXTEND_XSTAT(s, t) \
108         do { \
109                 diff = le32_to_cpu(xclient->s) - le32_to_cpu(old_xclient->s); \
110                 old_xclient->s = xclient->s; \
111                 ADD_EXTEND_64(qstats->t##_hi, qstats->t##_lo, diff); \
112         } while (0)
113
114 /* minuend -= subtrahend */
115 #define SUB_64(m_hi, s_hi, m_lo, s_lo) \
116         do { \
117                 DIFF_64(m_hi, m_hi, s_hi, m_lo, m_lo, s_lo); \
118         } while (0)
119
120 /* minuend[hi:lo] -= subtrahend */
121 #define SUB_EXTEND_64(m_hi, m_lo, s) \
122         do { \
123                 SUB_64(m_hi, 0, m_lo, s); \
124         } while (0)
125
126 #define SUB_EXTEND_USTAT(s, t) \
127         do { \
128                 diff = le32_to_cpu(uclient->s) - le32_to_cpu(old_uclient->s); \
129                 SUB_EXTEND_64(qstats->t##_hi, qstats->t##_lo, diff); \
130         } while (0)
131
132 /*
133  * General service functions
134  */
135
136 static inline long bnx2x_hilo(u32 *hiref)
137 {
138         u32 lo = *(hiref + 1);
139 #if (BITS_PER_LONG == 64)
140         u32 hi = *hiref;
141
142         return HILO_U64(hi, lo);
143 #else
144         return lo;
145 #endif
146 }
147
148 /*
149  * Init service functions
150  */
151
152
153 static void bnx2x_storm_stats_post(struct bnx2x *bp)
154 {
155         if (!bp->stats_pending) {
156                 struct common_query_ramrod_data ramrod_data = {0};
157                 int i, rc;
158
159                 spin_lock_bh(&bp->stats_lock);
160
161                 ramrod_data.drv_counter = bp->stats_counter++;
162                 ramrod_data.collect_port = bp->port.pmf ? 1 : 0;
163                 for_each_queue(bp, i)
164                         ramrod_data.ctr_id_vector |= (1 << bp->fp[i].cl_id);
165
166                 rc = bnx2x_sp_post(bp, RAMROD_CMD_ID_COMMON_STAT_QUERY, 0,
167                                    ((u32 *)&ramrod_data)[1],
168                                    ((u32 *)&ramrod_data)[0], 1);
169                 if (rc == 0)
170                         bp->stats_pending = 1;
171
172                 spin_unlock_bh(&bp->stats_lock);
173         }
174 }
175
176 static void bnx2x_hw_stats_post(struct bnx2x *bp)
177 {
178         struct dmae_command *dmae = &bp->stats_dmae;
179         u32 *stats_comp = bnx2x_sp(bp, stats_comp);
180
181         *stats_comp = DMAE_COMP_VAL;
182         if (CHIP_REV_IS_SLOW(bp))
183                 return;
184
185         /* loader */
186         if (bp->executer_idx) {
187                 int loader_idx = PMF_DMAE_C(bp);
188
189                 memset(dmae, 0, sizeof(struct dmae_command));
190
191                 dmae->opcode = (DMAE_CMD_SRC_PCI | DMAE_CMD_DST_GRC |
192                                 DMAE_CMD_C_DST_GRC | DMAE_CMD_C_ENABLE |
193                                 DMAE_CMD_DST_RESET |
194 #ifdef __BIG_ENDIAN
195                                 DMAE_CMD_ENDIANITY_B_DW_SWAP |
196 #else
197                                 DMAE_CMD_ENDIANITY_DW_SWAP |
198 #endif
199                                 (BP_PORT(bp) ? DMAE_CMD_PORT_1 :
200                                                DMAE_CMD_PORT_0) |
201                                 (BP_E1HVN(bp) << DMAE_CMD_E1HVN_SHIFT));
202                 dmae->src_addr_lo = U64_LO(bnx2x_sp_mapping(bp, dmae[0]));
203                 dmae->src_addr_hi = U64_HI(bnx2x_sp_mapping(bp, dmae[0]));
204                 dmae->dst_addr_lo = (DMAE_REG_CMD_MEM +
205                                      sizeof(struct dmae_command) *
206                                      (loader_idx + 1)) >> 2;
207                 dmae->dst_addr_hi = 0;
208                 dmae->len = sizeof(struct dmae_command) >> 2;
209                 if (CHIP_IS_E1(bp))
210                         dmae->len--;
211                 dmae->comp_addr_lo = dmae_reg_go_c[loader_idx + 1] >> 2;
212                 dmae->comp_addr_hi = 0;
213                 dmae->comp_val = 1;
214
215                 *stats_comp = 0;
216                 bnx2x_post_dmae(bp, dmae, loader_idx);
217
218         } else if (bp->func_stx) {
219                 *stats_comp = 0;
220                 bnx2x_post_dmae(bp, dmae, INIT_DMAE_C(bp));
221         }
222 }
223
224 static int bnx2x_stats_comp(struct bnx2x *bp)
225 {
226         u32 *stats_comp = bnx2x_sp(bp, stats_comp);
227         int cnt = 10;
228
229         might_sleep();
230         while (*stats_comp != DMAE_COMP_VAL) {
231                 if (!cnt) {
232                         BNX2X_ERR("timeout waiting for stats finished\n");
233                         break;
234                 }
235                 cnt--;
236                 msleep(1);
237         }
238         return 1;
239 }
240
241 /*
242  * Statistics service functions
243  */
244
245 static void bnx2x_stats_pmf_update(struct bnx2x *bp)
246 {
247         struct dmae_command *dmae;
248         u32 opcode;
249         int loader_idx = PMF_DMAE_C(bp);
250         u32 *stats_comp = bnx2x_sp(bp, stats_comp);
251
252         /* sanity */
253         if (!IS_MF(bp) || !bp->port.pmf || !bp->port.port_stx) {
254                 BNX2X_ERR("BUG!\n");
255                 return;
256         }
257
258         bp->executer_idx = 0;
259
260         opcode = (DMAE_CMD_SRC_GRC | DMAE_CMD_DST_PCI |
261                   DMAE_CMD_C_ENABLE |
262                   DMAE_CMD_SRC_RESET | DMAE_CMD_DST_RESET |
263 #ifdef __BIG_ENDIAN
264                   DMAE_CMD_ENDIANITY_B_DW_SWAP |
265 #else
266                   DMAE_CMD_ENDIANITY_DW_SWAP |
267 #endif
268                   (BP_PORT(bp) ? DMAE_CMD_PORT_1 : DMAE_CMD_PORT_0) |
269                   (BP_E1HVN(bp) << DMAE_CMD_E1HVN_SHIFT));
270
271         dmae = bnx2x_sp(bp, dmae[bp->executer_idx++]);
272         dmae->opcode = (opcode | DMAE_CMD_C_DST_GRC);
273         dmae->src_addr_lo = bp->port.port_stx >> 2;
274         dmae->src_addr_hi = 0;
275         dmae->dst_addr_lo = U64_LO(bnx2x_sp_mapping(bp, port_stats));
276         dmae->dst_addr_hi = U64_HI(bnx2x_sp_mapping(bp, port_stats));
277         dmae->len = DMAE_LEN32_RD_MAX;
278         dmae->comp_addr_lo = dmae_reg_go_c[loader_idx] >> 2;
279         dmae->comp_addr_hi = 0;
280         dmae->comp_val = 1;
281
282         dmae = bnx2x_sp(bp, dmae[bp->executer_idx++]);
283         dmae->opcode = (opcode | DMAE_CMD_C_DST_PCI);
284         dmae->src_addr_lo = (bp->port.port_stx >> 2) + DMAE_LEN32_RD_MAX;
285         dmae->src_addr_hi = 0;
286         dmae->dst_addr_lo = U64_LO(bnx2x_sp_mapping(bp, port_stats) +
287                                    DMAE_LEN32_RD_MAX * 4);
288         dmae->dst_addr_hi = U64_HI(bnx2x_sp_mapping(bp, port_stats) +
289                                    DMAE_LEN32_RD_MAX * 4);
290         dmae->len = (sizeof(struct host_port_stats) >> 2) - DMAE_LEN32_RD_MAX;
291         dmae->comp_addr_lo = U64_LO(bnx2x_sp_mapping(bp, stats_comp));
292         dmae->comp_addr_hi = U64_HI(bnx2x_sp_mapping(bp, stats_comp));
293         dmae->comp_val = DMAE_COMP_VAL;
294
295         *stats_comp = 0;
296         bnx2x_hw_stats_post(bp);
297         bnx2x_stats_comp(bp);
298 }
299
300 static void bnx2x_port_stats_init(struct bnx2x *bp)
301 {
302         struct dmae_command *dmae;
303         int port = BP_PORT(bp);
304         int vn = BP_E1HVN(bp);
305         u32 opcode;
306         int loader_idx = PMF_DMAE_C(bp);
307         u32 mac_addr;
308         u32 *stats_comp = bnx2x_sp(bp, stats_comp);
309
310         /* sanity */
311         if (!bp->link_vars.link_up || !bp->port.pmf) {
312                 BNX2X_ERR("BUG!\n");
313                 return;
314         }
315
316         bp->executer_idx = 0;
317
318         /* MCP */
319         opcode = (DMAE_CMD_SRC_PCI | DMAE_CMD_DST_GRC |
320                   DMAE_CMD_C_DST_GRC | DMAE_CMD_C_ENABLE |
321                   DMAE_CMD_SRC_RESET | DMAE_CMD_DST_RESET |
322 #ifdef __BIG_ENDIAN
323                   DMAE_CMD_ENDIANITY_B_DW_SWAP |
324 #else
325                   DMAE_CMD_ENDIANITY_DW_SWAP |
326 #endif
327                   (port ? DMAE_CMD_PORT_1 : DMAE_CMD_PORT_0) |
328                   (vn << DMAE_CMD_E1HVN_SHIFT));
329
330         if (bp->port.port_stx) {
331
332                 dmae = bnx2x_sp(bp, dmae[bp->executer_idx++]);
333                 dmae->opcode = opcode;
334                 dmae->src_addr_lo = U64_LO(bnx2x_sp_mapping(bp, port_stats));
335                 dmae->src_addr_hi = U64_HI(bnx2x_sp_mapping(bp, port_stats));
336                 dmae->dst_addr_lo = bp->port.port_stx >> 2;
337                 dmae->dst_addr_hi = 0;
338                 dmae->len = sizeof(struct host_port_stats) >> 2;
339                 dmae->comp_addr_lo = dmae_reg_go_c[loader_idx] >> 2;
340                 dmae->comp_addr_hi = 0;
341                 dmae->comp_val = 1;
342         }
343
344         if (bp->func_stx) {
345
346                 dmae = bnx2x_sp(bp, dmae[bp->executer_idx++]);
347                 dmae->opcode = opcode;
348                 dmae->src_addr_lo = U64_LO(bnx2x_sp_mapping(bp, func_stats));
349                 dmae->src_addr_hi = U64_HI(bnx2x_sp_mapping(bp, func_stats));
350                 dmae->dst_addr_lo = bp->func_stx >> 2;
351                 dmae->dst_addr_hi = 0;
352                 dmae->len = sizeof(struct host_func_stats) >> 2;
353                 dmae->comp_addr_lo = dmae_reg_go_c[loader_idx] >> 2;
354                 dmae->comp_addr_hi = 0;
355                 dmae->comp_val = 1;
356         }
357
358         /* MAC */
359         opcode = (DMAE_CMD_SRC_GRC | DMAE_CMD_DST_PCI |
360                   DMAE_CMD_C_DST_GRC | DMAE_CMD_C_ENABLE |
361                   DMAE_CMD_SRC_RESET | DMAE_CMD_DST_RESET |
362 #ifdef __BIG_ENDIAN
363                   DMAE_CMD_ENDIANITY_B_DW_SWAP |
364 #else
365                   DMAE_CMD_ENDIANITY_DW_SWAP |
366 #endif
367                   (port ? DMAE_CMD_PORT_1 : DMAE_CMD_PORT_0) |
368                   (vn << DMAE_CMD_E1HVN_SHIFT));
369
370         if (bp->link_vars.mac_type == MAC_TYPE_BMAC) {
371
372                 mac_addr = (port ? NIG_REG_INGRESS_BMAC1_MEM :
373                                    NIG_REG_INGRESS_BMAC0_MEM);
374
375                 /* BIGMAC_REGISTER_TX_STAT_GTPKT ..
376                    BIGMAC_REGISTER_TX_STAT_GTBYT */
377                 dmae = bnx2x_sp(bp, dmae[bp->executer_idx++]);
378                 dmae->opcode = opcode;
379                 dmae->src_addr_lo = (mac_addr +
380                                      BIGMAC_REGISTER_TX_STAT_GTPKT) >> 2;
381                 dmae->src_addr_hi = 0;
382                 dmae->dst_addr_lo = U64_LO(bnx2x_sp_mapping(bp, mac_stats));
383                 dmae->dst_addr_hi = U64_HI(bnx2x_sp_mapping(bp, mac_stats));
384                 dmae->len = (8 + BIGMAC_REGISTER_TX_STAT_GTBYT -
385                              BIGMAC_REGISTER_TX_STAT_GTPKT) >> 2;
386                 dmae->comp_addr_lo = dmae_reg_go_c[loader_idx] >> 2;
387                 dmae->comp_addr_hi = 0;
388                 dmae->comp_val = 1;
389
390                 /* BIGMAC_REGISTER_RX_STAT_GR64 ..
391                    BIGMAC_REGISTER_RX_STAT_GRIPJ */
392                 dmae = bnx2x_sp(bp, dmae[bp->executer_idx++]);
393                 dmae->opcode = opcode;
394                 dmae->src_addr_lo = (mac_addr +
395                                      BIGMAC_REGISTER_RX_STAT_GR64) >> 2;
396                 dmae->src_addr_hi = 0;
397                 dmae->dst_addr_lo = U64_LO(bnx2x_sp_mapping(bp, mac_stats) +
398                                 offsetof(struct bmac1_stats, rx_stat_gr64_lo));
399                 dmae->dst_addr_hi = U64_HI(bnx2x_sp_mapping(bp, mac_stats) +
400                                 offsetof(struct bmac1_stats, rx_stat_gr64_lo));
401                 dmae->len = (8 + BIGMAC_REGISTER_RX_STAT_GRIPJ -
402                              BIGMAC_REGISTER_RX_STAT_GR64) >> 2;
403                 dmae->comp_addr_lo = dmae_reg_go_c[loader_idx] >> 2;
404                 dmae->comp_addr_hi = 0;
405                 dmae->comp_val = 1;
406
407         } else if (bp->link_vars.mac_type == MAC_TYPE_EMAC) {
408
409                 mac_addr = (port ? GRCBASE_EMAC1 : GRCBASE_EMAC0);
410
411                 /* EMAC_REG_EMAC_RX_STAT_AC (EMAC_REG_EMAC_RX_STAT_AC_COUNT)*/
412                 dmae = bnx2x_sp(bp, dmae[bp->executer_idx++]);
413                 dmae->opcode = opcode;
414                 dmae->src_addr_lo = (mac_addr +
415                                      EMAC_REG_EMAC_RX_STAT_AC) >> 2;
416                 dmae->src_addr_hi = 0;
417                 dmae->dst_addr_lo = U64_LO(bnx2x_sp_mapping(bp, mac_stats));
418                 dmae->dst_addr_hi = U64_HI(bnx2x_sp_mapping(bp, mac_stats));
419                 dmae->len = EMAC_REG_EMAC_RX_STAT_AC_COUNT;
420                 dmae->comp_addr_lo = dmae_reg_go_c[loader_idx] >> 2;
421                 dmae->comp_addr_hi = 0;
422                 dmae->comp_val = 1;
423
424                 /* EMAC_REG_EMAC_RX_STAT_AC_28 */
425                 dmae = bnx2x_sp(bp, dmae[bp->executer_idx++]);
426                 dmae->opcode = opcode;
427                 dmae->src_addr_lo = (mac_addr +
428                                      EMAC_REG_EMAC_RX_STAT_AC_28) >> 2;
429                 dmae->src_addr_hi = 0;
430                 dmae->dst_addr_lo = U64_LO(bnx2x_sp_mapping(bp, mac_stats) +
431                      offsetof(struct emac_stats, rx_stat_falsecarriererrors));
432                 dmae->dst_addr_hi = U64_HI(bnx2x_sp_mapping(bp, mac_stats) +
433                      offsetof(struct emac_stats, rx_stat_falsecarriererrors));
434                 dmae->len = 1;
435                 dmae->comp_addr_lo = dmae_reg_go_c[loader_idx] >> 2;
436                 dmae->comp_addr_hi = 0;
437                 dmae->comp_val = 1;
438
439                 /* EMAC_REG_EMAC_TX_STAT_AC (EMAC_REG_EMAC_TX_STAT_AC_COUNT)*/
440                 dmae = bnx2x_sp(bp, dmae[bp->executer_idx++]);
441                 dmae->opcode = opcode;
442                 dmae->src_addr_lo = (mac_addr +
443                                      EMAC_REG_EMAC_TX_STAT_AC) >> 2;
444                 dmae->src_addr_hi = 0;
445                 dmae->dst_addr_lo = U64_LO(bnx2x_sp_mapping(bp, mac_stats) +
446                         offsetof(struct emac_stats, tx_stat_ifhcoutoctets));
447                 dmae->dst_addr_hi = U64_HI(bnx2x_sp_mapping(bp, mac_stats) +
448                         offsetof(struct emac_stats, tx_stat_ifhcoutoctets));
449                 dmae->len = EMAC_REG_EMAC_TX_STAT_AC_COUNT;
450                 dmae->comp_addr_lo = dmae_reg_go_c[loader_idx] >> 2;
451                 dmae->comp_addr_hi = 0;
452                 dmae->comp_val = 1;
453         }
454
455         /* NIG */
456         dmae = bnx2x_sp(bp, dmae[bp->executer_idx++]);
457         dmae->opcode = opcode;
458         dmae->src_addr_lo = (port ? NIG_REG_STAT1_BRB_DISCARD :
459                                     NIG_REG_STAT0_BRB_DISCARD) >> 2;
460         dmae->src_addr_hi = 0;
461         dmae->dst_addr_lo = U64_LO(bnx2x_sp_mapping(bp, nig_stats));
462         dmae->dst_addr_hi = U64_HI(bnx2x_sp_mapping(bp, nig_stats));
463         dmae->len = (sizeof(struct nig_stats) - 4*sizeof(u32)) >> 2;
464         dmae->comp_addr_lo = dmae_reg_go_c[loader_idx] >> 2;
465         dmae->comp_addr_hi = 0;
466         dmae->comp_val = 1;
467
468         dmae = bnx2x_sp(bp, dmae[bp->executer_idx++]);
469         dmae->opcode = opcode;
470         dmae->src_addr_lo = (port ? NIG_REG_STAT1_EGRESS_MAC_PKT0 :
471                                     NIG_REG_STAT0_EGRESS_MAC_PKT0) >> 2;
472         dmae->src_addr_hi = 0;
473         dmae->dst_addr_lo = U64_LO(bnx2x_sp_mapping(bp, nig_stats) +
474                         offsetof(struct nig_stats, egress_mac_pkt0_lo));
475         dmae->dst_addr_hi = U64_HI(bnx2x_sp_mapping(bp, nig_stats) +
476                         offsetof(struct nig_stats, egress_mac_pkt0_lo));
477         dmae->len = (2*sizeof(u32)) >> 2;
478         dmae->comp_addr_lo = dmae_reg_go_c[loader_idx] >> 2;
479         dmae->comp_addr_hi = 0;
480         dmae->comp_val = 1;
481
482         dmae = bnx2x_sp(bp, dmae[bp->executer_idx++]);
483         dmae->opcode = (DMAE_CMD_SRC_GRC | DMAE_CMD_DST_PCI |
484                         DMAE_CMD_C_DST_PCI | DMAE_CMD_C_ENABLE |
485                         DMAE_CMD_SRC_RESET | DMAE_CMD_DST_RESET |
486 #ifdef __BIG_ENDIAN
487                         DMAE_CMD_ENDIANITY_B_DW_SWAP |
488 #else
489                         DMAE_CMD_ENDIANITY_DW_SWAP |
490 #endif
491                         (port ? DMAE_CMD_PORT_1 : DMAE_CMD_PORT_0) |
492                         (vn << DMAE_CMD_E1HVN_SHIFT));
493         dmae->src_addr_lo = (port ? NIG_REG_STAT1_EGRESS_MAC_PKT1 :
494                                     NIG_REG_STAT0_EGRESS_MAC_PKT1) >> 2;
495         dmae->src_addr_hi = 0;
496         dmae->dst_addr_lo = U64_LO(bnx2x_sp_mapping(bp, nig_stats) +
497                         offsetof(struct nig_stats, egress_mac_pkt1_lo));
498         dmae->dst_addr_hi = U64_HI(bnx2x_sp_mapping(bp, nig_stats) +
499                         offsetof(struct nig_stats, egress_mac_pkt1_lo));
500         dmae->len = (2*sizeof(u32)) >> 2;
501         dmae->comp_addr_lo = U64_LO(bnx2x_sp_mapping(bp, stats_comp));
502         dmae->comp_addr_hi = U64_HI(bnx2x_sp_mapping(bp, stats_comp));
503         dmae->comp_val = DMAE_COMP_VAL;
504
505         *stats_comp = 0;
506 }
507
508 static void bnx2x_func_stats_init(struct bnx2x *bp)
509 {
510         struct dmae_command *dmae = &bp->stats_dmae;
511         u32 *stats_comp = bnx2x_sp(bp, stats_comp);
512
513         /* sanity */
514         if (!bp->func_stx) {
515                 BNX2X_ERR("BUG!\n");
516                 return;
517         }
518
519         bp->executer_idx = 0;
520         memset(dmae, 0, sizeof(struct dmae_command));
521
522         dmae->opcode = (DMAE_CMD_SRC_PCI | DMAE_CMD_DST_GRC |
523                         DMAE_CMD_C_DST_PCI | DMAE_CMD_C_ENABLE |
524                         DMAE_CMD_SRC_RESET | DMAE_CMD_DST_RESET |
525 #ifdef __BIG_ENDIAN
526                         DMAE_CMD_ENDIANITY_B_DW_SWAP |
527 #else
528                         DMAE_CMD_ENDIANITY_DW_SWAP |
529 #endif
530                         (BP_PORT(bp) ? DMAE_CMD_PORT_1 : DMAE_CMD_PORT_0) |
531                         (BP_E1HVN(bp) << DMAE_CMD_E1HVN_SHIFT));
532         dmae->src_addr_lo = U64_LO(bnx2x_sp_mapping(bp, func_stats));
533         dmae->src_addr_hi = U64_HI(bnx2x_sp_mapping(bp, func_stats));
534         dmae->dst_addr_lo = bp->func_stx >> 2;
535         dmae->dst_addr_hi = 0;
536         dmae->len = sizeof(struct host_func_stats) >> 2;
537         dmae->comp_addr_lo = U64_LO(bnx2x_sp_mapping(bp, stats_comp));
538         dmae->comp_addr_hi = U64_HI(bnx2x_sp_mapping(bp, stats_comp));
539         dmae->comp_val = DMAE_COMP_VAL;
540
541         *stats_comp = 0;
542 }
543
544 static void bnx2x_stats_start(struct bnx2x *bp)
545 {
546         if (bp->port.pmf)
547                 bnx2x_port_stats_init(bp);
548
549         else if (bp->func_stx)
550                 bnx2x_func_stats_init(bp);
551
552         bnx2x_hw_stats_post(bp);
553         bnx2x_storm_stats_post(bp);
554 }
555
556 static void bnx2x_stats_pmf_start(struct bnx2x *bp)
557 {
558         bnx2x_stats_comp(bp);
559         bnx2x_stats_pmf_update(bp);
560         bnx2x_stats_start(bp);
561 }
562
563 static void bnx2x_stats_restart(struct bnx2x *bp)
564 {
565         bnx2x_stats_comp(bp);
566         bnx2x_stats_start(bp);
567 }
568
569 static void bnx2x_bmac_stats_update(struct bnx2x *bp)
570 {
571         struct bmac1_stats *new = bnx2x_sp(bp, mac_stats.bmac1_stats);
572         struct host_port_stats *pstats = bnx2x_sp(bp, port_stats);
573         struct bnx2x_eth_stats *estats = &bp->eth_stats;
574         struct {
575                 u32 lo;
576                 u32 hi;
577         } diff;
578
579         UPDATE_STAT64(rx_stat_grerb, rx_stat_ifhcinbadoctets);
580         UPDATE_STAT64(rx_stat_grfcs, rx_stat_dot3statsfcserrors);
581         UPDATE_STAT64(rx_stat_grund, rx_stat_etherstatsundersizepkts);
582         UPDATE_STAT64(rx_stat_grovr, rx_stat_dot3statsframestoolong);
583         UPDATE_STAT64(rx_stat_grfrg, rx_stat_etherstatsfragments);
584         UPDATE_STAT64(rx_stat_grjbr, rx_stat_etherstatsjabbers);
585         UPDATE_STAT64(rx_stat_grxcf, rx_stat_maccontrolframesreceived);
586         UPDATE_STAT64(rx_stat_grxpf, rx_stat_xoffstateentered);
587         UPDATE_STAT64(rx_stat_grxpf, rx_stat_bmac_xpf);
588         UPDATE_STAT64(tx_stat_gtxpf, tx_stat_outxoffsent);
589         UPDATE_STAT64(tx_stat_gtxpf, tx_stat_flowcontroldone);
590         UPDATE_STAT64(tx_stat_gt64, tx_stat_etherstatspkts64octets);
591         UPDATE_STAT64(tx_stat_gt127,
592                                 tx_stat_etherstatspkts65octetsto127octets);
593         UPDATE_STAT64(tx_stat_gt255,
594                                 tx_stat_etherstatspkts128octetsto255octets);
595         UPDATE_STAT64(tx_stat_gt511,
596                                 tx_stat_etherstatspkts256octetsto511octets);
597         UPDATE_STAT64(tx_stat_gt1023,
598                                 tx_stat_etherstatspkts512octetsto1023octets);
599         UPDATE_STAT64(tx_stat_gt1518,
600                                 tx_stat_etherstatspkts1024octetsto1522octets);
601         UPDATE_STAT64(tx_stat_gt2047, tx_stat_bmac_2047);
602         UPDATE_STAT64(tx_stat_gt4095, tx_stat_bmac_4095);
603         UPDATE_STAT64(tx_stat_gt9216, tx_stat_bmac_9216);
604         UPDATE_STAT64(tx_stat_gt16383, tx_stat_bmac_16383);
605         UPDATE_STAT64(tx_stat_gterr,
606                                 tx_stat_dot3statsinternalmactransmiterrors);
607         UPDATE_STAT64(tx_stat_gtufl, tx_stat_bmac_ufl);
608
609         estats->pause_frames_received_hi =
610                                 pstats->mac_stx[1].rx_stat_bmac_xpf_hi;
611         estats->pause_frames_received_lo =
612                                 pstats->mac_stx[1].rx_stat_bmac_xpf_lo;
613
614         estats->pause_frames_sent_hi =
615                                 pstats->mac_stx[1].tx_stat_outxoffsent_hi;
616         estats->pause_frames_sent_lo =
617                                 pstats->mac_stx[1].tx_stat_outxoffsent_lo;
618 }
619
620 static void bnx2x_emac_stats_update(struct bnx2x *bp)
621 {
622         struct emac_stats *new = bnx2x_sp(bp, mac_stats.emac_stats);
623         struct host_port_stats *pstats = bnx2x_sp(bp, port_stats);
624         struct bnx2x_eth_stats *estats = &bp->eth_stats;
625
626         UPDATE_EXTEND_STAT(rx_stat_ifhcinbadoctets);
627         UPDATE_EXTEND_STAT(tx_stat_ifhcoutbadoctets);
628         UPDATE_EXTEND_STAT(rx_stat_dot3statsfcserrors);
629         UPDATE_EXTEND_STAT(rx_stat_dot3statsalignmenterrors);
630         UPDATE_EXTEND_STAT(rx_stat_dot3statscarriersenseerrors);
631         UPDATE_EXTEND_STAT(rx_stat_falsecarriererrors);
632         UPDATE_EXTEND_STAT(rx_stat_etherstatsundersizepkts);
633         UPDATE_EXTEND_STAT(rx_stat_dot3statsframestoolong);
634         UPDATE_EXTEND_STAT(rx_stat_etherstatsfragments);
635         UPDATE_EXTEND_STAT(rx_stat_etherstatsjabbers);
636         UPDATE_EXTEND_STAT(rx_stat_maccontrolframesreceived);
637         UPDATE_EXTEND_STAT(rx_stat_xoffstateentered);
638         UPDATE_EXTEND_STAT(rx_stat_xonpauseframesreceived);
639         UPDATE_EXTEND_STAT(rx_stat_xoffpauseframesreceived);
640         UPDATE_EXTEND_STAT(tx_stat_outxonsent);
641         UPDATE_EXTEND_STAT(tx_stat_outxoffsent);
642         UPDATE_EXTEND_STAT(tx_stat_flowcontroldone);
643         UPDATE_EXTEND_STAT(tx_stat_etherstatscollisions);
644         UPDATE_EXTEND_STAT(tx_stat_dot3statssinglecollisionframes);
645         UPDATE_EXTEND_STAT(tx_stat_dot3statsmultiplecollisionframes);
646         UPDATE_EXTEND_STAT(tx_stat_dot3statsdeferredtransmissions);
647         UPDATE_EXTEND_STAT(tx_stat_dot3statsexcessivecollisions);
648         UPDATE_EXTEND_STAT(tx_stat_dot3statslatecollisions);
649         UPDATE_EXTEND_STAT(tx_stat_etherstatspkts64octets);
650         UPDATE_EXTEND_STAT(tx_stat_etherstatspkts65octetsto127octets);
651         UPDATE_EXTEND_STAT(tx_stat_etherstatspkts128octetsto255octets);
652         UPDATE_EXTEND_STAT(tx_stat_etherstatspkts256octetsto511octets);
653         UPDATE_EXTEND_STAT(tx_stat_etherstatspkts512octetsto1023octets);
654         UPDATE_EXTEND_STAT(tx_stat_etherstatspkts1024octetsto1522octets);
655         UPDATE_EXTEND_STAT(tx_stat_etherstatspktsover1522octets);
656         UPDATE_EXTEND_STAT(tx_stat_dot3statsinternalmactransmiterrors);
657
658         estats->pause_frames_received_hi =
659                         pstats->mac_stx[1].rx_stat_xonpauseframesreceived_hi;
660         estats->pause_frames_received_lo =
661                         pstats->mac_stx[1].rx_stat_xonpauseframesreceived_lo;
662         ADD_64(estats->pause_frames_received_hi,
663                pstats->mac_stx[1].rx_stat_xoffpauseframesreceived_hi,
664                estats->pause_frames_received_lo,
665                pstats->mac_stx[1].rx_stat_xoffpauseframesreceived_lo);
666
667         estats->pause_frames_sent_hi =
668                         pstats->mac_stx[1].tx_stat_outxonsent_hi;
669         estats->pause_frames_sent_lo =
670                         pstats->mac_stx[1].tx_stat_outxonsent_lo;
671         ADD_64(estats->pause_frames_sent_hi,
672                pstats->mac_stx[1].tx_stat_outxoffsent_hi,
673                estats->pause_frames_sent_lo,
674                pstats->mac_stx[1].tx_stat_outxoffsent_lo);
675 }
676
677 static int bnx2x_hw_stats_update(struct bnx2x *bp)
678 {
679         struct nig_stats *new = bnx2x_sp(bp, nig_stats);
680         struct nig_stats *old = &(bp->port.old_nig_stats);
681         struct host_port_stats *pstats = bnx2x_sp(bp, port_stats);
682         struct bnx2x_eth_stats *estats = &bp->eth_stats;
683         struct {
684                 u32 lo;
685                 u32 hi;
686         } diff;
687
688         if (bp->link_vars.mac_type == MAC_TYPE_BMAC)
689                 bnx2x_bmac_stats_update(bp);
690
691         else if (bp->link_vars.mac_type == MAC_TYPE_EMAC)
692                 bnx2x_emac_stats_update(bp);
693
694         else { /* unreached */
695                 BNX2X_ERR("stats updated by DMAE but no MAC active\n");
696                 return -1;
697         }
698
699         ADD_EXTEND_64(pstats->brb_drop_hi, pstats->brb_drop_lo,
700                       new->brb_discard - old->brb_discard);
701         ADD_EXTEND_64(estats->brb_truncate_hi, estats->brb_truncate_lo,
702                       new->brb_truncate - old->brb_truncate);
703
704         UPDATE_STAT64_NIG(egress_mac_pkt0,
705                                         etherstatspkts1024octetsto1522octets);
706         UPDATE_STAT64_NIG(egress_mac_pkt1, etherstatspktsover1522octets);
707
708         memcpy(old, new, sizeof(struct nig_stats));
709
710         memcpy(&(estats->rx_stat_ifhcinbadoctets_hi), &(pstats->mac_stx[1]),
711                sizeof(struct mac_stx));
712         estats->brb_drop_hi = pstats->brb_drop_hi;
713         estats->brb_drop_lo = pstats->brb_drop_lo;
714
715         pstats->host_port_stats_start = ++pstats->host_port_stats_end;
716
717         if (!BP_NOMCP(bp)) {
718                 u32 nig_timer_max =
719                         SHMEM_RD(bp, port_mb[BP_PORT(bp)].stat_nig_timer);
720                 if (nig_timer_max != estats->nig_timer_max) {
721                         estats->nig_timer_max = nig_timer_max;
722                         BNX2X_ERR("NIG timer max (%u)\n",
723                                   estats->nig_timer_max);
724                 }
725         }
726
727         return 0;
728 }
729
730 static int bnx2x_storm_stats_update(struct bnx2x *bp)
731 {
732         struct eth_stats_query *stats = bnx2x_sp(bp, fw_stats);
733         struct tstorm_per_port_stats *tport =
734                                         &stats->tstorm_common.port_statistics;
735         struct host_func_stats *fstats = bnx2x_sp(bp, func_stats);
736         struct bnx2x_eth_stats *estats = &bp->eth_stats;
737         int i;
738         u16 cur_stats_counter;
739
740         /* Make sure we use the value of the counter
741          * used for sending the last stats ramrod.
742          */
743         spin_lock_bh(&bp->stats_lock);
744         cur_stats_counter = bp->stats_counter - 1;
745         spin_unlock_bh(&bp->stats_lock);
746
747         memcpy(&(fstats->total_bytes_received_hi),
748                &(bnx2x_sp(bp, func_stats_base)->total_bytes_received_hi),
749                sizeof(struct host_func_stats) - 2*sizeof(u32));
750         estats->error_bytes_received_hi = 0;
751         estats->error_bytes_received_lo = 0;
752         estats->etherstatsoverrsizepkts_hi = 0;
753         estats->etherstatsoverrsizepkts_lo = 0;
754         estats->no_buff_discard_hi = 0;
755         estats->no_buff_discard_lo = 0;
756
757         for_each_queue(bp, i) {
758                 struct bnx2x_fastpath *fp = &bp->fp[i];
759                 int cl_id = fp->cl_id;
760                 struct tstorm_per_client_stats *tclient =
761                                 &stats->tstorm_common.client_statistics[cl_id];
762                 struct tstorm_per_client_stats *old_tclient = &fp->old_tclient;
763                 struct ustorm_per_client_stats *uclient =
764                                 &stats->ustorm_common.client_statistics[cl_id];
765                 struct ustorm_per_client_stats *old_uclient = &fp->old_uclient;
766                 struct xstorm_per_client_stats *xclient =
767                                 &stats->xstorm_common.client_statistics[cl_id];
768                 struct xstorm_per_client_stats *old_xclient = &fp->old_xclient;
769                 struct bnx2x_eth_q_stats *qstats = &fp->eth_q_stats;
770                 u32 diff;
771
772                 /* are storm stats valid? */
773                 if (le16_to_cpu(xclient->stats_counter) != cur_stats_counter) {
774                         DP(BNX2X_MSG_STATS, "[%d] stats not updated by xstorm"
775                            "  xstorm counter (0x%x) != stats_counter (0x%x)\n",
776                            i, xclient->stats_counter, cur_stats_counter + 1);
777                         return -1;
778                 }
779                 if (le16_to_cpu(tclient->stats_counter) != cur_stats_counter) {
780                         DP(BNX2X_MSG_STATS, "[%d] stats not updated by tstorm"
781                            "  tstorm counter (0x%x) != stats_counter (0x%x)\n",
782                            i, tclient->stats_counter, cur_stats_counter + 1);
783                         return -2;
784                 }
785                 if (le16_to_cpu(uclient->stats_counter) != cur_stats_counter) {
786                         DP(BNX2X_MSG_STATS, "[%d] stats not updated by ustorm"
787                            "  ustorm counter (0x%x) != stats_counter (0x%x)\n",
788                            i, uclient->stats_counter, cur_stats_counter + 1);
789                         return -4;
790                 }
791
792                 qstats->total_bytes_received_hi =
793                         le32_to_cpu(tclient->rcv_broadcast_bytes.hi);
794                 qstats->total_bytes_received_lo =
795                         le32_to_cpu(tclient->rcv_broadcast_bytes.lo);
796
797                 ADD_64(qstats->total_bytes_received_hi,
798                        le32_to_cpu(tclient->rcv_multicast_bytes.hi),
799                        qstats->total_bytes_received_lo,
800                        le32_to_cpu(tclient->rcv_multicast_bytes.lo));
801
802                 ADD_64(qstats->total_bytes_received_hi,
803                        le32_to_cpu(tclient->rcv_unicast_bytes.hi),
804                        qstats->total_bytes_received_lo,
805                        le32_to_cpu(tclient->rcv_unicast_bytes.lo));
806
807                 SUB_64(qstats->total_bytes_received_hi,
808                        le32_to_cpu(uclient->bcast_no_buff_bytes.hi),
809                        qstats->total_bytes_received_lo,
810                        le32_to_cpu(uclient->bcast_no_buff_bytes.lo));
811
812                 SUB_64(qstats->total_bytes_received_hi,
813                        le32_to_cpu(uclient->mcast_no_buff_bytes.hi),
814                        qstats->total_bytes_received_lo,
815                        le32_to_cpu(uclient->mcast_no_buff_bytes.lo));
816
817                 SUB_64(qstats->total_bytes_received_hi,
818                        le32_to_cpu(uclient->ucast_no_buff_bytes.hi),
819                        qstats->total_bytes_received_lo,
820                        le32_to_cpu(uclient->ucast_no_buff_bytes.lo));
821
822                 qstats->valid_bytes_received_hi =
823                                         qstats->total_bytes_received_hi;
824                 qstats->valid_bytes_received_lo =
825                                         qstats->total_bytes_received_lo;
826
827                 qstats->error_bytes_received_hi =
828                                 le32_to_cpu(tclient->rcv_error_bytes.hi);
829                 qstats->error_bytes_received_lo =
830                                 le32_to_cpu(tclient->rcv_error_bytes.lo);
831
832                 ADD_64(qstats->total_bytes_received_hi,
833                        qstats->error_bytes_received_hi,
834                        qstats->total_bytes_received_lo,
835                        qstats->error_bytes_received_lo);
836
837                 UPDATE_EXTEND_TSTAT(rcv_unicast_pkts,
838                                         total_unicast_packets_received);
839                 UPDATE_EXTEND_TSTAT(rcv_multicast_pkts,
840                                         total_multicast_packets_received);
841                 UPDATE_EXTEND_TSTAT(rcv_broadcast_pkts,
842                                         total_broadcast_packets_received);
843                 UPDATE_EXTEND_TSTAT(packets_too_big_discard,
844                                         etherstatsoverrsizepkts);
845                 UPDATE_EXTEND_TSTAT(no_buff_discard, no_buff_discard);
846
847                 SUB_EXTEND_USTAT(ucast_no_buff_pkts,
848                                         total_unicast_packets_received);
849                 SUB_EXTEND_USTAT(mcast_no_buff_pkts,
850                                         total_multicast_packets_received);
851                 SUB_EXTEND_USTAT(bcast_no_buff_pkts,
852                                         total_broadcast_packets_received);
853                 UPDATE_EXTEND_USTAT(ucast_no_buff_pkts, no_buff_discard);
854                 UPDATE_EXTEND_USTAT(mcast_no_buff_pkts, no_buff_discard);
855                 UPDATE_EXTEND_USTAT(bcast_no_buff_pkts, no_buff_discard);
856
857                 qstats->total_bytes_transmitted_hi =
858                                 le32_to_cpu(xclient->unicast_bytes_sent.hi);
859                 qstats->total_bytes_transmitted_lo =
860                                 le32_to_cpu(xclient->unicast_bytes_sent.lo);
861
862                 ADD_64(qstats->total_bytes_transmitted_hi,
863                        le32_to_cpu(xclient->multicast_bytes_sent.hi),
864                        qstats->total_bytes_transmitted_lo,
865                        le32_to_cpu(xclient->multicast_bytes_sent.lo));
866
867                 ADD_64(qstats->total_bytes_transmitted_hi,
868                        le32_to_cpu(xclient->broadcast_bytes_sent.hi),
869                        qstats->total_bytes_transmitted_lo,
870                        le32_to_cpu(xclient->broadcast_bytes_sent.lo));
871
872                 UPDATE_EXTEND_XSTAT(unicast_pkts_sent,
873                                         total_unicast_packets_transmitted);
874                 UPDATE_EXTEND_XSTAT(multicast_pkts_sent,
875                                         total_multicast_packets_transmitted);
876                 UPDATE_EXTEND_XSTAT(broadcast_pkts_sent,
877                                         total_broadcast_packets_transmitted);
878
879                 old_tclient->checksum_discard = tclient->checksum_discard;
880                 old_tclient->ttl0_discard = tclient->ttl0_discard;
881
882                 ADD_64(fstats->total_bytes_received_hi,
883                        qstats->total_bytes_received_hi,
884                        fstats->total_bytes_received_lo,
885                        qstats->total_bytes_received_lo);
886                 ADD_64(fstats->total_bytes_transmitted_hi,
887                        qstats->total_bytes_transmitted_hi,
888                        fstats->total_bytes_transmitted_lo,
889                        qstats->total_bytes_transmitted_lo);
890                 ADD_64(fstats->total_unicast_packets_received_hi,
891                        qstats->total_unicast_packets_received_hi,
892                        fstats->total_unicast_packets_received_lo,
893                        qstats->total_unicast_packets_received_lo);
894                 ADD_64(fstats->total_multicast_packets_received_hi,
895                        qstats->total_multicast_packets_received_hi,
896                        fstats->total_multicast_packets_received_lo,
897                        qstats->total_multicast_packets_received_lo);
898                 ADD_64(fstats->total_broadcast_packets_received_hi,
899                        qstats->total_broadcast_packets_received_hi,
900                        fstats->total_broadcast_packets_received_lo,
901                        qstats->total_broadcast_packets_received_lo);
902                 ADD_64(fstats->total_unicast_packets_transmitted_hi,
903                        qstats->total_unicast_packets_transmitted_hi,
904                        fstats->total_unicast_packets_transmitted_lo,
905                        qstats->total_unicast_packets_transmitted_lo);
906                 ADD_64(fstats->total_multicast_packets_transmitted_hi,
907                        qstats->total_multicast_packets_transmitted_hi,
908                        fstats->total_multicast_packets_transmitted_lo,
909                        qstats->total_multicast_packets_transmitted_lo);
910                 ADD_64(fstats->total_broadcast_packets_transmitted_hi,
911                        qstats->total_broadcast_packets_transmitted_hi,
912                        fstats->total_broadcast_packets_transmitted_lo,
913                        qstats->total_broadcast_packets_transmitted_lo);
914                 ADD_64(fstats->valid_bytes_received_hi,
915                        qstats->valid_bytes_received_hi,
916                        fstats->valid_bytes_received_lo,
917                        qstats->valid_bytes_received_lo);
918
919                 ADD_64(estats->error_bytes_received_hi,
920                        qstats->error_bytes_received_hi,
921                        estats->error_bytes_received_lo,
922                        qstats->error_bytes_received_lo);
923                 ADD_64(estats->etherstatsoverrsizepkts_hi,
924                        qstats->etherstatsoverrsizepkts_hi,
925                        estats->etherstatsoverrsizepkts_lo,
926                        qstats->etherstatsoverrsizepkts_lo);
927                 ADD_64(estats->no_buff_discard_hi, qstats->no_buff_discard_hi,
928                        estats->no_buff_discard_lo, qstats->no_buff_discard_lo);
929         }
930
931         ADD_64(fstats->total_bytes_received_hi,
932                estats->rx_stat_ifhcinbadoctets_hi,
933                fstats->total_bytes_received_lo,
934                estats->rx_stat_ifhcinbadoctets_lo);
935
936         memcpy(estats, &(fstats->total_bytes_received_hi),
937                sizeof(struct host_func_stats) - 2*sizeof(u32));
938
939         ADD_64(estats->etherstatsoverrsizepkts_hi,
940                estats->rx_stat_dot3statsframestoolong_hi,
941                estats->etherstatsoverrsizepkts_lo,
942                estats->rx_stat_dot3statsframestoolong_lo);
943         ADD_64(estats->error_bytes_received_hi,
944                estats->rx_stat_ifhcinbadoctets_hi,
945                estats->error_bytes_received_lo,
946                estats->rx_stat_ifhcinbadoctets_lo);
947
948         if (bp->port.pmf) {
949                 estats->mac_filter_discard =
950                                 le32_to_cpu(tport->mac_filter_discard);
951                 estats->xxoverflow_discard =
952                                 le32_to_cpu(tport->xxoverflow_discard);
953                 estats->brb_truncate_discard =
954                                 le32_to_cpu(tport->brb_truncate_discard);
955                 estats->mac_discard = le32_to_cpu(tport->mac_discard);
956         }
957
958         fstats->host_func_stats_start = ++fstats->host_func_stats_end;
959
960         bp->stats_pending = 0;
961
962         return 0;
963 }
964
965 static void bnx2x_net_stats_update(struct bnx2x *bp)
966 {
967         struct bnx2x_eth_stats *estats = &bp->eth_stats;
968         struct net_device_stats *nstats = &bp->dev->stats;
969         unsigned long tmp;
970         int i;
971
972         nstats->rx_packets =
973                 bnx2x_hilo(&estats->total_unicast_packets_received_hi) +
974                 bnx2x_hilo(&estats->total_multicast_packets_received_hi) +
975                 bnx2x_hilo(&estats->total_broadcast_packets_received_hi);
976
977         nstats->tx_packets =
978                 bnx2x_hilo(&estats->total_unicast_packets_transmitted_hi) +
979                 bnx2x_hilo(&estats->total_multicast_packets_transmitted_hi) +
980                 bnx2x_hilo(&estats->total_broadcast_packets_transmitted_hi);
981
982         nstats->rx_bytes = bnx2x_hilo(&estats->total_bytes_received_hi);
983
984         nstats->tx_bytes = bnx2x_hilo(&estats->total_bytes_transmitted_hi);
985
986         tmp = estats->mac_discard;
987         for_each_queue(bp, i)
988                 tmp += le32_to_cpu(bp->fp[i].old_tclient.checksum_discard);
989         nstats->rx_dropped = tmp;
990
991         nstats->tx_dropped = 0;
992
993         nstats->multicast =
994                 bnx2x_hilo(&estats->total_multicast_packets_received_hi);
995
996         nstats->collisions =
997                 bnx2x_hilo(&estats->tx_stat_etherstatscollisions_hi);
998
999         nstats->rx_length_errors =
1000                 bnx2x_hilo(&estats->rx_stat_etherstatsundersizepkts_hi) +
1001                 bnx2x_hilo(&estats->etherstatsoverrsizepkts_hi);
1002         nstats->rx_over_errors = bnx2x_hilo(&estats->brb_drop_hi) +
1003                                  bnx2x_hilo(&estats->brb_truncate_hi);
1004         nstats->rx_crc_errors =
1005                 bnx2x_hilo(&estats->rx_stat_dot3statsfcserrors_hi);
1006         nstats->rx_frame_errors =
1007                 bnx2x_hilo(&estats->rx_stat_dot3statsalignmenterrors_hi);
1008         nstats->rx_fifo_errors = bnx2x_hilo(&estats->no_buff_discard_hi);
1009         nstats->rx_missed_errors = estats->xxoverflow_discard;
1010
1011         nstats->rx_errors = nstats->rx_length_errors +
1012                             nstats->rx_over_errors +
1013                             nstats->rx_crc_errors +
1014                             nstats->rx_frame_errors +
1015                             nstats->rx_fifo_errors +
1016                             nstats->rx_missed_errors;
1017
1018         nstats->tx_aborted_errors =
1019                 bnx2x_hilo(&estats->tx_stat_dot3statslatecollisions_hi) +
1020                 bnx2x_hilo(&estats->tx_stat_dot3statsexcessivecollisions_hi);
1021         nstats->tx_carrier_errors =
1022                 bnx2x_hilo(&estats->rx_stat_dot3statscarriersenseerrors_hi);
1023         nstats->tx_fifo_errors = 0;
1024         nstats->tx_heartbeat_errors = 0;
1025         nstats->tx_window_errors = 0;
1026
1027         nstats->tx_errors = nstats->tx_aborted_errors +
1028                             nstats->tx_carrier_errors +
1029             bnx2x_hilo(&estats->tx_stat_dot3statsinternalmactransmiterrors_hi);
1030 }
1031
1032 static void bnx2x_drv_stats_update(struct bnx2x *bp)
1033 {
1034         struct bnx2x_eth_stats *estats = &bp->eth_stats;
1035         int i;
1036
1037         estats->driver_xoff = 0;
1038         estats->rx_err_discard_pkt = 0;
1039         estats->rx_skb_alloc_failed = 0;
1040         estats->hw_csum_err = 0;
1041         for_each_queue(bp, i) {
1042                 struct bnx2x_eth_q_stats *qstats = &bp->fp[i].eth_q_stats;
1043
1044                 estats->driver_xoff += qstats->driver_xoff;
1045                 estats->rx_err_discard_pkt += qstats->rx_err_discard_pkt;
1046                 estats->rx_skb_alloc_failed += qstats->rx_skb_alloc_failed;
1047                 estats->hw_csum_err += qstats->hw_csum_err;
1048         }
1049 }
1050
1051 static void bnx2x_stats_update(struct bnx2x *bp)
1052 {
1053         u32 *stats_comp = bnx2x_sp(bp, stats_comp);
1054
1055         if (*stats_comp != DMAE_COMP_VAL)
1056                 return;
1057
1058         if (bp->port.pmf)
1059                 bnx2x_hw_stats_update(bp);
1060
1061         if (bnx2x_storm_stats_update(bp) && (bp->stats_pending++ == 3)) {
1062                 BNX2X_ERR("storm stats were not updated for 3 times\n");
1063                 bnx2x_panic();
1064                 return;
1065         }
1066
1067         bnx2x_net_stats_update(bp);
1068         bnx2x_drv_stats_update(bp);
1069
1070         if (netif_msg_timer(bp)) {
1071                 struct bnx2x_eth_stats *estats = &bp->eth_stats;
1072                 int i;
1073
1074                 printk(KERN_DEBUG "%s: brb drops %u  brb truncate %u\n",
1075                        bp->dev->name,
1076                        estats->brb_drop_lo, estats->brb_truncate_lo);
1077
1078                 for_each_queue(bp, i) {
1079                         struct bnx2x_fastpath *fp = &bp->fp[i];
1080                         struct bnx2x_eth_q_stats *qstats = &fp->eth_q_stats;
1081
1082                         printk(KERN_DEBUG "%s: rx usage(%4u)  *rx_cons_sb(%u)"
1083                                           "  rx pkt(%lu)  rx calls(%lu %lu)\n",
1084                                fp->name, (le16_to_cpu(*fp->rx_cons_sb) -
1085                                fp->rx_comp_cons),
1086                                le16_to_cpu(*fp->rx_cons_sb),
1087                                bnx2x_hilo(&qstats->
1088                                           total_unicast_packets_received_hi),
1089                                fp->rx_calls, fp->rx_pkt);
1090                 }
1091
1092                 for_each_queue(bp, i) {
1093                         struct bnx2x_fastpath *fp = &bp->fp[i];
1094                         struct bnx2x_eth_q_stats *qstats = &fp->eth_q_stats;
1095                         struct netdev_queue *txq =
1096                                 netdev_get_tx_queue(bp->dev, i);
1097
1098                         printk(KERN_DEBUG "%s: tx avail(%4u)  *tx_cons_sb(%u)"
1099                                           "  tx pkt(%lu) tx calls (%lu)"
1100                                           "  %s (Xoff events %u)\n",
1101                                fp->name, bnx2x_tx_avail(fp),
1102                                le16_to_cpu(*fp->tx_cons_sb),
1103                                bnx2x_hilo(&qstats->
1104                                           total_unicast_packets_transmitted_hi),
1105                                fp->tx_pkt,
1106                                (netif_tx_queue_stopped(txq) ? "Xoff" : "Xon"),
1107                                qstats->driver_xoff);
1108                 }
1109         }
1110
1111         bnx2x_hw_stats_post(bp);
1112         bnx2x_storm_stats_post(bp);
1113 }
1114
1115 static void bnx2x_port_stats_stop(struct bnx2x *bp)
1116 {
1117         struct dmae_command *dmae;
1118         u32 opcode;
1119         int loader_idx = PMF_DMAE_C(bp);
1120         u32 *stats_comp = bnx2x_sp(bp, stats_comp);
1121
1122         bp->executer_idx = 0;
1123
1124         opcode = (DMAE_CMD_SRC_PCI | DMAE_CMD_DST_GRC |
1125                   DMAE_CMD_C_ENABLE |
1126                   DMAE_CMD_SRC_RESET | DMAE_CMD_DST_RESET |
1127 #ifdef __BIG_ENDIAN
1128                   DMAE_CMD_ENDIANITY_B_DW_SWAP |
1129 #else
1130                   DMAE_CMD_ENDIANITY_DW_SWAP |
1131 #endif
1132                   (BP_PORT(bp) ? DMAE_CMD_PORT_1 : DMAE_CMD_PORT_0) |
1133                   (BP_E1HVN(bp) << DMAE_CMD_E1HVN_SHIFT));
1134
1135         if (bp->port.port_stx) {
1136
1137                 dmae = bnx2x_sp(bp, dmae[bp->executer_idx++]);
1138                 if (bp->func_stx)
1139                         dmae->opcode = (opcode | DMAE_CMD_C_DST_GRC);
1140                 else
1141                         dmae->opcode = (opcode | DMAE_CMD_C_DST_PCI);
1142                 dmae->src_addr_lo = U64_LO(bnx2x_sp_mapping(bp, port_stats));
1143                 dmae->src_addr_hi = U64_HI(bnx2x_sp_mapping(bp, port_stats));
1144                 dmae->dst_addr_lo = bp->port.port_stx >> 2;
1145                 dmae->dst_addr_hi = 0;
1146                 dmae->len = sizeof(struct host_port_stats) >> 2;
1147                 if (bp->func_stx) {
1148                         dmae->comp_addr_lo = dmae_reg_go_c[loader_idx] >> 2;
1149                         dmae->comp_addr_hi = 0;
1150                         dmae->comp_val = 1;
1151                 } else {
1152                         dmae->comp_addr_lo =
1153                                 U64_LO(bnx2x_sp_mapping(bp, stats_comp));
1154                         dmae->comp_addr_hi =
1155                                 U64_HI(bnx2x_sp_mapping(bp, stats_comp));
1156                         dmae->comp_val = DMAE_COMP_VAL;
1157
1158                         *stats_comp = 0;
1159                 }
1160         }
1161
1162         if (bp->func_stx) {
1163
1164                 dmae = bnx2x_sp(bp, dmae[bp->executer_idx++]);
1165                 dmae->opcode = (opcode | DMAE_CMD_C_DST_PCI);
1166                 dmae->src_addr_lo = U64_LO(bnx2x_sp_mapping(bp, func_stats));
1167                 dmae->src_addr_hi = U64_HI(bnx2x_sp_mapping(bp, func_stats));
1168                 dmae->dst_addr_lo = bp->func_stx >> 2;
1169                 dmae->dst_addr_hi = 0;
1170                 dmae->len = sizeof(struct host_func_stats) >> 2;
1171                 dmae->comp_addr_lo = U64_LO(bnx2x_sp_mapping(bp, stats_comp));
1172                 dmae->comp_addr_hi = U64_HI(bnx2x_sp_mapping(bp, stats_comp));
1173                 dmae->comp_val = DMAE_COMP_VAL;
1174
1175                 *stats_comp = 0;
1176         }
1177 }
1178
1179 static void bnx2x_stats_stop(struct bnx2x *bp)
1180 {
1181         int update = 0;
1182
1183         bnx2x_stats_comp(bp);
1184
1185         if (bp->port.pmf)
1186                 update = (bnx2x_hw_stats_update(bp) == 0);
1187
1188         update |= (bnx2x_storm_stats_update(bp) == 0);
1189
1190         if (update) {
1191                 bnx2x_net_stats_update(bp);
1192
1193                 if (bp->port.pmf)
1194                         bnx2x_port_stats_stop(bp);
1195
1196                 bnx2x_hw_stats_post(bp);
1197                 bnx2x_stats_comp(bp);
1198         }
1199 }
1200
1201 static void bnx2x_stats_do_nothing(struct bnx2x *bp)
1202 {
1203 }
1204
1205 static const struct {
1206         void (*action)(struct bnx2x *bp);
1207         enum bnx2x_stats_state next_state;
1208 } bnx2x_stats_stm[STATS_STATE_MAX][STATS_EVENT_MAX] = {
1209 /* state        event   */
1210 {
1211 /* DISABLED     PMF     */ {bnx2x_stats_pmf_update, STATS_STATE_DISABLED},
1212 /*              LINK_UP */ {bnx2x_stats_start,      STATS_STATE_ENABLED},
1213 /*              UPDATE  */ {bnx2x_stats_do_nothing, STATS_STATE_DISABLED},
1214 /*              STOP    */ {bnx2x_stats_do_nothing, STATS_STATE_DISABLED}
1215 },
1216 {
1217 /* ENABLED      PMF     */ {bnx2x_stats_pmf_start,  STATS_STATE_ENABLED},
1218 /*              LINK_UP */ {bnx2x_stats_restart,    STATS_STATE_ENABLED},
1219 /*              UPDATE  */ {bnx2x_stats_update,     STATS_STATE_ENABLED},
1220 /*              STOP    */ {bnx2x_stats_stop,       STATS_STATE_DISABLED}
1221 }
1222 };
1223
1224 void bnx2x_stats_handle(struct bnx2x *bp, enum bnx2x_stats_event event)
1225 {
1226         enum bnx2x_stats_state state;
1227
1228         if (unlikely(bp->panic))
1229                 return;
1230
1231         /* Protect a state change flow */
1232         spin_lock_bh(&bp->stats_lock);
1233         state = bp->stats_state;
1234         bp->stats_state = bnx2x_stats_stm[state][event].next_state;
1235         spin_unlock_bh(&bp->stats_lock);
1236
1237         bnx2x_stats_stm[state][event].action(bp);
1238
1239         if ((event != STATS_EVENT_UPDATE) || netif_msg_timer(bp))
1240                 DP(BNX2X_MSG_STATS, "state %d -> event %d -> state %d\n",
1241                    state, event, bp->stats_state);
1242 }
1243
1244 static void bnx2x_port_stats_base_init(struct bnx2x *bp)
1245 {
1246         struct dmae_command *dmae;
1247         u32 *stats_comp = bnx2x_sp(bp, stats_comp);
1248
1249         /* sanity */
1250         if (!bp->port.pmf || !bp->port.port_stx) {
1251                 BNX2X_ERR("BUG!\n");
1252                 return;
1253         }
1254
1255         bp->executer_idx = 0;
1256
1257         dmae = bnx2x_sp(bp, dmae[bp->executer_idx++]);
1258         dmae->opcode = (DMAE_CMD_SRC_PCI | DMAE_CMD_DST_GRC |
1259                         DMAE_CMD_C_DST_PCI | DMAE_CMD_C_ENABLE |
1260                         DMAE_CMD_SRC_RESET | DMAE_CMD_DST_RESET |
1261 #ifdef __BIG_ENDIAN
1262                         DMAE_CMD_ENDIANITY_B_DW_SWAP |
1263 #else
1264                         DMAE_CMD_ENDIANITY_DW_SWAP |
1265 #endif
1266                         (BP_PORT(bp) ? DMAE_CMD_PORT_1 : DMAE_CMD_PORT_0) |
1267                         (BP_E1HVN(bp) << DMAE_CMD_E1HVN_SHIFT));
1268         dmae->src_addr_lo = U64_LO(bnx2x_sp_mapping(bp, port_stats));
1269         dmae->src_addr_hi = U64_HI(bnx2x_sp_mapping(bp, port_stats));
1270         dmae->dst_addr_lo = bp->port.port_stx >> 2;
1271         dmae->dst_addr_hi = 0;
1272         dmae->len = sizeof(struct host_port_stats) >> 2;
1273         dmae->comp_addr_lo = U64_LO(bnx2x_sp_mapping(bp, stats_comp));
1274         dmae->comp_addr_hi = U64_HI(bnx2x_sp_mapping(bp, stats_comp));
1275         dmae->comp_val = DMAE_COMP_VAL;
1276
1277         *stats_comp = 0;
1278         bnx2x_hw_stats_post(bp);
1279         bnx2x_stats_comp(bp);
1280 }
1281
1282 static void bnx2x_func_stats_base_init(struct bnx2x *bp)
1283 {
1284         int vn, vn_max = IS_MF(bp) ? E1HVN_MAX : E1VN_MAX;
1285         int port = BP_PORT(bp);
1286         int func;
1287         u32 func_stx;
1288
1289         /* sanity */
1290         if (!bp->port.pmf || !bp->func_stx) {
1291                 BNX2X_ERR("BUG!\n");
1292                 return;
1293         }
1294
1295         /* save our func_stx */
1296         func_stx = bp->func_stx;
1297
1298         for (vn = VN_0; vn < vn_max; vn++) {
1299                 func = 2*vn + port;
1300
1301                 bp->func_stx = SHMEM_RD(bp, func_mb[func].fw_mb_param);
1302                 bnx2x_func_stats_init(bp);
1303                 bnx2x_hw_stats_post(bp);
1304                 bnx2x_stats_comp(bp);
1305         }
1306
1307         /* restore our func_stx */
1308         bp->func_stx = func_stx;
1309 }
1310
1311 static void bnx2x_func_stats_base_update(struct bnx2x *bp)
1312 {
1313         struct dmae_command *dmae = &bp->stats_dmae;
1314         u32 *stats_comp = bnx2x_sp(bp, stats_comp);
1315
1316         /* sanity */
1317         if (!bp->func_stx) {
1318                 BNX2X_ERR("BUG!\n");
1319                 return;
1320         }
1321
1322         bp->executer_idx = 0;
1323         memset(dmae, 0, sizeof(struct dmae_command));
1324
1325         dmae->opcode = (DMAE_CMD_SRC_GRC | DMAE_CMD_DST_PCI |
1326                         DMAE_CMD_C_DST_PCI | DMAE_CMD_C_ENABLE |
1327                         DMAE_CMD_SRC_RESET | DMAE_CMD_DST_RESET |
1328 #ifdef __BIG_ENDIAN
1329                         DMAE_CMD_ENDIANITY_B_DW_SWAP |
1330 #else
1331                         DMAE_CMD_ENDIANITY_DW_SWAP |
1332 #endif
1333                         (BP_PORT(bp) ? DMAE_CMD_PORT_1 : DMAE_CMD_PORT_0) |
1334                         (BP_E1HVN(bp) << DMAE_CMD_E1HVN_SHIFT));
1335         dmae->src_addr_lo = bp->func_stx >> 2;
1336         dmae->src_addr_hi = 0;
1337         dmae->dst_addr_lo = U64_LO(bnx2x_sp_mapping(bp, func_stats_base));
1338         dmae->dst_addr_hi = U64_HI(bnx2x_sp_mapping(bp, func_stats_base));
1339         dmae->len = sizeof(struct host_func_stats) >> 2;
1340         dmae->comp_addr_lo = U64_LO(bnx2x_sp_mapping(bp, stats_comp));
1341         dmae->comp_addr_hi = U64_HI(bnx2x_sp_mapping(bp, stats_comp));
1342         dmae->comp_val = DMAE_COMP_VAL;
1343
1344         *stats_comp = 0;
1345         bnx2x_hw_stats_post(bp);
1346         bnx2x_stats_comp(bp);
1347 }
1348
1349 void bnx2x_stats_init(struct bnx2x *bp)
1350 {
1351         int port = BP_PORT(bp);
1352         int func = BP_FUNC(bp);
1353         int i;
1354
1355         bp->stats_pending = 0;
1356         bp->executer_idx = 0;
1357         bp->stats_counter = 0;
1358
1359         /* port and func stats for management */
1360         if (!BP_NOMCP(bp)) {
1361                 bp->port.port_stx = SHMEM_RD(bp, port_mb[port].port_stx);
1362                 bp->func_stx = SHMEM_RD(bp, func_mb[func].fw_mb_param);
1363
1364         } else {
1365                 bp->port.port_stx = 0;
1366                 bp->func_stx = 0;
1367         }
1368         DP(BNX2X_MSG_STATS, "port_stx 0x%x  func_stx 0x%x\n",
1369            bp->port.port_stx, bp->func_stx);
1370
1371         /* port stats */
1372         memset(&(bp->port.old_nig_stats), 0, sizeof(struct nig_stats));
1373         bp->port.old_nig_stats.brb_discard =
1374                         REG_RD(bp, NIG_REG_STAT0_BRB_DISCARD + port*0x38);
1375         bp->port.old_nig_stats.brb_truncate =
1376                         REG_RD(bp, NIG_REG_STAT0_BRB_TRUNCATE + port*0x38);
1377         REG_RD_DMAE(bp, NIG_REG_STAT0_EGRESS_MAC_PKT0 + port*0x50,
1378                     &(bp->port.old_nig_stats.egress_mac_pkt0_lo), 2);
1379         REG_RD_DMAE(bp, NIG_REG_STAT0_EGRESS_MAC_PKT1 + port*0x50,
1380                     &(bp->port.old_nig_stats.egress_mac_pkt1_lo), 2);
1381
1382         /* function stats */
1383         for_each_queue(bp, i) {
1384                 struct bnx2x_fastpath *fp = &bp->fp[i];
1385
1386                 memset(&fp->old_tclient, 0,
1387                        sizeof(struct tstorm_per_client_stats));
1388                 memset(&fp->old_uclient, 0,
1389                        sizeof(struct ustorm_per_client_stats));
1390                 memset(&fp->old_xclient, 0,
1391                        sizeof(struct xstorm_per_client_stats));
1392                 memset(&fp->eth_q_stats, 0, sizeof(struct bnx2x_eth_q_stats));
1393         }
1394
1395         memset(&bp->dev->stats, 0, sizeof(struct net_device_stats));
1396         memset(&bp->eth_stats, 0, sizeof(struct bnx2x_eth_stats));
1397
1398         bp->stats_state = STATS_STATE_DISABLED;
1399
1400         if (bp->port.pmf) {
1401                 if (bp->port.port_stx)
1402                         bnx2x_port_stats_base_init(bp);
1403
1404                 if (bp->func_stx)
1405                         bnx2x_func_stats_base_init(bp);
1406
1407         } else if (bp->func_stx)
1408                 bnx2x_func_stats_base_update(bp);
1409 }