xref: /freebsd/sys/dev/bce/if_bce.c (revision d5fc25e5d6c52b306312784663ccad85923a9c76)
1 /*-
2  * Copyright (c) 2006-2009 Broadcom Corporation
3  *	David Christensen <davidch@broadcom.com>.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  * 3. Neither the name of Broadcom Corporation nor the name of its contributors
15  *    may be used to endorse or promote products derived from this software
16  *    without specific prior written consent.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS'
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
22  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
28  * THE POSSIBILITY OF SUCH DAMAGE.
29  */
30 
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
33 
34 /*
35  * The following controllers are supported by this driver:
36  *   BCM5706C A2, A3
37  *   BCM5706S A2, A3
38  *   BCM5708C B1, B2
39  *   BCM5708S B1, B2
40  *   BCM5709C A1, C0
41  * 	 BCM5716C C0
42  *
43  * The following controllers are not supported by this driver:
44  *   BCM5706C A0, A1 (pre-production)
45  *   BCM5706S A0, A1 (pre-production)
46  *   BCM5708C A0, B0 (pre-production)
47  *   BCM5708S A0, B0 (pre-production)
48  *   BCM5709C A0  B0, B1, B2 (pre-production)
49  *   BCM5709S A0, A1, B0, B1, B2, C0 (pre-production)
50  */
51 
52 #include "opt_bce.h"
53 
54 #include <dev/bce/if_bcereg.h>
55 #include <dev/bce/if_bcefw.h>
56 
57 /****************************************************************************/
58 /* BCE Debug Options                                                        */
59 /****************************************************************************/
60 #ifdef BCE_DEBUG
61 	u32 bce_debug = BCE_WARN;
62 
63 	/*          0 = Never              */
64 	/*          1 = 1 in 2,147,483,648 */
65 	/*        256 = 1 in     8,388,608 */
66 	/*       2048 = 1 in     1,048,576 */
67 	/*      65536 = 1 in        32,768 */
68 	/*    1048576 = 1 in         2,048 */
69 	/*  268435456 =	1 in             8 */
70 	/*  536870912 = 1 in             4 */
71 	/* 1073741824 = 1 in             2 */
72 
73 	/* Controls how often the l2_fhdr frame error check will fail. */
74 	int l2fhdr_error_sim_control = 0;
75 
76 	/* Controls how often the unexpected attention check will fail. */
77 	int unexpected_attention_sim_control = 0;
78 
79 	/* Controls how often to simulate an mbuf allocation failure. */
80 	int mbuf_alloc_failed_sim_control = 0;
81 
82 	/* Controls how often to simulate a DMA mapping failure. */
83 	int dma_map_addr_failed_sim_control = 0;
84 
85 	/* Controls how often to simulate a bootcode failure. */
86 	int bootcode_running_failure_sim_control = 0;
87 #endif
88 
89 /****************************************************************************/
90 /* BCE Build Time Options                                                   */
91 /****************************************************************************/
92 /* #define BCE_NVRAM_WRITE_SUPPORT 1 */
93 
94 
95 /****************************************************************************/
96 /* PCI Device ID Table                                                      */
97 /*                                                                          */
98 /* Used by bce_probe() to identify the devices supported by this driver.    */
99 /****************************************************************************/
100 #define BCE_DEVDESC_MAX		64
101 
102 static struct bce_type bce_devs[] = {
103 	/* BCM5706C Controllers and OEM boards. */
104 	{ BRCM_VENDORID, BRCM_DEVICEID_BCM5706,  HP_VENDORID, 0x3101,
105 		"HP NC370T Multifunction Gigabit Server Adapter" },
106 	{ BRCM_VENDORID, BRCM_DEVICEID_BCM5706,  HP_VENDORID, 0x3106,
107 		"HP NC370i Multifunction Gigabit Server Adapter" },
108 	{ BRCM_VENDORID, BRCM_DEVICEID_BCM5706,  HP_VENDORID, 0x3070,
109 		"HP NC380T PCIe DP Multifunc Gig Server Adapter" },
110 	{ BRCM_VENDORID, BRCM_DEVICEID_BCM5706,  HP_VENDORID, 0x1709,
111 		"HP NC371i Multifunction Gigabit Server Adapter" },
112 	{ BRCM_VENDORID, BRCM_DEVICEID_BCM5706,  PCI_ANY_ID,  PCI_ANY_ID,
113 		"Broadcom NetXtreme II BCM5706 1000Base-T" },
114 
115 	/* BCM5706S controllers and OEM boards. */
116 	{ BRCM_VENDORID, BRCM_DEVICEID_BCM5706S, HP_VENDORID, 0x3102,
117 		"HP NC370F Multifunction Gigabit Server Adapter" },
118 	{ BRCM_VENDORID, BRCM_DEVICEID_BCM5706S, PCI_ANY_ID,  PCI_ANY_ID,
119 		"Broadcom NetXtreme II BCM5706 1000Base-SX" },
120 
121 	/* BCM5708C controllers and OEM boards. */
122 	{ BRCM_VENDORID, BRCM_DEVICEID_BCM5708,  HP_VENDORID, 0x7037,
123 		"HP NC373T PCIe Multifunction Gig Server Adapter" },
124 	{ BRCM_VENDORID, BRCM_DEVICEID_BCM5708,  HP_VENDORID, 0x7038,
125 		"HP NC373i Multifunction Gigabit Server Adapter" },
126 	{ BRCM_VENDORID, BRCM_DEVICEID_BCM5708,  HP_VENDORID, 0x7045,
127 		"HP NC374m PCIe Multifunction Adapter" },
128 	{ BRCM_VENDORID, BRCM_DEVICEID_BCM5708,  PCI_ANY_ID,  PCI_ANY_ID,
129 		"Broadcom NetXtreme II BCM5708 1000Base-T" },
130 
131 	/* BCM5708S controllers and OEM boards. */
132 	{ BRCM_VENDORID, BRCM_DEVICEID_BCM5708S,  HP_VENDORID, 0x1706,
133 		"HP NC373m Multifunction Gigabit Server Adapter" },
134 	{ BRCM_VENDORID, BRCM_DEVICEID_BCM5708S,  HP_VENDORID, 0x703b,
135 		"HP NC373i Multifunction Gigabit Server Adapter" },
136 	{ BRCM_VENDORID, BRCM_DEVICEID_BCM5708S,  HP_VENDORID, 0x703d,
137 		"HP NC373F PCIe Multifunc Giga Server Adapter" },
138 	{ BRCM_VENDORID, BRCM_DEVICEID_BCM5708S,  PCI_ANY_ID,  PCI_ANY_ID,
139 		"Broadcom NetXtreme II BCM5708 1000Base-SX" },
140 
141 	/* BCM5709C controllers and OEM boards. */
142 	{ BRCM_VENDORID, BRCM_DEVICEID_BCM5709,  HP_VENDORID, 0x7055,
143 		"HP NC382i DP Multifunction Gigabit Server Adapter" },
144 	{ BRCM_VENDORID, BRCM_DEVICEID_BCM5709,  HP_VENDORID, 0x7059,
145 		"HP NC382T PCIe DP Multifunction Gigabit Server Adapter" },
146 	{ BRCM_VENDORID, BRCM_DEVICEID_BCM5709,  PCI_ANY_ID,  PCI_ANY_ID,
147 		"Broadcom NetXtreme II BCM5709 1000Base-T" },
148 
149 	/* BCM5709S controllers and OEM boards. */
150 	{ BRCM_VENDORID, BRCM_DEVICEID_BCM5709S,  HP_VENDORID, 0x171d,
151 		"HP NC382m DP 1GbE Multifunction BL-c Adapter" },
152 	{ BRCM_VENDORID, BRCM_DEVICEID_BCM5709S,  HP_VENDORID, 0x7056,
153 		"HP NC382i DP Multifunction Gigabit Server Adapter" },
154 	{ BRCM_VENDORID, BRCM_DEVICEID_BCM5709S,  PCI_ANY_ID,  PCI_ANY_ID,
155 		"Broadcom NetXtreme II BCM5709 1000Base-SX" },
156 
157 	/* BCM5716 controllers and OEM boards. */
158 	{ BRCM_VENDORID, BRCM_DEVICEID_BCM5716,  PCI_ANY_ID,  PCI_ANY_ID,
159 		"Broadcom NetXtreme II BCM5716 1000Base-T" },
160 
161 	{ 0, 0, 0, 0, NULL }
162 };
163 
164 
165 /****************************************************************************/
166 /* Supported Flash NVRAM device data.                                       */
167 /****************************************************************************/
168 static struct flash_spec flash_table[] =
169 {
170 #define BUFFERED_FLAGS		(BCE_NV_BUFFERED | BCE_NV_TRANSLATE)
171 #define NONBUFFERED_FLAGS	(BCE_NV_WREN)
172 
173 	/* Slow EEPROM */
174 	{0x00000000, 0x40830380, 0x009f0081, 0xa184a053, 0xaf000400,
175 	 BUFFERED_FLAGS, SEEPROM_PAGE_BITS, SEEPROM_PAGE_SIZE,
176 	 SEEPROM_BYTE_ADDR_MASK, SEEPROM_TOTAL_SIZE,
177 	 "EEPROM - slow"},
178 	/* Expansion entry 0001 */
179 	{0x08000002, 0x4b808201, 0x00050081, 0x03840253, 0xaf020406,
180 	 NONBUFFERED_FLAGS, SAIFUN_FLASH_PAGE_BITS, SAIFUN_FLASH_PAGE_SIZE,
181 	 SAIFUN_FLASH_BYTE_ADDR_MASK, 0,
182 	 "Entry 0001"},
183 	/* Saifun SA25F010 (non-buffered flash) */
184 	/* strap, cfg1, & write1 need updates */
185 	{0x04000001, 0x47808201, 0x00050081, 0x03840253, 0xaf020406,
186 	 NONBUFFERED_FLAGS, SAIFUN_FLASH_PAGE_BITS, SAIFUN_FLASH_PAGE_SIZE,
187 	 SAIFUN_FLASH_BYTE_ADDR_MASK, SAIFUN_FLASH_BASE_TOTAL_SIZE*2,
188 	 "Non-buffered flash (128kB)"},
189 	/* Saifun SA25F020 (non-buffered flash) */
190 	/* strap, cfg1, & write1 need updates */
191 	{0x0c000003, 0x4f808201, 0x00050081, 0x03840253, 0xaf020406,
192 	 NONBUFFERED_FLAGS, SAIFUN_FLASH_PAGE_BITS, SAIFUN_FLASH_PAGE_SIZE,
193 	 SAIFUN_FLASH_BYTE_ADDR_MASK, SAIFUN_FLASH_BASE_TOTAL_SIZE*4,
194 	 "Non-buffered flash (256kB)"},
195 	/* Expansion entry 0100 */
196 	{0x11000000, 0x53808201, 0x00050081, 0x03840253, 0xaf020406,
197 	 NONBUFFERED_FLAGS, SAIFUN_FLASH_PAGE_BITS, SAIFUN_FLASH_PAGE_SIZE,
198 	 SAIFUN_FLASH_BYTE_ADDR_MASK, 0,
199 	 "Entry 0100"},
200 	/* Entry 0101: ST M45PE10 (non-buffered flash, TetonII B0) */
201 	{0x19000002, 0x5b808201, 0x000500db, 0x03840253, 0xaf020406,
202 	 NONBUFFERED_FLAGS, ST_MICRO_FLASH_PAGE_BITS, ST_MICRO_FLASH_PAGE_SIZE,
203 	 ST_MICRO_FLASH_BYTE_ADDR_MASK, ST_MICRO_FLASH_BASE_TOTAL_SIZE*2,
204 	 "Entry 0101: ST M45PE10 (128kB non-bufferred)"},
205 	/* Entry 0110: ST M45PE20 (non-buffered flash)*/
206 	{0x15000001, 0x57808201, 0x000500db, 0x03840253, 0xaf020406,
207 	 NONBUFFERED_FLAGS, ST_MICRO_FLASH_PAGE_BITS, ST_MICRO_FLASH_PAGE_SIZE,
208 	 ST_MICRO_FLASH_BYTE_ADDR_MASK, ST_MICRO_FLASH_BASE_TOTAL_SIZE*4,
209 	 "Entry 0110: ST M45PE20 (256kB non-bufferred)"},
210 	/* Saifun SA25F005 (non-buffered flash) */
211 	/* strap, cfg1, & write1 need updates */
212 	{0x1d000003, 0x5f808201, 0x00050081, 0x03840253, 0xaf020406,
213 	 NONBUFFERED_FLAGS, SAIFUN_FLASH_PAGE_BITS, SAIFUN_FLASH_PAGE_SIZE,
214 	 SAIFUN_FLASH_BYTE_ADDR_MASK, SAIFUN_FLASH_BASE_TOTAL_SIZE,
215 	 "Non-buffered flash (64kB)"},
216 	/* Fast EEPROM */
217 	{0x22000000, 0x62808380, 0x009f0081, 0xa184a053, 0xaf000400,
218 	 BUFFERED_FLAGS, SEEPROM_PAGE_BITS, SEEPROM_PAGE_SIZE,
219 	 SEEPROM_BYTE_ADDR_MASK, SEEPROM_TOTAL_SIZE,
220 	 "EEPROM - fast"},
221 	/* Expansion entry 1001 */
222 	{0x2a000002, 0x6b808201, 0x00050081, 0x03840253, 0xaf020406,
223 	 NONBUFFERED_FLAGS, SAIFUN_FLASH_PAGE_BITS, SAIFUN_FLASH_PAGE_SIZE,
224 	 SAIFUN_FLASH_BYTE_ADDR_MASK, 0,
225 	 "Entry 1001"},
226 	/* Expansion entry 1010 */
227 	{0x26000001, 0x67808201, 0x00050081, 0x03840253, 0xaf020406,
228 	 NONBUFFERED_FLAGS, SAIFUN_FLASH_PAGE_BITS, SAIFUN_FLASH_PAGE_SIZE,
229 	 SAIFUN_FLASH_BYTE_ADDR_MASK, 0,
230 	 "Entry 1010"},
231 	/* ATMEL AT45DB011B (buffered flash) */
232 	{0x2e000003, 0x6e808273, 0x00570081, 0x68848353, 0xaf000400,
233 	 BUFFERED_FLAGS, BUFFERED_FLASH_PAGE_BITS, BUFFERED_FLASH_PAGE_SIZE,
234 	 BUFFERED_FLASH_BYTE_ADDR_MASK, BUFFERED_FLASH_TOTAL_SIZE,
235 	 "Buffered flash (128kB)"},
236 	/* Expansion entry 1100 */
237 	{0x33000000, 0x73808201, 0x00050081, 0x03840253, 0xaf020406,
238 	 NONBUFFERED_FLAGS, SAIFUN_FLASH_PAGE_BITS, SAIFUN_FLASH_PAGE_SIZE,
239 	 SAIFUN_FLASH_BYTE_ADDR_MASK, 0,
240 	 "Entry 1100"},
241 	/* Expansion entry 1101 */
242 	{0x3b000002, 0x7b808201, 0x00050081, 0x03840253, 0xaf020406,
243 	 NONBUFFERED_FLAGS, SAIFUN_FLASH_PAGE_BITS, SAIFUN_FLASH_PAGE_SIZE,
244 	 SAIFUN_FLASH_BYTE_ADDR_MASK, 0,
245 	 "Entry 1101"},
246 	/* Ateml Expansion entry 1110 */
247 	{0x37000001, 0x76808273, 0x00570081, 0x68848353, 0xaf000400,
248 	 BUFFERED_FLAGS, BUFFERED_FLASH_PAGE_BITS, BUFFERED_FLASH_PAGE_SIZE,
249 	 BUFFERED_FLASH_BYTE_ADDR_MASK, 0,
250 	 "Entry 1110 (Atmel)"},
251 	/* ATMEL AT45DB021B (buffered flash) */
252 	{0x3f000003, 0x7e808273, 0x00570081, 0x68848353, 0xaf000400,
253 	 BUFFERED_FLAGS, BUFFERED_FLASH_PAGE_BITS, BUFFERED_FLASH_PAGE_SIZE,
254 	 BUFFERED_FLASH_BYTE_ADDR_MASK, BUFFERED_FLASH_TOTAL_SIZE*2,
255 	 "Buffered flash (256kB)"},
256 };
257 
258 /*
259  * The BCM5709 controllers transparently handle the
260  * differences between Atmel 264 byte pages and all
261  * flash devices which use 256 byte pages, so no
262  * logical-to-physical mapping is required in the
263  * driver.
264  */
265 static struct flash_spec flash_5709 = {
266 	.flags		= BCE_NV_BUFFERED,
267 	.page_bits	= BCM5709_FLASH_PAGE_BITS,
268 	.page_size	= BCM5709_FLASH_PAGE_SIZE,
269 	.addr_mask	= BCM5709_FLASH_BYTE_ADDR_MASK,
270 	.total_size	= BUFFERED_FLASH_TOTAL_SIZE * 2,
271 	.name		= "5709/5716 buffered flash (256kB)",
272 };
273 
274 
275 /****************************************************************************/
276 /* FreeBSD device entry points.                                             */
277 /****************************************************************************/
278 static int  bce_probe				(device_t);
279 static int  bce_attach				(device_t);
280 static int  bce_detach				(device_t);
281 static int  bce_shutdown			(device_t);
282 
283 
284 /****************************************************************************/
285 /* BCE Debug Data Structure Dump Routines                                   */
286 /****************************************************************************/
287 #ifdef BCE_DEBUG
288 static u32	bce_reg_rd				(struct bce_softc *, u32);
289 static void	bce_reg_wr				(struct bce_softc *, u32, u32);
290 static void	bce_reg_wr16			(struct bce_softc *, u32, u16);
291 static u32  bce_ctx_rd				(struct bce_softc *, u32, u32);
292 static void bce_dump_enet           (struct bce_softc *, struct mbuf *);
293 static void bce_dump_mbuf 			(struct bce_softc *, struct mbuf *);
294 static void bce_dump_tx_mbuf_chain	(struct bce_softc *, u16, int);
295 static void bce_dump_rx_mbuf_chain	(struct bce_softc *, u16, int);
296 #ifdef ZERO_COPY_SOCKETS
297 static void bce_dump_pg_mbuf_chain	(struct bce_softc *, u16, int);
298 #endif
299 static void bce_dump_txbd			(struct bce_softc *, int, struct tx_bd *);
300 static void bce_dump_rxbd			(struct bce_softc *, int, struct rx_bd *);
301 #ifdef ZERO_COPY_SOCKETS
302 static void bce_dump_pgbd			(struct bce_softc *, int, struct rx_bd *);
303 #endif
304 static void bce_dump_l2fhdr			(struct bce_softc *, int, struct l2_fhdr *);
305 static void bce_dump_ctx			(struct bce_softc *, u16);
306 static void bce_dump_ftqs			(struct bce_softc *);
307 static void bce_dump_tx_chain		(struct bce_softc *, u16, int);
308 static void bce_dump_rx_chain		(struct bce_softc *, u16, int);
309 #ifdef ZERO_COPY_SOCKETS
310 static void bce_dump_pg_chain		(struct bce_softc *, u16, int);
311 #endif
312 static void bce_dump_status_block	(struct bce_softc *);
313 static void bce_dump_stats_block	(struct bce_softc *);
314 static void bce_dump_driver_state	(struct bce_softc *);
315 static void bce_dump_hw_state		(struct bce_softc *);
316 static void bce_dump_mq_regs        (struct bce_softc *);
317 static void bce_dump_bc_state		(struct bce_softc *);
318 static void bce_dump_txp_state		(struct bce_softc *, int);
319 static void bce_dump_rxp_state		(struct bce_softc *, int);
320 static void bce_dump_tpat_state		(struct bce_softc *, int);
321 static void bce_dump_cp_state		(struct bce_softc *, int);
322 static void bce_dump_com_state		(struct bce_softc *, int);
323 static void bce_breakpoint			(struct bce_softc *);
324 #endif
325 
326 
327 /****************************************************************************/
328 /* BCE Register/Memory Access Routines                                      */
329 /****************************************************************************/
330 static u32  bce_reg_rd_ind			(struct bce_softc *, u32);
331 static void bce_reg_wr_ind			(struct bce_softc *, u32, u32);
332 static void bce_ctx_wr				(struct bce_softc *, u32, u32, u32);
333 static int  bce_miibus_read_reg		(device_t, int, int);
334 static int  bce_miibus_write_reg	(device_t, int, int, int);
335 static void bce_miibus_statchg		(device_t);
336 
337 
338 /****************************************************************************/
339 /* BCE NVRAM Access Routines                                                */
340 /****************************************************************************/
341 static int  bce_acquire_nvram_lock	(struct bce_softc *);
342 static int  bce_release_nvram_lock	(struct bce_softc *);
343 static void bce_enable_nvram_access	(struct bce_softc *);
344 static void	bce_disable_nvram_access(struct bce_softc *);
345 static int  bce_nvram_read_dword	(struct bce_softc *, u32, u8 *, u32);
346 static int  bce_init_nvram			(struct bce_softc *);
347 static int  bce_nvram_read			(struct bce_softc *, u32, u8 *, int);
348 static int  bce_nvram_test			(struct bce_softc *);
349 #ifdef BCE_NVRAM_WRITE_SUPPORT
350 static int  bce_enable_nvram_write	(struct bce_softc *);
351 static void bce_disable_nvram_write	(struct bce_softc *);
352 static int  bce_nvram_erase_page	(struct bce_softc *, u32);
353 static int  bce_nvram_write_dword	(struct bce_softc *, u32, u8 *, u32);
354 static int  bce_nvram_write			(struct bce_softc *, u32, u8 *, int);
355 #endif
356 
357 /****************************************************************************/
358 /*                                                                          */
359 /****************************************************************************/
360 static void bce_get_media			(struct bce_softc *);
361 static void bce_dma_map_addr		(void *, bus_dma_segment_t *, int, int);
362 static int  bce_dma_alloc			(device_t);
363 static void bce_dma_free			(struct bce_softc *);
364 static void bce_release_resources	(struct bce_softc *);
365 
366 /****************************************************************************/
367 /* BCE Firmware Synchronization and Load                                    */
368 /****************************************************************************/
369 static int  bce_fw_sync				(struct bce_softc *, u32);
370 static void bce_load_rv2p_fw		(struct bce_softc *, u32 *, u32, u32);
371 static void bce_load_cpu_fw			(struct bce_softc *, struct cpu_reg *, struct fw_info *);
372 static void bce_init_rxp_cpu		(struct bce_softc *);
373 static void bce_init_txp_cpu 		(struct bce_softc *);
374 static void bce_init_tpat_cpu		(struct bce_softc *);
375 static void bce_init_cp_cpu		  	(struct bce_softc *);
376 static void bce_init_com_cpu	  	(struct bce_softc *);
377 static void bce_init_cpus			(struct bce_softc *);
378 
379 static void	bce_print_adapter_info	(struct bce_softc *);
380 static void bce_probe_pci_caps		(device_t, struct bce_softc *);
381 static void bce_stop				(struct bce_softc *);
382 static int  bce_reset				(struct bce_softc *, u32);
383 static int  bce_chipinit 			(struct bce_softc *);
384 static int  bce_blockinit 			(struct bce_softc *);
385 
386 static int  bce_init_tx_chain		(struct bce_softc *);
387 static void bce_free_tx_chain		(struct bce_softc *);
388 
389 static int  bce_get_rx_buf			(struct bce_softc *, struct mbuf *, u16 *, u16 *, u32 *);
390 static int  bce_init_rx_chain		(struct bce_softc *);
391 static void bce_fill_rx_chain		(struct bce_softc *);
392 static void bce_free_rx_chain		(struct bce_softc *);
393 
394 #ifdef ZERO_COPY_SOCKETS
395 static int  bce_get_pg_buf			(struct bce_softc *, struct mbuf *, u16 *, u16 *);
396 static int  bce_init_pg_chain		(struct bce_softc *);
397 static void bce_fill_pg_chain		(struct bce_softc *);
398 static void bce_free_pg_chain		(struct bce_softc *);
399 #endif
400 
401 static int  bce_tx_encap			(struct bce_softc *, struct mbuf **);
402 static void bce_start_locked		(struct ifnet *);
403 static void bce_start				(struct ifnet *);
404 static int  bce_ioctl				(struct ifnet *, u_long, caddr_t);
405 static void bce_watchdog			(struct bce_softc *);
406 static int  bce_ifmedia_upd			(struct ifnet *);
407 static void bce_ifmedia_upd_locked	(struct ifnet *);
408 static void bce_ifmedia_sts			(struct ifnet *, struct ifmediareq *);
409 static void bce_init_locked			(struct bce_softc *);
410 static void bce_init				(void *);
411 static void bce_mgmt_init_locked	(struct bce_softc *sc);
412 
413 static void bce_init_ctx			(struct bce_softc *);
414 static void bce_get_mac_addr		(struct bce_softc *);
415 static void bce_set_mac_addr		(struct bce_softc *);
416 static void bce_phy_intr			(struct bce_softc *);
417 static inline u16 bce_get_hw_rx_cons(struct bce_softc *);
418 static void bce_rx_intr				(struct bce_softc *);
419 static void bce_tx_intr				(struct bce_softc *);
420 static void bce_disable_intr		(struct bce_softc *);
421 static void bce_enable_intr			(struct bce_softc *, int);
422 
423 static void bce_intr				(void *);
424 static void bce_set_rx_mode			(struct bce_softc *);
425 static void bce_stats_update		(struct bce_softc *);
426 static void bce_tick				(void *);
427 static void bce_pulse				(void *);
428 static void bce_add_sysctls			(struct bce_softc *);
429 
430 
431 /****************************************************************************/
432 /* FreeBSD device dispatch table.                                           */
433 /****************************************************************************/
434 static device_method_t bce_methods[] = {
435 	/* Device interface (device_if.h) */
436 	DEVMETHOD(device_probe,		bce_probe),
437 	DEVMETHOD(device_attach,	bce_attach),
438 	DEVMETHOD(device_detach,	bce_detach),
439 	DEVMETHOD(device_shutdown,	bce_shutdown),
440 /* Supported by device interface but not used here. */
441 /*	DEVMETHOD(device_identify,	bce_identify),      */
442 /*	DEVMETHOD(device_suspend,	bce_suspend),       */
443 /*	DEVMETHOD(device_resume,	bce_resume),        */
444 /*	DEVMETHOD(device_quiesce,	bce_quiesce),       */
445 
446 	/* Bus interface (bus_if.h) */
447 	DEVMETHOD(bus_print_child,	bus_generic_print_child),
448 	DEVMETHOD(bus_driver_added,	bus_generic_driver_added),
449 
450 	/* MII interface (miibus_if.h) */
451 	DEVMETHOD(miibus_readreg,	bce_miibus_read_reg),
452 	DEVMETHOD(miibus_writereg,	bce_miibus_write_reg),
453 	DEVMETHOD(miibus_statchg,	bce_miibus_statchg),
454 /* Supported by MII interface but not used here.       */
455 /*	DEVMETHOD(miibus_linkchg,	bce_miibus_linkchg),   */
456 /*	DEVMETHOD(miibus_mediainit,	bce_miibus_mediainit), */
457 
458 	{ 0, 0 }
459 };
460 
461 static driver_t bce_driver = {
462 	"bce",
463 	bce_methods,
464 	sizeof(struct bce_softc)
465 };
466 
467 static devclass_t bce_devclass;
468 
469 MODULE_DEPEND(bce, pci, 1, 1, 1);
470 MODULE_DEPEND(bce, ether, 1, 1, 1);
471 MODULE_DEPEND(bce, miibus, 1, 1, 1);
472 
473 DRIVER_MODULE(bce, pci, bce_driver, bce_devclass, 0, 0);
474 DRIVER_MODULE(miibus, bce, miibus_driver, miibus_devclass, 0, 0);
475 
476 
477 /****************************************************************************/
478 /* Tunable device values                                                    */
479 /****************************************************************************/
480 SYSCTL_NODE(_hw, OID_AUTO, bce, CTLFLAG_RD, 0, "bce driver parameters");
481 
482 /* Allowable values are TRUE or FALSE */
483 static int bce_tso_enable = TRUE;
484 TUNABLE_INT("hw.bce.tso_enable", &bce_tso_enable);
485 SYSCTL_UINT(_hw_bce, OID_AUTO, tso_enable, CTLFLAG_RDTUN, &bce_tso_enable, 0,
486 "TSO Enable/Disable");
487 
488 /* Allowable values are 0 (IRQ), 1 (MSI/IRQ), and 2 (MSI-X/MSI/IRQ) */
489 /* ToDo: Add MSI-X support. */
490 static int bce_msi_enable = 1;
491 TUNABLE_INT("hw.bce.msi_enable", &bce_msi_enable);
492 SYSCTL_UINT(_hw_bce, OID_AUTO, msi_enable, CTLFLAG_RDTUN, &bce_msi_enable, 0,
493 "MSI-X|MSI|INTx selector");
494 
495 /* ToDo: Add tunable to enable/disable strict MTU handling. */
496 /* Currently allows "loose" RX MTU checking (i.e. sets the  */
497 /* H/W RX MTU to the size of the largest receive buffer, or */
498 /* 2048 bytes). This will cause a UNH failure but is more   */
499 /* desireable from a functional perspective.                */
500 
501 
502 /****************************************************************************/
503 /* Device probe function.                                                   */
504 /*                                                                          */
505 /* Compares the device to the driver's list of supported devices and        */
506 /* reports back to the OS whether this is the right driver for the device.  */
507 /*                                                                          */
508 /* Returns:                                                                 */
509 /*   BUS_PROBE_DEFAULT on success, positive value on failure.               */
510 /****************************************************************************/
511 static int
512 bce_probe(device_t dev)
513 {
514 	struct bce_type *t;
515 	struct bce_softc *sc;
516 	char *descbuf;
517 	u16 vid = 0, did = 0, svid = 0, sdid = 0;
518 
519 	t = bce_devs;
520 
521 	sc = device_get_softc(dev);
522 	bzero(sc, sizeof(struct bce_softc));
523 	sc->bce_unit = device_get_unit(dev);
524 	sc->bce_dev = dev;
525 
526 	/* Get the data for the device to be probed. */
527 	vid  = pci_get_vendor(dev);
528 	did  = pci_get_device(dev);
529 	svid = pci_get_subvendor(dev);
530 	sdid = pci_get_subdevice(dev);
531 
532 	DBPRINT(sc, BCE_EXTREME_LOAD,
533 		"%s(); VID = 0x%04X, DID = 0x%04X, SVID = 0x%04X, "
534 		"SDID = 0x%04X\n", __FUNCTION__, vid, did, svid, sdid);
535 
536 	/* Look through the list of known devices for a match. */
537 	while(t->bce_name != NULL) {
538 
539 		if ((vid == t->bce_vid) && (did == t->bce_did) &&
540 			((svid == t->bce_svid) || (t->bce_svid == PCI_ANY_ID)) &&
541 			((sdid == t->bce_sdid) || (t->bce_sdid == PCI_ANY_ID))) {
542 
543 			descbuf = malloc(BCE_DEVDESC_MAX, M_TEMP, M_NOWAIT);
544 
545 			if (descbuf == NULL)
546 				return(ENOMEM);
547 
548 			/* Print out the device identity. */
549 			snprintf(descbuf, BCE_DEVDESC_MAX, "%s (%c%d)",
550 				t->bce_name,
551 			    (((pci_read_config(dev, PCIR_REVID, 4) & 0xf0) >> 4) + 'A'),
552 			    (pci_read_config(dev, PCIR_REVID, 4) & 0xf));
553 
554 			device_set_desc_copy(dev, descbuf);
555 			free(descbuf, M_TEMP);
556 			return(BUS_PROBE_DEFAULT);
557 		}
558 		t++;
559 	}
560 
561 	return(ENXIO);
562 }
563 
564 
565 /****************************************************************************/
566 /* PCI Capabilities Probe Function.                                         */
567 /*                                                                          */
568 /* Walks the PCI capabiites list for the device to find what features are   */
569 /* supported.                                                               */
570 /*                                                                          */
571 /* Returns:                                                                 */
572 /*   None.                                                                  */
573 /****************************************************************************/
574 static void
575 bce_print_adapter_info(struct bce_softc *sc)
576 {
577 	DBENTER(BCE_VERBOSE_LOAD);
578 
579 	BCE_PRINTF("ASIC (0x%08X); ", sc->bce_chipid);
580 	printf("Rev (%c%d); ", ((BCE_CHIP_ID(sc) & 0xf000) >> 12) + 'A',
581 		((BCE_CHIP_ID(sc) & 0x0ff0) >> 4));
582 
583 	/* Bus info. */
584 	if (sc->bce_flags & BCE_PCIE_FLAG) {
585 		printf("Bus (PCIe x%d, ", sc->link_width);
586 		switch (sc->link_speed) {
587 			case 1: printf("2.5Gbps); "); break;
588 			case 2:	printf("5Gbps); "); break;
589 			default: printf("Unknown link speed); ");
590 		}
591 	} else {
592 		printf("Bus (PCI%s, %s, %dMHz); ",
593 			((sc->bce_flags & BCE_PCIX_FLAG) ? "-X" : ""),
594 			((sc->bce_flags & BCE_PCI_32BIT_FLAG) ? "32-bit" : "64-bit"),
595 			sc->bus_speed_mhz);
596 	}
597 
598 	/* Firmware version and device features. */
599 	printf("B/C (0x%08X); Flags( ", sc->bce_bc_ver);
600 #ifdef ZERO_COPY_SOCKETS
601 	printf("SPLT ");
602 #endif
603 	if (sc->bce_flags & BCE_MFW_ENABLE_FLAG)
604 		printf("MFW ");
605 	if (sc->bce_flags & BCE_USING_MSI_FLAG)
606 		printf("MSI ");
607 	if (sc->bce_flags & BCE_USING_MSIX_FLAG)
608 		printf("MSI-X ");
609 	if (sc->bce_phy_flags & BCE_PHY_2_5G_CAPABLE_FLAG)
610 		printf("2.5G ");
611 	printf(")\n");
612 
613 	DBEXIT(BCE_VERBOSE_LOAD);
614 }
615 
616 
617 /****************************************************************************/
618 /* PCI Capabilities Probe Function.                                         */
619 /*                                                                          */
620 /* Walks the PCI capabiites list for the device to find what features are   */
621 /* supported.                                                               */
622 /*                                                                          */
623 /* Returns:                                                                 */
624 /*   None.                                                                  */
625 /****************************************************************************/
626 static void
627 bce_probe_pci_caps(device_t dev, struct bce_softc *sc)
628 {
629 	u32 reg;
630 
631 	DBENTER(BCE_VERBOSE_LOAD);
632 
633 	/* Check if PCI-X capability is enabled. */
634 	if (pci_find_extcap(dev, PCIY_PCIX, &reg) == 0) {
635 		if (reg != 0)
636 			sc->bce_cap_flags |= BCE_PCIX_CAPABLE_FLAG;
637 	}
638 
639 	/* Check if PCIe capability is enabled. */
640 	if (pci_find_extcap(dev, PCIY_EXPRESS, &reg) == 0) {
641 		if (reg != 0) {
642 			u16 link_status = pci_read_config(dev, reg + 0x12, 2);
643 			DBPRINT(sc, BCE_INFO_LOAD, "PCIe link_status = 0x%08X\n",
644 				link_status);
645 			sc->link_speed = link_status & 0xf;
646 			sc->link_width = (link_status >> 4) & 0x3f;
647 			sc->bce_cap_flags |= BCE_PCIE_CAPABLE_FLAG;
648 			sc->bce_flags |= BCE_PCIE_FLAG;
649 		}
650 	}
651 
652 	/* Check if MSI capability is enabled. */
653 	if (pci_find_extcap(dev, PCIY_MSI, &reg) == 0) {
654 		if (reg != 0)
655 			sc->bce_cap_flags |= BCE_MSI_CAPABLE_FLAG;
656 	}
657 
658 	/* Check if MSI-X capability is enabled. */
659 	if (pci_find_extcap(dev, PCIY_MSIX, &reg) == 0) {
660 		if (reg != 0)
661 			sc->bce_cap_flags |= BCE_MSIX_CAPABLE_FLAG;
662 	}
663 
664 	DBEXIT(BCE_VERBOSE_LOAD);
665 }
666 
667 
668 /****************************************************************************/
669 /* Device attach function.                                                  */
670 /*                                                                          */
671 /* Allocates device resources, performs secondary chip identification,      */
672 /* resets and initializes the hardware, and initializes driver instance     */
673 /* variables.                                                               */
674 /*                                                                          */
675 /* Returns:                                                                 */
676 /*   0 on success, positive value on failure.                               */
677 /****************************************************************************/
678 static int
679 bce_attach(device_t dev)
680 {
681 	struct bce_softc *sc;
682 	struct ifnet *ifp;
683 	u32 val;
684 	int error, rid, rc = 0;
685 
686 	sc = device_get_softc(dev);
687 	sc->bce_dev = dev;
688 
689 	DBENTER(BCE_VERBOSE_LOAD | BCE_VERBOSE_RESET);
690 
691 	sc->bce_unit = device_get_unit(dev);
692 
693 	/* Set initial device and PHY flags */
694 	sc->bce_flags = 0;
695 	sc->bce_phy_flags = 0;
696 
697 	pci_enable_busmaster(dev);
698 
699 	/* Allocate PCI memory resources. */
700 	rid = PCIR_BAR(0);
701 	sc->bce_res_mem = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
702 		&rid, RF_ACTIVE);
703 
704 	if (sc->bce_res_mem == NULL) {
705 		BCE_PRINTF("%s(%d): PCI memory allocation failed\n",
706 			__FILE__, __LINE__);
707 		rc = ENXIO;
708 		goto bce_attach_fail;
709 	}
710 
711 	/* Get various resource handles. */
712 	sc->bce_btag    = rman_get_bustag(sc->bce_res_mem);
713 	sc->bce_bhandle = rman_get_bushandle(sc->bce_res_mem);
714 	sc->bce_vhandle = (vm_offset_t) rman_get_virtual(sc->bce_res_mem);
715 
716 	bce_probe_pci_caps(dev, sc);
717 
718 	rid = 1;
719 #if 0
720 	/* Try allocating MSI-X interrupts. */
721 	if ((sc->bce_cap_flags & BCE_MSIX_CAPABLE_FLAG) &&
722 		(bce_msi_enable >= 2) &&
723 		((sc->bce_res_irq = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
724 		&rid, RF_ACTIVE)) != NULL)) {
725 
726 		msi_needed = sc->bce_msi_count = 1;
727 
728 		if (((error = pci_alloc_msix(dev, &sc->bce_msi_count)) != 0) ||
729 			(sc->bce_msi_count != msi_needed)) {
730 			BCE_PRINTF("%s(%d): MSI-X allocation failed! Requested = %d,"
731 				"Received = %d, error = %d\n", __FILE__, __LINE__,
732 				msi_needed, sc->bce_msi_count, error);
733 			sc->bce_msi_count = 0;
734 			pci_release_msi(dev);
735 			bus_release_resource(dev, SYS_RES_MEMORY, rid,
736 				sc->bce_res_irq);
737 			sc->bce_res_irq = NULL;
738 		} else {
739 			DBPRINT(sc, BCE_INFO_LOAD, "%s(): Using MSI-X interrupt.\n",
740 				__FUNCTION__);
741 			sc->bce_flags |= BCE_USING_MSIX_FLAG;
742 			sc->bce_intr = bce_intr;
743 		}
744 	}
745 #endif
746 
747 	/* Try allocating a MSI interrupt. */
748 	if ((sc->bce_cap_flags & BCE_MSI_CAPABLE_FLAG) &&
749 		(bce_msi_enable >= 1) && (sc->bce_msi_count == 0)) {
750 		sc->bce_msi_count = 1;
751 		if ((error = pci_alloc_msi(dev, &sc->bce_msi_count)) != 0) {
752 			BCE_PRINTF("%s(%d): MSI allocation failed! error = %d\n",
753 				__FILE__, __LINE__, error);
754 			sc->bce_msi_count = 0;
755 			pci_release_msi(dev);
756 		} else {
757 			DBPRINT(sc, BCE_INFO_LOAD, "%s(): Using MSI interrupt.\n",
758 				__FUNCTION__);
759 			sc->bce_flags |= BCE_USING_MSI_FLAG;
760 			if ((BCE_CHIP_NUM(sc) == BCE_CHIP_NUM_5709) ||
761 				(BCE_CHIP_NUM(sc) == BCE_CHIP_NUM_5716))
762 				sc->bce_flags |= BCE_ONE_SHOT_MSI_FLAG;
763 			sc->bce_irq_rid = 1;
764 			sc->bce_intr = bce_intr;
765 		}
766 	}
767 
768 	/* Try allocating a legacy interrupt. */
769 	if (sc->bce_msi_count == 0) {
770 		DBPRINT(sc, BCE_INFO_LOAD, "%s(): Using INTx interrupt.\n",
771 			__FUNCTION__);
772 		rid = 0;
773 		sc->bce_intr = bce_intr;
774 	}
775 
776 	sc->bce_res_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ,
777 		&rid, RF_SHAREABLE | RF_ACTIVE);
778 
779 	sc->bce_irq_rid = rid;
780 
781 	/* Report any IRQ allocation errors. */
782 	if (sc->bce_res_irq == NULL) {
783 		BCE_PRINTF("%s(%d): PCI map interrupt failed!\n",
784 			__FILE__, __LINE__);
785 		rc = ENXIO;
786 		goto bce_attach_fail;
787 	}
788 
789 	/* Initialize mutex for the current device instance. */
790 	BCE_LOCK_INIT(sc, device_get_nameunit(dev));
791 
792 	/*
793 	 * Configure byte swap and enable indirect register access.
794 	 * Rely on CPU to do target byte swapping on big endian systems.
795 	 * Access to registers outside of PCI configurtion space are not
796 	 * valid until this is done.
797 	 */
798 	pci_write_config(dev, BCE_PCICFG_MISC_CONFIG,
799 			       BCE_PCICFG_MISC_CONFIG_REG_WINDOW_ENA |
800 			       BCE_PCICFG_MISC_CONFIG_TARGET_MB_WORD_SWAP, 4);
801 
802 	/* Save ASIC revsion info. */
803 	sc->bce_chipid =  REG_RD(sc, BCE_MISC_ID);
804 
805 	/* Weed out any non-production controller revisions. */
806 	switch(BCE_CHIP_ID(sc)) {
807 		case BCE_CHIP_ID_5706_A0:
808 		case BCE_CHIP_ID_5706_A1:
809 		case BCE_CHIP_ID_5708_A0:
810 		case BCE_CHIP_ID_5708_B0:
811 		case BCE_CHIP_ID_5709_A0:
812 		case BCE_CHIP_ID_5709_B0:
813 		case BCE_CHIP_ID_5709_B1:
814 		case BCE_CHIP_ID_5709_B2:
815 			BCE_PRINTF("%s(%d): Unsupported controller revision (%c%d)!\n",
816 				__FILE__, __LINE__,
817 				(((pci_read_config(dev, PCIR_REVID, 4) & 0xf0) >> 4) + 'A'),
818 			    (pci_read_config(dev, PCIR_REVID, 4) & 0xf));
819 			rc = ENODEV;
820 			goto bce_attach_fail;
821 	}
822 
823 	/*
824 	 * The embedded PCIe to PCI-X bridge (EPB)
825 	 * in the 5708 cannot address memory above
826 	 * 40 bits (E7_5708CB1_23043 & E6_5708SB1_23043).
827 	 */
828 	if (BCE_CHIP_NUM(sc) == BCE_CHIP_NUM_5708)
829 		sc->max_bus_addr = BCE_BUS_SPACE_MAXADDR;
830 	else
831 		sc->max_bus_addr = BUS_SPACE_MAXADDR;
832 
833 	/*
834 	 * Find the base address for shared memory access.
835 	 * Newer versions of bootcode use a signature and offset
836 	 * while older versions use a fixed address.
837 	 */
838 	val = REG_RD_IND(sc, BCE_SHM_HDR_SIGNATURE);
839 	if ((val & BCE_SHM_HDR_SIGNATURE_SIG_MASK) == BCE_SHM_HDR_SIGNATURE_SIG)
840 		/* Multi-port devices use different offsets in shared memory. */
841 		sc->bce_shmem_base = REG_RD_IND(sc, BCE_SHM_HDR_ADDR_0 +
842 			(pci_get_function(sc->bce_dev) << 2));
843 	else
844 		sc->bce_shmem_base = HOST_VIEW_SHMEM_BASE;
845 
846 	DBPRINT(sc, BCE_VERBOSE_FIRMWARE, "%s(): bce_shmem_base = 0x%08X\n",
847 		__FUNCTION__, sc->bce_shmem_base);
848 
849 	/* Fetch the bootcode revision. */
850 	sc->bce_bc_ver = REG_RD_IND(sc, sc->bce_shmem_base +
851 		BCE_DEV_INFO_BC_REV);
852 
853 	/* Check if any management firmware is running. */
854 	val = REG_RD_IND(sc, sc->bce_shmem_base + BCE_PORT_FEATURE);
855 	if (val & (BCE_PORT_FEATURE_ASF_ENABLED | BCE_PORT_FEATURE_IMD_ENABLED))
856 		sc->bce_flags |= BCE_MFW_ENABLE_FLAG;
857 
858 	/* Get PCI bus information (speed and type). */
859 	val = REG_RD(sc, BCE_PCICFG_MISC_STATUS);
860 	if (val & BCE_PCICFG_MISC_STATUS_PCIX_DET) {
861 		u32 clkreg;
862 
863 		sc->bce_flags |= BCE_PCIX_FLAG;
864 
865 		clkreg = REG_RD(sc, BCE_PCICFG_PCI_CLOCK_CONTROL_BITS);
866 
867 		clkreg &= BCE_PCICFG_PCI_CLOCK_CONTROL_BITS_PCI_CLK_SPD_DET;
868 		switch (clkreg) {
869 		case BCE_PCICFG_PCI_CLOCK_CONTROL_BITS_PCI_CLK_SPD_DET_133MHZ:
870 			sc->bus_speed_mhz = 133;
871 			break;
872 
873 		case BCE_PCICFG_PCI_CLOCK_CONTROL_BITS_PCI_CLK_SPD_DET_95MHZ:
874 			sc->bus_speed_mhz = 100;
875 			break;
876 
877 		case BCE_PCICFG_PCI_CLOCK_CONTROL_BITS_PCI_CLK_SPD_DET_66MHZ:
878 		case BCE_PCICFG_PCI_CLOCK_CONTROL_BITS_PCI_CLK_SPD_DET_80MHZ:
879 			sc->bus_speed_mhz = 66;
880 			break;
881 
882 		case BCE_PCICFG_PCI_CLOCK_CONTROL_BITS_PCI_CLK_SPD_DET_48MHZ:
883 		case BCE_PCICFG_PCI_CLOCK_CONTROL_BITS_PCI_CLK_SPD_DET_55MHZ:
884 			sc->bus_speed_mhz = 50;
885 			break;
886 
887 		case BCE_PCICFG_PCI_CLOCK_CONTROL_BITS_PCI_CLK_SPD_DET_LOW:
888 		case BCE_PCICFG_PCI_CLOCK_CONTROL_BITS_PCI_CLK_SPD_DET_32MHZ:
889 		case BCE_PCICFG_PCI_CLOCK_CONTROL_BITS_PCI_CLK_SPD_DET_38MHZ:
890 			sc->bus_speed_mhz = 33;
891 			break;
892 		}
893 	} else {
894 		if (val & BCE_PCICFG_MISC_STATUS_M66EN)
895 			sc->bus_speed_mhz = 66;
896 		else
897 			sc->bus_speed_mhz = 33;
898 	}
899 
900 	if (val & BCE_PCICFG_MISC_STATUS_32BIT_DET)
901 		sc->bce_flags |= BCE_PCI_32BIT_FLAG;
902 
903 	/* Reset the controller and announce to bootcode that driver is present. */
904 	if (bce_reset(sc, BCE_DRV_MSG_CODE_RESET)) {
905 		BCE_PRINTF("%s(%d): Controller reset failed!\n",
906 			__FILE__, __LINE__);
907 		rc = ENXIO;
908 		goto bce_attach_fail;
909 	}
910 
911 	/* Initialize the controller. */
912 	if (bce_chipinit(sc)) {
913 		BCE_PRINTF("%s(%d): Controller initialization failed!\n",
914 			__FILE__, __LINE__);
915 		rc = ENXIO;
916 		goto bce_attach_fail;
917 	}
918 
919 	/* Perform NVRAM test. */
920 	if (bce_nvram_test(sc)) {
921 		BCE_PRINTF("%s(%d): NVRAM test failed!\n",
922 			__FILE__, __LINE__);
923 		rc = ENXIO;
924 		goto bce_attach_fail;
925 	}
926 
927 	/* Fetch the permanent Ethernet MAC address. */
928 	bce_get_mac_addr(sc);
929 
930 	/*
931 	 * Trip points control how many BDs
932 	 * should be ready before generating an
933 	 * interrupt while ticks control how long
934 	 * a BD can sit in the chain before
935 	 * generating an interrupt.  Set the default
936 	 * values for the RX and TX chains.
937 	 */
938 
939 #ifdef BCE_DEBUG
940 	/* Force more frequent interrupts. */
941 	sc->bce_tx_quick_cons_trip_int = 1;
942 	sc->bce_tx_quick_cons_trip     = 1;
943 	sc->bce_tx_ticks_int           = 0;
944 	sc->bce_tx_ticks               = 0;
945 
946 	sc->bce_rx_quick_cons_trip_int = 1;
947 	sc->bce_rx_quick_cons_trip     = 1;
948 	sc->bce_rx_ticks_int           = 0;
949 	sc->bce_rx_ticks               = 0;
950 #else
951 	/* Improve throughput at the expense of increased latency. */
952 	sc->bce_tx_quick_cons_trip_int = 20;
953 	sc->bce_tx_quick_cons_trip     = 20;
954 	sc->bce_tx_ticks_int           = 80;
955 	sc->bce_tx_ticks               = 80;
956 
957 	sc->bce_rx_quick_cons_trip_int = 6;
958 	sc->bce_rx_quick_cons_trip     = 6;
959 	sc->bce_rx_ticks_int           = 18;
960 	sc->bce_rx_ticks               = 18;
961 #endif
962 
963 	/* Update statistics once every second. */
964 	sc->bce_stats_ticks = 1000000 & 0xffff00;
965 
966 	/* Find the media type for the adapter. */
967 	bce_get_media(sc);
968 
969 	/* Store data needed by PHY driver for backplane applications */
970 	sc->bce_shared_hw_cfg = REG_RD_IND(sc, sc->bce_shmem_base +
971 		BCE_SHARED_HW_CFG_CONFIG);
972 	sc->bce_port_hw_cfg   = REG_RD_IND(sc, sc->bce_shmem_base +
973 		BCE_PORT_HW_CFG_CONFIG);
974 
975 	/* Allocate DMA memory resources. */
976 	if (bce_dma_alloc(dev)) {
977 		BCE_PRINTF("%s(%d): DMA resource allocation failed!\n",
978 		    __FILE__, __LINE__);
979 		rc = ENXIO;
980 		goto bce_attach_fail;
981 	}
982 
983 	/* Allocate an ifnet structure. */
984 	ifp = sc->bce_ifp = if_alloc(IFT_ETHER);
985 	if (ifp == NULL) {
986 		BCE_PRINTF("%s(%d): Interface allocation failed!\n",
987 			__FILE__, __LINE__);
988 		rc = ENXIO;
989 		goto bce_attach_fail;
990 	}
991 
992 	/* Initialize the ifnet interface. */
993 	ifp->if_softc        = sc;
994 	if_initname(ifp, device_get_name(dev), device_get_unit(dev));
995 	ifp->if_flags        = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
996 	ifp->if_ioctl        = bce_ioctl;
997 	ifp->if_start        = bce_start;
998 	ifp->if_init         = bce_init;
999 	ifp->if_mtu          = ETHERMTU;
1000 
1001 	if (bce_tso_enable) {
1002 		ifp->if_hwassist = BCE_IF_HWASSIST | CSUM_TSO;
1003 		ifp->if_capabilities = BCE_IF_CAPABILITIES | IFCAP_TSO4;
1004 	} else {
1005 		ifp->if_hwassist = BCE_IF_HWASSIST;
1006 		ifp->if_capabilities = BCE_IF_CAPABILITIES;
1007 	}
1008 
1009 	ifp->if_capenable    = ifp->if_capabilities;
1010 
1011 	/*
1012 	 * Assume standard mbuf sizes for buffer allocation.
1013 	 * This may change later if the MTU size is set to
1014 	 * something other than 1500.
1015 	 */
1016 #ifdef ZERO_COPY_SOCKETS
1017 	sc->rx_bd_mbuf_alloc_size = MHLEN;
1018 	/* Make sure offset is 16 byte aligned for hardware. */
1019 	sc->rx_bd_mbuf_align_pad  = roundup2((MSIZE - MHLEN), 16) -
1020 		(MSIZE - MHLEN);
1021 	sc->rx_bd_mbuf_data_len   = sc->rx_bd_mbuf_alloc_size -
1022 		sc->rx_bd_mbuf_align_pad;
1023 	sc->pg_bd_mbuf_alloc_size = MCLBYTES;
1024 #else
1025 	sc->rx_bd_mbuf_alloc_size = MCLBYTES;
1026 	sc->rx_bd_mbuf_align_pad  = roundup2(MCLBYTES, 16) - MCLBYTES;
1027 	sc->rx_bd_mbuf_data_len   = sc->rx_bd_mbuf_alloc_size -
1028 		sc->rx_bd_mbuf_align_pad;
1029 #endif
1030 
1031 	ifp->if_snd.ifq_drv_maxlen = USABLE_TX_BD;
1032 	IFQ_SET_MAXLEN(&ifp->if_snd, ifp->if_snd.ifq_drv_maxlen);
1033 	IFQ_SET_READY(&ifp->if_snd);
1034 
1035 	if (sc->bce_phy_flags & BCE_PHY_2_5G_CAPABLE_FLAG)
1036 		ifp->if_baudrate = IF_Mbps(2500ULL);
1037 	else
1038 		ifp->if_baudrate = IF_Mbps(1000);
1039 
1040 	/* Check for an MII child bus by probing the PHY. */
1041 	if (mii_phy_probe(dev, &sc->bce_miibus, bce_ifmedia_upd,
1042 		bce_ifmedia_sts)) {
1043 		BCE_PRINTF("%s(%d): No PHY found on child MII bus!\n",
1044 			__FILE__, __LINE__);
1045 		rc = ENXIO;
1046 		goto bce_attach_fail;
1047 	}
1048 
1049 	/* Attach to the Ethernet interface list. */
1050 	ether_ifattach(ifp, sc->eaddr);
1051 
1052 #if __FreeBSD_version < 500000
1053 	callout_init(&sc->bce_tick_callout);
1054 	callout_init(&sc->bce_pulse_callout);
1055 #else
1056 	callout_init_mtx(&sc->bce_tick_callout, &sc->bce_mtx, 0);
1057 	callout_init_mtx(&sc->bce_pulse_callout, &sc->bce_mtx, 0);
1058 #endif
1059 
1060 	/* Hookup IRQ last. */
1061 	rc = bus_setup_intr(dev, sc->bce_res_irq, INTR_TYPE_NET | INTR_MPSAFE,
1062 		NULL, bce_intr, sc, &sc->bce_intrhand);
1063 
1064 	if (rc) {
1065 		BCE_PRINTF("%s(%d): Failed to setup IRQ!\n",
1066 			__FILE__, __LINE__);
1067 		bce_detach(dev);
1068 		goto bce_attach_exit;
1069 	}
1070 
1071 	/*
1072 	 * At this point we've acquired all the resources
1073 	 * we need to run so there's no turning back, we're
1074 	 * cleared for launch.
1075 	 */
1076 
1077 	/* Print some important debugging info. */
1078 	DBRUNMSG(BCE_INFO, bce_dump_driver_state(sc));
1079 
1080 	/* Add the supported sysctls to the kernel. */
1081 	bce_add_sysctls(sc);
1082 
1083 	BCE_LOCK(sc);
1084 
1085 	/*
1086 	 * The chip reset earlier notified the bootcode that
1087 	 * a driver is present.  We now need to start our pulse
1088 	 * routine so that the bootcode is reminded that we're
1089 	 * still running.
1090 	 */
1091 	bce_pulse(sc);
1092 
1093 	bce_mgmt_init_locked(sc);
1094 	BCE_UNLOCK(sc);
1095 
1096 	/* Finally, print some useful adapter info */
1097 	bce_print_adapter_info(sc);
1098 	DBPRINT(sc, BCE_FATAL, "%s(): sc = %p\n",
1099 		__FUNCTION__, sc);
1100 
1101 	goto bce_attach_exit;
1102 
1103 bce_attach_fail:
1104 	bce_release_resources(sc);
1105 
1106 bce_attach_exit:
1107 
1108 	DBEXIT(BCE_VERBOSE_LOAD | BCE_VERBOSE_RESET);
1109 
1110 	return(rc);
1111 }
1112 
1113 
1114 /****************************************************************************/
1115 /* Device detach function.                                                  */
1116 /*                                                                          */
1117 /* Stops the controller, resets the controller, and releases resources.     */
1118 /*                                                                          */
1119 /* Returns:                                                                 */
1120 /*   0 on success, positive value on failure.                               */
1121 /****************************************************************************/
1122 static int
1123 bce_detach(device_t dev)
1124 {
1125 	struct bce_softc *sc = device_get_softc(dev);
1126 	struct ifnet *ifp;
1127 	u32 msg;
1128 
1129 	DBENTER(BCE_VERBOSE_UNLOAD | BCE_VERBOSE_RESET);
1130 
1131 	ifp = sc->bce_ifp;
1132 
1133 	/* Stop and reset the controller. */
1134 	BCE_LOCK(sc);
1135 
1136 	/* Stop the pulse so the bootcode can go to driver absent state. */
1137 	callout_stop(&sc->bce_pulse_callout);
1138 
1139 	bce_stop(sc);
1140 	if (sc->bce_flags & BCE_NO_WOL_FLAG)
1141 		msg = BCE_DRV_MSG_CODE_UNLOAD_LNK_DN;
1142 	else
1143 		msg = BCE_DRV_MSG_CODE_UNLOAD;
1144 	bce_reset(sc, msg);
1145 
1146 	BCE_UNLOCK(sc);
1147 
1148 	ether_ifdetach(ifp);
1149 
1150 	/* If we have a child device on the MII bus remove it too. */
1151 	bus_generic_detach(dev);
1152 	device_delete_child(dev, sc->bce_miibus);
1153 
1154 	/* Release all remaining resources. */
1155 	bce_release_resources(sc);
1156 
1157 	DBEXIT(BCE_VERBOSE_UNLOAD | BCE_VERBOSE_RESET);
1158 
1159 	return(0);
1160 }
1161 
1162 
1163 /****************************************************************************/
1164 /* Device shutdown function.                                                */
1165 /*                                                                          */
1166 /* Stops and resets the controller.                                         */
1167 /*                                                                          */
1168 /* Returns:                                                                 */
1169 /*   0 on success, positive value on failure.                               */
1170 /****************************************************************************/
1171 static int
1172 bce_shutdown(device_t dev)
1173 {
1174 	struct bce_softc *sc = device_get_softc(dev);
1175 	u32 msg;
1176 
1177 	DBENTER(BCE_VERBOSE);
1178 
1179 	BCE_LOCK(sc);
1180 	bce_stop(sc);
1181 	if (sc->bce_flags & BCE_NO_WOL_FLAG)
1182 		msg = BCE_DRV_MSG_CODE_UNLOAD_LNK_DN;
1183 	else
1184 		msg = BCE_DRV_MSG_CODE_UNLOAD;
1185 	bce_reset(sc, msg);
1186 	BCE_UNLOCK(sc);
1187 
1188 	DBEXIT(BCE_VERBOSE);
1189 
1190 	return (0);
1191 }
1192 
1193 
1194 #ifdef BCE_DEBUG
1195 /****************************************************************************/
1196 /* Register read.                                                           */
1197 /*                                                                          */
1198 /* Returns:                                                                 */
1199 /*   The value of the register.                                             */
1200 /****************************************************************************/
1201 static u32
1202 bce_reg_rd(struct bce_softc *sc, u32 offset)
1203 {
1204 	u32 val = bus_space_read_4(sc->bce_btag, sc->bce_bhandle, offset);
1205 	DBPRINT(sc, BCE_INSANE_REG, "%s(); offset = 0x%08X, val = 0x%08X\n",
1206 		__FUNCTION__, offset, val);
1207 	return val;
1208 }
1209 
1210 
1211 /****************************************************************************/
1212 /* Register write (16 bit).                                                 */
1213 /*                                                                          */
1214 /* Returns:                                                                 */
1215 /*   Nothing.                                                               */
1216 /****************************************************************************/
1217 static void
1218 bce_reg_wr16(struct bce_softc *sc, u32 offset, u16 val)
1219 {
1220 	DBPRINT(sc, BCE_INSANE_REG, "%s(); offset = 0x%08X, val = 0x%04X\n",
1221 		__FUNCTION__, offset, val);
1222 	bus_space_write_2(sc->bce_btag, sc->bce_bhandle, offset, val);
1223 }
1224 
1225 
1226 /****************************************************************************/
1227 /* Register write.                                                          */
1228 /*                                                                          */
1229 /* Returns:                                                                 */
1230 /*   Nothing.                                                               */
1231 /****************************************************************************/
1232 static void
1233 bce_reg_wr(struct bce_softc *sc, u32 offset, u32 val)
1234 {
1235 	DBPRINT(sc, BCE_INSANE_REG, "%s(); offset = 0x%08X, val = 0x%08X\n",
1236 		__FUNCTION__, offset, val);
1237 	bus_space_write_4(sc->bce_btag, sc->bce_bhandle, offset, val);
1238 }
1239 #endif
1240 
1241 /****************************************************************************/
1242 /* Indirect register read.                                                  */
1243 /*                                                                          */
1244 /* Reads NetXtreme II registers using an index/data register pair in PCI    */
1245 /* configuration space.  Using this mechanism avoids issues with posted     */
1246 /* reads but is much slower than memory-mapped I/O.                         */
1247 /*                                                                          */
1248 /* Returns:                                                                 */
1249 /*   The value of the register.                                             */
1250 /****************************************************************************/
1251 static u32
1252 bce_reg_rd_ind(struct bce_softc *sc, u32 offset)
1253 {
1254 	device_t dev;
1255 	dev = sc->bce_dev;
1256 
1257 	pci_write_config(dev, BCE_PCICFG_REG_WINDOW_ADDRESS, offset, 4);
1258 #ifdef BCE_DEBUG
1259 	{
1260 		u32 val;
1261 		val = pci_read_config(dev, BCE_PCICFG_REG_WINDOW, 4);
1262 		DBPRINT(sc, BCE_INSANE_REG, "%s(); offset = 0x%08X, val = 0x%08X\n",
1263 			__FUNCTION__, offset, val);
1264 		return val;
1265 	}
1266 #else
1267 	return pci_read_config(dev, BCE_PCICFG_REG_WINDOW, 4);
1268 #endif
1269 }
1270 
1271 
1272 /****************************************************************************/
1273 /* Indirect register write.                                                 */
1274 /*                                                                          */
1275 /* Writes NetXtreme II registers using an index/data register pair in PCI   */
1276 /* configuration space.  Using this mechanism avoids issues with posted     */
1277 /* writes but is muchh slower than memory-mapped I/O.                       */
1278 /*                                                                          */
1279 /* Returns:                                                                 */
1280 /*   Nothing.                                                               */
1281 /****************************************************************************/
1282 static void
1283 bce_reg_wr_ind(struct bce_softc *sc, u32 offset, u32 val)
1284 {
1285 	device_t dev;
1286 	dev = sc->bce_dev;
1287 
1288 	DBPRINT(sc, BCE_INSANE_REG, "%s(); offset = 0x%08X, val = 0x%08X\n",
1289 		__FUNCTION__, offset, val);
1290 
1291 	pci_write_config(dev, BCE_PCICFG_REG_WINDOW_ADDRESS, offset, 4);
1292 	pci_write_config(dev, BCE_PCICFG_REG_WINDOW, val, 4);
1293 }
1294 
1295 
1296 #ifdef BCE_DEBUG
1297 /****************************************************************************/
1298 /* Context memory read.                                                     */
1299 /*                                                                          */
1300 /* The NetXtreme II controller uses context memory to track connection      */
1301 /* information for L2 and higher network protocols.                         */
1302 /*                                                                          */
1303 /* Returns:                                                                 */
1304 /*   The requested 32 bit value of context memory.                          */
1305 /****************************************************************************/
1306 static u32
1307 bce_ctx_rd(struct bce_softc *sc, u32 cid_addr, u32 ctx_offset)
1308 {
1309 	u32 idx, offset, retry_cnt = 5, val;
1310 
1311 	DBRUNIF((cid_addr > MAX_CID_ADDR || ctx_offset & 0x3 || cid_addr & CTX_MASK),
1312 		BCE_PRINTF("%s(): Invalid CID address: 0x%08X.\n",
1313 			__FUNCTION__, cid_addr));
1314 
1315 	offset = ctx_offset + cid_addr;
1316 
1317 	if ((BCE_CHIP_NUM(sc) == BCE_CHIP_NUM_5709) ||
1318 		(BCE_CHIP_NUM(sc) == BCE_CHIP_NUM_5716)) {
1319 
1320 		REG_WR(sc, BCE_CTX_CTX_CTRL, (offset | BCE_CTX_CTX_CTRL_READ_REQ));
1321 
1322 		for (idx = 0; idx < retry_cnt; idx++) {
1323 			val = REG_RD(sc, BCE_CTX_CTX_CTRL);
1324 			if ((val & BCE_CTX_CTX_CTRL_READ_REQ) == 0)
1325 				break;
1326 			DELAY(5);
1327 		}
1328 
1329 		if (val & BCE_CTX_CTX_CTRL_READ_REQ)
1330 			BCE_PRINTF("%s(%d); Unable to read CTX memory: "
1331 				"cid_addr = 0x%08X, offset = 0x%08X!\n",
1332 				__FILE__, __LINE__, cid_addr, ctx_offset);
1333 
1334 		val = REG_RD(sc, BCE_CTX_CTX_DATA);
1335 	} else {
1336 		REG_WR(sc, BCE_CTX_DATA_ADR, offset);
1337 		val = REG_RD(sc, BCE_CTX_DATA);
1338 	}
1339 
1340 	DBPRINT(sc, BCE_EXTREME_CTX, "%s(); cid_addr = 0x%08X, offset = 0x%08X, "
1341 		"val = 0x%08X\n", __FUNCTION__, cid_addr, ctx_offset, val);
1342 
1343 	return(val);
1344 }
1345 #endif
1346 
1347 
1348 /****************************************************************************/
1349 /* Context memory write.                                                    */
1350 /*                                                                          */
1351 /* The NetXtreme II controller uses context memory to track connection      */
1352 /* information for L2 and higher network protocols.                         */
1353 /*                                                                          */
1354 /* Returns:                                                                 */
1355 /*   Nothing.                                                               */
1356 /****************************************************************************/
1357 static void
1358 bce_ctx_wr(struct bce_softc *sc, u32 cid_addr, u32 ctx_offset, u32 ctx_val)
1359 {
1360 	u32 idx, offset = ctx_offset + cid_addr;
1361 	u32 val, retry_cnt = 5;
1362 
1363 	DBPRINT(sc, BCE_EXTREME_CTX, "%s(); cid_addr = 0x%08X, offset = 0x%08X, "
1364 		"val = 0x%08X\n", __FUNCTION__, cid_addr, ctx_offset, ctx_val);
1365 
1366 	DBRUNIF((cid_addr > MAX_CID_ADDR || ctx_offset & 0x3 || cid_addr & CTX_MASK),
1367 		BCE_PRINTF("%s(): Invalid CID address: 0x%08X.\n",
1368 			__FUNCTION__, cid_addr));
1369 
1370 	if ((BCE_CHIP_NUM(sc) == BCE_CHIP_NUM_5709) ||
1371 		(BCE_CHIP_NUM(sc) == BCE_CHIP_NUM_5716)) {
1372 
1373 		REG_WR(sc, BCE_CTX_CTX_DATA, ctx_val);
1374 		REG_WR(sc, BCE_CTX_CTX_CTRL, (offset | BCE_CTX_CTX_CTRL_WRITE_REQ));
1375 
1376 		for (idx = 0; idx < retry_cnt; idx++) {
1377 			val = REG_RD(sc, BCE_CTX_CTX_CTRL);
1378 			if ((val & BCE_CTX_CTX_CTRL_WRITE_REQ) == 0)
1379 				break;
1380 			DELAY(5);
1381 		}
1382 
1383 		if (val & BCE_CTX_CTX_CTRL_WRITE_REQ)
1384 			BCE_PRINTF("%s(%d); Unable to write CTX memory: "
1385 				"cid_addr = 0x%08X, offset = 0x%08X!\n",
1386 				__FILE__, __LINE__, cid_addr, ctx_offset);
1387 
1388 	} else {
1389 		REG_WR(sc, BCE_CTX_DATA_ADR, offset);
1390 		REG_WR(sc, BCE_CTX_DATA, ctx_val);
1391 	}
1392 }
1393 
1394 
1395 /****************************************************************************/
1396 /* PHY register read.                                                       */
1397 /*                                                                          */
1398 /* Implements register reads on the MII bus.                                */
1399 /*                                                                          */
1400 /* Returns:                                                                 */
1401 /*   The value of the register.                                             */
1402 /****************************************************************************/
1403 static int
1404 bce_miibus_read_reg(device_t dev, int phy, int reg)
1405 {
1406 	struct bce_softc *sc;
1407 	u32 val;
1408 	int i;
1409 
1410 	sc = device_get_softc(dev);
1411 
1412 	/* Make sure we are accessing the correct PHY address. */
1413 	if (phy != sc->bce_phy_addr) {
1414 		DBPRINT(sc, BCE_INSANE_PHY, "Invalid PHY address %d for PHY read!\n", phy);
1415 		return(0);
1416 	}
1417 
1418 	if (sc->bce_phy_flags & BCE_PHY_INT_MODE_AUTO_POLLING_FLAG) {
1419 		val = REG_RD(sc, BCE_EMAC_MDIO_MODE);
1420 		val &= ~BCE_EMAC_MDIO_MODE_AUTO_POLL;
1421 
1422 		REG_WR(sc, BCE_EMAC_MDIO_MODE, val);
1423 		REG_RD(sc, BCE_EMAC_MDIO_MODE);
1424 
1425 		DELAY(40);
1426 	}
1427 
1428 
1429 	val = BCE_MIPHY(phy) | BCE_MIREG(reg) |
1430 		BCE_EMAC_MDIO_COMM_COMMAND_READ | BCE_EMAC_MDIO_COMM_DISEXT |
1431 		BCE_EMAC_MDIO_COMM_START_BUSY;
1432 	REG_WR(sc, BCE_EMAC_MDIO_COMM, val);
1433 
1434 	for (i = 0; i < BCE_PHY_TIMEOUT; i++) {
1435 		DELAY(10);
1436 
1437 		val = REG_RD(sc, BCE_EMAC_MDIO_COMM);
1438 		if (!(val & BCE_EMAC_MDIO_COMM_START_BUSY)) {
1439 			DELAY(5);
1440 
1441 			val = REG_RD(sc, BCE_EMAC_MDIO_COMM);
1442 			val &= BCE_EMAC_MDIO_COMM_DATA;
1443 
1444 			break;
1445 		}
1446 	}
1447 
1448 	if (val & BCE_EMAC_MDIO_COMM_START_BUSY) {
1449 		BCE_PRINTF("%s(%d): Error: PHY read timeout! phy = %d, reg = 0x%04X\n",
1450 			__FILE__, __LINE__, phy, reg);
1451 		val = 0x0;
1452 	} else {
1453 		val = REG_RD(sc, BCE_EMAC_MDIO_COMM);
1454 	}
1455 
1456 
1457 	if (sc->bce_phy_flags & BCE_PHY_INT_MODE_AUTO_POLLING_FLAG) {
1458 		val = REG_RD(sc, BCE_EMAC_MDIO_MODE);
1459 		val |= BCE_EMAC_MDIO_MODE_AUTO_POLL;
1460 
1461 		REG_WR(sc, BCE_EMAC_MDIO_MODE, val);
1462 		REG_RD(sc, BCE_EMAC_MDIO_MODE);
1463 
1464 		DELAY(40);
1465 	}
1466 
1467 	DB_PRINT_PHY_REG(reg, val);
1468 	return (val & 0xffff);
1469 
1470 }
1471 
1472 
1473 /****************************************************************************/
1474 /* PHY register write.                                                      */
1475 /*                                                                          */
1476 /* Implements register writes on the MII bus.                               */
1477 /*                                                                          */
1478 /* Returns:                                                                 */
1479 /*   The value of the register.                                             */
1480 /****************************************************************************/
1481 static int
1482 bce_miibus_write_reg(device_t dev, int phy, int reg, int val)
1483 {
1484 	struct bce_softc *sc;
1485 	u32 val1;
1486 	int i;
1487 
1488 	sc = device_get_softc(dev);
1489 
1490 	/* Make sure we are accessing the correct PHY address. */
1491 	if (phy != sc->bce_phy_addr) {
1492 		DBPRINT(sc, BCE_INSANE_PHY, "Invalid PHY address %d for PHY write!\n", phy);
1493 		return(0);
1494 	}
1495 
1496 	DB_PRINT_PHY_REG(reg, val);
1497 
1498 	if (sc->bce_phy_flags & BCE_PHY_INT_MODE_AUTO_POLLING_FLAG) {
1499 		val1 = REG_RD(sc, BCE_EMAC_MDIO_MODE);
1500 		val1 &= ~BCE_EMAC_MDIO_MODE_AUTO_POLL;
1501 
1502 		REG_WR(sc, BCE_EMAC_MDIO_MODE, val1);
1503 		REG_RD(sc, BCE_EMAC_MDIO_MODE);
1504 
1505 		DELAY(40);
1506 	}
1507 
1508 	val1 = BCE_MIPHY(phy) | BCE_MIREG(reg) | val |
1509 		BCE_EMAC_MDIO_COMM_COMMAND_WRITE |
1510 		BCE_EMAC_MDIO_COMM_START_BUSY | BCE_EMAC_MDIO_COMM_DISEXT;
1511 	REG_WR(sc, BCE_EMAC_MDIO_COMM, val1);
1512 
1513 	for (i = 0; i < BCE_PHY_TIMEOUT; i++) {
1514 		DELAY(10);
1515 
1516 		val1 = REG_RD(sc, BCE_EMAC_MDIO_COMM);
1517 		if (!(val1 & BCE_EMAC_MDIO_COMM_START_BUSY)) {
1518 			DELAY(5);
1519 			break;
1520 		}
1521 	}
1522 
1523 	if (val1 & BCE_EMAC_MDIO_COMM_START_BUSY)
1524 		BCE_PRINTF("%s(%d): PHY write timeout!\n",
1525 			__FILE__, __LINE__);
1526 
1527 	if (sc->bce_phy_flags & BCE_PHY_INT_MODE_AUTO_POLLING_FLAG) {
1528 		val1 = REG_RD(sc, BCE_EMAC_MDIO_MODE);
1529 		val1 |= BCE_EMAC_MDIO_MODE_AUTO_POLL;
1530 
1531 		REG_WR(sc, BCE_EMAC_MDIO_MODE, val1);
1532 		REG_RD(sc, BCE_EMAC_MDIO_MODE);
1533 
1534 		DELAY(40);
1535 	}
1536 
1537 	return 0;
1538 }
1539 
1540 
1541 /****************************************************************************/
1542 /* MII bus status change.                                                   */
1543 /*                                                                          */
1544 /* Called by the MII bus driver when the PHY establishes link to set the    */
1545 /* MAC interface registers.                                                 */
1546 /*                                                                          */
1547 /* Returns:                                                                 */
1548 /*   Nothing.                                                               */
1549 /****************************************************************************/
1550 static void
1551 bce_miibus_statchg(device_t dev)
1552 {
1553 	struct bce_softc *sc;
1554 	struct mii_data *mii;
1555 	int val;
1556 
1557 	sc = device_get_softc(dev);
1558 
1559 	DBENTER(BCE_VERBOSE_PHY);
1560 
1561 	mii = device_get_softc(sc->bce_miibus);
1562 
1563 	val = REG_RD(sc, BCE_EMAC_MODE);
1564 	val &= ~(BCE_EMAC_MODE_PORT | BCE_EMAC_MODE_HALF_DUPLEX |
1565 		BCE_EMAC_MODE_MAC_LOOP | BCE_EMAC_MODE_FORCE_LINK |
1566 		BCE_EMAC_MODE_25G);
1567 
1568 	/* Set MII or GMII interface based on the speed negotiated by the PHY. */
1569 	switch (IFM_SUBTYPE(mii->mii_media_active)) {
1570 	case IFM_10_T:
1571 		if (BCE_CHIP_NUM(sc) != BCE_CHIP_NUM_5706) {
1572 			DBPRINT(sc, BCE_INFO, "Enabling 10Mb interface.\n");
1573 			val |= BCE_EMAC_MODE_PORT_MII_10;
1574 			break;
1575 		}
1576 		/* fall-through */
1577 	case IFM_100_TX:
1578 		DBPRINT(sc, BCE_INFO, "Enabling MII interface.\n");
1579 		val |= BCE_EMAC_MODE_PORT_MII;
1580 		break;
1581 	case IFM_2500_SX:
1582 		DBPRINT(sc, BCE_INFO, "Enabling 2.5G MAC mode.\n");
1583 		val |= BCE_EMAC_MODE_25G;
1584 		/* fall-through */
1585 	case IFM_1000_T:
1586 	case IFM_1000_SX:
1587 		DBPRINT(sc, BCE_INFO, "Enabling GMII interface.\n");
1588 		val |= BCE_EMAC_MODE_PORT_GMII;
1589 		break;
1590 	default:
1591 		DBPRINT(sc, BCE_INFO, "Unknown speed, enabling default GMII "
1592 			"interface.\n");
1593 		val |= BCE_EMAC_MODE_PORT_GMII;
1594 	}
1595 
1596 	/* Set half or full duplex based on the duplicity negotiated by the PHY. */
1597 	if ((mii->mii_media_active & IFM_GMASK) == IFM_HDX) {
1598 		DBPRINT(sc, BCE_INFO, "Setting Half-Duplex interface.\n");
1599 		val |= BCE_EMAC_MODE_HALF_DUPLEX;
1600 	} else
1601 		DBPRINT(sc, BCE_INFO, "Setting Full-Duplex interface.\n");
1602 
1603 	REG_WR(sc, BCE_EMAC_MODE, val);
1604 
1605 #if 0
1606 	/* ToDo: Enable flow control support in brgphy and bge. */
1607 	/* FLAG0 is set if RX is enabled and FLAG1 if TX is enabled */
1608 	if (mii->mii_media_active & IFM_FLAG0)
1609 		BCE_SETBIT(sc, BCE_EMAC_RX_MODE, BCE_EMAC_RX_MODE_FLOW_EN);
1610 	if (mii->mii_media_active & IFM_FLAG1)
1611 		BCE_SETBIT(sc, BCE_EMAC_RX_MODE, BCE_EMAC_TX_MODE_FLOW_EN);
1612 #endif
1613 
1614 	DBEXIT(BCE_VERBOSE_PHY);
1615 }
1616 
1617 
1618 /****************************************************************************/
1619 /* Acquire NVRAM lock.                                                      */
1620 /*                                                                          */
1621 /* Before the NVRAM can be accessed the caller must acquire an NVRAM lock.  */
1622 /* Locks 0 and 2 are reserved, lock 1 is used by firmware and lock 2 is     */
1623 /* for use by the driver.                                                   */
1624 /*                                                                          */
1625 /* Returns:                                                                 */
1626 /*   0 on success, positive value on failure.                               */
1627 /****************************************************************************/
1628 static int
1629 bce_acquire_nvram_lock(struct bce_softc *sc)
1630 {
1631 	u32 val;
1632 	int j, rc = 0;
1633 
1634 	DBENTER(BCE_VERBOSE_NVRAM);
1635 
1636 	/* Request access to the flash interface. */
1637 	REG_WR(sc, BCE_NVM_SW_ARB, BCE_NVM_SW_ARB_ARB_REQ_SET2);
1638 	for (j = 0; j < NVRAM_TIMEOUT_COUNT; j++) {
1639 		val = REG_RD(sc, BCE_NVM_SW_ARB);
1640 		if (val & BCE_NVM_SW_ARB_ARB_ARB2)
1641 			break;
1642 
1643 		DELAY(5);
1644 	}
1645 
1646 	if (j >= NVRAM_TIMEOUT_COUNT) {
1647 		DBPRINT(sc, BCE_WARN, "Timeout acquiring NVRAM lock!\n");
1648 		rc = EBUSY;
1649 	}
1650 
1651 	DBEXIT(BCE_VERBOSE_NVRAM);
1652 	return (rc);
1653 }
1654 
1655 
1656 /****************************************************************************/
1657 /* Release NVRAM lock.                                                      */
1658 /*                                                                          */
1659 /* When the caller is finished accessing NVRAM the lock must be released.   */
1660 /* Locks 0 and 2 are reserved, lock 1 is used by firmware and lock 2 is     */
1661 /* for use by the driver.                                                   */
1662 /*                                                                          */
1663 /* Returns:                                                                 */
1664 /*   0 on success, positive value on failure.                               */
1665 /****************************************************************************/
1666 static int
1667 bce_release_nvram_lock(struct bce_softc *sc)
1668 {
1669 	u32 val;
1670 	int j, rc = 0;
1671 
1672 	DBENTER(BCE_VERBOSE_NVRAM);
1673 
1674 	/*
1675 	 * Relinquish nvram interface.
1676 	 */
1677 	REG_WR(sc, BCE_NVM_SW_ARB, BCE_NVM_SW_ARB_ARB_REQ_CLR2);
1678 
1679 	for (j = 0; j < NVRAM_TIMEOUT_COUNT; j++) {
1680 		val = REG_RD(sc, BCE_NVM_SW_ARB);
1681 		if (!(val & BCE_NVM_SW_ARB_ARB_ARB2))
1682 			break;
1683 
1684 		DELAY(5);
1685 	}
1686 
1687 	if (j >= NVRAM_TIMEOUT_COUNT) {
1688 		DBPRINT(sc, BCE_WARN, "Timeout releasing NVRAM lock!\n");
1689 		rc = EBUSY;
1690 	}
1691 
1692 	DBEXIT(BCE_VERBOSE_NVRAM);
1693 	return (rc);
1694 }
1695 
1696 
1697 #ifdef BCE_NVRAM_WRITE_SUPPORT
1698 /****************************************************************************/
1699 /* Enable NVRAM write access.                                               */
1700 /*                                                                          */
1701 /* Before writing to NVRAM the caller must enable NVRAM writes.             */
1702 /*                                                                          */
1703 /* Returns:                                                                 */
1704 /*   0 on success, positive value on failure.                               */
1705 /****************************************************************************/
1706 static int
1707 bce_enable_nvram_write(struct bce_softc *sc)
1708 {
1709 	u32 val;
1710 	int rc = 0;
1711 
1712 	DBENTER(BCE_VERBOSE_NVRAM);
1713 
1714 	val = REG_RD(sc, BCE_MISC_CFG);
1715 	REG_WR(sc, BCE_MISC_CFG, val | BCE_MISC_CFG_NVM_WR_EN_PCI);
1716 
1717 	if (!(sc->bce_flash_info->flags & BCE_NV_BUFFERED)) {
1718 		int j;
1719 
1720 		REG_WR(sc, BCE_NVM_COMMAND, BCE_NVM_COMMAND_DONE);
1721 		REG_WR(sc, BCE_NVM_COMMAND,	BCE_NVM_COMMAND_WREN | BCE_NVM_COMMAND_DOIT);
1722 
1723 		for (j = 0; j < NVRAM_TIMEOUT_COUNT; j++) {
1724 			DELAY(5);
1725 
1726 			val = REG_RD(sc, BCE_NVM_COMMAND);
1727 			if (val & BCE_NVM_COMMAND_DONE)
1728 				break;
1729 		}
1730 
1731 		if (j >= NVRAM_TIMEOUT_COUNT) {
1732 			DBPRINT(sc, BCE_WARN, "Timeout writing NVRAM!\n");
1733 			rc = EBUSY;
1734 		}
1735 	}
1736 
1737 	DBENTER(BCE_VERBOSE_NVRAM);
1738 	return (rc);
1739 }
1740 
1741 
1742 /****************************************************************************/
1743 /* Disable NVRAM write access.                                              */
1744 /*                                                                          */
1745 /* When the caller is finished writing to NVRAM write access must be        */
1746 /* disabled.                                                                */
1747 /*                                                                          */
1748 /* Returns:                                                                 */
1749 /*   Nothing.                                                               */
1750 /****************************************************************************/
1751 static void
1752 bce_disable_nvram_write(struct bce_softc *sc)
1753 {
1754 	u32 val;
1755 
1756 	DBENTER(BCE_VERBOSE_NVRAM);
1757 
1758 	val = REG_RD(sc, BCE_MISC_CFG);
1759 	REG_WR(sc, BCE_MISC_CFG, val & ~BCE_MISC_CFG_NVM_WR_EN);
1760 
1761 	DBEXIT(BCE_VERBOSE_NVRAM);
1762 
1763 }
1764 #endif
1765 
1766 
1767 /****************************************************************************/
1768 /* Enable NVRAM access.                                                     */
1769 /*                                                                          */
1770 /* Before accessing NVRAM for read or write operations the caller must      */
1771 /* enabled NVRAM access.                                                    */
1772 /*                                                                          */
1773 /* Returns:                                                                 */
1774 /*   Nothing.                                                               */
1775 /****************************************************************************/
1776 static void
1777 bce_enable_nvram_access(struct bce_softc *sc)
1778 {
1779 	u32 val;
1780 
1781 	DBENTER(BCE_VERBOSE_NVRAM);
1782 
1783 	val = REG_RD(sc, BCE_NVM_ACCESS_ENABLE);
1784 	/* Enable both bits, even on read. */
1785 	REG_WR(sc, BCE_NVM_ACCESS_ENABLE,
1786 	       val | BCE_NVM_ACCESS_ENABLE_EN | BCE_NVM_ACCESS_ENABLE_WR_EN);
1787 
1788 	DBEXIT(BCE_VERBOSE_NVRAM);
1789 }
1790 
1791 
1792 /****************************************************************************/
1793 /* Disable NVRAM access.                                                    */
1794 /*                                                                          */
1795 /* When the caller is finished accessing NVRAM access must be disabled.     */
1796 /*                                                                          */
1797 /* Returns:                                                                 */
1798 /*   Nothing.                                                               */
1799 /****************************************************************************/
1800 static void
1801 bce_disable_nvram_access(struct bce_softc *sc)
1802 {
1803 	u32 val;
1804 
1805 	DBENTER(BCE_VERBOSE_NVRAM);
1806 
1807 	val = REG_RD(sc, BCE_NVM_ACCESS_ENABLE);
1808 
1809 	/* Disable both bits, even after read. */
1810 	REG_WR(sc, BCE_NVM_ACCESS_ENABLE,
1811 		val & ~(BCE_NVM_ACCESS_ENABLE_EN |
1812 			BCE_NVM_ACCESS_ENABLE_WR_EN));
1813 
1814 	DBEXIT(BCE_VERBOSE_NVRAM);
1815 }
1816 
1817 
1818 #ifdef BCE_NVRAM_WRITE_SUPPORT
1819 /****************************************************************************/
1820 /* Erase NVRAM page before writing.                                         */
1821 /*                                                                          */
1822 /* Non-buffered flash parts require that a page be erased before it is      */
1823 /* written.                                                                 */
1824 /*                                                                          */
1825 /* Returns:                                                                 */
1826 /*   0 on success, positive value on failure.                               */
1827 /****************************************************************************/
1828 static int
1829 bce_nvram_erase_page(struct bce_softc *sc, u32 offset)
1830 {
1831 	u32 cmd;
1832 	int j, rc = 0;
1833 
1834 	DBENTER(BCE_VERBOSE_NVRAM);
1835 
1836 	/* Buffered flash doesn't require an erase. */
1837 	if (sc->bce_flash_info->flags & BCE_NV_BUFFERED)
1838 		goto bce_nvram_erase_page_exit;
1839 
1840 	/* Build an erase command. */
1841 	cmd = BCE_NVM_COMMAND_ERASE | BCE_NVM_COMMAND_WR |
1842 	      BCE_NVM_COMMAND_DOIT;
1843 
1844 	/*
1845 	 * Clear the DONE bit separately, set the NVRAM adress to erase,
1846 	 * and issue the erase command.
1847 	 */
1848 	REG_WR(sc, BCE_NVM_COMMAND, BCE_NVM_COMMAND_DONE);
1849 	REG_WR(sc, BCE_NVM_ADDR, offset & BCE_NVM_ADDR_NVM_ADDR_VALUE);
1850 	REG_WR(sc, BCE_NVM_COMMAND, cmd);
1851 
1852 	/* Wait for completion. */
1853 	for (j = 0; j < NVRAM_TIMEOUT_COUNT; j++) {
1854 		u32 val;
1855 
1856 		DELAY(5);
1857 
1858 		val = REG_RD(sc, BCE_NVM_COMMAND);
1859 		if (val & BCE_NVM_COMMAND_DONE)
1860 			break;
1861 	}
1862 
1863 	if (j >= NVRAM_TIMEOUT_COUNT) {
1864 		DBPRINT(sc, BCE_WARN, "Timeout erasing NVRAM.\n");
1865 		rc = EBUSY;
1866 	}
1867 
1868 bce_nvram_erase_page_exit:
1869 	DBEXIT(BCE_VERBOSE_NVRAM);
1870 	return (rc);
1871 }
1872 #endif /* BCE_NVRAM_WRITE_SUPPORT */
1873 
1874 
1875 /****************************************************************************/
1876 /* Read a dword (32 bits) from NVRAM.                                       */
1877 /*                                                                          */
1878 /* Read a 32 bit word from NVRAM.  The caller is assumed to have already    */
1879 /* obtained the NVRAM lock and enabled the controller for NVRAM access.     */
1880 /*                                                                          */
1881 /* Returns:                                                                 */
1882 /*   0 on success and the 32 bit value read, positive value on failure.     */
1883 /****************************************************************************/
1884 static int
1885 bce_nvram_read_dword(struct bce_softc *sc, u32 offset, u8 *ret_val,
1886 							u32 cmd_flags)
1887 {
1888 	u32 cmd;
1889 	int i, rc = 0;
1890 
1891 	DBENTER(BCE_EXTREME_NVRAM);
1892 
1893 	/* Build the command word. */
1894 	cmd = BCE_NVM_COMMAND_DOIT | cmd_flags;
1895 
1896 	/* Calculate the offset for buffered flash if translation is used. */
1897 	if (sc->bce_flash_info->flags & BCE_NV_TRANSLATE) {
1898 		offset = ((offset / sc->bce_flash_info->page_size) <<
1899 			   sc->bce_flash_info->page_bits) +
1900 			  (offset % sc->bce_flash_info->page_size);
1901 	}
1902 
1903 	/*
1904 	 * Clear the DONE bit separately, set the address to read,
1905 	 * and issue the read.
1906 	 */
1907 	REG_WR(sc, BCE_NVM_COMMAND, BCE_NVM_COMMAND_DONE);
1908 	REG_WR(sc, BCE_NVM_ADDR, offset & BCE_NVM_ADDR_NVM_ADDR_VALUE);
1909 	REG_WR(sc, BCE_NVM_COMMAND, cmd);
1910 
1911 	/* Wait for completion. */
1912 	for (i = 0; i < NVRAM_TIMEOUT_COUNT; i++) {
1913 		u32 val;
1914 
1915 		DELAY(5);
1916 
1917 		val = REG_RD(sc, BCE_NVM_COMMAND);
1918 		if (val & BCE_NVM_COMMAND_DONE) {
1919 			val = REG_RD(sc, BCE_NVM_READ);
1920 
1921 			val = bce_be32toh(val);
1922 			memcpy(ret_val, &val, 4);
1923 			break;
1924 		}
1925 	}
1926 
1927 	/* Check for errors. */
1928 	if (i >= NVRAM_TIMEOUT_COUNT) {
1929 		BCE_PRINTF("%s(%d): Timeout error reading NVRAM at offset 0x%08X!\n",
1930 			__FILE__, __LINE__, offset);
1931 		rc = EBUSY;
1932 	}
1933 
1934 	DBEXIT(BCE_EXTREME_NVRAM);
1935 	return(rc);
1936 }
1937 
1938 
1939 #ifdef BCE_NVRAM_WRITE_SUPPORT
1940 /****************************************************************************/
1941 /* Write a dword (32 bits) to NVRAM.                                        */
1942 /*                                                                          */
1943 /* Write a 32 bit word to NVRAM.  The caller is assumed to have already     */
1944 /* obtained the NVRAM lock, enabled the controller for NVRAM access, and    */
1945 /* enabled NVRAM write access.                                              */
1946 /*                                                                          */
1947 /* Returns:                                                                 */
1948 /*   0 on success, positive value on failure.                               */
1949 /****************************************************************************/
1950 static int
1951 bce_nvram_write_dword(struct bce_softc *sc, u32 offset, u8 *val,
1952 	u32 cmd_flags)
1953 {
1954 	u32 cmd, val32;
1955 	int j, rc = 0;
1956 
1957 	DBENTER(BCE_VERBOSE_NVRAM);
1958 
1959 	/* Build the command word. */
1960 	cmd = BCE_NVM_COMMAND_DOIT | BCE_NVM_COMMAND_WR | cmd_flags;
1961 
1962 	/* Calculate the offset for buffered flash if translation is used. */
1963 	if (sc->bce_flash_info->flags & BCE_NV_TRANSLATE) {
1964 		offset = ((offset / sc->bce_flash_info->page_size) <<
1965 			  sc->bce_flash_info->page_bits) +
1966 			 (offset % sc->bce_flash_info->page_size);
1967 	}
1968 
1969 	/*
1970 	 * Clear the DONE bit separately, convert NVRAM data to big-endian,
1971 	 * set the NVRAM address to write, and issue the write command
1972 	 */
1973 	REG_WR(sc, BCE_NVM_COMMAND, BCE_NVM_COMMAND_DONE);
1974 	memcpy(&val32, val, 4);
1975 	val32 = htobe32(val32);
1976 	REG_WR(sc, BCE_NVM_WRITE, val32);
1977 	REG_WR(sc, BCE_NVM_ADDR, offset & BCE_NVM_ADDR_NVM_ADDR_VALUE);
1978 	REG_WR(sc, BCE_NVM_COMMAND, cmd);
1979 
1980 	/* Wait for completion. */
1981 	for (j = 0; j < NVRAM_TIMEOUT_COUNT; j++) {
1982 		DELAY(5);
1983 
1984 		if (REG_RD(sc, BCE_NVM_COMMAND) & BCE_NVM_COMMAND_DONE)
1985 			break;
1986 	}
1987 	if (j >= NVRAM_TIMEOUT_COUNT) {
1988 		BCE_PRINTF("%s(%d): Timeout error writing NVRAM at offset 0x%08X\n",
1989 			__FILE__, __LINE__, offset);
1990 		rc = EBUSY;
1991 	}
1992 
1993 	DBEXIT(BCE_VERBOSE_NVRAM);
1994 	return (rc);
1995 }
1996 #endif /* BCE_NVRAM_WRITE_SUPPORT */
1997 
1998 
1999 /****************************************************************************/
2000 /* Initialize NVRAM access.                                                 */
2001 /*                                                                          */
2002 /* Identify the NVRAM device in use and prepare the NVRAM interface to      */
2003 /* access that device.                                                      */
2004 /*                                                                          */
2005 /* Returns:                                                                 */
2006 /*   0 on success, positive value on failure.                               */
2007 /****************************************************************************/
2008 static int
2009 bce_init_nvram(struct bce_softc *sc)
2010 {
2011 	u32 val;
2012 	int j, entry_count, rc = 0;
2013 	struct flash_spec *flash;
2014 
2015 	DBENTER(BCE_VERBOSE_NVRAM);
2016 
2017 	if ((BCE_CHIP_NUM(sc) == BCE_CHIP_NUM_5709) ||
2018 		(BCE_CHIP_NUM(sc) == BCE_CHIP_NUM_5716)) {
2019 		sc->bce_flash_info = &flash_5709;
2020 		goto bce_init_nvram_get_flash_size;
2021 	}
2022 
2023 	/* Determine the selected interface. */
2024 	val = REG_RD(sc, BCE_NVM_CFG1);
2025 
2026 	entry_count = sizeof(flash_table) / sizeof(struct flash_spec);
2027 
2028 	/*
2029 	 * Flash reconfiguration is required to support additional
2030 	 * NVRAM devices not directly supported in hardware.
2031 	 * Check if the flash interface was reconfigured
2032 	 * by the bootcode.
2033 	 */
2034 
2035 	if (val & 0x40000000) {
2036 		/* Flash interface reconfigured by bootcode. */
2037 
2038 		DBPRINT(sc,BCE_INFO_LOAD,
2039 			"bce_init_nvram(): Flash WAS reconfigured.\n");
2040 
2041 		for (j = 0, flash = &flash_table[0]; j < entry_count;
2042 		     j++, flash++) {
2043 			if ((val & FLASH_BACKUP_STRAP_MASK) ==
2044 			    (flash->config1 & FLASH_BACKUP_STRAP_MASK)) {
2045 				sc->bce_flash_info = flash;
2046 				break;
2047 			}
2048 		}
2049 	} else {
2050 		/* Flash interface not yet reconfigured. */
2051 		u32 mask;
2052 
2053 		DBPRINT(sc, BCE_INFO_LOAD, "%s(): Flash was NOT reconfigured.\n",
2054 			__FUNCTION__);
2055 
2056 		if (val & (1 << 23))
2057 			mask = FLASH_BACKUP_STRAP_MASK;
2058 		else
2059 			mask = FLASH_STRAP_MASK;
2060 
2061 		/* Look for the matching NVRAM device configuration data. */
2062 		for (j = 0, flash = &flash_table[0]; j < entry_count; j++, flash++) {
2063 
2064 			/* Check if the device matches any of the known devices. */
2065 			if ((val & mask) == (flash->strapping & mask)) {
2066 				/* Found a device match. */
2067 				sc->bce_flash_info = flash;
2068 
2069 				/* Request access to the flash interface. */
2070 				if ((rc = bce_acquire_nvram_lock(sc)) != 0)
2071 					return rc;
2072 
2073 				/* Reconfigure the flash interface. */
2074 				bce_enable_nvram_access(sc);
2075 				REG_WR(sc, BCE_NVM_CFG1, flash->config1);
2076 				REG_WR(sc, BCE_NVM_CFG2, flash->config2);
2077 				REG_WR(sc, BCE_NVM_CFG3, flash->config3);
2078 				REG_WR(sc, BCE_NVM_WRITE1, flash->write1);
2079 				bce_disable_nvram_access(sc);
2080 				bce_release_nvram_lock(sc);
2081 
2082 				break;
2083 			}
2084 		}
2085 	}
2086 
2087 	/* Check if a matching device was found. */
2088 	if (j == entry_count) {
2089 		sc->bce_flash_info = NULL;
2090 		BCE_PRINTF("%s(%d): Unknown Flash NVRAM found!\n",
2091 			__FILE__, __LINE__);
2092 		rc = ENODEV;
2093 	}
2094 
2095 bce_init_nvram_get_flash_size:
2096 	/* Write the flash config data to the shared memory interface. */
2097 	val = REG_RD_IND(sc, sc->bce_shmem_base + BCE_SHARED_HW_CFG_CONFIG2);
2098 	val &= BCE_SHARED_HW_CFG2_NVM_SIZE_MASK;
2099 	if (val)
2100 		sc->bce_flash_size = val;
2101 	else
2102 		sc->bce_flash_size = sc->bce_flash_info->total_size;
2103 
2104 	DBPRINT(sc, BCE_INFO_LOAD, "%s(): Found %s, size = 0x%08X\n",
2105 		__FUNCTION__, sc->bce_flash_info->name,
2106 		sc->bce_flash_info->total_size);
2107 
2108 	DBEXIT(BCE_VERBOSE_NVRAM);
2109 	return rc;
2110 }
2111 
2112 
2113 /****************************************************************************/
2114 /* Read an arbitrary range of data from NVRAM.                              */
2115 /*                                                                          */
2116 /* Prepares the NVRAM interface for access and reads the requested data     */
2117 /* into the supplied buffer.                                                */
2118 /*                                                                          */
2119 /* Returns:                                                                 */
2120 /*   0 on success and the data read, positive value on failure.             */
2121 /****************************************************************************/
2122 static int
2123 bce_nvram_read(struct bce_softc *sc, u32 offset, u8 *ret_buf,
2124 	int buf_size)
2125 {
2126 	int rc = 0;
2127 	u32 cmd_flags, offset32, len32, extra;
2128 
2129 	DBENTER(BCE_VERBOSE_NVRAM);
2130 
2131 	if (buf_size == 0)
2132 		goto bce_nvram_read_exit;
2133 
2134 	/* Request access to the flash interface. */
2135 	if ((rc = bce_acquire_nvram_lock(sc)) != 0)
2136 		goto bce_nvram_read_exit;
2137 
2138 	/* Enable access to flash interface */
2139 	bce_enable_nvram_access(sc);
2140 
2141 	len32 = buf_size;
2142 	offset32 = offset;
2143 	extra = 0;
2144 
2145 	cmd_flags = 0;
2146 
2147 	if (offset32 & 3) {
2148 		u8 buf[4];
2149 		u32 pre_len;
2150 
2151 		offset32 &= ~3;
2152 		pre_len = 4 - (offset & 3);
2153 
2154 		if (pre_len >= len32) {
2155 			pre_len = len32;
2156 			cmd_flags = BCE_NVM_COMMAND_FIRST | BCE_NVM_COMMAND_LAST;
2157 		}
2158 		else {
2159 			cmd_flags = BCE_NVM_COMMAND_FIRST;
2160 		}
2161 
2162 		rc = bce_nvram_read_dword(sc, offset32, buf, cmd_flags);
2163 
2164 		if (rc)
2165 			return rc;
2166 
2167 		memcpy(ret_buf, buf + (offset & 3), pre_len);
2168 
2169 		offset32 += 4;
2170 		ret_buf += pre_len;
2171 		len32 -= pre_len;
2172 	}
2173 
2174 	if (len32 & 3) {
2175 		extra = 4 - (len32 & 3);
2176 		len32 = (len32 + 4) & ~3;
2177 	}
2178 
2179 	if (len32 == 4) {
2180 		u8 buf[4];
2181 
2182 		if (cmd_flags)
2183 			cmd_flags = BCE_NVM_COMMAND_LAST;
2184 		else
2185 			cmd_flags = BCE_NVM_COMMAND_FIRST |
2186 				    BCE_NVM_COMMAND_LAST;
2187 
2188 		rc = bce_nvram_read_dword(sc, offset32, buf, cmd_flags);
2189 
2190 		memcpy(ret_buf, buf, 4 - extra);
2191 	}
2192 	else if (len32 > 0) {
2193 		u8 buf[4];
2194 
2195 		/* Read the first word. */
2196 		if (cmd_flags)
2197 			cmd_flags = 0;
2198 		else
2199 			cmd_flags = BCE_NVM_COMMAND_FIRST;
2200 
2201 		rc = bce_nvram_read_dword(sc, offset32, ret_buf, cmd_flags);
2202 
2203 		/* Advance to the next dword. */
2204 		offset32 += 4;
2205 		ret_buf += 4;
2206 		len32 -= 4;
2207 
2208 		while (len32 > 4 && rc == 0) {
2209 			rc = bce_nvram_read_dword(sc, offset32, ret_buf, 0);
2210 
2211 			/* Advance to the next dword. */
2212 			offset32 += 4;
2213 			ret_buf += 4;
2214 			len32 -= 4;
2215 		}
2216 
2217 		if (rc)
2218 			goto bce_nvram_read_locked_exit;
2219 
2220 		cmd_flags = BCE_NVM_COMMAND_LAST;
2221 		rc = bce_nvram_read_dword(sc, offset32, buf, cmd_flags);
2222 
2223 		memcpy(ret_buf, buf, 4 - extra);
2224 	}
2225 
2226 bce_nvram_read_locked_exit:
2227 	/* Disable access to flash interface and release the lock. */
2228 	bce_disable_nvram_access(sc);
2229 	bce_release_nvram_lock(sc);
2230 
2231 bce_nvram_read_exit:
2232 	DBEXIT(BCE_VERBOSE_NVRAM);
2233 	return rc;
2234 }
2235 
2236 
2237 #ifdef BCE_NVRAM_WRITE_SUPPORT
2238 /****************************************************************************/
2239 /* Write an arbitrary range of data from NVRAM.                             */
2240 /*                                                                          */
2241 /* Prepares the NVRAM interface for write access and writes the requested   */
2242 /* data from the supplied buffer.  The caller is responsible for            */
2243 /* calculating any appropriate CRCs.                                        */
2244 /*                                                                          */
2245 /* Returns:                                                                 */
2246 /*   0 on success, positive value on failure.                               */
2247 /****************************************************************************/
2248 static int
2249 bce_nvram_write(struct bce_softc *sc, u32 offset, u8 *data_buf,
2250 	int buf_size)
2251 {
2252 	u32 written, offset32, len32;
2253 	u8 *buf, start[4], end[4];
2254 	int rc = 0;
2255 	int align_start, align_end;
2256 
2257 	DBENTER(BCE_VERBOSE_NVRAM);
2258 
2259 	buf = data_buf;
2260 	offset32 = offset;
2261 	len32 = buf_size;
2262 	align_start = align_end = 0;
2263 
2264 	if ((align_start = (offset32 & 3))) {
2265 		offset32 &= ~3;
2266 		len32 += align_start;
2267 		if ((rc = bce_nvram_read(sc, offset32, start, 4)))
2268 			goto bce_nvram_write_exit;
2269 	}
2270 
2271 	if (len32 & 3) {
2272 	       	if ((len32 > 4) || !align_start) {
2273 			align_end = 4 - (len32 & 3);
2274 			len32 += align_end;
2275 			if ((rc = bce_nvram_read(sc, offset32 + len32 - 4,
2276 				end, 4))) {
2277 				goto bce_nvram_write_exit;
2278 			}
2279 		}
2280 	}
2281 
2282 	if (align_start || align_end) {
2283 		buf = malloc(len32, M_DEVBUF, M_NOWAIT);
2284 		if (buf == 0) {
2285 			rc = ENOMEM;
2286 			goto bce_nvram_write_exit;
2287 		}
2288 
2289 		if (align_start) {
2290 			memcpy(buf, start, 4);
2291 		}
2292 
2293 		if (align_end) {
2294 			memcpy(buf + len32 - 4, end, 4);
2295 		}
2296 		memcpy(buf + align_start, data_buf, buf_size);
2297 	}
2298 
2299 	written = 0;
2300 	while ((written < len32) && (rc == 0)) {
2301 		u32 page_start, page_end, data_start, data_end;
2302 		u32 addr, cmd_flags;
2303 		int i;
2304 		u8 flash_buffer[264];
2305 
2306 	    /* Find the page_start addr */
2307 		page_start = offset32 + written;
2308 		page_start -= (page_start % sc->bce_flash_info->page_size);
2309 		/* Find the page_end addr */
2310 		page_end = page_start + sc->bce_flash_info->page_size;
2311 		/* Find the data_start addr */
2312 		data_start = (written == 0) ? offset32 : page_start;
2313 		/* Find the data_end addr */
2314 		data_end = (page_end > offset32 + len32) ?
2315 			(offset32 + len32) : page_end;
2316 
2317 		/* Request access to the flash interface. */
2318 		if ((rc = bce_acquire_nvram_lock(sc)) != 0)
2319 			goto bce_nvram_write_exit;
2320 
2321 		/* Enable access to flash interface */
2322 		bce_enable_nvram_access(sc);
2323 
2324 		cmd_flags = BCE_NVM_COMMAND_FIRST;
2325 		if (!(sc->bce_flash_info->flags & BCE_NV_BUFFERED)) {
2326 			int j;
2327 
2328 			/* Read the whole page into the buffer
2329 			 * (non-buffer flash only) */
2330 			for (j = 0; j < sc->bce_flash_info->page_size; j += 4) {
2331 				if (j == (sc->bce_flash_info->page_size - 4)) {
2332 					cmd_flags |= BCE_NVM_COMMAND_LAST;
2333 				}
2334 				rc = bce_nvram_read_dword(sc,
2335 					page_start + j,
2336 					&flash_buffer[j],
2337 					cmd_flags);
2338 
2339 				if (rc)
2340 					goto bce_nvram_write_locked_exit;
2341 
2342 				cmd_flags = 0;
2343 			}
2344 		}
2345 
2346 		/* Enable writes to flash interface (unlock write-protect) */
2347 		if ((rc = bce_enable_nvram_write(sc)) != 0)
2348 			goto bce_nvram_write_locked_exit;
2349 
2350 		/* Erase the page */
2351 		if ((rc = bce_nvram_erase_page(sc, page_start)) != 0)
2352 			goto bce_nvram_write_locked_exit;
2353 
2354 		/* Re-enable the write again for the actual write */
2355 		bce_enable_nvram_write(sc);
2356 
2357 		/* Loop to write back the buffer data from page_start to
2358 		 * data_start */
2359 		i = 0;
2360 		if (!(sc->bce_flash_info->flags & BCE_NV_BUFFERED)) {
2361 			for (addr = page_start; addr < data_start;
2362 				addr += 4, i += 4) {
2363 
2364 				rc = bce_nvram_write_dword(sc, addr,
2365 					&flash_buffer[i], cmd_flags);
2366 
2367 				if (rc != 0)
2368 					goto bce_nvram_write_locked_exit;
2369 
2370 				cmd_flags = 0;
2371 			}
2372 		}
2373 
2374 		/* Loop to write the new data from data_start to data_end */
2375 		for (addr = data_start; addr < data_end; addr += 4, i++) {
2376 			if ((addr == page_end - 4) ||
2377 				((sc->bce_flash_info->flags & BCE_NV_BUFFERED) &&
2378 				(addr == data_end - 4))) {
2379 
2380 				cmd_flags |= BCE_NVM_COMMAND_LAST;
2381 			}
2382 			rc = bce_nvram_write_dword(sc, addr, buf,
2383 				cmd_flags);
2384 
2385 			if (rc != 0)
2386 				goto bce_nvram_write_locked_exit;
2387 
2388 			cmd_flags = 0;
2389 			buf += 4;
2390 		}
2391 
2392 		/* Loop to write back the buffer data from data_end
2393 		 * to page_end */
2394 		if (!(sc->bce_flash_info->flags & BCE_NV_BUFFERED)) {
2395 			for (addr = data_end; addr < page_end;
2396 				addr += 4, i += 4) {
2397 
2398 				if (addr == page_end-4) {
2399 					cmd_flags = BCE_NVM_COMMAND_LAST;
2400                 		}
2401 				rc = bce_nvram_write_dword(sc, addr,
2402 					&flash_buffer[i], cmd_flags);
2403 
2404 				if (rc != 0)
2405 					goto bce_nvram_write_locked_exit;
2406 
2407 				cmd_flags = 0;
2408 			}
2409 		}
2410 
2411 		/* Disable writes to flash interface (lock write-protect) */
2412 		bce_disable_nvram_write(sc);
2413 
2414 		/* Disable access to flash interface */
2415 		bce_disable_nvram_access(sc);
2416 		bce_release_nvram_lock(sc);
2417 
2418 		/* Increment written */
2419 		written += data_end - data_start;
2420 	}
2421 
2422 	goto bce_nvram_write_exit;
2423 
2424 bce_nvram_write_locked_exit:
2425 		bce_disable_nvram_write(sc);
2426 		bce_disable_nvram_access(sc);
2427 		bce_release_nvram_lock(sc);
2428 
2429 bce_nvram_write_exit:
2430 	if (align_start || align_end)
2431 		free(buf, M_DEVBUF);
2432 
2433 	DBEXIT(BCE_VERBOSE_NVRAM);
2434 	return (rc);
2435 }
2436 #endif /* BCE_NVRAM_WRITE_SUPPORT */
2437 
2438 
2439 /****************************************************************************/
2440 /* Verifies that NVRAM is accessible and contains valid data.               */
2441 /*                                                                          */
2442 /* Reads the configuration data from NVRAM and verifies that the CRC is     */
2443 /* correct.                                                                 */
2444 /*                                                                          */
2445 /* Returns:                                                                 */
2446 /*   0 on success, positive value on failure.                               */
2447 /****************************************************************************/
2448 static int
2449 bce_nvram_test(struct bce_softc *sc)
2450 {
2451 	u32 buf[BCE_NVRAM_SIZE / 4];
2452 	u8 *data = (u8 *) buf;
2453 	int rc = 0;
2454 	u32 magic, csum;
2455 
2456 	DBENTER(BCE_VERBOSE_NVRAM | BCE_VERBOSE_LOAD | BCE_VERBOSE_RESET);
2457 
2458 	/*
2459 	 * Check that the device NVRAM is valid by reading
2460 	 * the magic value at offset 0.
2461 	 */
2462 	if ((rc = bce_nvram_read(sc, 0, data, 4)) != 0) {
2463 		BCE_PRINTF("%s(%d): Unable to read NVRAM!\n", __FILE__, __LINE__);
2464 		goto bce_nvram_test_exit;
2465 	}
2466 
2467 	/*
2468 	 * Verify that offset 0 of the NVRAM contains
2469 	 * a valid magic number.
2470 	 */
2471     magic = bce_be32toh(buf[0]);
2472 	if (magic != BCE_NVRAM_MAGIC) {
2473 		rc = ENODEV;
2474 		BCE_PRINTF("%s(%d): Invalid NVRAM magic value! Expected: 0x%08X, "
2475 			"Found: 0x%08X\n",
2476 			__FILE__, __LINE__, BCE_NVRAM_MAGIC, magic);
2477 		goto bce_nvram_test_exit;
2478 	}
2479 
2480 	/*
2481 	 * Verify that the device NVRAM includes valid
2482 	 * configuration data.
2483 	 */
2484 	if ((rc = bce_nvram_read(sc, 0x100, data, BCE_NVRAM_SIZE)) != 0) {
2485 		BCE_PRINTF("%s(%d): Unable to read Manufacturing Information from "
2486 			"NVRAM!\n", __FILE__, __LINE__);
2487 		goto bce_nvram_test_exit;
2488 	}
2489 
2490 	csum = ether_crc32_le(data, 0x100);
2491 	if (csum != BCE_CRC32_RESIDUAL) {
2492 		rc = ENODEV;
2493 		BCE_PRINTF("%s(%d): Invalid Manufacturing Information NVRAM CRC! "
2494 			"Expected: 0x%08X, Found: 0x%08X\n",
2495 			__FILE__, __LINE__, BCE_CRC32_RESIDUAL, csum);
2496 		goto bce_nvram_test_exit;
2497 	}
2498 
2499 	csum = ether_crc32_le(data + 0x100, 0x100);
2500 	if (csum != BCE_CRC32_RESIDUAL) {
2501 		rc = ENODEV;
2502 		BCE_PRINTF("%s(%d): Invalid Feature Configuration Information "
2503 			"NVRAM CRC! Expected: 0x%08X, Found: 08%08X\n",
2504 			__FILE__, __LINE__, BCE_CRC32_RESIDUAL, csum);
2505 	}
2506 
2507 bce_nvram_test_exit:
2508 	DBEXIT(BCE_VERBOSE_NVRAM | BCE_VERBOSE_LOAD | BCE_VERBOSE_RESET);
2509 	return rc;
2510 }
2511 
2512 
2513 /****************************************************************************/
2514 /* Identifies the current media type of the controller and sets the PHY     */
2515 /* address.                                                                 */
2516 /*                                                                          */
2517 /* Returns:                                                                 */
2518 /*   Nothing.                                                               */
2519 /****************************************************************************/
2520 static void
2521 bce_get_media(struct bce_softc *sc)
2522 {
2523 	u32 val;
2524 
2525 	DBENTER(BCE_VERBOSE);
2526 
2527 	/* Assume PHY address for copper controllers. */
2528 	sc->bce_phy_addr = 1;
2529 
2530 	if (BCE_CHIP_NUM(sc) == BCE_CHIP_NUM_5709) {
2531  		u32 val = REG_RD(sc, BCE_MISC_DUAL_MEDIA_CTRL);
2532 		u32 bond_id = val & BCE_MISC_DUAL_MEDIA_CTRL_BOND_ID;
2533 		u32 strap;
2534 
2535 		/*
2536 		 * The BCM5709S is software configurable
2537 		 * for Copper or SerDes operation.
2538 		 */
2539 		if (bond_id == BCE_MISC_DUAL_MEDIA_CTRL_BOND_ID_C) {
2540 			DBPRINT(sc, BCE_INFO_LOAD, "5709 bonded for copper.\n");
2541 			goto bce_get_media_exit;
2542 		} else if (bond_id == BCE_MISC_DUAL_MEDIA_CTRL_BOND_ID_S) {
2543 			DBPRINT(sc, BCE_INFO_LOAD, "5709 bonded for dual media.\n");
2544 			sc->bce_phy_flags |= BCE_PHY_SERDES_FLAG;
2545 			goto bce_get_media_exit;
2546 		}
2547 
2548 		if (val & BCE_MISC_DUAL_MEDIA_CTRL_STRAP_OVERRIDE)
2549 			strap = (val & BCE_MISC_DUAL_MEDIA_CTRL_PHY_CTRL) >> 21;
2550 		else
2551 			strap = (val & BCE_MISC_DUAL_MEDIA_CTRL_PHY_CTRL_STRAP) >> 8;
2552 
2553 		if (pci_get_function(sc->bce_dev) == 0) {
2554 			switch (strap) {
2555 			case 0x4:
2556 			case 0x5:
2557 			case 0x6:
2558 				DBPRINT(sc, BCE_INFO_LOAD,
2559 					"BCM5709 s/w configured for SerDes.\n");
2560 				sc->bce_phy_flags |= BCE_PHY_SERDES_FLAG;
2561 			default:
2562 				DBPRINT(sc, BCE_INFO_LOAD,
2563 					"BCM5709 s/w configured for Copper.\n");
2564 			}
2565 		} else {
2566 			switch (strap) {
2567 			case 0x1:
2568 			case 0x2:
2569 			case 0x4:
2570 				DBPRINT(sc, BCE_INFO_LOAD,
2571 					"BCM5709 s/w configured for SerDes.\n");
2572 				sc->bce_phy_flags |= BCE_PHY_SERDES_FLAG;
2573 			default:
2574 				DBPRINT(sc, BCE_INFO_LOAD,
2575 					"BCM5709 s/w configured for Copper.\n");
2576 			}
2577 		}
2578 
2579 	} else if (BCE_CHIP_BOND_ID(sc) & BCE_CHIP_BOND_ID_SERDES_BIT)
2580 		sc->bce_phy_flags |= BCE_PHY_SERDES_FLAG;
2581 
2582 	if (sc->bce_phy_flags & BCE_PHY_SERDES_FLAG) {
2583 		sc->bce_flags |= BCE_NO_WOL_FLAG;
2584 		if (BCE_CHIP_NUM(sc) != BCE_CHIP_NUM_5706) {
2585 			sc->bce_phy_addr = 2;
2586 			val = REG_RD_IND(sc, sc->bce_shmem_base +
2587 				 BCE_SHARED_HW_CFG_CONFIG);
2588 			if (val & BCE_SHARED_HW_CFG_PHY_2_5G) {
2589 				sc->bce_phy_flags |= BCE_PHY_2_5G_CAPABLE_FLAG;
2590 				DBPRINT(sc, BCE_INFO_LOAD, "Found 2.5Gb capable adapter\n");
2591 			}
2592 		}
2593 	} else if ((BCE_CHIP_NUM(sc) == BCE_CHIP_NUM_5706) ||
2594 		   (BCE_CHIP_NUM(sc) == BCE_CHIP_NUM_5708))
2595 		sc->bce_phy_flags |= BCE_PHY_CRC_FIX_FLAG;
2596 
2597 bce_get_media_exit:
2598 	DBPRINT(sc, (BCE_INFO_LOAD | BCE_INFO_PHY),
2599 		"Using PHY address %d.\n", sc->bce_phy_addr);
2600 
2601 	DBEXIT(BCE_VERBOSE);
2602 }
2603 
2604 
2605 /****************************************************************************/
2606 /* Free any DMA memory owned by the driver.                                 */
2607 /*                                                                          */
2608 /* Scans through each data structre that requires DMA memory and frees      */
2609 /* the memory if allocated.                                                 */
2610 /*                                                                          */
2611 /* Returns:                                                                 */
2612 /*   Nothing.                                                               */
2613 /****************************************************************************/
2614 static void
2615 bce_dma_free(struct bce_softc *sc)
2616 {
2617 	int i;
2618 
2619 	DBENTER(BCE_VERBOSE_RESET | BCE_VERBOSE_UNLOAD | BCE_VERBOSE_CTX);
2620 
2621 	/* Free, unmap, and destroy the status block. */
2622 	if (sc->status_block != NULL) {
2623 		bus_dmamem_free(
2624 			sc->status_tag,
2625 		    sc->status_block,
2626 		    sc->status_map);
2627 		sc->status_block = NULL;
2628 	}
2629 
2630 	if (sc->status_map != NULL) {
2631 		bus_dmamap_unload(
2632 			sc->status_tag,
2633 		    sc->status_map);
2634 		bus_dmamap_destroy(sc->status_tag,
2635 		    sc->status_map);
2636 		sc->status_map = NULL;
2637 	}
2638 
2639 	if (sc->status_tag != NULL) {
2640 		bus_dma_tag_destroy(sc->status_tag);
2641 		sc->status_tag = NULL;
2642 	}
2643 
2644 
2645 	/* Free, unmap, and destroy the statistics block. */
2646 	if (sc->stats_block != NULL) {
2647 		bus_dmamem_free(
2648 			sc->stats_tag,
2649 		    sc->stats_block,
2650 		    sc->stats_map);
2651 		sc->stats_block = NULL;
2652 	}
2653 
2654 	if (sc->stats_map != NULL) {
2655 		bus_dmamap_unload(
2656 			sc->stats_tag,
2657 		    sc->stats_map);
2658 		bus_dmamap_destroy(sc->stats_tag,
2659 		    sc->stats_map);
2660 		sc->stats_map = NULL;
2661 	}
2662 
2663 	if (sc->stats_tag != NULL) {
2664 		bus_dma_tag_destroy(sc->stats_tag);
2665 		sc->stats_tag = NULL;
2666 	}
2667 
2668 
2669 	/* Free, unmap and destroy all context memory pages. */
2670 	if ((BCE_CHIP_NUM(sc) == BCE_CHIP_NUM_5709) ||
2671 		(BCE_CHIP_NUM(sc) == BCE_CHIP_NUM_5716)) {
2672 		for (i = 0; i < sc->ctx_pages; i++ ) {
2673 			if (sc->ctx_block[i] != NULL) {
2674 				bus_dmamem_free(
2675 					sc->ctx_tag,
2676 				    sc->ctx_block[i],
2677 				    sc->ctx_map[i]);
2678 				sc->ctx_block[i] = NULL;
2679 			}
2680 
2681 			if (sc->ctx_map[i] != NULL) {
2682 				bus_dmamap_unload(
2683 					sc->ctx_tag,
2684 		    		sc->ctx_map[i]);
2685 				bus_dmamap_destroy(
2686 					sc->ctx_tag,
2687 				    sc->ctx_map[i]);
2688 				sc->ctx_map[i] = NULL;
2689 			}
2690 		}
2691 
2692 		/* Destroy the context memory tag. */
2693 		if (sc->ctx_tag != NULL) {
2694 			bus_dma_tag_destroy(sc->ctx_tag);
2695 			sc->ctx_tag = NULL;
2696 		}
2697 	}
2698 
2699 
2700 	/* Free, unmap and destroy all TX buffer descriptor chain pages. */
2701 	for (i = 0; i < TX_PAGES; i++ ) {
2702 		if (sc->tx_bd_chain[i] != NULL) {
2703 			bus_dmamem_free(
2704 				sc->tx_bd_chain_tag,
2705 			    sc->tx_bd_chain[i],
2706 			    sc->tx_bd_chain_map[i]);
2707 			sc->tx_bd_chain[i] = NULL;
2708 		}
2709 
2710 		if (sc->tx_bd_chain_map[i] != NULL) {
2711 			bus_dmamap_unload(
2712 				sc->tx_bd_chain_tag,
2713 		    	sc->tx_bd_chain_map[i]);
2714 			bus_dmamap_destroy(
2715 				sc->tx_bd_chain_tag,
2716 			    sc->tx_bd_chain_map[i]);
2717 			sc->tx_bd_chain_map[i] = NULL;
2718 		}
2719 	}
2720 
2721 	/* Destroy the TX buffer descriptor tag. */
2722 	if (sc->tx_bd_chain_tag != NULL) {
2723 		bus_dma_tag_destroy(sc->tx_bd_chain_tag);
2724 		sc->tx_bd_chain_tag = NULL;
2725 	}
2726 
2727 
2728 	/* Free, unmap and destroy all RX buffer descriptor chain pages. */
2729 	for (i = 0; i < RX_PAGES; i++ ) {
2730 		if (sc->rx_bd_chain[i] != NULL) {
2731 			bus_dmamem_free(
2732 				sc->rx_bd_chain_tag,
2733 			    sc->rx_bd_chain[i],
2734 			    sc->rx_bd_chain_map[i]);
2735 			sc->rx_bd_chain[i] = NULL;
2736 		}
2737 
2738 		if (sc->rx_bd_chain_map[i] != NULL) {
2739 			bus_dmamap_unload(
2740 				sc->rx_bd_chain_tag,
2741 		    	sc->rx_bd_chain_map[i]);
2742 			bus_dmamap_destroy(
2743 				sc->rx_bd_chain_tag,
2744 			    sc->rx_bd_chain_map[i]);
2745 			sc->rx_bd_chain_map[i] = NULL;
2746 		}
2747 	}
2748 
2749 	/* Destroy the RX buffer descriptor tag. */
2750 	if (sc->rx_bd_chain_tag != NULL) {
2751 		bus_dma_tag_destroy(sc->rx_bd_chain_tag);
2752 		sc->rx_bd_chain_tag = NULL;
2753 	}
2754 
2755 
2756 #ifdef ZERO_COPY_SOCKETS
2757 	/* Free, unmap and destroy all page buffer descriptor chain pages. */
2758 	for (i = 0; i < PG_PAGES; i++ ) {
2759 		if (sc->pg_bd_chain[i] != NULL) {
2760 			bus_dmamem_free(
2761 				sc->pg_bd_chain_tag,
2762 			    sc->pg_bd_chain[i],
2763 			    sc->pg_bd_chain_map[i]);
2764 			sc->pg_bd_chain[i] = NULL;
2765 		}
2766 
2767 		if (sc->pg_bd_chain_map[i] != NULL) {
2768 			bus_dmamap_unload(
2769 				sc->pg_bd_chain_tag,
2770 		    	sc->pg_bd_chain_map[i]);
2771 			bus_dmamap_destroy(
2772 				sc->pg_bd_chain_tag,
2773 			    sc->pg_bd_chain_map[i]);
2774 			sc->pg_bd_chain_map[i] = NULL;
2775 		}
2776 	}
2777 
2778 	/* Destroy the page buffer descriptor tag. */
2779 	if (sc->pg_bd_chain_tag != NULL) {
2780 		bus_dma_tag_destroy(sc->pg_bd_chain_tag);
2781 		sc->pg_bd_chain_tag = NULL;
2782 	}
2783 #endif
2784 
2785 
2786 	/* Unload and destroy the TX mbuf maps. */
2787 	for (i = 0; i < TOTAL_TX_BD; i++) {
2788 		if (sc->tx_mbuf_map[i] != NULL) {
2789 			bus_dmamap_unload(sc->tx_mbuf_tag,
2790 				sc->tx_mbuf_map[i]);
2791 			bus_dmamap_destroy(sc->tx_mbuf_tag,
2792 	 			sc->tx_mbuf_map[i]);
2793 			sc->tx_mbuf_map[i] = NULL;
2794 		}
2795 	}
2796 
2797 	/* Destroy the TX mbuf tag. */
2798 	if (sc->tx_mbuf_tag != NULL) {
2799 		bus_dma_tag_destroy(sc->tx_mbuf_tag);
2800 		sc->tx_mbuf_tag = NULL;
2801 	}
2802 
2803 	/* Unload and destroy the RX mbuf maps. */
2804 	for (i = 0; i < TOTAL_RX_BD; i++) {
2805 		if (sc->rx_mbuf_map[i] != NULL) {
2806 			bus_dmamap_unload(sc->rx_mbuf_tag,
2807 				sc->rx_mbuf_map[i]);
2808 			bus_dmamap_destroy(sc->rx_mbuf_tag,
2809 	 			sc->rx_mbuf_map[i]);
2810 			sc->rx_mbuf_map[i] = NULL;
2811 		}
2812 	}
2813 
2814 	/* Destroy the RX mbuf tag. */
2815 	if (sc->rx_mbuf_tag != NULL) {
2816 		bus_dma_tag_destroy(sc->rx_mbuf_tag);
2817 		sc->rx_mbuf_tag = NULL;
2818 	}
2819 
2820 #ifdef ZERO_COPY_SOCKETS
2821 	/* Unload and destroy the page mbuf maps. */
2822 	for (i = 0; i < TOTAL_PG_BD; i++) {
2823 		if (sc->pg_mbuf_map[i] != NULL) {
2824 			bus_dmamap_unload(sc->pg_mbuf_tag,
2825 				sc->pg_mbuf_map[i]);
2826 			bus_dmamap_destroy(sc->pg_mbuf_tag,
2827 	 			sc->pg_mbuf_map[i]);
2828 			sc->pg_mbuf_map[i] = NULL;
2829 		}
2830 	}
2831 
2832 	/* Destroy the page mbuf tag. */
2833 	if (sc->pg_mbuf_tag != NULL) {
2834 		bus_dma_tag_destroy(sc->pg_mbuf_tag);
2835 		sc->pg_mbuf_tag = NULL;
2836 	}
2837 #endif
2838 
2839 	/* Destroy the parent tag */
2840 	if (sc->parent_tag != NULL) {
2841 		bus_dma_tag_destroy(sc->parent_tag);
2842 		sc->parent_tag = NULL;
2843 	}
2844 
2845 	DBEXIT(BCE_VERBOSE_RESET | BCE_VERBOSE_UNLOAD | BCE_VERBOSE_CTX);
2846 }
2847 
2848 
2849 /****************************************************************************/
2850 /* Get DMA memory from the OS.                                              */
2851 /*                                                                          */
2852 /* Validates that the OS has provided DMA buffers in response to a          */
2853 /* bus_dmamap_load() call and saves the physical address of those buffers.  */
2854 /* When the callback is used the OS will return 0 for the mapping function  */
2855 /* (bus_dmamap_load()) so we use the value of map_arg->maxsegs to pass any  */
2856 /* failures back to the caller.                                             */
2857 /*                                                                          */
2858 /* Returns:                                                                 */
2859 /*   Nothing.                                                               */
2860 /****************************************************************************/
2861 static void
2862 bce_dma_map_addr(void *arg, bus_dma_segment_t *segs, int nseg, int error)
2863 {
2864 	bus_addr_t *busaddr = arg;
2865 
2866 	/* Simulate a mapping failure. */
2867 	DBRUNIF(DB_RANDOMTRUE(dma_map_addr_failed_sim_control),
2868 		error = ENOMEM);
2869 
2870 	/* Check for an error and signal the caller that an error occurred. */
2871 	if (error) {
2872 		*busaddr = 0;
2873 	} else {
2874 		*busaddr = segs->ds_addr;
2875 	}
2876 
2877 	return;
2878 }
2879 
2880 
2881 /****************************************************************************/
2882 /* Allocate any DMA memory needed by the driver.                            */
2883 /*                                                                          */
2884 /* Allocates DMA memory needed for the various global structures needed by  */
2885 /* hardware.                                                                */
2886 /*                                                                          */
2887 /* Memory alignment requirements:                                           */
2888 /* +-----------------+----------+----------+----------+----------+          */
2889 /* |                 |   5706   |   5708   |   5709   |   5716   |          */
2890 /* +-----------------+----------+----------+----------+----------+          */
2891 /* |Status Block     | 8 bytes  | 8 bytes  | 16 bytes | 16 bytes |          */
2892 /* |Statistics Block | 8 bytes  | 8 bytes  | 16 bytes | 16 bytes |          */
2893 /* |RX Buffers       | 16 bytes | 16 bytes | 16 bytes | 16 bytes |          */
2894 /* |PG Buffers       |   none   |   none   |   none   |   none   |          */
2895 /* |TX Buffers       |   none   |   none   |   none   |   none   |          */
2896 /* |Chain Pages(1)   |   4KiB   |   4KiB   |   4KiB   |   4KiB   |          */
2897 /* +-----------------+----------+----------+----------+----------+          */
2898 /*                                                                          */
2899 /* (1) Must align with CPU page size (BCM_PAGE_SZIE).                       */
2900 /*                                                                          */
2901 /* Returns:                                                                 */
2902 /*   0 for success, positive value for failure.                             */
2903 /****************************************************************************/
2904 static int
2905 bce_dma_alloc(device_t dev)
2906 {
2907 	struct bce_softc *sc;
2908 	int i, error, rc = 0;
2909 	bus_size_t max_size, max_seg_size;
2910 	int max_segments;
2911 
2912 	sc = device_get_softc(dev);
2913 
2914 	DBENTER(BCE_VERBOSE_RESET | BCE_VERBOSE_CTX);
2915 
2916 	/*
2917 	 * Allocate the parent bus DMA tag appropriate for PCI.
2918 	 */
2919 	if (bus_dma_tag_create(NULL,
2920 			1,
2921 			BCE_DMA_BOUNDARY,
2922 			sc->max_bus_addr,
2923 			BUS_SPACE_MAXADDR,
2924 			NULL, NULL,
2925 			MAXBSIZE,
2926 			BUS_SPACE_UNRESTRICTED,
2927 			BUS_SPACE_MAXSIZE_32BIT,
2928 			0,
2929 			NULL, NULL,
2930 			&sc->parent_tag)) {
2931 		BCE_PRINTF("%s(%d): Could not allocate parent DMA tag!\n",
2932 			__FILE__, __LINE__);
2933 		rc = ENOMEM;
2934 		goto bce_dma_alloc_exit;
2935 	}
2936 
2937 	/*
2938 	 * Create a DMA tag for the status block, allocate and clear the
2939 	 * memory, map the memory into DMA space, and fetch the physical
2940 	 * address of the block.
2941 	 */
2942 	if (bus_dma_tag_create(sc->parent_tag,
2943 	    	BCE_DMA_ALIGN,
2944 	    	BCE_DMA_BOUNDARY,
2945 	    	sc->max_bus_addr,
2946 	    	BUS_SPACE_MAXADDR,
2947 	    	NULL, NULL,
2948 	    	BCE_STATUS_BLK_SZ,
2949 	    	1,
2950 	    	BCE_STATUS_BLK_SZ,
2951 	    	0,
2952 	    	NULL, NULL,
2953 	    	&sc->status_tag)) {
2954 		BCE_PRINTF("%s(%d): Could not allocate status block DMA tag!\n",
2955 			__FILE__, __LINE__);
2956 		rc = ENOMEM;
2957 		goto bce_dma_alloc_exit;
2958 	}
2959 
2960 	if(bus_dmamem_alloc(sc->status_tag,
2961 	    	(void **)&sc->status_block,
2962 	    	BUS_DMA_NOWAIT,
2963 	    	&sc->status_map)) {
2964 		BCE_PRINTF("%s(%d): Could not allocate status block DMA memory!\n",
2965 			__FILE__, __LINE__);
2966 		rc = ENOMEM;
2967 		goto bce_dma_alloc_exit;
2968 	}
2969 
2970 	bzero((char *)sc->status_block, BCE_STATUS_BLK_SZ);
2971 
2972 	error = bus_dmamap_load(sc->status_tag,
2973 	    	sc->status_map,
2974 	    	sc->status_block,
2975 	    	BCE_STATUS_BLK_SZ,
2976 	    	bce_dma_map_addr,
2977 	    	&sc->status_block_paddr,
2978 	    	BUS_DMA_NOWAIT);
2979 
2980 	if (error) {
2981 		BCE_PRINTF("%s(%d): Could not map status block DMA memory!\n",
2982 			__FILE__, __LINE__);
2983 		rc = ENOMEM;
2984 		goto bce_dma_alloc_exit;
2985 	}
2986 
2987 	DBPRINT(sc, BCE_INFO, "%s(): status_block_paddr = 0x%jX\n",
2988 		__FUNCTION__, (uintmax_t) sc->status_block_paddr);
2989 
2990 	/*
2991 	 * Create a DMA tag for the statistics block, allocate and clear the
2992 	 * memory, map the memory into DMA space, and fetch the physical
2993 	 * address of the block.
2994 	 */
2995 	if (bus_dma_tag_create(sc->parent_tag,
2996 	    	BCE_DMA_ALIGN,
2997 	    	BCE_DMA_BOUNDARY,
2998 	    	sc->max_bus_addr,
2999 	    	BUS_SPACE_MAXADDR,
3000 	    	NULL, NULL,
3001 	    	BCE_STATS_BLK_SZ,
3002 	    	1,
3003 	    	BCE_STATS_BLK_SZ,
3004 	    	0,
3005 	    	NULL, NULL,
3006 	    	&sc->stats_tag)) {
3007 		BCE_PRINTF("%s(%d): Could not allocate statistics block DMA tag!\n",
3008 			__FILE__, __LINE__);
3009 		rc = ENOMEM;
3010 		goto bce_dma_alloc_exit;
3011 	}
3012 
3013 	if (bus_dmamem_alloc(sc->stats_tag,
3014 	    	(void **)&sc->stats_block,
3015 	    	BUS_DMA_NOWAIT,
3016 	    	&sc->stats_map)) {
3017 		BCE_PRINTF("%s(%d): Could not allocate statistics block DMA memory!\n",
3018 			__FILE__, __LINE__);
3019 		rc = ENOMEM;
3020 		goto bce_dma_alloc_exit;
3021 	}
3022 
3023 	bzero((char *)sc->stats_block, BCE_STATS_BLK_SZ);
3024 
3025 	error = bus_dmamap_load(sc->stats_tag,
3026 	    	sc->stats_map,
3027 	    	sc->stats_block,
3028 	    	BCE_STATS_BLK_SZ,
3029 	    	bce_dma_map_addr,
3030 	    	&sc->stats_block_paddr,
3031 	    	BUS_DMA_NOWAIT);
3032 
3033 	if(error) {
3034 		BCE_PRINTF("%s(%d): Could not map statistics block DMA memory!\n",
3035 			__FILE__, __LINE__);
3036 		rc = ENOMEM;
3037 		goto bce_dma_alloc_exit;
3038 	}
3039 
3040 	DBPRINT(sc, BCE_INFO, "%s(): stats_block_paddr = 0x%jX\n",
3041 		__FUNCTION__, (uintmax_t) sc->stats_block_paddr);
3042 
3043 	/* BCM5709 uses host memory as cache for context memory. */
3044 	if ((BCE_CHIP_NUM(sc) == BCE_CHIP_NUM_5709) ||
3045 		(BCE_CHIP_NUM(sc) == BCE_CHIP_NUM_5716)) {
3046 		sc->ctx_pages = 0x2000 / BCM_PAGE_SIZE;
3047 		if (sc->ctx_pages == 0)
3048 			sc->ctx_pages = 1;
3049 
3050 		DBRUNIF((sc->ctx_pages > 512),
3051 			BCE_PRINTF("%s(%d): Too many CTX pages! %d > 512\n",
3052 				__FILE__, __LINE__, sc->ctx_pages));
3053 
3054 		/*
3055 		 * Create a DMA tag for the context pages,
3056 		 * allocate and clear the memory, map the
3057 		 * memory into DMA space, and fetch the
3058 		 * physical address of the block.
3059 		 */
3060 		if(bus_dma_tag_create(sc->parent_tag,
3061 			BCM_PAGE_SIZE,
3062 		    BCE_DMA_BOUNDARY,
3063 			sc->max_bus_addr,
3064 			BUS_SPACE_MAXADDR,
3065 			NULL, NULL,
3066 			BCM_PAGE_SIZE,
3067 			1,
3068 			BCM_PAGE_SIZE,
3069 			0,
3070 			NULL, NULL,
3071 			&sc->ctx_tag)) {
3072 			BCE_PRINTF("%s(%d): Could not allocate CTX DMA tag!\n",
3073 				__FILE__, __LINE__);
3074 			rc = ENOMEM;
3075 			goto bce_dma_alloc_exit;
3076 		}
3077 
3078 		for (i = 0; i < sc->ctx_pages; i++) {
3079 
3080 			if(bus_dmamem_alloc(sc->ctx_tag,
3081 		    		(void **)&sc->ctx_block[i],
3082 	    		BUS_DMA_NOWAIT,
3083 		    	&sc->ctx_map[i])) {
3084 				BCE_PRINTF("%s(%d): Could not allocate CTX "
3085 					"DMA memory!\n", __FILE__, __LINE__);
3086 				rc = ENOMEM;
3087 				goto bce_dma_alloc_exit;
3088 			}
3089 
3090 			bzero((char *)sc->ctx_block[i], BCM_PAGE_SIZE);
3091 
3092 			error = bus_dmamap_load(sc->ctx_tag,
3093 	    		sc->ctx_map[i],
3094 	    		sc->ctx_block[i],
3095 		    	BCM_PAGE_SIZE,
3096 		    	bce_dma_map_addr,
3097 	    		&sc->ctx_paddr[i],
3098 	    		BUS_DMA_NOWAIT);
3099 
3100 			if (error) {
3101 				BCE_PRINTF("%s(%d): Could not map CTX DMA memory!\n",
3102 					__FILE__, __LINE__);
3103 				rc = ENOMEM;
3104 				goto bce_dma_alloc_exit;
3105 			}
3106 
3107 			DBPRINT(sc, BCE_INFO, "%s(): ctx_paddr[%d] = 0x%jX\n",
3108 				__FUNCTION__, i, (uintmax_t) sc->ctx_paddr[i]);
3109 		}
3110 	}
3111 
3112 	/*
3113 	 * Create a DMA tag for the TX buffer descriptor chain,
3114 	 * allocate and clear the  memory, and fetch the
3115 	 * physical address of the block.
3116 	 */
3117 	if(bus_dma_tag_create(sc->parent_tag,
3118 			BCM_PAGE_SIZE,
3119 		    BCE_DMA_BOUNDARY,
3120 			sc->max_bus_addr,
3121 			BUS_SPACE_MAXADDR,
3122 			NULL, NULL,
3123 			BCE_TX_CHAIN_PAGE_SZ,
3124 			1,
3125 			BCE_TX_CHAIN_PAGE_SZ,
3126 			0,
3127 			NULL, NULL,
3128 			&sc->tx_bd_chain_tag)) {
3129 		BCE_PRINTF("%s(%d): Could not allocate TX descriptor chain DMA tag!\n",
3130 			__FILE__, __LINE__);
3131 		rc = ENOMEM;
3132 		goto bce_dma_alloc_exit;
3133 	}
3134 
3135 	for (i = 0; i < TX_PAGES; i++) {
3136 
3137 		if(bus_dmamem_alloc(sc->tx_bd_chain_tag,
3138 	    		(void **)&sc->tx_bd_chain[i],
3139 	    		BUS_DMA_NOWAIT,
3140 		    	&sc->tx_bd_chain_map[i])) {
3141 			BCE_PRINTF("%s(%d): Could not allocate TX descriptor "
3142 				"chain DMA memory!\n", __FILE__, __LINE__);
3143 			rc = ENOMEM;
3144 			goto bce_dma_alloc_exit;
3145 		}
3146 
3147 		error = bus_dmamap_load(sc->tx_bd_chain_tag,
3148 	    		sc->tx_bd_chain_map[i],
3149 	    		sc->tx_bd_chain[i],
3150 		    	BCE_TX_CHAIN_PAGE_SZ,
3151 		    	bce_dma_map_addr,
3152 	    		&sc->tx_bd_chain_paddr[i],
3153 	    		BUS_DMA_NOWAIT);
3154 
3155 		if (error) {
3156 			BCE_PRINTF("%s(%d): Could not map TX descriptor chain DMA memory!\n",
3157 				__FILE__, __LINE__);
3158 			rc = ENOMEM;
3159 			goto bce_dma_alloc_exit;
3160 		}
3161 
3162 		DBPRINT(sc, BCE_INFO, "%s(): tx_bd_chain_paddr[%d] = 0x%jX\n",
3163 			__FUNCTION__, i, (uintmax_t) sc->tx_bd_chain_paddr[i]);
3164 	}
3165 
3166 	/* Check the required size before mapping to conserve resources. */
3167 	if (bce_tso_enable) {
3168 		max_size     = BCE_TSO_MAX_SIZE;
3169 		max_segments = BCE_MAX_SEGMENTS;
3170 		max_seg_size = BCE_TSO_MAX_SEG_SIZE;
3171 	} else {
3172 		max_size     = MCLBYTES * BCE_MAX_SEGMENTS;
3173 		max_segments = BCE_MAX_SEGMENTS;
3174 		max_seg_size = MCLBYTES;
3175 	}
3176 
3177 	/* Create a DMA tag for TX mbufs. */
3178 	if (bus_dma_tag_create(sc->parent_tag,
3179 			1,
3180 			BCE_DMA_BOUNDARY,
3181 			sc->max_bus_addr,
3182 			BUS_SPACE_MAXADDR,
3183 			NULL, NULL,
3184 			max_size,
3185 			max_segments,
3186 			max_seg_size,
3187 			0,
3188 			NULL, NULL,
3189 			&sc->tx_mbuf_tag)) {
3190 		BCE_PRINTF("%s(%d): Could not allocate TX mbuf DMA tag!\n",
3191 			__FILE__, __LINE__);
3192 		rc = ENOMEM;
3193 		goto bce_dma_alloc_exit;
3194 	}
3195 
3196 	/* Create DMA maps for the TX mbufs clusters. */
3197 	for (i = 0; i < TOTAL_TX_BD; i++) {
3198 		if (bus_dmamap_create(sc->tx_mbuf_tag, BUS_DMA_NOWAIT,
3199 			&sc->tx_mbuf_map[i])) {
3200 			BCE_PRINTF("%s(%d): Unable to create TX mbuf DMA map!\n",
3201 				__FILE__, __LINE__);
3202 			rc = ENOMEM;
3203 			goto bce_dma_alloc_exit;
3204 		}
3205 	}
3206 
3207 	/*
3208 	 * Create a DMA tag for the RX buffer descriptor chain,
3209 	 * allocate and clear the memory, and fetch the physical
3210 	 * address of the blocks.
3211 	 */
3212 	if (bus_dma_tag_create(sc->parent_tag,
3213 			BCM_PAGE_SIZE,
3214 			BCE_DMA_BOUNDARY,
3215 			BUS_SPACE_MAXADDR,
3216 			sc->max_bus_addr,
3217 			NULL, NULL,
3218 			BCE_RX_CHAIN_PAGE_SZ,
3219 			1,
3220 			BCE_RX_CHAIN_PAGE_SZ,
3221 			0,
3222 			NULL, NULL,
3223 			&sc->rx_bd_chain_tag)) {
3224 		BCE_PRINTF("%s(%d): Could not allocate RX descriptor chain DMA tag!\n",
3225 			__FILE__, __LINE__);
3226 		rc = ENOMEM;
3227 		goto bce_dma_alloc_exit;
3228 	}
3229 
3230 	for (i = 0; i < RX_PAGES; i++) {
3231 
3232 		if (bus_dmamem_alloc(sc->rx_bd_chain_tag,
3233 	    		(void **)&sc->rx_bd_chain[i],
3234 	    		BUS_DMA_NOWAIT,
3235 		    	&sc->rx_bd_chain_map[i])) {
3236 			BCE_PRINTF("%s(%d): Could not allocate RX descriptor chain "
3237 				"DMA memory!\n", __FILE__, __LINE__);
3238 			rc = ENOMEM;
3239 			goto bce_dma_alloc_exit;
3240 		}
3241 
3242 		bzero((char *)sc->rx_bd_chain[i], BCE_RX_CHAIN_PAGE_SZ);
3243 
3244 		error = bus_dmamap_load(sc->rx_bd_chain_tag,
3245 	    		sc->rx_bd_chain_map[i],
3246 	    		sc->rx_bd_chain[i],
3247 		    	BCE_RX_CHAIN_PAGE_SZ,
3248 		    	bce_dma_map_addr,
3249 	    		&sc->rx_bd_chain_paddr[i],
3250 	    		BUS_DMA_NOWAIT);
3251 
3252 		if (error) {
3253 			BCE_PRINTF("%s(%d): Could not map RX descriptor chain DMA memory!\n",
3254 				__FILE__, __LINE__);
3255 			rc = ENOMEM;
3256 			goto bce_dma_alloc_exit;
3257 		}
3258 
3259 		DBPRINT(sc, BCE_INFO, "%s(): rx_bd_chain_paddr[%d] = 0x%jX\n",
3260 			__FUNCTION__, i, (uintmax_t) sc->rx_bd_chain_paddr[i]);
3261 	}
3262 
3263 	/*
3264 	 * Create a DMA tag for RX mbufs.
3265 	 */
3266 #ifdef ZERO_COPY_SOCKETS
3267 	max_size = max_seg_size = ((sc->rx_bd_mbuf_alloc_size < MCLBYTES) ?
3268 		MCLBYTES : sc->rx_bd_mbuf_alloc_size);
3269 #else
3270 	max_size = max_seg_size = MJUM9BYTES;
3271 #endif
3272 	max_segments = 1;
3273 
3274 	DBPRINT(sc, BCE_INFO, "%s(): Creating rx_mbuf_tag (max size = 0x%jX "
3275 		"max segments = %d, max segment size = 0x%jX)\n", __FUNCTION__,
3276 		(uintmax_t) max_size, max_segments, (uintmax_t) max_seg_size);
3277 
3278 	if (bus_dma_tag_create(sc->parent_tag,
3279 			1,
3280 			BCE_DMA_BOUNDARY,
3281 			sc->max_bus_addr,
3282 			BUS_SPACE_MAXADDR,
3283 			NULL, NULL,
3284 			max_size,
3285 			max_segments,
3286 			max_seg_size,
3287 			0,
3288 			NULL, NULL,
3289 	    	&sc->rx_mbuf_tag)) {
3290 		BCE_PRINTF("%s(%d): Could not allocate RX mbuf DMA tag!\n",
3291 			__FILE__, __LINE__);
3292 		rc = ENOMEM;
3293 		goto bce_dma_alloc_exit;
3294 	}
3295 
3296 	/* Create DMA maps for the RX mbuf clusters. */
3297 	for (i = 0; i < TOTAL_RX_BD; i++) {
3298 		if (bus_dmamap_create(sc->rx_mbuf_tag, BUS_DMA_NOWAIT,
3299 				&sc->rx_mbuf_map[i])) {
3300 			BCE_PRINTF("%s(%d): Unable to create RX mbuf DMA map!\n",
3301 				__FILE__, __LINE__);
3302 			rc = ENOMEM;
3303 			goto bce_dma_alloc_exit;
3304 		}
3305 	}
3306 
3307 #ifdef ZERO_COPY_SOCKETS
3308 	/*
3309 	 * Create a DMA tag for the page buffer descriptor chain,
3310 	 * allocate and clear the memory, and fetch the physical
3311 	 * address of the blocks.
3312 	 */
3313 	if (bus_dma_tag_create(sc->parent_tag,
3314 			BCM_PAGE_SIZE,
3315 			BCE_DMA_BOUNDARY,
3316 			BUS_SPACE_MAXADDR,
3317 			sc->max_bus_addr,
3318 			NULL, NULL,
3319 			BCE_PG_CHAIN_PAGE_SZ,
3320 			1,
3321 			BCE_PG_CHAIN_PAGE_SZ,
3322 			0,
3323 			NULL, NULL,
3324 			&sc->pg_bd_chain_tag)) {
3325 		BCE_PRINTF("%s(%d): Could not allocate page descriptor chain DMA tag!\n",
3326 			__FILE__, __LINE__);
3327 		rc = ENOMEM;
3328 		goto bce_dma_alloc_exit;
3329 	}
3330 
3331 	for (i = 0; i < PG_PAGES; i++) {
3332 
3333 		if (bus_dmamem_alloc(sc->pg_bd_chain_tag,
3334 	    		(void **)&sc->pg_bd_chain[i],
3335 	    		BUS_DMA_NOWAIT,
3336 		    	&sc->pg_bd_chain_map[i])) {
3337 			BCE_PRINTF("%s(%d): Could not allocate page descriptor chain "
3338 				"DMA memory!\n", __FILE__, __LINE__);
3339 			rc = ENOMEM;
3340 			goto bce_dma_alloc_exit;
3341 		}
3342 
3343 		bzero((char *)sc->pg_bd_chain[i], BCE_PG_CHAIN_PAGE_SZ);
3344 
3345 		error = bus_dmamap_load(sc->pg_bd_chain_tag,
3346 	    		sc->pg_bd_chain_map[i],
3347 	    		sc->pg_bd_chain[i],
3348 		    	BCE_PG_CHAIN_PAGE_SZ,
3349 		    	bce_dma_map_addr,
3350 	    		&sc->pg_bd_chain_paddr[i],
3351 	    		BUS_DMA_NOWAIT);
3352 
3353 		if (error) {
3354 			BCE_PRINTF("%s(%d): Could not map page descriptor chain DMA memory!\n",
3355 				__FILE__, __LINE__);
3356 			rc = ENOMEM;
3357 			goto bce_dma_alloc_exit;
3358 		}
3359 
3360 		DBPRINT(sc, BCE_INFO, "%s(): pg_bd_chain_paddr[%d] = 0x%jX\n",
3361 			__FUNCTION__, i, (uintmax_t) sc->pg_bd_chain_paddr[i]);
3362 	}
3363 
3364 	/*
3365 	 * Create a DMA tag for page mbufs.
3366 	 */
3367 	max_size = max_seg_size = ((sc->pg_bd_mbuf_alloc_size < MCLBYTES) ?
3368 		MCLBYTES : sc->pg_bd_mbuf_alloc_size);
3369 
3370 	if (bus_dma_tag_create(sc->parent_tag,
3371 			1,
3372 			BCE_DMA_BOUNDARY,
3373 			sc->max_bus_addr,
3374 			BUS_SPACE_MAXADDR,
3375 			NULL, NULL,
3376 			max_size,
3377 			1,
3378 			max_seg_size,
3379 			0,
3380 			NULL, NULL,
3381 	    	&sc->pg_mbuf_tag)) {
3382 		BCE_PRINTF("%s(%d): Could not allocate page mbuf DMA tag!\n",
3383 			__FILE__, __LINE__);
3384 		rc = ENOMEM;
3385 		goto bce_dma_alloc_exit;
3386 	}
3387 
3388 	/* Create DMA maps for the page mbuf clusters. */
3389 	for (i = 0; i < TOTAL_PG_BD; i++) {
3390 		if (bus_dmamap_create(sc->pg_mbuf_tag, BUS_DMA_NOWAIT,
3391 				&sc->pg_mbuf_map[i])) {
3392 			BCE_PRINTF("%s(%d): Unable to create page mbuf DMA map!\n",
3393 				__FILE__, __LINE__);
3394 			rc = ENOMEM;
3395 			goto bce_dma_alloc_exit;
3396 		}
3397 	}
3398 #endif
3399 
3400 bce_dma_alloc_exit:
3401 	DBEXIT(BCE_VERBOSE_RESET | BCE_VERBOSE_CTX);
3402 	return(rc);
3403 }
3404 
3405 
3406 /****************************************************************************/
3407 /* Release all resources used by the driver.                                */
3408 /*                                                                          */
3409 /* Releases all resources acquired by the driver including interrupts,      */
3410 /* interrupt handler, interfaces, mutexes, and DMA memory.                  */
3411 /*                                                                          */
3412 /* Returns:                                                                 */
3413 /*   Nothing.                                                               */
3414 /****************************************************************************/
3415 static void
3416 bce_release_resources(struct bce_softc *sc)
3417 {
3418 	device_t dev;
3419 
3420 	DBENTER(BCE_VERBOSE_RESET);
3421 
3422 	dev = sc->bce_dev;
3423 
3424 	bce_dma_free(sc);
3425 
3426 	if (sc->bce_intrhand != NULL) {
3427 		DBPRINT(sc, BCE_INFO_RESET, "Removing interrupt handler.\n");
3428 		bus_teardown_intr(dev, sc->bce_res_irq, sc->bce_intrhand);
3429 	}
3430 
3431 	if (sc->bce_res_irq != NULL) {
3432 		DBPRINT(sc, BCE_INFO_RESET, "Releasing IRQ.\n");
3433 		bus_release_resource(dev, SYS_RES_IRQ, sc->bce_irq_rid,
3434 			sc->bce_res_irq);
3435 	}
3436 
3437 	if (sc->bce_flags & (BCE_USING_MSI_FLAG | BCE_USING_MSIX_FLAG)) {
3438 		DBPRINT(sc, BCE_INFO_RESET, "Releasing MSI/MSI-X vector.\n");
3439 		pci_release_msi(dev);
3440 	}
3441 
3442 	if (sc->bce_res_mem != NULL) {
3443 		DBPRINT(sc, BCE_INFO_RESET, "Releasing PCI memory.\n");
3444 		bus_release_resource(dev, SYS_RES_MEMORY, PCIR_BAR(0), sc->bce_res_mem);
3445 	}
3446 
3447 	if (sc->bce_ifp != NULL) {
3448 		DBPRINT(sc, BCE_INFO_RESET, "Releasing IF.\n");
3449 		if_free(sc->bce_ifp);
3450 	}
3451 
3452 	if (mtx_initialized(&sc->bce_mtx))
3453 		BCE_LOCK_DESTROY(sc);
3454 
3455 	DBEXIT(BCE_VERBOSE_RESET);
3456 }
3457 
3458 
3459 /****************************************************************************/
3460 /* Firmware synchronization.                                                */
3461 /*                                                                          */
3462 /* Before performing certain events such as a chip reset, synchronize with  */
3463 /* the firmware first.                                                      */
3464 /*                                                                          */
3465 /* Returns:                                                                 */
3466 /*   0 for success, positive value for failure.                             */
3467 /****************************************************************************/
3468 static int
3469 bce_fw_sync(struct bce_softc *sc, u32 msg_data)
3470 {
3471 	int i, rc = 0;
3472 	u32 val;
3473 
3474 	DBENTER(BCE_VERBOSE_RESET);
3475 
3476 	/* Don't waste any time if we've timed out before. */
3477 	if (sc->bce_fw_timed_out) {
3478 		rc = EBUSY;
3479 		goto bce_fw_sync_exit;
3480 	}
3481 
3482 	/* Increment the message sequence number. */
3483 	sc->bce_fw_wr_seq++;
3484 	msg_data |= sc->bce_fw_wr_seq;
3485 
3486  	DBPRINT(sc, BCE_VERBOSE_FIRMWARE, "bce_fw_sync(): msg_data = 0x%08X\n",
3487  		msg_data);
3488 
3489 	/* Send the message to the bootcode driver mailbox. */
3490 	REG_WR_IND(sc, sc->bce_shmem_base + BCE_DRV_MB, msg_data);
3491 
3492 	/* Wait for the bootcode to acknowledge the message. */
3493 	for (i = 0; i < FW_ACK_TIME_OUT_MS; i++) {
3494 		/* Check for a response in the bootcode firmware mailbox. */
3495 		val = REG_RD_IND(sc, sc->bce_shmem_base + BCE_FW_MB);
3496 		if ((val & BCE_FW_MSG_ACK) == (msg_data & BCE_DRV_MSG_SEQ))
3497 			break;
3498 		DELAY(1000);
3499 	}
3500 
3501 	/* If we've timed out, tell the bootcode that we've stopped waiting. */
3502 	if (((val & BCE_FW_MSG_ACK) != (msg_data & BCE_DRV_MSG_SEQ)) &&
3503 		((msg_data & BCE_DRV_MSG_DATA) != BCE_DRV_MSG_DATA_WAIT0)) {
3504 
3505 		BCE_PRINTF("%s(%d): Firmware synchronization timeout! "
3506 			"msg_data = 0x%08X\n",
3507 			__FILE__, __LINE__, msg_data);
3508 
3509 		msg_data &= ~BCE_DRV_MSG_CODE;
3510 		msg_data |= BCE_DRV_MSG_CODE_FW_TIMEOUT;
3511 
3512 		REG_WR_IND(sc, sc->bce_shmem_base + BCE_DRV_MB, msg_data);
3513 
3514 		sc->bce_fw_timed_out = 1;
3515 		rc = EBUSY;
3516 	}
3517 
3518 bce_fw_sync_exit:
3519 	DBEXIT(BCE_VERBOSE_RESET);
3520 	return (rc);
3521 }
3522 
3523 
3524 /****************************************************************************/
3525 /* Load Receive Virtual 2 Physical (RV2P) processor firmware.               */
3526 /*                                                                          */
3527 /* Returns:                                                                 */
3528 /*   Nothing.                                                               */
3529 /****************************************************************************/
3530 static void
3531 bce_load_rv2p_fw(struct bce_softc *sc, u32 *rv2p_code,
3532 	u32 rv2p_code_len, u32 rv2p_proc)
3533 {
3534 	int i;
3535 	u32 val;
3536 
3537 	DBENTER(BCE_VERBOSE_RESET);
3538 
3539 	/* Set the page size used by RV2P. */
3540 	if (rv2p_proc == RV2P_PROC2) {
3541 		BCE_RV2P_PROC2_CHG_MAX_BD_PAGE(USABLE_RX_BD_PER_PAGE);
3542 	}
3543 
3544 	for (i = 0; i < rv2p_code_len; i += 8) {
3545 		REG_WR(sc, BCE_RV2P_INSTR_HIGH, *rv2p_code);
3546 		rv2p_code++;
3547 		REG_WR(sc, BCE_RV2P_INSTR_LOW, *rv2p_code);
3548 		rv2p_code++;
3549 
3550 		if (rv2p_proc == RV2P_PROC1) {
3551 			val = (i / 8) | BCE_RV2P_PROC1_ADDR_CMD_RDWR;
3552 			REG_WR(sc, BCE_RV2P_PROC1_ADDR_CMD, val);
3553 		}
3554 		else {
3555 			val = (i / 8) | BCE_RV2P_PROC2_ADDR_CMD_RDWR;
3556 			REG_WR(sc, BCE_RV2P_PROC2_ADDR_CMD, val);
3557 		}
3558 	}
3559 
3560 	/* Reset the processor, un-stall is done later. */
3561 	if (rv2p_proc == RV2P_PROC1) {
3562 		REG_WR(sc, BCE_RV2P_COMMAND, BCE_RV2P_COMMAND_PROC1_RESET);
3563 	}
3564 	else {
3565 		REG_WR(sc, BCE_RV2P_COMMAND, BCE_RV2P_COMMAND_PROC2_RESET);
3566 	}
3567 
3568 	DBEXIT(BCE_VERBOSE_RESET);
3569 }
3570 
3571 
3572 /****************************************************************************/
3573 /* Load RISC processor firmware.                                            */
3574 /*                                                                          */
3575 /* Loads firmware from the file if_bcefw.h into the scratchpad memory       */
3576 /* associated with a particular processor.                                  */
3577 /*                                                                          */
3578 /* Returns:                                                                 */
3579 /*   Nothing.                                                               */
3580 /****************************************************************************/
3581 static void
3582 bce_load_cpu_fw(struct bce_softc *sc, struct cpu_reg *cpu_reg,
3583 	struct fw_info *fw)
3584 {
3585 	u32 offset;
3586 	u32 val;
3587 
3588 	DBENTER(BCE_VERBOSE_RESET);
3589 
3590 	/* Halt the CPU. */
3591 	val = REG_RD_IND(sc, cpu_reg->mode);
3592 	val |= cpu_reg->mode_value_halt;
3593 	REG_WR_IND(sc, cpu_reg->mode, val);
3594 	REG_WR_IND(sc, cpu_reg->state, cpu_reg->state_value_clear);
3595 
3596 	/* Load the Text area. */
3597 	offset = cpu_reg->spad_base + (fw->text_addr - cpu_reg->mips_view_base);
3598 	if (fw->text) {
3599 		int j;
3600 
3601 		for (j = 0; j < (fw->text_len / 4); j++, offset += 4) {
3602 			REG_WR_IND(sc, offset, fw->text[j]);
3603 	        }
3604 	}
3605 
3606 	/* Load the Data area. */
3607 	offset = cpu_reg->spad_base + (fw->data_addr - cpu_reg->mips_view_base);
3608 	if (fw->data) {
3609 		int j;
3610 
3611 		for (j = 0; j < (fw->data_len / 4); j++, offset += 4) {
3612 			REG_WR_IND(sc, offset, fw->data[j]);
3613 		}
3614 	}
3615 
3616 	/* Load the SBSS area. */
3617 	offset = cpu_reg->spad_base + (fw->sbss_addr - cpu_reg->mips_view_base);
3618 	if (fw->sbss) {
3619 		int j;
3620 
3621 		for (j = 0; j < (fw->sbss_len / 4); j++, offset += 4) {
3622 			REG_WR_IND(sc, offset, fw->sbss[j]);
3623 		}
3624 	}
3625 
3626 	/* Load the BSS area. */
3627 	offset = cpu_reg->spad_base + (fw->bss_addr - cpu_reg->mips_view_base);
3628 	if (fw->bss) {
3629 		int j;
3630 
3631 		for (j = 0; j < (fw->bss_len/4); j++, offset += 4) {
3632 			REG_WR_IND(sc, offset, fw->bss[j]);
3633 		}
3634 	}
3635 
3636 	/* Load the Read-Only area. */
3637 	offset = cpu_reg->spad_base +
3638 		(fw->rodata_addr - cpu_reg->mips_view_base);
3639 	if (fw->rodata) {
3640 		int j;
3641 
3642 		for (j = 0; j < (fw->rodata_len / 4); j++, offset += 4) {
3643 			REG_WR_IND(sc, offset, fw->rodata[j]);
3644 		}
3645 	}
3646 
3647 	/* Clear the pre-fetch instruction. */
3648 	REG_WR_IND(sc, cpu_reg->inst, 0);
3649 	REG_WR_IND(sc, cpu_reg->pc, fw->start_addr);
3650 
3651 	/* Start the CPU. */
3652 	val = REG_RD_IND(sc, cpu_reg->mode);
3653 	val &= ~cpu_reg->mode_value_halt;
3654 	REG_WR_IND(sc, cpu_reg->state, cpu_reg->state_value_clear);
3655 	REG_WR_IND(sc, cpu_reg->mode, val);
3656 
3657 	DBEXIT(BCE_VERBOSE_RESET);
3658 }
3659 
3660 
3661 /****************************************************************************/
3662 /* Initialize the RX CPU.                                                   */
3663 /*                                                                          */
3664 /* Returns:                                                                 */
3665 /*   Nothing.                                                               */
3666 /****************************************************************************/
3667 static void
3668 bce_init_rxp_cpu(struct bce_softc *sc)
3669 {
3670 	struct cpu_reg cpu_reg;
3671 	struct fw_info fw;
3672 
3673 	DBENTER(BCE_VERBOSE_RESET);
3674 
3675 	cpu_reg.mode = BCE_RXP_CPU_MODE;
3676 	cpu_reg.mode_value_halt = BCE_RXP_CPU_MODE_SOFT_HALT;
3677 	cpu_reg.mode_value_sstep = BCE_RXP_CPU_MODE_STEP_ENA;
3678 	cpu_reg.state = BCE_RXP_CPU_STATE;
3679 	cpu_reg.state_value_clear = 0xffffff;
3680 	cpu_reg.gpr0 = BCE_RXP_CPU_REG_FILE;
3681 	cpu_reg.evmask = BCE_RXP_CPU_EVENT_MASK;
3682 	cpu_reg.pc = BCE_RXP_CPU_PROGRAM_COUNTER;
3683 	cpu_reg.inst = BCE_RXP_CPU_INSTRUCTION;
3684 	cpu_reg.bp = BCE_RXP_CPU_HW_BREAKPOINT;
3685 	cpu_reg.spad_base = BCE_RXP_SCRATCH;
3686 	cpu_reg.mips_view_base = 0x8000000;
3687 
3688 	if ((BCE_CHIP_NUM(sc) == BCE_CHIP_NUM_5709) ||
3689 		(BCE_CHIP_NUM(sc) == BCE_CHIP_NUM_5716)) {
3690  		fw.ver_major = bce_RXP_b09FwReleaseMajor;
3691 		fw.ver_minor = bce_RXP_b09FwReleaseMinor;
3692 		fw.ver_fix = bce_RXP_b09FwReleaseFix;
3693 		fw.start_addr = bce_RXP_b09FwStartAddr;
3694 
3695 		fw.text_addr = bce_RXP_b09FwTextAddr;
3696 		fw.text_len = bce_RXP_b09FwTextLen;
3697 		fw.text_index = 0;
3698 		fw.text = bce_RXP_b09FwText;
3699 
3700 		fw.data_addr = bce_RXP_b09FwDataAddr;
3701 		fw.data_len = bce_RXP_b09FwDataLen;
3702 		fw.data_index = 0;
3703 		fw.data = bce_RXP_b09FwData;
3704 
3705 		fw.sbss_addr = bce_RXP_b09FwSbssAddr;
3706 		fw.sbss_len = bce_RXP_b09FwSbssLen;
3707 		fw.sbss_index = 0;
3708 		fw.sbss = bce_RXP_b09FwSbss;
3709 
3710 		fw.bss_addr = bce_RXP_b09FwBssAddr;
3711 		fw.bss_len = bce_RXP_b09FwBssLen;
3712 		fw.bss_index = 0;
3713 		fw.bss = bce_RXP_b09FwBss;
3714 
3715 		fw.rodata_addr = bce_RXP_b09FwRodataAddr;
3716 		fw.rodata_len = bce_RXP_b09FwRodataLen;
3717 		fw.rodata_index = 0;
3718 		fw.rodata = bce_RXP_b09FwRodata;
3719 	} else {
3720 		fw.ver_major = bce_RXP_b06FwReleaseMajor;
3721 		fw.ver_minor = bce_RXP_b06FwReleaseMinor;
3722 		fw.ver_fix = bce_RXP_b06FwReleaseFix;
3723 		fw.start_addr = bce_RXP_b06FwStartAddr;
3724 
3725 		fw.text_addr = bce_RXP_b06FwTextAddr;
3726 		fw.text_len = bce_RXP_b06FwTextLen;
3727 		fw.text_index = 0;
3728 		fw.text = bce_RXP_b06FwText;
3729 
3730 		fw.data_addr = bce_RXP_b06FwDataAddr;
3731 		fw.data_len = bce_RXP_b06FwDataLen;
3732 		fw.data_index = 0;
3733 		fw.data = bce_RXP_b06FwData;
3734 
3735 		fw.sbss_addr = bce_RXP_b06FwSbssAddr;
3736 		fw.sbss_len = bce_RXP_b06FwSbssLen;
3737 		fw.sbss_index = 0;
3738 		fw.sbss = bce_RXP_b06FwSbss;
3739 
3740 		fw.bss_addr = bce_RXP_b06FwBssAddr;
3741 		fw.bss_len = bce_RXP_b06FwBssLen;
3742 		fw.bss_index = 0;
3743 		fw.bss = bce_RXP_b06FwBss;
3744 
3745 		fw.rodata_addr = bce_RXP_b06FwRodataAddr;
3746 		fw.rodata_len = bce_RXP_b06FwRodataLen;
3747 		fw.rodata_index = 0;
3748 		fw.rodata = bce_RXP_b06FwRodata;
3749 	}
3750 
3751 	DBPRINT(sc, BCE_INFO_RESET, "Loading RX firmware.\n");
3752 	bce_load_cpu_fw(sc, &cpu_reg, &fw);
3753 
3754 	DBEXIT(BCE_VERBOSE_RESET);
3755 }
3756 
3757 
3758 /****************************************************************************/
3759 /* Initialize the TX CPU.                                                   */
3760 /*                                                                          */
3761 /* Returns:                                                                 */
3762 /*   Nothing.                                                               */
3763 /****************************************************************************/
3764 static void
3765 bce_init_txp_cpu(struct bce_softc *sc)
3766 {
3767 	struct cpu_reg cpu_reg;
3768 	struct fw_info fw;
3769 
3770 	DBENTER(BCE_VERBOSE_RESET);
3771 
3772 	cpu_reg.mode = BCE_TXP_CPU_MODE;
3773 	cpu_reg.mode_value_halt = BCE_TXP_CPU_MODE_SOFT_HALT;
3774 	cpu_reg.mode_value_sstep = BCE_TXP_CPU_MODE_STEP_ENA;
3775 	cpu_reg.state = BCE_TXP_CPU_STATE;
3776 	cpu_reg.state_value_clear = 0xffffff;
3777 	cpu_reg.gpr0 = BCE_TXP_CPU_REG_FILE;
3778 	cpu_reg.evmask = BCE_TXP_CPU_EVENT_MASK;
3779 	cpu_reg.pc = BCE_TXP_CPU_PROGRAM_COUNTER;
3780 	cpu_reg.inst = BCE_TXP_CPU_INSTRUCTION;
3781 	cpu_reg.bp = BCE_TXP_CPU_HW_BREAKPOINT;
3782 	cpu_reg.spad_base = BCE_TXP_SCRATCH;
3783 	cpu_reg.mips_view_base = 0x8000000;
3784 
3785 	if ((BCE_CHIP_NUM(sc) == BCE_CHIP_NUM_5709) ||
3786 		(BCE_CHIP_NUM(sc) == BCE_CHIP_NUM_5716)) {
3787 		fw.ver_major = bce_TXP_b09FwReleaseMajor;
3788 		fw.ver_minor = bce_TXP_b09FwReleaseMinor;
3789 		fw.ver_fix = bce_TXP_b09FwReleaseFix;
3790 		fw.start_addr = bce_TXP_b09FwStartAddr;
3791 
3792 		fw.text_addr = bce_TXP_b09FwTextAddr;
3793 		fw.text_len = bce_TXP_b09FwTextLen;
3794 		fw.text_index = 0;
3795 		fw.text = bce_TXP_b09FwText;
3796 
3797 		fw.data_addr = bce_TXP_b09FwDataAddr;
3798 		fw.data_len = bce_TXP_b09FwDataLen;
3799 		fw.data_index = 0;
3800 		fw.data = bce_TXP_b09FwData;
3801 
3802 		fw.sbss_addr = bce_TXP_b09FwSbssAddr;
3803 		fw.sbss_len = bce_TXP_b09FwSbssLen;
3804 		fw.sbss_index = 0;
3805 		fw.sbss = bce_TXP_b09FwSbss;
3806 
3807 		fw.bss_addr = bce_TXP_b09FwBssAddr;
3808 		fw.bss_len = bce_TXP_b09FwBssLen;
3809 		fw.bss_index = 0;
3810 		fw.bss = bce_TXP_b09FwBss;
3811 
3812 		fw.rodata_addr = bce_TXP_b09FwRodataAddr;
3813 		fw.rodata_len = bce_TXP_b09FwRodataLen;
3814 		fw.rodata_index = 0;
3815 		fw.rodata = bce_TXP_b09FwRodata;
3816 	} else {
3817 		fw.ver_major = bce_TXP_b06FwReleaseMajor;
3818 		fw.ver_minor = bce_TXP_b06FwReleaseMinor;
3819 		fw.ver_fix = bce_TXP_b06FwReleaseFix;
3820 		fw.start_addr = bce_TXP_b06FwStartAddr;
3821 
3822 		fw.text_addr = bce_TXP_b06FwTextAddr;
3823 		fw.text_len = bce_TXP_b06FwTextLen;
3824 		fw.text_index = 0;
3825 		fw.text = bce_TXP_b06FwText;
3826 
3827 		fw.data_addr = bce_TXP_b06FwDataAddr;
3828 		fw.data_len = bce_TXP_b06FwDataLen;
3829 		fw.data_index = 0;
3830 		fw.data = bce_TXP_b06FwData;
3831 
3832 		fw.sbss_addr = bce_TXP_b06FwSbssAddr;
3833 		fw.sbss_len = bce_TXP_b06FwSbssLen;
3834 		fw.sbss_index = 0;
3835 		fw.sbss = bce_TXP_b06FwSbss;
3836 
3837 		fw.bss_addr = bce_TXP_b06FwBssAddr;
3838 		fw.bss_len = bce_TXP_b06FwBssLen;
3839 		fw.bss_index = 0;
3840 		fw.bss = bce_TXP_b06FwBss;
3841 
3842 		fw.rodata_addr = bce_TXP_b06FwRodataAddr;
3843 		fw.rodata_len = bce_TXP_b06FwRodataLen;
3844 		fw.rodata_index = 0;
3845 		fw.rodata = bce_TXP_b06FwRodata;
3846 	}
3847 
3848 	DBPRINT(sc, BCE_INFO_RESET, "Loading TX firmware.\n");
3849 	bce_load_cpu_fw(sc, &cpu_reg, &fw);
3850 
3851 	DBEXIT(BCE_VERBOSE_RESET);
3852 }
3853 
3854 
3855 /****************************************************************************/
3856 /* Initialize the TPAT CPU.                                                 */
3857 /*                                                                          */
3858 /* Returns:                                                                 */
3859 /*   Nothing.                                                               */
3860 /****************************************************************************/
3861 static void
3862 bce_init_tpat_cpu(struct bce_softc *sc)
3863 {
3864 	struct cpu_reg cpu_reg;
3865 	struct fw_info fw;
3866 
3867 	DBENTER(BCE_VERBOSE_RESET);
3868 
3869 	cpu_reg.mode = BCE_TPAT_CPU_MODE;
3870 	cpu_reg.mode_value_halt = BCE_TPAT_CPU_MODE_SOFT_HALT;
3871 	cpu_reg.mode_value_sstep = BCE_TPAT_CPU_MODE_STEP_ENA;
3872 	cpu_reg.state = BCE_TPAT_CPU_STATE;
3873 	cpu_reg.state_value_clear = 0xffffff;
3874 	cpu_reg.gpr0 = BCE_TPAT_CPU_REG_FILE;
3875 	cpu_reg.evmask = BCE_TPAT_CPU_EVENT_MASK;
3876 	cpu_reg.pc = BCE_TPAT_CPU_PROGRAM_COUNTER;
3877 	cpu_reg.inst = BCE_TPAT_CPU_INSTRUCTION;
3878 	cpu_reg.bp = BCE_TPAT_CPU_HW_BREAKPOINT;
3879 	cpu_reg.spad_base = BCE_TPAT_SCRATCH;
3880 	cpu_reg.mips_view_base = 0x8000000;
3881 
3882 	if ((BCE_CHIP_NUM(sc) == BCE_CHIP_NUM_5709) ||
3883 		(BCE_CHIP_NUM(sc) == BCE_CHIP_NUM_5716)) {
3884 		fw.ver_major = bce_TPAT_b09FwReleaseMajor;
3885 		fw.ver_minor = bce_TPAT_b09FwReleaseMinor;
3886 		fw.ver_fix = bce_TPAT_b09FwReleaseFix;
3887 		fw.start_addr = bce_TPAT_b09FwStartAddr;
3888 
3889 		fw.text_addr = bce_TPAT_b09FwTextAddr;
3890 		fw.text_len = bce_TPAT_b09FwTextLen;
3891 		fw.text_index = 0;
3892 		fw.text = bce_TPAT_b09FwText;
3893 
3894 		fw.data_addr = bce_TPAT_b09FwDataAddr;
3895 		fw.data_len = bce_TPAT_b09FwDataLen;
3896 		fw.data_index = 0;
3897 		fw.data = bce_TPAT_b09FwData;
3898 
3899 		fw.sbss_addr = bce_TPAT_b09FwSbssAddr;
3900 		fw.sbss_len = bce_TPAT_b09FwSbssLen;
3901 		fw.sbss_index = 0;
3902 		fw.sbss = bce_TPAT_b09FwSbss;
3903 
3904 		fw.bss_addr = bce_TPAT_b09FwBssAddr;
3905 		fw.bss_len = bce_TPAT_b09FwBssLen;
3906 		fw.bss_index = 0;
3907 		fw.bss = bce_TPAT_b09FwBss;
3908 
3909 		fw.rodata_addr = bce_TPAT_b09FwRodataAddr;
3910 		fw.rodata_len = bce_TPAT_b09FwRodataLen;
3911 		fw.rodata_index = 0;
3912 		fw.rodata = bce_TPAT_b09FwRodata;
3913 	} else {
3914 		fw.ver_major = bce_TPAT_b06FwReleaseMajor;
3915 		fw.ver_minor = bce_TPAT_b06FwReleaseMinor;
3916 		fw.ver_fix = bce_TPAT_b06FwReleaseFix;
3917 		fw.start_addr = bce_TPAT_b06FwStartAddr;
3918 
3919 		fw.text_addr = bce_TPAT_b06FwTextAddr;
3920 		fw.text_len = bce_TPAT_b06FwTextLen;
3921 		fw.text_index = 0;
3922 		fw.text = bce_TPAT_b06FwText;
3923 
3924 		fw.data_addr = bce_TPAT_b06FwDataAddr;
3925 		fw.data_len = bce_TPAT_b06FwDataLen;
3926 		fw.data_index = 0;
3927 		fw.data = bce_TPAT_b06FwData;
3928 
3929 		fw.sbss_addr = bce_TPAT_b06FwSbssAddr;
3930 		fw.sbss_len = bce_TPAT_b06FwSbssLen;
3931 		fw.sbss_index = 0;
3932 		fw.sbss = bce_TPAT_b06FwSbss;
3933 
3934 		fw.bss_addr = bce_TPAT_b06FwBssAddr;
3935 		fw.bss_len = bce_TPAT_b06FwBssLen;
3936 		fw.bss_index = 0;
3937 		fw.bss = bce_TPAT_b06FwBss;
3938 
3939 		fw.rodata_addr = bce_TPAT_b06FwRodataAddr;
3940 		fw.rodata_len = bce_TPAT_b06FwRodataLen;
3941 		fw.rodata_index = 0;
3942 		fw.rodata = bce_TPAT_b06FwRodata;
3943 	}
3944 
3945 	DBPRINT(sc, BCE_INFO_RESET, "Loading TPAT firmware.\n");
3946 	bce_load_cpu_fw(sc, &cpu_reg, &fw);
3947 
3948 	DBEXIT(BCE_VERBOSE_RESET);
3949 }
3950 
3951 
3952 /****************************************************************************/
3953 /* Initialize the CP CPU.                                                   */
3954 /*                                                                          */
3955 /* Returns:                                                                 */
3956 /*   Nothing.                                                               */
3957 /****************************************************************************/
3958 static void
3959 bce_init_cp_cpu(struct bce_softc *sc)
3960 {
3961 	struct cpu_reg cpu_reg;
3962 	struct fw_info fw;
3963 
3964 	DBENTER(BCE_VERBOSE_RESET);
3965 
3966 	cpu_reg.mode = BCE_CP_CPU_MODE;
3967 	cpu_reg.mode_value_halt = BCE_CP_CPU_MODE_SOFT_HALT;
3968 	cpu_reg.mode_value_sstep = BCE_CP_CPU_MODE_STEP_ENA;
3969 	cpu_reg.state = BCE_CP_CPU_STATE;
3970 	cpu_reg.state_value_clear = 0xffffff;
3971 	cpu_reg.gpr0 = BCE_CP_CPU_REG_FILE;
3972 	cpu_reg.evmask = BCE_CP_CPU_EVENT_MASK;
3973 	cpu_reg.pc = BCE_CP_CPU_PROGRAM_COUNTER;
3974 	cpu_reg.inst = BCE_CP_CPU_INSTRUCTION;
3975 	cpu_reg.bp = BCE_CP_CPU_HW_BREAKPOINT;
3976 	cpu_reg.spad_base = BCE_CP_SCRATCH;
3977 	cpu_reg.mips_view_base = 0x8000000;
3978 
3979 	if ((BCE_CHIP_NUM(sc) == BCE_CHIP_NUM_5709) ||
3980 		(BCE_CHIP_NUM(sc) == BCE_CHIP_NUM_5716)) {
3981 		fw.ver_major = bce_CP_b09FwReleaseMajor;
3982 		fw.ver_minor = bce_CP_b09FwReleaseMinor;
3983 		fw.ver_fix = bce_CP_b09FwReleaseFix;
3984 		fw.start_addr = bce_CP_b09FwStartAddr;
3985 
3986 		fw.text_addr = bce_CP_b09FwTextAddr;
3987 		fw.text_len = bce_CP_b09FwTextLen;
3988 		fw.text_index = 0;
3989 		fw.text = bce_CP_b09FwText;
3990 
3991 		fw.data_addr = bce_CP_b09FwDataAddr;
3992 		fw.data_len = bce_CP_b09FwDataLen;
3993 		fw.data_index = 0;
3994 		fw.data = bce_CP_b09FwData;
3995 
3996 		fw.sbss_addr = bce_CP_b09FwSbssAddr;
3997 		fw.sbss_len = bce_CP_b09FwSbssLen;
3998 		fw.sbss_index = 0;
3999 		fw.sbss = bce_CP_b09FwSbss;
4000 
4001 		fw.bss_addr = bce_CP_b09FwBssAddr;
4002 		fw.bss_len = bce_CP_b09FwBssLen;
4003 		fw.bss_index = 0;
4004 		fw.bss = bce_CP_b09FwBss;
4005 
4006 		fw.rodata_addr = bce_CP_b09FwRodataAddr;
4007 		fw.rodata_len = bce_CP_b09FwRodataLen;
4008 		fw.rodata_index = 0;
4009 		fw.rodata = bce_CP_b09FwRodata;
4010 	} else {
4011 		fw.ver_major = bce_CP_b06FwReleaseMajor;
4012 		fw.ver_minor = bce_CP_b06FwReleaseMinor;
4013 		fw.ver_fix = bce_CP_b06FwReleaseFix;
4014 		fw.start_addr = bce_CP_b06FwStartAddr;
4015 
4016 		fw.text_addr = bce_CP_b06FwTextAddr;
4017 		fw.text_len = bce_CP_b06FwTextLen;
4018 		fw.text_index = 0;
4019 		fw.text = bce_CP_b06FwText;
4020 
4021 		fw.data_addr = bce_CP_b06FwDataAddr;
4022 		fw.data_len = bce_CP_b06FwDataLen;
4023 		fw.data_index = 0;
4024 		fw.data = bce_CP_b06FwData;
4025 
4026 		fw.sbss_addr = bce_CP_b06FwSbssAddr;
4027 		fw.sbss_len = bce_CP_b06FwSbssLen;
4028 		fw.sbss_index = 0;
4029 		fw.sbss = bce_CP_b06FwSbss;
4030 
4031 		fw.bss_addr = bce_CP_b06FwBssAddr;
4032 		fw.bss_len = bce_CP_b06FwBssLen;
4033 		fw.bss_index = 0;
4034 		fw.bss = bce_CP_b06FwBss;
4035 
4036 		fw.rodata_addr = bce_CP_b06FwRodataAddr;
4037 		fw.rodata_len = bce_CP_b06FwRodataLen;
4038 		fw.rodata_index = 0;
4039 		fw.rodata = bce_CP_b06FwRodata;
4040 	}
4041 
4042 	DBPRINT(sc, BCE_INFO_RESET, "Loading CP firmware.\n");
4043 	bce_load_cpu_fw(sc, &cpu_reg, &fw);
4044 
4045 	DBEXIT(BCE_VERBOSE_RESET);
4046 }
4047 
4048 
4049 /****************************************************************************/
4050 /* Initialize the COM CPU.                                                 */
4051 /*                                                                          */
4052 /* Returns:                                                                 */
4053 /*   Nothing.                                                               */
4054 /****************************************************************************/
4055 static void
4056 bce_init_com_cpu(struct bce_softc *sc)
4057 {
4058 	struct cpu_reg cpu_reg;
4059 	struct fw_info fw;
4060 
4061 	DBENTER(BCE_VERBOSE_RESET);
4062 
4063 	cpu_reg.mode = BCE_COM_CPU_MODE;
4064 	cpu_reg.mode_value_halt = BCE_COM_CPU_MODE_SOFT_HALT;
4065 	cpu_reg.mode_value_sstep = BCE_COM_CPU_MODE_STEP_ENA;
4066 	cpu_reg.state = BCE_COM_CPU_STATE;
4067 	cpu_reg.state_value_clear = 0xffffff;
4068 	cpu_reg.gpr0 = BCE_COM_CPU_REG_FILE;
4069 	cpu_reg.evmask = BCE_COM_CPU_EVENT_MASK;
4070 	cpu_reg.pc = BCE_COM_CPU_PROGRAM_COUNTER;
4071 	cpu_reg.inst = BCE_COM_CPU_INSTRUCTION;
4072 	cpu_reg.bp = BCE_COM_CPU_HW_BREAKPOINT;
4073 	cpu_reg.spad_base = BCE_COM_SCRATCH;
4074 	cpu_reg.mips_view_base = 0x8000000;
4075 
4076 	if ((BCE_CHIP_NUM(sc) == BCE_CHIP_NUM_5709) ||
4077 		(BCE_CHIP_NUM(sc) == BCE_CHIP_NUM_5716)) {
4078 		fw.ver_major = bce_COM_b09FwReleaseMajor;
4079 		fw.ver_minor = bce_COM_b09FwReleaseMinor;
4080 		fw.ver_fix = bce_COM_b09FwReleaseFix;
4081 		fw.start_addr = bce_COM_b09FwStartAddr;
4082 
4083 		fw.text_addr = bce_COM_b09FwTextAddr;
4084 		fw.text_len = bce_COM_b09FwTextLen;
4085 		fw.text_index = 0;
4086 		fw.text = bce_COM_b09FwText;
4087 
4088 		fw.data_addr = bce_COM_b09FwDataAddr;
4089 		fw.data_len = bce_COM_b09FwDataLen;
4090 		fw.data_index = 0;
4091 		fw.data = bce_COM_b09FwData;
4092 
4093 		fw.sbss_addr = bce_COM_b09FwSbssAddr;
4094 		fw.sbss_len = bce_COM_b09FwSbssLen;
4095 		fw.sbss_index = 0;
4096 		fw.sbss = bce_COM_b09FwSbss;
4097 
4098 		fw.bss_addr = bce_COM_b09FwBssAddr;
4099 		fw.bss_len = bce_COM_b09FwBssLen;
4100 		fw.bss_index = 0;
4101 		fw.bss = bce_COM_b09FwBss;
4102 
4103 		fw.rodata_addr = bce_COM_b09FwRodataAddr;
4104 		fw.rodata_len = bce_COM_b09FwRodataLen;
4105 		fw.rodata_index = 0;
4106 		fw.rodata = bce_COM_b09FwRodata;
4107 	} else {
4108 		fw.ver_major = bce_COM_b06FwReleaseMajor;
4109 		fw.ver_minor = bce_COM_b06FwReleaseMinor;
4110 		fw.ver_fix = bce_COM_b06FwReleaseFix;
4111 		fw.start_addr = bce_COM_b06FwStartAddr;
4112 
4113 		fw.text_addr = bce_COM_b06FwTextAddr;
4114 		fw.text_len = bce_COM_b06FwTextLen;
4115 		fw.text_index = 0;
4116 		fw.text = bce_COM_b06FwText;
4117 
4118 		fw.data_addr = bce_COM_b06FwDataAddr;
4119 		fw.data_len = bce_COM_b06FwDataLen;
4120 		fw.data_index = 0;
4121 		fw.data = bce_COM_b06FwData;
4122 
4123 		fw.sbss_addr = bce_COM_b06FwSbssAddr;
4124 		fw.sbss_len = bce_COM_b06FwSbssLen;
4125 		fw.sbss_index = 0;
4126 		fw.sbss = bce_COM_b06FwSbss;
4127 
4128 		fw.bss_addr = bce_COM_b06FwBssAddr;
4129 		fw.bss_len = bce_COM_b06FwBssLen;
4130 		fw.bss_index = 0;
4131 		fw.bss = bce_COM_b06FwBss;
4132 
4133 		fw.rodata_addr = bce_COM_b06FwRodataAddr;
4134 		fw.rodata_len = bce_COM_b06FwRodataLen;
4135 		fw.rodata_index = 0;
4136 		fw.rodata = bce_COM_b06FwRodata;
4137 	}
4138 
4139 	DBPRINT(sc, BCE_INFO_RESET, "Loading COM firmware.\n");
4140 	bce_load_cpu_fw(sc, &cpu_reg, &fw);
4141 
4142 	DBEXIT(BCE_VERBOSE_RESET);
4143 }
4144 
4145 
4146 /****************************************************************************/
4147 /* Initialize the RV2P, RX, TX, TPAT, COM, and CP CPUs.                     */
4148 /*                                                                          */
4149 /* Loads the firmware for each CPU and starts the CPU.                      */
4150 /*                                                                          */
4151 /* Returns:                                                                 */
4152 /*   Nothing.                                                               */
4153 /****************************************************************************/
4154 static void
4155 bce_init_cpus(struct bce_softc *sc)
4156 {
4157 	DBENTER(BCE_VERBOSE_RESET);
4158 
4159 	if ((BCE_CHIP_NUM(sc) == BCE_CHIP_NUM_5709) ||
4160 		(BCE_CHIP_NUM(sc) == BCE_CHIP_NUM_5716)) {
4161 
4162 		if ((BCE_CHIP_REV(sc) == BCE_CHIP_REV_Ax)) {
4163 			bce_load_rv2p_fw(sc, bce_xi90_rv2p_proc1,
4164 				sizeof(bce_xi90_rv2p_proc1), RV2P_PROC1);
4165 			bce_load_rv2p_fw(sc, bce_xi90_rv2p_proc2,
4166 				sizeof(bce_xi90_rv2p_proc2), RV2P_PROC2);
4167 		} else {
4168 			bce_load_rv2p_fw(sc, bce_xi_rv2p_proc1,
4169 				sizeof(bce_xi_rv2p_proc1), RV2P_PROC1);
4170 			bce_load_rv2p_fw(sc, bce_xi_rv2p_proc2,
4171 				sizeof(bce_xi_rv2p_proc2), RV2P_PROC2);
4172 		}
4173 
4174 	} else {
4175 		bce_load_rv2p_fw(sc, bce_rv2p_proc1,
4176 			sizeof(bce_rv2p_proc1),	RV2P_PROC1);
4177 		bce_load_rv2p_fw(sc, bce_rv2p_proc2,
4178 			sizeof(bce_rv2p_proc2),	RV2P_PROC2);
4179 	}
4180 
4181 	bce_init_rxp_cpu(sc);
4182 	bce_init_txp_cpu(sc);
4183 	bce_init_tpat_cpu(sc);
4184 	bce_init_com_cpu(sc);
4185 	bce_init_cp_cpu(sc);
4186 
4187 	DBEXIT(BCE_VERBOSE_RESET);
4188 }
4189 
4190 
4191 /****************************************************************************/
4192 /* Initialize context memory.                                               */
4193 /*                                                                          */
4194 /* Clears the memory associated with each Context ID (CID).                 */
4195 /*                                                                          */
4196 /* Returns:                                                                 */
4197 /*   Nothing.                                                               */
4198 /****************************************************************************/
4199 static void
4200 bce_init_ctx(struct bce_softc *sc)
4201 {
4202 
4203 	DBENTER(BCE_VERBOSE_RESET | BCE_VERBOSE_CTX);
4204 
4205 	if ((BCE_CHIP_NUM(sc) == BCE_CHIP_NUM_5709) ||
4206 		(BCE_CHIP_NUM(sc) == BCE_CHIP_NUM_5716)) {
4207 		int i, retry_cnt = CTX_INIT_RETRY_COUNT;
4208 		u32 val;
4209 
4210 		DBPRINT(sc, BCE_INFO_CTX, "Initializing 5709 context.\n");
4211 
4212 		/*
4213 		 * BCM5709 context memory may be cached
4214 		 * in host memory so prepare the host memory
4215 		 * for access.
4216 		 */
4217 		val = BCE_CTX_COMMAND_ENABLED | BCE_CTX_COMMAND_MEM_INIT | (1 << 12);
4218 		val |= (BCM_PAGE_BITS - 8) << 16;
4219 		REG_WR(sc, BCE_CTX_COMMAND, val);
4220 
4221 		/* Wait for mem init command to complete. */
4222 		for (i = 0; i < retry_cnt; i++) {
4223 			val = REG_RD(sc, BCE_CTX_COMMAND);
4224 			if (!(val & BCE_CTX_COMMAND_MEM_INIT))
4225 				break;
4226 			DELAY(2);
4227 		}
4228 
4229 		/* ToDo: Consider returning an error here. */
4230 		DBRUNIF((val & BCE_CTX_COMMAND_MEM_INIT),
4231 			BCE_PRINTF("%s(): Context memory initialization failed!\n",
4232 			__FUNCTION__));
4233 
4234 		for (i = 0; i < sc->ctx_pages; i++) {
4235 			int j;
4236 
4237 			/* Set the physical address of the context memory cache. */
4238 			REG_WR(sc, BCE_CTX_HOST_PAGE_TBL_DATA0,
4239 				BCE_ADDR_LO(sc->ctx_paddr[i] & 0xfffffff0) |
4240 				BCE_CTX_HOST_PAGE_TBL_DATA0_VALID);
4241 			REG_WR(sc, BCE_CTX_HOST_PAGE_TBL_DATA1,
4242 				BCE_ADDR_HI(sc->ctx_paddr[i]));
4243 			REG_WR(sc, BCE_CTX_HOST_PAGE_TBL_CTRL, i |
4244 				BCE_CTX_HOST_PAGE_TBL_CTRL_WRITE_REQ);
4245 
4246 			/* Verify that the context memory write was successful. */
4247 			for (j = 0; j < retry_cnt; j++) {
4248 				val = REG_RD(sc, BCE_CTX_HOST_PAGE_TBL_CTRL);
4249 				if ((val & BCE_CTX_HOST_PAGE_TBL_CTRL_WRITE_REQ) == 0)
4250 					break;
4251 				DELAY(5);
4252 			}
4253 
4254 			/* ToDo: Consider returning an error here. */
4255 			DBRUNIF((val & BCE_CTX_HOST_PAGE_TBL_CTRL_WRITE_REQ),
4256 				BCE_PRINTF("%s(): Failed to initialize context page %d!\n",
4257 				__FUNCTION__, i));
4258 		}
4259 	} else {
4260 		u32 vcid_addr, offset;
4261 
4262 		DBPRINT(sc, BCE_INFO, "Initializing 5706/5708 context.\n");
4263 
4264 		/*
4265 		 * For the 5706/5708, context memory is local to
4266 		 * the controller, so initialize the controller
4267 		 * context memory.
4268 		 */
4269 
4270 		vcid_addr = GET_CID_ADDR(96);
4271 		while (vcid_addr) {
4272 
4273 			vcid_addr -= PHY_CTX_SIZE;
4274 
4275 			REG_WR(sc, BCE_CTX_VIRT_ADDR, 0);
4276 			REG_WR(sc, BCE_CTX_PAGE_TBL, vcid_addr);
4277 
4278             for(offset = 0; offset < PHY_CTX_SIZE; offset += 4) {
4279                 CTX_WR(sc, 0x00, offset, 0);
4280             }
4281 
4282 			REG_WR(sc, BCE_CTX_VIRT_ADDR, vcid_addr);
4283 			REG_WR(sc, BCE_CTX_PAGE_TBL, vcid_addr);
4284 		}
4285 
4286 	}
4287 	DBEXIT(BCE_VERBOSE_RESET | BCE_VERBOSE_CTX);
4288 }
4289 
4290 
4291 /****************************************************************************/
4292 /* Fetch the permanent MAC address of the controller.                       */
4293 /*                                                                          */
4294 /* Returns:                                                                 */
4295 /*   Nothing.                                                               */
4296 /****************************************************************************/
4297 static void
4298 bce_get_mac_addr(struct bce_softc *sc)
4299 {
4300 	u32 mac_lo = 0, mac_hi = 0;
4301 
4302 	DBENTER(BCE_VERBOSE_RESET);
4303 	/*
4304 	 * The NetXtreme II bootcode populates various NIC
4305 	 * power-on and runtime configuration items in a
4306 	 * shared memory area.  The factory configured MAC
4307 	 * address is available from both NVRAM and the
4308 	 * shared memory area so we'll read the value from
4309 	 * shared memory for speed.
4310 	 */
4311 
4312 	mac_hi = REG_RD_IND(sc, sc->bce_shmem_base +
4313 		BCE_PORT_HW_CFG_MAC_UPPER);
4314 	mac_lo = REG_RD_IND(sc, sc->bce_shmem_base +
4315 		BCE_PORT_HW_CFG_MAC_LOWER);
4316 
4317 	if ((mac_lo == 0) && (mac_hi == 0)) {
4318 		BCE_PRINTF("%s(%d): Invalid Ethernet address!\n",
4319 			__FILE__, __LINE__);
4320 	} else {
4321 		sc->eaddr[0] = (u_char)(mac_hi >> 8);
4322 		sc->eaddr[1] = (u_char)(mac_hi >> 0);
4323 		sc->eaddr[2] = (u_char)(mac_lo >> 24);
4324 		sc->eaddr[3] = (u_char)(mac_lo >> 16);
4325 		sc->eaddr[4] = (u_char)(mac_lo >> 8);
4326 		sc->eaddr[5] = (u_char)(mac_lo >> 0);
4327 	}
4328 
4329 	DBPRINT(sc, BCE_INFO_MISC, "Permanent Ethernet address = %6D\n", sc->eaddr, ":");
4330 	DBEXIT(BCE_VERBOSE_RESET);
4331 }
4332 
4333 
4334 /****************************************************************************/
4335 /* Program the MAC address.                                                 */
4336 /*                                                                          */
4337 /* Returns:                                                                 */
4338 /*   Nothing.                                                               */
4339 /****************************************************************************/
4340 static void
4341 bce_set_mac_addr(struct bce_softc *sc)
4342 {
4343 	u32 val;
4344 	u8 *mac_addr = sc->eaddr;
4345 
4346 	/* ToDo: Add support for setting multiple MAC addresses. */
4347 
4348 	DBENTER(BCE_VERBOSE_RESET);
4349 	DBPRINT(sc, BCE_INFO_MISC, "Setting Ethernet address = %6D\n", sc->eaddr, ":");
4350 
4351 	val = (mac_addr[0] << 8) | mac_addr[1];
4352 
4353 	REG_WR(sc, BCE_EMAC_MAC_MATCH0, val);
4354 
4355 	val = (mac_addr[2] << 24) | (mac_addr[3] << 16) |
4356 		(mac_addr[4] << 8) | mac_addr[5];
4357 
4358 	REG_WR(sc, BCE_EMAC_MAC_MATCH1, val);
4359 
4360 	DBEXIT(BCE_VERBOSE_RESET);
4361 }
4362 
4363 
4364 /****************************************************************************/
4365 /* Stop the controller.                                                     */
4366 /*                                                                          */
4367 /* Returns:                                                                 */
4368 /*   Nothing.                                                               */
4369 /****************************************************************************/
4370 static void
4371 bce_stop(struct bce_softc *sc)
4372 {
4373 	struct ifnet *ifp;
4374 	struct ifmedia_entry *ifm;
4375 	struct mii_data *mii = NULL;
4376 	int mtmp, itmp;
4377 
4378 	DBENTER(BCE_VERBOSE_RESET);
4379 
4380 	BCE_LOCK_ASSERT(sc);
4381 
4382 	ifp = sc->bce_ifp;
4383 
4384 	mii = device_get_softc(sc->bce_miibus);
4385 
4386 	callout_stop(&sc->bce_tick_callout);
4387 
4388 	/* Disable the transmit/receive blocks. */
4389 	REG_WR(sc, BCE_MISC_ENABLE_CLR_BITS, BCE_MISC_ENABLE_CLR_DEFAULT);
4390 	REG_RD(sc, BCE_MISC_ENABLE_CLR_BITS);
4391 	DELAY(20);
4392 
4393 	bce_disable_intr(sc);
4394 
4395 	/* Free RX buffers. */
4396 #ifdef ZERO_COPY_SOCKETS
4397 	bce_free_pg_chain(sc);
4398 #endif
4399 	bce_free_rx_chain(sc);
4400 
4401 	/* Free TX buffers. */
4402 	bce_free_tx_chain(sc);
4403 
4404 	/*
4405 	 * Isolate/power down the PHY, but leave the media selection
4406 	 * unchanged so that things will be put back to normal when
4407 	 * we bring the interface back up.
4408 	 */
4409 
4410 	itmp = ifp->if_flags;
4411 	ifp->if_flags |= IFF_UP;
4412 
4413 	/* If we are called from bce_detach(), mii is already NULL. */
4414 	if (mii != NULL) {
4415 		ifm = mii->mii_media.ifm_cur;
4416 		mtmp = ifm->ifm_media;
4417 		ifm->ifm_media = IFM_ETHER | IFM_NONE;
4418 		mii_mediachg(mii);
4419 		ifm->ifm_media = mtmp;
4420 	}
4421 
4422 	ifp->if_flags = itmp;
4423 	sc->watchdog_timer = 0;
4424 
4425 	sc->bce_link = 0;
4426 
4427 	ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
4428 
4429 	DBEXIT(BCE_VERBOSE_RESET);
4430 }
4431 
4432 
4433 static int
4434 bce_reset(struct bce_softc *sc, u32 reset_code)
4435 {
4436 	u32 val;
4437 	int i, rc = 0;
4438 
4439 	DBENTER(BCE_VERBOSE_RESET);
4440 
4441 	DBPRINT(sc, BCE_VERBOSE_RESET, "%s(): reset_code = 0x%08X\n",
4442 		__FUNCTION__, reset_code);
4443 
4444 	/* Wait for pending PCI transactions to complete. */
4445 	REG_WR(sc, BCE_MISC_ENABLE_CLR_BITS,
4446 	       BCE_MISC_ENABLE_CLR_BITS_TX_DMA_ENABLE |
4447 	       BCE_MISC_ENABLE_CLR_BITS_DMA_ENGINE_ENABLE |
4448 	       BCE_MISC_ENABLE_CLR_BITS_RX_DMA_ENABLE |
4449 	       BCE_MISC_ENABLE_CLR_BITS_HOST_COALESCE_ENABLE);
4450 	val = REG_RD(sc, BCE_MISC_ENABLE_CLR_BITS);
4451 	DELAY(5);
4452 
4453 	/* Disable DMA */
4454 	if ((BCE_CHIP_NUM(sc) == BCE_CHIP_NUM_5709) ||
4455 		(BCE_CHIP_NUM(sc) == BCE_CHIP_NUM_5716)) {
4456 		val = REG_RD(sc, BCE_MISC_NEW_CORE_CTL);
4457 		val &= ~BCE_MISC_NEW_CORE_CTL_DMA_ENABLE;
4458 		REG_WR(sc, BCE_MISC_NEW_CORE_CTL, val);
4459 	}
4460 
4461 	/* Assume bootcode is running. */
4462 	sc->bce_fw_timed_out = 0;
4463 
4464 	/* Give the firmware a chance to prepare for the reset. */
4465 	rc = bce_fw_sync(sc, BCE_DRV_MSG_DATA_WAIT0 | reset_code);
4466 	if (rc)
4467 		goto bce_reset_exit;
4468 
4469 	/* Set a firmware reminder that this is a soft reset. */
4470 	REG_WR_IND(sc, sc->bce_shmem_base + BCE_DRV_RESET_SIGNATURE,
4471 		   BCE_DRV_RESET_SIGNATURE_MAGIC);
4472 
4473 	/* Dummy read to force the chip to complete all current transactions. */
4474 	val = REG_RD(sc, BCE_MISC_ID);
4475 
4476 	/* Chip reset. */
4477 	if ((BCE_CHIP_NUM(sc) == BCE_CHIP_NUM_5709) ||
4478 		(BCE_CHIP_NUM(sc) == BCE_CHIP_NUM_5716)) {
4479 		REG_WR(sc, BCE_MISC_COMMAND, BCE_MISC_COMMAND_SW_RESET);
4480 		REG_RD(sc, BCE_MISC_COMMAND);
4481 		DELAY(5);
4482 
4483 		val = BCE_PCICFG_MISC_CONFIG_REG_WINDOW_ENA |
4484 		      BCE_PCICFG_MISC_CONFIG_TARGET_MB_WORD_SWAP;
4485 
4486 		pci_write_config(sc->bce_dev, BCE_PCICFG_MISC_CONFIG, val, 4);
4487 	} else {
4488 		val = BCE_PCICFG_MISC_CONFIG_CORE_RST_REQ |
4489 			BCE_PCICFG_MISC_CONFIG_REG_WINDOW_ENA |
4490 			BCE_PCICFG_MISC_CONFIG_TARGET_MB_WORD_SWAP;
4491 		REG_WR(sc, BCE_PCICFG_MISC_CONFIG, val);
4492 
4493 		/* Allow up to 30us for reset to complete. */
4494 		for (i = 0; i < 10; i++) {
4495 			val = REG_RD(sc, BCE_PCICFG_MISC_CONFIG);
4496 			if ((val & (BCE_PCICFG_MISC_CONFIG_CORE_RST_REQ |
4497 				BCE_PCICFG_MISC_CONFIG_CORE_RST_BSY)) == 0) {
4498 				break;
4499 			}
4500 			DELAY(10);
4501 		}
4502 
4503 		/* Check that reset completed successfully. */
4504 		if (val & (BCE_PCICFG_MISC_CONFIG_CORE_RST_REQ |
4505 			BCE_PCICFG_MISC_CONFIG_CORE_RST_BSY)) {
4506 			BCE_PRINTF("%s(%d): Reset failed!\n",
4507 				__FILE__, __LINE__);
4508 			rc = EBUSY;
4509 			goto bce_reset_exit;
4510 		}
4511 	}
4512 
4513 	/* Make sure byte swapping is properly configured. */
4514 	val = REG_RD(sc, BCE_PCI_SWAP_DIAG0);
4515 	if (val != 0x01020304) {
4516 		BCE_PRINTF("%s(%d): Byte swap is incorrect!\n",
4517 			__FILE__, __LINE__);
4518 		rc = ENODEV;
4519 		goto bce_reset_exit;
4520 	}
4521 
4522 	/* Just completed a reset, assume that firmware is running again. */
4523 	sc->bce_fw_timed_out = 0;
4524 
4525 	/* Wait for the firmware to finish its initialization. */
4526 	rc = bce_fw_sync(sc, BCE_DRV_MSG_DATA_WAIT1 | reset_code);
4527 	if (rc)
4528 		BCE_PRINTF("%s(%d): Firmware did not complete initialization!\n",
4529 			__FILE__, __LINE__);
4530 
4531 bce_reset_exit:
4532 	DBEXIT(BCE_VERBOSE_RESET);
4533 	return (rc);
4534 }
4535 
4536 
4537 static int
4538 bce_chipinit(struct bce_softc *sc)
4539 {
4540 	u32 val;
4541 	int rc = 0;
4542 
4543 	DBENTER(BCE_VERBOSE_RESET);
4544 
4545 	bce_disable_intr(sc);
4546 
4547 	/*
4548 	 * Initialize DMA byte/word swapping, configure the number of DMA
4549 	 * channels and PCI clock compensation delay.
4550 	 */
4551 	val = BCE_DMA_CONFIG_DATA_BYTE_SWAP |
4552 	      BCE_DMA_CONFIG_DATA_WORD_SWAP |
4553 #if BYTE_ORDER == BIG_ENDIAN
4554 	      BCE_DMA_CONFIG_CNTL_BYTE_SWAP |
4555 #endif
4556 	      BCE_DMA_CONFIG_CNTL_WORD_SWAP |
4557 	      DMA_READ_CHANS << 12 |
4558 	      DMA_WRITE_CHANS << 16;
4559 
4560 	val |= (0x2 << 20) | BCE_DMA_CONFIG_CNTL_PCI_COMP_DLY;
4561 
4562 	if ((sc->bce_flags & BCE_PCIX_FLAG) && (sc->bus_speed_mhz == 133))
4563 		val |= BCE_DMA_CONFIG_PCI_FAST_CLK_CMP;
4564 
4565 	/*
4566 	 * This setting resolves a problem observed on certain Intel PCI
4567 	 * chipsets that cannot handle multiple outstanding DMA operations.
4568 	 * See errata E9_5706A1_65.
4569 	 */
4570 	if ((BCE_CHIP_NUM(sc) == BCE_CHIP_NUM_5706) &&
4571 	    (BCE_CHIP_ID(sc) != BCE_CHIP_ID_5706_A0) &&
4572 	    !(sc->bce_flags & BCE_PCIX_FLAG))
4573 		val |= BCE_DMA_CONFIG_CNTL_PING_PONG_DMA;
4574 
4575 	REG_WR(sc, BCE_DMA_CONFIG, val);
4576 
4577 	/* Enable the RX_V2P and Context state machines before access. */
4578 	REG_WR(sc, BCE_MISC_ENABLE_SET_BITS,
4579 	       BCE_MISC_ENABLE_SET_BITS_HOST_COALESCE_ENABLE |
4580 	       BCE_MISC_ENABLE_STATUS_BITS_RX_V2P_ENABLE |
4581 	       BCE_MISC_ENABLE_STATUS_BITS_CONTEXT_ENABLE);
4582 
4583 	/* Initialize context mapping and zero out the quick contexts. */
4584 	bce_init_ctx(sc);
4585 
4586 	/* Initialize the on-boards CPUs */
4587 	bce_init_cpus(sc);
4588 
4589 	/* Prepare NVRAM for access. */
4590 	if (bce_init_nvram(sc)) {
4591 		rc = ENODEV;
4592 		goto bce_chipinit_exit;
4593 	}
4594 
4595 	/* Set the kernel bypass block size */
4596 	val = REG_RD(sc, BCE_MQ_CONFIG);
4597 	val &= ~BCE_MQ_CONFIG_KNL_BYP_BLK_SIZE;
4598 	val |= BCE_MQ_CONFIG_KNL_BYP_BLK_SIZE_256;
4599 
4600 	/* Enable bins used on the 5709. */
4601 	if ((BCE_CHIP_NUM(sc) == BCE_CHIP_NUM_5709) ||
4602 		(BCE_CHIP_NUM(sc) == BCE_CHIP_NUM_5716)) {
4603 		val |= BCE_MQ_CONFIG_BIN_MQ_MODE;
4604 		if (BCE_CHIP_ID(sc) == BCE_CHIP_ID_5709_A1)
4605 			val |= BCE_MQ_CONFIG_HALT_DIS;
4606 	}
4607 
4608 	REG_WR(sc, BCE_MQ_CONFIG, val);
4609 
4610 	val = 0x10000 + (MAX_CID_CNT * MB_KERNEL_CTX_SIZE);
4611 	REG_WR(sc, BCE_MQ_KNL_BYP_WIND_START, val);
4612 	REG_WR(sc, BCE_MQ_KNL_WIND_END, val);
4613 
4614 	/* Set the page size and clear the RV2P processor stall bits. */
4615 	val = (BCM_PAGE_BITS - 8) << 24;
4616 	REG_WR(sc, BCE_RV2P_CONFIG, val);
4617 
4618 	/* Configure page size. */
4619 	val = REG_RD(sc, BCE_TBDR_CONFIG);
4620 	val &= ~BCE_TBDR_CONFIG_PAGE_SIZE;
4621 	val |= (BCM_PAGE_BITS - 8) << 24 | 0x40;
4622 	REG_WR(sc, BCE_TBDR_CONFIG, val);
4623 
4624 	/* Set the perfect match control register to default. */
4625 	REG_WR_IND(sc, BCE_RXP_PM_CTRL, 0);
4626 
4627 bce_chipinit_exit:
4628 	DBEXIT(BCE_VERBOSE_RESET);
4629 
4630 	return(rc);
4631 }
4632 
4633 
4634 /****************************************************************************/
4635 /* Initialize the controller in preparation to send/receive traffic.        */
4636 /*                                                                          */
4637 /* Returns:                                                                 */
4638 /*   0 for success, positive value for failure.                             */
4639 /****************************************************************************/
4640 static int
4641 bce_blockinit(struct bce_softc *sc)
4642 {
4643 	u32 reg, val;
4644 	int rc = 0;
4645 
4646 	DBENTER(BCE_VERBOSE_RESET);
4647 
4648 	/* Load the hardware default MAC address. */
4649 	bce_set_mac_addr(sc);
4650 
4651 	/* Set the Ethernet backoff seed value */
4652 	val = sc->eaddr[0]         + (sc->eaddr[1] << 8) +
4653 	      (sc->eaddr[2] << 16) + (sc->eaddr[3]     ) +
4654 	      (sc->eaddr[4] << 8)  + (sc->eaddr[5] << 16);
4655 	REG_WR(sc, BCE_EMAC_BACKOFF_SEED, val);
4656 
4657 	sc->last_status_idx = 0;
4658 	sc->rx_mode = BCE_EMAC_RX_MODE_SORT_MODE;
4659 
4660 	/* Set up link change interrupt generation. */
4661 	REG_WR(sc, BCE_EMAC_ATTENTION_ENA, BCE_EMAC_ATTENTION_ENA_LINK);
4662 
4663 	/* Program the physical address of the status block. */
4664 	REG_WR(sc, BCE_HC_STATUS_ADDR_L,
4665 		BCE_ADDR_LO(sc->status_block_paddr));
4666 	REG_WR(sc, BCE_HC_STATUS_ADDR_H,
4667 		BCE_ADDR_HI(sc->status_block_paddr));
4668 
4669 	/* Program the physical address of the statistics block. */
4670 	REG_WR(sc, BCE_HC_STATISTICS_ADDR_L,
4671 		BCE_ADDR_LO(sc->stats_block_paddr));
4672 	REG_WR(sc, BCE_HC_STATISTICS_ADDR_H,
4673 		BCE_ADDR_HI(sc->stats_block_paddr));
4674 
4675 	/* Program various host coalescing parameters. */
4676 	REG_WR(sc, BCE_HC_TX_QUICK_CONS_TRIP,
4677 		(sc->bce_tx_quick_cons_trip_int << 16) | sc->bce_tx_quick_cons_trip);
4678 	REG_WR(sc, BCE_HC_RX_QUICK_CONS_TRIP,
4679 		(sc->bce_rx_quick_cons_trip_int << 16) | sc->bce_rx_quick_cons_trip);
4680 	REG_WR(sc, BCE_HC_COMP_PROD_TRIP,
4681 		(sc->bce_comp_prod_trip_int << 16) | sc->bce_comp_prod_trip);
4682 	REG_WR(sc, BCE_HC_TX_TICKS,
4683 		(sc->bce_tx_ticks_int << 16) | sc->bce_tx_ticks);
4684 	REG_WR(sc, BCE_HC_RX_TICKS,
4685 		(sc->bce_rx_ticks_int << 16) | sc->bce_rx_ticks);
4686 	REG_WR(sc, BCE_HC_COM_TICKS,
4687 		(sc->bce_com_ticks_int << 16) | sc->bce_com_ticks);
4688 	REG_WR(sc, BCE_HC_CMD_TICKS,
4689 		(sc->bce_cmd_ticks_int << 16) | sc->bce_cmd_ticks);
4690 	REG_WR(sc, BCE_HC_STATS_TICKS,
4691 		(sc->bce_stats_ticks & 0xffff00));
4692 	REG_WR(sc, BCE_HC_STAT_COLLECT_TICKS, 0xbb8);  /* 3ms */
4693 
4694 	/* Configure the Host Coalescing block. */
4695 	val = BCE_HC_CONFIG_RX_TMR_MODE | BCE_HC_CONFIG_TX_TMR_MODE |
4696 		      BCE_HC_CONFIG_COLLECT_STATS;
4697 
4698 #if 0
4699 	/* ToDo: Add MSI-X support. */
4700 	if (sc->bce_flags & BCE_USING_MSIX_FLAG) {
4701 		u32 base = ((BCE_TX_VEC - 1) * BCE_HC_SB_CONFIG_SIZE) +
4702 			   BCE_HC_SB_CONFIG_1;
4703 
4704 		REG_WR(sc, BCE_HC_MSIX_BIT_VECTOR, BCE_HC_MSIX_BIT_VECTOR_VAL);
4705 
4706 		REG_WR(sc, base, BCE_HC_SB_CONFIG_1_TX_TMR_MODE |
4707 			BCE_HC_SB_CONFIG_1_ONE_SHOT);
4708 
4709 		REG_WR(sc, base + BCE_HC_TX_QUICK_CONS_TRIP_OFF,
4710 			(sc->tx_quick_cons_trip_int << 16) |
4711 			 sc->tx_quick_cons_trip);
4712 
4713 		REG_WR(sc, base + BCE_HC_TX_TICKS_OFF,
4714 			(sc->tx_ticks_int << 16) | sc->tx_ticks);
4715 
4716 		val |= BCE_HC_CONFIG_SB_ADDR_INC_128B;
4717 	}
4718 
4719 	/*
4720 	 * Tell the HC block to automatically set the
4721 	 * INT_MASK bit after an MSI/MSI-X interrupt
4722 	 * is generated so the driver doesn't have to.
4723 	 */
4724 	if (sc->bce_flags & BCE_ONE_SHOT_MSI_FLAG)
4725 		val |= BCE_HC_CONFIG_ONE_SHOT;
4726 
4727 	/* Set the MSI-X status blocks to 128 byte boundaries. */
4728 	if (sc->bce_flags & BCE_USING_MSIX_FLAG)
4729 		val |= BCE_HC_CONFIG_SB_ADDR_INC_128B;
4730 #endif
4731 
4732 	REG_WR(sc, BCE_HC_CONFIG, val);
4733 
4734 	/* Clear the internal statistics counters. */
4735 	REG_WR(sc, BCE_HC_COMMAND, BCE_HC_COMMAND_CLR_STAT_NOW);
4736 
4737 	/* Verify that bootcode is running. */
4738 	reg = REG_RD_IND(sc, sc->bce_shmem_base + BCE_DEV_INFO_SIGNATURE);
4739 
4740 	DBRUNIF(DB_RANDOMTRUE(bootcode_running_failure_sim_control),
4741 		BCE_PRINTF("%s(%d): Simulating bootcode failure.\n",
4742 			__FILE__, __LINE__);
4743 		reg = 0);
4744 
4745 	if ((reg & BCE_DEV_INFO_SIGNATURE_MAGIC_MASK) !=
4746 	    BCE_DEV_INFO_SIGNATURE_MAGIC) {
4747 		BCE_PRINTF("%s(%d): Bootcode not running! Found: 0x%08X, "
4748 			"Expected: 08%08X\n", __FILE__, __LINE__,
4749 			(reg & BCE_DEV_INFO_SIGNATURE_MAGIC_MASK),
4750 			BCE_DEV_INFO_SIGNATURE_MAGIC);
4751 		rc = ENODEV;
4752 		goto bce_blockinit_exit;
4753 	}
4754 
4755 	/* Enable DMA */
4756 	if ((BCE_CHIP_NUM(sc) == BCE_CHIP_NUM_5709) ||
4757 		(BCE_CHIP_NUM(sc) == BCE_CHIP_NUM_5716)) {
4758 		val = REG_RD(sc, BCE_MISC_NEW_CORE_CTL);
4759 		val |= BCE_MISC_NEW_CORE_CTL_DMA_ENABLE;
4760 		REG_WR(sc, BCE_MISC_NEW_CORE_CTL, val);
4761 	}
4762 
4763 	/* Allow bootcode to apply any additional fixes before enabling MAC. */
4764 	rc = bce_fw_sync(sc, BCE_DRV_MSG_DATA_WAIT2 | BCE_DRV_MSG_CODE_RESET);
4765 
4766 	/* Enable link state change interrupt generation. */
4767 	REG_WR(sc, BCE_HC_ATTN_BITS_ENABLE, STATUS_ATTN_BITS_LINK_STATE);
4768 
4769 	/* Enable all remaining blocks in the MAC. */
4770 	if ((BCE_CHIP_NUM(sc) == BCE_CHIP_NUM_5709)	||
4771 		(BCE_CHIP_NUM(sc) == BCE_CHIP_NUM_5716))
4772 		REG_WR(sc, BCE_MISC_ENABLE_SET_BITS, BCE_MISC_ENABLE_DEFAULT_XI);
4773 	else
4774 		REG_WR(sc, BCE_MISC_ENABLE_SET_BITS, BCE_MISC_ENABLE_DEFAULT);
4775 
4776 	REG_RD(sc, BCE_MISC_ENABLE_SET_BITS);
4777 	DELAY(20);
4778 
4779 	/* Save the current host coalescing block settings. */
4780 	sc->hc_command = REG_RD(sc, BCE_HC_COMMAND);
4781 
4782 bce_blockinit_exit:
4783 	DBEXIT(BCE_VERBOSE_RESET);
4784 
4785 	return (rc);
4786 }
4787 
4788 
4789 /****************************************************************************/
4790 /* Encapsulate an mbuf into the rx_bd chain.                                */
4791 /*                                                                          */
4792 /* Returns:                                                                 */
4793 /*   0 for success, positive value for failure.                             */
4794 /****************************************************************************/
4795 static int
4796 bce_get_rx_buf(struct bce_softc *sc, struct mbuf *m, u16 *prod,
4797 	u16 *chain_prod, u32 *prod_bseq)
4798 {
4799 	bus_dmamap_t map;
4800 	bus_dma_segment_t segs[BCE_MAX_SEGMENTS];
4801 	struct mbuf *m_new = NULL;
4802 	struct rx_bd *rxbd;
4803 	int nsegs, error, rc = 0;
4804 #ifdef BCE_DEBUG
4805 	u16 debug_chain_prod = *chain_prod;
4806 #endif
4807 
4808 	DBENTER(BCE_EXTREME_RESET | BCE_EXTREME_RECV | BCE_EXTREME_LOAD);
4809 
4810 	/* Make sure the inputs are valid. */
4811 	DBRUNIF((*chain_prod > MAX_RX_BD),
4812 		BCE_PRINTF("%s(%d): RX producer out of range: 0x%04X > 0x%04X\n",
4813 		__FILE__, __LINE__, *chain_prod, (u16) MAX_RX_BD));
4814 
4815 	DBPRINT(sc, BCE_EXTREME_RECV, "%s(enter): prod = 0x%04X, chain_prod = 0x%04X, "
4816 		"prod_bseq = 0x%08X\n", __FUNCTION__, *prod, *chain_prod, *prod_bseq);
4817 
4818 	/* Update some debug statistic counters */
4819 	DBRUNIF((sc->free_rx_bd < sc->rx_low_watermark),
4820 		sc->rx_low_watermark = sc->free_rx_bd);
4821 	DBRUNIF((sc->free_rx_bd == sc->max_rx_bd), sc->rx_empty_count++);
4822 
4823 	/* Check whether this is a new mbuf allocation. */
4824 	if (m == NULL) {
4825 
4826 		/* Simulate an mbuf allocation failure. */
4827 		DBRUNIF(DB_RANDOMTRUE(mbuf_alloc_failed_sim_control),
4828 			sc->mbuf_alloc_failed_count++;
4829 			sc->mbuf_alloc_failed_sim_count++;
4830 			rc = ENOBUFS;
4831 			goto bce_get_rx_buf_exit);
4832 
4833 		/* This is a new mbuf allocation. */
4834 #ifdef ZERO_COPY_SOCKETS
4835 		MGETHDR(m_new, M_DONTWAIT, MT_DATA);
4836 #else
4837 		if (sc->rx_bd_mbuf_alloc_size <= MCLBYTES)
4838 			m_new = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR);
4839 		else
4840 			m_new = m_getjcl(M_DONTWAIT, MT_DATA, M_PKTHDR, sc->rx_bd_mbuf_alloc_size);
4841 #endif
4842 
4843 		if (m_new == NULL) {
4844 			sc->mbuf_alloc_failed_count++;
4845 			rc = ENOBUFS;
4846 			goto bce_get_rx_buf_exit;
4847 		}
4848 
4849 		DBRUN(sc->debug_rx_mbuf_alloc++);
4850 	} else {
4851 		/* Reuse an existing mbuf. */
4852 		m_new = m;
4853 	}
4854 
4855 	/* Make sure we have a valid packet header. */
4856 	M_ASSERTPKTHDR(m_new);
4857 
4858 	/* Initialize the mbuf size and pad if necessary for alignment. */
4859 	m_new->m_pkthdr.len = m_new->m_len = sc->rx_bd_mbuf_alloc_size;
4860 	m_adj(m_new, sc->rx_bd_mbuf_align_pad);
4861 
4862 	/* ToDo: Consider calling m_fragment() to test error handling. */
4863 
4864 	/* Map the mbuf cluster into device memory. */
4865 	map = sc->rx_mbuf_map[*chain_prod];
4866 	error = bus_dmamap_load_mbuf_sg(sc->rx_mbuf_tag, map, m_new,
4867 	    segs, &nsegs, BUS_DMA_NOWAIT);
4868 
4869 	/* Handle any mapping errors. */
4870 	if (error) {
4871 		BCE_PRINTF("%s(%d): Error mapping mbuf into RX chain (%d)!\n",
4872 			__FILE__, __LINE__, error);
4873 
4874 		sc->dma_map_addr_rx_failed_count++;
4875 		m_freem(m_new);
4876 
4877 		DBRUN(sc->debug_rx_mbuf_alloc--);
4878 
4879 		rc = ENOBUFS;
4880 		goto bce_get_rx_buf_exit;
4881 	}
4882 
4883 	/* All mbufs must map to a single segment. */
4884 	KASSERT(nsegs == 1, ("%s(): Too many segments returned (%d)!",
4885 		 __FUNCTION__, nsegs));
4886 
4887 	/* ToDo: Do we need bus_dmamap_sync(,,BUS_DMASYNC_PREREAD) here? */
4888 
4889 	/* Setup the rx_bd for the segment. */
4890 	rxbd = &sc->rx_bd_chain[RX_PAGE(*chain_prod)][RX_IDX(*chain_prod)];
4891 
4892 	rxbd->rx_bd_haddr_lo  = htole32(BCE_ADDR_LO(segs[0].ds_addr));
4893 	rxbd->rx_bd_haddr_hi  = htole32(BCE_ADDR_HI(segs[0].ds_addr));
4894 	rxbd->rx_bd_len       = htole32(segs[0].ds_len);
4895 	rxbd->rx_bd_flags     = htole32(RX_BD_FLAGS_START | RX_BD_FLAGS_END);
4896 	*prod_bseq += segs[0].ds_len;
4897 
4898 	/* Save the mbuf and update our counter. */
4899 	sc->rx_mbuf_ptr[*chain_prod] = m_new;
4900 	sc->free_rx_bd -= nsegs;
4901 
4902 	DBRUNMSG(BCE_INSANE_RECV, bce_dump_rx_mbuf_chain(sc, debug_chain_prod,
4903 		nsegs));
4904 
4905 	DBPRINT(sc, BCE_EXTREME_RECV, "%s(exit): prod = 0x%04X, chain_prod = 0x%04X, "
4906 		"prod_bseq = 0x%08X\n", __FUNCTION__, *prod, *chain_prod, *prod_bseq);
4907 
4908 bce_get_rx_buf_exit:
4909 	DBEXIT(BCE_EXTREME_RESET | BCE_EXTREME_RECV | BCE_EXTREME_LOAD);
4910 
4911 	return(rc);
4912 }
4913 
4914 
4915 #ifdef ZERO_COPY_SOCKETS
4916 /****************************************************************************/
4917 /* Encapsulate an mbuf cluster into the page chain.                        */
4918 /*                                                                          */
4919 /* Returns:                                                                 */
4920 /*   0 for success, positive value for failure.                             */
4921 /****************************************************************************/
4922 static int
4923 bce_get_pg_buf(struct bce_softc *sc, struct mbuf *m, u16 *prod,
4924 	u16 *prod_idx)
4925 {
4926 	bus_dmamap_t map;
4927 	bus_addr_t busaddr;
4928 	struct mbuf *m_new = NULL;
4929 	struct rx_bd *pgbd;
4930 	int error, rc = 0;
4931 #ifdef BCE_DEBUG
4932 	u16 debug_prod_idx = *prod_idx;
4933 #endif
4934 
4935 	DBENTER(BCE_EXTREME_RESET | BCE_EXTREME_RECV | BCE_EXTREME_LOAD);
4936 
4937 	/* Make sure the inputs are valid. */
4938 	DBRUNIF((*prod_idx > MAX_PG_BD),
4939 		BCE_PRINTF("%s(%d): page producer out of range: 0x%04X > 0x%04X\n",
4940 		__FILE__, __LINE__, *prod_idx, (u16) MAX_PG_BD));
4941 
4942 	DBPRINT(sc, BCE_EXTREME_RECV, "%s(enter): prod = 0x%04X, "
4943 		"chain_prod = 0x%04X\n", __FUNCTION__, *prod, *prod_idx);
4944 
4945 	/* Update counters if we've hit a new low or run out of pages. */
4946 	DBRUNIF((sc->free_pg_bd < sc->pg_low_watermark),
4947 		sc->pg_low_watermark = sc->free_pg_bd);
4948 	DBRUNIF((sc->free_pg_bd == sc->max_pg_bd), sc->pg_empty_count++);
4949 
4950 	/* Check whether this is a new mbuf allocation. */
4951 	if (m == NULL) {
4952 
4953 		/* Simulate an mbuf allocation failure. */
4954 		DBRUNIF(DB_RANDOMTRUE(mbuf_alloc_failed_sim_control),
4955 			sc->mbuf_alloc_failed_count++;
4956 			sc->mbuf_alloc_failed_sim_count++;
4957 			rc = ENOBUFS;
4958 			goto bce_get_pg_buf_exit);
4959 
4960 		/* This is a new mbuf allocation. */
4961 		m_new = m_getcl(M_DONTWAIT, MT_DATA, 0);
4962 		if (m_new == NULL) {
4963 			sc->mbuf_alloc_failed_count++;
4964 			rc = ENOBUFS;
4965 			goto bce_get_pg_buf_exit;
4966 		}
4967 
4968 		DBRUN(sc->debug_pg_mbuf_alloc++);
4969 	} else {
4970 		/* Reuse an existing mbuf. */
4971 		m_new = m;
4972 		m_new->m_data = m_new->m_ext.ext_buf;
4973 	}
4974 
4975 	m_new->m_len = sc->pg_bd_mbuf_alloc_size;
4976 
4977 	/* ToDo: Consider calling m_fragment() to test error handling. */
4978 
4979 	/* Map the mbuf cluster into device memory. */
4980 	map = sc->pg_mbuf_map[*prod_idx];
4981 	error = bus_dmamap_load(sc->pg_mbuf_tag, map, mtod(m_new, void *),
4982 	    sc->pg_bd_mbuf_alloc_size, bce_dma_map_addr, &busaddr, BUS_DMA_NOWAIT);
4983 
4984 	/* Handle any mapping errors. */
4985 	if (error) {
4986 		BCE_PRINTF("%s(%d): Error mapping mbuf into page chain!\n",
4987 			__FILE__, __LINE__);
4988 
4989 		m_freem(m_new);
4990 		DBRUN(sc->debug_pg_mbuf_alloc--);
4991 
4992 		rc = ENOBUFS;
4993 		goto bce_get_pg_buf_exit;
4994 	}
4995 
4996 	/* ToDo: Do we need bus_dmamap_sync(,,BUS_DMASYNC_PREREAD) here? */
4997 
4998 	/*
4999 	 * The page chain uses the same rx_bd data structure
5000 	 * as the receive chain but doesn't require a byte sequence (bseq).
5001 	 */
5002 	pgbd = &sc->pg_bd_chain[PG_PAGE(*prod_idx)][PG_IDX(*prod_idx)];
5003 
5004 	pgbd->rx_bd_haddr_lo  = htole32(BCE_ADDR_LO(busaddr));
5005 	pgbd->rx_bd_haddr_hi  = htole32(BCE_ADDR_HI(busaddr));
5006 	pgbd->rx_bd_len       = htole32(sc->pg_bd_mbuf_alloc_size);
5007 	pgbd->rx_bd_flags     = htole32(RX_BD_FLAGS_START | RX_BD_FLAGS_END);
5008 
5009 	/* Save the mbuf and update our counter. */
5010 	sc->pg_mbuf_ptr[*prod_idx] = m_new;
5011 	sc->free_pg_bd--;
5012 
5013 	DBRUNMSG(BCE_INSANE_RECV, bce_dump_pg_mbuf_chain(sc, debug_prod_idx,
5014 		1));
5015 
5016 	DBPRINT(sc, BCE_EXTREME_RECV, "%s(exit): prod = 0x%04X, "
5017 		"prod_idx = 0x%04X\n", __FUNCTION__, *prod, *prod_idx);
5018 
5019 bce_get_pg_buf_exit:
5020 	DBEXIT(BCE_EXTREME_RESET | BCE_EXTREME_RECV | BCE_EXTREME_LOAD);
5021 
5022 	return(rc);
5023 }
5024 #endif /* ZERO_COPY_SOCKETS */
5025 
5026 /****************************************************************************/
5027 /* Initialize the TX context memory.                                        */
5028 /*                                                                          */
5029 /* Returns:                                                                 */
5030 /*   Nothing                                                                */
5031 /****************************************************************************/
5032 static void
5033 bce_init_tx_context(struct bce_softc *sc)
5034 {
5035 	u32 val;
5036 
5037 	DBENTER(BCE_VERBOSE_RESET | BCE_VERBOSE_SEND | BCE_VERBOSE_CTX);
5038 
5039 	/* Initialize the context ID for an L2 TX chain. */
5040 	if ((BCE_CHIP_NUM(sc) == BCE_CHIP_NUM_5709) ||
5041 		(BCE_CHIP_NUM(sc) == BCE_CHIP_NUM_5716)) {
5042 		/* Set the CID type to support an L2 connection. */
5043 		val = BCE_L2CTX_TX_TYPE_TYPE_L2_XI | BCE_L2CTX_TX_TYPE_SIZE_L2_XI;
5044 		CTX_WR(sc, GET_CID_ADDR(TX_CID), BCE_L2CTX_TX_TYPE_XI, val);
5045 		val = BCE_L2CTX_TX_CMD_TYPE_TYPE_L2_XI | (8 << 16);
5046 		CTX_WR(sc, GET_CID_ADDR(TX_CID), BCE_L2CTX_TX_CMD_TYPE_XI, val);
5047 
5048 		/* Point the hardware to the first page in the chain. */
5049 		val = BCE_ADDR_HI(sc->tx_bd_chain_paddr[0]);
5050 		CTX_WR(sc, GET_CID_ADDR(TX_CID), BCE_L2CTX_TX_TBDR_BHADDR_HI_XI, val);
5051 		val = BCE_ADDR_LO(sc->tx_bd_chain_paddr[0]);
5052 		CTX_WR(sc, GET_CID_ADDR(TX_CID), BCE_L2CTX_TX_TBDR_BHADDR_LO_XI, val);
5053 	} else {
5054 		/* Set the CID type to support an L2 connection. */
5055 		val = BCE_L2CTX_TX_TYPE_TYPE_L2 | BCE_L2CTX_TX_TYPE_SIZE_L2;
5056 		CTX_WR(sc, GET_CID_ADDR(TX_CID), BCE_L2CTX_TX_TYPE, val);
5057 		val = BCE_L2CTX_TX_CMD_TYPE_TYPE_L2 | (8 << 16);
5058 		CTX_WR(sc, GET_CID_ADDR(TX_CID), BCE_L2CTX_TX_CMD_TYPE, val);
5059 
5060 		/* Point the hardware to the first page in the chain. */
5061 		val = BCE_ADDR_HI(sc->tx_bd_chain_paddr[0]);
5062 		CTX_WR(sc, GET_CID_ADDR(TX_CID), BCE_L2CTX_TX_TBDR_BHADDR_HI, val);
5063 		val = BCE_ADDR_LO(sc->tx_bd_chain_paddr[0]);
5064 		CTX_WR(sc, GET_CID_ADDR(TX_CID), BCE_L2CTX_TX_TBDR_BHADDR_LO, val);
5065 	}
5066 
5067 	DBEXIT(BCE_VERBOSE_RESET | BCE_VERBOSE_SEND | BCE_VERBOSE_CTX);
5068 }
5069 
5070 
5071 /****************************************************************************/
5072 /* Allocate memory and initialize the TX data structures.                   */
5073 /*                                                                          */
5074 /* Returns:                                                                 */
5075 /*   0 for success, positive value for failure.                             */
5076 /****************************************************************************/
5077 static int
5078 bce_init_tx_chain(struct bce_softc *sc)
5079 {
5080 	struct tx_bd *txbd;
5081 	int i, rc = 0;
5082 
5083 	DBENTER(BCE_VERBOSE_RESET | BCE_VERBOSE_SEND | BCE_VERBOSE_LOAD);
5084 
5085 	/* Set the initial TX producer/consumer indices. */
5086 	sc->tx_prod        = 0;
5087 	sc->tx_cons        = 0;
5088 	sc->tx_prod_bseq   = 0;
5089 	sc->used_tx_bd     = 0;
5090 	sc->max_tx_bd      = USABLE_TX_BD;
5091 	DBRUN(sc->tx_hi_watermark = USABLE_TX_BD);
5092 	DBRUN(sc->tx_full_count = 0);
5093 
5094 	/*
5095 	 * The NetXtreme II supports a linked-list structre called
5096 	 * a Buffer Descriptor Chain (or BD chain).  A BD chain
5097 	 * consists of a series of 1 or more chain pages, each of which
5098 	 * consists of a fixed number of BD entries.
5099 	 * The last BD entry on each page is a pointer to the next page
5100 	 * in the chain, and the last pointer in the BD chain
5101 	 * points back to the beginning of the chain.
5102 	 */
5103 
5104 	/* Set the TX next pointer chain entries. */
5105 	for (i = 0; i < TX_PAGES; i++) {
5106 		int j;
5107 
5108 		txbd = &sc->tx_bd_chain[i][USABLE_TX_BD_PER_PAGE];
5109 
5110 		/* Check if we've reached the last page. */
5111 		if (i == (TX_PAGES - 1))
5112 			j = 0;
5113 		else
5114 			j = i + 1;
5115 
5116 		txbd->tx_bd_haddr_hi = htole32(BCE_ADDR_HI(sc->tx_bd_chain_paddr[j]));
5117 		txbd->tx_bd_haddr_lo = htole32(BCE_ADDR_LO(sc->tx_bd_chain_paddr[j]));
5118 	}
5119 
5120 	bce_init_tx_context(sc);
5121 
5122 	DBRUNMSG(BCE_INSANE_SEND, bce_dump_tx_chain(sc, 0, TOTAL_TX_BD));
5123 	DBEXIT(BCE_VERBOSE_RESET | BCE_VERBOSE_SEND | BCE_VERBOSE_LOAD);
5124 
5125 	return(rc);
5126 }
5127 
5128 
5129 /****************************************************************************/
5130 /* Free memory and clear the TX data structures.                            */
5131 /*                                                                          */
5132 /* Returns:                                                                 */
5133 /*   Nothing.                                                               */
5134 /****************************************************************************/
5135 static void
5136 bce_free_tx_chain(struct bce_softc *sc)
5137 {
5138 	int i;
5139 
5140 	DBENTER(BCE_VERBOSE_RESET | BCE_VERBOSE_SEND | BCE_VERBOSE_UNLOAD);
5141 
5142 	/* Unmap, unload, and free any mbufs still in the TX mbuf chain. */
5143 	for (i = 0; i < TOTAL_TX_BD; i++) {
5144 		if (sc->tx_mbuf_ptr[i] != NULL) {
5145 			if (sc->tx_mbuf_map[i] != NULL)
5146 				bus_dmamap_sync(sc->tx_mbuf_tag, sc->tx_mbuf_map[i],
5147 					BUS_DMASYNC_POSTWRITE);
5148 			m_freem(sc->tx_mbuf_ptr[i]);
5149 			sc->tx_mbuf_ptr[i] = NULL;
5150 			DBRUN(sc->debug_tx_mbuf_alloc--);
5151 		}
5152 	}
5153 
5154 	/* Clear each TX chain page. */
5155 	for (i = 0; i < TX_PAGES; i++)
5156 		bzero((char *)sc->tx_bd_chain[i], BCE_TX_CHAIN_PAGE_SZ);
5157 
5158 	sc->used_tx_bd     = 0;
5159 
5160 	/* Check if we lost any mbufs in the process. */
5161 	DBRUNIF((sc->debug_tx_mbuf_alloc),
5162 		BCE_PRINTF("%s(%d): Memory leak! Lost %d mbufs "
5163 			"from tx chain!\n",
5164 			__FILE__, __LINE__, sc->debug_tx_mbuf_alloc));
5165 
5166 	DBEXIT(BCE_VERBOSE_RESET | BCE_VERBOSE_SEND | BCE_VERBOSE_UNLOAD);
5167 }
5168 
5169 
5170 /****************************************************************************/
5171 /* Initialize the RX context memory.                                        */
5172 /*                                                                          */
5173 /* Returns:                                                                 */
5174 /*   Nothing                                                                */
5175 /****************************************************************************/
5176 static void
5177 bce_init_rx_context(struct bce_softc *sc)
5178 {
5179 	u32 val;
5180 
5181 	DBENTER(BCE_VERBOSE_RESET | BCE_VERBOSE_RECV | BCE_VERBOSE_CTX);
5182 
5183 	/* Initialize the type, size, and BD cache levels for the RX context. */
5184 	val = BCE_L2CTX_RX_CTX_TYPE_CTX_BD_CHN_TYPE_VALUE |
5185 		BCE_L2CTX_RX_CTX_TYPE_SIZE_L2 |
5186 		(0x02 << BCE_L2CTX_RX_BD_PRE_READ_SHIFT);
5187 
5188 	/*
5189 	 * Set the level for generating pause frames
5190 	 * when the number of available rx_bd's gets
5191 	 * too low (the low watermark) and the level
5192 	 * when pause frames can be stopped (the high
5193 	 * watermark).
5194 	 */
5195 	if ((BCE_CHIP_NUM(sc) == BCE_CHIP_NUM_5709) ||
5196 		(BCE_CHIP_NUM(sc) == BCE_CHIP_NUM_5716)) {
5197 		u32 lo_water, hi_water;
5198 
5199 		lo_water = BCE_L2CTX_RX_LO_WATER_MARK_DEFAULT;
5200 		hi_water = USABLE_RX_BD / 4;
5201 
5202 		lo_water /= BCE_L2CTX_RX_LO_WATER_MARK_SCALE;
5203 		hi_water /= BCE_L2CTX_RX_HI_WATER_MARK_SCALE;
5204 
5205 		if (hi_water > 0xf)
5206 			hi_water = 0xf;
5207 		else if (hi_water == 0)
5208 			lo_water = 0;
5209 		val |= (lo_water << BCE_L2CTX_RX_LO_WATER_MARK_SHIFT) |
5210 			(hi_water << BCE_L2CTX_RX_HI_WATER_MARK_SHIFT);
5211 	}
5212 
5213  	CTX_WR(sc, GET_CID_ADDR(RX_CID), BCE_L2CTX_RX_CTX_TYPE, val);
5214 
5215 	/* Setup the MQ BIN mapping for l2_ctx_host_bseq. */
5216 	if ((BCE_CHIP_NUM(sc) == BCE_CHIP_NUM_5709) ||
5217 		(BCE_CHIP_NUM(sc) == BCE_CHIP_NUM_5716)) {
5218 		val = REG_RD(sc, BCE_MQ_MAP_L2_5);
5219 		REG_WR(sc, BCE_MQ_MAP_L2_5, val | BCE_MQ_MAP_L2_5_ARM);
5220 	}
5221 
5222 	/* Point the hardware to the first page in the chain. */
5223 	val = BCE_ADDR_HI(sc->rx_bd_chain_paddr[0]);
5224 	CTX_WR(sc, GET_CID_ADDR(RX_CID), BCE_L2CTX_RX_NX_BDHADDR_HI, val);
5225 	val = BCE_ADDR_LO(sc->rx_bd_chain_paddr[0]);
5226 	CTX_WR(sc, GET_CID_ADDR(RX_CID), BCE_L2CTX_RX_NX_BDHADDR_LO, val);
5227 
5228 	DBEXIT(BCE_VERBOSE_RESET | BCE_VERBOSE_RECV | BCE_VERBOSE_CTX);
5229 }
5230 
5231 
5232 /****************************************************************************/
5233 /* Allocate memory and initialize the RX data structures.                   */
5234 /*                                                                          */
5235 /* Returns:                                                                 */
5236 /*   0 for success, positive value for failure.                             */
5237 /****************************************************************************/
5238 static int
5239 bce_init_rx_chain(struct bce_softc *sc)
5240 {
5241 	struct rx_bd *rxbd;
5242 	int i, rc = 0;
5243 
5244 	DBENTER(BCE_VERBOSE_RESET | BCE_VERBOSE_RECV | BCE_VERBOSE_LOAD |
5245 		BCE_VERBOSE_CTX);
5246 
5247 	/* Initialize the RX producer and consumer indices. */
5248 	sc->rx_prod        = 0;
5249 	sc->rx_cons        = 0;
5250 	sc->rx_prod_bseq   = 0;
5251 	sc->free_rx_bd     = USABLE_RX_BD;
5252 	sc->max_rx_bd      = USABLE_RX_BD;
5253 	DBRUN(sc->rx_low_watermark = sc->max_rx_bd);
5254 	DBRUN(sc->rx_empty_count = 0);
5255 
5256 	/* Initialize the RX next pointer chain entries. */
5257 	for (i = 0; i < RX_PAGES; i++) {
5258 		int j;
5259 
5260 		rxbd = &sc->rx_bd_chain[i][USABLE_RX_BD_PER_PAGE];
5261 
5262 		/* Check if we've reached the last page. */
5263 		if (i == (RX_PAGES - 1))
5264 			j = 0;
5265 		else
5266 			j = i + 1;
5267 
5268 		/* Setup the chain page pointers. */
5269 		rxbd->rx_bd_haddr_hi = htole32(BCE_ADDR_HI(sc->rx_bd_chain_paddr[j]));
5270 		rxbd->rx_bd_haddr_lo = htole32(BCE_ADDR_LO(sc->rx_bd_chain_paddr[j]));
5271 	}
5272 
5273 	/* Fill up the RX chain. */
5274 	bce_fill_rx_chain(sc);
5275 
5276 	for (i = 0; i < RX_PAGES; i++) {
5277 		bus_dmamap_sync(sc->rx_bd_chain_tag, sc->rx_bd_chain_map[i],
5278 		    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
5279 	}
5280 
5281 	bce_init_rx_context(sc);
5282 
5283 	DBRUNMSG(BCE_EXTREME_RECV, bce_dump_rx_chain(sc, 0, TOTAL_RX_BD));
5284 	DBEXIT(BCE_VERBOSE_RESET | BCE_VERBOSE_RECV | BCE_VERBOSE_LOAD |
5285 		BCE_VERBOSE_CTX);
5286 	/* ToDo: Are there possible failure modes here? */
5287 	return(rc);
5288 }
5289 
5290 
5291 /****************************************************************************/
5292 /* Add mbufs to the RX chain until its full or an mbuf allocation error     */
5293 /* occurs.                                                                  */
5294 /*                                                                          */
5295 /* Returns:                                                                 */
5296 /*   Nothing                                                                */
5297 /****************************************************************************/
5298 static void
5299 bce_fill_rx_chain(struct bce_softc *sc)
5300 {
5301 	u16 prod, prod_idx;
5302 	u32 prod_bseq;
5303 
5304 	DBENTER(BCE_VERBOSE_RESET | BCE_EXTREME_RECV | BCE_VERBOSE_LOAD |
5305 		BCE_VERBOSE_CTX);
5306 
5307 	/* Get the RX chain producer indices. */
5308 	prod      = sc->rx_prod;
5309 	prod_bseq = sc->rx_prod_bseq;
5310 
5311 	/* Keep filling the RX chain until it's full. */
5312 	while (sc->free_rx_bd > 0) {
5313 		prod_idx = RX_CHAIN_IDX(prod);
5314 		if (bce_get_rx_buf(sc, NULL, &prod, &prod_idx, &prod_bseq)) {
5315 			/* Bail out if we can't add an mbuf to the chain. */
5316 			break;
5317 		}
5318 		prod = NEXT_RX_BD(prod);
5319 	}
5320 
5321 	/* Save the RX chain producer indices. */
5322 	sc->rx_prod      = prod;
5323 	sc->rx_prod_bseq = prod_bseq;
5324 
5325 	DBRUNIF(((prod & USABLE_RX_BD_PER_PAGE) == USABLE_RX_BD_PER_PAGE),
5326 		BCE_PRINTF("%s(): Invalid rx_prod value: 0x%04X\n",
5327 		__FUNCTION__, sc->rx_prod));
5328 
5329 	/* Write the mailbox and tell the chip about the waiting rx_bd's. */
5330 	REG_WR16(sc, MB_GET_CID_ADDR(RX_CID) + BCE_L2MQ_RX_HOST_BDIDX,
5331 		sc->rx_prod);
5332 	REG_WR(sc, MB_GET_CID_ADDR(RX_CID) + BCE_L2MQ_RX_HOST_BSEQ,
5333 		sc->rx_prod_bseq);
5334 
5335 	DBEXIT(BCE_VERBOSE_RESET | BCE_EXTREME_RECV | BCE_VERBOSE_LOAD |
5336 		BCE_VERBOSE_CTX);
5337 }
5338 
5339 
5340 /****************************************************************************/
5341 /* Free memory and clear the RX data structures.                            */
5342 /*                                                                          */
5343 /* Returns:                                                                 */
5344 /*   Nothing.                                                               */
5345 /****************************************************************************/
5346 static void
5347 bce_free_rx_chain(struct bce_softc *sc)
5348 {
5349 	int i;
5350 
5351 	DBENTER(BCE_VERBOSE_RESET | BCE_VERBOSE_RECV | BCE_VERBOSE_UNLOAD);
5352 
5353 	/* Free any mbufs still in the RX mbuf chain. */
5354 	for (i = 0; i < TOTAL_RX_BD; i++) {
5355 		if (sc->rx_mbuf_ptr[i] != NULL) {
5356 			if (sc->rx_mbuf_map[i] != NULL)
5357 				bus_dmamap_sync(sc->rx_mbuf_tag, sc->rx_mbuf_map[i],
5358 					BUS_DMASYNC_POSTREAD);
5359 			m_freem(sc->rx_mbuf_ptr[i]);
5360 			sc->rx_mbuf_ptr[i] = NULL;
5361 			DBRUN(sc->debug_rx_mbuf_alloc--);
5362 		}
5363 	}
5364 
5365 	/* Clear each RX chain page. */
5366 	for (i = 0; i < RX_PAGES; i++)
5367 		bzero((char *)sc->rx_bd_chain[i], BCE_RX_CHAIN_PAGE_SZ);
5368 
5369 	sc->free_rx_bd = sc->max_rx_bd;
5370 
5371 	/* Check if we lost any mbufs in the process. */
5372 	DBRUNIF((sc->debug_rx_mbuf_alloc),
5373 		BCE_PRINTF("%s(): Memory leak! Lost %d mbufs from rx chain!\n",
5374 			__FUNCTION__, sc->debug_rx_mbuf_alloc));
5375 
5376 	DBEXIT(BCE_VERBOSE_RESET | BCE_VERBOSE_RECV | BCE_VERBOSE_UNLOAD);
5377 }
5378 
5379 
5380 #ifdef ZERO_COPY_SOCKETS
5381 /****************************************************************************/
5382 /* Allocate memory and initialize the page data structures.                 */
5383 /* Assumes that bce_init_rx_chain() has not already been called.            */
5384 /*                                                                          */
5385 /* Returns:                                                                 */
5386 /*   0 for success, positive value for failure.                             */
5387 /****************************************************************************/
5388 static int
5389 bce_init_pg_chain(struct bce_softc *sc)
5390 {
5391 	struct rx_bd *pgbd;
5392 	int i, rc = 0;
5393 	u32 val;
5394 
5395 	DBENTER(BCE_VERBOSE_RESET | BCE_VERBOSE_RECV | BCE_VERBOSE_LOAD |
5396 		BCE_VERBOSE_CTX);
5397 
5398 	/* Initialize the page producer and consumer indices. */
5399 	sc->pg_prod        = 0;
5400 	sc->pg_cons        = 0;
5401 	sc->free_pg_bd     = USABLE_PG_BD;
5402 	sc->max_pg_bd      = USABLE_PG_BD;
5403 	DBRUN(sc->pg_low_watermark = sc->max_pg_bd);
5404 	DBRUN(sc->pg_empty_count = 0);
5405 
5406 	/* Initialize the page next pointer chain entries. */
5407 	for (i = 0; i < PG_PAGES; i++) {
5408 		int j;
5409 
5410 		pgbd = &sc->pg_bd_chain[i][USABLE_PG_BD_PER_PAGE];
5411 
5412 		/* Check if we've reached the last page. */
5413 		if (i == (PG_PAGES - 1))
5414 			j = 0;
5415 		else
5416 			j = i + 1;
5417 
5418 		/* Setup the chain page pointers. */
5419 		pgbd->rx_bd_haddr_hi = htole32(BCE_ADDR_HI(sc->pg_bd_chain_paddr[j]));
5420 		pgbd->rx_bd_haddr_lo = htole32(BCE_ADDR_LO(sc->pg_bd_chain_paddr[j]));
5421 	}
5422 
5423 	/* Setup the MQ BIN mapping for host_pg_bidx. */
5424 	if ((BCE_CHIP_NUM(sc) == BCE_CHIP_NUM_5709)	||
5425 		(BCE_CHIP_NUM(sc) == BCE_CHIP_NUM_5716))
5426 		REG_WR(sc, BCE_MQ_MAP_L2_3, BCE_MQ_MAP_L2_3_DEFAULT);
5427 
5428 	CTX_WR(sc, GET_CID_ADDR(RX_CID), BCE_L2CTX_RX_PG_BUF_SIZE, 0);
5429 
5430 	/* Configure the rx_bd and page chain mbuf cluster size. */
5431 	val = (sc->rx_bd_mbuf_data_len << 16) | sc->pg_bd_mbuf_alloc_size;
5432 	CTX_WR(sc, GET_CID_ADDR(RX_CID), BCE_L2CTX_RX_PG_BUF_SIZE, val);
5433 
5434 	/* Configure the context reserved for jumbo support. */
5435 	CTX_WR(sc, GET_CID_ADDR(RX_CID), BCE_L2CTX_RX_RBDC_KEY,
5436 		BCE_L2CTX_RX_RBDC_JUMBO_KEY);
5437 
5438 	/* Point the hardware to the first page in the page chain. */
5439 	val = BCE_ADDR_HI(sc->pg_bd_chain_paddr[0]);
5440 	CTX_WR(sc, GET_CID_ADDR(RX_CID), BCE_L2CTX_RX_NX_PG_BDHADDR_HI, val);
5441 	val = BCE_ADDR_LO(sc->pg_bd_chain_paddr[0]);
5442 	CTX_WR(sc, GET_CID_ADDR(RX_CID), BCE_L2CTX_RX_NX_PG_BDHADDR_LO, val);
5443 
5444 	/* Fill up the page chain. */
5445 	bce_fill_pg_chain(sc);
5446 
5447 	for (i = 0; i < PG_PAGES; i++) {
5448 		bus_dmamap_sync(sc->pg_bd_chain_tag, sc->pg_bd_chain_map[i],
5449 		    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
5450 	}
5451 
5452 	DBRUNMSG(BCE_EXTREME_RECV, bce_dump_pg_chain(sc, 0, TOTAL_PG_BD));
5453 	DBEXIT(BCE_VERBOSE_RESET | BCE_VERBOSE_RECV | BCE_VERBOSE_LOAD |
5454 		BCE_VERBOSE_CTX);
5455 	return(rc);
5456 }
5457 
5458 
5459 /****************************************************************************/
5460 /* Add mbufs to the page chain until its full or an mbuf allocation error   */
5461 /* occurs.                                                                  */
5462 /*                                                                          */
5463 /* Returns:                                                                 */
5464 /*   Nothing                                                                */
5465 /****************************************************************************/
5466 static void
5467 bce_fill_pg_chain(struct bce_softc *sc)
5468 {
5469 	u16 prod, prod_idx;
5470 
5471 	DBENTER(BCE_VERBOSE_RESET | BCE_EXTREME_RECV | BCE_VERBOSE_LOAD |
5472 		BCE_VERBOSE_CTX);
5473 
5474 	/* Get the page chain prodcuer index. */
5475 	prod = sc->pg_prod;
5476 
5477 	/* Keep filling the page chain until it's full. */
5478 	while (sc->free_pg_bd > 0) {
5479 		prod_idx = PG_CHAIN_IDX(prod);
5480 		if (bce_get_pg_buf(sc, NULL, &prod, &prod_idx)) {
5481 			/* Bail out if we can't add an mbuf to the chain. */
5482 			break;
5483 		}
5484 		prod = NEXT_PG_BD(prod);
5485 	}
5486 
5487 	/* Save the page chain producer index. */
5488 	sc->pg_prod = prod;
5489 
5490 	DBRUNIF(((prod & USABLE_RX_BD_PER_PAGE) == USABLE_RX_BD_PER_PAGE),
5491 		BCE_PRINTF("%s(): Invalid pg_prod value: 0x%04X\n",
5492 		__FUNCTION__, sc->pg_prod));
5493 
5494 	/*
5495 	 * Write the mailbox and tell the chip about
5496 	 * the new rx_bd's in the page chain.
5497 	 */
5498 	REG_WR16(sc, MB_GET_CID_ADDR(RX_CID) + BCE_L2MQ_RX_HOST_PG_BDIDX,
5499 		sc->pg_prod);
5500 
5501 	DBEXIT(BCE_VERBOSE_RESET | BCE_EXTREME_RECV | BCE_VERBOSE_LOAD |
5502 		BCE_VERBOSE_CTX);
5503 }
5504 
5505 
5506 /****************************************************************************/
5507 /* Free memory and clear the RX data structures.                            */
5508 /*                                                                          */
5509 /* Returns:                                                                 */
5510 /*   Nothing.                                                               */
5511 /****************************************************************************/
5512 static void
5513 bce_free_pg_chain(struct bce_softc *sc)
5514 {
5515 	int i;
5516 
5517 	DBENTER(BCE_VERBOSE_RESET | BCE_VERBOSE_RECV | BCE_VERBOSE_UNLOAD);
5518 
5519 	/* Free any mbufs still in the mbuf page chain. */
5520 	for (i = 0; i < TOTAL_PG_BD; i++) {
5521 		if (sc->pg_mbuf_ptr[i] != NULL) {
5522 			if (sc->pg_mbuf_map[i] != NULL)
5523 				bus_dmamap_sync(sc->pg_mbuf_tag, sc->pg_mbuf_map[i],
5524 					BUS_DMASYNC_POSTREAD);
5525 			m_freem(sc->pg_mbuf_ptr[i]);
5526 			sc->pg_mbuf_ptr[i] = NULL;
5527 			DBRUN(sc->debug_pg_mbuf_alloc--);
5528 		}
5529 	}
5530 
5531 	/* Clear each page chain pages. */
5532 	for (i = 0; i < PG_PAGES; i++)
5533 		bzero((char *)sc->pg_bd_chain[i], BCE_PG_CHAIN_PAGE_SZ);
5534 
5535 	sc->free_pg_bd = sc->max_pg_bd;
5536 
5537 	/* Check if we lost any mbufs in the process. */
5538 	DBRUNIF((sc->debug_pg_mbuf_alloc),
5539 		BCE_PRINTF("%s(): Memory leak! Lost %d mbufs from page chain!\n",
5540 			__FUNCTION__, sc->debug_pg_mbuf_alloc));
5541 
5542 	DBEXIT(BCE_VERBOSE_RESET | BCE_VERBOSE_RECV | BCE_VERBOSE_UNLOAD);
5543 }
5544 #endif /* ZERO_COPY_SOCKETS */
5545 
5546 
5547 /****************************************************************************/
5548 /* Set media options.                                                       */
5549 /*                                                                          */
5550 /* Returns:                                                                 */
5551 /*   0 for success, positive value for failure.                             */
5552 /****************************************************************************/
5553 static int
5554 bce_ifmedia_upd(struct ifnet *ifp)
5555 {
5556 	struct bce_softc *sc = ifp->if_softc;
5557 
5558 	DBENTER(BCE_VERBOSE);
5559 
5560 	BCE_LOCK(sc);
5561 	bce_ifmedia_upd_locked(ifp);
5562 	BCE_UNLOCK(sc);
5563 
5564 	DBEXIT(BCE_VERBOSE);
5565 	return (0);
5566 }
5567 
5568 
5569 /****************************************************************************/
5570 /* Set media options.                                                       */
5571 /*                                                                          */
5572 /* Returns:                                                                 */
5573 /*   Nothing.                                                               */
5574 /****************************************************************************/
5575 static void
5576 bce_ifmedia_upd_locked(struct ifnet *ifp)
5577 {
5578 	struct bce_softc *sc = ifp->if_softc;
5579 	struct mii_data *mii;
5580 
5581 	DBENTER(BCE_VERBOSE);
5582 
5583 	BCE_LOCK_ASSERT(sc);
5584 
5585 	mii = device_get_softc(sc->bce_miibus);
5586 
5587 	/* Make sure the MII bus has been enumerated. */
5588 	if (mii) {
5589 		sc->bce_link = 0;
5590 		if (mii->mii_instance) {
5591 			struct mii_softc *miisc;
5592 
5593 			LIST_FOREACH(miisc, &mii->mii_phys, mii_list)
5594 				mii_phy_reset(miisc);
5595 		}
5596 		mii_mediachg(mii);
5597 	}
5598 
5599 	DBEXIT(BCE_VERBOSE);
5600 }
5601 
5602 
5603 /****************************************************************************/
5604 /* Reports current media status.                                            */
5605 /*                                                                          */
5606 /* Returns:                                                                 */
5607 /*   Nothing.                                                               */
5608 /****************************************************************************/
5609 static void
5610 bce_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
5611 {
5612 	struct bce_softc *sc = ifp->if_softc;
5613 	struct mii_data *mii;
5614 
5615 	DBENTER(BCE_VERBOSE);
5616 
5617 	BCE_LOCK(sc);
5618 
5619 	mii = device_get_softc(sc->bce_miibus);
5620 
5621 	mii_pollstat(mii);
5622 	ifmr->ifm_active = mii->mii_media_active;
5623 	ifmr->ifm_status = mii->mii_media_status;
5624 
5625 	BCE_UNLOCK(sc);
5626 
5627 	DBEXIT(BCE_VERBOSE);
5628 }
5629 
5630 
5631 /****************************************************************************/
5632 /* Handles PHY generated interrupt events.                                  */
5633 /*                                                                          */
5634 /* Returns:                                                                 */
5635 /*   Nothing.                                                               */
5636 /****************************************************************************/
5637 static void
5638 bce_phy_intr(struct bce_softc *sc)
5639 {
5640 	u32 new_link_state, old_link_state;
5641 
5642 	DBENTER(BCE_VERBOSE_PHY | BCE_VERBOSE_INTR);
5643 
5644 	new_link_state = sc->status_block->status_attn_bits &
5645 		STATUS_ATTN_BITS_LINK_STATE;
5646 	old_link_state = sc->status_block->status_attn_bits_ack &
5647 		STATUS_ATTN_BITS_LINK_STATE;
5648 
5649 	/* Handle any changes if the link state has changed. */
5650 	if (new_link_state != old_link_state) {
5651 
5652 		/* Update the status_attn_bits_ack field in the status block. */
5653 		if (new_link_state) {
5654 			REG_WR(sc, BCE_PCICFG_STATUS_BIT_SET_CMD,
5655 				STATUS_ATTN_BITS_LINK_STATE);
5656 			DBPRINT(sc, BCE_INFO_PHY, "%s(): Link is now UP.\n",
5657 				__FUNCTION__);
5658 		}
5659 		else {
5660 			REG_WR(sc, BCE_PCICFG_STATUS_BIT_CLEAR_CMD,
5661 				STATUS_ATTN_BITS_LINK_STATE);
5662 			DBPRINT(sc, BCE_INFO_PHY, "%s(): Link is now DOWN.\n",
5663 				__FUNCTION__);
5664 		}
5665 
5666 		/*
5667 		 * Assume link is down and allow
5668 		 * tick routine to update the state
5669 		 * based on the actual media state.
5670 		 */
5671 		sc->bce_link = 0;
5672 		callout_stop(&sc->bce_tick_callout);
5673 		bce_tick(sc);
5674 	}
5675 
5676 	/* Acknowledge the link change interrupt. */
5677 	REG_WR(sc, BCE_EMAC_STATUS, BCE_EMAC_STATUS_LINK_CHANGE);
5678 
5679 	DBEXIT(BCE_VERBOSE_PHY | BCE_VERBOSE_INTR);
5680 }
5681 
5682 
5683 /****************************************************************************/
5684 /* Reads the receive consumer value from the status block (skipping over    */
5685 /* chain page pointer if necessary).                                        */
5686 /*                                                                          */
5687 /* Returns:                                                                 */
5688 /*   hw_cons                                                                */
5689 /****************************************************************************/
5690 static inline u16
5691 bce_get_hw_rx_cons(struct bce_softc *sc)
5692 {
5693 	u16 hw_cons;
5694 
5695 	rmb();
5696 	hw_cons = sc->status_block->status_rx_quick_consumer_index0;
5697 	if ((hw_cons & USABLE_RX_BD_PER_PAGE) == USABLE_RX_BD_PER_PAGE)
5698 		hw_cons++;
5699 
5700 	return hw_cons;
5701 }
5702 
5703 /****************************************************************************/
5704 /* Handles received frame interrupt events.                                 */
5705 /*                                                                          */
5706 /* Returns:                                                                 */
5707 /*   Nothing.                                                               */
5708 /****************************************************************************/
5709 static void
5710 bce_rx_intr(struct bce_softc *sc)
5711 {
5712 	struct ifnet *ifp = sc->bce_ifp;
5713 	struct l2_fhdr *l2fhdr;
5714 	unsigned int pkt_len;
5715 	u16 sw_rx_cons, sw_rx_cons_idx, hw_rx_cons;
5716 	u32 status;
5717 #ifdef ZERO_COPY_SOCKETS
5718 	unsigned int rem_len;
5719 	u16 sw_pg_cons, sw_pg_cons_idx;
5720 #endif
5721 
5722 	DBENTER(BCE_VERBOSE_RECV | BCE_VERBOSE_INTR);
5723 	DBRUN(sc->rx_interrupts++);
5724 	DBPRINT(sc, BCE_EXTREME_RECV, "%s(enter): rx_prod = 0x%04X, "
5725 		"rx_cons = 0x%04X, rx_prod_bseq = 0x%08X\n",
5726 		__FUNCTION__, sc->rx_prod, sc->rx_cons, sc->rx_prod_bseq);
5727 
5728 	/* Prepare the RX chain pages to be accessed by the host CPU. */
5729 	for (int i = 0; i < RX_PAGES; i++)
5730 		bus_dmamap_sync(sc->rx_bd_chain_tag,
5731 		    sc->rx_bd_chain_map[i], BUS_DMASYNC_POSTREAD);
5732 
5733 #ifdef ZERO_COPY_SOCKETS
5734 	/* Prepare the page chain pages to be accessed by the host CPU. */
5735 	for (int i = 0; i < PG_PAGES; i++)
5736 		bus_dmamap_sync(sc->pg_bd_chain_tag,
5737 		    sc->pg_bd_chain_map[i], BUS_DMASYNC_POSTREAD);
5738 #endif
5739 
5740 	/* Get the hardware's view of the RX consumer index. */
5741 	hw_rx_cons = sc->hw_rx_cons = bce_get_hw_rx_cons(sc);
5742 
5743 	/* Get working copies of the driver's view of the consumer indices. */
5744 	sw_rx_cons = sc->rx_cons;
5745 #ifdef ZERO_COPY_SOCKETS
5746 	sw_pg_cons = sc->pg_cons;
5747 #endif
5748 
5749 	/* Update some debug statistics counters */
5750 	DBRUNIF((sc->free_rx_bd < sc->rx_low_watermark),
5751 		sc->rx_low_watermark = sc->free_rx_bd);
5752 	DBRUNIF((sc->free_rx_bd == sc->max_rx_bd), sc->rx_empty_count++);
5753 
5754 	/* Scan through the receive chain as long as there is work to do */
5755 	/* ToDo: Consider setting a limit on the number of packets processed. */
5756 	rmb();
5757 	while (sw_rx_cons != hw_rx_cons) {
5758 		struct mbuf *m0;
5759 
5760 		/* Convert the producer/consumer indices to an actual rx_bd index. */
5761 		sw_rx_cons_idx = RX_CHAIN_IDX(sw_rx_cons);
5762 
5763 		/* Unmap the mbuf from DMA space. */
5764 		bus_dmamap_sync(sc->rx_mbuf_tag, sc->rx_mbuf_map[sw_rx_cons_idx],
5765 		    BUS_DMASYNC_POSTREAD);
5766 		bus_dmamap_unload(sc->rx_mbuf_tag,
5767 		    sc->rx_mbuf_map[sw_rx_cons_idx]);
5768 
5769 		/* Remove the mbuf from the RX chain. */
5770 		m0 = sc->rx_mbuf_ptr[sw_rx_cons_idx];
5771 		sc->rx_mbuf_ptr[sw_rx_cons_idx] = NULL;
5772 		DBRUN(sc->debug_rx_mbuf_alloc--);
5773 		sc->free_rx_bd++;
5774 
5775 		/*
5776 		 * Frames received on the NetXteme II are prepended	with an
5777 		 * l2_fhdr structure which provides status information about
5778 		 * the received frame (including VLAN tags and checksum info).
5779 		 * The frames are also automatically adjusted to align the IP
5780 		 * header (i.e. two null bytes are inserted before the Ethernet
5781 		 * header).  As a result the data DMA'd by the controller into
5782 		 * the mbuf is as follows:
5783 		 *
5784 		 * +---------+-----+---------------------+-----+
5785 		 * | l2_fhdr | pad | packet data         | FCS |
5786 		 * +---------+-----+---------------------+-----+
5787 		 *
5788 		 * The l2_fhdr needs to be checked and skipped and the FCS needs
5789 		 * to be stripped before sending the packet up the stack.
5790 		 */
5791 		l2fhdr  = mtod(m0, struct l2_fhdr *);
5792 
5793 		/* Get the packet data + FCS length and the status. */
5794 		pkt_len = l2fhdr->l2_fhdr_pkt_len;
5795 		status  = l2fhdr->l2_fhdr_status;
5796 
5797 		/*
5798 		 * Skip over the l2_fhdr and pad, resulting in the
5799 		 * following data in the mbuf:
5800 		 * +---------------------+-----+
5801 		 * | packet data         | FCS |
5802 		 * +---------------------+-----+
5803 		 */
5804 		m_adj(m0, sizeof(struct l2_fhdr) + ETHER_ALIGN);
5805 
5806 #ifdef ZERO_COPY_SOCKETS
5807 		/*
5808 		 * Check whether the received frame fits in a single
5809 		 * mbuf or not (i.e. packet data + FCS <=
5810 		 * sc->rx_bd_mbuf_data_len bytes).
5811 		 */
5812 		if (pkt_len > m0->m_len) {
5813 			/*
5814 			 * The received frame is larger than a single mbuf.
5815 			 * If the frame was a TCP frame then only the TCP
5816 			 * header is placed in the mbuf, the remaining
5817 			 * payload (including FCS) is placed in the page
5818 			 * chain, the SPLIT flag is set, and the header
5819 			 * length is placed in the IP checksum field.
5820 			 * If the frame is not a TCP frame then the mbuf
5821 			 * is filled and the remaining bytes are placed
5822 			 * in the page chain.
5823 			 */
5824 
5825 			DBPRINT(sc, BCE_INFO_RECV, "%s(): Found a large packet.\n",
5826 				__FUNCTION__);
5827 
5828 			/*
5829 			 * When the page chain is enabled and the TCP
5830 			 * header has been split from the TCP payload,
5831 			 * the ip_xsum structure will reflect the length
5832 			 * of the TCP header, not the IP checksum.  Set
5833 			 * the packet length of the mbuf accordingly.
5834 			 */
5835 		 	if (status & L2_FHDR_STATUS_SPLIT)
5836 				m0->m_len = l2fhdr->l2_fhdr_ip_xsum;
5837 
5838 			rem_len = pkt_len - m0->m_len;
5839 
5840 			/* Pull mbufs off the page chain for the remaining data. */
5841 			while (rem_len > 0) {
5842 				struct mbuf *m_pg;
5843 
5844 				sw_pg_cons_idx = PG_CHAIN_IDX(sw_pg_cons);
5845 
5846 				/* Remove the mbuf from the page chain. */
5847 				m_pg = sc->pg_mbuf_ptr[sw_pg_cons_idx];
5848 				sc->pg_mbuf_ptr[sw_pg_cons_idx] = NULL;
5849 				DBRUN(sc->debug_pg_mbuf_alloc--);
5850 				sc->free_pg_bd++;
5851 
5852 				/* Unmap the page chain mbuf from DMA space. */
5853 				bus_dmamap_sync(sc->pg_mbuf_tag,
5854 					sc->pg_mbuf_map[sw_pg_cons_idx],
5855 					BUS_DMASYNC_POSTREAD);
5856 				bus_dmamap_unload(sc->pg_mbuf_tag,
5857 					sc->pg_mbuf_map[sw_pg_cons_idx]);
5858 
5859 				/* Adjust the mbuf length. */
5860 				if (rem_len < m_pg->m_len) {
5861 					/* The mbuf chain is complete. */
5862 					m_pg->m_len = rem_len;
5863 					rem_len = 0;
5864 				} else {
5865 					/* More packet data is waiting. */
5866 					rem_len -= m_pg->m_len;
5867 				}
5868 
5869 				/* Concatenate the mbuf cluster to the mbuf. */
5870 				m_cat(m0, m_pg);
5871 
5872 				sw_pg_cons = NEXT_PG_BD(sw_pg_cons);
5873 			}
5874 
5875 			/* Set the total packet length. */
5876 			m0->m_pkthdr.len = pkt_len;
5877 
5878 		} else {
5879 			/*
5880 			 * The received packet is small and fits in a
5881 			 * single mbuf (i.e. the l2_fhdr + pad + packet +
5882 			 * FCS <= MHLEN).  In other words, the packet is
5883 			 * 154 bytes or less in size.
5884 			 */
5885 
5886 			DBPRINT(sc, BCE_INFO_RECV, "%s(): Found a small packet.\n",
5887 				__FUNCTION__);
5888 
5889 			/* Set the total packet length. */
5890 			m0->m_pkthdr.len = m0->m_len = pkt_len;
5891 		}
5892 #else
5893         /* Set the total packet length. */
5894 		m0->m_pkthdr.len = m0->m_len = pkt_len;
5895 #endif
5896 
5897 		/* Remove the trailing Ethernet FCS. */
5898 		m_adj(m0, -ETHER_CRC_LEN);
5899 
5900 		/* Check that the resulting mbuf chain is valid. */
5901 		DBRUN(m_sanity(m0, FALSE));
5902 		DBRUNIF(((m0->m_len < ETHER_HDR_LEN) |
5903 			(m0->m_pkthdr.len > BCE_MAX_JUMBO_ETHER_MTU_VLAN)),
5904 			BCE_PRINTF("Invalid Ethernet frame size!\n");
5905 			m_print(m0, 128));
5906 
5907 		DBRUNIF(DB_RANDOMTRUE(l2fhdr_error_sim_control),
5908 			BCE_PRINTF("Simulating l2_fhdr status error.\n");
5909 			sc->l2fhdr_error_sim_count++;
5910 			status = status | L2_FHDR_ERRORS_PHY_DECODE);
5911 
5912 		/* Check the received frame for errors. */
5913 		if (status & (L2_FHDR_ERRORS_BAD_CRC |
5914 			L2_FHDR_ERRORS_PHY_DECODE | L2_FHDR_ERRORS_ALIGNMENT |
5915 			L2_FHDR_ERRORS_TOO_SHORT  | L2_FHDR_ERRORS_GIANT_FRAME)) {
5916 
5917 			/* Log the error and release the mbuf. */
5918 			ifp->if_ierrors++;
5919 			sc->l2fhdr_error_count++;
5920 
5921 			m_freem(m0);
5922 			m0 = NULL;
5923 			goto bce_rx_int_next_rx;
5924 		}
5925 
5926 		/* Send the packet to the appropriate interface. */
5927 		m0->m_pkthdr.rcvif = ifp;
5928 
5929 		/* Assume no hardware checksum. */
5930 		m0->m_pkthdr.csum_flags = 0;
5931 
5932 		/* Validate the checksum if offload enabled. */
5933 		if (ifp->if_capenable & IFCAP_RXCSUM) {
5934 
5935 			/* Check for an IP datagram. */
5936 		 	if (!(status & L2_FHDR_STATUS_SPLIT) &&
5937 				(status & L2_FHDR_STATUS_IP_DATAGRAM)) {
5938 				m0->m_pkthdr.csum_flags |= CSUM_IP_CHECKED;
5939 
5940 				/* Check if the IP checksum is valid. */
5941 				if ((l2fhdr->l2_fhdr_ip_xsum ^ 0xffff) == 0)
5942 					m0->m_pkthdr.csum_flags |= CSUM_IP_VALID;
5943 			}
5944 
5945 			/* Check for a valid TCP/UDP frame. */
5946 			if (status & (L2_FHDR_STATUS_TCP_SEGMENT |
5947 				L2_FHDR_STATUS_UDP_DATAGRAM)) {
5948 
5949 				/* Check for a good TCP/UDP checksum. */
5950 				if ((status & (L2_FHDR_ERRORS_TCP_XSUM |
5951 					      L2_FHDR_ERRORS_UDP_XSUM)) == 0) {
5952 					m0->m_pkthdr.csum_data =
5953 					    l2fhdr->l2_fhdr_tcp_udp_xsum;
5954 					m0->m_pkthdr.csum_flags |= (CSUM_DATA_VALID
5955 						| CSUM_PSEUDO_HDR);
5956 				}
5957 			}
5958 		}
5959 
5960 		/* Attach the VLAN tag.	*/
5961 		if (status & L2_FHDR_STATUS_L2_VLAN_TAG) {
5962 #if __FreeBSD_version < 700000
5963 			VLAN_INPUT_TAG(ifp, m0, l2fhdr->l2_fhdr_vlan_tag, continue);
5964 #else
5965 			m0->m_pkthdr.ether_vtag = l2fhdr->l2_fhdr_vlan_tag;
5966 			m0->m_flags |= M_VLANTAG;
5967 #endif
5968 		}
5969 
5970 		/* Increment received packet statistics. */
5971 		ifp->if_ipackets++;
5972 
5973 bce_rx_int_next_rx:
5974 		sw_rx_cons = NEXT_RX_BD(sw_rx_cons);
5975 
5976 		/* If we have a packet, pass it up the stack */
5977 		if (m0) {
5978 			/* Make sure we don't lose our place when we release the lock. */
5979 			sc->rx_cons = sw_rx_cons;
5980 #ifdef ZERO_COPY_SOCKETS
5981 			sc->pg_cons = sw_pg_cons;
5982 #endif
5983 
5984 			BCE_UNLOCK(sc);
5985 			(*ifp->if_input)(ifp, m0);
5986 			BCE_LOCK(sc);
5987 
5988 			/* Recover our place. */
5989 			sw_rx_cons = sc->rx_cons;
5990 #ifdef ZERO_COPY_SOCKETS
5991 			sw_pg_cons = sc->pg_cons;
5992 #endif
5993 		}
5994 
5995 		/* Refresh hw_cons to see if there's new work */
5996 		if (sw_rx_cons == hw_rx_cons)
5997 			hw_rx_cons = sc->hw_rx_cons = bce_get_hw_rx_cons(sc);
5998 	}
5999 
6000 	/* No new packets to process.  Refill the RX and page chains and exit. */
6001 #ifdef ZERO_COPY_SOCKETS
6002 	sc->pg_cons = sw_pg_cons;
6003 	bce_fill_pg_chain(sc);
6004 #endif
6005 
6006 	sc->rx_cons = sw_rx_cons;
6007 	bce_fill_rx_chain(sc);
6008 
6009 	/* Prepare the page chain pages to be accessed by the NIC. */
6010 	for (int i = 0; i < RX_PAGES; i++)
6011 		bus_dmamap_sync(sc->rx_bd_chain_tag,
6012 		    sc->rx_bd_chain_map[i], BUS_DMASYNC_PREWRITE);
6013 
6014 #ifdef ZERO_COPY_SOCKETS
6015 	for (int i = 0; i < PG_PAGES; i++)
6016 		bus_dmamap_sync(sc->pg_bd_chain_tag,
6017 		    sc->pg_bd_chain_map[i], BUS_DMASYNC_PREWRITE);
6018 #endif
6019 
6020 	DBPRINT(sc, BCE_EXTREME_RECV, "%s(exit): rx_prod = 0x%04X, "
6021 		"rx_cons = 0x%04X, rx_prod_bseq = 0x%08X\n",
6022 		__FUNCTION__, sc->rx_prod, sc->rx_cons, sc->rx_prod_bseq);
6023 	DBEXIT(BCE_VERBOSE_RECV | BCE_VERBOSE_INTR);
6024 }
6025 
6026 
6027 /****************************************************************************/
6028 /* Reads the transmit consumer value from the status block (skipping over   */
6029 /* chain page pointer if necessary).                                        */
6030 /*                                                                          */
6031 /* Returns:                                                                 */
6032 /*   hw_cons                                                                */
6033 /****************************************************************************/
6034 static inline u16
6035 bce_get_hw_tx_cons(struct bce_softc *sc)
6036 {
6037 	u16 hw_cons;
6038 
6039 	mb();
6040 	hw_cons = sc->status_block->status_tx_quick_consumer_index0;
6041 	if ((hw_cons & USABLE_TX_BD_PER_PAGE) == USABLE_TX_BD_PER_PAGE)
6042 		hw_cons++;
6043 
6044 	return hw_cons;
6045 }
6046 
6047 
6048 /****************************************************************************/
6049 /* Handles transmit completion interrupt events.                            */
6050 /*                                                                          */
6051 /* Returns:                                                                 */
6052 /*   Nothing.                                                               */
6053 /****************************************************************************/
6054 static void
6055 bce_tx_intr(struct bce_softc *sc)
6056 {
6057 	struct ifnet *ifp = sc->bce_ifp;
6058 	u16 hw_tx_cons, sw_tx_cons, sw_tx_chain_cons;
6059 
6060 	DBENTER(BCE_VERBOSE_SEND | BCE_VERBOSE_INTR);
6061 	DBRUN(sc->tx_interrupts++);
6062 	DBPRINT(sc, BCE_EXTREME_SEND, "%s(enter): tx_prod = 0x%04X, "
6063 		"tx_cons = 0x%04X, tx_prod_bseq = 0x%08X\n",
6064 		__FUNCTION__, sc->tx_prod, sc->tx_cons, sc->tx_prod_bseq);
6065 
6066 	BCE_LOCK_ASSERT(sc);
6067 
6068 	/* Get the hardware's view of the TX consumer index. */
6069 	hw_tx_cons = sc->hw_tx_cons = bce_get_hw_tx_cons(sc);
6070 	sw_tx_cons = sc->tx_cons;
6071 
6072 	/* Prevent speculative reads from getting ahead of the status block. */
6073 	bus_space_barrier(sc->bce_btag, sc->bce_bhandle, 0, 0,
6074 		BUS_SPACE_BARRIER_READ);
6075 
6076 	/* Cycle through any completed TX chain page entries. */
6077 	while (sw_tx_cons != hw_tx_cons) {
6078 #ifdef BCE_DEBUG
6079 		struct tx_bd *txbd = NULL;
6080 #endif
6081 		sw_tx_chain_cons = TX_CHAIN_IDX(sw_tx_cons);
6082 
6083 		DBPRINT(sc, BCE_INFO_SEND,
6084 			"%s(): hw_tx_cons = 0x%04X, sw_tx_cons = 0x%04X, "
6085 			"sw_tx_chain_cons = 0x%04X\n",
6086 			__FUNCTION__, hw_tx_cons, sw_tx_cons, sw_tx_chain_cons);
6087 
6088 		DBRUNIF((sw_tx_chain_cons > MAX_TX_BD),
6089 			BCE_PRINTF("%s(%d): TX chain consumer out of range! "
6090 				" 0x%04X > 0x%04X\n", __FILE__, __LINE__, sw_tx_chain_cons,
6091 				(int) MAX_TX_BD);
6092 			bce_breakpoint(sc));
6093 
6094 		DBRUN(txbd = &sc->tx_bd_chain[TX_PAGE(sw_tx_chain_cons)]
6095 				[TX_IDX(sw_tx_chain_cons)]);
6096 
6097 		DBRUNIF((txbd == NULL),
6098 			BCE_PRINTF("%s(%d): Unexpected NULL tx_bd[0x%04X]!\n",
6099 				__FILE__, __LINE__, sw_tx_chain_cons);
6100 			bce_breakpoint(sc));
6101 
6102 		DBRUNMSG(BCE_INFO_SEND, BCE_PRINTF("%s(): ", __FUNCTION__);
6103 			bce_dump_txbd(sc, sw_tx_chain_cons, txbd));
6104 
6105 		/*
6106 		 * Free the associated mbuf. Remember
6107 		 * that only the last tx_bd of a packet
6108 		 * has an mbuf pointer and DMA map.
6109 		 */
6110 		if (sc->tx_mbuf_ptr[sw_tx_chain_cons] != NULL) {
6111 
6112 			/* Validate that this is the last tx_bd. */
6113 			DBRUNIF((!(txbd->tx_bd_flags & TX_BD_FLAGS_END)),
6114 				BCE_PRINTF("%s(%d): tx_bd END flag not set but "
6115 				"txmbuf == NULL!\n", __FILE__, __LINE__);
6116 				bce_breakpoint(sc));
6117 
6118 			DBRUNMSG(BCE_INFO_SEND,
6119 				BCE_PRINTF("%s(): Unloading map/freeing mbuf "
6120 					"from tx_bd[0x%04X]\n", __FUNCTION__, sw_tx_chain_cons));
6121 
6122 			/* Unmap the mbuf. */
6123 			bus_dmamap_unload(sc->tx_mbuf_tag,
6124 			    sc->tx_mbuf_map[sw_tx_chain_cons]);
6125 
6126 			/* Free the mbuf. */
6127 			m_freem(sc->tx_mbuf_ptr[sw_tx_chain_cons]);
6128 			sc->tx_mbuf_ptr[sw_tx_chain_cons] = NULL;
6129 			DBRUN(sc->debug_tx_mbuf_alloc--);
6130 
6131 			ifp->if_opackets++;
6132 		}
6133 
6134 		sc->used_tx_bd--;
6135 		sw_tx_cons = NEXT_TX_BD(sw_tx_cons);
6136 
6137 		/* Refresh hw_cons to see if there's new work. */
6138 		hw_tx_cons = sc->hw_tx_cons = bce_get_hw_tx_cons(sc);
6139 
6140 		/* Prevent speculative reads from getting ahead of the status block. */
6141 		bus_space_barrier(sc->bce_btag, sc->bce_bhandle, 0, 0,
6142 			BUS_SPACE_BARRIER_READ);
6143 	}
6144 
6145 	/* Clear the TX timeout timer. */
6146 	sc->watchdog_timer = 0;
6147 
6148 	/* Clear the tx hardware queue full flag. */
6149 	if (sc->used_tx_bd < sc->max_tx_bd) {
6150 		DBRUNIF((ifp->if_drv_flags & IFF_DRV_OACTIVE),
6151 			DBPRINT(sc, BCE_INFO_SEND,
6152 				"%s(): Open TX chain! %d/%d (used/total)\n",
6153 				__FUNCTION__, sc->used_tx_bd, sc->max_tx_bd));
6154 		ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
6155 	}
6156 
6157 	sc->tx_cons = sw_tx_cons;
6158 
6159 	DBPRINT(sc, BCE_EXTREME_SEND, "%s(exit): tx_prod = 0x%04X, "
6160 		"tx_cons = 0x%04X, tx_prod_bseq = 0x%08X\n",
6161 		__FUNCTION__, sc->tx_prod, sc->tx_cons, sc->tx_prod_bseq);
6162 	DBEXIT(BCE_VERBOSE_SEND | BCE_VERBOSE_INTR);
6163 }
6164 
6165 
6166 /****************************************************************************/
6167 /* Disables interrupt generation.                                           */
6168 /*                                                                          */
6169 /* Returns:                                                                 */
6170 /*   Nothing.                                                               */
6171 /****************************************************************************/
6172 static void
6173 bce_disable_intr(struct bce_softc *sc)
6174 {
6175 	DBENTER(BCE_VERBOSE_INTR);
6176 
6177 	REG_WR(sc, BCE_PCICFG_INT_ACK_CMD, BCE_PCICFG_INT_ACK_CMD_MASK_INT);
6178 	REG_RD(sc, BCE_PCICFG_INT_ACK_CMD);
6179 
6180 	DBEXIT(BCE_VERBOSE_INTR);
6181 }
6182 
6183 
6184 /****************************************************************************/
6185 /* Enables interrupt generation.                                            */
6186 /*                                                                          */
6187 /* Returns:                                                                 */
6188 /*   Nothing.                                                               */
6189 /****************************************************************************/
6190 static void
6191 bce_enable_intr(struct bce_softc *sc, int coal_now)
6192 {
6193 	DBENTER(BCE_VERBOSE_INTR);
6194 
6195 	REG_WR(sc, BCE_PCICFG_INT_ACK_CMD,
6196 	       BCE_PCICFG_INT_ACK_CMD_INDEX_VALID |
6197 	       BCE_PCICFG_INT_ACK_CMD_MASK_INT | sc->last_status_idx);
6198 
6199 	REG_WR(sc, BCE_PCICFG_INT_ACK_CMD,
6200 	       BCE_PCICFG_INT_ACK_CMD_INDEX_VALID | sc->last_status_idx);
6201 
6202 	/* Force an immediate interrupt (whether there is new data or not). */
6203 	if (coal_now)
6204 		REG_WR(sc, BCE_HC_COMMAND, sc->hc_command | BCE_HC_COMMAND_COAL_NOW);
6205 
6206 	DBEXIT(BCE_VERBOSE_INTR);
6207 }
6208 
6209 
6210 /****************************************************************************/
6211 /* Handles controller initialization.                                       */
6212 /*                                                                          */
6213 /* Returns:                                                                 */
6214 /*   Nothing.                                                               */
6215 /****************************************************************************/
6216 static void
6217 bce_init_locked(struct bce_softc *sc)
6218 {
6219 	struct ifnet *ifp;
6220 	u32 ether_mtu = 0;
6221 
6222 	DBENTER(BCE_VERBOSE_RESET);
6223 
6224 	BCE_LOCK_ASSERT(sc);
6225 
6226 	ifp = sc->bce_ifp;
6227 
6228 	/* Check if the driver is still running and bail out if it is. */
6229 	if (ifp->if_drv_flags & IFF_DRV_RUNNING)
6230 		goto bce_init_locked_exit;
6231 
6232 	bce_stop(sc);
6233 
6234 	if (bce_reset(sc, BCE_DRV_MSG_CODE_RESET)) {
6235 		BCE_PRINTF("%s(%d): Controller reset failed!\n",
6236 			__FILE__, __LINE__);
6237 		goto bce_init_locked_exit;
6238 	}
6239 
6240 	if (bce_chipinit(sc)) {
6241 		BCE_PRINTF("%s(%d): Controller initialization failed!\n",
6242 			__FILE__, __LINE__);
6243 		goto bce_init_locked_exit;
6244 	}
6245 
6246 	if (bce_blockinit(sc)) {
6247 		BCE_PRINTF("%s(%d): Block initialization failed!\n",
6248 			__FILE__, __LINE__);
6249 		goto bce_init_locked_exit;
6250 	}
6251 
6252 	/* Load our MAC address. */
6253 	bcopy(IF_LLADDR(sc->bce_ifp), sc->eaddr, ETHER_ADDR_LEN);
6254 	bce_set_mac_addr(sc);
6255 
6256 	/*
6257 	 * Calculate and program the hardware Ethernet MTU
6258 	 * size. Be generous on the receive if we have room.
6259 	 */
6260 #ifdef ZERO_COPY_SOCKETS
6261 	if (ifp->if_mtu <= (sc->rx_bd_mbuf_data_len + sc->pg_bd_mbuf_alloc_size))
6262 		ether_mtu = sc->rx_bd_mbuf_data_len + sc->pg_bd_mbuf_alloc_size;
6263 #else
6264 	if (ifp->if_mtu <= sc->rx_bd_mbuf_data_len)
6265 		ether_mtu = sc->rx_bd_mbuf_data_len;
6266 #endif
6267 	else
6268 		ether_mtu = ifp->if_mtu;
6269 
6270 	ether_mtu += ETHER_HDR_LEN + ETHER_VLAN_ENCAP_LEN + ETHER_CRC_LEN;
6271 
6272 	DBPRINT(sc, BCE_INFO_MISC, "%s(): setting h/w mtu = %d\n", __FUNCTION__,
6273 		ether_mtu);
6274 
6275 	/* Program the mtu, enabling jumbo frame support if necessary. */
6276 	if (ether_mtu > (ETHER_MAX_LEN + ETHER_VLAN_ENCAP_LEN))
6277 		REG_WR(sc, BCE_EMAC_RX_MTU_SIZE,
6278 			min(ether_mtu, BCE_MAX_JUMBO_ETHER_MTU) |
6279 			BCE_EMAC_RX_MTU_SIZE_JUMBO_ENA);
6280 	else
6281 		REG_WR(sc, BCE_EMAC_RX_MTU_SIZE, ether_mtu);
6282 
6283 	DBPRINT(sc, BCE_INFO_LOAD,
6284 		"%s(): rx_bd_mbuf_alloc_size = %d, rx_bce_mbuf_data_len = %d, "
6285 		"rx_bd_mbuf_align_pad = %d\n", __FUNCTION__,
6286 		sc->rx_bd_mbuf_alloc_size, sc->rx_bd_mbuf_data_len,
6287 		sc->rx_bd_mbuf_align_pad);
6288 
6289 	/* Program appropriate promiscuous/multicast filtering. */
6290 	bce_set_rx_mode(sc);
6291 
6292 #ifdef ZERO_COPY_SOCKETS
6293 	DBPRINT(sc, BCE_INFO_LOAD, "%s(): pg_bd_mbuf_alloc_size = %d\n",
6294 		__FUNCTION__, sc->pg_bd_mbuf_alloc_size);
6295 
6296 	/* Init page buffer descriptor chain. */
6297 	bce_init_pg_chain(sc);
6298 #endif
6299 
6300 	/* Init RX buffer descriptor chain. */
6301 	bce_init_rx_chain(sc);
6302 
6303 	/* Init TX buffer descriptor chain. */
6304 	bce_init_tx_chain(sc);
6305 
6306 	/* Enable host interrupts. */
6307 	bce_enable_intr(sc, 1);
6308 
6309 	bce_ifmedia_upd_locked(ifp);
6310 
6311 	ifp->if_drv_flags |= IFF_DRV_RUNNING;
6312 	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
6313 
6314 	callout_reset(&sc->bce_tick_callout, hz, bce_tick, sc);
6315 
6316 bce_init_locked_exit:
6317 	DBEXIT(BCE_VERBOSE_RESET);
6318 }
6319 
6320 
6321 /****************************************************************************/
6322 /* Initialize the controller just enough so that any management firmware    */
6323 /* running on the device will continue to operate correctly.                */
6324 /*                                                                          */
6325 /* Returns:                                                                 */
6326 /*   Nothing.                                                               */
6327 /****************************************************************************/
6328 static void
6329 bce_mgmt_init_locked(struct bce_softc *sc)
6330 {
6331 	struct ifnet *ifp;
6332 
6333 	DBENTER(BCE_VERBOSE_RESET);
6334 
6335 	BCE_LOCK_ASSERT(sc);
6336 
6337 	/* Bail out if management firmware is not running. */
6338 	if (!(sc->bce_flags & BCE_MFW_ENABLE_FLAG)) {
6339 		DBPRINT(sc, BCE_VERBOSE_SPECIAL,
6340 			"No management firmware running...\n");
6341 		goto bce_mgmt_init_locked_exit;
6342 	}
6343 
6344 	ifp = sc->bce_ifp;
6345 
6346 	/* Enable all critical blocks in the MAC. */
6347 	REG_WR(sc, BCE_MISC_ENABLE_SET_BITS, BCE_MISC_ENABLE_DEFAULT);
6348 	REG_RD(sc, BCE_MISC_ENABLE_SET_BITS);
6349 	DELAY(20);
6350 
6351 	bce_ifmedia_upd_locked(ifp);
6352 
6353 bce_mgmt_init_locked_exit:
6354 	DBEXIT(BCE_VERBOSE_RESET);
6355 }
6356 
6357 
6358 /****************************************************************************/
6359 /* Handles controller initialization when called from an unlocked routine.  */
6360 /*                                                                          */
6361 /* Returns:                                                                 */
6362 /*   Nothing.                                                               */
6363 /****************************************************************************/
6364 static void
6365 bce_init(void *xsc)
6366 {
6367 	struct bce_softc *sc = xsc;
6368 
6369 	DBENTER(BCE_VERBOSE_RESET);
6370 
6371 	BCE_LOCK(sc);
6372 	bce_init_locked(sc);
6373 	BCE_UNLOCK(sc);
6374 
6375 	DBEXIT(BCE_VERBOSE_RESET);
6376 }
6377 
6378 
6379 /****************************************************************************/
6380 /* Encapsultes an mbuf cluster into the tx_bd chain structure and makes the */
6381 /* memory visible to the controller.                                        */
6382 /*                                                                          */
6383 /* Returns:                                                                 */
6384 /*   0 for success, positive value for failure.                             */
6385 /* Modified:                                                                */
6386 /*   m_head: May be set to NULL if MBUF is excessively fragmented.          */
6387 /****************************************************************************/
6388 static int
6389 bce_tx_encap(struct bce_softc *sc, struct mbuf **m_head)
6390 {
6391 	bus_dma_segment_t segs[BCE_MAX_SEGMENTS];
6392 	bus_dmamap_t map;
6393 	struct tx_bd *txbd = NULL;
6394 	struct mbuf *m0;
6395 	struct ether_vlan_header *eh;
6396 	struct ip *ip;
6397 	struct tcphdr *th;
6398 	u16 prod, chain_prod, etype, mss = 0, vlan_tag = 0, flags = 0;
6399 	u32 prod_bseq;
6400 	int hdr_len = 0, e_hlen = 0, ip_hlen = 0, tcp_hlen = 0, ip_len = 0;
6401 
6402 #ifdef BCE_DEBUG
6403 	u16 debug_prod;
6404 #endif
6405 	int i, error, nsegs, rc = 0;
6406 
6407 	DBENTER(BCE_VERBOSE_SEND);
6408 	DBPRINT(sc, BCE_INFO_SEND,
6409 		"%s(enter): tx_prod = 0x%04X, tx_chain_prod = %04X, "
6410 		"tx_prod_bseq = 0x%08X\n",
6411 		__FUNCTION__, sc->tx_prod, (u16) TX_CHAIN_IDX(sc->tx_prod),
6412 		sc->tx_prod_bseq);
6413 
6414 	/* Transfer any checksum offload flags to the bd. */
6415 	m0 = *m_head;
6416 	if (m0->m_pkthdr.csum_flags) {
6417 		if (m0->m_pkthdr.csum_flags & CSUM_IP)
6418 			flags |= TX_BD_FLAGS_IP_CKSUM;
6419 		if (m0->m_pkthdr.csum_flags & (CSUM_TCP | CSUM_UDP))
6420 			flags |= TX_BD_FLAGS_TCP_UDP_CKSUM;
6421 		if (m0->m_pkthdr.csum_flags & CSUM_TSO) {
6422 			/* For TSO the controller needs two pieces of info, */
6423 			/* the MSS and the IP+TCP options length.           */
6424 			mss = htole16(m0->m_pkthdr.tso_segsz);
6425 
6426 			/* Map the header and find the Ethernet type & header length */
6427 			eh = mtod(m0, struct ether_vlan_header *);
6428 			if (eh->evl_encap_proto == htons(ETHERTYPE_VLAN)) {
6429 				etype = ntohs(eh->evl_proto);
6430 				e_hlen = ETHER_HDR_LEN + ETHER_VLAN_ENCAP_LEN;
6431 			} else {
6432 				etype = ntohs(eh->evl_encap_proto);
6433 				e_hlen = ETHER_HDR_LEN;
6434 			}
6435 
6436 			/* Check for supported TSO Ethernet types (only IPv4 for now) */
6437 			switch (etype) {
6438 				case ETHERTYPE_IP:
6439 					ip = (struct ip *)(m0->m_data + e_hlen);
6440 
6441 					/* TSO only supported for TCP protocol */
6442 					if (ip->ip_p != IPPROTO_TCP) {
6443 						BCE_PRINTF("%s(%d): TSO enabled for non-TCP frame!.\n",
6444 							__FILE__, __LINE__);
6445 						goto bce_tx_encap_skip_tso;
6446 					}
6447 
6448 					/* Get IP header length in bytes (min 20) */
6449 					ip_hlen = ip->ip_hl << 2;
6450 
6451 					/* Get the TCP header length in bytes (min 20) */
6452 					th = (struct tcphdr *)((caddr_t)ip + ip_hlen);
6453 					tcp_hlen = (th->th_off << 2);
6454 
6455 					/* IP header length and checksum will be calc'd by hardware */
6456 					ip_len = ip->ip_len;
6457 					ip->ip_len = 0;
6458 					ip->ip_sum = 0;
6459 					break;
6460 				case ETHERTYPE_IPV6:
6461 					BCE_PRINTF("%s(%d): TSO over IPv6 not supported!.\n",
6462 						__FILE__, __LINE__);
6463 					goto bce_tx_encap_skip_tso;
6464 				default:
6465 					BCE_PRINTF("%s(%d): TSO enabled for unsupported protocol!.\n",
6466 						__FILE__, __LINE__);
6467 					goto bce_tx_encap_skip_tso;
6468 			}
6469 
6470 			hdr_len = e_hlen + ip_hlen + tcp_hlen;
6471 
6472 			DBPRINT(sc, BCE_EXTREME_SEND,
6473 				"%s(): hdr_len = %d, e_hlen = %d, ip_hlen = %d, tcp_hlen = %d, ip_len = %d\n",
6474 				 __FUNCTION__, hdr_len, e_hlen, ip_hlen, tcp_hlen, ip_len);
6475 
6476 			/* Set the LSO flag in the TX BD */
6477 			flags |= TX_BD_FLAGS_SW_LSO;
6478 			/* Set the length of IP + TCP options (in 32 bit words) */
6479 			flags |= (((ip_hlen + tcp_hlen - 40) >> 2) << 8);
6480 
6481 bce_tx_encap_skip_tso:
6482 			DBRUN(sc->requested_tso_frames++);
6483 		}
6484 	}
6485 
6486 	/* Transfer any VLAN tags to the bd. */
6487 	if (m0->m_flags & M_VLANTAG) {
6488 		flags |= TX_BD_FLAGS_VLAN_TAG;
6489 		vlan_tag = m0->m_pkthdr.ether_vtag;
6490 	}
6491 
6492 	/* Map the mbuf into DMAable memory. */
6493 	prod = sc->tx_prod;
6494 	chain_prod = TX_CHAIN_IDX(prod);
6495 	map = sc->tx_mbuf_map[chain_prod];
6496 
6497 	/* Map the mbuf into our DMA address space. */
6498 	error = bus_dmamap_load_mbuf_sg(sc->tx_mbuf_tag, map, m0,
6499 	    segs, &nsegs, BUS_DMA_NOWAIT);
6500 
6501 	/* Check if the DMA mapping was successful */
6502 	if (error == EFBIG) {
6503 
6504 		sc->fragmented_mbuf_count++;
6505 
6506 		/* Try to defrag the mbuf. */
6507 		m0 = m_defrag(*m_head, M_DONTWAIT);
6508 		if (m0 == NULL) {
6509 			/* Defrag was unsuccessful */
6510 			m_freem(*m_head);
6511 			*m_head = NULL;
6512 			sc->mbuf_alloc_failed_count++;
6513 			rc = ENOBUFS;
6514 			goto bce_tx_encap_exit;
6515 		}
6516 
6517 		/* Defrag was successful, try mapping again */
6518 		*m_head = m0;
6519 		error = bus_dmamap_load_mbuf_sg(sc->tx_mbuf_tag, map, m0,
6520 		    segs, &nsegs, BUS_DMA_NOWAIT);
6521 
6522 		/* Still getting an error after a defrag. */
6523 		if (error == ENOMEM) {
6524 			/* Insufficient DMA buffers available. */
6525 			sc->dma_map_addr_tx_failed_count++;
6526 			rc = error;
6527 			goto bce_tx_encap_exit;
6528 		} else if (error != 0) {
6529 			/* Still can't map the mbuf, release it and return an error. */
6530 			BCE_PRINTF(
6531 			    "%s(%d): Unknown error mapping mbuf into TX chain!\n",
6532 			    __FILE__, __LINE__);
6533 			m_freem(m0);
6534 			*m_head = NULL;
6535 			sc->dma_map_addr_tx_failed_count++;
6536 			rc = ENOBUFS;
6537 			goto bce_tx_encap_exit;
6538 		}
6539 	} else if (error == ENOMEM) {
6540 		/* Insufficient DMA buffers available. */
6541 		sc->dma_map_addr_tx_failed_count++;
6542 		rc = error;
6543 		goto bce_tx_encap_exit;
6544 	} else if (error != 0) {
6545 		m_freem(m0);
6546 		*m_head = NULL;
6547 		sc->dma_map_addr_tx_failed_count++;
6548 		rc = error;
6549 		goto bce_tx_encap_exit;
6550 	}
6551 
6552 	/* Make sure there's room in the chain */
6553 	if (nsegs > (sc->max_tx_bd - sc->used_tx_bd)) {
6554 		bus_dmamap_unload(sc->tx_mbuf_tag, map);
6555 		rc = ENOBUFS;
6556 		goto bce_tx_encap_exit;
6557 	}
6558 
6559 	/* prod points to an empty tx_bd at this point. */
6560 	prod_bseq  = sc->tx_prod_bseq;
6561 
6562 #ifdef BCE_DEBUG
6563 	debug_prod = chain_prod;
6564 #endif
6565 
6566 	DBPRINT(sc, BCE_INFO_SEND,
6567 		"%s(start): prod = 0x%04X, chain_prod = 0x%04X, "
6568 		"prod_bseq = 0x%08X\n",
6569 		__FUNCTION__, prod, chain_prod, prod_bseq);
6570 
6571 	/*
6572 	 * Cycle through each mbuf segment that makes up
6573 	 * the outgoing frame, gathering the mapping info
6574 	 * for that segment and creating a tx_bd for
6575 	 * the mbuf.
6576 	 */
6577 	for (i = 0; i < nsegs ; i++) {
6578 
6579 		chain_prod = TX_CHAIN_IDX(prod);
6580 		txbd= &sc->tx_bd_chain[TX_PAGE(chain_prod)][TX_IDX(chain_prod)];
6581 
6582 		txbd->tx_bd_haddr_lo = htole32(BCE_ADDR_LO(segs[i].ds_addr));
6583 		txbd->tx_bd_haddr_hi = htole32(BCE_ADDR_HI(segs[i].ds_addr));
6584 		txbd->tx_bd_mss_nbytes = htole32(mss << 16) | htole16(segs[i].ds_len);
6585 		txbd->tx_bd_vlan_tag = htole16(vlan_tag);
6586 		txbd->tx_bd_flags = htole16(flags);
6587 		prod_bseq += segs[i].ds_len;
6588 		if (i == 0)
6589 			txbd->tx_bd_flags |= htole16(TX_BD_FLAGS_START);
6590 		prod = NEXT_TX_BD(prod);
6591 	}
6592 
6593 	/* Set the END flag on the last TX buffer descriptor. */
6594 	txbd->tx_bd_flags |= htole16(TX_BD_FLAGS_END);
6595 
6596 	DBRUNMSG(BCE_EXTREME_SEND, bce_dump_tx_chain(sc, debug_prod, nsegs));
6597 
6598 	DBPRINT(sc, BCE_INFO_SEND,
6599 		"%s( end ): prod = 0x%04X, chain_prod = 0x%04X, "
6600 		"prod_bseq = 0x%08X\n",
6601 		__FUNCTION__, prod, chain_prod, prod_bseq);
6602 
6603 	/*
6604 	 * Ensure that the mbuf pointer for this transmission
6605 	 * is placed at the array index of the last
6606 	 * descriptor in this chain.  This is done
6607 	 * because a single map is used for all
6608 	 * segments of the mbuf and we don't want to
6609 	 * unload the map before all of the segments
6610 	 * have been freed.
6611 	 */
6612 	sc->tx_mbuf_ptr[chain_prod] = m0;
6613 	sc->used_tx_bd += nsegs;
6614 
6615 	/* Update some debug statistic counters */
6616 	DBRUNIF((sc->used_tx_bd > sc->tx_hi_watermark),
6617 		sc->tx_hi_watermark = sc->used_tx_bd);
6618 	DBRUNIF((sc->used_tx_bd == sc->max_tx_bd), sc->tx_full_count++);
6619 	DBRUNIF(sc->debug_tx_mbuf_alloc++);
6620 
6621 	DBRUNMSG(BCE_EXTREME_SEND, bce_dump_tx_mbuf_chain(sc, chain_prod, 1));
6622 
6623 	/* prod points to the next free tx_bd at this point. */
6624 	sc->tx_prod = prod;
6625 	sc->tx_prod_bseq = prod_bseq;
6626 
6627 	DBPRINT(sc, BCE_INFO_SEND,
6628 		"%s(exit): prod = 0x%04X, chain_prod = %04X, "
6629 		"prod_bseq = 0x%08X\n",
6630 		__FUNCTION__, sc->tx_prod, (u16) TX_CHAIN_IDX(sc->tx_prod),
6631 		sc->tx_prod_bseq);
6632 
6633 bce_tx_encap_exit:
6634 	DBEXIT(BCE_VERBOSE_SEND);
6635 	return(rc);
6636 }
6637 
6638 
6639 /****************************************************************************/
6640 /* Main transmit routine when called from another routine with a lock.      */
6641 /*                                                                          */
6642 /* Returns:                                                                 */
6643 /*   Nothing.                                                               */
6644 /****************************************************************************/
6645 static void
6646 bce_start_locked(struct ifnet *ifp)
6647 {
6648 	struct bce_softc *sc = ifp->if_softc;
6649 	struct mbuf *m_head = NULL;
6650 	int count = 0;
6651 	u16 tx_prod, tx_chain_prod;
6652 
6653 	DBENTER(BCE_VERBOSE_SEND | BCE_VERBOSE_CTX);
6654 
6655 	BCE_LOCK_ASSERT(sc);
6656 
6657 	/* prod points to the next free tx_bd. */
6658 	tx_prod = sc->tx_prod;
6659 	tx_chain_prod = TX_CHAIN_IDX(tx_prod);
6660 
6661 	DBPRINT(sc, BCE_INFO_SEND,
6662 		"%s(enter): tx_prod = 0x%04X, tx_chain_prod = 0x%04X, "
6663 		"tx_prod_bseq = 0x%08X\n",
6664 		__FUNCTION__, tx_prod, tx_chain_prod, sc->tx_prod_bseq);
6665 
6666 	/* If there's no link or the transmit queue is empty then just exit. */
6667 	if (!sc->bce_link) {
6668 		DBPRINT(sc, BCE_INFO_SEND, "%s(): No link.\n",
6669 			__FUNCTION__);
6670 		goto bce_start_locked_exit;
6671 	}
6672 
6673 	if (IFQ_DRV_IS_EMPTY(&ifp->if_snd)) {
6674 		DBPRINT(sc, BCE_INFO_SEND, "%s(): Transmit queue empty.\n",
6675 			__FUNCTION__);
6676 		goto bce_start_locked_exit;
6677 	}
6678 
6679 	/*
6680 	 * Keep adding entries while there is space in the ring.
6681 	 */
6682 	while (sc->used_tx_bd < sc->max_tx_bd) {
6683 
6684 		/* Check for any frames to send. */
6685 		IFQ_DRV_DEQUEUE(&ifp->if_snd, m_head);
6686 
6687 		/* Stop when the transmit queue is empty. */
6688 		if (m_head == NULL)
6689 			break;
6690 
6691 		/*
6692 		 * Pack the data into the transmit ring. If we
6693 		 * don't have room, place the mbuf back at the
6694 		 * head of the queue and set the OACTIVE flag
6695 		 * to wait for the NIC to drain the chain.
6696 		 */
6697 		if (bce_tx_encap(sc, &m_head)) {
6698 			/* No room, put the frame back on the transmit queue. */
6699 			if (m_head != NULL)
6700 				IFQ_DRV_PREPEND(&ifp->if_snd, m_head);
6701 			ifp->if_drv_flags |= IFF_DRV_OACTIVE;
6702 			DBPRINT(sc, BCE_INFO_SEND,
6703 				"TX chain is closed for business! Total tx_bd used = %d\n",
6704 				sc->used_tx_bd);
6705 			break;
6706 		}
6707 
6708 		count++;
6709 
6710 		/* Send a copy of the frame to any BPF listeners. */
6711 		ETHER_BPF_MTAP(ifp, m_head);
6712 	}
6713 
6714 	/* Exit if no packets were dequeued. */
6715 	if (count == 0) {
6716 		DBPRINT(sc, BCE_VERBOSE_SEND, "%s(): No packets were dequeued\n",
6717 			__FUNCTION__);
6718 		goto bce_start_locked_exit;
6719 	}
6720 
6721 	DBPRINT(sc, BCE_VERBOSE_SEND, "%s(): Inserted %d frames into send queue.\n",
6722 		__FUNCTION__, count);
6723 
6724 	REG_WR(sc, BCE_MQ_COMMAND, REG_RD(sc, BCE_MQ_COMMAND) | BCE_MQ_COMMAND_NO_MAP_ERROR);
6725 
6726 	/* Write the mailbox and tell the chip about the waiting tx_bd's. */
6727 	DBPRINT(sc, BCE_VERBOSE_SEND, "%s(): MB_GET_CID_ADDR(TX_CID) = 0x%08X; "
6728 		"BCE_L2MQ_TX_HOST_BIDX = 0x%08X, sc->tx_prod = 0x%04X\n",
6729 		__FUNCTION__,
6730 		MB_GET_CID_ADDR(TX_CID), BCE_L2MQ_TX_HOST_BIDX, sc->tx_prod);
6731 	REG_WR16(sc, MB_GET_CID_ADDR(TX_CID) + BCE_L2MQ_TX_HOST_BIDX, sc->tx_prod);
6732 	DBPRINT(sc, BCE_VERBOSE_SEND, "%s(): MB_GET_CID_ADDR(TX_CID) = 0x%08X; "
6733 		"BCE_L2MQ_TX_HOST_BSEQ = 0x%08X, sc->tx_prod_bseq = 0x%04X\n",
6734 		__FUNCTION__,
6735 		MB_GET_CID_ADDR(TX_CID), BCE_L2MQ_TX_HOST_BSEQ, sc->tx_prod_bseq);
6736 	REG_WR(sc, MB_GET_CID_ADDR(TX_CID) + BCE_L2MQ_TX_HOST_BSEQ, sc->tx_prod_bseq);
6737 
6738 	/* Set the tx timeout. */
6739 	sc->watchdog_timer = BCE_TX_TIMEOUT;
6740 
6741 	DBRUNMSG(BCE_VERBOSE_SEND, bce_dump_ctx(sc, TX_CID));
6742 	DBRUNMSG(BCE_VERBOSE_SEND, bce_dump_mq_regs(sc));
6743 
6744 bce_start_locked_exit:
6745 	DBEXIT(BCE_VERBOSE_SEND | BCE_VERBOSE_CTX);
6746 	return;
6747 }
6748 
6749 
6750 /****************************************************************************/
6751 /* Main transmit routine when called from another routine without a lock.   */
6752 /*                                                                          */
6753 /* Returns:                                                                 */
6754 /*   Nothing.                                                               */
6755 /****************************************************************************/
6756 static void
6757 bce_start(struct ifnet *ifp)
6758 {
6759 	struct bce_softc *sc = ifp->if_softc;
6760 
6761 	DBENTER(BCE_VERBOSE_SEND);
6762 
6763 	BCE_LOCK(sc);
6764 	bce_start_locked(ifp);
6765 	BCE_UNLOCK(sc);
6766 
6767 	DBEXIT(BCE_VERBOSE_SEND);
6768 }
6769 
6770 
6771 /****************************************************************************/
6772 /* Handles any IOCTL calls from the operating system.                       */
6773 /*                                                                          */
6774 /* Returns:                                                                 */
6775 /*   0 for success, positive value for failure.                             */
6776 /****************************************************************************/
6777 static int
6778 bce_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
6779 {
6780 	struct bce_softc *sc = ifp->if_softc;
6781 	struct ifreq *ifr = (struct ifreq *) data;
6782 	struct mii_data *mii;
6783 	int mask, error = 0;
6784 
6785 	DBENTER(BCE_VERBOSE_MISC);
6786 
6787 	switch(command) {
6788 
6789 		/* Set the interface MTU. */
6790 		case SIOCSIFMTU:
6791 			/* Check that the MTU setting is supported. */
6792 			if ((ifr->ifr_mtu < BCE_MIN_MTU) ||
6793 				(ifr->ifr_mtu > BCE_MAX_JUMBO_MTU)) {
6794 				error = EINVAL;
6795 				break;
6796 			}
6797 
6798 			DBPRINT(sc, BCE_INFO_MISC,
6799 				"SIOCSIFMTU: Changing MTU from %d to %d\n",
6800 				(int) ifp->if_mtu, (int) ifr->ifr_mtu);
6801 
6802 			BCE_LOCK(sc);
6803 			ifp->if_mtu = ifr->ifr_mtu;
6804 			ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
6805 #ifdef ZERO_COPY_SOCKETS
6806 			/* No buffer allocation size changes are necessary. */
6807 #else
6808 			/* Recalculate our buffer allocation sizes. */
6809 			if ((ifp->if_mtu + ETHER_HDR_LEN + ETHER_VLAN_ENCAP_LEN + ETHER_CRC_LEN) > MCLBYTES) {
6810 				sc->rx_bd_mbuf_alloc_size = MJUM9BYTES;
6811 				sc->rx_bd_mbuf_align_pad  = roundup2(MJUM9BYTES, 16) - MJUM9BYTES;
6812 				sc->rx_bd_mbuf_data_len   = sc->rx_bd_mbuf_alloc_size -
6813 					sc->rx_bd_mbuf_align_pad;
6814 			} else {
6815 				sc->rx_bd_mbuf_alloc_size = MCLBYTES;
6816 				sc->rx_bd_mbuf_align_pad  = roundup2(MCLBYTES, 16) - MCLBYTES;
6817 				sc->rx_bd_mbuf_data_len   = sc->rx_bd_mbuf_alloc_size -
6818 					sc->rx_bd_mbuf_align_pad;
6819 			}
6820 #endif
6821 
6822 			bce_init_locked(sc);
6823 			BCE_UNLOCK(sc);
6824 			break;
6825 
6826 		/* Set interface flags. */
6827 		case SIOCSIFFLAGS:
6828 			DBPRINT(sc, BCE_VERBOSE_SPECIAL, "Received SIOCSIFFLAGS\n");
6829 
6830 			BCE_LOCK(sc);
6831 
6832 			/* Check if the interface is up. */
6833 			if (ifp->if_flags & IFF_UP) {
6834 				if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
6835 					/* Change promiscuous/multicast flags as necessary. */
6836 					bce_set_rx_mode(sc);
6837 				} else {
6838 					/* Start the HW */
6839 					bce_init_locked(sc);
6840 				}
6841 			} else {
6842 				/* The interface is down, check if driver is running. */
6843 				if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
6844 					bce_stop(sc);
6845 
6846 					/* If MFW is running, restart the controller a bit. */
6847 					if (sc->bce_flags & BCE_MFW_ENABLE_FLAG) {
6848 						bce_reset(sc, BCE_DRV_MSG_CODE_RESET);
6849 						bce_chipinit(sc);
6850 						bce_mgmt_init_locked(sc);
6851 					}
6852 				}
6853 			}
6854 
6855 			BCE_UNLOCK(sc);
6856 			error = 0;
6857 
6858 			break;
6859 
6860 		/* Add/Delete multicast address */
6861 		case SIOCADDMULTI:
6862 		case SIOCDELMULTI:
6863 			DBPRINT(sc, BCE_VERBOSE_MISC, "Received SIOCADDMULTI/SIOCDELMULTI\n");
6864 
6865 			BCE_LOCK(sc);
6866 			if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
6867 				bce_set_rx_mode(sc);
6868 				error = 0;
6869 			}
6870 			BCE_UNLOCK(sc);
6871 
6872 			break;
6873 
6874 		/* Set/Get Interface media */
6875 		case SIOCSIFMEDIA:
6876 		case SIOCGIFMEDIA:
6877 			DBPRINT(sc, BCE_VERBOSE_MISC, "Received SIOCSIFMEDIA/SIOCGIFMEDIA\n");
6878 
6879 			mii = device_get_softc(sc->bce_miibus);
6880 			error = ifmedia_ioctl(ifp, ifr,
6881 			    &mii->mii_media, command);
6882 			break;
6883 
6884 		/* Set interface capability */
6885 		case SIOCSIFCAP:
6886 			mask = ifr->ifr_reqcap ^ ifp->if_capenable;
6887 			DBPRINT(sc, BCE_INFO_MISC, "Received SIOCSIFCAP = 0x%08X\n", (u32) mask);
6888 
6889 			/* Toggle the TX checksum capabilites enable flag. */
6890 			if (mask & IFCAP_TXCSUM) {
6891 				ifp->if_capenable ^= IFCAP_TXCSUM;
6892 				if (IFCAP_TXCSUM & ifp->if_capenable)
6893 					ifp->if_hwassist = BCE_IF_HWASSIST;
6894 				else
6895 					ifp->if_hwassist = 0;
6896 			}
6897 
6898 			/* Toggle the RX checksum capabilities enable flag. */
6899 			if (mask & IFCAP_RXCSUM) {
6900 				ifp->if_capenable ^= IFCAP_RXCSUM;
6901 				if (IFCAP_RXCSUM & ifp->if_capenable)
6902 					ifp->if_hwassist = BCE_IF_HWASSIST;
6903 				else
6904 					ifp->if_hwassist = 0;
6905 			}
6906 
6907 			/* Toggle the TSO capabilities enable flag. */
6908 			if (bce_tso_enable && (mask & IFCAP_TSO4)) {
6909 				ifp->if_capenable ^= IFCAP_TSO4;
6910 				if (IFCAP_RXCSUM & ifp->if_capenable)
6911 					ifp->if_hwassist = BCE_IF_HWASSIST;
6912 				else
6913 					ifp->if_hwassist = 0;
6914 			}
6915 
6916 			/* Toggle VLAN_MTU capabilities enable flag. */
6917 			if (mask & IFCAP_VLAN_MTU) {
6918 				BCE_PRINTF("%s(%d): Changing VLAN_MTU not supported.\n",
6919 					__FILE__, __LINE__);
6920 			}
6921 
6922 			/* Toggle VLANHWTAG capabilities enabled flag. */
6923 			if (mask & IFCAP_VLAN_HWTAGGING) {
6924 				if (sc->bce_flags & BCE_MFW_ENABLE_FLAG)
6925 					BCE_PRINTF("%s(%d): Cannot change VLAN_HWTAGGING while "
6926 						"management firmware (ASF/IPMI/UMP) is running!\n",
6927 						__FILE__, __LINE__);
6928 				else
6929 					BCE_PRINTF("%s(%d): Changing VLAN_HWTAGGING not supported!\n",
6930 						__FILE__, __LINE__);
6931 			}
6932 
6933 			break;
6934 		default:
6935 			/* We don't know how to handle the IOCTL, pass it on. */
6936 			error = ether_ioctl(ifp, command, data);
6937 			break;
6938 	}
6939 
6940 	DBEXIT(BCE_VERBOSE_MISC);
6941 	return(error);
6942 }
6943 
6944 
6945 /****************************************************************************/
6946 /* Transmit timeout handler.                                                */
6947 /*                                                                          */
6948 /* Returns:                                                                 */
6949 /*   Nothing.                                                               */
6950 /****************************************************************************/
6951 static void
6952 bce_watchdog(struct bce_softc *sc)
6953 {
6954 	DBENTER(BCE_EXTREME_SEND);
6955 
6956 	BCE_LOCK_ASSERT(sc);
6957 
6958 	/* If the watchdog timer hasn't expired then just exit. */
6959 	if (sc->watchdog_timer == 0 || --sc->watchdog_timer)
6960 		goto bce_watchdog_exit;
6961 
6962 	/* If pause frames are active then don't reset the hardware. */
6963 	/* ToDo: Should we reset the timer here? */
6964 	if (REG_RD(sc, BCE_EMAC_TX_STATUS) & BCE_EMAC_TX_STATUS_XOFFED)
6965 		goto bce_watchdog_exit;
6966 
6967 	BCE_PRINTF("%s(%d): Watchdog timeout occurred, resetting!\n",
6968 		__FILE__, __LINE__);
6969 
6970 	DBRUNMSG(BCE_INFO,
6971 		bce_dump_driver_state(sc);
6972 		bce_dump_status_block(sc);
6973 		bce_dump_stats_block(sc);
6974 		bce_dump_ftqs(sc);
6975 		bce_dump_txp_state(sc, 0);
6976 		bce_dump_rxp_state(sc, 0);
6977 		bce_dump_tpat_state(sc, 0);
6978 		bce_dump_cp_state(sc, 0);
6979 		bce_dump_com_state(sc, 0));
6980 
6981 	DBRUN(bce_breakpoint(sc));
6982 
6983 	sc->bce_ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
6984 
6985 	bce_init_locked(sc);
6986 	sc->bce_ifp->if_oerrors++;
6987 
6988 bce_watchdog_exit:
6989 	DBEXIT(BCE_EXTREME_SEND);
6990 }
6991 
6992 
6993 /*
6994  * Interrupt handler.
6995  */
6996 /****************************************************************************/
6997 /* Main interrupt entry point.  Verifies that the controller generated the  */
6998 /* interrupt and then calls a separate routine for handle the various       */
6999 /* interrupt causes (PHY, TX, RX).                                          */
7000 /*                                                                          */
7001 /* Returns:                                                                 */
7002 /*   0 for success, positive value for failure.                             */
7003 /****************************************************************************/
7004 static void
7005 bce_intr(void *xsc)
7006 {
7007 	struct bce_softc *sc;
7008 	struct ifnet *ifp;
7009 	u32 status_attn_bits;
7010 	u16 hw_rx_cons, hw_tx_cons;
7011 
7012 	sc = xsc;
7013 	ifp = sc->bce_ifp;
7014 
7015 	DBENTER(BCE_VERBOSE_SEND | BCE_VERBOSE_RECV | BCE_VERBOSE_INTR);
7016 	DBRUNMSG(BCE_VERBOSE_INTR, bce_dump_status_block(sc));
7017 
7018 	BCE_LOCK(sc);
7019 
7020 	DBRUN(sc->interrupts_generated++);
7021 
7022 	/* Synchnorize before we read from interface's status block */
7023 	bus_dmamap_sync(sc->status_tag, sc->status_map,
7024 	    BUS_DMASYNC_POSTREAD);
7025 
7026 	/*
7027 	 * If the hardware status block index
7028 	 * matches the last value read by the
7029 	 * driver and we haven't asserted our
7030 	 * interrupt then there's nothing to do.
7031 	 */
7032 	if ((sc->status_block->status_idx == sc->last_status_idx) &&
7033 		(REG_RD(sc, BCE_PCICFG_MISC_STATUS) & BCE_PCICFG_MISC_STATUS_INTA_VALUE)) {
7034 			DBPRINT(sc, BCE_VERBOSE_INTR, "%s(): Spurious interrupt.\n",
7035 				__FUNCTION__);
7036 			goto bce_intr_exit;
7037 	}
7038 
7039 	/* Ack the interrupt and stop others from occuring. */
7040 	REG_WR(sc, BCE_PCICFG_INT_ACK_CMD,
7041 		BCE_PCICFG_INT_ACK_CMD_USE_INT_HC_PARAM |
7042 		BCE_PCICFG_INT_ACK_CMD_MASK_INT);
7043 
7044 	/* Check if the hardware has finished any work. */
7045 	hw_rx_cons = bce_get_hw_rx_cons(sc);
7046 	hw_tx_cons = bce_get_hw_tx_cons(sc);
7047 
7048 	/* Keep processing data as long as there is work to do. */
7049 	for (;;) {
7050 
7051 		status_attn_bits = sc->status_block->status_attn_bits;
7052 
7053 	DBRUNIF(DB_RANDOMTRUE(unexpected_attention_sim_control),
7054 		BCE_PRINTF("Simulating unexpected status attention bit set.");
7055 		sc->unexpected_attention_sim_count++;
7056 		status_attn_bits = status_attn_bits | STATUS_ATTN_BITS_PARITY_ERROR);
7057 
7058 		/* Was it a link change interrupt? */
7059 		if ((status_attn_bits & STATUS_ATTN_BITS_LINK_STATE) !=
7060 			(sc->status_block->status_attn_bits_ack & STATUS_ATTN_BITS_LINK_STATE)) {
7061 			bce_phy_intr(sc);
7062 
7063 			/* Clear any transient status updates during link state change. */
7064 			REG_WR(sc, BCE_HC_COMMAND,
7065 				sc->hc_command | BCE_HC_COMMAND_COAL_NOW_WO_INT);
7066 			REG_RD(sc, BCE_HC_COMMAND);
7067 		}
7068 
7069 		/* If any other attention is asserted then the chip is toast. */
7070 		if (((status_attn_bits & ~STATUS_ATTN_BITS_LINK_STATE) !=
7071 			(sc->status_block->status_attn_bits_ack &
7072 			~STATUS_ATTN_BITS_LINK_STATE))) {
7073 
7074 		sc->unexpected_attention_count++;
7075 
7076 			BCE_PRINTF("%s(%d): Fatal attention detected: 0x%08X\n",
7077 				__FILE__, __LINE__, sc->status_block->status_attn_bits);
7078 
7079 			DBRUNMSG(BCE_FATAL,
7080 				if (unexpected_attention_sim_control == 0)
7081 					bce_breakpoint(sc));
7082 
7083 			bce_init_locked(sc);
7084 			goto bce_intr_exit;
7085 		}
7086 
7087 		/* Check for any completed RX frames. */
7088 		if (hw_rx_cons != sc->hw_rx_cons)
7089 			bce_rx_intr(sc);
7090 
7091 		/* Check for any completed TX frames. */
7092 		if (hw_tx_cons != sc->hw_tx_cons)
7093 			bce_tx_intr(sc);
7094 
7095 		/* Save the status block index value for use during the next interrupt. */
7096 		sc->last_status_idx = sc->status_block->status_idx;
7097 
7098 		/* Prevent speculative reads from getting ahead of the status block. */
7099 		bus_space_barrier(sc->bce_btag, sc->bce_bhandle, 0, 0,
7100 			BUS_SPACE_BARRIER_READ);
7101 
7102 		/* If there's no work left then exit the interrupt service routine. */
7103 		hw_rx_cons = bce_get_hw_rx_cons(sc);
7104 		hw_tx_cons = bce_get_hw_tx_cons(sc);
7105 
7106 		if ((hw_rx_cons == sc->hw_rx_cons) && (hw_tx_cons == sc->hw_tx_cons))
7107 			break;
7108 
7109 	}
7110 
7111 	bus_dmamap_sync(sc->status_tag,	sc->status_map,
7112 	    BUS_DMASYNC_PREREAD);
7113 
7114 	/* Re-enable interrupts. */
7115 	bce_enable_intr(sc, 0);
7116 
7117 	/* Handle any frames that arrived while handling the interrupt. */
7118 	if (ifp->if_drv_flags & IFF_DRV_RUNNING && !IFQ_DRV_IS_EMPTY(&ifp->if_snd))
7119 		bce_start_locked(ifp);
7120 
7121 bce_intr_exit:
7122 	BCE_UNLOCK(sc);
7123 
7124 	DBEXIT(BCE_VERBOSE_SEND | BCE_VERBOSE_RECV | BCE_VERBOSE_INTR);
7125 }
7126 
7127 
7128 /****************************************************************************/
7129 /* Programs the various packet receive modes (broadcast and multicast).     */
7130 /*                                                                          */
7131 /* Returns:                                                                 */
7132 /*   Nothing.                                                               */
7133 /****************************************************************************/
7134 static void
7135 bce_set_rx_mode(struct bce_softc *sc)
7136 {
7137 	struct ifnet *ifp;
7138 	struct ifmultiaddr *ifma;
7139 	u32 hashes[NUM_MC_HASH_REGISTERS] = { 0, 0, 0, 0, 0, 0, 0, 0 };
7140 	u32 rx_mode, sort_mode;
7141 	int h, i;
7142 
7143 	DBENTER(BCE_VERBOSE_MISC);
7144 
7145 	BCE_LOCK_ASSERT(sc);
7146 
7147 	ifp = sc->bce_ifp;
7148 
7149 	/* Initialize receive mode default settings. */
7150 	rx_mode   = sc->rx_mode & ~(BCE_EMAC_RX_MODE_PROMISCUOUS |
7151 			    BCE_EMAC_RX_MODE_KEEP_VLAN_TAG);
7152 	sort_mode = 1 | BCE_RPM_SORT_USER0_BC_EN;
7153 
7154 	/*
7155 	 * ASF/IPMI/UMP firmware requires that VLAN tag stripping
7156 	 * be enbled.
7157 	 */
7158 	if (!(BCE_IF_CAPABILITIES & IFCAP_VLAN_HWTAGGING) &&
7159 		(!(sc->bce_flags & BCE_MFW_ENABLE_FLAG)))
7160 		rx_mode |= BCE_EMAC_RX_MODE_KEEP_VLAN_TAG;
7161 
7162 	/*
7163 	 * Check for promiscuous, all multicast, or selected
7164 	 * multicast address filtering.
7165 	 */
7166 	if (ifp->if_flags & IFF_PROMISC) {
7167 		DBPRINT(sc, BCE_INFO_MISC, "Enabling promiscuous mode.\n");
7168 
7169 		/* Enable promiscuous mode. */
7170 		rx_mode |= BCE_EMAC_RX_MODE_PROMISCUOUS;
7171 		sort_mode |= BCE_RPM_SORT_USER0_PROM_EN;
7172 	} else if (ifp->if_flags & IFF_ALLMULTI) {
7173 		DBPRINT(sc, BCE_INFO_MISC, "Enabling all multicast mode.\n");
7174 
7175 		/* Enable all multicast addresses. */
7176 		for (i = 0; i < NUM_MC_HASH_REGISTERS; i++) {
7177 			REG_WR(sc, BCE_EMAC_MULTICAST_HASH0 + (i * 4), 0xffffffff);
7178        	}
7179 		sort_mode |= BCE_RPM_SORT_USER0_MC_EN;
7180 	} else {
7181 		/* Accept one or more multicast(s). */
7182 		DBPRINT(sc, BCE_INFO_MISC, "Enabling selective multicast mode.\n");
7183 
7184 		IF_ADDR_LOCK(ifp);
7185 		TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
7186 			if (ifma->ifma_addr->sa_family != AF_LINK)
7187 				continue;
7188 			h = ether_crc32_le(LLADDR((struct sockaddr_dl *)
7189 			    ifma->ifma_addr), ETHER_ADDR_LEN) & 0xFF;
7190 			    hashes[(h & 0xE0) >> 5] |= 1 << (h & 0x1F);
7191 		}
7192 		IF_ADDR_UNLOCK(ifp);
7193 
7194 		for (i = 0; i < NUM_MC_HASH_REGISTERS; i++)
7195 			REG_WR(sc, BCE_EMAC_MULTICAST_HASH0 + (i * 4), hashes[i]);
7196 
7197 		sort_mode |= BCE_RPM_SORT_USER0_MC_HSH_EN;
7198 	}
7199 
7200 	/* Only make changes if the recive mode has actually changed. */
7201 	if (rx_mode != sc->rx_mode) {
7202 		DBPRINT(sc, BCE_VERBOSE_MISC, "Enabling new receive mode: 0x%08X\n",
7203 			rx_mode);
7204 
7205 		sc->rx_mode = rx_mode;
7206 		REG_WR(sc, BCE_EMAC_RX_MODE, rx_mode);
7207 	}
7208 
7209 	/* Disable and clear the exisitng sort before enabling a new sort. */
7210 	REG_WR(sc, BCE_RPM_SORT_USER0, 0x0);
7211 	REG_WR(sc, BCE_RPM_SORT_USER0, sort_mode);
7212 	REG_WR(sc, BCE_RPM_SORT_USER0, sort_mode | BCE_RPM_SORT_USER0_ENA);
7213 
7214 	DBEXIT(BCE_VERBOSE_MISC);
7215 }
7216 
7217 
7218 /****************************************************************************/
7219 /* Called periodically to updates statistics from the controllers           */
7220 /* statistics block.                                                        */
7221 /*                                                                          */
7222 /* Returns:                                                                 */
7223 /*   Nothing.                                                               */
7224 /****************************************************************************/
7225 static void
7226 bce_stats_update(struct bce_softc *sc)
7227 {
7228 	struct ifnet *ifp;
7229 	struct statistics_block *stats;
7230 
7231 	DBENTER(BCE_EXTREME_MISC);
7232 
7233 	ifp = sc->bce_ifp;
7234 
7235 	stats = (struct statistics_block *) sc->stats_block;
7236 
7237 	/*
7238 	 * Certain controllers don't report
7239 	 * carrier sense errors correctly.
7240 	 * See errata E11_5708CA0_1165.
7241 	 */
7242 	if (!(BCE_CHIP_NUM(sc) == BCE_CHIP_NUM_5706) &&
7243 	    !(BCE_CHIP_ID(sc) == BCE_CHIP_ID_5708_A0))
7244 		ifp->if_oerrors += (u_long) stats->stat_Dot3StatsCarrierSenseErrors;
7245 
7246 	/*
7247 	 * Update the sysctl statistics from the
7248 	 * hardware statistics.
7249 	 */
7250 	sc->stat_IfHCInOctets =
7251 		((u64) stats->stat_IfHCInOctets_hi << 32) +
7252 		 (u64) stats->stat_IfHCInOctets_lo;
7253 
7254 	sc->stat_IfHCInBadOctets =
7255 		((u64) stats->stat_IfHCInBadOctets_hi << 32) +
7256 		 (u64) stats->stat_IfHCInBadOctets_lo;
7257 
7258 	sc->stat_IfHCOutOctets =
7259 		((u64) stats->stat_IfHCOutOctets_hi << 32) +
7260 		 (u64) stats->stat_IfHCOutOctets_lo;
7261 
7262 	sc->stat_IfHCOutBadOctets =
7263 		((u64) stats->stat_IfHCOutBadOctets_hi << 32) +
7264 		 (u64) stats->stat_IfHCOutBadOctets_lo;
7265 
7266 	sc->stat_IfHCInUcastPkts =
7267 		((u64) stats->stat_IfHCInUcastPkts_hi << 32) +
7268 		 (u64) stats->stat_IfHCInUcastPkts_lo;
7269 
7270 	sc->stat_IfHCInMulticastPkts =
7271 		((u64) stats->stat_IfHCInMulticastPkts_hi << 32) +
7272 		 (u64) stats->stat_IfHCInMulticastPkts_lo;
7273 
7274 	sc->stat_IfHCInBroadcastPkts =
7275 		((u64) stats->stat_IfHCInBroadcastPkts_hi << 32) +
7276 		 (u64) stats->stat_IfHCInBroadcastPkts_lo;
7277 
7278 	sc->stat_IfHCOutUcastPkts =
7279 		((u64) stats->stat_IfHCOutUcastPkts_hi << 32) +
7280 		 (u64) stats->stat_IfHCOutUcastPkts_lo;
7281 
7282 	sc->stat_IfHCOutMulticastPkts =
7283 		((u64) stats->stat_IfHCOutMulticastPkts_hi << 32) +
7284 		 (u64) stats->stat_IfHCOutMulticastPkts_lo;
7285 
7286 	sc->stat_IfHCOutBroadcastPkts =
7287 		((u64) stats->stat_IfHCOutBroadcastPkts_hi << 32) +
7288 		 (u64) stats->stat_IfHCOutBroadcastPkts_lo;
7289 
7290 	sc->stat_emac_tx_stat_dot3statsinternalmactransmiterrors =
7291 		stats->stat_emac_tx_stat_dot3statsinternalmactransmiterrors;
7292 
7293 	sc->stat_Dot3StatsCarrierSenseErrors =
7294 		stats->stat_Dot3StatsCarrierSenseErrors;
7295 
7296 	sc->stat_Dot3StatsFCSErrors =
7297 		stats->stat_Dot3StatsFCSErrors;
7298 
7299 	sc->stat_Dot3StatsAlignmentErrors =
7300 		stats->stat_Dot3StatsAlignmentErrors;
7301 
7302 	sc->stat_Dot3StatsSingleCollisionFrames =
7303 		stats->stat_Dot3StatsSingleCollisionFrames;
7304 
7305 	sc->stat_Dot3StatsMultipleCollisionFrames =
7306 		stats->stat_Dot3StatsMultipleCollisionFrames;
7307 
7308 	sc->stat_Dot3StatsDeferredTransmissions =
7309 		stats->stat_Dot3StatsDeferredTransmissions;
7310 
7311 	sc->stat_Dot3StatsExcessiveCollisions =
7312 		stats->stat_Dot3StatsExcessiveCollisions;
7313 
7314 	sc->stat_Dot3StatsLateCollisions =
7315 		stats->stat_Dot3StatsLateCollisions;
7316 
7317 	sc->stat_EtherStatsCollisions =
7318 		stats->stat_EtherStatsCollisions;
7319 
7320 	sc->stat_EtherStatsFragments =
7321 		stats->stat_EtherStatsFragments;
7322 
7323 	sc->stat_EtherStatsJabbers =
7324 		stats->stat_EtherStatsJabbers;
7325 
7326 	sc->stat_EtherStatsUndersizePkts =
7327 		stats->stat_EtherStatsUndersizePkts;
7328 
7329 	sc->stat_EtherStatsOversizePkts =
7330 		stats->stat_EtherStatsOversizePkts;
7331 
7332 	sc->stat_EtherStatsPktsRx64Octets =
7333 		stats->stat_EtherStatsPktsRx64Octets;
7334 
7335 	sc->stat_EtherStatsPktsRx65Octetsto127Octets =
7336 		stats->stat_EtherStatsPktsRx65Octetsto127Octets;
7337 
7338 	sc->stat_EtherStatsPktsRx128Octetsto255Octets =
7339 		stats->stat_EtherStatsPktsRx128Octetsto255Octets;
7340 
7341 	sc->stat_EtherStatsPktsRx256Octetsto511Octets =
7342 		stats->stat_EtherStatsPktsRx256Octetsto511Octets;
7343 
7344 	sc->stat_EtherStatsPktsRx512Octetsto1023Octets =
7345 		stats->stat_EtherStatsPktsRx512Octetsto1023Octets;
7346 
7347 	sc->stat_EtherStatsPktsRx1024Octetsto1522Octets =
7348 		stats->stat_EtherStatsPktsRx1024Octetsto1522Octets;
7349 
7350 	sc->stat_EtherStatsPktsRx1523Octetsto9022Octets =
7351 		stats->stat_EtherStatsPktsRx1523Octetsto9022Octets;
7352 
7353 	sc->stat_EtherStatsPktsTx64Octets =
7354 		stats->stat_EtherStatsPktsTx64Octets;
7355 
7356 	sc->stat_EtherStatsPktsTx65Octetsto127Octets =
7357 		stats->stat_EtherStatsPktsTx65Octetsto127Octets;
7358 
7359 	sc->stat_EtherStatsPktsTx128Octetsto255Octets =
7360 		stats->stat_EtherStatsPktsTx128Octetsto255Octets;
7361 
7362 	sc->stat_EtherStatsPktsTx256Octetsto511Octets =
7363 		stats->stat_EtherStatsPktsTx256Octetsto511Octets;
7364 
7365 	sc->stat_EtherStatsPktsTx512Octetsto1023Octets =
7366 		stats->stat_EtherStatsPktsTx512Octetsto1023Octets;
7367 
7368 	sc->stat_EtherStatsPktsTx1024Octetsto1522Octets =
7369 		stats->stat_EtherStatsPktsTx1024Octetsto1522Octets;
7370 
7371 	sc->stat_EtherStatsPktsTx1523Octetsto9022Octets =
7372 		stats->stat_EtherStatsPktsTx1523Octetsto9022Octets;
7373 
7374 	sc->stat_XonPauseFramesReceived =
7375 		stats->stat_XonPauseFramesReceived;
7376 
7377 	sc->stat_XoffPauseFramesReceived =
7378 		stats->stat_XoffPauseFramesReceived;
7379 
7380 	sc->stat_OutXonSent =
7381 		stats->stat_OutXonSent;
7382 
7383 	sc->stat_OutXoffSent =
7384 		stats->stat_OutXoffSent;
7385 
7386 	sc->stat_FlowControlDone =
7387 		stats->stat_FlowControlDone;
7388 
7389 	sc->stat_MacControlFramesReceived =
7390 		stats->stat_MacControlFramesReceived;
7391 
7392 	sc->stat_XoffStateEntered =
7393 		stats->stat_XoffStateEntered;
7394 
7395 	sc->stat_IfInFramesL2FilterDiscards =
7396 		stats->stat_IfInFramesL2FilterDiscards;
7397 
7398 	sc->stat_IfInRuleCheckerDiscards =
7399 		stats->stat_IfInRuleCheckerDiscards;
7400 
7401 	sc->stat_IfInFTQDiscards =
7402 		stats->stat_IfInFTQDiscards;
7403 
7404 	sc->stat_IfInMBUFDiscards =
7405 		stats->stat_IfInMBUFDiscards;
7406 
7407 	sc->stat_IfInRuleCheckerP4Hit =
7408 		stats->stat_IfInRuleCheckerP4Hit;
7409 
7410 	sc->stat_CatchupInRuleCheckerDiscards =
7411 		stats->stat_CatchupInRuleCheckerDiscards;
7412 
7413 	sc->stat_CatchupInFTQDiscards =
7414 		stats->stat_CatchupInFTQDiscards;
7415 
7416 	sc->stat_CatchupInMBUFDiscards =
7417 		stats->stat_CatchupInMBUFDiscards;
7418 
7419 	sc->stat_CatchupInRuleCheckerP4Hit =
7420 		stats->stat_CatchupInRuleCheckerP4Hit;
7421 
7422 	sc->com_no_buffers = REG_RD_IND(sc, 0x120084);
7423 
7424 	/*
7425 	 * Update the interface statistics from the
7426 	 * hardware statistics.
7427 	 */
7428 	ifp->if_collisions =
7429 		(u_long) sc->stat_EtherStatsCollisions;
7430 
7431 	/* ToDo: This method loses soft errors. */
7432 	ifp->if_ierrors =
7433 		(u_long) sc->stat_EtherStatsUndersizePkts +
7434 		(u_long) sc->stat_EtherStatsOversizePkts +
7435 		(u_long) sc->stat_IfInMBUFDiscards +
7436 		(u_long) sc->stat_Dot3StatsAlignmentErrors +
7437 		(u_long) sc->stat_Dot3StatsFCSErrors +
7438 		(u_long) sc->stat_IfInRuleCheckerDiscards +
7439 		(u_long) sc->stat_IfInFTQDiscards +
7440 		(u_long) sc->com_no_buffers;
7441 
7442 	/* ToDo: This method loses soft errors. */
7443 	ifp->if_oerrors =
7444 		(u_long) sc->stat_emac_tx_stat_dot3statsinternalmactransmiterrors +
7445 		(u_long) sc->stat_Dot3StatsExcessiveCollisions +
7446 		(u_long) sc->stat_Dot3StatsLateCollisions;
7447 
7448 	/* ToDo: Add additional statistics. */
7449 
7450 	DBEXIT(BCE_EXTREME_MISC);
7451 }
7452 
7453 
7454 /****************************************************************************/
7455 /* Periodic function to notify the bootcode that the driver is still        */
7456 /* present.                                                                 */
7457 /*                                                                          */
7458 /* Returns:                                                                 */
7459 /*   Nothing.                                                               */
7460 /****************************************************************************/
7461 static void
7462 bce_pulse(void *xsc)
7463 {
7464 	struct bce_softc *sc = xsc;
7465 	u32 msg;
7466 
7467 	DBENTER(BCE_EXTREME_MISC);
7468 
7469 	BCE_LOCK_ASSERT(sc);
7470 
7471 	/* Tell the firmware that the driver is still running. */
7472 	msg = (u32) ++sc->bce_fw_drv_pulse_wr_seq;
7473 	REG_WR_IND(sc, sc->bce_shmem_base + BCE_DRV_PULSE_MB, msg);
7474 
7475 	/* Schedule the next pulse. */
7476 	callout_reset(&sc->bce_pulse_callout, hz, bce_pulse, sc);
7477 
7478 	DBEXIT(BCE_EXTREME_MISC);
7479 }
7480 
7481 
7482 /****************************************************************************/
7483 /* Periodic function to perform maintenance tasks.                          */
7484 /*                                                                          */
7485 /* Returns:                                                                 */
7486 /*   Nothing.                                                               */
7487 /****************************************************************************/
7488 static void
7489 bce_tick(void *xsc)
7490 {
7491 	struct bce_softc *sc = xsc;
7492 	struct mii_data *mii;
7493 	struct ifnet *ifp;
7494 
7495 	ifp = sc->bce_ifp;
7496 
7497 	DBENTER(BCE_EXTREME_MISC);
7498 
7499 	BCE_LOCK_ASSERT(sc);
7500 
7501 	/* Schedule the next tick. */
7502 	callout_reset(&sc->bce_tick_callout, hz, bce_tick, sc);
7503 
7504 	/* Update the statistics from the hardware statistics block. */
7505 	bce_stats_update(sc);
7506 
7507 	/* Top off the receive and page chains. */
7508 #ifdef ZERO_COPY_SOCKETS
7509 	bce_fill_pg_chain(sc);
7510 #endif
7511 	bce_fill_rx_chain(sc);
7512 
7513 	/* Check that chip hasn't hung. */
7514 	bce_watchdog(sc);
7515 
7516 	/* If link is up already up then we're done. */
7517 	if (sc->bce_link)
7518 		goto bce_tick_exit;
7519 
7520 	/* Link is down.  Check what the PHY's doing. */
7521 	mii = device_get_softc(sc->bce_miibus);
7522 	mii_tick(mii);
7523 
7524 	/* Check if the link has come up. */
7525 	if ((mii->mii_media_status & IFM_ACTIVE) &&
7526 	    (IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE)) {
7527 		DBPRINT(sc, BCE_VERBOSE_MISC, "%s(): Link up!\n", __FUNCTION__);
7528 		sc->bce_link++;
7529 		if ((IFM_SUBTYPE(mii->mii_media_active) == IFM_1000_T ||
7530 		    IFM_SUBTYPE(mii->mii_media_active) == IFM_1000_SX) &&
7531 		    bootverbose)
7532 			BCE_PRINTF("Gigabit link up!\n");
7533 		/* Now that link is up, handle any outstanding TX traffic. */
7534 		if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd)) {
7535 			DBPRINT(sc, BCE_VERBOSE_MISC, "%s(): Found pending TX traffic.\n",
7536 				 __FUNCTION__);
7537 			bce_start_locked(ifp);
7538 		}
7539 	}
7540 
7541 bce_tick_exit:
7542 	DBEXIT(BCE_EXTREME_MISC);
7543 	return;
7544 }
7545 
7546 
7547 #ifdef BCE_DEBUG
7548 /****************************************************************************/
7549 /* Allows the driver state to be dumped through the sysctl interface.       */
7550 /*                                                                          */
7551 /* Returns:                                                                 */
7552 /*   0 for success, positive value for failure.                             */
7553 /****************************************************************************/
7554 static int
7555 bce_sysctl_driver_state(SYSCTL_HANDLER_ARGS)
7556 {
7557         int error;
7558         int result;
7559         struct bce_softc *sc;
7560 
7561         result = -1;
7562         error = sysctl_handle_int(oidp, &result, 0, req);
7563 
7564         if (error || !req->newptr)
7565                 return (error);
7566 
7567         if (result == 1) {
7568                 sc = (struct bce_softc *)arg1;
7569                 bce_dump_driver_state(sc);
7570         }
7571 
7572         return error;
7573 }
7574 
7575 
7576 /****************************************************************************/
7577 /* Allows the hardware state to be dumped through the sysctl interface.     */
7578 /*                                                                          */
7579 /* Returns:                                                                 */
7580 /*   0 for success, positive value for failure.                             */
7581 /****************************************************************************/
7582 static int
7583 bce_sysctl_hw_state(SYSCTL_HANDLER_ARGS)
7584 {
7585         int error;
7586         int result;
7587         struct bce_softc *sc;
7588 
7589         result = -1;
7590         error = sysctl_handle_int(oidp, &result, 0, req);
7591 
7592         if (error || !req->newptr)
7593                 return (error);
7594 
7595         if (result == 1) {
7596                 sc = (struct bce_softc *)arg1;
7597                 bce_dump_hw_state(sc);
7598         }
7599 
7600         return error;
7601 }
7602 
7603 
7604 /****************************************************************************/
7605 /* Allows the bootcode state to be dumped through the sysctl interface.     */
7606 /*                                                                          */
7607 /* Returns:                                                                 */
7608 /*   0 for success, positive value for failure.                             */
7609 /****************************************************************************/
7610 static int
7611 bce_sysctl_bc_state(SYSCTL_HANDLER_ARGS)
7612 {
7613         int error;
7614         int result;
7615         struct bce_softc *sc;
7616 
7617         result = -1;
7618         error = sysctl_handle_int(oidp, &result, 0, req);
7619 
7620         if (error || !req->newptr)
7621                 return (error);
7622 
7623         if (result == 1) {
7624                 sc = (struct bce_softc *)arg1;
7625                 bce_dump_bc_state(sc);
7626         }
7627 
7628         return error;
7629 }
7630 
7631 
7632 /****************************************************************************/
7633 /* Provides a sysctl interface to allow dumping the RX chain.               */
7634 /*                                                                          */
7635 /* Returns:                                                                 */
7636 /*   0 for success, positive value for failure.                             */
7637 /****************************************************************************/
7638 static int
7639 bce_sysctl_dump_rx_chain(SYSCTL_HANDLER_ARGS)
7640 {
7641         int error;
7642         int result;
7643         struct bce_softc *sc;
7644 
7645         result = -1;
7646         error = sysctl_handle_int(oidp, &result, 0, req);
7647 
7648         if (error || !req->newptr)
7649                 return (error);
7650 
7651         if (result == 1) {
7652                 sc = (struct bce_softc *)arg1;
7653                 bce_dump_rx_chain(sc, 0, TOTAL_RX_BD);
7654         }
7655 
7656         return error;
7657 }
7658 
7659 
7660 /****************************************************************************/
7661 /* Provides a sysctl interface to allow dumping the TX chain.               */
7662 /*                                                                          */
7663 /* Returns:                                                                 */
7664 /*   0 for success, positive value for failure.                             */
7665 /****************************************************************************/
7666 static int
7667 bce_sysctl_dump_tx_chain(SYSCTL_HANDLER_ARGS)
7668 {
7669         int error;
7670         int result;
7671         struct bce_softc *sc;
7672 
7673         result = -1;
7674         error = sysctl_handle_int(oidp, &result, 0, req);
7675 
7676         if (error || !req->newptr)
7677                 return (error);
7678 
7679         if (result == 1) {
7680                 sc = (struct bce_softc *)arg1;
7681                 bce_dump_tx_chain(sc, 0, USABLE_TX_BD);
7682         }
7683 
7684         return error;
7685 }
7686 
7687 
7688 #ifdef ZERO_COPY_SOCKETS
7689 /****************************************************************************/
7690 /* Provides a sysctl interface to allow dumping the page chain.             */
7691 /*                                                                          */
7692 /* Returns:                                                                 */
7693 /*   0 for success, positive value for failure.                             */
7694 /****************************************************************************/
7695 static int
7696 bce_sysctl_dump_pg_chain(SYSCTL_HANDLER_ARGS)
7697 {
7698         int error;
7699         int result;
7700         struct bce_softc *sc;
7701 
7702         result = -1;
7703         error = sysctl_handle_int(oidp, &result, 0, req);
7704 
7705         if (error || !req->newptr)
7706                 return (error);
7707 
7708         if (result == 1) {
7709                 sc = (struct bce_softc *)arg1;
7710                 bce_dump_pg_chain(sc, 0, TOTAL_PG_BD);
7711         }
7712 
7713         return error;
7714 }
7715 #endif
7716 
7717 /****************************************************************************/
7718 /* Provides a sysctl interface to allow reading arbitrary NVRAM offsets in  */
7719 /* the device.  DO NOT ENABLE ON PRODUCTION SYSTEMS!                        */
7720 /*                                                                          */
7721 /* Returns:                                                                 */
7722 /*   0 for success, positive value for failure.                             */
7723 /****************************************************************************/
7724 static int
7725 bce_sysctl_nvram_read(SYSCTL_HANDLER_ARGS)
7726 {
7727 	struct bce_softc *sc = (struct bce_softc *)arg1;
7728 	int error;
7729 	u32 result;
7730 	u32 val[1];
7731 	u8 *data = (u8 *) val;
7732 
7733 	result = -1;
7734 	error = sysctl_handle_int(oidp, &result, 0, req);
7735 	if (error || (req->newptr == NULL))
7736 		return (error);
7737 
7738 	bce_nvram_read(sc, result, data, 4);
7739 	BCE_PRINTF("offset 0x%08X = 0x%08X\n", result, bce_be32toh(val[0]));
7740 
7741 	return (error);
7742 }
7743 
7744 
7745 /****************************************************************************/
7746 /* Provides a sysctl interface to allow reading arbitrary registers in the  */
7747 /* device.  DO NOT ENABLE ON PRODUCTION SYSTEMS!                            */
7748 /*                                                                          */
7749 /* Returns:                                                                 */
7750 /*   0 for success, positive value for failure.                             */
7751 /****************************************************************************/
7752 static int
7753 bce_sysctl_reg_read(SYSCTL_HANDLER_ARGS)
7754 {
7755 	struct bce_softc *sc = (struct bce_softc *)arg1;
7756 	int error;
7757 	u32 val, result;
7758 
7759 	result = -1;
7760 	error = sysctl_handle_int(oidp, &result, 0, req);
7761 	if (error || (req->newptr == NULL))
7762 		return (error);
7763 
7764 	/* Make sure the register is accessible. */
7765 	if (result < 0x8000) {
7766 		val = REG_RD(sc, result);
7767 		BCE_PRINTF("reg 0x%08X = 0x%08X\n", result, val);
7768 	} else if (result < 0x0280000) {
7769 		val = REG_RD_IND(sc, result);
7770 		BCE_PRINTF("reg 0x%08X = 0x%08X\n", result, val);
7771 	}
7772 
7773 	return (error);
7774 }
7775 
7776 
7777 /****************************************************************************/
7778 /* Provides a sysctl interface to allow reading arbitrary PHY registers in  */
7779 /* the device.  DO NOT ENABLE ON PRODUCTION SYSTEMS!                        */
7780 /*                                                                          */
7781 /* Returns:                                                                 */
7782 /*   0 for success, positive value for failure.                             */
7783 /****************************************************************************/
7784 static int
7785 bce_sysctl_phy_read(SYSCTL_HANDLER_ARGS)
7786 {
7787 	struct bce_softc *sc;
7788 	device_t dev;
7789 	int error, result;
7790 	u16 val;
7791 
7792 	result = -1;
7793 	error = sysctl_handle_int(oidp, &result, 0, req);
7794 	if (error || (req->newptr == NULL))
7795 		return (error);
7796 
7797 	/* Make sure the register is accessible. */
7798 	if (result < 0x20) {
7799 		sc = (struct bce_softc *)arg1;
7800 		dev = sc->bce_dev;
7801 		val = bce_miibus_read_reg(dev, sc->bce_phy_addr, result);
7802 		BCE_PRINTF("phy 0x%02X = 0x%04X\n", result, val);
7803 	}
7804 	return (error);
7805 }
7806 
7807 
7808 /****************************************************************************/
7809 /* Provides a sysctl interface to allow reading a CID.                      */
7810 /*                                                                          */
7811 /* Returns:                                                                 */
7812 /*   0 for success, positive value for failure.                             */
7813 /****************************************************************************/
7814 static int
7815 bce_sysctl_dump_ctx(SYSCTL_HANDLER_ARGS)
7816 {
7817 	struct bce_softc *sc;
7818 	int error;
7819 	u16 result;
7820 
7821 	result = -1;
7822 	error = sysctl_handle_int(oidp, &result, 0, req);
7823 	if (error || (req->newptr == NULL))
7824 		return (error);
7825 
7826 	/* Make sure the register is accessible. */
7827 	if (result <= TX_CID) {
7828 		sc = (struct bce_softc *)arg1;
7829 		bce_dump_ctx(sc, result);
7830 	}
7831 
7832 	return (error);
7833 }
7834 
7835 
7836  /****************************************************************************/
7837 /* Provides a sysctl interface to forcing the driver to dump state and      */
7838 /* enter the debugger.  DO NOT ENABLE ON PRODUCTION SYSTEMS!                */
7839 /*                                                                          */
7840 /* Returns:                                                                 */
7841 /*   0 for success, positive value for failure.                             */
7842 /****************************************************************************/
7843 static int
7844 bce_sysctl_breakpoint(SYSCTL_HANDLER_ARGS)
7845 {
7846         int error;
7847         int result;
7848         struct bce_softc *sc;
7849 
7850         result = -1;
7851         error = sysctl_handle_int(oidp, &result, 0, req);
7852 
7853         if (error || !req->newptr)
7854                 return (error);
7855 
7856         if (result == 1) {
7857                 sc = (struct bce_softc *)arg1;
7858                 bce_breakpoint(sc);
7859         }
7860 
7861         return error;
7862 }
7863 #endif
7864 
7865 
7866 /****************************************************************************/
7867 /* Adds any sysctl parameters for tuning or debugging purposes.             */
7868 /*                                                                          */
7869 /* Returns:                                                                 */
7870 /*   0 for success, positive value for failure.                             */
7871 /****************************************************************************/
7872 static void
7873 bce_add_sysctls(struct bce_softc *sc)
7874 {
7875 	struct sysctl_ctx_list *ctx;
7876 	struct sysctl_oid_list *children;
7877 
7878 	DBENTER(BCE_VERBOSE_MISC);
7879 
7880 	ctx = device_get_sysctl_ctx(sc->bce_dev);
7881 	children = SYSCTL_CHILDREN(device_get_sysctl_tree(sc->bce_dev));
7882 
7883 #ifdef BCE_DEBUG
7884 	SYSCTL_ADD_INT(ctx, children, OID_AUTO,
7885 		"l2fhdr_error_sim_control",
7886 		CTLFLAG_RW, &l2fhdr_error_sim_control,
7887 		0, "Debug control to force l2fhdr errors");
7888 
7889 	SYSCTL_ADD_INT(ctx, children, OID_AUTO,
7890 		"l2fhdr_error_sim_count",
7891 		CTLFLAG_RD, &sc->l2fhdr_error_sim_count,
7892 		0, "Number of simulated l2_fhdr errors");
7893 #endif
7894 
7895 	SYSCTL_ADD_INT(ctx, children, OID_AUTO,
7896 		"l2fhdr_error_count",
7897 		CTLFLAG_RD, &sc->l2fhdr_error_count,
7898 		0, "Number of l2_fhdr errors");
7899 
7900 #ifdef BCE_DEBUG
7901 	SYSCTL_ADD_INT(ctx, children, OID_AUTO,
7902 		"mbuf_alloc_failed_sim_control",
7903 		CTLFLAG_RW, &mbuf_alloc_failed_sim_control,
7904 		0, "Debug control to force mbuf allocation failures");
7905 
7906 	SYSCTL_ADD_INT(ctx, children, OID_AUTO,
7907 		"mbuf_alloc_failed_sim_count",
7908 		CTLFLAG_RD, &sc->mbuf_alloc_failed_sim_count,
7909 		0, "Number of simulated mbuf cluster allocation failures");
7910 #endif
7911 
7912 	SYSCTL_ADD_INT(ctx, children, OID_AUTO,
7913 		"mbuf_alloc_failed_count",
7914 		CTLFLAG_RD, &sc->mbuf_alloc_failed_count,
7915 		0, "Number of mbuf allocation failures");
7916 
7917 	SYSCTL_ADD_INT(ctx, children, OID_AUTO,
7918 		"fragmented_mbuf_count",
7919 		CTLFLAG_RD, &sc->fragmented_mbuf_count,
7920 		0, "Number of fragmented mbufs");
7921 
7922 #ifdef BCE_DEBUG
7923 	SYSCTL_ADD_INT(ctx, children, OID_AUTO,
7924 		"dma_map_addr_failed_sim_control",
7925 		CTLFLAG_RW, &dma_map_addr_failed_sim_control,
7926 		0, "Debug control to force DMA mapping failures");
7927 
7928 	/* ToDo: Figure out how to update this value in bce_dma_map_addr(). */
7929 	SYSCTL_ADD_INT(ctx, children, OID_AUTO,
7930 		"dma_map_addr_failed_sim_count",
7931 		CTLFLAG_RD, &sc->dma_map_addr_failed_sim_count,
7932 		0, "Number of simulated DMA mapping failures");
7933 
7934 #endif
7935 
7936 	SYSCTL_ADD_INT(ctx, children, OID_AUTO,
7937 		"dma_map_addr_rx_failed_count",
7938 		CTLFLAG_RD, &sc->dma_map_addr_rx_failed_count,
7939 		0, "Number of RX DMA mapping failures");
7940 
7941 	SYSCTL_ADD_INT(ctx, children, OID_AUTO,
7942 		"dma_map_addr_tx_failed_count",
7943 		CTLFLAG_RD, &sc->dma_map_addr_tx_failed_count,
7944 		0, "Number of TX DMA mapping failures");
7945 
7946 #ifdef BCE_DEBUG
7947 	SYSCTL_ADD_INT(ctx, children, OID_AUTO,
7948 		"unexpected_attention_sim_control",
7949 		CTLFLAG_RW, &unexpected_attention_sim_control,
7950 		0, "Debug control to simulate unexpected attentions");
7951 
7952 	SYSCTL_ADD_INT(ctx, children, OID_AUTO,
7953 		"unexpected_attention_sim_count",
7954 		CTLFLAG_RW, &sc->unexpected_attention_sim_count,
7955 		0, "Number of simulated unexpected attentions");
7956 #endif
7957 
7958 	SYSCTL_ADD_INT(ctx, children, OID_AUTO,
7959 		"unexpected_attention_count",
7960 		CTLFLAG_RW, &sc->unexpected_attention_count,
7961 		0, "Number of unexpected attentions");
7962 
7963 #ifdef BCE_DEBUG
7964 	SYSCTL_ADD_INT(ctx, children, OID_AUTO,
7965 		"debug_bootcode_running_failure",
7966 		CTLFLAG_RW, &bootcode_running_failure_sim_control,
7967 		0, "Debug control to force bootcode running failures");
7968 
7969 	SYSCTL_ADD_INT(ctx, children, OID_AUTO,
7970 		"rx_low_watermark",
7971 		CTLFLAG_RD, &sc->rx_low_watermark,
7972 		0, "Lowest level of free rx_bd's");
7973 
7974 	SYSCTL_ADD_INT(ctx, children, OID_AUTO,
7975 		"rx_empty_count",
7976 		CTLFLAG_RD, &sc->rx_empty_count,
7977 		0, "Number of times the RX chain was empty");
7978 
7979 	SYSCTL_ADD_INT(ctx, children, OID_AUTO,
7980 		"tx_hi_watermark",
7981 		CTLFLAG_RD, &sc->tx_hi_watermark,
7982 		0, "Highest level of used tx_bd's");
7983 
7984 	SYSCTL_ADD_INT(ctx, children, OID_AUTO,
7985 		"tx_full_count",
7986 		CTLFLAG_RD, &sc->tx_full_count,
7987 		0, "Number of times the TX chain was full");
7988 
7989 	SYSCTL_ADD_INT(ctx, children, OID_AUTO,
7990 		"requested_tso_frames",
7991 		CTLFLAG_RD, &sc->requested_tso_frames,
7992 		0, "Number of TSO frames received");
7993 
7994 	SYSCTL_ADD_UINT(ctx, children, OID_AUTO,
7995 		"rx_interrupts",
7996 		CTLFLAG_RD, &sc->rx_interrupts,
7997 		0, "Number of RX interrupts");
7998 
7999 	SYSCTL_ADD_UINT(ctx, children, OID_AUTO,
8000 		"tx_interrupts",
8001 		CTLFLAG_RD, &sc->tx_interrupts,
8002 		0, "Number of TX interrupts");
8003 
8004 	SYSCTL_ADD_ULONG(ctx, children, OID_AUTO,
8005 		"rx_intr_time",
8006 		CTLFLAG_RD, &sc->rx_intr_time,
8007 		"RX interrupt time");
8008 
8009 	SYSCTL_ADD_ULONG(ctx, children, OID_AUTO,
8010 		"tx_intr_time",
8011 		CTLFLAG_RD, &sc->tx_intr_time,
8012 		"TX interrupt time");
8013 #endif
8014 
8015 	SYSCTL_ADD_ULONG(ctx, children, OID_AUTO,
8016 		"stat_IfHcInOctets",
8017 		CTLFLAG_RD, &sc->stat_IfHCInOctets,
8018 		"Bytes received");
8019 
8020 	SYSCTL_ADD_ULONG(ctx, children, OID_AUTO,
8021 		"stat_IfHCInBadOctets",
8022 		CTLFLAG_RD, &sc->stat_IfHCInBadOctets,
8023 		"Bad bytes received");
8024 
8025 	SYSCTL_ADD_ULONG(ctx, children, OID_AUTO,
8026 		"stat_IfHCOutOctets",
8027 		CTLFLAG_RD, &sc->stat_IfHCOutOctets,
8028 		"Bytes sent");
8029 
8030 	SYSCTL_ADD_ULONG(ctx, children, OID_AUTO,
8031 		"stat_IfHCOutBadOctets",
8032 		CTLFLAG_RD, &sc->stat_IfHCOutBadOctets,
8033 		"Bad bytes sent");
8034 
8035 	SYSCTL_ADD_ULONG(ctx, children, OID_AUTO,
8036 		"stat_IfHCInUcastPkts",
8037 		CTLFLAG_RD, &sc->stat_IfHCInUcastPkts,
8038 		"Unicast packets received");
8039 
8040 	SYSCTL_ADD_ULONG(ctx, children, OID_AUTO,
8041 		"stat_IfHCInMulticastPkts",
8042 		CTLFLAG_RD, &sc->stat_IfHCInMulticastPkts,
8043 		"Multicast packets received");
8044 
8045 	SYSCTL_ADD_ULONG(ctx, children, OID_AUTO,
8046 		"stat_IfHCInBroadcastPkts",
8047 		CTLFLAG_RD, &sc->stat_IfHCInBroadcastPkts,
8048 		"Broadcast packets received");
8049 
8050 	SYSCTL_ADD_ULONG(ctx, children, OID_AUTO,
8051 		"stat_IfHCOutUcastPkts",
8052 		CTLFLAG_RD, &sc->stat_IfHCOutUcastPkts,
8053 		"Unicast packets sent");
8054 
8055 	SYSCTL_ADD_ULONG(ctx, children, OID_AUTO,
8056 		"stat_IfHCOutMulticastPkts",
8057 		CTLFLAG_RD, &sc->stat_IfHCOutMulticastPkts,
8058 		"Multicast packets sent");
8059 
8060 	SYSCTL_ADD_ULONG(ctx, children, OID_AUTO,
8061 		"stat_IfHCOutBroadcastPkts",
8062 		CTLFLAG_RD, &sc->stat_IfHCOutBroadcastPkts,
8063 		"Broadcast packets sent");
8064 
8065 	SYSCTL_ADD_UINT(ctx, children, OID_AUTO,
8066 		"stat_emac_tx_stat_dot3statsinternalmactransmiterrors",
8067 		CTLFLAG_RD, &sc->stat_emac_tx_stat_dot3statsinternalmactransmiterrors,
8068 		0, "Internal MAC transmit errors");
8069 
8070 	SYSCTL_ADD_UINT(ctx, children, OID_AUTO,
8071 		"stat_Dot3StatsCarrierSenseErrors",
8072 		CTLFLAG_RD, &sc->stat_Dot3StatsCarrierSenseErrors,
8073 		0, "Carrier sense errors");
8074 
8075 	SYSCTL_ADD_UINT(ctx, children, OID_AUTO,
8076 		"stat_Dot3StatsFCSErrors",
8077 		CTLFLAG_RD, &sc->stat_Dot3StatsFCSErrors,
8078 		0, "Frame check sequence errors");
8079 
8080 	SYSCTL_ADD_UINT(ctx, children, OID_AUTO,
8081 		"stat_Dot3StatsAlignmentErrors",
8082 		CTLFLAG_RD, &sc->stat_Dot3StatsAlignmentErrors,
8083 		0, "Alignment errors");
8084 
8085 	SYSCTL_ADD_UINT(ctx, children, OID_AUTO,
8086 		"stat_Dot3StatsSingleCollisionFrames",
8087 		CTLFLAG_RD, &sc->stat_Dot3StatsSingleCollisionFrames,
8088 		0, "Single Collision Frames");
8089 
8090 	SYSCTL_ADD_UINT(ctx, children, OID_AUTO,
8091 		"stat_Dot3StatsMultipleCollisionFrames",
8092 		CTLFLAG_RD, &sc->stat_Dot3StatsMultipleCollisionFrames,
8093 		0, "Multiple Collision Frames");
8094 
8095 	SYSCTL_ADD_UINT(ctx, children, OID_AUTO,
8096 		"stat_Dot3StatsDeferredTransmissions",
8097 		CTLFLAG_RD, &sc->stat_Dot3StatsDeferredTransmissions,
8098 		0, "Deferred Transmissions");
8099 
8100 	SYSCTL_ADD_UINT(ctx, children, OID_AUTO,
8101 		"stat_Dot3StatsExcessiveCollisions",
8102 		CTLFLAG_RD, &sc->stat_Dot3StatsExcessiveCollisions,
8103 		0, "Excessive Collisions");
8104 
8105 	SYSCTL_ADD_UINT(ctx, children, OID_AUTO,
8106 		"stat_Dot3StatsLateCollisions",
8107 		CTLFLAG_RD, &sc->stat_Dot3StatsLateCollisions,
8108 		0, "Late Collisions");
8109 
8110 	SYSCTL_ADD_UINT(ctx, children, OID_AUTO,
8111 		"stat_EtherStatsCollisions",
8112 		CTLFLAG_RD, &sc->stat_EtherStatsCollisions,
8113 		0, "Collisions");
8114 
8115 	SYSCTL_ADD_UINT(ctx, children, OID_AUTO,
8116 		"stat_EtherStatsFragments",
8117 		CTLFLAG_RD, &sc->stat_EtherStatsFragments,
8118 		0, "Fragments");
8119 
8120 	SYSCTL_ADD_UINT(ctx, children, OID_AUTO,
8121 		"stat_EtherStatsJabbers",
8122 		CTLFLAG_RD, &sc->stat_EtherStatsJabbers,
8123 		0, "Jabbers");
8124 
8125 	SYSCTL_ADD_UINT(ctx, children, OID_AUTO,
8126 		"stat_EtherStatsUndersizePkts",
8127 		CTLFLAG_RD, &sc->stat_EtherStatsUndersizePkts,
8128 		0, "Undersize packets");
8129 
8130 	SYSCTL_ADD_UINT(ctx, children, OID_AUTO,
8131 		"stat_EtherStatsOversizePkts",
8132 		CTLFLAG_RD, &sc->stat_EtherStatsOversizePkts,
8133 		0, "stat_EtherStatsOversizePkts");
8134 
8135 	SYSCTL_ADD_UINT(ctx, children, OID_AUTO,
8136 		"stat_EtherStatsPktsRx64Octets",
8137 		CTLFLAG_RD, &sc->stat_EtherStatsPktsRx64Octets,
8138 		0, "Bytes received in 64 byte packets");
8139 
8140 	SYSCTL_ADD_UINT(ctx, children, OID_AUTO,
8141 		"stat_EtherStatsPktsRx65Octetsto127Octets",
8142 		CTLFLAG_RD, &sc->stat_EtherStatsPktsRx65Octetsto127Octets,
8143 		0, "Bytes received in 65 to 127 byte packets");
8144 
8145 	SYSCTL_ADD_UINT(ctx, children, OID_AUTO,
8146 		"stat_EtherStatsPktsRx128Octetsto255Octets",
8147 		CTLFLAG_RD, &sc->stat_EtherStatsPktsRx128Octetsto255Octets,
8148 		0, "Bytes received in 128 to 255 byte packets");
8149 
8150 	SYSCTL_ADD_UINT(ctx, children, OID_AUTO,
8151 		"stat_EtherStatsPktsRx256Octetsto511Octets",
8152 		CTLFLAG_RD, &sc->stat_EtherStatsPktsRx256Octetsto511Octets,
8153 		0, "Bytes received in 256 to 511 byte packets");
8154 
8155 	SYSCTL_ADD_UINT(ctx, children, OID_AUTO,
8156 		"stat_EtherStatsPktsRx512Octetsto1023Octets",
8157 		CTLFLAG_RD, &sc->stat_EtherStatsPktsRx512Octetsto1023Octets,
8158 		0, "Bytes received in 512 to 1023 byte packets");
8159 
8160 	SYSCTL_ADD_UINT(ctx, children, OID_AUTO,
8161 		"stat_EtherStatsPktsRx1024Octetsto1522Octets",
8162 		CTLFLAG_RD, &sc->stat_EtherStatsPktsRx1024Octetsto1522Octets,
8163 		0, "Bytes received in 1024 t0 1522 byte packets");
8164 
8165 	SYSCTL_ADD_UINT(ctx, children, OID_AUTO,
8166 		"stat_EtherStatsPktsRx1523Octetsto9022Octets",
8167 		CTLFLAG_RD, &sc->stat_EtherStatsPktsRx1523Octetsto9022Octets,
8168 		0, "Bytes received in 1523 to 9022 byte packets");
8169 
8170 	SYSCTL_ADD_UINT(ctx, children, OID_AUTO,
8171 		"stat_EtherStatsPktsTx64Octets",
8172 		CTLFLAG_RD, &sc->stat_EtherStatsPktsTx64Octets,
8173 		0, "Bytes sent in 64 byte packets");
8174 
8175 	SYSCTL_ADD_UINT(ctx, children, OID_AUTO,
8176 		"stat_EtherStatsPktsTx65Octetsto127Octets",
8177 		CTLFLAG_RD, &sc->stat_EtherStatsPktsTx65Octetsto127Octets,
8178 		0, "Bytes sent in 65 to 127 byte packets");
8179 
8180 	SYSCTL_ADD_UINT(ctx, children, OID_AUTO,
8181 		"stat_EtherStatsPktsTx128Octetsto255Octets",
8182 		CTLFLAG_RD, &sc->stat_EtherStatsPktsTx128Octetsto255Octets,
8183 		0, "Bytes sent in 128 to 255 byte packets");
8184 
8185 	SYSCTL_ADD_UINT(ctx, children, OID_AUTO,
8186 		"stat_EtherStatsPktsTx256Octetsto511Octets",
8187 		CTLFLAG_RD, &sc->stat_EtherStatsPktsTx256Octetsto511Octets,
8188 		0, "Bytes sent in 256 to 511 byte packets");
8189 
8190 	SYSCTL_ADD_UINT(ctx, children, OID_AUTO,
8191 		"stat_EtherStatsPktsTx512Octetsto1023Octets",
8192 		CTLFLAG_RD, &sc->stat_EtherStatsPktsTx512Octetsto1023Octets,
8193 		0, "Bytes sent in 512 to 1023 byte packets");
8194 
8195 	SYSCTL_ADD_UINT(ctx, children, OID_AUTO,
8196 		"stat_EtherStatsPktsTx1024Octetsto1522Octets",
8197 		CTLFLAG_RD, &sc->stat_EtherStatsPktsTx1024Octetsto1522Octets,
8198 		0, "Bytes sent in 1024 to 1522 byte packets");
8199 
8200 	SYSCTL_ADD_UINT(ctx, children, OID_AUTO,
8201 		"stat_EtherStatsPktsTx1523Octetsto9022Octets",
8202 		CTLFLAG_RD, &sc->stat_EtherStatsPktsTx1523Octetsto9022Octets,
8203 		0, "Bytes sent in 1523 to 9022 byte packets");
8204 
8205 	SYSCTL_ADD_UINT(ctx, children, OID_AUTO,
8206 		"stat_XonPauseFramesReceived",
8207 		CTLFLAG_RD, &sc->stat_XonPauseFramesReceived,
8208 		0, "XON pause frames receved");
8209 
8210 	SYSCTL_ADD_UINT(ctx, children, OID_AUTO,
8211 		"stat_XoffPauseFramesReceived",
8212 		CTLFLAG_RD, &sc->stat_XoffPauseFramesReceived,
8213 		0, "XOFF pause frames received");
8214 
8215 	SYSCTL_ADD_UINT(ctx, children, OID_AUTO,
8216 		"stat_OutXonSent",
8217 		CTLFLAG_RD, &sc->stat_OutXonSent,
8218 		0, "XON pause frames sent");
8219 
8220 	SYSCTL_ADD_UINT(ctx, children, OID_AUTO,
8221 		"stat_OutXoffSent",
8222 		CTLFLAG_RD, &sc->stat_OutXoffSent,
8223 		0, "XOFF pause frames sent");
8224 
8225 	SYSCTL_ADD_UINT(ctx, children, OID_AUTO,
8226 		"stat_FlowControlDone",
8227 		CTLFLAG_RD, &sc->stat_FlowControlDone,
8228 		0, "Flow control done");
8229 
8230 	SYSCTL_ADD_UINT(ctx, children, OID_AUTO,
8231 		"stat_MacControlFramesReceived",
8232 		CTLFLAG_RD, &sc->stat_MacControlFramesReceived,
8233 		0, "MAC control frames received");
8234 
8235 	SYSCTL_ADD_UINT(ctx, children, OID_AUTO,
8236 		"stat_XoffStateEntered",
8237 		CTLFLAG_RD, &sc->stat_XoffStateEntered,
8238 		0, "XOFF state entered");
8239 
8240 	SYSCTL_ADD_UINT(ctx, children, OID_AUTO,
8241 		"stat_IfInFramesL2FilterDiscards",
8242 		CTLFLAG_RD, &sc->stat_IfInFramesL2FilterDiscards,
8243 		0, "Received L2 packets discarded");
8244 
8245 	SYSCTL_ADD_UINT(ctx, children, OID_AUTO,
8246 		"stat_IfInRuleCheckerDiscards",
8247 		CTLFLAG_RD, &sc->stat_IfInRuleCheckerDiscards,
8248 		0, "Received packets discarded by rule");
8249 
8250 	SYSCTL_ADD_UINT(ctx, children, OID_AUTO,
8251 		"stat_IfInFTQDiscards",
8252 		CTLFLAG_RD, &sc->stat_IfInFTQDiscards,
8253 		0, "Received packet FTQ discards");
8254 
8255 	SYSCTL_ADD_UINT(ctx, children, OID_AUTO,
8256 		"stat_IfInMBUFDiscards",
8257 		CTLFLAG_RD, &sc->stat_IfInMBUFDiscards,
8258 		0, "Received packets discarded due to lack of controller buffer memory");
8259 
8260 	SYSCTL_ADD_UINT(ctx, children, OID_AUTO,
8261 		"stat_IfInRuleCheckerP4Hit",
8262 		CTLFLAG_RD, &sc->stat_IfInRuleCheckerP4Hit,
8263 		0, "Received packets rule checker hits");
8264 
8265 	SYSCTL_ADD_UINT(ctx, children, OID_AUTO,
8266 		"stat_CatchupInRuleCheckerDiscards",
8267 		CTLFLAG_RD, &sc->stat_CatchupInRuleCheckerDiscards,
8268 		0, "Received packets discarded in Catchup path");
8269 
8270 	SYSCTL_ADD_UINT(ctx, children, OID_AUTO,
8271 		"stat_CatchupInFTQDiscards",
8272 		CTLFLAG_RD, &sc->stat_CatchupInFTQDiscards,
8273 		0, "Received packets discarded in FTQ in Catchup path");
8274 
8275 	SYSCTL_ADD_UINT(ctx, children, OID_AUTO,
8276 		"stat_CatchupInMBUFDiscards",
8277 		CTLFLAG_RD, &sc->stat_CatchupInMBUFDiscards,
8278 		0, "Received packets discarded in controller buffer memory in Catchup path");
8279 
8280 	SYSCTL_ADD_UINT(ctx, children, OID_AUTO,
8281 		"stat_CatchupInRuleCheckerP4Hit",
8282 		CTLFLAG_RD, &sc->stat_CatchupInRuleCheckerP4Hit,
8283 		0, "Received packets rule checker hits in Catchup path");
8284 
8285 	SYSCTL_ADD_UINT(ctx, children, OID_AUTO,
8286 		"com_no_buffers",
8287 		CTLFLAG_RD, &sc->com_no_buffers,
8288 		0, "Valid packets received but no RX buffers available");
8289 
8290 #ifdef BCE_DEBUG
8291 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO,
8292 		"driver_state", CTLTYPE_INT | CTLFLAG_RW,
8293 		(void *)sc, 0,
8294 		bce_sysctl_driver_state, "I", "Drive state information");
8295 
8296 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO,
8297 		"hw_state", CTLTYPE_INT | CTLFLAG_RW,
8298 		(void *)sc, 0,
8299 		bce_sysctl_hw_state, "I", "Hardware state information");
8300 
8301 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO,
8302 		"bc_state", CTLTYPE_INT | CTLFLAG_RW,
8303 		(void *)sc, 0,
8304 		bce_sysctl_bc_state, "I", "Bootcode state information");
8305 
8306 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO,
8307 		"dump_rx_chain", CTLTYPE_INT | CTLFLAG_RW,
8308 		(void *)sc, 0,
8309 		bce_sysctl_dump_rx_chain, "I", "Dump rx_bd chain");
8310 
8311 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO,
8312 		"dump_tx_chain", CTLTYPE_INT | CTLFLAG_RW,
8313 		(void *)sc, 0,
8314 		bce_sysctl_dump_tx_chain, "I", "Dump tx_bd chain");
8315 
8316 #ifdef ZERO_COPY_SOCKETS
8317 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO,
8318 		"dump_pg_chain", CTLTYPE_INT | CTLFLAG_RW,
8319 		(void *)sc, 0,
8320 		bce_sysctl_dump_pg_chain, "I", "Dump page chain");
8321 #endif
8322 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO,
8323 		"dump_ctx", CTLTYPE_INT | CTLFLAG_RW,
8324 		(void *)sc, 0,
8325 		bce_sysctl_dump_ctx, "I", "Dump context memory");
8326 
8327 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO,
8328 		"breakpoint", CTLTYPE_INT | CTLFLAG_RW,
8329 		(void *)sc, 0,
8330 		bce_sysctl_breakpoint, "I", "Driver breakpoint");
8331 
8332 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO,
8333 		"reg_read", CTLTYPE_INT | CTLFLAG_RW,
8334 		(void *)sc, 0,
8335 		bce_sysctl_reg_read, "I", "Register read");
8336 
8337 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO,
8338 		"nvram_read", CTLTYPE_INT | CTLFLAG_RW,
8339 		(void *)sc, 0,
8340 		bce_sysctl_nvram_read, "I", "NVRAM read");
8341 
8342 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO,
8343 		"phy_read", CTLTYPE_INT | CTLFLAG_RW,
8344 		(void *)sc, 0,
8345 		bce_sysctl_phy_read, "I", "PHY register read");
8346 
8347 #endif
8348 
8349 	DBEXIT(BCE_VERBOSE_MISC);
8350 }
8351 
8352 
8353 /****************************************************************************/
8354 /* BCE Debug Routines                                                       */
8355 /****************************************************************************/
8356 #ifdef BCE_DEBUG
8357 
8358 /****************************************************************************/
8359 /* Freezes the controller to allow for a cohesive state dump.               */
8360 /*                                                                          */
8361 /* Returns:                                                                 */
8362 /*   Nothing.                                                               */
8363 /****************************************************************************/
8364 static void
8365 bce_freeze_controller(struct bce_softc *sc)
8366 {
8367 	u32 val;
8368 	val = REG_RD(sc, BCE_MISC_COMMAND);
8369 	val |= BCE_MISC_COMMAND_DISABLE_ALL;
8370 	REG_WR(sc, BCE_MISC_COMMAND, val);
8371 }
8372 
8373 
8374 /****************************************************************************/
8375 /* Unfreezes the controller after a freeze operation.  This may not always  */
8376 /* work and the controller will require a reset!                            */
8377 /*                                                                          */
8378 /* Returns:                                                                 */
8379 /*   Nothing.                                                               */
8380 /****************************************************************************/
8381 static void
8382 bce_unfreeze_controller(struct bce_softc *sc)
8383 {
8384 	u32 val;
8385 	val = REG_RD(sc, BCE_MISC_COMMAND);
8386 	val |= BCE_MISC_COMMAND_ENABLE_ALL;
8387 	REG_WR(sc, BCE_MISC_COMMAND, val);
8388 }
8389 
8390 
8391 /****************************************************************************/
8392 /* Prints out Ethernet frame information from an mbuf.                      */
8393 /*                                                                          */
8394 /* Partially decode an Ethernet frame to look at some important headers.    */
8395 /*                                                                          */
8396 /* Returns:                                                                 */
8397 /*   Nothing.                                                               */
8398 /****************************************************************************/
8399 static void
8400 bce_dump_enet(struct bce_softc *sc, struct mbuf *m)
8401 {
8402 	struct ether_vlan_header *eh;
8403 	u16 etype;
8404 	int ehlen;
8405 	struct ip *ip;
8406 	struct tcphdr *th;
8407 	struct udphdr *uh;
8408 	struct arphdr *ah;
8409 
8410 		BCE_PRINTF(
8411 			"-----------------------------"
8412 			" Frame Decode "
8413 			"-----------------------------\n");
8414 
8415 	eh = mtod(m, struct ether_vlan_header *);
8416 
8417 	/* Handle VLAN encapsulation if present. */
8418 	if (eh->evl_encap_proto == htons(ETHERTYPE_VLAN)) {
8419 		etype = ntohs(eh->evl_proto);
8420 		ehlen = ETHER_HDR_LEN + ETHER_VLAN_ENCAP_LEN;
8421 	} else {
8422 		etype = ntohs(eh->evl_encap_proto);
8423 		ehlen = ETHER_HDR_LEN;
8424 	}
8425 
8426 	/* ToDo: Add VLAN output. */
8427 	BCE_PRINTF("enet: dest = %6D, src = %6D, type = 0x%04X, hlen = %d\n",
8428 		eh->evl_dhost, ":", eh->evl_shost, ":", etype, ehlen);
8429 
8430 	switch (etype) {
8431 		case ETHERTYPE_IP:
8432 			ip = (struct ip *)(m->m_data + ehlen);
8433 			BCE_PRINTF("--ip: dest = 0x%08X , src = 0x%08X, len = %d bytes, "
8434 				"protocol = 0x%02X, xsum = 0x%04X\n",
8435 				ntohl(ip->ip_dst.s_addr), ntohl(ip->ip_src.s_addr),
8436 				ntohs(ip->ip_len), ip->ip_p, ntohs(ip->ip_sum));
8437 
8438 			switch (ip->ip_p) {
8439 				case IPPROTO_TCP:
8440 					th = (struct tcphdr *)((caddr_t)ip + (ip->ip_hl << 2));
8441 					BCE_PRINTF("-tcp: dest = %d, src = %d, hlen = %d bytes, "
8442 						"flags = 0x%b, csum = 0x%04X\n",
8443 						ntohs(th->th_dport), ntohs(th->th_sport), (th->th_off << 2),
8444 						th->th_flags, "\20\10CWR\07ECE\06URG\05ACK\04PSH\03RST\02SYN\01FIN",
8445 						ntohs(th->th_sum));
8446 					break;
8447 				case IPPROTO_UDP:
8448         		    uh = (struct udphdr *)((caddr_t)ip + (ip->ip_hl << 2));
8449 					BCE_PRINTF("-udp: dest = %d, src = %d, len = %d bytes, "
8450 						"csum = 0x%04X\n", ntohs(uh->uh_dport), ntohs(uh->uh_sport),
8451 						ntohs(uh->uh_ulen), ntohs(uh->uh_sum));
8452 					break;
8453 				case IPPROTO_ICMP:
8454 					BCE_PRINTF("icmp:\n");
8455 					break;
8456 				default:
8457 					BCE_PRINTF("----: Other IP protocol.\n");
8458 			}
8459 			break;
8460 		case ETHERTYPE_IPV6:
8461 			BCE_PRINTF("ipv6: No decode supported.\n");
8462 			break;
8463 		case ETHERTYPE_ARP:
8464 			BCE_PRINTF("-arp: ");
8465 			ah = (struct arphdr *) (m->m_data + ehlen);
8466 			switch (ntohs(ah->ar_op)) {
8467 				case ARPOP_REVREQUEST:
8468 					printf("reverse ARP request\n");
8469 					break;
8470 				case ARPOP_REVREPLY:
8471 					printf("reverse ARP reply\n");
8472 					break;
8473 				case ARPOP_REQUEST:
8474 					printf("ARP request\n");
8475 					break;
8476 				case ARPOP_REPLY:
8477 					printf("ARP reply\n");
8478 					break;
8479 				default:
8480 					printf("other ARP operation\n");
8481 			}
8482 			break;
8483 		default:
8484 			BCE_PRINTF("----: Other protocol.\n");
8485 	}
8486 
8487 	BCE_PRINTF(
8488 		"-----------------------------"
8489 		"--------------"
8490 		"-----------------------------\n");
8491 }
8492 
8493 
8494 /****************************************************************************/
8495 /* Prints out information about an mbuf.                                    */
8496 /*                                                                          */
8497 /* Returns:                                                                 */
8498 /*   Nothing.                                                               */
8499 /****************************************************************************/
8500 static __attribute__ ((noinline)) void
8501 bce_dump_mbuf(struct bce_softc *sc, struct mbuf *m)
8502 {
8503 	struct mbuf *mp = m;
8504 
8505 	if (m == NULL) {
8506 		BCE_PRINTF("mbuf: null pointer\n");
8507 		return;
8508 	}
8509 
8510 	while (mp) {
8511 		BCE_PRINTF("mbuf: %p, m_len = %d, m_flags = 0x%b, m_data = %p\n",
8512 			mp, mp->m_len, mp->m_flags,
8513 			"\20\1M_EXT\2M_PKTHDR\3M_EOR\4M_RDONLY",
8514 			mp->m_data);
8515 
8516 		if (mp->m_flags & M_PKTHDR) {
8517 			BCE_PRINTF("- m_pkthdr: len = %d, flags = 0x%b, csum_flags = %b\n",
8518 				mp->m_pkthdr.len, mp->m_flags,
8519 				"\20\12M_BCAST\13M_MCAST\14M_FRAG\15M_FIRSTFRAG"
8520 				"\16M_LASTFRAG\21M_VLANTAG\22M_PROMISC\23M_NOFREE",
8521 				mp->m_pkthdr.csum_flags,
8522 				"\20\1CSUM_IP\2CSUM_TCP\3CSUM_UDP\4CSUM_IP_FRAGS"
8523 				"\5CSUM_FRAGMENT\6CSUM_TSO\11CSUM_IP_CHECKED"
8524 				"\12CSUM_IP_VALID\13CSUM_DATA_VALID\14CSUM_PSEUDO_HDR");
8525 		}
8526 
8527 		if (mp->m_flags & M_EXT) {
8528 			BCE_PRINTF("- m_ext: %p, ext_size = %d, type = ",
8529 				mp->m_ext.ext_buf, mp->m_ext.ext_size);
8530 			switch (mp->m_ext.ext_type) {
8531 				case EXT_CLUSTER:    printf("EXT_CLUSTER\n"); break;
8532 				case EXT_SFBUF:      printf("EXT_SFBUF\n"); break;
8533 				case EXT_JUMBO9:     printf("EXT_JUMBO9\n"); break;
8534 				case EXT_JUMBO16:    printf("EXT_JUMBO16\n"); break;
8535 				case EXT_PACKET:     printf("EXT_PACKET\n"); break;
8536 				case EXT_MBUF:       printf("EXT_MBUF\n"); break;
8537 				case EXT_NET_DRV:    printf("EXT_NET_DRV\n"); break;
8538 				case EXT_MOD_TYPE:   printf("EXT_MDD_TYPE\n"); break;
8539 				case EXT_DISPOSABLE: printf("EXT_DISPOSABLE\n"); break;
8540 				case EXT_EXTREF:     printf("EXT_EXTREF\n"); break;
8541 				default:             printf("UNKNOWN\n");
8542 			}
8543 		}
8544 
8545 		mp = mp->m_next;
8546 	}
8547 }
8548 
8549 
8550 /****************************************************************************/
8551 /* Prints out the mbufs in the TX mbuf chain.                               */
8552 /*                                                                          */
8553 /* Returns:                                                                 */
8554 /*   Nothing.                                                               */
8555 /****************************************************************************/
8556 static __attribute__ ((noinline)) void
8557 bce_dump_tx_mbuf_chain(struct bce_softc *sc, u16 chain_prod, int count)
8558 {
8559 	struct mbuf *m;
8560 
8561 	BCE_PRINTF(
8562 		"----------------------------"
8563 		"  tx mbuf data  "
8564 		"----------------------------\n");
8565 
8566 	for (int i = 0; i < count; i++) {
8567 	 	m = sc->tx_mbuf_ptr[chain_prod];
8568 		BCE_PRINTF("txmbuf[0x%04X]\n", chain_prod);
8569 		bce_dump_mbuf(sc, m);
8570 		chain_prod = TX_CHAIN_IDX(NEXT_TX_BD(chain_prod));
8571 	}
8572 
8573 	BCE_PRINTF(
8574 		"----------------------------"
8575 		"----------------"
8576 		"----------------------------\n");
8577 }
8578 
8579 
8580 /****************************************************************************/
8581 /* Prints out the mbufs in the RX mbuf chain.                               */
8582 /*                                                                          */
8583 /* Returns:                                                                 */
8584 /*   Nothing.                                                               */
8585 /****************************************************************************/
8586 static __attribute__ ((noinline)) void
8587 bce_dump_rx_mbuf_chain(struct bce_softc *sc, u16 chain_prod, int count)
8588 {
8589 	struct mbuf *m;
8590 
8591 	BCE_PRINTF(
8592 		"----------------------------"
8593 		"  rx mbuf data  "
8594 		"----------------------------\n");
8595 
8596 	for (int i = 0; i < count; i++) {
8597 	 	m = sc->rx_mbuf_ptr[chain_prod];
8598 		BCE_PRINTF("rxmbuf[0x%04X]\n", chain_prod);
8599 		bce_dump_mbuf(sc, m);
8600 		chain_prod = RX_CHAIN_IDX(NEXT_RX_BD(chain_prod));
8601 	}
8602 
8603 
8604 	BCE_PRINTF(
8605 		"----------------------------"
8606 		"----------------"
8607 		"----------------------------\n");
8608 }
8609 
8610 
8611 #ifdef ZERO_COPY_SOCKETS
8612 /****************************************************************************/
8613 /* Prints out the mbufs in the mbuf page chain.                             */
8614 /*                                                                          */
8615 /* Returns:                                                                 */
8616 /*   Nothing.                                                               */
8617 /****************************************************************************/
8618 static __attribute__ ((noinline)) void
8619 bce_dump_pg_mbuf_chain(struct bce_softc *sc, u16 chain_prod, int count)
8620 {
8621 	struct mbuf *m;
8622 
8623 	BCE_PRINTF(
8624 		"----------------------------"
8625 		"  pg mbuf data  "
8626 		"----------------------------\n");
8627 
8628 	for (int i = 0; i < count; i++) {
8629 	 	m = sc->pg_mbuf_ptr[chain_prod];
8630 		BCE_PRINTF("pgmbuf[0x%04X]\n", chain_prod);
8631 		bce_dump_mbuf(sc, m);
8632 		chain_prod = PG_CHAIN_IDX(NEXT_PG_BD(chain_prod));
8633 	}
8634 
8635 
8636 	BCE_PRINTF(
8637 		"----------------------------"
8638 		"----------------"
8639 		"----------------------------\n");
8640 }
8641 #endif
8642 
8643 
8644 /****************************************************************************/
8645 /* Prints out a tx_bd structure.                                            */
8646 /*                                                                          */
8647 /* Returns:                                                                 */
8648 /*   Nothing.                                                               */
8649 /****************************************************************************/
8650 static __attribute__ ((noinline)) void
8651 bce_dump_txbd(struct bce_softc *sc, int idx, struct tx_bd *txbd)
8652 {
8653 	if (idx > MAX_TX_BD)
8654 		/* Index out of range. */
8655 		BCE_PRINTF("tx_bd[0x%04X]: Invalid tx_bd index!\n", idx);
8656 	else if ((idx & USABLE_TX_BD_PER_PAGE) == USABLE_TX_BD_PER_PAGE)
8657 		/* TX Chain page pointer. */
8658 		BCE_PRINTF("tx_bd[0x%04X]: haddr = 0x%08X:%08X, chain page pointer\n",
8659 			idx, txbd->tx_bd_haddr_hi, txbd->tx_bd_haddr_lo);
8660 	else {
8661 			/* Normal tx_bd entry. */
8662 			BCE_PRINTF("tx_bd[0x%04X]: haddr = 0x%08X:%08X, nbytes = 0x%08X, "
8663 				"vlan tag= 0x%04X, flags = 0x%04X (", idx,
8664 				txbd->tx_bd_haddr_hi, txbd->tx_bd_haddr_lo,
8665 				txbd->tx_bd_mss_nbytes, txbd->tx_bd_vlan_tag,
8666 				txbd->tx_bd_flags);
8667 
8668 			if (txbd->tx_bd_flags & TX_BD_FLAGS_CONN_FAULT)
8669 				printf(" CONN_FAULT");
8670 
8671 			if (txbd->tx_bd_flags & TX_BD_FLAGS_TCP_UDP_CKSUM)
8672 				printf(" TCP_UDP_CKSUM");
8673 
8674 			if (txbd->tx_bd_flags & TX_BD_FLAGS_IP_CKSUM)
8675 				printf(" IP_CKSUM");
8676 
8677 			if (txbd->tx_bd_flags & TX_BD_FLAGS_VLAN_TAG)
8678 				printf("  VLAN");
8679 
8680 			if (txbd->tx_bd_flags & TX_BD_FLAGS_COAL_NOW)
8681 				printf(" COAL_NOW");
8682 
8683 			if (txbd->tx_bd_flags & TX_BD_FLAGS_DONT_GEN_CRC)
8684 				printf(" DONT_GEN_CRC");
8685 
8686 			if (txbd->tx_bd_flags & TX_BD_FLAGS_START)
8687 				printf(" START");
8688 
8689 			if (txbd->tx_bd_flags & TX_BD_FLAGS_END)
8690 				printf(" END");
8691 
8692 			if (txbd->tx_bd_flags & TX_BD_FLAGS_SW_LSO)
8693 				printf(" LSO");
8694 
8695 			if (txbd->tx_bd_flags & TX_BD_FLAGS_SW_OPTION_WORD)
8696 				printf(" OPTION_WORD");
8697 
8698 			if (txbd->tx_bd_flags & TX_BD_FLAGS_SW_FLAGS)
8699 				printf(" FLAGS");
8700 
8701 			if (txbd->tx_bd_flags & TX_BD_FLAGS_SW_SNAP)
8702 				printf(" SNAP");
8703 
8704 			printf(" )\n");
8705 		}
8706 
8707 }
8708 
8709 
8710 /****************************************************************************/
8711 /* Prints out a rx_bd structure.                                            */
8712 /*                                                                          */
8713 /* Returns:                                                                 */
8714 /*   Nothing.                                                               */
8715 /****************************************************************************/
8716 static __attribute__ ((noinline)) void
8717 bce_dump_rxbd(struct bce_softc *sc, int idx, struct rx_bd *rxbd)
8718 {
8719 	if (idx > MAX_RX_BD)
8720 		/* Index out of range. */
8721 		BCE_PRINTF("rx_bd[0x%04X]: Invalid rx_bd index!\n", idx);
8722 	else if ((idx & USABLE_RX_BD_PER_PAGE) == USABLE_RX_BD_PER_PAGE)
8723 		/* RX Chain page pointer. */
8724 		BCE_PRINTF("rx_bd[0x%04X]: haddr = 0x%08X:%08X, chain page pointer\n",
8725 			idx, rxbd->rx_bd_haddr_hi, rxbd->rx_bd_haddr_lo);
8726 	else
8727 		/* Normal rx_bd entry. */
8728 		BCE_PRINTF("rx_bd[0x%04X]: haddr = 0x%08X:%08X, nbytes = 0x%08X, "
8729 			"flags = 0x%08X\n", idx,
8730 			rxbd->rx_bd_haddr_hi, rxbd->rx_bd_haddr_lo,
8731 			rxbd->rx_bd_len, rxbd->rx_bd_flags);
8732 }
8733 
8734 
8735 #ifdef ZERO_COPY_SOCKETS
8736 /****************************************************************************/
8737 /* Prints out a rx_bd structure in the page chain.                          */
8738 /*                                                                          */
8739 /* Returns:                                                                 */
8740 /*   Nothing.                                                               */
8741 /****************************************************************************/
8742 static __attribute__ ((noinline)) void
8743 bce_dump_pgbd(struct bce_softc *sc, int idx, struct rx_bd *pgbd)
8744 {
8745 	if (idx > MAX_PG_BD)
8746 		/* Index out of range. */
8747 		BCE_PRINTF("pg_bd[0x%04X]: Invalid pg_bd index!\n", idx);
8748 	else if ((idx & USABLE_PG_BD_PER_PAGE) == USABLE_PG_BD_PER_PAGE)
8749 		/* Page Chain page pointer. */
8750 		BCE_PRINTF("px_bd[0x%04X]: haddr = 0x%08X:%08X, chain page pointer\n",
8751 			idx, pgbd->rx_bd_haddr_hi, pgbd->rx_bd_haddr_lo);
8752 	else
8753 		/* Normal rx_bd entry. */
8754 		BCE_PRINTF("pg_bd[0x%04X]: haddr = 0x%08X:%08X, nbytes = 0x%08X, "
8755 			"flags = 0x%08X\n", idx,
8756 			pgbd->rx_bd_haddr_hi, pgbd->rx_bd_haddr_lo,
8757 			pgbd->rx_bd_len, pgbd->rx_bd_flags);
8758 }
8759 #endif
8760 
8761 
8762 /****************************************************************************/
8763 /* Prints out a l2_fhdr structure.                                          */
8764 /*                                                                          */
8765 /* Returns:                                                                 */
8766 /*   Nothing.                                                               */
8767 /****************************************************************************/
8768 static __attribute__ ((noinline)) void
8769 bce_dump_l2fhdr(struct bce_softc *sc, int idx, struct l2_fhdr *l2fhdr)
8770 {
8771 	BCE_PRINTF("l2_fhdr[0x%04X]: status = 0x%b, "
8772 		"pkt_len = %d, vlan = 0x%04x, ip_xsum/hdr_len = 0x%04X, "
8773 		"tcp_udp_xsum = 0x%04X\n", idx,
8774 		l2fhdr->l2_fhdr_status, BCE_L2FHDR_PRINTFB,
8775 		l2fhdr->l2_fhdr_pkt_len, l2fhdr->l2_fhdr_vlan_tag,
8776 		l2fhdr->l2_fhdr_ip_xsum, l2fhdr->l2_fhdr_tcp_udp_xsum);
8777 }
8778 
8779 
8780 /****************************************************************************/
8781 /* Prints out context memory info.  (Only useful for CID 0 to 16.)          */
8782 /*                                                                          */
8783 /* Returns:                                                                 */
8784 /*   Nothing.                                                               */
8785 /****************************************************************************/
8786 static __attribute__ ((noinline)) void
8787 bce_dump_ctx(struct bce_softc *sc, u16 cid)
8788 {
8789 	if (cid <= TX_CID) {
8790 		BCE_PRINTF(
8791 			"----------------------------"
8792 			"    CTX Data    "
8793 			"----------------------------\n");
8794 
8795 		BCE_PRINTF("     0x%04X - (CID) Context ID\n", cid);
8796 
8797 		if (cid == RX_CID) {
8798 			BCE_PRINTF(" 0x%08X - (L2CTX_RX_HOST_BDIDX) host rx "
8799 				"producer index\n",
8800 				CTX_RD(sc, GET_CID_ADDR(cid), BCE_L2CTX_RX_HOST_BDIDX));
8801 			BCE_PRINTF(" 0x%08X - (L2CTX_RX_HOST_BSEQ) host byte sequence\n",
8802 				CTX_RD(sc, GET_CID_ADDR(cid), BCE_L2CTX_RX_HOST_BSEQ));
8803 			BCE_PRINTF(" 0x%08X - (L2CTX_RX_NX_BSEQ) h/w byte sequence\n",
8804 				CTX_RD(sc, GET_CID_ADDR(cid), BCE_L2CTX_RX_NX_BSEQ));
8805 			BCE_PRINTF(" 0x%08X - (L2CTX_RX_NX_BDHADDR_HI) h/w buffer "
8806 				"descriptor address\n",
8807  				CTX_RD(sc, GET_CID_ADDR(cid), BCE_L2CTX_RX_NX_BDHADDR_HI));
8808 			BCE_PRINTF(" 0x%08X - (L2CTX_RX_NX_BDHADDR_LO) h/w buffer "
8809 				"descriptor address\n",
8810 				CTX_RD(sc, GET_CID_ADDR(cid), BCE_L2CTX_RX_NX_BDHADDR_LO));
8811 			BCE_PRINTF(" 0x%08X - (L2CTX_RX_NX_BDIDX) h/w rx consumer index\n",
8812 				CTX_RD(sc, GET_CID_ADDR(cid), BCE_L2CTX_RX_NX_BDIDX));
8813 			BCE_PRINTF(" 0x%08X - (L2CTX_RX_HOST_PG_BDIDX) host page "
8814 				"producer index\n",
8815 				CTX_RD(sc, GET_CID_ADDR(cid), BCE_L2CTX_RX_HOST_PG_BDIDX));
8816 			BCE_PRINTF(" 0x%08X - (L2CTX_RX_PG_BUF_SIZE) host rx_bd/page "
8817 				"buffer size\n",
8818 				CTX_RD(sc, GET_CID_ADDR(cid), BCE_L2CTX_RX_PG_BUF_SIZE));
8819 			BCE_PRINTF(" 0x%08X - (L2CTX_RX_NX_PG_BDHADDR_HI) h/w page "
8820 				"chain address\n",
8821 				CTX_RD(sc, GET_CID_ADDR(cid), BCE_L2CTX_RX_NX_PG_BDHADDR_HI));
8822 			BCE_PRINTF(" 0x%08X - (L2CTX_RX_NX_PG_BDHADDR_LO) h/w page "
8823 				"chain address\n",
8824 				CTX_RD(sc, GET_CID_ADDR(cid), BCE_L2CTX_RX_NX_PG_BDHADDR_LO));
8825 			BCE_PRINTF(" 0x%08X - (L2CTX_RX_NX_PG_BDIDX) h/w page "
8826 				"consumer index\n",
8827 				CTX_RD(sc, GET_CID_ADDR(cid), BCE_L2CTX_RX_NX_PG_BDIDX));
8828 		} else if (cid == TX_CID) {
8829 			if ((BCE_CHIP_NUM(sc) == BCE_CHIP_NUM_5709) ||
8830 				(BCE_CHIP_NUM(sc) == BCE_CHIP_NUM_5716)) {
8831 				BCE_PRINTF(" 0x%08X - (L2CTX_TX_TYPE_XI) ctx type\n",
8832 					CTX_RD(sc, GET_CID_ADDR(cid), BCE_L2CTX_TX_TYPE_XI));
8833 				BCE_PRINTF(" 0x%08X - (L2CTX_CMD_TX_TYPE_XI) ctx cmd\n",
8834 					CTX_RD(sc, GET_CID_ADDR(cid), BCE_L2CTX_TX_CMD_TYPE_XI));
8835 				BCE_PRINTF(" 0x%08X - (L2CTX_TX_TBDR_BDHADDR_HI_XI) h/w buffer "
8836 					"descriptor address\n",	CTX_RD(sc,
8837 					GET_CID_ADDR(cid), BCE_L2CTX_TX_TBDR_BHADDR_HI_XI));
8838 				BCE_PRINTF(" 0x%08X - (L2CTX_TX_TBDR_BHADDR_LO_XI) h/w buffer "
8839 					"descriptor address\n", CTX_RD(sc,
8840 					GET_CID_ADDR(cid), BCE_L2CTX_TX_TBDR_BHADDR_LO_XI));
8841 				BCE_PRINTF(" 0x%08X - (L2CTX_TX_HOST_BIDX_XI) host producer "
8842 					"index\n", CTX_RD(sc, GET_CID_ADDR(cid),
8843 					BCE_L2CTX_TX_HOST_BIDX_XI));
8844 				BCE_PRINTF(" 0x%08X - (L2CTX_TX_HOST_BSEQ_XI) host byte "
8845 					"sequence\n", CTX_RD(sc, GET_CID_ADDR(cid),
8846 					BCE_L2CTX_TX_HOST_BSEQ_XI));
8847 			} else {
8848 				BCE_PRINTF(" 0x%08X - (L2CTX_TX_TYPE) ctx type\n",
8849 					CTX_RD(sc, GET_CID_ADDR(cid), BCE_L2CTX_TX_TYPE));
8850 				BCE_PRINTF(" 0x%08X - (L2CTX_TX_CMD_TYPE) ctx cmd\n",
8851 					CTX_RD(sc, GET_CID_ADDR(cid), BCE_L2CTX_TX_CMD_TYPE));
8852 				BCE_PRINTF(" 0x%08X - (L2CTX_TX_TBDR_BDHADDR_HI) h/w buffer "
8853 					"descriptor address\n", CTX_RD(sc, GET_CID_ADDR(cid),
8854 					BCE_L2CTX_TX_TBDR_BHADDR_HI));
8855 				BCE_PRINTF(" 0x%08X - (L2CTX_TX_TBDR_BHADDR_LO) h/w buffer "
8856 					"descriptor address\n", CTX_RD(sc, GET_CID_ADDR(cid),
8857 					BCE_L2CTX_TX_TBDR_BHADDR_LO));
8858 				BCE_PRINTF(" 0x%08X - (L2CTX_TX_HOST_BIDX) host producer "
8859 					"index\n", CTX_RD(sc, GET_CID_ADDR(cid),
8860 					BCE_L2CTX_TX_HOST_BIDX));
8861 				BCE_PRINTF(" 0x%08X - (L2CTX_TX_HOST_BSEQ) host byte "
8862 					"sequence\n", CTX_RD(sc, GET_CID_ADDR(cid),
8863 					BCE_L2CTX_TX_HOST_BSEQ));
8864 			}
8865 		} else
8866 			BCE_PRINTF(" Unknown CID\n");
8867 
8868 		BCE_PRINTF(
8869 			"----------------------------"
8870 			"    Raw CTX     "
8871 			"----------------------------\n");
8872 
8873 		for (int i = 0x0; i < 0x300; i += 0x10) {
8874 			BCE_PRINTF("0x%04X: 0x%08X 0x%08X 0x%08X 0x%08X\n", i,
8875 				CTX_RD(sc, GET_CID_ADDR(cid), i),
8876 				CTX_RD(sc, GET_CID_ADDR(cid), i + 0x4),
8877 				CTX_RD(sc, GET_CID_ADDR(cid), i + 0x8),
8878 				CTX_RD(sc, GET_CID_ADDR(cid), i + 0xc));
8879 		}
8880 
8881 
8882 		BCE_PRINTF(
8883 			"----------------------------"
8884 			"----------------"
8885 			"----------------------------\n");
8886 	}
8887 }
8888 
8889 
8890 /****************************************************************************/
8891 /* Prints out the FTQ data.                                                 */
8892 /*                                                                          */
8893 /* Returns:                                                                */
8894 /*   Nothing.                                                               */
8895 /****************************************************************************/
8896 static __attribute__ ((noinline)) void
8897 bce_dump_ftqs(struct bce_softc *sc)
8898 {
8899 	u32 cmd, ctl, cur_depth, max_depth, valid_cnt, val;
8900 
8901 	BCE_PRINTF(
8902 		"----------------------------"
8903 		"    FTQ Data    "
8904 		"----------------------------\n");
8905 
8906 	BCE_PRINTF("   FTQ    Command    Control   Depth_Now  Max_Depth  Valid_Cnt \n");
8907 	BCE_PRINTF(" ------- ---------- ---------- ---------- ---------- ----------\n");
8908 
8909 	/* Setup the generic statistic counters for the FTQ valid count. */
8910 	val = (BCE_HC_STAT_GEN_SEL_0_GEN_SEL_0_RV2PPQ_VALID_CNT << 24) |
8911 		(BCE_HC_STAT_GEN_SEL_0_GEN_SEL_0_RXPCQ_VALID_CNT  << 16) |
8912 		(BCE_HC_STAT_GEN_SEL_0_GEN_SEL_0_RXPQ_VALID_CNT   <<  8) |
8913 		(BCE_HC_STAT_GEN_SEL_0_GEN_SEL_0_RLUPQ_VALID_CNT);
8914 	REG_WR(sc, BCE_HC_STAT_GEN_SEL_0, val);
8915 
8916 	val = (BCE_HC_STAT_GEN_SEL_0_GEN_SEL_0_TSCHQ_VALID_CNT  << 24) |
8917 		(BCE_HC_STAT_GEN_SEL_0_GEN_SEL_0_RDMAQ_VALID_CNT  << 16) |
8918 		(BCE_HC_STAT_GEN_SEL_0_GEN_SEL_0_RV2PTQ_VALID_CNT <<  8) |
8919 		(BCE_HC_STAT_GEN_SEL_0_GEN_SEL_0_RV2PMQ_VALID_CNT);
8920 	REG_WR(sc, BCE_HC_STAT_GEN_SEL_1, val);
8921 
8922 	val = (BCE_HC_STAT_GEN_SEL_0_GEN_SEL_0_TPATQ_VALID_CNT  << 24) |
8923 		(BCE_HC_STAT_GEN_SEL_0_GEN_SEL_0_TDMAQ_VALID_CNT  << 16) |
8924 		(BCE_HC_STAT_GEN_SEL_0_GEN_SEL_0_TXPQ_VALID_CNT   <<  8) |
8925 		(BCE_HC_STAT_GEN_SEL_0_GEN_SEL_0_TBDRQ_VALID_CNT);
8926 	REG_WR(sc, BCE_HC_STAT_GEN_SEL_2, val);
8927 
8928 	val = (BCE_HC_STAT_GEN_SEL_0_GEN_SEL_0_COMQ_VALID_CNT   << 24) |
8929 		(BCE_HC_STAT_GEN_SEL_0_GEN_SEL_0_COMTQ_VALID_CNT  << 16) |
8930 		(BCE_HC_STAT_GEN_SEL_0_GEN_SEL_0_COMXQ_VALID_CNT  <<  8) |
8931 		(BCE_HC_STAT_GEN_SEL_0_GEN_SEL_0_TASQ_VALID_CNT);
8932 	REG_WR(sc, BCE_HC_STAT_GEN_SEL_3, val);
8933 
8934 	/* Input queue to the Receive Lookup state machine */
8935 	cmd = REG_RD(sc, BCE_RLUP_FTQ_CMD);
8936 	ctl = REG_RD(sc, BCE_RLUP_FTQ_CTL);
8937 	cur_depth = (ctl & BCE_RLUP_FTQ_CTL_CUR_DEPTH) >> 22;
8938 	max_depth = (ctl & BCE_RLUP_FTQ_CTL_MAX_DEPTH) >> 12;
8939 	valid_cnt = REG_RD(sc, BCE_HC_STAT_GEN_STAT0);
8940 	BCE_PRINTF(" RLUP    0x%08X 0x%08X 0x%08X 0x%08X 0x%08X\n",
8941 		cmd, ctl, cur_depth, max_depth, valid_cnt);
8942 
8943 	/* Input queue to the Receive Processor */
8944 	cmd = REG_RD_IND(sc, BCE_RXP_FTQ_CMD);
8945 	ctl = REG_RD_IND(sc, BCE_RXP_FTQ_CTL);
8946 	cur_depth = (ctl & BCE_RXP_FTQ_CTL_CUR_DEPTH) >> 22;
8947 	max_depth = (ctl & BCE_RXP_FTQ_CTL_MAX_DEPTH) >> 12;
8948 	valid_cnt = REG_RD(sc, BCE_HC_STAT_GEN_STAT1);
8949 	BCE_PRINTF(" RXP     0x%08X 0x%08X 0x%08X 0x%08X 0x%08X\n",
8950 		cmd, ctl, cur_depth, max_depth, valid_cnt);
8951 
8952 	/* Input queue to the Recevie Processor */
8953 	cmd = REG_RD_IND(sc, BCE_RXP_CFTQ_CMD);
8954 	ctl = REG_RD_IND(sc, BCE_RXP_CFTQ_CTL);
8955 	cur_depth = (ctl & BCE_RXP_CFTQ_CTL_CUR_DEPTH) >> 22;
8956 	max_depth = (ctl & BCE_RXP_CFTQ_CTL_MAX_DEPTH) >> 12;
8957 	valid_cnt = REG_RD(sc, BCE_HC_STAT_GEN_STAT2);
8958 	BCE_PRINTF(" RXPC    0x%08X 0x%08X 0x%08X 0x%08X 0x%08X\n",
8959 		cmd, ctl, cur_depth, max_depth, valid_cnt);
8960 
8961 	/* Input queue to the Receive Virtual to Physical state machine */
8962 	cmd = REG_RD(sc, BCE_RV2P_PFTQ_CMD);
8963 	ctl = REG_RD(sc, BCE_RV2P_PFTQ_CTL);
8964 	cur_depth = (ctl & BCE_RV2P_PFTQ_CTL_CUR_DEPTH) >> 22;
8965 	max_depth = (ctl & BCE_RV2P_PFTQ_CTL_MAX_DEPTH) >> 12;
8966 	valid_cnt = REG_RD(sc, BCE_HC_STAT_GEN_STAT3);
8967 	BCE_PRINTF(" RV2PP   0x%08X 0x%08X 0x%08X 0x%08X 0x%08X\n",
8968 		cmd, ctl, cur_depth, max_depth, valid_cnt);
8969 
8970 	/* Input queue to the Recevie Virtual to Physical state machine */
8971 	cmd = REG_RD(sc, BCE_RV2P_MFTQ_CMD);
8972 	ctl = REG_RD(sc, BCE_RV2P_MFTQ_CTL);
8973 	cur_depth = (ctl & BCE_RV2P_MFTQ_CTL_CUR_DEPTH) >> 22;
8974 	max_depth = (ctl & BCE_RV2P_MFTQ_CTL_MAX_DEPTH) >> 12;
8975 	valid_cnt = REG_RD(sc, BCE_HC_STAT_GEN_STAT4);
8976 	BCE_PRINTF(" RV2PM   0x%08X 0x%08X 0x%08X 0x%08X 0x%08X\n",
8977 		cmd, ctl, cur_depth, max_depth, valid_cnt);
8978 
8979 	/* Input queue to the Receive Virtual to Physical state machine */
8980 	cmd = REG_RD(sc, BCE_RV2P_TFTQ_CMD);
8981 	ctl = REG_RD(sc, BCE_RV2P_TFTQ_CTL);
8982 	cur_depth = (ctl & BCE_RV2P_TFTQ_CTL_CUR_DEPTH) >> 22;
8983 	max_depth = (ctl & BCE_RV2P_TFTQ_CTL_MAX_DEPTH) >> 12;
8984 	valid_cnt = REG_RD(sc, BCE_HC_STAT_GEN_STAT5);
8985 	BCE_PRINTF(" RV2PT   0x%08X 0x%08X 0x%08X 0x%08X 0x%08X\n",
8986 		cmd, ctl, cur_depth, max_depth, valid_cnt);
8987 
8988 	/* Input queue to the Receive DMA state machine */
8989 	cmd = REG_RD(sc, BCE_RDMA_FTQ_CMD);
8990 	ctl = REG_RD(sc, BCE_RDMA_FTQ_CTL);
8991 	cur_depth = (ctl & BCE_RDMA_FTQ_CTL_CUR_DEPTH) >> 22;
8992 	max_depth = (ctl & BCE_RDMA_FTQ_CTL_MAX_DEPTH) >> 12;
8993 	valid_cnt = REG_RD(sc, BCE_HC_STAT_GEN_STAT6);
8994 	BCE_PRINTF(" RDMA    0x%08X 0x%08X 0x%08X 0x%08X 0x%08X\n",
8995 		cmd, ctl, cur_depth, max_depth, valid_cnt);
8996 
8997 	/* Input queue to the Transmit Scheduler state machine */
8998 	cmd = REG_RD(sc, BCE_TSCH_FTQ_CMD);
8999 	ctl = REG_RD(sc, BCE_TSCH_FTQ_CTL);
9000 	cur_depth = (ctl & BCE_TSCH_FTQ_CTL_CUR_DEPTH) >> 22;
9001 	max_depth = (ctl & BCE_TSCH_FTQ_CTL_MAX_DEPTH) >> 12;
9002 	valid_cnt = REG_RD(sc, BCE_HC_STAT_GEN_STAT7);
9003 	BCE_PRINTF(" TSCH    0x%08X 0x%08X 0x%08X 0x%08X 0x%08X\n",
9004 		cmd, ctl, cur_depth, max_depth, valid_cnt);
9005 
9006 	/* Input queue to the Transmit Buffer Descriptor state machine */
9007 	cmd = REG_RD(sc, BCE_TBDR_FTQ_CMD);
9008 	ctl = REG_RD(sc, BCE_TBDR_FTQ_CTL);
9009 	cur_depth = (ctl & BCE_TBDR_FTQ_CTL_CUR_DEPTH) >> 22;
9010 	max_depth = (ctl & BCE_TBDR_FTQ_CTL_MAX_DEPTH) >> 12;
9011 	valid_cnt = REG_RD(sc, BCE_HC_STAT_GEN_STAT8);
9012 	BCE_PRINTF(" TBDR    0x%08X 0x%08X 0x%08X 0x%08X 0x%08X\n",
9013 		cmd, ctl, cur_depth, max_depth, valid_cnt);
9014 
9015 	/* Input queue to the Transmit Processor */
9016 	cmd = REG_RD_IND(sc, BCE_TXP_FTQ_CMD);
9017 	ctl = REG_RD_IND(sc, BCE_TXP_FTQ_CTL);
9018 	cur_depth = (ctl & BCE_TXP_FTQ_CTL_CUR_DEPTH) >> 22;
9019 	max_depth = (ctl & BCE_TXP_FTQ_CTL_MAX_DEPTH) >> 12;
9020 	valid_cnt = REG_RD(sc, BCE_HC_STAT_GEN_STAT9);
9021 	BCE_PRINTF(" TXP     0x%08X 0x%08X 0x%08X 0x%08X 0x%08X\n",
9022 		cmd, ctl, cur_depth, max_depth, valid_cnt);
9023 
9024 	/* Input queue to the Transmit DMA state machine */
9025 	cmd = REG_RD(sc, BCE_TDMA_FTQ_CMD);
9026 	ctl = REG_RD(sc, BCE_TDMA_FTQ_CTL);
9027 	cur_depth = (ctl & BCE_TDMA_FTQ_CTL_CUR_DEPTH) >> 22;
9028 	max_depth = (ctl & BCE_TDMA_FTQ_CTL_MAX_DEPTH) >> 12;
9029 	valid_cnt = REG_RD(sc, BCE_HC_STAT_GEN_STAT10);
9030 	BCE_PRINTF(" TDMA    0x%08X 0x%08X 0x%08X 0x%08X 0x%08X\n",
9031 		cmd, ctl, cur_depth, max_depth, valid_cnt);
9032 
9033 	/* Input queue to the Transmit Patch-Up Processor */
9034 	cmd = REG_RD_IND(sc, BCE_TPAT_FTQ_CMD);
9035 	ctl = REG_RD_IND(sc, BCE_TPAT_FTQ_CTL);
9036 	cur_depth = (ctl & BCE_TPAT_FTQ_CTL_CUR_DEPTH) >> 22;
9037 	max_depth = (ctl & BCE_TPAT_FTQ_CTL_MAX_DEPTH) >> 12;
9038 	valid_cnt = REG_RD(sc, BCE_HC_STAT_GEN_STAT11);
9039 	BCE_PRINTF(" TPAT    0x%08X 0x%08X 0x%08X 0x%08X 0x%08X\n",
9040 		cmd, ctl, cur_depth, max_depth, valid_cnt);
9041 
9042 	/* Input queue to the Transmit Assembler state machine */
9043 	cmd = REG_RD_IND(sc, BCE_TAS_FTQ_CMD);
9044 	ctl = REG_RD_IND(sc, BCE_TAS_FTQ_CTL);
9045 	cur_depth = (ctl & BCE_TAS_FTQ_CTL_CUR_DEPTH) >> 22;
9046 	max_depth = (ctl & BCE_TAS_FTQ_CTL_MAX_DEPTH) >> 12;
9047 	valid_cnt = REG_RD(sc, BCE_HC_STAT_GEN_STAT12);
9048 	BCE_PRINTF(" TAS     0x%08X 0x%08X 0x%08X 0x%08X 0x%08X\n",
9049 		cmd, ctl, cur_depth, max_depth, valid_cnt);
9050 
9051 	/* Input queue to the Completion Processor */
9052 	cmd = REG_RD_IND(sc, BCE_COM_COMXQ_FTQ_CMD);
9053 	ctl = REG_RD_IND(sc, BCE_COM_COMXQ_FTQ_CTL);
9054 	cur_depth = (ctl & BCE_COM_COMXQ_FTQ_CTL_CUR_DEPTH) >> 22;
9055 	max_depth = (ctl & BCE_COM_COMXQ_FTQ_CTL_MAX_DEPTH) >> 12;
9056 	valid_cnt = REG_RD(sc, BCE_HC_STAT_GEN_STAT13);
9057 	BCE_PRINTF(" COMX    0x%08X 0x%08X 0x%08X 0x%08X 0x%08X\n",
9058 		cmd, ctl, cur_depth, max_depth, valid_cnt);
9059 
9060 	/* Input queue to the Completion Processor */
9061 	cmd = REG_RD_IND(sc, BCE_COM_COMTQ_FTQ_CMD);
9062 	ctl = REG_RD_IND(sc, BCE_COM_COMTQ_FTQ_CTL);
9063 	cur_depth = (ctl & BCE_COM_COMTQ_FTQ_CTL_CUR_DEPTH) >> 22;
9064 	max_depth = (ctl & BCE_COM_COMTQ_FTQ_CTL_MAX_DEPTH) >> 12;
9065 	valid_cnt = REG_RD(sc, BCE_HC_STAT_GEN_STAT14);
9066 	BCE_PRINTF(" COMT    0x%08X 0x%08X 0x%08X 0x%08X 0x%08X\n",
9067 		cmd, ctl, cur_depth, max_depth, valid_cnt);
9068 
9069 	/* Input queue to the Completion Processor */
9070 	cmd = REG_RD_IND(sc, BCE_COM_COMQ_FTQ_CMD);
9071 	ctl = REG_RD_IND(sc, BCE_COM_COMQ_FTQ_CTL);
9072 	cur_depth = (ctl & BCE_COM_COMQ_FTQ_CTL_CUR_DEPTH) >> 22;
9073 	max_depth = (ctl & BCE_COM_COMQ_FTQ_CTL_MAX_DEPTH) >> 12;
9074 	valid_cnt = REG_RD(sc, BCE_HC_STAT_GEN_STAT15);
9075 	BCE_PRINTF(" COMX    0x%08X 0x%08X 0x%08X 0x%08X 0x%08X\n",
9076 		cmd, ctl, cur_depth, max_depth, valid_cnt);
9077 
9078 	/* Setup the generic statistic counters for the FTQ valid count. */
9079 	val = (BCE_HC_STAT_GEN_SEL_0_GEN_SEL_0_CSQ_VALID_CNT  << 16) |
9080 		(BCE_HC_STAT_GEN_SEL_0_GEN_SEL_0_CPQ_VALID_CNT  <<  8) |
9081 		(BCE_HC_STAT_GEN_SEL_0_GEN_SEL_0_MGMQ_VALID_CNT);
9082 
9083 	if ((BCE_CHIP_NUM(sc) == BCE_CHIP_NUM_5709)	||
9084 		(BCE_CHIP_NUM(sc) == BCE_CHIP_NUM_5716))
9085 		val = val | (BCE_HC_STAT_GEN_SEL_0_GEN_SEL_0_RV2PCSQ_VALID_CNT_XI << 24);
9086 		REG_WR(sc, BCE_HC_STAT_GEN_SEL_0, val);
9087 
9088 	/* Input queue to the Management Control Processor */
9089 	cmd = REG_RD_IND(sc, BCE_MCP_MCPQ_FTQ_CMD);
9090 	ctl = REG_RD_IND(sc, BCE_MCP_MCPQ_FTQ_CTL);
9091 	cur_depth = (ctl & BCE_MCP_MCPQ_FTQ_CTL_CUR_DEPTH) >> 22;
9092 	max_depth = (ctl & BCE_MCP_MCPQ_FTQ_CTL_MAX_DEPTH) >> 12;
9093 	valid_cnt = REG_RD(sc, BCE_HC_STAT_GEN_STAT0);
9094 	BCE_PRINTF(" MCP     0x%08X 0x%08X 0x%08X 0x%08X 0x%08X\n",
9095 		cmd, ctl, cur_depth, max_depth, valid_cnt);
9096 
9097 	/* Input queue to the Command Processor */
9098 	cmd = REG_RD_IND(sc, BCE_CP_CPQ_FTQ_CMD);
9099 	ctl = REG_RD_IND(sc, BCE_CP_CPQ_FTQ_CTL);
9100 	cur_depth = (ctl & BCE_CP_CPQ_FTQ_CTL_CUR_DEPTH) >> 22;
9101 	max_depth = (ctl & BCE_CP_CPQ_FTQ_CTL_MAX_DEPTH) >> 12;
9102 	valid_cnt = REG_RD(sc, BCE_HC_STAT_GEN_STAT1);
9103 	BCE_PRINTF(" CP      0x%08X 0x%08X 0x%08X 0x%08X 0x%08X\n",
9104 		cmd, ctl, cur_depth, max_depth, valid_cnt);
9105 
9106 	/* Input queue to the Completion Scheduler state machine */
9107 	cmd = REG_RD(sc, BCE_CSCH_CH_FTQ_CMD);
9108 	ctl = REG_RD(sc, BCE_CSCH_CH_FTQ_CTL);
9109 	cur_depth = (ctl & BCE_CSCH_CH_FTQ_CTL_CUR_DEPTH) >> 22;
9110 	max_depth = (ctl & BCE_CSCH_CH_FTQ_CTL_MAX_DEPTH) >> 12;
9111 	valid_cnt = REG_RD(sc, BCE_HC_STAT_GEN_STAT2);
9112 	BCE_PRINTF(" CS      0x%08X 0x%08X 0x%08X 0x%08X 0x%08X\n",
9113 		cmd, ctl, cur_depth, max_depth, valid_cnt);
9114 
9115 	if ((BCE_CHIP_NUM(sc) == BCE_CHIP_NUM_5709) ||
9116 		(BCE_CHIP_NUM(sc) == BCE_CHIP_NUM_5716)) {
9117 		/* Input queue to the Receive Virtual to Physical Command Scheduler */
9118 		cmd = REG_RD(sc, BCE_RV2PCSR_FTQ_CMD);
9119 		ctl = REG_RD(sc, BCE_RV2PCSR_FTQ_CTL);
9120 		cur_depth = (ctl & 0xFFC00000) >> 22;
9121 		max_depth = (ctl & 0x003FF000) >> 12;
9122 		valid_cnt = REG_RD(sc, BCE_HC_STAT_GEN_STAT3);
9123 		BCE_PRINTF(" RV2PCSR 0x%08X 0x%08X 0x%08X 0x%08X 0x%08X\n",
9124 			cmd, ctl, cur_depth, max_depth, valid_cnt);
9125 	}
9126 
9127 	BCE_PRINTF(
9128 		"----------------------------"
9129 		"----------------"
9130 		"----------------------------\n");
9131 }
9132 
9133 
9134 /****************************************************************************/
9135 /* Prints out the TX chain.                                                 */
9136 /*                                                                          */
9137 /* Returns:                                                                 */
9138 /*   Nothing.                                                               */
9139 /****************************************************************************/
9140 static __attribute__ ((noinline)) void
9141 bce_dump_tx_chain(struct bce_softc *sc, u16 tx_prod, int count)
9142 {
9143 	struct tx_bd *txbd;
9144 
9145 	/* First some info about the tx_bd chain structure. */
9146 	BCE_PRINTF(
9147 		"----------------------------"
9148 		"  tx_bd  chain  "
9149 		"----------------------------\n");
9150 
9151 	BCE_PRINTF("page size      = 0x%08X, tx chain pages        = 0x%08X\n",
9152 		(u32) BCM_PAGE_SIZE, (u32) TX_PAGES);
9153 
9154 	BCE_PRINTF("tx_bd per page = 0x%08X, usable tx_bd per page = 0x%08X\n",
9155 		(u32) TOTAL_TX_BD_PER_PAGE, (u32) USABLE_TX_BD_PER_PAGE);
9156 
9157 	BCE_PRINTF("total tx_bd    = 0x%08X\n", (u32) TOTAL_TX_BD);
9158 
9159 	BCE_PRINTF(
9160 		"----------------------------"
9161 		"   tx_bd data   "
9162 		"----------------------------\n");
9163 
9164 	/* Now print out the tx_bd's themselves. */
9165 	for (int i = 0; i < count; i++) {
9166 	 	txbd = &sc->tx_bd_chain[TX_PAGE(tx_prod)][TX_IDX(tx_prod)];
9167 		bce_dump_txbd(sc, tx_prod, txbd);
9168 		tx_prod = NEXT_TX_BD(tx_prod);
9169 	}
9170 
9171 	BCE_PRINTF(
9172 		"----------------------------"
9173 		"----------------"
9174 		"----------------------------\n");
9175 }
9176 
9177 
9178 /****************************************************************************/
9179 /* Prints out the RX chain.                                                 */
9180 /*                                                                          */
9181 /* Returns:                                                                 */
9182 /*   Nothing.                                                               */
9183 /****************************************************************************/
9184 static __attribute__ ((noinline)) void
9185 bce_dump_rx_chain(struct bce_softc *sc, u16 rx_prod, int count)
9186 {
9187 	struct rx_bd *rxbd;
9188 
9189 	/* First some info about the rx_bd chain structure. */
9190 	BCE_PRINTF(
9191 		"----------------------------"
9192 		"  rx_bd  chain  "
9193 		"----------------------------\n");
9194 
9195 	BCE_PRINTF("page size      = 0x%08X, rx chain pages        = 0x%08X\n",
9196 		(u32) BCM_PAGE_SIZE, (u32) RX_PAGES);
9197 
9198 	BCE_PRINTF("rx_bd per page = 0x%08X, usable rx_bd per page = 0x%08X\n",
9199 		(u32) TOTAL_RX_BD_PER_PAGE, (u32) USABLE_RX_BD_PER_PAGE);
9200 
9201 	BCE_PRINTF("total rx_bd    = 0x%08X\n", (u32) TOTAL_RX_BD);
9202 
9203 	BCE_PRINTF(
9204 		"----------------------------"
9205 		"   rx_bd data   "
9206 		"----------------------------\n");
9207 
9208 	/* Now print out the rx_bd's themselves. */
9209 	for (int i = 0; i < count; i++) {
9210 		rxbd = &sc->rx_bd_chain[RX_PAGE(rx_prod)][RX_IDX(rx_prod)];
9211 		bce_dump_rxbd(sc, rx_prod, rxbd);
9212 		rx_prod = RX_CHAIN_IDX(rx_prod + 1);
9213 	}
9214 
9215 	BCE_PRINTF(
9216 		"----------------------------"
9217 		"----------------"
9218 		"----------------------------\n");
9219 }
9220 
9221 
9222 #ifdef ZERO_COPY_SOCKETS
9223 /****************************************************************************/
9224 /* Prints out the page chain.                                               */
9225 /*                                                                          */
9226 /* Returns:                                                                 */
9227 /*   Nothing.                                                               */
9228 /****************************************************************************/
9229 static __attribute__ ((noinline)) void
9230 bce_dump_pg_chain(struct bce_softc *sc, u16 pg_prod, int count)
9231 {
9232 	struct rx_bd *pgbd;
9233 
9234 	/* First some info about the page chain structure. */
9235 	BCE_PRINTF(
9236 		"----------------------------"
9237 		"   page chain   "
9238 		"----------------------------\n");
9239 
9240 	BCE_PRINTF("page size      = 0x%08X, pg chain pages        = 0x%08X\n",
9241 		(u32) BCM_PAGE_SIZE, (u32) PG_PAGES);
9242 
9243 	BCE_PRINTF("rx_bd per page = 0x%08X, usable rx_bd per page = 0x%08X\n",
9244 		(u32) TOTAL_PG_BD_PER_PAGE, (u32) USABLE_PG_BD_PER_PAGE);
9245 
9246 	BCE_PRINTF("total rx_bd    = 0x%08X, max_pg_bd             = 0x%08X\n",
9247 		(u32) TOTAL_PG_BD, (u32) MAX_PG_BD);
9248 
9249 	BCE_PRINTF(
9250 		"----------------------------"
9251 		"   page data    "
9252 		"----------------------------\n");
9253 
9254 	/* Now print out the rx_bd's themselves. */
9255 	for (int i = 0; i < count; i++) {
9256 		pgbd = &sc->pg_bd_chain[PG_PAGE(pg_prod)][PG_IDX(pg_prod)];
9257 		bce_dump_pgbd(sc, pg_prod, pgbd);
9258 		pg_prod = PG_CHAIN_IDX(pg_prod + 1);
9259 	}
9260 
9261 	BCE_PRINTF(
9262 		"----------------------------"
9263 		"----------------"
9264 		"----------------------------\n");
9265 }
9266 #endif
9267 
9268 
9269 /****************************************************************************/
9270 /* Prints out the status block from host memory.                            */
9271 /*                                                                          */
9272 /* Returns:                                                                 */
9273 /*   Nothing.                                                               */
9274 /****************************************************************************/
9275 static __attribute__ ((noinline)) void
9276 bce_dump_status_block(struct bce_softc *sc)
9277 {
9278 	struct status_block *sblk;
9279 
9280 	sblk = sc->status_block;
9281 
9282    	BCE_PRINTF(
9283 		"----------------------------"
9284 		"  Status Block  "
9285 		"----------------------------\n");
9286 
9287 	BCE_PRINTF("    0x%08X - attn_bits\n",
9288 		sblk->status_attn_bits);
9289 
9290 	BCE_PRINTF("    0x%08X - attn_bits_ack\n",
9291 		sblk->status_attn_bits_ack);
9292 
9293 	BCE_PRINTF("0x%04X(0x%04X) - rx_cons0\n",
9294 		sblk->status_rx_quick_consumer_index0,
9295 		(u16) RX_CHAIN_IDX(sblk->status_rx_quick_consumer_index0));
9296 
9297 	BCE_PRINTF("0x%04X(0x%04X) - tx_cons0\n",
9298 		sblk->status_tx_quick_consumer_index0,
9299 		(u16) TX_CHAIN_IDX(sblk->status_tx_quick_consumer_index0));
9300 
9301 	BCE_PRINTF("        0x%04X - status_idx\n", sblk->status_idx);
9302 
9303 	/* Theses indices are not used for normal L2 drivers. */
9304 	if (sblk->status_rx_quick_consumer_index1)
9305 		BCE_PRINTF("0x%04X(0x%04X) - rx_cons1\n",
9306 			sblk->status_rx_quick_consumer_index1,
9307 			(u16) RX_CHAIN_IDX(sblk->status_rx_quick_consumer_index1));
9308 
9309 	if (sblk->status_tx_quick_consumer_index1)
9310 		BCE_PRINTF("0x%04X(0x%04X) - tx_cons1\n",
9311 			sblk->status_tx_quick_consumer_index1,
9312 			(u16) TX_CHAIN_IDX(sblk->status_tx_quick_consumer_index1));
9313 
9314 	if (sblk->status_rx_quick_consumer_index2)
9315 		BCE_PRINTF("0x%04X(0x%04X)- rx_cons2\n",
9316 			sblk->status_rx_quick_consumer_index2,
9317 			(u16) RX_CHAIN_IDX(sblk->status_rx_quick_consumer_index2));
9318 
9319 	if (sblk->status_tx_quick_consumer_index2)
9320 		BCE_PRINTF("0x%04X(0x%04X) - tx_cons2\n",
9321 			sblk->status_tx_quick_consumer_index2,
9322 			(u16) TX_CHAIN_IDX(sblk->status_tx_quick_consumer_index2));
9323 
9324 	if (sblk->status_rx_quick_consumer_index3)
9325 		BCE_PRINTF("0x%04X(0x%04X) - rx_cons3\n",
9326 			sblk->status_rx_quick_consumer_index3,
9327 			(u16) RX_CHAIN_IDX(sblk->status_rx_quick_consumer_index3));
9328 
9329 	if (sblk->status_tx_quick_consumer_index3)
9330 		BCE_PRINTF("0x%04X(0x%04X) - tx_cons3\n",
9331 			sblk->status_tx_quick_consumer_index3,
9332 			(u16) TX_CHAIN_IDX(sblk->status_tx_quick_consumer_index3));
9333 
9334 	if (sblk->status_rx_quick_consumer_index4 ||
9335 		sblk->status_rx_quick_consumer_index5)
9336 		BCE_PRINTF("rx_cons4  = 0x%08X, rx_cons5      = 0x%08X\n",
9337 			sblk->status_rx_quick_consumer_index4,
9338 			sblk->status_rx_quick_consumer_index5);
9339 
9340 	if (sblk->status_rx_quick_consumer_index6 ||
9341 		sblk->status_rx_quick_consumer_index7)
9342 		BCE_PRINTF("rx_cons6  = 0x%08X, rx_cons7      = 0x%08X\n",
9343 			sblk->status_rx_quick_consumer_index6,
9344 			sblk->status_rx_quick_consumer_index7);
9345 
9346 	if (sblk->status_rx_quick_consumer_index8 ||
9347 		sblk->status_rx_quick_consumer_index9)
9348 		BCE_PRINTF("rx_cons8  = 0x%08X, rx_cons9      = 0x%08X\n",
9349 			sblk->status_rx_quick_consumer_index8,
9350 			sblk->status_rx_quick_consumer_index9);
9351 
9352 	if (sblk->status_rx_quick_consumer_index10 ||
9353 		sblk->status_rx_quick_consumer_index11)
9354 		BCE_PRINTF("rx_cons10 = 0x%08X, rx_cons11     = 0x%08X\n",
9355 			sblk->status_rx_quick_consumer_index10,
9356 			sblk->status_rx_quick_consumer_index11);
9357 
9358 	if (sblk->status_rx_quick_consumer_index12 ||
9359 		sblk->status_rx_quick_consumer_index13)
9360 		BCE_PRINTF("rx_cons12 = 0x%08X, rx_cons13     = 0x%08X\n",
9361 			sblk->status_rx_quick_consumer_index12,
9362 			sblk->status_rx_quick_consumer_index13);
9363 
9364 	if (sblk->status_rx_quick_consumer_index14 ||
9365 		sblk->status_rx_quick_consumer_index15)
9366 		BCE_PRINTF("rx_cons14 = 0x%08X, rx_cons15     = 0x%08X\n",
9367 			sblk->status_rx_quick_consumer_index14,
9368 			sblk->status_rx_quick_consumer_index15);
9369 
9370 	if (sblk->status_completion_producer_index ||
9371 		sblk->status_cmd_consumer_index)
9372 		BCE_PRINTF("com_prod  = 0x%08X, cmd_cons      = 0x%08X\n",
9373 			sblk->status_completion_producer_index,
9374 			sblk->status_cmd_consumer_index);
9375 
9376 	BCE_PRINTF(
9377 		"----------------------------"
9378 		"----------------"
9379 		"----------------------------\n");
9380 }
9381 
9382 
9383 /****************************************************************************/
9384 /* Prints out the statistics block from host memory.                        */
9385 /*                                                                          */
9386 /* Returns:                                                                 */
9387 /*   Nothing.                                                               */
9388 /****************************************************************************/
9389 static __attribute__ ((noinline)) void
9390 bce_dump_stats_block(struct bce_softc *sc)
9391 {
9392 	struct statistics_block *sblk;
9393 
9394 	sblk = sc->stats_block;
9395 
9396 	BCE_PRINTF(
9397 		"---------------"
9398 		" Stats Block  (All Stats Not Shown Are 0) "
9399 		"---------------\n");
9400 
9401 	if (sblk->stat_IfHCInOctets_hi
9402 		|| sblk->stat_IfHCInOctets_lo)
9403 		BCE_PRINTF("0x%08X:%08X : "
9404 			"IfHcInOctets\n",
9405 			sblk->stat_IfHCInOctets_hi,
9406 			sblk->stat_IfHCInOctets_lo);
9407 
9408 	if (sblk->stat_IfHCInBadOctets_hi
9409 		|| sblk->stat_IfHCInBadOctets_lo)
9410 		BCE_PRINTF("0x%08X:%08X : "
9411 			"IfHcInBadOctets\n",
9412 			sblk->stat_IfHCInBadOctets_hi,
9413 			sblk->stat_IfHCInBadOctets_lo);
9414 
9415 	if (sblk->stat_IfHCOutOctets_hi
9416 		|| sblk->stat_IfHCOutOctets_lo)
9417 		BCE_PRINTF("0x%08X:%08X : "
9418 			"IfHcOutOctets\n",
9419 			sblk->stat_IfHCOutOctets_hi,
9420 			sblk->stat_IfHCOutOctets_lo);
9421 
9422 	if (sblk->stat_IfHCOutBadOctets_hi
9423 		|| sblk->stat_IfHCOutBadOctets_lo)
9424 		BCE_PRINTF("0x%08X:%08X : "
9425 			"IfHcOutBadOctets\n",
9426 			sblk->stat_IfHCOutBadOctets_hi,
9427 			sblk->stat_IfHCOutBadOctets_lo);
9428 
9429 	if (sblk->stat_IfHCInUcastPkts_hi
9430 		|| sblk->stat_IfHCInUcastPkts_lo)
9431 		BCE_PRINTF("0x%08X:%08X : "
9432 			"IfHcInUcastPkts\n",
9433 			sblk->stat_IfHCInUcastPkts_hi,
9434 			sblk->stat_IfHCInUcastPkts_lo);
9435 
9436 	if (sblk->stat_IfHCInBroadcastPkts_hi
9437 		|| sblk->stat_IfHCInBroadcastPkts_lo)
9438 		BCE_PRINTF("0x%08X:%08X : "
9439 			"IfHcInBroadcastPkts\n",
9440 			sblk->stat_IfHCInBroadcastPkts_hi,
9441 			sblk->stat_IfHCInBroadcastPkts_lo);
9442 
9443 	if (sblk->stat_IfHCInMulticastPkts_hi
9444 		|| sblk->stat_IfHCInMulticastPkts_lo)
9445 		BCE_PRINTF("0x%08X:%08X : "
9446 			"IfHcInMulticastPkts\n",
9447 			sblk->stat_IfHCInMulticastPkts_hi,
9448 			sblk->stat_IfHCInMulticastPkts_lo);
9449 
9450 	if (sblk->stat_IfHCOutUcastPkts_hi
9451 		|| sblk->stat_IfHCOutUcastPkts_lo)
9452 		BCE_PRINTF("0x%08X:%08X : "
9453 			"IfHcOutUcastPkts\n",
9454 			sblk->stat_IfHCOutUcastPkts_hi,
9455 			sblk->stat_IfHCOutUcastPkts_lo);
9456 
9457 	if (sblk->stat_IfHCOutBroadcastPkts_hi
9458 		|| sblk->stat_IfHCOutBroadcastPkts_lo)
9459 		BCE_PRINTF("0x%08X:%08X : "
9460 			"IfHcOutBroadcastPkts\n",
9461 			sblk->stat_IfHCOutBroadcastPkts_hi,
9462 			sblk->stat_IfHCOutBroadcastPkts_lo);
9463 
9464 	if (sblk->stat_IfHCOutMulticastPkts_hi
9465 		|| sblk->stat_IfHCOutMulticastPkts_lo)
9466 		BCE_PRINTF("0x%08X:%08X : "
9467 			"IfHcOutMulticastPkts\n",
9468 			sblk->stat_IfHCOutMulticastPkts_hi,
9469 			sblk->stat_IfHCOutMulticastPkts_lo);
9470 
9471 	if (sblk->stat_emac_tx_stat_dot3statsinternalmactransmiterrors)
9472 		BCE_PRINTF("         0x%08X : "
9473 			"emac_tx_stat_dot3statsinternalmactransmiterrors\n",
9474 			sblk->stat_emac_tx_stat_dot3statsinternalmactransmiterrors);
9475 
9476 	if (sblk->stat_Dot3StatsCarrierSenseErrors)
9477 		BCE_PRINTF("         0x%08X : Dot3StatsCarrierSenseErrors\n",
9478 			sblk->stat_Dot3StatsCarrierSenseErrors);
9479 
9480 	if (sblk->stat_Dot3StatsFCSErrors)
9481 		BCE_PRINTF("         0x%08X : Dot3StatsFCSErrors\n",
9482 			sblk->stat_Dot3StatsFCSErrors);
9483 
9484 	if (sblk->stat_Dot3StatsAlignmentErrors)
9485 		BCE_PRINTF("         0x%08X : Dot3StatsAlignmentErrors\n",
9486 			sblk->stat_Dot3StatsAlignmentErrors);
9487 
9488 	if (sblk->stat_Dot3StatsSingleCollisionFrames)
9489 		BCE_PRINTF("         0x%08X : Dot3StatsSingleCollisionFrames\n",
9490 			sblk->stat_Dot3StatsSingleCollisionFrames);
9491 
9492 	if (sblk->stat_Dot3StatsMultipleCollisionFrames)
9493 		BCE_PRINTF("         0x%08X : Dot3StatsMultipleCollisionFrames\n",
9494 			sblk->stat_Dot3StatsMultipleCollisionFrames);
9495 
9496 	if (sblk->stat_Dot3StatsDeferredTransmissions)
9497 		BCE_PRINTF("         0x%08X : Dot3StatsDeferredTransmissions\n",
9498 			sblk->stat_Dot3StatsDeferredTransmissions);
9499 
9500 	if (sblk->stat_Dot3StatsExcessiveCollisions)
9501 		BCE_PRINTF("         0x%08X : Dot3StatsExcessiveCollisions\n",
9502 			sblk->stat_Dot3StatsExcessiveCollisions);
9503 
9504 	if (sblk->stat_Dot3StatsLateCollisions)
9505 		BCE_PRINTF("         0x%08X : Dot3StatsLateCollisions\n",
9506 			sblk->stat_Dot3StatsLateCollisions);
9507 
9508 	if (sblk->stat_EtherStatsCollisions)
9509 		BCE_PRINTF("         0x%08X : EtherStatsCollisions\n",
9510 			sblk->stat_EtherStatsCollisions);
9511 
9512 	if (sblk->stat_EtherStatsFragments)
9513 		BCE_PRINTF("         0x%08X : EtherStatsFragments\n",
9514 			sblk->stat_EtherStatsFragments);
9515 
9516 	if (sblk->stat_EtherStatsJabbers)
9517 		BCE_PRINTF("         0x%08X : EtherStatsJabbers\n",
9518 			sblk->stat_EtherStatsJabbers);
9519 
9520 	if (sblk->stat_EtherStatsUndersizePkts)
9521 		BCE_PRINTF("         0x%08X : EtherStatsUndersizePkts\n",
9522 			sblk->stat_EtherStatsUndersizePkts);
9523 
9524 	if (sblk->stat_EtherStatsOversizePkts)
9525 		BCE_PRINTF("         0x%08X : EtherStatsOverrsizePkts\n",
9526 			sblk->stat_EtherStatsOversizePkts);
9527 
9528 	if (sblk->stat_EtherStatsPktsRx64Octets)
9529 		BCE_PRINTF("         0x%08X : EtherStatsPktsRx64Octets\n",
9530 			sblk->stat_EtherStatsPktsRx64Octets);
9531 
9532 	if (sblk->stat_EtherStatsPktsRx65Octetsto127Octets)
9533 		BCE_PRINTF("         0x%08X : EtherStatsPktsRx65Octetsto127Octets\n",
9534 			sblk->stat_EtherStatsPktsRx65Octetsto127Octets);
9535 
9536 	if (sblk->stat_EtherStatsPktsRx128Octetsto255Octets)
9537 		BCE_PRINTF("         0x%08X : EtherStatsPktsRx128Octetsto255Octets\n",
9538 			sblk->stat_EtherStatsPktsRx128Octetsto255Octets);
9539 
9540 	if (sblk->stat_EtherStatsPktsRx256Octetsto511Octets)
9541 		BCE_PRINTF("         0x%08X : EtherStatsPktsRx256Octetsto511Octets\n",
9542 			sblk->stat_EtherStatsPktsRx256Octetsto511Octets);
9543 
9544 	if (sblk->stat_EtherStatsPktsRx512Octetsto1023Octets)
9545 		BCE_PRINTF("         0x%08X : EtherStatsPktsRx512Octetsto1023Octets\n",
9546 			sblk->stat_EtherStatsPktsRx512Octetsto1023Octets);
9547 
9548 	if (sblk->stat_EtherStatsPktsRx1024Octetsto1522Octets)
9549 		BCE_PRINTF("         0x%08X : EtherStatsPktsRx1024Octetsto1522Octets\n",
9550 			sblk->stat_EtherStatsPktsRx1024Octetsto1522Octets);
9551 
9552 	if (sblk->stat_EtherStatsPktsRx1523Octetsto9022Octets)
9553 		BCE_PRINTF("         0x%08X : EtherStatsPktsRx1523Octetsto9022Octets\n",
9554 			sblk->stat_EtherStatsPktsRx1523Octetsto9022Octets);
9555 
9556 	if (sblk->stat_EtherStatsPktsTx64Octets)
9557 		BCE_PRINTF("         0x%08X : EtherStatsPktsTx64Octets\n",
9558 			sblk->stat_EtherStatsPktsTx64Octets);
9559 
9560 	if (sblk->stat_EtherStatsPktsTx65Octetsto127Octets)
9561 		BCE_PRINTF("         0x%08X : EtherStatsPktsTx65Octetsto127Octets\n",
9562 			sblk->stat_EtherStatsPktsTx65Octetsto127Octets);
9563 
9564 	if (sblk->stat_EtherStatsPktsTx128Octetsto255Octets)
9565 		BCE_PRINTF("         0x%08X : EtherStatsPktsTx128Octetsto255Octets\n",
9566 			sblk->stat_EtherStatsPktsTx128Octetsto255Octets);
9567 
9568 	if (sblk->stat_EtherStatsPktsTx256Octetsto511Octets)
9569 		BCE_PRINTF("         0x%08X : EtherStatsPktsTx256Octetsto511Octets\n",
9570 			sblk->stat_EtherStatsPktsTx256Octetsto511Octets);
9571 
9572 	if (sblk->stat_EtherStatsPktsTx512Octetsto1023Octets)
9573 		BCE_PRINTF("         0x%08X : EtherStatsPktsTx512Octetsto1023Octets\n",
9574 			sblk->stat_EtherStatsPktsTx512Octetsto1023Octets);
9575 
9576 	if (sblk->stat_EtherStatsPktsTx1024Octetsto1522Octets)
9577 		BCE_PRINTF("         0x%08X : EtherStatsPktsTx1024Octetsto1522Octets\n",
9578 			sblk->stat_EtherStatsPktsTx1024Octetsto1522Octets);
9579 
9580 	if (sblk->stat_EtherStatsPktsTx1523Octetsto9022Octets)
9581 		BCE_PRINTF("         0x%08X : EtherStatsPktsTx1523Octetsto9022Octets\n",
9582 			sblk->stat_EtherStatsPktsTx1523Octetsto9022Octets);
9583 
9584 	if (sblk->stat_XonPauseFramesReceived)
9585 		BCE_PRINTF("         0x%08X : XonPauseFramesReceived\n",
9586 			sblk->stat_XonPauseFramesReceived);
9587 
9588 	if (sblk->stat_XoffPauseFramesReceived)
9589 	   BCE_PRINTF("          0x%08X : XoffPauseFramesReceived\n",
9590 			sblk->stat_XoffPauseFramesReceived);
9591 
9592 	if (sblk->stat_OutXonSent)
9593 		BCE_PRINTF("         0x%08X : OutXonSent\n",
9594 			sblk->stat_OutXonSent);
9595 
9596 	if (sblk->stat_OutXoffSent)
9597 		BCE_PRINTF("         0x%08X : OutXoffSent\n",
9598 			sblk->stat_OutXoffSent);
9599 
9600 	if (sblk->stat_FlowControlDone)
9601 		BCE_PRINTF("         0x%08X : FlowControlDone\n",
9602 			sblk->stat_FlowControlDone);
9603 
9604 	if (sblk->stat_MacControlFramesReceived)
9605 		BCE_PRINTF("         0x%08X : MacControlFramesReceived\n",
9606 			sblk->stat_MacControlFramesReceived);
9607 
9608 	if (sblk->stat_XoffStateEntered)
9609 		BCE_PRINTF("         0x%08X : XoffStateEntered\n",
9610 			sblk->stat_XoffStateEntered);
9611 
9612 	if (sblk->stat_IfInFramesL2FilterDiscards)
9613 		BCE_PRINTF("         0x%08X : IfInFramesL2FilterDiscards\n",
9614 			sblk->stat_IfInFramesL2FilterDiscards);
9615 
9616 	if (sblk->stat_IfInRuleCheckerDiscards)
9617 		BCE_PRINTF("         0x%08X : IfInRuleCheckerDiscards\n",
9618 			sblk->stat_IfInRuleCheckerDiscards);
9619 
9620 	if (sblk->stat_IfInFTQDiscards)
9621 		BCE_PRINTF("         0x%08X : IfInFTQDiscards\n",
9622 			sblk->stat_IfInFTQDiscards);
9623 
9624 	if (sblk->stat_IfInMBUFDiscards)
9625 		BCE_PRINTF("         0x%08X : IfInMBUFDiscards\n",
9626 			sblk->stat_IfInMBUFDiscards);
9627 
9628 	if (sblk->stat_IfInRuleCheckerP4Hit)
9629 		BCE_PRINTF("         0x%08X : IfInRuleCheckerP4Hit\n",
9630 			sblk->stat_IfInRuleCheckerP4Hit);
9631 
9632 	if (sblk->stat_CatchupInRuleCheckerDiscards)
9633 		BCE_PRINTF("         0x%08X : CatchupInRuleCheckerDiscards\n",
9634 			sblk->stat_CatchupInRuleCheckerDiscards);
9635 
9636 	if (sblk->stat_CatchupInFTQDiscards)
9637 		BCE_PRINTF("         0x%08X : CatchupInFTQDiscards\n",
9638 			sblk->stat_CatchupInFTQDiscards);
9639 
9640 	if (sblk->stat_CatchupInMBUFDiscards)
9641 		BCE_PRINTF("         0x%08X : CatchupInMBUFDiscards\n",
9642 			sblk->stat_CatchupInMBUFDiscards);
9643 
9644 	if (sblk->stat_CatchupInRuleCheckerP4Hit)
9645 		BCE_PRINTF("         0x%08X : CatchupInRuleCheckerP4Hit\n",
9646 			sblk->stat_CatchupInRuleCheckerP4Hit);
9647 
9648 	BCE_PRINTF(
9649 		"----------------------------"
9650 		"----------------"
9651 		"----------------------------\n");
9652 }
9653 
9654 
9655 /****************************************************************************/
9656 /* Prints out a summary of the driver state.                                */
9657 /*                                                                          */
9658 /* Returns:                                                                 */
9659 /*   Nothing.                                                               */
9660 /****************************************************************************/
9661 static __attribute__ ((noinline)) void
9662 bce_dump_driver_state(struct bce_softc *sc)
9663 {
9664 	u32 val_hi, val_lo;
9665 
9666 	BCE_PRINTF(
9667 		"-----------------------------"
9668 		" Driver State "
9669 		"-----------------------------\n");
9670 
9671 	val_hi = BCE_ADDR_HI(sc);
9672 	val_lo = BCE_ADDR_LO(sc);
9673 	BCE_PRINTF("0x%08X:%08X - (sc) driver softc structure virtual address\n",
9674 		val_hi, val_lo);
9675 
9676 	val_hi = BCE_ADDR_HI(sc->bce_vhandle);
9677 	val_lo = BCE_ADDR_LO(sc->bce_vhandle);
9678 	BCE_PRINTF("0x%08X:%08X - (sc->bce_vhandle) PCI BAR virtual address\n",
9679 		val_hi, val_lo);
9680 
9681 	val_hi = BCE_ADDR_HI(sc->status_block);
9682 	val_lo = BCE_ADDR_LO(sc->status_block);
9683 	BCE_PRINTF("0x%08X:%08X - (sc->status_block) status block virtual address\n",
9684 		val_hi, val_lo);
9685 
9686 	val_hi = BCE_ADDR_HI(sc->stats_block);
9687 	val_lo = BCE_ADDR_LO(sc->stats_block);
9688 	BCE_PRINTF("0x%08X:%08X - (sc->stats_block) statistics block virtual address\n",
9689 		val_hi, val_lo);
9690 
9691 	val_hi = BCE_ADDR_HI(sc->tx_bd_chain);
9692 	val_lo = BCE_ADDR_LO(sc->tx_bd_chain);
9693 	BCE_PRINTF(
9694 		"0x%08X:%08X - (sc->tx_bd_chain) tx_bd chain virtual adddress\n",
9695 		val_hi, val_lo);
9696 
9697 	val_hi = BCE_ADDR_HI(sc->rx_bd_chain);
9698 	val_lo = BCE_ADDR_LO(sc->rx_bd_chain);
9699 	BCE_PRINTF(
9700 		"0x%08X:%08X - (sc->rx_bd_chain) rx_bd chain virtual address\n",
9701 		val_hi, val_lo);
9702 
9703 #ifdef ZERO_COPY_SOCKETS
9704 	val_hi = BCE_ADDR_HI(sc->pg_bd_chain);
9705 	val_lo = BCE_ADDR_LO(sc->pg_bd_chain);
9706 	BCE_PRINTF(
9707 		"0x%08X:%08X - (sc->pg_bd_chain) page chain virtual address\n",
9708 		val_hi, val_lo);
9709 #endif
9710 
9711 	val_hi = BCE_ADDR_HI(sc->tx_mbuf_ptr);
9712 	val_lo = BCE_ADDR_LO(sc->tx_mbuf_ptr);
9713 	BCE_PRINTF(
9714 		"0x%08X:%08X - (sc->tx_mbuf_ptr) tx mbuf chain virtual address\n",
9715 		val_hi, val_lo);
9716 
9717 	val_hi = BCE_ADDR_HI(sc->rx_mbuf_ptr);
9718 	val_lo = BCE_ADDR_LO(sc->rx_mbuf_ptr);
9719 	BCE_PRINTF(
9720 		"0x%08X:%08X - (sc->rx_mbuf_ptr) rx mbuf chain virtual address\n",
9721 		val_hi, val_lo);
9722 
9723 #ifdef ZERO_COPY_SOCKETS
9724 	val_hi = BCE_ADDR_HI(sc->pg_mbuf_ptr);
9725 	val_lo = BCE_ADDR_LO(sc->pg_mbuf_ptr);
9726 	BCE_PRINTF(
9727 		"0x%08X:%08X - (sc->pg_mbuf_ptr) page mbuf chain virtual address\n",
9728 		val_hi, val_lo);
9729 #endif
9730 
9731 	BCE_PRINTF("         0x%08X - (sc->interrupts_generated) h/w intrs\n",
9732 		sc->interrupts_generated);
9733 
9734 	BCE_PRINTF("         0x%08X - (sc->rx_interrupts) rx interrupts handled\n",
9735 		sc->rx_interrupts);
9736 
9737 	BCE_PRINTF("         0x%08X - (sc->tx_interrupts) tx interrupts handled\n",
9738 		sc->tx_interrupts);
9739 
9740 	BCE_PRINTF("         0x%08X - (sc->last_status_idx) status block index\n",
9741 		sc->last_status_idx);
9742 
9743 	BCE_PRINTF("     0x%04X(0x%04X) - (sc->tx_prod) tx producer index\n",
9744 		sc->tx_prod, (u16) TX_CHAIN_IDX(sc->tx_prod));
9745 
9746 	BCE_PRINTF("     0x%04X(0x%04X) - (sc->tx_cons) tx consumer index\n",
9747 		sc->tx_cons, (u16) TX_CHAIN_IDX(sc->tx_cons));
9748 
9749 	BCE_PRINTF("         0x%08X - (sc->tx_prod_bseq) tx producer bseq index\n",
9750 		sc->tx_prod_bseq);
9751 
9752 	BCE_PRINTF("         0x%08X - (sc->debug_tx_mbuf_alloc) tx mbufs allocated\n",
9753 		sc->debug_tx_mbuf_alloc);
9754 
9755 	BCE_PRINTF("         0x%08X - (sc->used_tx_bd) used tx_bd's\n",
9756 		sc->used_tx_bd);
9757 
9758 	BCE_PRINTF("0x%08X/%08X - (sc->tx_hi_watermark) tx hi watermark\n",
9759 		sc->tx_hi_watermark, sc->max_tx_bd);
9760 
9761 	BCE_PRINTF("     0x%04X(0x%04X) - (sc->rx_prod) rx producer index\n",
9762 		sc->rx_prod, (u16) RX_CHAIN_IDX(sc->rx_prod));
9763 
9764 	BCE_PRINTF("     0x%04X(0x%04X) - (sc->rx_cons) rx consumer index\n",
9765 		sc->rx_cons, (u16) RX_CHAIN_IDX(sc->rx_cons));
9766 
9767 	BCE_PRINTF("         0x%08X - (sc->rx_prod_bseq) rx producer bseq index\n",
9768 		sc->rx_prod_bseq);
9769 
9770 	BCE_PRINTF("         0x%08X - (sc->debug_rx_mbuf_alloc) rx mbufs allocated\n",
9771 		sc->debug_rx_mbuf_alloc);
9772 
9773 	BCE_PRINTF("         0x%08X - (sc->free_rx_bd) free rx_bd's\n",
9774 		sc->free_rx_bd);
9775 
9776 #ifdef ZERO_COPY_SOCKETS
9777 	BCE_PRINTF("     0x%04X(0x%04X) - (sc->pg_prod) page producer index\n",
9778 		sc->pg_prod, (u16) PG_CHAIN_IDX(sc->pg_prod));
9779 
9780 	BCE_PRINTF("     0x%04X(0x%04X) - (sc->pg_cons) page consumer index\n",
9781 		sc->pg_cons, (u16) PG_CHAIN_IDX(sc->pg_cons));
9782 
9783 	BCE_PRINTF("         0x%08X - (sc->debug_pg_mbuf_alloc) page mbufs allocated\n",
9784 		sc->debug_pg_mbuf_alloc);
9785 
9786 	BCE_PRINTF("         0x%08X - (sc->free_pg_bd) free page rx_bd's\n",
9787 		sc->free_pg_bd);
9788 
9789 	BCE_PRINTF("0x%08X/%08X - (sc->pg_low_watermark) page low watermark\n",
9790 		sc->pg_low_watermark, sc->max_pg_bd);
9791 #endif
9792 
9793 	BCE_PRINTF("         0x%08X - (sc->mbuf_alloc_failed_count) "
9794 		"mbuf alloc failures\n",
9795 		sc->mbuf_alloc_failed_count);
9796 
9797 	BCE_PRINTF("         0x%08X - (sc->bce_flags) bce mac flags\n",
9798 		sc->bce_flags);
9799 
9800 	BCE_PRINTF("         0x%08X - (sc->bce_phy_flags) bce phy flags\n",
9801 		sc->bce_phy_flags);
9802 
9803 	BCE_PRINTF(
9804 		"----------------------------"
9805 		"----------------"
9806 		"----------------------------\n");
9807 }
9808 
9809 
9810 /****************************************************************************/
9811 /* Prints out the hardware state through a summary of important register,   */
9812 /* followed by a complete register dump.                                    */
9813 /*                                                                          */
9814 /* Returns:                                                                 */
9815 /*   Nothing.                                                               */
9816 /****************************************************************************/
9817 static __attribute__ ((noinline)) void
9818 bce_dump_hw_state(struct bce_softc *sc)
9819 {
9820 	u32 val;
9821 
9822 	BCE_PRINTF(
9823 		"----------------------------"
9824 		" Hardware State "
9825 		"----------------------------\n");
9826 
9827 	BCE_PRINTF("0x%08X - bootcode version\n", sc->bce_bc_ver);
9828 
9829 	val = REG_RD(sc, BCE_MISC_ENABLE_STATUS_BITS);
9830 	BCE_PRINTF("0x%08X - (0x%06X) misc_enable_status_bits\n",
9831 		val, BCE_MISC_ENABLE_STATUS_BITS);
9832 
9833 	val = REG_RD(sc, BCE_DMA_STATUS);
9834 	BCE_PRINTF("0x%08X - (0x%06X) dma_status\n", val, BCE_DMA_STATUS);
9835 
9836 	val = REG_RD(sc, BCE_CTX_STATUS);
9837 	BCE_PRINTF("0x%08X - (0x%06X) ctx_status\n", val, BCE_CTX_STATUS);
9838 
9839 	val = REG_RD(sc, BCE_EMAC_STATUS);
9840 	BCE_PRINTF("0x%08X - (0x%06X) emac_status\n", val, BCE_EMAC_STATUS);
9841 
9842 	val = REG_RD(sc, BCE_RPM_STATUS);
9843 	BCE_PRINTF("0x%08X - (0x%06X) rpm_status\n", val, BCE_RPM_STATUS);
9844 
9845 	val = REG_RD(sc, 0x2004);
9846 	BCE_PRINTF("0x%08X - (0x%06X) rlup_status\n", val, 0x2004);
9847 
9848 	val = REG_RD(sc, BCE_RV2P_STATUS);
9849 	BCE_PRINTF("0x%08X - (0x%06X) rv2p_status\n", val, BCE_RV2P_STATUS);
9850 
9851 	val = REG_RD(sc, 0x2c04);
9852 	BCE_PRINTF("0x%08X - (0x%06X) rdma_status\n", val, 0x2c04);
9853 
9854 	val = REG_RD(sc, BCE_TBDR_STATUS);
9855 	BCE_PRINTF("0x%08X - (0x%06X) tbdr_status\n", val, BCE_TBDR_STATUS);
9856 
9857 	val = REG_RD(sc, BCE_TDMA_STATUS);
9858 	BCE_PRINTF("0x%08X - (0x%06X) tdma_status\n", val, BCE_TDMA_STATUS);
9859 
9860 	val = REG_RD(sc, BCE_HC_STATUS);
9861 	BCE_PRINTF("0x%08X - (0x%06X) hc_status\n", val, BCE_HC_STATUS);
9862 
9863 	val = REG_RD_IND(sc, BCE_TXP_CPU_STATE);
9864 	BCE_PRINTF("0x%08X - (0x%06X) txp_cpu_state\n", val, BCE_TXP_CPU_STATE);
9865 
9866 	val = REG_RD_IND(sc, BCE_TPAT_CPU_STATE);
9867 	BCE_PRINTF("0x%08X - (0x%06X) tpat_cpu_state\n", val, BCE_TPAT_CPU_STATE);
9868 
9869 	val = REG_RD_IND(sc, BCE_RXP_CPU_STATE);
9870 	BCE_PRINTF("0x%08X - (0x%06X) rxp_cpu_state\n", val, BCE_RXP_CPU_STATE);
9871 
9872 	val = REG_RD_IND(sc, BCE_COM_CPU_STATE);
9873 	BCE_PRINTF("0x%08X - (0x%06X) com_cpu_state\n", val, BCE_COM_CPU_STATE);
9874 
9875 	val = REG_RD_IND(sc, BCE_MCP_CPU_STATE);
9876 	BCE_PRINTF("0x%08X - (0x%06X) mcp_cpu_state\n", val, BCE_MCP_CPU_STATE);
9877 
9878 	val = REG_RD_IND(sc, BCE_CP_CPU_STATE);
9879 	BCE_PRINTF("0x%08X - (0x%06X) cp_cpu_state\n", val, BCE_CP_CPU_STATE);
9880 
9881 	BCE_PRINTF(
9882 		"----------------------------"
9883 		"----------------"
9884 		"----------------------------\n");
9885 
9886 	BCE_PRINTF(
9887 		"----------------------------"
9888 		" Register  Dump "
9889 		"----------------------------\n");
9890 
9891 	for (int i = 0x400; i < 0x8000; i += 0x10) {
9892 		BCE_PRINTF("0x%04X: 0x%08X 0x%08X 0x%08X 0x%08X\n",
9893 			i, REG_RD(sc, i), REG_RD(sc, i + 0x4),
9894 			REG_RD(sc, i + 0x8), REG_RD(sc, i + 0xC));
9895 	}
9896 
9897 	BCE_PRINTF(
9898 		"----------------------------"
9899 		"----------------"
9900 		"----------------------------\n");
9901 }
9902 
9903 
9904 /****************************************************************************/
9905 /* Prints out the mailbox queue registers.                                  */
9906 /*                                                                          */
9907 /* Returns:                                                                 */
9908 /*   Nothing.                                                               */
9909 /****************************************************************************/
9910 static __attribute__ ((noinline)) void
9911 bce_dump_mq_regs(struct bce_softc *sc)
9912 {
9913 	BCE_PRINTF(
9914 		"----------------------------"
9915 		"    MQ Regs     "
9916 		"----------------------------\n");
9917 
9918 	BCE_PRINTF(
9919 		"----------------------------"
9920 		"----------------"
9921 		"----------------------------\n");
9922 
9923 	for (int i = 0x3c00; i < 0x4000; i += 0x10) {
9924 		BCE_PRINTF("0x%04X: 0x%08X 0x%08X 0x%08X 0x%08X\n",
9925 			i, REG_RD(sc, i), REG_RD(sc, i + 0x4),
9926 			REG_RD(sc, i + 0x8), REG_RD(sc, i + 0xC));
9927 	}
9928 
9929 	BCE_PRINTF(
9930 		"----------------------------"
9931 		"----------------"
9932 		"----------------------------\n");
9933 }
9934 
9935 
9936 /****************************************************************************/
9937 /* Prints out the bootcode state.                                           */
9938 /*                                                                          */
9939 /* Returns:                                                                 */
9940 /*   Nothing.                                                               */
9941 /****************************************************************************/
9942 static __attribute__ ((noinline)) void
9943 bce_dump_bc_state(struct bce_softc *sc)
9944 {
9945 	u32 val;
9946 
9947 	BCE_PRINTF(
9948 		"----------------------------"
9949 		" Bootcode State "
9950 		"----------------------------\n");
9951 
9952 	BCE_PRINTF("0x%08X - bootcode version\n", sc->bce_bc_ver);
9953 
9954 	val = REG_RD_IND(sc, sc->bce_shmem_base + BCE_BC_RESET_TYPE);
9955 	BCE_PRINTF("0x%08X - (0x%06X) reset_type\n",
9956 		val, BCE_BC_RESET_TYPE);
9957 
9958 	val = REG_RD_IND(sc, sc->bce_shmem_base + BCE_BC_STATE);
9959 	BCE_PRINTF("0x%08X - (0x%06X) state\n",
9960 		val, BCE_BC_STATE);
9961 
9962 	val = REG_RD_IND(sc, sc->bce_shmem_base + BCE_BC_CONDITION);
9963 	BCE_PRINTF("0x%08X - (0x%06X) condition\n",
9964 		val, BCE_BC_CONDITION);
9965 
9966 	val = REG_RD_IND(sc, sc->bce_shmem_base + BCE_BC_STATE_DEBUG_CMD);
9967 	BCE_PRINTF("0x%08X - (0x%06X) debug_cmd\n",
9968 		val, BCE_BC_STATE_DEBUG_CMD);
9969 
9970 	BCE_PRINTF(
9971 		"----------------------------"
9972 		"----------------"
9973 		"----------------------------\n");
9974 }
9975 
9976 
9977 /****************************************************************************/
9978 /* Prints out the TXP processor state.                                      */
9979 /*                                                                          */
9980 /* Returns:                                                                 */
9981 /*   Nothing.                                                               */
9982 /****************************************************************************/
9983 static __attribute__ ((noinline)) void
9984 bce_dump_txp_state(struct bce_softc *sc, int regs)
9985 {
9986 	u32 val;
9987 	u32 fw_version[3];
9988 
9989 	BCE_PRINTF(
9990 		"----------------------------"
9991 		"   TXP  State   "
9992 		"----------------------------\n");
9993 
9994 	for (int i = 0; i < 3; i++)
9995 		fw_version[i] = htonl(REG_RD_IND(sc,
9996 			(BCE_TXP_SCRATCH + 0x10 + i * 4)));
9997 	BCE_PRINTF("Firmware version - %s\n", (char *) fw_version);
9998 
9999 	val = REG_RD_IND(sc, BCE_TXP_CPU_MODE);
10000 	BCE_PRINTF("0x%08X - (0x%06X) txp_cpu_mode\n", val, BCE_TXP_CPU_MODE);
10001 
10002 	val = REG_RD_IND(sc, BCE_TXP_CPU_STATE);
10003 	BCE_PRINTF("0x%08X - (0x%06X) txp_cpu_state\n", val, BCE_TXP_CPU_STATE);
10004 
10005 	val = REG_RD_IND(sc, BCE_TXP_CPU_EVENT_MASK);
10006 	BCE_PRINTF("0x%08X - (0x%06X) txp_cpu_event_mask\n", val,
10007 		BCE_TXP_CPU_EVENT_MASK);
10008 
10009 	if (regs) {
10010 		BCE_PRINTF(
10011 			"----------------------------"
10012 			" Register  Dump "
10013 			"----------------------------\n");
10014 
10015 		for (int i = BCE_TXP_CPU_MODE; i < 0x68000; i += 0x10) {
10016 			/* Skip the big blank spaces */
10017 			if (i < 0x454000 && i > 0x5ffff)
10018 				BCE_PRINTF("0x%04X: 0x%08X 0x%08X 0x%08X 0x%08X\n",
10019 					i, REG_RD_IND(sc, i), REG_RD_IND(sc, i + 0x4),
10020 					REG_RD_IND(sc, i + 0x8), REG_RD_IND(sc, i + 0xC));
10021 		}
10022 	}
10023 
10024 	BCE_PRINTF(
10025 		"----------------------------"
10026 		"----------------"
10027 		"----------------------------\n");
10028 }
10029 
10030 
10031 /****************************************************************************/
10032 /* Prints out the RXP processor state.                                      */
10033 /*                                                                          */
10034 /* Returns:                                                                 */
10035 /*   Nothing.                                                               */
10036 /****************************************************************************/
10037 static __attribute__ ((noinline)) void
10038 bce_dump_rxp_state(struct bce_softc *sc, int regs)
10039 {
10040 	u32 val;
10041 	u32 fw_version[3];
10042 
10043 	BCE_PRINTF(
10044 		"----------------------------"
10045 		"   RXP  State   "
10046 		"----------------------------\n");
10047 
10048 	for (int i = 0; i < 3; i++)
10049 		fw_version[i] = htonl(REG_RD_IND(sc,
10050 			(BCE_RXP_SCRATCH + 0x10 + i * 4)));
10051 	BCE_PRINTF("Firmware version - %s\n", (char *) fw_version);
10052 
10053 	val = REG_RD_IND(sc, BCE_RXP_CPU_MODE);
10054 	BCE_PRINTF("0x%08X - (0x%06X) rxp_cpu_mode\n", val, BCE_RXP_CPU_MODE);
10055 
10056 	val = REG_RD_IND(sc, BCE_RXP_CPU_STATE);
10057 	BCE_PRINTF("0x%08X - (0x%06X) rxp_cpu_state\n", val, BCE_RXP_CPU_STATE);
10058 
10059 	val = REG_RD_IND(sc, BCE_RXP_CPU_EVENT_MASK);
10060 	BCE_PRINTF("0x%08X - (0x%06X) rxp_cpu_event_mask\n", val,
10061 		BCE_RXP_CPU_EVENT_MASK);
10062 
10063 	if (regs) {
10064 		BCE_PRINTF(
10065 			"----------------------------"
10066 			" Register  Dump "
10067 			"----------------------------\n");
10068 
10069 		for (int i = BCE_RXP_CPU_MODE; i < 0xe8fff; i += 0x10) {
10070 			/* Skip the big blank sapces */
10071 			if (i < 0xc5400 && i > 0xdffff)
10072 				BCE_PRINTF("0x%04X: 0x%08X 0x%08X 0x%08X 0x%08X\n",
10073 	 				i, REG_RD_IND(sc, i), REG_RD_IND(sc, i + 0x4),
10074 					REG_RD_IND(sc, i + 0x8), REG_RD_IND(sc, i + 0xC));
10075 		}
10076 	}
10077 
10078 	BCE_PRINTF(
10079 		"----------------------------"
10080 		"----------------"
10081 		"----------------------------\n");
10082 }
10083 
10084 
10085 /****************************************************************************/
10086 /* Prints out the TPAT processor state.                                     */
10087 /*                                                                          */
10088 /* Returns:                                                                 */
10089 /*   Nothing.                                                               */
10090 /****************************************************************************/
10091 static __attribute__ ((noinline)) void
10092 bce_dump_tpat_state(struct bce_softc *sc, int regs)
10093 {
10094 	u32 val;
10095 	u32 fw_version[3];
10096 
10097 	BCE_PRINTF(
10098 		"----------------------------"
10099 		"   TPAT State   "
10100 		"----------------------------\n");
10101 
10102 	for (int i = 0; i < 3; i++)
10103 		fw_version[i] = htonl(REG_RD_IND(sc,
10104 			(BCE_TPAT_SCRATCH + 0x410 + i * 4)));
10105 	BCE_PRINTF("Firmware version - %s\n", (char *) fw_version);
10106 
10107 	val = REG_RD_IND(sc, BCE_TPAT_CPU_MODE);
10108 	BCE_PRINTF("0x%08X - (0x%06X) tpat_cpu_mode\n", val, BCE_TPAT_CPU_MODE);
10109 
10110 	val = REG_RD_IND(sc, BCE_TPAT_CPU_STATE);
10111 	BCE_PRINTF("0x%08X - (0x%06X) tpat_cpu_state\n", val, BCE_TPAT_CPU_STATE);
10112 
10113 	val = REG_RD_IND(sc, BCE_TPAT_CPU_EVENT_MASK);
10114 	BCE_PRINTF("0x%08X - (0x%06X) tpat_cpu_event_mask\n", val,
10115 		BCE_TPAT_CPU_EVENT_MASK);
10116 
10117 	if (regs) {
10118 		BCE_PRINTF(
10119 			"----------------------------"
10120 			" Register  Dump "
10121 			"----------------------------\n");
10122 
10123 		for (int i = BCE_TPAT_CPU_MODE; i < 0xa3fff; i += 0x10) {
10124 			/* Skip the big blank spaces */
10125 			if (i < 0x854000 && i > 0x9ffff)
10126 				BCE_PRINTF("0x%04X: 0x%08X 0x%08X 0x%08X 0x%08X\n",
10127 					i, REG_RD_IND(sc, i), REG_RD_IND(sc, i + 0x4),
10128 					REG_RD_IND(sc, i + 0x8), REG_RD_IND(sc, i + 0xC));
10129 		}
10130 	}
10131 
10132 	BCE_PRINTF(
10133 		"----------------------------"
10134 		"----------------"
10135 		"----------------------------\n");
10136 }
10137 
10138 
10139 /****************************************************************************/
10140 /* Prints out the Command Procesor (CP) state.                              */
10141 /*                                                                          */
10142 /* Returns:                                                                 */
10143 /*   Nothing.                                                               */
10144 /****************************************************************************/
10145 static __attribute__ ((noinline)) void
10146 bce_dump_cp_state(struct bce_softc *sc, int regs)
10147 {
10148 	u32 val;
10149 	u32 fw_version[3];
10150 
10151 	BCE_PRINTF(
10152 		"----------------------------"
10153 		"    CP State    "
10154 		"----------------------------\n");
10155 
10156 	for (int i = 0; i < 3; i++)
10157 		fw_version[i] = htonl(REG_RD_IND(sc,
10158 			(BCE_CP_SCRATCH + 0x10 + i * 4)));
10159 	BCE_PRINTF("Firmware version - %s\n", (char *) fw_version);
10160 
10161 	val = REG_RD_IND(sc, BCE_CP_CPU_MODE);
10162 	BCE_PRINTF("0x%08X - (0x%06X) cp_cpu_mode\n", val, BCE_CP_CPU_MODE);
10163 
10164 	val = REG_RD_IND(sc, BCE_CP_CPU_STATE);
10165 	BCE_PRINTF("0x%08X - (0x%06X) cp_cpu_state\n", val, BCE_CP_CPU_STATE);
10166 
10167 	val = REG_RD_IND(sc, BCE_CP_CPU_EVENT_MASK);
10168 	BCE_PRINTF("0x%08X - (0x%06X) cp_cpu_event_mask\n", val,
10169 		BCE_CP_CPU_EVENT_MASK);
10170 
10171 	if (regs) {
10172 		BCE_PRINTF(
10173 			"----------------------------"
10174 			" Register  Dump "
10175 			"----------------------------\n");
10176 
10177 		for (int i = BCE_CP_CPU_MODE; i < 0x1aa000; i += 0x10) {
10178 			/* Skip the big blank spaces */
10179 			if (i < 0x185400 && i > 0x19ffff)
10180 				BCE_PRINTF("0x%04X: 0x%08X 0x%08X 0x%08X 0x%08X\n",
10181 					i, REG_RD_IND(sc, i), REG_RD_IND(sc, i + 0x4),
10182 					REG_RD_IND(sc, i + 0x8), REG_RD_IND(sc, i + 0xC));
10183 		}
10184 	}
10185 
10186 	BCE_PRINTF(
10187 		"----------------------------"
10188 		"----------------"
10189 		"----------------------------\n");
10190 }
10191 
10192 
10193 /****************************************************************************/
10194 /* Prints out the Completion Procesor (COM) state.                          */
10195 /*                                                                          */
10196 /* Returns:                                                                 */
10197 /*   Nothing.                                                               */
10198 /****************************************************************************/
10199 static __attribute__ ((noinline)) void
10200 bce_dump_com_state(struct bce_softc *sc, int regs)
10201 {
10202 	u32 val;
10203 	u32 fw_version[3];
10204 
10205 	BCE_PRINTF(
10206 		"----------------------------"
10207 		"   COM State    "
10208 		"----------------------------\n");
10209 
10210 	for (int i = 0; i < 3; i++)
10211 		fw_version[i] = htonl(REG_RD_IND(sc,
10212 			(BCE_COM_SCRATCH + 0x10 + i * 4)));
10213 	BCE_PRINTF("Firmware version - %s\n", (char *) fw_version);
10214 
10215 	val = REG_RD_IND(sc, BCE_COM_CPU_MODE);
10216 	BCE_PRINTF("0x%08X - (0x%06X) com_cpu_mode\n", val, BCE_COM_CPU_MODE);
10217 
10218 	val = REG_RD_IND(sc, BCE_COM_CPU_STATE);
10219 	BCE_PRINTF("0x%08X - (0x%06X) com_cpu_state\n", val, BCE_COM_CPU_STATE);
10220 
10221 	val = REG_RD_IND(sc, BCE_COM_CPU_EVENT_MASK);
10222 	BCE_PRINTF("0x%08X - (0x%06X) com_cpu_event_mask\n", val,
10223 		BCE_COM_CPU_EVENT_MASK);
10224 
10225 	if (regs) {
10226 		BCE_PRINTF(
10227 			"----------------------------"
10228 			" Register  Dump "
10229 			"----------------------------\n");
10230 
10231 		for (int i = BCE_COM_CPU_MODE; i < 0x1053e8; i += 0x10) {
10232 			BCE_PRINTF("0x%04X: 0x%08X 0x%08X 0x%08X 0x%08X\n",
10233 				i, REG_RD_IND(sc, i), REG_RD_IND(sc, i + 0x4),
10234 				REG_RD_IND(sc, i + 0x8), REG_RD_IND(sc, i + 0xC));
10235 		}
10236 	}
10237 
10238 	BCE_PRINTF(
10239 		"----------------------------"
10240 		"----------------"
10241 		"----------------------------\n");
10242 }
10243 
10244 
10245 /****************************************************************************/
10246 /* Prints out the driver state and then enters the debugger.                */
10247 /*                                                                          */
10248 /* Returns:                                                                 */
10249 /*   Nothing.                                                               */
10250 /****************************************************************************/
10251 static void
10252 bce_breakpoint(struct bce_softc *sc)
10253 {
10254 
10255 	/*
10256 	 * Unreachable code to silence compiler warnings
10257 	 * about unused functions.
10258 	 */
10259 	if (0) {
10260 		bce_freeze_controller(sc);
10261 		bce_unfreeze_controller(sc);
10262 		bce_dump_enet(sc, NULL);
10263    		bce_dump_txbd(sc, 0, NULL);
10264 		bce_dump_rxbd(sc, 0, NULL);
10265 		bce_dump_tx_mbuf_chain(sc, 0, USABLE_TX_BD);
10266 		bce_dump_rx_mbuf_chain(sc, 0, USABLE_RX_BD);
10267 		bce_dump_l2fhdr(sc, 0, NULL);
10268 		bce_dump_ctx(sc, RX_CID);
10269 		bce_dump_ftqs(sc);
10270 		bce_dump_tx_chain(sc, 0, USABLE_TX_BD);
10271 		bce_dump_rx_chain(sc, 0, USABLE_RX_BD);
10272 		bce_dump_status_block(sc);
10273 		bce_dump_stats_block(sc);
10274 		bce_dump_driver_state(sc);
10275 		bce_dump_hw_state(sc);
10276 		bce_dump_bc_state(sc);
10277 		bce_dump_txp_state(sc, 0);
10278 		bce_dump_rxp_state(sc, 0);
10279 		bce_dump_tpat_state(sc, 0);
10280 		bce_dump_cp_state(sc, 0);
10281 		bce_dump_com_state(sc, 0);
10282 #ifdef ZERO_COPY_SOCKETS
10283 		bce_dump_pgbd(sc, 0, NULL);
10284 		bce_dump_pg_mbuf_chain(sc, 0, USABLE_PG_BD);
10285 		bce_dump_pg_chain(sc, 0, USABLE_PG_BD);
10286 #endif
10287 	}
10288 
10289 	bce_dump_status_block(sc);
10290 	bce_dump_driver_state(sc);
10291 
10292 	/* Call the debugger. */
10293 	breakpoint();
10294 
10295 	return;
10296 }
10297 #endif
10298 
10299