xref: /freebsd/sys/dev/bge/if_bge.c (revision 5f06c5bb282cc40fefeab7f63a701e77b466553e)
1 /*-
2  * Copyright (c) 2001 Wind River Systems
3  * Copyright (c) 1997, 1998, 1999, 2001
4  *	Bill Paul <wpaul@windriver.com>.  All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
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. All advertising materials mentioning features or use of this software
15  *    must display the following acknowledgement:
16  *	This product includes software developed by Bill Paul.
17  * 4. Neither the name of the author nor the names of any co-contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD
25  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
31  * THE POSSIBILITY OF SUCH DAMAGE.
32  */
33 
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
36 
37 /*
38  * Broadcom BCM570x family gigabit ethernet driver for FreeBSD.
39  *
40  * The Broadcom BCM5700 is based on technology originally developed by
41  * Alteon Networks as part of the Tigon I and Tigon II gigabit ethernet
42  * MAC chips. The BCM5700, sometimes refered to as the Tigon III, has
43  * two on-board MIPS R4000 CPUs and can have as much as 16MB of external
44  * SSRAM. The BCM5700 supports TCP, UDP and IP checksum offload, jumbo
45  * frames, highly configurable RX filtering, and 16 RX and TX queues
46  * (which, along with RX filter rules, can be used for QOS applications).
47  * Other features, such as TCP segmentation, may be available as part
48  * of value-added firmware updates. Unlike the Tigon I and Tigon II,
49  * firmware images can be stored in hardware and need not be compiled
50  * into the driver.
51  *
52  * The BCM5700 supports the PCI v2.2 and PCI-X v1.0 standards, and will
53  * function in a 32-bit/64-bit 33/66Mhz bus, or a 64-bit/133Mhz bus.
54  *
55  * The BCM5701 is a single-chip solution incorporating both the BCM5700
56  * MAC and a BCM5401 10/100/1000 PHY. Unlike the BCM5700, the BCM5701
57  * does not support external SSRAM.
58  *
59  * Broadcom also produces a variation of the BCM5700 under the "Altima"
60  * brand name, which is functionally similar but lacks PCI-X support.
61  *
62  * Without external SSRAM, you can only have at most 4 TX rings,
63  * and the use of the mini RX ring is disabled. This seems to imply
64  * that these features are simply not available on the BCM5701. As a
65  * result, this driver does not implement any support for the mini RX
66  * ring.
67  */
68 
69 #ifdef HAVE_KERNEL_OPTION_HEADERS
70 #include "opt_device_polling.h"
71 #endif
72 
73 #include <sys/param.h>
74 #include <sys/endian.h>
75 #include <sys/systm.h>
76 #include <sys/sockio.h>
77 #include <sys/mbuf.h>
78 #include <sys/malloc.h>
79 #include <sys/kernel.h>
80 #include <sys/module.h>
81 #include <sys/socket.h>
82 #include <sys/sysctl.h>
83 
84 #include <net/if.h>
85 #include <net/if_arp.h>
86 #include <net/ethernet.h>
87 #include <net/if_dl.h>
88 #include <net/if_media.h>
89 
90 #include <net/bpf.h>
91 
92 #include <net/if_types.h>
93 #include <net/if_vlan_var.h>
94 
95 #include <netinet/in_systm.h>
96 #include <netinet/in.h>
97 #include <netinet/ip.h>
98 
99 #include <machine/bus.h>
100 #include <machine/resource.h>
101 #include <sys/bus.h>
102 #include <sys/rman.h>
103 
104 #include <dev/mii/mii.h>
105 #include <dev/mii/miivar.h>
106 #include "miidevs.h"
107 #include <dev/mii/brgphyreg.h>
108 
109 #ifdef __sparc64__
110 #include <dev/ofw/ofw_bus.h>
111 #include <dev/ofw/openfirm.h>
112 #include <machine/ofw_machdep.h>
113 #include <machine/ver.h>
114 #endif
115 
116 #include <dev/pci/pcireg.h>
117 #include <dev/pci/pcivar.h>
118 
119 #include <dev/bge/if_bgereg.h>
120 
121 #define	BGE_CSUM_FEATURES	(CSUM_IP | CSUM_TCP | CSUM_UDP)
122 #define	ETHER_MIN_NOPAD		(ETHER_MIN_LEN - ETHER_CRC_LEN) /* i.e., 60 */
123 
124 MODULE_DEPEND(bge, pci, 1, 1, 1);
125 MODULE_DEPEND(bge, ether, 1, 1, 1);
126 MODULE_DEPEND(bge, miibus, 1, 1, 1);
127 
128 /* "device miibus" required.  See GENERIC if you get errors here. */
129 #include "miibus_if.h"
130 
131 /*
132  * Various supported device vendors/types and their names. Note: the
133  * spec seems to indicate that the hardware still has Alteon's vendor
134  * ID burned into it, though it will always be overriden by the vendor
135  * ID in the EEPROM. Just to be safe, we cover all possibilities.
136  */
137 static struct bge_type {
138 	uint16_t	bge_vid;
139 	uint16_t	bge_did;
140 } bge_devs[] = {
141 	{ ALTEON_VENDORID,	ALTEON_DEVICEID_BCM5700 },
142 	{ ALTEON_VENDORID,	ALTEON_DEVICEID_BCM5701 },
143 
144 	{ ALTIMA_VENDORID,	ALTIMA_DEVICE_AC1000 },
145 	{ ALTIMA_VENDORID,	ALTIMA_DEVICE_AC1002 },
146 	{ ALTIMA_VENDORID,	ALTIMA_DEVICE_AC9100 },
147 
148 	{ APPLE_VENDORID,	APPLE_DEVICE_BCM5701 },
149 
150 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5700 },
151 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5701 },
152 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5702 },
153 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5702_ALT },
154 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5702X },
155 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5703 },
156 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5703_ALT },
157 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5703X },
158 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5704C },
159 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5704S },
160 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5704S_ALT },
161 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5705 },
162 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5705F },
163 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5705K },
164 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5705M },
165 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5705M_ALT },
166 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5714C },
167 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5714S },
168 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5715 },
169 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5715S },
170 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5720 },
171 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5721 },
172 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5722 },
173 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5750 },
174 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5750M },
175 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5751 },
176 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5751F },
177 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5751M },
178 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5752 },
179 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5752M },
180 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5753 },
181 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5753F },
182 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5753M },
183 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5754 },
184 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5754M },
185 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5755 },
186 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5755M },
187 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5780 },
188 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5780S },
189 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5781 },
190 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5782 },
191 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5786 },
192 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5787 },
193 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5787M },
194 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5788 },
195 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5789 },
196 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5901 },
197 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5901A2 },
198 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5903M },
199 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5906 },
200 	{ BCOM_VENDORID,	BCOM_DEVICEID_BCM5906M },
201 
202 	{ SK_VENDORID,		SK_DEVICEID_ALTIMA },
203 
204 	{ TC_VENDORID,		TC_DEVICEID_3C996 },
205 
206 	{ 0, 0 }
207 };
208 
209 static const struct bge_vendor {
210 	uint16_t	v_id;
211 	const char	*v_name;
212 } bge_vendors[] = {
213 	{ ALTEON_VENDORID,	"Alteon" },
214 	{ ALTIMA_VENDORID,	"Altima" },
215 	{ APPLE_VENDORID,	"Apple" },
216 	{ BCOM_VENDORID,	"Broadcom" },
217 	{ SK_VENDORID,		"SysKonnect" },
218 	{ TC_VENDORID,		"3Com" },
219 
220 	{ 0, NULL }
221 };
222 
223 static const struct bge_revision {
224 	uint32_t	br_chipid;
225 	const char	*br_name;
226 } bge_revisions[] = {
227 	{ BGE_CHIPID_BCM5700_A0,	"BCM5700 A0" },
228 	{ BGE_CHIPID_BCM5700_A1,	"BCM5700 A1" },
229 	{ BGE_CHIPID_BCM5700_B0,	"BCM5700 B0" },
230 	{ BGE_CHIPID_BCM5700_B1,	"BCM5700 B1" },
231 	{ BGE_CHIPID_BCM5700_B2,	"BCM5700 B2" },
232 	{ BGE_CHIPID_BCM5700_B3,	"BCM5700 B3" },
233 	{ BGE_CHIPID_BCM5700_ALTIMA,	"BCM5700 Altima" },
234 	{ BGE_CHIPID_BCM5700_C0,	"BCM5700 C0" },
235 	{ BGE_CHIPID_BCM5701_A0,	"BCM5701 A0" },
236 	{ BGE_CHIPID_BCM5701_B0,	"BCM5701 B0" },
237 	{ BGE_CHIPID_BCM5701_B2,	"BCM5701 B2" },
238 	{ BGE_CHIPID_BCM5701_B5,	"BCM5701 B5" },
239 	{ BGE_CHIPID_BCM5703_A0,	"BCM5703 A0" },
240 	{ BGE_CHIPID_BCM5703_A1,	"BCM5703 A1" },
241 	{ BGE_CHIPID_BCM5703_A2,	"BCM5703 A2" },
242 	{ BGE_CHIPID_BCM5703_A3,	"BCM5703 A3" },
243 	{ BGE_CHIPID_BCM5703_B0,	"BCM5703 B0" },
244 	{ BGE_CHIPID_BCM5704_A0,	"BCM5704 A0" },
245 	{ BGE_CHIPID_BCM5704_A1,	"BCM5704 A1" },
246 	{ BGE_CHIPID_BCM5704_A2,	"BCM5704 A2" },
247 	{ BGE_CHIPID_BCM5704_A3,	"BCM5704 A3" },
248 	{ BGE_CHIPID_BCM5704_B0,	"BCM5704 B0" },
249 	{ BGE_CHIPID_BCM5705_A0,	"BCM5705 A0" },
250 	{ BGE_CHIPID_BCM5705_A1,	"BCM5705 A1" },
251 	{ BGE_CHIPID_BCM5705_A2,	"BCM5705 A2" },
252 	{ BGE_CHIPID_BCM5705_A3,	"BCM5705 A3" },
253 	{ BGE_CHIPID_BCM5750_A0,	"BCM5750 A0" },
254 	{ BGE_CHIPID_BCM5750_A1,	"BCM5750 A1" },
255 	{ BGE_CHIPID_BCM5750_A3,	"BCM5750 A3" },
256 	{ BGE_CHIPID_BCM5750_B0,	"BCM5750 B0" },
257 	{ BGE_CHIPID_BCM5750_B1,	"BCM5750 B1" },
258 	{ BGE_CHIPID_BCM5750_C0,	"BCM5750 C0" },
259 	{ BGE_CHIPID_BCM5750_C1,	"BCM5750 C1" },
260 	{ BGE_CHIPID_BCM5750_C2,	"BCM5750 C2" },
261 	{ BGE_CHIPID_BCM5714_A0,	"BCM5714 A0" },
262 	{ BGE_CHIPID_BCM5752_A0,	"BCM5752 A0" },
263 	{ BGE_CHIPID_BCM5752_A1,	"BCM5752 A1" },
264 	{ BGE_CHIPID_BCM5752_A2,	"BCM5752 A2" },
265 	{ BGE_CHIPID_BCM5714_B0,	"BCM5714 B0" },
266 	{ BGE_CHIPID_BCM5714_B3,	"BCM5714 B3" },
267 	{ BGE_CHIPID_BCM5715_A0,	"BCM5715 A0" },
268 	{ BGE_CHIPID_BCM5715_A1,	"BCM5715 A1" },
269 	{ BGE_CHIPID_BCM5715_A3,	"BCM5715 A3" },
270 	{ BGE_CHIPID_BCM5755_A0,	"BCM5755 A0" },
271 	{ BGE_CHIPID_BCM5755_A1,	"BCM5755 A1" },
272 	{ BGE_CHIPID_BCM5755_A2,	"BCM5755 A2" },
273 	{ BGE_CHIPID_BCM5722_A0,	"BCM5722 A0" },
274 	/* 5754 and 5787 share the same ASIC ID */
275 	{ BGE_CHIPID_BCM5787_A0,	"BCM5754/5787 A0" },
276 	{ BGE_CHIPID_BCM5787_A1,	"BCM5754/5787 A1" },
277 	{ BGE_CHIPID_BCM5787_A2,	"BCM5754/5787 A2" },
278 	{ BGE_CHIPID_BCM5906_A1,	"BCM5906 A1" },
279 	{ BGE_CHIPID_BCM5906_A2,	"BCM5906 A2" },
280 
281 	{ 0, NULL }
282 };
283 
284 /*
285  * Some defaults for major revisions, so that newer steppings
286  * that we don't know about have a shot at working.
287  */
288 static const struct bge_revision bge_majorrevs[] = {
289 	{ BGE_ASICREV_BCM5700,		"unknown BCM5700" },
290 	{ BGE_ASICREV_BCM5701,		"unknown BCM5701" },
291 	{ BGE_ASICREV_BCM5703,		"unknown BCM5703" },
292 	{ BGE_ASICREV_BCM5704,		"unknown BCM5704" },
293 	{ BGE_ASICREV_BCM5705,		"unknown BCM5705" },
294 	{ BGE_ASICREV_BCM5750,		"unknown BCM5750" },
295 	{ BGE_ASICREV_BCM5714_A0,	"unknown BCM5714" },
296 	{ BGE_ASICREV_BCM5752,		"unknown BCM5752" },
297 	{ BGE_ASICREV_BCM5780,		"unknown BCM5780" },
298 	{ BGE_ASICREV_BCM5714,		"unknown BCM5714" },
299 	{ BGE_ASICREV_BCM5755,		"unknown BCM5755" },
300 	/* 5754 and 5787 share the same ASIC ID */
301 	{ BGE_ASICREV_BCM5787,		"unknown BCM5754/5787" },
302 	{ BGE_ASICREV_BCM5906,		"unknown BCM5906" },
303 
304 	{ 0, NULL }
305 };
306 
307 #define	BGE_IS_JUMBO_CAPABLE(sc)	((sc)->bge_flags & BGE_FLAG_JUMBO)
308 #define	BGE_IS_5700_FAMILY(sc)		((sc)->bge_flags & BGE_FLAG_5700_FAMILY)
309 #define	BGE_IS_5705_PLUS(sc)		((sc)->bge_flags & BGE_FLAG_5705_PLUS)
310 #define	BGE_IS_5714_FAMILY(sc)		((sc)->bge_flags & BGE_FLAG_5714_FAMILY)
311 #define	BGE_IS_575X_PLUS(sc)		((sc)->bge_flags & BGE_FLAG_575X_PLUS)
312 
313 const struct bge_revision * bge_lookup_rev(uint32_t);
314 const struct bge_vendor * bge_lookup_vendor(uint16_t);
315 
316 typedef int	(*bge_eaddr_fcn_t)(struct bge_softc *, uint8_t[]);
317 
318 static int bge_probe(device_t);
319 static int bge_attach(device_t);
320 static int bge_detach(device_t);
321 static int bge_suspend(device_t);
322 static int bge_resume(device_t);
323 static void bge_release_resources(struct bge_softc *);
324 static void bge_dma_map_addr(void *, bus_dma_segment_t *, int, int);
325 static int bge_dma_alloc(device_t);
326 static void bge_dma_free(struct bge_softc *);
327 
328 static int bge_get_eaddr_mem(struct bge_softc *, uint8_t[]);
329 static int bge_get_eaddr_nvram(struct bge_softc *, uint8_t[]);
330 static int bge_get_eaddr_eeprom(struct bge_softc *, uint8_t[]);
331 static int bge_get_eaddr(struct bge_softc *, uint8_t[]);
332 
333 static void bge_txeof(struct bge_softc *);
334 static void bge_rxeof(struct bge_softc *);
335 
336 static void bge_asf_driver_up (struct bge_softc *);
337 static void bge_tick(void *);
338 static void bge_stats_update(struct bge_softc *);
339 static void bge_stats_update_regs(struct bge_softc *);
340 static int bge_encap(struct bge_softc *, struct mbuf **, uint32_t *);
341 
342 static void bge_intr(void *);
343 static void bge_start_locked(struct ifnet *);
344 static void bge_start(struct ifnet *);
345 static int bge_ioctl(struct ifnet *, u_long, caddr_t);
346 static void bge_init_locked(struct bge_softc *);
347 static void bge_init(void *);
348 static void bge_stop(struct bge_softc *);
349 static void bge_watchdog(struct bge_softc *);
350 static void bge_shutdown(device_t);
351 static int bge_ifmedia_upd_locked(struct ifnet *);
352 static int bge_ifmedia_upd(struct ifnet *);
353 static void bge_ifmedia_sts(struct ifnet *, struct ifmediareq *);
354 
355 static uint8_t bge_nvram_getbyte(struct bge_softc *, int, uint8_t *);
356 static int bge_read_nvram(struct bge_softc *, caddr_t, int, int);
357 
358 static uint8_t bge_eeprom_getbyte(struct bge_softc *, int, uint8_t *);
359 static int bge_read_eeprom(struct bge_softc *, caddr_t, int, int);
360 
361 static void bge_setpromisc(struct bge_softc *);
362 static void bge_setmulti(struct bge_softc *);
363 static void bge_setvlan(struct bge_softc *);
364 
365 static int bge_newbuf_std(struct bge_softc *, int, struct mbuf *);
366 static int bge_newbuf_jumbo(struct bge_softc *, int, struct mbuf *);
367 static int bge_init_rx_ring_std(struct bge_softc *);
368 static void bge_free_rx_ring_std(struct bge_softc *);
369 static int bge_init_rx_ring_jumbo(struct bge_softc *);
370 static void bge_free_rx_ring_jumbo(struct bge_softc *);
371 static void bge_free_tx_ring(struct bge_softc *);
372 static int bge_init_tx_ring(struct bge_softc *);
373 
374 static int bge_chipinit(struct bge_softc *);
375 static int bge_blockinit(struct bge_softc *);
376 
377 static int bge_has_eeprom(struct bge_softc *);
378 static uint32_t bge_readmem_ind(struct bge_softc *, int);
379 static void bge_writemem_ind(struct bge_softc *, int, int);
380 static void bge_writembx(struct bge_softc *, int, int);
381 #ifdef notdef
382 static uint32_t bge_readreg_ind(struct bge_softc *, int);
383 #endif
384 static void bge_writemem_direct(struct bge_softc *, int, int);
385 static void bge_writereg_ind(struct bge_softc *, int, int);
386 
387 static int bge_miibus_readreg(device_t, int, int);
388 static int bge_miibus_writereg(device_t, int, int, int);
389 static void bge_miibus_statchg(device_t);
390 #ifdef DEVICE_POLLING
391 static void bge_poll(struct ifnet *ifp, enum poll_cmd cmd, int count);
392 #endif
393 
394 #define	BGE_RESET_START 1
395 #define	BGE_RESET_STOP  2
396 static void bge_sig_post_reset(struct bge_softc *, int);
397 static void bge_sig_legacy(struct bge_softc *, int);
398 static void bge_sig_pre_reset(struct bge_softc *, int);
399 static int bge_reset(struct bge_softc *);
400 static void bge_link_upd(struct bge_softc *);
401 
402 /*
403  * The BGE_REGISTER_DEBUG option is only for low-level debugging.  It may
404  * leak information to untrusted users.  It is also known to cause alignment
405  * traps on certain architectures.
406  */
407 #ifdef BGE_REGISTER_DEBUG
408 static int bge_sysctl_debug_info(SYSCTL_HANDLER_ARGS);
409 static int bge_sysctl_reg_read(SYSCTL_HANDLER_ARGS);
410 static int bge_sysctl_mem_read(SYSCTL_HANDLER_ARGS);
411 #endif
412 static void bge_add_sysctls(struct bge_softc *);
413 static int bge_sysctl_stats(SYSCTL_HANDLER_ARGS);
414 
415 static device_method_t bge_methods[] = {
416 	/* Device interface */
417 	DEVMETHOD(device_probe,		bge_probe),
418 	DEVMETHOD(device_attach,	bge_attach),
419 	DEVMETHOD(device_detach,	bge_detach),
420 	DEVMETHOD(device_shutdown,	bge_shutdown),
421 	DEVMETHOD(device_suspend,	bge_suspend),
422 	DEVMETHOD(device_resume,	bge_resume),
423 
424 	/* bus interface */
425 	DEVMETHOD(bus_print_child,	bus_generic_print_child),
426 	DEVMETHOD(bus_driver_added,	bus_generic_driver_added),
427 
428 	/* MII interface */
429 	DEVMETHOD(miibus_readreg,	bge_miibus_readreg),
430 	DEVMETHOD(miibus_writereg,	bge_miibus_writereg),
431 	DEVMETHOD(miibus_statchg,	bge_miibus_statchg),
432 
433 	{ 0, 0 }
434 };
435 
436 static driver_t bge_driver = {
437 	"bge",
438 	bge_methods,
439 	sizeof(struct bge_softc)
440 };
441 
442 static devclass_t bge_devclass;
443 
444 DRIVER_MODULE(bge, pci, bge_driver, bge_devclass, 0, 0);
445 DRIVER_MODULE(miibus, bge, miibus_driver, miibus_devclass, 0, 0);
446 
447 static int bge_allow_asf = 1;
448 
449 TUNABLE_INT("hw.bge.allow_asf", &bge_allow_asf);
450 
451 SYSCTL_NODE(_hw, OID_AUTO, bge, CTLFLAG_RD, 0, "BGE driver parameters");
452 SYSCTL_INT(_hw_bge, OID_AUTO, allow_asf, CTLFLAG_RD, &bge_allow_asf, 0,
453 	"Allow ASF mode if available");
454 
455 #define	SPARC64_BLADE_1500_MODEL	"SUNW,Sun-Blade-1500"
456 #define	SPARC64_BLADE_1500_PATH_BGE	"/pci@1f,700000/network@2"
457 #define	SPARC64_BLADE_2500_MODEL	"SUNW,Sun-Blade-2500"
458 #define	SPARC64_BLADE_2500_PATH_BGE	"/pci@1c,600000/network@3"
459 #define	SPARC64_OFW_SUBVENDOR		"subsystem-vendor-id"
460 
461 static int
462 bge_has_eeprom(struct bge_softc *sc)
463 {
464 #ifdef __sparc64__
465 	char buf[sizeof(SPARC64_BLADE_1500_PATH_BGE)];
466 	device_t dev;
467 	uint32_t subvendor;
468 
469 	dev = sc->bge_dev;
470 
471 	/*
472 	 * The on-board BGEs found in sun4u machines aren't fitted with
473 	 * an EEPROM which means that we have to obtain the MAC address
474 	 * via OFW and that some tests will always fail. We distinguish
475 	 * such BGEs by the subvendor ID, which also has to be obtained
476 	 * from OFW instead of the PCI configuration space as the latter
477 	 * indicates Broadcom as the subvendor of the netboot interface.
478 	 * For early Blade 1500 and 2500 we even have to check the OFW
479 	 * device path as the subvendor ID always defaults to Broadcom
480 	 * there.
481 	 */
482 	if (OF_getprop(ofw_bus_get_node(dev), SPARC64_OFW_SUBVENDOR,
483 	    &subvendor, sizeof(subvendor)) == sizeof(subvendor) &&
484 	    subvendor == SUN_VENDORID)
485 		return (0);
486 	memset(buf, 0, sizeof(buf));
487 	if (OF_package_to_path(ofw_bus_get_node(dev), buf, sizeof(buf)) > 0) {
488 		if (strcmp(sparc64_model, SPARC64_BLADE_1500_MODEL) == 0 &&
489 		    strcmp(buf, SPARC64_BLADE_1500_PATH_BGE) == 0)
490 			return (0);
491 		if (strcmp(sparc64_model, SPARC64_BLADE_2500_MODEL) == 0 &&
492 		    strcmp(buf, SPARC64_BLADE_2500_PATH_BGE) == 0)
493 			return (0);
494 	}
495 #endif
496 
497 	if (sc->bge_asicrev == BGE_ASICREV_BCM5906)
498 		return (0);
499 
500 	return (1);
501 }
502 
503 static uint32_t
504 bge_readmem_ind(struct bge_softc *sc, int off)
505 {
506 	device_t dev;
507 	uint32_t val;
508 
509 	dev = sc->bge_dev;
510 
511 	pci_write_config(dev, BGE_PCI_MEMWIN_BASEADDR, off, 4);
512 	val = pci_read_config(dev, BGE_PCI_MEMWIN_DATA, 4);
513 	pci_write_config(dev, BGE_PCI_MEMWIN_BASEADDR, 0, 4);
514 	return (val);
515 }
516 
517 static void
518 bge_writemem_ind(struct bge_softc *sc, int off, int val)
519 {
520 	device_t dev;
521 
522 	dev = sc->bge_dev;
523 
524 	pci_write_config(dev, BGE_PCI_MEMWIN_BASEADDR, off, 4);
525 	pci_write_config(dev, BGE_PCI_MEMWIN_DATA, val, 4);
526 	pci_write_config(dev, BGE_PCI_MEMWIN_BASEADDR, 0, 4);
527 }
528 
529 #ifdef notdef
530 static uint32_t
531 bge_readreg_ind(struct bge_softc *sc, int off)
532 {
533 	device_t dev;
534 
535 	dev = sc->bge_dev;
536 
537 	pci_write_config(dev, BGE_PCI_REG_BASEADDR, off, 4);
538 	return (pci_read_config(dev, BGE_PCI_REG_DATA, 4));
539 }
540 #endif
541 
542 static void
543 bge_writereg_ind(struct bge_softc *sc, int off, int val)
544 {
545 	device_t dev;
546 
547 	dev = sc->bge_dev;
548 
549 	pci_write_config(dev, BGE_PCI_REG_BASEADDR, off, 4);
550 	pci_write_config(dev, BGE_PCI_REG_DATA, val, 4);
551 }
552 
553 static void
554 bge_writemem_direct(struct bge_softc *sc, int off, int val)
555 {
556 	CSR_WRITE_4(sc, off, val);
557 }
558 
559 static void
560 bge_writembx(struct bge_softc *sc, int off, int val)
561 {
562 	if (sc->bge_asicrev == BGE_ASICREV_BCM5906)
563 		off += BGE_LPMBX_IRQ0_HI - BGE_MBX_IRQ0_HI;
564 
565 	CSR_WRITE_4(sc, off, val);
566 }
567 
568 /*
569  * Map a single buffer address.
570  */
571 
572 static void
573 bge_dma_map_addr(void *arg, bus_dma_segment_t *segs, int nseg, int error)
574 {
575 	struct bge_dmamap_arg *ctx;
576 
577 	if (error)
578 		return;
579 
580 	ctx = arg;
581 
582 	if (nseg > ctx->bge_maxsegs) {
583 		ctx->bge_maxsegs = 0;
584 		return;
585 	}
586 
587 	ctx->bge_busaddr = segs->ds_addr;
588 }
589 
590 static uint8_t
591 bge_nvram_getbyte(struct bge_softc *sc, int addr, uint8_t *dest)
592 {
593 	uint32_t access, byte = 0;
594 	int i;
595 
596 	/* Lock. */
597 	CSR_WRITE_4(sc, BGE_NVRAM_SWARB, BGE_NVRAMSWARB_SET1);
598 	for (i = 0; i < 8000; i++) {
599 		if (CSR_READ_4(sc, BGE_NVRAM_SWARB) & BGE_NVRAMSWARB_GNT1)
600 			break;
601 		DELAY(20);
602 	}
603 	if (i == 8000)
604 		return (1);
605 
606 	/* Enable access. */
607 	access = CSR_READ_4(sc, BGE_NVRAM_ACCESS);
608 	CSR_WRITE_4(sc, BGE_NVRAM_ACCESS, access | BGE_NVRAMACC_ENABLE);
609 
610 	CSR_WRITE_4(sc, BGE_NVRAM_ADDR, addr & 0xfffffffc);
611 	CSR_WRITE_4(sc, BGE_NVRAM_CMD, BGE_NVRAM_READCMD);
612 	for (i = 0; i < BGE_TIMEOUT * 10; i++) {
613 		DELAY(10);
614 		if (CSR_READ_4(sc, BGE_NVRAM_CMD) & BGE_NVRAMCMD_DONE) {
615 			DELAY(10);
616 			break;
617 		}
618 	}
619 
620 	if (i == BGE_TIMEOUT * 10) {
621 		if_printf(sc->bge_ifp, "nvram read timed out\n");
622 		return (1);
623 	}
624 
625 	/* Get result. */
626 	byte = CSR_READ_4(sc, BGE_NVRAM_RDDATA);
627 
628 	*dest = (bswap32(byte) >> ((addr % 4) * 8)) & 0xFF;
629 
630 	/* Disable access. */
631 	CSR_WRITE_4(sc, BGE_NVRAM_ACCESS, access);
632 
633 	/* Unlock. */
634 	CSR_WRITE_4(sc, BGE_NVRAM_SWARB, BGE_NVRAMSWARB_CLR1);
635 	CSR_READ_4(sc, BGE_NVRAM_SWARB);
636 
637 	return (0);
638 }
639 
640 /*
641  * Read a sequence of bytes from NVRAM.
642  */
643 static int
644 bge_read_nvram(struct bge_softc *sc, caddr_t dest, int off, int cnt)
645 {
646 	int err = 0, i;
647 	uint8_t byte = 0;
648 
649 	if (sc->bge_asicrev != BGE_ASICREV_BCM5906)
650 		return (1);
651 
652 	for (i = 0; i < cnt; i++) {
653 		err = bge_nvram_getbyte(sc, off + i, &byte);
654 		if (err)
655 			break;
656 		*(dest + i) = byte;
657 	}
658 
659 	return (err ? 1 : 0);
660 }
661 
662 /*
663  * Read a byte of data stored in the EEPROM at address 'addr.' The
664  * BCM570x supports both the traditional bitbang interface and an
665  * auto access interface for reading the EEPROM. We use the auto
666  * access method.
667  */
668 static uint8_t
669 bge_eeprom_getbyte(struct bge_softc *sc, int addr, uint8_t *dest)
670 {
671 	int i;
672 	uint32_t byte = 0;
673 
674 	/*
675 	 * Enable use of auto EEPROM access so we can avoid
676 	 * having to use the bitbang method.
677 	 */
678 	BGE_SETBIT(sc, BGE_MISC_LOCAL_CTL, BGE_MLC_AUTO_EEPROM);
679 
680 	/* Reset the EEPROM, load the clock period. */
681 	CSR_WRITE_4(sc, BGE_EE_ADDR,
682 	    BGE_EEADDR_RESET | BGE_EEHALFCLK(BGE_HALFCLK_384SCL));
683 	DELAY(20);
684 
685 	/* Issue the read EEPROM command. */
686 	CSR_WRITE_4(sc, BGE_EE_ADDR, BGE_EE_READCMD | addr);
687 
688 	/* Wait for completion */
689 	for(i = 0; i < BGE_TIMEOUT * 10; i++) {
690 		DELAY(10);
691 		if (CSR_READ_4(sc, BGE_EE_ADDR) & BGE_EEADDR_DONE)
692 			break;
693 	}
694 
695 	if (i == BGE_TIMEOUT * 10) {
696 		device_printf(sc->bge_dev, "EEPROM read timed out\n");
697 		return (1);
698 	}
699 
700 	/* Get result. */
701 	byte = CSR_READ_4(sc, BGE_EE_DATA);
702 
703 	*dest = (byte >> ((addr % 4) * 8)) & 0xFF;
704 
705 	return (0);
706 }
707 
708 /*
709  * Read a sequence of bytes from the EEPROM.
710  */
711 static int
712 bge_read_eeprom(struct bge_softc *sc, caddr_t dest, int off, int cnt)
713 {
714 	int i, error = 0;
715 	uint8_t byte = 0;
716 
717 	for (i = 0; i < cnt; i++) {
718 		error = bge_eeprom_getbyte(sc, off + i, &byte);
719 		if (error)
720 			break;
721 		*(dest + i) = byte;
722 	}
723 
724 	return (error ? 1 : 0);
725 }
726 
727 static int
728 bge_miibus_readreg(device_t dev, int phy, int reg)
729 {
730 	struct bge_softc *sc;
731 	uint32_t val, autopoll;
732 	int i;
733 
734 	sc = device_get_softc(dev);
735 
736 	/*
737 	 * Broadcom's own driver always assumes the internal
738 	 * PHY is at GMII address 1. On some chips, the PHY responds
739 	 * to accesses at all addresses, which could cause us to
740 	 * bogusly attach the PHY 32 times at probe type. Always
741 	 * restricting the lookup to address 1 is simpler than
742 	 * trying to figure out which chips revisions should be
743 	 * special-cased.
744 	 */
745 	if (phy != 1)
746 		return (0);
747 
748 	/* Reading with autopolling on may trigger PCI errors */
749 	autopoll = CSR_READ_4(sc, BGE_MI_MODE);
750 	if (autopoll & BGE_MIMODE_AUTOPOLL) {
751 		BGE_CLRBIT(sc, BGE_MI_MODE, BGE_MIMODE_AUTOPOLL);
752 		DELAY(40);
753 	}
754 
755 	CSR_WRITE_4(sc, BGE_MI_COMM, BGE_MICMD_READ | BGE_MICOMM_BUSY |
756 	    BGE_MIPHY(phy) | BGE_MIREG(reg));
757 
758 	for (i = 0; i < BGE_TIMEOUT; i++) {
759 		DELAY(10);
760 		val = CSR_READ_4(sc, BGE_MI_COMM);
761 		if (!(val & BGE_MICOMM_BUSY))
762 			break;
763 	}
764 
765 	if (i == BGE_TIMEOUT) {
766 		device_printf(sc->bge_dev, "PHY read timed out "
767 			  "(phy %d, reg %d, val 0x%08x)\n", phy, reg, val);
768 		val = 0;
769 		goto done;
770 	}
771 
772 	DELAY(5);
773 	val = CSR_READ_4(sc, BGE_MI_COMM);
774 
775 done:
776 	if (autopoll & BGE_MIMODE_AUTOPOLL) {
777 		BGE_SETBIT(sc, BGE_MI_MODE, BGE_MIMODE_AUTOPOLL);
778 		DELAY(40);
779 	}
780 
781 	if (val & BGE_MICOMM_READFAIL)
782 		return (0);
783 
784 	return (val & 0xFFFF);
785 }
786 
787 static int
788 bge_miibus_writereg(device_t dev, int phy, int reg, int val)
789 {
790 	struct bge_softc *sc;
791 	uint32_t autopoll;
792 	int i;
793 
794 	sc = device_get_softc(dev);
795 
796 	if (sc->bge_asicrev == BGE_ASICREV_BCM5906 &&
797 	    (reg == BRGPHY_MII_1000CTL || reg == BRGPHY_MII_AUXCTL))
798 		return(0);
799 
800 	/* Reading with autopolling on may trigger PCI errors */
801 	autopoll = CSR_READ_4(sc, BGE_MI_MODE);
802 	if (autopoll & BGE_MIMODE_AUTOPOLL) {
803 		BGE_CLRBIT(sc, BGE_MI_MODE, BGE_MIMODE_AUTOPOLL);
804 		DELAY(40);
805 	}
806 
807 	CSR_WRITE_4(sc, BGE_MI_COMM, BGE_MICMD_WRITE | BGE_MICOMM_BUSY |
808 	    BGE_MIPHY(phy) | BGE_MIREG(reg) | val);
809 
810 	for (i = 0; i < BGE_TIMEOUT; i++) {
811 		DELAY(10);
812 		if (!(CSR_READ_4(sc, BGE_MI_COMM) & BGE_MICOMM_BUSY)) {
813 			DELAY(5);
814 			CSR_READ_4(sc, BGE_MI_COMM); /* dummy read */
815 			break;
816 		}
817 	}
818 
819 	if (i == BGE_TIMEOUT) {
820 		device_printf(sc->bge_dev,
821 			  "PHY write timed out (phy %d, reg %d, val %d)\n",
822 			  phy, reg, val);
823 		return (0);
824 	}
825 
826 	if (autopoll & BGE_MIMODE_AUTOPOLL) {
827 		BGE_SETBIT(sc, BGE_MI_MODE, BGE_MIMODE_AUTOPOLL);
828 		DELAY(40);
829 	}
830 
831 	return (0);
832 }
833 
834 static void
835 bge_miibus_statchg(device_t dev)
836 {
837 	struct bge_softc *sc;
838 	struct mii_data *mii;
839 	sc = device_get_softc(dev);
840 	mii = device_get_softc(sc->bge_miibus);
841 
842 	BGE_CLRBIT(sc, BGE_MAC_MODE, BGE_MACMODE_PORTMODE);
843 	if (IFM_SUBTYPE(mii->mii_media_active) == IFM_1000_T)
844 		BGE_SETBIT(sc, BGE_MAC_MODE, BGE_PORTMODE_GMII);
845 	else
846 		BGE_SETBIT(sc, BGE_MAC_MODE, BGE_PORTMODE_MII);
847 
848 	if ((mii->mii_media_active & IFM_GMASK) == IFM_FDX)
849 		BGE_CLRBIT(sc, BGE_MAC_MODE, BGE_MACMODE_HALF_DUPLEX);
850 	else
851 		BGE_SETBIT(sc, BGE_MAC_MODE, BGE_MACMODE_HALF_DUPLEX);
852 }
853 
854 /*
855  * Intialize a standard receive ring descriptor.
856  */
857 static int
858 bge_newbuf_std(struct bge_softc *sc, int i, struct mbuf *m)
859 {
860 	struct mbuf *m_new = NULL;
861 	struct bge_rx_bd *r;
862 	struct bge_dmamap_arg ctx;
863 	int error;
864 
865 	if (m == NULL) {
866 		m_new = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR);
867 		if (m_new == NULL)
868 			return (ENOBUFS);
869 		m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
870 	} else {
871 		m_new = m;
872 		m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
873 		m_new->m_data = m_new->m_ext.ext_buf;
874 	}
875 
876 	if ((sc->bge_flags & BGE_FLAG_RX_ALIGNBUG) == 0)
877 		m_adj(m_new, ETHER_ALIGN);
878 	sc->bge_cdata.bge_rx_std_chain[i] = m_new;
879 	r = &sc->bge_ldata.bge_rx_std_ring[i];
880 	ctx.bge_maxsegs = 1;
881 	ctx.sc = sc;
882 	error = bus_dmamap_load(sc->bge_cdata.bge_mtag,
883 	    sc->bge_cdata.bge_rx_std_dmamap[i], mtod(m_new, void *),
884 	    m_new->m_len, bge_dma_map_addr, &ctx, BUS_DMA_NOWAIT);
885 	if (error || ctx.bge_maxsegs == 0) {
886 		if (m == NULL) {
887 			sc->bge_cdata.bge_rx_std_chain[i] = NULL;
888 			m_freem(m_new);
889 		}
890 		return (ENOMEM);
891 	}
892 	r->bge_addr.bge_addr_lo = BGE_ADDR_LO(ctx.bge_busaddr);
893 	r->bge_addr.bge_addr_hi = BGE_ADDR_HI(ctx.bge_busaddr);
894 	r->bge_flags = BGE_RXBDFLAG_END;
895 	r->bge_len = m_new->m_len;
896 	r->bge_idx = i;
897 
898 	bus_dmamap_sync(sc->bge_cdata.bge_mtag,
899 	    sc->bge_cdata.bge_rx_std_dmamap[i],
900 	    BUS_DMASYNC_PREREAD);
901 
902 	return (0);
903 }
904 
905 /*
906  * Initialize a jumbo receive ring descriptor. This allocates
907  * a jumbo buffer from the pool managed internally by the driver.
908  */
909 static int
910 bge_newbuf_jumbo(struct bge_softc *sc, int i, struct mbuf *m)
911 {
912 	bus_dma_segment_t segs[BGE_NSEG_JUMBO];
913 	struct bge_extrx_bd *r;
914 	struct mbuf *m_new = NULL;
915 	int nsegs;
916 	int error;
917 
918 	if (m == NULL) {
919 		MGETHDR(m_new, M_DONTWAIT, MT_DATA);
920 		if (m_new == NULL)
921 			return (ENOBUFS);
922 
923 		m_cljget(m_new, M_DONTWAIT, MJUM9BYTES);
924 		if (!(m_new->m_flags & M_EXT)) {
925 			m_freem(m_new);
926 			return (ENOBUFS);
927 		}
928 		m_new->m_len = m_new->m_pkthdr.len = MJUM9BYTES;
929 	} else {
930 		m_new = m;
931 		m_new->m_len = m_new->m_pkthdr.len = MJUM9BYTES;
932 		m_new->m_data = m_new->m_ext.ext_buf;
933 	}
934 
935 	if ((sc->bge_flags & BGE_FLAG_RX_ALIGNBUG) == 0)
936 		m_adj(m_new, ETHER_ALIGN);
937 
938 	error = bus_dmamap_load_mbuf_sg(sc->bge_cdata.bge_mtag_jumbo,
939 	    sc->bge_cdata.bge_rx_jumbo_dmamap[i],
940 	    m_new, segs, &nsegs, BUS_DMA_NOWAIT);
941 	if (error) {
942 		if (m == NULL)
943 			m_freem(m_new);
944 		return (error);
945 	}
946 	sc->bge_cdata.bge_rx_jumbo_chain[i] = m_new;
947 
948 	/*
949 	 * Fill in the extended RX buffer descriptor.
950 	 */
951 	r = &sc->bge_ldata.bge_rx_jumbo_ring[i];
952 	r->bge_flags = BGE_RXBDFLAG_JUMBO_RING | BGE_RXBDFLAG_END;
953 	r->bge_idx = i;
954 	r->bge_len3 = r->bge_len2 = r->bge_len1 = 0;
955 	switch (nsegs) {
956 	case 4:
957 		r->bge_addr3.bge_addr_lo = BGE_ADDR_LO(segs[3].ds_addr);
958 		r->bge_addr3.bge_addr_hi = BGE_ADDR_HI(segs[3].ds_addr);
959 		r->bge_len3 = segs[3].ds_len;
960 	case 3:
961 		r->bge_addr2.bge_addr_lo = BGE_ADDR_LO(segs[2].ds_addr);
962 		r->bge_addr2.bge_addr_hi = BGE_ADDR_HI(segs[2].ds_addr);
963 		r->bge_len2 = segs[2].ds_len;
964 	case 2:
965 		r->bge_addr1.bge_addr_lo = BGE_ADDR_LO(segs[1].ds_addr);
966 		r->bge_addr1.bge_addr_hi = BGE_ADDR_HI(segs[1].ds_addr);
967 		r->bge_len1 = segs[1].ds_len;
968 	case 1:
969 		r->bge_addr0.bge_addr_lo = BGE_ADDR_LO(segs[0].ds_addr);
970 		r->bge_addr0.bge_addr_hi = BGE_ADDR_HI(segs[0].ds_addr);
971 		r->bge_len0 = segs[0].ds_len;
972 		break;
973 	default:
974 		panic("%s: %d segments\n", __func__, nsegs);
975 	}
976 
977 	bus_dmamap_sync(sc->bge_cdata.bge_mtag,
978 	    sc->bge_cdata.bge_rx_jumbo_dmamap[i],
979 	    BUS_DMASYNC_PREREAD);
980 
981 	return (0);
982 }
983 
984 /*
985  * The standard receive ring has 512 entries in it. At 2K per mbuf cluster,
986  * that's 1MB or memory, which is a lot. For now, we fill only the first
987  * 256 ring entries and hope that our CPU is fast enough to keep up with
988  * the NIC.
989  */
990 static int
991 bge_init_rx_ring_std(struct bge_softc *sc)
992 {
993 	int i;
994 
995 	for (i = 0; i < BGE_SSLOTS; i++) {
996 		if (bge_newbuf_std(sc, i, NULL) == ENOBUFS)
997 			return (ENOBUFS);
998 	};
999 
1000 	bus_dmamap_sync(sc->bge_cdata.bge_rx_std_ring_tag,
1001 	    sc->bge_cdata.bge_rx_std_ring_map,
1002 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
1003 
1004 	sc->bge_std = i - 1;
1005 	bge_writembx(sc, BGE_MBX_RX_STD_PROD_LO, sc->bge_std);
1006 
1007 	return (0);
1008 }
1009 
1010 static void
1011 bge_free_rx_ring_std(struct bge_softc *sc)
1012 {
1013 	int i;
1014 
1015 	for (i = 0; i < BGE_STD_RX_RING_CNT; i++) {
1016 		if (sc->bge_cdata.bge_rx_std_chain[i] != NULL) {
1017 			bus_dmamap_sync(sc->bge_cdata.bge_mtag,
1018 			    sc->bge_cdata.bge_rx_std_dmamap[i],
1019 			    BUS_DMASYNC_POSTREAD);
1020 			bus_dmamap_unload(sc->bge_cdata.bge_mtag,
1021 			    sc->bge_cdata.bge_rx_std_dmamap[i]);
1022 			m_freem(sc->bge_cdata.bge_rx_std_chain[i]);
1023 			sc->bge_cdata.bge_rx_std_chain[i] = NULL;
1024 		}
1025 		bzero((char *)&sc->bge_ldata.bge_rx_std_ring[i],
1026 		    sizeof(struct bge_rx_bd));
1027 	}
1028 }
1029 
1030 static int
1031 bge_init_rx_ring_jumbo(struct bge_softc *sc)
1032 {
1033 	struct bge_rcb *rcb;
1034 	int i;
1035 
1036 	for (i = 0; i < BGE_JUMBO_RX_RING_CNT; i++) {
1037 		if (bge_newbuf_jumbo(sc, i, NULL) == ENOBUFS)
1038 			return (ENOBUFS);
1039 	};
1040 
1041 	bus_dmamap_sync(sc->bge_cdata.bge_rx_jumbo_ring_tag,
1042 	    sc->bge_cdata.bge_rx_jumbo_ring_map,
1043 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
1044 
1045 	sc->bge_jumbo = i - 1;
1046 
1047 	rcb = &sc->bge_ldata.bge_info.bge_jumbo_rx_rcb;
1048 	rcb->bge_maxlen_flags = BGE_RCB_MAXLEN_FLAGS(0,
1049 				    BGE_RCB_FLAG_USE_EXT_RX_BD);
1050 	CSR_WRITE_4(sc, BGE_RX_JUMBO_RCB_MAXLEN_FLAGS, rcb->bge_maxlen_flags);
1051 
1052 	bge_writembx(sc, BGE_MBX_RX_JUMBO_PROD_LO, sc->bge_jumbo);
1053 
1054 	return (0);
1055 }
1056 
1057 static void
1058 bge_free_rx_ring_jumbo(struct bge_softc *sc)
1059 {
1060 	int i;
1061 
1062 	for (i = 0; i < BGE_JUMBO_RX_RING_CNT; i++) {
1063 		if (sc->bge_cdata.bge_rx_jumbo_chain[i] != NULL) {
1064 			bus_dmamap_sync(sc->bge_cdata.bge_mtag_jumbo,
1065 			    sc->bge_cdata.bge_rx_jumbo_dmamap[i],
1066 			    BUS_DMASYNC_POSTREAD);
1067 			bus_dmamap_unload(sc->bge_cdata.bge_mtag_jumbo,
1068 			    sc->bge_cdata.bge_rx_jumbo_dmamap[i]);
1069 			m_freem(sc->bge_cdata.bge_rx_jumbo_chain[i]);
1070 			sc->bge_cdata.bge_rx_jumbo_chain[i] = NULL;
1071 		}
1072 		bzero((char *)&sc->bge_ldata.bge_rx_jumbo_ring[i],
1073 		    sizeof(struct bge_extrx_bd));
1074 	}
1075 }
1076 
1077 static void
1078 bge_free_tx_ring(struct bge_softc *sc)
1079 {
1080 	int i;
1081 
1082 	if (sc->bge_ldata.bge_tx_ring == NULL)
1083 		return;
1084 
1085 	for (i = 0; i < BGE_TX_RING_CNT; i++) {
1086 		if (sc->bge_cdata.bge_tx_chain[i] != NULL) {
1087 			bus_dmamap_sync(sc->bge_cdata.bge_mtag,
1088 			    sc->bge_cdata.bge_tx_dmamap[i],
1089 			    BUS_DMASYNC_POSTWRITE);
1090 			bus_dmamap_unload(sc->bge_cdata.bge_mtag,
1091 			    sc->bge_cdata.bge_tx_dmamap[i]);
1092 			m_freem(sc->bge_cdata.bge_tx_chain[i]);
1093 			sc->bge_cdata.bge_tx_chain[i] = NULL;
1094 		}
1095 		bzero((char *)&sc->bge_ldata.bge_tx_ring[i],
1096 		    sizeof(struct bge_tx_bd));
1097 	}
1098 }
1099 
1100 static int
1101 bge_init_tx_ring(struct bge_softc *sc)
1102 {
1103 	sc->bge_txcnt = 0;
1104 	sc->bge_tx_saved_considx = 0;
1105 
1106 	/* Initialize transmit producer index for host-memory send ring. */
1107 	sc->bge_tx_prodidx = 0;
1108 	bge_writembx(sc, BGE_MBX_TX_HOST_PROD0_LO, sc->bge_tx_prodidx);
1109 
1110 	/* 5700 b2 errata */
1111 	if (sc->bge_chiprev == BGE_CHIPREV_5700_BX)
1112 		bge_writembx(sc, BGE_MBX_TX_HOST_PROD0_LO, sc->bge_tx_prodidx);
1113 
1114 	/* NIC-memory send ring not used; initialize to zero. */
1115 	bge_writembx(sc, BGE_MBX_TX_NIC_PROD0_LO, 0);
1116 	/* 5700 b2 errata */
1117 	if (sc->bge_chiprev == BGE_CHIPREV_5700_BX)
1118 		bge_writembx(sc, BGE_MBX_TX_NIC_PROD0_LO, 0);
1119 
1120 	return (0);
1121 }
1122 
1123 static void
1124 bge_setpromisc(struct bge_softc *sc)
1125 {
1126 	struct ifnet *ifp;
1127 
1128 	BGE_LOCK_ASSERT(sc);
1129 
1130 	ifp = sc->bge_ifp;
1131 
1132 	/* Enable or disable promiscuous mode as needed. */
1133 	if (ifp->if_flags & IFF_PROMISC)
1134 		BGE_SETBIT(sc, BGE_RX_MODE, BGE_RXMODE_RX_PROMISC);
1135 	else
1136 		BGE_CLRBIT(sc, BGE_RX_MODE, BGE_RXMODE_RX_PROMISC);
1137 }
1138 
1139 static void
1140 bge_setmulti(struct bge_softc *sc)
1141 {
1142 	struct ifnet *ifp;
1143 	struct ifmultiaddr *ifma;
1144 	uint32_t hashes[4] = { 0, 0, 0, 0 };
1145 	int h, i;
1146 
1147 	BGE_LOCK_ASSERT(sc);
1148 
1149 	ifp = sc->bge_ifp;
1150 
1151 	if (ifp->if_flags & IFF_ALLMULTI || ifp->if_flags & IFF_PROMISC) {
1152 		for (i = 0; i < 4; i++)
1153 			CSR_WRITE_4(sc, BGE_MAR0 + (i * 4), 0xFFFFFFFF);
1154 		return;
1155 	}
1156 
1157 	/* First, zot all the existing filters. */
1158 	for (i = 0; i < 4; i++)
1159 		CSR_WRITE_4(sc, BGE_MAR0 + (i * 4), 0);
1160 
1161 	/* Now program new ones. */
1162 	IF_ADDR_LOCK(ifp);
1163 	TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
1164 		if (ifma->ifma_addr->sa_family != AF_LINK)
1165 			continue;
1166 		h = ether_crc32_le(LLADDR((struct sockaddr_dl *)
1167 		    ifma->ifma_addr), ETHER_ADDR_LEN) & 0x7F;
1168 		hashes[(h & 0x60) >> 5] |= 1 << (h & 0x1F);
1169 	}
1170 	IF_ADDR_UNLOCK(ifp);
1171 
1172 	for (i = 0; i < 4; i++)
1173 		CSR_WRITE_4(sc, BGE_MAR0 + (i * 4), hashes[i]);
1174 }
1175 
1176 static void
1177 bge_setvlan(struct bge_softc *sc)
1178 {
1179 	struct ifnet *ifp;
1180 
1181 	BGE_LOCK_ASSERT(sc);
1182 
1183 	ifp = sc->bge_ifp;
1184 
1185 	/* Enable or disable VLAN tag stripping as needed. */
1186 	if (ifp->if_capenable & IFCAP_VLAN_HWTAGGING)
1187 		BGE_CLRBIT(sc, BGE_RX_MODE, BGE_RXMODE_RX_KEEP_VLAN_DIAG);
1188 	else
1189 		BGE_SETBIT(sc, BGE_RX_MODE, BGE_RXMODE_RX_KEEP_VLAN_DIAG);
1190 }
1191 
1192 static void
1193 bge_sig_pre_reset(sc, type)
1194 	struct bge_softc *sc;
1195 	int type;
1196 {
1197 	/*
1198 	 * Some chips don't like this so only do this if ASF is enabled
1199 	 */
1200 	if (sc->bge_asf_mode)
1201 		bge_writemem_ind(sc, BGE_SOFTWARE_GENCOMM, BGE_MAGIC_NUMBER);
1202 
1203 	if (sc->bge_asf_mode & ASF_NEW_HANDSHAKE) {
1204 		switch (type) {
1205 		case BGE_RESET_START:
1206 			bge_writemem_ind(sc, BGE_SDI_STATUS, 0x1); /* START */
1207 			break;
1208 		case BGE_RESET_STOP:
1209 			bge_writemem_ind(sc, BGE_SDI_STATUS, 0x2); /* UNLOAD */
1210 			break;
1211 		}
1212 	}
1213 }
1214 
1215 static void
1216 bge_sig_post_reset(sc, type)
1217 	struct bge_softc *sc;
1218 	int type;
1219 {
1220 	if (sc->bge_asf_mode & ASF_NEW_HANDSHAKE) {
1221 		switch (type) {
1222 		case BGE_RESET_START:
1223 			bge_writemem_ind(sc, BGE_SDI_STATUS, 0x80000001);
1224 			/* START DONE */
1225 			break;
1226 		case BGE_RESET_STOP:
1227 			bge_writemem_ind(sc, BGE_SDI_STATUS, 0x80000002);
1228 			break;
1229 		}
1230 	}
1231 }
1232 
1233 static void
1234 bge_sig_legacy(sc, type)
1235 	struct bge_softc *sc;
1236 	int type;
1237 {
1238 	if (sc->bge_asf_mode) {
1239 		switch (type) {
1240 		case BGE_RESET_START:
1241 			bge_writemem_ind(sc, BGE_SDI_STATUS, 0x1); /* START */
1242 			break;
1243 		case BGE_RESET_STOP:
1244 			bge_writemem_ind(sc, BGE_SDI_STATUS, 0x2); /* UNLOAD */
1245 			break;
1246 		}
1247 	}
1248 }
1249 
1250 void bge_stop_fw(struct bge_softc *);
1251 void
1252 bge_stop_fw(sc)
1253 	struct bge_softc *sc;
1254 {
1255 	int i;
1256 
1257 	if (sc->bge_asf_mode) {
1258 		bge_writemem_ind(sc, BGE_SOFTWARE_GENCOMM_FW, BGE_FW_PAUSE);
1259 		CSR_WRITE_4(sc, BGE_CPU_EVENT,
1260 		    CSR_READ_4(sc, BGE_CPU_EVENT) | (1 << 14));
1261 
1262 		for (i = 0; i < 100; i++ ) {
1263 			if (!(CSR_READ_4(sc, BGE_CPU_EVENT) & (1 << 14)))
1264 				break;
1265 			DELAY(10);
1266 		}
1267 	}
1268 }
1269 
1270 /*
1271  * Do endian, PCI and DMA initialization. Also check the on-board ROM
1272  * self-test results.
1273  */
1274 static int
1275 bge_chipinit(struct bge_softc *sc)
1276 {
1277 	uint32_t dma_rw_ctl;
1278 	int i;
1279 
1280 	/* Set endianness before we access any non-PCI registers. */
1281 	pci_write_config(sc->bge_dev, BGE_PCI_MISC_CTL, BGE_INIT, 4);
1282 
1283 	/*
1284 	 * Check the 'ROM failed' bit on the RX CPU to see if
1285 	 * self-tests passed. Skip this check when there's no
1286 	 * EEPROM fitted, since in that case it will always
1287 	 * fail.
1288 	 */
1289 	if ((sc->bge_flags & BGE_FLAG_EEPROM) &&
1290 	    CSR_READ_4(sc, BGE_RXCPU_MODE) & BGE_RXCPUMODE_ROMFAIL) {
1291 		device_printf(sc->bge_dev, "RX CPU self-diagnostics failed!\n");
1292 		return (ENODEV);
1293 	}
1294 
1295 	/* Clear the MAC control register */
1296 	CSR_WRITE_4(sc, BGE_MAC_MODE, 0);
1297 
1298 	/*
1299 	 * Clear the MAC statistics block in the NIC's
1300 	 * internal memory.
1301 	 */
1302 	for (i = BGE_STATS_BLOCK;
1303 	    i < BGE_STATS_BLOCK_END + 1; i += sizeof(uint32_t))
1304 		BGE_MEMWIN_WRITE(sc, i, 0);
1305 
1306 	for (i = BGE_STATUS_BLOCK;
1307 	    i < BGE_STATUS_BLOCK_END + 1; i += sizeof(uint32_t))
1308 		BGE_MEMWIN_WRITE(sc, i, 0);
1309 
1310 	/*
1311 	 * Set up the PCI DMA control register.
1312 	 */
1313 	dma_rw_ctl = BGE_PCIDMARWCTL_RD_CMD_SHIFT(6) |
1314 	    BGE_PCIDMARWCTL_WR_CMD_SHIFT(7);
1315 	if (sc->bge_flags & BGE_FLAG_PCIE) {
1316 		/* Read watermark not used, 128 bytes for write. */
1317 		dma_rw_ctl |= BGE_PCIDMARWCTL_WR_WAT_SHIFT(3);
1318 	} else if (sc->bge_flags & BGE_FLAG_PCIX) {
1319 		if (BGE_IS_5714_FAMILY(sc)) {
1320 			/* 256 bytes for read and write. */
1321 			dma_rw_ctl |= BGE_PCIDMARWCTL_RD_WAT_SHIFT(2) |
1322 			    BGE_PCIDMARWCTL_WR_WAT_SHIFT(2);
1323 			dma_rw_ctl |= (sc->bge_asicrev == BGE_ASICREV_BCM5780) ?
1324 			    BGE_PCIDMARWCTL_ONEDMA_ATONCE_GLOBAL :
1325 			    BGE_PCIDMARWCTL_ONEDMA_ATONCE_LOCAL;
1326 		} else if (sc->bge_asicrev == BGE_ASICREV_BCM5704) {
1327 			/* 1536 bytes for read, 384 bytes for write. */
1328 			dma_rw_ctl |= BGE_PCIDMARWCTL_RD_WAT_SHIFT(7) |
1329 			    BGE_PCIDMARWCTL_WR_WAT_SHIFT(3);
1330 		} else {
1331 			/* 384 bytes for read and write. */
1332 			dma_rw_ctl |= BGE_PCIDMARWCTL_RD_WAT_SHIFT(3) |
1333 			    BGE_PCIDMARWCTL_WR_WAT_SHIFT(3) |
1334 			    0x0F;
1335 		}
1336 		if (sc->bge_asicrev == BGE_ASICREV_BCM5703 ||
1337 		    sc->bge_asicrev == BGE_ASICREV_BCM5704) {
1338 			uint32_t tmp;
1339 
1340 			/* Set ONE_DMA_AT_ONCE for hardware workaround. */
1341 			tmp = CSR_READ_4(sc, BGE_PCI_CLKCTL) & 0x1F;
1342 			if (tmp == 6 || tmp == 7)
1343 				dma_rw_ctl |=
1344 				    BGE_PCIDMARWCTL_ONEDMA_ATONCE_GLOBAL;
1345 
1346 			/* Set PCI-X DMA write workaround. */
1347 			dma_rw_ctl |= BGE_PCIDMARWCTL_ASRT_ALL_BE;
1348 		}
1349 	} else {
1350 		/* Conventional PCI bus: 256 bytes for read and write. */
1351 		dma_rw_ctl |= BGE_PCIDMARWCTL_RD_WAT_SHIFT(7) |
1352 		    BGE_PCIDMARWCTL_WR_WAT_SHIFT(7);
1353 
1354 		if (sc->bge_asicrev != BGE_ASICREV_BCM5705 &&
1355 		    sc->bge_asicrev != BGE_ASICREV_BCM5750)
1356 			dma_rw_ctl |= 0x0F;
1357 	}
1358 	if (sc->bge_asicrev == BGE_ASICREV_BCM5700 ||
1359 	    sc->bge_asicrev == BGE_ASICREV_BCM5701)
1360 		dma_rw_ctl |= BGE_PCIDMARWCTL_USE_MRM |
1361 		    BGE_PCIDMARWCTL_ASRT_ALL_BE;
1362 	if (sc->bge_asicrev == BGE_ASICREV_BCM5703 ||
1363 	    sc->bge_asicrev == BGE_ASICREV_BCM5704)
1364 		dma_rw_ctl &= ~BGE_PCIDMARWCTL_MINDMA;
1365 	pci_write_config(sc->bge_dev, BGE_PCI_DMA_RW_CTL, dma_rw_ctl, 4);
1366 
1367 	/*
1368 	 * Set up general mode register.
1369 	 */
1370 	CSR_WRITE_4(sc, BGE_MODE_CTL, BGE_DMA_SWAP_OPTIONS |
1371 	    BGE_MODECTL_MAC_ATTN_INTR | BGE_MODECTL_HOST_SEND_BDS |
1372 	    BGE_MODECTL_TX_NO_PHDR_CSUM);
1373 
1374 	/*
1375 	 * Tell the firmware the driver is running
1376 	 */
1377 	if (sc->bge_asf_mode & ASF_STACKUP)
1378 		BGE_SETBIT(sc, BGE_MODE_CTL, BGE_MODECTL_STACKUP);
1379 
1380 	/*
1381 	 * Disable memory write invalidate.  Apparently it is not supported
1382 	 * properly by these devices.
1383 	 */
1384 	PCI_CLRBIT(sc->bge_dev, BGE_PCI_CMD, PCIM_CMD_MWIEN, 4);
1385 
1386 	/* Set the timer prescaler (always 66Mhz) */
1387 	CSR_WRITE_4(sc, BGE_MISC_CFG, BGE_32BITTIME_66MHZ);
1388 
1389 	/* XXX: The Linux tg3 driver does this at the start of brgphy_reset. */
1390 	if (sc->bge_asicrev == BGE_ASICREV_BCM5906) {
1391 		DELAY(40);	/* XXX */
1392 
1393 		/* Put PHY into ready state */
1394 		BGE_CLRBIT(sc, BGE_MISC_CFG, BGE_MISCCFG_EPHY_IDDQ);
1395 		CSR_READ_4(sc, BGE_MISC_CFG); /* Flush */
1396 		DELAY(40);
1397 	}
1398 
1399 	return (0);
1400 }
1401 
1402 static int
1403 bge_blockinit(struct bge_softc *sc)
1404 {
1405 	struct bge_rcb *rcb;
1406 	bus_size_t vrcb;
1407 	bge_hostaddr taddr;
1408 	uint32_t val;
1409 	int i;
1410 
1411 	/*
1412 	 * Initialize the memory window pointer register so that
1413 	 * we can access the first 32K of internal NIC RAM. This will
1414 	 * allow us to set up the TX send ring RCBs and the RX return
1415 	 * ring RCBs, plus other things which live in NIC memory.
1416 	 */
1417 	CSR_WRITE_4(sc, BGE_PCI_MEMWIN_BASEADDR, 0);
1418 
1419 	/* Note: the BCM5704 has a smaller mbuf space than other chips. */
1420 
1421 	if (!(BGE_IS_5705_PLUS(sc))) {
1422 		/* Configure mbuf memory pool */
1423 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_BASEADDR, BGE_BUFFPOOL_1);
1424 		if (sc->bge_asicrev == BGE_ASICREV_BCM5704)
1425 			CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_LEN, 0x10000);
1426 		else
1427 			CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_LEN, 0x18000);
1428 
1429 		/* Configure DMA resource pool */
1430 		CSR_WRITE_4(sc, BGE_BMAN_DMA_DESCPOOL_BASEADDR,
1431 		    BGE_DMA_DESCRIPTORS);
1432 		CSR_WRITE_4(sc, BGE_BMAN_DMA_DESCPOOL_LEN, 0x2000);
1433 	}
1434 
1435 	/* Configure mbuf pool watermarks */
1436 	if (!BGE_IS_5705_PLUS(sc)) {
1437 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_READDMA_LOWAT, 0x50);
1438 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_MACRX_LOWAT, 0x20);
1439 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_HIWAT, 0x60);
1440 	} else if (sc->bge_asicrev == BGE_ASICREV_BCM5906) {
1441 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_READDMA_LOWAT, 0x0);
1442 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_MACRX_LOWAT, 0x04);
1443 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_HIWAT, 0x10);
1444 	} else {
1445 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_READDMA_LOWAT, 0x0);
1446 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_MACRX_LOWAT, 0x10);
1447 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_HIWAT, 0x60);
1448 	}
1449 
1450 	/* Configure DMA resource watermarks */
1451 	CSR_WRITE_4(sc, BGE_BMAN_DMA_DESCPOOL_LOWAT, 5);
1452 	CSR_WRITE_4(sc, BGE_BMAN_DMA_DESCPOOL_HIWAT, 10);
1453 
1454 	/* Enable buffer manager */
1455 	if (!(BGE_IS_5705_PLUS(sc))) {
1456 		CSR_WRITE_4(sc, BGE_BMAN_MODE,
1457 		    BGE_BMANMODE_ENABLE | BGE_BMANMODE_LOMBUF_ATTN);
1458 
1459 		/* Poll for buffer manager start indication */
1460 		for (i = 0; i < BGE_TIMEOUT; i++) {
1461 			DELAY(10);
1462 			if (CSR_READ_4(sc, BGE_BMAN_MODE) & BGE_BMANMODE_ENABLE)
1463 				break;
1464 		}
1465 
1466 		if (i == BGE_TIMEOUT) {
1467 			device_printf(sc->bge_dev,
1468 			    "buffer manager failed to start\n");
1469 			return (ENXIO);
1470 		}
1471 	}
1472 
1473 	/* Enable flow-through queues */
1474 	CSR_WRITE_4(sc, BGE_FTQ_RESET, 0xFFFFFFFF);
1475 	CSR_WRITE_4(sc, BGE_FTQ_RESET, 0);
1476 
1477 	/* Wait until queue initialization is complete */
1478 	for (i = 0; i < BGE_TIMEOUT; i++) {
1479 		DELAY(10);
1480 		if (CSR_READ_4(sc, BGE_FTQ_RESET) == 0)
1481 			break;
1482 	}
1483 
1484 	if (i == BGE_TIMEOUT) {
1485 		device_printf(sc->bge_dev, "flow-through queue init failed\n");
1486 		return (ENXIO);
1487 	}
1488 
1489 	/* Initialize the standard RX ring control block */
1490 	rcb = &sc->bge_ldata.bge_info.bge_std_rx_rcb;
1491 	rcb->bge_hostaddr.bge_addr_lo =
1492 	    BGE_ADDR_LO(sc->bge_ldata.bge_rx_std_ring_paddr);
1493 	rcb->bge_hostaddr.bge_addr_hi =
1494 	    BGE_ADDR_HI(sc->bge_ldata.bge_rx_std_ring_paddr);
1495 	bus_dmamap_sync(sc->bge_cdata.bge_rx_std_ring_tag,
1496 	    sc->bge_cdata.bge_rx_std_ring_map, BUS_DMASYNC_PREREAD);
1497 	if (BGE_IS_5705_PLUS(sc))
1498 		rcb->bge_maxlen_flags = BGE_RCB_MAXLEN_FLAGS(512, 0);
1499 	else
1500 		rcb->bge_maxlen_flags =
1501 		    BGE_RCB_MAXLEN_FLAGS(BGE_MAX_FRAMELEN, 0);
1502 	rcb->bge_nicaddr = BGE_STD_RX_RINGS;
1503 	CSR_WRITE_4(sc, BGE_RX_STD_RCB_HADDR_HI, rcb->bge_hostaddr.bge_addr_hi);
1504 	CSR_WRITE_4(sc, BGE_RX_STD_RCB_HADDR_LO, rcb->bge_hostaddr.bge_addr_lo);
1505 
1506 	CSR_WRITE_4(sc, BGE_RX_STD_RCB_MAXLEN_FLAGS, rcb->bge_maxlen_flags);
1507 	CSR_WRITE_4(sc, BGE_RX_STD_RCB_NICADDR, rcb->bge_nicaddr);
1508 
1509 	/*
1510 	 * Initialize the jumbo RX ring control block
1511 	 * We set the 'ring disabled' bit in the flags
1512 	 * field until we're actually ready to start
1513 	 * using this ring (i.e. once we set the MTU
1514 	 * high enough to require it).
1515 	 */
1516 	if (BGE_IS_JUMBO_CAPABLE(sc)) {
1517 		rcb = &sc->bge_ldata.bge_info.bge_jumbo_rx_rcb;
1518 
1519 		rcb->bge_hostaddr.bge_addr_lo =
1520 		    BGE_ADDR_LO(sc->bge_ldata.bge_rx_jumbo_ring_paddr);
1521 		rcb->bge_hostaddr.bge_addr_hi =
1522 		    BGE_ADDR_HI(sc->bge_ldata.bge_rx_jumbo_ring_paddr);
1523 		bus_dmamap_sync(sc->bge_cdata.bge_rx_jumbo_ring_tag,
1524 		    sc->bge_cdata.bge_rx_jumbo_ring_map,
1525 		    BUS_DMASYNC_PREREAD);
1526 		rcb->bge_maxlen_flags = BGE_RCB_MAXLEN_FLAGS(0,
1527 		    BGE_RCB_FLAG_USE_EXT_RX_BD | BGE_RCB_FLAG_RING_DISABLED);
1528 		rcb->bge_nicaddr = BGE_JUMBO_RX_RINGS;
1529 		CSR_WRITE_4(sc, BGE_RX_JUMBO_RCB_HADDR_HI,
1530 		    rcb->bge_hostaddr.bge_addr_hi);
1531 		CSR_WRITE_4(sc, BGE_RX_JUMBO_RCB_HADDR_LO,
1532 		    rcb->bge_hostaddr.bge_addr_lo);
1533 
1534 		CSR_WRITE_4(sc, BGE_RX_JUMBO_RCB_MAXLEN_FLAGS,
1535 		    rcb->bge_maxlen_flags);
1536 		CSR_WRITE_4(sc, BGE_RX_JUMBO_RCB_NICADDR, rcb->bge_nicaddr);
1537 
1538 		/* Set up dummy disabled mini ring RCB */
1539 		rcb = &sc->bge_ldata.bge_info.bge_mini_rx_rcb;
1540 		rcb->bge_maxlen_flags =
1541 		    BGE_RCB_MAXLEN_FLAGS(0, BGE_RCB_FLAG_RING_DISABLED);
1542 		CSR_WRITE_4(sc, BGE_RX_MINI_RCB_MAXLEN_FLAGS,
1543 		    rcb->bge_maxlen_flags);
1544 	}
1545 
1546 	/*
1547 	 * Set the BD ring replentish thresholds. The recommended
1548 	 * values are 1/8th the number of descriptors allocated to
1549 	 * each ring.
1550 	 * XXX The 5754 requires a lower threshold, so it might be a
1551 	 * requirement of all 575x family chips.  The Linux driver sets
1552 	 * the lower threshold for all 5705 family chips as well, but there
1553 	 * are reports that it might not need to be so strict.
1554 	 *
1555 	 * XXX Linux does some extra fiddling here for the 5906 parts as
1556 	 * well.
1557 	 */
1558 	if (BGE_IS_5705_PLUS(sc))
1559 		val = 8;
1560 	else
1561 		val = BGE_STD_RX_RING_CNT / 8;
1562 	CSR_WRITE_4(sc, BGE_RBDI_STD_REPL_THRESH, val);
1563 	CSR_WRITE_4(sc, BGE_RBDI_JUMBO_REPL_THRESH, BGE_JUMBO_RX_RING_CNT/8);
1564 
1565 	/*
1566 	 * Disable all unused send rings by setting the 'ring disabled'
1567 	 * bit in the flags field of all the TX send ring control blocks.
1568 	 * These are located in NIC memory.
1569 	 */
1570 	vrcb = BGE_MEMWIN_START + BGE_SEND_RING_RCB;
1571 	for (i = 0; i < BGE_TX_RINGS_EXTSSRAM_MAX; i++) {
1572 		RCB_WRITE_4(sc, vrcb, bge_maxlen_flags,
1573 		    BGE_RCB_MAXLEN_FLAGS(0, BGE_RCB_FLAG_RING_DISABLED));
1574 		RCB_WRITE_4(sc, vrcb, bge_nicaddr, 0);
1575 		vrcb += sizeof(struct bge_rcb);
1576 	}
1577 
1578 	/* Configure TX RCB 0 (we use only the first ring) */
1579 	vrcb = BGE_MEMWIN_START + BGE_SEND_RING_RCB;
1580 	BGE_HOSTADDR(taddr, sc->bge_ldata.bge_tx_ring_paddr);
1581 	RCB_WRITE_4(sc, vrcb, bge_hostaddr.bge_addr_hi, taddr.bge_addr_hi);
1582 	RCB_WRITE_4(sc, vrcb, bge_hostaddr.bge_addr_lo, taddr.bge_addr_lo);
1583 	RCB_WRITE_4(sc, vrcb, bge_nicaddr,
1584 	    BGE_NIC_TXRING_ADDR(0, BGE_TX_RING_CNT));
1585 	if (!(BGE_IS_5705_PLUS(sc)))
1586 		RCB_WRITE_4(sc, vrcb, bge_maxlen_flags,
1587 		    BGE_RCB_MAXLEN_FLAGS(BGE_TX_RING_CNT, 0));
1588 
1589 	/* Disable all unused RX return rings */
1590 	vrcb = BGE_MEMWIN_START + BGE_RX_RETURN_RING_RCB;
1591 	for (i = 0; i < BGE_RX_RINGS_MAX; i++) {
1592 		RCB_WRITE_4(sc, vrcb, bge_hostaddr.bge_addr_hi, 0);
1593 		RCB_WRITE_4(sc, vrcb, bge_hostaddr.bge_addr_lo, 0);
1594 		RCB_WRITE_4(sc, vrcb, bge_maxlen_flags,
1595 		    BGE_RCB_MAXLEN_FLAGS(sc->bge_return_ring_cnt,
1596 		    BGE_RCB_FLAG_RING_DISABLED));
1597 		RCB_WRITE_4(sc, vrcb, bge_nicaddr, 0);
1598 		bge_writembx(sc, BGE_MBX_RX_CONS0_LO +
1599 		    (i * (sizeof(uint64_t))), 0);
1600 		vrcb += sizeof(struct bge_rcb);
1601 	}
1602 
1603 	/* Initialize RX ring indexes */
1604 	bge_writembx(sc, BGE_MBX_RX_STD_PROD_LO, 0);
1605 	bge_writembx(sc, BGE_MBX_RX_JUMBO_PROD_LO, 0);
1606 	bge_writembx(sc, BGE_MBX_RX_MINI_PROD_LO, 0);
1607 
1608 	/*
1609 	 * Set up RX return ring 0
1610 	 * Note that the NIC address for RX return rings is 0x00000000.
1611 	 * The return rings live entirely within the host, so the
1612 	 * nicaddr field in the RCB isn't used.
1613 	 */
1614 	vrcb = BGE_MEMWIN_START + BGE_RX_RETURN_RING_RCB;
1615 	BGE_HOSTADDR(taddr, sc->bge_ldata.bge_rx_return_ring_paddr);
1616 	RCB_WRITE_4(sc, vrcb, bge_hostaddr.bge_addr_hi, taddr.bge_addr_hi);
1617 	RCB_WRITE_4(sc, vrcb, bge_hostaddr.bge_addr_lo, taddr.bge_addr_lo);
1618 	RCB_WRITE_4(sc, vrcb, bge_nicaddr, 0x00000000);
1619 	RCB_WRITE_4(sc, vrcb, bge_maxlen_flags,
1620 	    BGE_RCB_MAXLEN_FLAGS(sc->bge_return_ring_cnt, 0));
1621 
1622 	/* Set random backoff seed for TX */
1623 	CSR_WRITE_4(sc, BGE_TX_RANDOM_BACKOFF,
1624 	    IF_LLADDR(sc->bge_ifp)[0] + IF_LLADDR(sc->bge_ifp)[1] +
1625 	    IF_LLADDR(sc->bge_ifp)[2] + IF_LLADDR(sc->bge_ifp)[3] +
1626 	    IF_LLADDR(sc->bge_ifp)[4] + IF_LLADDR(sc->bge_ifp)[5] +
1627 	    BGE_TX_BACKOFF_SEED_MASK);
1628 
1629 	/* Set inter-packet gap */
1630 	CSR_WRITE_4(sc, BGE_TX_LENGTHS, 0x2620);
1631 
1632 	/*
1633 	 * Specify which ring to use for packets that don't match
1634 	 * any RX rules.
1635 	 */
1636 	CSR_WRITE_4(sc, BGE_RX_RULES_CFG, 0x08);
1637 
1638 	/*
1639 	 * Configure number of RX lists. One interrupt distribution
1640 	 * list, sixteen active lists, one bad frames class.
1641 	 */
1642 	CSR_WRITE_4(sc, BGE_RXLP_CFG, 0x181);
1643 
1644 	/* Inialize RX list placement stats mask. */
1645 	CSR_WRITE_4(sc, BGE_RXLP_STATS_ENABLE_MASK, 0x007FFFFF);
1646 	CSR_WRITE_4(sc, BGE_RXLP_STATS_CTL, 0x1);
1647 
1648 	/* Disable host coalescing until we get it set up */
1649 	CSR_WRITE_4(sc, BGE_HCC_MODE, 0x00000000);
1650 
1651 	/* Poll to make sure it's shut down. */
1652 	for (i = 0; i < BGE_TIMEOUT; i++) {
1653 		DELAY(10);
1654 		if (!(CSR_READ_4(sc, BGE_HCC_MODE) & BGE_HCCMODE_ENABLE))
1655 			break;
1656 	}
1657 
1658 	if (i == BGE_TIMEOUT) {
1659 		device_printf(sc->bge_dev,
1660 		    "host coalescing engine failed to idle\n");
1661 		return (ENXIO);
1662 	}
1663 
1664 	/* Set up host coalescing defaults */
1665 	CSR_WRITE_4(sc, BGE_HCC_RX_COAL_TICKS, sc->bge_rx_coal_ticks);
1666 	CSR_WRITE_4(sc, BGE_HCC_TX_COAL_TICKS, sc->bge_tx_coal_ticks);
1667 	CSR_WRITE_4(sc, BGE_HCC_RX_MAX_COAL_BDS, sc->bge_rx_max_coal_bds);
1668 	CSR_WRITE_4(sc, BGE_HCC_TX_MAX_COAL_BDS, sc->bge_tx_max_coal_bds);
1669 	if (!(BGE_IS_5705_PLUS(sc))) {
1670 		CSR_WRITE_4(sc, BGE_HCC_RX_COAL_TICKS_INT, 0);
1671 		CSR_WRITE_4(sc, BGE_HCC_TX_COAL_TICKS_INT, 0);
1672 	}
1673 	CSR_WRITE_4(sc, BGE_HCC_RX_MAX_COAL_BDS_INT, 1);
1674 	CSR_WRITE_4(sc, BGE_HCC_TX_MAX_COAL_BDS_INT, 1);
1675 
1676 	/* Set up address of statistics block */
1677 	if (!(BGE_IS_5705_PLUS(sc))) {
1678 		CSR_WRITE_4(sc, BGE_HCC_STATS_ADDR_HI,
1679 		    BGE_ADDR_HI(sc->bge_ldata.bge_stats_paddr));
1680 		CSR_WRITE_4(sc, BGE_HCC_STATS_ADDR_LO,
1681 		    BGE_ADDR_LO(sc->bge_ldata.bge_stats_paddr));
1682 		CSR_WRITE_4(sc, BGE_HCC_STATS_BASEADDR, BGE_STATS_BLOCK);
1683 		CSR_WRITE_4(sc, BGE_HCC_STATUSBLK_BASEADDR, BGE_STATUS_BLOCK);
1684 		CSR_WRITE_4(sc, BGE_HCC_STATS_TICKS, sc->bge_stat_ticks);
1685 	}
1686 
1687 	/* Set up address of status block */
1688 	CSR_WRITE_4(sc, BGE_HCC_STATUSBLK_ADDR_HI,
1689 	    BGE_ADDR_HI(sc->bge_ldata.bge_status_block_paddr));
1690 	CSR_WRITE_4(sc, BGE_HCC_STATUSBLK_ADDR_LO,
1691 	    BGE_ADDR_LO(sc->bge_ldata.bge_status_block_paddr));
1692 	sc->bge_ldata.bge_status_block->bge_idx[0].bge_rx_prod_idx = 0;
1693 	sc->bge_ldata.bge_status_block->bge_idx[0].bge_tx_cons_idx = 0;
1694 
1695 	/* Turn on host coalescing state machine */
1696 	CSR_WRITE_4(sc, BGE_HCC_MODE, BGE_HCCMODE_ENABLE);
1697 
1698 	/* Turn on RX BD completion state machine and enable attentions */
1699 	CSR_WRITE_4(sc, BGE_RBDC_MODE,
1700 	    BGE_RBDCMODE_ENABLE | BGE_RBDCMODE_ATTN);
1701 
1702 	/* Turn on RX list placement state machine */
1703 	CSR_WRITE_4(sc, BGE_RXLP_MODE, BGE_RXLPMODE_ENABLE);
1704 
1705 	/* Turn on RX list selector state machine. */
1706 	if (!(BGE_IS_5705_PLUS(sc)))
1707 		CSR_WRITE_4(sc, BGE_RXLS_MODE, BGE_RXLSMODE_ENABLE);
1708 
1709 	/* Turn on DMA, clear stats */
1710 	CSR_WRITE_4(sc, BGE_MAC_MODE, BGE_MACMODE_TXDMA_ENB |
1711 	    BGE_MACMODE_RXDMA_ENB | BGE_MACMODE_RX_STATS_CLEAR |
1712 	    BGE_MACMODE_TX_STATS_CLEAR | BGE_MACMODE_RX_STATS_ENB |
1713 	    BGE_MACMODE_TX_STATS_ENB | BGE_MACMODE_FRMHDR_DMA_ENB |
1714 	    ((sc->bge_flags & BGE_FLAG_TBI) ?
1715 	    BGE_PORTMODE_TBI : BGE_PORTMODE_MII));
1716 
1717 	/* Set misc. local control, enable interrupts on attentions */
1718 	CSR_WRITE_4(sc, BGE_MISC_LOCAL_CTL, BGE_MLC_INTR_ONATTN);
1719 
1720 #ifdef notdef
1721 	/* Assert GPIO pins for PHY reset */
1722 	BGE_SETBIT(sc, BGE_MISC_LOCAL_CTL, BGE_MLC_MISCIO_OUT0 |
1723 	    BGE_MLC_MISCIO_OUT1 | BGE_MLC_MISCIO_OUT2);
1724 	BGE_SETBIT(sc, BGE_MISC_LOCAL_CTL, BGE_MLC_MISCIO_OUTEN0 |
1725 	    BGE_MLC_MISCIO_OUTEN1 | BGE_MLC_MISCIO_OUTEN2);
1726 #endif
1727 
1728 	/* Turn on DMA completion state machine */
1729 	if (!(BGE_IS_5705_PLUS(sc)))
1730 		CSR_WRITE_4(sc, BGE_DMAC_MODE, BGE_DMACMODE_ENABLE);
1731 
1732 	val = BGE_WDMAMODE_ENABLE | BGE_WDMAMODE_ALL_ATTNS;
1733 
1734 	/* Enable host coalescing bug fix. */
1735 	if (sc->bge_asicrev == BGE_ASICREV_BCM5755 ||
1736 	    sc->bge_asicrev == BGE_ASICREV_BCM5787)
1737 			val |= 1 << 29;
1738 
1739 	/* Turn on write DMA state machine */
1740 	CSR_WRITE_4(sc, BGE_WDMA_MODE, val);
1741 
1742 	/* Turn on read DMA state machine */
1743 	CSR_WRITE_4(sc, BGE_RDMA_MODE,
1744 	    BGE_RDMAMODE_ENABLE | BGE_RDMAMODE_ALL_ATTNS);
1745 
1746 	/* Turn on RX data completion state machine */
1747 	CSR_WRITE_4(sc, BGE_RDC_MODE, BGE_RDCMODE_ENABLE);
1748 
1749 	/* Turn on RX BD initiator state machine */
1750 	CSR_WRITE_4(sc, BGE_RBDI_MODE, BGE_RBDIMODE_ENABLE);
1751 
1752 	/* Turn on RX data and RX BD initiator state machine */
1753 	CSR_WRITE_4(sc, BGE_RDBDI_MODE, BGE_RDBDIMODE_ENABLE);
1754 
1755 	/* Turn on Mbuf cluster free state machine */
1756 	if (!(BGE_IS_5705_PLUS(sc)))
1757 		CSR_WRITE_4(sc, BGE_MBCF_MODE, BGE_MBCFMODE_ENABLE);
1758 
1759 	/* Turn on send BD completion state machine */
1760 	CSR_WRITE_4(sc, BGE_SBDC_MODE, BGE_SBDCMODE_ENABLE);
1761 
1762 	/* Turn on send data completion state machine */
1763 	CSR_WRITE_4(sc, BGE_SDC_MODE, BGE_SDCMODE_ENABLE);
1764 
1765 	/* Turn on send data initiator state machine */
1766 	CSR_WRITE_4(sc, BGE_SDI_MODE, BGE_SDIMODE_ENABLE);
1767 
1768 	/* Turn on send BD initiator state machine */
1769 	CSR_WRITE_4(sc, BGE_SBDI_MODE, BGE_SBDIMODE_ENABLE);
1770 
1771 	/* Turn on send BD selector state machine */
1772 	CSR_WRITE_4(sc, BGE_SRS_MODE, BGE_SRSMODE_ENABLE);
1773 
1774 	CSR_WRITE_4(sc, BGE_SDI_STATS_ENABLE_MASK, 0x007FFFFF);
1775 	CSR_WRITE_4(sc, BGE_SDI_STATS_CTL,
1776 	    BGE_SDISTATSCTL_ENABLE | BGE_SDISTATSCTL_FASTER);
1777 
1778 	/* ack/clear link change events */
1779 	CSR_WRITE_4(sc, BGE_MAC_STS, BGE_MACSTAT_SYNC_CHANGED |
1780 	    BGE_MACSTAT_CFG_CHANGED | BGE_MACSTAT_MI_COMPLETE |
1781 	    BGE_MACSTAT_LINK_CHANGED);
1782 	CSR_WRITE_4(sc, BGE_MI_STS, 0);
1783 
1784 	/* Enable PHY auto polling (for MII/GMII only) */
1785 	if (sc->bge_flags & BGE_FLAG_TBI) {
1786 		CSR_WRITE_4(sc, BGE_MI_STS, BGE_MISTS_LINK);
1787 	} else {
1788 		BGE_SETBIT(sc, BGE_MI_MODE, BGE_MIMODE_AUTOPOLL | (10 << 16));
1789 		if (sc->bge_asicrev == BGE_ASICREV_BCM5700 &&
1790 		    sc->bge_chipid != BGE_CHIPID_BCM5700_B2)
1791 			CSR_WRITE_4(sc, BGE_MAC_EVT_ENB,
1792 			    BGE_EVTENB_MI_INTERRUPT);
1793 	}
1794 
1795 	/*
1796 	 * Clear any pending link state attention.
1797 	 * Otherwise some link state change events may be lost until attention
1798 	 * is cleared by bge_intr() -> bge_link_upd() sequence.
1799 	 * It's not necessary on newer BCM chips - perhaps enabling link
1800 	 * state change attentions implies clearing pending attention.
1801 	 */
1802 	CSR_WRITE_4(sc, BGE_MAC_STS, BGE_MACSTAT_SYNC_CHANGED |
1803 	    BGE_MACSTAT_CFG_CHANGED | BGE_MACSTAT_MI_COMPLETE |
1804 	    BGE_MACSTAT_LINK_CHANGED);
1805 
1806 	/* Enable link state change attentions. */
1807 	BGE_SETBIT(sc, BGE_MAC_EVT_ENB, BGE_EVTENB_LINK_CHANGED);
1808 
1809 	return (0);
1810 }
1811 
1812 const struct bge_revision *
1813 bge_lookup_rev(uint32_t chipid)
1814 {
1815 	const struct bge_revision *br;
1816 
1817 	for (br = bge_revisions; br->br_name != NULL; br++) {
1818 		if (br->br_chipid == chipid)
1819 			return (br);
1820 	}
1821 
1822 	for (br = bge_majorrevs; br->br_name != NULL; br++) {
1823 		if (br->br_chipid == BGE_ASICREV(chipid))
1824 			return (br);
1825 	}
1826 
1827 	return (NULL);
1828 }
1829 
1830 const struct bge_vendor *
1831 bge_lookup_vendor(uint16_t vid)
1832 {
1833 	const struct bge_vendor *v;
1834 
1835 	for (v = bge_vendors; v->v_name != NULL; v++)
1836 		if (v->v_id == vid)
1837 			return (v);
1838 
1839 	panic("%s: unknown vendor %d", __func__, vid);
1840 	return (NULL);
1841 }
1842 
1843 /*
1844  * Probe for a Broadcom chip. Check the PCI vendor and device IDs
1845  * against our list and return its name if we find a match.
1846  *
1847  * Note that since the Broadcom controller contains VPD support, we
1848  * try to get the device name string from the controller itself instead
1849  * of the compiled-in string. It guarantees we'll always announce the
1850  * right product name. We fall back to the compiled-in string when
1851  * VPD is unavailable or corrupt.
1852  */
1853 static int
1854 bge_probe(device_t dev)
1855 {
1856 	struct bge_type *t = bge_devs;
1857 	struct bge_softc *sc = device_get_softc(dev);
1858 	uint16_t vid, did;
1859 
1860 	sc->bge_dev = dev;
1861 	vid = pci_get_vendor(dev);
1862 	did = pci_get_device(dev);
1863 	while(t->bge_vid != 0) {
1864 		if ((vid == t->bge_vid) && (did == t->bge_did)) {
1865 			char model[64], buf[96];
1866 			const struct bge_revision *br;
1867 			const struct bge_vendor *v;
1868 			uint32_t id;
1869 
1870 			id = pci_read_config(dev, BGE_PCI_MISC_CTL, 4) &
1871 			    BGE_PCIMISCCTL_ASICREV;
1872 			br = bge_lookup_rev(id);
1873 			v = bge_lookup_vendor(vid);
1874 			{
1875 #if __FreeBSD_version > 700024
1876 				const char *pname;
1877 
1878 				if (pci_get_vpd_ident(dev, &pname) == 0)
1879 					snprintf(model, 64, "%s", pname);
1880 				else
1881 #endif
1882 					snprintf(model, 64, "%s %s",
1883 					    v->v_name,
1884 					    br != NULL ? br->br_name :
1885 					    "NetXtreme Ethernet Controller");
1886 			}
1887 			snprintf(buf, 96, "%s, %sASIC rev. %#04x", model,
1888 			    br != NULL ? "" : "unknown ", id >> 16);
1889 			device_set_desc_copy(dev, buf);
1890 			if (pci_get_subvendor(dev) == DELL_VENDORID)
1891 				sc->bge_flags |= BGE_FLAG_NO_3LED;
1892 			if (did == BCOM_DEVICEID_BCM5755M)
1893 				sc->bge_flags |= BGE_FLAG_ADJUST_TRIM;
1894 			return (0);
1895 		}
1896 		t++;
1897 	}
1898 
1899 	return (ENXIO);
1900 }
1901 
1902 static void
1903 bge_dma_free(struct bge_softc *sc)
1904 {
1905 	int i;
1906 
1907 	/* Destroy DMA maps for RX buffers. */
1908 	for (i = 0; i < BGE_STD_RX_RING_CNT; i++) {
1909 		if (sc->bge_cdata.bge_rx_std_dmamap[i])
1910 			bus_dmamap_destroy(sc->bge_cdata.bge_mtag,
1911 			    sc->bge_cdata.bge_rx_std_dmamap[i]);
1912 	}
1913 
1914 	/* Destroy DMA maps for jumbo RX buffers. */
1915 	for (i = 0; i < BGE_JUMBO_RX_RING_CNT; i++) {
1916 		if (sc->bge_cdata.bge_rx_jumbo_dmamap[i])
1917 			bus_dmamap_destroy(sc->bge_cdata.bge_mtag_jumbo,
1918 			    sc->bge_cdata.bge_rx_jumbo_dmamap[i]);
1919 	}
1920 
1921 	/* Destroy DMA maps for TX buffers. */
1922 	for (i = 0; i < BGE_TX_RING_CNT; i++) {
1923 		if (sc->bge_cdata.bge_tx_dmamap[i])
1924 			bus_dmamap_destroy(sc->bge_cdata.bge_mtag,
1925 			    sc->bge_cdata.bge_tx_dmamap[i]);
1926 	}
1927 
1928 	if (sc->bge_cdata.bge_mtag)
1929 		bus_dma_tag_destroy(sc->bge_cdata.bge_mtag);
1930 
1931 
1932 	/* Destroy standard RX ring. */
1933 	if (sc->bge_cdata.bge_rx_std_ring_map)
1934 		bus_dmamap_unload(sc->bge_cdata.bge_rx_std_ring_tag,
1935 		    sc->bge_cdata.bge_rx_std_ring_map);
1936 	if (sc->bge_cdata.bge_rx_std_ring_map && sc->bge_ldata.bge_rx_std_ring)
1937 		bus_dmamem_free(sc->bge_cdata.bge_rx_std_ring_tag,
1938 		    sc->bge_ldata.bge_rx_std_ring,
1939 		    sc->bge_cdata.bge_rx_std_ring_map);
1940 
1941 	if (sc->bge_cdata.bge_rx_std_ring_tag)
1942 		bus_dma_tag_destroy(sc->bge_cdata.bge_rx_std_ring_tag);
1943 
1944 	/* Destroy jumbo RX ring. */
1945 	if (sc->bge_cdata.bge_rx_jumbo_ring_map)
1946 		bus_dmamap_unload(sc->bge_cdata.bge_rx_jumbo_ring_tag,
1947 		    sc->bge_cdata.bge_rx_jumbo_ring_map);
1948 
1949 	if (sc->bge_cdata.bge_rx_jumbo_ring_map &&
1950 	    sc->bge_ldata.bge_rx_jumbo_ring)
1951 		bus_dmamem_free(sc->bge_cdata.bge_rx_jumbo_ring_tag,
1952 		    sc->bge_ldata.bge_rx_jumbo_ring,
1953 		    sc->bge_cdata.bge_rx_jumbo_ring_map);
1954 
1955 	if (sc->bge_cdata.bge_rx_jumbo_ring_tag)
1956 		bus_dma_tag_destroy(sc->bge_cdata.bge_rx_jumbo_ring_tag);
1957 
1958 	/* Destroy RX return ring. */
1959 	if (sc->bge_cdata.bge_rx_return_ring_map)
1960 		bus_dmamap_unload(sc->bge_cdata.bge_rx_return_ring_tag,
1961 		    sc->bge_cdata.bge_rx_return_ring_map);
1962 
1963 	if (sc->bge_cdata.bge_rx_return_ring_map &&
1964 	    sc->bge_ldata.bge_rx_return_ring)
1965 		bus_dmamem_free(sc->bge_cdata.bge_rx_return_ring_tag,
1966 		    sc->bge_ldata.bge_rx_return_ring,
1967 		    sc->bge_cdata.bge_rx_return_ring_map);
1968 
1969 	if (sc->bge_cdata.bge_rx_return_ring_tag)
1970 		bus_dma_tag_destroy(sc->bge_cdata.bge_rx_return_ring_tag);
1971 
1972 	/* Destroy TX ring. */
1973 	if (sc->bge_cdata.bge_tx_ring_map)
1974 		bus_dmamap_unload(sc->bge_cdata.bge_tx_ring_tag,
1975 		    sc->bge_cdata.bge_tx_ring_map);
1976 
1977 	if (sc->bge_cdata.bge_tx_ring_map && sc->bge_ldata.bge_tx_ring)
1978 		bus_dmamem_free(sc->bge_cdata.bge_tx_ring_tag,
1979 		    sc->bge_ldata.bge_tx_ring,
1980 		    sc->bge_cdata.bge_tx_ring_map);
1981 
1982 	if (sc->bge_cdata.bge_tx_ring_tag)
1983 		bus_dma_tag_destroy(sc->bge_cdata.bge_tx_ring_tag);
1984 
1985 	/* Destroy status block. */
1986 	if (sc->bge_cdata.bge_status_map)
1987 		bus_dmamap_unload(sc->bge_cdata.bge_status_tag,
1988 		    sc->bge_cdata.bge_status_map);
1989 
1990 	if (sc->bge_cdata.bge_status_map && sc->bge_ldata.bge_status_block)
1991 		bus_dmamem_free(sc->bge_cdata.bge_status_tag,
1992 		    sc->bge_ldata.bge_status_block,
1993 		    sc->bge_cdata.bge_status_map);
1994 
1995 	if (sc->bge_cdata.bge_status_tag)
1996 		bus_dma_tag_destroy(sc->bge_cdata.bge_status_tag);
1997 
1998 	/* Destroy statistics block. */
1999 	if (sc->bge_cdata.bge_stats_map)
2000 		bus_dmamap_unload(sc->bge_cdata.bge_stats_tag,
2001 		    sc->bge_cdata.bge_stats_map);
2002 
2003 	if (sc->bge_cdata.bge_stats_map && sc->bge_ldata.bge_stats)
2004 		bus_dmamem_free(sc->bge_cdata.bge_stats_tag,
2005 		    sc->bge_ldata.bge_stats,
2006 		    sc->bge_cdata.bge_stats_map);
2007 
2008 	if (sc->bge_cdata.bge_stats_tag)
2009 		bus_dma_tag_destroy(sc->bge_cdata.bge_stats_tag);
2010 
2011 	/* Destroy the parent tag. */
2012 	if (sc->bge_cdata.bge_parent_tag)
2013 		bus_dma_tag_destroy(sc->bge_cdata.bge_parent_tag);
2014 }
2015 
2016 static int
2017 bge_dma_alloc(device_t dev)
2018 {
2019 	struct bge_dmamap_arg ctx;
2020 	struct bge_softc *sc;
2021 	int i, error;
2022 
2023 	sc = device_get_softc(dev);
2024 
2025 	/*
2026 	 * Allocate the parent bus DMA tag appropriate for PCI.
2027 	 */
2028 	error = bus_dma_tag_create(bus_get_dma_tag(sc->bge_dev),
2029 	    1, 0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR,	NULL,
2030 	    NULL, BUS_SPACE_MAXSIZE_32BIT, 0, BUS_SPACE_MAXSIZE_32BIT,
2031 	    0, NULL, NULL, &sc->bge_cdata.bge_parent_tag);
2032 
2033 	if (error != 0) {
2034 		device_printf(sc->bge_dev,
2035 		    "could not allocate parent dma tag\n");
2036 		return (ENOMEM);
2037 	}
2038 
2039 	/*
2040 	 * Create tag for mbufs.
2041 	 */
2042 	error = bus_dma_tag_create(sc->bge_cdata.bge_parent_tag, 1,
2043 	    0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL,
2044 	    NULL, MCLBYTES * BGE_NSEG_NEW, BGE_NSEG_NEW, MCLBYTES,
2045 	    BUS_DMA_ALLOCNOW, NULL, NULL, &sc->bge_cdata.bge_mtag);
2046 
2047 	if (error) {
2048 		device_printf(sc->bge_dev, "could not allocate dma tag\n");
2049 		return (ENOMEM);
2050 	}
2051 
2052 	/* Create DMA maps for RX buffers. */
2053 	for (i = 0; i < BGE_STD_RX_RING_CNT; i++) {
2054 		error = bus_dmamap_create(sc->bge_cdata.bge_mtag, 0,
2055 			    &sc->bge_cdata.bge_rx_std_dmamap[i]);
2056 		if (error) {
2057 			device_printf(sc->bge_dev,
2058 			    "can't create DMA map for RX\n");
2059 			return (ENOMEM);
2060 		}
2061 	}
2062 
2063 	/* Create DMA maps for TX buffers. */
2064 	for (i = 0; i < BGE_TX_RING_CNT; i++) {
2065 		error = bus_dmamap_create(sc->bge_cdata.bge_mtag, 0,
2066 			    &sc->bge_cdata.bge_tx_dmamap[i]);
2067 		if (error) {
2068 			device_printf(sc->bge_dev,
2069 			    "can't create DMA map for RX\n");
2070 			return (ENOMEM);
2071 		}
2072 	}
2073 
2074 	/* Create tag for standard RX ring. */
2075 	error = bus_dma_tag_create(sc->bge_cdata.bge_parent_tag,
2076 	    PAGE_SIZE, 0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL,
2077 	    NULL, BGE_STD_RX_RING_SZ, 1, BGE_STD_RX_RING_SZ, 0,
2078 	    NULL, NULL, &sc->bge_cdata.bge_rx_std_ring_tag);
2079 
2080 	if (error) {
2081 		device_printf(sc->bge_dev, "could not allocate dma tag\n");
2082 		return (ENOMEM);
2083 	}
2084 
2085 	/* Allocate DMA'able memory for standard RX ring. */
2086 	error = bus_dmamem_alloc(sc->bge_cdata.bge_rx_std_ring_tag,
2087 	    (void **)&sc->bge_ldata.bge_rx_std_ring, BUS_DMA_NOWAIT,
2088 	    &sc->bge_cdata.bge_rx_std_ring_map);
2089 	if (error)
2090 		return (ENOMEM);
2091 
2092 	bzero((char *)sc->bge_ldata.bge_rx_std_ring, BGE_STD_RX_RING_SZ);
2093 
2094 	/* Load the address of the standard RX ring. */
2095 	ctx.bge_maxsegs = 1;
2096 	ctx.sc = sc;
2097 
2098 	error = bus_dmamap_load(sc->bge_cdata.bge_rx_std_ring_tag,
2099 	    sc->bge_cdata.bge_rx_std_ring_map, sc->bge_ldata.bge_rx_std_ring,
2100 	    BGE_STD_RX_RING_SZ, bge_dma_map_addr, &ctx, BUS_DMA_NOWAIT);
2101 
2102 	if (error)
2103 		return (ENOMEM);
2104 
2105 	sc->bge_ldata.bge_rx_std_ring_paddr = ctx.bge_busaddr;
2106 
2107 	/* Create tags for jumbo mbufs. */
2108 	if (BGE_IS_JUMBO_CAPABLE(sc)) {
2109 		error = bus_dma_tag_create(sc->bge_cdata.bge_parent_tag,
2110 		    1, 0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL,
2111 		    NULL, MJUM9BYTES, BGE_NSEG_JUMBO, PAGE_SIZE,
2112 		    0, NULL, NULL, &sc->bge_cdata.bge_mtag_jumbo);
2113 		if (error) {
2114 			device_printf(sc->bge_dev,
2115 			    "could not allocate jumbo dma tag\n");
2116 			return (ENOMEM);
2117 		}
2118 
2119 		/* Create tag for jumbo RX ring. */
2120 		error = bus_dma_tag_create(sc->bge_cdata.bge_parent_tag,
2121 		    PAGE_SIZE, 0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL,
2122 		    NULL, BGE_JUMBO_RX_RING_SZ, 1, BGE_JUMBO_RX_RING_SZ, 0,
2123 		    NULL, NULL, &sc->bge_cdata.bge_rx_jumbo_ring_tag);
2124 
2125 		if (error) {
2126 			device_printf(sc->bge_dev,
2127 			    "could not allocate jumbo ring dma tag\n");
2128 			return (ENOMEM);
2129 		}
2130 
2131 		/* Allocate DMA'able memory for jumbo RX ring. */
2132 		error = bus_dmamem_alloc(sc->bge_cdata.bge_rx_jumbo_ring_tag,
2133 		    (void **)&sc->bge_ldata.bge_rx_jumbo_ring,
2134 		    BUS_DMA_NOWAIT | BUS_DMA_ZERO,
2135 		    &sc->bge_cdata.bge_rx_jumbo_ring_map);
2136 		if (error)
2137 			return (ENOMEM);
2138 
2139 		/* Load the address of the jumbo RX ring. */
2140 		ctx.bge_maxsegs = 1;
2141 		ctx.sc = sc;
2142 
2143 		error = bus_dmamap_load(sc->bge_cdata.bge_rx_jumbo_ring_tag,
2144 		    sc->bge_cdata.bge_rx_jumbo_ring_map,
2145 		    sc->bge_ldata.bge_rx_jumbo_ring, BGE_JUMBO_RX_RING_SZ,
2146 		    bge_dma_map_addr, &ctx, BUS_DMA_NOWAIT);
2147 
2148 		if (error)
2149 			return (ENOMEM);
2150 
2151 		sc->bge_ldata.bge_rx_jumbo_ring_paddr = ctx.bge_busaddr;
2152 
2153 		/* Create DMA maps for jumbo RX buffers. */
2154 		for (i = 0; i < BGE_JUMBO_RX_RING_CNT; i++) {
2155 			error = bus_dmamap_create(sc->bge_cdata.bge_mtag_jumbo,
2156 				    0, &sc->bge_cdata.bge_rx_jumbo_dmamap[i]);
2157 			if (error) {
2158 				device_printf(sc->bge_dev,
2159 				    "can't create DMA map for jumbo RX\n");
2160 				return (ENOMEM);
2161 			}
2162 		}
2163 
2164 	}
2165 
2166 	/* Create tag for RX return ring. */
2167 	error = bus_dma_tag_create(sc->bge_cdata.bge_parent_tag,
2168 	    PAGE_SIZE, 0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL,
2169 	    NULL, BGE_RX_RTN_RING_SZ(sc), 1, BGE_RX_RTN_RING_SZ(sc), 0,
2170 	    NULL, NULL, &sc->bge_cdata.bge_rx_return_ring_tag);
2171 
2172 	if (error) {
2173 		device_printf(sc->bge_dev, "could not allocate dma tag\n");
2174 		return (ENOMEM);
2175 	}
2176 
2177 	/* Allocate DMA'able memory for RX return ring. */
2178 	error = bus_dmamem_alloc(sc->bge_cdata.bge_rx_return_ring_tag,
2179 	    (void **)&sc->bge_ldata.bge_rx_return_ring, BUS_DMA_NOWAIT,
2180 	    &sc->bge_cdata.bge_rx_return_ring_map);
2181 	if (error)
2182 		return (ENOMEM);
2183 
2184 	bzero((char *)sc->bge_ldata.bge_rx_return_ring,
2185 	    BGE_RX_RTN_RING_SZ(sc));
2186 
2187 	/* Load the address of the RX return ring. */
2188 	ctx.bge_maxsegs = 1;
2189 	ctx.sc = sc;
2190 
2191 	error = bus_dmamap_load(sc->bge_cdata.bge_rx_return_ring_tag,
2192 	    sc->bge_cdata.bge_rx_return_ring_map,
2193 	    sc->bge_ldata.bge_rx_return_ring, BGE_RX_RTN_RING_SZ(sc),
2194 	    bge_dma_map_addr, &ctx, BUS_DMA_NOWAIT);
2195 
2196 	if (error)
2197 		return (ENOMEM);
2198 
2199 	sc->bge_ldata.bge_rx_return_ring_paddr = ctx.bge_busaddr;
2200 
2201 	/* Create tag for TX ring. */
2202 	error = bus_dma_tag_create(sc->bge_cdata.bge_parent_tag,
2203 	    PAGE_SIZE, 0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL,
2204 	    NULL, BGE_TX_RING_SZ, 1, BGE_TX_RING_SZ, 0, NULL, NULL,
2205 	    &sc->bge_cdata.bge_tx_ring_tag);
2206 
2207 	if (error) {
2208 		device_printf(sc->bge_dev, "could not allocate dma tag\n");
2209 		return (ENOMEM);
2210 	}
2211 
2212 	/* Allocate DMA'able memory for TX ring. */
2213 	error = bus_dmamem_alloc(sc->bge_cdata.bge_tx_ring_tag,
2214 	    (void **)&sc->bge_ldata.bge_tx_ring, BUS_DMA_NOWAIT,
2215 	    &sc->bge_cdata.bge_tx_ring_map);
2216 	if (error)
2217 		return (ENOMEM);
2218 
2219 	bzero((char *)sc->bge_ldata.bge_tx_ring, BGE_TX_RING_SZ);
2220 
2221 	/* Load the address of the TX ring. */
2222 	ctx.bge_maxsegs = 1;
2223 	ctx.sc = sc;
2224 
2225 	error = bus_dmamap_load(sc->bge_cdata.bge_tx_ring_tag,
2226 	    sc->bge_cdata.bge_tx_ring_map, sc->bge_ldata.bge_tx_ring,
2227 	    BGE_TX_RING_SZ, bge_dma_map_addr, &ctx, BUS_DMA_NOWAIT);
2228 
2229 	if (error)
2230 		return (ENOMEM);
2231 
2232 	sc->bge_ldata.bge_tx_ring_paddr = ctx.bge_busaddr;
2233 
2234 	/* Create tag for status block. */
2235 	error = bus_dma_tag_create(sc->bge_cdata.bge_parent_tag,
2236 	    PAGE_SIZE, 0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL,
2237 	    NULL, BGE_STATUS_BLK_SZ, 1, BGE_STATUS_BLK_SZ, 0,
2238 	    NULL, NULL, &sc->bge_cdata.bge_status_tag);
2239 
2240 	if (error) {
2241 		device_printf(sc->bge_dev, "could not allocate dma tag\n");
2242 		return (ENOMEM);
2243 	}
2244 
2245 	/* Allocate DMA'able memory for status block. */
2246 	error = bus_dmamem_alloc(sc->bge_cdata.bge_status_tag,
2247 	    (void **)&sc->bge_ldata.bge_status_block, BUS_DMA_NOWAIT,
2248 	    &sc->bge_cdata.bge_status_map);
2249 	if (error)
2250 		return (ENOMEM);
2251 
2252 	bzero((char *)sc->bge_ldata.bge_status_block, BGE_STATUS_BLK_SZ);
2253 
2254 	/* Load the address of the status block. */
2255 	ctx.sc = sc;
2256 	ctx.bge_maxsegs = 1;
2257 
2258 	error = bus_dmamap_load(sc->bge_cdata.bge_status_tag,
2259 	    sc->bge_cdata.bge_status_map, sc->bge_ldata.bge_status_block,
2260 	    BGE_STATUS_BLK_SZ, bge_dma_map_addr, &ctx, BUS_DMA_NOWAIT);
2261 
2262 	if (error)
2263 		return (ENOMEM);
2264 
2265 	sc->bge_ldata.bge_status_block_paddr = ctx.bge_busaddr;
2266 
2267 	/* Create tag for statistics block. */
2268 	error = bus_dma_tag_create(sc->bge_cdata.bge_parent_tag,
2269 	    PAGE_SIZE, 0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL,
2270 	    NULL, BGE_STATS_SZ, 1, BGE_STATS_SZ, 0, NULL, NULL,
2271 	    &sc->bge_cdata.bge_stats_tag);
2272 
2273 	if (error) {
2274 		device_printf(sc->bge_dev, "could not allocate dma tag\n");
2275 		return (ENOMEM);
2276 	}
2277 
2278 	/* Allocate DMA'able memory for statistics block. */
2279 	error = bus_dmamem_alloc(sc->bge_cdata.bge_stats_tag,
2280 	    (void **)&sc->bge_ldata.bge_stats, BUS_DMA_NOWAIT,
2281 	    &sc->bge_cdata.bge_stats_map);
2282 	if (error)
2283 		return (ENOMEM);
2284 
2285 	bzero((char *)sc->bge_ldata.bge_stats, BGE_STATS_SZ);
2286 
2287 	/* Load the address of the statstics block. */
2288 	ctx.sc = sc;
2289 	ctx.bge_maxsegs = 1;
2290 
2291 	error = bus_dmamap_load(sc->bge_cdata.bge_stats_tag,
2292 	    sc->bge_cdata.bge_stats_map, sc->bge_ldata.bge_stats,
2293 	    BGE_STATS_SZ, bge_dma_map_addr, &ctx, BUS_DMA_NOWAIT);
2294 
2295 	if (error)
2296 		return (ENOMEM);
2297 
2298 	sc->bge_ldata.bge_stats_paddr = ctx.bge_busaddr;
2299 
2300 	return (0);
2301 }
2302 
2303 #if __FreeBSD_version > 602105
2304 /*
2305  * Return true if this device has more than one port.
2306  */
2307 static int
2308 bge_has_multiple_ports(struct bge_softc *sc)
2309 {
2310 	device_t dev = sc->bge_dev;
2311 	u_int b, d, f, fscan, s;
2312 
2313 	d = pci_get_domain(dev);
2314 	b = pci_get_bus(dev);
2315 	s = pci_get_slot(dev);
2316 	f = pci_get_function(dev);
2317 	for (fscan = 0; fscan <= PCI_FUNCMAX; fscan++)
2318 		if (fscan != f && pci_find_dbsf(d, b, s, fscan) != NULL)
2319 			return (1);
2320 	return (0);
2321 }
2322 
2323 /*
2324  * Return true if MSI can be used with this device.
2325  */
2326 static int
2327 bge_can_use_msi(struct bge_softc *sc)
2328 {
2329 	int can_use_msi = 0;
2330 
2331 	switch (sc->bge_asicrev) {
2332 	case BGE_ASICREV_BCM5714:
2333 		/*
2334 		 * Apparently, MSI doesn't work when this chip is configured
2335 		 * in single-port mode.
2336 		 */
2337 		if (bge_has_multiple_ports(sc))
2338 			can_use_msi = 1;
2339 		break;
2340 	case BGE_ASICREV_BCM5750:
2341 		if (sc->bge_chiprev != BGE_CHIPREV_5750_AX &&
2342 		    sc->bge_chiprev != BGE_CHIPREV_5750_BX)
2343 			can_use_msi = 1;
2344 		break;
2345 	case BGE_ASICREV_BCM5752:
2346 	case BGE_ASICREV_BCM5780:
2347 		can_use_msi = 1;
2348 		break;
2349 	}
2350 	return (can_use_msi);
2351 }
2352 #endif
2353 
2354 static int
2355 bge_attach(device_t dev)
2356 {
2357 	struct ifnet *ifp;
2358 	struct bge_softc *sc;
2359 	uint32_t hwcfg = 0, misccfg;
2360 	u_char eaddr[ETHER_ADDR_LEN];
2361 	int error, reg, rid, trys;
2362 
2363 	sc = device_get_softc(dev);
2364 	sc->bge_dev = dev;
2365 
2366 	/*
2367 	 * Map control/status registers.
2368 	 */
2369 	pci_enable_busmaster(dev);
2370 
2371 	rid = BGE_PCI_BAR0;
2372 	sc->bge_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
2373 	    RF_ACTIVE);
2374 
2375 	if (sc->bge_res == NULL) {
2376 		device_printf (sc->bge_dev, "couldn't map memory\n");
2377 		error = ENXIO;
2378 		goto fail;
2379 	}
2380 
2381 	sc->bge_btag = rman_get_bustag(sc->bge_res);
2382 	sc->bge_bhandle = rman_get_bushandle(sc->bge_res);
2383 
2384 	/* Save ASIC rev. */
2385 
2386 	sc->bge_chipid =
2387 	    pci_read_config(dev, BGE_PCI_MISC_CTL, 4) &
2388 	    BGE_PCIMISCCTL_ASICREV;
2389 	sc->bge_asicrev = BGE_ASICREV(sc->bge_chipid);
2390 	sc->bge_chiprev = BGE_CHIPREV(sc->bge_chipid);
2391 
2392 	/*
2393 	 * Don't enable Ethernet@WireSpeed for the 5700, 5906, or the
2394 	 * 5705 A0 and A1 chips.
2395 	 */
2396 	if (sc->bge_asicrev != BGE_ASICREV_BCM5700 &&
2397 	    sc->bge_asicrev != BGE_ASICREV_BCM5906 &&
2398 	    sc->bge_chipid != BGE_CHIPID_BCM5705_A0 &&
2399 	    sc->bge_chipid != BGE_CHIPID_BCM5705_A1)
2400 		sc->bge_flags |= BGE_FLAG_WIRESPEED;
2401 
2402 	if (bge_has_eeprom(sc))
2403 		sc->bge_flags |= BGE_FLAG_EEPROM;
2404 
2405 	/* Save chipset family. */
2406 	switch (sc->bge_asicrev) {
2407 	case BGE_ASICREV_BCM5700:
2408 	case BGE_ASICREV_BCM5701:
2409 	case BGE_ASICREV_BCM5703:
2410 	case BGE_ASICREV_BCM5704:
2411 		sc->bge_flags |= BGE_FLAG_5700_FAMILY | BGE_FLAG_JUMBO;
2412 		break;
2413 	case BGE_ASICREV_BCM5714_A0:
2414 	case BGE_ASICREV_BCM5780:
2415 	case BGE_ASICREV_BCM5714:
2416 		sc->bge_flags |= BGE_FLAG_5714_FAMILY /* | BGE_FLAG_JUMBO */;
2417 		/* FALLTHRU */
2418 	case BGE_ASICREV_BCM5750:
2419 	case BGE_ASICREV_BCM5752:
2420 	case BGE_ASICREV_BCM5755:
2421 	case BGE_ASICREV_BCM5787:
2422 	case BGE_ASICREV_BCM5906:
2423 		sc->bge_flags |= BGE_FLAG_575X_PLUS;
2424 		/* FALLTHRU */
2425 	case BGE_ASICREV_BCM5705:
2426 		sc->bge_flags |= BGE_FLAG_5705_PLUS;
2427 		break;
2428 	}
2429 
2430 	/* Set various bug flags. */
2431 	if (sc->bge_chipid == BGE_CHIPID_BCM5701_A0 ||
2432 	    sc->bge_chipid == BGE_CHIPID_BCM5701_B0)
2433 		sc->bge_flags |= BGE_FLAG_CRC_BUG;
2434 	if (sc->bge_chiprev == BGE_CHIPREV_5703_AX ||
2435 	    sc->bge_chiprev == BGE_CHIPREV_5704_AX)
2436 		sc->bge_flags |= BGE_FLAG_ADC_BUG;
2437 	if (sc->bge_chipid == BGE_CHIPID_BCM5704_A0)
2438 		sc->bge_flags |= BGE_FLAG_5704_A0_BUG;
2439 	if (BGE_IS_5705_PLUS(sc) &&
2440 	    !(sc->bge_flags & BGE_FLAG_ADJUST_TRIM)) {
2441 		if (sc->bge_asicrev == BGE_ASICREV_BCM5755 ||
2442 		    sc->bge_asicrev == BGE_ASICREV_BCM5787) {
2443 			if (sc->bge_chipid != BGE_CHIPID_BCM5722_A0)
2444 				sc->bge_flags |= BGE_FLAG_JITTER_BUG;
2445 		} else if (sc->bge_asicrev != BGE_ASICREV_BCM5906)
2446 			sc->bge_flags |= BGE_FLAG_BER_BUG;
2447 	}
2448 
2449 
2450 	/*
2451 	 * We could possibly check for BCOM_DEVICEID_BCM5788 in bge_probe()
2452 	 * but I do not know the DEVICEID for the 5788M.
2453 	 */
2454 	misccfg = CSR_READ_4(sc, BGE_MISC_CFG) & BGE_MISCCFG_BOARD_ID;
2455 	if (misccfg == BGE_MISCCFG_BOARD_ID_5788 ||
2456 	    misccfg == BGE_MISCCFG_BOARD_ID_5788M)
2457 		sc->bge_flags |= BGE_FLAG_5788;
2458 
2459   	/*
2460 	 * Check if this is a PCI-X or PCI Express device.
2461   	 */
2462 #if __FreeBSD_version > 602101
2463 	if (pci_find_extcap(dev, PCIY_EXPRESS, &reg) == 0) {
2464 		/*
2465 		 * Found a PCI Express capabilities register, this
2466 		 * must be a PCI Express device.
2467 		 */
2468 		if (reg != 0)
2469 			sc->bge_flags |= BGE_FLAG_PCIE;
2470 	} else if (pci_find_extcap(dev, PCIY_PCIX, &reg) == 0) {
2471 		if (reg != 0)
2472 			sc->bge_flags |= BGE_FLAG_PCIX;
2473 	}
2474 
2475 #else
2476 	if (BGE_IS_5705_PLUS(sc)) {
2477 		reg = pci_read_config(dev, BGE_PCIE_CAPID_REG, 4);
2478 		if ((reg & 0xFF) == BGE_PCIE_CAPID)
2479 			sc->bge_flags |= BGE_FLAG_PCIE;
2480 	} else {
2481 		/*
2482 		 * Check if the device is in PCI-X Mode.
2483 		 * (This bit is not valid on PCI Express controllers.)
2484 		 */
2485 		if ((pci_read_config(sc->bge_dev, BGE_PCI_PCISTATE, 4) &
2486 		    BGE_PCISTATE_PCI_BUSMODE) == 0)
2487 			sc->bge_flags |= BGE_FLAG_PCIX;
2488 	}
2489 #endif
2490 
2491 #if __FreeBSD_version > 602105
2492 	{
2493 		int msicount;
2494 
2495 		/*
2496 		 * Allocate the interrupt, using MSI if possible.  These devices
2497 		 * support 8 MSI messages, but only the first one is used in
2498 		 * normal operation.
2499 		 */
2500 		if (bge_can_use_msi(sc)) {
2501 			msicount = pci_msi_count(dev);
2502 			if (msicount > 1)
2503 				msicount = 1;
2504 		} else
2505 			msicount = 0;
2506 		if (msicount == 1 && pci_alloc_msi(dev, &msicount) == 0) {
2507 			rid = 1;
2508 			sc->bge_flags |= BGE_FLAG_MSI;
2509 		} else
2510 			rid = 0;
2511 	}
2512 #else
2513 	rid = 0;
2514 #endif
2515 
2516 	sc->bge_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
2517 	    RF_SHAREABLE | RF_ACTIVE);
2518 
2519 	if (sc->bge_irq == NULL) {
2520 		device_printf(sc->bge_dev, "couldn't map interrupt\n");
2521 		error = ENXIO;
2522 		goto fail;
2523 	}
2524 
2525 	BGE_LOCK_INIT(sc, device_get_nameunit(dev));
2526 
2527 	/* Try to reset the chip. */
2528 	if (bge_reset(sc)) {
2529 		device_printf(sc->bge_dev, "chip reset failed\n");
2530 		error = ENXIO;
2531 		goto fail;
2532 	}
2533 
2534 	sc->bge_asf_mode = 0;
2535 	if (bge_allow_asf && (bge_readmem_ind(sc, BGE_SOFTWARE_GENCOMM_SIG)
2536 	    == BGE_MAGIC_NUMBER)) {
2537 		if (bge_readmem_ind(sc, BGE_SOFTWARE_GENCOMM_NICCFG)
2538 		    & BGE_HWCFG_ASF) {
2539 			sc->bge_asf_mode |= ASF_ENABLE;
2540 			sc->bge_asf_mode |= ASF_STACKUP;
2541 			if (sc->bge_asicrev == BGE_ASICREV_BCM5750) {
2542 				sc->bge_asf_mode |= ASF_NEW_HANDSHAKE;
2543 			}
2544 		}
2545 	}
2546 
2547 	/* Try to reset the chip again the nice way. */
2548 	bge_stop_fw(sc);
2549 	bge_sig_pre_reset(sc, BGE_RESET_STOP);
2550 	if (bge_reset(sc)) {
2551 		device_printf(sc->bge_dev, "chip reset failed\n");
2552 		error = ENXIO;
2553 		goto fail;
2554 	}
2555 
2556 	bge_sig_legacy(sc, BGE_RESET_STOP);
2557 	bge_sig_post_reset(sc, BGE_RESET_STOP);
2558 
2559 	if (bge_chipinit(sc)) {
2560 		device_printf(sc->bge_dev, "chip initialization failed\n");
2561 		error = ENXIO;
2562 		goto fail;
2563 	}
2564 
2565 #ifdef __sparc64__
2566 	if (((sc->bge_flags & BGE_FLAG_EEPROM) == 0) &&
2567 	    (sc->bge_asicrev != BGE_ASICREV_BCM5906))
2568 		OF_getetheraddr(dev, eaddr);
2569 	else
2570 #endif
2571 	{
2572 		error = bge_get_eaddr(sc, eaddr);
2573 		if (error) {
2574 			device_printf(sc->bge_dev,
2575 			    "failed to read station address\n");
2576 			error = ENXIO;
2577 			goto fail;
2578 		}
2579 	}
2580 
2581 	/* 5705 limits RX return ring to 512 entries. */
2582 	if (BGE_IS_5705_PLUS(sc))
2583 		sc->bge_return_ring_cnt = BGE_RETURN_RING_CNT_5705;
2584 	else
2585 		sc->bge_return_ring_cnt = BGE_RETURN_RING_CNT;
2586 
2587 	if (bge_dma_alloc(dev)) {
2588 		device_printf(sc->bge_dev,
2589 		    "failed to allocate DMA resources\n");
2590 		error = ENXIO;
2591 		goto fail;
2592 	}
2593 
2594 	/* Set default tuneable values. */
2595 	sc->bge_stat_ticks = BGE_TICKS_PER_SEC;
2596 	sc->bge_rx_coal_ticks = 150;
2597 	sc->bge_tx_coal_ticks = 150;
2598 	sc->bge_rx_max_coal_bds = 10;
2599 	sc->bge_tx_max_coal_bds = 10;
2600 
2601 	/* Set up ifnet structure */
2602 	ifp = sc->bge_ifp = if_alloc(IFT_ETHER);
2603 	if (ifp == NULL) {
2604 		device_printf(sc->bge_dev, "failed to if_alloc()\n");
2605 		error = ENXIO;
2606 		goto fail;
2607 	}
2608 	ifp->if_softc = sc;
2609 	if_initname(ifp, device_get_name(dev), device_get_unit(dev));
2610 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
2611 	ifp->if_ioctl = bge_ioctl;
2612 	ifp->if_start = bge_start;
2613 	ifp->if_init = bge_init;
2614 	ifp->if_mtu = ETHERMTU;
2615 	ifp->if_snd.ifq_drv_maxlen = BGE_TX_RING_CNT - 1;
2616 	IFQ_SET_MAXLEN(&ifp->if_snd, ifp->if_snd.ifq_drv_maxlen);
2617 	IFQ_SET_READY(&ifp->if_snd);
2618 	ifp->if_hwassist = BGE_CSUM_FEATURES;
2619 	ifp->if_capabilities = IFCAP_HWCSUM | IFCAP_VLAN_HWTAGGING |
2620 	    IFCAP_VLAN_MTU;
2621 #ifdef IFCAP_VLAN_HWCSUM
2622 	ifp->if_capabilities |= IFCAP_VLAN_HWCSUM;
2623 #endif
2624 	ifp->if_capenable = ifp->if_capabilities;
2625 #ifdef DEVICE_POLLING
2626 	ifp->if_capabilities |= IFCAP_POLLING;
2627 #endif
2628 
2629 	/*
2630 	 * 5700 B0 chips do not support checksumming correctly due
2631 	 * to hardware bugs.
2632 	 */
2633 	if (sc->bge_chipid == BGE_CHIPID_BCM5700_B0) {
2634 		ifp->if_capabilities &= ~IFCAP_HWCSUM;
2635 		ifp->if_capenable &= IFCAP_HWCSUM;
2636 		ifp->if_hwassist = 0;
2637 	}
2638 
2639 	/*
2640 	 * Figure out what sort of media we have by checking the
2641 	 * hardware config word in the first 32k of NIC internal memory,
2642 	 * or fall back to examining the EEPROM if necessary.
2643 	 * Note: on some BCM5700 cards, this value appears to be unset.
2644 	 * If that's the case, we have to rely on identifying the NIC
2645 	 * by its PCI subsystem ID, as we do below for the SysKonnect
2646 	 * SK-9D41.
2647 	 */
2648 	if (bge_readmem_ind(sc, BGE_SOFTWARE_GENCOMM_SIG) == BGE_MAGIC_NUMBER)
2649 		hwcfg = bge_readmem_ind(sc, BGE_SOFTWARE_GENCOMM_NICCFG);
2650 	else if (sc->bge_flags & BGE_FLAG_EEPROM) {
2651 		if (bge_read_eeprom(sc, (caddr_t)&hwcfg, BGE_EE_HWCFG_OFFSET,
2652 		    sizeof(hwcfg))) {
2653 			device_printf(sc->bge_dev, "failed to read EEPROM\n");
2654 			error = ENXIO;
2655 			goto fail;
2656 		}
2657 		hwcfg = ntohl(hwcfg);
2658 	}
2659 
2660 	if ((hwcfg & BGE_HWCFG_MEDIA) == BGE_MEDIA_FIBER)
2661 		sc->bge_flags |= BGE_FLAG_TBI;
2662 
2663 	/* The SysKonnect SK-9D41 is a 1000baseSX card. */
2664 	if ((pci_read_config(dev, BGE_PCI_SUBSYS, 4) >> 16) == SK_SUBSYSID_9D41)
2665 		sc->bge_flags |= BGE_FLAG_TBI;
2666 
2667 	if (sc->bge_flags & BGE_FLAG_TBI) {
2668 		ifmedia_init(&sc->bge_ifmedia, IFM_IMASK, bge_ifmedia_upd,
2669 		    bge_ifmedia_sts);
2670 		ifmedia_add(&sc->bge_ifmedia, IFM_ETHER | IFM_1000_SX, 0, NULL);
2671 		ifmedia_add(&sc->bge_ifmedia, IFM_ETHER | IFM_1000_SX | IFM_FDX,
2672 		    0, NULL);
2673 		ifmedia_add(&sc->bge_ifmedia, IFM_ETHER | IFM_AUTO, 0, NULL);
2674 		ifmedia_set(&sc->bge_ifmedia, IFM_ETHER | IFM_AUTO);
2675 		sc->bge_ifmedia.ifm_media = sc->bge_ifmedia.ifm_cur->ifm_media;
2676 	} else {
2677 		/*
2678 		 * Do transceiver setup and tell the firmware the
2679 		 * driver is down so we can try to get access the
2680 		 * probe if ASF is running.  Retry a couple of times
2681 		 * if we get a conflict with the ASF firmware accessing
2682 		 * the PHY.
2683 		 */
2684 		BGE_CLRBIT(sc, BGE_MODE_CTL, BGE_MODECTL_STACKUP);
2685 again:
2686 		bge_asf_driver_up(sc);
2687 
2688 		trys = 0;
2689 		if (mii_phy_probe(dev, &sc->bge_miibus,
2690 		    bge_ifmedia_upd, bge_ifmedia_sts)) {
2691 			if (trys++ < 4) {
2692 				device_printf(sc->bge_dev, "Try again\n");
2693 				bge_miibus_writereg(sc->bge_dev, 1, MII_BMCR,
2694 				    BMCR_RESET);
2695 				goto again;
2696 			}
2697 
2698 			device_printf(sc->bge_dev, "MII without any PHY!\n");
2699 			error = ENXIO;
2700 			goto fail;
2701 		}
2702 
2703 		/*
2704 		 * Now tell the firmware we are going up after probing the PHY
2705 		 */
2706 		if (sc->bge_asf_mode & ASF_STACKUP)
2707 			BGE_SETBIT(sc, BGE_MODE_CTL, BGE_MODECTL_STACKUP);
2708 	}
2709 
2710 	/*
2711 	 * When using the BCM5701 in PCI-X mode, data corruption has
2712 	 * been observed in the first few bytes of some received packets.
2713 	 * Aligning the packet buffer in memory eliminates the corruption.
2714 	 * Unfortunately, this misaligns the packet payloads.  On platforms
2715 	 * which do not support unaligned accesses, we will realign the
2716 	 * payloads by copying the received packets.
2717 	 */
2718 	if (sc->bge_asicrev == BGE_ASICREV_BCM5701 &&
2719 	    sc->bge_flags & BGE_FLAG_PCIX)
2720                 sc->bge_flags |= BGE_FLAG_RX_ALIGNBUG;
2721 
2722 	/*
2723 	 * Call MI attach routine.
2724 	 */
2725 	ether_ifattach(ifp, eaddr);
2726 	callout_init_mtx(&sc->bge_stat_ch, &sc->bge_mtx, 0);
2727 
2728 	/*
2729 	 * Hookup IRQ last.
2730 	 */
2731 #if __FreeBSD_version > 700030
2732 	error = bus_setup_intr(dev, sc->bge_irq, INTR_TYPE_NET | INTR_MPSAFE,
2733 	   NULL, bge_intr, sc, &sc->bge_intrhand);
2734 #else
2735 	error = bus_setup_intr(dev, sc->bge_irq, INTR_TYPE_NET | INTR_MPSAFE,
2736 	   bge_intr, sc, &sc->bge_intrhand);
2737 #endif
2738 
2739 	if (error) {
2740 		bge_detach(dev);
2741 		device_printf(sc->bge_dev, "couldn't set up irq\n");
2742 	}
2743 
2744 	bge_add_sysctls(sc);
2745 
2746 	return (0);
2747 
2748 fail:
2749 	bge_release_resources(sc);
2750 
2751 	return (error);
2752 }
2753 
2754 static int
2755 bge_detach(device_t dev)
2756 {
2757 	struct bge_softc *sc;
2758 	struct ifnet *ifp;
2759 
2760 	sc = device_get_softc(dev);
2761 	ifp = sc->bge_ifp;
2762 
2763 #ifdef DEVICE_POLLING
2764 	if (ifp->if_capenable & IFCAP_POLLING)
2765 		ether_poll_deregister(ifp);
2766 #endif
2767 
2768 	BGE_LOCK(sc);
2769 	bge_stop(sc);
2770 	bge_reset(sc);
2771 	BGE_UNLOCK(sc);
2772 
2773 	callout_drain(&sc->bge_stat_ch);
2774 
2775 	ether_ifdetach(ifp);
2776 
2777 	if (sc->bge_flags & BGE_FLAG_TBI) {
2778 		ifmedia_removeall(&sc->bge_ifmedia);
2779 	} else {
2780 		bus_generic_detach(dev);
2781 		device_delete_child(dev, sc->bge_miibus);
2782 	}
2783 
2784 	bge_release_resources(sc);
2785 
2786 	return (0);
2787 }
2788 
2789 static void
2790 bge_release_resources(struct bge_softc *sc)
2791 {
2792 	device_t dev;
2793 
2794 	dev = sc->bge_dev;
2795 
2796 	if (sc->bge_intrhand != NULL)
2797 		bus_teardown_intr(dev, sc->bge_irq, sc->bge_intrhand);
2798 
2799 	if (sc->bge_irq != NULL)
2800 		bus_release_resource(dev, SYS_RES_IRQ,
2801 		    sc->bge_flags & BGE_FLAG_MSI ? 1 : 0, sc->bge_irq);
2802 
2803 #if __FreeBSD_version > 602105
2804 	if (sc->bge_flags & BGE_FLAG_MSI)
2805 		pci_release_msi(dev);
2806 #endif
2807 
2808 	if (sc->bge_res != NULL)
2809 		bus_release_resource(dev, SYS_RES_MEMORY,
2810 		    BGE_PCI_BAR0, sc->bge_res);
2811 
2812 	if (sc->bge_ifp != NULL)
2813 		if_free(sc->bge_ifp);
2814 
2815 	bge_dma_free(sc);
2816 
2817 	if (mtx_initialized(&sc->bge_mtx))	/* XXX */
2818 		BGE_LOCK_DESTROY(sc);
2819 }
2820 
2821 static int
2822 bge_reset(struct bge_softc *sc)
2823 {
2824 	device_t dev;
2825 	uint32_t cachesize, command, pcistate, reset;
2826 	void (*write_op)(struct bge_softc *, int, int);
2827 	int i, val = 0;
2828 
2829 	dev = sc->bge_dev;
2830 
2831 	if (BGE_IS_575X_PLUS(sc) && !BGE_IS_5714_FAMILY(sc) &&
2832 	    (sc->bge_asicrev != BGE_ASICREV_BCM5906)) {
2833 		if (sc->bge_flags & BGE_FLAG_PCIE)
2834 			write_op = bge_writemem_direct;
2835 		else
2836 			write_op = bge_writemem_ind;
2837 	} else
2838 		write_op = bge_writereg_ind;
2839 
2840 	/* Save some important PCI state. */
2841 	cachesize = pci_read_config(dev, BGE_PCI_CACHESZ, 4);
2842 	command = pci_read_config(dev, BGE_PCI_CMD, 4);
2843 	pcistate = pci_read_config(dev, BGE_PCI_PCISTATE, 4);
2844 
2845 	pci_write_config(dev, BGE_PCI_MISC_CTL,
2846 	    BGE_PCIMISCCTL_INDIRECT_ACCESS | BGE_PCIMISCCTL_MASK_PCI_INTR |
2847 	    BGE_HIF_SWAP_OPTIONS | BGE_PCIMISCCTL_PCISTATE_RW, 4);
2848 
2849 	/* Disable fastboot on controllers that support it. */
2850 	if (sc->bge_asicrev == BGE_ASICREV_BCM5752 ||
2851 	    sc->bge_asicrev == BGE_ASICREV_BCM5755 ||
2852 	    sc->bge_asicrev == BGE_ASICREV_BCM5787) {
2853 		if (bootverbose)
2854 			device_printf(sc->bge_dev, "Disabling fastboot\n");
2855 		CSR_WRITE_4(sc, BGE_FASTBOOT_PC, 0x0);
2856 	}
2857 
2858 	/*
2859 	 * Write the magic number to SRAM at offset 0xB50.
2860 	 * When firmware finishes its initialization it will
2861 	 * write ~BGE_MAGIC_NUMBER to the same location.
2862 	 */
2863 	bge_writemem_ind(sc, BGE_SOFTWARE_GENCOMM, BGE_MAGIC_NUMBER);
2864 
2865 	reset = BGE_MISCCFG_RESET_CORE_CLOCKS | BGE_32BITTIME_66MHZ;
2866 
2867 	/* XXX: Broadcom Linux driver. */
2868 	if (sc->bge_flags & BGE_FLAG_PCIE) {
2869 		if (CSR_READ_4(sc, 0x7E2C) == 0x60)	/* PCIE 1.0 */
2870 			CSR_WRITE_4(sc, 0x7E2C, 0x20);
2871 		if (sc->bge_chipid != BGE_CHIPID_BCM5750_A0) {
2872 			/* Prevent PCIE link training during global reset */
2873 			CSR_WRITE_4(sc, BGE_MISC_CFG, 1 << 29);
2874 			reset |= 1 << 29;
2875 		}
2876 	}
2877 
2878 	/*
2879 	 * Set GPHY Power Down Override to leave GPHY
2880 	 * powered up in D0 uninitialized.
2881 	 */
2882 	if (BGE_IS_5705_PLUS(sc))
2883 		reset |= 0x04000000;
2884 
2885 	/* Issue global reset */
2886 	write_op(sc, BGE_MISC_CFG, reset);
2887 
2888 	if (sc->bge_asicrev == BGE_ASICREV_BCM5906) {
2889 		uint32_t status, ctrl;
2890 
2891 		status = CSR_READ_4(sc, BGE_VCPU_STATUS);
2892 		CSR_WRITE_4(sc, BGE_VCPU_STATUS,
2893 		    status | BGE_VCPU_STATUS_DRV_RESET);
2894 		ctrl = CSR_READ_4(sc, BGE_VCPU_EXT_CTRL);
2895 		CSR_WRITE_4(sc, BGE_VCPU_EXT_CTRL,
2896 		    ctrl & ~BGE_VCPU_EXT_CTRL_HALT_CPU);
2897 	}
2898 
2899 	DELAY(1000);
2900 
2901 	/* XXX: Broadcom Linux driver. */
2902 	if (sc->bge_flags & BGE_FLAG_PCIE) {
2903 		if (sc->bge_chipid == BGE_CHIPID_BCM5750_A0) {
2904 			uint32_t v;
2905 
2906 			DELAY(500000); /* wait for link training to complete */
2907 			v = pci_read_config(dev, 0xC4, 4);
2908 			pci_write_config(dev, 0xC4, v | (1 << 15), 4);
2909 		}
2910 		/*
2911 		 * Set PCIE max payload size to 128 bytes and clear error
2912 		 * status.
2913 		 */
2914 		pci_write_config(dev, 0xD8, 0xF5000, 4);
2915 	}
2916 
2917 	/* Reset some of the PCI state that got zapped by reset. */
2918 	pci_write_config(dev, BGE_PCI_MISC_CTL,
2919 	    BGE_PCIMISCCTL_INDIRECT_ACCESS | BGE_PCIMISCCTL_MASK_PCI_INTR |
2920 	    BGE_HIF_SWAP_OPTIONS | BGE_PCIMISCCTL_PCISTATE_RW, 4);
2921 	pci_write_config(dev, BGE_PCI_CACHESZ, cachesize, 4);
2922 	pci_write_config(dev, BGE_PCI_CMD, command, 4);
2923 	write_op(sc, BGE_MISC_CFG, BGE_32BITTIME_66MHZ);
2924 
2925 	/* Re-enable MSI, if neccesary, and enable the memory arbiter. */
2926 	if (BGE_IS_5714_FAMILY(sc)) {
2927 		uint32_t val;
2928 
2929 		/* This chip disables MSI on reset. */
2930 		if (sc->bge_flags & BGE_FLAG_MSI) {
2931 			val = pci_read_config(dev, BGE_PCI_MSI_CTL, 2);
2932 			pci_write_config(dev, BGE_PCI_MSI_CTL,
2933 			    val | PCIM_MSICTRL_MSI_ENABLE, 2);
2934 			val = CSR_READ_4(sc, BGE_MSI_MODE);
2935 			CSR_WRITE_4(sc, BGE_MSI_MODE,
2936 			    val | BGE_MSIMODE_ENABLE);
2937 		}
2938 		val = CSR_READ_4(sc, BGE_MARB_MODE);
2939 		CSR_WRITE_4(sc, BGE_MARB_MODE, BGE_MARBMODE_ENABLE | val);
2940 	} else
2941 		CSR_WRITE_4(sc, BGE_MARB_MODE, BGE_MARBMODE_ENABLE);
2942 
2943 	if (sc->bge_asicrev == BGE_ASICREV_BCM5906) {
2944 		for (i = 0; i < BGE_TIMEOUT; i++) {
2945 			val = CSR_READ_4(sc, BGE_VCPU_STATUS);
2946 			if (val & BGE_VCPU_STATUS_INIT_DONE)
2947 				break;
2948 			DELAY(100);
2949 		}
2950 		if (i == BGE_TIMEOUT) {
2951 			device_printf(sc->bge_dev, "reset timed out\n");
2952 			return (1);
2953 		}
2954 	} else {
2955 		/*
2956 		 * Poll until we see the 1's complement of the magic number.
2957 		 * This indicates that the firmware initialization is complete.
2958 		 * We expect this to fail if no EEPROM is fitted though.
2959 		 */
2960 		for (i = 0; i < BGE_TIMEOUT; i++) {
2961 			DELAY(10);
2962 			val = bge_readmem_ind(sc, BGE_SOFTWARE_GENCOMM);
2963 			if (val == ~BGE_MAGIC_NUMBER)
2964 				break;
2965 		}
2966 
2967 		if ((sc->bge_flags & BGE_FLAG_EEPROM) && i == BGE_TIMEOUT)
2968 			device_printf(sc->bge_dev, "firmware handshake timed out, "
2969 			    "found 0x%08x\n", val);
2970 	}
2971 
2972 	/*
2973 	 * XXX Wait for the value of the PCISTATE register to
2974 	 * return to its original pre-reset state. This is a
2975 	 * fairly good indicator of reset completion. If we don't
2976 	 * wait for the reset to fully complete, trying to read
2977 	 * from the device's non-PCI registers may yield garbage
2978 	 * results.
2979 	 */
2980 	for (i = 0; i < BGE_TIMEOUT; i++) {
2981 		if (pci_read_config(dev, BGE_PCI_PCISTATE, 4) == pcistate)
2982 			break;
2983 		DELAY(10);
2984 	}
2985 
2986 	if (sc->bge_flags & BGE_FLAG_PCIE) {
2987 		reset = bge_readmem_ind(sc, 0x7C00);
2988 		bge_writemem_ind(sc, 0x7C00, reset | (1 << 25));
2989 	}
2990 
2991 	/* Fix up byte swapping. */
2992 	CSR_WRITE_4(sc, BGE_MODE_CTL, BGE_DMA_SWAP_OPTIONS |
2993 	    BGE_MODECTL_BYTESWAP_DATA);
2994 
2995 	/* Tell the ASF firmware we are up */
2996 	if (sc->bge_asf_mode & ASF_STACKUP)
2997 		BGE_SETBIT(sc, BGE_MODE_CTL, BGE_MODECTL_STACKUP);
2998 
2999 	CSR_WRITE_4(sc, BGE_MAC_MODE, 0);
3000 
3001 	/*
3002 	 * The 5704 in TBI mode apparently needs some special
3003 	 * adjustment to insure the SERDES drive level is set
3004 	 * to 1.2V.
3005 	 */
3006 	if (sc->bge_asicrev == BGE_ASICREV_BCM5704 &&
3007 	    sc->bge_flags & BGE_FLAG_TBI) {
3008 		uint32_t serdescfg;
3009 
3010 		serdescfg = CSR_READ_4(sc, BGE_SERDES_CFG);
3011 		serdescfg = (serdescfg & ~0xFFF) | 0x880;
3012 		CSR_WRITE_4(sc, BGE_SERDES_CFG, serdescfg);
3013 	}
3014 
3015 	/* XXX: Broadcom Linux driver. */
3016 	if (sc->bge_flags & BGE_FLAG_PCIE &&
3017 	    sc->bge_chipid != BGE_CHIPID_BCM5750_A0) {
3018 		uint32_t v;
3019 
3020 		v = CSR_READ_4(sc, 0x7C00);
3021 		CSR_WRITE_4(sc, 0x7C00, v | (1 << 25));
3022 	}
3023 	DELAY(10000);
3024 
3025 	return(0);
3026 }
3027 
3028 /*
3029  * Frame reception handling. This is called if there's a frame
3030  * on the receive return list.
3031  *
3032  * Note: we have to be able to handle two possibilities here:
3033  * 1) the frame is from the jumbo receive ring
3034  * 2) the frame is from the standard receive ring
3035  */
3036 
3037 static void
3038 bge_rxeof(struct bge_softc *sc)
3039 {
3040 	struct ifnet *ifp;
3041 	int stdcnt = 0, jumbocnt = 0;
3042 
3043 	BGE_LOCK_ASSERT(sc);
3044 
3045 	/* Nothing to do. */
3046 	if (sc->bge_rx_saved_considx ==
3047 	    sc->bge_ldata.bge_status_block->bge_idx[0].bge_rx_prod_idx)
3048 		return;
3049 
3050 	ifp = sc->bge_ifp;
3051 
3052 	bus_dmamap_sync(sc->bge_cdata.bge_rx_return_ring_tag,
3053 	    sc->bge_cdata.bge_rx_return_ring_map, BUS_DMASYNC_POSTREAD);
3054 	bus_dmamap_sync(sc->bge_cdata.bge_rx_std_ring_tag,
3055 	    sc->bge_cdata.bge_rx_std_ring_map, BUS_DMASYNC_POSTREAD);
3056 	if (BGE_IS_JUMBO_CAPABLE(sc))
3057 		bus_dmamap_sync(sc->bge_cdata.bge_rx_jumbo_ring_tag,
3058 		    sc->bge_cdata.bge_rx_jumbo_ring_map, BUS_DMASYNC_POSTREAD);
3059 
3060 	while(sc->bge_rx_saved_considx !=
3061 	    sc->bge_ldata.bge_status_block->bge_idx[0].bge_rx_prod_idx) {
3062 		struct bge_rx_bd	*cur_rx;
3063 		uint32_t		rxidx;
3064 		struct mbuf		*m = NULL;
3065 		uint16_t		vlan_tag = 0;
3066 		int			have_tag = 0;
3067 
3068 #ifdef DEVICE_POLLING
3069 		if (ifp->if_capenable & IFCAP_POLLING) {
3070 			if (sc->rxcycles <= 0)
3071 				break;
3072 			sc->rxcycles--;
3073 		}
3074 #endif
3075 
3076 		cur_rx =
3077 	    &sc->bge_ldata.bge_rx_return_ring[sc->bge_rx_saved_considx];
3078 
3079 		rxidx = cur_rx->bge_idx;
3080 		BGE_INC(sc->bge_rx_saved_considx, sc->bge_return_ring_cnt);
3081 
3082 		if (ifp->if_capenable & IFCAP_VLAN_HWTAGGING &&
3083 		    cur_rx->bge_flags & BGE_RXBDFLAG_VLAN_TAG) {
3084 			have_tag = 1;
3085 			vlan_tag = cur_rx->bge_vlan_tag;
3086 		}
3087 
3088 		if (cur_rx->bge_flags & BGE_RXBDFLAG_JUMBO_RING) {
3089 			BGE_INC(sc->bge_jumbo, BGE_JUMBO_RX_RING_CNT);
3090 			bus_dmamap_sync(sc->bge_cdata.bge_mtag_jumbo,
3091 			    sc->bge_cdata.bge_rx_jumbo_dmamap[rxidx],
3092 			    BUS_DMASYNC_POSTREAD);
3093 			bus_dmamap_unload(sc->bge_cdata.bge_mtag_jumbo,
3094 			    sc->bge_cdata.bge_rx_jumbo_dmamap[rxidx]);
3095 			m = sc->bge_cdata.bge_rx_jumbo_chain[rxidx];
3096 			sc->bge_cdata.bge_rx_jumbo_chain[rxidx] = NULL;
3097 			jumbocnt++;
3098 			if (cur_rx->bge_flags & BGE_RXBDFLAG_ERROR) {
3099 				ifp->if_ierrors++;
3100 				bge_newbuf_jumbo(sc, sc->bge_jumbo, m);
3101 				continue;
3102 			}
3103 			if (bge_newbuf_jumbo(sc,
3104 			    sc->bge_jumbo, NULL) == ENOBUFS) {
3105 				ifp->if_ierrors++;
3106 				bge_newbuf_jumbo(sc, sc->bge_jumbo, m);
3107 				continue;
3108 			}
3109 		} else {
3110 			BGE_INC(sc->bge_std, BGE_STD_RX_RING_CNT);
3111 			bus_dmamap_sync(sc->bge_cdata.bge_mtag,
3112 			    sc->bge_cdata.bge_rx_std_dmamap[rxidx],
3113 			    BUS_DMASYNC_POSTREAD);
3114 			bus_dmamap_unload(sc->bge_cdata.bge_mtag,
3115 			    sc->bge_cdata.bge_rx_std_dmamap[rxidx]);
3116 			m = sc->bge_cdata.bge_rx_std_chain[rxidx];
3117 			sc->bge_cdata.bge_rx_std_chain[rxidx] = NULL;
3118 			stdcnt++;
3119 			if (cur_rx->bge_flags & BGE_RXBDFLAG_ERROR) {
3120 				ifp->if_ierrors++;
3121 				bge_newbuf_std(sc, sc->bge_std, m);
3122 				continue;
3123 			}
3124 			if (bge_newbuf_std(sc, sc->bge_std,
3125 			    NULL) == ENOBUFS) {
3126 				ifp->if_ierrors++;
3127 				bge_newbuf_std(sc, sc->bge_std, m);
3128 				continue;
3129 			}
3130 		}
3131 
3132 		ifp->if_ipackets++;
3133 #ifndef __NO_STRICT_ALIGNMENT
3134 		/*
3135 		 * For architectures with strict alignment we must make sure
3136 		 * the payload is aligned.
3137 		 */
3138 		if (sc->bge_flags & BGE_FLAG_RX_ALIGNBUG) {
3139 			bcopy(m->m_data, m->m_data + ETHER_ALIGN,
3140 			    cur_rx->bge_len);
3141 			m->m_data += ETHER_ALIGN;
3142 		}
3143 #endif
3144 		m->m_pkthdr.len = m->m_len = cur_rx->bge_len - ETHER_CRC_LEN;
3145 		m->m_pkthdr.rcvif = ifp;
3146 
3147 		if (ifp->if_capenable & IFCAP_RXCSUM) {
3148 			if (cur_rx->bge_flags & BGE_RXBDFLAG_IP_CSUM) {
3149 				m->m_pkthdr.csum_flags |= CSUM_IP_CHECKED;
3150 				if ((cur_rx->bge_ip_csum ^ 0xFFFF) == 0)
3151 					m->m_pkthdr.csum_flags |= CSUM_IP_VALID;
3152 			}
3153 			if (cur_rx->bge_flags & BGE_RXBDFLAG_TCP_UDP_CSUM &&
3154 			    m->m_pkthdr.len >= ETHER_MIN_NOPAD) {
3155 				m->m_pkthdr.csum_data =
3156 				    cur_rx->bge_tcp_udp_csum;
3157 				m->m_pkthdr.csum_flags |=
3158 				    CSUM_DATA_VALID | CSUM_PSEUDO_HDR;
3159 			}
3160 		}
3161 
3162 		/*
3163 		 * If we received a packet with a vlan tag,
3164 		 * attach that information to the packet.
3165 		 */
3166 		if (have_tag) {
3167 #if __FreeBSD_version > 700022
3168 			m->m_pkthdr.ether_vtag = vlan_tag;
3169 			m->m_flags |= M_VLANTAG;
3170 #else
3171 			VLAN_INPUT_TAG_NEW(ifp, m, vlan_tag);
3172 			if (m == NULL)
3173 				continue;
3174 #endif
3175 		}
3176 
3177 		BGE_UNLOCK(sc);
3178 		(*ifp->if_input)(ifp, m);
3179 		BGE_LOCK(sc);
3180 	}
3181 
3182 	if (stdcnt > 0)
3183 		bus_dmamap_sync(sc->bge_cdata.bge_rx_std_ring_tag,
3184 		    sc->bge_cdata.bge_rx_std_ring_map, BUS_DMASYNC_PREWRITE);
3185 
3186 	if (BGE_IS_JUMBO_CAPABLE(sc) && jumbocnt > 0)
3187 		bus_dmamap_sync(sc->bge_cdata.bge_rx_jumbo_ring_tag,
3188 		    sc->bge_cdata.bge_rx_jumbo_ring_map, BUS_DMASYNC_PREWRITE);
3189 
3190 	bge_writembx(sc, BGE_MBX_RX_CONS0_LO, sc->bge_rx_saved_considx);
3191 	if (stdcnt)
3192 		bge_writembx(sc, BGE_MBX_RX_STD_PROD_LO, sc->bge_std);
3193 	if (jumbocnt)
3194 		bge_writembx(sc, BGE_MBX_RX_JUMBO_PROD_LO, sc->bge_jumbo);
3195 #ifdef notyet
3196 	/*
3197 	 * This register wraps very quickly under heavy packet drops.
3198 	 * If you need correct statistics, you can enable this check.
3199 	 */
3200 	if (BGE_IS_5705_PLUS(sc))
3201 		ifp->if_ierrors += CSR_READ_4(sc, BGE_RXLP_LOCSTAT_IFIN_DROPS);
3202 #endif
3203 }
3204 
3205 static void
3206 bge_txeof(struct bge_softc *sc)
3207 {
3208 	struct bge_tx_bd *cur_tx = NULL;
3209 	struct ifnet *ifp;
3210 
3211 	BGE_LOCK_ASSERT(sc);
3212 
3213 	/* Nothing to do. */
3214 	if (sc->bge_tx_saved_considx ==
3215 	    sc->bge_ldata.bge_status_block->bge_idx[0].bge_tx_cons_idx)
3216 		return;
3217 
3218 	ifp = sc->bge_ifp;
3219 
3220 	bus_dmamap_sync(sc->bge_cdata.bge_tx_ring_tag,
3221 	    sc->bge_cdata.bge_tx_ring_map,
3222 	    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
3223 	/*
3224 	 * Go through our tx ring and free mbufs for those
3225 	 * frames that have been sent.
3226 	 */
3227 	while (sc->bge_tx_saved_considx !=
3228 	    sc->bge_ldata.bge_status_block->bge_idx[0].bge_tx_cons_idx) {
3229 		uint32_t		idx = 0;
3230 
3231 		idx = sc->bge_tx_saved_considx;
3232 		cur_tx = &sc->bge_ldata.bge_tx_ring[idx];
3233 		if (cur_tx->bge_flags & BGE_TXBDFLAG_END)
3234 			ifp->if_opackets++;
3235 		if (sc->bge_cdata.bge_tx_chain[idx] != NULL) {
3236 			bus_dmamap_sync(sc->bge_cdata.bge_mtag,
3237 			    sc->bge_cdata.bge_tx_dmamap[idx],
3238 			    BUS_DMASYNC_POSTWRITE);
3239 			bus_dmamap_unload(sc->bge_cdata.bge_mtag,
3240 			    sc->bge_cdata.bge_tx_dmamap[idx]);
3241 			m_freem(sc->bge_cdata.bge_tx_chain[idx]);
3242 			sc->bge_cdata.bge_tx_chain[idx] = NULL;
3243 		}
3244 		sc->bge_txcnt--;
3245 		BGE_INC(sc->bge_tx_saved_considx, BGE_TX_RING_CNT);
3246 	}
3247 
3248 	if (cur_tx != NULL)
3249 		ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
3250 	if (sc->bge_txcnt == 0)
3251 		sc->bge_timer = 0;
3252 }
3253 
3254 #ifdef DEVICE_POLLING
3255 static void
3256 bge_poll(struct ifnet *ifp, enum poll_cmd cmd, int count)
3257 {
3258 	struct bge_softc *sc = ifp->if_softc;
3259 	uint32_t statusword;
3260 
3261 	BGE_LOCK(sc);
3262 	if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
3263 		BGE_UNLOCK(sc);
3264 		return;
3265 	}
3266 
3267 	bus_dmamap_sync(sc->bge_cdata.bge_status_tag,
3268 	    sc->bge_cdata.bge_status_map, BUS_DMASYNC_POSTREAD);
3269 
3270 	statusword = atomic_readandclear_32(
3271 	    &sc->bge_ldata.bge_status_block->bge_status);
3272 
3273 	bus_dmamap_sync(sc->bge_cdata.bge_status_tag,
3274 	    sc->bge_cdata.bge_status_map, BUS_DMASYNC_PREREAD);
3275 
3276 	/* Note link event. It will be processed by POLL_AND_CHECK_STATUS. */
3277 	if (statusword & BGE_STATFLAG_LINKSTATE_CHANGED)
3278 		sc->bge_link_evt++;
3279 
3280 	if (cmd == POLL_AND_CHECK_STATUS)
3281 		if ((sc->bge_asicrev == BGE_ASICREV_BCM5700 &&
3282 		    sc->bge_chipid != BGE_CHIPID_BCM5700_B2) ||
3283 		    sc->bge_link_evt || (sc->bge_flags & BGE_FLAG_TBI))
3284 			bge_link_upd(sc);
3285 
3286 	sc->rxcycles = count;
3287 	bge_rxeof(sc);
3288 	bge_txeof(sc);
3289 	if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd))
3290 		bge_start_locked(ifp);
3291 
3292 	BGE_UNLOCK(sc);
3293 }
3294 #endif /* DEVICE_POLLING */
3295 
3296 static void
3297 bge_intr(void *xsc)
3298 {
3299 	struct bge_softc *sc;
3300 	struct ifnet *ifp;
3301 	uint32_t statusword;
3302 
3303 	sc = xsc;
3304 
3305 	BGE_LOCK(sc);
3306 
3307 	ifp = sc->bge_ifp;
3308 
3309 #ifdef DEVICE_POLLING
3310 	if (ifp->if_capenable & IFCAP_POLLING) {
3311 		BGE_UNLOCK(sc);
3312 		return;
3313 	}
3314 #endif
3315 
3316 	/*
3317 	 * Ack the interrupt by writing something to BGE_MBX_IRQ0_LO.  Don't
3318 	 * disable interrupts by writing nonzero like we used to, since with
3319 	 * our current organization this just gives complications and
3320 	 * pessimizations for re-enabling interrupts.  We used to have races
3321 	 * instead of the necessary complications.  Disabling interrupts
3322 	 * would just reduce the chance of a status update while we are
3323 	 * running (by switching to the interrupt-mode coalescence
3324 	 * parameters), but this chance is already very low so it is more
3325 	 * efficient to get another interrupt than prevent it.
3326 	 *
3327 	 * We do the ack first to ensure another interrupt if there is a
3328 	 * status update after the ack.  We don't check for the status
3329 	 * changing later because it is more efficient to get another
3330 	 * interrupt than prevent it, not quite as above (not checking is
3331 	 * a smaller optimization than not toggling the interrupt enable,
3332 	 * since checking doesn't involve PCI accesses and toggling require
3333 	 * the status check).  So toggling would probably be a pessimization
3334 	 * even with MSI.  It would only be needed for using a task queue.
3335 	 */
3336 	bge_writembx(sc, BGE_MBX_IRQ0_LO, 0);
3337 
3338 	/*
3339 	 * Do the mandatory PCI flush as well as get the link status.
3340 	 */
3341 	statusword = CSR_READ_4(sc, BGE_MAC_STS) & BGE_MACSTAT_LINK_CHANGED;
3342 
3343 	/* Make sure the descriptor ring indexes are coherent. */
3344 	bus_dmamap_sync(sc->bge_cdata.bge_status_tag,
3345 	    sc->bge_cdata.bge_status_map, BUS_DMASYNC_POSTREAD);
3346 	bus_dmamap_sync(sc->bge_cdata.bge_status_tag,
3347 	    sc->bge_cdata.bge_status_map, BUS_DMASYNC_PREREAD);
3348 
3349 	if ((sc->bge_asicrev == BGE_ASICREV_BCM5700 &&
3350 	    sc->bge_chipid != BGE_CHIPID_BCM5700_B2) ||
3351 	    statusword || sc->bge_link_evt)
3352 		bge_link_upd(sc);
3353 
3354 	if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
3355 		/* Check RX return ring producer/consumer. */
3356 		bge_rxeof(sc);
3357 
3358 		/* Check TX ring producer/consumer. */
3359 		bge_txeof(sc);
3360 	}
3361 
3362 	if (ifp->if_drv_flags & IFF_DRV_RUNNING &&
3363 	    !IFQ_DRV_IS_EMPTY(&ifp->if_snd))
3364 		bge_start_locked(ifp);
3365 
3366 	BGE_UNLOCK(sc);
3367 }
3368 
3369 static void
3370 bge_asf_driver_up(struct bge_softc *sc)
3371 {
3372 	if (sc->bge_asf_mode & ASF_STACKUP) {
3373 		/* Send ASF heartbeat aprox. every 2s */
3374 		if (sc->bge_asf_count)
3375 			sc->bge_asf_count --;
3376 		else {
3377 			sc->bge_asf_count = 5;
3378 			bge_writemem_ind(sc, BGE_SOFTWARE_GENCOMM_FW,
3379 			    BGE_FW_DRV_ALIVE);
3380 			bge_writemem_ind(sc, BGE_SOFTWARE_GENNCOMM_FW_LEN, 4);
3381 			bge_writemem_ind(sc, BGE_SOFTWARE_GENNCOMM_FW_DATA, 3);
3382 			CSR_WRITE_4(sc, BGE_CPU_EVENT,
3383 			    CSR_READ_4(sc, BGE_CPU_EVENT) | (1 << 14));
3384 		}
3385 	}
3386 }
3387 
3388 static void
3389 bge_tick(void *xsc)
3390 {
3391 	struct bge_softc *sc = xsc;
3392 	struct mii_data *mii = NULL;
3393 
3394 	BGE_LOCK_ASSERT(sc);
3395 
3396 	/* Synchronize with possible callout reset/stop. */
3397 	if (callout_pending(&sc->bge_stat_ch) ||
3398 	    !callout_active(&sc->bge_stat_ch))
3399 	    	return;
3400 
3401 	if (BGE_IS_5705_PLUS(sc))
3402 		bge_stats_update_regs(sc);
3403 	else
3404 		bge_stats_update(sc);
3405 
3406 	if ((sc->bge_flags & BGE_FLAG_TBI) == 0) {
3407 		mii = device_get_softc(sc->bge_miibus);
3408 		/* Don't mess with the PHY in IPMI/ASF mode */
3409 		if (!((sc->bge_asf_mode & ASF_STACKUP) && (sc->bge_link)))
3410 			mii_tick(mii);
3411 	} else {
3412 		/*
3413 		 * Since in TBI mode auto-polling can't be used we should poll
3414 		 * link status manually. Here we register pending link event
3415 		 * and trigger interrupt.
3416 		 */
3417 #ifdef DEVICE_POLLING
3418 		/* In polling mode we poll link state in bge_poll(). */
3419 		if (!(sc->bge_ifp->if_capenable & IFCAP_POLLING))
3420 #endif
3421 		{
3422 		sc->bge_link_evt++;
3423 		if (sc->bge_asicrev == BGE_ASICREV_BCM5700 ||
3424 		    sc->bge_flags & BGE_FLAG_5788)
3425 			BGE_SETBIT(sc, BGE_MISC_LOCAL_CTL, BGE_MLC_INTR_SET);
3426 		else
3427 			BGE_SETBIT(sc, BGE_HCC_MODE, BGE_HCCMODE_COAL_NOW);
3428 		}
3429 	}
3430 
3431 	bge_asf_driver_up(sc);
3432 	bge_watchdog(sc);
3433 
3434 	callout_reset(&sc->bge_stat_ch, hz, bge_tick, sc);
3435 }
3436 
3437 static void
3438 bge_stats_update_regs(struct bge_softc *sc)
3439 {
3440 	struct ifnet *ifp;
3441 
3442 	ifp = sc->bge_ifp;
3443 
3444 	ifp->if_collisions += CSR_READ_4(sc, BGE_MAC_STATS +
3445 	    offsetof(struct bge_mac_stats_regs, etherStatsCollisions));
3446 
3447 	ifp->if_ierrors += CSR_READ_4(sc, BGE_RXLP_LOCSTAT_IFIN_DROPS);
3448 }
3449 
3450 static void
3451 bge_stats_update(struct bge_softc *sc)
3452 {
3453 	struct ifnet *ifp;
3454 	bus_size_t stats;
3455 	uint32_t cnt;	/* current register value */
3456 
3457 	ifp = sc->bge_ifp;
3458 
3459 	stats = BGE_MEMWIN_START + BGE_STATS_BLOCK;
3460 
3461 #define	READ_STAT(sc, stats, stat) \
3462 	CSR_READ_4(sc, stats + offsetof(struct bge_stats, stat))
3463 
3464 	cnt = READ_STAT(sc, stats, txstats.etherStatsCollisions.bge_addr_lo);
3465 	ifp->if_collisions += (uint32_t)(cnt - sc->bge_tx_collisions);
3466 	sc->bge_tx_collisions = cnt;
3467 
3468 	cnt = READ_STAT(sc, stats, ifInDiscards.bge_addr_lo);
3469 	ifp->if_ierrors += (uint32_t)(cnt - sc->bge_rx_discards);
3470 	sc->bge_rx_discards = cnt;
3471 
3472 	cnt = READ_STAT(sc, stats, txstats.ifOutDiscards.bge_addr_lo);
3473 	ifp->if_oerrors += (uint32_t)(cnt - sc->bge_tx_discards);
3474 	sc->bge_tx_discards = cnt;
3475 
3476 #undef	READ_STAT
3477 }
3478 
3479 /*
3480  * Pad outbound frame to ETHER_MIN_NOPAD for an unusual reason.
3481  * The bge hardware will pad out Tx runts to ETHER_MIN_NOPAD,
3482  * but when such padded frames employ the bge IP/TCP checksum offload,
3483  * the hardware checksum assist gives incorrect results (possibly
3484  * from incorporating its own padding into the UDP/TCP checksum; who knows).
3485  * If we pad such runts with zeros, the onboard checksum comes out correct.
3486  */
3487 static __inline int
3488 bge_cksum_pad(struct mbuf *m)
3489 {
3490 	int padlen = ETHER_MIN_NOPAD - m->m_pkthdr.len;
3491 	struct mbuf *last;
3492 
3493 	/* If there's only the packet-header and we can pad there, use it. */
3494 	if (m->m_pkthdr.len == m->m_len && M_WRITABLE(m) &&
3495 	    M_TRAILINGSPACE(m) >= padlen) {
3496 		last = m;
3497 	} else {
3498 		/*
3499 		 * Walk packet chain to find last mbuf. We will either
3500 		 * pad there, or append a new mbuf and pad it.
3501 		 */
3502 		for (last = m; last->m_next != NULL; last = last->m_next);
3503 		if (!(M_WRITABLE(last) && M_TRAILINGSPACE(last) >= padlen)) {
3504 			/* Allocate new empty mbuf, pad it. Compact later. */
3505 			struct mbuf *n;
3506 
3507 			MGET(n, M_DONTWAIT, MT_DATA);
3508 			if (n == NULL)
3509 				return (ENOBUFS);
3510 			n->m_len = 0;
3511 			last->m_next = n;
3512 			last = n;
3513 		}
3514 	}
3515 
3516 	/* Now zero the pad area, to avoid the bge cksum-assist bug. */
3517 	memset(mtod(last, caddr_t) + last->m_len, 0, padlen);
3518 	last->m_len += padlen;
3519 	m->m_pkthdr.len += padlen;
3520 
3521 	return (0);
3522 }
3523 
3524 /*
3525  * Encapsulate an mbuf chain in the tx ring  by coupling the mbuf data
3526  * pointers to descriptors.
3527  */
3528 static int
3529 bge_encap(struct bge_softc *sc, struct mbuf **m_head, uint32_t *txidx)
3530 {
3531 	bus_dma_segment_t	segs[BGE_NSEG_NEW];
3532 	bus_dmamap_t		map;
3533 	struct bge_tx_bd	*d;
3534 	struct mbuf		*m = *m_head;
3535 	uint32_t		idx = *txidx;
3536 	uint16_t		csum_flags;
3537 	int			nsegs, i, error;
3538 
3539 	csum_flags = 0;
3540 	if (m->m_pkthdr.csum_flags) {
3541 		if (m->m_pkthdr.csum_flags & CSUM_IP)
3542 			csum_flags |= BGE_TXBDFLAG_IP_CSUM;
3543 		if (m->m_pkthdr.csum_flags & (CSUM_TCP | CSUM_UDP)) {
3544 			csum_flags |= BGE_TXBDFLAG_TCP_UDP_CSUM;
3545 			if (m->m_pkthdr.len < ETHER_MIN_NOPAD &&
3546 			    (error = bge_cksum_pad(m)) != 0) {
3547 				m_freem(m);
3548 				*m_head = NULL;
3549 				return (error);
3550 			}
3551 		}
3552 		if (m->m_flags & M_LASTFRAG)
3553 			csum_flags |= BGE_TXBDFLAG_IP_FRAG_END;
3554 		else if (m->m_flags & M_FRAG)
3555 			csum_flags |= BGE_TXBDFLAG_IP_FRAG;
3556 	}
3557 
3558 	map = sc->bge_cdata.bge_tx_dmamap[idx];
3559 	error = bus_dmamap_load_mbuf_sg(sc->bge_cdata.bge_mtag, map, m, segs,
3560 	    &nsegs, BUS_DMA_NOWAIT);
3561 	if (error == EFBIG) {
3562 		m = m_collapse(m, M_DONTWAIT, BGE_NSEG_NEW);
3563 		if (m == NULL) {
3564 			m_freem(*m_head);
3565 			*m_head = NULL;
3566 			return (ENOBUFS);
3567 		}
3568 		*m_head = m;
3569 		error = bus_dmamap_load_mbuf_sg(sc->bge_cdata.bge_mtag, map, m,
3570 		    segs, &nsegs, BUS_DMA_NOWAIT);
3571 		if (error) {
3572 			m_freem(m);
3573 			*m_head = NULL;
3574 			return (error);
3575 		}
3576 	} else if (error != 0)
3577 		return (error);
3578 
3579 	/*
3580 	 * Sanity check: avoid coming within 16 descriptors
3581 	 * of the end of the ring.
3582 	 */
3583 	if (nsegs > (BGE_TX_RING_CNT - sc->bge_txcnt - 16)) {
3584 		bus_dmamap_unload(sc->bge_cdata.bge_mtag, map);
3585 		return (ENOBUFS);
3586 	}
3587 
3588 	bus_dmamap_sync(sc->bge_cdata.bge_mtag, map, BUS_DMASYNC_PREWRITE);
3589 
3590 	for (i = 0; ; i++) {
3591 		d = &sc->bge_ldata.bge_tx_ring[idx];
3592 		d->bge_addr.bge_addr_lo = BGE_ADDR_LO(segs[i].ds_addr);
3593 		d->bge_addr.bge_addr_hi = BGE_ADDR_HI(segs[i].ds_addr);
3594 		d->bge_len = segs[i].ds_len;
3595 		d->bge_flags = csum_flags;
3596 		if (i == nsegs - 1)
3597 			break;
3598 		BGE_INC(idx, BGE_TX_RING_CNT);
3599 	}
3600 
3601 	/* Mark the last segment as end of packet... */
3602 	d->bge_flags |= BGE_TXBDFLAG_END;
3603 
3604 	/* ... and put VLAN tag into first segment.  */
3605 	d = &sc->bge_ldata.bge_tx_ring[*txidx];
3606 #if __FreeBSD_version > 700022
3607 	if (m->m_flags & M_VLANTAG) {
3608 		d->bge_flags |= BGE_TXBDFLAG_VLAN_TAG;
3609 		d->bge_vlan_tag = m->m_pkthdr.ether_vtag;
3610 	} else
3611 		d->bge_vlan_tag = 0;
3612 #else
3613 	{
3614 		struct m_tag		*mtag;
3615 
3616 		if ((mtag = VLAN_OUTPUT_TAG(sc->bge_ifp, m)) != NULL) {
3617 			d->bge_flags |= BGE_TXBDFLAG_VLAN_TAG;
3618 			d->bge_vlan_tag = VLAN_TAG_VALUE(mtag);
3619 		} else
3620 			d->bge_vlan_tag = 0;
3621 	}
3622 #endif
3623 
3624 	/*
3625 	 * Insure that the map for this transmission
3626 	 * is placed at the array index of the last descriptor
3627 	 * in this chain.
3628 	 */
3629 	sc->bge_cdata.bge_tx_dmamap[*txidx] = sc->bge_cdata.bge_tx_dmamap[idx];
3630 	sc->bge_cdata.bge_tx_dmamap[idx] = map;
3631 	sc->bge_cdata.bge_tx_chain[idx] = m;
3632 	sc->bge_txcnt += nsegs;
3633 
3634 	BGE_INC(idx, BGE_TX_RING_CNT);
3635 	*txidx = idx;
3636 
3637 	return (0);
3638 }
3639 
3640 /*
3641  * Main transmit routine. To avoid having to do mbuf copies, we put pointers
3642  * to the mbuf data regions directly in the transmit descriptors.
3643  */
3644 static void
3645 bge_start_locked(struct ifnet *ifp)
3646 {
3647 	struct bge_softc *sc;
3648 	struct mbuf *m_head = NULL;
3649 	uint32_t prodidx;
3650 	int count = 0;
3651 
3652 	sc = ifp->if_softc;
3653 
3654 	if (!sc->bge_link || IFQ_DRV_IS_EMPTY(&ifp->if_snd))
3655 		return;
3656 
3657 	prodidx = sc->bge_tx_prodidx;
3658 
3659 	while(sc->bge_cdata.bge_tx_chain[prodidx] == NULL) {
3660 		IFQ_DRV_DEQUEUE(&ifp->if_snd, m_head);
3661 		if (m_head == NULL)
3662 			break;
3663 
3664 		/*
3665 		 * XXX
3666 		 * The code inside the if() block is never reached since we
3667 		 * must mark CSUM_IP_FRAGS in our if_hwassist to start getting
3668 		 * requests to checksum TCP/UDP in a fragmented packet.
3669 		 *
3670 		 * XXX
3671 		 * safety overkill.  If this is a fragmented packet chain
3672 		 * with delayed TCP/UDP checksums, then only encapsulate
3673 		 * it if we have enough descriptors to handle the entire
3674 		 * chain at once.
3675 		 * (paranoia -- may not actually be needed)
3676 		 */
3677 		if (m_head->m_flags & M_FIRSTFRAG &&
3678 		    m_head->m_pkthdr.csum_flags & (CSUM_DELAY_DATA)) {
3679 			if ((BGE_TX_RING_CNT - sc->bge_txcnt) <
3680 			    m_head->m_pkthdr.csum_data + 16) {
3681 				IFQ_DRV_PREPEND(&ifp->if_snd, m_head);
3682 				ifp->if_drv_flags |= IFF_DRV_OACTIVE;
3683 				break;
3684 			}
3685 		}
3686 
3687 		/*
3688 		 * Pack the data into the transmit ring. If we
3689 		 * don't have room, set the OACTIVE flag and wait
3690 		 * for the NIC to drain the ring.
3691 		 */
3692 		if (bge_encap(sc, &m_head, &prodidx)) {
3693 			if (m_head == NULL)
3694 				break;
3695 			IFQ_DRV_PREPEND(&ifp->if_snd, m_head);
3696 			ifp->if_drv_flags |= IFF_DRV_OACTIVE;
3697 			break;
3698 		}
3699 		++count;
3700 
3701 		/*
3702 		 * If there's a BPF listener, bounce a copy of this frame
3703 		 * to him.
3704 		 */
3705 #ifdef ETHER_BPF_MTAP
3706 		ETHER_BPF_MTAP(ifp, m_head);
3707 #else
3708 		BPF_MTAP(ifp, m_head);
3709 #endif
3710 	}
3711 
3712 	if (count == 0)
3713 		/* No packets were dequeued. */
3714 		return;
3715 
3716 	/* Transmit. */
3717 	bge_writembx(sc, BGE_MBX_TX_HOST_PROD0_LO, prodidx);
3718 	/* 5700 b2 errata */
3719 	if (sc->bge_chiprev == BGE_CHIPREV_5700_BX)
3720 		bge_writembx(sc, BGE_MBX_TX_HOST_PROD0_LO, prodidx);
3721 
3722 	sc->bge_tx_prodidx = prodidx;
3723 
3724 	/*
3725 	 * Set a timeout in case the chip goes out to lunch.
3726 	 */
3727 	sc->bge_timer = 5;
3728 }
3729 
3730 /*
3731  * Main transmit routine. To avoid having to do mbuf copies, we put pointers
3732  * to the mbuf data regions directly in the transmit descriptors.
3733  */
3734 static void
3735 bge_start(struct ifnet *ifp)
3736 {
3737 	struct bge_softc *sc;
3738 
3739 	sc = ifp->if_softc;
3740 	BGE_LOCK(sc);
3741 	bge_start_locked(ifp);
3742 	BGE_UNLOCK(sc);
3743 }
3744 
3745 static void
3746 bge_init_locked(struct bge_softc *sc)
3747 {
3748 	struct ifnet *ifp;
3749 	uint16_t *m;
3750 
3751 	BGE_LOCK_ASSERT(sc);
3752 
3753 	ifp = sc->bge_ifp;
3754 
3755 	if (ifp->if_drv_flags & IFF_DRV_RUNNING)
3756 		return;
3757 
3758 	/* Cancel pending I/O and flush buffers. */
3759 	bge_stop(sc);
3760 
3761 	bge_stop_fw(sc);
3762 	bge_sig_pre_reset(sc, BGE_RESET_START);
3763 	bge_reset(sc);
3764 	bge_sig_legacy(sc, BGE_RESET_START);
3765 	bge_sig_post_reset(sc, BGE_RESET_START);
3766 
3767 	bge_chipinit(sc);
3768 
3769 	/*
3770 	 * Init the various state machines, ring
3771 	 * control blocks and firmware.
3772 	 */
3773 	if (bge_blockinit(sc)) {
3774 		device_printf(sc->bge_dev, "initialization failure\n");
3775 		return;
3776 	}
3777 
3778 	ifp = sc->bge_ifp;
3779 
3780 	/* Specify MTU. */
3781 	CSR_WRITE_4(sc, BGE_RX_MTU, ifp->if_mtu +
3782 	    ETHER_HDR_LEN + ETHER_CRC_LEN +
3783 	    (ifp->if_capenable & IFCAP_VLAN_MTU ? ETHER_VLAN_ENCAP_LEN : 0));
3784 
3785 	/* Load our MAC address. */
3786 	m = (uint16_t *)IF_LLADDR(sc->bge_ifp);
3787 	CSR_WRITE_4(sc, BGE_MAC_ADDR1_LO, htons(m[0]));
3788 	CSR_WRITE_4(sc, BGE_MAC_ADDR1_HI, (htons(m[1]) << 16) | htons(m[2]));
3789 
3790 	/* Program promiscuous mode. */
3791 	bge_setpromisc(sc);
3792 
3793 	/* Program multicast filter. */
3794 	bge_setmulti(sc);
3795 
3796 	/* Program VLAN tag stripping. */
3797 	bge_setvlan(sc);
3798 
3799 	/* Init RX ring. */
3800 	bge_init_rx_ring_std(sc);
3801 
3802 	/*
3803 	 * Workaround for a bug in 5705 ASIC rev A0. Poll the NIC's
3804 	 * memory to insure that the chip has in fact read the first
3805 	 * entry of the ring.
3806 	 */
3807 	if (sc->bge_chipid == BGE_CHIPID_BCM5705_A0) {
3808 		uint32_t		v, i;
3809 		for (i = 0; i < 10; i++) {
3810 			DELAY(20);
3811 			v = bge_readmem_ind(sc, BGE_STD_RX_RINGS + 8);
3812 			if (v == (MCLBYTES - ETHER_ALIGN))
3813 				break;
3814 		}
3815 		if (i == 10)
3816 			device_printf (sc->bge_dev,
3817 			    "5705 A0 chip failed to load RX ring\n");
3818 	}
3819 
3820 	/* Init jumbo RX ring. */
3821 	if (ifp->if_mtu > (ETHERMTU + ETHER_HDR_LEN + ETHER_CRC_LEN))
3822 		bge_init_rx_ring_jumbo(sc);
3823 
3824 	/* Init our RX return ring index. */
3825 	sc->bge_rx_saved_considx = 0;
3826 
3827 	/* Init our RX/TX stat counters. */
3828 	sc->bge_rx_discards = sc->bge_tx_discards = sc->bge_tx_collisions = 0;
3829 
3830 	/* Init TX ring. */
3831 	bge_init_tx_ring(sc);
3832 
3833 	/* Turn on transmitter. */
3834 	BGE_SETBIT(sc, BGE_TX_MODE, BGE_TXMODE_ENABLE);
3835 
3836 	/* Turn on receiver. */
3837 	BGE_SETBIT(sc, BGE_RX_MODE, BGE_RXMODE_ENABLE);
3838 
3839 	/* Tell firmware we're alive. */
3840 	BGE_SETBIT(sc, BGE_MODE_CTL, BGE_MODECTL_STACKUP);
3841 
3842 #ifdef DEVICE_POLLING
3843 	/* Disable interrupts if we are polling. */
3844 	if (ifp->if_capenable & IFCAP_POLLING) {
3845 		BGE_SETBIT(sc, BGE_PCI_MISC_CTL,
3846 		    BGE_PCIMISCCTL_MASK_PCI_INTR);
3847 		bge_writembx(sc, BGE_MBX_IRQ0_LO, 1);
3848 	} else
3849 #endif
3850 
3851 	/* Enable host interrupts. */
3852 	{
3853 	BGE_SETBIT(sc, BGE_PCI_MISC_CTL, BGE_PCIMISCCTL_CLEAR_INTA);
3854 	BGE_CLRBIT(sc, BGE_PCI_MISC_CTL, BGE_PCIMISCCTL_MASK_PCI_INTR);
3855 	bge_writembx(sc, BGE_MBX_IRQ0_LO, 0);
3856 	}
3857 
3858 	bge_ifmedia_upd_locked(ifp);
3859 
3860 	ifp->if_drv_flags |= IFF_DRV_RUNNING;
3861 	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
3862 
3863 	callout_reset(&sc->bge_stat_ch, hz, bge_tick, sc);
3864 }
3865 
3866 static void
3867 bge_init(void *xsc)
3868 {
3869 	struct bge_softc *sc = xsc;
3870 
3871 	BGE_LOCK(sc);
3872 	bge_init_locked(sc);
3873 	BGE_UNLOCK(sc);
3874 }
3875 
3876 /*
3877  * Set media options.
3878  */
3879 static int
3880 bge_ifmedia_upd(struct ifnet *ifp)
3881 {
3882 	struct bge_softc *sc = ifp->if_softc;
3883 	int res;
3884 
3885 	BGE_LOCK(sc);
3886 	res = bge_ifmedia_upd_locked(ifp);
3887 	BGE_UNLOCK(sc);
3888 
3889 	return (res);
3890 }
3891 
3892 static int
3893 bge_ifmedia_upd_locked(struct ifnet *ifp)
3894 {
3895 	struct bge_softc *sc = ifp->if_softc;
3896 	struct mii_data *mii;
3897 	struct ifmedia *ifm;
3898 
3899 	BGE_LOCK_ASSERT(sc);
3900 
3901 	ifm = &sc->bge_ifmedia;
3902 
3903 	/* If this is a 1000baseX NIC, enable the TBI port. */
3904 	if (sc->bge_flags & BGE_FLAG_TBI) {
3905 		if (IFM_TYPE(ifm->ifm_media) != IFM_ETHER)
3906 			return (EINVAL);
3907 		switch(IFM_SUBTYPE(ifm->ifm_media)) {
3908 		case IFM_AUTO:
3909 			/*
3910 			 * The BCM5704 ASIC appears to have a special
3911 			 * mechanism for programming the autoneg
3912 			 * advertisement registers in TBI mode.
3913 			 */
3914 			if (sc->bge_asicrev == BGE_ASICREV_BCM5704) {
3915 				uint32_t sgdig;
3916 				sgdig = CSR_READ_4(sc, BGE_SGDIG_STS);
3917 				if (sgdig & BGE_SGDIGSTS_DONE) {
3918 					CSR_WRITE_4(sc, BGE_TX_TBI_AUTONEG, 0);
3919 					sgdig = CSR_READ_4(sc, BGE_SGDIG_CFG);
3920 					sgdig |= BGE_SGDIGCFG_AUTO |
3921 					    BGE_SGDIGCFG_PAUSE_CAP |
3922 					    BGE_SGDIGCFG_ASYM_PAUSE;
3923 					CSR_WRITE_4(sc, BGE_SGDIG_CFG,
3924 					    sgdig | BGE_SGDIGCFG_SEND);
3925 					DELAY(5);
3926 					CSR_WRITE_4(sc, BGE_SGDIG_CFG, sgdig);
3927 				}
3928 			}
3929 			break;
3930 		case IFM_1000_SX:
3931 			if ((ifm->ifm_media & IFM_GMASK) == IFM_FDX) {
3932 				BGE_CLRBIT(sc, BGE_MAC_MODE,
3933 				    BGE_MACMODE_HALF_DUPLEX);
3934 			} else {
3935 				BGE_SETBIT(sc, BGE_MAC_MODE,
3936 				    BGE_MACMODE_HALF_DUPLEX);
3937 			}
3938 			break;
3939 		default:
3940 			return (EINVAL);
3941 		}
3942 		return (0);
3943 	}
3944 
3945 	sc->bge_link_evt++;
3946 	mii = device_get_softc(sc->bge_miibus);
3947 	if (mii->mii_instance) {
3948 		struct mii_softc *miisc;
3949 		for (miisc = LIST_FIRST(&mii->mii_phys); miisc != NULL;
3950 		    miisc = LIST_NEXT(miisc, mii_list))
3951 			mii_phy_reset(miisc);
3952 	}
3953 	mii_mediachg(mii);
3954 
3955 	/*
3956 	 * Force an interrupt so that we will call bge_link_upd
3957 	 * if needed and clear any pending link state attention.
3958 	 * Without this we are not getting any further interrupts
3959 	 * for link state changes and thus will not UP the link and
3960 	 * not be able to send in bge_start_locked. The only
3961 	 * way to get things working was to receive a packet and
3962 	 * get an RX intr.
3963 	 * bge_tick should help for fiber cards and we might not
3964 	 * need to do this here if BGE_FLAG_TBI is set but as
3965 	 * we poll for fiber anyway it should not harm.
3966 	 */
3967 	if (sc->bge_asicrev == BGE_ASICREV_BCM5700 ||
3968 	    sc->bge_flags & BGE_FLAG_5788)
3969 		BGE_SETBIT(sc, BGE_MISC_LOCAL_CTL, BGE_MLC_INTR_SET);
3970 	else
3971 		BGE_SETBIT(sc, BGE_HCC_MODE, BGE_HCCMODE_COAL_NOW);
3972 
3973 	return (0);
3974 }
3975 
3976 /*
3977  * Report current media status.
3978  */
3979 static void
3980 bge_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
3981 {
3982 	struct bge_softc *sc = ifp->if_softc;
3983 	struct mii_data *mii;
3984 
3985 	BGE_LOCK(sc);
3986 
3987 	if (sc->bge_flags & BGE_FLAG_TBI) {
3988 		ifmr->ifm_status = IFM_AVALID;
3989 		ifmr->ifm_active = IFM_ETHER;
3990 		if (CSR_READ_4(sc, BGE_MAC_STS) &
3991 		    BGE_MACSTAT_TBI_PCS_SYNCHED)
3992 			ifmr->ifm_status |= IFM_ACTIVE;
3993 		else {
3994 			ifmr->ifm_active |= IFM_NONE;
3995 			BGE_UNLOCK(sc);
3996 			return;
3997 		}
3998 		ifmr->ifm_active |= IFM_1000_SX;
3999 		if (CSR_READ_4(sc, BGE_MAC_MODE) & BGE_MACMODE_HALF_DUPLEX)
4000 			ifmr->ifm_active |= IFM_HDX;
4001 		else
4002 			ifmr->ifm_active |= IFM_FDX;
4003 		BGE_UNLOCK(sc);
4004 		return;
4005 	}
4006 
4007 	mii = device_get_softc(sc->bge_miibus);
4008 	mii_pollstat(mii);
4009 	ifmr->ifm_active = mii->mii_media_active;
4010 	ifmr->ifm_status = mii->mii_media_status;
4011 
4012 	BGE_UNLOCK(sc);
4013 }
4014 
4015 static int
4016 bge_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
4017 {
4018 	struct bge_softc *sc = ifp->if_softc;
4019 	struct ifreq *ifr = (struct ifreq *) data;
4020 	struct mii_data *mii;
4021 	int flags, mask, error = 0;
4022 
4023 	switch (command) {
4024 	case SIOCSIFMTU:
4025 		if (ifr->ifr_mtu < ETHERMIN ||
4026 		    ((BGE_IS_JUMBO_CAPABLE(sc)) &&
4027 		    ifr->ifr_mtu > BGE_JUMBO_MTU) ||
4028 		    ((!BGE_IS_JUMBO_CAPABLE(sc)) &&
4029 		    ifr->ifr_mtu > ETHERMTU))
4030 			error = EINVAL;
4031 		else if (ifp->if_mtu != ifr->ifr_mtu) {
4032 			ifp->if_mtu = ifr->ifr_mtu;
4033 			ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
4034 			bge_init(sc);
4035 		}
4036 		break;
4037 	case SIOCSIFFLAGS:
4038 		BGE_LOCK(sc);
4039 		if (ifp->if_flags & IFF_UP) {
4040 			/*
4041 			 * If only the state of the PROMISC flag changed,
4042 			 * then just use the 'set promisc mode' command
4043 			 * instead of reinitializing the entire NIC. Doing
4044 			 * a full re-init means reloading the firmware and
4045 			 * waiting for it to start up, which may take a
4046 			 * second or two.  Similarly for ALLMULTI.
4047 			 */
4048 			if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
4049 				flags = ifp->if_flags ^ sc->bge_if_flags;
4050 				if (flags & IFF_PROMISC)
4051 					bge_setpromisc(sc);
4052 				if (flags & IFF_ALLMULTI)
4053 					bge_setmulti(sc);
4054 			} else
4055 				bge_init_locked(sc);
4056 		} else {
4057 			if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
4058 				bge_stop(sc);
4059 			}
4060 		}
4061 		sc->bge_if_flags = ifp->if_flags;
4062 		BGE_UNLOCK(sc);
4063 		error = 0;
4064 		break;
4065 	case SIOCADDMULTI:
4066 	case SIOCDELMULTI:
4067 		if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
4068 			BGE_LOCK(sc);
4069 			bge_setmulti(sc);
4070 			BGE_UNLOCK(sc);
4071 			error = 0;
4072 		}
4073 		break;
4074 	case SIOCSIFMEDIA:
4075 	case SIOCGIFMEDIA:
4076 		if (sc->bge_flags & BGE_FLAG_TBI) {
4077 			error = ifmedia_ioctl(ifp, ifr,
4078 			    &sc->bge_ifmedia, command);
4079 		} else {
4080 			mii = device_get_softc(sc->bge_miibus);
4081 			error = ifmedia_ioctl(ifp, ifr,
4082 			    &mii->mii_media, command);
4083 		}
4084 		break;
4085 	case SIOCSIFCAP:
4086 		mask = ifr->ifr_reqcap ^ ifp->if_capenable;
4087 #ifdef DEVICE_POLLING
4088 		if (mask & IFCAP_POLLING) {
4089 			if (ifr->ifr_reqcap & IFCAP_POLLING) {
4090 				error = ether_poll_register(bge_poll, ifp);
4091 				if (error)
4092 					return (error);
4093 				BGE_LOCK(sc);
4094 				BGE_SETBIT(sc, BGE_PCI_MISC_CTL,
4095 				    BGE_PCIMISCCTL_MASK_PCI_INTR);
4096 				bge_writembx(sc, BGE_MBX_IRQ0_LO, 1);
4097 				ifp->if_capenable |= IFCAP_POLLING;
4098 				BGE_UNLOCK(sc);
4099 			} else {
4100 				error = ether_poll_deregister(ifp);
4101 				/* Enable interrupt even in error case */
4102 				BGE_LOCK(sc);
4103 				BGE_CLRBIT(sc, BGE_PCI_MISC_CTL,
4104 				    BGE_PCIMISCCTL_MASK_PCI_INTR);
4105 				bge_writembx(sc, BGE_MBX_IRQ0_LO, 0);
4106 				ifp->if_capenable &= ~IFCAP_POLLING;
4107 				BGE_UNLOCK(sc);
4108 			}
4109 		}
4110 #endif
4111 		if (mask & IFCAP_HWCSUM) {
4112 			ifp->if_capenable ^= IFCAP_HWCSUM;
4113 			if (IFCAP_HWCSUM & ifp->if_capenable &&
4114 			    IFCAP_HWCSUM & ifp->if_capabilities)
4115 				ifp->if_hwassist = BGE_CSUM_FEATURES;
4116 			else
4117 				ifp->if_hwassist = 0;
4118 #ifdef VLAN_CAPABILITIES
4119 			VLAN_CAPABILITIES(ifp);
4120 #endif
4121 		}
4122 
4123 		if (mask & IFCAP_VLAN_MTU) {
4124 			ifp->if_capenable ^= IFCAP_VLAN_MTU;
4125 			ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
4126 			bge_init(sc);
4127 		}
4128 
4129 		if (mask & IFCAP_VLAN_HWTAGGING) {
4130 			ifp->if_capenable ^= IFCAP_VLAN_HWTAGGING;
4131 			BGE_LOCK(sc);
4132 			bge_setvlan(sc);
4133 			BGE_UNLOCK(sc);
4134 #ifdef VLAN_CAPABILITIES
4135 			VLAN_CAPABILITIES(ifp);
4136 #endif
4137 		}
4138 
4139 		break;
4140 	default:
4141 		error = ether_ioctl(ifp, command, data);
4142 		break;
4143 	}
4144 
4145 	return (error);
4146 }
4147 
4148 static void
4149 bge_watchdog(struct bge_softc *sc)
4150 {
4151 	struct ifnet *ifp;
4152 
4153 	BGE_LOCK_ASSERT(sc);
4154 
4155 	if (sc->bge_timer == 0 || --sc->bge_timer)
4156 		return;
4157 
4158 	ifp = sc->bge_ifp;
4159 
4160 	if_printf(ifp, "watchdog timeout -- resetting\n");
4161 
4162 	ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
4163 	bge_init_locked(sc);
4164 
4165 	ifp->if_oerrors++;
4166 }
4167 
4168 /*
4169  * Stop the adapter and free any mbufs allocated to the
4170  * RX and TX lists.
4171  */
4172 static void
4173 bge_stop(struct bge_softc *sc)
4174 {
4175 	struct ifnet *ifp;
4176 	struct ifmedia_entry *ifm;
4177 	struct mii_data *mii = NULL;
4178 	int mtmp, itmp;
4179 
4180 	BGE_LOCK_ASSERT(sc);
4181 
4182 	ifp = sc->bge_ifp;
4183 
4184 	if ((sc->bge_flags & BGE_FLAG_TBI) == 0)
4185 		mii = device_get_softc(sc->bge_miibus);
4186 
4187 	callout_stop(&sc->bge_stat_ch);
4188 
4189 	/*
4190 	 * Disable all of the receiver blocks.
4191 	 */
4192 	BGE_CLRBIT(sc, BGE_RX_MODE, BGE_RXMODE_ENABLE);
4193 	BGE_CLRBIT(sc, BGE_RBDI_MODE, BGE_RBDIMODE_ENABLE);
4194 	BGE_CLRBIT(sc, BGE_RXLP_MODE, BGE_RXLPMODE_ENABLE);
4195 	if (!(BGE_IS_5705_PLUS(sc)))
4196 		BGE_CLRBIT(sc, BGE_RXLS_MODE, BGE_RXLSMODE_ENABLE);
4197 	BGE_CLRBIT(sc, BGE_RDBDI_MODE, BGE_RBDIMODE_ENABLE);
4198 	BGE_CLRBIT(sc, BGE_RDC_MODE, BGE_RDCMODE_ENABLE);
4199 	BGE_CLRBIT(sc, BGE_RBDC_MODE, BGE_RBDCMODE_ENABLE);
4200 
4201 	/*
4202 	 * Disable all of the transmit blocks.
4203 	 */
4204 	BGE_CLRBIT(sc, BGE_SRS_MODE, BGE_SRSMODE_ENABLE);
4205 	BGE_CLRBIT(sc, BGE_SBDI_MODE, BGE_SBDIMODE_ENABLE);
4206 	BGE_CLRBIT(sc, BGE_SDI_MODE, BGE_SDIMODE_ENABLE);
4207 	BGE_CLRBIT(sc, BGE_RDMA_MODE, BGE_RDMAMODE_ENABLE);
4208 	BGE_CLRBIT(sc, BGE_SDC_MODE, BGE_SDCMODE_ENABLE);
4209 	if (!(BGE_IS_5705_PLUS(sc)))
4210 		BGE_CLRBIT(sc, BGE_DMAC_MODE, BGE_DMACMODE_ENABLE);
4211 	BGE_CLRBIT(sc, BGE_SBDC_MODE, BGE_SBDCMODE_ENABLE);
4212 
4213 	/*
4214 	 * Shut down all of the memory managers and related
4215 	 * state machines.
4216 	 */
4217 	BGE_CLRBIT(sc, BGE_HCC_MODE, BGE_HCCMODE_ENABLE);
4218 	BGE_CLRBIT(sc, BGE_WDMA_MODE, BGE_WDMAMODE_ENABLE);
4219 	if (!(BGE_IS_5705_PLUS(sc)))
4220 		BGE_CLRBIT(sc, BGE_MBCF_MODE, BGE_MBCFMODE_ENABLE);
4221 	CSR_WRITE_4(sc, BGE_FTQ_RESET, 0xFFFFFFFF);
4222 	CSR_WRITE_4(sc, BGE_FTQ_RESET, 0);
4223 	if (!(BGE_IS_5705_PLUS(sc))) {
4224 		BGE_CLRBIT(sc, BGE_BMAN_MODE, BGE_BMANMODE_ENABLE);
4225 		BGE_CLRBIT(sc, BGE_MARB_MODE, BGE_MARBMODE_ENABLE);
4226 	}
4227 
4228 	/* Disable host interrupts. */
4229 	BGE_SETBIT(sc, BGE_PCI_MISC_CTL, BGE_PCIMISCCTL_MASK_PCI_INTR);
4230 	bge_writembx(sc, BGE_MBX_IRQ0_LO, 1);
4231 
4232 	/*
4233 	 * Tell firmware we're shutting down.
4234 	 */
4235 
4236 	bge_stop_fw(sc);
4237 	bge_sig_pre_reset(sc, BGE_RESET_STOP);
4238 	bge_reset(sc);
4239 	bge_sig_legacy(sc, BGE_RESET_STOP);
4240 	bge_sig_post_reset(sc, BGE_RESET_STOP);
4241 
4242 	/*
4243 	 * Keep the ASF firmware running if up.
4244 	 */
4245 	if (sc->bge_asf_mode & ASF_STACKUP)
4246 		BGE_SETBIT(sc, BGE_MODE_CTL, BGE_MODECTL_STACKUP);
4247 	else
4248 		BGE_CLRBIT(sc, BGE_MODE_CTL, BGE_MODECTL_STACKUP);
4249 
4250 	/* Free the RX lists. */
4251 	bge_free_rx_ring_std(sc);
4252 
4253 	/* Free jumbo RX list. */
4254 	if (BGE_IS_JUMBO_CAPABLE(sc))
4255 		bge_free_rx_ring_jumbo(sc);
4256 
4257 	/* Free TX buffers. */
4258 	bge_free_tx_ring(sc);
4259 
4260 	/*
4261 	 * Isolate/power down the PHY, but leave the media selection
4262 	 * unchanged so that things will be put back to normal when
4263 	 * we bring the interface back up.
4264 	 */
4265 	if ((sc->bge_flags & BGE_FLAG_TBI) == 0) {
4266 		itmp = ifp->if_flags;
4267 		ifp->if_flags |= IFF_UP;
4268 		/*
4269 		 * If we are called from bge_detach(), mii is already NULL.
4270 		 */
4271 		if (mii != NULL) {
4272 			ifm = mii->mii_media.ifm_cur;
4273 			mtmp = ifm->ifm_media;
4274 			ifm->ifm_media = IFM_ETHER | IFM_NONE;
4275 			mii_mediachg(mii);
4276 			ifm->ifm_media = mtmp;
4277 		}
4278 		ifp->if_flags = itmp;
4279 	}
4280 
4281 	sc->bge_tx_saved_considx = BGE_TXCONS_UNSET;
4282 
4283 	/* Clear MAC's link state (PHY may still have link UP). */
4284 	if (bootverbose && sc->bge_link)
4285 		if_printf(sc->bge_ifp, "link DOWN\n");
4286 	sc->bge_link = 0;
4287 
4288 	ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
4289 }
4290 
4291 /*
4292  * Stop all chip I/O so that the kernel's probe routines don't
4293  * get confused by errant DMAs when rebooting.
4294  */
4295 static void
4296 bge_shutdown(device_t dev)
4297 {
4298 	struct bge_softc *sc;
4299 
4300 	sc = device_get_softc(dev);
4301 
4302 	BGE_LOCK(sc);
4303 	bge_stop(sc);
4304 	bge_reset(sc);
4305 	BGE_UNLOCK(sc);
4306 }
4307 
4308 static int
4309 bge_suspend(device_t dev)
4310 {
4311 	struct bge_softc *sc;
4312 
4313 	sc = device_get_softc(dev);
4314 	BGE_LOCK(sc);
4315 	bge_stop(sc);
4316 	BGE_UNLOCK(sc);
4317 
4318 	return (0);
4319 }
4320 
4321 static int
4322 bge_resume(device_t dev)
4323 {
4324 	struct bge_softc *sc;
4325 	struct ifnet *ifp;
4326 
4327 	sc = device_get_softc(dev);
4328 	BGE_LOCK(sc);
4329 	ifp = sc->bge_ifp;
4330 	if (ifp->if_flags & IFF_UP) {
4331 		bge_init_locked(sc);
4332 		if (ifp->if_drv_flags & IFF_DRV_RUNNING)
4333 			bge_start_locked(ifp);
4334 	}
4335 	BGE_UNLOCK(sc);
4336 
4337 	return (0);
4338 }
4339 
4340 static void
4341 bge_link_upd(struct bge_softc *sc)
4342 {
4343 	struct mii_data *mii;
4344 	uint32_t link, status;
4345 
4346 	BGE_LOCK_ASSERT(sc);
4347 
4348 	/* Clear 'pending link event' flag. */
4349 	sc->bge_link_evt = 0;
4350 
4351 	/*
4352 	 * Process link state changes.
4353 	 * Grrr. The link status word in the status block does
4354 	 * not work correctly on the BCM5700 rev AX and BX chips,
4355 	 * according to all available information. Hence, we have
4356 	 * to enable MII interrupts in order to properly obtain
4357 	 * async link changes. Unfortunately, this also means that
4358 	 * we have to read the MAC status register to detect link
4359 	 * changes, thereby adding an additional register access to
4360 	 * the interrupt handler.
4361 	 *
4362 	 * XXX: perhaps link state detection procedure used for
4363 	 * BGE_CHIPID_BCM5700_B2 can be used for others BCM5700 revisions.
4364 	 */
4365 
4366 	if (sc->bge_asicrev == BGE_ASICREV_BCM5700 &&
4367 	    sc->bge_chipid != BGE_CHIPID_BCM5700_B2) {
4368 		status = CSR_READ_4(sc, BGE_MAC_STS);
4369 		if (status & BGE_MACSTAT_MI_INTERRUPT) {
4370 			mii = device_get_softc(sc->bge_miibus);
4371 			mii_pollstat(mii);
4372 			if (!sc->bge_link &&
4373 			    mii->mii_media_status & IFM_ACTIVE &&
4374 			    IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE) {
4375 				sc->bge_link++;
4376 				if (bootverbose)
4377 					if_printf(sc->bge_ifp, "link UP\n");
4378 			} else if (sc->bge_link &&
4379 			    (!(mii->mii_media_status & IFM_ACTIVE) ||
4380 			    IFM_SUBTYPE(mii->mii_media_active) == IFM_NONE)) {
4381 				sc->bge_link = 0;
4382 				if (bootverbose)
4383 					if_printf(sc->bge_ifp, "link DOWN\n");
4384 			}
4385 
4386 			/* Clear the interrupt. */
4387 			CSR_WRITE_4(sc, BGE_MAC_EVT_ENB,
4388 			    BGE_EVTENB_MI_INTERRUPT);
4389 			bge_miibus_readreg(sc->bge_dev, 1, BRGPHY_MII_ISR);
4390 			bge_miibus_writereg(sc->bge_dev, 1, BRGPHY_MII_IMR,
4391 			    BRGPHY_INTRS);
4392 		}
4393 		return;
4394 	}
4395 
4396 	if (sc->bge_flags & BGE_FLAG_TBI) {
4397 		status = CSR_READ_4(sc, BGE_MAC_STS);
4398 		if (status & BGE_MACSTAT_TBI_PCS_SYNCHED) {
4399 			if (!sc->bge_link) {
4400 				sc->bge_link++;
4401 				if (sc->bge_asicrev == BGE_ASICREV_BCM5704)
4402 					BGE_CLRBIT(sc, BGE_MAC_MODE,
4403 					    BGE_MACMODE_TBI_SEND_CFGS);
4404 				CSR_WRITE_4(sc, BGE_MAC_STS, 0xFFFFFFFF);
4405 				if (bootverbose)
4406 					if_printf(sc->bge_ifp, "link UP\n");
4407 				if_link_state_change(sc->bge_ifp,
4408 				    LINK_STATE_UP);
4409 			}
4410 		} else if (sc->bge_link) {
4411 			sc->bge_link = 0;
4412 			if (bootverbose)
4413 				if_printf(sc->bge_ifp, "link DOWN\n");
4414 			if_link_state_change(sc->bge_ifp, LINK_STATE_DOWN);
4415 		}
4416 	} else if (CSR_READ_4(sc, BGE_MI_MODE) & BGE_MIMODE_AUTOPOLL) {
4417 		/*
4418 		 * Some broken BCM chips have BGE_STATFLAG_LINKSTATE_CHANGED bit
4419 		 * in status word always set. Workaround this bug by reading
4420 		 * PHY link status directly.
4421 		 */
4422 		link = (CSR_READ_4(sc, BGE_MI_STS) & BGE_MISTS_LINK) ? 1 : 0;
4423 
4424 		if (link != sc->bge_link ||
4425 		    sc->bge_asicrev == BGE_ASICREV_BCM5700) {
4426 			mii = device_get_softc(sc->bge_miibus);
4427 			mii_pollstat(mii);
4428 			if (!sc->bge_link &&
4429 			    mii->mii_media_status & IFM_ACTIVE &&
4430 			    IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE) {
4431 				sc->bge_link++;
4432 				if (bootverbose)
4433 					if_printf(sc->bge_ifp, "link UP\n");
4434 			} else if (sc->bge_link &&
4435 			    (!(mii->mii_media_status & IFM_ACTIVE) ||
4436 			    IFM_SUBTYPE(mii->mii_media_active) == IFM_NONE)) {
4437 				sc->bge_link = 0;
4438 				if (bootverbose)
4439 					if_printf(sc->bge_ifp, "link DOWN\n");
4440 			}
4441 		}
4442 	} else {
4443 		/*
4444 		 * Discard link events for MII/GMII controllers
4445 		 * if MI auto-polling is disabled.
4446 		 */
4447 	}
4448 
4449 	/* Clear the attention. */
4450 	CSR_WRITE_4(sc, BGE_MAC_STS, BGE_MACSTAT_SYNC_CHANGED |
4451 	    BGE_MACSTAT_CFG_CHANGED | BGE_MACSTAT_MI_COMPLETE |
4452 	    BGE_MACSTAT_LINK_CHANGED);
4453 }
4454 
4455 #define BGE_SYSCTL_STAT(sc, ctx, desc, parent, node, oid) \
4456 	SYSCTL_ADD_PROC(ctx, parent, OID_AUTO, oid, CTLTYPE_UINT|CTLFLAG_RD, \
4457 	    sc, offsetof(struct bge_stats, node), bge_sysctl_stats, "IU", \
4458 	    desc)
4459 
4460 static void
4461 bge_add_sysctls(struct bge_softc *sc)
4462 {
4463 	struct sysctl_ctx_list *ctx;
4464 	struct sysctl_oid_list *children, *schildren;
4465 	struct sysctl_oid *tree;
4466 
4467 	ctx = device_get_sysctl_ctx(sc->bge_dev);
4468 	children = SYSCTL_CHILDREN(device_get_sysctl_tree(sc->bge_dev));
4469 
4470 #ifdef BGE_REGISTER_DEBUG
4471 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "debug_info",
4472 	    CTLTYPE_INT | CTLFLAG_RW, sc, 0, bge_sysctl_debug_info, "I",
4473 	    "Debug Information");
4474 
4475 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "reg_read",
4476 	    CTLTYPE_INT | CTLFLAG_RW, sc, 0, bge_sysctl_reg_read, "I",
4477 	    "Register Read");
4478 
4479 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "mem_read",
4480 	    CTLTYPE_INT | CTLFLAG_RW, sc, 0, bge_sysctl_mem_read, "I",
4481 	    "Memory Read");
4482 
4483 #endif
4484 
4485 	if (BGE_IS_5705_PLUS(sc))
4486 		return;
4487 
4488 	tree = SYSCTL_ADD_NODE(ctx, children, OID_AUTO, "stats", CTLFLAG_RD,
4489 	    NULL, "BGE Statistics");
4490 	schildren = children = SYSCTL_CHILDREN(tree);
4491 	BGE_SYSCTL_STAT(sc, ctx, "Frames Dropped Due To Filters",
4492 	    children, COSFramesDroppedDueToFilters,
4493 	    "FramesDroppedDueToFilters");
4494 	BGE_SYSCTL_STAT(sc, ctx, "NIC DMA Write Queue Full",
4495 	    children, nicDmaWriteQueueFull, "DmaWriteQueueFull");
4496 	BGE_SYSCTL_STAT(sc, ctx, "NIC DMA Write High Priority Queue Full",
4497 	    children, nicDmaWriteHighPriQueueFull, "DmaWriteHighPriQueueFull");
4498 	BGE_SYSCTL_STAT(sc, ctx, "NIC No More RX Buffer Descriptors",
4499 	    children, nicNoMoreRxBDs, "NoMoreRxBDs");
4500 	BGE_SYSCTL_STAT(sc, ctx, "Discarded Input Frames",
4501 	    children, ifInDiscards, "InputDiscards");
4502 	BGE_SYSCTL_STAT(sc, ctx, "Input Errors",
4503 	    children, ifInErrors, "InputErrors");
4504 	BGE_SYSCTL_STAT(sc, ctx, "NIC Recv Threshold Hit",
4505 	    children, nicRecvThresholdHit, "RecvThresholdHit");
4506 	BGE_SYSCTL_STAT(sc, ctx, "NIC DMA Read Queue Full",
4507 	    children, nicDmaReadQueueFull, "DmaReadQueueFull");
4508 	BGE_SYSCTL_STAT(sc, ctx, "NIC DMA Read High Priority Queue Full",
4509 	    children, nicDmaReadHighPriQueueFull, "DmaReadHighPriQueueFull");
4510 	BGE_SYSCTL_STAT(sc, ctx, "NIC Send Data Complete Queue Full",
4511 	    children, nicSendDataCompQueueFull, "SendDataCompQueueFull");
4512 	BGE_SYSCTL_STAT(sc, ctx, "NIC Ring Set Send Producer Index",
4513 	    children, nicRingSetSendProdIndex, "RingSetSendProdIndex");
4514 	BGE_SYSCTL_STAT(sc, ctx, "NIC Ring Status Update",
4515 	    children, nicRingStatusUpdate, "RingStatusUpdate");
4516 	BGE_SYSCTL_STAT(sc, ctx, "NIC Interrupts",
4517 	    children, nicInterrupts, "Interrupts");
4518 	BGE_SYSCTL_STAT(sc, ctx, "NIC Avoided Interrupts",
4519 	    children, nicAvoidedInterrupts, "AvoidedInterrupts");
4520 	BGE_SYSCTL_STAT(sc, ctx, "NIC Send Threshold Hit",
4521 	    children, nicSendThresholdHit, "SendThresholdHit");
4522 
4523 	tree = SYSCTL_ADD_NODE(ctx, schildren, OID_AUTO, "rx", CTLFLAG_RD,
4524 	    NULL, "BGE RX Statistics");
4525 	children = SYSCTL_CHILDREN(tree);
4526 	BGE_SYSCTL_STAT(sc, ctx, "Inbound Octets",
4527 	    children, rxstats.ifHCInOctets, "Octets");
4528 	BGE_SYSCTL_STAT(sc, ctx, "Fragments",
4529 	    children, rxstats.etherStatsFragments, "Fragments");
4530 	BGE_SYSCTL_STAT(sc, ctx, "Inbound Unicast Packets",
4531 	    children, rxstats.ifHCInUcastPkts, "UcastPkts");
4532 	BGE_SYSCTL_STAT(sc, ctx, "Inbound Multicast Packets",
4533 	    children, rxstats.ifHCInMulticastPkts, "MulticastPkts");
4534 	BGE_SYSCTL_STAT(sc, ctx, "FCS Errors",
4535 	    children, rxstats.dot3StatsFCSErrors, "FCSErrors");
4536 	BGE_SYSCTL_STAT(sc, ctx, "Alignment Errors",
4537 	    children, rxstats.dot3StatsAlignmentErrors, "AlignmentErrors");
4538 	BGE_SYSCTL_STAT(sc, ctx, "XON Pause Frames Received",
4539 	    children, rxstats.xonPauseFramesReceived, "xonPauseFramesReceived");
4540 	BGE_SYSCTL_STAT(sc, ctx, "XOFF Pause Frames Received",
4541 	    children, rxstats.xoffPauseFramesReceived,
4542 	    "xoffPauseFramesReceived");
4543 	BGE_SYSCTL_STAT(sc, ctx, "MAC Control Frames Received",
4544 	    children, rxstats.macControlFramesReceived,
4545 	    "ControlFramesReceived");
4546 	BGE_SYSCTL_STAT(sc, ctx, "XOFF State Entered",
4547 	    children, rxstats.xoffStateEntered, "xoffStateEntered");
4548 	BGE_SYSCTL_STAT(sc, ctx, "Frames Too Long",
4549 	    children, rxstats.dot3StatsFramesTooLong, "FramesTooLong");
4550 	BGE_SYSCTL_STAT(sc, ctx, "Jabbers",
4551 	    children, rxstats.etherStatsJabbers, "Jabbers");
4552 	BGE_SYSCTL_STAT(sc, ctx, "Undersized Packets",
4553 	    children, rxstats.etherStatsUndersizePkts, "UndersizePkts");
4554 	BGE_SYSCTL_STAT(sc, ctx, "Inbound Range Length Errors",
4555 	    children, rxstats.inRangeLengthError, "inRangeLengthError");
4556 	BGE_SYSCTL_STAT(sc, ctx, "Outbound Range Length Errors",
4557 	    children, rxstats.outRangeLengthError, "outRangeLengthError");
4558 
4559 	tree = SYSCTL_ADD_NODE(ctx, schildren, OID_AUTO, "tx", CTLFLAG_RD,
4560 	    NULL, "BGE TX Statistics");
4561 	children = SYSCTL_CHILDREN(tree);
4562 	BGE_SYSCTL_STAT(sc, ctx, "Outbound Octets",
4563 	    children, txstats.ifHCOutOctets, "Octets");
4564 	BGE_SYSCTL_STAT(sc, ctx, "TX Collisions",
4565 	    children, txstats.etherStatsCollisions, "Collisions");
4566 	BGE_SYSCTL_STAT(sc, ctx, "XON Sent",
4567 	    children, txstats.outXonSent, "XonSent");
4568 	BGE_SYSCTL_STAT(sc, ctx, "XOFF Sent",
4569 	    children, txstats.outXoffSent, "XoffSent");
4570 	BGE_SYSCTL_STAT(sc, ctx, "Flow Control Done",
4571 	    children, txstats.flowControlDone, "flowControlDone");
4572 	BGE_SYSCTL_STAT(sc, ctx, "Internal MAC TX errors",
4573 	    children, txstats.dot3StatsInternalMacTransmitErrors,
4574 	    "InternalMacTransmitErrors");
4575 	BGE_SYSCTL_STAT(sc, ctx, "Single Collision Frames",
4576 	    children, txstats.dot3StatsSingleCollisionFrames,
4577 	    "SingleCollisionFrames");
4578 	BGE_SYSCTL_STAT(sc, ctx, "Multiple Collision Frames",
4579 	    children, txstats.dot3StatsMultipleCollisionFrames,
4580 	    "MultipleCollisionFrames");
4581 	BGE_SYSCTL_STAT(sc, ctx, "Deferred Transmissions",
4582 	    children, txstats.dot3StatsDeferredTransmissions,
4583 	    "DeferredTransmissions");
4584 	BGE_SYSCTL_STAT(sc, ctx, "Excessive Collisions",
4585 	    children, txstats.dot3StatsExcessiveCollisions,
4586 	    "ExcessiveCollisions");
4587 	BGE_SYSCTL_STAT(sc, ctx, "Late Collisions",
4588 	    children, txstats.dot3StatsLateCollisions,
4589 	    "LateCollisions");
4590 	BGE_SYSCTL_STAT(sc, ctx, "Outbound Unicast Packets",
4591 	    children, txstats.ifHCOutUcastPkts, "UcastPkts");
4592 	BGE_SYSCTL_STAT(sc, ctx, "Outbound Multicast Packets",
4593 	    children, txstats.ifHCOutMulticastPkts, "MulticastPkts");
4594 	BGE_SYSCTL_STAT(sc, ctx, "Outbound Broadcast Packets",
4595 	    children, txstats.ifHCOutBroadcastPkts, "BroadcastPkts");
4596 	BGE_SYSCTL_STAT(sc, ctx, "Carrier Sense Errors",
4597 	    children, txstats.dot3StatsCarrierSenseErrors,
4598 	    "CarrierSenseErrors");
4599 	BGE_SYSCTL_STAT(sc, ctx, "Outbound Discards",
4600 	    children, txstats.ifOutDiscards, "Discards");
4601 	BGE_SYSCTL_STAT(sc, ctx, "Outbound Errors",
4602 	    children, txstats.ifOutErrors, "Errors");
4603 }
4604 
4605 static int
4606 bge_sysctl_stats(SYSCTL_HANDLER_ARGS)
4607 {
4608 	struct bge_softc *sc;
4609 	uint32_t result;
4610 	int offset;
4611 
4612 	sc = (struct bge_softc *)arg1;
4613 	offset = arg2;
4614 	result = CSR_READ_4(sc, BGE_MEMWIN_START + BGE_STATS_BLOCK + offset +
4615 	    offsetof(bge_hostaddr, bge_addr_lo));
4616 	return (sysctl_handle_int(oidp, &result, 0, req));
4617 }
4618 
4619 #ifdef BGE_REGISTER_DEBUG
4620 static int
4621 bge_sysctl_debug_info(SYSCTL_HANDLER_ARGS)
4622 {
4623 	struct bge_softc *sc;
4624 	uint16_t *sbdata;
4625 	int error;
4626 	int result;
4627 	int i, j;
4628 
4629 	result = -1;
4630 	error = sysctl_handle_int(oidp, &result, 0, req);
4631 	if (error || (req->newptr == NULL))
4632 		return (error);
4633 
4634 	if (result == 1) {
4635 		sc = (struct bge_softc *)arg1;
4636 
4637 		sbdata = (uint16_t *)sc->bge_ldata.bge_status_block;
4638 		printf("Status Block:\n");
4639 		for (i = 0x0; i < (BGE_STATUS_BLK_SZ / 4); ) {
4640 			printf("%06x:", i);
4641 			for (j = 0; j < 8; j++) {
4642 				printf(" %04x", sbdata[i]);
4643 				i += 4;
4644 			}
4645 			printf("\n");
4646 		}
4647 
4648 		printf("Registers:\n");
4649 		for (i = 0x800; i < 0xA00; ) {
4650 			printf("%06x:", i);
4651 			for (j = 0; j < 8; j++) {
4652 				printf(" %08x", CSR_READ_4(sc, i));
4653 				i += 4;
4654 			}
4655 			printf("\n");
4656 		}
4657 
4658 		printf("Hardware Flags:\n");
4659 		if (BGE_IS_575X_PLUS(sc))
4660 			printf(" - 575X Plus\n");
4661 		if (BGE_IS_5705_PLUS(sc))
4662 			printf(" - 5705 Plus\n");
4663 		if (BGE_IS_5714_FAMILY(sc))
4664 			printf(" - 5714 Family\n");
4665 		if (BGE_IS_5700_FAMILY(sc))
4666 			printf(" - 5700 Family\n");
4667 		if (sc->bge_flags & BGE_FLAG_JUMBO)
4668 			printf(" - Supports Jumbo Frames\n");
4669 		if (sc->bge_flags & BGE_FLAG_PCIX)
4670 			printf(" - PCI-X Bus\n");
4671 		if (sc->bge_flags & BGE_FLAG_PCIE)
4672 			printf(" - PCI Express Bus\n");
4673 		if (sc->bge_flags & BGE_FLAG_NO_3LED)
4674 			printf(" - No 3 LEDs\n");
4675 		if (sc->bge_flags & BGE_FLAG_RX_ALIGNBUG)
4676 			printf(" - RX Alignment Bug\n");
4677 	}
4678 
4679 	return (error);
4680 }
4681 
4682 static int
4683 bge_sysctl_reg_read(SYSCTL_HANDLER_ARGS)
4684 {
4685 	struct bge_softc *sc;
4686 	int error;
4687 	uint16_t result;
4688 	uint32_t val;
4689 
4690 	result = -1;
4691 	error = sysctl_handle_int(oidp, &result, 0, req);
4692 	if (error || (req->newptr == NULL))
4693 		return (error);
4694 
4695 	if (result < 0x8000) {
4696 		sc = (struct bge_softc *)arg1;
4697 		val = CSR_READ_4(sc, result);
4698 		printf("reg 0x%06X = 0x%08X\n", result, val);
4699 	}
4700 
4701 	return (error);
4702 }
4703 
4704 static int
4705 bge_sysctl_mem_read(SYSCTL_HANDLER_ARGS)
4706 {
4707 	struct bge_softc *sc;
4708 	int error;
4709 	uint16_t result;
4710 	uint32_t val;
4711 
4712 	result = -1;
4713 	error = sysctl_handle_int(oidp, &result, 0, req);
4714 	if (error || (req->newptr == NULL))
4715 		return (error);
4716 
4717 	if (result < 0x8000) {
4718 		sc = (struct bge_softc *)arg1;
4719 		val = bge_readmem_ind(sc, result);
4720 		printf("mem 0x%06X = 0x%08X\n", result, val);
4721 	}
4722 
4723 	return (error);
4724 }
4725 #endif
4726 
4727 static int
4728 bge_get_eaddr_mem(struct bge_softc *sc, uint8_t ether_addr[])
4729 {
4730 	uint32_t mac_addr;
4731 	int ret = 1;
4732 
4733 	mac_addr = bge_readmem_ind(sc, 0x0c14);
4734 	if ((mac_addr >> 16) == 0x484b) {
4735 		ether_addr[0] = (uint8_t)(mac_addr >> 8);
4736 		ether_addr[1] = (uint8_t)mac_addr;
4737 		mac_addr = bge_readmem_ind(sc, 0x0c18);
4738 		ether_addr[2] = (uint8_t)(mac_addr >> 24);
4739 		ether_addr[3] = (uint8_t)(mac_addr >> 16);
4740 		ether_addr[4] = (uint8_t)(mac_addr >> 8);
4741 		ether_addr[5] = (uint8_t)mac_addr;
4742 		ret = 0;
4743 	}
4744 	return ret;
4745 }
4746 
4747 static int
4748 bge_get_eaddr_nvram(struct bge_softc *sc, uint8_t ether_addr[])
4749 {
4750 	int mac_offset = BGE_EE_MAC_OFFSET;
4751 
4752 	if (sc->bge_asicrev == BGE_ASICREV_BCM5906)
4753 		mac_offset = BGE_EE_MAC_OFFSET_5906;
4754 
4755 	return bge_read_nvram(sc, ether_addr, mac_offset + 2, ETHER_ADDR_LEN);
4756 }
4757 
4758 static int
4759 bge_get_eaddr_eeprom(struct bge_softc *sc, uint8_t ether_addr[])
4760 {
4761 	if (!(sc->bge_flags & BGE_FLAG_EEPROM))
4762 		return 1;
4763 
4764 	return bge_read_eeprom(sc, ether_addr, BGE_EE_MAC_OFFSET + 2,
4765 			       ETHER_ADDR_LEN);
4766 }
4767 
4768 static int
4769 bge_get_eaddr(struct bge_softc *sc, uint8_t eaddr[])
4770 {
4771 	static const bge_eaddr_fcn_t bge_eaddr_funcs[] = {
4772 		/* NOTE: Order is critical */
4773 		bge_get_eaddr_mem,
4774 		bge_get_eaddr_nvram,
4775 		bge_get_eaddr_eeprom,
4776 		NULL
4777 	};
4778 	const bge_eaddr_fcn_t *func;
4779 
4780 	for (func = bge_eaddr_funcs; *func != NULL; ++func) {
4781 		if ((*func)(sc, eaddr) == 0)
4782 			break;
4783 	}
4784 	return (*func == NULL ? ENXIO : 0);
4785 }
4786