]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/dev/bxe/if_bxe.c
Merge new version of libcxxrt. This brings in three fixes:
[FreeBSD/FreeBSD.git] / sys / dev / bxe / if_bxe.c
1 /*-
2  * Copyright (c) 2007-2011 Broadcom Corporation. All rights reserved.
3  *
4  *    Gary Zambrano <zambrano@broadcom.com>
5  *    David Christensen <davidch@broadcom.com>
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. Neither the name of Broadcom Corporation nor the name of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written consent.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS'
21  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
24  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
30  * THE POSSIBILITY OF SUCH DAMAGE.
31  */
32
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
35
36 /*
37  * The following controllers are supported by this driver:
38  *   BCM57710  A1+
39  *   BCM57711  A0+
40  *   BCM57711E A0+
41  *
42  * The following controllers are not supported by this driver:
43  *   BCM57710  A0 (pre-production)
44  *
45  * External PHY References:
46  * ------------------------
47  * BCM8073  - Dual Port 10GBase-KR Ethernet PHY
48  * BCM8705  - 10Gb Ethernet Serial Transceiver
49  * BCM8706  - 10Gb Ethernet LRM PHY
50  * BCM8726  - Dual Port 10Gb Ethernet LRM PHY
51  * BCM8727  - Dual Port 10Gb Ethernet LRM PHY
52  * BCM8481  - Single Port 10GBase-T Ethernet PHY
53  * BCM84823 - Dual Port 10GBase-T Ethernet PHY
54  * SFX7101  - Solarflare 10GBase-T Ethernet PHY
55  *
56  */
57
58 #include "opt_bxe.h"
59 #include "bxe_include.h"
60 #include "if_bxe.h"
61 #include "bxe_init.h"
62
63 #include "hw_dump_reg_st.h"
64 #include "dump_e1.h"
65 #include "dump_e1h.h"
66
67 #include "bxe_self_test.h"
68
69 /* BXE Debug Options */
70 #ifdef BXE_DEBUG
71 uint32_t bxe_debug = BXE_WARN;
72
73 /*          0 = Never              */
74 /*          1 = 1 in 2,147,483,648 */
75 /*        256 = 1 in     8,388,608 */
76 /*       2048 = 1 in     1,048,576 */
77 /*      65536 = 1 in        32,768 */
78 /*    1048576 = 1 in         2,048 */
79 /*  268435456 = 1 in             8 */
80 /*  536870912 = 1 in             4 */
81 /* 1073741824 = 1 in             2 */
82
83 /* Controls how often to simulate an mbuf allocation failure. */
84 int bxe_debug_mbuf_allocation_failure = 0;
85
86 /* Controls how often to simulate a DMA mapping failure.  */
87 int bxe_debug_dma_map_addr_failure = 0;
88
89 /* Controls how often to simulate a bootcode failure. */
90 int bxe_debug_bootcode_running_failure = 0;
91 #endif
92
93 #define MDIO_INDIRECT_REG_ADDR  0x1f
94 #define MDIO_SET_REG_BANK(sc, reg_bank)         \
95         bxe_mdio22_write(sc, MDIO_INDIRECT_REG_ADDR, reg_bank)
96
97 #define MDIO_ACCESS_TIMEOUT     1000
98 #define BMAC_CONTROL_RX_ENABLE  2
99
100 /* BXE Build Time Options */
101 /* #define BXE_NVRAM_WRITE 1 */
102 #define BXE_USE_DMAE 1
103
104 /*
105  * PCI Device ID Table
106  * Used by bxe_probe() to identify the devices supported by this driver.
107  */
108 #define BXE_DEVDESC_MAX         64
109
110 static struct bxe_type bxe_devs[] = {
111         /* BCM57710 Controllers and OEM boards. */
112         { BRCM_VENDORID, BRCM_DEVICEID_BCM57710, PCI_ANY_ID,  PCI_ANY_ID,
113             "Broadcom NetXtreme II BCM57710 10GbE" },
114         /* BCM57711 Controllers and OEM boards. */
115         { BRCM_VENDORID, BRCM_DEVICEID_BCM57711, PCI_ANY_ID,  PCI_ANY_ID,
116             "Broadcom NetXtreme II BCM57711 10GbE" },
117         /* BCM57711E Controllers and OEM boards. */
118         { BRCM_VENDORID, BRCM_DEVICEID_BCM57711E, PCI_ANY_ID,  PCI_ANY_ID,
119             "Broadcom NetXtreme II BCM57711E 10GbE" },
120         {0, 0, 0, 0, NULL}
121 };
122
123 /*
124  * FreeBSD device entry points.
125  */
126 static int  bxe_probe(device_t);
127 static int  bxe_attach(device_t);
128 static int  bxe_detach(device_t);
129 static int  bxe_shutdown(device_t);
130
131 /*
132  * Driver local functions.
133  */
134 static void bxe_tunables_set(struct bxe_softc *);
135 static void bxe_print_adapter_info(struct bxe_softc *);
136 static void bxe_probe_pci_caps(struct bxe_softc *);
137 static void bxe_link_settings_supported(struct bxe_softc *, uint32_t);
138 static void bxe_link_settings_requested(struct bxe_softc *);
139 static int  bxe_hwinfo_function_get(struct bxe_softc *);
140 static int  bxe_hwinfo_port_get(struct bxe_softc *);
141 static int  bxe_hwinfo_common_get(struct bxe_softc *);
142 static void bxe_undi_unload(struct bxe_softc *);
143 static int  bxe_setup_leading(struct bxe_softc *);
144 static int  bxe_stop_leading(struct bxe_softc *);
145 static int  bxe_setup_multi(struct bxe_softc *, int);
146 static int  bxe_stop_multi(struct bxe_softc *, int);
147 static int  bxe_stop_locked(struct bxe_softc *, int);
148 static int  bxe_alloc_buf_rings(struct bxe_softc *);
149 static void bxe_free_buf_rings(struct bxe_softc *);
150 static void bxe_init_locked(struct bxe_softc *, int);
151 static int  bxe_wait_ramrod(struct bxe_softc *, int, int, int *, int);
152 static void bxe_init_str_wr(struct bxe_softc *, uint32_t, const uint32_t *,
153             uint32_t);
154 static void bxe_init_ind_wr(struct bxe_softc *, uint32_t, const uint32_t *,
155             uint16_t);
156 static void bxe_init_wr_64(struct bxe_softc *, uint32_t, const uint32_t *,
157             uint32_t);
158 static void bxe_write_big_buf(struct bxe_softc *, uint32_t, uint32_t);
159 static void bxe_init_fill(struct bxe_softc *, uint32_t, int, uint32_t);
160 static void bxe_init_block(struct bxe_softc *, uint32_t, uint32_t);
161 static void bxe_init(void *);
162 static void bxe_release_resources(struct bxe_softc *);
163 static void bxe_reg_wr_ind(struct bxe_softc *, uint32_t, uint32_t);
164 static uint32_t bxe_reg_rd_ind(struct bxe_softc *, uint32_t);
165 static void bxe_post_dmae(struct bxe_softc *, struct dmae_command *, int);
166 static void bxe_wb_wr(struct bxe_softc *, int, uint32_t, uint32_t);
167 static __inline uint32_t bxe_reg_poll(struct bxe_softc *, uint32_t,
168             uint32_t, int, int);
169 static int  bxe_mc_assert(struct bxe_softc *);
170 static void bxe_panic_dump(struct bxe_softc *);
171 static void bxe_int_enable(struct bxe_softc *);
172 static void bxe_int_disable(struct bxe_softc *);
173
174 static int  bxe_nvram_acquire_lock(struct bxe_softc *);
175 static int  bxe_nvram_release_lock(struct bxe_softc *);
176 static void bxe_nvram_enable_access(struct bxe_softc *);
177 static void bxe_nvram_disable_access(struct bxe_softc *);
178 static int  bxe_nvram_read_dword        (struct bxe_softc *, uint32_t, uint32_t *,
179             uint32_t);
180 static int  bxe_nvram_read(struct bxe_softc *, uint32_t, uint8_t *, int);
181
182 #ifdef BXE_NVRAM_WRITE_SUPPORT
183 static int  bxe_nvram_write_dword(struct bxe_softc *, uint32_t, uint32_t,
184             uint32_t);
185 static int  bxe_nvram_write1(struct bxe_softc *, uint32_t, uint8_t *, int);
186 static int  bxe_nvram_write(struct bxe_softc *, uint32_t, uint8_t *, int);
187 #endif
188
189 static int bxe_nvram_test(struct bxe_softc *);
190
191 static __inline void bxe_ack_sb(struct bxe_softc *, uint8_t, uint8_t, uint16_t,
192             uint8_t, uint8_t);
193 static __inline uint16_t bxe_update_fpsb_idx(struct bxe_fastpath *);
194 static uint16_t bxe_ack_int(struct bxe_softc *);
195 static void bxe_sp_event(struct bxe_fastpath *, union eth_rx_cqe *);
196 static int  bxe_acquire_hw_lock(struct bxe_softc *, uint32_t);
197 static int  bxe_release_hw_lock(struct bxe_softc *, uint32_t);
198 static void bxe_acquire_phy_lock(struct bxe_softc *);
199 static void bxe_release_phy_lock(struct bxe_softc *);
200 static void bxe_pmf_update(struct bxe_softc *);
201 static void bxe_init_port_minmax(struct bxe_softc *);
202 static void bxe_link_attn(struct bxe_softc *);
203
204 static int  bxe_sp_post(struct bxe_softc *, int, int, uint32_t, uint32_t, int);
205 static int  bxe_acquire_alr(struct bxe_softc *);
206 static void bxe_release_alr(struct bxe_softc *);
207 static uint16_t  bxe_update_dsb_idx(struct bxe_softc *);
208 static void bxe_attn_int_asserted(struct bxe_softc *, uint32_t);
209 static __inline void bxe_attn_int_deasserted0(struct bxe_softc *, uint32_t);
210 static __inline void bxe_attn_int_deasserted1(struct bxe_softc *, uint32_t);
211 static __inline void bxe_attn_int_deasserted2(struct bxe_softc *, uint32_t);
212 static __inline void bxe_attn_int_deasserted3(struct bxe_softc *, uint32_t);
213 static void bxe_attn_int_deasserted(struct bxe_softc *, uint32_t);
214 static void bxe_attn_int(struct bxe_softc *);
215
216 static void bxe_stats_storm_post(struct bxe_softc *);
217 static void bxe_stats_init(struct bxe_softc *);
218 static void bxe_stats_hw_post(struct bxe_softc *);
219 static int  bxe_stats_comp(struct bxe_softc *);
220 static void bxe_stats_pmf_update(struct bxe_softc *);
221 static void bxe_stats_port_base_init(struct bxe_softc *);
222 static void bxe_stats_port_init(struct bxe_softc *);
223 static void bxe_stats_func_base_init(struct bxe_softc *);
224 static void bxe_stats_func_init(struct bxe_softc *);
225 static void bxe_stats_start(struct bxe_softc *);
226 static void bxe_stats_pmf_start(struct bxe_softc *);
227 static void bxe_stats_restart(struct bxe_softc *);
228 static void bxe_stats_bmac_update(struct bxe_softc *);
229 static void bxe_stats_emac_update(struct bxe_softc *);
230 static int  bxe_stats_hw_update(struct bxe_softc *);
231 static int  bxe_stats_storm_update(struct bxe_softc *);
232 static void bxe_stats_func_base_update(struct bxe_softc *);
233 static void bxe_stats_update(struct bxe_softc *);
234 static void bxe_stats_port_stop(struct bxe_softc *);
235 static void bxe_stats_stop(struct bxe_softc *);
236 static void bxe_stats_do_nothing(struct bxe_softc *);
237 static void bxe_stats_handle(struct bxe_softc *, enum bxe_stats_event);
238
239 static int  bxe_tx_encap(struct bxe_fastpath *, struct mbuf **);
240 static void bxe_tx_start(struct ifnet *);
241 static void bxe_tx_start_locked(struct ifnet *, struct bxe_fastpath *);
242 static int  bxe_tx_mq_start(struct ifnet *, struct mbuf *);
243 static int  bxe_tx_mq_start_locked(struct ifnet *,
244     struct bxe_fastpath *, struct mbuf *);
245 static void bxe_mq_flush(struct ifnet *ifp);
246 static int  bxe_ioctl(struct ifnet *, u_long, caddr_t);
247 static __inline int bxe_has_rx_work(struct bxe_fastpath *);
248 static __inline int bxe_has_tx_work(struct bxe_fastpath *);
249
250 static void bxe_intr_legacy(void *);
251 static void bxe_task_sp(void *, int);
252 static void bxe_intr_sp(void *);
253 static void bxe_task_fp(void *, int);
254 static void bxe_intr_fp(void *);
255 static void bxe_zero_sb(struct bxe_softc *, int);
256 static void bxe_init_sb(struct bxe_softc *,
257     struct host_status_block *, bus_addr_t, int);
258 static void bxe_zero_def_sb(struct bxe_softc *);
259 static void bxe_init_def_sb(struct bxe_softc *,
260     struct host_def_status_block *, bus_addr_t, int);
261 static void bxe_update_coalesce(struct bxe_softc *);
262 static __inline void bxe_update_rx_prod(struct bxe_softc *,
263     struct bxe_fastpath *, uint16_t, uint16_t, uint16_t);
264 static void bxe_clear_sge_mask_next_elems(struct bxe_fastpath *);
265 static __inline void bxe_init_sge_ring_bit_mask(struct bxe_fastpath *);
266 static int  bxe_alloc_tpa_mbuf(struct bxe_fastpath *, int);
267 static int  bxe_fill_tpa_pool(struct bxe_fastpath *);
268 static void bxe_free_tpa_pool(struct bxe_fastpath *);
269
270 static int  bxe_alloc_rx_sge_mbuf(struct bxe_fastpath *, uint16_t);
271 static int  bxe_fill_sg_chain(struct bxe_fastpath *);
272 static void bxe_free_sg_chain(struct bxe_fastpath *);
273
274 static int  bxe_alloc_rx_bd_mbuf(struct bxe_fastpath *, uint16_t);
275 static int  bxe_fill_rx_bd_chain(struct bxe_fastpath *);
276 static void bxe_free_rx_bd_chain(struct bxe_fastpath *);
277
278 static void bxe_mutexes_alloc(struct bxe_softc *);
279 static void bxe_mutexes_free(struct bxe_softc *);
280 static void bxe_clear_rx_chains(struct bxe_softc *);
281 static int  bxe_init_rx_chains(struct bxe_softc *);
282 static void bxe_clear_tx_chains(struct bxe_softc *);
283 static void bxe_init_tx_chains(struct bxe_softc *);
284 static void bxe_init_sp_ring(struct bxe_softc *);
285 static void bxe_init_context(struct bxe_softc *);
286 static void bxe_init_ind_table(struct bxe_softc *);
287 static void bxe_set_client_config(struct bxe_softc *);
288 static void bxe_set_storm_rx_mode(struct bxe_softc *);
289 static void bxe_init_internal_common(struct bxe_softc *);
290 static void bxe_init_internal_port(struct bxe_softc *);
291
292 static void bxe_init_internal_func(struct bxe_softc *);
293 static void bxe_init_internal(struct bxe_softc *, uint32_t);
294 static int  bxe_init_nic(struct bxe_softc *, uint32_t);
295 static void bxe_lb_pckt(struct bxe_softc *);
296 static int  bxe_int_mem_test(struct bxe_softc *);
297 static void bxe_enable_blocks_attention (struct bxe_softc *);
298
299 static void bxe_init_pxp(struct bxe_softc *);
300 static int  bxe_init_common(struct bxe_softc *);
301 static int  bxe_init_port(struct bxe_softc *);
302 static void bxe_ilt_wr(struct bxe_softc *, uint32_t, bus_addr_t);
303 static int  bxe_init_func(struct bxe_softc *);
304 static int  bxe_init_hw(struct bxe_softc *, uint32_t);
305 static int  bxe_fw_command(struct bxe_softc *, uint32_t);
306 static void bxe_host_structures_free(struct bxe_softc *);
307 static void bxe_dma_map_addr(void *, bus_dma_segment_t *, int, int);
308 static int  bxe_host_structures_alloc(device_t);
309 static void bxe_set_mac_addr_e1(struct bxe_softc *, int);
310 static void bxe_set_mac_addr_e1h(struct bxe_softc *, int);
311 static void bxe_set_rx_mode(struct bxe_softc *);
312 static void bxe_reset_func(struct bxe_softc *);
313 static void bxe_reset_port(struct bxe_softc *);
314 static void bxe_reset_common(struct bxe_softc *);
315 static void bxe_reset_chip(struct bxe_softc *, uint32_t);
316 static int  bxe_ifmedia_upd(struct ifnet *);
317 static void bxe_ifmedia_status(struct ifnet *, struct ifmediareq *);
318 static __inline void bxe_update_last_max_sge(struct bxe_fastpath *, uint16_t);
319 static void bxe_update_sge_prod(struct bxe_fastpath *,
320             struct eth_fast_path_rx_cqe *);
321 static void bxe_tpa_start(struct bxe_fastpath *, uint16_t, uint16_t, uint16_t);
322 static int  bxe_fill_frag_mbuf(struct bxe_softc *, struct bxe_fastpath *,
323             struct mbuf *, struct eth_fast_path_rx_cqe *, uint16_t);
324 static void bxe_tpa_stop(struct bxe_softc *, struct bxe_fastpath *, uint16_t,
325             int, int, union eth_rx_cqe *, uint16_t);
326 static void bxe_rxeof(struct bxe_fastpath *);
327 static void bxe_txeof(struct bxe_fastpath *);
328 static int  bxe_watchdog(struct bxe_fastpath *fp);
329 static void bxe_tick(void *);
330 static void bxe_add_sysctls(struct bxe_softc *);
331
332 static void bxe_write_dmae_phys_len(struct bxe_softc *,
333     bus_addr_t, uint32_t, uint32_t);
334
335 void bxe_write_dmae(struct bxe_softc *, bus_addr_t, uint32_t, uint32_t);
336 void bxe_read_dmae(struct bxe_softc *, uint32_t, uint32_t);
337 int  bxe_set_gpio(struct bxe_softc *, int, uint32_t, uint8_t);
338 int  bxe_get_gpio(struct bxe_softc *, int, uint8_t);
339 int  bxe_set_spio(struct bxe_softc *, int, uint32_t);
340 int  bxe_set_gpio_int(struct bxe_softc *, int, uint32_t, uint8_t);
341
342 /*
343  * BXE Debug Data Structure Dump Routines
344  */
345
346 #ifdef BXE_DEBUG
347 static int bxe_sysctl_driver_state(SYSCTL_HANDLER_ARGS);
348 static int bxe_sysctl_hw_state(SYSCTL_HANDLER_ARGS);
349 static int bxe_sysctl_dump_fw(SYSCTL_HANDLER_ARGS);
350 static int bxe_sysctl_dump_rx_cq_chain(SYSCTL_HANDLER_ARGS);
351 static int bxe_sysctl_dump_rx_bd_chain(SYSCTL_HANDLER_ARGS);
352 static int bxe_sysctl_dump_tx_chain(SYSCTL_HANDLER_ARGS);
353 static int bxe_sysctl_reg_read(SYSCTL_HANDLER_ARGS);
354 static int bxe_sysctl_breakpoint(SYSCTL_HANDLER_ARGS);
355 static __noinline void bxe_validate_rx_packet(struct bxe_fastpath *,
356     uint16_t, union eth_rx_cqe *, struct mbuf *);
357 static void bxe_grcdump(struct bxe_softc *, int);
358 static __noinline void bxe_dump_enet(struct bxe_softc *,struct mbuf *);
359 static __noinline void bxe_dump_mbuf (struct bxe_softc *, struct mbuf *);
360 static __noinline void bxe_dump_tx_mbuf_chain(struct bxe_softc *, int, int);
361 static __noinline void bxe_dump_rx_mbuf_chain(struct bxe_softc *, int, int);
362 static __noinline void bxe_dump_tx_parsing_bd(struct bxe_fastpath *,int,
363     struct eth_tx_parse_bd *);
364 static __noinline void bxe_dump_txbd(struct bxe_fastpath *, int,
365     union eth_tx_bd_types *);
366 static __noinline void bxe_dump_rxbd(struct bxe_fastpath *, int,
367     struct eth_rx_bd *);
368 static __noinline void bxe_dump_cqe(struct bxe_fastpath *,
369     int, union eth_rx_cqe *);
370 static __noinline void bxe_dump_tx_chain(struct bxe_fastpath *, int, int);
371 static __noinline void bxe_dump_rx_cq_chain(struct bxe_fastpath *, int, int);
372 static __noinline void bxe_dump_rx_bd_chain(struct bxe_fastpath *, int, int);
373 static __noinline void bxe_dump_status_block(struct bxe_softc *);
374 static __noinline void bxe_dump_stats_block(struct bxe_softc *);
375 static __noinline void bxe_dump_fp_state(struct bxe_fastpath *);
376 static __noinline void bxe_dump_port_state_locked(struct bxe_softc *);
377 static __noinline void bxe_dump_link_vars_state_locked(struct bxe_softc *);
378 static __noinline void bxe_dump_link_params_state_locked(struct bxe_softc *);
379 static __noinline void bxe_dump_driver_state(struct bxe_softc *);
380 static __noinline void bxe_dump_hw_state(struct bxe_softc *);
381 static __noinline void bxe_dump_fw(struct bxe_softc *);
382 static void bxe_decode_mb_msgs(struct bxe_softc *, uint32_t, uint32_t);
383 static void bxe_decode_ramrod_cmd(struct bxe_softc *, int);
384 static void bxe_breakpoint(struct bxe_softc *);
385 #endif
386
387
388 #define BXE_DRIVER_VERSION      "1.5.52"
389
390 static void bxe_init_e1_firmware(struct bxe_softc *sc);
391 static void bxe_init_e1h_firmware(struct bxe_softc *sc);
392
393 /*
394  * FreeBSD device dispatch table.
395  */
396 static device_method_t bxe_methods[] = {
397         /* Device interface (device_if.h) */
398         DEVMETHOD(device_probe,         bxe_probe),
399         DEVMETHOD(device_attach,        bxe_attach),
400         DEVMETHOD(device_detach,        bxe_detach),
401         DEVMETHOD(device_shutdown,      bxe_shutdown),
402
403         DEVMETHOD_END
404 };
405
406
407 static driver_t bxe_driver = {
408         "bxe",
409         bxe_methods,
410         sizeof(struct bxe_softc)
411 };
412
413 static devclass_t bxe_devclass;
414
415 MODULE_DEPEND(bxe, pci, 1, 1, 1);
416 MODULE_DEPEND(bxe, ether, 1, 1, 1);
417 DRIVER_MODULE(bxe, pci, bxe_driver, bxe_devclass, 0, 0);
418
419 /*
420  * Tunable device values
421  */
422 static SYSCTL_NODE(_hw, OID_AUTO, bxe, CTLFLAG_RD, 0, "bxe driver parameters");
423 /* Allowable values are TRUE (1) or FALSE (0). */
424
425 static int bxe_dcc_enable = FALSE;
426 TUNABLE_INT("hw.bxe.dcc_enable", &bxe_dcc_enable);
427 SYSCTL_UINT(_hw_bxe, OID_AUTO, dcc_enable, CTLFLAG_RDTUN, &bxe_dcc_enable,
428     0, "dcc Enable/Disable");
429
430 /* Allowable values are TRUE (1) or FALSE (0). */
431 static int bxe_tso_enable = TRUE;
432 TUNABLE_INT("hw.bxe.tso_enable", &bxe_tso_enable);
433 SYSCTL_UINT(_hw_bxe, OID_AUTO, tso_enable, CTLFLAG_RDTUN, &bxe_tso_enable,
434     0, "TSO Enable/Disable");
435
436 /* Allowable values are 0 (IRQ), 1 (MSI/IRQ), and 2 (MSI-X/MSI/IRQ). */
437 static int bxe_int_mode = 2;
438 TUNABLE_INT("hw.bxe.int_mode", &bxe_int_mode);
439 SYSCTL_UINT(_hw_bxe, OID_AUTO, int_mode, CTLFLAG_RDTUN, &bxe_int_mode,
440     0, "Interrupt (MSI-X|MSI|INTx) mode");
441
442 /*
443  * Specifies the number of queues that will be used when a multi-queue
444  * RSS mode is selected  using bxe_multi_mode below.
445  *
446  * Allowable values are 0 (Auto) or 1 to MAX_CONTEXT (fixed queue number).
447  */
448 static int bxe_queue_count = 0;
449 TUNABLE_INT("hw.bxe.queue_count", &bxe_queue_count);
450 SYSCTL_UINT(_hw_bxe, OID_AUTO, queue_count, CTLFLAG_RDTUN, &bxe_queue_count,
451     0, "Multi-Queue queue count");
452
453 /*
454  * ETH_RSS_MODE_DISABLED (0)
455  * Disables all multi-queue/packet sorting algorithms.  All
456  * received frames are routed to a single receive queue.
457  *
458  * ETH_RSS_MODE_REGULAR (1)
459  * The default mode which assigns incoming frames to receive
460  * queues according to RSS (i.e a 2-tuple match on the source/
461  * destination IP address or a 4-tuple match on the source/
462  * destination IP address and the source/destination TCP port).
463  *
464  */
465 static int bxe_multi_mode = ETH_RSS_MODE_REGULAR;
466 TUNABLE_INT("hw.bxe.multi_mode", &bxe_multi_mode);
467 SYSCTL_UINT(_hw_bxe, OID_AUTO, multi_mode, CTLFLAG_RDTUN, &bxe_multi_mode,
468     0, "Multi-Queue Mode");
469
470 /*
471  * Host interrupt coalescing is controller by these values.
472  * The first frame always causes an interrupt but subsequent
473  * frames are coalesced until the RX/TX ticks timer value
474  * expires and another interrupt occurs.  (Ticks are measured
475  * in microseconds.)
476  */
477 static uint32_t bxe_rx_ticks = 25;
478 TUNABLE_INT("hw.bxe.rx_ticks", &bxe_rx_ticks);
479 SYSCTL_UINT(_hw_bxe, OID_AUTO, rx_ticks, CTLFLAG_RDTUN, &bxe_rx_ticks,
480     0, "Receive ticks");
481
482 static uint32_t bxe_tx_ticks = 50;
483 TUNABLE_INT("hw.bxe.tx_ticks", &bxe_tx_ticks);
484 SYSCTL_UINT(_hw_bxe, OID_AUTO, tx_ticks, CTLFLAG_RDTUN, &bxe_tx_ticks,
485     0, "Transmit ticks");
486
487 /*
488  * Allows the PCIe maximum read request size value to be manually
489  * set during initialization rather than automatically determined
490  * by the driver.
491  *
492  * Allowable values are:
493  * -1 (Auto), 0 (128B), 1 (256B), 2 (512B), 3 (1KB)
494  */
495 static int bxe_mrrs = -1;
496 TUNABLE_INT("hw.bxe.mrrs", &bxe_mrrs);
497 SYSCTL_UINT(_hw_bxe, OID_AUTO, mrrs, CTLFLAG_RDTUN, &bxe_mrrs,
498     0, "PCIe maximum read request size.");
499
500 #if 0
501 /*
502  * Allows setting the maximum number of received frames to process
503  * during an interrupt.
504  *
505  * Allowable values are:
506  * -1 (Unlimited), 0 (None), otherwise specifies the number of RX frames.
507  */
508 static int bxe_rx_limit = -1;
509 TUNABLE_INT("hw.bxe.rx_limit", &bxe_rx_limit);
510 SYSCTL_UINT(_hw_bxe, OID_AUTO, rx_limit, CTLFLAG_RDTUN, &bxe_rx_limit,
511     0, "Maximum received frames processed during an interrupt.");
512
513 /*
514  * Allows setting the maximum number of transmit frames to process
515  * during an interrupt.
516  *
517  * Allowable values are:
518  * -1 (Unlimited), 0 (None), otherwise specifies the number of TX frames.
519  */
520 static int bxe_tx_limit = -1;
521 TUNABLE_INT("hw.bxe.tx_limit", &bxe_tx_limit);
522 SYSCTL_UINT(_hw_bxe, OID_AUTO, tx_limit, CTLFLAG_RDTUN, &bxe_tx_limit,
523         0, "Maximum transmit frames processed during an interrupt.");
524 #endif
525
526 /*
527  * Global variables
528  */
529
530 /* 0 is common, 1 is port 0, 2 is port 1. */
531 static int load_count[3];
532
533 /* Tracks whether MCP firmware is running. */
534 static int nomcp;
535
536 #ifdef BXE_DEBUG
537 /*
538  * A debug version of the 32 bit OS register write function to
539  * capture/display values written to the controller.
540  *
541  * Returns:
542  *   None.
543  */
544 void
545 bxe_reg_write32(struct bxe_softc *sc, bus_size_t offset, uint32_t val)
546 {
547
548         if ((offset % 4) != 0) {
549                 DBPRINT(sc, BXE_WARN,
550                     "%s(): Warning! Unaligned write to 0x%jX!\n", __FUNCTION__,
551                     (uintmax_t)offset);
552         }
553
554         DBPRINT(sc, BXE_INSANE_REGS, "%s(): offset = 0x%jX, val = 0x%08X\n",
555             __FUNCTION__, (uintmax_t)offset, val);
556
557         bus_space_write_4(sc->bxe_btag, sc->bxe_bhandle, offset, val);
558 }
559
560 /*
561  * A debug version of the 16 bit OS register write function to
562  * capture/display values written to the controller.
563  *
564  * Returns:
565  *   None.
566  */
567 static void
568 bxe_reg_write16(struct bxe_softc *sc, bus_size_t offset, uint16_t val)
569 {
570
571         if ((offset % 2) != 0) {
572                 DBPRINT(sc, BXE_WARN,
573                     "%s(): Warning! Unaligned write to 0x%jX!\n", __FUNCTION__,
574                     (uintmax_t)offset);
575         }
576
577         DBPRINT(sc, BXE_INSANE_REGS, "%s(): offset = 0x%jX, val = 0x%04X\n",
578             __FUNCTION__, (uintmax_t)offset, val);
579
580         bus_space_write_2(sc->bxe_btag, sc->bxe_bhandle, offset, val);
581 }
582
583 /*
584  * A debug version of the 8 bit OS register write function to
585  * capture/display values written to the controller.
586  *
587  * Returns:
588  *   None.
589  */
590 static void
591 bxe_reg_write8(struct bxe_softc *sc, bus_size_t offset, uint8_t val)
592 {
593
594         DBPRINT(sc, BXE_INSANE_REGS, "%s(): offset = 0x%jX, val = 0x%02X\n",
595             __FUNCTION__, (uintmax_t)offset, val);
596
597         bus_space_write_1(sc->bxe_btag, sc->bxe_bhandle, offset, val);
598 }
599
600 /*
601  * A debug version of the 32 bit OS register read function to
602  * capture/display values read from the controller.
603  *
604  * Returns:
605  *   32bit value read.
606  */
607 uint32_t
608 bxe_reg_read32(struct bxe_softc *sc, bus_size_t offset)
609 {
610         uint32_t val;
611
612         if ((offset % 4) != 0) {
613                 DBPRINT(sc, BXE_WARN,
614                     "%s(): Warning! Unaligned read from 0x%jX!\n",
615                     __FUNCTION__, (uintmax_t)offset);
616         }
617
618         val = bus_space_read_4(sc->bxe_btag, sc->bxe_bhandle, offset);
619
620         DBPRINT(sc, BXE_INSANE_REGS, "%s(): offset = 0x%jX, val = 0x%08X\n",
621                 __FUNCTION__, (uintmax_t)offset, val);
622
623         return (val);
624 }
625
626 /*
627  * A debug version of the 16 bit OS register read function to
628  * capture/display values read from the controller.
629  *
630  * Returns:
631  *   16bit value read.
632  */
633 static uint16_t
634 bxe_reg_read16(struct bxe_softc *sc, bus_size_t offset)
635 {
636         uint16_t val;
637
638         if ((offset % 2) != 0) {
639                 DBPRINT(sc, BXE_WARN,
640                     "%s(): Warning! Unaligned read from 0x%jX!\n",
641                     __FUNCTION__, (uintmax_t)offset);
642         }
643
644         val = bus_space_read_2(sc->bxe_btag, sc->bxe_bhandle, offset);
645
646         DBPRINT(sc, BXE_INSANE_REGS, "%s(): offset = 0x%jX, val = 0x%08X\n",
647             __FUNCTION__, (uintmax_t)offset, val);
648
649         return (val);
650 }
651
652
653 /*
654  * A debug version of the 8 bit OS register write function to
655  * capture/display values written to the controller.
656  *
657  * Returns:
658  *   8bit value read.
659  */
660 static uint8_t
661 bxe_reg_read8(struct bxe_softc *sc, bus_size_t offset)
662 {
663         uint8_t val = bus_space_read_1(sc->bxe_btag, sc->bxe_bhandle, offset);
664
665         DBPRINT(sc, BXE_INSANE_REGS, "%s(): offset = 0x%jX, val = 0x%02X\n",
666                 __FUNCTION__, (uintmax_t)offset, val);
667
668         return (val);
669 }
670 #endif
671
672 static void
673 bxe_read_mf_cfg(struct bxe_softc *sc)
674 {
675         int func, vn;
676
677         for (vn = VN_0; vn < E1HVN_MAX; vn++) {
678                 func = 2 * vn + BP_PORT(sc);
679                 sc->mf_config[vn] =
680                     SHMEM_RD(sc,mf_cfg.func_mf_config[func].config);
681         }
682 }
683
684
685 static void
686 bxe_e1h_disable(struct bxe_softc *sc)
687 {
688         int port;
689
690         port = BP_PORT(sc);
691         REG_WR(sc, NIG_REG_LLH0_FUNC_EN + port * 8, 0);
692         sc->bxe_ifp->if_drv_flags = 0;
693 }
694
695 static void
696 bxe_e1h_enable(struct bxe_softc *sc)
697 {
698         int port;
699
700         port = BP_PORT(sc);
701         REG_WR(sc, NIG_REG_LLH0_FUNC_EN + port * 8, 1);
702         sc->bxe_ifp->if_drv_flags = IFF_DRV_RUNNING;
703 }
704
705 /*
706  * Calculates the sum of vn_min_rates.
707  * It's needed for further normalizing of the min_rates.
708  * Returns:
709  *   sum of vn_min_rates.
710  *     or
711  *   0 - if all the min_rates are 0. In the later case fainess
712  *       algorithm should be deactivated. If not all min_rates are
713  *       zero then those that are zeroes will be set to 1.
714  */
715 static void
716 bxe_calc_vn_wsum(struct bxe_softc *sc)
717 {
718         uint32_t vn_cfg, vn_min_rate;
719         int all_zero, vn;
720
721         DBENTER(BXE_VERBOSE_LOAD);
722
723         all_zero = 1;
724         sc->vn_wsum = 0;
725         for (vn = VN_0; vn < E1HVN_MAX; vn++) {
726                 vn_cfg = sc->mf_config[vn];
727                 vn_min_rate = ((vn_cfg & FUNC_MF_CFG_MIN_BW_MASK) >>
728                     FUNC_MF_CFG_MIN_BW_SHIFT) * 100;
729                 /* Skip hidden vns */
730                 if (vn_cfg & FUNC_MF_CFG_FUNC_HIDE)
731                         continue;
732                 /* If min rate is zero - set it to 1. */
733                 if (!vn_min_rate)
734                         vn_min_rate = DEF_MIN_RATE;
735                 else
736                         all_zero = 0;
737
738                 sc->vn_wsum += vn_min_rate;
739         }
740
741         /* ... only if all min rates are zeros - disable fairness */
742         if (all_zero)
743                 sc->cmng.flags.cmng_enables &= ~CMNG_FLAGS_PER_PORT_FAIRNESS_VN;
744         else
745                 sc->cmng.flags.cmng_enables |= CMNG_FLAGS_PER_PORT_FAIRNESS_VN;
746
747         DBEXIT(BXE_VERBOSE_LOAD);
748 }
749
750 /*
751  *
752  * Returns:
753  *   None.
754  */
755 static void
756 bxe_init_vn_minmax(struct bxe_softc *sc, int vn)
757 {
758         struct rate_shaping_vars_per_vn m_rs_vn;
759         struct fairness_vars_per_vn m_fair_vn;
760         uint32_t vn_cfg;
761         uint16_t vn_min_rate, vn_max_rate;
762         int func, i;
763
764         vn_cfg = sc->mf_config[vn];
765         func = 2 * vn + BP_PORT(sc);
766
767         DBENTER(BXE_VERBOSE_LOAD);
768
769         /* If function is hidden - set min and max to zeroes. */
770         if (vn_cfg & FUNC_MF_CFG_FUNC_HIDE) {
771                 vn_min_rate = 0;
772                 vn_max_rate = 0;
773         } else {
774                 vn_min_rate = ((vn_cfg & FUNC_MF_CFG_MIN_BW_MASK) >>
775                     FUNC_MF_CFG_MIN_BW_SHIFT) * 100;
776                 /*
777                  * If fairness is enabled (i.e. not all min rates are zero),
778                  * and if the current min rate is zero, set it to 1.
779                  * This is a requirement of the algorithm.
780                  */
781                 if (sc->vn_wsum && (vn_min_rate == 0))
782                         vn_min_rate = DEF_MIN_RATE;
783
784                 vn_max_rate = ((vn_cfg & FUNC_MF_CFG_MAX_BW_MASK) >>
785                     FUNC_MF_CFG_MAX_BW_SHIFT) * 100;
786
787                 if (vn_max_rate == 0)
788                         return;
789         }
790         DBPRINT(sc, BXE_INFO_LOAD,
791             "%s(): func %d: vn_min_rate = %d, vn_max_rate = %d, wsum = %d.\n",
792             __FUNCTION__, func, vn_min_rate, vn_max_rate, sc->vn_wsum);
793
794         memset(&m_rs_vn, 0, sizeof(struct rate_shaping_vars_per_vn));
795         memset(&m_fair_vn, 0, sizeof(struct fairness_vars_per_vn));
796
797         /* Global VNIC counter - maximal Mbps for this VNIC. */
798         m_rs_vn.vn_counter.rate = vn_max_rate;
799
800         /* Quota - number of bytes transmitted in this period. */
801         m_rs_vn.vn_counter.quota =
802             (vn_max_rate * RS_PERIODIC_TIMEOUT_USEC) / 8;
803
804         if (sc->vn_wsum) {
805                 /*
806                  * Credit for each period of the fairness algorithm.  The
807                  * number of bytes in T_FAIR (the VNIC shares the port rate).
808                  * vn_wsum should not be larger than 10000, thus
809                  * T_FAIR_COEF / (8 * vn_wsum) will always be grater than zero.
810                  */
811                 m_fair_vn.vn_credit_delta =
812                     max((uint32_t)(vn_min_rate * (T_FAIR_COEF /
813                     (8 * sc->vn_wsum))),
814                     (uint32_t)(sc->cmng.fair_vars.fair_threshold * 2));
815         }
816
817         func = BP_FUNC(sc);
818
819         /* Store it to internal memory */
820         for (i = 0; i < sizeof(struct rate_shaping_vars_per_vn) / 4; i++)
821                 REG_WR(sc, BAR_XSTORM_INTMEM +
822                     XSTORM_RATE_SHAPING_PER_VN_VARS_OFFSET(func) + (i * 4),
823                     ((uint32_t *)(&m_rs_vn))[i]);
824
825         for (i = 0; i < sizeof(struct fairness_vars_per_vn) / 4; i++)
826                 REG_WR(sc, BAR_XSTORM_INTMEM +
827                     XSTORM_FAIRNESS_PER_VN_VARS_OFFSET(func) + (i * 4),
828                     ((uint32_t *)(&m_fair_vn))[i]);
829
830         DBEXIT(BXE_VERBOSE_LOAD);
831 }
832
833 static void
834 bxe_congestionmgmt(struct bxe_softc *sc, uint8_t readshm)
835 {
836         int vn;
837
838         DBENTER(BXE_VERBOSE_LOAD);
839
840         /* Read mf conf from shmem. */
841         if (readshm)
842                 bxe_read_mf_cfg(sc);
843
844         /* Init rate shaping and fairness contexts */
845         bxe_init_port_minmax(sc);
846
847         /* vn_weight_sum and enable fairness if not 0 */
848         bxe_calc_vn_wsum(sc);
849
850         /* calculate and set min-max rate for each vn */
851         for (vn = 0; vn < E1HVN_MAX; vn++)
852                 bxe_init_vn_minmax(sc, vn);
853
854         /* Always enable rate shaping and fairness. */
855         sc->cmng.flags.cmng_enables |= CMNG_FLAGS_PER_PORT_RATE_SHAPING_VN;
856
857         DBPRINT(sc, BXE_VERBOSE_LOAD,
858             "%s(): Rate shaping set\n", __FUNCTION__);
859
860         if (!sc->vn_wsum)
861                 DBPRINT(sc, BXE_INFO_LOAD, "%s(): All MIN values "
862                     "are zeroes, fairness is disabled\n", __FUNCTION__);
863
864         DBEXIT(BXE_VERBOSE_LOAD);
865 }
866
867 static void
868 bxe_dcc_event(struct bxe_softc *sc, uint32_t dcc_event)
869 {
870         int i, port;
871
872         DBENTER(BXE_VERBOSE_LOAD);
873
874         if (dcc_event & DRV_STATUS_DCC_DISABLE_ENABLE_PF) {
875                 if (sc->mf_config[BP_E1HVN(sc)] & FUNC_MF_CFG_FUNC_DISABLED) {
876                         DBPRINT(sc, BXE_INFO_LOAD, "%s(): mf_cfg function "
877                             "disabled\n", __FUNCTION__);
878                         sc->state = BXE_STATE_DISABLED;
879                         bxe_e1h_disable(sc);
880                 } else {
881                         DBPRINT(sc, BXE_INFO_LOAD, "%s(): mf_cfg function "
882                             "enabled\n", __FUNCTION__);
883                         sc->state = BXE_STATE_OPEN;
884                         bxe_e1h_enable(sc);
885                 }
886                 dcc_event &= ~DRV_STATUS_DCC_DISABLE_ENABLE_PF;
887         }
888         if (dcc_event & DRV_STATUS_DCC_BANDWIDTH_ALLOCATION) {
889                 port = BP_PORT(sc);
890                 bxe_congestionmgmt(sc, TRUE);
891                 for (i = 0; i < sizeof(struct cmng_struct_per_port) / 4; i++)
892                         REG_WR(sc, BAR_XSTORM_INTMEM +
893                                XSTORM_CMNG_PER_PORT_VARS_OFFSET(port) + i*4,
894                                ((uint32_t *)(&sc->cmng))[i]);
895                 dcc_event &= ~DRV_STATUS_DCC_BANDWIDTH_ALLOCATION;
896         }
897
898         /* Report results to MCP */
899         if (dcc_event)
900                 bxe_fw_command(sc, DRV_MSG_CODE_DCC_FAILURE);
901         else
902                 bxe_fw_command(sc, DRV_MSG_CODE_DCC_OK);
903
904         DBEXIT(BXE_VERBOSE_LOAD);
905 }
906
907 /*
908  * Device probe function.
909  *
910  * Compares the device to the driver's list of supported devices and
911  * reports back to the OS whether this is the right driver for the device.
912  *
913  * Returns:
914  *   BUS_PROBE_DEFAULT on success, positive value on failure.
915  */
916 static int
917 bxe_probe(device_t dev)
918 {
919         struct bxe_softc *sc;
920         struct bxe_type *t;
921         char *descbuf;
922         uint16_t did, sdid, svid, vid;
923
924         sc = device_get_softc(dev);
925         sc->dev = dev;
926         t = bxe_devs;
927
928         /* Get the data for the device to be probed. */
929         vid  = pci_get_vendor(dev);
930         did  = pci_get_device(dev);
931         svid = pci_get_subvendor(dev);
932         sdid = pci_get_subdevice(dev);
933
934         DBPRINT(sc, BXE_VERBOSE_LOAD,
935             "%s(); VID = 0x%04X, DID = 0x%04X, SVID = 0x%04X, "
936             "SDID = 0x%04X\n", __FUNCTION__, vid, did, svid, sdid);
937
938         /* Look through the list of known devices for a match. */
939         while (t->bxe_name != NULL) {
940                 if ((vid == t->bxe_vid) && (did == t->bxe_did) &&
941                     ((svid == t->bxe_svid) || (t->bxe_svid == PCI_ANY_ID)) &&
942                     ((sdid == t->bxe_sdid) || (t->bxe_sdid == PCI_ANY_ID))) {
943                         descbuf = malloc(BXE_DEVDESC_MAX, M_TEMP, M_NOWAIT);
944                         if (descbuf == NULL)
945                                 return (ENOMEM);
946
947                         /* Print out the device identity. */
948                         snprintf(descbuf, BXE_DEVDESC_MAX,
949                             "%s (%c%d) BXE v:%s\n", t->bxe_name,
950                             (((pci_read_config(dev, PCIR_REVID, 4) &
951                             0xf0) >> 4) + 'A'),
952                             (pci_read_config(dev, PCIR_REVID, 4) & 0xf),
953                             BXE_DRIVER_VERSION);
954
955                         device_set_desc_copy(dev, descbuf);
956                         free(descbuf, M_TEMP);
957                         return (BUS_PROBE_DEFAULT);
958                 }
959                 t++;
960         }
961
962         return (ENXIO);
963 }
964
965 /*
966  * Prints useful adapter info.
967  *
968  * Returns:
969  *   None.
970  */
971 /* ToDo: Create a sysctl for this info. */
972 static void
973 bxe_print_adapter_info(struct bxe_softc *sc)
974 {
975         int i = 0;
976
977         DBENTER(BXE_EXTREME_LOAD);
978
979         /* Hardware chip info. */
980         BXE_PRINTF("ASIC (0x%08X); ", sc->common.chip_id);
981         printf("Rev (%c%d); ", (CHIP_REV(sc) >> 12) + 'A',
982                (CHIP_METAL(sc) >> 4));
983
984         /* Bus info. */
985         printf("Bus (PCIe x%d, ", sc->pcie_link_width);
986         switch (sc->pcie_link_speed) {
987         case 1:
988                 printf("2.5Gbps");
989                 break;
990         case 2:
991                 printf("5Gbps");
992                 break;
993         default:
994                 printf("Unknown link speed");
995         }
996
997         /* Device features. */
998         printf("); Flags (");
999
1000         /* Miscellaneous flags. */
1001         if (sc->msi_count > 0)
1002                 printf("MSI");
1003
1004         if (sc->msix_count > 0) {
1005                 if (i > 0) printf("|");
1006                 printf("MSI-X"); i++;
1007         }
1008
1009         if (TPA_ENABLED(sc)) {
1010                 if (i > 0) printf("|");
1011                 printf("TPA"); i++;
1012         }
1013
1014         printf("); Queues (");
1015         switch (sc->multi_mode) {
1016         case ETH_RSS_MODE_DISABLED:
1017                 printf("None");
1018                 break;
1019         case ETH_RSS_MODE_REGULAR:
1020                 printf("RSS:%d", sc->num_queues);
1021                 break;
1022         default:
1023                 printf("Unknown");
1024                 break;
1025         }
1026
1027         printf("); BD's (RX:%d,TX:%d",
1028             (int) USABLE_RX_BD, (int) USABLE_TX_BD);
1029
1030         /* Firmware versions and device features. */
1031         printf("); Firmware (%d.%d.%d); Bootcode (%d.%d.%d)\n",
1032             BCM_5710_FW_MAJOR_VERSION,
1033             BCM_5710_FW_MINOR_VERSION,
1034             BCM_5710_FW_REVISION_VERSION,
1035             (int)((sc->common.bc_ver & 0xff0000) >> 16),
1036             (int)((sc->common.bc_ver & 0x00ff00) >> 8),
1037             (int)((sc->common.bc_ver & 0x0000ff)));
1038
1039         DBEXIT(BXE_EXTREME_LOAD);
1040 }
1041
1042 /*
1043  * Release any interrupts allocated by the driver.
1044  *
1045  * Returns:
1046  *   None
1047  */
1048 static void
1049 bxe_interrupt_free(struct bxe_softc *sc)
1050 {
1051         device_t dev;
1052         int i;
1053
1054         DBENTER(BXE_VERBOSE_RESET | BXE_VERBOSE_UNLOAD);
1055
1056         dev = sc->dev;
1057
1058         if (sc->msix_count > 0) {
1059                 /* Free MSI-X resources. */
1060
1061                 for (i = 0; i < sc->msix_count; i++) {
1062                         DBPRINT(sc, (BXE_VERBOSE_LOAD | BXE_VERBOSE_RESET |
1063                             BXE_VERBOSE_INTR), "%s(): Releasing MSI-X[%d] "
1064                             "vector.\n", __FUNCTION__, i);
1065                         if (sc->bxe_msix_res[i] && sc->bxe_msix_rid[i])
1066                                 bus_release_resource(dev, SYS_RES_IRQ,
1067                                     sc->bxe_msix_rid[i], sc->bxe_msix_res[i]);
1068                 }
1069
1070                 pci_release_msi(dev);
1071
1072         } else if (sc->msi_count > 0) {
1073                 /* Free MSI resources. */
1074
1075                 for (i = 0; i < sc->msi_count; i++) {
1076                         DBPRINT(sc, (BXE_VERBOSE_LOAD | BXE_VERBOSE_RESET |
1077                             BXE_VERBOSE_INTR), "%s(): Releasing MSI[%d] "
1078                             "vector.\n", __FUNCTION__, i);
1079                         if (sc->bxe_msi_res[i] && sc->bxe_msi_rid[i])
1080                                 bus_release_resource(dev, SYS_RES_IRQ,
1081                                     sc->bxe_msi_rid[i], sc->bxe_msi_res[i]);
1082                 }
1083
1084                 pci_release_msi(dev);
1085
1086         } else {
1087                 /* Free legacy interrupt resources. */
1088
1089                 DBPRINT(sc, (BXE_VERBOSE_LOAD | BXE_VERBOSE_RESET |
1090                     BXE_VERBOSE_INTR), "%s(): Releasing legacy interrupt.\n",
1091                     __FUNCTION__);
1092                 if (sc->bxe_irq_res != NULL)
1093                         bus_release_resource(dev, SYS_RES_IRQ,
1094                             sc->bxe_irq_rid, sc->bxe_irq_res);
1095         }
1096
1097         DBEXIT(BXE_VERBOSE_RESET | BXE_VERBOSE_UNLOAD);
1098 }
1099
1100 /*
1101  * This function determines and allocates the appropriate
1102  * interrupt based on system capabilites and user request.
1103  *
1104  * The user may force a particular interrupt mode, specify
1105  * the number of receive queues, specify the method for
1106  * distribuitng received frames to receive queues, or use
1107  * the default settings which will automatically select the
1108  * best supported combination.  In addition, the OS may or
1109  * may not support certain combinations of these settings.
1110  * This routine attempts to reconcile the settings requested
1111  * by the user with the capabilites available from the system
1112  * to select the optimal combination of features.
1113  *
1114  * Returns:
1115  *   0 = Success, !0 = Failure.
1116  */
1117 static int
1118 bxe_interrupt_alloc(struct bxe_softc *sc)
1119 {
1120         device_t dev;
1121         int error, i, rid, rc;
1122         int msi_count, msi_required, msi_allocated;
1123         int msix_count, msix_required, msix_allocated;
1124
1125         DBENTER(BXE_VERBOSE_LOAD | BXE_VERBOSE_INTR);
1126
1127         rc = 0;
1128         dev = sc->dev;
1129         msi_count = msi_required = msi_allocated = 0;
1130         msix_count = msix_required = msix_allocated = 0;
1131
1132         /* Get the number of available MSI/MSI-X interrupts from the OS. */
1133         if (sc->int_mode > 0) {
1134                 if (sc->bxe_cap_flags & BXE_MSIX_CAPABLE_FLAG)
1135                         msix_count = pci_msix_count(dev);
1136
1137                 if (sc->bxe_cap_flags & BXE_MSI_CAPABLE_FLAG)
1138                         msi_count = pci_msi_count(dev);
1139
1140                 DBPRINT(sc, (BXE_VERBOSE_LOAD | BXE_VERBOSE_INTR),
1141                     "%s(): %d MSI and %d MSI-X vectors available.\n",
1142                     __FUNCTION__, msi_count, msix_count);
1143         }
1144
1145         /* Try allocating MSI-X interrupt resources. */
1146         if ((sc->bxe_cap_flags & BXE_MSIX_CAPABLE_FLAG) &&
1147             (sc->int_mode > 1) && (msix_count > 0) &&
1148             (msix_count >= sc->num_queues)) {
1149                 /* Ask for the necessary number of MSI-X vectors. */
1150                 if (sc->num_queues == 1)
1151                         msix_allocated = msix_required = 2;
1152                 else
1153                         msix_allocated = msix_required = sc->num_queues + 1;
1154
1155                 DBPRINT(sc, (BXE_VERBOSE_LOAD | BXE_VERBOSE_INTR),
1156                     "%s(): Requesting %d MSI-X vectors.\n",
1157                     __FUNCTION__, msix_required);
1158
1159                 /* BSD resource identifier */
1160                 rid = 1;
1161                 error = pci_alloc_msix(dev, &msix_allocated);
1162                 if (error == 0) {
1163                         DBPRINT(sc, (BXE_VERBOSE_LOAD | BXE_VERBOSE_INTR),
1164                 "%s(): Required/Allocated (%d/%d) MSI-X vector(s).\n",
1165                             __FUNCTION__, msix_required, msix_allocated);
1166
1167                         /* Make sure we got all the interrupts we asked for. */
1168                         if (msix_allocated >= msix_required) {
1169                                 sc->msix_count = msix_required;
1170                                 msi_count = 0;
1171
1172                                 /* Allocate the MSI-X vectors. */
1173                                 for (i = 0; i < msix_required; i++) {
1174                                         sc->bxe_msix_rid[i] = rid + i +
1175                                             BP_L_ID(sc);
1176                                         sc->bxe_msix_res[i] =
1177                                             bus_alloc_resource_any(dev,
1178                                             SYS_RES_IRQ, &sc->bxe_msix_rid[i],
1179                                             RF_ACTIVE);
1180                                         /* Report any IRQ allocation errors. */
1181                                         if (sc->bxe_msix_res[i] == NULL) {
1182                                                 BXE_PRINTF(
1183                                 "%s(%d): Failed to map MSI-X[%d] vector!\n",
1184                                                     __FILE__, __LINE__, (3));
1185                                                 rc = ENXIO;
1186                                                 goto bxe_interrupt_alloc_exit;
1187                                         }
1188                                 }
1189                         } else {
1190
1191                                 DBPRINT(sc, BXE_WARN,
1192                                     "%s(): MSI-X allocation failed!\n",
1193                                     __FUNCTION__);
1194
1195                                 /* Release any resources acquired. */
1196                                 pci_release_msi(dev);
1197                                 sc->msix_count = msix_count = 0;
1198
1199                                 /* We'll try MSI next. */
1200                                 sc->int_mode = 1;
1201                         }
1202                 }
1203         }
1204
1205         /* Try allocating MSI vector resources. */
1206         if ((sc->bxe_cap_flags & BXE_MSI_CAPABLE_FLAG) &&
1207             (sc->int_mode > 0) && (msi_count > 0) &&
1208             (msi_count >= sc->num_queues)) {
1209                 /* Ask for the necessary number of MSI vectors. */
1210                 if (sc->num_queues == 1)
1211                         msi_required = msi_allocated = 1;
1212                 else
1213                         msi_required = msi_allocated = BXE_MSI_VECTOR_COUNT;
1214
1215                 DBPRINT(sc, (BXE_VERBOSE_LOAD | BXE_VERBOSE_INTR),
1216                     "%s(): Requesting %d MSI vectors.\n", __FUNCTION__,
1217                     msi_required);
1218
1219                 rid = 1;
1220                 error = pci_alloc_msi(dev, &msi_allocated);
1221                 if (error == 0) {
1222                         DBPRINT(sc, (BXE_VERBOSE_LOAD | BXE_VERBOSE_INTR),
1223                             "%s(): Required/Allocated (%d/%d) MSI vector(s).\n",
1224                             __FUNCTION__, msi_required, msi_allocated);
1225
1226                         /*
1227                          * Make sure we got all the vectors we asked for.
1228                          * XXX
1229                          * FreeBSD always gives 8 even if we ask for less.
1230                          */
1231                         if (msi_required >= msi_allocated) {
1232                                 sc->msi_count = msi_required;
1233                                 /* Allocate the MSI vectors. */
1234                                 for (i = 0; i < msi_required; i++) {
1235                                         sc->bxe_msi_rid[i] = i + rid;
1236                                         sc->bxe_msi_res[i] =
1237                                             bus_alloc_resource_any(dev,
1238                                             SYS_RES_IRQ, &sc->bxe_msi_rid[i],
1239                                             RF_ACTIVE);
1240                                         /* Report any IRQ allocation errors. */
1241                                         if (sc->bxe_msi_res[i] == NULL) {
1242                                                 BXE_PRINTF(
1243                                 "%s(%d): Failed to map MSI vector (%d)!\n",
1244                                                     __FILE__, __LINE__, (i));
1245                                                 rc = ENXIO;
1246                                                 goto bxe_interrupt_alloc_exit;
1247                                         }
1248                                 }
1249                         }
1250                 } else {
1251
1252                         DBPRINT(sc, BXE_WARN, "%s(): MSI allocation failed!\n",
1253                             __FUNCTION__);
1254
1255                         /* Release any resources acquired. */
1256                         pci_release_msi(dev);
1257                         sc->msi_count = msi_count = 0;
1258
1259                         /* We'll try INTx next. */
1260                         sc->int_mode = 0;
1261                 }
1262         }
1263
1264         /* Try allocating INTx resources. */
1265         if (sc->int_mode == 0) {
1266                 sc->num_queues = 1;
1267                 sc->multi_mode = ETH_RSS_MODE_DISABLED;
1268
1269                 DBPRINT(sc, (BXE_VERBOSE_LOAD | BXE_VERBOSE_INTR),
1270                     "%s(): Requesting legacy INTx interrupt.\n",
1271                     __FUNCTION__);
1272
1273                 rid = 0;
1274                 sc->bxe_irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
1275                     RF_SHAREABLE | RF_ACTIVE);
1276                 /* Report any IRQ allocation errors. */
1277                 if (sc->bxe_irq_res == NULL) {
1278                         BXE_PRINTF("%s(%d): PCI map interrupt failed!\n",
1279                             __FILE__, __LINE__);
1280                         rc = ENXIO;
1281                         goto bxe_interrupt_alloc_exit;
1282                 }
1283                 sc->bxe_irq_rid = rid;
1284         }
1285
1286         DBPRINT(sc, (BXE_VERBOSE_LOAD | BXE_VERBOSE_INTR),
1287             "%s(): Actual: int_mode = %d, multi_mode = %d, num_queues = %d\n",
1288             __FUNCTION__, sc->int_mode, sc->multi_mode, sc->num_queues);
1289
1290 bxe_interrupt_alloc_exit:
1291         DBEXIT(BXE_VERBOSE_LOAD | BXE_VERBOSE_INTR);
1292         return (rc);
1293 }
1294
1295 /*
1296  * This function releases taskqueues.
1297  *
1298  * Returns:
1299  *   None
1300  */
1301 static void
1302 bxe_interrupt_detach(struct bxe_softc *sc)
1303 {
1304 #ifdef BXE_TASK
1305         struct bxe_fastpath *fp;
1306 #endif
1307         device_t dev;
1308         int i;
1309
1310         DBENTER(BXE_VERBOSE_UNLOAD);
1311
1312         dev = sc->dev;
1313
1314 #ifdef BXE_TASK
1315         /* Free the OS taskqueue resources. */
1316         for (i = 0; i < sc->num_queues; i++) {
1317                 fp = &sc->fp[i];
1318
1319                 if (fp->tq != NULL) {
1320                         taskqueue_drain(fp->tq, &fp->task);
1321                         taskqueue_free(fp->tq);
1322                 }
1323         }
1324
1325         if (sc->tq != NULL) {
1326                 taskqueue_drain(sc->tq, &sc->task);
1327                 taskqueue_free(sc->tq);
1328         }
1329 #endif
1330
1331         /* Release interrupt resources. */
1332         if (sc->msix_count > 0) {
1333                 for (i = 0; i < sc->msix_count; i++) {
1334                         if (sc->bxe_msix_tag[i] && sc->bxe_msix_res[i])
1335                                 bus_teardown_intr(dev, sc->bxe_msix_res[i],
1336                                     sc->bxe_msix_tag[i]);
1337                 }
1338         } else if (sc->msi_count > 0) {
1339                 for (i = 0; i < sc->msi_count; i++) {
1340                         if (sc->bxe_msi_tag[i] && sc->bxe_msi_res[i])
1341                                 bus_teardown_intr(dev, sc->bxe_msi_res[i],
1342                                     sc->bxe_msi_tag[i]);
1343                 }
1344         } else {
1345                 if (sc->bxe_irq_tag != NULL)
1346                         bus_teardown_intr(dev, sc->bxe_irq_res,
1347                             sc->bxe_irq_tag);
1348         }
1349
1350         DBEXIT(BXE_VERBOSE_UNLOAD);
1351 }
1352
1353 /*
1354  * This function enables interrupts and attachs to the ISR.
1355  *
1356  * When using multiple MSI/MSI-X vectors the first vector
1357  * is used for slowpath operations while all remaining
1358  * vectors are used for fastpath operations.  If only a
1359  * single MSI/MSI-X vector is used (SINGLE_ISR) then the
1360  * ISR must look for both slowpath and fastpath completions.
1361  *
1362  * Returns:
1363  *   0 = Success, !0 = Failure.
1364  */
1365 static int
1366 bxe_interrupt_attach(struct bxe_softc *sc)
1367 {
1368         struct bxe_fastpath *fp;
1369         int i, rc;
1370
1371         DBENTER(BXE_VERBOSE_LOAD | BXE_VERBOSE_INTR);
1372
1373         rc = 0;
1374
1375 #ifdef BXE_TASK
1376         /* Setup the slowpath deferred task queue. */
1377         TASK_INIT(&sc->task, 0, bxe_task_sp, sc);
1378         sc->tq = taskqueue_create_fast("bxe_spq", M_NOWAIT,
1379             taskqueue_thread_enqueue, &sc->tq);
1380         taskqueue_start_threads(&sc->tq, 1, PI_NET, "%s spq",
1381             device_get_nameunit(sc->dev));
1382 #endif
1383
1384         /* Setup interrupt handlers. */
1385         if (sc->msix_count > 0) {
1386                 DBPRINT(sc, (BXE_VERBOSE_LOAD | BXE_VERBOSE_INTR),
1387                     "%s(): Enabling slowpath MSI-X[0] vector.\n",__FUNCTION__);
1388                 /*
1389                  * Setup the interrupt handler.  Note that we pass the
1390                  * driver instance to the interrupt handler for the
1391                  * slowpath.
1392                  */
1393                 rc = bus_setup_intr(sc->dev, sc->bxe_msix_res[0],
1394                     INTR_TYPE_NET | INTR_MPSAFE, NULL, bxe_intr_sp,
1395                     sc, &sc->bxe_msix_tag[0]);
1396
1397                 if (rc) {
1398                         BXE_PRINTF(
1399                             "%s(%d): Failed to allocate MSI-X[0] vector!\n",
1400                             __FILE__, __LINE__);
1401                         goto bxe_interrupt_attach_exit;
1402                 }
1403
1404 #if __FreeBSD_version >= 800504
1405                 bus_describe_intr(sc->dev, sc->bxe_msix_res[0],
1406                                   sc->bxe_msix_tag[0], "sp");
1407 #endif
1408
1409                 /* Now initialize the fastpath vectors. */
1410                 for (i = 0; i < (sc->num_queues); i++) {
1411                         fp = &sc->fp[i];
1412                         DBPRINT(sc, (BXE_VERBOSE_LOAD | BXE_VERBOSE_INTR),
1413                             "%s(): Enabling MSI-X[%d] vector.\n",
1414                             __FUNCTION__, i + 1);
1415                         /*
1416                          * Setup the interrupt handler. Note that we pass the
1417                          * fastpath context to the interrupt handler in this
1418                          * case. Also the first msix_res was used by the sp.
1419                          */
1420                         rc = bus_setup_intr(sc->dev, sc->bxe_msix_res[i + 1],
1421                             INTR_TYPE_NET | INTR_MPSAFE, NULL, bxe_intr_fp,
1422                             fp, &sc->bxe_msix_tag[i + 1]);
1423
1424                         if (rc) {
1425                             BXE_PRINTF(
1426                             "%s(%d): Failed to allocate MSI-X[%d] vector!\n",
1427                             __FILE__, __LINE__, (i + 1));
1428                             goto bxe_interrupt_attach_exit;
1429                         }
1430
1431 #if __FreeBSD_version >= 800504
1432                         bus_describe_intr(sc->dev, sc->bxe_msix_res[i + 1],
1433                             sc->bxe_msix_tag[i + 1], "fp[%02d]",        i);
1434 #endif
1435
1436                         /* Bind the fastpath instance to a CPU. */
1437                         if (sc->num_queues > 1) {
1438                                 bus_bind_intr(sc->dev,
1439                                      sc->bxe_msix_res[i + 1], i);
1440                         }
1441
1442 #ifdef BXE_TASK
1443                         TASK_INIT(&fp->task, 0, bxe_task_fp, fp);
1444                         fp->tq = taskqueue_create_fast("bxe_fpq", M_NOWAIT,
1445                             taskqueue_thread_enqueue, &fp->tq);
1446                         taskqueue_start_threads(&fp->tq, 1, PI_NET, "%s fpq",
1447                             device_get_nameunit(sc->dev));
1448 #endif
1449                         fp->state = BXE_FP_STATE_IRQ;
1450                 }
1451         } else if (sc->msi_count > 0) {
1452                 DBPRINT(sc, (BXE_VERBOSE_LOAD | BXE_VERBOSE_INTR),
1453                         "%s(): Enabling slowpath MSI[0] vector.\n",
1454                         __FUNCTION__);
1455                 /*
1456                  * Setup the interrupt handler. Note that we pass the driver
1457                  * instance to the interrupt handler for the slowpath.
1458                  */
1459                 rc = bus_setup_intr(sc->dev,sc->bxe_msi_res[0],
1460                     INTR_TYPE_NET | INTR_MPSAFE, NULL, bxe_intr_sp,
1461                     sc, &sc->bxe_msi_tag[0]);
1462
1463                 if (rc) {
1464                         BXE_PRINTF(
1465                             "%s(%d): Failed to allocate MSI[0] vector!\n",
1466                             __FILE__, __LINE__);
1467                         goto bxe_interrupt_attach_exit;
1468                 }
1469
1470 #if __FreeBSD_version >= 800504
1471                 bus_describe_intr(sc->dev, sc->bxe_msi_res[0],
1472                     sc->bxe_msi_tag[0], "sp");
1473 #endif
1474
1475                 /* Now initialize the fastpath vectors. */
1476                 for (i = 0; i < (sc->num_queues); i++) {
1477                         fp = &sc->fp[i];
1478                         DBPRINT(sc,
1479                                 (BXE_VERBOSE_LOAD | BXE_VERBOSE_INTR),
1480                                 "%s(): Enabling MSI[%d] vector.\n",
1481                                 __FUNCTION__, i + 1);
1482                         /*
1483                          * Setup the interrupt handler. Note that we pass the
1484                          * fastpath context to the interrupt handler in this
1485                          * case.
1486                          */
1487                         rc = bus_setup_intr(sc->dev, sc->bxe_msi_res[i + 1],
1488                             INTR_TYPE_NET | INTR_MPSAFE, NULL, bxe_intr_fp,
1489                             fp, &sc->bxe_msi_tag[i + 1]);
1490
1491                         if (rc) {
1492                                 BXE_PRINTF(
1493                                 "%s(%d): Failed to allocate MSI[%d] vector!\n",
1494                                 __FILE__, __LINE__, (i + 1));
1495                                 goto bxe_interrupt_attach_exit;
1496                         }
1497
1498 #if __FreeBSD_version >= 800504
1499                         bus_describe_intr(sc->dev, sc->bxe_msi_res[i + 1],
1500                              sc->bxe_msi_tag[i + 1], "fp[%02d]", i);
1501 #endif
1502
1503 #ifdef BXE_TASK
1504                         TASK_INIT(&fp->task, 0, bxe_task_fp, fp);
1505                         fp->tq = taskqueue_create_fast("bxe_fpq", M_NOWAIT,
1506                             taskqueue_thread_enqueue, &fp->tq);
1507                         taskqueue_start_threads(&fp->tq, 1, PI_NET, "%s fpq",
1508                             device_get_nameunit(sc->dev));
1509 #endif
1510                 }
1511
1512         } else {
1513 #ifdef BXE_TASK
1514                 fp = &sc->fp[0];
1515 #endif
1516                 DBPRINT(sc, (BXE_VERBOSE_LOAD | BXE_VERBOSE_INTR),
1517                     "%s(): Enabling INTx interrupts.\n", __FUNCTION__);
1518
1519                 /*
1520                  * Setup the interrupt handler.  Note that we pass the
1521                  * driver instance to the interrupt handler which
1522                  * will handle both the slowpath and fastpath.
1523                  */
1524                 rc = bus_setup_intr(sc->dev,sc->bxe_irq_res, INTR_TYPE_NET |
1525                     INTR_MPSAFE, NULL, bxe_intr_legacy, sc, &sc->bxe_irq_tag);
1526
1527                 if (rc) {
1528                         BXE_PRINTF("%s(%d): Failed to allocate interrupt!\n",
1529                             __FILE__, __LINE__);
1530                         goto bxe_interrupt_attach_exit;
1531                 }
1532 #ifdef BXE_TASK
1533                 TASK_INIT(&fp->task, 0, bxe_task_fp, fp);
1534                 fp->tq = taskqueue_create_fast("bxe_fpq",
1535                     M_NOWAIT, taskqueue_thread_enqueue, &fp->tq);
1536                 taskqueue_start_threads(&fp->tq, 1,
1537                     PI_NET, "%s fpq", device_get_nameunit(sc->dev));
1538 #endif
1539         }
1540
1541 bxe_interrupt_attach_exit:
1542         DBEXIT(BXE_VERBOSE_LOAD | BXE_VERBOSE_INTR);
1543         return (rc);
1544 }
1545
1546
1547 /*
1548  * PCI Capabilities Probe Function.
1549  *
1550  * Walks the PCI capabiites list for the device to find what features are
1551  * supported.  These capabilites may be enabled/disabled by firmware so it's
1552  * best to walk the list rather than hard code any values.
1553  *
1554  * Returns:
1555  *   None.
1556  */
1557 static void
1558 bxe_probe_pci_caps(struct bxe_softc *sc)
1559 {
1560         device_t dev;
1561         uint32_t reg;
1562         uint16_t link_status;
1563
1564         dev = sc->dev;
1565         DBENTER(BXE_EXTREME_LOAD);
1566
1567         /* Check if PCI Power Management capability is enabled. */
1568         if (pci_find_cap(dev, PCIY_PMG, &reg) == 0) {
1569                 if (reg != 0) {
1570                         DBPRINT(sc, BXE_EXTREME_LOAD,
1571                                 "%s(): Found PM capability at 0x%04X\n",
1572                                 __FUNCTION__, reg);
1573                         sc->pm_cap = reg;
1574                 }
1575         }
1576
1577         /* Check if PCIe capability is enabled. */
1578         if (pci_find_cap(dev, PCIY_EXPRESS, &reg) == 0) {
1579                 if (reg != 0) {
1580                         link_status = pci_read_config(dev, reg + 0x12, 2);
1581
1582                         DBPRINT(sc, BXE_EXTREME_LOAD,
1583                                 "%s(): Found PCIe capability at 0x%04X\n",
1584                                 __FUNCTION__, reg);
1585
1586                         /* Handle PCIe 2.0 workarounds for the 57710. */
1587                         if (CHIP_IS_E1(sc)) {
1588                                 /* Workaround for 57710 errata E4_57710_27462. */
1589                                 sc->pcie_link_speed =
1590                                         (REG_RD(sc, 0x3d04) & (1 << 24)) ? 2 : 1;
1591
1592                                 /* Workaround for 57710 errata E4_57710_27488. */
1593                                 sc->pcie_link_width = (link_status >> 4) & 0x3f;
1594                                 if (sc->pcie_link_speed > 1)
1595                                         sc->pcie_link_width =
1596                                                 ((link_status >> 4) & 0x3f) >> 1;
1597
1598                         } else {
1599
1600                                 sc->pcie_link_speed = link_status & 0xf;
1601                                 sc->pcie_link_width = (link_status >> 4) & 0x3f;
1602
1603                         }
1604
1605                         sc->bxe_cap_flags |= BXE_PCIE_CAPABLE_FLAG;
1606                         sc->pcie_cap = reg;
1607                 }
1608         }
1609
1610
1611         /* Check if MSI capability is enabled. */
1612         if (pci_find_cap(dev, PCIY_MSI, &reg) == 0) {
1613                 if (reg != 0) {
1614                         DBPRINT(sc, BXE_EXTREME_LOAD,
1615                                 "%s(): Found MSI capability at 0x%04X\n",
1616                                 __FUNCTION__, reg);
1617                         sc->bxe_cap_flags |= BXE_MSI_CAPABLE_FLAG;
1618                 }
1619         }
1620
1621         /* Check if MSI-X capability is enabled. */
1622         if (pci_find_cap(dev, PCIY_MSIX, &reg) == 0) {
1623                 if (reg != 0) {
1624                         DBPRINT(sc, BXE_EXTREME_LOAD,
1625                                 "%s(): Found MSI-X capability at 0x%04X\n",
1626                                 __FUNCTION__, reg);
1627                         sc->bxe_cap_flags |= BXE_MSIX_CAPABLE_FLAG;
1628                 }
1629         }
1630
1631         DBEXIT(BXE_EXTREME_LOAD);
1632 }
1633
1634 /*
1635  * Setup firmware pointers for BCM57710.
1636  *
1637  * Returns:
1638  *   None
1639  */
1640 static void
1641 bxe_init_e1_firmware(struct bxe_softc *sc)
1642 {
1643         INIT_OPS(sc)                    = (struct raw_op *)init_ops_e1;
1644         INIT_DATA(sc)                   = (const uint32_t *)init_data_e1;
1645         INIT_OPS_OFFSETS(sc)            = (const uint16_t *)init_ops_offsets_e1;
1646         INIT_TSEM_INT_TABLE_DATA(sc)    = tsem_int_table_data_e1;
1647         INIT_TSEM_PRAM_DATA(sc)         = tsem_pram_data_e1;
1648         INIT_USEM_INT_TABLE_DATA(sc)    = usem_int_table_data_e1;
1649         INIT_USEM_PRAM_DATA(sc)         = usem_pram_data_e1;
1650         INIT_XSEM_INT_TABLE_DATA(sc)    = xsem_int_table_data_e1;
1651         INIT_XSEM_PRAM_DATA(sc)         = xsem_pram_data_e1;
1652         INIT_CSEM_INT_TABLE_DATA(sc)    = csem_int_table_data_e1;
1653         INIT_CSEM_PRAM_DATA(sc)         = csem_pram_data_e1;
1654 }
1655
1656 /*
1657  * Setup firmware pointers for BCM57711.
1658  *
1659  * Returns:
1660  *   None
1661  */
1662 static void
1663 bxe_init_e1h_firmware(struct bxe_softc *sc)
1664 {
1665         INIT_OPS(sc)                    = (struct raw_op *)init_ops_e1h;
1666         INIT_DATA(sc)                   = (const uint32_t *)init_data_e1h;
1667         INIT_OPS_OFFSETS(sc)            = (const uint16_t *)init_ops_offsets_e1h;
1668         INIT_TSEM_INT_TABLE_DATA(sc)    = tsem_int_table_data_e1h;
1669         INIT_TSEM_PRAM_DATA(sc)         = tsem_pram_data_e1h;
1670         INIT_USEM_INT_TABLE_DATA(sc)    = usem_int_table_data_e1h;
1671         INIT_USEM_PRAM_DATA(sc)         = usem_pram_data_e1h;
1672         INIT_XSEM_INT_TABLE_DATA(sc)    = xsem_int_table_data_e1h;
1673         INIT_XSEM_PRAM_DATA(sc)         = xsem_pram_data_e1h;
1674         INIT_CSEM_INT_TABLE_DATA(sc)    = csem_int_table_data_e1h;
1675         INIT_CSEM_PRAM_DATA(sc)         = csem_pram_data_e1h;
1676 }
1677
1678 /*
1679  * Sets up pointers for loading controller firmware.
1680  *
1681  * Returns:
1682  *   0 = Success, !0 = Failure
1683  */
1684 static int
1685 bxe_init_firmware(struct bxe_softc *sc)
1686 {
1687         int rc;
1688
1689         rc = 0;
1690
1691         if (CHIP_IS_E1(sc))
1692                 bxe_init_e1_firmware(sc);
1693         else if (CHIP_IS_E1H(sc))
1694                 bxe_init_e1h_firmware(sc);
1695         else {
1696                 BXE_PRINTF("%s(%d): No firmware to support chip revision!\n",
1697                     __FILE__, __LINE__);
1698                 rc = ENXIO;
1699         }
1700
1701         return (rc);
1702 }
1703
1704 static void
1705 bxe_tunables_set(struct bxe_softc *sc)
1706 {
1707         /*
1708          * Get our starting point for interrupt mode/number of queues.
1709          * We will progressively step down from MSI-X to MSI to INTx
1710          * and reduce the number of receive queues as necessary to
1711          * match the system capabilities.
1712          */
1713         sc->multi_mode  = bxe_multi_mode;
1714         sc->int_mode    = bxe_int_mode;
1715         sc->tso_enable  = bxe_tso_enable;
1716
1717         /*
1718          * Verify the Priority -> Receive Queue mappings.
1719          */
1720         if (sc->int_mode > 0) {
1721                 /* Multi-queue modes require MSI/MSI-X. */
1722                 switch (sc->multi_mode) {
1723                 case ETH_RSS_MODE_DISABLED:
1724                         /* No multi-queue mode requested. */
1725                         sc->num_queues = 1;
1726                         break;
1727                 case ETH_RSS_MODE_REGULAR:
1728                         if (sc->int_mode > 1) {
1729                                 /*
1730                                  * Assume we can use MSI-X
1731                                  * (max of 16 receive queues).
1732                                  */
1733                                 sc->num_queues = min((bxe_queue_count ?
1734                                     bxe_queue_count : mp_ncpus), MAX_CONTEXT);
1735                         } else {
1736                                 /*
1737                                  * Assume we can use MSI
1738                                  * (max of 7 receive queues).
1739                                  */
1740                                 sc->num_queues = min((bxe_queue_count ?
1741                                     bxe_queue_count : mp_ncpus),
1742                                     BXE_MSI_VECTOR_COUNT - 1);
1743                         }
1744                         break;
1745                 default:
1746                         BXE_PRINTF(
1747                             "%s(%d): Unsupported multi_mode parameter (%d), "
1748                             "disabling multi-queue support!\n", __FILE__,
1749                             __LINE__, sc->multi_mode);
1750                         sc->multi_mode = ETH_RSS_MODE_DISABLED;
1751                         sc->num_queues = 1;
1752                         break;
1753                 }
1754         } else {
1755                 /* User has forced INTx mode. */
1756                 sc->multi_mode = ETH_RSS_MODE_DISABLED;
1757                 sc->num_queues = 1;
1758         }
1759
1760         DBPRINT(sc, (BXE_VERBOSE_LOAD | BXE_VERBOSE_INTR),
1761             "%s(): Requested: int_mode = %d, multi_mode = %d num_queues = %d\n",
1762             __FUNCTION__, sc->int_mode, sc->multi_mode, sc->num_queues);
1763
1764         sc->stats_enable = TRUE;
1765
1766         /* Select the host coalescing tick count values (limit values). */
1767         if (bxe_tx_ticks > 100) {
1768                 BXE_PRINTF("%s(%d): bxe_tx_ticks too large "
1769                     "(%d), setting default value of 50.\n",
1770                     __FILE__, __LINE__, bxe_tx_ticks);
1771                 sc->tx_ticks = 50;
1772         } else
1773                 sc->tx_ticks = bxe_tx_ticks;
1774
1775         if (bxe_rx_ticks > 100) {
1776                 BXE_PRINTF("%s(%d): bxe_rx_ticks too large "
1777                     "(%d), setting default value of 25.\n",
1778                     __FILE__, __LINE__, bxe_rx_ticks);
1779                 sc->rx_ticks = 25;
1780         } else
1781                 sc->rx_ticks = bxe_rx_ticks;
1782
1783         /* Select the PCIe maximum read request size (MRRS). */
1784         if (bxe_mrrs > 3)
1785                 sc->mrrs = 3;
1786         else
1787                 sc->mrrs = bxe_mrrs;
1788
1789         /* Check for DCC support. */
1790         if (bxe_dcc_enable == FALSE)
1791                 sc->dcc_enable = FALSE;
1792         else
1793                 sc->dcc_enable = TRUE;
1794 }
1795
1796
1797 /*
1798  * Allocates PCI resources from OS.
1799  *
1800  * Returns:
1801  *   0 = Success, !0 = Failure
1802  */
1803 static int
1804 bxe_pci_resources_alloc(struct bxe_softc *sc)
1805 {
1806         int rid, rc = 0;
1807
1808         DBENTER(BXE_VERBOSE_LOAD);
1809
1810         /*
1811          * Allocate PCI memory resources for BAR0.
1812          * This includes device registers and internal
1813          * processor memory.
1814          */
1815         rid = PCIR_BAR(0);
1816         sc->bxe_res = bus_alloc_resource_any(sc->dev,
1817             SYS_RES_MEMORY, &rid, RF_ACTIVE);
1818         if (sc->bxe_res == NULL) {
1819                 BXE_PRINTF("%s(%d):PCI BAR0 memory allocation failed\n",
1820                     __FILE__, __LINE__);
1821                 rc = ENXIO;
1822                 goto bxe_pci_resources_alloc_exit;
1823         }
1824
1825         /* Get OS resource handles for BAR0 memory. */
1826         sc->bxe_btag    = rman_get_bustag(sc->bxe_res);
1827         sc->bxe_bhandle = rman_get_bushandle(sc->bxe_res);
1828         sc->bxe_vhandle = (vm_offset_t) rman_get_virtual(sc->bxe_res);
1829
1830         /*
1831          * Allocate PCI memory resources for BAR2.
1832          * Doorbell (DB) memory.
1833          */
1834         rid = PCIR_BAR(2);
1835         sc->bxe_db_res = bus_alloc_resource_any(sc->dev,
1836             SYS_RES_MEMORY, &rid, RF_ACTIVE);
1837         if (sc->bxe_db_res == NULL) {
1838                 BXE_PRINTF("%s(%d): PCI BAR2 memory allocation failed\n",
1839                     __FILE__, __LINE__);
1840                 rc = ENXIO;
1841                 goto bxe_pci_resources_alloc_exit;
1842         }
1843
1844         /* Get OS resource handles for BAR2 memory. */
1845         sc->bxe_db_btag    = rman_get_bustag(sc->bxe_db_res);
1846         sc->bxe_db_bhandle = rman_get_bushandle(sc->bxe_db_res);
1847         sc->bxe_db_vhandle = (vm_offset_t) rman_get_virtual(sc->bxe_db_res);
1848
1849 bxe_pci_resources_alloc_exit:
1850         DBEXIT(BXE_VERBOSE_LOAD);
1851         return (rc);
1852 }
1853
1854
1855 /*
1856  * Frees PCI resources allocated in bxe_pci_resources_alloc().
1857  *
1858  * Returns:
1859  *   None
1860  */
1861 static void
1862 bxe_pci_resources_free(struct bxe_softc *sc)
1863 {
1864         DBENTER(BXE_VERBOSE_UNLOAD);
1865
1866         /* Release the PCIe BAR0 mapped memory. */
1867         if (sc->bxe_res != NULL) {
1868                 bus_release_resource(sc->dev, SYS_RES_MEMORY,
1869                     PCIR_BAR(0), sc->bxe_res);
1870         }
1871
1872         /* Release the PCIe BAR2 (doorbell) mapped memory. */
1873         if (sc->bxe_db_res != NULL) {
1874                 bus_release_resource(sc->dev, SYS_RES_MEMORY,
1875                     PCIR_BAR(2), sc->bxe_db_res);
1876         }
1877
1878         DBENTER(BXE_VERBOSE_UNLOAD);
1879 }
1880
1881
1882 /*
1883  * Determines the media reported to the OS by examining
1884  * the installed PHY type.
1885  *
1886  * Returns:
1887  *   0 = Success, !0 = Failure
1888  */
1889 static int
1890 bxe_media_detect(struct bxe_softc *sc)
1891 {
1892         int rc;
1893
1894         rc = 0;
1895
1896         /* Identify supported media based on the PHY type. */
1897         switch (XGXS_EXT_PHY_TYPE(sc->link_params.ext_phy_config)) {
1898         case PORT_HW_CFG_XGXS_EXT_PHY_TYPE_DIRECT:
1899                 DBPRINT(sc, BXE_INFO_LOAD,
1900                     "%s(): Found 10GBase-CX4 media.\n", __FUNCTION__);
1901                 sc->media = IFM_10G_CX4;
1902                 break;
1903         case PORT_HW_CFG_XGXS_EXT_PHY_TYPE_BCM8073:
1904                 /* Technically 10GBase-KR but report as 10GBase-SR*/
1905         case PORT_HW_CFG_XGXS_EXT_PHY_TYPE_BCM8726:
1906         case PORT_HW_CFG_XGXS_EXT_PHY_TYPE_BCM8727:
1907         case PORT_HW_CFG_XGXS_EXT_PHY_TYPE_BCM8727_NOC:
1908                 DBPRINT(sc, BXE_INFO_LOAD,
1909                     "%s(): Found 10GBase-SR media.\n", __FUNCTION__);
1910                 sc->media = IFM_10G_SR;
1911                 break;
1912         case PORT_HW_CFG_XGXS_EXT_PHY_TYPE_BCM8705:
1913         case PORT_HW_CFG_XGXS_EXT_PHY_TYPE_BCM8706:
1914                 DBPRINT(sc, BXE_INFO_LOAD,
1915                     "%s(): Found 10Gb twinax media.\n", __FUNCTION__);
1916                 sc->media = IFM_10G_TWINAX;
1917                 break;
1918         case PORT_HW_CFG_XGXS_EXT_PHY_TYPE_BCM8481:
1919         case PORT_HW_CFG_XGXS_EXT_PHY_TYPE_SFX7101:
1920         case PORT_HW_CFG_XGXS_EXT_PHY_TYPE_BCM84823:
1921                 DBPRINT(sc, BXE_INFO_LOAD,
1922                     "%s(): Found 10GBase-T media.\n", __FUNCTION__);
1923                 sc->media = IFM_10G_T;
1924                 break;
1925         case PORT_HW_CFG_XGXS_EXT_PHY_TYPE_FAILURE:
1926         case PORT_HW_CFG_XGXS_EXT_PHY_TYPE_NOT_CONN:
1927         default:
1928                 sc->media = 0;
1929                 rc = ENODEV;
1930         }
1931
1932         return (rc);
1933 }
1934
1935
1936 /*
1937  * Device attach function.
1938  *
1939  * Allocates device resources, performs secondary chip identification,
1940  * resets and initializes the hardware, and initializes driver instance
1941  * variables.
1942  *
1943  * Returns:
1944  *   0 = Success, Positive value on failure.
1945  */
1946 static int
1947 bxe_attach(device_t dev)
1948 {
1949         struct bxe_softc *sc;
1950         struct ifnet *ifp;
1951         int rc;
1952
1953         sc = device_get_softc(dev);
1954         DBENTER(BXE_INFO_LOAD | BXE_INFO_RESET);
1955
1956         sc->dev = dev;
1957         sc->bxe_unit = device_get_unit(dev);
1958         sc->bxe_func = pci_get_function(dev);
1959         sc->bxe_flags = 0;
1960         sc->state = BXE_STATE_CLOSED;
1961         rc = 0;
1962
1963         DBPRINT(sc, BXE_FATAL, "%s(): ************************\n",
1964             __FUNCTION__);
1965         DBPRINT(sc, BXE_FATAL, "%s(): ** Debug mode enabled **\n",
1966             __FUNCTION__);
1967         DBPRINT(sc, BXE_FATAL, "%s(): ************************\n",
1968             __FUNCTION__);
1969         DBPRINT(sc, BXE_FATAL, "%s(): sc vaddr = 0x%08X:%08X\n",
1970             __FUNCTION__, (uint32_t) U64_HI(sc), (uint32_t) U64_LO(sc));
1971
1972         /* Get the user configurable values for driver load. */
1973         bxe_tunables_set(sc);
1974
1975         bxe_mutexes_alloc(sc);
1976
1977         /* Prepare tick routine. */
1978         callout_init_mtx(&sc->bxe_tick_callout, &sc->bxe_core_mtx, 0);
1979
1980         /* Enable bus master capability */
1981         pci_enable_busmaster(dev);
1982
1983         /* Enable PCI BAR mapped memory for register access. */
1984         rc = bxe_pci_resources_alloc(sc);
1985         if (rc != 0) {
1986                 BXE_PRINTF("%s(%d): Error allocating PCI resources!\n",
1987                     __FILE__, __LINE__);
1988                 goto bxe_attach_fail;
1989         }
1990
1991         /* Put indirect address registers into a sane state. */
1992         pci_write_config(sc->dev, PCICFG_GRC_ADDRESS,
1993             PCICFG_VENDOR_ID_OFFSET, 4);
1994         REG_WR(sc, PXP2_REG_PGL_ADDR_88_F0 + BP_PORT(sc) * 16, 0);
1995         REG_WR(sc, PXP2_REG_PGL_ADDR_8C_F0 + BP_PORT(sc) * 16, 0);
1996         REG_WR(sc, PXP2_REG_PGL_ADDR_90_F0 + BP_PORT(sc) * 16, 0);
1997         REG_WR(sc, PXP2_REG_PGL_ADDR_94_F0 + BP_PORT(sc) * 16, 0);
1998
1999         /* Get hardware info from shared memory and validate data. */
2000         rc = bxe_hwinfo_function_get(sc);
2001         if (rc != 0) {
2002                 DBPRINT(sc, BXE_WARN,
2003                     "%s(): Failed to get hardware info!\n", __FUNCTION__);
2004                 goto bxe_attach_fail;
2005         }
2006
2007         /* Setup supported media options. */
2008         rc = bxe_media_detect(sc);
2009         if (rc != 0) {
2010                 BXE_PRINTF("%s(%d): Unknown media (PHY) type!\n",
2011                     __FILE__, __LINE__);
2012                 goto bxe_attach_fail;
2013         }
2014
2015         /* Interface entrypoint for media type/status reporting. */
2016         ifmedia_init(&sc->bxe_ifmedia,
2017             IFM_IMASK, bxe_ifmedia_upd, bxe_ifmedia_status);
2018
2019         /* Default interface values. */
2020         ifmedia_add(&sc->bxe_ifmedia,
2021             IFM_ETHER | sc->media | IFM_FDX, 0, NULL);
2022         ifmedia_add(&sc->bxe_ifmedia,
2023             IFM_ETHER | IFM_AUTO, 0, NULL);
2024         ifmedia_set(&sc->bxe_ifmedia,
2025             IFM_ETHER | IFM_AUTO);
2026         sc->bxe_ifmedia.ifm_media =
2027             sc->bxe_ifmedia.ifm_cur->ifm_media;
2028
2029         /* Setup firmware arrays (firmware load comes later). */
2030         rc = bxe_init_firmware(sc);
2031         if (rc) {
2032                 BXE_PRINTF("%s(%d): Error preparing firmware load!\n",
2033                     __FILE__, __LINE__);
2034                 goto bxe_attach_fail;
2035         }
2036
2037 #ifdef BXE_DEBUG
2038         /* Allocate a memory buffer for grcdump output.*/
2039         sc->grcdump_buffer = malloc(BXE_GRCDUMP_BUF_SIZE, M_TEMP, M_NOWAIT);
2040         if (sc->grcdump_buffer == NULL) {
2041                 BXE_PRINTF("%s(%d): Failed to allocate grcdump memory "
2042                     "buffer!\n", __FILE__, __LINE__);
2043                 rc = ENOBUFS;
2044         }
2045 #endif
2046
2047         /* Check that NVRAM contents are valid.*/
2048         rc = bxe_nvram_test(sc);
2049         if (rc != 0) {
2050                 BXE_PRINTF("%s(%d): Failed NVRAM test!\n",
2051                     __FILE__, __LINE__);
2052                 goto bxe_attach_fail;
2053         }
2054
2055         /* Allocate the appropriate interrupts.*/
2056         rc = bxe_interrupt_alloc(sc);
2057         if (rc != 0) {
2058                 BXE_PRINTF("%s(%d): Interrupt allocation failed!\n",
2059                     __FILE__, __LINE__);
2060                 goto bxe_attach_fail;
2061         }
2062
2063         /* Useful for accessing unconfigured devices (i.e. factory diags).*/
2064         if (nomcp)
2065                 sc->bxe_flags |= BXE_NO_MCP_FLAG;
2066
2067         /* If bootcode is not running only initialize port 0. */
2068         if (nomcp && BP_PORT(sc)) {
2069                 BXE_PRINTF(
2070                     "%s(%d): Second device disabled (no bootcode), "
2071                     "exiting...\n", __FILE__, __LINE__);
2072                 rc = ENODEV;
2073                 goto bxe_attach_fail;
2074         }
2075
2076         /* Check if PXE/UNDI is still active and unload it. */
2077         if (!NOMCP(sc))
2078                 bxe_undi_unload(sc);
2079
2080         /*
2081          * Select the RX and TX ring sizes.  The actual
2082          * ring size for TX is complicated by the fact
2083          * that a single TX frame may be broken up into
2084          * many buffer descriptors (tx_start_bd,
2085          * tx_parse_bd, tx_data_bd).  In the best case,
2086          * there are always at least two BD's required
2087          * so we'll assume the best case here.
2088          */
2089         sc->tx_ring_size = (USABLE_TX_BD >> 1);
2090         sc->rx_ring_size = USABLE_RX_BD;
2091
2092         /* Assume receive IP/TCP/UDP checksum is enabled. */
2093         /* ToDo: Change when IOCTL changes checksum offload? */
2094         sc->rx_csum = 1;
2095
2096         /* Disable WoL. */
2097         sc->wol = 0;
2098
2099         /* Assume a standard 1500 byte MTU size for mbuf allocations. */
2100         sc->mbuf_alloc_size  = MCLBYTES;
2101
2102         /* Allocate DMA memory resources. */
2103         rc = bxe_host_structures_alloc(sc->dev);
2104         if (rc != 0) {
2105                 BXE_PRINTF("%s(%d): DMA memory allocation failed!\n",
2106                     __FILE__, __LINE__);
2107                 goto bxe_attach_fail;
2108         }
2109
2110         /* Allocate a FreeBSD ifnet structure. */
2111         ifp = sc->bxe_ifp = if_alloc(IFT_ETHER);
2112         if (ifp == NULL) {
2113                 BXE_PRINTF("%s(%d): Interface allocation failed!\n",
2114                     __FILE__, __LINE__);
2115                 rc = ENXIO;
2116                 goto bxe_attach_fail;
2117         }
2118
2119         /* Initialize the FreeBSD ifnet interface. */
2120         ifp->if_softc = sc;
2121         if_initname(ifp, device_get_name(dev), device_get_unit(dev));
2122
2123         /* Written by driver before attach, read-only afterwards. */
2124         ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
2125
2126         /* Driver entrypoints from the network interface. */
2127         ifp->if_ioctl = bxe_ioctl;
2128         ifp->if_start = bxe_tx_start;
2129 #if __FreeBSD_version >= 800000
2130         ifp->if_transmit = bxe_tx_mq_start;
2131         ifp->if_qflush   = bxe_mq_flush;
2132 #endif
2133
2134 #ifdef FreeBSD8_0
2135         ifp->if_timer = 0;
2136 #endif
2137
2138         ifp->if_init = bxe_init;
2139         ifp->if_hwassist = BXE_IF_HWASSIST;
2140         ifp->if_capabilities = BXE_IF_CAPABILITIES;
2141         /* TPA not enabled by default. */
2142         ifp->if_capenable = BXE_IF_CAPABILITIES & ~IFCAP_LRO;
2143         if_initbaudrate(ifp, IF_Gbps(10));
2144
2145         ifp->if_snd.ifq_drv_maxlen = sc->tx_ring_size;
2146
2147         IFQ_SET_MAXLEN(&ifp->if_snd, ifp->if_snd.ifq_drv_maxlen);
2148         IFQ_SET_READY(&ifp->if_snd);
2149
2150         /* Attach to the Ethernet interface list. */
2151         ether_ifattach(ifp, sc->link_params.mac_addr);
2152
2153         /* Attach the interrupts to the interrupt handlers. */
2154         rc = bxe_interrupt_attach(sc);
2155         if (rc != 0) {
2156                 BXE_PRINTF("%s(%d): Interrupt allocation failed!\n",
2157                     __FILE__, __LINE__);
2158                 goto bxe_attach_fail;
2159         }
2160
2161         /* Print important adapter info for the user. */
2162         bxe_print_adapter_info(sc);
2163
2164         /* Add the supported sysctls to the kernel. */
2165         bxe_add_sysctls(sc);
2166
2167 bxe_attach_fail:
2168         if (rc != 0)
2169                 bxe_detach(dev);
2170
2171         DBEXIT(BXE_INFO_LOAD | BXE_INFO_RESET);
2172         return (rc);
2173 }
2174
2175
2176 /*
2177  * Supported link settings.
2178  *
2179  * Examines hardware configuration present in NVRAM and
2180  * determines the link settings that are supported between
2181  * the external PHY and the switch.
2182  *
2183  * Returns:
2184  *   None.
2185  *
2186  * Side effects:
2187  *       Sets sc->port.supported
2188  *       Sets sc->link_params.phy_addr
2189  */
2190 static void
2191 bxe_link_settings_supported(struct bxe_softc *sc, uint32_t switch_cfg)
2192 {
2193         uint32_t ext_phy_type;
2194         int port;
2195
2196         DBENTER(BXE_VERBOSE_PHY);
2197         DBPRINT(sc, BXE_VERBOSE_PHY, "%s(): switch_cfg = 0x%08X\n",
2198                 __FUNCTION__, switch_cfg);
2199
2200         port = BP_PORT(sc);
2201         /* Get the link settings supported by the external PHY. */
2202         switch (switch_cfg) {
2203         case SWITCH_CFG_1G:
2204                 ext_phy_type =
2205                     SERDES_EXT_PHY_TYPE(sc->link_params.ext_phy_config);
2206
2207                 DBPRINT(sc, BXE_VERBOSE_PHY,
2208                     "%s(): 1G switch w/ ext_phy_type = "
2209                     "0x%08X\n", __FUNCTION__, ext_phy_type);
2210
2211                 switch (ext_phy_type) {
2212                 case PORT_HW_CFG_SERDES_EXT_PHY_TYPE_DIRECT:
2213                         DBPRINT(sc, BXE_VERBOSE_PHY, "%s(): 1G Direct.\n",
2214                             __FUNCTION__);
2215
2216                         sc->port.supported |=
2217                             (SUPPORTED_10baseT_Half |
2218                              SUPPORTED_10baseT_Full |
2219                              SUPPORTED_100baseT_Half |
2220                              SUPPORTED_100baseT_Full |
2221                              SUPPORTED_1000baseT_Full |
2222                              SUPPORTED_2500baseX_Full |
2223                              SUPPORTED_TP |
2224                              SUPPORTED_FIBRE |
2225                              SUPPORTED_Autoneg |
2226                              SUPPORTED_Pause |
2227                              SUPPORTED_Asym_Pause);
2228                         break;
2229
2230                 case PORT_HW_CFG_SERDES_EXT_PHY_TYPE_BCM5482:
2231                         DBPRINT(sc, BXE_VERBOSE_PHY, "%s(): 1G 5482\n",
2232                                 __FUNCTION__);
2233
2234                         sc->port.supported |=
2235                             (SUPPORTED_10baseT_Half |
2236                              SUPPORTED_10baseT_Full |
2237                              SUPPORTED_100baseT_Half |
2238                              SUPPORTED_100baseT_Full |
2239                              SUPPORTED_1000baseT_Full |
2240                              SUPPORTED_TP |
2241                              SUPPORTED_FIBRE |
2242                              SUPPORTED_Autoneg |
2243                              SUPPORTED_Pause |
2244                              SUPPORTED_Asym_Pause);
2245                         break;
2246
2247                 default:
2248                         BXE_PRINTF(
2249                             "%s(%d): Bad NVRAM 1Gb PHY configuration data "
2250                             "(ext_phy_config=0x%08X).\n",
2251                             __FILE__, __LINE__,
2252                             sc->link_params.ext_phy_config);
2253                         goto bxe_link_settings_supported_exit;
2254                 }
2255
2256                 sc->port.phy_addr =
2257                     REG_RD(sc, NIG_REG_SERDES0_CTRL_PHY_ADDR + (port * 0x10));
2258
2259                 DBPRINT(sc, BXE_VERBOSE_PHY, "%s(): phy_addr = 0x%08X\n",
2260                     __FUNCTION__, sc->port.phy_addr);
2261                 break;
2262
2263         case SWITCH_CFG_10G:
2264                 ext_phy_type =
2265                     XGXS_EXT_PHY_TYPE(sc->link_params.ext_phy_config);
2266
2267                 DBPRINT(
2268                     sc, BXE_VERBOSE_PHY,
2269                     "%s(): 10G switch w/ ext_phy_type = 0x%08X\n",
2270                     __FUNCTION__, ext_phy_type);
2271
2272                 switch (ext_phy_type) {
2273                 case PORT_HW_CFG_XGXS_EXT_PHY_TYPE_DIRECT:
2274                         DBPRINT(sc, BXE_VERBOSE_PHY,
2275                             "%s(): 10G switch w/ direct connect.\n",
2276                             __FUNCTION__);
2277
2278                         sc->port.supported |=
2279                             (SUPPORTED_10baseT_Half |
2280                              SUPPORTED_10baseT_Full |
2281                              SUPPORTED_100baseT_Half |
2282                              SUPPORTED_100baseT_Full |
2283                              SUPPORTED_1000baseT_Full |
2284                              SUPPORTED_2500baseX_Full |
2285                              SUPPORTED_10000baseT_Full |
2286                              SUPPORTED_TP |
2287                              SUPPORTED_FIBRE |
2288                              SUPPORTED_Autoneg |
2289                              SUPPORTED_Pause |
2290                              SUPPORTED_Asym_Pause);
2291                         break;
2292
2293                 case PORT_HW_CFG_XGXS_EXT_PHY_TYPE_BCM8072:
2294                         DBPRINT(sc, BXE_VERBOSE_PHY,
2295                             "ext_phy_type 0x%x (8072)\n",ext_phy_type);
2296
2297                         sc->port.supported |=
2298                             (SUPPORTED_10000baseT_Full |
2299                              SUPPORTED_1000baseT_Full |
2300                              SUPPORTED_FIBRE |
2301                              SUPPORTED_Autoneg |
2302                              SUPPORTED_Pause |
2303                              SUPPORTED_Asym_Pause);
2304                         break;
2305
2306                 case PORT_HW_CFG_XGXS_EXT_PHY_TYPE_BCM8073:
2307                         DBPRINT(sc,
2308                             BXE_VERBOSE_PHY,"ext_phy_type 0x%x (8073)\n",
2309                             ext_phy_type);
2310
2311                         sc->port.supported |=
2312                             (SUPPORTED_10000baseT_Full |
2313                              SUPPORTED_2500baseX_Full |
2314                              SUPPORTED_1000baseT_Full |
2315                              SUPPORTED_FIBRE |
2316                              SUPPORTED_Autoneg |
2317                              SUPPORTED_Pause |
2318                              SUPPORTED_Asym_Pause);
2319                         break;
2320
2321                 case PORT_HW_CFG_XGXS_EXT_PHY_TYPE_BCM8705:
2322                         DBPRINT(sc, BXE_VERBOSE_PHY,
2323                             "%s(): 10G switch w/ 8705.\n",__FUNCTION__);
2324
2325                         sc->port.supported |=
2326                             (SUPPORTED_10000baseT_Full |
2327                              SUPPORTED_FIBRE |
2328                              SUPPORTED_Pause |
2329                              SUPPORTED_Asym_Pause);
2330                         break;
2331
2332                 case PORT_HW_CFG_XGXS_EXT_PHY_TYPE_BCM8706:
2333                         DBPRINT(sc, BXE_VERBOSE_PHY,
2334                             "%s(): 10G switch w/ 8706.\n",
2335                             __FUNCTION__);
2336
2337                         sc->port.supported |=
2338                             (SUPPORTED_10000baseT_Full |
2339                              SUPPORTED_1000baseT_Full |
2340                              SUPPORTED_FIBRE |
2341                              SUPPORTED_Pause |
2342                              SUPPORTED_Asym_Pause);
2343                         break;
2344
2345                 case PORT_HW_CFG_XGXS_EXT_PHY_TYPE_BCM8726:
2346                         DBPRINT(sc, BXE_VERBOSE_PHY,
2347                             "%s(): 10G switch w/ 8726.\n",
2348                             __FUNCTION__);
2349
2350                         sc->port.supported |=
2351                             (SUPPORTED_10000baseT_Full |
2352                              SUPPORTED_FIBRE |
2353                              SUPPORTED_Pause |
2354                              SUPPORTED_Asym_Pause);
2355                         break;
2356
2357                 case PORT_HW_CFG_XGXS_EXT_PHY_TYPE_BCM8727:
2358                         DBPRINT(sc, BXE_VERBOSE_PHY,"ext_phy_type 0x%x (8727)\n",
2359                             ext_phy_type);
2360
2361                         sc->port.supported |=
2362                             (SUPPORTED_10000baseT_Full |
2363                              SUPPORTED_1000baseT_Full |
2364                              SUPPORTED_Autoneg |
2365                              SUPPORTED_FIBRE |
2366                              SUPPORTED_Pause |
2367                              SUPPORTED_Asym_Pause);
2368                         break;
2369
2370                 case PORT_HW_CFG_XGXS_EXT_PHY_TYPE_SFX7101:
2371                          DBPRINT(sc, BXE_VERBOSE_PHY,
2372                             "%s(): 10G switch w/ SFX7101.\n",
2373                             __FUNCTION__);
2374
2375                         sc->port.supported |=
2376                             (SUPPORTED_10000baseT_Full |
2377                              SUPPORTED_TP |
2378                              SUPPORTED_Autoneg |
2379                              SUPPORTED_Pause |
2380                              SUPPORTED_Asym_Pause);
2381                         break;
2382
2383                 case PORT_HW_CFG_XGXS_EXT_PHY_TYPE_BCM8481:
2384                          DBPRINT(sc, BXE_VERBOSE_PHY,
2385                             "ext_phy_type 0x%x (BCM8481)\n",
2386                             ext_phy_type);
2387
2388                         sc->port.supported |=
2389                             (SUPPORTED_10baseT_Half |
2390                              SUPPORTED_10baseT_Full |
2391                              SUPPORTED_100baseT_Half |
2392                              SUPPORTED_100baseT_Full |
2393                              SUPPORTED_1000baseT_Full |
2394                              SUPPORTED_10000baseT_Full |
2395                              SUPPORTED_TP |
2396                              SUPPORTED_Autoneg |
2397                              SUPPORTED_Pause |
2398                              SUPPORTED_Asym_Pause);
2399                         break;
2400
2401                 case PORT_HW_CFG_XGXS_EXT_PHY_TYPE_FAILURE:
2402                         DBPRINT(sc, BXE_WARN,
2403                             "%s(): 10G XGXS PHY failure detected.\n",
2404                             __FUNCTION__);
2405                         break;
2406
2407                         BXE_PRINTF(
2408                             "%s(%d): Bad NVRAM 10Gb PHY configuration data "
2409                             "(ext_phy_config=0x%08X).\n",
2410                             __FILE__, __LINE__,
2411                             sc->link_params.ext_phy_config);
2412                         goto bxe_link_settings_supported_exit;
2413                 }
2414
2415                 sc->port.phy_addr =
2416                     REG_RD(sc, NIG_REG_XGXS0_CTRL_PHY_ADDR +(port * 0x18));
2417                 break;
2418
2419         default:
2420                 DBPRINT(sc, BXE_WARN, "%s(): BAD switch configuration "
2421                     "(link_config = 0x%08X)\n", __FUNCTION__,
2422                     sc->port.link_config);
2423                 goto bxe_link_settings_supported_exit;
2424         }
2425
2426         sc->link_params.phy_addr = sc->port.phy_addr;
2427
2428         /* Mask out unsupported speeds according to NVRAM. */
2429         if ((sc->link_params.speed_cap_mask &
2430             PORT_HW_CFG_SPEED_CAPABILITY_D0_10M_HALF) == 0)
2431                 sc->port.supported &= ~SUPPORTED_10baseT_Half;
2432
2433         if ((sc->link_params.speed_cap_mask &
2434             PORT_HW_CFG_SPEED_CAPABILITY_D0_10M_FULL) == 0)
2435                 sc->port.supported &= ~SUPPORTED_10baseT_Full;
2436
2437         if ((sc->link_params.speed_cap_mask &
2438             PORT_HW_CFG_SPEED_CAPABILITY_D0_100M_HALF) == 0)
2439                 sc->port.supported &= ~SUPPORTED_100baseT_Half;
2440
2441         if ((sc->link_params.speed_cap_mask &
2442             PORT_HW_CFG_SPEED_CAPABILITY_D0_100M_FULL) == 0)
2443                 sc->port.supported &= ~SUPPORTED_100baseT_Full;
2444
2445         if ((sc->link_params.speed_cap_mask &
2446             PORT_HW_CFG_SPEED_CAPABILITY_D0_1G) == 0)
2447                 sc->port.supported &= ~(SUPPORTED_1000baseT_Half |
2448                         SUPPORTED_1000baseT_Full);
2449
2450         if ((sc->link_params.speed_cap_mask &
2451             PORT_HW_CFG_SPEED_CAPABILITY_D0_2_5G) == 0)
2452                 sc->port.supported &= ~SUPPORTED_2500baseX_Full;
2453
2454         if ((sc->link_params.speed_cap_mask &
2455             PORT_HW_CFG_SPEED_CAPABILITY_D0_10G) == 0)
2456                 sc->port.supported &= ~SUPPORTED_10000baseT_Full;
2457
2458         DBPRINT(sc, BXE_VERBOSE_PHY,
2459             "%s(): Supported link settings = 0x%b\n", __FUNCTION__,
2460             sc->port.supported, BXE_SUPPORTED_PRINTFB);
2461
2462 bxe_link_settings_supported_exit:
2463
2464         DBEXIT(BXE_VERBOSE_PHY);
2465 }
2466
2467 /*
2468  * Requested link settings.
2469  *
2470  * Returns:
2471  *   None.
2472  */
2473 static void
2474 bxe_link_settings_requested(struct bxe_softc *sc)
2475 {
2476         uint32_t ext_phy_type;
2477         DBENTER(BXE_VERBOSE_PHY);
2478
2479         sc->link_params.req_duplex = MEDIUM_FULL_DUPLEX;
2480
2481         switch (sc->port.link_config & PORT_FEATURE_LINK_SPEED_MASK) {
2482
2483         case PORT_FEATURE_LINK_SPEED_AUTO:
2484                 if (sc->port.supported & SUPPORTED_Autoneg) {
2485                         sc->link_params.req_line_speed |= SPEED_AUTO_NEG;
2486                         sc->port.advertising = sc->port.supported;
2487                 } else {
2488                         ext_phy_type = XGXS_EXT_PHY_TYPE(
2489                             sc->link_params.ext_phy_config);
2490
2491                         if ((ext_phy_type ==
2492                              PORT_HW_CFG_XGXS_EXT_PHY_TYPE_BCM8705) ||
2493                             (ext_phy_type ==
2494                              PORT_HW_CFG_XGXS_EXT_PHY_TYPE_BCM8706)) {
2495                                 /* Force 10G, no autonegotiation. */
2496                                 sc->link_params.req_line_speed = SPEED_10000;
2497                                 sc->port.advertising =
2498                                     ADVERTISED_10000baseT_Full |
2499                                     ADVERTISED_FIBRE;
2500                                 break;
2501                         }
2502
2503                         DBPRINT(sc, BXE_FATAL,
2504                             "%s(): NVRAM config error. Invalid "
2505                             "link_config (0x%08X) - Autoneg not supported!\n",
2506                             __FUNCTION__, sc->port.link_config);
2507                         goto bxe_link_settings_requested_exit;
2508                 }
2509                 break;
2510         case PORT_FEATURE_LINK_SPEED_10M_FULL:
2511                 if (sc->port.supported & SUPPORTED_10baseT_Full) {
2512                         sc->link_params.req_line_speed = SPEED_10;
2513                         sc->port.advertising = ADVERTISED_10baseT_Full |
2514                                 ADVERTISED_TP;
2515                 } else {
2516                         DBPRINT(sc, BXE_FATAL,
2517                             "%s(): NVRAM config error. Invalid "
2518                             "link_config (0x%08X) - speed_cap_mask 0x%08X\n",
2519                             __FUNCTION__, sc->port.link_config,
2520                             sc->link_params.speed_cap_mask);
2521                         goto bxe_link_settings_requested_exit;
2522                 }
2523                 break;
2524         case PORT_FEATURE_LINK_SPEED_10M_HALF:
2525                 if (sc->port.supported & SUPPORTED_10baseT_Half) {
2526                         sc->link_params.req_line_speed = SPEED_10;
2527                         sc->link_params.req_duplex = MEDIUM_HALF_DUPLEX;
2528                         sc->port.advertising = ADVERTISED_10baseT_Half |
2529                                 ADVERTISED_TP;
2530                 } else {
2531                         DBPRINT(sc, BXE_FATAL,
2532                             "%s(): NVRAM config error. Invalid "
2533                             "link_config (0x%08X) - speed_cap_mask = 0x%08X\n",
2534                             __FUNCTION__, sc->port.link_config,
2535                             sc->link_params.speed_cap_mask);
2536                         goto bxe_link_settings_requested_exit;
2537                 }
2538                 break;
2539         case PORT_FEATURE_LINK_SPEED_100M_FULL:
2540                 if (sc->port.supported & SUPPORTED_100baseT_Full) {
2541                         sc->link_params.req_line_speed = SPEED_100;
2542                         sc->port.advertising = ADVERTISED_100baseT_Full |
2543                                 ADVERTISED_TP;
2544                 } else {
2545                         DBPRINT(sc, BXE_FATAL,
2546                             "%s(): NVRAM config error. Invalid "
2547                             "link_config (0x%08X) - speed_cap_mask = 0x%08X\n",
2548                             __FUNCTION__, sc->port.link_config,
2549                             sc->link_params.speed_cap_mask);
2550                         goto bxe_link_settings_requested_exit;
2551                 }
2552                 break;
2553         case PORT_FEATURE_LINK_SPEED_100M_HALF:
2554                 if (sc->port.supported & SUPPORTED_100baseT_Half) {
2555                         sc->link_params.req_line_speed = SPEED_100;
2556                         sc->link_params.req_duplex = MEDIUM_HALF_DUPLEX;
2557                         sc->port.advertising = ADVERTISED_100baseT_Half |
2558                                 ADVERTISED_TP;
2559                 } else {
2560                         DBPRINT(sc, BXE_FATAL,
2561                             "%s(): NVRAM config error. Invalid "
2562                             "link_config (0x%08X) - speed_cap_mask = 0x%08X\n",
2563                             __FUNCTION__, sc->port.link_config,
2564                             sc->link_params.speed_cap_mask);
2565                         goto bxe_link_settings_requested_exit;
2566                 }
2567                 break;
2568         case PORT_FEATURE_LINK_SPEED_1G:
2569                 if (sc->port.supported & SUPPORTED_1000baseT_Full) {
2570                         sc->link_params.req_line_speed = SPEED_1000;
2571                         sc->port.advertising = ADVERTISED_1000baseT_Full |
2572                                 ADVERTISED_TP;
2573                 } else {
2574                         DBPRINT(sc, BXE_FATAL,
2575                             "%s(): NVRAM config error. Invalid "
2576                             "link_config (0x%08X) - speed_cap_mask = 0x%08X\n",
2577                             __FUNCTION__, sc->port.link_config,
2578                             sc->link_params.speed_cap_mask);
2579                         goto bxe_link_settings_requested_exit;
2580                 }
2581                 break;
2582         case PORT_FEATURE_LINK_SPEED_2_5G:
2583                 if (sc->port.supported & SUPPORTED_2500baseX_Full) {
2584                         sc->link_params.req_line_speed = SPEED_2500;
2585                         sc->port.advertising = ADVERTISED_2500baseX_Full |
2586                                 ADVERTISED_TP;
2587                 } else {
2588                         DBPRINT(sc, BXE_FATAL,
2589                             "%s(): NVRAM config error. Invalid "
2590                             "link_config (0x%08X) - speed_cap_mask = 0x%08X\n",
2591                             __FUNCTION__, sc->port.link_config,
2592                             sc->link_params.speed_cap_mask);
2593                         goto bxe_link_settings_requested_exit;
2594                 }
2595                 break;
2596         case PORT_FEATURE_LINK_SPEED_10G_CX4:
2597         case PORT_FEATURE_LINK_SPEED_10G_KX4:
2598         case PORT_FEATURE_LINK_SPEED_10G_KR:
2599                 if (sc->port.supported & SUPPORTED_10000baseT_Full) {
2600                         sc->link_params.req_line_speed = SPEED_10000;
2601                         sc->port.advertising = ADVERTISED_10000baseT_Full |
2602                             ADVERTISED_FIBRE;
2603                 } else {
2604                         DBPRINT(sc, BXE_FATAL,
2605                             "%s(): NVRAM config error. Invalid "
2606                             "link_config (0x%08X) - speed_cap_mask = 0x%08X\n",
2607                             __FUNCTION__, sc->port.link_config,
2608                             sc->link_params.speed_cap_mask);
2609                         goto bxe_link_settings_requested_exit;
2610                 }
2611                 break;
2612         default:
2613                 DBPRINT(sc, BXE_FATAL, "%s(): NVRAM config error. BAD link "
2614                     "speed - link_config = 0x%08X\n", __FUNCTION__,
2615                     sc->port.link_config);
2616                 sc->link_params.req_line_speed = 0;
2617                 sc->port.advertising = sc->port.supported;
2618                 break;
2619         }
2620
2621         DBPRINT(sc, BXE_VERBOSE_PHY,
2622             "%s(): req_line_speed = %d, req_duplex = %d\n",
2623             __FUNCTION__, sc->link_params.req_line_speed,
2624             sc->link_params.req_duplex);
2625
2626         sc->link_params.req_flow_ctrl =
2627             sc->port.link_config & PORT_FEATURE_FLOW_CONTROL_MASK;
2628
2629         if ((sc->link_params.req_flow_ctrl == FLOW_CTRL_AUTO) &&
2630             !(sc->port.supported & SUPPORTED_Autoneg))
2631                 sc->link_params.req_flow_ctrl = FLOW_CTRL_NONE;
2632
2633         DBPRINT(sc, BXE_VERBOSE_PHY,
2634                 "%s(): req_flow_ctrl = 0x%08X, advertising = 0x%08X\n",
2635                 __FUNCTION__, sc->link_params.req_flow_ctrl,
2636                 sc->port.advertising);
2637
2638 bxe_link_settings_requested_exit:
2639
2640         DBEXIT(BXE_VERBOSE_PHY);
2641 }
2642
2643
2644 /*
2645  * Get function specific hardware configuration.
2646  *
2647  * Multiple function devices such as the BCM57711E have configuration
2648  * information that is specific to each PCIe function of the controller.
2649  * The number of PCIe functions is not necessarily the same as the number
2650  * of Ethernet ports supported by the device.
2651  *
2652  * Returns:
2653  *   0 = Success, !0 = Failure
2654  */
2655 static int
2656 bxe_hwinfo_function_get(struct bxe_softc *sc)
2657 {
2658         uint32_t mac_hi, mac_lo, val;
2659         int func, rc;
2660
2661         DBENTER(BXE_VERBOSE_LOAD);
2662
2663         rc = 0;
2664         func = BP_FUNC(sc);
2665
2666         /* Get the common hardware configuration first. */
2667         bxe_hwinfo_common_get(sc);
2668
2669         /* Assume no outer VLAN/multi-function support. */
2670         sc->e1hov = sc->e1hmf = 0;
2671
2672         /* Get config info for mf enabled devices. */
2673         if (CHIP_IS_E1H(sc)) {
2674                 sc->mf_config[BP_E1HVN(sc)] =
2675                     SHMEM_RD(sc, mf_cfg.func_mf_config[func].config);
2676                 val = (SHMEM_RD(sc, mf_cfg.func_mf_config[func].e1hov_tag) &
2677                     FUNC_MF_CFG_E1HOV_TAG_MASK);
2678                 if (val != FUNC_MF_CFG_E1HOV_TAG_DEFAULT) {
2679                         sc->e1hov = (uint16_t) val;
2680                         sc->e1hmf = 1;
2681                 } else {
2682                         if (BP_E1HVN(sc)) {
2683                                 rc = EPERM;
2684                                 goto bxe_hwinfo_function_get_exit;
2685                         }
2686                 }
2687         }
2688
2689         if (!NOMCP(sc)) {
2690                 bxe_hwinfo_port_get(sc);
2691                 sc->fw_seq = SHMEM_RD(sc, func_mb[func].drv_mb_header) &
2692                     DRV_MSG_SEQ_NUMBER_MASK;
2693         }
2694
2695
2696         /*
2697          * Fetch the factory configured MAC address for multi function
2698          * devices. If this is not a multi-function device then the MAC
2699          * address was already read in the bxe_hwinfo_port_get() routine.
2700          * The MAC addresses used by the port are not the same as the MAC
2701          * addressed used by the function.
2702          */
2703         if (IS_E1HMF(sc)) {
2704                 mac_hi = SHMEM_RD(sc, mf_cfg.func_mf_config[func].mac_upper);
2705                 mac_lo = SHMEM_RD(sc, mf_cfg.func_mf_config[func].mac_lower);
2706
2707                 if ((mac_lo == 0) && (mac_hi == 0)) {
2708                         BXE_PRINTF("%s(%d): Invalid Ethernet address!\n",
2709                             __FILE__, __LINE__);
2710                         rc = ENODEV;
2711                 } else {
2712                         sc->link_params.mac_addr[0] = (u_char)(mac_hi >> 8);
2713                         sc->link_params.mac_addr[1] = (u_char)(mac_hi);
2714                         sc->link_params.mac_addr[2] = (u_char)(mac_lo >> 24);
2715                         sc->link_params.mac_addr[3] = (u_char)(mac_lo >> 16);
2716                         sc->link_params.mac_addr[4] = (u_char)(mac_lo >> 8);
2717                         sc->link_params.mac_addr[5] = (u_char)(mac_lo);
2718                 }
2719         }
2720
2721
2722 bxe_hwinfo_function_get_exit:
2723         DBEXIT(BXE_VERBOSE_LOAD);
2724         return (rc);
2725 }
2726
2727
2728 /*
2729  * Get port specific hardware configuration.
2730  *
2731  * Multiple port devices such as the BCM57710 have configuration
2732  * information that is specific to each Ethernet port of the
2733  * controller.  This function reads that configuration
2734  * information from the bootcode's shared memory and saves it
2735  * for future use.
2736  *
2737  * Returns:
2738  *   0 = Success, !0 = Failure
2739  */
2740 static int
2741 bxe_hwinfo_port_get(struct bxe_softc *sc)
2742 {
2743         int i, port, rc;
2744         uint32_t val, mac_hi, mac_lo;
2745
2746         DBENTER(BXE_VERBOSE_LOAD);
2747         rc = 0;
2748
2749         port = BP_PORT(sc);
2750         sc->link_params.sc = sc;
2751         sc->link_params.port = port;
2752
2753         /* Fetch several configuration values from bootcode shared memory. */
2754         sc->link_params.lane_config =
2755             SHMEM_RD(sc, dev_info.port_hw_config[port].lane_config);
2756         sc->link_params.ext_phy_config =
2757             SHMEM_RD(sc, dev_info.port_hw_config[port].external_phy_config);
2758
2759         if (XGXS_EXT_PHY_TYPE(sc->link_params.ext_phy_config) ==
2760             PORT_HW_CFG_XGXS_EXT_PHY_TYPE_BCM8727_NOC) {
2761                 sc->link_params.ext_phy_config &=
2762                     ~PORT_HW_CFG_XGXS_EXT_PHY_TYPE_MASK;
2763                 sc->link_params.ext_phy_config |=
2764                     PORT_HW_CFG_XGXS_EXT_PHY_TYPE_BCM8727;
2765                 sc->link_params.feature_config_flags |=
2766                     FEATURE_CONFIG_BCM8727_NOC;
2767         }
2768
2769         sc->link_params.speed_cap_mask =
2770             SHMEM_RD(sc, dev_info.port_hw_config[port].speed_capability_mask);
2771         sc->port.link_config =
2772             SHMEM_RD(sc, dev_info.port_feature_config[port].link_config);
2773
2774
2775         /* Read the XGXS RX/TX preemphasis values. */
2776         for (i = 0; i < 2; i++) {
2777                 val = SHMEM_RD(sc,
2778                     dev_info.port_hw_config[port].xgxs_config_rx[i<<1]);
2779                 sc->link_params.xgxs_config_rx[i << 1] = ((val >> 16) & 0xffff);
2780                 sc->link_params.xgxs_config_rx[(i << 1) + 1] = (val & 0xffff);
2781
2782                 val = SHMEM_RD(sc,
2783                     dev_info.port_hw_config[port].xgxs_config_tx[i<<1]);
2784                 sc->link_params.xgxs_config_tx[i << 1] = ((val >> 16) & 0xffff);
2785                 sc->link_params.xgxs_config_tx[(i << 1) + 1] = (val & 0xffff);
2786         }
2787
2788         /* Fetch the device configured link settings. */
2789         sc->link_params.switch_cfg = sc->port.link_config &
2790             PORT_FEATURE_CONNECTED_SWITCH_MASK;
2791
2792         bxe_link_settings_supported(sc, sc->link_params.switch_cfg);
2793         bxe_link_settings_requested(sc);
2794
2795         mac_hi = SHMEM_RD(sc, dev_info.port_hw_config[port].mac_upper);
2796         mac_lo = SHMEM_RD(sc, dev_info.port_hw_config[port].mac_lower);
2797
2798         if (mac_lo == 0 && mac_hi == 0) {
2799                 BXE_PRINTF("%s(%d): No Ethernet address programmed on the "
2800                     "controller!\n", __FILE__, __LINE__);
2801                 rc = ENODEV;
2802         } else {
2803                 sc->link_params.mac_addr[0] = (u_char)(mac_hi >> 8);
2804                 sc->link_params.mac_addr[1] = (u_char)(mac_hi);
2805                 sc->link_params.mac_addr[2] = (u_char)(mac_lo >> 24);
2806                 sc->link_params.mac_addr[3] = (u_char)(mac_lo >> 16);
2807                 sc->link_params.mac_addr[4] = (u_char)(mac_lo >> 8);
2808                 sc->link_params.mac_addr[5] = (u_char)(mac_lo);
2809         }
2810
2811         DBEXIT(BXE_VERBOSE_LOAD);
2812         return (rc);
2813 }
2814
2815
2816 /*
2817  * Get common hardware configuration.
2818  *
2819  * Multiple port devices such as the BCM57710 have configuration
2820  * information that is shared between all ports of the Ethernet
2821  * controller.  This function reads that configuration
2822  * information from the bootcode's shared memory and saves it
2823  * for future use.
2824  *
2825  * Returns:
2826  *   0 = Success, !0 = Failure
2827  */
2828 static int
2829 bxe_hwinfo_common_get(struct bxe_softc *sc)
2830 {
2831         uint32_t val;
2832         int rc;
2833
2834         DBENTER(BXE_VERBOSE_LOAD);
2835         rc = 0;
2836
2837         /* Get the chip revision. */
2838         sc->common.chip_id = sc->link_params.chip_id =
2839             ((REG_RD(sc, MISC_REG_CHIP_NUM) & 0xffff) << 16) |
2840             ((REG_RD(sc, MISC_REG_CHIP_REV) & 0x000f) << 12) |
2841             ((REG_RD(sc, MISC_REG_CHIP_METAL) & 0xff) << 4) |
2842             ((REG_RD(sc, MISC_REG_BOND_ID) & 0xf));
2843
2844         DBPRINT(sc, BXE_VERBOSE_LOAD, "%s(): chip_id = 0x%08X.\n",
2845             __FUNCTION__, sc->common.chip_id);
2846
2847         val = (REG_RD(sc, 0x2874) & 0x55);
2848         if ((sc->common.chip_id & 0x1) ||
2849             (CHIP_IS_E1(sc) && val) || (CHIP_IS_E1H(sc) && (val == 0x55))) {
2850                 sc->bxe_flags |= BXE_ONE_PORT_FLAG;
2851                 DBPRINT(sc, BXE_VERBOSE_LOAD, "%s(): Single port device.\n",
2852                     __FUNCTION__);
2853         }
2854
2855         /* Identify enabled PCI capabilites (PCIe, MSI-X, etc.). */
2856         bxe_probe_pci_caps(sc);
2857
2858         /* Get the NVRAM size. */
2859         val = REG_RD(sc, MCP_REG_MCPR_NVM_CFG4);
2860         sc->common.flash_size = (NVRAM_1MB_SIZE <<
2861             (val & MCPR_NVM_CFG4_FLASH_SIZE));
2862
2863         DBPRINT(sc, BXE_VERBOSE_LOAD, "%s(): flash_size = 0x%08x (%dKB)\n",
2864             __FUNCTION__, sc->common.flash_size,(sc->common.flash_size >> 10));
2865
2866         /* Find the shared memory base address. */
2867         sc->common.shmem_base = sc->link_params.shmem_base =
2868             REG_RD(sc, MISC_REG_SHARED_MEM_ADDR);
2869         sc->common.shmem2_base = REG_RD(sc, MISC_REG_GENERIC_CR_0);
2870         DBPRINT(sc, BXE_VERBOSE_LOAD, "%s(): shmem_base = 0x%08X\n",
2871             __FUNCTION__, sc->common.shmem_base);
2872
2873         /* Make sure the shared memory address is valid. */
2874         if (!sc->common.shmem_base ||
2875             (sc->common.shmem_base < 0xA0000) ||
2876             (sc->common.shmem_base > 0xC0000)) {
2877
2878                 BXE_PRINTF("%s(%d): MCP is not active!\n",
2879                     __FILE__, __LINE__);
2880                 /* ToDo: Remove the NOMCP support. */
2881                 sc->bxe_flags |= BXE_NO_MCP_FLAG;
2882                 rc = ENODEV;
2883                 goto bxe_hwinfo_common_get_exit;
2884         }
2885
2886         /* Make sure the shared memory contents are valid. */
2887         val = SHMEM_RD(sc, validity_map[BP_PORT(sc)]);
2888         if ((val & (SHR_MEM_VALIDITY_DEV_INFO | SHR_MEM_VALIDITY_MB)) !=
2889             (SHR_MEM_VALIDITY_DEV_INFO | SHR_MEM_VALIDITY_MB)) {
2890                 BXE_PRINTF("%s(%d): Invalid NVRAM! Bad validity "
2891                     "signature.\n", __FILE__, __LINE__);
2892                 rc = ENODEV;
2893                 goto bxe_hwinfo_common_get_exit;
2894         }
2895
2896         /* Read the device configuration from shared memory. */
2897         sc->common.hw_config =
2898             SHMEM_RD(sc, dev_info.shared_hw_config.config);
2899         sc->link_params.hw_led_mode = ((sc->common.hw_config &
2900             SHARED_HW_CFG_LED_MODE_MASK) >> SHARED_HW_CFG_LED_MODE_SHIFT);
2901
2902         /* Check if we need to override the preemphasis values. */
2903         sc->link_params.feature_config_flags = 0;
2904         val = SHMEM_RD(sc, dev_info.shared_feature_config.config);
2905         if (val & SHARED_FEAT_CFG_OVERRIDE_PREEMPHASIS_CFG_ENABLED)
2906                 sc->link_params.feature_config_flags |=
2907                     FEATURE_CONFIG_OVERRIDE_PREEMPHASIS_ENABLED;
2908         else
2909                 sc->link_params.feature_config_flags &=
2910                     ~FEATURE_CONFIG_OVERRIDE_PREEMPHASIS_ENABLED;
2911
2912         /* In multifunction mode, we can't support WoL on a VN. */
2913         if (BP_E1HVN(sc) == 0) {
2914                 val = REG_RD(sc, PCICFG_OFFSET + PCICFG_PM_CAPABILITY);
2915                 sc->bxe_flags |= (val & PCICFG_PM_CAPABILITY_PME_IN_D3_COLD) ?
2916                         0 : BXE_NO_WOL_FLAG;
2917         } else
2918                 sc->bxe_flags |= BXE_NO_WOL_FLAG;
2919
2920         DBPRINT(sc, BXE_VERBOSE_LOAD, "%s(): %sWoL capable\n", __FUNCTION__,
2921                 (sc->bxe_flags & BXE_NO_WOL_FLAG) ? "Not " : "");
2922
2923         /* Check bootcode version */
2924         sc->common.bc_ver = ((SHMEM_RD(sc, dev_info.bc_rev)) >> 8);
2925         if (sc->common.bc_ver < MIN_BXE_BC_VER) {
2926                 BXE_PRINTF("%s(%d): Warning: This driver needs bootcode "
2927                     "0x%08X but found 0x%08X, please upgrade!\n",
2928                     __FILE__, __LINE__, MIN_BXE_BC_VER, sc->common.bc_ver);
2929                 rc = ENODEV;
2930                 goto bxe_hwinfo_common_get_exit;
2931         }
2932
2933 bxe_hwinfo_common_get_exit:
2934         DBEXIT(BXE_VERBOSE_LOAD);
2935         return (rc);
2936 }
2937
2938
2939 /*
2940  * Remove traces of PXE boot by forcing UNDI driver unload.
2941  *
2942  * Returns:
2943  *   None.
2944  */
2945 static void
2946 bxe_undi_unload(struct bxe_softc *sc)
2947 {
2948         uint32_t reset_code, swap_en, swap_val, val;
2949         int func;
2950
2951         DBENTER(BXE_VERBOSE_LOAD);
2952
2953         /* Check if there is any driver already loaded */
2954         val = REG_RD(sc, MISC_REG_UNPREPARED);
2955         if (val == 0x1) {
2956
2957                 /* Check if it is the UNDI driver. */
2958                 bxe_acquire_hw_lock(sc, HW_LOCK_RESOURCE_UNDI);
2959                 val = REG_RD(sc, DORQ_REG_NORM_CID_OFST);
2960                 if (val == 0x7) {
2961                         reset_code = DRV_MSG_CODE_UNLOAD_REQ_WOL_DIS;
2962                         func = BP_FUNC(sc);
2963
2964                         DBPRINT(sc, BXE_WARN,
2965                             "%s(): UNDI is active! Resetting the device.\n",
2966                             __FUNCTION__);
2967
2968                         /* Clear the UNDI indication. */
2969                         REG_WR(sc, DORQ_REG_NORM_CID_OFST, 0);
2970
2971                         /* Try to unload UNDI on port 0. */
2972                         sc->bxe_func = 0;
2973                         sc->fw_seq = (SHMEM_RD(sc,
2974                             func_mb[sc->bxe_func].drv_mb_header) &
2975                             DRV_MSG_SEQ_NUMBER_MASK);
2976                         reset_code = bxe_fw_command(sc, reset_code);
2977
2978                         /* Check if UNDI is active on port 1. */
2979                         if (reset_code != FW_MSG_CODE_DRV_UNLOAD_COMMON) {
2980
2981                                 /* Send "done" for previous unload. */
2982                                 bxe_fw_command(sc, DRV_MSG_CODE_UNLOAD_DONE);
2983
2984                                 /* Now unload on port 1. */
2985                                 sc->bxe_func = 1;
2986                                 sc->fw_seq = (SHMEM_RD(sc,
2987                                     func_mb[sc->bxe_func].drv_mb_header) &
2988                                     DRV_MSG_SEQ_NUMBER_MASK);
2989
2990                                 reset_code = DRV_MSG_CODE_UNLOAD_REQ_WOL_DIS;
2991                                 bxe_fw_command(sc, reset_code);
2992                         }
2993
2994                         /* It's now safe to release the lock. */
2995                         bxe_release_hw_lock(sc, HW_LOCK_RESOURCE_UNDI);
2996
2997                         REG_WR(sc, (BP_PORT(sc) ? HC_REG_CONFIG_1 :
2998                                 HC_REG_CONFIG_0), 0x1000);
2999
3000                         REG_WR(sc, (BP_PORT(sc) ?
3001                             NIG_REG_LLH1_BRB1_DRV_MASK :
3002                             NIG_REG_LLH0_BRB1_DRV_MASK), 0x0);
3003
3004                         REG_WR(sc, (BP_PORT(sc) ?
3005                             NIG_REG_LLH1_BRB1_NOT_MCP :
3006                             NIG_REG_LLH0_BRB1_NOT_MCP), 0x0);
3007
3008                         /* Clear AEU. */
3009                         REG_WR(sc, (BP_PORT(sc) ?
3010                             MISC_REG_AEU_MASK_ATTN_FUNC_1 :
3011                             MISC_REG_AEU_MASK_ATTN_FUNC_0), 0);
3012
3013                         DELAY(10000);
3014
3015                         /* Save NIG port swap information. */
3016                         swap_val = REG_RD(sc, NIG_REG_PORT_SWAP);
3017                         swap_en = REG_RD(sc, NIG_REG_STRAP_OVERRIDE);
3018
3019                         /* Reset the controller. */
3020                         REG_WR(sc, GRCBASE_MISC +
3021                             MISC_REGISTERS_RESET_REG_1_CLEAR, 0xd3ffffff);
3022                         REG_WR(sc, GRCBASE_MISC +
3023                             MISC_REGISTERS_RESET_REG_2_CLEAR, 0x00001403);
3024
3025                         /* Take the NIG out of reset and restore swap values.*/
3026                         REG_WR(sc, GRCBASE_MISC +
3027                             MISC_REGISTERS_RESET_REG_1_SET,
3028                             MISC_REGISTERS_RESET_REG_1_RST_NIG);
3029                         REG_WR(sc, NIG_REG_PORT_SWAP, swap_val);
3030                         REG_WR(sc, NIG_REG_STRAP_OVERRIDE, swap_en);
3031
3032                         /* Send completion message to the MCP. */
3033                         bxe_fw_command(sc, DRV_MSG_CODE_UNLOAD_DONE);
3034
3035                         /*
3036                          * Restore our function and firmware sequence counter.
3037                          */
3038                         sc->bxe_func = func;
3039                         sc->fw_seq = (SHMEM_RD(sc,
3040                             func_mb[sc->bxe_func].drv_mb_header) &
3041                             DRV_MSG_SEQ_NUMBER_MASK);
3042                 } else
3043                         bxe_release_hw_lock(sc, HW_LOCK_RESOURCE_UNDI);
3044         }
3045
3046         DBEXIT(BXE_VERBOSE_LOAD);
3047 }
3048
3049
3050 /*
3051  * Device detach function.
3052  *
3053  * Stops the controller, resets the controller, and releases resources.
3054  *
3055  * Returns:
3056  *   0 on success, !0 = failure.
3057  */
3058 static int
3059 bxe_detach(device_t dev)
3060 {
3061         struct bxe_softc *sc;
3062         struct ifnet *ifp;
3063         int rc;
3064
3065         sc = device_get_softc(dev);
3066         DBENTER(BXE_INFO_UNLOAD);
3067
3068         rc = 0;
3069
3070         ifp = sc->bxe_ifp;
3071         if (ifp != NULL && ifp->if_vlantrunk != NULL) {
3072                 BXE_PRINTF("%s(%d): Cannot detach while VLANs are in use.\n",
3073                     __FILE__, __LINE__);
3074                 rc = EBUSY;
3075                 goto bxe_detach_exit;
3076         }
3077
3078         /* Stop and reset the controller if it was open. */
3079         if (sc->state != BXE_STATE_CLOSED) {
3080                 BXE_CORE_LOCK(sc);
3081                 rc = bxe_stop_locked(sc, UNLOAD_CLOSE);
3082                 BXE_CORE_UNLOCK(sc);
3083         }
3084
3085 #ifdef BXE_DEBUG
3086         /* Free memory buffer for grcdump output.*/
3087         if (sc->grcdump_buffer != NULL)
3088                 free(sc->grcdump_buffer, M_TEMP);
3089 #endif
3090
3091         /* Clean-up any remaining interrupt resources. */
3092         bxe_interrupt_detach(sc);
3093         bxe_interrupt_free(sc);
3094
3095         /* Release the network interface. */
3096         if (ifp != NULL)
3097                 ether_ifdetach(ifp);
3098         ifmedia_removeall(&sc->bxe_ifmedia);
3099
3100         /* Release all remaining resources. */
3101         bxe_release_resources(sc);
3102
3103         /* Free all PCI resources. */
3104         bxe_pci_resources_free(sc);
3105         pci_disable_busmaster(dev);
3106
3107         bxe_mutexes_free(sc);
3108
3109 bxe_detach_exit:
3110         DBEXIT(BXE_INFO_UNLOAD);
3111         return(0);
3112 }
3113
3114
3115 /*
3116  * Setup a leading connection for the controller.
3117  *
3118  * Returns:
3119  *   0 = Success, !0 = Failure.
3120  */
3121 static int
3122 bxe_setup_leading(struct bxe_softc *sc)
3123 {
3124         int rc;
3125
3126         DBENTER(BXE_VERBOSE_LOAD | BXE_VERBOSE_RESET | BXE_VERBOSE_RAMROD);
3127
3128         DBPRINT(sc, BXE_VERBOSE_LOAD, "%s(): Setup leading connection "
3129             "on fp[00].\n", __FUNCTION__);
3130
3131         /* Reset IGU state for the leading connection. */
3132         bxe_ack_sb(sc, sc->fp[0].sb_id, CSTORM_ID, 0, IGU_INT_ENABLE, 0);
3133
3134         /* Post a PORT_SETUP ramrod and wait for completion. */
3135         bxe_sp_post(sc, RAMROD_CMD_ID_ETH_PORT_SETUP, 0, 0, 0, 0);
3136
3137         /* Wait for the ramrod to complete on the leading connection. */
3138         rc = bxe_wait_ramrod(sc, BXE_STATE_OPEN, 0, &(sc->state), 1);
3139
3140         DBEXIT(BXE_VERBOSE_LOAD | BXE_VERBOSE_RESET | BXE_VERBOSE_RAMROD);
3141         return (rc);
3142 }
3143
3144
3145 /*
3146  * Stop the leading connection on the controller.
3147  *
3148  * Returns:
3149  *   None.
3150  */
3151 static int
3152 bxe_stop_leading(struct bxe_softc *sc)
3153 {
3154         uint16_t dsb_sp_prod_idx;
3155         int rc, timeout;
3156
3157         DBPRINT(sc, (BXE_VERBOSE_LOAD | BXE_VERBOSE_RESET |
3158             BXE_VERBOSE_UNLOAD), "%s(): Stop client connection "
3159             "on fp[00].\n", __FUNCTION__);
3160
3161         /* Send the ETH_HALT ramrod. */
3162         sc->fp[0].state = BXE_FP_STATE_HALTING;
3163         bxe_sp_post(sc,RAMROD_CMD_ID_ETH_HALT, 0, 0, sc->fp[0].cl_id, 0);
3164
3165         /* Poll for the ETH_HALT ramrod on the leading connection. */
3166         rc = bxe_wait_ramrod(sc, BXE_FP_STATE_HALTED,
3167             0, &(sc->fp[0].state), 1);
3168         if (rc) {
3169                 DBPRINT(sc, BXE_FATAL, "%s(): Timeout waiting for "
3170                     "STATE_HALTED ramrod completion!\n", __FUNCTION__);
3171                 goto bxe_stop_leading_exit;
3172         }
3173
3174         /* Get the default status block SP producer index. */
3175         dsb_sp_prod_idx = *sc->dsb_sp_prod;
3176
3177         /* After HALT we send PORT_DELETE ramrod. */
3178         bxe_sp_post(sc, RAMROD_CMD_ID_ETH_PORT_DEL, 0, 0, 0, 1);
3179
3180         /* Be patient but don't wait forever. */
3181         timeout = 500;
3182         while (dsb_sp_prod_idx == *sc->dsb_sp_prod) {
3183                 if (timeout == 0) {
3184                         DBPRINT(sc, BXE_FATAL, "%s(): Timeout waiting for "
3185                             "PORT_DEL ramrod completion!\n", __FUNCTION__);
3186                         rc = EBUSY;
3187                         break;
3188                 }
3189                 timeout--;
3190                 DELAY(1000);
3191                 rmb();
3192         }
3193
3194         /* Update the adapter and connection states. */
3195         sc->state = BXE_STATE_CLOSING_WAIT4_UNLOAD;
3196         sc->fp[0].state = BXE_FP_STATE_CLOSED;
3197
3198 bxe_stop_leading_exit:
3199         return (rc);
3200 }
3201
3202 /*
3203  * Setup a client connection when using multi-queue/RSS.
3204  *
3205  * Returns:
3206  *   Nothing.
3207  */
3208 static int
3209 bxe_setup_multi(struct bxe_softc *sc, int index)
3210 {
3211         struct bxe_fastpath *fp;
3212         int rc;
3213
3214         DBPRINT(sc, (BXE_VERBOSE_LOAD | BXE_VERBOSE_RESET |
3215             BXE_VERBOSE_UNLOAD), "%s(): Setup client connection "
3216             "on fp[%02d].\n", __FUNCTION__, index);
3217
3218         fp = &sc->fp[index];
3219         /* Reset IGU state. */
3220         bxe_ack_sb(sc, fp->sb_id, CSTORM_ID, 0, IGU_INT_ENABLE, 0);
3221
3222         /* Post a CLIENT_SETUP ramrod. */
3223         fp->state = BXE_FP_STATE_OPENING;
3224         bxe_sp_post(sc, RAMROD_CMD_ID_ETH_CLIENT_SETUP, index, 0, fp->cl_id, 0);
3225
3226         /* Wait for the ramrod to complete. */
3227         rc = bxe_wait_ramrod(sc, BXE_FP_STATE_OPEN, index, &fp->state, 1);
3228
3229         return (rc);
3230 }
3231
3232 /*
3233  * Stop a client connection.
3234  *
3235  * Stops an individual client connection on the device.  Use
3236  * bxe_stop_leading() for the first/default connection.
3237  *
3238  * Returns:
3239  *   0 = Success, !0 = Failure.
3240  */
3241 static int
3242 bxe_stop_multi(struct bxe_softc *sc, int index)
3243 {
3244         struct bxe_fastpath *fp;
3245         int rc;
3246
3247         DBPRINT(sc, (BXE_VERBOSE_LOAD | BXE_VERBOSE_RESET |
3248             BXE_VERBOSE_UNLOAD), "%s(): Stop client connection "
3249             "on fp[%02d].\n", __FUNCTION__, index);
3250
3251         fp = &sc->fp[index];
3252
3253         /* Halt the client connection. */
3254         fp->state = BXE_FP_STATE_HALTING;
3255         bxe_sp_post(sc, RAMROD_CMD_ID_ETH_HALT, index, 0, fp->cl_id, 0);
3256
3257         /* Wait for the HALT ramrod completion. */
3258         rc = bxe_wait_ramrod(sc, BXE_FP_STATE_HALTED, index, &fp->state, 1);
3259         if (rc){
3260                 BXE_PRINTF("%s(%d): fp[%02d] client ramrod halt failed!\n",
3261                     __FILE__, __LINE__, index);
3262                 goto bxe_stop_multi_exit;
3263         }
3264         /* Delete the CFC entry. */
3265         bxe_sp_post(sc, RAMROD_CMD_ID_ETH_CFC_DEL, index, 0, 0, 1);
3266
3267         /* Poll for the DELETE ramrod completion. */
3268         rc = bxe_wait_ramrod(sc, BXE_FP_STATE_CLOSED, index, &fp->state, 1);
3269
3270 bxe_stop_multi_exit:
3271         return (rc);
3272 }
3273
3274 /*
3275  * Hardware lock for shared, dual-port PHYs.
3276  *
3277  * Returns:
3278  *   None.
3279  */
3280 static void
3281 bxe_acquire_phy_lock(struct bxe_softc *sc)
3282 {
3283         uint32_t ext_phy_type;
3284
3285         DBENTER(BXE_VERBOSE_PHY);
3286
3287         ext_phy_type = XGXS_EXT_PHY_TYPE(sc->link_params.ext_phy_config);
3288         switch(ext_phy_type){
3289                 case PORT_HW_CFG_XGXS_EXT_PHY_TYPE_BCM8072:
3290                 case PORT_HW_CFG_XGXS_EXT_PHY_TYPE_BCM8073:
3291                 case PORT_HW_CFG_XGXS_EXT_PHY_TYPE_BCM8726:
3292                 case PORT_HW_CFG_XGXS_EXT_PHY_TYPE_BCM8727:
3293                         bxe_acquire_hw_lock(sc, HW_LOCK_RESOURCE_MDIO);
3294                         break;
3295                 default:
3296                         break;
3297         }
3298         DBEXIT(BXE_VERBOSE_PHY);
3299 }
3300
3301 /*
3302  * Hardware unlock for shared, dual-port PHYs.
3303  *
3304  * Returns:
3305  *   None.
3306  */
3307 static void
3308 bxe_release_phy_lock(struct bxe_softc *sc)
3309 {
3310         uint32_t ext_phy_type;
3311
3312         DBENTER(BXE_VERBOSE_PHY);
3313         ext_phy_type = XGXS_EXT_PHY_TYPE(sc->link_params.ext_phy_config);
3314         switch(ext_phy_type){
3315                 case PORT_HW_CFG_XGXS_EXT_PHY_TYPE_BCM8072:
3316                 case PORT_HW_CFG_XGXS_EXT_PHY_TYPE_BCM8073:
3317                 case PORT_HW_CFG_XGXS_EXT_PHY_TYPE_BCM8726:
3318                 case PORT_HW_CFG_XGXS_EXT_PHY_TYPE_BCM8727:
3319                         bxe_release_hw_lock(sc, HW_LOCK_RESOURCE_MDIO);
3320                         break;
3321                 default:
3322                         break;
3323         }
3324
3325         DBEXIT(BXE_VERBOSE_PHY);
3326 }
3327
3328 /*
3329  *
3330  * Returns:
3331  *   None.
3332  */
3333 static void
3334 bxe__link_reset(struct bxe_softc *sc)
3335 {
3336         DBENTER(BXE_VERBOSE_PHY);
3337
3338         if (!NOMCP(sc)) {
3339                 bxe_acquire_phy_lock(sc);
3340                 bxe_link_reset(&sc->link_params, &sc->link_vars, 1);
3341                 bxe_release_phy_lock(sc);
3342         } else {
3343                 DBPRINT(sc, BXE_WARN,
3344                     "%s(): Bootcode is not running, not resetting link!\n",
3345                     __FUNCTION__);
3346         }
3347
3348         DBEXIT(BXE_VERBOSE_PHY);
3349 }
3350
3351 /*
3352  * Stop the controller.
3353  *
3354  * Returns:
3355  *   0 = Success, !0 = Failure
3356  */
3357 static int
3358 bxe_stop_locked(struct bxe_softc *sc, int unload_mode)
3359 {
3360         struct ifnet *ifp;
3361         struct mac_configuration_cmd *config;
3362         struct bxe_fastpath *fp;
3363         uint32_t reset_code;
3364         uint32_t emac_base, val;
3365         uint8_t entry, *mac_addr;
3366         int count, i, port, rc;
3367
3368         DBENTER(BXE_INFO_LOAD | BXE_INFO_RESET | BXE_INFO_UNLOAD);
3369
3370         ifp = sc->bxe_ifp;
3371         port = BP_PORT(sc),
3372         rc = reset_code = 0;
3373
3374         BXE_CORE_LOCK_ASSERT(sc);
3375
3376         /* Stop the periodic tick. */
3377         callout_stop(&sc->bxe_tick_callout);
3378
3379         sc->state = BXE_STATE_CLOSING_WAIT4_HALT;
3380
3381         /* Prevent any further RX traffic. */
3382         sc->rx_mode = BXE_RX_MODE_NONE;
3383         bxe_set_storm_rx_mode(sc);
3384
3385         /* Tell the stack the driver is stopped and TX queue is full. */
3386         if (ifp != NULL)
3387                 ifp->if_drv_flags = 0;
3388
3389         /* Tell the bootcode to stop watching for a heartbeat. */
3390         SHMEM_WR(sc, func_mb[BP_FUNC(sc)].drv_pulse_mb,
3391             (DRV_PULSE_ALWAYS_ALIVE | sc->fw_drv_pulse_wr_seq));
3392
3393         /* Stop the statistics updates. */
3394         bxe_stats_handle(sc, STATS_EVENT_STOP);
3395
3396         /* Wait until all TX fastpath tasks have completed. */
3397         for (i = 0; i < sc->num_queues; i++) {
3398                 fp = &sc->fp[i];
3399
3400                 if (fp == NULL || fp->tx_pkt_cons_sb == NULL)
3401                         break;
3402
3403                 count = 1000;
3404                 while (bxe_has_tx_work(fp)) {
3405
3406                         bxe_txeof(fp);
3407
3408                         if (count == 0) {
3409                                 BXE_PRINTF(
3410                 "%s(%d): Timeout wating for fp[%02d] transmits to complete!\n",
3411                                     __FILE__, __LINE__, i);
3412                                 break;
3413                         }
3414                         count--;
3415                         DELAY(1000);
3416                         rmb();
3417                 }
3418         }
3419
3420         /* Wait until all slowpath tasks have completed. */
3421         count = 1000;
3422         while ((sc->spq_left != MAX_SPQ_PENDING) && count--)
3423                 DELAY(1000);
3424
3425         /* Disable Interrupts */
3426         bxe_int_disable(sc);
3427         DELAY(1000);
3428
3429         /* Clear the MAC addresses. */
3430         if (CHIP_IS_E1(sc)) {
3431                 config = BXE_SP(sc, mcast_config);
3432                 bxe_set_mac_addr_e1(sc, 0);
3433
3434                 for (i = 0; i < config->hdr.length; i++)
3435                         CAM_INVALIDATE(&config->config_table[i]);
3436
3437                 config->hdr.length = i;
3438                 config->hdr.offset = BXE_MAX_MULTICAST * (1 + port);
3439                 config->hdr.client_id = BP_CL_ID(sc);
3440                 config->hdr.reserved1 = 0;
3441
3442                 bxe_sp_post(sc, RAMROD_CMD_ID_ETH_SET_MAC, 0,
3443                     U64_HI(BXE_SP_MAPPING(sc, mcast_config)),
3444                     U64_LO(BXE_SP_MAPPING(sc, mcast_config)), 0);
3445         } else {
3446                 REG_WR(sc, NIG_REG_LLH0_FUNC_EN + port * 8, 0);
3447                 bxe_set_mac_addr_e1h(sc, 0);
3448                 for (i = 0; i < MC_HASH_SIZE; i++)
3449                         REG_WR(sc, MC_HASH_OFFSET(sc, i), 0);
3450                 REG_WR(sc, MISC_REG_E1HMF_MODE, 0);
3451         }
3452
3453         /* Determine if any WoL settings needed. */
3454         if (unload_mode == UNLOAD_NORMAL)
3455                 /* Driver initiatied WoL is disabled. */
3456                 reset_code = DRV_MSG_CODE_UNLOAD_REQ_WOL_DIS;
3457         else if (sc->bxe_flags & BXE_NO_WOL_FLAG) {
3458                 /* Driver initiated WoL is disabled, use OOB WoL settings. */
3459                 reset_code = DRV_MSG_CODE_UNLOAD_REQ_WOL_MCP;
3460                 if (CHIP_IS_E1H(sc))
3461                         REG_WR(sc, MISC_REG_E1HMF_MODE, 0);
3462         } else if (sc->wol) {
3463                 emac_base = BP_PORT(sc) ?  GRCBASE_EMAC0 : GRCBASE_EMAC1;
3464                 mac_addr = sc->link_params.mac_addr;
3465                 entry = (BP_E1HVN(sc) + 1) * 8;
3466                 val = (mac_addr[0] << 8) | mac_addr[1];
3467                 EMAC_WR(sc, EMAC_REG_EMAC_MAC_MATCH + entry, val);
3468                 val = (mac_addr[2] << 24) | (mac_addr[3] << 16) |
3469                     (mac_addr[4] << 8) | mac_addr[5];
3470                 EMAC_WR(sc, EMAC_REG_EMAC_MAC_MATCH + entry + 4, val);
3471                 reset_code = DRV_MSG_CODE_UNLOAD_REQ_WOL_EN;
3472         } else {
3473                 /* Prevent WoL. */
3474                 reset_code = DRV_MSG_CODE_UNLOAD_REQ_WOL_DIS;
3475         }
3476
3477         /* Stop all non-leading client connections. */
3478         for (i = 1; i < sc->num_queues; i++) {
3479                 if (bxe_stop_multi(sc, i)){
3480                         goto bxe_stop_locked_exit;
3481                 }
3482         }
3483
3484         /* Stop the leading client connection. */
3485         rc = bxe_stop_leading(sc);
3486         DELAY(10000);
3487
3488 bxe_stop_locked_exit:
3489         if (NOMCP(sc)) {
3490                 DBPRINT(sc, BXE_INFO,
3491                     "%s(): Old No MCP load counts:  %d, %d, %d\n",
3492                     __FUNCTION__, load_count[0], load_count[1], load_count[2]);
3493
3494                 load_count[0]--;
3495                 load_count[1 + port]--;
3496                 DBPRINT(sc, BXE_INFO,
3497                     "%s(): New No MCP load counts:  %d, %d, %d\n",
3498                     __FUNCTION__, load_count[0], load_count[1], load_count[2]);
3499
3500                 if (load_count[0] == 0)
3501                         reset_code = FW_MSG_CODE_DRV_UNLOAD_COMMON;
3502                 else if (load_count[1 + BP_PORT(sc)] == 0)
3503                         reset_code = FW_MSG_CODE_DRV_UNLOAD_PORT;
3504                 else
3505                         reset_code = FW_MSG_CODE_DRV_UNLOAD_FUNCTION;
3506         } else {
3507                 /* Tell MCP driver unload is complete. */
3508                 reset_code = bxe_fw_command(sc, reset_code);
3509         }
3510
3511         if ((reset_code == FW_MSG_CODE_DRV_UNLOAD_COMMON) ||
3512             (reset_code == FW_MSG_CODE_DRV_UNLOAD_PORT))
3513                 bxe__link_reset(sc);
3514
3515         DELAY(10000);
3516
3517         /* Reset the chip */
3518         bxe_reset_chip(sc, reset_code);
3519
3520         DELAY(10000);
3521
3522         /* Report UNLOAD_DONE to MCP */
3523         if (!NOMCP(sc))
3524                 bxe_fw_command(sc, DRV_MSG_CODE_UNLOAD_DONE);
3525         sc->port.pmf = 0;
3526
3527         /* Free RX chains and buffers. */
3528         bxe_clear_rx_chains(sc);
3529
3530         /* Free TX chains and buffers. */
3531         bxe_clear_tx_chains(sc);
3532
3533         sc->state = BXE_STATE_CLOSED;
3534
3535         bxe_ack_int(sc);
3536
3537         DBEXIT(BXE_INFO_LOAD | BXE_INFO_RESET |BXE_INFO_UNLOAD);
3538         return (rc);
3539 }
3540
3541 /*
3542  * Device shutdown function.
3543  *
3544  * Stops and resets the controller.
3545  *
3546  * Returns:
3547  *   0 = Success, !0 = Failure
3548  */
3549 static int
3550 bxe_shutdown(device_t dev)
3551 {
3552         struct bxe_softc *sc;
3553
3554         sc = device_get_softc(dev);
3555         DBENTER(BXE_INFO_LOAD | BXE_INFO_RESET | BXE_INFO_UNLOAD);
3556
3557         /* Stop the controller, but only if it was ever started.
3558          * Stopping an uninitialized controller can cause
3559          * IPMI bus errors on some systems.
3560          */
3561         BXE_CORE_LOCK(sc);
3562         if (sc->state != BXE_STATE_CLOSED) {
3563                 bxe_stop_locked(sc, UNLOAD_NORMAL);
3564         }
3565         BXE_CORE_UNLOCK(sc);
3566
3567         DBEXIT(BXE_INFO_LOAD | BXE_INFO_RESET | BXE_INFO_UNLOAD);
3568         return (0);
3569 }
3570
3571 /*
3572  * Prints out link speed and duplex setting to console.
3573  *
3574  * Returns:
3575  *   None.
3576  */
3577 static void
3578 bxe_link_report(struct bxe_softc *sc)
3579 {
3580         uint32_t line_speed;
3581         uint16_t vn_max_rate;
3582
3583         DBENTER(BXE_VERBOSE_PHY);
3584
3585         if (sc->link_vars.link_up) {
3586                 /* Report the link status change to OS. */
3587                 if (sc->state == BXE_STATE_OPEN)
3588                         if_link_state_change(sc->bxe_ifp, LINK_STATE_UP);
3589
3590                 line_speed = sc->link_vars.line_speed;
3591
3592                 if (IS_E1HMF(sc)){
3593                         vn_max_rate = ((sc->mf_config[BP_E1HVN(sc)] &
3594                             FUNC_MF_CFG_MAX_BW_MASK) >>
3595                             FUNC_MF_CFG_MAX_BW_SHIFT) * 100;
3596                         if (vn_max_rate < line_speed)
3597                                 line_speed = vn_max_rate;
3598                 }
3599
3600                 BXE_PRINTF("Link is up, %d Mbps, ", line_speed);
3601
3602                 if (sc->link_vars.duplex == MEDIUM_FULL_DUPLEX)
3603                         printf("full duplex");
3604                 else
3605                         printf("half duplex");
3606
3607                 if (sc->link_vars.flow_ctrl) {
3608                         if (sc->link_vars.flow_ctrl & FLOW_CTRL_RX) {
3609                                 printf(", receive ");
3610                                 if (sc->link_vars.flow_ctrl & FLOW_CTRL_TX)
3611                                         printf("& transmit ");
3612                         } else
3613                                 printf(", transmit ");
3614                         printf("flow control ON");
3615                 }
3616                 printf("\n");
3617         } else {
3618                 /* Report the link down */
3619                 BXE_PRINTF("Link is down\n");
3620                 if_link_state_change(sc->bxe_ifp, LINK_STATE_DOWN);
3621         }
3622
3623         DBEXIT(BXE_VERBOSE_PHY);
3624 }
3625
3626 /*
3627  *
3628  * Returns:
3629  *   None.
3630  */
3631 static void
3632 bxe__link_status_update(struct bxe_softc *sc)
3633 {
3634         DBENTER(BXE_VERBOSE_PHY);
3635
3636         if (sc->stats_enable == FALSE || sc->state != BXE_STATE_OPEN)
3637                 return;
3638
3639         bxe_link_status_update(&sc->link_params, &sc->link_vars);
3640
3641         if (sc->link_vars.link_up)
3642                 bxe_stats_handle(sc, STATS_EVENT_LINK_UP);
3643         else
3644                 bxe_stats_handle(sc, STATS_EVENT_STOP);
3645
3646         bxe_read_mf_cfg(sc);
3647
3648         /* Indicate link status. */
3649         bxe_link_report(sc);
3650
3651         DBEXIT(BXE_VERBOSE_PHY);
3652 }
3653
3654 /*
3655  * Calculate flow control to advertise during autonegotiation.
3656  *
3657  * Returns:
3658  *   None.
3659  */
3660 static void
3661 bxe_calc_fc_adv(struct bxe_softc *sc)
3662 {
3663         DBENTER(BXE_EXTREME_PHY);
3664
3665         switch (sc->link_vars.ieee_fc &
3666             MDIO_COMBO_IEEE0_AUTO_NEG_ADV_PAUSE_MASK) {
3667
3668         case MDIO_COMBO_IEEE0_AUTO_NEG_ADV_PAUSE_NONE:
3669                 sc->port.advertising &= ~(ADVERTISED_Asym_Pause |
3670                     ADVERTISED_Pause);
3671                 break;
3672
3673         case MDIO_COMBO_IEEE0_AUTO_NEG_ADV_PAUSE_BOTH:
3674                 sc->port.advertising |= (ADVERTISED_Asym_Pause |
3675                     ADVERTISED_Pause);
3676                 break;
3677
3678         case MDIO_COMBO_IEEE0_AUTO_NEG_ADV_PAUSE_ASYMMETRIC:
3679                 sc->port.advertising |= ADVERTISED_Asym_Pause;
3680                 break;
3681
3682         default:
3683                 sc->port.advertising &= ~(ADVERTISED_Asym_Pause |
3684                     ADVERTISED_Pause);
3685                 break;
3686         }
3687
3688         DBEXIT(BXE_EXTREME_PHY);
3689 }
3690
3691
3692
3693 /*
3694  *
3695  * Returns:
3696  *
3697  */
3698 static uint8_t
3699 bxe_initial_phy_init(struct bxe_softc *sc)
3700 {
3701         uint8_t rc;
3702
3703         DBENTER(BXE_VERBOSE_PHY);
3704
3705         rc = 0;
3706         if (!NOMCP(sc)) {
3707
3708                 /*
3709                  * It is recommended to turn off RX flow control for 5771x
3710                  * when using jumbo frames for better performance.
3711                  */
3712                 if (!IS_E1HMF(sc) && (sc->mbuf_alloc_size > 5000))
3713                         sc->link_params.req_fc_auto_adv = FLOW_CTRL_TX;
3714                 else
3715                         sc->link_params.req_fc_auto_adv = FLOW_CTRL_BOTH;
3716
3717                 bxe_acquire_phy_lock(sc);
3718                 rc = bxe_phy_init(&sc->link_params, &sc->link_vars);
3719                 bxe_release_phy_lock(sc);
3720
3721                 bxe_calc_fc_adv(sc);
3722
3723                 if (sc->link_vars.link_up) {
3724                     bxe_stats_handle(sc,STATS_EVENT_LINK_UP);
3725                     bxe_link_report(sc);
3726                 }
3727
3728         } else {
3729                 DBPRINT(sc, BXE_FATAL, "%s(): Bootcode is not running, "
3730                     "not initializing link!\n", __FUNCTION__);
3731                 rc = EINVAL;
3732         }
3733
3734         DBEXIT(BXE_VERBOSE_PHY);
3735         return (rc);
3736 }
3737
3738
3739 #if __FreeBSD_version >= 800000
3740 /*
3741  * Allocate buffer rings used for multiqueue.
3742  *
3743  * Returns:
3744  *   0 = Success, !0 = Failure.
3745  */
3746 static int
3747 bxe_alloc_buf_rings(struct bxe_softc *sc)
3748 {
3749         struct bxe_fastpath *fp;
3750         int i, rc;
3751
3752         DBENTER(BXE_VERBOSE_LOAD);
3753         rc = 0;
3754
3755         for (i = 0; i < sc->num_queues; i++) {
3756                 fp = &sc->fp[i];
3757
3758                 if (fp != NULL) {
3759                         fp->br = buf_ring_alloc(BXE_BR_SIZE,
3760                             M_DEVBUF, M_NOWAIT, &fp->mtx);
3761                         if (fp->br == NULL) {
3762                                 rc = ENOMEM;
3763                                 goto bxe_alloc_buf_rings_exit;
3764                         }
3765                 } else
3766                         BXE_PRINTF("%s(%d): Bug!\n", __FILE__, __LINE__);
3767         }
3768
3769 bxe_alloc_buf_rings_exit:
3770         DBEXIT(BXE_VERBOSE_LOAD);
3771         return (rc);
3772 }
3773
3774 /*
3775  * Releases buffer rings used for multiqueue.
3776  *
3777  * Returns:
3778  *   None
3779  */
3780 static void
3781 bxe_free_buf_rings(struct bxe_softc *sc)
3782 {
3783         struct bxe_fastpath *fp;
3784         int i;
3785
3786         DBENTER(BXE_VERBOSE_UNLOAD);
3787
3788         for (i = 0; i < sc->num_queues; i++) {
3789                 fp = &sc->fp[i];
3790                 if (fp != NULL) {
3791                         if (fp->br != NULL)
3792                                 buf_ring_free(fp->br, M_DEVBUF);
3793                 }
3794         }
3795
3796         DBEXIT(BXE_VERBOSE_UNLOAD);
3797 }
3798 #endif
3799
3800
3801 /*
3802  * Handles controller initialization.
3803  *
3804  * Must be called from a locked routine.  Since this code
3805  * may be called from the OS it does not provide a return
3806  * error value and must clean-up it's own mess.
3807  *
3808  * Returns:
3809  *   Nothing.
3810  */
3811 static void
3812 bxe_init_locked(struct bxe_softc *sc, int load_mode)
3813 {
3814         struct ifnet *ifp;
3815         uint32_t load_code;
3816         int error, i, port;
3817
3818         DBENTER(BXE_INFO_LOAD | BXE_INFO_RESET);
3819
3820         BXE_CORE_LOCK_ASSERT(sc);
3821
3822         ifp = sc->bxe_ifp;
3823         /* Skip if we're in panic mode. */
3824         if (sc->panic) {
3825                 DBPRINT(sc, BXE_WARN, "%s(): Panic mode enabled, exiting!\n",
3826                     __FUNCTION__);
3827                 goto bxe_init_locked_exit;
3828         }
3829
3830         /* Check if the driver is still running and bail out if it is. */
3831         if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
3832                 DBPRINT(sc, BXE_WARN,
3833                     "%s(): Init called while driver is running!\n",
3834                     __FUNCTION__);
3835                 goto bxe_init_locked_exit;
3836         }
3837
3838         /*
3839          * Send LOAD_REQUEST command to MCP.
3840          * The MCP will return the type of LOAD
3841          * the driver should perform.
3842          * - If it is the first port to be initialized
3843          *   then all common blocks should be initialized.
3844          * - If it is not the first port to be initialized
3845          *   then don't do the common block initialization.
3846          */
3847         sc->state = BXE_STATE_OPENING_WAIT4_LOAD;
3848
3849         if (NOMCP(sc)) {
3850                 port = BP_PORT(sc);
3851
3852                 DBPRINT(sc, BXE_INFO,
3853                     "%s(): Old No MCP load counts:  %d, %d, %d\n",
3854                     __FUNCTION__,
3855                     load_count[0], load_count[1], load_count[2]);
3856
3857                 load_count[0]++;
3858                 load_count[1 + port]++;
3859
3860                 DBPRINT(sc, BXE_INFO,
3861                     "%s(): New No MCP load counts:  %d, %d, %d\n",
3862                     __FUNCTION__,
3863                     load_count[0], load_count[1], load_count[2]);
3864
3865                 /* No MCP to tell us what to do. */
3866                 if (load_count[0] == 1)
3867                         load_code = FW_MSG_CODE_DRV_LOAD_COMMON;
3868                 else if (load_count[1 + port] == 1)
3869                         load_code = FW_MSG_CODE_DRV_LOAD_PORT;
3870                 else
3871                         load_code = FW_MSG_CODE_DRV_LOAD_FUNCTION;
3872
3873         } else {
3874                 /* Ask the MCP what type of initialization we need to do. */
3875                 load_code = bxe_fw_command(sc, DRV_MSG_CODE_LOAD_REQ);
3876
3877                 if ((load_code == 0) ||
3878                     (load_code == FW_MSG_CODE_DRV_LOAD_REFUSED)) {
3879                         BXE_PRINTF("%s(%d): Bootcode refused load request.!\n",
3880                             __FILE__, __LINE__);
3881                         goto bxe_init_locked_failed1;
3882                 }
3883         }
3884
3885         /* Keep track of whether we are controlling the port. */
3886         if ((load_code == FW_MSG_CODE_DRV_LOAD_COMMON) ||
3887             (load_code == FW_MSG_CODE_DRV_LOAD_PORT))
3888                 sc->port.pmf = 1;
3889         else
3890                 sc->port.pmf = 0;
3891
3892         /* Block any interrupts until we're ready. */
3893         sc->intr_sem = 1;
3894
3895         /* Initialize hardware. */
3896         error = bxe_init_hw(sc, load_code);
3897         if (error != 0){
3898                 BXE_PRINTF("%s(%d): Hardware initialization failed, "
3899                     "aborting!\n", __FILE__, __LINE__);
3900                 goto bxe_init_locked_failed1;
3901         }
3902
3903         /* Calculate and save the Ethernet MTU size. */
3904         sc->port.ether_mtu = ifp->if_mtu + ETHER_HDR_LEN +
3905             (ETHER_VLAN_ENCAP_LEN * 2) + ETHER_CRC_LEN + 4;
3906
3907         DBPRINT(sc, BXE_INFO, "%s(): Setting MTU = %d\n",
3908             __FUNCTION__, sc->port.ether_mtu);
3909
3910         /* Setup the mbuf allocation size for RX frames. */
3911         if (sc->port.ether_mtu <= MCLBYTES)
3912                 sc->mbuf_alloc_size = MCLBYTES;
3913         else if (sc->port.ether_mtu <= PAGE_SIZE)
3914                 sc->mbuf_alloc_size = PAGE_SIZE;
3915         else
3916                 sc->mbuf_alloc_size = MJUM9BYTES;
3917
3918         DBPRINT(sc, BXE_INFO, "%s(): mbuf_alloc_size = %d, "
3919             "max_frame_size = %d\n", __FUNCTION__,
3920             sc->mbuf_alloc_size, sc->port.ether_mtu);
3921
3922         /* Setup NIC internals and enable interrupts. */
3923         error = bxe_init_nic(sc, load_code);
3924         if (error != 0) {
3925                 BXE_PRINTF("%s(%d): NIC initialization failed, "
3926                     "aborting!\n", __FILE__, __LINE__);
3927                 goto bxe_init_locked_failed1;
3928         }
3929
3930         if ((load_code == FW_MSG_CODE_DRV_LOAD_COMMON) &&
3931             (sc->common.shmem2_base)){
3932                 if (sc->dcc_enable == TRUE) {
3933                         BXE_PRINTF("Enabing DCC support\n");
3934                         SHMEM2_WR(sc, dcc_support,
3935                             (SHMEM_DCC_SUPPORT_DISABLE_ENABLE_PF_TLV |
3936                              SHMEM_DCC_SUPPORT_BANDWIDTH_ALLOCATION_TLV));
3937                 }
3938         }
3939
3940 #if __FreeBSD_version >= 800000
3941         /* Allocate buffer rings for multiqueue operation. */
3942         error = bxe_alloc_buf_rings(sc);
3943         if (error != 0) {
3944                 BXE_PRINTF("%s(%d): Buffer ring initialization failed, "
3945                     "aborting!\n", __FILE__, __LINE__);
3946                 goto bxe_init_locked_failed1;
3947         }
3948 #endif
3949
3950         /* Tell MCP that driver load is done. */
3951         if (!NOMCP(sc)) {
3952                 load_code = bxe_fw_command(sc, DRV_MSG_CODE_LOAD_DONE);
3953                 if (!load_code) {
3954                         BXE_PRINTF("%s(%d): Driver load failed! No MCP "
3955                             "response to LOAD_DONE!\n", __FILE__, __LINE__);
3956                         goto bxe_init_locked_failed2;
3957                 }
3958         }
3959
3960         sc->state = BXE_STATE_OPENING_WAIT4_PORT;
3961
3962         /* Enable ISR for PORT_SETUP ramrod. */
3963         sc->intr_sem = 0;
3964
3965         /* Setup the leading connection for the controller. */
3966         error = bxe_setup_leading(sc);
3967         if (error != 0) {
3968                 DBPRINT(sc, BXE_FATAL, "%s(): Initial PORT_SETUP ramrod "
3969                     "failed. State is not OPEN!\n", __FUNCTION__);
3970                 goto bxe_init_locked_failed3;
3971         }
3972
3973         if (CHIP_IS_E1H(sc)) {
3974                 if (sc->mf_config[BP_E1HVN(sc)] & FUNC_MF_CFG_FUNC_DISABLED) {
3975                         BXE_PRINTF("Multi-function mode is disabled\n");
3976                         /* sc->state = BXE_STATE_DISABLED; */
3977                 }
3978
3979                 /* Setup additional client connections for RSS/multi-queue */
3980                 if (sc->state == BXE_STATE_OPEN) {
3981                         for (i = 1; i < sc->num_queues; i++) {
3982                                 if (bxe_setup_multi(sc, i)) {
3983                                         DBPRINT(sc, BXE_FATAL,
3984                 "%s(): fp[%02d] CLIENT_SETUP ramrod failed! State not OPEN!\n",
3985                                             __FUNCTION__, i);
3986                                         goto bxe_init_locked_failed4;
3987                                 }
3988                         }
3989                 }
3990         }
3991
3992         DELAY(5000);
3993         bxe_int_enable(sc);
3994         DELAY(5000);
3995         /* Initialize statistics. */
3996         bxe_stats_init(sc);
3997         DELAY(1000);
3998
3999         /* Load our MAC address. */
4000         bcopy(IF_LLADDR(sc->bxe_ifp), sc->link_params.mac_addr, ETHER_ADDR_LEN);
4001
4002         if (CHIP_IS_E1(sc))
4003                 bxe_set_mac_addr_e1(sc, 1);
4004         else
4005                 bxe_set_mac_addr_e1h(sc, 1);
4006
4007         DELAY(1000);
4008
4009         /* Perform PHY initialization for the primary port. */
4010         if (sc->port.pmf)
4011                 bxe_initial_phy_init(sc);
4012
4013         DELAY(1000);
4014
4015         /* Start fastpath. */
4016         switch (load_mode) {
4017         case LOAD_NORMAL:
4018         case LOAD_OPEN:
4019                 /* Initialize the receive filters. */
4020                 bxe_set_rx_mode(sc);
4021                 break;
4022
4023         case LOAD_DIAG:
4024                 /* Initialize the receive filters. */
4025                 bxe_set_rx_mode(sc);
4026                 sc->state = BXE_STATE_DIAG;
4027                 break;
4028
4029         default:
4030                 DBPRINT(sc, BXE_WARN, "%s(): Unknown load mode (%d)!\n",
4031                     __FUNCTION__, load_mode);
4032                 break;
4033         }
4034
4035         if (!sc->port.pmf)
4036                 bxe__link_status_update(sc);
4037
4038         DELAY(1000);
4039         /* Tell the stack the driver is running. */
4040         ifp->if_drv_flags = IFF_DRV_RUNNING;
4041
4042         /* Schedule our periodic timer tick. */
4043         callout_reset(&sc->bxe_tick_callout, hz, bxe_tick, sc);
4044         /* Everything went OK, go ahead and exit. */
4045         goto bxe_init_locked_exit;
4046
4047 bxe_init_locked_failed4:
4048         /* Try and gracefully shutdown the device because of a failure. */
4049         for (i = 1; i < sc->num_queues; i++)
4050                 bxe_stop_multi(sc, i);
4051
4052 bxe_init_locked_failed3:
4053         bxe_stop_leading(sc);
4054         bxe_stats_handle(sc, STATS_EVENT_STOP);
4055
4056 bxe_init_locked_failed2:
4057         bxe_int_disable(sc);
4058
4059 bxe_init_locked_failed1:
4060         if (!NOMCP(sc)) {
4061                 bxe_fw_command(sc, DRV_MSG_CODE_LOAD_DONE);
4062                 bxe_fw_command(sc, DRV_MSG_CODE_UNLOAD_REQ_WOL_MCP);
4063                 bxe_fw_command(sc, DRV_MSG_CODE_UNLOAD_DONE);
4064         }
4065         sc->port.pmf = 0;
4066
4067 #if __FreeBSD_version >= 800000
4068         bxe_free_buf_rings(sc);
4069 #endif
4070
4071         DBPRINT(sc, BXE_WARN, "%s(): Initialization failed!\n", __FUNCTION__);
4072
4073 bxe_init_locked_exit:
4074         DBEXIT(BXE_INFO_LOAD | BXE_INFO_RESET);
4075 }
4076
4077 /*
4078  * Ramrod wait function.
4079  *
4080  * Waits for a ramrod command to complete.
4081  *
4082  * Returns:
4083  *   0 = Success, !0 = Failure
4084  */
4085 static int
4086 bxe_wait_ramrod(struct bxe_softc *sc, int state, int idx, int *state_p,
4087     int poll)
4088 {
4089         int rc, timeout;
4090
4091         DBENTER(BXE_VERBOSE_LOAD | BXE_VERBOSE_RESET | BXE_VERBOSE_RAMROD);
4092
4093         DBPRINT(sc, BXE_VERBOSE_RAMROD, "%s(): %s for state 0x%08X on "
4094             "fp[%02d], currently 0x%08X.\n", __FUNCTION__,
4095             poll ? "Polling" : "Waiting", state, idx, *state_p);
4096
4097         rc = 0;
4098         timeout = 5000;
4099         while (timeout) {
4100
4101                 /* Manually check for the completion. */
4102                 if (poll) {
4103                         bxe_rxeof(sc->fp);
4104                         /*
4105                          * Some commands don't use the leading client
4106                          * connection.
4107                          */
4108                         if (idx)
4109                                 bxe_rxeof(&sc->fp[idx]);
4110                 }
4111
4112                 /* State may be changed by bxe_sp_event(). */
4113                 mb();
4114                 if (*state_p == state)
4115                         goto bxe_wait_ramrod_exit;
4116
4117                 timeout--;
4118
4119                 /* Pause 1ms before checking again. */
4120                 DELAY(1000);
4121         }
4122
4123         /* We timed out polling for a completion. */
4124         DBPRINT(sc, BXE_FATAL, "%s(): Timeout %s for state 0x%08X on fp[%02d]. "
4125             "Got 0x%x instead\n", __FUNCTION__, poll ? "polling" : "waiting",
4126             state, idx, *state_p);
4127
4128         rc = EBUSY;
4129
4130 bxe_wait_ramrod_exit:
4131
4132         DBEXIT(BXE_VERBOSE_LOAD | BXE_VERBOSE_RESET | BXE_VERBOSE_RAMROD);
4133         return (rc);
4134 }
4135
4136 /*
4137  *
4138  *
4139  */
4140 static void
4141 bxe_write_dmae_phys_len(struct bxe_softc *sc, bus_addr_t phys_addr,
4142     uint32_t addr, uint32_t len)
4143 {
4144         int dmae_wr_max, offset;
4145         DBENTER(BXE_INSANE_REGS);
4146
4147         dmae_wr_max = DMAE_LEN32_WR_MAX(sc);
4148         offset = 0;
4149         while (len > dmae_wr_max) {
4150                 bxe_write_dmae(sc, phys_addr + offset, addr + offset,
4151                     dmae_wr_max);
4152                 offset += dmae_wr_max * 4;
4153                 len -= dmae_wr_max;
4154         }
4155         bxe_write_dmae(sc, phys_addr + offset, addr + offset, len);
4156         DBEXIT(BXE_INSANE_REGS);
4157
4158 }
4159
4160
4161
4162 #define INIT_MEM_WR(block, reg, part, hw, data, reg_off, len) \
4163         bxe_init_str_wr(sc, GRCBASE_##block + reg + reg_off * 4, data, len)
4164
4165
4166 /*
4167  * Write a block of data to a range of registers.
4168  *
4169  * Returns:
4170  *   None.
4171  */
4172 static void
4173 bxe_init_str_wr(struct bxe_softc *sc, uint32_t addr, const uint32_t *data,
4174     uint32_t len)
4175 {
4176         uint32_t i;
4177         for (i = 0; i < len; i++)
4178                 REG_WR(sc, addr + i * 4, data[i]);
4179 }
4180
4181 /*
4182  * Write a block of data to a range of registers using indirect access.
4183  *
4184  * Returns:
4185  *   None.
4186  */
4187 static void
4188 bxe_init_ind_wr(struct bxe_softc *sc, uint32_t addr, const uint32_t *data,
4189     uint16_t len)
4190 {
4191         uint32_t i;
4192         for (i = 0; i < len; i++)
4193                 REG_WR_IND(sc, addr + i * 4, data[i]);
4194 }
4195
4196 /*
4197  *
4198  * Returns:
4199  *   None.
4200  */
4201 static void
4202 bxe_write_big_buf(struct bxe_softc *sc, uint32_t addr, uint32_t len)
4203 {
4204         DBENTER(BXE_INSANE_REGS);
4205 #ifdef BXE_USE_DMAE
4206         if (sc->dmae_ready)
4207                 bxe_write_dmae_phys_len(sc, sc->gz_dma.paddr, addr, len);
4208         else
4209                 bxe_init_str_wr(sc, addr, sc->gz, len);
4210 #else
4211         bxe_init_str_wr(sc, addr, sc->gz, len);
4212 #endif
4213
4214         DBEXIT(BXE_INSANE_REGS);
4215 }
4216
4217 /*
4218  * Fill areas of device memory with the specified value.
4219  *
4220  * Generally used to clear a small area of device memory prior to writing
4221  * firmware to STORM memory or writing STORM firmware to device memory.
4222  *
4223  * Returns:
4224  *   None.
4225  */
4226 static void
4227 bxe_init_fill(struct bxe_softc *sc, uint32_t addr, int fill, uint32_t len)
4228 {
4229         uint32_t cur_len, i, leftovers, length;
4230
4231         DBENTER(BXE_VERBOSE_LOAD);
4232
4233         length = (((len * 4) > BXE_FW_BUF_SIZE) ? BXE_FW_BUF_SIZE : (len * 4));
4234         leftovers = length / 4;
4235         memset(sc->gz, fill, length);
4236
4237         for (i = 0; i < len; i += leftovers) {
4238                 cur_len = min(leftovers, len - i);
4239                 bxe_write_big_buf(sc, addr + i * 4, cur_len);
4240         }
4241
4242         DBEXIT(BXE_VERBOSE_LOAD);
4243 }
4244
4245 /*
4246  *
4247  * Returns:
4248  *   None.
4249  */
4250 static void
4251 bxe_init_wr_64(struct bxe_softc *sc, uint32_t addr, const uint32_t *data,
4252     uint32_t len64)
4253 {
4254         uint64_t data64, *pdata;
4255         uint32_t buf_len32, cur_len, len;
4256         int i;
4257
4258         DBENTER(BXE_INSANE_REGS);
4259
4260         buf_len32 = BXE_FW_BUF_SIZE / 4;
4261         len = len64 * 2;
4262         /* 64 bit value is in a blob: first low DWORD, then high DWORD. */
4263         data64 = HILO_U64((*(data + 1)), (*data));
4264         len64 = min((uint32_t)(BXE_FW_BUF_SIZE / 8), len64);
4265         for (i = 0; i < len64; i++) {
4266                 pdata = ((uint64_t *)(sc->gz)) + i;
4267                 *pdata = data64;
4268         }
4269
4270         for (i = 0; i < len; i += buf_len32) {
4271                 cur_len = min(buf_len32, len - i);
4272                 bxe_write_big_buf(sc, addr + i*4, cur_len);
4273         }
4274
4275         DBEXIT(BXE_INSANE_REGS);
4276 }
4277
4278
4279 /*
4280  * There are different blobs for each PRAM section. In addition, each
4281  * blob write operation is divided into multiple, smaller write
4282  * operations in order to decrease the amount of physically contiguous
4283  * buffer memory needed. Thus, when we select a blob, the address may
4284  * be with some offset from the beginning of PRAM section. The same
4285  * holds for the INT_TABLE sections.
4286  */
4287
4288 #define IF_IS_INT_TABLE_ADDR(base, addr) \
4289         if (((base) <= (addr)) && ((base) + 0x400 >= (addr)))
4290
4291 #define IF_IS_PRAM_ADDR(base, addr) \
4292         if (((base) <= (addr)) && ((base) + 0x40000 >= (addr)))
4293
4294 /*
4295  *
4296  * Returns:
4297  *   None.
4298  */
4299
4300 static const uint8_t *
4301 bxe_sel_blob(struct bxe_softc *sc, uint32_t addr, const uint8_t *data)
4302 {
4303
4304         IF_IS_INT_TABLE_ADDR(TSEM_REG_INT_TABLE, addr)
4305                 data = INIT_TSEM_INT_TABLE_DATA(sc);
4306         else
4307                 IF_IS_INT_TABLE_ADDR(CSEM_REG_INT_TABLE, addr)
4308                         data = INIT_CSEM_INT_TABLE_DATA(sc);
4309         else
4310                 IF_IS_INT_TABLE_ADDR(USEM_REG_INT_TABLE, addr)
4311                         data = INIT_USEM_INT_TABLE_DATA(sc);
4312         else
4313                 IF_IS_INT_TABLE_ADDR(XSEM_REG_INT_TABLE, addr)
4314                         data = INIT_XSEM_INT_TABLE_DATA(sc);
4315         else
4316                 IF_IS_PRAM_ADDR(TSEM_REG_PRAM, addr)
4317                         data = INIT_TSEM_PRAM_DATA(sc);
4318         else
4319                 IF_IS_PRAM_ADDR(CSEM_REG_PRAM, addr)
4320                         data = INIT_CSEM_PRAM_DATA(sc);
4321         else
4322                 IF_IS_PRAM_ADDR(USEM_REG_PRAM, addr)
4323                         data = INIT_USEM_PRAM_DATA(sc);
4324         else
4325                 IF_IS_PRAM_ADDR(XSEM_REG_PRAM, addr)
4326                         data = INIT_XSEM_PRAM_DATA(sc);
4327
4328         return (data);
4329
4330 }
4331
4332 static void
4333 bxe_write_big_buf_wb(struct bxe_softc *sc, uint32_t addr, uint32_t len)
4334 {
4335         if (sc->dmae_ready)
4336                 bxe_write_dmae_phys_len(sc, sc->gz_dma.paddr, addr, len);
4337         else
4338                 bxe_init_ind_wr(sc, addr, sc->gz, len);
4339 }
4340
4341
4342 #define VIRT_WR_DMAE_LEN(sc, data, addr, len32, le32_swap) \
4343         do { \
4344                 memcpy(sc->gz, data, (len32)*4); \
4345                 bxe_write_big_buf_wb(sc, addr, len32); \
4346         } while (0)
4347
4348
4349 /*
4350  *
4351  * Returns:
4352  *   None.
4353  */
4354 static void
4355 bxe_init_wr_wb(struct bxe_softc *sc, uint32_t addr, const uint32_t *data,
4356     uint32_t len)
4357 {
4358         const uint32_t *old_data;
4359
4360         DBENTER(BXE_INSANE_REGS);
4361         old_data = data;
4362         data = (const uint32_t *)bxe_sel_blob(sc, addr, (const uint8_t *)data);
4363         if (sc->dmae_ready) {
4364                 if (old_data != data)
4365                         VIRT_WR_DMAE_LEN(sc, data, addr, len, 1);
4366                 else
4367                         VIRT_WR_DMAE_LEN(sc, data, addr, len, 0);
4368         } else
4369                 bxe_init_ind_wr(sc, addr, data, len);
4370
4371         DBEXIT(BXE_INSANE_REGS);
4372 }
4373
4374 static void
4375 bxe_init_wr_zp(struct bxe_softc *sc, uint32_t addr, uint32_t len,
4376     uint32_t blob_off)
4377 {
4378         BXE_PRINTF("%s(%d): Compressed FW is not supported yet. "
4379             "ERROR: address:0x%x len:0x%x blob_offset:0x%x\n",
4380             __FILE__, __LINE__, addr, len, blob_off);
4381 }
4382
4383 /*
4384  * Initialize blocks of the device.
4385  *
4386  * This routine basically performs bulk register programming for different
4387  * blocks within the controller.  The file bxe_init_values.h contains a
4388  * series of register access operations (read, write, fill, etc.) as well
4389  * as a BLOB of data to initialize multiple blocks within the controller.
4390  * Block initialization may be supported by all controllers or by specific
4391  * models only.
4392  *
4393  * Returns:
4394  *   None.
4395  */
4396 static void
4397 bxe_init_block(struct bxe_softc *sc, uint32_t block, uint32_t stage)
4398 {
4399         union init_op *op;
4400         const uint32_t *data, *data_base;
4401         uint32_t i, op_type, addr, len;
4402         uint16_t op_end, op_start;
4403         int hw_wr;
4404
4405         DBENTER(BXE_VERBOSE_LOAD | BXE_VERBOSE_RESET);
4406
4407         op_start = INIT_OPS_OFFSETS(sc)[BLOCK_OPS_IDX(block, stage,
4408             STAGE_START)];
4409         op_end = INIT_OPS_OFFSETS(sc)[BLOCK_OPS_IDX(block, stage, STAGE_END)];
4410         /* If empty block */
4411         if (op_start == op_end)
4412                 return;
4413
4414         hw_wr = OP_WR_ASIC;
4415
4416         data_base = INIT_DATA(sc);
4417
4418         for (i = op_start; i < op_end; i++) {
4419
4420                 op = (union init_op *)&(INIT_OPS(sc)[i]);
4421
4422                 op_type = op->str_wr.op;
4423                 addr = op->str_wr.offset;
4424                 len = op->str_wr.data_len;
4425                 data = data_base + op->str_wr.data_off;
4426
4427                 /* HW/EMUL specific */
4428                 if ((op_type > OP_WB) && (op_type == hw_wr))
4429                         op_type = OP_WR;
4430
4431                 switch (op_type) {
4432                 case OP_RD:
4433                         REG_RD(sc, addr);
4434                         break;
4435                 case OP_WR:
4436                         REG_WR(sc, addr, op->write.val);
4437                         break;
4438                 case OP_SW:
4439                         bxe_init_str_wr(sc, addr, data, len);
4440                         break;
4441                 case OP_WB:
4442                         bxe_init_wr_wb(sc, addr, data, len);
4443                         break;
4444                 case OP_SI:
4445                         bxe_init_ind_wr(sc, addr, data, len);
4446                         break;
4447                 case OP_ZR:
4448                         bxe_init_fill(sc, addr, 0, op->zero.len);
4449                         break;
4450                 case OP_ZP:
4451                         bxe_init_wr_zp(sc, addr, len, op->str_wr.data_off);
4452                         break;
4453                 case OP_WR_64:
4454                         bxe_init_wr_64(sc, addr, data, len);
4455                         break;
4456                 default:
4457                         /* happens whenever an op is of a diff HW */
4458                         break;
4459                 }
4460         }
4461
4462         DBEXIT(BXE_VERBOSE_LOAD | BXE_VERBOSE_RESET);
4463 }
4464
4465 /*
4466  * Handles controller initialization when called from an unlocked routine.
4467  * ifconfig calls this function.
4468  *
4469  * Returns:
4470  *   None.
4471  */
4472 static void
4473 bxe_init(void *xsc)
4474 {
4475         struct bxe_softc *sc;
4476
4477         sc = xsc;
4478
4479         BXE_CORE_LOCK(sc);
4480         bxe_init_locked(sc, LOAD_NORMAL);
4481         BXE_CORE_UNLOCK(sc);
4482 }
4483
4484 /*
4485  * Release all resources used by the driver.
4486  *
4487  * Releases all resources acquired by the driver including interrupts,
4488  * interrupt handler, interfaces, mutexes, and DMA memory.
4489  *
4490  * Returns:
4491  *   None.
4492  */
4493 static void
4494 bxe_release_resources(struct bxe_softc *sc)
4495 {
4496         device_t dev;
4497
4498         DBENTER(BXE_VERBOSE_RESET | BXE_VERBOSE_UNLOAD);
4499
4500         dev = sc->dev;
4501
4502         /* Release the FreeBSD interface. */
4503         if (sc->bxe_ifp != NULL)
4504                 if_free(sc->bxe_ifp);
4505
4506         /* Free the DMA resources. */
4507         bxe_host_structures_free(sc);
4508
4509 #if __FreeBSD_version >= 800000
4510         /* Free multiqueue buffer rings. */
4511         bxe_free_buf_rings(sc);
4512 #endif
4513
4514 }
4515
4516
4517 /*
4518  * Indirect register write.
4519  *
4520  * Writes NetXtreme II registers using an index/data register pair in PCI
4521  * configuration space.  Using this mechanism avoids issues with posted
4522  * writes but is much slower than memory-mapped I/O.
4523  *
4524  * Returns:
4525  *   None.
4526  */
4527 static void
4528 bxe_reg_wr_ind(struct bxe_softc *sc, uint32_t offset, uint32_t val)
4529 {
4530         DBPRINT(sc, BXE_INSANE_REGS, "%s(); offset = 0x%08X, val = 0x%08X\n",
4531                 __FUNCTION__, offset, val);
4532
4533         pci_write_config(sc->dev, PCICFG_GRC_ADDRESS, offset, 4);
4534         pci_write_config(sc->dev, PCICFG_GRC_DATA, val, 4);
4535
4536         /* Return to a safe address. */
4537         pci_write_config(sc->dev, PCICFG_GRC_ADDRESS,
4538             PCICFG_VENDOR_ID_OFFSET, 4);
4539 }
4540
4541
4542 /*
4543  * Indirect register read.
4544  *
4545  * Reads NetXtreme II registers using an index/data register pair in PCI
4546  * configuration space.  Using this mechanism avoids issues with posted
4547  * reads but is much slower than memory-mapped I/O.
4548  *
4549  * Returns:
4550  *   The value of the register.
4551  */
4552 static uint32_t
4553 bxe_reg_rd_ind(struct bxe_softc *sc, uint32_t offset)
4554 {
4555         uint32_t val;
4556
4557         pci_write_config(sc->dev, PCICFG_GRC_ADDRESS, offset, 4);
4558         val = pci_read_config(sc->dev, PCICFG_GRC_DATA, 4);
4559
4560         /* Return to a safe address. */
4561         pci_write_config(sc->dev, PCICFG_GRC_ADDRESS,
4562             PCICFG_VENDOR_ID_OFFSET, 4);
4563
4564         DBPRINT(sc, BXE_INSANE_REGS, "%s(); offset = 0x%08X, val = 0x%08X\n",
4565             __FUNCTION__, offset, val);
4566         return (val);
4567 }
4568
4569
4570
4571 static uint32_t dmae_reg_go_c[] = {
4572         DMAE_REG_GO_C0, DMAE_REG_GO_C1, DMAE_REG_GO_C2, DMAE_REG_GO_C3,
4573         DMAE_REG_GO_C4, DMAE_REG_GO_C5, DMAE_REG_GO_C6, DMAE_REG_GO_C7,
4574         DMAE_REG_GO_C8, DMAE_REG_GO_C9, DMAE_REG_GO_C10, DMAE_REG_GO_C11,
4575         DMAE_REG_GO_C12, DMAE_REG_GO_C13, DMAE_REG_GO_C14, DMAE_REG_GO_C15
4576 };
4577
4578
4579 /*
4580  * Copy DMAE command into memory and start the command.
4581  *
4582  * Returns:
4583  *   None.
4584  */
4585 static void
4586 bxe_post_dmae(struct bxe_softc *sc, struct dmae_command *dmae, int idx)
4587 {
4588         uint32_t cmd_offset;
4589         int i;
4590         cmd_offset = (DMAE_REG_CMD_MEM + sizeof(struct dmae_command) * idx);
4591
4592         for (i = 0; i < (sizeof(struct dmae_command) / 4); i++) {
4593                 REG_WR(sc, cmd_offset + i * 4, *(((uint32_t *)dmae) + i));
4594                 DBPRINT(sc, BXE_INSANE_REGS, "%s(): DMAE cmd[%d].%d : 0x%08X\n",
4595                     __FUNCTION__, idx, i, cmd_offset + i * 4);
4596         }
4597
4598         /* Kick off the command. */
4599         REG_WR(sc, dmae_reg_go_c[idx], 1);
4600 }
4601
4602
4603 /*
4604  * Perform a DMAE write to device memory.
4605  *
4606  * Some of the registers on the 577XX controller are 128bits wide.  It is
4607  * required that when accessing those registers that they be written
4608  * atomically and that no intervening bus acceses to the device occur.
4609  * This could be handled by a lock held across all driver instances for
4610  * the device or it can be handled by performing a DMA operation when
4611  * writing to the device.  This code implements the latter.
4612  *
4613  * Returns:
4614  *   None.
4615  */
4616 void
4617 bxe_write_dmae(struct bxe_softc *sc, bus_addr_t dma_addr, uint32_t dst_addr,
4618     uint32_t len32)
4619 {
4620         struct dmae_command dmae;
4621         uint32_t *data, *wb_comp;
4622         int timeout;
4623
4624         DBENTER(BXE_INSANE_REGS);
4625
4626         DBPRINT(sc, BXE_EXTREME_REGS,
4627             "%s(): host addr = 0x%jX, device addr = 0x%08X, length = %d.\n",
4628             __FUNCTION__, (uintmax_t)dma_addr, dst_addr, (int)len32);
4629
4630         wb_comp = BXE_SP(sc, wb_comp);
4631         /* Fall back to indirect access if DMAE is not ready. */
4632         if (!sc->dmae_ready) {
4633                 data = BXE_SP(sc, wb_data[0]);
4634
4635                 DBPRINT(sc, BXE_WARN, "%s(): DMAE not ready, "
4636                     "using indirect.\n", __FUNCTION__);
4637
4638                 bxe_init_ind_wr(sc, dst_addr, data, len32);
4639                 goto bxe_write_dmae_exit;
4640         }
4641
4642         memset(&dmae, 0, sizeof(struct dmae_command));
4643
4644         dmae.opcode = (DMAE_CMD_SRC_PCI | DMAE_CMD_DST_GRC |
4645             DMAE_CMD_C_DST_PCI | DMAE_CMD_C_ENABLE |
4646             DMAE_CMD_SRC_RESET | DMAE_CMD_DST_RESET |
4647 #ifdef __BIG_ENDIAN
4648             DMAE_CMD_ENDIANITY_B_DW_SWAP |
4649 #else
4650             DMAE_CMD_ENDIANITY_DW_SWAP |
4651 #endif
4652             (BP_PORT(sc) ? DMAE_CMD_PORT_1 : DMAE_CMD_PORT_0) |
4653             (BP_E1HVN(sc) << DMAE_CMD_E1HVN_SHIFT));
4654         dmae.src_addr_lo = U64_LO(dma_addr);
4655         dmae.src_addr_hi = U64_HI(dma_addr);
4656         dmae.dst_addr_lo = dst_addr >> 2;
4657         dmae.dst_addr_hi = 0;
4658         dmae.len = len32;
4659         dmae.comp_addr_lo = U64_LO(BXE_SP_MAPPING(sc, wb_comp));
4660         dmae.comp_addr_hi = U64_HI(BXE_SP_MAPPING(sc, wb_comp));
4661         dmae.comp_val = BXE_WB_COMP_VAL;
4662
4663         BXE_DMAE_LOCK(sc);
4664
4665         *wb_comp = 0;
4666
4667         bxe_post_dmae(sc, &dmae, INIT_DMAE_C(sc));
4668
4669         DELAY(50);
4670
4671         /* Wait up to 200ms. */
4672         timeout = 4000;
4673         while (*wb_comp != BXE_WB_COMP_VAL) {
4674                 if (!timeout) {
4675                         DBPRINT(sc, BXE_FATAL,
4676                         "%s(): DMAE timeout (dst_addr = 0x%08X, len = %d)!\n",
4677                             __FUNCTION__, dst_addr, len32);
4678                         break;
4679                 }
4680                 timeout--;
4681                 DELAY(50);
4682         }
4683
4684         BXE_DMAE_UNLOCK(sc);
4685
4686 bxe_write_dmae_exit:
4687         DBEXIT(BXE_INSANE_REGS);
4688 }
4689
4690
4691 /*
4692  * Perform a DMAE read from to device memory.
4693  *
4694  * Some of the registers on the 577XX controller are 128bits wide.  It is
4695  * required that when accessing those registers that they be read
4696  * atomically and that no intervening bus acceses to the device occur.
4697  * This could be handled by a lock held across all driver instances for
4698  * the device or it can be handled by performing a DMA operation when
4699  * reading from the device.  This code implements the latter.
4700  *
4701  * Returns:
4702  *   None.
4703  */
4704 void
4705 bxe_read_dmae(struct bxe_softc *sc, uint32_t src_addr,
4706     uint32_t len32)
4707 {
4708         struct dmae_command dmae;
4709         uint32_t *data, *wb_comp;
4710         int i, timeout;
4711
4712         DBENTER(BXE_INSANE_REGS);
4713
4714         wb_comp = BXE_SP(sc, wb_comp);
4715         /* Fall back to indirect access if DMAE is not ready. */
4716         if (!sc->dmae_ready) {
4717                 data = BXE_SP(sc, wb_data[0]);
4718
4719                 DBPRINT(sc, BXE_WARN, "%s(): DMAE not ready, "
4720                     "using indirect.\n", __FUNCTION__);
4721
4722                 for (i = 0; i < len32; i++)
4723                         data[i] = bxe_reg_rd_ind(sc, src_addr + i * 4);
4724
4725                 goto bxe_read_dmae_exit;
4726         }
4727
4728         memset(&dmae, 0, sizeof(struct dmae_command));
4729
4730         dmae.opcode = (DMAE_CMD_SRC_GRC | DMAE_CMD_DST_PCI |
4731             DMAE_CMD_C_DST_PCI | DMAE_CMD_C_ENABLE |
4732             DMAE_CMD_SRC_RESET | DMAE_CMD_DST_RESET |
4733 #ifdef __BIG_ENDIAN
4734             DMAE_CMD_ENDIANITY_B_DW_SWAP |
4735 #else
4736             DMAE_CMD_ENDIANITY_DW_SWAP |
4737 #endif
4738             (BP_PORT(sc) ? DMAE_CMD_PORT_1 : DMAE_CMD_PORT_0) |
4739             (BP_E1HVN(sc) << DMAE_CMD_E1HVN_SHIFT));
4740
4741         dmae.src_addr_lo = src_addr >> 2;
4742         dmae.src_addr_hi = 0;
4743         dmae.dst_addr_lo = U64_LO(BXE_SP_MAPPING(sc, wb_data));
4744         dmae.dst_addr_hi = U64_HI(BXE_SP_MAPPING(sc, wb_data));
4745         dmae.len = len32;
4746         dmae.comp_addr_lo = U64_LO(BXE_SP_MAPPING(sc, wb_comp));
4747         dmae.comp_addr_hi = U64_HI(BXE_SP_MAPPING(sc, wb_comp));
4748         dmae.comp_val = BXE_WB_COMP_VAL;
4749
4750         BXE_DMAE_LOCK(sc);
4751
4752         memset(BXE_SP(sc, wb_data[0]), 0, sizeof(uint32_t) * 4);
4753         *wb_comp = 0;
4754
4755         bxe_post_dmae(sc, &dmae, INIT_DMAE_C(sc));
4756
4757         DELAY(50);
4758
4759         timeout = 4000;
4760         while (*wb_comp != BXE_WB_COMP_VAL) {
4761                 if (!timeout) {
4762                         DBPRINT(sc, BXE_FATAL,
4763                         "%s(): DMAE timeout (src_addr = 0x%08X, len = %d)!\n",
4764                             __FUNCTION__, src_addr, len32);
4765                         break;
4766                 }
4767                 timeout--;
4768                 DELAY(50);
4769         }
4770
4771         BXE_DMAE_UNLOCK(sc);
4772
4773 bxe_read_dmae_exit:
4774         DBEXIT(BXE_INSANE_REGS);
4775 }
4776
4777 /*
4778  * DMAE write wrapper.
4779  *
4780  * Returns:
4781  *   None.
4782  */
4783 static void
4784 bxe_wb_wr(struct bxe_softc *sc, int reg, uint32_t val_hi, uint32_t val_lo)
4785 {
4786         uint32_t wb_write[2];
4787
4788         wb_write[0] = val_hi;
4789         wb_write[1] = val_lo;
4790         REG_WR_DMAE(sc, reg, wb_write, 2);
4791 }
4792
4793
4794
4795 /*
4796  * Poll a register waiting for a value.
4797  *
4798  * Returns:
4799  *   The last read register value.
4800  */
4801 static __inline
4802 uint32_t bxe_reg_poll(struct bxe_softc *sc, uint32_t reg, uint32_t expected,
4803     int ms, int wait)
4804 {
4805         uint32_t val;
4806
4807         do {
4808                 val = REG_RD(sc, reg);
4809                 if (val == expected)
4810                         break;
4811                 ms -= wait;
4812                 DELAY(wait * 1000);
4813
4814         } while (ms > 0);
4815
4816         return (val);
4817 }
4818
4819
4820 /*
4821  * Microcode assert display.
4822  *
4823  * This function walks through each STORM processor and prints out a
4824  * listing of all asserts currently in effect.  Useful for post-mortem
4825  * debugging.
4826  *
4827  * Returns:
4828  *   The number of asserts detected.
4829  */
4830 static int
4831 bxe_mc_assert(struct bxe_softc *sc)
4832 {
4833         uint32_t row0, row1, row2, row3;
4834         char last_idx;
4835         int i, rc;
4836
4837         DBENTER(BXE_VERBOSE_INTR);
4838
4839         rc = 0;
4840         /* XSTORM */
4841         last_idx = REG_RD8(sc, BAR_XSTORM_INTMEM +
4842             XSTORM_ASSERT_LIST_INDEX_OFFSET);
4843
4844         if (last_idx)
4845                 DBPRINT(sc, BXE_FATAL, "DATA XSTORM_ASSERT_LIST_INDEX 0x%x\n",
4846                     last_idx);
4847
4848         /* Print the asserts */
4849         for (i = 0; i < STORM_ASSERT_ARRAY_SIZE; i++) {
4850
4851                 row0 = REG_RD(sc, BAR_XSTORM_INTMEM +
4852                     XSTORM_ASSERT_LIST_OFFSET(i));
4853                 row1 = REG_RD(sc, BAR_XSTORM_INTMEM +
4854                     XSTORM_ASSERT_LIST_OFFSET(i) + 4);
4855                 row2 = REG_RD(sc, BAR_XSTORM_INTMEM +
4856                     XSTORM_ASSERT_LIST_OFFSET(i) + 8);
4857                 row3 = REG_RD(sc, BAR_XSTORM_INTMEM +
4858                     XSTORM_ASSERT_LIST_OFFSET(i) + 12);
4859
4860                 if (row0 != COMMON_ASM_INVALID_ASSERT_OPCODE) {
4861                         DBPRINT(sc, BXE_FATAL, "DATA XSTORM_ASSERT_INDEX %d = "
4862                             "0x%08x 0x%08x 0x%08x 0x%08x\n", i, row3, row2,
4863                             row1, row0);
4864                         rc++;
4865                 } else
4866                         break;
4867         }
4868
4869         /* TSTORM */
4870         last_idx = REG_RD8(sc, BAR_TSTORM_INTMEM +
4871             TSTORM_ASSERT_LIST_INDEX_OFFSET);
4872
4873         if (last_idx)
4874                 DBPRINT(sc, BXE_FATAL, "DATA TSTORM_ASSERT_LIST_INDEX 0x%x\n",
4875                         last_idx);
4876
4877         /* Print the asserts */
4878         for (i = 0; i < STORM_ASSERT_ARRAY_SIZE; i++) {
4879
4880                 row0 = REG_RD(sc, BAR_TSTORM_INTMEM +
4881                     TSTORM_ASSERT_LIST_OFFSET(i));
4882                 row1 = REG_RD(sc, BAR_TSTORM_INTMEM +
4883                     TSTORM_ASSERT_LIST_OFFSET(i) + 4);
4884                 row2 = REG_RD(sc, BAR_TSTORM_INTMEM +
4885                     TSTORM_ASSERT_LIST_OFFSET(i) + 8);
4886                 row3 = REG_RD(sc, BAR_TSTORM_INTMEM +
4887                     TSTORM_ASSERT_LIST_OFFSET(i) + 12);
4888
4889                 if (row0 != COMMON_ASM_INVALID_ASSERT_OPCODE) {
4890                         DBPRINT(sc, BXE_FATAL, "DATA TSTORM_ASSERT_INDEX %d = "
4891                             "0x%08x 0x%08x 0x%08x 0x%08x\n", i, row3, row2,
4892                             row1, row0);
4893                         rc++;
4894                 } else
4895                         break;
4896         }
4897
4898         /* CSTORM */
4899         last_idx = REG_RD8(sc, BAR_CSTORM_INTMEM +
4900             CSTORM_ASSERT_LIST_INDEX_OFFSET);
4901
4902         if (last_idx)
4903                 DBPRINT(sc, BXE_FATAL, "DATA CSTORM_ASSERT_LIST_INDEX 0x%x\n",
4904                     last_idx);
4905
4906         /* Print the asserts */
4907         for (i = 0; i < STORM_ASSERT_ARRAY_SIZE; i++) {
4908
4909                 row0 = REG_RD(sc, BAR_CSTORM_INTMEM +
4910                     CSTORM_ASSERT_LIST_OFFSET(i));
4911                 row1 = REG_RD(sc, BAR_CSTORM_INTMEM +
4912                     CSTORM_ASSERT_LIST_OFFSET(i) + 4);
4913                 row2 = REG_RD(sc, BAR_CSTORM_INTMEM +
4914                     CSTORM_ASSERT_LIST_OFFSET(i) + 8);
4915                 row3 = REG_RD(sc, BAR_CSTORM_INTMEM +
4916                     CSTORM_ASSERT_LIST_OFFSET(i) + 12);
4917
4918                 if (row0 != COMMON_ASM_INVALID_ASSERT_OPCODE) {
4919                         DBPRINT(sc, BXE_FATAL, "DATA CSTORM_ASSERT_INDEX %d = "
4920                             "0x%08x 0x%08x 0x%08x 0x%08x\n", i, row3, row2,
4921                             row1, row0);
4922                         rc++;
4923                 } else
4924                         break;
4925         }
4926
4927         /* USTORM */
4928         last_idx = REG_RD8(sc, BAR_USTORM_INTMEM +
4929             USTORM_ASSERT_LIST_INDEX_OFFSET);
4930
4931         if (last_idx)
4932                 DBPRINT(sc, BXE_FATAL, "DATA USTORM_ASSERT_LIST_INDEX 0x%x\n",
4933                     last_idx);
4934
4935         /* Print the asserts */
4936         for (i = 0; i < STORM_ASSERT_ARRAY_SIZE; i++) {
4937
4938                 row0 = REG_RD(sc, BAR_USTORM_INTMEM +
4939                     USTORM_ASSERT_LIST_OFFSET(i));
4940                 row1 = REG_RD(sc, BAR_USTORM_INTMEM +
4941                     USTORM_ASSERT_LIST_OFFSET(i) + 4);
4942                 row2 = REG_RD(sc, BAR_USTORM_INTMEM +
4943                     USTORM_ASSERT_LIST_OFFSET(i) + 8);
4944                 row3 = REG_RD(sc, BAR_USTORM_INTMEM +
4945                     USTORM_ASSERT_LIST_OFFSET(i) + 12);
4946
4947                 if (row0 != COMMON_ASM_INVALID_ASSERT_OPCODE) {
4948                         DBPRINT(sc, BXE_FATAL, "DATA USTORM_ASSERT_INDEX %d = "
4949                             "0x%08x 0x%08x 0x%08x 0x%08x\n", i, row3, row2,
4950                             row1, row0);
4951                         rc++;
4952                 } else
4953                         break;
4954         }
4955
4956         DBEXIT(BXE_VERBOSE_INTR);
4957         return (rc);
4958 }
4959
4960
4961 /*
4962  * Perform a panic dump.
4963  *
4964  * Returns:
4965  *   None
4966  */
4967 static void
4968 bxe_panic_dump(struct bxe_softc *sc)
4969 {
4970         DBENTER(BXE_FATAL);
4971
4972         sc->stats_state = STATS_STATE_DISABLED;
4973
4974         BXE_PRINTF("---------- Begin crash dump ----------\n");
4975
4976         /* Idle check is run twice to verify the controller has stopped. */
4977         bxe_idle_chk(sc);
4978         bxe_idle_chk(sc);
4979         bxe_mc_assert(sc);
4980
4981 #ifdef BXE_DEBUG
4982         bxe_breakpoint(sc);
4983 #endif
4984
4985         BXE_PRINTF("----------  End crash dump  ----------\n");
4986
4987         DBEXIT(BXE_FATAL);
4988 }
4989
4990
4991 /*
4992  * Enables interrupt generation.
4993  *
4994  * Returns:
4995  *   None.
4996  */
4997 static void
4998 bxe_int_enable(struct bxe_softc *sc)
4999 {
5000         uint32_t hc_addr, val;
5001         int port;
5002
5003         DBENTER(BXE_VERBOSE_INTR);
5004
5005         port = BP_PORT(sc);
5006         hc_addr = port ? HC_REG_CONFIG_1 : HC_REG_CONFIG_0;
5007         val = REG_RD(sc, hc_addr);
5008         if (sc->msix_count > 0) {
5009                 if (sc->msix_count == 1) {
5010
5011                         /* Single interrupt, multiple queues.*/
5012                         DBPRINT(sc, BXE_VERBOSE_INTR,
5013                 "%s(): Setting host coalescing registers for MSI-X (SIMQ).\n",
5014                             __FUNCTION__);
5015
5016                         /* Clear INTx. */
5017                         val &= ~HC_CONFIG_0_REG_INT_LINE_EN_0;
5018
5019                         /* Enable single ISR mode, MSI/MSI-X, and attention messages. */
5020                         val |= (HC_CONFIG_0_REG_SINGLE_ISR_EN_0 |
5021                             HC_CONFIG_0_REG_MSI_MSIX_INT_EN_0 |
5022                             HC_CONFIG_0_REG_ATTN_BIT_EN_0);
5023                 } else {
5024
5025                         /* Multiple interrupts, multiple queues.*/
5026                         DBPRINT(sc, BXE_VERBOSE_INTR,
5027                 "%s(): Setting host coalescing registers for MSI-X (MIMQ).\n",
5028                             __FUNCTION__);
5029
5030                         /* Clear single ISR mode and INTx. */
5031                         val &= ~(HC_CONFIG_0_REG_SINGLE_ISR_EN_0 |
5032                             HC_CONFIG_0_REG_INT_LINE_EN_0);
5033
5034                         /* Enable MSI/MSI-X and attention messages. */
5035                         val |= (HC_CONFIG_0_REG_MSI_MSIX_INT_EN_0 |
5036                             HC_CONFIG_0_REG_ATTN_BIT_EN_0);
5037                 }
5038
5039         } else if (sc->msi_count > 0) {
5040
5041                 if (sc->msi_count == 1) {
5042
5043                         /* Single interrupt, multiple queues.*/
5044                         DBPRINT(sc, BXE_VERBOSE_INTR,
5045                 "%s(): Setting host coalescing registers for MSI (SIMQ).\n",
5046                             __FUNCTION__);
5047
5048                         /* Clear INTx. */
5049                         val &= ~HC_CONFIG_0_REG_INT_LINE_EN_0;
5050
5051                         /* Enable single ISR mode, MSI/MSI-X, and attention
5052                          * messages.
5053                          */
5054                         val |= (HC_CONFIG_0_REG_SINGLE_ISR_EN_0 |
5055                             HC_CONFIG_0_REG_MSI_MSIX_INT_EN_0 |
5056                             HC_CONFIG_0_REG_ATTN_BIT_EN_0);
5057                 } else {
5058                         /* Multiple interrupts, multiple queues.*/
5059                         DBPRINT(sc, BXE_VERBOSE_INTR,
5060                             "%s(): Setting host coalescing registers for"
5061                             "MSI (MIMQ).\n",
5062                             __FUNCTION__);
5063
5064                         /* Clear single ISR mode and INTx. */
5065                         val &= ~(HC_CONFIG_0_REG_SINGLE_ISR_EN_0 |
5066                             HC_CONFIG_0_REG_INT_LINE_EN_0);
5067
5068                         /* Enable MSI/MSI-X and attention messages. */
5069                         val |= (HC_CONFIG_0_REG_MSI_MSIX_INT_EN_0 |
5070                             HC_CONFIG_0_REG_ATTN_BIT_EN_0);
5071                 }
5072         } else {
5073                 /* Single interrupt, single queue. */
5074                 DBPRINT(sc, BXE_VERBOSE_INTR,
5075                     "%s(): Setting host coalescing registers for INTA#.\n",
5076                     __FUNCTION__);
5077
5078                 val |= (HC_CONFIG_0_REG_SINGLE_ISR_EN_0   |
5079                     HC_CONFIG_0_REG_MSI_MSIX_INT_EN_0 |
5080                     HC_CONFIG_0_REG_INT_LINE_EN_0     |
5081                     HC_CONFIG_0_REG_ATTN_BIT_EN_0);
5082                 REG_WR(sc, hc_addr, val);
5083
5084                 val &= ~HC_CONFIG_0_REG_MSI_MSIX_INT_EN_0;
5085         }
5086
5087         /* Write the interrupt mode to the host coalescing block. */
5088         REG_WR(sc, hc_addr, val);
5089
5090         if (CHIP_IS_E1H(sc)) {
5091
5092                 /* Init leading/trailing edge attention generation. */
5093                 if (IS_E1HMF(sc)) {
5094                         val = (0xee0f | (1 << (BP_E1HVN(sc) + 4)));
5095
5096                         /*
5097                          * Check if this driver instance is the port
5098                          * master function.
5099                          */
5100                         if (sc->port.pmf)
5101                                 /* Enable nig & GPIO3 attentions. */
5102                                 val |= 0x1100;
5103                 } else
5104                         val = 0xffff;
5105
5106                 REG_WR(sc, HC_REG_TRAILING_EDGE_0 + port * 8, val);
5107                 REG_WR(sc, HC_REG_LEADING_EDGE_0 + port * 8, val);
5108         }
5109
5110         DBEXIT(BXE_VERBOSE_INTR);
5111 }
5112
5113
5114 /*
5115  * Disables interrupt generation.
5116  *
5117  * Returns:
5118  *   None.
5119  */
5120 static void
5121 bxe_int_disable(struct bxe_softc *sc)
5122 {
5123         uint32_t hc_addr, val;
5124         int port;
5125
5126         DBENTER(BXE_VERBOSE_INTR | BXE_VERBOSE_RESET | BXE_VERBOSE_UNLOAD);
5127
5128         port = BP_PORT(sc);
5129         hc_addr = port ? HC_REG_CONFIG_1 : HC_REG_CONFIG_0;
5130         val = REG_RD(sc, hc_addr);
5131
5132         val &= ~(HC_CONFIG_0_REG_MSI_MSIX_INT_EN_0 |
5133             HC_CONFIG_0_REG_INT_LINE_EN_0 | HC_CONFIG_0_REG_ATTN_BIT_EN_0);
5134
5135         REG_WR(sc, hc_addr, val);
5136
5137         if (REG_RD(sc, hc_addr)!= val) {
5138                 DBPRINT(sc, BXE_WARN, "%s(): BUG! Returned value from IGU "
5139                     "doesn't match value written (0x%08X).\n",
5140                     __FUNCTION__, val);
5141         }
5142
5143         DBEXIT(BXE_VERBOSE_INTR | BXE_VERBOSE_RESET | BXE_VERBOSE_UNLOAD);
5144 }
5145
5146 #define BXE_CRC32_RESIDUAL      0xdebb20e3
5147
5148 /*
5149  * Returns:
5150  *   0 = Success, !0 = Failure.
5151  */
5152 static int
5153 bxe_nvram_acquire_lock(struct bxe_softc *sc)
5154 {
5155         uint32_t val;
5156         int i, port, rc;
5157
5158         DBENTER(BXE_VERBOSE_NVRAM);
5159
5160         port = BP_PORT(sc);
5161         rc = 0;
5162         val = 0;
5163
5164         /* Acquire the NVRAM lock. */
5165         REG_WR(sc, MCP_REG_MCPR_NVM_SW_ARB,
5166             (MCPR_NVM_SW_ARB_ARB_REQ_SET1 << port));
5167
5168         for (i = 0; i < NVRAM_TIMEOUT_COUNT * 10; i++) {
5169                 val = REG_RD(sc, MCP_REG_MCPR_NVM_SW_ARB);
5170                 if (val & (MCPR_NVM_SW_ARB_ARB_ARB1 << port))
5171                         break;
5172
5173                 DELAY(5);
5174         }
5175
5176         if (!(val & (MCPR_NVM_SW_ARB_ARB_ARB1 << port))) {
5177                 DBPRINT(sc, BXE_WARN, "%s(): Cannot acquire NVRAM lock!\n",
5178                     __FUNCTION__);
5179                 rc = EBUSY;
5180         }
5181
5182         DBEXIT(BXE_VERBOSE_NVRAM);
5183         return (rc);
5184 }
5185
5186 /*
5187  * Returns:
5188  *   0 = Success, !0 = Failure.
5189  */
5190 static int
5191 bxe_nvram_release_lock(struct bxe_softc *sc)
5192 {
5193         uint32_t val;
5194         int i, port, rc;
5195
5196         DBENTER(BXE_VERBOSE_NVRAM);
5197
5198         port = BP_PORT(sc);
5199         rc = 0;
5200         val = 0;
5201
5202         /* Release the NVRAM lock. */
5203         REG_WR(sc, MCP_REG_MCPR_NVM_SW_ARB,
5204             (MCPR_NVM_SW_ARB_ARB_REQ_CLR1 << port));
5205
5206         for (i = 0; i < NVRAM_TIMEOUT_COUNT * 10; i++) {
5207                 val = REG_RD(sc, MCP_REG_MCPR_NVM_SW_ARB);
5208                 if (!(val & (MCPR_NVM_SW_ARB_ARB_ARB1 << port)))
5209                         break;
5210
5211                 DELAY(5);
5212         }
5213
5214         if (val & (MCPR_NVM_SW_ARB_ARB_ARB1 << port)) {
5215                 DBPRINT(sc, BXE_WARN, "%s(): Cannot release NVRAM lock!\n",
5216                     __FUNCTION__);
5217                 rc = EBUSY;
5218         }
5219
5220         DBEXIT(BXE_VERBOSE_NVRAM);
5221         return (rc);
5222 }
5223
5224 /*
5225  * Returns:
5226  *   None.
5227  */
5228 static void
5229 bxe_nvram_enable_access(struct bxe_softc *sc)
5230 {
5231         uint32_t val;
5232
5233         DBENTER(BXE_VERBOSE_NVRAM);
5234
5235         val = REG_RD(sc, MCP_REG_MCPR_NVM_ACCESS_ENABLE);
5236
5237         /* Enable both bits, even on read */
5238         REG_WR(sc, MCP_REG_MCPR_NVM_ACCESS_ENABLE,
5239             (val | MCPR_NVM_ACCESS_ENABLE_EN |
5240              MCPR_NVM_ACCESS_ENABLE_WR_EN));
5241
5242         DBEXIT(BXE_VERBOSE_NVRAM);
5243 }
5244
5245 /*
5246  * Returns:
5247  *   None.
5248  */
5249 static void
5250 bxe_nvram_disable_access(struct bxe_softc *sc)
5251 {
5252         uint32_t val;
5253
5254         DBENTER(BXE_VERBOSE_NVRAM);
5255
5256         val = REG_RD(sc, MCP_REG_MCPR_NVM_ACCESS_ENABLE);
5257
5258         /* Disable both bits, even after read. */
5259         REG_WR(sc, MCP_REG_MCPR_NVM_ACCESS_ENABLE,
5260             (val & ~(MCPR_NVM_ACCESS_ENABLE_EN |
5261             MCPR_NVM_ACCESS_ENABLE_WR_EN)));
5262
5263         DBEXIT(BXE_VERBOSE_NVRAM);
5264 }
5265
5266 /*
5267  * Returns:
5268  *   0 = Success, !0 = Failure.
5269  */
5270 static int
5271 bxe_nvram_read_dword(struct bxe_softc *sc, uint32_t offset, uint32_t *ret_val,
5272     uint32_t cmd_flags)
5273 {
5274         uint32_t val;
5275         int i, rc;
5276
5277         DBENTER(BXE_INSANE_NVRAM);
5278
5279         /* Build the command word. */
5280         cmd_flags |= MCPR_NVM_COMMAND_DOIT;
5281
5282         /* Need to clear DONE bit separately. */
5283         REG_WR(sc, MCP_REG_MCPR_NVM_COMMAND, MCPR_NVM_COMMAND_DONE);
5284
5285         /* Address within the NVRAM to read. */
5286         REG_WR(sc, MCP_REG_MCPR_NVM_ADDR,
5287                 (offset & MCPR_NVM_ADDR_NVM_ADDR_VALUE));
5288
5289         /* Issue a read command. */
5290         REG_WR(sc, MCP_REG_MCPR_NVM_COMMAND, cmd_flags);
5291
5292         /* Wait for completion. */
5293         *ret_val = 0;
5294         rc = EBUSY;
5295         for (i = 0; i < NVRAM_TIMEOUT_COUNT; i++) {
5296                 DELAY(5);
5297                 val = REG_RD(sc, MCP_REG_MCPR_NVM_COMMAND);
5298
5299                 if (val & MCPR_NVM_COMMAND_DONE) {
5300                         val = REG_RD(sc, MCP_REG_MCPR_NVM_READ);
5301                         val = htobe32(val);
5302                         *ret_val = val;
5303                         rc = 0;
5304                         break;
5305                 }
5306         }
5307
5308         DBPRINT(sc, BXE_INSANE_NVRAM, "%s(): Read 0x%08X from offset 0x%08X.\n",
5309             __FUNCTION__, *ret_val, offset);
5310         DBEXIT(BXE_INSANE_NVRAM);
5311         return (rc);
5312 }
5313
5314 /*
5315  * Returns:
5316  *   0 = Success, !0 = Failure.
5317  */
5318 static int
5319 bxe_nvram_read(struct bxe_softc *sc, uint32_t offset, uint8_t *ret_buf,
5320     int buf_size)
5321 {
5322         uint32_t cmd_flags, val;
5323         int rc;
5324
5325         DBENTER(BXE_EXTREME_NVRAM);
5326
5327         if ((offset & 0x03) || (buf_size & 0x03) || (buf_size == 0)) {
5328                 DBPRINT(sc, BXE_WARN, "%s(): Unaligned address or invalid "
5329                     "buffer for NVRAM read (offset = 0x%08X, buf_size = %d)!\n",
5330                     __FUNCTION__, offset, buf_size);
5331                 rc = EINVAL;
5332                 goto bxe_nvram_read_exit;
5333         }
5334
5335         if (offset + buf_size > sc->common.flash_size) {
5336                 DBPRINT(sc, BXE_WARN, "%s(): Read extends beyond the end of "
5337                     "the NVRAM (offset (0x%08X) + buf_size (%d) > flash_size "
5338                     "(0x%08X))!\n", __FUNCTION__, offset, buf_size,
5339                     sc->common.flash_size);
5340                 rc = EINVAL;
5341                 goto bxe_nvram_read_exit;
5342         }
5343
5344         rc = bxe_nvram_acquire_lock(sc);
5345         if (rc)
5346                 goto bxe_nvram_read_exit;
5347
5348         bxe_nvram_enable_access(sc);
5349
5350         /* Read the first word(s). */
5351         cmd_flags = MCPR_NVM_COMMAND_FIRST;
5352         while ((buf_size > sizeof(uint32_t)) && (rc == 0)) {
5353                 rc = bxe_nvram_read_dword(sc, offset, &val, cmd_flags);
5354                 memcpy(ret_buf, &val, 4);
5355
5356                 /* Advance to the next DWORD. */
5357                 offset += sizeof(uint32_t);
5358                 ret_buf += sizeof(uint32_t);
5359                 buf_size -= sizeof(uint32_t);
5360                 cmd_flags = 0;
5361         }
5362
5363         /* Read the final word. */
5364         if (rc == 0) {
5365                 cmd_flags |= MCPR_NVM_COMMAND_LAST;
5366                 rc = bxe_nvram_read_dword(sc, offset, &val, cmd_flags);
5367                 memcpy(ret_buf, &val, 4);
5368         }
5369
5370         /* Disable access to NVRAM interface. */
5371         bxe_nvram_disable_access(sc);
5372         bxe_nvram_release_lock(sc);
5373
5374 bxe_nvram_read_exit:
5375         DBEXIT(BXE_EXTREME_NVRAM);
5376         return (rc);
5377 }
5378
5379 #ifdef BXE_NVRAM_WRITE_SUPPORT
5380 /*
5381  * Returns:
5382  *   0 = Success, !0 = Failure.
5383  */
5384 static int
5385 bxe_nvram_write_dword(struct bxe_softc *sc, uint32_t offset, uint32_t val,
5386 uint32_t cmd_flags)
5387 {
5388         int i, rc;
5389
5390         DBENTER(BXE_VERBOSE_NVRAM);
5391
5392         /* Build the command word. */
5393         cmd_flags |= MCPR_NVM_COMMAND_DOIT | MCPR_NVM_COMMAND_WR;
5394
5395         /* Need to clear DONE bit separately. */
5396         REG_WR(sc, MCP_REG_MCPR_NVM_COMMAND, MCPR_NVM_COMMAND_DONE);
5397
5398         /* Write the data. */
5399         REG_WR(sc, MCP_REG_MCPR_NVM_WRITE, val);
5400
5401         /* Address to write within the NVRAM. */
5402         REG_WR(sc, MCP_REG_MCPR_NVM_ADDR,
5403                 (offset & MCPR_NVM_ADDR_NVM_ADDR_VALUE));
5404
5405         /* Issue the write command. */
5406         REG_WR(sc, MCP_REG_MCPR_NVM_COMMAND, cmd_flags);
5407
5408         /* Wait for completion. */
5409         rc = EBUSY;
5410         for (i = 0; i < NVRAM_TIMEOUT_COUNT; i++) {
5411                 DELAY(5);
5412                 val = REG_RD(sc, MCP_REG_MCPR_NVM_COMMAND);
5413                 if (val & MCPR_NVM_COMMAND_DONE) {
5414                         rc = 0;
5415                         break;
5416                 }
5417         }
5418
5419         DBEXIT(BXE_VERBOSE_NVRAM);
5420         return (rc);
5421 }
5422
5423 #define BYTE_OFFSET(offset)             (8 * (offset & 0x03))
5424
5425 /*
5426  * Returns:
5427  *
5428  */
5429 static int
5430 bxe_nvram_write1(struct bxe_softc *sc, uint32_t offset, uint8_t *data_buf,
5431     int buf_size)
5432 {
5433         uint32_t align_offset, cmd_flags, val;
5434         int rc;
5435
5436         DBENTER(BXE_VERBOSE_NVRAM);
5437
5438         if (offset + buf_size > sc->common.flash_size) {
5439                 DBPRINT(sc, BXE_WARN, "%s(): Write extends beyond the end of "
5440                     "the NVRAM (offset (0x%08X) + buf_size (%d) > flash_size "
5441                     "(0x%08X))!\n", __FUNCTION__, offset, buf_size,
5442                     sc->common.flash_size);
5443                 rc = EINVAL;
5444                 goto bxe_nvram_write1_exit;
5445         }
5446
5447         /* request access to nvram interface */
5448         rc = bxe_nvram_acquire_lock(sc);
5449         if (rc)
5450                 goto bxe_nvram_write1_exit;
5451
5452         /* Enable access to the NVRAM interface. */
5453         bxe_nvram_enable_access(sc);
5454
5455         cmd_flags = (MCPR_NVM_COMMAND_FIRST | MCPR_NVM_COMMAND_LAST);
5456         align_offset = (offset & ~0x03);
5457         rc = bxe_nvram_read_dword(sc, align_offset, &val, cmd_flags);
5458
5459         if (rc == 0) {
5460                 val &= ~(0xff << BYTE_OFFSET(offset));
5461                 val |= (*data_buf << BYTE_OFFSET(offset));
5462
5463                 val = be32toh(val);
5464                 rc = bxe_nvram_write_dword(sc, align_offset, val, cmd_flags);
5465         }
5466
5467         /* Disable access to the NVRAM interface. */
5468         bxe_nvram_disable_access(sc);
5469         bxe_nvram_release_lock(sc);
5470
5471 bxe_nvram_write1_exit:
5472         DBEXIT(BXE_VERBOSE_NVRAM);
5473         return (rc);
5474 }
5475
5476 /*
5477  * Returns:
5478  *   0 = Success, !0 = Failure.
5479  */
5480 static int
5481 bxe_nvram_write(struct bxe_softc *sc, uint32_t offset, uint8_t *data_buf,
5482     int buf_size)
5483 {
5484         uint32_t cmd_flags, val, written_so_far;
5485         int rc;
5486
5487         rc = 0;
5488
5489         if (buf_size == 1)
5490                 return (bxe_nvram_write1(sc, offset, data_buf, buf_size));
5491
5492         if ((offset & 0x03) || (buf_size & 0x03) || (buf_size == 0)) {
5493                 DBPRINT(sc, BXE_WARN, "%s(): Unaligned address or invalid "
5494                     "buffer for NVRAM write "
5495                     "(offset = 0x%08X, buf_size = %d)!\n", __FUNCTION__,
5496                     offset, buf_size);
5497                 rc = EINVAL;
5498                 goto bxe_nvram_write_exit;
5499         }
5500
5501         if (offset + buf_size > sc->common.flash_size) {
5502                 DBPRINT(sc, BXE_WARN, "%s(): Write extends beyond the end of "
5503                     "the NVRAM (offset (0x%08X) + buf_size (%d) > flash_size "
5504                     "(0x%08X))!\n", __FUNCTION__, offset, buf_size,
5505                     sc->common.flash_size);
5506                 rc = EINVAL;
5507                 goto bxe_nvram_write_exit;
5508         }
5509
5510         /* Request access to NVRAM interface. */
5511         rc = bxe_nvram_acquire_lock(sc);
5512         if (rc)
5513                 goto bxe_nvram_write_exit;
5514
5515         /* Enable access to the NVRAM interface. */
5516         bxe_nvram_enable_access(sc);
5517
5518         written_so_far = 0;
5519         cmd_flags = MCPR_NVM_COMMAND_FIRST;
5520         while ((written_so_far < buf_size) && (rc == 0)) {
5521                 if (written_so_far == (buf_size - sizeof(uint32_t)))
5522                         cmd_flags |= MCPR_NVM_COMMAND_LAST;
5523                 else if (((offset + 4) % NVRAM_PAGE_SIZE) == 0)
5524                         cmd_flags |= MCPR_NVM_COMMAND_LAST;
5525                 else if ((offset % NVRAM_PAGE_SIZE) == 0)
5526                         cmd_flags |= MCPR_NVM_COMMAND_FIRST;
5527
5528                 memcpy(&val, data_buf, 4);
5529
5530                 rc = bxe_nvram_write_dword(sc, offset, val, cmd_flags);
5531
5532                 /* Advance to the next DWORD. */
5533                 offset += sizeof(uint32_t);
5534                 data_buf += sizeof(uint32_t);
5535                 written_so_far += sizeof(uint32_t);
5536                 cmd_flags = 0;
5537         }
5538
5539         /* Disable access to the NVRAM interface. */
5540         bxe_nvram_disable_access(sc);
5541         bxe_nvram_release_lock(sc);
5542
5543 bxe_nvram_write_exit:
5544         DBEXIT(BXE_VERBOSE_NVRAM);
5545         return (rc);
5546 }
5547 #endif
5548
5549 /*
5550  * This function validates NVRAM content by reading spcific
5551  * regions and validating that the NVRAM checksum matches the
5552  * actual content.
5553  *
5554  * Returns:
5555  *   0 = Success, !0 = Failure.
5556  */
5557 static int
5558 bxe_nvram_test(struct bxe_softc *sc)
5559 {
5560         static const struct {
5561                 int offset;
5562                 int size;
5563         } nvram_tbl[] = {
5564                 {     0,  0x14 }, /* bootstrap area*/
5565                 {  0x14,  0xec }, /* directory area */
5566                 { 0x100, 0x350 }, /* manuf_info */
5567                 { 0x450,  0xf0 }, /* feature_info */
5568                 { 0x640,  0x64 }, /* upgrade_key_info */
5569                 { 0x708,  0x70 }, /* manuf_key_info */
5570                 {     0,     0 }
5571         };
5572         uint32_t magic, csum, buf[0x350 / 4];
5573         uint8_t *data;
5574         int i, rc;
5575
5576         DBENTER(BXE_VERBOSE_NVRAM);
5577
5578         data = (uint8_t *) buf;
5579
5580         /* Read the DWORD at offset 0 in NVRAM. */
5581         rc = bxe_nvram_read(sc, 0, data, 4);
5582         if (rc) {
5583                 BXE_PRINTF("%s(%d): Error (%d) returned reading NVRAM!\n",
5584                     __FILE__, __LINE__, rc);
5585                 goto bxe_nvram_test_exit;
5586         }
5587
5588         /* Make sure we found our magic value. */
5589         magic = be32toh(buf[0]);
5590         if (magic != 0x669955aa) {
5591                 BXE_PRINTF("%s(%d): Invalid magic value (0x%08x) found!\n",
5592                     __FILE__, __LINE__, magic);
5593                 rc = ENODEV;
5594                 goto bxe_nvram_test_exit;
5595         }
5596
5597         /* Read through each region in NVRAM and validate the checksum. */
5598         for (i = 0; nvram_tbl[i].size; i++) {
5599                 DBPRINT(sc, BXE_VERBOSE_NVRAM, "%s(): Testing NVRAM region %d, "
5600                     "starting offset = %d, length = %d\n", __FUNCTION__, i,
5601                     nvram_tbl[i].offset, nvram_tbl[i].size);
5602
5603                 rc = bxe_nvram_read(sc, nvram_tbl[i].offset, data,
5604                         nvram_tbl[i].size);
5605                 if (rc) {
5606                         BXE_PRINTF("%s(%d): Error (%d) returned reading NVRAM "
5607                             "region %d!\n", __FILE__, __LINE__, rc, i);
5608                         goto bxe_nvram_test_exit;
5609                 }
5610
5611                 csum = ether_crc32_le(data, nvram_tbl[i].size);
5612                 if (csum != BXE_CRC32_RESIDUAL) {
5613                         BXE_PRINTF("%s(%d): Checksum error (0x%08X) for NVRAM "
5614                             "region %d!\n", __FILE__, __LINE__, csum, i);
5615                         rc = ENODEV;
5616                         goto bxe_nvram_test_exit;
5617                 }
5618         }
5619
5620 bxe_nvram_test_exit:
5621         DBEXIT(BXE_VERBOSE_NVRAM);
5622         return (rc);
5623 }
5624
5625 /*
5626  * Acknowledge status block and modify interrupt mode.
5627  *
5628  * Returns:
5629  *   None.
5630  */
5631 static __inline void
5632 bxe_ack_sb(struct bxe_softc *sc, uint8_t sb_id, uint8_t storm, uint16_t index,
5633     uint8_t int_mode, uint8_t update)
5634 {
5635         struct igu_ack_register igu_ack;
5636         uint32_t hc_addr;
5637
5638         hc_addr = (HC_REG_COMMAND_REG + BP_PORT(sc) * 32 + COMMAND_REG_INT_ACK);
5639         igu_ack.status_block_index = index;
5640         igu_ack.sb_id_and_flags =
5641             ((sb_id << IGU_ACK_REGISTER_STATUS_BLOCK_ID_SHIFT) |
5642             (storm << IGU_ACK_REGISTER_STORM_ID_SHIFT) |
5643             (update << IGU_ACK_REGISTER_UPDATE_INDEX_SHIFT) |
5644             (int_mode << IGU_ACK_REGISTER_INTERRUPT_MODE_SHIFT));
5645
5646         rmb();
5647         REG_WR(sc, hc_addr, (*(uint32_t *) &igu_ack));
5648         wmb();
5649 }
5650
5651 /*
5652  * Update fastpath status block index.
5653  *
5654  * Returns:
5655  *   0 = Nu completes, 1 = TX completes, 2 = RX completes,
5656  *   3 = RX & TX completes
5657  */
5658 static __inline uint16_t
5659 bxe_update_fpsb_idx(struct bxe_fastpath *fp)
5660 {
5661         struct host_status_block *fpsb;
5662         uint16_t rc;
5663
5664         fpsb = fp->status_block;
5665         rc = 0;
5666
5667         rmb();
5668
5669         /* Check for any CSTORM transmit completions. */
5670         if (fp->fp_c_idx != le16toh(fpsb->c_status_block.status_block_index)) {
5671                 fp->fp_c_idx = le16toh(fpsb->c_status_block.status_block_index);
5672                 rc |= 0x1;
5673         }
5674
5675         /* Check for any USTORM receive completions. */
5676         if (fp->fp_u_idx != le16toh(fpsb->u_status_block.status_block_index)) {
5677                 fp->fp_u_idx = le16toh(fpsb->u_status_block.status_block_index);
5678                 rc |= 0x2;
5679         }
5680
5681         return (rc);
5682 }
5683
5684 /*
5685  * Acknowledge interrupt.
5686  *
5687  * Returns:
5688  *   Interrupt value read from IGU.
5689  */
5690 static uint16_t
5691 bxe_ack_int(struct bxe_softc *sc)
5692 {
5693         uint32_t hc_addr, result;
5694
5695         hc_addr = HC_REG_COMMAND_REG + BP_PORT(sc) * 32 + COMMAND_REG_SIMD_MASK;
5696         result = REG_RD(sc, hc_addr);
5697         DBPRINT(sc, BXE_INSANE_INTR, "%s(): Read 0x%08X from HC addr 0x%08X\n",
5698             __FUNCTION__, result, hc_addr);
5699
5700         return (result);
5701 }
5702
5703 /*
5704  * Slowpath event handler.
5705  *
5706  * Checks that a ramrod completion occurs while the
5707  * controller is in the proper state.
5708  *
5709  * Returns:
5710  *   None.
5711  */
5712 static void
5713 bxe_sp_event(struct bxe_fastpath *fp, union eth_rx_cqe *rr_cqe)
5714 {
5715         struct bxe_softc *sc;
5716         int cid, command;
5717
5718         sc = fp->sc;
5719         DBENTER(BXE_VERBOSE_RAMROD);
5720
5721         cid = SW_CID(rr_cqe->ramrod_cqe.conn_and_cmd_data);
5722         command = CQE_CMD(rr_cqe->ramrod_cqe.conn_and_cmd_data);
5723         DBPRINT(sc, BXE_VERBOSE_RAMROD, "%s(): CID = %d, ramrod command = %d, "
5724             "device state = 0x%08X, fp[%02d].state = 0x%08X, type = %d\n",
5725             __FUNCTION__, cid, command, sc->state, fp->index, fp->state,
5726             rr_cqe->ramrod_cqe.ramrod_type);
5727
5728         /* Free up an entry on the slowpath queue. */
5729         sc->spq_left++;
5730
5731         /* Handle ramrod commands that completed on a client connection. */
5732         if (fp->index) {
5733                 /* Check for a completion for the current state. */
5734                 switch (command | fp->state) {
5735                 case (RAMROD_CMD_ID_ETH_CLIENT_SETUP | BXE_FP_STATE_OPENING):
5736                         DBPRINT(sc, BXE_VERBOSE_RAMROD,
5737                             "%s(): Completed fp[%02d] CLIENT_SETUP Ramrod.\n",
5738                             __FUNCTION__, cid);
5739                         fp->state = BXE_FP_STATE_OPEN;
5740                         break;
5741                 case (RAMROD_CMD_ID_ETH_HALT | BXE_FP_STATE_HALTING):
5742                         DBPRINT(sc, BXE_VERBOSE_RAMROD,
5743                             "%s(): Completed fp[%02d] ETH_HALT ramrod\n",
5744                             __FUNCTION__, cid);
5745                         fp->state = BXE_FP_STATE_HALTED;
5746                         break;
5747                 default:
5748                         DBPRINT(sc, BXE_VERBOSE_RAMROD,
5749                             "%s(): Unexpected microcode reply (%d) while "
5750                             "in state 0x%04X!\n", __FUNCTION__, command,
5751                             fp->state);
5752                 }
5753
5754                 goto bxe_sp_event_exit;
5755         }
5756
5757         /* Handle ramrod commands that completed on the leading connection. */
5758         switch (command | sc->state) {
5759         case (RAMROD_CMD_ID_ETH_PORT_SETUP | BXE_STATE_OPENING_WAIT4_PORT):
5760                 DBPRINT(sc, BXE_VERBOSE_RAMROD,
5761                     "%s(): Completed PORT_SETUP ramrod.\n", __FUNCTION__);
5762                 sc->state = BXE_STATE_OPEN;
5763                 break;
5764         case (RAMROD_CMD_ID_ETH_HALT | BXE_STATE_CLOSING_WAIT4_HALT):
5765                 DBPRINT(sc, BXE_VERBOSE_RAMROD,
5766                     "%s(): Completed ETH_HALT ramrod.\n", __FUNCTION__);
5767                 sc->state = BXE_STATE_CLOSING_WAIT4_DELETE;
5768                 fp->state = BXE_FP_STATE_HALTED;
5769                 break;
5770         case (RAMROD_CMD_ID_ETH_CFC_DEL | BXE_STATE_CLOSING_WAIT4_HALT):
5771                 DBPRINT(sc, BXE_VERBOSE_RAMROD,
5772                     "%s(): Completed fp[%02d] ETH_CFC_DEL ramrod.\n",
5773                     __FUNCTION__, cid);
5774                 sc->fp[cid].state = BXE_FP_STATE_CLOSED;
5775                 break;
5776         case (RAMROD_CMD_ID_ETH_SET_MAC | BXE_STATE_OPEN):
5777                 DBPRINT(sc, BXE_VERBOSE_RAMROD,
5778                     "%s(): Completed ETH_SET_MAC ramrod in STATE_OPEN state.\n",
5779                     __FUNCTION__);
5780                 break;
5781         case (RAMROD_CMD_ID_ETH_SET_MAC | BXE_STATE_CLOSING_WAIT4_HALT):
5782                 DBPRINT(sc, BXE_VERBOSE_RAMROD,
5783                     "%s(): Completed ETH_SET_MAC ramrod in "
5784                     "CLOSING_WAIT4_HALT state.\n", __FUNCTION__);
5785                 break;
5786         default:
5787                 DBPRINT(sc, BXE_FATAL, "%s(): Unexpected microcode reply (%d)! "
5788                     "State is 0x%08X\n", __FUNCTION__, command, sc->state);
5789         }
5790
5791 bxe_sp_event_exit:
5792         /* Force bxe_wait_ramrod() to see the change. */
5793         mb();
5794         DBEXIT(BXE_VERBOSE_RAMROD);
5795 }
5796
5797 /*
5798  * Lock access to a hardware resource using controller arbitration
5799  * register.
5800  *
5801  * Returns:
5802  *   0 = Success, !0 = Failure.
5803  */
5804 static int
5805 bxe_acquire_hw_lock(struct bxe_softc *sc, uint32_t resource)
5806 {
5807         uint32_t hw_lock_control_reg, lock_status, resource_bit;
5808         uint8_t func;
5809         int cnt, rc;
5810
5811         DBENTER(BXE_VERBOSE_MISC);
5812         DBPRINT(sc, BXE_VERBOSE_MISC, "%s(): Locking resource 0x%08X\n",
5813             __FUNCTION__, resource);
5814
5815         func = BP_FUNC(sc);
5816         resource_bit = 1 << resource;
5817         rc = 0;
5818
5819         hw_lock_control_reg = ((func <= 5) ?
5820             (MISC_REG_DRIVER_CONTROL_1 + func * 8) :
5821             (MISC_REG_DRIVER_CONTROL_7 + (func - 6) * 8));
5822
5823         /* Validating that the resource is within range. */
5824         if (resource > HW_LOCK_MAX_RESOURCE_VALUE) {
5825                 DBPRINT(sc, BXE_WARN, "%s(): Resource is out of range! "
5826                     "resource(0x%08X) > HW_LOCK_MAX_RESOURCE_VALUE(0x%08X)\n",
5827                     __FUNCTION__, resource, HW_LOCK_MAX_RESOURCE_VALUE);
5828                 rc = EINVAL;
5829                 goto bxe_acquire_hw_lock_exit;
5830         }
5831
5832         /* Validating that the resource is not already taken. */
5833         lock_status = REG_RD(sc, hw_lock_control_reg);
5834         if (lock_status & resource_bit) {
5835                 DBPRINT(sc, BXE_WARN, "%s(): Failed to acquire lock! "
5836                     "lock_status = 0x%08X, resource_bit = 0x%08X\n",
5837                     __FUNCTION__, lock_status, resource_bit);
5838                 rc = EEXIST;
5839                 goto bxe_acquire_hw_lock_exit;
5840         }
5841
5842         /* Try for 5 seconds every 5ms. */
5843         for (cnt = 0; cnt < 1000; cnt++) {
5844                 /* Try to acquire the lock. */
5845                 REG_WR(sc, hw_lock_control_reg + 4, resource_bit);
5846                 lock_status = REG_RD(sc, hw_lock_control_reg);
5847
5848                 if (lock_status & resource_bit)
5849                         goto bxe_acquire_hw_lock_exit;
5850                 DELAY(5000);
5851         }
5852
5853         DBPRINT(sc, BXE_WARN, "%s(): Timeout!\n", __FUNCTION__);
5854         rc = EAGAIN;
5855
5856 bxe_acquire_hw_lock_exit:
5857         DBEXIT(BXE_VERBOSE_MISC);
5858         return (rc);
5859 }
5860
5861 /*
5862  * Unlock access to a hardware resource using controller arbitration
5863  * register.
5864  *
5865  * Returns:
5866  *   0 = Success, !0 = Failure.
5867  */
5868 static int
5869 bxe_release_hw_lock(struct bxe_softc *sc, uint32_t resource)
5870 {
5871         uint32_t hw_lock_control_reg, lock_status, resource_bit;
5872         uint8_t func;
5873         int rc;
5874
5875         DBENTER(BXE_VERBOSE_MISC);
5876         DBPRINT(sc, BXE_VERBOSE_MISC, "%s(): Unlocking resource 0x%08X\n",
5877                 __FUNCTION__, resource);
5878
5879         resource_bit = 1 << resource;
5880         func = BP_FUNC(sc);
5881         rc = 0;
5882         /* Validating that the resource is within range */
5883         if (resource > HW_LOCK_MAX_RESOURCE_VALUE) {
5884                 DBPRINT(sc, BXE_WARN, "%s(): Resource is out of range! "
5885                     "resource(0x%08X) > HW_LOCK_MAX_RESOURCE_VALUE(0x%08X)\n",
5886                     __FUNCTION__, resource, HW_LOCK_MAX_RESOURCE_VALUE);
5887                 rc = EINVAL;
5888                 goto bxe_release_hw_lock_exit;
5889         }
5890
5891         /* Find the register for the resource lock. */
5892         hw_lock_control_reg = ((func <= 5) ?
5893             (MISC_REG_DRIVER_CONTROL_1 + func * 8) :
5894             (MISC_REG_DRIVER_CONTROL_7 + (func - 6) * 8));
5895
5896         /* Validating that the resource is currently taken */
5897         lock_status = REG_RD(sc, hw_lock_control_reg);
5898         if (!(lock_status & resource_bit)) {
5899                 DBPRINT(sc, BXE_WARN, "%s(): The resource is not currently "
5900                     "locked! lock_status = 0x%08X, resource_bit = 0x%08X\n",
5901                     __FUNCTION__, lock_status, resource_bit);
5902                 rc = EFAULT;
5903                 goto bxe_release_hw_lock_exit;
5904         }
5905
5906         /* Free the hardware lock. */
5907         REG_WR(sc, hw_lock_control_reg, resource_bit);
5908
5909 bxe_release_hw_lock_exit:
5910         DBEXIT(BXE_VERBOSE_MISC);
5911         return (rc);
5912 }
5913
5914 int
5915 bxe_get_gpio(struct bxe_softc *sc, int gpio_num, uint8_t port)
5916 {
5917         uint32_t gpio_mask, gpio_reg;
5918         int gpio_port, gpio_shift, value;
5919
5920         /* The GPIO should be swapped if swap register is set and active */
5921         gpio_port = (REG_RD(sc, NIG_REG_PORT_SWAP) && REG_RD(sc,
5922             NIG_REG_STRAP_OVERRIDE)) ^ port;
5923         gpio_shift = gpio_num +
5924             (gpio_port ? MISC_REGISTERS_GPIO_PORT_SHIFT : 0);
5925         gpio_mask = 1 << gpio_shift;
5926
5927         if (gpio_num > MISC_REGISTERS_GPIO_3) {
5928                 DBPRINT(sc, BXE_WARN, "%s(): Invalid GPIO %d\n",
5929                     __FUNCTION__, gpio_num);
5930                 return (-EINVAL);
5931         }
5932
5933         /* read GPIO value */
5934         gpio_reg = REG_RD(sc, MISC_REG_GPIO);
5935
5936         /* get the requested pin value */
5937         if ((gpio_reg & gpio_mask) == gpio_mask)
5938                 value = 1;
5939         else
5940                 value = 0;
5941
5942         DBPRINT(sc, BXE_VERBOSE_PHY, "pin %d  value 0x%x\n", gpio_num, value);
5943
5944         return (value);
5945 }
5946
5947 /*
5948  * Sets the state of a General Purpose I/O (GPIO).
5949  *
5950  * Returns:
5951  *   None.
5952  */
5953 int
5954 bxe_set_gpio(struct bxe_softc *sc, int gpio_num, uint32_t mode, uint8_t port)
5955 {
5956         uint32_t gpio_reg, gpio_mask;
5957         int gpio_port, gpio_shift, rc;
5958
5959         DBENTER(BXE_VERBOSE_MISC);
5960
5961         /* The GPIO should be swapped if swap register is set and active. */
5962         gpio_port = (REG_RD(sc, NIG_REG_PORT_SWAP) && REG_RD(sc,
5963             NIG_REG_STRAP_OVERRIDE)) ^ port;
5964         gpio_shift = gpio_num +
5965             (gpio_port ? MISC_REGISTERS_GPIO_PORT_SHIFT : 0);
5966         gpio_mask = (1 << gpio_shift);
5967         rc = 0;
5968
5969         if (gpio_num > MISC_REGISTERS_GPIO_3) {
5970                 DBPRINT(sc, BXE_FATAL, "%s(): Invalid GPIO (%d)!\n",
5971                     __FUNCTION__, gpio_num);
5972                 rc = EINVAL;
5973                 goto bxe_set_gpio_exit;
5974         }
5975
5976         /* Make sure no one else is trying to use the GPIO. */
5977         rc = bxe_acquire_hw_lock(sc, HW_LOCK_RESOURCE_GPIO);
5978         if (rc) {
5979                 DBPRINT(sc, BXE_WARN, "%s(): Can't acquire GPIO lock!\n",
5980                     __FUNCTION__);
5981                 goto bxe_set_gpio_exit;
5982         }
5983
5984         /* Read GPIO and mask all but the float bits. */
5985         gpio_reg = (REG_RD(sc, MISC_REG_GPIO) & MISC_REGISTERS_GPIO_FLOAT);
5986
5987         switch (mode) {
5988         case MISC_REGISTERS_GPIO_OUTPUT_LOW:
5989                 DBPRINT(sc, BXE_VERBOSE, "%s(): Set GPIO %d (shift %d) -> "
5990                     "output low\n", __FUNCTION__, gpio_num, gpio_shift);
5991                 gpio_reg &= ~(gpio_mask << MISC_REGISTERS_GPIO_FLOAT_POS);
5992                 gpio_reg |=  (gpio_mask << MISC_REGISTERS_GPIO_CLR_POS);
5993                 break;
5994         case MISC_REGISTERS_GPIO_OUTPUT_HIGH:
5995                 DBPRINT(sc, BXE_VERBOSE, "%s(): Set GPIO %d (shift %d) -> "
5996                     "output high\n", __FUNCTION__, gpio_num, gpio_shift);
5997                 gpio_reg &= ~(gpio_mask << MISC_REGISTERS_GPIO_FLOAT_POS);
5998                 gpio_reg |=  (gpio_mask << MISC_REGISTERS_GPIO_SET_POS);
5999                 break;
6000         case MISC_REGISTERS_GPIO_INPUT_HI_Z:
6001                 DBPRINT(sc, BXE_VERBOSE, "%s(): Set GPIO %d (shift %d) -> "
6002                     "input\n", __FUNCTION__, gpio_num, gpio_shift);
6003                 gpio_reg |= (gpio_mask << MISC_REGISTERS_GPIO_FLOAT_POS);
6004                 break;
6005         default:
6006                 DBPRINT(sc, BXE_FATAL, "%s(): Unknown GPIO mode (0x%08X)!\n",
6007                     __FUNCTION__, mode);
6008                 break;
6009         }
6010
6011         REG_WR(sc, MISC_REG_GPIO, gpio_reg);
6012         rc = bxe_release_hw_lock(sc, HW_LOCK_RESOURCE_GPIO);
6013         if (rc) {
6014                 DBPRINT(sc, BXE_WARN, "%s(): Can't release GPIO lock!\n",
6015                     __FUNCTION__);
6016         }
6017
6018 bxe_set_gpio_exit:
6019         DBEXIT(BXE_VERBOSE_MISC);
6020         return (rc);
6021 }
6022
6023 int
6024 bxe_set_gpio_int(struct bxe_softc *sc, int gpio_num, uint32_t mode,
6025     uint8_t port)
6026 {
6027         uint32_t gpio_mask, gpio_reg;
6028         int gpio_port, gpio_shift;
6029
6030         /* The GPIO should be swapped if swap register is set and active */
6031         gpio_port = (REG_RD(sc, NIG_REG_PORT_SWAP) && REG_RD(sc,
6032             NIG_REG_STRAP_OVERRIDE)) ^ port;
6033         gpio_shift = gpio_num +
6034             (gpio_port ? MISC_REGISTERS_GPIO_PORT_SHIFT : 0);
6035         gpio_mask = (1 << gpio_shift);
6036         if (gpio_num > MISC_REGISTERS_GPIO_3) {
6037                 DBPRINT(sc, BXE_WARN, "%s(): Invalid GPIO %d\n",
6038                     __FUNCTION__, gpio_num);
6039                 return (-EINVAL);
6040         }
6041
6042         bxe_acquire_hw_lock(sc, HW_LOCK_RESOURCE_GPIO);
6043         /* read GPIO int */
6044         gpio_reg = REG_RD(sc, MISC_REG_GPIO_INT);
6045
6046         switch (mode) {
6047         case MISC_REGISTERS_GPIO_INT_OUTPUT_CLR:
6048                 DBPRINT(sc, BXE_VERBOSE_PHY, "Clear GPIO INT %d (shift %d) -> "
6049                     "output low\n", gpio_num, gpio_shift);
6050                 /* clear SET and set CLR */
6051                 gpio_reg &= ~(gpio_mask << MISC_REGISTERS_GPIO_INT_SET_POS);
6052                 gpio_reg |=  (gpio_mask << MISC_REGISTERS_GPIO_INT_CLR_POS);
6053                 break;
6054         case MISC_REGISTERS_GPIO_INT_OUTPUT_SET:
6055                 DBPRINT(sc, BXE_VERBOSE_PHY, "Set GPIO INT %d (shift %d) -> "
6056                     "output high\n", gpio_num, gpio_shift);
6057                 /* clear CLR and set SET */
6058                 gpio_reg &= ~(gpio_mask << MISC_REGISTERS_GPIO_INT_CLR_POS);
6059                 gpio_reg |=  (gpio_mask << MISC_REGISTERS_GPIO_INT_SET_POS);
6060                 break;
6061         default:
6062                 break;
6063         }
6064
6065         REG_WR(sc, MISC_REG_GPIO_INT, gpio_reg);
6066         bxe_release_hw_lock(sc, HW_LOCK_RESOURCE_GPIO);
6067
6068         return (0);
6069 }
6070
6071 /*
6072  * Sets the state of a Shared Purpose I/O (SPIO).
6073  *
6074  * Returns:
6075  *   0 = Success, !0 = Failure.
6076  */
6077 int
6078 bxe_set_spio(struct bxe_softc *sc, int spio_num, uint32_t mode)
6079 {
6080         uint32_t spio_reg, spio_mask;
6081         int rc;
6082
6083         rc = 0;
6084         spio_mask = 1 << spio_num;
6085
6086         /* Validate the SPIO. */
6087         if ((spio_num < MISC_REGISTERS_SPIO_4) ||
6088             (spio_num > MISC_REGISTERS_SPIO_7)) {
6089                 DBPRINT(sc, BXE_WARN, "%s(): Invalid SPIO (%d)!\n",
6090                     __FUNCTION__, spio_num);
6091                 rc = EINVAL;
6092                 goto bxe_set_spio_exit;
6093         }
6094
6095         rc = bxe_acquire_hw_lock(sc, HW_LOCK_RESOURCE_SPIO);
6096         if (rc) {
6097                 DBPRINT(sc, BXE_WARN, "%s(): Can't acquire SPIO lock!\n",
6098                     __FUNCTION__);
6099                 goto bxe_set_spio_exit;
6100         }
6101
6102         /* Read SPIO and mask all but the float bits. */
6103         spio_reg = (REG_RD(sc, MISC_REG_SPIO) & MISC_REGISTERS_SPIO_FLOAT);
6104
6105         switch (mode) {
6106         case MISC_REGISTERS_SPIO_OUTPUT_LOW :
6107                 DBPRINT(sc, BXE_VERBOSE_MISC, "%s(): Set SPIO %d -> "
6108                     "output low\n", __FUNCTION__, spio_num);
6109                 spio_reg &= ~(spio_mask << MISC_REGISTERS_SPIO_FLOAT_POS);
6110                 spio_reg |=  (spio_mask << MISC_REGISTERS_SPIO_CLR_POS);
6111                 break;
6112         case MISC_REGISTERS_SPIO_OUTPUT_HIGH :
6113                 DBPRINT(sc, BXE_VERBOSE_MISC,  "%s(): Set SPIO %d -> "
6114                     "output high\n", __FUNCTION__, spio_num);
6115                 spio_reg &= ~(spio_mask << MISC_REGISTERS_SPIO_FLOAT_POS);
6116                 spio_reg |=  (spio_mask << MISC_REGISTERS_SPIO_SET_POS);
6117                 break;
6118         case MISC_REGISTERS_SPIO_INPUT_HI_Z:
6119                 DBPRINT(sc, BXE_VERBOSE_MISC, "%s(): Set SPIO %d -> "
6120                     "input\n", __FUNCTION__, spio_num);
6121                 spio_reg |= (spio_mask << MISC_REGISTERS_SPIO_FLOAT_POS);
6122                 break;
6123         default:
6124                 DBPRINT(sc, BXE_WARN, "%s(): Unknown SPIO mode (0x%08X)!\n",
6125                     __FUNCTION__, mode);
6126                 break;
6127         }
6128
6129         REG_WR(sc, MISC_REG_SPIO, spio_reg);
6130         rc = bxe_release_hw_lock(sc, HW_LOCK_RESOURCE_SPIO);
6131         if (rc) {
6132                 DBPRINT(sc, BXE_WARN, "%s(): Can't release SPIO lock!\n",
6133                     __FUNCTION__);
6134         }
6135
6136 bxe_set_spio_exit:
6137         return (rc);
6138 }
6139
6140 /*
6141  * When the 57711E is operating in multi-function mode, the controller
6142  * must be configured to arbitrate TX between multiple VNICs.
6143  *
6144  * Returns:
6145  *   None.
6146  */
6147 static void
6148 bxe_init_port_minmax(struct bxe_softc *sc)
6149 {
6150         uint32_t fair_periodic_timeout_usec, r_param, t_fair;
6151
6152         DBENTER(BXE_VERBOSE_MISC);
6153
6154         r_param = sc->link_vars.line_speed / 8;
6155
6156         memset(&(sc->cmng.rs_vars), 0,
6157             sizeof(struct rate_shaping_vars_per_port));
6158         memset(&(sc->cmng.fair_vars), 0, sizeof(struct fairness_vars_per_port));
6159
6160         /* 100 usec in SDM ticks = 25 since each tick is 4 usec. */
6161         sc->cmng.rs_vars.rs_periodic_timeout = RS_PERIODIC_TIMEOUT_USEC / 4;
6162         /*
6163          * This is the threshold below which no timer arming will occur.
6164          * We use a coefficient of 1, 25 so that the threshold is a
6165          * little bigger that real time to compensate for timer
6166          * in-accuracy.
6167          */
6168         sc->cmng.rs_vars.rs_threshold = (RS_PERIODIC_TIMEOUT_USEC *
6169             r_param * 5) / 4;
6170         /* Resolution of fairness timer. */
6171         fair_periodic_timeout_usec = QM_ARB_BYTES / r_param;
6172
6173         /* For 10G it is 1000us, for 1G it is 10000us. */
6174         t_fair = T_FAIR_COEF / sc->link_vars.line_speed;
6175         /* This is the threshold where we won't arm the timer
6176            anymore. */
6177         sc->cmng.fair_vars.fair_threshold = QM_ARB_BYTES;
6178         /*
6179          * Multiply by 1e3/8 to get bytes/msec. We don't want the
6180          * credits to pass a credit of the T_FAIR*FAIR_MEM (algorithm
6181          * resolution)
6182          */
6183         sc->cmng.fair_vars.upper_bound = r_param * t_fair * FAIR_MEM;
6184         /* Since each tick is 4 us. */
6185         sc->cmng.fair_vars.fairness_timeout = fair_periodic_timeout_usec / 4;
6186
6187         DBEXIT(BXE_VERBOSE_MISC);
6188 }
6189
6190
6191 /*
6192  * This function is called when a link interrupt is generated
6193  * and configures the controller for the new link state.
6194  *
6195  * Returns:
6196  *   None.
6197  */
6198 static void
6199 bxe_link_attn(struct bxe_softc *sc)
6200 {
6201         struct host_port_stats *pstats;
6202         uint32_t pause_enabled;
6203         int func, i, port, vn;
6204
6205         DBENTER(BXE_VERBOSE_PHY);
6206
6207         /* Make sure that we are synced with the current statistics. */
6208         bxe_stats_handle(sc, STATS_EVENT_STOP);
6209
6210         bxe_link_update(&sc->link_params, &sc->link_vars);
6211
6212         if (sc->link_vars.link_up) {
6213                 if (CHIP_IS_E1H(sc)) {
6214                         port = BP_PORT(sc);
6215                         pause_enabled = 0;
6216
6217                         if (sc->link_vars.flow_ctrl & FLOW_CTRL_TX)
6218                                 pause_enabled = 1;
6219
6220                         REG_WR(sc, BAR_USTORM_INTMEM +
6221                             USTORM_ETH_PAUSE_ENABLED_OFFSET(port),
6222                             pause_enabled);
6223                 }
6224
6225                 if (sc->link_vars.mac_type == MAC_TYPE_BMAC) {
6226                         pstats = BXE_SP(sc, port_stats);
6227                         /* Reset old BMAC statistics. */
6228                         memset(&(pstats->mac_stx[0]), 0,
6229                             sizeof(struct mac_stx));
6230                 }
6231
6232                 if ((sc->state == BXE_STATE_OPEN) ||
6233                     (sc->state == BXE_STATE_DISABLED))
6234                         bxe_stats_handle(sc, STATS_EVENT_LINK_UP);
6235         }
6236
6237         /* Need additional handling for multi-function devices. */
6238         if (IS_E1HMF(sc)) {
6239                 port = BP_PORT(sc);
6240                 if (sc->link_vars.link_up) {
6241                         if (sc->dcc_enable == TRUE) {
6242                                 bxe_congestionmgmt(sc, TRUE);
6243                                 /* Store in internal memory. */
6244                                 for (i = 0; i <
6245                                     sizeof(struct cmng_struct_per_port) / 4;
6246                                     i++) {
6247                                         REG_WR(sc, BAR_XSTORM_INTMEM +
6248                                 XSTORM_CMNG_PER_PORT_VARS_OFFSET(port) + (i*4),
6249                                             ((uint32_t *)(&sc->cmng))[i]);
6250                                 }
6251                         }
6252                 }
6253                 for (vn = VN_0; vn < E1HVN_MAX; vn++) {
6254                         /* Don't send an attention to ourselves. */
6255                         if (vn == BP_E1HVN(sc))
6256                                 continue;
6257                         func = ((vn << 1) | port);
6258                         /*
6259                          * Send an attention to other drivers on the same port.
6260                          */
6261                         REG_WR(sc, MISC_REG_AEU_GENERAL_ATTN_0 +
6262                             (LINK_SYNC_ATTENTION_BIT_FUNC_0 + func) * 4, 1);
6263                 }
6264         }
6265
6266         DBEXIT(BXE_VERBOSE_PHY);
6267 }
6268
6269 /*
6270  * Sets the driver instance as the port management function (PMF).
6271  *
6272  * This is only used on "multi-function" capable devices such as the
6273  * 57711E and initializes the controller so that the PMF driver instance
6274  * can interact with other driver instances that may be operating on
6275  * the same Ethernet port.
6276  *
6277  * Returns:
6278  *   None.
6279  */
6280 static void
6281 bxe_pmf_update(struct bxe_softc *sc)
6282 {
6283         uint32_t val;
6284         int port;
6285
6286         /* Record that this driver instance is managing the port. */
6287         sc->port.pmf = 1;
6288         DBPRINT(sc, BXE_INFO, "%s(): Enabling this port as PMF.\n",
6289             __FUNCTION__);
6290
6291         /* Enable NIG attention. */
6292         port = BP_PORT(sc);
6293         val = (0xff0f | (1 << (BP_E1HVN(sc) + 4)));
6294         REG_WR(sc, HC_REG_TRAILING_EDGE_0 + port * 8, val);
6295         REG_WR(sc, HC_REG_LEADING_EDGE_0 + port * 8, val);
6296
6297         bxe_stats_handle(sc, STATS_EVENT_PMF);
6298 }
6299
6300 /* 8073 Download definitions */
6301 /* spi Parameters.*/
6302 #define SPI_CTRL_1_L    0xC000
6303 #define SPI_CTRL_1_H    0xC002
6304 #define SPI_CTRL_2_L    0xC400
6305 #define SPI_CTRL_2_H    0xC402
6306 #define SPI_TXFIFO      0xD000
6307 #define SPI_RXFIFO      0xD400
6308
6309 /* Input Command Messages.*/
6310 /*
6311  * Write CPU/SPI Control Regs, followed by Count And CPU/SPI Controller
6312  * Reg add/data pairs.
6313  */
6314 #define WR_CPU_CTRL_REGS        0x11
6315 /*
6316  * Read CPU/SPI Control Regs, followed by Count and CPU/SPI Controller
6317  * Register Add.
6318  */
6319 #define RD_CPU_CTRL_REGS        0xEE
6320 /*
6321  * Write CPU/SPI Control Regs Continously, followed by Count and
6322  * CPU/SPI Controller Reg addr and data's.
6323  */
6324 #define WR_CPU_CTRL_FIFO        0x66
6325 /* Output Command Messages.*/
6326 #define DONE                    0x4321
6327
6328 /* SPI Controller Commands (known As messages).*/
6329 #define MSGTYPE_HWR     0x40
6330 #define MSGTYPE_HRD     0x80
6331 #define WRSR_OPCODE     0x01
6332 #define WR_OPCODE       0x02
6333 #define RD_OPCODE       0x03
6334 #define WRDI_OPCODE     0x04
6335 #define RDSR_OPCODE     0x05
6336 #define WREN_OPCODE     0x06
6337 #define WR_BLOCK_SIZE   0x40    /* Maximum 64 Bytes Writes.*/
6338
6339 /*
6340  * Post a slowpath command.
6341  *
6342  * A slowpath command is used to propogate a configuration change through
6343  * the controller in a controlled manner, allowing each STORM processor and
6344  * other H/W blocks to phase in the change.  The commands sent on the
6345  * slowpath are referred to as ramrods.  Depending on the ramrod used the
6346  * completion of the ramrod will occur in different ways.  Here's a
6347  * breakdown of ramrods and how they complete:
6348  *
6349  * RAMROD_CMD_ID_ETH_PORT_SETUP
6350  *   Used to setup the leading connection on a port.  Completes on the
6351  *   Receive Completion Queue (RCQ) of that port (typically fp[0]).
6352  *
6353  * RAMROD_CMD_ID_ETH_CLIENT_SETUP
6354  *   Used to setup an additional connection on a port.  Completes on the
6355  *   RCQ of the multi-queue/RSS connection being initialized.
6356  *
6357  * RAMROD_CMD_ID_ETH_STAT_QUERY
6358  *   Used to force the storm processors to update the statistics database
6359  *   in host memory.  This ramrod is send on the leading connection CID and
6360  *   completes as an index increment of the CSTORM on the default status
6361  *   block.
6362  *
6363  * RAMROD_CMD_ID_ETH_UPDATE
6364  *   Used to update the state of the leading connection, usually to udpate
6365  *   the RSS indirection table.  Completes on the RCQ of the leading
6366  *   connection. (Not currently used under FreeBSD until OS support becomes
6367  *   available.)
6368  *
6369  * RAMROD_CMD_ID_ETH_HALT
6370  *   Used when tearing down a connection prior to driver unload.  Completes
6371  *   on the RCQ of the multi-queue/RSS connection being torn down.  Don't
6372  *   use this on the leading connection.
6373  *
6374  * RAMROD_CMD_ID_ETH_SET_MAC
6375  *   Sets the Unicast/Broadcast/Multicast used by the port.  Completes on
6376  *   the RCQ of the leading connection.
6377  *
6378  * RAMROD_CMD_ID_ETH_CFC_DEL
6379  *   Used when tearing down a conneciton prior to driver unload.  Completes
6380  *   on the RCQ of the leading connection (since the current connection
6381  *   has been completely removed from controller memory).
6382  *
6383  * RAMROD_CMD_ID_ETH_PORT_DEL
6384  *   Used to tear down the leading connection prior to driver unload,
6385  *   typically fp[0].  Completes as an index increment of the CSTORM on the
6386  *   default status block.
6387  *
6388  * RAMROD_CMD_ID_ETH_FORWARD_SETUP
6389  *   Used for connection offload.  Completes on the RCQ of the multi-queue
6390  *   RSS connection that is being offloaded.  (Not currently used under
6391  *   FreeBSD.)
6392  *
6393  * There can only be one command pending per function.
6394  *
6395  * Returns:
6396  *   0 = Success, !0 = Failure.
6397  */
6398 static int
6399 bxe_sp_post(struct bxe_softc *sc, int command, int cid, uint32_t data_hi,
6400     uint32_t data_lo, int common)
6401 {
6402         int func, rc;
6403
6404         DBRUNMSG((BXE_EXTREME_LOAD | BXE_EXTREME_RESET |
6405             BXE_EXTREME_UNLOAD | BXE_EXTREME_RAMROD),
6406             bxe_decode_ramrod_cmd(sc, command));
6407
6408         DBPRINT(sc, BXE_VERBOSE_RAMROD, "%s(): cid = %d, data_hi = 0x%08X, "
6409             "data_low = 0x%08X, remaining spq entries = %d\n", __FUNCTION__,
6410             cid, data_hi, data_lo, sc->spq_left);
6411
6412         rc = 0;
6413         /* Skip all slowpath commands if the driver has panic'd. */
6414         if (sc->panic) {
6415                 rc = EIO;
6416                 goto bxe_sp_post_exit;
6417         }
6418
6419         BXE_SP_LOCK(sc);
6420
6421         /* We are limited to 8 slowpath commands. */
6422         if (!sc->spq_left) {
6423                 BXE_PRINTF("%s(%d): Slowpath queue is full!\n",
6424                     __FILE__, __LINE__);
6425                 bxe_panic_dump(sc);
6426                 rc = EBUSY;
6427                 goto bxe_sp_post_exit;
6428         }
6429
6430         /* Encode the CID with the command. */
6431         sc->spq_prod_bd->hdr.conn_and_cmd_data =
6432             htole32(((command << SPE_HDR_CMD_ID_SHIFT) | HW_CID(sc, cid)));
6433         sc->spq_prod_bd->hdr.type = htole16(ETH_CONNECTION_TYPE);
6434
6435         if (common)
6436                 sc->spq_prod_bd->hdr.type |=
6437                     htole16((1 << SPE_HDR_COMMON_RAMROD_SHIFT));
6438
6439         /* Point the hardware at the new configuration data. */
6440         sc->spq_prod_bd->data.mac_config_addr.hi = htole32(data_hi);
6441         sc->spq_prod_bd->data.mac_config_addr.lo = htole32(data_lo);
6442
6443         /* Reduce the number of available slots for slowpath commands. */
6444         sc->spq_left--;
6445
6446         /* Manage the end of the ring. */
6447         if (sc->spq_prod_bd == sc->spq_last_bd) {
6448                 sc->spq_prod_bd = sc->spq;
6449                 sc->spq_prod_idx = 0;
6450                 DBPRINT(sc, BXE_VERBOSE, "%s(): End of slowpath queue.\n",
6451                     __FUNCTION__);
6452         } else {
6453                 sc->spq_prod_bd++;
6454                 sc->spq_prod_idx++;
6455         }
6456
6457         func = BP_FUNC(sc);
6458         /* Kick off the slowpath command. */
6459         REG_WR(sc, BAR_XSTORM_INTMEM + XSTORM_SPQ_PROD_OFFSET(func),
6460             sc->spq_prod_idx);
6461
6462 bxe_sp_post_exit:
6463         BXE_SP_UNLOCK(sc);
6464
6465         return (rc);
6466 }
6467
6468 /*
6469  * Acquire the MCP access lock.
6470  *
6471  * Returns:
6472  *   0 = Success, !0 = Failure.
6473  */
6474 static int
6475 bxe_acquire_alr(struct bxe_softc *sc)
6476 {
6477         uint32_t val;
6478         int i, rc, retries;
6479
6480         DBENTER(BXE_VERBOSE_MISC);
6481
6482         rc = 0;
6483         retries = 100;
6484         /* Acquire lock using mcpr_access_lock SPLIT register. */
6485         for (i = 0; i < retries * 10; i++) {
6486                 val = 1UL << 31;
6487                 REG_WR(sc, GRCBASE_MCP + 0x9c, val);
6488                 val = REG_RD(sc, GRCBASE_MCP + 0x9c);
6489
6490                 if (val & (1L << 31))
6491                         break;
6492
6493                 DELAY(5000);
6494         }
6495
6496         if (!(val & (1L << 31))) {
6497                 DBPRINT(sc, BXE_WARN,
6498                     "%s(): Cannot acquire MCP split access lock.\n",
6499                     __FUNCTION__);
6500                 rc = EBUSY;
6501         }
6502
6503         DBEXIT(BXE_VERBOSE_MISC);
6504         return (rc);
6505 }
6506
6507
6508 /*
6509  * Release the MCP access lock.
6510  *
6511  * Returns:
6512  *   None.
6513  */
6514 static void
6515 bxe_release_alr(struct bxe_softc* sc)
6516 {
6517
6518         DBENTER(BXE_VERBOSE_MISC);
6519
6520         REG_WR(sc, GRCBASE_MCP + 0x9c, 0);
6521
6522         DBEXIT(BXE_VERBOSE_MISC);
6523 }
6524
6525 /*
6526  * Update driver's copies of the values in the host default status block.
6527  *
6528  * Returns:
6529  *   Bitmap indicating changes to the block.
6530  */
6531 static __inline uint16_t
6532 bxe_update_dsb_idx(struct bxe_softc *sc)
6533 {
6534         struct host_def_status_block *dsb;
6535         uint16_t rc;
6536
6537         rc = 0;
6538         dsb = sc->def_sb;
6539         /* Read memory barrier since block is written by hardware. */
6540         rmb();
6541
6542         if (sc->def_att_idx !=
6543             le16toh(dsb->atten_status_block.attn_bits_index)) {
6544                 sc->def_att_idx =
6545                     le16toh(dsb->atten_status_block.attn_bits_index);
6546                 rc |= 0x1;
6547         }
6548
6549         if (sc->def_c_idx !=
6550             le16toh(dsb->c_def_status_block.status_block_index)) {
6551                 sc->def_c_idx =
6552                     le16toh(dsb->c_def_status_block.status_block_index);
6553                 rc |= 0x2;
6554         }
6555
6556         if (sc->def_u_idx !=
6557             le16toh(dsb->u_def_status_block.status_block_index)) {
6558                 sc->def_u_idx =
6559                     le16toh(dsb->u_def_status_block.status_block_index);
6560                 rc |= 0x4;
6561         }
6562
6563         if (sc->def_x_idx !=
6564             le16toh(dsb->x_def_status_block.status_block_index)) {
6565                 sc->def_x_idx =
6566                     le16toh(dsb->x_def_status_block.status_block_index);
6567                 rc |= 0x8;
6568         }
6569
6570         if (sc->def_t_idx !=
6571             le16toh(dsb->t_def_status_block.status_block_index)) {
6572                 sc->def_t_idx =
6573                     le16toh(dsb->t_def_status_block.status_block_index);
6574                 rc |= 0x10;
6575         }
6576
6577         return (rc);
6578 }
6579
6580 /*
6581  * Handle any attentions that have been newly asserted.
6582  *
6583  * Returns:
6584  *   None
6585  */
6586 static void
6587 bxe_attn_int_asserted(struct bxe_softc *sc, uint32_t asserted)
6588 {
6589         uint32_t aeu_addr, hc_addr, nig_int_mask_addr;
6590         uint32_t aeu_mask, nig_mask;
6591         int port, rc;
6592
6593         DBENTER(BXE_VERBOSE_INTR);
6594
6595         port = BP_PORT(sc);
6596         hc_addr = (HC_REG_COMMAND_REG + port * 32 + COMMAND_REG_ATTN_BITS_SET);
6597         aeu_addr = port ? MISC_REG_AEU_MASK_ATTN_FUNC_1 :
6598             MISC_REG_AEU_MASK_ATTN_FUNC_0;
6599         nig_int_mask_addr = port ? NIG_REG_MASK_INTERRUPT_PORT1 :
6600             NIG_REG_MASK_INTERRUPT_PORT0;
6601         nig_mask = 0;
6602
6603         if (sc->attn_state & asserted)
6604                 BXE_PRINTF("%s(%d): IGU attention ERROR!\n",
6605                     __FILE__, __LINE__);
6606
6607         rc = bxe_acquire_hw_lock(sc,
6608                 HW_LOCK_RESOURCE_PORT0_ATT_MASK + port);
6609         if (rc) {
6610                 DBPRINT(sc, BXE_WARN,
6611                     "%s(): Failed to acquire attention lock for port %d!\n",
6612                     __FUNCTION__, port);
6613                 goto bxe_attn_int_asserted_exit;
6614         }
6615
6616         aeu_mask = REG_RD(sc, aeu_addr);
6617         DBPRINT(sc, BXE_VERBOSE_INTR,
6618             "%s(): aeu_mask = 0x%08X, newly asserted = 0x%08X\n", __FUNCTION__,
6619             aeu_mask, asserted);
6620
6621         aeu_mask &= ~(asserted & 0xff);
6622         DBPRINT(sc, BXE_VERBOSE_INTR, "%s(): new mask = 0x%08X\n", __FUNCTION__,
6623             aeu_mask);
6624         REG_WR(sc, aeu_addr, aeu_mask);
6625
6626         rc = bxe_release_hw_lock(sc, HW_LOCK_RESOURCE_PORT0_ATT_MASK + port);
6627         if (rc) {
6628                 DBPRINT(sc, BXE_WARN,
6629                     "%s(): Failed to release attention lock!\n", __FUNCTION__);
6630                 goto bxe_attn_int_asserted_exit;
6631         }
6632
6633         DBPRINT(sc, BXE_VERBOSE_INTR, "%s(): attn_state = 0x%08X\n",
6634             __FUNCTION__, sc->attn_state);
6635
6636         sc->attn_state |= asserted;
6637         DBPRINT(sc, BXE_VERBOSE_INTR, "%s(): new attn_state = 0x%08X\n",
6638             __FUNCTION__, sc->attn_state);
6639
6640         if (asserted & ATTN_HARD_WIRED_MASK) {
6641                 if (asserted & ATTN_NIG_FOR_FUNC) {
6642                         bxe_acquire_phy_lock(sc);
6643
6644                         /* Save NIG interrupt mask. */
6645                         nig_mask = REG_RD(sc, nig_int_mask_addr);
6646                         REG_WR(sc, nig_int_mask_addr, 0);
6647
6648                         bxe_link_attn(sc);
6649                 }
6650
6651                 if (asserted & ATTN_SW_TIMER_4_FUNC)
6652                         DBPRINT(sc, BXE_WARN, "%s(): ATTN_SW_TIMER_4_FUNC!\n",
6653                             __FUNCTION__);
6654
6655                 if (asserted & GPIO_2_FUNC)
6656                         DBPRINT(sc, BXE_WARN, "%s(): GPIO_2_FUNC!\n",
6657                             __FUNCTION__);
6658
6659                 if (asserted & GPIO_3_FUNC)
6660                         DBPRINT(sc, BXE_WARN, "%s(): GPIO_3_FUNC!\n",
6661                             __FUNCTION__);
6662
6663                 if (asserted & GPIO_4_FUNC)
6664                         DBPRINT(sc, BXE_WARN, "%s(): GPIO_4_FUNC!\n",
6665                             __FUNCTION__);
6666
6667                 if (port == 0) {
6668                         if (asserted & ATTN_GENERAL_ATTN_1) {
6669                                 DBPRINT(sc, BXE_WARN,
6670                                     "%s(): ATTN_GENERAL_ATTN_1!\n",
6671                                     __FUNCTION__);
6672                                 REG_WR(sc, MISC_REG_AEU_GENERAL_ATTN_1, 0x0);
6673                         }
6674
6675                         if (asserted & ATTN_GENERAL_ATTN_2) {
6676                                 DBPRINT(sc, BXE_WARN,
6677                                     "%s(): ATTN_GENERAL_ATTN_2!\n",
6678                                     __FUNCTION__);
6679                                 REG_WR(sc, MISC_REG_AEU_GENERAL_ATTN_2, 0x0);
6680                         }
6681
6682                         if (asserted & ATTN_GENERAL_ATTN_3) {
6683                                 DBPRINT(sc, BXE_WARN,
6684                                     "%s(): ATTN_GENERAL_ATTN_3!\n",
6685                                     __FUNCTION__);
6686                                 REG_WR(sc, MISC_REG_AEU_GENERAL_ATTN_3, 0x0);
6687                         }
6688                 } else {
6689                         if (asserted & ATTN_GENERAL_ATTN_4) {
6690                                 DBPRINT(sc, BXE_WARN,
6691                                     "%s(): ATTN_GENERAL_ATTN_4!\n",
6692                                     __FUNCTION__);
6693                                 REG_WR(sc, MISC_REG_AEU_GENERAL_ATTN_4, 0x0);
6694                         }
6695
6696                         if (asserted & ATTN_GENERAL_ATTN_5) {
6697                                 DBPRINT(sc, BXE_WARN,
6698                                     "%s(): ATTN_GENERAL_ATTN_5!\n",
6699                                     __FUNCTION__);
6700                                 REG_WR(sc, MISC_REG_AEU_GENERAL_ATTN_5, 0x0);
6701                         }
6702                         if (asserted & ATTN_GENERAL_ATTN_6) {
6703                                 DBPRINT(sc, BXE_WARN,
6704                                     "%s(): ATTN_GENERAL_ATTN_6!\n",
6705                                     __FUNCTION__);
6706                                 REG_WR(sc, MISC_REG_AEU_GENERAL_ATTN_6, 0x0);
6707                         }
6708                 }
6709         }
6710
6711         DBPRINT(sc, BXE_VERBOSE_INTR,
6712             "%s(): Writing 0x%08X to HC addr 0x%08X\n", __FUNCTION__,
6713             asserted, hc_addr);
6714         REG_WR(sc, hc_addr, asserted);
6715
6716         /* Now set back the NIG mask. */
6717         if (asserted & ATTN_NIG_FOR_FUNC) {
6718                 REG_WR(sc, nig_int_mask_addr, nig_mask);
6719                 bxe_release_phy_lock(sc);
6720         }
6721
6722 bxe_attn_int_asserted_exit:
6723         DBEXIT(BXE_VERBOSE_INTR);
6724 }
6725
6726 /*
6727  * Handle any attentions that have been newly deasserted.
6728  *
6729  * Returns:
6730  *   None
6731  */
6732 static __inline void
6733 bxe_attn_int_deasserted0(struct bxe_softc *sc, uint32_t attn)
6734 {
6735         uint32_t val, swap_val, swap_override;
6736         int port, reg_offset;
6737
6738         DBENTER(BXE_VERBOSE_INTR);
6739
6740         port = BP_PORT(sc);
6741         reg_offset = port ? MISC_REG_AEU_ENABLE1_FUNC_1_OUT_0 :
6742             MISC_REG_AEU_ENABLE1_FUNC_0_OUT_0;
6743
6744         /* Handle SPIO5 attention. */
6745         if (attn & AEU_INPUTS_ATTN_BITS_SPIO5) {
6746                 val = REG_RD(sc, reg_offset);
6747                 val &= ~AEU_INPUTS_ATTN_BITS_SPIO5;
6748                 REG_WR(sc, reg_offset, val);
6749
6750                 DBPRINT(sc, BXE_FATAL, "%s(): SPIO5 H/W attention!\n",
6751                     __FUNCTION__);
6752                 /* Fan failure attention */
6753                 switch (XGXS_EXT_PHY_TYPE(sc->link_params.ext_phy_config)) {
6754                 case PORT_HW_CFG_XGXS_EXT_PHY_TYPE_SFX7101:
6755                         /*
6756                          * SPIO5 is used on A1022G boards to indicate
6757                          * fan failure.  Shutdown the controller and
6758                          * associated PHY to avoid damage.
6759                          */
6760
6761                         /* Low power mode is controled by GPIO 2. */
6762                         bxe_set_gpio(sc, MISC_REGISTERS_GPIO_2,
6763                                 MISC_REGISTERS_GPIO_OUTPUT_LOW, port);
6764                         /* PHY reset is controled by GPIO 1. */
6765                         bxe_set_gpio(sc, MISC_REGISTERS_GPIO_1,
6766                                 MISC_REGISTERS_GPIO_OUTPUT_LOW, port);
6767                         break;
6768                 case PORT_HW_CFG_XGXS_EXT_PHY_TYPE_BCM8727:
6769                 case PORT_HW_CFG_XGXS_EXT_PHY_TYPE_BCM8481:
6770                         /*
6771                          * The PHY reset is controlled by GPIO 1.
6772                          * Fake the port number to cancel the swap done in
6773                          * set_gpio().
6774                          */
6775                         swap_val = REG_RD(sc, NIG_REG_PORT_SWAP);
6776                         swap_override = REG_RD(sc, NIG_REG_STRAP_OVERRIDE);
6777                         port = (swap_val && swap_override) ^ 1;
6778                         bxe_set_gpio(sc, MISC_REGISTERS_GPIO_1,
6779                             MISC_REGISTERS_GPIO_OUTPUT_LOW, port);
6780                         break;
6781                 default:
6782                         break;
6783                 }
6784
6785                 /* Mark the failure. */
6786                 sc->link_params.ext_phy_config &=
6787                     ~PORT_HW_CFG_XGXS_EXT_PHY_TYPE_MASK;
6788                 sc->link_params.ext_phy_config |=
6789                     PORT_HW_CFG_XGXS_EXT_PHY_TYPE_FAILURE;
6790                 SHMEM_WR(sc, dev_info.port_hw_config[port].external_phy_config,
6791                     sc->link_params.ext_phy_config);
6792                 /* Log the failure */
6793                 BXE_PRINTF("A fan failure has caused the driver to "
6794                     "shutdown the device to prevent permanent damage.\n");
6795         }
6796
6797         if (attn & (AEU_INPUTS_ATTN_BITS_GPIO3_FUNCTION_0 |
6798             AEU_INPUTS_ATTN_BITS_GPIO3_FUNCTION_1)) {
6799                 bxe_acquire_phy_lock(sc);
6800                 bxe_handle_module_detect_int(&sc->link_params);
6801                 bxe_release_phy_lock(sc);
6802         }
6803
6804         /* Checking for an assert on the zero block */
6805         if (attn & HW_INTERRUT_ASSERT_SET_0) {
6806                 val = REG_RD(sc, reg_offset);
6807                 val &= ~(attn & HW_INTERRUT_ASSERT_SET_0);
6808                 REG_WR(sc, reg_offset, val);
6809
6810                 BXE_PRINTF("%s(%d): FATAL hardware block attention "
6811                     "(set0 = 0x%08X)!\n", __FILE__, __LINE__,
6812                     (attn & (uint32_t)HW_INTERRUT_ASSERT_SET_0));
6813
6814                 bxe_panic_dump(sc);
6815         }
6816
6817         DBEXIT(BXE_VERBOSE_INTR);
6818 }
6819
6820 /*
6821  * Handle any attentions that have been newly deasserted.
6822  *
6823  * Returns:
6824  *   None
6825  */
6826 static __inline void
6827 bxe_attn_int_deasserted1(struct bxe_softc *sc, uint32_t attn)
6828 {
6829         uint32_t val;
6830         int port, reg_offset;
6831
6832         DBENTER(BXE_VERBOSE_INTR);
6833
6834         if (attn & AEU_INPUTS_ATTN_BITS_DOORBELLQ_HW_INTERRUPT) {
6835                 val = REG_RD(sc, DORQ_REG_DORQ_INT_STS_CLR);
6836
6837                 DBPRINT(sc, BXE_FATAL,
6838                     "%s(): Doorbell hardware attention (0x%08X).\n",
6839                     __FUNCTION__, val);
6840
6841                 /* DORQ discard attention */
6842                 if (val & 0x2)
6843                         DBPRINT(sc, BXE_FATAL,
6844                             "%s(): FATAL doorbell queue error!\n",
6845                             __FUNCTION__);
6846         }
6847
6848         if (attn & HW_INTERRUT_ASSERT_SET_1) {
6849                 port = BP_PORT(sc);
6850                 reg_offset = port ? MISC_REG_AEU_ENABLE1_FUNC_1_OUT_1 :
6851                     MISC_REG_AEU_ENABLE1_FUNC_0_OUT_1;
6852
6853                 val = REG_RD(sc, reg_offset);
6854                 val &= ~(attn & HW_INTERRUT_ASSERT_SET_1);
6855                 REG_WR(sc, reg_offset, val);
6856
6857                 BXE_PRINTF("%s(%d): FATAL hardware block attention "
6858                     "(set1 = 0x%08X)!\n", __FILE__, __LINE__,
6859                     (attn & (uint32_t)HW_INTERRUT_ASSERT_SET_1));
6860
6861                 bxe_panic_dump(sc);
6862         }
6863
6864         DBEXIT(BXE_VERBOSE_INTR);
6865 }
6866
6867 /*
6868  * Handle any attentions that have been newly deasserted.
6869  *
6870  * Returns:
6871  *   None
6872  */
6873 static __inline void
6874 bxe_attn_int_deasserted2(struct bxe_softc *sc, uint32_t attn)
6875 {
6876         uint32_t val;
6877         int port, reg_offset;
6878
6879         DBENTER(BXE_VERBOSE_INTR);
6880
6881         if (attn & AEU_INPUTS_ATTN_BITS_CFC_HW_INTERRUPT) {
6882                 val = REG_RD(sc, CFC_REG_CFC_INT_STS_CLR);
6883
6884                 DBPRINT(sc, BXE_FATAL,
6885                     "%s(): CFC hardware attention (0x%08X).\n", __FUNCTION__,
6886                     val);
6887
6888                 /* CFC error attention. */
6889                 if (val & 0x2)
6890                         DBPRINT(sc, BXE_FATAL, "%s(): FATAL CFC error!\n",
6891                             __FUNCTION__);
6892         }
6893
6894         if (attn & AEU_INPUTS_ATTN_BITS_PXP_HW_INTERRUPT) {
6895                 val = REG_RD(sc, PXP_REG_PXP_INT_STS_CLR_0);
6896
6897                 DBPRINT(sc, BXE_FATAL,
6898                     "%s(): PXP hardware attention (0x%08X).\n", __FUNCTION__,
6899                     val);
6900
6901                 /* RQ_USDMDP_FIFO_OVERFLOW */
6902                 if (val & 0x18000)
6903                         DBPRINT(sc, BXE_FATAL, "%s(): FATAL PXP error!\n",
6904                             __FUNCTION__);
6905         }
6906
6907         if (attn & HW_INTERRUT_ASSERT_SET_2) {
6908                 port = BP_PORT(sc);
6909                 reg_offset = port ? MISC_REG_AEU_ENABLE1_FUNC_1_OUT_2 :
6910                     MISC_REG_AEU_ENABLE1_FUNC_0_OUT_2;
6911
6912                 val = REG_RD(sc, reg_offset);
6913                 val &= ~(attn & HW_INTERRUT_ASSERT_SET_2);
6914                 REG_WR(sc, reg_offset, val);
6915
6916                 BXE_PRINTF("%s(%d): FATAL hardware block attention (set2 = "
6917                     "0x%08X)! port=%d, val written=0x%x attn=0x%x\n", __FILE__,
6918                     __LINE__, (attn & (uint32_t)HW_INTERRUT_ASSERT_SET_2),
6919                     port, val, attn);
6920
6921                 bxe_panic_dump(sc);
6922         }
6923
6924         DBEXIT(BXE_VERBOSE_INTR);
6925 }
6926
6927 /*
6928  * Handle any attentions that have been newly deasserted.
6929  *
6930  * Returns:
6931  *   None
6932  */
6933 static __inline void
6934 bxe_attn_int_deasserted3(struct bxe_softc *sc, uint32_t attn)
6935 {
6936         uint32_t val;
6937         int func;
6938
6939         DBENTER(BXE_VERBOSE_INTR);
6940
6941         if (attn & EVEREST_GEN_ATTN_IN_USE_MASK) {
6942                 /* Look for any port assertions. */
6943                 if (attn & BXE_PMF_LINK_ASSERT) {
6944                         /*
6945                          * We received a message from the driver instance
6946                          * that is managing the Ethernet port (link up/down).
6947                          * Go ahead and handle it.
6948                          */
6949                         func = BP_FUNC(sc);
6950
6951                         DBPRINT(sc, BXE_INFO,
6952                             "%s(): Received link attention from PMF.\n",
6953                             __FUNCTION__);
6954
6955                         /* Clear the attention. */
6956                         REG_WR(sc, MISC_REG_AEU_GENERAL_ATTN_12 + func * 4, 0);
6957                         sc->mf_config[BP_E1HVN(sc)] =
6958                             SHMEM_RD(sc,
6959                             mf_cfg.func_mf_config[(sc->bxe_func & 1)].config);
6960                         val = SHMEM_RD(sc, func_mb[func].drv_status);
6961                         if (sc->dcc_enable == TRUE) {
6962                                 if (val & DRV_STATUS_DCC_EVENT_MASK)
6963                                         bxe_dcc_event(sc,
6964                                             val & DRV_STATUS_DCC_EVENT_MASK);
6965                         }
6966                         bxe__link_status_update(sc);
6967
6968                         if ((sc->port.pmf == 0) && (val & DRV_STATUS_PMF))
6969                                 bxe_pmf_update(sc);
6970                 /* Look for any microcode assertions. */
6971                 } else if (attn & BXE_MC_ASSERT_BITS) {
6972                         DBPRINT(sc, BXE_FATAL, "%s(): Microcode assert!\n",
6973                             __FUNCTION__);
6974
6975                         REG_WR(sc, MISC_REG_AEU_GENERAL_ATTN_10, 0);
6976                         REG_WR(sc, MISC_REG_AEU_GENERAL_ATTN_9, 0);
6977                         REG_WR(sc, MISC_REG_AEU_GENERAL_ATTN_8, 0);
6978                         REG_WR(sc, MISC_REG_AEU_GENERAL_ATTN_7, 0);
6979
6980                         bxe_panic_dump(sc);
6981
6982                 /* Look for any bootcode assertions. */
6983                 } else if (attn & BXE_MCP_ASSERT) {
6984                         DBPRINT(sc, BXE_FATAL, "%s(): Bootcode assert!\n",
6985                                 __FUNCTION__);
6986
6987                         REG_WR(sc, MISC_REG_AEU_GENERAL_ATTN_11, 0);
6988
6989                         DBRUN(bxe_dump_fw(sc));
6990                 } else
6991                         DBPRINT(sc, BXE_FATAL,
6992                             "%s(): Unknown hardware assertion "
6993                             "(attn = 0x%08X)!\n", __FUNCTION__, attn);
6994         }
6995
6996         /* Look for any hardware latched attentions. */
6997         if (attn & EVEREST_LATCHED_ATTN_IN_USE_MASK) {
6998                 DBPRINT(sc, BXE_FATAL,
6999                     "%s(): Latched attention 0x%08X (masked)!\n", __FUNCTION__,
7000                     attn);
7001
7002                 /* Check if a GRC register access timeout occurred. */
7003                 if (attn & BXE_GRC_TIMEOUT) {
7004                         val = CHIP_IS_E1H(sc) ? REG_RD(sc,
7005                             MISC_REG_GRC_TIMEOUT_ATTN) : 0;
7006
7007                         DBPRINT(sc, BXE_WARN,
7008                             "%s(): GRC timeout for register 0x%08X!\n",
7009                             __FUNCTION__, val);
7010                 }
7011
7012                 /* Check if a GRC reserved register was accessed. */
7013                 if (attn & BXE_GRC_RSV) {
7014                         val = CHIP_IS_E1H(sc) ? REG_RD(sc,
7015                             MISC_REG_GRC_RSV_ATTN) : 0;
7016
7017                         DBPRINT(sc, BXE_WARN,
7018                             "%s(): GRC register 0x%08X is reserved!\n",
7019                             __FUNCTION__, val);
7020                 }
7021
7022                 REG_WR(sc, MISC_REG_AEU_CLR_LATCH_SIGNAL, 0x7ff);
7023         }
7024
7025         DBEXIT(BXE_VERBOSE_INTR);
7026 }
7027
7028 /*
7029  * Handle any attentions that have been newly deasserted.
7030  *
7031  * Returns:
7032  *   None
7033  */
7034 static void
7035 bxe_attn_int_deasserted(struct bxe_softc *sc, uint32_t deasserted)
7036 {
7037         struct attn_route attn;
7038         struct attn_route group_mask;
7039         uint32_t val, reg_addr, aeu_mask;
7040         int index, port;
7041
7042         DBENTER(BXE_VERBOSE_INTR);
7043
7044         /*
7045          * Need to take HW lock because MCP or other port might also try
7046          * to handle this event.
7047          */
7048         bxe_acquire_alr(sc);
7049
7050         port = BP_PORT(sc);
7051         /* Get the current attention signal bits. */
7052         attn.sig[0] = REG_RD(sc,
7053             MISC_REG_AEU_AFTER_INVERT_1_FUNC_0 + port * 4);
7054         attn.sig[1] = REG_RD(sc,
7055             MISC_REG_AEU_AFTER_INVERT_2_FUNC_0 + port * 4);
7056         attn.sig[2] = REG_RD(sc,
7057             MISC_REG_AEU_AFTER_INVERT_3_FUNC_0 + port * 4);
7058         attn.sig[3] = REG_RD(sc,
7059             MISC_REG_AEU_AFTER_INVERT_4_FUNC_0 + port * 4);
7060
7061         DBPRINT(sc, BXE_EXTREME_INTR,
7062             "%s(): attention = 0x%08X 0x%08X 0x%08X 0x%08X\n", __FUNCTION__,
7063             attn.sig[0], attn.sig[1], attn.sig[2], attn.sig[3]);
7064
7065         /*
7066          * Compare the current attention bits to each attention group
7067          * to see if anyone has registered this attention.
7068          */
7069         for (index = 0; index < MAX_DYNAMIC_ATTN_GRPS; index++) {
7070                 if (deasserted & (1 << index)) {
7071                         group_mask = sc->attn_group[index];
7072
7073                         DBPRINT(sc, BXE_EXTREME_INTR,
7074                             "%s(): group[%02d] = 0x%08X 0x%08X 0x%08x 0X%08x\n",
7075                             __FUNCTION__, index, group_mask.sig[0],
7076                             group_mask.sig[1], group_mask.sig[2],
7077                             group_mask.sig[3]);
7078
7079                         /* Handle any registered attentions. */
7080                         bxe_attn_int_deasserted3(sc,
7081                             attn.sig[3] & group_mask.sig[3]);
7082                         bxe_attn_int_deasserted1(sc,
7083                             attn.sig[1] & group_mask.sig[1]);
7084                         bxe_attn_int_deasserted2(sc,
7085                             attn.sig[2] & group_mask.sig[2]);
7086                         bxe_attn_int_deasserted0(sc,
7087                             attn.sig[0] & group_mask.sig[0]);
7088
7089                         if ((attn.sig[0] & group_mask.sig[0] &
7090                             HW_PRTY_ASSERT_SET_0) ||
7091                             (attn.sig[1] & group_mask.sig[1] &
7092                             HW_PRTY_ASSERT_SET_1) ||
7093                             (attn.sig[2] & group_mask.sig[2] &
7094                             HW_PRTY_ASSERT_SET_2))
7095                                 BXE_PRINTF("%s(%d): FATAL hardware block "
7096                                     "parity attention!\n", __FILE__, __LINE__);
7097                 }
7098         }
7099
7100         bxe_release_alr(sc);
7101
7102         reg_addr = (HC_REG_COMMAND_REG +
7103             port * 32 + COMMAND_REG_ATTN_BITS_CLR);
7104
7105         val = ~deasserted;
7106         DBPRINT(sc, BXE_EXTREME_INTR,
7107             "%s(): About to mask 0x%08X at HC addr 0x%08X\n", __FUNCTION__,
7108             deasserted, reg_addr);
7109         REG_WR(sc, reg_addr, val);
7110
7111         if (~sc->attn_state & deasserted)
7112                 DBPRINT(sc, BXE_FATAL, "%s(): IGU Bug!\n", __FUNCTION__);
7113
7114         reg_addr = port ? MISC_REG_AEU_MASK_ATTN_FUNC_1 :
7115             MISC_REG_AEU_MASK_ATTN_FUNC_0;
7116
7117         bxe_acquire_hw_lock(sc, HW_LOCK_RESOURCE_PORT0_ATT_MASK + port);
7118         aeu_mask = REG_RD(sc, reg_addr);
7119
7120         DBPRINT(sc, BXE_EXTREME_INTR,
7121             "%s(): Current aeu_mask = 0x%08X, newly deasserted = 0x%08X\n",
7122             __FUNCTION__, aeu_mask, deasserted);
7123         aeu_mask |= (deasserted & 0xff);
7124
7125         DBPRINT(sc, BXE_EXTREME_INTR, "%s(): New aeu_mask = 0x%08X\n",
7126             __FUNCTION__, aeu_mask);
7127
7128         REG_WR(sc, reg_addr, aeu_mask);
7129         bxe_release_hw_lock(sc, HW_LOCK_RESOURCE_PORT0_ATT_MASK + port);
7130
7131         DBPRINT(sc, BXE_EXTREME_INTR, "%s(): Current attn_state = 0x%08X\n",
7132             __FUNCTION__, sc->attn_state);
7133
7134         sc->attn_state &= ~deasserted;
7135         DBPRINT(sc, BXE_EXTREME_INTR, "%s(): New attn_state = 0x%08X\n",
7136             __FUNCTION__, sc->attn_state);
7137
7138         DBEXIT(BXE_VERBOSE_INTR);
7139 }
7140
7141 /*
7142  * Handle interrupts caused by internal attentions (everything else other
7143  * than RX, TX, and link state changes).
7144  *
7145  * Returns:
7146  *   None
7147  */
7148 static void
7149 bxe_attn_int(struct bxe_softc* sc)
7150 {
7151         uint32_t attn_ack, attn_bits, attn_state;
7152         uint32_t asserted, deasserted;
7153
7154         DBENTER(BXE_VERBOSE_INTR);
7155
7156         attn_bits = le32toh(sc->def_sb->atten_status_block.attn_bits);
7157         attn_ack =
7158             le32toh(sc->def_sb->atten_status_block.attn_bits_ack);
7159         attn_state = sc->attn_state;
7160         asserted = attn_bits & ~attn_ack & ~attn_state;
7161         deasserted = ~attn_bits &  attn_ack &  attn_state;
7162
7163         /* Make sure we're in a sane state. */
7164         if (~(attn_bits ^ attn_ack) & (attn_bits ^ attn_state))
7165             BXE_PRINTF("%s(%d): Bad attention state!\n",
7166                 __FILE__, __LINE__);
7167
7168         /* Handle any attentions that are newly asserted. */
7169         if (asserted) {
7170                 DBPRINT(sc, BXE_VERBOSE_INTR,
7171                     "%s(): attn_state = 0x%08X, attn_bits = 0x%08X, "
7172                     "attn_ack = 0x%08X, asserted = 0x%08X\n", __FUNCTION__,
7173                     attn_state, attn_bits, attn_ack, asserted);
7174                 bxe_attn_int_asserted(sc, asserted);
7175         }
7176
7177         /* Handle any attentions that are newly deasserted. */
7178         if (deasserted) {
7179                 DBPRINT(sc, BXE_VERBOSE_INTR,
7180                     "%s(): attn_state = 0x%08X, attn_bits = 0x%08X, "
7181                     "attn_ack = 0x%08X, deasserted = 0x%08X\n", __FUNCTION__,
7182                     attn_state, attn_bits, attn_ack, deasserted);
7183                 bxe_attn_int_deasserted(sc, deasserted);
7184         }
7185
7186         DBEXIT(BXE_VERBOSE_INTR);
7187 }
7188
7189 /* sum[hi:lo] += add[hi:lo] */
7190 #define ADD_64(s_hi, a_hi, s_lo, a_lo) do {                     \
7191         s_lo += a_lo;                                           \
7192         s_hi += a_hi + ((s_lo < a_lo) ? 1 : 0);                 \
7193 } while (0)
7194
7195 /* Subtraction = minuend -= subtrahend */
7196 #define SUB_64(m_hi, s_hi, m_lo, s_lo)                          \
7197         do {                                                    \
7198                 DIFF_64(m_hi, m_hi, s_hi, m_lo, m_lo, s_lo);    \
7199         } while (0)
7200
7201
7202 /* difference = minuend - subtrahend */
7203 #define DIFF_64(d_hi, m_hi, s_hi, d_lo, m_lo, s_lo) do {        \
7204         if (m_lo < s_lo) {                                      \
7205                 /* underflow */                                 \
7206                 d_hi = m_hi - s_hi;                             \
7207                 if (d_hi > 0) {                                 \
7208                         /* we can 'loan' 1 */                   \
7209                         d_hi--;                                 \
7210                         d_lo = m_lo + (UINT_MAX - s_lo) + 1;    \
7211                 } else {                                        \
7212                         /* m_hi <= s_hi */                      \
7213                         d_hi = 0;                               \
7214                         d_lo = 0;                               \
7215                 }                                               \
7216         } else {                                                \
7217                 /* m_lo >= s_lo */                              \
7218                 if (m_hi < s_hi) {                              \
7219                         d_hi = 0;                               \
7220                         d_lo = 0;                               \
7221                 } else {                                        \
7222                         /* m_hi >= s_hi */                      \
7223                         d_hi = m_hi - s_hi;                     \
7224                         d_lo = m_lo - s_lo;                     \
7225                 }                                               \
7226         }                                                       \
7227 } while (0)
7228
7229 #define UPDATE_STAT64(s, t) do {                                \
7230         DIFF_64(diff.hi, new->s##_hi, pstats->mac_stx[0].t##_hi,\
7231             diff.lo, new->s##_lo, pstats->mac_stx[0].t##_lo);   \
7232         pstats->mac_stx[0].t##_hi = new->s##_hi;                \
7233         pstats->mac_stx[0].t##_lo = new->s##_lo;                \
7234         ADD_64(pstats->mac_stx[1].t##_hi, diff.hi,              \
7235             pstats->mac_stx[1].t##_lo, diff.lo);                \
7236 } while (0)
7237
7238 #define UPDATE_STAT64_NIG(s, t) do {                            \
7239         DIFF_64(diff.hi, new->s##_hi, old->s##_hi,              \
7240             diff.lo, new->s##_lo, old->s##_lo);                 \
7241         ADD_64(estats->t##_hi, diff.hi,                         \
7242             estats->t##_lo, diff.lo);                           \
7243 } while (0)
7244
7245 /* sum[hi:lo] += add */
7246 #define ADD_EXTEND_64(s_hi, s_lo, a) do {                       \
7247         s_lo += a;                                              \
7248         s_hi += (s_lo < a) ? 1 : 0;                             \
7249 } while (0)
7250
7251 #define UPDATE_EXTEND_STAT(s) do {                              \
7252         ADD_EXTEND_64(pstats->mac_stx[1].s##_hi,                \
7253             pstats->mac_stx[1].s##_lo, new->s);                 \
7254 } while (0)
7255
7256 #define UPDATE_EXTEND_TSTAT(s, t) do {                          \
7257         diff = (tclient->s) - (old_tclient->s); \
7258         old_tclient->s = (tclient->s);                          \
7259         ADD_EXTEND_64(qstats->t##_hi, qstats->t##_lo, diff);    \
7260 } while (0)
7261
7262 #define UPDATE_EXTEND_XSTAT(s, t) do {                          \
7263         diff = xclient->s - old_xclient->s;     \
7264         old_xclient->s = xclient->s;                            \
7265         ADD_EXTEND_64(qstats->t##_hi, qstats->t##_lo, diff);    \
7266 } while (0)
7267
7268 #define UPDATE_EXTEND_USTAT(s, t) do {                          \
7269         diff = uclient->s - old_uclient->s;     \
7270         old_uclient->s = uclient->s;                            \
7271         ADD_EXTEND_64(qstats->t##_hi, qstats->t##_lo, diff);    \
7272 } while (0)
7273
7274 #define SUB_EXTEND_64(m_hi, m_lo, s)do {                        \
7275         SUB_64(m_hi, 0, m_lo, s);                               \
7276 } while (0)
7277
7278 #define SUB_EXTEND_USTAT(s, t)do {                              \
7279         diff = (uclient->s) - (old_uclient->s); \
7280         SUB_EXTEND_64(qstats->t##_hi, qstats->t##_lo, diff);    \
7281 } while (0)
7282
7283
7284
7285
7286 #ifdef __i386__
7287 #define BITS_PER_LONG   32
7288 #else
7289 #define BITS_PER_LONG   64
7290 #endif
7291
7292 static __inline long
7293 bxe_hilo(uint32_t *hiref)
7294 {
7295         uint32_t lo;
7296
7297         lo = *(hiref + 1);
7298 #if (BITS_PER_LONG == 64)
7299         uint32_t hi = *hiref;
7300         return (HILO_U64(hi, lo));
7301 #else
7302         return (lo);
7303 #endif
7304 }
7305
7306 /*
7307  * Request the STORM statistics by posting a slowpath ramrod.
7308  *
7309  * Returns:
7310  *   None.
7311  */
7312 static void
7313 bxe_stats_storm_post(struct bxe_softc *sc)
7314 {
7315         struct eth_query_ramrod_data ramrod_data = {0};
7316         int i, rc;
7317
7318         DBENTER(BXE_INSANE_STATS);
7319
7320         if (!sc->stats_pending) {
7321                 ramrod_data.drv_counter = sc->stats_counter++;
7322                 ramrod_data.collect_port = sc->port.pmf ? 1 : 0;
7323                 for (i = 0; i < sc->num_queues; i++)
7324                         ramrod_data.ctr_id_vector |= (1 << sc->fp[i].cl_id);
7325
7326                 rc = bxe_sp_post(sc, RAMROD_CMD_ID_ETH_STAT_QUERY, 0,
7327                     ((uint32_t *)&ramrod_data)[1],
7328                     ((uint32_t *)&ramrod_data)[0], 0);
7329                 if (rc == 0) {
7330                         /* Stats ramrod has it's own slot on the SPQ. */
7331                         sc->spq_left++;
7332                         sc->stats_pending = 1;
7333                 }
7334         }
7335
7336         DBEXIT(BXE_INSANE_STATS);
7337 }
7338
7339 /*
7340  * Setup the adrress used by the driver to report port-based statistics
7341  * back to the controller.
7342  *
7343  * Returns:
7344  *   None.
7345  */
7346 static void
7347 bxe_stats_port_base_init(struct bxe_softc *sc)
7348 {
7349         uint32_t *stats_comp;
7350         struct dmae_command *dmae;
7351
7352         DBENTER(BXE_VERBOSE_STATS);
7353
7354         /* Only the port management function (PMF) does this work. */
7355         if ((sc->port.pmf == 0) || !sc->port.port_stx) {
7356                 BXE_PRINTF("%s(%d): Invalid statistcs port setup!\n",
7357                     __FILE__, __LINE__);
7358                 goto bxe_stats_port_base_init_exit;
7359         }
7360
7361         stats_comp = BXE_SP(sc, stats_comp);
7362         sc->executer_idx = 0;
7363
7364         /* DMA the address of the drivers port statistics block. */
7365         dmae = BXE_SP(sc, dmae[sc->executer_idx++]);
7366         dmae->opcode = (DMAE_CMD_SRC_PCI | DMAE_CMD_DST_GRC |
7367                         DMAE_CMD_C_DST_PCI | DMAE_CMD_C_ENABLE |
7368                         DMAE_CMD_SRC_RESET | DMAE_CMD_DST_RESET |
7369 #ifdef __BIG_ENDIAN
7370                         DMAE_CMD_ENDIANITY_B_DW_SWAP |
7371 #else
7372                         DMAE_CMD_ENDIANITY_DW_SWAP |
7373 #endif
7374                         (BP_PORT(sc) ? DMAE_CMD_PORT_1 : DMAE_CMD_PORT_0) |
7375                         (BP_E1HVN(sc) << DMAE_CMD_E1HVN_SHIFT));
7376         dmae->src_addr_lo = U64_LO(BXE_SP_MAPPING(sc, port_stats));
7377         dmae->src_addr_hi = U64_HI(BXE_SP_MAPPING(sc, port_stats));
7378         dmae->dst_addr_lo = sc->port.port_stx >> 2;
7379         dmae->dst_addr_hi = 0;
7380         dmae->len = sizeof(struct host_port_stats) >> 2;
7381         dmae->comp_addr_lo = U64_LO(BXE_SP_MAPPING(sc, stats_comp));
7382         dmae->comp_addr_hi = U64_HI(BXE_SP_MAPPING(sc, stats_comp));
7383         dmae->comp_val = DMAE_COMP_VAL;
7384
7385         *stats_comp = 0;
7386         bxe_stats_hw_post(sc);
7387         bxe_stats_comp(sc);
7388
7389 bxe_stats_port_base_init_exit:
7390         DBEXIT(BXE_VERBOSE_STATS);
7391 }
7392
7393 /*
7394  * Setup the adrress used by the driver to report function-based statistics
7395  * back to the controller.
7396  *
7397  * Returns:
7398  *   None.
7399  */
7400 static void
7401 bxe_stats_func_base_init(struct bxe_softc *sc)
7402 {
7403         int port, func;
7404         int vn, vn_max;
7405         uint32_t func_stx;
7406
7407         DBENTER(BXE_VERBOSE_STATS);
7408
7409         /* Only the port management function (PMF) does this work. */
7410         if ((sc->port.pmf == 0) || !sc->func_stx) {
7411                 BXE_PRINTF("%s(%d): Invalid statistcs function setup!\n",
7412                     __FILE__, __LINE__);
7413                 goto bxe_stats_func_base_init_exit;
7414         }
7415
7416         port = BP_PORT(sc);
7417         func_stx = sc->func_stx;
7418         vn_max = IS_E1HMF(sc) ? E1HVN_MAX : E1VN_MAX;
7419
7420         /* Initialize each function individually. */
7421         for (vn = VN_0; vn < vn_max; vn++) {
7422                 func = 2 * vn + port;
7423                 sc->func_stx = SHMEM_RD(sc, func_mb[func].fw_mb_param);
7424                 bxe_stats_func_init(sc);
7425                 bxe_stats_hw_post(sc);
7426                 bxe_stats_comp(sc);
7427         }
7428
7429         sc->func_stx = func_stx;
7430
7431 bxe_stats_func_base_init_exit:
7432         DBEXIT(BXE_VERBOSE_STATS);
7433 }
7434
7435 /*
7436  * DMA the function-based statistics to the controller.
7437  *
7438  * Returns:
7439  *   None.
7440  */
7441 static void
7442 bxe_stats_func_base_update(struct bxe_softc *sc)
7443 {
7444         uint32_t *stats_comp;
7445         struct dmae_command *dmae;
7446
7447         DBENTER(BXE_VERBOSE_STATS);
7448
7449         /* Only the port management function (PMF) does this work. */
7450         if ((sc->port.pmf == 0) || !sc->func_stx) {
7451                 BXE_PRINTF("%s(%d): Invalid statistcs function update!\n",
7452                     __FILE__, __LINE__);
7453                 goto bxe_stats_func_base_update_exit;
7454         }
7455
7456         dmae = &sc->stats_dmae;
7457         stats_comp = BXE_SP(sc, stats_comp);
7458         sc->executer_idx = 0;
7459         memset(dmae, 0, sizeof(struct dmae_command));
7460
7461         /* DMA the function statistics from the driver to the H/W. */
7462         dmae->opcode = (DMAE_CMD_SRC_GRC | DMAE_CMD_DST_PCI |
7463                         DMAE_CMD_C_DST_PCI | DMAE_CMD_C_ENABLE |
7464                         DMAE_CMD_SRC_RESET | DMAE_CMD_DST_RESET |
7465 #ifdef __BIG_ENDIAN
7466                         DMAE_CMD_ENDIANITY_B_DW_SWAP |
7467 #else
7468                         DMAE_CMD_ENDIANITY_DW_SWAP |
7469 #endif
7470                         (BP_PORT(sc) ? DMAE_CMD_PORT_1 : DMAE_CMD_PORT_0) |
7471                         (BP_E1HVN(sc) << DMAE_CMD_E1HVN_SHIFT));
7472         dmae->src_addr_lo = sc->func_stx >> 2;
7473         dmae->src_addr_hi = 0;
7474         dmae->dst_addr_lo = U64_LO(BXE_SP_MAPPING(sc, func_stats_base));
7475         dmae->dst_addr_hi = U64_HI(BXE_SP_MAPPING(sc, func_stats_base));
7476         dmae->len = sizeof(struct host_func_stats) >> 2;
7477         dmae->comp_addr_lo = U64_LO(BXE_SP_MAPPING(sc, stats_comp));
7478         dmae->comp_addr_hi = U64_HI(BXE_SP_MAPPING(sc, stats_comp));
7479         dmae->comp_val = DMAE_COMP_VAL;
7480
7481         *stats_comp = 0;
7482         bxe_stats_hw_post(sc);
7483         bxe_stats_comp(sc);
7484
7485 bxe_stats_func_base_update_exit:
7486         DBEXIT(BXE_VERBOSE_STATS);
7487 }
7488
7489
7490 /*
7491  * Initialize statistics.
7492  *
7493  * Returns:
7494  *   Nothing.
7495  */
7496 static void
7497 bxe_stats_init(struct bxe_softc *sc)
7498 {
7499         struct bxe_fastpath *fp;
7500         int func, i, port;
7501
7502         DBENTER(BXE_VERBOSE_STATS);
7503
7504         if (sc->stats_enable == FALSE)
7505                 goto bxe_stats_init_exit;
7506
7507         port = BP_PORT(sc);
7508         func = BP_FUNC(sc);
7509         sc->executer_idx  = 0;
7510         sc->stats_counter = 0;
7511         sc->stats_pending = 0;
7512
7513         /* Fetch the offset of port & function statistics in shared memory. */
7514         if (NOMCP(sc)){
7515                 sc->port.port_stx = 0;
7516                 sc->func_stx = 0;
7517         } else{
7518                 sc->port.port_stx = SHMEM_RD(sc, port_mb[port].port_stx);
7519                 sc->func_stx = SHMEM_RD(sc, func_mb[func].fw_mb_param);
7520         }
7521
7522         DBPRINT(sc, BXE_VERBOSE_STATS, "%s(): sc->port.port_stx = 0x%08X\n",
7523             __FUNCTION__, sc->port.port_stx);
7524         DBPRINT(sc, BXE_VERBOSE_STATS, "%s(): sc->func_stx = 0x%08X\n",
7525             __FUNCTION__, sc->func_stx);
7526
7527         /* Port statistics. */
7528         memset(&(sc->port.old_nig_stats), 0, sizeof(struct nig_stats));
7529         sc->port.old_nig_stats.brb_discard = REG_RD(sc,
7530             NIG_REG_STAT0_BRB_DISCARD + port * 0x38);
7531         sc->port.old_nig_stats.brb_truncate = REG_RD(sc,
7532             NIG_REG_STAT0_BRB_TRUNCATE + port * 0x38);
7533         REG_RD_DMAE(sc, NIG_REG_STAT0_EGRESS_MAC_PKT0 + port * 0x50,
7534             &(sc->port.old_nig_stats.egress_mac_pkt0_lo), 2);
7535         REG_RD_DMAE(sc, NIG_REG_STAT0_EGRESS_MAC_PKT1 + port * 0x50,
7536             &(sc->port.old_nig_stats.egress_mac_pkt1_lo), 2);
7537
7538         /* Function statistics. */
7539         for (i = 0; i < sc->num_queues; i++) {
7540                 fp = &sc->fp[i];
7541
7542                 /* Clear all per-queue statistics. */
7543                 memset(&fp->old_tclient, 0,
7544                     sizeof(struct tstorm_per_client_stats));
7545                 memset(&fp->old_uclient, 0,
7546                     sizeof(struct ustorm_per_client_stats));
7547                 memset(&fp->old_xclient, 0,
7548                     sizeof(struct xstorm_per_client_stats));
7549                 memset(&fp->eth_q_stats, 0,
7550                     sizeof(struct bxe_q_stats));
7551         }
7552
7553         /* ToDo: Clear any driver specific statistics? */
7554
7555         sc->stats_state = STATS_STATE_DISABLED;
7556
7557         if (sc->port.pmf == 1) {
7558                 /* Init port & function stats if we're PMF. */
7559                 if (sc->port.port_stx)
7560                         bxe_stats_port_base_init(sc);
7561                 if (sc->func_stx)
7562                         bxe_stats_func_base_init(sc);
7563         } else if (sc->func_stx)
7564                 /* Update function stats if we're not PMF. */
7565                 bxe_stats_func_base_update(sc);
7566
7567 bxe_stats_init_exit:
7568         DBEXIT(BXE_VERBOSE_STATS);
7569 }
7570
7571 /*
7572  *
7573  * Returns:
7574  *   None.
7575  */
7576 static void
7577 bxe_stats_hw_post(struct bxe_softc *sc)
7578 {
7579         struct dmae_command *dmae;
7580         uint32_t *stats_comp;
7581         int loader_idx;
7582
7583         DBENTER(BXE_INSANE_STATS);
7584
7585         dmae = &sc->stats_dmae;
7586         stats_comp = BXE_SP(sc, stats_comp);
7587         *stats_comp = DMAE_COMP_VAL;
7588
7589         if (sc->executer_idx) {
7590                 loader_idx = PMF_DMAE_C(sc);
7591
7592                 memset(dmae, 0, sizeof(struct dmae_command));
7593
7594                 dmae->opcode = (DMAE_CMD_SRC_PCI | DMAE_CMD_DST_GRC |
7595                     DMAE_CMD_C_DST_GRC | DMAE_CMD_C_ENABLE |
7596                     DMAE_CMD_DST_RESET |
7597 #ifdef __BIG_ENDIAN
7598                     DMAE_CMD_ENDIANITY_B_DW_SWAP |
7599 #else
7600                     DMAE_CMD_ENDIANITY_DW_SWAP |
7601 #endif
7602                     (BP_PORT(sc) ? DMAE_CMD_PORT_1 : DMAE_CMD_PORT_0) |
7603                     (BP_E1HVN(sc) << DMAE_CMD_E1HVN_SHIFT));
7604
7605                 dmae->src_addr_lo = U64_LO(BXE_SP_MAPPING(sc, dmae[0]));
7606                 dmae->src_addr_hi = U64_HI(BXE_SP_MAPPING(sc, dmae[0]));
7607                 dmae->dst_addr_lo = (DMAE_REG_CMD_MEM +
7608                     sizeof(struct dmae_command) * (loader_idx + 1)) >> 2;
7609                 dmae->dst_addr_hi = 0;
7610                 dmae->len = sizeof(struct dmae_command) >> 2;
7611
7612                 if (CHIP_IS_E1(sc))
7613                         dmae->len--;
7614
7615                 dmae->comp_addr_lo = dmae_reg_go_c[loader_idx + 1] >> 2;
7616                 dmae->comp_addr_hi = 0;
7617                 dmae->comp_val = 1;
7618
7619                 *stats_comp = 0;
7620                 bxe_post_dmae(sc, dmae, loader_idx);
7621
7622         } else if (sc->func_stx) {
7623                 *stats_comp = 0;
7624                 bxe_post_dmae(sc, dmae, INIT_DMAE_C(sc));
7625         }
7626
7627         DBEXIT(BXE_INSANE_STATS);
7628 }
7629
7630 /*
7631  * Delay routine which polls for the DMA engine to complete.
7632  *
7633  * Returns:
7634  *   0 = Failure, !0 = Success
7635  */
7636 static int
7637 bxe_stats_comp(struct bxe_softc *sc)
7638 {
7639         uint32_t *stats_comp;
7640         int cnt;
7641
7642         DBENTER(BXE_VERBOSE_STATS);
7643
7644         stats_comp = BXE_SP(sc, stats_comp);
7645         cnt = 10;
7646
7647         while (*stats_comp != DMAE_COMP_VAL) {
7648                 if (!cnt) {
7649                         BXE_PRINTF("%s(%d): Timeout waiting for statistics "
7650                             "completions.\n", __FILE__, __LINE__);
7651                         break;
7652                 }
7653                 cnt--;
7654                 DELAY(1000);
7655         }
7656
7657         DBEXIT(BXE_VERBOSE_STATS);
7658         /* ToDo: Shouldn't this return the value of cnt? */
7659         return (1);
7660 }
7661
7662 /*
7663  * DMA port statistcs from controller to driver.
7664  *
7665  * Returns:
7666  *   None.
7667  */
7668 static void
7669 bxe_stats_pmf_update(struct bxe_softc *sc)
7670 {
7671         struct dmae_command *dmae;
7672         uint32_t opcode, *stats_comp;
7673         int loader_idx;
7674
7675         DBENTER(BXE_VERBOSE_STATS);
7676
7677         stats_comp = BXE_SP(sc, stats_comp);
7678         loader_idx = PMF_DMAE_C(sc);
7679
7680         /* We shouldn't be here if any of the following are false. */
7681         if (!IS_E1HMF(sc) || (sc->port.pmf == 0) || !sc->port.port_stx) {
7682                 BXE_PRINTF("%s(%d): Statistics bug!\n", __FILE__, __LINE__);
7683                 goto bxe_stats_pmf_update_exit;
7684         }
7685
7686         sc->executer_idx = 0;
7687
7688         /* Instruct DMA engine to copy port statistics from H/W to driver. */
7689         opcode = (DMAE_CMD_SRC_GRC | DMAE_CMD_DST_PCI |
7690             DMAE_CMD_C_DST_PCI | DMAE_CMD_C_ENABLE |
7691             DMAE_CMD_SRC_RESET | DMAE_CMD_DST_RESET |
7692 #ifdef __BIG_ENDIAN
7693             DMAE_CMD_ENDIANITY_B_DW_SWAP |
7694 #else
7695             DMAE_CMD_ENDIANITY_DW_SWAP |
7696 #endif
7697             (BP_PORT(sc) ? DMAE_CMD_PORT_1 : DMAE_CMD_PORT_0) |
7698             (BP_E1HVN(sc) << DMAE_CMD_E1HVN_SHIFT));
7699
7700         dmae = BXE_SP(sc, dmae[sc->executer_idx++]);
7701         dmae->opcode = (opcode | DMAE_CMD_C_DST_GRC);
7702         dmae->src_addr_lo = sc->port.port_stx >> 2;
7703         dmae->src_addr_hi = 0;
7704         dmae->dst_addr_lo = U64_LO(BXE_SP_MAPPING(sc, port_stats));
7705         dmae->dst_addr_hi = U64_HI(BXE_SP_MAPPING(sc, port_stats));
7706         dmae->len = DMAE_LEN32_RD_MAX;
7707         dmae->comp_addr_lo = dmae_reg_go_c[loader_idx] >> 2;
7708         dmae->comp_addr_hi = 0;
7709         dmae->comp_val = 1;
7710
7711         dmae = BXE_SP(sc, dmae[sc->executer_idx++]);
7712         dmae->opcode = (opcode | DMAE_CMD_C_DST_PCI);
7713         dmae->src_addr_lo = (sc->port.port_stx >> 2) + DMAE_LEN32_RD_MAX;
7714         dmae->src_addr_hi = 0;
7715         dmae->dst_addr_lo = U64_LO(BXE_SP_MAPPING(sc, port_stats) +
7716             DMAE_LEN32_RD_MAX * 4);
7717         dmae->dst_addr_hi = U64_HI(BXE_SP_MAPPING(sc, port_stats) +
7718             DMAE_LEN32_RD_MAX * 4);
7719         dmae->len = (sizeof(struct host_port_stats) >> 2) -
7720             DMAE_LEN32_RD_MAX;
7721         dmae->comp_addr_lo = U64_LO(BXE_SP_MAPPING(sc, stats_comp));
7722         dmae->comp_addr_hi = U64_HI(BXE_SP_MAPPING(sc, stats_comp));
7723         dmae->comp_val = DMAE_COMP_VAL;
7724
7725         /* Start the DMA and wait for the result. */
7726         *stats_comp = 0;
7727         bxe_stats_hw_post(sc);
7728         bxe_stats_comp(sc);
7729
7730 bxe_stats_pmf_update_exit:
7731         DBEXIT(BXE_VERBOSE_STATS);
7732 }
7733
7734 /*
7735  * Prepare the DMAE parameters required for all statistics.
7736  *
7737  * This function should only be called by the driver instance
7738  * that is designated as the port management function (PMF).
7739  *
7740  * Returns:
7741  *   None.
7742  */
7743 static void
7744 bxe_stats_port_init(struct bxe_softc *sc)
7745 {
7746         struct dmae_command *dmae;
7747         uint32_t mac_addr, opcode, *stats_comp;
7748         int loader_idx, port, vn;
7749
7750         DBENTER(BXE_VERBOSE_STATS);
7751
7752         port = BP_PORT(sc);
7753         vn = BP_E1HVN(sc);
7754         loader_idx = PMF_DMAE_C(sc);
7755         stats_comp = BXE_SP(sc, stats_comp);
7756
7757         /* Only the port management function (PMF) does this work. */
7758         if (!sc->link_vars.link_up || (sc->port.pmf == 0)) {
7759                 BXE_PRINTF("%s(%d): Invalid statistics port setup!\n",
7760                     __FILE__, __LINE__);
7761                 goto bxe_stats_port_init_exit;
7762         }
7763
7764         sc->executer_idx = 0;
7765
7766         /* The same opcde is used for multiple DMA operations. */
7767         opcode = (DMAE_CMD_SRC_PCI | DMAE_CMD_DST_GRC |
7768             DMAE_CMD_C_DST_GRC | DMAE_CMD_C_ENABLE |
7769             DMAE_CMD_SRC_RESET | DMAE_CMD_DST_RESET |
7770 #ifdef __BIG_ENDIAN
7771             DMAE_CMD_ENDIANITY_B_DW_SWAP |
7772 #else
7773             DMAE_CMD_ENDIANITY_DW_SWAP |
7774 #endif
7775             (port ? DMAE_CMD_PORT_1 : DMAE_CMD_PORT_0) |
7776             (vn << DMAE_CMD_E1HVN_SHIFT));
7777
7778         /* Setup the DMA for port statistics. */
7779         if (sc->port.port_stx) {
7780                 dmae = BXE_SP(sc, dmae[sc->executer_idx++]);
7781                 dmae->opcode = opcode;
7782                 dmae->src_addr_lo = U64_LO(BXE_SP_MAPPING(sc, port_stats));
7783                 dmae->src_addr_hi = U64_HI(BXE_SP_MAPPING(sc, port_stats));
7784                 dmae->dst_addr_lo = sc->port.port_stx >> 2;
7785                 dmae->dst_addr_hi = 0;
7786                 dmae->len = sizeof(struct host_port_stats) >> 2;
7787                 dmae->comp_addr_lo = dmae_reg_go_c[loader_idx] >> 2;
7788                 dmae->comp_addr_hi = 0;
7789                 dmae->comp_val = 1;
7790         }
7791
7792         /* Setup the DMA for function statistics. */
7793         if (sc->func_stx) {
7794                 dmae = BXE_SP(sc, dmae[sc->executer_idx++]);
7795                 dmae->opcode = opcode;
7796                 dmae->src_addr_lo = U64_LO(BXE_SP_MAPPING(sc, func_stats));
7797                 dmae->src_addr_hi = U64_HI(BXE_SP_MAPPING(sc, func_stats));
7798                 dmae->dst_addr_lo = sc->func_stx >> 2;
7799                 dmae->dst_addr_hi = 0;
7800                 dmae->len = sizeof(struct host_func_stats) >> 2;
7801                 dmae->comp_addr_lo = dmae_reg_go_c[loader_idx] >> 2;
7802                 dmae->comp_addr_hi = 0;
7803                 dmae->comp_val = 1;
7804         }
7805
7806         /* Setup statistics reporting for the MAC. */
7807         opcode = (DMAE_CMD_SRC_GRC | DMAE_CMD_DST_PCI |
7808             DMAE_CMD_C_DST_GRC | DMAE_CMD_C_ENABLE |
7809             DMAE_CMD_SRC_RESET | DMAE_CMD_DST_RESET |
7810 #ifdef __BIG_ENDIAN
7811             DMAE_CMD_ENDIANITY_B_DW_SWAP |
7812 #else
7813             DMAE_CMD_ENDIANITY_DW_SWAP |
7814 #endif
7815             (port ? DMAE_CMD_PORT_1 : DMAE_CMD_PORT_0) |
7816             (vn << DMAE_CMD_E1HVN_SHIFT));
7817
7818         if (sc->link_vars.mac_type == MAC_TYPE_BMAC) {
7819                 /* Enable statistics for the 10Gb BMAC. */
7820
7821                 mac_addr = (port ? NIG_REG_INGRESS_BMAC1_MEM :
7822                         NIG_REG_INGRESS_BMAC0_MEM);
7823
7824                 /* Setup BMAC TX statistics (TX_STAT_GTPKT .. TX_STAT_GTBYT). */
7825                 dmae = BXE_SP(sc, dmae[sc->executer_idx++]);
7826                 dmae->opcode = opcode;
7827                 dmae->src_addr_lo = (mac_addr +
7828                     BIGMAC_REGISTER_TX_STAT_GTPKT) >> 2;
7829                 dmae->src_addr_hi = 0;
7830                 dmae->dst_addr_lo = U64_LO(BXE_SP_MAPPING(sc, mac_stats));
7831                 dmae->dst_addr_hi = U64_HI(BXE_SP_MAPPING(sc, mac_stats));
7832                 dmae->len = (8 + BIGMAC_REGISTER_TX_STAT_GTBYT -
7833                     BIGMAC_REGISTER_TX_STAT_GTPKT) >> 2;
7834                 dmae->comp_addr_lo = dmae_reg_go_c[loader_idx] >> 2;
7835                 dmae->comp_addr_hi = 0;
7836                 dmae->comp_val = 1;
7837
7838                 /* Setup BMAC RX statistcs (RX_STAT_GR64 .. RX_STAT_GRIPJ). */
7839                 dmae = BXE_SP(sc, dmae[sc->executer_idx++]);
7840                 dmae->opcode = opcode;
7841                 dmae->src_addr_lo = (mac_addr +
7842                     BIGMAC_REGISTER_RX_STAT_GR64) >> 2;
7843                 dmae->src_addr_hi = 0;
7844                 dmae->dst_addr_lo = U64_LO(BXE_SP_MAPPING(sc, mac_stats) +
7845                     offsetof(struct bmac_stats, rx_stat_gr64_lo));
7846                 dmae->dst_addr_hi = U64_HI(BXE_SP_MAPPING(sc, mac_stats) +
7847                     offsetof(struct bmac_stats, rx_stat_gr64_lo));
7848                 dmae->len = (8 + BIGMAC_REGISTER_RX_STAT_GRIPJ -
7849                     BIGMAC_REGISTER_RX_STAT_GR64) >> 2;
7850                 dmae->comp_addr_lo = dmae_reg_go_c[loader_idx] >> 2;
7851                 dmae->comp_addr_hi = 0;
7852                 dmae->comp_val = 1;
7853
7854         } else if (sc->link_vars.mac_type == MAC_TYPE_EMAC) {
7855                 /* Enable statistics for the 1Gb EMAC. */
7856
7857                 mac_addr = (port ? GRCBASE_EMAC1 : GRCBASE_EMAC0);
7858
7859                 /* Setup EMAC RX statistics. */
7860                 dmae = BXE_SP(sc, dmae[sc->executer_idx++]);
7861                 dmae->opcode = opcode;
7862                 dmae->src_addr_lo = (mac_addr + EMAC_REG_EMAC_RX_STAT_AC) >> 2;
7863                 dmae->src_addr_hi = 0;
7864                 dmae->dst_addr_lo = U64_LO(BXE_SP_MAPPING(sc, mac_stats));
7865                 dmae->dst_addr_hi = U64_HI(BXE_SP_MAPPING(sc, mac_stats));
7866                 dmae->len = EMAC_REG_EMAC_RX_STAT_AC_COUNT;
7867                 dmae->comp_addr_lo = dmae_reg_go_c[loader_idx] >> 2;
7868                 dmae->comp_addr_hi = 0;
7869                 dmae->comp_val = 1;
7870
7871                 /* Setup additional EMAC RX statistics. */
7872                 dmae = BXE_SP(sc, dmae[sc->executer_idx++]);
7873                 dmae->opcode = opcode;
7874                 dmae->src_addr_lo = (mac_addr +
7875                     EMAC_REG_EMAC_RX_STAT_AC_28) >> 2;
7876                 dmae->src_addr_hi = 0;
7877                 dmae->dst_addr_lo = U64_LO(BXE_SP_MAPPING(sc, mac_stats) +
7878                     offsetof(struct emac_stats, rx_stat_falsecarriererrors));
7879                 dmae->dst_addr_hi = U64_HI(BXE_SP_MAPPING(sc, mac_stats) +
7880                     offsetof(struct emac_stats, rx_stat_falsecarriererrors));
7881                 dmae->len = 1;
7882                 dmae->comp_addr_lo = dmae_reg_go_c[loader_idx] >> 2;
7883                 dmae->comp_addr_hi = 0;
7884                 dmae->comp_val = 1;
7885
7886                 /* Setup EMAC TX statistics. */
7887                 dmae = BXE_SP(sc, dmae[sc->executer_idx++]);
7888                 dmae->opcode = opcode;
7889                 dmae->src_addr_lo = (mac_addr + EMAC_REG_EMAC_TX_STAT_AC) >> 2;
7890                 dmae->src_addr_hi = 0;
7891                 dmae->dst_addr_lo = U64_LO(BXE_SP_MAPPING(sc, mac_stats) +
7892                     offsetof(struct emac_stats, tx_stat_ifhcoutoctets));
7893                 dmae->dst_addr_hi = U64_HI(BXE_SP_MAPPING(sc, mac_stats) +
7894                     offsetof(struct emac_stats, tx_stat_ifhcoutoctets));
7895                 dmae->len = EMAC_REG_EMAC_TX_STAT_AC_COUNT;
7896                 dmae->comp_addr_lo = dmae_reg_go_c[loader_idx] >> 2;
7897                 dmae->comp_addr_hi = 0;
7898                 dmae->comp_val = 1;
7899         } else {
7900                 DBPRINT(sc, BXE_WARN, "%s(): Undefined MAC type.\n",
7901                     __FUNCTION__);
7902         }
7903
7904         /* Enable NIG statistics. */
7905         dmae = BXE_SP(sc, dmae[sc->executer_idx++]);
7906         dmae->opcode = opcode;
7907         dmae->src_addr_lo = (port ? NIG_REG_STAT1_BRB_DISCARD :
7908             NIG_REG_STAT0_BRB_DISCARD) >> 2;
7909         dmae->src_addr_hi = 0;
7910         dmae->dst_addr_lo = U64_LO(BXE_SP_MAPPING(sc, nig_stats));
7911         dmae->dst_addr_hi = U64_HI(BXE_SP_MAPPING(sc, nig_stats));
7912         dmae->len = (sizeof(struct nig_stats) - 4 * sizeof(uint32_t)) >> 2;
7913         dmae->comp_addr_lo = dmae_reg_go_c[loader_idx] >> 2;
7914         dmae->comp_addr_hi = 0;
7915         dmae->comp_val = 1;
7916
7917         dmae = BXE_SP(sc, dmae[sc->executer_idx++]);
7918         dmae->opcode = opcode;
7919         dmae->src_addr_lo = (port ? NIG_REG_STAT1_EGRESS_MAC_PKT0 :
7920             NIG_REG_STAT0_EGRESS_MAC_PKT0) >> 2;
7921         dmae->src_addr_hi = 0;
7922         dmae->dst_addr_lo = U64_LO(BXE_SP_MAPPING(sc, nig_stats) +
7923             offsetof(struct nig_stats, egress_mac_pkt0_lo));
7924         dmae->dst_addr_hi = U64_HI(BXE_SP_MAPPING(sc, nig_stats) +
7925             offsetof(struct nig_stats, egress_mac_pkt0_lo));
7926         dmae->len = (2 * sizeof(uint32_t)) >> 2;
7927         dmae->comp_addr_lo = dmae_reg_go_c[loader_idx] >> 2;
7928         dmae->comp_addr_hi = 0;
7929         dmae->comp_val = 1;
7930
7931         dmae = BXE_SP(sc, dmae[sc->executer_idx++]);
7932         dmae->opcode = (DMAE_CMD_SRC_GRC | DMAE_CMD_DST_PCI |
7933             DMAE_CMD_C_DST_PCI | DMAE_CMD_C_ENABLE |
7934             DMAE_CMD_SRC_RESET | DMAE_CMD_DST_RESET |
7935 #ifdef __BIG_ENDIAN
7936             DMAE_CMD_ENDIANITY_B_DW_SWAP |
7937 #else
7938             DMAE_CMD_ENDIANITY_DW_SWAP |
7939 #endif
7940             (port ? DMAE_CMD_PORT_1 : DMAE_CMD_PORT_0) |
7941             (vn << DMAE_CMD_E1HVN_SHIFT));
7942         dmae->src_addr_lo = (port ? NIG_REG_STAT1_EGRESS_MAC_PKT1 :
7943             NIG_REG_STAT0_EGRESS_MAC_PKT1) >> 2;
7944         dmae->src_addr_hi = 0;
7945         dmae->dst_addr_lo = U64_LO(BXE_SP_MAPPING(sc, nig_stats) +
7946             offsetof(struct nig_stats, egress_mac_pkt1_lo));
7947         dmae->dst_addr_hi = U64_HI(BXE_SP_MAPPING(sc, nig_stats) +
7948             offsetof(struct nig_stats, egress_mac_pkt1_lo));
7949         dmae->len = (2 * sizeof(uint32_t)) >> 2;
7950         dmae->comp_addr_lo = U64_LO(BXE_SP_MAPPING(sc, stats_comp));
7951         dmae->comp_addr_hi = U64_HI(BXE_SP_MAPPING(sc, stats_comp));
7952         dmae->comp_val = DMAE_COMP_VAL;
7953
7954         /* Clear the statistics completion value. */
7955         *stats_comp = 0;
7956
7957 bxe_stats_port_init_exit:
7958         DBEXIT(BXE_VERBOSE_STATS);
7959 }
7960
7961 /*
7962  * Prepare the DMAE parameters required for function statistics.
7963  *
7964  * This function is called by all driver instances.
7965  *
7966  * Returns:
7967  *   None.
7968  */
7969 static void
7970 bxe_stats_func_init(struct bxe_softc *sc)
7971 {
7972         struct dmae_command *dmae;
7973         uint32_t *stats_comp;
7974
7975         DBENTER(BXE_VERBOSE_STATS);
7976
7977         if (!sc->func_stx) {
7978                 BXE_PRINTF("%s(%d): Invalid statistics function setup!\n",
7979                      __FILE__, __LINE__);
7980                 goto bxe_stats_func_init_exit;
7981         }
7982
7983         dmae = &sc->stats_dmae;
7984         stats_comp = BXE_SP(sc, stats_comp);
7985         sc->executer_idx = 0;
7986         memset(dmae, 0, sizeof(struct dmae_command));
7987
7988         /* Setup the DMA for function statistics. */
7989         dmae->opcode = (DMAE_CMD_SRC_PCI | DMAE_CMD_DST_GRC |
7990             DMAE_CMD_C_DST_PCI | DMAE_CMD_C_ENABLE |
7991             DMAE_CMD_SRC_RESET | DMAE_CMD_DST_RESET |
7992 #ifdef __BIG_ENDIAN
7993             DMAE_CMD_ENDIANITY_B_DW_SWAP |
7994 #else
7995             DMAE_CMD_ENDIANITY_DW_SWAP |
7996 #endif
7997             (BP_PORT(sc) ? DMAE_CMD_PORT_1 : DMAE_CMD_PORT_0) |
7998             (BP_E1HVN(sc) << DMAE_CMD_E1HVN_SHIFT));
7999
8000         dmae->src_addr_lo = U64_LO(BXE_SP_MAPPING(sc, func_stats));
8001         dmae->src_addr_hi = U64_HI(BXE_SP_MAPPING(sc, func_stats));
8002         dmae->dst_addr_lo = sc->func_stx >> 2;
8003         dmae->dst_addr_hi = 0;
8004         dmae->len = sizeof(struct host_func_stats) >> 2;
8005         dmae->comp_addr_lo = U64_LO(BXE_SP_MAPPING(sc, stats_comp));
8006         dmae->comp_addr_hi = U64_HI(BXE_SP_MAPPING(sc, stats_comp));
8007         dmae->comp_val = DMAE_COMP_VAL;
8008
8009         *stats_comp = 0;
8010
8011 bxe_stats_func_init_exit:
8012         DBEXIT(BXE_VERBOSE_STATS);
8013 }
8014
8015 /*
8016  * Starts a statistics update DMA and waits for completion.
8017  *
8018  * Returns:
8019  *   None.
8020  */
8021 static void
8022 bxe_stats_start(struct bxe_softc *sc)
8023 {
8024
8025         DBENTER(BXE_VERBOSE_STATS);
8026
8027         if (sc->port.pmf == 1)
8028                 bxe_stats_port_init(sc);
8029         else if (sc->func_stx)
8030                 bxe_stats_func_init(sc);
8031
8032         bxe_stats_hw_post(sc);
8033         bxe_stats_storm_post(sc);
8034
8035         DBEXIT(BXE_VERBOSE_STATS);
8036 }
8037
8038 /*
8039  * Returns:
8040  *   None.
8041  */
8042 static void
8043 bxe_stats_pmf_start(struct bxe_softc *sc)
8044 {
8045         DBENTER(BXE_VERBOSE_STATS);
8046
8047         bxe_stats_comp(sc);
8048         bxe_stats_pmf_update(sc);
8049         bxe_stats_start(sc);
8050
8051         DBEXIT(BXE_VERBOSE_STATS);
8052 }
8053
8054 /*
8055  * Returns:
8056  *   None.
8057  */
8058 static void
8059 bxe_stats_restart(struct bxe_softc *sc)
8060 {
8061
8062         DBENTER(BXE_VERBOSE_STATS);
8063
8064         bxe_stats_comp(sc);
8065         bxe_stats_start(sc);
8066
8067         DBEXIT(BXE_VERBOSE_STATS);
8068 }
8069
8070 /*
8071  * Update the Big MAC (10Gb BMAC) statistics.
8072  *
8073  * Returns:
8074  *   None.
8075  */
8076 static void
8077 bxe_stats_bmac_update(struct bxe_softc *sc)
8078 {
8079         struct bmac_stats *new;
8080         struct host_port_stats *pstats;
8081         struct bxe_port_stats *estats;
8082         struct regpair diff;
8083
8084         DBENTER(BXE_INSANE_STATS);
8085
8086         new = BXE_SP(sc, mac_stats.bmac_stats);
8087         pstats = BXE_SP(sc, port_stats);
8088         estats = &sc->eth_stats;
8089
8090         UPDATE_STAT64(rx_stat_grerb,
8091             rx_stat_ifhcinbadoctets);
8092         UPDATE_STAT64(rx_stat_grfcs,
8093             rx_stat_dot3statsfcserrors);
8094         UPDATE_STAT64(rx_stat_grund,
8095             rx_stat_etherstatsundersizepkts);
8096         UPDATE_STAT64(rx_stat_grovr,
8097             rx_stat_dot3statsframestoolong);
8098         UPDATE_STAT64(rx_stat_grfrg,
8099             rx_stat_etherstatsfragments);
8100         UPDATE_STAT64(rx_stat_grjbr,
8101             rx_stat_etherstatsjabbers);
8102         UPDATE_STAT64(rx_stat_grxcf,
8103             rx_stat_maccontrolframesreceived);
8104         UPDATE_STAT64(rx_stat_grxpf,
8105             rx_stat_xoffstateentered);
8106         UPDATE_STAT64(rx_stat_grxpf,
8107             rx_stat_bmac_xpf);
8108         UPDATE_STAT64(tx_stat_gtxpf,
8109             tx_stat_outxoffsent);
8110         UPDATE_STAT64(tx_stat_gtxpf,
8111             tx_stat_flowcontroldone);
8112         UPDATE_STAT64(tx_stat_gt64,
8113             tx_stat_etherstatspkts64octets);
8114         UPDATE_STAT64(tx_stat_gt127,
8115             tx_stat_etherstatspkts65octetsto127octets);
8116         UPDATE_STAT64(tx_stat_gt255,
8117             tx_stat_etherstatspkts128octetsto255octets);
8118         UPDATE_STAT64(tx_stat_gt511,
8119             tx_stat_etherstatspkts256octetsto511octets);
8120         UPDATE_STAT64(tx_stat_gt1023,
8121             tx_stat_etherstatspkts512octetsto1023octets);
8122         UPDATE_STAT64(tx_stat_gt1518,
8123             tx_stat_etherstatspkts1024octetsto1522octets);
8124         UPDATE_STAT64(tx_stat_gt2047,
8125             tx_stat_bmac_2047);
8126         UPDATE_STAT64(tx_stat_gt4095,
8127             tx_stat_bmac_4095);
8128         UPDATE_STAT64(tx_stat_gt9216,
8129             tx_stat_bmac_9216);
8130         UPDATE_STAT64(tx_stat_gt16383,
8131             tx_stat_bmac_16383);
8132         UPDATE_STAT64(tx_stat_gterr,
8133             tx_stat_dot3statsinternalmactransmiterrors);
8134         UPDATE_STAT64(tx_stat_gtufl,
8135             tx_stat_bmac_ufl);
8136
8137         estats->pause_frames_received_hi =
8138             pstats->mac_stx[1].rx_stat_bmac_xpf_hi;
8139         estats->pause_frames_received_lo =
8140             pstats->mac_stx[1].rx_stat_bmac_xpf_lo;
8141         estats->pause_frames_sent_hi =
8142             pstats->mac_stx[1].tx_stat_outxoffsent_hi;
8143         estats->pause_frames_sent_lo =
8144             pstats->mac_stx[1].tx_stat_outxoffsent_lo;
8145
8146         DBEXIT(BXE_INSANE_STATS);
8147 }
8148
8149 /*
8150  * Update the Ethernet MAC (1Gb EMAC) statistics.
8151  *
8152  * Returns:
8153  *   None.
8154  */
8155 static void
8156 bxe_stats_emac_update(struct bxe_softc *sc)
8157 {
8158         struct emac_stats *new;
8159         struct host_port_stats *pstats;
8160         struct bxe_port_stats *estats;
8161
8162         DBENTER(BXE_INSANE_STATS);
8163
8164         new = BXE_SP(sc, mac_stats.emac_stats);
8165         pstats = BXE_SP(sc, port_stats);
8166         estats = &sc->eth_stats;
8167
8168         UPDATE_EXTEND_STAT(rx_stat_ifhcinbadoctets);
8169         UPDATE_EXTEND_STAT(tx_stat_ifhcoutbadoctets);
8170         UPDATE_EXTEND_STAT(rx_stat_dot3statsfcserrors);
8171         UPDATE_EXTEND_STAT(rx_stat_dot3statsalignmenterrors);
8172         UPDATE_EXTEND_STAT(rx_stat_dot3statscarriersenseerrors);
8173         UPDATE_EXTEND_STAT(rx_stat_falsecarriererrors);
8174         UPDATE_EXTEND_STAT(rx_stat_etherstatsundersizepkts);
8175         UPDATE_EXTEND_STAT(rx_stat_dot3statsframestoolong);
8176         UPDATE_EXTEND_STAT(rx_stat_etherstatsfragments);
8177         UPDATE_EXTEND_STAT(rx_stat_etherstatsjabbers);
8178         UPDATE_EXTEND_STAT(rx_stat_maccontrolframesreceived);
8179         UPDATE_EXTEND_STAT(rx_stat_xoffstateentered);
8180         UPDATE_EXTEND_STAT(rx_stat_xonpauseframesreceived);
8181         UPDATE_EXTEND_STAT(rx_stat_xoffpauseframesreceived);
8182         UPDATE_EXTEND_STAT(tx_stat_outxonsent);
8183         UPDATE_EXTEND_STAT(tx_stat_outxoffsent);
8184         UPDATE_EXTEND_STAT(tx_stat_flowcontroldone);
8185         UPDATE_EXTEND_STAT(tx_stat_etherstatscollisions);
8186         UPDATE_EXTEND_STAT(tx_stat_dot3statssinglecollisionframes);
8187         UPDATE_EXTEND_STAT(tx_stat_dot3statsmultiplecollisionframes);
8188         UPDATE_EXTEND_STAT(tx_stat_dot3statsdeferredtransmissions);
8189         UPDATE_EXTEND_STAT(tx_stat_dot3statsexcessivecollisions);
8190         UPDATE_EXTEND_STAT(tx_stat_dot3statslatecollisions);
8191         UPDATE_EXTEND_STAT(tx_stat_etherstatspkts64octets);
8192         UPDATE_EXTEND_STAT(tx_stat_etherstatspkts65octetsto127octets);
8193         UPDATE_EXTEND_STAT(tx_stat_etherstatspkts128octetsto255octets);
8194         UPDATE_EXTEND_STAT(tx_stat_etherstatspkts256octetsto511octets);
8195         UPDATE_EXTEND_STAT(tx_stat_etherstatspkts512octetsto1023octets);
8196         UPDATE_EXTEND_STAT(tx_stat_etherstatspkts1024octetsto1522octets);
8197         UPDATE_EXTEND_STAT(tx_stat_etherstatspktsover1522octets);
8198         UPDATE_EXTEND_STAT(tx_stat_dot3statsinternalmactransmiterrors);
8199
8200         estats->pause_frames_received_hi =
8201             pstats->mac_stx[1].rx_stat_xonpauseframesreceived_hi;
8202         estats->pause_frames_received_lo =
8203             pstats->mac_stx[1].rx_stat_xonpauseframesreceived_lo;
8204         ADD_64(estats->pause_frames_received_hi,
8205             pstats->mac_stx[1].rx_stat_xoffpauseframesreceived_hi,
8206             estats->pause_frames_received_lo,
8207             pstats->mac_stx[1].rx_stat_xoffpauseframesreceived_lo);
8208
8209         estats->pause_frames_sent_hi =
8210             pstats->mac_stx[1].tx_stat_outxonsent_hi;
8211         estats->pause_frames_sent_lo =
8212             pstats->mac_stx[1].tx_stat_outxonsent_lo;
8213         ADD_64(estats->pause_frames_sent_hi,
8214             pstats->mac_stx[1].tx_stat_outxoffsent_hi,
8215             estats->pause_frames_sent_lo,
8216             pstats->mac_stx[1].tx_stat_outxoffsent_lo);
8217
8218         DBEXIT(BXE_INSANE_STATS);
8219 }
8220
8221 /*
8222  * Returns:
8223  *   0 = Success, !0 = Failure.
8224  */
8225 static int
8226 bxe_stats_hw_update(struct bxe_softc *sc)
8227 {
8228         struct nig_stats *new, *old;
8229         struct host_port_stats *pstats;
8230         struct bxe_port_stats *estats;
8231         struct regpair diff;
8232         uint32_t nig_timer_max;
8233         int rc;
8234
8235         DBENTER(BXE_INSANE_STATS);
8236
8237         rc = 0;
8238         new = BXE_SP(sc, nig_stats);
8239         old = &(sc->port.old_nig_stats);
8240         pstats = BXE_SP(sc, port_stats);
8241         estats = &sc->eth_stats;
8242
8243         /* Update statistics for the active MAC. */
8244         if (sc->link_vars.mac_type == MAC_TYPE_BMAC)
8245                 bxe_stats_bmac_update(sc);
8246         else if (sc->link_vars.mac_type == MAC_TYPE_EMAC)
8247                 bxe_stats_emac_update(sc);
8248         else {
8249                 DBPRINT(sc, BXE_WARN,
8250                     "%s(): Statistics updated by DMAE but no MAC is active!\n",
8251                     __FUNCTION__);
8252                 rc = EINVAL;
8253                 goto bxe_stats_hw_update_exit;
8254         }
8255
8256         /* Now update the hardware (NIG) statistics. */
8257         ADD_EXTEND_64(pstats->brb_drop_hi, pstats->brb_drop_lo,
8258             new->brb_discard - old->brb_discard);
8259         ADD_EXTEND_64(estats->brb_truncate_hi, estats->brb_truncate_lo,
8260             new->brb_truncate - old->brb_truncate);
8261
8262         UPDATE_STAT64_NIG(egress_mac_pkt0,
8263             etherstatspkts1024octetsto1522octets);
8264         UPDATE_STAT64_NIG(egress_mac_pkt1, etherstatspktsover1522octets);
8265
8266         memcpy(old, new, sizeof(struct nig_stats));
8267
8268         memcpy(&(estats->rx_stat_ifhcinbadoctets_hi), &(pstats->mac_stx[1]),
8269             sizeof(struct mac_stx));
8270         estats->brb_drop_hi = pstats->brb_drop_hi;
8271         estats->brb_drop_lo = pstats->brb_drop_lo;
8272
8273         pstats->host_port_stats_start = ++pstats->host_port_stats_end;
8274
8275         if (!NOMCP(sc)) {
8276                 nig_timer_max =
8277                     SHMEM_RD(sc, port_mb[BP_PORT(sc)].stat_nig_timer);
8278                 if (nig_timer_max != estats->nig_timer_max) {
8279                         estats->nig_timer_max = nig_timer_max;
8280                         DBPRINT(sc, BXE_WARN,
8281                             "%s(): NIG timer reached max value (%u)!\n",
8282                             __FUNCTION__, estats->nig_timer_max);
8283                 }
8284         }
8285
8286 bxe_stats_hw_update_exit:
8287         DBEXIT(BXE_INSANE_STATS);
8288         return (rc);
8289 }
8290
8291 /*
8292  * Returns:
8293  *   0 = Success, !0 = Failure.
8294  */
8295 // DRC - Done
8296 static int
8297 bxe_stats_storm_update(struct bxe_softc *sc)
8298 {
8299         int rc, i, cl_id;
8300         struct eth_stats_query *stats;
8301         struct bxe_port_stats *estats;
8302         struct host_func_stats *fstats;
8303         struct bxe_q_stats *qstats;
8304         struct tstorm_per_port_stats *tport;
8305         struct tstorm_per_client_stats *tclient;
8306         struct ustorm_per_client_stats *uclient;
8307         struct xstorm_per_client_stats *xclient;
8308         struct tstorm_per_client_stats *old_tclient;
8309         struct ustorm_per_client_stats *old_uclient;
8310         struct xstorm_per_client_stats *old_xclient;
8311         struct bxe_fastpath * fp;
8312         uint32_t diff;
8313
8314         DBENTER(BXE_INSANE_STATS);
8315
8316         rc = 0;
8317         diff = 0;
8318         stats = BXE_SP(sc, fw_stats);
8319         tport = &stats->tstorm_common.port_statistics;
8320         fstats = BXE_SP(sc, func_stats);
8321
8322         memcpy(&(fstats->total_bytes_received_hi),
8323             &(BXE_SP(sc, func_stats_base)->total_bytes_received_hi),
8324             sizeof(struct host_func_stats) - 2 * sizeof(uint32_t));
8325
8326         estats = &sc->eth_stats;
8327         estats->no_buff_discard_hi = 0;
8328         estats->no_buff_discard_lo = 0;
8329         estats->error_bytes_received_hi = 0;
8330         estats->error_bytes_received_lo = 0;
8331         estats->etherstatsoverrsizepkts_hi = 0;
8332         estats->etherstatsoverrsizepkts_lo = 0;
8333
8334         for (i = 0; i < sc->num_queues; i++) {
8335                 fp = &sc->fp[i];
8336                 cl_id = fp->cl_id;
8337                 tclient = &stats->tstorm_common.client_statistics[cl_id];
8338                 old_tclient = &fp->old_tclient;
8339                 uclient = &stats->ustorm_common.client_statistics[cl_id];
8340                 old_uclient = &fp->old_uclient;
8341                 xclient = &stats->xstorm_common.client_statistics[cl_id];
8342                 old_xclient = &fp->old_xclient;
8343                 qstats = &fp->eth_q_stats;
8344
8345                 /* Are TSTORM statistics valid? */
8346                 if ((uint16_t)(le16toh(tclient->stats_counter) + 1) !=
8347                     sc->stats_counter) {
8348                         DBPRINT(sc, BXE_WARN, "%s(): Stats not updated by TSTORM "
8349                             "(tstorm counter (%d) != stats_counter (%d))!\n",
8350                             __FUNCTION__, tclient->stats_counter, sc->stats_counter);
8351                         rc = 1;
8352                         goto bxe_stats_storm_update_exit;
8353                 }
8354
8355                 /* Are USTORM statistics valid? */
8356                 if ((uint16_t)(le16toh(uclient->stats_counter) + 1) !=
8357                     sc->stats_counter) {
8358                         DBPRINT(sc, BXE_WARN, "%s(): Stats not updated by USTORM "
8359                             "(ustorm counter (%d) != stats_counter (%d))!\n",
8360                             __FUNCTION__, uclient->stats_counter, sc->stats_counter);
8361                         rc = 2;
8362                         goto bxe_stats_storm_update_exit;
8363                 }
8364
8365                 /* Are XSTORM statistics valid? */
8366                 if ((uint16_t)(le16toh(xclient->stats_counter) + 1) !=
8367                         sc->stats_counter) {
8368                         DBPRINT(sc, BXE_WARN, "%s(): Stats not updated by XSTORM "
8369                             "(xstorm counter (%d) != stats_counter (%d))!\n",
8370                             __FUNCTION__, xclient->stats_counter, sc->stats_counter);
8371                         rc = 3;
8372                         goto bxe_stats_storm_update_exit;
8373                 }
8374
8375                 qstats->total_bytes_received_hi =
8376                     (tclient->rcv_broadcast_bytes.hi);
8377                 qstats->total_bytes_received_lo =
8378                     le32toh(tclient->rcv_broadcast_bytes.lo);
8379
8380                 ADD_64(qstats->total_bytes_received_hi,
8381                     le32toh(tclient->rcv_multicast_bytes.hi),
8382                     qstats->total_bytes_received_lo,
8383                     le32toh(tclient->rcv_multicast_bytes.lo));
8384
8385                 ADD_64(qstats->total_bytes_received_hi,
8386                     le32toh(tclient->rcv_unicast_bytes.hi),
8387                     qstats->total_bytes_received_lo,
8388                     le32toh(tclient->rcv_unicast_bytes.lo));
8389
8390                 SUB_64(qstats->total_bytes_received_hi,
8391                     le32toh(uclient->bcast_no_buff_bytes.hi),
8392                     qstats->total_bytes_received_lo,
8393                     le32toh(uclient->bcast_no_buff_bytes.lo));
8394
8395                 SUB_64(qstats->total_bytes_received_hi,
8396                     le32toh(uclient->mcast_no_buff_bytes.hi),
8397                     qstats->total_bytes_received_lo,
8398                     le32toh(uclient->mcast_no_buff_bytes.lo));
8399
8400                 SUB_64(qstats->total_bytes_received_hi,
8401                     le32toh(uclient->ucast_no_buff_bytes.hi),
8402                     qstats->total_bytes_received_lo,
8403                     le32toh(uclient->ucast_no_buff_bytes.lo));
8404
8405                 qstats->valid_bytes_received_hi =
8406                     qstats->total_bytes_received_hi;
8407                 qstats->valid_bytes_received_lo =
8408                     qstats->total_bytes_received_lo;
8409
8410                 qstats->error_bytes_received_hi =
8411                     le32toh(tclient->rcv_error_bytes.hi);
8412                 qstats->error_bytes_received_lo =
8413                     le32toh(tclient->rcv_error_bytes.lo);
8414
8415                 ADD_64(qstats->total_bytes_received_hi,
8416                     qstats->error_bytes_received_hi,
8417                     qstats->total_bytes_received_lo,
8418                     qstats->error_bytes_received_lo);
8419
8420                 UPDATE_EXTEND_TSTAT(rcv_unicast_pkts,
8421                     total_unicast_packets_received);
8422                 UPDATE_EXTEND_TSTAT(rcv_multicast_pkts,
8423                     total_multicast_packets_received);
8424                 UPDATE_EXTEND_TSTAT(rcv_broadcast_pkts,
8425                     total_broadcast_packets_received);
8426                 UPDATE_EXTEND_TSTAT(packets_too_big_discard,
8427                     etherstatsoverrsizepkts);
8428                 UPDATE_EXTEND_TSTAT(no_buff_discard, no_buff_discard);
8429
8430                 SUB_EXTEND_USTAT(ucast_no_buff_pkts,
8431                     total_unicast_packets_received);
8432                 SUB_EXTEND_USTAT(mcast_no_buff_pkts,
8433                     total_multicast_packets_received);
8434                 SUB_EXTEND_USTAT(bcast_no_buff_pkts,
8435                     total_broadcast_packets_received);
8436                 UPDATE_EXTEND_USTAT(ucast_no_buff_pkts, no_buff_discard);
8437                 UPDATE_EXTEND_USTAT(mcast_no_buff_pkts, no_buff_discard);
8438                 UPDATE_EXTEND_USTAT(bcast_no_buff_pkts, no_buff_discard);
8439
8440                 qstats->total_bytes_transmitted_hi =
8441                     le32toh(xclient->unicast_bytes_sent.hi);
8442                 qstats->total_bytes_transmitted_lo =
8443                     le32toh(xclient->unicast_bytes_sent.lo);
8444
8445                 ADD_64(qstats->total_bytes_transmitted_hi,
8446                     le32toh(xclient->multicast_bytes_sent.hi),
8447                     qstats->total_bytes_transmitted_lo,
8448                     le32toh(xclient->multicast_bytes_sent.lo));
8449
8450                 ADD_64(qstats->total_bytes_transmitted_hi,
8451                     le32toh(xclient->broadcast_bytes_sent.hi),
8452                     qstats->total_bytes_transmitted_lo,
8453                     le32toh(xclient->broadcast_bytes_sent.lo));
8454
8455                 UPDATE_EXTEND_XSTAT(unicast_pkts_sent,
8456                     total_unicast_packets_transmitted);
8457
8458                 UPDATE_EXTEND_XSTAT(multicast_pkts_sent,
8459                     total_multicast_packets_transmitted);
8460
8461                 UPDATE_EXTEND_XSTAT(broadcast_pkts_sent,
8462                     total_broadcast_packets_transmitted);
8463
8464                 old_tclient->checksum_discard = tclient->checksum_discard;
8465                 old_tclient->ttl0_discard = tclient->ttl0_discard;
8466
8467                 ADD_64(fstats->total_bytes_received_hi,
8468                        qstats->total_bytes_received_hi,
8469                        fstats->total_bytes_received_lo,
8470                        qstats->total_bytes_received_lo);
8471                 ADD_64(fstats->total_bytes_transmitted_hi,
8472                        qstats->total_bytes_transmitted_hi,
8473                        fstats->total_bytes_transmitted_lo,
8474                        qstats->total_bytes_transmitted_lo);
8475                 ADD_64(fstats->total_unicast_packets_received_hi,
8476                        qstats->total_unicast_packets_received_hi,
8477                        fstats->total_unicast_packets_received_lo,
8478                        qstats->total_unicast_packets_received_lo);
8479                 ADD_64(fstats->total_multicast_packets_received_hi,
8480                        qstats->total_multicast_packets_received_hi,
8481                        fstats->total_multicast_packets_received_lo,
8482                        qstats->total_multicast_packets_received_lo);
8483                 ADD_64(fstats->total_broadcast_packets_received_hi,
8484                        qstats->total_broadcast_packets_received_hi,
8485                        fstats->total_broadcast_packets_received_lo,
8486                        qstats->total_broadcast_packets_received_lo);
8487                 ADD_64(fstats->total_unicast_packets_transmitted_hi,
8488                        qstats->total_unicast_packets_transmitted_hi,
8489                        fstats->total_unicast_packets_transmitted_lo,
8490                        qstats->total_unicast_packets_transmitted_lo);
8491                 ADD_64(fstats->total_multicast_packets_transmitted_hi,
8492                        qstats->total_multicast_packets_transmitted_hi,
8493                        fstats->total_multicast_packets_transmitted_lo,
8494                        qstats->total_multicast_packets_transmitted_lo);
8495                 ADD_64(fstats->total_broadcast_packets_transmitted_hi,
8496                        qstats->total_broadcast_packets_transmitted_hi,
8497                        fstats->total_broadcast_packets_transmitted_lo,
8498                        qstats->total_broadcast_packets_transmitted_lo);
8499                 ADD_64(fstats->valid_bytes_received_hi,
8500                        qstats->valid_bytes_received_hi,
8501                        fstats->valid_bytes_received_lo,
8502                        qstats->valid_bytes_received_lo);
8503
8504                 ADD_64(estats->error_bytes_received_hi,
8505                        qstats->error_bytes_received_hi,
8506                        estats->error_bytes_received_lo,
8507                        qstats->error_bytes_received_lo);
8508                 ADD_64(estats->etherstatsoverrsizepkts_hi,
8509                        qstats->etherstatsoverrsizepkts_hi,
8510                        estats->etherstatsoverrsizepkts_lo,
8511                        qstats->etherstatsoverrsizepkts_lo);
8512                 ADD_64(estats->no_buff_discard_hi,
8513                        qstats->no_buff_discard_hi,
8514                        estats->no_buff_discard_lo,
8515                        qstats->no_buff_discard_lo);
8516         }
8517
8518         ADD_64(fstats->total_bytes_received_hi,
8519                estats->rx_stat_ifhcinbadoctets_hi,
8520                fstats->total_bytes_received_lo,
8521                estats->rx_stat_ifhcinbadoctets_lo);
8522
8523         memcpy(estats, &(fstats->total_bytes_received_hi),
8524             sizeof(struct host_func_stats) - 2 * sizeof(uint32_t));
8525
8526         ADD_64(estats->etherstatsoverrsizepkts_hi,
8527                estats->rx_stat_dot3statsframestoolong_hi,
8528                estats->etherstatsoverrsizepkts_lo,
8529                estats->rx_stat_dot3statsframestoolong_lo);
8530         ADD_64(estats->error_bytes_received_hi,
8531                estats->rx_stat_ifhcinbadoctets_hi,
8532                estats->error_bytes_received_lo,
8533                estats->rx_stat_ifhcinbadoctets_lo);
8534
8535         if (sc->port.pmf) {
8536                 estats->mac_filter_discard =
8537                     le32toh(tport->mac_filter_discard);
8538                 estats->xxoverflow_discard =
8539                     le32toh(tport->xxoverflow_discard);
8540                 estats->brb_truncate_discard =
8541                     le32toh(tport->brb_truncate_discard);
8542                 estats->mac_discard = le32toh(tport->mac_discard);
8543         }
8544
8545         fstats->host_func_stats_start = ++fstats->host_func_stats_end;
8546
8547         sc->stats_pending = 0;
8548
8549 bxe_stats_storm_update_exit:
8550
8551         DBEXIT(BXE_INSANE_STATS);
8552         return (rc);
8553 }
8554
8555 /*
8556  * Copy the controller maintained statistics over to the OS.
8557  *
8558  * Returns:
8559  *   None.
8560  */
8561 static void
8562 bxe_stats_net_update(struct bxe_softc *sc)
8563 {
8564         struct tstorm_per_client_stats *old_tclient;
8565         struct bxe_port_stats *estats;
8566         struct ifnet *ifp;
8567
8568         DBENTER(BXE_INSANE_STATS);
8569
8570         old_tclient = &sc->fp[0].old_tclient;
8571         estats = &sc->eth_stats;
8572         ifp = sc->bxe_ifp;
8573
8574         /*
8575          * Update the OS interface statistics from
8576          * the hardware statistics.
8577          */
8578
8579         ifp->if_collisions =
8580             (u_long) estats->tx_stat_dot3statssinglecollisionframes_lo +
8581             (u_long) estats->tx_stat_dot3statsmultiplecollisionframes_lo +
8582             (u_long) estats->tx_stat_dot3statslatecollisions_lo +
8583             (u_long) estats->tx_stat_dot3statsexcessivecollisions_lo;
8584
8585         ifp->if_ierrors =
8586             (u_long) old_tclient->checksum_discard +
8587             (u_long) estats->no_buff_discard_lo +
8588             (u_long) estats->mac_discard +
8589             (u_long) estats->rx_stat_etherstatsundersizepkts_lo +
8590             (u_long) estats->brb_drop_lo +
8591             (u_long) estats->brb_truncate_discard +
8592             (u_long) estats->rx_stat_dot3statsfcserrors_lo +
8593             (u_long) estats->rx_stat_dot3statsalignmenterrors_lo +
8594             (u_long) estats->xxoverflow_discard;
8595
8596         ifp->if_oerrors =
8597             (u_long) estats->tx_stat_dot3statslatecollisions_lo +
8598             (u_long) estats->tx_stat_dot3statsexcessivecollisions_lo +
8599             (u_long) estats->tx_stat_dot3statsinternalmactransmiterrors_lo;
8600
8601         ifp->if_ipackets =
8602             bxe_hilo(&estats->total_unicast_packets_received_hi) +
8603             bxe_hilo(&estats->total_multicast_packets_received_hi) +
8604             bxe_hilo(&estats->total_broadcast_packets_received_hi);
8605
8606         ifp->if_opackets =
8607             bxe_hilo(&estats->total_unicast_packets_transmitted_hi) +
8608             bxe_hilo(&estats->total_multicast_packets_transmitted_hi) +
8609             bxe_hilo(&estats->total_broadcast_packets_transmitted_hi);
8610
8611         DBEXIT(BXE_INSANE_STATS);
8612 }
8613
8614 /*
8615  *
8616  * Returns:
8617  *   None.
8618  */
8619 static void
8620 bxe_stats_update(struct bxe_softc *sc)
8621 {
8622         uint32_t *stats_comp;
8623         int update;
8624
8625         DBENTER(BXE_INSANE_STATS);
8626
8627         stats_comp = BXE_SP(sc, stats_comp);
8628         update = 0;
8629
8630         /* Make sure the statistics DMAE update has completed. */
8631         if (*stats_comp != DMAE_COMP_VAL)
8632                 goto bxe_stats_update_exit;
8633
8634         /* Check for any hardware statistics updates. */
8635         if (sc->port.pmf == 1)
8636                 update = (bxe_stats_hw_update(sc) == 0);
8637
8638         /* Check for any STORM statistics updates. */
8639         update |= (bxe_stats_storm_update(sc) == 0);
8640
8641         /* If we got updated hardware statistics then update the OS. */
8642         if (update)
8643                 bxe_stats_net_update(sc);
8644         else {
8645                 /* Check if any statistics updates are pending. */
8646                 if (sc->stats_pending) {
8647                         /* The update hasn't completed, keep waiting. */
8648                         sc->stats_pending++;
8649
8650                         /* Have we been waiting for too long? */
8651                         if (sc->stats_pending >= 3) {
8652                                 BXE_PRINTF(
8653                                     "%s(%d): Failed to get statistics after "
8654                                     "3 tries!\n", __FILE__, __LINE__);
8655                                 bxe_panic_dump(sc);
8656                                 goto bxe_stats_update_exit;
8657                         }
8658                 }
8659         }
8660
8661         /* Kickoff the next statistics request. */
8662         bxe_stats_hw_post(sc);
8663         bxe_stats_storm_post(sc);
8664
8665 bxe_stats_update_exit:
8666         DBEXIT(BXE_INSANE_STATS);
8667 }
8668
8669 /*
8670  *
8671  * Returns:
8672  *   None.
8673  */
8674 static void
8675 bxe_stats_port_stop(struct bxe_softc *sc)
8676 {
8677         struct dmae_command *dmae;
8678         uint32_t opcode, *stats_comp;
8679         int loader_idx;
8680
8681         DBENTER(BXE_VERBOSE_STATS);
8682
8683         stats_comp = BXE_SP(sc, stats_comp);
8684         loader_idx = PMF_DMAE_C(sc);
8685         sc->executer_idx = 0;
8686
8687         opcode = (DMAE_CMD_SRC_PCI | DMAE_CMD_DST_GRC |
8688             DMAE_CMD_C_ENABLE |
8689             DMAE_CMD_SRC_RESET | DMAE_CMD_DST_RESET |
8690 #ifdef __BIG_ENDIAN
8691             DMAE_CMD_ENDIANITY_B_DW_SWAP |
8692 #else
8693             DMAE_CMD_ENDIANITY_DW_SWAP |
8694 #endif
8695             (BP_PORT(sc) ? DMAE_CMD_PORT_1 : DMAE_CMD_PORT_0) |
8696             (BP_E1HVN(sc) << DMAE_CMD_E1HVN_SHIFT));
8697
8698         if (sc->port.port_stx) {
8699                 dmae = BXE_SP(sc, dmae[sc->executer_idx++]);
8700
8701                 if (sc->func_stx)
8702                         dmae->opcode = (opcode | DMAE_CMD_C_DST_GRC);
8703                 else
8704                         dmae->opcode = (opcode | DMAE_CMD_C_DST_PCI);
8705
8706                 dmae->src_addr_lo = U64_LO(BXE_SP_MAPPING(sc, port_stats));
8707                 dmae->src_addr_hi = U64_HI(BXE_SP_MAPPING(sc, port_stats));
8708                 dmae->dst_addr_lo = sc->port.port_stx >> 2;
8709                 dmae->dst_addr_hi = 0;
8710                 dmae->len = sizeof(struct host_port_stats) >> 2;
8711
8712                 if (sc->func_stx) {
8713                         dmae->comp_addr_lo = dmae_reg_go_c[loader_idx] >> 2;
8714                         dmae->comp_addr_hi = 0;
8715                         dmae->comp_val = 1;
8716                 } else {
8717                         dmae->comp_addr_lo = U64_LO(BXE_SP_MAPPING(sc,
8718                             stats_comp));
8719                         dmae->comp_addr_hi = U64_HI(BXE_SP_MAPPING(sc,
8720                             stats_comp));
8721                         dmae->comp_val = DMAE_COMP_VAL;
8722
8723                         *stats_comp = 0;
8724                 }
8725         }
8726
8727         if (sc->func_stx) {
8728                 dmae = BXE_SP(sc, dmae[sc->executer_idx++]);
8729                 dmae->opcode = (opcode | DMAE_CMD_C_DST_PCI);
8730                 dmae->src_addr_lo = U64_LO(BXE_SP_MAPPING(sc, func_stats));
8731                 dmae->src_addr_hi = U64_HI(BXE_SP_MAPPING(sc, func_stats));
8732                 dmae->dst_addr_lo = sc->func_stx >> 2;
8733                 dmae->dst_addr_hi = 0;
8734                 dmae->len = sizeof(struct host_func_stats) >> 2;
8735                 dmae->comp_addr_lo = U64_LO(BXE_SP_MAPPING(sc, stats_comp));
8736                 dmae->comp_addr_hi = U64_HI(BXE_SP_MAPPING(sc, stats_comp));
8737                 dmae->comp_val = DMAE_COMP_VAL;
8738
8739                 *stats_comp = 0;
8740         }
8741
8742         DBEXIT(BXE_VERBOSE_STATS);
8743 }
8744
8745 /*
8746  * Returns:
8747  *   None.
8748  */
8749 static void
8750 bxe_stats_stop(struct bxe_softc *sc)
8751 {
8752         int update;
8753
8754         DBENTER(BXE_VERBOSE_STATS);
8755
8756         update = 0;
8757
8758         /* Wait for any pending completions. */
8759         bxe_stats_comp(sc);
8760
8761         if (sc->port.pmf == 1)
8762                 update = (bxe_stats_hw_update(sc) == 0);
8763
8764         update |= (bxe_stats_storm_update(sc) == 0);
8765
8766         if (update) {
8767                 bxe_stats_net_update(sc);
8768
8769                 if (sc->port.pmf == 1)
8770                         bxe_stats_port_stop(sc);
8771
8772                 bxe_stats_hw_post(sc);
8773                 bxe_stats_comp(sc);
8774         }
8775
8776         DBEXIT(BXE_VERBOSE_STATS);
8777 }
8778
8779 /*
8780  * A dummy function to fill in the statistics state transition table.
8781  *
8782  * Returns:
8783  *   None.
8784  */
8785 static void
8786 bxe_stats_do_nothing(struct bxe_softc *sc)
8787 {
8788         DBENTER(BXE_VERBOSE_STATS);
8789         DBEXIT(BXE_VERBOSE_STATS);
8790 }
8791
8792 static const struct {
8793         void (*action)(struct bxe_softc *sc);
8794         enum bxe_stats_state next_state;
8795 } bxe_stats_stm[STATS_STATE_MAX][STATS_EVENT_MAX] = {
8796     /* State   Event  */
8797     {
8798     /* DISABLED PMF     */ {bxe_stats_pmf_update,       STATS_STATE_DISABLED},
8799     /*          LINK_UP */ {bxe_stats_start,            STATS_STATE_ENABLED},
8800     /*          UPDATE  */ {bxe_stats_do_nothing,       STATS_STATE_DISABLED},
8801     /*          STOP    */ {bxe_stats_do_nothing,       STATS_STATE_DISABLED}
8802     },
8803
8804     {
8805     /* ENABLED  PMF     */ {bxe_stats_pmf_start,        STATS_STATE_ENABLED},
8806     /*          LINK_UP */ {bxe_stats_restart,          STATS_STATE_ENABLED},
8807     /*          UPDATE  */ {bxe_stats_update,           STATS_STATE_ENABLED},
8808     /*          STOP    */ {bxe_stats_stop,             STATS_STATE_DISABLED}
8809     }
8810 };
8811
8812 /*
8813  * Move to the next state of the statistics state machine.
8814  *
8815  * Returns:
8816  *   None.
8817  */
8818 static void
8819 bxe_stats_handle(struct bxe_softc *sc, enum bxe_stats_event event)
8820 {
8821         enum bxe_stats_state state;
8822
8823         DBENTER(BXE_EXTREME_STATS);
8824
8825         state = sc->stats_state;
8826
8827 #ifdef BXE_DEBUG
8828         if (event != STATS_EVENT_UPDATE)
8829                 DBPRINT(sc, BXE_VERBOSE_STATS,
8830                     "%s(): Current state = %d, event = %d.\n", __FUNCTION__,
8831                     state, event);
8832 #endif
8833
8834         bxe_stats_stm[state][event].action(sc);
8835         sc->stats_state = bxe_stats_stm[state][event].next_state;
8836
8837 #ifdef BXE_DEBUG
8838         if (event != STATS_EVENT_UPDATE)
8839                 DBPRINT(sc, BXE_VERBOSE_STATS, "%s(): New state = %d.\n",
8840                     __FUNCTION__, sc->stats_state);
8841 #endif
8842
8843         DBEXIT(BXE_EXTREME_STATS);
8844 }
8845
8846 /*
8847  * bxe_chktso_window()
8848  * Checks to ensure the 13 bd sliding window is >= MSS for TSO.
8849  * Check that (13 total bds - 3bds) = 10 bd window >= MSS.
8850  * The window: 3 bds are = 1 (for headers BD) + 2 (for PBD and last BD)
8851  * The headers comes in a seperate bd in FreeBSD. So 13-3=10.
8852  *
8853  * Returns:
8854  *   0 if OK to send, 1 if packet needs further defragmentation.
8855  */
8856 static int
8857 bxe_chktso_window(struct bxe_softc* sc, int nsegs, bus_dma_segment_t *segs,
8858     struct mbuf *m0)
8859 {
8860         uint32_t num_wnds, wnd_size, wnd_sum;
8861         int32_t frag_idx, wnd_idx;
8862         unsigned short lso_mss;
8863         int defrag;
8864
8865         defrag = 0;
8866         wnd_sum = 0;
8867         wnd_size = 10;
8868         num_wnds = nsegs - wnd_size;
8869         lso_mss = htole16(m0->m_pkthdr.tso_segsz);
8870
8871         /*
8872          * Total Header lengths Eth+IP+TCP in 1st FreeBSD mbuf so
8873          * calculate the first window sum of data skip the first
8874          * assuming it is the header in FreeBSD.
8875          */
8876         for (frag_idx = 1; (frag_idx <= wnd_size); frag_idx++)
8877                 wnd_sum += htole16(segs[frag_idx].ds_len);
8878
8879         /* Chk the first 10 bd window size */
8880         if (wnd_sum < lso_mss)
8881                 return (defrag = 1);
8882
8883         /* Run through the windows */
8884         for (wnd_idx = 0; wnd_idx < num_wnds; wnd_idx++, frag_idx++) {
8885                 /* Subtract the 1st mbuf->m_len of the last wndw(-header). */
8886                 wnd_sum -= htole16(segs[wnd_idx+1].ds_len);
8887                 /* Add the next mbuf len to the len of our new window. */
8888                 wnd_sum +=  htole16(segs[frag_idx].ds_len);
8889                 if (wnd_sum < lso_mss) {
8890                         defrag = 1;
8891                         break;
8892                 }
8893         }
8894
8895         return (defrag);
8896 }
8897
8898
8899 /*
8900  * Encapsultes an mbuf cluster into the tx_bd chain structure and
8901  * makes the memory visible to the controller.
8902  *
8903  * If an mbuf is submitted to this routine and cannot be given to the
8904  * controller (e.g. it has too many fragments) then the function may free
8905  * the mbuf and return to the caller.
8906  *
8907  * Returns:
8908  *   0 = Success, !0 = Failure
8909  *   Note the side effect that an mbuf may be freed if it causes a problem.
8910  */
8911 static int
8912 bxe_tx_encap(struct bxe_fastpath *fp, struct mbuf **m_head)
8913 {
8914         bus_dma_segment_t segs[32];
8915         bus_dmamap_t map;
8916         struct mbuf *m0;
8917         struct eth_tx_parse_bd *tx_parse_bd;
8918         struct eth_tx_bd *tx_data_bd;
8919         struct eth_tx_bd *tx_total_pkt_size_bd;
8920         struct eth_tx_start_bd *tx_start_bd;
8921         uint16_t etype, sw_tx_bd_prod, sw_pkt_prod, total_pkt_size;
8922 //      uint16_t bd_index, pkt_index;
8923         uint8_t mac_type;
8924         int i, defragged, e_hlen, error, nsegs, rc, nbds, vlan_off, ovlan;
8925         struct bxe_softc *sc;
8926
8927         sc = fp->sc;
8928         DBENTER(BXE_VERBOSE_SEND);
8929
8930         DBRUN(M_ASSERTPKTHDR(*m_head));
8931
8932         m0 = *m_head;
8933         rc = defragged = nbds = ovlan = vlan_off = total_pkt_size = 0;
8934         tx_start_bd = NULL;
8935         tx_data_bd = NULL;
8936         tx_parse_bd = NULL;
8937         tx_total_pkt_size_bd = NULL;
8938
8939         /* Get the H/W pointer (0 to 65535) for packets and BD's. */
8940         sw_pkt_prod = fp->tx_pkt_prod;
8941         sw_tx_bd_prod = fp->tx_bd_prod;
8942
8943         /* Create the S/W index (0 to MAX_TX_BD) for packets and BD's. */
8944 //      pkt_index = TX_BD(sw_pkt_prod);
8945 //      bd_index = TX_BD(sw_tx_bd_prod);
8946
8947         mac_type = UNICAST_ADDRESS;
8948
8949         /* Map the mbuf into the next open DMAable memory. */
8950         map = fp->tx_mbuf_map[TX_BD(sw_pkt_prod)];
8951         error = bus_dmamap_load_mbuf_sg(fp->tx_mbuf_tag, map, m0,
8952             segs, &nsegs, BUS_DMA_NOWAIT);
8953
8954         /* Handle any mapping errors. */
8955         if(__predict_false(error != 0)){
8956                 fp->tx_dma_mapping_failure++;
8957                 if (error == ENOMEM) {
8958                         /* Resource issue, try again later. */
8959                         rc = ENOMEM;
8960                 } else if (error == EFBIG) {
8961                         /* Possibly recoverable with defragmentation. */
8962                         fp->mbuf_defrag_attempts++;
8963                         m0 = m_defrag(*m_head, M_NOWAIT);
8964                         if (m0 == NULL) {
8965                                 fp->mbuf_defrag_failures++;
8966                                 rc = ENOBUFS;
8967                         } else {
8968                                 /* Defrag successful, try mapping again.*/
8969                                 *m_head = m0;
8970                                 error = bus_dmamap_load_mbuf_sg(
8971                                     fp->tx_mbuf_tag, map, m0,
8972                                     segs, &nsegs, BUS_DMA_NOWAIT);
8973                                 if (error) {
8974                                         fp->tx_dma_mapping_failure++;
8975                                         rc = error;
8976                                 }
8977                         }
8978                 } else {
8979                         /* Unknown, unrecoverable mapping error. */
8980                         DBPRINT(sc, BXE_WARN_SEND,
8981                             "%s(): Unknown TX mapping error! "
8982                             "rc = %d.\n", __FUNCTION__, error);
8983                         DBRUN(bxe_dump_mbuf(sc, m0));
8984                         rc = error;
8985                 }
8986
8987                 goto bxe_tx_encap_continue;
8988         }
8989
8990         /* Make sure there's enough room in the send queue. */
8991         if (__predict_false((nsegs + 2) >
8992             (USABLE_TX_BD - fp->tx_bd_used))) {
8993                 /* Recoverable, try again later. */
8994                 fp->tx_hw_queue_full++;
8995                 bus_dmamap_unload(fp->tx_mbuf_tag, map);
8996                 rc = ENOMEM;
8997                 goto bxe_tx_encap_continue;
8998         }
8999
9000         /* Capture the current H/W TX chain high watermark. */
9001         if (__predict_false(fp->tx_hw_max_queue_depth <
9002             fp->tx_bd_used))
9003                 fp->tx_hw_max_queue_depth = fp->tx_bd_used;
9004
9005         /* Now make sure it fits in the packet window. */
9006         if (__predict_false(nsegs > 12)) {
9007                 /*
9008                  * The mbuf may be to big for the controller
9009                  * to handle.  If the frame is a TSO frame
9010                  * we'll need to do an additional check.
9011                  */
9012                 if(m0->m_pkthdr.csum_flags & CSUM_TSO){
9013                         if (bxe_chktso_window(sc,nsegs,segs,m0) == 0)
9014                                 /* OK to send. */
9015                                 goto bxe_tx_encap_continue;
9016                         else
9017                                 fp->tx_window_violation_tso++;
9018                 } else
9019                         fp->tx_window_violation_std++;
9020
9021                 /* No sense trying to defrag again, we'll drop the frame. */
9022                 if (defragged > 0)
9023                         rc = ENODEV;
9024         }
9025
9026 bxe_tx_encap_continue:
9027         /* Check for errors */
9028         if (rc){
9029                 if(rc == ENOMEM){
9030                         /* Recoverable try again later  */
9031                 }else{
9032                         fp->tx_soft_errors++;
9033                         fp->tx_mbuf_alloc--;
9034                         m_freem(*m_head);
9035                         *m_head = NULL;
9036                 }
9037                 goto bxe_tx_encap_exit;
9038         }
9039
9040         /* Save the mbuf and mapping. */
9041         fp->tx_mbuf_ptr[TX_BD(sw_pkt_prod)] = m0;
9042         fp->tx_mbuf_map[TX_BD(sw_pkt_prod)] = map;
9043
9044         /* Set flag according to packet type (UNICAST_ADDRESS is default). */
9045         if (m0->m_flags & M_BCAST)
9046                 mac_type = BROADCAST_ADDRESS;
9047         else if (m0->m_flags & M_MCAST)
9048                 mac_type = MULTICAST_ADDRESS;
9049
9050         /* Prepare the first transmit (Start) BD for the mbuf. */
9051         tx_start_bd = &fp->tx_chain[TX_BD(sw_tx_bd_prod)].start_bd;
9052
9053         tx_start_bd->addr_lo = htole32(U64_LO(segs[0].ds_addr));
9054         tx_start_bd->addr_hi = htole32(U64_HI(segs[0].ds_addr));
9055         tx_start_bd->nbytes  = htole16(segs[0].ds_len);
9056         total_pkt_size += tx_start_bd->nbytes;
9057         tx_start_bd->bd_flags.as_bitfield = ETH_TX_BD_FLAGS_START_BD;
9058         tx_start_bd->general_data =
9059                 (mac_type << ETH_TX_START_BD_ETH_ADDR_TYPE_SHIFT);
9060
9061         tx_start_bd->general_data |= (1 << ETH_TX_START_BD_HDR_NBDS_SHIFT);
9062
9063         /* All frames have at least Start BD + Parsing BD. */
9064         nbds = nsegs + 1;
9065         tx_start_bd->nbd = htole16(nbds);
9066
9067         if (m0->m_flags & M_VLANTAG) {
9068                 tx_start_bd->bd_flags.as_bitfield |= ETH_TX_BD_FLAGS_VLAN_TAG;
9069                 tx_start_bd->vlan = htole16(m0->m_pkthdr.ether_vtag);
9070         } else
9071                 /*
9072                  * In cases where the VLAN tag is not used the firmware
9073                  * expects to see a packet counter in the VLAN tag field
9074                  * Failure to do so will cause an assertion which will
9075                  * stop the controller.
9076                  */
9077                 tx_start_bd->vlan = htole16(fp->tx_pkt_prod);
9078
9079         /*
9080          * Add a parsing BD from the chain. The parsing BD is always added,
9081          * however, it is only used for TSO & chksum.
9082          */
9083         sw_tx_bd_prod = NEXT_TX_BD(sw_tx_bd_prod);
9084         tx_parse_bd = (struct eth_tx_parse_bd *)
9085             &fp->tx_chain[TX_BD(sw_tx_bd_prod)].parse_bd;
9086         memset(tx_parse_bd, 0, sizeof(struct eth_tx_parse_bd));
9087
9088         /* Gather all info about the packet and add to tx_parse_bd */
9089         if (m0->m_pkthdr.csum_flags) {
9090                 struct ether_vlan_header *eh;
9091                 struct ip *ip = NULL;
9092                 struct tcphdr *th = NULL;
9093                 uint16_t flags = 0;
9094                 struct udphdr *uh = NULL;
9095
9096                 /* Map Ethernet header to find type & header length. */
9097                 eh = mtod(m0, struct ether_vlan_header *);
9098
9099                 /* Handle VLAN encapsulation if present. */
9100                 if (eh->evl_encap_proto == htons(ETHERTYPE_VLAN)) {
9101                         etype = ntohs(eh->evl_proto);
9102                         e_hlen = ETHER_HDR_LEN + vlan_off;
9103                 } else {
9104                         etype = ntohs(eh->evl_encap_proto);
9105                         e_hlen = ETHER_HDR_LEN;
9106                 }
9107
9108                 /* Set the Ethernet header length in 16 bit words. */
9109                 tx_parse_bd->global_data = (e_hlen + ovlan) >> 1;
9110                 tx_parse_bd->global_data |= ((m0->m_flags & M_VLANTAG) <<
9111                     ETH_TX_PARSE_BD_LLC_SNAP_EN_SHIFT);
9112
9113                 switch (etype) {
9114                 case ETHERTYPE_IP:
9115                         /* If mbuf len < 20bytes, IP header is in next mbuf. */
9116                         if (m0->m_len < sizeof(struct ip))
9117                                 ip = (struct ip *) m0->m_next->m_data;
9118                         else
9119                                 ip = (struct ip *) (m0->m_data + e_hlen);
9120
9121                         /* Calculate IP header length (16 bit words). */
9122                         tx_parse_bd->ip_hlen = (ip->ip_hl << 1);
9123
9124                         /* Calculate enet + IP header length (16 bit words). */
9125                         tx_parse_bd->total_hlen = tx_parse_bd->ip_hlen +
9126                             (e_hlen >> 1);
9127
9128                         if (m0->m_pkthdr.csum_flags & CSUM_IP) {
9129                                 fp->tx_offload_frames_csum_ip++;
9130                                 flags |= ETH_TX_BD_FLAGS_IP_CSUM;
9131                         }
9132
9133                         /* Handle any checksums requested by the stack. */
9134                         if ((m0->m_pkthdr.csum_flags & CSUM_TCP)||
9135                             (m0->m_pkthdr.csum_flags & CSUM_TSO)){
9136
9137                                 /* Get the TCP header. */
9138                                 th = (struct tcphdr *)((caddr_t)ip +
9139                                     (ip->ip_hl << 2));
9140
9141                                 /* Add the TCP checksum offload flag. */
9142                                 flags |= ETH_TX_BD_FLAGS_L4_CSUM;
9143                                 fp->tx_offload_frames_csum_tcp++;
9144
9145                                 /* Update the enet + IP + TCP header length. */
9146                                 tx_parse_bd->total_hlen +=
9147                                     (uint16_t)(th->th_off << 1);
9148
9149                                 /* Get the pseudo header checksum. */
9150                                 tx_parse_bd->tcp_pseudo_csum =
9151                                     ntohs(th->th_sum);
9152
9153                         } else if (m0->m_pkthdr.csum_flags & CSUM_UDP) {
9154                                 /*
9155                                  * The hardware doesn't actually support UDP
9156                                  * checksum offload but we can fake it by
9157                                  * doing TCP checksum offload and factoring
9158                                  * out the extra bytes that are different
9159                                  * between the TCP header and the UDP header.
9160                                  *
9161                                  * Calculation will begin 10 bytes before the
9162                                  * actual start of the UDP header.  To work
9163                                  * around this we need to calculate the
9164                                  * checksum of the 10 bytes before the UDP
9165                                  * header and factor that out of the UDP
9166                                  * pseudo header checksum before asking the
9167                                  * H/W to calculate the full UDP checksum.
9168                                  */
9169                                 uint16_t tmp_csum;
9170                                 uint32_t *tmp_uh;
9171
9172                                 /* This value is 10. */
9173                 uint8_t fix = (uint8_t) (offsetof(struct tcphdr, th_sum) -
9174                                     (int) offsetof(struct udphdr, uh_sum));
9175
9176                                 /*
9177                                  * Add the TCP checksum offload flag for
9178                                  * UDP frames too.*
9179                                  */
9180                                 flags |= ETH_TX_BD_FLAGS_L4_CSUM;
9181                                 fp->tx_offload_frames_csum_udp++;
9182                                 tx_parse_bd->global_data |=
9183                                     ETH_TX_PARSE_BD_UDP_CS_FLG;
9184
9185                                 /* Get a pointer to the UDP header. */
9186                                 uh = (struct udphdr *)((caddr_t)ip +
9187                                     (ip->ip_hl << 2));
9188
9189                                 /* Set pointer 10 bytes before UDP header. */
9190                                         tmp_uh = (uint32_t *)((uint8_t *)uh -
9191                                             fix);
9192
9193                                 /*
9194                                  * Calculate a pseudo header checksum over
9195                                  * the 10 bytes before the UDP header.
9196                                  */
9197                                 tmp_csum = in_pseudo(ntohl(*tmp_uh),
9198                                     ntohl(*(tmp_uh + 1)),
9199                                     ntohl((*(tmp_uh + 2)) & 0x0000FFFF));
9200
9201                                 /* Update the enet + IP + UDP header length. */
9202                                 tx_parse_bd->total_hlen +=
9203                                     (sizeof(struct udphdr) >> 1);
9204                                 tx_parse_bd->tcp_pseudo_csum =
9205                                     ~in_addword(uh->uh_sum, ~tmp_csum);
9206                         }
9207
9208                         /* Update the offload flags. */
9209                         tx_start_bd->bd_flags.as_bitfield |= flags;
9210                         break;
9211
9212                 case ETHERTYPE_IPV6:
9213                         fp->tx_unsupported_tso_request_ipv6++;
9214                         /* ToDo: Add IPv6 support. */
9215                         break;
9216
9217                 default:
9218                         fp->tx_unsupported_tso_request_not_tcp++;
9219                         /* ToDo - How to handle this error? */
9220                 }
9221
9222                 /* Setup the Parsing BD with TSO specific info */
9223                 if (m0->m_pkthdr.csum_flags & CSUM_TSO) {
9224                         uint16_t hdr_len = tx_parse_bd->total_hlen << 1;
9225
9226                         tx_start_bd->bd_flags.as_bitfield |=
9227                             ETH_TX_BD_FLAGS_SW_LSO;
9228                         fp->tx_offload_frames_tso++;
9229
9230                         /* ToDo: Does this really help? */
9231                         if (__predict_false(tx_start_bd->nbytes > hdr_len)) {
9232                                 fp->tx_header_splits++;
9233                                 /*
9234                                  * Split the first BD into 2 BDs to make the
9235                                  * firmwares job easy...
9236                                  */
9237                                 tx_start_bd->nbd++;
9238                                 DBPRINT(sc, BXE_EXTREME_SEND,
9239                         "%s(): TSO split headr size is %d (%x:%x) nbds %d\n",
9240                                     __FUNCTION__, tx_start_bd->nbytes,
9241                                     tx_start_bd->addr_hi,
9242                                     tx_start_bd->addr_lo, nbds);
9243
9244                                 sw_tx_bd_prod = NEXT_TX_BD(sw_tx_bd_prod);
9245
9246                                 /* New transmit BD (after the tx_parse_bd). */
9247                                 tx_data_bd =
9248                                     &fp->tx_chain[TX_BD(sw_tx_bd_prod)].reg_bd;
9249                                 tx_data_bd->addr_hi =
9250                                     htole32(U64_HI(segs[0].ds_addr + hdr_len));
9251                                 tx_data_bd->addr_lo =
9252                                     htole32(U64_LO(segs[0].ds_addr + hdr_len));
9253                                 tx_data_bd->nbytes =
9254                                     htole16(segs[0].ds_len) - hdr_len;
9255                                 if (tx_total_pkt_size_bd == NULL)
9256                                         tx_total_pkt_size_bd = tx_data_bd;
9257                         }
9258
9259                         /*
9260                          * The controller needs the following info for TSO:
9261                          * MSS, tcp_send_seq, ip_id, and tcp_pseudo_csum.
9262                          */
9263                         tx_parse_bd->lso_mss = htole16(m0->m_pkthdr.tso_segsz);
9264                         tx_parse_bd->tcp_send_seq = ntohl(th->th_seq);
9265                         tx_parse_bd->tcp_flags = th->th_flags;
9266                         tx_parse_bd->ip_id = ntohs(ip->ip_id);
9267
9268                         tx_parse_bd->tcp_pseudo_csum =
9269                             ntohs(in_pseudo(ip->ip_src.s_addr,
9270                             ip->ip_dst.s_addr, htons(IPPROTO_TCP)));
9271
9272                         tx_parse_bd->global_data |=
9273                             ETH_TX_PARSE_BD_PSEUDO_CS_WITHOUT_LEN;
9274                 }
9275         }
9276
9277         /* Prepare remaining BDs. Start_tx_bd contains first seg (frag). */
9278         for (i = 1; i < nsegs ; i++) {
9279                 sw_tx_bd_prod = NEXT_TX_BD(sw_tx_bd_prod);
9280                 tx_data_bd = &fp->tx_chain[TX_BD(sw_tx_bd_prod)].reg_bd;
9281                 tx_data_bd->addr_lo = htole32(U64_LO(segs[i].ds_addr));
9282                 tx_data_bd->addr_hi = htole32(U64_HI(segs[i].ds_addr));
9283                 tx_data_bd->nbytes = htole16(segs[i].ds_len);
9284                 if (tx_total_pkt_size_bd == NULL)
9285                         tx_total_pkt_size_bd = tx_data_bd;
9286                 total_pkt_size += tx_data_bd->nbytes;
9287         }
9288
9289         if(tx_total_pkt_size_bd != NULL)
9290                 tx_total_pkt_size_bd->total_pkt_bytes = total_pkt_size;
9291
9292         /* Update TX BD producer index value for next TX */
9293         sw_tx_bd_prod = NEXT_TX_BD(sw_tx_bd_prod);
9294
9295         /* Update the used TX BD counter. */
9296         fp->tx_bd_used += nbds;
9297
9298         /*
9299          * If the chain of tx_bd's describing this frame
9300          * is adjacent to or spans an eth_tx_next_bd element
9301          * then we need to increment the nbds value.
9302          */
9303         if(TX_IDX(sw_tx_bd_prod) < nbds)
9304                 nbds++;
9305
9306         /* Don't allow reordering of writes for nbd and packets. */
9307         mb();
9308         fp->tx_db.data.prod += nbds;
9309
9310         /* Producer points to the next free tx_bd at this point. */
9311         fp->tx_pkt_prod++;
9312         fp->tx_bd_prod = sw_tx_bd_prod;
9313
9314         DOORBELL(sc, fp->index, fp->tx_db.raw);
9315
9316         fp->tx_pkts++;
9317
9318         /* Prevent speculative reads from getting ahead of the status block. */
9319         bus_space_barrier(sc->bxe_btag, sc->bxe_bhandle,
9320             0, 0, BUS_SPACE_BARRIER_READ);
9321
9322         /* Prevent speculative reads from getting ahead of the doorbell. */
9323         bus_space_barrier(sc->bxe_db_btag, sc->bxe_db_bhandle,
9324             0, 0, BUS_SPACE_BARRIER_READ);
9325
9326 bxe_tx_encap_exit:
9327         DBEXIT(BXE_VERBOSE_SEND);
9328         return (rc);
9329 }
9330
9331
9332 /*
9333  * Legacy (non-RSS) dispatch routine.
9334  *
9335  * Returns:
9336  *   Nothing.
9337  */
9338 static void
9339 bxe_tx_start(struct ifnet *ifp)
9340 {
9341         struct bxe_softc *sc;
9342         struct bxe_fastpath *fp;
9343
9344         sc = ifp->if_softc;
9345         DBENTER(BXE_EXTREME_SEND);
9346
9347         /* Exit if the transmit queue is full or link down. */
9348         if (((ifp->if_drv_flags & (IFF_DRV_RUNNING | IFF_DRV_OACTIVE)) !=
9349             IFF_DRV_RUNNING) || !sc->link_vars.link_up) {
9350                 DBPRINT(sc, BXE_WARN,
9351                     "%s(): No link or TX queue full, ignoring "
9352                     "transmit request.\n", __FUNCTION__);
9353                 goto bxe_tx_start_exit;
9354         }
9355
9356         /* Set the TX queue for the frame. */
9357         fp = &sc->fp[0];
9358
9359         BXE_FP_LOCK(fp);
9360         bxe_tx_start_locked(ifp, fp);
9361         BXE_FP_UNLOCK(fp);
9362
9363 bxe_tx_start_exit:
9364         DBEXIT(BXE_EXTREME_SEND);
9365 }
9366
9367
9368 /*
9369  * Legacy (non-RSS) transmit routine.
9370  *
9371  * Returns:
9372  *   Nothing.
9373  */
9374 static void
9375 bxe_tx_start_locked(struct ifnet *ifp, struct bxe_fastpath *fp)
9376 {
9377         struct bxe_softc *sc;
9378         struct mbuf *m = NULL;
9379         int tx_count = 0;
9380
9381         sc = fp->sc;
9382         DBENTER(BXE_EXTREME_SEND);
9383
9384         BXE_FP_LOCK_ASSERT(fp);
9385
9386         /* Keep adding entries while there are frames to send. */
9387         while (!IFQ_DRV_IS_EMPTY(&ifp->if_snd)) {
9388
9389                 /* Check for any frames to send. */
9390                 IFQ_DRV_DEQUEUE(&ifp->if_snd, m);
9391                 if (__predict_false(m == NULL))
9392                         break;
9393
9394                 /* The transmit mbuf now belongs to us, keep track of it. */
9395                 fp->tx_mbuf_alloc++;
9396
9397                 /*
9398                  * Pack the data into the transmit ring. If we
9399                  * don't have room, place the mbuf back at the
9400                  * head of the TX queue, set the OACTIVE flag,
9401                  * and wait for the NIC to drain the chain.
9402                  */
9403                 if (__predict_false(bxe_tx_encap(fp, &m))) {
9404                         fp->tx_encap_failures++;
9405                         /* Very Bad Frames(tm) may have been dropped. */
9406                         if (m != NULL) {
9407                                 /*
9408                                  * Mark the TX queue as full and return
9409                                  * the frame.
9410                                  */
9411                                 ifp->if_drv_flags |= IFF_DRV_OACTIVE;
9412                                 IFQ_DRV_PREPEND(&ifp->if_snd, m);
9413                                 fp->tx_mbuf_alloc--;
9414                                 fp->tx_queue_xoff++;
9415                         } else {
9416
9417                         }
9418
9419                         /* Stop looking for more work. */
9420                         break;
9421                 }
9422
9423                 /* The transmit frame was enqueued successfully. */
9424                 tx_count++;
9425
9426                 /* Send a copy of the frame to any BPF listeners. */
9427                 BPF_MTAP(ifp, m);
9428         }
9429
9430         /* No TX packets were dequeued. */
9431         if (tx_count > 0)
9432                 /* Reset the TX watchdog timeout timer. */
9433                 fp->watchdog_timer = BXE_TX_TIMEOUT;
9434
9435         DBEXIT(BXE_EXTREME_SEND);
9436 }
9437
9438 #if __FreeBSD_version >= 800000
9439 /*
9440  * Multiqueue (RSS) dispatch routine.
9441  *
9442  * Returns:
9443  *   0 if transmit succeeds, !0 otherwise.
9444  */
9445 static int
9446 bxe_tx_mq_start(struct ifnet *ifp, struct mbuf *m)
9447 {
9448         struct bxe_softc *sc;
9449         struct bxe_fastpath *fp;
9450         int fp_index, rc;
9451
9452         sc = ifp->if_softc;
9453         DBENTER(BXE_EXTREME_SEND);
9454
9455         fp_index = 0;
9456
9457         /* If using flow ID, assign the TX queue based on the flow ID. */
9458         if ((m->m_flags & M_FLOWID) != 0)
9459                 fp_index = m->m_pkthdr.flowid % sc->num_queues;
9460
9461         /* Select the fastpath TX queue for the frame. */
9462         fp = &sc->fp[fp_index];
9463
9464         /* Skip H/W enqueue if transmit queue is full or link down. */
9465         if (((ifp->if_drv_flags & (IFF_DRV_RUNNING | IFF_DRV_OACTIVE)) !=
9466             IFF_DRV_RUNNING) || !sc->link_vars.link_up) {
9467                 /* Stash the mbuf if we can. */
9468                 rc = drbr_enqueue(ifp, fp->br, m);
9469                 goto bxe_tx_mq_start_exit;
9470         }
9471
9472         BXE_FP_LOCK(fp);
9473         rc = bxe_tx_mq_start_locked(ifp, fp, m);
9474         BXE_FP_UNLOCK(fp);
9475
9476 bxe_tx_mq_start_exit:
9477         DBEXIT(BXE_EXTREME_SEND);
9478         return (rc);
9479 }
9480
9481
9482 /*
9483  * Multiqueue (TSS) transmit routine.  This routine is responsible
9484  * for adding a frame to the hardware's transmit queue.
9485  *
9486  * Returns:
9487  *   0 if transmit succeeds, !0 otherwise.
9488  */
9489 static int
9490 bxe_tx_mq_start_locked(struct ifnet *ifp,
9491     struct bxe_fastpath *fp, struct mbuf *m)
9492 {
9493         struct bxe_softc *sc;
9494         struct mbuf *next;
9495         int depth, rc, tx_count;
9496
9497         sc = fp->sc;
9498         DBENTER(BXE_EXTREME_SEND);
9499
9500         rc = tx_count = 0;
9501
9502         /* Fetch the depth of the driver queue. */
9503         depth = drbr_inuse(ifp, fp->br);
9504         if (depth > fp->tx_max_drbr_queue_depth)
9505                 fp->tx_max_drbr_queue_depth = depth;
9506
9507         BXE_FP_LOCK_ASSERT(fp);
9508
9509         if (m == NULL) {
9510                 /* No new work, check for pending frames. */
9511                 next = drbr_dequeue(ifp, fp->br);
9512         } else if (drbr_needs_enqueue(ifp, fp->br)) {
9513                 /* Both new and pending work, maintain packet order. */
9514                 rc = drbr_enqueue(ifp, fp->br, m);
9515                 if (rc != 0) {
9516                         fp->tx_soft_errors++;
9517                         goto bxe_tx_mq_start_locked_exit;
9518                 }
9519                 next = drbr_dequeue(ifp, fp->br);
9520         } else
9521                 /* New work only, nothing pending. */
9522                 next = m;
9523
9524         /* Keep adding entries while there are frames to send. */
9525         while (next != NULL) {
9526
9527                 /* The transmit mbuf now belongs to us, keep track of it. */
9528                 fp->tx_mbuf_alloc++;
9529
9530                 /*
9531                  * Pack the data into the transmit ring. If we
9532                  * don't have room, place the mbuf back at the
9533                  * head of the TX queue, set the OACTIVE flag,
9534                  * and wait for the NIC to drain the chain.
9535                  */
9536                 rc = bxe_tx_encap(fp, &next);
9537                 if (__predict_false(rc != 0)) {
9538                         fp->tx_encap_failures++;
9539                         /* Very Bad Frames(tm) may have been dropped. */
9540                         if (next != NULL) {
9541                                 /*
9542                                  * Mark the TX queue as full and save
9543                                  * the frame.
9544                                  */
9545                                 ifp->if_drv_flags |= IFF_DRV_OACTIVE;
9546                                 fp->tx_frame_deferred++;
9547
9548                                 /* This may reorder frame. */
9549                                 rc = drbr_enqueue(ifp, fp->br, next);
9550                                 fp->tx_mbuf_alloc--;
9551                         }
9552
9553                         /* Stop looking for more work. */
9554                         break;
9555                 }
9556
9557                 /* The transmit frame was enqueued successfully. */
9558                 tx_count++;
9559
9560                 /* Update stats */
9561                 ifp->if_obytes += next->m_pkthdr.len;
9562                 if (next->m_flags & M_MCAST)
9563                         ifp->if_omcasts++;
9564
9565                 /* Send a copy of the frame to any BPF listeners. */
9566                 BPF_MTAP(ifp, next);
9567
9568                 /* Handle any completions if we're running low. */
9569                 if (fp->tx_bd_used >= BXE_TX_CLEANUP_THRESHOLD)
9570                         bxe_txeof(fp);
9571
9572                 /* Close TX since there's so little room left. */
9573                 if (fp->tx_bd_used >= BXE_TX_CLEANUP_THRESHOLD) {
9574                         ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
9575                         break;
9576                 }
9577
9578                 next = drbr_dequeue(ifp, fp->br);
9579         }
9580
9581         /* No TX packets were dequeued. */
9582         if (tx_count > 0)
9583                 /* Reset the TX watchdog timeout timer. */
9584                 fp->watchdog_timer = BXE_TX_TIMEOUT;
9585
9586 bxe_tx_mq_start_locked_exit:
9587         DBEXIT(BXE_EXTREME_SEND);
9588         return (rc);
9589 }
9590
9591
9592 static void
9593 bxe_mq_flush(struct ifnet *ifp)
9594 {
9595         struct bxe_softc *sc;
9596         struct bxe_fastpath *fp;
9597         struct mbuf *m;
9598         int i;
9599
9600         sc = ifp->if_softc;
9601
9602         DBENTER(BXE_VERBOSE_UNLOAD);
9603
9604         for (i = 0; i < sc->num_queues; i++) {
9605                 fp = &sc->fp[i];
9606
9607                 if (fp->br != NULL) {
9608                         DBPRINT(sc, BXE_VERBOSE_UNLOAD,
9609                             "%s(): Clearing fp[%02d]...\n",
9610                             __FUNCTION__, fp->index);
9611
9612                         BXE_FP_LOCK(fp);
9613                         while ((m = buf_ring_dequeue_sc(fp->br)) != NULL)
9614                                 m_freem(m);
9615                         BXE_FP_UNLOCK(fp);
9616                 }
9617         }
9618
9619         if_qflush(ifp);
9620
9621         DBEXIT(BXE_VERBOSE_UNLOAD);
9622 }
9623 #endif /* FreeBSD_version >= 800000 */
9624
9625
9626 /*
9627  * Handles any IOCTL calls from the operating system.
9628  *
9629  * Returns:
9630  *   0 for success, positive value for failure.
9631  */
9632 static int
9633 bxe_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
9634 {
9635         struct bxe_softc *sc;
9636         struct ifreq *ifr;
9637         int error, mask, reinit;
9638
9639         sc = ifp->if_softc;
9640         DBENTER(BXE_VERBOSE_LOAD | BXE_VERBOSE_RESET | BXE_VERBOSE_MISC);
9641
9642         ifr = (struct ifreq *)data;
9643         error = 0;
9644         reinit = 0;
9645
9646         switch (command) {
9647         case SIOCSIFMTU:
9648                 /* Set the MTU. */
9649                 DBPRINT(sc, BXE_VERBOSE_MISC, "%s(): Received SIOCSIFMTU\n",
9650                     __FUNCTION__);
9651
9652                 /* Check that the MTU setting is supported. */
9653                 if ((ifr->ifr_mtu < BXE_MIN_MTU) ||
9654                     (ifr->ifr_mtu > BXE_JUMBO_MTU)) {
9655                         error = EINVAL;
9656                         break;
9657                 }
9658
9659                 BXE_CORE_LOCK(sc);
9660                 ifp->if_mtu = ifr->ifr_mtu;
9661                 BXE_CORE_UNLOCK(sc);
9662
9663                 reinit = 1;
9664                 break;
9665         case SIOCSIFFLAGS:
9666                 /* Toggle the interface state up or down. */
9667                 DBPRINT(sc, BXE_VERBOSE_MISC, "%s(): Received SIOCSIFFLAGS\n",
9668                     __FUNCTION__);
9669
9670                 BXE_CORE_LOCK(sc);
9671                 /* Check if the interface is up. */
9672                 if (ifp->if_flags & IFF_UP) {
9673                         if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
9674                                 /* Set promiscuous/multicast flags. */
9675                                 bxe_set_rx_mode(sc);
9676                         } else {
9677                                 /* Start the HW */
9678                                 bxe_init_locked(sc, LOAD_NORMAL);
9679                         }
9680                 } else {
9681                         /* Bring down the interface. */
9682                         if (ifp->if_drv_flags & IFF_DRV_RUNNING)
9683                                 bxe_stop_locked(sc, UNLOAD_NORMAL);
9684                 }
9685                 BXE_CORE_UNLOCK(sc);
9686
9687                 break;
9688         case SIOCADDMULTI:
9689         case SIOCDELMULTI:
9690                 /* Add/Delete multicast addresses. */
9691                 DBPRINT(sc, BXE_VERBOSE_MISC,
9692                     "%s(): Received SIOCADDMULTI/SIOCDELMULTI\n", __FUNCTION__);
9693
9694                 BXE_CORE_LOCK(sc);
9695                 /* Check if the interface is up. */
9696                 if (ifp->if_drv_flags & IFF_DRV_RUNNING)
9697                         /* Set receive mode flags. */
9698                         bxe_set_rx_mode(sc);
9699                 BXE_CORE_UNLOCK(sc);
9700
9701                 break;
9702         case SIOCSIFMEDIA:
9703         case SIOCGIFMEDIA:
9704                 /* Set/Get Interface media */
9705                 DBPRINT(sc, BXE_VERBOSE_MISC,
9706                     "%s(): Received SIOCSIFMEDIA/SIOCGIFMEDIA\n", __FUNCTION__);
9707
9708                 error = ifmedia_ioctl(ifp, ifr, &sc->bxe_ifmedia, command);
9709                 break;
9710         case SIOCSIFCAP:
9711                 /* Set interface capability */
9712
9713                 /* Find out which capabilities have changed. */
9714                 mask = ifr->ifr_reqcap ^ ifp->if_capenable;
9715                 DBPRINT(sc, BXE_VERBOSE_MISC,
9716                     "%s(): Received SIOCSIFCAP (mask = 0x%08X)\n", __FUNCTION__,
9717                     (uint32_t)mask);
9718
9719                 BXE_CORE_LOCK(sc);
9720
9721                 /* Toggle the LRO capabilites enable flag. */
9722                 if (mask & IFCAP_LRO) {
9723                         ifp->if_capenable ^= IFCAP_LRO;
9724                         sc->bxe_flags ^= BXE_TPA_ENABLE_FLAG;
9725                         DBPRINT(sc, BXE_INFO_MISC,
9726                             "%s(): Toggling LRO (bxe_flags = "
9727                             "0x%08X).\n", __FUNCTION__, sc->bxe_flags);
9728
9729                         /* LRO requires different buffer setup. */
9730                         reinit = 1;
9731                 }
9732
9733                 /* Toggle the TX checksum capabilites enable flag. */
9734                 if (mask & IFCAP_TXCSUM) {
9735                         DBPRINT(sc, BXE_VERBOSE_MISC,
9736                             "%s(): Toggling IFCAP_TXCSUM.\n", __FUNCTION__);
9737
9738                         ifp->if_capenable ^= IFCAP_TXCSUM;
9739
9740                         if (IFCAP_TXCSUM & ifp->if_capenable)
9741                                 ifp->if_hwassist = BXE_IF_HWASSIST;
9742                         else
9743                                 ifp->if_hwassist = 0;
9744                 }
9745
9746                 /* Toggle the RX checksum capabilities enable flag. */
9747                 if (mask & IFCAP_RXCSUM) {
9748                         DBPRINT(sc, BXE_VERBOSE_MISC,
9749                             "%s(): Toggling IFCAP_RXCSUM.\n", __FUNCTION__);
9750
9751                         ifp->if_capenable ^= IFCAP_RXCSUM;
9752
9753                         if (IFCAP_RXCSUM & ifp->if_capenable)
9754                                 ifp->if_hwassist = BXE_IF_HWASSIST;
9755                         else
9756                                 ifp->if_hwassist = 0;
9757                 }
9758
9759                 /* Toggle VLAN_MTU capabilities enable flag. */
9760                 if (mask & IFCAP_VLAN_MTU) {
9761                         /* ToDo: Is this really true? */
9762                         BXE_PRINTF("%s(%d): Changing VLAN_MTU not supported.\n",
9763                             __FILE__, __LINE__);
9764                         error = EINVAL;
9765                 }
9766
9767                 /* Toggle VLANHWTAG capabilities enabled flag. */
9768                 if (mask & IFCAP_VLAN_HWTAGGING) {
9769                         /* ToDo: Is this really true? */
9770                         BXE_PRINTF(
9771                             "%s(%d): Changing VLAN_HWTAGGING not supported!\n",
9772                             __FILE__, __LINE__);
9773                         error = EINVAL;
9774                 }
9775
9776                 /* Toggle TSO4 capabilities enabled flag. */
9777                 if (mask & IFCAP_TSO4) {
9778                         DBPRINT(sc, BXE_VERBOSE_MISC,
9779                             "%s(): Toggling IFCAP_TSO4.\n", __FUNCTION__);
9780
9781                         ifp->if_capenable ^= IFCAP_TSO4;
9782                 }
9783
9784                 /* Toggle TSO6 capabilities enabled flag. */
9785                 if (mask & IFCAP_TSO6) {
9786                         /* ToDo: Add TSO6 support. */
9787                         BXE_PRINTF(
9788                             "%s(%d): Changing TSO6 not supported!\n",
9789                             __FILE__, __LINE__);
9790                 }
9791                 BXE_CORE_UNLOCK(sc);
9792
9793                 /*
9794                  * ToDo: Look into supporting:
9795                  *   VLAN_HWFILTER
9796                  *   VLAN_HWCSUM
9797                  *   VLAN_HWTSO
9798                  *   POLLING
9799                  *   WOL[_UCAST|_MCAST|_MAGIC]
9800                  *
9801                  */
9802                 break;
9803         default:
9804                 /* We don't know how to handle the IOCTL, pass it on. */
9805                 error = ether_ioctl(ifp, command, data);
9806                 break;
9807         }
9808
9809         /* Restart the controller with the new capabilities. */
9810         if ((ifp->if_drv_flags & IFF_DRV_RUNNING) && (reinit != 0)) {
9811                 BXE_CORE_LOCK(sc);
9812                 bxe_stop_locked(sc, UNLOAD_NORMAL);
9813                 bxe_init_locked(sc, LOAD_NORMAL);
9814                 BXE_CORE_UNLOCK(sc);
9815         }
9816
9817         DBEXIT(BXE_VERBOSE_LOAD | BXE_VERBOSE_RESET | BXE_VERBOSE_MISC);
9818
9819         return (error);
9820 }
9821
9822 /*
9823  * Gets the current value of the RX Completion Consumer index
9824  * from the fastpath status block, updates it as necessary if
9825  * it is pointing to a "Next Page" entry, and returns it to the
9826  * caller.
9827  *
9828  * Returns:
9829  *   The adjusted value of *fp->rx_cons_sb.
9830  */
9831 static __inline uint16_t
9832 bxe_rx_cq_cons(struct bxe_fastpath *fp)
9833 {
9834         volatile uint16_t rx_cq_cons_sb = 0;
9835
9836         rmb();
9837         rx_cq_cons_sb = (volatile uint16_t) le16toh(*fp->rx_cq_cons_sb);
9838
9839         /*
9840          * It is valid for the hardware's copy of the completion
9841          * consumer index to be pointing at a "Next Page" entry in
9842          * the completion chain but the driver prefers to assume
9843          * that it is pointing at the next available CQE so we
9844          * need to adjust the value accordingly.
9845          */
9846         if ((rx_cq_cons_sb & USABLE_RCQ_ENTRIES_PER_PAGE) ==
9847             USABLE_RCQ_ENTRIES_PER_PAGE)
9848                 rx_cq_cons_sb++;
9849
9850         return (rx_cq_cons_sb);
9851 }
9852
9853 static __inline int
9854 bxe_has_tx_work(struct bxe_fastpath *fp)
9855 {
9856
9857         rmb();
9858         return (((fp->tx_pkt_prod != le16toh(*fp->tx_pkt_cons_sb)) || \
9859             (fp->tx_pkt_prod != fp->tx_pkt_cons)));
9860 }
9861
9862 /*
9863  * Checks if there are any received frames to process on the
9864  * completion queue.
9865  *
9866  * Returns:
9867  *   0 = No received frames pending, !0 = Received frames
9868  *       pending
9869  */
9870 static __inline int
9871 bxe_has_rx_work(struct bxe_fastpath *fp)
9872 {
9873
9874         rmb();
9875         return (bxe_rx_cq_cons(fp) != fp->rx_cq_cons);
9876 }
9877
9878 /*
9879  * Slowpath task entry point.
9880  *
9881  * Returns:
9882  *   None
9883  */
9884 static void
9885 bxe_task_sp(void *xsc, int pending)
9886 {
9887         struct bxe_softc *sc;
9888         uint32_t sp_status;
9889
9890         sc = xsc;
9891
9892         DBPRINT(sc, BXE_EXTREME_INTR, "%s(): pending = %d.\n", __FUNCTION__,
9893             pending);
9894
9895         /* Check for the source of the interrupt. */
9896         sp_status = bxe_update_dsb_idx(sc);
9897
9898         /* Handle any hardware attentions. */
9899         if (sp_status & 0x1) {
9900                 bxe_attn_int(sc);
9901                 sp_status &= ~0x1;
9902         }
9903
9904         /* CSTORM event asserted (query_stats, port delete ramrod, etc.). */
9905         if (sp_status & 0x2) {
9906                 sc->stats_pending = 0;
9907                 sp_status &= ~0x2;
9908         }
9909
9910         /* Check for other weirdness. */
9911         if (sp_status != 0) {
9912                 DBPRINT(sc, BXE_WARN, "%s(): Unexpected slowpath interrupt "
9913                     "(sp_status = 0x%04X)!\n", __FUNCTION__, sp_status);
9914         }
9915
9916         /* Acknowledge the xSTORM tags and enable slowpath interrupts. */
9917         bxe_ack_sb(sc, DEF_SB_ID, ATTENTION_ID, le16toh(sc->def_att_idx),
9918             IGU_INT_NOP, 1);
9919         bxe_ack_sb(sc, DEF_SB_ID, USTORM_ID, le16toh(sc->def_u_idx),
9920             IGU_INT_NOP, 1);
9921         bxe_ack_sb(sc, DEF_SB_ID, CSTORM_ID, le16toh(sc->def_c_idx),
9922             IGU_INT_NOP, 1);
9923         bxe_ack_sb(sc, DEF_SB_ID, XSTORM_ID, le16toh(sc->def_x_idx),
9924             IGU_INT_NOP, 1);
9925         bxe_ack_sb(sc, DEF_SB_ID, TSTORM_ID, le16toh(sc->def_t_idx),
9926             IGU_INT_ENABLE, 1);
9927 }
9928
9929
9930 /*
9931  * Legacy interrupt entry point.
9932  *
9933  * Verifies that the controller generated the interrupt and
9934  * then calls a separate routine to handle the various
9935  * interrupt causes: link, RX, and TX.
9936  *
9937  * Returns:
9938  *   None
9939  */
9940 static void
9941 bxe_intr_legacy(void *xsc)
9942 {
9943         struct bxe_softc *sc;
9944         struct bxe_fastpath *fp;
9945         uint32_t mask, fp_status;
9946
9947         sc = xsc;
9948         fp = &sc->fp[0];
9949
9950         /* Don't handle any interrupts if we're not ready. */
9951         if (__predict_false(sc->intr_sem != 0))
9952                 goto bxe_intr_legacy_exit;
9953
9954         /* Bail out if the interrupt wasn't generated by our hardware. */
9955         fp_status = bxe_ack_int(sc);
9956         if (fp_status == 0)
9957                 goto bxe_intr_legacy_exit;
9958
9959         /* Handle the fastpath interrupt. */
9960         /*
9961          * sb_id = 0 for ustorm, 1 for cstorm.
9962          * The bits returned from ack_int() are 0-15,
9963          * bit 0=attention status block
9964          * bit 1=fast path status block
9965          * A mask of 0x2 or more = tx/rx event
9966          * A mask of 1 = slow path event
9967          */
9968
9969         mask = (0x2 << fp->sb_id);
9970         DBPRINT(sc, BXE_INSANE_INTR, "%s(): fp_status = 0x%08X, mask = "
9971             "0x%08X\n", __FUNCTION__, fp_status, mask);
9972
9973         /* CSTORM event means fastpath completion. */
9974         if (fp_status & mask) {
9975                 /* This interrupt must be ours, disable further interrupts. */
9976                 bxe_ack_sb(sc, fp->sb_id, USTORM_ID, 0, IGU_INT_DISABLE, 0);
9977 #ifdef BXE_TASK
9978                 taskqueue_enqueue(fp->tq, &fp->task);
9979 #else
9980                 bxe_task_fp((void *)fp, 0);
9981 #endif
9982                 /* Clear this event from the status flags. */
9983                 fp_status &= ~mask;
9984         }
9985
9986         /* Handle all slow path interrupts and attentions */
9987         if (fp_status & 0x1) {
9988                 /* Acknowledge and disable further slowpath interrupts. */
9989                 bxe_ack_sb(sc, DEF_SB_ID, TSTORM_ID, 0, IGU_INT_DISABLE, 0);
9990 #ifdef BXE_TASK
9991                 /* Schedule the slowpath task. */
9992                 taskqueue_enqueue(sc->tq, &sc->task);
9993 #else
9994                 bxe_task_sp(xsc, 0);
9995 #endif
9996                 /* Clear this event from the status flags. */
9997                 fp_status &= ~0x1;
9998         }
9999
10000 #ifdef BXE_DEBUG
10001         if (fp_status) {
10002                 DBPRINT(sc, BXE_WARN,
10003                     "%s(): Unexpected fastpath status (fp_status = 0x%08X)!\n",
10004                     __FUNCTION__, fp_status);
10005         }
10006 #endif
10007
10008         DBEXIT(BXE_EXTREME_INTR);
10009
10010 bxe_intr_legacy_exit:
10011         return;
10012 }
10013
10014 /*
10015  * Slowpath interrupt entry point.
10016  *
10017  * Acknowledge the interrupt and schedule a slowpath task.
10018  *
10019  * Returns:
10020  *   None
10021  */
10022 static void
10023 bxe_intr_sp(void *xsc)
10024 {
10025         struct bxe_softc *sc;
10026
10027         sc = xsc;
10028
10029         DBPRINT(sc, BXE_INSANE_INTR, "%s(%d): Slowpath interrupt.\n",
10030             __FUNCTION__, curcpu);
10031
10032         /* Don't handle any interrupts if we're not ready. */
10033         if (__predict_false(sc->intr_sem != 0))
10034                 goto bxe_intr_sp_exit;
10035
10036         /* Acknowledge and disable further slowpath interrupts. */
10037         bxe_ack_sb(sc, DEF_SB_ID, TSTORM_ID, 0, IGU_INT_DISABLE, 0);
10038
10039 #ifdef BXE_TASK
10040         /* Schedule the slowpath task. */
10041         taskqueue_enqueue(sc->tq, &sc->task);
10042 #else
10043         bxe_task_sp(xsc, 0);
10044 #endif
10045
10046 bxe_intr_sp_exit:
10047         return;
10048 }
10049
10050 /*
10051  * Fastpath interrupt entry point.
10052  *
10053  * Acknowledge the interrupt and schedule a fastpath task.
10054  *
10055  * Returns:
10056  *   None
10057  */
10058 static void
10059 bxe_intr_fp (void *xfp)
10060 {
10061         struct bxe_fastpath *fp;
10062         struct bxe_softc *sc;
10063
10064         fp = xfp;
10065         sc = fp->sc;
10066
10067         DBPRINT(sc, BXE_INSANE_INTR,
10068             "%s(%d): fp[%02d].sb_id = %d interrupt.\n",
10069             __FUNCTION__, curcpu, fp->index, fp->sb_id);
10070
10071         /* Don't handle any interrupts if we're not ready. */
10072         if (__predict_false(sc->intr_sem != 0))
10073                 goto bxe_intr_fp_exit;
10074
10075         /* Disable further interrupts. */
10076         bxe_ack_sb(sc, fp->sb_id, USTORM_ID, 0, IGU_INT_DISABLE, 0);
10077 #ifdef BXE_TASK
10078         taskqueue_enqueue(fp->tq, &fp->task);
10079 #else
10080         bxe_task_fp (xfp, 0);
10081 #endif
10082
10083 bxe_intr_fp_exit:
10084         return;
10085 }
10086
10087 /*
10088  * Fastpath task entry point.
10089  *
10090  * Handle any pending transmit or receive events.
10091  *
10092  * Returns:
10093  *   None
10094  */
10095 static void
10096 bxe_task_fp (void *xfp, int pending)
10097 {
10098         struct bxe_fastpath *fp;
10099         struct bxe_softc *sc;
10100
10101         fp = xfp;
10102         sc = fp->sc;
10103
10104         DBPRINT(sc, BXE_EXTREME_INTR, "%s(%d): Fastpath task on fp[%02d]"
10105             ".sb_id = %d\n", __FUNCTION__, curcpu, fp->index, fp->sb_id);
10106
10107         /* Update the fast path indices */
10108         bxe_update_fpsb_idx(fp);
10109
10110         /* Service any completed TX frames. */
10111         if (bxe_has_tx_work(fp)) {
10112                 BXE_FP_LOCK(fp);
10113                 bxe_txeof(fp);
10114                 BXE_FP_UNLOCK(fp);
10115         }
10116
10117         /* Service any completed RX frames. */
10118         rmb();
10119         bxe_rxeof(fp);
10120
10121         /* Acknowledge the fastpath status block indices. */
10122         bxe_ack_sb(sc, fp->sb_id, USTORM_ID, fp->fp_u_idx, IGU_INT_NOP, 1);
10123         bxe_ack_sb(sc, fp->sb_id, CSTORM_ID, fp->fp_c_idx, IGU_INT_ENABLE, 1);
10124 }
10125
10126 /*
10127  * Clears the fastpath (per-queue) status block.
10128  *
10129  * Returns:
10130  *   None
10131  */
10132 static void
10133 bxe_zero_sb(struct bxe_softc *sc, int sb_id)
10134 {
10135         int port;
10136
10137         DBENTER(BXE_VERBOSE_LOAD | BXE_VERBOSE_RESET | BXE_VERBOSE_INTR);
10138         port = BP_PORT(sc);
10139
10140         /* "CSTORM" */
10141         bxe_init_fill(sc, CSEM_REG_FAST_MEMORY +
10142             CSTORM_SB_HOST_STATUS_BLOCK_U_OFFSET(port, sb_id), 0,
10143             CSTORM_SB_STATUS_BLOCK_U_SIZE / 4);
10144         bxe_init_fill(sc, CSEM_REG_FAST_MEMORY +
10145             CSTORM_SB_HOST_STATUS_BLOCK_C_OFFSET(port, sb_id), 0,
10146             CSTORM_SB_STATUS_BLOCK_C_SIZE / 4);
10147
10148         DBEXIT(BXE_VERBOSE_LOAD | BXE_VERBOSE_RESET | BXE_VERBOSE_INTR);
10149 }
10150
10151 /*
10152  * Initialize the fastpath (per queue) status block.
10153  *
10154  * Returns:
10155  *   None
10156  */
10157 static void
10158 bxe_init_sb(struct bxe_softc *sc, struct host_status_block *sb,
10159     bus_addr_t mapping, int sb_id)
10160 {
10161         uint64_t section;
10162         int func, index, port;
10163
10164         DBENTER(BXE_VERBOSE_LOAD | BXE_VERBOSE_RESET | BXE_VERBOSE_INTR);
10165
10166         port = BP_PORT(sc);
10167         func = BP_FUNC(sc);
10168
10169         DBPRINT(sc, (BXE_VERBOSE_LOAD | BXE_VERBOSE_RESET | BXE_VERBOSE_INTR),
10170             "%s(): Initializing sb_id = %d on port %d, function %d.\n",
10171             __FUNCTION__, sb_id, port, func);
10172
10173         /* Setup the USTORM status block. */
10174         section = ((uint64_t)mapping) + offsetof(struct host_status_block,
10175             u_status_block);
10176         sb->u_status_block.status_block_id = sb_id;
10177
10178         REG_WR(sc, BAR_CSTORM_INTMEM +
10179             CSTORM_SB_HOST_SB_ADDR_U_OFFSET(port, sb_id), U64_LO(section));
10180         REG_WR(sc, BAR_CSTORM_INTMEM +
10181             ((CSTORM_SB_HOST_SB_ADDR_U_OFFSET(port, sb_id)) + 4),
10182             U64_HI(section));
10183         REG_WR8(sc, BAR_CSTORM_INTMEM + FP_USB_FUNC_OFF +
10184             CSTORM_SB_HOST_STATUS_BLOCK_U_OFFSET(port, sb_id), func);
10185
10186         for (index = 0; index < HC_USTORM_SB_NUM_INDICES; index++)
10187                 REG_WR16(sc, BAR_CSTORM_INTMEM +
10188                     CSTORM_SB_HC_DISABLE_U_OFFSET(port, sb_id, index), 0x1);
10189
10190         /* Setup the CSTORM status block. */
10191         section = ((uint64_t)mapping) + offsetof(struct host_status_block,
10192             c_status_block);
10193         sb->c_status_block.status_block_id = sb_id;
10194
10195         /* Write the status block address to CSTORM. Order is important! */
10196         REG_WR(sc, BAR_CSTORM_INTMEM +
10197             CSTORM_SB_HOST_SB_ADDR_C_OFFSET(port, sb_id), U64_LO(section));
10198         REG_WR(sc, BAR_CSTORM_INTMEM +
10199             ((CSTORM_SB_HOST_SB_ADDR_C_OFFSET(port, sb_id)) + 4),
10200             U64_HI(section));
10201         REG_WR8(sc, BAR_CSTORM_INTMEM + FP_CSB_FUNC_OFF +
10202             CSTORM_SB_HOST_STATUS_BLOCK_C_OFFSET(port, sb_id), func);
10203
10204         for (index = 0; index < HC_CSTORM_SB_NUM_INDICES; index++)
10205                 REG_WR16(sc, BAR_CSTORM_INTMEM +
10206                     CSTORM_SB_HC_DISABLE_C_OFFSET(port, sb_id, index), 0x1);
10207
10208         /* Enable interrupts. */
10209         bxe_ack_sb(sc, sb_id, CSTORM_ID, 0, IGU_INT_ENABLE, 0);
10210
10211         DBEXIT(BXE_VERBOSE_LOAD | BXE_VERBOSE_RESET | BXE_VERBOSE_INTR);
10212 }
10213
10214 /*
10215  * Clears the default status block.
10216  *
10217  * Returns:
10218  *   None
10219  */
10220 static void
10221 bxe_zero_def_sb(struct bxe_softc *sc)
10222 {
10223         int func;
10224
10225         func = BP_FUNC(sc);
10226
10227         DBENTER(BXE_VERBOSE_LOAD | BXE_VERBOSE_RESET | BXE_VERBOSE_INTR);
10228         DBPRINT(sc, (BXE_VERBOSE_LOAD | BXE_VERBOSE_RESET | BXE_VERBOSE_INTR),
10229             "%s(): Clearing default status block on function %d.\n",
10230             __FUNCTION__, func);
10231
10232         /* Fill the STORM's copy of the default status block with 0. */
10233         bxe_init_fill(sc, TSEM_REG_FAST_MEMORY +
10234             TSTORM_DEF_SB_HOST_STATUS_BLOCK_OFFSET(func), 0,
10235             sizeof(struct tstorm_def_status_block) / 4);
10236         bxe_init_fill(sc, CSEM_REG_FAST_MEMORY +
10237             CSTORM_DEF_SB_HOST_STATUS_BLOCK_U_OFFSET(func), 0,
10238             sizeof(struct cstorm_def_status_block_u) / 4);
10239         bxe_init_fill(sc, CSEM_REG_FAST_MEMORY +
10240             CSTORM_DEF_SB_HOST_STATUS_BLOCK_C_OFFSET(func), 0,
10241             sizeof(struct cstorm_def_status_block_c) / 4);
10242         bxe_init_fill(sc, XSEM_REG_FAST_MEMORY +
10243             XSTORM_DEF_SB_HOST_STATUS_BLOCK_OFFSET(func), 0,
10244             sizeof(struct xstorm_def_status_block) / 4);
10245
10246         DBEXIT(BXE_VERBOSE_LOAD | BXE_VERBOSE_RESET | BXE_VERBOSE_INTR);
10247 }
10248
10249 /*
10250  * Initialize default status block.
10251  *
10252  * Returns:
10253  *   None
10254  */
10255 static void
10256 bxe_init_def_sb(struct bxe_softc *sc, struct host_def_status_block *def_sb,
10257     bus_addr_t mapping, int sb_id)
10258 {
10259         uint64_t section;
10260         int func, index, port, reg_offset, val;
10261
10262         port = BP_PORT(sc);
10263         func = BP_FUNC(sc);
10264
10265         DBENTER(BXE_VERBOSE_LOAD | BXE_VERBOSE_RESET | BXE_VERBOSE_INTR);
10266         DBPRINT(sc, (BXE_VERBOSE_LOAD | BXE_VERBOSE_RESET | BXE_VERBOSE_INTR),
10267            "%s(): Initializing default status block on port %d, function %d.\n",
10268            __FUNCTION__, port, func);
10269
10270         /* Setup the default status block (DSB). */
10271         section = ((uint64_t)mapping) + offsetof(struct host_def_status_block,
10272             atten_status_block);
10273         def_sb->atten_status_block.status_block_id = sb_id;
10274         sc->attn_state = 0;
10275         sc->def_att_idx = 0;
10276
10277         /*
10278          * Read routing configuration for attn signal
10279          * output of groups. Currently, only groups
10280          * 0 through 3 are wired.
10281          */
10282         reg_offset = port ? MISC_REG_AEU_ENABLE1_FUNC_1_OUT_0 :
10283             MISC_REG_AEU_ENABLE1_FUNC_0_OUT_0;
10284
10285         for (index = 0; index < MAX_DYNAMIC_ATTN_GRPS; index++) {
10286                 sc->attn_group[index].sig[0] = REG_RD(sc, reg_offset +
10287                     0x10 * index);
10288                 sc->attn_group[index].sig[1] = REG_RD(sc, reg_offset +
10289                     0x10 * index + 0x4);
10290                 sc->attn_group[index].sig[2] = REG_RD(sc, reg_offset +
10291                     0x10 * index + 0x8);
10292                 sc->attn_group[index].sig[3] = REG_RD(sc, reg_offset +
10293                     0x10 * index + 0xc);
10294
10295                 DBPRINT(sc, (BXE_VERBOSE_LOAD | BXE_VERBOSE_RESET |
10296                     BXE_VERBOSE_INTR),
10297                     "%s(): attn_group[%d] = 0x%08X 0x%08X 0x%08x 0X%08x\n",
10298                     __FUNCTION__, index, sc->attn_group[index].sig[0],
10299                     sc->attn_group[index].sig[1], sc->attn_group[index].sig[2],
10300                     sc->attn_group[index].sig[3]);
10301         }
10302
10303         reg_offset = port ? HC_REG_ATTN_MSG1_ADDR_L : HC_REG_ATTN_MSG0_ADDR_L;
10304
10305         REG_WR(sc, reg_offset, U64_LO(section));
10306         REG_WR(sc, reg_offset + 4, U64_HI(section));
10307
10308         reg_offset = port ? HC_REG_ATTN_NUM_P1 : HC_REG_ATTN_NUM_P0;
10309
10310         val = REG_RD(sc, reg_offset);
10311         val |= sb_id;
10312         REG_WR(sc, reg_offset, val);
10313
10314         /* USTORM */
10315         section = ((uint64_t)mapping) + offsetof(struct host_def_status_block,
10316             u_def_status_block);
10317         def_sb->u_def_status_block.status_block_id = sb_id;
10318         sc->def_u_idx = 0;
10319
10320         REG_WR(sc, BAR_CSTORM_INTMEM +
10321             CSTORM_DEF_SB_HOST_SB_ADDR_U_OFFSET(func), U64_LO(section));
10322         REG_WR(sc, BAR_CSTORM_INTMEM +
10323             ((CSTORM_DEF_SB_HOST_SB_ADDR_U_OFFSET(func)) + 4), U64_HI(section));
10324         REG_WR8(sc, BAR_CSTORM_INTMEM + DEF_USB_FUNC_OFF +
10325             CSTORM_DEF_SB_HOST_STATUS_BLOCK_U_OFFSET(func), func);
10326
10327         for (index = 0; index < HC_USTORM_DEF_SB_NUM_INDICES; index++)
10328                 REG_WR16(sc, BAR_CSTORM_INTMEM +
10329                     CSTORM_DEF_SB_HC_DISABLE_U_OFFSET(func, index), 1);
10330
10331         /* CSTORM */
10332         section = ((uint64_t)mapping) + offsetof(struct host_def_status_block,
10333             c_def_status_block);
10334         def_sb->c_def_status_block.status_block_id = sb_id;
10335         sc->def_c_idx = 0;
10336
10337         REG_WR(sc, BAR_CSTORM_INTMEM +
10338             CSTORM_DEF_SB_HOST_SB_ADDR_C_OFFSET(func), U64_LO(section));
10339         REG_WR(sc, BAR_CSTORM_INTMEM +
10340             ((CSTORM_DEF_SB_HOST_SB_ADDR_C_OFFSET(func)) + 4), U64_HI(section));
10341         REG_WR8(sc, BAR_CSTORM_INTMEM + DEF_CSB_FUNC_OFF +
10342             CSTORM_DEF_SB_HOST_STATUS_BLOCK_C_OFFSET(func), func);
10343
10344         for (index = 0; index < HC_CSTORM_DEF_SB_NUM_INDICES; index++)
10345                 REG_WR16(sc, BAR_CSTORM_INTMEM +
10346                     CSTORM_DEF_SB_HC_DISABLE_C_OFFSET(func, index), 1);
10347
10348         /* TSTORM */
10349         section = ((uint64_t)mapping) + offsetof(struct host_def_status_block,
10350             t_def_status_block);
10351         def_sb->t_def_status_block.status_block_id = sb_id;
10352         sc->def_t_idx = 0;
10353
10354         REG_WR(sc, BAR_TSTORM_INTMEM +
10355             TSTORM_DEF_SB_HOST_SB_ADDR_OFFSET(func), U64_LO(section));
10356         REG_WR(sc, BAR_TSTORM_INTMEM +
10357             ((TSTORM_DEF_SB_HOST_SB_ADDR_OFFSET(func)) + 4), U64_HI(section));
10358         REG_WR8(sc, BAR_TSTORM_INTMEM + DEF_TSB_FUNC_OFF +
10359             TSTORM_DEF_SB_HOST_STATUS_BLOCK_OFFSET(func), func);
10360
10361         for (index = 0; index < HC_TSTORM_DEF_SB_NUM_INDICES; index++)
10362                 REG_WR16(sc, BAR_TSTORM_INTMEM +
10363                     TSTORM_DEF_SB_HC_DISABLE_OFFSET(func, index), 1);
10364
10365         /* XSTORM */
10366         section = ((uint64_t)mapping) + offsetof(struct host_def_status_block,
10367             x_def_status_block);
10368         def_sb->x_def_status_block.status_block_id = sb_id;
10369         sc->def_x_idx = 0;
10370
10371         REG_WR(sc, BAR_XSTORM_INTMEM +
10372             XSTORM_DEF_SB_HOST_SB_ADDR_OFFSET(func), U64_LO(section));
10373         REG_WR(sc, BAR_XSTORM_INTMEM +
10374             ((XSTORM_DEF_SB_HOST_SB_ADDR_OFFSET(func)) + 4), U64_HI(section));
10375         REG_WR8(sc, BAR_XSTORM_INTMEM + DEF_XSB_FUNC_OFF +
10376             XSTORM_DEF_SB_HOST_STATUS_BLOCK_OFFSET(func), func);
10377
10378         for (index = 0; index < HC_XSTORM_DEF_SB_NUM_INDICES; index++)
10379                 REG_WR16(sc, BAR_XSTORM_INTMEM +
10380                     XSTORM_DEF_SB_HC_DISABLE_OFFSET(func, index), 1);
10381
10382         sc->stats_pending = 0;
10383         sc->set_mac_pending = 0;
10384
10385         bxe_ack_sb(sc, sb_id, CSTORM_ID, 0, IGU_INT_ENABLE, 0);
10386
10387         DBEXIT(BXE_VERBOSE_LOAD | BXE_VERBOSE_RESET | BXE_VERBOSE_INTR);
10388 }
10389
10390 /*
10391  * Update interrupt coalescing parameters.
10392  *
10393  * Returns:
10394  *   None
10395  */
10396 static void
10397 bxe_update_coalesce(struct bxe_softc *sc)
10398 {
10399         int i, port, sb_id;
10400
10401         DBENTER(BXE_VERBOSE_LOAD | BXE_VERBOSE_RESET);
10402
10403         port = BP_PORT(sc);
10404         /* Cycle through each fastpath queue and set the coalescing values. */
10405         for (i = 0; i < sc->num_queues; i++) {
10406                 sb_id = sc->fp[i].sb_id;
10407
10408                 /* Receive interrupt coalescing is done on USTORM. */
10409                 REG_WR8(sc, BAR_CSTORM_INTMEM +
10410                     CSTORM_SB_HC_TIMEOUT_U_OFFSET(port, sb_id,
10411                     U_SB_ETH_RX_CQ_INDEX), sc->rx_ticks / (BXE_BTR * 4));
10412
10413                 REG_WR16(sc, BAR_CSTORM_INTMEM +
10414                     CSTORM_SB_HC_DISABLE_U_OFFSET(port, sb_id,
10415                     U_SB_ETH_RX_CQ_INDEX),
10416                     (sc->rx_ticks / (BXE_BTR * 4)) ? 0 : 1);
10417
10418                 /* Transmit interrupt coalescing is done on CSTORM. */
10419                 REG_WR8(sc, BAR_CSTORM_INTMEM +
10420                     CSTORM_SB_HC_TIMEOUT_C_OFFSET(port, sb_id,
10421                     C_SB_ETH_TX_CQ_INDEX), sc->tx_ticks / (BXE_BTR * 4));
10422                 REG_WR16(sc, BAR_CSTORM_INTMEM +
10423                     CSTORM_SB_HC_DISABLE_C_OFFSET(port, sb_id,
10424                     C_SB_ETH_TX_CQ_INDEX),
10425                     (sc->tx_ticks / (BXE_BTR * 4)) ? 0 : 1);
10426         }
10427
10428         DBEXIT(BXE_VERBOSE_LOAD | BXE_VERBOSE_RESET);
10429 }
10430
10431 /*
10432  * Allocate an mbuf and assign it to the TPA pool.
10433  *
10434  * Returns:
10435  *   0 = Success, !0 = Failure
10436  *
10437  * Modifies:
10438  *   fp->tpa_mbuf_ptr[queue]
10439  *   fp->tpa_mbuf_map[queue]
10440  *   fp->tpa_mbuf_segs[queue]
10441  */
10442 static int
10443 bxe_alloc_tpa_mbuf(struct bxe_fastpath *fp, int queue)
10444 {
10445         struct bxe_softc *sc;
10446         bus_dma_segment_t segs[1];
10447         bus_dmamap_t map;
10448         struct mbuf *m;
10449         int nsegs, rc;
10450
10451         sc = fp->sc;
10452         DBENTER(BXE_INSANE_TPA);
10453         rc = 0;
10454
10455         DBRUNIF((fp->disable_tpa == TRUE),
10456             BXE_PRINTF("%s(): fp[%02d] TPA disabled!\n",
10457             __FUNCTION__, fp->index));
10458
10459 #ifdef BXE_DEBUG
10460         /* Simulate an mbuf allocation failure. */
10461         if (DB_RANDOMTRUE(bxe_debug_mbuf_allocation_failure)) {
10462                 sc->debug_sim_mbuf_alloc_failed++;
10463                 fp->mbuf_tpa_alloc_failed++;
10464                 rc = ENOMEM;
10465                 goto bxe_alloc_tpa_mbuf_exit;
10466         }
10467 #endif
10468
10469         /* Allocate the new TPA mbuf. */
10470         m = m_getjcl(M_NOWAIT, MT_DATA, M_PKTHDR, sc->mbuf_alloc_size);
10471         if (__predict_false(m == NULL)) {
10472                 fp->mbuf_tpa_alloc_failed++;
10473                 rc = ENOBUFS;
10474                 goto bxe_alloc_tpa_mbuf_exit;
10475         }
10476
10477         DBRUN(fp->tpa_mbuf_alloc++);
10478
10479         /* Initialize the mbuf buffer length. */
10480         m->m_pkthdr.len = m->m_len = sc->mbuf_alloc_size;
10481
10482 #ifdef BXE_DEBUG
10483         /* Simulate an mbuf mapping failure. */
10484         if (DB_RANDOMTRUE(bxe_debug_dma_map_addr_failure)) {
10485                 sc->debug_sim_mbuf_map_failed++;
10486                 fp->mbuf_tpa_mapping_failed++;
10487                 m_freem(m);
10488                 DBRUN(fp->tpa_mbuf_alloc--);
10489                 rc = ENOMEM;
10490                 goto bxe_alloc_tpa_mbuf_exit;
10491         }
10492 #endif
10493
10494         /* Map the TPA mbuf into non-paged pool. */
10495         rc = bus_dmamap_load_mbuf_sg(fp->rx_mbuf_tag,
10496             fp->tpa_mbuf_spare_map, m, segs, &nsegs, BUS_DMA_NOWAIT);
10497         if (__predict_false(rc != 0)) {
10498                 fp->mbuf_tpa_mapping_failed++;
10499                 m_free(m);
10500                 DBRUN(fp->tpa_mbuf_alloc--);
10501                 goto bxe_alloc_tpa_mbuf_exit;
10502         }
10503
10504         /* All mubfs must map to a single segment. */
10505         KASSERT(nsegs == 1, ("%s(): Too many segments (%d) returned!",
10506             __FUNCTION__, nsegs));
10507
10508         /* Release any existing TPA mbuf mapping. */
10509         if (fp->tpa_mbuf_map[queue] != NULL) {
10510                 bus_dmamap_sync(fp->rx_mbuf_tag,
10511                     fp->tpa_mbuf_map[queue], BUS_DMASYNC_POSTREAD);
10512                 bus_dmamap_unload(fp->rx_mbuf_tag,
10513                     fp->tpa_mbuf_map[queue]);
10514         }
10515
10516         /* Save the mbuf and mapping info for the TPA mbuf. */
10517         map = fp->tpa_mbuf_map[queue];
10518         fp->tpa_mbuf_map[queue] = fp->tpa_mbuf_spare_map;
10519         fp->tpa_mbuf_spare_map = map;
10520         bus_dmamap_sync(fp->rx_mbuf_tag,
10521             fp->tpa_mbuf_map[queue], BUS_DMASYNC_PREREAD);
10522         fp->tpa_mbuf_ptr[queue] = m;
10523         fp->tpa_mbuf_segs[queue] = segs[0];
10524
10525 bxe_alloc_tpa_mbuf_exit:
10526         DBEXIT(BXE_INSANE_TPA);
10527         return (rc);
10528 }
10529
10530 /*
10531  * Allocate mbufs for a fastpath TPA pool.
10532  *
10533  * Returns:
10534  *   0 = Success, !0 = Failure.
10535  *
10536  * Modifies:
10537  *   fp->tpa_state[]
10538  *   fp->disable_tpa
10539  */
10540 static int
10541 bxe_fill_tpa_pool(struct bxe_fastpath *fp)
10542 {
10543         struct bxe_softc *sc;
10544         int max_agg_queues, queue, rc;
10545
10546         sc = fp->sc;
10547         DBENTER(BXE_VERBOSE_LOAD | BXE_VERBOSE_RESET);
10548         rc = 0;
10549
10550         if (!TPA_ENABLED(sc)) {
10551                 fp->disable_tpa = TRUE;
10552                 goto bxe_fill_tpa_pool_exit;
10553         }
10554
10555         max_agg_queues = CHIP_IS_E1(sc) ? ETH_MAX_AGGREGATION_QUEUES_E1 :
10556             ETH_MAX_AGGREGATION_QUEUES_E1H;
10557
10558         /* Assume the fill operation worked. */
10559         fp->disable_tpa = FALSE;
10560
10561         /* Fill the TPA pool. */
10562         for (queue = 0; queue < max_agg_queues; queue++) {
10563                 rc = bxe_alloc_tpa_mbuf(fp, queue);
10564                 if (rc != 0) {
10565                         BXE_PRINTF(
10566                             "%s(%d): fp[%02d] TPA disabled!\n",
10567                             __FILE__, __LINE__, fp->index);
10568                         fp->disable_tpa = TRUE;
10569                         break;
10570                 }
10571                 fp->tpa_state[queue] = BXE_TPA_STATE_STOP;
10572         }
10573
10574 bxe_fill_tpa_pool_exit:
10575         DBEXIT(BXE_VERBOSE_LOAD | BXE_VERBOSE_RESET);
10576         return (rc);
10577 }
10578
10579 /*
10580  * Free all mbufs from a fastpath TPA pool.
10581  *
10582  * Returns:
10583  *   None
10584  *
10585  * Modifies:
10586  *   fp->tpa_mbuf_ptr[]
10587  *   fp->tpa_mbuf_map[]
10588  *   fp->tpa_mbuf_alloc
10589  */
10590 static void
10591 bxe_free_tpa_pool(struct bxe_fastpath *fp)
10592 {
10593         struct bxe_softc *sc;
10594         int i, max_agg_queues;
10595
10596         sc = fp->sc;
10597         DBENTER(BXE_INSANE_LOAD | BXE_INSANE_UNLOAD | BXE_INSANE_TPA);
10598
10599         if (fp->rx_mbuf_tag == NULL)
10600                 goto bxe_free_tpa_pool_exit;
10601
10602         max_agg_queues = CHIP_IS_E1H(sc) ?
10603             ETH_MAX_AGGREGATION_QUEUES_E1H :
10604             ETH_MAX_AGGREGATION_QUEUES_E1;
10605
10606         /* Release all mbufs and and all DMA maps in the TPA pool. */
10607         for (i = 0; i < max_agg_queues; i++) {
10608                 if (fp->tpa_mbuf_map[i] != NULL) {
10609                         bus_dmamap_sync(fp->rx_mbuf_tag, fp->tpa_mbuf_map[i],
10610                             BUS_DMASYNC_POSTREAD);
10611                         bus_dmamap_unload(fp->rx_mbuf_tag, fp->tpa_mbuf_map[i]);
10612                 }
10613
10614                 if (fp->tpa_mbuf_ptr[i] != NULL) {
10615                         m_freem(fp->tpa_mbuf_ptr[i]);
10616                         DBRUN(fp->tpa_mbuf_alloc--);
10617                         fp->tpa_mbuf_ptr[i] = NULL;
10618                 }
10619         }
10620
10621 bxe_free_tpa_pool_exit:
10622         DBEXIT(BXE_INSANE_LOAD | BXE_INSANE_UNLOAD | BXE_INSANE_TPA);
10623 }
10624
10625 /*
10626  * Allocate an mbuf and assign it to the receive scatter gather chain.
10627  * The caller must take care to save a copy of the existing mbuf in the
10628  * SG mbuf chain.
10629  *
10630  * Returns:
10631  *   0 = Success, !0= Failure.
10632  *
10633  * Modifies:
10634  *   fp->sg_chain[index]
10635  *   fp->rx_sge_buf_ptr[index]
10636  *   fp->rx_sge_buf_map[index]
10637  *   fp->rx_sge_spare_map
10638  */
10639 static int
10640 bxe_alloc_rx_sge_mbuf(struct bxe_fastpath *fp, uint16_t index)
10641 {
10642         struct bxe_softc *sc;
10643         struct eth_rx_sge *sge;
10644         bus_dma_segment_t segs[1];
10645         bus_dmamap_t map;
10646         struct mbuf *m;
10647         int nsegs, rc;
10648
10649         sc = fp->sc;
10650         DBENTER(BXE_INSANE_TPA);
10651         rc = 0;
10652
10653 #ifdef BXE_DEBUG
10654         /* Simulate an mbuf allocation failure. */
10655         if (DB_RANDOMTRUE(bxe_debug_mbuf_allocation_failure)) {
10656                 sc->debug_sim_mbuf_alloc_failed++;
10657                 fp->mbuf_sge_alloc_failed++;
10658                 rc = ENOMEM;
10659                 goto bxe_alloc_rx_sge_mbuf_exit;
10660         }
10661 #endif
10662
10663         /* Allocate a new SGE mbuf. */
10664         m = m_getjcl(M_NOWAIT, MT_DATA, M_PKTHDR, SGE_PAGE_SIZE);
10665         if (__predict_false(m == NULL)) {
10666                 fp->mbuf_sge_alloc_failed++;
10667                 rc = ENOMEM;
10668                 goto bxe_alloc_rx_sge_mbuf_exit;
10669         }
10670
10671         DBRUN(fp->sge_mbuf_alloc++);
10672
10673         /* Initialize the mbuf buffer length. */
10674         m->m_pkthdr.len = m->m_len = SGE_PAGE_SIZE;
10675
10676 #ifdef BXE_DEBUG
10677         /* Simulate an mbuf mapping failure. */
10678         if (DB_RANDOMTRUE(bxe_debug_dma_map_addr_failure)) {
10679                 sc->debug_sim_mbuf_map_failed++;
10680                 fp->mbuf_sge_mapping_failed++;
10681                 m_freem(m);
10682                 DBRUN(fp->sge_mbuf_alloc--);
10683                 rc = ENOMEM;
10684                 goto bxe_alloc_rx_sge_mbuf_exit;
10685         }
10686 #endif
10687
10688         /* Map the SGE mbuf into non-paged pool. */
10689         rc = bus_dmamap_load_mbuf_sg(fp->rx_sge_buf_tag,
10690             fp->rx_sge_spare_map, m, segs, &nsegs, BUS_DMA_NOWAIT);
10691         if (__predict_false(rc != 0)) {
10692                 fp->mbuf_sge_mapping_failed++;
10693                 m_freem(m);
10694                 DBRUN(fp->sge_mbuf_alloc--);
10695                 goto bxe_alloc_rx_sge_mbuf_exit;
10696         }
10697
10698         /* All mubfs must map to a single segment. */
10699         KASSERT(nsegs == 1, ("%s(): Too many segments (%d) returned!",
10700             __FUNCTION__, nsegs));
10701
10702         /* Unload any existing SGE mbuf mapping. */
10703         if (fp->rx_sge_buf_map[index] != NULL) {
10704                 bus_dmamap_sync(fp->rx_sge_buf_tag,
10705                     fp->rx_sge_buf_map[index], BUS_DMASYNC_POSTREAD);
10706                 bus_dmamap_unload(fp->rx_sge_buf_tag,
10707                     fp->rx_sge_buf_map[index]);
10708         }
10709
10710         /* Add the new SGE mbuf to the SGE ring. */
10711         map = fp->rx_sge_buf_map[index];
10712         fp->rx_sge_buf_map[index] = fp->rx_sge_spare_map;
10713         fp->rx_sge_spare_map = map;
10714         bus_dmamap_sync(fp->rx_sge_buf_tag,
10715             fp->rx_sge_buf_map[index], BUS_DMASYNC_PREREAD);
10716         fp->rx_sge_buf_ptr[index] = m;
10717         sge = &fp->sg_chain[index];
10718         sge->addr_hi = htole32(U64_HI(segs[0].ds_addr));
10719         sge->addr_lo = htole32(U64_LO(segs[0].ds_addr));
10720
10721 bxe_alloc_rx_sge_mbuf_exit:
10722         DBEXIT(BXE_INSANE_TPA);
10723         return (rc);
10724 }
10725
10726 /*
10727  * Allocate mbufs for a SGE chain.
10728  *
10729  * Returns:
10730  *   0 = Success, !0 = Failure.
10731  *
10732  * Modifies:
10733  *   fp->disable_tpa
10734  *   fp->rx_sge_prod
10735  */
10736 static int
10737 bxe_fill_sg_chain(struct bxe_fastpath *fp)
10738 {
10739         struct bxe_softc *sc;
10740         uint16_t index;
10741         int i, rc;
10742
10743
10744         sc = fp->sc;
10745         DBENTER(BXE_VERBOSE_LOAD | BXE_VERBOSE_RESET);
10746         rc = 0;
10747
10748         if (!TPA_ENABLED(sc)) {
10749                 fp->disable_tpa = TRUE;
10750                 goto bxe_fill_sg_chain_exit;
10751         }
10752
10753         /* Assume the fill operation works. */
10754         fp->disable_tpa = FALSE;
10755
10756         /* Fill the RX SGE chain. */
10757         index = 0;
10758         for (i = 0; i < USABLE_RX_SGE; i++) {
10759                 rc = bxe_alloc_rx_sge_mbuf(fp, index);
10760                 if (rc != 0) {
10761                         BXE_PRINTF(
10762                         "%s(%d): fp[%02d] SGE memory allocation failure!\n",
10763                             __FILE__, __LINE__, fp->index);
10764                         index = 0;
10765                         fp->disable_tpa = TRUE;
10766                         break;
10767                 }
10768                 index = NEXT_SGE_IDX(index);
10769         }
10770
10771         /* Update the driver's copy of the RX SGE producer index. */
10772         fp->rx_sge_prod = index;
10773
10774 bxe_fill_sg_chain_exit:
10775         DBEXIT(BXE_VERBOSE_LOAD | BXE_VERBOSE_RESET);
10776         return (rc);
10777 }
10778
10779 /*
10780  * Free all elements from the receive scatter gather chain.
10781  *
10782  * Returns:
10783  *   None
10784  *
10785  * Modifies:
10786  *   fp->rx_sge_buf_ptr[]
10787  *   fp->rx_sge_buf_map[]
10788  *   fp->sge_mbuf_alloc
10789  */
10790 static void
10791 bxe_free_sg_chain(struct bxe_fastpath *fp)
10792 {
10793         struct bxe_softc *sc;
10794         int i;
10795
10796         sc = fp->sc;
10797         DBENTER(BXE_INSANE_TPA);
10798
10799         if (fp->rx_sge_buf_tag == NULL)
10800                 goto bxe_free_sg_chain_exit;
10801
10802         /* Free all mbufs and unload all maps. */
10803         for (i = 0; i < TOTAL_RX_SGE; i++) {
10804                 /* Free the map and the mbuf if they're allocated. */
10805                 if (fp->rx_sge_buf_map[i] != NULL) {
10806                         bus_dmamap_sync(fp->rx_sge_buf_tag,
10807                             fp->rx_sge_buf_map[i], BUS_DMASYNC_POSTREAD);
10808                         bus_dmamap_unload(fp->rx_sge_buf_tag,
10809                             fp->rx_sge_buf_map[i]);
10810                 }
10811
10812                 if (fp->rx_sge_buf_ptr[i] != NULL) {
10813                         m_freem(fp->rx_sge_buf_ptr[i]);
10814                         DBRUN(fp->sge_mbuf_alloc--);
10815                         fp->rx_sge_buf_ptr[i] = NULL;
10816                 }
10817         }
10818
10819 bxe_free_sg_chain_exit:
10820         DBEXIT(BXE_INSANE_TPA);
10821 }
10822
10823 /*
10824  * Allocate an mbuf, if necessary, and add it to the receive chain.
10825  *
10826  * Returns:
10827  *   0 = Success, !0 = Failure.
10828  */
10829 static int
10830 bxe_alloc_rx_bd_mbuf(struct bxe_fastpath *fp, uint16_t index)
10831 {
10832         struct bxe_softc *sc;
10833         struct eth_rx_bd *rx_bd;
10834         bus_dma_segment_t segs[1];
10835         bus_dmamap_t map;
10836         struct mbuf *m;
10837         int nsegs, rc;
10838
10839         sc = fp->sc;
10840         DBENTER(BXE_INSANE_LOAD | BXE_INSANE_RESET | BXE_INSANE_RECV);
10841         rc = 0;
10842
10843 #ifdef BXE_DEBUG
10844         /* Simulate an mbuf allocation failure. */
10845         if (DB_RANDOMTRUE(bxe_debug_mbuf_allocation_failure)) {
10846                 sc->debug_sim_mbuf_alloc_failed++;
10847                 fp->mbuf_rx_bd_alloc_failed++;
10848                 rc = ENOMEM;
10849                 goto bxe_alloc_rx_bd_mbuf_exit;
10850         }
10851 #endif
10852
10853         /* Allocate the new RX BD mbuf. */
10854         m = m_getjcl(M_NOWAIT, MT_DATA, M_PKTHDR, sc->mbuf_alloc_size);
10855         if (__predict_false(m == NULL)) {
10856                 fp->mbuf_rx_bd_alloc_failed++;
10857                 rc = ENOBUFS;
10858                 goto bxe_alloc_rx_bd_mbuf_exit;
10859         }
10860
10861         DBRUN(fp->rx_mbuf_alloc++);
10862
10863         /* Initialize the mbuf buffer length. */
10864         m->m_pkthdr.len = m->m_len = sc->mbuf_alloc_size;
10865
10866 #ifdef BXE_DEBUG
10867         /* Simulate an mbuf mapping failure. */
10868         if (DB_RANDOMTRUE(bxe_debug_dma_map_addr_failure)) {
10869                 sc->debug_sim_mbuf_map_failed++;
10870                 fp->mbuf_rx_bd_mapping_failed++;
10871                 m_freem(m);
10872                 DBRUN(fp->rx_mbuf_alloc--);
10873                 rc = ENOMEM;
10874                 goto bxe_alloc_rx_bd_mbuf_exit;
10875         }
10876 #endif
10877
10878         /* Map the TPA mbuf into non-paged pool. */
10879         rc = bus_dmamap_load_mbuf_sg(fp->rx_mbuf_tag,
10880             fp->rx_mbuf_spare_map, m, segs, &nsegs, BUS_DMA_NOWAIT);
10881         if (__predict_false(rc != 0)) {
10882                 fp->mbuf_rx_bd_mapping_failed++;
10883                 m_freem(m);
10884                 DBRUN(fp->rx_mbuf_alloc--);
10885                 goto bxe_alloc_rx_bd_mbuf_exit;
10886         }
10887
10888         /* All mubfs must map to a single segment. */
10889         KASSERT(nsegs == 1, ("%s(): Too many segments (%d) returned!",
10890             __FUNCTION__, nsegs));
10891
10892         /* Release any existing RX BD mbuf mapping. */
10893         if (fp->rx_mbuf_map[index] != NULL) {
10894                 bus_dmamap_sync(fp->rx_mbuf_tag,
10895                     fp->rx_mbuf_map[index], BUS_DMASYNC_POSTREAD);
10896                 bus_dmamap_unload(fp->rx_mbuf_tag,
10897                     fp->rx_mbuf_map[index]);
10898         }
10899
10900         /* Save the mbuf and mapping info. */
10901         map = fp->rx_mbuf_map[index];
10902         fp->rx_mbuf_map[index] = fp->rx_mbuf_spare_map;
10903         fp->rx_mbuf_spare_map = map;
10904         bus_dmamap_sync(fp->rx_mbuf_tag,
10905             fp->rx_mbuf_map[index], BUS_DMASYNC_PREREAD);
10906         fp->rx_mbuf_ptr[index] = m;
10907         rx_bd = &fp->rx_chain[index];
10908         rx_bd->addr_hi  = htole32(U64_HI(segs[0].ds_addr));
10909         rx_bd->addr_lo  = htole32(U64_LO(segs[0].ds_addr));
10910
10911 bxe_alloc_rx_bd_mbuf_exit:
10912         DBEXIT(BXE_INSANE_LOAD | BXE_INSANE_RESET | BXE_INSANE_RECV);
10913         return (rc);
10914 }
10915
10916
10917
10918 /*
10919  * Allocate mbufs for a receive  chain.
10920  *
10921  * Returns:
10922  *   0 = Success, !0 = Failure.
10923  *
10924  * Modifies:
10925  *   fp->rx_bd_prod
10926  */
10927 static int
10928 bxe_fill_rx_bd_chain(struct bxe_fastpath *fp)
10929 {
10930         struct bxe_softc *sc;
10931         uint16_t index;
10932         int i, rc;
10933
10934         sc = fp->sc;
10935         DBENTER(BXE_VERBOSE_LOAD | BXE_VERBOSE_RESET);
10936         rc = index = 0;
10937
10938         /* Allocate buffers for all the RX BDs in RX BD Chain. */
10939         for (i = 0; i < USABLE_RX_BD; i++) {
10940                 rc = bxe_alloc_rx_bd_mbuf(fp, index);
10941                 if (rc != 0) {
10942                         BXE_PRINTF(
10943         "%s(%d): Memory allocation failure! Cannot fill fp[%02d] RX chain.\n",
10944                             __FILE__, __LINE__, fp->index);
10945                         index = 0;
10946                         break;
10947                 }
10948                 index = NEXT_RX_BD(index);
10949         }
10950
10951         fp->rx_bd_prod = index;
10952         DBEXIT(BXE_VERBOSE_LOAD | BXE_VERBOSE_RESET);
10953         return (rc);
10954 }
10955
10956 /*
10957  * Free all buffers from the receive chain.
10958  *
10959  * Returns:
10960  *   None
10961  *
10962  * Modifies:
10963  *   fp->rx_mbuf_ptr[]
10964  *   fp->rx_mbuf_map[]
10965  *   fp->rx_mbuf_alloc
10966  */
10967 static void
10968 bxe_free_rx_bd_chain(struct bxe_fastpath *fp)
10969 {
10970         struct bxe_softc *sc;
10971         int i;
10972
10973         sc = fp->sc;
10974         DBENTER(BXE_VERBOSE_LOAD | BXE_VERBOSE_RESET);
10975
10976         if (fp->rx_mbuf_tag == NULL)
10977                 goto bxe_free_rx_bd_chain_exit;
10978
10979         /* Free all mbufs and unload all maps. */
10980         for (i = 0; i < TOTAL_RX_BD; i++) {
10981                 if (fp->rx_mbuf_map[i] != NULL) {
10982                         bus_dmamap_sync(fp->rx_mbuf_tag, fp->rx_mbuf_map[i],
10983                             BUS_DMASYNC_POSTREAD);
10984                         bus_dmamap_unload(fp->rx_mbuf_tag, fp->rx_mbuf_map[i]);
10985                 }
10986
10987                 if (fp->rx_mbuf_ptr[i] != NULL) {
10988                         m_freem(fp->rx_mbuf_ptr[i]);
10989                         DBRUN(fp->rx_mbuf_alloc--);
10990                         fp->rx_mbuf_ptr[i] = NULL;
10991                 }
10992         }
10993
10994 bxe_free_rx_bd_chain_exit:
10995         DBEXIT(BXE_VERBOSE_LOAD | BXE_VERBOSE_RESET);
10996 }
10997
10998 /*
10999  * Setup mutexes used by the driver.
11000  *
11001  * Returns:
11002  *   None.
11003  */
11004 static void
11005 bxe_mutexes_alloc(struct bxe_softc *sc)
11006 {
11007         struct bxe_fastpath *fp;
11008         int i;
11009
11010         DBENTER(BXE_VERBOSE_LOAD);
11011
11012         BXE_CORE_LOCK_INIT(sc, device_get_nameunit(sc->dev));
11013         BXE_SP_LOCK_INIT(sc, "bxe_sp_lock");
11014         BXE_DMAE_LOCK_INIT(sc, "bxe_dmae_lock");
11015         BXE_PHY_LOCK_INIT(sc, "bxe_phy_lock");
11016         BXE_FWMB_LOCK_INIT(sc, "bxe_fwmb_lock");
11017         BXE_PRINT_LOCK_INIT(sc, "bxe_print_lock");
11018
11019         /* Allocate one mutex for each fastpath structure. */
11020         for (i = 0; i < sc->num_queues; i++ ) {
11021                 fp = &sc->fp[i];
11022
11023                 /* Allocate per fastpath mutexes. */
11024                 snprintf(fp->mtx_name, sizeof(fp->mtx_name), "%s:fp[%02d]",
11025                     device_get_nameunit(sc->dev), fp->index);
11026                 mtx_init(&fp->mtx, fp->mtx_name, NULL, MTX_DEF);
11027         }
11028
11029         DBEXIT(BXE_VERBOSE_LOAD);
11030 }
11031
11032 /*
11033  * Free mutexes used by the driver.
11034  *
11035  * Returns:
11036  *   None.
11037  */
11038 static void
11039 bxe_mutexes_free(struct bxe_softc *sc)
11040 {
11041         struct bxe_fastpath *fp;
11042         int i;
11043
11044         DBENTER(BXE_VERBOSE_UNLOAD);
11045
11046         for (i = 0; i < sc->num_queues; i++ ) {
11047                 fp = &sc->fp[i];
11048
11049                 /* Release per fastpath mutexes. */
11050                 if (mtx_initialized(&fp->mtx))
11051                         mtx_destroy(&fp->mtx);
11052         }
11053
11054         BXE_PRINT_LOCK_DESTROY(sc);
11055         BXE_FWMB_LOCK_DESTROY(sc);
11056         BXE_PHY_LOCK_DESTROY(sc);
11057         BXE_DMAE_LOCK_DESTROY(sc);
11058         BXE_SP_LOCK_DESTROY(sc);
11059         BXE_CORE_LOCK_DESTROY(sc);
11060
11061         DBEXIT(BXE_VERBOSE_UNLOAD);
11062
11063 }
11064
11065 /*
11066  * Free memory and clear the RX data structures.
11067  *
11068  * Returns:
11069  *   Nothing.
11070  */
11071 static void
11072 bxe_clear_rx_chains(struct bxe_softc *sc)
11073 {
11074         struct bxe_fastpath *fp;
11075         int i;
11076
11077         DBENTER(BXE_VERBOSE_RESET);
11078
11079         for (i = 0; i < sc->num_queues; i++) {
11080                 fp = &sc->fp[i];
11081
11082                 /* Free all RX buffers. */
11083                 bxe_free_rx_bd_chain(fp);
11084                 bxe_free_tpa_pool(fp);
11085                 bxe_free_sg_chain(fp);
11086
11087                 /* Check if any mbufs lost in the process. */
11088                 DBRUNIF((fp->tpa_mbuf_alloc), DBPRINT(sc, BXE_FATAL,
11089                 "%s(): Memory leak! Lost %d mbufs from fp[%02d] TPA pool!\n",
11090                     __FUNCTION__, fp->tpa_mbuf_alloc, fp->index));
11091                 DBRUNIF((fp->sge_mbuf_alloc), DBPRINT(sc, BXE_FATAL,
11092                 "%s(): Memory leak! Lost %d mbufs from fp[%02d] SGE chain!\n",
11093                     __FUNCTION__, fp->sge_mbuf_alloc, fp->index));
11094                 DBRUNIF((fp->rx_mbuf_alloc), DBPRINT(sc, BXE_FATAL,
11095                 "%s(): Memory leak! Lost %d mbufs from fp[%02d] RX chain!\n",
11096                     __FUNCTION__, fp->rx_mbuf_alloc, fp->index));
11097         }
11098
11099         DBEXIT(BXE_VERBOSE_RESET);
11100 }
11101
11102 /*
11103  * Initialize the receive rings.
11104  *
11105  * Returns:
11106  *   None.
11107  */
11108 static int
11109 bxe_init_rx_chains(struct bxe_softc *sc)
11110 {
11111         struct bxe_fastpath *fp;
11112         int func, i, rc;
11113
11114         DBENTER(BXE_VERBOSE_LOAD | BXE_VERBOSE_RESET);
11115         rc = 0;
11116         func = BP_FUNC(sc);
11117
11118         /* Allocate memory for RX and CQ chains. */
11119         for (i = 0; i < sc->num_queues; i++) {
11120                 fp = &sc->fp[i];
11121                 DBPRINT(sc, (BXE_VERBOSE_LOAD | BXE_VERBOSE_RESET),
11122                     "%s(): Initializing fp[%02d] RX chain.\n", __FUNCTION__, i);
11123
11124                 fp->rx_bd_cons = fp->rx_bd_prod = 0;
11125                 fp->rx_cq_cons = fp->rx_cq_prod = 0;
11126
11127                 /* Pointer to status block's CQ consumer index. */
11128                 fp->rx_cq_cons_sb = &fp->status_block->
11129                     u_status_block.index_values[HC_INDEX_U_ETH_RX_CQ_CONS];
11130
11131                 /* Pointer to status block's receive consumer index. */
11132                 fp->rx_bd_cons_sb = &fp->status_block->
11133                     u_status_block.index_values[HC_INDEX_U_ETH_RX_BD_CONS];
11134
11135                 fp->rx_cq_prod = TOTAL_RCQ_ENTRIES;
11136                 fp->rx_pkts = fp->rx_tpa_pkts = fp->rx_soft_errors = 0;
11137
11138                 /* Allocate memory for the receive chain. */
11139                 rc = bxe_fill_rx_bd_chain(fp);
11140                 if (rc != 0)
11141                         goto bxe_init_rx_chains_exit;
11142
11143                 /* Allocate memory for TPA pool. */
11144                 rc = bxe_fill_tpa_pool(fp);
11145                 if (rc != 0)
11146                         goto bxe_init_rx_chains_exit;
11147
11148                 /* Allocate memory for scatter-gather chain. */
11149                 rc = bxe_fill_sg_chain(fp);
11150                 if (rc != 0)
11151                         goto bxe_init_rx_chains_exit;
11152
11153                 /* Prepare the receive BD and CQ buffers for DMA access. */
11154                 bus_dmamap_sync(fp->rx_dma.tag, fp->rx_dma.map,
11155                      BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
11156
11157                 bus_dmamap_sync(fp->rcq_dma.tag, fp->rcq_dma.map,
11158                      BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
11159
11160                 /*
11161                  * Tell the controller that we have rx_bd's and CQE's
11162                  * available.  Warning! this will generate an interrupt
11163                  * (to the TSTORM).  This must only be done when the
11164                  * controller is initialized.
11165                  */
11166                 bxe_update_rx_prod(sc, fp, fp->rx_bd_prod,
11167                     fp->rx_cq_prod, fp->rx_sge_prod);
11168
11169                 /* ToDo - Move to dma_alloc(). */
11170                 /*
11171                  * Tell controller where the receive CQ
11172                  * chains start in physical memory.
11173                  */
11174                 if (i == 0) {
11175                         REG_WR(sc, BAR_USTORM_INTMEM +
11176                             USTORM_MEM_WORKAROUND_ADDRESS_OFFSET(func),
11177                             U64_LO(fp->rcq_dma.paddr));
11178                         REG_WR(sc, BAR_USTORM_INTMEM +
11179                             USTORM_MEM_WORKAROUND_ADDRESS_OFFSET(func) + 4,
11180                             U64_HI(fp->rcq_dma.paddr));
11181                 }
11182         }
11183
11184 bxe_init_rx_chains_exit:
11185         /* Release memory if an error occurred. */
11186         if (rc != 0)
11187                 bxe_clear_rx_chains(sc);
11188
11189         DBEXIT(BXE_VERBOSE_LOAD | BXE_VERBOSE_RESET);
11190         return (rc);
11191 }
11192
11193 /*
11194  * Free memory and clear the TX data structures.
11195  *
11196  * Returns:
11197  *   Nothing.
11198  */
11199 static void
11200 bxe_clear_tx_chains(struct bxe_softc *sc)
11201 {
11202         struct bxe_fastpath *fp;
11203         int i, j;
11204
11205         DBENTER(BXE_VERBOSE_RESET);
11206
11207         for (i = 0; i < sc->num_queues; i++) {
11208                 fp = &sc->fp[i];
11209
11210                 /* Free all mbufs and unload all maps. */
11211                 if (fp->tx_mbuf_tag) {
11212                         for (j = 0; j < TOTAL_TX_BD; j++) {
11213                                 if (fp->tx_mbuf_ptr[j] != NULL) {
11214                                         bus_dmamap_sync(fp->tx_mbuf_tag,
11215                                             fp->tx_mbuf_map[j],
11216                                             BUS_DMASYNC_POSTWRITE);
11217                                         bus_dmamap_unload(fp->tx_mbuf_tag,
11218                                             fp->tx_mbuf_map[j]);
11219                                         m_freem(fp->tx_mbuf_ptr[j]);
11220                                         fp->tx_mbuf_alloc--;
11221                                         fp->tx_mbuf_ptr[j] = NULL;
11222                                 }
11223                         }
11224                 }
11225
11226                 /* Check if we lost any mbufs in the process. */
11227                 DBRUNIF((fp->tx_mbuf_alloc), DBPRINT(sc, BXE_FATAL,
11228         "%s(): Memory leak! Lost %d mbufs from fp[%02d] TX chain!\n",
11229                     __FUNCTION__, fp->tx_mbuf_alloc, fp->index));
11230         }
11231
11232         DBEXIT(BXE_VERBOSE_RESET);
11233 }
11234
11235 /*
11236  * Initialize the transmit chain.
11237  *
11238  * Returns:
11239  *   None.
11240  */
11241 static void
11242 bxe_init_tx_chains(struct bxe_softc *sc)
11243 {
11244         struct bxe_fastpath *fp;
11245         int i, j;
11246
11247         DBENTER(BXE_VERBOSE_LOAD | BXE_VERBOSE_RESET);
11248
11249         for (i = 0; i < sc->num_queues; i++) {
11250                 fp = &sc->fp[i];
11251
11252                 /* Initialize transmit doorbell. */
11253                 fp->tx_db.data.header.header = DOORBELL_HDR_DB_TYPE;
11254                 fp->tx_db.data.zero_fill1 = 0;
11255                 fp->tx_db.data.prod = 0;
11256
11257                 /* Initialize tranmsit producer/consumer indices. */
11258                 fp->tx_pkt_prod = fp->tx_pkt_cons = 0;
11259                 fp->tx_bd_prod  = fp->tx_bd_cons  = 0;
11260                 fp->tx_bd_used  = 0;
11261
11262                 /* Pointer to TX packet consumer in status block. */
11263                 fp->tx_pkt_cons_sb =
11264                     &fp->status_block->c_status_block.index_values[C_SB_ETH_TX_CQ_INDEX];
11265
11266                 /* Soft TX counters. */
11267                 fp->tx_pkts = 0;
11268                 fp->tx_soft_errors = 0;
11269                 fp->tx_offload_frames_csum_ip = 0;
11270                 fp->tx_offload_frames_csum_tcp = 0;
11271                 fp->tx_offload_frames_csum_udp = 0;
11272                 fp->tx_offload_frames_tso = 0;
11273                 fp->tx_header_splits = 0;
11274                 fp->tx_encap_failures = 0;
11275                 fp->tx_hw_queue_full = 0;
11276                 fp->tx_hw_max_queue_depth = 0;
11277                 fp->tx_dma_mapping_failure = 0;
11278                 fp->tx_max_drbr_queue_depth = 0;
11279                 fp->tx_window_violation_std = 0;
11280                 fp->tx_window_violation_tso = 0;
11281                 fp->tx_unsupported_tso_request_ipv6 = 0;
11282                 fp->tx_unsupported_tso_request_not_tcp = 0;
11283                 fp->tx_chain_lost_mbuf = 0;
11284                 fp->tx_frame_deferred = 0;
11285                 fp->tx_queue_xoff = 0;
11286
11287                 /* Clear all TX mbuf pointers. */
11288                 for (j = 0; j < TOTAL_TX_BD; j++) {
11289                         fp->tx_mbuf_ptr[j] = NULL;
11290                 }
11291         }
11292
11293         DBEXIT(BXE_VERBOSE_LOAD | BXE_VERBOSE_RESET);
11294 }
11295
11296 /*
11297  * Initialize the slowpath ring.
11298  *
11299  * Returns:
11300  *   None.
11301  */
11302 static void
11303 bxe_init_sp_ring(struct bxe_softc *sc)
11304 {
11305         int func;
11306
11307         func = BP_FUNC(sc);
11308
11309         DBENTER(BXE_VERBOSE_LOAD | BXE_VERBOSE_RESET);
11310
11311         bzero((char *)sc->slowpath, BXE_SLOWPATH_SZ);
11312
11313         /* When the producer equals the consumer the chain is empty. */
11314         sc->spq_left = MAX_SPQ_PENDING;
11315         sc->spq_prod_idx = 0;
11316         sc->dsb_sp_prod = BXE_SP_DSB_INDEX;
11317         sc->spq_prod_bd = sc->spq;
11318         sc->spq_last_bd = sc->spq_prod_bd + MAX_SP_DESC_CNT;
11319
11320         /* Tell the controller the address of the slowpath ring. */
11321         REG_WR(sc, XSEM_REG_FAST_MEMORY + XSTORM_SPQ_PAGE_BASE_OFFSET(func),
11322             U64_LO(sc->spq_dma.paddr));
11323         REG_WR(sc, XSEM_REG_FAST_MEMORY + XSTORM_SPQ_PAGE_BASE_OFFSET(func) + 4,
11324             U64_HI(sc->spq_dma.paddr));
11325         REG_WR(sc, XSEM_REG_FAST_MEMORY + XSTORM_SPQ_PROD_OFFSET(func),
11326             sc->spq_prod_idx);
11327
11328         DBEXIT(BXE_VERBOSE_LOAD | BXE_VERBOSE_RESET);
11329 }
11330
11331 /*
11332  * Initialize STORM processor context.
11333  *
11334  * Returns:
11335  *   None.
11336  */
11337 static void
11338 bxe_init_context(struct bxe_softc *sc)
11339 {
11340         struct eth_context *context;
11341         struct bxe_fastpath *fp;
11342         uint8_t sb_id;
11343         uint8_t cl_id;
11344         int i;
11345
11346         DBENTER(BXE_VERBOSE_LOAD | BXE_VERBOSE_RESET);
11347
11348         for (i = 0; i < sc->num_queues; i++) {
11349                 context = BXE_SP(sc, context[i].eth);
11350                 fp = &sc->fp[i];
11351                 sb_id = fp->sb_id;
11352                 cl_id = fp->cl_id;
11353
11354                 /* Update the USTORM context. */
11355                 context->ustorm_st_context.common.sb_index_numbers =
11356                     BXE_RX_SB_INDEX_NUM;
11357                 context->ustorm_st_context.common.clientId = cl_id;
11358                 context->ustorm_st_context.common.status_block_id = sb_id;
11359                 /* Enable packet alignment/pad and statistics. */
11360                 context->ustorm_st_context.common.flags =
11361                     USTORM_ETH_ST_CONTEXT_CONFIG_ENABLE_MC_ALIGNMENT;
11362                 if (sc->stats_enable == TRUE)
11363                         context->ustorm_st_context.common.flags |=
11364                     USTORM_ETH_ST_CONTEXT_CONFIG_ENABLE_STATISTICS;
11365                 context->ustorm_st_context.common.statistics_counter_id=cl_id;
11366                 /*
11367                  * Set packet alignment boundary.
11368                  * (Must be >= 4 (i.e. 16 bytes).)
11369                  */
11370                 context->ustorm_st_context.common.mc_alignment_log_size = 8;
11371                 /* Set the size of the receive buffers. */
11372                 context->ustorm_st_context.common.bd_buff_size =
11373                     sc->mbuf_alloc_size;
11374
11375                 /* Set the address of the receive chain base page. */
11376                 context->ustorm_st_context.common.bd_page_base_hi =
11377                     U64_HI(fp->rx_dma.paddr);
11378                 context->ustorm_st_context.common.bd_page_base_lo =
11379                     U64_LO(fp->rx_dma.paddr);
11380
11381                 if (TPA_ENABLED(sc) && (fp->disable_tpa == FALSE)) {
11382                         /* Enable TPA and SGE chain support. */
11383                         context->ustorm_st_context.common.flags |=
11384                             USTORM_ETH_ST_CONTEXT_CONFIG_ENABLE_TPA;
11385
11386                         /* Set the size of the SGE buffer. */
11387                         context->ustorm_st_context.common.sge_buff_size =
11388                             (uint16_t) (SGE_PAGE_SIZE * PAGES_PER_SGE);
11389
11390                         /* Set the address of the SGE chain base page. */
11391                         context->ustorm_st_context.common.sge_page_base_hi =
11392                             U64_HI(fp->sg_dma.paddr);
11393                         context->ustorm_st_context.common.sge_page_base_lo =
11394                             U64_LO(fp->sg_dma.paddr);
11395
11396                         DBPRINT(sc, BXE_VERBOSE_TPA, "%s(): MTU = %d\n",
11397                             __FUNCTION__, (int) sc->bxe_ifp->if_mtu);
11398
11399                         /* Describe MTU to SGE alignment. */
11400                         context->ustorm_st_context.common.max_sges_for_packet =
11401                             SGE_PAGE_ALIGN(sc->bxe_ifp->if_mtu) >>
11402                             SGE_PAGE_SHIFT;
11403                         context->ustorm_st_context.common.max_sges_for_packet =
11404                             ((context->ustorm_st_context.common.
11405                             max_sges_for_packet + PAGES_PER_SGE - 1) &
11406                             (~(PAGES_PER_SGE - 1))) >> PAGES_PER_SGE_SHIFT;
11407
11408                         DBPRINT(sc, BXE_VERBOSE_TPA,
11409                             "%s(): max_sges_for_packet = %d\n", __FUNCTION__,
11410                             context->ustorm_st_context.common.max_sges_for_packet);
11411                 }
11412
11413                 /* Update USTORM context. */
11414                 context->ustorm_ag_context.cdu_usage =
11415                     CDU_RSRVD_VALUE_TYPE_A(HW_CID(sc, i),
11416                     CDU_REGION_NUMBER_UCM_AG, ETH_CONNECTION_TYPE);
11417
11418                 /* Update XSTORM context. */
11419                 context->xstorm_ag_context.cdu_reserved =
11420                     CDU_RSRVD_VALUE_TYPE_A(HW_CID(sc, i),
11421                     CDU_REGION_NUMBER_XCM_AG, ETH_CONNECTION_TYPE);
11422
11423                 /* Set the address of the transmit chain base page. */
11424                 context->xstorm_st_context.tx_bd_page_base_hi =
11425                     U64_HI(fp->tx_dma.paddr);
11426                 context->xstorm_st_context.tx_bd_page_base_lo =
11427                     U64_LO(fp->tx_dma.paddr);
11428
11429                 /* Enable XSTORM statistics. */
11430                 context->xstorm_st_context.statistics_data = (cl_id |
11431                     XSTORM_ETH_ST_CONTEXT_STATISTICS_ENABLE);
11432
11433                 /* Update CSTORM status block configuration. */
11434                 context->cstorm_st_context.sb_index_number =
11435                     C_SB_ETH_TX_CQ_INDEX;
11436                 context->cstorm_st_context.status_block_id = sb_id;
11437         }
11438
11439         DBEXIT(BXE_VERBOSE_LOAD | BXE_VERBOSE_RESET);
11440 }
11441
11442 /*
11443  * Initialize indirection table.
11444  *
11445  * Returns:
11446  *   None.
11447  */
11448 static void
11449 bxe_init_ind_table(struct bxe_softc *sc)
11450 {
11451         int func, i;
11452
11453         func = BP_FUNC(sc);
11454
11455         DBENTER(BXE_VERBOSE_LOAD | BXE_VERBOSE_RESET);
11456
11457         if (sc->multi_mode == ETH_RSS_MODE_DISABLED)
11458                 return;
11459
11460         /* Initialize the indirection table. */
11461         for (i = 0; i < TSTORM_INDIRECTION_TABLE_SIZE; i++)
11462                 REG_WR8(sc, BAR_TSTORM_INTMEM +
11463                     TSTORM_INDIRECTION_TABLE_OFFSET(func) + i,
11464                     sc->fp->cl_id + (i % sc->num_queues));
11465
11466         DBEXIT(BXE_VERBOSE_LOAD | BXE_VERBOSE_RESET);
11467 }
11468
11469 /*
11470  * Set client configuration.
11471  *
11472  * Returns:
11473  *   None.
11474  */
11475 static void
11476 bxe_set_client_config(struct bxe_softc *sc)
11477 {
11478         struct tstorm_eth_client_config tstorm_client = {0};
11479         int i, port;
11480
11481         port = BP_PORT(sc);
11482
11483         DBENTER(BXE_VERBOSE_MISC);
11484
11485         tstorm_client.mtu = sc->bxe_ifp->if_mtu; /* ETHERMTU */
11486         tstorm_client.config_flags =
11487             (TSTORM_ETH_CLIENT_CONFIG_STATSITICS_ENABLE |
11488             TSTORM_ETH_CLIENT_CONFIG_E1HOV_REM_ENABLE);
11489
11490         /* Unconditionally enable VLAN tag stripping. */
11491         if (sc->rx_mode) {
11492                 tstorm_client.config_flags |=
11493                     TSTORM_ETH_CLIENT_CONFIG_VLAN_REM_ENABLE;
11494                 DBPRINT(sc, BXE_VERBOSE, "%s(): VLAN tag stripping enabled.\n",
11495                     __FUNCTION__);
11496         }
11497
11498         /* Initialize the receive mode for each receive queue. */
11499         for (i = 0; i < sc->num_queues; i++) {
11500                 tstorm_client.statistics_counter_id = sc->fp[i].cl_id;
11501
11502                 REG_WR(sc, BAR_TSTORM_INTMEM +
11503                     TSTORM_CLIENT_CONFIG_OFFSET(port, sc->fp[i].cl_id),
11504                     ((uint32_t *) &tstorm_client)[0]);
11505                 REG_WR(sc, BAR_TSTORM_INTMEM +
11506                     TSTORM_CLIENT_CONFIG_OFFSET(port, sc->fp[i].cl_id) + 4,
11507                     ((uint32_t *) &tstorm_client)[1]);
11508         }
11509
11510         DBEXIT(BXE_VERBOSE_MISC);
11511 }
11512
11513 /*
11514  * Set receive mode.
11515  *
11516  * Programs the MAC according to the type of unicast/broadcast/multicast
11517  * packets it should receive.
11518  *
11519  * Returns:
11520  *   None.
11521  */
11522 static void
11523 bxe_set_storm_rx_mode(struct bxe_softc *sc)
11524 {
11525         struct tstorm_eth_mac_filter_config tstorm_mac_filter = {0};
11526         uint32_t llh_mask;
11527         int mode, mask;
11528         int func, i , port;
11529
11530         DBENTER(BXE_VERBOSE_LOAD | BXE_VERBOSE_RESET);
11531
11532         mode = sc->rx_mode;
11533         mask = 1 << BP_L_ID(sc);
11534         func = BP_FUNC(sc);
11535         port = BP_PORT(sc);
11536
11537         /* All but management unicast packets should pass to the host as well */
11538         llh_mask = NIG_LLH0_BRB1_DRV_MASK_REG_LLH0_BRB1_DRV_MASK_BRCST |
11539             NIG_LLH0_BRB1_DRV_MASK_REG_LLH0_BRB1_DRV_MASK_MLCST |
11540             NIG_LLH0_BRB1_DRV_MASK_REG_LLH0_BRB1_DRV_MASK_VLAN |
11541             NIG_LLH0_BRB1_DRV_MASK_REG_LLH0_BRB1_DRV_MASK_NO_VLAN;
11542
11543         /* Set the individual accept/drop flags based on the receive mode. */
11544         switch (mode) {
11545         case BXE_RX_MODE_NONE:
11546                 /* Drop everything. */
11547                 DBPRINT(sc, BXE_VERBOSE,
11548                     "%s(): Setting RX_MODE_NONE for function %d.\n",
11549                     __FUNCTION__, func);
11550                 tstorm_mac_filter.ucast_drop_all = mask;
11551                 tstorm_mac_filter.mcast_drop_all = mask;
11552                 tstorm_mac_filter.bcast_drop_all = mask;
11553                 break;
11554         case BXE_RX_MODE_NORMAL:
11555                 /* Accept all broadcast frames. */
11556                 DBPRINT(sc, BXE_VERBOSE,
11557                     "%s(): Setting RX_MODE_NORMAL for function %d.\n",
11558                     __FUNCTION__, func);
11559                 tstorm_mac_filter.bcast_accept_all = mask;
11560                 break;
11561         case BXE_RX_MODE_ALLMULTI:
11562                 /* Accept all broadcast and multicast frames. */
11563                 DBPRINT(sc, BXE_VERBOSE,
11564                     "%s(): Setting RX_MODE_ALLMULTI for function %d.\n",
11565                     __FUNCTION__, func);
11566                 tstorm_mac_filter.mcast_accept_all = mask;
11567                 tstorm_mac_filter.bcast_accept_all = mask;
11568                 break;
11569         case BXE_RX_MODE_PROMISC:
11570                 /* Accept all frames (promiscuous mode). */
11571                 DBPRINT(sc, BXE_VERBOSE,
11572                     "%s(): Setting RX_MODE_PROMISC for function %d.\n",
11573                     __FUNCTION__, func);
11574                 tstorm_mac_filter.ucast_accept_all = mask;
11575                 tstorm_mac_filter.mcast_accept_all = mask;
11576                 tstorm_mac_filter.bcast_accept_all = mask;
11577                 llh_mask |= NIG_LLH0_BRB1_DRV_MASK_REG_LLH0_BRB1_DRV_MASK_UNCST;
11578                 break;
11579
11580         default:
11581                 BXE_PRINTF(
11582                     "%s(%d): Tried to set unknown receive mode (0x%08X)!\n",
11583                     __FILE__, __LINE__, mode);
11584         }
11585
11586         REG_WR(sc, port ? NIG_REG_LLH1_BRB1_DRV_MASK :
11587             NIG_REG_LLH0_BRB1_DRV_MASK, llh_mask);
11588
11589         /* Write the RX mode filter to the TSTORM. */
11590         for (i = 0; i < sizeof(struct tstorm_eth_mac_filter_config) / 4; i++)
11591                 REG_WR(sc, BAR_TSTORM_INTMEM +
11592                     TSTORM_MAC_FILTER_CONFIG_OFFSET(func) + (i * 4),
11593                     ((uint32_t *) &tstorm_mac_filter)[i]);
11594
11595         if (mode != BXE_RX_MODE_NONE)
11596                 bxe_set_client_config(sc);
11597
11598         DBEXIT(BXE_VERBOSE_LOAD | BXE_VERBOSE_RESET);
11599 }
11600
11601 /*
11602  * Initialize common internal resources.  (Applies to both ports and
11603  * functions.)
11604  *
11605  * Returns:
11606  *   Nothing.
11607  */
11608 static void
11609 bxe_init_internal_common(struct bxe_softc *sc)
11610 {
11611         int i;
11612
11613         DBENTER(BXE_VERBOSE_LOAD | BXE_VERBOSE_RESET);
11614
11615         /*
11616          * Zero this manually as its initialization is currently not
11617          * handled through block initialization.
11618          */
11619         for (i = 0; i < (USTORM_AGG_DATA_SIZE >> 2); i++)
11620                 REG_WR(sc, BAR_USTORM_INTMEM + USTORM_AGG_DATA_OFFSET + i * 4,
11621                     0);
11622
11623         DBEXIT(BXE_VERBOSE_LOAD | BXE_VERBOSE_RESET);
11624 }
11625
11626 /*
11627  * Initialize port specific internal resources.
11628  *
11629  * Returns:
11630  *   Nothing.
11631  */
11632 static void
11633 bxe_init_internal_port(struct bxe_softc *sc)
11634 {
11635         int port = BP_PORT(sc);
11636
11637         port = BP_PORT(sc);
11638
11639         DBENTER(BXE_VERBOSE_LOAD | BXE_VERBOSE_RESET);
11640         DBPRINT(sc, (BXE_VERBOSE_LOAD | BXE_VERBOSE_RESET),
11641             "%s(): Port %d internal initialization.\n", __FUNCTION__, port);
11642
11643         /*
11644          * Each SDM timer tick is 4us. Configure host coalescing
11645          * basic timer resolution (BTR) to 12us (3 * 4us).
11646          */
11647         REG_WR(sc, BAR_CSTORM_INTMEM + CSTORM_HC_BTR_U_OFFSET(port), BXE_BTR);
11648         REG_WR(sc, BAR_CSTORM_INTMEM + CSTORM_HC_BTR_C_OFFSET(port), BXE_BTR);
11649         REG_WR(sc, BAR_TSTORM_INTMEM + TSTORM_HC_BTR_OFFSET(port), BXE_BTR);
11650         REG_WR(sc, BAR_XSTORM_INTMEM + XSTORM_HC_BTR_OFFSET(port), BXE_BTR);
11651
11652         DBEXIT(BXE_VERBOSE_LOAD | BXE_VERBOSE_RESET);
11653 }
11654
11655 /*
11656  * Initialize function specific internal resources.
11657  *
11658  * Returns:
11659  *   Nothing.
11660  */
11661 static void
11662 bxe_init_internal_func(struct bxe_softc *sc)
11663 {
11664         struct tstorm_eth_function_common_config tstorm_config = {0};
11665         struct stats_indication_flags stats_flags = {0};
11666         struct ustorm_eth_rx_pause_data_e1h rx_pause = {0};
11667         struct bxe_fastpath *fp;
11668         struct eth_rx_cqe_next_page *nextpg;
11669         uint32_t offset, size;
11670         uint16_t max_agg_size;
11671         uint8_t cl_id;
11672         int func, i, j, port;
11673
11674         port = BP_PORT(sc);
11675         func = BP_FUNC(sc);
11676
11677         DBENTER(BXE_VERBOSE_LOAD | BXE_VERBOSE_RESET);
11678         DBPRINT(sc, (BXE_VERBOSE_LOAD | BXE_VERBOSE_RESET),
11679             "%s(): Port %d, function %d internal initialization.\n",
11680             __FUNCTION__, port, func);
11681
11682         /*
11683          * Configure which fields the controller looks at when
11684          * distributing incoming frames for RSS/multi-queue operation.
11685          */
11686         if (sc->num_queues > 1) {
11687                 tstorm_config.config_flags = MULTI_FLAGS(sc);
11688                 tstorm_config.rss_result_mask = MULTI_MASK;
11689         }
11690
11691         /* Enable TPA if needed */
11692         if (TPA_ENABLED(sc))
11693                 tstorm_config.config_flags |=
11694                     TSTORM_ETH_FUNCTION_COMMON_CONFIG_ENABLE_TPA;
11695
11696         if (IS_E1HMF(sc))
11697                 tstorm_config.config_flags |=
11698                     TSTORM_ETH_FUNCTION_COMMON_CONFIG_E1HOV_IN_CAM;
11699
11700         tstorm_config.leading_client_id = BP_L_ID(sc);
11701
11702         REG_WR(sc, BAR_TSTORM_INTMEM +
11703             TSTORM_FUNCTION_COMMON_CONFIG_OFFSET(func),
11704             (*(uint32_t *)&tstorm_config));
11705
11706         /* Don't receive anything until the link is up. */
11707         sc->rx_mode = BXE_RX_MODE_NONE;
11708         sc->rx_mode_cl_mask = (1 << BP_L_ID(sc));
11709         bxe_set_storm_rx_mode(sc);
11710
11711         for (i = 0; i < sc->num_queues; i++) {
11712                 cl_id = sc->fp[i].cl_id;
11713                 /* Reset XSTORM per client statistics. */
11714                 size = sizeof(struct xstorm_per_client_stats) / 4;
11715                 offset = BAR_XSTORM_INTMEM +
11716                     XSTORM_PER_COUNTER_ID_STATS_OFFSET(port, cl_id);
11717                 for (j = 0; j < size; j++)
11718                         REG_WR(sc, offset +(j * 4), 0);
11719
11720                 /* Reset TSTORM per client statistics. */
11721                 size = sizeof(struct tstorm_per_client_stats) / 4;
11722                 offset = BAR_TSTORM_INTMEM +
11723                     TSTORM_PER_COUNTER_ID_STATS_OFFSET(port, cl_id);
11724                 for (j = 0; j < size; j++)
11725                         REG_WR(sc, offset + (j * 4), 0);
11726
11727                 /* Reset USTORM per client statistics. */
11728                 size = sizeof(struct ustorm_per_client_stats) / 4;
11729                 offset = BAR_USTORM_INTMEM +
11730                     USTORM_PER_COUNTER_ID_STATS_OFFSET(port, cl_id);
11731                 for (j = 0; j < size; j++)
11732                         REG_WR(sc, offset + (j * 4), 0);
11733         }
11734
11735         /* Initialize statistics related context. */
11736         stats_flags.collect_eth = 1;
11737
11738         REG_WR(sc, BAR_XSTORM_INTMEM + XSTORM_STATS_FLAGS_OFFSET(func),
11739             ((uint32_t *)&stats_flags)[0]);
11740         REG_WR(sc, BAR_XSTORM_INTMEM + XSTORM_STATS_FLAGS_OFFSET(func) + 4,
11741             ((uint32_t *)&stats_flags)[1]);
11742
11743         REG_WR(sc, BAR_TSTORM_INTMEM + TSTORM_STATS_FLAGS_OFFSET(func),
11744             ((uint32_t *)&stats_flags)[0]);
11745         REG_WR(sc, BAR_TSTORM_INTMEM + TSTORM_STATS_FLAGS_OFFSET(func) + 4,
11746             ((uint32_t *)&stats_flags)[1]);
11747
11748         REG_WR(sc, BAR_USTORM_INTMEM + USTORM_STATS_FLAGS_OFFSET(func),
11749             ((uint32_t *)&stats_flags)[0]);
11750         REG_WR(sc, BAR_USTORM_INTMEM + USTORM_STATS_FLAGS_OFFSET(func) + 4,
11751             ((uint32_t *)&stats_flags)[1]);
11752
11753         REG_WR(sc, BAR_CSTORM_INTMEM + CSTORM_STATS_FLAGS_OFFSET(func),
11754             ((uint32_t *)&stats_flags)[0]);
11755         REG_WR(sc, BAR_CSTORM_INTMEM + CSTORM_STATS_FLAGS_OFFSET(func) + 4,
11756             ((uint32_t *)&stats_flags)[1]);
11757
11758         REG_WR(sc, BAR_XSTORM_INTMEM + XSTORM_ETH_STATS_QUERY_ADDR_OFFSET(func),
11759             U64_LO(BXE_SP_MAPPING(sc, fw_stats)));
11760         REG_WR(sc, BAR_XSTORM_INTMEM +
11761             XSTORM_ETH_STATS_QUERY_ADDR_OFFSET(func) + 4,
11762             U64_HI(BXE_SP_MAPPING(sc, fw_stats)));
11763
11764         REG_WR(sc, BAR_TSTORM_INTMEM + TSTORM_ETH_STATS_QUERY_ADDR_OFFSET(func),
11765             U64_LO(BXE_SP_MAPPING(sc, fw_stats)));
11766         REG_WR(sc, BAR_TSTORM_INTMEM +
11767             TSTORM_ETH_STATS_QUERY_ADDR_OFFSET(func) + 4,
11768             U64_HI(BXE_SP_MAPPING(sc, fw_stats)));
11769
11770         REG_WR(sc, BAR_USTORM_INTMEM + USTORM_ETH_STATS_QUERY_ADDR_OFFSET(func),
11771             U64_LO(BXE_SP_MAPPING(sc, fw_stats)));
11772         REG_WR(sc, BAR_USTORM_INTMEM +
11773             USTORM_ETH_STATS_QUERY_ADDR_OFFSET(func) + 4,
11774             U64_HI(BXE_SP_MAPPING(sc, fw_stats)));
11775
11776         /* Additional initialization for 57711/57711E. */
11777         if (CHIP_IS_E1H(sc)) {
11778                 REG_WR8(sc, BAR_XSTORM_INTMEM + XSTORM_FUNCTION_MODE_OFFSET,
11779                     IS_E1HMF(sc));
11780                 REG_WR8(sc, BAR_TSTORM_INTMEM + TSTORM_FUNCTION_MODE_OFFSET,
11781                     IS_E1HMF(sc));
11782                 REG_WR8(sc, BAR_CSTORM_INTMEM + CSTORM_FUNCTION_MODE_OFFSET,
11783                     IS_E1HMF(sc));
11784                 REG_WR8(sc, BAR_USTORM_INTMEM + USTORM_FUNCTION_MODE_OFFSET,
11785                     IS_E1HMF(sc));
11786
11787                 /* Set the outer VLAN tag. */
11788                 REG_WR16(sc, BAR_XSTORM_INTMEM + XSTORM_E1HOV_OFFSET(func),
11789                     sc->e1hov);
11790         }
11791
11792         /* Init completion queue mapping and TPA aggregation size. */
11793         max_agg_size = min((uint32_t)(sc->mbuf_alloc_size +
11794             (8 * BCM_PAGE_SIZE * PAGES_PER_SGE)), (uint32_t)0xffff);
11795
11796         DBPRINT(sc, BXE_VERBOSE_TPA, "%s(): max_agg_size = 0x%08X\n",
11797             __FUNCTION__, max_agg_size);
11798
11799         for (i = 0; i < sc->num_queues; i++) {
11800                 fp = &sc->fp[i];
11801                 nextpg = (struct eth_rx_cqe_next_page *)
11802                     &fp->rcq_chain[USABLE_RCQ_ENTRIES_PER_PAGE];
11803
11804                 /* Program the completion queue address. */
11805                 REG_WR(sc, BAR_USTORM_INTMEM +
11806                     USTORM_CQE_PAGE_BASE_OFFSET(port, fp->cl_id),
11807                     U64_LO(fp->rcq_dma.paddr));
11808                 REG_WR(sc, BAR_USTORM_INTMEM +
11809                     USTORM_CQE_PAGE_BASE_OFFSET(port, fp->cl_id) + 4,
11810                     U64_HI(fp->rcq_dma.paddr));
11811
11812                 /* Program the first CQ next page address. */
11813                 REG_WR(sc, BAR_USTORM_INTMEM +
11814                     USTORM_CQE_PAGE_NEXT_OFFSET(port, fp->cl_id),
11815                     nextpg->addr_lo);
11816                 REG_WR(sc, BAR_USTORM_INTMEM +
11817                     USTORM_CQE_PAGE_NEXT_OFFSET(port, fp->cl_id) + 4,
11818                     nextpg->addr_hi);
11819
11820                 /* Set the maximum TPA aggregation size. */
11821                 REG_WR16(sc, BAR_USTORM_INTMEM +
11822                     USTORM_MAX_AGG_SIZE_OFFSET(port, fp->cl_id),
11823                     max_agg_size);
11824         }
11825
11826         /* Configure lossless flow control. */
11827         if (CHIP_IS_E1H(sc)) {
11828                 rx_pause.bd_thr_low = 250;
11829                 rx_pause.cqe_thr_low = 250;
11830                 rx_pause.cos = 1;
11831                 rx_pause.sge_thr_low = 0;
11832                 rx_pause.bd_thr_high = 350;
11833                 rx_pause.cqe_thr_high = 350;
11834                 rx_pause.sge_thr_high = 0;
11835
11836                 for (i = 0; i < sc->num_queues; i++) {
11837                         fp = &sc->fp[i];
11838                         if (fp->disable_tpa == FALSE) {
11839                                 rx_pause.sge_thr_low = 150;
11840                                 rx_pause.sge_thr_high = 250;
11841                         }
11842
11843                         offset = BAR_USTORM_INTMEM +
11844                             USTORM_ETH_RING_PAUSE_DATA_OFFSET(port, fp->cl_id);
11845
11846                         for (j = 0; j <
11847                             sizeof(struct ustorm_eth_rx_pause_data_e1h) / 4;
11848                             j++)
11849                                 REG_WR(sc, offset + (j * 4),
11850                                     ((uint32_t *)&rx_pause)[j]);
11851                 }
11852         }
11853
11854         memset(&(sc->cmng), 0, sizeof(struct cmng_struct_per_port));
11855         if (IS_E1HMF(sc)) {
11856                 /*
11857                  * During init there is no active link.
11858                  * Until link is up, assume link rate @ 10Gbps
11859                  */
11860                 bxe_read_mf_cfg(sc);
11861
11862                 if (!sc->vn_wsum)
11863                         DBPRINT(sc, BXE_VERBOSE_MISC,
11864                             "%s(): All MIN values are zeroes, "
11865                             "fairness will be disabled.\n", __FUNCTION__);
11866         }
11867
11868         /* Store it to internal memory */
11869         if (sc->port.pmf) {
11870                 for (i = 0; i < sizeof(struct cmng_struct_per_port) / 4; i++)
11871                         REG_WR(sc, BAR_XSTORM_INTMEM +
11872                             XSTORM_CMNG_PER_PORT_VARS_OFFSET(port) + i * 4,
11873                             ((uint32_t *)(&sc->cmng))[i]);
11874         }
11875
11876         DBEXIT(BXE_VERBOSE_LOAD | BXE_VERBOSE_RESET);
11877 }
11878
11879 /*
11880  * Initialize internal resources.
11881  *
11882  * Returns:
11883  *   Nothing.
11884  */
11885 static void
11886 bxe_init_internal(struct bxe_softc *sc, uint32_t load_code)
11887 {
11888
11889         DBENTER(BXE_VERBOSE_LOAD | BXE_VERBOSE_RESET);
11890
11891         switch (load_code) {
11892         case FW_MSG_CODE_DRV_LOAD_COMMON:
11893                 bxe_init_internal_common(sc);
11894                 /* FALLTHROUGH */
11895
11896         case FW_MSG_CODE_DRV_LOAD_PORT:
11897                 bxe_init_internal_port(sc);
11898                 /* FALLTHROUGH */
11899
11900         case FW_MSG_CODE_DRV_LOAD_FUNCTION:
11901                 bxe_init_internal_func(sc);
11902                 break;
11903
11904         default:
11905                 BXE_PRINTF(
11906                     "%s(%d): Unknown load_code (0x%08X) from MCP!\n",
11907                     __FILE__, __LINE__, load_code);
11908                 break;
11909         }
11910
11911         DBEXIT(BXE_VERBOSE_LOAD | BXE_VERBOSE_RESET);
11912 }
11913
11914
11915 /*
11916  * Perform driver instance specific initialization.
11917  *
11918  * Returns:
11919  *   None
11920  */
11921 static int
11922 bxe_init_nic(struct bxe_softc *sc, uint32_t load_code)
11923 {
11924         struct bxe_fastpath *fp;
11925         int i, rc;
11926
11927         DBENTER(BXE_VERBOSE_LOAD | BXE_VERBOSE_RESET);
11928
11929         /* Intialize fastpath structures and the status block. */
11930         for (i = 0; i < sc->num_queues; i++) {
11931                 fp = &sc->fp[i];
11932                 fp->disable_tpa = TRUE;
11933
11934                 bzero((char *)fp->status_block, BXE_STATUS_BLK_SZ);
11935                 fp->fp_u_idx = 0;
11936                 fp->fp_c_idx = 0;
11937
11938                 /* Set a pointer back to the driver instance. */
11939                 fp->sc = sc;
11940
11941                 /* Set the fastpath starting state as closed. */
11942                 fp->state = BXE_FP_STATE_CLOSED;
11943
11944                 /* Self-reference to this fastpath's instance. */
11945                 fp->index = i;
11946
11947                 /* Set the client ID beginning with the leading id. */
11948                 fp->cl_id = BP_L_ID(sc) + i;
11949
11950                 /* Set the status block ID for this fastpath instance. */
11951                 fp->sb_id = fp->cl_id;
11952
11953                 DBPRINT(sc, (BXE_VERBOSE_LOAD | BXE_VERBOSE_RESET),
11954                     "%s(): fp[%02d]: cl_id = %d, sb_id = %d\n",
11955                     __FUNCTION__, fp->index, fp->cl_id, fp->sb_id);
11956
11957                 /* Initialize the fastpath status block. */
11958                 bxe_init_sb(sc, fp->status_block, fp->sb_dma.paddr,
11959                     fp->sb_id);
11960                 bxe_update_fpsb_idx(fp);
11961         }
11962
11963         rmb();
11964
11965         bzero((char *)sc->def_sb, BXE_DEF_STATUS_BLK_SZ);
11966
11967         /* Initialize the Default Status Block. */
11968         bxe_init_def_sb(sc, sc->def_sb, sc->def_sb_dma.paddr, DEF_SB_ID);
11969         bxe_update_dsb_idx(sc);
11970
11971         /* Initialize the coalescence parameters. */
11972         bxe_update_coalesce(sc);
11973
11974         /* Initialize receive chains. */
11975         rc = bxe_init_rx_chains(sc);
11976         if (rc != 0) {
11977                 goto bxe_init_nic_exit;
11978         }
11979
11980         /* Initialize the Transmit BD Chain. */
11981         bxe_init_tx_chains(sc);
11982
11983         /* Initialize the Slow Path Chain. */
11984         bxe_init_sp_ring(sc);
11985
11986         /* Initialize STORM processor context/configuration. */
11987         bxe_init_context(sc);
11988
11989         /* Initialize the Context. */
11990         bxe_init_internal(sc, load_code);
11991
11992         /* Enable indirection table for multi-queue operation. */
11993         bxe_init_ind_table(sc);
11994
11995         mb();
11996
11997         /* Disable the interrupts from device until init is complete.*/
11998         bxe_int_disable(sc);
11999
12000 bxe_init_nic_exit:
12001         DBEXIT(BXE_VERBOSE_LOAD | BXE_VERBOSE_RESET);
12002         return (rc);
12003 }
12004
12005 /*
12006  * Send a loopback packet through the Network Interface Glue (NIG) block.
12007  *
12008  * Returns:
12009  *   None.
12010  */
12011 static void
12012 bxe_lb_pckt(struct bxe_softc *sc)
12013 {
12014 #ifdef BXE_USE_DMAE
12015         uint32_t wb_write[3];
12016 #endif
12017
12018         DBENTER(BXE_VERBOSE_LOAD | BXE_VERBOSE_RESET);
12019
12020         /* Ethernet source and destination addresses. */
12021 #ifdef BXE_USE_DMAE
12022         wb_write[0] = 0x55555555;
12023         wb_write[1] = 0x55555555;
12024         wb_write[2] = 0x20;     /* SOP */
12025         REG_WR_DMAE(sc, NIG_REG_DEBUG_PACKET_LB, wb_write, 3);
12026 #else
12027         REG_WR_IND(sc, NIG_REG_DEBUG_PACKET_LB, 0x55555555);
12028         REG_WR_IND(sc, NIG_REG_DEBUG_PACKET_LB + 4, 0x55555555);
12029         REG_WR_IND(sc, NIG_REG_DEBUG_PACKET_LB + 8, 0x20);
12030 #endif
12031
12032         /* NON-IP protocol. */
12033 #ifdef BXE_USE_DMAE
12034         wb_write[0] = 0x09000000;
12035         wb_write[1] = 0x55555555;
12036         wb_write[2] = 0x10;     /* EOP */
12037         REG_WR_DMAE(sc, NIG_REG_DEBUG_PACKET_LB, wb_write, 3);
12038 #else
12039         REG_WR_IND(sc, NIG_REG_DEBUG_PACKET_LB, 0x09000000);
12040         REG_WR_IND(sc, NIG_REG_DEBUG_PACKET_LB + 4, 0x55555555);
12041         REG_WR_IND(sc, NIG_REG_DEBUG_PACKET_LB + 8, 0x10);
12042 #endif
12043
12044         DBEXIT(BXE_VERBOSE_LOAD | BXE_VERBOSE_RESET);
12045 }
12046
12047 /*
12048  * Perform an internal memory test.
12049  *
12050  * Some internal memories are not accessible through the PCIe interface so
12051  * we send some debug packets for the test.
12052  *
12053  * Returns:
12054  *   0 = Success, !0 = Failure.
12055  */
12056 static int
12057 bxe_int_mem_test(struct bxe_softc *sc)
12058 {
12059         uint32_t val;
12060         int count, i, rc;
12061
12062         rc = 0;
12063         val = 0;
12064
12065         DBENTER(BXE_VERBOSE_LOAD | BXE_VERBOSE_RESET);
12066
12067         /* Perform a single debug packet test. */
12068
12069         /* Disable inputs of parser neighbor blocks. */
12070         REG_WR(sc, TSDM_REG_ENABLE_IN1, 0x0);
12071         REG_WR(sc, TCM_REG_PRS_IFEN, 0x0);
12072         REG_WR(sc, CFC_REG_DEBUG0, 0x1);
12073         REG_WR(sc, NIG_REG_PRS_REQ_IN_EN, 0x0);
12074
12075         /*  Write 0 to parser credits for CFC search request. */
12076         REG_WR(sc, PRS_REG_CFC_SEARCH_INITIAL_CREDIT, 0x0);
12077
12078         /* Send an Ethernet packet. */
12079         bxe_lb_pckt(sc);
12080
12081         /* Wait until NIG register shows 1 packet of size 0x10. */
12082         count = 1000;
12083         while (count) {
12084                 bxe_read_dmae(sc, NIG_REG_STAT2_BRB_OCTET, 2);
12085                 val = *BXE_SP(sc, wb_data[0]);
12086                 if (val == 0x10)
12087                         break;
12088
12089                 DELAY(10000);
12090                 count--;
12091         }
12092
12093         if (val != 0x10) {
12094                 DBPRINT(sc, BXE_FATAL,
12095                     "%s(): NIG loopback test 1 timeout (val = 0x%08X)!\n",
12096                     __FUNCTION__, val);
12097                 rc = 1;
12098                 goto bxe_int_mem_test_exit;
12099         }
12100
12101         /* Wait until PRS register shows 1 packet */
12102         count = 1000;
12103         while (count) {
12104                 val = REG_RD(sc, PRS_REG_NUM_OF_PACKETS);
12105
12106                 if (val == 1)
12107                         break;
12108
12109                 DELAY(10000);
12110                 count--;
12111         }
12112
12113         if (val != 0x1) {
12114                 DBPRINT(sc, BXE_FATAL,
12115                     "%s(): PRS loopback test 1 timeout (val = 0x%08X)!\n",
12116                     __FUNCTION__, val);
12117                 rc = 2;
12118                 goto bxe_int_mem_test_exit;
12119         }
12120
12121         /* Reset and init BRB, PRS. */
12122         REG_WR(sc, GRCBASE_MISC + MISC_REGISTERS_RESET_REG_1_CLEAR, 0x3);
12123         DELAY(50000);
12124         REG_WR(sc, GRCBASE_MISC + MISC_REGISTERS_RESET_REG_1_SET, 0x3);
12125         DELAY(50000);
12126         bxe_init_block(sc, BRB1_BLOCK, COMMON_STAGE);
12127         bxe_init_block(sc, PRS_BLOCK, COMMON_STAGE);
12128
12129         /* Perform the test again, this time with 10 packets. */
12130
12131         /* Disable inputs of parser neighbor blocks. */
12132         REG_WR(sc, TSDM_REG_ENABLE_IN1, 0x0);
12133         REG_WR(sc, TCM_REG_PRS_IFEN, 0x0);
12134         REG_WR(sc, CFC_REG_DEBUG0, 0x1);
12135         REG_WR(sc, NIG_REG_PRS_REQ_IN_EN, 0x0);
12136
12137         /* Write 0 to parser credits for CFC search request. */
12138         REG_WR(sc, PRS_REG_CFC_SEARCH_INITIAL_CREDIT, 0x0);
12139
12140         /* Send 10 Ethernet packets. */
12141         for (i = 0; i < 10; i++)
12142                 bxe_lb_pckt(sc);
12143
12144         /* Wait until NIG shows 10 + 1 packets of size 11 * 0x10 = 0xb0. */
12145         count = 1000;
12146         while (count) {
12147                 bxe_read_dmae(sc, NIG_REG_STAT2_BRB_OCTET, 2);
12148                 val = *BXE_SP(sc, wb_data[0]);
12149                 if (val == 0xb0)
12150                         break;
12151
12152                 DELAY(10000);
12153                 count--;
12154         }
12155
12156         if (val != 0xb0) {
12157                 DBPRINT(sc, BXE_FATAL,
12158                     "%s(): NIG loopback test 2 timeout (val = 0x%08X)!\n",
12159                     __FUNCTION__, val);
12160                 rc = 3;
12161                 goto bxe_int_mem_test_exit;
12162         }
12163
12164         /* Wait until PRS register shows 2 packets. */
12165         val = REG_RD(sc, PRS_REG_NUM_OF_PACKETS);
12166         if (val != 2) {
12167                 DBPRINT(sc, BXE_FATAL,
12168                     "%s(): PRS loopback test 2 timeout (val = 0x%x)!\n",
12169                     __FUNCTION__, val);
12170                 rc = 4;
12171                 goto bxe_int_mem_test_exit;
12172         }
12173
12174         /* Write 1 to parser credits for CFC search request. */
12175         REG_WR(sc, PRS_REG_CFC_SEARCH_INITIAL_CREDIT, 0x1);
12176
12177         /* Wait until PRS register shows 3 packets. */
12178         DELAY(10000);
12179
12180         /* Wait until NIG register shows 1 packet of size 0x10. */
12181         val = REG_RD(sc, PRS_REG_NUM_OF_PACKETS);
12182         if (val != 3) {
12183                 DBPRINT(sc, BXE_FATAL,
12184                     "%s(): PRS loopback test 3 timeout (val = 0x%08X)!\n",
12185                     __FUNCTION__, val);
12186                 rc = 5;
12187                 goto bxe_int_mem_test_exit;
12188         }
12189
12190         /* Clear NIG end-of-packet FIFO. */
12191         for (i = 0; i < 11; i++)
12192                 REG_RD(sc, NIG_REG_INGRESS_EOP_LB_FIFO);
12193
12194         val = REG_RD(sc, NIG_REG_INGRESS_EOP_LB_EMPTY);
12195         if (val != 1) {
12196                 DBPRINT(sc, BXE_INFO, "%s(): Unable to clear NIG!\n",
12197                     __FUNCTION__);
12198                 rc = 6;
12199                 goto bxe_int_mem_test_exit;
12200         }
12201
12202         /* Reset and init BRB, PRS, NIG. */
12203         REG_WR(sc, GRCBASE_MISC + MISC_REGISTERS_RESET_REG_1_CLEAR, 0x03);
12204         DELAY(50000);
12205         REG_WR(sc, GRCBASE_MISC + MISC_REGISTERS_RESET_REG_1_SET, 0x03);
12206         DELAY(50000);
12207         bxe_init_block(sc, BRB1_BLOCK, COMMON_STAGE);
12208         bxe_init_block(sc, PRS_BLOCK, COMMON_STAGE);
12209
12210         /* Set NIC mode. */
12211         REG_WR(sc, PRS_REG_NIC_MODE, 1);
12212
12213         /* Enable inputs of parser neighbor blocks. */
12214         REG_WR(sc, TSDM_REG_ENABLE_IN1, 0x7fffffff);
12215         REG_WR(sc, TCM_REG_PRS_IFEN, 0x1);
12216         REG_WR(sc, CFC_REG_DEBUG0, 0x0);
12217         REG_WR(sc, NIG_REG_PRS_REQ_IN_EN, 0x1);
12218
12219 bxe_int_mem_test_exit:
12220         DBEXIT(BXE_VERBOSE_LOAD | BXE_VERBOSE_RESET);
12221         return (rc);
12222 }
12223
12224 /*
12225  * Enable attentions from various blocks.
12226  *
12227  * Returns:
12228  *   None.
12229  */
12230 static void
12231 bxe_enable_blocks_attention(struct bxe_softc *sc)
12232 {
12233
12234         DBENTER(BXE_VERBOSE_LOAD | BXE_VERBOSE_RESET);
12235
12236         REG_WR(sc, PXP_REG_PXP_INT_MASK_0, 0);
12237         REG_WR(sc, PXP_REG_PXP_INT_MASK_1, 0);
12238         REG_WR(sc, DORQ_REG_DORQ_INT_MASK, 0);
12239         REG_WR(sc, CFC_REG_CFC_INT_MASK, 0);
12240         REG_WR(sc, QM_REG_QM_INT_MASK, 0);
12241         REG_WR(sc, TM_REG_TM_INT_MASK, 0);
12242         REG_WR(sc, XSDM_REG_XSDM_INT_MASK_0, 0);
12243         REG_WR(sc, XSDM_REG_XSDM_INT_MASK_1, 0);
12244         REG_WR(sc, XCM_REG_XCM_INT_MASK, 0);
12245
12246         REG_WR(sc, USDM_REG_USDM_INT_MASK_0, 0);
12247         REG_WR(sc, USDM_REG_USDM_INT_MASK_1, 0);
12248         REG_WR(sc, UCM_REG_UCM_INT_MASK, 0);
12249
12250         REG_WR(sc, GRCBASE_UPB + PB_REG_PB_INT_MASK, 0);
12251         REG_WR(sc, CSDM_REG_CSDM_INT_MASK_0, 0);
12252         REG_WR(sc, CSDM_REG_CSDM_INT_MASK_1, 0);
12253         REG_WR(sc, CCM_REG_CCM_INT_MASK, 0);
12254
12255         REG_WR(sc, PXP2_REG_PXP2_INT_MASK_0, 0x480000);
12256
12257         REG_WR(sc, TSDM_REG_TSDM_INT_MASK_0, 0);
12258         REG_WR(sc, TSDM_REG_TSDM_INT_MASK_1, 0);
12259         REG_WR(sc, TCM_REG_TCM_INT_MASK, 0);
12260
12261         REG_WR(sc, CDU_REG_CDU_INT_MASK, 0);
12262         REG_WR(sc, DMAE_REG_DMAE_INT_MASK, 0);
12263         REG_WR(sc, PBF_REG_PBF_INT_MASK, 0X18);
12264
12265         DBEXIT(BXE_VERBOSE_LOAD | BXE_VERBOSE_RESET);
12266 }
12267
12268 /*
12269  * PXP Arbiter
12270  */
12271
12272 /*
12273  * This code configures the PCI read/write arbiter
12274  * which implements a weighted round robin
12275  * between the virtual queues in the chip.
12276  *
12277  * The values were derived for each PCI max payload and max request size.
12278  * since max payload and max request size are only known at run time,
12279  * this is done as a separate init stage.
12280  */
12281
12282 #define NUM_WR_Q                        13
12283 #define NUM_RD_Q                        29
12284 #define MAX_RD_ORD                      3
12285 #define MAX_WR_ORD                      2
12286
12287 /* Configuration for one arbiter queue. */
12288 struct arb_line {
12289         int l;
12290         int add;
12291         int ubound;
12292 };
12293
12294 /* Derived configuration for each read queue for each max request size. */
12295 static const struct arb_line read_arb_data[NUM_RD_Q][MAX_RD_ORD + 1] = {
12296 /* 1 */ { {8, 64, 25}, {16, 64, 25}, {32, 64, 25}, {64, 64, 41} },
12297         { {4, 8,  4},  {4,  8,  4},  {4,  8,  4},  {4,  8,  4}  },
12298         { {4, 3,  3},  {4,  3,  3},  {4,  3,  3},  {4,  3,  3}  },
12299         { {8, 3,  6},  {16, 3,  11}, {16, 3,  11}, {16, 3,  11} },
12300         { {8, 64, 25}, {16, 64, 25}, {32, 64, 25}, {64, 64, 41} },
12301         { {8, 3,  6},  {16, 3,  11}, {32, 3,  21}, {64, 3,  41} },
12302         { {8, 3,  6},  {16, 3,  11}, {32, 3,  21}, {64, 3,  41} },
12303         { {8, 3,  6},  {16, 3,  11}, {32, 3,  21}, {64, 3,  41} },
12304         { {8, 3,  6},  {16, 3,  11}, {32, 3,  21}, {64, 3,  41} },
12305 /* 10 */{ {8, 3,  6},  {16, 3,  11}, {32, 3,  21}, {32, 3,  21} },
12306         { {8, 3,  6},  {16, 3,  11}, {32, 3,  21}, {32, 3,  21} },
12307         { {8, 3,  6},  {16, 3,  11}, {32, 3,  21}, {32, 3,  21} },
12308         { {8, 3,  6},  {16, 3,  11}, {32, 3,  21}, {32, 3,  21} },
12309         { {8, 3,  6},  {16, 3,  11}, {32, 3,  21}, {32, 3,  21} },
12310         { {8, 3,  6},  {16, 3,  11}, {32, 3,  21}, {32, 3,  21} },
12311         { {8, 3,  6},  {16, 3,  11}, {32, 3,  21}, {32, 3,  21} },
12312         { {8, 64, 6},  {16, 64, 11}, {32, 64, 21}, {32, 64, 21} },
12313         { {8, 3,  6},  {16, 3,  11}, {32, 3,  21}, {32, 3,  21} },
12314         { {8, 3,  6},  {16, 3,  11}, {32, 3,  21}, {32, 3,  21} },
12315 /* 20 */{ {8, 3,  6},  {16, 3,  11}, {32, 3,  21}, {32, 3,  21} },
12316         { {8, 3,  6},  {16, 3,  11}, {32, 3,  21}, {32, 3,  21} },
12317         { {8, 3,  6},  {16, 3,  11}, {32, 3,  21}, {32, 3,  21} },
12318         { {8, 3,  6},  {16, 3,  11}, {32, 3,  21}, {32, 3,  21} },
12319         { {8, 3,  6},  {16, 3,  11}, {32, 3,  21}, {32, 3,  21} },
12320         { {8, 3,  6},  {16, 3,  11}, {32, 3,  21}, {32, 3,  21} },
12321         { {8, 3,  6},  {16, 3,  11}, {32, 3,  21}, {32, 3,  21} },
12322         { {8, 3,  6},  {16, 3,  11}, {32, 3,  21}, {32, 3,  21} },
12323         { {8, 3,  6},  {16, 3,  11}, {32, 3,  21}, {32, 3,  21} },
12324         { {8, 64, 25}, {16, 64, 41}, {32, 64, 81}, {64, 64, 120} }
12325 };
12326
12327 /* Derived configuration for each write queue for each max request size. */
12328 static const struct arb_line write_arb_data[NUM_WR_Q][MAX_WR_ORD + 1] = {
12329 /* 1 */ { {4, 6,  3},  {4,  6,  3},  {4,  6,  3} },
12330         { {4, 2,  3},  {4,  2,  3},  {4,  2,  3} },
12331         { {8, 2,  6},  {16, 2,  11}, {16, 2,  11} },
12332         { {8, 2,  6},  {16, 2,  11}, {32, 2,  21} },
12333         { {8, 2,  6},  {16, 2,  11}, {32, 2,  21} },
12334         { {8, 2,  6},  {16, 2,  11}, {32, 2,  21} },
12335         { {8, 64, 25}, {16, 64, 25}, {32, 64, 25} },
12336         { {8, 2,  6},  {16, 2,  11}, {16, 2,  11} },
12337         { {8, 2,  6},  {16, 2,  11}, {16, 2,  11} },
12338 /* 10 */{ {8, 9,  6},  {16, 9,  11}, {32, 9,  21} },
12339         { {8, 47, 19}, {16, 47, 19}, {32, 47, 21} },
12340         { {8, 9,  6},  {16, 9,  11}, {16, 9,  11} },
12341         { {8, 64, 25}, {16, 64, 41}, {32, 64, 81} }
12342 };
12343
12344 /* Register addresses for read queues. */
12345 static const struct arb_line read_arb_addr[NUM_RD_Q-1] = {
12346 /* 1 */ {PXP2_REG_RQ_BW_RD_L0, PXP2_REG_RQ_BW_RD_ADD0,
12347             PXP2_REG_RQ_BW_RD_UBOUND0},
12348         {PXP2_REG_PSWRQ_BW_L1, PXP2_REG_PSWRQ_BW_ADD1,
12349             PXP2_REG_PSWRQ_BW_UB1},
12350         {PXP2_REG_PSWRQ_BW_L2, PXP2_REG_PSWRQ_BW_ADD2,
12351             PXP2_REG_PSWRQ_BW_UB2},
12352         {PXP2_REG_PSWRQ_BW_L3, PXP2_REG_PSWRQ_BW_ADD3,
12353             PXP2_REG_PSWRQ_BW_UB3},
12354         {PXP2_REG_RQ_BW_RD_L4, PXP2_REG_RQ_BW_RD_ADD4,
12355             PXP2_REG_RQ_BW_RD_UBOUND4},
12356         {PXP2_REG_RQ_BW_RD_L5, PXP2_REG_RQ_BW_RD_ADD5,
12357             PXP2_REG_RQ_BW_RD_UBOUND5},
12358         {PXP2_REG_PSWRQ_BW_L6, PXP2_REG_PSWRQ_BW_ADD6,
12359             PXP2_REG_PSWRQ_BW_UB6},
12360         {PXP2_REG_PSWRQ_BW_L7, PXP2_REG_PSWRQ_BW_ADD7,
12361             PXP2_REG_PSWRQ_BW_UB7},
12362         {PXP2_REG_PSWRQ_BW_L8, PXP2_REG_PSWRQ_BW_ADD8,
12363             PXP2_REG_PSWRQ_BW_UB8},
12364 /* 10 */{PXP2_REG_PSWRQ_BW_L9, PXP2_REG_PSWRQ_BW_ADD9,
12365             PXP2_REG_PSWRQ_BW_UB9},
12366         {PXP2_REG_PSWRQ_BW_L10, PXP2_REG_PSWRQ_BW_ADD10,
12367             PXP2_REG_PSWRQ_BW_UB10},
12368         {PXP2_REG_PSWRQ_BW_L11, PXP2_REG_PSWRQ_BW_ADD11,
12369             PXP2_REG_PSWRQ_BW_UB11},
12370         {PXP2_REG_RQ_BW_RD_L12, PXP2_REG_RQ_BW_RD_ADD12,
12371             PXP2_REG_RQ_BW_RD_UBOUND12},
12372         {PXP2_REG_RQ_BW_RD_L13, PXP2_REG_RQ_BW_RD_ADD13,
12373             PXP2_REG_RQ_BW_RD_UBOUND13},
12374         {PXP2_REG_RQ_BW_RD_L14, PXP2_REG_RQ_BW_RD_ADD14,
12375             PXP2_REG_RQ_BW_RD_UBOUND14},
12376         {PXP2_REG_RQ_BW_RD_L15, PXP2_REG_RQ_BW_RD_ADD15,
12377             PXP2_REG_RQ_BW_RD_UBOUND15},
12378         {PXP2_REG_RQ_BW_RD_L16, PXP2_REG_RQ_BW_RD_ADD16,
12379             PXP2_REG_RQ_BW_RD_UBOUND16},
12380         {PXP2_REG_RQ_BW_RD_L17, PXP2_REG_RQ_BW_RD_ADD17,
12381             PXP2_REG_RQ_BW_RD_UBOUND17},
12382         {PXP2_REG_RQ_BW_RD_L18, PXP2_REG_RQ_BW_RD_ADD18,
12383             PXP2_REG_RQ_BW_RD_UBOUND18},
12384 /* 20 */{PXP2_REG_RQ_BW_RD_L19, PXP2_REG_RQ_BW_RD_ADD19,
12385             PXP2_REG_RQ_BW_RD_UBOUND19},
12386         {PXP2_REG_RQ_BW_RD_L20, PXP2_REG_RQ_BW_RD_ADD20,
12387             PXP2_REG_RQ_BW_RD_UBOUND20},
12388         {PXP2_REG_RQ_BW_RD_L22, PXP2_REG_RQ_BW_RD_ADD22,
12389             PXP2_REG_RQ_BW_RD_UBOUND22},
12390         {PXP2_REG_RQ_BW_RD_L23, PXP2_REG_RQ_BW_RD_ADD23,
12391             PXP2_REG_RQ_BW_RD_UBOUND23},
12392         {PXP2_REG_RQ_BW_RD_L24, PXP2_REG_RQ_BW_RD_ADD24,
12393             PXP2_REG_RQ_BW_RD_UBOUND24},
12394         {PXP2_REG_RQ_BW_RD_L25, PXP2_REG_RQ_BW_RD_ADD25,
12395             PXP2_REG_RQ_BW_RD_UBOUND25},
12396         {PXP2_REG_RQ_BW_RD_L26, PXP2_REG_RQ_BW_RD_ADD26,
12397             PXP2_REG_RQ_BW_RD_UBOUND26},
12398         {PXP2_REG_RQ_BW_RD_L27, PXP2_REG_RQ_BW_RD_ADD27,
12399             PXP2_REG_RQ_BW_RD_UBOUND27},
12400         {PXP2_REG_PSWRQ_BW_L28, PXP2_REG_PSWRQ_BW_ADD28,
12401             PXP2_REG_PSWRQ_BW_UB28}
12402 };
12403
12404 /* Register addresses for write queues. */
12405 static const struct arb_line write_arb_addr[NUM_WR_Q-1] = {
12406 /* 1 */ {PXP2_REG_PSWRQ_BW_L1, PXP2_REG_PSWRQ_BW_ADD1,
12407             PXP2_REG_PSWRQ_BW_UB1},
12408         {PXP2_REG_PSWRQ_BW_L2, PXP2_REG_PSWRQ_BW_ADD2,
12409             PXP2_REG_PSWRQ_BW_UB2},
12410         {PXP2_REG_PSWRQ_BW_L3, PXP2_REG_PSWRQ_BW_ADD3,
12411             PXP2_REG_PSWRQ_BW_UB3},
12412         {PXP2_REG_PSWRQ_BW_L6, PXP2_REG_PSWRQ_BW_ADD6,
12413             PXP2_REG_PSWRQ_BW_UB6},
12414         {PXP2_REG_PSWRQ_BW_L7, PXP2_REG_PSWRQ_BW_ADD7,
12415             PXP2_REG_PSWRQ_BW_UB7},
12416         {PXP2_REG_PSWRQ_BW_L8, PXP2_REG_PSWRQ_BW_ADD8,
12417             PXP2_REG_PSWRQ_BW_UB8},
12418         {PXP2_REG_PSWRQ_BW_L9, PXP2_REG_PSWRQ_BW_ADD9,
12419             PXP2_REG_PSWRQ_BW_UB9},
12420         {PXP2_REG_PSWRQ_BW_L10, PXP2_REG_PSWRQ_BW_ADD10,
12421             PXP2_REG_PSWRQ_BW_UB10},
12422         {PXP2_REG_PSWRQ_BW_L11, PXP2_REG_PSWRQ_BW_ADD11,
12423             PXP2_REG_PSWRQ_BW_UB11},
12424 /* 10 */{PXP2_REG_PSWRQ_BW_L28, PXP2_REG_PSWRQ_BW_ADD28,
12425             PXP2_REG_PSWRQ_BW_UB28},
12426         {PXP2_REG_RQ_BW_WR_L29, PXP2_REG_RQ_BW_WR_ADD29,
12427             PXP2_REG_RQ_BW_WR_UBOUND29},
12428         {PXP2_REG_RQ_BW_WR_L30, PXP2_REG_RQ_BW_WR_ADD30,
12429             PXP2_REG_RQ_BW_WR_UBOUND30}
12430 };
12431
12432 static void
12433 bxe_init_pxp_arb(struct bxe_softc *sc, int r_order, int w_order)
12434 {
12435         uint32_t val, i;
12436
12437         if (r_order > MAX_RD_ORD) {
12438                 DBPRINT(sc, (BXE_VERBOSE_LOAD | BXE_VERBOSE_RESET),
12439                     "%s(): Read order of %d order adjusted to %d\n",
12440                     __FUNCTION__,  r_order, MAX_RD_ORD);
12441                 r_order = MAX_RD_ORD;
12442         }
12443         if (w_order > MAX_WR_ORD) {
12444                 DBPRINT(sc, (BXE_VERBOSE_LOAD | BXE_VERBOSE_RESET),
12445                     "%s(): Write order of %d order adjusted to %d\n",
12446                     __FUNCTION__, w_order, MAX_WR_ORD);
12447                 w_order = MAX_WR_ORD;
12448         }
12449
12450         DBPRINT(sc, (BXE_VERBOSE_LOAD | BXE_VERBOSE_RESET),
12451             "%s(): Read order %d, write order %d\n",
12452             __FUNCTION__, r_order, w_order);
12453
12454         for (i = 0; i < NUM_RD_Q - 1; i++) {
12455                 REG_WR(sc, read_arb_addr[i].l,
12456                     read_arb_data[i][r_order].l);
12457                 REG_WR(sc, read_arb_addr[i].add,
12458                     read_arb_data[i][r_order].add);
12459                 REG_WR(sc, read_arb_addr[i].ubound,
12460                     read_arb_data[i][r_order].ubound);
12461         }
12462
12463         for (i = 0; i < NUM_WR_Q - 1; i++) {
12464                 if ((write_arb_addr[i].l == PXP2_REG_RQ_BW_WR_L29) ||
12465                     (write_arb_addr[i].l == PXP2_REG_RQ_BW_WR_L30)) {
12466
12467                         REG_WR(sc, write_arb_addr[i].l,
12468                             write_arb_data[i][w_order].l);
12469
12470                         REG_WR(sc, write_arb_addr[i].add,
12471                             write_arb_data[i][w_order].add);
12472
12473                         REG_WR(sc, write_arb_addr[i].ubound,
12474                             write_arb_data[i][w_order].ubound);
12475                 } else {
12476
12477                         val = REG_RD(sc, write_arb_addr[i].l);
12478                         REG_WR(sc, write_arb_addr[i].l, val |
12479                             (write_arb_data[i][w_order].l << 10));
12480
12481                         val = REG_RD(sc, write_arb_addr[i].add);
12482                         REG_WR(sc, write_arb_addr[i].add, val |
12483                             (write_arb_data[i][w_order].add << 10));
12484
12485                         val = REG_RD(sc, write_arb_addr[i].ubound);
12486                         REG_WR(sc, write_arb_addr[i].ubound, val |
12487                             (write_arb_data[i][w_order].ubound << 7));
12488                 }
12489         }
12490
12491         val =  write_arb_data[NUM_WR_Q - 1][w_order].add;
12492         val += write_arb_data[NUM_WR_Q - 1][w_order].ubound << 10;
12493         val += write_arb_data[NUM_WR_Q - 1][w_order].l << 17;
12494         REG_WR(sc, PXP2_REG_PSWRQ_BW_RD, val);
12495
12496         val =  read_arb_data[NUM_RD_Q - 1][r_order].add;
12497         val += read_arb_data[NUM_RD_Q - 1][r_order].ubound << 10;
12498         val += read_arb_data[NUM_RD_Q - 1][r_order].l << 17;
12499         REG_WR(sc, PXP2_REG_PSWRQ_BW_WR, val);
12500
12501         REG_WR(sc, PXP2_REG_RQ_WR_MBS0, w_order);
12502         REG_WR(sc, PXP2_REG_RQ_WR_MBS1, w_order);
12503         REG_WR(sc, PXP2_REG_RQ_RD_MBS0, r_order);
12504         REG_WR(sc, PXP2_REG_RQ_RD_MBS1, r_order);
12505
12506         if (r_order == MAX_RD_ORD)
12507                 REG_WR(sc, PXP2_REG_RQ_PDR_LIMIT, 0xe00);
12508
12509         REG_WR(sc, PXP2_REG_WR_USDMDP_TH, (0x18 << w_order));
12510
12511         if (CHIP_IS_E1H(sc)) {
12512                 /*    MPS      w_order     optimal TH      presently TH
12513                  *    128         0             0               2
12514                  *    256         1             1               3
12515                  *    >=512       2             2               3
12516                  */
12517                 val = ((w_order == 0) ? 2 : 3);
12518                 REG_WR(sc, PXP2_REG_WR_HC_MPS, val);
12519                 REG_WR(sc, PXP2_REG_WR_USDM_MPS, val);
12520                 REG_WR(sc, PXP2_REG_WR_CSDM_MPS, val);
12521                 REG_WR(sc, PXP2_REG_WR_TSDM_MPS, val);
12522                 REG_WR(sc, PXP2_REG_WR_XSDM_MPS, val);
12523                 REG_WR(sc, PXP2_REG_WR_QM_MPS, val);
12524                 REG_WR(sc, PXP2_REG_WR_TM_MPS, val);
12525                 REG_WR(sc, PXP2_REG_WR_SRC_MPS, val);
12526                 REG_WR(sc, PXP2_REG_WR_DBG_MPS, val);
12527                 REG_WR(sc, PXP2_REG_WR_DMAE_MPS, 2); /* DMAE is special */
12528                 REG_WR(sc, PXP2_REG_WR_CDU_MPS, val);
12529         }
12530 }
12531
12532 static void
12533 bxe_init_pxp(struct bxe_softc *sc)
12534 {
12535         uint16_t devctl;
12536         int r_order, w_order;
12537
12538         devctl = pci_read_config(sc->dev,
12539             sc->pcie_cap + PCI_EXP_DEVCTL, 2);
12540         DBPRINT(sc, (BXE_VERBOSE_LOAD | BXE_VERBOSE_RESET),
12541             "%s(): Read 0x%x from devctl\n", __FUNCTION__, devctl);
12542         w_order = ((devctl & PCI_EXP_DEVCTL_PAYLOAD) >> 5);
12543         if (sc->mrrs == -1)
12544                 r_order = ((devctl & PCI_EXP_DEVCTL_READRQ) >> 12);
12545         else {
12546                 DBPRINT(sc, (BXE_VERBOSE_LOAD | BXE_VERBOSE_RESET),
12547                     "%s(): Force MRRS read order to %d\n",
12548                     __FUNCTION__, sc->mrrs);
12549                 r_order = sc->mrrs;
12550         }
12551
12552         bxe_init_pxp_arb(sc, r_order, w_order);
12553 }
12554
12555 static void
12556 bxe_setup_fan_failure_detection(struct bxe_softc *sc)
12557 {
12558         uint32_t phy_type, val;
12559         int is_required, port;
12560
12561         is_required = 0;
12562         if (NOMCP(sc))
12563                 return;
12564
12565         val = SHMEM_RD(sc, dev_info.shared_hw_config.config2) &
12566             SHARED_HW_CFG_FAN_FAILURE_MASK;
12567
12568         if (val == SHARED_HW_CFG_FAN_FAILURE_ENABLED)
12569                 is_required = 1;
12570
12571         /*
12572          * The fan failure mechanism is usually related to the PHY type since
12573          * the power consumption of the board is affected by the PHY. Currently,
12574          * fan is required for most designs with SFX7101, BCM8727 and BCM8481.
12575          */
12576         else if (val == SHARED_HW_CFG_FAN_FAILURE_PHY_TYPE)
12577                 for (port = PORT_0; port < PORT_MAX; port++) {
12578                         phy_type = SHMEM_RD(sc,
12579                             dev_info.port_hw_config[port].external_phy_config) &
12580                             PORT_HW_CFG_XGXS_EXT_PHY_TYPE_MASK;
12581                         is_required |=
12582                         ((phy_type == PORT_HW_CFG_XGXS_EXT_PHY_TYPE_SFX7101) ||
12583                          (phy_type == PORT_HW_CFG_XGXS_EXT_PHY_TYPE_BCM8727) ||
12584                          (phy_type == PORT_HW_CFG_XGXS_EXT_PHY_TYPE_BCM8481));
12585                 }
12586
12587         if (is_required == 0)
12588                 return;
12589
12590         /* Fan failure is indicated by SPIO 5. */
12591         bxe_set_spio(sc, MISC_REGISTERS_SPIO_5, MISC_REGISTERS_SPIO_INPUT_HI_Z);
12592
12593         /* Set to active low mode. */
12594         val = REG_RD(sc, MISC_REG_SPIO_INT);
12595         val |= ((1 << MISC_REGISTERS_SPIO_5) <<
12596                                         MISC_REGISTERS_SPIO_INT_OLD_SET_POS);
12597         REG_WR(sc, MISC_REG_SPIO_INT, val);
12598
12599         /* Enable interrupt to signal the IGU. */
12600         val = REG_RD(sc, MISC_REG_SPIO_EVENT_EN);
12601         val |= (1 << MISC_REGISTERS_SPIO_5);
12602         REG_WR(sc, MISC_REG_SPIO_EVENT_EN, val);
12603 }
12604
12605 /*
12606  * Common initialization.
12607  *
12608  * Returns:
12609  *   0 = Success, !0 = Failure.
12610  */
12611 static int
12612 bxe_init_common(struct bxe_softc *sc)
12613 {
12614         uint32_t val;
12615         int i, rc;
12616
12617         rc = 0;
12618         DBENTER(BXE_VERBOSE_LOAD | BXE_VERBOSE_RESET);
12619
12620         /* Reset all blocks within the chip except the BMAC. */
12621         bxe_reset_common(sc);
12622         DELAY(30000);
12623         REG_WR(sc, GRCBASE_MISC + MISC_REGISTERS_RESET_REG_1_SET, 0xffffffff);
12624         REG_WR(sc, GRCBASE_MISC + MISC_REGISTERS_RESET_REG_2_SET, 0xfffc);
12625         DELAY(30000);
12626
12627         bxe_init_block(sc, MISC_BLOCK, COMMON_STAGE);
12628         if (CHIP_IS_E1H(sc))
12629                 REG_WR(sc, MISC_REG_E1HMF_MODE, IS_E1HMF(sc));
12630
12631         REG_WR(sc, MISC_REG_LCPLL_CTRL_REG_2, 0x100);
12632         DELAY(30000);
12633         REG_WR(sc, MISC_REG_LCPLL_CTRL_REG_2, 0x0);
12634
12635         bxe_init_block(sc, PXP_BLOCK, COMMON_STAGE);
12636         if (CHIP_IS_E1(sc)) {
12637                 /*
12638                  * Enable HW interrupt from PXP on USDM overflow
12639                  * bit 16 on INT_MASK_0.
12640                  */
12641                 REG_WR(sc, PXP_REG_PXP_INT_MASK_0, 0);
12642         }
12643
12644         bxe_init_block(sc, PXP2_BLOCK, COMMON_STAGE);
12645         bxe_init_pxp(sc);
12646
12647 #ifdef __BIG_ENDIAN
12648         REG_WR(sc, PXP2_REG_RQ_QM_ENDIAN_M, 1);
12649         REG_WR(sc, PXP2_REG_RQ_TM_ENDIAN_M, 1);
12650         REG_WR(sc, PXP2_REG_RQ_SRC_ENDIAN_M, 1);
12651         REG_WR(sc, PXP2_REG_RQ_CDU_ENDIAN_M, 1);
12652         REG_WR(sc, PXP2_REG_RQ_DBG_ENDIAN_M, 1);
12653         /* Make sure this value is 0. */
12654         REG_WR(sc, PXP2_REG_RQ_HC_ENDIAN_M, 0);
12655
12656         REG_WR(sc, PXP2_REG_RD_QM_SWAP_MODE, 1);
12657         REG_WR(sc, PXP2_REG_RD_TM_SWAP_MODE, 1);
12658         REG_WR(sc, PXP2_REG_RD_SRC_SWAP_MODE, 1);
12659         REG_WR(sc, PXP2_REG_RD_CDURD_SWAP_MODE, 1);
12660 #endif
12661
12662         REG_WR(sc, PXP2_REG_RQ_CDU_P_SIZE, 2);
12663
12664         /* Let the HW do it's magic ... */
12665         DELAY(100000);
12666         /* Finish the PXP initialization. */
12667         val = REG_RD(sc, PXP2_REG_RQ_CFG_DONE);
12668         if (val != 1) {
12669                 BXE_PRINTF("%s(%d): PXP2 CFG failed!\n", __FILE__, __LINE__);
12670                 rc = EBUSY;
12671                 goto bxe_init_common_exit;
12672         }
12673
12674         val = REG_RD(sc, PXP2_REG_RD_INIT_DONE);
12675         if (val != 1) {
12676                 BXE_PRINTF("%s(%d): PXP2 RD_INIT failed!\n", __FILE__,
12677                     __LINE__);
12678                 rc = EBUSY;
12679                 goto bxe_init_common_exit;
12680         }
12681
12682         REG_WR(sc, PXP2_REG_RQ_DISABLE_INPUTS, 0);
12683         REG_WR(sc, PXP2_REG_RD_DISABLE_INPUTS, 0);
12684
12685         bxe_init_block(sc, DMAE_BLOCK, COMMON_STAGE);
12686
12687         sc->dmae_ready = 1;
12688         bxe_init_fill(sc, TSEM_REG_PRAM, 0, 8);
12689
12690         bxe_init_block(sc, TCM_BLOCK, COMMON_STAGE);
12691         bxe_init_block(sc, UCM_BLOCK, COMMON_STAGE);
12692         bxe_init_block(sc, CCM_BLOCK, COMMON_STAGE);
12693         bxe_init_block(sc, XCM_BLOCK, COMMON_STAGE);
12694
12695         bxe_read_dmae(sc, XSEM_REG_PASSIVE_BUFFER, 3);
12696         bxe_read_dmae(sc, CSEM_REG_PASSIVE_BUFFER, 3);
12697         bxe_read_dmae(sc, TSEM_REG_PASSIVE_BUFFER, 3);
12698         bxe_read_dmae(sc, USEM_REG_PASSIVE_BUFFER, 3);
12699
12700         bxe_init_block(sc, QM_BLOCK, COMMON_STAGE);
12701
12702         /* Soft reset pulse. */
12703         REG_WR(sc, QM_REG_SOFT_RESET, 1);
12704         REG_WR(sc, QM_REG_SOFT_RESET, 0);
12705
12706         bxe_init_block(sc, DQ_BLOCK, COMMON_STAGE);
12707         REG_WR(sc, DORQ_REG_DPM_CID_OFST, BCM_PAGE_SHIFT);
12708
12709         REG_WR(sc, DORQ_REG_DORQ_INT_MASK, 0);
12710
12711         bxe_init_block(sc, BRB1_BLOCK, COMMON_STAGE);
12712         bxe_init_block(sc, PRS_BLOCK, COMMON_STAGE);
12713         REG_WR(sc, PRS_REG_A_PRSU_20, 0xf);
12714
12715         if (CHIP_IS_E1H(sc))
12716                 REG_WR(sc, PRS_REG_E1HOV_MODE, IS_E1HMF(sc));
12717
12718         bxe_init_block(sc, TSDM_BLOCK, COMMON_STAGE);
12719         bxe_init_block(sc, CSDM_BLOCK, COMMON_STAGE);
12720         bxe_init_block(sc, USDM_BLOCK, COMMON_STAGE);
12721         bxe_init_block(sc, XSDM_BLOCK, COMMON_STAGE);
12722         /* Clear STORM processor memory. */
12723         bxe_init_fill(sc, TSEM_REG_FAST_MEMORY, 0, STORM_INTMEM_SIZE(sc));
12724         bxe_init_fill(sc, USEM_REG_FAST_MEMORY, 0, STORM_INTMEM_SIZE(sc));
12725         bxe_init_fill(sc, CSEM_REG_FAST_MEMORY, 0, STORM_INTMEM_SIZE(sc));
12726         bxe_init_fill(sc, XSEM_REG_FAST_MEMORY, 0, STORM_INTMEM_SIZE(sc));
12727
12728         bxe_init_block(sc, TSEM_BLOCK, COMMON_STAGE);
12729         bxe_init_block(sc, USEM_BLOCK, COMMON_STAGE);
12730         bxe_init_block(sc, CSEM_BLOCK, COMMON_STAGE);
12731         bxe_init_block(sc, XSEM_BLOCK, COMMON_STAGE);
12732
12733         /* Sync semi rtc. */
12734         REG_WR(sc, GRCBASE_MISC + MISC_REGISTERS_RESET_REG_1_CLEAR, 0x80000000);
12735         REG_WR(sc, GRCBASE_MISC + MISC_REGISTERS_RESET_REG_1_SET, 0x80000000);
12736
12737         bxe_init_block(sc, UPB_BLOCK, COMMON_STAGE);
12738         bxe_init_block(sc, XPB_BLOCK, COMMON_STAGE);
12739         bxe_init_block(sc, PBF_BLOCK, COMMON_STAGE);
12740
12741         REG_WR(sc, SRC_REG_SOFT_RST, 1);
12742         /* Setup RSS/multi-queue hasking keys. */
12743         for (i = SRC_REG_KEYRSS0_0; i <= SRC_REG_KEYRSS1_9; i += 4)
12744                 REG_WR(sc, i, 0xc0cac01a);
12745
12746         bxe_init_block(sc, SRCH_BLOCK, COMMON_STAGE);
12747
12748         REG_WR(sc, SRC_REG_SOFT_RST, 0);
12749
12750         /* Make sure the cdu_context structure has the right size. */
12751         if (sizeof(union cdu_context) != 1024) {
12752                 BXE_PRINTF("%s(%d): Invalid size for context (%ld != 1024)!\n",
12753                     __FILE__, __LINE__, (long)sizeof(union cdu_context));
12754                 rc = EBUSY;
12755                 goto bxe_init_common_exit;
12756         }
12757
12758         bxe_init_block(sc, CDU_BLOCK, COMMON_STAGE);
12759
12760         /*
12761          * val = (num_context_in_page << 24) +
12762          * (context_waste_size << 12) +
12763          * context_line_size.
12764          */
12765
12766         val = (4 << 24) + (0 << 12) + 1024;
12767         REG_WR(sc, CDU_REG_CDU_GLOBAL_PARAMS, val);
12768
12769         bxe_init_block(sc, CFC_BLOCK, COMMON_STAGE);
12770         REG_WR(sc, CFC_REG_INIT_REG, 0x7FF);
12771         /* Enable context validation interrupt from CFC. */
12772         REG_WR(sc, CFC_REG_CFC_INT_MASK, 0);
12773
12774         /* Set the thresholds to prevent CFC/CDU race. */
12775         REG_WR(sc, CFC_REG_DEBUG0, 0x20020000);
12776
12777         bxe_init_block(sc, HC_BLOCK, COMMON_STAGE);
12778         bxe_init_block(sc, MISC_AEU_BLOCK, COMMON_STAGE);
12779
12780         bxe_init_block(sc, PXPCS_BLOCK, COMMON_STAGE);
12781         /* Clear PCIe block debug status bits. */
12782         REG_WR(sc, 0x2814, 0xffffffff);
12783         REG_WR(sc, 0x3820, 0xffffffff);
12784
12785         bxe_init_block(sc, EMAC0_BLOCK, COMMON_STAGE);
12786         bxe_init_block(sc, EMAC1_BLOCK, COMMON_STAGE);
12787         bxe_init_block(sc, DBU_BLOCK, COMMON_STAGE);
12788         bxe_init_block(sc, DBG_BLOCK, COMMON_STAGE);
12789
12790         bxe_init_block(sc, NIG_BLOCK, COMMON_STAGE);
12791         if (CHIP_IS_E1H(sc)) {
12792                 REG_WR(sc, NIG_REG_LLH_MF_MODE, IS_E1HMF(sc));
12793                 REG_WR(sc, NIG_REG_LLH_E1HOV_MODE, IS_E1HOV(sc));
12794         }
12795
12796         /* Finish CFC initialization. */
12797         val = bxe_reg_poll(sc, CFC_REG_LL_INIT_DONE, 1, 100, 10);
12798         if (val != 1) {
12799                 BXE_PRINTF("%s(%d): CFC LL_INIT failed!\n",
12800                     __FILE__, __LINE__);
12801                 rc = EBUSY;
12802                 goto bxe_init_common_exit;
12803         }
12804
12805         val = bxe_reg_poll(sc, CFC_REG_AC_INIT_DONE, 1, 100, 10);
12806         if (val != 1) {
12807                 BXE_PRINTF("%s(%d): CFC AC_INIT failed!\n",
12808                      __FILE__, __LINE__);
12809                 rc = EBUSY;
12810                 goto bxe_init_common_exit;
12811         }
12812
12813         val = bxe_reg_poll(sc, CFC_REG_CAM_INIT_DONE, 1, 100, 10);
12814         if (val != 1) {
12815                 BXE_PRINTF("%s(%d): CFC CAM_INIT failed!\n",
12816                     __FILE__, __LINE__);
12817                 rc = EBUSY;
12818                 goto bxe_init_common_exit;
12819         }
12820
12821         REG_WR(sc, CFC_REG_DEBUG0, 0);
12822
12823         /* Read NIG statistic and check for first load since powerup. */
12824         bxe_read_dmae(sc, NIG_REG_STAT2_BRB_OCTET, 2);
12825         val = *BXE_SP(sc, wb_data[0]);
12826
12827         /* Do internal memory self test only after a full power cycle. */
12828         if ((CHIP_IS_E1(sc)) && (val == 0) && bxe_int_mem_test(sc)) {
12829                 BXE_PRINTF("%s(%d): Internal memory self-test failed!\n",
12830                     __FILE__, __LINE__);
12831                 rc = EBUSY;
12832                 goto bxe_init_common_exit;
12833         }
12834
12835         /* Handle any board specific initialization. */
12836         switch (XGXS_EXT_PHY_TYPE(sc->link_params.ext_phy_config)) {
12837         case PORT_HW_CFG_XGXS_EXT_PHY_TYPE_BCM8072:
12838         case PORT_HW_CFG_XGXS_EXT_PHY_TYPE_BCM8073:
12839         case PORT_HW_CFG_XGXS_EXT_PHY_TYPE_BCM8726:
12840         case PORT_HW_CFG_XGXS_EXT_PHY_TYPE_BCM8727:
12841                 break;
12842
12843         default:
12844                 break;
12845         }
12846
12847         bxe_setup_fan_failure_detection(sc);
12848
12849         /* Clear PXP2 attentions. */
12850         REG_RD(sc, PXP2_REG_PXP2_INT_STS_CLR_0);
12851
12852         bxe_enable_blocks_attention(sc);
12853
12854         if (!NOMCP(sc)) {
12855                 bxe_acquire_phy_lock(sc);
12856                 bxe_common_init_phy(sc, sc->common.shmem_base);
12857                 bxe_release_phy_lock(sc);
12858         } else
12859                 BXE_PRINTF(
12860                     "%s(%d): Bootcode is missing - cannot initialize PHY!\n",
12861                     __FILE__, __LINE__);
12862
12863 bxe_init_common_exit:
12864         DBEXIT(BXE_VERBOSE_LOAD | BXE_VERBOSE_RESET);
12865         return (rc);
12866 }
12867
12868 /*
12869  * Port initialization.
12870  *
12871  * Returns:
12872  *   0 = Success, !0 = Failure.
12873  */
12874 static int
12875 bxe_init_port(struct bxe_softc *sc)
12876 {
12877         uint32_t val, low, high;
12878         uint32_t swap_val, swap_override, aeu_gpio_mask, offset;
12879         uint32_t reg_addr;
12880         int init_stage, port;
12881
12882         port = BP_PORT(sc);
12883         init_stage = port ? PORT1_STAGE : PORT0_STAGE;
12884
12885         DBENTER(BXE_VERBOSE_LOAD | BXE_VERBOSE_RESET);
12886
12887         DBPRINT(sc, (BXE_VERBOSE_LOAD | BXE_VERBOSE_RESET),
12888             "%s(): Initializing port %d.\n", __FUNCTION__, port);
12889
12890         REG_WR(sc, NIG_REG_MASK_INTERRUPT_PORT0 + port * 4, 0);
12891
12892         bxe_init_block(sc, PXP_BLOCK, init_stage);
12893         bxe_init_block(sc, PXP2_BLOCK, init_stage);
12894
12895         bxe_init_block(sc, TCM_BLOCK, init_stage);
12896         bxe_init_block(sc, UCM_BLOCK, init_stage);
12897         bxe_init_block(sc, CCM_BLOCK, init_stage);
12898         bxe_init_block(sc, XCM_BLOCK, init_stage);
12899
12900         bxe_init_block(sc, DQ_BLOCK, init_stage);
12901
12902         bxe_init_block(sc, BRB1_BLOCK, init_stage);
12903
12904         /* Determine the pause threshold for the BRB */
12905         if (IS_E1HMF(sc))
12906                 low = (sc->bxe_flags & BXE_ONE_PORT_FLAG) ? 160 : 246;
12907         else if (sc->bxe_ifp->if_mtu > 4096) {
12908                 if (sc->bxe_flags & BXE_ONE_PORT_FLAG)
12909                         low = 160;
12910                 else {
12911                         val = sc->bxe_ifp->if_mtu;
12912                         /* (24*1024 + val*4)/256 */
12913                         low = 96 + (val/64) + ((val % 64) ? 1 : 0);
12914                 }
12915         } else
12916                 low = (sc->bxe_flags & BXE_ONE_PORT_FLAG) ? 80 : 160;
12917         high = low + 56;        /* 14 * 1024 / 256 */
12918
12919         REG_WR(sc, BRB1_REG_PAUSE_LOW_THRESHOLD_0 + port * 4, low);
12920         REG_WR(sc, BRB1_REG_PAUSE_HIGH_THRESHOLD_0 + port * 4, high);
12921
12922         /* Port PRS comes here. */
12923         bxe_init_block(sc, PRS_BLOCK, init_stage);
12924
12925         bxe_init_block(sc, TSDM_BLOCK, init_stage);
12926         bxe_init_block(sc, CSDM_BLOCK, init_stage);
12927         bxe_init_block(sc, USDM_BLOCK, init_stage);
12928         bxe_init_block(sc, XSDM_BLOCK, init_stage);
12929
12930         bxe_init_block(sc, TSEM_BLOCK, init_stage);
12931         bxe_init_block(sc, USEM_BLOCK, init_stage);
12932         bxe_init_block(sc, CSEM_BLOCK, init_stage);
12933         bxe_init_block(sc, XSEM_BLOCK, init_stage);
12934
12935         bxe_init_block(sc, UPB_BLOCK, init_stage);
12936         bxe_init_block(sc, XPB_BLOCK, init_stage);
12937
12938         bxe_init_block(sc, PBF_BLOCK, init_stage);
12939
12940         /* Configure PBF to work without pause for MTU = 9000. */
12941         REG_WR(sc, PBF_REG_P0_PAUSE_ENABLE + port * 4, 0);
12942
12943         /* Update threshold. */
12944         REG_WR(sc, PBF_REG_P0_ARB_THRSH + port * 4, (9040/16));
12945         /* Update initial credit. */
12946         REG_WR(sc, PBF_REG_P0_INIT_CRD + port * 4, (9040/16) + 553 - 22);
12947
12948         /* Probe changes. */
12949         REG_WR(sc, PBF_REG_INIT_P0 + port * 4, 1);
12950         DELAY(5000);
12951         REG_WR(sc, PBF_REG_INIT_P0 + port * 4, 0);
12952
12953         bxe_init_block(sc, CDU_BLOCK, init_stage);
12954         bxe_init_block(sc, CFC_BLOCK, init_stage);
12955
12956         if (CHIP_IS_E1(sc)) {
12957                 REG_WR(sc, HC_REG_LEADING_EDGE_0 + port * 8, 0);
12958                 REG_WR(sc, HC_REG_TRAILING_EDGE_0 + port * 8, 0);
12959         }
12960
12961         bxe_init_block(sc, HC_BLOCK, init_stage);
12962
12963         bxe_init_block(sc, MISC_AEU_BLOCK, init_stage);
12964         /*
12965          * init aeu_mask_attn_func_0/1:
12966          *  - SF mode: bits 3-7 are masked. only bits 0-2 are in use
12967          *  - MF mode: bit 3 is masked. bits 0-2 are in use as in SF
12968          *             bits 4-7 are used for "per vn group attention"
12969          */
12970         REG_WR(sc, MISC_REG_AEU_MASK_ATTN_FUNC_0 + port * 4,
12971             (IS_E1HMF(sc) ? 0xF7 : 0x7));
12972
12973         bxe_init_block(sc, PXPCS_BLOCK, init_stage);
12974         bxe_init_block(sc, EMAC0_BLOCK, init_stage);
12975         bxe_init_block(sc, EMAC1_BLOCK, init_stage);
12976         bxe_init_block(sc, DBU_BLOCK, init_stage);
12977         bxe_init_block(sc, DBG_BLOCK, init_stage);
12978
12979         bxe_init_block(sc, NIG_BLOCK, init_stage);
12980
12981         REG_WR(sc, NIG_REG_XGXS_SERDES0_MODE_SEL + port * 4, 1);
12982
12983         if (CHIP_IS_E1H(sc)) {
12984                 /* Enable outer VLAN support if required. */
12985                 REG_WR(sc, NIG_REG_LLH0_BRB1_DRV_MASK_MF + port * 4,
12986                     (IS_E1HOV(sc) ? 0x1 : 0x2));
12987         }
12988
12989         REG_WR(sc, NIG_REG_LLFC_ENABLE_0 + port * 4, 0);
12990         REG_WR(sc, NIG_REG_LLFC_OUT_EN_0 + port * 4, 0);
12991         REG_WR(sc, NIG_REG_PAUSE_ENABLE_0 + port * 4, 1);
12992
12993         bxe_init_block(sc, MCP_BLOCK, init_stage);
12994         bxe_init_block(sc, DMAE_BLOCK, init_stage);
12995
12996         switch (XGXS_EXT_PHY_TYPE(sc->link_params.ext_phy_config)) {
12997         case PORT_HW_CFG_XGXS_EXT_PHY_TYPE_BCM8726:
12998                 bxe_set_gpio(sc, MISC_REGISTERS_GPIO_3,
12999                     MISC_REGISTERS_GPIO_INPUT_HI_Z, port);
13000
13001                 /*
13002                  * The GPIO should be swapped if the swap register is
13003                  * set and active.
13004                  */
13005                 swap_val = REG_RD(sc, NIG_REG_PORT_SWAP);
13006                 swap_override = REG_RD(sc, NIG_REG_STRAP_OVERRIDE);
13007
13008                 /* Select function upon port-swap configuration. */
13009                 if (port == 0) {
13010                         offset = MISC_REG_AEU_ENABLE1_FUNC_0_OUT_0;
13011                         aeu_gpio_mask = (swap_val && swap_override) ?
13012                             AEU_INPUTS_ATTN_BITS_GPIO3_FUNCTION_1 :
13013                             AEU_INPUTS_ATTN_BITS_GPIO3_FUNCTION_0;
13014                 } else {
13015                         offset = MISC_REG_AEU_ENABLE1_FUNC_1_OUT_0;
13016                         aeu_gpio_mask = (swap_val && swap_override) ?
13017                             AEU_INPUTS_ATTN_BITS_GPIO3_FUNCTION_0 :
13018                             AEU_INPUTS_ATTN_BITS_GPIO3_FUNCTION_1;
13019                 }
13020                 val = REG_RD(sc, offset);
13021                 /* Add GPIO3 to group. */
13022                 val |= aeu_gpio_mask;
13023                 REG_WR(sc, offset, val);
13024                 break;
13025         case PORT_HW_CFG_XGXS_EXT_PHY_TYPE_SFX7101:
13026         case PORT_HW_CFG_XGXS_EXT_PHY_TYPE_BCM8727:
13027                 /* Add SPIO 5 to group 0. */
13028                 reg_addr = port ? MISC_REG_AEU_ENABLE1_FUNC_1_OUT_0 :
13029                     MISC_REG_AEU_ENABLE1_FUNC_0_OUT_0;
13030                 val = REG_RD(sc, reg_addr);
13031                 val |= AEU_INPUTS_ATTN_BITS_SPIO5;
13032                 REG_WR(sc, reg_addr, val);
13033                 break;
13034         default:
13035                 break;
13036         }
13037
13038         bxe__link_reset(sc);
13039
13040         DBEXIT(BXE_VERBOSE_LOAD | BXE_VERBOSE_RESET);
13041
13042         return (0);
13043 }
13044
13045 #define ILT_PER_FUNC            (768/2)
13046 #define FUNC_ILT_BASE(func)     (func * ILT_PER_FUNC)
13047 /*
13048  * The phys address is shifted right 12 bits and has an added 1=valid
13049  * bit added to the 53rd bit (bit 52) then since this is a wide
13050  * register(TM) we split it into two 32 bit writes.
13051  */
13052 #define ONCHIP_ADDR1(x)         ((uint32_t)(((uint64_t)x >> 12) & 0xFFFFFFFF))
13053 #define ONCHIP_ADDR2(x)         ((uint32_t)((1 << 20) | ((uint64_t)x >> 44)))
13054 #define PXP_ONE_ILT(x)          (((x) << 10) | x)
13055 #define PXP_ILT_RANGE(f, l)     (((l) << 10) | f)
13056 #define CNIC_ILT_LINES          0
13057
13058 /*
13059  * ILT write.
13060  *
13061  * Returns:
13062  *   None.
13063  */
13064 static void
13065 bxe_ilt_wr(struct bxe_softc *sc, uint32_t index, bus_addr_t addr)
13066 {
13067         int reg;
13068
13069         DBENTER(BXE_INSANE_LOAD | BXE_INSANE_RESET);
13070
13071         if (CHIP_IS_E1H(sc))
13072                 reg = PXP2_REG_RQ_ONCHIP_AT_B0 + index * 8;
13073         else
13074                 reg = PXP2_REG_RQ_ONCHIP_AT + index * 8;
13075
13076         bxe_wb_wr(sc, reg, ONCHIP_ADDR1(addr), ONCHIP_ADDR2(addr));
13077
13078         DBEXIT(BXE_INSANE_LOAD | BXE_INSANE_RESET);
13079 }
13080
13081 /*
13082  * Initialize a function.
13083  *
13084  * Returns:
13085  *   0 = Success, !0 = Failure.
13086  */
13087 static int
13088 bxe_init_func(struct bxe_softc *sc)
13089 {
13090         uint32_t addr, val;
13091         int func, i, port;
13092
13093         port = BP_PORT(sc);
13094         func = BP_FUNC(sc);
13095
13096         DBENTER(BXE_VERBOSE_LOAD | BXE_VERBOSE_RESET);
13097
13098         DBPRINT(sc, (BXE_VERBOSE_LOAD | BXE_VERBOSE_RESET),
13099             "%s(): Initializing port %d, function %d.\n", __FUNCTION__, port,
13100             func);
13101
13102         /* Set MSI reconfigure capability. */
13103         addr = (port ? HC_REG_CONFIG_1 : HC_REG_CONFIG_0);
13104         val = REG_RD(sc, addr);
13105         val |= HC_CONFIG_0_REG_MSI_ATTN_EN_0;
13106         REG_WR(sc, addr, val);
13107
13108         i = FUNC_ILT_BASE(func);
13109
13110         bxe_ilt_wr(sc, i, BXE_SP_MAPPING(sc, context));
13111
13112         if (CHIP_IS_E1H(sc)) {
13113                 REG_WR(sc, PXP2_REG_RQ_CDU_FIRST_ILT, i);
13114                 REG_WR(sc, PXP2_REG_RQ_CDU_LAST_ILT, i + CNIC_ILT_LINES);
13115         } else /* E1 */
13116                 REG_WR(sc, PXP2_REG_PSWRQ_CDU0_L2P + func * 4,
13117                     PXP_ILT_RANGE(i, i + CNIC_ILT_LINES));
13118
13119         if (CHIP_IS_E1H(sc)) {
13120                 bxe_init_block(sc, MISC_BLOCK, FUNC0_STAGE + func);
13121                 bxe_init_block(sc, TCM_BLOCK, FUNC0_STAGE + func);
13122                 bxe_init_block(sc, UCM_BLOCK, FUNC0_STAGE + func);
13123                 bxe_init_block(sc, CCM_BLOCK, FUNC0_STAGE + func);
13124                 bxe_init_block(sc, XCM_BLOCK, FUNC0_STAGE + func);
13125                 bxe_init_block(sc, TSEM_BLOCK, FUNC0_STAGE + func);
13126                 bxe_init_block(sc, USEM_BLOCK, FUNC0_STAGE + func);
13127                 bxe_init_block(sc, CSEM_BLOCK, FUNC0_STAGE + func);
13128                 bxe_init_block(sc, XSEM_BLOCK, FUNC0_STAGE + func);
13129
13130                 REG_WR(sc, NIG_REG_LLH0_FUNC_EN + port * 8, 1);
13131                 REG_WR(sc, NIG_REG_LLH0_FUNC_VLAN_ID + port * 8, sc->e1hov);
13132         }
13133
13134         /* Host Coalescing initialization per function. */
13135         if (CHIP_IS_E1H(sc)) {
13136                 REG_WR(sc, MISC_REG_AEU_GENERAL_ATTN_12 + func * 4, 0);
13137                 REG_WR(sc, HC_REG_LEADING_EDGE_0 + port * 8, 0);
13138                 REG_WR(sc, HC_REG_TRAILING_EDGE_0 + port * 8, 0);
13139         }
13140
13141         bxe_init_block(sc, HC_BLOCK, FUNC0_STAGE + func);
13142
13143         /* Reset PCIe block debug values. */
13144         REG_WR(sc, 0x2114, 0xffffffff);
13145         REG_WR(sc, 0x2120, 0xffffffff);
13146
13147         DBEXIT(BXE_VERBOSE_LOAD | BXE_VERBOSE_RESET);
13148
13149         return (0);
13150 }
13151
13152 /*
13153  *
13154  * Returns:
13155  *   0 = Failure, !0 = Failure.
13156  */
13157 static int
13158 bxe_init_hw(struct bxe_softc *sc, uint32_t load_code)
13159 {
13160         int func, i, rc;
13161
13162         rc = 0;
13163         DBENTER(BXE_VERBOSE_LOAD | BXE_VERBOSE_RESET);
13164
13165         sc->dmae_ready = 0;
13166         switch (load_code) {
13167         case FW_MSG_CODE_DRV_LOAD_COMMON:
13168                 rc = bxe_init_common(sc);
13169                 if (rc)
13170                         goto bxe_init_hw_exit;
13171                 /* FALLTHROUGH */
13172         case FW_MSG_CODE_DRV_LOAD_PORT:
13173                 sc->dmae_ready = 1;
13174                 rc = bxe_init_port(sc);
13175                 if (rc)
13176                         goto bxe_init_hw_exit;
13177                 /* FALLTHROUGH */
13178         case FW_MSG_CODE_DRV_LOAD_FUNCTION:
13179                 sc->dmae_ready = 1;
13180                 rc = bxe_init_func(sc);
13181                 if (rc)
13182                         goto bxe_init_hw_exit;
13183                 break;
13184         default:
13185                 DBPRINT(sc, BXE_WARN,
13186                     "%s(): Unknown load_code (0x%08X) from MCP!\n",
13187                     __FUNCTION__, load_code);
13188                 break;
13189         }
13190
13191         /* Fetch additional config data if the bootcode is running. */
13192         if (!NOMCP(sc)) {
13193                 func = BP_FUNC(sc);
13194                 /* Fetch the pulse sequence number. */
13195                 sc->fw_drv_pulse_wr_seq = (SHMEM_RD(sc,
13196                     func_mb[func].drv_pulse_mb) & DRV_PULSE_SEQ_MASK);
13197         }
13198
13199         /* Clear the default status block. */
13200         bxe_zero_def_sb(sc);
13201         for (i = 0; i < sc->num_queues; i++)
13202                 bxe_zero_sb(sc, BP_L_ID(sc) + i);
13203
13204 bxe_init_hw_exit:
13205         DBEXIT(BXE_VERBOSE_LOAD | BXE_VERBOSE_RESET);
13206
13207         return (rc);
13208 }
13209
13210 /*
13211  * Send a firmware command and wait for the response.
13212  *
13213  * Post a command to shared memory for the bootcode running on the MCP and
13214  * stall until the bootcode responds or a timeout occurs.
13215  *
13216  * Returns:
13217  *   0 = Failure, otherwise firmware response code (FW_MSG_CODE_*).
13218  */
13219 static int
13220 bxe_fw_command(struct bxe_softc *sc, uint32_t command)
13221 {
13222         uint32_t cnt, rc, seq;
13223         int func;
13224
13225         func = BP_FUNC(sc);
13226         seq = ++sc->fw_seq;
13227         rc = 0;
13228         cnt = 1;
13229
13230         DBRUNMSG(BXE_VERBOSE, bxe_decode_mb_msgs(sc, (command | seq), 0));
13231
13232         BXE_FWMB_LOCK(sc);
13233
13234         /* Write the command to the shared memory mailbox. */
13235         SHMEM_WR(sc, func_mb[func].drv_mb_header, (command | seq));
13236
13237         /* Wait up to 2 seconds for a response. */
13238         do {
13239                 /* Wait 10ms for a response. */
13240                 DELAY(10000);
13241
13242                 /* Pickup the response. */
13243                 rc = SHMEM_RD(sc, func_mb[func].fw_mb_header);
13244         } while ((seq != (rc & FW_MSG_SEQ_NUMBER_MASK)) && (cnt++ < 400));
13245
13246         DBRUNMSG(BXE_VERBOSE, bxe_decode_mb_msgs(sc, 0, rc));
13247
13248         /* Make sure we read the right response. */
13249         if (seq == (rc & FW_MSG_SEQ_NUMBER_MASK ))
13250                 rc &= FW_MSG_CODE_MASK;
13251         else {
13252                 BXE_PRINTF("%s(%d): Bootcode failed to respond!\n",
13253                     __FILE__, __LINE__);
13254                 DBRUN(bxe_dump_fw(sc));
13255                 rc = 0;
13256         }
13257
13258         BXE_FWMB_UNLOCK(sc);
13259         return (rc);
13260 }
13261
13262 /*
13263  * Allocate a block of memory and map it for DMA.  No partial
13264  * completions allowed, release any resources acquired if we
13265  * can't acquire all resources.
13266  *
13267  * Returns:
13268  *   0 = Success, !0 = Failure
13269  *
13270  * Modifies:
13271  *   dma->paddr
13272  *   dma->vaddr
13273  *   dma->tag
13274  *   dma->map
13275  *   dma->size
13276  *
13277  */
13278 static int
13279 bxe_dma_malloc(struct bxe_softc *sc, bus_size_t size,
13280     struct bxe_dma *dma, int mapflags, const char *msg)
13281 {
13282         int rc;
13283
13284         DBENTER(BXE_VERBOSE_LOAD | BXE_VERBOSE_RESET);
13285
13286         DBRUNIF(dma->size > 0,
13287             BXE_PRINTF("%s(): Called for %s with size > 0 (%05d)!\n",
13288             __FUNCTION__, msg, (int) dma->size));
13289
13290         rc = bus_dma_tag_create(
13291             sc->parent_tag,             /* parent */
13292             BCM_PAGE_SIZE,              /* alignment for segs */
13293             BXE_DMA_BOUNDARY,           /* cannot cross */
13294             BUS_SPACE_MAXADDR,          /* restricted low */
13295             BUS_SPACE_MAXADDR,          /* restricted hi */
13296             NULL, NULL,                 /* filter f(), arg */
13297             size,                       /* max size for this tag */
13298             1,                          /* # of discontinuities */
13299             size,                       /* max seg size */
13300             BUS_DMA_ALLOCNOW,           /* flags */
13301             NULL, NULL,                 /* lock f(), arg */
13302             &dma->tag);
13303
13304         if (rc != 0) {
13305                 BXE_PRINTF("%s(%d): bus_dma_tag_create() "
13306                     "failed (rc = %d) for %s!\n",
13307                     __FILE__, __LINE__, rc, msg);
13308                 goto bxe_dma_malloc_fail_create;
13309         }
13310
13311         rc = bus_dmamem_alloc(dma->tag, (void **)&dma->vaddr,
13312             BUS_DMA_NOWAIT, &dma->map);
13313         if (rc != 0) {
13314                 BXE_PRINTF("%s(%d): bus_dmamem_alloc() "
13315                     "failed (rc = %d) for %s!\n",
13316                     __FILE__, __LINE__, rc, msg);
13317                 goto bxe_dma_malloc_fail_alloc;
13318         }
13319
13320         rc = bus_dmamap_load(dma->tag, dma->map, dma->vaddr, size,
13321             bxe_dma_map_addr, &dma->paddr, mapflags | BUS_DMA_NOWAIT);
13322         if (rc != 0) {
13323                 BXE_PRINTF("%s(%d): bus_dmamap_load() "
13324                     "failed (rc = %d) for %s!\n",
13325                     __FILE__, __LINE__, rc, msg);
13326                 goto bxe_dma_malloc_fail_load;
13327         }
13328
13329         dma->size = size;
13330
13331         DBPRINT(sc, BXE_VERBOSE, "%s(): size=%06d, vaddr=0x%p, "
13332             "paddr=0x%jX - %s\n", __FUNCTION__, (int) dma->size,
13333             dma->vaddr, (uintmax_t) dma->paddr, msg);
13334
13335         goto bxe_dma_malloc_exit;
13336
13337 bxe_dma_malloc_fail_load:
13338         bus_dmamem_free(dma->tag, dma->vaddr, dma->map);
13339
13340 bxe_dma_malloc_fail_alloc:
13341         bus_dma_tag_destroy(dma->tag);
13342         dma->vaddr = NULL;
13343
13344 bxe_dma_malloc_fail_create:
13345         dma->map = NULL;
13346         dma->tag = NULL;
13347         dma->size = 0;
13348
13349 bxe_dma_malloc_exit:
13350         DBEXIT(BXE_VERBOSE_LOAD | BXE_VERBOSE_RESET);
13351         return (rc);
13352 }
13353
13354 /*
13355  * Release a block of DMA memory associated tag/map.
13356  *
13357  * Returns:
13358  *   None
13359  */
13360 static void
13361 bxe_dma_free(struct bxe_softc *sc, struct bxe_dma *dma)
13362 {
13363         DBENTER(BXE_VERBOSE_LOAD | BXE_VERBOSE_UNLOAD);
13364
13365         if (dma->size > 0) {
13366                 bus_dmamap_sync(dma->tag, dma->map,
13367                     BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
13368                 bus_dmamap_unload(dma->tag, dma->map);
13369                 bus_dmamem_free(dma->tag, dma->vaddr, dma->map);
13370                 bus_dma_tag_destroy(dma->tag);
13371                 dma->size = 0;
13372         }
13373
13374         DBEXIT(BXE_VERBOSE_LOAD | BXE_VERBOSE_UNLOAD);
13375 }
13376
13377 /*
13378  * Free any DMA memory owned by the driver.
13379  *
13380  * Scans through each data structre that requires DMA memory and frees
13381  * the memory if allocated.
13382  *
13383  * Returns:
13384  *   Nothing.
13385  */
13386 static void
13387 bxe_host_structures_free(struct bxe_softc *sc)
13388 {
13389         struct bxe_fastpath *fp;
13390         int i, j, max_agg_queues;
13391
13392         DBENTER(BXE_VERBOSE_LOAD | BXE_VERBOSE_RESET | BXE_VERBOSE_UNLOAD);
13393         max_agg_queues = CHIP_IS_E1H(sc) ?
13394             ETH_MAX_AGGREGATION_QUEUES_E1H :
13395             ETH_MAX_AGGREGATION_QUEUES_E1;
13396
13397         if (sc->parent_tag == NULL)
13398                 goto bxe_host_structures_free_exit;
13399
13400         for (i = 0; i < sc->num_queues; i++) {
13401                 fp = &sc->fp[i];
13402
13403                 /* Trust no one! */
13404                 if (fp == NULL)
13405                         break;
13406
13407                 /* Status block. */
13408                 bxe_dma_free(sc, &fp->sb_dma);
13409
13410                 /* TX chain. */
13411                 bxe_dma_free(sc, &fp->tx_dma);
13412                 fp->tx_chain = NULL;
13413
13414                 /* RX chain */
13415                 bxe_dma_free(sc, &fp->rx_dma);
13416                 fp->rx_chain = NULL;
13417
13418                 /* RCQ chain */
13419                 bxe_dma_free(sc, &fp->rcq_dma);
13420                 fp->rcq_chain = NULL;
13421
13422                 /* SG chain */
13423                 bxe_dma_free(sc, &fp->sg_dma);
13424                 fp->sg_chain = NULL;
13425
13426                 /* Unload and destroy the TX mbuf maps. */
13427                 if (fp->tx_mbuf_tag != NULL) {
13428                         for (j = 0; j < TOTAL_TX_BD; j++) {
13429                                 if (fp->tx_mbuf_map[j] != NULL) {
13430                                         bus_dmamap_unload(
13431                                             fp->tx_mbuf_tag,
13432                                             fp->tx_mbuf_map[j]);
13433                                         bus_dmamap_destroy(
13434                                             fp->tx_mbuf_tag,
13435                                             fp->tx_mbuf_map[j]);
13436                                 }
13437                         }
13438
13439                         bus_dma_tag_destroy(fp->tx_mbuf_tag);
13440                 }
13441
13442                 /* Unload and destroy the TPA pool mbuf maps. */
13443                 if (fp->rx_mbuf_tag != NULL) {
13444                         if (fp->tpa_mbuf_spare_map != NULL) {
13445                                 bus_dmamap_unload(
13446                                     fp->rx_mbuf_tag,
13447                                     fp->tpa_mbuf_spare_map);
13448                                 bus_dmamap_destroy(
13449                                     fp->rx_mbuf_tag,
13450                                     fp->tpa_mbuf_spare_map);
13451                         }
13452
13453                         for (j = 0; j < max_agg_queues; j++) {
13454                                 if (fp->tpa_mbuf_map[j] != NULL) {
13455                                         bus_dmamap_unload(
13456                                             fp->rx_mbuf_tag,
13457                                             fp->tpa_mbuf_map[j]);
13458                                         bus_dmamap_destroy(
13459                                             fp->rx_mbuf_tag,
13460                                             fp->tpa_mbuf_map[j]);
13461                                 }
13462                         }
13463                 }
13464
13465                 /* Unload and destroy the SGE Buf maps. */
13466                 if (fp->rx_sge_buf_tag != NULL) {
13467                         if (fp->rx_sge_spare_map != NULL) {
13468                                 bus_dmamap_unload(
13469                                     fp->rx_sge_buf_tag,
13470                                     fp->rx_sge_spare_map);
13471                                 bus_dmamap_destroy(
13472                                     fp->rx_sge_buf_tag,
13473                                     fp->rx_sge_spare_map);
13474                         }
13475
13476                         for (j = 0; j < TOTAL_RX_SGE; j++) {
13477                                 if (fp->rx_sge_buf_map[j] != NULL) {
13478                                         bus_dmamap_unload(
13479                                             fp->rx_sge_buf_tag,
13480                                             fp->rx_sge_buf_map[j]);
13481                                         bus_dmamap_destroy(
13482                                             fp->rx_sge_buf_tag,
13483                                             fp->rx_sge_buf_map[j]);
13484                                 }
13485                         }
13486
13487                         bus_dma_tag_destroy(fp->rx_sge_buf_tag);
13488                 }
13489
13490                 /* Unload and destroy the RX mbuf maps. */
13491                 if (fp->rx_mbuf_tag != NULL) {
13492                         if (fp->rx_mbuf_spare_map != NULL) {
13493                                 bus_dmamap_unload(fp->rx_mbuf_tag,
13494                                     fp->rx_mbuf_spare_map);
13495                                 bus_dmamap_destroy(fp->rx_mbuf_tag,
13496                                     fp->rx_mbuf_spare_map);
13497                         }
13498
13499                         for (j = 0; j < TOTAL_RX_BD; j++) {
13500                                 if (fp->rx_mbuf_map[j] != NULL) {
13501                                         bus_dmamap_unload(
13502                                             fp->rx_mbuf_tag,
13503                                             fp->rx_mbuf_map[j]);
13504                                         bus_dmamap_destroy(
13505                                             fp->rx_mbuf_tag,
13506                                             fp->rx_mbuf_map[j]);
13507                                 }
13508                         }
13509
13510                         bus_dma_tag_destroy(fp->rx_mbuf_tag);
13511                 }
13512         }
13513
13514         /* Destroy the default status block */
13515         bxe_dma_free(sc, &sc->def_sb_dma);
13516         sc->def_sb = NULL;
13517
13518         /* Destroy the statistics block */
13519         bxe_dma_free(sc, &sc->stats_dma);
13520         sc->stats = NULL;
13521
13522         /* Destroy the slowpath block. */
13523         bxe_dma_free(sc, &sc->slowpath_dma);
13524         sc->slowpath = NULL;
13525
13526         /* Destroy the slowpath queue. */
13527         bxe_dma_free(sc, &sc->spq_dma);
13528         sc->spq = NULL;
13529
13530         /* Destroy the slowpath queue. */
13531         bxe_dma_free(sc, &sc->gz_dma);
13532         sc->gz = NULL;
13533         free(sc->strm, M_DEVBUF);
13534         sc->strm = NULL;
13535
13536 bxe_host_structures_free_exit:
13537         DBEXIT(BXE_VERBOSE_LOAD | BXE_VERBOSE_RESET | BXE_VERBOSE_UNLOAD);
13538 }
13539
13540 /*
13541  * Get DMA memory from the OS.
13542  *
13543  * Validates that the OS has provided DMA buffers in response to a
13544  * bus_dmamap_load call and saves the physical address of those buffers.
13545  * When the callback is used the OS will return 0 for the mapping function
13546  * (bus_dmamap_load) so we use the value of map_arg->maxsegs to pass any
13547  * failures back to the caller.
13548  *
13549  * Returns:
13550  *   Nothing.
13551  */
13552 static void
13553 bxe_dma_map_addr(void *arg, bus_dma_segment_t *segs, int nseg, int error)
13554 {
13555         bus_addr_t *busaddr;
13556
13557         busaddr = arg;
13558         /* Check for an error and signal the caller that an error occurred. */
13559         if (error) {
13560                 printf(
13561                     "bxe %s(%d): DMA mapping error (error = %d, nseg = %d)!\n",
13562                     __FILE__, __LINE__, error, nseg);
13563                 *busaddr = 0;
13564                 return;
13565         }
13566
13567         *busaddr = segs->ds_addr;
13568 }
13569
13570 /*
13571  * Allocate any non-paged DMA memory needed by the driver.
13572  *
13573  * Returns:
13574  *   0 = Success, !0 = Failure.
13575  */
13576 static int
13577 bxe_host_structures_alloc(device_t dev)
13578 {
13579         struct bxe_softc *sc;
13580         struct bxe_fastpath *fp;
13581         int rc;
13582         bus_addr_t busaddr;
13583         bus_size_t max_size, max_seg_size;
13584         int i, j, max_segments;
13585
13586         sc = device_get_softc(dev);
13587         DBENTER(BXE_VERBOSE_RESET);
13588         rc = 0;
13589         int max_agg_queues = CHIP_IS_E1H(sc) ?
13590             ETH_MAX_AGGREGATION_QUEUES_E1H :
13591             ETH_MAX_AGGREGATION_QUEUES_E1;
13592
13593         /*
13594          * Allocate the parent bus DMA tag appropriate for PCI.
13595          */
13596         rc = bus_dma_tag_create(
13597             bus_get_dma_tag(dev),       /* PCI parent tag */
13598             1,                          /* alignment for segs */
13599             BXE_DMA_BOUNDARY,           /* cannot cross */
13600             BUS_SPACE_MAXADDR,          /* restricted low */
13601             BUS_SPACE_MAXADDR,          /* restricted hi */
13602             NULL,                       /* filter f() */
13603             NULL,                       /* filter f() arg */
13604             MAXBSIZE,                   /* max map for this tag */
13605             BUS_SPACE_UNRESTRICTED,     /* # of discontinuities */
13606             BUS_SPACE_MAXSIZE_32BIT,    /* max seg size */
13607             0,                          /* flags */
13608             NULL,                       /* lock f() */
13609             NULL,                       /* lock f() arg */
13610             &sc->parent_tag);           /* dma tag */
13611         if (rc != 0) {
13612                 BXE_PRINTF("%s(%d): Could not allocate parent DMA tag!\n",
13613                     __FILE__, __LINE__);
13614                 rc = ENOMEM;
13615                 goto bxe_host_structures_alloc_exit;
13616         }
13617
13618         /* Allocate DMA memory for each fastpath structure. */
13619         for (i = 0; i < sc->num_queues; i++) {
13620                 fp = &sc->fp[i];
13621
13622                 /*
13623                  * Allocate status block*
13624                 */
13625                 rc = bxe_dma_malloc(sc, BXE_STATUS_BLK_SZ,
13626                     &fp->sb_dma, BUS_DMA_NOWAIT, "fp status block");
13627                 /* ToDo: Only using 32 bytes out of 4KB allocation! */
13628                 if (rc != 0)
13629                         goto bxe_host_structures_alloc_exit;
13630                 fp->status_block =
13631                     (struct host_status_block *) fp->sb_dma.vaddr;
13632
13633                 /*
13634                  * Allocate TX chain.
13635                  */
13636                 rc = bxe_dma_malloc(sc, BXE_TX_CHAIN_PAGE_SZ *
13637                     NUM_TX_PAGES, &fp->tx_dma, BUS_DMA_NOWAIT,
13638                     "tx chain pages");
13639                 if (rc != 0)
13640                         goto bxe_host_structures_alloc_exit;
13641                 fp->tx_chain = (union eth_tx_bd_types *) fp->tx_dma.vaddr;
13642
13643                 /* Link the TX chain pages. */
13644                 for (j = 1; j <= NUM_TX_PAGES; j++) {
13645                         struct eth_tx_next_bd *tx_n_bd =
13646                             &fp->tx_chain[TOTAL_TX_BD_PER_PAGE * j - 1].next_bd;
13647
13648                         busaddr = fp->tx_dma.paddr +
13649                             BCM_PAGE_SIZE * (j % NUM_TX_PAGES);
13650                         tx_n_bd->addr_hi = htole32(U64_HI(busaddr));
13651                         tx_n_bd->addr_lo = htole32(U64_LO(busaddr));
13652                 }
13653
13654                 /*
13655                  * Allocate RX chain.
13656                  */
13657                 rc = bxe_dma_malloc(sc, BXE_RX_CHAIN_PAGE_SZ *
13658                     NUM_RX_PAGES, &fp->rx_dma, BUS_DMA_NOWAIT,
13659                     "rx chain pages");
13660                 if (rc != 0)
13661                         goto bxe_host_structures_alloc_exit;
13662                 fp->rx_chain = (struct eth_rx_bd *) fp->rx_dma.vaddr;
13663
13664                 /* Link the RX chain pages. */
13665                 for (j = 1; j <= NUM_RX_PAGES; j++) {
13666                         struct eth_rx_bd *rx_bd =
13667                             &fp->rx_chain[TOTAL_RX_BD_PER_PAGE * j - 2];
13668
13669                         busaddr = fp->rx_dma.paddr +
13670                             BCM_PAGE_SIZE * (j % NUM_RX_PAGES);
13671                         rx_bd->addr_hi = htole32(U64_HI(busaddr));
13672                         rx_bd->addr_lo = htole32(U64_LO(busaddr));
13673                 }
13674
13675                 /*
13676                  * Allocate CQ chain.
13677                  */
13678                 rc = bxe_dma_malloc(sc, BXE_RX_CHAIN_PAGE_SZ *
13679                     NUM_RCQ_PAGES, &fp->rcq_dma, BUS_DMA_NOWAIT,
13680                     "rcq chain pages");
13681                 if (rc != 0)
13682                         goto bxe_host_structures_alloc_exit;
13683                 fp->rcq_chain = (union eth_rx_cqe *) fp->rcq_dma.vaddr;
13684
13685                 /* Link the CQ chain pages. */
13686                 for (j = 1; j <= NUM_RCQ_PAGES; j++) {
13687                         struct eth_rx_cqe_next_page *nextpg =
13688                             (struct eth_rx_cqe_next_page *)
13689                             &fp->rcq_chain[TOTAL_RCQ_ENTRIES_PER_PAGE * j - 1];
13690
13691                         busaddr = fp->rcq_dma.paddr +
13692                             BCM_PAGE_SIZE * (j % NUM_RCQ_PAGES);
13693                         nextpg->addr_hi = htole32(U64_HI(busaddr));
13694                         nextpg->addr_lo = htole32(U64_LO(busaddr));
13695                 }
13696
13697                 /*
13698                  * Allocate SG chain.
13699                  */
13700                 rc = bxe_dma_malloc(sc, BXE_RX_CHAIN_PAGE_SZ *
13701                     NUM_RX_SGE_PAGES, &fp->sg_dma, BUS_DMA_NOWAIT,
13702                     "sg chain pages");
13703                 if (rc != 0)
13704                         goto bxe_host_structures_alloc_exit;
13705                 fp->sg_chain = (struct eth_rx_sge *) fp->sg_dma.vaddr;
13706
13707                 /* Link the SG chain pages. */
13708                 for (j = 1; j <= NUM_RX_SGE_PAGES; j++) {
13709                         struct eth_rx_sge *nextpg =
13710                             &fp->sg_chain[TOTAL_RX_SGE_PER_PAGE * j - 2];
13711
13712                         busaddr = fp->sg_dma.paddr +
13713                             BCM_PAGE_SIZE * (j % NUM_RX_SGE_PAGES);
13714                         nextpg->addr_hi = htole32(U64_HI(busaddr));
13715                         nextpg->addr_lo = htole32(U64_LO(busaddr));
13716                 }
13717
13718                 /*
13719                  * Check required size before mapping to conserve resources.
13720                  */
13721                 if (sc->tso_enable == TRUE) {
13722                         max_size     = BXE_TSO_MAX_SIZE;
13723                         max_segments = BXE_TSO_MAX_SEGMENTS;
13724                         max_seg_size = BXE_TSO_MAX_SEG_SIZE;
13725                 } else {
13726                         max_size     = MCLBYTES * BXE_MAX_SEGMENTS;
13727                         max_segments = BXE_MAX_SEGMENTS;
13728                         max_seg_size = MCLBYTES;
13729                 }
13730
13731                 /* Create a DMA tag for TX mbufs. */
13732                 if (bus_dma_tag_create(sc->parent_tag,
13733                     1,                  /* alignment for segs */
13734                     BXE_DMA_BOUNDARY,   /* cannot cross */
13735                     BUS_SPACE_MAXADDR,  /* restricted low */
13736                     BUS_SPACE_MAXADDR,  /* restricted hi */
13737                     NULL,               /* filter f() */
13738                     NULL,               /* filter f() arg */
13739                     max_size,           /* max map for this tag */
13740                     max_segments,       /* # of discontinuities */
13741                     max_seg_size,       /* max seg size */
13742                     0,                  /* flags */
13743                     NULL,               /* lock f() */
13744                     NULL,               /* lock f() arg */
13745                     &fp->tx_mbuf_tag)) {
13746                         BXE_PRINTF(
13747                             "%s(%d): Could not allocate fp[%d] "
13748                             "TX mbuf DMA tag!\n",
13749                             __FILE__, __LINE__, i);
13750                         rc = ENOMEM;
13751                         goto bxe_host_structures_alloc_exit;
13752                 }
13753
13754                 /* Create DMA maps for each the TX mbuf cluster(ext buf). */
13755                 for (j = 0; j < TOTAL_TX_BD; j++) {
13756                         if (bus_dmamap_create(fp->tx_mbuf_tag,
13757                             BUS_DMA_NOWAIT,
13758                             &fp->tx_mbuf_map[j])) {
13759                                 BXE_PRINTF(
13760                                     "%s(%d): Unable to create fp[%02d]."
13761                                     "tx_mbuf_map[%d] DMA map!\n",
13762                                     __FILE__, __LINE__, i, j);
13763                                 rc = ENOMEM;
13764                                 goto bxe_host_structures_alloc_exit;
13765                         }
13766                 }
13767
13768                 /*
13769                  * Create a DMA tag for RX mbufs.
13770                  */
13771                 if (bus_dma_tag_create(sc->parent_tag,
13772                     1,                  /* alignment for segs */
13773                     BXE_DMA_BOUNDARY,   /* cannot cross */
13774                     BUS_SPACE_MAXADDR,  /* restricted low */
13775                     BUS_SPACE_MAXADDR,  /* restricted hi */
13776                     NULL,               /* filter f() */
13777                     NULL,               /* filter f() arg */
13778                     MJUM9BYTES,         /* max map for this tag */
13779                     1,                  /* # of discontinuities */
13780                     MJUM9BYTES,         /* max seg size */
13781                     0,                  /* flags */
13782                     NULL,               /* lock f() */
13783                     NULL,               /* lock f() arg */
13784                     &fp->rx_mbuf_tag)) {
13785                         BXE_PRINTF(
13786                             "%s(%d): Could not allocate fp[%02d] "
13787                             "RX mbuf DMA tag!\n",
13788                             __FILE__, __LINE__, i);
13789                         rc = ENOMEM;
13790                         goto bxe_host_structures_alloc_exit;
13791                 }
13792
13793                 /* Create DMA maps for the RX mbuf clusters. */
13794                 if (bus_dmamap_create(fp->rx_mbuf_tag,
13795                     BUS_DMA_NOWAIT, &fp->rx_mbuf_spare_map)) {
13796                         BXE_PRINTF(
13797                             "%s(%d): Unable to create fp[%02d]."
13798                             "rx_mbuf_spare_map DMA map!\n",
13799                             __FILE__, __LINE__, i);
13800                         rc = ENOMEM;
13801                         goto bxe_host_structures_alloc_exit;
13802                 }
13803
13804                 for (j = 0; j < TOTAL_RX_BD; j++) {
13805                         if (bus_dmamap_create(fp->rx_mbuf_tag,
13806                             BUS_DMA_NOWAIT, &fp->rx_mbuf_map[j])) {
13807                                 BXE_PRINTF(
13808                                     "%s(%d): Unable to create fp[%02d]."
13809                                     "rx_mbuf_map[%d] DMA map!\n",
13810                                     __FILE__, __LINE__, i, j);
13811                                 rc = ENOMEM;
13812                                 goto bxe_host_structures_alloc_exit;
13813                         }
13814                 }
13815
13816                 /*
13817                  * Create a DMA tag for RX SGE bufs.
13818                  */
13819                 if (bus_dma_tag_create(sc->parent_tag, 1,
13820                     BXE_DMA_BOUNDARY, BUS_SPACE_MAXADDR,
13821                     BUS_SPACE_MAXADDR, NULL, NULL, PAGE_SIZE, 1,
13822                     PAGE_SIZE, 0, NULL, NULL, &fp->rx_sge_buf_tag)) {
13823                         BXE_PRINTF(
13824                             "%s(%d): Could not allocate fp[%02d] "
13825                             "RX SGE mbuf DMA tag!\n",
13826                             __FILE__, __LINE__, i);
13827                         rc = ENOMEM;
13828                         goto bxe_host_structures_alloc_exit;
13829                 }
13830
13831                 /* Create DMA maps for the SGE mbuf clusters. */
13832                 if (bus_dmamap_create(fp->rx_sge_buf_tag,
13833                     BUS_DMA_NOWAIT, &fp->rx_sge_spare_map)) {
13834                         BXE_PRINTF(
13835                            "%s(%d): Unable to create fp[%02d]."
13836                            "rx_sge_spare_map DMA map!\n",
13837                             __FILE__, __LINE__, i);
13838                         rc = ENOMEM;
13839                         goto bxe_host_structures_alloc_exit;
13840                 }
13841
13842                 for (j = 0; j < TOTAL_RX_SGE; j++) {
13843                         if (bus_dmamap_create(fp->rx_sge_buf_tag,
13844                             BUS_DMA_NOWAIT, &fp->rx_sge_buf_map[j])) {
13845                                 BXE_PRINTF(
13846                                    "%s(%d): Unable to create fp[%02d]."
13847                                    "rx_sge_buf_map[%d] DMA map!\n",
13848                                     __FILE__, __LINE__, i, j);
13849                                 rc = ENOMEM;
13850                                 goto bxe_host_structures_alloc_exit;
13851                         }
13852                 }
13853
13854                 /* Create DMA maps for the TPA pool mbufs. */
13855                 if (bus_dmamap_create(fp->rx_mbuf_tag,
13856                     BUS_DMA_NOWAIT, &fp->tpa_mbuf_spare_map)) {
13857                         BXE_PRINTF(
13858                             "%s(%d): Unable to create fp[%02d]."
13859                             "tpa_mbuf_spare_map DMA map!\n",
13860                             __FILE__, __LINE__, i);
13861                         rc = ENOMEM;
13862                         goto bxe_host_structures_alloc_exit;
13863                 }
13864
13865                 for (j = 0; j < max_agg_queues; j++) {
13866                         if (bus_dmamap_create(fp->rx_mbuf_tag,
13867                             BUS_DMA_NOWAIT, &fp->tpa_mbuf_map[j])) {
13868                                 BXE_PRINTF(
13869                                     "%s(%d): Unable to create fp[%02d]."
13870                                     "tpa_mbuf_map[%d] DMA map!\n",
13871                                     __FILE__, __LINE__, i, j);
13872                                 rc = ENOMEM;
13873                                 goto bxe_host_structures_alloc_exit;
13874                         }
13875                 }
13876
13877                 bxe_init_sge_ring_bit_mask(fp);
13878         }
13879
13880         /*
13881          * Allocate default status block.
13882          */
13883         rc = bxe_dma_malloc(sc, BXE_DEF_STATUS_BLK_SZ, &sc->def_sb_dma,
13884             BUS_DMA_NOWAIT, "default status block");
13885         if (rc != 0)
13886                 goto bxe_host_structures_alloc_exit;
13887         sc->def_sb = (struct host_def_status_block *) sc->def_sb_dma.vaddr;
13888
13889         /*
13890          * Allocate statistics block.
13891          */
13892         rc = bxe_dma_malloc(sc, BXE_STATS_BLK_SZ, &sc->stats_dma,
13893             BUS_DMA_NOWAIT, "statistics block");
13894         if (rc != 0)
13895                 goto bxe_host_structures_alloc_exit;
13896         sc->stats = (struct statistics_block *) sc->stats_dma.vaddr;
13897
13898         /*
13899          * Allocate slowpath block.
13900          */
13901         rc = bxe_dma_malloc(sc, BXE_SLOWPATH_SZ, &sc->slowpath_dma,
13902             BUS_DMA_NOWAIT, "slowpath block");
13903         if (rc != 0)
13904                 goto bxe_host_structures_alloc_exit;
13905         sc->slowpath = (struct bxe_slowpath *) sc->slowpath_dma.vaddr;
13906
13907         /*
13908          * Allocate slowpath queue.
13909          */
13910         rc = bxe_dma_malloc(sc, BXE_SPQ_SZ, &sc->spq_dma,
13911             BUS_DMA_NOWAIT, "slowpath queue");
13912         if (rc != 0)
13913                 goto bxe_host_structures_alloc_exit;
13914         sc->spq = (struct eth_spe *) sc->spq_dma.vaddr;
13915
13916         /*
13917          * Allocate firmware decompression buffer.
13918          */
13919         rc = bxe_dma_malloc(sc, BXE_FW_BUF_SIZE, &sc->gz_dma,
13920             BUS_DMA_NOWAIT, "gunzip buffer");
13921         if (rc != 0)
13922                 goto bxe_host_structures_alloc_exit;
13923         sc->gz = sc->gz_dma.vaddr;
13924         if (sc->strm == NULL) {
13925                 goto bxe_host_structures_alloc_exit;
13926         }
13927
13928         sc->strm = malloc(sizeof(*sc->strm), M_DEVBUF, M_NOWAIT);
13929
13930 bxe_host_structures_alloc_exit:
13931         DBEXIT(BXE_VERBOSE_RESET);
13932         return (rc);
13933 }
13934
13935 /*
13936  * Program the MAC address for 57710 controllers.
13937  *
13938  * Returns:
13939  *   Nothing.
13940  */
13941 static void
13942 bxe_set_mac_addr_e1(struct bxe_softc *sc, int set)
13943 {
13944         struct mac_configuration_cmd *config;
13945         struct mac_configuration_entry *config_table;
13946         uint8_t *eaddr;
13947         int port;
13948
13949         DBENTER(BXE_VERBOSE_LOAD | BXE_VERBOSE_RESET | BXE_VERBOSE_UNLOAD);
13950
13951         config = BXE_SP(sc, mac_config);
13952         port = BP_PORT(sc);
13953         /*
13954          * CAM allocation:
13955          * Port 0 Unicast Addresses: 32 Perfect Match Filters (31-0)
13956          * Port 1 Unicast Addresses: 32 Perfect Match Filters (63-32)
13957          * Port 0 Multicast Addresses: 128 Hashes (127-64)
13958          * Port 1 Multicast Addresses: 128 Hashes (191-128)
13959          */
13960
13961         config->hdr.length = 2;
13962         config->hdr.offset = port ? 32 : 0;
13963         config->hdr.client_id = BP_CL_ID(sc);
13964         config->hdr.reserved1 = 0;
13965
13966         /* Program the primary MAC address. */
13967         config_table = &config->config_table[0];
13968         eaddr = sc->link_params.mac_addr;
13969         config_table->cam_entry.msb_mac_addr = eaddr[0] << 8 | eaddr[1];
13970         config_table->cam_entry.middle_mac_addr = eaddr[2] << 8 | eaddr[3];
13971         config_table->cam_entry.lsb_mac_addr = eaddr[4] << 8 | eaddr[5];
13972         config_table->cam_entry.flags = htole16(port);
13973
13974         if (set)
13975                 config_table->target_table_entry.flags = 0;
13976         else
13977                 CAM_INVALIDATE(config_table);
13978
13979         config_table->target_table_entry.vlan_id = 0;
13980
13981         DBPRINT(sc, BXE_VERBOSE, "%s(): %s MAC (%04x:%04x:%04x)\n",
13982            __FUNCTION__, (set ? "Setting" : "Clearing"),
13983            config_table->cam_entry.msb_mac_addr,
13984            config_table->cam_entry.middle_mac_addr,
13985            config_table->cam_entry.lsb_mac_addr);
13986
13987         /* Program the broadcast MAC address. */
13988         config_table = &config->config_table[1];
13989         config_table->cam_entry.msb_mac_addr = 0xffff;
13990         config_table->cam_entry.middle_mac_addr = 0xffff;
13991         config_table->cam_entry.lsb_mac_addr = 0xffff;
13992         config_table->cam_entry.flags = htole16(port);
13993
13994         if (set)
13995                 config_table->target_table_entry.flags =
13996                     TSTORM_CAM_TARGET_TABLE_ENTRY_BROADCAST;
13997         else
13998                 CAM_INVALIDATE(config_table);
13999
14000         config_table->target_table_entry.vlan_id = 0;
14001
14002         /* Post the command to slow path queue. */
14003         bxe_sp_post(sc, RAMROD_CMD_ID_ETH_SET_MAC, 0,
14004             U64_HI(BXE_SP_MAPPING(sc, mac_config)),
14005             U64_LO(BXE_SP_MAPPING(sc, mac_config)), 0);
14006
14007         DBEXIT(BXE_VERBOSE_LOAD | BXE_VERBOSE_RESET | BXE_VERBOSE_UNLOAD);
14008 }
14009
14010 /*
14011  * Program the MAC address for 57711/57711E controllers.
14012  *
14013  * Returns:
14014  *   Nothing.
14015  */
14016 static void
14017 bxe_set_mac_addr_e1h(struct bxe_softc *sc, int set)
14018 {
14019         struct mac_configuration_cmd_e1h *config;
14020         struct mac_configuration_entry_e1h *config_table;
14021         uint8_t *eaddr;
14022         int func, port;
14023
14024         DBENTER(BXE_VERBOSE_LOAD | BXE_VERBOSE_RESET | BXE_VERBOSE_UNLOAD);
14025
14026         config = (struct mac_configuration_cmd_e1h *)BXE_SP(sc, mac_config);
14027         port = BP_PORT(sc);
14028         func = BP_FUNC(sc);
14029
14030         if (set && (sc->state != BXE_STATE_OPEN)) {
14031                 DBPRINT(sc, BXE_VERBOSE,
14032                     "%s(): Can't set E1H MAC in state 0x%08X!\n", __FUNCTION__,
14033                     sc->state);
14034                 goto bxe_set_mac_addr_e1h_exit;
14035         }
14036
14037         /*
14038          * CAM allocation:
14039          * Function 0-7 Unicast Addresses: 8 Perfect Match Filters
14040          * Multicast Addresses: 20 + FUNC * 20, 20 each (???)
14041          */
14042         config->hdr.length = 1;
14043         config->hdr.offset = func;
14044         config->hdr.client_id = 0xff;
14045         config->hdr.reserved1 = 0;
14046
14047         /* Program the primary MAC address. */
14048         config_table = &config->config_table[0];
14049         eaddr = sc->link_params.mac_addr;
14050         config_table->msb_mac_addr = eaddr[0] << 8 | eaddr[1];
14051         config_table->middle_mac_addr = eaddr[2] << 8 | eaddr[3];
14052         config_table->lsb_mac_addr = eaddr[4] << 8 | eaddr[5];
14053         config_table->clients_bit_vector = htole32(1 << sc->fp->cl_id);
14054
14055         config_table->vlan_id = 0;
14056         config_table->e1hov_id = htole16(sc->e1hov);
14057
14058         if (set)
14059                 config_table->flags = port;
14060         else
14061                 config_table->flags =
14062                         MAC_CONFIGURATION_ENTRY_E1H_ACTION_TYPE;
14063
14064         DBPRINT(sc, BXE_VERBOSE,
14065             "%s(): %s MAC (%04x:%04x:%04x), E1HOV = %d, CLID = %d\n",
14066             __FUNCTION__, (set ? "Setting" : "Clearing"),
14067             config_table->msb_mac_addr, config_table->middle_mac_addr,
14068             config_table->lsb_mac_addr, sc->e1hov, BP_L_ID(sc));
14069
14070         bxe_sp_post(sc, RAMROD_CMD_ID_ETH_SET_MAC, 0,
14071             U64_HI(BXE_SP_MAPPING(sc, mac_config)),
14072             U64_LO(BXE_SP_MAPPING(sc, mac_config)), 0);
14073
14074 bxe_set_mac_addr_e1h_exit:
14075         DBEXIT(BXE_VERBOSE_LOAD | BXE_VERBOSE_RESET | BXE_VERBOSE_UNLOAD);
14076 }
14077
14078 /*
14079  * Programs the various packet receive modes (broadcast and multicast).
14080  *
14081  * Returns:
14082  *   Nothing.
14083  */
14084
14085 static void
14086 bxe_set_rx_mode(struct bxe_softc *sc)
14087 {
14088         struct ifnet *ifp;
14089         struct ifmultiaddr *ifma;
14090         struct mac_configuration_cmd *config;
14091         struct mac_configuration_entry *config_table;
14092         uint32_t mc_filter[MC_HASH_SIZE];
14093         uint8_t *maddr;
14094         uint32_t crc, bit, regidx, rx_mode;
14095         int i, old, offset, port;
14096
14097         BXE_CORE_LOCK_ASSERT(sc);
14098
14099         rx_mode = BXE_RX_MODE_NORMAL;
14100         port = BP_PORT(sc);
14101
14102         DBENTER(BXE_VERBOSE_LOAD | BXE_VERBOSE_RESET);
14103
14104         if (sc->state != BXE_STATE_OPEN) {
14105                 DBPRINT(sc, BXE_WARN, "%s(): State (0x%08X) is not open!\n",
14106                     __FUNCTION__, sc->state);
14107                 goto bxe_set_rx_mode_exit;
14108         }
14109
14110         ifp = sc->bxe_ifp;
14111
14112         /*
14113          * Check for promiscuous, all multicast, or selected
14114          * multicast address filtering.
14115          */
14116         if (ifp->if_flags & IFF_PROMISC) {
14117                 /* Enable promiscuous mode. */
14118                 rx_mode = BXE_RX_MODE_PROMISC;
14119         } else if (ifp->if_flags & IFF_ALLMULTI ||
14120             ifp->if_amcount > BXE_MAX_MULTICAST) {
14121                 /* Enable all multicast addresses. */
14122                 rx_mode = BXE_RX_MODE_ALLMULTI;
14123         } else {
14124                 /* Enable selective multicast mode. */
14125                 if (CHIP_IS_E1(sc)) {
14126                         i = 0;
14127                         config = BXE_SP(sc, mcast_config);
14128
14129                         if_maddr_rlock(ifp);
14130
14131                         TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
14132                                 if (ifma->ifma_addr->sa_family != AF_LINK)
14133                                         continue;
14134                                 maddr = (uint8_t *)LLADDR(
14135                                     (struct sockaddr_dl *)ifma->ifma_addr);
14136                                 config_table = &config->config_table[i];
14137                                 config_table->cam_entry.msb_mac_addr =
14138                                     maddr[0] << 8 | maddr[1];
14139                                 config_table->cam_entry.middle_mac_addr =
14140                                     maddr[2] << 8 | maddr[3];
14141                                 config_table->cam_entry.lsb_mac_addr =
14142                                     maddr[4] << 8 | maddr[5];
14143                                 config_table->cam_entry.flags = htole16(port);
14144                                 config_table->target_table_entry.flags = 0;
14145                                 config_table->target_table_entry.
14146                                     clients_bit_vector =
14147                                     htole32(1 << BP_L_ID(sc));
14148                                 config_table->target_table_entry.vlan_id = 0;
14149                                 i++;
14150                                 DBPRINT(sc, BXE_INFO,
14151                         "%s(): Setting MCAST[%d] (%04X:%04X:%04X)\n",
14152                                     __FUNCTION__, i,
14153                                     config_table->cam_entry.msb_mac_addr,
14154                                     config_table->cam_entry.middle_mac_addr,
14155                                     config_table->cam_entry.lsb_mac_addr);
14156                         }
14157
14158                         if_maddr_runlock(ifp);
14159
14160                         old = config->hdr.length;
14161
14162                         /* Invalidate any extra MC entries in the CAM. */
14163                         if (old > i) {
14164                                 for (; i < old; i++) {
14165                                         config_table = &config->config_table[i];
14166                                         if (CAM_IS_INVALID(config_table))
14167                                                 break;
14168                                         /* Invalidate */
14169                                         CAM_INVALIDATE(config_table);
14170                                 }
14171                         }
14172
14173                         offset = BXE_MAX_MULTICAST * (1 + port);
14174                         config->hdr.length = i;
14175                         config->hdr.offset = offset;
14176                         config->hdr.client_id = sc->fp->cl_id;
14177                         config->hdr.reserved1 = 0;
14178                         wmb();
14179                         bxe_sp_post(sc, RAMROD_CMD_ID_ETH_SET_MAC, 0,
14180                             U64_HI(BXE_SP_MAPPING(sc, mcast_config)),
14181                             U64_LO(BXE_SP_MAPPING(sc, mcast_config)), 0);
14182                 } else { /* E1H */
14183                         /* Accept one or more multicasts */
14184                         memset(mc_filter, 0, 4 * MC_HASH_SIZE);
14185
14186                         if_maddr_rlock(ifp);
14187
14188                         TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
14189                                 if (ifma->ifma_addr->sa_family != AF_LINK)
14190                                         continue;
14191                                 crc = ether_crc32_le(ifma->ifma_addr->sa_data,
14192                                     ETHER_ADDR_LEN);
14193                                 bit = (crc >> 24) & 0xff;
14194                                 regidx = bit >> 5;
14195                                 bit &= 0x1f;
14196                                 mc_filter[regidx] |= (1 << bit);
14197                         }
14198                         if_maddr_runlock(ifp);
14199
14200                         for (i = 0; i < MC_HASH_SIZE; i++)
14201                                 REG_WR(sc, MC_HASH_OFFSET(sc, i), mc_filter[i]);
14202                 }
14203         }
14204
14205         DBPRINT(sc, BXE_VERBOSE, "%s(): Enabling new receive mode: 0x%08X\n",
14206             __FUNCTION__, rx_mode);
14207
14208         sc->rx_mode = rx_mode;
14209         bxe_set_storm_rx_mode(sc);
14210
14211 bxe_set_rx_mode_exit:
14212         DBEXIT(BXE_VERBOSE_LOAD | BXE_VERBOSE_RESET);
14213 }
14214
14215 /*
14216  * Function specific controller reset.
14217  *
14218  * Returns:
14219  *   Nothing.
14220  */
14221 static void
14222 bxe_reset_func(struct bxe_softc *sc)
14223 {
14224         int base, func, i, port;
14225
14226         DBENTER(BXE_VERBOSE_LOAD | BXE_VERBOSE_RESET | BXE_VERBOSE_UNLOAD);
14227
14228         port = BP_PORT(sc);
14229         func = BP_FUNC(sc);
14230
14231         /* Configure IGU. */
14232         REG_WR(sc, HC_REG_LEADING_EDGE_0 + port * 8, 0);
14233         REG_WR(sc, HC_REG_TRAILING_EDGE_0 + port * 8, 0);
14234         REG_WR(sc, HC_REG_CONFIG_0 + (port * 4), 0x1000);
14235
14236         /* Clear ILT. */
14237         base = FUNC_ILT_BASE(func);
14238         for (i = base; i < base + ILT_PER_FUNC; i++)
14239                 bxe_ilt_wr(sc, i, 0);
14240
14241         DBEXIT(BXE_VERBOSE_LOAD | BXE_VERBOSE_RESET | BXE_VERBOSE_UNLOAD);
14242 }
14243
14244 /*
14245  * Port specific controller reset.
14246  *
14247  * Returns:
14248  *   Nothing.
14249  */
14250 static void
14251 bxe_reset_port(struct bxe_softc *sc)
14252 {
14253         uint32_t val;
14254         int port;
14255
14256         DBENTER(BXE_VERBOSE_LOAD | BXE_VERBOSE_RESET | BXE_VERBOSE_UNLOAD);
14257
14258         port = BP_PORT(sc);
14259         REG_WR(sc, NIG_REG_MASK_INTERRUPT_PORT0 + port * 4, 0);
14260
14261         /* Do not receive packets to BRB. */
14262         REG_WR(sc, NIG_REG_LLH0_BRB1_DRV_MASK + port * 4, 0x0);
14263
14264         /* Do not direct receive packets that are not for MCP to the BRB. */
14265         REG_WR(sc, port ? NIG_REG_LLH1_BRB1_NOT_MCP :
14266             NIG_REG_LLH0_BRB1_NOT_MCP, 0x0);
14267
14268         /* Configure AEU. */
14269         REG_WR(sc, MISC_REG_AEU_MASK_ATTN_FUNC_0 + port * 4, 0);
14270
14271         DELAY(100000);
14272
14273         /* Check for BRB port occupancy. */
14274         val = REG_RD(sc, BRB1_REG_PORT_NUM_OCC_BLOCKS_0 + port * 4);
14275         if (val)
14276                 DBPRINT(sc, BXE_VERBOSE,
14277                     "%s(): BRB1 is not empty (%d blocks are occupied)!\n",
14278                     __FUNCTION__, val);
14279
14280         DBEXIT(BXE_VERBOSE_LOAD | BXE_VERBOSE_RESET | BXE_VERBOSE_UNLOAD);
14281 }
14282
14283 /*
14284  * Common controller reset.
14285  *
14286  * Returns:
14287  *   Nothing.
14288  */
14289 static void
14290 bxe_reset_common(struct bxe_softc *sc)
14291 {
14292
14293         DBENTER(BXE_VERBOSE_LOAD | BXE_VERBOSE_RESET | BXE_VERBOSE_UNLOAD);
14294
14295         REG_WR(sc, GRCBASE_MISC + MISC_REGISTERS_RESET_REG_1_CLEAR,
14296             0xd3ffff7f);
14297         REG_WR(sc, GRCBASE_MISC + MISC_REGISTERS_RESET_REG_2_CLEAR,
14298             0x1403);
14299
14300         DBEXIT(BXE_VERBOSE_LOAD | BXE_VERBOSE_RESET | BXE_VERBOSE_UNLOAD);
14301 }
14302
14303 /*
14304  * Reset the controller.
14305  *
14306  * Returns:
14307  *   Nothing.
14308  */
14309 static void
14310 bxe_reset_chip(struct bxe_softc *sc, uint32_t reset_code)
14311 {
14312
14313         DBENTER(BXE_VERBOSE_LOAD | BXE_VERBOSE_RESET | BXE_VERBOSE_UNLOAD);
14314
14315         switch (reset_code) {
14316         case FW_MSG_CODE_DRV_UNLOAD_COMMON:
14317                 bxe_reset_port(sc);
14318                 bxe_reset_func(sc);
14319                 bxe_reset_common(sc);
14320                 break;
14321         case FW_MSG_CODE_DRV_UNLOAD_PORT:
14322                 bxe_reset_port(sc);
14323                 bxe_reset_func(sc);
14324                 break;
14325         case FW_MSG_CODE_DRV_UNLOAD_FUNCTION:
14326                 bxe_reset_func(sc);
14327                 break;
14328         default:
14329                 BXE_PRINTF("%s(%d): Unknown reset code (0x%08X) from MCP!\n",
14330                     __FILE__, __LINE__, reset_code);
14331                 break;
14332         }
14333
14334         DBEXIT(BXE_VERBOSE_LOAD | BXE_VERBOSE_RESET | BXE_VERBOSE_UNLOAD);
14335 }
14336
14337 /*
14338  * Called by the OS to set media options (link, speed, etc.)
14339  * when the user specifies "ifconfig bxe media XXX" or
14340  * "ifconfig bxe mediaopt XXX".
14341  *
14342  * Returns:
14343  *   0 = Success, !0 = Failure
14344  */
14345 static int
14346 bxe_ifmedia_upd(struct ifnet *ifp)
14347 {
14348         struct bxe_softc *sc;
14349         struct ifmedia *ifm;
14350         int rc;
14351
14352         sc = ifp->if_softc;
14353         DBENTER(BXE_VERBOSE_PHY);
14354
14355         ifm = &sc->bxe_ifmedia;
14356         rc = 0;
14357
14358         /* We only support Ethernet media type. */
14359         if (IFM_TYPE(ifm->ifm_media) != IFM_ETHER) {
14360                 rc = EINVAL;
14361                 goto bxe_ifmedia_upd_exit;
14362         }
14363
14364         switch (IFM_SUBTYPE(ifm->ifm_media)) {
14365         case IFM_AUTO:
14366                 /* ToDo: What to do here? */
14367                 /* Doing nothing translates to success here. */
14368                  break;
14369         case IFM_10G_CX4:
14370                 /* Fall-through */
14371         case IFM_10G_SR:
14372                 /* Fall-through */
14373         case IFM_10G_T:
14374                 /* Fall-through */
14375         case IFM_10G_TWINAX:
14376                 /* Fall-through */
14377         default:
14378                 /* We don't support channging the media type. */
14379                 DBPRINT(sc, BXE_WARN, "%s(): Invalid media type!\n",
14380                     __FUNCTION__);
14381                 rc = EINVAL;
14382         }
14383
14384 bxe_ifmedia_upd_exit:
14385         DBENTER(BXE_VERBOSE_PHY);
14386         return (rc);
14387 }
14388
14389 /*
14390  * Called by the OS to report current media status
14391  * (link, speed, etc.).
14392  *
14393  * Returns:
14394  *   Nothing.
14395  */
14396 static void
14397 bxe_ifmedia_status(struct ifnet *ifp, struct ifmediareq *ifmr)
14398 {
14399         struct bxe_softc *sc;
14400
14401         sc = ifp->if_softc;
14402         DBENTER(BXE_EXTREME_LOAD | BXE_EXTREME_RESET);
14403
14404         /* Report link down if the driver isn't running. */
14405         if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) {
14406                 ifmr->ifm_active |= IFM_NONE;
14407                 goto bxe_ifmedia_status_exit;
14408         }
14409
14410         /* Setup the default interface info. */
14411         ifmr->ifm_status = IFM_AVALID;
14412         ifmr->ifm_active = IFM_ETHER;
14413
14414         if (sc->link_vars.link_up)
14415                 ifmr->ifm_status |= IFM_ACTIVE;
14416         else {
14417                 ifmr->ifm_active |= IFM_NONE;
14418                 goto bxe_ifmedia_status_exit;
14419         }
14420
14421         ifmr->ifm_active |= sc->media;
14422
14423         if (sc->link_vars.duplex == MEDIUM_FULL_DUPLEX)
14424                 ifmr->ifm_active |= IFM_FDX;
14425         else
14426                 ifmr->ifm_active |= IFM_HDX;
14427
14428 bxe_ifmedia_status_exit:
14429         DBEXIT(BXE_EXTREME_LOAD | BXE_EXTREME_RESET);
14430 }
14431
14432
14433 /*
14434  * Update last maximum scatter gather entry.
14435  *
14436  * Returns:
14437  *   None.
14438  */
14439 static __inline void
14440 bxe_update_last_max_sge(struct bxe_fastpath *fp, uint16_t index)
14441 {
14442         uint16_t last_max;
14443
14444         last_max = fp->last_max_sge;
14445         if (SUB_S16(index, last_max) > 0)
14446                 fp->last_max_sge = index;
14447 }
14448
14449 /*
14450  * Clear scatter gather mask next elements.
14451  *
14452  * Returns:
14453  *   None
14454  */
14455 static void
14456 bxe_clear_sge_mask_next_elems(struct bxe_fastpath *fp)
14457 {
14458         int i, index, j;
14459
14460         for (i = 0; i < NUM_RX_SGE_PAGES; i++) {
14461                 index = i * TOTAL_RX_SGE_PER_PAGE + USABLE_RX_SGE_PER_PAGE;
14462                 for (j = 0; j < 2; j++) {
14463                         SGE_MASK_CLEAR_BIT(fp, index);
14464                         index++;
14465                 }
14466         }
14467 }
14468
14469 /*
14470  * Update SGE producer.
14471  *
14472  * Returns:
14473  *   None.
14474  */
14475 static void
14476 bxe_update_sge_prod(struct bxe_fastpath *fp,
14477     struct eth_fast_path_rx_cqe *fp_cqe)
14478 {
14479         struct bxe_softc *sc;
14480         uint16_t delta, first_elem, last_max, last_elem, sge_len;
14481         int i;
14482
14483         sc = fp->sc;
14484         DBENTER(BXE_EXTREME_RECV);
14485
14486         delta = 0;
14487         sge_len = SGE_PAGE_ALIGN(le16toh(fp_cqe->pkt_len) -
14488             le16toh(fp_cqe->len_on_bd)) >> SGE_PAGE_SHIFT;
14489         if (!sge_len)
14490                 goto bxe_update_sge_prod_exit;
14491
14492         /* First mark all used pages. */
14493         for (i = 0; i < sge_len; i++)
14494                 SGE_MASK_CLEAR_BIT(fp, RX_SGE(le16toh(fp_cqe->sgl[i])));
14495
14496         /* Assume that the last SGE index is the biggest. */
14497         bxe_update_last_max_sge(fp, le16toh(fp_cqe->sgl[sge_len - 1]));
14498
14499         last_max = RX_SGE(fp->last_max_sge);
14500         last_elem = last_max >> RX_SGE_MASK_ELEM_SHIFT;
14501         first_elem = RX_SGE(fp->rx_sge_prod) >> RX_SGE_MASK_ELEM_SHIFT;
14502
14503         /* If ring is not full. */
14504         if (last_elem + 1 != first_elem)
14505                 last_elem++;
14506
14507         /* Now update the producer index. */
14508         for (i = first_elem; i != last_elem; i = NEXT_SGE_MASK_ELEM(i)) {
14509                 if (fp->rx_sge_mask[i])
14510                         break;
14511
14512                 fp->rx_sge_mask[i] = RX_SGE_MASK_ELEM_ONE_MASK;
14513                 delta += RX_SGE_MASK_ELEM_SZ;
14514         }
14515
14516         if (delta > 0) {
14517                 fp->rx_sge_prod += delta;
14518                 /* clear page-end entries */
14519                 bxe_clear_sge_mask_next_elems(fp);
14520         }
14521
14522 bxe_update_sge_prod_exit:
14523         DBEXIT(BXE_EXTREME_RECV);
14524 }
14525
14526 /*
14527  * Initialize scatter gather ring bitmask.
14528  *
14529  * Each entry in the SGE is associated with an aggregation in process.
14530  * Since there is no guarantee that all Ethernet frames associated with
14531  * a partciular TCP flow will arrive at the adapter and be placed into
14532  * the SGE chain contiguously, we maintain a bitmask for each SGE element
14533  * that identifies which aggregation an Ethernet frame belongs to.
14534  *
14535  * Returns:
14536  *   None
14537  */
14538 static __inline void
14539 bxe_init_sge_ring_bit_mask(struct bxe_fastpath *fp)
14540 {
14541
14542         /* Set the mask to all 1s, it's faster to compare to 0 than to 0xf. */
14543         memset(fp->rx_sge_mask, 0xff,
14544             (TOTAL_RX_SGE >> RX_SGE_MASK_ELEM_SHIFT) * sizeof(uint64_t));
14545
14546         /*
14547          * The SGE chain is formatted just like the RX chain.
14548          * The last two elements are reserved as a "next page pointer"
14549          * to the next page of SGE elements.  Clear the last two
14550          * elements in each SGE chain page since they will never be
14551          * used to track an aggregation.
14552          */
14553         bxe_clear_sge_mask_next_elems(fp);
14554 }
14555
14556 /*
14557  * The current mbuf is part of an aggregation.  Swap the mbuf into the TPA
14558  * aggregation queue, swap an empty mbuf back onto the receive chain, and
14559  * mark the current aggregation queue as in-progress.
14560  *
14561  * Returns:
14562  *   None.
14563  */
14564 static void
14565 bxe_tpa_start(struct bxe_fastpath *fp, uint16_t queue, uint16_t cons,
14566     uint16_t prod)
14567 {
14568         struct bxe_softc *sc;
14569         struct mbuf *m_temp;
14570         struct eth_rx_bd *rx_bd;
14571         bus_dmamap_t map_temp;
14572         int max_agg_queues;
14573
14574         sc = fp->sc;
14575         DBENTER(BXE_INSANE_RECV | BXE_INSANE_TPA);
14576
14577
14578
14579         DBPRINT(sc, BXE_EXTREME_TPA,
14580             "%s(): fp[%02d].tpa[%02d], cons=0x%04X, prod=0x%04X\n",
14581             __FUNCTION__, fp->index, queue, cons, prod);
14582
14583         max_agg_queues = CHIP_IS_E1(sc) ? ETH_MAX_AGGREGATION_QUEUES_E1 :
14584             ETH_MAX_AGGREGATION_QUEUES_E1H;
14585
14586         DBRUNIF((queue > max_agg_queues),
14587             BXE_PRINTF("%s(): fp[%02d] illegal aggregation (%d > %d)!\n",
14588             __FUNCTION__, fp->index, queue, max_agg_queues));
14589
14590         DBRUNIF((fp->tpa_state[queue] != BXE_TPA_STATE_STOP),
14591             BXE_PRINTF("%s(): Starting aggregation on "
14592             "fp[%02d].tpa[%02d] even though queue is not in the "
14593             "TPA_STOP state!\n", __FUNCTION__, fp->index, queue));
14594
14595         /* Remove the existing mbuf and mapping from the TPA pool. */
14596         m_temp = fp->tpa_mbuf_ptr[queue];
14597         map_temp = fp->tpa_mbuf_map[queue];
14598
14599         /* Only the paranoid survive! */
14600         if(m_temp == NULL) {
14601                 BXE_PRINTF("%s(%d): fp[%02d].tpa[%02d] not allocated!\n",
14602                     __FILE__, __LINE__, fp->index, queue);
14603                 /* ToDo: Additional error handling! */
14604                 goto bxe_tpa_start_exit;
14605         }
14606
14607         /* Move received mbuf and mapping to TPA pool. */
14608         fp->tpa_mbuf_ptr[queue] = fp->rx_mbuf_ptr[cons];
14609         fp->tpa_mbuf_map[queue] = fp->rx_mbuf_map[cons];
14610
14611         /* Place the TPA bin into the START state. */
14612         fp->tpa_state[queue] = BXE_TPA_STATE_START;
14613         DBRUN(fp->tpa_queue_used |= (1 << queue));
14614
14615         /* Get the rx_bd for the next open entry on the receive chain. */
14616         rx_bd = &fp->rx_chain[prod];
14617
14618         /* Update the rx_bd with the empty mbuf from the TPA pool. */
14619         rx_bd->addr_hi = htole32(U64_HI(fp->tpa_mbuf_segs[queue].ds_addr));
14620         rx_bd->addr_lo = htole32(U64_LO(fp->tpa_mbuf_segs[queue].ds_addr));
14621         fp->rx_mbuf_ptr[prod] = m_temp;
14622         fp->rx_mbuf_map[prod] = map_temp;
14623
14624 bxe_tpa_start_exit:
14625         DBEXIT(BXE_INSANE_RECV | BXE_INSANE_TPA);
14626 }
14627
14628 /*
14629  * When a TPA aggregation is completed, loop through the individual mbufs
14630  * of the aggregation, combining them into a single mbuf which will be sent
14631  * up the stack.  Refill all freed SGEs with mbufs as we go along.
14632  *
14633  * Returns:
14634  *   0 = Success, !0 = Failure.
14635  */
14636 static int
14637 bxe_fill_frag_mbuf(struct bxe_softc *sc, struct bxe_fastpath *fp,
14638     struct mbuf *m, struct eth_fast_path_rx_cqe *fp_cqe, uint16_t cqe_idx)
14639 {
14640         struct mbuf *m_frag;
14641         uint32_t frag_len, frag_size, pages, i;
14642         uint16_t sge_idx, len_on_bd;
14643         int j, rc;
14644
14645         DBENTER(BXE_EXTREME_RECV | BXE_EXTREME_TPA);
14646
14647         rc = 0;
14648         len_on_bd = le16toh(fp_cqe->len_on_bd);
14649         frag_size = le16toh(fp_cqe->pkt_len) - len_on_bd;
14650         pages = SGE_PAGE_ALIGN(frag_size) >> SGE_PAGE_SHIFT;
14651
14652         DBPRINT(sc, BXE_VERBOSE_TPA,
14653             "%s(): len_on_bd=%d, frag_size=%d, pages=%d\n",
14654             __FUNCTION__, len_on_bd, frag_size, pages);
14655
14656         /* Make sure the aggregated frame is not too big to handle. */
14657         if (pages > 8 * PAGES_PER_SGE) {
14658                 DBPRINT(sc, BXE_FATAL,
14659                     "%s(): fp[%02d].rx_sge[0x%04X] has too many pages (%d)!\n",
14660                     __FUNCTION__, fp->index, cqe_idx, pages);
14661                 DBPRINT(sc, BXE_FATAL,
14662                     "%s(): fp_cqe->pkt_len = %d fp_cqe->len_on_bd = %d\n",
14663                     __FUNCTION__, le16toh(fp_cqe->pkt_len), len_on_bd);
14664                 bxe_panic_dump(sc);
14665                 rc = EINVAL;
14666                 goto bxe_fill_frag_mbuf_exit;
14667         }
14668
14669         /*
14670          * Scan through the scatter gather list, pulling individual
14671          * mbufs into a single mbuf for the host stack.
14672          */
14673         for (i = 0, j = 0; i < pages; i += PAGES_PER_SGE, j++) {
14674                 sge_idx = RX_SGE(le16toh(fp_cqe->sgl[j]));
14675
14676                 /*
14677                  * Firmware gives the indices of the SGE as if the ring is an
14678                  * array (meaning that the "next" element will consume 2
14679                  * indices).
14680                  */
14681                 frag_len = min(frag_size, (uint32_t)(BCM_PAGE_SIZE *
14682                     PAGES_PER_SGE));
14683
14684                 DBPRINT(sc, BXE_VERBOSE_TPA,
14685                     "%s(): i=%d, j=%d, frag_size=%d, frag_len=%d\n",
14686                     __FUNCTION__, i, j, frag_size, frag_len);
14687
14688                 m_frag = fp->rx_sge_buf_ptr[sge_idx];
14689
14690                 /* Allocate a new mbuf for the SGE. */
14691                 rc = bxe_alloc_rx_sge_mbuf(fp, sge_idx);
14692                 if (rc) {
14693                         /*
14694                          * Leave all remaining SGEs in the ring.
14695                          */
14696                         goto bxe_fill_frag_mbuf_exit;
14697                 }
14698
14699                 /* Update the fragment its length. */
14700                 m_frag->m_len = frag_len;
14701
14702                 /* Concatenate the fragment to the head mbuf. */
14703                 m_cat(m, m_frag);
14704                 DBRUN(fp->sge_mbuf_alloc--);
14705
14706                 /* Update TPA mbuf size and remaining fragment size. */
14707                 m->m_pkthdr.len += frag_len;
14708                 frag_size -= frag_len;
14709         }
14710
14711 bxe_fill_frag_mbuf_exit:
14712         DBPRINT(sc, BXE_VERBOSE_TPA,
14713             "%s(): frag_size=%d\n", __FUNCTION__, frag_size);
14714         DBEXIT(BXE_EXTREME_RECV | BXE_EXTREME_TPA);
14715         return (rc);
14716 }
14717
14718 /*
14719  * The aggregation on the current TPA queue has completed.  Pull the
14720  * individual mbuf fragments together into a single mbuf, perform all
14721  * necessary checksum calculations, and send the resuting mbuf to the stack.
14722  *
14723  * Returns:
14724  *   None.
14725  */
14726 static void
14727 bxe_tpa_stop(struct bxe_softc *sc, struct bxe_fastpath *fp, uint16_t queue,
14728     int pad, int len, union eth_rx_cqe *cqe, uint16_t cqe_idx)
14729 {
14730         struct mbuf *m;
14731         struct ifnet *ifp;
14732         int rc;
14733
14734         DBENTER(BXE_INSANE_RECV | BXE_INSANE_TPA);
14735         DBPRINT(sc, (BXE_EXTREME_RECV | BXE_EXTREME_TPA),
14736             "%s(): fp[%02d].tpa[%02d], len=%d, pad=%d\n",
14737             __FUNCTION__, fp->index, queue, len, pad);
14738
14739         rc = 0;
14740         ifp = sc->bxe_ifp;
14741         m = fp->tpa_mbuf_ptr[queue];
14742
14743         /* Allocate a replacement before modifying existing mbuf. */
14744         rc = bxe_alloc_tpa_mbuf(fp, queue);
14745         if (rc) {
14746                 /* Drop the frame and log a soft error. */
14747                 fp->rx_soft_errors++;
14748                 goto bxe_tpa_stop_exit;
14749         }
14750
14751         /* We have a replacement, fixup the current mbuf. */
14752         m_adj(m, pad);
14753         m->m_pkthdr.len = m->m_len = len;
14754
14755         /* Mark the checksums valid (taken care of by firmware). */
14756         m->m_pkthdr.csum_flags |= CSUM_IP_CHECKED | CSUM_IP_VALID |
14757             CSUM_DATA_VALID | CSUM_PSEUDO_HDR;
14758         m->m_pkthdr.csum_data = 0xffff;
14759
14760         /* Aggregate all of the SGEs into a single mbuf. */
14761         rc = bxe_fill_frag_mbuf(sc, fp, m, &cqe->fast_path_cqe, cqe_idx);
14762         if (rc) {
14763                 /* Drop the packet and log an error. */
14764                 fp->rx_soft_errors++;
14765                 m_freem(m);
14766         } else  {
14767                 /* Find VLAN tag and send frame up to the stack. */
14768                 if ((le16toh(cqe->fast_path_cqe.pars_flags.flags) &
14769                     PARSING_FLAGS_VLAN)) {
14770                         m->m_pkthdr.ether_vtag =
14771                             cqe->fast_path_cqe.vlan_tag;
14772                         m->m_flags |= M_VLANTAG;
14773                 }
14774
14775                 /* Assign packet to the appropriate interface. */
14776                 m->m_pkthdr.rcvif = ifp;
14777
14778                 /* Update packet statistics. */
14779                 fp->rx_tpa_pkts++;
14780                 ifp->if_ipackets++;
14781
14782                 /* ToDo: Any potential locking issues here? */
14783                 /* Pass the frame to the stack. */
14784                 (*ifp->if_input)(ifp, m);
14785         }
14786
14787         /* We passed mbuf up the stack or dropped the frame. */
14788         DBRUN(fp->tpa_mbuf_alloc--);
14789
14790 bxe_tpa_stop_exit:
14791         fp->tpa_state[queue] = BXE_TPA_STATE_STOP;
14792         DBRUN(fp->tpa_queue_used &= ~(1 << queue));
14793         DBEXIT(BXE_INSANE_RECV | BXE_INSANE_TPA);
14794 }
14795
14796 /*
14797  * Notify the controller that the RX producer indices have been updated for
14798  * a fastpath connection by writing them to the controller.
14799  *
14800  * Returns:
14801  *   None
14802  */
14803 static __inline void
14804 bxe_update_rx_prod(struct bxe_softc *sc, struct bxe_fastpath *fp,
14805     uint16_t bd_prod, uint16_t cqe_prod, uint16_t sge_prod)
14806 {
14807         volatile struct ustorm_eth_rx_producers rx_prods = {0};
14808         int i;
14809
14810         /* Update producers. */
14811         rx_prods.bd_prod  =  bd_prod;
14812         rx_prods.cqe_prod = cqe_prod;
14813         rx_prods.sge_prod = sge_prod;
14814
14815         wmb();
14816
14817         for (i = 0; i < sizeof(struct ustorm_eth_rx_producers) / 4; i++){
14818                 REG_WR(sc, BAR_USTORM_INTMEM +
14819                     USTORM_RX_PRODS_OFFSET(BP_PORT(sc), fp->cl_id) + i * 4,
14820                     ((volatile uint32_t *) &rx_prods)[i]);
14821         }
14822
14823         DBPRINT(sc, BXE_EXTREME_RECV, "%s(%d): Wrote fp[%02d] bd_prod = 0x%04X, "
14824             "cqe_prod = 0x%04X, sge_prod = 0x%04X\n", __FUNCTION__, curcpu,
14825             fp->index, bd_prod, cqe_prod, sge_prod);
14826 }
14827
14828 /*
14829  * Processes received frames.
14830  *
14831  * Returns:
14832  *   Nothing.
14833  */
14834 static void
14835 bxe_rxeof(struct bxe_fastpath *fp)
14836 {
14837         struct bxe_softc *sc;
14838         struct ifnet *ifp;
14839         uint16_t rx_bd_cons, rx_bd_cons_idx;
14840         uint16_t rx_bd_prod, rx_bd_prod_idx;
14841         uint16_t rx_cq_cons, rx_cq_cons_idx;
14842         uint16_t rx_cq_prod, rx_cq_cons_sb;
14843         unsigned long rx_pkts = 0;
14844         int rc;
14845
14846         sc = fp->sc;
14847         ifp = sc->bxe_ifp;
14848
14849         DBENTER(BXE_EXTREME_RECV);
14850
14851         /* Get the status block's view of the RX completion consumer index. */
14852         rx_cq_cons_sb = bxe_rx_cq_cons(fp);
14853
14854         /*
14855          * Get working copies of the driver's view of the
14856          * RX indices. These are 16 bit values that are
14857          * expected to increment from 0 to 65535 and then
14858          * wrap-around to 0 again.
14859          */
14860         rx_bd_cons = fp->rx_bd_cons;
14861         rx_bd_prod = fp->rx_bd_prod;
14862         rx_cq_cons = fp->rx_cq_cons;
14863         rx_cq_prod = fp->rx_cq_prod;
14864
14865         DBPRINT(sc, (BXE_EXTREME_RECV),
14866             "%s(%d): BEFORE: fp[%02d], rx_bd_cons = 0x%04X, rx_bd_prod = 0x%04X, "
14867             "rx_cq_cons_sw = 0x%04X, rx_cq_prod_sw = 0x%04X\n", __FUNCTION__,
14868             curcpu, fp->index, rx_bd_cons, rx_bd_prod, rx_cq_cons, rx_cq_prod);
14869
14870         /*
14871          * Memory barrier to prevent speculative reads of the RX buffer
14872          * from getting ahead of the index in the status block.
14873          */
14874         rmb();
14875
14876         /*
14877          * Scan through the receive chain as long
14878          * as there is work to do.
14879          */
14880         while (rx_cq_cons != rx_cq_cons_sb) {
14881                 struct mbuf *m;
14882                 union eth_rx_cqe *cqe;
14883                 uint8_t cqe_fp_flags;
14884                 uint16_t len, pad;
14885
14886                 /*
14887                  * Convert the 16 bit indices used by hardware
14888                  * into array indices used by the driver.
14889                  */
14890                 rx_cq_cons_idx = RCQ_ENTRY(rx_cq_cons);
14891                 rx_bd_prod_idx = RX_BD(rx_bd_prod);
14892                 rx_bd_cons_idx = RX_BD(rx_bd_cons);
14893                 wmb();
14894
14895                 /* Fetch the completion queue entry (i.e. cookie). */
14896                 cqe = (union eth_rx_cqe *)
14897                     &fp->rcq_chain[rx_cq_cons_idx];
14898                 cqe_fp_flags = cqe->fast_path_cqe.type_error_flags;
14899
14900                 /* Sanity check the cookie flags. */
14901                 if (__predict_false(cqe_fp_flags == 0)) {
14902                         fp->rx_null_cqe_flags++;
14903                         DBRUN(bxe_dump_cqe(fp, rx_cq_cons_idx, cqe));
14904                         /* ToDo: What error handling can be done here? */
14905                 }
14906
14907                 /* Check the CQE type for slowpath or fastpath completion. */
14908                 if (__predict_false(CQE_TYPE(cqe_fp_flags) ==
14909                     RX_ETH_CQE_TYPE_ETH_RAMROD)) {
14910                         /* This is a slowpath completion. */
14911                         bxe_sp_event(fp, cqe);
14912                         goto bxe_rxeof_next_cqe;
14913
14914                 } else {
14915                         /* This is a fastpath completion. */
14916
14917                         /* Get the length and pad information from the CQE. */
14918                         len = le16toh(cqe->fast_path_cqe.pkt_len);
14919                         pad = cqe->fast_path_cqe.placement_offset;
14920
14921                         /* Check if the completion is for TPA. */
14922                         if ((fp->disable_tpa == FALSE) &&
14923                             (TPA_TYPE(cqe_fp_flags) !=
14924                             (TPA_TYPE_START | TPA_TYPE_END))) {
14925                                 uint16_t queue = cqe->fast_path_cqe.queue_index;
14926
14927                                 /*
14928                                  * No need to worry about error flags in
14929                                  * the frame as the firmware has already
14930                                  * managed that for us when aggregating
14931                                  * the frames.
14932                                  */
14933
14934                                 /* Check if TPA aggregation has started. */
14935                                 if (TPA_TYPE(cqe_fp_flags) == TPA_TYPE_START) {
14936                                         bxe_tpa_start(fp, queue, rx_bd_cons_idx,
14937                                             rx_bd_prod_idx);
14938                                         goto bxe_rxeof_next_rx;
14939                                 }
14940
14941                                 /* Check if TPA aggregation has completed. */
14942                                 if (TPA_TYPE(cqe_fp_flags) == TPA_TYPE_END) {
14943                                         DBRUNIF(!BXE_RX_SUM_FIX(cqe),
14944                                             DBPRINT(sc, BXE_FATAL,
14945                                             "%s(): STOP on non-TCP data.\n",
14946                                             __FUNCTION__));
14947
14948                                         /*
14949                                          * This is the size of the linear
14950                                          * data on this mbuf.
14951                                          */
14952                                         len = le16toh(cqe->fast_path_cqe.len_on_bd);
14953
14954                                         /*
14955                                          * Stop the aggregation and pass
14956                                          * the frame up.
14957                                          */
14958                                         bxe_tpa_stop(sc, fp, queue, pad, len,
14959                                             cqe, rx_cq_cons_idx);
14960                                         bxe_update_sge_prod(fp,
14961                                             &cqe->fast_path_cqe);
14962                                         goto bxe_rxeof_next_cqe;
14963                                 }
14964                         }
14965
14966                         m = fp->rx_mbuf_ptr[rx_bd_cons_idx];
14967
14968                         /* Allocate a replacement before modifying existing mbuf. */
14969                         rc = bxe_alloc_rx_bd_mbuf(fp, rx_bd_prod_idx);
14970                         if (rc) {
14971                                 /* Drop the frame and log a soft error. */
14972                                 fp->rx_soft_errors++;
14973                                 goto bxe_rxeof_next_rx;
14974                         }
14975
14976                         /* Check if the received frame has any errors. */
14977                         if (__predict_false(cqe_fp_flags &
14978                             ETH_RX_ERROR_FLAGS)) {
14979                                 DBPRINT(sc, BXE_WARN ,
14980                                     "%s(): fp[%02d].cqe[0x%04X] has errors "
14981                                     "(0x%08X)!\n", __FUNCTION__, fp->index,
14982                                     rx_cq_cons, cqe_fp_flags);
14983
14984                                 fp->rx_soft_errors++;
14985                                 goto bxe_rxeof_next_rx;
14986                         }
14987
14988                         /* We have a replacement, fixup the current mbuf. */
14989                         m_adj(m, pad);
14990                         m->m_pkthdr.len = m->m_len = len;
14991
14992                         /* Assign packet to the appropriate interface. */
14993                         m->m_pkthdr.rcvif = ifp;
14994
14995                         /* Assume no hardware checksum complated. */
14996                         m->m_pkthdr.csum_flags = 0;
14997
14998                         /* Validate checksum if offload enabled. */
14999                         if (ifp->if_capenable & IFCAP_RXCSUM) {
15000                                 /* Check whether IP checksummed or not. */
15001                                 if (sc->rx_csum &&
15002                                     !(cqe->fast_path_cqe.status_flags &
15003                                     ETH_FAST_PATH_RX_CQE_IP_XSUM_NO_VALIDATION_FLG)) {
15004                                         m->m_pkthdr.csum_flags |=
15005                                             CSUM_IP_CHECKED;
15006                                         if (__predict_false(cqe_fp_flags &
15007                                             ETH_FAST_PATH_RX_CQE_IP_BAD_XSUM_FLG)) {
15008                                                 DBPRINT(sc, BXE_WARN_SEND,
15009                                         "%s(): Invalid IP checksum!\n",
15010                                                     __FUNCTION__);
15011                                         } else
15012                                                 m->m_pkthdr.csum_flags |=
15013                                                     CSUM_IP_VALID;
15014                                 }
15015
15016                                 /* Check for a valid TCP/UDP frame. */
15017                                 if (sc->rx_csum &&
15018                                     !(cqe->fast_path_cqe.status_flags &
15019                                     ETH_FAST_PATH_RX_CQE_L4_XSUM_NO_VALIDATION_FLG)) {
15020                                         /* Check for a good TCP/UDP checksum. */
15021                                         if (__predict_false(cqe_fp_flags &
15022                                             ETH_FAST_PATH_RX_CQE_L4_BAD_XSUM_FLG)) {
15023                                                 DBPRINT(sc, BXE_VERBOSE_RECV,
15024                                         "%s(): Invalid TCP/UDP checksum!\n",
15025                                                     __FUNCTION__);
15026                                         } else {
15027                                                 m->m_pkthdr.csum_data = 0xFFFF;
15028                                                 m->m_pkthdr.csum_flags |=
15029                                                     (CSUM_DATA_VALID |
15030                                                     CSUM_PSEUDO_HDR);
15031                                         }
15032                                 }
15033                         }
15034
15035                         /*
15036                          * If we received a packet with a vlan tag,
15037                          * attach that information to the packet.
15038                          */
15039                         if (cqe->fast_path_cqe.pars_flags.flags &
15040                             PARSING_FLAGS_VLAN) {
15041                                 m->m_pkthdr.ether_vtag =
15042                                     cqe->fast_path_cqe.vlan_tag;
15043                                 m->m_flags |= M_VLANTAG;
15044                         }
15045
15046 #if __FreeBSD_version >= 800000
15047                         /* Tell OS what RSS queue was used for this flow. */
15048                         m->m_pkthdr.flowid = fp->index;
15049                         m->m_flags |= M_FLOWID;
15050 #endif
15051
15052                         /* Last chance to check for problems. */
15053                         DBRUN(bxe_validate_rx_packet(fp, rx_cq_cons, cqe, m));
15054
15055                         /* Update packet statistics. */
15056                         ifp->if_ipackets++;
15057                         rx_pkts++;
15058
15059                         /* ToDo: Any potential locking issues here? */
15060                         /* Pass the frame to the stack. */
15061                         (*ifp->if_input)(ifp, m);
15062
15063                         DBRUN(fp->rx_mbuf_alloc--);
15064                 }
15065
15066 bxe_rxeof_next_rx:
15067                 rx_bd_prod = NEXT_RX_BD(rx_bd_prod);
15068                 rx_bd_cons = NEXT_RX_BD(rx_bd_cons);
15069
15070 bxe_rxeof_next_cqe:
15071                 rx_cq_prod = NEXT_RCQ_IDX(rx_cq_prod);
15072                 rx_cq_cons = NEXT_RCQ_IDX(rx_cq_cons);
15073
15074                 /*
15075                  * Memory barrier to prevent speculative reads of the RX buffer
15076                  * from getting ahead of the index in the status block.
15077                  */
15078                 rmb();
15079         }
15080
15081         /* Update driver copy of the fastpath indices. */
15082         fp->rx_bd_cons = rx_bd_cons;
15083         fp->rx_bd_prod = rx_bd_prod;
15084         fp->rx_cq_cons = rx_cq_cons;
15085         fp->rx_cq_prod = rx_cq_prod;
15086
15087         DBPRINT(sc, (BXE_EXTREME_RECV),
15088             "%s(%d):  AFTER: fp[%02d], rx_bd_cons = 0x%04X, rx_bd_prod = 0x%04X, "
15089             "rx_cq_cons_sw = 0x%04X, rx_cq_prod_sw = 0x%04X\n", __FUNCTION__,
15090             curcpu, fp->index, rx_bd_cons, rx_bd_prod, rx_cq_cons, rx_cq_prod);
15091
15092         /* Update producers */
15093         bxe_update_rx_prod(sc, fp, fp->rx_bd_prod,
15094             fp->rx_cq_prod, fp->rx_sge_prod);
15095         bus_space_barrier(sc->bxe_btag, sc->bxe_bhandle, 0, 0,
15096             BUS_SPACE_BARRIER_READ);
15097
15098         fp->rx_pkts += rx_pkts;
15099         DBEXIT(BXE_EXTREME_RECV);
15100 }
15101
15102 /*
15103  * Processes transmit completions.
15104  *
15105  * Returns:
15106  *   Nothing.
15107  */
15108 static void
15109 bxe_txeof(struct bxe_fastpath *fp)
15110 {
15111         struct bxe_softc *sc;
15112         struct ifnet *ifp;
15113         struct eth_tx_start_bd *txbd;
15114         uint16_t hw_pkt_cons, sw_pkt_cons, sw_tx_bd_cons;
15115         uint16_t bd_index, pkt_index, nbds;
15116         int i;
15117
15118         sc = fp->sc;
15119         ifp = sc->bxe_ifp;
15120
15121         DBENTER(BXE_EXTREME_SEND);
15122
15123         /* Get the hardware's view of the TX packet consumer index. */
15124         hw_pkt_cons = le16toh(*fp->tx_pkt_cons_sb);
15125         sw_pkt_cons = fp->tx_pkt_cons;
15126         sw_tx_bd_cons = fp->tx_bd_cons;
15127
15128         /* Cycle through any completed TX chain page entries. */
15129         while (sw_pkt_cons != hw_pkt_cons) {
15130                 bd_index = TX_BD(sw_tx_bd_cons);
15131                 pkt_index = TX_BD(sw_pkt_cons);
15132
15133                 txbd = &fp->tx_chain[bd_index].start_bd;
15134                 nbds = txbd->nbd;
15135
15136                 /* Free the completed frame's mbuf. */
15137                 if (__predict_true(fp->tx_mbuf_ptr[pkt_index] != NULL)) {
15138                         /* Unmap the mbuf from non-paged memory. */
15139                         bus_dmamap_unload(fp->tx_mbuf_tag,
15140                             fp->tx_mbuf_map[pkt_index]);
15141
15142                         /* Return the mbuf to the system. */
15143                         m_freem(fp->tx_mbuf_ptr[pkt_index]);
15144                         fp->tx_mbuf_alloc--;
15145                         fp->tx_mbuf_ptr[pkt_index] = NULL;
15146                         fp->opackets++;
15147                 } else {
15148                         fp->tx_chain_lost_mbuf++;
15149                 }
15150
15151                 /* Updated packet consumer value. */
15152                 sw_pkt_cons++;
15153
15154                 /* Skip over the remaining used buffer descriptors. */
15155                 fp->tx_bd_used -= nbds;
15156                 for (i = 0; i < nbds; i++)
15157                         sw_tx_bd_cons = NEXT_TX_BD(sw_tx_bd_cons);
15158
15159                 /* Check for new work since we started. */
15160                 hw_pkt_cons = le16toh(*fp->tx_pkt_cons_sb);
15161                 rmb();
15162         }
15163
15164         /* Enable new transmits if we've made enough room. */
15165         if (fp->tx_bd_used < BXE_TX_CLEANUP_THRESHOLD) {
15166                 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
15167                 if (fp->tx_bd_used == 0) {
15168                         /*
15169                          * Clear the watchdog timer if we've emptied
15170                          * the TX chain.
15171                          */
15172                         fp->watchdog_timer = 0;
15173                 } else {
15174                         /*
15175                          * Reset the watchdog timer if we still have
15176                          * transmits pending.
15177                          */
15178                         fp->watchdog_timer = BXE_TX_TIMEOUT;
15179                 }
15180         }
15181
15182         /* Save our indices. */
15183         fp->tx_pkt_cons = sw_pkt_cons;
15184         fp->tx_bd_cons = sw_tx_bd_cons;
15185         DBEXIT(BXE_EXTREME_SEND);
15186 }
15187
15188 /*
15189  * Transmit timeout handler.
15190  *
15191  * Returns:
15192  *   0 = No timeout, !0 = timeout occurred.
15193  */
15194 static int
15195 bxe_watchdog(struct bxe_fastpath *fp)
15196 {
15197         struct bxe_softc *sc;
15198         int rc = 0;
15199
15200         sc = fp->sc;
15201         DBENTER(BXE_INSANE_SEND);
15202
15203         BXE_FP_LOCK(fp);
15204         if (fp->watchdog_timer == 0 || --fp->watchdog_timer) {
15205                 rc = EINVAL;
15206                 BXE_FP_UNLOCK(fp);
15207                 goto bxe_watchdog_exit;
15208         }
15209         BXE_FP_UNLOCK(fp);
15210
15211         BXE_PRINTF("TX watchdog timeout occurred on fp[%02d], "
15212             "resetting!\n", fp->index);
15213
15214         /* DBRUNLV(BXE_FATAL, bxe_breakpoint(sc)); */
15215
15216         BXE_CORE_LOCK(sc);
15217
15218         /* Mark the interface as down. */
15219         sc->bxe_ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
15220
15221         bxe_stop_locked(sc, UNLOAD_NORMAL);
15222         DELAY(10000);
15223         bxe_init_locked(sc, LOAD_OPEN);
15224
15225         BXE_CORE_UNLOCK(sc);
15226
15227 bxe_watchdog_exit:
15228         DBEXIT(BXE_INSANE_SEND);
15229         return (rc);
15230 }
15231
15232
15233 /*
15234  * The periodic timer tick routine.
15235  *
15236  * This code only runs when the interface is up.
15237  *
15238  * Returns:
15239  *   None
15240  */
15241 static void
15242 bxe_tick(void *xsc)
15243 {
15244         struct bxe_softc *sc;
15245         struct bxe_fastpath *fp;
15246 #if 0
15247         /* Re-enable at a later time. */
15248         uint32_t drv_pulse, mcp_pulse;
15249 #endif
15250         int i, func;
15251
15252         sc = xsc;
15253         DBENTER(BXE_INSANE_MISC);
15254
15255
15256         /* Check for TX timeouts on any fastpath. */
15257         for (i = 0; i < sc->num_queues; i++) {
15258                 fp = &sc->fp[i];
15259
15260                 if (bxe_watchdog(fp) != 0)
15261                         break;
15262         }
15263
15264         func = BP_FUNC(sc);
15265
15266         /* Schedule the next tick. */
15267         callout_reset(&sc->bxe_tick_callout, hz, bxe_tick, sc);
15268
15269 #if 0
15270         if (!NOMCP(sc)) {
15271                 func = BP_FUNC(sc);
15272
15273                 ++sc->fw_drv_pulse_wr_seq;
15274                 sc->fw_drv_pulse_wr_seq &= DRV_PULSE_SEQ_MASK;
15275
15276                 /* Let the MCP know we're alive. */
15277                 drv_pulse = sc->fw_drv_pulse_wr_seq;
15278                 SHMEM_WR(sc, func_mb[func].drv_pulse_mb, drv_pulse);
15279
15280                 /* Check if the MCP is still alive. */
15281                 mcp_pulse = (SHMEM_RD(sc, func_mb[func].mcp_pulse_mb) &
15282                     MCP_PULSE_SEQ_MASK);
15283
15284                 /*
15285                  * The delta between driver pulse and MCP response should be 1
15286                  * (before MCP response) or 0 (after MCP response).
15287                  */
15288                 if ((drv_pulse != mcp_pulse) && (drv_pulse != ((mcp_pulse + 1) &
15289                     MCP_PULSE_SEQ_MASK))) {
15290                         /* Someone's in cardiac arrest. */
15291                         DBPRINT(sc, BXE_WARN,
15292                             "%s(): drv_pulse (0x%x) != mcp_pulse (0x%x)\n",
15293                             __FUNCTION__, drv_pulse, mcp_pulse);
15294                 }
15295         }
15296 #endif
15297
15298         if ((sc->state == BXE_STATE_OPEN) || (sc->state == BXE_STATE_DISABLED))
15299                 bxe_stats_handle(sc, STATS_EVENT_UPDATE);
15300 }
15301
15302 #ifdef BXE_DEBUG
15303 /*
15304  * Allows the driver state to be dumped through the sysctl interface.
15305  *
15306  * Returns:
15307  *   0 for success, positive value for failure.
15308  */
15309 static int
15310 bxe_sysctl_driver_state(SYSCTL_HANDLER_ARGS)
15311 {
15312         struct bxe_softc *sc;
15313         struct bxe_fastpath *fp;
15314         int error, i, result;
15315
15316         sc = (struct bxe_softc *)arg1;
15317         result = -1;
15318         error = sysctl_handle_int(oidp, &result, 0, req);
15319         if (error || !req->newptr)
15320                 return (error);
15321
15322         if (result == 1) {
15323                 bxe_dump_driver_state(sc);
15324                 for (i = 0; i < sc->num_queues; i++) {
15325                         fp = &sc->fp[i];
15326                         bxe_dump_fp_state(fp);
15327                 }
15328                 bxe_dump_status_block(sc);
15329         }
15330
15331         return (error);
15332 }
15333
15334 /*
15335  * Allows the hardware state to be dumped through the sysctl interface.
15336  *
15337  * Returns:
15338  *   0 for success, positive value for failure.
15339  */
15340 static int
15341 bxe_sysctl_hw_state(SYSCTL_HANDLER_ARGS)
15342 {
15343         struct bxe_softc *sc;
15344         int error, result;
15345
15346         sc = (struct bxe_softc *)arg1;
15347         result = -1;
15348         error = sysctl_handle_int(oidp, &result, 0, req);
15349         if (error || !req->newptr)
15350                 return (error);
15351
15352         if (result == 1)
15353                 bxe_dump_hw_state(sc);
15354
15355         return (error);
15356 }
15357
15358 /*
15359  * Allows the MCP firmware to be dumped through the sysctl interface.
15360  *
15361  * Returns:
15362  *   0 for success, positive value for failure.
15363  */
15364 static int
15365 bxe_sysctl_dump_fw(SYSCTL_HANDLER_ARGS)
15366 {
15367         struct bxe_softc *sc;
15368         int error, result;
15369
15370         sc = (struct bxe_softc *)arg1;
15371         result = -1;
15372         error = sysctl_handle_int(oidp, &result, 0, req);
15373         if (error || !req->newptr)
15374                 return (error);
15375
15376         if (result == 1)
15377                 bxe_dump_fw(sc);
15378
15379         return (error);
15380 }
15381
15382 /*
15383  * Provides a sysctl interface to allow dumping the RX completion chain.
15384  *
15385  * Returns:
15386  *   0 for success, positive value for failure.
15387  */
15388 static int
15389 bxe_sysctl_dump_rx_cq_chain(SYSCTL_HANDLER_ARGS)
15390 {
15391         struct bxe_softc *sc;
15392         struct bxe_fastpath *fp;
15393         int error, result;
15394
15395         sc = (struct bxe_softc *)arg1;
15396         result = -1;
15397         error = sysctl_handle_int(oidp, &result, 0, req);
15398         if (error || !req->newptr)
15399                 return (error);
15400
15401         if ((result >= 0) && (result < sc->num_queues)) {
15402                 fp = &sc->fp[result];
15403                 bxe_dump_rx_cq_chain(fp, 0, TOTAL_RCQ_ENTRIES);
15404         }
15405
15406         return (error);
15407 }
15408
15409
15410 /*
15411  * Provides a sysctl interface to allow dumping the RX chain.
15412  *
15413  * Returns:
15414  *   0 for success, positive value for failure.
15415  */
15416 static int
15417 bxe_sysctl_dump_rx_bd_chain(SYSCTL_HANDLER_ARGS)
15418 {
15419         struct bxe_softc *sc;
15420         struct bxe_fastpath *fp;
15421         int error, result;
15422
15423         sc = (struct bxe_softc *)arg1;
15424         result = -1;
15425         error = sysctl_handle_int(oidp, &result, 0, req);
15426         if (error || !req->newptr)
15427                 return (error);
15428
15429         if ((result >= 0) && (result < sc->num_queues)) {
15430                 fp = &sc->fp[result];
15431                 bxe_dump_rx_bd_chain(fp, 0, TOTAL_RX_BD);
15432         }
15433
15434         return (error);
15435 }
15436
15437 /*
15438 * Provides a sysctl interface to allow dumping the TX chain.
15439 *
15440 * Returns:
15441 *   0 for success, positive value for failure.
15442 */
15443 static int
15444 bxe_sysctl_dump_tx_chain(SYSCTL_HANDLER_ARGS)
15445 {
15446         struct bxe_softc *sc;
15447         struct bxe_fastpath *fp;
15448         int error, result;
15449
15450         sc = (struct bxe_softc *)arg1;
15451         result = -1;
15452         error = sysctl_handle_int(oidp, &result, 0, req);
15453         if (error || !req->newptr)
15454                 return (error);
15455
15456         if ((result >= 0) && (result < sc->num_queues)) {
15457                 fp = &sc->fp[result];
15458                 bxe_dump_tx_chain(fp, 0, TOTAL_TX_BD);
15459         }
15460
15461         return (error);
15462 }
15463
15464 /*
15465  * Provides a sysctl interface to allow reading arbitrary registers in the
15466  * device.  DO NOT ENABLE ON PRODUCTION SYSTEMS!
15467  *
15468  * Returns:
15469  *   0 for success, positive value for failure.
15470  */
15471 static int
15472 bxe_sysctl_reg_read(SYSCTL_HANDLER_ARGS)
15473 {
15474         struct bxe_softc *sc;
15475         uint32_t result, val;
15476         int error;
15477
15478         sc = (struct bxe_softc *)arg1;
15479         result = -1;
15480         error = sysctl_handle_int(oidp, &result, 0, req);
15481         if (error || (req->newptr == NULL))
15482                 return (error);
15483
15484         val = REG_RD(sc, result);
15485         BXE_PRINTF("reg 0x%08X = 0x%08X\n", result, val);
15486
15487         return (error);
15488 }
15489
15490 /*
15491 * Provides a sysctl interface to allow generating a grcdump.
15492 *
15493 * Returns:
15494 *   0 for success, positive value for failure.
15495 */
15496 static int
15497 bxe_sysctl_grcdump(SYSCTL_HANDLER_ARGS)
15498 {
15499         struct bxe_softc *sc;
15500         int error, result;
15501
15502         sc = (struct bxe_softc *)arg1;
15503         result = -1;
15504         error = sysctl_handle_int(oidp, &result, 0, req);
15505         if (error || !req->newptr)
15506                 return (error);
15507
15508         if (result == 1) {
15509                 /* Generate a grcdump and log the contents.*/
15510                 bxe_grcdump(sc, 1);
15511         } else {
15512                 /* Generate a grcdump and don't log the contents. */
15513                 bxe_grcdump(sc, 0);
15514         }
15515
15516         return (error);
15517 }
15518
15519 /*
15520  * Provides a sysctl interface to forcing the driver to dump state and
15521  * enter the debugger.  DO NOT ENABLE ON PRODUCTION SYSTEMS!
15522  *
15523  * Returns:
15524  *   0 for success, positive value for failure.
15525  */
15526 static int
15527 bxe_sysctl_breakpoint(SYSCTL_HANDLER_ARGS)
15528 {
15529         struct bxe_softc *sc;
15530         int error, result;
15531
15532         result = -1;
15533         error = sysctl_handle_int(oidp, &result, 0, req);
15534         if (error || !req->newptr)
15535                 return (error);
15536
15537         if (result == 1) {
15538                 sc = (struct bxe_softc *)arg1;
15539                 bxe_breakpoint(sc);
15540         }
15541
15542         return (error);
15543 }
15544 #endif
15545
15546 /*
15547  * Adds any sysctl parameters for tuning or debugging purposes.
15548  *
15549  * Returns:
15550  *   None.
15551  */
15552 static void
15553 bxe_add_sysctls(struct bxe_softc *sc)
15554 {
15555         struct sysctl_ctx_list *ctx =
15556             device_get_sysctl_ctx(sc->dev);
15557         struct sysctl_oid_list *children =
15558             SYSCTL_CHILDREN(device_get_sysctl_tree(sc->dev));
15559         struct bxe_port_stats *estats = &sc->eth_stats;
15560
15561         SYSCTL_ADD_UINT(ctx, children, OID_AUTO,
15562             "estats_total_bytes_received_hi",
15563             CTLFLAG_RD, &estats->total_bytes_received_hi,
15564             0, "Total bytes received (hi)");
15565
15566         SYSCTL_ADD_UINT(ctx, children, OID_AUTO,
15567             "estats_total_bytes_received_lo",
15568             CTLFLAG_RD, &estats->total_bytes_received_lo,
15569             0, "Total bytes received (lo)");
15570
15571         SYSCTL_ADD_UINT(ctx, children, OID_AUTO,
15572            "estats_valid_bytes_received_hi",
15573            CTLFLAG_RD, &estats->valid_bytes_received_hi,
15574            0, "Valid bytes received (hi)");
15575
15576         SYSCTL_ADD_UINT(ctx, children, OID_AUTO,
15577             "estats_valid_bytes_received_lo",
15578             CTLFLAG_RD, &estats->valid_bytes_received_lo,
15579             0, "Valid bytes received (lo)");
15580
15581         SYSCTL_ADD_UINT(ctx, children, OID_AUTO,
15582             "estats_total_unicast_packets_received_hi",
15583             CTLFLAG_RD, &estats->total_unicast_packets_received_hi,
15584             0, "Total unicast packets received (hi)");
15585
15586         SYSCTL_ADD_UINT(ctx, children, OID_AUTO,
15587             "estats_total_unicast_packets_received_lo",
15588             CTLFLAG_RD, &estats->total_unicast_packets_received_lo,
15589             0, "Total unicast packets received (lo)");
15590
15591         SYSCTL_ADD_UINT(ctx, children, OID_AUTO,
15592             "estats_total_bytes_transmitted_hi",
15593             CTLFLAG_RD, &estats->total_bytes_transmitted_hi,
15594             0, "Total bytes transmitted (hi)");
15595
15596         SYSCTL_ADD_UINT(ctx, children, OID_AUTO,
15597             "estats_total_bytes_transmitted_lo",
15598             CTLFLAG_RD, &estats->total_bytes_transmitted_lo,
15599             0, "Total bytes transmitted (lo)");
15600
15601         SYSCTL_ADD_UINT(ctx, children, OID_AUTO,
15602             "estats_total_unicast_packets_transmitted_hi",
15603             CTLFLAG_RD, &estats->total_unicast_packets_transmitted_hi,
15604             0, "Total unicast packets transmitted (hi)");
15605
15606         SYSCTL_ADD_UINT(ctx, children, OID_AUTO,
15607             "estats_total_unicast_packets_transmitted_lo",
15608             CTLFLAG_RD, &estats->total_unicast_packets_transmitted_lo,
15609             0, "Total unicast packets transmitted (lo)");
15610
15611         SYSCTL_ADD_UINT(ctx, children, OID_AUTO,
15612             "estats_total_broadcast_packets_received_lo",
15613             CTLFLAG_RD, &estats->total_broadcast_packets_received_lo,
15614             0, "Total broadcast packets received (lo)");
15615
15616         SYSCTL_ADD_UINT(ctx, children, OID_AUTO,
15617             "estats_total_broadcast_packets_transmitted_lo",
15618             CTLFLAG_RD, &estats->total_broadcast_packets_transmitted_lo,
15619             0, "Total broadcast packets transmitted (lo)");
15620
15621         SYSCTL_ADD_UINT(ctx, children, OID_AUTO,
15622             "estats_total_multicast_packets_received_lo",
15623             CTLFLAG_RD, &estats->total_multicast_packets_received_lo,
15624             0, "Total multicast packets received (lo)");
15625
15626         SYSCTL_ADD_UINT(ctx, children, OID_AUTO,
15627             "estats_total_multicast_packets_transmitted_lo",
15628             CTLFLAG_RD, &estats->total_multicast_packets_transmitted_lo,
15629             0, "Total multicast packets transmitted (lo)");
15630
15631         SYSCTL_ADD_UINT(ctx, children, OID_AUTO,
15632             "tx_stat_etherstatspkts64octets_hi",
15633             CTLFLAG_RD, &estats->tx_stat_etherstatspkts64octets_hi,
15634             0, "Total 64 byte packets transmitted (hi)");
15635
15636         /* ToDo: Fix for 64 bit access. */
15637         SYSCTL_ADD_UINT(ctx, children, OID_AUTO,
15638             "tx_stat_etherstatspkts64octets_lo",
15639             CTLFLAG_RD, &estats->tx_stat_etherstatspkts64octets_lo,
15640             0, "Total 64 byte packets transmitted (lo)");
15641
15642         SYSCTL_ADD_UINT(ctx, children, OID_AUTO,
15643             "driver_xoff",
15644             CTLFLAG_RD, &estats->driver_xoff,
15645             0, "Driver transmit queue full count");
15646
15647         SYSCTL_ADD_ULONG(ctx, children, OID_AUTO,
15648             "tx_start_called_with_link_down",
15649             CTLFLAG_RD, &sc->tx_start_called_with_link_down,
15650             "TX start routine called while link down count");
15651
15652         SYSCTL_ADD_ULONG(ctx, children, OID_AUTO,
15653             "tx_start_called_with_queue_full",
15654             CTLFLAG_RD, &sc->tx_start_called_with_queue_full,
15655             "TX start routine called with queue full count");
15656
15657         /* ToDo: Add more statistics here. */
15658
15659 #ifdef BXE_DEBUG
15660         SYSCTL_ADD_INT(ctx, children, OID_AUTO, "bxe_debug",
15661             CTLFLAG_RW, &bxe_debug, 0,
15662             "Debug message level flag");
15663 #endif
15664
15665         do {
15666 #define QUEUE_NAME_LEN 32
15667                 char namebuf[QUEUE_NAME_LEN];
15668                 struct sysctl_oid *queue_node;
15669                 struct sysctl_oid_list *queue_list;
15670
15671                 for (int i = 0; i < sc->num_queues; i++) {
15672                         struct bxe_fastpath *fp = &sc->fp[i];
15673                         snprintf(namebuf, QUEUE_NAME_LEN, "fp[%02d]", i);
15674
15675                         queue_node = SYSCTL_ADD_NODE(ctx, children, OID_AUTO,
15676                             namebuf, CTLFLAG_RD, NULL, "Queue Name");
15677                         queue_list = SYSCTL_CHILDREN(queue_node);
15678
15679                         /*
15680                          * Receive related fastpath statistics.*
15681                          */
15682                         SYSCTL_ADD_ULONG(ctx, queue_list, OID_AUTO,
15683                             "rx_pkts",
15684                             CTLFLAG_RD, &fp->rx_pkts,
15685                             "Received packets");
15686
15687                         SYSCTL_ADD_ULONG(ctx, queue_list, OID_AUTO,
15688                             "rx_tpa_pkts",
15689                             CTLFLAG_RD, &fp->rx_tpa_pkts,
15690                             "Received TPA packets");
15691
15692                         SYSCTL_ADD_ULONG(ctx, queue_list, OID_AUTO,
15693                             "rx_null_cqe_flags",
15694                             CTLFLAG_RD, &fp->rx_null_cqe_flags,
15695                             "CQEs with NULL flags count");
15696
15697                         SYSCTL_ADD_ULONG(ctx, queue_list, OID_AUTO,
15698                             "rx_soft_errors",
15699                             CTLFLAG_RD, &fp->rx_soft_errors,
15700                             "Received frames dropped by driver count");
15701
15702                         /*
15703                          * Transmit related fastpath statistics.*
15704                          */
15705                         SYSCTL_ADD_ULONG(ctx, queue_list, OID_AUTO,
15706                             "tx_pkts",
15707                             CTLFLAG_RD, &fp->tx_pkts,
15708                             "Transmitted packets");
15709
15710                         SYSCTL_ADD_ULONG(ctx, queue_list, OID_AUTO,
15711                             "tx_soft_errors",
15712                             CTLFLAG_RD, &fp->tx_soft_errors,
15713                             "Transmit frames dropped by driver count");
15714
15715                         SYSCTL_ADD_ULONG(ctx, queue_list, OID_AUTO,
15716                             "tx_offload_frames_csum_ip",
15717                             CTLFLAG_RD, &fp->tx_offload_frames_csum_ip,
15718                             "IP checksum offload frame count");
15719
15720                         SYSCTL_ADD_ULONG(ctx, queue_list, OID_AUTO,
15721                             "tx_offload_frames_csum_tcp",
15722                             CTLFLAG_RD, &fp->tx_offload_frames_csum_tcp,
15723                             "TCP checksum offload frame count");
15724
15725                         SYSCTL_ADD_ULONG(ctx, queue_list, OID_AUTO,
15726                             "tx_offload_frames_csum_udp",
15727                             CTLFLAG_RD, &fp->tx_offload_frames_csum_udp,
15728                             "UDP checksum offload frame count");
15729
15730                         SYSCTL_ADD_ULONG(ctx, queue_list, OID_AUTO,
15731                             "tx_offload_frames_tso",
15732                             CTLFLAG_RD, &fp->tx_offload_frames_tso,
15733                             "TSO offload frame count");
15734
15735                         SYSCTL_ADD_ULONG(ctx, queue_list, OID_AUTO,
15736                             "tx_header_splits",
15737                             CTLFLAG_RD, &fp->tx_header_splits,
15738                             "TSO frame header/data split count");
15739
15740                         SYSCTL_ADD_ULONG(ctx, queue_list, OID_AUTO,
15741                             "tx_encap_failures",
15742                             CTLFLAG_RD, &fp->tx_encap_failures,
15743                             "TX encapsulation failure count");
15744
15745                         SYSCTL_ADD_ULONG(ctx, queue_list, OID_AUTO,
15746                             "tx_hw_queue_full",
15747                             CTLFLAG_RD, &fp->tx_hw_queue_full,
15748                             "TX H/W queue too full to add a frame count");
15749
15750                         SYSCTL_ADD_ULONG(ctx, queue_list, OID_AUTO,
15751                             "tx_hw_max_queue_depth",
15752                             CTLFLAG_RD, &fp->tx_hw_max_queue_depth,
15753                             "TX H/W maximum queue depth count");
15754
15755                         SYSCTL_ADD_ULONG(ctx, queue_list, OID_AUTO,
15756                             "tx_dma_mapping_failure",
15757                             CTLFLAG_RD, &fp->tx_dma_mapping_failure,
15758                             "TX DMA mapping failure");
15759
15760                         SYSCTL_ADD_INT(ctx, queue_list, OID_AUTO,
15761                             "tx_max_drbr_queue_depth",
15762                             CTLFLAG_RD, &fp->tx_max_drbr_queue_depth,
15763                             0, "TX S/W queue maximum depth");
15764
15765                         SYSCTL_ADD_ULONG(ctx, queue_list, OID_AUTO,
15766                             "tx_window_violation_std",
15767                             CTLFLAG_RD, &fp->tx_window_violation_std,
15768                             "Standard frame TX BD window violation count");
15769
15770                         SYSCTL_ADD_ULONG(ctx, queue_list, OID_AUTO,
15771                             "tx_window_violation_tso",
15772                             CTLFLAG_RD, &fp->tx_window_violation_tso,
15773                             "TSO frame TX BD window violation count");
15774
15775                         SYSCTL_ADD_ULONG(ctx, queue_list, OID_AUTO,
15776                             "tx_unsupported_tso_request_ipv6",
15777                             CTLFLAG_RD, &fp->tx_unsupported_tso_request_ipv6,
15778                             "TSO frames with unsupported IPv6 protocol count");
15779
15780                         SYSCTL_ADD_ULONG(ctx, queue_list, OID_AUTO,
15781                             "tx_unsupported_tso_request_not_tcp",
15782                             CTLFLAG_RD, &fp->tx_unsupported_tso_request_not_tcp,
15783                             "TSO frames with unsupported protocol count");
15784
15785                         SYSCTL_ADD_ULONG(ctx, queue_list, OID_AUTO,
15786                             "tx_chain_lost_mbuf",
15787                             CTLFLAG_RD, &fp->tx_chain_lost_mbuf,
15788                             "Mbufs lost on TX chain count");
15789
15790                         SYSCTL_ADD_ULONG(ctx, queue_list, OID_AUTO,
15791                             "tx_frame_deferred",
15792                             CTLFLAG_RD, &fp->tx_frame_deferred,
15793                             "TX frame deferred from H/W queue to S/W queue count");
15794
15795                         SYSCTL_ADD_ULONG(ctx, queue_list, OID_AUTO,
15796                             "tx_queue_xoff",
15797                             CTLFLAG_RD, &fp->tx_queue_xoff,
15798                             "TX queue full count");
15799
15800                         /*
15801                          * Memory related fastpath statistics.*
15802                          */
15803                         SYSCTL_ADD_ULONG(ctx, queue_list, OID_AUTO,
15804                             "mbuf_rx_bd_alloc_failed",
15805                             CTLFLAG_RD, &fp->mbuf_rx_bd_alloc_failed,
15806                             "RX BD mbuf allocation failure count");
15807
15808                         SYSCTL_ADD_ULONG(ctx, queue_list, OID_AUTO,
15809                             "mbuf_rx_bd_mapping_failed",
15810                             CTLFLAG_RD, &fp->mbuf_rx_bd_mapping_failed,
15811                             "RX BD mbuf mapping failure count");
15812
15813                         SYSCTL_ADD_ULONG(ctx, queue_list, OID_AUTO,
15814                             "mbuf_tpa_alloc_failed",
15815                             CTLFLAG_RD, &fp->mbuf_tpa_alloc_failed,
15816                             "TPA mbuf allocation failure count");
15817
15818                         SYSCTL_ADD_ULONG(ctx, queue_list, OID_AUTO,
15819                             "mbuf_tpa_mapping_failed",
15820                             CTLFLAG_RD, &fp->mbuf_tpa_mapping_failed,
15821                             "TPA mbuf mapping failure count");
15822
15823                         SYSCTL_ADD_ULONG(ctx, queue_list, OID_AUTO,
15824                             "mbuf_sge_alloc_failed",
15825                             CTLFLAG_RD, &fp->mbuf_sge_alloc_failed,
15826                             "SGE mbuf allocation failure count");
15827
15828                         SYSCTL_ADD_ULONG(ctx, queue_list, OID_AUTO,
15829                             "mbuf_sge_mapping_failed",
15830                             CTLFLAG_RD, &fp->mbuf_sge_mapping_failed,
15831                             "SGE mbuf mapping failure count");
15832
15833                         SYSCTL_ADD_ULONG(ctx, queue_list, OID_AUTO,
15834                             "mbuf_defrag_attempts",
15835                             CTLFLAG_RD, &fp->mbuf_defrag_attempts,
15836                             "Mbuf defrag attempt count");
15837
15838                         SYSCTL_ADD_ULONG(ctx, queue_list, OID_AUTO,
15839                             "mbuf_defrag_failures",
15840                             CTLFLAG_RD, &fp->mbuf_defrag_failures,
15841                             "Mbuf defrag failure count");
15842                 }
15843         } while (0);
15844
15845
15846 #ifdef BXE_DEBUG
15847         SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "driver_state",
15848             CTLTYPE_INT | CTLFLAG_RW, (void *)sc, 0,
15849             bxe_sysctl_driver_state, "I", "Drive state information");
15850
15851         SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "hw_state",
15852             CTLTYPE_INT | CTLFLAG_RW, (void *)sc, 0,
15853             bxe_sysctl_hw_state, "I", "Hardware state information");
15854
15855         SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "dump_fw",
15856             CTLTYPE_INT | CTLFLAG_RW, (void *)sc, 0,
15857             bxe_sysctl_dump_fw, "I", "Dump MCP firmware");
15858
15859         SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "dump_rx_bd_chain",
15860             CTLTYPE_INT | CTLFLAG_RW, (void *)sc, 0,
15861             bxe_sysctl_dump_rx_bd_chain, "I", "Dump rx_bd chain");
15862
15863         SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "dump_rx_cq_chain",
15864             CTLTYPE_INT | CTLFLAG_RW, (void *)sc, 0,
15865             bxe_sysctl_dump_rx_cq_chain, "I", "Dump cqe chain");
15866
15867         SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "dump_tx_chain",
15868             CTLTYPE_INT | CTLFLAG_RW, (void *)sc, 0,
15869             bxe_sysctl_dump_tx_chain, "I", "Dump tx_bd chain");
15870
15871         /*
15872          * Generates a GRCdump (run sysctl dev.bxe.0.grcdump=0
15873          * before accessing buffer below).
15874          */
15875         SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "grcdump",
15876             CTLTYPE_INT | CTLFLAG_RW, (void *)sc, 0, bxe_sysctl_grcdump,
15877             "I", "Initiate a grcdump operation");
15878
15879         /*
15880          * Hidden sysctl.
15881          *  Use "sysctl -b dev.bxe.0.grcdump_buffer > buf.bin".
15882          */
15883         SYSCTL_ADD_OPAQUE(ctx, children, OID_AUTO, "grcdump_buffer",
15884             CTLFLAG_RD | CTLFLAG_SKIP, sc->grcdump_buffer,
15885             BXE_GRCDUMP_BUF_SIZE, "IU", "Access grcdump buffer");
15886
15887         SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "breakpoint",
15888             CTLTYPE_INT | CTLFLAG_RW, (void *)sc, 0,
15889             bxe_sysctl_breakpoint, "I", "Driver breakpoint");
15890
15891         SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "reg_read",
15892             CTLTYPE_INT | CTLFLAG_RW, (void *)sc, 0,
15893             bxe_sysctl_reg_read, "I", "Register read");
15894
15895 #endif /* BXE_DEBUG */
15896 }
15897
15898 /*
15899  * BXE Debug Routines
15900  */
15901 #ifdef BXE_DEBUG
15902 /*
15903  * Writes out the header for the debug dump buffer.
15904  *
15905  * Returns:
15906  *       None.
15907  *
15908  * Modifies:
15909  *   index
15910  */
15911 static void
15912 bxe_dump_debug_header(struct bxe_softc *sc, uint32_t *index)
15913 {
15914         struct hd_param hd_param_cu = {0};
15915         uint32_t *buf;
15916
15917         buf = sc->grcdump_buffer;
15918         if (CHIP_IS_E1H(sc))
15919                 hd_param_cu = hd_param_e1h;
15920         else
15921                 hd_param_cu = hd_param_e1;
15922
15923         buf[(*index)++] = hd_param_cu.time_stamp;
15924         buf[(*index)++] = hd_param_cu.diag_ver;
15925         buf[(*index)++] = hd_param_cu.grc_dump_ver;
15926
15927         buf[(*index)++] = REG_RD_IND(sc, XSTORM_WAITP_ADDRESS);
15928         buf[(*index)++] = REG_RD_IND(sc, TSTORM_WAITP_ADDRESS);
15929         buf[(*index)++] = REG_RD_IND(sc, USTORM_WAITP_ADDRESS);
15930         buf[(*index)++] = REG_RD_IND(sc, CSTORM_WAITP_ADDRESS);
15931
15932         /* The size of the header is stored at the first DWORD. */
15933         buf[0] = (*index) - 1;
15934 }
15935
15936
15937 /*
15938  * Writes to the controller to prepare it for a dump.
15939  *
15940  * Returns:
15941  *       None.
15942  *
15943  * Modifies:
15944  *   None.
15945  */
15946 static void
15947 bxe_dump_debug_writes(struct bxe_softc *sc)
15948 {
15949         uint32_t write_val;
15950
15951         write_val = 1;
15952         /* Halt the STORMs to get a consistent device state. */
15953         REG_WR_IND(sc, XSTORM_WAITP_ADDRESS, write_val);
15954         REG_WR_IND(sc, TSTORM_WAITP_ADDRESS, write_val);
15955         REG_WR_IND(sc, USTORM_WAITP_ADDRESS, write_val);
15956         REG_WR_IND(sc, CSTORM_WAITP_ADDRESS, write_val);
15957
15958         if (CHIP_IS_E1H(sc))
15959                 REG_WR_IND(sc, TSTORM_CAM_MODE, write_val);
15960 }
15961
15962
15963 /*
15964  * Cycles through the required register reads and dumps them
15965  * to the debug buffer.
15966  *
15967  * Returns:
15968  *       None.
15969  *
15970  * Modifies:
15971  *   index
15972  */
15973 static void
15974 bxe_dump_debug_reg_read(struct bxe_softc *sc, uint32_t *index)
15975 {
15976         preg_addr preg_addrs;
15977         uint32_t regs_count, *buf;
15978         uint32_t i, reg_addrs_index;
15979
15980         buf = sc->grcdump_buffer;
15981         preg_addrs = NULL;
15982
15983         /* Read different registers for different controllers. */
15984         if (CHIP_IS_E1H(sc)) {
15985                 regs_count = regs_count_e1h;
15986                 preg_addrs = &reg_addrs_e1h[0];
15987         } else {
15988                 regs_count = regs_count_e1;
15989                 preg_addrs = &reg_addrs_e1[0];
15990         }
15991
15992         /* ToDo: Add a buffer size check. */
15993         for (reg_addrs_index = 0; reg_addrs_index < regs_count;
15994             reg_addrs_index++) {
15995                 for (i = 0; i < preg_addrs[reg_addrs_index].size; i++) {
15996                         buf[(*index)++] = REG_RD_IND(sc,
15997                             preg_addrs[reg_addrs_index].addr + (i * 4));
15998                 }
15999         }
16000 }
16001
16002 /*
16003  * Cycles through the required wide register reads and dumps them
16004  * to the debug buffer.
16005  *
16006  * Returns:
16007  *   None.
16008  */
16009 static void
16010 bxe_dump_debug_reg_wread(struct bxe_softc *sc, uint32_t *index)
16011 {
16012         pwreg_addr pwreg_addrs;
16013         uint32_t reg_addrs_index, reg_add_read, reg_add_count;
16014         uint32_t *buf, cam_index, wregs_count;
16015
16016         buf = sc->grcdump_buffer;
16017         pwreg_addrs = NULL;
16018
16019         /* Read different registers for different controllers. */
16020         if (CHIP_IS_E1H(sc)) {
16021                 wregs_count = wregs_count_e1h;
16022                 pwreg_addrs = &wreg_addrs_e1h[0];
16023         } else {
16024                 wregs_count = wregs_count_e1;
16025                 pwreg_addrs = &wreg_addrs_e1[0];
16026         }
16027
16028         for (reg_addrs_index = 0; reg_addrs_index < wregs_count;
16029             reg_addrs_index++) {
16030                 reg_add_read = pwreg_addrs[reg_addrs_index].addr;
16031                 for (reg_add_count = 0; reg_add_count <
16032                     pwreg_addrs[reg_addrs_index].size; reg_add_count++) {
16033                         buf[(*index)++] = REG_RD_IND(sc, reg_add_read);
16034                         reg_add_read += sizeof(uint32_t);
16035
16036                         for (cam_index = 0; cam_index <
16037                             pwreg_addrs[reg_addrs_index].const_regs_count;
16038                             cam_index++)
16039                                 buf[(*index)++] = REG_RD_IND(sc,
16040                                     pwreg_addrs[reg_addrs_index].const_regs[cam_index]);
16041                 }
16042         }
16043 }
16044
16045 /*
16046  * Performs a debug dump for offline diagnostics.
16047  *
16048  * Note that when this routine is called the STORM
16049  * processors will be stopped in order to create a
16050  * cohesive dump.  The controller will need to be
16051  * reset before the device can begin passing traffic
16052  * again.
16053  *
16054  * Returns:
16055  *   None.
16056  */
16057 static void
16058 bxe_grcdump(struct bxe_softc *sc, int log)
16059 {
16060         uint32_t *buf, i, index;
16061
16062         index = 1;
16063         buf = sc->grcdump_buffer;
16064         if (buf != NULL) {
16065
16066                 /* Write the header and regsiters contents to the dump buffer. */
16067                 bxe_dump_debug_header(sc, &index);
16068                 bxe_dump_debug_writes(sc);
16069                 bxe_dump_debug_reg_read(sc,&index);
16070                 bxe_dump_debug_reg_wread(sc, &index);
16071
16072                 /* Print the results to the system log is necessary. */
16073                 if (log) {
16074                         BXE_PRINTF(
16075                             "-----------------------------"
16076                             "    grcdump   "
16077                             "-----------------------------\n");
16078                         BXE_PRINTF("Buffer length = 0x%08X bytes\n", index * 4);
16079
16080                         for (i = 0; i < index; i += 8) {
16081                                 BXE_PRINTF(
16082                                     "0x%08X - 0x%08X 0x%08X 0x%08X 0x%08X "
16083                                     "0x%08X 0x%08X 0x%08X 0x%08X\n", i * 4,
16084                                     buf[i + 0], buf[i + 1], buf[i + 2],
16085                                     buf[i + 3], buf[i + 4], buf[i + 5],
16086                                     buf[i + 6], buf[i + 7]);
16087                         }
16088
16089                         BXE_PRINTF(
16090                             "-----------------------------"
16091                             "--------------"
16092                             "-----------------------------\n");
16093                 }
16094         } else {
16095                 BXE_PRINTF("No grcdump buffer allocated!\n");
16096         }
16097 }
16098
16099 /*
16100  * Check that an Etherent frame is valid and prints out debug info if it's
16101  * not.
16102  *
16103  * Returns:
16104  *   Nothing.
16105  */
16106 static __noinline
16107 void bxe_validate_rx_packet(struct bxe_fastpath *fp, uint16_t comp_cons,
16108     union eth_rx_cqe *cqe, struct mbuf *m)
16109 {
16110         struct bxe_softc *sc;
16111         int error;
16112
16113         sc = fp->sc;
16114
16115         /* Check that the mbuf is sane. */
16116         error = m_sanity(m, FALSE);
16117         if (error != 1 || ((m->m_len < ETHER_HDR_LEN) |
16118             (m->m_len > ETH_MAX_JUMBO_PACKET_SIZE + ETH_OVREHEAD))) {
16119                 m_print(m, 128);
16120                 bxe_dump_enet(sc, m);
16121                 bxe_dump_cqe(fp, comp_cons, cqe);
16122                 /* Make sure the packet has a valid length. */
16123         }
16124 }
16125
16126 /*
16127  * Prints out Ethernet frame information from an mbuf.
16128  *
16129  * Partially decode an Ethernet frame to look at some important headers.
16130  *
16131  * Returns:
16132  *   Nothing.
16133  */
16134 static __noinline
16135 void bxe_dump_enet(struct bxe_softc *sc, struct mbuf *m)
16136 {
16137         struct ether_vlan_header *eh;
16138         uint16_t etype;
16139         int e_hlen;
16140         struct ip *ip;
16141         struct tcphdr *th;
16142         struct udphdr *uh;
16143         struct arphdr *ah;
16144
16145         BXE_PRINTF(
16146             "-----------------------------"
16147             " Frame Decode "
16148             "-----------------------------\n");
16149
16150         eh = mtod(m, struct ether_vlan_header *);
16151
16152         /* Handle VLAN encapsulation if present. */
16153         if (eh->evl_encap_proto == htons(ETHERTYPE_VLAN)) {
16154                 etype = ntohs(eh->evl_proto);
16155                 e_hlen = ETHER_HDR_LEN + ETHER_VLAN_ENCAP_LEN;
16156         } else {
16157                 etype = ntohs(eh->evl_encap_proto);
16158                 e_hlen = ETHER_HDR_LEN;
16159         }
16160
16161         BXE_PRINTF("enet: dest = %6D, src = %6D, type = 0x%04X, e_hlen = %d\n",
16162             eh->evl_dhost, ":", eh->evl_shost, ":", etype, e_hlen);
16163
16164         switch (etype) {
16165         case ETHERTYPE_IP:
16166                 ip = (struct ip *)(m->m_data + e_hlen);
16167                 BXE_PRINTF(
16168                     "--ip: dest = 0x%08X , src = 0x%08X, "
16169                     "ip_hlen = %d bytes, len = %d bytes, protocol = 0x%02X, "
16170                     "ip_id = 0x%04X, csum = 0x%04X\n",
16171                     ntohl(ip->ip_dst.s_addr), ntohl(ip->ip_src.s_addr),
16172                     (ip->ip_hl << 2), ntohs(ip->ip_len), ip->ip_p,
16173                     ntohs(ip->ip_id), ntohs(ip->ip_sum));
16174
16175                 switch (ip->ip_p) {
16176                 case IPPROTO_TCP:
16177                         th = (struct tcphdr *)((caddr_t)ip + (ip->ip_hl << 2));
16178                         BXE_PRINTF(
16179                             "-tcp: dest = %d, src = %d, tcp_hlen = %d "
16180                             "bytes, flags = 0x%b, csum = 0x%04X\n",
16181                             ntohs(th->th_dport), ntohs(th->th_sport),
16182                             (th->th_off << 2), th->th_flags,
16183                             "\20\10CWR\07ECE\06URG\05ACK\04PSH\03RST\02SYN\01FIN",
16184                             ntohs(th->th_sum));
16185                         break;
16186                 case IPPROTO_UDP:
16187                         uh = (struct udphdr *)((caddr_t)ip + (ip->ip_hl << 2));
16188                         BXE_PRINTF(
16189                             "-udp: dest = %d, src = %d, udp_hlen = %d "
16190                             "bytes, len = %d bytes, csum = 0x%04X\n",
16191                             ntohs(uh->uh_dport), ntohs(uh->uh_sport),
16192                             (int)sizeof(struct udphdr), ntohs(uh->uh_ulen),
16193                             ntohs(uh->uh_sum));
16194                         break;
16195                 case IPPROTO_ICMP:
16196                         BXE_PRINTF("icmp:\n");
16197                         break;
16198                 default:
16199                         BXE_PRINTF("----: Other IP protocol.\n");
16200                 }
16201                 break;
16202         case ETHERTYPE_IPV6:
16203                 /* ToDo: Add IPv6 support. */
16204                 BXE_PRINTF("IPv6 not supported!.\n");
16205                 break;
16206         case ETHERTYPE_ARP:
16207                 BXE_PRINTF("-arp: ");
16208                 ah = (struct arphdr *) (m->m_data + e_hlen);
16209                 switch (ntohs(ah->ar_op)) {
16210                 case ARPOP_REVREQUEST:
16211                         printf("reverse ARP request\n");
16212                         break;
16213                 case ARPOP_REVREPLY:
16214                         printf("reverse ARP reply\n");
16215                         break;
16216                 case ARPOP_REQUEST:
16217                         printf("ARP request\n");
16218                         break;
16219                 case ARPOP_REPLY:
16220                         printf("ARP reply\n");
16221                         break;
16222                 default:
16223                         printf("other ARP operation\n");
16224                 }
16225                 break;
16226         default:
16227                 BXE_PRINTF("----: Other protocol.\n");
16228         }
16229
16230         BXE_PRINTF(
16231            "-----------------------------"
16232            "--------------"
16233            "-----------------------------\n");
16234 }
16235
16236 #if 0
16237 static void
16238 bxe_dump_mbuf_data(struct mbuf *m, int len)
16239 {
16240         uint8_t *ptr;
16241         int i;
16242
16243         ptr = mtod(m, uint8_t *);
16244         printf("\nmbuf->m_data:");
16245         printf("\n0x");
16246         for (i = 0; i < len; i++){
16247                 if (i != 0 && i % 40 == 0)
16248                         printf("\n0x");
16249                 else if (i != 0 && i % 6 == 0)
16250                         printf(" 0x");
16251                 printf("%02x", *ptr++);
16252         }
16253         printf("\n\n");
16254 }
16255 #endif
16256
16257
16258 /*
16259  * Prints out information about an mbuf.
16260  *
16261  * Returns:
16262  *   Nothing.
16263  */
16264 static __noinline
16265 void bxe_dump_mbuf(struct bxe_softc *sc, struct mbuf *m)
16266 {
16267         if (m == NULL) {
16268                 BXE_PRINTF("mbuf: null pointer\n");
16269                 return;
16270         }
16271
16272         while (m) {
16273                 BXE_PRINTF("mbuf: %p, m_len = %d, m_flags = 0x%b, "
16274                     "m_data = %p\n", m, m->m_len, m->m_flags,
16275                     "\20\1M_EXT\2M_PKTHDR\3M_EOR\4M_RDONLY", m->m_data);
16276
16277                 if (m->m_flags & M_PKTHDR) {
16278                          BXE_PRINTF("- m_pkthdr: len = %d, flags = 0x%b, "
16279                             "csum_flags = %b\n", m->m_pkthdr.len,
16280                             m->m_flags, "\20\12M_BCAST\13M_MCAST\14M_FRAG"
16281                             "\15M_FIRSTFRAG\16M_LASTFRAG\21M_VLANTAG"
16282                             "\22M_PROMISC\23M_NOFREE",
16283                             m->m_pkthdr.csum_flags,
16284                             "\20\1CSUM_IP\2CSUM_TCP\3CSUM_UDP"
16285                             "\5CSUM_FRAGMENT\6CSUM_TSO\11CSUM_IP_CHECKED"
16286                             "\12CSUM_IP_VALID\13CSUM_DATA_VALID"
16287                             "\14CSUM_PSEUDO_HDR");
16288                 }
16289
16290                 if (m->m_flags & M_EXT) {
16291                         BXE_PRINTF("- m_ext: %p, ext_size = %d, type = ",
16292                             m->m_ext.ext_buf, m->m_ext.ext_size);
16293                         switch (m->m_ext.ext_type) {
16294                         case EXT_CLUSTER:
16295                                 printf("EXT_CLUSTER\n"); break;
16296                         case EXT_SFBUF:
16297                                 printf("EXT_SFBUF\n"); break;
16298                         case EXT_JUMBO9:
16299                                 printf("EXT_JUMBO9\n"); break;
16300                         case EXT_JUMBO16:
16301                                 printf("EXT_JUMBO16\n"); break;
16302                         case EXT_PACKET:
16303                                 printf("EXT_PACKET\n"); break;
16304                         case EXT_MBUF:
16305                                 printf("EXT_MBUF\n"); break;
16306                         case EXT_NET_DRV:
16307                                 printf("EXT_NET_DRV\n"); break;
16308                         case EXT_MOD_TYPE:
16309                                 printf("EXT_MOD_TYPE\n"); break;
16310                         case EXT_DISPOSABLE:
16311                                 printf("EXT_DISPOSABLE\n"); break;
16312                         case EXT_EXTREF:
16313                                 printf("EXT_EXTREF\n"); break;
16314                         default:
16315                                 printf("UNKNOWN\n");
16316                         }
16317                 }
16318
16319                 m = m->m_next;
16320         }
16321 }
16322
16323 /*
16324  * Prints out information about an rx_bd.
16325  *
16326  * Returns:
16327  *   Nothing.
16328  */
16329 static __noinline
16330 void bxe_dump_rxbd(struct bxe_fastpath *fp, int idx,
16331     struct eth_rx_bd *rx_bd)
16332 {
16333         struct bxe_softc *sc;
16334
16335         sc = fp->sc;
16336
16337         /* Check if index out of range. */
16338         if (idx > MAX_RX_BD) {
16339                 BXE_PRINTF("fp[%02d].rx_bd[0x%04X] XX: Invalid rx_bd index!\n",
16340                     fp->index, idx);
16341         } else if ((idx & RX_BD_PER_PAGE_MASK) >= USABLE_RX_BD_PER_PAGE) {
16342                 /* RX Chain page pointer. */
16343                 BXE_PRINTF("fp[%02d].rx_bd[0x%04X] NP: haddr=0x%08X:%08X\n",
16344                     fp->index, idx, rx_bd->addr_hi, rx_bd->addr_lo);
16345         } else {
16346                 BXE_PRINTF("fp[%02d].rx_bd[0x%04X] RX: haddr=0x%08X:%08X\n",
16347                     fp->index, idx, rx_bd->addr_hi, rx_bd->addr_lo);
16348         }
16349 }
16350
16351 /*
16352  * Prints out a completion queue entry.
16353  *
16354  * Returns:
16355  *   Nothing.
16356  */
16357 static __noinline
16358 void bxe_dump_cqe(struct bxe_fastpath *fp, int idx,
16359     union eth_rx_cqe *cqe)
16360 {
16361         struct bxe_softc *sc;
16362
16363         sc = fp->sc;
16364
16365         if (idx > MAX_RCQ_ENTRIES) {
16366                 /* Index out of range. */
16367                 BXE_PRINTF("fp[%02d].rx_cqe[0x%04X]: Invalid rx_cqe index!\n",
16368                     fp->index, idx);
16369         } else if ((idx & USABLE_RCQ_ENTRIES_PER_PAGE) ==
16370             USABLE_RCQ_ENTRIES_PER_PAGE) {
16371                 /* CQE next page pointer. */
16372                 BXE_PRINTF("fp[%02d].rx_cqe[0x%04X] NP: haddr=0x%08X:%08X\n",
16373                     fp->index, idx,
16374                     le32toh(cqe->next_page_cqe.addr_hi),
16375                     le32toh(cqe->next_page_cqe.addr_lo));
16376         } else {
16377                 /* Normal CQE. */
16378                 BXE_PRINTF("fp[%02d].rx_cqe[0x%04X] CQ: error_flags=0x%b, "
16379                     "pkt_len=0x%04X, status_flags=0x%02X, vlan=0x%04X "
16380                     "rss_hash=0x%08X\n", fp->index, idx,
16381                     cqe->fast_path_cqe.type_error_flags,
16382                     BXE_ETH_FAST_PATH_RX_CQE_ERROR_FLAGS_PRINTFB,
16383                     le16toh(cqe->fast_path_cqe.pkt_len),
16384                     cqe->fast_path_cqe.status_flags,
16385                     le16toh(cqe->fast_path_cqe.vlan_tag),
16386                     le32toh(cqe->fast_path_cqe.rss_hash_result));
16387         }
16388 }
16389
16390 /*
16391  * Prints out information about a TX parsing BD.
16392  *
16393  * Returns:
16394  *   Nothing.
16395  */
16396 static __noinline
16397 void bxe_dump_tx_parsing_bd(struct bxe_fastpath *fp, int idx,
16398     struct eth_tx_parse_bd *p_bd)
16399 {
16400         struct bxe_softc *sc;
16401
16402         sc = fp->sc;
16403
16404         if (idx > MAX_TX_BD){
16405                 /* Index out of range. */
16406                 BXE_PRINTF("fp[%02d].tx_bd[0x%04X] XX: Invalid tx_bd index!\n",
16407                    fp->index, idx);
16408         } else {
16409                 BXE_PRINTF("fp[%02d]:tx_bd[0x%04X] PB: global_data=0x%b, "
16410                     "tcp_flags=0x%b, ip_hlen=%04d, total_hlen=%04d, "
16411                     "tcp_pseudo_csum=0x%04X, lso_mss=0x%04X, ip_id=0x%04X, "
16412                     "tcp_send_seq=0x%08X\n", fp->index, idx,
16413                     p_bd->global_data, BXE_ETH_TX_PARSE_BD_GLOBAL_DATA_PRINTFB,
16414                     p_bd->tcp_flags, BXE_ETH_TX_PARSE_BD_TCP_FLAGS_PRINTFB,
16415                     p_bd->ip_hlen, p_bd->total_hlen, p_bd->tcp_pseudo_csum,
16416                     p_bd->lso_mss, p_bd->ip_id, p_bd->tcp_send_seq);
16417         }
16418 }
16419
16420 /*
16421  * Prints out information about a tx_bd.
16422  *
16423  * Returns:
16424  *   Nothing.
16425  */
16426 static __noinline
16427 void bxe_dump_txbd(struct bxe_fastpath *fp, int idx,
16428     union eth_tx_bd_types *tx_bd)
16429 {
16430         struct bxe_softc *sc;
16431
16432         sc = fp->sc;
16433
16434         if (idx > MAX_TX_BD){
16435                 /* Index out of range. */
16436                 BXE_PRINTF("fp[%02d]:tx_bd[0x%04X] XX: Invalid tx_bd index!\n",
16437                     fp->index, idx);
16438         } else if ((idx & USABLE_TX_BD_PER_PAGE) == USABLE_TX_BD_PER_PAGE) {
16439                 /* TX next page BD. */
16440                 BXE_PRINTF("fp[%02d]:tx_bd[0x%04X] NP: haddr=0x%08X:%08X\n",
16441                     fp->index, idx,     tx_bd->next_bd.addr_hi,
16442                     tx_bd->next_bd.addr_lo);
16443         } else if ((tx_bd->start_bd.bd_flags.as_bitfield &
16444             ETH_TX_BD_FLAGS_START_BD) != 0) {
16445                 /* TX start BD. */
16446                 BXE_PRINTF("fp[%02d]:tx_bd[0x%04X] ST: haddr=0x%08X:%08X, "
16447                     "nbd=%02d, nbytes=%05d, vlan/idx=0x%04X, flags=0x%b, "
16448                     "gendata=0x%02X\n",
16449                     fp->index, idx, tx_bd->start_bd.addr_hi,
16450                     tx_bd->start_bd.addr_lo, tx_bd->start_bd.nbd,
16451                     tx_bd->start_bd.nbytes, tx_bd->start_bd.vlan,
16452                     tx_bd->start_bd.bd_flags.as_bitfield,
16453                     BXE_ETH_TX_BD_FLAGS_PRINTFB,
16454                     tx_bd->start_bd.general_data);
16455         } else {
16456                 /* Regular TX BD. */
16457                 BXE_PRINTF("fp[%02d]:tx_bd[0x%04X] TX: haddr=0x%08X:%08X, "
16458                     "total_pkt_bytes=%05d, nbytes=%05d\n", fp->index, idx,
16459                     tx_bd->reg_bd.addr_hi, tx_bd->reg_bd.addr_lo,
16460                     tx_bd->reg_bd.total_pkt_bytes, tx_bd->reg_bd.nbytes);
16461         }
16462 }
16463
16464
16465 /*
16466  * Prints out the transmit chain.
16467  *
16468  * Returns:
16469  *   Nothing.
16470  */
16471 static __noinline
16472 void bxe_dump_tx_chain(struct bxe_fastpath * fp, int tx_bd_prod, int count)
16473 {
16474         struct bxe_softc *sc;
16475         union eth_tx_bd_types *tx_bd;
16476         uint32_t val_hi, val_lo;
16477         int i, parsing_bd = 0;
16478
16479         sc = fp->sc;
16480
16481         /* First some info about the tx_bd chain structure. */
16482         BXE_PRINTF(
16483             "----------------------------"
16484             "  tx_bd chain "
16485             "----------------------------\n");
16486
16487         val_hi = U64_HI(fp->tx_dma.paddr);
16488         val_lo = U64_LO(fp->tx_dma.paddr);
16489         BXE_PRINTF(
16490             "0x%08X:%08X - (fp[%02d]->tx_dma.paddr) TX Chain physical address\n",
16491             val_hi, val_lo, fp->index);
16492         BXE_PRINTF(
16493             "page size      = 0x%08X, tx chain pages        = 0x%08X\n",
16494             (uint32_t)BCM_PAGE_SIZE, (uint32_t)NUM_TX_PAGES);
16495         BXE_PRINTF(
16496             "tx_bd per page = 0x%08X, usable tx_bd per page = 0x%08X\n",
16497             (uint32_t)TOTAL_TX_BD_PER_PAGE, (uint32_t)USABLE_TX_BD_PER_PAGE);
16498         BXE_PRINTF(
16499             "total tx_bd    = 0x%08X\n", (uint32_t)TOTAL_TX_BD);
16500
16501         BXE_PRINTF(
16502             "-----------------------------"
16503             "  tx_bd data  "
16504             "-----------------------------\n");
16505
16506         /* Now print out the tx_bd's themselves. */
16507         for (i = 0; i < count; i++) {
16508                 tx_bd = &fp->tx_chain[tx_bd_prod];
16509                 if (parsing_bd) {
16510                         struct eth_tx_parse_bd *p_bd;
16511                         p_bd = (struct eth_tx_parse_bd *)
16512                             &fp->tx_chain[tx_bd_prod].parse_bd;
16513                         bxe_dump_tx_parsing_bd(fp, tx_bd_prod, p_bd);
16514                         parsing_bd = 0;
16515                 } else {
16516                         bxe_dump_txbd(fp, tx_bd_prod, tx_bd);
16517                         if ((tx_bd->start_bd.bd_flags.as_bitfield &
16518                             ETH_TX_BD_FLAGS_START_BD) != 0)
16519                                 /*
16520                                  * There is always a parsing BD following the
16521                                  * tx_bd with the start bit set.
16522                                  */
16523                                 parsing_bd = 1;
16524                 }
16525                 /* Don't skip next page pointers. */
16526                    tx_bd_prod = ((tx_bd_prod + 1) & MAX_TX_BD);
16527         }
16528
16529         BXE_PRINTF(
16530             "-----------------------------"
16531             "--------------"
16532             "-----------------------------\n");
16533 }
16534
16535 /*
16536  * Prints out the receive completion queue chain.
16537  *
16538  * Returns:
16539  *   Nothing.
16540  */
16541 static __noinline
16542 void bxe_dump_rx_cq_chain(struct bxe_fastpath *fp, int rx_cq_prod, int count)
16543 {
16544         struct bxe_softc *sc;
16545         union eth_rx_cqe *cqe;
16546         int i;
16547
16548         sc = fp->sc;
16549
16550         /* First some info about the tx_bd chain structure. */
16551         BXE_PRINTF(
16552            "----------------------------"
16553            "   CQE  Chain   "
16554            "----------------------------\n");
16555
16556         BXE_PRINTF("fp[%02d]->rcq_dma.paddr = 0x%jX\n",
16557             fp->index, (uintmax_t) fp->rcq_dma.paddr);
16558
16559         BXE_PRINTF("page size       = 0x%08X, cq chain pages    "
16560             "     = 0x%08X\n",
16561             (uint32_t)BCM_PAGE_SIZE, (uint32_t) NUM_RCQ_PAGES);
16562
16563         BXE_PRINTF("cqe_bd per page = 0x%08X, usable cqe_bd per "
16564             "page = 0x%08X\n",
16565             (uint32_t) TOTAL_RCQ_ENTRIES_PER_PAGE,
16566             (uint32_t) USABLE_RCQ_ENTRIES_PER_PAGE);
16567
16568         BXE_PRINTF("total cqe_bd    = 0x%08X\n",(uint32_t) TOTAL_RCQ_ENTRIES);
16569
16570         /* Now the CQE entries themselves. */
16571         BXE_PRINTF(
16572             "----------------------------"
16573             "    CQE Data    "
16574             "----------------------------\n");
16575
16576         for (i = 0; i < count; i++) {
16577                 cqe = (union eth_rx_cqe *)&fp->rcq_chain[rx_cq_prod];
16578
16579                 bxe_dump_cqe(fp, rx_cq_prod, cqe);
16580
16581                 /* Don't skip next page pointers. */
16582                 rx_cq_prod = ((rx_cq_prod + 1) & MAX_RCQ_ENTRIES);
16583         }
16584
16585         BXE_PRINTF(
16586             "----------------------------"
16587             "--------------"
16588             "----------------------------\n");
16589 }
16590
16591 /*
16592  * Prints out the receive chain.
16593  *
16594  * Returns:
16595  *   Nothing.
16596  */
16597 static __noinline
16598 void bxe_dump_rx_bd_chain(struct bxe_fastpath *fp, int prod, int count)
16599 {
16600         struct bxe_softc *sc;
16601         struct eth_rx_bd *rx_bd;
16602         struct mbuf *m;
16603         int i;
16604
16605         sc = fp->sc;
16606
16607         /* First some info about the tx_bd chain structure. */
16608         BXE_PRINTF(
16609             "----------------------------"
16610             "  rx_bd  chain  "
16611             "----------------------------\n");
16612
16613         BXE_PRINTF(
16614             "----- RX_BD Chain -----\n");
16615
16616         BXE_PRINTF("fp[%02d]->rx_dma.paddr = 0x%jX\n",
16617             fp->index, (uintmax_t) fp->rx_dma.paddr);
16618
16619         BXE_PRINTF(
16620             "page size = 0x%08X, rx chain pages = 0x%08X\n",
16621             (uint32_t)BCM_PAGE_SIZE, (uint32_t)NUM_RX_PAGES);
16622
16623         BXE_PRINTF(
16624             "rx_bd per page = 0x%08X, usable rx_bd per page = 0x%08X\n",
16625             (uint32_t)TOTAL_RX_BD_PER_PAGE, (uint32_t)USABLE_RX_BD_PER_PAGE);
16626
16627         BXE_PRINTF(
16628             "total rx_bd = 0x%08X\n", (uint32_t)TOTAL_RX_BD);
16629
16630         /* Now the rx_bd entries themselves. */
16631         BXE_PRINTF(
16632             "----------------------------"
16633             "   rx_bd data   "
16634             "----------------------------\n");
16635
16636         /* Now print out the rx_bd's themselves. */
16637         for (i = 0; i < count; i++) {
16638                 rx_bd = (struct eth_rx_bd *) (&fp->rx_chain[prod]);
16639                 m = sc->fp->rx_mbuf_ptr[prod];
16640
16641                 bxe_dump_rxbd(fp, prod, rx_bd);
16642                 bxe_dump_mbuf(sc, m);
16643
16644                 /* Don't skip next page pointers. */
16645                 prod = ((prod + 1) & MAX_RX_BD);
16646         }
16647
16648         BXE_PRINTF(
16649             "----------------------------"
16650             "--------------"
16651             "----------------------------\n");
16652 }
16653
16654 /*
16655  * Prints out a register dump.
16656  *
16657  * Returns:
16658  *   Nothing.
16659  */
16660 static __noinline
16661 void bxe_dump_hw_state(struct bxe_softc *sc)
16662 {
16663         int i;
16664
16665         BXE_PRINTF(
16666                 "----------------------------"
16667                 " Hardware State "
16668                 "----------------------------\n");
16669
16670         for (i = 0x2000; i < 0x10000; i += 0x10)
16671                 BXE_PRINTF("0x%04X: 0x%08X 0x%08X 0x%08X 0x%08X\n", i,
16672                     REG_RD(sc, 0 + i), REG_RD(sc, 0 + i + 0x4),
16673                     REG_RD(sc, 0 + i + 0x8), REG_RD(sc, 0 + i + 0xC));
16674
16675         BXE_PRINTF(
16676             "----------------------------"
16677             "----------------"
16678             "----------------------------\n");
16679 }
16680
16681 /*
16682  * Prints out the RX mbuf chain.
16683  *
16684  * Returns:
16685  *   Nothing.
16686  */
16687 static __noinline
16688 void bxe_dump_rx_mbuf_chain(struct bxe_softc *sc, int chain_prod, int count)
16689 {
16690         struct mbuf *m;
16691         int i;
16692
16693         BXE_PRINTF(
16694             "----------------------------"
16695             "  rx mbuf data  "
16696             "----------------------------\n");
16697
16698         for (i = 0; i < count; i++) {
16699                 m = sc->fp->rx_mbuf_ptr[chain_prod];
16700                 BXE_PRINTF("rxmbuf[0x%04X]\n", chain_prod);
16701                 bxe_dump_mbuf(sc, m);
16702                 chain_prod = RX_BD(NEXT_RX_BD(chain_prod));
16703         }
16704
16705         BXE_PRINTF(
16706             "----------------------------"
16707             "----------------"
16708             "----------------------------\n");
16709 }
16710
16711 /*
16712  * Prints out the mbufs in the TX mbuf chain.
16713  *
16714  * Returns:
16715  *   Nothing.
16716  */
16717 static __noinline
16718 void bxe_dump_tx_mbuf_chain(struct bxe_softc *sc, int chain_prod, int count)
16719 {
16720         struct mbuf *m;
16721         int i;
16722
16723         BXE_PRINTF(
16724             "----------------------------"
16725             "  tx mbuf data  "
16726             "----------------------------\n");
16727
16728         for (i = 0; i < count; i++) {
16729                 m = sc->fp->tx_mbuf_ptr[chain_prod];
16730                 BXE_PRINTF("txmbuf[%d]\n", chain_prod);
16731                 bxe_dump_mbuf(sc, m);
16732                 chain_prod = TX_BD(NEXT_TX_BD(chain_prod));
16733         }
16734
16735         BXE_PRINTF(
16736             "----------------------------"
16737             "----------------"
16738             "----------------------------\n");
16739 }
16740
16741 /*
16742  * Prints out the status block from host memory.
16743  *
16744  * Returns:
16745  *   Nothing.
16746  */
16747 static __noinline
16748 void bxe_dump_status_block(struct bxe_softc *sc)
16749 {
16750         struct bxe_fastpath *fp;
16751         struct host_def_status_block *def_sb;
16752         struct host_status_block *fpsb;
16753         int i;
16754
16755         def_sb = sc->def_sb;
16756         BXE_PRINTF(
16757             "----------------------------"
16758             "  Status Block  "
16759             "----------------------------\n");
16760
16761         for (i = 0; i < sc->num_queues; i++) {
16762                 fp = &sc->fp[i];
16763                 fpsb = fp->status_block;
16764                 BXE_PRINTF(
16765                     "----------------------------"
16766                     "     fp[%02d]     "
16767                     "----------------------------\n", fp->index);
16768
16769                 /* Print the USTORM fields (HC_USTORM_SB_NUM_INDICES). */
16770                 BXE_PRINTF(
16771                     "0x%08X - USTORM Flags (F/W RESERVED)\n",
16772                     fpsb->u_status_block.__flags);
16773                 BXE_PRINTF(
16774                     "      0x%02X - USTORM PCIe Function\n",
16775                     fpsb->u_status_block.func);
16776                 BXE_PRINTF(
16777                     "      0x%02X - USTORM Status Block ID\n",
16778                     fpsb->u_status_block.status_block_id);
16779                 BXE_PRINTF(
16780                     "    0x%04X - USTORM Status Block Index (Tag)\n",
16781                     fpsb->u_status_block.status_block_index);
16782                 BXE_PRINTF(
16783                     "    0x%04X - USTORM [TOE_RX_CQ_CONS]\n",
16784                     fpsb->u_status_block.index_values[HC_INDEX_U_TOE_RX_CQ_CONS]);
16785                 BXE_PRINTF(
16786                     "    0x%04X - USTORM [ETH_RX_CQ_CONS]\n",
16787                     fpsb->u_status_block.index_values[HC_INDEX_U_ETH_RX_CQ_CONS]);
16788                 BXE_PRINTF(
16789                     "    0x%04X - USTORM [ETH_RX_BD_CONS]\n",
16790                     fpsb->u_status_block.index_values[HC_INDEX_U_ETH_RX_BD_CONS]);
16791                 BXE_PRINTF(
16792                     "    0x%04X - USTORM [RESERVED]\n",
16793                     fpsb->u_status_block.index_values[3]);
16794
16795                 /* Print the CSTORM fields (HC_CSTORM_SB_NUM_INDICES). */
16796                 BXE_PRINTF(
16797                     "0x%08X - CSTORM Flags (F/W RESERVED)\n",
16798                     fpsb->c_status_block.__flags);
16799                 BXE_PRINTF(
16800                     "      0x%02X - CSTORM PCIe Function\n",
16801                     fpsb->c_status_block.func);
16802                 BXE_PRINTF(
16803                     "      0x%02X - CSTORM Status Block ID\n",
16804                     fpsb->c_status_block.status_block_id);
16805                 BXE_PRINTF(
16806                     "    0x%04X - CSTORM Status Block Index (Tag)\n",
16807                     fpsb->c_status_block.status_block_index);
16808                 BXE_PRINTF(
16809                     "    0x%04X - CSTORM [TOE_TX_CQ_CONS]\n",
16810                     fpsb->c_status_block.index_values[HC_INDEX_C_TOE_TX_CQ_CONS]);
16811                 BXE_PRINTF(
16812                     "    0x%04X - CSTORM [ETH_TX_CQ_CONS]\n",
16813                     fpsb->c_status_block.index_values[HC_INDEX_C_ETH_TX_CQ_CONS]);
16814                 BXE_PRINTF(
16815                     "    0x%04X - CSTORM [ISCSI_EQ_CONS]\n",
16816                     fpsb->c_status_block.index_values[HC_INDEX_C_ISCSI_EQ_CONS]);
16817                 BXE_PRINTF(
16818                     "    0x%04X - CSTORM [RESERVED]\n",
16819                     fpsb->c_status_block.index_values[3]);
16820         }
16821
16822         BXE_PRINTF(
16823             "--------------------------"
16824             "  Def Status Block  "
16825             "--------------------------\n");
16826
16827         /* Print attention information. */
16828         BXE_PRINTF(
16829             "      0x%02X - Status Block ID\n",
16830             def_sb->atten_status_block.status_block_id);
16831         BXE_PRINTF(
16832             "0x%08X - Attn Bits\n",
16833             def_sb->atten_status_block.attn_bits);
16834         BXE_PRINTF(
16835             "0x%08X - Attn Bits Ack\n",
16836             def_sb->atten_status_block.attn_bits_ack);
16837         BXE_PRINTF(
16838             "    0x%04X - Attn Block Index\n",
16839             le16toh(def_sb->atten_status_block.attn_bits_index));
16840
16841         /* Print the USTORM fields (HC_USTORM_DEF_SB_NUM_INDICES). */
16842         BXE_PRINTF(
16843             "      0x%02X - USTORM Status Block ID\n",
16844             def_sb->u_def_status_block.status_block_id);
16845         BXE_PRINTF(
16846             "    0x%04X - USTORM Status Block Index\n",
16847             le16toh(def_sb->u_def_status_block.status_block_index));
16848         BXE_PRINTF(
16849             "    0x%04X - USTORM [ETH_RDMA_RX_CQ_CONS]\n",
16850             le16toh(def_sb->u_def_status_block.index_values[HC_INDEX_DEF_U_ETH_RDMA_RX_CQ_CONS]));
16851         BXE_PRINTF(
16852             "    0x%04X - USTORM [ETH_ISCSI_RX_CQ_CONS]\n",
16853             le16toh(def_sb->u_def_status_block.index_values[HC_INDEX_DEF_U_ETH_ISCSI_RX_CQ_CONS]));
16854         BXE_PRINTF(
16855             "    0x%04X - USTORM [ETH_RDMA_RX_BD_CONS]\n",
16856             le16toh(def_sb->u_def_status_block.index_values[HC_INDEX_DEF_U_ETH_RDMA_RX_BD_CONS]));
16857         BXE_PRINTF(
16858             "    0x%04X - USTORM [ETH_ISCSI_RX_BD_CONS]\n",
16859             le16toh(def_sb->u_def_status_block.index_values[HC_INDEX_DEF_U_ETH_ISCSI_RX_BD_CONS]));
16860
16861         /* Print the CSTORM fields (HC_CSTORM_DEF_SB_NUM_INDICES). */
16862         BXE_PRINTF(
16863             "      0x%02X - CSTORM Status Block ID\n",
16864             def_sb->c_def_status_block.status_block_id);
16865         BXE_PRINTF(
16866             "    0x%04X - CSTORM Status Block Index\n",
16867             le16toh(def_sb->c_def_status_block.status_block_index));
16868         BXE_PRINTF(
16869             "    0x%04X - CSTORM [RDMA_EQ_CONS]\n",
16870             le16toh(def_sb->c_def_status_block.index_values[HC_INDEX_DEF_C_RDMA_EQ_CONS]));
16871         BXE_PRINTF(
16872             "    0x%04X - CSTORM [RDMA_NAL_PROD]\n",
16873             le16toh(def_sb->c_def_status_block.index_values[HC_INDEX_DEF_C_RDMA_NAL_PROD]));
16874         BXE_PRINTF(
16875             "    0x%04X - CSTORM [ETH_FW_TX_CQ_CONS]\n",
16876             le16toh(def_sb->c_def_status_block.index_values[HC_INDEX_DEF_C_ETH_FW_TX_CQ_CONS]));
16877         BXE_PRINTF(
16878             "    0x%04X - CSTORM [ETH_SLOW_PATH]\n",
16879             le16toh(def_sb->c_def_status_block.index_values[HC_INDEX_DEF_C_ETH_SLOW_PATH]));
16880         BXE_PRINTF(
16881             "    0x%04X - CSTORM [ETH_RDMA_CQ_CONS]\n",
16882             le16toh(def_sb->c_def_status_block.index_values[HC_INDEX_DEF_C_ETH_RDMA_CQ_CONS]));
16883         BXE_PRINTF(
16884             "    0x%04X - CSTORM [ETH_ISCSI_CQ_CONS]\n",
16885             le16toh(def_sb->c_def_status_block.index_values[HC_INDEX_DEF_C_ETH_ISCSI_CQ_CONS]));
16886         BXE_PRINTF(
16887             "    0x%04X - CSTORM [UNUSED]\n",
16888             le16toh(def_sb->c_def_status_block.index_values[6]));
16889         BXE_PRINTF(
16890             "    0x%04X - CSTORM [UNUSED]\n",
16891             le16toh(def_sb->c_def_status_block.index_values[7]));
16892
16893         /* Print the TSTORM fields (HC_TSTORM_DEF_SB_NUM_INDICES). */
16894         BXE_PRINTF(
16895             "      0x%02X - TSTORM Status Block ID\n",
16896             def_sb->t_def_status_block.status_block_id);
16897         BXE_PRINTF(
16898              "    0x%04X - TSTORM Status Block Index\n",
16899             le16toh(def_sb->t_def_status_block.status_block_index));
16900         for (i = 0; i < HC_TSTORM_DEF_SB_NUM_INDICES; i++)
16901                 BXE_PRINTF(
16902                     "    0x%04X - TSTORM [UNUSED]\n",
16903                     le16toh(def_sb->t_def_status_block.index_values[i]));
16904
16905         /* Print the XSTORM fields (HC_XSTORM_DEF_SB_NUM_INDICES). */
16906         BXE_PRINTF(
16907             "      0x%02X - XSTORM Status Block ID\n",
16908             def_sb->x_def_status_block.status_block_id);
16909         BXE_PRINTF(
16910             "    0x%04X - XSTORM Status Block Index\n",
16911             le16toh(def_sb->x_def_status_block.status_block_index));
16912         for (i = 0; i < HC_XSTORM_DEF_SB_NUM_INDICES; i++)
16913                 BXE_PRINTF(
16914                     "    0x%04X - XSTORM [UNUSED]\n",
16915                     le16toh(def_sb->x_def_status_block.index_values[i]));
16916
16917         BXE_PRINTF(
16918             "----------------------------"
16919             "----------------"
16920             "----------------------------\n");
16921 }
16922
16923
16924 /*
16925  * Prints out the statistics block from host memory.
16926  *
16927  * Returns:
16928  *   Nothing.
16929  */
16930 static __noinline
16931 void bxe_dump_stats_block(struct bxe_softc *sc)
16932 {
16933
16934 }
16935
16936 /*
16937  * Prints out a summary of the fastpath state.
16938  *
16939  * Returns:
16940  *   Nothing.
16941  */
16942 static __noinline
16943 void bxe_dump_fp_state(struct bxe_fastpath *fp)
16944 {
16945         struct bxe_softc *sc;
16946         uint32_t val_hi, val_lo;
16947         int i;
16948
16949         sc = fp->sc;
16950         BXE_PRINTF(
16951             "----------------------------"
16952             " Fastpath State "
16953             "----------------------------\n");
16954
16955         val_hi = U64_HI(fp);
16956         val_lo = U64_LO(fp);
16957         BXE_PRINTF(
16958             "0x%08X:%08X - (fp[%02d]) fastpath virtual address\n",
16959             val_hi, val_lo, fp->index);
16960         BXE_PRINTF(
16961             "                %3d - (fp[%02d]->sb_id)\n",
16962             fp->sb_id, fp->index);
16963         BXE_PRINTF(
16964             "                %3d - (fp[%02d]->cl_id)\n",
16965             fp->cl_id, fp->index);
16966         BXE_PRINTF(
16967             "         0x%08X - (fp[%02d]->state)\n",
16968             (uint32_t)fp->state, fp->index);
16969
16970         /* Receive state. */
16971         BXE_PRINTF(
16972             "             0x%04X - (fp[%02d]->rx_bd_prod)\n",
16973             fp->rx_bd_prod, fp->index);
16974         BXE_PRINTF(
16975             "             0x%04X - (fp[%02d]->rx_bd_cons)\n",
16976             fp->rx_bd_cons, fp->index);
16977         BXE_PRINTF(
16978             "             0x%04X - (fp[%02d]->rx_cq_prod)\n",
16979             fp->rx_cq_prod, fp->index);
16980         BXE_PRINTF(
16981             "             0x%04X - (fp[%02d]->rx_cq_cons)\n",
16982             fp->rx_cq_cons, fp->index);
16983         BXE_PRINTF(
16984             "   %16lu - (fp[%02d]->rx_pkts)\n",
16985             fp->rx_pkts, fp->index);
16986         BXE_PRINTF(
16987             "         0x%08X - (fp[%02d]->rx_mbuf_alloc)\n",
16988             fp->rx_mbuf_alloc, fp->index);
16989         BXE_PRINTF(
16990             "   %16lu - (fp[%02d]->ipackets)\n",
16991             fp->ipackets, fp->index);
16992         BXE_PRINTF(
16993             "   %16lu - (fp[%02d]->rx_soft_errors)\n",
16994             fp->rx_soft_errors, fp->index);
16995
16996         /* Transmit state. */
16997         BXE_PRINTF(
16998             "             0x%04X - (fp[%02d]->tx_bd_used)\n",
16999             fp->tx_bd_used, fp->index);
17000         BXE_PRINTF(
17001             "             0x%04X - (fp[%02d]->tx_bd_prod)\n",
17002             fp->tx_bd_prod, fp->index);
17003         BXE_PRINTF(
17004             "             0x%04X - (fp[%02d]->tx_bd_cons)\n",
17005             fp->tx_bd_cons, fp->index);
17006         BXE_PRINTF(
17007             "             0x%04X - (fp[%02d]->tx_pkt_prod)\n",
17008             fp->tx_pkt_prod, fp->index);
17009         BXE_PRINTF(
17010             "             0x%04X - (fp[%02d]->tx_pkt_cons)\n",
17011             fp->tx_pkt_cons, fp->index);
17012         BXE_PRINTF(
17013             "   %16lu - (fp[%02d]->tx_pkts)\n",
17014             fp->tx_pkts, fp->index);
17015         BXE_PRINTF(
17016             "         0x%08X - (fp[%02d]->tx_mbuf_alloc)\n",
17017             fp->tx_mbuf_alloc, fp->index);
17018         BXE_PRINTF(
17019             "   %16lu - (fp[%02d]->opackets)\n",
17020             fp->opackets, fp->index);
17021         BXE_PRINTF(
17022             "   %16lu - (fp[%02d]->tx_soft_errors)\n",
17023             fp->tx_soft_errors, fp->index);
17024
17025         /* TPA state. */
17026         if (TPA_ENABLED(sc)) {
17027                 BXE_PRINTF(
17028                     "   %16lu - (fp[%02d]->rx_tpa_pkts)\n",
17029                     fp->rx_tpa_pkts, fp->index);
17030                 BXE_PRINTF(
17031                     "         0x%08X - (fp[%02d]->tpa_mbuf_alloc)\n",
17032                     fp->tpa_mbuf_alloc, fp->index);
17033                 BXE_PRINTF(
17034                     "         0x%08X - (fp[%02d]->sge_mbuf_alloc)\n",
17035                     fp->sge_mbuf_alloc, fp->index);
17036
17037                 if (CHIP_IS_E1(sc)) {
17038                         for (i = 0; i < ETH_MAX_AGGREGATION_QUEUES_E1; i++)
17039                                 BXE_PRINTF(
17040                         "         0x%08X - (fp[%02d]->tpa_state[%02d])\n",
17041                                     (uint32_t)fp->tpa_state[i], fp->index, i);
17042                 } else {
17043                         for (i = 0; i < ETH_MAX_AGGREGATION_QUEUES_E1; i++)
17044                                 BXE_PRINTF(
17045                         "         0x%08X - (fp[%02d]->tpa_state[%02d])\n",
17046                                     (uint32_t)fp->tpa_state[i], fp->index, i);
17047                 }
17048         }
17049
17050         BXE_PRINTF(
17051             "----------------------------"
17052             "----------------"
17053             "----------------------------\n");
17054 }
17055
17056 /*
17057  * Returns:
17058  *   Nothing.
17059  */
17060 static __noinline
17061 void bxe_dump_port_state_locked(struct bxe_softc *sc)
17062 {
17063
17064         BXE_PRINTF(
17065             "------------------------------"
17066             " Port State "
17067             "------------------------------\n");
17068
17069         BXE_PRINTF(
17070             "        %2d - (port) pmf\n", sc->port.pmf);
17071         BXE_PRINTF(
17072             "0x%08X - (port) link_config\n", sc->port.link_config);
17073         BXE_PRINTF(
17074             "0x%08X - (port) supported\n", sc->port.supported);
17075         BXE_PRINTF(
17076             "0x%08X - (port) advertising\n", sc->port.advertising);
17077         BXE_PRINTF(
17078             "0x%08X - (port) port_stx\n", sc->port.port_stx);
17079
17080         BXE_PRINTF(
17081             "----------------------------"
17082             "----------------"
17083             "----------------------------\n");
17084 }
17085
17086 /*
17087  * Returns:
17088  *   Nothing.
17089  */
17090 static __noinline
17091 void bxe_dump_link_vars_state_locked(struct bxe_softc *sc)
17092 {
17093         BXE_PRINTF(
17094             "---------------------------"
17095             " Link Vars State "
17096             "----------------------------\n");
17097
17098         switch (sc->link_vars.mac_type) {
17099         case MAC_TYPE_NONE:
17100                 BXE_PRINTF("      NONE");
17101                 break;
17102         case MAC_TYPE_EMAC:
17103                 BXE_PRINTF("      EMAC");
17104                 break;
17105         case MAC_TYPE_BMAC:
17106                 BXE_PRINTF("      BMAC");
17107                 break;
17108         default:
17109                 BXE_PRINTF("      UNKN");
17110         }
17111         printf(" - (link_vars->mac_type)\n");
17112
17113         BXE_PRINTF(
17114             "        %2d - (link_vars->phy_link_up)\n",
17115             sc->link_vars.phy_link_up);
17116         BXE_PRINTF(
17117             "        %2d - (link_vars->link_up)\n",
17118             sc->link_vars.link_up);
17119         BXE_PRINTF(
17120             "        %2d - (link_vars->duplex)\n",
17121             sc->link_vars.duplex);
17122         BXE_PRINTF(
17123             "    0x%04X - (link_vars->flow_ctrl)\n",
17124             sc->link_vars.flow_ctrl);
17125         BXE_PRINTF(
17126             "    0x%04X - (link_vars->line_speed)\n",
17127             sc->link_vars.line_speed);
17128         BXE_PRINTF(
17129             "0x%08X - (link_vars->ieee_fc)\n",
17130             sc->link_vars.ieee_fc);
17131         BXE_PRINTF(
17132             "0x%08X - (link_vars->autoneg)\n",
17133             sc->link_vars.autoneg);
17134         BXE_PRINTF(
17135             "0x%08X - (link_vars->phy_flags)\n",
17136             sc->link_vars.phy_flags);
17137         BXE_PRINTF(
17138             "0x%08X - (link_vars->link_status)\n",
17139             sc->link_vars.link_status);
17140
17141         BXE_PRINTF(
17142             "----------------------------"
17143             "----------------"
17144             "----------------------------\n");
17145 }
17146
17147
17148 /*
17149  *
17150  * Returns:
17151  *   Nothing.
17152  */
17153 static __noinline
17154 void bxe_dump_link_params_state_locked(struct bxe_softc *sc)
17155 {
17156         BXE_PRINTF(
17157             "--------------------------"
17158             " Link Params State "
17159             "---------------------------\n");
17160
17161         BXE_PRINTF(
17162             "        %2d - (link_params->port)\n",
17163             sc->link_params.port);
17164         BXE_PRINTF(
17165             "        %2d - (link_params->loopback_mode)\n",
17166             sc->link_params.loopback_mode);
17167         BXE_PRINTF(
17168             "       %3d - (link_params->phy_addr)\n",
17169             sc->link_params.phy_addr);
17170         BXE_PRINTF(
17171             "    0x%04X - (link_params->req_duplex)\n",
17172             sc->link_params.req_duplex);
17173         BXE_PRINTF(
17174             "    0x%04X - (link_params->req_flow_ctrl)\n",
17175             sc->link_params.req_flow_ctrl);
17176         BXE_PRINTF(
17177             "    0x%04X - (link_params->req_line_speed)\n",
17178             sc->link_params.req_line_speed);
17179         BXE_PRINTF(
17180             "     %5d - (link_params->ether_mtu)\n",
17181             sc->port.ether_mtu);
17182         BXE_PRINTF(
17183             "0x%08X - (link_params->shmem_base) shared memory base address\n",
17184             sc->link_params.shmem_base);
17185         BXE_PRINTF(
17186             "0x%08X - (link_params->speed_cap_mask)\n",
17187             sc->link_params.speed_cap_mask);
17188         BXE_PRINTF(
17189             "0x%08X - (link_params->ext_phy_config)\n",
17190             sc->link_params.ext_phy_config);
17191         BXE_PRINTF(
17192             "0x%08X - (link_params->switch_cfg)\n",
17193             sc->link_params.switch_cfg);
17194
17195         BXE_PRINTF(
17196             "----------------------------"
17197                 "----------------"
17198                 "----------------------------\n");
17199 }
17200
17201 /*
17202  * Prints out a summary of the driver state.
17203  *
17204  * Returns:
17205  *   Nothing.
17206  */
17207 static __noinline
17208 void bxe_dump_driver_state(struct bxe_softc *sc)
17209 {
17210         uint32_t val_hi, val_lo;
17211
17212         BXE_PRINTF(
17213             "-----------------------------"
17214             " Driver State "
17215             "-----------------------------\n");
17216
17217         val_hi = U64_HI(sc);
17218         val_lo = U64_LO(sc);
17219         BXE_PRINTF(
17220             "0x%08X:%08X - (sc) driver softc structure virtual address\n",
17221             val_hi, val_lo);
17222
17223         val_hi = U64_HI(sc->bxe_vhandle);
17224         val_lo = U64_LO(sc->bxe_vhandle);
17225         BXE_PRINTF(
17226             "0x%08X:%08X - (sc->bxe_vhandle) PCI BAR0 virtual address\n",
17227             val_hi, val_lo);
17228
17229         val_hi = U64_HI(sc->bxe_db_vhandle);
17230         val_lo = U64_LO(sc->bxe_db_vhandle);
17231         BXE_PRINTF(
17232             "0x%08X:%08X - (sc->bxe_db_vhandle) PCI BAR2 virtual address\n",
17233             val_hi, val_lo);
17234
17235         BXE_PRINTF("         0x%08X - (sc->num_queues) Fastpath queues\n",
17236             sc->num_queues);
17237         BXE_PRINTF("         0x%08X - (sc->rx_lane_swap) RX XAUI lane swap\n",
17238             sc->rx_lane_swap);
17239         BXE_PRINTF("         0x%08X - (sc->tx_lane_swap) TX XAUI lane swap\n",
17240             sc->tx_lane_swap);
17241         BXE_PRINTF("   %16lu - (sc->debug_sim_mbuf_alloc_failed)\n",
17242             sc->debug_sim_mbuf_alloc_failed);
17243         BXE_PRINTF("   %16lu - (sc->debug_sim_mbuf_map_failed)\n",
17244             sc->debug_sim_mbuf_map_failed);
17245
17246         BXE_PRINTF(
17247             "----------------------------"
17248             "----------------"
17249             "----------------------------\n");
17250
17251         bxe_dump_port_state_locked(sc);
17252         bxe_dump_link_params_state_locked(sc);
17253         bxe_dump_link_vars_state_locked(sc);
17254 }
17255
17256 /*
17257  * Dump bootcode (MCP) debug buffer to the console.
17258  *
17259  * Returns:
17260  *   None
17261  */
17262 static __noinline
17263 void bxe_dump_fw(struct bxe_softc *sc)
17264 {
17265         uint32_t addr, mark, data[9], offset;
17266         int word;
17267
17268         addr = sc->common.shmem_base - 0x0800 + 4;
17269         mark = REG_RD(sc, addr);
17270         mark = MCP_REG_MCPR_SCRATCH + ((mark + 0x3) & ~0x3) - 0x08000000;
17271
17272         BXE_PRINTF(
17273             "---------------------------"
17274             " MCP Debug Buffer "
17275             "---------------------------\n");
17276
17277         /* Read from "mark" to the end of the buffer. */
17278         for (offset = mark; offset <= sc->common.shmem_base;
17279             offset += (0x8 * 4)) {
17280                 for (word = 0; word < 8; word++)
17281                         data[word] = htonl(REG_RD(sc, offset + 4 * word));
17282                 data[8] = 0x0;
17283                 printf("%s", (char *) data);
17284         }
17285
17286         /* Read from the start of the buffer to "mark". */
17287         for (offset = addr + 4; offset <= mark; offset += (0x8 * 4)) {
17288                 for (word = 0; word < 8; word++)
17289                         data[word] = htonl(REG_RD(sc, offset + 4 * word));
17290                 data[8] = 0x0;
17291                 printf("%s", (char *) data);
17292         }
17293
17294         BXE_PRINTF(
17295             "----------------------------"
17296             "----------------"
17297             "----------------------------\n");
17298 }
17299
17300 /*
17301  * Decode firmware messages.
17302  *
17303  * Returns:
17304  *   None
17305  */
17306 static void
17307 bxe_decode_mb_msgs(struct bxe_softc *sc, uint32_t drv_mb_header,
17308     uint32_t fw_mb_header)
17309 {
17310
17311         if (drv_mb_header) {
17312                 BXE_PRINTF("Driver message is ");
17313                 switch (drv_mb_header & DRV_MSG_CODE_MASK) {
17314                 case DRV_MSG_CODE_LOAD_REQ:
17315                         printf(
17316                             "LOAD_REQ (0x%08X)",
17317                             (uint32_t)DRV_MSG_CODE_LOAD_REQ);
17318                         break;
17319                 case DRV_MSG_CODE_LOAD_DONE:
17320                         printf(
17321                             "LOAD_DONE (0x%08X)",
17322                             (uint32_t)DRV_MSG_CODE_LOAD_DONE);
17323                         break;
17324                 case DRV_MSG_CODE_UNLOAD_REQ_WOL_EN:
17325                         printf(
17326                             "UNLOAD_REQ_WOL_EN (0x%08X)",
17327                             (uint32_t)DRV_MSG_CODE_UNLOAD_REQ_WOL_EN);
17328                         break;
17329                 case DRV_MSG_CODE_UNLOAD_REQ_WOL_DIS:
17330                         printf(
17331                             "UNLOAD_REQ_WOL_DIS (0x%08X)",
17332                             (uint32_t)DRV_MSG_CODE_UNLOAD_REQ_WOL_DIS);
17333                         break;
17334                 case DRV_MSG_CODE_UNLOAD_REQ_WOL_MCP:
17335                         printf(
17336                             "UNLOADREQ_WOL_MCP (0x%08X)",
17337                             (uint32_t)DRV_MSG_CODE_UNLOAD_REQ_WOL_MCP);
17338                         break;
17339                 case DRV_MSG_CODE_UNLOAD_DONE:
17340                         printf(
17341                             "UNLOAD_DONE (0x%08X)",
17342                             (uint32_t)DRV_MSG_CODE_UNLOAD_DONE);
17343                         break;
17344                 case DRV_MSG_CODE_DIAG_ENTER_REQ:
17345                         printf(
17346                             "DIAG_ENTER_REQ (0x%08X)",
17347                             (uint32_t)DRV_MSG_CODE_DIAG_ENTER_REQ);
17348                         break;
17349                 case DRV_MSG_CODE_DIAG_EXIT_REQ:
17350                         printf(
17351                             "DIAG_EXIT_REQ (0x%08X)",
17352                             (uint32_t)DRV_MSG_CODE_DIAG_EXIT_REQ);
17353                         break;
17354                 case DRV_MSG_CODE_VALIDATE_KEY:
17355                         printf(
17356                             "CODE_VALIDITY_KEY (0x%08X)",
17357                             (uint32_t)DRV_MSG_CODE_VALIDATE_KEY);
17358                         break;
17359                 case DRV_MSG_CODE_GET_CURR_KEY:
17360                         printf(
17361                             "GET_CURR_KEY (0x%08X)",
17362                             (uint32_t) DRV_MSG_CODE_GET_CURR_KEY);
17363                         break;
17364                 case DRV_MSG_CODE_GET_UPGRADE_KEY:
17365                         printf(
17366                             "GET_UPGRADE_KEY (0x%08X)",
17367                             (uint32_t)DRV_MSG_CODE_GET_UPGRADE_KEY);
17368                         break;
17369                 case DRV_MSG_CODE_GET_MANUF_KEY:
17370                         printf(
17371                             "GET_MANUF_KEY (0x%08X)",
17372                             (uint32_t)DRV_MSG_CODE_GET_MANUF_KEY);
17373                         break;
17374                 case DRV_MSG_CODE_LOAD_L2B_PRAM:
17375                         printf(
17376                             "LOAD_L2B_PRAM (0x%08X)",
17377                             (uint32_t)DRV_MSG_CODE_LOAD_L2B_PRAM);
17378                                 break;
17379                 case BIOS_MSG_CODE_LIC_CHALLENGE:
17380                         printf(
17381                             "LIC_CHALLENGE (0x%08X)",
17382                             (uint32_t)BIOS_MSG_CODE_LIC_CHALLENGE);
17383                         break;
17384                 case BIOS_MSG_CODE_LIC_RESPONSE:
17385                         printf(
17386                             "LIC_RESPONSE (0x%08X)",
17387                             (uint32_t)BIOS_MSG_CODE_LIC_RESPONSE);
17388                         break;
17389                 case BIOS_MSG_CODE_VIRT_MAC_PRIM:
17390                         printf(
17391                             "VIRT_MAC_PRIM (0x%08X)",
17392                             (uint32_t)BIOS_MSG_CODE_VIRT_MAC_PRIM);
17393                         break;
17394                 case BIOS_MSG_CODE_VIRT_MAC_ISCSI:
17395                         printf(
17396                             "VIRT_MAC_ISCSI (0x%08X)",
17397                             (uint32_t)BIOS_MSG_CODE_VIRT_MAC_ISCSI);
17398                         break;
17399                 default:
17400                         printf(
17401                             "Unknown command (0x%08X)!",
17402                             (drv_mb_header & DRV_MSG_CODE_MASK));
17403                 }
17404
17405                 printf(" (seq = 0x%04X)\n", (drv_mb_header &
17406                     DRV_MSG_SEQ_NUMBER_MASK));
17407         }
17408
17409         if (fw_mb_header) {
17410                 BXE_PRINTF("Firmware response is ");
17411                 switch (fw_mb_header & FW_MSG_CODE_MASK) {
17412                 case FW_MSG_CODE_DRV_LOAD_COMMON:
17413                         printf(
17414                             "DRV_LOAD_COMMON (0x%08X)",
17415                             (uint32_t)FW_MSG_CODE_DRV_LOAD_COMMON);
17416                         break;
17417                 case FW_MSG_CODE_DRV_LOAD_PORT:
17418                         printf(
17419                             "DRV_LOAD_PORT (0x%08X)",
17420                             (uint32_t)FW_MSG_CODE_DRV_LOAD_PORT);
17421                         break;
17422                 case FW_MSG_CODE_DRV_LOAD_FUNCTION:
17423                         printf(
17424                             "DRV_LOAD_FUNCTION (0x%08X)",
17425                             (uint32_t)FW_MSG_CODE_DRV_LOAD_FUNCTION);
17426                         break;
17427                 case FW_MSG_CODE_DRV_LOAD_REFUSED:
17428                         printf(
17429                             "DRV_LOAD_REFUSED (0x%08X)",
17430                             (uint32_t)FW_MSG_CODE_DRV_LOAD_REFUSED);
17431                         break;
17432                 case FW_MSG_CODE_DRV_LOAD_DONE:
17433                         printf(
17434                             "DRV_LOAD_DONE (0x%08X)",
17435                             (uint32_t)FW_MSG_CODE_DRV_LOAD_DONE);
17436                         break;
17437                 case FW_MSG_CODE_DRV_UNLOAD_COMMON:
17438                         printf(
17439                             "DRV_UNLOAD_COMMON (0x%08X)",
17440                             (uint32_t)FW_MSG_CODE_DRV_UNLOAD_COMMON);
17441                         break;
17442                 case FW_MSG_CODE_DRV_UNLOAD_PORT:
17443                         printf(
17444                             "DRV_UNLOAD_PORT (0x%08X)",
17445                             (uint32_t)FW_MSG_CODE_DRV_UNLOAD_PORT);
17446                         break;
17447                 case FW_MSG_CODE_DRV_UNLOAD_FUNCTION:
17448                         printf(
17449                             "DRV_UNLOAD_FUNCTION (0x%08X)",
17450                             (uint32_t)FW_MSG_CODE_DRV_UNLOAD_FUNCTION);
17451                         break;
17452                 case FW_MSG_CODE_DRV_UNLOAD_DONE:
17453                         printf(
17454                             "DRV_UNLOAD_DONE (0x%08X)",
17455                             (uint32_t)FW_MSG_CODE_DRV_UNLOAD_DONE);
17456                         break;
17457                 case FW_MSG_CODE_DIAG_ENTER_DONE:
17458                         printf(
17459                             "DIAG_ENTER_DONE (0x%08X)",
17460                             (uint32_t)FW_MSG_CODE_DIAG_ENTER_DONE);
17461                         break;
17462                 case FW_MSG_CODE_DIAG_REFUSE:
17463                         printf(
17464                             "DIAG_REFUSE (0x%08X)",
17465                             (uint32_t)FW_MSG_CODE_DIAG_REFUSE);
17466                         break;
17467                 case FW_MSG_CODE_DIAG_EXIT_DONE:
17468                         printf(
17469                             "DIAG_EXIT_DONE (0x%08X)",
17470                             (uint32_t)FW_MSG_CODE_DIAG_EXIT_DONE);
17471                         break;
17472                 case FW_MSG_CODE_VALIDATE_KEY_SUCCESS:
17473                         printf(
17474                             "VALIDATE_KEY_SUCCESS (0x%08X)",
17475                             (uint32_t)FW_MSG_CODE_VALIDATE_KEY_SUCCESS);
17476                         break;
17477                 case FW_MSG_CODE_VALIDATE_KEY_FAILURE:
17478                         printf(
17479                             "VALIDATE_KEY_FAILURE (0x%08X)",
17480                             (uint32_t)FW_MSG_CODE_VALIDATE_KEY_FAILURE);
17481                         break;
17482                 case FW_MSG_CODE_GET_KEY_DONE:
17483                         printf(
17484                             "GET_KEY_DONE (0x%08X)",
17485                             (uint32_t)FW_MSG_CODE_GET_KEY_DONE);
17486                         break;
17487                 case FW_MSG_CODE_NO_KEY:
17488                         printf(
17489                             "NO_KEY (0x%08X)",
17490                             (uint32_t)FW_MSG_CODE_NO_KEY);
17491                         break;
17492                 default:
17493                         printf(
17494                             "unknown value (0x%08X)!",
17495                             (fw_mb_header & FW_MSG_CODE_MASK));
17496                 }
17497
17498                 printf(" (seq = 0x%04X)\n", (fw_mb_header &
17499                     FW_MSG_SEQ_NUMBER_MASK));
17500         }
17501 }
17502
17503 /*
17504  * Prints a text string for the ramrod command.
17505  *
17506  * Returns:
17507  *   None
17508  */
17509 static void
17510 bxe_decode_ramrod_cmd(struct bxe_softc *sc, int command)
17511 {
17512         BXE_PRINTF("Ramrod command = ");
17513
17514         switch (command) {
17515         case RAMROD_CMD_ID_ETH_PORT_SETUP:
17516                 printf("ETH_PORT_SETUP\n");
17517                 break;
17518         case RAMROD_CMD_ID_ETH_CLIENT_SETUP:
17519                 printf("ETH_CLIENT_SETUP\n");
17520                 break;
17521         case RAMROD_CMD_ID_ETH_STAT_QUERY:
17522                 printf("ETH_STAT_QUERY\n");
17523                 break;
17524         case RAMROD_CMD_ID_ETH_UPDATE:
17525                 printf("ETH_UPDATE\n");
17526                 break;
17527         case RAMROD_CMD_ID_ETH_HALT:
17528                 printf("ETH_HALT\n");
17529                 break;
17530         case RAMROD_CMD_ID_ETH_SET_MAC:
17531                 printf("ETH_SET_MAC\n");
17532                 break;
17533         case RAMROD_CMD_ID_ETH_CFC_DEL:
17534                 printf("ETH_CFC_DEL\n");
17535                 break;
17536         case RAMROD_CMD_ID_ETH_PORT_DEL:
17537                 printf("ETH_PORT_DEL\n");
17538                 break;
17539         case RAMROD_CMD_ID_ETH_FORWARD_SETUP:
17540                 printf("ETH_FORWARD_SETUP\n");
17541                 break;
17542         default:
17543                 printf("Unknown ramrod command!\n");
17544         }
17545 }
17546
17547
17548 /*
17549  * Prints out driver information and forces a kernel breakpoint.
17550  *
17551  * Returns:
17552  *   Nothing.
17553  */
17554 static void
17555 bxe_breakpoint(struct bxe_softc *sc)
17556 {
17557         struct bxe_fastpath *fp;
17558         int i;
17559
17560         fp = &sc->fp[0];
17561         /* Unreachable code to silence the compiler about unused functions. */
17562         if (0) {
17563                 bxe_reg_read16(sc, PCICFG_OFFSET);
17564                 bxe_dump_tx_mbuf_chain(sc, 0, USABLE_TX_BD);
17565                 bxe_dump_rx_mbuf_chain(sc, 0, USABLE_RX_BD);
17566                 bxe_dump_tx_chain(fp, 0, USABLE_TX_BD);
17567                 bxe_dump_rx_cq_chain(fp, 0, USABLE_RCQ_ENTRIES);
17568                 bxe_dump_rx_bd_chain(fp, 0, USABLE_RX_BD);
17569                 bxe_dump_status_block(sc);
17570                 bxe_dump_stats_block(sc);
17571                 bxe_dump_fp_state(fp);
17572                 bxe_dump_driver_state(sc);
17573                 bxe_dump_hw_state(sc);
17574                 bxe_dump_fw(sc);
17575         }
17576
17577         /*
17578          * Do some device sanity checking.  Run it twice in case
17579          * the hardware is still running so we can identify any
17580          * transient conditions.
17581          */
17582         bxe_idle_chk(sc); bxe_idle_chk(sc);
17583
17584         bxe_dump_driver_state(sc);
17585
17586         for (i = 0; i < sc->num_queues; i++)
17587                 bxe_dump_fp_state(&sc->fp[i]);
17588
17589         bxe_dump_status_block(sc);
17590         bxe_dump_fw(sc);
17591
17592         /* Call the OS debugger. */
17593         breakpoint();
17594 }
17595 #endif