]> CyberLeo.Net >> Repos - FreeBSD/stable/9.git/blob - sys/dev/bxe/if_bxe.c
MFC r226173, r227843, r227848 and r227908:
[FreeBSD/stable/9.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 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_mtu = ETHERMTU;
2140         ifp->if_hwassist = BXE_IF_HWASSIST;
2141         ifp->if_capabilities = BXE_IF_CAPABILITIES;
2142         /* TPA not enabled by default. */
2143         ifp->if_capenable = BXE_IF_CAPABILITIES & ~IFCAP_LRO;
2144         ifp->if_baudrate = IF_Gbps(10UL);
2145
2146         ifp->if_snd.ifq_drv_maxlen = sc->tx_ring_size;
2147
2148         IFQ_SET_MAXLEN(&ifp->if_snd, ifp->if_snd.ifq_drv_maxlen);
2149         IFQ_SET_READY(&ifp->if_snd);
2150
2151         /* Attach to the Ethernet interface list. */
2152         ether_ifattach(ifp, sc->link_params.mac_addr);
2153
2154         /* Attach the interrupts to the interrupt handlers. */
2155         rc = bxe_interrupt_attach(sc);
2156         if (rc != 0) {
2157                 BXE_PRINTF("%s(%d): Interrupt allocation failed!\n",
2158                     __FILE__, __LINE__);
2159                 goto bxe_attach_fail;
2160         }
2161
2162         /* Print important adapter info for the user. */
2163         bxe_print_adapter_info(sc);
2164
2165         /* Add the supported sysctls to the kernel. */
2166         bxe_add_sysctls(sc);
2167
2168 bxe_attach_fail:
2169         if (rc != 0)
2170                 bxe_detach(dev);
2171
2172         DBEXIT(BXE_INFO_LOAD | BXE_INFO_RESET);
2173         return (rc);
2174 }
2175
2176
2177 /*
2178  * Supported link settings.
2179  *
2180  * Examines hardware configuration present in NVRAM and
2181  * determines the link settings that are supported between
2182  * the external PHY and the switch.
2183  *
2184  * Returns:
2185  *   None.
2186  *
2187  * Side effects:
2188  *       Sets sc->port.supported
2189  *       Sets sc->link_params.phy_addr
2190  */
2191 static void
2192 bxe_link_settings_supported(struct bxe_softc *sc, uint32_t switch_cfg)
2193 {
2194         uint32_t ext_phy_type;
2195         int port;
2196
2197         DBENTER(BXE_VERBOSE_PHY);
2198         DBPRINT(sc, BXE_VERBOSE_PHY, "%s(): switch_cfg = 0x%08X\n",
2199                 __FUNCTION__, switch_cfg);
2200
2201         port = BP_PORT(sc);
2202         /* Get the link settings supported by the external PHY. */
2203         switch (switch_cfg) {
2204         case SWITCH_CFG_1G:
2205                 ext_phy_type =
2206                     SERDES_EXT_PHY_TYPE(sc->link_params.ext_phy_config);
2207
2208                 DBPRINT(sc, BXE_VERBOSE_PHY,
2209                     "%s(): 1G switch w/ ext_phy_type = "
2210                     "0x%08X\n", __FUNCTION__, ext_phy_type);
2211
2212                 switch (ext_phy_type) {
2213                 case PORT_HW_CFG_SERDES_EXT_PHY_TYPE_DIRECT:
2214                         DBPRINT(sc, BXE_VERBOSE_PHY, "%s(): 1G Direct.\n",
2215                             __FUNCTION__);
2216
2217                         sc->port.supported |=
2218                             (SUPPORTED_10baseT_Half |
2219                              SUPPORTED_10baseT_Full |
2220                              SUPPORTED_100baseT_Half |
2221                              SUPPORTED_100baseT_Full |
2222                              SUPPORTED_1000baseT_Full |
2223                              SUPPORTED_2500baseX_Full |
2224                              SUPPORTED_TP |
2225                              SUPPORTED_FIBRE |
2226                              SUPPORTED_Autoneg |
2227                              SUPPORTED_Pause |
2228                              SUPPORTED_Asym_Pause);
2229                         break;
2230
2231                 case PORT_HW_CFG_SERDES_EXT_PHY_TYPE_BCM5482:
2232                         DBPRINT(sc, BXE_VERBOSE_PHY, "%s(): 1G 5482\n",
2233                                 __FUNCTION__);
2234
2235                         sc->port.supported |=
2236                             (SUPPORTED_10baseT_Half |
2237                              SUPPORTED_10baseT_Full |
2238                              SUPPORTED_100baseT_Half |
2239                              SUPPORTED_100baseT_Full |
2240                              SUPPORTED_1000baseT_Full |
2241                              SUPPORTED_TP |
2242                              SUPPORTED_FIBRE |
2243                              SUPPORTED_Autoneg |
2244                              SUPPORTED_Pause |
2245                              SUPPORTED_Asym_Pause);
2246                         break;
2247
2248                 default:
2249                         BXE_PRINTF(
2250                             "%s(%d): Bad NVRAM 1Gb PHY configuration data "
2251                             "(ext_phy_config=0x%08X).\n",
2252                             __FILE__, __LINE__,
2253                             sc->link_params.ext_phy_config);
2254                         goto bxe_link_settings_supported_exit;
2255                 }
2256
2257                 sc->port.phy_addr =
2258                     REG_RD(sc, NIG_REG_SERDES0_CTRL_PHY_ADDR + (port * 0x10));
2259
2260                 DBPRINT(sc, BXE_VERBOSE_PHY, "%s(): phy_addr = 0x%08X\n",
2261                     __FUNCTION__, sc->port.phy_addr);
2262                 break;
2263
2264         case SWITCH_CFG_10G:
2265                 ext_phy_type =
2266                     XGXS_EXT_PHY_TYPE(sc->link_params.ext_phy_config);
2267
2268                 DBPRINT(
2269                     sc, BXE_VERBOSE_PHY,
2270                     "%s(): 10G switch w/ ext_phy_type = 0x%08X\n",
2271                     __FUNCTION__, ext_phy_type);
2272
2273                 switch (ext_phy_type) {
2274                 case PORT_HW_CFG_XGXS_EXT_PHY_TYPE_DIRECT:
2275                         DBPRINT(sc, BXE_VERBOSE_PHY,
2276                             "%s(): 10G switch w/ direct connect.\n",
2277                             __FUNCTION__);
2278
2279                         sc->port.supported |=
2280                             (SUPPORTED_10baseT_Half |
2281                              SUPPORTED_10baseT_Full |
2282                              SUPPORTED_100baseT_Half |
2283                              SUPPORTED_100baseT_Full |
2284                              SUPPORTED_1000baseT_Full |
2285                              SUPPORTED_2500baseX_Full |
2286                              SUPPORTED_10000baseT_Full |
2287                              SUPPORTED_TP |
2288                              SUPPORTED_FIBRE |
2289                              SUPPORTED_Autoneg |
2290                              SUPPORTED_Pause |
2291                              SUPPORTED_Asym_Pause);
2292                         break;
2293
2294                 case PORT_HW_CFG_XGXS_EXT_PHY_TYPE_BCM8072:
2295                         DBPRINT(sc, BXE_VERBOSE_PHY,
2296                             "ext_phy_type 0x%x (8072)\n",ext_phy_type);
2297
2298                         sc->port.supported |=
2299                             (SUPPORTED_10000baseT_Full |
2300                              SUPPORTED_1000baseT_Full |
2301                              SUPPORTED_FIBRE |
2302                              SUPPORTED_Autoneg |
2303                              SUPPORTED_Pause |
2304                              SUPPORTED_Asym_Pause);
2305                         break;
2306
2307                 case PORT_HW_CFG_XGXS_EXT_PHY_TYPE_BCM8073:
2308                         DBPRINT(sc,
2309                             BXE_VERBOSE_PHY,"ext_phy_type 0x%x (8073)\n",
2310                             ext_phy_type);
2311
2312                         sc->port.supported |=
2313                             (SUPPORTED_10000baseT_Full |
2314                              SUPPORTED_2500baseX_Full |
2315                              SUPPORTED_1000baseT_Full |
2316                              SUPPORTED_FIBRE |
2317                              SUPPORTED_Autoneg |
2318                              SUPPORTED_Pause |
2319                              SUPPORTED_Asym_Pause);
2320                         break;
2321
2322                 case PORT_HW_CFG_XGXS_EXT_PHY_TYPE_BCM8705:
2323                         DBPRINT(sc, BXE_VERBOSE_PHY,
2324                             "%s(): 10G switch w/ 8705.\n",__FUNCTION__);
2325
2326                         sc->port.supported |=
2327                             (SUPPORTED_10000baseT_Full |
2328                              SUPPORTED_FIBRE |
2329                              SUPPORTED_Pause |
2330                              SUPPORTED_Asym_Pause);
2331                         break;
2332
2333                 case PORT_HW_CFG_XGXS_EXT_PHY_TYPE_BCM8706:
2334                         DBPRINT(sc, BXE_VERBOSE_PHY,
2335                             "%s(): 10G switch w/ 8706.\n",
2336                             __FUNCTION__);
2337
2338                         sc->port.supported |=
2339                             (SUPPORTED_10000baseT_Full |
2340                              SUPPORTED_1000baseT_Full |
2341                              SUPPORTED_FIBRE |
2342                              SUPPORTED_Pause |
2343                              SUPPORTED_Asym_Pause);
2344                         break;
2345
2346                 case PORT_HW_CFG_XGXS_EXT_PHY_TYPE_BCM8726:
2347                         DBPRINT(sc, BXE_VERBOSE_PHY,
2348                             "%s(): 10G switch w/ 8726.\n",
2349                             __FUNCTION__);
2350
2351                         sc->port.supported |=
2352                             (SUPPORTED_10000baseT_Full |
2353                              SUPPORTED_FIBRE |
2354                              SUPPORTED_Pause |
2355                              SUPPORTED_Asym_Pause);
2356                         break;
2357
2358                 case PORT_HW_CFG_XGXS_EXT_PHY_TYPE_BCM8727:
2359                         DBPRINT(sc, BXE_VERBOSE_PHY,"ext_phy_type 0x%x (8727)\n",
2360                             ext_phy_type);
2361
2362                         sc->port.supported |=
2363                             (SUPPORTED_10000baseT_Full |
2364                              SUPPORTED_1000baseT_Full |
2365                              SUPPORTED_Autoneg |
2366                              SUPPORTED_FIBRE |
2367                              SUPPORTED_Pause |
2368                              SUPPORTED_Asym_Pause);
2369                         break;
2370
2371                 case PORT_HW_CFG_XGXS_EXT_PHY_TYPE_SFX7101:
2372                          DBPRINT(sc, BXE_VERBOSE_PHY,
2373                             "%s(): 10G switch w/ SFX7101.\n",
2374                             __FUNCTION__);
2375
2376                         sc->port.supported |=
2377                             (SUPPORTED_10000baseT_Full |
2378                              SUPPORTED_TP |
2379                              SUPPORTED_Autoneg |
2380                              SUPPORTED_Pause |
2381                              SUPPORTED_Asym_Pause);
2382                         break;
2383
2384                 case PORT_HW_CFG_XGXS_EXT_PHY_TYPE_BCM8481:
2385                          DBPRINT(sc, BXE_VERBOSE_PHY,
2386                             "ext_phy_type 0x%x (BCM8481)\n",
2387                             ext_phy_type);
2388
2389                         sc->port.supported |=
2390                             (SUPPORTED_10baseT_Half |
2391                              SUPPORTED_10baseT_Full |
2392                              SUPPORTED_100baseT_Half |
2393                              SUPPORTED_100baseT_Full |
2394                              SUPPORTED_1000baseT_Full |
2395                              SUPPORTED_10000baseT_Full |
2396                              SUPPORTED_TP |
2397                              SUPPORTED_Autoneg |
2398                              SUPPORTED_Pause |
2399                              SUPPORTED_Asym_Pause);
2400                         break;
2401
2402                 case PORT_HW_CFG_XGXS_EXT_PHY_TYPE_FAILURE:
2403                         DBPRINT(sc, BXE_WARN,
2404                             "%s(): 10G XGXS PHY failure detected.\n",
2405                             __FUNCTION__);
2406                         break;
2407
2408                         BXE_PRINTF(
2409                             "%s(%d): Bad NVRAM 10Gb PHY configuration data "
2410                             "(ext_phy_config=0x%08X).\n",
2411                             __FILE__, __LINE__,
2412                             sc->link_params.ext_phy_config);
2413                         goto bxe_link_settings_supported_exit;
2414                 }
2415
2416                 sc->port.phy_addr =
2417                     REG_RD(sc, NIG_REG_XGXS0_CTRL_PHY_ADDR +(port * 0x18));
2418                 break;
2419
2420         default:
2421                 DBPRINT(sc, BXE_WARN, "%s(): BAD switch configuration "
2422                     "(link_config = 0x%08X)\n", __FUNCTION__,
2423                     sc->port.link_config);
2424                 goto bxe_link_settings_supported_exit;
2425         }
2426
2427         sc->link_params.phy_addr = sc->port.phy_addr;
2428
2429         /* Mask out unsupported speeds according to NVRAM. */
2430         if ((sc->link_params.speed_cap_mask &
2431             PORT_HW_CFG_SPEED_CAPABILITY_D0_10M_HALF) == 0)
2432                 sc->port.supported &= ~SUPPORTED_10baseT_Half;
2433
2434         if ((sc->link_params.speed_cap_mask &
2435             PORT_HW_CFG_SPEED_CAPABILITY_D0_10M_FULL) == 0)
2436                 sc->port.supported &= ~SUPPORTED_10baseT_Full;
2437
2438         if ((sc->link_params.speed_cap_mask &
2439             PORT_HW_CFG_SPEED_CAPABILITY_D0_100M_HALF) == 0)
2440                 sc->port.supported &= ~SUPPORTED_100baseT_Half;
2441
2442         if ((sc->link_params.speed_cap_mask &
2443             PORT_HW_CFG_SPEED_CAPABILITY_D0_100M_FULL) == 0)
2444                 sc->port.supported &= ~SUPPORTED_100baseT_Full;
2445
2446         if ((sc->link_params.speed_cap_mask &
2447             PORT_HW_CFG_SPEED_CAPABILITY_D0_1G) == 0)
2448                 sc->port.supported &= ~(SUPPORTED_1000baseT_Half |
2449                         SUPPORTED_1000baseT_Full);
2450
2451         if ((sc->link_params.speed_cap_mask &
2452             PORT_HW_CFG_SPEED_CAPABILITY_D0_2_5G) == 0)
2453                 sc->port.supported &= ~SUPPORTED_2500baseX_Full;
2454
2455         if ((sc->link_params.speed_cap_mask &
2456             PORT_HW_CFG_SPEED_CAPABILITY_D0_10G) == 0)
2457                 sc->port.supported &= ~SUPPORTED_10000baseT_Full;
2458
2459         DBPRINT(sc, BXE_VERBOSE_PHY,
2460             "%s(): Supported link settings = 0x%b\n", __FUNCTION__,
2461             sc->port.supported, BXE_SUPPORTED_PRINTFB);
2462
2463 bxe_link_settings_supported_exit:
2464
2465         DBEXIT(BXE_VERBOSE_PHY);
2466 }
2467
2468 /*
2469  * Requested link settings.
2470  *
2471  * Returns:
2472  *   None.
2473  */
2474 static void
2475 bxe_link_settings_requested(struct bxe_softc *sc)
2476 {
2477         uint32_t ext_phy_type;
2478         DBENTER(BXE_VERBOSE_PHY);
2479
2480         sc->link_params.req_duplex = MEDIUM_FULL_DUPLEX;
2481
2482         switch (sc->port.link_config & PORT_FEATURE_LINK_SPEED_MASK) {
2483
2484         case PORT_FEATURE_LINK_SPEED_AUTO:
2485                 if (sc->port.supported & SUPPORTED_Autoneg) {
2486                         sc->link_params.req_line_speed |= SPEED_AUTO_NEG;
2487                         sc->port.advertising = sc->port.supported;
2488                 } else {
2489                         ext_phy_type = XGXS_EXT_PHY_TYPE(
2490                             sc->link_params.ext_phy_config);
2491
2492                         if ((ext_phy_type ==
2493                              PORT_HW_CFG_XGXS_EXT_PHY_TYPE_BCM8705) ||
2494                             (ext_phy_type ==
2495                              PORT_HW_CFG_XGXS_EXT_PHY_TYPE_BCM8706)) {
2496                                 /* Force 10G, no autonegotiation. */
2497                                 sc->link_params.req_line_speed = SPEED_10000;
2498                                 sc->port.advertising =
2499                                     ADVERTISED_10000baseT_Full |
2500                                     ADVERTISED_FIBRE;
2501                                 break;
2502                         }
2503
2504                         DBPRINT(sc, BXE_FATAL,
2505                             "%s(): NVRAM config error. Invalid "
2506                             "link_config (0x%08X) - Autoneg not supported!\n",
2507                             __FUNCTION__, sc->port.link_config);
2508                         goto bxe_link_settings_requested_exit;
2509                 }
2510                 break;
2511         case PORT_FEATURE_LINK_SPEED_10M_FULL:
2512                 if (sc->port.supported & SUPPORTED_10baseT_Full) {
2513                         sc->link_params.req_line_speed = SPEED_10;
2514                         sc->port.advertising = ADVERTISED_10baseT_Full |
2515                                 ADVERTISED_TP;
2516                 } else {
2517                         DBPRINT(sc, BXE_FATAL,
2518                             "%s(): NVRAM config error. Invalid "
2519                             "link_config (0x%08X) - speed_cap_mask 0x%08X\n",
2520                             __FUNCTION__, sc->port.link_config,
2521                             sc->link_params.speed_cap_mask);
2522                         goto bxe_link_settings_requested_exit;
2523                 }
2524                 break;
2525         case PORT_FEATURE_LINK_SPEED_10M_HALF:
2526                 if (sc->port.supported & SUPPORTED_10baseT_Half) {
2527                         sc->link_params.req_line_speed = SPEED_10;
2528                         sc->link_params.req_duplex = MEDIUM_HALF_DUPLEX;
2529                         sc->port.advertising = ADVERTISED_10baseT_Half |
2530                                 ADVERTISED_TP;
2531                 } else {
2532                         DBPRINT(sc, BXE_FATAL,
2533                             "%s(): NVRAM config error. Invalid "
2534                             "link_config (0x%08X) - speed_cap_mask = 0x%08X\n",
2535                             __FUNCTION__, sc->port.link_config,
2536                             sc->link_params.speed_cap_mask);
2537                         goto bxe_link_settings_requested_exit;
2538                 }
2539                 break;
2540         case PORT_FEATURE_LINK_SPEED_100M_FULL:
2541                 if (sc->port.supported & SUPPORTED_100baseT_Full) {
2542                         sc->link_params.req_line_speed = SPEED_100;
2543                         sc->port.advertising = ADVERTISED_100baseT_Full |
2544                                 ADVERTISED_TP;
2545                 } else {
2546                         DBPRINT(sc, BXE_FATAL,
2547                             "%s(): NVRAM config error. Invalid "
2548                             "link_config (0x%08X) - speed_cap_mask = 0x%08X\n",
2549                             __FUNCTION__, sc->port.link_config,
2550                             sc->link_params.speed_cap_mask);
2551                         goto bxe_link_settings_requested_exit;
2552                 }
2553                 break;
2554         case PORT_FEATURE_LINK_SPEED_100M_HALF:
2555                 if (sc->port.supported & SUPPORTED_100baseT_Half) {
2556                         sc->link_params.req_line_speed = SPEED_100;
2557                         sc->link_params.req_duplex = MEDIUM_HALF_DUPLEX;
2558                         sc->port.advertising = ADVERTISED_100baseT_Half |
2559                                 ADVERTISED_TP;
2560                 } else {
2561                         DBPRINT(sc, BXE_FATAL,
2562                             "%s(): NVRAM config error. Invalid "
2563                             "link_config (0x%08X) - speed_cap_mask = 0x%08X\n",
2564                             __FUNCTION__, sc->port.link_config,
2565                             sc->link_params.speed_cap_mask);
2566                         goto bxe_link_settings_requested_exit;
2567                 }
2568                 break;
2569         case PORT_FEATURE_LINK_SPEED_1G:
2570                 if (sc->port.supported & SUPPORTED_1000baseT_Full) {
2571                         sc->link_params.req_line_speed = SPEED_1000;
2572                         sc->port.advertising = ADVERTISED_1000baseT_Full |
2573                                 ADVERTISED_TP;
2574                 } else {
2575                         DBPRINT(sc, BXE_FATAL,
2576                             "%s(): NVRAM config error. Invalid "
2577                             "link_config (0x%08X) - speed_cap_mask = 0x%08X\n",
2578                             __FUNCTION__, sc->port.link_config,
2579                             sc->link_params.speed_cap_mask);
2580                         goto bxe_link_settings_requested_exit;
2581                 }
2582                 break;
2583         case PORT_FEATURE_LINK_SPEED_2_5G:
2584                 if (sc->port.supported & SUPPORTED_2500baseX_Full) {
2585                         sc->link_params.req_line_speed = SPEED_2500;
2586                         sc->port.advertising = ADVERTISED_2500baseX_Full |
2587                                 ADVERTISED_TP;
2588                 } else {
2589                         DBPRINT(sc, BXE_FATAL,
2590                             "%s(): NVRAM config error. Invalid "
2591                             "link_config (0x%08X) - speed_cap_mask = 0x%08X\n",
2592                             __FUNCTION__, sc->port.link_config,
2593                             sc->link_params.speed_cap_mask);
2594                         goto bxe_link_settings_requested_exit;
2595                 }
2596                 break;
2597         case PORT_FEATURE_LINK_SPEED_10G_CX4:
2598         case PORT_FEATURE_LINK_SPEED_10G_KX4:
2599         case PORT_FEATURE_LINK_SPEED_10G_KR:
2600                 if (sc->port.supported & SUPPORTED_10000baseT_Full) {
2601                         sc->link_params.req_line_speed = SPEED_10000;
2602                         sc->port.advertising = ADVERTISED_10000baseT_Full |
2603                             ADVERTISED_FIBRE;
2604                 } else {
2605                         DBPRINT(sc, BXE_FATAL,
2606                             "%s(): NVRAM config error. Invalid "
2607                             "link_config (0x%08X) - speed_cap_mask = 0x%08X\n",
2608                             __FUNCTION__, sc->port.link_config,
2609                             sc->link_params.speed_cap_mask);
2610                         goto bxe_link_settings_requested_exit;
2611                 }
2612                 break;
2613         default:
2614                 DBPRINT(sc, BXE_FATAL, "%s(): NVRAM config error. BAD link "
2615                     "speed - link_config = 0x%08X\n", __FUNCTION__,
2616                     sc->port.link_config);
2617                 sc->link_params.req_line_speed = 0;
2618                 sc->port.advertising = sc->port.supported;
2619                 break;
2620         }
2621
2622         DBPRINT(sc, BXE_VERBOSE_PHY,
2623             "%s(): req_line_speed = %d, req_duplex = %d\n",
2624             __FUNCTION__, sc->link_params.req_line_speed,
2625             sc->link_params.req_duplex);
2626
2627         sc->link_params.req_flow_ctrl =
2628             sc->port.link_config & PORT_FEATURE_FLOW_CONTROL_MASK;
2629
2630         if ((sc->link_params.req_flow_ctrl == FLOW_CTRL_AUTO) &&
2631             !(sc->port.supported & SUPPORTED_Autoneg))
2632                 sc->link_params.req_flow_ctrl = FLOW_CTRL_NONE;
2633
2634         DBPRINT(sc, BXE_VERBOSE_PHY,
2635                 "%s(): req_flow_ctrl = 0x%08X, advertising = 0x%08X\n",
2636                 __FUNCTION__, sc->link_params.req_flow_ctrl,
2637                 sc->port.advertising);
2638
2639 bxe_link_settings_requested_exit:
2640
2641         DBEXIT(BXE_VERBOSE_PHY);
2642 }
2643
2644
2645 /*
2646  * Get function specific hardware configuration.
2647  *
2648  * Multiple function devices such as the BCM57711E have configuration
2649  * information that is specific to each PCIe function of the controller.
2650  * The number of PCIe functions is not necessarily the same as the number
2651  * of Ethernet ports supported by the device.
2652  *
2653  * Returns:
2654  *   0 = Success, !0 = Failure
2655  */
2656 static int
2657 bxe_hwinfo_function_get(struct bxe_softc *sc)
2658 {
2659         uint32_t mac_hi, mac_lo, val;
2660         int func, rc;
2661
2662         DBENTER(BXE_VERBOSE_LOAD);
2663
2664         rc = 0;
2665         func = BP_FUNC(sc);
2666
2667         /* Get the common hardware configuration first. */
2668         bxe_hwinfo_common_get(sc);
2669
2670         /* Assume no outer VLAN/multi-function support. */
2671         sc->e1hov = sc->e1hmf = 0;
2672
2673         /* Get config info for mf enabled devices. */
2674         if (CHIP_IS_E1H(sc)) {
2675                 sc->mf_config[BP_E1HVN(sc)] =
2676                     SHMEM_RD(sc, mf_cfg.func_mf_config[func].config);
2677                 val = (SHMEM_RD(sc, mf_cfg.func_mf_config[func].e1hov_tag) &
2678                     FUNC_MF_CFG_E1HOV_TAG_MASK);
2679                 if (val != FUNC_MF_CFG_E1HOV_TAG_DEFAULT) {
2680                         sc->e1hov = (uint16_t) val;
2681                         sc->e1hmf = 1;
2682                 } else {
2683                         if (BP_E1HVN(sc)) {
2684                                 rc = EPERM;
2685                                 goto bxe_hwinfo_function_get_exit;
2686                         }
2687                 }
2688         }
2689
2690         if (!NOMCP(sc)) {
2691                 bxe_hwinfo_port_get(sc);
2692                 sc->fw_seq = SHMEM_RD(sc, func_mb[func].drv_mb_header) &
2693                     DRV_MSG_SEQ_NUMBER_MASK;
2694         }
2695
2696
2697         /*
2698          * Fetch the factory configured MAC address for multi function
2699          * devices. If this is not a multi-function device then the MAC
2700          * address was already read in the bxe_hwinfo_port_get() routine.
2701          * The MAC addresses used by the port are not the same as the MAC
2702          * addressed used by the function.
2703          */
2704         if (IS_E1HMF(sc)) {
2705                 mac_hi = SHMEM_RD(sc, mf_cfg.func_mf_config[func].mac_upper);
2706                 mac_lo = SHMEM_RD(sc, mf_cfg.func_mf_config[func].mac_lower);
2707
2708                 if ((mac_lo == 0) && (mac_hi == 0)) {
2709                         BXE_PRINTF("%s(%d): Invalid Ethernet address!\n",
2710                             __FILE__, __LINE__);
2711                         rc = ENODEV;
2712                 } else {
2713                         sc->link_params.mac_addr[0] = (u_char)(mac_hi >> 8);
2714                         sc->link_params.mac_addr[1] = (u_char)(mac_hi);
2715                         sc->link_params.mac_addr[2] = (u_char)(mac_lo >> 24);
2716                         sc->link_params.mac_addr[3] = (u_char)(mac_lo >> 16);
2717                         sc->link_params.mac_addr[4] = (u_char)(mac_lo >> 8);
2718                         sc->link_params.mac_addr[5] = (u_char)(mac_lo);
2719                 }
2720         }
2721
2722
2723 bxe_hwinfo_function_get_exit:
2724         DBEXIT(BXE_VERBOSE_LOAD);
2725         return (rc);
2726 }
2727
2728
2729 /*
2730  * Get port specific hardware configuration.
2731  *
2732  * Multiple port devices such as the BCM57710 have configuration
2733  * information that is specific to each Ethernet port of the
2734  * controller.  This function reads that configuration
2735  * information from the bootcode's shared memory and saves it
2736  * for future use.
2737  *
2738  * Returns:
2739  *   0 = Success, !0 = Failure
2740  */
2741 static int
2742 bxe_hwinfo_port_get(struct bxe_softc *sc)
2743 {
2744         int i, port, rc;
2745         uint32_t val, mac_hi, mac_lo;
2746
2747         DBENTER(BXE_VERBOSE_LOAD);
2748         rc = 0;
2749
2750         port = BP_PORT(sc);
2751         sc->link_params.sc = sc;
2752         sc->link_params.port = port;
2753
2754         /* Fetch several configuration values from bootcode shared memory. */
2755         sc->link_params.lane_config =
2756             SHMEM_RD(sc, dev_info.port_hw_config[port].lane_config);
2757         sc->link_params.ext_phy_config =
2758             SHMEM_RD(sc, dev_info.port_hw_config[port].external_phy_config);
2759
2760         if (XGXS_EXT_PHY_TYPE(sc->link_params.ext_phy_config) ==
2761             PORT_HW_CFG_XGXS_EXT_PHY_TYPE_BCM8727_NOC) {
2762                 sc->link_params.ext_phy_config &=
2763                     ~PORT_HW_CFG_XGXS_EXT_PHY_TYPE_MASK;
2764                 sc->link_params.ext_phy_config |=
2765                     PORT_HW_CFG_XGXS_EXT_PHY_TYPE_BCM8727;
2766                 sc->link_params.feature_config_flags |=
2767                     FEATURE_CONFIG_BCM8727_NOC;
2768         }
2769
2770         sc->link_params.speed_cap_mask =
2771             SHMEM_RD(sc, dev_info.port_hw_config[port].speed_capability_mask);
2772         sc->port.link_config =
2773             SHMEM_RD(sc, dev_info.port_feature_config[port].link_config);
2774
2775
2776         /* Read the XGXS RX/TX preemphasis values. */
2777         for (i = 0; i < 2; i++) {
2778                 val = SHMEM_RD(sc,
2779                     dev_info.port_hw_config[port].xgxs_config_rx[i<<1]);
2780                 sc->link_params.xgxs_config_rx[i << 1] = ((val >> 16) & 0xffff);
2781                 sc->link_params.xgxs_config_rx[(i << 1) + 1] = (val & 0xffff);
2782
2783                 val = SHMEM_RD(sc,
2784                     dev_info.port_hw_config[port].xgxs_config_tx[i<<1]);
2785                 sc->link_params.xgxs_config_tx[i << 1] = ((val >> 16) & 0xffff);
2786                 sc->link_params.xgxs_config_tx[(i << 1) + 1] = (val & 0xffff);
2787         }
2788
2789         /* Fetch the device configured link settings. */
2790         sc->link_params.switch_cfg = sc->port.link_config &
2791             PORT_FEATURE_CONNECTED_SWITCH_MASK;
2792
2793         bxe_link_settings_supported(sc, sc->link_params.switch_cfg);
2794         bxe_link_settings_requested(sc);
2795
2796         mac_hi = SHMEM_RD(sc, dev_info.port_hw_config[port].mac_upper);
2797         mac_lo = SHMEM_RD(sc, dev_info.port_hw_config[port].mac_lower);
2798
2799         if (mac_lo == 0 && mac_hi == 0) {
2800                 BXE_PRINTF("%s(%d): No Ethernet address programmed on the "
2801                     "controller!\n", __FILE__, __LINE__);
2802                 rc = ENODEV;
2803         } else {
2804                 sc->link_params.mac_addr[0] = (u_char)(mac_hi >> 8);
2805                 sc->link_params.mac_addr[1] = (u_char)(mac_hi);
2806                 sc->link_params.mac_addr[2] = (u_char)(mac_lo >> 24);
2807                 sc->link_params.mac_addr[3] = (u_char)(mac_lo >> 16);
2808                 sc->link_params.mac_addr[4] = (u_char)(mac_lo >> 8);
2809                 sc->link_params.mac_addr[5] = (u_char)(mac_lo);
2810         }
2811
2812         DBEXIT(BXE_VERBOSE_LOAD);
2813         return (rc);
2814 }
2815
2816
2817 /*
2818  * Get common hardware configuration.
2819  *
2820  * Multiple port devices such as the BCM57710 have configuration
2821  * information that is shared between all ports of the Ethernet
2822  * controller.  This function reads that configuration
2823  * information from the bootcode's shared memory and saves it
2824  * for future use.
2825  *
2826  * Returns:
2827  *   0 = Success, !0 = Failure
2828  */
2829 static int
2830 bxe_hwinfo_common_get(struct bxe_softc *sc)
2831 {
2832         uint32_t val;
2833         int rc;
2834
2835         DBENTER(BXE_VERBOSE_LOAD);
2836         rc = 0;
2837
2838         /* Get the chip revision. */
2839         sc->common.chip_id = sc->link_params.chip_id =
2840             ((REG_RD(sc, MISC_REG_CHIP_NUM) & 0xffff) << 16) |
2841             ((REG_RD(sc, MISC_REG_CHIP_REV) & 0x000f) << 12) |
2842             ((REG_RD(sc, MISC_REG_CHIP_METAL) & 0xff) << 4) |
2843             ((REG_RD(sc, MISC_REG_BOND_ID) & 0xf));
2844
2845         DBPRINT(sc, BXE_VERBOSE_LOAD, "%s(): chip_id = 0x%08X.\n",
2846             __FUNCTION__, sc->common.chip_id);
2847
2848         val = (REG_RD(sc, 0x2874) & 0x55);
2849         if ((sc->common.chip_id & 0x1) ||
2850             (CHIP_IS_E1(sc) && val) || (CHIP_IS_E1H(sc) && (val == 0x55))) {
2851                 sc->bxe_flags |= BXE_ONE_PORT_FLAG;
2852                 DBPRINT(sc, BXE_VERBOSE_LOAD, "%s(): Single port device.\n",
2853                     __FUNCTION__);
2854         }
2855
2856         /* Identify enabled PCI capabilites (PCIe, MSI-X, etc.). */
2857         bxe_probe_pci_caps(sc);
2858
2859         /* Get the NVRAM size. */
2860         val = REG_RD(sc, MCP_REG_MCPR_NVM_CFG4);
2861         sc->common.flash_size = (NVRAM_1MB_SIZE <<
2862             (val & MCPR_NVM_CFG4_FLASH_SIZE));
2863
2864         DBPRINT(sc, BXE_VERBOSE_LOAD, "%s(): flash_size = 0x%08x (%dKB)\n",
2865             __FUNCTION__, sc->common.flash_size,(sc->common.flash_size >> 10));
2866
2867         /* Find the shared memory base address. */
2868         sc->common.shmem_base = sc->link_params.shmem_base =
2869             REG_RD(sc, MISC_REG_SHARED_MEM_ADDR);
2870         sc->common.shmem2_base = REG_RD(sc, MISC_REG_GENERIC_CR_0);
2871         DBPRINT(sc, BXE_VERBOSE_LOAD, "%s(): shmem_base = 0x%08X\n",
2872             __FUNCTION__, sc->common.shmem_base);
2873
2874         /* Make sure the shared memory address is valid. */
2875         if (!sc->common.shmem_base ||
2876             (sc->common.shmem_base < 0xA0000) ||
2877             (sc->common.shmem_base > 0xC0000)) {
2878
2879                 BXE_PRINTF("%s(%d): MCP is not active!\n",
2880                     __FILE__, __LINE__);
2881                 /* ToDo: Remove the NOMCP support. */
2882                 sc->bxe_flags |= BXE_NO_MCP_FLAG;
2883                 rc = ENODEV;
2884                 goto bxe_hwinfo_common_get_exit;
2885         }
2886
2887         /* Make sure the shared memory contents are valid. */
2888         val = SHMEM_RD(sc, validity_map[BP_PORT(sc)]);
2889         if ((val & (SHR_MEM_VALIDITY_DEV_INFO | SHR_MEM_VALIDITY_MB)) !=
2890             (SHR_MEM_VALIDITY_DEV_INFO | SHR_MEM_VALIDITY_MB)) {
2891                 BXE_PRINTF("%s(%d): Invalid NVRAM! Bad validity "
2892                     "signature.\n", __FILE__, __LINE__);
2893                 rc = ENODEV;
2894                 goto bxe_hwinfo_common_get_exit;
2895         }
2896
2897         /* Read the device configuration from shared memory. */
2898         sc->common.hw_config =
2899             SHMEM_RD(sc, dev_info.shared_hw_config.config);
2900         sc->link_params.hw_led_mode = ((sc->common.hw_config &
2901             SHARED_HW_CFG_LED_MODE_MASK) >> SHARED_HW_CFG_LED_MODE_SHIFT);
2902
2903         /* Check if we need to override the preemphasis values. */
2904         sc->link_params.feature_config_flags = 0;
2905         val = SHMEM_RD(sc, dev_info.shared_feature_config.config);
2906         if (val & SHARED_FEAT_CFG_OVERRIDE_PREEMPHASIS_CFG_ENABLED)
2907                 sc->link_params.feature_config_flags |=
2908                     FEATURE_CONFIG_OVERRIDE_PREEMPHASIS_ENABLED;
2909         else
2910                 sc->link_params.feature_config_flags &=
2911                     ~FEATURE_CONFIG_OVERRIDE_PREEMPHASIS_ENABLED;
2912
2913         /* In multifunction mode, we can't support WoL on a VN. */
2914         if (BP_E1HVN(sc) == 0) {
2915                 val = REG_RD(sc, PCICFG_OFFSET + PCICFG_PM_CAPABILITY);
2916                 sc->bxe_flags |= (val & PCICFG_PM_CAPABILITY_PME_IN_D3_COLD) ?
2917                         0 : BXE_NO_WOL_FLAG;
2918         } else
2919                 sc->bxe_flags |= BXE_NO_WOL_FLAG;
2920
2921         DBPRINT(sc, BXE_VERBOSE_LOAD, "%s(): %sWoL capable\n", __FUNCTION__,
2922                 (sc->bxe_flags & BXE_NO_WOL_FLAG) ? "Not " : "");
2923
2924         /* Check bootcode version */
2925         sc->common.bc_ver = ((SHMEM_RD(sc, dev_info.bc_rev)) >> 8);
2926         if (sc->common.bc_ver < MIN_BXE_BC_VER) {
2927                 BXE_PRINTF("%s(%d): Warning: This driver needs bootcode "
2928                     "0x%08X but found 0x%08X, please upgrade!\n",
2929                     __FILE__, __LINE__, MIN_BXE_BC_VER, sc->common.bc_ver);
2930                 rc = ENODEV;
2931                 goto bxe_hwinfo_common_get_exit;
2932         }
2933
2934 bxe_hwinfo_common_get_exit:
2935         DBEXIT(BXE_VERBOSE_LOAD);
2936         return (rc);
2937 }
2938
2939
2940 /*
2941  * Remove traces of PXE boot by forcing UNDI driver unload.
2942  *
2943  * Returns:
2944  *   None.
2945  */
2946 static void
2947 bxe_undi_unload(struct bxe_softc *sc)
2948 {
2949         uint32_t reset_code, swap_en, swap_val, val;
2950         int func;
2951
2952         DBENTER(BXE_VERBOSE_LOAD);
2953
2954         /* Check if there is any driver already loaded */
2955         val = REG_RD(sc, MISC_REG_UNPREPARED);
2956         if (val == 0x1) {
2957
2958                 /* Check if it is the UNDI driver. */
2959                 bxe_acquire_hw_lock(sc, HW_LOCK_RESOURCE_UNDI);
2960                 val = REG_RD(sc, DORQ_REG_NORM_CID_OFST);
2961                 if (val == 0x7) {
2962                         reset_code = DRV_MSG_CODE_UNLOAD_REQ_WOL_DIS;
2963                         func = BP_FUNC(sc);
2964
2965                         DBPRINT(sc, BXE_WARN,
2966                             "%s(): UNDI is active! Resetting the device.\n",
2967                             __FUNCTION__);
2968
2969                         /* Clear the UNDI indication. */
2970                         REG_WR(sc, DORQ_REG_NORM_CID_OFST, 0);
2971
2972                         /* Try to unload UNDI on port 0. */
2973                         sc->bxe_func = 0;
2974                         sc->fw_seq = (SHMEM_RD(sc,
2975                             func_mb[sc->bxe_func].drv_mb_header) &
2976                             DRV_MSG_SEQ_NUMBER_MASK);
2977                         reset_code = bxe_fw_command(sc, reset_code);
2978
2979                         /* Check if UNDI is active on port 1. */
2980                         if (reset_code != FW_MSG_CODE_DRV_UNLOAD_COMMON) {
2981
2982                                 /* Send "done" for previous unload. */
2983                                 bxe_fw_command(sc, DRV_MSG_CODE_UNLOAD_DONE);
2984
2985                                 /* Now unload on port 1. */
2986                                 sc->bxe_func = 1;
2987                                 sc->fw_seq = (SHMEM_RD(sc,
2988                                     func_mb[sc->bxe_func].drv_mb_header) &
2989                                     DRV_MSG_SEQ_NUMBER_MASK);
2990
2991                                 reset_code = DRV_MSG_CODE_UNLOAD_REQ_WOL_DIS;
2992                                 bxe_fw_command(sc, reset_code);
2993                         }
2994
2995                         /* It's now safe to release the lock. */
2996                         bxe_release_hw_lock(sc, HW_LOCK_RESOURCE_UNDI);
2997
2998                         REG_WR(sc, (BP_PORT(sc) ? HC_REG_CONFIG_1 :
2999                                 HC_REG_CONFIG_0), 0x1000);
3000
3001                         REG_WR(sc, (BP_PORT(sc) ?
3002                             NIG_REG_LLH1_BRB1_DRV_MASK :
3003                             NIG_REG_LLH0_BRB1_DRV_MASK), 0x0);
3004
3005                         REG_WR(sc, (BP_PORT(sc) ?
3006                             NIG_REG_LLH1_BRB1_NOT_MCP :
3007                             NIG_REG_LLH0_BRB1_NOT_MCP), 0x0);
3008
3009                         /* Clear AEU. */
3010                         REG_WR(sc, (BP_PORT(sc) ?
3011                             MISC_REG_AEU_MASK_ATTN_FUNC_1 :
3012                             MISC_REG_AEU_MASK_ATTN_FUNC_0), 0);
3013
3014                         DELAY(10000);
3015
3016                         /* Save NIG port swap information. */
3017                         swap_val = REG_RD(sc, NIG_REG_PORT_SWAP);
3018                         swap_en = REG_RD(sc, NIG_REG_STRAP_OVERRIDE);
3019
3020                         /* Reset the controller. */
3021                         REG_WR(sc, GRCBASE_MISC +
3022                             MISC_REGISTERS_RESET_REG_1_CLEAR, 0xd3ffffff);
3023                         REG_WR(sc, GRCBASE_MISC +
3024                             MISC_REGISTERS_RESET_REG_2_CLEAR, 0x00001403);
3025
3026                         /* Take the NIG out of reset and restore swap values.*/
3027                         REG_WR(sc, GRCBASE_MISC +
3028                             MISC_REGISTERS_RESET_REG_1_SET,
3029                             MISC_REGISTERS_RESET_REG_1_RST_NIG);
3030                         REG_WR(sc, NIG_REG_PORT_SWAP, swap_val);
3031                         REG_WR(sc, NIG_REG_STRAP_OVERRIDE, swap_en);
3032
3033                         /* Send completion message to the MCP. */
3034                         bxe_fw_command(sc, DRV_MSG_CODE_UNLOAD_DONE);
3035
3036                         /*
3037                          * Restore our function and firmware sequence counter.
3038                          */
3039                         sc->bxe_func = func;
3040                         sc->fw_seq = (SHMEM_RD(sc,
3041                             func_mb[sc->bxe_func].drv_mb_header) &
3042                             DRV_MSG_SEQ_NUMBER_MASK);
3043                 } else
3044                         bxe_release_hw_lock(sc, HW_LOCK_RESOURCE_UNDI);
3045         }
3046
3047         DBEXIT(BXE_VERBOSE_LOAD);
3048 }
3049
3050
3051 /*
3052  * Device detach function.
3053  *
3054  * Stops the controller, resets the controller, and releases resources.
3055  *
3056  * Returns:
3057  *   0 on success, !0 = failure.
3058  */
3059 static int
3060 bxe_detach(device_t dev)
3061 {
3062         struct bxe_softc *sc;
3063         struct ifnet *ifp;
3064         int rc;
3065
3066         sc = device_get_softc(dev);
3067         DBENTER(BXE_INFO_UNLOAD);
3068
3069         rc = 0;
3070
3071         ifp = sc->bxe_ifp;
3072         if (ifp != NULL && ifp->if_vlantrunk != NULL) {
3073                 BXE_PRINTF("%s(%d): Cannot detach while VLANs are in use.\n",
3074                     __FILE__, __LINE__);
3075                 rc = EBUSY;
3076                 goto bxe_detach_exit;
3077         }
3078
3079         /* Stop and reset the controller if it was open. */
3080         if (sc->state != BXE_STATE_CLOSED) {
3081                 BXE_CORE_LOCK(sc);
3082                 rc = bxe_stop_locked(sc, UNLOAD_CLOSE);
3083                 BXE_CORE_UNLOCK(sc);
3084         }
3085
3086 #ifdef BXE_DEBUG
3087         /* Free memory buffer for grcdump output.*/
3088         if (sc->grcdump_buffer != NULL)
3089                 free(sc->grcdump_buffer, M_TEMP);
3090 #endif
3091
3092         /* Clean-up any remaining interrupt resources. */
3093         bxe_interrupt_detach(sc);
3094         bxe_interrupt_free(sc);
3095
3096         /* Release the network interface. */
3097         if (ifp != NULL)
3098                 ether_ifdetach(ifp);
3099         ifmedia_removeall(&sc->bxe_ifmedia);
3100
3101         /* Release all remaining resources. */
3102         bxe_release_resources(sc);
3103
3104         /* Free all PCI resources. */
3105         bxe_pci_resources_free(sc);
3106         pci_disable_busmaster(dev);
3107
3108         bxe_mutexes_free(sc);
3109
3110 bxe_detach_exit:
3111         DBEXIT(BXE_INFO_UNLOAD);
3112         return(0);
3113 }
3114
3115
3116 /*
3117  * Setup a leading connection for the controller.
3118  *
3119  * Returns:
3120  *   0 = Success, !0 = Failure.
3121  */
3122 static int
3123 bxe_setup_leading(struct bxe_softc *sc)
3124 {
3125         int rc;
3126
3127         DBENTER(BXE_VERBOSE_LOAD | BXE_VERBOSE_RESET | BXE_VERBOSE_RAMROD);
3128
3129         DBPRINT(sc, BXE_VERBOSE_LOAD, "%s(): Setup leading connection "
3130             "on fp[00].\n", __FUNCTION__);
3131
3132         /* Reset IGU state for the leading connection. */
3133         bxe_ack_sb(sc, sc->fp[0].sb_id, CSTORM_ID, 0, IGU_INT_ENABLE, 0);
3134
3135         /* Post a PORT_SETUP ramrod and wait for completion. */
3136         bxe_sp_post(sc, RAMROD_CMD_ID_ETH_PORT_SETUP, 0, 0, 0, 0);
3137
3138         /* Wait for the ramrod to complete on the leading connection. */
3139         rc = bxe_wait_ramrod(sc, BXE_STATE_OPEN, 0, &(sc->state), 1);
3140
3141         DBEXIT(BXE_VERBOSE_LOAD | BXE_VERBOSE_RESET | BXE_VERBOSE_RAMROD);
3142         return (rc);
3143 }
3144
3145
3146 /*
3147  * Stop the leading connection on the controller.
3148  *
3149  * Returns:
3150  *   None.
3151  */
3152 static int
3153 bxe_stop_leading(struct bxe_softc *sc)
3154 {
3155         uint16_t dsb_sp_prod_idx;
3156         int rc, timeout;
3157
3158         DBPRINT(sc, (BXE_VERBOSE_LOAD | BXE_VERBOSE_RESET |
3159             BXE_VERBOSE_UNLOAD), "%s(): Stop client connection "
3160             "on fp[00].\n", __FUNCTION__);
3161
3162         /* Send the ETH_HALT ramrod. */
3163         sc->fp[0].state = BXE_FP_STATE_HALTING;
3164         bxe_sp_post(sc,RAMROD_CMD_ID_ETH_HALT, 0, 0, sc->fp[0].cl_id, 0);
3165
3166         /* Poll for the ETH_HALT ramrod on the leading connection. */
3167         rc = bxe_wait_ramrod(sc, BXE_FP_STATE_HALTED,
3168             0, &(sc->fp[0].state), 1);
3169         if (rc) {
3170                 DBPRINT(sc, BXE_FATAL, "%s(): Timeout waiting for "
3171                     "STATE_HALTED ramrod completion!\n", __FUNCTION__);
3172                 goto bxe_stop_leading_exit;
3173         }
3174
3175         /* Get the default status block SP producer index. */
3176         dsb_sp_prod_idx = *sc->dsb_sp_prod;
3177
3178         /* After HALT we send PORT_DELETE ramrod. */
3179         bxe_sp_post(sc, RAMROD_CMD_ID_ETH_PORT_DEL, 0, 0, 0, 1);
3180
3181         /* Be patient but don't wait forever. */
3182         timeout = 500;
3183         while (dsb_sp_prod_idx == *sc->dsb_sp_prod) {
3184                 if (timeout == 0) {
3185                         DBPRINT(sc, BXE_FATAL, "%s(): Timeout waiting for "
3186                             "PORT_DEL ramrod completion!\n", __FUNCTION__);
3187                         rc = EBUSY;
3188                         break;
3189                 }
3190                 timeout--;
3191                 DELAY(1000);
3192                 rmb();
3193         }
3194
3195         /* Update the adapter and connection states. */
3196         sc->state = BXE_STATE_CLOSING_WAIT4_UNLOAD;
3197         sc->fp[0].state = BXE_FP_STATE_CLOSED;
3198
3199 bxe_stop_leading_exit:
3200         return (rc);
3201 }
3202
3203 /*
3204  * Setup a client connection when using multi-queue/RSS.
3205  *
3206  * Returns:
3207  *   Nothing.
3208  */
3209 static int
3210 bxe_setup_multi(struct bxe_softc *sc, int index)
3211 {
3212         struct bxe_fastpath *fp;
3213         int rc;
3214
3215         DBPRINT(sc, (BXE_VERBOSE_LOAD | BXE_VERBOSE_RESET |
3216             BXE_VERBOSE_UNLOAD), "%s(): Setup client connection "
3217             "on fp[%02d].\n", __FUNCTION__, index);
3218
3219         fp = &sc->fp[index];
3220         /* Reset IGU state. */
3221         bxe_ack_sb(sc, fp->sb_id, CSTORM_ID, 0, IGU_INT_ENABLE, 0);
3222
3223         /* Post a CLIENT_SETUP ramrod. */
3224         fp->state = BXE_FP_STATE_OPENING;
3225         bxe_sp_post(sc, RAMROD_CMD_ID_ETH_CLIENT_SETUP, index, 0, fp->cl_id, 0);
3226
3227         /* Wait for the ramrod to complete. */
3228         rc = bxe_wait_ramrod(sc, BXE_FP_STATE_OPEN, index, &fp->state, 1);
3229
3230         return (rc);
3231 }
3232
3233 /*
3234  * Stop a client connection.
3235  *
3236  * Stops an individual client connection on the device.  Use
3237  * bxe_stop_leading() for the first/default connection.
3238  *
3239  * Returns:
3240  *   0 = Success, !0 = Failure.
3241  */
3242 static int
3243 bxe_stop_multi(struct bxe_softc *sc, int index)
3244 {
3245         struct bxe_fastpath *fp;
3246         int rc;
3247
3248         DBPRINT(sc, (BXE_VERBOSE_LOAD | BXE_VERBOSE_RESET |
3249             BXE_VERBOSE_UNLOAD), "%s(): Stop client connection "
3250             "on fp[%02d].\n", __FUNCTION__, index);
3251
3252         fp = &sc->fp[index];
3253
3254         /* Halt the client connection. */
3255         fp->state = BXE_FP_STATE_HALTING;
3256         bxe_sp_post(sc, RAMROD_CMD_ID_ETH_HALT, index, 0, fp->cl_id, 0);
3257
3258         /* Wait for the HALT ramrod completion. */
3259         rc = bxe_wait_ramrod(sc, BXE_FP_STATE_HALTED, index, &fp->state, 1);
3260         if (rc){
3261                 BXE_PRINTF("%s(%d): fp[%02d] client ramrod halt failed!\n",
3262                     __FILE__, __LINE__, index);
3263                 goto bxe_stop_multi_exit;
3264         }
3265         /* Delete the CFC entry. */
3266         bxe_sp_post(sc, RAMROD_CMD_ID_ETH_CFC_DEL, index, 0, 0, 1);
3267
3268         /* Poll for the DELETE ramrod completion. */
3269         rc = bxe_wait_ramrod(sc, BXE_FP_STATE_CLOSED, index, &fp->state, 1);
3270
3271 bxe_stop_multi_exit:
3272         return (rc);
3273 }
3274
3275 /*
3276  * Hardware lock for shared, dual-port PHYs.
3277  *
3278  * Returns:
3279  *   None.
3280  */
3281 static void
3282 bxe_acquire_phy_lock(struct bxe_softc *sc)
3283 {
3284         uint32_t ext_phy_type;
3285
3286         DBENTER(BXE_VERBOSE_PHY);
3287
3288         ext_phy_type = XGXS_EXT_PHY_TYPE(sc->link_params.ext_phy_config);
3289         switch(ext_phy_type){
3290                 case PORT_HW_CFG_XGXS_EXT_PHY_TYPE_BCM8072:
3291                 case PORT_HW_CFG_XGXS_EXT_PHY_TYPE_BCM8073:
3292                 case PORT_HW_CFG_XGXS_EXT_PHY_TYPE_BCM8726:
3293                 case PORT_HW_CFG_XGXS_EXT_PHY_TYPE_BCM8727:
3294                         bxe_acquire_hw_lock(sc, HW_LOCK_RESOURCE_MDIO);
3295                         break;
3296                 default:
3297                         break;
3298         }
3299         DBEXIT(BXE_VERBOSE_PHY);
3300 }
3301
3302 /*
3303  * Hardware unlock for shared, dual-port PHYs.
3304  *
3305  * Returns:
3306  *   None.
3307  */
3308 static void
3309 bxe_release_phy_lock(struct bxe_softc *sc)
3310 {
3311         uint32_t ext_phy_type;
3312
3313         DBENTER(BXE_VERBOSE_PHY);
3314         ext_phy_type = XGXS_EXT_PHY_TYPE(sc->link_params.ext_phy_config);
3315         switch(ext_phy_type){
3316                 case PORT_HW_CFG_XGXS_EXT_PHY_TYPE_BCM8072:
3317                 case PORT_HW_CFG_XGXS_EXT_PHY_TYPE_BCM8073:
3318                 case PORT_HW_CFG_XGXS_EXT_PHY_TYPE_BCM8726:
3319                 case PORT_HW_CFG_XGXS_EXT_PHY_TYPE_BCM8727:
3320                         bxe_release_hw_lock(sc, HW_LOCK_RESOURCE_MDIO);
3321                         break;
3322                 default:
3323                         break;
3324         }
3325
3326         DBEXIT(BXE_VERBOSE_PHY);
3327 }
3328
3329 /*
3330  *
3331  * Returns:
3332  *   None.
3333  */
3334 static void
3335 bxe__link_reset(struct bxe_softc *sc)
3336 {
3337         DBENTER(BXE_VERBOSE_PHY);
3338
3339         if (!NOMCP(sc)) {
3340                 bxe_acquire_phy_lock(sc);
3341                 bxe_link_reset(&sc->link_params, &sc->link_vars, 1);
3342                 bxe_release_phy_lock(sc);
3343         } else {
3344                 DBPRINT(sc, BXE_WARN,
3345                     "%s(): Bootcode is not running, not resetting link!\n",
3346                     __FUNCTION__);
3347         }
3348
3349         DBEXIT(BXE_VERBOSE_PHY);
3350 }
3351
3352 /*
3353  * Stop the controller.
3354  *
3355  * Returns:
3356  *   0 = Success, !0 = Failure
3357  */
3358 static int
3359 bxe_stop_locked(struct bxe_softc *sc, int unload_mode)
3360 {
3361         struct ifnet *ifp;
3362         struct mac_configuration_cmd *config;
3363         struct bxe_fastpath *fp;
3364         uint32_t reset_code;
3365         uint32_t emac_base, val;
3366         uint8_t entry, *mac_addr;
3367         int count, i, port, rc;
3368
3369         DBENTER(BXE_INFO_LOAD | BXE_INFO_RESET | BXE_INFO_UNLOAD);
3370
3371         ifp = sc->bxe_ifp;
3372         port = BP_PORT(sc),
3373         rc = reset_code = 0;
3374
3375         BXE_CORE_LOCK_ASSERT(sc);
3376
3377         /* Stop the periodic tick. */
3378         callout_stop(&sc->bxe_tick_callout);
3379
3380         sc->state = BXE_STATE_CLOSING_WAIT4_HALT;
3381
3382         /* Prevent any further RX traffic. */
3383         sc->rx_mode = BXE_RX_MODE_NONE;
3384         bxe_set_storm_rx_mode(sc);
3385
3386         /* Tell the stack the driver is stopped and TX queue is full. */
3387         if (ifp != NULL)
3388                 ifp->if_drv_flags = 0;
3389
3390         /* Tell the bootcode to stop watching for a heartbeat. */
3391         SHMEM_WR(sc, func_mb[BP_FUNC(sc)].drv_pulse_mb,
3392             (DRV_PULSE_ALWAYS_ALIVE | sc->fw_drv_pulse_wr_seq));
3393
3394         /* Stop the statistics updates. */
3395         bxe_stats_handle(sc, STATS_EVENT_STOP);
3396
3397         /* Wait until all TX fastpath tasks have completed. */
3398         for (i = 0; i < sc->num_queues; i++) {
3399                 fp = &sc->fp[i];
3400
3401                 if (fp == NULL || fp->tx_pkt_cons_sb == NULL)
3402                         break;
3403
3404                 count = 1000;
3405                 while (bxe_has_tx_work(fp)) {
3406
3407                         bxe_txeof(fp);
3408
3409                         if (count == 0) {
3410                                 BXE_PRINTF(
3411                 "%s(%d): Timeout wating for fp[%02d] transmits to complete!\n",
3412                                     __FILE__, __LINE__, i);
3413                                 break;
3414                         }
3415                         count--;
3416                         DELAY(1000);
3417                         rmb();
3418                 }
3419         }
3420
3421         /* Wait until all slowpath tasks have completed. */
3422         count = 1000;
3423         while ((sc->spq_left != MAX_SPQ_PENDING) && count--)
3424                 DELAY(1000);
3425
3426         /* Disable Interrupts */
3427         bxe_int_disable(sc);
3428         DELAY(1000);
3429
3430         /* Clear the MAC addresses. */
3431         if (CHIP_IS_E1(sc)) {
3432                 config = BXE_SP(sc, mcast_config);
3433                 bxe_set_mac_addr_e1(sc, 0);
3434
3435                 for (i = 0; i < config->hdr.length; i++)
3436                         CAM_INVALIDATE(&config->config_table[i]);
3437
3438                 config->hdr.length = i;
3439                 config->hdr.offset = BXE_MAX_MULTICAST * (1 + port);
3440                 config->hdr.client_id = BP_CL_ID(sc);
3441                 config->hdr.reserved1 = 0;
3442
3443                 bxe_sp_post(sc, RAMROD_CMD_ID_ETH_SET_MAC, 0,
3444                     U64_HI(BXE_SP_MAPPING(sc, mcast_config)),
3445                     U64_LO(BXE_SP_MAPPING(sc, mcast_config)), 0);
3446         } else {
3447                 REG_WR(sc, NIG_REG_LLH0_FUNC_EN + port * 8, 0);
3448                 bxe_set_mac_addr_e1h(sc, 0);
3449                 for (i = 0; i < MC_HASH_SIZE; i++)
3450                         REG_WR(sc, MC_HASH_OFFSET(sc, i), 0);
3451                 REG_WR(sc, MISC_REG_E1HMF_MODE, 0);
3452         }
3453
3454         /* Determine if any WoL settings needed. */
3455         if (unload_mode == UNLOAD_NORMAL)
3456                 /* Driver initiatied WoL is disabled. */
3457                 reset_code = DRV_MSG_CODE_UNLOAD_REQ_WOL_DIS;
3458         else if (sc->bxe_flags & BXE_NO_WOL_FLAG) {
3459                 /* Driver initiated WoL is disabled, use OOB WoL settings. */
3460                 reset_code = DRV_MSG_CODE_UNLOAD_REQ_WOL_MCP;
3461                 if (CHIP_IS_E1H(sc))
3462                         REG_WR(sc, MISC_REG_E1HMF_MODE, 0);
3463         } else if (sc->wol) {
3464                 emac_base = BP_PORT(sc) ?  GRCBASE_EMAC0 : GRCBASE_EMAC1;
3465                 mac_addr = sc->link_params.mac_addr;
3466                 entry = (BP_E1HVN(sc) + 1) * 8;
3467                 val = (mac_addr[0] << 8) | mac_addr[1];
3468                 EMAC_WR(sc, EMAC_REG_EMAC_MAC_MATCH + entry, val);
3469                 val = (mac_addr[2] << 24) | (mac_addr[3] << 16) |
3470                     (mac_addr[4] << 8) | mac_addr[5];
3471                 EMAC_WR(sc, EMAC_REG_EMAC_MAC_MATCH + entry + 4, val);
3472                 reset_code = DRV_MSG_CODE_UNLOAD_REQ_WOL_EN;
3473         } else {
3474                 /* Prevent WoL. */
3475                 reset_code = DRV_MSG_CODE_UNLOAD_REQ_WOL_DIS;
3476         }
3477
3478         /* Stop all non-leading client connections. */
3479         for (i = 1; i < sc->num_queues; i++) {
3480                 if (bxe_stop_multi(sc, i)){
3481                         goto bxe_stop_locked_exit;
3482                 }
3483         }
3484
3485         /* Stop the leading client connection. */
3486         rc = bxe_stop_leading(sc);
3487         DELAY(10000);
3488
3489 bxe_stop_locked_exit:
3490         if (NOMCP(sc)) {
3491                 DBPRINT(sc, BXE_INFO,
3492                     "%s(): Old No MCP load counts:  %d, %d, %d\n",
3493                     __FUNCTION__, load_count[0], load_count[1], load_count[2]);
3494
3495                 load_count[0]--;
3496                 load_count[1 + port]--;
3497                 DBPRINT(sc, BXE_INFO,
3498                     "%s(): New No MCP load counts:  %d, %d, %d\n",
3499                     __FUNCTION__, load_count[0], load_count[1], load_count[2]);
3500
3501                 if (load_count[0] == 0)
3502                         reset_code = FW_MSG_CODE_DRV_UNLOAD_COMMON;
3503                 else if (load_count[1 + BP_PORT(sc)] == 0)
3504                         reset_code = FW_MSG_CODE_DRV_UNLOAD_PORT;
3505                 else
3506                         reset_code = FW_MSG_CODE_DRV_UNLOAD_FUNCTION;
3507         } else {
3508                 /* Tell MCP driver unload is complete. */
3509                 reset_code = bxe_fw_command(sc, reset_code);
3510         }
3511
3512         if ((reset_code == FW_MSG_CODE_DRV_UNLOAD_COMMON) ||
3513             (reset_code == FW_MSG_CODE_DRV_UNLOAD_PORT))
3514                 bxe__link_reset(sc);
3515
3516         DELAY(10000);
3517
3518         /* Reset the chip */
3519         bxe_reset_chip(sc, reset_code);
3520
3521         DELAY(10000);
3522
3523         /* Report UNLOAD_DONE to MCP */
3524         if (!NOMCP(sc))
3525                 bxe_fw_command(sc, DRV_MSG_CODE_UNLOAD_DONE);
3526         sc->port.pmf = 0;
3527
3528         /* Free RX chains and buffers. */
3529         bxe_clear_rx_chains(sc);
3530
3531         /* Free TX chains and buffers. */
3532         bxe_clear_tx_chains(sc);
3533
3534         sc->state = BXE_STATE_CLOSED;
3535
3536         bxe_ack_int(sc);
3537
3538         DBEXIT(BXE_INFO_LOAD | BXE_INFO_RESET |BXE_INFO_UNLOAD);
3539         return (rc);
3540 }
3541
3542 /*
3543  * Device shutdown function.
3544  *
3545  * Stops and resets the controller.
3546  *
3547  * Returns:
3548  *   0 = Success, !0 = Failure
3549  */
3550 static int
3551 bxe_shutdown(device_t dev)
3552 {
3553         struct bxe_softc *sc;
3554
3555         sc = device_get_softc(dev);
3556         DBENTER(BXE_INFO_LOAD | BXE_INFO_RESET | BXE_INFO_UNLOAD);
3557
3558         BXE_CORE_LOCK(sc);
3559         bxe_stop_locked(sc, UNLOAD_NORMAL);
3560         BXE_CORE_UNLOCK(sc);
3561
3562         DBEXIT(BXE_INFO_LOAD | BXE_INFO_RESET | BXE_INFO_UNLOAD);
3563         return (0);
3564 }
3565
3566 /*
3567  * Prints out link speed and duplex setting to console.
3568  *
3569  * Returns:
3570  *   None.
3571  */
3572 static void
3573 bxe_link_report(struct bxe_softc *sc)
3574 {
3575         uint32_t line_speed;
3576         uint16_t vn_max_rate;
3577
3578         DBENTER(BXE_VERBOSE_PHY);
3579
3580         if (sc->link_vars.link_up) {
3581                 /* Report the link status change to OS. */
3582                 if (sc->state == BXE_STATE_OPEN)
3583                         if_link_state_change(sc->bxe_ifp, LINK_STATE_UP);
3584
3585                 line_speed = sc->link_vars.line_speed;
3586
3587                 if (IS_E1HMF(sc)){
3588                         vn_max_rate = ((sc->mf_config[BP_E1HVN(sc)] &
3589                             FUNC_MF_CFG_MAX_BW_MASK) >>
3590                             FUNC_MF_CFG_MAX_BW_SHIFT) * 100;
3591                         if (vn_max_rate < line_speed)
3592                                 line_speed = vn_max_rate;
3593                 }
3594
3595                 BXE_PRINTF("Link is up, %d Mbps, ", line_speed);
3596
3597                 if (sc->link_vars.duplex == MEDIUM_FULL_DUPLEX)
3598                         printf("full duplex");
3599                 else
3600                         printf("half duplex");
3601
3602                 if (sc->link_vars.flow_ctrl) {
3603                         if (sc->link_vars.flow_ctrl & FLOW_CTRL_RX) {
3604                                 printf(", receive ");
3605                                 if (sc->link_vars.flow_ctrl & FLOW_CTRL_TX)
3606                                         printf("& transmit ");
3607                         } else
3608                                 printf(", transmit ");
3609                         printf("flow control ON");
3610                 }
3611                 printf("\n");
3612         } else {
3613                 /* Report the link down */
3614                 BXE_PRINTF("Link is down\n");
3615                 if_link_state_change(sc->bxe_ifp, LINK_STATE_DOWN);
3616         }
3617
3618         DBEXIT(BXE_VERBOSE_PHY);
3619 }
3620
3621 /*
3622  *
3623  * Returns:
3624  *   None.
3625  */
3626 static void
3627 bxe__link_status_update(struct bxe_softc *sc)
3628 {
3629         DBENTER(BXE_VERBOSE_PHY);
3630
3631         if (sc->stats_enable == FALSE || sc->state != BXE_STATE_OPEN)
3632                 return;
3633
3634         bxe_link_status_update(&sc->link_params, &sc->link_vars);
3635
3636         if (sc->link_vars.link_up)
3637                 bxe_stats_handle(sc, STATS_EVENT_LINK_UP);
3638         else
3639                 bxe_stats_handle(sc, STATS_EVENT_STOP);
3640
3641         bxe_read_mf_cfg(sc);
3642
3643         /* Indicate link status. */
3644         bxe_link_report(sc);
3645
3646         DBEXIT(BXE_VERBOSE_PHY);
3647 }
3648
3649 /*
3650  * Calculate flow control to advertise during autonegotiation.
3651  *
3652  * Returns:
3653  *   None.
3654  */
3655 static void
3656 bxe_calc_fc_adv(struct bxe_softc *sc)
3657 {
3658         DBENTER(BXE_EXTREME_PHY);
3659
3660         switch (sc->link_vars.ieee_fc &
3661             MDIO_COMBO_IEEE0_AUTO_NEG_ADV_PAUSE_MASK) {
3662
3663         case MDIO_COMBO_IEEE0_AUTO_NEG_ADV_PAUSE_NONE:
3664                 sc->port.advertising &= ~(ADVERTISED_Asym_Pause |
3665                     ADVERTISED_Pause);
3666                 break;
3667
3668         case MDIO_COMBO_IEEE0_AUTO_NEG_ADV_PAUSE_BOTH:
3669                 sc->port.advertising |= (ADVERTISED_Asym_Pause |
3670                     ADVERTISED_Pause);
3671                 break;
3672
3673         case MDIO_COMBO_IEEE0_AUTO_NEG_ADV_PAUSE_ASYMMETRIC:
3674                 sc->port.advertising |= ADVERTISED_Asym_Pause;
3675                 break;
3676
3677         default:
3678                 sc->port.advertising &= ~(ADVERTISED_Asym_Pause |
3679                     ADVERTISED_Pause);
3680                 break;
3681         }
3682
3683         DBEXIT(BXE_EXTREME_PHY);
3684 }
3685
3686
3687
3688 /*
3689  *
3690  * Returns:
3691  *
3692  */
3693 static uint8_t
3694 bxe_initial_phy_init(struct bxe_softc *sc)
3695 {
3696         uint8_t rc;
3697
3698         DBENTER(BXE_VERBOSE_PHY);
3699
3700         rc = 0;
3701         if (!NOMCP(sc)) {
3702
3703                 /*
3704                  * It is recommended to turn off RX flow control for 5771x
3705                  * when using jumbo frames for better performance.
3706                  */
3707                 if (!IS_E1HMF(sc) && (sc->mbuf_alloc_size > 5000))
3708                         sc->link_params.req_fc_auto_adv = FLOW_CTRL_TX;
3709                 else
3710                         sc->link_params.req_fc_auto_adv = FLOW_CTRL_BOTH;
3711
3712                 bxe_acquire_phy_lock(sc);
3713                 rc = bxe_phy_init(&sc->link_params, &sc->link_vars);
3714                 bxe_release_phy_lock(sc);
3715
3716                 bxe_calc_fc_adv(sc);
3717
3718                 if (sc->link_vars.link_up) {
3719                     bxe_stats_handle(sc,STATS_EVENT_LINK_UP);
3720                     bxe_link_report(sc);
3721                 }
3722
3723         } else {
3724                 DBPRINT(sc, BXE_FATAL, "%s(): Bootcode is not running, "
3725                     "not initializing link!\n", __FUNCTION__);
3726                 rc = EINVAL;
3727         }
3728
3729         DBEXIT(BXE_VERBOSE_PHY);
3730         return (rc);
3731 }
3732
3733
3734 #if __FreeBSD_version >= 800000
3735 /*
3736  * Allocate buffer rings used for multiqueue.
3737  *
3738  * Returns:
3739  *   0 = Success, !0 = Failure.
3740  */
3741 static int
3742 bxe_alloc_buf_rings(struct bxe_softc *sc)
3743 {
3744         struct bxe_fastpath *fp;
3745         int i, rc;
3746
3747         DBENTER(BXE_VERBOSE_LOAD);
3748         rc = 0;
3749
3750         for (i = 0; i < sc->num_queues; i++) {
3751                 fp = &sc->fp[i];
3752
3753                 if (fp != NULL) {
3754                         fp->br = buf_ring_alloc(BXE_BR_SIZE,
3755                             M_DEVBUF, M_DONTWAIT, &fp->mtx);
3756                         if (fp->br == NULL) {
3757                                 rc = ENOMEM;
3758                                 goto bxe_alloc_buf_rings_exit;
3759                         }
3760                 } else
3761                         BXE_PRINTF("%s(%d): Bug!\n", __FILE__, __LINE__);
3762         }
3763
3764 bxe_alloc_buf_rings_exit:
3765         DBEXIT(BXE_VERBOSE_LOAD);
3766         return (rc);
3767 }
3768
3769 /*
3770  * Releases buffer rings used for multiqueue.
3771  *
3772  * Returns:
3773  *   None
3774  */
3775 static void
3776 bxe_free_buf_rings(struct bxe_softc *sc)
3777 {
3778         struct bxe_fastpath *fp;
3779         int i;
3780
3781         DBENTER(BXE_VERBOSE_UNLOAD);
3782
3783         for (i = 0; i < sc->num_queues; i++) {
3784                 fp = &sc->fp[i];
3785                 if (fp != NULL) {
3786                         if (fp->br != NULL)
3787                                 buf_ring_free(fp->br, M_DEVBUF);
3788                 }
3789         }
3790
3791         DBEXIT(BXE_VERBOSE_UNLOAD);
3792 }
3793 #endif
3794
3795
3796 /*
3797  * Handles controller initialization.
3798  *
3799  * Must be called from a locked routine.  Since this code
3800  * may be called from the OS it does not provide a return
3801  * error value and must clean-up it's own mess.
3802  *
3803  * Returns:
3804  *   Nothing.
3805  */
3806 static void
3807 bxe_init_locked(struct bxe_softc *sc, int load_mode)
3808 {
3809         struct ifnet *ifp;
3810         uint32_t load_code;
3811         int error, i, port;
3812
3813         DBENTER(BXE_INFO_LOAD | BXE_INFO_RESET);
3814
3815         BXE_CORE_LOCK_ASSERT(sc);
3816
3817         ifp = sc->bxe_ifp;
3818         /* Skip if we're in panic mode. */
3819         if (sc->panic) {
3820                 DBPRINT(sc, BXE_WARN, "%s(): Panic mode enabled, exiting!\n",
3821                     __FUNCTION__);
3822                 goto bxe_init_locked_exit;
3823         }
3824
3825         /* Check if the driver is still running and bail out if it is. */
3826         if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
3827                 DBPRINT(sc, BXE_WARN,
3828                     "%s(): Init called while driver is running!\n",
3829                     __FUNCTION__);
3830                 goto bxe_init_locked_exit;
3831         }
3832
3833         /*
3834          * Send LOAD_REQUEST command to MCP.
3835          * The MCP will return the type of LOAD
3836          * the driver should perform.
3837          * - If it is the first port to be initialized
3838          *   then all common blocks should be initialized.
3839          * - If it is not the first port to be initialized
3840          *   then don't do the common block initialization.
3841          */
3842         sc->state = BXE_STATE_OPENING_WAIT4_LOAD;
3843
3844         if (NOMCP(sc)) {
3845                 port = BP_PORT(sc);
3846
3847                 DBPRINT(sc, BXE_INFO,
3848                     "%s(): Old No MCP load counts:  %d, %d, %d\n",
3849                     __FUNCTION__,
3850                     load_count[0], load_count[1], load_count[2]);
3851
3852                 load_count[0]++;
3853                 load_count[1 + port]++;
3854
3855                 DBPRINT(sc, BXE_INFO,
3856                     "%s(): New No MCP load counts:  %d, %d, %d\n",
3857                     __FUNCTION__,
3858                     load_count[0], load_count[1], load_count[2]);
3859
3860                 /* No MCP to tell us what to do. */
3861                 if (load_count[0] == 1)
3862                         load_code = FW_MSG_CODE_DRV_LOAD_COMMON;
3863                 else if (load_count[1 + port] == 1)
3864                         load_code = FW_MSG_CODE_DRV_LOAD_PORT;
3865                 else
3866                         load_code = FW_MSG_CODE_DRV_LOAD_FUNCTION;
3867
3868         } else {
3869                 /* Ask the MCP what type of initialization we need to do. */
3870                 load_code = bxe_fw_command(sc, DRV_MSG_CODE_LOAD_REQ);
3871
3872                 if ((load_code == 0) ||
3873                     (load_code == FW_MSG_CODE_DRV_LOAD_REFUSED)) {
3874                         BXE_PRINTF("%s(%d): Bootcode refused load request.!\n",
3875                             __FILE__, __LINE__);
3876                         goto bxe_init_locked_failed1;
3877                 }
3878         }
3879
3880         /* Keep track of whether we are controlling the port. */
3881         if ((load_code == FW_MSG_CODE_DRV_LOAD_COMMON) ||
3882             (load_code == FW_MSG_CODE_DRV_LOAD_PORT))
3883                 sc->port.pmf = 1;
3884         else
3885                 sc->port.pmf = 0;
3886
3887         /* Block any interrupts until we're ready. */
3888         sc->intr_sem = 1;
3889
3890         /* Initialize hardware. */
3891         error = bxe_init_hw(sc, load_code);
3892         if (error != 0){
3893                 BXE_PRINTF("%s(%d): Hardware initialization failed, "
3894                     "aborting!\n", __FILE__, __LINE__);
3895                 goto bxe_init_locked_failed1;
3896         }
3897
3898         /* Calculate and save the Ethernet MTU size. */
3899         sc->port.ether_mtu = ifp->if_mtu + ETHER_HDR_LEN +
3900             (ETHER_VLAN_ENCAP_LEN * 2) + ETHER_CRC_LEN + 4;
3901
3902         DBPRINT(sc, BXE_INFO, "%s(): Setting MTU = %d\n",
3903             __FUNCTION__, sc->port.ether_mtu);
3904
3905         /* Setup the mbuf allocation size for RX frames. */
3906         if (sc->port.ether_mtu <= MCLBYTES)
3907                 sc->mbuf_alloc_size = MCLBYTES;
3908         else if (sc->port.ether_mtu <= PAGE_SIZE)
3909                 sc->mbuf_alloc_size = PAGE_SIZE;
3910         else
3911                 sc->mbuf_alloc_size = MJUM9BYTES;
3912
3913         DBPRINT(sc, BXE_INFO, "%s(): mbuf_alloc_size = %d, "
3914             "max_frame_size = %d\n", __FUNCTION__,
3915             sc->mbuf_alloc_size, sc->port.ether_mtu);
3916
3917         /* Setup NIC internals and enable interrupts. */
3918         error = bxe_init_nic(sc, load_code);
3919         if (error != 0) {
3920                 BXE_PRINTF("%s(%d): NIC initialization failed, "
3921                     "aborting!\n", __FILE__, __LINE__);
3922                 goto bxe_init_locked_failed1;
3923         }
3924
3925         if ((load_code == FW_MSG_CODE_DRV_LOAD_COMMON) &&
3926             (sc->common.shmem2_base)){
3927                 if (sc->dcc_enable == TRUE) {
3928                         BXE_PRINTF("Enabing DCC support\n");
3929                         SHMEM2_WR(sc, dcc_support,
3930                             (SHMEM_DCC_SUPPORT_DISABLE_ENABLE_PF_TLV |
3931                              SHMEM_DCC_SUPPORT_BANDWIDTH_ALLOCATION_TLV));
3932                 }
3933         }
3934
3935 #if __FreeBSD_version >= 800000
3936         /* Allocate buffer rings for multiqueue operation. */
3937         error = bxe_alloc_buf_rings(sc);
3938         if (error != 0) {
3939                 BXE_PRINTF("%s(%d): Buffer ring initialization failed, "
3940                     "aborting!\n", __FILE__, __LINE__);
3941                 goto bxe_init_locked_failed1;
3942         }
3943 #endif
3944
3945         /* Tell MCP that driver load is done. */
3946         if (!NOMCP(sc)) {
3947                 load_code = bxe_fw_command(sc, DRV_MSG_CODE_LOAD_DONE);
3948                 if (!load_code) {
3949                         BXE_PRINTF("%s(%d): Driver load failed! No MCP "
3950                             "response to LOAD_DONE!\n", __FILE__, __LINE__);
3951                         goto bxe_init_locked_failed2;
3952                 }
3953         }
3954
3955         sc->state = BXE_STATE_OPENING_WAIT4_PORT;
3956
3957         /* Enable ISR for PORT_SETUP ramrod. */
3958         sc->intr_sem = 0;
3959
3960         /* Setup the leading connection for the controller. */
3961         error = bxe_setup_leading(sc);
3962         if (error != 0) {
3963                 DBPRINT(sc, BXE_FATAL, "%s(): Initial PORT_SETUP ramrod "
3964                     "failed. State is not OPEN!\n", __FUNCTION__);
3965                 goto bxe_init_locked_failed3;
3966         }
3967
3968         if (CHIP_IS_E1H(sc)) {
3969                 if (sc->mf_config[BP_E1HVN(sc)] & FUNC_MF_CFG_FUNC_DISABLED) {
3970                         BXE_PRINTF("Multi-function mode is disabled\n");
3971                         /* sc->state = BXE_STATE_DISABLED; */
3972                 }
3973
3974                 /* Setup additional client connections for RSS/multi-queue */
3975                 if (sc->state == BXE_STATE_OPEN) {
3976                         for (i = 1; i < sc->num_queues; i++) {
3977                                 if (bxe_setup_multi(sc, i)) {
3978                                         DBPRINT(sc, BXE_FATAL,
3979                 "%s(): fp[%02d] CLIENT_SETUP ramrod failed! State not OPEN!\n",
3980                                             __FUNCTION__, i);
3981                                         goto bxe_init_locked_failed4;
3982                                 }
3983                         }
3984                 }
3985         }
3986
3987         DELAY(5000);
3988         bxe_int_enable(sc);
3989         DELAY(5000);
3990         /* Initialize statistics. */
3991         bxe_stats_init(sc);
3992         DELAY(1000);
3993
3994         /* Load our MAC address. */
3995         bcopy(IF_LLADDR(sc->bxe_ifp), sc->link_params.mac_addr, ETHER_ADDR_LEN);
3996
3997         if (CHIP_IS_E1(sc))
3998                 bxe_set_mac_addr_e1(sc, 1);
3999         else
4000                 bxe_set_mac_addr_e1h(sc, 1);
4001
4002         DELAY(1000);
4003
4004         /* Perform PHY initialization for the primary port. */
4005         if (sc->port.pmf)
4006                 bxe_initial_phy_init(sc);
4007
4008         DELAY(1000);
4009
4010         /* Start fastpath. */
4011         switch (load_mode) {
4012         case LOAD_NORMAL:
4013         case LOAD_OPEN:
4014                 /* Initialize the receive filters. */
4015                 bxe_set_rx_mode(sc);
4016                 break;
4017
4018         case LOAD_DIAG:
4019                 /* Initialize the receive filters. */
4020                 bxe_set_rx_mode(sc);
4021                 sc->state = BXE_STATE_DIAG;
4022                 break;
4023
4024         default:
4025                 DBPRINT(sc, BXE_WARN, "%s(): Unknown load mode (%d)!\n",
4026                     __FUNCTION__, load_mode);
4027                 break;
4028         }
4029
4030         if (!sc->port.pmf)
4031                 bxe__link_status_update(sc);
4032
4033         DELAY(1000);
4034         /* Tell the stack the driver is running. */
4035         ifp->if_drv_flags = IFF_DRV_RUNNING;
4036
4037         /* Schedule our periodic timer tick. */
4038         callout_reset(&sc->bxe_tick_callout, hz, bxe_tick, sc);
4039         /* Everything went OK, go ahead and exit. */
4040         goto bxe_init_locked_exit;
4041
4042 bxe_init_locked_failed4:
4043         /* Try and gracefully shutdown the device because of a failure. */
4044         for (i = 1; i < sc->num_queues; i++)
4045                 bxe_stop_multi(sc, i);
4046
4047 bxe_init_locked_failed3:
4048         bxe_stop_leading(sc);
4049         bxe_stats_handle(sc, STATS_EVENT_STOP);
4050
4051 bxe_init_locked_failed2:
4052         bxe_int_disable(sc);
4053
4054 bxe_init_locked_failed1:
4055         if (!NOMCP(sc)) {
4056                 bxe_fw_command(sc, DRV_MSG_CODE_LOAD_DONE);
4057                 bxe_fw_command(sc, DRV_MSG_CODE_UNLOAD_REQ_WOL_MCP);
4058                 bxe_fw_command(sc, DRV_MSG_CODE_UNLOAD_DONE);
4059         }
4060         sc->port.pmf = 0;
4061
4062 #if __FreeBSD_version >= 800000
4063         bxe_free_buf_rings(sc);
4064 #endif
4065
4066         DBPRINT(sc, BXE_WARN, "%s(): Initialization failed!\n", __FUNCTION__);
4067
4068 bxe_init_locked_exit:
4069         DBEXIT(BXE_INFO_LOAD | BXE_INFO_RESET);
4070 }
4071
4072 /*
4073  * Ramrod wait function.
4074  *
4075  * Waits for a ramrod command to complete.
4076  *
4077  * Returns:
4078  *   0 = Success, !0 = Failure
4079  */
4080 static int
4081 bxe_wait_ramrod(struct bxe_softc *sc, int state, int idx, int *state_p,
4082     int poll)
4083 {
4084         int rc, timeout;
4085
4086         DBENTER(BXE_VERBOSE_LOAD | BXE_VERBOSE_RESET | BXE_VERBOSE_RAMROD);
4087
4088         DBPRINT(sc, BXE_VERBOSE_RAMROD, "%s(): %s for state 0x%08X on "
4089             "fp[%02d], currently 0x%08X.\n", __FUNCTION__,
4090             poll ? "Polling" : "Waiting", state, idx, *state_p);
4091
4092         rc = 0;
4093         timeout = 5000;
4094         while (timeout) {
4095
4096                 /* Manually check for the completion. */
4097                 if (poll) {
4098                         bxe_rxeof(sc->fp);
4099                         /*
4100                          * Some commands don't use the leading client
4101                          * connection.
4102                          */
4103                         if (idx)
4104                                 bxe_rxeof(&sc->fp[idx]);
4105                 }
4106
4107                 /* State may be changed by bxe_sp_event(). */
4108                 mb();
4109                 if (*state_p == state)
4110                         goto bxe_wait_ramrod_exit;
4111
4112                 timeout--;
4113
4114                 /* Pause 1ms before checking again. */
4115                 DELAY(1000);
4116         }
4117
4118         /* We timed out polling for a completion. */
4119         DBPRINT(sc, BXE_FATAL, "%s(): Timeout %s for state 0x%08X on fp[%02d]. "
4120             "Got 0x%x instead\n", __FUNCTION__, poll ? "polling" : "waiting",
4121             state, idx, *state_p);
4122
4123         rc = EBUSY;
4124
4125 bxe_wait_ramrod_exit:
4126
4127         DBEXIT(BXE_VERBOSE_LOAD | BXE_VERBOSE_RESET | BXE_VERBOSE_RAMROD);
4128         return (rc);
4129 }
4130
4131 /*
4132  *
4133  *
4134  */
4135 static void
4136 bxe_write_dmae_phys_len(struct bxe_softc *sc, bus_addr_t phys_addr,
4137     uint32_t addr, uint32_t len)
4138 {
4139         int dmae_wr_max, offset;
4140         DBENTER(BXE_INSANE_REGS);
4141
4142         dmae_wr_max = DMAE_LEN32_WR_MAX(sc);
4143         offset = 0;
4144         while (len > dmae_wr_max) {
4145                 bxe_write_dmae(sc, phys_addr + offset, addr + offset,
4146                     dmae_wr_max);
4147                 offset += dmae_wr_max * 4;
4148                 len -= dmae_wr_max;
4149         }
4150         bxe_write_dmae(sc, phys_addr + offset, addr + offset, len);
4151         DBEXIT(BXE_INSANE_REGS);
4152
4153 }
4154
4155
4156
4157 #define INIT_MEM_WR(block, reg, part, hw, data, reg_off, len) \
4158         bxe_init_str_wr(sc, GRCBASE_##block + reg + reg_off * 4, data, len)
4159
4160
4161 /*
4162  * Write a block of data to a range of registers.
4163  *
4164  * Returns:
4165  *   None.
4166  */
4167 static void
4168 bxe_init_str_wr(struct bxe_softc *sc, uint32_t addr, const uint32_t *data,
4169     uint32_t len)
4170 {
4171         uint32_t i;
4172         for (i = 0; i < len; i++)
4173                 REG_WR(sc, addr + i * 4, data[i]);
4174 }
4175
4176 /*
4177  * Write a block of data to a range of registers using indirect access.
4178  *
4179  * Returns:
4180  *   None.
4181  */
4182 static void
4183 bxe_init_ind_wr(struct bxe_softc *sc, uint32_t addr, const uint32_t *data,
4184     uint16_t len)
4185 {
4186         uint32_t i;
4187         for (i = 0; i < len; i++)
4188                 REG_WR_IND(sc, addr + i * 4, data[i]);
4189 }
4190
4191 /*
4192  *
4193  * Returns:
4194  *   None.
4195  */
4196 static void
4197 bxe_write_big_buf(struct bxe_softc *sc, uint32_t addr, uint32_t len)
4198 {
4199         DBENTER(BXE_INSANE_REGS);
4200 #ifdef BXE_USE_DMAE
4201         if (sc->dmae_ready)
4202                 bxe_write_dmae_phys_len(sc, sc->gz_dma.paddr, addr, len);
4203         else
4204                 bxe_init_str_wr(sc, addr, sc->gz, len);
4205 #else
4206         bxe_init_str_wr(sc, addr, sc->gz, len);
4207 #endif
4208
4209         DBEXIT(BXE_INSANE_REGS);
4210 }
4211
4212 /*
4213  * Fill areas of device memory with the specified value.
4214  *
4215  * Generally used to clear a small area of device memory prior to writing
4216  * firmware to STORM memory or writing STORM firmware to device memory.
4217  *
4218  * Returns:
4219  *   None.
4220  */
4221 static void
4222 bxe_init_fill(struct bxe_softc *sc, uint32_t addr, int fill, uint32_t len)
4223 {
4224         uint32_t cur_len, i, leftovers, length;
4225
4226         DBENTER(BXE_VERBOSE_LOAD);
4227
4228         length = (((len * 4) > BXE_FW_BUF_SIZE) ? BXE_FW_BUF_SIZE : (len * 4));
4229         leftovers = length / 4;
4230         memset(sc->gz, fill, length);
4231
4232         for (i = 0; i < len; i += leftovers) {
4233                 cur_len = min(leftovers, len - i);
4234                 bxe_write_big_buf(sc, addr + i * 4, cur_len);
4235         }
4236
4237         DBEXIT(BXE_VERBOSE_LOAD);
4238 }
4239
4240 /*
4241  *
4242  * Returns:
4243  *   None.
4244  */
4245 static void
4246 bxe_init_wr_64(struct bxe_softc *sc, uint32_t addr, const uint32_t *data,
4247     uint32_t len64)
4248 {
4249         uint64_t data64, *pdata;
4250         uint32_t buf_len32, cur_len, len;
4251         int i;
4252
4253         DBENTER(BXE_INSANE_REGS);
4254
4255         buf_len32 = BXE_FW_BUF_SIZE / 4;
4256         len = len64 * 2;
4257         /* 64 bit value is in a blob: first low DWORD, then high DWORD. */
4258         data64 = HILO_U64((*(data + 1)), (*data));
4259         len64 = min((uint32_t)(BXE_FW_BUF_SIZE / 8), len64);
4260         for (i = 0; i < len64; i++) {
4261                 pdata = ((uint64_t *)(sc->gz)) + i;
4262                 *pdata = data64;
4263         }
4264
4265         for (i = 0; i < len; i += buf_len32) {
4266                 cur_len = min(buf_len32, len - i);
4267                 bxe_write_big_buf(sc, addr + i*4, cur_len);
4268         }
4269
4270         DBEXIT(BXE_INSANE_REGS);
4271 }
4272
4273
4274 /*
4275  * There are different blobs for each PRAM section. In addition, each
4276  * blob write operation is divided into multiple, smaller write
4277  * operations in order to decrease the amount of physically contiguous
4278  * buffer memory needed. Thus, when we select a blob, the address may
4279  * be with some offset from the beginning of PRAM section. The same
4280  * holds for the INT_TABLE sections.
4281  */
4282
4283 #define IF_IS_INT_TABLE_ADDR(base, addr) \
4284         if (((base) <= (addr)) && ((base) + 0x400 >= (addr)))
4285
4286 #define IF_IS_PRAM_ADDR(base, addr) \
4287         if (((base) <= (addr)) && ((base) + 0x40000 >= (addr)))
4288
4289 /*
4290  *
4291  * Returns:
4292  *   None.
4293  */
4294
4295 static const uint8_t *
4296 bxe_sel_blob(struct bxe_softc *sc, uint32_t addr, const uint8_t *data)
4297 {
4298
4299         IF_IS_INT_TABLE_ADDR(TSEM_REG_INT_TABLE, addr)
4300                 data = INIT_TSEM_INT_TABLE_DATA(sc);
4301         else
4302                 IF_IS_INT_TABLE_ADDR(CSEM_REG_INT_TABLE, addr)
4303                         data = INIT_CSEM_INT_TABLE_DATA(sc);
4304         else
4305                 IF_IS_INT_TABLE_ADDR(USEM_REG_INT_TABLE, addr)
4306                         data = INIT_USEM_INT_TABLE_DATA(sc);
4307         else
4308                 IF_IS_INT_TABLE_ADDR(XSEM_REG_INT_TABLE, addr)
4309                         data = INIT_XSEM_INT_TABLE_DATA(sc);
4310         else
4311                 IF_IS_PRAM_ADDR(TSEM_REG_PRAM, addr)
4312                         data = INIT_TSEM_PRAM_DATA(sc);
4313         else
4314                 IF_IS_PRAM_ADDR(CSEM_REG_PRAM, addr)
4315                         data = INIT_CSEM_PRAM_DATA(sc);
4316         else
4317                 IF_IS_PRAM_ADDR(USEM_REG_PRAM, addr)
4318                         data = INIT_USEM_PRAM_DATA(sc);
4319         else
4320                 IF_IS_PRAM_ADDR(XSEM_REG_PRAM, addr)
4321                         data = INIT_XSEM_PRAM_DATA(sc);
4322
4323         return (data);
4324
4325 }
4326
4327 static void
4328 bxe_write_big_buf_wb(struct bxe_softc *sc, uint32_t addr, uint32_t len)
4329 {
4330         if (sc->dmae_ready)
4331                 bxe_write_dmae_phys_len(sc, sc->gz_dma.paddr, addr, len);
4332         else
4333                 bxe_init_ind_wr(sc, addr, sc->gz, len);
4334 }
4335
4336
4337 #define VIRT_WR_DMAE_LEN(sc, data, addr, len32, le32_swap) \
4338         do { \
4339                 memcpy(sc->gz, data, (len32)*4); \
4340                 bxe_write_big_buf_wb(sc, addr, len32); \
4341         } while (0)
4342
4343
4344 /*
4345  *
4346  * Returns:
4347  *   None.
4348  */
4349 static void
4350 bxe_init_wr_wb(struct bxe_softc *sc, uint32_t addr, const uint32_t *data,
4351     uint32_t len)
4352 {
4353         const uint32_t *old_data;
4354
4355         DBENTER(BXE_INSANE_REGS);
4356         old_data = data;
4357         data = (const uint32_t *)bxe_sel_blob(sc, addr, (const uint8_t *)data);
4358         if (sc->dmae_ready) {
4359                 if (old_data != data)
4360                         VIRT_WR_DMAE_LEN(sc, data, addr, len, 1);
4361                 else
4362                         VIRT_WR_DMAE_LEN(sc, data, addr, len, 0);
4363         } else
4364                 bxe_init_ind_wr(sc, addr, data, len);
4365
4366         DBEXIT(BXE_INSANE_REGS);
4367 }
4368
4369 static void
4370 bxe_init_wr_zp(struct bxe_softc *sc, uint32_t addr, uint32_t len,
4371     uint32_t blob_off)
4372 {
4373         BXE_PRINTF("%s(%d): Compressed FW is not supported yet. "
4374             "ERROR: address:0x%x len:0x%x blob_offset:0x%x\n",
4375             __FILE__, __LINE__, addr, len, blob_off);
4376 }
4377
4378 /*
4379  * Initialize blocks of the device.
4380  *
4381  * This routine basically performs bulk register programming for different
4382  * blocks within the controller.  The file bxe_init_values.h contains a
4383  * series of register access operations (read, write, fill, etc.) as well
4384  * as a BLOB of data to initialize multiple blocks within the controller.
4385  * Block initialization may be supported by all controllers or by specific
4386  * models only.
4387  *
4388  * Returns:
4389  *   None.
4390  */
4391 static void
4392 bxe_init_block(struct bxe_softc *sc, uint32_t block, uint32_t stage)
4393 {
4394         union init_op *op;
4395         const uint32_t *data, *data_base;
4396         uint32_t i, op_type, addr, len;
4397         uint16_t op_end, op_start;
4398         int hw_wr;
4399
4400         DBENTER(BXE_VERBOSE_LOAD | BXE_VERBOSE_RESET);
4401
4402         op_start = INIT_OPS_OFFSETS(sc)[BLOCK_OPS_IDX(block, stage,
4403             STAGE_START)];
4404         op_end = INIT_OPS_OFFSETS(sc)[BLOCK_OPS_IDX(block, stage, STAGE_END)];
4405         /* If empty block */
4406         if (op_start == op_end)
4407                 return;
4408
4409         hw_wr = OP_WR_ASIC;
4410
4411         data_base = INIT_DATA(sc);
4412
4413         for (i = op_start; i < op_end; i++) {
4414
4415                 op = (union init_op *)&(INIT_OPS(sc)[i]);
4416
4417                 op_type = op->str_wr.op;
4418                 addr = op->str_wr.offset;
4419                 len = op->str_wr.data_len;
4420                 data = data_base + op->str_wr.data_off;
4421
4422                 /* HW/EMUL specific */
4423                 if ((op_type > OP_WB) && (op_type == hw_wr))
4424                         op_type = OP_WR;
4425
4426                 switch (op_type) {
4427                 case OP_RD:
4428                         REG_RD(sc, addr);
4429                         break;
4430                 case OP_WR:
4431                         REG_WR(sc, addr, op->write.val);
4432                         break;
4433                 case OP_SW:
4434                         bxe_init_str_wr(sc, addr, data, len);
4435                         break;
4436                 case OP_WB:
4437                         bxe_init_wr_wb(sc, addr, data, len);
4438                         break;
4439                 case OP_SI:
4440                         bxe_init_ind_wr(sc, addr, data, len);
4441                         break;
4442                 case OP_ZR:
4443                         bxe_init_fill(sc, addr, 0, op->zero.len);
4444                         break;
4445                 case OP_ZP:
4446                         bxe_init_wr_zp(sc, addr, len, op->str_wr.data_off);
4447                         break;
4448                 case OP_WR_64:
4449                         bxe_init_wr_64(sc, addr, data, len);
4450                         break;
4451                 default:
4452                         /* happens whenever an op is of a diff HW */
4453                         break;
4454                 }
4455         }
4456
4457         DBEXIT(BXE_VERBOSE_LOAD | BXE_VERBOSE_RESET);
4458 }
4459
4460 /*
4461  * Handles controller initialization when called from an unlocked routine.
4462  * ifconfig calls this function.
4463  *
4464  * Returns:
4465  *   None.
4466  */
4467 static void
4468 bxe_init(void *xsc)
4469 {
4470         struct bxe_softc *sc;
4471
4472         sc = xsc;
4473
4474         BXE_CORE_LOCK(sc);
4475         bxe_init_locked(sc, LOAD_NORMAL);
4476         BXE_CORE_UNLOCK(sc);
4477 }
4478
4479 /*
4480  * Release all resources used by the driver.
4481  *
4482  * Releases all resources acquired by the driver including interrupts,
4483  * interrupt handler, interfaces, mutexes, and DMA memory.
4484  *
4485  * Returns:
4486  *   None.
4487  */
4488 static void
4489 bxe_release_resources(struct bxe_softc *sc)
4490 {
4491         device_t dev;
4492
4493         DBENTER(BXE_VERBOSE_RESET | BXE_VERBOSE_UNLOAD);
4494
4495         dev = sc->dev;
4496
4497         /* Release the FreeBSD interface. */
4498         if (sc->bxe_ifp != NULL)
4499                 if_free(sc->bxe_ifp);
4500
4501         /* Free the DMA resources. */
4502         bxe_host_structures_free(sc);
4503
4504 #if __FreeBSD_version >= 800000
4505         /* Free multiqueue buffer rings. */
4506         bxe_free_buf_rings(sc);
4507 #endif
4508
4509 }
4510
4511
4512 /*
4513  * Indirect register write.
4514  *
4515  * Writes NetXtreme II registers using an index/data register pair in PCI
4516  * configuration space.  Using this mechanism avoids issues with posted
4517  * writes but is much slower than memory-mapped I/O.
4518  *
4519  * Returns:
4520  *   None.
4521  */
4522 static void
4523 bxe_reg_wr_ind(struct bxe_softc *sc, uint32_t offset, uint32_t val)
4524 {
4525         DBPRINT(sc, BXE_INSANE_REGS, "%s(); offset = 0x%08X, val = 0x%08X\n",
4526                 __FUNCTION__, offset, val);
4527
4528         pci_write_config(sc->dev, PCICFG_GRC_ADDRESS, offset, 4);
4529         pci_write_config(sc->dev, PCICFG_GRC_DATA, val, 4);
4530
4531         /* Return to a safe address. */
4532         pci_write_config(sc->dev, PCICFG_GRC_ADDRESS,
4533             PCICFG_VENDOR_ID_OFFSET, 4);
4534 }
4535
4536
4537 /*
4538  * Indirect register read.
4539  *
4540  * Reads NetXtreme II registers using an index/data register pair in PCI
4541  * configuration space.  Using this mechanism avoids issues with posted
4542  * reads but is much slower than memory-mapped I/O.
4543  *
4544  * Returns:
4545  *   The value of the register.
4546  */
4547 static uint32_t
4548 bxe_reg_rd_ind(struct bxe_softc *sc, uint32_t offset)
4549 {
4550         uint32_t val;
4551
4552         pci_write_config(sc->dev, PCICFG_GRC_ADDRESS, offset, 4);
4553         val = pci_read_config(sc->dev, PCICFG_GRC_DATA, 4);
4554
4555         /* Return to a safe address. */
4556         pci_write_config(sc->dev, PCICFG_GRC_ADDRESS,
4557             PCICFG_VENDOR_ID_OFFSET, 4);
4558
4559         DBPRINT(sc, BXE_INSANE_REGS, "%s(); offset = 0x%08X, val = 0x%08X\n",
4560             __FUNCTION__, offset, val);
4561         return (val);
4562 }
4563
4564
4565
4566 static uint32_t dmae_reg_go_c[] = {
4567         DMAE_REG_GO_C0, DMAE_REG_GO_C1, DMAE_REG_GO_C2, DMAE_REG_GO_C3,
4568         DMAE_REG_GO_C4, DMAE_REG_GO_C5, DMAE_REG_GO_C6, DMAE_REG_GO_C7,
4569         DMAE_REG_GO_C8, DMAE_REG_GO_C9, DMAE_REG_GO_C10, DMAE_REG_GO_C11,
4570         DMAE_REG_GO_C12, DMAE_REG_GO_C13, DMAE_REG_GO_C14, DMAE_REG_GO_C15
4571 };
4572
4573
4574 /*
4575  * Copy DMAE command into memory and start the command.
4576  *
4577  * Returns:
4578  *   None.
4579  */
4580 static void
4581 bxe_post_dmae(struct bxe_softc *sc, struct dmae_command *dmae, int idx)
4582 {
4583         uint32_t cmd_offset;
4584         int i;
4585         cmd_offset = (DMAE_REG_CMD_MEM + sizeof(struct dmae_command) * idx);
4586
4587         for (i = 0; i < (sizeof(struct dmae_command) / 4); i++) {
4588                 REG_WR(sc, cmd_offset + i * 4, *(((uint32_t *)dmae) + i));
4589                 DBPRINT(sc, BXE_INSANE_REGS, "%s(): DMAE cmd[%d].%d : 0x%08X\n",
4590                     __FUNCTION__, idx, i, cmd_offset + i * 4);
4591         }
4592
4593         /* Kick off the command. */
4594         REG_WR(sc, dmae_reg_go_c[idx], 1);
4595 }
4596
4597
4598 /*
4599  * Perform a DMAE write to device memory.
4600  *
4601  * Some of the registers on the 577XX controller are 128bits wide.  It is
4602  * required that when accessing those registers that they be written
4603  * atomically and that no intervening bus acceses to the device occur.
4604  * This could be handled by a lock held across all driver instances for
4605  * the device or it can be handled by performing a DMA operation when
4606  * writing to the device.  This code implements the latter.
4607  *
4608  * Returns:
4609  *   None.
4610  */
4611 void
4612 bxe_write_dmae(struct bxe_softc *sc, bus_addr_t dma_addr, uint32_t dst_addr,
4613     uint32_t len32)
4614 {
4615         struct dmae_command dmae;
4616         uint32_t *data, *wb_comp;
4617         int timeout;
4618
4619         DBENTER(BXE_INSANE_REGS);
4620
4621         DBPRINT(sc, BXE_EXTREME_REGS,
4622             "%s(): host addr = 0x%jX, device addr = 0x%08X, length = %d.\n",
4623             __FUNCTION__, (uintmax_t)dma_addr, dst_addr, (int)len32);
4624
4625         wb_comp = BXE_SP(sc, wb_comp);
4626         /* Fall back to indirect access if DMAE is not ready. */
4627         if (!sc->dmae_ready) {
4628                 data = BXE_SP(sc, wb_data[0]);
4629
4630                 DBPRINT(sc, BXE_WARN, "%s(): DMAE not ready, "
4631                     "using indirect.\n", __FUNCTION__);
4632
4633                 bxe_init_ind_wr(sc, dst_addr, data, len32);
4634                 goto bxe_write_dmae_exit;
4635         }
4636
4637         memset(&dmae, 0, sizeof(struct dmae_command));
4638
4639         dmae.opcode = (DMAE_CMD_SRC_PCI | DMAE_CMD_DST_GRC |
4640             DMAE_CMD_C_DST_PCI | DMAE_CMD_C_ENABLE |
4641             DMAE_CMD_SRC_RESET | DMAE_CMD_DST_RESET |
4642 #ifdef __BIG_ENDIAN
4643             DMAE_CMD_ENDIANITY_B_DW_SWAP |
4644 #else
4645             DMAE_CMD_ENDIANITY_DW_SWAP |
4646 #endif
4647             (BP_PORT(sc) ? DMAE_CMD_PORT_1 : DMAE_CMD_PORT_0) |
4648             (BP_E1HVN(sc) << DMAE_CMD_E1HVN_SHIFT));
4649         dmae.src_addr_lo = U64_LO(dma_addr);
4650         dmae.src_addr_hi = U64_HI(dma_addr);
4651         dmae.dst_addr_lo = dst_addr >> 2;
4652         dmae.dst_addr_hi = 0;
4653         dmae.len = len32;
4654         dmae.comp_addr_lo = U64_LO(BXE_SP_MAPPING(sc, wb_comp));
4655         dmae.comp_addr_hi = U64_HI(BXE_SP_MAPPING(sc, wb_comp));
4656         dmae.comp_val = BXE_WB_COMP_VAL;
4657
4658         BXE_DMAE_LOCK(sc);
4659
4660         *wb_comp = 0;
4661
4662         bxe_post_dmae(sc, &dmae, INIT_DMAE_C(sc));
4663
4664         DELAY(50);
4665
4666         /* Wait up to 200ms. */
4667         timeout = 4000;
4668         while (*wb_comp != BXE_WB_COMP_VAL) {
4669                 if (!timeout) {
4670                         DBPRINT(sc, BXE_FATAL,
4671                         "%s(): DMAE timeout (dst_addr = 0x%08X, len = %d)!\n",
4672                             __FUNCTION__, dst_addr, len32);
4673                         break;
4674                 }
4675                 timeout--;
4676                 DELAY(50);
4677         }
4678
4679         BXE_DMAE_UNLOCK(sc);
4680
4681 bxe_write_dmae_exit:
4682         DBEXIT(BXE_INSANE_REGS);
4683 }
4684
4685
4686 /*
4687  * Perform a DMAE read from to device memory.
4688  *
4689  * Some of the registers on the 577XX controller are 128bits wide.  It is
4690  * required that when accessing those registers that they be read
4691  * atomically and that no intervening bus acceses to the device occur.
4692  * This could be handled by a lock held across all driver instances for
4693  * the device or it can be handled by performing a DMA operation when
4694  * reading from the device.  This code implements the latter.
4695  *
4696  * Returns:
4697  *   None.
4698  */
4699 void
4700 bxe_read_dmae(struct bxe_softc *sc, uint32_t src_addr,
4701     uint32_t len32)
4702 {
4703         struct dmae_command dmae;
4704         uint32_t *data, *wb_comp;
4705         int i, timeout;
4706
4707         DBENTER(BXE_INSANE_REGS);
4708
4709         wb_comp = BXE_SP(sc, wb_comp);
4710         /* Fall back to indirect access if DMAE is not ready. */
4711         if (!sc->dmae_ready) {
4712                 data = BXE_SP(sc, wb_data[0]);
4713
4714                 DBPRINT(sc, BXE_WARN, "%s(): DMAE not ready, "
4715                     "using indirect.\n", __FUNCTION__);
4716
4717                 for (i = 0; i < len32; i++)
4718                         data[i] = bxe_reg_rd_ind(sc, src_addr + i * 4);
4719
4720                 goto bxe_read_dmae_exit;
4721         }
4722
4723         memset(&dmae, 0, sizeof(struct dmae_command));
4724
4725         dmae.opcode = (DMAE_CMD_SRC_GRC | DMAE_CMD_DST_PCI |
4726             DMAE_CMD_C_DST_PCI | DMAE_CMD_C_ENABLE |
4727             DMAE_CMD_SRC_RESET | DMAE_CMD_DST_RESET |
4728 #ifdef __BIG_ENDIAN
4729             DMAE_CMD_ENDIANITY_B_DW_SWAP |
4730 #else
4731             DMAE_CMD_ENDIANITY_DW_SWAP |
4732 #endif
4733             (BP_PORT(sc) ? DMAE_CMD_PORT_1 : DMAE_CMD_PORT_0) |
4734             (BP_E1HVN(sc) << DMAE_CMD_E1HVN_SHIFT));
4735
4736         dmae.src_addr_lo = src_addr >> 2;
4737         dmae.src_addr_hi = 0;
4738         dmae.dst_addr_lo = U64_LO(BXE_SP_MAPPING(sc, wb_data));
4739         dmae.dst_addr_hi = U64_HI(BXE_SP_MAPPING(sc, wb_data));
4740         dmae.len = len32;
4741         dmae.comp_addr_lo = U64_LO(BXE_SP_MAPPING(sc, wb_comp));
4742         dmae.comp_addr_hi = U64_HI(BXE_SP_MAPPING(sc, wb_comp));
4743         dmae.comp_val = BXE_WB_COMP_VAL;
4744
4745         BXE_DMAE_LOCK(sc);
4746
4747         memset(BXE_SP(sc, wb_data[0]), 0, sizeof(uint32_t) * 4);
4748         *wb_comp = 0;
4749
4750         bxe_post_dmae(sc, &dmae, INIT_DMAE_C(sc));
4751
4752         DELAY(50);
4753
4754         timeout = 4000;
4755         while (*wb_comp != BXE_WB_COMP_VAL) {
4756                 if (!timeout) {
4757                         DBPRINT(sc, BXE_FATAL,
4758                         "%s(): DMAE timeout (src_addr = 0x%08X, len = %d)!\n",
4759                             __FUNCTION__, src_addr, len32);
4760                         break;
4761                 }
4762                 timeout--;
4763                 DELAY(50);
4764         }
4765
4766         BXE_DMAE_UNLOCK(sc);
4767
4768 bxe_read_dmae_exit:
4769         DBEXIT(BXE_INSANE_REGS);
4770 }
4771
4772 /*
4773  * DMAE write wrapper.
4774  *
4775  * Returns:
4776  *   None.
4777  */
4778 static void
4779 bxe_wb_wr(struct bxe_softc *sc, int reg, uint32_t val_hi, uint32_t val_lo)
4780 {
4781         uint32_t wb_write[2];
4782
4783         wb_write[0] = val_hi;
4784         wb_write[1] = val_lo;
4785         REG_WR_DMAE(sc, reg, wb_write, 2);
4786 }
4787
4788
4789
4790 /*
4791  * Poll a register waiting for a value.
4792  *
4793  * Returns:
4794  *   The last read register value.
4795  */
4796 static __inline
4797 uint32_t bxe_reg_poll(struct bxe_softc *sc, uint32_t reg, uint32_t expected,
4798     int ms, int wait)
4799 {
4800         uint32_t val;
4801
4802         do {
4803                 val = REG_RD(sc, reg);
4804                 if (val == expected)
4805                         break;
4806                 ms -= wait;
4807                 DELAY(wait * 1000);
4808
4809         } while (ms > 0);
4810
4811         return (val);
4812 }
4813
4814
4815 /*
4816  * Microcode assert display.
4817  *
4818  * This function walks through each STORM processor and prints out a
4819  * listing of all asserts currently in effect.  Useful for post-mortem
4820  * debugging.
4821  *
4822  * Returns:
4823  *   The number of asserts detected.
4824  */
4825 static int
4826 bxe_mc_assert(struct bxe_softc *sc)
4827 {
4828         uint32_t row0, row1, row2, row3;
4829         char last_idx;
4830         int i, rc;
4831
4832         DBENTER(BXE_VERBOSE_INTR);
4833
4834         rc = 0;
4835         /* XSTORM */
4836         last_idx = REG_RD8(sc, BAR_XSTORM_INTMEM +
4837             XSTORM_ASSERT_LIST_INDEX_OFFSET);
4838
4839         if (last_idx)
4840                 DBPRINT(sc, BXE_FATAL, "DATA XSTORM_ASSERT_LIST_INDEX 0x%x\n",
4841                     last_idx);
4842
4843         /* Print the asserts */
4844         for (i = 0; i < STORM_ASSERT_ARRAY_SIZE; i++) {
4845
4846                 row0 = REG_RD(sc, BAR_XSTORM_INTMEM +
4847                     XSTORM_ASSERT_LIST_OFFSET(i));
4848                 row1 = REG_RD(sc, BAR_XSTORM_INTMEM +
4849                     XSTORM_ASSERT_LIST_OFFSET(i) + 4);
4850                 row2 = REG_RD(sc, BAR_XSTORM_INTMEM +
4851                     XSTORM_ASSERT_LIST_OFFSET(i) + 8);
4852                 row3 = REG_RD(sc, BAR_XSTORM_INTMEM +
4853                     XSTORM_ASSERT_LIST_OFFSET(i) + 12);
4854
4855                 if (row0 != COMMON_ASM_INVALID_ASSERT_OPCODE) {
4856                         DBPRINT(sc, BXE_FATAL, "DATA XSTORM_ASSERT_INDEX %d = "
4857                             "0x%08x 0x%08x 0x%08x 0x%08x\n", i, row3, row2,
4858                             row1, row0);
4859                         rc++;
4860                 } else
4861                         break;
4862         }
4863
4864         /* TSTORM */
4865         last_idx = REG_RD8(sc, BAR_TSTORM_INTMEM +
4866             TSTORM_ASSERT_LIST_INDEX_OFFSET);
4867
4868         if (last_idx)
4869                 DBPRINT(sc, BXE_FATAL, "DATA TSTORM_ASSERT_LIST_INDEX 0x%x\n",
4870                         last_idx);
4871
4872         /* Print the asserts */
4873         for (i = 0; i < STORM_ASSERT_ARRAY_SIZE; i++) {
4874
4875                 row0 = REG_RD(sc, BAR_TSTORM_INTMEM +
4876                     TSTORM_ASSERT_LIST_OFFSET(i));
4877                 row1 = REG_RD(sc, BAR_TSTORM_INTMEM +
4878                     TSTORM_ASSERT_LIST_OFFSET(i) + 4);
4879                 row2 = REG_RD(sc, BAR_TSTORM_INTMEM +
4880                     TSTORM_ASSERT_LIST_OFFSET(i) + 8);
4881                 row3 = REG_RD(sc, BAR_TSTORM_INTMEM +
4882                     TSTORM_ASSERT_LIST_OFFSET(i) + 12);
4883
4884                 if (row0 != COMMON_ASM_INVALID_ASSERT_OPCODE) {
4885                         DBPRINT(sc, BXE_FATAL, "DATA TSTORM_ASSERT_INDEX %d = "
4886                             "0x%08x 0x%08x 0x%08x 0x%08x\n", i, row3, row2,
4887                             row1, row0);
4888                         rc++;
4889                 } else
4890                         break;
4891         }
4892
4893         /* CSTORM */
4894         last_idx = REG_RD8(sc, BAR_CSTORM_INTMEM +
4895             CSTORM_ASSERT_LIST_INDEX_OFFSET);
4896
4897         if (last_idx)
4898                 DBPRINT(sc, BXE_FATAL, "DATA CSTORM_ASSERT_LIST_INDEX 0x%x\n",
4899                     last_idx);
4900
4901         /* Print the asserts */
4902         for (i = 0; i < STORM_ASSERT_ARRAY_SIZE; i++) {
4903
4904                 row0 = REG_RD(sc, BAR_CSTORM_INTMEM +
4905                     CSTORM_ASSERT_LIST_OFFSET(i));
4906                 row1 = REG_RD(sc, BAR_CSTORM_INTMEM +
4907                     CSTORM_ASSERT_LIST_OFFSET(i) + 4);
4908                 row2 = REG_RD(sc, BAR_CSTORM_INTMEM +
4909                     CSTORM_ASSERT_LIST_OFFSET(i) + 8);
4910                 row3 = REG_RD(sc, BAR_CSTORM_INTMEM +
4911                     CSTORM_ASSERT_LIST_OFFSET(i) + 12);
4912
4913                 if (row0 != COMMON_ASM_INVALID_ASSERT_OPCODE) {
4914                         DBPRINT(sc, BXE_FATAL, "DATA CSTORM_ASSERT_INDEX %d = "
4915                             "0x%08x 0x%08x 0x%08x 0x%08x\n", i, row3, row2,
4916                             row1, row0);
4917                         rc++;
4918                 } else
4919                         break;
4920         }
4921
4922         /* USTORM */
4923         last_idx = REG_RD8(sc, BAR_USTORM_INTMEM +
4924             USTORM_ASSERT_LIST_INDEX_OFFSET);
4925
4926         if (last_idx)
4927                 DBPRINT(sc, BXE_FATAL, "DATA USTORM_ASSERT_LIST_INDEX 0x%x\n",
4928                     last_idx);
4929
4930         /* Print the asserts */
4931         for (i = 0; i < STORM_ASSERT_ARRAY_SIZE; i++) {
4932
4933                 row0 = REG_RD(sc, BAR_USTORM_INTMEM +
4934                     USTORM_ASSERT_LIST_OFFSET(i));
4935                 row1 = REG_RD(sc, BAR_USTORM_INTMEM +
4936                     USTORM_ASSERT_LIST_OFFSET(i) + 4);
4937                 row2 = REG_RD(sc, BAR_USTORM_INTMEM +
4938                     USTORM_ASSERT_LIST_OFFSET(i) + 8);
4939                 row3 = REG_RD(sc, BAR_USTORM_INTMEM +
4940                     USTORM_ASSERT_LIST_OFFSET(i) + 12);
4941
4942                 if (row0 != COMMON_ASM_INVALID_ASSERT_OPCODE) {
4943                         DBPRINT(sc, BXE_FATAL, "DATA USTORM_ASSERT_INDEX %d = "
4944                             "0x%08x 0x%08x 0x%08x 0x%08x\n", i, row3, row2,
4945                             row1, row0);
4946                         rc++;
4947                 } else
4948                         break;
4949         }
4950
4951         DBEXIT(BXE_VERBOSE_INTR);
4952         return (rc);
4953 }
4954
4955
4956 /*
4957  * Perform a panic dump.
4958  *
4959  * Returns:
4960  *   None
4961  */
4962 static void
4963 bxe_panic_dump(struct bxe_softc *sc)
4964 {
4965         DBENTER(BXE_FATAL);
4966
4967         sc->stats_state = STATS_STATE_DISABLED;
4968
4969         BXE_PRINTF("---------- Begin crash dump ----------\n");
4970
4971         /* Idle check is run twice to verify the controller has stopped. */
4972         bxe_idle_chk(sc);
4973         bxe_idle_chk(sc);
4974         bxe_mc_assert(sc);
4975
4976 #ifdef BXE_DEBUG
4977         bxe_breakpoint(sc);
4978 #endif
4979
4980         BXE_PRINTF("----------  End crash dump  ----------\n");
4981
4982         DBEXIT(BXE_FATAL);
4983 }
4984
4985
4986 /*
4987  * Enables interrupt generation.
4988  *
4989  * Returns:
4990  *   None.
4991  */
4992 static void
4993 bxe_int_enable(struct bxe_softc *sc)
4994 {
4995         uint32_t hc_addr, val;
4996         int port;
4997
4998         DBENTER(BXE_VERBOSE_INTR);
4999
5000         port = BP_PORT(sc);
5001         hc_addr = port ? HC_REG_CONFIG_1 : HC_REG_CONFIG_0;
5002         val = REG_RD(sc, hc_addr);
5003         if (sc->msix_count > 0) {
5004                 if (sc->msix_count == 1) {
5005
5006                         /* Single interrupt, multiple queues.*/
5007                         DBPRINT(sc, BXE_VERBOSE_INTR,
5008                 "%s(): Setting host coalescing registers for MSI-X (SIMQ).\n",
5009                             __FUNCTION__);
5010
5011                         /* Clear INTx. */
5012                         val &= ~HC_CONFIG_0_REG_INT_LINE_EN_0;
5013
5014                         /* Enable single ISR mode, MSI/MSI-X, and attention messages. */
5015                         val |= (HC_CONFIG_0_REG_SINGLE_ISR_EN_0 |
5016                             HC_CONFIG_0_REG_MSI_MSIX_INT_EN_0 |
5017                             HC_CONFIG_0_REG_ATTN_BIT_EN_0);
5018                 } else {
5019
5020                         /* Multiple interrupts, multiple queues.*/
5021                         DBPRINT(sc, BXE_VERBOSE_INTR,
5022                 "%s(): Setting host coalescing registers for MSI-X (MIMQ).\n",
5023                             __FUNCTION__);
5024
5025                         /* Clear single ISR mode and INTx. */
5026                         val &= ~(HC_CONFIG_0_REG_SINGLE_ISR_EN_0 |
5027                             HC_CONFIG_0_REG_INT_LINE_EN_0);
5028
5029                         /* Enable MSI/MSI-X and attention messages. */
5030                         val |= (HC_CONFIG_0_REG_MSI_MSIX_INT_EN_0 |
5031                             HC_CONFIG_0_REG_ATTN_BIT_EN_0);
5032                 }
5033
5034         } else if (sc->msi_count > 0) {
5035
5036                 if (sc->msi_count == 1) {
5037
5038                         /* Single interrupt, multiple queues.*/
5039                         DBPRINT(sc, BXE_VERBOSE_INTR,
5040                 "%s(): Setting host coalescing registers for MSI (SIMQ).\n",
5041                             __FUNCTION__);
5042
5043                         /* Clear INTx. */
5044                         val &= ~HC_CONFIG_0_REG_INT_LINE_EN_0;
5045
5046                         /* Enable single ISR mode, MSI/MSI-X, and attention
5047                          * messages.
5048                          */
5049                         val |= (HC_CONFIG_0_REG_SINGLE_ISR_EN_0 |
5050                             HC_CONFIG_0_REG_MSI_MSIX_INT_EN_0 |
5051                             HC_CONFIG_0_REG_ATTN_BIT_EN_0);
5052                 } else {
5053                         /* Multiple interrupts, multiple queues.*/
5054                         DBPRINT(sc, BXE_VERBOSE_INTR,
5055                             "%s(): Setting host coalescing registers for"
5056                             "MSI (MIMQ).\n",
5057                             __FUNCTION__);
5058
5059                         /* Clear single ISR mode and INTx. */
5060                         val &= ~(HC_CONFIG_0_REG_SINGLE_ISR_EN_0 |
5061                             HC_CONFIG_0_REG_INT_LINE_EN_0);
5062
5063                         /* Enable MSI/MSI-X and attention messages. */
5064                         val |= (HC_CONFIG_0_REG_MSI_MSIX_INT_EN_0 |
5065                             HC_CONFIG_0_REG_ATTN_BIT_EN_0);
5066                 }
5067         } else {
5068                 /* Single interrupt, single queue. */
5069                 DBPRINT(sc, BXE_VERBOSE_INTR,
5070                     "%s(): Setting host coalescing registers for INTA#.\n",
5071                     __FUNCTION__);
5072
5073                 val |= (HC_CONFIG_0_REG_SINGLE_ISR_EN_0   |
5074                     HC_CONFIG_0_REG_MSI_MSIX_INT_EN_0 |
5075                     HC_CONFIG_0_REG_INT_LINE_EN_0     |
5076                     HC_CONFIG_0_REG_ATTN_BIT_EN_0);
5077                 REG_WR(sc, hc_addr, val);
5078
5079                 val &= ~HC_CONFIG_0_REG_MSI_MSIX_INT_EN_0;
5080         }
5081
5082         /* Write the interrupt mode to the host coalescing block. */
5083         REG_WR(sc, hc_addr, val);
5084
5085         if (CHIP_IS_E1H(sc)) {
5086
5087                 /* Init leading/trailing edge attention generation. */
5088                 if (IS_E1HMF(sc)) {
5089                         val = (0xee0f | (1 << (BP_E1HVN(sc) + 4)));
5090
5091                         /*
5092                          * Check if this driver instance is the port
5093                          * master function.
5094                          */
5095                         if (sc->port.pmf)
5096                                 /* Enable nig & GPIO3 attentions. */
5097                                 val |= 0x1100;
5098                 } else
5099                         val = 0xffff;
5100
5101                 REG_WR(sc, HC_REG_TRAILING_EDGE_0 + port * 8, val);
5102                 REG_WR(sc, HC_REG_LEADING_EDGE_0 + port * 8, val);
5103         }
5104
5105         DBEXIT(BXE_VERBOSE_INTR);
5106 }
5107
5108
5109 /*
5110  * Disables interrupt generation.
5111  *
5112  * Returns:
5113  *   None.
5114  */
5115 static void
5116 bxe_int_disable(struct bxe_softc *sc)
5117 {
5118         uint32_t hc_addr, val;
5119         int port;
5120
5121         DBENTER(BXE_VERBOSE_INTR | BXE_VERBOSE_RESET | BXE_VERBOSE_UNLOAD);
5122
5123         port = BP_PORT(sc);
5124         hc_addr = port ? HC_REG_CONFIG_1 : HC_REG_CONFIG_0;
5125         val = REG_RD(sc, hc_addr);
5126
5127         val &= ~(HC_CONFIG_0_REG_MSI_MSIX_INT_EN_0 |
5128             HC_CONFIG_0_REG_INT_LINE_EN_0 | HC_CONFIG_0_REG_ATTN_BIT_EN_0);
5129
5130         REG_WR(sc, hc_addr, val);
5131
5132         if (REG_RD(sc, hc_addr)!= val) {
5133                 DBPRINT(sc, BXE_WARN, "%s(): BUG! Returned value from IGU "
5134                     "doesn't match value written (0x%08X).\n",
5135                     __FUNCTION__, val);
5136         }
5137
5138         DBEXIT(BXE_VERBOSE_INTR | BXE_VERBOSE_RESET | BXE_VERBOSE_UNLOAD);
5139 }
5140
5141 #define BXE_CRC32_RESIDUAL      0xdebb20e3
5142
5143 /*
5144  * Returns:
5145  *   0 = Success, !0 = Failure.
5146  */
5147 static int
5148 bxe_nvram_acquire_lock(struct bxe_softc *sc)
5149 {
5150         uint32_t val;
5151         int i, port, rc;
5152
5153         DBENTER(BXE_VERBOSE_NVRAM);
5154
5155         port = BP_PORT(sc);
5156         rc = 0;
5157         val = 0;
5158
5159         /* Acquire the NVRAM lock. */
5160         REG_WR(sc, MCP_REG_MCPR_NVM_SW_ARB,
5161             (MCPR_NVM_SW_ARB_ARB_REQ_SET1 << port));
5162
5163         for (i = 0; i < NVRAM_TIMEOUT_COUNT * 10; i++) {
5164                 val = REG_RD(sc, MCP_REG_MCPR_NVM_SW_ARB);
5165                 if (val & (MCPR_NVM_SW_ARB_ARB_ARB1 << port))
5166                         break;
5167
5168                 DELAY(5);
5169         }
5170
5171         if (!(val & (MCPR_NVM_SW_ARB_ARB_ARB1 << port))) {
5172                 DBPRINT(sc, BXE_WARN, "%s(): Cannot acquire NVRAM lock!\n",
5173                     __FUNCTION__);
5174                 rc = EBUSY;
5175         }
5176
5177         DBEXIT(BXE_VERBOSE_NVRAM);
5178         return (rc);
5179 }
5180
5181 /*
5182  * Returns:
5183  *   0 = Success, !0 = Failure.
5184  */
5185 static int
5186 bxe_nvram_release_lock(struct bxe_softc *sc)
5187 {
5188         uint32_t val;
5189         int i, port, rc;
5190
5191         DBENTER(BXE_VERBOSE_NVRAM);
5192
5193         port = BP_PORT(sc);
5194         rc = 0;
5195         val = 0;
5196
5197         /* Release the NVRAM lock. */
5198         REG_WR(sc, MCP_REG_MCPR_NVM_SW_ARB,
5199             (MCPR_NVM_SW_ARB_ARB_REQ_CLR1 << port));
5200
5201         for (i = 0; i < NVRAM_TIMEOUT_COUNT * 10; i++) {
5202                 val = REG_RD(sc, MCP_REG_MCPR_NVM_SW_ARB);
5203                 if (!(val & (MCPR_NVM_SW_ARB_ARB_ARB1 << port)))
5204                         break;
5205
5206                 DELAY(5);
5207         }
5208
5209         if (val & (MCPR_NVM_SW_ARB_ARB_ARB1 << port)) {
5210                 DBPRINT(sc, BXE_WARN, "%s(): Cannot release NVRAM lock!\n",
5211                     __FUNCTION__);
5212                 rc = EBUSY;
5213         }
5214
5215         DBEXIT(BXE_VERBOSE_NVRAM);
5216         return (rc);
5217 }
5218
5219 /*
5220  * Returns:
5221  *   None.
5222  */
5223 static void
5224 bxe_nvram_enable_access(struct bxe_softc *sc)
5225 {
5226         uint32_t val;
5227
5228         DBENTER(BXE_VERBOSE_NVRAM);
5229
5230         val = REG_RD(sc, MCP_REG_MCPR_NVM_ACCESS_ENABLE);
5231
5232         /* Enable both bits, even on read */
5233         REG_WR(sc, MCP_REG_MCPR_NVM_ACCESS_ENABLE,
5234             (val | MCPR_NVM_ACCESS_ENABLE_EN |
5235              MCPR_NVM_ACCESS_ENABLE_WR_EN));
5236
5237         DBEXIT(BXE_VERBOSE_NVRAM);
5238 }
5239
5240 /*
5241  * Returns:
5242  *   None.
5243  */
5244 static void
5245 bxe_nvram_disable_access(struct bxe_softc *sc)
5246 {
5247         uint32_t val;
5248
5249         DBENTER(BXE_VERBOSE_NVRAM);
5250
5251         val = REG_RD(sc, MCP_REG_MCPR_NVM_ACCESS_ENABLE);
5252
5253         /* Disable both bits, even after read. */
5254         REG_WR(sc, MCP_REG_MCPR_NVM_ACCESS_ENABLE,
5255             (val & ~(MCPR_NVM_ACCESS_ENABLE_EN |
5256             MCPR_NVM_ACCESS_ENABLE_WR_EN)));
5257
5258         DBEXIT(BXE_VERBOSE_NVRAM);
5259 }
5260
5261 /*
5262  * Returns:
5263  *   0 = Success, !0 = Failure.
5264  */
5265 static int
5266 bxe_nvram_read_dword(struct bxe_softc *sc, uint32_t offset, uint32_t *ret_val,
5267     uint32_t cmd_flags)
5268 {
5269         uint32_t val;
5270         int i, rc;
5271
5272         DBENTER(BXE_INSANE_NVRAM);
5273
5274         /* Build the command word. */
5275         cmd_flags |= MCPR_NVM_COMMAND_DOIT;
5276
5277         /* Need to clear DONE bit separately. */
5278         REG_WR(sc, MCP_REG_MCPR_NVM_COMMAND, MCPR_NVM_COMMAND_DONE);
5279
5280         /* Address within the NVRAM to read. */
5281         REG_WR(sc, MCP_REG_MCPR_NVM_ADDR,
5282                 (offset & MCPR_NVM_ADDR_NVM_ADDR_VALUE));
5283
5284         /* Issue a read command. */
5285         REG_WR(sc, MCP_REG_MCPR_NVM_COMMAND, cmd_flags);
5286
5287         /* Wait for completion. */
5288         *ret_val = 0;
5289         rc = EBUSY;
5290         for (i = 0; i < NVRAM_TIMEOUT_COUNT; i++) {
5291                 DELAY(5);
5292                 val = REG_RD(sc, MCP_REG_MCPR_NVM_COMMAND);
5293
5294                 if (val & MCPR_NVM_COMMAND_DONE) {
5295                         val = REG_RD(sc, MCP_REG_MCPR_NVM_READ);
5296                         val = htobe32(val);
5297                         *ret_val = val;
5298                         rc = 0;
5299                         break;
5300                 }
5301         }
5302
5303         DBPRINT(sc, BXE_INSANE_NVRAM, "%s(): Read 0x%08X from offset 0x%08X.\n",
5304             __FUNCTION__, *ret_val, offset);
5305         DBEXIT(BXE_INSANE_NVRAM);
5306         return (rc);
5307 }
5308
5309 /*
5310  * Returns:
5311  *   0 = Success, !0 = Failure.
5312  */
5313 static int
5314 bxe_nvram_read(struct bxe_softc *sc, uint32_t offset, uint8_t *ret_buf,
5315     int buf_size)
5316 {
5317         uint32_t cmd_flags, val;
5318         int rc;
5319
5320         DBENTER(BXE_EXTREME_NVRAM);
5321
5322         if ((offset & 0x03) || (buf_size & 0x03) || (buf_size == 0)) {
5323                 DBPRINT(sc, BXE_WARN, "%s(): Unaligned address or invalid "
5324                     "buffer for NVRAM read (offset = 0x%08X, buf_size = %d)!\n",
5325                     __FUNCTION__, offset, buf_size);
5326                 rc = EINVAL;
5327                 goto bxe_nvram_read_exit;
5328         }
5329
5330         if (offset + buf_size > sc->common.flash_size) {
5331                 DBPRINT(sc, BXE_WARN, "%s(): Read extends beyond the end of "
5332                     "the NVRAM (offset (0x%08X) + buf_size (%d) > flash_size "
5333                     "(0x%08X))!\n", __FUNCTION__, offset, buf_size,
5334                     sc->common.flash_size);
5335                 rc = EINVAL;
5336                 goto bxe_nvram_read_exit;
5337         }
5338
5339         rc = bxe_nvram_acquire_lock(sc);
5340         if (rc)
5341                 goto bxe_nvram_read_exit;
5342
5343         bxe_nvram_enable_access(sc);
5344
5345         /* Read the first word(s). */
5346         cmd_flags = MCPR_NVM_COMMAND_FIRST;
5347         while ((buf_size > sizeof(uint32_t)) && (rc == 0)) {
5348                 rc = bxe_nvram_read_dword(sc, offset, &val, cmd_flags);
5349                 memcpy(ret_buf, &val, 4);
5350
5351                 /* Advance to the next DWORD. */
5352                 offset += sizeof(uint32_t);
5353                 ret_buf += sizeof(uint32_t);
5354                 buf_size -= sizeof(uint32_t);
5355                 cmd_flags = 0;
5356         }
5357
5358         /* Read the final word. */
5359         if (rc == 0) {
5360                 cmd_flags |= MCPR_NVM_COMMAND_LAST;
5361                 rc = bxe_nvram_read_dword(sc, offset, &val, cmd_flags);
5362                 memcpy(ret_buf, &val, 4);
5363         }
5364
5365         /* Disable access to NVRAM interface. */
5366         bxe_nvram_disable_access(sc);
5367         bxe_nvram_release_lock(sc);
5368
5369 bxe_nvram_read_exit:
5370         DBEXIT(BXE_EXTREME_NVRAM);
5371         return (rc);
5372 }
5373
5374 #ifdef BXE_NVRAM_WRITE_SUPPORT
5375 /*
5376  * Returns:
5377  *   0 = Success, !0 = Failure.
5378  */
5379 static int
5380 bxe_nvram_write_dword(struct bxe_softc *sc, uint32_t offset, uint32_t val,
5381 uint32_t cmd_flags)
5382 {
5383         int i, rc;
5384
5385         DBENTER(BXE_VERBOSE_NVRAM);
5386
5387         /* Build the command word. */
5388         cmd_flags |= MCPR_NVM_COMMAND_DOIT | MCPR_NVM_COMMAND_WR;
5389
5390         /* Need to clear DONE bit separately. */
5391         REG_WR(sc, MCP_REG_MCPR_NVM_COMMAND, MCPR_NVM_COMMAND_DONE);
5392
5393         /* Write the data. */
5394         REG_WR(sc, MCP_REG_MCPR_NVM_WRITE, val);
5395
5396         /* Address to write within the NVRAM. */
5397         REG_WR(sc, MCP_REG_MCPR_NVM_ADDR,
5398                 (offset & MCPR_NVM_ADDR_NVM_ADDR_VALUE));
5399
5400         /* Issue the write command. */
5401         REG_WR(sc, MCP_REG_MCPR_NVM_COMMAND, cmd_flags);
5402
5403         /* Wait for completion. */
5404         rc = EBUSY;
5405         for (i = 0; i < NVRAM_TIMEOUT_COUNT; i++) {
5406                 DELAY(5);
5407                 val = REG_RD(sc, MCP_REG_MCPR_NVM_COMMAND);
5408                 if (val & MCPR_NVM_COMMAND_DONE) {
5409                         rc = 0;
5410                         break;
5411                 }
5412         }
5413
5414         DBEXIT(BXE_VERBOSE_NVRAM);
5415         return (rc);
5416 }
5417
5418 #define BYTE_OFFSET(offset)             (8 * (offset & 0x03))
5419
5420 /*
5421  * Returns:
5422  *
5423  */
5424 static int
5425 bxe_nvram_write1(struct bxe_softc *sc, uint32_t offset, uint8_t *data_buf,
5426     int buf_size)
5427 {
5428         uint32_t align_offset, cmd_flags, val;
5429         int rc;
5430
5431         DBENTER(BXE_VERBOSE_NVRAM);
5432
5433         if (offset + buf_size > sc->common.flash_size) {
5434                 DBPRINT(sc, BXE_WARN, "%s(): Write extends beyond the end of "
5435                     "the NVRAM (offset (0x%08X) + buf_size (%d) > flash_size "
5436                     "(0x%08X))!\n", __FUNCTION__, offset, buf_size,
5437                     sc->common.flash_size);
5438                 rc = EINVAL;
5439                 goto bxe_nvram_write1_exit;
5440         }
5441
5442         /* request access to nvram interface */
5443         rc = bxe_nvram_acquire_lock(sc);
5444         if (rc)
5445                 goto bxe_nvram_write1_exit;
5446
5447         /* Enable access to the NVRAM interface. */
5448         bxe_nvram_enable_access(sc);
5449
5450         cmd_flags = (MCPR_NVM_COMMAND_FIRST | MCPR_NVM_COMMAND_LAST);
5451         align_offset = (offset & ~0x03);
5452         rc = bxe_nvram_read_dword(sc, align_offset, &val, cmd_flags);
5453
5454         if (rc == 0) {
5455                 val &= ~(0xff << BYTE_OFFSET(offset));
5456                 val |= (*data_buf << BYTE_OFFSET(offset));
5457
5458                 val = be32toh(val);
5459                 rc = bxe_nvram_write_dword(sc, align_offset, val, cmd_flags);
5460         }
5461
5462         /* Disable access to the NVRAM interface. */
5463         bxe_nvram_disable_access(sc);
5464         bxe_nvram_release_lock(sc);
5465
5466 bxe_nvram_write1_exit:
5467         DBEXIT(BXE_VERBOSE_NVRAM);
5468         return (rc);
5469 }
5470
5471 /*
5472  * Returns:
5473  *   0 = Success, !0 = Failure.
5474  */
5475 static int
5476 bxe_nvram_write(struct bxe_softc *sc, uint32_t offset, uint8_t *data_buf,
5477     int buf_size)
5478 {
5479         uint32_t cmd_flags, val, written_so_far;
5480         int rc;
5481
5482         rc = 0;
5483
5484         if (buf_size == 1)
5485                 return (bxe_nvram_write1(sc, offset, data_buf, buf_size));
5486
5487         if ((offset & 0x03) || (buf_size & 0x03) || (buf_size == 0)) {
5488                 DBPRINT(sc, BXE_WARN, "%s(): Unaligned address or invalid "
5489                     "buffer for NVRAM write "
5490                     "(offset = 0x%08X, buf_size = %d)!\n", __FUNCTION__,
5491                     offset, buf_size);
5492                 rc = EINVAL;
5493                 goto bxe_nvram_write_exit;
5494         }
5495
5496         if (offset + buf_size > sc->common.flash_size) {
5497                 DBPRINT(sc, BXE_WARN, "%s(): Write extends beyond the end of "
5498                     "the NVRAM (offset (0x%08X) + buf_size (%d) > flash_size "
5499                     "(0x%08X))!\n", __FUNCTION__, offset, buf_size,
5500                     sc->common.flash_size);
5501                 rc = EINVAL;
5502                 goto bxe_nvram_write_exit;
5503         }
5504
5505         /* Request access to NVRAM interface. */
5506         rc = bxe_nvram_acquire_lock(sc);
5507         if (rc)
5508                 goto bxe_nvram_write_exit;
5509
5510         /* Enable access to the NVRAM interface. */
5511         bxe_nvram_enable_access(sc);
5512
5513         written_so_far = 0;
5514         cmd_flags = MCPR_NVM_COMMAND_FIRST;
5515         while ((written_so_far < buf_size) && (rc == 0)) {
5516                 if (written_so_far == (buf_size - sizeof(uint32_t)))
5517                         cmd_flags |= MCPR_NVM_COMMAND_LAST;
5518                 else if (((offset + 4) % NVRAM_PAGE_SIZE) == 0)
5519                         cmd_flags |= MCPR_NVM_COMMAND_LAST;
5520                 else if ((offset % NVRAM_PAGE_SIZE) == 0)
5521                         cmd_flags |= MCPR_NVM_COMMAND_FIRST;
5522
5523                 memcpy(&val, data_buf, 4);
5524
5525                 rc = bxe_nvram_write_dword(sc, offset, val, cmd_flags);
5526
5527                 /* Advance to the next DWORD. */
5528                 offset += sizeof(uint32_t);
5529                 data_buf += sizeof(uint32_t);
5530                 written_so_far += sizeof(uint32_t);
5531                 cmd_flags = 0;
5532         }
5533
5534         /* Disable access to the NVRAM interface. */
5535         bxe_nvram_disable_access(sc);
5536         bxe_nvram_release_lock(sc);
5537
5538 bxe_nvram_write_exit:
5539         DBEXIT(BXE_VERBOSE_NVRAM);
5540         return (rc);
5541 }
5542 #endif
5543
5544 /*
5545  * This function validates NVRAM content by reading spcific
5546  * regions and validating that the NVRAM checksum matches the
5547  * actual content.
5548  *
5549  * Returns:
5550  *   0 = Success, !0 = Failure.
5551  */
5552 static int
5553 bxe_nvram_test(struct bxe_softc *sc)
5554 {
5555         static const struct {
5556                 int offset;
5557                 int size;
5558         } nvram_tbl[] = {
5559                 {     0,  0x14 }, /* bootstrap area*/
5560                 {  0x14,  0xec }, /* directory area */
5561                 { 0x100, 0x350 }, /* manuf_info */
5562                 { 0x450,  0xf0 }, /* feature_info */
5563                 { 0x640,  0x64 }, /* upgrade_key_info */
5564                 { 0x708,  0x70 }, /* manuf_key_info */
5565                 {     0,     0 }
5566         };
5567         uint32_t magic, csum, buf[0x350 / 4];
5568         uint8_t *data;
5569         int i, rc;
5570
5571         DBENTER(BXE_VERBOSE_NVRAM);
5572
5573         data = (uint8_t *) buf;
5574
5575         /* Read the DWORD at offset 0 in NVRAM. */
5576         rc = bxe_nvram_read(sc, 0, data, 4);
5577         if (rc) {
5578                 BXE_PRINTF("%s(%d): Error (%d) returned reading NVRAM!\n",
5579                     __FILE__, __LINE__, rc);
5580                 goto bxe_nvram_test_exit;
5581         }
5582
5583         /* Make sure we found our magic value. */
5584         magic = be32toh(buf[0]);
5585         if (magic != 0x669955aa) {
5586                 BXE_PRINTF("%s(%d): Invalid magic value (0x%08x) found!\n",
5587                     __FILE__, __LINE__, magic);
5588                 rc = ENODEV;
5589                 goto bxe_nvram_test_exit;
5590         }
5591
5592         /* Read through each region in NVRAM and validate the checksum. */
5593         for (i = 0; nvram_tbl[i].size; i++) {
5594                 DBPRINT(sc, BXE_VERBOSE_NVRAM, "%s(): Testing NVRAM region %d, "
5595                     "starting offset = %d, length = %d\n", __FUNCTION__, i,
5596                     nvram_tbl[i].offset, nvram_tbl[i].size);
5597
5598                 rc = bxe_nvram_read(sc, nvram_tbl[i].offset, data,
5599                         nvram_tbl[i].size);
5600                 if (rc) {
5601                         BXE_PRINTF("%s(%d): Error (%d) returned reading NVRAM "
5602                             "region %d!\n", __FILE__, __LINE__, rc, i);
5603                         goto bxe_nvram_test_exit;
5604                 }
5605
5606                 csum = ether_crc32_le(data, nvram_tbl[i].size);
5607                 if (csum != BXE_CRC32_RESIDUAL) {
5608                         BXE_PRINTF("%s(%d): Checksum error (0x%08X) for NVRAM "
5609                             "region %d!\n", __FILE__, __LINE__, csum, i);
5610                         rc = ENODEV;
5611                         goto bxe_nvram_test_exit;
5612                 }
5613         }
5614
5615 bxe_nvram_test_exit:
5616         DBEXIT(BXE_VERBOSE_NVRAM);
5617         return (rc);
5618 }
5619
5620 /*
5621  * Acknowledge status block and modify interrupt mode.
5622  *
5623  * Returns:
5624  *   None.
5625  */
5626 static __inline void
5627 bxe_ack_sb(struct bxe_softc *sc, uint8_t sb_id, uint8_t storm, uint16_t index,
5628     uint8_t int_mode, uint8_t update)
5629 {
5630         struct igu_ack_register igu_ack;
5631         uint32_t hc_addr;
5632
5633         hc_addr = (HC_REG_COMMAND_REG + BP_PORT(sc) * 32 + COMMAND_REG_INT_ACK);
5634         igu_ack.status_block_index = index;
5635         igu_ack.sb_id_and_flags =
5636             ((sb_id << IGU_ACK_REGISTER_STATUS_BLOCK_ID_SHIFT) |
5637             (storm << IGU_ACK_REGISTER_STORM_ID_SHIFT) |
5638             (update << IGU_ACK_REGISTER_UPDATE_INDEX_SHIFT) |
5639             (int_mode << IGU_ACK_REGISTER_INTERRUPT_MODE_SHIFT));
5640
5641         rmb();
5642         REG_WR(sc, hc_addr, (*(uint32_t *) &igu_ack));
5643         wmb();
5644 }
5645
5646 /*
5647  * Update fastpath status block index.
5648  *
5649  * Returns:
5650  *   0 = Nu completes, 1 = TX completes, 2 = RX completes,
5651  *   3 = RX & TX completes
5652  */
5653 static __inline uint16_t
5654 bxe_update_fpsb_idx(struct bxe_fastpath *fp)
5655 {
5656         struct host_status_block *fpsb;
5657         uint16_t rc;
5658
5659         fpsb = fp->status_block;
5660         rc = 0;
5661
5662         rmb();
5663
5664         /* Check for any CSTORM transmit completions. */
5665         if (fp->fp_c_idx != le16toh(fpsb->c_status_block.status_block_index)) {
5666                 fp->fp_c_idx = le16toh(fpsb->c_status_block.status_block_index);
5667                 rc |= 0x1;
5668         }
5669
5670         /* Check for any USTORM receive completions. */
5671         if (fp->fp_u_idx != le16toh(fpsb->u_status_block.status_block_index)) {
5672                 fp->fp_u_idx = le16toh(fpsb->u_status_block.status_block_index);
5673                 rc |= 0x2;
5674         }
5675
5676         return (rc);
5677 }
5678
5679 /*
5680  * Acknowledge interrupt.
5681  *
5682  * Returns:
5683  *   Interrupt value read from IGU.
5684  */
5685 static uint16_t
5686 bxe_ack_int(struct bxe_softc *sc)
5687 {
5688         uint32_t hc_addr, result;
5689
5690         hc_addr = HC_REG_COMMAND_REG + BP_PORT(sc) * 32 + COMMAND_REG_SIMD_MASK;
5691         result = REG_RD(sc, hc_addr);
5692         DBPRINT(sc, BXE_INSANE_INTR, "%s(): Read 0x%08X from HC addr 0x%08X\n",
5693             __FUNCTION__, result, hc_addr);
5694
5695         return (result);
5696 }
5697
5698 /*
5699  * Slowpath event handler.
5700  *
5701  * Checks that a ramrod completion occurs while the
5702  * controller is in the proper state.
5703  *
5704  * Returns:
5705  *   None.
5706  */
5707 static void
5708 bxe_sp_event(struct bxe_fastpath *fp, union eth_rx_cqe *rr_cqe)
5709 {
5710         struct bxe_softc *sc;
5711         int cid, command;
5712
5713         sc = fp->sc;
5714         DBENTER(BXE_VERBOSE_RAMROD);
5715
5716         cid = SW_CID(rr_cqe->ramrod_cqe.conn_and_cmd_data);
5717         command = CQE_CMD(rr_cqe->ramrod_cqe.conn_and_cmd_data);
5718         DBPRINT(sc, BXE_VERBOSE_RAMROD, "%s(): CID = %d, ramrod command = %d, "
5719             "device state = 0x%08X, fp[%02d].state = 0x%08X, type = %d\n",
5720             __FUNCTION__, cid, command, sc->state, fp->index, fp->state,
5721             rr_cqe->ramrod_cqe.ramrod_type);
5722
5723         /* Free up an entry on the slowpath queue. */
5724         sc->spq_left++;
5725
5726         /* Handle ramrod commands that completed on a client connection. */
5727         if (fp->index) {
5728                 /* Check for a completion for the current state. */
5729                 switch (command | fp->state) {
5730                 case (RAMROD_CMD_ID_ETH_CLIENT_SETUP | BXE_FP_STATE_OPENING):
5731                         DBPRINT(sc, BXE_VERBOSE_RAMROD,
5732                             "%s(): Completed fp[%02d] CLIENT_SETUP Ramrod.\n",
5733                             __FUNCTION__, cid);
5734                         fp->state = BXE_FP_STATE_OPEN;
5735                         break;
5736                 case (RAMROD_CMD_ID_ETH_HALT | BXE_FP_STATE_HALTING):
5737                         DBPRINT(sc, BXE_VERBOSE_RAMROD,
5738                             "%s(): Completed fp[%02d] ETH_HALT ramrod\n",
5739                             __FUNCTION__, cid);
5740                         fp->state = BXE_FP_STATE_HALTED;
5741                         break;
5742                 default:
5743                         DBPRINT(sc, BXE_VERBOSE_RAMROD,
5744                             "%s(): Unexpected microcode reply (%d) while "
5745                             "in state 0x%04X!\n", __FUNCTION__, command,
5746                             fp->state);
5747                 }
5748
5749                 goto bxe_sp_event_exit;
5750         }
5751
5752         /* Handle ramrod commands that completed on the leading connection. */
5753         switch (command | sc->state) {
5754         case (RAMROD_CMD_ID_ETH_PORT_SETUP | BXE_STATE_OPENING_WAIT4_PORT):
5755                 DBPRINT(sc, BXE_VERBOSE_RAMROD,
5756                     "%s(): Completed PORT_SETUP ramrod.\n", __FUNCTION__);
5757                 sc->state = BXE_STATE_OPEN;
5758                 break;
5759         case (RAMROD_CMD_ID_ETH_HALT | BXE_STATE_CLOSING_WAIT4_HALT):
5760                 DBPRINT(sc, BXE_VERBOSE_RAMROD,
5761                     "%s(): Completed ETH_HALT ramrod.\n", __FUNCTION__);
5762                 sc->state = BXE_STATE_CLOSING_WAIT4_DELETE;
5763                 fp->state = BXE_FP_STATE_HALTED;
5764                 break;
5765         case (RAMROD_CMD_ID_ETH_CFC_DEL | BXE_STATE_CLOSING_WAIT4_HALT):
5766                 DBPRINT(sc, BXE_VERBOSE_RAMROD,
5767                     "%s(): Completed fp[%02d] ETH_CFC_DEL ramrod.\n",
5768                     __FUNCTION__, cid);
5769                 sc->fp[cid].state = BXE_FP_STATE_CLOSED;
5770                 break;
5771         case (RAMROD_CMD_ID_ETH_SET_MAC | BXE_STATE_OPEN):
5772                 DBPRINT(sc, BXE_VERBOSE_RAMROD,
5773                     "%s(): Completed ETH_SET_MAC ramrod in STATE_OPEN state.\n",
5774                     __FUNCTION__);
5775                 break;
5776         case (RAMROD_CMD_ID_ETH_SET_MAC | BXE_STATE_CLOSING_WAIT4_HALT):
5777                 DBPRINT(sc, BXE_VERBOSE_RAMROD,
5778                     "%s(): Completed ETH_SET_MAC ramrod in "
5779                     "CLOSING_WAIT4_HALT state.\n", __FUNCTION__);
5780                 break;
5781         default:
5782                 DBPRINT(sc, BXE_FATAL, "%s(): Unexpected microcode reply (%d)! "
5783                     "State is 0x%08X\n", __FUNCTION__, command, sc->state);
5784         }
5785
5786 bxe_sp_event_exit:
5787         /* Force bxe_wait_ramrod() to see the change. */
5788         mb();
5789         DBEXIT(BXE_VERBOSE_RAMROD);
5790 }
5791
5792 /*
5793  * Lock access to a hardware resource using controller arbitration
5794  * register.
5795  *
5796  * Returns:
5797  *   0 = Success, !0 = Failure.
5798  */
5799 static int
5800 bxe_acquire_hw_lock(struct bxe_softc *sc, uint32_t resource)
5801 {
5802         uint32_t hw_lock_control_reg, lock_status, resource_bit;
5803         uint8_t func;
5804         int cnt, rc;
5805
5806         DBENTER(BXE_VERBOSE_MISC);
5807         DBPRINT(sc, BXE_VERBOSE_MISC, "%s(): Locking resource 0x%08X\n",
5808             __FUNCTION__, resource);
5809
5810         func = BP_FUNC(sc);
5811         resource_bit = 1 << resource;
5812         rc = 0;
5813
5814         hw_lock_control_reg = ((func <= 5) ?
5815             (MISC_REG_DRIVER_CONTROL_1 + func * 8) :
5816             (MISC_REG_DRIVER_CONTROL_7 + (func - 6) * 8));
5817
5818         /* Validating that the resource is within range. */
5819         if (resource > HW_LOCK_MAX_RESOURCE_VALUE) {
5820                 DBPRINT(sc, BXE_WARN, "%s(): Resource is out of range! "
5821                     "resource(0x%08X) > HW_LOCK_MAX_RESOURCE_VALUE(0x%08X)\n",
5822                     __FUNCTION__, resource, HW_LOCK_MAX_RESOURCE_VALUE);
5823                 rc = EINVAL;
5824                 goto bxe_acquire_hw_lock_exit;
5825         }
5826
5827         /* Validating that the resource is not already taken. */
5828         lock_status = REG_RD(sc, hw_lock_control_reg);
5829         if (lock_status & resource_bit) {
5830                 DBPRINT(sc, BXE_WARN, "%s(): Failed to acquire lock! "
5831                     "lock_status = 0x%08X, resource_bit = 0x%08X\n",
5832                     __FUNCTION__, lock_status, resource_bit);
5833                 rc = EEXIST;
5834                 goto bxe_acquire_hw_lock_exit;
5835         }
5836
5837         /* Try for 5 seconds every 5ms. */
5838         for (cnt = 0; cnt < 1000; cnt++) {
5839                 /* Try to acquire the lock. */
5840                 REG_WR(sc, hw_lock_control_reg + 4, resource_bit);
5841                 lock_status = REG_RD(sc, hw_lock_control_reg);
5842
5843                 if (lock_status & resource_bit)
5844                         goto bxe_acquire_hw_lock_exit;
5845                 DELAY(5000);
5846         }
5847
5848         DBPRINT(sc, BXE_WARN, "%s(): Timeout!\n", __FUNCTION__);
5849         rc = EAGAIN;
5850
5851 bxe_acquire_hw_lock_exit:
5852         DBEXIT(BXE_VERBOSE_MISC);
5853         return (rc);
5854 }
5855
5856 /*
5857  * Unlock access to a hardware resource using controller arbitration
5858  * register.
5859  *
5860  * Returns:
5861  *   0 = Success, !0 = Failure.
5862  */
5863 static int
5864 bxe_release_hw_lock(struct bxe_softc *sc, uint32_t resource)
5865 {
5866         uint32_t hw_lock_control_reg, lock_status, resource_bit;
5867         uint8_t func;
5868         int rc;
5869
5870         DBENTER(BXE_VERBOSE_MISC);
5871         DBPRINT(sc, BXE_VERBOSE_MISC, "%s(): Unlocking resource 0x%08X\n",
5872                 __FUNCTION__, resource);
5873
5874         resource_bit = 1 << resource;
5875         func = BP_FUNC(sc);
5876         rc = 0;
5877         /* Validating that the resource is within range */
5878         if (resource > HW_LOCK_MAX_RESOURCE_VALUE) {
5879                 DBPRINT(sc, BXE_WARN, "%s(): Resource is out of range! "
5880                     "resource(0x%08X) > HW_LOCK_MAX_RESOURCE_VALUE(0x%08X)\n",
5881                     __FUNCTION__, resource, HW_LOCK_MAX_RESOURCE_VALUE);
5882                 rc = EINVAL;
5883                 goto bxe_release_hw_lock_exit;
5884         }
5885
5886         /* Find the register for the resource lock. */
5887         hw_lock_control_reg = ((func <= 5) ?
5888             (MISC_REG_DRIVER_CONTROL_1 + func * 8) :
5889             (MISC_REG_DRIVER_CONTROL_7 + (func - 6) * 8));
5890
5891         /* Validating that the resource is currently taken */
5892         lock_status = REG_RD(sc, hw_lock_control_reg);
5893         if (!(lock_status & resource_bit)) {
5894                 DBPRINT(sc, BXE_WARN, "%s(): The resource is not currently "
5895                     "locked! lock_status = 0x%08X, resource_bit = 0x%08X\n",
5896                     __FUNCTION__, lock_status, resource_bit);
5897                 rc = EFAULT;
5898                 goto bxe_release_hw_lock_exit;
5899         }
5900
5901         /* Free the hardware lock. */
5902         REG_WR(sc, hw_lock_control_reg, resource_bit);
5903
5904 bxe_release_hw_lock_exit:
5905         DBEXIT(BXE_VERBOSE_MISC);
5906         return (rc);
5907 }
5908
5909 int
5910 bxe_get_gpio(struct bxe_softc *sc, int gpio_num, uint8_t port)
5911 {
5912         uint32_t gpio_mask, gpio_reg;
5913         int gpio_port, gpio_shift, value;
5914
5915         /* The GPIO should be swapped if swap register is set and active */
5916         gpio_port = (REG_RD(sc, NIG_REG_PORT_SWAP) && REG_RD(sc,
5917             NIG_REG_STRAP_OVERRIDE)) ^ port;
5918         gpio_shift = gpio_num +
5919             (gpio_port ? MISC_REGISTERS_GPIO_PORT_SHIFT : 0);
5920         gpio_mask = 1 << gpio_shift;
5921
5922         if (gpio_num > MISC_REGISTERS_GPIO_3) {
5923                 DBPRINT(sc, BXE_WARN, "%s(): Invalid GPIO %d\n",
5924                     __FUNCTION__, gpio_num);
5925                 return (-EINVAL);
5926         }
5927
5928         /* read GPIO value */
5929         gpio_reg = REG_RD(sc, MISC_REG_GPIO);
5930
5931         /* get the requested pin value */
5932         if ((gpio_reg & gpio_mask) == gpio_mask)
5933                 value = 1;
5934         else
5935                 value = 0;
5936
5937         DBPRINT(sc, BXE_VERBOSE_PHY, "pin %d  value 0x%x\n", gpio_num, value);
5938
5939         return (value);
5940 }
5941
5942 /*
5943  * Sets the state of a General Purpose I/O (GPIO).
5944  *
5945  * Returns:
5946  *   None.
5947  */
5948 int
5949 bxe_set_gpio(struct bxe_softc *sc, int gpio_num, uint32_t mode, uint8_t port)
5950 {
5951         uint32_t gpio_reg, gpio_mask;
5952         int gpio_port, gpio_shift, rc;
5953
5954         DBENTER(BXE_VERBOSE_MISC);
5955
5956         /* The GPIO should be swapped if swap register is set and active. */
5957         gpio_port = (REG_RD(sc, NIG_REG_PORT_SWAP) && REG_RD(sc,
5958             NIG_REG_STRAP_OVERRIDE)) ^ port;
5959         gpio_shift = gpio_num +
5960             (gpio_port ? MISC_REGISTERS_GPIO_PORT_SHIFT : 0);
5961         gpio_mask = (1 << gpio_shift);
5962         rc = 0;
5963
5964         if (gpio_num > MISC_REGISTERS_GPIO_3) {
5965                 DBPRINT(sc, BXE_FATAL, "%s(): Invalid GPIO (%d)!\n",
5966                     __FUNCTION__, gpio_num);
5967                 rc = EINVAL;
5968                 goto bxe_set_gpio_exit;
5969         }
5970
5971         /* Make sure no one else is trying to use the GPIO. */
5972         rc = bxe_acquire_hw_lock(sc, HW_LOCK_RESOURCE_GPIO);
5973         if (rc) {
5974                 DBPRINT(sc, BXE_WARN, "%s(): Can't acquire GPIO lock!\n",
5975                     __FUNCTION__);
5976                 goto bxe_set_gpio_exit;
5977         }
5978
5979         /* Read GPIO and mask all but the float bits. */
5980         gpio_reg = (REG_RD(sc, MISC_REG_GPIO) & MISC_REGISTERS_GPIO_FLOAT);
5981
5982         switch (mode) {
5983         case MISC_REGISTERS_GPIO_OUTPUT_LOW:
5984                 DBPRINT(sc, BXE_VERBOSE, "%s(): Set GPIO %d (shift %d) -> "
5985                     "output low\n", __FUNCTION__, gpio_num, gpio_shift);
5986                 gpio_reg &= ~(gpio_mask << MISC_REGISTERS_GPIO_FLOAT_POS);
5987                 gpio_reg |=  (gpio_mask << MISC_REGISTERS_GPIO_CLR_POS);
5988                 break;
5989         case MISC_REGISTERS_GPIO_OUTPUT_HIGH:
5990                 DBPRINT(sc, BXE_VERBOSE, "%s(): Set GPIO %d (shift %d) -> "
5991                     "output high\n", __FUNCTION__, gpio_num, gpio_shift);
5992                 gpio_reg &= ~(gpio_mask << MISC_REGISTERS_GPIO_FLOAT_POS);
5993                 gpio_reg |=  (gpio_mask << MISC_REGISTERS_GPIO_SET_POS);
5994                 break;
5995         case MISC_REGISTERS_GPIO_INPUT_HI_Z:
5996                 DBPRINT(sc, BXE_VERBOSE, "%s(): Set GPIO %d (shift %d) -> "
5997                     "input\n", __FUNCTION__, gpio_num, gpio_shift);
5998                 gpio_reg |= (gpio_mask << MISC_REGISTERS_GPIO_FLOAT_POS);
5999                 break;
6000         default:
6001                 DBPRINT(sc, BXE_FATAL, "%s(): Unknown GPIO mode (0x%08X)!\n",
6002                     __FUNCTION__, mode);
6003                 break;
6004         }
6005
6006         REG_WR(sc, MISC_REG_GPIO, gpio_reg);
6007         rc = bxe_release_hw_lock(sc, HW_LOCK_RESOURCE_GPIO);
6008         if (rc) {
6009                 DBPRINT(sc, BXE_WARN, "%s(): Can't release GPIO lock!\n",
6010                     __FUNCTION__);
6011         }
6012
6013 bxe_set_gpio_exit:
6014         DBEXIT(BXE_VERBOSE_MISC);
6015         return (rc);
6016 }
6017
6018 int
6019 bxe_set_gpio_int(struct bxe_softc *sc, int gpio_num, uint32_t mode,
6020     uint8_t port)
6021 {
6022         uint32_t gpio_mask, gpio_reg;
6023         int gpio_port, gpio_shift;
6024
6025         /* The GPIO should be swapped if swap register is set and active */
6026         gpio_port = (REG_RD(sc, NIG_REG_PORT_SWAP) && REG_RD(sc,
6027             NIG_REG_STRAP_OVERRIDE)) ^ port;
6028         gpio_shift = gpio_num +
6029             (gpio_port ? MISC_REGISTERS_GPIO_PORT_SHIFT : 0);
6030         gpio_mask = (1 << gpio_shift);
6031         if (gpio_num > MISC_REGISTERS_GPIO_3) {
6032                 DBPRINT(sc, BXE_WARN, "%s(): Invalid GPIO %d\n",
6033                     __FUNCTION__, gpio_num);
6034                 return (-EINVAL);
6035         }
6036
6037         bxe_acquire_hw_lock(sc, HW_LOCK_RESOURCE_GPIO);
6038         /* read GPIO int */
6039         gpio_reg = REG_RD(sc, MISC_REG_GPIO_INT);
6040
6041         switch (mode) {
6042         case MISC_REGISTERS_GPIO_INT_OUTPUT_CLR:
6043                 DBPRINT(sc, BXE_VERBOSE_PHY, "Clear GPIO INT %d (shift %d) -> "
6044                     "output low\n", gpio_num, gpio_shift);
6045                 /* clear SET and set CLR */
6046                 gpio_reg &= ~(gpio_mask << MISC_REGISTERS_GPIO_INT_SET_POS);
6047                 gpio_reg |=  (gpio_mask << MISC_REGISTERS_GPIO_INT_CLR_POS);
6048                 break;
6049         case MISC_REGISTERS_GPIO_INT_OUTPUT_SET:
6050                 DBPRINT(sc, BXE_VERBOSE_PHY, "Set GPIO INT %d (shift %d) -> "
6051                     "output high\n", gpio_num, gpio_shift);
6052                 /* clear CLR and set SET */
6053                 gpio_reg &= ~(gpio_mask << MISC_REGISTERS_GPIO_INT_CLR_POS);
6054                 gpio_reg |=  (gpio_mask << MISC_REGISTERS_GPIO_INT_SET_POS);
6055                 break;
6056         default:
6057                 break;
6058         }
6059
6060         REG_WR(sc, MISC_REG_GPIO_INT, gpio_reg);
6061         bxe_release_hw_lock(sc, HW_LOCK_RESOURCE_GPIO);
6062
6063         return (0);
6064 }
6065
6066 /*
6067  * Sets the state of a Shared Purpose I/O (SPIO).
6068  *
6069  * Returns:
6070  *   0 = Success, !0 = Failure.
6071  */
6072 int
6073 bxe_set_spio(struct bxe_softc *sc, int spio_num, uint32_t mode)
6074 {
6075         uint32_t spio_reg, spio_mask;
6076         int rc;
6077
6078         rc = 0;
6079         spio_mask = 1 << spio_num;
6080
6081         /* Validate the SPIO. */
6082         if ((spio_num < MISC_REGISTERS_SPIO_4) ||
6083             (spio_num > MISC_REGISTERS_SPIO_7)) {
6084                 DBPRINT(sc, BXE_WARN, "%s(): Invalid SPIO (%d)!\n",
6085                     __FUNCTION__, spio_num);
6086                 rc = EINVAL;
6087                 goto bxe_set_spio_exit;
6088         }
6089
6090         rc = bxe_acquire_hw_lock(sc, HW_LOCK_RESOURCE_SPIO);
6091         if (rc) {
6092                 DBPRINT(sc, BXE_WARN, "%s(): Can't acquire SPIO lock!\n",
6093                     __FUNCTION__);
6094                 goto bxe_set_spio_exit;
6095         }
6096
6097         /* Read SPIO and mask all but the float bits. */
6098         spio_reg = (REG_RD(sc, MISC_REG_SPIO) & MISC_REGISTERS_SPIO_FLOAT);
6099
6100         switch (mode) {
6101         case MISC_REGISTERS_SPIO_OUTPUT_LOW :
6102                 DBPRINT(sc, BXE_VERBOSE_MISC, "%s(): Set SPIO %d -> "
6103                     "output low\n", __FUNCTION__, spio_num);
6104                 spio_reg &= ~(spio_mask << MISC_REGISTERS_SPIO_FLOAT_POS);
6105                 spio_reg |=  (spio_mask << MISC_REGISTERS_SPIO_CLR_POS);
6106                 break;
6107         case MISC_REGISTERS_SPIO_OUTPUT_HIGH :
6108                 DBPRINT(sc, BXE_VERBOSE_MISC,  "%s(): Set SPIO %d -> "
6109                     "output high\n", __FUNCTION__, spio_num);
6110                 spio_reg &= ~(spio_mask << MISC_REGISTERS_SPIO_FLOAT_POS);
6111                 spio_reg |=  (spio_mask << MISC_REGISTERS_SPIO_SET_POS);
6112                 break;
6113         case MISC_REGISTERS_SPIO_INPUT_HI_Z:
6114                 DBPRINT(sc, BXE_VERBOSE_MISC, "%s(): Set SPIO %d -> "
6115                     "input\n", __FUNCTION__, spio_num);
6116                 spio_reg |= (spio_mask << MISC_REGISTERS_SPIO_FLOAT_POS);
6117                 break;
6118         default:
6119                 DBPRINT(sc, BXE_WARN, "%s(): Unknown SPIO mode (0x%08X)!\n",
6120                     __FUNCTION__, mode);
6121                 break;
6122         }
6123
6124         REG_WR(sc, MISC_REG_SPIO, spio_reg);
6125         rc = bxe_release_hw_lock(sc, HW_LOCK_RESOURCE_SPIO);
6126         if (rc) {
6127                 DBPRINT(sc, BXE_WARN, "%s(): Can't release SPIO lock!\n",
6128                     __FUNCTION__);
6129         }
6130
6131 bxe_set_spio_exit:
6132         return (rc);
6133 }
6134
6135 /*
6136  * When the 57711E is operating in multi-function mode, the controller
6137  * must be configured to arbitrate TX between multiple VNICs.
6138  *
6139  * Returns:
6140  *   None.
6141  */
6142 static void
6143 bxe_init_port_minmax(struct bxe_softc *sc)
6144 {
6145         uint32_t fair_periodic_timeout_usec, r_param, t_fair;
6146
6147         DBENTER(BXE_VERBOSE_MISC);
6148
6149         r_param = sc->link_vars.line_speed / 8;
6150
6151         memset(&(sc->cmng.rs_vars), 0,
6152             sizeof(struct rate_shaping_vars_per_port));
6153         memset(&(sc->cmng.fair_vars), 0, sizeof(struct fairness_vars_per_port));
6154
6155         /* 100 usec in SDM ticks = 25 since each tick is 4 usec. */
6156         sc->cmng.rs_vars.rs_periodic_timeout = RS_PERIODIC_TIMEOUT_USEC / 4;
6157         /*
6158          * This is the threshold below which no timer arming will occur.
6159          * We use a coefficient of 1, 25 so that the threshold is a
6160          * little bigger that real time to compensate for timer
6161          * in-accuracy.
6162          */
6163         sc->cmng.rs_vars.rs_threshold = (RS_PERIODIC_TIMEOUT_USEC *
6164             r_param * 5) / 4;
6165         /* Resolution of fairness timer. */
6166         fair_periodic_timeout_usec = QM_ARB_BYTES / r_param;
6167
6168         /* For 10G it is 1000us, for 1G it is 10000us. */
6169         t_fair = T_FAIR_COEF / sc->link_vars.line_speed;
6170         /* This is the threshold where we won't arm the timer
6171            anymore. */
6172         sc->cmng.fair_vars.fair_threshold = QM_ARB_BYTES;
6173         /*
6174          * Multiply by 1e3/8 to get bytes/msec. We don't want the
6175          * credits to pass a credit of the T_FAIR*FAIR_MEM (algorithm
6176          * resolution)
6177          */
6178         sc->cmng.fair_vars.upper_bound = r_param * t_fair * FAIR_MEM;
6179         /* Since each tick is 4 us. */
6180         sc->cmng.fair_vars.fairness_timeout = fair_periodic_timeout_usec / 4;
6181
6182         DBEXIT(BXE_VERBOSE_MISC);
6183 }
6184
6185
6186 /*
6187  * This function is called when a link interrupt is generated
6188  * and configures the controller for the new link state.
6189  *
6190  * Returns:
6191  *   None.
6192  */
6193 static void
6194 bxe_link_attn(struct bxe_softc *sc)
6195 {
6196         struct host_port_stats *pstats;
6197         uint32_t pause_enabled;
6198         int func, i, port, vn;
6199
6200         DBENTER(BXE_VERBOSE_PHY);
6201
6202         /* Make sure that we are synced with the current statistics. */
6203         bxe_stats_handle(sc, STATS_EVENT_STOP);
6204
6205         bxe_link_update(&sc->link_params, &sc->link_vars);
6206
6207         if (sc->link_vars.link_up) {
6208                 if (CHIP_IS_E1H(sc)) {
6209                         port = BP_PORT(sc);
6210                         pause_enabled = 0;
6211
6212                         if (sc->link_vars.flow_ctrl & FLOW_CTRL_TX)
6213                                 pause_enabled = 1;
6214
6215                         REG_WR(sc, BAR_USTORM_INTMEM +
6216                             USTORM_ETH_PAUSE_ENABLED_OFFSET(port),
6217                             pause_enabled);
6218                 }
6219
6220                 if (sc->link_vars.mac_type == MAC_TYPE_BMAC) {
6221                         pstats = BXE_SP(sc, port_stats);
6222                         /* Reset old BMAC statistics. */
6223                         memset(&(pstats->mac_stx[0]), 0,
6224                             sizeof(struct mac_stx));
6225                 }
6226
6227                 if ((sc->state == BXE_STATE_OPEN) ||
6228                     (sc->state == BXE_STATE_DISABLED))
6229                         bxe_stats_handle(sc, STATS_EVENT_LINK_UP);
6230         }
6231
6232         /* Need additional handling for multi-function devices. */
6233         if (IS_E1HMF(sc)) {
6234                 port = BP_PORT(sc);
6235                 if (sc->link_vars.link_up) {
6236                         if (sc->dcc_enable == TRUE) {
6237                                 bxe_congestionmgmt(sc, TRUE);
6238                                 /* Store in internal memory. */
6239                                 for (i = 0; i <
6240                                     sizeof(struct cmng_struct_per_port) / 4;
6241                                     i++) {
6242                                         REG_WR(sc, BAR_XSTORM_INTMEM +
6243                                 XSTORM_CMNG_PER_PORT_VARS_OFFSET(port) + (i*4),
6244                                             ((uint32_t *)(&sc->cmng))[i]);
6245                                 }
6246                         }
6247                 }
6248                 for (vn = VN_0; vn < E1HVN_MAX; vn++) {
6249                         /* Don't send an attention to ourselves. */
6250                         if (vn == BP_E1HVN(sc))
6251                                 continue;
6252                         func = ((vn << 1) | port);
6253                         /*
6254                          * Send an attention to other drivers on the same port.
6255                          */
6256                         REG_WR(sc, MISC_REG_AEU_GENERAL_ATTN_0 +
6257                             (LINK_SYNC_ATTENTION_BIT_FUNC_0 + func) * 4, 1);
6258                 }
6259         }
6260
6261         DBEXIT(BXE_VERBOSE_PHY);
6262 }
6263
6264 /*
6265  * Sets the driver instance as the port management function (PMF).
6266  *
6267  * This is only used on "multi-function" capable devices such as the
6268  * 57711E and initializes the controller so that the PMF driver instance
6269  * can interact with other driver instances that may be operating on
6270  * the same Ethernet port.
6271  *
6272  * Returns:
6273  *   None.
6274  */
6275 static void
6276 bxe_pmf_update(struct bxe_softc *sc)
6277 {
6278         uint32_t val;
6279         int port;
6280
6281         /* Record that this driver instance is managing the port. */
6282         sc->port.pmf = 1;
6283         DBPRINT(sc, BXE_INFO, "%s(): Enabling this port as PMF.\n",
6284             __FUNCTION__);
6285
6286         /* Enable NIG attention. */
6287         port = BP_PORT(sc);
6288         val = (0xff0f | (1 << (BP_E1HVN(sc) + 4)));
6289         REG_WR(sc, HC_REG_TRAILING_EDGE_0 + port * 8, val);
6290         REG_WR(sc, HC_REG_LEADING_EDGE_0 + port * 8, val);
6291
6292         bxe_stats_handle(sc, STATS_EVENT_PMF);
6293 }
6294
6295 /* 8073 Download definitions */
6296 /* spi Parameters.*/
6297 #define SPI_CTRL_1_L    0xC000
6298 #define SPI_CTRL_1_H    0xC002
6299 #define SPI_CTRL_2_L    0xC400
6300 #define SPI_CTRL_2_H    0xC402
6301 #define SPI_TXFIFO      0xD000
6302 #define SPI_RXFIFO      0xD400
6303
6304 /* Input Command Messages.*/
6305 /*
6306  * Write CPU/SPI Control Regs, followed by Count And CPU/SPI Controller
6307  * Reg add/data pairs.
6308  */
6309 #define WR_CPU_CTRL_REGS        0x11
6310 /*
6311  * Read CPU/SPI Control Regs, followed by Count and CPU/SPI Controller
6312  * Register Add.
6313  */
6314 #define RD_CPU_CTRL_REGS        0xEE
6315 /*
6316  * Write CPU/SPI Control Regs Continously, followed by Count and
6317  * CPU/SPI Controller Reg addr and data's.
6318  */
6319 #define WR_CPU_CTRL_FIFO        0x66
6320 /* Output Command Messages.*/
6321 #define DONE                    0x4321
6322
6323 /* SPI Controller Commands (known As messages).*/
6324 #define MSGTYPE_HWR     0x40
6325 #define MSGTYPE_HRD     0x80
6326 #define WRSR_OPCODE     0x01
6327 #define WR_OPCODE       0x02
6328 #define RD_OPCODE       0x03
6329 #define WRDI_OPCODE     0x04
6330 #define RDSR_OPCODE     0x05
6331 #define WREN_OPCODE     0x06
6332 #define WR_BLOCK_SIZE   0x40    /* Maximum 64 Bytes Writes.*/
6333
6334 /*
6335  * Post a slowpath command.
6336  *
6337  * A slowpath command is used to propogate a configuration change through
6338  * the controller in a controlled manner, allowing each STORM processor and
6339  * other H/W blocks to phase in the change.  The commands sent on the
6340  * slowpath are referred to as ramrods.  Depending on the ramrod used the
6341  * completion of the ramrod will occur in different ways.  Here's a
6342  * breakdown of ramrods and how they complete:
6343  *
6344  * RAMROD_CMD_ID_ETH_PORT_SETUP
6345  *   Used to setup the leading connection on a port.  Completes on the
6346  *   Receive Completion Queue (RCQ) of that port (typically fp[0]).
6347  *
6348  * RAMROD_CMD_ID_ETH_CLIENT_SETUP
6349  *   Used to setup an additional connection on a port.  Completes on the
6350  *   RCQ of the multi-queue/RSS connection being initialized.
6351  *
6352  * RAMROD_CMD_ID_ETH_STAT_QUERY
6353  *   Used to force the storm processors to update the statistics database
6354  *   in host memory.  This ramrod is send on the leading connection CID and
6355  *   completes as an index increment of the CSTORM on the default status
6356  *   block.
6357  *
6358  * RAMROD_CMD_ID_ETH_UPDATE
6359  *   Used to update the state of the leading connection, usually to udpate
6360  *   the RSS indirection table.  Completes on the RCQ of the leading
6361  *   connection. (Not currently used under FreeBSD until OS support becomes
6362  *   available.)
6363  *
6364  * RAMROD_CMD_ID_ETH_HALT
6365  *   Used when tearing down a connection prior to driver unload.  Completes
6366  *   on the RCQ of the multi-queue/RSS connection being torn down.  Don't
6367  *   use this on the leading connection.
6368  *
6369  * RAMROD_CMD_ID_ETH_SET_MAC
6370  *   Sets the Unicast/Broadcast/Multicast used by the port.  Completes on
6371  *   the RCQ of the leading connection.
6372  *
6373  * RAMROD_CMD_ID_ETH_CFC_DEL
6374  *   Used when tearing down a conneciton prior to driver unload.  Completes
6375  *   on the RCQ of the leading connection (since the current connection
6376  *   has been completely removed from controller memory).
6377  *
6378  * RAMROD_CMD_ID_ETH_PORT_DEL
6379  *   Used to tear down the leading connection prior to driver unload,
6380  *   typically fp[0].  Completes as an index increment of the CSTORM on the
6381  *   default status block.
6382  *
6383  * RAMROD_CMD_ID_ETH_FORWARD_SETUP
6384  *   Used for connection offload.  Completes on the RCQ of the multi-queue
6385  *   RSS connection that is being offloaded.  (Not currently used under
6386  *   FreeBSD.)
6387  *
6388  * There can only be one command pending per function.
6389  *
6390  * Returns:
6391  *   0 = Success, !0 = Failure.
6392  */
6393 static int
6394 bxe_sp_post(struct bxe_softc *sc, int command, int cid, uint32_t data_hi,
6395     uint32_t data_lo, int common)
6396 {
6397         int func, rc;
6398
6399         DBRUNMSG((BXE_EXTREME_LOAD | BXE_EXTREME_RESET |
6400             BXE_EXTREME_UNLOAD | BXE_EXTREME_RAMROD),
6401             bxe_decode_ramrod_cmd(sc, command));
6402
6403         DBPRINT(sc, BXE_VERBOSE_RAMROD, "%s(): cid = %d, data_hi = 0x%08X, "
6404             "data_low = 0x%08X, remaining spq entries = %d\n", __FUNCTION__,
6405             cid, data_hi, data_lo, sc->spq_left);
6406
6407         rc = 0;
6408         /* Skip all slowpath commands if the driver has panic'd. */
6409         if (sc->panic) {
6410                 rc = EIO;
6411                 goto bxe_sp_post_exit;
6412         }
6413
6414         BXE_SP_LOCK(sc);
6415
6416         /* We are limited to 8 slowpath commands. */
6417         if (!sc->spq_left) {
6418                 BXE_PRINTF("%s(%d): Slowpath queue is full!\n",
6419                     __FILE__, __LINE__);
6420                 bxe_panic_dump(sc);
6421                 rc = EBUSY;
6422                 goto bxe_sp_post_exit;
6423         }
6424
6425         /* Encode the CID with the command. */
6426         sc->spq_prod_bd->hdr.conn_and_cmd_data =
6427             htole32(((command << SPE_HDR_CMD_ID_SHIFT) | HW_CID(sc, cid)));
6428         sc->spq_prod_bd->hdr.type = htole16(ETH_CONNECTION_TYPE);
6429
6430         if (common)
6431                 sc->spq_prod_bd->hdr.type |=
6432                     htole16((1 << SPE_HDR_COMMON_RAMROD_SHIFT));
6433
6434         /* Point the hardware at the new configuration data. */
6435         sc->spq_prod_bd->data.mac_config_addr.hi = htole32(data_hi);
6436         sc->spq_prod_bd->data.mac_config_addr.lo = htole32(data_lo);
6437
6438         /* Reduce the number of available slots for slowpath commands. */
6439         sc->spq_left--;
6440
6441         /* Manage the end of the ring. */
6442         if (sc->spq_prod_bd == sc->spq_last_bd) {
6443                 sc->spq_prod_bd = sc->spq;
6444                 sc->spq_prod_idx = 0;
6445                 DBPRINT(sc, BXE_VERBOSE, "%s(): End of slowpath queue.\n",
6446                     __FUNCTION__);
6447         } else {
6448                 sc->spq_prod_bd++;
6449                 sc->spq_prod_idx++;
6450         }
6451
6452         func = BP_FUNC(sc);
6453         /* Kick off the slowpath command. */
6454         REG_WR(sc, BAR_XSTORM_INTMEM + XSTORM_SPQ_PROD_OFFSET(func),
6455             sc->spq_prod_idx);
6456
6457 bxe_sp_post_exit:
6458         BXE_SP_UNLOCK(sc);
6459
6460         return (rc);
6461 }
6462
6463 /*
6464  * Acquire the MCP access lock.
6465  *
6466  * Returns:
6467  *   0 = Success, !0 = Failure.
6468  */
6469 static int
6470 bxe_acquire_alr(struct bxe_softc *sc)
6471 {
6472         uint32_t val;
6473         int i, rc, retries;
6474
6475         DBENTER(BXE_VERBOSE_MISC);
6476
6477         rc = 0;
6478         retries = 100;
6479         /* Acquire lock using mcpr_access_lock SPLIT register. */
6480         for (i = 0; i < retries * 10; i++) {
6481                 val = 1UL << 31;
6482                 REG_WR(sc, GRCBASE_MCP + 0x9c, val);
6483                 val = REG_RD(sc, GRCBASE_MCP + 0x9c);
6484
6485                 if (val & (1L << 31))
6486                         break;
6487
6488                 DELAY(5000);
6489         }
6490
6491         if (!(val & (1L << 31))) {
6492                 DBPRINT(sc, BXE_WARN,
6493                     "%s(): Cannot acquire MCP split access lock.\n",
6494                     __FUNCTION__);
6495                 rc = EBUSY;
6496         }
6497
6498         DBEXIT(BXE_VERBOSE_MISC);
6499         return (rc);
6500 }
6501
6502
6503 /*
6504  * Release the MCP access lock.
6505  *
6506  * Returns:
6507  *   None.
6508  */
6509 static void
6510 bxe_release_alr(struct bxe_softc* sc)
6511 {
6512
6513         DBENTER(BXE_VERBOSE_MISC);
6514
6515         REG_WR(sc, GRCBASE_MCP + 0x9c, 0);
6516
6517         DBEXIT(BXE_VERBOSE_MISC);
6518 }
6519
6520 /*
6521  * Update driver's copies of the values in the host default status block.
6522  *
6523  * Returns:
6524  *   Bitmap indicating changes to the block.
6525  */
6526 static __inline uint16_t
6527 bxe_update_dsb_idx(struct bxe_softc *sc)
6528 {
6529         struct host_def_status_block *dsb;
6530         uint16_t rc;
6531
6532         rc = 0;
6533         dsb = sc->def_sb;
6534         /* Read memory barrier since block is written by hardware. */
6535         rmb();
6536
6537         if (sc->def_att_idx !=
6538             le16toh(dsb->atten_status_block.attn_bits_index)) {
6539                 sc->def_att_idx =
6540                     le16toh(dsb->atten_status_block.attn_bits_index);
6541                 rc |= 0x1;
6542         }
6543
6544         if (sc->def_c_idx !=
6545             le16toh(dsb->c_def_status_block.status_block_index)) {
6546                 sc->def_c_idx =
6547                     le16toh(dsb->c_def_status_block.status_block_index);
6548                 rc |= 0x2;
6549         }
6550
6551         if (sc->def_u_idx !=
6552             le16toh(dsb->u_def_status_block.status_block_index)) {
6553                 sc->def_u_idx =
6554                     le16toh(dsb->u_def_status_block.status_block_index);
6555                 rc |= 0x4;
6556         }
6557
6558         if (sc->def_x_idx !=
6559             le16toh(dsb->x_def_status_block.status_block_index)) {
6560                 sc->def_x_idx =
6561                     le16toh(dsb->x_def_status_block.status_block_index);
6562                 rc |= 0x8;
6563         }
6564
6565         if (sc->def_t_idx !=
6566             le16toh(dsb->t_def_status_block.status_block_index)) {
6567                 sc->def_t_idx =
6568                     le16toh(dsb->t_def_status_block.status_block_index);
6569                 rc |= 0x10;
6570         }
6571
6572         return (rc);
6573 }
6574
6575 /*
6576  * Handle any attentions that have been newly asserted.
6577  *
6578  * Returns:
6579  *   None
6580  */
6581 static void
6582 bxe_attn_int_asserted(struct bxe_softc *sc, uint32_t asserted)
6583 {
6584         uint32_t aeu_addr, hc_addr, nig_int_mask_addr;
6585         uint32_t aeu_mask, nig_mask;
6586         int port, rc;
6587
6588         DBENTER(BXE_VERBOSE_INTR);
6589
6590         port = BP_PORT(sc);
6591         hc_addr = (HC_REG_COMMAND_REG + port * 32 + COMMAND_REG_ATTN_BITS_SET);
6592         aeu_addr = port ? MISC_REG_AEU_MASK_ATTN_FUNC_1 :
6593             MISC_REG_AEU_MASK_ATTN_FUNC_0;
6594         nig_int_mask_addr = port ? NIG_REG_MASK_INTERRUPT_PORT1 :
6595             NIG_REG_MASK_INTERRUPT_PORT0;
6596         nig_mask = 0;
6597
6598         if (sc->attn_state & asserted)
6599                 BXE_PRINTF("%s(%d): IGU attention ERROR!\n",
6600                     __FILE__, __LINE__);
6601
6602         rc = bxe_acquire_hw_lock(sc,
6603                 HW_LOCK_RESOURCE_PORT0_ATT_MASK + port);
6604         if (rc) {
6605                 DBPRINT(sc, BXE_WARN,
6606                     "%s(): Failed to acquire attention lock for port %d!\n",
6607                     __FUNCTION__, port);
6608                 goto bxe_attn_int_asserted_exit;
6609         }
6610
6611         aeu_mask = REG_RD(sc, aeu_addr);
6612         DBPRINT(sc, BXE_VERBOSE_INTR,
6613             "%s(): aeu_mask = 0x%08X, newly asserted = 0x%08X\n", __FUNCTION__,
6614             aeu_mask, asserted);
6615
6616         aeu_mask &= ~(asserted & 0xff);
6617         DBPRINT(sc, BXE_VERBOSE_INTR, "%s(): new mask = 0x%08X\n", __FUNCTION__,
6618             aeu_mask);
6619         REG_WR(sc, aeu_addr, aeu_mask);
6620
6621         rc = bxe_release_hw_lock(sc, HW_LOCK_RESOURCE_PORT0_ATT_MASK + port);
6622         if (rc) {
6623                 DBPRINT(sc, BXE_WARN,
6624                     "%s(): Failed to release attention lock!\n", __FUNCTION__);
6625                 goto bxe_attn_int_asserted_exit;
6626         }
6627
6628         DBPRINT(sc, BXE_VERBOSE_INTR, "%s(): attn_state = 0x%08X\n",
6629             __FUNCTION__, sc->attn_state);
6630
6631         sc->attn_state |= asserted;
6632         DBPRINT(sc, BXE_VERBOSE_INTR, "%s(): new attn_state = 0x%08X\n",
6633             __FUNCTION__, sc->attn_state);
6634
6635         if (asserted & ATTN_HARD_WIRED_MASK) {
6636                 if (asserted & ATTN_NIG_FOR_FUNC) {
6637                         bxe_acquire_phy_lock(sc);
6638
6639                         /* Save NIG interrupt mask. */
6640                         nig_mask = REG_RD(sc, nig_int_mask_addr);
6641                         REG_WR(sc, nig_int_mask_addr, 0);
6642
6643                         bxe_link_attn(sc);
6644                 }
6645
6646                 if (asserted & ATTN_SW_TIMER_4_FUNC)
6647                         DBPRINT(sc, BXE_WARN, "%s(): ATTN_SW_TIMER_4_FUNC!\n",
6648                             __FUNCTION__);
6649
6650                 if (asserted & GPIO_2_FUNC)
6651                         DBPRINT(sc, BXE_WARN, "%s(): GPIO_2_FUNC!\n",
6652                             __FUNCTION__);
6653
6654                 if (asserted & GPIO_3_FUNC)
6655                         DBPRINT(sc, BXE_WARN, "%s(): GPIO_3_FUNC!\n",
6656                             __FUNCTION__);
6657
6658                 if (asserted & GPIO_4_FUNC)
6659                         DBPRINT(sc, BXE_WARN, "%s(): GPIO_4_FUNC!\n",
6660                             __FUNCTION__);
6661
6662                 if (port == 0) {
6663                         if (asserted & ATTN_GENERAL_ATTN_1) {
6664                                 DBPRINT(sc, BXE_WARN,
6665                                     "%s(): ATTN_GENERAL_ATTN_1!\n",
6666                                     __FUNCTION__);
6667                                 REG_WR(sc, MISC_REG_AEU_GENERAL_ATTN_1, 0x0);
6668                         }
6669
6670                         if (asserted & ATTN_GENERAL_ATTN_2) {
6671                                 DBPRINT(sc, BXE_WARN,
6672                                     "%s(): ATTN_GENERAL_ATTN_2!\n",
6673                                     __FUNCTION__);
6674                                 REG_WR(sc, MISC_REG_AEU_GENERAL_ATTN_2, 0x0);
6675                         }
6676
6677                         if (asserted & ATTN_GENERAL_ATTN_3) {
6678                                 DBPRINT(sc, BXE_WARN,
6679                                     "%s(): ATTN_GENERAL_ATTN_3!\n",
6680                                     __FUNCTION__);
6681                                 REG_WR(sc, MISC_REG_AEU_GENERAL_ATTN_3, 0x0);
6682                         }
6683                 } else {
6684                         if (asserted & ATTN_GENERAL_ATTN_4) {
6685                                 DBPRINT(sc, BXE_WARN,
6686                                     "%s(): ATTN_GENERAL_ATTN_4!\n",
6687                                     __FUNCTION__);
6688                                 REG_WR(sc, MISC_REG_AEU_GENERAL_ATTN_4, 0x0);
6689                         }
6690
6691                         if (asserted & ATTN_GENERAL_ATTN_5) {
6692                                 DBPRINT(sc, BXE_WARN,
6693                                     "%s(): ATTN_GENERAL_ATTN_5!\n",
6694                                     __FUNCTION__);
6695                                 REG_WR(sc, MISC_REG_AEU_GENERAL_ATTN_5, 0x0);
6696                         }
6697                         if (asserted & ATTN_GENERAL_ATTN_6) {
6698                                 DBPRINT(sc, BXE_WARN,
6699                                     "%s(): ATTN_GENERAL_ATTN_6!\n",
6700                                     __FUNCTION__);
6701                                 REG_WR(sc, MISC_REG_AEU_GENERAL_ATTN_6, 0x0);
6702                         }
6703                 }
6704         }
6705
6706         DBPRINT(sc, BXE_VERBOSE_INTR,
6707             "%s(): Writing 0x%08X to HC addr 0x%08X\n", __FUNCTION__,
6708             asserted, hc_addr);
6709         REG_WR(sc, hc_addr, asserted);
6710
6711         /* Now set back the NIG mask. */
6712         if (asserted & ATTN_NIG_FOR_FUNC) {
6713                 REG_WR(sc, nig_int_mask_addr, nig_mask);
6714                 bxe_release_phy_lock(sc);
6715         }
6716
6717 bxe_attn_int_asserted_exit:
6718         DBEXIT(BXE_VERBOSE_INTR);
6719 }
6720
6721 /*
6722  * Handle any attentions that have been newly deasserted.
6723  *
6724  * Returns:
6725  *   None
6726  */
6727 static __inline void
6728 bxe_attn_int_deasserted0(struct bxe_softc *sc, uint32_t attn)
6729 {
6730         uint32_t val, swap_val, swap_override;
6731         int port, reg_offset;
6732
6733         DBENTER(BXE_VERBOSE_INTR);
6734
6735         port = BP_PORT(sc);
6736         reg_offset = port ? MISC_REG_AEU_ENABLE1_FUNC_1_OUT_0 :
6737             MISC_REG_AEU_ENABLE1_FUNC_0_OUT_0;
6738
6739         /* Handle SPIO5 attention. */
6740         if (attn & AEU_INPUTS_ATTN_BITS_SPIO5) {
6741                 val = REG_RD(sc, reg_offset);
6742                 val &= ~AEU_INPUTS_ATTN_BITS_SPIO5;
6743                 REG_WR(sc, reg_offset, val);
6744
6745                 DBPRINT(sc, BXE_FATAL, "%s(): SPIO5 H/W attention!\n",
6746                     __FUNCTION__);
6747                 /* Fan failure attention */
6748                 switch (XGXS_EXT_PHY_TYPE(sc->link_params.ext_phy_config)) {
6749                 case PORT_HW_CFG_XGXS_EXT_PHY_TYPE_SFX7101:
6750                         /*
6751                          * SPIO5 is used on A1022G boards to indicate
6752                          * fan failure.  Shutdown the controller and
6753                          * associated PHY to avoid damage.
6754                          */
6755
6756                         /* Low power mode is controled by GPIO 2. */
6757                         bxe_set_gpio(sc, MISC_REGISTERS_GPIO_2,
6758                                 MISC_REGISTERS_GPIO_OUTPUT_LOW, port);
6759                         /* PHY reset is controled by GPIO 1. */
6760                         bxe_set_gpio(sc, MISC_REGISTERS_GPIO_1,
6761                                 MISC_REGISTERS_GPIO_OUTPUT_LOW, port);
6762                         break;
6763                 case PORT_HW_CFG_XGXS_EXT_PHY_TYPE_BCM8727:
6764                 case PORT_HW_CFG_XGXS_EXT_PHY_TYPE_BCM8481:
6765                         /*
6766                          * The PHY reset is controlled by GPIO 1.
6767                          * Fake the port number to cancel the swap done in
6768                          * set_gpio().
6769                          */
6770                         swap_val = REG_RD(sc, NIG_REG_PORT_SWAP);
6771                         swap_override = REG_RD(sc, NIG_REG_STRAP_OVERRIDE);
6772                         port = (swap_val && swap_override) ^ 1;
6773                         bxe_set_gpio(sc, MISC_REGISTERS_GPIO_1,
6774                             MISC_REGISTERS_GPIO_OUTPUT_LOW, port);
6775                         break;
6776                 default:
6777                         break;
6778                 }
6779
6780                 /* Mark the failure. */
6781                 sc->link_params.ext_phy_config &=
6782                     ~PORT_HW_CFG_XGXS_EXT_PHY_TYPE_MASK;
6783                 sc->link_params.ext_phy_config |=
6784                     PORT_HW_CFG_XGXS_EXT_PHY_TYPE_FAILURE;
6785                 SHMEM_WR(sc, dev_info.port_hw_config[port].external_phy_config,
6786                     sc->link_params.ext_phy_config);
6787                 /* Log the failure */
6788                 BXE_PRINTF("A fan failure has caused the driver to "
6789                     "shutdown the device to prevent permanent damage.\n");
6790         }
6791
6792         if (attn & (AEU_INPUTS_ATTN_BITS_GPIO3_FUNCTION_0 |
6793             AEU_INPUTS_ATTN_BITS_GPIO3_FUNCTION_1)) {
6794                 bxe_acquire_phy_lock(sc);
6795                 bxe_handle_module_detect_int(&sc->link_params);
6796                 bxe_release_phy_lock(sc);
6797         }
6798
6799         /* Checking for an assert on the zero block */
6800         if (attn & HW_INTERRUT_ASSERT_SET_0) {
6801                 val = REG_RD(sc, reg_offset);
6802                 val &= ~(attn & HW_INTERRUT_ASSERT_SET_0);
6803                 REG_WR(sc, reg_offset, val);
6804
6805                 BXE_PRINTF("%s(%d): FATAL hardware block attention "
6806                     "(set0 = 0x%08X)!\n", __FILE__, __LINE__,
6807                     (attn & (uint32_t)HW_INTERRUT_ASSERT_SET_0));
6808
6809                 bxe_panic_dump(sc);
6810         }
6811
6812         DBEXIT(BXE_VERBOSE_INTR);
6813 }
6814
6815 /*
6816  * Handle any attentions that have been newly deasserted.
6817  *
6818  * Returns:
6819  *   None
6820  */
6821 static __inline void
6822 bxe_attn_int_deasserted1(struct bxe_softc *sc, uint32_t attn)
6823 {
6824         uint32_t val;
6825         int port, reg_offset;
6826
6827         DBENTER(BXE_VERBOSE_INTR);
6828
6829         if (attn & AEU_INPUTS_ATTN_BITS_DOORBELLQ_HW_INTERRUPT) {
6830                 val = REG_RD(sc, DORQ_REG_DORQ_INT_STS_CLR);
6831
6832                 DBPRINT(sc, BXE_FATAL,
6833                     "%s(): Doorbell hardware attention (0x%08X).\n",
6834                     __FUNCTION__, val);
6835
6836                 /* DORQ discard attention */
6837                 if (val & 0x2)
6838                         DBPRINT(sc, BXE_FATAL,
6839                             "%s(): FATAL doorbell queue error!\n",
6840                             __FUNCTION__);
6841         }
6842
6843         if (attn & HW_INTERRUT_ASSERT_SET_1) {
6844                 port = BP_PORT(sc);
6845                 reg_offset = port ? MISC_REG_AEU_ENABLE1_FUNC_1_OUT_1 :
6846                     MISC_REG_AEU_ENABLE1_FUNC_0_OUT_1;
6847
6848                 val = REG_RD(sc, reg_offset);
6849                 val &= ~(attn & HW_INTERRUT_ASSERT_SET_1);
6850                 REG_WR(sc, reg_offset, val);
6851
6852                 BXE_PRINTF("%s(%d): FATAL hardware block attention "
6853                     "(set1 = 0x%08X)!\n", __FILE__, __LINE__,
6854                     (attn & (uint32_t)HW_INTERRUT_ASSERT_SET_1));
6855
6856                 bxe_panic_dump(sc);
6857         }
6858
6859         DBEXIT(BXE_VERBOSE_INTR);
6860 }
6861
6862 /*
6863  * Handle any attentions that have been newly deasserted.
6864  *
6865  * Returns:
6866  *   None
6867  */
6868 static __inline void
6869 bxe_attn_int_deasserted2(struct bxe_softc *sc, uint32_t attn)
6870 {
6871         uint32_t val;
6872         int port, reg_offset;
6873
6874         DBENTER(BXE_VERBOSE_INTR);
6875
6876         if (attn & AEU_INPUTS_ATTN_BITS_CFC_HW_INTERRUPT) {
6877                 val = REG_RD(sc, CFC_REG_CFC_INT_STS_CLR);
6878
6879                 DBPRINT(sc, BXE_FATAL,
6880                     "%s(): CFC hardware attention (0x%08X).\n", __FUNCTION__,
6881                     val);
6882
6883                 /* CFC error attention. */
6884                 if (val & 0x2)
6885                         DBPRINT(sc, BXE_FATAL, "%s(): FATAL CFC error!\n",
6886                             __FUNCTION__);
6887         }
6888
6889         if (attn & AEU_INPUTS_ATTN_BITS_PXP_HW_INTERRUPT) {
6890                 val = REG_RD(sc, PXP_REG_PXP_INT_STS_CLR_0);
6891
6892                 DBPRINT(sc, BXE_FATAL,
6893                     "%s(): PXP hardware attention (0x%08X).\n", __FUNCTION__,
6894                     val);
6895
6896                 /* RQ_USDMDP_FIFO_OVERFLOW */
6897                 if (val & 0x18000)
6898                         DBPRINT(sc, BXE_FATAL, "%s(): FATAL PXP error!\n",
6899                             __FUNCTION__);
6900         }
6901
6902         if (attn & HW_INTERRUT_ASSERT_SET_2) {
6903                 port = BP_PORT(sc);
6904                 reg_offset = port ? MISC_REG_AEU_ENABLE1_FUNC_1_OUT_2 :
6905                     MISC_REG_AEU_ENABLE1_FUNC_0_OUT_2;
6906
6907                 val = REG_RD(sc, reg_offset);
6908                 val &= ~(attn & HW_INTERRUT_ASSERT_SET_2);
6909                 REG_WR(sc, reg_offset, val);
6910
6911                 BXE_PRINTF("%s(%d): FATAL hardware block attention (set2 = "
6912                     "0x%08X)! port=%d, val written=0x%x attn=0x%x\n", __FILE__,
6913                     __LINE__, (attn & (uint32_t)HW_INTERRUT_ASSERT_SET_2),
6914                     port, val, attn);
6915
6916                 bxe_panic_dump(sc);
6917         }
6918
6919         DBEXIT(BXE_VERBOSE_INTR);
6920 }
6921
6922 /*
6923  * Handle any attentions that have been newly deasserted.
6924  *
6925  * Returns:
6926  *   None
6927  */
6928 static __inline void
6929 bxe_attn_int_deasserted3(struct bxe_softc *sc, uint32_t attn)
6930 {
6931         uint32_t val;
6932         int func;
6933
6934         DBENTER(BXE_VERBOSE_INTR);
6935
6936         if (attn & EVEREST_GEN_ATTN_IN_USE_MASK) {
6937                 /* Look for any port assertions. */
6938                 if (attn & BXE_PMF_LINK_ASSERT) {
6939                         /*
6940                          * We received a message from the driver instance
6941                          * that is managing the Ethernet port (link up/down).
6942                          * Go ahead and handle it.
6943                          */
6944                         func = BP_FUNC(sc);
6945
6946                         DBPRINT(sc, BXE_INFO,
6947                             "%s(): Received link attention from PMF.\n",
6948                             __FUNCTION__);
6949
6950                         /* Clear the attention. */
6951                         REG_WR(sc, MISC_REG_AEU_GENERAL_ATTN_12 + func * 4, 0);
6952                         sc->mf_config[BP_E1HVN(sc)] =
6953                             SHMEM_RD(sc,
6954                             mf_cfg.func_mf_config[(sc->bxe_func & 1)].config);
6955                         val = SHMEM_RD(sc, func_mb[func].drv_status);
6956                         if (sc->dcc_enable == TRUE) {
6957                                 if (val & DRV_STATUS_DCC_EVENT_MASK)
6958                                         bxe_dcc_event(sc,
6959                                             val & DRV_STATUS_DCC_EVENT_MASK);
6960                         }
6961                         bxe__link_status_update(sc);
6962
6963                         if ((sc->port.pmf == 0) && (val & DRV_STATUS_PMF))
6964                                 bxe_pmf_update(sc);
6965                 /* Look for any microcode assertions. */
6966                 } else if (attn & BXE_MC_ASSERT_BITS) {
6967                         DBPRINT(sc, BXE_FATAL, "%s(): Microcode assert!\n",
6968                             __FUNCTION__);
6969
6970                         REG_WR(sc, MISC_REG_AEU_GENERAL_ATTN_10, 0);
6971                         REG_WR(sc, MISC_REG_AEU_GENERAL_ATTN_9, 0);
6972                         REG_WR(sc, MISC_REG_AEU_GENERAL_ATTN_8, 0);
6973                         REG_WR(sc, MISC_REG_AEU_GENERAL_ATTN_7, 0);
6974
6975                         bxe_panic_dump(sc);
6976
6977                 /* Look for any bootcode assertions. */
6978                 } else if (attn & BXE_MCP_ASSERT) {
6979                         DBPRINT(sc, BXE_FATAL, "%s(): Bootcode assert!\n",
6980                                 __FUNCTION__);
6981
6982                         REG_WR(sc, MISC_REG_AEU_GENERAL_ATTN_11, 0);
6983
6984                         DBRUN(bxe_dump_fw(sc));
6985                 } else
6986                         DBPRINT(sc, BXE_FATAL,
6987                             "%s(): Unknown hardware assertion "
6988                             "(attn = 0x%08X)!\n", __FUNCTION__, attn);
6989         }
6990
6991         /* Look for any hardware latched attentions. */
6992         if (attn & EVEREST_LATCHED_ATTN_IN_USE_MASK) {
6993                 DBPRINT(sc, BXE_FATAL,
6994                     "%s(): Latched attention 0x%08X (masked)!\n", __FUNCTION__,
6995                     attn);
6996
6997                 /* Check if a GRC register access timeout occurred. */
6998                 if (attn & BXE_GRC_TIMEOUT) {
6999                         val = CHIP_IS_E1H(sc) ? REG_RD(sc,
7000                             MISC_REG_GRC_TIMEOUT_ATTN) : 0;
7001
7002                         DBPRINT(sc, BXE_WARN,
7003                             "%s(): GRC timeout for register 0x%08X!\n",
7004                             __FUNCTION__, val);
7005                 }
7006
7007                 /* Check if a GRC reserved register was accessed. */
7008                 if (attn & BXE_GRC_RSV) {
7009                         val = CHIP_IS_E1H(sc) ? REG_RD(sc,
7010                             MISC_REG_GRC_RSV_ATTN) : 0;
7011
7012                         DBPRINT(sc, BXE_WARN,
7013                             "%s(): GRC register 0x%08X is reserved!\n",
7014                             __FUNCTION__, val);
7015                 }
7016
7017                 REG_WR(sc, MISC_REG_AEU_CLR_LATCH_SIGNAL, 0x7ff);
7018         }
7019
7020         DBEXIT(BXE_VERBOSE_INTR);
7021 }
7022
7023 /*
7024  * Handle any attentions that have been newly deasserted.
7025  *
7026  * Returns:
7027  *   None
7028  */
7029 static void
7030 bxe_attn_int_deasserted(struct bxe_softc *sc, uint32_t deasserted)
7031 {
7032         struct attn_route attn;
7033         struct attn_route group_mask;
7034         uint32_t val, reg_addr, aeu_mask;
7035         int index, port;
7036
7037         DBENTER(BXE_VERBOSE_INTR);
7038
7039         /*
7040          * Need to take HW lock because MCP or other port might also try
7041          * to handle this event.
7042          */
7043         bxe_acquire_alr(sc);
7044
7045         port = BP_PORT(sc);
7046         /* Get the current attention signal bits. */
7047         attn.sig[0] = REG_RD(sc,
7048             MISC_REG_AEU_AFTER_INVERT_1_FUNC_0 + port * 4);
7049         attn.sig[1] = REG_RD(sc,
7050             MISC_REG_AEU_AFTER_INVERT_2_FUNC_0 + port * 4);
7051         attn.sig[2] = REG_RD(sc,
7052             MISC_REG_AEU_AFTER_INVERT_3_FUNC_0 + port * 4);
7053         attn.sig[3] = REG_RD(sc,
7054             MISC_REG_AEU_AFTER_INVERT_4_FUNC_0 + port * 4);
7055
7056         DBPRINT(sc, BXE_EXTREME_INTR,
7057             "%s(): attention = 0x%08X 0x%08X 0x%08X 0x%08X\n", __FUNCTION__,
7058             attn.sig[0], attn.sig[1], attn.sig[2], attn.sig[3]);
7059
7060         /*
7061          * Compare the current attention bits to each attention group
7062          * to see if anyone has registered this attention.
7063          */
7064         for (index = 0; index < MAX_DYNAMIC_ATTN_GRPS; index++) {
7065                 if (deasserted & (1 << index)) {
7066                         group_mask = sc->attn_group[index];
7067
7068                         DBPRINT(sc, BXE_EXTREME_INTR,
7069                             "%s(): group[%02d] = 0x%08X 0x%08X 0x%08x 0X%08x\n",
7070                             __FUNCTION__, index, group_mask.sig[0],
7071                             group_mask.sig[1], group_mask.sig[2],
7072                             group_mask.sig[3]);
7073
7074                         /* Handle any registered attentions. */
7075                         bxe_attn_int_deasserted3(sc,
7076                             attn.sig[3] & group_mask.sig[3]);
7077                         bxe_attn_int_deasserted1(sc,
7078                             attn.sig[1] & group_mask.sig[1]);
7079                         bxe_attn_int_deasserted2(sc,
7080                             attn.sig[2] & group_mask.sig[2]);
7081                         bxe_attn_int_deasserted0(sc,
7082                             attn.sig[0] & group_mask.sig[0]);
7083
7084                         if ((attn.sig[0] & group_mask.sig[0] &
7085                             HW_PRTY_ASSERT_SET_0) ||
7086                             (attn.sig[1] & group_mask.sig[1] &
7087                             HW_PRTY_ASSERT_SET_1) ||
7088                             (attn.sig[2] & group_mask.sig[2] &
7089                             HW_PRTY_ASSERT_SET_2))
7090                                 BXE_PRINTF("%s(%d): FATAL hardware block "
7091                                     "parity attention!\n", __FILE__, __LINE__);
7092                 }
7093         }
7094
7095         bxe_release_alr(sc);
7096
7097         reg_addr = (HC_REG_COMMAND_REG +
7098             port * 32 + COMMAND_REG_ATTN_BITS_CLR);
7099
7100         val = ~deasserted;
7101         DBPRINT(sc, BXE_EXTREME_INTR,
7102             "%s(): About to mask 0x%08X at HC addr 0x%08X\n", __FUNCTION__,
7103             deasserted, reg_addr);
7104         REG_WR(sc, reg_addr, val);
7105
7106         if (~sc->attn_state & deasserted)
7107                 DBPRINT(sc, BXE_FATAL, "%s(): IGU Bug!\n", __FUNCTION__);
7108
7109         reg_addr = port ? MISC_REG_AEU_MASK_ATTN_FUNC_1 :
7110             MISC_REG_AEU_MASK_ATTN_FUNC_0;
7111
7112         bxe_acquire_hw_lock(sc, HW_LOCK_RESOURCE_PORT0_ATT_MASK + port);
7113         aeu_mask = REG_RD(sc, reg_addr);
7114
7115         DBPRINT(sc, BXE_EXTREME_INTR,
7116             "%s(): Current aeu_mask = 0x%08X, newly deasserted = 0x%08X\n",
7117             __FUNCTION__, aeu_mask, deasserted);
7118         aeu_mask |= (deasserted & 0xff);
7119
7120         DBPRINT(sc, BXE_EXTREME_INTR, "%s(): New aeu_mask = 0x%08X\n",
7121             __FUNCTION__, aeu_mask);
7122
7123         REG_WR(sc, reg_addr, aeu_mask);
7124         bxe_release_hw_lock(sc, HW_LOCK_RESOURCE_PORT0_ATT_MASK + port);
7125
7126         DBPRINT(sc, BXE_EXTREME_INTR, "%s(): Current attn_state = 0x%08X\n",
7127             __FUNCTION__, sc->attn_state);
7128
7129         sc->attn_state &= ~deasserted;
7130         DBPRINT(sc, BXE_EXTREME_INTR, "%s(): New attn_state = 0x%08X\n",
7131             __FUNCTION__, sc->attn_state);
7132
7133         DBEXIT(BXE_VERBOSE_INTR);
7134 }
7135
7136 /*
7137  * Handle interrupts caused by internal attentions (everything else other
7138  * than RX, TX, and link state changes).
7139  *
7140  * Returns:
7141  *   None
7142  */
7143 static void
7144 bxe_attn_int(struct bxe_softc* sc)
7145 {
7146         uint32_t attn_ack, attn_bits, attn_state;
7147         uint32_t asserted, deasserted;
7148
7149         DBENTER(BXE_VERBOSE_INTR);
7150
7151         attn_bits = le32toh(sc->def_sb->atten_status_block.attn_bits);
7152         attn_ack =
7153             le32toh(sc->def_sb->atten_status_block.attn_bits_ack);
7154         attn_state = sc->attn_state;
7155         asserted = attn_bits & ~attn_ack & ~attn_state;
7156         deasserted = ~attn_bits &  attn_ack &  attn_state;
7157
7158         /* Make sure we're in a sane state. */
7159         if (~(attn_bits ^ attn_ack) & (attn_bits ^ attn_state))
7160             BXE_PRINTF("%s(%d): Bad attention state!\n",
7161                 __FILE__, __LINE__);
7162
7163         /* Handle any attentions that are newly asserted. */
7164         if (asserted) {
7165                 DBPRINT(sc, BXE_VERBOSE_INTR,
7166                     "%s(): attn_state = 0x%08X, attn_bits = 0x%08X, "
7167                     "attn_ack = 0x%08X, asserted = 0x%08X\n", __FUNCTION__,
7168                     attn_state, attn_bits, attn_ack, asserted);
7169                 bxe_attn_int_asserted(sc, asserted);
7170         }
7171
7172         /* Handle any attentions that are newly deasserted. */
7173         if (deasserted) {
7174                 DBPRINT(sc, BXE_VERBOSE_INTR,
7175                     "%s(): attn_state = 0x%08X, attn_bits = 0x%08X, "
7176                     "attn_ack = 0x%08X, deasserted = 0x%08X\n", __FUNCTION__,
7177                     attn_state, attn_bits, attn_ack, deasserted);
7178                 bxe_attn_int_deasserted(sc, deasserted);
7179         }
7180
7181         DBEXIT(BXE_VERBOSE_INTR);
7182 }
7183
7184 /* sum[hi:lo] += add[hi:lo] */
7185 #define ADD_64(s_hi, a_hi, s_lo, a_lo) do {                     \
7186         s_lo += a_lo;                                           \
7187         s_hi += a_hi + ((s_lo < a_lo) ? 1 : 0);                 \
7188 } while (0)
7189
7190 /* Subtraction = minuend -= subtrahend */
7191 #define SUB_64(m_hi, s_hi, m_lo, s_lo)                          \
7192         do {                                                    \
7193                 DIFF_64(m_hi, m_hi, s_hi, m_lo, m_lo, s_lo);    \
7194         } while (0)
7195
7196
7197 /* difference = minuend - subtrahend */
7198 #define DIFF_64(d_hi, m_hi, s_hi, d_lo, m_lo, s_lo) do {        \
7199         if (m_lo < s_lo) {                                      \
7200                 /* underflow */                                 \
7201                 d_hi = m_hi - s_hi;                             \
7202                 if (d_hi > 0) {                                 \
7203                         /* we can 'loan' 1 */                   \
7204                         d_hi--;                                 \
7205                         d_lo = m_lo + (UINT_MAX - s_lo) + 1;    \
7206                 } else {                                        \
7207                         /* m_hi <= s_hi */                      \
7208                         d_hi = 0;                               \
7209                         d_lo = 0;                               \
7210                 }                                               \
7211         } else {                                                \
7212                 /* m_lo >= s_lo */                              \
7213                 if (m_hi < s_hi) {                              \
7214                         d_hi = 0;                               \
7215                         d_lo = 0;                               \
7216                 } else {                                        \
7217                         /* m_hi >= s_hi */                      \
7218                         d_hi = m_hi - s_hi;                     \
7219                         d_lo = m_lo - s_lo;                     \
7220                 }                                               \
7221         }                                                       \
7222 } while (0)
7223
7224 #define UPDATE_STAT64(s, t) do {                                \
7225         DIFF_64(diff.hi, new->s##_hi, pstats->mac_stx[0].t##_hi,\
7226             diff.lo, new->s##_lo, pstats->mac_stx[0].t##_lo);   \
7227         pstats->mac_stx[0].t##_hi = new->s##_hi;                \
7228         pstats->mac_stx[0].t##_lo = new->s##_lo;                \
7229         ADD_64(pstats->mac_stx[1].t##_hi, diff.hi,              \
7230             pstats->mac_stx[1].t##_lo, diff.lo);                \
7231 } while (0)
7232
7233 #define UPDATE_STAT64_NIG(s, t) do {                            \
7234         DIFF_64(diff.hi, new->s##_hi, old->s##_hi,              \
7235             diff.lo, new->s##_lo, old->s##_lo);                 \
7236         ADD_64(estats->t##_hi, diff.hi,                         \
7237             estats->t##_lo, diff.lo);                           \
7238 } while (0)
7239
7240 /* sum[hi:lo] += add */
7241 #define ADD_EXTEND_64(s_hi, s_lo, a) do {                       \
7242         s_lo += a;                                              \
7243         s_hi += (s_lo < a) ? 1 : 0;                             \
7244 } while (0)
7245
7246 #define UPDATE_EXTEND_STAT(s) do {                              \
7247         ADD_EXTEND_64(pstats->mac_stx[1].s##_hi,                \
7248             pstats->mac_stx[1].s##_lo, new->s);                 \
7249 } while (0)
7250
7251 #define UPDATE_EXTEND_TSTAT(s, t) do {                          \
7252         diff = (tclient->s) - (old_tclient->s); \
7253         old_tclient->s = (tclient->s);                          \
7254         ADD_EXTEND_64(qstats->t##_hi, qstats->t##_lo, diff);    \
7255 } while (0)
7256
7257 #define UPDATE_EXTEND_XSTAT(s, t) do {                          \
7258         diff = xclient->s - old_xclient->s;     \
7259         old_xclient->s = xclient->s;                            \
7260         ADD_EXTEND_64(qstats->t##_hi, qstats->t##_lo, diff);    \
7261 } while (0)
7262
7263 #define UPDATE_EXTEND_USTAT(s, t) do {                          \
7264         diff = uclient->s - old_uclient->s;     \
7265         old_uclient->s = uclient->s;                            \
7266         ADD_EXTEND_64(qstats->t##_hi, qstats->t##_lo, diff);    \
7267 } while (0)
7268
7269 #define SUB_EXTEND_64(m_hi, m_lo, s)do {                        \
7270         SUB_64(m_hi, 0, m_lo, s);                               \
7271 } while (0)
7272
7273 #define SUB_EXTEND_USTAT(s, t)do {                              \
7274         diff = (uclient->s) - (old_uclient->s); \
7275         SUB_EXTEND_64(qstats->t##_hi, qstats->t##_lo, diff);    \
7276 } while (0)
7277
7278
7279
7280
7281 #ifdef __i386__
7282 #define BITS_PER_LONG   32
7283 #else
7284 #define BITS_PER_LONG   64
7285 #endif
7286
7287 static __inline long
7288 bxe_hilo(uint32_t *hiref)
7289 {
7290         uint32_t lo;
7291
7292         lo = *(hiref + 1);
7293 #if (BITS_PER_LONG == 64)
7294         uint32_t hi = *hiref;
7295         return (HILO_U64(hi, lo));
7296 #else
7297         return (lo);
7298 #endif
7299 }
7300
7301 /*
7302  * Request the STORM statistics by posting a slowpath ramrod.
7303  *
7304  * Returns:
7305  *   None.
7306  */
7307 static void
7308 bxe_stats_storm_post(struct bxe_softc *sc)
7309 {
7310         struct eth_query_ramrod_data ramrod_data = {0};
7311         int i, rc;
7312
7313         DBENTER(BXE_INSANE_STATS);
7314
7315         if (!sc->stats_pending) {
7316                 ramrod_data.drv_counter = sc->stats_counter++;
7317                 ramrod_data.collect_port = sc->port.pmf ? 1 : 0;
7318                 for (i = 0; i < sc->num_queues; i++)
7319                         ramrod_data.ctr_id_vector |= (1 << sc->fp[i].cl_id);
7320
7321                 rc = bxe_sp_post(sc, RAMROD_CMD_ID_ETH_STAT_QUERY, 0,
7322                     ((uint32_t *)&ramrod_data)[1],
7323                     ((uint32_t *)&ramrod_data)[0], 0);
7324                 if (rc == 0) {
7325                         /* Stats ramrod has it's own slot on the SPQ. */
7326                         sc->spq_left++;
7327                         sc->stats_pending = 1;
7328                 }
7329         }
7330
7331         DBEXIT(BXE_INSANE_STATS);
7332 }
7333
7334 /*
7335  * Setup the adrress used by the driver to report port-based statistics
7336  * back to the controller.
7337  *
7338  * Returns:
7339  *   None.
7340  */
7341 static void
7342 bxe_stats_port_base_init(struct bxe_softc *sc)
7343 {
7344         uint32_t *stats_comp;
7345         struct dmae_command *dmae;
7346
7347         DBENTER(BXE_VERBOSE_STATS);
7348
7349         /* Only the port management function (PMF) does this work. */
7350         if ((sc->port.pmf == 0) || !sc->port.port_stx) {
7351                 BXE_PRINTF("%s(%d): Invalid statistcs port setup!\n",
7352                     __FILE__, __LINE__);
7353                 goto bxe_stats_port_base_init_exit;
7354         }
7355
7356         stats_comp = BXE_SP(sc, stats_comp);
7357         sc->executer_idx = 0;
7358
7359         /* DMA the address of the drivers port statistics block. */
7360         dmae = BXE_SP(sc, dmae[sc->executer_idx++]);
7361         dmae->opcode = (DMAE_CMD_SRC_PCI | DMAE_CMD_DST_GRC |
7362                         DMAE_CMD_C_DST_PCI | DMAE_CMD_C_ENABLE |
7363                         DMAE_CMD_SRC_RESET | DMAE_CMD_DST_RESET |
7364 #ifdef __BIG_ENDIAN
7365                         DMAE_CMD_ENDIANITY_B_DW_SWAP |
7366 #else
7367                         DMAE_CMD_ENDIANITY_DW_SWAP |
7368 #endif
7369                         (BP_PORT(sc) ? DMAE_CMD_PORT_1 : DMAE_CMD_PORT_0) |
7370                         (BP_E1HVN(sc) << DMAE_CMD_E1HVN_SHIFT));
7371         dmae->src_addr_lo = U64_LO(BXE_SP_MAPPING(sc, port_stats));
7372         dmae->src_addr_hi = U64_HI(BXE_SP_MAPPING(sc, port_stats));
7373         dmae->dst_addr_lo = sc->port.port_stx >> 2;
7374         dmae->dst_addr_hi = 0;
7375         dmae->len = sizeof(struct host_port_stats) >> 2;
7376         dmae->comp_addr_lo = U64_LO(BXE_SP_MAPPING(sc, stats_comp));
7377         dmae->comp_addr_hi = U64_HI(BXE_SP_MAPPING(sc, stats_comp));
7378         dmae->comp_val = DMAE_COMP_VAL;
7379
7380         *stats_comp = 0;
7381         bxe_stats_hw_post(sc);
7382         bxe_stats_comp(sc);
7383
7384 bxe_stats_port_base_init_exit:
7385         DBEXIT(BXE_VERBOSE_STATS);
7386 }
7387
7388 /*
7389  * Setup the adrress used by the driver to report function-based statistics
7390  * back to the controller.
7391  *
7392  * Returns:
7393  *   None.
7394  */
7395 static void
7396 bxe_stats_func_base_init(struct bxe_softc *sc)
7397 {
7398         int port, func;
7399         int vn, vn_max;
7400         uint32_t func_stx;
7401
7402         DBENTER(BXE_VERBOSE_STATS);
7403
7404         /* Only the port management function (PMF) does this work. */
7405         if ((sc->port.pmf == 0) || !sc->func_stx) {
7406                 BXE_PRINTF("%s(%d): Invalid statistcs function setup!\n",
7407                     __FILE__, __LINE__);
7408                 goto bxe_stats_func_base_init_exit;
7409         }
7410
7411         port = BP_PORT(sc);
7412         func_stx = sc->func_stx;
7413         vn_max = IS_E1HMF(sc) ? E1HVN_MAX : E1VN_MAX;
7414
7415         /* Initialize each function individually. */
7416         for (vn = VN_0; vn < vn_max; vn++) {
7417                 func = 2 * vn + port;
7418                 sc->func_stx = SHMEM_RD(sc, func_mb[func].fw_mb_param);
7419                 bxe_stats_func_init(sc);
7420                 bxe_stats_hw_post(sc);
7421                 bxe_stats_comp(sc);
7422         }
7423
7424         sc->func_stx = func_stx;
7425
7426 bxe_stats_func_base_init_exit:
7427         DBEXIT(BXE_VERBOSE_STATS);
7428 }
7429
7430 /*
7431  * DMA the function-based statistics to the controller.
7432  *
7433  * Returns:
7434  *   None.
7435  */
7436 static void
7437 bxe_stats_func_base_update(struct bxe_softc *sc)
7438 {
7439         uint32_t *stats_comp;
7440         struct dmae_command *dmae;
7441
7442         DBENTER(BXE_VERBOSE_STATS);
7443
7444         /* Only the port management function (PMF) does this work. */
7445         if ((sc->port.pmf == 0) || !sc->func_stx) {
7446                 BXE_PRINTF("%s(%d): Invalid statistcs function update!\n",
7447                     __FILE__, __LINE__);
7448                 goto bxe_stats_func_base_update_exit;
7449         }
7450
7451         dmae = &sc->stats_dmae;
7452         stats_comp = BXE_SP(sc, stats_comp);
7453         sc->executer_idx = 0;
7454         memset(dmae, 0, sizeof(struct dmae_command));
7455
7456         /* DMA the function statistics from the driver to the H/W. */
7457         dmae->opcode = (DMAE_CMD_SRC_GRC | DMAE_CMD_DST_PCI |
7458                         DMAE_CMD_C_DST_PCI | DMAE_CMD_C_ENABLE |
7459                         DMAE_CMD_SRC_RESET | DMAE_CMD_DST_RESET |
7460 #ifdef __BIG_ENDIAN
7461                         DMAE_CMD_ENDIANITY_B_DW_SWAP |
7462 #else
7463                         DMAE_CMD_ENDIANITY_DW_SWAP |
7464 #endif
7465                         (BP_PORT(sc) ? DMAE_CMD_PORT_1 : DMAE_CMD_PORT_0) |
7466                         (BP_E1HVN(sc) << DMAE_CMD_E1HVN_SHIFT));
7467         dmae->src_addr_lo = sc->func_stx >> 2;
7468         dmae->src_addr_hi = 0;
7469         dmae->dst_addr_lo = U64_LO(BXE_SP_MAPPING(sc, func_stats_base));
7470         dmae->dst_addr_hi = U64_HI(BXE_SP_MAPPING(sc, func_stats_base));
7471         dmae->len = sizeof(struct host_func_stats) >> 2;
7472         dmae->comp_addr_lo = U64_LO(BXE_SP_MAPPING(sc, stats_comp));
7473         dmae->comp_addr_hi = U64_HI(BXE_SP_MAPPING(sc, stats_comp));
7474         dmae->comp_val = DMAE_COMP_VAL;
7475
7476         *stats_comp = 0;
7477         bxe_stats_hw_post(sc);
7478         bxe_stats_comp(sc);
7479
7480 bxe_stats_func_base_update_exit:
7481         DBEXIT(BXE_VERBOSE_STATS);
7482 }
7483
7484
7485 /*
7486  * Initialize statistics.
7487  *
7488  * Returns:
7489  *   Nothing.
7490  */
7491 static void
7492 bxe_stats_init(struct bxe_softc *sc)
7493 {
7494         struct bxe_fastpath *fp;
7495         int func, i, port;
7496
7497         DBENTER(BXE_VERBOSE_STATS);
7498
7499         if (sc->stats_enable == FALSE)
7500                 goto bxe_stats_init_exit;
7501
7502         port = BP_PORT(sc);
7503         func = BP_FUNC(sc);
7504         sc->executer_idx  = 0;
7505         sc->stats_counter = 0;
7506         sc->stats_pending = 0;
7507
7508         /* Fetch the offset of port & function statistics in shared memory. */
7509         if (NOMCP(sc)){
7510                 sc->port.port_stx = 0;
7511                 sc->func_stx = 0;
7512         } else{
7513                 sc->port.port_stx = SHMEM_RD(sc, port_mb[port].port_stx);
7514                 sc->func_stx = SHMEM_RD(sc, func_mb[func].fw_mb_param);
7515         }
7516
7517         DBPRINT(sc, BXE_VERBOSE_STATS, "%s(): sc->port.port_stx = 0x%08X\n",
7518             __FUNCTION__, sc->port.port_stx);
7519         DBPRINT(sc, BXE_VERBOSE_STATS, "%s(): sc->func_stx = 0x%08X\n",
7520             __FUNCTION__, sc->func_stx);
7521
7522         /* Port statistics. */
7523         memset(&(sc->port.old_nig_stats), 0, sizeof(struct nig_stats));
7524         sc->port.old_nig_stats.brb_discard = REG_RD(sc,
7525             NIG_REG_STAT0_BRB_DISCARD + port * 0x38);
7526         sc->port.old_nig_stats.brb_truncate = REG_RD(sc,
7527             NIG_REG_STAT0_BRB_TRUNCATE + port * 0x38);
7528         REG_RD_DMAE(sc, NIG_REG_STAT0_EGRESS_MAC_PKT0 + port * 0x50,
7529             &(sc->port.old_nig_stats.egress_mac_pkt0_lo), 2);
7530         REG_RD_DMAE(sc, NIG_REG_STAT0_EGRESS_MAC_PKT1 + port * 0x50,
7531             &(sc->port.old_nig_stats.egress_mac_pkt1_lo), 2);
7532
7533         /* Function statistics. */
7534         for (i = 0; i < sc->num_queues; i++) {
7535                 fp = &sc->fp[i];
7536
7537                 /* Clear all per-queue statistics. */
7538                 memset(&fp->old_tclient, 0,
7539                     sizeof(struct tstorm_per_client_stats));
7540                 memset(&fp->old_uclient, 0,
7541                     sizeof(struct ustorm_per_client_stats));
7542                 memset(&fp->old_xclient, 0,
7543                     sizeof(struct xstorm_per_client_stats));
7544                 memset(&fp->eth_q_stats, 0,
7545                     sizeof(struct bxe_q_stats));
7546         }
7547
7548         /* ToDo: Clear any driver specific statistics? */
7549
7550         sc->stats_state = STATS_STATE_DISABLED;
7551
7552         if (sc->port.pmf == 1) {
7553                 /* Init port & function stats if we're PMF. */
7554                 if (sc->port.port_stx)
7555                         bxe_stats_port_base_init(sc);
7556                 if (sc->func_stx)
7557                         bxe_stats_func_base_init(sc);
7558         } else if (sc->func_stx)
7559                 /* Update function stats if we're not PMF. */
7560                 bxe_stats_func_base_update(sc);
7561
7562 bxe_stats_init_exit:
7563         DBEXIT(BXE_VERBOSE_STATS);
7564 }
7565
7566 /*
7567  *
7568  * Returns:
7569  *   None.
7570  */
7571 static void
7572 bxe_stats_hw_post(struct bxe_softc *sc)
7573 {
7574         struct dmae_command *dmae;
7575         uint32_t *stats_comp;
7576         int loader_idx;
7577
7578         DBENTER(BXE_INSANE_STATS);
7579
7580         dmae = &sc->stats_dmae;
7581         stats_comp = BXE_SP(sc, stats_comp);
7582         *stats_comp = DMAE_COMP_VAL;
7583
7584         if (sc->executer_idx) {
7585                 loader_idx = PMF_DMAE_C(sc);
7586
7587                 memset(dmae, 0, sizeof(struct dmae_command));
7588
7589                 dmae->opcode = (DMAE_CMD_SRC_PCI | DMAE_CMD_DST_GRC |
7590                     DMAE_CMD_C_DST_GRC | DMAE_CMD_C_ENABLE |
7591                     DMAE_CMD_DST_RESET |
7592 #ifdef __BIG_ENDIAN
7593                     DMAE_CMD_ENDIANITY_B_DW_SWAP |
7594 #else
7595                     DMAE_CMD_ENDIANITY_DW_SWAP |
7596 #endif
7597                     (BP_PORT(sc) ? DMAE_CMD_PORT_1 : DMAE_CMD_PORT_0) |
7598                     (BP_E1HVN(sc) << DMAE_CMD_E1HVN_SHIFT));
7599
7600                 dmae->src_addr_lo = U64_LO(BXE_SP_MAPPING(sc, dmae[0]));
7601                 dmae->src_addr_hi = U64_HI(BXE_SP_MAPPING(sc, dmae[0]));
7602                 dmae->dst_addr_lo = (DMAE_REG_CMD_MEM +
7603                     sizeof(struct dmae_command) * (loader_idx + 1)) >> 2;
7604                 dmae->dst_addr_hi = 0;
7605                 dmae->len = sizeof(struct dmae_command) >> 2;
7606
7607                 if (CHIP_IS_E1(sc))
7608                         dmae->len--;
7609
7610                 dmae->comp_addr_lo = dmae_reg_go_c[loader_idx + 1] >> 2;
7611                 dmae->comp_addr_hi = 0;
7612                 dmae->comp_val = 1;
7613
7614                 *stats_comp = 0;
7615                 bxe_post_dmae(sc, dmae, loader_idx);
7616
7617         } else if (sc->func_stx) {
7618                 *stats_comp = 0;
7619                 bxe_post_dmae(sc, dmae, INIT_DMAE_C(sc));
7620         }
7621
7622         DBEXIT(BXE_INSANE_STATS);
7623 }
7624
7625 /*
7626  * Delay routine which polls for the DMA engine to complete.
7627  *
7628  * Returns:
7629  *   0 = Failure, !0 = Success
7630  */
7631 static int
7632 bxe_stats_comp(struct bxe_softc *sc)
7633 {
7634         uint32_t *stats_comp;
7635         int cnt;
7636
7637         DBENTER(BXE_VERBOSE_STATS);
7638
7639         stats_comp = BXE_SP(sc, stats_comp);
7640         cnt = 10;
7641
7642         while (*stats_comp != DMAE_COMP_VAL) {
7643                 if (!cnt) {
7644                         BXE_PRINTF("%s(%d): Timeout waiting for statistics "
7645                             "completions.\n", __FILE__, __LINE__);
7646                         break;
7647                 }
7648                 cnt--;
7649                 DELAY(1000);
7650         }
7651
7652         DBEXIT(BXE_VERBOSE_STATS);
7653         /* ToDo: Shouldn't this return the value of cnt? */
7654         return (1);
7655 }
7656
7657 /*
7658  * DMA port statistcs from controller to driver.
7659  *
7660  * Returns:
7661  *   None.
7662  */
7663 static void
7664 bxe_stats_pmf_update(struct bxe_softc *sc)
7665 {
7666         struct dmae_command *dmae;
7667         uint32_t opcode, *stats_comp;
7668         int loader_idx;
7669
7670         DBENTER(BXE_VERBOSE_STATS);
7671
7672         stats_comp = BXE_SP(sc, stats_comp);
7673         loader_idx = PMF_DMAE_C(sc);
7674
7675         /* We shouldn't be here if any of the following are false. */
7676         if (!IS_E1HMF(sc) || (sc->port.pmf == 0) || !sc->port.port_stx) {
7677                 BXE_PRINTF("%s(%d): Statistics bug!\n", __FILE__, __LINE__);
7678                 goto bxe_stats_pmf_update_exit;
7679         }
7680
7681         sc->executer_idx = 0;
7682
7683         /* Instruct DMA engine to copy port statistics from H/W to driver. */
7684         opcode = (DMAE_CMD_SRC_GRC | DMAE_CMD_DST_PCI |
7685             DMAE_CMD_C_DST_PCI | DMAE_CMD_C_ENABLE |
7686             DMAE_CMD_SRC_RESET | DMAE_CMD_DST_RESET |
7687 #ifdef __BIG_ENDIAN
7688             DMAE_CMD_ENDIANITY_B_DW_SWAP |
7689 #else
7690             DMAE_CMD_ENDIANITY_DW_SWAP |
7691 #endif
7692             (BP_PORT(sc) ? DMAE_CMD_PORT_1 : DMAE_CMD_PORT_0) |
7693             (BP_E1HVN(sc) << DMAE_CMD_E1HVN_SHIFT));
7694
7695         dmae = BXE_SP(sc, dmae[sc->executer_idx++]);
7696         dmae->opcode = (opcode | DMAE_CMD_C_DST_GRC);
7697         dmae->src_addr_lo = sc->port.port_stx >> 2;
7698         dmae->src_addr_hi = 0;
7699         dmae->dst_addr_lo = U64_LO(BXE_SP_MAPPING(sc, port_stats));
7700         dmae->dst_addr_hi = U64_HI(BXE_SP_MAPPING(sc, port_stats));
7701         dmae->len = DMAE_LEN32_RD_MAX;
7702         dmae->comp_addr_lo = dmae_reg_go_c[loader_idx] >> 2;
7703         dmae->comp_addr_hi = 0;
7704         dmae->comp_val = 1;
7705
7706         dmae = BXE_SP(sc, dmae[sc->executer_idx++]);
7707         dmae->opcode = (opcode | DMAE_CMD_C_DST_PCI);
7708         dmae->src_addr_lo = (sc->port.port_stx >> 2) + DMAE_LEN32_RD_MAX;
7709         dmae->src_addr_hi = 0;
7710         dmae->dst_addr_lo = U64_LO(BXE_SP_MAPPING(sc, port_stats) +
7711             DMAE_LEN32_RD_MAX * 4);
7712         dmae->dst_addr_hi = U64_HI(BXE_SP_MAPPING(sc, port_stats) +
7713             DMAE_LEN32_RD_MAX * 4);
7714         dmae->len = (sizeof(struct host_port_stats) >> 2) -
7715             DMAE_LEN32_RD_MAX;
7716         dmae->comp_addr_lo = U64_LO(BXE_SP_MAPPING(sc, stats_comp));
7717         dmae->comp_addr_hi = U64_HI(BXE_SP_MAPPING(sc, stats_comp));
7718         dmae->comp_val = DMAE_COMP_VAL;
7719
7720         /* Start the DMA and wait for the result. */
7721         *stats_comp = 0;
7722         bxe_stats_hw_post(sc);
7723         bxe_stats_comp(sc);
7724
7725 bxe_stats_pmf_update_exit:
7726         DBEXIT(BXE_VERBOSE_STATS);
7727 }
7728
7729 /*
7730  * Prepare the DMAE parameters required for all statistics.
7731  *
7732  * This function should only be called by the driver instance
7733  * that is designated as the port management function (PMF).
7734  *
7735  * Returns:
7736  *   None.
7737  */
7738 static void
7739 bxe_stats_port_init(struct bxe_softc *sc)
7740 {
7741         struct dmae_command *dmae;
7742         uint32_t mac_addr, opcode, *stats_comp;
7743         int loader_idx, port, vn;
7744
7745         DBENTER(BXE_VERBOSE_STATS);
7746
7747         port = BP_PORT(sc);
7748         vn = BP_E1HVN(sc);
7749         loader_idx = PMF_DMAE_C(sc);
7750         stats_comp = BXE_SP(sc, stats_comp);
7751
7752         /* Only the port management function (PMF) does this work. */
7753         if (!sc->link_vars.link_up || (sc->port.pmf == 0)) {
7754                 BXE_PRINTF("%s(%d): Invalid statistics port setup!\n",
7755                     __FILE__, __LINE__);
7756                 goto bxe_stats_port_init_exit;
7757         }
7758
7759         sc->executer_idx = 0;
7760
7761         /* The same opcde is used for multiple DMA operations. */
7762         opcode = (DMAE_CMD_SRC_PCI | DMAE_CMD_DST_GRC |
7763             DMAE_CMD_C_DST_GRC | DMAE_CMD_C_ENABLE |
7764             DMAE_CMD_SRC_RESET | DMAE_CMD_DST_RESET |
7765 #ifdef __BIG_ENDIAN
7766             DMAE_CMD_ENDIANITY_B_DW_SWAP |
7767 #else
7768             DMAE_CMD_ENDIANITY_DW_SWAP |
7769 #endif
7770             (port ? DMAE_CMD_PORT_1 : DMAE_CMD_PORT_0) |
7771             (vn << DMAE_CMD_E1HVN_SHIFT));
7772
7773         /* Setup the DMA for port statistics. */
7774         if (sc->port.port_stx) {
7775                 dmae = BXE_SP(sc, dmae[sc->executer_idx++]);
7776                 dmae->opcode = opcode;
7777                 dmae->src_addr_lo = U64_LO(BXE_SP_MAPPING(sc, port_stats));
7778                 dmae->src_addr_hi = U64_HI(BXE_SP_MAPPING(sc, port_stats));
7779                 dmae->dst_addr_lo = sc->port.port_stx >> 2;
7780                 dmae->dst_addr_hi = 0;
7781                 dmae->len = sizeof(struct host_port_stats) >> 2;
7782                 dmae->comp_addr_lo = dmae_reg_go_c[loader_idx] >> 2;
7783                 dmae->comp_addr_hi = 0;
7784                 dmae->comp_val = 1;
7785         }
7786
7787         /* Setup the DMA for function statistics. */
7788         if (sc->func_stx) {
7789                 dmae = BXE_SP(sc, dmae[sc->executer_idx++]);
7790                 dmae->opcode = opcode;
7791                 dmae->src_addr_lo = U64_LO(BXE_SP_MAPPING(sc, func_stats));
7792                 dmae->src_addr_hi = U64_HI(BXE_SP_MAPPING(sc, func_stats));
7793                 dmae->dst_addr_lo = sc->func_stx >> 2;
7794                 dmae->dst_addr_hi = 0;
7795                 dmae->len = sizeof(struct host_func_stats) >> 2;
7796                 dmae->comp_addr_lo = dmae_reg_go_c[loader_idx] >> 2;
7797                 dmae->comp_addr_hi = 0;
7798                 dmae->comp_val = 1;
7799         }
7800
7801         /* Setup statistics reporting for the MAC. */
7802         opcode = (DMAE_CMD_SRC_GRC | DMAE_CMD_DST_PCI |
7803             DMAE_CMD_C_DST_GRC | DMAE_CMD_C_ENABLE |
7804             DMAE_CMD_SRC_RESET | DMAE_CMD_DST_RESET |
7805 #ifdef __BIG_ENDIAN
7806             DMAE_CMD_ENDIANITY_B_DW_SWAP |
7807 #else
7808             DMAE_CMD_ENDIANITY_DW_SWAP |
7809 #endif
7810             (port ? DMAE_CMD_PORT_1 : DMAE_CMD_PORT_0) |
7811             (vn << DMAE_CMD_E1HVN_SHIFT));
7812
7813         if (sc->link_vars.mac_type == MAC_TYPE_BMAC) {
7814                 /* Enable statistics for the 10Gb BMAC. */
7815
7816                 mac_addr = (port ? NIG_REG_INGRESS_BMAC1_MEM :
7817                         NIG_REG_INGRESS_BMAC0_MEM);
7818
7819                 /* Setup BMAC TX statistics (TX_STAT_GTPKT .. TX_STAT_GTBYT). */
7820                 dmae = BXE_SP(sc, dmae[sc->executer_idx++]);
7821                 dmae->opcode = opcode;
7822                 dmae->src_addr_lo = (mac_addr +
7823                     BIGMAC_REGISTER_TX_STAT_GTPKT) >> 2;
7824                 dmae->src_addr_hi = 0;
7825                 dmae->dst_addr_lo = U64_LO(BXE_SP_MAPPING(sc, mac_stats));
7826                 dmae->dst_addr_hi = U64_HI(BXE_SP_MAPPING(sc, mac_stats));
7827                 dmae->len = (8 + BIGMAC_REGISTER_TX_STAT_GTBYT -
7828                     BIGMAC_REGISTER_TX_STAT_GTPKT) >> 2;
7829                 dmae->comp_addr_lo = dmae_reg_go_c[loader_idx] >> 2;
7830                 dmae->comp_addr_hi = 0;
7831                 dmae->comp_val = 1;
7832
7833                 /* Setup BMAC RX statistcs (RX_STAT_GR64 .. RX_STAT_GRIPJ). */
7834                 dmae = BXE_SP(sc, dmae[sc->executer_idx++]);
7835                 dmae->opcode = opcode;
7836                 dmae->src_addr_lo = (mac_addr +
7837                     BIGMAC_REGISTER_RX_STAT_GR64) >> 2;
7838                 dmae->src_addr_hi = 0;
7839                 dmae->dst_addr_lo = U64_LO(BXE_SP_MAPPING(sc, mac_stats) +
7840                     offsetof(struct bmac_stats, rx_stat_gr64_lo));
7841                 dmae->dst_addr_hi = U64_HI(BXE_SP_MAPPING(sc, mac_stats) +
7842                     offsetof(struct bmac_stats, rx_stat_gr64_lo));
7843                 dmae->len = (8 + BIGMAC_REGISTER_RX_STAT_GRIPJ -
7844                     BIGMAC_REGISTER_RX_STAT_GR64) >> 2;
7845                 dmae->comp_addr_lo = dmae_reg_go_c[loader_idx] >> 2;
7846                 dmae->comp_addr_hi = 0;
7847                 dmae->comp_val = 1;
7848
7849         } else if (sc->link_vars.mac_type == MAC_TYPE_EMAC) {
7850                 /* Enable statistics for the 1Gb EMAC. */
7851
7852                 mac_addr = (port ? GRCBASE_EMAC1 : GRCBASE_EMAC0);
7853
7854                 /* Setup EMAC RX statistics. */
7855                 dmae = BXE_SP(sc, dmae[sc->executer_idx++]);
7856                 dmae->opcode = opcode;
7857                 dmae->src_addr_lo = (mac_addr + EMAC_REG_EMAC_RX_STAT_AC) >> 2;
7858                 dmae->src_addr_hi = 0;
7859                 dmae->dst_addr_lo = U64_LO(BXE_SP_MAPPING(sc, mac_stats));
7860                 dmae->dst_addr_hi = U64_HI(BXE_SP_MAPPING(sc, mac_stats));
7861                 dmae->len = EMAC_REG_EMAC_RX_STAT_AC_COUNT;
7862                 dmae->comp_addr_lo = dmae_reg_go_c[loader_idx] >> 2;
7863                 dmae->comp_addr_hi = 0;
7864                 dmae->comp_val = 1;
7865
7866                 /* Setup additional EMAC RX statistics. */
7867                 dmae = BXE_SP(sc, dmae[sc->executer_idx++]);
7868                 dmae->opcode = opcode;
7869                 dmae->src_addr_lo = (mac_addr +
7870                     EMAC_REG_EMAC_RX_STAT_AC_28) >> 2;
7871                 dmae->src_addr_hi = 0;
7872                 dmae->dst_addr_lo = U64_LO(BXE_SP_MAPPING(sc, mac_stats) +
7873                     offsetof(struct emac_stats, rx_stat_falsecarriererrors));
7874                 dmae->dst_addr_hi = U64_HI(BXE_SP_MAPPING(sc, mac_stats) +
7875                     offsetof(struct emac_stats, rx_stat_falsecarriererrors));
7876                 dmae->len = 1;
7877                 dmae->comp_addr_lo = dmae_reg_go_c[loader_idx] >> 2;
7878                 dmae->comp_addr_hi = 0;
7879                 dmae->comp_val = 1;
7880
7881                 /* Setup EMAC TX statistics. */
7882                 dmae = BXE_SP(sc, dmae[sc->executer_idx++]);
7883                 dmae->opcode = opcode;
7884                 dmae->src_addr_lo = (mac_addr + EMAC_REG_EMAC_TX_STAT_AC) >> 2;
7885                 dmae->src_addr_hi = 0;
7886                 dmae->dst_addr_lo = U64_LO(BXE_SP_MAPPING(sc, mac_stats) +
7887                     offsetof(struct emac_stats, tx_stat_ifhcoutoctets));
7888                 dmae->dst_addr_hi = U64_HI(BXE_SP_MAPPING(sc, mac_stats) +
7889                     offsetof(struct emac_stats, tx_stat_ifhcoutoctets));
7890                 dmae->len = EMAC_REG_EMAC_TX_STAT_AC_COUNT;
7891                 dmae->comp_addr_lo = dmae_reg_go_c[loader_idx] >> 2;
7892                 dmae->comp_addr_hi = 0;
7893                 dmae->comp_val = 1;
7894         } else {
7895                 DBPRINT(sc, BXE_WARN, "%s(): Undefined MAC type.\n",
7896                     __FUNCTION__);
7897         }
7898
7899         /* Enable NIG statistics. */
7900         dmae = BXE_SP(sc, dmae[sc->executer_idx++]);
7901         dmae->opcode = opcode;
7902         dmae->src_addr_lo = (port ? NIG_REG_STAT1_BRB_DISCARD :
7903             NIG_REG_STAT0_BRB_DISCARD) >> 2;
7904         dmae->src_addr_hi = 0;
7905         dmae->dst_addr_lo = U64_LO(BXE_SP_MAPPING(sc, nig_stats));
7906         dmae->dst_addr_hi = U64_HI(BXE_SP_MAPPING(sc, nig_stats));
7907         dmae->len = (sizeof(struct nig_stats) - 4 * sizeof(uint32_t)) >> 2;
7908         dmae->comp_addr_lo = dmae_reg_go_c[loader_idx] >> 2;
7909         dmae->comp_addr_hi = 0;
7910         dmae->comp_val = 1;
7911
7912         dmae = BXE_SP(sc, dmae[sc->executer_idx++]);
7913         dmae->opcode = opcode;
7914         dmae->src_addr_lo = (port ? NIG_REG_STAT1_EGRESS_MAC_PKT0 :
7915             NIG_REG_STAT0_EGRESS_MAC_PKT0) >> 2;
7916         dmae->src_addr_hi = 0;
7917         dmae->dst_addr_lo = U64_LO(BXE_SP_MAPPING(sc, nig_stats) +
7918             offsetof(struct nig_stats, egress_mac_pkt0_lo));
7919         dmae->dst_addr_hi = U64_HI(BXE_SP_MAPPING(sc, nig_stats) +
7920             offsetof(struct nig_stats, egress_mac_pkt0_lo));
7921         dmae->len = (2 * sizeof(uint32_t)) >> 2;
7922         dmae->comp_addr_lo = dmae_reg_go_c[loader_idx] >> 2;
7923         dmae->comp_addr_hi = 0;
7924         dmae->comp_val = 1;
7925
7926         dmae = BXE_SP(sc, dmae[sc->executer_idx++]);
7927         dmae->opcode = (DMAE_CMD_SRC_GRC | DMAE_CMD_DST_PCI |
7928             DMAE_CMD_C_DST_PCI | DMAE_CMD_C_ENABLE |
7929             DMAE_CMD_SRC_RESET | DMAE_CMD_DST_RESET |
7930 #ifdef __BIG_ENDIAN
7931             DMAE_CMD_ENDIANITY_B_DW_SWAP |
7932 #else
7933             DMAE_CMD_ENDIANITY_DW_SWAP |
7934 #endif
7935             (port ? DMAE_CMD_PORT_1 : DMAE_CMD_PORT_0) |
7936             (vn << DMAE_CMD_E1HVN_SHIFT));
7937         dmae->src_addr_lo = (port ? NIG_REG_STAT1_EGRESS_MAC_PKT1 :
7938             NIG_REG_STAT0_EGRESS_MAC_PKT1) >> 2;
7939         dmae->src_addr_hi = 0;
7940         dmae->dst_addr_lo = U64_LO(BXE_SP_MAPPING(sc, nig_stats) +
7941             offsetof(struct nig_stats, egress_mac_pkt1_lo));
7942         dmae->dst_addr_hi = U64_HI(BXE_SP_MAPPING(sc, nig_stats) +
7943             offsetof(struct nig_stats, egress_mac_pkt1_lo));
7944         dmae->len = (2 * sizeof(uint32_t)) >> 2;
7945         dmae->comp_addr_lo = U64_LO(BXE_SP_MAPPING(sc, stats_comp));
7946         dmae->comp_addr_hi = U64_HI(BXE_SP_MAPPING(sc, stats_comp));
7947         dmae->comp_val = DMAE_COMP_VAL;
7948
7949         /* Clear the statistics completion value. */
7950         *stats_comp = 0;
7951
7952 bxe_stats_port_init_exit:
7953         DBEXIT(BXE_VERBOSE_STATS);
7954 }
7955
7956 /*
7957  * Prepare the DMAE parameters required for function statistics.
7958  *
7959  * This function is called by all driver instances.
7960  *
7961  * Returns:
7962  *   None.
7963  */
7964 static void
7965 bxe_stats_func_init(struct bxe_softc *sc)
7966 {
7967         struct dmae_command *dmae;
7968         uint32_t *stats_comp;
7969
7970         DBENTER(BXE_VERBOSE_STATS);
7971
7972         if (!sc->func_stx) {
7973                 BXE_PRINTF("%s(%d): Invalid statistics function setup!\n",
7974                      __FILE__, __LINE__);
7975                 goto bxe_stats_func_init_exit;
7976         }
7977
7978         dmae = &sc->stats_dmae;
7979         stats_comp = BXE_SP(sc, stats_comp);
7980         sc->executer_idx = 0;
7981         memset(dmae, 0, sizeof(struct dmae_command));
7982
7983         /* Setup the DMA for function statistics. */
7984         dmae->opcode = (DMAE_CMD_SRC_PCI | DMAE_CMD_DST_GRC |
7985             DMAE_CMD_C_DST_PCI | DMAE_CMD_C_ENABLE |
7986             DMAE_CMD_SRC_RESET | DMAE_CMD_DST_RESET |
7987 #ifdef __BIG_ENDIAN
7988             DMAE_CMD_ENDIANITY_B_DW_SWAP |
7989 #else
7990             DMAE_CMD_ENDIANITY_DW_SWAP |
7991 #endif
7992             (BP_PORT(sc) ? DMAE_CMD_PORT_1 : DMAE_CMD_PORT_0) |
7993             (BP_E1HVN(sc) << DMAE_CMD_E1HVN_SHIFT));
7994
7995         dmae->src_addr_lo = U64_LO(BXE_SP_MAPPING(sc, func_stats));
7996         dmae->src_addr_hi = U64_HI(BXE_SP_MAPPING(sc, func_stats));
7997         dmae->dst_addr_lo = sc->func_stx >> 2;
7998         dmae->dst_addr_hi = 0;
7999         dmae->len = sizeof(struct host_func_stats) >> 2;
8000         dmae->comp_addr_lo = U64_LO(BXE_SP_MAPPING(sc, stats_comp));
8001         dmae->comp_addr_hi = U64_HI(BXE_SP_MAPPING(sc, stats_comp));
8002         dmae->comp_val = DMAE_COMP_VAL;
8003
8004         *stats_comp = 0;
8005
8006 bxe_stats_func_init_exit:
8007         DBEXIT(BXE_VERBOSE_STATS);
8008 }
8009
8010 /*
8011  * Starts a statistics update DMA and waits for completion.
8012  *
8013  * Returns:
8014  *   None.
8015  */
8016 static void
8017 bxe_stats_start(struct bxe_softc *sc)
8018 {
8019
8020         DBENTER(BXE_VERBOSE_STATS);
8021
8022         if (sc->port.pmf == 1)
8023                 bxe_stats_port_init(sc);
8024         else if (sc->func_stx)
8025                 bxe_stats_func_init(sc);
8026
8027         bxe_stats_hw_post(sc);
8028         bxe_stats_storm_post(sc);
8029
8030         DBEXIT(BXE_VERBOSE_STATS);
8031 }
8032
8033 /*
8034  * Returns:
8035  *   None.
8036  */
8037 static void
8038 bxe_stats_pmf_start(struct bxe_softc *sc)
8039 {
8040         DBENTER(BXE_VERBOSE_STATS);
8041
8042         bxe_stats_comp(sc);
8043         bxe_stats_pmf_update(sc);
8044         bxe_stats_start(sc);
8045
8046         DBEXIT(BXE_VERBOSE_STATS);
8047 }
8048
8049 /*
8050  * Returns:
8051  *   None.
8052  */
8053 static void
8054 bxe_stats_restart(struct bxe_softc *sc)
8055 {
8056
8057         DBENTER(BXE_VERBOSE_STATS);
8058
8059         bxe_stats_comp(sc);
8060         bxe_stats_start(sc);
8061
8062         DBEXIT(BXE_VERBOSE_STATS);
8063 }
8064
8065 /*
8066  * Update the Big MAC (10Gb BMAC) statistics.
8067  *
8068  * Returns:
8069  *   None.
8070  */
8071 static void
8072 bxe_stats_bmac_update(struct bxe_softc *sc)
8073 {
8074         struct bmac_stats *new;
8075         struct host_port_stats *pstats;
8076         struct bxe_port_stats *estats;
8077         struct regpair diff;
8078
8079         DBENTER(BXE_INSANE_STATS);
8080
8081         new = BXE_SP(sc, mac_stats.bmac_stats);
8082         pstats = BXE_SP(sc, port_stats);
8083         estats = &sc->eth_stats;
8084
8085         UPDATE_STAT64(rx_stat_grerb,
8086             rx_stat_ifhcinbadoctets);
8087         UPDATE_STAT64(rx_stat_grfcs,
8088             rx_stat_dot3statsfcserrors);
8089         UPDATE_STAT64(rx_stat_grund,
8090             rx_stat_etherstatsundersizepkts);
8091         UPDATE_STAT64(rx_stat_grovr,
8092             rx_stat_dot3statsframestoolong);
8093         UPDATE_STAT64(rx_stat_grfrg,
8094             rx_stat_etherstatsfragments);
8095         UPDATE_STAT64(rx_stat_grjbr,
8096             rx_stat_etherstatsjabbers);
8097         UPDATE_STAT64(rx_stat_grxcf,
8098             rx_stat_maccontrolframesreceived);
8099         UPDATE_STAT64(rx_stat_grxpf,
8100             rx_stat_xoffstateentered);
8101         UPDATE_STAT64(rx_stat_grxpf,
8102             rx_stat_bmac_xpf);
8103         UPDATE_STAT64(tx_stat_gtxpf,
8104             tx_stat_outxoffsent);
8105         UPDATE_STAT64(tx_stat_gtxpf,
8106             tx_stat_flowcontroldone);
8107         UPDATE_STAT64(tx_stat_gt64,
8108             tx_stat_etherstatspkts64octets);
8109         UPDATE_STAT64(tx_stat_gt127,
8110             tx_stat_etherstatspkts65octetsto127octets);
8111         UPDATE_STAT64(tx_stat_gt255,
8112             tx_stat_etherstatspkts128octetsto255octets);
8113         UPDATE_STAT64(tx_stat_gt511,
8114             tx_stat_etherstatspkts256octetsto511octets);
8115         UPDATE_STAT64(tx_stat_gt1023,
8116             tx_stat_etherstatspkts512octetsto1023octets);
8117         UPDATE_STAT64(tx_stat_gt1518,
8118             tx_stat_etherstatspkts1024octetsto1522octets);
8119         UPDATE_STAT64(tx_stat_gt2047,
8120             tx_stat_bmac_2047);
8121         UPDATE_STAT64(tx_stat_gt4095,
8122             tx_stat_bmac_4095);
8123         UPDATE_STAT64(tx_stat_gt9216,
8124             tx_stat_bmac_9216);
8125         UPDATE_STAT64(tx_stat_gt16383,
8126             tx_stat_bmac_16383);
8127         UPDATE_STAT64(tx_stat_gterr,
8128             tx_stat_dot3statsinternalmactransmiterrors);
8129         UPDATE_STAT64(tx_stat_gtufl,
8130             tx_stat_bmac_ufl);
8131
8132         estats->pause_frames_received_hi =
8133             pstats->mac_stx[1].rx_stat_bmac_xpf_hi;
8134         estats->pause_frames_received_lo =
8135             pstats->mac_stx[1].rx_stat_bmac_xpf_lo;
8136         estats->pause_frames_sent_hi =
8137             pstats->mac_stx[1].tx_stat_outxoffsent_hi;
8138         estats->pause_frames_sent_lo =
8139             pstats->mac_stx[1].tx_stat_outxoffsent_lo;
8140
8141         DBEXIT(BXE_INSANE_STATS);
8142 }
8143
8144 /*
8145  * Update the Ethernet MAC (1Gb EMAC) statistics.
8146  *
8147  * Returns:
8148  *   None.
8149  */
8150 static void
8151 bxe_stats_emac_update(struct bxe_softc *sc)
8152 {
8153         struct emac_stats *new;
8154         struct host_port_stats *pstats;
8155         struct bxe_port_stats *estats;
8156
8157         DBENTER(BXE_INSANE_STATS);
8158
8159         new = BXE_SP(sc, mac_stats.emac_stats);
8160         pstats = BXE_SP(sc, port_stats);
8161         estats = &sc->eth_stats;
8162
8163         UPDATE_EXTEND_STAT(rx_stat_ifhcinbadoctets);
8164         UPDATE_EXTEND_STAT(tx_stat_ifhcoutbadoctets);
8165         UPDATE_EXTEND_STAT(rx_stat_dot3statsfcserrors);
8166         UPDATE_EXTEND_STAT(rx_stat_dot3statsalignmenterrors);
8167         UPDATE_EXTEND_STAT(rx_stat_dot3statscarriersenseerrors);
8168         UPDATE_EXTEND_STAT(rx_stat_falsecarriererrors);
8169         UPDATE_EXTEND_STAT(rx_stat_etherstatsundersizepkts);
8170         UPDATE_EXTEND_STAT(rx_stat_dot3statsframestoolong);
8171         UPDATE_EXTEND_STAT(rx_stat_etherstatsfragments);
8172         UPDATE_EXTEND_STAT(rx_stat_etherstatsjabbers);
8173         UPDATE_EXTEND_STAT(rx_stat_maccontrolframesreceived);
8174         UPDATE_EXTEND_STAT(rx_stat_xoffstateentered);
8175         UPDATE_EXTEND_STAT(rx_stat_xonpauseframesreceived);
8176         UPDATE_EXTEND_STAT(rx_stat_xoffpauseframesreceived);
8177         UPDATE_EXTEND_STAT(tx_stat_outxonsent);
8178         UPDATE_EXTEND_STAT(tx_stat_outxoffsent);
8179         UPDATE_EXTEND_STAT(tx_stat_flowcontroldone);
8180         UPDATE_EXTEND_STAT(tx_stat_etherstatscollisions);
8181         UPDATE_EXTEND_STAT(tx_stat_dot3statssinglecollisionframes);
8182         UPDATE_EXTEND_STAT(tx_stat_dot3statsmultiplecollisionframes);
8183         UPDATE_EXTEND_STAT(tx_stat_dot3statsdeferredtransmissions);
8184         UPDATE_EXTEND_STAT(tx_stat_dot3statsexcessivecollisions);
8185         UPDATE_EXTEND_STAT(tx_stat_dot3statslatecollisions);
8186         UPDATE_EXTEND_STAT(tx_stat_etherstatspkts64octets);
8187         UPDATE_EXTEND_STAT(tx_stat_etherstatspkts65octetsto127octets);
8188         UPDATE_EXTEND_STAT(tx_stat_etherstatspkts128octetsto255octets);
8189         UPDATE_EXTEND_STAT(tx_stat_etherstatspkts256octetsto511octets);
8190         UPDATE_EXTEND_STAT(tx_stat_etherstatspkts512octetsto1023octets);
8191         UPDATE_EXTEND_STAT(tx_stat_etherstatspkts1024octetsto1522octets);
8192         UPDATE_EXTEND_STAT(tx_stat_etherstatspktsover1522octets);
8193         UPDATE_EXTEND_STAT(tx_stat_dot3statsinternalmactransmiterrors);
8194
8195         estats->pause_frames_received_hi =
8196             pstats->mac_stx[1].rx_stat_xonpauseframesreceived_hi;
8197         estats->pause_frames_received_lo =
8198             pstats->mac_stx[1].rx_stat_xonpauseframesreceived_lo;
8199         ADD_64(estats->pause_frames_received_hi,
8200             pstats->mac_stx[1].rx_stat_xoffpauseframesreceived_hi,
8201             estats->pause_frames_received_lo,
8202             pstats->mac_stx[1].rx_stat_xoffpauseframesreceived_lo);
8203
8204         estats->pause_frames_sent_hi =
8205             pstats->mac_stx[1].tx_stat_outxonsent_hi;
8206         estats->pause_frames_sent_lo =
8207             pstats->mac_stx[1].tx_stat_outxonsent_lo;
8208         ADD_64(estats->pause_frames_sent_hi,
8209             pstats->mac_stx[1].tx_stat_outxoffsent_hi,
8210             estats->pause_frames_sent_lo,
8211             pstats->mac_stx[1].tx_stat_outxoffsent_lo);
8212
8213         DBEXIT(BXE_INSANE_STATS);
8214 }
8215
8216 /*
8217  * Returns:
8218  *   0 = Success, !0 = Failure.
8219  */
8220 static int
8221 bxe_stats_hw_update(struct bxe_softc *sc)
8222 {
8223         struct nig_stats *new, *old;
8224         struct host_port_stats *pstats;
8225         struct bxe_port_stats *estats;
8226         struct regpair diff;
8227         uint32_t nig_timer_max;
8228         int rc;
8229
8230         DBENTER(BXE_INSANE_STATS);
8231
8232         rc = 0;
8233         new = BXE_SP(sc, nig_stats);
8234         old = &(sc->port.old_nig_stats);
8235         pstats = BXE_SP(sc, port_stats);
8236         estats = &sc->eth_stats;
8237
8238         /* Update statistics for the active MAC. */
8239         if (sc->link_vars.mac_type == MAC_TYPE_BMAC)
8240                 bxe_stats_bmac_update(sc);
8241         else if (sc->link_vars.mac_type == MAC_TYPE_EMAC)
8242                 bxe_stats_emac_update(sc);
8243         else {
8244                 DBPRINT(sc, BXE_WARN,
8245                     "%s(): Statistics updated by DMAE but no MAC is active!\n",
8246                     __FUNCTION__);
8247                 rc = EINVAL;
8248                 goto bxe_stats_hw_update_exit;
8249         }
8250
8251         /* Now update the hardware (NIG) statistics. */
8252         ADD_EXTEND_64(pstats->brb_drop_hi, pstats->brb_drop_lo,
8253             new->brb_discard - old->brb_discard);
8254         ADD_EXTEND_64(estats->brb_truncate_hi, estats->brb_truncate_lo,
8255             new->brb_truncate - old->brb_truncate);
8256
8257         UPDATE_STAT64_NIG(egress_mac_pkt0,
8258             etherstatspkts1024octetsto1522octets);
8259         UPDATE_STAT64_NIG(egress_mac_pkt1, etherstatspktsover1522octets);
8260
8261         memcpy(old, new, sizeof(struct nig_stats));
8262
8263         memcpy(&(estats->rx_stat_ifhcinbadoctets_hi), &(pstats->mac_stx[1]),
8264             sizeof(struct mac_stx));
8265         estats->brb_drop_hi = pstats->brb_drop_hi;
8266         estats->brb_drop_lo = pstats->brb_drop_lo;
8267
8268         pstats->host_port_stats_start = ++pstats->host_port_stats_end;
8269
8270         if (!NOMCP(sc)) {
8271                 nig_timer_max =
8272                     SHMEM_RD(sc, port_mb[BP_PORT(sc)].stat_nig_timer);
8273                 if (nig_timer_max != estats->nig_timer_max) {
8274                         estats->nig_timer_max = nig_timer_max;
8275                         DBPRINT(sc, BXE_WARN,
8276                             "%s(): NIG timer reached max value (%u)!\n",
8277                             __FUNCTION__, estats->nig_timer_max);
8278                 }
8279         }
8280
8281 bxe_stats_hw_update_exit:
8282         DBEXIT(BXE_INSANE_STATS);
8283         return (rc);
8284 }
8285
8286 /*
8287  * Returns:
8288  *   0 = Success, !0 = Failure.
8289  */
8290 // DRC - Done
8291 static int
8292 bxe_stats_storm_update(struct bxe_softc *sc)
8293 {
8294         int rc, i, cl_id;
8295         struct eth_stats_query *stats;
8296         struct bxe_port_stats *estats;
8297         struct host_func_stats *fstats;
8298         struct bxe_q_stats *qstats;
8299         struct tstorm_per_port_stats *tport;
8300         struct tstorm_per_client_stats *tclient;
8301         struct ustorm_per_client_stats *uclient;
8302         struct xstorm_per_client_stats *xclient;
8303         struct tstorm_per_client_stats *old_tclient;
8304         struct ustorm_per_client_stats *old_uclient;
8305         struct xstorm_per_client_stats *old_xclient;
8306         struct bxe_fastpath * fp;
8307         uint32_t diff;
8308
8309         DBENTER(BXE_INSANE_STATS);
8310
8311         rc = 0;
8312         diff = 0;
8313         stats = BXE_SP(sc, fw_stats);
8314         tport = &stats->tstorm_common.port_statistics;
8315         fstats = BXE_SP(sc, func_stats);
8316
8317         memcpy(&(fstats->total_bytes_received_hi),
8318             &(BXE_SP(sc, func_stats_base)->total_bytes_received_hi),
8319             sizeof(struct host_func_stats) - 2 * sizeof(uint32_t));
8320
8321         estats = &sc->eth_stats;
8322         estats->no_buff_discard_hi = 0;
8323         estats->no_buff_discard_lo = 0;
8324         estats->error_bytes_received_hi = 0;
8325         estats->error_bytes_received_lo = 0;
8326         estats->etherstatsoverrsizepkts_hi = 0;
8327         estats->etherstatsoverrsizepkts_lo = 0;
8328
8329         for (i = 0; i < sc->num_queues; i++) {
8330                 fp = &sc->fp[i];
8331                 cl_id = fp->cl_id;
8332                 tclient = &stats->tstorm_common.client_statistics[cl_id];
8333                 old_tclient = &fp->old_tclient;
8334                 uclient = &stats->ustorm_common.client_statistics[cl_id];
8335                 old_uclient = &fp->old_uclient;
8336                 xclient = &stats->xstorm_common.client_statistics[cl_id];
8337                 old_xclient = &fp->old_xclient;
8338                 qstats = &fp->eth_q_stats;
8339
8340                 /* Are TSTORM statistics valid? */
8341                 if ((uint16_t)(le16toh(tclient->stats_counter) + 1) !=
8342                     sc->stats_counter) {
8343                         DBPRINT(sc, BXE_WARN, "%s(): Stats not updated by TSTORM "
8344                             "(tstorm counter (%d) != stats_counter (%d))!\n",
8345                             __FUNCTION__, tclient->stats_counter, sc->stats_counter);
8346                         rc = 1;
8347                         goto bxe_stats_storm_update_exit;
8348                 }
8349
8350                 /* Are USTORM statistics valid? */
8351                 if ((uint16_t)(le16toh(uclient->stats_counter) + 1) !=
8352                     sc->stats_counter) {
8353                         DBPRINT(sc, BXE_WARN, "%s(): Stats not updated by USTORM "
8354                             "(ustorm counter (%d) != stats_counter (%d))!\n",
8355                             __FUNCTION__, uclient->stats_counter, sc->stats_counter);
8356                         rc = 2;
8357                         goto bxe_stats_storm_update_exit;
8358                 }
8359
8360                 /* Are XSTORM statistics valid? */
8361                 if ((uint16_t)(le16toh(xclient->stats_counter) + 1) !=
8362                         sc->stats_counter) {
8363                         DBPRINT(sc, BXE_WARN, "%s(): Stats not updated by XSTORM "
8364                             "(xstorm counter (%d) != stats_counter (%d))!\n",
8365                             __FUNCTION__, xclient->stats_counter, sc->stats_counter);
8366                         rc = 3;
8367                         goto bxe_stats_storm_update_exit;
8368                 }
8369
8370                 qstats->total_bytes_received_hi =
8371                     (tclient->rcv_broadcast_bytes.hi);
8372                 qstats->total_bytes_received_lo =
8373                     le32toh(tclient->rcv_broadcast_bytes.lo);
8374
8375                 ADD_64(qstats->total_bytes_received_hi,
8376                     le32toh(tclient->rcv_multicast_bytes.hi),
8377                     qstats->total_bytes_received_lo,
8378                     le32toh(tclient->rcv_multicast_bytes.lo));
8379
8380                 ADD_64(qstats->total_bytes_received_hi,
8381                     le32toh(tclient->rcv_unicast_bytes.hi),
8382                     qstats->total_bytes_received_lo,
8383                     le32toh(tclient->rcv_unicast_bytes.lo));
8384
8385                 SUB_64(qstats->total_bytes_received_hi,
8386                     le32toh(uclient->bcast_no_buff_bytes.hi),
8387                     qstats->total_bytes_received_lo,
8388                     le32toh(uclient->bcast_no_buff_bytes.lo));
8389
8390                 SUB_64(qstats->total_bytes_received_hi,
8391                     le32toh(uclient->mcast_no_buff_bytes.hi),
8392                     qstats->total_bytes_received_lo,
8393                     le32toh(uclient->mcast_no_buff_bytes.lo));
8394
8395                 SUB_64(qstats->total_bytes_received_hi,
8396                     le32toh(uclient->ucast_no_buff_bytes.hi),
8397                     qstats->total_bytes_received_lo,
8398                     le32toh(uclient->ucast_no_buff_bytes.lo));
8399
8400                 qstats->valid_bytes_received_hi =
8401                     qstats->total_bytes_received_hi;
8402                 qstats->valid_bytes_received_lo =
8403                     qstats->total_bytes_received_lo;
8404
8405                 qstats->error_bytes_received_hi =
8406                     le32toh(tclient->rcv_error_bytes.hi);
8407                 qstats->error_bytes_received_lo =
8408                     le32toh(tclient->rcv_error_bytes.lo);
8409
8410                 ADD_64(qstats->total_bytes_received_hi,
8411                     qstats->error_bytes_received_hi,
8412                     qstats->total_bytes_received_lo,
8413                     qstats->error_bytes_received_lo);
8414
8415                 UPDATE_EXTEND_TSTAT(rcv_unicast_pkts,
8416                     total_unicast_packets_received);
8417                 UPDATE_EXTEND_TSTAT(rcv_multicast_pkts,
8418                     total_multicast_packets_received);
8419                 UPDATE_EXTEND_TSTAT(rcv_broadcast_pkts,
8420                     total_broadcast_packets_received);
8421                 UPDATE_EXTEND_TSTAT(packets_too_big_discard,
8422                     etherstatsoverrsizepkts);
8423                 UPDATE_EXTEND_TSTAT(no_buff_discard, no_buff_discard);
8424
8425                 SUB_EXTEND_USTAT(ucast_no_buff_pkts,
8426                     total_unicast_packets_received);
8427                 SUB_EXTEND_USTAT(mcast_no_buff_pkts,
8428                     total_multicast_packets_received);
8429                 SUB_EXTEND_USTAT(bcast_no_buff_pkts,
8430                     total_broadcast_packets_received);
8431                 UPDATE_EXTEND_USTAT(ucast_no_buff_pkts, no_buff_discard);
8432                 UPDATE_EXTEND_USTAT(mcast_no_buff_pkts, no_buff_discard);
8433                 UPDATE_EXTEND_USTAT(bcast_no_buff_pkts, no_buff_discard);
8434
8435                 qstats->total_bytes_transmitted_hi =
8436                     le32toh(xclient->unicast_bytes_sent.hi);
8437                 qstats->total_bytes_transmitted_lo =
8438                     le32toh(xclient->unicast_bytes_sent.lo);
8439
8440                 ADD_64(qstats->total_bytes_transmitted_hi,
8441                     le32toh(xclient->multicast_bytes_sent.hi),
8442                     qstats->total_bytes_transmitted_lo,
8443                     le32toh(xclient->multicast_bytes_sent.lo));
8444
8445                 ADD_64(qstats->total_bytes_transmitted_hi,
8446                     le32toh(xclient->broadcast_bytes_sent.hi),
8447                     qstats->total_bytes_transmitted_lo,
8448                     le32toh(xclient->broadcast_bytes_sent.lo));
8449
8450                 UPDATE_EXTEND_XSTAT(unicast_pkts_sent,
8451                     total_unicast_packets_transmitted);
8452
8453                 UPDATE_EXTEND_XSTAT(multicast_pkts_sent,
8454                     total_multicast_packets_transmitted);
8455
8456                 UPDATE_EXTEND_XSTAT(broadcast_pkts_sent,
8457                     total_broadcast_packets_transmitted);
8458
8459                 old_tclient->checksum_discard = tclient->checksum_discard;
8460                 old_tclient->ttl0_discard = tclient->ttl0_discard;
8461
8462                 ADD_64(fstats->total_bytes_received_hi,
8463                        qstats->total_bytes_received_hi,
8464                        fstats->total_bytes_received_lo,
8465                        qstats->total_bytes_received_lo);
8466                 ADD_64(fstats->total_bytes_transmitted_hi,
8467                        qstats->total_bytes_transmitted_hi,
8468                        fstats->total_bytes_transmitted_lo,
8469                        qstats->total_bytes_transmitted_lo);
8470                 ADD_64(fstats->total_unicast_packets_received_hi,
8471                        qstats->total_unicast_packets_received_hi,
8472                        fstats->total_unicast_packets_received_lo,
8473                        qstats->total_unicast_packets_received_lo);
8474                 ADD_64(fstats->total_multicast_packets_received_hi,
8475                        qstats->total_multicast_packets_received_hi,
8476                        fstats->total_multicast_packets_received_lo,
8477                        qstats->total_multicast_packets_received_lo);
8478                 ADD_64(fstats->total_broadcast_packets_received_hi,
8479                        qstats->total_broadcast_packets_received_hi,
8480                        fstats->total_broadcast_packets_received_lo,
8481                        qstats->total_broadcast_packets_received_lo);
8482                 ADD_64(fstats->total_unicast_packets_transmitted_hi,
8483                        qstats->total_unicast_packets_transmitted_hi,
8484                        fstats->total_unicast_packets_transmitted_lo,
8485                        qstats->total_unicast_packets_transmitted_lo);
8486                 ADD_64(fstats->total_multicast_packets_transmitted_hi,
8487                        qstats->total_multicast_packets_transmitted_hi,
8488                        fstats->total_multicast_packets_transmitted_lo,
8489                        qstats->total_multicast_packets_transmitted_lo);
8490                 ADD_64(fstats->total_broadcast_packets_transmitted_hi,
8491                        qstats->total_broadcast_packets_transmitted_hi,
8492                        fstats->total_broadcast_packets_transmitted_lo,
8493                        qstats->total_broadcast_packets_transmitted_lo);
8494                 ADD_64(fstats->valid_bytes_received_hi,
8495                        qstats->valid_bytes_received_hi,
8496                        fstats->valid_bytes_received_lo,
8497                        qstats->valid_bytes_received_lo);
8498
8499                 ADD_64(estats->error_bytes_received_hi,
8500                        qstats->error_bytes_received_hi,
8501                        estats->error_bytes_received_lo,
8502                        qstats->error_bytes_received_lo);
8503                 ADD_64(estats->etherstatsoverrsizepkts_hi,
8504                        qstats->etherstatsoverrsizepkts_hi,
8505                        estats->etherstatsoverrsizepkts_lo,
8506                        qstats->etherstatsoverrsizepkts_lo);
8507                 ADD_64(estats->no_buff_discard_hi,
8508                        qstats->no_buff_discard_hi,
8509                        estats->no_buff_discard_lo,
8510                        qstats->no_buff_discard_lo);
8511         }
8512
8513         ADD_64(fstats->total_bytes_received_hi,
8514                estats->rx_stat_ifhcinbadoctets_hi,
8515                fstats->total_bytes_received_lo,
8516                estats->rx_stat_ifhcinbadoctets_lo);
8517
8518         memcpy(estats, &(fstats->total_bytes_received_hi),
8519             sizeof(struct host_func_stats) - 2 * sizeof(uint32_t));
8520
8521         ADD_64(estats->etherstatsoverrsizepkts_hi,
8522                estats->rx_stat_dot3statsframestoolong_hi,
8523                estats->etherstatsoverrsizepkts_lo,
8524                estats->rx_stat_dot3statsframestoolong_lo);
8525         ADD_64(estats->error_bytes_received_hi,
8526                estats->rx_stat_ifhcinbadoctets_hi,
8527                estats->error_bytes_received_lo,
8528                estats->rx_stat_ifhcinbadoctets_lo);
8529
8530         if (sc->port.pmf) {
8531                 estats->mac_filter_discard =
8532                     le32toh(tport->mac_filter_discard);
8533                 estats->xxoverflow_discard =
8534                     le32toh(tport->xxoverflow_discard);
8535                 estats->brb_truncate_discard =
8536                     le32toh(tport->brb_truncate_discard);
8537                 estats->mac_discard = le32toh(tport->mac_discard);
8538         }
8539
8540         fstats->host_func_stats_start = ++fstats->host_func_stats_end;
8541
8542         sc->stats_pending = 0;
8543
8544 bxe_stats_storm_update_exit:
8545
8546         DBEXIT(BXE_INSANE_STATS);
8547         return (rc);
8548 }
8549
8550 /*
8551  * Copy the controller maintained statistics over to the OS.
8552  *
8553  * Returns:
8554  *   None.
8555  */
8556 static void
8557 bxe_stats_net_update(struct bxe_softc *sc)
8558 {
8559         struct tstorm_per_client_stats *old_tclient;
8560         struct bxe_port_stats *estats;
8561         struct ifnet *ifp;
8562
8563         DBENTER(BXE_INSANE_STATS);
8564
8565         old_tclient = &sc->fp[0].old_tclient;
8566         estats = &sc->eth_stats;
8567         ifp = sc->bxe_ifp;
8568
8569         /*
8570          * Update the OS interface statistics from
8571          * the hardware statistics.
8572          */
8573
8574         ifp->if_collisions =
8575             (u_long) estats->tx_stat_dot3statssinglecollisionframes_lo +
8576             (u_long) estats->tx_stat_dot3statsmultiplecollisionframes_lo +
8577             (u_long) estats->tx_stat_dot3statslatecollisions_lo +
8578             (u_long) estats->tx_stat_dot3statsexcessivecollisions_lo;
8579
8580         ifp->if_ierrors =
8581             (u_long) old_tclient->checksum_discard +
8582             (u_long) estats->no_buff_discard_lo +
8583             (u_long) estats->mac_discard +
8584             (u_long) estats->rx_stat_etherstatsundersizepkts_lo +
8585             (u_long) estats->brb_drop_lo +
8586             (u_long) estats->brb_truncate_discard +
8587             (u_long) estats->rx_stat_dot3statsfcserrors_lo +
8588             (u_long) estats->rx_stat_dot3statsalignmenterrors_lo +
8589             (u_long) estats->xxoverflow_discard;
8590
8591         ifp->if_oerrors =
8592             (u_long) estats->tx_stat_dot3statslatecollisions_lo +
8593             (u_long) estats->tx_stat_dot3statsexcessivecollisions_lo +
8594             (u_long) estats->tx_stat_dot3statsinternalmactransmiterrors_lo;
8595
8596         ifp->if_ipackets =
8597             bxe_hilo(&estats->total_unicast_packets_received_hi) +
8598             bxe_hilo(&estats->total_multicast_packets_received_hi) +
8599             bxe_hilo(&estats->total_broadcast_packets_received_hi);
8600
8601         ifp->if_opackets =
8602             bxe_hilo(&estats->total_unicast_packets_transmitted_hi) +
8603             bxe_hilo(&estats->total_multicast_packets_transmitted_hi) +
8604             bxe_hilo(&estats->total_broadcast_packets_transmitted_hi);
8605
8606         DBEXIT(BXE_INSANE_STATS);
8607 }
8608
8609 /*
8610  *
8611  * Returns:
8612  *   None.
8613  */
8614 static void
8615 bxe_stats_update(struct bxe_softc *sc)
8616 {
8617         uint32_t *stats_comp;
8618         int update;
8619
8620         DBENTER(BXE_INSANE_STATS);
8621
8622         stats_comp = BXE_SP(sc, stats_comp);
8623         update = 0;
8624
8625         /* Make sure the statistics DMAE update has completed. */
8626         if (*stats_comp != DMAE_COMP_VAL)
8627                 goto bxe_stats_update_exit;
8628
8629         /* Check for any hardware statistics updates. */
8630         if (sc->port.pmf == 1)
8631                 update = (bxe_stats_hw_update(sc) == 0);
8632
8633         /* Check for any STORM statistics updates. */
8634         update |= (bxe_stats_storm_update(sc) == 0);
8635
8636         /* If we got updated hardware statistics then update the OS. */
8637         if (update)
8638                 bxe_stats_net_update(sc);
8639         else {
8640                 /* Check if any statistics updates are pending. */
8641                 if (sc->stats_pending) {
8642                         /* The update hasn't completed, keep waiting. */
8643                         sc->stats_pending++;
8644
8645                         /* Have we been waiting for too long? */
8646                         if (sc->stats_pending >= 3) {
8647                                 BXE_PRINTF(
8648                                     "%s(%d): Failed to get statistics after "
8649                                     "3 tries!\n", __FILE__, __LINE__);
8650                                 bxe_panic_dump(sc);
8651                                 goto bxe_stats_update_exit;
8652                         }
8653                 }
8654         }
8655
8656         /* Kickoff the next statistics request. */
8657         bxe_stats_hw_post(sc);
8658         bxe_stats_storm_post(sc);
8659
8660 bxe_stats_update_exit:
8661         DBEXIT(BXE_INSANE_STATS);
8662 }
8663
8664 /*
8665  *
8666  * Returns:
8667  *   None.
8668  */
8669 static void
8670 bxe_stats_port_stop(struct bxe_softc *sc)
8671 {
8672         struct dmae_command *dmae;
8673         uint32_t opcode, *stats_comp;
8674         int loader_idx;
8675
8676         DBENTER(BXE_VERBOSE_STATS);
8677
8678         stats_comp = BXE_SP(sc, stats_comp);
8679         loader_idx = PMF_DMAE_C(sc);
8680         sc->executer_idx = 0;
8681
8682         opcode = (DMAE_CMD_SRC_PCI | DMAE_CMD_DST_GRC |
8683             DMAE_CMD_C_ENABLE |
8684             DMAE_CMD_SRC_RESET | DMAE_CMD_DST_RESET |
8685 #ifdef __BIG_ENDIAN
8686             DMAE_CMD_ENDIANITY_B_DW_SWAP |
8687 #else
8688             DMAE_CMD_ENDIANITY_DW_SWAP |
8689 #endif
8690             (BP_PORT(sc) ? DMAE_CMD_PORT_1 : DMAE_CMD_PORT_0) |
8691             (BP_E1HVN(sc) << DMAE_CMD_E1HVN_SHIFT));
8692
8693         if (sc->port.port_stx) {
8694                 dmae = BXE_SP(sc, dmae[sc->executer_idx++]);
8695
8696                 if (sc->func_stx)
8697                         dmae->opcode = (opcode | DMAE_CMD_C_DST_GRC);
8698                 else
8699                         dmae->opcode = (opcode | DMAE_CMD_C_DST_PCI);
8700
8701                 dmae->src_addr_lo = U64_LO(BXE_SP_MAPPING(sc, port_stats));
8702                 dmae->src_addr_hi = U64_HI(BXE_SP_MAPPING(sc, port_stats));
8703                 dmae->dst_addr_lo = sc->port.port_stx >> 2;
8704                 dmae->dst_addr_hi = 0;
8705                 dmae->len = sizeof(struct host_port_stats) >> 2;
8706
8707                 if (sc->func_stx) {
8708                         dmae->comp_addr_lo = dmae_reg_go_c[loader_idx] >> 2;
8709                         dmae->comp_addr_hi = 0;
8710                         dmae->comp_val = 1;
8711                 } else {
8712                         dmae->comp_addr_lo = U64_LO(BXE_SP_MAPPING(sc,
8713                             stats_comp));
8714                         dmae->comp_addr_hi = U64_HI(BXE_SP_MAPPING(sc,
8715                             stats_comp));
8716                         dmae->comp_val = DMAE_COMP_VAL;
8717
8718                         *stats_comp = 0;
8719                 }
8720         }
8721
8722         if (sc->func_stx) {
8723                 dmae = BXE_SP(sc, dmae[sc->executer_idx++]);
8724                 dmae->opcode = (opcode | DMAE_CMD_C_DST_PCI);
8725                 dmae->src_addr_lo = U64_LO(BXE_SP_MAPPING(sc, func_stats));
8726                 dmae->src_addr_hi = U64_HI(BXE_SP_MAPPING(sc, func_stats));
8727                 dmae->dst_addr_lo = sc->func_stx >> 2;
8728                 dmae->dst_addr_hi = 0;
8729                 dmae->len = sizeof(struct host_func_stats) >> 2;
8730                 dmae->comp_addr_lo = U64_LO(BXE_SP_MAPPING(sc, stats_comp));
8731                 dmae->comp_addr_hi = U64_HI(BXE_SP_MAPPING(sc, stats_comp));
8732                 dmae->comp_val = DMAE_COMP_VAL;
8733
8734                 *stats_comp = 0;
8735         }
8736
8737         DBEXIT(BXE_VERBOSE_STATS);
8738 }
8739
8740 /*
8741  * Returns:
8742  *   None.
8743  */
8744 static void
8745 bxe_stats_stop(struct bxe_softc *sc)
8746 {
8747         int update;
8748
8749         DBENTER(BXE_VERBOSE_STATS);
8750
8751         update = 0;
8752
8753         /* Wait for any pending completions. */
8754         bxe_stats_comp(sc);
8755
8756         if (sc->port.pmf == 1)
8757                 update = (bxe_stats_hw_update(sc) == 0);
8758
8759         update |= (bxe_stats_storm_update(sc) == 0);
8760
8761         if (update) {
8762                 bxe_stats_net_update(sc);
8763
8764                 if (sc->port.pmf == 1)
8765                         bxe_stats_port_stop(sc);
8766
8767                 bxe_stats_hw_post(sc);
8768                 bxe_stats_comp(sc);
8769         }
8770
8771         DBEXIT(BXE_VERBOSE_STATS);
8772 }
8773
8774 /*
8775  * A dummy function to fill in the statistics state transition table.
8776  *
8777  * Returns:
8778  *   None.
8779  */
8780 static void
8781 bxe_stats_do_nothing(struct bxe_softc *sc)
8782 {
8783         DBENTER(BXE_VERBOSE_STATS);
8784         DBEXIT(BXE_VERBOSE_STATS);
8785 }
8786
8787 static const struct {
8788         void (*action)(struct bxe_softc *sc);
8789         enum bxe_stats_state next_state;
8790 } bxe_stats_stm[STATS_STATE_MAX][STATS_EVENT_MAX] = {
8791     /* State   Event  */
8792     {
8793     /* DISABLED PMF     */ {bxe_stats_pmf_update,       STATS_STATE_DISABLED},
8794     /*          LINK_UP */ {bxe_stats_start,            STATS_STATE_ENABLED},
8795     /*          UPDATE  */ {bxe_stats_do_nothing,       STATS_STATE_DISABLED},
8796     /*          STOP    */ {bxe_stats_do_nothing,       STATS_STATE_DISABLED}
8797     },
8798
8799     {
8800     /* ENABLED  PMF     */ {bxe_stats_pmf_start,        STATS_STATE_ENABLED},
8801     /*          LINK_UP */ {bxe_stats_restart,          STATS_STATE_ENABLED},
8802     /*          UPDATE  */ {bxe_stats_update,           STATS_STATE_ENABLED},
8803     /*          STOP    */ {bxe_stats_stop,             STATS_STATE_DISABLED}
8804     }
8805 };
8806
8807 /*
8808  * Move to the next state of the statistics state machine.
8809  *
8810  * Returns:
8811  *   None.
8812  */
8813 static void
8814 bxe_stats_handle(struct bxe_softc *sc, enum bxe_stats_event event)
8815 {
8816         enum bxe_stats_state state;
8817
8818         DBENTER(BXE_EXTREME_STATS);
8819
8820         state = sc->stats_state;
8821
8822 #ifdef BXE_DEBUG
8823         if (event != STATS_EVENT_UPDATE)
8824                 DBPRINT(sc, BXE_VERBOSE_STATS,
8825                     "%s(): Current state = %d, event = %d.\n", __FUNCTION__,
8826                     state, event);
8827 #endif
8828
8829         bxe_stats_stm[state][event].action(sc);
8830         sc->stats_state = bxe_stats_stm[state][event].next_state;
8831
8832 #ifdef BXE_DEBUG
8833         if (event != STATS_EVENT_UPDATE)
8834                 DBPRINT(sc, BXE_VERBOSE_STATS, "%s(): New state = %d.\n",
8835                     __FUNCTION__, sc->stats_state);
8836 #endif
8837
8838         DBEXIT(BXE_EXTREME_STATS);
8839 }
8840
8841 /*
8842  * bxe_chktso_window()
8843  * Checks to ensure the 13 bd sliding window is >= MSS for TSO.
8844  * Check that (13 total bds - 3bds) = 10 bd window >= MSS.
8845  * The window: 3 bds are = 1 (for headers BD) + 2 (for PBD and last BD)
8846  * The headers comes in a seperate bd in FreeBSD. So 13-3=10.
8847  *
8848  * Returns:
8849  *   0 if OK to send, 1 if packet needs further defragmentation.
8850  */
8851 static int
8852 bxe_chktso_window(struct bxe_softc* sc, int nsegs, bus_dma_segment_t *segs,
8853     struct mbuf *m0)
8854 {
8855         uint32_t num_wnds, wnd_size, wnd_sum;
8856         int32_t frag_idx, wnd_idx;
8857         unsigned short lso_mss;
8858         int defrag;
8859
8860         defrag = 0;
8861         wnd_sum = 0;
8862         wnd_size = 10;
8863         num_wnds = nsegs - wnd_size;
8864         lso_mss = htole16(m0->m_pkthdr.tso_segsz);
8865
8866         /*
8867          * Total Header lengths Eth+IP+TCP in 1st FreeBSD mbuf so
8868          * calculate the first window sum of data skip the first
8869          * assuming it is the header in FreeBSD.
8870          */
8871         for (frag_idx = 1; (frag_idx <= wnd_size); frag_idx++)
8872                 wnd_sum += htole16(segs[frag_idx].ds_len);
8873
8874         /* Chk the first 10 bd window size */
8875         if (wnd_sum < lso_mss)
8876                 return (defrag = 1);
8877
8878         /* Run through the windows */
8879         for (wnd_idx = 0; wnd_idx < num_wnds; wnd_idx++, frag_idx++) {
8880                 /* Subtract the 1st mbuf->m_len of the last wndw(-header). */
8881                 wnd_sum -= htole16(segs[wnd_idx+1].ds_len);
8882                 /* Add the next mbuf len to the len of our new window. */
8883                 wnd_sum +=  htole16(segs[frag_idx].ds_len);
8884                 if (wnd_sum < lso_mss) {
8885                         defrag = 1;
8886                         break;
8887                 }
8888         }
8889
8890         return (defrag);
8891 }
8892
8893
8894 /*
8895  * Encapsultes an mbuf cluster into the tx_bd chain structure and
8896  * makes the memory visible to the controller.
8897  *
8898  * If an mbuf is submitted to this routine and cannot be given to the
8899  * controller (e.g. it has too many fragments) then the function may free
8900  * the mbuf and return to the caller.
8901  *
8902  * Returns:
8903  *   0 = Success, !0 = Failure
8904  *   Note the side effect that an mbuf may be freed if it causes a problem.
8905  */
8906 static int
8907 bxe_tx_encap(struct bxe_fastpath *fp, struct mbuf **m_head)
8908 {
8909         bus_dma_segment_t segs[32];
8910         bus_dmamap_t map;
8911         struct mbuf *m0;
8912         struct eth_tx_parse_bd *tx_parse_bd;
8913         struct eth_tx_bd *tx_data_bd;
8914         struct eth_tx_bd *tx_total_pkt_size_bd;
8915         struct eth_tx_start_bd *tx_start_bd;
8916         uint16_t etype, sw_tx_bd_prod, sw_pkt_prod, total_pkt_size;
8917 //      uint16_t bd_index, pkt_index;
8918         uint8_t mac_type;
8919         int i, defragged, e_hlen, error, nsegs, rc, nbds, vlan_off, ovlan;
8920         struct bxe_softc *sc;
8921
8922         sc = fp->sc;
8923         DBENTER(BXE_VERBOSE_SEND);
8924
8925         DBRUN(M_ASSERTPKTHDR(*m_head));
8926
8927         m0 = *m_head;
8928         rc = defragged = nbds = ovlan = vlan_off = total_pkt_size = 0;
8929         tx_start_bd = NULL;
8930         tx_data_bd = NULL;
8931         tx_parse_bd = NULL;
8932         tx_total_pkt_size_bd = NULL;
8933
8934         /* Get the H/W pointer (0 to 65535) for packets and BD's. */
8935         sw_pkt_prod = fp->tx_pkt_prod;
8936         sw_tx_bd_prod = fp->tx_bd_prod;
8937
8938         /* Create the S/W index (0 to MAX_TX_BD) for packets and BD's. */
8939 //      pkt_index = TX_BD(sw_pkt_prod);
8940 //      bd_index = TX_BD(sw_tx_bd_prod);
8941
8942         mac_type = UNICAST_ADDRESS;
8943
8944         /* Map the mbuf into the next open DMAable memory. */
8945         map = fp->tx_mbuf_map[TX_BD(sw_pkt_prod)];
8946         error = bus_dmamap_load_mbuf_sg(fp->tx_mbuf_tag, map, m0,
8947             segs, &nsegs, BUS_DMA_NOWAIT);
8948
8949         /* Handle any mapping errors. */
8950         if(__predict_false(error != 0)){
8951                 fp->tx_dma_mapping_failure++;
8952                 if (error == ENOMEM) {
8953                         /* Resource issue, try again later. */
8954                         rc = ENOMEM;
8955                 } else if (error == EFBIG) {
8956                         /* Possibly recoverable with defragmentation. */
8957                         fp->mbuf_defrag_attempts++;
8958                         m0 = m_defrag(*m_head, M_DONTWAIT);
8959                         if (m0 == NULL) {
8960                                 fp->mbuf_defrag_failures++;
8961                                 rc = ENOBUFS;
8962                         } else {
8963                                 /* Defrag successful, try mapping again.*/
8964                                 *m_head = m0;
8965                                 error = bus_dmamap_load_mbuf_sg(
8966                                     fp->tx_mbuf_tag, map, m0,
8967                                     segs, &nsegs, BUS_DMA_NOWAIT);
8968                                 if (error) {
8969                                         fp->tx_dma_mapping_failure++;
8970                                         rc = error;
8971                                 }
8972                         }
8973                 } else {
8974                         /* Unknown, unrecoverable mapping error. */
8975                         DBPRINT(sc, BXE_WARN_SEND,
8976                             "%s(): Unknown TX mapping error! "
8977                             "rc = %d.\n", __FUNCTION__, error);
8978                         DBRUN(bxe_dump_mbuf(sc, m0));
8979                         rc = error;
8980                 }
8981
8982                 goto bxe_tx_encap_continue;
8983         }
8984
8985         /* Make sure there's enough room in the send queue. */
8986         if (__predict_false((nsegs + 2) >
8987             (USABLE_TX_BD - fp->tx_bd_used))) {
8988                 /* Recoverable, try again later. */
8989                 fp->tx_hw_queue_full++;
8990                 bus_dmamap_unload(fp->tx_mbuf_tag, map);
8991                 rc = ENOMEM;
8992                 goto bxe_tx_encap_continue;
8993         }
8994
8995         /* Capture the current H/W TX chain high watermark. */
8996         if (__predict_false(fp->tx_hw_max_queue_depth <
8997             fp->tx_bd_used))
8998                 fp->tx_hw_max_queue_depth = fp->tx_bd_used;
8999
9000         /* Now make sure it fits in the packet window. */
9001         if (__predict_false(nsegs > 12)) {
9002                 /*
9003                  * The mbuf may be to big for the controller
9004                  * to handle.  If the frame is a TSO frame
9005                  * we'll need to do an additional check.
9006                  */
9007                 if(m0->m_pkthdr.csum_flags & CSUM_TSO){
9008                         if (bxe_chktso_window(sc,nsegs,segs,m0) == 0)
9009                                 /* OK to send. */
9010                                 goto bxe_tx_encap_continue;
9011                         else
9012                                 fp->tx_window_violation_tso++;
9013                 } else
9014                         fp->tx_window_violation_std++;
9015
9016                 /* No sense trying to defrag again, we'll drop the frame. */
9017                 if (defragged > 0)
9018                         rc = ENODEV;
9019         }
9020
9021 bxe_tx_encap_continue:
9022         /* Check for errors */
9023         if (rc){
9024                 if(rc == ENOMEM){
9025                         /* Recoverable try again later  */
9026                 }else{
9027                         fp->tx_soft_errors++;
9028                         fp->tx_mbuf_alloc--;
9029                         m_freem(*m_head);
9030                         *m_head = NULL;
9031                 }
9032                 goto bxe_tx_encap_exit;
9033         }
9034
9035         /* Save the mbuf and mapping. */
9036         fp->tx_mbuf_ptr[TX_BD(sw_pkt_prod)] = m0;
9037         fp->tx_mbuf_map[TX_BD(sw_pkt_prod)] = map;
9038
9039         /* Set flag according to packet type (UNICAST_ADDRESS is default). */
9040         if (m0->m_flags & M_BCAST)
9041                 mac_type = BROADCAST_ADDRESS;
9042         else if (m0->m_flags & M_MCAST)
9043                 mac_type = MULTICAST_ADDRESS;
9044
9045         /* Prepare the first transmit (Start) BD for the mbuf. */
9046         tx_start_bd = &fp->tx_chain[TX_BD(sw_tx_bd_prod)].start_bd;
9047
9048         tx_start_bd->addr_lo = htole32(U64_LO(segs[0].ds_addr));
9049         tx_start_bd->addr_hi = htole32(U64_HI(segs[0].ds_addr));
9050         tx_start_bd->nbytes  = htole16(segs[0].ds_len);
9051         total_pkt_size += tx_start_bd->nbytes;
9052         tx_start_bd->bd_flags.as_bitfield = ETH_TX_BD_FLAGS_START_BD;
9053         tx_start_bd->general_data =
9054                 (mac_type << ETH_TX_START_BD_ETH_ADDR_TYPE_SHIFT);
9055
9056         tx_start_bd->general_data |= (1 << ETH_TX_START_BD_HDR_NBDS_SHIFT);
9057
9058         /* All frames have at least Start BD + Parsing BD. */
9059         nbds = nsegs + 1;
9060         tx_start_bd->nbd = htole16(nbds);
9061
9062         if (m0->m_flags & M_VLANTAG) {
9063                 tx_start_bd->bd_flags.as_bitfield |= ETH_TX_BD_FLAGS_VLAN_TAG;
9064                 tx_start_bd->vlan = htole16(m0->m_pkthdr.ether_vtag);
9065         } else
9066                 /*
9067                  * In cases where the VLAN tag is not used the firmware
9068                  * expects to see a packet counter in the VLAN tag field
9069                  * Failure to do so will cause an assertion which will
9070                  * stop the controller.
9071                  */
9072                 tx_start_bd->vlan = htole16(fp->tx_pkt_prod);
9073
9074         /*
9075          * Add a parsing BD from the chain. The parsing BD is always added,
9076          * however, it is only used for TSO & chksum.
9077          */
9078         sw_tx_bd_prod = NEXT_TX_BD(sw_tx_bd_prod);
9079         tx_parse_bd = (struct eth_tx_parse_bd *)
9080             &fp->tx_chain[TX_BD(sw_tx_bd_prod)].parse_bd;
9081         memset(tx_parse_bd, 0, sizeof(struct eth_tx_parse_bd));
9082
9083         /* Gather all info about the packet and add to tx_parse_bd */
9084         if (m0->m_pkthdr.csum_flags) {
9085                 struct ether_vlan_header *eh;
9086                 struct ip *ip = NULL;
9087                 struct tcphdr *th = NULL;
9088                 uint16_t flags = 0;
9089                 struct udphdr *uh = NULL;
9090
9091                 /* Map Ethernet header to find type & header length. */
9092                 eh = mtod(m0, struct ether_vlan_header *);
9093
9094                 /* Handle VLAN encapsulation if present. */
9095                 if (eh->evl_encap_proto == htons(ETHERTYPE_VLAN)) {
9096                         etype = ntohs(eh->evl_proto);
9097                         e_hlen = ETHER_HDR_LEN + vlan_off;
9098                 } else {
9099                         etype = ntohs(eh->evl_encap_proto);
9100                         e_hlen = ETHER_HDR_LEN;
9101                 }
9102
9103                 /* Set the Ethernet header length in 16 bit words. */
9104                 tx_parse_bd->global_data = (e_hlen + ovlan) >> 1;
9105                 tx_parse_bd->global_data |= ((m0->m_flags & M_VLANTAG) <<
9106                     ETH_TX_PARSE_BD_LLC_SNAP_EN_SHIFT);
9107
9108                 switch (etype) {
9109                 case ETHERTYPE_IP:
9110                         /* If mbuf len < 20bytes, IP header is in next mbuf. */
9111                         if (m0->m_len < sizeof(struct ip))
9112                                 ip = (struct ip *) m0->m_next->m_data;
9113                         else
9114                                 ip = (struct ip *) (m0->m_data + e_hlen);
9115
9116                         /* Calculate IP header length (16 bit words). */
9117                         tx_parse_bd->ip_hlen = (ip->ip_hl << 1);
9118
9119                         /* Calculate enet + IP header length (16 bit words). */
9120                         tx_parse_bd->total_hlen = tx_parse_bd->ip_hlen +
9121                             (e_hlen >> 1);
9122
9123                         if (m0->m_pkthdr.csum_flags & CSUM_IP) {
9124                                 fp->tx_offload_frames_csum_ip++;
9125                                 flags |= ETH_TX_BD_FLAGS_IP_CSUM;
9126                         }
9127
9128                         /* Handle any checksums requested by the stack. */
9129                         if ((m0->m_pkthdr.csum_flags & CSUM_TCP)||
9130                             (m0->m_pkthdr.csum_flags & CSUM_TSO)){
9131
9132                                 /* Get the TCP header. */
9133                                 th = (struct tcphdr *)((caddr_t)ip +
9134                                     (ip->ip_hl << 2));
9135
9136                                 /* Add the TCP checksum offload flag. */
9137                                 flags |= ETH_TX_BD_FLAGS_L4_CSUM;
9138                                 fp->tx_offload_frames_csum_tcp++;
9139
9140                                 /* Update the enet + IP + TCP header length. */
9141                                 tx_parse_bd->total_hlen +=
9142                                     (uint16_t)(th->th_off << 1);
9143
9144                                 /* Get the pseudo header checksum. */
9145                                 tx_parse_bd->tcp_pseudo_csum =
9146                                     ntohs(th->th_sum);
9147
9148                         } else if (m0->m_pkthdr.csum_flags & CSUM_UDP) {
9149                                 /*
9150                                  * The hardware doesn't actually support UDP
9151                                  * checksum offload but we can fake it by
9152                                  * doing TCP checksum offload and factoring
9153                                  * out the extra bytes that are different
9154                                  * between the TCP header and the UDP header.
9155                                  *
9156                                  * Calculation will begin 10 bytes before the
9157                                  * actual start of the UDP header.  To work
9158                                  * around this we need to calculate the
9159                                  * checksum of the 10 bytes before the UDP
9160                                  * header and factor that out of the UDP
9161                                  * pseudo header checksum before asking the
9162                                  * H/W to calculate the full UDP checksum.
9163                                  */
9164                                 uint16_t tmp_csum;
9165                                 uint32_t *tmp_uh;
9166
9167                                 /* This value is 10. */
9168                 uint8_t fix = (uint8_t) (offsetof(struct tcphdr, th_sum) -
9169                                     (int) offsetof(struct udphdr, uh_sum));
9170
9171                                 /*
9172                                  * Add the TCP checksum offload flag for
9173                                  * UDP frames too.*
9174                                  */
9175                                 flags |= ETH_TX_BD_FLAGS_L4_CSUM;
9176                                 fp->tx_offload_frames_csum_udp++;
9177                                 tx_parse_bd->global_data |=
9178                                     ETH_TX_PARSE_BD_UDP_CS_FLG;
9179
9180                                 /* Get a pointer to the UDP header. */
9181                                 uh = (struct udphdr *)((caddr_t)ip +
9182                                     (ip->ip_hl << 2));
9183
9184                                 /* Set pointer 10 bytes before UDP header. */
9185                                         tmp_uh = (uint32_t *)((uint8_t *)uh -
9186                                             fix);
9187
9188                                 /*
9189                                  * Calculate a pseudo header checksum over
9190                                  * the 10 bytes before the UDP header.
9191                                  */
9192                                 tmp_csum = in_pseudo(ntohl(*tmp_uh),
9193                                     ntohl(*(tmp_uh + 1)),
9194                                     ntohl((*(tmp_uh + 2)) & 0x0000FFFF));
9195
9196                                 /* Update the enet + IP + UDP header length. */
9197                                 tx_parse_bd->total_hlen +=
9198                                     (sizeof(struct udphdr) >> 1);
9199                                 tx_parse_bd->tcp_pseudo_csum =
9200                                     ~in_addword(uh->uh_sum, ~tmp_csum);
9201                         }
9202
9203                         /* Update the offload flags. */
9204                         tx_start_bd->bd_flags.as_bitfield |= flags;
9205                         break;
9206
9207                 case ETHERTYPE_IPV6:
9208                         fp->tx_unsupported_tso_request_ipv6++;
9209                         /* ToDo: Add IPv6 support. */
9210                         break;
9211
9212                 default:
9213                         fp->tx_unsupported_tso_request_not_tcp++;
9214                         /* ToDo - How to handle this error? */
9215                 }
9216
9217                 /* Setup the Parsing BD with TSO specific info */
9218                 if (m0->m_pkthdr.csum_flags & CSUM_TSO) {
9219                         uint16_t hdr_len = tx_parse_bd->total_hlen << 1;
9220
9221                         tx_start_bd->bd_flags.as_bitfield |=
9222                             ETH_TX_BD_FLAGS_SW_LSO;
9223                         fp->tx_offload_frames_tso++;
9224
9225                         /* ToDo: Does this really help? */
9226                         if (__predict_false(tx_start_bd->nbytes > hdr_len)) {
9227                                 fp->tx_header_splits++;
9228                                 /*
9229                                  * Split the first BD into 2 BDs to make the
9230                                  * firmwares job easy...
9231                                  */
9232                                 tx_start_bd->nbd++;
9233                                 DBPRINT(sc, BXE_EXTREME_SEND,
9234                         "%s(): TSO split headr size is %d (%x:%x) nbds %d\n",
9235                                     __FUNCTION__, tx_start_bd->nbytes,
9236                                     tx_start_bd->addr_hi,
9237                                     tx_start_bd->addr_lo, nbds);
9238
9239                                 sw_tx_bd_prod = NEXT_TX_BD(sw_tx_bd_prod);
9240
9241                                 /* New transmit BD (after the tx_parse_bd). */
9242                                 tx_data_bd =
9243                                     &fp->tx_chain[TX_BD(sw_tx_bd_prod)].reg_bd;
9244                                 tx_data_bd->addr_hi =
9245                                     htole32(U64_HI(segs[0].ds_addr + hdr_len));
9246                                 tx_data_bd->addr_lo =
9247                                     htole32(U64_LO(segs[0].ds_addr + hdr_len));
9248                                 tx_data_bd->nbytes =
9249                                     htole16(segs[0].ds_len) - hdr_len;
9250                                 if (tx_total_pkt_size_bd == NULL)
9251                                         tx_total_pkt_size_bd = tx_data_bd;
9252                         }
9253
9254                         /*
9255                          * The controller needs the following info for TSO:
9256                          * MSS, tcp_send_seq, ip_id, and tcp_pseudo_csum.
9257                          */
9258                         tx_parse_bd->lso_mss = htole16(m0->m_pkthdr.tso_segsz);
9259                         tx_parse_bd->tcp_send_seq = ntohl(th->th_seq);
9260                         tx_parse_bd->tcp_flags = th->th_flags;
9261                         tx_parse_bd->ip_id = ntohs(ip->ip_id);
9262
9263                         tx_parse_bd->tcp_pseudo_csum =
9264                             ntohs(in_pseudo(ip->ip_src.s_addr,
9265                             ip->ip_dst.s_addr, htons(IPPROTO_TCP)));
9266
9267                         tx_parse_bd->global_data |=
9268                             ETH_TX_PARSE_BD_PSEUDO_CS_WITHOUT_LEN;
9269                 }
9270         }
9271
9272         /* Prepare remaining BDs. Start_tx_bd contains first seg (frag). */
9273         for (i = 1; i < nsegs ; i++) {
9274                 sw_tx_bd_prod = NEXT_TX_BD(sw_tx_bd_prod);
9275                 tx_data_bd = &fp->tx_chain[TX_BD(sw_tx_bd_prod)].reg_bd;
9276                 tx_data_bd->addr_lo = htole32(U64_LO(segs[i].ds_addr));
9277                 tx_data_bd->addr_hi = htole32(U64_HI(segs[i].ds_addr));
9278                 tx_data_bd->nbytes = htole16(segs[i].ds_len);
9279                 if (tx_total_pkt_size_bd == NULL)
9280                         tx_total_pkt_size_bd = tx_data_bd;
9281                 total_pkt_size += tx_data_bd->nbytes;
9282         }
9283
9284         if(tx_total_pkt_size_bd != NULL)
9285                 tx_total_pkt_size_bd->total_pkt_bytes = total_pkt_size;
9286
9287         /* Update TX BD producer index value for next TX */
9288         sw_tx_bd_prod = NEXT_TX_BD(sw_tx_bd_prod);
9289
9290         /* Update the used TX BD counter. */
9291         fp->tx_bd_used += nbds;
9292
9293         /*
9294          * If the chain of tx_bd's describing this frame
9295          * is adjacent to or spans an eth_tx_next_bd element
9296          * then we need to increment the nbds value.
9297          */
9298         if(TX_IDX(sw_tx_bd_prod) < nbds)
9299                 nbds++;
9300
9301         /* Don't allow reordering of writes for nbd and packets. */
9302         mb();
9303         fp->tx_db.data.prod += nbds;
9304
9305         /* Producer points to the next free tx_bd at this point. */
9306         fp->tx_pkt_prod++;
9307         fp->tx_bd_prod = sw_tx_bd_prod;
9308
9309         DOORBELL(sc, fp->index, fp->tx_db.raw);
9310
9311         fp->tx_pkts++;
9312
9313         /* Prevent speculative reads from getting ahead of the status block. */
9314         bus_space_barrier(sc->bxe_btag, sc->bxe_bhandle,
9315             0, 0, BUS_SPACE_BARRIER_READ);
9316
9317         /* Prevent speculative reads from getting ahead of the doorbell. */
9318         bus_space_barrier(sc->bxe_db_btag, sc->bxe_db_bhandle,
9319             0, 0, BUS_SPACE_BARRIER_READ);
9320
9321 bxe_tx_encap_exit:
9322         DBEXIT(BXE_VERBOSE_SEND);
9323         return (rc);
9324 }
9325
9326
9327 /*
9328  * Legacy (non-RSS) dispatch routine.
9329  *
9330  * Returns:
9331  *   Nothing.
9332  */
9333 static void
9334 bxe_tx_start(struct ifnet *ifp)
9335 {
9336         struct bxe_softc *sc;
9337         struct bxe_fastpath *fp;
9338
9339         sc = ifp->if_softc;
9340         DBENTER(BXE_EXTREME_SEND);
9341
9342         /* Exit if the transmit queue is full or link down. */
9343         if (((ifp->if_drv_flags & (IFF_DRV_RUNNING | IFF_DRV_OACTIVE)) !=
9344             IFF_DRV_RUNNING) || !sc->link_vars.link_up) {
9345                 DBPRINT(sc, BXE_WARN,
9346                     "%s(): No link or TX queue full, ignoring "
9347                     "transmit request.\n", __FUNCTION__);
9348                 goto bxe_tx_start_exit;
9349         }
9350
9351         /* Set the TX queue for the frame. */
9352         fp = &sc->fp[0];
9353
9354         BXE_FP_LOCK(fp);
9355         bxe_tx_start_locked(ifp, fp);
9356         BXE_FP_UNLOCK(fp);
9357
9358 bxe_tx_start_exit:
9359         DBEXIT(BXE_EXTREME_SEND);
9360 }
9361
9362
9363 /*
9364  * Legacy (non-RSS) transmit routine.
9365  *
9366  * Returns:
9367  *   Nothing.
9368  */
9369 static void
9370 bxe_tx_start_locked(struct ifnet *ifp, struct bxe_fastpath *fp)
9371 {
9372         struct bxe_softc *sc;
9373         struct mbuf *m = NULL;
9374         int tx_count = 0;
9375
9376         sc = fp->sc;
9377         DBENTER(BXE_EXTREME_SEND);
9378
9379         BXE_FP_LOCK_ASSERT(fp);
9380
9381         /* Keep adding entries while there are frames to send. */
9382         while (!IFQ_DRV_IS_EMPTY(&ifp->if_snd)) {
9383
9384                 /* Check for any frames to send. */
9385                 IFQ_DRV_DEQUEUE(&ifp->if_snd, m);
9386                 if (__predict_false(m == NULL))
9387                         break;
9388
9389                 /* The transmit mbuf now belongs to us, keep track of it. */
9390                 fp->tx_mbuf_alloc++;
9391
9392                 /*
9393                  * Pack the data into the transmit ring. If we
9394                  * don't have room, place the mbuf back at the
9395                  * head of the TX queue, set the OACTIVE flag,
9396                  * and wait for the NIC to drain the chain.
9397                  */
9398                 if (__predict_false(bxe_tx_encap(fp, &m))) {
9399                         fp->tx_encap_failures++;
9400                         /* Very Bad Frames(tm) may have been dropped. */
9401                         if (m != NULL) {
9402                                 /*
9403                                  * Mark the TX queue as full and return
9404                                  * the frame.
9405                                  */
9406                                 ifp->if_drv_flags |= IFF_DRV_OACTIVE;
9407                                 IFQ_DRV_PREPEND(&ifp->if_snd, m);
9408                                 fp->tx_mbuf_alloc--;
9409                                 fp->tx_queue_xoff++;
9410                         } else {
9411
9412                         }
9413
9414                         /* Stop looking for more work. */
9415                         break;
9416                 }
9417
9418                 /* The transmit frame was enqueued successfully. */
9419                 tx_count++;
9420
9421                 /* Send a copy of the frame to any BPF listeners. */
9422                 BPF_MTAP(ifp, m);
9423         }
9424
9425         /* No TX packets were dequeued. */
9426         if (tx_count > 0)
9427                 /* Reset the TX watchdog timeout timer. */
9428                 fp->watchdog_timer = BXE_TX_TIMEOUT;
9429
9430         DBEXIT(BXE_EXTREME_SEND);
9431 }
9432
9433 #if __FreeBSD_version >= 800000
9434 /*
9435  * Multiqueue (RSS) dispatch routine.
9436  *
9437  * Returns:
9438  *   0 if transmit succeeds, !0 otherwise.
9439  */
9440 static int
9441 bxe_tx_mq_start(struct ifnet *ifp, struct mbuf *m)
9442 {
9443         struct bxe_softc *sc;
9444         struct bxe_fastpath *fp;
9445         int fp_index, rc;
9446
9447         sc = ifp->if_softc;
9448         DBENTER(BXE_EXTREME_SEND);
9449
9450         fp_index = 0;
9451
9452         /* If using flow ID, assign the TX queue based on the flow ID. */
9453         if ((m->m_flags & M_FLOWID) != 0)
9454                 fp_index = m->m_pkthdr.flowid % sc->num_queues;
9455
9456         /* Select the fastpath TX queue for the frame. */
9457         fp = &sc->fp[fp_index];
9458
9459         /* Skip H/W enqueue if transmit queue is full or link down. */
9460         if (((ifp->if_drv_flags & (IFF_DRV_RUNNING | IFF_DRV_OACTIVE)) !=
9461             IFF_DRV_RUNNING) || !sc->link_vars.link_up) {
9462                 /* Stash the mbuf if we can. */
9463                 rc = drbr_enqueue(ifp, fp->br, m);
9464                 goto bxe_tx_mq_start_exit;
9465         }
9466
9467         BXE_FP_LOCK(fp);
9468         rc = bxe_tx_mq_start_locked(ifp, fp, m);
9469         BXE_FP_UNLOCK(fp);
9470
9471 bxe_tx_mq_start_exit:
9472         DBEXIT(BXE_EXTREME_SEND);
9473         return (rc);
9474 }
9475
9476
9477 /*
9478  * Multiqueue (TSS) transmit routine.  This routine is responsible
9479  * for adding a frame to the hardware's transmit queue.
9480  *
9481  * Returns:
9482  *   0 if transmit succeeds, !0 otherwise.
9483  */
9484 static int
9485 bxe_tx_mq_start_locked(struct ifnet *ifp,
9486     struct bxe_fastpath *fp, struct mbuf *m)
9487 {
9488         struct bxe_softc *sc;
9489         struct mbuf *next;
9490         int depth, rc, tx_count;
9491
9492         sc = fp->sc;
9493         DBENTER(BXE_EXTREME_SEND);
9494
9495         rc = tx_count = 0;
9496
9497         /* Fetch the depth of the driver queue. */
9498         depth = drbr_inuse(ifp, fp->br);
9499         if (depth > fp->tx_max_drbr_queue_depth)
9500                 fp->tx_max_drbr_queue_depth = depth;
9501
9502         BXE_FP_LOCK_ASSERT(fp);
9503
9504         if (m == NULL) {
9505                 /* No new work, check for pending frames. */
9506                 next = drbr_dequeue(ifp, fp->br);
9507         } else if (drbr_needs_enqueue(ifp, fp->br)) {
9508                 /* Both new and pending work, maintain packet order. */
9509                 rc = drbr_enqueue(ifp, fp->br, m);
9510                 if (rc != 0) {
9511                         fp->tx_soft_errors++;
9512                         goto bxe_tx_mq_start_locked_exit;
9513                 }
9514                 next = drbr_dequeue(ifp, fp->br);
9515         } else
9516                 /* New work only, nothing pending. */
9517                 next = m;
9518
9519         /* Keep adding entries while there are frames to send. */
9520         while (next != NULL) {
9521
9522                 /* The transmit mbuf now belongs to us, keep track of it. */
9523                 fp->tx_mbuf_alloc++;
9524
9525                 /*
9526                  * Pack the data into the transmit ring. If we
9527                  * don't have room, place the mbuf back at the
9528                  * head of the TX queue, set the OACTIVE flag,
9529                  * and wait for the NIC to drain the chain.
9530                  */
9531                 rc = bxe_tx_encap(fp, &next);
9532                 if (__predict_false(rc != 0)) {
9533                         fp->tx_encap_failures++;
9534                         /* Very Bad Frames(tm) may have been dropped. */
9535                         if (next != NULL) {
9536                                 /*
9537                                  * Mark the TX queue as full and save
9538                                  * the frame.
9539                                  */
9540                                 ifp->if_drv_flags |= IFF_DRV_OACTIVE;
9541                                 fp->tx_frame_deferred++;
9542
9543                                 /* This may reorder frame. */
9544                                 rc = drbr_enqueue(ifp, fp->br, next);
9545                                 fp->tx_mbuf_alloc--;
9546                         }
9547
9548                         /* Stop looking for more work. */
9549                         break;
9550                 }
9551
9552                 /* The transmit frame was enqueued successfully. */
9553                 tx_count++;
9554
9555                 /* Send a copy of the frame to any BPF listeners. */
9556                 BPF_MTAP(ifp, next);
9557
9558                 /* Handle any completions if we're running low. */
9559                 if (fp->tx_bd_used >= BXE_TX_CLEANUP_THRESHOLD)
9560                         bxe_txeof(fp);
9561
9562                 /* Close TX since there's so little room left. */
9563                 if (fp->tx_bd_used >= BXE_TX_CLEANUP_THRESHOLD) {
9564                         ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
9565                         break;
9566                 }
9567
9568                 next = drbr_dequeue(ifp, fp->br);
9569         }
9570
9571         /* No TX packets were dequeued. */
9572         if (tx_count > 0)
9573                 /* Reset the TX watchdog timeout timer. */
9574                 fp->watchdog_timer = BXE_TX_TIMEOUT;
9575
9576 bxe_tx_mq_start_locked_exit:
9577         DBEXIT(BXE_EXTREME_SEND);
9578         return (rc);
9579 }
9580
9581
9582 static void
9583 bxe_mq_flush(struct ifnet *ifp)
9584 {
9585         struct bxe_softc *sc;
9586         struct bxe_fastpath *fp;
9587         struct mbuf *m;
9588         int i;
9589
9590         sc = ifp->if_softc;
9591
9592         DBENTER(BXE_VERBOSE_UNLOAD);
9593
9594         for (i = 0; i < sc->num_queues; i++) {
9595                 fp = &sc->fp[i];
9596
9597                 if (fp->br != NULL) {
9598                         DBPRINT(sc, BXE_VERBOSE_UNLOAD,
9599                             "%s(): Clearing fp[%02d]...\n",
9600                             __FUNCTION__, fp->index);
9601
9602                         BXE_FP_LOCK(fp);
9603                         while ((m = buf_ring_dequeue_sc(fp->br)) != NULL)
9604                                 m_freem(m);
9605                         BXE_FP_UNLOCK(fp);
9606                 }
9607         }
9608
9609         if_qflush(ifp);
9610
9611         DBEXIT(BXE_VERBOSE_UNLOAD);
9612 }
9613 #endif /* FreeBSD_version >= 800000 */
9614
9615
9616 /*
9617  * Handles any IOCTL calls from the operating system.
9618  *
9619  * Returns:
9620  *   0 for success, positive value for failure.
9621  */
9622 static int
9623 bxe_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
9624 {
9625         struct bxe_softc *sc;
9626         struct ifreq *ifr;
9627         int error, mask, reinit;
9628
9629         sc = ifp->if_softc;
9630         DBENTER(BXE_VERBOSE_LOAD | BXE_VERBOSE_RESET | BXE_VERBOSE_MISC);
9631
9632         ifr = (struct ifreq *)data;
9633         error = 0;
9634         reinit = 0;
9635
9636         switch (command) {
9637         case SIOCSIFMTU:
9638                 /* Set the MTU. */
9639                 DBPRINT(sc, BXE_VERBOSE_MISC, "%s(): Received SIOCSIFMTU\n",
9640                     __FUNCTION__);
9641
9642                 /* Check that the MTU setting is supported. */
9643                 if ((ifr->ifr_mtu < BXE_MIN_MTU) ||
9644                     (ifr->ifr_mtu > BXE_JUMBO_MTU)) {
9645                         error = EINVAL;
9646                         break;
9647                 }
9648
9649                 BXE_CORE_LOCK(sc);
9650                 ifp->if_mtu = ifr->ifr_mtu;
9651                 BXE_CORE_UNLOCK(sc);
9652
9653                 reinit = 1;
9654                 break;
9655         case SIOCSIFFLAGS:
9656                 /* Toggle the interface state up or down. */
9657                 DBPRINT(sc, BXE_VERBOSE_MISC, "%s(): Received SIOCSIFFLAGS\n",
9658                     __FUNCTION__);
9659
9660                 BXE_CORE_LOCK(sc);
9661                 /* Check if the interface is up. */
9662                 if (ifp->if_flags & IFF_UP) {
9663                         if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
9664                                 /* Set promiscuous/multicast flags. */
9665                                 bxe_set_rx_mode(sc);
9666                         } else {
9667                                 /* Start the HW */
9668                                 bxe_init_locked(sc, LOAD_NORMAL);
9669                         }
9670                 } else {
9671                         /* Bring down the interface. */
9672                         if (ifp->if_drv_flags & IFF_DRV_RUNNING)
9673                                 bxe_stop_locked(sc, UNLOAD_NORMAL);
9674                 }
9675                 BXE_CORE_UNLOCK(sc);
9676
9677                 break;
9678         case SIOCADDMULTI:
9679         case SIOCDELMULTI:
9680                 /* Add/Delete multicast addresses. */
9681                 DBPRINT(sc, BXE_VERBOSE_MISC,
9682                     "%s(): Received SIOCADDMULTI/SIOCDELMULTI\n", __FUNCTION__);
9683
9684                 BXE_CORE_LOCK(sc);
9685                 /* Check if the interface is up. */
9686                 if (ifp->if_drv_flags & IFF_DRV_RUNNING)
9687                         /* Set receive mode flags. */
9688                         bxe_set_rx_mode(sc);
9689                 BXE_CORE_UNLOCK(sc);
9690
9691                 break;
9692         case SIOCSIFMEDIA:
9693         case SIOCGIFMEDIA:
9694                 /* Set/Get Interface media */
9695                 DBPRINT(sc, BXE_VERBOSE_MISC,
9696                     "%s(): Received SIOCSIFMEDIA/SIOCGIFMEDIA\n", __FUNCTION__);
9697
9698                 error = ifmedia_ioctl(ifp, ifr, &sc->bxe_ifmedia, command);
9699                 break;
9700         case SIOCSIFCAP:
9701                 /* Set interface capability */
9702
9703                 /* Find out which capabilities have changed. */
9704                 mask = ifr->ifr_reqcap ^ ifp->if_capenable;
9705                 DBPRINT(sc, BXE_VERBOSE_MISC,
9706                     "%s(): Received SIOCSIFCAP (mask = 0x%08X)\n", __FUNCTION__,
9707                     (uint32_t)mask);
9708
9709                 BXE_CORE_LOCK(sc);
9710
9711                 /* Toggle the LRO capabilites enable flag. */
9712                 if (mask & IFCAP_LRO) {
9713                         ifp->if_capenable ^= IFCAP_LRO;
9714                         sc->bxe_flags ^= BXE_TPA_ENABLE_FLAG;
9715                         DBPRINT(sc, BXE_INFO_MISC,
9716                             "%s(): Toggling LRO (bxe_flags = "
9717                             "0x%08X).\n", __FUNCTION__, sc->bxe_flags);
9718
9719                         /* LRO requires different buffer setup. */
9720                         reinit = 1;
9721                 }
9722
9723                 /* Toggle the TX checksum capabilites enable flag. */
9724                 if (mask & IFCAP_TXCSUM) {
9725                         DBPRINT(sc, BXE_VERBOSE_MISC,
9726                             "%s(): Toggling IFCAP_TXCSUM.\n", __FUNCTION__);
9727
9728                         ifp->if_capenable ^= IFCAP_TXCSUM;
9729
9730                         if (IFCAP_TXCSUM & ifp->if_capenable)
9731                                 ifp->if_hwassist = BXE_IF_HWASSIST;
9732                         else
9733                                 ifp->if_hwassist = 0;
9734                 }
9735
9736                 /* Toggle the RX checksum capabilities enable flag. */
9737                 if (mask & IFCAP_RXCSUM) {
9738                         DBPRINT(sc, BXE_VERBOSE_MISC,
9739                             "%s(): Toggling IFCAP_RXCSUM.\n", __FUNCTION__);
9740
9741                         ifp->if_capenable ^= IFCAP_RXCSUM;
9742
9743                         if (IFCAP_RXCSUM & ifp->if_capenable)
9744                                 ifp->if_hwassist = BXE_IF_HWASSIST;
9745                         else
9746                                 ifp->if_hwassist = 0;
9747                 }
9748
9749                 /* Toggle VLAN_MTU capabilities enable flag. */
9750                 if (mask & IFCAP_VLAN_MTU) {
9751                         /* ToDo: Is this really true? */
9752                         BXE_PRINTF("%s(%d): Changing VLAN_MTU not supported.\n",
9753                             __FILE__, __LINE__);
9754                         error = EINVAL;
9755                 }
9756
9757                 /* Toggle VLANHWTAG capabilities enabled flag. */
9758                 if (mask & IFCAP_VLAN_HWTAGGING) {
9759                         /* ToDo: Is this really true? */
9760                         BXE_PRINTF(
9761                             "%s(%d): Changing VLAN_HWTAGGING not supported!\n",
9762                             __FILE__, __LINE__);
9763                         error = EINVAL;
9764                 }
9765
9766                 /* Toggle TSO4 capabilities enabled flag. */
9767                 if (mask & IFCAP_TSO4) {
9768                         DBPRINT(sc, BXE_VERBOSE_MISC,
9769                             "%s(): Toggling IFCAP_TSO4.\n", __FUNCTION__);
9770
9771                         ifp->if_capenable ^= IFCAP_TSO4;
9772                 }
9773
9774                 /* Toggle TSO6 capabilities enabled flag. */
9775                 if (mask & IFCAP_TSO6) {
9776                         /* ToDo: Add TSO6 support. */
9777                         BXE_PRINTF(
9778                             "%s(%d): Changing TSO6 not supported!\n",
9779                             __FILE__, __LINE__);
9780                 }
9781                 BXE_CORE_UNLOCK(sc);
9782
9783                 /*
9784                  * ToDo: Look into supporting:
9785                  *   VLAN_HWFILTER
9786                  *   VLAN_HWCSUM
9787                  *   VLAN_HWTSO
9788                  *   POLLING
9789                  *   WOL[_UCAST|_MCAST|_MAGIC]
9790                  *
9791                  */
9792                 break;
9793         default:
9794                 /* We don't know how to handle the IOCTL, pass it on. */
9795                 error = ether_ioctl(ifp, command, data);
9796                 break;
9797         }
9798
9799         /* Restart the controller with the new capabilities. */
9800         if ((ifp->if_drv_flags & IFF_DRV_RUNNING) && (reinit != 0)) {
9801                 BXE_CORE_LOCK(sc);
9802                 bxe_stop_locked(sc, UNLOAD_NORMAL);
9803                 bxe_init_locked(sc, LOAD_NORMAL);
9804                 BXE_CORE_UNLOCK(sc);
9805         }
9806
9807         DBEXIT(BXE_VERBOSE_LOAD | BXE_VERBOSE_RESET | BXE_VERBOSE_MISC);
9808
9809         return (error);
9810 }
9811
9812 /*
9813  * Gets the current value of the RX Completion Consumer index
9814  * from the fastpath status block, updates it as necessary if
9815  * it is pointing to a "Next Page" entry, and returns it to the
9816  * caller.
9817  *
9818  * Returns:
9819  *   The adjusted value of *fp->rx_cons_sb.
9820  */
9821 static __inline uint16_t
9822 bxe_rx_cq_cons(struct bxe_fastpath *fp)
9823 {
9824         volatile uint16_t rx_cq_cons_sb = 0;
9825
9826         rmb();
9827         rx_cq_cons_sb = (volatile uint16_t) le16toh(*fp->rx_cq_cons_sb);
9828
9829         /*
9830          * It is valid for the hardware's copy of the completion
9831          * consumer index to be pointing at a "Next Page" entry in
9832          * the completion chain but the driver prefers to assume
9833          * that it is pointing at the next available CQE so we
9834          * need to adjust the value accordingly.
9835          */
9836         if ((rx_cq_cons_sb & USABLE_RCQ_ENTRIES_PER_PAGE) ==
9837             USABLE_RCQ_ENTRIES_PER_PAGE)
9838                 rx_cq_cons_sb++;
9839
9840         return (rx_cq_cons_sb);
9841 }
9842
9843 static __inline int
9844 bxe_has_tx_work(struct bxe_fastpath *fp)
9845 {
9846
9847         rmb();
9848         return (((fp->tx_pkt_prod != le16toh(*fp->tx_pkt_cons_sb)) || \
9849             (fp->tx_pkt_prod != fp->tx_pkt_cons)));
9850 }
9851
9852 /*
9853  * Checks if there are any received frames to process on the
9854  * completion queue.
9855  *
9856  * Returns:
9857  *   0 = No received frames pending, !0 = Received frames
9858  *       pending
9859  */
9860 static __inline int
9861 bxe_has_rx_work(struct bxe_fastpath *fp)
9862 {
9863
9864         rmb();
9865         return (bxe_rx_cq_cons(fp) != fp->rx_cq_cons);
9866 }
9867
9868 /*
9869  * Slowpath task entry point.
9870  *
9871  * Returns:
9872  *   None
9873  */
9874 static void
9875 bxe_task_sp(void *xsc, int pending)
9876 {
9877         struct bxe_softc *sc;
9878         uint32_t sp_status;
9879
9880         sc = xsc;
9881
9882         DBPRINT(sc, BXE_EXTREME_INTR, "%s(): pending = %d.\n", __FUNCTION__,
9883             pending);
9884
9885         /* Check for the source of the interrupt. */
9886         sp_status = bxe_update_dsb_idx(sc);
9887
9888         /* Handle any hardware attentions. */
9889         if (sp_status & 0x1) {
9890                 bxe_attn_int(sc);
9891                 sp_status &= ~0x1;
9892         }
9893
9894         /* CSTORM event asserted (query_stats, port delete ramrod, etc.). */
9895         if (sp_status & 0x2) {
9896                 sc->stats_pending = 0;
9897                 sp_status &= ~0x2;
9898         }
9899
9900         /* Check for other weirdness. */
9901         if (sp_status != 0) {
9902                 DBPRINT(sc, BXE_WARN, "%s(): Unexpected slowpath interrupt "
9903                     "(sp_status = 0x%04X)!\n", __FUNCTION__, sp_status);
9904         }
9905
9906         /* Acknowledge the xSTORM tags and enable slowpath interrupts. */
9907         bxe_ack_sb(sc, DEF_SB_ID, ATTENTION_ID, le16toh(sc->def_att_idx),
9908             IGU_INT_NOP, 1);
9909         bxe_ack_sb(sc, DEF_SB_ID, USTORM_ID, le16toh(sc->def_u_idx),
9910             IGU_INT_NOP, 1);
9911         bxe_ack_sb(sc, DEF_SB_ID, CSTORM_ID, le16toh(sc->def_c_idx),
9912             IGU_INT_NOP, 1);
9913         bxe_ack_sb(sc, DEF_SB_ID, XSTORM_ID, le16toh(sc->def_x_idx),
9914             IGU_INT_NOP, 1);
9915         bxe_ack_sb(sc, DEF_SB_ID, TSTORM_ID, le16toh(sc->def_t_idx),
9916             IGU_INT_ENABLE, 1);
9917 }
9918
9919
9920 /*
9921  * Legacy interrupt entry point.
9922  *
9923  * Verifies that the controller generated the interrupt and
9924  * then calls a separate routine to handle the various
9925  * interrupt causes: link, RX, and TX.
9926  *
9927  * Returns:
9928  *   None
9929  */
9930 static void
9931 bxe_intr_legacy(void *xsc)
9932 {
9933         struct bxe_softc *sc;
9934         struct bxe_fastpath *fp;
9935         uint32_t mask, fp_status;
9936
9937         sc = xsc;
9938         fp = &sc->fp[0];
9939
9940         /* Don't handle any interrupts if we're not ready. */
9941         if (__predict_false(sc->intr_sem != 0))
9942                 goto bxe_intr_legacy_exit;
9943
9944         /* Bail out if the interrupt wasn't generated by our hardware. */
9945         fp_status = bxe_ack_int(sc);
9946         if (fp_status == 0)
9947                 goto bxe_intr_legacy_exit;
9948
9949         /* Handle the fastpath interrupt. */
9950         /*
9951          * sb_id = 0 for ustorm, 1 for cstorm.
9952          * The bits returned from ack_int() are 0-15,
9953          * bit 0=attention status block
9954          * bit 1=fast path status block
9955          * A mask of 0x2 or more = tx/rx event
9956          * A mask of 1 = slow path event
9957          */
9958
9959         mask = (0x2 << fp->sb_id);
9960         DBPRINT(sc, BXE_INSANE_INTR, "%s(): fp_status = 0x%08X, mask = "
9961             "0x%08X\n", __FUNCTION__, fp_status, mask);
9962
9963         /* CSTORM event means fastpath completion. */
9964         if (fp_status & mask) {
9965                 /* This interrupt must be ours, disable further interrupts. */
9966                 bxe_ack_sb(sc, fp->sb_id, USTORM_ID, 0, IGU_INT_DISABLE, 0);
9967 #ifdef BXE_TASK
9968                 taskqueue_enqueue(fp->tq, &fp->task);
9969 #else
9970                 bxe_task_fp((void *)fp, 0);
9971 #endif
9972                 /* Clear this event from the status flags. */
9973                 fp_status &= ~mask;
9974         }
9975
9976         /* Handle all slow path interrupts and attentions */
9977         if (fp_status & 0x1) {
9978                 /* Acknowledge and disable further slowpath interrupts. */
9979                 bxe_ack_sb(sc, DEF_SB_ID, TSTORM_ID, 0, IGU_INT_DISABLE, 0);
9980 #ifdef BXE_TASK
9981                 /* Schedule the slowpath task. */
9982                 taskqueue_enqueue(sc->tq, &sc->task);
9983 #else
9984                 bxe_task_sp(xsc, 0);
9985 #endif
9986                 /* Clear this event from the status flags. */
9987                 fp_status &= ~0x1;
9988         }
9989
9990 #ifdef BXE_DEBUG
9991         if (fp_status) {
9992                 DBPRINT(sc, BXE_WARN,
9993                     "%s(): Unexpected fastpath status (fp_status = 0x%08X)!\n",
9994                     __FUNCTION__, fp_status);
9995         }
9996 #endif
9997
9998         DBEXIT(BXE_EXTREME_INTR);
9999
10000 bxe_intr_legacy_exit:
10001         return;
10002 }
10003
10004 /*
10005  * Slowpath interrupt entry point.
10006  *
10007  * Acknowledge the interrupt and schedule a slowpath task.
10008  *
10009  * Returns:
10010  *   None
10011  */
10012 static void
10013 bxe_intr_sp(void *xsc)
10014 {
10015         struct bxe_softc *sc;
10016
10017         sc = xsc;
10018
10019         DBPRINT(sc, BXE_INSANE_INTR, "%s(%d): Slowpath interrupt.\n",
10020             __FUNCTION__, curcpu);
10021
10022         /* Don't handle any interrupts if we're not ready. */
10023         if (__predict_false(sc->intr_sem != 0))
10024                 goto bxe_intr_sp_exit;
10025
10026         /* Acknowledge and disable further slowpath interrupts. */
10027         bxe_ack_sb(sc, DEF_SB_ID, TSTORM_ID, 0, IGU_INT_DISABLE, 0);
10028
10029 #ifdef BXE_TASK
10030         /* Schedule the slowpath task. */
10031         taskqueue_enqueue(sc->tq, &sc->task);
10032 #else
10033         bxe_task_sp(xsc, 0);
10034 #endif
10035
10036 bxe_intr_sp_exit:
10037         return;
10038 }
10039
10040 /*
10041  * Fastpath interrupt entry point.
10042  *
10043  * Acknowledge the interrupt and schedule a fastpath task.
10044  *
10045  * Returns:
10046  *   None
10047  */
10048 static void
10049 bxe_intr_fp (void *xfp)
10050 {
10051         struct bxe_fastpath *fp;
10052         struct bxe_softc *sc;
10053
10054         fp = xfp;
10055         sc = fp->sc;
10056
10057         DBPRINT(sc, BXE_INSANE_INTR,
10058             "%s(%d): fp[%02d].sb_id = %d interrupt.\n",
10059             __FUNCTION__, curcpu, fp->index, fp->sb_id);
10060
10061         /* Don't handle any interrupts if we're not ready. */
10062         if (__predict_false(sc->intr_sem != 0))
10063                 goto bxe_intr_fp_exit;
10064
10065         /* Disable further interrupts. */
10066         bxe_ack_sb(sc, fp->sb_id, USTORM_ID, 0, IGU_INT_DISABLE, 0);
10067 #ifdef BXE_TASK
10068         taskqueue_enqueue(fp->tq, &fp->task);
10069 #else
10070         bxe_task_fp (xfp, 0);
10071 #endif
10072
10073 bxe_intr_fp_exit:
10074         return;
10075 }
10076
10077 /*
10078  * Fastpath task entry point.
10079  *
10080  * Handle any pending transmit or receive events.
10081  *
10082  * Returns:
10083  *   None
10084  */
10085 static void
10086 bxe_task_fp (void *xfp, int pending)
10087 {
10088         struct bxe_fastpath *fp;
10089         struct bxe_softc *sc;
10090
10091         fp = xfp;
10092         sc = fp->sc;
10093
10094         DBPRINT(sc, BXE_EXTREME_INTR, "%s(%d): Fastpath task on fp[%02d]"
10095             ".sb_id = %d\n", __FUNCTION__, curcpu, fp->index, fp->sb_id);
10096
10097         /* Update the fast path indices */
10098         bxe_update_fpsb_idx(fp);
10099
10100         /* Service any completed TX frames. */
10101         if (bxe_has_tx_work(fp)) {
10102                 BXE_FP_LOCK(fp);
10103                 bxe_txeof(fp);
10104                 BXE_FP_UNLOCK(fp);
10105         }
10106
10107         /* Service any completed RX frames. */
10108         rmb();
10109         bxe_rxeof(fp);
10110
10111         /* Acknowledge the fastpath status block indices. */
10112         bxe_ack_sb(sc, fp->sb_id, USTORM_ID, fp->fp_u_idx, IGU_INT_NOP, 1);
10113         bxe_ack_sb(sc, fp->sb_id, CSTORM_ID, fp->fp_c_idx, IGU_INT_ENABLE, 1);
10114 }
10115
10116 /*
10117  * Clears the fastpath (per-queue) status block.
10118  *
10119  * Returns:
10120  *   None
10121  */
10122 static void
10123 bxe_zero_sb(struct bxe_softc *sc, int sb_id)
10124 {
10125         int port;
10126
10127         DBENTER(BXE_VERBOSE_LOAD | BXE_VERBOSE_RESET | BXE_VERBOSE_INTR);
10128         port = BP_PORT(sc);
10129
10130         /* "CSTORM" */
10131         bxe_init_fill(sc, CSEM_REG_FAST_MEMORY +
10132             CSTORM_SB_HOST_STATUS_BLOCK_U_OFFSET(port, sb_id), 0,
10133             CSTORM_SB_STATUS_BLOCK_U_SIZE / 4);
10134         bxe_init_fill(sc, CSEM_REG_FAST_MEMORY +
10135             CSTORM_SB_HOST_STATUS_BLOCK_C_OFFSET(port, sb_id), 0,
10136             CSTORM_SB_STATUS_BLOCK_C_SIZE / 4);
10137
10138         DBEXIT(BXE_VERBOSE_LOAD | BXE_VERBOSE_RESET | BXE_VERBOSE_INTR);
10139 }
10140
10141 /*
10142  * Initialize the fastpath (per queue) status block.
10143  *
10144  * Returns:
10145  *   None
10146  */
10147 static void
10148 bxe_init_sb(struct bxe_softc *sc, struct host_status_block *sb,
10149     bus_addr_t mapping, int sb_id)
10150 {
10151         uint64_t section;
10152         int func, index, port;
10153
10154         DBENTER(BXE_VERBOSE_LOAD | BXE_VERBOSE_RESET | BXE_VERBOSE_INTR);
10155
10156         port = BP_PORT(sc);
10157         func = BP_FUNC(sc);
10158
10159         DBPRINT(sc, (BXE_VERBOSE_LOAD | BXE_VERBOSE_RESET | BXE_VERBOSE_INTR),
10160             "%s(): Initializing sb_id = %d on port %d, function %d.\n",
10161             __FUNCTION__, sb_id, port, func);
10162
10163         /* Setup the USTORM status block. */
10164         section = ((uint64_t)mapping) + offsetof(struct host_status_block,
10165             u_status_block);
10166         sb->u_status_block.status_block_id = sb_id;
10167
10168         REG_WR(sc, BAR_CSTORM_INTMEM +
10169             CSTORM_SB_HOST_SB_ADDR_U_OFFSET(port, sb_id), U64_LO(section));
10170         REG_WR(sc, BAR_CSTORM_INTMEM +
10171             ((CSTORM_SB_HOST_SB_ADDR_U_OFFSET(port, sb_id)) + 4),
10172             U64_HI(section));
10173         REG_WR8(sc, BAR_CSTORM_INTMEM + FP_USB_FUNC_OFF +
10174             CSTORM_SB_HOST_STATUS_BLOCK_U_OFFSET(port, sb_id), func);
10175
10176         for (index = 0; index < HC_USTORM_SB_NUM_INDICES; index++)
10177                 REG_WR16(sc, BAR_CSTORM_INTMEM +
10178                     CSTORM_SB_HC_DISABLE_U_OFFSET(port, sb_id, index), 0x1);
10179
10180         /* Setup the CSTORM status block. */
10181         section = ((uint64_t)mapping) + offsetof(struct host_status_block,
10182             c_status_block);
10183         sb->c_status_block.status_block_id = sb_id;
10184
10185         /* Write the status block address to CSTORM. Order is important! */
10186         REG_WR(sc, BAR_CSTORM_INTMEM +
10187             CSTORM_SB_HOST_SB_ADDR_C_OFFSET(port, sb_id), U64_LO(section));
10188         REG_WR(sc, BAR_CSTORM_INTMEM +
10189             ((CSTORM_SB_HOST_SB_ADDR_C_OFFSET(port, sb_id)) + 4),
10190             U64_HI(section));
10191         REG_WR8(sc, BAR_CSTORM_INTMEM + FP_CSB_FUNC_OFF +
10192             CSTORM_SB_HOST_STATUS_BLOCK_C_OFFSET(port, sb_id), func);
10193
10194         for (index = 0; index < HC_CSTORM_SB_NUM_INDICES; index++)
10195                 REG_WR16(sc, BAR_CSTORM_INTMEM +
10196                     CSTORM_SB_HC_DISABLE_C_OFFSET(port, sb_id, index), 0x1);
10197
10198         /* Enable interrupts. */
10199         bxe_ack_sb(sc, sb_id, CSTORM_ID, 0, IGU_INT_ENABLE, 0);
10200
10201         DBEXIT(BXE_VERBOSE_LOAD | BXE_VERBOSE_RESET | BXE_VERBOSE_INTR);
10202 }
10203
10204 /*
10205  * Clears the default status block.
10206  *
10207  * Returns:
10208  *   None
10209  */
10210 static void
10211 bxe_zero_def_sb(struct bxe_softc *sc)
10212 {
10213         int func;
10214
10215         func = BP_FUNC(sc);
10216
10217         DBENTER(BXE_VERBOSE_LOAD | BXE_VERBOSE_RESET | BXE_VERBOSE_INTR);
10218         DBPRINT(sc, (BXE_VERBOSE_LOAD | BXE_VERBOSE_RESET | BXE_VERBOSE_INTR),
10219             "%s(): Clearing default status block on function %d.\n",
10220             __FUNCTION__, func);
10221
10222         /* Fill the STORM's copy of the default status block with 0. */
10223         bxe_init_fill(sc, TSEM_REG_FAST_MEMORY +
10224             TSTORM_DEF_SB_HOST_STATUS_BLOCK_OFFSET(func), 0,
10225             sizeof(struct tstorm_def_status_block) / 4);
10226         bxe_init_fill(sc, CSEM_REG_FAST_MEMORY +
10227             CSTORM_DEF_SB_HOST_STATUS_BLOCK_U_OFFSET(func), 0,
10228             sizeof(struct cstorm_def_status_block_u) / 4);
10229         bxe_init_fill(sc, CSEM_REG_FAST_MEMORY +
10230             CSTORM_DEF_SB_HOST_STATUS_BLOCK_C_OFFSET(func), 0,
10231             sizeof(struct cstorm_def_status_block_c) / 4);
10232         bxe_init_fill(sc, XSEM_REG_FAST_MEMORY +
10233             XSTORM_DEF_SB_HOST_STATUS_BLOCK_OFFSET(func), 0,
10234             sizeof(struct xstorm_def_status_block) / 4);
10235
10236         DBEXIT(BXE_VERBOSE_LOAD | BXE_VERBOSE_RESET | BXE_VERBOSE_INTR);
10237 }
10238
10239 /*
10240  * Initialize default status block.
10241  *
10242  * Returns:
10243  *   None
10244  */
10245 static void
10246 bxe_init_def_sb(struct bxe_softc *sc, struct host_def_status_block *def_sb,
10247     bus_addr_t mapping, int sb_id)
10248 {
10249         uint64_t section;
10250         int func, index, port, reg_offset, val;
10251
10252         port = BP_PORT(sc);
10253         func = BP_FUNC(sc);
10254
10255         DBENTER(BXE_VERBOSE_LOAD | BXE_VERBOSE_RESET | BXE_VERBOSE_INTR);
10256         DBPRINT(sc, (BXE_VERBOSE_LOAD | BXE_VERBOSE_RESET | BXE_VERBOSE_INTR),
10257            "%s(): Initializing default status block on port %d, function %d.\n",
10258            __FUNCTION__, port, func);
10259
10260         /* Setup the default status block (DSB). */
10261         section = ((uint64_t)mapping) + offsetof(struct host_def_status_block,
10262             atten_status_block);
10263         def_sb->atten_status_block.status_block_id = sb_id;
10264         sc->attn_state = 0;
10265         sc->def_att_idx = 0;
10266
10267         /*
10268          * Read routing configuration for attn signal
10269          * output of groups. Currently, only groups
10270          * 0 through 3 are wired.
10271          */
10272         reg_offset = port ? MISC_REG_AEU_ENABLE1_FUNC_1_OUT_0 :
10273             MISC_REG_AEU_ENABLE1_FUNC_0_OUT_0;
10274
10275         for (index = 0; index < MAX_DYNAMIC_ATTN_GRPS; index++) {
10276                 sc->attn_group[index].sig[0] = REG_RD(sc, reg_offset +
10277                     0x10 * index);
10278                 sc->attn_group[index].sig[1] = REG_RD(sc, reg_offset +
10279                     0x10 * index + 0x4);
10280                 sc->attn_group[index].sig[2] = REG_RD(sc, reg_offset +
10281                     0x10 * index + 0x8);
10282                 sc->attn_group[index].sig[3] = REG_RD(sc, reg_offset +
10283                     0x10 * index + 0xc);
10284
10285                 DBPRINT(sc, (BXE_VERBOSE_LOAD | BXE_VERBOSE_RESET |
10286                     BXE_VERBOSE_INTR),
10287                     "%s(): attn_group[%d] = 0x%08X 0x%08X 0x%08x 0X%08x\n",
10288                     __FUNCTION__, index, sc->attn_group[index].sig[0],
10289                     sc->attn_group[index].sig[1], sc->attn_group[index].sig[2],
10290                     sc->attn_group[index].sig[3]);
10291         }
10292
10293         reg_offset = port ? HC_REG_ATTN_MSG1_ADDR_L : HC_REG_ATTN_MSG0_ADDR_L;
10294
10295         REG_WR(sc, reg_offset, U64_LO(section));
10296         REG_WR(sc, reg_offset + 4, U64_HI(section));
10297
10298         reg_offset = port ? HC_REG_ATTN_NUM_P1 : HC_REG_ATTN_NUM_P0;
10299
10300         val = REG_RD(sc, reg_offset);
10301         val |= sb_id;
10302         REG_WR(sc, reg_offset, val);
10303
10304         /* USTORM */
10305         section = ((uint64_t)mapping) + offsetof(struct host_def_status_block,
10306             u_def_status_block);
10307         def_sb->u_def_status_block.status_block_id = sb_id;
10308         sc->def_u_idx = 0;
10309
10310         REG_WR(sc, BAR_CSTORM_INTMEM +
10311             CSTORM_DEF_SB_HOST_SB_ADDR_U_OFFSET(func), U64_LO(section));
10312         REG_WR(sc, BAR_CSTORM_INTMEM +
10313             ((CSTORM_DEF_SB_HOST_SB_ADDR_U_OFFSET(func)) + 4), U64_HI(section));
10314         REG_WR8(sc, BAR_CSTORM_INTMEM + DEF_USB_FUNC_OFF +
10315             CSTORM_DEF_SB_HOST_STATUS_BLOCK_U_OFFSET(func), func);
10316
10317         for (index = 0; index < HC_USTORM_DEF_SB_NUM_INDICES; index++)
10318                 REG_WR16(sc, BAR_CSTORM_INTMEM +
10319                     CSTORM_DEF_SB_HC_DISABLE_U_OFFSET(func, index), 1);
10320
10321         /* CSTORM */
10322         section = ((uint64_t)mapping) + offsetof(struct host_def_status_block,
10323             c_def_status_block);
10324         def_sb->c_def_status_block.status_block_id = sb_id;
10325         sc->def_c_idx = 0;
10326
10327         REG_WR(sc, BAR_CSTORM_INTMEM +
10328             CSTORM_DEF_SB_HOST_SB_ADDR_C_OFFSET(func), U64_LO(section));
10329         REG_WR(sc, BAR_CSTORM_INTMEM +
10330             ((CSTORM_DEF_SB_HOST_SB_ADDR_C_OFFSET(func)) + 4), U64_HI(section));
10331         REG_WR8(sc, BAR_CSTORM_INTMEM + DEF_CSB_FUNC_OFF +
10332             CSTORM_DEF_SB_HOST_STATUS_BLOCK_C_OFFSET(func), func);
10333
10334         for (index = 0; index < HC_CSTORM_DEF_SB_NUM_INDICES; index++)
10335                 REG_WR16(sc, BAR_CSTORM_INTMEM +
10336                     CSTORM_DEF_SB_HC_DISABLE_C_OFFSET(func, index), 1);
10337
10338         /* TSTORM */
10339         section = ((uint64_t)mapping) + offsetof(struct host_def_status_block,
10340             t_def_status_block);
10341         def_sb->t_def_status_block.status_block_id = sb_id;
10342         sc->def_t_idx = 0;
10343
10344         REG_WR(sc, BAR_TSTORM_INTMEM +
10345             TSTORM_DEF_SB_HOST_SB_ADDR_OFFSET(func), U64_LO(section));
10346         REG_WR(sc, BAR_TSTORM_INTMEM +
10347             ((TSTORM_DEF_SB_HOST_SB_ADDR_OFFSET(func)) + 4), U64_HI(section));
10348         REG_WR8(sc, BAR_TSTORM_INTMEM + DEF_TSB_FUNC_OFF +
10349             TSTORM_DEF_SB_HOST_STATUS_BLOCK_OFFSET(func), func);
10350
10351         for (index = 0; index < HC_TSTORM_DEF_SB_NUM_INDICES; index++)
10352                 REG_WR16(sc, BAR_TSTORM_INTMEM +
10353                     TSTORM_DEF_SB_HC_DISABLE_OFFSET(func, index), 1);
10354
10355         /* XSTORM */
10356         section = ((uint64_t)mapping) + offsetof(struct host_def_status_block,
10357             x_def_status_block);
10358         def_sb->x_def_status_block.status_block_id = sb_id;
10359         sc->def_x_idx = 0;
10360
10361         REG_WR(sc, BAR_XSTORM_INTMEM +
10362             XSTORM_DEF_SB_HOST_SB_ADDR_OFFSET(func), U64_LO(section));
10363         REG_WR(sc, BAR_XSTORM_INTMEM +
10364             ((XSTORM_DEF_SB_HOST_SB_ADDR_OFFSET(func)) + 4), U64_HI(section));
10365         REG_WR8(sc, BAR_XSTORM_INTMEM + DEF_XSB_FUNC_OFF +
10366             XSTORM_DEF_SB_HOST_STATUS_BLOCK_OFFSET(func), func);
10367
10368         for (index = 0; index < HC_XSTORM_DEF_SB_NUM_INDICES; index++)
10369                 REG_WR16(sc, BAR_XSTORM_INTMEM +
10370                     XSTORM_DEF_SB_HC_DISABLE_OFFSET(func, index), 1);
10371
10372         sc->stats_pending = 0;
10373         sc->set_mac_pending = 0;
10374
10375         bxe_ack_sb(sc, sb_id, CSTORM_ID, 0, IGU_INT_ENABLE, 0);
10376
10377         DBEXIT(BXE_VERBOSE_LOAD | BXE_VERBOSE_RESET | BXE_VERBOSE_INTR);
10378 }
10379
10380 /*
10381  * Update interrupt coalescing parameters.
10382  *
10383  * Returns:
10384  *   None
10385  */
10386 static void
10387 bxe_update_coalesce(struct bxe_softc *sc)
10388 {
10389         int i, port, sb_id;
10390
10391         DBENTER(BXE_VERBOSE_LOAD | BXE_VERBOSE_RESET);
10392
10393         port = BP_PORT(sc);
10394         /* Cycle through each fastpath queue and set the coalescing values. */
10395         for (i = 0; i < sc->num_queues; i++) {
10396                 sb_id = sc->fp[i].sb_id;
10397
10398                 /* Receive interrupt coalescing is done on USTORM. */
10399                 REG_WR8(sc, BAR_CSTORM_INTMEM +
10400                     CSTORM_SB_HC_TIMEOUT_U_OFFSET(port, sb_id,
10401                     U_SB_ETH_RX_CQ_INDEX), sc->rx_ticks / (BXE_BTR * 4));
10402
10403                 REG_WR16(sc, BAR_CSTORM_INTMEM +
10404                     CSTORM_SB_HC_DISABLE_U_OFFSET(port, sb_id,
10405                     U_SB_ETH_RX_CQ_INDEX),
10406                     (sc->rx_ticks / (BXE_BTR * 4)) ? 0 : 1);
10407
10408                 /* Transmit interrupt coalescing is done on CSTORM. */
10409                 REG_WR8(sc, BAR_CSTORM_INTMEM +
10410                     CSTORM_SB_HC_TIMEOUT_C_OFFSET(port, sb_id,
10411                     C_SB_ETH_TX_CQ_INDEX), sc->tx_ticks / (BXE_BTR * 4));
10412                 REG_WR16(sc, BAR_CSTORM_INTMEM +
10413                     CSTORM_SB_HC_DISABLE_C_OFFSET(port, sb_id,
10414                     C_SB_ETH_TX_CQ_INDEX),
10415                     (sc->tx_ticks / (BXE_BTR * 4)) ? 0 : 1);
10416         }
10417
10418         DBEXIT(BXE_VERBOSE_LOAD | BXE_VERBOSE_RESET);
10419 }
10420
10421 /*
10422  * Allocate an mbuf and assign it to the TPA pool.
10423  *
10424  * Returns:
10425  *   0 = Success, !0 = Failure
10426  *
10427  * Modifies:
10428  *   fp->tpa_mbuf_ptr[queue]
10429  *   fp->tpa_mbuf_map[queue]
10430  *   fp->tpa_mbuf_segs[queue]
10431  */
10432 static int
10433 bxe_alloc_tpa_mbuf(struct bxe_fastpath *fp, int queue)
10434 {
10435         struct bxe_softc *sc;
10436         bus_dma_segment_t segs[1];
10437         bus_dmamap_t map;
10438         struct mbuf *m;
10439         int nsegs, rc;
10440
10441         sc = fp->sc;
10442         DBENTER(BXE_INSANE_TPA);
10443         rc = 0;
10444
10445         DBRUNIF((fp->disable_tpa == TRUE),
10446             BXE_PRINTF("%s(): fp[%02d] TPA disabled!\n",
10447             __FUNCTION__, fp->index));
10448
10449 #ifdef BXE_DEBUG
10450         /* Simulate an mbuf allocation failure. */
10451         if (DB_RANDOMTRUE(bxe_debug_mbuf_allocation_failure)) {
10452                 sc->debug_sim_mbuf_alloc_failed++;
10453                 fp->mbuf_tpa_alloc_failed++;
10454                 rc = ENOMEM;
10455                 goto bxe_alloc_tpa_mbuf_exit;
10456         }
10457 #endif
10458
10459         /* Allocate the new TPA mbuf. */
10460         m = m_getjcl(M_DONTWAIT, MT_DATA, M_PKTHDR, sc->mbuf_alloc_size);
10461         if (__predict_false(m == NULL)) {
10462                 fp->mbuf_tpa_alloc_failed++;
10463                 rc = ENOBUFS;
10464                 goto bxe_alloc_tpa_mbuf_exit;
10465         }
10466
10467         DBRUN(fp->tpa_mbuf_alloc++);
10468
10469         /* Initialize the mbuf buffer length. */
10470         m->m_pkthdr.len = m->m_len = sc->mbuf_alloc_size;
10471
10472 #ifdef BXE_DEBUG
10473         /* Simulate an mbuf mapping failure. */
10474         if (DB_RANDOMTRUE(bxe_debug_dma_map_addr_failure)) {
10475                 sc->debug_sim_mbuf_map_failed++;
10476                 fp->mbuf_tpa_mapping_failed++;
10477                 m_freem(m);
10478                 DBRUN(fp->tpa_mbuf_alloc--);
10479                 rc = ENOMEM;
10480                 goto bxe_alloc_tpa_mbuf_exit;
10481         }
10482 #endif
10483
10484         /* Map the TPA mbuf into non-paged pool. */
10485         rc = bus_dmamap_load_mbuf_sg(fp->rx_mbuf_tag,
10486             fp->tpa_mbuf_spare_map, m, segs, &nsegs, BUS_DMA_NOWAIT);
10487         if (__predict_false(rc != 0)) {
10488                 fp->mbuf_tpa_mapping_failed++;
10489                 m_free(m);
10490                 DBRUN(fp->tpa_mbuf_alloc--);
10491                 goto bxe_alloc_tpa_mbuf_exit;
10492         }
10493
10494         /* All mubfs must map to a single segment. */
10495         KASSERT(nsegs == 1, ("%s(): Too many segments (%d) returned!",
10496             __FUNCTION__, nsegs));
10497
10498         /* Release any existing TPA mbuf mapping. */
10499         if (fp->tpa_mbuf_map[queue] != NULL) {
10500                 bus_dmamap_sync(fp->rx_mbuf_tag,
10501                     fp->tpa_mbuf_map[queue], BUS_DMASYNC_POSTREAD);
10502                 bus_dmamap_unload(fp->rx_mbuf_tag,
10503                     fp->tpa_mbuf_map[queue]);
10504         }
10505
10506         /* Save the mbuf and mapping info for the TPA mbuf. */
10507         map = fp->tpa_mbuf_map[queue];
10508         fp->tpa_mbuf_map[queue] = fp->tpa_mbuf_spare_map;
10509         fp->tpa_mbuf_spare_map = map;
10510         bus_dmamap_sync(fp->rx_mbuf_tag,
10511             fp->tpa_mbuf_map[queue], BUS_DMASYNC_PREREAD);
10512         fp->tpa_mbuf_ptr[queue] = m;
10513         fp->tpa_mbuf_segs[queue] = segs[0];
10514
10515 bxe_alloc_tpa_mbuf_exit:
10516         DBEXIT(BXE_INSANE_TPA);
10517         return (rc);
10518 }
10519
10520 /*
10521  * Allocate mbufs for a fastpath TPA pool.
10522  *
10523  * Returns:
10524  *   0 = Success, !0 = Failure.
10525  *
10526  * Modifies:
10527  *   fp->tpa_state[]
10528  *   fp->disable_tpa
10529  */
10530 static int
10531 bxe_fill_tpa_pool(struct bxe_fastpath *fp)
10532 {
10533         struct bxe_softc *sc;
10534         int max_agg_queues, queue, rc;
10535
10536         sc = fp->sc;
10537         DBENTER(BXE_VERBOSE_LOAD | BXE_VERBOSE_RESET);
10538         rc = 0;
10539
10540         if (!TPA_ENABLED(sc)) {
10541                 fp->disable_tpa = TRUE;
10542                 goto bxe_fill_tpa_pool_exit;
10543         }
10544
10545         max_agg_queues = CHIP_IS_E1(sc) ? ETH_MAX_AGGREGATION_QUEUES_E1 :
10546             ETH_MAX_AGGREGATION_QUEUES_E1H;
10547
10548         /* Assume the fill operation worked. */
10549         fp->disable_tpa = FALSE;
10550
10551         /* Fill the TPA pool. */
10552         for (queue = 0; queue < max_agg_queues; queue++) {
10553                 rc = bxe_alloc_tpa_mbuf(fp, queue);
10554                 if (rc != 0) {
10555                         BXE_PRINTF(
10556                             "%s(%d): fp[%02d] TPA disabled!\n",
10557                             __FILE__, __LINE__, fp->index);
10558                         fp->disable_tpa = TRUE;
10559                         break;
10560                 }
10561                 fp->tpa_state[queue] = BXE_TPA_STATE_STOP;
10562         }
10563
10564 bxe_fill_tpa_pool_exit:
10565         DBEXIT(BXE_VERBOSE_LOAD | BXE_VERBOSE_RESET);
10566         return (rc);
10567 }
10568
10569 /*
10570  * Free all mbufs from a fastpath TPA pool.
10571  *
10572  * Returns:
10573  *   None
10574  *
10575  * Modifies:
10576  *   fp->tpa_mbuf_ptr[]
10577  *   fp->tpa_mbuf_map[]
10578  *   fp->tpa_mbuf_alloc
10579  */
10580 static void
10581 bxe_free_tpa_pool(struct bxe_fastpath *fp)
10582 {
10583         struct bxe_softc *sc;
10584         int i, max_agg_queues;
10585
10586         sc = fp->sc;
10587         DBENTER(BXE_INSANE_LOAD | BXE_INSANE_UNLOAD | BXE_INSANE_TPA);
10588
10589         if (fp->rx_mbuf_tag == NULL)
10590                 goto bxe_free_tpa_pool_exit;
10591
10592         max_agg_queues = CHIP_IS_E1H(sc) ?
10593             ETH_MAX_AGGREGATION_QUEUES_E1H :
10594             ETH_MAX_AGGREGATION_QUEUES_E1;
10595
10596         /* Release all mbufs and and all DMA maps in the TPA pool. */
10597         for (i = 0; i < max_agg_queues; i++) {
10598                 if (fp->tpa_mbuf_map[i] != NULL) {
10599                         bus_dmamap_sync(fp->rx_mbuf_tag, fp->tpa_mbuf_map[i],
10600                             BUS_DMASYNC_POSTREAD);
10601                         bus_dmamap_unload(fp->rx_mbuf_tag, fp->tpa_mbuf_map[i]);
10602                 }
10603
10604                 if (fp->tpa_mbuf_ptr[i] != NULL) {
10605                         m_freem(fp->tpa_mbuf_ptr[i]);
10606                         DBRUN(fp->tpa_mbuf_alloc--);
10607                         fp->tpa_mbuf_ptr[i] = NULL;
10608                 }
10609         }
10610
10611 bxe_free_tpa_pool_exit:
10612         DBEXIT(BXE_INSANE_LOAD | BXE_INSANE_UNLOAD | BXE_INSANE_TPA);
10613 }
10614
10615 /*
10616  * Allocate an mbuf and assign it to the receive scatter gather chain.
10617  * The caller must take care to save a copy of the existing mbuf in the
10618  * SG mbuf chain.
10619  *
10620  * Returns:
10621  *   0 = Success, !0= Failure.
10622  *
10623  * Modifies:
10624  *   fp->sg_chain[index]
10625  *   fp->rx_sge_buf_ptr[index]
10626  *   fp->rx_sge_buf_map[index]
10627  *   fp->rx_sge_spare_map
10628  */
10629 static int
10630 bxe_alloc_rx_sge_mbuf(struct bxe_fastpath *fp, uint16_t index)
10631 {
10632         struct bxe_softc *sc;
10633         struct eth_rx_sge *sge;
10634         bus_dma_segment_t segs[1];
10635         bus_dmamap_t map;
10636         struct mbuf *m;
10637         int nsegs, rc;
10638
10639         sc = fp->sc;
10640         DBENTER(BXE_INSANE_TPA);
10641         rc = 0;
10642
10643 #ifdef BXE_DEBUG
10644         /* Simulate an mbuf allocation failure. */
10645         if (DB_RANDOMTRUE(bxe_debug_mbuf_allocation_failure)) {
10646                 sc->debug_sim_mbuf_alloc_failed++;
10647                 fp->mbuf_sge_alloc_failed++;
10648                 rc = ENOMEM;
10649                 goto bxe_alloc_rx_sge_mbuf_exit;
10650         }
10651 #endif
10652
10653         /* Allocate a new SGE mbuf. */
10654         m = m_getjcl(M_DONTWAIT, MT_DATA, M_PKTHDR, SGE_PAGE_SIZE);
10655         if (__predict_false(m == NULL)) {
10656                 fp->mbuf_sge_alloc_failed++;
10657                 rc = ENOMEM;
10658                 goto bxe_alloc_rx_sge_mbuf_exit;
10659         }
10660
10661         DBRUN(fp->sge_mbuf_alloc++);
10662
10663         /* Initialize the mbuf buffer length. */
10664         m->m_pkthdr.len = m->m_len = SGE_PAGE_SIZE;
10665
10666 #ifdef BXE_DEBUG
10667         /* Simulate an mbuf mapping failure. */
10668         if (DB_RANDOMTRUE(bxe_debug_dma_map_addr_failure)) {
10669                 sc->debug_sim_mbuf_map_failed++;
10670                 fp->mbuf_sge_mapping_failed++;
10671                 m_freem(m);
10672                 DBRUN(fp->sge_mbuf_alloc--);
10673                 rc = ENOMEM;
10674                 goto bxe_alloc_rx_sge_mbuf_exit;
10675         }
10676 #endif
10677
10678         /* Map the SGE mbuf into non-paged pool. */
10679         rc = bus_dmamap_load_mbuf_sg(fp->rx_sge_buf_tag,
10680             fp->rx_sge_spare_map, m, segs, &nsegs, BUS_DMA_NOWAIT);
10681         if (__predict_false(rc != 0)) {
10682                 fp->mbuf_sge_mapping_failed++;
10683                 m_freem(m);
10684                 DBRUN(fp->sge_mbuf_alloc--);
10685                 goto bxe_alloc_rx_sge_mbuf_exit;
10686         }
10687
10688         /* All mubfs must map to a single segment. */
10689         KASSERT(nsegs == 1, ("%s(): Too many segments (%d) returned!",
10690             __FUNCTION__, nsegs));
10691
10692         /* Unload any existing SGE mbuf mapping. */
10693         if (fp->rx_sge_buf_map[index] != NULL) {
10694                 bus_dmamap_sync(fp->rx_sge_buf_tag,
10695                     fp->rx_sge_buf_map[index], BUS_DMASYNC_POSTREAD);
10696                 bus_dmamap_unload(fp->rx_sge_buf_tag,
10697                     fp->rx_sge_buf_map[index]);
10698         }
10699
10700         /* Add the new SGE mbuf to the SGE ring. */
10701         map = fp->rx_sge_buf_map[index];
10702         fp->rx_sge_buf_map[index] = fp->rx_sge_spare_map;
10703         fp->rx_sge_spare_map = map;
10704         bus_dmamap_sync(fp->rx_sge_buf_tag,
10705             fp->rx_sge_buf_map[index], BUS_DMASYNC_PREREAD);
10706         fp->rx_sge_buf_ptr[index] = m;
10707         sge = &fp->sg_chain[index];
10708         sge->addr_hi = htole32(U64_HI(segs[0].ds_addr));
10709         sge->addr_lo = htole32(U64_LO(segs[0].ds_addr));
10710
10711 bxe_alloc_rx_sge_mbuf_exit:
10712         DBEXIT(BXE_INSANE_TPA);
10713         return (rc);
10714 }
10715
10716 /*
10717  * Allocate mbufs for a SGE chain.
10718  *
10719  * Returns:
10720  *   0 = Success, !0 = Failure.
10721  *
10722  * Modifies:
10723  *   fp->disable_tpa
10724  *   fp->rx_sge_prod
10725  */
10726 static int
10727 bxe_fill_sg_chain(struct bxe_fastpath *fp)
10728 {
10729         struct bxe_softc *sc;
10730         uint16_t index;
10731         int i, rc;
10732
10733
10734         sc = fp->sc;
10735         DBENTER(BXE_VERBOSE_LOAD | BXE_VERBOSE_RESET);
10736         rc = 0;
10737
10738         if (!TPA_ENABLED(sc)) {
10739                 fp->disable_tpa = TRUE;
10740                 goto bxe_fill_sg_chain_exit;
10741         }
10742
10743         /* Assume the fill operation works. */
10744         fp->disable_tpa = FALSE;
10745
10746         /* Fill the RX SGE chain. */
10747         index = 0;
10748         for (i = 0; i < USABLE_RX_SGE; i++) {
10749                 rc = bxe_alloc_rx_sge_mbuf(fp, index);
10750                 if (rc != 0) {
10751                         BXE_PRINTF(
10752                         "%s(%d): fp[%02d] SGE memory allocation failure!\n",
10753                             __FILE__, __LINE__, fp->index);
10754                         index = 0;
10755                         fp->disable_tpa = TRUE;
10756                         break;
10757                 }
10758                 index = NEXT_SGE_IDX(index);
10759         }
10760
10761         /* Update the driver's copy of the RX SGE producer index. */
10762         fp->rx_sge_prod = index;
10763
10764 bxe_fill_sg_chain_exit:
10765         DBEXIT(BXE_VERBOSE_LOAD | BXE_VERBOSE_RESET);
10766         return (rc);
10767 }
10768
10769 /*
10770  * Free all elements from the receive scatter gather chain.
10771  *
10772  * Returns:
10773  *   None
10774  *
10775  * Modifies:
10776  *   fp->rx_sge_buf_ptr[]
10777  *   fp->rx_sge_buf_map[]
10778  *   fp->sge_mbuf_alloc
10779  */
10780 static void
10781 bxe_free_sg_chain(struct bxe_fastpath *fp)
10782 {
10783         struct bxe_softc *sc;
10784         int i;
10785
10786         sc = fp->sc;
10787         DBENTER(BXE_INSANE_TPA);
10788
10789         if (fp->rx_sge_buf_tag == NULL)
10790                 goto bxe_free_sg_chain_exit;
10791
10792         /* Free all mbufs and unload all maps. */
10793         for (i = 0; i < TOTAL_RX_SGE; i++) {
10794                 /* Free the map and the mbuf if they're allocated. */
10795                 if (fp->rx_sge_buf_map[i] != NULL) {
10796                         bus_dmamap_sync(fp->rx_sge_buf_tag,
10797                             fp->rx_sge_buf_map[i], BUS_DMASYNC_POSTREAD);
10798                         bus_dmamap_unload(fp->rx_sge_buf_tag,
10799                             fp->rx_sge_buf_map[i]);
10800                 }
10801
10802                 if (fp->rx_sge_buf_ptr[i] != NULL) {
10803                         m_freem(fp->rx_sge_buf_ptr[i]);
10804                         DBRUN(fp->sge_mbuf_alloc--);
10805                         fp->rx_sge_buf_ptr[i] = NULL;
10806                 }
10807         }
10808
10809 bxe_free_sg_chain_exit:
10810         DBEXIT(BXE_INSANE_TPA);
10811 }
10812
10813 /*
10814  * Allocate an mbuf, if necessary, and add it to the receive chain.
10815  *
10816  * Returns:
10817  *   0 = Success, !0 = Failure.
10818  */
10819 static int
10820 bxe_alloc_rx_bd_mbuf(struct bxe_fastpath *fp, uint16_t index)
10821 {
10822         struct bxe_softc *sc;
10823         struct eth_rx_bd *rx_bd;
10824         bus_dma_segment_t segs[1];
10825         bus_dmamap_t map;
10826         struct mbuf *m;
10827         int nsegs, rc;
10828
10829         sc = fp->sc;
10830         DBENTER(BXE_INSANE_LOAD | BXE_INSANE_RESET | BXE_INSANE_RECV);
10831         rc = 0;
10832
10833 #ifdef BXE_DEBUG
10834         /* Simulate an mbuf allocation failure. */
10835         if (DB_RANDOMTRUE(bxe_debug_mbuf_allocation_failure)) {
10836                 sc->debug_sim_mbuf_alloc_failed++;
10837                 fp->mbuf_rx_bd_alloc_failed++;
10838                 rc = ENOMEM;
10839                 goto bxe_alloc_rx_bd_mbuf_exit;
10840         }
10841 #endif
10842
10843         /* Allocate the new RX BD mbuf. */
10844         m = m_getjcl(M_DONTWAIT, MT_DATA, M_PKTHDR, sc->mbuf_alloc_size);
10845         if (__predict_false(m == NULL)) {
10846                 fp->mbuf_rx_bd_alloc_failed++;
10847                 rc = ENOBUFS;
10848                 goto bxe_alloc_rx_bd_mbuf_exit;
10849         }
10850
10851         DBRUN(fp->rx_mbuf_alloc++);
10852
10853         /* Initialize the mbuf buffer length. */
10854         m->m_pkthdr.len = m->m_len = sc->mbuf_alloc_size;
10855
10856 #ifdef BXE_DEBUG
10857         /* Simulate an mbuf mapping failure. */
10858         if (DB_RANDOMTRUE(bxe_debug_dma_map_addr_failure)) {
10859                 sc->debug_sim_mbuf_map_failed++;
10860                 fp->mbuf_rx_bd_mapping_failed++;
10861                 m_freem(m);
10862                 DBRUN(fp->rx_mbuf_alloc--);
10863                 rc = ENOMEM;
10864                 goto bxe_alloc_rx_bd_mbuf_exit;
10865         }
10866 #endif
10867
10868         /* Map the TPA mbuf into non-paged pool. */
10869         rc = bus_dmamap_load_mbuf_sg(fp->rx_mbuf_tag,
10870             fp->rx_mbuf_spare_map, m, segs, &nsegs, BUS_DMA_NOWAIT);
10871         if (__predict_false(rc != 0)) {
10872                 fp->mbuf_rx_bd_mapping_failed++;
10873                 m_freem(m);
10874                 DBRUN(fp->rx_mbuf_alloc--);
10875                 goto bxe_alloc_rx_bd_mbuf_exit;
10876         }
10877
10878         /* All mubfs must map to a single segment. */
10879         KASSERT(nsegs == 1, ("%s(): Too many segments (%d) returned!",
10880             __FUNCTION__, nsegs));
10881
10882         /* Release any existing RX BD mbuf mapping. */
10883         if (fp->rx_mbuf_map[index] != NULL) {
10884                 bus_dmamap_sync(fp->rx_mbuf_tag,
10885                     fp->rx_mbuf_map[index], BUS_DMASYNC_POSTREAD);
10886                 bus_dmamap_unload(fp->rx_mbuf_tag,
10887                     fp->rx_mbuf_map[index]);
10888         }
10889
10890         /* Save the mbuf and mapping info. */
10891         map = fp->rx_mbuf_map[index];
10892         fp->rx_mbuf_map[index] = fp->rx_mbuf_spare_map;
10893         fp->rx_mbuf_spare_map = map;
10894         bus_dmamap_sync(fp->rx_mbuf_tag,
10895             fp->rx_mbuf_map[index], BUS_DMASYNC_PREREAD);
10896         fp->rx_mbuf_ptr[index] = m;
10897         rx_bd = &fp->rx_chain[index];
10898         rx_bd->addr_hi  = htole32(U64_HI(segs[0].ds_addr));
10899         rx_bd->addr_lo  = htole32(U64_LO(segs[0].ds_addr));
10900
10901 bxe_alloc_rx_bd_mbuf_exit:
10902         DBEXIT(BXE_INSANE_LOAD | BXE_INSANE_RESET | BXE_INSANE_RECV);
10903         return (rc);
10904 }
10905
10906
10907
10908 /*
10909  * Allocate mbufs for a receive  chain.
10910  *
10911  * Returns:
10912  *   0 = Success, !0 = Failure.
10913  *
10914  * Modifies:
10915  *   fp->rx_bd_prod
10916  */
10917 static int
10918 bxe_fill_rx_bd_chain(struct bxe_fastpath *fp)
10919 {
10920         struct bxe_softc *sc;
10921         uint16_t index;
10922         int i, rc;
10923
10924         sc = fp->sc;
10925         DBENTER(BXE_VERBOSE_LOAD | BXE_VERBOSE_RESET);
10926         rc = index = 0;
10927
10928         /* Allocate buffers for all the RX BDs in RX BD Chain. */
10929         for (i = 0; i < USABLE_RX_BD; i++) {
10930                 rc = bxe_alloc_rx_bd_mbuf(fp, index);
10931                 if (rc != 0) {
10932                         BXE_PRINTF(
10933         "%s(%d): Memory allocation failure! Cannot fill fp[%02d] RX chain.\n",
10934                             __FILE__, __LINE__, fp->index);
10935                         index = 0;
10936                         break;
10937                 }
10938                 index = NEXT_RX_BD(index);
10939         }
10940
10941         fp->rx_bd_prod = index;
10942         DBEXIT(BXE_VERBOSE_LOAD | BXE_VERBOSE_RESET);
10943         return (rc);
10944 }
10945
10946 /*
10947  * Free all buffers from the receive chain.
10948  *
10949  * Returns:
10950  *   None
10951  *
10952  * Modifies:
10953  *   fp->rx_mbuf_ptr[]
10954  *   fp->rx_mbuf_map[]
10955  *   fp->rx_mbuf_alloc
10956  */
10957 static void
10958 bxe_free_rx_bd_chain(struct bxe_fastpath *fp)
10959 {
10960         struct bxe_softc *sc;
10961         int i;
10962
10963         sc = fp->sc;
10964         DBENTER(BXE_VERBOSE_LOAD | BXE_VERBOSE_RESET);
10965
10966         if (fp->rx_mbuf_tag == NULL)
10967                 goto bxe_free_rx_bd_chain_exit;
10968
10969         /* Free all mbufs and unload all maps. */
10970         for (i = 0; i < TOTAL_RX_BD; i++) {
10971                 if (fp->rx_mbuf_map[i] != NULL) {
10972                         bus_dmamap_sync(fp->rx_mbuf_tag, fp->rx_mbuf_map[i],
10973                             BUS_DMASYNC_POSTREAD);
10974                         bus_dmamap_unload(fp->rx_mbuf_tag, fp->rx_mbuf_map[i]);
10975                 }
10976
10977                 if (fp->rx_mbuf_ptr[i] != NULL) {
10978                         m_freem(fp->rx_mbuf_ptr[i]);
10979                         DBRUN(fp->rx_mbuf_alloc--);
10980                         fp->rx_mbuf_ptr[i] = NULL;
10981                 }
10982         }
10983
10984 bxe_free_rx_bd_chain_exit:
10985         DBEXIT(BXE_VERBOSE_LOAD | BXE_VERBOSE_RESET);
10986 }
10987
10988 /*
10989  * Setup mutexes used by the driver.
10990  *
10991  * Returns:
10992  *   None.
10993  */
10994 static void
10995 bxe_mutexes_alloc(struct bxe_softc *sc)
10996 {
10997         struct bxe_fastpath *fp;
10998         int i;
10999
11000         DBENTER(BXE_VERBOSE_LOAD);
11001
11002         BXE_CORE_LOCK_INIT(sc, device_get_nameunit(sc->dev));
11003         BXE_SP_LOCK_INIT(sc, "bxe_sp_lock");
11004         BXE_DMAE_LOCK_INIT(sc, "bxe_dmae_lock");
11005         BXE_PHY_LOCK_INIT(sc, "bxe_phy_lock");
11006         BXE_FWMB_LOCK_INIT(sc, "bxe_fwmb_lock");
11007         BXE_PRINT_LOCK_INIT(sc, "bxe_print_lock");
11008
11009         /* Allocate one mutex for each fastpath structure. */
11010         for (i = 0; i < sc->num_queues; i++ ) {
11011                 fp = &sc->fp[i];
11012
11013                 /* Allocate per fastpath mutexes. */
11014                 snprintf(fp->mtx_name, sizeof(fp->mtx_name), "%s:fp[%02d]",
11015                     device_get_nameunit(sc->dev), fp->index);
11016                 mtx_init(&fp->mtx, fp->mtx_name, NULL, MTX_DEF);
11017         }
11018
11019         DBEXIT(BXE_VERBOSE_LOAD);
11020 }
11021
11022 /*
11023  * Free mutexes used by the driver.
11024  *
11025  * Returns:
11026  *   None.
11027  */
11028 static void
11029 bxe_mutexes_free(struct bxe_softc *sc)
11030 {
11031         struct bxe_fastpath *fp;
11032         int i;
11033
11034         DBENTER(BXE_VERBOSE_UNLOAD);
11035
11036         for (i = 0; i < sc->num_queues; i++ ) {
11037                 fp = &sc->fp[i];
11038
11039                 /* Release per fastpath mutexes. */
11040                 if (mtx_initialized(&fp->mtx))
11041                         mtx_destroy(&fp->mtx);
11042         }
11043
11044         BXE_PRINT_LOCK_DESTROY(sc);
11045         BXE_FWMB_LOCK_DESTROY(sc);
11046         BXE_PHY_LOCK_DESTROY(sc);
11047         BXE_DMAE_LOCK_DESTROY(sc);
11048         BXE_SP_LOCK_DESTROY(sc);
11049         BXE_CORE_LOCK_DESTROY(sc);
11050
11051         DBEXIT(BXE_VERBOSE_UNLOAD);
11052
11053 }
11054
11055 /*
11056  * Free memory and clear the RX data structures.
11057  *
11058  * Returns:
11059  *   Nothing.
11060  */
11061 static void
11062 bxe_clear_rx_chains(struct bxe_softc *sc)
11063 {
11064         struct bxe_fastpath *fp;
11065         int i;
11066
11067         DBENTER(BXE_VERBOSE_RESET);
11068
11069         for (i = 0; i < sc->num_queues; i++) {
11070                 fp = &sc->fp[i];
11071
11072                 /* Free all RX buffers. */
11073                 bxe_free_rx_bd_chain(fp);
11074                 bxe_free_tpa_pool(fp);
11075                 bxe_free_sg_chain(fp);
11076
11077                 /* Check if any mbufs lost in the process. */
11078                 DBRUNIF((fp->tpa_mbuf_alloc), DBPRINT(sc, BXE_FATAL,
11079                 "%s(): Memory leak! Lost %d mbufs from fp[%02d] TPA pool!\n",
11080                     __FUNCTION__, fp->tpa_mbuf_alloc, fp->index));
11081                 DBRUNIF((fp->sge_mbuf_alloc), DBPRINT(sc, BXE_FATAL,
11082                 "%s(): Memory leak! Lost %d mbufs from fp[%02d] SGE chain!\n",
11083                     __FUNCTION__, fp->sge_mbuf_alloc, fp->index));
11084                 DBRUNIF((fp->rx_mbuf_alloc), DBPRINT(sc, BXE_FATAL,
11085                 "%s(): Memory leak! Lost %d mbufs from fp[%02d] RX chain!\n",
11086                     __FUNCTION__, fp->rx_mbuf_alloc, fp->index));
11087         }
11088
11089         DBEXIT(BXE_VERBOSE_RESET);
11090 }
11091
11092 /*
11093  * Initialize the receive rings.
11094  *
11095  * Returns:
11096  *   None.
11097  */
11098 static int
11099 bxe_init_rx_chains(struct bxe_softc *sc)
11100 {
11101         struct bxe_fastpath *fp;
11102         int func, i, rc;
11103
11104         DBENTER(BXE_VERBOSE_LOAD | BXE_VERBOSE_RESET);
11105         rc = 0;
11106         func = BP_FUNC(sc);
11107
11108         /* Allocate memory for RX and CQ chains. */
11109         for (i = 0; i < sc->num_queues; i++) {
11110                 fp = &sc->fp[i];
11111                 DBPRINT(sc, (BXE_VERBOSE_LOAD | BXE_VERBOSE_RESET),
11112                     "%s(): Initializing fp[%02d] RX chain.\n", __FUNCTION__, i);
11113
11114                 fp->rx_bd_cons = fp->rx_bd_prod = 0;
11115                 fp->rx_cq_cons = fp->rx_cq_prod = 0;
11116
11117                 /* Pointer to status block's CQ consumer index. */
11118                 fp->rx_cq_cons_sb = &fp->status_block->
11119                     u_status_block.index_values[HC_INDEX_U_ETH_RX_CQ_CONS];
11120
11121                 /* Pointer to status block's receive consumer index. */
11122                 fp->rx_bd_cons_sb = &fp->status_block->
11123                     u_status_block.index_values[HC_INDEX_U_ETH_RX_BD_CONS];
11124
11125                 fp->rx_cq_prod = TOTAL_RCQ_ENTRIES;
11126                 fp->rx_pkts = fp->rx_tpa_pkts = fp->rx_soft_errors = 0;
11127
11128                 /* Allocate memory for the receive chain. */
11129                 rc = bxe_fill_rx_bd_chain(fp);
11130                 if (rc != 0)
11131                         goto bxe_init_rx_chains_exit;
11132
11133                 /* Allocate memory for TPA pool. */
11134                 rc = bxe_fill_tpa_pool(fp);
11135                 if (rc != 0)
11136                         goto bxe_init_rx_chains_exit;
11137
11138                 /* Allocate memory for scatter-gather chain. */
11139                 rc = bxe_fill_sg_chain(fp);
11140                 if (rc != 0)
11141                         goto bxe_init_rx_chains_exit;
11142
11143                 /* Prepare the receive BD and CQ buffers for DMA access. */
11144                 bus_dmamap_sync(fp->rx_dma.tag, fp->rx_dma.map,
11145                      BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
11146
11147                 bus_dmamap_sync(fp->rcq_dma.tag, fp->rcq_dma.map,
11148                      BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
11149
11150                 /*
11151                  * Tell the controller that we have rx_bd's and CQE's
11152                  * available.  Warning! this will generate an interrupt
11153                  * (to the TSTORM).  This must only be done when the
11154                  * controller is initialized.
11155                  */
11156                 bxe_update_rx_prod(sc, fp, fp->rx_bd_prod,
11157                     fp->rx_cq_prod, fp->rx_sge_prod);
11158
11159                 /* ToDo - Move to dma_alloc(). */
11160                 /*
11161                  * Tell controller where the receive CQ
11162                  * chains start in physical memory.
11163                  */
11164                 if (i == 0) {
11165                         REG_WR(sc, BAR_USTORM_INTMEM +
11166                             USTORM_MEM_WORKAROUND_ADDRESS_OFFSET(func),
11167                             U64_LO(fp->rcq_dma.paddr));
11168                         REG_WR(sc, BAR_USTORM_INTMEM +
11169                             USTORM_MEM_WORKAROUND_ADDRESS_OFFSET(func) + 4,
11170                             U64_HI(fp->rcq_dma.paddr));
11171                 }
11172         }
11173
11174 bxe_init_rx_chains_exit:
11175         /* Release memory if an error occurred. */
11176         if (rc != 0)
11177                 bxe_clear_rx_chains(sc);
11178
11179         DBEXIT(BXE_VERBOSE_LOAD | BXE_VERBOSE_RESET);
11180         return (rc);
11181 }
11182
11183 /*
11184  * Free memory and clear the TX data structures.
11185  *
11186  * Returns:
11187  *   Nothing.
11188  */
11189 static void
11190 bxe_clear_tx_chains(struct bxe_softc *sc)
11191 {
11192         struct bxe_fastpath *fp;
11193         int i, j;
11194
11195         DBENTER(BXE_VERBOSE_RESET);
11196
11197         for (i = 0; i < sc->num_queues; i++) {
11198                 fp = &sc->fp[i];
11199
11200                 /* Free all mbufs and unload all maps. */
11201                 if (fp->tx_mbuf_tag) {
11202                         for (j = 0; j < TOTAL_TX_BD; j++) {
11203                                 if (fp->tx_mbuf_ptr[j] != NULL) {
11204                                         bus_dmamap_sync(fp->tx_mbuf_tag,
11205                                             fp->tx_mbuf_map[j],
11206                                             BUS_DMASYNC_POSTWRITE);
11207                                         bus_dmamap_unload(fp->tx_mbuf_tag,
11208                                             fp->tx_mbuf_map[j]);
11209                                         m_freem(fp->tx_mbuf_ptr[j]);
11210                                         fp->tx_mbuf_alloc--;
11211                                         fp->tx_mbuf_ptr[j] = NULL;
11212                                 }
11213                         }
11214                 }
11215
11216                 /* Check if we lost any mbufs in the process. */
11217                 DBRUNIF((fp->tx_mbuf_alloc), DBPRINT(sc, BXE_FATAL,
11218         "%s(): Memory leak! Lost %d mbufs from fp[%02d] TX chain!\n",
11219                     __FUNCTION__, fp->tx_mbuf_alloc, fp->index));
11220         }
11221
11222         DBEXIT(BXE_VERBOSE_RESET);
11223 }
11224
11225 /*
11226  * Initialize the transmit chain.
11227  *
11228  * Returns:
11229  *   None.
11230  */
11231 static void
11232 bxe_init_tx_chains(struct bxe_softc *sc)
11233 {
11234         struct bxe_fastpath *fp;
11235         int i, j;
11236
11237         DBENTER(BXE_VERBOSE_LOAD | BXE_VERBOSE_RESET);
11238
11239         for (i = 0; i < sc->num_queues; i++) {
11240                 fp = &sc->fp[i];
11241
11242                 /* Initialize transmit doorbell. */
11243                 fp->tx_db.data.header.header = DOORBELL_HDR_DB_TYPE;
11244                 fp->tx_db.data.zero_fill1 = 0;
11245                 fp->tx_db.data.prod = 0;
11246
11247                 /* Initialize tranmsit producer/consumer indices. */
11248                 fp->tx_pkt_prod = fp->tx_pkt_cons = 0;
11249                 fp->tx_bd_prod  = fp->tx_bd_cons  = 0;
11250                 fp->tx_bd_used  = 0;
11251
11252                 /* Pointer to TX packet consumer in status block. */
11253                 fp->tx_pkt_cons_sb =
11254                     &fp->status_block->c_status_block.index_values[C_SB_ETH_TX_CQ_INDEX];
11255
11256                 /* Soft TX counters. */
11257                 fp->tx_pkts = 0;
11258                 fp->tx_soft_errors = 0;
11259                 fp->tx_offload_frames_csum_ip = 0;
11260                 fp->tx_offload_frames_csum_tcp = 0;
11261                 fp->tx_offload_frames_csum_udp = 0;
11262                 fp->tx_offload_frames_tso = 0;
11263                 fp->tx_header_splits = 0;
11264                 fp->tx_encap_failures = 0;
11265                 fp->tx_hw_queue_full = 0;
11266                 fp->tx_hw_max_queue_depth = 0;
11267                 fp->tx_dma_mapping_failure = 0;
11268                 fp->tx_max_drbr_queue_depth = 0;
11269                 fp->tx_window_violation_std = 0;
11270                 fp->tx_window_violation_tso = 0;
11271                 fp->tx_unsupported_tso_request_ipv6 = 0;
11272                 fp->tx_unsupported_tso_request_not_tcp = 0;
11273                 fp->tx_chain_lost_mbuf = 0;
11274                 fp->tx_frame_deferred = 0;
11275                 fp->tx_queue_xoff = 0;
11276
11277                 /* Clear all TX mbuf pointers. */
11278                 for (j = 0; j < TOTAL_TX_BD; j++) {
11279                         fp->tx_mbuf_ptr[j] = NULL;
11280                 }
11281         }
11282
11283         DBEXIT(BXE_VERBOSE_LOAD | BXE_VERBOSE_RESET);
11284 }
11285
11286 /*
11287  * Initialize the slowpath ring.
11288  *
11289  * Returns:
11290  *   None.
11291  */
11292 static void
11293 bxe_init_sp_ring(struct bxe_softc *sc)
11294 {
11295         int func;
11296
11297         func = BP_FUNC(sc);
11298
11299         DBENTER(BXE_VERBOSE_LOAD | BXE_VERBOSE_RESET);
11300
11301         bzero((char *)sc->slowpath, BXE_SLOWPATH_SZ);
11302
11303         /* When the producer equals the consumer the chain is empty. */
11304         sc->spq_left = MAX_SPQ_PENDING;
11305         sc->spq_prod_idx = 0;
11306         sc->dsb_sp_prod = BXE_SP_DSB_INDEX;
11307         sc->spq_prod_bd = sc->spq;
11308         sc->spq_last_bd = sc->spq_prod_bd + MAX_SP_DESC_CNT;
11309
11310         /* Tell the controller the address of the slowpath ring. */
11311         REG_WR(sc, XSEM_REG_FAST_MEMORY + XSTORM_SPQ_PAGE_BASE_OFFSET(func),
11312             U64_LO(sc->spq_dma.paddr));
11313         REG_WR(sc, XSEM_REG_FAST_MEMORY + XSTORM_SPQ_PAGE_BASE_OFFSET(func) + 4,
11314             U64_HI(sc->spq_dma.paddr));
11315         REG_WR(sc, XSEM_REG_FAST_MEMORY + XSTORM_SPQ_PROD_OFFSET(func),
11316             sc->spq_prod_idx);
11317
11318         DBEXIT(BXE_VERBOSE_LOAD | BXE_VERBOSE_RESET);
11319 }
11320
11321 /*
11322  * Initialize STORM processor context.
11323  *
11324  * Returns:
11325  *   None.
11326  */
11327 static void
11328 bxe_init_context(struct bxe_softc *sc)
11329 {
11330         struct eth_context *context;
11331         struct bxe_fastpath *fp;
11332         uint8_t sb_id;
11333         uint8_t cl_id;
11334         int i;
11335
11336         DBENTER(BXE_VERBOSE_LOAD | BXE_VERBOSE_RESET);
11337
11338         for (i = 0; i < sc->num_queues; i++) {
11339                 context = BXE_SP(sc, context[i].eth);
11340                 fp = &sc->fp[i];
11341                 sb_id = fp->sb_id;
11342                 cl_id = fp->cl_id;
11343
11344                 /* Update the USTORM context. */
11345                 context->ustorm_st_context.common.sb_index_numbers =
11346                     BXE_RX_SB_INDEX_NUM;
11347                 context->ustorm_st_context.common.clientId = cl_id;
11348                 context->ustorm_st_context.common.status_block_id = sb_id;
11349                 /* Enable packet alignment/pad and statistics. */
11350                 context->ustorm_st_context.common.flags =
11351                     USTORM_ETH_ST_CONTEXT_CONFIG_ENABLE_MC_ALIGNMENT;
11352                 if (sc->stats_enable == TRUE)
11353                         context->ustorm_st_context.common.flags |=
11354                     USTORM_ETH_ST_CONTEXT_CONFIG_ENABLE_STATISTICS;
11355                 context->ustorm_st_context.common.statistics_counter_id=cl_id;
11356                 /*
11357                  * Set packet alignment boundary.
11358                  * (Must be >= 4 (i.e. 16 bytes).)
11359                  */
11360                 context->ustorm_st_context.common.mc_alignment_log_size = 8;
11361                 /* Set the size of the receive buffers. */
11362                 context->ustorm_st_context.common.bd_buff_size =
11363                     sc->mbuf_alloc_size;
11364
11365                 /* Set the address of the receive chain base page. */
11366                 context->ustorm_st_context.common.bd_page_base_hi =
11367                     U64_HI(fp->rx_dma.paddr);
11368                 context->ustorm_st_context.common.bd_page_base_lo =
11369                     U64_LO(fp->rx_dma.paddr);
11370
11371                 if (TPA_ENABLED(sc) && (fp->disable_tpa == FALSE)) {
11372                         /* Enable TPA and SGE chain support. */
11373                         context->ustorm_st_context.common.flags |=
11374                             USTORM_ETH_ST_CONTEXT_CONFIG_ENABLE_TPA;
11375
11376                         /* Set the size of the SGE buffer. */
11377                         context->ustorm_st_context.common.sge_buff_size =
11378                             (uint16_t) (SGE_PAGE_SIZE * PAGES_PER_SGE);
11379
11380                         /* Set the address of the SGE chain base page. */
11381                         context->ustorm_st_context.common.sge_page_base_hi =
11382                             U64_HI(fp->sg_dma.paddr);
11383                         context->ustorm_st_context.common.sge_page_base_lo =
11384                             U64_LO(fp->sg_dma.paddr);
11385
11386                         DBPRINT(sc, BXE_VERBOSE_TPA, "%s(): MTU = %d\n",
11387                             __FUNCTION__, (int) sc->bxe_ifp->if_mtu);
11388
11389                         /* Describe MTU to SGE alignment. */
11390                         context->ustorm_st_context.common.max_sges_for_packet =
11391                             SGE_PAGE_ALIGN(sc->bxe_ifp->if_mtu) >>
11392                             SGE_PAGE_SHIFT;
11393                         context->ustorm_st_context.common.max_sges_for_packet =
11394                             ((context->ustorm_st_context.common.
11395                             max_sges_for_packet + PAGES_PER_SGE - 1) &
11396                             (~(PAGES_PER_SGE - 1))) >> PAGES_PER_SGE_SHIFT;
11397
11398                         DBPRINT(sc, BXE_VERBOSE_TPA,
11399                             "%s(): max_sges_for_packet = %d\n", __FUNCTION__,
11400                             context->ustorm_st_context.common.max_sges_for_packet);
11401                 }
11402
11403                 /* Update USTORM context. */
11404                 context->ustorm_ag_context.cdu_usage =
11405                     CDU_RSRVD_VALUE_TYPE_A(HW_CID(sc, i),
11406                     CDU_REGION_NUMBER_UCM_AG, ETH_CONNECTION_TYPE);
11407
11408                 /* Update XSTORM context. */
11409                 context->xstorm_ag_context.cdu_reserved =
11410                     CDU_RSRVD_VALUE_TYPE_A(HW_CID(sc, i),
11411                     CDU_REGION_NUMBER_XCM_AG, ETH_CONNECTION_TYPE);
11412
11413                 /* Set the address of the transmit chain base page. */
11414                 context->xstorm_st_context.tx_bd_page_base_hi =
11415                     U64_HI(fp->tx_dma.paddr);
11416                 context->xstorm_st_context.tx_bd_page_base_lo =
11417                     U64_LO(fp->tx_dma.paddr);
11418
11419                 /* Enable XSTORM statistics. */
11420                 context->xstorm_st_context.statistics_data = (cl_id |
11421                     XSTORM_ETH_ST_CONTEXT_STATISTICS_ENABLE);
11422
11423                 /* Update CSTORM status block configuration. */
11424                 context->cstorm_st_context.sb_index_number =
11425                     C_SB_ETH_TX_CQ_INDEX;
11426                 context->cstorm_st_context.status_block_id = sb_id;
11427         }
11428
11429         DBEXIT(BXE_VERBOSE_LOAD | BXE_VERBOSE_RESET);
11430 }
11431
11432 /*
11433  * Initialize indirection table.
11434  *
11435  * Returns:
11436  *   None.
11437  */
11438 static void
11439 bxe_init_ind_table(struct bxe_softc *sc)
11440 {
11441         int func, i;
11442
11443         func = BP_FUNC(sc);
11444
11445         DBENTER(BXE_VERBOSE_LOAD | BXE_VERBOSE_RESET);
11446
11447         if (sc->multi_mode == ETH_RSS_MODE_DISABLED)
11448                 return;
11449
11450         /* Initialize the indirection table. */
11451         for (i = 0; i < TSTORM_INDIRECTION_TABLE_SIZE; i++)
11452                 REG_WR8(sc, BAR_TSTORM_INTMEM +
11453                     TSTORM_INDIRECTION_TABLE_OFFSET(func) + i,
11454                     sc->fp->cl_id + (i % sc->num_queues));
11455
11456         DBEXIT(BXE_VERBOSE_LOAD | BXE_VERBOSE_RESET);
11457 }
11458
11459 /*
11460  * Set client configuration.
11461  *
11462  * Returns:
11463  *   None.
11464  */
11465 static void
11466 bxe_set_client_config(struct bxe_softc *sc)
11467 {
11468         struct tstorm_eth_client_config tstorm_client = {0};
11469         int i, port;
11470
11471         port = BP_PORT(sc);
11472
11473         DBENTER(BXE_VERBOSE_MISC);
11474
11475         tstorm_client.mtu = sc->bxe_ifp->if_mtu; /* ETHERMTU */
11476         tstorm_client.config_flags =
11477             (TSTORM_ETH_CLIENT_CONFIG_STATSITICS_ENABLE |
11478             TSTORM_ETH_CLIENT_CONFIG_E1HOV_REM_ENABLE);
11479
11480         /* Unconditionally enable VLAN tag stripping. */
11481         if (sc->rx_mode) {
11482                 tstorm_client.config_flags |=
11483                     TSTORM_ETH_CLIENT_CONFIG_VLAN_REM_ENABLE;
11484                 DBPRINT(sc, BXE_VERBOSE, "%s(): VLAN tag stripping enabled.\n",
11485                     __FUNCTION__);
11486         }
11487
11488         /* Initialize the receive mode for each receive queue. */
11489         for (i = 0; i < sc->num_queues; i++) {
11490                 tstorm_client.statistics_counter_id = sc->fp[i].cl_id;
11491
11492                 REG_WR(sc, BAR_TSTORM_INTMEM +
11493                     TSTORM_CLIENT_CONFIG_OFFSET(port, sc->fp[i].cl_id),
11494                     ((uint32_t *) &tstorm_client)[0]);
11495                 REG_WR(sc, BAR_TSTORM_INTMEM +
11496                     TSTORM_CLIENT_CONFIG_OFFSET(port, sc->fp[i].cl_id) + 4,
11497                     ((uint32_t *) &tstorm_client)[1]);
11498         }
11499
11500         DBEXIT(BXE_VERBOSE_MISC);
11501 }
11502
11503 /*
11504  * Set receive mode.
11505  *
11506  * Programs the MAC according to the type of unicast/broadcast/multicast
11507  * packets it should receive.
11508  *
11509  * Returns:
11510  *   None.
11511  */
11512 static void
11513 bxe_set_storm_rx_mode(struct bxe_softc *sc)
11514 {
11515         struct tstorm_eth_mac_filter_config tstorm_mac_filter = {0};
11516         uint32_t llh_mask;
11517         int mode, mask;
11518         int func, i , port;
11519
11520         DBENTER(BXE_VERBOSE_LOAD | BXE_VERBOSE_RESET);
11521
11522         mode = sc->rx_mode;
11523         mask = 1 << BP_L_ID(sc);
11524         func = BP_FUNC(sc);
11525         port = BP_PORT(sc);
11526
11527         /* All but management unicast packets should pass to the host as well */
11528         llh_mask = NIG_LLH0_BRB1_DRV_MASK_REG_LLH0_BRB1_DRV_MASK_BRCST |
11529             NIG_LLH0_BRB1_DRV_MASK_REG_LLH0_BRB1_DRV_MASK_MLCST |
11530             NIG_LLH0_BRB1_DRV_MASK_REG_LLH0_BRB1_DRV_MASK_VLAN |
11531             NIG_LLH0_BRB1_DRV_MASK_REG_LLH0_BRB1_DRV_MASK_NO_VLAN;
11532
11533         /* Set the individual accept/drop flags based on the receive mode. */
11534         switch (mode) {
11535         case BXE_RX_MODE_NONE:
11536                 /* Drop everything. */
11537                 DBPRINT(sc, BXE_VERBOSE,
11538                     "%s(): Setting RX_MODE_NONE for function %d.\n",
11539                     __FUNCTION__, func);
11540                 tstorm_mac_filter.ucast_drop_all = mask;
11541                 tstorm_mac_filter.mcast_drop_all = mask;
11542                 tstorm_mac_filter.bcast_drop_all = mask;
11543                 break;
11544         case BXE_RX_MODE_NORMAL:
11545                 /* Accept all broadcast frames. */
11546                 DBPRINT(sc, BXE_VERBOSE,
11547                     "%s(): Setting RX_MODE_NORMAL for function %d.\n",
11548                     __FUNCTION__, func);
11549                 tstorm_mac_filter.bcast_accept_all = mask;
11550                 break;
11551         case BXE_RX_MODE_ALLMULTI:
11552                 /* Accept all broadcast and multicast frames. */
11553                 DBPRINT(sc, BXE_VERBOSE,
11554                     "%s(): Setting RX_MODE_ALLMULTI for function %d.\n",
11555                     __FUNCTION__, func);
11556                 tstorm_mac_filter.mcast_accept_all = mask;
11557                 tstorm_mac_filter.bcast_accept_all = mask;
11558                 break;
11559         case BXE_RX_MODE_PROMISC:
11560                 /* Accept all frames (promiscuous mode). */
11561                 DBPRINT(sc, BXE_VERBOSE,
11562                     "%s(): Setting RX_MODE_PROMISC for function %d.\n",
11563                     __FUNCTION__, func);
11564                 tstorm_mac_filter.ucast_accept_all = mask;
11565                 tstorm_mac_filter.mcast_accept_all = mask;
11566                 tstorm_mac_filter.bcast_accept_all = mask;
11567                 llh_mask |= NIG_LLH0_BRB1_DRV_MASK_REG_LLH0_BRB1_DRV_MASK_UNCST;
11568                 break;
11569
11570         default:
11571                 BXE_PRINTF(
11572                     "%s(%d): Tried to set unknown receive mode (0x%08X)!\n",
11573                     __FILE__, __LINE__, mode);
11574         }
11575
11576         REG_WR(sc, port ? NIG_REG_LLH1_BRB1_DRV_MASK :
11577             NIG_REG_LLH0_BRB1_DRV_MASK, llh_mask);
11578
11579         /* Write the RX mode filter to the TSTORM. */
11580         for (i = 0; i < sizeof(struct tstorm_eth_mac_filter_config) / 4; i++)
11581                 REG_WR(sc, BAR_TSTORM_INTMEM +
11582                     TSTORM_MAC_FILTER_CONFIG_OFFSET(func) + (i * 4),
11583                     ((uint32_t *) &tstorm_mac_filter)[i]);
11584
11585         if (mode != BXE_RX_MODE_NONE)
11586                 bxe_set_client_config(sc);
11587
11588         DBEXIT(BXE_VERBOSE_LOAD | BXE_VERBOSE_RESET);
11589 }
11590
11591 /*
11592  * Initialize common internal resources.  (Applies to both ports and
11593  * functions.)
11594  *
11595  * Returns:
11596  *   Nothing.
11597  */
11598 static void
11599 bxe_init_internal_common(struct bxe_softc *sc)
11600 {
11601         int i;
11602
11603         DBENTER(BXE_VERBOSE_LOAD | BXE_VERBOSE_RESET);
11604
11605         /*
11606          * Zero this manually as its initialization is currently not
11607          * handled through block initialization.
11608          */
11609         for (i = 0; i < (USTORM_AGG_DATA_SIZE >> 2); i++)
11610                 REG_WR(sc, BAR_USTORM_INTMEM + USTORM_AGG_DATA_OFFSET + i * 4,
11611                     0);
11612
11613         DBEXIT(BXE_VERBOSE_LOAD | BXE_VERBOSE_RESET);
11614 }
11615
11616 /*
11617  * Initialize port specific internal resources.
11618  *
11619  * Returns:
11620  *   Nothing.
11621  */
11622 static void
11623 bxe_init_internal_port(struct bxe_softc *sc)
11624 {
11625         int port = BP_PORT(sc);
11626
11627         port = BP_PORT(sc);
11628
11629         DBENTER(BXE_VERBOSE_LOAD | BXE_VERBOSE_RESET);
11630         DBPRINT(sc, (BXE_VERBOSE_LOAD | BXE_VERBOSE_RESET),
11631             "%s(): Port %d internal initialization.\n", __FUNCTION__, port);
11632
11633         /*
11634          * Each SDM timer tick is 4us. Configure host coalescing
11635          * basic timer resolution (BTR) to 12us (3 * 4us).
11636          */
11637         REG_WR(sc, BAR_CSTORM_INTMEM + CSTORM_HC_BTR_U_OFFSET(port), BXE_BTR);
11638         REG_WR(sc, BAR_CSTORM_INTMEM + CSTORM_HC_BTR_C_OFFSET(port), BXE_BTR);
11639         REG_WR(sc, BAR_TSTORM_INTMEM + TSTORM_HC_BTR_OFFSET(port), BXE_BTR);
11640         REG_WR(sc, BAR_XSTORM_INTMEM + XSTORM_HC_BTR_OFFSET(port), BXE_BTR);
11641
11642         DBEXIT(BXE_VERBOSE_LOAD | BXE_VERBOSE_RESET);
11643 }
11644
11645 /*
11646  * Initialize function specific internal resources.
11647  *
11648  * Returns:
11649  *   Nothing.
11650  */
11651 static void
11652 bxe_init_internal_func(struct bxe_softc *sc)
11653 {
11654         struct tstorm_eth_function_common_config tstorm_config = {0};
11655         struct stats_indication_flags stats_flags = {0};
11656         struct ustorm_eth_rx_pause_data_e1h rx_pause = {0};
11657         struct bxe_fastpath *fp;
11658         struct eth_rx_cqe_next_page *nextpg;
11659         uint32_t offset, size;
11660         uint16_t max_agg_size;
11661         uint8_t cl_id;
11662         int func, i, j, port;
11663
11664         port = BP_PORT(sc);
11665         func = BP_FUNC(sc);
11666
11667         DBENTER(BXE_VERBOSE_LOAD | BXE_VERBOSE_RESET);
11668         DBPRINT(sc, (BXE_VERBOSE_LOAD | BXE_VERBOSE_RESET),
11669             "%s(): Port %d, function %d internal initialization.\n",
11670             __FUNCTION__, port, func);
11671
11672         /*
11673          * Configure which fields the controller looks at when
11674          * distributing incoming frames for RSS/multi-queue operation.
11675          */
11676         if (sc->num_queues > 1) {
11677                 tstorm_config.config_flags = MULTI_FLAGS(sc);
11678                 tstorm_config.rss_result_mask = MULTI_MASK;
11679         }
11680
11681         /* Enable TPA if needed */
11682         if (TPA_ENABLED(sc))
11683                 tstorm_config.config_flags |=
11684                     TSTORM_ETH_FUNCTION_COMMON_CONFIG_ENABLE_TPA;
11685
11686         if (IS_E1HMF(sc))
11687                 tstorm_config.config_flags |=
11688                     TSTORM_ETH_FUNCTION_COMMON_CONFIG_E1HOV_IN_CAM;
11689
11690         tstorm_config.leading_client_id = BP_L_ID(sc);
11691
11692         REG_WR(sc, BAR_TSTORM_INTMEM +
11693             TSTORM_FUNCTION_COMMON_CONFIG_OFFSET(func),
11694             (*(uint32_t *)&tstorm_config));
11695
11696         /* Don't receive anything until the link is up. */
11697         sc->rx_mode = BXE_RX_MODE_NONE;
11698         sc->rx_mode_cl_mask = (1 << BP_L_ID(sc));
11699         bxe_set_storm_rx_mode(sc);
11700
11701         for (i = 0; i < sc->num_queues; i++) {
11702                 cl_id = sc->fp[i].cl_id;
11703                 /* Reset XSTORM per client statistics. */
11704                 size = sizeof(struct xstorm_per_client_stats) / 4;
11705                 offset = BAR_XSTORM_INTMEM +
11706                     XSTORM_PER_COUNTER_ID_STATS_OFFSET(port, cl_id);
11707                 for (j = 0; j < size; j++)
11708                         REG_WR(sc, offset +(j * 4), 0);
11709
11710                 /* Reset TSTORM per client statistics. */
11711                 size = sizeof(struct tstorm_per_client_stats) / 4;
11712                 offset = BAR_TSTORM_INTMEM +
11713                     TSTORM_PER_COUNTER_ID_STATS_OFFSET(port, cl_id);
11714                 for (j = 0; j < size; j++)
11715                         REG_WR(sc, offset + (j * 4), 0);
11716
11717                 /* Reset USTORM per client statistics. */
11718                 size = sizeof(struct ustorm_per_client_stats) / 4;
11719                 offset = BAR_USTORM_INTMEM +
11720                     USTORM_PER_COUNTER_ID_STATS_OFFSET(port, cl_id);
11721                 for (j = 0; j < size; j++)
11722                         REG_WR(sc, offset + (j * 4), 0);
11723         }
11724
11725         /* Initialize statistics related context. */
11726         stats_flags.collect_eth = 1;
11727
11728         REG_WR(sc, BAR_XSTORM_INTMEM + XSTORM_STATS_FLAGS_OFFSET(func),
11729             ((uint32_t *)&stats_flags)[0]);
11730         REG_WR(sc, BAR_XSTORM_INTMEM + XSTORM_STATS_FLAGS_OFFSET(func) + 4,
11731             ((uint32_t *)&stats_flags)[1]);
11732
11733         REG_WR(sc, BAR_TSTORM_INTMEM + TSTORM_STATS_FLAGS_OFFSET(func),
11734             ((uint32_t *)&stats_flags)[0]);
11735         REG_WR(sc, BAR_TSTORM_INTMEM + TSTORM_STATS_FLAGS_OFFSET(func) + 4,
11736             ((uint32_t *)&stats_flags)[1]);
11737
11738         REG_WR(sc, BAR_USTORM_INTMEM + USTORM_STATS_FLAGS_OFFSET(func),
11739             ((uint32_t *)&stats_flags)[0]);
11740         REG_WR(sc, BAR_USTORM_INTMEM + USTORM_STATS_FLAGS_OFFSET(func) + 4,
11741             ((uint32_t *)&stats_flags)[1]);
11742
11743         REG_WR(sc, BAR_CSTORM_INTMEM + CSTORM_STATS_FLAGS_OFFSET(func),
11744             ((uint32_t *)&stats_flags)[0]);
11745         REG_WR(sc, BAR_CSTORM_INTMEM + CSTORM_STATS_FLAGS_OFFSET(func) + 4,
11746             ((uint32_t *)&stats_flags)[1]);
11747
11748         REG_WR(sc, BAR_XSTORM_INTMEM + XSTORM_ETH_STATS_QUERY_ADDR_OFFSET(func),
11749             U64_LO(BXE_SP_MAPPING(sc, fw_stats)));
11750         REG_WR(sc, BAR_XSTORM_INTMEM +
11751             XSTORM_ETH_STATS_QUERY_ADDR_OFFSET(func) + 4,
11752             U64_HI(BXE_SP_MAPPING(sc, fw_stats)));
11753
11754         REG_WR(sc, BAR_TSTORM_INTMEM + TSTORM_ETH_STATS_QUERY_ADDR_OFFSET(func),
11755             U64_LO(BXE_SP_MAPPING(sc, fw_stats)));
11756         REG_WR(sc, BAR_TSTORM_INTMEM +
11757             TSTORM_ETH_STATS_QUERY_ADDR_OFFSET(func) + 4,
11758             U64_HI(BXE_SP_MAPPING(sc, fw_stats)));
11759
11760         REG_WR(sc, BAR_USTORM_INTMEM + USTORM_ETH_STATS_QUERY_ADDR_OFFSET(func),
11761             U64_LO(BXE_SP_MAPPING(sc, fw_stats)));
11762         REG_WR(sc, BAR_USTORM_INTMEM +
11763             USTORM_ETH_STATS_QUERY_ADDR_OFFSET(func) + 4,
11764             U64_HI(BXE_SP_MAPPING(sc, fw_stats)));
11765
11766         /* Additional initialization for 57711/57711E. */
11767         if (CHIP_IS_E1H(sc)) {
11768                 REG_WR8(sc, BAR_XSTORM_INTMEM + XSTORM_FUNCTION_MODE_OFFSET,
11769                     IS_E1HMF(sc));
11770                 REG_WR8(sc, BAR_TSTORM_INTMEM + TSTORM_FUNCTION_MODE_OFFSET,
11771                     IS_E1HMF(sc));
11772                 REG_WR8(sc, BAR_CSTORM_INTMEM + CSTORM_FUNCTION_MODE_OFFSET,
11773                     IS_E1HMF(sc));
11774                 REG_WR8(sc, BAR_USTORM_INTMEM + USTORM_FUNCTION_MODE_OFFSET,
11775                     IS_E1HMF(sc));
11776
11777                 /* Set the outer VLAN tag. */
11778                 REG_WR16(sc, BAR_XSTORM_INTMEM + XSTORM_E1HOV_OFFSET(func),
11779                     sc->e1hov);
11780         }
11781
11782         /* Init completion queue mapping and TPA aggregation size. */
11783         max_agg_size = min((uint32_t)(sc->mbuf_alloc_size +
11784             (8 * BCM_PAGE_SIZE * PAGES_PER_SGE)), (uint32_t)0xffff);
11785
11786         DBPRINT(sc, BXE_VERBOSE_TPA, "%s(): max_agg_size = 0x%08X\n",
11787             __FUNCTION__, max_agg_size);
11788
11789         for (i = 0; i < sc->num_queues; i++) {
11790                 fp = &sc->fp[i];
11791                 nextpg = (struct eth_rx_cqe_next_page *)
11792                     &fp->rcq_chain[USABLE_RCQ_ENTRIES_PER_PAGE];
11793
11794                 /* Program the completion queue address. */
11795                 REG_WR(sc, BAR_USTORM_INTMEM +
11796                     USTORM_CQE_PAGE_BASE_OFFSET(port, fp->cl_id),
11797                     U64_LO(fp->rcq_dma.paddr));
11798                 REG_WR(sc, BAR_USTORM_INTMEM +
11799                     USTORM_CQE_PAGE_BASE_OFFSET(port, fp->cl_id) + 4,
11800                     U64_HI(fp->rcq_dma.paddr));
11801
11802                 /* Program the first CQ next page address. */
11803                 REG_WR(sc, BAR_USTORM_INTMEM +
11804                     USTORM_CQE_PAGE_NEXT_OFFSET(port, fp->cl_id),
11805                     nextpg->addr_lo);
11806                 REG_WR(sc, BAR_USTORM_INTMEM +
11807                     USTORM_CQE_PAGE_NEXT_OFFSET(port, fp->cl_id) + 4,
11808                     nextpg->addr_hi);
11809
11810                 /* Set the maximum TPA aggregation size. */
11811                 REG_WR16(sc, BAR_USTORM_INTMEM +
11812                     USTORM_MAX_AGG_SIZE_OFFSET(port, fp->cl_id),
11813                     max_agg_size);
11814         }
11815
11816         /* Configure lossless flow control. */
11817         if (CHIP_IS_E1H(sc)) {
11818                 rx_pause.bd_thr_low = 250;
11819                 rx_pause.cqe_thr_low = 250;
11820                 rx_pause.cos = 1;
11821                 rx_pause.sge_thr_low = 0;
11822                 rx_pause.bd_thr_high = 350;
11823                 rx_pause.cqe_thr_high = 350;
11824                 rx_pause.sge_thr_high = 0;
11825
11826                 for (i = 0; i < sc->num_queues; i++) {
11827                         fp = &sc->fp[i];
11828                         if (fp->disable_tpa == FALSE) {
11829                                 rx_pause.sge_thr_low = 150;
11830                                 rx_pause.sge_thr_high = 250;
11831                         }
11832
11833                         offset = BAR_USTORM_INTMEM +
11834                             USTORM_ETH_RING_PAUSE_DATA_OFFSET(port, fp->cl_id);
11835
11836                         for (j = 0; j <
11837                             sizeof(struct ustorm_eth_rx_pause_data_e1h) / 4;
11838                             j++)
11839                                 REG_WR(sc, offset + (j * 4),
11840                                     ((uint32_t *)&rx_pause)[j]);
11841                 }
11842         }
11843
11844         memset(&(sc->cmng), 0, sizeof(struct cmng_struct_per_port));
11845         if (IS_E1HMF(sc)) {
11846                 /*
11847                  * During init there is no active link.
11848                  * Until link is up, assume link rate @ 10Gbps
11849                  */
11850                 bxe_read_mf_cfg(sc);
11851
11852                 if (!sc->vn_wsum)
11853                         DBPRINT(sc, BXE_VERBOSE_MISC,
11854                             "%s(): All MIN values are zeroes, "
11855                             "fairness will be disabled.\n", __FUNCTION__);
11856         }
11857
11858         /* Store it to internal memory */
11859         if (sc->port.pmf) {
11860                 for (i = 0; i < sizeof(struct cmng_struct_per_port) / 4; i++)
11861                         REG_WR(sc, BAR_XSTORM_INTMEM +
11862                             XSTORM_CMNG_PER_PORT_VARS_OFFSET(port) + i * 4,
11863                             ((uint32_t *)(&sc->cmng))[i]);
11864         }
11865
11866         DBEXIT(BXE_VERBOSE_LOAD | BXE_VERBOSE_RESET);
11867 }
11868
11869 /*
11870  * Initialize internal resources.
11871  *
11872  * Returns:
11873  *   Nothing.
11874  */
11875 static void
11876 bxe_init_internal(struct bxe_softc *sc, uint32_t load_code)
11877 {
11878
11879         DBENTER(BXE_VERBOSE_LOAD | BXE_VERBOSE_RESET);
11880
11881         switch (load_code) {
11882         case FW_MSG_CODE_DRV_LOAD_COMMON:
11883                 bxe_init_internal_common(sc);
11884                 /* FALLTHROUGH */
11885
11886         case FW_MSG_CODE_DRV_LOAD_PORT:
11887                 bxe_init_internal_port(sc);
11888                 /* FALLTHROUGH */
11889
11890         case FW_MSG_CODE_DRV_LOAD_FUNCTION:
11891                 bxe_init_internal_func(sc);
11892                 break;
11893
11894         default:
11895                 BXE_PRINTF(
11896                     "%s(%d): Unknown load_code (0x%08X) from MCP!\n",
11897                     __FILE__, __LINE__, load_code);
11898                 break;
11899         }
11900
11901         DBEXIT(BXE_VERBOSE_LOAD | BXE_VERBOSE_RESET);
11902 }
11903
11904
11905 /*
11906  * Perform driver instance specific initialization.
11907  *
11908  * Returns:
11909  *   None
11910  */
11911 static int
11912 bxe_init_nic(struct bxe_softc *sc, uint32_t load_code)
11913 {
11914         struct bxe_fastpath *fp;
11915         int i, rc;
11916
11917         DBENTER(BXE_VERBOSE_LOAD | BXE_VERBOSE_RESET);
11918
11919         /* Intialize fastpath structures and the status block. */
11920         for (i = 0; i < sc->num_queues; i++) {
11921                 fp = &sc->fp[i];
11922                 fp->disable_tpa = TRUE;
11923
11924                 bzero((char *)fp->status_block, BXE_STATUS_BLK_SZ);
11925                 fp->fp_u_idx = 0;
11926                 fp->fp_c_idx = 0;
11927
11928                 /* Set a pointer back to the driver instance. */
11929                 fp->sc = sc;
11930
11931                 /* Set the fastpath starting state as closed. */
11932                 fp->state = BXE_FP_STATE_CLOSED;
11933
11934                 /* Self-reference to this fastpath's instance. */
11935                 fp->index = i;
11936
11937                 /* Set the client ID beginning with the leading id. */
11938                 fp->cl_id = BP_L_ID(sc) + i;
11939
11940                 /* Set the status block ID for this fastpath instance. */
11941                 fp->sb_id = fp->cl_id;
11942
11943                 DBPRINT(sc, (BXE_VERBOSE_LOAD | BXE_VERBOSE_RESET),
11944                     "%s(): fp[%02d]: cl_id = %d, sb_id = %d\n",
11945                     __FUNCTION__, fp->index, fp->cl_id, fp->sb_id);
11946
11947                 /* Initialize the fastpath status block. */
11948                 bxe_init_sb(sc, fp->status_block, fp->sb_dma.paddr,
11949                     fp->sb_id);
11950                 bxe_update_fpsb_idx(fp);
11951         }
11952
11953         rmb();
11954
11955         bzero((char *)sc->def_sb, BXE_DEF_STATUS_BLK_SZ);
11956
11957         /* Initialize the Default Status Block. */
11958         bxe_init_def_sb(sc, sc->def_sb, sc->def_sb_dma.paddr, DEF_SB_ID);
11959         bxe_update_dsb_idx(sc);
11960
11961         /* Initialize the coalescence parameters. */
11962         bxe_update_coalesce(sc);
11963
11964         /* Initialize receive chains. */
11965         rc = bxe_init_rx_chains(sc);
11966         if (rc != 0) {
11967                 goto bxe_init_nic_exit;
11968         }
11969
11970         /* Initialize the Transmit BD Chain. */
11971         bxe_init_tx_chains(sc);
11972
11973         /* Initialize the Slow Path Chain. */
11974         bxe_init_sp_ring(sc);
11975
11976         /* Initialize STORM processor context/configuration. */
11977         bxe_init_context(sc);
11978
11979         /* Initialize the Context. */
11980         bxe_init_internal(sc, load_code);
11981
11982         /* Enable indirection table for multi-queue operation. */
11983         bxe_init_ind_table(sc);
11984
11985         mb();
11986
11987         /* Disable the interrupts from device until init is complete.*/
11988         bxe_int_disable(sc);
11989
11990 bxe_init_nic_exit:
11991         DBEXIT(BXE_VERBOSE_LOAD | BXE_VERBOSE_RESET);
11992         return (rc);
11993 }
11994
11995 /*
11996  * Send a loopback packet through the Network Interface Glue (NIG) block.
11997  *
11998  * Returns:
11999  *   None.
12000  */
12001 static void
12002 bxe_lb_pckt(struct bxe_softc *sc)
12003 {
12004 #ifdef BXE_USE_DMAE
12005         uint32_t wb_write[3];
12006 #endif
12007
12008         DBENTER(BXE_VERBOSE_LOAD | BXE_VERBOSE_RESET);
12009
12010         /* Ethernet source and destination addresses. */
12011 #ifdef BXE_USE_DMAE
12012         wb_write[0] = 0x55555555;
12013         wb_write[1] = 0x55555555;
12014         wb_write[2] = 0x20;     /* SOP */
12015         REG_WR_DMAE(sc, NIG_REG_DEBUG_PACKET_LB, wb_write, 3);
12016 #else
12017         REG_WR_IND(sc, NIG_REG_DEBUG_PACKET_LB, 0x55555555);
12018         REG_WR_IND(sc, NIG_REG_DEBUG_PACKET_LB + 4, 0x55555555);
12019         REG_WR_IND(sc, NIG_REG_DEBUG_PACKET_LB + 8, 0x20);
12020 #endif
12021
12022         /* NON-IP protocol. */
12023 #ifdef BXE_USE_DMAE
12024         wb_write[0] = 0x09000000;
12025         wb_write[1] = 0x55555555;
12026         wb_write[2] = 0x10;     /* EOP */
12027         REG_WR_DMAE(sc, NIG_REG_DEBUG_PACKET_LB, wb_write, 3);
12028 #else
12029         REG_WR_IND(sc, NIG_REG_DEBUG_PACKET_LB, 0x09000000);
12030         REG_WR_IND(sc, NIG_REG_DEBUG_PACKET_LB + 4, 0x55555555);
12031         REG_WR_IND(sc, NIG_REG_DEBUG_PACKET_LB + 8, 0x10);
12032 #endif
12033
12034         DBEXIT(BXE_VERBOSE_LOAD | BXE_VERBOSE_RESET);
12035 }
12036
12037 /*
12038  * Perform an internal memory test.
12039  *
12040  * Some internal memories are not accessible through the PCIe interface so
12041  * we send some debug packets for the test.
12042  *
12043  * Returns:
12044  *   0 = Success, !0 = Failure.
12045  */
12046 static int
12047 bxe_int_mem_test(struct bxe_softc *sc)
12048 {
12049         uint32_t val;
12050         int count, i, rc;
12051
12052         rc = 0;
12053         val = 0;
12054
12055         DBENTER(BXE_VERBOSE_LOAD | BXE_VERBOSE_RESET);
12056
12057         /* Perform a single debug packet test. */
12058
12059         /* Disable inputs of parser neighbor blocks. */
12060         REG_WR(sc, TSDM_REG_ENABLE_IN1, 0x0);
12061         REG_WR(sc, TCM_REG_PRS_IFEN, 0x0);
12062         REG_WR(sc, CFC_REG_DEBUG0, 0x1);
12063         REG_WR(sc, NIG_REG_PRS_REQ_IN_EN, 0x0);
12064
12065         /*  Write 0 to parser credits for CFC search request. */
12066         REG_WR(sc, PRS_REG_CFC_SEARCH_INITIAL_CREDIT, 0x0);
12067
12068         /* Send an Ethernet packet. */
12069         bxe_lb_pckt(sc);
12070
12071         /* Wait until NIG register shows 1 packet of size 0x10. */
12072         count = 1000;
12073         while (count) {
12074                 bxe_read_dmae(sc, NIG_REG_STAT2_BRB_OCTET, 2);
12075                 val = *BXE_SP(sc, wb_data[0]);
12076                 if (val == 0x10)
12077                         break;
12078
12079                 DELAY(10000);
12080                 count--;
12081         }
12082
12083         if (val != 0x10) {
12084                 DBPRINT(sc, BXE_FATAL,
12085                     "%s(): NIG loopback test 1 timeout (val = 0x%08X)!\n",
12086                     __FUNCTION__, val);
12087                 rc = 1;
12088                 goto bxe_int_mem_test_exit;
12089         }
12090
12091         /* Wait until PRS register shows 1 packet */
12092         count = 1000;
12093         while (count) {
12094                 val = REG_RD(sc, PRS_REG_NUM_OF_PACKETS);
12095
12096                 if (val == 1)
12097                         break;
12098
12099                 DELAY(10000);
12100                 count--;
12101         }
12102
12103         if (val != 0x1) {
12104                 DBPRINT(sc, BXE_FATAL,
12105                     "%s(): PRS loopback test 1 timeout (val = 0x%08X)!\n",
12106                     __FUNCTION__, val);
12107                 rc = 2;
12108                 goto bxe_int_mem_test_exit;
12109         }
12110
12111         /* Reset and init BRB, PRS. */
12112         REG_WR(sc, GRCBASE_MISC + MISC_REGISTERS_RESET_REG_1_CLEAR, 0x3);
12113         DELAY(50000);
12114         REG_WR(sc, GRCBASE_MISC + MISC_REGISTERS_RESET_REG_1_SET, 0x3);
12115         DELAY(50000);
12116         bxe_init_block(sc, BRB1_BLOCK, COMMON_STAGE);
12117         bxe_init_block(sc, PRS_BLOCK, COMMON_STAGE);
12118
12119         /* Perform the test again, this time with 10 packets. */
12120
12121         /* Disable inputs of parser neighbor blocks. */
12122         REG_WR(sc, TSDM_REG_ENABLE_IN1, 0x0);
12123         REG_WR(sc, TCM_REG_PRS_IFEN, 0x0);
12124         REG_WR(sc, CFC_REG_DEBUG0, 0x1);
12125         REG_WR(sc, NIG_REG_PRS_REQ_IN_EN, 0x0);
12126
12127         /* Write 0 to parser credits for CFC search request. */
12128         REG_WR(sc, PRS_REG_CFC_SEARCH_INITIAL_CREDIT, 0x0);
12129
12130         /* Send 10 Ethernet packets. */
12131         for (i = 0; i < 10; i++)
12132                 bxe_lb_pckt(sc);
12133
12134         /* Wait until NIG shows 10 + 1 packets of size 11 * 0x10 = 0xb0. */
12135         count = 1000;
12136         while (count) {
12137                 bxe_read_dmae(sc, NIG_REG_STAT2_BRB_OCTET, 2);
12138                 val = *BXE_SP(sc, wb_data[0]);
12139                 if (val == 0xb0)
12140                         break;
12141
12142                 DELAY(10000);
12143                 count--;
12144         }
12145
12146         if (val != 0xb0) {
12147                 DBPRINT(sc, BXE_FATAL,
12148                     "%s(): NIG loopback test 2 timeout (val = 0x%08X)!\n",
12149                     __FUNCTION__, val);
12150                 rc = 3;
12151                 goto bxe_int_mem_test_exit;
12152         }
12153
12154         /* Wait until PRS register shows 2 packets. */
12155         val = REG_RD(sc, PRS_REG_NUM_OF_PACKETS);
12156         if (val != 2) {
12157                 DBPRINT(sc, BXE_FATAL,
12158                     "%s(): PRS loopback test 2 timeout (val = 0x%x)!\n",
12159                     __FUNCTION__, val);
12160                 rc = 4;
12161                 goto bxe_int_mem_test_exit;
12162         }
12163
12164         /* Write 1 to parser credits for CFC search request. */
12165         REG_WR(sc, PRS_REG_CFC_SEARCH_INITIAL_CREDIT, 0x1);
12166
12167         /* Wait until PRS register shows 3 packets. */
12168         DELAY(10000);
12169
12170         /* Wait until NIG register shows 1 packet of size 0x10. */
12171         val = REG_RD(sc, PRS_REG_NUM_OF_PACKETS);
12172         if (val != 3) {
12173                 DBPRINT(sc, BXE_FATAL,
12174                     "%s(): PRS loopback test 3 timeout (val = 0x%08X)!\n",
12175                     __FUNCTION__, val);
12176                 rc = 5;
12177                 goto bxe_int_mem_test_exit;
12178         }
12179
12180         /* Clear NIG end-of-packet FIFO. */
12181         for (i = 0; i < 11; i++)
12182                 REG_RD(sc, NIG_REG_INGRESS_EOP_LB_FIFO);
12183
12184         val = REG_RD(sc, NIG_REG_INGRESS_EOP_LB_EMPTY);
12185         if (val != 1) {
12186                 DBPRINT(sc, BXE_INFO, "%s(): Unable to clear NIG!\n",
12187                     __FUNCTION__);
12188                 rc = 6;
12189                 goto bxe_int_mem_test_exit;
12190         }
12191
12192         /* Reset and init BRB, PRS, NIG. */
12193         REG_WR(sc, GRCBASE_MISC + MISC_REGISTERS_RESET_REG_1_CLEAR, 0x03);
12194         DELAY(50000);
12195         REG_WR(sc, GRCBASE_MISC + MISC_REGISTERS_RESET_REG_1_SET, 0x03);
12196         DELAY(50000);
12197         bxe_init_block(sc, BRB1_BLOCK, COMMON_STAGE);
12198         bxe_init_block(sc, PRS_BLOCK, COMMON_STAGE);
12199
12200         /* Set NIC mode. */
12201         REG_WR(sc, PRS_REG_NIC_MODE, 1);
12202
12203         /* Enable inputs of parser neighbor blocks. */
12204         REG_WR(sc, TSDM_REG_ENABLE_IN1, 0x7fffffff);
12205         REG_WR(sc, TCM_REG_PRS_IFEN, 0x1);
12206         REG_WR(sc, CFC_REG_DEBUG0, 0x0);
12207         REG_WR(sc, NIG_REG_PRS_REQ_IN_EN, 0x1);
12208
12209 bxe_int_mem_test_exit:
12210         DBEXIT(BXE_VERBOSE_LOAD | BXE_VERBOSE_RESET);
12211         return (rc);
12212 }
12213
12214 /*
12215  * Enable attentions from various blocks.
12216  *
12217  * Returns:
12218  *   None.
12219  */
12220 static void
12221 bxe_enable_blocks_attention(struct bxe_softc *sc)
12222 {
12223
12224         DBENTER(BXE_VERBOSE_LOAD | BXE_VERBOSE_RESET);
12225
12226         REG_WR(sc, PXP_REG_PXP_INT_MASK_0, 0);
12227         REG_WR(sc, PXP_REG_PXP_INT_MASK_1, 0);
12228         REG_WR(sc, DORQ_REG_DORQ_INT_MASK, 0);
12229         REG_WR(sc, CFC_REG_CFC_INT_MASK, 0);
12230         REG_WR(sc, QM_REG_QM_INT_MASK, 0);
12231         REG_WR(sc, TM_REG_TM_INT_MASK, 0);
12232         REG_WR(sc, XSDM_REG_XSDM_INT_MASK_0, 0);
12233         REG_WR(sc, XSDM_REG_XSDM_INT_MASK_1, 0);
12234         REG_WR(sc, XCM_REG_XCM_INT_MASK, 0);
12235
12236         REG_WR(sc, USDM_REG_USDM_INT_MASK_0, 0);
12237         REG_WR(sc, USDM_REG_USDM_INT_MASK_1, 0);
12238         REG_WR(sc, UCM_REG_UCM_INT_MASK, 0);
12239
12240         REG_WR(sc, GRCBASE_UPB + PB_REG_PB_INT_MASK, 0);
12241         REG_WR(sc, CSDM_REG_CSDM_INT_MASK_0, 0);
12242         REG_WR(sc, CSDM_REG_CSDM_INT_MASK_1, 0);
12243         REG_WR(sc, CCM_REG_CCM_INT_MASK, 0);
12244
12245         REG_WR(sc, PXP2_REG_PXP2_INT_MASK_0, 0x480000);
12246
12247         REG_WR(sc, TSDM_REG_TSDM_INT_MASK_0, 0);
12248         REG_WR(sc, TSDM_REG_TSDM_INT_MASK_1, 0);
12249         REG_WR(sc, TCM_REG_TCM_INT_MASK, 0);
12250
12251         REG_WR(sc, CDU_REG_CDU_INT_MASK, 0);
12252         REG_WR(sc, DMAE_REG_DMAE_INT_MASK, 0);
12253         REG_WR(sc, PBF_REG_PBF_INT_MASK, 0X18);
12254
12255         DBEXIT(BXE_VERBOSE_LOAD | BXE_VERBOSE_RESET);
12256 }
12257
12258 /*
12259  * PXP Arbiter
12260  */
12261
12262 /*
12263  * This code configures the PCI read/write arbiter
12264  * which implements a weighted round robin
12265  * between the virtual queues in the chip.
12266  *
12267  * The values were derived for each PCI max payload and max request size.
12268  * since max payload and max request size are only known at run time,
12269  * this is done as a separate init stage.
12270  */
12271
12272 #define NUM_WR_Q                        13
12273 #define NUM_RD_Q                        29
12274 #define MAX_RD_ORD                      3
12275 #define MAX_WR_ORD                      2
12276
12277 /* Configuration for one arbiter queue. */
12278 struct arb_line {
12279         int l;
12280         int add;
12281         int ubound;
12282 };
12283
12284 /* Derived configuration for each read queue for each max request size. */
12285 static const struct arb_line read_arb_data[NUM_RD_Q][MAX_RD_ORD + 1] = {
12286 /* 1 */ { {8, 64, 25}, {16, 64, 25}, {32, 64, 25}, {64, 64, 41} },
12287         { {4, 8,  4},  {4,  8,  4},  {4,  8,  4},  {4,  8,  4}  },
12288         { {4, 3,  3},  {4,  3,  3},  {4,  3,  3},  {4,  3,  3}  },
12289         { {8, 3,  6},  {16, 3,  11}, {16, 3,  11}, {16, 3,  11} },
12290         { {8, 64, 25}, {16, 64, 25}, {32, 64, 25}, {64, 64, 41} },
12291         { {8, 3,  6},  {16, 3,  11}, {32, 3,  21}, {64, 3,  41} },
12292         { {8, 3,  6},  {16, 3,  11}, {32, 3,  21}, {64, 3,  41} },
12293         { {8, 3,  6},  {16, 3,  11}, {32, 3,  21}, {64, 3,  41} },
12294         { {8, 3,  6},  {16, 3,  11}, {32, 3,  21}, {64, 3,  41} },
12295 /* 10 */{ {8, 3,  6},  {16, 3,  11}, {32, 3,  21}, {32, 3,  21} },
12296         { {8, 3,  6},  {16, 3,  11}, {32, 3,  21}, {32, 3,  21} },
12297         { {8, 3,  6},  {16, 3,  11}, {32, 3,  21}, {32, 3,  21} },
12298         { {8, 3,  6},  {16, 3,  11}, {32, 3,  21}, {32, 3,  21} },
12299         { {8, 3,  6},  {16, 3,  11}, {32, 3,  21}, {32, 3,  21} },
12300         { {8, 3,  6},  {16, 3,  11}, {32, 3,  21}, {32, 3,  21} },
12301         { {8, 3,  6},  {16, 3,  11}, {32, 3,  21}, {32, 3,  21} },
12302         { {8, 64, 6},  {16, 64, 11}, {32, 64, 21}, {32, 64, 21} },
12303         { {8, 3,  6},  {16, 3,  11}, {32, 3,  21}, {32, 3,  21} },
12304         { {8, 3,  6},  {16, 3,  11}, {32, 3,  21}, {32, 3,  21} },
12305 /* 20 */{ {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, 3,  6},  {16, 3,  11}, {32, 3,  21}, {32, 3,  21} },
12313         { {8, 3,  6},  {16, 3,  11}, {32, 3,  21}, {32, 3,  21} },
12314         { {8, 64, 25}, {16, 64, 41}, {32, 64, 81}, {64, 64, 120} }
12315 };
12316
12317 /* Derived configuration for each write queue for each max request size. */
12318 static const struct arb_line write_arb_data[NUM_WR_Q][MAX_WR_ORD + 1] = {
12319 /* 1 */ { {4, 6,  3},  {4,  6,  3},  {4,  6,  3} },
12320         { {4, 2,  3},  {4,  2,  3},  {4,  2,  3} },
12321         { {8, 2,  6},  {16, 2,  11}, {16, 2,  11} },
12322         { {8, 2,  6},  {16, 2,  11}, {32, 2,  21} },
12323         { {8, 2,  6},  {16, 2,  11}, {32, 2,  21} },
12324         { {8, 2,  6},  {16, 2,  11}, {32, 2,  21} },
12325         { {8, 64, 25}, {16, 64, 25}, {32, 64, 25} },
12326         { {8, 2,  6},  {16, 2,  11}, {16, 2,  11} },
12327         { {8, 2,  6},  {16, 2,  11}, {16, 2,  11} },
12328 /* 10 */{ {8, 9,  6},  {16, 9,  11}, {32, 9,  21} },
12329         { {8, 47, 19}, {16, 47, 19}, {32, 47, 21} },
12330         { {8, 9,  6},  {16, 9,  11}, {16, 9,  11} },
12331         { {8, 64, 25}, {16, 64, 41}, {32, 64, 81} }
12332 };
12333
12334 /* Register addresses for read queues. */
12335 static const struct arb_line read_arb_addr[NUM_RD_Q-1] = {
12336 /* 1 */ {PXP2_REG_RQ_BW_RD_L0, PXP2_REG_RQ_BW_RD_ADD0,
12337             PXP2_REG_RQ_BW_RD_UBOUND0},
12338         {PXP2_REG_PSWRQ_BW_L1, PXP2_REG_PSWRQ_BW_ADD1,
12339             PXP2_REG_PSWRQ_BW_UB1},
12340         {PXP2_REG_PSWRQ_BW_L2, PXP2_REG_PSWRQ_BW_ADD2,
12341             PXP2_REG_PSWRQ_BW_UB2},
12342         {PXP2_REG_PSWRQ_BW_L3, PXP2_REG_PSWRQ_BW_ADD3,
12343             PXP2_REG_PSWRQ_BW_UB3},
12344         {PXP2_REG_RQ_BW_RD_L4, PXP2_REG_RQ_BW_RD_ADD4,
12345             PXP2_REG_RQ_BW_RD_UBOUND4},
12346         {PXP2_REG_RQ_BW_RD_L5, PXP2_REG_RQ_BW_RD_ADD5,
12347             PXP2_REG_RQ_BW_RD_UBOUND5},
12348         {PXP2_REG_PSWRQ_BW_L6, PXP2_REG_PSWRQ_BW_ADD6,
12349             PXP2_REG_PSWRQ_BW_UB6},
12350         {PXP2_REG_PSWRQ_BW_L7, PXP2_REG_PSWRQ_BW_ADD7,
12351             PXP2_REG_PSWRQ_BW_UB7},
12352         {PXP2_REG_PSWRQ_BW_L8, PXP2_REG_PSWRQ_BW_ADD8,
12353             PXP2_REG_PSWRQ_BW_UB8},
12354 /* 10 */{PXP2_REG_PSWRQ_BW_L9, PXP2_REG_PSWRQ_BW_ADD9,
12355             PXP2_REG_PSWRQ_BW_UB9},
12356         {PXP2_REG_PSWRQ_BW_L10, PXP2_REG_PSWRQ_BW_ADD10,
12357             PXP2_REG_PSWRQ_BW_UB10},
12358         {PXP2_REG_PSWRQ_BW_L11, PXP2_REG_PSWRQ_BW_ADD11,
12359             PXP2_REG_PSWRQ_BW_UB11},
12360         {PXP2_REG_RQ_BW_RD_L12, PXP2_REG_RQ_BW_RD_ADD12,
12361             PXP2_REG_RQ_BW_RD_UBOUND12},
12362         {PXP2_REG_RQ_BW_RD_L13, PXP2_REG_RQ_BW_RD_ADD13,
12363             PXP2_REG_RQ_BW_RD_UBOUND13},
12364         {PXP2_REG_RQ_BW_RD_L14, PXP2_REG_RQ_BW_RD_ADD14,
12365             PXP2_REG_RQ_BW_RD_UBOUND14},
12366         {PXP2_REG_RQ_BW_RD_L15, PXP2_REG_RQ_BW_RD_ADD15,
12367             PXP2_REG_RQ_BW_RD_UBOUND15},
12368         {PXP2_REG_RQ_BW_RD_L16, PXP2_REG_RQ_BW_RD_ADD16,
12369             PXP2_REG_RQ_BW_RD_UBOUND16},
12370         {PXP2_REG_RQ_BW_RD_L17, PXP2_REG_RQ_BW_RD_ADD17,
12371             PXP2_REG_RQ_BW_RD_UBOUND17},
12372         {PXP2_REG_RQ_BW_RD_L18, PXP2_REG_RQ_BW_RD_ADD18,
12373             PXP2_REG_RQ_BW_RD_UBOUND18},
12374 /* 20 */{PXP2_REG_RQ_BW_RD_L19, PXP2_REG_RQ_BW_RD_ADD19,
12375             PXP2_REG_RQ_BW_RD_UBOUND19},
12376         {PXP2_REG_RQ_BW_RD_L20, PXP2_REG_RQ_BW_RD_ADD20,
12377             PXP2_REG_RQ_BW_RD_UBOUND20},
12378         {PXP2_REG_RQ_BW_RD_L22, PXP2_REG_RQ_BW_RD_ADD22,
12379             PXP2_REG_RQ_BW_RD_UBOUND22},
12380         {PXP2_REG_RQ_BW_RD_L23, PXP2_REG_RQ_BW_RD_ADD23,
12381             PXP2_REG_RQ_BW_RD_UBOUND23},
12382         {PXP2_REG_RQ_BW_RD_L24, PXP2_REG_RQ_BW_RD_ADD24,
12383             PXP2_REG_RQ_BW_RD_UBOUND24},
12384         {PXP2_REG_RQ_BW_RD_L25, PXP2_REG_RQ_BW_RD_ADD25,
12385             PXP2_REG_RQ_BW_RD_UBOUND25},
12386         {PXP2_REG_RQ_BW_RD_L26, PXP2_REG_RQ_BW_RD_ADD26,
12387             PXP2_REG_RQ_BW_RD_UBOUND26},
12388         {PXP2_REG_RQ_BW_RD_L27, PXP2_REG_RQ_BW_RD_ADD27,
12389             PXP2_REG_RQ_BW_RD_UBOUND27},
12390         {PXP2_REG_PSWRQ_BW_L28, PXP2_REG_PSWRQ_BW_ADD28,
12391             PXP2_REG_PSWRQ_BW_UB28}
12392 };
12393
12394 /* Register addresses for write queues. */
12395 static const struct arb_line write_arb_addr[NUM_WR_Q-1] = {
12396 /* 1 */ {PXP2_REG_PSWRQ_BW_L1, PXP2_REG_PSWRQ_BW_ADD1,
12397             PXP2_REG_PSWRQ_BW_UB1},
12398         {PXP2_REG_PSWRQ_BW_L2, PXP2_REG_PSWRQ_BW_ADD2,
12399             PXP2_REG_PSWRQ_BW_UB2},
12400         {PXP2_REG_PSWRQ_BW_L3, PXP2_REG_PSWRQ_BW_ADD3,
12401             PXP2_REG_PSWRQ_BW_UB3},
12402         {PXP2_REG_PSWRQ_BW_L6, PXP2_REG_PSWRQ_BW_ADD6,
12403             PXP2_REG_PSWRQ_BW_UB6},
12404         {PXP2_REG_PSWRQ_BW_L7, PXP2_REG_PSWRQ_BW_ADD7,
12405             PXP2_REG_PSWRQ_BW_UB7},
12406         {PXP2_REG_PSWRQ_BW_L8, PXP2_REG_PSWRQ_BW_ADD8,
12407             PXP2_REG_PSWRQ_BW_UB8},
12408         {PXP2_REG_PSWRQ_BW_L9, PXP2_REG_PSWRQ_BW_ADD9,
12409             PXP2_REG_PSWRQ_BW_UB9},
12410         {PXP2_REG_PSWRQ_BW_L10, PXP2_REG_PSWRQ_BW_ADD10,
12411             PXP2_REG_PSWRQ_BW_UB10},
12412         {PXP2_REG_PSWRQ_BW_L11, PXP2_REG_PSWRQ_BW_ADD11,
12413             PXP2_REG_PSWRQ_BW_UB11},
12414 /* 10 */{PXP2_REG_PSWRQ_BW_L28, PXP2_REG_PSWRQ_BW_ADD28,
12415             PXP2_REG_PSWRQ_BW_UB28},
12416         {PXP2_REG_RQ_BW_WR_L29, PXP2_REG_RQ_BW_WR_ADD29,
12417             PXP2_REG_RQ_BW_WR_UBOUND29},
12418         {PXP2_REG_RQ_BW_WR_L30, PXP2_REG_RQ_BW_WR_ADD30,
12419             PXP2_REG_RQ_BW_WR_UBOUND30}
12420 };
12421
12422 static void
12423 bxe_init_pxp_arb(struct bxe_softc *sc, int r_order, int w_order)
12424 {
12425         uint32_t val, i;
12426
12427         if (r_order > MAX_RD_ORD) {
12428                 DBPRINT(sc, (BXE_VERBOSE_LOAD | BXE_VERBOSE_RESET),
12429                     "%s(): Read order of %d order adjusted to %d\n",
12430                     __FUNCTION__,  r_order, MAX_RD_ORD);
12431                 r_order = MAX_RD_ORD;
12432         }
12433         if (w_order > MAX_WR_ORD) {
12434                 DBPRINT(sc, (BXE_VERBOSE_LOAD | BXE_VERBOSE_RESET),
12435                     "%s(): Write order of %d order adjusted to %d\n",
12436                     __FUNCTION__, w_order, MAX_WR_ORD);
12437                 w_order = MAX_WR_ORD;
12438         }
12439
12440         DBPRINT(sc, (BXE_VERBOSE_LOAD | BXE_VERBOSE_RESET),
12441             "%s(): Read order %d, write order %d\n",
12442             __FUNCTION__, r_order, w_order);
12443
12444         for (i = 0; i < NUM_RD_Q - 1; i++) {
12445                 REG_WR(sc, read_arb_addr[i].l,
12446                     read_arb_data[i][r_order].l);
12447                 REG_WR(sc, read_arb_addr[i].add,
12448                     read_arb_data[i][r_order].add);
12449                 REG_WR(sc, read_arb_addr[i].ubound,
12450                     read_arb_data[i][r_order].ubound);
12451         }
12452
12453         for (i = 0; i < NUM_WR_Q - 1; i++) {
12454                 if ((write_arb_addr[i].l == PXP2_REG_RQ_BW_WR_L29) ||
12455                     (write_arb_addr[i].l == PXP2_REG_RQ_BW_WR_L30)) {
12456
12457                         REG_WR(sc, write_arb_addr[i].l,
12458                             write_arb_data[i][w_order].l);
12459
12460                         REG_WR(sc, write_arb_addr[i].add,
12461                             write_arb_data[i][w_order].add);
12462
12463                         REG_WR(sc, write_arb_addr[i].ubound,
12464                             write_arb_data[i][w_order].ubound);
12465                 } else {
12466
12467                         val = REG_RD(sc, write_arb_addr[i].l);
12468                         REG_WR(sc, write_arb_addr[i].l, val |
12469                             (write_arb_data[i][w_order].l << 10));
12470
12471                         val = REG_RD(sc, write_arb_addr[i].add);
12472                         REG_WR(sc, write_arb_addr[i].add, val |
12473                             (write_arb_data[i][w_order].add << 10));
12474
12475                         val = REG_RD(sc, write_arb_addr[i].ubound);
12476                         REG_WR(sc, write_arb_addr[i].ubound, val |
12477                             (write_arb_data[i][w_order].ubound << 7));
12478                 }
12479         }
12480
12481         val =  write_arb_data[NUM_WR_Q - 1][w_order].add;
12482         val += write_arb_data[NUM_WR_Q - 1][w_order].ubound << 10;
12483         val += write_arb_data[NUM_WR_Q - 1][w_order].l << 17;
12484         REG_WR(sc, PXP2_REG_PSWRQ_BW_RD, val);
12485
12486         val =  read_arb_data[NUM_RD_Q - 1][r_order].add;
12487         val += read_arb_data[NUM_RD_Q - 1][r_order].ubound << 10;
12488         val += read_arb_data[NUM_RD_Q - 1][r_order].l << 17;
12489         REG_WR(sc, PXP2_REG_PSWRQ_BW_WR, val);
12490
12491         REG_WR(sc, PXP2_REG_RQ_WR_MBS0, w_order);
12492         REG_WR(sc, PXP2_REG_RQ_WR_MBS1, w_order);
12493         REG_WR(sc, PXP2_REG_RQ_RD_MBS0, r_order);
12494         REG_WR(sc, PXP2_REG_RQ_RD_MBS1, r_order);
12495
12496         if (r_order == MAX_RD_ORD)
12497                 REG_WR(sc, PXP2_REG_RQ_PDR_LIMIT, 0xe00);
12498
12499         REG_WR(sc, PXP2_REG_WR_USDMDP_TH, (0x18 << w_order));
12500
12501         if (CHIP_IS_E1H(sc)) {
12502                 /*    MPS      w_order     optimal TH      presently TH
12503                  *    128         0             0               2
12504                  *    256         1             1               3
12505                  *    >=512       2             2               3
12506                  */
12507                 val = ((w_order == 0) ? 2 : 3);
12508                 REG_WR(sc, PXP2_REG_WR_HC_MPS, val);
12509                 REG_WR(sc, PXP2_REG_WR_USDM_MPS, val);
12510                 REG_WR(sc, PXP2_REG_WR_CSDM_MPS, val);
12511                 REG_WR(sc, PXP2_REG_WR_TSDM_MPS, val);
12512                 REG_WR(sc, PXP2_REG_WR_XSDM_MPS, val);
12513                 REG_WR(sc, PXP2_REG_WR_QM_MPS, val);
12514                 REG_WR(sc, PXP2_REG_WR_TM_MPS, val);
12515                 REG_WR(sc, PXP2_REG_WR_SRC_MPS, val);
12516                 REG_WR(sc, PXP2_REG_WR_DBG_MPS, val);
12517                 REG_WR(sc, PXP2_REG_WR_DMAE_MPS, 2); /* DMAE is special */
12518                 REG_WR(sc, PXP2_REG_WR_CDU_MPS, val);
12519         }
12520 }
12521
12522 static void
12523 bxe_init_pxp(struct bxe_softc *sc)
12524 {
12525         uint16_t devctl;
12526         int r_order, w_order;
12527
12528         devctl = pci_read_config(sc->dev,
12529             sc->pcie_cap + PCI_EXP_DEVCTL, 2);
12530         DBPRINT(sc, (BXE_VERBOSE_LOAD | BXE_VERBOSE_RESET),
12531             "%s(): Read 0x%x from devctl\n", __FUNCTION__, devctl);
12532         w_order = ((devctl & PCI_EXP_DEVCTL_PAYLOAD) >> 5);
12533         if (sc->mrrs == -1)
12534                 r_order = ((devctl & PCI_EXP_DEVCTL_READRQ) >> 12);
12535         else {
12536                 DBPRINT(sc, (BXE_VERBOSE_LOAD | BXE_VERBOSE_RESET),
12537                     "%s(): Force MRRS read order to %d\n",
12538                     __FUNCTION__, sc->mrrs);
12539                 r_order = sc->mrrs;
12540         }
12541
12542         bxe_init_pxp_arb(sc, r_order, w_order);
12543 }
12544
12545 static void
12546 bxe_setup_fan_failure_detection(struct bxe_softc *sc)
12547 {
12548         uint32_t phy_type, val;
12549         int is_required, port;
12550
12551         is_required = 0;
12552         if (NOMCP(sc))
12553                 return;
12554
12555         val = SHMEM_RD(sc, dev_info.shared_hw_config.config2) &
12556             SHARED_HW_CFG_FAN_FAILURE_MASK;
12557
12558         if (val == SHARED_HW_CFG_FAN_FAILURE_ENABLED)
12559                 is_required = 1;
12560
12561         /*
12562          * The fan failure mechanism is usually related to the PHY type since
12563          * the power consumption of the board is affected by the PHY. Currently,
12564          * fan is required for most designs with SFX7101, BCM8727 and BCM8481.
12565          */
12566         else if (val == SHARED_HW_CFG_FAN_FAILURE_PHY_TYPE)
12567                 for (port = PORT_0; port < PORT_MAX; port++) {
12568                         phy_type = SHMEM_RD(sc,
12569                             dev_info.port_hw_config[port].external_phy_config) &
12570                             PORT_HW_CFG_XGXS_EXT_PHY_TYPE_MASK;
12571                         is_required |=
12572                         ((phy_type == PORT_HW_CFG_XGXS_EXT_PHY_TYPE_SFX7101) ||
12573                          (phy_type == PORT_HW_CFG_XGXS_EXT_PHY_TYPE_BCM8727) ||
12574                          (phy_type == PORT_HW_CFG_XGXS_EXT_PHY_TYPE_BCM8481));
12575                 }
12576
12577         if (is_required == 0)
12578                 return;
12579
12580         /* Fan failure is indicated by SPIO 5. */
12581         bxe_set_spio(sc, MISC_REGISTERS_SPIO_5, MISC_REGISTERS_SPIO_INPUT_HI_Z);
12582
12583         /* Set to active low mode. */
12584         val = REG_RD(sc, MISC_REG_SPIO_INT);
12585         val |= ((1 << MISC_REGISTERS_SPIO_5) <<
12586                                         MISC_REGISTERS_SPIO_INT_OLD_SET_POS);
12587         REG_WR(sc, MISC_REG_SPIO_INT, val);
12588
12589         /* Enable interrupt to signal the IGU. */
12590         val = REG_RD(sc, MISC_REG_SPIO_EVENT_EN);
12591         val |= (1 << MISC_REGISTERS_SPIO_5);
12592         REG_WR(sc, MISC_REG_SPIO_EVENT_EN, val);
12593 }
12594
12595 /*
12596  * Common initialization.
12597  *
12598  * Returns:
12599  *   0 = Success, !0 = Failure.
12600  */
12601 static int
12602 bxe_init_common(struct bxe_softc *sc)
12603 {
12604         uint32_t val;
12605         int i, rc;
12606
12607         rc = 0;
12608         DBENTER(BXE_VERBOSE_LOAD | BXE_VERBOSE_RESET);
12609
12610         /* Reset all blocks within the chip except the BMAC. */
12611         bxe_reset_common(sc);
12612         DELAY(30000);
12613         REG_WR(sc, GRCBASE_MISC + MISC_REGISTERS_RESET_REG_1_SET, 0xffffffff);
12614         REG_WR(sc, GRCBASE_MISC + MISC_REGISTERS_RESET_REG_2_SET, 0xfffc);
12615         DELAY(30000);
12616
12617         bxe_init_block(sc, MISC_BLOCK, COMMON_STAGE);
12618         if (CHIP_IS_E1H(sc))
12619                 REG_WR(sc, MISC_REG_E1HMF_MODE, IS_E1HMF(sc));
12620
12621         REG_WR(sc, MISC_REG_LCPLL_CTRL_REG_2, 0x100);
12622         DELAY(30000);
12623         REG_WR(sc, MISC_REG_LCPLL_CTRL_REG_2, 0x0);
12624
12625         bxe_init_block(sc, PXP_BLOCK, COMMON_STAGE);
12626         if (CHIP_IS_E1(sc)) {
12627                 /*
12628                  * Enable HW interrupt from PXP on USDM overflow
12629                  * bit 16 on INT_MASK_0.
12630                  */
12631                 REG_WR(sc, PXP_REG_PXP_INT_MASK_0, 0);
12632         }
12633
12634         bxe_init_block(sc, PXP2_BLOCK, COMMON_STAGE);
12635         bxe_init_pxp(sc);
12636
12637 #ifdef __BIG_ENDIAN
12638         REG_WR(sc, PXP2_REG_RQ_QM_ENDIAN_M, 1);
12639         REG_WR(sc, PXP2_REG_RQ_TM_ENDIAN_M, 1);
12640         REG_WR(sc, PXP2_REG_RQ_SRC_ENDIAN_M, 1);
12641         REG_WR(sc, PXP2_REG_RQ_CDU_ENDIAN_M, 1);
12642         REG_WR(sc, PXP2_REG_RQ_DBG_ENDIAN_M, 1);
12643         /* Make sure this value is 0. */
12644         REG_WR(sc, PXP2_REG_RQ_HC_ENDIAN_M, 0);
12645
12646         REG_WR(sc, PXP2_REG_RD_QM_SWAP_MODE, 1);
12647         REG_WR(sc, PXP2_REG_RD_TM_SWAP_MODE, 1);
12648         REG_WR(sc, PXP2_REG_RD_SRC_SWAP_MODE, 1);
12649         REG_WR(sc, PXP2_REG_RD_CDURD_SWAP_MODE, 1);
12650 #endif
12651
12652         REG_WR(sc, PXP2_REG_RQ_CDU_P_SIZE, 2);
12653
12654         /* Let the HW do it's magic ... */
12655         DELAY(100000);
12656         /* Finish the PXP initialization. */
12657         val = REG_RD(sc, PXP2_REG_RQ_CFG_DONE);
12658         if (val != 1) {
12659                 BXE_PRINTF("%s(%d): PXP2 CFG failed!\n", __FILE__, __LINE__);
12660                 rc = EBUSY;
12661                 goto bxe_init_common_exit;
12662         }
12663
12664         val = REG_RD(sc, PXP2_REG_RD_INIT_DONE);
12665         if (val != 1) {
12666                 BXE_PRINTF("%s(%d): PXP2 RD_INIT failed!\n", __FILE__,
12667                     __LINE__);
12668                 rc = EBUSY;
12669                 goto bxe_init_common_exit;
12670         }
12671
12672         REG_WR(sc, PXP2_REG_RQ_DISABLE_INPUTS, 0);
12673         REG_WR(sc, PXP2_REG_RD_DISABLE_INPUTS, 0);
12674
12675         bxe_init_block(sc, DMAE_BLOCK, COMMON_STAGE);
12676
12677         sc->dmae_ready = 1;
12678         bxe_init_fill(sc, TSEM_REG_PRAM, 0, 8);
12679
12680         bxe_init_block(sc, TCM_BLOCK, COMMON_STAGE);
12681         bxe_init_block(sc, UCM_BLOCK, COMMON_STAGE);
12682         bxe_init_block(sc, CCM_BLOCK, COMMON_STAGE);
12683         bxe_init_block(sc, XCM_BLOCK, COMMON_STAGE);
12684
12685         bxe_read_dmae(sc, XSEM_REG_PASSIVE_BUFFER, 3);
12686         bxe_read_dmae(sc, CSEM_REG_PASSIVE_BUFFER, 3);
12687         bxe_read_dmae(sc, TSEM_REG_PASSIVE_BUFFER, 3);
12688         bxe_read_dmae(sc, USEM_REG_PASSIVE_BUFFER, 3);
12689
12690         bxe_init_block(sc, QM_BLOCK, COMMON_STAGE);
12691
12692         /* Soft reset pulse. */
12693         REG_WR(sc, QM_REG_SOFT_RESET, 1);
12694         REG_WR(sc, QM_REG_SOFT_RESET, 0);
12695
12696         bxe_init_block(sc, DQ_BLOCK, COMMON_STAGE);
12697         REG_WR(sc, DORQ_REG_DPM_CID_OFST, BCM_PAGE_SHIFT);
12698
12699         REG_WR(sc, DORQ_REG_DORQ_INT_MASK, 0);
12700
12701         bxe_init_block(sc, BRB1_BLOCK, COMMON_STAGE);
12702         bxe_init_block(sc, PRS_BLOCK, COMMON_STAGE);
12703         REG_WR(sc, PRS_REG_A_PRSU_20, 0xf);
12704
12705         if (CHIP_IS_E1H(sc))
12706                 REG_WR(sc, PRS_REG_E1HOV_MODE, IS_E1HMF(sc));
12707
12708         bxe_init_block(sc, TSDM_BLOCK, COMMON_STAGE);
12709         bxe_init_block(sc, CSDM_BLOCK, COMMON_STAGE);
12710         bxe_init_block(sc, USDM_BLOCK, COMMON_STAGE);
12711         bxe_init_block(sc, XSDM_BLOCK, COMMON_STAGE);
12712         /* Clear STORM processor memory. */
12713         bxe_init_fill(sc, TSEM_REG_FAST_MEMORY, 0, STORM_INTMEM_SIZE(sc));
12714         bxe_init_fill(sc, USEM_REG_FAST_MEMORY, 0, STORM_INTMEM_SIZE(sc));
12715         bxe_init_fill(sc, CSEM_REG_FAST_MEMORY, 0, STORM_INTMEM_SIZE(sc));
12716         bxe_init_fill(sc, XSEM_REG_FAST_MEMORY, 0, STORM_INTMEM_SIZE(sc));
12717
12718         bxe_init_block(sc, TSEM_BLOCK, COMMON_STAGE);
12719         bxe_init_block(sc, USEM_BLOCK, COMMON_STAGE);
12720         bxe_init_block(sc, CSEM_BLOCK, COMMON_STAGE);
12721         bxe_init_block(sc, XSEM_BLOCK, COMMON_STAGE);
12722
12723         /* Sync semi rtc. */
12724         REG_WR(sc, GRCBASE_MISC + MISC_REGISTERS_RESET_REG_1_CLEAR, 0x80000000);
12725         REG_WR(sc, GRCBASE_MISC + MISC_REGISTERS_RESET_REG_1_SET, 0x80000000);
12726
12727         bxe_init_block(sc, UPB_BLOCK, COMMON_STAGE);
12728         bxe_init_block(sc, XPB_BLOCK, COMMON_STAGE);
12729         bxe_init_block(sc, PBF_BLOCK, COMMON_STAGE);
12730
12731         REG_WR(sc, SRC_REG_SOFT_RST, 1);
12732         /* Setup RSS/multi-queue hasking keys. */
12733         for (i = SRC_REG_KEYRSS0_0; i <= SRC_REG_KEYRSS1_9; i += 4)
12734                 REG_WR(sc, i, 0xc0cac01a);
12735
12736         bxe_init_block(sc, SRCH_BLOCK, COMMON_STAGE);
12737
12738         REG_WR(sc, SRC_REG_SOFT_RST, 0);
12739
12740         /* Make sure the cdu_context structure has the right size. */
12741         if (sizeof(union cdu_context) != 1024) {
12742                 BXE_PRINTF("%s(%d): Invalid size for context (%ld != 1024)!\n",
12743                     __FILE__, __LINE__, (long)sizeof(union cdu_context));
12744                 rc = EBUSY;
12745                 goto bxe_init_common_exit;
12746         }
12747
12748         bxe_init_block(sc, CDU_BLOCK, COMMON_STAGE);
12749
12750         /*
12751          * val = (num_context_in_page << 24) +
12752          * (context_waste_size << 12) +
12753          * context_line_size.
12754          */
12755
12756         val = (4 << 24) + (0 << 12) + 1024;
12757         REG_WR(sc, CDU_REG_CDU_GLOBAL_PARAMS, val);
12758
12759         bxe_init_block(sc, CFC_BLOCK, COMMON_STAGE);
12760         REG_WR(sc, CFC_REG_INIT_REG, 0x7FF);
12761         /* Enable context validation interrupt from CFC. */
12762         REG_WR(sc, CFC_REG_CFC_INT_MASK, 0);
12763
12764         /* Set the thresholds to prevent CFC/CDU race. */
12765         REG_WR(sc, CFC_REG_DEBUG0, 0x20020000);
12766
12767         bxe_init_block(sc, HC_BLOCK, COMMON_STAGE);
12768         bxe_init_block(sc, MISC_AEU_BLOCK, COMMON_STAGE);
12769
12770         bxe_init_block(sc, PXPCS_BLOCK, COMMON_STAGE);
12771         /* Clear PCIe block debug status bits. */
12772         REG_WR(sc, 0x2814, 0xffffffff);
12773         REG_WR(sc, 0x3820, 0xffffffff);
12774
12775         bxe_init_block(sc, EMAC0_BLOCK, COMMON_STAGE);
12776         bxe_init_block(sc, EMAC1_BLOCK, COMMON_STAGE);
12777         bxe_init_block(sc, DBU_BLOCK, COMMON_STAGE);
12778         bxe_init_block(sc, DBG_BLOCK, COMMON_STAGE);
12779
12780         bxe_init_block(sc, NIG_BLOCK, COMMON_STAGE);
12781         if (CHIP_IS_E1H(sc)) {
12782                 REG_WR(sc, NIG_REG_LLH_MF_MODE, IS_E1HMF(sc));
12783                 REG_WR(sc, NIG_REG_LLH_E1HOV_MODE, IS_E1HOV(sc));
12784         }
12785
12786         /* Finish CFC initialization. */
12787         val = bxe_reg_poll(sc, CFC_REG_LL_INIT_DONE, 1, 100, 10);
12788         if (val != 1) {
12789                 BXE_PRINTF("%s(%d): CFC LL_INIT failed!\n",
12790                     __FILE__, __LINE__);
12791                 rc = EBUSY;
12792                 goto bxe_init_common_exit;
12793         }
12794
12795         val = bxe_reg_poll(sc, CFC_REG_AC_INIT_DONE, 1, 100, 10);
12796         if (val != 1) {
12797                 BXE_PRINTF("%s(%d): CFC AC_INIT failed!\n",
12798                      __FILE__, __LINE__);
12799                 rc = EBUSY;
12800                 goto bxe_init_common_exit;
12801         }
12802
12803         val = bxe_reg_poll(sc, CFC_REG_CAM_INIT_DONE, 1, 100, 10);
12804         if (val != 1) {
12805                 BXE_PRINTF("%s(%d): CFC CAM_INIT failed!\n",
12806                     __FILE__, __LINE__);
12807                 rc = EBUSY;
12808                 goto bxe_init_common_exit;
12809         }
12810
12811         REG_WR(sc, CFC_REG_DEBUG0, 0);
12812
12813         /* Read NIG statistic and check for first load since powerup. */
12814         bxe_read_dmae(sc, NIG_REG_STAT2_BRB_OCTET, 2);
12815         val = *BXE_SP(sc, wb_data[0]);
12816
12817         /* Do internal memory self test only after a full power cycle. */
12818         if ((CHIP_IS_E1(sc)) && (val == 0) && bxe_int_mem_test(sc)) {
12819                 BXE_PRINTF("%s(%d): Internal memory self-test failed!\n",
12820                     __FILE__, __LINE__);
12821                 rc = EBUSY;
12822                 goto bxe_init_common_exit;
12823         }
12824
12825         /* Handle any board specific initialization. */
12826         switch (XGXS_EXT_PHY_TYPE(sc->link_params.ext_phy_config)) {
12827         case PORT_HW_CFG_XGXS_EXT_PHY_TYPE_BCM8072:
12828         case PORT_HW_CFG_XGXS_EXT_PHY_TYPE_BCM8073:
12829         case PORT_HW_CFG_XGXS_EXT_PHY_TYPE_BCM8726:
12830         case PORT_HW_CFG_XGXS_EXT_PHY_TYPE_BCM8727:
12831                 break;
12832
12833         default:
12834                 break;
12835         }
12836
12837         bxe_setup_fan_failure_detection(sc);
12838
12839         /* Clear PXP2 attentions. */
12840         REG_RD(sc, PXP2_REG_PXP2_INT_STS_CLR_0);
12841
12842         bxe_enable_blocks_attention(sc);
12843
12844         if (!NOMCP(sc)) {
12845                 bxe_acquire_phy_lock(sc);
12846                 bxe_common_init_phy(sc, sc->common.shmem_base);
12847                 bxe_release_phy_lock(sc);
12848         } else
12849                 BXE_PRINTF(
12850                     "%s(%d): Bootcode is missing - cannot initialize PHY!\n",
12851                     __FILE__, __LINE__);
12852
12853 bxe_init_common_exit:
12854         DBEXIT(BXE_VERBOSE_LOAD | BXE_VERBOSE_RESET);
12855         return (rc);
12856 }
12857
12858 /*
12859  * Port initialization.
12860  *
12861  * Returns:
12862  *   0 = Success, !0 = Failure.
12863  */
12864 static int
12865 bxe_init_port(struct bxe_softc *sc)
12866 {
12867         uint32_t val, low, high;
12868         uint32_t swap_val, swap_override, aeu_gpio_mask, offset;
12869         uint32_t reg_addr;
12870         int init_stage, port;
12871
12872         port = BP_PORT(sc);
12873         init_stage = port ? PORT1_STAGE : PORT0_STAGE;
12874
12875         DBENTER(BXE_VERBOSE_LOAD | BXE_VERBOSE_RESET);
12876
12877         DBPRINT(sc, (BXE_VERBOSE_LOAD | BXE_VERBOSE_RESET),
12878             "%s(): Initializing port %d.\n", __FUNCTION__, port);
12879
12880         REG_WR(sc, NIG_REG_MASK_INTERRUPT_PORT0 + port * 4, 0);
12881
12882         bxe_init_block(sc, PXP_BLOCK, init_stage);
12883         bxe_init_block(sc, PXP2_BLOCK, init_stage);
12884
12885         bxe_init_block(sc, TCM_BLOCK, init_stage);
12886         bxe_init_block(sc, UCM_BLOCK, init_stage);
12887         bxe_init_block(sc, CCM_BLOCK, init_stage);
12888         bxe_init_block(sc, XCM_BLOCK, init_stage);
12889
12890         bxe_init_block(sc, DQ_BLOCK, init_stage);
12891
12892         bxe_init_block(sc, BRB1_BLOCK, init_stage);
12893
12894         /* Determine the pause threshold for the BRB */
12895         if (IS_E1HMF(sc))
12896                 low = (sc->bxe_flags & BXE_ONE_PORT_FLAG) ? 160 : 246;
12897         else if (sc->bxe_ifp->if_mtu > 4096) {
12898                 if (sc->bxe_flags & BXE_ONE_PORT_FLAG)
12899                         low = 160;
12900                 else {
12901                         val = sc->bxe_ifp->if_mtu;
12902                         /* (24*1024 + val*4)/256 */
12903                         low = 96 + (val/64) + ((val % 64) ? 1 : 0);
12904                 }
12905         } else
12906                 low = (sc->bxe_flags & BXE_ONE_PORT_FLAG) ? 80 : 160;
12907         high = low + 56;        /* 14 * 1024 / 256 */
12908
12909         REG_WR(sc, BRB1_REG_PAUSE_LOW_THRESHOLD_0 + port * 4, low);
12910         REG_WR(sc, BRB1_REG_PAUSE_HIGH_THRESHOLD_0 + port * 4, high);
12911
12912         /* Port PRS comes here. */
12913         bxe_init_block(sc, PRS_BLOCK, init_stage);
12914
12915         bxe_init_block(sc, TSDM_BLOCK, init_stage);
12916         bxe_init_block(sc, CSDM_BLOCK, init_stage);
12917         bxe_init_block(sc, USDM_BLOCK, init_stage);
12918         bxe_init_block(sc, XSDM_BLOCK, init_stage);
12919
12920         bxe_init_block(sc, TSEM_BLOCK, init_stage);
12921         bxe_init_block(sc, USEM_BLOCK, init_stage);
12922         bxe_init_block(sc, CSEM_BLOCK, init_stage);
12923         bxe_init_block(sc, XSEM_BLOCK, init_stage);
12924
12925         bxe_init_block(sc, UPB_BLOCK, init_stage);
12926         bxe_init_block(sc, XPB_BLOCK, init_stage);
12927
12928         bxe_init_block(sc, PBF_BLOCK, init_stage);
12929
12930         /* Configure PBF to work without pause for MTU = 9000. */
12931         REG_WR(sc, PBF_REG_P0_PAUSE_ENABLE + port * 4, 0);
12932
12933         /* Update threshold. */
12934         REG_WR(sc, PBF_REG_P0_ARB_THRSH + port * 4, (9040/16));
12935         /* Update initial credit. */
12936         REG_WR(sc, PBF_REG_P0_INIT_CRD + port * 4, (9040/16) + 553 - 22);
12937
12938         /* Probe changes. */
12939         REG_WR(sc, PBF_REG_INIT_P0 + port * 4, 1);
12940         DELAY(5000);
12941         REG_WR(sc, PBF_REG_INIT_P0 + port * 4, 0);
12942
12943         bxe_init_block(sc, CDU_BLOCK, init_stage);
12944         bxe_init_block(sc, CFC_BLOCK, init_stage);
12945
12946         if (CHIP_IS_E1(sc)) {
12947                 REG_WR(sc, HC_REG_LEADING_EDGE_0 + port * 8, 0);
12948                 REG_WR(sc, HC_REG_TRAILING_EDGE_0 + port * 8, 0);
12949         }
12950
12951         bxe_init_block(sc, HC_BLOCK, init_stage);
12952
12953         bxe_init_block(sc, MISC_AEU_BLOCK, init_stage);
12954         /*
12955          * init aeu_mask_attn_func_0/1:
12956          *  - SF mode: bits 3-7 are masked. only bits 0-2 are in use
12957          *  - MF mode: bit 3 is masked. bits 0-2 are in use as in SF
12958          *             bits 4-7 are used for "per vn group attention"
12959          */
12960         REG_WR(sc, MISC_REG_AEU_MASK_ATTN_FUNC_0 + port * 4,
12961             (IS_E1HMF(sc) ? 0xF7 : 0x7));
12962
12963         bxe_init_block(sc, PXPCS_BLOCK, init_stage);
12964         bxe_init_block(sc, EMAC0_BLOCK, init_stage);
12965         bxe_init_block(sc, EMAC1_BLOCK, init_stage);
12966         bxe_init_block(sc, DBU_BLOCK, init_stage);
12967         bxe_init_block(sc, DBG_BLOCK, init_stage);
12968
12969         bxe_init_block(sc, NIG_BLOCK, init_stage);
12970
12971         REG_WR(sc, NIG_REG_XGXS_SERDES0_MODE_SEL + port * 4, 1);
12972
12973         if (CHIP_IS_E1H(sc)) {
12974                 /* Enable outer VLAN support if required. */
12975                 REG_WR(sc, NIG_REG_LLH0_BRB1_DRV_MASK_MF + port * 4,
12976                     (IS_E1HOV(sc) ? 0x1 : 0x2));
12977         }
12978
12979         REG_WR(sc, NIG_REG_LLFC_ENABLE_0 + port * 4, 0);
12980         REG_WR(sc, NIG_REG_LLFC_OUT_EN_0 + port * 4, 0);
12981         REG_WR(sc, NIG_REG_PAUSE_ENABLE_0 + port * 4, 1);
12982
12983         bxe_init_block(sc, MCP_BLOCK, init_stage);
12984         bxe_init_block(sc, DMAE_BLOCK, init_stage);
12985
12986         switch (XGXS_EXT_PHY_TYPE(sc->link_params.ext_phy_config)) {
12987         case PORT_HW_CFG_XGXS_EXT_PHY_TYPE_BCM8726:
12988                 bxe_set_gpio(sc, MISC_REGISTERS_GPIO_3,
12989                     MISC_REGISTERS_GPIO_INPUT_HI_Z, port);
12990
12991                 /*
12992                  * The GPIO should be swapped if the swap register is
12993                  * set and active.
12994                  */
12995                 swap_val = REG_RD(sc, NIG_REG_PORT_SWAP);
12996                 swap_override = REG_RD(sc, NIG_REG_STRAP_OVERRIDE);
12997
12998                 /* Select function upon port-swap configuration. */
12999                 if (port == 0) {
13000                         offset = MISC_REG_AEU_ENABLE1_FUNC_0_OUT_0;
13001                         aeu_gpio_mask = (swap_val && swap_override) ?
13002                             AEU_INPUTS_ATTN_BITS_GPIO3_FUNCTION_1 :
13003                             AEU_INPUTS_ATTN_BITS_GPIO3_FUNCTION_0;
13004                 } else {
13005                         offset = MISC_REG_AEU_ENABLE1_FUNC_1_OUT_0;
13006                         aeu_gpio_mask = (swap_val && swap_override) ?
13007                             AEU_INPUTS_ATTN_BITS_GPIO3_FUNCTION_0 :
13008                             AEU_INPUTS_ATTN_BITS_GPIO3_FUNCTION_1;
13009                 }
13010                 val = REG_RD(sc, offset);
13011                 /* Add GPIO3 to group. */
13012                 val |= aeu_gpio_mask;
13013                 REG_WR(sc, offset, val);
13014                 break;
13015         case PORT_HW_CFG_XGXS_EXT_PHY_TYPE_SFX7101:
13016         case PORT_HW_CFG_XGXS_EXT_PHY_TYPE_BCM8727:
13017                 /* Add SPIO 5 to group 0. */
13018                 reg_addr = port ? MISC_REG_AEU_ENABLE1_FUNC_1_OUT_0 :
13019                     MISC_REG_AEU_ENABLE1_FUNC_0_OUT_0;
13020                 val = REG_RD(sc, reg_addr);
13021                 val |= AEU_INPUTS_ATTN_BITS_SPIO5;
13022                 REG_WR(sc, reg_addr, val);
13023                 break;
13024         default:
13025                 break;
13026         }
13027
13028         bxe__link_reset(sc);
13029
13030         DBEXIT(BXE_VERBOSE_LOAD | BXE_VERBOSE_RESET);
13031
13032         return (0);
13033 }
13034
13035 #define ILT_PER_FUNC            (768/2)
13036 #define FUNC_ILT_BASE(func)     (func * ILT_PER_FUNC)
13037 /*
13038  * The phys address is shifted right 12 bits and has an added 1=valid
13039  * bit added to the 53rd bit (bit 52) then since this is a wide
13040  * register(TM) we split it into two 32 bit writes.
13041  */
13042 #define ONCHIP_ADDR1(x)         ((uint32_t)(((uint64_t)x >> 12) & 0xFFFFFFFF))
13043 #define ONCHIP_ADDR2(x)         ((uint32_t)((1 << 20) | ((uint64_t)x >> 44)))
13044 #define PXP_ONE_ILT(x)          (((x) << 10) | x)
13045 #define PXP_ILT_RANGE(f, l)     (((l) << 10) | f)
13046 #define CNIC_ILT_LINES          0
13047
13048 /*
13049  * ILT write.
13050  *
13051  * Returns:
13052  *   None.
13053  */
13054 static void
13055 bxe_ilt_wr(struct bxe_softc *sc, uint32_t index, bus_addr_t addr)
13056 {
13057         int reg;
13058
13059         DBENTER(BXE_INSANE_LOAD | BXE_INSANE_RESET);
13060
13061         if (CHIP_IS_E1H(sc))
13062                 reg = PXP2_REG_RQ_ONCHIP_AT_B0 + index * 8;
13063         else
13064                 reg = PXP2_REG_RQ_ONCHIP_AT + index * 8;
13065
13066         bxe_wb_wr(sc, reg, ONCHIP_ADDR1(addr), ONCHIP_ADDR2(addr));
13067
13068         DBEXIT(BXE_INSANE_LOAD | BXE_INSANE_RESET);
13069 }
13070
13071 /*
13072  * Initialize a function.
13073  *
13074  * Returns:
13075  *   0 = Success, !0 = Failure.
13076  */
13077 static int
13078 bxe_init_func(struct bxe_softc *sc)
13079 {
13080         uint32_t addr, val;
13081         int func, i, port;
13082
13083         port = BP_PORT(sc);
13084         func = BP_FUNC(sc);
13085
13086         DBENTER(BXE_VERBOSE_LOAD | BXE_VERBOSE_RESET);
13087
13088         DBPRINT(sc, (BXE_VERBOSE_LOAD | BXE_VERBOSE_RESET),
13089             "%s(): Initializing port %d, function %d.\n", __FUNCTION__, port,
13090             func);
13091
13092         /* Set MSI reconfigure capability. */
13093         addr = (port ? HC_REG_CONFIG_1 : HC_REG_CONFIG_0);
13094         val = REG_RD(sc, addr);
13095         val |= HC_CONFIG_0_REG_MSI_ATTN_EN_0;
13096         REG_WR(sc, addr, val);
13097
13098         i = FUNC_ILT_BASE(func);
13099
13100         bxe_ilt_wr(sc, i, BXE_SP_MAPPING(sc, context));
13101
13102         if (CHIP_IS_E1H(sc)) {
13103                 REG_WR(sc, PXP2_REG_RQ_CDU_FIRST_ILT, i);
13104                 REG_WR(sc, PXP2_REG_RQ_CDU_LAST_ILT, i + CNIC_ILT_LINES);
13105         } else /* E1 */
13106                 REG_WR(sc, PXP2_REG_PSWRQ_CDU0_L2P + func * 4,
13107                     PXP_ILT_RANGE(i, i + CNIC_ILT_LINES));
13108
13109         if (CHIP_IS_E1H(sc)) {
13110                 bxe_init_block(sc, MISC_BLOCK, FUNC0_STAGE + func);
13111                 bxe_init_block(sc, TCM_BLOCK, FUNC0_STAGE + func);
13112                 bxe_init_block(sc, UCM_BLOCK, FUNC0_STAGE + func);
13113                 bxe_init_block(sc, CCM_BLOCK, FUNC0_STAGE + func);
13114                 bxe_init_block(sc, XCM_BLOCK, FUNC0_STAGE + func);
13115                 bxe_init_block(sc, TSEM_BLOCK, FUNC0_STAGE + func);
13116                 bxe_init_block(sc, USEM_BLOCK, FUNC0_STAGE + func);
13117                 bxe_init_block(sc, CSEM_BLOCK, FUNC0_STAGE + func);
13118                 bxe_init_block(sc, XSEM_BLOCK, FUNC0_STAGE + func);
13119
13120                 REG_WR(sc, NIG_REG_LLH0_FUNC_EN + port * 8, 1);
13121                 REG_WR(sc, NIG_REG_LLH0_FUNC_VLAN_ID + port * 8, sc->e1hov);
13122         }
13123
13124         /* Host Coalescing initialization per function. */
13125         if (CHIP_IS_E1H(sc)) {
13126                 REG_WR(sc, MISC_REG_AEU_GENERAL_ATTN_12 + func * 4, 0);
13127                 REG_WR(sc, HC_REG_LEADING_EDGE_0 + port * 8, 0);
13128                 REG_WR(sc, HC_REG_TRAILING_EDGE_0 + port * 8, 0);
13129         }
13130
13131         bxe_init_block(sc, HC_BLOCK, FUNC0_STAGE + func);
13132
13133         /* Reset PCIe block debug values. */
13134         REG_WR(sc, 0x2114, 0xffffffff);
13135         REG_WR(sc, 0x2120, 0xffffffff);
13136
13137         DBEXIT(BXE_VERBOSE_LOAD | BXE_VERBOSE_RESET);
13138
13139         return (0);
13140 }
13141
13142 /*
13143  *
13144  * Returns:
13145  *   0 = Failure, !0 = Failure.
13146  */
13147 static int
13148 bxe_init_hw(struct bxe_softc *sc, uint32_t load_code)
13149 {
13150         int func, i, rc;
13151
13152         rc = 0;
13153         DBENTER(BXE_VERBOSE_LOAD | BXE_VERBOSE_RESET);
13154
13155         sc->dmae_ready = 0;
13156         switch (load_code) {
13157         case FW_MSG_CODE_DRV_LOAD_COMMON:
13158                 rc = bxe_init_common(sc);
13159                 if (rc)
13160                         goto bxe_init_hw_exit;
13161                 /* FALLTHROUGH */
13162         case FW_MSG_CODE_DRV_LOAD_PORT:
13163                 sc->dmae_ready = 1;
13164                 rc = bxe_init_port(sc);
13165                 if (rc)
13166                         goto bxe_init_hw_exit;
13167                 /* FALLTHROUGH */
13168         case FW_MSG_CODE_DRV_LOAD_FUNCTION:
13169                 sc->dmae_ready = 1;
13170                 rc = bxe_init_func(sc);
13171                 if (rc)
13172                         goto bxe_init_hw_exit;
13173                 break;
13174         default:
13175                 DBPRINT(sc, BXE_WARN,
13176                     "%s(): Unknown load_code (0x%08X) from MCP!\n",
13177                     __FUNCTION__, load_code);
13178                 break;
13179         }
13180
13181         /* Fetch additional config data if the bootcode is running. */
13182         if (!NOMCP(sc)) {
13183                 func = BP_FUNC(sc);
13184                 /* Fetch the pulse sequence number. */
13185                 sc->fw_drv_pulse_wr_seq = (SHMEM_RD(sc,
13186                     func_mb[func].drv_pulse_mb) & DRV_PULSE_SEQ_MASK);
13187         }
13188
13189         /* Clear the default status block. */
13190         bxe_zero_def_sb(sc);
13191         for (i = 0; i < sc->num_queues; i++)
13192                 bxe_zero_sb(sc, BP_L_ID(sc) + i);
13193
13194 bxe_init_hw_exit:
13195         DBEXIT(BXE_VERBOSE_LOAD | BXE_VERBOSE_RESET);
13196
13197         return (rc);
13198 }
13199
13200 /*
13201  * Send a firmware command and wait for the response.
13202  *
13203  * Post a command to shared memory for the bootcode running on the MCP and
13204  * stall until the bootcode responds or a timeout occurs.
13205  *
13206  * Returns:
13207  *   0 = Failure, otherwise firmware response code (FW_MSG_CODE_*).
13208  */
13209 static int
13210 bxe_fw_command(struct bxe_softc *sc, uint32_t command)
13211 {
13212         uint32_t cnt, rc, seq;
13213         int func;
13214
13215         func = BP_FUNC(sc);
13216         seq = ++sc->fw_seq;
13217         rc = 0;
13218         cnt = 1;
13219
13220         DBRUNMSG(BXE_VERBOSE, bxe_decode_mb_msgs(sc, (command | seq), 0));
13221
13222         BXE_FWMB_LOCK(sc);
13223
13224         /* Write the command to the shared memory mailbox. */
13225         SHMEM_WR(sc, func_mb[func].drv_mb_header, (command | seq));
13226
13227         /* Wait up to 2 seconds for a response. */
13228         do {
13229                 /* Wait 10ms for a response. */
13230                 DELAY(10000);
13231
13232                 /* Pickup the response. */
13233                 rc = SHMEM_RD(sc, func_mb[func].fw_mb_header);
13234         } while ((seq != (rc & FW_MSG_SEQ_NUMBER_MASK)) && (cnt++ < 400));
13235
13236         DBRUNMSG(BXE_VERBOSE, bxe_decode_mb_msgs(sc, 0, rc));
13237
13238         /* Make sure we read the right response. */
13239         if (seq == (rc & FW_MSG_SEQ_NUMBER_MASK ))
13240                 rc &= FW_MSG_CODE_MASK;
13241         else {
13242                 BXE_PRINTF("%s(%d): Bootcode failed to respond!\n",
13243                     __FILE__, __LINE__);
13244                 DBRUN(bxe_dump_fw(sc));
13245                 rc = 0;
13246         }
13247
13248         BXE_FWMB_UNLOCK(sc);
13249         return (rc);
13250 }
13251
13252 /*
13253  * Allocate a block of memory and map it for DMA.  No partial
13254  * completions allowed, release any resources acquired if we
13255  * can't acquire all resources.
13256  *
13257  * Returns:
13258  *   0 = Success, !0 = Failure
13259  *
13260  * Modifies:
13261  *   dma->paddr
13262  *   dma->vaddr
13263  *   dma->tag
13264  *   dma->map
13265  *   dma->size
13266  *
13267  */
13268 static int
13269 bxe_dma_malloc(struct bxe_softc *sc, bus_size_t size,
13270     struct bxe_dma *dma, int mapflags, const char *msg)
13271 {
13272         int rc;
13273
13274         DBENTER(BXE_VERBOSE_LOAD | BXE_VERBOSE_RESET);
13275
13276         DBRUNIF(dma->size > 0,
13277             BXE_PRINTF("%s(): Called for %s with size > 0 (%05d)!\n",
13278             __FUNCTION__, msg, (int) dma->size));
13279
13280         rc = bus_dma_tag_create(
13281             sc->parent_tag,             /* parent */
13282             BCM_PAGE_SIZE,              /* alignment for segs */
13283             BXE_DMA_BOUNDARY,           /* cannot cross */
13284             BUS_SPACE_MAXADDR,          /* restricted low */
13285             BUS_SPACE_MAXADDR,          /* restricted hi */
13286             NULL, NULL,                 /* filter f(), arg */
13287             size,                       /* max size for this tag */
13288             1,                          /* # of discontinuities */
13289             size,                       /* max seg size */
13290             BUS_DMA_ALLOCNOW,           /* flags */
13291             NULL, NULL,                 /* lock f(), arg */
13292             &dma->tag);
13293
13294         if (rc != 0) {
13295                 BXE_PRINTF("%s(%d): bus_dma_tag_create() "
13296                     "failed (rc = %d) for %s!\n",
13297                     __FILE__, __LINE__, rc, msg);
13298                 goto bxe_dma_malloc_fail_create;
13299         }
13300
13301         rc = bus_dmamem_alloc(dma->tag, (void **)&dma->vaddr,
13302             BUS_DMA_NOWAIT, &dma->map);
13303         if (rc != 0) {
13304                 BXE_PRINTF("%s(%d): bus_dmamem_alloc() "
13305                     "failed (rc = %d) for %s!\n",
13306                     __FILE__, __LINE__, rc, msg);
13307                 goto bxe_dma_malloc_fail_alloc;
13308         }
13309
13310         rc = bus_dmamap_load(dma->tag, dma->map, dma->vaddr, size,
13311             bxe_dma_map_addr, &dma->paddr, mapflags | BUS_DMA_NOWAIT);
13312         if (rc != 0) {
13313                 BXE_PRINTF("%s(%d): bus_dmamap_load() "
13314                     "failed (rc = %d) for %s!\n",
13315                     __FILE__, __LINE__, rc, msg);
13316                 goto bxe_dma_malloc_fail_load;
13317         }
13318
13319         dma->size = size;
13320
13321         DBPRINT(sc, BXE_VERBOSE, "%s(): size=%06d, vaddr=0x%p, "
13322             "paddr=0x%jX - %s\n", __FUNCTION__, (int) dma->size,
13323             dma->vaddr, (uintmax_t) dma->paddr, msg);
13324
13325         goto bxe_dma_malloc_exit;
13326
13327 bxe_dma_malloc_fail_load:
13328         bus_dmamem_free(dma->tag, dma->vaddr, dma->map);
13329
13330 bxe_dma_malloc_fail_alloc:
13331         bus_dma_tag_destroy(dma->tag);
13332         dma->vaddr = NULL;
13333
13334 bxe_dma_malloc_fail_create:
13335         dma->map = NULL;
13336         dma->tag = NULL;
13337         dma->size = 0;
13338
13339 bxe_dma_malloc_exit:
13340         DBEXIT(BXE_VERBOSE_LOAD | BXE_VERBOSE_RESET);
13341         return (rc);
13342 }
13343
13344 /*
13345  * Release a block of DMA memory associated tag/map.
13346  *
13347  * Returns:
13348  *   None
13349  */
13350 static void
13351 bxe_dma_free(struct bxe_softc *sc, struct bxe_dma *dma)
13352 {
13353         DBENTER(BXE_VERBOSE_LOAD | BXE_VERBOSE_UNLOAD);
13354
13355         if (dma->size > 0) {
13356                 bus_dmamap_sync(dma->tag, dma->map,
13357                     BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
13358                 bus_dmamap_unload(dma->tag, dma->map);
13359                 bus_dmamem_free(dma->tag, dma->vaddr, dma->map);
13360                 bus_dma_tag_destroy(dma->tag);
13361                 dma->size = 0;
13362         }
13363
13364         DBEXIT(BXE_VERBOSE_LOAD | BXE_VERBOSE_UNLOAD);
13365 }
13366
13367 /*
13368  * Free any DMA memory owned by the driver.
13369  *
13370  * Scans through each data structre that requires DMA memory and frees
13371  * the memory if allocated.
13372  *
13373  * Returns:
13374  *   Nothing.
13375  */
13376 static void
13377 bxe_host_structures_free(struct bxe_softc *sc)
13378 {
13379         struct bxe_fastpath *fp;
13380         int i, j, max_agg_queues;
13381
13382         DBENTER(BXE_VERBOSE_LOAD | BXE_VERBOSE_RESET | BXE_VERBOSE_UNLOAD);
13383         max_agg_queues = CHIP_IS_E1H(sc) ?
13384             ETH_MAX_AGGREGATION_QUEUES_E1H :
13385             ETH_MAX_AGGREGATION_QUEUES_E1;
13386
13387         if (sc->parent_tag == NULL)
13388                 goto bxe_host_structures_free_exit;
13389
13390         for (i = 0; i < sc->num_queues; i++) {
13391                 fp = &sc->fp[i];
13392
13393                 /* Trust no one! */
13394                 if (fp == NULL)
13395                         break;
13396
13397                 /* Status block. */
13398                 bxe_dma_free(sc, &fp->sb_dma);
13399
13400                 /* TX chain. */
13401                 bxe_dma_free(sc, &fp->tx_dma);
13402                 fp->tx_chain = NULL;
13403
13404                 /* RX chain */
13405                 bxe_dma_free(sc, &fp->rx_dma);
13406                 fp->rx_chain = NULL;
13407
13408                 /* RCQ chain */
13409                 bxe_dma_free(sc, &fp->rcq_dma);
13410                 fp->rcq_chain = NULL;
13411
13412                 /* SG chain */
13413                 bxe_dma_free(sc, &fp->sg_dma);
13414                 fp->sg_chain = NULL;
13415
13416                 /* Unload and destroy the TX mbuf maps. */
13417                 if (fp->tx_mbuf_tag != NULL) {
13418                         for (j = 0; j < TOTAL_TX_BD; j++) {
13419                                 if (fp->tx_mbuf_map[j] != NULL) {
13420                                         bus_dmamap_unload(
13421                                             fp->tx_mbuf_tag,
13422                                             fp->tx_mbuf_map[j]);
13423                                         bus_dmamap_destroy(
13424                                             fp->tx_mbuf_tag,
13425                                             fp->tx_mbuf_map[j]);
13426                                 }
13427                         }
13428
13429                         bus_dma_tag_destroy(fp->tx_mbuf_tag);
13430                 }
13431
13432                 /* Unload and destroy the TPA pool mbuf maps. */
13433                 if (fp->rx_mbuf_tag != NULL) {
13434                         if (fp->tpa_mbuf_spare_map != NULL) {
13435                                 bus_dmamap_unload(
13436                                     fp->rx_mbuf_tag,
13437                                     fp->tpa_mbuf_spare_map);
13438                                 bus_dmamap_destroy(
13439                                     fp->rx_mbuf_tag,
13440                                     fp->tpa_mbuf_spare_map);
13441                         }
13442
13443                         for (j = 0; j < max_agg_queues; j++) {
13444                                 if (fp->tpa_mbuf_map[j] != NULL) {
13445                                         bus_dmamap_unload(
13446                                             fp->rx_mbuf_tag,
13447                                             fp->tpa_mbuf_map[j]);
13448                                         bus_dmamap_destroy(
13449                                             fp->rx_mbuf_tag,
13450                                             fp->tpa_mbuf_map[j]);
13451                                 }
13452                         }
13453                 }
13454
13455                 /* Unload and destroy the SGE Buf maps. */
13456                 if (fp->rx_sge_buf_tag != NULL) {
13457                         if (fp->rx_sge_spare_map != NULL) {
13458                                 bus_dmamap_unload(
13459                                     fp->rx_sge_buf_tag,
13460                                     fp->rx_sge_spare_map);
13461                                 bus_dmamap_destroy(
13462                                     fp->rx_sge_buf_tag,
13463                                     fp->rx_sge_spare_map);
13464                         }
13465
13466                         for (j = 0; j < TOTAL_RX_SGE; j++) {
13467                                 if (fp->rx_sge_buf_map[j] != NULL) {
13468                                         bus_dmamap_unload(
13469                                             fp->rx_sge_buf_tag,
13470                                             fp->rx_sge_buf_map[j]);
13471                                         bus_dmamap_destroy(
13472                                             fp->rx_sge_buf_tag,
13473                                             fp->rx_sge_buf_map[j]);
13474                                 }
13475                         }
13476
13477                         bus_dma_tag_destroy(fp->rx_sge_buf_tag);
13478                 }
13479
13480                 /* Unload and destroy the RX mbuf maps. */
13481                 if (fp->rx_mbuf_tag != NULL) {
13482                         if (fp->rx_mbuf_spare_map != NULL) {
13483                                 bus_dmamap_unload(fp->rx_mbuf_tag,
13484                                     fp->rx_mbuf_spare_map);
13485                                 bus_dmamap_destroy(fp->rx_mbuf_tag,
13486                                     fp->rx_mbuf_spare_map);
13487                         }
13488
13489                         for (j = 0; j < TOTAL_RX_BD; j++) {
13490                                 if (fp->rx_mbuf_map[j] != NULL) {
13491                                         bus_dmamap_unload(
13492                                             fp->rx_mbuf_tag,
13493                                             fp->rx_mbuf_map[j]);
13494                                         bus_dmamap_destroy(
13495                                             fp->rx_mbuf_tag,
13496                                             fp->rx_mbuf_map[j]);
13497                                 }
13498                         }
13499
13500                         bus_dma_tag_destroy(fp->rx_mbuf_tag);
13501                 }
13502         }
13503
13504         /* Destroy the default status block */
13505         bxe_dma_free(sc, &sc->def_sb_dma);
13506         sc->def_sb = NULL;
13507
13508         /* Destroy the statistics block */
13509         bxe_dma_free(sc, &sc->stats_dma);
13510         sc->stats = NULL;
13511
13512         /* Destroy the slowpath block. */
13513         bxe_dma_free(sc, &sc->slowpath_dma);
13514         sc->slowpath = NULL;
13515
13516         /* Destroy the slowpath queue. */
13517         bxe_dma_free(sc, &sc->spq_dma);
13518         sc->spq = NULL;
13519
13520         /* Destroy the slowpath queue. */
13521         bxe_dma_free(sc, &sc->gz_dma);
13522         sc->gz = NULL;
13523         free(sc->strm, M_DEVBUF);
13524         sc->strm = NULL;
13525
13526 bxe_host_structures_free_exit:
13527         DBEXIT(BXE_VERBOSE_LOAD | BXE_VERBOSE_RESET | BXE_VERBOSE_UNLOAD);
13528 }
13529
13530 /*
13531  * Get DMA memory from the OS.
13532  *
13533  * Validates that the OS has provided DMA buffers in response to a
13534  * bus_dmamap_load call and saves the physical address of those buffers.
13535  * When the callback is used the OS will return 0 for the mapping function
13536  * (bus_dmamap_load) so we use the value of map_arg->maxsegs to pass any
13537  * failures back to the caller.
13538  *
13539  * Returns:
13540  *   Nothing.
13541  */
13542 static void
13543 bxe_dma_map_addr(void *arg, bus_dma_segment_t *segs, int nseg, int error)
13544 {
13545         bus_addr_t *busaddr;
13546
13547         busaddr = arg;
13548         /* Check for an error and signal the caller that an error occurred. */
13549         if (error) {
13550                 printf(
13551                     "bxe %s(%d): DMA mapping error (error = %d, nseg = %d)!\n",
13552                     __FILE__, __LINE__, error, nseg);
13553                 *busaddr = 0;
13554                 return;
13555         }
13556
13557         *busaddr = segs->ds_addr;
13558 }
13559
13560 /*
13561  * Allocate any non-paged DMA memory needed by the driver.
13562  *
13563  * Returns:
13564  *   0 = Success, !0 = Failure.
13565  */
13566 static int
13567 bxe_host_structures_alloc(device_t dev)
13568 {
13569         struct bxe_softc *sc;
13570         struct bxe_fastpath *fp;
13571         int rc;
13572         bus_addr_t busaddr;
13573         bus_size_t max_size, max_seg_size;
13574         int i, j, max_segments;
13575
13576         sc = device_get_softc(dev);
13577         DBENTER(BXE_VERBOSE_RESET);
13578         rc = 0;
13579         int max_agg_queues = CHIP_IS_E1H(sc) ?
13580             ETH_MAX_AGGREGATION_QUEUES_E1H :
13581             ETH_MAX_AGGREGATION_QUEUES_E1;
13582
13583         /*
13584          * Allocate the parent bus DMA tag appropriate for PCI.
13585          */
13586         rc = bus_dma_tag_create(NULL,   /* parent tag */
13587             1,                          /* alignment for segs */
13588             BXE_DMA_BOUNDARY,           /* cannot cross */
13589             BUS_SPACE_MAXADDR,          /* restricted low */
13590             BUS_SPACE_MAXADDR,          /* restricted hi */
13591             NULL,                       /* filter f() */
13592             NULL,                       /* filter f() arg */
13593             MAXBSIZE,                   /* max map for this tag */
13594             BUS_SPACE_UNRESTRICTED,     /* # of discontinuities */
13595             BUS_SPACE_MAXSIZE_32BIT,    /* max seg size */
13596             0,                          /* flags */
13597             NULL,                       /* lock f() */
13598             NULL,                       /* lock f() arg */
13599             &sc->parent_tag);           /* dma tag */
13600         if (rc != 0) {
13601                 BXE_PRINTF("%s(%d): Could not allocate parent DMA tag!\n",
13602                     __FILE__, __LINE__);
13603                 rc = ENOMEM;
13604                 goto bxe_host_structures_alloc_exit;
13605         }
13606
13607         /* Allocate DMA memory for each fastpath structure. */
13608         for (i = 0; i < sc->num_queues; i++) {
13609                 fp = &sc->fp[i];
13610
13611                 /*
13612                  * Allocate status block*
13613                 */
13614                 rc = bxe_dma_malloc(sc, BXE_STATUS_BLK_SZ,
13615                     &fp->sb_dma, BUS_DMA_NOWAIT, "fp status block");
13616                 /* ToDo: Only using 32 bytes out of 4KB allocation! */
13617                 if (rc != 0)
13618                         goto bxe_host_structures_alloc_exit;
13619                 fp->status_block =
13620                     (struct host_status_block *) fp->sb_dma.vaddr;
13621
13622                 /*
13623                  * Allocate TX chain.
13624                  */
13625                 rc = bxe_dma_malloc(sc, BXE_TX_CHAIN_PAGE_SZ *
13626                     NUM_TX_PAGES, &fp->tx_dma, BUS_DMA_NOWAIT,
13627                     "tx chain pages");
13628                 if (rc != 0)
13629                         goto bxe_host_structures_alloc_exit;
13630                 fp->tx_chain = (union eth_tx_bd_types *) fp->tx_dma.vaddr;
13631
13632                 /* Link the TX chain pages. */
13633                 for (j = 1; j <= NUM_TX_PAGES; j++) {
13634                         struct eth_tx_next_bd *tx_n_bd =
13635                             &fp->tx_chain[TOTAL_TX_BD_PER_PAGE * j - 1].next_bd;
13636
13637                         busaddr = fp->tx_dma.paddr +
13638                             BCM_PAGE_SIZE * (j % NUM_TX_PAGES);
13639                         tx_n_bd->addr_hi = htole32(U64_HI(busaddr));
13640                         tx_n_bd->addr_lo = htole32(U64_LO(busaddr));
13641                 }
13642
13643                 /*
13644                  * Allocate RX chain.
13645                  */
13646                 rc = bxe_dma_malloc(sc, BXE_RX_CHAIN_PAGE_SZ *
13647                     NUM_RX_PAGES, &fp->rx_dma, BUS_DMA_NOWAIT,
13648                     "rx chain pages");
13649                 if (rc != 0)
13650                         goto bxe_host_structures_alloc_exit;
13651                 fp->rx_chain = (struct eth_rx_bd *) fp->rx_dma.vaddr;
13652
13653                 /* Link the RX chain pages. */
13654                 for (j = 1; j <= NUM_RX_PAGES; j++) {
13655                         struct eth_rx_bd *rx_bd =
13656                             &fp->rx_chain[TOTAL_RX_BD_PER_PAGE * j - 2];
13657
13658                         busaddr = fp->rx_dma.paddr +
13659                             BCM_PAGE_SIZE * (j % NUM_RX_PAGES);
13660                         rx_bd->addr_hi = htole32(U64_HI(busaddr));
13661                         rx_bd->addr_lo = htole32(U64_LO(busaddr));
13662                 }
13663
13664                 /*
13665                  * Allocate CQ chain.
13666                  */
13667                 rc = bxe_dma_malloc(sc, BXE_RX_CHAIN_PAGE_SZ *
13668                     NUM_RCQ_PAGES, &fp->rcq_dma, BUS_DMA_NOWAIT,
13669                     "rcq chain pages");
13670                 if (rc != 0)
13671                         goto bxe_host_structures_alloc_exit;
13672                 fp->rcq_chain = (union eth_rx_cqe *) fp->rcq_dma.vaddr;
13673
13674                 /* Link the CQ chain pages. */
13675                 for (j = 1; j <= NUM_RCQ_PAGES; j++) {
13676                         struct eth_rx_cqe_next_page *nextpg =
13677                             (struct eth_rx_cqe_next_page *)
13678                             &fp->rcq_chain[TOTAL_RCQ_ENTRIES_PER_PAGE * j - 1];
13679
13680                         busaddr = fp->rcq_dma.paddr +
13681                             BCM_PAGE_SIZE * (j % NUM_RCQ_PAGES);
13682                         nextpg->addr_hi = htole32(U64_HI(busaddr));
13683                         nextpg->addr_lo = htole32(U64_LO(busaddr));
13684                 }
13685
13686                 /*
13687                  * Allocate SG chain.
13688                  */
13689                 rc = bxe_dma_malloc(sc, BXE_RX_CHAIN_PAGE_SZ *
13690                     NUM_RX_SGE_PAGES, &fp->sg_dma, BUS_DMA_NOWAIT,
13691                     "sg chain pages");
13692                 if (rc != 0)
13693                         goto bxe_host_structures_alloc_exit;
13694                 fp->sg_chain = (struct eth_rx_sge *) fp->sg_dma.vaddr;
13695
13696                 /* Link the SG chain pages. */
13697                 for (j = 1; j <= NUM_RX_SGE_PAGES; j++) {
13698                         struct eth_rx_sge *nextpg =
13699                             &fp->sg_chain[TOTAL_RX_SGE_PER_PAGE * j - 2];
13700
13701                         busaddr = fp->sg_dma.paddr +
13702                             BCM_PAGE_SIZE * (j % NUM_RX_SGE_PAGES);
13703                         nextpg->addr_hi = htole32(U64_HI(busaddr));
13704                         nextpg->addr_lo = htole32(U64_LO(busaddr));
13705                 }
13706
13707                 /*
13708                  * Check required size before mapping to conserve resources.
13709                  */
13710                 if (sc->tso_enable == TRUE) {
13711                         max_size     = BXE_TSO_MAX_SIZE;
13712                         max_segments = BXE_TSO_MAX_SEGMENTS;
13713                         max_seg_size = BXE_TSO_MAX_SEG_SIZE;
13714                 } else {
13715                         max_size     = MCLBYTES * BXE_MAX_SEGMENTS;
13716                         max_segments = BXE_MAX_SEGMENTS;
13717                         max_seg_size = MCLBYTES;
13718                 }
13719
13720                 /* Create a DMA tag for TX mbufs. */
13721                 if (bus_dma_tag_create(sc->parent_tag,
13722                     1,                  /* alignment for segs */
13723                     BXE_DMA_BOUNDARY,   /* cannot cross */
13724                     BUS_SPACE_MAXADDR,  /* restricted low */
13725                     BUS_SPACE_MAXADDR,  /* restricted hi */
13726                     NULL,               /* filter f() */
13727                     NULL,               /* filter f() arg */
13728                     max_size,           /* max map for this tag */
13729                     max_segments,       /* # of discontinuities */
13730                     max_seg_size,       /* max seg size */
13731                     0,                  /* flags */
13732                     NULL,               /* lock f() */
13733                     NULL,               /* lock f() arg */
13734                     &fp->tx_mbuf_tag)) {
13735                         BXE_PRINTF(
13736                             "%s(%d): Could not allocate fp[%d] "
13737                             "TX mbuf DMA tag!\n",
13738                             __FILE__, __LINE__, i);
13739                         rc = ENOMEM;
13740                         goto bxe_host_structures_alloc_exit;
13741                 }
13742
13743                 /* Create DMA maps for each the TX mbuf cluster(ext buf). */
13744                 for (j = 0; j < TOTAL_TX_BD; j++) {
13745                         if (bus_dmamap_create(fp->tx_mbuf_tag,
13746                             BUS_DMA_NOWAIT,
13747                             &fp->tx_mbuf_map[j])) {
13748                                 BXE_PRINTF(
13749                                     "%s(%d): Unable to create fp[%02d]."
13750                                     "tx_mbuf_map[%d] DMA map!\n",
13751                                     __FILE__, __LINE__, i, j);
13752                                 rc = ENOMEM;
13753                                 goto bxe_host_structures_alloc_exit;
13754                         }
13755                 }
13756
13757                 /*
13758                  * Create a DMA tag for RX mbufs.
13759                  */
13760                 if (bus_dma_tag_create(sc->parent_tag,
13761                     1,                  /* alignment for segs */
13762                     BXE_DMA_BOUNDARY,   /* cannot cross */
13763                     BUS_SPACE_MAXADDR,  /* restricted low */
13764                     BUS_SPACE_MAXADDR,  /* restricted hi */
13765                     NULL,               /* filter f() */
13766                     NULL,               /* filter f() arg */
13767                     MJUM9BYTES,         /* max map for this tag */
13768                     1,                  /* # of discontinuities */
13769                     MJUM9BYTES,         /* max seg size */
13770                     0,                  /* flags */
13771                     NULL,               /* lock f() */
13772                     NULL,               /* lock f() arg */
13773                     &fp->rx_mbuf_tag)) {
13774                         BXE_PRINTF(
13775                             "%s(%d): Could not allocate fp[%02d] "
13776                             "RX mbuf DMA tag!\n",
13777                             __FILE__, __LINE__, i);
13778                         rc = ENOMEM;
13779                         goto bxe_host_structures_alloc_exit;
13780                 }
13781
13782                 /* Create DMA maps for the RX mbuf clusters. */
13783                 if (bus_dmamap_create(fp->rx_mbuf_tag,
13784                     BUS_DMA_NOWAIT, &fp->rx_mbuf_spare_map)) {
13785                         BXE_PRINTF(
13786                             "%s(%d): Unable to create fp[%02d]."
13787                             "rx_mbuf_spare_map DMA map!\n",
13788                             __FILE__, __LINE__, i);
13789                         rc = ENOMEM;
13790                         goto bxe_host_structures_alloc_exit;
13791                 }
13792
13793                 for (j = 0; j < TOTAL_RX_BD; j++) {
13794                         if (bus_dmamap_create(fp->rx_mbuf_tag,
13795                             BUS_DMA_NOWAIT, &fp->rx_mbuf_map[j])) {
13796                                 BXE_PRINTF(
13797                                     "%s(%d): Unable to create fp[%02d]."
13798                                     "rx_mbuf_map[%d] DMA map!\n",
13799                                     __FILE__, __LINE__, i, j);
13800                                 rc = ENOMEM;
13801                                 goto bxe_host_structures_alloc_exit;
13802                         }
13803                 }
13804
13805                 /*
13806                  * Create a DMA tag for RX SGE bufs.
13807                  */
13808                 if (bus_dma_tag_create(sc->parent_tag, 1,
13809                     BXE_DMA_BOUNDARY, BUS_SPACE_MAXADDR,
13810                     BUS_SPACE_MAXADDR, NULL, NULL, PAGE_SIZE, 1,
13811                     PAGE_SIZE, 0, NULL, NULL, &fp->rx_sge_buf_tag)) {
13812                         BXE_PRINTF(
13813                             "%s(%d): Could not allocate fp[%02d] "
13814                             "RX SGE mbuf DMA tag!\n",
13815                             __FILE__, __LINE__, i);
13816                         rc = ENOMEM;
13817                         goto bxe_host_structures_alloc_exit;
13818                 }
13819
13820                 /* Create DMA maps for the SGE mbuf clusters. */
13821                 if (bus_dmamap_create(fp->rx_sge_buf_tag,
13822                     BUS_DMA_NOWAIT, &fp->rx_sge_spare_map)) {
13823                         BXE_PRINTF(
13824                            "%s(%d): Unable to create fp[%02d]."
13825                            "rx_sge_spare_map DMA map!\n",
13826                             __FILE__, __LINE__, i);
13827                         rc = ENOMEM;
13828                         goto bxe_host_structures_alloc_exit;
13829                 }
13830
13831                 for (j = 0; j < TOTAL_RX_SGE; j++) {
13832                         if (bus_dmamap_create(fp->rx_sge_buf_tag,
13833                             BUS_DMA_NOWAIT, &fp->rx_sge_buf_map[j])) {
13834                                 BXE_PRINTF(
13835                                    "%s(%d): Unable to create fp[%02d]."
13836                                    "rx_sge_buf_map[%d] DMA map!\n",
13837                                     __FILE__, __LINE__, i, j);
13838                                 rc = ENOMEM;
13839                                 goto bxe_host_structures_alloc_exit;
13840                         }
13841                 }
13842
13843                 /* Create DMA maps for the TPA pool mbufs. */
13844                 if (bus_dmamap_create(fp->rx_mbuf_tag,
13845                     BUS_DMA_NOWAIT, &fp->tpa_mbuf_spare_map)) {
13846                         BXE_PRINTF(
13847                             "%s(%d): Unable to create fp[%02d]."
13848                             "tpa_mbuf_spare_map DMA map!\n",
13849                             __FILE__, __LINE__, i);
13850                         rc = ENOMEM;
13851                         goto bxe_host_structures_alloc_exit;
13852                 }
13853
13854                 for (j = 0; j < max_agg_queues; j++) {
13855                         if (bus_dmamap_create(fp->rx_mbuf_tag,
13856                             BUS_DMA_NOWAIT, &fp->tpa_mbuf_map[j])) {
13857                                 BXE_PRINTF(
13858                                     "%s(%d): Unable to create fp[%02d]."
13859                                     "tpa_mbuf_map[%d] DMA map!\n",
13860                                     __FILE__, __LINE__, i, j);
13861                                 rc = ENOMEM;
13862                                 goto bxe_host_structures_alloc_exit;
13863                         }
13864                 }
13865
13866                 bxe_init_sge_ring_bit_mask(fp);
13867         }
13868
13869         /*
13870          * Allocate default status block.
13871          */
13872         rc = bxe_dma_malloc(sc, BXE_DEF_STATUS_BLK_SZ, &sc->def_sb_dma,
13873             BUS_DMA_NOWAIT, "default status block");
13874         if (rc != 0)
13875                 goto bxe_host_structures_alloc_exit;
13876         sc->def_sb = (struct host_def_status_block *) sc->def_sb_dma.vaddr;
13877
13878         /*
13879          * Allocate statistics block.
13880          */
13881         rc = bxe_dma_malloc(sc, BXE_STATS_BLK_SZ, &sc->stats_dma,
13882             BUS_DMA_NOWAIT, "statistics block");
13883         if (rc != 0)
13884                 goto bxe_host_structures_alloc_exit;
13885         sc->stats = (struct statistics_block *) sc->stats_dma.vaddr;
13886
13887         /*
13888          * Allocate slowpath block.
13889          */
13890         rc = bxe_dma_malloc(sc, BXE_SLOWPATH_SZ, &sc->slowpath_dma,
13891             BUS_DMA_NOWAIT, "slowpath block");
13892         if (rc != 0)
13893                 goto bxe_host_structures_alloc_exit;
13894         sc->slowpath = (struct bxe_slowpath *) sc->slowpath_dma.vaddr;
13895
13896         /*
13897          * Allocate slowpath queue.
13898          */
13899         rc = bxe_dma_malloc(sc, BXE_SPQ_SZ, &sc->spq_dma,
13900             BUS_DMA_NOWAIT, "slowpath queue");
13901         if (rc != 0)
13902                 goto bxe_host_structures_alloc_exit;
13903         sc->spq = (struct eth_spe *) sc->spq_dma.vaddr;
13904
13905         /*
13906          * Allocate firmware decompression buffer.
13907          */
13908         rc = bxe_dma_malloc(sc, BXE_FW_BUF_SIZE, &sc->gz_dma,
13909             BUS_DMA_NOWAIT, "gunzip buffer");
13910         if (rc != 0)
13911                 goto bxe_host_structures_alloc_exit;
13912         sc->gz = sc->gz_dma.vaddr;
13913         if (sc->strm == NULL) {
13914                 goto bxe_host_structures_alloc_exit;
13915         }
13916
13917         sc->strm = malloc(sizeof(*sc->strm), M_DEVBUF, M_NOWAIT);
13918
13919 bxe_host_structures_alloc_exit:
13920         DBEXIT(BXE_VERBOSE_RESET);
13921         return (rc);
13922 }
13923
13924 /*
13925  * Program the MAC address for 57710 controllers.
13926  *
13927  * Returns:
13928  *   Nothing.
13929  */
13930 static void
13931 bxe_set_mac_addr_e1(struct bxe_softc *sc, int set)
13932 {
13933         struct mac_configuration_cmd *config;
13934         struct mac_configuration_entry *config_table;
13935         uint8_t *eaddr;
13936         int port;
13937
13938         DBENTER(BXE_VERBOSE_LOAD | BXE_VERBOSE_RESET | BXE_VERBOSE_UNLOAD);
13939
13940         config = BXE_SP(sc, mac_config);
13941         port = BP_PORT(sc);
13942         /*
13943          * CAM allocation:
13944          * Port 0 Unicast Addresses: 32 Perfect Match Filters (31-0)
13945          * Port 1 Unicast Addresses: 32 Perfect Match Filters (63-32)
13946          * Port 0 Multicast Addresses: 128 Hashes (127-64)
13947          * Port 1 Multicast Addresses: 128 Hashes (191-128)
13948          */
13949
13950         config->hdr.length = 2;
13951         config->hdr.offset = port ? 32 : 0;
13952         config->hdr.client_id = BP_CL_ID(sc);
13953         config->hdr.reserved1 = 0;
13954
13955         /* Program the primary MAC address. */
13956         config_table = &config->config_table[0];
13957         eaddr = sc->link_params.mac_addr;
13958         config_table->cam_entry.msb_mac_addr = eaddr[0] << 8 | eaddr[1];
13959         config_table->cam_entry.middle_mac_addr = eaddr[2] << 8 | eaddr[3];
13960         config_table->cam_entry.lsb_mac_addr = eaddr[4] << 8 | eaddr[5];
13961         config_table->cam_entry.flags = htole16(port);
13962
13963         if (set)
13964                 config_table->target_table_entry.flags = 0;
13965         else
13966                 CAM_INVALIDATE(config_table);
13967
13968         config_table->target_table_entry.vlan_id = 0;
13969
13970         DBPRINT(sc, BXE_VERBOSE, "%s(): %s MAC (%04x:%04x:%04x)\n",
13971            __FUNCTION__, (set ? "Setting" : "Clearing"),
13972            config_table->cam_entry.msb_mac_addr,
13973            config_table->cam_entry.middle_mac_addr,
13974            config_table->cam_entry.lsb_mac_addr);
13975
13976         /* Program the broadcast MAC address. */
13977         config_table = &config->config_table[1];
13978         config_table->cam_entry.msb_mac_addr = 0xffff;
13979         config_table->cam_entry.middle_mac_addr = 0xffff;
13980         config_table->cam_entry.lsb_mac_addr = 0xffff;
13981         config_table->cam_entry.flags = htole16(port);
13982
13983         if (set)
13984                 config_table->target_table_entry.flags =
13985                     TSTORM_CAM_TARGET_TABLE_ENTRY_BROADCAST;
13986         else
13987                 CAM_INVALIDATE(config_table);
13988
13989         config_table->target_table_entry.vlan_id = 0;
13990
13991         /* Post the command to slow path queue. */
13992         bxe_sp_post(sc, RAMROD_CMD_ID_ETH_SET_MAC, 0,
13993             U64_HI(BXE_SP_MAPPING(sc, mac_config)),
13994             U64_LO(BXE_SP_MAPPING(sc, mac_config)), 0);
13995
13996         DBEXIT(BXE_VERBOSE_LOAD | BXE_VERBOSE_RESET | BXE_VERBOSE_UNLOAD);
13997 }
13998
13999 /*
14000  * Program the MAC address for 57711/57711E controllers.
14001  *
14002  * Returns:
14003  *   Nothing.
14004  */
14005 static void
14006 bxe_set_mac_addr_e1h(struct bxe_softc *sc, int set)
14007 {
14008         struct mac_configuration_cmd_e1h *config;
14009         struct mac_configuration_entry_e1h *config_table;
14010         uint8_t *eaddr;
14011         int func, port;
14012
14013         DBENTER(BXE_VERBOSE_LOAD | BXE_VERBOSE_RESET | BXE_VERBOSE_UNLOAD);
14014
14015         config = (struct mac_configuration_cmd_e1h *)BXE_SP(sc, mac_config);
14016         port = BP_PORT(sc);
14017         func = BP_FUNC(sc);
14018
14019         if (set && (sc->state != BXE_STATE_OPEN)) {
14020                 DBPRINT(sc, BXE_VERBOSE,
14021                     "%s(): Can't set E1H MAC in state 0x%08X!\n", __FUNCTION__,
14022                     sc->state);
14023                 goto bxe_set_mac_addr_e1h_exit;
14024         }
14025
14026         /*
14027          * CAM allocation:
14028          * Function 0-7 Unicast Addresses: 8 Perfect Match Filters
14029          * Multicast Addresses: 20 + FUNC * 20, 20 each (???)
14030          */
14031         config->hdr.length = 1;
14032         config->hdr.offset = func;
14033         config->hdr.client_id = 0xff;
14034         config->hdr.reserved1 = 0;
14035
14036         /* Program the primary MAC address. */
14037         config_table = &config->config_table[0];
14038         eaddr = sc->link_params.mac_addr;
14039         config_table->msb_mac_addr = eaddr[0] << 8 | eaddr[1];
14040         config_table->middle_mac_addr = eaddr[2] << 8 | eaddr[3];
14041         config_table->lsb_mac_addr = eaddr[4] << 8 | eaddr[5];
14042         config_table->clients_bit_vector = htole32(1 << sc->fp->cl_id);
14043
14044         config_table->vlan_id = 0;
14045         config_table->e1hov_id = htole16(sc->e1hov);
14046
14047         if (set)
14048                 config_table->flags = port;
14049         else
14050                 config_table->flags =
14051                         MAC_CONFIGURATION_ENTRY_E1H_ACTION_TYPE;
14052
14053         DBPRINT(sc, BXE_VERBOSE,
14054             "%s(): %s MAC (%04x:%04x:%04x), E1HOV = %d, CLID = %d\n",
14055             __FUNCTION__, (set ? "Setting" : "Clearing"),
14056             config_table->msb_mac_addr, config_table->middle_mac_addr,
14057             config_table->lsb_mac_addr, sc->e1hov, BP_L_ID(sc));
14058
14059         bxe_sp_post(sc, RAMROD_CMD_ID_ETH_SET_MAC, 0,
14060             U64_HI(BXE_SP_MAPPING(sc, mac_config)),
14061             U64_LO(BXE_SP_MAPPING(sc, mac_config)), 0);
14062
14063 bxe_set_mac_addr_e1h_exit:
14064         DBEXIT(BXE_VERBOSE_LOAD | BXE_VERBOSE_RESET | BXE_VERBOSE_UNLOAD);
14065 }
14066
14067 /*
14068  * Programs the various packet receive modes (broadcast and multicast).
14069  *
14070  * Returns:
14071  *   Nothing.
14072  */
14073
14074 static void
14075 bxe_set_rx_mode(struct bxe_softc *sc)
14076 {
14077         struct ifnet *ifp;
14078         struct ifmultiaddr *ifma;
14079         struct mac_configuration_cmd *config;
14080         struct mac_configuration_entry *config_table;
14081         uint32_t mc_filter[MC_HASH_SIZE];
14082         uint8_t *maddr;
14083         uint32_t crc, bit, regidx, rx_mode;
14084         int i, old, offset, port;
14085
14086         BXE_CORE_LOCK_ASSERT(sc);
14087
14088         rx_mode = BXE_RX_MODE_NORMAL;
14089         port = BP_PORT(sc);
14090
14091         DBENTER(BXE_VERBOSE_LOAD | BXE_VERBOSE_RESET);
14092
14093         if (sc->state != BXE_STATE_OPEN) {
14094                 DBPRINT(sc, BXE_WARN, "%s(): State (0x%08X) is not open!\n",
14095                     __FUNCTION__, sc->state);
14096                 goto bxe_set_rx_mode_exit;
14097         }
14098
14099         ifp = sc->bxe_ifp;
14100
14101         /*
14102          * Check for promiscuous, all multicast, or selected
14103          * multicast address filtering.
14104          */
14105         if (ifp->if_flags & IFF_PROMISC) {
14106                 /* Enable promiscuous mode. */
14107                 rx_mode = BXE_RX_MODE_PROMISC;
14108         } else if (ifp->if_flags & IFF_ALLMULTI ||
14109             ifp->if_amcount > BXE_MAX_MULTICAST) {
14110                 /* Enable all multicast addresses. */
14111                 rx_mode = BXE_RX_MODE_ALLMULTI;
14112         } else {
14113                 /* Enable selective multicast mode. */
14114                 if (CHIP_IS_E1(sc)) {
14115                         i = 0;
14116                         config = BXE_SP(sc, mcast_config);
14117
14118                         IF_ADDR_LOCK(ifp);
14119
14120                         TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
14121                                 if (ifma->ifma_addr->sa_family != AF_LINK)
14122                                         continue;
14123                                 maddr = (uint8_t *)LLADDR(
14124                                     (struct sockaddr_dl *)ifma->ifma_addr);
14125                                 config_table = &config->config_table[i];
14126                                 config_table->cam_entry.msb_mac_addr =
14127                                     maddr[0] << 8 | maddr[1];
14128                                 config_table->cam_entry.middle_mac_addr =
14129                                     maddr[2] << 8 | maddr[3];
14130                                 config_table->cam_entry.lsb_mac_addr =
14131                                     maddr[4] << 8 | maddr[5];
14132                                 config_table->cam_entry.flags = htole16(port);
14133                                 config_table->target_table_entry.flags = 0;
14134                                 config_table->target_table_entry.
14135                                     clients_bit_vector =
14136                                     htole32(1 << BP_L_ID(sc));
14137                                 config_table->target_table_entry.vlan_id = 0;
14138                                 i++;
14139                                 DBPRINT(sc, BXE_INFO,
14140                         "%s(): Setting MCAST[%d] (%04X:%04X:%04X)\n",
14141                                     __FUNCTION__, i,
14142                                     config_table->cam_entry.msb_mac_addr,
14143                                     config_table->cam_entry.middle_mac_addr,
14144                                     config_table->cam_entry.lsb_mac_addr);
14145                         }
14146
14147                         IF_ADDR_UNLOCK(ifp);
14148
14149                         old = config->hdr.length;
14150
14151                         /* Invalidate any extra MC entries in the CAM. */
14152                         if (old > i) {
14153                                 for (; i < old; i++) {
14154                                         config_table = &config->config_table[i];
14155                                         if (CAM_IS_INVALID(config_table))
14156                                                 break;
14157                                         /* Invalidate */
14158                                         CAM_INVALIDATE(config_table);
14159                                 }
14160                         }
14161
14162                         offset = BXE_MAX_MULTICAST * (1 + port);
14163                         config->hdr.length = i;
14164                         config->hdr.offset = offset;
14165                         config->hdr.client_id = sc->fp->cl_id;
14166                         config->hdr.reserved1 = 0;
14167                         wmb();
14168                         bxe_sp_post(sc, RAMROD_CMD_ID_ETH_SET_MAC, 0,
14169                             U64_HI(BXE_SP_MAPPING(sc, mcast_config)),
14170                             U64_LO(BXE_SP_MAPPING(sc, mcast_config)), 0);
14171                 } else { /* E1H */
14172                         /* Accept one or more multicasts */
14173                         memset(mc_filter, 0, 4 * MC_HASH_SIZE);
14174
14175                         IF_ADDR_LOCK(ifp);
14176
14177                         TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
14178                                 if (ifma->ifma_addr->sa_family != AF_LINK)
14179                                         continue;
14180                                 crc = ether_crc32_le(ifma->ifma_addr->sa_data,
14181                                     ETHER_ADDR_LEN);
14182                                 bit = (crc >> 24) & 0xff;
14183                                 regidx = bit >> 5;
14184                                 bit &= 0x1f;
14185                                 mc_filter[regidx] |= (1 << bit);
14186                         }
14187                         IF_ADDR_UNLOCK(ifp);
14188
14189                         for (i = 0; i < MC_HASH_SIZE; i++)
14190                                 REG_WR(sc, MC_HASH_OFFSET(sc, i), mc_filter[i]);
14191                 }
14192         }
14193
14194         DBPRINT(sc, BXE_VERBOSE, "%s(): Enabling new receive mode: 0x%08X\n",
14195             __FUNCTION__, rx_mode);
14196
14197         sc->rx_mode = rx_mode;
14198         bxe_set_storm_rx_mode(sc);
14199
14200 bxe_set_rx_mode_exit:
14201         DBEXIT(BXE_VERBOSE_LOAD | BXE_VERBOSE_RESET);
14202 }
14203
14204 /*
14205  * Function specific controller reset.
14206  *
14207  * Returns:
14208  *   Nothing.
14209  */
14210 static void
14211 bxe_reset_func(struct bxe_softc *sc)
14212 {
14213         int base, func, i, port;
14214
14215         DBENTER(BXE_VERBOSE_LOAD | BXE_VERBOSE_RESET | BXE_VERBOSE_UNLOAD);
14216
14217         port = BP_PORT(sc);
14218         func = BP_FUNC(sc);
14219
14220         /* Configure IGU. */
14221         REG_WR(sc, HC_REG_LEADING_EDGE_0 + port * 8, 0);
14222         REG_WR(sc, HC_REG_TRAILING_EDGE_0 + port * 8, 0);
14223         REG_WR(sc, HC_REG_CONFIG_0 + (port * 4), 0x1000);
14224
14225         /* Clear ILT. */
14226         base = FUNC_ILT_BASE(func);
14227         for (i = base; i < base + ILT_PER_FUNC; i++)
14228                 bxe_ilt_wr(sc, i, 0);
14229
14230         DBEXIT(BXE_VERBOSE_LOAD | BXE_VERBOSE_RESET | BXE_VERBOSE_UNLOAD);
14231 }
14232
14233 /*
14234  * Port specific controller reset.
14235  *
14236  * Returns:
14237  *   Nothing.
14238  */
14239 static void
14240 bxe_reset_port(struct bxe_softc *sc)
14241 {
14242         uint32_t val;
14243         int port;
14244
14245         DBENTER(BXE_VERBOSE_LOAD | BXE_VERBOSE_RESET | BXE_VERBOSE_UNLOAD);
14246
14247         port = BP_PORT(sc);
14248         REG_WR(sc, NIG_REG_MASK_INTERRUPT_PORT0 + port * 4, 0);
14249
14250         /* Do not receive packets to BRB. */
14251         REG_WR(sc, NIG_REG_LLH0_BRB1_DRV_MASK + port * 4, 0x0);
14252
14253         /* Do not direct receive packets that are not for MCP to the BRB. */
14254         REG_WR(sc, port ? NIG_REG_LLH1_BRB1_NOT_MCP :
14255             NIG_REG_LLH0_BRB1_NOT_MCP, 0x0);
14256
14257         /* Configure AEU. */
14258         REG_WR(sc, MISC_REG_AEU_MASK_ATTN_FUNC_0 + port * 4, 0);
14259
14260         DELAY(100000);
14261
14262         /* Check for BRB port occupancy. */
14263         val = REG_RD(sc, BRB1_REG_PORT_NUM_OCC_BLOCKS_0 + port * 4);
14264         if (val)
14265                 DBPRINT(sc, BXE_VERBOSE,
14266                     "%s(): BRB1 is not empty (%d blocks are occupied)!\n",
14267                     __FUNCTION__, val);
14268
14269         DBEXIT(BXE_VERBOSE_LOAD | BXE_VERBOSE_RESET | BXE_VERBOSE_UNLOAD);
14270 }
14271
14272 /*
14273  * Common controller reset.
14274  *
14275  * Returns:
14276  *   Nothing.
14277  */
14278 static void
14279 bxe_reset_common(struct bxe_softc *sc)
14280 {
14281
14282         DBENTER(BXE_VERBOSE_LOAD | BXE_VERBOSE_RESET | BXE_VERBOSE_UNLOAD);
14283
14284         REG_WR(sc, GRCBASE_MISC + MISC_REGISTERS_RESET_REG_1_CLEAR,
14285             0xd3ffff7f);
14286         REG_WR(sc, GRCBASE_MISC + MISC_REGISTERS_RESET_REG_2_CLEAR,
14287             0x1403);
14288
14289         DBEXIT(BXE_VERBOSE_LOAD | BXE_VERBOSE_RESET | BXE_VERBOSE_UNLOAD);
14290 }
14291
14292 /*
14293  * Reset the controller.
14294  *
14295  * Returns:
14296  *   Nothing.
14297  */
14298 static void
14299 bxe_reset_chip(struct bxe_softc *sc, uint32_t reset_code)
14300 {
14301
14302         DBENTER(BXE_VERBOSE_LOAD | BXE_VERBOSE_RESET | BXE_VERBOSE_UNLOAD);
14303
14304         switch (reset_code) {
14305         case FW_MSG_CODE_DRV_UNLOAD_COMMON:
14306                 bxe_reset_port(sc);
14307                 bxe_reset_func(sc);
14308                 bxe_reset_common(sc);
14309                 break;
14310         case FW_MSG_CODE_DRV_UNLOAD_PORT:
14311                 bxe_reset_port(sc);
14312                 bxe_reset_func(sc);
14313                 break;
14314         case FW_MSG_CODE_DRV_UNLOAD_FUNCTION:
14315                 bxe_reset_func(sc);
14316                 break;
14317         default:
14318                 BXE_PRINTF("%s(%d): Unknown reset code (0x%08X) from MCP!\n",
14319                     __FILE__, __LINE__, reset_code);
14320                 break;
14321         }
14322
14323         DBEXIT(BXE_VERBOSE_LOAD | BXE_VERBOSE_RESET | BXE_VERBOSE_UNLOAD);
14324 }
14325
14326 /*
14327  * Called by the OS to set media options (link, speed, etc.)
14328  * when the user specifies "ifconfig bxe media XXX" or
14329  * "ifconfig bxe mediaopt XXX".
14330  *
14331  * Returns:
14332  *   0 = Success, !0 = Failure
14333  */
14334 static int
14335 bxe_ifmedia_upd(struct ifnet *ifp)
14336 {
14337         struct bxe_softc *sc;
14338         struct ifmedia *ifm;
14339         int rc;
14340
14341         sc = ifp->if_softc;
14342         DBENTER(BXE_VERBOSE_PHY);
14343
14344         ifm = &sc->bxe_ifmedia;
14345         rc = 0;
14346
14347         /* We only support Ethernet media type. */
14348         if (IFM_TYPE(ifm->ifm_media) != IFM_ETHER) {
14349                 rc = EINVAL;
14350                 goto bxe_ifmedia_upd_exit;
14351         }
14352
14353         switch (IFM_SUBTYPE(ifm->ifm_media)) {
14354         case IFM_AUTO:
14355                 /* ToDo: What to do here? */
14356                 /* Doing nothing translates to success here. */
14357                  break;
14358         case IFM_10G_CX4:
14359                 /* Fall-through */
14360         case IFM_10G_SR:
14361                 /* Fall-through */
14362         case IFM_10G_T:
14363                 /* Fall-through */
14364         case IFM_10G_TWINAX:
14365                 /* Fall-through */
14366         default:
14367                 /* We don't support channging the media type. */
14368                 DBPRINT(sc, BXE_WARN, "%s(): Invalid media type!\n",
14369                     __FUNCTION__);
14370                 rc = EINVAL;
14371         }
14372
14373 bxe_ifmedia_upd_exit:
14374         DBENTER(BXE_VERBOSE_PHY);
14375         return (rc);
14376 }
14377
14378 /*
14379  * Called by the OS to report current media status
14380  * (link, speed, etc.).
14381  *
14382  * Returns:
14383  *   Nothing.
14384  */
14385 static void
14386 bxe_ifmedia_status(struct ifnet *ifp, struct ifmediareq *ifmr)
14387 {
14388         struct bxe_softc *sc;
14389
14390         sc = ifp->if_softc;
14391         DBENTER(BXE_EXTREME_LOAD | BXE_EXTREME_RESET);
14392
14393         /* Report link down if the driver isn't running. */
14394         if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) {
14395                 ifmr->ifm_active |= IFM_NONE;
14396                 goto bxe_ifmedia_status_exit;
14397         }
14398
14399         /* Setup the default interface info. */
14400         ifmr->ifm_status = IFM_AVALID;
14401         ifmr->ifm_active = IFM_ETHER;
14402
14403         if (sc->link_vars.link_up)
14404                 ifmr->ifm_status |= IFM_ACTIVE;
14405         else {
14406                 ifmr->ifm_active |= IFM_NONE;
14407                 goto bxe_ifmedia_status_exit;
14408         }
14409
14410         ifmr->ifm_active |= sc->media;
14411
14412         if (sc->link_vars.duplex == MEDIUM_FULL_DUPLEX)
14413                 ifmr->ifm_active |= IFM_FDX;
14414         else
14415                 ifmr->ifm_active |= IFM_HDX;
14416
14417 bxe_ifmedia_status_exit:
14418         DBEXIT(BXE_EXTREME_LOAD | BXE_EXTREME_RESET);
14419 }
14420
14421
14422 /*
14423  * Update last maximum scatter gather entry.
14424  *
14425  * Returns:
14426  *   None.
14427  */
14428 static __inline void
14429 bxe_update_last_max_sge(struct bxe_fastpath *fp, uint16_t index)
14430 {
14431         uint16_t last_max;
14432
14433         last_max = fp->last_max_sge;
14434         if (SUB_S16(index, last_max) > 0)
14435                 fp->last_max_sge = index;
14436 }
14437
14438 /*
14439  * Clear scatter gather mask next elements.
14440  *
14441  * Returns:
14442  *   None
14443  */
14444 static void
14445 bxe_clear_sge_mask_next_elems(struct bxe_fastpath *fp)
14446 {
14447         int i, index, j;
14448
14449         for (i = 0; i < NUM_RX_SGE_PAGES; i++) {
14450                 index = i * TOTAL_RX_SGE_PER_PAGE + USABLE_RX_SGE_PER_PAGE;
14451                 for (j = 0; j < 2; j++) {
14452                         SGE_MASK_CLEAR_BIT(fp, index);
14453                         index++;
14454                 }
14455         }
14456 }
14457
14458 /*
14459  * Update SGE producer.
14460  *
14461  * Returns:
14462  *   None.
14463  */
14464 static void
14465 bxe_update_sge_prod(struct bxe_fastpath *fp,
14466     struct eth_fast_path_rx_cqe *fp_cqe)
14467 {
14468         struct bxe_softc *sc;
14469         uint16_t delta, first_elem, last_max, last_elem, sge_len;
14470         int i;
14471
14472         sc = fp->sc;
14473         DBENTER(BXE_EXTREME_RECV);
14474
14475         delta = 0;
14476         sge_len = SGE_PAGE_ALIGN(le16toh(fp_cqe->pkt_len) -
14477             le16toh(fp_cqe->len_on_bd)) >> SGE_PAGE_SHIFT;
14478         if (!sge_len)
14479                 goto bxe_update_sge_prod_exit;
14480
14481         /* First mark all used pages. */
14482         for (i = 0; i < sge_len; i++)
14483                 SGE_MASK_CLEAR_BIT(fp, RX_SGE(le16toh(fp_cqe->sgl[i])));
14484
14485         /* Assume that the last SGE index is the biggest. */
14486         bxe_update_last_max_sge(fp, le16toh(fp_cqe->sgl[sge_len - 1]));
14487
14488         last_max = RX_SGE(fp->last_max_sge);
14489         last_elem = last_max >> RX_SGE_MASK_ELEM_SHIFT;
14490         first_elem = RX_SGE(fp->rx_sge_prod) >> RX_SGE_MASK_ELEM_SHIFT;
14491
14492         /* If ring is not full. */
14493         if (last_elem + 1 != first_elem)
14494                 last_elem++;
14495
14496         /* Now update the producer index. */
14497         for (i = first_elem; i != last_elem; i = NEXT_SGE_MASK_ELEM(i)) {
14498                 if (fp->rx_sge_mask[i])
14499                         break;
14500
14501                 fp->rx_sge_mask[i] = RX_SGE_MASK_ELEM_ONE_MASK;
14502                 delta += RX_SGE_MASK_ELEM_SZ;
14503         }
14504
14505         if (delta > 0) {
14506                 fp->rx_sge_prod += delta;
14507                 /* clear page-end entries */
14508                 bxe_clear_sge_mask_next_elems(fp);
14509         }
14510
14511 bxe_update_sge_prod_exit:
14512         DBEXIT(BXE_EXTREME_RECV);
14513 }
14514
14515 /*
14516  * Initialize scatter gather ring bitmask.
14517  *
14518  * Each entry in the SGE is associated with an aggregation in process.
14519  * Since there is no guarantee that all Ethernet frames associated with
14520  * a partciular TCP flow will arrive at the adapter and be placed into
14521  * the SGE chain contiguously, we maintain a bitmask for each SGE element
14522  * that identifies which aggregation an Ethernet frame belongs to.
14523  *
14524  * Returns:
14525  *   None
14526  */
14527 static __inline void
14528 bxe_init_sge_ring_bit_mask(struct bxe_fastpath *fp)
14529 {
14530
14531         /* Set the mask to all 1s, it's faster to compare to 0 than to 0xf. */
14532         memset(fp->rx_sge_mask, 0xff,
14533             (TOTAL_RX_SGE >> RX_SGE_MASK_ELEM_SHIFT) * sizeof(uint64_t));
14534
14535         /*
14536          * The SGE chain is formatted just like the RX chain.
14537          * The last two elements are reserved as a "next page pointer"
14538          * to the next page of SGE elements.  Clear the last two
14539          * elements in each SGE chain page since they will never be
14540          * used to track an aggregation.
14541          */
14542         bxe_clear_sge_mask_next_elems(fp);
14543 }
14544
14545 /*
14546  * The current mbuf is part of an aggregation.  Swap the mbuf into the TPA
14547  * aggregation queue, swap an empty mbuf back onto the receive chain, and
14548  * mark the current aggregation queue as in-progress.
14549  *
14550  * Returns:
14551  *   None.
14552  */
14553 static void
14554 bxe_tpa_start(struct bxe_fastpath *fp, uint16_t queue, uint16_t cons,
14555     uint16_t prod)
14556 {
14557         struct bxe_softc *sc;
14558         struct mbuf *m_temp;
14559         struct eth_rx_bd *rx_bd;
14560         bus_dmamap_t map_temp;
14561         int max_agg_queues;
14562
14563         sc = fp->sc;
14564         DBENTER(BXE_INSANE_RECV | BXE_INSANE_TPA);
14565
14566
14567
14568         DBPRINT(sc, BXE_EXTREME_TPA,
14569             "%s(): fp[%02d].tpa[%02d], cons=0x%04X, prod=0x%04X\n",
14570             __FUNCTION__, fp->index, queue, cons, prod);
14571
14572         max_agg_queues = CHIP_IS_E1(sc) ? ETH_MAX_AGGREGATION_QUEUES_E1 :
14573             ETH_MAX_AGGREGATION_QUEUES_E1H;
14574
14575         DBRUNIF((queue > max_agg_queues),
14576             BXE_PRINTF("%s(): fp[%02d] illegal aggregation (%d > %d)!\n",
14577             __FUNCTION__, fp->index, queue, max_agg_queues));
14578
14579         DBRUNIF((fp->tpa_state[queue] != BXE_TPA_STATE_STOP),
14580             BXE_PRINTF("%s(): Starting aggregation on "
14581             "fp[%02d].tpa[%02d] even though queue is not in the "
14582             "TPA_STOP state!\n", __FUNCTION__, fp->index, queue));
14583
14584         /* Remove the existing mbuf and mapping from the TPA pool. */
14585         m_temp = fp->tpa_mbuf_ptr[queue];
14586         map_temp = fp->tpa_mbuf_map[queue];
14587
14588         /* Only the paranoid survive! */
14589         if(m_temp == NULL) {
14590                 BXE_PRINTF("%s(%d): fp[%02d].tpa[%02d] not allocated!\n",
14591                     __FILE__, __LINE__, fp->index, queue);
14592                 /* ToDo: Additional error handling! */
14593                 goto bxe_tpa_start_exit;
14594         }
14595
14596         /* Move received mbuf and mapping to TPA pool. */
14597         fp->tpa_mbuf_ptr[queue] = fp->rx_mbuf_ptr[cons];
14598         fp->tpa_mbuf_map[queue] = fp->rx_mbuf_map[cons];
14599
14600         /* Place the TPA bin into the START state. */
14601         fp->tpa_state[queue] = BXE_TPA_STATE_START;
14602         DBRUN(fp->tpa_queue_used |= (1 << queue));
14603
14604         /* Get the rx_bd for the next open entry on the receive chain. */
14605         rx_bd = &fp->rx_chain[prod];
14606
14607         /* Update the rx_bd with the empty mbuf from the TPA pool. */
14608         rx_bd->addr_hi = htole32(U64_HI(fp->tpa_mbuf_segs[queue].ds_addr));
14609         rx_bd->addr_lo = htole32(U64_LO(fp->tpa_mbuf_segs[queue].ds_addr));
14610         fp->rx_mbuf_ptr[prod] = m_temp;
14611         fp->rx_mbuf_map[prod] = map_temp;
14612
14613 bxe_tpa_start_exit:
14614         DBEXIT(BXE_INSANE_RECV | BXE_INSANE_TPA);
14615 }
14616
14617 /*
14618  * When a TPA aggregation is completed, loop through the individual mbufs
14619  * of the aggregation, combining them into a single mbuf which will be sent
14620  * up the stack.  Refill all freed SGEs with mbufs as we go along.
14621  *
14622  * Returns:
14623  *   0 = Success, !0 = Failure.
14624  */
14625 static int
14626 bxe_fill_frag_mbuf(struct bxe_softc *sc, struct bxe_fastpath *fp,
14627     struct mbuf *m, struct eth_fast_path_rx_cqe *fp_cqe, uint16_t cqe_idx)
14628 {
14629         struct mbuf *m_frag;
14630         uint32_t frag_len, frag_size, pages, i;
14631         uint16_t sge_idx, len_on_bd;
14632         int j, rc;
14633
14634         DBENTER(BXE_EXTREME_RECV | BXE_EXTREME_TPA);
14635
14636         rc = 0;
14637         len_on_bd = le16toh(fp_cqe->len_on_bd);
14638         frag_size = le16toh(fp_cqe->pkt_len) - len_on_bd;
14639         pages = SGE_PAGE_ALIGN(frag_size) >> SGE_PAGE_SHIFT;
14640
14641         DBPRINT(sc, BXE_VERBOSE_TPA,
14642             "%s(): len_on_bd=%d, frag_size=%d, pages=%d\n",
14643             __FUNCTION__, len_on_bd, frag_size, pages);
14644
14645         /* Make sure the aggregated frame is not too big to handle. */
14646         if (pages > 8 * PAGES_PER_SGE) {
14647                 DBPRINT(sc, BXE_FATAL,
14648                     "%s(): fp[%02d].rx_sge[0x%04X] has too many pages (%d)!\n",
14649                     __FUNCTION__, fp->index, cqe_idx, pages);
14650                 DBPRINT(sc, BXE_FATAL,
14651                     "%s(): fp_cqe->pkt_len = %d fp_cqe->len_on_bd = %d\n",
14652                     __FUNCTION__, le16toh(fp_cqe->pkt_len), len_on_bd);
14653                 bxe_panic_dump(sc);
14654                 rc = EINVAL;
14655                 goto bxe_fill_frag_mbuf_exit;
14656         }
14657
14658         /*
14659          * Scan through the scatter gather list, pulling individual
14660          * mbufs into a single mbuf for the host stack.
14661          */
14662         for (i = 0, j = 0; i < pages; i += PAGES_PER_SGE, j++) {
14663                 sge_idx = RX_SGE(le16toh(fp_cqe->sgl[j]));
14664
14665                 /*
14666                  * Firmware gives the indices of the SGE as if the ring is an
14667                  * array (meaning that the "next" element will consume 2
14668                  * indices).
14669                  */
14670                 frag_len = min(frag_size, (uint32_t)(BCM_PAGE_SIZE *
14671                     PAGES_PER_SGE));
14672
14673                 DBPRINT(sc, BXE_VERBOSE_TPA,
14674                     "%s(): i=%d, j=%d, frag_size=%d, frag_len=%d\n",
14675                     __FUNCTION__, i, j, frag_size, frag_len);
14676
14677                 m_frag = fp->rx_sge_buf_ptr[sge_idx];
14678
14679                 /* Allocate a new mbuf for the SGE. */
14680                 rc = bxe_alloc_rx_sge_mbuf(fp, sge_idx);
14681                 if (rc) {
14682                         /*
14683                          * Leave all remaining SGEs in the ring.
14684                          */
14685                         goto bxe_fill_frag_mbuf_exit;
14686                 }
14687
14688                 /* Update the fragment its length. */
14689                 m_frag->m_len = frag_len;
14690
14691                 /* Concatenate the fragment to the head mbuf. */
14692                 m_cat(m, m_frag);
14693                 DBRUN(fp->sge_mbuf_alloc--);
14694
14695                 /* Update TPA mbuf size and remaining fragment size. */
14696                 m->m_pkthdr.len += frag_len;
14697                 frag_size -= frag_len;
14698         }
14699
14700 bxe_fill_frag_mbuf_exit:
14701         DBPRINT(sc, BXE_VERBOSE_TPA,
14702             "%s(): frag_size=%d\n", __FUNCTION__, frag_size);
14703         DBEXIT(BXE_EXTREME_RECV | BXE_EXTREME_TPA);
14704         return (rc);
14705 }
14706
14707 /*
14708  * The aggregation on the current TPA queue has completed.  Pull the
14709  * individual mbuf fragments together into a single mbuf, perform all
14710  * necessary checksum calculations, and send the resuting mbuf to the stack.
14711  *
14712  * Returns:
14713  *   None.
14714  */
14715 static void
14716 bxe_tpa_stop(struct bxe_softc *sc, struct bxe_fastpath *fp, uint16_t queue,
14717     int pad, int len, union eth_rx_cqe *cqe, uint16_t cqe_idx)
14718 {
14719         struct mbuf *m;
14720         struct ifnet *ifp;
14721         int rc;
14722
14723         DBENTER(BXE_INSANE_RECV | BXE_INSANE_TPA);
14724         DBPRINT(sc, (BXE_EXTREME_RECV | BXE_EXTREME_TPA),
14725             "%s(): fp[%02d].tpa[%02d], len=%d, pad=%d\n",
14726             __FUNCTION__, fp->index, queue, len, pad);
14727
14728         rc = 0;
14729         ifp = sc->bxe_ifp;
14730         m = fp->tpa_mbuf_ptr[queue];
14731
14732         /* Allocate a replacement before modifying existing mbuf. */
14733         rc = bxe_alloc_tpa_mbuf(fp, queue);
14734         if (rc) {
14735                 /* Drop the frame and log a soft error. */
14736                 fp->rx_soft_errors++;
14737                 goto bxe_tpa_stop_exit;
14738         }
14739
14740         /* We have a replacement, fixup the current mbuf. */
14741         m_adj(m, pad);
14742         m->m_pkthdr.len = m->m_len = len;
14743
14744         /* Mark the checksums valid (taken care of by firmware). */
14745         m->m_pkthdr.csum_flags |= CSUM_IP_CHECKED | CSUM_IP_VALID |
14746             CSUM_DATA_VALID | CSUM_PSEUDO_HDR;
14747         m->m_pkthdr.csum_data = 0xffff;
14748
14749         /* Aggregate all of the SGEs into a single mbuf. */
14750         rc = bxe_fill_frag_mbuf(sc, fp, m, &cqe->fast_path_cqe, cqe_idx);
14751         if (rc) {
14752                 /* Drop the packet and log an error. */
14753                 fp->rx_soft_errors++;
14754                 m_freem(m);
14755         } else  {
14756                 /* Find VLAN tag and send frame up to the stack. */
14757                 if ((le16toh(cqe->fast_path_cqe.pars_flags.flags) &
14758                     PARSING_FLAGS_VLAN)) {
14759                         m->m_pkthdr.ether_vtag =
14760                             cqe->fast_path_cqe.vlan_tag;
14761                         m->m_flags |= M_VLANTAG;
14762                 }
14763
14764                 /* Assign packet to the appropriate interface. */
14765                 m->m_pkthdr.rcvif = ifp;
14766
14767                 /* Update packet statistics. */
14768                 fp->rx_tpa_pkts++;
14769                 ifp->if_ipackets++;
14770
14771                 /* ToDo: Any potential locking issues here? */
14772                 /* Pass the frame to the stack. */
14773                 (*ifp->if_input)(ifp, m);
14774         }
14775
14776         /* We passed mbuf up the stack or dropped the frame. */
14777         DBRUN(fp->tpa_mbuf_alloc--);
14778
14779 bxe_tpa_stop_exit:
14780         fp->tpa_state[queue] = BXE_TPA_STATE_STOP;
14781         DBRUN(fp->tpa_queue_used &= ~(1 << queue));
14782         DBEXIT(BXE_INSANE_RECV | BXE_INSANE_TPA);
14783 }
14784
14785 /*
14786  * Notify the controller that the RX producer indices have been updated for
14787  * a fastpath connection by writing them to the controller.
14788  *
14789  * Returns:
14790  *   None
14791  */
14792 static __inline void
14793 bxe_update_rx_prod(struct bxe_softc *sc, struct bxe_fastpath *fp,
14794     uint16_t bd_prod, uint16_t cqe_prod, uint16_t sge_prod)
14795 {
14796         volatile struct ustorm_eth_rx_producers rx_prods = {0};
14797         int i;
14798
14799         /* Update producers. */
14800         rx_prods.bd_prod  =  bd_prod;
14801         rx_prods.cqe_prod = cqe_prod;
14802         rx_prods.sge_prod = sge_prod;
14803
14804         wmb();
14805
14806         for (i = 0; i < sizeof(struct ustorm_eth_rx_producers) / 4; i++){
14807                 REG_WR(sc, BAR_USTORM_INTMEM +
14808                     USTORM_RX_PRODS_OFFSET(BP_PORT(sc), fp->cl_id) + i * 4,
14809                     ((volatile uint32_t *) &rx_prods)[i]);
14810         }
14811
14812         DBPRINT(sc, BXE_EXTREME_RECV, "%s(%d): Wrote fp[%02d] bd_prod = 0x%04X, "
14813             "cqe_prod = 0x%04X, sge_prod = 0x%04X\n", __FUNCTION__, curcpu,
14814             fp->index, bd_prod, cqe_prod, sge_prod);
14815 }
14816
14817 /*
14818  * Processes received frames.
14819  *
14820  * Returns:
14821  *   Nothing.
14822  */
14823 static void
14824 bxe_rxeof(struct bxe_fastpath *fp)
14825 {
14826         struct bxe_softc *sc;
14827         struct ifnet *ifp;
14828         uint16_t rx_bd_cons, rx_bd_cons_idx;
14829         uint16_t rx_bd_prod, rx_bd_prod_idx;
14830         uint16_t rx_cq_cons, rx_cq_cons_idx;
14831         uint16_t rx_cq_prod, rx_cq_cons_sb;
14832         unsigned long rx_pkts = 0;
14833         int rc;
14834
14835         sc = fp->sc;
14836         ifp = sc->bxe_ifp;
14837
14838         DBENTER(BXE_EXTREME_RECV);
14839
14840         /* Get the status block's view of the RX completion consumer index. */
14841         rx_cq_cons_sb = bxe_rx_cq_cons(fp);
14842
14843         /*
14844          * Get working copies of the driver's view of the
14845          * RX indices. These are 16 bit values that are
14846          * expected to increment from 0 to 65535 and then
14847          * wrap-around to 0 again.
14848          */
14849         rx_bd_cons = fp->rx_bd_cons;
14850         rx_bd_prod = fp->rx_bd_prod;
14851         rx_cq_cons = fp->rx_cq_cons;
14852         rx_cq_prod = fp->rx_cq_prod;
14853
14854         DBPRINT(sc, (BXE_EXTREME_RECV),
14855             "%s(%d): BEFORE: fp[%02d], rx_bd_cons = 0x%04X, rx_bd_prod = 0x%04X, "
14856             "rx_cq_cons_sw = 0x%04X, rx_cq_prod_sw = 0x%04X\n", __FUNCTION__,
14857             curcpu, fp->index, rx_bd_cons, rx_bd_prod, rx_cq_cons, rx_cq_prod);
14858
14859         /*
14860          * Memory barrier to prevent speculative reads of the RX buffer
14861          * from getting ahead of the index in the status block.
14862          */
14863         rmb();
14864
14865         /*
14866          * Scan through the receive chain as long
14867          * as there is work to do.
14868          */
14869         while (rx_cq_cons != rx_cq_cons_sb) {
14870                 struct mbuf *m;
14871                 union eth_rx_cqe *cqe;
14872                 uint8_t cqe_fp_flags;
14873                 uint16_t len, pad;
14874
14875                 /*
14876                  * Convert the 16 bit indices used by hardware
14877                  * into array indices used by the driver.
14878                  */
14879                 rx_cq_cons_idx = RCQ_ENTRY(rx_cq_cons);
14880                 rx_bd_prod_idx = RX_BD(rx_bd_prod);
14881                 rx_bd_cons_idx = RX_BD(rx_bd_cons);
14882                 wmb();
14883
14884                 /* Fetch the completion queue entry (i.e. cookie). */
14885                 cqe = (union eth_rx_cqe *)
14886                     &fp->rcq_chain[rx_cq_cons_idx];
14887                 cqe_fp_flags = cqe->fast_path_cqe.type_error_flags;
14888
14889                 /* Sanity check the cookie flags. */
14890                 if (__predict_false(cqe_fp_flags == 0)) {
14891                         fp->rx_null_cqe_flags++;
14892                         DBRUN(bxe_dump_cqe(fp, rx_cq_cons_idx, cqe));
14893                         /* ToDo: What error handling can be done here? */
14894                 }
14895
14896                 /* Check the CQE type for slowpath or fastpath completion. */
14897                 if (__predict_false(CQE_TYPE(cqe_fp_flags) ==
14898                     RX_ETH_CQE_TYPE_ETH_RAMROD)) {
14899                         /* This is a slowpath completion. */
14900                         bxe_sp_event(fp, cqe);
14901                         goto bxe_rxeof_next_cqe;
14902
14903                 } else {
14904                         /* This is a fastpath completion. */
14905
14906                         /* Get the length and pad information from the CQE. */
14907                         len = le16toh(cqe->fast_path_cqe.pkt_len);
14908                         pad = cqe->fast_path_cqe.placement_offset;
14909
14910                         /* Check if the completion is for TPA. */
14911                         if ((fp->disable_tpa == FALSE) &&
14912                             (TPA_TYPE(cqe_fp_flags) !=
14913                             (TPA_TYPE_START | TPA_TYPE_END))) {
14914                                 uint16_t queue = cqe->fast_path_cqe.queue_index;
14915
14916                                 /*
14917                                  * No need to worry about error flags in
14918                                  * the frame as the firmware has already
14919                                  * managed that for us when aggregating
14920                                  * the frames.
14921                                  */
14922
14923                                 /* Check if TPA aggregation has started. */
14924                                 if (TPA_TYPE(cqe_fp_flags) == TPA_TYPE_START) {
14925                                         bxe_tpa_start(fp, queue, rx_bd_cons_idx,
14926                                             rx_bd_prod_idx);
14927                                         goto bxe_rxeof_next_rx;
14928                                 }
14929
14930                                 /* Check if TPA aggregation has completed. */
14931                                 if (TPA_TYPE(cqe_fp_flags) == TPA_TYPE_END) {
14932                                         DBRUNIF(!BXE_RX_SUM_FIX(cqe),
14933                                             DBPRINT(sc, BXE_FATAL,
14934                                             "%s(): STOP on non-TCP data.\n",
14935                                             __FUNCTION__));
14936
14937                                         /*
14938                                          * This is the size of the linear
14939                                          * data on this mbuf.
14940                                          */
14941                                         len = le16toh(cqe->fast_path_cqe.len_on_bd);
14942
14943                                         /*
14944                                          * Stop the aggregation and pass
14945                                          * the frame up.
14946                                          */
14947                                         bxe_tpa_stop(sc, fp, queue, pad, len,
14948                                             cqe, rx_cq_cons_idx);
14949                                         bxe_update_sge_prod(fp,
14950                                             &cqe->fast_path_cqe);
14951                                         goto bxe_rxeof_next_cqe;
14952                                 }
14953                         }
14954
14955                         m = fp->rx_mbuf_ptr[rx_bd_cons_idx];
14956
14957                         /* Allocate a replacement before modifying existing mbuf. */
14958                         rc = bxe_alloc_rx_bd_mbuf(fp, rx_bd_prod_idx);
14959                         if (rc) {
14960                                 /* Drop the frame and log a soft error. */
14961                                 fp->rx_soft_errors++;
14962                                 goto bxe_rxeof_next_rx;
14963                         }
14964
14965                         /* Check if the received frame has any errors. */
14966                         if (__predict_false(cqe_fp_flags &
14967                             ETH_RX_ERROR_FLAGS)) {
14968                                 DBPRINT(sc, BXE_WARN ,
14969                                     "%s(): fp[%02d].cqe[0x%04X] has errors "
14970                                     "(0x%08X)!\n", __FUNCTION__, fp->index,
14971                                     rx_cq_cons, cqe_fp_flags);
14972
14973                                 fp->rx_soft_errors++;
14974                                 goto bxe_rxeof_next_rx;
14975                         }
14976
14977                         /* We have a replacement, fixup the current mbuf. */
14978                         m_adj(m, pad);
14979                         m->m_pkthdr.len = m->m_len = len;
14980
14981                         /* Assign packet to the appropriate interface. */
14982                         m->m_pkthdr.rcvif = ifp;
14983
14984                         /* Assume no hardware checksum complated. */
14985                         m->m_pkthdr.csum_flags = 0;
14986
14987                         /* Validate checksum if offload enabled. */
14988                         if (ifp->if_capenable & IFCAP_RXCSUM) {
14989                                 /* Check whether IP checksummed or not. */
14990                                 if (sc->rx_csum &&
14991                                     !(cqe->fast_path_cqe.status_flags &
14992                                     ETH_FAST_PATH_RX_CQE_IP_XSUM_NO_VALIDATION_FLG)) {
14993                                         m->m_pkthdr.csum_flags |=
14994                                             CSUM_IP_CHECKED;
14995                                         if (__predict_false(cqe_fp_flags &
14996                                             ETH_FAST_PATH_RX_CQE_IP_BAD_XSUM_FLG)) {
14997                                                 DBPRINT(sc, BXE_WARN_SEND,
14998                                         "%s(): Invalid IP checksum!\n",
14999                                                     __FUNCTION__);
15000                                         } else
15001                                                 m->m_pkthdr.csum_flags |=
15002                                                     CSUM_IP_VALID;
15003                                 }
15004
15005                                 /* Check for a valid TCP/UDP frame. */
15006                                 if (sc->rx_csum &&
15007                                     !(cqe->fast_path_cqe.status_flags &
15008                                     ETH_FAST_PATH_RX_CQE_L4_XSUM_NO_VALIDATION_FLG)) {
15009                                         /* Check for a good TCP/UDP checksum. */
15010                                         if (__predict_false(cqe_fp_flags &
15011                                             ETH_FAST_PATH_RX_CQE_L4_BAD_XSUM_FLG)) {
15012                                                 DBPRINT(sc, BXE_VERBOSE_RECV,
15013                                         "%s(): Invalid TCP/UDP checksum!\n",
15014                                                     __FUNCTION__);
15015                                         } else {
15016                                                 m->m_pkthdr.csum_data = 0xFFFF;
15017                                                 m->m_pkthdr.csum_flags |=
15018                                                     (CSUM_DATA_VALID |
15019                                                     CSUM_PSEUDO_HDR);
15020                                         }
15021                                 }
15022                         }
15023
15024                         /*
15025                          * If we received a packet with a vlan tag,
15026                          * attach that information to the packet.
15027                          */
15028                         if (cqe->fast_path_cqe.pars_flags.flags &
15029                             PARSING_FLAGS_VLAN) {
15030                                 m->m_pkthdr.ether_vtag =
15031                                     cqe->fast_path_cqe.vlan_tag;
15032                                 m->m_flags |= M_VLANTAG;
15033                         }
15034
15035 #if __FreeBSD_version >= 800000
15036                         /* Tell OS what RSS queue was used for this flow. */
15037                         m->m_pkthdr.flowid = fp->index;
15038                         m->m_flags |= M_FLOWID;
15039 #endif
15040
15041                         /* Last chance to check for problems. */
15042                         DBRUN(bxe_validate_rx_packet(fp, rx_cq_cons, cqe, m));
15043
15044                         /* Update packet statistics. */
15045                         ifp->if_ipackets++;
15046                         rx_pkts++;
15047
15048                         /* ToDo: Any potential locking issues here? */
15049                         /* Pass the frame to the stack. */
15050                         (*ifp->if_input)(ifp, m);
15051
15052                         DBRUN(fp->rx_mbuf_alloc--);
15053                 }
15054
15055 bxe_rxeof_next_rx:
15056                 rx_bd_prod = NEXT_RX_BD(rx_bd_prod);
15057                 rx_bd_cons = NEXT_RX_BD(rx_bd_cons);
15058
15059 bxe_rxeof_next_cqe:
15060                 rx_cq_prod = NEXT_RCQ_IDX(rx_cq_prod);
15061                 rx_cq_cons = NEXT_RCQ_IDX(rx_cq_cons);
15062
15063                 /*
15064                  * Memory barrier to prevent speculative reads of the RX buffer
15065                  * from getting ahead of the index in the status block.
15066                  */
15067                 rmb();
15068         }
15069
15070         /* Update driver copy of the fastpath indices. */
15071         fp->rx_bd_cons = rx_bd_cons;
15072         fp->rx_bd_prod = rx_bd_prod;
15073         fp->rx_cq_cons = rx_cq_cons;
15074         fp->rx_cq_prod = rx_cq_prod;
15075
15076         DBPRINT(sc, (BXE_EXTREME_RECV),
15077             "%s(%d):  AFTER: fp[%02d], rx_bd_cons = 0x%04X, rx_bd_prod = 0x%04X, "
15078             "rx_cq_cons_sw = 0x%04X, rx_cq_prod_sw = 0x%04X\n", __FUNCTION__,
15079             curcpu, fp->index, rx_bd_cons, rx_bd_prod, rx_cq_cons, rx_cq_prod);
15080
15081         /* Update producers */
15082         bxe_update_rx_prod(sc, fp, fp->rx_bd_prod,
15083             fp->rx_cq_prod, fp->rx_sge_prod);
15084         bus_space_barrier(sc->bxe_btag, sc->bxe_bhandle, 0, 0,
15085             BUS_SPACE_BARRIER_READ);
15086
15087         fp->rx_pkts += rx_pkts;
15088         DBEXIT(BXE_EXTREME_RECV);
15089 }
15090
15091 /*
15092  * Processes transmit completions.
15093  *
15094  * Returns:
15095  *   Nothing.
15096  */
15097 static void
15098 bxe_txeof(struct bxe_fastpath *fp)
15099 {
15100         struct bxe_softc *sc;
15101         struct ifnet *ifp;
15102         struct eth_tx_start_bd *txbd;
15103         uint16_t hw_pkt_cons, sw_pkt_cons, sw_tx_bd_cons;
15104         uint16_t bd_index, pkt_index, nbds;
15105         int i;
15106
15107         sc = fp->sc;
15108         ifp = sc->bxe_ifp;
15109
15110         DBENTER(BXE_EXTREME_SEND);
15111
15112         /* Get the hardware's view of the TX packet consumer index. */
15113         hw_pkt_cons = le16toh(*fp->tx_pkt_cons_sb);
15114         sw_pkt_cons = fp->tx_pkt_cons;
15115         sw_tx_bd_cons = fp->tx_bd_cons;
15116
15117         /* Cycle through any completed TX chain page entries. */
15118         while (sw_pkt_cons != hw_pkt_cons) {
15119                 bd_index = TX_BD(sw_tx_bd_cons);
15120                 pkt_index = TX_BD(sw_pkt_cons);
15121
15122                 txbd = &fp->tx_chain[bd_index].start_bd;
15123                 nbds = txbd->nbd;
15124
15125                 /* Free the completed frame's mbuf. */
15126                 if (__predict_true(fp->tx_mbuf_ptr[pkt_index] != NULL)) {
15127                         /* Unmap the mbuf from non-paged memory. */
15128                         bus_dmamap_unload(fp->tx_mbuf_tag,
15129                             fp->tx_mbuf_map[pkt_index]);
15130
15131                         /* Return the mbuf to the system. */
15132                         m_freem(fp->tx_mbuf_ptr[pkt_index]);
15133                         fp->tx_mbuf_alloc--;
15134                         fp->tx_mbuf_ptr[pkt_index] = NULL;
15135                         fp->opackets++;
15136                 } else {
15137                         fp->tx_chain_lost_mbuf++;
15138                 }
15139
15140                 /* Updated packet consumer value. */
15141                 sw_pkt_cons++;
15142
15143                 /* Skip over the remaining used buffer descriptors. */
15144                 fp->tx_bd_used -= nbds;
15145                 for (i = 0; i < nbds; i++)
15146                         sw_tx_bd_cons = NEXT_TX_BD(sw_tx_bd_cons);
15147
15148                 /* Check for new work since we started. */
15149                 hw_pkt_cons = le16toh(*fp->tx_pkt_cons_sb);
15150                 rmb();
15151         }
15152
15153         /* Enable new transmits if we've made enough room. */
15154         if (fp->tx_bd_used < BXE_TX_CLEANUP_THRESHOLD) {
15155                 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
15156                 if (fp->tx_bd_used == 0) {
15157                         /*
15158                          * Clear the watchdog timer if we've emptied
15159                          * the TX chain.
15160                          */
15161                         fp->watchdog_timer = 0;
15162                 } else {
15163                         /*
15164                          * Reset the watchdog timer if we still have
15165                          * transmits pending.
15166                          */
15167                         fp->watchdog_timer = BXE_TX_TIMEOUT;
15168                 }
15169         }
15170
15171         /* Save our indices. */
15172         fp->tx_pkt_cons = sw_pkt_cons;
15173         fp->tx_bd_cons = sw_tx_bd_cons;
15174         DBEXIT(BXE_EXTREME_SEND);
15175 }
15176
15177 /*
15178  * Transmit timeout handler.
15179  *
15180  * Returns:
15181  *   0 = No timeout, !0 = timeout occurred.
15182  */
15183 static int
15184 bxe_watchdog(struct bxe_fastpath *fp)
15185 {
15186         struct bxe_softc *sc;
15187         int rc = 0;
15188
15189         sc = fp->sc;
15190         DBENTER(BXE_INSANE_SEND);
15191
15192         BXE_FP_LOCK(fp);
15193         if (fp->watchdog_timer == 0 || --fp->watchdog_timer) {
15194                 rc = EINVAL;
15195                 BXE_FP_UNLOCK(fp);
15196                 goto bxe_watchdog_exit;
15197         }
15198         BXE_FP_UNLOCK(fp);
15199
15200         BXE_PRINTF("TX watchdog timeout occurred on fp[%02d], "
15201             "resetting!\n", fp->index);
15202
15203         /* DBRUNLV(BXE_FATAL, bxe_breakpoint(sc)); */
15204
15205         BXE_CORE_LOCK(sc);
15206
15207         /* Mark the interface as down. */
15208         sc->bxe_ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
15209
15210         bxe_stop_locked(sc, UNLOAD_NORMAL);
15211         DELAY(10000);
15212         bxe_init_locked(sc, LOAD_OPEN);
15213
15214         BXE_CORE_UNLOCK(sc);
15215
15216 bxe_watchdog_exit:
15217         DBEXIT(BXE_INSANE_SEND);
15218         return (rc);
15219 }
15220
15221
15222 /*
15223  * The periodic timer tick routine.
15224  *
15225  * This code only runs when the interface is up.
15226  *
15227  * Returns:
15228  *   None
15229  */
15230 static void
15231 bxe_tick(void *xsc)
15232 {
15233         struct bxe_softc *sc;
15234         struct bxe_fastpath *fp;
15235 #if 0
15236         /* Re-enable at a later time. */
15237         uint32_t drv_pulse, mcp_pulse;
15238 #endif
15239         int i, func;
15240
15241         sc = xsc;
15242         DBENTER(BXE_INSANE_MISC);
15243
15244
15245         /* Check for TX timeouts on any fastpath. */
15246         for (i = 0; i < sc->num_queues; i++) {
15247                 fp = &sc->fp[i];
15248
15249                 if (bxe_watchdog(fp) != 0)
15250                         break;
15251         }
15252
15253         func = BP_FUNC(sc);
15254
15255         /* Schedule the next tick. */
15256         callout_reset(&sc->bxe_tick_callout, hz, bxe_tick, sc);
15257
15258 #if 0
15259         if (!NOMCP(sc)) {
15260                 func = BP_FUNC(sc);
15261
15262                 ++sc->fw_drv_pulse_wr_seq;
15263                 sc->fw_drv_pulse_wr_seq &= DRV_PULSE_SEQ_MASK;
15264
15265                 /* Let the MCP know we're alive. */
15266                 drv_pulse = sc->fw_drv_pulse_wr_seq;
15267                 SHMEM_WR(sc, func_mb[func].drv_pulse_mb, drv_pulse);
15268
15269                 /* Check if the MCP is still alive. */
15270                 mcp_pulse = (SHMEM_RD(sc, func_mb[func].mcp_pulse_mb) &
15271                     MCP_PULSE_SEQ_MASK);
15272
15273                 /*
15274                  * The delta between driver pulse and MCP response should be 1
15275                  * (before MCP response) or 0 (after MCP response).
15276                  */
15277                 if ((drv_pulse != mcp_pulse) && (drv_pulse != ((mcp_pulse + 1) &
15278                     MCP_PULSE_SEQ_MASK))) {
15279                         /* Someone's in cardiac arrest. */
15280                         DBPRINT(sc, BXE_WARN,
15281                             "%s(): drv_pulse (0x%x) != mcp_pulse (0x%x)\n",
15282                             __FUNCTION__, drv_pulse, mcp_pulse);
15283                 }
15284         }
15285 #endif
15286
15287         if ((sc->state == BXE_STATE_OPEN) || (sc->state == BXE_STATE_DISABLED))
15288                 bxe_stats_handle(sc, STATS_EVENT_UPDATE);
15289 }
15290
15291 #ifdef BXE_DEBUG
15292 /*
15293  * Allows the driver state to be dumped through the sysctl interface.
15294  *
15295  * Returns:
15296  *   0 for success, positive value for failure.
15297  */
15298 static int
15299 bxe_sysctl_driver_state(SYSCTL_HANDLER_ARGS)
15300 {
15301         struct bxe_softc *sc;
15302         struct bxe_fastpath *fp;
15303         int error, i, result;
15304
15305         sc = (struct bxe_softc *)arg1;
15306         result = -1;
15307         error = sysctl_handle_int(oidp, &result, 0, req);
15308         if (error || !req->newptr)
15309                 return (error);
15310
15311         if (result == 1) {
15312                 bxe_dump_driver_state(sc);
15313                 for (i = 0; i < sc->num_queues; i++) {
15314                         fp = &sc->fp[i];
15315                         bxe_dump_fp_state(fp);
15316                 }
15317                 bxe_dump_status_block(sc);
15318         }
15319
15320         return (error);
15321 }
15322
15323 /*
15324  * Allows the hardware state to be dumped through the sysctl interface.
15325  *
15326  * Returns:
15327  *   0 for success, positive value for failure.
15328  */
15329 static int
15330 bxe_sysctl_hw_state(SYSCTL_HANDLER_ARGS)
15331 {
15332         struct bxe_softc *sc;
15333         int error, result;
15334
15335         sc = (struct bxe_softc *)arg1;
15336         result = -1;
15337         error = sysctl_handle_int(oidp, &result, 0, req);
15338         if (error || !req->newptr)
15339                 return (error);
15340
15341         if (result == 1)
15342                 bxe_dump_hw_state(sc);
15343
15344         return (error);
15345 }
15346
15347 /*
15348  * Allows the MCP firmware to be dumped through the sysctl interface.
15349  *
15350  * Returns:
15351  *   0 for success, positive value for failure.
15352  */
15353 static int
15354 bxe_sysctl_dump_fw(SYSCTL_HANDLER_ARGS)
15355 {
15356         struct bxe_softc *sc;
15357         int error, result;
15358
15359         sc = (struct bxe_softc *)arg1;
15360         result = -1;
15361         error = sysctl_handle_int(oidp, &result, 0, req);
15362         if (error || !req->newptr)
15363                 return (error);
15364
15365         if (result == 1)
15366                 bxe_dump_fw(sc);
15367
15368         return (error);
15369 }
15370
15371 /*
15372  * Provides a sysctl interface to allow dumping the RX completion chain.
15373  *
15374  * Returns:
15375  *   0 for success, positive value for failure.
15376  */
15377 static int
15378 bxe_sysctl_dump_rx_cq_chain(SYSCTL_HANDLER_ARGS)
15379 {
15380         struct bxe_softc *sc;
15381         struct bxe_fastpath *fp;
15382         int error, result;
15383
15384         sc = (struct bxe_softc *)arg1;
15385         result = -1;
15386         error = sysctl_handle_int(oidp, &result, 0, req);
15387         if (error || !req->newptr)
15388                 return (error);
15389
15390         if ((result >= 0) && (result < sc->num_queues)) {
15391                 fp = &sc->fp[result];
15392                 bxe_dump_rx_cq_chain(fp, 0, TOTAL_RCQ_ENTRIES);
15393         }
15394
15395         return (error);
15396 }
15397
15398
15399 /*
15400  * Provides a sysctl interface to allow dumping the RX chain.
15401  *
15402  * Returns:
15403  *   0 for success, positive value for failure.
15404  */
15405 static int
15406 bxe_sysctl_dump_rx_bd_chain(SYSCTL_HANDLER_ARGS)
15407 {
15408         struct bxe_softc *sc;
15409         struct bxe_fastpath *fp;
15410         int error, result;
15411
15412         sc = (struct bxe_softc *)arg1;
15413         result = -1;
15414         error = sysctl_handle_int(oidp, &result, 0, req);
15415         if (error || !req->newptr)
15416                 return (error);
15417
15418         if ((result >= 0) && (result < sc->num_queues)) {
15419                 fp = &sc->fp[result];
15420                 bxe_dump_rx_bd_chain(fp, 0, TOTAL_RX_BD);
15421         }
15422
15423         return (error);
15424 }
15425
15426 /*
15427 * Provides a sysctl interface to allow dumping the TX chain.
15428 *
15429 * Returns:
15430 *   0 for success, positive value for failure.
15431 */
15432 static int
15433 bxe_sysctl_dump_tx_chain(SYSCTL_HANDLER_ARGS)
15434 {
15435         struct bxe_softc *sc;
15436         struct bxe_fastpath *fp;
15437         int error, result;
15438
15439         sc = (struct bxe_softc *)arg1;
15440         result = -1;
15441         error = sysctl_handle_int(oidp, &result, 0, req);
15442         if (error || !req->newptr)
15443                 return (error);
15444
15445         if ((result >= 0) && (result < sc->num_queues)) {
15446                 fp = &sc->fp[result];
15447                 bxe_dump_tx_chain(fp, 0, TOTAL_TX_BD);
15448         }
15449
15450         return (error);
15451 }
15452
15453 /*
15454  * Provides a sysctl interface to allow reading arbitrary registers in the
15455  * device.  DO NOT ENABLE ON PRODUCTION SYSTEMS!
15456  *
15457  * Returns:
15458  *   0 for success, positive value for failure.
15459  */
15460 static int
15461 bxe_sysctl_reg_read(SYSCTL_HANDLER_ARGS)
15462 {
15463         struct bxe_softc *sc;
15464         uint32_t result, val;
15465         int error;
15466
15467         sc = (struct bxe_softc *)arg1;
15468         result = -1;
15469         error = sysctl_handle_int(oidp, &result, 0, req);
15470         if (error || (req->newptr == NULL))
15471                 return (error);
15472
15473         val = REG_RD(sc, result);
15474         BXE_PRINTF("reg 0x%08X = 0x%08X\n", result, val);
15475
15476         return (error);
15477 }
15478
15479 /*
15480 * Provides a sysctl interface to allow generating a grcdump.
15481 *
15482 * Returns:
15483 *   0 for success, positive value for failure.
15484 */
15485 static int
15486 bxe_sysctl_grcdump(SYSCTL_HANDLER_ARGS)
15487 {
15488         struct bxe_softc *sc;
15489         int error, result;
15490
15491         sc = (struct bxe_softc *)arg1;
15492         result = -1;
15493         error = sysctl_handle_int(oidp, &result, 0, req);
15494         if (error || !req->newptr)
15495                 return (error);
15496
15497         if (result == 1) {
15498                 /* Generate a grcdump and log the contents.*/
15499                 bxe_grcdump(sc, 1);
15500         } else {
15501                 /* Generate a grcdump and don't log the contents. */
15502                 bxe_grcdump(sc, 0);
15503         }
15504
15505         return (error);
15506 }
15507
15508 /*
15509  * Provides a sysctl interface to forcing the driver to dump state and
15510  * enter the debugger.  DO NOT ENABLE ON PRODUCTION SYSTEMS!
15511  *
15512  * Returns:
15513  *   0 for success, positive value for failure.
15514  */
15515 static int
15516 bxe_sysctl_breakpoint(SYSCTL_HANDLER_ARGS)
15517 {
15518         struct bxe_softc *sc;
15519         int error, result;
15520
15521         result = -1;
15522         error = sysctl_handle_int(oidp, &result, 0, req);
15523         if (error || !req->newptr)
15524                 return (error);
15525
15526         if (result == 1) {
15527                 sc = (struct bxe_softc *)arg1;
15528                 bxe_breakpoint(sc);
15529         }
15530
15531         return (error);
15532 }
15533 #endif
15534
15535 /*
15536  * Adds any sysctl parameters for tuning or debugging purposes.
15537  *
15538  * Returns:
15539  *   None.
15540  */
15541 static void
15542 bxe_add_sysctls(struct bxe_softc *sc)
15543 {
15544         struct sysctl_ctx_list *ctx =
15545             device_get_sysctl_ctx(sc->dev);
15546         struct sysctl_oid_list *children =
15547             SYSCTL_CHILDREN(device_get_sysctl_tree(sc->dev));
15548         struct bxe_port_stats *estats = &sc->eth_stats;
15549
15550         SYSCTL_ADD_UINT(ctx, children, OID_AUTO,
15551             "estats_total_bytes_received_hi",
15552             CTLFLAG_RD, &estats->total_bytes_received_hi,
15553             0, "Total bytes received (hi)");
15554
15555         SYSCTL_ADD_UINT(ctx, children, OID_AUTO,
15556             "estats_total_bytes_received_lo",
15557             CTLFLAG_RD, &estats->total_bytes_received_lo,
15558             0, "Total bytes received (lo)");
15559
15560         SYSCTL_ADD_UINT(ctx, children, OID_AUTO,
15561            "estats_valid_bytes_received_hi",
15562            CTLFLAG_RD, &estats->valid_bytes_received_hi,
15563            0, "Valid bytes received (hi)");
15564
15565         SYSCTL_ADD_UINT(ctx, children, OID_AUTO,
15566             "estats_valid_bytes_received_lo",
15567             CTLFLAG_RD, &estats->valid_bytes_received_lo,
15568             0, "Valid bytes received (lo)");
15569
15570         SYSCTL_ADD_UINT(ctx, children, OID_AUTO,
15571             "estats_total_unicast_packets_received_hi",
15572             CTLFLAG_RD, &estats->total_unicast_packets_received_hi,
15573             0, "Total unicast packets received (hi)");
15574
15575         SYSCTL_ADD_UINT(ctx, children, OID_AUTO,
15576             "estats_total_unicast_packets_received_lo",
15577             CTLFLAG_RD, &estats->total_unicast_packets_received_lo,
15578             0, "Total unicast packets received (lo)");
15579
15580         SYSCTL_ADD_UINT(ctx, children, OID_AUTO,
15581             "estats_total_bytes_transmitted_hi",
15582             CTLFLAG_RD, &estats->total_bytes_transmitted_hi,
15583             0, "Total bytes transmitted (hi)");
15584
15585         SYSCTL_ADD_UINT(ctx, children, OID_AUTO,
15586             "estats_total_bytes_transmitted_lo",
15587             CTLFLAG_RD, &estats->total_bytes_transmitted_lo,
15588             0, "Total bytes transmitted (lo)");
15589
15590         SYSCTL_ADD_UINT(ctx, children, OID_AUTO,
15591             "estats_total_unicast_packets_transmitted_hi",
15592             CTLFLAG_RD, &estats->total_unicast_packets_transmitted_hi,
15593             0, "Total unicast packets transmitted (hi)");
15594
15595         SYSCTL_ADD_UINT(ctx, children, OID_AUTO,
15596             "estats_total_unicast_packets_transmitted_lo",
15597             CTLFLAG_RD, &estats->total_unicast_packets_transmitted_lo,
15598             0, "Total unicast packets transmitted (lo)");
15599
15600         SYSCTL_ADD_UINT(ctx, children, OID_AUTO,
15601             "estats_total_broadcast_packets_received_lo",
15602             CTLFLAG_RD, &estats->total_broadcast_packets_received_lo,
15603             0, "Total broadcast packets received (lo)");
15604
15605         SYSCTL_ADD_UINT(ctx, children, OID_AUTO,
15606             "estats_total_broadcast_packets_transmitted_lo",
15607             CTLFLAG_RD, &estats->total_broadcast_packets_transmitted_lo,
15608             0, "Total broadcast packets transmitted (lo)");
15609
15610         SYSCTL_ADD_UINT(ctx, children, OID_AUTO,
15611             "estats_total_multicast_packets_received_lo",
15612             CTLFLAG_RD, &estats->total_multicast_packets_received_lo,
15613             0, "Total multicast packets received (lo)");
15614
15615         SYSCTL_ADD_UINT(ctx, children, OID_AUTO,
15616             "estats_total_multicast_packets_transmitted_lo",
15617             CTLFLAG_RD, &estats->total_multicast_packets_transmitted_lo,
15618             0, "Total multicast packets transmitted (lo)");
15619
15620         SYSCTL_ADD_UINT(ctx, children, OID_AUTO,
15621             "tx_stat_etherstatspkts64octets_hi",
15622             CTLFLAG_RD, &estats->tx_stat_etherstatspkts64octets_hi,
15623             0, "Total 64 byte packets transmitted (hi)");
15624
15625         /* ToDo: Fix for 64 bit access. */
15626         SYSCTL_ADD_UINT(ctx, children, OID_AUTO,
15627             "tx_stat_etherstatspkts64octets_lo",
15628             CTLFLAG_RD, &estats->tx_stat_etherstatspkts64octets_lo,
15629             0, "Total 64 byte packets transmitted (lo)");
15630
15631         SYSCTL_ADD_UINT(ctx, children, OID_AUTO,
15632             "driver_xoff",
15633             CTLFLAG_RD, &estats->driver_xoff,
15634             0, "Driver transmit queue full count");
15635
15636         SYSCTL_ADD_ULONG(ctx, children, OID_AUTO,
15637             "tx_start_called_with_link_down",
15638             CTLFLAG_RD, &sc->tx_start_called_with_link_down,
15639             "TX start routine called while link down count");
15640
15641         SYSCTL_ADD_ULONG(ctx, children, OID_AUTO,
15642             "tx_start_called_with_queue_full",
15643             CTLFLAG_RD, &sc->tx_start_called_with_queue_full,
15644             "TX start routine called with queue full count");
15645
15646         /* ToDo: Add more statistics here. */
15647
15648 #ifdef BXE_DEBUG
15649         SYSCTL_ADD_INT(ctx, children, OID_AUTO, "bxe_debug",
15650             CTLFLAG_RW, &bxe_debug, 0,
15651             "Debug message level flag");
15652 #endif
15653
15654         do {
15655 #define QUEUE_NAME_LEN 32
15656                 char namebuf[QUEUE_NAME_LEN];
15657                 struct sysctl_oid *queue_node;
15658                 struct sysctl_oid_list *queue_list;
15659
15660                 for (int i = 0; i < sc->num_queues; i++) {
15661                         struct bxe_fastpath *fp = &sc->fp[i];
15662                         snprintf(namebuf, QUEUE_NAME_LEN, "fp[%02d]", i);
15663
15664                         queue_node = SYSCTL_ADD_NODE(ctx, children, OID_AUTO,
15665                             namebuf, CTLFLAG_RD, NULL, "Queue Name");
15666                         queue_list = SYSCTL_CHILDREN(queue_node);
15667
15668                         /*
15669                          * Receive related fastpath statistics.*
15670                          */
15671                         SYSCTL_ADD_ULONG(ctx, queue_list, OID_AUTO,
15672                             "rx_pkts",
15673                             CTLFLAG_RD, &fp->rx_pkts,
15674                             "Received packets");
15675
15676                         SYSCTL_ADD_ULONG(ctx, queue_list, OID_AUTO,
15677                             "rx_tpa_pkts",
15678                             CTLFLAG_RD, &fp->rx_tpa_pkts,
15679                             "Received TPA packets");
15680
15681                         SYSCTL_ADD_ULONG(ctx, queue_list, OID_AUTO,
15682                             "rx_null_cqe_flags",
15683                             CTLFLAG_RD, &fp->rx_null_cqe_flags,
15684                             "CQEs with NULL flags count");
15685
15686                         SYSCTL_ADD_ULONG(ctx, queue_list, OID_AUTO,
15687                             "rx_soft_errors",
15688                             CTLFLAG_RD, &fp->rx_soft_errors,
15689                             "Received frames dropped by driver count");
15690
15691                         /*
15692                          * Transmit related fastpath statistics.*
15693                          */
15694                         SYSCTL_ADD_ULONG(ctx, queue_list, OID_AUTO,
15695                             "tx_pkts",
15696                             CTLFLAG_RD, &fp->tx_pkts,
15697                             "Transmitted packets");
15698
15699                         SYSCTL_ADD_ULONG(ctx, queue_list, OID_AUTO,
15700                             "tx_soft_errors",
15701                             CTLFLAG_RD, &fp->tx_soft_errors,
15702                             "Transmit frames dropped by driver count");
15703
15704                         SYSCTL_ADD_ULONG(ctx, queue_list, OID_AUTO,
15705                             "tx_offload_frames_csum_ip",
15706                             CTLFLAG_RD, &fp->tx_offload_frames_csum_ip,
15707                             "IP checksum offload frame count");
15708
15709                         SYSCTL_ADD_ULONG(ctx, queue_list, OID_AUTO,
15710                             "tx_offload_frames_csum_tcp",
15711                             CTLFLAG_RD, &fp->tx_offload_frames_csum_tcp,
15712                             "TCP checksum offload frame count");
15713
15714                         SYSCTL_ADD_ULONG(ctx, queue_list, OID_AUTO,
15715                             "tx_offload_frames_csum_udp",
15716                             CTLFLAG_RD, &fp->tx_offload_frames_csum_udp,
15717                             "UDP checksum offload frame count");
15718
15719                         SYSCTL_ADD_ULONG(ctx, queue_list, OID_AUTO,
15720                             "tx_offload_frames_tso",
15721                             CTLFLAG_RD, &fp->tx_offload_frames_tso,
15722                             "TSO offload frame count");
15723
15724                         SYSCTL_ADD_ULONG(ctx, queue_list, OID_AUTO,
15725                             "tx_header_splits",
15726                             CTLFLAG_RD, &fp->tx_header_splits,
15727                             "TSO frame header/data split count");
15728
15729                         SYSCTL_ADD_ULONG(ctx, queue_list, OID_AUTO,
15730                             "tx_encap_failures",
15731                             CTLFLAG_RD, &fp->tx_encap_failures,
15732                             "TX encapsulation failure count");
15733
15734                         SYSCTL_ADD_ULONG(ctx, queue_list, OID_AUTO,
15735                             "tx_hw_queue_full",
15736                             CTLFLAG_RD, &fp->tx_hw_queue_full,
15737                             "TX H/W queue too full to add a frame count");
15738
15739                         SYSCTL_ADD_ULONG(ctx, queue_list, OID_AUTO,
15740                             "tx_hw_max_queue_depth",
15741                             CTLFLAG_RD, &fp->tx_hw_max_queue_depth,
15742                             "TX H/W maximum queue depth count");
15743
15744                         SYSCTL_ADD_ULONG(ctx, queue_list, OID_AUTO,
15745                             "tx_dma_mapping_failure",
15746                             CTLFLAG_RD, &fp->tx_dma_mapping_failure,
15747                             "TX DMA mapping failure");
15748
15749                         SYSCTL_ADD_INT(ctx, queue_list, OID_AUTO,
15750                             "tx_max_drbr_queue_depth",
15751                             CTLFLAG_RD, &fp->tx_max_drbr_queue_depth,
15752                             0, "TX S/W queue maximum depth");
15753
15754                         SYSCTL_ADD_ULONG(ctx, queue_list, OID_AUTO,
15755                             "tx_window_violation_std",
15756                             CTLFLAG_RD, &fp->tx_window_violation_std,
15757                             "Standard frame TX BD window violation count");
15758
15759                         SYSCTL_ADD_ULONG(ctx, queue_list, OID_AUTO,
15760                             "tx_window_violation_tso",
15761                             CTLFLAG_RD, &fp->tx_window_violation_tso,
15762                             "TSO frame TX BD window violation count");
15763
15764                         SYSCTL_ADD_ULONG(ctx, queue_list, OID_AUTO,
15765                             "tx_unsupported_tso_request_ipv6",
15766                             CTLFLAG_RD, &fp->tx_unsupported_tso_request_ipv6,
15767                             "TSO frames with unsupported IPv6 protocol count");
15768
15769                         SYSCTL_ADD_ULONG(ctx, queue_list, OID_AUTO,
15770                             "tx_unsupported_tso_request_not_tcp",
15771                             CTLFLAG_RD, &fp->tx_unsupported_tso_request_not_tcp,
15772                             "TSO frames with unsupported protocol count");
15773
15774                         SYSCTL_ADD_ULONG(ctx, queue_list, OID_AUTO,
15775                             "tx_chain_lost_mbuf",
15776                             CTLFLAG_RD, &fp->tx_chain_lost_mbuf,
15777                             "Mbufs lost on TX chain count");
15778
15779                         SYSCTL_ADD_ULONG(ctx, queue_list, OID_AUTO,
15780                             "tx_frame_deferred",
15781                             CTLFLAG_RD, &fp->tx_frame_deferred,
15782                             "TX frame deferred from H/W queue to S/W queue count");
15783
15784                         SYSCTL_ADD_ULONG(ctx, queue_list, OID_AUTO,
15785                             "tx_queue_xoff",
15786                             CTLFLAG_RD, &fp->tx_queue_xoff,
15787                             "TX queue full count");
15788
15789                         /*
15790                          * Memory related fastpath statistics.*
15791                          */
15792                         SYSCTL_ADD_ULONG(ctx, queue_list, OID_AUTO,
15793                             "mbuf_rx_bd_alloc_failed",
15794                             CTLFLAG_RD, &fp->mbuf_rx_bd_alloc_failed,
15795                             "RX BD mbuf allocation failure count");
15796
15797                         SYSCTL_ADD_ULONG(ctx, queue_list, OID_AUTO,
15798                             "mbuf_rx_bd_mapping_failed",
15799                             CTLFLAG_RD, &fp->mbuf_rx_bd_mapping_failed,
15800                             "RX BD mbuf mapping failure count");
15801
15802                         SYSCTL_ADD_ULONG(ctx, queue_list, OID_AUTO,
15803                             "mbuf_tpa_alloc_failed",
15804                             CTLFLAG_RD, &fp->mbuf_tpa_alloc_failed,
15805                             "TPA mbuf allocation failure count");
15806
15807                         SYSCTL_ADD_ULONG(ctx, queue_list, OID_AUTO,
15808                             "mbuf_tpa_mapping_failed",
15809                             CTLFLAG_RD, &fp->mbuf_tpa_mapping_failed,
15810                             "TPA mbuf mapping failure count");
15811
15812                         SYSCTL_ADD_ULONG(ctx, queue_list, OID_AUTO,
15813                             "mbuf_sge_alloc_failed",
15814                             CTLFLAG_RD, &fp->mbuf_sge_alloc_failed,
15815                             "SGE mbuf allocation failure count");
15816
15817                         SYSCTL_ADD_ULONG(ctx, queue_list, OID_AUTO,
15818                             "mbuf_sge_mapping_failed",
15819                             CTLFLAG_RD, &fp->mbuf_sge_mapping_failed,
15820                             "SGE mbuf mapping failure count");
15821
15822                         SYSCTL_ADD_ULONG(ctx, queue_list, OID_AUTO,
15823                             "mbuf_defrag_attempts",
15824                             CTLFLAG_RD, &fp->mbuf_defrag_attempts,
15825                             "Mbuf defrag attempt count");
15826
15827                         SYSCTL_ADD_ULONG(ctx, queue_list, OID_AUTO,
15828                             "mbuf_defrag_failures",
15829                             CTLFLAG_RD, &fp->mbuf_defrag_failures,
15830                             "Mbuf defrag failure count");
15831                 }
15832         } while (0);
15833
15834
15835 #ifdef BXE_DEBUG
15836         SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "driver_state",
15837             CTLTYPE_INT | CTLFLAG_RW, (void *)sc, 0,
15838             bxe_sysctl_driver_state, "I", "Drive state information");
15839
15840         SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "hw_state",
15841             CTLTYPE_INT | CTLFLAG_RW, (void *)sc, 0,
15842             bxe_sysctl_hw_state, "I", "Hardware state information");
15843
15844         SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "dump_fw",
15845             CTLTYPE_INT | CTLFLAG_RW, (void *)sc, 0,
15846             bxe_sysctl_dump_fw, "I", "Dump MCP firmware");
15847
15848         SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "dump_rx_bd_chain",
15849             CTLTYPE_INT | CTLFLAG_RW, (void *)sc, 0,
15850             bxe_sysctl_dump_rx_bd_chain, "I", "Dump rx_bd chain");
15851
15852         SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "dump_rx_cq_chain",
15853             CTLTYPE_INT | CTLFLAG_RW, (void *)sc, 0,
15854             bxe_sysctl_dump_rx_cq_chain, "I", "Dump cqe chain");
15855
15856         SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "dump_tx_chain",
15857             CTLTYPE_INT | CTLFLAG_RW, (void *)sc, 0,
15858             bxe_sysctl_dump_tx_chain, "I", "Dump tx_bd chain");
15859
15860         /*
15861          * Generates a GRCdump (run sysctl dev.bxe.0.grcdump=0
15862          * before accessing buffer below).
15863          */
15864         SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "grcdump",
15865             CTLTYPE_INT | CTLFLAG_RW, (void *)sc, 0, bxe_sysctl_grcdump,
15866             "I", "Initiate a grcdump operation");
15867
15868         /*
15869          * Hidden sysctl.
15870          *  Use "sysctl -b dev.bxe.0.grcdump_buffer > buf.bin".
15871          */
15872         SYSCTL_ADD_OPAQUE(ctx, children, OID_AUTO, "grcdump_buffer",
15873             CTLFLAG_RD | CTLFLAG_SKIP, sc->grcdump_buffer,
15874             BXE_GRCDUMP_BUF_SIZE, "IU", "Access grcdump buffer");
15875
15876         SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "breakpoint",
15877             CTLTYPE_INT | CTLFLAG_RW, (void *)sc, 0,
15878             bxe_sysctl_breakpoint, "I", "Driver breakpoint");
15879
15880         SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "reg_read",
15881             CTLTYPE_INT | CTLFLAG_RW, (void *)sc, 0,
15882             bxe_sysctl_reg_read, "I", "Register read");
15883
15884 #endif /* BXE_DEBUG */
15885 }
15886
15887 /*
15888  * BXE Debug Routines
15889  */
15890 #ifdef BXE_DEBUG
15891 /*
15892  * Writes out the header for the debug dump buffer.
15893  *
15894  * Returns:
15895  *       None.
15896  *
15897  * Modifies:
15898  *   index
15899  */
15900 static void
15901 bxe_dump_debug_header(struct bxe_softc *sc, uint32_t *index)
15902 {
15903         struct hd_param hd_param_cu = {0};
15904         uint32_t *buf;
15905
15906         buf = sc->grcdump_buffer;
15907         if (CHIP_IS_E1H(sc))
15908                 hd_param_cu = hd_param_e1h;
15909         else
15910                 hd_param_cu = hd_param_e1;
15911
15912         buf[(*index)++] = hd_param_cu.time_stamp;
15913         buf[(*index)++] = hd_param_cu.diag_ver;
15914         buf[(*index)++] = hd_param_cu.grc_dump_ver;
15915
15916         buf[(*index)++] = REG_RD_IND(sc, XSTORM_WAITP_ADDRESS);
15917         buf[(*index)++] = REG_RD_IND(sc, TSTORM_WAITP_ADDRESS);
15918         buf[(*index)++] = REG_RD_IND(sc, USTORM_WAITP_ADDRESS);
15919         buf[(*index)++] = REG_RD_IND(sc, CSTORM_WAITP_ADDRESS);
15920
15921         /* The size of the header is stored at the first DWORD. */
15922         buf[0] = (*index) - 1;
15923 }
15924
15925
15926 /*
15927  * Writes to the controller to prepare it for a dump.
15928  *
15929  * Returns:
15930  *       None.
15931  *
15932  * Modifies:
15933  *   None.
15934  */
15935 static void
15936 bxe_dump_debug_writes(struct bxe_softc *sc)
15937 {
15938         uint32_t write_val;
15939
15940         write_val = 1;
15941         /* Halt the STORMs to get a consistent device state. */
15942         REG_WR_IND(sc, XSTORM_WAITP_ADDRESS, write_val);
15943         REG_WR_IND(sc, TSTORM_WAITP_ADDRESS, write_val);
15944         REG_WR_IND(sc, USTORM_WAITP_ADDRESS, write_val);
15945         REG_WR_IND(sc, CSTORM_WAITP_ADDRESS, write_val);
15946
15947         if (CHIP_IS_E1H(sc))
15948                 REG_WR_IND(sc, TSTORM_CAM_MODE, write_val);
15949 }
15950
15951
15952 /*
15953  * Cycles through the required register reads and dumps them
15954  * to the debug buffer.
15955  *
15956  * Returns:
15957  *       None.
15958  *
15959  * Modifies:
15960  *   index
15961  */
15962 static void
15963 bxe_dump_debug_reg_read(struct bxe_softc *sc, uint32_t *index)
15964 {
15965         preg_addr preg_addrs;
15966         uint32_t regs_count, *buf;
15967         uint32_t i, reg_addrs_index;
15968
15969         buf = sc->grcdump_buffer;
15970         preg_addrs = NULL;
15971
15972         /* Read different registers for different controllers. */
15973         if (CHIP_IS_E1H(sc)) {
15974                 regs_count = regs_count_e1h;
15975                 preg_addrs = &reg_addrs_e1h[0];
15976         } else {
15977                 regs_count = regs_count_e1;
15978                 preg_addrs = &reg_addrs_e1[0];
15979         }
15980
15981         /* ToDo: Add a buffer size check. */
15982         for (reg_addrs_index = 0; reg_addrs_index < regs_count;
15983             reg_addrs_index++) {
15984                 for (i = 0; i < preg_addrs[reg_addrs_index].size; i++) {
15985                         buf[(*index)++] = REG_RD_IND(sc,
15986                             preg_addrs[reg_addrs_index].addr + (i * 4));
15987                 }
15988         }
15989 }
15990
15991 /*
15992  * Cycles through the required wide register reads and dumps them
15993  * to the debug buffer.
15994  *
15995  * Returns:
15996  *   None.
15997  */
15998 static void
15999 bxe_dump_debug_reg_wread(struct bxe_softc *sc, uint32_t *index)
16000 {
16001         pwreg_addr pwreg_addrs;
16002         uint32_t reg_addrs_index, reg_add_read, reg_add_count;
16003         uint32_t *buf, cam_index, wregs_count;
16004
16005         buf = sc->grcdump_buffer;
16006         pwreg_addrs = NULL;
16007
16008         /* Read different registers for different controllers. */
16009         if (CHIP_IS_E1H(sc)) {
16010                 wregs_count = wregs_count_e1h;
16011                 pwreg_addrs = &wreg_addrs_e1h[0];
16012         } else {
16013                 wregs_count = wregs_count_e1;
16014                 pwreg_addrs = &wreg_addrs_e1[0];
16015         }
16016
16017         for (reg_addrs_index = 0; reg_addrs_index < wregs_count;
16018             reg_addrs_index++) {
16019                 reg_add_read = pwreg_addrs[reg_addrs_index].addr;
16020                 for (reg_add_count = 0; reg_add_count <
16021                     pwreg_addrs[reg_addrs_index].size; reg_add_count++) {
16022                         buf[(*index)++] = REG_RD_IND(sc, reg_add_read);
16023                         reg_add_read += sizeof(uint32_t);
16024
16025                         for (cam_index = 0; cam_index <
16026                             pwreg_addrs[reg_addrs_index].const_regs_count;
16027                             cam_index++)
16028                                 buf[(*index)++] = REG_RD_IND(sc,
16029                                     pwreg_addrs[reg_addrs_index].const_regs[cam_index]);
16030                 }
16031         }
16032 }
16033
16034 /*
16035  * Performs a debug dump for offline diagnostics.
16036  *
16037  * Note that when this routine is called the STORM
16038  * processors will be stopped in order to create a
16039  * cohesive dump.  The controller will need to be
16040  * reset before the device can begin passing traffic
16041  * again.
16042  *
16043  * Returns:
16044  *   None.
16045  */
16046 static void
16047 bxe_grcdump(struct bxe_softc *sc, int log)
16048 {
16049         uint32_t *buf, i, index;
16050
16051         index = 1;
16052         buf = sc->grcdump_buffer;
16053         if (buf != NULL) {
16054
16055                 /* Write the header and regsiters contents to the dump buffer. */
16056                 bxe_dump_debug_header(sc, &index);
16057                 bxe_dump_debug_writes(sc);
16058                 bxe_dump_debug_reg_read(sc,&index);
16059                 bxe_dump_debug_reg_wread(sc, &index);
16060
16061                 /* Print the results to the system log is necessary. */
16062                 if (log) {
16063                         BXE_PRINTF(
16064                             "-----------------------------"
16065                             "    grcdump   "
16066                             "-----------------------------\n");
16067                         BXE_PRINTF("Buffer length = 0x%08X bytes\n", index * 4);
16068
16069                         for (i = 0; i < index; i += 8) {
16070                                 BXE_PRINTF(
16071                                     "0x%08X - 0x%08X 0x%08X 0x%08X 0x%08X "
16072                                     "0x%08X 0x%08X 0x%08X 0x%08X\n", i * 4,
16073                                     buf[i + 0], buf[i + 1], buf[i + 2],
16074                                     buf[i + 3], buf[i + 4], buf[i + 5],
16075                                     buf[i + 6], buf[i + 7]);
16076                         }
16077
16078                         BXE_PRINTF(
16079                             "-----------------------------"
16080                             "--------------"
16081                             "-----------------------------\n");
16082                 }
16083         } else {
16084                 BXE_PRINTF("No grcdump buffer allocated!\n");
16085         }
16086 }
16087
16088 /*
16089  * Check that an Etherent frame is valid and prints out debug info if it's
16090  * not.
16091  *
16092  * Returns:
16093  *   Nothing.
16094  */
16095 static __noinline
16096 void bxe_validate_rx_packet(struct bxe_fastpath *fp, uint16_t comp_cons,
16097     union eth_rx_cqe *cqe, struct mbuf *m)
16098 {
16099         struct bxe_softc *sc;
16100         int error;
16101
16102         sc = fp->sc;
16103
16104         /* Check that the mbuf is sane. */
16105         error = m_sanity(m, FALSE);
16106         if (error != 1 || ((m->m_len < ETHER_HDR_LEN) |
16107             (m->m_len > ETH_MAX_JUMBO_PACKET_SIZE + ETH_OVREHEAD))) {
16108                 m_print(m, 128);
16109                 bxe_dump_enet(sc, m);
16110                 bxe_dump_cqe(fp, comp_cons, cqe);
16111                 /* Make sure the packet has a valid length. */
16112         }
16113 }
16114
16115 /*
16116  * Prints out Ethernet frame information from an mbuf.
16117  *
16118  * Partially decode an Ethernet frame to look at some important headers.
16119  *
16120  * Returns:
16121  *   Nothing.
16122  */
16123 static __noinline
16124 void bxe_dump_enet(struct bxe_softc *sc, struct mbuf *m)
16125 {
16126         struct ether_vlan_header *eh;
16127         uint16_t etype;
16128         int e_hlen;
16129         struct ip *ip;
16130         struct tcphdr *th;
16131         struct udphdr *uh;
16132         struct arphdr *ah;
16133
16134         BXE_PRINTF(
16135             "-----------------------------"
16136             " Frame Decode "
16137             "-----------------------------\n");
16138
16139         eh = mtod(m, struct ether_vlan_header *);
16140
16141         /* Handle VLAN encapsulation if present. */
16142         if (eh->evl_encap_proto == htons(ETHERTYPE_VLAN)) {
16143                 etype = ntohs(eh->evl_proto);
16144                 e_hlen = ETHER_HDR_LEN + ETHER_VLAN_ENCAP_LEN;
16145         } else {
16146                 etype = ntohs(eh->evl_encap_proto);
16147                 e_hlen = ETHER_HDR_LEN;
16148         }
16149
16150         BXE_PRINTF("enet: dest = %6D, src = %6D, type = 0x%04X, e_hlen = %d\n",
16151             eh->evl_dhost, ":", eh->evl_shost, ":", etype, e_hlen);
16152
16153         switch (etype) {
16154         case ETHERTYPE_IP:
16155                 ip = (struct ip *)(m->m_data + e_hlen);
16156                 BXE_PRINTF(
16157                     "--ip: dest = 0x%08X , src = 0x%08X, "
16158                     "ip_hlen = %d bytes, len = %d bytes, protocol = 0x%02X, "
16159                     "ip_id = 0x%04X, csum = 0x%04X\n",
16160                     ntohl(ip->ip_dst.s_addr), ntohl(ip->ip_src.s_addr),
16161                     (ip->ip_hl << 2), ntohs(ip->ip_len), ip->ip_p,
16162                     ntohs(ip->ip_id), ntohs(ip->ip_sum));
16163
16164                 switch (ip->ip_p) {
16165                 case IPPROTO_TCP:
16166                         th = (struct tcphdr *)((caddr_t)ip + (ip->ip_hl << 2));
16167                         BXE_PRINTF(
16168                             "-tcp: dest = %d, src = %d, tcp_hlen = %d "
16169                             "bytes, flags = 0x%b, csum = 0x%04X\n",
16170                             ntohs(th->th_dport), ntohs(th->th_sport),
16171                             (th->th_off << 2), th->th_flags,
16172                             "\20\10CWR\07ECE\06URG\05ACK\04PSH\03RST\02SYN\01FIN",
16173                             ntohs(th->th_sum));
16174                         break;
16175                 case IPPROTO_UDP:
16176                         uh = (struct udphdr *)((caddr_t)ip + (ip->ip_hl << 2));
16177                         BXE_PRINTF(
16178                             "-udp: dest = %d, src = %d, udp_hlen = %d "
16179                             "bytes, len = %d bytes, csum = 0x%04X\n",
16180                             ntohs(uh->uh_dport), ntohs(uh->uh_sport),
16181                             (int)sizeof(struct udphdr), ntohs(uh->uh_ulen),
16182                             ntohs(uh->uh_sum));
16183                         break;
16184                 case IPPROTO_ICMP:
16185                         BXE_PRINTF("icmp:\n");
16186                         break;
16187                 default:
16188                         BXE_PRINTF("----: Other IP protocol.\n");
16189                 }
16190                 break;
16191         case ETHERTYPE_IPV6:
16192                 /* ToDo: Add IPv6 support. */
16193                 BXE_PRINTF("IPv6 not supported!.\n");
16194                 break;
16195         case ETHERTYPE_ARP:
16196                 BXE_PRINTF("-arp: ");
16197                 ah = (struct arphdr *) (m->m_data + e_hlen);
16198                 switch (ntohs(ah->ar_op)) {
16199                 case ARPOP_REVREQUEST:
16200                         printf("reverse ARP request\n");
16201                         break;
16202                 case ARPOP_REVREPLY:
16203                         printf("reverse ARP reply\n");
16204                         break;
16205                 case ARPOP_REQUEST:
16206                         printf("ARP request\n");
16207                         break;
16208                 case ARPOP_REPLY:
16209                         printf("ARP reply\n");
16210                         break;
16211                 default:
16212                         printf("other ARP operation\n");
16213                 }
16214                 break;
16215         default:
16216                 BXE_PRINTF("----: Other protocol.\n");
16217         }
16218
16219         BXE_PRINTF(
16220            "-----------------------------"
16221            "--------------"
16222            "-----------------------------\n");
16223 }
16224
16225 #if 0
16226 static void
16227 bxe_dump_mbuf_data(struct mbuf *m, int len)
16228 {
16229         uint8_t *ptr;
16230         int i;
16231
16232         ptr = mtod(m, uint8_t *);
16233         printf("\nmbuf->m_data:");
16234         printf("\n0x");
16235         for (i = 0; i < len; i++){
16236                 if (i != 0 && i % 40 == 0)
16237                         printf("\n0x");
16238                 else if (i != 0 && i % 6 == 0)
16239                         printf(" 0x");
16240                 printf("%02x", *ptr++);
16241         }
16242         printf("\n\n");
16243 }
16244 #endif
16245
16246
16247 /*
16248  * Prints out information about an mbuf.
16249  *
16250  * Returns:
16251  *   Nothing.
16252  */
16253 static __noinline
16254 void bxe_dump_mbuf(struct bxe_softc *sc, struct mbuf *m)
16255 {
16256         if (m == NULL) {
16257                 BXE_PRINTF("mbuf: null pointer\n");
16258                 return;
16259         }
16260
16261         while (m) {
16262                 BXE_PRINTF("mbuf: %p, m_len = %d, m_flags = 0x%b, "
16263                     "m_data = %p\n", m, m->m_len, m->m_flags,
16264                     "\20\1M_EXT\2M_PKTHDR\3M_EOR\4M_RDONLY", m->m_data);
16265
16266                 if (m->m_flags & M_PKTHDR) {
16267                          BXE_PRINTF("- m_pkthdr: len = %d, flags = 0x%b, "
16268                             "csum_flags = %b\n", m->m_pkthdr.len,
16269                             m->m_flags, "\20\12M_BCAST\13M_MCAST\14M_FRAG"
16270                             "\15M_FIRSTFRAG\16M_LASTFRAG\21M_VLANTAG"
16271                             "\22M_PROMISC\23M_NOFREE",
16272                             m->m_pkthdr.csum_flags,
16273                             "\20\1CSUM_IP\2CSUM_TCP\3CSUM_UDP\4CSUM_IP_FRAGS"
16274                             "\5CSUM_FRAGMENT\6CSUM_TSO\11CSUM_IP_CHECKED"
16275                             "\12CSUM_IP_VALID\13CSUM_DATA_VALID"
16276                             "\14CSUM_PSEUDO_HDR");
16277                 }
16278
16279                 if (m->m_flags & M_EXT) {
16280                         BXE_PRINTF("- m_ext: %p, ext_size = %d, type = ",
16281                             m->m_ext.ext_buf, m->m_ext.ext_size);
16282                         switch (m->m_ext.ext_type) {
16283                         case EXT_CLUSTER:
16284                                 printf("EXT_CLUSTER\n"); break;
16285                         case EXT_SFBUF:
16286                                 printf("EXT_SFBUF\n"); break;
16287                         case EXT_JUMBO9:
16288                                 printf("EXT_JUMBO9\n"); break;
16289                         case EXT_JUMBO16:
16290                                 printf("EXT_JUMBO16\n"); break;
16291                         case EXT_PACKET:
16292                                 printf("EXT_PACKET\n"); break;
16293                         case EXT_MBUF:
16294                                 printf("EXT_MBUF\n"); break;
16295                         case EXT_NET_DRV:
16296                                 printf("EXT_NET_DRV\n"); break;
16297                         case EXT_MOD_TYPE:
16298                                 printf("EXT_MOD_TYPE\n"); break;
16299                         case EXT_DISPOSABLE:
16300                                 printf("EXT_DISPOSABLE\n"); break;
16301                         case EXT_EXTREF:
16302                                 printf("EXT_EXTREF\n"); break;
16303                         default:
16304                                 printf("UNKNOWN\n");
16305                         }
16306                 }
16307
16308                 m = m->m_next;
16309         }
16310 }
16311
16312 /*
16313  * Prints out information about an rx_bd.
16314  *
16315  * Returns:
16316  *   Nothing.
16317  */
16318 static __noinline
16319 void bxe_dump_rxbd(struct bxe_fastpath *fp, int idx,
16320     struct eth_rx_bd *rx_bd)
16321 {
16322         struct bxe_softc *sc;
16323
16324         sc = fp->sc;
16325
16326         /* Check if index out of range. */
16327         if (idx > MAX_RX_BD) {
16328                 BXE_PRINTF("fp[%02d].rx_bd[0x%04X] XX: Invalid rx_bd index!\n",
16329                     fp->index, idx);
16330         } else if ((idx & RX_BD_PER_PAGE_MASK) >= USABLE_RX_BD_PER_PAGE) {
16331                 /* RX Chain page pointer. */
16332                 BXE_PRINTF("fp[%02d].rx_bd[0x%04X] NP: haddr=0x%08X:%08X\n",
16333                     fp->index, idx, rx_bd->addr_hi, rx_bd->addr_lo);
16334         } else {
16335                 BXE_PRINTF("fp[%02d].rx_bd[0x%04X] RX: haddr=0x%08X:%08X\n",
16336                     fp->index, idx, rx_bd->addr_hi, rx_bd->addr_lo);
16337         }
16338 }
16339
16340 /*
16341  * Prints out a completion queue entry.
16342  *
16343  * Returns:
16344  *   Nothing.
16345  */
16346 static __noinline
16347 void bxe_dump_cqe(struct bxe_fastpath *fp, int idx,
16348     union eth_rx_cqe *cqe)
16349 {
16350         struct bxe_softc *sc;
16351
16352         sc = fp->sc;
16353
16354         if (idx > MAX_RCQ_ENTRIES) {
16355                 /* Index out of range. */
16356                 BXE_PRINTF("fp[%02d].rx_cqe[0x%04X]: Invalid rx_cqe index!\n",
16357                     fp->index, idx);
16358         } else if ((idx & USABLE_RCQ_ENTRIES_PER_PAGE) ==
16359             USABLE_RCQ_ENTRIES_PER_PAGE) {
16360                 /* CQE next page pointer. */
16361                 BXE_PRINTF("fp[%02d].rx_cqe[0x%04X] NP: haddr=0x%08X:%08X\n",
16362                     fp->index, idx,
16363                     le32toh(cqe->next_page_cqe.addr_hi),
16364                     le32toh(cqe->next_page_cqe.addr_lo));
16365         } else {
16366                 /* Normal CQE. */
16367                 BXE_PRINTF("fp[%02d].rx_cqe[0x%04X] CQ: error_flags=0x%b, "
16368                     "pkt_len=0x%04X, status_flags=0x%02X, vlan=0x%04X "
16369                     "rss_hash=0x%08X\n", fp->index, idx,
16370                     cqe->fast_path_cqe.type_error_flags,
16371                     BXE_ETH_FAST_PATH_RX_CQE_ERROR_FLAGS_PRINTFB,
16372                     le16toh(cqe->fast_path_cqe.pkt_len),
16373                     cqe->fast_path_cqe.status_flags,
16374                     le16toh(cqe->fast_path_cqe.vlan_tag),
16375                     le32toh(cqe->fast_path_cqe.rss_hash_result));
16376         }
16377 }
16378
16379 /*
16380  * Prints out information about a TX parsing BD.
16381  *
16382  * Returns:
16383  *   Nothing.
16384  */
16385 static __noinline
16386 void bxe_dump_tx_parsing_bd(struct bxe_fastpath *fp, int idx,
16387     struct eth_tx_parse_bd *p_bd)
16388 {
16389         struct bxe_softc *sc;
16390
16391         sc = fp->sc;
16392
16393         if (idx > MAX_TX_BD){
16394                 /* Index out of range. */
16395                 BXE_PRINTF("fp[%02d].tx_bd[0x%04X] XX: Invalid tx_bd index!\n",
16396                    fp->index, idx);
16397         } else {
16398                 BXE_PRINTF("fp[%02d]:tx_bd[0x%04X] PB: global_data=0x%b, "
16399                     "tcp_flags=0x%b, ip_hlen=%04d, total_hlen=%04d, "
16400                     "tcp_pseudo_csum=0x%04X, lso_mss=0x%04X, ip_id=0x%04X, "
16401                     "tcp_send_seq=0x%08X\n", fp->index, idx,
16402                     p_bd->global_data, BXE_ETH_TX_PARSE_BD_GLOBAL_DATA_PRINTFB,
16403                     p_bd->tcp_flags, BXE_ETH_TX_PARSE_BD_TCP_FLAGS_PRINTFB,
16404                     p_bd->ip_hlen, p_bd->total_hlen, p_bd->tcp_pseudo_csum,
16405                     p_bd->lso_mss, p_bd->ip_id, p_bd->tcp_send_seq);
16406         }
16407 }
16408
16409 /*
16410  * Prints out information about a tx_bd.
16411  *
16412  * Returns:
16413  *   Nothing.
16414  */
16415 static __noinline
16416 void bxe_dump_txbd(struct bxe_fastpath *fp, int idx,
16417     union eth_tx_bd_types *tx_bd)
16418 {
16419         struct bxe_softc *sc;
16420
16421         sc = fp->sc;
16422
16423         if (idx > MAX_TX_BD){
16424                 /* Index out of range. */
16425                 BXE_PRINTF("fp[%02d]:tx_bd[0x%04X] XX: Invalid tx_bd index!\n",
16426                     fp->index, idx);
16427         } else if ((idx & USABLE_TX_BD_PER_PAGE) == USABLE_TX_BD_PER_PAGE) {
16428                 /* TX next page BD. */
16429                 BXE_PRINTF("fp[%02d]:tx_bd[0x%04X] NP: haddr=0x%08X:%08X\n",
16430                     fp->index, idx,     tx_bd->next_bd.addr_hi,
16431                     tx_bd->next_bd.addr_lo);
16432         } else if ((tx_bd->start_bd.bd_flags.as_bitfield &
16433             ETH_TX_BD_FLAGS_START_BD) != 0) {
16434                 /* TX start BD. */
16435                 BXE_PRINTF("fp[%02d]:tx_bd[0x%04X] ST: haddr=0x%08X:%08X, "
16436                     "nbd=%02d, nbytes=%05d, vlan/idx=0x%04X, flags=0x%b, "
16437                     "gendata=0x%02X\n",
16438                     fp->index, idx, tx_bd->start_bd.addr_hi,
16439                     tx_bd->start_bd.addr_lo, tx_bd->start_bd.nbd,
16440                     tx_bd->start_bd.nbytes, tx_bd->start_bd.vlan,
16441                     tx_bd->start_bd.bd_flags.as_bitfield,
16442                     BXE_ETH_TX_BD_FLAGS_PRINTFB,
16443                     tx_bd->start_bd.general_data);
16444         } else {
16445                 /* Regular TX BD. */
16446                 BXE_PRINTF("fp[%02d]:tx_bd[0x%04X] TX: haddr=0x%08X:%08X, "
16447                     "total_pkt_bytes=%05d, nbytes=%05d\n", fp->index, idx,
16448                     tx_bd->reg_bd.addr_hi, tx_bd->reg_bd.addr_lo,
16449                     tx_bd->reg_bd.total_pkt_bytes, tx_bd->reg_bd.nbytes);
16450         }
16451 }
16452
16453
16454 /*
16455  * Prints out the transmit chain.
16456  *
16457  * Returns:
16458  *   Nothing.
16459  */
16460 static __noinline
16461 void bxe_dump_tx_chain(struct bxe_fastpath * fp, int tx_bd_prod, int count)
16462 {
16463         struct bxe_softc *sc;
16464         union eth_tx_bd_types *tx_bd;
16465         uint32_t val_hi, val_lo;
16466         int i, parsing_bd = 0;
16467
16468         sc = fp->sc;
16469
16470         /* First some info about the tx_bd chain structure. */
16471         BXE_PRINTF(
16472             "----------------------------"
16473             "  tx_bd chain "
16474             "----------------------------\n");
16475
16476         val_hi = U64_HI(fp->tx_dma.paddr);
16477         val_lo = U64_LO(fp->tx_dma.paddr);
16478         BXE_PRINTF(
16479             "0x%08X:%08X - (fp[%02d]->tx_dma.paddr) TX Chain physical address\n",
16480             val_hi, val_lo, fp->index);
16481         BXE_PRINTF(
16482             "page size      = 0x%08X, tx chain pages        = 0x%08X\n",
16483             (uint32_t)BCM_PAGE_SIZE, (uint32_t)NUM_TX_PAGES);
16484         BXE_PRINTF(
16485             "tx_bd per page = 0x%08X, usable tx_bd per page = 0x%08X\n",
16486             (uint32_t)TOTAL_TX_BD_PER_PAGE, (uint32_t)USABLE_TX_BD_PER_PAGE);
16487         BXE_PRINTF(
16488             "total tx_bd    = 0x%08X\n", (uint32_t)TOTAL_TX_BD);
16489
16490         BXE_PRINTF(
16491             "-----------------------------"
16492             "  tx_bd data  "
16493             "-----------------------------\n");
16494
16495         /* Now print out the tx_bd's themselves. */
16496         for (i = 0; i < count; i++) {
16497                 tx_bd = &fp->tx_chain[tx_bd_prod];
16498                 if (parsing_bd) {
16499                         struct eth_tx_parse_bd *p_bd;
16500                         p_bd = (struct eth_tx_parse_bd *)
16501                             &fp->tx_chain[tx_bd_prod].parse_bd;
16502                         bxe_dump_tx_parsing_bd(fp, tx_bd_prod, p_bd);
16503                         parsing_bd = 0;
16504                 } else {
16505                         bxe_dump_txbd(fp, tx_bd_prod, tx_bd);
16506                         if ((tx_bd->start_bd.bd_flags.as_bitfield &
16507                             ETH_TX_BD_FLAGS_START_BD) != 0)
16508                                 /*
16509                                  * There is always a parsing BD following the
16510                                  * tx_bd with the start bit set.
16511                                  */
16512                                 parsing_bd = 1;
16513                 }
16514                 /* Don't skip next page pointers. */
16515                    tx_bd_prod = ((tx_bd_prod + 1) & MAX_TX_BD);
16516         }
16517
16518         BXE_PRINTF(
16519             "-----------------------------"
16520             "--------------"
16521             "-----------------------------\n");
16522 }
16523
16524 /*
16525  * Prints out the receive completion queue chain.
16526  *
16527  * Returns:
16528  *   Nothing.
16529  */
16530 static __noinline
16531 void bxe_dump_rx_cq_chain(struct bxe_fastpath *fp, int rx_cq_prod, int count)
16532 {
16533         struct bxe_softc *sc;
16534         union eth_rx_cqe *cqe;
16535         int i;
16536
16537         sc = fp->sc;
16538
16539         /* First some info about the tx_bd chain structure. */
16540         BXE_PRINTF(
16541            "----------------------------"
16542            "   CQE  Chain   "
16543            "----------------------------\n");
16544
16545         BXE_PRINTF("fp[%02d]->rcq_dma.paddr = 0x%jX\n",
16546             fp->index, (uintmax_t) fp->rcq_dma.paddr);
16547
16548         BXE_PRINTF("page size       = 0x%08X, cq chain pages    "
16549             "     = 0x%08X\n",
16550             (uint32_t)BCM_PAGE_SIZE, (uint32_t) NUM_RCQ_PAGES);
16551
16552         BXE_PRINTF("cqe_bd per page = 0x%08X, usable cqe_bd per "
16553             "page = 0x%08X\n",
16554             (uint32_t) TOTAL_RCQ_ENTRIES_PER_PAGE,
16555             (uint32_t) USABLE_RCQ_ENTRIES_PER_PAGE);
16556
16557         BXE_PRINTF("total cqe_bd    = 0x%08X\n",(uint32_t) TOTAL_RCQ_ENTRIES);
16558
16559         /* Now the CQE entries themselves. */
16560         BXE_PRINTF(
16561             "----------------------------"
16562             "    CQE Data    "
16563             "----------------------------\n");
16564
16565         for (i = 0; i < count; i++) {
16566                 cqe = (union eth_rx_cqe *)&fp->rcq_chain[rx_cq_prod];
16567
16568                 bxe_dump_cqe(fp, rx_cq_prod, cqe);
16569
16570                 /* Don't skip next page pointers. */
16571                 rx_cq_prod = ((rx_cq_prod + 1) & MAX_RCQ_ENTRIES);
16572         }
16573
16574         BXE_PRINTF(
16575             "----------------------------"
16576             "--------------"
16577             "----------------------------\n");
16578 }
16579
16580 /*
16581  * Prints out the receive chain.
16582  *
16583  * Returns:
16584  *   Nothing.
16585  */
16586 static __noinline
16587 void bxe_dump_rx_bd_chain(struct bxe_fastpath *fp, int prod, int count)
16588 {
16589         struct bxe_softc *sc;
16590         struct eth_rx_bd *rx_bd;
16591         struct mbuf *m;
16592         int i;
16593
16594         sc = fp->sc;
16595
16596         /* First some info about the tx_bd chain structure. */
16597         BXE_PRINTF(
16598             "----------------------------"
16599             "  rx_bd  chain  "
16600             "----------------------------\n");
16601
16602         BXE_PRINTF(
16603             "----- RX_BD Chain -----\n");
16604
16605         BXE_PRINTF("fp[%02d]->rx_dma.paddr = 0x%jX\n",
16606             fp->index, (uintmax_t) fp->rx_dma.paddr);
16607
16608         BXE_PRINTF(
16609             "page size = 0x%08X, rx chain pages = 0x%08X\n",
16610             (uint32_t)BCM_PAGE_SIZE, (uint32_t)NUM_RX_PAGES);
16611
16612         BXE_PRINTF(
16613             "rx_bd per page = 0x%08X, usable rx_bd per page = 0x%08X\n",
16614             (uint32_t)TOTAL_RX_BD_PER_PAGE, (uint32_t)USABLE_RX_BD_PER_PAGE);
16615
16616         BXE_PRINTF(
16617             "total rx_bd = 0x%08X\n", (uint32_t)TOTAL_RX_BD);
16618
16619         /* Now the rx_bd entries themselves. */
16620         BXE_PRINTF(
16621             "----------------------------"
16622             "   rx_bd data   "
16623             "----------------------------\n");
16624
16625         /* Now print out the rx_bd's themselves. */
16626         for (i = 0; i < count; i++) {
16627                 rx_bd = (struct eth_rx_bd *) (&fp->rx_chain[prod]);
16628                 m = sc->fp->rx_mbuf_ptr[prod];
16629
16630                 bxe_dump_rxbd(fp, prod, rx_bd);
16631                 bxe_dump_mbuf(sc, m);
16632
16633                 /* Don't skip next page pointers. */
16634                 prod = ((prod + 1) & MAX_RX_BD);
16635         }
16636
16637         BXE_PRINTF(
16638             "----------------------------"
16639             "--------------"
16640             "----------------------------\n");
16641 }
16642
16643 /*
16644  * Prints out a register dump.
16645  *
16646  * Returns:
16647  *   Nothing.
16648  */
16649 static __noinline
16650 void bxe_dump_hw_state(struct bxe_softc *sc)
16651 {
16652         int i;
16653
16654         BXE_PRINTF(
16655                 "----------------------------"
16656                 " Hardware State "
16657                 "----------------------------\n");
16658
16659         for (i = 0x2000; i < 0x10000; i += 0x10)
16660                 BXE_PRINTF("0x%04X: 0x%08X 0x%08X 0x%08X 0x%08X\n", i,
16661                     REG_RD(sc, 0 + i), REG_RD(sc, 0 + i + 0x4),
16662                     REG_RD(sc, 0 + i + 0x8), REG_RD(sc, 0 + i + 0xC));
16663
16664         BXE_PRINTF(
16665             "----------------------------"
16666             "----------------"
16667             "----------------------------\n");
16668 }
16669
16670 /*
16671  * Prints out the RX mbuf chain.
16672  *
16673  * Returns:
16674  *   Nothing.
16675  */
16676 static __noinline
16677 void bxe_dump_rx_mbuf_chain(struct bxe_softc *sc, int chain_prod, int count)
16678 {
16679         struct mbuf *m;
16680         int i;
16681
16682         BXE_PRINTF(
16683             "----------------------------"
16684             "  rx mbuf data  "
16685             "----------------------------\n");
16686
16687         for (i = 0; i < count; i++) {
16688                 m = sc->fp->rx_mbuf_ptr[chain_prod];
16689                 BXE_PRINTF("rxmbuf[0x%04X]\n", chain_prod);
16690                 bxe_dump_mbuf(sc, m);
16691                 chain_prod = RX_BD(NEXT_RX_BD(chain_prod));
16692         }
16693
16694         BXE_PRINTF(
16695             "----------------------------"
16696             "----------------"
16697             "----------------------------\n");
16698 }
16699
16700 /*
16701  * Prints out the mbufs in the TX mbuf chain.
16702  *
16703  * Returns:
16704  *   Nothing.
16705  */
16706 static __noinline
16707 void bxe_dump_tx_mbuf_chain(struct bxe_softc *sc, int chain_prod, int count)
16708 {
16709         struct mbuf *m;
16710         int i;
16711
16712         BXE_PRINTF(
16713             "----------------------------"
16714             "  tx mbuf data  "
16715             "----------------------------\n");
16716
16717         for (i = 0; i < count; i++) {
16718                 m = sc->fp->tx_mbuf_ptr[chain_prod];
16719                 BXE_PRINTF("txmbuf[%d]\n", chain_prod);
16720                 bxe_dump_mbuf(sc, m);
16721                 chain_prod = TX_BD(NEXT_TX_BD(chain_prod));
16722         }
16723
16724         BXE_PRINTF(
16725             "----------------------------"
16726             "----------------"
16727             "----------------------------\n");
16728 }
16729
16730 /*
16731  * Prints out the status block from host memory.
16732  *
16733  * Returns:
16734  *   Nothing.
16735  */
16736 static __noinline
16737 void bxe_dump_status_block(struct bxe_softc *sc)
16738 {
16739         struct bxe_fastpath *fp;
16740         struct host_def_status_block *def_sb;
16741         struct host_status_block *fpsb;
16742         int i;
16743
16744         def_sb = sc->def_sb;
16745         BXE_PRINTF(
16746             "----------------------------"
16747             "  Status Block  "
16748             "----------------------------\n");
16749
16750         for (i = 0; i < sc->num_queues; i++) {
16751                 fp = &sc->fp[i];
16752                 fpsb = fp->status_block;
16753                 BXE_PRINTF(
16754                     "----------------------------"
16755                     "     fp[%02d]     "
16756                     "----------------------------\n", fp->index);
16757
16758                 /* Print the USTORM fields (HC_USTORM_SB_NUM_INDICES). */
16759                 BXE_PRINTF(
16760                     "0x%08X - USTORM Flags (F/W RESERVED)\n",
16761                     fpsb->u_status_block.__flags);
16762                 BXE_PRINTF(
16763                     "      0x%02X - USTORM PCIe Function\n",
16764                     fpsb->u_status_block.func);
16765                 BXE_PRINTF(
16766                     "      0x%02X - USTORM Status Block ID\n",
16767                     fpsb->u_status_block.status_block_id);
16768                 BXE_PRINTF(
16769                     "    0x%04X - USTORM Status Block Index (Tag)\n",
16770                     fpsb->u_status_block.status_block_index);
16771                 BXE_PRINTF(
16772                     "    0x%04X - USTORM [TOE_RX_CQ_CONS]\n",
16773                     fpsb->u_status_block.index_values[HC_INDEX_U_TOE_RX_CQ_CONS]);
16774                 BXE_PRINTF(
16775                     "    0x%04X - USTORM [ETH_RX_CQ_CONS]\n",
16776                     fpsb->u_status_block.index_values[HC_INDEX_U_ETH_RX_CQ_CONS]);
16777                 BXE_PRINTF(
16778                     "    0x%04X - USTORM [ETH_RX_BD_CONS]\n",
16779                     fpsb->u_status_block.index_values[HC_INDEX_U_ETH_RX_BD_CONS]);
16780                 BXE_PRINTF(
16781                     "    0x%04X - USTORM [RESERVED]\n",
16782                     fpsb->u_status_block.index_values[3]);
16783
16784                 /* Print the CSTORM fields (HC_CSTORM_SB_NUM_INDICES). */
16785                 BXE_PRINTF(
16786                     "0x%08X - CSTORM Flags (F/W RESERVED)\n",
16787                     fpsb->c_status_block.__flags);
16788                 BXE_PRINTF(
16789                     "      0x%02X - CSTORM PCIe Function\n",
16790                     fpsb->c_status_block.func);
16791                 BXE_PRINTF(
16792                     "      0x%02X - CSTORM Status Block ID\n",
16793                     fpsb->c_status_block.status_block_id);
16794                 BXE_PRINTF(
16795                     "    0x%04X - CSTORM Status Block Index (Tag)\n",
16796                     fpsb->c_status_block.status_block_index);
16797                 BXE_PRINTF(
16798                     "    0x%04X - CSTORM [TOE_TX_CQ_CONS]\n",
16799                     fpsb->c_status_block.index_values[HC_INDEX_C_TOE_TX_CQ_CONS]);
16800                 BXE_PRINTF(
16801                     "    0x%04X - CSTORM [ETH_TX_CQ_CONS]\n",
16802                     fpsb->c_status_block.index_values[HC_INDEX_C_ETH_TX_CQ_CONS]);
16803                 BXE_PRINTF(
16804                     "    0x%04X - CSTORM [ISCSI_EQ_CONS]\n",
16805                     fpsb->c_status_block.index_values[HC_INDEX_C_ISCSI_EQ_CONS]);
16806                 BXE_PRINTF(
16807                     "    0x%04X - CSTORM [RESERVED]\n",
16808                     fpsb->c_status_block.index_values[3]);
16809         }
16810
16811         BXE_PRINTF(
16812             "--------------------------"
16813             "  Def Status Block  "
16814             "--------------------------\n");
16815
16816         /* Print attention information. */
16817         BXE_PRINTF(
16818             "      0x%02X - Status Block ID\n",
16819             def_sb->atten_status_block.status_block_id);
16820         BXE_PRINTF(
16821             "0x%08X - Attn Bits\n",
16822             def_sb->atten_status_block.attn_bits);
16823         BXE_PRINTF(
16824             "0x%08X - Attn Bits Ack\n",
16825             def_sb->atten_status_block.attn_bits_ack);
16826         BXE_PRINTF(
16827             "    0x%04X - Attn Block Index\n",
16828             le16toh(def_sb->atten_status_block.attn_bits_index));
16829
16830         /* Print the USTORM fields (HC_USTORM_DEF_SB_NUM_INDICES). */
16831         BXE_PRINTF(
16832             "      0x%02X - USTORM Status Block ID\n",
16833             def_sb->u_def_status_block.status_block_id);
16834         BXE_PRINTF(
16835             "    0x%04X - USTORM Status Block Index\n",
16836             le16toh(def_sb->u_def_status_block.status_block_index));
16837         BXE_PRINTF(
16838             "    0x%04X - USTORM [ETH_RDMA_RX_CQ_CONS]\n",
16839             le16toh(def_sb->u_def_status_block.index_values[HC_INDEX_DEF_U_ETH_RDMA_RX_CQ_CONS]));
16840         BXE_PRINTF(
16841             "    0x%04X - USTORM [ETH_ISCSI_RX_CQ_CONS]\n",
16842             le16toh(def_sb->u_def_status_block.index_values[HC_INDEX_DEF_U_ETH_ISCSI_RX_CQ_CONS]));
16843         BXE_PRINTF(
16844             "    0x%04X - USTORM [ETH_RDMA_RX_BD_CONS]\n",
16845             le16toh(def_sb->u_def_status_block.index_values[HC_INDEX_DEF_U_ETH_RDMA_RX_BD_CONS]));
16846         BXE_PRINTF(
16847             "    0x%04X - USTORM [ETH_ISCSI_RX_BD_CONS]\n",
16848             le16toh(def_sb->u_def_status_block.index_values[HC_INDEX_DEF_U_ETH_ISCSI_RX_BD_CONS]));
16849
16850         /* Print the CSTORM fields (HC_CSTORM_DEF_SB_NUM_INDICES). */
16851         BXE_PRINTF(
16852             "      0x%02X - CSTORM Status Block ID\n",
16853             def_sb->c_def_status_block.status_block_id);
16854         BXE_PRINTF(
16855             "    0x%04X - CSTORM Status Block Index\n",
16856             le16toh(def_sb->c_def_status_block.status_block_index));
16857         BXE_PRINTF(
16858             "    0x%04X - CSTORM [RDMA_EQ_CONS]\n",
16859             le16toh(def_sb->c_def_status_block.index_values[HC_INDEX_DEF_C_RDMA_EQ_CONS]));
16860         BXE_PRINTF(
16861             "    0x%04X - CSTORM [RDMA_NAL_PROD]\n",
16862             le16toh(def_sb->c_def_status_block.index_values[HC_INDEX_DEF_C_RDMA_NAL_PROD]));
16863         BXE_PRINTF(
16864             "    0x%04X - CSTORM [ETH_FW_TX_CQ_CONS]\n",
16865             le16toh(def_sb->c_def_status_block.index_values[HC_INDEX_DEF_C_ETH_FW_TX_CQ_CONS]));
16866         BXE_PRINTF(
16867             "    0x%04X - CSTORM [ETH_SLOW_PATH]\n",
16868             le16toh(def_sb->c_def_status_block.index_values[HC_INDEX_DEF_C_ETH_SLOW_PATH]));
16869         BXE_PRINTF(
16870             "    0x%04X - CSTORM [ETH_RDMA_CQ_CONS]\n",
16871             le16toh(def_sb->c_def_status_block.index_values[HC_INDEX_DEF_C_ETH_RDMA_CQ_CONS]));
16872         BXE_PRINTF(
16873             "    0x%04X - CSTORM [ETH_ISCSI_CQ_CONS]\n",
16874             le16toh(def_sb->c_def_status_block.index_values[HC_INDEX_DEF_C_ETH_ISCSI_CQ_CONS]));
16875         BXE_PRINTF(
16876             "    0x%04X - CSTORM [UNUSED]\n",
16877             le16toh(def_sb->c_def_status_block.index_values[6]));
16878         BXE_PRINTF(
16879             "    0x%04X - CSTORM [UNUSED]\n",
16880             le16toh(def_sb->c_def_status_block.index_values[7]));
16881
16882         /* Print the TSTORM fields (HC_TSTORM_DEF_SB_NUM_INDICES). */
16883         BXE_PRINTF(
16884             "      0x%02X - TSTORM Status Block ID\n",
16885             def_sb->t_def_status_block.status_block_id);
16886         BXE_PRINTF(
16887              "    0x%04X - TSTORM Status Block Index\n",
16888             le16toh(def_sb->t_def_status_block.status_block_index));
16889         for (i = 0; i < HC_TSTORM_DEF_SB_NUM_INDICES; i++)
16890                 BXE_PRINTF(
16891                     "    0x%04X - TSTORM [UNUSED]\n",
16892                     le16toh(def_sb->t_def_status_block.index_values[i]));
16893
16894         /* Print the XSTORM fields (HC_XSTORM_DEF_SB_NUM_INDICES). */
16895         BXE_PRINTF(
16896             "      0x%02X - XSTORM Status Block ID\n",
16897             def_sb->x_def_status_block.status_block_id);
16898         BXE_PRINTF(
16899             "    0x%04X - XSTORM Status Block Index\n",
16900             le16toh(def_sb->x_def_status_block.status_block_index));
16901         for (i = 0; i < HC_XSTORM_DEF_SB_NUM_INDICES; i++)
16902                 BXE_PRINTF(
16903                     "    0x%04X - XSTORM [UNUSED]\n",
16904                     le16toh(def_sb->x_def_status_block.index_values[i]));
16905
16906         BXE_PRINTF(
16907             "----------------------------"
16908             "----------------"
16909             "----------------------------\n");
16910 }
16911
16912
16913 /*
16914  * Prints out the statistics block from host memory.
16915  *
16916  * Returns:
16917  *   Nothing.
16918  */
16919 static __noinline
16920 void bxe_dump_stats_block(struct bxe_softc *sc)
16921 {
16922
16923 }
16924
16925 /*
16926  * Prints out a summary of the fastpath state.
16927  *
16928  * Returns:
16929  *   Nothing.
16930  */
16931 static __noinline
16932 void bxe_dump_fp_state(struct bxe_fastpath *fp)
16933 {
16934         struct bxe_softc *sc;
16935         uint32_t val_hi, val_lo;
16936         int i;
16937
16938         sc = fp->sc;
16939         BXE_PRINTF(
16940             "----------------------------"
16941             " Fastpath State "
16942             "----------------------------\n");
16943
16944         val_hi = U64_HI(fp);
16945         val_lo = U64_LO(fp);
16946         BXE_PRINTF(
16947             "0x%08X:%08X - (fp[%02d]) fastpath virtual address\n",
16948             val_hi, val_lo, fp->index);
16949         BXE_PRINTF(
16950             "                %3d - (fp[%02d]->sb_id)\n",
16951             fp->sb_id, fp->index);
16952         BXE_PRINTF(
16953             "                %3d - (fp[%02d]->cl_id)\n",
16954             fp->cl_id, fp->index);
16955         BXE_PRINTF(
16956             "         0x%08X - (fp[%02d]->state)\n",
16957             (uint32_t)fp->state, fp->index);
16958
16959         /* Receive state. */
16960         BXE_PRINTF(
16961             "             0x%04X - (fp[%02d]->rx_bd_prod)\n",
16962             fp->rx_bd_prod, fp->index);
16963         BXE_PRINTF(
16964             "             0x%04X - (fp[%02d]->rx_bd_cons)\n",
16965             fp->rx_bd_cons, fp->index);
16966         BXE_PRINTF(
16967             "             0x%04X - (fp[%02d]->rx_cq_prod)\n",
16968             fp->rx_cq_prod, fp->index);
16969         BXE_PRINTF(
16970             "             0x%04X - (fp[%02d]->rx_cq_cons)\n",
16971             fp->rx_cq_cons, fp->index);
16972         BXE_PRINTF(
16973             "   %16lu - (fp[%02d]->rx_pkts)\n",
16974             fp->rx_pkts, fp->index);
16975         BXE_PRINTF(
16976             "         0x%08X - (fp[%02d]->rx_mbuf_alloc)\n",
16977             fp->rx_mbuf_alloc, fp->index);
16978         BXE_PRINTF(
16979             "   %16lu - (fp[%02d]->ipackets)\n",
16980             fp->ipackets, fp->index);
16981         BXE_PRINTF(
16982             "   %16lu - (fp[%02d]->rx_soft_errors)\n",
16983             fp->rx_soft_errors, fp->index);
16984
16985         /* Transmit state. */
16986         BXE_PRINTF(
16987             "             0x%04X - (fp[%02d]->tx_bd_used)\n",
16988             fp->tx_bd_used, fp->index);
16989         BXE_PRINTF(
16990             "             0x%04X - (fp[%02d]->tx_bd_prod)\n",
16991             fp->tx_bd_prod, fp->index);
16992         BXE_PRINTF(
16993             "             0x%04X - (fp[%02d]->tx_bd_cons)\n",
16994             fp->tx_bd_cons, fp->index);
16995         BXE_PRINTF(
16996             "             0x%04X - (fp[%02d]->tx_pkt_prod)\n",
16997             fp->tx_pkt_prod, fp->index);
16998         BXE_PRINTF(
16999             "             0x%04X - (fp[%02d]->tx_pkt_cons)\n",
17000             fp->tx_pkt_cons, fp->index);
17001         BXE_PRINTF(
17002             "   %16lu - (fp[%02d]->tx_pkts)\n",
17003             fp->tx_pkts, fp->index);
17004         BXE_PRINTF(
17005             "         0x%08X - (fp[%02d]->tx_mbuf_alloc)\n",
17006             fp->tx_mbuf_alloc, fp->index);
17007         BXE_PRINTF(
17008             "   %16lu - (fp[%02d]->opackets)\n",
17009             fp->opackets, fp->index);
17010         BXE_PRINTF(
17011             "   %16lu - (fp[%02d]->tx_soft_errors)\n",
17012             fp->tx_soft_errors, fp->index);
17013
17014         /* TPA state. */
17015         if (TPA_ENABLED(sc)) {
17016                 BXE_PRINTF(
17017                     "   %16lu - (fp[%02d]->rx_tpa_pkts)\n",
17018                     fp->rx_tpa_pkts, fp->index);
17019                 BXE_PRINTF(
17020                     "         0x%08X - (fp[%02d]->tpa_mbuf_alloc)\n",
17021                     fp->tpa_mbuf_alloc, fp->index);
17022                 BXE_PRINTF(
17023                     "         0x%08X - (fp[%02d]->sge_mbuf_alloc)\n",
17024                     fp->sge_mbuf_alloc, fp->index);
17025
17026                 if (CHIP_IS_E1(sc)) {
17027                         for (i = 0; i < ETH_MAX_AGGREGATION_QUEUES_E1; i++)
17028                                 BXE_PRINTF(
17029                         "         0x%08X - (fp[%02d]->tpa_state[%02d])\n",
17030                                     (uint32_t)fp->tpa_state[i], fp->index, i);
17031                 } else {
17032                         for (i = 0; i < ETH_MAX_AGGREGATION_QUEUES_E1; i++)
17033                                 BXE_PRINTF(
17034                         "         0x%08X - (fp[%02d]->tpa_state[%02d])\n",
17035                                     (uint32_t)fp->tpa_state[i], fp->index, i);
17036                 }
17037         }
17038
17039         BXE_PRINTF(
17040             "----------------------------"
17041             "----------------"
17042             "----------------------------\n");
17043 }
17044
17045 /*
17046  * Returns:
17047  *   Nothing.
17048  */
17049 static __noinline
17050 void bxe_dump_port_state_locked(struct bxe_softc *sc)
17051 {
17052
17053         BXE_PRINTF(
17054             "------------------------------"
17055             " Port State "
17056             "------------------------------\n");
17057
17058         BXE_PRINTF(
17059             "        %2d - (port) pmf\n", sc->port.pmf);
17060         BXE_PRINTF(
17061             "0x%08X - (port) link_config\n", sc->port.link_config);
17062         BXE_PRINTF(
17063             "0x%08X - (port) supported\n", sc->port.supported);
17064         BXE_PRINTF(
17065             "0x%08X - (port) advertising\n", sc->port.advertising);
17066         BXE_PRINTF(
17067             "0x%08X - (port) port_stx\n", sc->port.port_stx);
17068
17069         BXE_PRINTF(
17070             "----------------------------"
17071             "----------------"
17072             "----------------------------\n");
17073 }
17074
17075 /*
17076  * Returns:
17077  *   Nothing.
17078  */
17079 static __noinline
17080 void bxe_dump_link_vars_state_locked(struct bxe_softc *sc)
17081 {
17082         BXE_PRINTF(
17083             "---------------------------"
17084             " Link Vars State "
17085             "----------------------------\n");
17086
17087         switch (sc->link_vars.mac_type) {
17088         case MAC_TYPE_NONE:
17089                 BXE_PRINTF("      NONE");
17090                 break;
17091         case MAC_TYPE_EMAC:
17092                 BXE_PRINTF("      EMAC");
17093                 break;
17094         case MAC_TYPE_BMAC:
17095                 BXE_PRINTF("      BMAC");
17096                 break;
17097         default:
17098                 BXE_PRINTF("      UNKN");
17099         }
17100         printf(" - (link_vars->mac_type)\n");
17101
17102         BXE_PRINTF(
17103             "        %2d - (link_vars->phy_link_up)\n",
17104             sc->link_vars.phy_link_up);
17105         BXE_PRINTF(
17106             "        %2d - (link_vars->link_up)\n",
17107             sc->link_vars.link_up);
17108         BXE_PRINTF(
17109             "        %2d - (link_vars->duplex)\n",
17110             sc->link_vars.duplex);
17111         BXE_PRINTF(
17112             "    0x%04X - (link_vars->flow_ctrl)\n",
17113             sc->link_vars.flow_ctrl);
17114         BXE_PRINTF(
17115             "    0x%04X - (link_vars->line_speed)\n",
17116             sc->link_vars.line_speed);
17117         BXE_PRINTF(
17118             "0x%08X - (link_vars->ieee_fc)\n",
17119             sc->link_vars.ieee_fc);
17120         BXE_PRINTF(
17121             "0x%08X - (link_vars->autoneg)\n",
17122             sc->link_vars.autoneg);
17123         BXE_PRINTF(
17124             "0x%08X - (link_vars->phy_flags)\n",
17125             sc->link_vars.phy_flags);
17126         BXE_PRINTF(
17127             "0x%08X - (link_vars->link_status)\n",
17128             sc->link_vars.link_status);
17129
17130         BXE_PRINTF(
17131             "----------------------------"
17132             "----------------"
17133             "----------------------------\n");
17134 }
17135
17136
17137 /*
17138  *
17139  * Returns:
17140  *   Nothing.
17141  */
17142 static __noinline
17143 void bxe_dump_link_params_state_locked(struct bxe_softc *sc)
17144 {
17145         BXE_PRINTF(
17146             "--------------------------"
17147             " Link Params State "
17148             "---------------------------\n");
17149
17150         BXE_PRINTF(
17151             "        %2d - (link_params->port)\n",
17152             sc->link_params.port);
17153         BXE_PRINTF(
17154             "        %2d - (link_params->loopback_mode)\n",
17155             sc->link_params.loopback_mode);
17156         BXE_PRINTF(
17157             "       %3d - (link_params->phy_addr)\n",
17158             sc->link_params.phy_addr);
17159         BXE_PRINTF(
17160             "    0x%04X - (link_params->req_duplex)\n",
17161             sc->link_params.req_duplex);
17162         BXE_PRINTF(
17163             "    0x%04X - (link_params->req_flow_ctrl)\n",
17164             sc->link_params.req_flow_ctrl);
17165         BXE_PRINTF(
17166             "    0x%04X - (link_params->req_line_speed)\n",
17167             sc->link_params.req_line_speed);
17168         BXE_PRINTF(
17169             "     %5d - (link_params->ether_mtu)\n",
17170             sc->port.ether_mtu);
17171         BXE_PRINTF(
17172             "0x%08X - (link_params->shmem_base) shared memory base address\n",
17173             sc->link_params.shmem_base);
17174         BXE_PRINTF(
17175             "0x%08X - (link_params->speed_cap_mask)\n",
17176             sc->link_params.speed_cap_mask);
17177         BXE_PRINTF(
17178             "0x%08X - (link_params->ext_phy_config)\n",
17179             sc->link_params.ext_phy_config);
17180         BXE_PRINTF(
17181             "0x%08X - (link_params->switch_cfg)\n",
17182             sc->link_params.switch_cfg);
17183
17184         BXE_PRINTF(
17185             "----------------------------"
17186                 "----------------"
17187                 "----------------------------\n");
17188 }
17189
17190 /*
17191  * Prints out a summary of the driver state.
17192  *
17193  * Returns:
17194  *   Nothing.
17195  */
17196 static __noinline
17197 void bxe_dump_driver_state(struct bxe_softc *sc)
17198 {
17199         uint32_t val_hi, val_lo;
17200
17201         BXE_PRINTF(
17202             "-----------------------------"
17203             " Driver State "
17204             "-----------------------------\n");
17205
17206         val_hi = U64_HI(sc);
17207         val_lo = U64_LO(sc);
17208         BXE_PRINTF(
17209             "0x%08X:%08X - (sc) driver softc structure virtual address\n",
17210             val_hi, val_lo);
17211
17212         val_hi = U64_HI(sc->bxe_vhandle);
17213         val_lo = U64_LO(sc->bxe_vhandle);
17214         BXE_PRINTF(
17215             "0x%08X:%08X - (sc->bxe_vhandle) PCI BAR0 virtual address\n",
17216             val_hi, val_lo);
17217
17218         val_hi = U64_HI(sc->bxe_db_vhandle);
17219         val_lo = U64_LO(sc->bxe_db_vhandle);
17220         BXE_PRINTF(
17221             "0x%08X:%08X - (sc->bxe_db_vhandle) PCI BAR2 virtual address\n",
17222             val_hi, val_lo);
17223
17224         BXE_PRINTF("         0x%08X - (sc->num_queues) Fastpath queues\n",
17225             sc->num_queues);
17226         BXE_PRINTF("         0x%08X - (sc->rx_lane_swap) RX XAUI lane swap\n",
17227             sc->rx_lane_swap);
17228         BXE_PRINTF("         0x%08X - (sc->tx_lane_swap) TX XAUI lane swap\n",
17229             sc->tx_lane_swap);
17230         BXE_PRINTF("   %16lu - (sc->debug_sim_mbuf_alloc_failed)\n",
17231             sc->debug_sim_mbuf_alloc_failed);
17232         BXE_PRINTF("   %16lu - (sc->debug_sim_mbuf_map_failed)\n",
17233             sc->debug_sim_mbuf_map_failed);
17234
17235         BXE_PRINTF(
17236             "----------------------------"
17237             "----------------"
17238             "----------------------------\n");
17239
17240         bxe_dump_port_state_locked(sc);
17241         bxe_dump_link_params_state_locked(sc);
17242         bxe_dump_link_vars_state_locked(sc);
17243 }
17244
17245 /*
17246  * Dump bootcode (MCP) debug buffer to the console.
17247  *
17248  * Returns:
17249  *   None
17250  */
17251 static __noinline
17252 void bxe_dump_fw(struct bxe_softc *sc)
17253 {
17254         uint32_t addr, mark, data[9], offset;
17255         int word;
17256
17257         addr = sc->common.shmem_base - 0x0800 + 4;
17258         mark = REG_RD(sc, addr);
17259         mark = MCP_REG_MCPR_SCRATCH + ((mark + 0x3) & ~0x3) - 0x08000000;
17260
17261         BXE_PRINTF(
17262             "---------------------------"
17263             " MCP Debug Buffer "
17264             "---------------------------\n");
17265
17266         /* Read from "mark" to the end of the buffer. */
17267         for (offset = mark; offset <= sc->common.shmem_base;
17268             offset += (0x8 * 4)) {
17269                 for (word = 0; word < 8; word++)
17270                         data[word] = htonl(REG_RD(sc, offset + 4 * word));
17271                 data[8] = 0x0;
17272                 printf("%s", (char *) data);
17273         }
17274
17275         /* Read from the start of the buffer to "mark". */
17276         for (offset = addr + 4; offset <= mark; offset += (0x8 * 4)) {
17277                 for (word = 0; word < 8; word++)
17278                         data[word] = htonl(REG_RD(sc, offset + 4 * word));
17279                 data[8] = 0x0;
17280                 printf("%s", (char *) data);
17281         }
17282
17283         BXE_PRINTF(
17284             "----------------------------"
17285             "----------------"
17286             "----------------------------\n");
17287 }
17288
17289 /*
17290  * Decode firmware messages.
17291  *
17292  * Returns:
17293  *   None
17294  */
17295 static void
17296 bxe_decode_mb_msgs(struct bxe_softc *sc, uint32_t drv_mb_header,
17297     uint32_t fw_mb_header)
17298 {
17299
17300         if (drv_mb_header) {
17301                 BXE_PRINTF("Driver message is ");
17302                 switch (drv_mb_header & DRV_MSG_CODE_MASK) {
17303                 case DRV_MSG_CODE_LOAD_REQ:
17304                         printf(
17305                             "LOAD_REQ (0x%08X)",
17306                             (uint32_t)DRV_MSG_CODE_LOAD_REQ);
17307                         break;
17308                 case DRV_MSG_CODE_LOAD_DONE:
17309                         printf(
17310                             "LOAD_DONE (0x%08X)",
17311                             (uint32_t)DRV_MSG_CODE_LOAD_DONE);
17312                         break;
17313                 case DRV_MSG_CODE_UNLOAD_REQ_WOL_EN:
17314                         printf(
17315                             "UNLOAD_REQ_WOL_EN (0x%08X)",
17316                             (uint32_t)DRV_MSG_CODE_UNLOAD_REQ_WOL_EN);
17317                         break;
17318                 case DRV_MSG_CODE_UNLOAD_REQ_WOL_DIS:
17319                         printf(
17320                             "UNLOAD_REQ_WOL_DIS (0x%08X)",
17321                             (uint32_t)DRV_MSG_CODE_UNLOAD_REQ_WOL_DIS);
17322                         break;
17323                 case DRV_MSG_CODE_UNLOAD_REQ_WOL_MCP:
17324                         printf(
17325                             "UNLOADREQ_WOL_MCP (0x%08X)",
17326                             (uint32_t)DRV_MSG_CODE_UNLOAD_REQ_WOL_MCP);
17327                         break;
17328                 case DRV_MSG_CODE_UNLOAD_DONE:
17329                         printf(
17330                             "UNLOAD_DONE (0x%08X)",
17331                             (uint32_t)DRV_MSG_CODE_UNLOAD_DONE);
17332                         break;
17333                 case DRV_MSG_CODE_DIAG_ENTER_REQ:
17334                         printf(
17335                             "DIAG_ENTER_REQ (0x%08X)",
17336                             (uint32_t)DRV_MSG_CODE_DIAG_ENTER_REQ);
17337                         break;
17338                 case DRV_MSG_CODE_DIAG_EXIT_REQ:
17339                         printf(
17340                             "DIAG_EXIT_REQ (0x%08X)",
17341                             (uint32_t)DRV_MSG_CODE_DIAG_EXIT_REQ);
17342                         break;
17343                 case DRV_MSG_CODE_VALIDATE_KEY:
17344                         printf(
17345                             "CODE_VALIDITY_KEY (0x%08X)",
17346                             (uint32_t)DRV_MSG_CODE_VALIDATE_KEY);
17347                         break;
17348                 case DRV_MSG_CODE_GET_CURR_KEY:
17349                         printf(
17350                             "GET_CURR_KEY (0x%08X)",
17351                             (uint32_t) DRV_MSG_CODE_GET_CURR_KEY);
17352                         break;
17353                 case DRV_MSG_CODE_GET_UPGRADE_KEY:
17354                         printf(
17355                             "GET_UPGRADE_KEY (0x%08X)",
17356                             (uint32_t)DRV_MSG_CODE_GET_UPGRADE_KEY);
17357                         break;
17358                 case DRV_MSG_CODE_GET_MANUF_KEY:
17359                         printf(
17360                             "GET_MANUF_KEY (0x%08X)",
17361                             (uint32_t)DRV_MSG_CODE_GET_MANUF_KEY);
17362                         break;
17363                 case DRV_MSG_CODE_LOAD_L2B_PRAM:
17364                         printf(
17365                             "LOAD_L2B_PRAM (0x%08X)",
17366                             (uint32_t)DRV_MSG_CODE_LOAD_L2B_PRAM);
17367                                 break;
17368                 case BIOS_MSG_CODE_LIC_CHALLENGE:
17369                         printf(
17370                             "LIC_CHALLENGE (0x%08X)",
17371                             (uint32_t)BIOS_MSG_CODE_LIC_CHALLENGE);
17372                         break;
17373                 case BIOS_MSG_CODE_LIC_RESPONSE:
17374                         printf(
17375                             "LIC_RESPONSE (0x%08X)",
17376                             (uint32_t)BIOS_MSG_CODE_LIC_RESPONSE);
17377                         break;
17378                 case BIOS_MSG_CODE_VIRT_MAC_PRIM:
17379                         printf(
17380                             "VIRT_MAC_PRIM (0x%08X)",
17381                             (uint32_t)BIOS_MSG_CODE_VIRT_MAC_PRIM);
17382                         break;
17383                 case BIOS_MSG_CODE_VIRT_MAC_ISCSI:
17384                         printf(
17385                             "VIRT_MAC_ISCSI (0x%08X)",
17386                             (uint32_t)BIOS_MSG_CODE_VIRT_MAC_ISCSI);
17387                         break;
17388                 default:
17389                         printf(
17390                             "Unknown command (0x%08X)!",
17391                             (drv_mb_header & DRV_MSG_CODE_MASK));
17392                 }
17393
17394                 printf(" (seq = 0x%04X)\n", (drv_mb_header &
17395                     DRV_MSG_SEQ_NUMBER_MASK));
17396         }
17397
17398         if (fw_mb_header) {
17399                 BXE_PRINTF("Firmware response is ");
17400                 switch (fw_mb_header & FW_MSG_CODE_MASK) {
17401                 case FW_MSG_CODE_DRV_LOAD_COMMON:
17402                         printf(
17403                             "DRV_LOAD_COMMON (0x%08X)",
17404                             (uint32_t)FW_MSG_CODE_DRV_LOAD_COMMON);
17405                         break;
17406                 case FW_MSG_CODE_DRV_LOAD_PORT:
17407                         printf(
17408                             "DRV_LOAD_PORT (0x%08X)",
17409                             (uint32_t)FW_MSG_CODE_DRV_LOAD_PORT);
17410                         break;
17411                 case FW_MSG_CODE_DRV_LOAD_FUNCTION:
17412                         printf(
17413                             "DRV_LOAD_FUNCTION (0x%08X)",
17414                             (uint32_t)FW_MSG_CODE_DRV_LOAD_FUNCTION);
17415                         break;
17416                 case FW_MSG_CODE_DRV_LOAD_REFUSED:
17417                         printf(
17418                             "DRV_LOAD_REFUSED (0x%08X)",
17419                             (uint32_t)FW_MSG_CODE_DRV_LOAD_REFUSED);
17420                         break;
17421                 case FW_MSG_CODE_DRV_LOAD_DONE:
17422                         printf(
17423                             "DRV_LOAD_DONE (0x%08X)",
17424                             (uint32_t)FW_MSG_CODE_DRV_LOAD_DONE);
17425                         break;
17426                 case FW_MSG_CODE_DRV_UNLOAD_COMMON:
17427                         printf(
17428                             "DRV_UNLOAD_COMMON (0x%08X)",
17429                             (uint32_t)FW_MSG_CODE_DRV_UNLOAD_COMMON);
17430                         break;
17431                 case FW_MSG_CODE_DRV_UNLOAD_PORT:
17432                         printf(
17433                             "DRV_UNLOAD_PORT (0x%08X)",
17434                             (uint32_t)FW_MSG_CODE_DRV_UNLOAD_PORT);
17435                         break;
17436                 case FW_MSG_CODE_DRV_UNLOAD_FUNCTION:
17437                         printf(
17438                             "DRV_UNLOAD_FUNCTION (0x%08X)",
17439                             (uint32_t)FW_MSG_CODE_DRV_UNLOAD_FUNCTION);
17440                         break;
17441                 case FW_MSG_CODE_DRV_UNLOAD_DONE:
17442                         printf(
17443                             "DRV_UNLOAD_DONE (0x%08X)",
17444                             (uint32_t)FW_MSG_CODE_DRV_UNLOAD_DONE);
17445                         break;
17446                 case FW_MSG_CODE_DIAG_ENTER_DONE:
17447                         printf(
17448                             "DIAG_ENTER_DONE (0x%08X)",
17449                             (uint32_t)FW_MSG_CODE_DIAG_ENTER_DONE);
17450                         break;
17451                 case FW_MSG_CODE_DIAG_REFUSE:
17452                         printf(
17453                             "DIAG_REFUSE (0x%08X)",
17454                             (uint32_t)FW_MSG_CODE_DIAG_REFUSE);
17455                         break;
17456                 case FW_MSG_CODE_DIAG_EXIT_DONE:
17457                         printf(
17458                             "DIAG_EXIT_DONE (0x%08X)",
17459                             (uint32_t)FW_MSG_CODE_DIAG_EXIT_DONE);
17460                         break;
17461                 case FW_MSG_CODE_VALIDATE_KEY_SUCCESS:
17462                         printf(
17463                             "VALIDATE_KEY_SUCCESS (0x%08X)",
17464                             (uint32_t)FW_MSG_CODE_VALIDATE_KEY_SUCCESS);
17465                         break;
17466                 case FW_MSG_CODE_VALIDATE_KEY_FAILURE:
17467                         printf(
17468                             "VALIDATE_KEY_FAILURE (0x%08X)",
17469                             (uint32_t)FW_MSG_CODE_VALIDATE_KEY_FAILURE);
17470                         break;
17471                 case FW_MSG_CODE_GET_KEY_DONE:
17472                         printf(
17473                             "GET_KEY_DONE (0x%08X)",
17474                             (uint32_t)FW_MSG_CODE_GET_KEY_DONE);
17475                         break;
17476                 case FW_MSG_CODE_NO_KEY:
17477                         printf(
17478                             "NO_KEY (0x%08X)",
17479                             (uint32_t)FW_MSG_CODE_NO_KEY);
17480                         break;
17481                 default:
17482                         printf(
17483                             "unknown value (0x%08X)!",
17484                             (fw_mb_header & FW_MSG_CODE_MASK));
17485                 }
17486
17487                 printf(" (seq = 0x%04X)\n", (fw_mb_header &
17488                     FW_MSG_SEQ_NUMBER_MASK));
17489         }
17490 }
17491
17492 /*
17493  * Prints a text string for the ramrod command.
17494  *
17495  * Returns:
17496  *   None
17497  */
17498 static void
17499 bxe_decode_ramrod_cmd(struct bxe_softc *sc, int command)
17500 {
17501         BXE_PRINTF("Ramrod command = ");
17502
17503         switch (command) {
17504         case RAMROD_CMD_ID_ETH_PORT_SETUP:
17505                 printf("ETH_PORT_SETUP\n");
17506                 break;
17507         case RAMROD_CMD_ID_ETH_CLIENT_SETUP:
17508                 printf("ETH_CLIENT_SETUP\n");
17509                 break;
17510         case RAMROD_CMD_ID_ETH_STAT_QUERY:
17511                 printf("ETH_STAT_QUERY\n");
17512                 break;
17513         case RAMROD_CMD_ID_ETH_UPDATE:
17514                 printf("ETH_UPDATE\n");
17515                 break;
17516         case RAMROD_CMD_ID_ETH_HALT:
17517                 printf("ETH_HALT\n");
17518                 break;
17519         case RAMROD_CMD_ID_ETH_SET_MAC:
17520                 printf("ETH_SET_MAC\n");
17521                 break;
17522         case RAMROD_CMD_ID_ETH_CFC_DEL:
17523                 printf("ETH_CFC_DEL\n");
17524                 break;
17525         case RAMROD_CMD_ID_ETH_PORT_DEL:
17526                 printf("ETH_PORT_DEL\n");
17527                 break;
17528         case RAMROD_CMD_ID_ETH_FORWARD_SETUP:
17529                 printf("ETH_FORWARD_SETUP\n");
17530                 break;
17531         default:
17532                 printf("Unknown ramrod command!\n");
17533         }
17534 }
17535
17536
17537 /*
17538  * Prints out driver information and forces a kernel breakpoint.
17539  *
17540  * Returns:
17541  *   Nothing.
17542  */
17543 static void
17544 bxe_breakpoint(struct bxe_softc *sc)
17545 {
17546         struct bxe_fastpath *fp;
17547         int i;
17548
17549         fp = &sc->fp[0];
17550         /* Unreachable code to silence the compiler about unused functions. */
17551         if (0) {
17552                 bxe_reg_read16(sc, PCICFG_OFFSET);
17553                 bxe_dump_tx_mbuf_chain(sc, 0, USABLE_TX_BD);
17554                 bxe_dump_rx_mbuf_chain(sc, 0, USABLE_RX_BD);
17555                 bxe_dump_tx_chain(fp, 0, USABLE_TX_BD);
17556                 bxe_dump_rx_cq_chain(fp, 0, USABLE_RCQ_ENTRIES);
17557                 bxe_dump_rx_bd_chain(fp, 0, USABLE_RX_BD);
17558                 bxe_dump_status_block(sc);
17559                 bxe_dump_stats_block(sc);
17560                 bxe_dump_fp_state(fp);
17561                 bxe_dump_driver_state(sc);
17562                 bxe_dump_hw_state(sc);
17563                 bxe_dump_fw(sc);
17564         }
17565
17566         /*
17567          * Do some device sanity checking.  Run it twice in case
17568          * the hardware is still running so we can identify any
17569          * transient conditions.
17570          */
17571         bxe_idle_chk(sc); bxe_idle_chk(sc);
17572
17573         bxe_dump_driver_state(sc);
17574
17575         for (i = 0; i < sc->num_queues; i++)
17576                 bxe_dump_fp_state(&sc->fp[i]);
17577
17578         bxe_dump_status_block(sc);
17579         bxe_dump_fw(sc);
17580
17581         /* Call the OS debugger. */
17582         breakpoint();
17583 }
17584 #endif