xref: /freebsd/sys/dev/bge/if_bge.c (revision 2357939bc239bd5334a169b62313806178dd8f30)
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 #include <sys/param.h>
70 #include <sys/endian.h>
71 #include <sys/systm.h>
72 #include <sys/sockio.h>
73 #include <sys/mbuf.h>
74 #include <sys/malloc.h>
75 #include <sys/kernel.h>
76 #include <sys/socket.h>
77 #include <sys/queue.h>
78 
79 #include <net/if.h>
80 #include <net/if_arp.h>
81 #include <net/ethernet.h>
82 #include <net/if_dl.h>
83 #include <net/if_media.h>
84 
85 #include <net/bpf.h>
86 
87 #include <net/if_types.h>
88 #include <net/if_vlan_var.h>
89 
90 #include <netinet/in_systm.h>
91 #include <netinet/in.h>
92 #include <netinet/ip.h>
93 
94 #include <machine/clock.h>      /* for DELAY */
95 #include <machine/bus_memio.h>
96 #include <machine/bus.h>
97 #include <machine/resource.h>
98 #include <sys/bus.h>
99 #include <sys/rman.h>
100 
101 #include <dev/mii/mii.h>
102 #include <dev/mii/miivar.h>
103 #include "miidevs.h"
104 #include <dev/mii/brgphyreg.h>
105 
106 #include <dev/pci/pcireg.h>
107 #include <dev/pci/pcivar.h>
108 
109 #include <dev/bge/if_bgereg.h>
110 
111 #define BGE_CSUM_FEATURES	(CSUM_IP | CSUM_TCP | CSUM_UDP)
112 
113 MODULE_DEPEND(bge, pci, 1, 1, 1);
114 MODULE_DEPEND(bge, ether, 1, 1, 1);
115 MODULE_DEPEND(bge, miibus, 1, 1, 1);
116 
117 /* "controller miibus0" required.  See GENERIC if you get errors here. */
118 #include "miibus_if.h"
119 
120 /*
121  * Various supported device vendors/types and their names. Note: the
122  * spec seems to indicate that the hardware still has Alteon's vendor
123  * ID burned into it, though it will always be overriden by the vendor
124  * ID in the EEPROM. Just to be safe, we cover all possibilities.
125  */
126 #define BGE_DEVDESC_MAX		64	/* Maximum device description length */
127 
128 static struct bge_type bge_devs[] = {
129 	{ ALT_VENDORID,	ALT_DEVICEID_BCM5700,
130 		"Broadcom BCM5700 Gigabit Ethernet" },
131 	{ ALT_VENDORID,	ALT_DEVICEID_BCM5701,
132 		"Broadcom BCM5701 Gigabit Ethernet" },
133 	{ BCOM_VENDORID, BCOM_DEVICEID_BCM5700,
134 		"Broadcom BCM5700 Gigabit Ethernet" },
135 	{ BCOM_VENDORID, BCOM_DEVICEID_BCM5701,
136 		"Broadcom BCM5701 Gigabit Ethernet" },
137 	{ BCOM_VENDORID, BCOM_DEVICEID_BCM5702,
138 		"Broadcom BCM5702 Gigabit Ethernet" },
139 	{ BCOM_VENDORID, BCOM_DEVICEID_BCM5702X,
140 		"Broadcom BCM5702X Gigabit Ethernet" },
141 	{ BCOM_VENDORID, BCOM_DEVICEID_BCM5703,
142 		"Broadcom BCM5703 Gigabit Ethernet" },
143 	{ BCOM_VENDORID, BCOM_DEVICEID_BCM5703X,
144 		"Broadcom BCM5703X Gigabit Ethernet" },
145 	{ BCOM_VENDORID, BCOM_DEVICEID_BCM5704C,
146 		"Broadcom BCM5704C Dual Gigabit Ethernet" },
147 	{ BCOM_VENDORID, BCOM_DEVICEID_BCM5704S,
148 		"Broadcom BCM5704S Dual Gigabit Ethernet" },
149 	{ BCOM_VENDORID, BCOM_DEVICEID_BCM5705,
150 		"Broadcom BCM5705 Gigabit Ethernet" },
151 	{ BCOM_VENDORID, BCOM_DEVICEID_BCM5705M,
152 		"Broadcom BCM5705M Gigabit Ethernet" },
153 	{ BCOM_VENDORID, BCOM_DEVICEID_BCM5705M_ALT,
154 		"Broadcom BCM5705M Gigabit Ethernet" },
155 	{ BCOM_VENDORID, BCOM_DEVICEID_BCM5782,
156 		"Broadcom BCM5782 Gigabit Ethernet" },
157 	{ BCOM_VENDORID, BCOM_DEVICEID_BCM5788,
158 		"Broadcom BCM5788 Gigabit Ethernet" },
159 	{ BCOM_VENDORID, BCOM_DEVICEID_BCM5901,
160 		"Broadcom BCM5901 Fast Ethernet" },
161 	{ BCOM_VENDORID, BCOM_DEVICEID_BCM5901A2,
162 		"Broadcom BCM5901A2 Fast Ethernet" },
163 	{ SK_VENDORID, SK_DEVICEID_ALTIMA,
164 		"SysKonnect Gigabit Ethernet" },
165 	{ ALTIMA_VENDORID, ALTIMA_DEVICE_AC1000,
166 		"Altima AC1000 Gigabit Ethernet" },
167 	{ ALTIMA_VENDORID, ALTIMA_DEVICE_AC1002,
168 		"Altima AC1002 Gigabit Ethernet" },
169 	{ ALTIMA_VENDORID, ALTIMA_DEVICE_AC9100,
170 		"Altima AC9100 Gigabit Ethernet" },
171 	{ 0, 0, NULL }
172 };
173 
174 static int bge_probe		(device_t);
175 static int bge_attach		(device_t);
176 static int bge_detach		(device_t);
177 static void bge_release_resources
178 				(struct bge_softc *);
179 static void bge_dma_map_addr	(void *, bus_dma_segment_t *, int, int);
180 static void bge_dma_map_tx_desc	(void *, bus_dma_segment_t *, int,
181 				    bus_size_t, int);
182 static int bge_dma_alloc	(device_t);
183 static void bge_dma_free	(struct bge_softc *);
184 
185 static void bge_txeof		(struct bge_softc *);
186 static void bge_rxeof		(struct bge_softc *);
187 
188 static void bge_tick_locked	(struct bge_softc *);
189 static void bge_tick		(void *);
190 static void bge_stats_update	(struct bge_softc *);
191 static void bge_stats_update_regs
192 				(struct bge_softc *);
193 static int bge_encap		(struct bge_softc *, struct mbuf *,
194 					u_int32_t *);
195 
196 static void bge_intr		(void *);
197 static void bge_start_locked	(struct ifnet *);
198 static void bge_start		(struct ifnet *);
199 static int bge_ioctl		(struct ifnet *, u_long, caddr_t);
200 static void bge_init_locked	(struct bge_softc *);
201 static void bge_init		(void *);
202 static void bge_stop		(struct bge_softc *);
203 static void bge_watchdog		(struct ifnet *);
204 static void bge_shutdown		(device_t);
205 static int bge_ifmedia_upd	(struct ifnet *);
206 static void bge_ifmedia_sts	(struct ifnet *, struct ifmediareq *);
207 
208 static u_int8_t	bge_eeprom_getbyte	(struct bge_softc *, int, u_int8_t *);
209 static int bge_read_eeprom	(struct bge_softc *, caddr_t, int, int);
210 
211 static uint32_t bge_mchash	(const uint8_t *);
212 static void bge_setmulti	(struct bge_softc *);
213 
214 static void bge_handle_events	(struct bge_softc *);
215 static int bge_alloc_jumbo_mem	(struct bge_softc *);
216 static void bge_free_jumbo_mem	(struct bge_softc *);
217 static void *bge_jalloc		(struct bge_softc *);
218 static void bge_jfree		(void *, void *);
219 static int bge_newbuf_std	(struct bge_softc *, int, struct mbuf *);
220 static int bge_newbuf_jumbo	(struct bge_softc *, int, struct mbuf *);
221 static int bge_init_rx_ring_std	(struct bge_softc *);
222 static void bge_free_rx_ring_std	(struct bge_softc *);
223 static int bge_init_rx_ring_jumbo	(struct bge_softc *);
224 static void bge_free_rx_ring_jumbo	(struct bge_softc *);
225 static void bge_free_tx_ring	(struct bge_softc *);
226 static int bge_init_tx_ring	(struct bge_softc *);
227 
228 static int bge_chipinit		(struct bge_softc *);
229 static int bge_blockinit	(struct bge_softc *);
230 
231 #ifdef notdef
232 static u_int8_t bge_vpd_readbyte(struct bge_softc *, int);
233 static void bge_vpd_read_res	(struct bge_softc *, struct vpd_res *, int);
234 static void bge_vpd_read	(struct bge_softc *);
235 #endif
236 
237 static u_int32_t bge_readmem_ind
238 				(struct bge_softc *, int);
239 static void bge_writemem_ind	(struct bge_softc *, int, int);
240 #ifdef notdef
241 static u_int32_t bge_readreg_ind
242 				(struct bge_softc *, int);
243 #endif
244 static void bge_writereg_ind	(struct bge_softc *, int, int);
245 
246 static int bge_miibus_readreg	(device_t, int, int);
247 static int bge_miibus_writereg	(device_t, int, int, int);
248 static void bge_miibus_statchg	(device_t);
249 
250 static void bge_reset		(struct bge_softc *);
251 
252 static device_method_t bge_methods[] = {
253 	/* Device interface */
254 	DEVMETHOD(device_probe,		bge_probe),
255 	DEVMETHOD(device_attach,	bge_attach),
256 	DEVMETHOD(device_detach,	bge_detach),
257 	DEVMETHOD(device_shutdown,	bge_shutdown),
258 
259 	/* bus interface */
260 	DEVMETHOD(bus_print_child,	bus_generic_print_child),
261 	DEVMETHOD(bus_driver_added,	bus_generic_driver_added),
262 
263 	/* MII interface */
264 	DEVMETHOD(miibus_readreg,	bge_miibus_readreg),
265 	DEVMETHOD(miibus_writereg,	bge_miibus_writereg),
266 	DEVMETHOD(miibus_statchg,	bge_miibus_statchg),
267 
268 	{ 0, 0 }
269 };
270 
271 static driver_t bge_driver = {
272 	"bge",
273 	bge_methods,
274 	sizeof(struct bge_softc)
275 };
276 
277 static devclass_t bge_devclass;
278 
279 DRIVER_MODULE(bge, pci, bge_driver, bge_devclass, 0, 0);
280 DRIVER_MODULE(miibus, bge, miibus_driver, miibus_devclass, 0, 0);
281 
282 static u_int32_t
283 bge_readmem_ind(sc, off)
284 	struct bge_softc *sc;
285 	int off;
286 {
287 	device_t dev;
288 
289 	dev = sc->bge_dev;
290 
291 	pci_write_config(dev, BGE_PCI_MEMWIN_BASEADDR, off, 4);
292 	return(pci_read_config(dev, BGE_PCI_MEMWIN_DATA, 4));
293 }
294 
295 static void
296 bge_writemem_ind(sc, off, val)
297 	struct bge_softc *sc;
298 	int off, val;
299 {
300 	device_t dev;
301 
302 	dev = sc->bge_dev;
303 
304 	pci_write_config(dev, BGE_PCI_MEMWIN_BASEADDR, off, 4);
305 	pci_write_config(dev, BGE_PCI_MEMWIN_DATA, val, 4);
306 
307 	return;
308 }
309 
310 #ifdef notdef
311 static u_int32_t
312 bge_readreg_ind(sc, off)
313 	struct bge_softc *sc;
314 	int off;
315 {
316 	device_t dev;
317 
318 	dev = sc->bge_dev;
319 
320 	pci_write_config(dev, BGE_PCI_REG_BASEADDR, off, 4);
321 	return(pci_read_config(dev, BGE_PCI_REG_DATA, 4));
322 }
323 #endif
324 
325 static void
326 bge_writereg_ind(sc, off, val)
327 	struct bge_softc *sc;
328 	int off, val;
329 {
330 	device_t dev;
331 
332 	dev = sc->bge_dev;
333 
334 	pci_write_config(dev, BGE_PCI_REG_BASEADDR, off, 4);
335 	pci_write_config(dev, BGE_PCI_REG_DATA, val, 4);
336 
337 	return;
338 }
339 
340 /*
341  * Map a single buffer address.
342  */
343 
344 static void
345 bge_dma_map_addr(arg, segs, nseg, error)
346 	void *arg;
347 	bus_dma_segment_t *segs;
348 	int nseg;
349 	int error;
350 {
351 	struct bge_dmamap_arg *ctx;
352 
353 	if (error)
354 		return;
355 
356 	ctx = arg;
357 
358 	if (nseg > ctx->bge_maxsegs) {
359 		ctx->bge_maxsegs = 0;
360 		return;
361 	}
362 
363 	ctx->bge_busaddr = segs->ds_addr;
364 
365 	return;
366 }
367 
368 /*
369  * Map an mbuf chain into an TX ring.
370  */
371 
372 static void
373 bge_dma_map_tx_desc(arg, segs, nseg, mapsize, error)
374 	void *arg;
375 	bus_dma_segment_t *segs;
376 	int nseg;
377 	bus_size_t mapsize;
378 	int error;
379 {
380 	struct bge_dmamap_arg *ctx;
381 	struct bge_tx_bd *d = NULL;
382 	int i = 0, idx;
383 
384 	if (error)
385 		return;
386 
387 	ctx = arg;
388 
389 	/* Signal error to caller if there's too many segments */
390 	if (nseg > ctx->bge_maxsegs) {
391 		ctx->bge_maxsegs = 0;
392 		return;
393 	}
394 
395 	idx = ctx->bge_idx;
396 	while(1) {
397 		d = &ctx->bge_ring[idx];
398 		d->bge_addr.bge_addr_lo =
399 		    htole32(BGE_ADDR_LO(segs[i].ds_addr));
400 		d->bge_addr.bge_addr_hi =
401 		    htole32(BGE_ADDR_HI(segs[i].ds_addr));
402 		d->bge_len = htole16(segs[i].ds_len);
403 		d->bge_flags = htole16(ctx->bge_flags);
404                 i++;
405 		if (i == nseg)
406 			break;
407 		BGE_INC(idx, BGE_TX_RING_CNT);
408 	}
409 
410 	d->bge_flags |= htole16(BGE_TXBDFLAG_END);
411 	ctx->bge_maxsegs = nseg;
412 	ctx->bge_idx = idx;
413 
414 	return;
415 }
416 
417 
418 #ifdef notdef
419 static u_int8_t
420 bge_vpd_readbyte(sc, addr)
421 	struct bge_softc *sc;
422 	int addr;
423 {
424 	int i;
425 	device_t dev;
426 	u_int32_t val;
427 
428 	dev = sc->bge_dev;
429 	pci_write_config(dev, BGE_PCI_VPD_ADDR, addr, 2);
430 	for (i = 0; i < BGE_TIMEOUT * 10; i++) {
431 		DELAY(10);
432 		if (pci_read_config(dev, BGE_PCI_VPD_ADDR, 2) & BGE_VPD_FLAG)
433 			break;
434 	}
435 
436 	if (i == BGE_TIMEOUT) {
437 		printf("bge%d: VPD read timed out\n", sc->bge_unit);
438 		return(0);
439 	}
440 
441 	val = pci_read_config(dev, BGE_PCI_VPD_DATA, 4);
442 
443 	return((val >> ((addr % 4) * 8)) & 0xFF);
444 }
445 
446 static void
447 bge_vpd_read_res(sc, res, addr)
448 	struct bge_softc *sc;
449 	struct vpd_res *res;
450 	int addr;
451 {
452 	int i;
453 	u_int8_t *ptr;
454 
455 	ptr = (u_int8_t *)res;
456 	for (i = 0; i < sizeof(struct vpd_res); i++)
457 		ptr[i] = bge_vpd_readbyte(sc, i + addr);
458 
459 	return;
460 }
461 
462 static void
463 bge_vpd_read(sc)
464 	struct bge_softc *sc;
465 {
466 	int pos = 0, i;
467 	struct vpd_res res;
468 
469 	if (sc->bge_vpd_prodname != NULL)
470 		free(sc->bge_vpd_prodname, M_DEVBUF);
471 	if (sc->bge_vpd_readonly != NULL)
472 		free(sc->bge_vpd_readonly, M_DEVBUF);
473 	sc->bge_vpd_prodname = NULL;
474 	sc->bge_vpd_readonly = NULL;
475 
476 	bge_vpd_read_res(sc, &res, pos);
477 
478 	if (res.vr_id != VPD_RES_ID) {
479 		printf("bge%d: bad VPD resource id: expected %x got %x\n",
480 			sc->bge_unit, VPD_RES_ID, res.vr_id);
481                 return;
482         }
483 
484 	pos += sizeof(res);
485 	sc->bge_vpd_prodname = malloc(res.vr_len + 1, M_DEVBUF, M_NOWAIT);
486 	for (i = 0; i < res.vr_len; i++)
487 		sc->bge_vpd_prodname[i] = bge_vpd_readbyte(sc, i + pos);
488 	sc->bge_vpd_prodname[i] = '\0';
489 	pos += i;
490 
491 	bge_vpd_read_res(sc, &res, pos);
492 
493 	if (res.vr_id != VPD_RES_READ) {
494 		printf("bge%d: bad VPD resource id: expected %x got %x\n",
495 		    sc->bge_unit, VPD_RES_READ, res.vr_id);
496 		return;
497 	}
498 
499 	pos += sizeof(res);
500 	sc->bge_vpd_readonly = malloc(res.vr_len, M_DEVBUF, M_NOWAIT);
501 	for (i = 0; i < res.vr_len + 1; i++)
502 		sc->bge_vpd_readonly[i] = bge_vpd_readbyte(sc, i + pos);
503 
504 	return;
505 }
506 #endif
507 
508 /*
509  * Read a byte of data stored in the EEPROM at address 'addr.' The
510  * BCM570x supports both the traditional bitbang interface and an
511  * auto access interface for reading the EEPROM. We use the auto
512  * access method.
513  */
514 static u_int8_t
515 bge_eeprom_getbyte(sc, addr, dest)
516 	struct bge_softc *sc;
517 	int addr;
518 	u_int8_t *dest;
519 {
520 	int i;
521 	u_int32_t byte = 0;
522 
523 	/*
524 	 * Enable use of auto EEPROM access so we can avoid
525 	 * having to use the bitbang method.
526 	 */
527 	BGE_SETBIT(sc, BGE_MISC_LOCAL_CTL, BGE_MLC_AUTO_EEPROM);
528 
529 	/* Reset the EEPROM, load the clock period. */
530 	CSR_WRITE_4(sc, BGE_EE_ADDR,
531 	    BGE_EEADDR_RESET|BGE_EEHALFCLK(BGE_HALFCLK_384SCL));
532 	DELAY(20);
533 
534 	/* Issue the read EEPROM command. */
535 	CSR_WRITE_4(sc, BGE_EE_ADDR, BGE_EE_READCMD | addr);
536 
537 	/* Wait for completion */
538 	for(i = 0; i < BGE_TIMEOUT * 10; i++) {
539 		DELAY(10);
540 		if (CSR_READ_4(sc, BGE_EE_ADDR) & BGE_EEADDR_DONE)
541 			break;
542 	}
543 
544 	if (i == BGE_TIMEOUT) {
545 		printf("bge%d: eeprom read timed out\n", sc->bge_unit);
546 		return(0);
547 	}
548 
549 	/* Get result. */
550 	byte = CSR_READ_4(sc, BGE_EE_DATA);
551 
552         *dest = (byte >> ((addr % 4) * 8)) & 0xFF;
553 
554 	return(0);
555 }
556 
557 /*
558  * Read a sequence of bytes from the EEPROM.
559  */
560 static int
561 bge_read_eeprom(sc, dest, off, cnt)
562 	struct bge_softc *sc;
563 	caddr_t dest;
564 	int off;
565 	int cnt;
566 {
567 	int err = 0, i;
568 	u_int8_t byte = 0;
569 
570 	for (i = 0; i < cnt; i++) {
571 		err = bge_eeprom_getbyte(sc, off + i, &byte);
572 		if (err)
573 			break;
574 		*(dest + i) = byte;
575 	}
576 
577 	return(err ? 1 : 0);
578 }
579 
580 static int
581 bge_miibus_readreg(dev, phy, reg)
582 	device_t dev;
583 	int phy, reg;
584 {
585 	struct bge_softc *sc;
586 	u_int32_t val, autopoll;
587 	int i;
588 
589 	sc = device_get_softc(dev);
590 
591 	/*
592 	 * Broadcom's own driver always assumes the internal
593 	 * PHY is at GMII address 1. On some chips, the PHY responds
594 	 * to accesses at all addresses, which could cause us to
595 	 * bogusly attach the PHY 32 times at probe type. Always
596 	 * restricting the lookup to address 1 is simpler than
597 	 * trying to figure out which chips revisions should be
598 	 * special-cased.
599 	 */
600 	if (phy != 1)
601 		return(0);
602 
603 	/* Reading with autopolling on may trigger PCI errors */
604 	autopoll = CSR_READ_4(sc, BGE_MI_MODE);
605 	if (autopoll & BGE_MIMODE_AUTOPOLL) {
606 		BGE_CLRBIT(sc, BGE_MI_MODE, BGE_MIMODE_AUTOPOLL);
607 		DELAY(40);
608 	}
609 
610 	CSR_WRITE_4(sc, BGE_MI_COMM, BGE_MICMD_READ|BGE_MICOMM_BUSY|
611 	    BGE_MIPHY(phy)|BGE_MIREG(reg));
612 
613 	for (i = 0; i < BGE_TIMEOUT; i++) {
614 		val = CSR_READ_4(sc, BGE_MI_COMM);
615 		if (!(val & BGE_MICOMM_BUSY))
616 			break;
617 	}
618 
619 	if (i == BGE_TIMEOUT) {
620 		printf("bge%d: PHY read timed out\n", sc->bge_unit);
621 		val = 0;
622 		goto done;
623 	}
624 
625 	val = CSR_READ_4(sc, BGE_MI_COMM);
626 
627 done:
628 	if (autopoll & BGE_MIMODE_AUTOPOLL) {
629 		BGE_SETBIT(sc, BGE_MI_MODE, BGE_MIMODE_AUTOPOLL);
630 		DELAY(40);
631 	}
632 
633 	if (val & BGE_MICOMM_READFAIL)
634 		return(0);
635 
636 	return(val & 0xFFFF);
637 }
638 
639 static int
640 bge_miibus_writereg(dev, phy, reg, val)
641 	device_t dev;
642 	int phy, reg, val;
643 {
644 	struct bge_softc *sc;
645 	u_int32_t autopoll;
646 	int i;
647 
648 	sc = device_get_softc(dev);
649 
650 	/* Reading with autopolling on may trigger PCI errors */
651 	autopoll = CSR_READ_4(sc, BGE_MI_MODE);
652 	if (autopoll & BGE_MIMODE_AUTOPOLL) {
653 		BGE_CLRBIT(sc, BGE_MI_MODE, BGE_MIMODE_AUTOPOLL);
654 		DELAY(40);
655 	}
656 
657 	CSR_WRITE_4(sc, BGE_MI_COMM, BGE_MICMD_WRITE|BGE_MICOMM_BUSY|
658 	    BGE_MIPHY(phy)|BGE_MIREG(reg)|val);
659 
660 	for (i = 0; i < BGE_TIMEOUT; i++) {
661 		if (!(CSR_READ_4(sc, BGE_MI_COMM) & BGE_MICOMM_BUSY))
662 			break;
663 	}
664 
665 	if (autopoll & BGE_MIMODE_AUTOPOLL) {
666 		BGE_SETBIT(sc, BGE_MI_MODE, BGE_MIMODE_AUTOPOLL);
667 		DELAY(40);
668 	}
669 
670 	if (i == BGE_TIMEOUT) {
671 		printf("bge%d: PHY read timed out\n", sc->bge_unit);
672 		return(0);
673 	}
674 
675 	return(0);
676 }
677 
678 static void
679 bge_miibus_statchg(dev)
680 	device_t dev;
681 {
682 	struct bge_softc *sc;
683 	struct mii_data *mii;
684 
685 	sc = device_get_softc(dev);
686 	mii = device_get_softc(sc->bge_miibus);
687 
688 	BGE_CLRBIT(sc, BGE_MAC_MODE, BGE_MACMODE_PORTMODE);
689 	if (IFM_SUBTYPE(mii->mii_media_active) == IFM_1000_T) {
690 		BGE_SETBIT(sc, BGE_MAC_MODE, BGE_PORTMODE_GMII);
691 	} else {
692 		BGE_SETBIT(sc, BGE_MAC_MODE, BGE_PORTMODE_MII);
693 	}
694 
695 	if ((mii->mii_media_active & IFM_GMASK) == IFM_FDX) {
696 		BGE_CLRBIT(sc, BGE_MAC_MODE, BGE_MACMODE_HALF_DUPLEX);
697 	} else {
698 		BGE_SETBIT(sc, BGE_MAC_MODE, BGE_MACMODE_HALF_DUPLEX);
699 	}
700 
701 	return;
702 }
703 
704 /*
705  * Handle events that have triggered interrupts.
706  */
707 static void
708 bge_handle_events(sc)
709 	struct bge_softc		*sc;
710 {
711 
712 	return;
713 }
714 
715 /*
716  * Memory management for jumbo frames.
717  */
718 
719 static int
720 bge_alloc_jumbo_mem(sc)
721 	struct bge_softc		*sc;
722 {
723 	caddr_t			ptr;
724 	register int		i, error;
725 	struct bge_jpool_entry   *entry;
726 
727 	/* Create tag for jumbo buffer block */
728 
729 	error = bus_dma_tag_create(sc->bge_cdata.bge_parent_tag,
730 	    PAGE_SIZE, 0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL,
731 	    NULL, BGE_JMEM, 1, BGE_JMEM, 0, NULL, NULL,
732 	    &sc->bge_cdata.bge_jumbo_tag);
733 
734 	if (error) {
735 		printf("bge%d: could not allocate jumbo dma tag\n",
736 		    sc->bge_unit);
737 		return (ENOMEM);
738 	}
739 
740 	/* Allocate DMA'able memory for jumbo buffer block */
741 
742 	error = bus_dmamem_alloc(sc->bge_cdata.bge_jumbo_tag,
743 	    (void **)&sc->bge_ldata.bge_jumbo_buf, BUS_DMA_NOWAIT,
744 	    &sc->bge_cdata.bge_jumbo_map);
745 
746 	if (error)
747                 return (ENOMEM);
748 
749 	SLIST_INIT(&sc->bge_jfree_listhead);
750 	SLIST_INIT(&sc->bge_jinuse_listhead);
751 
752 	/*
753 	 * Now divide it up into 9K pieces and save the addresses
754 	 * in an array.
755 	 */
756 	ptr = sc->bge_ldata.bge_jumbo_buf;
757 	for (i = 0; i < BGE_JSLOTS; i++) {
758 		sc->bge_cdata.bge_jslots[i] = ptr;
759 		ptr += BGE_JLEN;
760 		entry = malloc(sizeof(struct bge_jpool_entry),
761 		    M_DEVBUF, M_NOWAIT);
762 		if (entry == NULL) {
763 			bge_free_jumbo_mem(sc);
764 			sc->bge_ldata.bge_jumbo_buf = NULL;
765 			printf("bge%d: no memory for jumbo "
766 			    "buffer queue!\n", sc->bge_unit);
767 			return(ENOBUFS);
768 		}
769 		entry->slot = i;
770 		SLIST_INSERT_HEAD(&sc->bge_jfree_listhead,
771 		    entry, jpool_entries);
772 	}
773 
774 	return(0);
775 }
776 
777 static void
778 bge_free_jumbo_mem(sc)
779         struct bge_softc *sc;
780 {
781         int i;
782         struct bge_jpool_entry *entry;
783 
784 	for (i = 0; i < BGE_JSLOTS; i++) {
785 		entry = SLIST_FIRST(&sc->bge_jfree_listhead);
786 		SLIST_REMOVE_HEAD(&sc->bge_jfree_listhead, jpool_entries);
787 		free(entry, M_DEVBUF);
788 	}
789 
790 	/* Destroy jumbo buffer block */
791 
792 	if (sc->bge_ldata.bge_rx_jumbo_ring)
793 		bus_dmamem_free(sc->bge_cdata.bge_jumbo_tag,
794 		    sc->bge_ldata.bge_jumbo_buf,
795 		    sc->bge_cdata.bge_jumbo_map);
796 
797 	if (sc->bge_cdata.bge_rx_jumbo_ring_map)
798 		bus_dmamap_destroy(sc->bge_cdata.bge_jumbo_tag,
799 		    sc->bge_cdata.bge_jumbo_map);
800 
801 	if (sc->bge_cdata.bge_jumbo_tag)
802 		bus_dma_tag_destroy(sc->bge_cdata.bge_jumbo_tag);
803 
804         return;
805 }
806 
807 /*
808  * Allocate a jumbo buffer.
809  */
810 static void *
811 bge_jalloc(sc)
812 	struct bge_softc		*sc;
813 {
814 	struct bge_jpool_entry   *entry;
815 
816 	entry = SLIST_FIRST(&sc->bge_jfree_listhead);
817 
818 	if (entry == NULL) {
819 		printf("bge%d: no free jumbo buffers\n", sc->bge_unit);
820 		return(NULL);
821 	}
822 
823 	SLIST_REMOVE_HEAD(&sc->bge_jfree_listhead, jpool_entries);
824 	SLIST_INSERT_HEAD(&sc->bge_jinuse_listhead, entry, jpool_entries);
825 	return(sc->bge_cdata.bge_jslots[entry->slot]);
826 }
827 
828 /*
829  * Release a jumbo buffer.
830  */
831 static void
832 bge_jfree(buf, args)
833 	void *buf;
834 	void *args;
835 {
836 	struct bge_jpool_entry *entry;
837 	struct bge_softc *sc;
838 	int i;
839 
840 	/* Extract the softc struct pointer. */
841 	sc = (struct bge_softc *)args;
842 
843 	if (sc == NULL)
844 		panic("bge_jfree: can't find softc pointer!");
845 
846 	/* calculate the slot this buffer belongs to */
847 
848 	i = ((vm_offset_t)buf
849 	     - (vm_offset_t)sc->bge_ldata.bge_jumbo_buf) / BGE_JLEN;
850 
851 	if ((i < 0) || (i >= BGE_JSLOTS))
852 		panic("bge_jfree: asked to free buffer that we don't manage!");
853 
854 	entry = SLIST_FIRST(&sc->bge_jinuse_listhead);
855 	if (entry == NULL)
856 		panic("bge_jfree: buffer not in use!");
857 	entry->slot = i;
858 	SLIST_REMOVE_HEAD(&sc->bge_jinuse_listhead, jpool_entries);
859 	SLIST_INSERT_HEAD(&sc->bge_jfree_listhead, entry, jpool_entries);
860 
861 	return;
862 }
863 
864 
865 /*
866  * Intialize a standard receive ring descriptor.
867  */
868 static int
869 bge_newbuf_std(sc, i, m)
870 	struct bge_softc	*sc;
871 	int			i;
872 	struct mbuf		*m;
873 {
874 	struct mbuf		*m_new = NULL;
875 	struct bge_rx_bd	*r;
876 	struct bge_dmamap_arg	ctx;
877 	int			error;
878 
879 	if (m == NULL) {
880 		MGETHDR(m_new, M_DONTWAIT, MT_DATA);
881 		if (m_new == NULL) {
882 			return(ENOBUFS);
883 		}
884 
885 		MCLGET(m_new, M_DONTWAIT);
886 		if (!(m_new->m_flags & M_EXT)) {
887 			m_freem(m_new);
888 			return(ENOBUFS);
889 		}
890 		m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
891 	} else {
892 		m_new = m;
893 		m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
894 		m_new->m_data = m_new->m_ext.ext_buf;
895 	}
896 
897 	if (!sc->bge_rx_alignment_bug)
898 		m_adj(m_new, ETHER_ALIGN);
899 	sc->bge_cdata.bge_rx_std_chain[i] = m_new;
900 	r = &sc->bge_ldata.bge_rx_std_ring[i];
901 	ctx.bge_maxsegs = 1;
902 	ctx.sc = sc;
903 	error = bus_dmamap_load(sc->bge_cdata.bge_mtag,
904 	    sc->bge_cdata.bge_rx_std_dmamap[i], mtod(m_new, void *),
905 	    m_new->m_len, bge_dma_map_addr, &ctx, BUS_DMA_NOWAIT);
906 	if (error || ctx.bge_maxsegs == 0) {
907 		if (m == NULL)
908 			m_freem(m_new);
909 		return(ENOMEM);
910 	}
911 	r->bge_addr.bge_addr_lo = htole32(BGE_ADDR_LO(ctx.bge_busaddr));
912 	r->bge_addr.bge_addr_hi = htole32(BGE_ADDR_HI(ctx.bge_busaddr));
913 	r->bge_flags = htole16(BGE_RXBDFLAG_END);
914 	r->bge_len = htole16(m_new->m_len);
915 	r->bge_idx = htole16(i);
916 
917 	bus_dmamap_sync(sc->bge_cdata.bge_mtag,
918 	    sc->bge_cdata.bge_rx_std_dmamap[i],
919 	    BUS_DMASYNC_PREREAD);
920 
921 	return(0);
922 }
923 
924 /*
925  * Initialize a jumbo receive ring descriptor. This allocates
926  * a jumbo buffer from the pool managed internally by the driver.
927  */
928 static int
929 bge_newbuf_jumbo(sc, i, m)
930 	struct bge_softc *sc;
931 	int i;
932 	struct mbuf *m;
933 {
934 	struct mbuf *m_new = NULL;
935 	struct bge_rx_bd *r;
936 	struct bge_dmamap_arg ctx;
937 	int error;
938 
939 	if (m == NULL) {
940 		caddr_t			*buf = NULL;
941 
942 		/* Allocate the mbuf. */
943 		MGETHDR(m_new, M_DONTWAIT, MT_DATA);
944 		if (m_new == NULL) {
945 			return(ENOBUFS);
946 		}
947 
948 		/* Allocate the jumbo buffer */
949 		buf = bge_jalloc(sc);
950 		if (buf == NULL) {
951 			m_freem(m_new);
952 			printf("bge%d: jumbo allocation failed "
953 			    "-- packet dropped!\n", sc->bge_unit);
954 			return(ENOBUFS);
955 		}
956 
957 		/* Attach the buffer to the mbuf. */
958 		m_new->m_data = (void *) buf;
959 		m_new->m_len = m_new->m_pkthdr.len = BGE_JUMBO_FRAMELEN;
960 		MEXTADD(m_new, buf, BGE_JUMBO_FRAMELEN, bge_jfree,
961 		    (struct bge_softc *)sc, 0, EXT_NET_DRV);
962 	} else {
963 		m_new = m;
964 		m_new->m_data = m_new->m_ext.ext_buf;
965 		m_new->m_ext.ext_size = BGE_JUMBO_FRAMELEN;
966 	}
967 
968 	if (!sc->bge_rx_alignment_bug)
969 		m_adj(m_new, ETHER_ALIGN);
970 	/* Set up the descriptor. */
971 	sc->bge_cdata.bge_rx_jumbo_chain[i] = m_new;
972 	r = &sc->bge_ldata.bge_rx_jumbo_ring[i];
973 	ctx.bge_maxsegs = 1;
974 	ctx.sc = sc;
975 	error = bus_dmamap_load(sc->bge_cdata.bge_mtag_jumbo,
976 	    sc->bge_cdata.bge_rx_jumbo_dmamap[i], mtod(m_new, void *),
977 	    m_new->m_len, bge_dma_map_addr, &ctx, BUS_DMA_NOWAIT);
978 	if (error || ctx.bge_maxsegs == 0) {
979 		if (m == NULL)
980 			m_freem(m_new);
981 		return(ENOMEM);
982 	}
983 	r->bge_addr.bge_addr_lo = htole32(BGE_ADDR_LO(ctx.bge_busaddr));
984 	r->bge_addr.bge_addr_hi = htole32(BGE_ADDR_HI(ctx.bge_busaddr));
985 	r->bge_flags = htole16(BGE_RXBDFLAG_END|BGE_RXBDFLAG_JUMBO_RING);
986 	r->bge_len = htole16(m_new->m_len);
987 	r->bge_idx = htole16(i);
988 
989 	bus_dmamap_sync(sc->bge_cdata.bge_mtag,
990 	    sc->bge_cdata.bge_rx_jumbo_dmamap[i],
991 	    BUS_DMASYNC_PREREAD);
992 
993 	return(0);
994 }
995 
996 /*
997  * The standard receive ring has 512 entries in it. At 2K per mbuf cluster,
998  * that's 1MB or memory, which is a lot. For now, we fill only the first
999  * 256 ring entries and hope that our CPU is fast enough to keep up with
1000  * the NIC.
1001  */
1002 static int
1003 bge_init_rx_ring_std(sc)
1004 	struct bge_softc *sc;
1005 {
1006 	int i;
1007 
1008 	for (i = 0; i < BGE_SSLOTS; i++) {
1009 		if (bge_newbuf_std(sc, i, NULL) == ENOBUFS)
1010 			return(ENOBUFS);
1011 	};
1012 
1013 	bus_dmamap_sync(sc->bge_cdata.bge_rx_std_ring_tag,
1014 	    sc->bge_cdata.bge_rx_std_ring_map,
1015 	    BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
1016 
1017 	sc->bge_std = i - 1;
1018 	CSR_WRITE_4(sc, BGE_MBX_RX_STD_PROD_LO, sc->bge_std);
1019 
1020 	return(0);
1021 }
1022 
1023 static void
1024 bge_free_rx_ring_std(sc)
1025 	struct bge_softc *sc;
1026 {
1027 	int i;
1028 
1029 	for (i = 0; i < BGE_STD_RX_RING_CNT; i++) {
1030 		if (sc->bge_cdata.bge_rx_std_chain[i] != NULL) {
1031 			m_freem(sc->bge_cdata.bge_rx_std_chain[i]);
1032 			sc->bge_cdata.bge_rx_std_chain[i] = NULL;
1033 			bus_dmamap_unload(sc->bge_cdata.bge_mtag,
1034 			    sc->bge_cdata.bge_rx_std_dmamap[i]);
1035 		}
1036 		bzero((char *)&sc->bge_ldata.bge_rx_std_ring[i],
1037 		    sizeof(struct bge_rx_bd));
1038 	}
1039 
1040 	return;
1041 }
1042 
1043 static int
1044 bge_init_rx_ring_jumbo(sc)
1045 	struct bge_softc *sc;
1046 {
1047 	int i;
1048 	struct bge_rcb *rcb;
1049 
1050 	for (i = 0; i < BGE_JUMBO_RX_RING_CNT; i++) {
1051 		if (bge_newbuf_jumbo(sc, i, NULL) == ENOBUFS)
1052 			return(ENOBUFS);
1053 	};
1054 
1055 	bus_dmamap_sync(sc->bge_cdata.bge_rx_jumbo_ring_tag,
1056 	    sc->bge_cdata.bge_rx_jumbo_ring_map,
1057 	    BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
1058 
1059 	sc->bge_jumbo = i - 1;
1060 
1061 	rcb = &sc->bge_ldata.bge_info.bge_jumbo_rx_rcb;
1062 	rcb->bge_maxlen_flags = BGE_RCB_MAXLEN_FLAGS(0, 0);
1063 	CSR_WRITE_4(sc, BGE_RX_JUMBO_RCB_MAXLEN_FLAGS, rcb->bge_maxlen_flags);
1064 
1065 	CSR_WRITE_4(sc, BGE_MBX_RX_JUMBO_PROD_LO, sc->bge_jumbo);
1066 
1067 	return(0);
1068 }
1069 
1070 static void
1071 bge_free_rx_ring_jumbo(sc)
1072 	struct bge_softc *sc;
1073 {
1074 	int i;
1075 
1076 	for (i = 0; i < BGE_JUMBO_RX_RING_CNT; i++) {
1077 		if (sc->bge_cdata.bge_rx_jumbo_chain[i] != NULL) {
1078 			m_freem(sc->bge_cdata.bge_rx_jumbo_chain[i]);
1079 			sc->bge_cdata.bge_rx_jumbo_chain[i] = NULL;
1080 			bus_dmamap_unload(sc->bge_cdata.bge_mtag_jumbo,
1081 			    sc->bge_cdata.bge_rx_jumbo_dmamap[i]);
1082 		}
1083 		bzero((char *)&sc->bge_ldata.bge_rx_jumbo_ring[i],
1084 		    sizeof(struct bge_rx_bd));
1085 	}
1086 
1087 	return;
1088 }
1089 
1090 static void
1091 bge_free_tx_ring(sc)
1092 	struct bge_softc *sc;
1093 {
1094 	int i;
1095 
1096 	if (sc->bge_ldata.bge_tx_ring == NULL)
1097 		return;
1098 
1099 	for (i = 0; i < BGE_TX_RING_CNT; i++) {
1100 		if (sc->bge_cdata.bge_tx_chain[i] != NULL) {
1101 			m_freem(sc->bge_cdata.bge_tx_chain[i]);
1102 			sc->bge_cdata.bge_tx_chain[i] = NULL;
1103 			bus_dmamap_unload(sc->bge_cdata.bge_mtag,
1104 			    sc->bge_cdata.bge_tx_dmamap[i]);
1105 		}
1106 		bzero((char *)&sc->bge_ldata.bge_tx_ring[i],
1107 		    sizeof(struct bge_tx_bd));
1108 	}
1109 
1110 	return;
1111 }
1112 
1113 static int
1114 bge_init_tx_ring(sc)
1115 	struct bge_softc *sc;
1116 {
1117 	sc->bge_txcnt = 0;
1118 	sc->bge_tx_saved_considx = 0;
1119 
1120 	CSR_WRITE_4(sc, BGE_MBX_TX_HOST_PROD0_LO, 0);
1121 	/* 5700 b2 errata */
1122 	if (sc->bge_chiprev == BGE_CHIPREV_5700_BX)
1123 		CSR_WRITE_4(sc, BGE_MBX_TX_HOST_PROD0_LO, 0);
1124 
1125 	CSR_WRITE_4(sc, BGE_MBX_TX_NIC_PROD0_LO, 0);
1126 	/* 5700 b2 errata */
1127 	if (sc->bge_chiprev == BGE_CHIPREV_5700_BX)
1128 		CSR_WRITE_4(sc, BGE_MBX_TX_NIC_PROD0_LO, 0);
1129 
1130 	return(0);
1131 }
1132 
1133 #define BGE_POLY	0xEDB88320
1134 
1135 static uint32_t
1136 bge_mchash(addr)
1137 	const uint8_t *addr;
1138 {
1139 	uint32_t crc;
1140 	int idx, bit;
1141 	uint8_t data;
1142 
1143 	/* Compute CRC for the address value. */
1144 	crc = 0xFFFFFFFF; /* initial value */
1145 
1146 	for (idx = 0; idx < 6; idx++) {
1147 		for (data = *addr++, bit = 0; bit < 8; bit++, data >>= 1)
1148 			crc = (crc >> 1) ^ (((crc ^ data) & 1) ? BGE_POLY : 0);
1149 	}
1150 
1151 	return(crc & 0x7F);
1152 }
1153 
1154 static void
1155 bge_setmulti(sc)
1156 	struct bge_softc *sc;
1157 {
1158 	struct ifnet *ifp;
1159 	struct ifmultiaddr *ifma;
1160 	u_int32_t hashes[4] = { 0, 0, 0, 0 };
1161 	int h, i;
1162 
1163 	BGE_LOCK_ASSERT(sc);
1164 
1165 	ifp = &sc->arpcom.ac_if;
1166 
1167 	if (ifp->if_flags & IFF_ALLMULTI || ifp->if_flags & IFF_PROMISC) {
1168 		for (i = 0; i < 4; i++)
1169 			CSR_WRITE_4(sc, BGE_MAR0 + (i * 4), 0xFFFFFFFF);
1170 		return;
1171 	}
1172 
1173 	/* First, zot all the existing filters. */
1174 	for (i = 0; i < 4; i++)
1175 		CSR_WRITE_4(sc, BGE_MAR0 + (i * 4), 0);
1176 
1177 	/* Now program new ones. */
1178 	TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
1179 		if (ifma->ifma_addr->sa_family != AF_LINK)
1180 			continue;
1181 		h = bge_mchash(LLADDR((struct sockaddr_dl *)ifma->ifma_addr));
1182 		hashes[(h & 0x60) >> 5] |= 1 << (h & 0x1F);
1183 	}
1184 
1185 	for (i = 0; i < 4; i++)
1186 		CSR_WRITE_4(sc, BGE_MAR0 + (i * 4), hashes[i]);
1187 
1188 	return;
1189 }
1190 
1191 /*
1192  * Do endian, PCI and DMA initialization. Also check the on-board ROM
1193  * self-test results.
1194  */
1195 static int
1196 bge_chipinit(sc)
1197 	struct bge_softc *sc;
1198 {
1199 	int			i;
1200 	u_int32_t		dma_rw_ctl;
1201 
1202 	/* Set endianness before we access any non-PCI registers. */
1203 #if BYTE_ORDER == BIG_ENDIAN
1204 	pci_write_config(sc->bge_dev, BGE_PCI_MISC_CTL,
1205 	    BGE_BIGENDIAN_INIT, 4);
1206 #else
1207 	pci_write_config(sc->bge_dev, BGE_PCI_MISC_CTL,
1208 	    BGE_LITTLEENDIAN_INIT, 4);
1209 #endif
1210 
1211 	/*
1212 	 * Check the 'ROM failed' bit on the RX CPU to see if
1213 	 * self-tests passed.
1214 	 */
1215 	if (CSR_READ_4(sc, BGE_RXCPU_MODE) & BGE_RXCPUMODE_ROMFAIL) {
1216 		printf("bge%d: RX CPU self-diagnostics failed!\n",
1217 		    sc->bge_unit);
1218 		return(ENODEV);
1219 	}
1220 
1221 	/* Clear the MAC control register */
1222 	CSR_WRITE_4(sc, BGE_MAC_MODE, 0);
1223 
1224 	/*
1225 	 * Clear the MAC statistics block in the NIC's
1226 	 * internal memory.
1227 	 */
1228 	for (i = BGE_STATS_BLOCK;
1229 	    i < BGE_STATS_BLOCK_END + 1; i += sizeof(u_int32_t))
1230 		BGE_MEMWIN_WRITE(sc, i, 0);
1231 
1232 	for (i = BGE_STATUS_BLOCK;
1233 	    i < BGE_STATUS_BLOCK_END + 1; i += sizeof(u_int32_t))
1234 		BGE_MEMWIN_WRITE(sc, i, 0);
1235 
1236 	/* Set up the PCI DMA control register. */
1237 	if (pci_read_config(sc->bge_dev, BGE_PCI_PCISTATE, 4) &
1238 	    BGE_PCISTATE_PCI_BUSMODE) {
1239 		/* Conventional PCI bus */
1240 		dma_rw_ctl = BGE_PCI_READ_CMD|BGE_PCI_WRITE_CMD |
1241 		    (0x7 << BGE_PCIDMARWCTL_RD_WAT_SHIFT) |
1242 		    (0x7 << BGE_PCIDMARWCTL_WR_WAT_SHIFT) |
1243 		    (0x0F);
1244 	} else {
1245 		/* PCI-X bus */
1246 		/*
1247 		 * The 5704 uses a different encoding of read/write
1248 		 * watermarks.
1249 		 */
1250 		if (sc->bge_asicrev == BGE_ASICREV_BCM5704)
1251 			dma_rw_ctl = BGE_PCI_READ_CMD|BGE_PCI_WRITE_CMD |
1252 			    (0x7 << BGE_PCIDMARWCTL_RD_WAT_SHIFT) |
1253 			    (0x3 << BGE_PCIDMARWCTL_WR_WAT_SHIFT);
1254 		else
1255 			dma_rw_ctl = BGE_PCI_READ_CMD|BGE_PCI_WRITE_CMD |
1256 			    (0x3 << BGE_PCIDMARWCTL_RD_WAT_SHIFT) |
1257 			    (0x3 << BGE_PCIDMARWCTL_WR_WAT_SHIFT) |
1258 			    (0x0F);
1259 
1260 		/*
1261 		 * 5703 and 5704 need ONEDMA_AT_ONCE as a workaround
1262 		 * for hardware bugs.
1263 		 */
1264 		if (sc->bge_asicrev == BGE_ASICREV_BCM5703 ||
1265 		    sc->bge_asicrev == BGE_ASICREV_BCM5704) {
1266 			u_int32_t tmp;
1267 
1268 			tmp = CSR_READ_4(sc, BGE_PCI_CLKCTL) & 0x1f;
1269 			if (tmp == 0x6 || tmp == 0x7)
1270 				dma_rw_ctl |= BGE_PCIDMARWCTL_ONEDMA_ATONCE;
1271 		}
1272 	}
1273 
1274 	if (sc->bge_asicrev == BGE_ASICREV_BCM5703 ||
1275 	    sc->bge_asicrev == BGE_ASICREV_BCM5704 ||
1276 	    sc->bge_asicrev == BGE_ASICREV_BCM5705)
1277 		dma_rw_ctl &= ~BGE_PCIDMARWCTL_MINDMA;
1278 	pci_write_config(sc->bge_dev, BGE_PCI_DMA_RW_CTL, dma_rw_ctl, 4);
1279 
1280 	/*
1281 	 * Set up general mode register.
1282 	 */
1283 	CSR_WRITE_4(sc, BGE_MODE_CTL, BGE_MODECTL_WORDSWAP_NONFRAME|
1284 	    BGE_MODECTL_BYTESWAP_DATA|BGE_MODECTL_WORDSWAP_DATA|
1285 	    BGE_MODECTL_MAC_ATTN_INTR|BGE_MODECTL_HOST_SEND_BDS|
1286 	    BGE_MODECTL_TX_NO_PHDR_CSUM|BGE_MODECTL_RX_NO_PHDR_CSUM);
1287 
1288 	/*
1289 	 * Disable memory write invalidate.  Apparently it is not supported
1290 	 * properly by these devices.
1291 	 */
1292 	PCI_CLRBIT(sc->bge_dev, BGE_PCI_CMD, PCIM_CMD_MWIEN, 4);
1293 
1294 #ifdef __brokenalpha__
1295 	/*
1296 	 * Must insure that we do not cross an 8K (bytes) boundary
1297 	 * for DMA reads.  Our highest limit is 1K bytes.  This is a
1298 	 * restriction on some ALPHA platforms with early revision
1299 	 * 21174 PCI chipsets, such as the AlphaPC 164lx
1300 	 */
1301 	PCI_SETBIT(sc->bge_dev, BGE_PCI_DMA_RW_CTL,
1302 	    BGE_PCI_READ_BNDRY_1024BYTES, 4);
1303 #endif
1304 
1305 	/* Set the timer prescaler (always 66Mhz) */
1306 	CSR_WRITE_4(sc, BGE_MISC_CFG, 65 << 1/*BGE_32BITTIME_66MHZ*/);
1307 
1308 	return(0);
1309 }
1310 
1311 static int
1312 bge_blockinit(sc)
1313 	struct bge_softc *sc;
1314 {
1315 	struct bge_rcb *rcb;
1316 	volatile struct bge_rcb *vrcb;
1317 	int i;
1318 
1319 	/*
1320 	 * Initialize the memory window pointer register so that
1321 	 * we can access the first 32K of internal NIC RAM. This will
1322 	 * allow us to set up the TX send ring RCBs and the RX return
1323 	 * ring RCBs, plus other things which live in NIC memory.
1324 	 */
1325 	CSR_WRITE_4(sc, BGE_PCI_MEMWIN_BASEADDR, 0);
1326 
1327 	/* Note: the BCM5704 has a smaller mbuf space than other chips. */
1328 
1329 	if (sc->bge_asicrev != BGE_ASICREV_BCM5705) {
1330 		/* Configure mbuf memory pool */
1331 		if (sc->bge_extram) {
1332 			CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_BASEADDR,
1333 			    BGE_EXT_SSRAM);
1334 			if (sc->bge_asicrev == BGE_ASICREV_BCM5704)
1335 				CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_LEN, 0x10000);
1336 			else
1337 				CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_LEN, 0x18000);
1338 		} else {
1339 			CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_BASEADDR,
1340 			    BGE_BUFFPOOL_1);
1341 			if (sc->bge_asicrev == BGE_ASICREV_BCM5704)
1342 				CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_LEN, 0x10000);
1343 			else
1344 				CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_LEN, 0x18000);
1345 		}
1346 
1347 		/* Configure DMA resource pool */
1348 		CSR_WRITE_4(sc, BGE_BMAN_DMA_DESCPOOL_BASEADDR,
1349 		    BGE_DMA_DESCRIPTORS);
1350 		CSR_WRITE_4(sc, BGE_BMAN_DMA_DESCPOOL_LEN, 0x2000);
1351 	}
1352 
1353 	/* Configure mbuf pool watermarks */
1354 	if (sc->bge_asicrev == BGE_ASICREV_BCM5705) {
1355 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_READDMA_LOWAT, 0x0);
1356 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_MACRX_LOWAT, 0x10);
1357 	} else {
1358 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_READDMA_LOWAT, 0x50);
1359 		CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_MACRX_LOWAT, 0x20);
1360 	}
1361 	CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_HIWAT, 0x60);
1362 
1363 	/* Configure DMA resource watermarks */
1364 	CSR_WRITE_4(sc, BGE_BMAN_DMA_DESCPOOL_LOWAT, 5);
1365 	CSR_WRITE_4(sc, BGE_BMAN_DMA_DESCPOOL_HIWAT, 10);
1366 
1367 	/* Enable buffer manager */
1368 	if (sc->bge_asicrev != BGE_ASICREV_BCM5705) {
1369 		CSR_WRITE_4(sc, BGE_BMAN_MODE,
1370 		    BGE_BMANMODE_ENABLE|BGE_BMANMODE_LOMBUF_ATTN);
1371 
1372 		/* Poll for buffer manager start indication */
1373 		for (i = 0; i < BGE_TIMEOUT; i++) {
1374 			if (CSR_READ_4(sc, BGE_BMAN_MODE) & BGE_BMANMODE_ENABLE)
1375 				break;
1376 			DELAY(10);
1377 		}
1378 
1379 		if (i == BGE_TIMEOUT) {
1380 			printf("bge%d: buffer manager failed to start\n",
1381 			    sc->bge_unit);
1382 			return(ENXIO);
1383 		}
1384 	}
1385 
1386 	/* Enable flow-through queues */
1387 	CSR_WRITE_4(sc, BGE_FTQ_RESET, 0xFFFFFFFF);
1388 	CSR_WRITE_4(sc, BGE_FTQ_RESET, 0);
1389 
1390 	/* Wait until queue initialization is complete */
1391 	for (i = 0; i < BGE_TIMEOUT; i++) {
1392 		if (CSR_READ_4(sc, BGE_FTQ_RESET) == 0)
1393 			break;
1394 		DELAY(10);
1395 	}
1396 
1397 	if (i == BGE_TIMEOUT) {
1398 		printf("bge%d: flow-through queue init failed\n",
1399 		    sc->bge_unit);
1400 		return(ENXIO);
1401 	}
1402 
1403 	/* Initialize the standard RX ring control block */
1404 	rcb = &sc->bge_ldata.bge_info.bge_std_rx_rcb;
1405 	rcb->bge_hostaddr.bge_addr_lo =
1406 	    BGE_ADDR_LO(sc->bge_ldata.bge_rx_std_ring_paddr);
1407 	rcb->bge_hostaddr.bge_addr_hi =
1408 	    BGE_ADDR_HI(sc->bge_ldata.bge_rx_std_ring_paddr);
1409 	bus_dmamap_sync(sc->bge_cdata.bge_rx_std_ring_tag,
1410 	    sc->bge_cdata.bge_rx_std_ring_map, BUS_DMASYNC_PREREAD);
1411 	if (sc->bge_asicrev == BGE_ASICREV_BCM5705)
1412 		rcb->bge_maxlen_flags = BGE_RCB_MAXLEN_FLAGS(512, 0);
1413 	else
1414 		rcb->bge_maxlen_flags =
1415 		    BGE_RCB_MAXLEN_FLAGS(BGE_MAX_FRAMELEN, 0);
1416 	if (sc->bge_extram)
1417 		rcb->bge_nicaddr = BGE_EXT_STD_RX_RINGS;
1418 	else
1419 		rcb->bge_nicaddr = BGE_STD_RX_RINGS;
1420 	CSR_WRITE_4(sc, BGE_RX_STD_RCB_HADDR_HI, rcb->bge_hostaddr.bge_addr_hi);
1421 	CSR_WRITE_4(sc, BGE_RX_STD_RCB_HADDR_LO, rcb->bge_hostaddr.bge_addr_lo);
1422 
1423 	CSR_WRITE_4(sc, BGE_RX_STD_RCB_MAXLEN_FLAGS, rcb->bge_maxlen_flags);
1424 	CSR_WRITE_4(sc, BGE_RX_STD_RCB_NICADDR, rcb->bge_nicaddr);
1425 
1426 	/*
1427 	 * Initialize the jumbo RX ring control block
1428 	 * We set the 'ring disabled' bit in the flags
1429 	 * field until we're actually ready to start
1430 	 * using this ring (i.e. once we set the MTU
1431 	 * high enough to require it).
1432 	 */
1433 	if (sc->bge_asicrev != BGE_ASICREV_BCM5705) {
1434 		rcb = &sc->bge_ldata.bge_info.bge_jumbo_rx_rcb;
1435 
1436 		rcb->bge_hostaddr.bge_addr_lo =
1437 		    BGE_ADDR_LO(sc->bge_ldata.bge_rx_jumbo_ring_paddr);
1438 		rcb->bge_hostaddr.bge_addr_hi =
1439 		    BGE_ADDR_HI(sc->bge_ldata.bge_rx_jumbo_ring_paddr);
1440 		bus_dmamap_sync(sc->bge_cdata.bge_rx_jumbo_ring_tag,
1441 		    sc->bge_cdata.bge_rx_jumbo_ring_map,
1442 		    BUS_DMASYNC_PREREAD);
1443 		rcb->bge_maxlen_flags =
1444 		    BGE_RCB_MAXLEN_FLAGS(BGE_MAX_FRAMELEN,
1445 		    BGE_RCB_FLAG_RING_DISABLED);
1446 		if (sc->bge_extram)
1447 			rcb->bge_nicaddr = BGE_EXT_JUMBO_RX_RINGS;
1448 		else
1449 			rcb->bge_nicaddr = BGE_JUMBO_RX_RINGS;
1450 		CSR_WRITE_4(sc, BGE_RX_JUMBO_RCB_HADDR_HI,
1451 		    rcb->bge_hostaddr.bge_addr_hi);
1452 		CSR_WRITE_4(sc, BGE_RX_JUMBO_RCB_HADDR_LO,
1453 		    rcb->bge_hostaddr.bge_addr_lo);
1454 
1455 		CSR_WRITE_4(sc, BGE_RX_JUMBO_RCB_MAXLEN_FLAGS,
1456 		    rcb->bge_maxlen_flags);
1457 		CSR_WRITE_4(sc, BGE_RX_JUMBO_RCB_NICADDR, rcb->bge_nicaddr);
1458 
1459 		/* Set up dummy disabled mini ring RCB */
1460 		rcb = &sc->bge_ldata.bge_info.bge_mini_rx_rcb;
1461 		rcb->bge_maxlen_flags =
1462 		    BGE_RCB_MAXLEN_FLAGS(0, BGE_RCB_FLAG_RING_DISABLED);
1463 		CSR_WRITE_4(sc, BGE_RX_MINI_RCB_MAXLEN_FLAGS,
1464 		    rcb->bge_maxlen_flags);
1465 	}
1466 
1467 	/*
1468 	 * Set the BD ring replentish thresholds. The recommended
1469 	 * values are 1/8th the number of descriptors allocated to
1470 	 * each ring.
1471 	 */
1472 	CSR_WRITE_4(sc, BGE_RBDI_STD_REPL_THRESH, BGE_STD_RX_RING_CNT/8);
1473 	CSR_WRITE_4(sc, BGE_RBDI_JUMBO_REPL_THRESH, BGE_JUMBO_RX_RING_CNT/8);
1474 
1475 	/*
1476 	 * Disable all unused send rings by setting the 'ring disabled'
1477 	 * bit in the flags field of all the TX send ring control blocks.
1478 	 * These are located in NIC memory.
1479 	 */
1480 	vrcb = (volatile struct bge_rcb *)(sc->bge_vhandle + BGE_MEMWIN_START +
1481 	    BGE_SEND_RING_RCB);
1482 	for (i = 0; i < BGE_TX_RINGS_EXTSSRAM_MAX; i++) {
1483 		vrcb->bge_maxlen_flags =
1484 		    BGE_RCB_MAXLEN_FLAGS(0, BGE_RCB_FLAG_RING_DISABLED);
1485 		vrcb->bge_nicaddr = 0;
1486 		vrcb++;
1487 	}
1488 
1489 	/* Configure TX RCB 0 (we use only the first ring) */
1490 	vrcb = (volatile struct bge_rcb *)(sc->bge_vhandle + BGE_MEMWIN_START +
1491 	    BGE_SEND_RING_RCB);
1492 	vrcb->bge_hostaddr.bge_addr_lo =
1493 	    htole32(BGE_ADDR_LO(sc->bge_ldata.bge_tx_ring_paddr));
1494 	vrcb->bge_hostaddr.bge_addr_hi =
1495 	    htole32(BGE_ADDR_HI(sc->bge_ldata.bge_tx_ring_paddr));
1496 	vrcb->bge_nicaddr = BGE_NIC_TXRING_ADDR(0, BGE_TX_RING_CNT);
1497 	if (sc->bge_asicrev != BGE_ASICREV_BCM5705)
1498 		vrcb->bge_maxlen_flags =
1499 		    BGE_RCB_MAXLEN_FLAGS(BGE_TX_RING_CNT, 0);
1500 
1501 	/* Disable all unused RX return rings */
1502 	vrcb = (volatile struct bge_rcb *)(sc->bge_vhandle + BGE_MEMWIN_START +
1503 	    BGE_RX_RETURN_RING_RCB);
1504 	for (i = 0; i < BGE_RX_RINGS_MAX; i++) {
1505 		vrcb->bge_hostaddr.bge_addr_hi = 0;
1506 		vrcb->bge_hostaddr.bge_addr_lo = 0;
1507 		vrcb->bge_maxlen_flags =
1508 		    BGE_RCB_MAXLEN_FLAGS(sc->bge_return_ring_cnt,
1509 		    BGE_RCB_FLAG_RING_DISABLED);
1510 		vrcb->bge_nicaddr = 0;
1511 		CSR_WRITE_4(sc, BGE_MBX_RX_CONS0_LO +
1512 		    (i * (sizeof(u_int64_t))), 0);
1513 		vrcb++;
1514 	}
1515 
1516 	/* Initialize RX ring indexes */
1517 	CSR_WRITE_4(sc, BGE_MBX_RX_STD_PROD_LO, 0);
1518 	CSR_WRITE_4(sc, BGE_MBX_RX_JUMBO_PROD_LO, 0);
1519 	CSR_WRITE_4(sc, BGE_MBX_RX_MINI_PROD_LO, 0);
1520 
1521 	/*
1522 	 * Set up RX return ring 0
1523 	 * Note that the NIC address for RX return rings is 0x00000000.
1524 	 * The return rings live entirely within the host, so the
1525 	 * nicaddr field in the RCB isn't used.
1526 	 */
1527 	vrcb = (volatile struct bge_rcb *)(sc->bge_vhandle + BGE_MEMWIN_START +
1528 	    BGE_RX_RETURN_RING_RCB);
1529 	vrcb->bge_hostaddr.bge_addr_lo =
1530 	    BGE_ADDR_LO(sc->bge_ldata.bge_rx_return_ring_paddr);
1531 	vrcb->bge_hostaddr.bge_addr_hi =
1532 	    BGE_ADDR_HI(sc->bge_ldata.bge_rx_return_ring_paddr);
1533 	bus_dmamap_sync(sc->bge_cdata.bge_rx_return_ring_tag,
1534 	    sc->bge_cdata.bge_rx_return_ring_map, BUS_DMASYNC_PREWRITE);
1535 	vrcb->bge_nicaddr = 0x00000000;
1536 	vrcb->bge_maxlen_flags =
1537 	    BGE_RCB_MAXLEN_FLAGS(sc->bge_return_ring_cnt, 0);
1538 
1539 	/* Set random backoff seed for TX */
1540 	CSR_WRITE_4(sc, BGE_TX_RANDOM_BACKOFF,
1541 	    sc->arpcom.ac_enaddr[0] + sc->arpcom.ac_enaddr[1] +
1542 	    sc->arpcom.ac_enaddr[2] + sc->arpcom.ac_enaddr[3] +
1543 	    sc->arpcom.ac_enaddr[4] + sc->arpcom.ac_enaddr[5] +
1544 	    BGE_TX_BACKOFF_SEED_MASK);
1545 
1546 	/* Set inter-packet gap */
1547 	CSR_WRITE_4(sc, BGE_TX_LENGTHS, 0x2620);
1548 
1549 	/*
1550 	 * Specify which ring to use for packets that don't match
1551 	 * any RX rules.
1552 	 */
1553 	CSR_WRITE_4(sc, BGE_RX_RULES_CFG, 0x08);
1554 
1555 	/*
1556 	 * Configure number of RX lists. One interrupt distribution
1557 	 * list, sixteen active lists, one bad frames class.
1558 	 */
1559 	CSR_WRITE_4(sc, BGE_RXLP_CFG, 0x181);
1560 
1561 	/* Inialize RX list placement stats mask. */
1562 	CSR_WRITE_4(sc, BGE_RXLP_STATS_ENABLE_MASK, 0x007FFFFF);
1563 	CSR_WRITE_4(sc, BGE_RXLP_STATS_CTL, 0x1);
1564 
1565 	/* Disable host coalescing until we get it set up */
1566 	CSR_WRITE_4(sc, BGE_HCC_MODE, 0x00000000);
1567 
1568 	/* Poll to make sure it's shut down. */
1569 	for (i = 0; i < BGE_TIMEOUT; i++) {
1570 		if (!(CSR_READ_4(sc, BGE_HCC_MODE) & BGE_HCCMODE_ENABLE))
1571 			break;
1572 		DELAY(10);
1573 	}
1574 
1575 	if (i == BGE_TIMEOUT) {
1576 		printf("bge%d: host coalescing engine failed to idle\n",
1577 		    sc->bge_unit);
1578 		return(ENXIO);
1579 	}
1580 
1581 	/* Set up host coalescing defaults */
1582 	CSR_WRITE_4(sc, BGE_HCC_RX_COAL_TICKS, sc->bge_rx_coal_ticks);
1583 	CSR_WRITE_4(sc, BGE_HCC_TX_COAL_TICKS, sc->bge_tx_coal_ticks);
1584 	CSR_WRITE_4(sc, BGE_HCC_RX_MAX_COAL_BDS, sc->bge_rx_max_coal_bds);
1585 	CSR_WRITE_4(sc, BGE_HCC_TX_MAX_COAL_BDS, sc->bge_tx_max_coal_bds);
1586 	if (sc->bge_asicrev != BGE_ASICREV_BCM5705) {
1587 		CSR_WRITE_4(sc, BGE_HCC_RX_COAL_TICKS_INT, 0);
1588 		CSR_WRITE_4(sc, BGE_HCC_TX_COAL_TICKS_INT, 0);
1589 	}
1590 	CSR_WRITE_4(sc, BGE_HCC_RX_MAX_COAL_BDS_INT, 0);
1591 	CSR_WRITE_4(sc, BGE_HCC_TX_MAX_COAL_BDS_INT, 0);
1592 
1593 	/* Set up address of statistics block */
1594 	if (sc->bge_asicrev != BGE_ASICREV_BCM5705) {
1595 		CSR_WRITE_4(sc, BGE_HCC_STATS_ADDR_HI,
1596 		    BGE_ADDR_HI(sc->bge_ldata.bge_stats_paddr));
1597 		CSR_WRITE_4(sc, BGE_HCC_STATS_ADDR_LO,
1598 		    BGE_ADDR_LO(sc->bge_ldata.bge_stats_paddr));
1599 		CSR_WRITE_4(sc, BGE_HCC_STATS_BASEADDR, BGE_STATS_BLOCK);
1600 		CSR_WRITE_4(sc, BGE_HCC_STATUSBLK_BASEADDR, BGE_STATUS_BLOCK);
1601 		CSR_WRITE_4(sc, BGE_HCC_STATS_TICKS, sc->bge_stat_ticks);
1602 	}
1603 
1604 	/* Set up address of status block */
1605 	CSR_WRITE_4(sc, BGE_HCC_STATUSBLK_ADDR_HI,
1606 	    BGE_ADDR_HI(sc->bge_ldata.bge_status_block_paddr));
1607 	CSR_WRITE_4(sc, BGE_HCC_STATUSBLK_ADDR_LO,
1608 	    BGE_ADDR_LO(sc->bge_ldata.bge_status_block_paddr));
1609 	bus_dmamap_sync(sc->bge_cdata.bge_status_tag,
1610 	    sc->bge_cdata.bge_status_map, BUS_DMASYNC_PREWRITE);
1611 	sc->bge_ldata.bge_status_block->bge_idx[0].bge_rx_prod_idx = 0;
1612 	sc->bge_ldata.bge_status_block->bge_idx[0].bge_tx_cons_idx = 0;
1613 
1614 	/* Turn on host coalescing state machine */
1615 	CSR_WRITE_4(sc, BGE_HCC_MODE, BGE_HCCMODE_ENABLE);
1616 
1617 	/* Turn on RX BD completion state machine and enable attentions */
1618 	CSR_WRITE_4(sc, BGE_RBDC_MODE,
1619 	    BGE_RBDCMODE_ENABLE|BGE_RBDCMODE_ATTN);
1620 
1621 	/* Turn on RX list placement state machine */
1622 	CSR_WRITE_4(sc, BGE_RXLP_MODE, BGE_RXLPMODE_ENABLE);
1623 
1624 	/* Turn on RX list selector state machine. */
1625 	if (sc->bge_asicrev != BGE_ASICREV_BCM5705)
1626 		CSR_WRITE_4(sc, BGE_RXLS_MODE, BGE_RXLSMODE_ENABLE);
1627 
1628 	/* Turn on DMA, clear stats */
1629 	CSR_WRITE_4(sc, BGE_MAC_MODE, BGE_MACMODE_TXDMA_ENB|
1630 	    BGE_MACMODE_RXDMA_ENB|BGE_MACMODE_RX_STATS_CLEAR|
1631 	    BGE_MACMODE_TX_STATS_CLEAR|BGE_MACMODE_RX_STATS_ENB|
1632 	    BGE_MACMODE_TX_STATS_ENB|BGE_MACMODE_FRMHDR_DMA_ENB|
1633 	    (sc->bge_tbi ? BGE_PORTMODE_TBI : BGE_PORTMODE_MII));
1634 
1635 	/* Set misc. local control, enable interrupts on attentions */
1636 	CSR_WRITE_4(sc, BGE_MISC_LOCAL_CTL, BGE_MLC_INTR_ONATTN);
1637 
1638 #ifdef notdef
1639 	/* Assert GPIO pins for PHY reset */
1640 	BGE_SETBIT(sc, BGE_MISC_LOCAL_CTL, BGE_MLC_MISCIO_OUT0|
1641 	    BGE_MLC_MISCIO_OUT1|BGE_MLC_MISCIO_OUT2);
1642 	BGE_SETBIT(sc, BGE_MISC_LOCAL_CTL, BGE_MLC_MISCIO_OUTEN0|
1643 	    BGE_MLC_MISCIO_OUTEN1|BGE_MLC_MISCIO_OUTEN2);
1644 #endif
1645 
1646 	/* Turn on DMA completion state machine */
1647 	if (sc->bge_asicrev != BGE_ASICREV_BCM5705)
1648 		CSR_WRITE_4(sc, BGE_DMAC_MODE, BGE_DMACMODE_ENABLE);
1649 
1650 	/* Turn on write DMA state machine */
1651 	CSR_WRITE_4(sc, BGE_WDMA_MODE,
1652 	    BGE_WDMAMODE_ENABLE|BGE_WDMAMODE_ALL_ATTNS);
1653 
1654 	/* Turn on read DMA state machine */
1655 	CSR_WRITE_4(sc, BGE_RDMA_MODE,
1656 	    BGE_RDMAMODE_ENABLE|BGE_RDMAMODE_ALL_ATTNS);
1657 
1658 	/* Turn on RX data completion state machine */
1659 	CSR_WRITE_4(sc, BGE_RDC_MODE, BGE_RDCMODE_ENABLE);
1660 
1661 	/* Turn on RX BD initiator state machine */
1662 	CSR_WRITE_4(sc, BGE_RBDI_MODE, BGE_RBDIMODE_ENABLE);
1663 
1664 	/* Turn on RX data and RX BD initiator state machine */
1665 	CSR_WRITE_4(sc, BGE_RDBDI_MODE, BGE_RDBDIMODE_ENABLE);
1666 
1667 	/* Turn on Mbuf cluster free state machine */
1668 	if (sc->bge_asicrev != BGE_ASICREV_BCM5705)
1669 		CSR_WRITE_4(sc, BGE_MBCF_MODE, BGE_MBCFMODE_ENABLE);
1670 
1671 	/* Turn on send BD completion state machine */
1672 	CSR_WRITE_4(sc, BGE_SBDC_MODE, BGE_SBDCMODE_ENABLE);
1673 
1674 	/* Turn on send data completion state machine */
1675 	CSR_WRITE_4(sc, BGE_SDC_MODE, BGE_SDCMODE_ENABLE);
1676 
1677 	/* Turn on send data initiator state machine */
1678 	CSR_WRITE_4(sc, BGE_SDI_MODE, BGE_SDIMODE_ENABLE);
1679 
1680 	/* Turn on send BD initiator state machine */
1681 	CSR_WRITE_4(sc, BGE_SBDI_MODE, BGE_SBDIMODE_ENABLE);
1682 
1683 	/* Turn on send BD selector state machine */
1684 	CSR_WRITE_4(sc, BGE_SRS_MODE, BGE_SRSMODE_ENABLE);
1685 
1686 	CSR_WRITE_4(sc, BGE_SDI_STATS_ENABLE_MASK, 0x007FFFFF);
1687 	CSR_WRITE_4(sc, BGE_SDI_STATS_CTL,
1688 	    BGE_SDISTATSCTL_ENABLE|BGE_SDISTATSCTL_FASTER);
1689 
1690 	/* ack/clear link change events */
1691 	CSR_WRITE_4(sc, BGE_MAC_STS, BGE_MACSTAT_SYNC_CHANGED|
1692 	    BGE_MACSTAT_CFG_CHANGED|BGE_MACSTAT_MI_COMPLETE|
1693 	    BGE_MACSTAT_LINK_CHANGED);
1694 	CSR_WRITE_4(sc, BGE_MI_STS, 0);
1695 
1696 	/* Enable PHY auto polling (for MII/GMII only) */
1697 	if (sc->bge_tbi) {
1698 		CSR_WRITE_4(sc, BGE_MI_STS, BGE_MISTS_LINK);
1699  	} else {
1700 		BGE_SETBIT(sc, BGE_MI_MODE, BGE_MIMODE_AUTOPOLL|10<<16);
1701 		if (sc->bge_asicrev == BGE_ASICREV_BCM5700)
1702 			CSR_WRITE_4(sc, BGE_MAC_EVT_ENB,
1703 			    BGE_EVTENB_MI_INTERRUPT);
1704 	}
1705 
1706 	/* Enable link state change attentions. */
1707 	BGE_SETBIT(sc, BGE_MAC_EVT_ENB, BGE_EVTENB_LINK_CHANGED);
1708 
1709 	return(0);
1710 }
1711 
1712 /*
1713  * Probe for a Broadcom chip. Check the PCI vendor and device IDs
1714  * against our list and return its name if we find a match. Note
1715  * that since the Broadcom controller contains VPD support, we
1716  * can get the device name string from the controller itself instead
1717  * of the compiled-in string. This is a little slow, but it guarantees
1718  * we'll always announce the right product name.
1719  */
1720 static int
1721 bge_probe(dev)
1722 	device_t dev;
1723 {
1724 	struct bge_type *t;
1725 	struct bge_softc *sc;
1726 	char *descbuf;
1727 
1728 	t = bge_devs;
1729 
1730 	sc = device_get_softc(dev);
1731 	bzero(sc, sizeof(struct bge_softc));
1732 	sc->bge_unit = device_get_unit(dev);
1733 	sc->bge_dev = dev;
1734 
1735 	while(t->bge_name != NULL) {
1736 		if ((pci_get_vendor(dev) == t->bge_vid) &&
1737 		    (pci_get_device(dev) == t->bge_did)) {
1738 #ifdef notdef
1739 			bge_vpd_read(sc);
1740 			device_set_desc(dev, sc->bge_vpd_prodname);
1741 #endif
1742 			descbuf = malloc(BGE_DEVDESC_MAX, M_TEMP, M_NOWAIT);
1743 			if (descbuf == NULL)
1744 				return(ENOMEM);
1745 			snprintf(descbuf, BGE_DEVDESC_MAX,
1746 			    "%s, ASIC rev. %#04x", t->bge_name,
1747 			    pci_read_config(dev, BGE_PCI_MISC_CTL, 4) >> 16);
1748 			device_set_desc_copy(dev, descbuf);
1749 			if (pci_get_subvendor(dev) == DELL_VENDORID)
1750 				sc->bge_no_3_led = 1;
1751 			free(descbuf, M_TEMP);
1752 			return(0);
1753 		}
1754 		t++;
1755 	}
1756 
1757 	return(ENXIO);
1758 }
1759 
1760 static void
1761 bge_dma_free(sc)
1762 	struct bge_softc *sc;
1763 {
1764 	int i;
1765 
1766 
1767 	/* Destroy DMA maps for RX buffers */
1768 
1769 	for (i = 0; i < BGE_STD_RX_RING_CNT; i++) {
1770 		if (sc->bge_cdata.bge_rx_std_dmamap[i])
1771 			bus_dmamap_destroy(sc->bge_cdata.bge_mtag,
1772 			    sc->bge_cdata.bge_rx_std_dmamap[i]);
1773 	}
1774 
1775 	/* Destroy DMA maps for jumbo RX buffers */
1776 
1777 	for (i = 0; i < BGE_JUMBO_RX_RING_CNT; i++) {
1778 		if (sc->bge_cdata.bge_rx_jumbo_dmamap[i])
1779 			bus_dmamap_destroy(sc->bge_cdata.bge_mtag_jumbo,
1780 			    sc->bge_cdata.bge_rx_jumbo_dmamap[i]);
1781 	}
1782 
1783 	/* Destroy DMA maps for TX buffers */
1784 
1785 	for (i = 0; i < BGE_TX_RING_CNT; i++) {
1786 		if (sc->bge_cdata.bge_tx_dmamap[i])
1787 			bus_dmamap_destroy(sc->bge_cdata.bge_mtag,
1788 			    sc->bge_cdata.bge_tx_dmamap[i]);
1789 	}
1790 
1791 	if (sc->bge_cdata.bge_mtag)
1792 		bus_dma_tag_destroy(sc->bge_cdata.bge_mtag);
1793 
1794 
1795 	/* Destroy standard RX ring */
1796 
1797 	if (sc->bge_ldata.bge_rx_std_ring)
1798 		bus_dmamem_free(sc->bge_cdata.bge_rx_std_ring_tag,
1799 		    sc->bge_ldata.bge_rx_std_ring,
1800 		    sc->bge_cdata.bge_rx_std_ring_map);
1801 
1802 	if (sc->bge_cdata.bge_rx_std_ring_map) {
1803 		bus_dmamap_unload(sc->bge_cdata.bge_rx_std_ring_tag,
1804 		    sc->bge_cdata.bge_rx_std_ring_map);
1805 		bus_dmamap_destroy(sc->bge_cdata.bge_rx_std_ring_tag,
1806 		    sc->bge_cdata.bge_rx_std_ring_map);
1807 	}
1808 
1809 	if (sc->bge_cdata.bge_rx_std_ring_tag)
1810 		bus_dma_tag_destroy(sc->bge_cdata.bge_rx_std_ring_tag);
1811 
1812 	/* Destroy jumbo RX ring */
1813 
1814 	if (sc->bge_ldata.bge_rx_jumbo_ring)
1815 		bus_dmamem_free(sc->bge_cdata.bge_rx_jumbo_ring_tag,
1816 		    sc->bge_ldata.bge_rx_jumbo_ring,
1817 		    sc->bge_cdata.bge_rx_jumbo_ring_map);
1818 
1819 	if (sc->bge_cdata.bge_rx_jumbo_ring_map) {
1820 		bus_dmamap_unload(sc->bge_cdata.bge_rx_jumbo_ring_tag,
1821 		    sc->bge_cdata.bge_rx_jumbo_ring_map);
1822 		bus_dmamap_destroy(sc->bge_cdata.bge_rx_jumbo_ring_tag,
1823 		    sc->bge_cdata.bge_rx_jumbo_ring_map);
1824 	}
1825 
1826 	if (sc->bge_cdata.bge_rx_jumbo_ring_tag)
1827 		bus_dma_tag_destroy(sc->bge_cdata.bge_rx_jumbo_ring_tag);
1828 
1829 	/* Destroy RX return ring */
1830 
1831 	if (sc->bge_ldata.bge_rx_return_ring)
1832 		bus_dmamem_free(sc->bge_cdata.bge_rx_return_ring_tag,
1833 		    sc->bge_ldata.bge_rx_return_ring,
1834 		    sc->bge_cdata.bge_rx_return_ring_map);
1835 
1836 	if (sc->bge_cdata.bge_rx_return_ring_map) {
1837 		bus_dmamap_unload(sc->bge_cdata.bge_rx_return_ring_tag,
1838 		    sc->bge_cdata.bge_rx_return_ring_map);
1839 		bus_dmamap_destroy(sc->bge_cdata.bge_rx_return_ring_tag,
1840 		    sc->bge_cdata.bge_rx_return_ring_map);
1841 	}
1842 
1843 	if (sc->bge_cdata.bge_rx_return_ring_tag)
1844 		bus_dma_tag_destroy(sc->bge_cdata.bge_rx_return_ring_tag);
1845 
1846 	/* Destroy TX ring */
1847 
1848 	if (sc->bge_ldata.bge_tx_ring)
1849 		bus_dmamem_free(sc->bge_cdata.bge_tx_ring_tag,
1850 		    sc->bge_ldata.bge_tx_ring,
1851 		    sc->bge_cdata.bge_tx_ring_map);
1852 
1853 	if (sc->bge_cdata.bge_tx_ring_map) {
1854 		bus_dmamap_unload(sc->bge_cdata.bge_tx_ring_tag,
1855 		    sc->bge_cdata.bge_tx_ring_map);
1856 		bus_dmamap_destroy(sc->bge_cdata.bge_tx_ring_tag,
1857 		    sc->bge_cdata.bge_tx_ring_map);
1858 	}
1859 
1860 	if (sc->bge_cdata.bge_tx_ring_tag)
1861 		bus_dma_tag_destroy(sc->bge_cdata.bge_tx_ring_tag);
1862 
1863 	/* Destroy status block */
1864 
1865 	if (sc->bge_ldata.bge_status_block)
1866 		bus_dmamem_free(sc->bge_cdata.bge_status_tag,
1867 		    sc->bge_ldata.bge_status_block,
1868 		    sc->bge_cdata.bge_status_map);
1869 
1870 	if (sc->bge_cdata.bge_status_map) {
1871 		bus_dmamap_unload(sc->bge_cdata.bge_status_tag,
1872 		    sc->bge_cdata.bge_status_map);
1873 		bus_dmamap_destroy(sc->bge_cdata.bge_status_tag,
1874 		    sc->bge_cdata.bge_status_map);
1875 	}
1876 
1877 	if (sc->bge_cdata.bge_status_tag)
1878 		bus_dma_tag_destroy(sc->bge_cdata.bge_status_tag);
1879 
1880 	/* Destroy statistics block */
1881 
1882 	if (sc->bge_ldata.bge_stats)
1883 		bus_dmamem_free(sc->bge_cdata.bge_stats_tag,
1884 		    sc->bge_ldata.bge_stats,
1885 		    sc->bge_cdata.bge_stats_map);
1886 
1887 	if (sc->bge_cdata.bge_stats_map) {
1888 		bus_dmamap_unload(sc->bge_cdata.bge_stats_tag,
1889 		    sc->bge_cdata.bge_stats_map);
1890 		bus_dmamap_destroy(sc->bge_cdata.bge_stats_tag,
1891 		    sc->bge_cdata.bge_stats_map);
1892 	}
1893 
1894 	if (sc->bge_cdata.bge_stats_tag)
1895 		bus_dma_tag_destroy(sc->bge_cdata.bge_stats_tag);
1896 
1897 	/* Destroy the parent tag */
1898 
1899 	if (sc->bge_cdata.bge_parent_tag)
1900 		bus_dma_tag_destroy(sc->bge_cdata.bge_parent_tag);
1901 
1902 	return;
1903 }
1904 
1905 static int
1906 bge_dma_alloc(dev)
1907 	device_t dev;
1908 {
1909 	struct bge_softc *sc;
1910 	int nseg, i, error;
1911 	struct bge_dmamap_arg ctx;
1912 
1913 	sc = device_get_softc(dev);
1914 
1915 	/*
1916 	 * Allocate the parent bus DMA tag appropriate for PCI.
1917 	 */
1918 #define BGE_NSEG_NEW 32
1919 	error = bus_dma_tag_create(NULL,	/* parent */
1920 			PAGE_SIZE, 0,		/* alignment, boundary */
1921 			BUS_SPACE_MAXADDR,	/* lowaddr */
1922 			BUS_SPACE_MAXADDR_32BIT,/* highaddr */
1923 			NULL, NULL,		/* filter, filterarg */
1924 			MAXBSIZE, BGE_NSEG_NEW,	/* maxsize, nsegments */
1925 			BUS_SPACE_MAXSIZE_32BIT,/* maxsegsize */
1926                         BUS_DMA_ALLOCNOW,	/* flags */
1927 			NULL, NULL,		/* lockfunc, lockarg */
1928 			&sc->bge_cdata.bge_parent_tag);
1929 
1930 	/*
1931 	 * Create tag for RX mbufs.
1932 	 */
1933 	nseg = 32;
1934 	error = bus_dma_tag_create(sc->bge_cdata.bge_parent_tag, ETHER_ALIGN,
1935 	    0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL,
1936 	    NULL, MCLBYTES * nseg, nseg, MCLBYTES, 0, NULL, NULL,
1937 	    &sc->bge_cdata.bge_mtag);
1938 
1939 	if (error) {
1940 		device_printf(dev, "could not allocate dma tag\n");
1941 		return (ENOMEM);
1942 	}
1943 
1944 	/* Create DMA maps for RX buffers */
1945 
1946 	for (i = 0; i < BGE_STD_RX_RING_CNT; i++) {
1947 		error = bus_dmamap_create(sc->bge_cdata.bge_mtag, 0,
1948 			    &sc->bge_cdata.bge_rx_std_dmamap[i]);
1949 		if (error) {
1950 			device_printf(dev, "can't create DMA map for RX\n");
1951 			return(ENOMEM);
1952 		}
1953 	}
1954 
1955 	/* Create DMA maps for TX buffers */
1956 
1957 	for (i = 0; i < BGE_TX_RING_CNT; i++) {
1958 		error = bus_dmamap_create(sc->bge_cdata.bge_mtag, 0,
1959 			    &sc->bge_cdata.bge_tx_dmamap[i]);
1960 		if (error) {
1961 			device_printf(dev, "can't create DMA map for RX\n");
1962 			return(ENOMEM);
1963 		}
1964 	}
1965 
1966 	/* Create tag for standard RX ring */
1967 
1968 	error = bus_dma_tag_create(sc->bge_cdata.bge_parent_tag,
1969 	    PAGE_SIZE, 0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL,
1970 	    NULL, BGE_STD_RX_RING_SZ, 1, BGE_STD_RX_RING_SZ, 0,
1971 	    NULL, NULL, &sc->bge_cdata.bge_rx_std_ring_tag);
1972 
1973 	if (error) {
1974 		device_printf(dev, "could not allocate dma tag\n");
1975 		return (ENOMEM);
1976 	}
1977 
1978 	/* Allocate DMA'able memory for standard RX ring */
1979 
1980 	error = bus_dmamem_alloc(sc->bge_cdata.bge_rx_std_ring_tag,
1981 	    (void **)&sc->bge_ldata.bge_rx_std_ring, BUS_DMA_NOWAIT,
1982 	    &sc->bge_cdata.bge_rx_std_ring_map);
1983         if (error)
1984                 return (ENOMEM);
1985 
1986         bzero((char *)sc->bge_ldata.bge_rx_std_ring, BGE_STD_RX_RING_SZ);
1987 
1988 	/* Load the address of the standard RX ring */
1989 
1990 	ctx.bge_maxsegs = 1;
1991 	ctx.sc = sc;
1992 
1993 	error = bus_dmamap_load(sc->bge_cdata.bge_rx_std_ring_tag,
1994 	    sc->bge_cdata.bge_rx_std_ring_map, sc->bge_ldata.bge_rx_std_ring,
1995 	    BGE_STD_RX_RING_SZ, bge_dma_map_addr, &ctx, BUS_DMA_NOWAIT);
1996 
1997 	if (error)
1998 		return (ENOMEM);
1999 
2000 	sc->bge_ldata.bge_rx_std_ring_paddr = ctx.bge_busaddr;
2001 
2002 	if (sc->bge_asicrev != BGE_ASICREV_BCM5705) {
2003 
2004 		/*
2005 		 * Create tag for jumbo mbufs.
2006 		 * This is really a bit of a kludge. We allocate a special
2007 		 * jumbo buffer pool which (thanks to the way our DMA
2008 		 * memory allocation works) will consist of contiguous
2009 		 * pages. This means that even though a jumbo buffer might
2010 		 * be larger than a page size, we don't really need to
2011 		 * map it into more than one DMA segment. However, the
2012 		 * default mbuf tag will result in multi-segment mappings,
2013 		 * so we have to create a special jumbo mbuf tag that
2014 		 * lets us get away with mapping the jumbo buffers as
2015 		 * a single segment. I think eventually the driver should
2016 		 * be changed so that it uses ordinary mbufs and cluster
2017 		 * buffers, i.e. jumbo frames can span multiple DMA
2018 		 * descriptors. But that's a project for another day.
2019 		 */
2020 
2021 		error = bus_dma_tag_create(sc->bge_cdata.bge_parent_tag,
2022 		    ETHER_ALIGN, 0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL,
2023 		    NULL, MCLBYTES * nseg, nseg, BGE_JLEN, 0, NULL, NULL,
2024 		    &sc->bge_cdata.bge_mtag_jumbo);
2025 
2026 		if (error) {
2027 			device_printf(dev, "could not allocate dma tag\n");
2028 			return (ENOMEM);
2029 		}
2030 
2031 		/* Create tag for jumbo RX ring */
2032 
2033 		error = bus_dma_tag_create(sc->bge_cdata.bge_parent_tag,
2034 		    PAGE_SIZE, 0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL,
2035 		    NULL, BGE_JUMBO_RX_RING_SZ, 1, BGE_JUMBO_RX_RING_SZ, 0,
2036 		    NULL, NULL, &sc->bge_cdata.bge_rx_jumbo_ring_tag);
2037 
2038 		if (error) {
2039 			device_printf(dev, "could not allocate dma tag\n");
2040 			return (ENOMEM);
2041 		}
2042 
2043 		/* Allocate DMA'able memory for jumbo RX ring */
2044 
2045 		error = bus_dmamem_alloc(sc->bge_cdata.bge_rx_jumbo_ring_tag,
2046 		    (void **)&sc->bge_ldata.bge_rx_jumbo_ring, BUS_DMA_NOWAIT,
2047 		    &sc->bge_cdata.bge_rx_jumbo_ring_map);
2048 		if (error)
2049 			return (ENOMEM);
2050 
2051 		bzero((char *)sc->bge_ldata.bge_rx_jumbo_ring,
2052 		    BGE_JUMBO_RX_RING_SZ);
2053 
2054 		/* Load the address of the jumbo RX ring */
2055 
2056 		ctx.bge_maxsegs = 1;
2057 		ctx.sc = sc;
2058 
2059 		error = bus_dmamap_load(sc->bge_cdata.bge_rx_jumbo_ring_tag,
2060 		    sc->bge_cdata.bge_rx_jumbo_ring_map,
2061 		    sc->bge_ldata.bge_rx_jumbo_ring, BGE_JUMBO_RX_RING_SZ,
2062 		    bge_dma_map_addr, &ctx, BUS_DMA_NOWAIT);
2063 
2064 		if (error)
2065 			return (ENOMEM);
2066 
2067 		sc->bge_ldata.bge_rx_jumbo_ring_paddr = ctx.bge_busaddr;
2068 
2069 		/* Create DMA maps for jumbo RX buffers */
2070 
2071 		for (i = 0; i < BGE_JUMBO_RX_RING_CNT; i++) {
2072 			error = bus_dmamap_create(sc->bge_cdata.bge_mtag_jumbo,
2073 				    0, &sc->bge_cdata.bge_rx_jumbo_dmamap[i]);
2074 			if (error) {
2075 				device_printf(dev,
2076 				    "can't create DMA map for RX\n");
2077 				return(ENOMEM);
2078 			}
2079 		}
2080 
2081 	}
2082 
2083 	/* Create tag for RX return ring */
2084 
2085 	error = bus_dma_tag_create(sc->bge_cdata.bge_parent_tag,
2086 	    PAGE_SIZE, 0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL,
2087 	    NULL, BGE_RX_RTN_RING_SZ(sc), 1, BGE_RX_RTN_RING_SZ(sc), 0,
2088 	    NULL, NULL, &sc->bge_cdata.bge_rx_return_ring_tag);
2089 
2090 	if (error) {
2091 		device_printf(dev, "could not allocate dma tag\n");
2092 		return (ENOMEM);
2093 	}
2094 
2095 	/* Allocate DMA'able memory for RX return ring */
2096 
2097 	error = bus_dmamem_alloc(sc->bge_cdata.bge_rx_return_ring_tag,
2098 	    (void **)&sc->bge_ldata.bge_rx_return_ring, BUS_DMA_NOWAIT,
2099 	    &sc->bge_cdata.bge_rx_return_ring_map);
2100         if (error)
2101                 return (ENOMEM);
2102 
2103         bzero((char *)sc->bge_ldata.bge_rx_return_ring,
2104 	    BGE_RX_RTN_RING_SZ(sc));
2105 
2106 	/* Load the address of the RX return ring */
2107 
2108 	ctx.bge_maxsegs = 1;
2109 	ctx.sc = sc;
2110 
2111 	error = bus_dmamap_load(sc->bge_cdata.bge_rx_return_ring_tag,
2112 	    sc->bge_cdata.bge_rx_return_ring_map,
2113 	    sc->bge_ldata.bge_rx_return_ring, BGE_RX_RTN_RING_SZ(sc),
2114 	    bge_dma_map_addr, &ctx, BUS_DMA_NOWAIT);
2115 
2116 	if (error)
2117 		return (ENOMEM);
2118 
2119 	sc->bge_ldata.bge_rx_return_ring_paddr = ctx.bge_busaddr;
2120 
2121 	/* Create tag for TX ring */
2122 
2123 	error = bus_dma_tag_create(sc->bge_cdata.bge_parent_tag,
2124 	    PAGE_SIZE, 0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL,
2125 	    NULL, BGE_TX_RING_SZ, 1, BGE_TX_RING_SZ, 0, NULL, NULL,
2126 	    &sc->bge_cdata.bge_tx_ring_tag);
2127 
2128 	if (error) {
2129 		device_printf(dev, "could not allocate dma tag\n");
2130 		return (ENOMEM);
2131 	}
2132 
2133 	/* Allocate DMA'able memory for TX ring */
2134 
2135 	error = bus_dmamem_alloc(sc->bge_cdata.bge_tx_ring_tag,
2136 	    (void **)&sc->bge_ldata.bge_tx_ring, BUS_DMA_NOWAIT,
2137 	    &sc->bge_cdata.bge_tx_ring_map);
2138         if (error)
2139                 return (ENOMEM);
2140 
2141         bzero((char *)sc->bge_ldata.bge_tx_ring, BGE_TX_RING_SZ);
2142 
2143 	/* Load the address of the TX ring */
2144 
2145 	ctx.bge_maxsegs = 1;
2146 	ctx.sc = sc;
2147 
2148 	error = bus_dmamap_load(sc->bge_cdata.bge_tx_ring_tag,
2149 	    sc->bge_cdata.bge_tx_ring_map, sc->bge_ldata.bge_tx_ring,
2150 	    BGE_TX_RING_SZ, bge_dma_map_addr, &ctx, BUS_DMA_NOWAIT);
2151 
2152 	if (error)
2153 		return (ENOMEM);
2154 
2155 	sc->bge_ldata.bge_tx_ring_paddr = ctx.bge_busaddr;
2156 
2157 	/* Create tag for status block */
2158 
2159 	error = bus_dma_tag_create(sc->bge_cdata.bge_parent_tag,
2160 	    PAGE_SIZE, 0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL,
2161 	    NULL, BGE_STATUS_BLK_SZ, 1, BGE_STATUS_BLK_SZ, 0,
2162 	    NULL, NULL, &sc->bge_cdata.bge_status_tag);
2163 
2164 	if (error) {
2165 		device_printf(dev, "could not allocate dma tag\n");
2166 		return (ENOMEM);
2167 	}
2168 
2169 	/* Allocate DMA'able memory for status block */
2170 
2171 	error = bus_dmamem_alloc(sc->bge_cdata.bge_status_tag,
2172 	    (void **)&sc->bge_ldata.bge_status_block, BUS_DMA_NOWAIT,
2173 	    &sc->bge_cdata.bge_status_map);
2174         if (error)
2175                 return (ENOMEM);
2176 
2177         bzero((char *)sc->bge_ldata.bge_status_block, BGE_STATUS_BLK_SZ);
2178 
2179 	/* Load the address of the status block */
2180 
2181 	ctx.sc = sc;
2182 	ctx.bge_maxsegs = 1;
2183 
2184 	error = bus_dmamap_load(sc->bge_cdata.bge_status_tag,
2185 	    sc->bge_cdata.bge_status_map, sc->bge_ldata.bge_status_block,
2186 	    BGE_STATUS_BLK_SZ, bge_dma_map_addr, &ctx, BUS_DMA_NOWAIT);
2187 
2188 	if (error)
2189 		return (ENOMEM);
2190 
2191 	sc->bge_ldata.bge_status_block_paddr = ctx.bge_busaddr;
2192 
2193 	/* Create tag for statistics block */
2194 
2195 	error = bus_dma_tag_create(sc->bge_cdata.bge_parent_tag,
2196 	    PAGE_SIZE, 0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL,
2197 	    NULL, BGE_STATS_SZ, 1, BGE_STATS_SZ, 0, NULL, NULL,
2198 	    &sc->bge_cdata.bge_stats_tag);
2199 
2200 	if (error) {
2201 		device_printf(dev, "could not allocate dma tag\n");
2202 		return (ENOMEM);
2203 	}
2204 
2205 	/* Allocate DMA'able memory for statistics block */
2206 
2207 	error = bus_dmamem_alloc(sc->bge_cdata.bge_stats_tag,
2208 	    (void **)&sc->bge_ldata.bge_stats, BUS_DMA_NOWAIT,
2209 	    &sc->bge_cdata.bge_stats_map);
2210         if (error)
2211                 return (ENOMEM);
2212 
2213         bzero((char *)sc->bge_ldata.bge_stats, BGE_STATS_SZ);
2214 
2215 	/* Load the address of the statstics block */
2216 
2217 	ctx.sc = sc;
2218 	ctx.bge_maxsegs = 1;
2219 
2220 	error = bus_dmamap_load(sc->bge_cdata.bge_stats_tag,
2221 	    sc->bge_cdata.bge_stats_map, sc->bge_ldata.bge_stats,
2222 	    BGE_STATS_SZ, bge_dma_map_addr, &ctx, BUS_DMA_NOWAIT);
2223 
2224 	if (error)
2225 		return (ENOMEM);
2226 
2227 	sc->bge_ldata.bge_stats_paddr = ctx.bge_busaddr;
2228 
2229 	return(0);
2230 }
2231 
2232 static int
2233 bge_attach(dev)
2234 	device_t dev;
2235 {
2236 	struct ifnet *ifp;
2237 	struct bge_softc *sc;
2238 	u_int32_t hwcfg = 0;
2239 	u_int32_t mac_addr = 0;
2240 	int unit, error = 0, rid;
2241 
2242 	sc = device_get_softc(dev);
2243 	unit = device_get_unit(dev);
2244 	sc->bge_dev = dev;
2245 	sc->bge_unit = unit;
2246 
2247 	/*
2248 	 * Map control/status registers.
2249 	 */
2250 	pci_enable_busmaster(dev);
2251 
2252 	rid = BGE_PCI_BAR0;
2253 	sc->bge_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
2254 	    RF_ACTIVE|PCI_RF_DENSE);
2255 
2256 	if (sc->bge_res == NULL) {
2257 		printf ("bge%d: couldn't map memory\n", unit);
2258 		error = ENXIO;
2259 		goto fail;
2260 	}
2261 
2262 	sc->bge_btag = rman_get_bustag(sc->bge_res);
2263 	sc->bge_bhandle = rman_get_bushandle(sc->bge_res);
2264 	sc->bge_vhandle = (vm_offset_t)rman_get_virtual(sc->bge_res);
2265 
2266 	/* Allocate interrupt */
2267 	rid = 0;
2268 
2269 	sc->bge_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
2270 	    RF_SHAREABLE | RF_ACTIVE);
2271 
2272 	if (sc->bge_irq == NULL) {
2273 		printf("bge%d: couldn't map interrupt\n", unit);
2274 		error = ENXIO;
2275 		goto fail;
2276 	}
2277 
2278 	sc->bge_unit = unit;
2279 
2280 	BGE_LOCK_INIT(sc, device_get_nameunit(dev));
2281 
2282 	/* Try to reset the chip. */
2283 	bge_reset(sc);
2284 
2285 	if (bge_chipinit(sc)) {
2286 		printf("bge%d: chip initialization failed\n", sc->bge_unit);
2287 		bge_release_resources(sc);
2288 		error = ENXIO;
2289 		goto fail;
2290 	}
2291 
2292 	/*
2293 	 * Get station address from the EEPROM.
2294 	 */
2295 	mac_addr = bge_readmem_ind(sc, 0x0c14);
2296 	if ((mac_addr >> 16) == 0x484b) {
2297 		sc->arpcom.ac_enaddr[0] = (u_char)(mac_addr >> 8);
2298 		sc->arpcom.ac_enaddr[1] = (u_char)mac_addr;
2299 		mac_addr = bge_readmem_ind(sc, 0x0c18);
2300 		sc->arpcom.ac_enaddr[2] = (u_char)(mac_addr >> 24);
2301 		sc->arpcom.ac_enaddr[3] = (u_char)(mac_addr >> 16);
2302 		sc->arpcom.ac_enaddr[4] = (u_char)(mac_addr >> 8);
2303 		sc->arpcom.ac_enaddr[5] = (u_char)mac_addr;
2304 	} else if (bge_read_eeprom(sc, (caddr_t)&sc->arpcom.ac_enaddr,
2305 	    BGE_EE_MAC_OFFSET + 2, ETHER_ADDR_LEN)) {
2306 		printf("bge%d: failed to read station address\n", unit);
2307 		bge_release_resources(sc);
2308 		error = ENXIO;
2309 		goto fail;
2310 	}
2311 
2312 	/* Save ASIC rev. */
2313 
2314 	sc->bge_chipid =
2315 	    pci_read_config(dev, BGE_PCI_MISC_CTL, 4) &
2316 	    BGE_PCIMISCCTL_ASICREV;
2317 	sc->bge_asicrev = BGE_ASICREV(sc->bge_chipid);
2318 	sc->bge_chiprev = BGE_CHIPREV(sc->bge_chipid);
2319 
2320 	/* 5705 limits RX return ring to 512 entries. */
2321 	if (sc->bge_asicrev == BGE_ASICREV_BCM5705)
2322 		sc->bge_return_ring_cnt = BGE_RETURN_RING_CNT_5705;
2323 	else
2324 		sc->bge_return_ring_cnt = BGE_RETURN_RING_CNT;
2325 
2326 	if (bge_dma_alloc(dev)) {
2327 		printf ("bge%d: failed to allocate DMA resources\n",
2328 		    sc->bge_unit);
2329 		bge_release_resources(sc);
2330 		error = ENXIO;
2331 		goto fail;
2332 	}
2333 
2334 	/*
2335 	 * Try to allocate memory for jumbo buffers.
2336 	 * The 5705 does not appear to support jumbo frames.
2337 	 */
2338 	if (sc->bge_asicrev != BGE_ASICREV_BCM5705) {
2339 		if (bge_alloc_jumbo_mem(sc)) {
2340 			printf("bge%d: jumbo buffer allocation "
2341 			    "failed\n", sc->bge_unit);
2342 			bge_release_resources(sc);
2343 			error = ENXIO;
2344 			goto fail;
2345 		}
2346 	}
2347 
2348 	/* Set default tuneable values. */
2349 	sc->bge_stat_ticks = BGE_TICKS_PER_SEC;
2350 	sc->bge_rx_coal_ticks = 150;
2351 	sc->bge_tx_coal_ticks = 150;
2352 	sc->bge_rx_max_coal_bds = 64;
2353 	sc->bge_tx_max_coal_bds = 128;
2354 
2355 	/* Set up ifnet structure */
2356 	ifp = &sc->arpcom.ac_if;
2357 	ifp->if_softc = sc;
2358 	if_initname(ifp, device_get_name(dev), device_get_unit(dev));
2359 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
2360 	ifp->if_ioctl = bge_ioctl;
2361 	ifp->if_output = ether_output;
2362 	ifp->if_start = bge_start;
2363 	ifp->if_watchdog = bge_watchdog;
2364 	ifp->if_init = bge_init;
2365 	ifp->if_mtu = ETHERMTU;
2366 	ifp->if_snd.ifq_maxlen = BGE_TX_RING_CNT - 1;
2367 	ifp->if_hwassist = BGE_CSUM_FEATURES;
2368 	ifp->if_capabilities = IFCAP_HWCSUM | IFCAP_VLAN_HWTAGGING |
2369 	    IFCAP_VLAN_MTU;
2370 	ifp->if_capenable = ifp->if_capabilities;
2371 
2372 	/*
2373 	 * Figure out what sort of media we have by checking the
2374 	 * hardware config word in the first 32k of NIC internal memory,
2375 	 * or fall back to examining the EEPROM if necessary.
2376 	 * Note: on some BCM5700 cards, this value appears to be unset.
2377 	 * If that's the case, we have to rely on identifying the NIC
2378 	 * by its PCI subsystem ID, as we do below for the SysKonnect
2379 	 * SK-9D41.
2380 	 */
2381 	if (bge_readmem_ind(sc, BGE_SOFTWARE_GENCOMM_SIG) == BGE_MAGIC_NUMBER)
2382 		hwcfg = bge_readmem_ind(sc, BGE_SOFTWARE_GENCOMM_NICCFG);
2383 	else {
2384 		bge_read_eeprom(sc, (caddr_t)&hwcfg,
2385 				BGE_EE_HWCFG_OFFSET, sizeof(hwcfg));
2386 		hwcfg = ntohl(hwcfg);
2387 	}
2388 
2389 	if ((hwcfg & BGE_HWCFG_MEDIA) == BGE_MEDIA_FIBER)
2390 		sc->bge_tbi = 1;
2391 
2392 	/* The SysKonnect SK-9D41 is a 1000baseSX card. */
2393 	if ((pci_read_config(dev, BGE_PCI_SUBSYS, 4) >> 16) == SK_SUBSYSID_9D41)
2394 		sc->bge_tbi = 1;
2395 
2396 	if (sc->bge_tbi) {
2397 		ifmedia_init(&sc->bge_ifmedia, IFM_IMASK,
2398 		    bge_ifmedia_upd, bge_ifmedia_sts);
2399 		ifmedia_add(&sc->bge_ifmedia, IFM_ETHER|IFM_1000_SX, 0, NULL);
2400 		ifmedia_add(&sc->bge_ifmedia,
2401 		    IFM_ETHER|IFM_1000_SX|IFM_FDX, 0, NULL);
2402 		ifmedia_add(&sc->bge_ifmedia, IFM_ETHER|IFM_AUTO, 0, NULL);
2403 		ifmedia_set(&sc->bge_ifmedia, IFM_ETHER|IFM_AUTO);
2404 	} else {
2405 		/*
2406 		 * Do transceiver setup.
2407 		 */
2408 		if (mii_phy_probe(dev, &sc->bge_miibus,
2409 		    bge_ifmedia_upd, bge_ifmedia_sts)) {
2410 			printf("bge%d: MII without any PHY!\n", sc->bge_unit);
2411 			bge_release_resources(sc);
2412 			bge_free_jumbo_mem(sc);
2413 			error = ENXIO;
2414 			goto fail;
2415 		}
2416 	}
2417 
2418 	/*
2419 	 * When using the BCM5701 in PCI-X mode, data corruption has
2420 	 * been observed in the first few bytes of some received packets.
2421 	 * Aligning the packet buffer in memory eliminates the corruption.
2422 	 * Unfortunately, this misaligns the packet payloads.  On platforms
2423 	 * which do not support unaligned accesses, we will realign the
2424 	 * payloads by copying the received packets.
2425 	 */
2426 	switch (sc->bge_chipid) {
2427 	case BGE_CHIPID_BCM5701_A0:
2428 	case BGE_CHIPID_BCM5701_B0:
2429 	case BGE_CHIPID_BCM5701_B2:
2430 	case BGE_CHIPID_BCM5701_B5:
2431 		/* If in PCI-X mode, work around the alignment bug. */
2432 		if ((pci_read_config(dev, BGE_PCI_PCISTATE, 4) &
2433 		    (BGE_PCISTATE_PCI_BUSMODE | BGE_PCISTATE_PCI_BUSSPEED)) ==
2434 		    BGE_PCISTATE_PCI_BUSSPEED)
2435 			sc->bge_rx_alignment_bug = 1;
2436 		break;
2437 	}
2438 
2439 	/*
2440 	 * Call MI attach routine.
2441 	 */
2442 	ether_ifattach(ifp, sc->arpcom.ac_enaddr);
2443 	callout_init(&sc->bge_stat_ch, CALLOUT_MPSAFE);
2444 
2445 	/*
2446 	 * Hookup IRQ last.
2447 	 */
2448 	error = bus_setup_intr(dev, sc->bge_irq, INTR_TYPE_NET | INTR_MPSAFE,
2449 	   bge_intr, sc, &sc->bge_intrhand);
2450 
2451 	if (error) {
2452 		bge_release_resources(sc);
2453 		printf("bge%d: couldn't set up irq\n", unit);
2454 	}
2455 
2456 fail:
2457 	return(error);
2458 }
2459 
2460 static int
2461 bge_detach(dev)
2462 	device_t dev;
2463 {
2464 	struct bge_softc *sc;
2465 	struct ifnet *ifp;
2466 
2467 	sc = device_get_softc(dev);
2468 	ifp = &sc->arpcom.ac_if;
2469 
2470 	BGE_LOCK(sc);
2471 	bge_stop(sc);
2472 	bge_reset(sc);
2473 	BGE_UNLOCK(sc);
2474 
2475 	ether_ifdetach(ifp);
2476 
2477 	if (sc->bge_tbi) {
2478 		ifmedia_removeall(&sc->bge_ifmedia);
2479 	} else {
2480 		bus_generic_detach(dev);
2481 		device_delete_child(dev, sc->bge_miibus);
2482 	}
2483 
2484 	bge_release_resources(sc);
2485 	if (sc->bge_asicrev != BGE_ASICREV_BCM5705)
2486 		bge_free_jumbo_mem(sc);
2487 
2488 	return(0);
2489 }
2490 
2491 static void
2492 bge_release_resources(sc)
2493 	struct bge_softc *sc;
2494 {
2495         device_t dev;
2496 
2497         dev = sc->bge_dev;
2498 
2499 	if (sc->bge_vpd_prodname != NULL)
2500 		free(sc->bge_vpd_prodname, M_DEVBUF);
2501 
2502 	if (sc->bge_vpd_readonly != NULL)
2503 		free(sc->bge_vpd_readonly, M_DEVBUF);
2504 
2505         if (sc->bge_intrhand != NULL)
2506                 bus_teardown_intr(dev, sc->bge_irq, sc->bge_intrhand);
2507 
2508         if (sc->bge_irq != NULL)
2509 		bus_release_resource(dev, SYS_RES_IRQ, 0, sc->bge_irq);
2510 
2511         if (sc->bge_res != NULL)
2512 		bus_release_resource(dev, SYS_RES_MEMORY,
2513 		    BGE_PCI_BAR0, sc->bge_res);
2514 
2515 	bge_dma_free(sc);
2516 
2517 	if (mtx_initialized(&sc->bge_mtx))	/* XXX */
2518 		BGE_LOCK_DESTROY(sc);
2519 
2520         return;
2521 }
2522 
2523 static void
2524 bge_reset(sc)
2525 	struct bge_softc *sc;
2526 {
2527 	device_t dev;
2528 	u_int32_t cachesize, command, pcistate;
2529 	int i, val = 0;
2530 
2531 	dev = sc->bge_dev;
2532 
2533 	/* Save some important PCI state. */
2534 	cachesize = pci_read_config(dev, BGE_PCI_CACHESZ, 4);
2535 	command = pci_read_config(dev, BGE_PCI_CMD, 4);
2536 	pcistate = pci_read_config(dev, BGE_PCI_PCISTATE, 4);
2537 
2538 	pci_write_config(dev, BGE_PCI_MISC_CTL,
2539 	    BGE_PCIMISCCTL_INDIRECT_ACCESS|BGE_PCIMISCCTL_MASK_PCI_INTR|
2540 	    BGE_PCIMISCCTL_ENDIAN_WORDSWAP|BGE_PCIMISCCTL_PCISTATE_RW, 4);
2541 
2542 	/* Issue global reset */
2543 	bge_writereg_ind(sc, BGE_MISC_CFG,
2544 	    BGE_MISCCFG_RESET_CORE_CLOCKS|(65<<1));
2545 
2546 	DELAY(1000);
2547 
2548 	/* Reset some of the PCI state that got zapped by reset */
2549 	pci_write_config(dev, BGE_PCI_MISC_CTL,
2550 	    BGE_PCIMISCCTL_INDIRECT_ACCESS|BGE_PCIMISCCTL_MASK_PCI_INTR|
2551 	    BGE_PCIMISCCTL_ENDIAN_WORDSWAP|BGE_PCIMISCCTL_PCISTATE_RW, 4);
2552 	pci_write_config(dev, BGE_PCI_CACHESZ, cachesize, 4);
2553 	pci_write_config(dev, BGE_PCI_CMD, command, 4);
2554 	bge_writereg_ind(sc, BGE_MISC_CFG, (65 << 1));
2555 
2556 	/* Enable memory arbiter. */
2557 	if (sc->bge_asicrev != BGE_ASICREV_BCM5705)
2558 		CSR_WRITE_4(sc, BGE_MARB_MODE, BGE_MARBMODE_ENABLE);
2559 
2560 	/*
2561 	 * Prevent PXE restart: write a magic number to the
2562 	 * general communications memory at 0xB50.
2563 	 */
2564 	bge_writemem_ind(sc, BGE_SOFTWARE_GENCOMM, BGE_MAGIC_NUMBER);
2565 	/*
2566 	 * Poll the value location we just wrote until
2567 	 * we see the 1's complement of the magic number.
2568 	 * This indicates that the firmware initialization
2569 	 * is complete.
2570 	 */
2571 	for (i = 0; i < BGE_TIMEOUT; i++) {
2572 		val = bge_readmem_ind(sc, BGE_SOFTWARE_GENCOMM);
2573 		if (val == ~BGE_MAGIC_NUMBER)
2574 			break;
2575 		DELAY(10);
2576 	}
2577 
2578 	if (i == BGE_TIMEOUT) {
2579 		printf("bge%d: firmware handshake timed out\n", sc->bge_unit);
2580 		return;
2581 	}
2582 
2583 	/*
2584 	 * XXX Wait for the value of the PCISTATE register to
2585 	 * return to its original pre-reset state. This is a
2586 	 * fairly good indicator of reset completion. If we don't
2587 	 * wait for the reset to fully complete, trying to read
2588 	 * from the device's non-PCI registers may yield garbage
2589 	 * results.
2590 	 */
2591 	for (i = 0; i < BGE_TIMEOUT; i++) {
2592 		if (pci_read_config(dev, BGE_PCI_PCISTATE, 4) == pcistate)
2593 			break;
2594 		DELAY(10);
2595 	}
2596 
2597 	/* Fix up byte swapping */
2598 	CSR_WRITE_4(sc, BGE_MODE_CTL, BGE_MODECTL_BYTESWAP_NONFRAME|
2599 	    BGE_MODECTL_BYTESWAP_DATA);
2600 
2601 	CSR_WRITE_4(sc, BGE_MAC_MODE, 0);
2602 
2603 	DELAY(10000);
2604 
2605 	return;
2606 }
2607 
2608 /*
2609  * Frame reception handling. This is called if there's a frame
2610  * on the receive return list.
2611  *
2612  * Note: we have to be able to handle two possibilities here:
2613  * 1) the frame is from the jumbo recieve ring
2614  * 2) the frame is from the standard receive ring
2615  */
2616 
2617 static void
2618 bge_rxeof(sc)
2619 	struct bge_softc *sc;
2620 {
2621 	struct ifnet *ifp;
2622 	int stdcnt = 0, jumbocnt = 0;
2623 
2624 	BGE_LOCK_ASSERT(sc);
2625 
2626 	ifp = &sc->arpcom.ac_if;
2627 
2628 	bus_dmamap_sync(sc->bge_cdata.bge_rx_return_ring_tag,
2629 	    sc->bge_cdata.bge_rx_return_ring_map, BUS_DMASYNC_POSTWRITE);
2630 	bus_dmamap_sync(sc->bge_cdata.bge_rx_std_ring_tag,
2631 	    sc->bge_cdata.bge_rx_std_ring_map, BUS_DMASYNC_POSTREAD);
2632 	if (sc->bge_asicrev != BGE_ASICREV_BCM5705) {
2633 		bus_dmamap_sync(sc->bge_cdata.bge_rx_jumbo_ring_tag,
2634 		    sc->bge_cdata.bge_rx_jumbo_ring_map,
2635 		    BUS_DMASYNC_POSTREAD);
2636 	}
2637 
2638 	while(sc->bge_rx_saved_considx !=
2639 	    sc->bge_ldata.bge_status_block->bge_idx[0].bge_rx_prod_idx) {
2640 		struct bge_rx_bd	*cur_rx;
2641 		u_int32_t		rxidx;
2642 		struct ether_header	*eh;
2643 		struct mbuf		*m = NULL;
2644 		u_int16_t		vlan_tag = 0;
2645 		int			have_tag = 0;
2646 
2647 		cur_rx =
2648 	    &sc->bge_ldata.bge_rx_return_ring[sc->bge_rx_saved_considx];
2649 
2650 		rxidx = cur_rx->bge_idx;
2651 		BGE_INC(sc->bge_rx_saved_considx, sc->bge_return_ring_cnt);
2652 
2653 		if (cur_rx->bge_flags & BGE_RXBDFLAG_VLAN_TAG) {
2654 			have_tag = 1;
2655 			vlan_tag = cur_rx->bge_vlan_tag;
2656 		}
2657 
2658 		if (cur_rx->bge_flags & BGE_RXBDFLAG_JUMBO_RING) {
2659 			BGE_INC(sc->bge_jumbo, BGE_JUMBO_RX_RING_CNT);
2660 			bus_dmamap_sync(sc->bge_cdata.bge_mtag_jumbo,
2661 			    sc->bge_cdata.bge_rx_jumbo_dmamap[rxidx],
2662 			    BUS_DMASYNC_POSTREAD);
2663 			bus_dmamap_unload(sc->bge_cdata.bge_mtag_jumbo,
2664 			    sc->bge_cdata.bge_rx_jumbo_dmamap[rxidx]);
2665 			m = sc->bge_cdata.bge_rx_jumbo_chain[rxidx];
2666 			sc->bge_cdata.bge_rx_jumbo_chain[rxidx] = NULL;
2667 			jumbocnt++;
2668 			if (cur_rx->bge_flags & BGE_RXBDFLAG_ERROR) {
2669 				ifp->if_ierrors++;
2670 				bge_newbuf_jumbo(sc, sc->bge_jumbo, m);
2671 				continue;
2672 			}
2673 			if (bge_newbuf_jumbo(sc,
2674 			    sc->bge_jumbo, NULL) == ENOBUFS) {
2675 				ifp->if_ierrors++;
2676 				bge_newbuf_jumbo(sc, sc->bge_jumbo, m);
2677 				continue;
2678 			}
2679 		} else {
2680 			BGE_INC(sc->bge_std, BGE_STD_RX_RING_CNT);
2681 			bus_dmamap_sync(sc->bge_cdata.bge_mtag,
2682 			    sc->bge_cdata.bge_rx_std_dmamap[rxidx],
2683 			    BUS_DMASYNC_POSTREAD);
2684 			bus_dmamap_unload(sc->bge_cdata.bge_mtag,
2685 			    sc->bge_cdata.bge_rx_std_dmamap[rxidx]);
2686 			m = sc->bge_cdata.bge_rx_std_chain[rxidx];
2687 			sc->bge_cdata.bge_rx_std_chain[rxidx] = NULL;
2688 			stdcnt++;
2689 			if (cur_rx->bge_flags & BGE_RXBDFLAG_ERROR) {
2690 				ifp->if_ierrors++;
2691 				bge_newbuf_std(sc, sc->bge_std, m);
2692 				continue;
2693 			}
2694 			if (bge_newbuf_std(sc, sc->bge_std,
2695 			    NULL) == ENOBUFS) {
2696 				ifp->if_ierrors++;
2697 				bge_newbuf_std(sc, sc->bge_std, m);
2698 				continue;
2699 			}
2700 		}
2701 
2702 		ifp->if_ipackets++;
2703 #ifndef __i386__
2704 		/*
2705 		 * The i386 allows unaligned accesses, but for other
2706 		 * platforms we must make sure the payload is aligned.
2707 		 */
2708 		if (sc->bge_rx_alignment_bug) {
2709 			bcopy(m->m_data, m->m_data + ETHER_ALIGN,
2710 			    cur_rx->bge_len);
2711 			m->m_data += ETHER_ALIGN;
2712 		}
2713 #endif
2714 		eh = mtod(m, struct ether_header *);
2715 		m->m_pkthdr.len = m->m_len = cur_rx->bge_len - ETHER_CRC_LEN;
2716 		m->m_pkthdr.rcvif = ifp;
2717 
2718 #if 0 /* currently broken for some packets, possibly related to TCP options */
2719 		if (ifp->if_hwassist) {
2720 			m->m_pkthdr.csum_flags |= CSUM_IP_CHECKED;
2721 			if ((cur_rx->bge_ip_csum ^ 0xffff) == 0)
2722 				m->m_pkthdr.csum_flags |= CSUM_IP_VALID;
2723 			if (cur_rx->bge_flags & BGE_RXBDFLAG_TCP_UDP_CSUM) {
2724 				m->m_pkthdr.csum_data =
2725 				    cur_rx->bge_tcp_udp_csum;
2726 				m->m_pkthdr.csum_flags |= CSUM_DATA_VALID;
2727 			}
2728 		}
2729 #endif
2730 
2731 		/*
2732 		 * If we received a packet with a vlan tag,
2733 		 * attach that information to the packet.
2734 		 */
2735 		if (have_tag)
2736 			VLAN_INPUT_TAG(ifp, m, vlan_tag, continue);
2737 
2738 		BGE_UNLOCK(sc);
2739 		(*ifp->if_input)(ifp, m);
2740 		BGE_LOCK(sc);
2741 	}
2742 
2743 	bus_dmamap_sync(sc->bge_cdata.bge_rx_return_ring_tag,
2744 	    sc->bge_cdata.bge_rx_return_ring_map, BUS_DMASYNC_PREWRITE);
2745 	bus_dmamap_sync(sc->bge_cdata.bge_rx_std_ring_tag,
2746 	    sc->bge_cdata.bge_rx_std_ring_map,
2747 	    BUS_DMASYNC_POSTREAD|BUS_DMASYNC_PREWRITE);
2748 	if (sc->bge_asicrev != BGE_ASICREV_BCM5705) {
2749 		bus_dmamap_sync(sc->bge_cdata.bge_rx_jumbo_ring_tag,
2750 		    sc->bge_cdata.bge_rx_jumbo_ring_map,
2751 		    BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
2752 	}
2753 
2754 	CSR_WRITE_4(sc, BGE_MBX_RX_CONS0_LO, sc->bge_rx_saved_considx);
2755 	if (stdcnt)
2756 		CSR_WRITE_4(sc, BGE_MBX_RX_STD_PROD_LO, sc->bge_std);
2757 	if (jumbocnt)
2758 		CSR_WRITE_4(sc, BGE_MBX_RX_JUMBO_PROD_LO, sc->bge_jumbo);
2759 
2760 	return;
2761 }
2762 
2763 static void
2764 bge_txeof(sc)
2765 	struct bge_softc *sc;
2766 {
2767 	struct bge_tx_bd *cur_tx = NULL;
2768 	struct ifnet *ifp;
2769 
2770 	BGE_LOCK_ASSERT(sc);
2771 
2772 	ifp = &sc->arpcom.ac_if;
2773 
2774 	/*
2775 	 * Go through our tx ring and free mbufs for those
2776 	 * frames that have been sent.
2777 	 */
2778 	while (sc->bge_tx_saved_considx !=
2779 	    sc->bge_ldata.bge_status_block->bge_idx[0].bge_tx_cons_idx) {
2780 		u_int32_t		idx = 0;
2781 
2782 		idx = sc->bge_tx_saved_considx;
2783 		cur_tx = &sc->bge_ldata.bge_tx_ring[idx];
2784 		if (cur_tx->bge_flags & BGE_TXBDFLAG_END)
2785 			ifp->if_opackets++;
2786 		if (sc->bge_cdata.bge_tx_chain[idx] != NULL) {
2787 			m_freem(sc->bge_cdata.bge_tx_chain[idx]);
2788 			sc->bge_cdata.bge_tx_chain[idx] = NULL;
2789 			bus_dmamap_unload(sc->bge_cdata.bge_mtag,
2790 			    sc->bge_cdata.bge_tx_dmamap[idx]);
2791 		}
2792 		sc->bge_txcnt--;
2793 		BGE_INC(sc->bge_tx_saved_considx, BGE_TX_RING_CNT);
2794 		ifp->if_timer = 0;
2795 	}
2796 
2797 	if (cur_tx != NULL)
2798 		ifp->if_flags &= ~IFF_OACTIVE;
2799 
2800 	return;
2801 }
2802 
2803 static void
2804 bge_intr(xsc)
2805 	void *xsc;
2806 {
2807 	struct bge_softc *sc;
2808 	struct ifnet *ifp;
2809 	u_int32_t statusword;
2810 	u_int32_t status;
2811 
2812 	sc = xsc;
2813 	ifp = &sc->arpcom.ac_if;
2814 
2815 	BGE_LOCK(sc);
2816 
2817 	bus_dmamap_sync(sc->bge_cdata.bge_status_tag,
2818 	    sc->bge_cdata.bge_status_map, BUS_DMASYNC_POSTWRITE);
2819 
2820 	statusword =
2821 	    atomic_readandclear_32(&sc->bge_ldata.bge_status_block->bge_status);
2822 
2823 #ifdef notdef
2824 	/* Avoid this for now -- checking this register is expensive. */
2825 	/* Make sure this is really our interrupt. */
2826 	if (!(CSR_READ_4(sc, BGE_MISC_LOCAL_CTL) & BGE_MLC_INTR_STATE))
2827 		return;
2828 #endif
2829 	/* Ack interrupt and stop others from occuring. */
2830 	CSR_WRITE_4(sc, BGE_MBX_IRQ0_LO, 1);
2831 
2832 	/*
2833 	 * Process link state changes.
2834 	 * Grrr. The link status word in the status block does
2835 	 * not work correctly on the BCM5700 rev AX and BX chips,
2836 	 * according to all available information. Hence, we have
2837 	 * to enable MII interrupts in order to properly obtain
2838 	 * async link changes. Unfortunately, this also means that
2839 	 * we have to read the MAC status register to detect link
2840 	 * changes, thereby adding an additional register access to
2841 	 * the interrupt handler.
2842 	 */
2843 
2844 	if (sc->bge_asicrev == BGE_ASICREV_BCM5700) {
2845 
2846 		status = CSR_READ_4(sc, BGE_MAC_STS);
2847 		if (status & BGE_MACSTAT_MI_INTERRUPT) {
2848 			sc->bge_link = 0;
2849 			callout_stop(&sc->bge_stat_ch);
2850 			bge_tick_locked(sc);
2851 			/* Clear the interrupt */
2852 			CSR_WRITE_4(sc, BGE_MAC_EVT_ENB,
2853 			    BGE_EVTENB_MI_INTERRUPT);
2854 			bge_miibus_readreg(sc->bge_dev, 1, BRGPHY_MII_ISR);
2855 			bge_miibus_writereg(sc->bge_dev, 1, BRGPHY_MII_IMR,
2856 			    BRGPHY_INTRS);
2857 		}
2858 	} else {
2859 		if (statusword & BGE_STATFLAG_LINKSTATE_CHANGED) {
2860 			/*
2861 			 * Sometimes PCS encoding errors are detected in
2862 			 * TBI mode (on fiber NICs), and for some reason
2863 			 * the chip will signal them as link changes.
2864 			 * If we get a link change event, but the 'PCS
2865 			 * encoding error' bit in the MAC status register
2866 			 * is set, don't bother doing a link check.
2867 			 * This avoids spurious "gigabit link up" messages
2868 			 * that sometimes appear on fiber NICs during
2869 			 * periods of heavy traffic. (There should be no
2870 			 * effect on copper NICs.)
2871 			 */
2872 			status = CSR_READ_4(sc, BGE_MAC_STS);
2873 			if (!(status & (BGE_MACSTAT_PORT_DECODE_ERROR|
2874 			    BGE_MACSTAT_MI_COMPLETE))) {
2875 				sc->bge_link = 0;
2876 				callout_stop(&sc->bge_stat_ch);
2877 				bge_tick_locked(sc);
2878 			}
2879 			/* Clear the interrupt */
2880 			CSR_WRITE_4(sc, BGE_MAC_STS, BGE_MACSTAT_SYNC_CHANGED|
2881 			    BGE_MACSTAT_CFG_CHANGED|BGE_MACSTAT_MI_COMPLETE|
2882 			    BGE_MACSTAT_LINK_CHANGED);
2883 
2884 			/* Force flush the status block cached by PCI bridge */
2885 			CSR_READ_4(sc, BGE_MBX_IRQ0_LO);
2886 		}
2887 	}
2888 
2889 	if (ifp->if_flags & IFF_RUNNING) {
2890 		/* Check RX return ring producer/consumer */
2891 		bge_rxeof(sc);
2892 
2893 		/* Check TX ring producer/consumer */
2894 		bge_txeof(sc);
2895 	}
2896 
2897 	bus_dmamap_sync(sc->bge_cdata.bge_status_tag,
2898 	    sc->bge_cdata.bge_status_map, BUS_DMASYNC_PREWRITE);
2899 
2900 	bge_handle_events(sc);
2901 
2902 	/* Re-enable interrupts. */
2903 	CSR_WRITE_4(sc, BGE_MBX_IRQ0_LO, 0);
2904 
2905 	if (ifp->if_flags & IFF_RUNNING && ifp->if_snd.ifq_head != NULL)
2906 		bge_start_locked(ifp);
2907 
2908 	BGE_UNLOCK(sc);
2909 
2910 	return;
2911 }
2912 
2913 static void
2914 bge_tick_locked(sc)
2915 	struct bge_softc *sc;
2916 {
2917 	struct mii_data *mii = NULL;
2918 	struct ifmedia *ifm = NULL;
2919 	struct ifnet *ifp;
2920 
2921 	ifp = &sc->arpcom.ac_if;
2922 
2923 	BGE_LOCK_ASSERT(sc);
2924 
2925 	if (sc->bge_asicrev == BGE_ASICREV_BCM5705)
2926 		bge_stats_update_regs(sc);
2927 	else
2928 		bge_stats_update(sc);
2929 	callout_reset(&sc->bge_stat_ch, hz, bge_tick, sc);
2930 	if (sc->bge_link)
2931 		return;
2932 
2933 	if (sc->bge_tbi) {
2934 		ifm = &sc->bge_ifmedia;
2935 		if (CSR_READ_4(sc, BGE_MAC_STS) &
2936 		    BGE_MACSTAT_TBI_PCS_SYNCHED) {
2937 			sc->bge_link++;
2938 			CSR_WRITE_4(sc, BGE_MAC_STS, 0xFFFFFFFF);
2939 			printf("bge%d: gigabit link up\n", sc->bge_unit);
2940 			if (ifp->if_snd.ifq_head != NULL)
2941 				bge_start_locked(ifp);
2942 		}
2943 		return;
2944 	}
2945 
2946 	mii = device_get_softc(sc->bge_miibus);
2947 	mii_tick(mii);
2948 
2949 	if (!sc->bge_link && mii->mii_media_status & IFM_ACTIVE &&
2950 	    IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE) {
2951 		sc->bge_link++;
2952 		if (IFM_SUBTYPE(mii->mii_media_active) == IFM_1000_T ||
2953 		    IFM_SUBTYPE(mii->mii_media_active) == IFM_1000_SX)
2954 			printf("bge%d: gigabit link up\n",
2955 			   sc->bge_unit);
2956 		if (ifp->if_snd.ifq_head != NULL)
2957 			bge_start_locked(ifp);
2958 	}
2959 
2960 	return;
2961 }
2962 
2963 static void
2964 bge_tick(xsc)
2965 	void *xsc;
2966 {
2967 	struct bge_softc *sc;
2968 
2969 	sc = xsc;
2970 
2971 	BGE_LOCK(sc);
2972 	bge_tick_locked(sc);
2973 	BGE_UNLOCK(sc);
2974 }
2975 
2976 static void
2977 bge_stats_update_regs(sc)
2978 	struct bge_softc *sc;
2979 {
2980 	struct ifnet *ifp;
2981 	struct bge_mac_stats_regs stats;
2982 	u_int32_t *s;
2983 	int i;
2984 
2985 	ifp = &sc->arpcom.ac_if;
2986 
2987 	s = (u_int32_t *)&stats;
2988 	for (i = 0; i < sizeof(struct bge_mac_stats_regs); i += 4) {
2989 		*s = CSR_READ_4(sc, BGE_RX_STATS + i);
2990 		s++;
2991 	}
2992 
2993 	ifp->if_collisions +=
2994 	   (stats.dot3StatsSingleCollisionFrames +
2995 	   stats.dot3StatsMultipleCollisionFrames +
2996 	   stats.dot3StatsExcessiveCollisions +
2997 	   stats.dot3StatsLateCollisions) -
2998 	   ifp->if_collisions;
2999 
3000 	return;
3001 }
3002 
3003 static void
3004 bge_stats_update(sc)
3005 	struct bge_softc *sc;
3006 {
3007 	struct ifnet *ifp;
3008 	struct bge_stats *stats;
3009 
3010 	ifp = &sc->arpcom.ac_if;
3011 
3012 	stats = (struct bge_stats *)(sc->bge_vhandle +
3013 	    BGE_MEMWIN_START + BGE_STATS_BLOCK);
3014 
3015 	ifp->if_collisions +=
3016 	   (stats->txstats.dot3StatsSingleCollisionFrames.bge_addr_lo +
3017 	   stats->txstats.dot3StatsMultipleCollisionFrames.bge_addr_lo +
3018 	   stats->txstats.dot3StatsExcessiveCollisions.bge_addr_lo +
3019 	   stats->txstats.dot3StatsLateCollisions.bge_addr_lo) -
3020 	   ifp->if_collisions;
3021 
3022 #ifdef notdef
3023 	ifp->if_collisions +=
3024 	   (sc->bge_rdata->bge_info.bge_stats.dot3StatsSingleCollisionFrames +
3025 	   sc->bge_rdata->bge_info.bge_stats.dot3StatsMultipleCollisionFrames +
3026 	   sc->bge_rdata->bge_info.bge_stats.dot3StatsExcessiveCollisions +
3027 	   sc->bge_rdata->bge_info.bge_stats.dot3StatsLateCollisions) -
3028 	   ifp->if_collisions;
3029 #endif
3030 
3031 	return;
3032 }
3033 
3034 /*
3035  * Encapsulate an mbuf chain in the tx ring  by coupling the mbuf data
3036  * pointers to descriptors.
3037  */
3038 static int
3039 bge_encap(sc, m_head, txidx)
3040 	struct bge_softc *sc;
3041 	struct mbuf *m_head;
3042 	u_int32_t *txidx;
3043 {
3044 	struct bge_tx_bd	*f = NULL;
3045 	u_int16_t		csum_flags = 0;
3046 	struct m_tag		*mtag;
3047 	struct bge_dmamap_arg	ctx;
3048 	bus_dmamap_t		map;
3049 	int			error;
3050 
3051 
3052 	if (m_head->m_pkthdr.csum_flags) {
3053 		if (m_head->m_pkthdr.csum_flags & CSUM_IP)
3054 			csum_flags |= BGE_TXBDFLAG_IP_CSUM;
3055 		if (m_head->m_pkthdr.csum_flags & (CSUM_TCP | CSUM_UDP))
3056 			csum_flags |= BGE_TXBDFLAG_TCP_UDP_CSUM;
3057 		if (m_head->m_flags & M_LASTFRAG)
3058 			csum_flags |= BGE_TXBDFLAG_IP_FRAG_END;
3059 		else if (m_head->m_flags & M_FRAG)
3060 			csum_flags |= BGE_TXBDFLAG_IP_FRAG;
3061 	}
3062 
3063 	mtag = VLAN_OUTPUT_TAG(&sc->arpcom.ac_if, m_head);
3064 
3065 	ctx.sc = sc;
3066 	ctx.bge_idx = *txidx;
3067 	ctx.bge_ring = sc->bge_ldata.bge_tx_ring;
3068 	ctx.bge_flags = csum_flags;
3069 	/*
3070 	 * Sanity check: avoid coming within 16 descriptors
3071 	 * of the end of the ring.
3072 	 */
3073 	ctx.bge_maxsegs = (BGE_TX_RING_CNT - sc->bge_txcnt) - 16;
3074 
3075 	map = sc->bge_cdata.bge_tx_dmamap[*txidx];
3076 	error = bus_dmamap_load_mbuf(sc->bge_cdata.bge_mtag, map,
3077 	    m_head, bge_dma_map_tx_desc, &ctx, BUS_DMA_NOWAIT);
3078 
3079 	if (error || ctx.bge_maxsegs == 0 /*||
3080 	    ctx.bge_idx == sc->bge_tx_saved_considx*/)
3081 		return (ENOBUFS);
3082 
3083 	/*
3084 	 * Insure that the map for this transmission
3085 	 * is placed at the array index of the last descriptor
3086 	 * in this chain.
3087 	 */
3088 	sc->bge_cdata.bge_tx_dmamap[*txidx] =
3089 	    sc->bge_cdata.bge_tx_dmamap[ctx.bge_idx];
3090 	sc->bge_cdata.bge_tx_dmamap[ctx.bge_idx] = map;
3091 	sc->bge_cdata.bge_tx_chain[ctx.bge_idx] = m_head;
3092 	sc->bge_txcnt += ctx.bge_maxsegs;
3093 	f = &sc->bge_ldata.bge_tx_ring[*txidx];
3094 	if (mtag != NULL) {
3095 		f->bge_flags |= htole16(BGE_TXBDFLAG_VLAN_TAG);
3096 		f->bge_vlan_tag = htole16(VLAN_TAG_VALUE(mtag));
3097 	} else {
3098 		f->bge_vlan_tag = 0;
3099 	}
3100 
3101 	BGE_INC(ctx.bge_idx, BGE_TX_RING_CNT);
3102 	*txidx = ctx.bge_idx;
3103 
3104 	return(0);
3105 }
3106 
3107 /*
3108  * Main transmit routine. To avoid having to do mbuf copies, we put pointers
3109  * to the mbuf data regions directly in the transmit descriptors.
3110  */
3111 static void
3112 bge_start_locked(ifp)
3113 	struct ifnet *ifp;
3114 {
3115 	struct bge_softc *sc;
3116 	struct mbuf *m_head = NULL;
3117 	u_int32_t prodidx = 0;
3118 
3119 	sc = ifp->if_softc;
3120 
3121 	if (!sc->bge_link && ifp->if_snd.ifq_len < 10)
3122 		return;
3123 
3124 	prodidx = CSR_READ_4(sc, BGE_MBX_TX_HOST_PROD0_LO);
3125 
3126 	while(sc->bge_cdata.bge_tx_chain[prodidx] == NULL) {
3127 		IF_DEQUEUE(&ifp->if_snd, m_head);
3128 		if (m_head == NULL)
3129 			break;
3130 
3131 		/*
3132 		 * XXX
3133 		 * safety overkill.  If this is a fragmented packet chain
3134 		 * with delayed TCP/UDP checksums, then only encapsulate
3135 		 * it if we have enough descriptors to handle the entire
3136 		 * chain at once.
3137 		 * (paranoia -- may not actually be needed)
3138 		 */
3139 		if (m_head->m_flags & M_FIRSTFRAG &&
3140 		    m_head->m_pkthdr.csum_flags & (CSUM_DELAY_DATA)) {
3141 			if ((BGE_TX_RING_CNT - sc->bge_txcnt) <
3142 			    m_head->m_pkthdr.csum_data + 16) {
3143 				IF_PREPEND(&ifp->if_snd, m_head);
3144 				ifp->if_flags |= IFF_OACTIVE;
3145 				break;
3146 			}
3147 		}
3148 
3149 		/*
3150 		 * Pack the data into the transmit ring. If we
3151 		 * don't have room, set the OACTIVE flag and wait
3152 		 * for the NIC to drain the ring.
3153 		 */
3154 		if (bge_encap(sc, m_head, &prodidx)) {
3155 			IF_PREPEND(&ifp->if_snd, m_head);
3156 			ifp->if_flags |= IFF_OACTIVE;
3157 			break;
3158 		}
3159 
3160 		/*
3161 		 * If there's a BPF listener, bounce a copy of this frame
3162 		 * to him.
3163 		 */
3164 		BPF_MTAP(ifp, m_head);
3165 	}
3166 
3167 	/* Transmit */
3168 	CSR_WRITE_4(sc, BGE_MBX_TX_HOST_PROD0_LO, prodidx);
3169 	/* 5700 b2 errata */
3170 	if (sc->bge_chiprev == BGE_CHIPREV_5700_BX)
3171 		CSR_WRITE_4(sc, BGE_MBX_TX_HOST_PROD0_LO, prodidx);
3172 
3173 	/*
3174 	 * Set a timeout in case the chip goes out to lunch.
3175 	 */
3176 	ifp->if_timer = 5;
3177 
3178 	return;
3179 }
3180 
3181 /*
3182  * Main transmit routine. To avoid having to do mbuf copies, we put pointers
3183  * to the mbuf data regions directly in the transmit descriptors.
3184  */
3185 static void
3186 bge_start(ifp)
3187 	struct ifnet *ifp;
3188 {
3189 	struct bge_softc *sc;
3190 
3191 	sc = ifp->if_softc;
3192 	BGE_LOCK(sc);
3193 	bge_start_locked(ifp);
3194 	BGE_UNLOCK(sc);
3195 }
3196 
3197 static void
3198 bge_init_locked(sc)
3199 	struct bge_softc *sc;
3200 {
3201 	struct ifnet *ifp;
3202 	u_int16_t *m;
3203 
3204 	BGE_LOCK_ASSERT(sc);
3205 
3206 	ifp = &sc->arpcom.ac_if;
3207 
3208 	if (ifp->if_flags & IFF_RUNNING)
3209 		return;
3210 
3211 	/* Cancel pending I/O and flush buffers. */
3212 	bge_stop(sc);
3213 	bge_reset(sc);
3214 	bge_chipinit(sc);
3215 
3216 	/*
3217 	 * Init the various state machines, ring
3218 	 * control blocks and firmware.
3219 	 */
3220 	if (bge_blockinit(sc)) {
3221 		printf("bge%d: initialization failure\n", sc->bge_unit);
3222 		return;
3223 	}
3224 
3225 	ifp = &sc->arpcom.ac_if;
3226 
3227 	/* Specify MTU. */
3228 	CSR_WRITE_4(sc, BGE_RX_MTU, ifp->if_mtu +
3229 	    ETHER_HDR_LEN + ETHER_CRC_LEN + ETHER_VLAN_ENCAP_LEN);
3230 
3231 	/* Load our MAC address. */
3232 	m = (u_int16_t *)&sc->arpcom.ac_enaddr[0];
3233 	CSR_WRITE_4(sc, BGE_MAC_ADDR1_LO, htons(m[0]));
3234 	CSR_WRITE_4(sc, BGE_MAC_ADDR1_HI, (htons(m[1]) << 16) | htons(m[2]));
3235 
3236 	/* Enable or disable promiscuous mode as needed. */
3237 	if (ifp->if_flags & IFF_PROMISC) {
3238 		BGE_SETBIT(sc, BGE_RX_MODE, BGE_RXMODE_RX_PROMISC);
3239 	} else {
3240 		BGE_CLRBIT(sc, BGE_RX_MODE, BGE_RXMODE_RX_PROMISC);
3241 	}
3242 
3243 	/* Program multicast filter. */
3244 	bge_setmulti(sc);
3245 
3246 	/* Init RX ring. */
3247 	bge_init_rx_ring_std(sc);
3248 
3249 	/*
3250 	 * Workaround for a bug in 5705 ASIC rev A0. Poll the NIC's
3251 	 * memory to insure that the chip has in fact read the first
3252 	 * entry of the ring.
3253 	 */
3254 	if (sc->bge_chipid == BGE_CHIPID_BCM5705_A0) {
3255 		u_int32_t		v, i;
3256 		for (i = 0; i < 10; i++) {
3257 			DELAY(20);
3258 			v = bge_readmem_ind(sc, BGE_STD_RX_RINGS + 8);
3259 			if (v == (MCLBYTES - ETHER_ALIGN))
3260 				break;
3261 		}
3262 		if (i == 10)
3263 			printf ("bge%d: 5705 A0 chip failed to load RX ring\n",
3264 			    sc->bge_unit);
3265 	}
3266 
3267 	/* Init jumbo RX ring. */
3268 	if (ifp->if_mtu > (ETHERMTU + ETHER_HDR_LEN + ETHER_CRC_LEN))
3269 		bge_init_rx_ring_jumbo(sc);
3270 
3271 	/* Init our RX return ring index */
3272 	sc->bge_rx_saved_considx = 0;
3273 
3274 	/* Init TX ring. */
3275 	bge_init_tx_ring(sc);
3276 
3277 	/* Turn on transmitter */
3278 	BGE_SETBIT(sc, BGE_TX_MODE, BGE_TXMODE_ENABLE);
3279 
3280 	/* Turn on receiver */
3281 	BGE_SETBIT(sc, BGE_RX_MODE, BGE_RXMODE_ENABLE);
3282 
3283 	/* Tell firmware we're alive. */
3284 	BGE_SETBIT(sc, BGE_MODE_CTL, BGE_MODECTL_STACKUP);
3285 
3286 	/* Enable host interrupts. */
3287 	BGE_SETBIT(sc, BGE_PCI_MISC_CTL, BGE_PCIMISCCTL_CLEAR_INTA);
3288 	BGE_CLRBIT(sc, BGE_PCI_MISC_CTL, BGE_PCIMISCCTL_MASK_PCI_INTR);
3289 	CSR_WRITE_4(sc, BGE_MBX_IRQ0_LO, 0);
3290 
3291 	bge_ifmedia_upd(ifp);
3292 
3293 	ifp->if_flags |= IFF_RUNNING;
3294 	ifp->if_flags &= ~IFF_OACTIVE;
3295 
3296 	callout_reset(&sc->bge_stat_ch, hz, bge_tick, sc);
3297 
3298 	return;
3299 }
3300 
3301 static void
3302 bge_init(xsc)
3303 	void *xsc;
3304 {
3305 	struct bge_softc *sc = xsc;
3306 
3307 	BGE_LOCK(sc);
3308 	bge_init_locked(sc);
3309 	BGE_UNLOCK(sc);
3310 
3311 	return;
3312 }
3313 
3314 /*
3315  * Set media options.
3316  */
3317 static int
3318 bge_ifmedia_upd(ifp)
3319 	struct ifnet *ifp;
3320 {
3321 	struct bge_softc *sc;
3322 	struct mii_data *mii;
3323 	struct ifmedia *ifm;
3324 
3325 	sc = ifp->if_softc;
3326 	ifm = &sc->bge_ifmedia;
3327 
3328 	/* If this is a 1000baseX NIC, enable the TBI port. */
3329 	if (sc->bge_tbi) {
3330 		if (IFM_TYPE(ifm->ifm_media) != IFM_ETHER)
3331 			return(EINVAL);
3332 		switch(IFM_SUBTYPE(ifm->ifm_media)) {
3333 		case IFM_AUTO:
3334 			break;
3335 		case IFM_1000_SX:
3336 			if ((ifm->ifm_media & IFM_GMASK) == IFM_FDX) {
3337 				BGE_CLRBIT(sc, BGE_MAC_MODE,
3338 				    BGE_MACMODE_HALF_DUPLEX);
3339 			} else {
3340 				BGE_SETBIT(sc, BGE_MAC_MODE,
3341 				    BGE_MACMODE_HALF_DUPLEX);
3342 			}
3343 			break;
3344 		default:
3345 			return(EINVAL);
3346 		}
3347 		return(0);
3348 	}
3349 
3350 	mii = device_get_softc(sc->bge_miibus);
3351 	sc->bge_link = 0;
3352 	if (mii->mii_instance) {
3353 		struct mii_softc *miisc;
3354 		for (miisc = LIST_FIRST(&mii->mii_phys); miisc != NULL;
3355 		    miisc = LIST_NEXT(miisc, mii_list))
3356 			mii_phy_reset(miisc);
3357 	}
3358 	mii_mediachg(mii);
3359 
3360 	return(0);
3361 }
3362 
3363 /*
3364  * Report current media status.
3365  */
3366 static void
3367 bge_ifmedia_sts(ifp, ifmr)
3368 	struct ifnet *ifp;
3369 	struct ifmediareq *ifmr;
3370 {
3371 	struct bge_softc *sc;
3372 	struct mii_data *mii;
3373 
3374 	sc = ifp->if_softc;
3375 
3376 	if (sc->bge_tbi) {
3377 		ifmr->ifm_status = IFM_AVALID;
3378 		ifmr->ifm_active = IFM_ETHER;
3379 		if (CSR_READ_4(sc, BGE_MAC_STS) &
3380 		    BGE_MACSTAT_TBI_PCS_SYNCHED)
3381 			ifmr->ifm_status |= IFM_ACTIVE;
3382 		ifmr->ifm_active |= IFM_1000_SX;
3383 		if (CSR_READ_4(sc, BGE_MAC_MODE) & BGE_MACMODE_HALF_DUPLEX)
3384 			ifmr->ifm_active |= IFM_HDX;
3385 		else
3386 			ifmr->ifm_active |= IFM_FDX;
3387 		return;
3388 	}
3389 
3390 	mii = device_get_softc(sc->bge_miibus);
3391 	mii_pollstat(mii);
3392 	ifmr->ifm_active = mii->mii_media_active;
3393 	ifmr->ifm_status = mii->mii_media_status;
3394 
3395 	return;
3396 }
3397 
3398 static int
3399 bge_ioctl(ifp, command, data)
3400 	struct ifnet *ifp;
3401 	u_long command;
3402 	caddr_t data;
3403 {
3404 	struct bge_softc *sc = ifp->if_softc;
3405 	struct ifreq *ifr = (struct ifreq *) data;
3406 	int mask, error = 0;
3407 	struct mii_data *mii;
3408 
3409 	switch(command) {
3410 	case SIOCSIFMTU:
3411 		/* Disallow jumbo frames on 5705. */
3412 		if ((sc->bge_asicrev == BGE_ASICREV_BCM5705 &&
3413 		    ifr->ifr_mtu > ETHERMTU) || ifr->ifr_mtu > BGE_JUMBO_MTU)
3414 			error = EINVAL;
3415 		else {
3416 			ifp->if_mtu = ifr->ifr_mtu;
3417 			ifp->if_flags &= ~IFF_RUNNING;
3418 			bge_init(sc);
3419 		}
3420 		break;
3421 	case SIOCSIFFLAGS:
3422 		BGE_LOCK(sc);
3423 		if (ifp->if_flags & IFF_UP) {
3424 			/*
3425 			 * If only the state of the PROMISC flag changed,
3426 			 * then just use the 'set promisc mode' command
3427 			 * instead of reinitializing the entire NIC. Doing
3428 			 * a full re-init means reloading the firmware and
3429 			 * waiting for it to start up, which may take a
3430 			 * second or two.
3431 			 */
3432 			if (ifp->if_flags & IFF_RUNNING &&
3433 			    ifp->if_flags & IFF_PROMISC &&
3434 			    !(sc->bge_if_flags & IFF_PROMISC)) {
3435 				BGE_SETBIT(sc, BGE_RX_MODE,
3436 				    BGE_RXMODE_RX_PROMISC);
3437 			} else if (ifp->if_flags & IFF_RUNNING &&
3438 			    !(ifp->if_flags & IFF_PROMISC) &&
3439 			    sc->bge_if_flags & IFF_PROMISC) {
3440 				BGE_CLRBIT(sc, BGE_RX_MODE,
3441 				    BGE_RXMODE_RX_PROMISC);
3442 			} else
3443 				bge_init_locked(sc);
3444 		} else {
3445 			if (ifp->if_flags & IFF_RUNNING) {
3446 				bge_stop(sc);
3447 			}
3448 		}
3449 		sc->bge_if_flags = ifp->if_flags;
3450 		BGE_UNLOCK(sc);
3451 		error = 0;
3452 		break;
3453 	case SIOCADDMULTI:
3454 	case SIOCDELMULTI:
3455 		if (ifp->if_flags & IFF_RUNNING) {
3456 			BGE_LOCK(sc);
3457 			bge_setmulti(sc);
3458 			BGE_UNLOCK(sc);
3459 			error = 0;
3460 		}
3461 		break;
3462 	case SIOCSIFMEDIA:
3463 	case SIOCGIFMEDIA:
3464 		if (sc->bge_tbi) {
3465 			error = ifmedia_ioctl(ifp, ifr,
3466 			    &sc->bge_ifmedia, command);
3467 		} else {
3468 			mii = device_get_softc(sc->bge_miibus);
3469 			error = ifmedia_ioctl(ifp, ifr,
3470 			    &mii->mii_media, command);
3471 		}
3472 		break;
3473         case SIOCSIFCAP:
3474 		mask = ifr->ifr_reqcap ^ ifp->if_capenable;
3475 		if (mask & IFCAP_HWCSUM) {
3476 			if (IFCAP_HWCSUM & ifp->if_capenable)
3477 				ifp->if_capenable &= ~IFCAP_HWCSUM;
3478 			else
3479 				ifp->if_capenable |= IFCAP_HWCSUM;
3480 		}
3481 		error = 0;
3482 		break;
3483 	default:
3484 		error = ether_ioctl(ifp, command, data);
3485 		break;
3486 	}
3487 
3488 	return(error);
3489 }
3490 
3491 static void
3492 bge_watchdog(ifp)
3493 	struct ifnet *ifp;
3494 {
3495 	struct bge_softc *sc;
3496 
3497 	sc = ifp->if_softc;
3498 
3499 	printf("bge%d: watchdog timeout -- resetting\n", sc->bge_unit);
3500 
3501 	ifp->if_flags &= ~IFF_RUNNING;
3502 	bge_init(sc);
3503 
3504 	ifp->if_oerrors++;
3505 
3506 	return;
3507 }
3508 
3509 /*
3510  * Stop the adapter and free any mbufs allocated to the
3511  * RX and TX lists.
3512  */
3513 static void
3514 bge_stop(sc)
3515 	struct bge_softc *sc;
3516 {
3517 	struct ifnet *ifp;
3518 	struct ifmedia_entry *ifm;
3519 	struct mii_data *mii = NULL;
3520 	int mtmp, itmp;
3521 
3522 	BGE_LOCK_ASSERT(sc);
3523 
3524 	ifp = &sc->arpcom.ac_if;
3525 
3526 	if (!sc->bge_tbi)
3527 		mii = device_get_softc(sc->bge_miibus);
3528 
3529 	callout_stop(&sc->bge_stat_ch);
3530 
3531 	/*
3532 	 * Disable all of the receiver blocks
3533 	 */
3534 	BGE_CLRBIT(sc, BGE_RX_MODE, BGE_RXMODE_ENABLE);
3535 	BGE_CLRBIT(sc, BGE_RBDI_MODE, BGE_RBDIMODE_ENABLE);
3536 	BGE_CLRBIT(sc, BGE_RXLP_MODE, BGE_RXLPMODE_ENABLE);
3537 	if (sc->bge_asicrev != BGE_ASICREV_BCM5705)
3538 		BGE_CLRBIT(sc, BGE_RXLS_MODE, BGE_RXLSMODE_ENABLE);
3539 	BGE_CLRBIT(sc, BGE_RDBDI_MODE, BGE_RBDIMODE_ENABLE);
3540 	BGE_CLRBIT(sc, BGE_RDC_MODE, BGE_RDCMODE_ENABLE);
3541 	BGE_CLRBIT(sc, BGE_RBDC_MODE, BGE_RBDCMODE_ENABLE);
3542 
3543 	/*
3544 	 * Disable all of the transmit blocks
3545 	 */
3546 	BGE_CLRBIT(sc, BGE_SRS_MODE, BGE_SRSMODE_ENABLE);
3547 	BGE_CLRBIT(sc, BGE_SBDI_MODE, BGE_SBDIMODE_ENABLE);
3548 	BGE_CLRBIT(sc, BGE_SDI_MODE, BGE_SDIMODE_ENABLE);
3549 	BGE_CLRBIT(sc, BGE_RDMA_MODE, BGE_RDMAMODE_ENABLE);
3550 	BGE_CLRBIT(sc, BGE_SDC_MODE, BGE_SDCMODE_ENABLE);
3551 	if (sc->bge_asicrev != BGE_ASICREV_BCM5705)
3552 		BGE_CLRBIT(sc, BGE_DMAC_MODE, BGE_DMACMODE_ENABLE);
3553 	BGE_CLRBIT(sc, BGE_SBDC_MODE, BGE_SBDCMODE_ENABLE);
3554 
3555 	/*
3556 	 * Shut down all of the memory managers and related
3557 	 * state machines.
3558 	 */
3559 	BGE_CLRBIT(sc, BGE_HCC_MODE, BGE_HCCMODE_ENABLE);
3560 	BGE_CLRBIT(sc, BGE_WDMA_MODE, BGE_WDMAMODE_ENABLE);
3561 	if (sc->bge_asicrev != BGE_ASICREV_BCM5705)
3562 		BGE_CLRBIT(sc, BGE_MBCF_MODE, BGE_MBCFMODE_ENABLE);
3563 	CSR_WRITE_4(sc, BGE_FTQ_RESET, 0xFFFFFFFF);
3564 	CSR_WRITE_4(sc, BGE_FTQ_RESET, 0);
3565 	if (sc->bge_asicrev != BGE_ASICREV_BCM5705) {
3566 		BGE_CLRBIT(sc, BGE_BMAN_MODE, BGE_BMANMODE_ENABLE);
3567 		BGE_CLRBIT(sc, BGE_MARB_MODE, BGE_MARBMODE_ENABLE);
3568 	}
3569 
3570 	/* Disable host interrupts. */
3571 	BGE_SETBIT(sc, BGE_PCI_MISC_CTL, BGE_PCIMISCCTL_MASK_PCI_INTR);
3572 	CSR_WRITE_4(sc, BGE_MBX_IRQ0_LO, 1);
3573 
3574 	/*
3575 	 * Tell firmware we're shutting down.
3576 	 */
3577 	BGE_CLRBIT(sc, BGE_MODE_CTL, BGE_MODECTL_STACKUP);
3578 
3579 	/* Free the RX lists. */
3580 	bge_free_rx_ring_std(sc);
3581 
3582 	/* Free jumbo RX list. */
3583 	if (sc->bge_asicrev != BGE_ASICREV_BCM5705)
3584 		bge_free_rx_ring_jumbo(sc);
3585 
3586 	/* Free TX buffers. */
3587 	bge_free_tx_ring(sc);
3588 
3589 	/*
3590 	 * Isolate/power down the PHY, but leave the media selection
3591 	 * unchanged so that things will be put back to normal when
3592 	 * we bring the interface back up.
3593 	 */
3594 	if (!sc->bge_tbi) {
3595 		itmp = ifp->if_flags;
3596 		ifp->if_flags |= IFF_UP;
3597 		ifm = mii->mii_media.ifm_cur;
3598 		mtmp = ifm->ifm_media;
3599 		ifm->ifm_media = IFM_ETHER|IFM_NONE;
3600 		mii_mediachg(mii);
3601 		ifm->ifm_media = mtmp;
3602 		ifp->if_flags = itmp;
3603 	}
3604 
3605 	sc->bge_link = 0;
3606 
3607 	sc->bge_tx_saved_considx = BGE_TXCONS_UNSET;
3608 
3609 	ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
3610 
3611 	return;
3612 }
3613 
3614 /*
3615  * Stop all chip I/O so that the kernel's probe routines don't
3616  * get confused by errant DMAs when rebooting.
3617  */
3618 static void
3619 bge_shutdown(dev)
3620 	device_t dev;
3621 {
3622 	struct bge_softc *sc;
3623 
3624 	sc = device_get_softc(dev);
3625 
3626 	BGE_LOCK(sc);
3627 	bge_stop(sc);
3628 	bge_reset(sc);
3629 	BGE_UNLOCK(sc);
3630 
3631 	return;
3632 }
3633